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 141da177e4SLinus Torvalds #include <linux/mm.h> 151da177e4SLinus Torvalds #include <linux/module.h> 165a0e3ad6STejun Heo #include <linux/gfp.h> 171da177e4SLinus Torvalds #include <linux/kernel_stat.h> 181da177e4SLinus Torvalds #include <linux/swap.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/highmem.h> 2270ddf637SAnton Vorontsov #include <linux/vmpressure.h> 23e129b5c2SAndrew Morton #include <linux/vmstat.h> 241da177e4SLinus Torvalds #include <linux/file.h> 251da177e4SLinus Torvalds #include <linux/writeback.h> 261da177e4SLinus Torvalds #include <linux/blkdev.h> 271da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 281da177e4SLinus Torvalds buffer_heads_over_limit */ 291da177e4SLinus Torvalds #include <linux/mm_inline.h> 301da177e4SLinus Torvalds #include <linux/backing-dev.h> 311da177e4SLinus Torvalds #include <linux/rmap.h> 321da177e4SLinus Torvalds #include <linux/topology.h> 331da177e4SLinus Torvalds #include <linux/cpu.h> 341da177e4SLinus Torvalds #include <linux/cpuset.h> 353e7d3449SMel Gorman #include <linux/compaction.h> 361da177e4SLinus Torvalds #include <linux/notifier.h> 371da177e4SLinus Torvalds #include <linux/rwsem.h> 38248a0301SRafael J. Wysocki #include <linux/delay.h> 393218ae14SYasunori Goto #include <linux/kthread.h> 407dfb7103SNigel Cunningham #include <linux/freezer.h> 4166e1707bSBalbir Singh #include <linux/memcontrol.h> 42873b4771SKeika Kobayashi #include <linux/delayacct.h> 43af936a16SLee Schermerhorn #include <linux/sysctl.h> 44929bea7cSKOSAKI Motohiro #include <linux/oom.h> 45268bb0ceSLinus Torvalds #include <linux/prefetch.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include <asm/tlbflush.h> 481da177e4SLinus Torvalds #include <asm/div64.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <linux/swapops.h> 51117aad1eSRafael Aquini #include <linux/balloon_compaction.h> 521da177e4SLinus Torvalds 530f8053a5SNick Piggin #include "internal.h" 540f8053a5SNick Piggin 5533906bc5SMel Gorman #define CREATE_TRACE_POINTS 5633906bc5SMel Gorman #include <trace/events/vmscan.h> 5733906bc5SMel Gorman 581da177e4SLinus Torvalds struct scan_control { 591da177e4SLinus Torvalds /* Incremented by the number of inactive pages that were scanned */ 601da177e4SLinus Torvalds unsigned long nr_scanned; 611da177e4SLinus Torvalds 62a79311c1SRik van Riel /* Number of pages freed so far during a call to shrink_zones() */ 63a79311c1SRik van Riel unsigned long nr_reclaimed; 64a79311c1SRik van Riel 6522fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6622fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6722fba335SKOSAKI Motohiro 687b51755cSKOSAKI Motohiro unsigned long hibernation_mode; 697b51755cSKOSAKI Motohiro 701da177e4SLinus Torvalds /* This context's GFP mask */ 716daa0e28SAl Viro gfp_t gfp_mask; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds int may_writepage; 741da177e4SLinus Torvalds 75a6dc60f8SJohannes Weiner /* Can mapped pages be reclaimed? */ 76a6dc60f8SJohannes Weiner int may_unmap; 77f1fd1067SChristoph Lameter 782e2e4259SKOSAKI Motohiro /* Can pages be swapped as part of reclaim? */ 792e2e4259SKOSAKI Motohiro int may_swap; 802e2e4259SKOSAKI Motohiro 815ad333ebSAndy Whitcroft int order; 8266e1707bSBalbir Singh 839e3b2f8cSKonstantin Khlebnikov /* Scan (total_size >> priority) pages at once */ 849e3b2f8cSKonstantin Khlebnikov int priority; 859e3b2f8cSKonstantin Khlebnikov 865f53e762SKOSAKI Motohiro /* 87f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 88f16015fbSJohannes Weiner * primary target of this reclaim invocation. 89f16015fbSJohannes Weiner */ 90f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 9166e1707bSBalbir Singh 92327c0e96SKAMEZAWA Hiroyuki /* 93327c0e96SKAMEZAWA Hiroyuki * Nodemask of nodes allowed by the caller. If NULL, all nodes 94327c0e96SKAMEZAWA Hiroyuki * are scanned. 95327c0e96SKAMEZAWA Hiroyuki */ 96327c0e96SKAMEZAWA Hiroyuki nodemask_t *nodemask; 971da177e4SLinus Torvalds }; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1021da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1031da177e4SLinus Torvalds do { \ 1041da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1051da177e4SLinus Torvalds struct page *prev; \ 1061da177e4SLinus Torvalds \ 1071da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1081da177e4SLinus Torvalds prefetch(&prev->_field); \ 1091da177e4SLinus Torvalds } \ 1101da177e4SLinus Torvalds } while (0) 1111da177e4SLinus Torvalds #else 1121da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1131da177e4SLinus Torvalds #endif 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1161da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1171da177e4SLinus Torvalds do { \ 1181da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1191da177e4SLinus Torvalds struct page *prev; \ 1201da177e4SLinus Torvalds \ 1211da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1221da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1231da177e4SLinus Torvalds } \ 1241da177e4SLinus Torvalds } while (0) 1251da177e4SLinus Torvalds #else 1261da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds int vm_swappiness = 60; 133b21e0b90SZhang Yanfei unsigned long vm_total_pages; /* The total number of pages which the VM controls */ 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1361da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1371da177e4SLinus Torvalds 138c255a458SAndrew Morton #ifdef CONFIG_MEMCG 13989b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14089b5fae5SJohannes Weiner { 141f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14289b5fae5SJohannes Weiner } 14391a45470SKAMEZAWA Hiroyuki #else 14489b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14589b5fae5SJohannes Weiner { 14689b5fae5SJohannes Weiner return true; 14789b5fae5SJohannes Weiner } 14891a45470SKAMEZAWA Hiroyuki #endif 14991a45470SKAMEZAWA Hiroyuki 1506e543d57SLisa Du unsigned long zone_reclaimable_pages(struct zone *zone) 1516e543d57SLisa Du { 1526e543d57SLisa Du int nr; 1536e543d57SLisa Du 1546e543d57SLisa Du nr = zone_page_state(zone, NR_ACTIVE_FILE) + 1556e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_FILE); 1566e543d57SLisa Du 1576e543d57SLisa Du if (get_nr_swap_pages() > 0) 1586e543d57SLisa Du nr += zone_page_state(zone, NR_ACTIVE_ANON) + 1596e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_ANON); 1606e543d57SLisa Du 1616e543d57SLisa Du return nr; 1626e543d57SLisa Du } 1636e543d57SLisa Du 1646e543d57SLisa Du bool zone_reclaimable(struct zone *zone) 1656e543d57SLisa Du { 1666e543d57SLisa Du return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 1676e543d57SLisa Du } 1686e543d57SLisa Du 1694d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 170c9f299d9SKOSAKI Motohiro { 171c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1724d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 173a3d8e054SKOSAKI Motohiro 174074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 175c9f299d9SKOSAKI Motohiro } 176c9f299d9SKOSAKI Motohiro 1771da177e4SLinus Torvalds /* 1781d3d4437SGlauber Costa * Add a shrinker callback to be called from the vm. 1791da177e4SLinus Torvalds */ 1801d3d4437SGlauber Costa int register_shrinker(struct shrinker *shrinker) 1811da177e4SLinus Torvalds { 1821d3d4437SGlauber Costa size_t size = sizeof(*shrinker->nr_deferred); 1831d3d4437SGlauber Costa 1841d3d4437SGlauber Costa /* 1851d3d4437SGlauber Costa * If we only have one possible node in the system anyway, save 1861d3d4437SGlauber Costa * ourselves the trouble and disable NUMA aware behavior. This way we 1871d3d4437SGlauber Costa * will save memory and some small loop time later. 1881d3d4437SGlauber Costa */ 1891d3d4437SGlauber Costa if (nr_node_ids == 1) 1901d3d4437SGlauber Costa shrinker->flags &= ~SHRINKER_NUMA_AWARE; 1911d3d4437SGlauber Costa 1921d3d4437SGlauber Costa if (shrinker->flags & SHRINKER_NUMA_AWARE) 1931d3d4437SGlauber Costa size *= nr_node_ids; 1941d3d4437SGlauber Costa 1951d3d4437SGlauber Costa shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); 1961d3d4437SGlauber Costa if (!shrinker->nr_deferred) 1971d3d4437SGlauber Costa return -ENOMEM; 1981d3d4437SGlauber Costa 1991da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2001da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 2011da177e4SLinus Torvalds up_write(&shrinker_rwsem); 2021d3d4437SGlauber Costa return 0; 2031da177e4SLinus Torvalds } 2048e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /* 2071da177e4SLinus Torvalds * Remove one 2081da177e4SLinus Torvalds */ 2098e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds down_write(&shrinker_rwsem); 2121da177e4SLinus Torvalds list_del(&shrinker->list); 2131da177e4SLinus Torvalds up_write(&shrinker_rwsem); 214ae393321SAndrew Vagin kfree(shrinker->nr_deferred); 2151da177e4SLinus Torvalds } 2168e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds #define SHRINK_BATCH 128 2191d3d4437SGlauber Costa 2201d3d4437SGlauber Costa static unsigned long 2211d3d4437SGlauber Costa shrink_slab_node(struct shrink_control *shrinkctl, struct shrinker *shrinker, 2221d3d4437SGlauber Costa unsigned long nr_pages_scanned, unsigned long lru_pages) 2231da177e4SLinus Torvalds { 22424f7c6b9SDave Chinner unsigned long freed = 0; 2251da177e4SLinus Torvalds unsigned long long delta; 226635697c6SKonstantin Khlebnikov long total_scan; 227635697c6SKonstantin Khlebnikov long max_pass; 228acf92b48SDave Chinner long nr; 229acf92b48SDave Chinner long new_nr; 2301d3d4437SGlauber Costa int nid = shrinkctl->nid; 231e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 232e9299f50SDave Chinner : SHRINK_BATCH; 2331da177e4SLinus Torvalds 23424f7c6b9SDave Chinner max_pass = shrinker->count_objects(shrinker, shrinkctl); 23524f7c6b9SDave Chinner if (max_pass == 0) 2361d3d4437SGlauber Costa return 0; 237635697c6SKonstantin Khlebnikov 238acf92b48SDave Chinner /* 239acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 240acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 241acf92b48SDave Chinner * don't also do this scanning work. 242acf92b48SDave Chinner */ 2431d3d4437SGlauber Costa nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0); 244acf92b48SDave Chinner 245acf92b48SDave Chinner total_scan = nr; 2461495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 247ea164d73SAndrea Arcangeli delta *= max_pass; 2481da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 249acf92b48SDave Chinner total_scan += delta; 250acf92b48SDave Chinner if (total_scan < 0) { 25124f7c6b9SDave Chinner printk(KERN_ERR 25224f7c6b9SDave Chinner "shrink_slab: %pF negative objects to delete nr=%ld\n", 253a0b02131SDave Chinner shrinker->scan_objects, total_scan); 254acf92b48SDave Chinner total_scan = max_pass; 255ea164d73SAndrea Arcangeli } 256ea164d73SAndrea Arcangeli 257ea164d73SAndrea Arcangeli /* 2583567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2593567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2603567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2613567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2623567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2633567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2643567b59aSDave Chinner * memory. 2653567b59aSDave Chinner * 2663567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2673567b59aSDave Chinner * a large delta change is calculated directly. 2683567b59aSDave Chinner */ 2693567b59aSDave Chinner if (delta < max_pass / 4) 2703567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2713567b59aSDave Chinner 2723567b59aSDave Chinner /* 273ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 274ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 275ea164d73SAndrea Arcangeli * freeable entries. 276ea164d73SAndrea Arcangeli */ 277acf92b48SDave Chinner if (total_scan > max_pass * 2) 278acf92b48SDave Chinner total_scan = max_pass * 2; 2791da177e4SLinus Torvalds 28024f7c6b9SDave Chinner trace_mm_shrink_slab_start(shrinker, shrinkctl, nr, 28109576073SDave Chinner nr_pages_scanned, lru_pages, 28209576073SDave Chinner max_pass, delta, total_scan); 28309576073SDave Chinner 284e9299f50SDave Chinner while (total_scan >= batch_size) { 28524f7c6b9SDave Chinner unsigned long ret; 2861da177e4SLinus Torvalds 28724f7c6b9SDave Chinner shrinkctl->nr_to_scan = batch_size; 28824f7c6b9SDave Chinner ret = shrinker->scan_objects(shrinker, shrinkctl); 28924f7c6b9SDave Chinner if (ret == SHRINK_STOP) 2901da177e4SLinus Torvalds break; 29124f7c6b9SDave Chinner freed += ret; 29224f7c6b9SDave Chinner 293e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 294e9299f50SDave Chinner total_scan -= batch_size; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds cond_resched(); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 299acf92b48SDave Chinner /* 300acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 301acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 302acf92b48SDave Chinner * scan, there is no need to do an update. 303acf92b48SDave Chinner */ 30483aeeadaSKonstantin Khlebnikov if (total_scan > 0) 30583aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 3061d3d4437SGlauber Costa &shrinker->nr_deferred[nid]); 30783aeeadaSKonstantin Khlebnikov else 3081d3d4437SGlauber Costa new_nr = atomic_long_read(&shrinker->nr_deferred[nid]); 309acf92b48SDave Chinner 31024f7c6b9SDave Chinner trace_mm_shrink_slab_end(shrinker, freed, nr, new_nr); 3111d3d4437SGlauber Costa return freed; 3121d3d4437SGlauber Costa } 3131d3d4437SGlauber Costa 3141d3d4437SGlauber Costa /* 3151d3d4437SGlauber Costa * Call the shrink functions to age shrinkable caches 3161d3d4437SGlauber Costa * 3171d3d4437SGlauber Costa * Here we assume it costs one seek to replace a lru page and that it also 3181d3d4437SGlauber Costa * takes a seek to recreate a cache object. With this in mind we age equal 3191d3d4437SGlauber Costa * percentages of the lru and ageable caches. This should balance the seeks 3201d3d4437SGlauber Costa * generated by these structures. 3211d3d4437SGlauber Costa * 3221d3d4437SGlauber Costa * If the vm encountered mapped pages on the LRU it increase the pressure on 3231d3d4437SGlauber Costa * slab to avoid swapping. 3241d3d4437SGlauber Costa * 3251d3d4437SGlauber Costa * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 3261d3d4437SGlauber Costa * 3271d3d4437SGlauber Costa * `lru_pages' represents the number of on-LRU pages in all the zones which 3281d3d4437SGlauber Costa * are eligible for the caller's allocation attempt. It is used for balancing 3291d3d4437SGlauber Costa * slab reclaim versus page reclaim. 3301d3d4437SGlauber Costa * 3311d3d4437SGlauber Costa * Returns the number of slab objects which we shrunk. 3321d3d4437SGlauber Costa */ 3331d3d4437SGlauber Costa unsigned long shrink_slab(struct shrink_control *shrinkctl, 3341d3d4437SGlauber Costa unsigned long nr_pages_scanned, 3351d3d4437SGlauber Costa unsigned long lru_pages) 3361d3d4437SGlauber Costa { 3371d3d4437SGlauber Costa struct shrinker *shrinker; 3381d3d4437SGlauber Costa unsigned long freed = 0; 3391d3d4437SGlauber Costa 3401d3d4437SGlauber Costa if (nr_pages_scanned == 0) 3411d3d4437SGlauber Costa nr_pages_scanned = SWAP_CLUSTER_MAX; 3421d3d4437SGlauber Costa 3431d3d4437SGlauber Costa if (!down_read_trylock(&shrinker_rwsem)) { 3441d3d4437SGlauber Costa /* 3451d3d4437SGlauber Costa * If we would return 0, our callers would understand that we 3461d3d4437SGlauber Costa * have nothing else to shrink and give up trying. By returning 3471d3d4437SGlauber Costa * 1 we keep it going and assume we'll be able to shrink next 3481d3d4437SGlauber Costa * time. 3491d3d4437SGlauber Costa */ 3501d3d4437SGlauber Costa freed = 1; 3511d3d4437SGlauber Costa goto out; 3521d3d4437SGlauber Costa } 3531d3d4437SGlauber Costa 3541d3d4437SGlauber Costa list_for_each_entry(shrinker, &shrinker_list, list) { 3551d3d4437SGlauber Costa for_each_node_mask(shrinkctl->nid, shrinkctl->nodes_to_scan) { 3561d3d4437SGlauber Costa if (!node_online(shrinkctl->nid)) 3571d3d4437SGlauber Costa continue; 3581d3d4437SGlauber Costa 3591d3d4437SGlauber Costa if (!(shrinker->flags & SHRINKER_NUMA_AWARE) && 3601d3d4437SGlauber Costa (shrinkctl->nid != 0)) 3611d3d4437SGlauber Costa break; 3621d3d4437SGlauber Costa 3631d3d4437SGlauber Costa freed += shrink_slab_node(shrinkctl, shrinker, 3641d3d4437SGlauber Costa nr_pages_scanned, lru_pages); 3651d3d4437SGlauber Costa 3661d3d4437SGlauber Costa } 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds up_read(&shrinker_rwsem); 369f06590bdSMinchan Kim out: 370f06590bdSMinchan Kim cond_resched(); 37124f7c6b9SDave Chinner return freed; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3751da177e4SLinus Torvalds { 376ceddc3a5SJohannes Weiner /* 377ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 378ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 379ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 380ceddc3a5SJohannes Weiner */ 381edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3821da177e4SLinus Torvalds } 3831da177e4SLinus Torvalds 3847d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3857d3579e8SKOSAKI Motohiro struct scan_control *sc) 3861da177e4SLinus Torvalds { 387930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3881da177e4SLinus Torvalds return 1; 3891da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3901da177e4SLinus Torvalds return 1; 3911da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3921da177e4SLinus Torvalds return 1; 3931da177e4SLinus Torvalds return 0; 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /* 3971da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3981da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3991da177e4SLinus Torvalds * fsync(), msync() or close(). 4001da177e4SLinus Torvalds * 4011da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 4021da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 4031da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 4041da177e4SLinus Torvalds * 4051da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 4061da177e4SLinus Torvalds * __GFP_FS. 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 4091da177e4SLinus Torvalds struct page *page, int error) 4101da177e4SLinus Torvalds { 4117eaceaccSJens Axboe lock_page(page); 4123e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 4133e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 4141da177e4SLinus Torvalds unlock_page(page); 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 41704e62a29SChristoph Lameter /* possible outcome of pageout() */ 41804e62a29SChristoph Lameter typedef enum { 41904e62a29SChristoph Lameter /* failed to write page out, page is locked */ 42004e62a29SChristoph Lameter PAGE_KEEP, 42104e62a29SChristoph Lameter /* move page to the active list, page is locked */ 42204e62a29SChristoph Lameter PAGE_ACTIVATE, 42304e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 42404e62a29SChristoph Lameter PAGE_SUCCESS, 42504e62a29SChristoph Lameter /* page is clean and locked */ 42604e62a29SChristoph Lameter PAGE_CLEAN, 42704e62a29SChristoph Lameter } pageout_t; 42804e62a29SChristoph Lameter 4291da177e4SLinus Torvalds /* 4301742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 4311742f19fSAndrew Morton * Calls ->writepage(). 4321da177e4SLinus Torvalds */ 433c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 4347d3579e8SKOSAKI Motohiro struct scan_control *sc) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 4381da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 4391da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 4401da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 4411da177e4SLinus Torvalds * PagePrivate for that. 4421da177e4SLinus Torvalds * 4436aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 4441da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 4451da177e4SLinus Torvalds * will block. 4461da177e4SLinus Torvalds * 4471da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4481da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4491da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4501da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4531da177e4SLinus Torvalds return PAGE_KEEP; 4541da177e4SLinus Torvalds if (!mapping) { 4551da177e4SLinus Torvalds /* 4561da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4571da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4581da177e4SLinus Torvalds */ 459266cf658SDavid Howells if (page_has_private(page)) { 4601da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4611da177e4SLinus Torvalds ClearPageDirty(page); 462d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4631da177e4SLinus Torvalds return PAGE_CLEAN; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds } 4661da177e4SLinus Torvalds return PAGE_KEEP; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4691da177e4SLinus Torvalds return PAGE_ACTIVATE; 4700e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4711da177e4SLinus Torvalds return PAGE_KEEP; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4741da177e4SLinus Torvalds int res; 4751da177e4SLinus Torvalds struct writeback_control wbc = { 4761da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4771da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 478111ebb6eSOGAWA Hirofumi .range_start = 0, 479111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4801da177e4SLinus Torvalds .for_reclaim = 1, 4811da177e4SLinus Torvalds }; 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds SetPageReclaim(page); 4841da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4851da177e4SLinus Torvalds if (res < 0) 4861da177e4SLinus Torvalds handle_write_error(mapping, page, res); 487994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4881da177e4SLinus Torvalds ClearPageReclaim(page); 4891da177e4SLinus Torvalds return PAGE_ACTIVATE; 4901da177e4SLinus Torvalds } 491c661b078SAndy Whitcroft 4921da177e4SLinus Torvalds if (!PageWriteback(page)) { 4931da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4941da177e4SLinus Torvalds ClearPageReclaim(page); 4951da177e4SLinus Torvalds } 49623b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 497e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4981da177e4SLinus Torvalds return PAGE_SUCCESS; 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds return PAGE_CLEAN; 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 504a649fd92SAndrew Morton /* 505e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 506e286781dSNick Piggin * gets returned with a refcount of 0. 507a649fd92SAndrew Morton */ 508e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 50949d2e9ccSChristoph Lameter { 51028e4d965SNick Piggin BUG_ON(!PageLocked(page)); 51128e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 51249d2e9ccSChristoph Lameter 51319fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 51449d2e9ccSChristoph Lameter /* 5150fd0e6b0SNick Piggin * The non racy check for a busy page. 5160fd0e6b0SNick Piggin * 5170fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 5180fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 5190fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 5200fd0e6b0SNick Piggin * here, then the following race may occur: 5210fd0e6b0SNick Piggin * 5220fd0e6b0SNick Piggin * get_user_pages(&page); 5230fd0e6b0SNick Piggin * [user mapping goes away] 5240fd0e6b0SNick Piggin * write_to(page); 5250fd0e6b0SNick Piggin * !PageDirty(page) [good] 5260fd0e6b0SNick Piggin * SetPageDirty(page); 5270fd0e6b0SNick Piggin * put_page(page); 5280fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 5290fd0e6b0SNick Piggin * 5300fd0e6b0SNick Piggin * [oops, our write_to data is lost] 5310fd0e6b0SNick Piggin * 5320fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 5330fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 5340fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 5350fd0e6b0SNick Piggin * 5360fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 5370fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 53849d2e9ccSChristoph Lameter */ 539e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 54049d2e9ccSChristoph Lameter goto cannot_free; 541e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 542e286781dSNick Piggin if (unlikely(PageDirty(page))) { 543e286781dSNick Piggin page_unfreeze_refs(page, 2); 54449d2e9ccSChristoph Lameter goto cannot_free; 545e286781dSNick Piggin } 54649d2e9ccSChristoph Lameter 54749d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 54849d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 54949d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 55019fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 551cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 552e286781dSNick Piggin } else { 5536072d13cSLinus Torvalds void (*freepage)(struct page *); 5546072d13cSLinus Torvalds 5556072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5566072d13cSLinus Torvalds 557e64a782fSMinchan Kim __delete_from_page_cache(page); 55819fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 559e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5606072d13cSLinus Torvalds 5616072d13cSLinus Torvalds if (freepage != NULL) 5626072d13cSLinus Torvalds freepage(page); 563e286781dSNick Piggin } 564e286781dSNick Piggin 56549d2e9ccSChristoph Lameter return 1; 56649d2e9ccSChristoph Lameter 56749d2e9ccSChristoph Lameter cannot_free: 56819fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 56949d2e9ccSChristoph Lameter return 0; 57049d2e9ccSChristoph Lameter } 57149d2e9ccSChristoph Lameter 5721da177e4SLinus Torvalds /* 573e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 574e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 575e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 576e286781dSNick Piggin * this page. 577e286781dSNick Piggin */ 578e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 579e286781dSNick Piggin { 580e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 581e286781dSNick Piggin /* 582e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 583e286781dSNick Piggin * drops the pagecache ref for us without requiring another 584e286781dSNick Piggin * atomic operation. 585e286781dSNick Piggin */ 586e286781dSNick Piggin page_unfreeze_refs(page, 1); 587e286781dSNick Piggin return 1; 588e286781dSNick Piggin } 589e286781dSNick Piggin return 0; 590e286781dSNick Piggin } 591e286781dSNick Piggin 592894bc310SLee Schermerhorn /** 593894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 594894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 595894bc310SLee Schermerhorn * 596894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 597894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 598894bc310SLee Schermerhorn * 599894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 600894bc310SLee Schermerhorn */ 601894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 602894bc310SLee Schermerhorn { 6030ec3b74cSVlastimil Babka bool is_unevictable; 604bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 605894bc310SLee Schermerhorn 606*309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 607894bc310SLee Schermerhorn 608894bc310SLee Schermerhorn redo: 609894bc310SLee Schermerhorn ClearPageUnevictable(page); 610894bc310SLee Schermerhorn 61139b5f29aSHugh Dickins if (page_evictable(page)) { 612894bc310SLee Schermerhorn /* 613894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 614894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 615894bc310SLee Schermerhorn * unevictable page on [in]active list. 616894bc310SLee Schermerhorn * We know how to handle that. 617894bc310SLee Schermerhorn */ 6180ec3b74cSVlastimil Babka is_unevictable = false; 619c53954a0SMel Gorman lru_cache_add(page); 620894bc310SLee Schermerhorn } else { 621894bc310SLee Schermerhorn /* 622894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 623894bc310SLee Schermerhorn * list. 624894bc310SLee Schermerhorn */ 6250ec3b74cSVlastimil Babka is_unevictable = true; 626894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 6276a7b9548SJohannes Weiner /* 62821ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 62921ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 63021ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 63124513264SHugh Dickins * isolation/check_move_unevictable_pages, 63221ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 6336a7b9548SJohannes Weiner * the page back to the evictable list. 6346a7b9548SJohannes Weiner * 63521ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 6366a7b9548SJohannes Weiner */ 6376a7b9548SJohannes Weiner smp_mb(); 638894bc310SLee Schermerhorn } 639894bc310SLee Schermerhorn 640894bc310SLee Schermerhorn /* 641894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 642894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 643894bc310SLee Schermerhorn * check after we added it to the list, again. 644894bc310SLee Schermerhorn */ 6450ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 646894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 647894bc310SLee Schermerhorn put_page(page); 648894bc310SLee Schermerhorn goto redo; 649894bc310SLee Schermerhorn } 650894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 651894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 652894bc310SLee Schermerhorn * nothing to do here. 653894bc310SLee Schermerhorn */ 654894bc310SLee Schermerhorn } 655894bc310SLee Schermerhorn 6560ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 657bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 6580ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 659bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 660bbfd28eeSLee Schermerhorn 661894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 662894bc310SLee Schermerhorn } 663894bc310SLee Schermerhorn 664dfc8d636SJohannes Weiner enum page_references { 665dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 666dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 66764574746SJohannes Weiner PAGEREF_KEEP, 668dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 669dfc8d636SJohannes Weiner }; 670dfc8d636SJohannes Weiner 671dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 672dfc8d636SJohannes Weiner struct scan_control *sc) 673dfc8d636SJohannes Weiner { 67464574746SJohannes Weiner int referenced_ptes, referenced_page; 675dfc8d636SJohannes Weiner unsigned long vm_flags; 676dfc8d636SJohannes Weiner 677c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 678c3ac9a8aSJohannes Weiner &vm_flags); 67964574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 680dfc8d636SJohannes Weiner 681dfc8d636SJohannes Weiner /* 682dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 683dfc8d636SJohannes Weiner * move the page to the unevictable list. 684dfc8d636SJohannes Weiner */ 685dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 686dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 687dfc8d636SJohannes Weiner 68864574746SJohannes Weiner if (referenced_ptes) { 689e4898273SMichal Hocko if (PageSwapBacked(page)) 69064574746SJohannes Weiner return PAGEREF_ACTIVATE; 69164574746SJohannes Weiner /* 69264574746SJohannes Weiner * All mapped pages start out with page table 69364574746SJohannes Weiner * references from the instantiating fault, so we need 69464574746SJohannes Weiner * to look twice if a mapped file page is used more 69564574746SJohannes Weiner * than once. 69664574746SJohannes Weiner * 69764574746SJohannes Weiner * Mark it and spare it for another trip around the 69864574746SJohannes Weiner * inactive list. Another page table reference will 69964574746SJohannes Weiner * lead to its activation. 70064574746SJohannes Weiner * 70164574746SJohannes Weiner * Note: the mark is set for activated pages as well 70264574746SJohannes Weiner * so that recently deactivated but used pages are 70364574746SJohannes Weiner * quickly recovered. 70464574746SJohannes Weiner */ 70564574746SJohannes Weiner SetPageReferenced(page); 70664574746SJohannes Weiner 70734dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 708dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 709dfc8d636SJohannes Weiner 710c909e993SKonstantin Khlebnikov /* 711c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 712c909e993SKonstantin Khlebnikov */ 713c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 714c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 715c909e993SKonstantin Khlebnikov 71664574746SJohannes Weiner return PAGEREF_KEEP; 71764574746SJohannes Weiner } 71864574746SJohannes Weiner 719dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 7202e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 721dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 72264574746SJohannes Weiner 72364574746SJohannes Weiner return PAGEREF_RECLAIM; 724dfc8d636SJohannes Weiner } 725dfc8d636SJohannes Weiner 726e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 727e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 728e2be15f6SMel Gorman bool *dirty, bool *writeback) 729e2be15f6SMel Gorman { 730b4597226SMel Gorman struct address_space *mapping; 731b4597226SMel Gorman 732e2be15f6SMel Gorman /* 733e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 734e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 735e2be15f6SMel Gorman */ 736e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 737e2be15f6SMel Gorman *dirty = false; 738e2be15f6SMel Gorman *writeback = false; 739e2be15f6SMel Gorman return; 740e2be15f6SMel Gorman } 741e2be15f6SMel Gorman 742e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 743e2be15f6SMel Gorman *dirty = PageDirty(page); 744e2be15f6SMel Gorman *writeback = PageWriteback(page); 745b4597226SMel Gorman 746b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 747b4597226SMel Gorman if (!page_has_private(page)) 748b4597226SMel Gorman return; 749b4597226SMel Gorman 750b4597226SMel Gorman mapping = page_mapping(page); 751b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 752b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 753e2be15f6SMel Gorman } 754e2be15f6SMel Gorman 755e286781dSNick Piggin /* 7561742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 7571da177e4SLinus Torvalds */ 7581742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 7596a18adb3SKonstantin Khlebnikov struct zone *zone, 760f84f6e2bSMel Gorman struct scan_control *sc, 76102c6de8dSMinchan Kim enum ttu_flags ttu_flags, 7628e950282SMel Gorman unsigned long *ret_nr_dirty, 763d43006d5SMel Gorman unsigned long *ret_nr_unqueued_dirty, 7648e950282SMel Gorman unsigned long *ret_nr_congested, 76502c6de8dSMinchan Kim unsigned long *ret_nr_writeback, 766b1a6f21eSMel Gorman unsigned long *ret_nr_immediate, 76702c6de8dSMinchan Kim bool force_reclaim) 7681da177e4SLinus Torvalds { 7691da177e4SLinus Torvalds LIST_HEAD(ret_pages); 770abe4c3b5SMel Gorman LIST_HEAD(free_pages); 7711da177e4SLinus Torvalds int pgactivate = 0; 772d43006d5SMel Gorman unsigned long nr_unqueued_dirty = 0; 7730e093d99SMel Gorman unsigned long nr_dirty = 0; 7740e093d99SMel Gorman unsigned long nr_congested = 0; 77505ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 77692df3a72SMel Gorman unsigned long nr_writeback = 0; 777b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds cond_resched(); 7801da177e4SLinus Torvalds 78169980e31STim Chen mem_cgroup_uncharge_start(); 7821da177e4SLinus Torvalds while (!list_empty(page_list)) { 7831da177e4SLinus Torvalds struct address_space *mapping; 7841da177e4SLinus Torvalds struct page *page; 7851da177e4SLinus Torvalds int may_enter_fs; 78602c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 787e2be15f6SMel Gorman bool dirty, writeback; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds cond_resched(); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds page = lru_to_page(page_list); 7921da177e4SLinus Torvalds list_del(&page->lru); 7931da177e4SLinus Torvalds 794529ae9aaSNick Piggin if (!trylock_page(page)) 7951da177e4SLinus Torvalds goto keep; 7961da177e4SLinus Torvalds 797*309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 798*309381feSSasha Levin VM_BUG_ON_PAGE(page_zone(page) != zone, page); 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds sc->nr_scanned++; 80180e43426SChristoph Lameter 80239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 803b291f000SNick Piggin goto cull_mlocked; 804894bc310SLee Schermerhorn 805a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 80680e43426SChristoph Lameter goto keep_locked; 80780e43426SChristoph Lameter 8081da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 8091da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 8101da177e4SLinus Torvalds sc->nr_scanned++; 8111da177e4SLinus Torvalds 812c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 813c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 814c661b078SAndy Whitcroft 815e62e384eSMichal Hocko /* 816e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 817e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 818e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 819e2be15f6SMel Gorman * is all dirty unqueued pages. 820e2be15f6SMel Gorman */ 821e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 822e2be15f6SMel Gorman if (dirty || writeback) 823e2be15f6SMel Gorman nr_dirty++; 824e2be15f6SMel Gorman 825e2be15f6SMel Gorman if (dirty && !writeback) 826e2be15f6SMel Gorman nr_unqueued_dirty++; 827e2be15f6SMel Gorman 828d04e8acdSMel Gorman /* 829d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 830d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 831d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 832d04e8acdSMel Gorman * end of the LRU a second time. 833d04e8acdSMel Gorman */ 834e2be15f6SMel Gorman mapping = page_mapping(page); 835d04e8acdSMel Gorman if ((mapping && bdi_write_congested(mapping->backing_dev_info)) || 836d04e8acdSMel Gorman (writeback && PageReclaim(page))) 837e2be15f6SMel Gorman nr_congested++; 838e2be15f6SMel Gorman 839e2be15f6SMel Gorman /* 840283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 841283aba9fSMel Gorman * are three cases to consider. 842e62e384eSMichal Hocko * 843283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 844283aba9fSMel Gorman * under writeback and this page is both under writeback and 845283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 846283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 847283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 848283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 849283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 850b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 851b1a6f21eSMel Gorman * caller can stall after page list has been processed. 852c3b94f44SHugh Dickins * 853283aba9fSMel Gorman * 2) Global reclaim encounters a page, memcg encounters a 854283aba9fSMel Gorman * page that is not marked for immediate reclaim or 855283aba9fSMel Gorman * the caller does not have __GFP_IO. In this case mark 856283aba9fSMel Gorman * the page for immediate reclaim and continue scanning. 857283aba9fSMel Gorman * 858283aba9fSMel Gorman * __GFP_IO is checked because a loop driver thread might 859283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 860283aba9fSMel Gorman * which it is needed to do the write (loop masks off 861283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 862283aba9fSMel Gorman * would probably show more reasons. 863283aba9fSMel Gorman * 864283aba9fSMel Gorman * Don't require __GFP_FS, since we're not going into the 865283aba9fSMel Gorman * FS, just waiting on its writeback completion. Worryingly, 866283aba9fSMel Gorman * ext4 gfs2 and xfs allocate pages with 867283aba9fSMel Gorman * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing 868283aba9fSMel Gorman * may_enter_fs here is liable to OOM on them. 869283aba9fSMel Gorman * 870283aba9fSMel Gorman * 3) memcg encounters a page that is not already marked 871283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 872283aba9fSMel Gorman * throttling so we could easily OOM just because too many 873283aba9fSMel Gorman * pages are in writeback and there is nothing else to 874283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 875e62e384eSMichal Hocko */ 876283aba9fSMel Gorman if (PageWriteback(page)) { 877283aba9fSMel Gorman /* Case 1 above */ 878283aba9fSMel Gorman if (current_is_kswapd() && 879283aba9fSMel Gorman PageReclaim(page) && 880283aba9fSMel Gorman zone_is_reclaim_writeback(zone)) { 881b1a6f21eSMel Gorman nr_immediate++; 882b1a6f21eSMel Gorman goto keep_locked; 883283aba9fSMel Gorman 884283aba9fSMel Gorman /* Case 2 above */ 885283aba9fSMel Gorman } else if (global_reclaim(sc) || 886c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 887c3b94f44SHugh Dickins /* 888c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 889c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 890c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 891c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 892c3b94f44SHugh Dickins * enough to care. What we do want is for this 893c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 894c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 895c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 896c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 897c3b94f44SHugh Dickins */ 898c3b94f44SHugh Dickins SetPageReclaim(page); 89992df3a72SMel Gorman nr_writeback++; 900283aba9fSMel Gorman 901c3b94f44SHugh Dickins goto keep_locked; 902283aba9fSMel Gorman 903283aba9fSMel Gorman /* Case 3 above */ 904283aba9fSMel Gorman } else { 905c3b94f44SHugh Dickins wait_on_page_writeback(page); 906e62e384eSMichal Hocko } 907283aba9fSMel Gorman } 9081da177e4SLinus Torvalds 90902c6de8dSMinchan Kim if (!force_reclaim) 9106a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 91102c6de8dSMinchan Kim 912dfc8d636SJohannes Weiner switch (references) { 913dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 9141da177e4SLinus Torvalds goto activate_locked; 91564574746SJohannes Weiner case PAGEREF_KEEP: 91664574746SJohannes Weiner goto keep_locked; 917dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 918dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 919dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 920dfc8d636SJohannes Weiner } 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds /* 9231da177e4SLinus Torvalds * Anonymous process memory has backing store? 9241da177e4SLinus Torvalds * Try to allocate it some swap space here. 9251da177e4SLinus Torvalds */ 926b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 92763eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 92863eb6b93SHugh Dickins goto keep_locked; 9295bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 9301da177e4SLinus Torvalds goto activate_locked; 93163eb6b93SHugh Dickins may_enter_fs = 1; 9321da177e4SLinus Torvalds 933e2be15f6SMel Gorman /* Adding to swap updated mapping */ 9341da177e4SLinus Torvalds mapping = page_mapping(page); 935e2be15f6SMel Gorman } 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 9391da177e4SLinus Torvalds * processes. Try to unmap it here. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 94202c6de8dSMinchan Kim switch (try_to_unmap(page, ttu_flags)) { 9431da177e4SLinus Torvalds case SWAP_FAIL: 9441da177e4SLinus Torvalds goto activate_locked; 9451da177e4SLinus Torvalds case SWAP_AGAIN: 9461da177e4SLinus Torvalds goto keep_locked; 947b291f000SNick Piggin case SWAP_MLOCK: 948b291f000SNick Piggin goto cull_mlocked; 9491da177e4SLinus Torvalds case SWAP_SUCCESS: 9501da177e4SLinus Torvalds ; /* try to free the page below */ 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds } 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds if (PageDirty(page)) { 955ee72886dSMel Gorman /* 956ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 957d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 958d43006d5SMel Gorman * if many dirty pages have been encountered. 959ee72886dSMel Gorman */ 960f84f6e2bSMel Gorman if (page_is_file_cache(page) && 9619e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 962d43006d5SMel Gorman !zone_is_reclaim_dirty(zone))) { 96349ea7eb6SMel Gorman /* 96449ea7eb6SMel Gorman * Immediately reclaim when written back. 96549ea7eb6SMel Gorman * Similar in principal to deactivate_page() 96649ea7eb6SMel Gorman * except we already have the page isolated 96749ea7eb6SMel Gorman * and know it's dirty 96849ea7eb6SMel Gorman */ 96949ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 97049ea7eb6SMel Gorman SetPageReclaim(page); 97149ea7eb6SMel Gorman 972ee72886dSMel Gorman goto keep_locked; 973ee72886dSMel Gorman } 974ee72886dSMel Gorman 975dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 9761da177e4SLinus Torvalds goto keep_locked; 9774dd4b920SAndrew Morton if (!may_enter_fs) 9781da177e4SLinus Torvalds goto keep_locked; 97952a8363eSChristoph Lameter if (!sc->may_writepage) 9801da177e4SLinus Torvalds goto keep_locked; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 9837d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 9841da177e4SLinus Torvalds case PAGE_KEEP: 9851da177e4SLinus Torvalds goto keep_locked; 9861da177e4SLinus Torvalds case PAGE_ACTIVATE: 9871da177e4SLinus Torvalds goto activate_locked; 9881da177e4SLinus Torvalds case PAGE_SUCCESS: 9897d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 99041ac1999SMel Gorman goto keep; 9917d3579e8SKOSAKI Motohiro if (PageDirty(page)) 9921da177e4SLinus Torvalds goto keep; 9937d3579e8SKOSAKI Motohiro 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 9961da177e4SLinus Torvalds * ahead and try to reclaim the page. 9971da177e4SLinus Torvalds */ 998529ae9aaSNick Piggin if (!trylock_page(page)) 9991da177e4SLinus Torvalds goto keep; 10001da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 10011da177e4SLinus Torvalds goto keep_locked; 10021da177e4SLinus Torvalds mapping = page_mapping(page); 10031da177e4SLinus Torvalds case PAGE_CLEAN: 10041da177e4SLinus Torvalds ; /* try to free the page below */ 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* 10091da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 10101da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 10111da177e4SLinus Torvalds * the page as well. 10121da177e4SLinus Torvalds * 10131da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 10141da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 10151da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 10161da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 10171da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 10181da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 10191da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 10201da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 10211da177e4SLinus Torvalds * 10221da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 10231da177e4SLinus Torvalds * the pages which were not successfully invalidated in 10241da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 10251da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 10261da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 10271da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 10281da177e4SLinus Torvalds */ 1029266cf658SDavid Howells if (page_has_private(page)) { 10301da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 10311da177e4SLinus Torvalds goto activate_locked; 1032e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 1033e286781dSNick Piggin unlock_page(page); 1034e286781dSNick Piggin if (put_page_testzero(page)) 10351da177e4SLinus Torvalds goto free_it; 1036e286781dSNick Piggin else { 1037e286781dSNick Piggin /* 1038e286781dSNick Piggin * rare race with speculative reference. 1039e286781dSNick Piggin * the speculative reference will free 1040e286781dSNick Piggin * this page shortly, so we may 1041e286781dSNick Piggin * increment nr_reclaimed here (and 1042e286781dSNick Piggin * leave it off the LRU). 1043e286781dSNick Piggin */ 1044e286781dSNick Piggin nr_reclaimed++; 1045e286781dSNick Piggin continue; 1046e286781dSNick Piggin } 1047e286781dSNick Piggin } 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 1050e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 105149d2e9ccSChristoph Lameter goto keep_locked; 10521da177e4SLinus Torvalds 1053a978d6f5SNick Piggin /* 1054a978d6f5SNick Piggin * At this point, we have no other references and there is 1055a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1056a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1057a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1058a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1059a978d6f5SNick Piggin */ 1060a978d6f5SNick Piggin __clear_page_locked(page); 1061e286781dSNick Piggin free_it: 106205ff5137SAndrew Morton nr_reclaimed++; 1063abe4c3b5SMel Gorman 1064abe4c3b5SMel Gorman /* 1065abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1066abe4c3b5SMel Gorman * appear not as the counts should be low 1067abe4c3b5SMel Gorman */ 1068abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 10691da177e4SLinus Torvalds continue; 10701da177e4SLinus Torvalds 1071b291f000SNick Piggin cull_mlocked: 107263d6c5adSHugh Dickins if (PageSwapCache(page)) 107363d6c5adSHugh Dickins try_to_free_swap(page); 1074b291f000SNick Piggin unlock_page(page); 1075b291f000SNick Piggin putback_lru_page(page); 1076b291f000SNick Piggin continue; 1077b291f000SNick Piggin 10781da177e4SLinus Torvalds activate_locked: 107968a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 108068a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 1081a2c43eedSHugh Dickins try_to_free_swap(page); 1082*309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 10831da177e4SLinus Torvalds SetPageActive(page); 10841da177e4SLinus Torvalds pgactivate++; 10851da177e4SLinus Torvalds keep_locked: 10861da177e4SLinus Torvalds unlock_page(page); 10871da177e4SLinus Torvalds keep: 10881da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1089*309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page); 10901da177e4SLinus Torvalds } 1091abe4c3b5SMel Gorman 1092cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 1093abe4c3b5SMel Gorman 10941da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1095f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 109669980e31STim Chen mem_cgroup_uncharge_end(); 10978e950282SMel Gorman *ret_nr_dirty += nr_dirty; 10988e950282SMel Gorman *ret_nr_congested += nr_congested; 1099d43006d5SMel Gorman *ret_nr_unqueued_dirty += nr_unqueued_dirty; 110092df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 1101b1a6f21eSMel Gorman *ret_nr_immediate += nr_immediate; 110205ff5137SAndrew Morton return nr_reclaimed; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 110502c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 110602c6de8dSMinchan Kim struct list_head *page_list) 110702c6de8dSMinchan Kim { 110802c6de8dSMinchan Kim struct scan_control sc = { 110902c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 111002c6de8dSMinchan Kim .priority = DEF_PRIORITY, 111102c6de8dSMinchan Kim .may_unmap = 1, 111202c6de8dSMinchan Kim }; 11138e950282SMel Gorman unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; 111402c6de8dSMinchan Kim struct page *page, *next; 111502c6de8dSMinchan Kim LIST_HEAD(clean_pages); 111602c6de8dSMinchan Kim 111702c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 1118117aad1eSRafael Aquini if (page_is_file_cache(page) && !PageDirty(page) && 1119117aad1eSRafael Aquini !isolated_balloon_page(page)) { 112002c6de8dSMinchan Kim ClearPageActive(page); 112102c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 112202c6de8dSMinchan Kim } 112302c6de8dSMinchan Kim } 112402c6de8dSMinchan Kim 112502c6de8dSMinchan Kim ret = shrink_page_list(&clean_pages, zone, &sc, 112602c6de8dSMinchan Kim TTU_UNMAP|TTU_IGNORE_ACCESS, 11278e950282SMel Gorman &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); 112802c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 112902c6de8dSMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret); 113002c6de8dSMinchan Kim return ret; 113102c6de8dSMinchan Kim } 113202c6de8dSMinchan Kim 11335ad333ebSAndy Whitcroft /* 11345ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 11355ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 11365ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 11375ad333ebSAndy Whitcroft * 11385ad333ebSAndy Whitcroft * page: page to consider 11395ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 11405ad333ebSAndy Whitcroft * 11415ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 11425ad333ebSAndy Whitcroft */ 1143f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 11445ad333ebSAndy Whitcroft { 11455ad333ebSAndy Whitcroft int ret = -EINVAL; 11465ad333ebSAndy Whitcroft 11475ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 11485ad333ebSAndy Whitcroft if (!PageLRU(page)) 11495ad333ebSAndy Whitcroft return ret; 11505ad333ebSAndy Whitcroft 1151e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1152e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1153894bc310SLee Schermerhorn return ret; 1154894bc310SLee Schermerhorn 11555ad333ebSAndy Whitcroft ret = -EBUSY; 115608e552c6SKAMEZAWA Hiroyuki 1157c8244935SMel Gorman /* 1158c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1159c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1160c8244935SMel Gorman * blocking - clean pages for the most part. 1161c8244935SMel Gorman * 1162c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1163c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1164c8244935SMel Gorman * 1165c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1166c8244935SMel Gorman * that it is possible to migrate without blocking 1167c8244935SMel Gorman */ 1168c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1169c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1170c8244935SMel Gorman if (PageWriteback(page)) 117139deaf85SMinchan Kim return ret; 117239deaf85SMinchan Kim 1173c8244935SMel Gorman if (PageDirty(page)) { 1174c8244935SMel Gorman struct address_space *mapping; 1175c8244935SMel Gorman 1176c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1177c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1178c8244935SMel Gorman return ret; 1179c8244935SMel Gorman 1180c8244935SMel Gorman /* 1181c8244935SMel Gorman * Only pages without mappings or that have a 1182c8244935SMel Gorman * ->migratepage callback are possible to migrate 1183c8244935SMel Gorman * without blocking 1184c8244935SMel Gorman */ 1185c8244935SMel Gorman mapping = page_mapping(page); 1186c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1187c8244935SMel Gorman return ret; 1188c8244935SMel Gorman } 1189c8244935SMel Gorman } 1190c8244935SMel Gorman 1191f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1192f80c0673SMinchan Kim return ret; 1193f80c0673SMinchan Kim 11945ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 11955ad333ebSAndy Whitcroft /* 11965ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 11975ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 11985ad333ebSAndy Whitcroft * page release code relies on it. 11995ad333ebSAndy Whitcroft */ 12005ad333ebSAndy Whitcroft ClearPageLRU(page); 12015ad333ebSAndy Whitcroft ret = 0; 12025ad333ebSAndy Whitcroft } 12035ad333ebSAndy Whitcroft 12045ad333ebSAndy Whitcroft return ret; 12055ad333ebSAndy Whitcroft } 12065ad333ebSAndy Whitcroft 120749d2e9ccSChristoph Lameter /* 12081da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 12091da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 12101da177e4SLinus Torvalds * and working on them outside the LRU lock. 12111da177e4SLinus Torvalds * 12121da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 12131da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 12141da177e4SLinus Torvalds * 12151da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 12161da177e4SLinus Torvalds * 12171da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 12185dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 12191da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1220f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1221fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 12225ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 12233cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 12241da177e4SLinus Torvalds * 12251da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 12261da177e4SLinus Torvalds */ 122769e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 12285dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1229fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 12303cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 12311da177e4SLinus Torvalds { 123275b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 123369e05944SAndrew Morton unsigned long nr_taken = 0; 1234c9b02d97SWu Fengguang unsigned long scan; 12351da177e4SLinus Torvalds 1236c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 12375ad333ebSAndy Whitcroft struct page *page; 1238fa9add64SHugh Dickins int nr_pages; 12395ad333ebSAndy Whitcroft 12401da177e4SLinus Torvalds page = lru_to_page(src); 12411da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 12421da177e4SLinus Torvalds 1243*309381feSSasha Levin VM_BUG_ON_PAGE(!PageLRU(page), page); 12448d438f96SNick Piggin 1245f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 12465ad333ebSAndy Whitcroft case 0: 1247fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1248fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 12495ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1250fa9add64SHugh Dickins nr_taken += nr_pages; 12515ad333ebSAndy Whitcroft break; 12527c8ee9a8SNick Piggin 12535ad333ebSAndy Whitcroft case -EBUSY: 12545ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 12555ad333ebSAndy Whitcroft list_move(&page->lru, src); 12565ad333ebSAndy Whitcroft continue; 12575ad333ebSAndy Whitcroft 12585ad333ebSAndy Whitcroft default: 12595ad333ebSAndy Whitcroft BUG(); 12605ad333ebSAndy Whitcroft } 12615ad333ebSAndy Whitcroft } 12621da177e4SLinus Torvalds 1263f626012dSHugh Dickins *nr_scanned = scan; 126475b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 126575b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 12661da177e4SLinus Torvalds return nr_taken; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 126962695a84SNick Piggin /** 127062695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 127162695a84SNick Piggin * @page: page to isolate from its LRU list 127262695a84SNick Piggin * 127362695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 127462695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 127562695a84SNick Piggin * 127662695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 127762695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 127862695a84SNick Piggin * 127962695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1280894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1281894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1282894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 128362695a84SNick Piggin * 128462695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 128562695a84SNick Piggin * found will be decremented. 128662695a84SNick Piggin * 128762695a84SNick Piggin * Restrictions: 128862695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 128962695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 129062695a84SNick Piggin * without a stable reference). 129162695a84SNick Piggin * (2) the lru_lock must not be held. 129262695a84SNick Piggin * (3) interrupts must be enabled. 129362695a84SNick Piggin */ 129462695a84SNick Piggin int isolate_lru_page(struct page *page) 129562695a84SNick Piggin { 129662695a84SNick Piggin int ret = -EBUSY; 129762695a84SNick Piggin 1298*309381feSSasha Levin VM_BUG_ON_PAGE(!page_count(page), page); 12990c917313SKonstantin Khlebnikov 130062695a84SNick Piggin if (PageLRU(page)) { 130162695a84SNick Piggin struct zone *zone = page_zone(page); 1302fa9add64SHugh Dickins struct lruvec *lruvec; 130362695a84SNick Piggin 130462695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1305fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 13060c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1307894bc310SLee Schermerhorn int lru = page_lru(page); 13080c917313SKonstantin Khlebnikov get_page(page); 130962695a84SNick Piggin ClearPageLRU(page); 1310fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1311fa9add64SHugh Dickins ret = 0; 131262695a84SNick Piggin } 131362695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 131462695a84SNick Piggin } 131562695a84SNick Piggin return ret; 131662695a84SNick Piggin } 131762695a84SNick Piggin 13185ad333ebSAndy Whitcroft /* 1319d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1320d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1321d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1322d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1323d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 132435cd7815SRik van Riel */ 132535cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 132635cd7815SRik van Riel struct scan_control *sc) 132735cd7815SRik van Riel { 132835cd7815SRik van Riel unsigned long inactive, isolated; 132935cd7815SRik van Riel 133035cd7815SRik van Riel if (current_is_kswapd()) 133135cd7815SRik van Riel return 0; 133235cd7815SRik van Riel 133389b5fae5SJohannes Weiner if (!global_reclaim(sc)) 133435cd7815SRik van Riel return 0; 133535cd7815SRik van Riel 133635cd7815SRik van Riel if (file) { 133735cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 133835cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 133935cd7815SRik van Riel } else { 134035cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 134135cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 134235cd7815SRik van Riel } 134335cd7815SRik van Riel 13443cf23841SFengguang Wu /* 13453cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 13463cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 13473cf23841SFengguang Wu * deadlock. 13483cf23841SFengguang Wu */ 13493cf23841SFengguang Wu if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS) 13503cf23841SFengguang Wu inactive >>= 3; 13513cf23841SFengguang Wu 135235cd7815SRik van Riel return isolated > inactive; 135335cd7815SRik van Riel } 135435cd7815SRik van Riel 135566635629SMel Gorman static noinline_for_stack void 135675b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 135766635629SMel Gorman { 135827ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 135927ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13603f79768fSHugh Dickins LIST_HEAD(pages_to_free); 136166635629SMel Gorman 136266635629SMel Gorman /* 136366635629SMel Gorman * Put back any unfreeable pages. 136466635629SMel Gorman */ 136566635629SMel Gorman while (!list_empty(page_list)) { 13663f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 136766635629SMel Gorman int lru; 13683f79768fSHugh Dickins 1369*309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 137066635629SMel Gorman list_del(&page->lru); 137139b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 137266635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 137366635629SMel Gorman putback_lru_page(page); 137466635629SMel Gorman spin_lock_irq(&zone->lru_lock); 137566635629SMel Gorman continue; 137666635629SMel Gorman } 1377fa9add64SHugh Dickins 1378fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1379fa9add64SHugh Dickins 13807a608572SLinus Torvalds SetPageLRU(page); 138166635629SMel Gorman lru = page_lru(page); 1382fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1383fa9add64SHugh Dickins 138466635629SMel Gorman if (is_active_lru(lru)) { 138566635629SMel Gorman int file = is_file_lru(lru); 13869992af10SRik van Riel int numpages = hpage_nr_pages(page); 13879992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 138866635629SMel Gorman } 13892bcf8879SHugh Dickins if (put_page_testzero(page)) { 13902bcf8879SHugh Dickins __ClearPageLRU(page); 13912bcf8879SHugh Dickins __ClearPageActive(page); 1392fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 13932bcf8879SHugh Dickins 13942bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 139566635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 13962bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 139766635629SMel Gorman spin_lock_irq(&zone->lru_lock); 13982bcf8879SHugh Dickins } else 13992bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 140066635629SMel Gorman } 140166635629SMel Gorman } 140266635629SMel Gorman 14033f79768fSHugh Dickins /* 14043f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 14053f79768fSHugh Dickins */ 14063f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 140766635629SMel Gorman } 140866635629SMel Gorman 140966635629SMel Gorman /* 14101742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 14111742f19fSAndrew Morton * of reclaimed pages 14121da177e4SLinus Torvalds */ 141366635629SMel Gorman static noinline_for_stack unsigned long 14141a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 14159e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 14161da177e4SLinus Torvalds { 14171da177e4SLinus Torvalds LIST_HEAD(page_list); 1418e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 141905ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1420e247dbceSKOSAKI Motohiro unsigned long nr_taken; 14218e950282SMel Gorman unsigned long nr_dirty = 0; 14228e950282SMel Gorman unsigned long nr_congested = 0; 1423e2be15f6SMel Gorman unsigned long nr_unqueued_dirty = 0; 142492df3a72SMel Gorman unsigned long nr_writeback = 0; 1425b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 1426f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 14273cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 14281a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 14291a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 143078dc583dSKOSAKI Motohiro 143135cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 143258355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 143335cd7815SRik van Riel 143435cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 143535cd7815SRik van Riel if (fatal_signal_pending(current)) 143635cd7815SRik van Riel return SWAP_CLUSTER_MAX; 143735cd7815SRik van Riel } 143835cd7815SRik van Riel 14391da177e4SLinus Torvalds lru_add_drain(); 1440f80c0673SMinchan Kim 1441f80c0673SMinchan Kim if (!sc->may_unmap) 144261317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1443f80c0673SMinchan Kim if (!sc->may_writepage) 144461317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1445f80c0673SMinchan Kim 14461da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 14471da177e4SLinus Torvalds 14485dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 14495dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 145095d918fcSKonstantin Khlebnikov 145195d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 145295d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 145395d918fcSKonstantin Khlebnikov 145489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1455e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1456b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 145775b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1458b35ea17bSKOSAKI Motohiro else 145975b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1460b35ea17bSKOSAKI Motohiro } 146166635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1462d563c050SHillf Danton 1463d563c050SHillf Danton if (nr_taken == 0) 146466635629SMel Gorman return 0; 1465b35ea17bSKOSAKI Motohiro 146602c6de8dSMinchan Kim nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP, 14678e950282SMel Gorman &nr_dirty, &nr_unqueued_dirty, &nr_congested, 14688e950282SMel Gorman &nr_writeback, &nr_immediate, 1469b1a6f21eSMel Gorman false); 1470c661b078SAndy Whitcroft 14713f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 14723f79768fSHugh Dickins 147395d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1474d563c050SHillf Danton 1475904249aaSYing Han if (global_reclaim(sc)) { 1476b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1477904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1478904249aaSYing Han nr_reclaimed); 1479904249aaSYing Han else 1480904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1481904249aaSYing Han nr_reclaimed); 1482904249aaSYing Han } 1483a74609faSNick Piggin 148427ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 14853f79768fSHugh Dickins 148695d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 14873f79768fSHugh Dickins 14883f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 14893f79768fSHugh Dickins 14903f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1491e11da5b4SMel Gorman 149292df3a72SMel Gorman /* 149392df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 149492df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 149592df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 149692df3a72SMel Gorman * at throttling processes due to the page distribution throughout 149792df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 149892df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 149992df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 150092df3a72SMel Gorman * same way balance_dirty_pages() manages. 150192df3a72SMel Gorman * 15028e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 15038e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 15048e950282SMel Gorman * are encountered in the nr_immediate check below. 150592df3a72SMel Gorman */ 1506918fc718SMel Gorman if (nr_writeback && nr_writeback == nr_taken) 1507283aba9fSMel Gorman zone_set_flag(zone, ZONE_WRITEBACK); 150892df3a72SMel Gorman 1509d43006d5SMel Gorman /* 1510b1a6f21eSMel Gorman * memcg will stall in page writeback so only consider forcibly 1511b1a6f21eSMel Gorman * stalling for global reclaim 1512d43006d5SMel Gorman */ 1513b1a6f21eSMel Gorman if (global_reclaim(sc)) { 1514b1a6f21eSMel Gorman /* 15158e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 15168e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 15178e950282SMel Gorman */ 15188e950282SMel Gorman if (nr_dirty && nr_dirty == nr_congested) 15198e950282SMel Gorman zone_set_flag(zone, ZONE_CONGESTED); 15208e950282SMel Gorman 15218e950282SMel Gorman /* 1522b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1523b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 1524b1a6f21eSMel Gorman * the zone ZONE_TAIL_LRU_DIRTY and kswapd will start writing 1525b1a6f21eSMel Gorman * pages from reclaim context. It will forcibly stall in the 1526b1a6f21eSMel Gorman * next check. 1527b1a6f21eSMel Gorman */ 1528b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken) 1529d43006d5SMel Gorman zone_set_flag(zone, ZONE_TAIL_LRU_DIRTY); 1530b1a6f21eSMel Gorman 1531b1a6f21eSMel Gorman /* 1532b1a6f21eSMel Gorman * In addition, if kswapd scans pages marked marked for 1533b1a6f21eSMel Gorman * immediate reclaim and under writeback (nr_immediate), it 1534b1a6f21eSMel Gorman * implies that pages are cycling through the LRU faster than 1535b1a6f21eSMel Gorman * they are written so also forcibly stall. 1536b1a6f21eSMel Gorman */ 1537b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken || nr_immediate) 1538b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1539e2be15f6SMel Gorman } 1540d43006d5SMel Gorman 15418e950282SMel Gorman /* 15428e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 15438e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 15448e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 15458e950282SMel Gorman */ 15468e950282SMel Gorman if (!sc->hibernation_mode && !current_is_kswapd()) 15478e950282SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 15488e950282SMel Gorman 1549e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1550e11da5b4SMel Gorman zone_idx(zone), 1551e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 15529e3b2f8cSKonstantin Khlebnikov sc->priority, 155323b9da55SMel Gorman trace_shrink_flags(file)); 155405ff5137SAndrew Morton return nr_reclaimed; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15573bb1a852SMartin Bligh /* 15581cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 15591cfb419bSKAMEZAWA Hiroyuki * 15601cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 15611cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 15621cfb419bSKAMEZAWA Hiroyuki * 15631cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 15641cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 15651cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 15661cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 15671cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 15681cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 15691cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 15701cfb419bSKAMEZAWA Hiroyuki * 15711cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 15721cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 15731cfb419bSKAMEZAWA Hiroyuki */ 15741cfb419bSKAMEZAWA Hiroyuki 1575fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 15763eb4140fSWu Fengguang struct list_head *list, 15772bcf8879SHugh Dickins struct list_head *pages_to_free, 15783eb4140fSWu Fengguang enum lru_list lru) 15793eb4140fSWu Fengguang { 1580fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 15813eb4140fSWu Fengguang unsigned long pgmoved = 0; 15823eb4140fSWu Fengguang struct page *page; 1583fa9add64SHugh Dickins int nr_pages; 15843eb4140fSWu Fengguang 15853eb4140fSWu Fengguang while (!list_empty(list)) { 15863eb4140fSWu Fengguang page = lru_to_page(list); 1587fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 15883eb4140fSWu Fengguang 1589*309381feSSasha Levin VM_BUG_ON_PAGE(PageLRU(page), page); 15903eb4140fSWu Fengguang SetPageLRU(page); 15913eb4140fSWu Fengguang 1592fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1593fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1594925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1595fa9add64SHugh Dickins pgmoved += nr_pages; 15963eb4140fSWu Fengguang 15972bcf8879SHugh Dickins if (put_page_testzero(page)) { 15982bcf8879SHugh Dickins __ClearPageLRU(page); 15992bcf8879SHugh Dickins __ClearPageActive(page); 1600fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 16012bcf8879SHugh Dickins 16022bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 16033eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 16042bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 16053eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 16062bcf8879SHugh Dickins } else 16072bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 16083eb4140fSWu Fengguang } 16093eb4140fSWu Fengguang } 16103eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 16113eb4140fSWu Fengguang if (!is_active_lru(lru)) 16123eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 16133eb4140fSWu Fengguang } 16141cfb419bSKAMEZAWA Hiroyuki 1615f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 16161a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1617f16015fbSJohannes Weiner struct scan_control *sc, 16189e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 16191cfb419bSKAMEZAWA Hiroyuki { 162044c241f1SKOSAKI Motohiro unsigned long nr_taken; 1621f626012dSHugh Dickins unsigned long nr_scanned; 16226fe6b7e3SWu Fengguang unsigned long vm_flags; 16231cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 16248cab4754SWu Fengguang LIST_HEAD(l_active); 1625b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 16261cfb419bSKAMEZAWA Hiroyuki struct page *page; 16271a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 162844c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1629f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 16303cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 16311a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 16321cfb419bSKAMEZAWA Hiroyuki 16331da177e4SLinus Torvalds lru_add_drain(); 1634f80c0673SMinchan Kim 1635f80c0673SMinchan Kim if (!sc->may_unmap) 163661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1637f80c0673SMinchan Kim if (!sc->may_writepage) 163861317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1639f80c0673SMinchan Kim 16401da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1641925b7673SJohannes Weiner 16425dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 16435dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 164489b5fae5SJohannes Weiner if (global_reclaim(sc)) 1645f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 164689b5fae5SJohannes Weiner 1647b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 16481cfb419bSKAMEZAWA Hiroyuki 1649f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 16503cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1651a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 16521da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 16551da177e4SLinus Torvalds cond_resched(); 16561da177e4SLinus Torvalds page = lru_to_page(&l_hold); 16571da177e4SLinus Torvalds list_del(&page->lru); 16587e9cd484SRik van Riel 165939b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1660894bc310SLee Schermerhorn putback_lru_page(page); 1661894bc310SLee Schermerhorn continue; 1662894bc310SLee Schermerhorn } 1663894bc310SLee Schermerhorn 1664cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1665cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1666cc715d99SMel Gorman if (page_has_private(page)) 1667cc715d99SMel Gorman try_to_release_page(page, 0); 1668cc715d99SMel Gorman unlock_page(page); 1669cc715d99SMel Gorman } 1670cc715d99SMel Gorman } 1671cc715d99SMel Gorman 1672c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1673c3ac9a8aSJohannes Weiner &vm_flags)) { 16749992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 16758cab4754SWu Fengguang /* 16768cab4754SWu Fengguang * Identify referenced, file-backed active pages and 16778cab4754SWu Fengguang * give them one more trip around the active list. So 16788cab4754SWu Fengguang * that executable code get better chances to stay in 16798cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 16808cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 16818cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 16828cab4754SWu Fengguang * so we ignore them here. 16838cab4754SWu Fengguang */ 168441e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 16858cab4754SWu Fengguang list_add(&page->lru, &l_active); 16868cab4754SWu Fengguang continue; 16878cab4754SWu Fengguang } 16888cab4754SWu Fengguang } 16897e9cd484SRik van Riel 16905205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 16911da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds 1694b555749aSAndrew Morton /* 16958cab4754SWu Fengguang * Move pages back to the lru list. 1696b555749aSAndrew Morton */ 16972a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 16984f98a2feSRik van Riel /* 16998cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 17008cab4754SWu Fengguang * even though only some of them are actually re-activated. This 17018cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 17028cab4754SWu Fengguang * get_scan_ratio. 1703556adecbSRik van Riel */ 1704b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1705556adecbSRik van Riel 1706fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1707fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1708a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1709f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 17102bcf8879SHugh Dickins 17112bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 171474e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 171514797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1716f89eb90eSKOSAKI Motohiro { 1717f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1718f89eb90eSKOSAKI Motohiro 1719f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1720f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1721f89eb90eSKOSAKI Motohiro 1722f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1723f89eb90eSKOSAKI Motohiro return 1; 1724f89eb90eSKOSAKI Motohiro 1725f89eb90eSKOSAKI Motohiro return 0; 1726f89eb90eSKOSAKI Motohiro } 1727f89eb90eSKOSAKI Motohiro 172814797e23SKOSAKI Motohiro /** 172914797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1730c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 173114797e23SKOSAKI Motohiro * 173214797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 173314797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 173414797e23SKOSAKI Motohiro */ 1735c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 173614797e23SKOSAKI Motohiro { 173774e3f3c3SMinchan Kim /* 173874e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 173974e3f3c3SMinchan Kim * is pointless. 174074e3f3c3SMinchan Kim */ 174174e3f3c3SMinchan Kim if (!total_swap_pages) 174274e3f3c3SMinchan Kim return 0; 174374e3f3c3SMinchan Kim 1744c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1745c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1746f16015fbSJohannes Weiner 1747c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 174814797e23SKOSAKI Motohiro } 174974e3f3c3SMinchan Kim #else 1750c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 175174e3f3c3SMinchan Kim { 175274e3f3c3SMinchan Kim return 0; 175374e3f3c3SMinchan Kim } 175474e3f3c3SMinchan Kim #endif 175514797e23SKOSAKI Motohiro 175656e49d21SRik van Riel /** 175756e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1758c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 175956e49d21SRik van Riel * 176056e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 176156e49d21SRik van Riel * ensures that active file pages get deactivated, until more 176256e49d21SRik van Riel * than half of the file pages are on the inactive list. 176356e49d21SRik van Riel * 176456e49d21SRik van Riel * Once we get to that situation, protect the system's working 176556e49d21SRik van Riel * set from being evicted by disabling active file page aging. 176656e49d21SRik van Riel * 176756e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 176856e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 176956e49d21SRik van Riel */ 1770c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 177156e49d21SRik van Riel { 1772e3790144SJohannes Weiner unsigned long inactive; 1773e3790144SJohannes Weiner unsigned long active; 177456e49d21SRik van Riel 1775e3790144SJohannes Weiner inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE); 1776e3790144SJohannes Weiner active = get_lru_size(lruvec, LRU_ACTIVE_FILE); 1777e3790144SJohannes Weiner 1778e3790144SJohannes Weiner return active > inactive; 177956e49d21SRik van Riel } 178056e49d21SRik van Riel 178175b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1782b39415b2SRik van Riel { 178375b00af7SHugh Dickins if (is_file_lru(lru)) 1784c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1785b39415b2SRik van Riel else 1786c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1787b39415b2SRik van Riel } 1788b39415b2SRik van Riel 17894f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 17901a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1791b69408e8SChristoph Lameter { 1792b39415b2SRik van Riel if (is_active_lru(lru)) { 179375b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 17941a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1795556adecbSRik van Riel return 0; 1796556adecbSRik van Riel } 1797556adecbSRik van Riel 17981a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1799b69408e8SChristoph Lameter } 1800b69408e8SChristoph Lameter 18013d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 18021f4c025bSKAMEZAWA Hiroyuki { 180389b5fae5SJohannes Weiner if (global_reclaim(sc)) 18041f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 18053d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 18061f4c025bSKAMEZAWA Hiroyuki } 18071f4c025bSKAMEZAWA Hiroyuki 18089a265114SJohannes Weiner enum scan_balance { 18099a265114SJohannes Weiner SCAN_EQUAL, 18109a265114SJohannes Weiner SCAN_FRACT, 18119a265114SJohannes Weiner SCAN_ANON, 18129a265114SJohannes Weiner SCAN_FILE, 18139a265114SJohannes Weiner }; 18149a265114SJohannes Weiner 18151da177e4SLinus Torvalds /* 18164f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 18174f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 18184f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 18194f98a2feSRik van Riel * onto the active list instead of evict. 18204f98a2feSRik van Riel * 1821be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1822be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 18234f98a2feSRik van Riel */ 182490126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 18259e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 18264f98a2feSRik van Riel { 182790126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 18289a265114SJohannes Weiner u64 fraction[2]; 18299a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 183090126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 18319a265114SJohannes Weiner unsigned long anon_prio, file_prio; 18329a265114SJohannes Weiner enum scan_balance scan_balance; 18339a265114SJohannes Weiner unsigned long anon, file, free; 18349a265114SJohannes Weiner bool force_scan = false; 18359a265114SJohannes Weiner unsigned long ap, fp; 18369a265114SJohannes Weiner enum lru_list lru; 1837246e87a9SKAMEZAWA Hiroyuki 1838f11c0ca5SJohannes Weiner /* 1839f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1840f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1841f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1842f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1843f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1844f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1845f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1846f11c0ca5SJohannes Weiner * well. 1847f11c0ca5SJohannes Weiner */ 18486e543d57SLisa Du if (current_is_kswapd() && !zone_reclaimable(zone)) 1849a4d3e9e7SJohannes Weiner force_scan = true; 185089b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1851a4d3e9e7SJohannes Weiner force_scan = true; 185276a33fc3SShaohua Li 185376a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 1854ec8acf20SShaohua Li if (!sc->may_swap || (get_nr_swap_pages() <= 0)) { 18559a265114SJohannes Weiner scan_balance = SCAN_FILE; 185676a33fc3SShaohua Li goto out; 185776a33fc3SShaohua Li } 18584f98a2feSRik van Riel 185910316b31SJohannes Weiner /* 186010316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 186110316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 186210316b31SJohannes Weiner * disable swapping for individual groups completely when 186310316b31SJohannes Weiner * using the memory controller's swap limit feature would be 186410316b31SJohannes Weiner * too expensive. 186510316b31SJohannes Weiner */ 186610316b31SJohannes Weiner if (!global_reclaim(sc) && !vmscan_swappiness(sc)) { 18679a265114SJohannes Weiner scan_balance = SCAN_FILE; 186810316b31SJohannes Weiner goto out; 186910316b31SJohannes Weiner } 187010316b31SJohannes Weiner 187110316b31SJohannes Weiner /* 187210316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 187310316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 187410316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 187510316b31SJohannes Weiner */ 187610316b31SJohannes Weiner if (!sc->priority && vmscan_swappiness(sc)) { 18779a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 187810316b31SJohannes Weiner goto out; 187910316b31SJohannes Weiner } 188010316b31SJohannes Weiner 18814d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 18824d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 18834d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 18844d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1885a4d3e9e7SJohannes Weiner 188611d16c25SJohannes Weiner /* 188711d16c25SJohannes Weiner * If it's foreseeable that reclaiming the file cache won't be 188811d16c25SJohannes Weiner * enough to get the zone back into a desirable shape, we have 188911d16c25SJohannes Weiner * to swap. Better start now and leave the - probably heavily 189011d16c25SJohannes Weiner * thrashing - remaining file pages alone. 189111d16c25SJohannes Weiner */ 189289b5fae5SJohannes Weiner if (global_reclaim(sc)) { 189390126375SKonstantin Khlebnikov free = zone_page_state(zone, NR_FREE_PAGES); 189490126375SKonstantin Khlebnikov if (unlikely(file + free <= high_wmark_pages(zone))) { 18959a265114SJohannes Weiner scan_balance = SCAN_ANON; 189676a33fc3SShaohua Li goto out; 18977c5bd705SJohannes Weiner } 18987c5bd705SJohannes Weiner } 18997c5bd705SJohannes Weiner 1900e9868505SRik van Riel /* 19017c5bd705SJohannes Weiner * There is enough inactive page cache, do not reclaim 19027c5bd705SJohannes Weiner * anything from the anonymous working set right now. 1903e9868505SRik van Riel */ 19047c5bd705SJohannes Weiner if (!inactive_file_is_low(lruvec)) { 19059a265114SJohannes Weiner scan_balance = SCAN_FILE; 1906e9868505SRik van Riel goto out; 19074f98a2feSRik van Riel } 19084f98a2feSRik van Riel 19099a265114SJohannes Weiner scan_balance = SCAN_FRACT; 19109a265114SJohannes Weiner 19114f98a2feSRik van Riel /* 191258c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 191358c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 191458c37f6eSKOSAKI Motohiro */ 19153d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 191675b00af7SHugh Dickins file_prio = 200 - anon_prio; 191758c37f6eSKOSAKI Motohiro 191858c37f6eSKOSAKI Motohiro /* 19194f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 19204f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 19214f98a2feSRik van Riel * ratios to determine how valuable each cache is. 19224f98a2feSRik van Riel * 19234f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 19244f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 19254f98a2feSRik van Riel * up weighing recent references more than old ones. 19264f98a2feSRik van Riel * 19274f98a2feSRik van Riel * anon in [0], file in [1] 19284f98a2feSRik van Riel */ 192990126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 193058c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 19316e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 19326e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 19334f98a2feSRik van Riel } 19344f98a2feSRik van Riel 19356e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 19366e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 19376e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 19384f98a2feSRik van Riel } 19394f98a2feSRik van Riel 19404f98a2feSRik van Riel /* 194100d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 194200d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 194300d8089cSRik van Riel * each list that were recently referenced and in active use. 19444f98a2feSRik van Riel */ 1945fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 19466e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 19474f98a2feSRik van Riel 1948fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 19496e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 195090126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 19514f98a2feSRik van Riel 195276a33fc3SShaohua Li fraction[0] = ap; 195376a33fc3SShaohua Li fraction[1] = fp; 195476a33fc3SShaohua Li denominator = ap + fp + 1; 195576a33fc3SShaohua Li out: 19564111304dSHugh Dickins for_each_evictable_lru(lru) { 19574111304dSHugh Dickins int file = is_file_lru(lru); 1958d778df51SJohannes Weiner unsigned long size; 195976a33fc3SShaohua Li unsigned long scan; 196076a33fc3SShaohua Li 1961d778df51SJohannes Weiner size = get_lru_size(lruvec, lru); 1962d778df51SJohannes Weiner scan = size >> sc->priority; 19639a265114SJohannes Weiner 1964f11c0ca5SJohannes Weiner if (!scan && force_scan) 1965d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 19669a265114SJohannes Weiner 19679a265114SJohannes Weiner switch (scan_balance) { 19689a265114SJohannes Weiner case SCAN_EQUAL: 19699a265114SJohannes Weiner /* Scan lists relative to size */ 19709a265114SJohannes Weiner break; 19719a265114SJohannes Weiner case SCAN_FRACT: 19729a265114SJohannes Weiner /* 19739a265114SJohannes Weiner * Scan types proportional to swappiness and 19749a265114SJohannes Weiner * their relative recent reclaim efficiency. 19759a265114SJohannes Weiner */ 197676a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 19779a265114SJohannes Weiner break; 19789a265114SJohannes Weiner case SCAN_FILE: 19799a265114SJohannes Weiner case SCAN_ANON: 19809a265114SJohannes Weiner /* Scan one type exclusively */ 19819a265114SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) 19829a265114SJohannes Weiner scan = 0; 19839a265114SJohannes Weiner break; 19849a265114SJohannes Weiner default: 19859a265114SJohannes Weiner /* Look ma, no brain */ 19869a265114SJohannes Weiner BUG(); 19879a265114SJohannes Weiner } 19884111304dSHugh Dickins nr[lru] = scan; 198976a33fc3SShaohua Li } 19906e08a369SWu Fengguang } 19914f98a2feSRik van Riel 19929b4f98cdSJohannes Weiner /* 19939b4f98cdSJohannes Weiner * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 19949b4f98cdSJohannes Weiner */ 19959b4f98cdSJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 19969b4f98cdSJohannes Weiner { 19979b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 1998e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 19999b4f98cdSJohannes Weiner unsigned long nr_to_scan; 20009b4f98cdSJohannes Weiner enum lru_list lru; 20019b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 20029b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 20039b4f98cdSJohannes Weiner struct blk_plug plug; 2004e82e0561SMel Gorman bool scan_adjusted = false; 20059b4f98cdSJohannes Weiner 20069b4f98cdSJohannes Weiner get_scan_count(lruvec, sc, nr); 20079b4f98cdSJohannes Weiner 2008e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 2009e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 2010e82e0561SMel Gorman 20119b4f98cdSJohannes Weiner blk_start_plug(&plug); 20129b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 20139b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 2014e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 2015e82e0561SMel Gorman unsigned long nr_scanned; 2016e82e0561SMel Gorman 20179b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 20189b4f98cdSJohannes Weiner if (nr[lru]) { 20199b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 20209b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 20219b4f98cdSJohannes Weiner 20229b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 20239b4f98cdSJohannes Weiner lruvec, sc); 20249b4f98cdSJohannes Weiner } 20259b4f98cdSJohannes Weiner } 2026e82e0561SMel Gorman 2027e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 2028e82e0561SMel Gorman continue; 2029e82e0561SMel Gorman 20309b4f98cdSJohannes Weiner /* 2031e82e0561SMel Gorman * For global direct reclaim, reclaim only the number of pages 2032e82e0561SMel Gorman * requested. Less care is taken to scan proportionally as it 2033e82e0561SMel Gorman * is more important to minimise direct reclaim stall latency 2034e82e0561SMel Gorman * than it is to properly age the LRU lists. 20359b4f98cdSJohannes Weiner */ 2036e82e0561SMel Gorman if (global_reclaim(sc) && !current_is_kswapd()) 20379b4f98cdSJohannes Weiner break; 2038e82e0561SMel Gorman 2039e82e0561SMel Gorman /* 2040e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 2041e82e0561SMel Gorman * requested. Ensure that the anon and file LRUs shrink 2042e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2043e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2044e82e0561SMel Gorman * proportional to the original scan target. 2045e82e0561SMel Gorman */ 2046e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2047e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2048e82e0561SMel Gorman 2049e82e0561SMel Gorman if (nr_file > nr_anon) { 2050e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2051e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2052e82e0561SMel Gorman lru = LRU_BASE; 2053e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2054e82e0561SMel Gorman } else { 2055e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2056e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2057e82e0561SMel Gorman lru = LRU_FILE; 2058e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2059e82e0561SMel Gorman } 2060e82e0561SMel Gorman 2061e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2062e82e0561SMel Gorman nr[lru] = 0; 2063e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2064e82e0561SMel Gorman 2065e82e0561SMel Gorman /* 2066e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2067e82e0561SMel Gorman * scan target and the percentage scanning already complete 2068e82e0561SMel Gorman */ 2069e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2070e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2071e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2072e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2073e82e0561SMel Gorman 2074e82e0561SMel Gorman lru += LRU_ACTIVE; 2075e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2076e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2077e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2078e82e0561SMel Gorman 2079e82e0561SMel Gorman scan_adjusted = true; 20809b4f98cdSJohannes Weiner } 20819b4f98cdSJohannes Weiner blk_finish_plug(&plug); 20829b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 20839b4f98cdSJohannes Weiner 20849b4f98cdSJohannes Weiner /* 20859b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 20869b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 20879b4f98cdSJohannes Weiner */ 20889b4f98cdSJohannes Weiner if (inactive_anon_is_low(lruvec)) 20899b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 20909b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 20919b4f98cdSJohannes Weiner 20929b4f98cdSJohannes Weiner throttle_vm_writeout(sc->gfp_mask); 20939b4f98cdSJohannes Weiner } 20949b4f98cdSJohannes Weiner 209523b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 20969e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 209723b9da55SMel Gorman { 2098d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 209923b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 21009e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 210123b9da55SMel Gorman return true; 210223b9da55SMel Gorman 210323b9da55SMel Gorman return false; 210423b9da55SMel Gorman } 210523b9da55SMel Gorman 21064f98a2feSRik van Riel /* 210723b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 210823b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 210923b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 211023b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 211123b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 21123e7d3449SMel Gorman */ 21139b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone, 21143e7d3449SMel Gorman unsigned long nr_reclaimed, 21153e7d3449SMel Gorman unsigned long nr_scanned, 21163e7d3449SMel Gorman struct scan_control *sc) 21173e7d3449SMel Gorman { 21183e7d3449SMel Gorman unsigned long pages_for_compaction; 21193e7d3449SMel Gorman unsigned long inactive_lru_pages; 21203e7d3449SMel Gorman 21213e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 21229e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 21233e7d3449SMel Gorman return false; 21243e7d3449SMel Gorman 21252876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 21262876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 21273e7d3449SMel Gorman /* 21282876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 21292876592fSMel Gorman * full LRU list has been scanned and we are still failing 21302876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 21312876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 21323e7d3449SMel Gorman */ 21333e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 21343e7d3449SMel Gorman return false; 21352876592fSMel Gorman } else { 21362876592fSMel Gorman /* 21372876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 21382876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 21392876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 21402876592fSMel Gorman * pages that were scanned. This will return to the 21412876592fSMel Gorman * caller faster at the risk reclaim/compaction and 21422876592fSMel Gorman * the resulting allocation attempt fails 21432876592fSMel Gorman */ 21442876592fSMel Gorman if (!nr_reclaimed) 21452876592fSMel Gorman return false; 21462876592fSMel Gorman } 21473e7d3449SMel Gorman 21483e7d3449SMel Gorman /* 21493e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 21503e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 21513e7d3449SMel Gorman */ 21523e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 21539b4f98cdSJohannes Weiner inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE); 2154ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 21559b4f98cdSJohannes Weiner inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON); 21563e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 21573e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 21583e7d3449SMel Gorman return true; 21593e7d3449SMel Gorman 21603e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 21619b4f98cdSJohannes Weiner switch (compaction_suitable(zone, sc->order)) { 21623e7d3449SMel Gorman case COMPACT_PARTIAL: 21633e7d3449SMel Gorman case COMPACT_CONTINUE: 21643e7d3449SMel Gorman return false; 21653e7d3449SMel Gorman default: 21663e7d3449SMel Gorman return true; 21673e7d3449SMel Gorman } 21683e7d3449SMel Gorman } 21693e7d3449SMel Gorman 21700608f43dSAndrew Morton static void shrink_zone(struct zone *zone, struct scan_control *sc) 2171f16015fbSJohannes Weiner { 21729b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 21739b4f98cdSJohannes Weiner 21749b4f98cdSJohannes Weiner do { 21755660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 21765660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 21775660048cSJohannes Weiner .zone = zone, 21789e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 21795660048cSJohannes Weiner }; 2180694fbc0fSAndrew Morton struct mem_cgroup *memcg; 21815660048cSJohannes Weiner 21829b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 21839b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 21849b4f98cdSJohannes Weiner 2185694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, NULL, &reclaim); 2186694fbc0fSAndrew Morton do { 21879b4f98cdSJohannes Weiner struct lruvec *lruvec; 21889b4f98cdSJohannes Weiner 21899b4f98cdSJohannes Weiner lruvec = mem_cgroup_zone_lruvec(zone, memcg); 21905660048cSJohannes Weiner 2191f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, sc); 2192f9be23d6SKonstantin Khlebnikov 21935660048cSJohannes Weiner /* 2194a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2195a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 21969b4f98cdSJohannes Weiner * zone. 2197a394cb8eSMichal Hocko * 2198a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2199a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2200a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2201a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 22025660048cSJohannes Weiner */ 2203a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2204a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 22055660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 22065660048cSJohannes Weiner break; 22075660048cSJohannes Weiner } 2208694fbc0fSAndrew Morton memcg = mem_cgroup_iter(root, memcg, &reclaim); 2209694fbc0fSAndrew Morton } while (memcg); 221070ddf637SAnton Vorontsov 221170ddf637SAnton Vorontsov vmpressure(sc->gfp_mask, sc->target_mem_cgroup, 221270ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 221370ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 221470ddf637SAnton Vorontsov 22159b4f98cdSJohannes Weiner } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed, 22169b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 2217f16015fbSJohannes Weiner } 2218f16015fbSJohannes Weiner 2219fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 2220fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2221fe4b1b24SMel Gorman { 2222fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 2223fe4b1b24SMel Gorman bool watermark_ok; 2224fe4b1b24SMel Gorman 2225fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 2226fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 2227fe4b1b24SMel Gorman return false; 2228fe4b1b24SMel Gorman 2229fe4b1b24SMel Gorman /* 2230fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 2231fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 2232fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 2233fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 2234fe4b1b24SMel Gorman */ 2235fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 2236b40da049SJiang Liu (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 2237fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2238fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 2239fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 2240fe4b1b24SMel Gorman 2241fe4b1b24SMel Gorman /* 2242fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 2243fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 2244fe4b1b24SMel Gorman */ 2245aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 2246fe4b1b24SMel Gorman return watermark_ok; 2247fe4b1b24SMel Gorman 2248fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 2249fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 2250fe4b1b24SMel Gorman return false; 2251fe4b1b24SMel Gorman 2252fe4b1b24SMel Gorman return watermark_ok; 2253fe4b1b24SMel Gorman } 2254fe4b1b24SMel Gorman 22551da177e4SLinus Torvalds /* 22561da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 22571da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 22581da177e4SLinus Torvalds * request. 22591da177e4SLinus Torvalds * 226041858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 226141858966SMel Gorman * Because: 22621da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 22631da177e4SLinus Torvalds * allocation or 226441858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 226541858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 226641858966SMel Gorman * zone defense algorithm. 22671da177e4SLinus Torvalds * 22681da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 22691da177e4SLinus Torvalds * scan then give up on it. 2270e0c23279SMel Gorman * 2271e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 2272fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 22730cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 22740cee34fdSMel Gorman * further reclaim. 22751da177e4SLinus Torvalds */ 22769e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 22771da177e4SLinus Torvalds { 2278dd1a239fSMel Gorman struct zoneref *z; 227954a6eb5cSMel Gorman struct zone *zone; 22800608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 22810608f43dSAndrew Morton unsigned long nr_soft_scanned; 22820cee34fdSMel Gorman bool aborted_reclaim = false; 22831cfb419bSKAMEZAWA Hiroyuki 2284cc715d99SMel Gorman /* 2285cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2286cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2287cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2288cc715d99SMel Gorman */ 2289cc715d99SMel Gorman if (buffer_heads_over_limit) 2290cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2291cc715d99SMel Gorman 2292d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2293d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 2294f3fe6512SCon Kolivas if (!populated_zone(zone)) 22951da177e4SLinus Torvalds continue; 22961cfb419bSKAMEZAWA Hiroyuki /* 22971cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 22981cfb419bSKAMEZAWA Hiroyuki * to global LRU. 22991cfb419bSKAMEZAWA Hiroyuki */ 230089b5fae5SJohannes Weiner if (global_reclaim(sc)) { 230102a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 23021da177e4SLinus Torvalds continue; 23036e543d57SLisa Du if (sc->priority != DEF_PRIORITY && 23046e543d57SLisa Du !zone_reclaimable(zone)) 23051da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 2306d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION)) { 2307e0887c19SRik van Riel /* 2308e0c23279SMel Gorman * If we already have plenty of memory free for 2309e0c23279SMel Gorman * compaction in this zone, don't free any more. 2310e0c23279SMel Gorman * Even though compaction is invoked for any 2311e0c23279SMel Gorman * non-zero order, only frequent costly order 2312e0c23279SMel Gorman * reclamation is disruptive enough to become a 2313c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2314c7cfa37bSCopot Alexandru * page allocations. 2315e0887c19SRik van Riel */ 2316fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 23170cee34fdSMel Gorman aborted_reclaim = true; 2318e0887c19SRik van Riel continue; 2319e0887c19SRik van Riel } 2320e0c23279SMel Gorman } 23210608f43dSAndrew Morton /* 23220608f43dSAndrew Morton * This steals pages from memory cgroups over softlimit 23230608f43dSAndrew Morton * and returns the number of reclaimed pages and 23240608f43dSAndrew Morton * scanned pages. This works for global memory pressure 23250608f43dSAndrew Morton * and balancing, not for a memcg's limit. 23260608f43dSAndrew Morton */ 23270608f43dSAndrew Morton nr_soft_scanned = 0; 23280608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 23290608f43dSAndrew Morton sc->order, sc->gfp_mask, 23300608f43dSAndrew Morton &nr_soft_scanned); 23310608f43dSAndrew Morton sc->nr_reclaimed += nr_soft_reclaimed; 23320608f43dSAndrew Morton sc->nr_scanned += nr_soft_scanned; 2333ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2334ac34a1a3SKAMEZAWA Hiroyuki } 2335d149e3b2SYing Han 23369e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 23371da177e4SLinus Torvalds } 2338e0c23279SMel Gorman 23390cee34fdSMel Gorman return aborted_reclaim; 2340d1908362SMinchan Kim } 2341d1908362SMinchan Kim 2342929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2343d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2344d1908362SMinchan Kim struct scan_control *sc) 2345d1908362SMinchan Kim { 2346d1908362SMinchan Kim struct zoneref *z; 2347d1908362SMinchan Kim struct zone *zone; 2348d1908362SMinchan Kim 2349d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2350d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2351d1908362SMinchan Kim if (!populated_zone(zone)) 2352d1908362SMinchan Kim continue; 2353d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2354d1908362SMinchan Kim continue; 23556e543d57SLisa Du if (zone_reclaimable(zone)) 2356929bea7cSKOSAKI Motohiro return false; 2357d1908362SMinchan Kim } 2358d1908362SMinchan Kim 2359929bea7cSKOSAKI Motohiro return true; 23601da177e4SLinus Torvalds } 23611da177e4SLinus Torvalds 23621da177e4SLinus Torvalds /* 23631da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 23641da177e4SLinus Torvalds * 23651da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 23661da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 23671da177e4SLinus Torvalds * 23681da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 23691da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 23705b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 23715b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 23725b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 23735b0830cbSJens Axboe * work, and the allocation attempt will fail. 2374a41f24eaSNishanth Aravamudan * 2375a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2376a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 23771da177e4SLinus Torvalds */ 2378dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2379a09ed5e0SYing Han struct scan_control *sc, 2380a09ed5e0SYing Han struct shrink_control *shrink) 23811da177e4SLinus Torvalds { 238269e05944SAndrew Morton unsigned long total_scanned = 0; 23831da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2384dd1a239fSMel Gorman struct zoneref *z; 238554a6eb5cSMel Gorman struct zone *zone; 238622fba335SKOSAKI Motohiro unsigned long writeback_threshold; 23870cee34fdSMel Gorman bool aborted_reclaim; 23881da177e4SLinus Torvalds 2389873b4771SKeika Kobayashi delayacct_freepages_start(); 2390873b4771SKeika Kobayashi 239189b5fae5SJohannes Weiner if (global_reclaim(sc)) 2392f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 23931da177e4SLinus Torvalds 23949e3b2f8cSKonstantin Khlebnikov do { 239570ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 239670ddf637SAnton Vorontsov sc->priority); 239766e1707bSBalbir Singh sc->nr_scanned = 0; 23989e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2399e0c23279SMel Gorman 240066e1707bSBalbir Singh /* 24015a1c9cbcSMel Gorman * Don't shrink slabs when reclaiming memory from over limit 24025a1c9cbcSMel Gorman * cgroups but do shrink slab at least once when aborting 24035a1c9cbcSMel Gorman * reclaim for compaction to avoid unevenly scanning file/anon 24045a1c9cbcSMel Gorman * LRU pages over slab pages. 240566e1707bSBalbir Singh */ 240689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2407c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 24080ce3d744SDave Chinner 24090ce3d744SDave Chinner nodes_clear(shrink->nodes_to_scan); 2410d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2411d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2412c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2413c6a8a8c5SKOSAKI Motohiro continue; 2414c6a8a8c5SKOSAKI Motohiro 2415c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 24160ce3d744SDave Chinner node_set(zone_to_nid(zone), 24170ce3d744SDave Chinner shrink->nodes_to_scan); 2418c6a8a8c5SKOSAKI Motohiro } 2419c6a8a8c5SKOSAKI Motohiro 24201495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 24211da177e4SLinus Torvalds if (reclaim_state) { 2422a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 24231da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 24241da177e4SLinus Torvalds } 242591a45470SKAMEZAWA Hiroyuki } 242666e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2427bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 24281da177e4SLinus Torvalds goto out; 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds /* 24310e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 24320e50ce3bSMinchan Kim * writepage even in laptop mode. 24330e50ce3bSMinchan Kim */ 24340e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 24350e50ce3bSMinchan Kim sc->may_writepage = 1; 24360e50ce3bSMinchan Kim 24370e50ce3bSMinchan Kim /* 24381da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 24391da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 24401da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 24411da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 24421da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 24431da177e4SLinus Torvalds */ 244422fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 244522fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 24460e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 24470e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 244866e1707bSBalbir Singh sc->may_writepage = 1; 24491da177e4SLinus Torvalds } 24505a1c9cbcSMel Gorman } while (--sc->priority >= 0 && !aborted_reclaim); 2451bb21c7ceSKOSAKI Motohiro 24521da177e4SLinus Torvalds out: 2453873b4771SKeika Kobayashi delayacct_freepages_end(); 2454873b4771SKeika Kobayashi 2455bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2456bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2457bb21c7ceSKOSAKI Motohiro 2458929bea7cSKOSAKI Motohiro /* 2459929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2460929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2461929bea7cSKOSAKI Motohiro * check. 2462929bea7cSKOSAKI Motohiro */ 2463929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2464929bea7cSKOSAKI Motohiro return 0; 2465929bea7cSKOSAKI Motohiro 24660cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 24670cee34fdSMel Gorman if (aborted_reclaim) 24687335084dSMel Gorman return 1; 24697335084dSMel Gorman 2470bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 247189b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2472bb21c7ceSKOSAKI Motohiro return 1; 2473bb21c7ceSKOSAKI Motohiro 2474bb21c7ceSKOSAKI Motohiro return 0; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24775515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 24785515061dSMel Gorman { 24795515061dSMel Gorman struct zone *zone; 24805515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 24815515061dSMel Gorman unsigned long free_pages = 0; 24825515061dSMel Gorman int i; 24835515061dSMel Gorman bool wmark_ok; 24845515061dSMel Gorman 24855515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 24865515061dSMel Gorman zone = &pgdat->node_zones[i]; 24875515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 24885515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 24895515061dSMel Gorman } 24905515061dSMel Gorman 24915515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 24925515061dSMel Gorman 24935515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 24945515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 24955515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 24965515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 24975515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 24985515061dSMel Gorman } 24995515061dSMel Gorman 25005515061dSMel Gorman return wmark_ok; 25015515061dSMel Gorman } 25025515061dSMel Gorman 25035515061dSMel Gorman /* 25045515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 25055515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 25065515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 250750694c28SMel Gorman * when the low watermark is reached. 250850694c28SMel Gorman * 250950694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 251050694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 25115515061dSMel Gorman */ 251250694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 25135515061dSMel Gorman nodemask_t *nodemask) 25145515061dSMel Gorman { 25155515061dSMel Gorman struct zone *zone; 25165515061dSMel Gorman int high_zoneidx = gfp_zone(gfp_mask); 25175515061dSMel Gorman pg_data_t *pgdat; 25185515061dSMel Gorman 25195515061dSMel Gorman /* 25205515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 25215515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 25225515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 25235515061dSMel Gorman * committing a transaction where throttling it could forcing other 25245515061dSMel Gorman * processes to block on log_wait_commit(). 25255515061dSMel Gorman */ 25265515061dSMel Gorman if (current->flags & PF_KTHREAD) 252750694c28SMel Gorman goto out; 252850694c28SMel Gorman 252950694c28SMel Gorman /* 253050694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 253150694c28SMel Gorman * It should return quickly so it can exit and free its memory 253250694c28SMel Gorman */ 253350694c28SMel Gorman if (fatal_signal_pending(current)) 253450694c28SMel Gorman goto out; 25355515061dSMel Gorman 25365515061dSMel Gorman /* Check if the pfmemalloc reserves are ok */ 25375515061dSMel Gorman first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone); 25385515061dSMel Gorman pgdat = zone->zone_pgdat; 25395515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 254050694c28SMel Gorman goto out; 25415515061dSMel Gorman 254268243e76SMel Gorman /* Account for the throttling */ 254368243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 254468243e76SMel Gorman 25455515061dSMel Gorman /* 25465515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 25475515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 25485515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 25495515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 25505515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 25515515061dSMel Gorman * second before continuing. 25525515061dSMel Gorman */ 25535515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 25545515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 25555515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 255650694c28SMel Gorman 255750694c28SMel Gorman goto check_pending; 25585515061dSMel Gorman } 25595515061dSMel Gorman 25605515061dSMel Gorman /* Throttle until kswapd wakes the process */ 25615515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 25625515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 256350694c28SMel Gorman 256450694c28SMel Gorman check_pending: 256550694c28SMel Gorman if (fatal_signal_pending(current)) 256650694c28SMel Gorman return true; 256750694c28SMel Gorman 256850694c28SMel Gorman out: 256950694c28SMel Gorman return false; 25705515061dSMel Gorman } 25715515061dSMel Gorman 2572dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2573327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 257466e1707bSBalbir Singh { 257533906bc5SMel Gorman unsigned long nr_reclaimed; 257666e1707bSBalbir Singh struct scan_control sc = { 257721caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 257866e1707bSBalbir Singh .may_writepage = !laptop_mode, 257922fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2580a6dc60f8SJohannes Weiner .may_unmap = 1, 25812e2e4259SKOSAKI Motohiro .may_swap = 1, 258266e1707bSBalbir Singh .order = order, 25839e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2584f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2585327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 258666e1707bSBalbir Singh }; 2587a09ed5e0SYing Han struct shrink_control shrink = { 2588a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2589a09ed5e0SYing Han }; 259066e1707bSBalbir Singh 25915515061dSMel Gorman /* 259250694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 259350694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 259450694c28SMel Gorman * point. 25955515061dSMel Gorman */ 259650694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 25975515061dSMel Gorman return 1; 25985515061dSMel Gorman 259933906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 260033906bc5SMel Gorman sc.may_writepage, 260133906bc5SMel Gorman gfp_mask); 260233906bc5SMel Gorman 2603a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 260433906bc5SMel Gorman 260533906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 260633906bc5SMel Gorman 260733906bc5SMel Gorman return nr_reclaimed; 260866e1707bSBalbir Singh } 260966e1707bSBalbir Singh 2610c255a458SAndrew Morton #ifdef CONFIG_MEMCG 261166e1707bSBalbir Singh 261272835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 26134e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 26140ae5e89cSYing Han struct zone *zone, 26150ae5e89cSYing Han unsigned long *nr_scanned) 26164e416953SBalbir Singh { 26174e416953SBalbir Singh struct scan_control sc = { 26180ae5e89cSYing Han .nr_scanned = 0, 2619b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 26204e416953SBalbir Singh .may_writepage = !laptop_mode, 26214e416953SBalbir Singh .may_unmap = 1, 26224e416953SBalbir Singh .may_swap = !noswap, 26234e416953SBalbir Singh .order = 0, 26249e3b2f8cSKonstantin Khlebnikov .priority = 0, 262572835c86SJohannes Weiner .target_mem_cgroup = memcg, 26264e416953SBalbir Singh }; 2627f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 26280ae5e89cSYing Han 26294e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 26304e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2631bdce6d9eSKOSAKI Motohiro 26329e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2633bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2634bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2635bdce6d9eSKOSAKI Motohiro 26364e416953SBalbir Singh /* 26374e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 26384e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 26394e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 26404e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 26414e416953SBalbir Singh * the priority and make it zero. 26424e416953SBalbir Singh */ 2643f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, &sc); 2644bdce6d9eSKOSAKI Motohiro 2645bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2646bdce6d9eSKOSAKI Motohiro 26470ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 26484e416953SBalbir Singh return sc.nr_reclaimed; 26494e416953SBalbir Singh } 26504e416953SBalbir Singh 265172835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 26528c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2653185efc0fSJohannes Weiner bool noswap) 265466e1707bSBalbir Singh { 26554e416953SBalbir Singh struct zonelist *zonelist; 2656bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2657889976dbSYing Han int nid; 265866e1707bSBalbir Singh struct scan_control sc = { 265966e1707bSBalbir Singh .may_writepage = !laptop_mode, 2660a6dc60f8SJohannes Weiner .may_unmap = 1, 26612e2e4259SKOSAKI Motohiro .may_swap = !noswap, 266222fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 266366e1707bSBalbir Singh .order = 0, 26649e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 266572835c86SJohannes Weiner .target_mem_cgroup = memcg, 2666327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2667a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2668a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2669a09ed5e0SYing Han }; 2670a09ed5e0SYing Han struct shrink_control shrink = { 2671a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 267266e1707bSBalbir Singh }; 267366e1707bSBalbir Singh 2674889976dbSYing Han /* 2675889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2676889976dbSYing Han * take care of from where we get pages. So the node where we start the 2677889976dbSYing Han * scan does not need to be the current node. 2678889976dbSYing Han */ 267972835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2680889976dbSYing Han 2681889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2682bdce6d9eSKOSAKI Motohiro 2683bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2684bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2685bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2686bdce6d9eSKOSAKI Motohiro 2687a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2688bdce6d9eSKOSAKI Motohiro 2689bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2690bdce6d9eSKOSAKI Motohiro 2691bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 269266e1707bSBalbir Singh } 269366e1707bSBalbir Singh #endif 269466e1707bSBalbir Singh 26959e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2696f16015fbSJohannes Weiner { 2697b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2698b95a2f2dSJohannes Weiner 2699b95a2f2dSJohannes Weiner if (!total_swap_pages) 2700b95a2f2dSJohannes Weiner return; 2701b95a2f2dSJohannes Weiner 2702b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2703b95a2f2dSJohannes Weiner do { 2704c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2705f16015fbSJohannes Weiner 2706c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 27071a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 27089e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2709b95a2f2dSJohannes Weiner 2710b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2711b95a2f2dSJohannes Weiner } while (memcg); 2712f16015fbSJohannes Weiner } 2713f16015fbSJohannes Weiner 271460cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order, 271560cefed4SJohannes Weiner unsigned long balance_gap, int classzone_idx) 271660cefed4SJohannes Weiner { 271760cefed4SJohannes Weiner if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) + 271860cefed4SJohannes Weiner balance_gap, classzone_idx, 0)) 271960cefed4SJohannes Weiner return false; 272060cefed4SJohannes Weiner 2721d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && order && 2722d84da3f9SKirill A. Shutemov !compaction_suitable(zone, order)) 272360cefed4SJohannes Weiner return false; 272460cefed4SJohannes Weiner 272560cefed4SJohannes Weiner return true; 272660cefed4SJohannes Weiner } 272760cefed4SJohannes Weiner 27281741c877SMel Gorman /* 27294ae0a48bSZlatko Calusic * pgdat_balanced() is used when checking if a node is balanced. 27304ae0a48bSZlatko Calusic * 27314ae0a48bSZlatko Calusic * For order-0, all zones must be balanced! 27324ae0a48bSZlatko Calusic * 27334ae0a48bSZlatko Calusic * For high-order allocations only zones that meet watermarks and are in a 27344ae0a48bSZlatko Calusic * zone allowed by the callers classzone_idx are added to balanced_pages. The 27354ae0a48bSZlatko Calusic * total of balanced pages must be at least 25% of the zones allowed by 27364ae0a48bSZlatko Calusic * classzone_idx for the node to be considered balanced. Forcing all zones to 27374ae0a48bSZlatko Calusic * be balanced for high orders can cause excessive reclaim when there are 27384ae0a48bSZlatko Calusic * imbalanced zones. 27391741c877SMel Gorman * The choice of 25% is due to 27401741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 27411741c877SMel Gorman * reasonable sized machine 27421741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 274325985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 27441741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 27451741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 27461741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 27471741c877SMel Gorman */ 27484ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 27491741c877SMel Gorman { 2750b40da049SJiang Liu unsigned long managed_pages = 0; 27514ae0a48bSZlatko Calusic unsigned long balanced_pages = 0; 27521741c877SMel Gorman int i; 27531741c877SMel Gorman 27544ae0a48bSZlatko Calusic /* Check the watermark levels */ 27554ae0a48bSZlatko Calusic for (i = 0; i <= classzone_idx; i++) { 27564ae0a48bSZlatko Calusic struct zone *zone = pgdat->node_zones + i; 27571741c877SMel Gorman 27584ae0a48bSZlatko Calusic if (!populated_zone(zone)) 27594ae0a48bSZlatko Calusic continue; 27604ae0a48bSZlatko Calusic 2761b40da049SJiang Liu managed_pages += zone->managed_pages; 27624ae0a48bSZlatko Calusic 27634ae0a48bSZlatko Calusic /* 27644ae0a48bSZlatko Calusic * A special case here: 27654ae0a48bSZlatko Calusic * 27664ae0a48bSZlatko Calusic * balance_pgdat() skips over all_unreclaimable after 27674ae0a48bSZlatko Calusic * DEF_PRIORITY. Effectively, it considers them balanced so 27684ae0a48bSZlatko Calusic * they must be considered balanced here as well! 27694ae0a48bSZlatko Calusic */ 27706e543d57SLisa Du if (!zone_reclaimable(zone)) { 2771b40da049SJiang Liu balanced_pages += zone->managed_pages; 27724ae0a48bSZlatko Calusic continue; 27734ae0a48bSZlatko Calusic } 27744ae0a48bSZlatko Calusic 27754ae0a48bSZlatko Calusic if (zone_balanced(zone, order, 0, i)) 2776b40da049SJiang Liu balanced_pages += zone->managed_pages; 27774ae0a48bSZlatko Calusic else if (!order) 27784ae0a48bSZlatko Calusic return false; 27794ae0a48bSZlatko Calusic } 27804ae0a48bSZlatko Calusic 27814ae0a48bSZlatko Calusic if (order) 2782b40da049SJiang Liu return balanced_pages >= (managed_pages >> 2); 27834ae0a48bSZlatko Calusic else 27844ae0a48bSZlatko Calusic return true; 27851741c877SMel Gorman } 27861741c877SMel Gorman 27875515061dSMel Gorman /* 27885515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 27895515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 27905515061dSMel Gorman * 27915515061dSMel Gorman * Returns true if kswapd is ready to sleep 27925515061dSMel Gorman */ 27935515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2794dc83edd9SMel Gorman int classzone_idx) 2795f50de2d3SMel Gorman { 2796f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2797f50de2d3SMel Gorman if (remaining) 27985515061dSMel Gorman return false; 27995515061dSMel Gorman 28005515061dSMel Gorman /* 28015515061dSMel Gorman * There is a potential race between when kswapd checks its watermarks 28025515061dSMel Gorman * and a process gets throttled. There is also a potential race if 28035515061dSMel Gorman * processes get throttled, kswapd wakes, a large process exits therby 28045515061dSMel Gorman * balancing the zones that causes kswapd to miss a wakeup. If kswapd 28055515061dSMel Gorman * is going to sleep, no process should be sleeping on pfmemalloc_wait 28065515061dSMel Gorman * so wake them now if necessary. If necessary, processes will wake 28075515061dSMel Gorman * kswapd and get throttled again 28085515061dSMel Gorman */ 28095515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait)) { 28105515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 28115515061dSMel Gorman return false; 28125515061dSMel Gorman } 2813f50de2d3SMel Gorman 28144ae0a48bSZlatko Calusic return pgdat_balanced(pgdat, order, classzone_idx); 2815f50de2d3SMel Gorman } 2816f50de2d3SMel Gorman 28171da177e4SLinus Torvalds /* 281875485363SMel Gorman * kswapd shrinks the zone by the number of pages required to reach 281975485363SMel Gorman * the high watermark. 2820b8e83b94SMel Gorman * 2821b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 2822283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 2823283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 282475485363SMel Gorman */ 2825b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone, 28267c954f6dSMel Gorman int classzone_idx, 282775485363SMel Gorman struct scan_control *sc, 28282ab44f43SMel Gorman unsigned long lru_pages, 28292ab44f43SMel Gorman unsigned long *nr_attempted) 283075485363SMel Gorman { 28317c954f6dSMel Gorman int testorder = sc->order; 28327c954f6dSMel Gorman unsigned long balance_gap; 283375485363SMel Gorman struct reclaim_state *reclaim_state = current->reclaim_state; 283475485363SMel Gorman struct shrink_control shrink = { 283575485363SMel Gorman .gfp_mask = sc->gfp_mask, 283675485363SMel Gorman }; 28377c954f6dSMel Gorman bool lowmem_pressure; 283875485363SMel Gorman 283975485363SMel Gorman /* Reclaim above the high watermark. */ 284075485363SMel Gorman sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone)); 28417c954f6dSMel Gorman 28427c954f6dSMel Gorman /* 28437c954f6dSMel Gorman * Kswapd reclaims only single pages with compaction enabled. Trying 28447c954f6dSMel Gorman * too hard to reclaim until contiguous free pages have become 28457c954f6dSMel Gorman * available can hurt performance by evicting too much useful data 28467c954f6dSMel Gorman * from memory. Do not reclaim more than needed for compaction. 28477c954f6dSMel Gorman */ 28487c954f6dSMel Gorman if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 28497c954f6dSMel Gorman compaction_suitable(zone, sc->order) != 28507c954f6dSMel Gorman COMPACT_SKIPPED) 28517c954f6dSMel Gorman testorder = 0; 28527c954f6dSMel Gorman 28537c954f6dSMel Gorman /* 28547c954f6dSMel Gorman * We put equal pressure on every zone, unless one zone has way too 28557c954f6dSMel Gorman * many pages free already. The "too many pages" is defined as the 28567c954f6dSMel Gorman * high wmark plus a "gap" where the gap is either the low 28577c954f6dSMel Gorman * watermark or 1% of the zone, whichever is smaller. 28587c954f6dSMel Gorman */ 28597c954f6dSMel Gorman balance_gap = min(low_wmark_pages(zone), 28607c954f6dSMel Gorman (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 28617c954f6dSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 28627c954f6dSMel Gorman 28637c954f6dSMel Gorman /* 28647c954f6dSMel Gorman * If there is no low memory pressure or the zone is balanced then no 28657c954f6dSMel Gorman * reclaim is necessary 28667c954f6dSMel Gorman */ 28677c954f6dSMel Gorman lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone)); 28687c954f6dSMel Gorman if (!lowmem_pressure && zone_balanced(zone, testorder, 28697c954f6dSMel Gorman balance_gap, classzone_idx)) 28707c954f6dSMel Gorman return true; 28717c954f6dSMel Gorman 287275485363SMel Gorman shrink_zone(zone, sc); 28730ce3d744SDave Chinner nodes_clear(shrink.nodes_to_scan); 28740ce3d744SDave Chinner node_set(zone_to_nid(zone), shrink.nodes_to_scan); 287575485363SMel Gorman 287675485363SMel Gorman reclaim_state->reclaimed_slab = 0; 28776e543d57SLisa Du shrink_slab(&shrink, sc->nr_scanned, lru_pages); 287875485363SMel Gorman sc->nr_reclaimed += reclaim_state->reclaimed_slab; 287975485363SMel Gorman 28802ab44f43SMel Gorman /* Account for the number of pages attempted to reclaim */ 28812ab44f43SMel Gorman *nr_attempted += sc->nr_to_reclaim; 28822ab44f43SMel Gorman 2883283aba9fSMel Gorman zone_clear_flag(zone, ZONE_WRITEBACK); 2884283aba9fSMel Gorman 28857c954f6dSMel Gorman /* 28867c954f6dSMel Gorman * If a zone reaches its high watermark, consider it to be no longer 28877c954f6dSMel Gorman * congested. It's possible there are dirty pages backed by congested 28887c954f6dSMel Gorman * BDIs but as pressure is relieved, speculatively avoid congestion 28897c954f6dSMel Gorman * waits. 28907c954f6dSMel Gorman */ 28916e543d57SLisa Du if (zone_reclaimable(zone) && 28927c954f6dSMel Gorman zone_balanced(zone, testorder, 0, classzone_idx)) { 28937c954f6dSMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 28947c954f6dSMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 28957c954f6dSMel Gorman } 28967c954f6dSMel Gorman 2897b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 289875485363SMel Gorman } 289975485363SMel Gorman 290075485363SMel Gorman /* 29011da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 290241858966SMel Gorman * they are all at high_wmark_pages(zone). 29031da177e4SLinus Torvalds * 29040abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 29051da177e4SLinus Torvalds * 29061da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 29071da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 29081da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 29091da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 29101da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 29111da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 29121da177e4SLinus Torvalds * the zone for when the problem goes away. 29131da177e4SLinus Torvalds * 29141da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 291541858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 291641858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 291741858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 291841858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 291941858966SMel Gorman * of pages is balanced across the zones. 29201da177e4SLinus Torvalds */ 292199504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2922dc83edd9SMel Gorman int *classzone_idx) 29231da177e4SLinus Torvalds { 29241da177e4SLinus Torvalds int i; 292599504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 29260608f43dSAndrew Morton unsigned long nr_soft_reclaimed; 29270608f43dSAndrew Morton unsigned long nr_soft_scanned; 2928179e9639SAndrew Morton struct scan_control sc = { 2929179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2930b8e83b94SMel Gorman .priority = DEF_PRIORITY, 2931a6dc60f8SJohannes Weiner .may_unmap = 1, 29322e2e4259SKOSAKI Motohiro .may_swap = 1, 2933b8e83b94SMel Gorman .may_writepage = !laptop_mode, 29345ad333ebSAndy Whitcroft .order = order, 2935f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2936179e9639SAndrew Morton }; 2937f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 29381da177e4SLinus Torvalds 29399e3b2f8cSKonstantin Khlebnikov do { 29401da177e4SLinus Torvalds unsigned long lru_pages = 0; 29412ab44f43SMel Gorman unsigned long nr_attempted = 0; 2942b8e83b94SMel Gorman bool raise_priority = true; 29432ab44f43SMel Gorman bool pgdat_needs_compaction = (order > 0); 2944b8e83b94SMel Gorman 2945b8e83b94SMel Gorman sc.nr_reclaimed = 0; 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds /* 29481da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 29491da177e4SLinus Torvalds * zone which needs scanning 29501da177e4SLinus Torvalds */ 29511da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 29521da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29531da177e4SLinus Torvalds 2954f3fe6512SCon Kolivas if (!populated_zone(zone)) 29551da177e4SLinus Torvalds continue; 29561da177e4SLinus Torvalds 29576e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 29586e543d57SLisa Du !zone_reclaimable(zone)) 29591da177e4SLinus Torvalds continue; 29601da177e4SLinus Torvalds 2961556adecbSRik van Riel /* 2962556adecbSRik van Riel * Do some background aging of the anon list, to give 2963556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2964556adecbSRik van Riel */ 29659e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2966556adecbSRik van Riel 2967cc715d99SMel Gorman /* 2968cc715d99SMel Gorman * If the number of buffer_heads in the machine 2969cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2970cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2971cc715d99SMel Gorman * it to relieve lowmem pressure. 2972cc715d99SMel Gorman */ 2973cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2974cc715d99SMel Gorman end_zone = i; 2975cc715d99SMel Gorman break; 2976cc715d99SMel Gorman } 2977cc715d99SMel Gorman 297860cefed4SJohannes Weiner if (!zone_balanced(zone, order, 0, 0)) { 29791da177e4SLinus Torvalds end_zone = i; 2980e1dbeda6SAndrew Morton break; 2981439423f6SShaohua Li } else { 2982d43006d5SMel Gorman /* 2983d43006d5SMel Gorman * If balanced, clear the dirty and congested 2984d43006d5SMel Gorman * flags 2985d43006d5SMel Gorman */ 2986439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 2987d43006d5SMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 29881da177e4SLinus Torvalds } 29891da177e4SLinus Torvalds } 2990dafcb73eSZlatko Calusic 2991b8e83b94SMel Gorman if (i < 0) 29921da177e4SLinus Torvalds goto out; 2993e1dbeda6SAndrew Morton 29941da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 29951da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29961da177e4SLinus Torvalds 29972ab44f43SMel Gorman if (!populated_zone(zone)) 29982ab44f43SMel Gorman continue; 29992ab44f43SMel Gorman 3000adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 30012ab44f43SMel Gorman 30022ab44f43SMel Gorman /* 30032ab44f43SMel Gorman * If any zone is currently balanced then kswapd will 30042ab44f43SMel Gorman * not call compaction as it is expected that the 30052ab44f43SMel Gorman * necessary pages are already available. 30062ab44f43SMel Gorman */ 30072ab44f43SMel Gorman if (pgdat_needs_compaction && 30082ab44f43SMel Gorman zone_watermark_ok(zone, order, 30092ab44f43SMel Gorman low_wmark_pages(zone), 30102ab44f43SMel Gorman *classzone_idx, 0)) 30112ab44f43SMel Gorman pgdat_needs_compaction = false; 30121da177e4SLinus Torvalds } 30131da177e4SLinus Torvalds 30141da177e4SLinus Torvalds /* 3015b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 3016b7ea3c41SMel Gorman * even in laptop mode. 3017b7ea3c41SMel Gorman */ 3018b7ea3c41SMel Gorman if (sc.priority < DEF_PRIORITY - 2) 3019b7ea3c41SMel Gorman sc.may_writepage = 1; 3020b7ea3c41SMel Gorman 3021b7ea3c41SMel Gorman /* 30221da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 30231da177e4SLinus Torvalds * at the last zone which needs scanning. 30241da177e4SLinus Torvalds * 30251da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 30261da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 30271da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 30281da177e4SLinus Torvalds * cause too much scanning of the lower zones. 30291da177e4SLinus Torvalds */ 30301da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 30311da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 30321da177e4SLinus Torvalds 3033f3fe6512SCon Kolivas if (!populated_zone(zone)) 30341da177e4SLinus Torvalds continue; 30351da177e4SLinus Torvalds 30366e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 30376e543d57SLisa Du !zone_reclaimable(zone)) 30381da177e4SLinus Torvalds continue; 30391da177e4SLinus Torvalds 30401da177e4SLinus Torvalds sc.nr_scanned = 0; 30414e416953SBalbir Singh 30420608f43dSAndrew Morton nr_soft_scanned = 0; 30430608f43dSAndrew Morton /* 30440608f43dSAndrew Morton * Call soft limit reclaim before calling shrink_zone. 30450608f43dSAndrew Morton */ 30460608f43dSAndrew Morton nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 30470608f43dSAndrew Morton order, sc.gfp_mask, 30480608f43dSAndrew Morton &nr_soft_scanned); 30490608f43dSAndrew Morton sc.nr_reclaimed += nr_soft_reclaimed; 30500608f43dSAndrew Morton 305132a4330dSRik van Riel /* 30527c954f6dSMel Gorman * There should be no need to raise the scanning 30537c954f6dSMel Gorman * priority if enough pages are already being scanned 30547c954f6dSMel Gorman * that that high watermark would be met at 100% 30557c954f6dSMel Gorman * efficiency. 305632a4330dSRik van Riel */ 30577c954f6dSMel Gorman if (kswapd_shrink_zone(zone, end_zone, &sc, 30587c954f6dSMel Gorman lru_pages, &nr_attempted)) 3059b8e83b94SMel Gorman raise_priority = false; 3060b8e83b94SMel Gorman } 3061d7868daeSMel Gorman 30625515061dSMel Gorman /* 30635515061dSMel Gorman * If the low watermark is met there is no need for processes 30645515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 30655515061dSMel Gorman * able to safely make forward progress. Wake them 30665515061dSMel Gorman */ 30675515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 30685515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 30695515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 30705515061dSMel Gorman 30711da177e4SLinus Torvalds /* 3072b8e83b94SMel Gorman * Fragmentation may mean that the system cannot be rebalanced 3073b8e83b94SMel Gorman * for high-order allocations in all zones. If twice the 3074b8e83b94SMel Gorman * allocation size has been reclaimed and the zones are still 3075b8e83b94SMel Gorman * not balanced then recheck the watermarks at order-0 to 3076b8e83b94SMel Gorman * prevent kswapd reclaiming excessively. Assume that a 3077b8e83b94SMel Gorman * process requested a high-order can direct reclaim/compact. 30781da177e4SLinus Torvalds */ 3079b8e83b94SMel Gorman if (order && sc.nr_reclaimed >= 2UL << order) 308073ce02e9SKOSAKI Motohiro order = sc.order = 0; 308173ce02e9SKOSAKI Motohiro 3082b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3083b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3084b8e83b94SMel Gorman break; 3085b8e83b94SMel Gorman 3086b8e83b94SMel Gorman /* 30872ab44f43SMel Gorman * Compact if necessary and kswapd is reclaiming at least the 30882ab44f43SMel Gorman * high watermark number of pages as requsted 30892ab44f43SMel Gorman */ 30902ab44f43SMel Gorman if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted) 30912ab44f43SMel Gorman compact_pgdat(pgdat, order); 30922ab44f43SMel Gorman 30932ab44f43SMel Gorman /* 3094b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3095b8e83b94SMel Gorman * progress in reclaiming pages 3096b8e83b94SMel Gorman */ 3097b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3098b8e83b94SMel Gorman sc.priority--; 30999aa41348SMel Gorman } while (sc.priority >= 1 && 3100b8e83b94SMel Gorman !pgdat_balanced(pgdat, order, *classzone_idx)); 31011da177e4SLinus Torvalds 3102b8e83b94SMel Gorman out: 31030abdee2bSMel Gorman /* 31045515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 31050abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 31060abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 31070abdee2bSMel Gorman * was awake, order will remain at the higher level 31080abdee2bSMel Gorman */ 3109dc83edd9SMel Gorman *classzone_idx = end_zone; 31100abdee2bSMel Gorman return order; 31111da177e4SLinus Torvalds } 31121da177e4SLinus Torvalds 3113dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3114f0bc0a60SKOSAKI Motohiro { 3115f0bc0a60SKOSAKI Motohiro long remaining = 0; 3116f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3117f0bc0a60SKOSAKI Motohiro 3118f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3119f0bc0a60SKOSAKI Motohiro return; 3120f0bc0a60SKOSAKI Motohiro 3121f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3122f0bc0a60SKOSAKI Motohiro 3123f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 31245515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3125f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 3126f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3127f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3128f0bc0a60SKOSAKI Motohiro } 3129f0bc0a60SKOSAKI Motohiro 3130f0bc0a60SKOSAKI Motohiro /* 3131f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3132f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3133f0bc0a60SKOSAKI Motohiro */ 31345515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3135f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3136f0bc0a60SKOSAKI Motohiro 3137f0bc0a60SKOSAKI Motohiro /* 3138f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3139f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3140f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3141f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3142f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3143f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3144f0bc0a60SKOSAKI Motohiro */ 3145f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 31461c7e7f6cSAaditya Kumar 314762997027SMel Gorman /* 314862997027SMel Gorman * Compaction records what page blocks it recently failed to 314962997027SMel Gorman * isolate pages from and skips them in the future scanning. 315062997027SMel Gorman * When kswapd is going to sleep, it is reasonable to assume 315162997027SMel Gorman * that pages and compaction may succeed so reset the cache. 315262997027SMel Gorman */ 315362997027SMel Gorman reset_isolation_suitable(pgdat); 315462997027SMel Gorman 31551c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3156f0bc0a60SKOSAKI Motohiro schedule(); 31571c7e7f6cSAaditya Kumar 3158f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3159f0bc0a60SKOSAKI Motohiro } else { 3160f0bc0a60SKOSAKI Motohiro if (remaining) 3161f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3162f0bc0a60SKOSAKI Motohiro else 3163f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3164f0bc0a60SKOSAKI Motohiro } 3165f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3166f0bc0a60SKOSAKI Motohiro } 3167f0bc0a60SKOSAKI Motohiro 31681da177e4SLinus Torvalds /* 31691da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 31701da177e4SLinus Torvalds * from the init process. 31711da177e4SLinus Torvalds * 31721da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 31731da177e4SLinus Torvalds * free memory available even if there is no other activity 31741da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 31751da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 31761da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 31771da177e4SLinus Torvalds * 31781da177e4SLinus Torvalds * If there are applications that are active memory-allocators 31791da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 31801da177e4SLinus Torvalds */ 31811da177e4SLinus Torvalds static int kswapd(void *p) 31821da177e4SLinus Torvalds { 3183215ddd66SMel Gorman unsigned long order, new_order; 3184d2ebd0f6SAlex,Shi unsigned balanced_order; 3185215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 3186d2ebd0f6SAlex,Shi int balanced_classzone_idx; 31871da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 31881da177e4SLinus Torvalds struct task_struct *tsk = current; 3189f0bc0a60SKOSAKI Motohiro 31901da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 31911da177e4SLinus Torvalds .reclaimed_slab = 0, 31921da177e4SLinus Torvalds }; 3193a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 31941da177e4SLinus Torvalds 3195cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3196cf40bd16SNick Piggin 3197174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3198c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 31991da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds /* 32021da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 32031da177e4SLinus Torvalds * and that if we need more memory we should get access to it 32041da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 32051da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 32061da177e4SLinus Torvalds * 32071da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 32081da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 32091da177e4SLinus Torvalds * page out something else, and this flag essentially protects 32101da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 32111da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 32121da177e4SLinus Torvalds */ 3213930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 321483144186SRafael J. Wysocki set_freezable(); 32151da177e4SLinus Torvalds 3216215ddd66SMel Gorman order = new_order = 0; 3217d2ebd0f6SAlex,Shi balanced_order = 0; 3218215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 3219d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 32201da177e4SLinus Torvalds for ( ; ; ) { 32216f6313d4SJeff Liu bool ret; 32223e1d1d28SChristoph Lameter 3223215ddd66SMel Gorman /* 3224215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 3225215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 3226215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 3227215ddd66SMel Gorman */ 3228d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 3229d2ebd0f6SAlex,Shi balanced_order == new_order) { 32301da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 323199504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 32321da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 3233215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 3234215ddd66SMel Gorman } 3235215ddd66SMel Gorman 323699504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 32371da177e4SLinus Torvalds /* 32381da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 323999504748SMel Gorman * allocation or has tigher zone constraints 32401da177e4SLinus Torvalds */ 32411da177e4SLinus Torvalds order = new_order; 324299504748SMel Gorman classzone_idx = new_classzone_idx; 32431da177e4SLinus Torvalds } else { 3244d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 3245d2ebd0f6SAlex,Shi balanced_classzone_idx); 32461da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 324799504748SMel Gorman classzone_idx = pgdat->classzone_idx; 3248f0dfcde0SAlex,Shi new_order = order; 3249f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 32504d40502eSMel Gorman pgdat->kswapd_max_order = 0; 3251215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 32521da177e4SLinus Torvalds } 32531da177e4SLinus Torvalds 32548fe23e05SDavid Rientjes ret = try_to_freeze(); 32558fe23e05SDavid Rientjes if (kthread_should_stop()) 32568fe23e05SDavid Rientjes break; 32578fe23e05SDavid Rientjes 32588fe23e05SDavid Rientjes /* 32598fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 32608fe23e05SDavid Rientjes * after returning from the refrigerator 3261b1296cc4SRafael J. Wysocki */ 326233906bc5SMel Gorman if (!ret) { 326333906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 3264d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 3265d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 3266d2ebd0f6SAlex,Shi &balanced_classzone_idx); 32671da177e4SLinus Torvalds } 326833906bc5SMel Gorman } 3269b0a8cc58STakamori Yamaguchi 3270b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 32711da177e4SLinus Torvalds return 0; 32721da177e4SLinus Torvalds } 32731da177e4SLinus Torvalds 32741da177e4SLinus Torvalds /* 32751da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 32761da177e4SLinus Torvalds */ 327799504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 32781da177e4SLinus Torvalds { 32791da177e4SLinus Torvalds pg_data_t *pgdat; 32801da177e4SLinus Torvalds 3281f3fe6512SCon Kolivas if (!populated_zone(zone)) 32821da177e4SLinus Torvalds return; 32831da177e4SLinus Torvalds 328402a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 32851da177e4SLinus Torvalds return; 328688f5acf8SMel Gorman pgdat = zone->zone_pgdat; 328799504748SMel Gorman if (pgdat->kswapd_max_order < order) { 328888f5acf8SMel Gorman pgdat->kswapd_max_order = order; 328999504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 329099504748SMel Gorman } 32918d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 32921da177e4SLinus Torvalds return; 3293892f795dSJohannes Weiner if (zone_balanced(zone, order, 0, 0)) 329488f5acf8SMel Gorman return; 329588f5acf8SMel Gorman 329688f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 32978d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 32981da177e4SLinus Torvalds } 32991da177e4SLinus Torvalds 3300adea02a1SWu Fengguang /* 3301adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 3302adea02a1SWu Fengguang * The less reclaimable pages may be 3303adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 3304adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 3305adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 3306adea02a1SWu Fengguang */ 3307adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 33084f98a2feSRik van Riel { 3309adea02a1SWu Fengguang int nr; 3310adea02a1SWu Fengguang 3311adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 3312adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 3313adea02a1SWu Fengguang 3314ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 3315adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 3316adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 3317adea02a1SWu Fengguang 3318adea02a1SWu Fengguang return nr; 3319adea02a1SWu Fengguang } 3320adea02a1SWu Fengguang 3321c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 33221da177e4SLinus Torvalds /* 33237b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3324d6277db4SRafael J. Wysocki * freed pages. 3325d6277db4SRafael J. Wysocki * 3326d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3327d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3328d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 33291da177e4SLinus Torvalds */ 33307b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 33311da177e4SLinus Torvalds { 3332d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3333d6277db4SRafael J. Wysocki struct scan_control sc = { 33347b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 33357b51755cSKOSAKI Motohiro .may_swap = 1, 33367b51755cSKOSAKI Motohiro .may_unmap = 1, 3337d6277db4SRafael J. Wysocki .may_writepage = 1, 33387b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 33397b51755cSKOSAKI Motohiro .hibernation_mode = 1, 33407b51755cSKOSAKI Motohiro .order = 0, 33419e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 33421da177e4SLinus Torvalds }; 3343a09ed5e0SYing Han struct shrink_control shrink = { 3344a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3345a09ed5e0SYing Han }; 33467b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 33477b51755cSKOSAKI Motohiro struct task_struct *p = current; 33487b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 33491da177e4SLinus Torvalds 33507b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 33517b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3352d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 33537b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3354d6277db4SRafael J. Wysocki 3355a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 3356d6277db4SRafael J. Wysocki 33577b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 33587b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 33597b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3360d6277db4SRafael J. Wysocki 33617b51755cSKOSAKI Motohiro return nr_reclaimed; 33621da177e4SLinus Torvalds } 3363c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 33641da177e4SLinus Torvalds 33651da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 33661da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 33671da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 33681da177e4SLinus Torvalds restore their cpu bindings. */ 3369fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action, 3370fcb35a9bSGreg Kroah-Hartman void *hcpu) 33711da177e4SLinus Torvalds { 337258c0a4a7SYasunori Goto int nid; 33731da177e4SLinus Torvalds 33748bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 337548fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3376c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3377a70f7302SRusty Russell const struct cpumask *mask; 3378a70f7302SRusty Russell 3379a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3380c5f59f08SMike Travis 33813e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 33821da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3383c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 33841da177e4SLinus Torvalds } 33851da177e4SLinus Torvalds } 33861da177e4SLinus Torvalds return NOTIFY_OK; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 33893218ae14SYasunori Goto /* 33903218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 33913218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 33923218ae14SYasunori Goto */ 33933218ae14SYasunori Goto int kswapd_run(int nid) 33943218ae14SYasunori Goto { 33953218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 33963218ae14SYasunori Goto int ret = 0; 33973218ae14SYasunori Goto 33983218ae14SYasunori Goto if (pgdat->kswapd) 33993218ae14SYasunori Goto return 0; 34003218ae14SYasunori Goto 34013218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 34023218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 34033218ae14SYasunori Goto /* failure at boot is fatal */ 34043218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3405d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3406d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3407d72515b8SXishi Qiu pgdat->kswapd = NULL; 34083218ae14SYasunori Goto } 34093218ae14SYasunori Goto return ret; 34103218ae14SYasunori Goto } 34113218ae14SYasunori Goto 34128fe23e05SDavid Rientjes /* 3413d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3414d8adde17SJiang Liu * hold lock_memory_hotplug(). 34158fe23e05SDavid Rientjes */ 34168fe23e05SDavid Rientjes void kswapd_stop(int nid) 34178fe23e05SDavid Rientjes { 34188fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 34198fe23e05SDavid Rientjes 3420d8adde17SJiang Liu if (kswapd) { 34218fe23e05SDavid Rientjes kthread_stop(kswapd); 3422d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3423d8adde17SJiang Liu } 34248fe23e05SDavid Rientjes } 34258fe23e05SDavid Rientjes 34261da177e4SLinus Torvalds static int __init kswapd_init(void) 34271da177e4SLinus Torvalds { 34283218ae14SYasunori Goto int nid; 342969e05944SAndrew Morton 34301da177e4SLinus Torvalds swap_setup(); 343148fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 34323218ae14SYasunori Goto kswapd_run(nid); 34331da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 34341da177e4SLinus Torvalds return 0; 34351da177e4SLinus Torvalds } 34361da177e4SLinus Torvalds 34371da177e4SLinus Torvalds module_init(kswapd_init) 34389eeff239SChristoph Lameter 34399eeff239SChristoph Lameter #ifdef CONFIG_NUMA 34409eeff239SChristoph Lameter /* 34419eeff239SChristoph Lameter * Zone reclaim mode 34429eeff239SChristoph Lameter * 34439eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 34449eeff239SChristoph Lameter * the watermarks. 34459eeff239SChristoph Lameter */ 34469eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 34479eeff239SChristoph Lameter 34481b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 34497d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 34501b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 34511b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 34521b2ffb78SChristoph Lameter 34539eeff239SChristoph Lameter /* 3454a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3455a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3456a92f7126SChristoph Lameter * a zone. 3457a92f7126SChristoph Lameter */ 3458a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3459a92f7126SChristoph Lameter 34609eeff239SChristoph Lameter /* 34619614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 34629614634fSChristoph Lameter * occur. 34639614634fSChristoph Lameter */ 34649614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 34659614634fSChristoph Lameter 34669614634fSChristoph Lameter /* 34670ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 34680ff38490SChristoph Lameter * slab reclaim needs to occur. 34690ff38490SChristoph Lameter */ 34700ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 34710ff38490SChristoph Lameter 347290afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 347390afa5deSMel Gorman { 347490afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 347590afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 347690afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 347790afa5deSMel Gorman 347890afa5deSMel Gorman /* 347990afa5deSMel Gorman * It's possible for there to be more file mapped pages than 348090afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 348190afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 348290afa5deSMel Gorman */ 348390afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 348490afa5deSMel Gorman } 348590afa5deSMel Gorman 348690afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 348790afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 348890afa5deSMel Gorman { 348990afa5deSMel Gorman long nr_pagecache_reclaimable; 349090afa5deSMel Gorman long delta = 0; 349190afa5deSMel Gorman 349290afa5deSMel Gorman /* 349390afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 349490afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 349590afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 349690afa5deSMel Gorman * a better estimate 349790afa5deSMel Gorman */ 349890afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 349990afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 350090afa5deSMel Gorman else 350190afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 350290afa5deSMel Gorman 350390afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 350490afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 350590afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 350690afa5deSMel Gorman 350790afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 350890afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 350990afa5deSMel Gorman delta = nr_pagecache_reclaimable; 351090afa5deSMel Gorman 351190afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 351290afa5deSMel Gorman } 351390afa5deSMel Gorman 35140ff38490SChristoph Lameter /* 35159eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 35169eeff239SChristoph Lameter */ 3517179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 35189eeff239SChristoph Lameter { 35197fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 352069e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 35219eeff239SChristoph Lameter struct task_struct *p = current; 35229eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3523179e9639SAndrew Morton struct scan_control sc = { 3524179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3525a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 35262e2e4259SKOSAKI Motohiro .may_swap = 1, 352762b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 352821caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3529bd2f6199SJohannes Weiner .order = order, 35309e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3531179e9639SAndrew Morton }; 3532a09ed5e0SYing Han struct shrink_control shrink = { 3533a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3534a09ed5e0SYing Han }; 353515748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 35369eeff239SChristoph Lameter 35379eeff239SChristoph Lameter cond_resched(); 3538d4f7796eSChristoph Lameter /* 3539d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3540d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3541d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3542d4f7796eSChristoph Lameter */ 3543d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 354476ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 35459eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 35469eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3547c84db23cSChristoph Lameter 354890afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3549a92f7126SChristoph Lameter /* 35500ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 35510ff38490SChristoph Lameter * priorities until we have enough memory freed. 3552a92f7126SChristoph Lameter */ 3553a92f7126SChristoph Lameter do { 35549e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 35559e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 35560ff38490SChristoph Lameter } 3557a92f7126SChristoph Lameter 355815748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 355915748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 35602a16e3f4SChristoph Lameter /* 35617fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 35620ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 35630ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 35640ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 35650ff38490SChristoph Lameter * pages. 35662a16e3f4SChristoph Lameter */ 35670ce3d744SDave Chinner nodes_clear(shrink.nodes_to_scan); 35680ce3d744SDave Chinner node_set(zone_to_nid(zone), shrink.nodes_to_scan); 35694dc4b3d9SKOSAKI Motohiro for (;;) { 35704dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 35714dc4b3d9SKOSAKI Motohiro 35724dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 35731495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 35744dc4b3d9SKOSAKI Motohiro break; 35754dc4b3d9SKOSAKI Motohiro 35764dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 35774dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 35784dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 35794dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 35804dc4b3d9SKOSAKI Motohiro break; 35814dc4b3d9SKOSAKI Motohiro } 358283e33a47SChristoph Lameter 358383e33a47SChristoph Lameter /* 358483e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 358583e33a47SChristoph Lameter * reclaimed from this zone. 358683e33a47SChristoph Lameter */ 358715748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 358815748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 358915748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 35902a16e3f4SChristoph Lameter } 35912a16e3f4SChristoph Lameter 35929eeff239SChristoph Lameter p->reclaim_state = NULL; 3593d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 359476ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3595a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 35969eeff239SChristoph Lameter } 3597179e9639SAndrew Morton 3598179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3599179e9639SAndrew Morton { 3600179e9639SAndrew Morton int node_id; 3601d773ed6bSDavid Rientjes int ret; 3602179e9639SAndrew Morton 3603179e9639SAndrew Morton /* 36040ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 36050ff38490SChristoph Lameter * slab pages if we are over the defined limits. 360634aa1330SChristoph Lameter * 36079614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 36089614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 36099614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 36109614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 36119614634fSChristoph Lameter * unmapped file backed pages. 3612179e9639SAndrew Morton */ 361390afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 361490afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3615fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3616179e9639SAndrew Morton 36176e543d57SLisa Du if (!zone_reclaimable(zone)) 3618fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3619d773ed6bSDavid Rientjes 3620179e9639SAndrew Morton /* 3621d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3622179e9639SAndrew Morton */ 3623d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3624fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3625179e9639SAndrew Morton 3626179e9639SAndrew Morton /* 3627179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3628179e9639SAndrew Morton * have associated processors. This will favor the local processor 3629179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3630179e9639SAndrew Morton * as wide as possible. 3631179e9639SAndrew Morton */ 363289fa3024SChristoph Lameter node_id = zone_to_nid(zone); 363337c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3634fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3635d773ed6bSDavid Rientjes 3636d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3637fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3638fa5e084eSMel Gorman 3639d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3640d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3641d773ed6bSDavid Rientjes 364224cf7251SMel Gorman if (!ret) 364324cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 364424cf7251SMel Gorman 3645d773ed6bSDavid Rientjes return ret; 3646179e9639SAndrew Morton } 36479eeff239SChristoph Lameter #endif 3648894bc310SLee Schermerhorn 3649894bc310SLee Schermerhorn /* 3650894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3651894bc310SLee Schermerhorn * @page: the page to test 3652894bc310SLee Schermerhorn * 3653894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 365439b5f29aSHugh Dickins * lists vs unevictable list. 3655894bc310SLee Schermerhorn * 3656894bc310SLee Schermerhorn * Reasons page might not be evictable: 3657ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3658b291f000SNick Piggin * (2) page is part of an mlocked VMA 3659ba9ddf49SLee Schermerhorn * 3660894bc310SLee Schermerhorn */ 366139b5f29aSHugh Dickins int page_evictable(struct page *page) 3662894bc310SLee Schermerhorn { 366339b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3664894bc310SLee Schermerhorn } 366589e004eaSLee Schermerhorn 366685046579SHugh Dickins #ifdef CONFIG_SHMEM 366789e004eaSLee Schermerhorn /** 366824513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 366924513264SHugh Dickins * @pages: array of pages to check 367024513264SHugh Dickins * @nr_pages: number of pages to check 367189e004eaSLee Schermerhorn * 367224513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 367385046579SHugh Dickins * 367485046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 367589e004eaSLee Schermerhorn */ 367624513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 367789e004eaSLee Schermerhorn { 3678925b7673SJohannes Weiner struct lruvec *lruvec; 367924513264SHugh Dickins struct zone *zone = NULL; 368024513264SHugh Dickins int pgscanned = 0; 368124513264SHugh Dickins int pgrescued = 0; 368289e004eaSLee Schermerhorn int i; 368389e004eaSLee Schermerhorn 368424513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 368524513264SHugh Dickins struct page *page = pages[i]; 368624513264SHugh Dickins struct zone *pagezone; 368789e004eaSLee Schermerhorn 368824513264SHugh Dickins pgscanned++; 368924513264SHugh Dickins pagezone = page_zone(page); 369089e004eaSLee Schermerhorn if (pagezone != zone) { 369189e004eaSLee Schermerhorn if (zone) 369289e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 369389e004eaSLee Schermerhorn zone = pagezone; 369489e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 369589e004eaSLee Schermerhorn } 3696fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 369789e004eaSLee Schermerhorn 369824513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 369924513264SHugh Dickins continue; 370089e004eaSLee Schermerhorn 370139b5f29aSHugh Dickins if (page_evictable(page)) { 370224513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 370324513264SHugh Dickins 3704*309381feSSasha Levin VM_BUG_ON_PAGE(PageActive(page), page); 370524513264SHugh Dickins ClearPageUnevictable(page); 3706fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3707fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 370824513264SHugh Dickins pgrescued++; 370989e004eaSLee Schermerhorn } 371089e004eaSLee Schermerhorn } 371124513264SHugh Dickins 371224513264SHugh Dickins if (zone) { 371324513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 371424513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 371524513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 371624513264SHugh Dickins } 371785046579SHugh Dickins } 371885046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3719af936a16SLee Schermerhorn 3720264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3721af936a16SLee Schermerhorn { 3722264e56d8SJohannes Weiner printk_once(KERN_WARNING 372325bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3724264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 372525bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 372625bd91bdSKOSAKI Motohiro current->comm); 3727af936a16SLee Schermerhorn } 3728af936a16SLee Schermerhorn 3729af936a16SLee Schermerhorn /* 3730af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3731af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3732af936a16SLee Schermerhorn */ 3733af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3734af936a16SLee Schermerhorn 3735af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 37368d65af78SAlexey Dobriyan void __user *buffer, 3737af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3738af936a16SLee Schermerhorn { 3739264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 37408d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3741af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3742af936a16SLee Schermerhorn return 0; 3743af936a16SLee Schermerhorn } 3744af936a16SLee Schermerhorn 3745e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3746af936a16SLee Schermerhorn /* 3747af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3748af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3749af936a16SLee Schermerhorn */ 3750af936a16SLee Schermerhorn 375110fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 375210fbcf4cSKay Sievers struct device_attribute *attr, 3753af936a16SLee Schermerhorn char *buf) 3754af936a16SLee Schermerhorn { 3755264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3756af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3757af936a16SLee Schermerhorn } 3758af936a16SLee Schermerhorn 375910fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 376010fbcf4cSKay Sievers struct device_attribute *attr, 3761af936a16SLee Schermerhorn const char *buf, size_t count) 3762af936a16SLee Schermerhorn { 3763264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3764af936a16SLee Schermerhorn return 1; 3765af936a16SLee Schermerhorn } 3766af936a16SLee Schermerhorn 3767af936a16SLee Schermerhorn 376810fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3769af936a16SLee Schermerhorn read_scan_unevictable_node, 3770af936a16SLee Schermerhorn write_scan_unevictable_node); 3771af936a16SLee Schermerhorn 3772af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3773af936a16SLee Schermerhorn { 377410fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3775af936a16SLee Schermerhorn } 3776af936a16SLee Schermerhorn 3777af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3778af936a16SLee Schermerhorn { 377910fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3780af936a16SLee Schermerhorn } 3781e4455abbSThadeu Lima de Souza Cascardo #endif 3782