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> 511da177e4SLinus Torvalds 520f8053a5SNick Piggin #include "internal.h" 530f8053a5SNick Piggin 5433906bc5SMel Gorman #define CREATE_TRACE_POINTS 5533906bc5SMel Gorman #include <trace/events/vmscan.h> 5633906bc5SMel Gorman 571da177e4SLinus Torvalds struct scan_control { 581da177e4SLinus Torvalds /* Incremented by the number of inactive pages that were scanned */ 591da177e4SLinus Torvalds unsigned long nr_scanned; 601da177e4SLinus Torvalds 61a79311c1SRik van Riel /* Number of pages freed so far during a call to shrink_zones() */ 62a79311c1SRik van Riel unsigned long nr_reclaimed; 63a79311c1SRik van Riel 6422fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6522fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6622fba335SKOSAKI Motohiro 677b51755cSKOSAKI Motohiro unsigned long hibernation_mode; 687b51755cSKOSAKI Motohiro 691da177e4SLinus Torvalds /* This context's GFP mask */ 706daa0e28SAl Viro gfp_t gfp_mask; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds int may_writepage; 731da177e4SLinus Torvalds 74a6dc60f8SJohannes Weiner /* Can mapped pages be reclaimed? */ 75a6dc60f8SJohannes Weiner int may_unmap; 76f1fd1067SChristoph Lameter 772e2e4259SKOSAKI Motohiro /* Can pages be swapped as part of reclaim? */ 782e2e4259SKOSAKI Motohiro int may_swap; 792e2e4259SKOSAKI Motohiro 805ad333ebSAndy Whitcroft int order; 8166e1707bSBalbir Singh 829e3b2f8cSKonstantin Khlebnikov /* Scan (total_size >> priority) pages at once */ 839e3b2f8cSKonstantin Khlebnikov int priority; 849e3b2f8cSKonstantin Khlebnikov 855f53e762SKOSAKI Motohiro /* 86f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 87f16015fbSJohannes Weiner * primary target of this reclaim invocation. 88f16015fbSJohannes Weiner */ 89f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 9066e1707bSBalbir Singh 91327c0e96SKAMEZAWA Hiroyuki /* 92327c0e96SKAMEZAWA Hiroyuki * Nodemask of nodes allowed by the caller. If NULL, all nodes 93327c0e96SKAMEZAWA Hiroyuki * are scanned. 94327c0e96SKAMEZAWA Hiroyuki */ 95327c0e96SKAMEZAWA Hiroyuki nodemask_t *nodemask; 961da177e4SLinus Torvalds }; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1011da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1021da177e4SLinus Torvalds do { \ 1031da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1041da177e4SLinus Torvalds struct page *prev; \ 1051da177e4SLinus Torvalds \ 1061da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1071da177e4SLinus Torvalds prefetch(&prev->_field); \ 1081da177e4SLinus Torvalds } \ 1091da177e4SLinus Torvalds } while (0) 1101da177e4SLinus Torvalds #else 1111da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1151da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1161da177e4SLinus Torvalds do { \ 1171da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1181da177e4SLinus Torvalds struct page *prev; \ 1191da177e4SLinus Torvalds \ 1201da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1211da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1221da177e4SLinus Torvalds } \ 1231da177e4SLinus Torvalds } while (0) 1241da177e4SLinus Torvalds #else 1251da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1261da177e4SLinus Torvalds #endif 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds int vm_swappiness = 60; 132b21e0b90SZhang Yanfei unsigned long vm_total_pages; /* The total number of pages which the VM controls */ 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1351da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1361da177e4SLinus Torvalds 137c255a458SAndrew Morton #ifdef CONFIG_MEMCG 13889b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 13989b5fae5SJohannes Weiner { 140f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14189b5fae5SJohannes Weiner } 14291a45470SKAMEZAWA Hiroyuki #else 14389b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14489b5fae5SJohannes Weiner { 14589b5fae5SJohannes Weiner return true; 14689b5fae5SJohannes Weiner } 14791a45470SKAMEZAWA Hiroyuki #endif 14891a45470SKAMEZAWA Hiroyuki 149*6e543d57SLisa Du unsigned long zone_reclaimable_pages(struct zone *zone) 150*6e543d57SLisa Du { 151*6e543d57SLisa Du int nr; 152*6e543d57SLisa Du 153*6e543d57SLisa Du nr = zone_page_state(zone, NR_ACTIVE_FILE) + 154*6e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_FILE); 155*6e543d57SLisa Du 156*6e543d57SLisa Du if (get_nr_swap_pages() > 0) 157*6e543d57SLisa Du nr += zone_page_state(zone, NR_ACTIVE_ANON) + 158*6e543d57SLisa Du zone_page_state(zone, NR_INACTIVE_ANON); 159*6e543d57SLisa Du 160*6e543d57SLisa Du return nr; 161*6e543d57SLisa Du } 162*6e543d57SLisa Du 163*6e543d57SLisa Du bool zone_reclaimable(struct zone *zone) 164*6e543d57SLisa Du { 165*6e543d57SLisa Du return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 166*6e543d57SLisa Du } 167*6e543d57SLisa Du 1684d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 169c9f299d9SKOSAKI Motohiro { 170c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1714d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 172a3d8e054SKOSAKI Motohiro 173074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 174c9f299d9SKOSAKI Motohiro } 175c9f299d9SKOSAKI Motohiro 1761da177e4SLinus Torvalds /* 1771da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1781da177e4SLinus Torvalds */ 1798e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker) 1801da177e4SLinus Torvalds { 18183aeeadaSKonstantin Khlebnikov atomic_long_set(&shrinker->nr_in_batch, 0); 1821da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1831da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1841da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1851da177e4SLinus Torvalds } 1868e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * Remove one 1901da177e4SLinus Torvalds */ 1918e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1941da177e4SLinus Torvalds list_del(&shrinker->list); 1951da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1961da177e4SLinus Torvalds } 1978e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 1981da177e4SLinus Torvalds 1991495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker, 2001495f230SYing Han struct shrink_control *sc, 2011495f230SYing Han unsigned long nr_to_scan) 2021495f230SYing Han { 2031495f230SYing Han sc->nr_to_scan = nr_to_scan; 2041495f230SYing Han return (*shrinker->shrink)(shrinker, sc); 2051495f230SYing Han } 2061495f230SYing Han 2071da177e4SLinus Torvalds #define SHRINK_BATCH 128 2081da177e4SLinus Torvalds /* 2091da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 2121da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 2131da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 2141da177e4SLinus Torvalds * generated by these structures. 2151da177e4SLinus Torvalds * 216183ff22bSSimon Arlott * If the vm encountered mapped pages on the LRU it increase the pressure on 2171da177e4SLinus Torvalds * slab to avoid swapping. 2181da177e4SLinus Torvalds * 2191da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 2201da177e4SLinus Torvalds * 2211da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 2221da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 2231da177e4SLinus Torvalds * slab reclaim versus page reclaim. 224b15e0905Sakpm@osdl.org * 225b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 2261da177e4SLinus Torvalds */ 227a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink, 2281495f230SYing Han unsigned long nr_pages_scanned, 22969e05944SAndrew Morton unsigned long lru_pages) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds struct shrinker *shrinker; 23269e05944SAndrew Morton unsigned long ret = 0; 2331da177e4SLinus Torvalds 2341495f230SYing Han if (nr_pages_scanned == 0) 2351495f230SYing Han nr_pages_scanned = SWAP_CLUSTER_MAX; 2361da177e4SLinus Torvalds 237f06590bdSMinchan Kim if (!down_read_trylock(&shrinker_rwsem)) { 238f06590bdSMinchan Kim /* Assume we'll be able to shrink next time */ 239f06590bdSMinchan Kim ret = 1; 240f06590bdSMinchan Kim goto out; 241f06590bdSMinchan Kim } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 2441da177e4SLinus Torvalds unsigned long long delta; 245635697c6SKonstantin Khlebnikov long total_scan; 246635697c6SKonstantin Khlebnikov long max_pass; 24709576073SDave Chinner int shrink_ret = 0; 248acf92b48SDave Chinner long nr; 249acf92b48SDave Chinner long new_nr; 250e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 251e9299f50SDave Chinner : SHRINK_BATCH; 2521da177e4SLinus Torvalds 253635697c6SKonstantin Khlebnikov max_pass = do_shrinker_shrink(shrinker, shrink, 0); 254635697c6SKonstantin Khlebnikov if (max_pass <= 0) 255635697c6SKonstantin Khlebnikov continue; 256635697c6SKonstantin Khlebnikov 257acf92b48SDave Chinner /* 258acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 259acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 260acf92b48SDave Chinner * don't also do this scanning work. 261acf92b48SDave Chinner */ 26283aeeadaSKonstantin Khlebnikov nr = atomic_long_xchg(&shrinker->nr_in_batch, 0); 263acf92b48SDave Chinner 264acf92b48SDave Chinner total_scan = nr; 2651495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 266ea164d73SAndrea Arcangeli delta *= max_pass; 2671da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 268acf92b48SDave Chinner total_scan += delta; 269acf92b48SDave Chinner if (total_scan < 0) { 27088c3bd70SDavid Rientjes printk(KERN_ERR "shrink_slab: %pF negative objects to " 27188c3bd70SDavid Rientjes "delete nr=%ld\n", 272acf92b48SDave Chinner shrinker->shrink, total_scan); 273acf92b48SDave Chinner total_scan = max_pass; 274ea164d73SAndrea Arcangeli } 275ea164d73SAndrea Arcangeli 276ea164d73SAndrea Arcangeli /* 2773567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2783567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2793567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2803567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2813567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2823567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2833567b59aSDave Chinner * memory. 2843567b59aSDave Chinner * 2853567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2863567b59aSDave Chinner * a large delta change is calculated directly. 2873567b59aSDave Chinner */ 2883567b59aSDave Chinner if (delta < max_pass / 4) 2893567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2903567b59aSDave Chinner 2913567b59aSDave Chinner /* 292ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 293ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 294ea164d73SAndrea Arcangeli * freeable entries. 295ea164d73SAndrea Arcangeli */ 296acf92b48SDave Chinner if (total_scan > max_pass * 2) 297acf92b48SDave Chinner total_scan = max_pass * 2; 2981da177e4SLinus Torvalds 299acf92b48SDave Chinner trace_mm_shrink_slab_start(shrinker, shrink, nr, 30009576073SDave Chinner nr_pages_scanned, lru_pages, 30109576073SDave Chinner max_pass, delta, total_scan); 30209576073SDave Chinner 303e9299f50SDave Chinner while (total_scan >= batch_size) { 304b15e0905Sakpm@osdl.org int nr_before; 3051da177e4SLinus Torvalds 3061495f230SYing Han nr_before = do_shrinker_shrink(shrinker, shrink, 0); 3071495f230SYing Han shrink_ret = do_shrinker_shrink(shrinker, shrink, 308e9299f50SDave Chinner batch_size); 3091da177e4SLinus Torvalds if (shrink_ret == -1) 3101da177e4SLinus Torvalds break; 311b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 312b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 313e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 314e9299f50SDave Chinner total_scan -= batch_size; 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds cond_resched(); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 319acf92b48SDave Chinner /* 320acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 321acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 322acf92b48SDave Chinner * scan, there is no need to do an update. 323acf92b48SDave Chinner */ 32483aeeadaSKonstantin Khlebnikov if (total_scan > 0) 32583aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 32683aeeadaSKonstantin Khlebnikov &shrinker->nr_in_batch); 32783aeeadaSKonstantin Khlebnikov else 32883aeeadaSKonstantin Khlebnikov new_nr = atomic_long_read(&shrinker->nr_in_batch); 329acf92b48SDave Chinner 330acf92b48SDave Chinner trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds up_read(&shrinker_rwsem); 333f06590bdSMinchan Kim out: 334f06590bdSMinchan Kim cond_resched(); 335b15e0905Sakpm@osdl.org return ret; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3391da177e4SLinus Torvalds { 340ceddc3a5SJohannes Weiner /* 341ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 342ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 343ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 344ceddc3a5SJohannes Weiner */ 345edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 3487d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3497d3579e8SKOSAKI Motohiro struct scan_control *sc) 3501da177e4SLinus Torvalds { 351930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3521da177e4SLinus Torvalds return 1; 3531da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3541da177e4SLinus Torvalds return 1; 3551da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3561da177e4SLinus Torvalds return 1; 3571da177e4SLinus Torvalds return 0; 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* 3611da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3621da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3631da177e4SLinus Torvalds * fsync(), msync() or close(). 3641da177e4SLinus Torvalds * 3651da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 3661da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 3671da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 3681da177e4SLinus Torvalds * 3691da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 3701da177e4SLinus Torvalds * __GFP_FS. 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 3731da177e4SLinus Torvalds struct page *page, int error) 3741da177e4SLinus Torvalds { 3757eaceaccSJens Axboe lock_page(page); 3763e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 3773e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 3781da177e4SLinus Torvalds unlock_page(page); 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 38104e62a29SChristoph Lameter /* possible outcome of pageout() */ 38204e62a29SChristoph Lameter typedef enum { 38304e62a29SChristoph Lameter /* failed to write page out, page is locked */ 38404e62a29SChristoph Lameter PAGE_KEEP, 38504e62a29SChristoph Lameter /* move page to the active list, page is locked */ 38604e62a29SChristoph Lameter PAGE_ACTIVATE, 38704e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 38804e62a29SChristoph Lameter PAGE_SUCCESS, 38904e62a29SChristoph Lameter /* page is clean and locked */ 39004e62a29SChristoph Lameter PAGE_CLEAN, 39104e62a29SChristoph Lameter } pageout_t; 39204e62a29SChristoph Lameter 3931da177e4SLinus Torvalds /* 3941742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3951742f19fSAndrew Morton * Calls ->writepage(). 3961da177e4SLinus Torvalds */ 397c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 3987d3579e8SKOSAKI Motohiro struct scan_control *sc) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds /* 4011da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 4021da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 4031da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 4041da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 4051da177e4SLinus Torvalds * PagePrivate for that. 4061da177e4SLinus Torvalds * 4076aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 4081da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 4091da177e4SLinus Torvalds * will block. 4101da177e4SLinus Torvalds * 4111da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4121da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4131da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4141da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4151da177e4SLinus Torvalds */ 4161da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4171da177e4SLinus Torvalds return PAGE_KEEP; 4181da177e4SLinus Torvalds if (!mapping) { 4191da177e4SLinus Torvalds /* 4201da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4211da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4221da177e4SLinus Torvalds */ 423266cf658SDavid Howells if (page_has_private(page)) { 4241da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4251da177e4SLinus Torvalds ClearPageDirty(page); 426d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4271da177e4SLinus Torvalds return PAGE_CLEAN; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds return PAGE_KEEP; 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4331da177e4SLinus Torvalds return PAGE_ACTIVATE; 4340e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4351da177e4SLinus Torvalds return PAGE_KEEP; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4381da177e4SLinus Torvalds int res; 4391da177e4SLinus Torvalds struct writeback_control wbc = { 4401da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4411da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 442111ebb6eSOGAWA Hirofumi .range_start = 0, 443111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4441da177e4SLinus Torvalds .for_reclaim = 1, 4451da177e4SLinus Torvalds }; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds SetPageReclaim(page); 4481da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4491da177e4SLinus Torvalds if (res < 0) 4501da177e4SLinus Torvalds handle_write_error(mapping, page, res); 451994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4521da177e4SLinus Torvalds ClearPageReclaim(page); 4531da177e4SLinus Torvalds return PAGE_ACTIVATE; 4541da177e4SLinus Torvalds } 455c661b078SAndy Whitcroft 4561da177e4SLinus Torvalds if (!PageWriteback(page)) { 4571da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4581da177e4SLinus Torvalds ClearPageReclaim(page); 4591da177e4SLinus Torvalds } 46023b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 461e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4621da177e4SLinus Torvalds return PAGE_SUCCESS; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds return PAGE_CLEAN; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds 468a649fd92SAndrew Morton /* 469e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 470e286781dSNick Piggin * gets returned with a refcount of 0. 471a649fd92SAndrew Morton */ 472e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 47349d2e9ccSChristoph Lameter { 47428e4d965SNick Piggin BUG_ON(!PageLocked(page)); 47528e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 47649d2e9ccSChristoph Lameter 47719fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 47849d2e9ccSChristoph Lameter /* 4790fd0e6b0SNick Piggin * The non racy check for a busy page. 4800fd0e6b0SNick Piggin * 4810fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 4820fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 4830fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 4840fd0e6b0SNick Piggin * here, then the following race may occur: 4850fd0e6b0SNick Piggin * 4860fd0e6b0SNick Piggin * get_user_pages(&page); 4870fd0e6b0SNick Piggin * [user mapping goes away] 4880fd0e6b0SNick Piggin * write_to(page); 4890fd0e6b0SNick Piggin * !PageDirty(page) [good] 4900fd0e6b0SNick Piggin * SetPageDirty(page); 4910fd0e6b0SNick Piggin * put_page(page); 4920fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 4930fd0e6b0SNick Piggin * 4940fd0e6b0SNick Piggin * [oops, our write_to data is lost] 4950fd0e6b0SNick Piggin * 4960fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 4970fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 4980fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 4990fd0e6b0SNick Piggin * 5000fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 5010fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 50249d2e9ccSChristoph Lameter */ 503e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 50449d2e9ccSChristoph Lameter goto cannot_free; 505e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 506e286781dSNick Piggin if (unlikely(PageDirty(page))) { 507e286781dSNick Piggin page_unfreeze_refs(page, 2); 50849d2e9ccSChristoph Lameter goto cannot_free; 509e286781dSNick Piggin } 51049d2e9ccSChristoph Lameter 51149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 51249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 51349d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 51419fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 515cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 516e286781dSNick Piggin } else { 5176072d13cSLinus Torvalds void (*freepage)(struct page *); 5186072d13cSLinus Torvalds 5196072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5206072d13cSLinus Torvalds 521e64a782fSMinchan Kim __delete_from_page_cache(page); 52219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 523e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5246072d13cSLinus Torvalds 5256072d13cSLinus Torvalds if (freepage != NULL) 5266072d13cSLinus Torvalds freepage(page); 527e286781dSNick Piggin } 528e286781dSNick Piggin 52949d2e9ccSChristoph Lameter return 1; 53049d2e9ccSChristoph Lameter 53149d2e9ccSChristoph Lameter cannot_free: 53219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 53349d2e9ccSChristoph Lameter return 0; 53449d2e9ccSChristoph Lameter } 53549d2e9ccSChristoph Lameter 5361da177e4SLinus Torvalds /* 537e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 538e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 539e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 540e286781dSNick Piggin * this page. 541e286781dSNick Piggin */ 542e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 543e286781dSNick Piggin { 544e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 545e286781dSNick Piggin /* 546e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 547e286781dSNick Piggin * drops the pagecache ref for us without requiring another 548e286781dSNick Piggin * atomic operation. 549e286781dSNick Piggin */ 550e286781dSNick Piggin page_unfreeze_refs(page, 1); 551e286781dSNick Piggin return 1; 552e286781dSNick Piggin } 553e286781dSNick Piggin return 0; 554e286781dSNick Piggin } 555e286781dSNick Piggin 556894bc310SLee Schermerhorn /** 557894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 558894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 559894bc310SLee Schermerhorn * 560894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 561894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 562894bc310SLee Schermerhorn * 563894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 564894bc310SLee Schermerhorn */ 565894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 566894bc310SLee Schermerhorn { 5670ec3b74cSVlastimil Babka bool is_unevictable; 568bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 569894bc310SLee Schermerhorn 570894bc310SLee Schermerhorn VM_BUG_ON(PageLRU(page)); 571894bc310SLee Schermerhorn 572894bc310SLee Schermerhorn redo: 573894bc310SLee Schermerhorn ClearPageUnevictable(page); 574894bc310SLee Schermerhorn 57539b5f29aSHugh Dickins if (page_evictable(page)) { 576894bc310SLee Schermerhorn /* 577894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 578894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 579894bc310SLee Schermerhorn * unevictable page on [in]active list. 580894bc310SLee Schermerhorn * We know how to handle that. 581894bc310SLee Schermerhorn */ 5820ec3b74cSVlastimil Babka is_unevictable = false; 583c53954a0SMel Gorman lru_cache_add(page); 584894bc310SLee Schermerhorn } else { 585894bc310SLee Schermerhorn /* 586894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 587894bc310SLee Schermerhorn * list. 588894bc310SLee Schermerhorn */ 5890ec3b74cSVlastimil Babka is_unevictable = true; 590894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 5916a7b9548SJohannes Weiner /* 59221ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 59321ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 59421ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 59524513264SHugh Dickins * isolation/check_move_unevictable_pages, 59621ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 5976a7b9548SJohannes Weiner * the page back to the evictable list. 5986a7b9548SJohannes Weiner * 59921ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 6006a7b9548SJohannes Weiner */ 6016a7b9548SJohannes Weiner smp_mb(); 602894bc310SLee Schermerhorn } 603894bc310SLee Schermerhorn 604894bc310SLee Schermerhorn /* 605894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 606894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 607894bc310SLee Schermerhorn * check after we added it to the list, again. 608894bc310SLee Schermerhorn */ 6090ec3b74cSVlastimil Babka if (is_unevictable && page_evictable(page)) { 610894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 611894bc310SLee Schermerhorn put_page(page); 612894bc310SLee Schermerhorn goto redo; 613894bc310SLee Schermerhorn } 614894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 615894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 616894bc310SLee Schermerhorn * nothing to do here. 617894bc310SLee Schermerhorn */ 618894bc310SLee Schermerhorn } 619894bc310SLee Schermerhorn 6200ec3b74cSVlastimil Babka if (was_unevictable && !is_unevictable) 621bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 6220ec3b74cSVlastimil Babka else if (!was_unevictable && is_unevictable) 623bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 624bbfd28eeSLee Schermerhorn 625894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 626894bc310SLee Schermerhorn } 627894bc310SLee Schermerhorn 628dfc8d636SJohannes Weiner enum page_references { 629dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 630dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 63164574746SJohannes Weiner PAGEREF_KEEP, 632dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 633dfc8d636SJohannes Weiner }; 634dfc8d636SJohannes Weiner 635dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 636dfc8d636SJohannes Weiner struct scan_control *sc) 637dfc8d636SJohannes Weiner { 63864574746SJohannes Weiner int referenced_ptes, referenced_page; 639dfc8d636SJohannes Weiner unsigned long vm_flags; 640dfc8d636SJohannes Weiner 641c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 642c3ac9a8aSJohannes Weiner &vm_flags); 64364574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 644dfc8d636SJohannes Weiner 645dfc8d636SJohannes Weiner /* 646dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 647dfc8d636SJohannes Weiner * move the page to the unevictable list. 648dfc8d636SJohannes Weiner */ 649dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 650dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 651dfc8d636SJohannes Weiner 65264574746SJohannes Weiner if (referenced_ptes) { 653e4898273SMichal Hocko if (PageSwapBacked(page)) 65464574746SJohannes Weiner return PAGEREF_ACTIVATE; 65564574746SJohannes Weiner /* 65664574746SJohannes Weiner * All mapped pages start out with page table 65764574746SJohannes Weiner * references from the instantiating fault, so we need 65864574746SJohannes Weiner * to look twice if a mapped file page is used more 65964574746SJohannes Weiner * than once. 66064574746SJohannes Weiner * 66164574746SJohannes Weiner * Mark it and spare it for another trip around the 66264574746SJohannes Weiner * inactive list. Another page table reference will 66364574746SJohannes Weiner * lead to its activation. 66464574746SJohannes Weiner * 66564574746SJohannes Weiner * Note: the mark is set for activated pages as well 66664574746SJohannes Weiner * so that recently deactivated but used pages are 66764574746SJohannes Weiner * quickly recovered. 66864574746SJohannes Weiner */ 66964574746SJohannes Weiner SetPageReferenced(page); 67064574746SJohannes Weiner 67134dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 672dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 673dfc8d636SJohannes Weiner 674c909e993SKonstantin Khlebnikov /* 675c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 676c909e993SKonstantin Khlebnikov */ 677c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 678c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 679c909e993SKonstantin Khlebnikov 68064574746SJohannes Weiner return PAGEREF_KEEP; 68164574746SJohannes Weiner } 68264574746SJohannes Weiner 683dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 6842e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 685dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 68664574746SJohannes Weiner 68764574746SJohannes Weiner return PAGEREF_RECLAIM; 688dfc8d636SJohannes Weiner } 689dfc8d636SJohannes Weiner 690e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 691e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 692e2be15f6SMel Gorman bool *dirty, bool *writeback) 693e2be15f6SMel Gorman { 694b4597226SMel Gorman struct address_space *mapping; 695b4597226SMel Gorman 696e2be15f6SMel Gorman /* 697e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 698e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 699e2be15f6SMel Gorman */ 700e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 701e2be15f6SMel Gorman *dirty = false; 702e2be15f6SMel Gorman *writeback = false; 703e2be15f6SMel Gorman return; 704e2be15f6SMel Gorman } 705e2be15f6SMel Gorman 706e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 707e2be15f6SMel Gorman *dirty = PageDirty(page); 708e2be15f6SMel Gorman *writeback = PageWriteback(page); 709b4597226SMel Gorman 710b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 711b4597226SMel Gorman if (!page_has_private(page)) 712b4597226SMel Gorman return; 713b4597226SMel Gorman 714b4597226SMel Gorman mapping = page_mapping(page); 715b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 716b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 717e2be15f6SMel Gorman } 718e2be15f6SMel Gorman 719e286781dSNick Piggin /* 7201742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 7211da177e4SLinus Torvalds */ 7221742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 7236a18adb3SKonstantin Khlebnikov struct zone *zone, 724f84f6e2bSMel Gorman struct scan_control *sc, 72502c6de8dSMinchan Kim enum ttu_flags ttu_flags, 7268e950282SMel Gorman unsigned long *ret_nr_dirty, 727d43006d5SMel Gorman unsigned long *ret_nr_unqueued_dirty, 7288e950282SMel Gorman unsigned long *ret_nr_congested, 72902c6de8dSMinchan Kim unsigned long *ret_nr_writeback, 730b1a6f21eSMel Gorman unsigned long *ret_nr_immediate, 73102c6de8dSMinchan Kim bool force_reclaim) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds LIST_HEAD(ret_pages); 734abe4c3b5SMel Gorman LIST_HEAD(free_pages); 7351da177e4SLinus Torvalds int pgactivate = 0; 736d43006d5SMel Gorman unsigned long nr_unqueued_dirty = 0; 7370e093d99SMel Gorman unsigned long nr_dirty = 0; 7380e093d99SMel Gorman unsigned long nr_congested = 0; 73905ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 74092df3a72SMel Gorman unsigned long nr_writeback = 0; 741b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds cond_resched(); 7441da177e4SLinus Torvalds 74569980e31STim Chen mem_cgroup_uncharge_start(); 7461da177e4SLinus Torvalds while (!list_empty(page_list)) { 7471da177e4SLinus Torvalds struct address_space *mapping; 7481da177e4SLinus Torvalds struct page *page; 7491da177e4SLinus Torvalds int may_enter_fs; 75002c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 751e2be15f6SMel Gorman bool dirty, writeback; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds cond_resched(); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds page = lru_to_page(page_list); 7561da177e4SLinus Torvalds list_del(&page->lru); 7571da177e4SLinus Torvalds 758529ae9aaSNick Piggin if (!trylock_page(page)) 7591da177e4SLinus Torvalds goto keep; 7601da177e4SLinus Torvalds 761725d704eSNick Piggin VM_BUG_ON(PageActive(page)); 7626a18adb3SKonstantin Khlebnikov VM_BUG_ON(page_zone(page) != zone); 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds sc->nr_scanned++; 76580e43426SChristoph Lameter 76639b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 767b291f000SNick Piggin goto cull_mlocked; 768894bc310SLee Schermerhorn 769a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 77080e43426SChristoph Lameter goto keep_locked; 77180e43426SChristoph Lameter 7721da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 7731da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 7741da177e4SLinus Torvalds sc->nr_scanned++; 7751da177e4SLinus Torvalds 776c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 777c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 778c661b078SAndy Whitcroft 779e62e384eSMichal Hocko /* 780e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 781e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 782e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 783e2be15f6SMel Gorman * is all dirty unqueued pages. 784e2be15f6SMel Gorman */ 785e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 786e2be15f6SMel Gorman if (dirty || writeback) 787e2be15f6SMel Gorman nr_dirty++; 788e2be15f6SMel Gorman 789e2be15f6SMel Gorman if (dirty && !writeback) 790e2be15f6SMel Gorman nr_unqueued_dirty++; 791e2be15f6SMel Gorman 792d04e8acdSMel Gorman /* 793d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 794d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 795d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 796d04e8acdSMel Gorman * end of the LRU a second time. 797d04e8acdSMel Gorman */ 798e2be15f6SMel Gorman mapping = page_mapping(page); 799d04e8acdSMel Gorman if ((mapping && bdi_write_congested(mapping->backing_dev_info)) || 800d04e8acdSMel Gorman (writeback && PageReclaim(page))) 801e2be15f6SMel Gorman nr_congested++; 802e2be15f6SMel Gorman 803e2be15f6SMel Gorman /* 804283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 805283aba9fSMel Gorman * are three cases to consider. 806e62e384eSMichal Hocko * 807283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 808283aba9fSMel Gorman * under writeback and this page is both under writeback and 809283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 810283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 811283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 812283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 813283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 814b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 815b1a6f21eSMel Gorman * caller can stall after page list has been processed. 816c3b94f44SHugh Dickins * 817283aba9fSMel Gorman * 2) Global reclaim encounters a page, memcg encounters a 818283aba9fSMel Gorman * page that is not marked for immediate reclaim or 819283aba9fSMel Gorman * the caller does not have __GFP_IO. In this case mark 820283aba9fSMel Gorman * the page for immediate reclaim and continue scanning. 821283aba9fSMel Gorman * 822283aba9fSMel Gorman * __GFP_IO is checked because a loop driver thread might 823283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 824283aba9fSMel Gorman * which it is needed to do the write (loop masks off 825283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 826283aba9fSMel Gorman * would probably show more reasons. 827283aba9fSMel Gorman * 828283aba9fSMel Gorman * Don't require __GFP_FS, since we're not going into the 829283aba9fSMel Gorman * FS, just waiting on its writeback completion. Worryingly, 830283aba9fSMel Gorman * ext4 gfs2 and xfs allocate pages with 831283aba9fSMel Gorman * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing 832283aba9fSMel Gorman * may_enter_fs here is liable to OOM on them. 833283aba9fSMel Gorman * 834283aba9fSMel Gorman * 3) memcg encounters a page that is not already marked 835283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 836283aba9fSMel Gorman * throttling so we could easily OOM just because too many 837283aba9fSMel Gorman * pages are in writeback and there is nothing else to 838283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 839e62e384eSMichal Hocko */ 840283aba9fSMel Gorman if (PageWriteback(page)) { 841283aba9fSMel Gorman /* Case 1 above */ 842283aba9fSMel Gorman if (current_is_kswapd() && 843283aba9fSMel Gorman PageReclaim(page) && 844283aba9fSMel Gorman zone_is_reclaim_writeback(zone)) { 845b1a6f21eSMel Gorman nr_immediate++; 846b1a6f21eSMel Gorman goto keep_locked; 847283aba9fSMel Gorman 848283aba9fSMel Gorman /* Case 2 above */ 849283aba9fSMel Gorman } else if (global_reclaim(sc) || 850c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 851c3b94f44SHugh Dickins /* 852c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 853c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 854c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 855c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 856c3b94f44SHugh Dickins * enough to care. What we do want is for this 857c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 858c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 859c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 860c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 861c3b94f44SHugh Dickins */ 862c3b94f44SHugh Dickins SetPageReclaim(page); 86392df3a72SMel Gorman nr_writeback++; 864283aba9fSMel Gorman 865c3b94f44SHugh Dickins goto keep_locked; 866283aba9fSMel Gorman 867283aba9fSMel Gorman /* Case 3 above */ 868283aba9fSMel Gorman } else { 869c3b94f44SHugh Dickins wait_on_page_writeback(page); 870e62e384eSMichal Hocko } 871283aba9fSMel Gorman } 8721da177e4SLinus Torvalds 87302c6de8dSMinchan Kim if (!force_reclaim) 8746a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 87502c6de8dSMinchan Kim 876dfc8d636SJohannes Weiner switch (references) { 877dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 8781da177e4SLinus Torvalds goto activate_locked; 87964574746SJohannes Weiner case PAGEREF_KEEP: 88064574746SJohannes Weiner goto keep_locked; 881dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 882dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 883dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 884dfc8d636SJohannes Weiner } 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds /* 8871da177e4SLinus Torvalds * Anonymous process memory has backing store? 8881da177e4SLinus Torvalds * Try to allocate it some swap space here. 8891da177e4SLinus Torvalds */ 890b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 89163eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 89263eb6b93SHugh Dickins goto keep_locked; 8935bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 8941da177e4SLinus Torvalds goto activate_locked; 89563eb6b93SHugh Dickins may_enter_fs = 1; 8961da177e4SLinus Torvalds 897e2be15f6SMel Gorman /* Adding to swap updated mapping */ 8981da177e4SLinus Torvalds mapping = page_mapping(page); 899e2be15f6SMel Gorman } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* 9021da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 9031da177e4SLinus Torvalds * processes. Try to unmap it here. 9041da177e4SLinus Torvalds */ 9051da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 90602c6de8dSMinchan Kim switch (try_to_unmap(page, ttu_flags)) { 9071da177e4SLinus Torvalds case SWAP_FAIL: 9081da177e4SLinus Torvalds goto activate_locked; 9091da177e4SLinus Torvalds case SWAP_AGAIN: 9101da177e4SLinus Torvalds goto keep_locked; 911b291f000SNick Piggin case SWAP_MLOCK: 912b291f000SNick Piggin goto cull_mlocked; 9131da177e4SLinus Torvalds case SWAP_SUCCESS: 9141da177e4SLinus Torvalds ; /* try to free the page below */ 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (PageDirty(page)) { 919ee72886dSMel Gorman /* 920ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 921d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 922d43006d5SMel Gorman * if many dirty pages have been encountered. 923ee72886dSMel Gorman */ 924f84f6e2bSMel Gorman if (page_is_file_cache(page) && 9259e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 926d43006d5SMel Gorman !zone_is_reclaim_dirty(zone))) { 92749ea7eb6SMel Gorman /* 92849ea7eb6SMel Gorman * Immediately reclaim when written back. 92949ea7eb6SMel Gorman * Similar in principal to deactivate_page() 93049ea7eb6SMel Gorman * except we already have the page isolated 93149ea7eb6SMel Gorman * and know it's dirty 93249ea7eb6SMel Gorman */ 93349ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 93449ea7eb6SMel Gorman SetPageReclaim(page); 93549ea7eb6SMel Gorman 936ee72886dSMel Gorman goto keep_locked; 937ee72886dSMel Gorman } 938ee72886dSMel Gorman 939dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 9401da177e4SLinus Torvalds goto keep_locked; 9414dd4b920SAndrew Morton if (!may_enter_fs) 9421da177e4SLinus Torvalds goto keep_locked; 94352a8363eSChristoph Lameter if (!sc->may_writepage) 9441da177e4SLinus Torvalds goto keep_locked; 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 9477d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 9481da177e4SLinus Torvalds case PAGE_KEEP: 9491da177e4SLinus Torvalds goto keep_locked; 9501da177e4SLinus Torvalds case PAGE_ACTIVATE: 9511da177e4SLinus Torvalds goto activate_locked; 9521da177e4SLinus Torvalds case PAGE_SUCCESS: 9537d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 95441ac1999SMel Gorman goto keep; 9557d3579e8SKOSAKI Motohiro if (PageDirty(page)) 9561da177e4SLinus Torvalds goto keep; 9577d3579e8SKOSAKI Motohiro 9581da177e4SLinus Torvalds /* 9591da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 9601da177e4SLinus Torvalds * ahead and try to reclaim the page. 9611da177e4SLinus Torvalds */ 962529ae9aaSNick Piggin if (!trylock_page(page)) 9631da177e4SLinus Torvalds goto keep; 9641da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 9651da177e4SLinus Torvalds goto keep_locked; 9661da177e4SLinus Torvalds mapping = page_mapping(page); 9671da177e4SLinus Torvalds case PAGE_CLEAN: 9681da177e4SLinus Torvalds ; /* try to free the page below */ 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds /* 9731da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 9741da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 9751da177e4SLinus Torvalds * the page as well. 9761da177e4SLinus Torvalds * 9771da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 9781da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 9791da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 9801da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 9811da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 9821da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 9831da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 9841da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 9851da177e4SLinus Torvalds * 9861da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 9871da177e4SLinus Torvalds * the pages which were not successfully invalidated in 9881da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 9891da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 9901da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 9911da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 9921da177e4SLinus Torvalds */ 993266cf658SDavid Howells if (page_has_private(page)) { 9941da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 9951da177e4SLinus Torvalds goto activate_locked; 996e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 997e286781dSNick Piggin unlock_page(page); 998e286781dSNick Piggin if (put_page_testzero(page)) 9991da177e4SLinus Torvalds goto free_it; 1000e286781dSNick Piggin else { 1001e286781dSNick Piggin /* 1002e286781dSNick Piggin * rare race with speculative reference. 1003e286781dSNick Piggin * the speculative reference will free 1004e286781dSNick Piggin * this page shortly, so we may 1005e286781dSNick Piggin * increment nr_reclaimed here (and 1006e286781dSNick Piggin * leave it off the LRU). 1007e286781dSNick Piggin */ 1008e286781dSNick Piggin nr_reclaimed++; 1009e286781dSNick Piggin continue; 1010e286781dSNick Piggin } 1011e286781dSNick Piggin } 10121da177e4SLinus Torvalds } 10131da177e4SLinus Torvalds 1014e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 101549d2e9ccSChristoph Lameter goto keep_locked; 10161da177e4SLinus Torvalds 1017a978d6f5SNick Piggin /* 1018a978d6f5SNick Piggin * At this point, we have no other references and there is 1019a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1020a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1021a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1022a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1023a978d6f5SNick Piggin */ 1024a978d6f5SNick Piggin __clear_page_locked(page); 1025e286781dSNick Piggin free_it: 102605ff5137SAndrew Morton nr_reclaimed++; 1027abe4c3b5SMel Gorman 1028abe4c3b5SMel Gorman /* 1029abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1030abe4c3b5SMel Gorman * appear not as the counts should be low 1031abe4c3b5SMel Gorman */ 1032abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 10331da177e4SLinus Torvalds continue; 10341da177e4SLinus Torvalds 1035b291f000SNick Piggin cull_mlocked: 103663d6c5adSHugh Dickins if (PageSwapCache(page)) 103763d6c5adSHugh Dickins try_to_free_swap(page); 1038b291f000SNick Piggin unlock_page(page); 1039b291f000SNick Piggin putback_lru_page(page); 1040b291f000SNick Piggin continue; 1041b291f000SNick Piggin 10421da177e4SLinus Torvalds activate_locked: 104368a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 104468a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 1045a2c43eedSHugh Dickins try_to_free_swap(page); 1046894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 10471da177e4SLinus Torvalds SetPageActive(page); 10481da177e4SLinus Torvalds pgactivate++; 10491da177e4SLinus Torvalds keep_locked: 10501da177e4SLinus Torvalds unlock_page(page); 10511da177e4SLinus Torvalds keep: 10521da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1053b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 10541da177e4SLinus Torvalds } 1055abe4c3b5SMel Gorman 1056cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 1057abe4c3b5SMel Gorman 10581da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1059f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 106069980e31STim Chen mem_cgroup_uncharge_end(); 10618e950282SMel Gorman *ret_nr_dirty += nr_dirty; 10628e950282SMel Gorman *ret_nr_congested += nr_congested; 1063d43006d5SMel Gorman *ret_nr_unqueued_dirty += nr_unqueued_dirty; 106492df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 1065b1a6f21eSMel Gorman *ret_nr_immediate += nr_immediate; 106605ff5137SAndrew Morton return nr_reclaimed; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 106902c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 107002c6de8dSMinchan Kim struct list_head *page_list) 107102c6de8dSMinchan Kim { 107202c6de8dSMinchan Kim struct scan_control sc = { 107302c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 107402c6de8dSMinchan Kim .priority = DEF_PRIORITY, 107502c6de8dSMinchan Kim .may_unmap = 1, 107602c6de8dSMinchan Kim }; 10778e950282SMel Gorman unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; 107802c6de8dSMinchan Kim struct page *page, *next; 107902c6de8dSMinchan Kim LIST_HEAD(clean_pages); 108002c6de8dSMinchan Kim 108102c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 108202c6de8dSMinchan Kim if (page_is_file_cache(page) && !PageDirty(page)) { 108302c6de8dSMinchan Kim ClearPageActive(page); 108402c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 108502c6de8dSMinchan Kim } 108602c6de8dSMinchan Kim } 108702c6de8dSMinchan Kim 108802c6de8dSMinchan Kim ret = shrink_page_list(&clean_pages, zone, &sc, 108902c6de8dSMinchan Kim TTU_UNMAP|TTU_IGNORE_ACCESS, 10908e950282SMel Gorman &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); 109102c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 109202c6de8dSMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret); 109302c6de8dSMinchan Kim return ret; 109402c6de8dSMinchan Kim } 109502c6de8dSMinchan Kim 10965ad333ebSAndy Whitcroft /* 10975ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 10985ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 10995ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 11005ad333ebSAndy Whitcroft * 11015ad333ebSAndy Whitcroft * page: page to consider 11025ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 11035ad333ebSAndy Whitcroft * 11045ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 11055ad333ebSAndy Whitcroft */ 1106f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 11075ad333ebSAndy Whitcroft { 11085ad333ebSAndy Whitcroft int ret = -EINVAL; 11095ad333ebSAndy Whitcroft 11105ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 11115ad333ebSAndy Whitcroft if (!PageLRU(page)) 11125ad333ebSAndy Whitcroft return ret; 11135ad333ebSAndy Whitcroft 1114e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1115e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1116894bc310SLee Schermerhorn return ret; 1117894bc310SLee Schermerhorn 11185ad333ebSAndy Whitcroft ret = -EBUSY; 111908e552c6SKAMEZAWA Hiroyuki 1120c8244935SMel Gorman /* 1121c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1122c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1123c8244935SMel Gorman * blocking - clean pages for the most part. 1124c8244935SMel Gorman * 1125c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1126c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1127c8244935SMel Gorman * 1128c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1129c8244935SMel Gorman * that it is possible to migrate without blocking 1130c8244935SMel Gorman */ 1131c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1132c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1133c8244935SMel Gorman if (PageWriteback(page)) 113439deaf85SMinchan Kim return ret; 113539deaf85SMinchan Kim 1136c8244935SMel Gorman if (PageDirty(page)) { 1137c8244935SMel Gorman struct address_space *mapping; 1138c8244935SMel Gorman 1139c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1140c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1141c8244935SMel Gorman return ret; 1142c8244935SMel Gorman 1143c8244935SMel Gorman /* 1144c8244935SMel Gorman * Only pages without mappings or that have a 1145c8244935SMel Gorman * ->migratepage callback are possible to migrate 1146c8244935SMel Gorman * without blocking 1147c8244935SMel Gorman */ 1148c8244935SMel Gorman mapping = page_mapping(page); 1149c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1150c8244935SMel Gorman return ret; 1151c8244935SMel Gorman } 1152c8244935SMel Gorman } 1153c8244935SMel Gorman 1154f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1155f80c0673SMinchan Kim return ret; 1156f80c0673SMinchan Kim 11575ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 11585ad333ebSAndy Whitcroft /* 11595ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 11605ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 11615ad333ebSAndy Whitcroft * page release code relies on it. 11625ad333ebSAndy Whitcroft */ 11635ad333ebSAndy Whitcroft ClearPageLRU(page); 11645ad333ebSAndy Whitcroft ret = 0; 11655ad333ebSAndy Whitcroft } 11665ad333ebSAndy Whitcroft 11675ad333ebSAndy Whitcroft return ret; 11685ad333ebSAndy Whitcroft } 11695ad333ebSAndy Whitcroft 117049d2e9ccSChristoph Lameter /* 11711da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 11721da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 11731da177e4SLinus Torvalds * and working on them outside the LRU lock. 11741da177e4SLinus Torvalds * 11751da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 11761da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 11771da177e4SLinus Torvalds * 11781da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 11791da177e4SLinus Torvalds * 11801da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 11815dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 11821da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1183f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1184fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 11855ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 11863cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 11871da177e4SLinus Torvalds * 11881da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 11891da177e4SLinus Torvalds */ 119069e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 11915dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1192fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 11933cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 11941da177e4SLinus Torvalds { 119575b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 119669e05944SAndrew Morton unsigned long nr_taken = 0; 1197c9b02d97SWu Fengguang unsigned long scan; 11981da177e4SLinus Torvalds 1199c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 12005ad333ebSAndy Whitcroft struct page *page; 1201fa9add64SHugh Dickins int nr_pages; 12025ad333ebSAndy Whitcroft 12031da177e4SLinus Torvalds page = lru_to_page(src); 12041da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 12051da177e4SLinus Torvalds 1206725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 12078d438f96SNick Piggin 1208f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 12095ad333ebSAndy Whitcroft case 0: 1210fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1211fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 12125ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1213fa9add64SHugh Dickins nr_taken += nr_pages; 12145ad333ebSAndy Whitcroft break; 12157c8ee9a8SNick Piggin 12165ad333ebSAndy Whitcroft case -EBUSY: 12175ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 12185ad333ebSAndy Whitcroft list_move(&page->lru, src); 12195ad333ebSAndy Whitcroft continue; 12205ad333ebSAndy Whitcroft 12215ad333ebSAndy Whitcroft default: 12225ad333ebSAndy Whitcroft BUG(); 12235ad333ebSAndy Whitcroft } 12245ad333ebSAndy Whitcroft } 12251da177e4SLinus Torvalds 1226f626012dSHugh Dickins *nr_scanned = scan; 122775b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 122875b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 12291da177e4SLinus Torvalds return nr_taken; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 123262695a84SNick Piggin /** 123362695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 123462695a84SNick Piggin * @page: page to isolate from its LRU list 123562695a84SNick Piggin * 123662695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 123762695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 123862695a84SNick Piggin * 123962695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 124062695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 124162695a84SNick Piggin * 124262695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1243894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1244894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1245894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 124662695a84SNick Piggin * 124762695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 124862695a84SNick Piggin * found will be decremented. 124962695a84SNick Piggin * 125062695a84SNick Piggin * Restrictions: 125162695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 125262695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 125362695a84SNick Piggin * without a stable reference). 125462695a84SNick Piggin * (2) the lru_lock must not be held. 125562695a84SNick Piggin * (3) interrupts must be enabled. 125662695a84SNick Piggin */ 125762695a84SNick Piggin int isolate_lru_page(struct page *page) 125862695a84SNick Piggin { 125962695a84SNick Piggin int ret = -EBUSY; 126062695a84SNick Piggin 12610c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 12620c917313SKonstantin Khlebnikov 126362695a84SNick Piggin if (PageLRU(page)) { 126462695a84SNick Piggin struct zone *zone = page_zone(page); 1265fa9add64SHugh Dickins struct lruvec *lruvec; 126662695a84SNick Piggin 126762695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1268fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 12690c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1270894bc310SLee Schermerhorn int lru = page_lru(page); 12710c917313SKonstantin Khlebnikov get_page(page); 127262695a84SNick Piggin ClearPageLRU(page); 1273fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1274fa9add64SHugh Dickins ret = 0; 127562695a84SNick Piggin } 127662695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 127762695a84SNick Piggin } 127862695a84SNick Piggin return ret; 127962695a84SNick Piggin } 128062695a84SNick Piggin 12815ad333ebSAndy Whitcroft /* 1282d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1283d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1284d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1285d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1286d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 128735cd7815SRik van Riel */ 128835cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 128935cd7815SRik van Riel struct scan_control *sc) 129035cd7815SRik van Riel { 129135cd7815SRik van Riel unsigned long inactive, isolated; 129235cd7815SRik van Riel 129335cd7815SRik van Riel if (current_is_kswapd()) 129435cd7815SRik van Riel return 0; 129535cd7815SRik van Riel 129689b5fae5SJohannes Weiner if (!global_reclaim(sc)) 129735cd7815SRik van Riel return 0; 129835cd7815SRik van Riel 129935cd7815SRik van Riel if (file) { 130035cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 130135cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 130235cd7815SRik van Riel } else { 130335cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 130435cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 130535cd7815SRik van Riel } 130635cd7815SRik van Riel 13073cf23841SFengguang Wu /* 13083cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 13093cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 13103cf23841SFengguang Wu * deadlock. 13113cf23841SFengguang Wu */ 13123cf23841SFengguang Wu if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS) 13133cf23841SFengguang Wu inactive >>= 3; 13143cf23841SFengguang Wu 131535cd7815SRik van Riel return isolated > inactive; 131635cd7815SRik van Riel } 131735cd7815SRik van Riel 131866635629SMel Gorman static noinline_for_stack void 131975b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 132066635629SMel Gorman { 132127ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 132227ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13233f79768fSHugh Dickins LIST_HEAD(pages_to_free); 132466635629SMel Gorman 132566635629SMel Gorman /* 132666635629SMel Gorman * Put back any unfreeable pages. 132766635629SMel Gorman */ 132866635629SMel Gorman while (!list_empty(page_list)) { 13293f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 133066635629SMel Gorman int lru; 13313f79768fSHugh Dickins 133266635629SMel Gorman VM_BUG_ON(PageLRU(page)); 133366635629SMel Gorman list_del(&page->lru); 133439b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 133566635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 133666635629SMel Gorman putback_lru_page(page); 133766635629SMel Gorman spin_lock_irq(&zone->lru_lock); 133866635629SMel Gorman continue; 133966635629SMel Gorman } 1340fa9add64SHugh Dickins 1341fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1342fa9add64SHugh Dickins 13437a608572SLinus Torvalds SetPageLRU(page); 134466635629SMel Gorman lru = page_lru(page); 1345fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1346fa9add64SHugh Dickins 134766635629SMel Gorman if (is_active_lru(lru)) { 134866635629SMel Gorman int file = is_file_lru(lru); 13499992af10SRik van Riel int numpages = hpage_nr_pages(page); 13509992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 135166635629SMel Gorman } 13522bcf8879SHugh Dickins if (put_page_testzero(page)) { 13532bcf8879SHugh Dickins __ClearPageLRU(page); 13542bcf8879SHugh Dickins __ClearPageActive(page); 1355fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 13562bcf8879SHugh Dickins 13572bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 135866635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 13592bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 136066635629SMel Gorman spin_lock_irq(&zone->lru_lock); 13612bcf8879SHugh Dickins } else 13622bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 136366635629SMel Gorman } 136466635629SMel Gorman } 136566635629SMel Gorman 13663f79768fSHugh Dickins /* 13673f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 13683f79768fSHugh Dickins */ 13693f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 137066635629SMel Gorman } 137166635629SMel Gorman 137266635629SMel Gorman /* 13731742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 13741742f19fSAndrew Morton * of reclaimed pages 13751da177e4SLinus Torvalds */ 137666635629SMel Gorman static noinline_for_stack unsigned long 13771a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 13789e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 13791da177e4SLinus Torvalds { 13801da177e4SLinus Torvalds LIST_HEAD(page_list); 1381e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 138205ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1383e247dbceSKOSAKI Motohiro unsigned long nr_taken; 13848e950282SMel Gorman unsigned long nr_dirty = 0; 13858e950282SMel Gorman unsigned long nr_congested = 0; 1386e2be15f6SMel Gorman unsigned long nr_unqueued_dirty = 0; 138792df3a72SMel Gorman unsigned long nr_writeback = 0; 1388b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 1389f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 13903cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 13911a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13921a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 139378dc583dSKOSAKI Motohiro 139435cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 139558355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 139635cd7815SRik van Riel 139735cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 139835cd7815SRik van Riel if (fatal_signal_pending(current)) 139935cd7815SRik van Riel return SWAP_CLUSTER_MAX; 140035cd7815SRik van Riel } 140135cd7815SRik van Riel 14021da177e4SLinus Torvalds lru_add_drain(); 1403f80c0673SMinchan Kim 1404f80c0673SMinchan Kim if (!sc->may_unmap) 140561317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1406f80c0673SMinchan Kim if (!sc->may_writepage) 140761317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1408f80c0673SMinchan Kim 14091da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 14101da177e4SLinus Torvalds 14115dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 14125dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 141395d918fcSKonstantin Khlebnikov 141495d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 141595d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 141695d918fcSKonstantin Khlebnikov 141789b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1418e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1419b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 142075b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1421b35ea17bSKOSAKI Motohiro else 142275b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1423b35ea17bSKOSAKI Motohiro } 142466635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1425d563c050SHillf Danton 1426d563c050SHillf Danton if (nr_taken == 0) 142766635629SMel Gorman return 0; 1428b35ea17bSKOSAKI Motohiro 142902c6de8dSMinchan Kim nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP, 14308e950282SMel Gorman &nr_dirty, &nr_unqueued_dirty, &nr_congested, 14318e950282SMel Gorman &nr_writeback, &nr_immediate, 1432b1a6f21eSMel Gorman false); 1433c661b078SAndy Whitcroft 14343f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 14353f79768fSHugh Dickins 143695d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1437d563c050SHillf Danton 1438904249aaSYing Han if (global_reclaim(sc)) { 1439b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1440904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1441904249aaSYing Han nr_reclaimed); 1442904249aaSYing Han else 1443904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1444904249aaSYing Han nr_reclaimed); 1445904249aaSYing Han } 1446a74609faSNick Piggin 144727ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 14483f79768fSHugh Dickins 144995d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 14503f79768fSHugh Dickins 14513f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 14523f79768fSHugh Dickins 14533f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1454e11da5b4SMel Gorman 145592df3a72SMel Gorman /* 145692df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 145792df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 145892df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 145992df3a72SMel Gorman * at throttling processes due to the page distribution throughout 146092df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 146192df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 146292df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 146392df3a72SMel Gorman * same way balance_dirty_pages() manages. 146492df3a72SMel Gorman * 14658e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 14668e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 14678e950282SMel Gorman * are encountered in the nr_immediate check below. 146892df3a72SMel Gorman */ 1469918fc718SMel Gorman if (nr_writeback && nr_writeback == nr_taken) 1470283aba9fSMel Gorman zone_set_flag(zone, ZONE_WRITEBACK); 147192df3a72SMel Gorman 1472d43006d5SMel Gorman /* 1473b1a6f21eSMel Gorman * memcg will stall in page writeback so only consider forcibly 1474b1a6f21eSMel Gorman * stalling for global reclaim 1475d43006d5SMel Gorman */ 1476b1a6f21eSMel Gorman if (global_reclaim(sc)) { 1477b1a6f21eSMel Gorman /* 14788e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 14798e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 14808e950282SMel Gorman */ 14818e950282SMel Gorman if (nr_dirty && nr_dirty == nr_congested) 14828e950282SMel Gorman zone_set_flag(zone, ZONE_CONGESTED); 14838e950282SMel Gorman 14848e950282SMel Gorman /* 1485b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1486b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 1487b1a6f21eSMel Gorman * the zone ZONE_TAIL_LRU_DIRTY and kswapd will start writing 1488b1a6f21eSMel Gorman * pages from reclaim context. It will forcibly stall in the 1489b1a6f21eSMel Gorman * next check. 1490b1a6f21eSMel Gorman */ 1491b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken) 1492d43006d5SMel Gorman zone_set_flag(zone, ZONE_TAIL_LRU_DIRTY); 1493b1a6f21eSMel Gorman 1494b1a6f21eSMel Gorman /* 1495b1a6f21eSMel Gorman * In addition, if kswapd scans pages marked marked for 1496b1a6f21eSMel Gorman * immediate reclaim and under writeback (nr_immediate), it 1497b1a6f21eSMel Gorman * implies that pages are cycling through the LRU faster than 1498b1a6f21eSMel Gorman * they are written so also forcibly stall. 1499b1a6f21eSMel Gorman */ 1500b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken || nr_immediate) 1501b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1502e2be15f6SMel Gorman } 1503d43006d5SMel Gorman 15048e950282SMel Gorman /* 15058e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 15068e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 15078e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 15088e950282SMel Gorman */ 15098e950282SMel Gorman if (!sc->hibernation_mode && !current_is_kswapd()) 15108e950282SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 15118e950282SMel Gorman 1512e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1513e11da5b4SMel Gorman zone_idx(zone), 1514e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 15159e3b2f8cSKonstantin Khlebnikov sc->priority, 151623b9da55SMel Gorman trace_shrink_flags(file)); 151705ff5137SAndrew Morton return nr_reclaimed; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15203bb1a852SMartin Bligh /* 15211cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 15221cfb419bSKAMEZAWA Hiroyuki * 15231cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 15241cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 15251cfb419bSKAMEZAWA Hiroyuki * 15261cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 15271cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 15281cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 15291cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 15301cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 15311cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 15321cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 15331cfb419bSKAMEZAWA Hiroyuki * 15341cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 15351cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 15361cfb419bSKAMEZAWA Hiroyuki */ 15371cfb419bSKAMEZAWA Hiroyuki 1538fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 15393eb4140fSWu Fengguang struct list_head *list, 15402bcf8879SHugh Dickins struct list_head *pages_to_free, 15413eb4140fSWu Fengguang enum lru_list lru) 15423eb4140fSWu Fengguang { 1543fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 15443eb4140fSWu Fengguang unsigned long pgmoved = 0; 15453eb4140fSWu Fengguang struct page *page; 1546fa9add64SHugh Dickins int nr_pages; 15473eb4140fSWu Fengguang 15483eb4140fSWu Fengguang while (!list_empty(list)) { 15493eb4140fSWu Fengguang page = lru_to_page(list); 1550fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 15513eb4140fSWu Fengguang 15523eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 15533eb4140fSWu Fengguang SetPageLRU(page); 15543eb4140fSWu Fengguang 1555fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1556fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1557925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1558fa9add64SHugh Dickins pgmoved += nr_pages; 15593eb4140fSWu Fengguang 15602bcf8879SHugh Dickins if (put_page_testzero(page)) { 15612bcf8879SHugh Dickins __ClearPageLRU(page); 15622bcf8879SHugh Dickins __ClearPageActive(page); 1563fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 15642bcf8879SHugh Dickins 15652bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 15663eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 15672bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 15683eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 15692bcf8879SHugh Dickins } else 15702bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 15713eb4140fSWu Fengguang } 15723eb4140fSWu Fengguang } 15733eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 15743eb4140fSWu Fengguang if (!is_active_lru(lru)) 15753eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 15763eb4140fSWu Fengguang } 15771cfb419bSKAMEZAWA Hiroyuki 1578f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 15791a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1580f16015fbSJohannes Weiner struct scan_control *sc, 15819e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 15821cfb419bSKAMEZAWA Hiroyuki { 158344c241f1SKOSAKI Motohiro unsigned long nr_taken; 1584f626012dSHugh Dickins unsigned long nr_scanned; 15856fe6b7e3SWu Fengguang unsigned long vm_flags; 15861cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 15878cab4754SWu Fengguang LIST_HEAD(l_active); 1588b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 15891cfb419bSKAMEZAWA Hiroyuki struct page *page; 15901a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 159144c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1592f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 15933cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 15941a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 15951cfb419bSKAMEZAWA Hiroyuki 15961da177e4SLinus Torvalds lru_add_drain(); 1597f80c0673SMinchan Kim 1598f80c0673SMinchan Kim if (!sc->may_unmap) 159961317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1600f80c0673SMinchan Kim if (!sc->may_writepage) 160161317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1602f80c0673SMinchan Kim 16031da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1604925b7673SJohannes Weiner 16055dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 16065dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 160789b5fae5SJohannes Weiner if (global_reclaim(sc)) 1608f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 160989b5fae5SJohannes Weiner 1610b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 16111cfb419bSKAMEZAWA Hiroyuki 1612f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 16133cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1614a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 16151da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 16181da177e4SLinus Torvalds cond_resched(); 16191da177e4SLinus Torvalds page = lru_to_page(&l_hold); 16201da177e4SLinus Torvalds list_del(&page->lru); 16217e9cd484SRik van Riel 162239b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1623894bc310SLee Schermerhorn putback_lru_page(page); 1624894bc310SLee Schermerhorn continue; 1625894bc310SLee Schermerhorn } 1626894bc310SLee Schermerhorn 1627cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1628cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1629cc715d99SMel Gorman if (page_has_private(page)) 1630cc715d99SMel Gorman try_to_release_page(page, 0); 1631cc715d99SMel Gorman unlock_page(page); 1632cc715d99SMel Gorman } 1633cc715d99SMel Gorman } 1634cc715d99SMel Gorman 1635c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1636c3ac9a8aSJohannes Weiner &vm_flags)) { 16379992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 16388cab4754SWu Fengguang /* 16398cab4754SWu Fengguang * Identify referenced, file-backed active pages and 16408cab4754SWu Fengguang * give them one more trip around the active list. So 16418cab4754SWu Fengguang * that executable code get better chances to stay in 16428cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 16438cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 16448cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 16458cab4754SWu Fengguang * so we ignore them here. 16468cab4754SWu Fengguang */ 164741e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 16488cab4754SWu Fengguang list_add(&page->lru, &l_active); 16498cab4754SWu Fengguang continue; 16508cab4754SWu Fengguang } 16518cab4754SWu Fengguang } 16527e9cd484SRik van Riel 16535205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 16541da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 1657b555749aSAndrew Morton /* 16588cab4754SWu Fengguang * Move pages back to the lru list. 1659b555749aSAndrew Morton */ 16602a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 16614f98a2feSRik van Riel /* 16628cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 16638cab4754SWu Fengguang * even though only some of them are actually re-activated. This 16648cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 16658cab4754SWu Fengguang * get_scan_ratio. 1666556adecbSRik van Riel */ 1667b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1668556adecbSRik van Riel 1669fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1670fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1671a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1672f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 16732bcf8879SHugh Dickins 16742bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 167774e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 167814797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1679f89eb90eSKOSAKI Motohiro { 1680f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1681f89eb90eSKOSAKI Motohiro 1682f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1683f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1684f89eb90eSKOSAKI Motohiro 1685f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1686f89eb90eSKOSAKI Motohiro return 1; 1687f89eb90eSKOSAKI Motohiro 1688f89eb90eSKOSAKI Motohiro return 0; 1689f89eb90eSKOSAKI Motohiro } 1690f89eb90eSKOSAKI Motohiro 169114797e23SKOSAKI Motohiro /** 169214797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1693c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 169414797e23SKOSAKI Motohiro * 169514797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 169614797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 169714797e23SKOSAKI Motohiro */ 1698c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 169914797e23SKOSAKI Motohiro { 170074e3f3c3SMinchan Kim /* 170174e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 170274e3f3c3SMinchan Kim * is pointless. 170374e3f3c3SMinchan Kim */ 170474e3f3c3SMinchan Kim if (!total_swap_pages) 170574e3f3c3SMinchan Kim return 0; 170674e3f3c3SMinchan Kim 1707c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1708c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1709f16015fbSJohannes Weiner 1710c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 171114797e23SKOSAKI Motohiro } 171274e3f3c3SMinchan Kim #else 1713c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 171474e3f3c3SMinchan Kim { 171574e3f3c3SMinchan Kim return 0; 171674e3f3c3SMinchan Kim } 171774e3f3c3SMinchan Kim #endif 171814797e23SKOSAKI Motohiro 171956e49d21SRik van Riel /** 172056e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1721c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 172256e49d21SRik van Riel * 172356e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 172456e49d21SRik van Riel * ensures that active file pages get deactivated, until more 172556e49d21SRik van Riel * than half of the file pages are on the inactive list. 172656e49d21SRik van Riel * 172756e49d21SRik van Riel * Once we get to that situation, protect the system's working 172856e49d21SRik van Riel * set from being evicted by disabling active file page aging. 172956e49d21SRik van Riel * 173056e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 173156e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 173256e49d21SRik van Riel */ 1733c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 173456e49d21SRik van Riel { 1735e3790144SJohannes Weiner unsigned long inactive; 1736e3790144SJohannes Weiner unsigned long active; 173756e49d21SRik van Riel 1738e3790144SJohannes Weiner inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE); 1739e3790144SJohannes Weiner active = get_lru_size(lruvec, LRU_ACTIVE_FILE); 1740e3790144SJohannes Weiner 1741e3790144SJohannes Weiner return active > inactive; 174256e49d21SRik van Riel } 174356e49d21SRik van Riel 174475b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1745b39415b2SRik van Riel { 174675b00af7SHugh Dickins if (is_file_lru(lru)) 1747c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1748b39415b2SRik van Riel else 1749c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1750b39415b2SRik van Riel } 1751b39415b2SRik van Riel 17524f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 17531a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1754b69408e8SChristoph Lameter { 1755b39415b2SRik van Riel if (is_active_lru(lru)) { 175675b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 17571a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1758556adecbSRik van Riel return 0; 1759556adecbSRik van Riel } 1760556adecbSRik van Riel 17611a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1762b69408e8SChristoph Lameter } 1763b69408e8SChristoph Lameter 17643d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 17651f4c025bSKAMEZAWA Hiroyuki { 176689b5fae5SJohannes Weiner if (global_reclaim(sc)) 17671f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 17683d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 17691f4c025bSKAMEZAWA Hiroyuki } 17701f4c025bSKAMEZAWA Hiroyuki 17719a265114SJohannes Weiner enum scan_balance { 17729a265114SJohannes Weiner SCAN_EQUAL, 17739a265114SJohannes Weiner SCAN_FRACT, 17749a265114SJohannes Weiner SCAN_ANON, 17759a265114SJohannes Weiner SCAN_FILE, 17769a265114SJohannes Weiner }; 17779a265114SJohannes Weiner 17781da177e4SLinus Torvalds /* 17794f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 17804f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 17814f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 17824f98a2feSRik van Riel * onto the active list instead of evict. 17834f98a2feSRik van Riel * 1784be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1785be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 17864f98a2feSRik van Riel */ 178790126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 17889e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 17894f98a2feSRik van Riel { 179090126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 17919a265114SJohannes Weiner u64 fraction[2]; 17929a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 179390126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 17949a265114SJohannes Weiner unsigned long anon_prio, file_prio; 17959a265114SJohannes Weiner enum scan_balance scan_balance; 17969a265114SJohannes Weiner unsigned long anon, file, free; 17979a265114SJohannes Weiner bool force_scan = false; 17989a265114SJohannes Weiner unsigned long ap, fp; 17999a265114SJohannes Weiner enum lru_list lru; 1800246e87a9SKAMEZAWA Hiroyuki 1801f11c0ca5SJohannes Weiner /* 1802f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1803f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1804f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1805f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1806f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1807f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1808f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1809f11c0ca5SJohannes Weiner * well. 1810f11c0ca5SJohannes Weiner */ 1811*6e543d57SLisa Du if (current_is_kswapd() && !zone_reclaimable(zone)) 1812a4d3e9e7SJohannes Weiner force_scan = true; 181389b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1814a4d3e9e7SJohannes Weiner force_scan = true; 181576a33fc3SShaohua Li 181676a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 1817ec8acf20SShaohua Li if (!sc->may_swap || (get_nr_swap_pages() <= 0)) { 18189a265114SJohannes Weiner scan_balance = SCAN_FILE; 181976a33fc3SShaohua Li goto out; 182076a33fc3SShaohua Li } 18214f98a2feSRik van Riel 182210316b31SJohannes Weiner /* 182310316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 182410316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 182510316b31SJohannes Weiner * disable swapping for individual groups completely when 182610316b31SJohannes Weiner * using the memory controller's swap limit feature would be 182710316b31SJohannes Weiner * too expensive. 182810316b31SJohannes Weiner */ 182910316b31SJohannes Weiner if (!global_reclaim(sc) && !vmscan_swappiness(sc)) { 18309a265114SJohannes Weiner scan_balance = SCAN_FILE; 183110316b31SJohannes Weiner goto out; 183210316b31SJohannes Weiner } 183310316b31SJohannes Weiner 183410316b31SJohannes Weiner /* 183510316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 183610316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 183710316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 183810316b31SJohannes Weiner */ 183910316b31SJohannes Weiner if (!sc->priority && vmscan_swappiness(sc)) { 18409a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 184110316b31SJohannes Weiner goto out; 184210316b31SJohannes Weiner } 184310316b31SJohannes Weiner 18444d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 18454d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 18464d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 18474d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1848a4d3e9e7SJohannes Weiner 184911d16c25SJohannes Weiner /* 185011d16c25SJohannes Weiner * If it's foreseeable that reclaiming the file cache won't be 185111d16c25SJohannes Weiner * enough to get the zone back into a desirable shape, we have 185211d16c25SJohannes Weiner * to swap. Better start now and leave the - probably heavily 185311d16c25SJohannes Weiner * thrashing - remaining file pages alone. 185411d16c25SJohannes Weiner */ 185589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 185690126375SKonstantin Khlebnikov free = zone_page_state(zone, NR_FREE_PAGES); 185790126375SKonstantin Khlebnikov if (unlikely(file + free <= high_wmark_pages(zone))) { 18589a265114SJohannes Weiner scan_balance = SCAN_ANON; 185976a33fc3SShaohua Li goto out; 18607c5bd705SJohannes Weiner } 18617c5bd705SJohannes Weiner } 18627c5bd705SJohannes Weiner 1863e9868505SRik van Riel /* 18647c5bd705SJohannes Weiner * There is enough inactive page cache, do not reclaim 18657c5bd705SJohannes Weiner * anything from the anonymous working set right now. 1866e9868505SRik van Riel */ 18677c5bd705SJohannes Weiner if (!inactive_file_is_low(lruvec)) { 18689a265114SJohannes Weiner scan_balance = SCAN_FILE; 1869e9868505SRik van Riel goto out; 18704f98a2feSRik van Riel } 18714f98a2feSRik van Riel 18729a265114SJohannes Weiner scan_balance = SCAN_FRACT; 18739a265114SJohannes Weiner 18744f98a2feSRik van Riel /* 187558c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 187658c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 187758c37f6eSKOSAKI Motohiro */ 18783d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 187975b00af7SHugh Dickins file_prio = 200 - anon_prio; 188058c37f6eSKOSAKI Motohiro 188158c37f6eSKOSAKI Motohiro /* 18824f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 18834f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 18844f98a2feSRik van Riel * ratios to determine how valuable each cache is. 18854f98a2feSRik van Riel * 18864f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 18874f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 18884f98a2feSRik van Riel * up weighing recent references more than old ones. 18894f98a2feSRik van Riel * 18904f98a2feSRik van Riel * anon in [0], file in [1] 18914f98a2feSRik van Riel */ 189290126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 189358c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 18946e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 18956e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 18964f98a2feSRik van Riel } 18974f98a2feSRik van Riel 18986e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 18996e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 19006e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 19014f98a2feSRik van Riel } 19024f98a2feSRik van Riel 19034f98a2feSRik van Riel /* 190400d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 190500d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 190600d8089cSRik van Riel * each list that were recently referenced and in active use. 19074f98a2feSRik van Riel */ 1908fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 19096e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 19104f98a2feSRik van Riel 1911fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 19126e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 191390126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 19144f98a2feSRik van Riel 191576a33fc3SShaohua Li fraction[0] = ap; 191676a33fc3SShaohua Li fraction[1] = fp; 191776a33fc3SShaohua Li denominator = ap + fp + 1; 191876a33fc3SShaohua Li out: 19194111304dSHugh Dickins for_each_evictable_lru(lru) { 19204111304dSHugh Dickins int file = is_file_lru(lru); 1921d778df51SJohannes Weiner unsigned long size; 192276a33fc3SShaohua Li unsigned long scan; 192376a33fc3SShaohua Li 1924d778df51SJohannes Weiner size = get_lru_size(lruvec, lru); 1925d778df51SJohannes Weiner scan = size >> sc->priority; 19269a265114SJohannes Weiner 1927f11c0ca5SJohannes Weiner if (!scan && force_scan) 1928d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 19299a265114SJohannes Weiner 19309a265114SJohannes Weiner switch (scan_balance) { 19319a265114SJohannes Weiner case SCAN_EQUAL: 19329a265114SJohannes Weiner /* Scan lists relative to size */ 19339a265114SJohannes Weiner break; 19349a265114SJohannes Weiner case SCAN_FRACT: 19359a265114SJohannes Weiner /* 19369a265114SJohannes Weiner * Scan types proportional to swappiness and 19379a265114SJohannes Weiner * their relative recent reclaim efficiency. 19389a265114SJohannes Weiner */ 193976a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 19409a265114SJohannes Weiner break; 19419a265114SJohannes Weiner case SCAN_FILE: 19429a265114SJohannes Weiner case SCAN_ANON: 19439a265114SJohannes Weiner /* Scan one type exclusively */ 19449a265114SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) 19459a265114SJohannes Weiner scan = 0; 19469a265114SJohannes Weiner break; 19479a265114SJohannes Weiner default: 19489a265114SJohannes Weiner /* Look ma, no brain */ 19499a265114SJohannes Weiner BUG(); 19509a265114SJohannes Weiner } 19514111304dSHugh Dickins nr[lru] = scan; 195276a33fc3SShaohua Li } 19536e08a369SWu Fengguang } 19544f98a2feSRik van Riel 19559b4f98cdSJohannes Weiner /* 19569b4f98cdSJohannes Weiner * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 19579b4f98cdSJohannes Weiner */ 19589b4f98cdSJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 19599b4f98cdSJohannes Weiner { 19609b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 1961e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 19629b4f98cdSJohannes Weiner unsigned long nr_to_scan; 19639b4f98cdSJohannes Weiner enum lru_list lru; 19649b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 19659b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 19669b4f98cdSJohannes Weiner struct blk_plug plug; 1967e82e0561SMel Gorman bool scan_adjusted = false; 19689b4f98cdSJohannes Weiner 19699b4f98cdSJohannes Weiner get_scan_count(lruvec, sc, nr); 19709b4f98cdSJohannes Weiner 1971e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 1972e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 1973e82e0561SMel Gorman 19749b4f98cdSJohannes Weiner blk_start_plug(&plug); 19759b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 19769b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 1977e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 1978e82e0561SMel Gorman unsigned long nr_scanned; 1979e82e0561SMel Gorman 19809b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 19819b4f98cdSJohannes Weiner if (nr[lru]) { 19829b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 19839b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 19849b4f98cdSJohannes Weiner 19859b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 19869b4f98cdSJohannes Weiner lruvec, sc); 19879b4f98cdSJohannes Weiner } 19889b4f98cdSJohannes Weiner } 1989e82e0561SMel Gorman 1990e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 1991e82e0561SMel Gorman continue; 1992e82e0561SMel Gorman 19939b4f98cdSJohannes Weiner /* 1994e82e0561SMel Gorman * For global direct reclaim, reclaim only the number of pages 1995e82e0561SMel Gorman * requested. Less care is taken to scan proportionally as it 1996e82e0561SMel Gorman * is more important to minimise direct reclaim stall latency 1997e82e0561SMel Gorman * than it is to properly age the LRU lists. 19989b4f98cdSJohannes Weiner */ 1999e82e0561SMel Gorman if (global_reclaim(sc) && !current_is_kswapd()) 20009b4f98cdSJohannes Weiner break; 2001e82e0561SMel Gorman 2002e82e0561SMel Gorman /* 2003e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 2004e82e0561SMel Gorman * requested. Ensure that the anon and file LRUs shrink 2005e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 2006e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 2007e82e0561SMel Gorman * proportional to the original scan target. 2008e82e0561SMel Gorman */ 2009e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 2010e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 2011e82e0561SMel Gorman 2012e82e0561SMel Gorman if (nr_file > nr_anon) { 2013e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 2014e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 2015e82e0561SMel Gorman lru = LRU_BASE; 2016e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 2017e82e0561SMel Gorman } else { 2018e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2019e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2020e82e0561SMel Gorman lru = LRU_FILE; 2021e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2022e82e0561SMel Gorman } 2023e82e0561SMel Gorman 2024e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2025e82e0561SMel Gorman nr[lru] = 0; 2026e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2027e82e0561SMel Gorman 2028e82e0561SMel Gorman /* 2029e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2030e82e0561SMel Gorman * scan target and the percentage scanning already complete 2031e82e0561SMel Gorman */ 2032e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2033e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2034e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2035e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2036e82e0561SMel Gorman 2037e82e0561SMel Gorman lru += LRU_ACTIVE; 2038e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2039e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2040e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2041e82e0561SMel Gorman 2042e82e0561SMel Gorman scan_adjusted = true; 20439b4f98cdSJohannes Weiner } 20449b4f98cdSJohannes Weiner blk_finish_plug(&plug); 20459b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 20469b4f98cdSJohannes Weiner 20479b4f98cdSJohannes Weiner /* 20489b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 20499b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 20509b4f98cdSJohannes Weiner */ 20519b4f98cdSJohannes Weiner if (inactive_anon_is_low(lruvec)) 20529b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 20539b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 20549b4f98cdSJohannes Weiner 20559b4f98cdSJohannes Weiner throttle_vm_writeout(sc->gfp_mask); 20569b4f98cdSJohannes Weiner } 20579b4f98cdSJohannes Weiner 205823b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 20599e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 206023b9da55SMel Gorman { 2061d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 206223b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 20639e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 206423b9da55SMel Gorman return true; 206523b9da55SMel Gorman 206623b9da55SMel Gorman return false; 206723b9da55SMel Gorman } 206823b9da55SMel Gorman 20694f98a2feSRik van Riel /* 207023b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 207123b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 207223b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 207323b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 207423b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 20753e7d3449SMel Gorman */ 20769b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone, 20773e7d3449SMel Gorman unsigned long nr_reclaimed, 20783e7d3449SMel Gorman unsigned long nr_scanned, 20793e7d3449SMel Gorman struct scan_control *sc) 20803e7d3449SMel Gorman { 20813e7d3449SMel Gorman unsigned long pages_for_compaction; 20823e7d3449SMel Gorman unsigned long inactive_lru_pages; 20833e7d3449SMel Gorman 20843e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 20859e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 20863e7d3449SMel Gorman return false; 20873e7d3449SMel Gorman 20882876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 20892876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 20903e7d3449SMel Gorman /* 20912876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 20922876592fSMel Gorman * full LRU list has been scanned and we are still failing 20932876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 20942876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 20953e7d3449SMel Gorman */ 20963e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 20973e7d3449SMel Gorman return false; 20982876592fSMel Gorman } else { 20992876592fSMel Gorman /* 21002876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 21012876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 21022876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 21032876592fSMel Gorman * pages that were scanned. This will return to the 21042876592fSMel Gorman * caller faster at the risk reclaim/compaction and 21052876592fSMel Gorman * the resulting allocation attempt fails 21062876592fSMel Gorman */ 21072876592fSMel Gorman if (!nr_reclaimed) 21082876592fSMel Gorman return false; 21092876592fSMel Gorman } 21103e7d3449SMel Gorman 21113e7d3449SMel Gorman /* 21123e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 21133e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 21143e7d3449SMel Gorman */ 21153e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 21169b4f98cdSJohannes Weiner inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE); 2117ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 21189b4f98cdSJohannes Weiner inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON); 21193e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 21203e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 21213e7d3449SMel Gorman return true; 21223e7d3449SMel Gorman 21233e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 21249b4f98cdSJohannes Weiner switch (compaction_suitable(zone, sc->order)) { 21253e7d3449SMel Gorman case COMPACT_PARTIAL: 21263e7d3449SMel Gorman case COMPACT_CONTINUE: 21273e7d3449SMel Gorman return false; 21283e7d3449SMel Gorman default: 21293e7d3449SMel Gorman return true; 21303e7d3449SMel Gorman } 21313e7d3449SMel Gorman } 21323e7d3449SMel Gorman 21339e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc) 2134f16015fbSJohannes Weiner { 21359b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 21369b4f98cdSJohannes Weiner 21379b4f98cdSJohannes Weiner do { 21385660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 21395660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 21405660048cSJohannes Weiner .zone = zone, 21419e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 21425660048cSJohannes Weiner }; 21435660048cSJohannes Weiner struct mem_cgroup *memcg; 21445660048cSJohannes Weiner 21459b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 21469b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 21479b4f98cdSJohannes Weiner 21485660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 21495660048cSJohannes Weiner do { 21509b4f98cdSJohannes Weiner struct lruvec *lruvec; 21519b4f98cdSJohannes Weiner 21529b4f98cdSJohannes Weiner lruvec = mem_cgroup_zone_lruvec(zone, memcg); 21535660048cSJohannes Weiner 2154f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, sc); 2155f9be23d6SKonstantin Khlebnikov 21565660048cSJohannes Weiner /* 2157a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2158a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 21599b4f98cdSJohannes Weiner * zone. 2160a394cb8eSMichal Hocko * 2161a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2162a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2163a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2164a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 21655660048cSJohannes Weiner */ 2166a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2167a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 21685660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 21695660048cSJohannes Weiner break; 21705660048cSJohannes Weiner } 21715660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 21725660048cSJohannes Weiner } while (memcg); 217370ddf637SAnton Vorontsov 217470ddf637SAnton Vorontsov vmpressure(sc->gfp_mask, sc->target_mem_cgroup, 217570ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 217670ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 217770ddf637SAnton Vorontsov 21789b4f98cdSJohannes Weiner } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed, 21799b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 2180f16015fbSJohannes Weiner } 2181f16015fbSJohannes Weiner 2182fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 2183fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2184fe4b1b24SMel Gorman { 2185fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 2186fe4b1b24SMel Gorman bool watermark_ok; 2187fe4b1b24SMel Gorman 2188fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 2189fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 2190fe4b1b24SMel Gorman return false; 2191fe4b1b24SMel Gorman 2192fe4b1b24SMel Gorman /* 2193fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 2194fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 2195fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 2196fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 2197fe4b1b24SMel Gorman */ 2198fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 2199b40da049SJiang Liu (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 2200fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2201fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 2202fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 2203fe4b1b24SMel Gorman 2204fe4b1b24SMel Gorman /* 2205fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 2206fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 2207fe4b1b24SMel Gorman */ 2208aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 2209fe4b1b24SMel Gorman return watermark_ok; 2210fe4b1b24SMel Gorman 2211fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 2212fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 2213fe4b1b24SMel Gorman return false; 2214fe4b1b24SMel Gorman 2215fe4b1b24SMel Gorman return watermark_ok; 2216fe4b1b24SMel Gorman } 2217fe4b1b24SMel Gorman 22181da177e4SLinus Torvalds /* 22191da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 22201da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 22211da177e4SLinus Torvalds * request. 22221da177e4SLinus Torvalds * 222341858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 222441858966SMel Gorman * Because: 22251da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 22261da177e4SLinus Torvalds * allocation or 222741858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 222841858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 222941858966SMel Gorman * zone defense algorithm. 22301da177e4SLinus Torvalds * 22311da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 22321da177e4SLinus Torvalds * scan then give up on it. 2233e0c23279SMel Gorman * 2234e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 2235fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 22360cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 22370cee34fdSMel Gorman * further reclaim. 22381da177e4SLinus Torvalds */ 22399e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 22401da177e4SLinus Torvalds { 2241dd1a239fSMel Gorman struct zoneref *z; 224254a6eb5cSMel Gorman struct zone *zone; 2243d149e3b2SYing Han unsigned long nr_soft_reclaimed; 2244d149e3b2SYing Han unsigned long nr_soft_scanned; 22450cee34fdSMel Gorman bool aborted_reclaim = false; 22461cfb419bSKAMEZAWA Hiroyuki 2247cc715d99SMel Gorman /* 2248cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2249cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2250cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2251cc715d99SMel Gorman */ 2252cc715d99SMel Gorman if (buffer_heads_over_limit) 2253cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2254cc715d99SMel Gorman 2255d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2256d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 2257f3fe6512SCon Kolivas if (!populated_zone(zone)) 22581da177e4SLinus Torvalds continue; 22591cfb419bSKAMEZAWA Hiroyuki /* 22601cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 22611cfb419bSKAMEZAWA Hiroyuki * to global LRU. 22621cfb419bSKAMEZAWA Hiroyuki */ 226389b5fae5SJohannes Weiner if (global_reclaim(sc)) { 226402a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 22651da177e4SLinus Torvalds continue; 2266*6e543d57SLisa Du if (sc->priority != DEF_PRIORITY && 2267*6e543d57SLisa Du !zone_reclaimable(zone)) 22681da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 2269d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION)) { 2270e0887c19SRik van Riel /* 2271e0c23279SMel Gorman * If we already have plenty of memory free for 2272e0c23279SMel Gorman * compaction in this zone, don't free any more. 2273e0c23279SMel Gorman * Even though compaction is invoked for any 2274e0c23279SMel Gorman * non-zero order, only frequent costly order 2275e0c23279SMel Gorman * reclamation is disruptive enough to become a 2276c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2277c7cfa37bSCopot Alexandru * page allocations. 2278e0887c19SRik van Riel */ 2279fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 22800cee34fdSMel Gorman aborted_reclaim = true; 2281e0887c19SRik van Riel continue; 2282e0887c19SRik van Riel } 2283e0c23279SMel Gorman } 2284ac34a1a3SKAMEZAWA Hiroyuki /* 2285ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 2286ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 2287ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 2288ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 2289ac34a1a3SKAMEZAWA Hiroyuki */ 2290d149e3b2SYing Han nr_soft_scanned = 0; 2291d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 2292d149e3b2SYing Han sc->order, sc->gfp_mask, 2293d149e3b2SYing Han &nr_soft_scanned); 2294d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 2295ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 2296ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2297ac34a1a3SKAMEZAWA Hiroyuki } 2298d149e3b2SYing Han 22999e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 23001da177e4SLinus Torvalds } 2301e0c23279SMel Gorman 23020cee34fdSMel Gorman return aborted_reclaim; 2303d1908362SMinchan Kim } 2304d1908362SMinchan Kim 2305929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2306d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2307d1908362SMinchan Kim struct scan_control *sc) 2308d1908362SMinchan Kim { 2309d1908362SMinchan Kim struct zoneref *z; 2310d1908362SMinchan Kim struct zone *zone; 2311d1908362SMinchan Kim 2312d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2313d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2314d1908362SMinchan Kim if (!populated_zone(zone)) 2315d1908362SMinchan Kim continue; 2316d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2317d1908362SMinchan Kim continue; 2318*6e543d57SLisa Du if (zone_reclaimable(zone)) 2319929bea7cSKOSAKI Motohiro return false; 2320d1908362SMinchan Kim } 2321d1908362SMinchan Kim 2322929bea7cSKOSAKI Motohiro return true; 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds 23251da177e4SLinus Torvalds /* 23261da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 23271da177e4SLinus Torvalds * 23281da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 23291da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 23301da177e4SLinus Torvalds * 23311da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 23321da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 23335b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 23345b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 23355b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 23365b0830cbSJens Axboe * work, and the allocation attempt will fail. 2337a41f24eaSNishanth Aravamudan * 2338a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2339a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 23401da177e4SLinus Torvalds */ 2341dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2342a09ed5e0SYing Han struct scan_control *sc, 2343a09ed5e0SYing Han struct shrink_control *shrink) 23441da177e4SLinus Torvalds { 234569e05944SAndrew Morton unsigned long total_scanned = 0; 23461da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2347dd1a239fSMel Gorman struct zoneref *z; 234854a6eb5cSMel Gorman struct zone *zone; 234922fba335SKOSAKI Motohiro unsigned long writeback_threshold; 23500cee34fdSMel Gorman bool aborted_reclaim; 23511da177e4SLinus Torvalds 2352873b4771SKeika Kobayashi delayacct_freepages_start(); 2353873b4771SKeika Kobayashi 235489b5fae5SJohannes Weiner if (global_reclaim(sc)) 2355f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 23561da177e4SLinus Torvalds 23579e3b2f8cSKonstantin Khlebnikov do { 235870ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 235970ddf637SAnton Vorontsov sc->priority); 236066e1707bSBalbir Singh sc->nr_scanned = 0; 23619e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2362e0c23279SMel Gorman 236366e1707bSBalbir Singh /* 23645a1c9cbcSMel Gorman * Don't shrink slabs when reclaiming memory from over limit 23655a1c9cbcSMel Gorman * cgroups but do shrink slab at least once when aborting 23665a1c9cbcSMel Gorman * reclaim for compaction to avoid unevenly scanning file/anon 23675a1c9cbcSMel Gorman * LRU pages over slab pages. 236866e1707bSBalbir Singh */ 236989b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2370c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2371d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2372d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2373c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2374c6a8a8c5SKOSAKI Motohiro continue; 2375c6a8a8c5SKOSAKI Motohiro 2376c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2377c6a8a8c5SKOSAKI Motohiro } 2378c6a8a8c5SKOSAKI Motohiro 23791495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 23801da177e4SLinus Torvalds if (reclaim_state) { 2381a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 23821da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 23831da177e4SLinus Torvalds } 238491a45470SKAMEZAWA Hiroyuki } 238566e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2386bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 23871da177e4SLinus Torvalds goto out; 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds /* 23900e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 23910e50ce3bSMinchan Kim * writepage even in laptop mode. 23920e50ce3bSMinchan Kim */ 23930e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 23940e50ce3bSMinchan Kim sc->may_writepage = 1; 23950e50ce3bSMinchan Kim 23960e50ce3bSMinchan Kim /* 23971da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 23981da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 23991da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 24001da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 24011da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 24021da177e4SLinus Torvalds */ 240322fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 240422fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 24050e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 24060e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 240766e1707bSBalbir Singh sc->may_writepage = 1; 24081da177e4SLinus Torvalds } 24095a1c9cbcSMel Gorman } while (--sc->priority >= 0 && !aborted_reclaim); 2410bb21c7ceSKOSAKI Motohiro 24111da177e4SLinus Torvalds out: 2412873b4771SKeika Kobayashi delayacct_freepages_end(); 2413873b4771SKeika Kobayashi 2414bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2415bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2416bb21c7ceSKOSAKI Motohiro 2417929bea7cSKOSAKI Motohiro /* 2418929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2419929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2420929bea7cSKOSAKI Motohiro * check. 2421929bea7cSKOSAKI Motohiro */ 2422929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2423929bea7cSKOSAKI Motohiro return 0; 2424929bea7cSKOSAKI Motohiro 24250cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 24260cee34fdSMel Gorman if (aborted_reclaim) 24277335084dSMel Gorman return 1; 24287335084dSMel Gorman 2429bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 243089b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2431bb21c7ceSKOSAKI Motohiro return 1; 2432bb21c7ceSKOSAKI Motohiro 2433bb21c7ceSKOSAKI Motohiro return 0; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds 24365515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 24375515061dSMel Gorman { 24385515061dSMel Gorman struct zone *zone; 24395515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 24405515061dSMel Gorman unsigned long free_pages = 0; 24415515061dSMel Gorman int i; 24425515061dSMel Gorman bool wmark_ok; 24435515061dSMel Gorman 24445515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 24455515061dSMel Gorman zone = &pgdat->node_zones[i]; 24465515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 24475515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 24485515061dSMel Gorman } 24495515061dSMel Gorman 24505515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 24515515061dSMel Gorman 24525515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 24535515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 24545515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 24555515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 24565515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 24575515061dSMel Gorman } 24585515061dSMel Gorman 24595515061dSMel Gorman return wmark_ok; 24605515061dSMel Gorman } 24615515061dSMel Gorman 24625515061dSMel Gorman /* 24635515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 24645515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 24655515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 246650694c28SMel Gorman * when the low watermark is reached. 246750694c28SMel Gorman * 246850694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 246950694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 24705515061dSMel Gorman */ 247150694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 24725515061dSMel Gorman nodemask_t *nodemask) 24735515061dSMel Gorman { 24745515061dSMel Gorman struct zone *zone; 24755515061dSMel Gorman int high_zoneidx = gfp_zone(gfp_mask); 24765515061dSMel Gorman pg_data_t *pgdat; 24775515061dSMel Gorman 24785515061dSMel Gorman /* 24795515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 24805515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 24815515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 24825515061dSMel Gorman * committing a transaction where throttling it could forcing other 24835515061dSMel Gorman * processes to block on log_wait_commit(). 24845515061dSMel Gorman */ 24855515061dSMel Gorman if (current->flags & PF_KTHREAD) 248650694c28SMel Gorman goto out; 248750694c28SMel Gorman 248850694c28SMel Gorman /* 248950694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 249050694c28SMel Gorman * It should return quickly so it can exit and free its memory 249150694c28SMel Gorman */ 249250694c28SMel Gorman if (fatal_signal_pending(current)) 249350694c28SMel Gorman goto out; 24945515061dSMel Gorman 24955515061dSMel Gorman /* Check if the pfmemalloc reserves are ok */ 24965515061dSMel Gorman first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone); 24975515061dSMel Gorman pgdat = zone->zone_pgdat; 24985515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 249950694c28SMel Gorman goto out; 25005515061dSMel Gorman 250168243e76SMel Gorman /* Account for the throttling */ 250268243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 250368243e76SMel Gorman 25045515061dSMel Gorman /* 25055515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 25065515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 25075515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 25085515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 25095515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 25105515061dSMel Gorman * second before continuing. 25115515061dSMel Gorman */ 25125515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 25135515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 25145515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 251550694c28SMel Gorman 251650694c28SMel Gorman goto check_pending; 25175515061dSMel Gorman } 25185515061dSMel Gorman 25195515061dSMel Gorman /* Throttle until kswapd wakes the process */ 25205515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 25215515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 252250694c28SMel Gorman 252350694c28SMel Gorman check_pending: 252450694c28SMel Gorman if (fatal_signal_pending(current)) 252550694c28SMel Gorman return true; 252650694c28SMel Gorman 252750694c28SMel Gorman out: 252850694c28SMel Gorman return false; 25295515061dSMel Gorman } 25305515061dSMel Gorman 2531dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2532327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 253366e1707bSBalbir Singh { 253433906bc5SMel Gorman unsigned long nr_reclaimed; 253566e1707bSBalbir Singh struct scan_control sc = { 253621caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 253766e1707bSBalbir Singh .may_writepage = !laptop_mode, 253822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2539a6dc60f8SJohannes Weiner .may_unmap = 1, 25402e2e4259SKOSAKI Motohiro .may_swap = 1, 254166e1707bSBalbir Singh .order = order, 25429e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2543f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2544327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 254566e1707bSBalbir Singh }; 2546a09ed5e0SYing Han struct shrink_control shrink = { 2547a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2548a09ed5e0SYing Han }; 254966e1707bSBalbir Singh 25505515061dSMel Gorman /* 255150694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 255250694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 255350694c28SMel Gorman * point. 25545515061dSMel Gorman */ 255550694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 25565515061dSMel Gorman return 1; 25575515061dSMel Gorman 255833906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 255933906bc5SMel Gorman sc.may_writepage, 256033906bc5SMel Gorman gfp_mask); 256133906bc5SMel Gorman 2562a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 256333906bc5SMel Gorman 256433906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 256533906bc5SMel Gorman 256633906bc5SMel Gorman return nr_reclaimed; 256766e1707bSBalbir Singh } 256866e1707bSBalbir Singh 2569c255a458SAndrew Morton #ifdef CONFIG_MEMCG 257066e1707bSBalbir Singh 257172835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 25724e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 25730ae5e89cSYing Han struct zone *zone, 25740ae5e89cSYing Han unsigned long *nr_scanned) 25754e416953SBalbir Singh { 25764e416953SBalbir Singh struct scan_control sc = { 25770ae5e89cSYing Han .nr_scanned = 0, 2578b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 25794e416953SBalbir Singh .may_writepage = !laptop_mode, 25804e416953SBalbir Singh .may_unmap = 1, 25814e416953SBalbir Singh .may_swap = !noswap, 25824e416953SBalbir Singh .order = 0, 25839e3b2f8cSKonstantin Khlebnikov .priority = 0, 258472835c86SJohannes Weiner .target_mem_cgroup = memcg, 25854e416953SBalbir Singh }; 2586f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 25870ae5e89cSYing Han 25884e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 25894e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2590bdce6d9eSKOSAKI Motohiro 25919e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2592bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2593bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2594bdce6d9eSKOSAKI Motohiro 25954e416953SBalbir Singh /* 25964e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 25974e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 25984e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 25994e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 26004e416953SBalbir Singh * the priority and make it zero. 26014e416953SBalbir Singh */ 2602f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, &sc); 2603bdce6d9eSKOSAKI Motohiro 2604bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2605bdce6d9eSKOSAKI Motohiro 26060ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 26074e416953SBalbir Singh return sc.nr_reclaimed; 26084e416953SBalbir Singh } 26094e416953SBalbir Singh 261072835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 26118c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2612185efc0fSJohannes Weiner bool noswap) 261366e1707bSBalbir Singh { 26144e416953SBalbir Singh struct zonelist *zonelist; 2615bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2616889976dbSYing Han int nid; 261766e1707bSBalbir Singh struct scan_control sc = { 261866e1707bSBalbir Singh .may_writepage = !laptop_mode, 2619a6dc60f8SJohannes Weiner .may_unmap = 1, 26202e2e4259SKOSAKI Motohiro .may_swap = !noswap, 262122fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 262266e1707bSBalbir Singh .order = 0, 26239e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 262472835c86SJohannes Weiner .target_mem_cgroup = memcg, 2625327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2626a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2627a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2628a09ed5e0SYing Han }; 2629a09ed5e0SYing Han struct shrink_control shrink = { 2630a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 263166e1707bSBalbir Singh }; 263266e1707bSBalbir Singh 2633889976dbSYing Han /* 2634889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2635889976dbSYing Han * take care of from where we get pages. So the node where we start the 2636889976dbSYing Han * scan does not need to be the current node. 2637889976dbSYing Han */ 263872835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2639889976dbSYing Han 2640889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2641bdce6d9eSKOSAKI Motohiro 2642bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2643bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2644bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2645bdce6d9eSKOSAKI Motohiro 2646a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2647bdce6d9eSKOSAKI Motohiro 2648bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2649bdce6d9eSKOSAKI Motohiro 2650bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 265166e1707bSBalbir Singh } 265266e1707bSBalbir Singh #endif 265366e1707bSBalbir Singh 26549e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2655f16015fbSJohannes Weiner { 2656b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2657b95a2f2dSJohannes Weiner 2658b95a2f2dSJohannes Weiner if (!total_swap_pages) 2659b95a2f2dSJohannes Weiner return; 2660b95a2f2dSJohannes Weiner 2661b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2662b95a2f2dSJohannes Weiner do { 2663c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2664f16015fbSJohannes Weiner 2665c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 26661a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 26679e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2668b95a2f2dSJohannes Weiner 2669b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2670b95a2f2dSJohannes Weiner } while (memcg); 2671f16015fbSJohannes Weiner } 2672f16015fbSJohannes Weiner 267360cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order, 267460cefed4SJohannes Weiner unsigned long balance_gap, int classzone_idx) 267560cefed4SJohannes Weiner { 267660cefed4SJohannes Weiner if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) + 267760cefed4SJohannes Weiner balance_gap, classzone_idx, 0)) 267860cefed4SJohannes Weiner return false; 267960cefed4SJohannes Weiner 2680d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && order && 2681d84da3f9SKirill A. Shutemov !compaction_suitable(zone, order)) 268260cefed4SJohannes Weiner return false; 268360cefed4SJohannes Weiner 268460cefed4SJohannes Weiner return true; 268560cefed4SJohannes Weiner } 268660cefed4SJohannes Weiner 26871741c877SMel Gorman /* 26884ae0a48bSZlatko Calusic * pgdat_balanced() is used when checking if a node is balanced. 26894ae0a48bSZlatko Calusic * 26904ae0a48bSZlatko Calusic * For order-0, all zones must be balanced! 26914ae0a48bSZlatko Calusic * 26924ae0a48bSZlatko Calusic * For high-order allocations only zones that meet watermarks and are in a 26934ae0a48bSZlatko Calusic * zone allowed by the callers classzone_idx are added to balanced_pages. The 26944ae0a48bSZlatko Calusic * total of balanced pages must be at least 25% of the zones allowed by 26954ae0a48bSZlatko Calusic * classzone_idx for the node to be considered balanced. Forcing all zones to 26964ae0a48bSZlatko Calusic * be balanced for high orders can cause excessive reclaim when there are 26974ae0a48bSZlatko Calusic * imbalanced zones. 26981741c877SMel Gorman * The choice of 25% is due to 26991741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 27001741c877SMel Gorman * reasonable sized machine 27011741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 270225985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 27031741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 27041741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 27051741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 27061741c877SMel Gorman */ 27074ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 27081741c877SMel Gorman { 2709b40da049SJiang Liu unsigned long managed_pages = 0; 27104ae0a48bSZlatko Calusic unsigned long balanced_pages = 0; 27111741c877SMel Gorman int i; 27121741c877SMel Gorman 27134ae0a48bSZlatko Calusic /* Check the watermark levels */ 27144ae0a48bSZlatko Calusic for (i = 0; i <= classzone_idx; i++) { 27154ae0a48bSZlatko Calusic struct zone *zone = pgdat->node_zones + i; 27161741c877SMel Gorman 27174ae0a48bSZlatko Calusic if (!populated_zone(zone)) 27184ae0a48bSZlatko Calusic continue; 27194ae0a48bSZlatko Calusic 2720b40da049SJiang Liu managed_pages += zone->managed_pages; 27214ae0a48bSZlatko Calusic 27224ae0a48bSZlatko Calusic /* 27234ae0a48bSZlatko Calusic * A special case here: 27244ae0a48bSZlatko Calusic * 27254ae0a48bSZlatko Calusic * balance_pgdat() skips over all_unreclaimable after 27264ae0a48bSZlatko Calusic * DEF_PRIORITY. Effectively, it considers them balanced so 27274ae0a48bSZlatko Calusic * they must be considered balanced here as well! 27284ae0a48bSZlatko Calusic */ 2729*6e543d57SLisa Du if (!zone_reclaimable(zone)) { 2730b40da049SJiang Liu balanced_pages += zone->managed_pages; 27314ae0a48bSZlatko Calusic continue; 27324ae0a48bSZlatko Calusic } 27334ae0a48bSZlatko Calusic 27344ae0a48bSZlatko Calusic if (zone_balanced(zone, order, 0, i)) 2735b40da049SJiang Liu balanced_pages += zone->managed_pages; 27364ae0a48bSZlatko Calusic else if (!order) 27374ae0a48bSZlatko Calusic return false; 27384ae0a48bSZlatko Calusic } 27394ae0a48bSZlatko Calusic 27404ae0a48bSZlatko Calusic if (order) 2741b40da049SJiang Liu return balanced_pages >= (managed_pages >> 2); 27424ae0a48bSZlatko Calusic else 27434ae0a48bSZlatko Calusic return true; 27441741c877SMel Gorman } 27451741c877SMel Gorman 27465515061dSMel Gorman /* 27475515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 27485515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 27495515061dSMel Gorman * 27505515061dSMel Gorman * Returns true if kswapd is ready to sleep 27515515061dSMel Gorman */ 27525515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2753dc83edd9SMel Gorman int classzone_idx) 2754f50de2d3SMel Gorman { 2755f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2756f50de2d3SMel Gorman if (remaining) 27575515061dSMel Gorman return false; 27585515061dSMel Gorman 27595515061dSMel Gorman /* 27605515061dSMel Gorman * There is a potential race between when kswapd checks its watermarks 27615515061dSMel Gorman * and a process gets throttled. There is also a potential race if 27625515061dSMel Gorman * processes get throttled, kswapd wakes, a large process exits therby 27635515061dSMel Gorman * balancing the zones that causes kswapd to miss a wakeup. If kswapd 27645515061dSMel Gorman * is going to sleep, no process should be sleeping on pfmemalloc_wait 27655515061dSMel Gorman * so wake them now if necessary. If necessary, processes will wake 27665515061dSMel Gorman * kswapd and get throttled again 27675515061dSMel Gorman */ 27685515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait)) { 27695515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 27705515061dSMel Gorman return false; 27715515061dSMel Gorman } 2772f50de2d3SMel Gorman 27734ae0a48bSZlatko Calusic return pgdat_balanced(pgdat, order, classzone_idx); 2774f50de2d3SMel Gorman } 2775f50de2d3SMel Gorman 27761da177e4SLinus Torvalds /* 277775485363SMel Gorman * kswapd shrinks the zone by the number of pages required to reach 277875485363SMel Gorman * the high watermark. 2779b8e83b94SMel Gorman * 2780b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 2781283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 2782283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 278375485363SMel Gorman */ 2784b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone, 27857c954f6dSMel Gorman int classzone_idx, 278675485363SMel Gorman struct scan_control *sc, 27872ab44f43SMel Gorman unsigned long lru_pages, 27882ab44f43SMel Gorman unsigned long *nr_attempted) 278975485363SMel Gorman { 27907c954f6dSMel Gorman int testorder = sc->order; 27917c954f6dSMel Gorman unsigned long balance_gap; 279275485363SMel Gorman struct reclaim_state *reclaim_state = current->reclaim_state; 279375485363SMel Gorman struct shrink_control shrink = { 279475485363SMel Gorman .gfp_mask = sc->gfp_mask, 279575485363SMel Gorman }; 27967c954f6dSMel Gorman bool lowmem_pressure; 279775485363SMel Gorman 279875485363SMel Gorman /* Reclaim above the high watermark. */ 279975485363SMel Gorman sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone)); 28007c954f6dSMel Gorman 28017c954f6dSMel Gorman /* 28027c954f6dSMel Gorman * Kswapd reclaims only single pages with compaction enabled. Trying 28037c954f6dSMel Gorman * too hard to reclaim until contiguous free pages have become 28047c954f6dSMel Gorman * available can hurt performance by evicting too much useful data 28057c954f6dSMel Gorman * from memory. Do not reclaim more than needed for compaction. 28067c954f6dSMel Gorman */ 28077c954f6dSMel Gorman if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 28087c954f6dSMel Gorman compaction_suitable(zone, sc->order) != 28097c954f6dSMel Gorman COMPACT_SKIPPED) 28107c954f6dSMel Gorman testorder = 0; 28117c954f6dSMel Gorman 28127c954f6dSMel Gorman /* 28137c954f6dSMel Gorman * We put equal pressure on every zone, unless one zone has way too 28147c954f6dSMel Gorman * many pages free already. The "too many pages" is defined as the 28157c954f6dSMel Gorman * high wmark plus a "gap" where the gap is either the low 28167c954f6dSMel Gorman * watermark or 1% of the zone, whichever is smaller. 28177c954f6dSMel Gorman */ 28187c954f6dSMel Gorman balance_gap = min(low_wmark_pages(zone), 28197c954f6dSMel Gorman (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 28207c954f6dSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 28217c954f6dSMel Gorman 28227c954f6dSMel Gorman /* 28237c954f6dSMel Gorman * If there is no low memory pressure or the zone is balanced then no 28247c954f6dSMel Gorman * reclaim is necessary 28257c954f6dSMel Gorman */ 28267c954f6dSMel Gorman lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone)); 28277c954f6dSMel Gorman if (!lowmem_pressure && zone_balanced(zone, testorder, 28287c954f6dSMel Gorman balance_gap, classzone_idx)) 28297c954f6dSMel Gorman return true; 28307c954f6dSMel Gorman 283175485363SMel Gorman shrink_zone(zone, sc); 283275485363SMel Gorman 283375485363SMel Gorman reclaim_state->reclaimed_slab = 0; 2834*6e543d57SLisa Du shrink_slab(&shrink, sc->nr_scanned, lru_pages); 283575485363SMel Gorman sc->nr_reclaimed += reclaim_state->reclaimed_slab; 283675485363SMel Gorman 28372ab44f43SMel Gorman /* Account for the number of pages attempted to reclaim */ 28382ab44f43SMel Gorman *nr_attempted += sc->nr_to_reclaim; 28392ab44f43SMel Gorman 2840283aba9fSMel Gorman zone_clear_flag(zone, ZONE_WRITEBACK); 2841283aba9fSMel Gorman 28427c954f6dSMel Gorman /* 28437c954f6dSMel Gorman * If a zone reaches its high watermark, consider it to be no longer 28447c954f6dSMel Gorman * congested. It's possible there are dirty pages backed by congested 28457c954f6dSMel Gorman * BDIs but as pressure is relieved, speculatively avoid congestion 28467c954f6dSMel Gorman * waits. 28477c954f6dSMel Gorman */ 2848*6e543d57SLisa Du if (zone_reclaimable(zone) && 28497c954f6dSMel Gorman zone_balanced(zone, testorder, 0, classzone_idx)) { 28507c954f6dSMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 28517c954f6dSMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 28527c954f6dSMel Gorman } 28537c954f6dSMel Gorman 2854b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 285575485363SMel Gorman } 285675485363SMel Gorman 285775485363SMel Gorman /* 28581da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 285941858966SMel Gorman * they are all at high_wmark_pages(zone). 28601da177e4SLinus Torvalds * 28610abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 28621da177e4SLinus Torvalds * 28631da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 28641da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 28651da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 28661da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 28671da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 28681da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 28691da177e4SLinus Torvalds * the zone for when the problem goes away. 28701da177e4SLinus Torvalds * 28711da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 287241858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 287341858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 287441858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 287541858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 287641858966SMel Gorman * of pages is balanced across the zones. 28771da177e4SLinus Torvalds */ 287899504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2879dc83edd9SMel Gorman int *classzone_idx) 28801da177e4SLinus Torvalds { 28811da177e4SLinus Torvalds int i; 288299504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 28830ae5e89cSYing Han unsigned long nr_soft_reclaimed; 28840ae5e89cSYing Han unsigned long nr_soft_scanned; 2885179e9639SAndrew Morton struct scan_control sc = { 2886179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2887b8e83b94SMel Gorman .priority = DEF_PRIORITY, 2888a6dc60f8SJohannes Weiner .may_unmap = 1, 28892e2e4259SKOSAKI Motohiro .may_swap = 1, 2890b8e83b94SMel Gorman .may_writepage = !laptop_mode, 28915ad333ebSAndy Whitcroft .order = order, 2892f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2893179e9639SAndrew Morton }; 2894f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 28951da177e4SLinus Torvalds 28969e3b2f8cSKonstantin Khlebnikov do { 28971da177e4SLinus Torvalds unsigned long lru_pages = 0; 28982ab44f43SMel Gorman unsigned long nr_attempted = 0; 2899b8e83b94SMel Gorman bool raise_priority = true; 29002ab44f43SMel Gorman bool pgdat_needs_compaction = (order > 0); 2901b8e83b94SMel Gorman 2902b8e83b94SMel Gorman sc.nr_reclaimed = 0; 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds /* 29051da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 29061da177e4SLinus Torvalds * zone which needs scanning 29071da177e4SLinus Torvalds */ 29081da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 29091da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29101da177e4SLinus Torvalds 2911f3fe6512SCon Kolivas if (!populated_zone(zone)) 29121da177e4SLinus Torvalds continue; 29131da177e4SLinus Torvalds 2914*6e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 2915*6e543d57SLisa Du !zone_reclaimable(zone)) 29161da177e4SLinus Torvalds continue; 29171da177e4SLinus Torvalds 2918556adecbSRik van Riel /* 2919556adecbSRik van Riel * Do some background aging of the anon list, to give 2920556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2921556adecbSRik van Riel */ 29229e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2923556adecbSRik van Riel 2924cc715d99SMel Gorman /* 2925cc715d99SMel Gorman * If the number of buffer_heads in the machine 2926cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2927cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2928cc715d99SMel Gorman * it to relieve lowmem pressure. 2929cc715d99SMel Gorman */ 2930cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2931cc715d99SMel Gorman end_zone = i; 2932cc715d99SMel Gorman break; 2933cc715d99SMel Gorman } 2934cc715d99SMel Gorman 293560cefed4SJohannes Weiner if (!zone_balanced(zone, order, 0, 0)) { 29361da177e4SLinus Torvalds end_zone = i; 2937e1dbeda6SAndrew Morton break; 2938439423f6SShaohua Li } else { 2939d43006d5SMel Gorman /* 2940d43006d5SMel Gorman * If balanced, clear the dirty and congested 2941d43006d5SMel Gorman * flags 2942d43006d5SMel Gorman */ 2943439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 2944d43006d5SMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 29451da177e4SLinus Torvalds } 29461da177e4SLinus Torvalds } 2947dafcb73eSZlatko Calusic 2948b8e83b94SMel Gorman if (i < 0) 29491da177e4SLinus Torvalds goto out; 2950e1dbeda6SAndrew Morton 29511da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 29521da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29531da177e4SLinus Torvalds 29542ab44f43SMel Gorman if (!populated_zone(zone)) 29552ab44f43SMel Gorman continue; 29562ab44f43SMel Gorman 2957adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 29582ab44f43SMel Gorman 29592ab44f43SMel Gorman /* 29602ab44f43SMel Gorman * If any zone is currently balanced then kswapd will 29612ab44f43SMel Gorman * not call compaction as it is expected that the 29622ab44f43SMel Gorman * necessary pages are already available. 29632ab44f43SMel Gorman */ 29642ab44f43SMel Gorman if (pgdat_needs_compaction && 29652ab44f43SMel Gorman zone_watermark_ok(zone, order, 29662ab44f43SMel Gorman low_wmark_pages(zone), 29672ab44f43SMel Gorman *classzone_idx, 0)) 29682ab44f43SMel Gorman pgdat_needs_compaction = false; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29711da177e4SLinus Torvalds /* 2972b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 2973b7ea3c41SMel Gorman * even in laptop mode. 2974b7ea3c41SMel Gorman */ 2975b7ea3c41SMel Gorman if (sc.priority < DEF_PRIORITY - 2) 2976b7ea3c41SMel Gorman sc.may_writepage = 1; 2977b7ea3c41SMel Gorman 2978b7ea3c41SMel Gorman /* 29791da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 29801da177e4SLinus Torvalds * at the last zone which needs scanning. 29811da177e4SLinus Torvalds * 29821da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 29831da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 29841da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 29851da177e4SLinus Torvalds * cause too much scanning of the lower zones. 29861da177e4SLinus Torvalds */ 29871da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 29881da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29891da177e4SLinus Torvalds 2990f3fe6512SCon Kolivas if (!populated_zone(zone)) 29911da177e4SLinus Torvalds continue; 29921da177e4SLinus Torvalds 2993*6e543d57SLisa Du if (sc.priority != DEF_PRIORITY && 2994*6e543d57SLisa Du !zone_reclaimable(zone)) 29951da177e4SLinus Torvalds continue; 29961da177e4SLinus Torvalds 29971da177e4SLinus Torvalds sc.nr_scanned = 0; 29984e416953SBalbir Singh 29990ae5e89cSYing Han nr_soft_scanned = 0; 30004e416953SBalbir Singh /* 30014e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 30024e416953SBalbir Singh */ 30030ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 30040ae5e89cSYing Han order, sc.gfp_mask, 30050ae5e89cSYing Han &nr_soft_scanned); 30060ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 300700918b6aSKOSAKI Motohiro 300832a4330dSRik van Riel /* 30097c954f6dSMel Gorman * There should be no need to raise the scanning 30107c954f6dSMel Gorman * priority if enough pages are already being scanned 30117c954f6dSMel Gorman * that that high watermark would be met at 100% 30127c954f6dSMel Gorman * efficiency. 301332a4330dSRik van Riel */ 30147c954f6dSMel Gorman if (kswapd_shrink_zone(zone, end_zone, &sc, 30157c954f6dSMel Gorman lru_pages, &nr_attempted)) 3016b8e83b94SMel Gorman raise_priority = false; 3017b8e83b94SMel Gorman } 3018d7868daeSMel Gorman 30195515061dSMel Gorman /* 30205515061dSMel Gorman * If the low watermark is met there is no need for processes 30215515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 30225515061dSMel Gorman * able to safely make forward progress. Wake them 30235515061dSMel Gorman */ 30245515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 30255515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 30265515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 30275515061dSMel Gorman 30281da177e4SLinus Torvalds /* 3029b8e83b94SMel Gorman * Fragmentation may mean that the system cannot be rebalanced 3030b8e83b94SMel Gorman * for high-order allocations in all zones. If twice the 3031b8e83b94SMel Gorman * allocation size has been reclaimed and the zones are still 3032b8e83b94SMel Gorman * not balanced then recheck the watermarks at order-0 to 3033b8e83b94SMel Gorman * prevent kswapd reclaiming excessively. Assume that a 3034b8e83b94SMel Gorman * process requested a high-order can direct reclaim/compact. 30351da177e4SLinus Torvalds */ 3036b8e83b94SMel Gorman if (order && sc.nr_reclaimed >= 2UL << order) 303773ce02e9SKOSAKI Motohiro order = sc.order = 0; 303873ce02e9SKOSAKI Motohiro 3039b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3040b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3041b8e83b94SMel Gorman break; 3042b8e83b94SMel Gorman 3043b8e83b94SMel Gorman /* 30442ab44f43SMel Gorman * Compact if necessary and kswapd is reclaiming at least the 30452ab44f43SMel Gorman * high watermark number of pages as requsted 30462ab44f43SMel Gorman */ 30472ab44f43SMel Gorman if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted) 30482ab44f43SMel Gorman compact_pgdat(pgdat, order); 30492ab44f43SMel Gorman 30502ab44f43SMel Gorman /* 3051b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3052b8e83b94SMel Gorman * progress in reclaiming pages 3053b8e83b94SMel Gorman */ 3054b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3055b8e83b94SMel Gorman sc.priority--; 30569aa41348SMel Gorman } while (sc.priority >= 1 && 3057b8e83b94SMel Gorman !pgdat_balanced(pgdat, order, *classzone_idx)); 30581da177e4SLinus Torvalds 3059b8e83b94SMel Gorman out: 30600abdee2bSMel Gorman /* 30615515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 30620abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 30630abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 30640abdee2bSMel Gorman * was awake, order will remain at the higher level 30650abdee2bSMel Gorman */ 3066dc83edd9SMel Gorman *classzone_idx = end_zone; 30670abdee2bSMel Gorman return order; 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 3070dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3071f0bc0a60SKOSAKI Motohiro { 3072f0bc0a60SKOSAKI Motohiro long remaining = 0; 3073f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3074f0bc0a60SKOSAKI Motohiro 3075f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3076f0bc0a60SKOSAKI Motohiro return; 3077f0bc0a60SKOSAKI Motohiro 3078f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3079f0bc0a60SKOSAKI Motohiro 3080f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 30815515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3082f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 3083f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3084f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3085f0bc0a60SKOSAKI Motohiro } 3086f0bc0a60SKOSAKI Motohiro 3087f0bc0a60SKOSAKI Motohiro /* 3088f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3089f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3090f0bc0a60SKOSAKI Motohiro */ 30915515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3092f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3093f0bc0a60SKOSAKI Motohiro 3094f0bc0a60SKOSAKI Motohiro /* 3095f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3096f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3097f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3098f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3099f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3100f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3101f0bc0a60SKOSAKI Motohiro */ 3102f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 31031c7e7f6cSAaditya Kumar 310462997027SMel Gorman /* 310562997027SMel Gorman * Compaction records what page blocks it recently failed to 310662997027SMel Gorman * isolate pages from and skips them in the future scanning. 310762997027SMel Gorman * When kswapd is going to sleep, it is reasonable to assume 310862997027SMel Gorman * that pages and compaction may succeed so reset the cache. 310962997027SMel Gorman */ 311062997027SMel Gorman reset_isolation_suitable(pgdat); 311162997027SMel Gorman 31121c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3113f0bc0a60SKOSAKI Motohiro schedule(); 31141c7e7f6cSAaditya Kumar 3115f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3116f0bc0a60SKOSAKI Motohiro } else { 3117f0bc0a60SKOSAKI Motohiro if (remaining) 3118f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3119f0bc0a60SKOSAKI Motohiro else 3120f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3121f0bc0a60SKOSAKI Motohiro } 3122f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3123f0bc0a60SKOSAKI Motohiro } 3124f0bc0a60SKOSAKI Motohiro 31251da177e4SLinus Torvalds /* 31261da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 31271da177e4SLinus Torvalds * from the init process. 31281da177e4SLinus Torvalds * 31291da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 31301da177e4SLinus Torvalds * free memory available even if there is no other activity 31311da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 31321da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 31331da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 31341da177e4SLinus Torvalds * 31351da177e4SLinus Torvalds * If there are applications that are active memory-allocators 31361da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 31371da177e4SLinus Torvalds */ 31381da177e4SLinus Torvalds static int kswapd(void *p) 31391da177e4SLinus Torvalds { 3140215ddd66SMel Gorman unsigned long order, new_order; 3141d2ebd0f6SAlex,Shi unsigned balanced_order; 3142215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 3143d2ebd0f6SAlex,Shi int balanced_classzone_idx; 31441da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 31451da177e4SLinus Torvalds struct task_struct *tsk = current; 3146f0bc0a60SKOSAKI Motohiro 31471da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 31481da177e4SLinus Torvalds .reclaimed_slab = 0, 31491da177e4SLinus Torvalds }; 3150a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 31511da177e4SLinus Torvalds 3152cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3153cf40bd16SNick Piggin 3154174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3155c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 31561da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 31571da177e4SLinus Torvalds 31581da177e4SLinus Torvalds /* 31591da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 31601da177e4SLinus Torvalds * and that if we need more memory we should get access to it 31611da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 31621da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 31631da177e4SLinus Torvalds * 31641da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 31651da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 31661da177e4SLinus Torvalds * page out something else, and this flag essentially protects 31671da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 31681da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 31691da177e4SLinus Torvalds */ 3170930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 317183144186SRafael J. Wysocki set_freezable(); 31721da177e4SLinus Torvalds 3173215ddd66SMel Gorman order = new_order = 0; 3174d2ebd0f6SAlex,Shi balanced_order = 0; 3175215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 3176d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 31771da177e4SLinus Torvalds for ( ; ; ) { 31786f6313d4SJeff Liu bool ret; 31793e1d1d28SChristoph Lameter 3180215ddd66SMel Gorman /* 3181215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 3182215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 3183215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 3184215ddd66SMel Gorman */ 3185d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 3186d2ebd0f6SAlex,Shi balanced_order == new_order) { 31871da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 318899504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 31891da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 3190215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 3191215ddd66SMel Gorman } 3192215ddd66SMel Gorman 319399504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 31941da177e4SLinus Torvalds /* 31951da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 319699504748SMel Gorman * allocation or has tigher zone constraints 31971da177e4SLinus Torvalds */ 31981da177e4SLinus Torvalds order = new_order; 319999504748SMel Gorman classzone_idx = new_classzone_idx; 32001da177e4SLinus Torvalds } else { 3201d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 3202d2ebd0f6SAlex,Shi balanced_classzone_idx); 32031da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 320499504748SMel Gorman classzone_idx = pgdat->classzone_idx; 3205f0dfcde0SAlex,Shi new_order = order; 3206f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 32074d40502eSMel Gorman pgdat->kswapd_max_order = 0; 3208215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 32091da177e4SLinus Torvalds } 32101da177e4SLinus Torvalds 32118fe23e05SDavid Rientjes ret = try_to_freeze(); 32128fe23e05SDavid Rientjes if (kthread_should_stop()) 32138fe23e05SDavid Rientjes break; 32148fe23e05SDavid Rientjes 32158fe23e05SDavid Rientjes /* 32168fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 32178fe23e05SDavid Rientjes * after returning from the refrigerator 3218b1296cc4SRafael J. Wysocki */ 321933906bc5SMel Gorman if (!ret) { 322033906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 3221d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 3222d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 3223d2ebd0f6SAlex,Shi &balanced_classzone_idx); 32241da177e4SLinus Torvalds } 322533906bc5SMel Gorman } 3226b0a8cc58STakamori Yamaguchi 3227b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 32281da177e4SLinus Torvalds return 0; 32291da177e4SLinus Torvalds } 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds /* 32321da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 32331da177e4SLinus Torvalds */ 323499504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 32351da177e4SLinus Torvalds { 32361da177e4SLinus Torvalds pg_data_t *pgdat; 32371da177e4SLinus Torvalds 3238f3fe6512SCon Kolivas if (!populated_zone(zone)) 32391da177e4SLinus Torvalds return; 32401da177e4SLinus Torvalds 324102a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 32421da177e4SLinus Torvalds return; 324388f5acf8SMel Gorman pgdat = zone->zone_pgdat; 324499504748SMel Gorman if (pgdat->kswapd_max_order < order) { 324588f5acf8SMel Gorman pgdat->kswapd_max_order = order; 324699504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 324799504748SMel Gorman } 32488d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 32491da177e4SLinus Torvalds return; 3250892f795dSJohannes Weiner if (zone_balanced(zone, order, 0, 0)) 325188f5acf8SMel Gorman return; 325288f5acf8SMel Gorman 325388f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 32548d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds 3257adea02a1SWu Fengguang /* 3258adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 3259adea02a1SWu Fengguang * The less reclaimable pages may be 3260adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 3261adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 3262adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 3263adea02a1SWu Fengguang */ 3264adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 32654f98a2feSRik van Riel { 3266adea02a1SWu Fengguang int nr; 3267adea02a1SWu Fengguang 3268adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 3269adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 3270adea02a1SWu Fengguang 3271ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 3272adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 3273adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 3274adea02a1SWu Fengguang 3275adea02a1SWu Fengguang return nr; 3276adea02a1SWu Fengguang } 3277adea02a1SWu Fengguang 3278c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 32791da177e4SLinus Torvalds /* 32807b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3281d6277db4SRafael J. Wysocki * freed pages. 3282d6277db4SRafael J. Wysocki * 3283d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3284d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3285d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 32861da177e4SLinus Torvalds */ 32877b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 32881da177e4SLinus Torvalds { 3289d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3290d6277db4SRafael J. Wysocki struct scan_control sc = { 32917b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 32927b51755cSKOSAKI Motohiro .may_swap = 1, 32937b51755cSKOSAKI Motohiro .may_unmap = 1, 3294d6277db4SRafael J. Wysocki .may_writepage = 1, 32957b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 32967b51755cSKOSAKI Motohiro .hibernation_mode = 1, 32977b51755cSKOSAKI Motohiro .order = 0, 32989e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 32991da177e4SLinus Torvalds }; 3300a09ed5e0SYing Han struct shrink_control shrink = { 3301a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3302a09ed5e0SYing Han }; 33037b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 33047b51755cSKOSAKI Motohiro struct task_struct *p = current; 33057b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 33061da177e4SLinus Torvalds 33077b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 33087b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3309d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 33107b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3311d6277db4SRafael J. Wysocki 3312a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 3313d6277db4SRafael J. Wysocki 33147b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 33157b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 33167b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3317d6277db4SRafael J. Wysocki 33187b51755cSKOSAKI Motohiro return nr_reclaimed; 33191da177e4SLinus Torvalds } 3320c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 33211da177e4SLinus Torvalds 33221da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 33231da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 33241da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 33251da177e4SLinus Torvalds restore their cpu bindings. */ 3326fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action, 3327fcb35a9bSGreg Kroah-Hartman void *hcpu) 33281da177e4SLinus Torvalds { 332958c0a4a7SYasunori Goto int nid; 33301da177e4SLinus Torvalds 33318bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 333248fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3333c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3334a70f7302SRusty Russell const struct cpumask *mask; 3335a70f7302SRusty Russell 3336a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3337c5f59f08SMike Travis 33383e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 33391da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3340c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds } 33431da177e4SLinus Torvalds return NOTIFY_OK; 33441da177e4SLinus Torvalds } 33451da177e4SLinus Torvalds 33463218ae14SYasunori Goto /* 33473218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 33483218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 33493218ae14SYasunori Goto */ 33503218ae14SYasunori Goto int kswapd_run(int nid) 33513218ae14SYasunori Goto { 33523218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 33533218ae14SYasunori Goto int ret = 0; 33543218ae14SYasunori Goto 33553218ae14SYasunori Goto if (pgdat->kswapd) 33563218ae14SYasunori Goto return 0; 33573218ae14SYasunori Goto 33583218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 33593218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 33603218ae14SYasunori Goto /* failure at boot is fatal */ 33613218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3362d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3363d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3364d72515b8SXishi Qiu pgdat->kswapd = NULL; 33653218ae14SYasunori Goto } 33663218ae14SYasunori Goto return ret; 33673218ae14SYasunori Goto } 33683218ae14SYasunori Goto 33698fe23e05SDavid Rientjes /* 3370d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3371d8adde17SJiang Liu * hold lock_memory_hotplug(). 33728fe23e05SDavid Rientjes */ 33738fe23e05SDavid Rientjes void kswapd_stop(int nid) 33748fe23e05SDavid Rientjes { 33758fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 33768fe23e05SDavid Rientjes 3377d8adde17SJiang Liu if (kswapd) { 33788fe23e05SDavid Rientjes kthread_stop(kswapd); 3379d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3380d8adde17SJiang Liu } 33818fe23e05SDavid Rientjes } 33828fe23e05SDavid Rientjes 33831da177e4SLinus Torvalds static int __init kswapd_init(void) 33841da177e4SLinus Torvalds { 33853218ae14SYasunori Goto int nid; 338669e05944SAndrew Morton 33871da177e4SLinus Torvalds swap_setup(); 338848fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 33893218ae14SYasunori Goto kswapd_run(nid); 33901da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 33911da177e4SLinus Torvalds return 0; 33921da177e4SLinus Torvalds } 33931da177e4SLinus Torvalds 33941da177e4SLinus Torvalds module_init(kswapd_init) 33959eeff239SChristoph Lameter 33969eeff239SChristoph Lameter #ifdef CONFIG_NUMA 33979eeff239SChristoph Lameter /* 33989eeff239SChristoph Lameter * Zone reclaim mode 33999eeff239SChristoph Lameter * 34009eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 34019eeff239SChristoph Lameter * the watermarks. 34029eeff239SChristoph Lameter */ 34039eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 34049eeff239SChristoph Lameter 34051b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 34067d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 34071b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 34081b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 34091b2ffb78SChristoph Lameter 34109eeff239SChristoph Lameter /* 3411a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3412a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3413a92f7126SChristoph Lameter * a zone. 3414a92f7126SChristoph Lameter */ 3415a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3416a92f7126SChristoph Lameter 34179eeff239SChristoph Lameter /* 34189614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 34199614634fSChristoph Lameter * occur. 34209614634fSChristoph Lameter */ 34219614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 34229614634fSChristoph Lameter 34239614634fSChristoph Lameter /* 34240ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 34250ff38490SChristoph Lameter * slab reclaim needs to occur. 34260ff38490SChristoph Lameter */ 34270ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 34280ff38490SChristoph Lameter 342990afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 343090afa5deSMel Gorman { 343190afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 343290afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 343390afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 343490afa5deSMel Gorman 343590afa5deSMel Gorman /* 343690afa5deSMel Gorman * It's possible for there to be more file mapped pages than 343790afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 343890afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 343990afa5deSMel Gorman */ 344090afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 344190afa5deSMel Gorman } 344290afa5deSMel Gorman 344390afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 344490afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 344590afa5deSMel Gorman { 344690afa5deSMel Gorman long nr_pagecache_reclaimable; 344790afa5deSMel Gorman long delta = 0; 344890afa5deSMel Gorman 344990afa5deSMel Gorman /* 345090afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 345190afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 345290afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 345390afa5deSMel Gorman * a better estimate 345490afa5deSMel Gorman */ 345590afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 345690afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 345790afa5deSMel Gorman else 345890afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 345990afa5deSMel Gorman 346090afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 346190afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 346290afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 346390afa5deSMel Gorman 346490afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 346590afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 346690afa5deSMel Gorman delta = nr_pagecache_reclaimable; 346790afa5deSMel Gorman 346890afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 346990afa5deSMel Gorman } 347090afa5deSMel Gorman 34710ff38490SChristoph Lameter /* 34729eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 34739eeff239SChristoph Lameter */ 3474179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 34759eeff239SChristoph Lameter { 34767fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 347769e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 34789eeff239SChristoph Lameter struct task_struct *p = current; 34799eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3480179e9639SAndrew Morton struct scan_control sc = { 3481179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3482a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 34832e2e4259SKOSAKI Motohiro .may_swap = 1, 348462b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 348521caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3486bd2f6199SJohannes Weiner .order = order, 34879e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3488179e9639SAndrew Morton }; 3489a09ed5e0SYing Han struct shrink_control shrink = { 3490a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3491a09ed5e0SYing Han }; 349215748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 34939eeff239SChristoph Lameter 34949eeff239SChristoph Lameter cond_resched(); 3495d4f7796eSChristoph Lameter /* 3496d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3497d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3498d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3499d4f7796eSChristoph Lameter */ 3500d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 350176ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 35029eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 35039eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3504c84db23cSChristoph Lameter 350590afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3506a92f7126SChristoph Lameter /* 35070ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 35080ff38490SChristoph Lameter * priorities until we have enough memory freed. 3509a92f7126SChristoph Lameter */ 3510a92f7126SChristoph Lameter do { 35119e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 35129e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 35130ff38490SChristoph Lameter } 3514a92f7126SChristoph Lameter 351515748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 351615748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 35172a16e3f4SChristoph Lameter /* 35187fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 35190ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 35200ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 35210ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 35220ff38490SChristoph Lameter * pages. 35232a16e3f4SChristoph Lameter * 35240ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 35250ff38490SChristoph Lameter * take a long time. 35262a16e3f4SChristoph Lameter */ 35274dc4b3d9SKOSAKI Motohiro for (;;) { 35284dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 35294dc4b3d9SKOSAKI Motohiro 35304dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 35311495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 35324dc4b3d9SKOSAKI Motohiro break; 35334dc4b3d9SKOSAKI Motohiro 35344dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 35354dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 35364dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 35374dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 35384dc4b3d9SKOSAKI Motohiro break; 35394dc4b3d9SKOSAKI Motohiro } 354083e33a47SChristoph Lameter 354183e33a47SChristoph Lameter /* 354283e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 354383e33a47SChristoph Lameter * reclaimed from this zone. 354483e33a47SChristoph Lameter */ 354515748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 354615748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 354715748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 35482a16e3f4SChristoph Lameter } 35492a16e3f4SChristoph Lameter 35509eeff239SChristoph Lameter p->reclaim_state = NULL; 3551d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 355276ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3553a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 35549eeff239SChristoph Lameter } 3555179e9639SAndrew Morton 3556179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3557179e9639SAndrew Morton { 3558179e9639SAndrew Morton int node_id; 3559d773ed6bSDavid Rientjes int ret; 3560179e9639SAndrew Morton 3561179e9639SAndrew Morton /* 35620ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 35630ff38490SChristoph Lameter * slab pages if we are over the defined limits. 356434aa1330SChristoph Lameter * 35659614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 35669614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 35679614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 35689614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 35699614634fSChristoph Lameter * unmapped file backed pages. 3570179e9639SAndrew Morton */ 357190afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 357290afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3573fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3574179e9639SAndrew Morton 3575*6e543d57SLisa Du if (!zone_reclaimable(zone)) 3576fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3577d773ed6bSDavid Rientjes 3578179e9639SAndrew Morton /* 3579d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3580179e9639SAndrew Morton */ 3581d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3582fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3583179e9639SAndrew Morton 3584179e9639SAndrew Morton /* 3585179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3586179e9639SAndrew Morton * have associated processors. This will favor the local processor 3587179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3588179e9639SAndrew Morton * as wide as possible. 3589179e9639SAndrew Morton */ 359089fa3024SChristoph Lameter node_id = zone_to_nid(zone); 359137c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3592fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3593d773ed6bSDavid Rientjes 3594d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3595fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3596fa5e084eSMel Gorman 3597d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3598d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3599d773ed6bSDavid Rientjes 360024cf7251SMel Gorman if (!ret) 360124cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 360224cf7251SMel Gorman 3603d773ed6bSDavid Rientjes return ret; 3604179e9639SAndrew Morton } 36059eeff239SChristoph Lameter #endif 3606894bc310SLee Schermerhorn 3607894bc310SLee Schermerhorn /* 3608894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3609894bc310SLee Schermerhorn * @page: the page to test 3610894bc310SLee Schermerhorn * 3611894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 361239b5f29aSHugh Dickins * lists vs unevictable list. 3613894bc310SLee Schermerhorn * 3614894bc310SLee Schermerhorn * Reasons page might not be evictable: 3615ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3616b291f000SNick Piggin * (2) page is part of an mlocked VMA 3617ba9ddf49SLee Schermerhorn * 3618894bc310SLee Schermerhorn */ 361939b5f29aSHugh Dickins int page_evictable(struct page *page) 3620894bc310SLee Schermerhorn { 362139b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3622894bc310SLee Schermerhorn } 362389e004eaSLee Schermerhorn 362485046579SHugh Dickins #ifdef CONFIG_SHMEM 362589e004eaSLee Schermerhorn /** 362624513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 362724513264SHugh Dickins * @pages: array of pages to check 362824513264SHugh Dickins * @nr_pages: number of pages to check 362989e004eaSLee Schermerhorn * 363024513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 363185046579SHugh Dickins * 363285046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 363389e004eaSLee Schermerhorn */ 363424513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 363589e004eaSLee Schermerhorn { 3636925b7673SJohannes Weiner struct lruvec *lruvec; 363724513264SHugh Dickins struct zone *zone = NULL; 363824513264SHugh Dickins int pgscanned = 0; 363924513264SHugh Dickins int pgrescued = 0; 364089e004eaSLee Schermerhorn int i; 364189e004eaSLee Schermerhorn 364224513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 364324513264SHugh Dickins struct page *page = pages[i]; 364424513264SHugh Dickins struct zone *pagezone; 364589e004eaSLee Schermerhorn 364624513264SHugh Dickins pgscanned++; 364724513264SHugh Dickins pagezone = page_zone(page); 364889e004eaSLee Schermerhorn if (pagezone != zone) { 364989e004eaSLee Schermerhorn if (zone) 365089e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 365189e004eaSLee Schermerhorn zone = pagezone; 365289e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 365389e004eaSLee Schermerhorn } 3654fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 365589e004eaSLee Schermerhorn 365624513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 365724513264SHugh Dickins continue; 365889e004eaSLee Schermerhorn 365939b5f29aSHugh Dickins if (page_evictable(page)) { 366024513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 366124513264SHugh Dickins 366224513264SHugh Dickins VM_BUG_ON(PageActive(page)); 366324513264SHugh Dickins ClearPageUnevictable(page); 3664fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3665fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 366624513264SHugh Dickins pgrescued++; 366789e004eaSLee Schermerhorn } 366889e004eaSLee Schermerhorn } 366924513264SHugh Dickins 367024513264SHugh Dickins if (zone) { 367124513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 367224513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 367324513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 367424513264SHugh Dickins } 367585046579SHugh Dickins } 367685046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3677af936a16SLee Schermerhorn 3678264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3679af936a16SLee Schermerhorn { 3680264e56d8SJohannes Weiner printk_once(KERN_WARNING 368125bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3682264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 368325bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 368425bd91bdSKOSAKI Motohiro current->comm); 3685af936a16SLee Schermerhorn } 3686af936a16SLee Schermerhorn 3687af936a16SLee Schermerhorn /* 3688af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3689af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3690af936a16SLee Schermerhorn */ 3691af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3692af936a16SLee Schermerhorn 3693af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 36948d65af78SAlexey Dobriyan void __user *buffer, 3695af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3696af936a16SLee Schermerhorn { 3697264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 36988d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3699af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3700af936a16SLee Schermerhorn return 0; 3701af936a16SLee Schermerhorn } 3702af936a16SLee Schermerhorn 3703e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3704af936a16SLee Schermerhorn /* 3705af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3706af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3707af936a16SLee Schermerhorn */ 3708af936a16SLee Schermerhorn 370910fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 371010fbcf4cSKay Sievers struct device_attribute *attr, 3711af936a16SLee Schermerhorn char *buf) 3712af936a16SLee Schermerhorn { 3713264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3714af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3715af936a16SLee Schermerhorn } 3716af936a16SLee Schermerhorn 371710fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 371810fbcf4cSKay Sievers struct device_attribute *attr, 3719af936a16SLee Schermerhorn const char *buf, size_t count) 3720af936a16SLee Schermerhorn { 3721264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3722af936a16SLee Schermerhorn return 1; 3723af936a16SLee Schermerhorn } 3724af936a16SLee Schermerhorn 3725af936a16SLee Schermerhorn 372610fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3727af936a16SLee Schermerhorn read_scan_unevictable_node, 3728af936a16SLee Schermerhorn write_scan_unevictable_node); 3729af936a16SLee Schermerhorn 3730af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3731af936a16SLee Schermerhorn { 373210fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3733af936a16SLee Schermerhorn } 3734af936a16SLee Schermerhorn 3735af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3736af936a16SLee Schermerhorn { 373710fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3738af936a16SLee Schermerhorn } 3739e4455abbSThadeu Lima de Souza Cascardo #endif 3740