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 1494d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 150c9f299d9SKOSAKI Motohiro { 151c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1524d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 153a3d8e054SKOSAKI Motohiro 154074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 155c9f299d9SKOSAKI Motohiro } 156c9f299d9SKOSAKI Motohiro 1571da177e4SLinus Torvalds /* 1581da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1591da177e4SLinus Torvalds */ 1608e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker) 1611da177e4SLinus Torvalds { 16283aeeadaSKonstantin Khlebnikov atomic_long_set(&shrinker->nr_in_batch, 0); 1631da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1641da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1651da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1661da177e4SLinus Torvalds } 1678e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds /* 1701da177e4SLinus Torvalds * Remove one 1711da177e4SLinus Torvalds */ 1728e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1751da177e4SLinus Torvalds list_del(&shrinker->list); 1761da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1771da177e4SLinus Torvalds } 1788e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 1791da177e4SLinus Torvalds 1801495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker, 1811495f230SYing Han struct shrink_control *sc, 1821495f230SYing Han unsigned long nr_to_scan) 1831495f230SYing Han { 1841495f230SYing Han sc->nr_to_scan = nr_to_scan; 1851495f230SYing Han return (*shrinker->shrink)(shrinker, sc); 1861495f230SYing Han } 1871495f230SYing Han 1881da177e4SLinus Torvalds #define SHRINK_BATCH 128 1891da177e4SLinus Torvalds /* 1901da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 1911da177e4SLinus Torvalds * 1921da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 1931da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 1941da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 1951da177e4SLinus Torvalds * generated by these structures. 1961da177e4SLinus Torvalds * 197183ff22bSSimon Arlott * If the vm encountered mapped pages on the LRU it increase the pressure on 1981da177e4SLinus Torvalds * slab to avoid swapping. 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 2011da177e4SLinus Torvalds * 2021da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 2031da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 2041da177e4SLinus Torvalds * slab reclaim versus page reclaim. 205b15e0905Sakpm@osdl.org * 206b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 2071da177e4SLinus Torvalds */ 208a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink, 2091495f230SYing Han unsigned long nr_pages_scanned, 21069e05944SAndrew Morton unsigned long lru_pages) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct shrinker *shrinker; 21369e05944SAndrew Morton unsigned long ret = 0; 2141da177e4SLinus Torvalds 2151495f230SYing Han if (nr_pages_scanned == 0) 2161495f230SYing Han nr_pages_scanned = SWAP_CLUSTER_MAX; 2171da177e4SLinus Torvalds 218f06590bdSMinchan Kim if (!down_read_trylock(&shrinker_rwsem)) { 219f06590bdSMinchan Kim /* Assume we'll be able to shrink next time */ 220f06590bdSMinchan Kim ret = 1; 221f06590bdSMinchan Kim goto out; 222f06590bdSMinchan Kim } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 2251da177e4SLinus Torvalds unsigned long long delta; 226635697c6SKonstantin Khlebnikov long total_scan; 227635697c6SKonstantin Khlebnikov long max_pass; 22809576073SDave Chinner int shrink_ret = 0; 229acf92b48SDave Chinner long nr; 230acf92b48SDave Chinner long new_nr; 231e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 232e9299f50SDave Chinner : SHRINK_BATCH; 2331da177e4SLinus Torvalds 234635697c6SKonstantin Khlebnikov max_pass = do_shrinker_shrink(shrinker, shrink, 0); 235635697c6SKonstantin Khlebnikov if (max_pass <= 0) 236635697c6SKonstantin Khlebnikov continue; 237635697c6SKonstantin Khlebnikov 238acf92b48SDave Chinner /* 239acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 240acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 241acf92b48SDave Chinner * don't also do this scanning work. 242acf92b48SDave Chinner */ 24383aeeadaSKonstantin Khlebnikov nr = atomic_long_xchg(&shrinker->nr_in_batch, 0); 244acf92b48SDave Chinner 245acf92b48SDave Chinner total_scan = nr; 2461495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 247ea164d73SAndrea Arcangeli delta *= max_pass; 2481da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 249acf92b48SDave Chinner total_scan += delta; 250acf92b48SDave Chinner if (total_scan < 0) { 25188c3bd70SDavid Rientjes printk(KERN_ERR "shrink_slab: %pF negative objects to " 25288c3bd70SDavid Rientjes "delete nr=%ld\n", 253acf92b48SDave Chinner shrinker->shrink, total_scan); 254acf92b48SDave Chinner total_scan = max_pass; 255ea164d73SAndrea Arcangeli } 256ea164d73SAndrea Arcangeli 257ea164d73SAndrea Arcangeli /* 2583567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2593567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2603567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2613567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2623567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2633567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2643567b59aSDave Chinner * memory. 2653567b59aSDave Chinner * 2663567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2673567b59aSDave Chinner * a large delta change is calculated directly. 2683567b59aSDave Chinner */ 2693567b59aSDave Chinner if (delta < max_pass / 4) 2703567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2713567b59aSDave Chinner 2723567b59aSDave Chinner /* 273ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 274ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 275ea164d73SAndrea Arcangeli * freeable entries. 276ea164d73SAndrea Arcangeli */ 277acf92b48SDave Chinner if (total_scan > max_pass * 2) 278acf92b48SDave Chinner total_scan = max_pass * 2; 2791da177e4SLinus Torvalds 280acf92b48SDave Chinner trace_mm_shrink_slab_start(shrinker, shrink, nr, 28109576073SDave Chinner nr_pages_scanned, lru_pages, 28209576073SDave Chinner max_pass, delta, total_scan); 28309576073SDave Chinner 284e9299f50SDave Chinner while (total_scan >= batch_size) { 285b15e0905Sakpm@osdl.org int nr_before; 2861da177e4SLinus Torvalds 2871495f230SYing Han nr_before = do_shrinker_shrink(shrinker, shrink, 0); 2881495f230SYing Han shrink_ret = do_shrinker_shrink(shrinker, shrink, 289e9299f50SDave Chinner batch_size); 2901da177e4SLinus Torvalds if (shrink_ret == -1) 2911da177e4SLinus Torvalds break; 292b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 293b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 294e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 295e9299f50SDave Chinner total_scan -= batch_size; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds cond_resched(); 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 300acf92b48SDave Chinner /* 301acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 302acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 303acf92b48SDave Chinner * scan, there is no need to do an update. 304acf92b48SDave Chinner */ 30583aeeadaSKonstantin Khlebnikov if (total_scan > 0) 30683aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 30783aeeadaSKonstantin Khlebnikov &shrinker->nr_in_batch); 30883aeeadaSKonstantin Khlebnikov else 30983aeeadaSKonstantin Khlebnikov new_nr = atomic_long_read(&shrinker->nr_in_batch); 310acf92b48SDave Chinner 311acf92b48SDave Chinner trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr); 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds up_read(&shrinker_rwsem); 314f06590bdSMinchan Kim out: 315f06590bdSMinchan Kim cond_resched(); 316b15e0905Sakpm@osdl.org return ret; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3201da177e4SLinus Torvalds { 321ceddc3a5SJohannes Weiner /* 322ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 323ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 324ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 325ceddc3a5SJohannes Weiner */ 326edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3297d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3307d3579e8SKOSAKI Motohiro struct scan_control *sc) 3311da177e4SLinus Torvalds { 332930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3331da177e4SLinus Torvalds return 1; 3341da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3351da177e4SLinus Torvalds return 1; 3361da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3371da177e4SLinus Torvalds return 1; 3381da177e4SLinus Torvalds return 0; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* 3421da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3431da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3441da177e4SLinus Torvalds * fsync(), msync() or close(). 3451da177e4SLinus Torvalds * 3461da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 3471da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 3481da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 3491da177e4SLinus Torvalds * 3501da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 3511da177e4SLinus Torvalds * __GFP_FS. 3521da177e4SLinus Torvalds */ 3531da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 3541da177e4SLinus Torvalds struct page *page, int error) 3551da177e4SLinus Torvalds { 3567eaceaccSJens Axboe lock_page(page); 3573e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 3583e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 3591da177e4SLinus Torvalds unlock_page(page); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 36204e62a29SChristoph Lameter /* possible outcome of pageout() */ 36304e62a29SChristoph Lameter typedef enum { 36404e62a29SChristoph Lameter /* failed to write page out, page is locked */ 36504e62a29SChristoph Lameter PAGE_KEEP, 36604e62a29SChristoph Lameter /* move page to the active list, page is locked */ 36704e62a29SChristoph Lameter PAGE_ACTIVATE, 36804e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 36904e62a29SChristoph Lameter PAGE_SUCCESS, 37004e62a29SChristoph Lameter /* page is clean and locked */ 37104e62a29SChristoph Lameter PAGE_CLEAN, 37204e62a29SChristoph Lameter } pageout_t; 37304e62a29SChristoph Lameter 3741da177e4SLinus Torvalds /* 3751742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3761742f19fSAndrew Morton * Calls ->writepage(). 3771da177e4SLinus Torvalds */ 378c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 3797d3579e8SKOSAKI Motohiro struct scan_control *sc) 3801da177e4SLinus Torvalds { 3811da177e4SLinus Torvalds /* 3821da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3831da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3841da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3851da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3861da177e4SLinus Torvalds * PagePrivate for that. 3871da177e4SLinus Torvalds * 3886aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 3891da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 3901da177e4SLinus Torvalds * will block. 3911da177e4SLinus Torvalds * 3921da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 3931da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 3941da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 3951da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 3961da177e4SLinus Torvalds */ 3971da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 3981da177e4SLinus Torvalds return PAGE_KEEP; 3991da177e4SLinus Torvalds if (!mapping) { 4001da177e4SLinus Torvalds /* 4011da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4021da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4031da177e4SLinus Torvalds */ 404266cf658SDavid Howells if (page_has_private(page)) { 4051da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4061da177e4SLinus Torvalds ClearPageDirty(page); 407d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4081da177e4SLinus Torvalds return PAGE_CLEAN; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds return PAGE_KEEP; 4121da177e4SLinus Torvalds } 4131da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4141da177e4SLinus Torvalds return PAGE_ACTIVATE; 4150e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4161da177e4SLinus Torvalds return PAGE_KEEP; 4171da177e4SLinus Torvalds 4181da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4191da177e4SLinus Torvalds int res; 4201da177e4SLinus Torvalds struct writeback_control wbc = { 4211da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4221da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 423111ebb6eSOGAWA Hirofumi .range_start = 0, 424111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4251da177e4SLinus Torvalds .for_reclaim = 1, 4261da177e4SLinus Torvalds }; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds SetPageReclaim(page); 4291da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4301da177e4SLinus Torvalds if (res < 0) 4311da177e4SLinus Torvalds handle_write_error(mapping, page, res); 432994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4331da177e4SLinus Torvalds ClearPageReclaim(page); 4341da177e4SLinus Torvalds return PAGE_ACTIVATE; 4351da177e4SLinus Torvalds } 436c661b078SAndy Whitcroft 4371da177e4SLinus Torvalds if (!PageWriteback(page)) { 4381da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4391da177e4SLinus Torvalds ClearPageReclaim(page); 4401da177e4SLinus Torvalds } 44123b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 442e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4431da177e4SLinus Torvalds return PAGE_SUCCESS; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds return PAGE_CLEAN; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds 449a649fd92SAndrew Morton /* 450e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 451e286781dSNick Piggin * gets returned with a refcount of 0. 452a649fd92SAndrew Morton */ 453e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 45449d2e9ccSChristoph Lameter { 45528e4d965SNick Piggin BUG_ON(!PageLocked(page)); 45628e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 45749d2e9ccSChristoph Lameter 45819fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 45949d2e9ccSChristoph Lameter /* 4600fd0e6b0SNick Piggin * The non racy check for a busy page. 4610fd0e6b0SNick Piggin * 4620fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 4630fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 4640fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 4650fd0e6b0SNick Piggin * here, then the following race may occur: 4660fd0e6b0SNick Piggin * 4670fd0e6b0SNick Piggin * get_user_pages(&page); 4680fd0e6b0SNick Piggin * [user mapping goes away] 4690fd0e6b0SNick Piggin * write_to(page); 4700fd0e6b0SNick Piggin * !PageDirty(page) [good] 4710fd0e6b0SNick Piggin * SetPageDirty(page); 4720fd0e6b0SNick Piggin * put_page(page); 4730fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 4740fd0e6b0SNick Piggin * 4750fd0e6b0SNick Piggin * [oops, our write_to data is lost] 4760fd0e6b0SNick Piggin * 4770fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 4780fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 4790fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 4800fd0e6b0SNick Piggin * 4810fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 4820fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 48349d2e9ccSChristoph Lameter */ 484e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 48549d2e9ccSChristoph Lameter goto cannot_free; 486e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 487e286781dSNick Piggin if (unlikely(PageDirty(page))) { 488e286781dSNick Piggin page_unfreeze_refs(page, 2); 48949d2e9ccSChristoph Lameter goto cannot_free; 490e286781dSNick Piggin } 49149d2e9ccSChristoph Lameter 49249d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 49349d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 49449d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 49519fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 496cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 497e286781dSNick Piggin } else { 4986072d13cSLinus Torvalds void (*freepage)(struct page *); 4996072d13cSLinus Torvalds 5006072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5016072d13cSLinus Torvalds 502e64a782fSMinchan Kim __delete_from_page_cache(page); 50319fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 504e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5056072d13cSLinus Torvalds 5066072d13cSLinus Torvalds if (freepage != NULL) 5076072d13cSLinus Torvalds freepage(page); 508e286781dSNick Piggin } 509e286781dSNick Piggin 51049d2e9ccSChristoph Lameter return 1; 51149d2e9ccSChristoph Lameter 51249d2e9ccSChristoph Lameter cannot_free: 51319fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 51449d2e9ccSChristoph Lameter return 0; 51549d2e9ccSChristoph Lameter } 51649d2e9ccSChristoph Lameter 5171da177e4SLinus Torvalds /* 518e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 519e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 520e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 521e286781dSNick Piggin * this page. 522e286781dSNick Piggin */ 523e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 524e286781dSNick Piggin { 525e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 526e286781dSNick Piggin /* 527e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 528e286781dSNick Piggin * drops the pagecache ref for us without requiring another 529e286781dSNick Piggin * atomic operation. 530e286781dSNick Piggin */ 531e286781dSNick Piggin page_unfreeze_refs(page, 1); 532e286781dSNick Piggin return 1; 533e286781dSNick Piggin } 534e286781dSNick Piggin return 0; 535e286781dSNick Piggin } 536e286781dSNick Piggin 537894bc310SLee Schermerhorn /** 538894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 539894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 540894bc310SLee Schermerhorn * 541894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 542894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 543894bc310SLee Schermerhorn * 544894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 545894bc310SLee Schermerhorn */ 546894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 547894bc310SLee Schermerhorn { 548894bc310SLee Schermerhorn int lru; 549bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 550894bc310SLee Schermerhorn 551894bc310SLee Schermerhorn VM_BUG_ON(PageLRU(page)); 552894bc310SLee Schermerhorn 553894bc310SLee Schermerhorn redo: 554894bc310SLee Schermerhorn ClearPageUnevictable(page); 555894bc310SLee Schermerhorn 55639b5f29aSHugh Dickins if (page_evictable(page)) { 557894bc310SLee Schermerhorn /* 558894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 559894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 560894bc310SLee Schermerhorn * unevictable page on [in]active list. 561894bc310SLee Schermerhorn * We know how to handle that. 562894bc310SLee Schermerhorn */ 563c53954a0SMel Gorman lru = page_lru_base_type(page); 564c53954a0SMel Gorman lru_cache_add(page); 565894bc310SLee Schermerhorn } else { 566894bc310SLee Schermerhorn /* 567894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 568894bc310SLee Schermerhorn * list. 569894bc310SLee Schermerhorn */ 570894bc310SLee Schermerhorn lru = LRU_UNEVICTABLE; 571894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 5726a7b9548SJohannes Weiner /* 57321ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 57421ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 57521ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 57624513264SHugh Dickins * isolation/check_move_unevictable_pages, 57721ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 5786a7b9548SJohannes Weiner * the page back to the evictable list. 5796a7b9548SJohannes Weiner * 58021ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 5816a7b9548SJohannes Weiner */ 5826a7b9548SJohannes Weiner smp_mb(); 583894bc310SLee Schermerhorn } 584894bc310SLee Schermerhorn 585894bc310SLee Schermerhorn /* 586894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 587894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 588894bc310SLee Schermerhorn * check after we added it to the list, again. 589894bc310SLee Schermerhorn */ 59039b5f29aSHugh Dickins if (lru == LRU_UNEVICTABLE && page_evictable(page)) { 591894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 592894bc310SLee Schermerhorn put_page(page); 593894bc310SLee Schermerhorn goto redo; 594894bc310SLee Schermerhorn } 595894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 596894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 597894bc310SLee Schermerhorn * nothing to do here. 598894bc310SLee Schermerhorn */ 599894bc310SLee Schermerhorn } 600894bc310SLee Schermerhorn 601bbfd28eeSLee Schermerhorn if (was_unevictable && lru != LRU_UNEVICTABLE) 602bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 603bbfd28eeSLee Schermerhorn else if (!was_unevictable && lru == LRU_UNEVICTABLE) 604bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 605bbfd28eeSLee Schermerhorn 606894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 607894bc310SLee Schermerhorn } 608894bc310SLee Schermerhorn 609dfc8d636SJohannes Weiner enum page_references { 610dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 611dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 61264574746SJohannes Weiner PAGEREF_KEEP, 613dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 614dfc8d636SJohannes Weiner }; 615dfc8d636SJohannes Weiner 616dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 617dfc8d636SJohannes Weiner struct scan_control *sc) 618dfc8d636SJohannes Weiner { 61964574746SJohannes Weiner int referenced_ptes, referenced_page; 620dfc8d636SJohannes Weiner unsigned long vm_flags; 621dfc8d636SJohannes Weiner 622c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 623c3ac9a8aSJohannes Weiner &vm_flags); 62464574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 625dfc8d636SJohannes Weiner 626dfc8d636SJohannes Weiner /* 627dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 628dfc8d636SJohannes Weiner * move the page to the unevictable list. 629dfc8d636SJohannes Weiner */ 630dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 631dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 632dfc8d636SJohannes Weiner 63364574746SJohannes Weiner if (referenced_ptes) { 634e4898273SMichal Hocko if (PageSwapBacked(page)) 63564574746SJohannes Weiner return PAGEREF_ACTIVATE; 63664574746SJohannes Weiner /* 63764574746SJohannes Weiner * All mapped pages start out with page table 63864574746SJohannes Weiner * references from the instantiating fault, so we need 63964574746SJohannes Weiner * to look twice if a mapped file page is used more 64064574746SJohannes Weiner * than once. 64164574746SJohannes Weiner * 64264574746SJohannes Weiner * Mark it and spare it for another trip around the 64364574746SJohannes Weiner * inactive list. Another page table reference will 64464574746SJohannes Weiner * lead to its activation. 64564574746SJohannes Weiner * 64664574746SJohannes Weiner * Note: the mark is set for activated pages as well 64764574746SJohannes Weiner * so that recently deactivated but used pages are 64864574746SJohannes Weiner * quickly recovered. 64964574746SJohannes Weiner */ 65064574746SJohannes Weiner SetPageReferenced(page); 65164574746SJohannes Weiner 65234dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 653dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 654dfc8d636SJohannes Weiner 655c909e993SKonstantin Khlebnikov /* 656c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 657c909e993SKonstantin Khlebnikov */ 658c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 659c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 660c909e993SKonstantin Khlebnikov 66164574746SJohannes Weiner return PAGEREF_KEEP; 66264574746SJohannes Weiner } 66364574746SJohannes Weiner 664dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 6652e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 666dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 66764574746SJohannes Weiner 66864574746SJohannes Weiner return PAGEREF_RECLAIM; 669dfc8d636SJohannes Weiner } 670dfc8d636SJohannes Weiner 671e2be15f6SMel Gorman /* Check if a page is dirty or under writeback */ 672e2be15f6SMel Gorman static void page_check_dirty_writeback(struct page *page, 673e2be15f6SMel Gorman bool *dirty, bool *writeback) 674e2be15f6SMel Gorman { 675b4597226SMel Gorman struct address_space *mapping; 676b4597226SMel Gorman 677e2be15f6SMel Gorman /* 678e2be15f6SMel Gorman * Anonymous pages are not handled by flushers and must be written 679e2be15f6SMel Gorman * from reclaim context. Do not stall reclaim based on them 680e2be15f6SMel Gorman */ 681e2be15f6SMel Gorman if (!page_is_file_cache(page)) { 682e2be15f6SMel Gorman *dirty = false; 683e2be15f6SMel Gorman *writeback = false; 684e2be15f6SMel Gorman return; 685e2be15f6SMel Gorman } 686e2be15f6SMel Gorman 687e2be15f6SMel Gorman /* By default assume that the page flags are accurate */ 688e2be15f6SMel Gorman *dirty = PageDirty(page); 689e2be15f6SMel Gorman *writeback = PageWriteback(page); 690b4597226SMel Gorman 691b4597226SMel Gorman /* Verify dirty/writeback state if the filesystem supports it */ 692b4597226SMel Gorman if (!page_has_private(page)) 693b4597226SMel Gorman return; 694b4597226SMel Gorman 695b4597226SMel Gorman mapping = page_mapping(page); 696b4597226SMel Gorman if (mapping && mapping->a_ops->is_dirty_writeback) 697b4597226SMel Gorman mapping->a_ops->is_dirty_writeback(page, dirty, writeback); 698e2be15f6SMel Gorman } 699e2be15f6SMel Gorman 700e286781dSNick Piggin /* 7011742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 7021da177e4SLinus Torvalds */ 7031742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 7046a18adb3SKonstantin Khlebnikov struct zone *zone, 705f84f6e2bSMel Gorman struct scan_control *sc, 70602c6de8dSMinchan Kim enum ttu_flags ttu_flags, 7078e950282SMel Gorman unsigned long *ret_nr_dirty, 708d43006d5SMel Gorman unsigned long *ret_nr_unqueued_dirty, 7098e950282SMel Gorman unsigned long *ret_nr_congested, 71002c6de8dSMinchan Kim unsigned long *ret_nr_writeback, 711b1a6f21eSMel Gorman unsigned long *ret_nr_immediate, 71202c6de8dSMinchan Kim bool force_reclaim) 7131da177e4SLinus Torvalds { 7141da177e4SLinus Torvalds LIST_HEAD(ret_pages); 715abe4c3b5SMel Gorman LIST_HEAD(free_pages); 7161da177e4SLinus Torvalds int pgactivate = 0; 717d43006d5SMel Gorman unsigned long nr_unqueued_dirty = 0; 7180e093d99SMel Gorman unsigned long nr_dirty = 0; 7190e093d99SMel Gorman unsigned long nr_congested = 0; 72005ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 72192df3a72SMel Gorman unsigned long nr_writeback = 0; 722b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds cond_resched(); 7251da177e4SLinus Torvalds 72669980e31STim Chen mem_cgroup_uncharge_start(); 7271da177e4SLinus Torvalds while (!list_empty(page_list)) { 7281da177e4SLinus Torvalds struct address_space *mapping; 7291da177e4SLinus Torvalds struct page *page; 7301da177e4SLinus Torvalds int may_enter_fs; 73102c6de8dSMinchan Kim enum page_references references = PAGEREF_RECLAIM_CLEAN; 732e2be15f6SMel Gorman bool dirty, writeback; 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds cond_resched(); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds page = lru_to_page(page_list); 7371da177e4SLinus Torvalds list_del(&page->lru); 7381da177e4SLinus Torvalds 739529ae9aaSNick Piggin if (!trylock_page(page)) 7401da177e4SLinus Torvalds goto keep; 7411da177e4SLinus Torvalds 742725d704eSNick Piggin VM_BUG_ON(PageActive(page)); 7436a18adb3SKonstantin Khlebnikov VM_BUG_ON(page_zone(page) != zone); 7441da177e4SLinus Torvalds 7451da177e4SLinus Torvalds sc->nr_scanned++; 74680e43426SChristoph Lameter 74739b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) 748b291f000SNick Piggin goto cull_mlocked; 749894bc310SLee Schermerhorn 750a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 75180e43426SChristoph Lameter goto keep_locked; 75280e43426SChristoph Lameter 7531da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 7541da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 7551da177e4SLinus Torvalds sc->nr_scanned++; 7561da177e4SLinus Torvalds 757c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 758c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 759c661b078SAndy Whitcroft 760e62e384eSMichal Hocko /* 761e2be15f6SMel Gorman * The number of dirty pages determines if a zone is marked 762e2be15f6SMel Gorman * reclaim_congested which affects wait_iff_congested. kswapd 763e2be15f6SMel Gorman * will stall and start writing pages if the tail of the LRU 764e2be15f6SMel Gorman * is all dirty unqueued pages. 765e2be15f6SMel Gorman */ 766e2be15f6SMel Gorman page_check_dirty_writeback(page, &dirty, &writeback); 767e2be15f6SMel Gorman if (dirty || writeback) 768e2be15f6SMel Gorman nr_dirty++; 769e2be15f6SMel Gorman 770e2be15f6SMel Gorman if (dirty && !writeback) 771e2be15f6SMel Gorman nr_unqueued_dirty++; 772e2be15f6SMel Gorman 773d04e8acdSMel Gorman /* 774d04e8acdSMel Gorman * Treat this page as congested if the underlying BDI is or if 775d04e8acdSMel Gorman * pages are cycling through the LRU so quickly that the 776d04e8acdSMel Gorman * pages marked for immediate reclaim are making it to the 777d04e8acdSMel Gorman * end of the LRU a second time. 778d04e8acdSMel Gorman */ 779e2be15f6SMel Gorman mapping = page_mapping(page); 780d04e8acdSMel Gorman if ((mapping && bdi_write_congested(mapping->backing_dev_info)) || 781d04e8acdSMel Gorman (writeback && PageReclaim(page))) 782e2be15f6SMel Gorman nr_congested++; 783e2be15f6SMel Gorman 784e2be15f6SMel Gorman /* 785283aba9fSMel Gorman * If a page at the tail of the LRU is under writeback, there 786283aba9fSMel Gorman * are three cases to consider. 787e62e384eSMichal Hocko * 788283aba9fSMel Gorman * 1) If reclaim is encountering an excessive number of pages 789283aba9fSMel Gorman * under writeback and this page is both under writeback and 790283aba9fSMel Gorman * PageReclaim then it indicates that pages are being queued 791283aba9fSMel Gorman * for IO but are being recycled through the LRU before the 792283aba9fSMel Gorman * IO can complete. Waiting on the page itself risks an 793283aba9fSMel Gorman * indefinite stall if it is impossible to writeback the 794283aba9fSMel Gorman * page due to IO error or disconnected storage so instead 795b1a6f21eSMel Gorman * note that the LRU is being scanned too quickly and the 796b1a6f21eSMel Gorman * caller can stall after page list has been processed. 797c3b94f44SHugh Dickins * 798283aba9fSMel Gorman * 2) Global reclaim encounters a page, memcg encounters a 799283aba9fSMel Gorman * page that is not marked for immediate reclaim or 800283aba9fSMel Gorman * the caller does not have __GFP_IO. In this case mark 801283aba9fSMel Gorman * the page for immediate reclaim and continue scanning. 802283aba9fSMel Gorman * 803283aba9fSMel Gorman * __GFP_IO is checked because a loop driver thread might 804283aba9fSMel Gorman * enter reclaim, and deadlock if it waits on a page for 805283aba9fSMel Gorman * which it is needed to do the write (loop masks off 806283aba9fSMel Gorman * __GFP_IO|__GFP_FS for this reason); but more thought 807283aba9fSMel Gorman * would probably show more reasons. 808283aba9fSMel Gorman * 809283aba9fSMel Gorman * Don't require __GFP_FS, since we're not going into the 810283aba9fSMel Gorman * FS, just waiting on its writeback completion. Worryingly, 811283aba9fSMel Gorman * ext4 gfs2 and xfs allocate pages with 812283aba9fSMel Gorman * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so testing 813283aba9fSMel Gorman * may_enter_fs here is liable to OOM on them. 814283aba9fSMel Gorman * 815283aba9fSMel Gorman * 3) memcg encounters a page that is not already marked 816283aba9fSMel Gorman * PageReclaim. memcg does not have any dirty pages 817283aba9fSMel Gorman * throttling so we could easily OOM just because too many 818283aba9fSMel Gorman * pages are in writeback and there is nothing else to 819283aba9fSMel Gorman * reclaim. Wait for the writeback to complete. 820e62e384eSMichal Hocko */ 821283aba9fSMel Gorman if (PageWriteback(page)) { 822283aba9fSMel Gorman /* Case 1 above */ 823283aba9fSMel Gorman if (current_is_kswapd() && 824283aba9fSMel Gorman PageReclaim(page) && 825283aba9fSMel Gorman zone_is_reclaim_writeback(zone)) { 826b1a6f21eSMel Gorman nr_immediate++; 827b1a6f21eSMel Gorman goto keep_locked; 828283aba9fSMel Gorman 829283aba9fSMel Gorman /* Case 2 above */ 830283aba9fSMel Gorman } else if (global_reclaim(sc) || 831c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 832c3b94f44SHugh Dickins /* 833c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 834c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 835c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 836c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 837c3b94f44SHugh Dickins * enough to care. What we do want is for this 838c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 839c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 840c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 841c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 842c3b94f44SHugh Dickins */ 843c3b94f44SHugh Dickins SetPageReclaim(page); 84492df3a72SMel Gorman nr_writeback++; 845283aba9fSMel Gorman 846c3b94f44SHugh Dickins goto keep_locked; 847283aba9fSMel Gorman 848283aba9fSMel Gorman /* Case 3 above */ 849283aba9fSMel Gorman } else { 850c3b94f44SHugh Dickins wait_on_page_writeback(page); 851e62e384eSMichal Hocko } 852283aba9fSMel Gorman } 8531da177e4SLinus Torvalds 85402c6de8dSMinchan Kim if (!force_reclaim) 8556a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 85602c6de8dSMinchan Kim 857dfc8d636SJohannes Weiner switch (references) { 858dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 8591da177e4SLinus Torvalds goto activate_locked; 86064574746SJohannes Weiner case PAGEREF_KEEP: 86164574746SJohannes Weiner goto keep_locked; 862dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 863dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 864dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 865dfc8d636SJohannes Weiner } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds /* 8681da177e4SLinus Torvalds * Anonymous process memory has backing store? 8691da177e4SLinus Torvalds * Try to allocate it some swap space here. 8701da177e4SLinus Torvalds */ 871b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 87263eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 87363eb6b93SHugh Dickins goto keep_locked; 8745bc7b8acSShaohua Li if (!add_to_swap(page, page_list)) 8751da177e4SLinus Torvalds goto activate_locked; 87663eb6b93SHugh Dickins may_enter_fs = 1; 8771da177e4SLinus Torvalds 878e2be15f6SMel Gorman /* Adding to swap updated mapping */ 8791da177e4SLinus Torvalds mapping = page_mapping(page); 880e2be15f6SMel Gorman } 8811da177e4SLinus Torvalds 8821da177e4SLinus Torvalds /* 8831da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 8841da177e4SLinus Torvalds * processes. Try to unmap it here. 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 88702c6de8dSMinchan Kim switch (try_to_unmap(page, ttu_flags)) { 8881da177e4SLinus Torvalds case SWAP_FAIL: 8891da177e4SLinus Torvalds goto activate_locked; 8901da177e4SLinus Torvalds case SWAP_AGAIN: 8911da177e4SLinus Torvalds goto keep_locked; 892b291f000SNick Piggin case SWAP_MLOCK: 893b291f000SNick Piggin goto cull_mlocked; 8941da177e4SLinus Torvalds case SWAP_SUCCESS: 8951da177e4SLinus Torvalds ; /* try to free the page below */ 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds if (PageDirty(page)) { 900ee72886dSMel Gorman /* 901ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 902d43006d5SMel Gorman * avoid risk of stack overflow but only writeback 903d43006d5SMel Gorman * if many dirty pages have been encountered. 904ee72886dSMel Gorman */ 905f84f6e2bSMel Gorman if (page_is_file_cache(page) && 9069e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 907d43006d5SMel Gorman !zone_is_reclaim_dirty(zone))) { 90849ea7eb6SMel Gorman /* 90949ea7eb6SMel Gorman * Immediately reclaim when written back. 91049ea7eb6SMel Gorman * Similar in principal to deactivate_page() 91149ea7eb6SMel Gorman * except we already have the page isolated 91249ea7eb6SMel Gorman * and know it's dirty 91349ea7eb6SMel Gorman */ 91449ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 91549ea7eb6SMel Gorman SetPageReclaim(page); 91649ea7eb6SMel Gorman 917ee72886dSMel Gorman goto keep_locked; 918ee72886dSMel Gorman } 919ee72886dSMel Gorman 920dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 9211da177e4SLinus Torvalds goto keep_locked; 9224dd4b920SAndrew Morton if (!may_enter_fs) 9231da177e4SLinus Torvalds goto keep_locked; 92452a8363eSChristoph Lameter if (!sc->may_writepage) 9251da177e4SLinus Torvalds goto keep_locked; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 9287d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 9291da177e4SLinus Torvalds case PAGE_KEEP: 9301da177e4SLinus Torvalds goto keep_locked; 9311da177e4SLinus Torvalds case PAGE_ACTIVATE: 9321da177e4SLinus Torvalds goto activate_locked; 9331da177e4SLinus Torvalds case PAGE_SUCCESS: 9347d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 93541ac1999SMel Gorman goto keep; 9367d3579e8SKOSAKI Motohiro if (PageDirty(page)) 9371da177e4SLinus Torvalds goto keep; 9387d3579e8SKOSAKI Motohiro 9391da177e4SLinus Torvalds /* 9401da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 9411da177e4SLinus Torvalds * ahead and try to reclaim the page. 9421da177e4SLinus Torvalds */ 943529ae9aaSNick Piggin if (!trylock_page(page)) 9441da177e4SLinus Torvalds goto keep; 9451da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 9461da177e4SLinus Torvalds goto keep_locked; 9471da177e4SLinus Torvalds mapping = page_mapping(page); 9481da177e4SLinus Torvalds case PAGE_CLEAN: 9491da177e4SLinus Torvalds ; /* try to free the page below */ 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 9541da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 9551da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 9561da177e4SLinus Torvalds * the page as well. 9571da177e4SLinus Torvalds * 9581da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 9591da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 9601da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 9611da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 9621da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 9631da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 9641da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 9651da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 9661da177e4SLinus Torvalds * 9671da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 9681da177e4SLinus Torvalds * the pages which were not successfully invalidated in 9691da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 9701da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 9711da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 9721da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 9731da177e4SLinus Torvalds */ 974266cf658SDavid Howells if (page_has_private(page)) { 9751da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 9761da177e4SLinus Torvalds goto activate_locked; 977e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 978e286781dSNick Piggin unlock_page(page); 979e286781dSNick Piggin if (put_page_testzero(page)) 9801da177e4SLinus Torvalds goto free_it; 981e286781dSNick Piggin else { 982e286781dSNick Piggin /* 983e286781dSNick Piggin * rare race with speculative reference. 984e286781dSNick Piggin * the speculative reference will free 985e286781dSNick Piggin * this page shortly, so we may 986e286781dSNick Piggin * increment nr_reclaimed here (and 987e286781dSNick Piggin * leave it off the LRU). 988e286781dSNick Piggin */ 989e286781dSNick Piggin nr_reclaimed++; 990e286781dSNick Piggin continue; 991e286781dSNick Piggin } 992e286781dSNick Piggin } 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 995e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 99649d2e9ccSChristoph Lameter goto keep_locked; 9971da177e4SLinus Torvalds 998a978d6f5SNick Piggin /* 999a978d6f5SNick Piggin * At this point, we have no other references and there is 1000a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 1001a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 1002a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 1003a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 1004a978d6f5SNick Piggin */ 1005a978d6f5SNick Piggin __clear_page_locked(page); 1006e286781dSNick Piggin free_it: 100705ff5137SAndrew Morton nr_reclaimed++; 1008abe4c3b5SMel Gorman 1009abe4c3b5SMel Gorman /* 1010abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 1011abe4c3b5SMel Gorman * appear not as the counts should be low 1012abe4c3b5SMel Gorman */ 1013abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 10141da177e4SLinus Torvalds continue; 10151da177e4SLinus Torvalds 1016b291f000SNick Piggin cull_mlocked: 101763d6c5adSHugh Dickins if (PageSwapCache(page)) 101863d6c5adSHugh Dickins try_to_free_swap(page); 1019b291f000SNick Piggin unlock_page(page); 1020b291f000SNick Piggin putback_lru_page(page); 1021b291f000SNick Piggin continue; 1022b291f000SNick Piggin 10231da177e4SLinus Torvalds activate_locked: 102468a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 102568a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 1026a2c43eedSHugh Dickins try_to_free_swap(page); 1027894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 10281da177e4SLinus Torvalds SetPageActive(page); 10291da177e4SLinus Torvalds pgactivate++; 10301da177e4SLinus Torvalds keep_locked: 10311da177e4SLinus Torvalds unlock_page(page); 10321da177e4SLinus Torvalds keep: 10331da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 1034b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 10351da177e4SLinus Torvalds } 1036abe4c3b5SMel Gorman 1037cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 1038abe4c3b5SMel Gorman 10391da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 1040f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 104169980e31STim Chen mem_cgroup_uncharge_end(); 10428e950282SMel Gorman *ret_nr_dirty += nr_dirty; 10438e950282SMel Gorman *ret_nr_congested += nr_congested; 1044d43006d5SMel Gorman *ret_nr_unqueued_dirty += nr_unqueued_dirty; 104592df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 1046b1a6f21eSMel Gorman *ret_nr_immediate += nr_immediate; 104705ff5137SAndrew Morton return nr_reclaimed; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 105002c6de8dSMinchan Kim unsigned long reclaim_clean_pages_from_list(struct zone *zone, 105102c6de8dSMinchan Kim struct list_head *page_list) 105202c6de8dSMinchan Kim { 105302c6de8dSMinchan Kim struct scan_control sc = { 105402c6de8dSMinchan Kim .gfp_mask = GFP_KERNEL, 105502c6de8dSMinchan Kim .priority = DEF_PRIORITY, 105602c6de8dSMinchan Kim .may_unmap = 1, 105702c6de8dSMinchan Kim }; 10588e950282SMel Gorman unsigned long ret, dummy1, dummy2, dummy3, dummy4, dummy5; 105902c6de8dSMinchan Kim struct page *page, *next; 106002c6de8dSMinchan Kim LIST_HEAD(clean_pages); 106102c6de8dSMinchan Kim 106202c6de8dSMinchan Kim list_for_each_entry_safe(page, next, page_list, lru) { 106302c6de8dSMinchan Kim if (page_is_file_cache(page) && !PageDirty(page)) { 106402c6de8dSMinchan Kim ClearPageActive(page); 106502c6de8dSMinchan Kim list_move(&page->lru, &clean_pages); 106602c6de8dSMinchan Kim } 106702c6de8dSMinchan Kim } 106802c6de8dSMinchan Kim 106902c6de8dSMinchan Kim ret = shrink_page_list(&clean_pages, zone, &sc, 107002c6de8dSMinchan Kim TTU_UNMAP|TTU_IGNORE_ACCESS, 10718e950282SMel Gorman &dummy1, &dummy2, &dummy3, &dummy4, &dummy5, true); 107202c6de8dSMinchan Kim list_splice(&clean_pages, page_list); 107302c6de8dSMinchan Kim __mod_zone_page_state(zone, NR_ISOLATED_FILE, -ret); 107402c6de8dSMinchan Kim return ret; 107502c6de8dSMinchan Kim } 107602c6de8dSMinchan Kim 10775ad333ebSAndy Whitcroft /* 10785ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 10795ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 10805ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 10815ad333ebSAndy Whitcroft * 10825ad333ebSAndy Whitcroft * page: page to consider 10835ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 10845ad333ebSAndy Whitcroft * 10855ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 10865ad333ebSAndy Whitcroft */ 1087f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 10885ad333ebSAndy Whitcroft { 10895ad333ebSAndy Whitcroft int ret = -EINVAL; 10905ad333ebSAndy Whitcroft 10915ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 10925ad333ebSAndy Whitcroft if (!PageLRU(page)) 10935ad333ebSAndy Whitcroft return ret; 10945ad333ebSAndy Whitcroft 1095e46a2879SMinchan Kim /* Compaction should not handle unevictable pages but CMA can do so */ 1096e46a2879SMinchan Kim if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE)) 1097894bc310SLee Schermerhorn return ret; 1098894bc310SLee Schermerhorn 10995ad333ebSAndy Whitcroft ret = -EBUSY; 110008e552c6SKAMEZAWA Hiroyuki 1101c8244935SMel Gorman /* 1102c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 1103c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 1104c8244935SMel Gorman * blocking - clean pages for the most part. 1105c8244935SMel Gorman * 1106c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 1107c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 1108c8244935SMel Gorman * 1109c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 1110c8244935SMel Gorman * that it is possible to migrate without blocking 1111c8244935SMel Gorman */ 1112c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 1113c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 1114c8244935SMel Gorman if (PageWriteback(page)) 111539deaf85SMinchan Kim return ret; 111639deaf85SMinchan Kim 1117c8244935SMel Gorman if (PageDirty(page)) { 1118c8244935SMel Gorman struct address_space *mapping; 1119c8244935SMel Gorman 1120c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1121c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1122c8244935SMel Gorman return ret; 1123c8244935SMel Gorman 1124c8244935SMel Gorman /* 1125c8244935SMel Gorman * Only pages without mappings or that have a 1126c8244935SMel Gorman * ->migratepage callback are possible to migrate 1127c8244935SMel Gorman * without blocking 1128c8244935SMel Gorman */ 1129c8244935SMel Gorman mapping = page_mapping(page); 1130c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1131c8244935SMel Gorman return ret; 1132c8244935SMel Gorman } 1133c8244935SMel Gorman } 1134c8244935SMel Gorman 1135f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1136f80c0673SMinchan Kim return ret; 1137f80c0673SMinchan Kim 11385ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 11395ad333ebSAndy Whitcroft /* 11405ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 11415ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 11425ad333ebSAndy Whitcroft * page release code relies on it. 11435ad333ebSAndy Whitcroft */ 11445ad333ebSAndy Whitcroft ClearPageLRU(page); 11455ad333ebSAndy Whitcroft ret = 0; 11465ad333ebSAndy Whitcroft } 11475ad333ebSAndy Whitcroft 11485ad333ebSAndy Whitcroft return ret; 11495ad333ebSAndy Whitcroft } 11505ad333ebSAndy Whitcroft 115149d2e9ccSChristoph Lameter /* 11521da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 11531da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 11541da177e4SLinus Torvalds * and working on them outside the LRU lock. 11551da177e4SLinus Torvalds * 11561da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 11571da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 11581da177e4SLinus Torvalds * 11591da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 11601da177e4SLinus Torvalds * 11611da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 11625dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 11631da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1164f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1165fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 11665ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 11673cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 11681da177e4SLinus Torvalds * 11691da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 11701da177e4SLinus Torvalds */ 117169e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 11725dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1173fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 11743cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 11751da177e4SLinus Torvalds { 117675b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 117769e05944SAndrew Morton unsigned long nr_taken = 0; 1178c9b02d97SWu Fengguang unsigned long scan; 11791da177e4SLinus Torvalds 1180c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 11815ad333ebSAndy Whitcroft struct page *page; 1182fa9add64SHugh Dickins int nr_pages; 11835ad333ebSAndy Whitcroft 11841da177e4SLinus Torvalds page = lru_to_page(src); 11851da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 11861da177e4SLinus Torvalds 1187725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 11888d438f96SNick Piggin 1189f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 11905ad333ebSAndy Whitcroft case 0: 1191fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1192fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 11935ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1194fa9add64SHugh Dickins nr_taken += nr_pages; 11955ad333ebSAndy Whitcroft break; 11967c8ee9a8SNick Piggin 11975ad333ebSAndy Whitcroft case -EBUSY: 11985ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 11995ad333ebSAndy Whitcroft list_move(&page->lru, src); 12005ad333ebSAndy Whitcroft continue; 12015ad333ebSAndy Whitcroft 12025ad333ebSAndy Whitcroft default: 12035ad333ebSAndy Whitcroft BUG(); 12045ad333ebSAndy Whitcroft } 12055ad333ebSAndy Whitcroft } 12061da177e4SLinus Torvalds 1207f626012dSHugh Dickins *nr_scanned = scan; 120875b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 120975b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 12101da177e4SLinus Torvalds return nr_taken; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 121362695a84SNick Piggin /** 121462695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 121562695a84SNick Piggin * @page: page to isolate from its LRU list 121662695a84SNick Piggin * 121762695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 121862695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 121962695a84SNick Piggin * 122062695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 122162695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 122262695a84SNick Piggin * 122362695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1224894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1225894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1226894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 122762695a84SNick Piggin * 122862695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 122962695a84SNick Piggin * found will be decremented. 123062695a84SNick Piggin * 123162695a84SNick Piggin * Restrictions: 123262695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 123362695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 123462695a84SNick Piggin * without a stable reference). 123562695a84SNick Piggin * (2) the lru_lock must not be held. 123662695a84SNick Piggin * (3) interrupts must be enabled. 123762695a84SNick Piggin */ 123862695a84SNick Piggin int isolate_lru_page(struct page *page) 123962695a84SNick Piggin { 124062695a84SNick Piggin int ret = -EBUSY; 124162695a84SNick Piggin 12420c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 12430c917313SKonstantin Khlebnikov 124462695a84SNick Piggin if (PageLRU(page)) { 124562695a84SNick Piggin struct zone *zone = page_zone(page); 1246fa9add64SHugh Dickins struct lruvec *lruvec; 124762695a84SNick Piggin 124862695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1249fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 12500c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1251894bc310SLee Schermerhorn int lru = page_lru(page); 12520c917313SKonstantin Khlebnikov get_page(page); 125362695a84SNick Piggin ClearPageLRU(page); 1254fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1255fa9add64SHugh Dickins ret = 0; 125662695a84SNick Piggin } 125762695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 125862695a84SNick Piggin } 125962695a84SNick Piggin return ret; 126062695a84SNick Piggin } 126162695a84SNick Piggin 12625ad333ebSAndy Whitcroft /* 1263d37dd5dcSFengguang Wu * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and 1264d37dd5dcSFengguang Wu * then get resheduled. When there are massive number of tasks doing page 1265d37dd5dcSFengguang Wu * allocation, such sleeping direct reclaimers may keep piling up on each CPU, 1266d37dd5dcSFengguang Wu * the LRU list will go small and be scanned faster than necessary, leading to 1267d37dd5dcSFengguang Wu * unnecessary swapping, thrashing and OOM. 126835cd7815SRik van Riel */ 126935cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 127035cd7815SRik van Riel struct scan_control *sc) 127135cd7815SRik van Riel { 127235cd7815SRik van Riel unsigned long inactive, isolated; 127335cd7815SRik van Riel 127435cd7815SRik van Riel if (current_is_kswapd()) 127535cd7815SRik van Riel return 0; 127635cd7815SRik van Riel 127789b5fae5SJohannes Weiner if (!global_reclaim(sc)) 127835cd7815SRik van Riel return 0; 127935cd7815SRik van Riel 128035cd7815SRik van Riel if (file) { 128135cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 128235cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 128335cd7815SRik van Riel } else { 128435cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 128535cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 128635cd7815SRik van Riel } 128735cd7815SRik van Riel 12883cf23841SFengguang Wu /* 12893cf23841SFengguang Wu * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they 12903cf23841SFengguang Wu * won't get blocked by normal direct-reclaimers, forming a circular 12913cf23841SFengguang Wu * deadlock. 12923cf23841SFengguang Wu */ 12933cf23841SFengguang Wu if ((sc->gfp_mask & GFP_IOFS) == GFP_IOFS) 12943cf23841SFengguang Wu inactive >>= 3; 12953cf23841SFengguang Wu 129635cd7815SRik van Riel return isolated > inactive; 129735cd7815SRik van Riel } 129835cd7815SRik van Riel 129966635629SMel Gorman static noinline_for_stack void 130075b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 130166635629SMel Gorman { 130227ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 130327ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13043f79768fSHugh Dickins LIST_HEAD(pages_to_free); 130566635629SMel Gorman 130666635629SMel Gorman /* 130766635629SMel Gorman * Put back any unfreeable pages. 130866635629SMel Gorman */ 130966635629SMel Gorman while (!list_empty(page_list)) { 13103f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 131166635629SMel Gorman int lru; 13123f79768fSHugh Dickins 131366635629SMel Gorman VM_BUG_ON(PageLRU(page)); 131466635629SMel Gorman list_del(&page->lru); 131539b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 131666635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 131766635629SMel Gorman putback_lru_page(page); 131866635629SMel Gorman spin_lock_irq(&zone->lru_lock); 131966635629SMel Gorman continue; 132066635629SMel Gorman } 1321fa9add64SHugh Dickins 1322fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1323fa9add64SHugh Dickins 13247a608572SLinus Torvalds SetPageLRU(page); 132566635629SMel Gorman lru = page_lru(page); 1326fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1327fa9add64SHugh Dickins 132866635629SMel Gorman if (is_active_lru(lru)) { 132966635629SMel Gorman int file = is_file_lru(lru); 13309992af10SRik van Riel int numpages = hpage_nr_pages(page); 13319992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 133266635629SMel Gorman } 13332bcf8879SHugh Dickins if (put_page_testzero(page)) { 13342bcf8879SHugh Dickins __ClearPageLRU(page); 13352bcf8879SHugh Dickins __ClearPageActive(page); 1336fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 13372bcf8879SHugh Dickins 13382bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 133966635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 13402bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 134166635629SMel Gorman spin_lock_irq(&zone->lru_lock); 13422bcf8879SHugh Dickins } else 13432bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 134466635629SMel Gorman } 134566635629SMel Gorman } 134666635629SMel Gorman 13473f79768fSHugh Dickins /* 13483f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 13493f79768fSHugh Dickins */ 13503f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 135166635629SMel Gorman } 135266635629SMel Gorman 135366635629SMel Gorman /* 13541742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 13551742f19fSAndrew Morton * of reclaimed pages 13561da177e4SLinus Torvalds */ 135766635629SMel Gorman static noinline_for_stack unsigned long 13581a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 13599e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 13601da177e4SLinus Torvalds { 13611da177e4SLinus Torvalds LIST_HEAD(page_list); 1362e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 136305ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1364e247dbceSKOSAKI Motohiro unsigned long nr_taken; 13658e950282SMel Gorman unsigned long nr_dirty = 0; 13668e950282SMel Gorman unsigned long nr_congested = 0; 1367e2be15f6SMel Gorman unsigned long nr_unqueued_dirty = 0; 136892df3a72SMel Gorman unsigned long nr_writeback = 0; 1369b1a6f21eSMel Gorman unsigned long nr_immediate = 0; 1370f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 13713cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 13721a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13731a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 137478dc583dSKOSAKI Motohiro 137535cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 137658355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 137735cd7815SRik van Riel 137835cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 137935cd7815SRik van Riel if (fatal_signal_pending(current)) 138035cd7815SRik van Riel return SWAP_CLUSTER_MAX; 138135cd7815SRik van Riel } 138235cd7815SRik van Riel 13831da177e4SLinus Torvalds lru_add_drain(); 1384f80c0673SMinchan Kim 1385f80c0673SMinchan Kim if (!sc->may_unmap) 138661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1387f80c0673SMinchan Kim if (!sc->may_writepage) 138861317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1389f80c0673SMinchan Kim 13901da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 13911da177e4SLinus Torvalds 13925dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 13935dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 139495d918fcSKonstantin Khlebnikov 139595d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 139695d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 139795d918fcSKonstantin Khlebnikov 139889b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1399e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1400b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 140175b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1402b35ea17bSKOSAKI Motohiro else 140375b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1404b35ea17bSKOSAKI Motohiro } 140566635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1406d563c050SHillf Danton 1407d563c050SHillf Danton if (nr_taken == 0) 140866635629SMel Gorman return 0; 1409b35ea17bSKOSAKI Motohiro 141002c6de8dSMinchan Kim nr_reclaimed = shrink_page_list(&page_list, zone, sc, TTU_UNMAP, 14118e950282SMel Gorman &nr_dirty, &nr_unqueued_dirty, &nr_congested, 14128e950282SMel Gorman &nr_writeback, &nr_immediate, 1413b1a6f21eSMel Gorman false); 1414c661b078SAndy Whitcroft 14153f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 14163f79768fSHugh Dickins 141795d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1418d563c050SHillf Danton 1419904249aaSYing Han if (global_reclaim(sc)) { 1420b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1421904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1422904249aaSYing Han nr_reclaimed); 1423904249aaSYing Han else 1424904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1425904249aaSYing Han nr_reclaimed); 1426904249aaSYing Han } 1427a74609faSNick Piggin 142827ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 14293f79768fSHugh Dickins 143095d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 14313f79768fSHugh Dickins 14323f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 14333f79768fSHugh Dickins 14343f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1435e11da5b4SMel Gorman 143692df3a72SMel Gorman /* 143792df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 143892df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 143992df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 144092df3a72SMel Gorman * at throttling processes due to the page distribution throughout 144192df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 144292df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 144392df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 144492df3a72SMel Gorman * same way balance_dirty_pages() manages. 144592df3a72SMel Gorman * 14468e950282SMel Gorman * Once a zone is flagged ZONE_WRITEBACK, kswapd will count the number 14478e950282SMel Gorman * of pages under pages flagged for immediate reclaim and stall if any 14488e950282SMel Gorman * are encountered in the nr_immediate check below. 144992df3a72SMel Gorman */ 1450*918fc718SMel Gorman if (nr_writeback && nr_writeback == nr_taken) 1451283aba9fSMel Gorman zone_set_flag(zone, ZONE_WRITEBACK); 145292df3a72SMel Gorman 1453d43006d5SMel Gorman /* 1454b1a6f21eSMel Gorman * memcg will stall in page writeback so only consider forcibly 1455b1a6f21eSMel Gorman * stalling for global reclaim 1456d43006d5SMel Gorman */ 1457b1a6f21eSMel Gorman if (global_reclaim(sc)) { 1458b1a6f21eSMel Gorman /* 14598e950282SMel Gorman * Tag a zone as congested if all the dirty pages scanned were 14608e950282SMel Gorman * backed by a congested BDI and wait_iff_congested will stall. 14618e950282SMel Gorman */ 14628e950282SMel Gorman if (nr_dirty && nr_dirty == nr_congested) 14638e950282SMel Gorman zone_set_flag(zone, ZONE_CONGESTED); 14648e950282SMel Gorman 14658e950282SMel Gorman /* 1466b1a6f21eSMel Gorman * If dirty pages are scanned that are not queued for IO, it 1467b1a6f21eSMel Gorman * implies that flushers are not keeping up. In this case, flag 1468b1a6f21eSMel Gorman * the zone ZONE_TAIL_LRU_DIRTY and kswapd will start writing 1469b1a6f21eSMel Gorman * pages from reclaim context. It will forcibly stall in the 1470b1a6f21eSMel Gorman * next check. 1471b1a6f21eSMel Gorman */ 1472b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken) 1473d43006d5SMel Gorman zone_set_flag(zone, ZONE_TAIL_LRU_DIRTY); 1474b1a6f21eSMel Gorman 1475b1a6f21eSMel Gorman /* 1476b1a6f21eSMel Gorman * In addition, if kswapd scans pages marked marked for 1477b1a6f21eSMel Gorman * immediate reclaim and under writeback (nr_immediate), it 1478b1a6f21eSMel Gorman * implies that pages are cycling through the LRU faster than 1479b1a6f21eSMel Gorman * they are written so also forcibly stall. 1480b1a6f21eSMel Gorman */ 1481b1a6f21eSMel Gorman if (nr_unqueued_dirty == nr_taken || nr_immediate) 1482b1a6f21eSMel Gorman congestion_wait(BLK_RW_ASYNC, HZ/10); 1483e2be15f6SMel Gorman } 1484d43006d5SMel Gorman 14858e950282SMel Gorman /* 14868e950282SMel Gorman * Stall direct reclaim for IO completions if underlying BDIs or zone 14878e950282SMel Gorman * is congested. Allow kswapd to continue until it starts encountering 14888e950282SMel Gorman * unqueued dirty pages or cycling through the LRU too quickly. 14898e950282SMel Gorman */ 14908e950282SMel Gorman if (!sc->hibernation_mode && !current_is_kswapd()) 14918e950282SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 14928e950282SMel Gorman 1493e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1494e11da5b4SMel Gorman zone_idx(zone), 1495e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 14969e3b2f8cSKonstantin Khlebnikov sc->priority, 149723b9da55SMel Gorman trace_shrink_flags(file)); 149805ff5137SAndrew Morton return nr_reclaimed; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15013bb1a852SMartin Bligh /* 15021cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 15031cfb419bSKAMEZAWA Hiroyuki * 15041cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 15051cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 15061cfb419bSKAMEZAWA Hiroyuki * 15071cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 15081cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 15091cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 15101cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 15111cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 15121cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 15131cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 15141cfb419bSKAMEZAWA Hiroyuki * 15151cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 15161cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 15171cfb419bSKAMEZAWA Hiroyuki */ 15181cfb419bSKAMEZAWA Hiroyuki 1519fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 15203eb4140fSWu Fengguang struct list_head *list, 15212bcf8879SHugh Dickins struct list_head *pages_to_free, 15223eb4140fSWu Fengguang enum lru_list lru) 15233eb4140fSWu Fengguang { 1524fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 15253eb4140fSWu Fengguang unsigned long pgmoved = 0; 15263eb4140fSWu Fengguang struct page *page; 1527fa9add64SHugh Dickins int nr_pages; 15283eb4140fSWu Fengguang 15293eb4140fSWu Fengguang while (!list_empty(list)) { 15303eb4140fSWu Fengguang page = lru_to_page(list); 1531fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 15323eb4140fSWu Fengguang 15333eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 15343eb4140fSWu Fengguang SetPageLRU(page); 15353eb4140fSWu Fengguang 1536fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1537fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1538925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1539fa9add64SHugh Dickins pgmoved += nr_pages; 15403eb4140fSWu Fengguang 15412bcf8879SHugh Dickins if (put_page_testzero(page)) { 15422bcf8879SHugh Dickins __ClearPageLRU(page); 15432bcf8879SHugh Dickins __ClearPageActive(page); 1544fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 15452bcf8879SHugh Dickins 15462bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 15473eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 15482bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 15493eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 15502bcf8879SHugh Dickins } else 15512bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 15523eb4140fSWu Fengguang } 15533eb4140fSWu Fengguang } 15543eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 15553eb4140fSWu Fengguang if (!is_active_lru(lru)) 15563eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 15573eb4140fSWu Fengguang } 15581cfb419bSKAMEZAWA Hiroyuki 1559f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 15601a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1561f16015fbSJohannes Weiner struct scan_control *sc, 15629e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 15631cfb419bSKAMEZAWA Hiroyuki { 156444c241f1SKOSAKI Motohiro unsigned long nr_taken; 1565f626012dSHugh Dickins unsigned long nr_scanned; 15666fe6b7e3SWu Fengguang unsigned long vm_flags; 15671cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 15688cab4754SWu Fengguang LIST_HEAD(l_active); 1569b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 15701cfb419bSKAMEZAWA Hiroyuki struct page *page; 15711a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 157244c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1573f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 15743cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 15751a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 15761cfb419bSKAMEZAWA Hiroyuki 15771da177e4SLinus Torvalds lru_add_drain(); 1578f80c0673SMinchan Kim 1579f80c0673SMinchan Kim if (!sc->may_unmap) 158061317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1581f80c0673SMinchan Kim if (!sc->may_writepage) 158261317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1583f80c0673SMinchan Kim 15841da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1585925b7673SJohannes Weiner 15865dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 15875dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 158889b5fae5SJohannes Weiner if (global_reclaim(sc)) 1589f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 159089b5fae5SJohannes Weiner 1591b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 15921cfb419bSKAMEZAWA Hiroyuki 1593f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 15943cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1595a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 15961da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 15991da177e4SLinus Torvalds cond_resched(); 16001da177e4SLinus Torvalds page = lru_to_page(&l_hold); 16011da177e4SLinus Torvalds list_del(&page->lru); 16027e9cd484SRik van Riel 160339b5f29aSHugh Dickins if (unlikely(!page_evictable(page))) { 1604894bc310SLee Schermerhorn putback_lru_page(page); 1605894bc310SLee Schermerhorn continue; 1606894bc310SLee Schermerhorn } 1607894bc310SLee Schermerhorn 1608cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1609cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1610cc715d99SMel Gorman if (page_has_private(page)) 1611cc715d99SMel Gorman try_to_release_page(page, 0); 1612cc715d99SMel Gorman unlock_page(page); 1613cc715d99SMel Gorman } 1614cc715d99SMel Gorman } 1615cc715d99SMel Gorman 1616c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1617c3ac9a8aSJohannes Weiner &vm_flags)) { 16189992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 16198cab4754SWu Fengguang /* 16208cab4754SWu Fengguang * Identify referenced, file-backed active pages and 16218cab4754SWu Fengguang * give them one more trip around the active list. So 16228cab4754SWu Fengguang * that executable code get better chances to stay in 16238cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 16248cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 16258cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 16268cab4754SWu Fengguang * so we ignore them here. 16278cab4754SWu Fengguang */ 162841e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 16298cab4754SWu Fengguang list_add(&page->lru, &l_active); 16308cab4754SWu Fengguang continue; 16318cab4754SWu Fengguang } 16328cab4754SWu Fengguang } 16337e9cd484SRik van Riel 16345205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 16351da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 16361da177e4SLinus Torvalds } 16371da177e4SLinus Torvalds 1638b555749aSAndrew Morton /* 16398cab4754SWu Fengguang * Move pages back to the lru list. 1640b555749aSAndrew Morton */ 16412a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 16424f98a2feSRik van Riel /* 16438cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 16448cab4754SWu Fengguang * even though only some of them are actually re-activated. This 16458cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 16468cab4754SWu Fengguang * get_scan_ratio. 1647556adecbSRik van Riel */ 1648b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1649556adecbSRik van Riel 1650fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1651fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1652a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1653f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 16542bcf8879SHugh Dickins 16552bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 165874e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 165914797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1660f89eb90eSKOSAKI Motohiro { 1661f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1662f89eb90eSKOSAKI Motohiro 1663f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1664f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1665f89eb90eSKOSAKI Motohiro 1666f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1667f89eb90eSKOSAKI Motohiro return 1; 1668f89eb90eSKOSAKI Motohiro 1669f89eb90eSKOSAKI Motohiro return 0; 1670f89eb90eSKOSAKI Motohiro } 1671f89eb90eSKOSAKI Motohiro 167214797e23SKOSAKI Motohiro /** 167314797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1674c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 167514797e23SKOSAKI Motohiro * 167614797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 167714797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 167814797e23SKOSAKI Motohiro */ 1679c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 168014797e23SKOSAKI Motohiro { 168174e3f3c3SMinchan Kim /* 168274e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 168374e3f3c3SMinchan Kim * is pointless. 168474e3f3c3SMinchan Kim */ 168574e3f3c3SMinchan Kim if (!total_swap_pages) 168674e3f3c3SMinchan Kim return 0; 168774e3f3c3SMinchan Kim 1688c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1689c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1690f16015fbSJohannes Weiner 1691c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 169214797e23SKOSAKI Motohiro } 169374e3f3c3SMinchan Kim #else 1694c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 169574e3f3c3SMinchan Kim { 169674e3f3c3SMinchan Kim return 0; 169774e3f3c3SMinchan Kim } 169874e3f3c3SMinchan Kim #endif 169914797e23SKOSAKI Motohiro 170056e49d21SRik van Riel /** 170156e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1702c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 170356e49d21SRik van Riel * 170456e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 170556e49d21SRik van Riel * ensures that active file pages get deactivated, until more 170656e49d21SRik van Riel * than half of the file pages are on the inactive list. 170756e49d21SRik van Riel * 170856e49d21SRik van Riel * Once we get to that situation, protect the system's working 170956e49d21SRik van Riel * set from being evicted by disabling active file page aging. 171056e49d21SRik van Riel * 171156e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 171256e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 171356e49d21SRik van Riel */ 1714c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 171556e49d21SRik van Riel { 1716e3790144SJohannes Weiner unsigned long inactive; 1717e3790144SJohannes Weiner unsigned long active; 171856e49d21SRik van Riel 1719e3790144SJohannes Weiner inactive = get_lru_size(lruvec, LRU_INACTIVE_FILE); 1720e3790144SJohannes Weiner active = get_lru_size(lruvec, LRU_ACTIVE_FILE); 1721e3790144SJohannes Weiner 1722e3790144SJohannes Weiner return active > inactive; 172356e49d21SRik van Riel } 172456e49d21SRik van Riel 172575b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1726b39415b2SRik van Riel { 172775b00af7SHugh Dickins if (is_file_lru(lru)) 1728c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1729b39415b2SRik van Riel else 1730c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1731b39415b2SRik van Riel } 1732b39415b2SRik van Riel 17334f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 17341a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1735b69408e8SChristoph Lameter { 1736b39415b2SRik van Riel if (is_active_lru(lru)) { 173775b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 17381a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1739556adecbSRik van Riel return 0; 1740556adecbSRik van Riel } 1741556adecbSRik van Riel 17421a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1743b69408e8SChristoph Lameter } 1744b69408e8SChristoph Lameter 17453d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 17461f4c025bSKAMEZAWA Hiroyuki { 174789b5fae5SJohannes Weiner if (global_reclaim(sc)) 17481f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 17493d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 17501f4c025bSKAMEZAWA Hiroyuki } 17511f4c025bSKAMEZAWA Hiroyuki 17529a265114SJohannes Weiner enum scan_balance { 17539a265114SJohannes Weiner SCAN_EQUAL, 17549a265114SJohannes Weiner SCAN_FRACT, 17559a265114SJohannes Weiner SCAN_ANON, 17569a265114SJohannes Weiner SCAN_FILE, 17579a265114SJohannes Weiner }; 17589a265114SJohannes Weiner 17591da177e4SLinus Torvalds /* 17604f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 17614f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 17624f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 17634f98a2feSRik van Riel * onto the active list instead of evict. 17644f98a2feSRik van Riel * 1765be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1766be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 17674f98a2feSRik van Riel */ 176890126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 17699e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 17704f98a2feSRik van Riel { 177190126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 17729a265114SJohannes Weiner u64 fraction[2]; 17739a265114SJohannes Weiner u64 denominator = 0; /* gcc */ 177490126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 17759a265114SJohannes Weiner unsigned long anon_prio, file_prio; 17769a265114SJohannes Weiner enum scan_balance scan_balance; 17779a265114SJohannes Weiner unsigned long anon, file, free; 17789a265114SJohannes Weiner bool force_scan = false; 17799a265114SJohannes Weiner unsigned long ap, fp; 17809a265114SJohannes Weiner enum lru_list lru; 1781246e87a9SKAMEZAWA Hiroyuki 1782f11c0ca5SJohannes Weiner /* 1783f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1784f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1785f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1786f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1787f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1788f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1789f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1790f11c0ca5SJohannes Weiner * well. 1791f11c0ca5SJohannes Weiner */ 179290126375SKonstantin Khlebnikov if (current_is_kswapd() && zone->all_unreclaimable) 1793a4d3e9e7SJohannes Weiner force_scan = true; 179489b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1795a4d3e9e7SJohannes Weiner force_scan = true; 179676a33fc3SShaohua Li 179776a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 1798ec8acf20SShaohua Li if (!sc->may_swap || (get_nr_swap_pages() <= 0)) { 17999a265114SJohannes Weiner scan_balance = SCAN_FILE; 180076a33fc3SShaohua Li goto out; 180176a33fc3SShaohua Li } 18024f98a2feSRik van Riel 180310316b31SJohannes Weiner /* 180410316b31SJohannes Weiner * Global reclaim will swap to prevent OOM even with no 180510316b31SJohannes Weiner * swappiness, but memcg users want to use this knob to 180610316b31SJohannes Weiner * disable swapping for individual groups completely when 180710316b31SJohannes Weiner * using the memory controller's swap limit feature would be 180810316b31SJohannes Weiner * too expensive. 180910316b31SJohannes Weiner */ 181010316b31SJohannes Weiner if (!global_reclaim(sc) && !vmscan_swappiness(sc)) { 18119a265114SJohannes Weiner scan_balance = SCAN_FILE; 181210316b31SJohannes Weiner goto out; 181310316b31SJohannes Weiner } 181410316b31SJohannes Weiner 181510316b31SJohannes Weiner /* 181610316b31SJohannes Weiner * Do not apply any pressure balancing cleverness when the 181710316b31SJohannes Weiner * system is close to OOM, scan both anon and file equally 181810316b31SJohannes Weiner * (unless the swappiness setting disagrees with swapping). 181910316b31SJohannes Weiner */ 182010316b31SJohannes Weiner if (!sc->priority && vmscan_swappiness(sc)) { 18219a265114SJohannes Weiner scan_balance = SCAN_EQUAL; 182210316b31SJohannes Weiner goto out; 182310316b31SJohannes Weiner } 182410316b31SJohannes Weiner 18254d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 18264d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 18274d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 18284d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1829a4d3e9e7SJohannes Weiner 183011d16c25SJohannes Weiner /* 183111d16c25SJohannes Weiner * If it's foreseeable that reclaiming the file cache won't be 183211d16c25SJohannes Weiner * enough to get the zone back into a desirable shape, we have 183311d16c25SJohannes Weiner * to swap. Better start now and leave the - probably heavily 183411d16c25SJohannes Weiner * thrashing - remaining file pages alone. 183511d16c25SJohannes Weiner */ 183689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 183790126375SKonstantin Khlebnikov free = zone_page_state(zone, NR_FREE_PAGES); 183890126375SKonstantin Khlebnikov if (unlikely(file + free <= high_wmark_pages(zone))) { 18399a265114SJohannes Weiner scan_balance = SCAN_ANON; 184076a33fc3SShaohua Li goto out; 18417c5bd705SJohannes Weiner } 18427c5bd705SJohannes Weiner } 18437c5bd705SJohannes Weiner 1844e9868505SRik van Riel /* 18457c5bd705SJohannes Weiner * There is enough inactive page cache, do not reclaim 18467c5bd705SJohannes Weiner * anything from the anonymous working set right now. 1847e9868505SRik van Riel */ 18487c5bd705SJohannes Weiner if (!inactive_file_is_low(lruvec)) { 18499a265114SJohannes Weiner scan_balance = SCAN_FILE; 1850e9868505SRik van Riel goto out; 18514f98a2feSRik van Riel } 18524f98a2feSRik van Riel 18539a265114SJohannes Weiner scan_balance = SCAN_FRACT; 18549a265114SJohannes Weiner 18554f98a2feSRik van Riel /* 185658c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 185758c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 185858c37f6eSKOSAKI Motohiro */ 18593d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 186075b00af7SHugh Dickins file_prio = 200 - anon_prio; 186158c37f6eSKOSAKI Motohiro 186258c37f6eSKOSAKI Motohiro /* 18634f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 18644f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 18654f98a2feSRik van Riel * ratios to determine how valuable each cache is. 18664f98a2feSRik van Riel * 18674f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 18684f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 18694f98a2feSRik van Riel * up weighing recent references more than old ones. 18704f98a2feSRik van Riel * 18714f98a2feSRik van Riel * anon in [0], file in [1] 18724f98a2feSRik van Riel */ 187390126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 187458c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 18756e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 18766e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 18774f98a2feSRik van Riel } 18784f98a2feSRik van Riel 18796e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 18806e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 18816e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 18824f98a2feSRik van Riel } 18834f98a2feSRik van Riel 18844f98a2feSRik van Riel /* 188500d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 188600d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 188700d8089cSRik van Riel * each list that were recently referenced and in active use. 18884f98a2feSRik van Riel */ 1889fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 18906e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 18914f98a2feSRik van Riel 1892fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 18936e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 189490126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 18954f98a2feSRik van Riel 189676a33fc3SShaohua Li fraction[0] = ap; 189776a33fc3SShaohua Li fraction[1] = fp; 189876a33fc3SShaohua Li denominator = ap + fp + 1; 189976a33fc3SShaohua Li out: 19004111304dSHugh Dickins for_each_evictable_lru(lru) { 19014111304dSHugh Dickins int file = is_file_lru(lru); 1902d778df51SJohannes Weiner unsigned long size; 190376a33fc3SShaohua Li unsigned long scan; 190476a33fc3SShaohua Li 1905d778df51SJohannes Weiner size = get_lru_size(lruvec, lru); 1906d778df51SJohannes Weiner scan = size >> sc->priority; 19079a265114SJohannes Weiner 1908f11c0ca5SJohannes Weiner if (!scan && force_scan) 1909d778df51SJohannes Weiner scan = min(size, SWAP_CLUSTER_MAX); 19109a265114SJohannes Weiner 19119a265114SJohannes Weiner switch (scan_balance) { 19129a265114SJohannes Weiner case SCAN_EQUAL: 19139a265114SJohannes Weiner /* Scan lists relative to size */ 19149a265114SJohannes Weiner break; 19159a265114SJohannes Weiner case SCAN_FRACT: 19169a265114SJohannes Weiner /* 19179a265114SJohannes Weiner * Scan types proportional to swappiness and 19189a265114SJohannes Weiner * their relative recent reclaim efficiency. 19199a265114SJohannes Weiner */ 192076a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 19219a265114SJohannes Weiner break; 19229a265114SJohannes Weiner case SCAN_FILE: 19239a265114SJohannes Weiner case SCAN_ANON: 19249a265114SJohannes Weiner /* Scan one type exclusively */ 19259a265114SJohannes Weiner if ((scan_balance == SCAN_FILE) != file) 19269a265114SJohannes Weiner scan = 0; 19279a265114SJohannes Weiner break; 19289a265114SJohannes Weiner default: 19299a265114SJohannes Weiner /* Look ma, no brain */ 19309a265114SJohannes Weiner BUG(); 19319a265114SJohannes Weiner } 19324111304dSHugh Dickins nr[lru] = scan; 193376a33fc3SShaohua Li } 19346e08a369SWu Fengguang } 19354f98a2feSRik van Riel 19369b4f98cdSJohannes Weiner /* 19379b4f98cdSJohannes Weiner * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 19389b4f98cdSJohannes Weiner */ 19399b4f98cdSJohannes Weiner static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 19409b4f98cdSJohannes Weiner { 19419b4f98cdSJohannes Weiner unsigned long nr[NR_LRU_LISTS]; 1942e82e0561SMel Gorman unsigned long targets[NR_LRU_LISTS]; 19439b4f98cdSJohannes Weiner unsigned long nr_to_scan; 19449b4f98cdSJohannes Weiner enum lru_list lru; 19459b4f98cdSJohannes Weiner unsigned long nr_reclaimed = 0; 19469b4f98cdSJohannes Weiner unsigned long nr_to_reclaim = sc->nr_to_reclaim; 19479b4f98cdSJohannes Weiner struct blk_plug plug; 1948e82e0561SMel Gorman bool scan_adjusted = false; 19499b4f98cdSJohannes Weiner 19509b4f98cdSJohannes Weiner get_scan_count(lruvec, sc, nr); 19519b4f98cdSJohannes Weiner 1952e82e0561SMel Gorman /* Record the original scan target for proportional adjustments later */ 1953e82e0561SMel Gorman memcpy(targets, nr, sizeof(nr)); 1954e82e0561SMel Gorman 19559b4f98cdSJohannes Weiner blk_start_plug(&plug); 19569b4f98cdSJohannes Weiner while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 19579b4f98cdSJohannes Weiner nr[LRU_INACTIVE_FILE]) { 1958e82e0561SMel Gorman unsigned long nr_anon, nr_file, percentage; 1959e82e0561SMel Gorman unsigned long nr_scanned; 1960e82e0561SMel Gorman 19619b4f98cdSJohannes Weiner for_each_evictable_lru(lru) { 19629b4f98cdSJohannes Weiner if (nr[lru]) { 19639b4f98cdSJohannes Weiner nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX); 19649b4f98cdSJohannes Weiner nr[lru] -= nr_to_scan; 19659b4f98cdSJohannes Weiner 19669b4f98cdSJohannes Weiner nr_reclaimed += shrink_list(lru, nr_to_scan, 19679b4f98cdSJohannes Weiner lruvec, sc); 19689b4f98cdSJohannes Weiner } 19699b4f98cdSJohannes Weiner } 1970e82e0561SMel Gorman 1971e82e0561SMel Gorman if (nr_reclaimed < nr_to_reclaim || scan_adjusted) 1972e82e0561SMel Gorman continue; 1973e82e0561SMel Gorman 19749b4f98cdSJohannes Weiner /* 1975e82e0561SMel Gorman * For global direct reclaim, reclaim only the number of pages 1976e82e0561SMel Gorman * requested. Less care is taken to scan proportionally as it 1977e82e0561SMel Gorman * is more important to minimise direct reclaim stall latency 1978e82e0561SMel Gorman * than it is to properly age the LRU lists. 19799b4f98cdSJohannes Weiner */ 1980e82e0561SMel Gorman if (global_reclaim(sc) && !current_is_kswapd()) 19819b4f98cdSJohannes Weiner break; 1982e82e0561SMel Gorman 1983e82e0561SMel Gorman /* 1984e82e0561SMel Gorman * For kswapd and memcg, reclaim at least the number of pages 1985e82e0561SMel Gorman * requested. Ensure that the anon and file LRUs shrink 1986e82e0561SMel Gorman * proportionally what was requested by get_scan_count(). We 1987e82e0561SMel Gorman * stop reclaiming one LRU and reduce the amount scanning 1988e82e0561SMel Gorman * proportional to the original scan target. 1989e82e0561SMel Gorman */ 1990e82e0561SMel Gorman nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE]; 1991e82e0561SMel Gorman nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON]; 1992e82e0561SMel Gorman 1993e82e0561SMel Gorman if (nr_file > nr_anon) { 1994e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_ANON] + 1995e82e0561SMel Gorman targets[LRU_ACTIVE_ANON] + 1; 1996e82e0561SMel Gorman lru = LRU_BASE; 1997e82e0561SMel Gorman percentage = nr_anon * 100 / scan_target; 1998e82e0561SMel Gorman } else { 1999e82e0561SMel Gorman unsigned long scan_target = targets[LRU_INACTIVE_FILE] + 2000e82e0561SMel Gorman targets[LRU_ACTIVE_FILE] + 1; 2001e82e0561SMel Gorman lru = LRU_FILE; 2002e82e0561SMel Gorman percentage = nr_file * 100 / scan_target; 2003e82e0561SMel Gorman } 2004e82e0561SMel Gorman 2005e82e0561SMel Gorman /* Stop scanning the smaller of the LRU */ 2006e82e0561SMel Gorman nr[lru] = 0; 2007e82e0561SMel Gorman nr[lru + LRU_ACTIVE] = 0; 2008e82e0561SMel Gorman 2009e82e0561SMel Gorman /* 2010e82e0561SMel Gorman * Recalculate the other LRU scan count based on its original 2011e82e0561SMel Gorman * scan target and the percentage scanning already complete 2012e82e0561SMel Gorman */ 2013e82e0561SMel Gorman lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE; 2014e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2015e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2016e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2017e82e0561SMel Gorman 2018e82e0561SMel Gorman lru += LRU_ACTIVE; 2019e82e0561SMel Gorman nr_scanned = targets[lru] - nr[lru]; 2020e82e0561SMel Gorman nr[lru] = targets[lru] * (100 - percentage) / 100; 2021e82e0561SMel Gorman nr[lru] -= min(nr[lru], nr_scanned); 2022e82e0561SMel Gorman 2023e82e0561SMel Gorman scan_adjusted = true; 20249b4f98cdSJohannes Weiner } 20259b4f98cdSJohannes Weiner blk_finish_plug(&plug); 20269b4f98cdSJohannes Weiner sc->nr_reclaimed += nr_reclaimed; 20279b4f98cdSJohannes Weiner 20289b4f98cdSJohannes Weiner /* 20299b4f98cdSJohannes Weiner * Even if we did not try to evict anon pages at all, we want to 20309b4f98cdSJohannes Weiner * rebalance the anon lru active/inactive ratio. 20319b4f98cdSJohannes Weiner */ 20329b4f98cdSJohannes Weiner if (inactive_anon_is_low(lruvec)) 20339b4f98cdSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 20349b4f98cdSJohannes Weiner sc, LRU_ACTIVE_ANON); 20359b4f98cdSJohannes Weiner 20369b4f98cdSJohannes Weiner throttle_vm_writeout(sc->gfp_mask); 20379b4f98cdSJohannes Weiner } 20389b4f98cdSJohannes Weiner 203923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 20409e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 204123b9da55SMel Gorman { 2042d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 204323b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 20449e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 204523b9da55SMel Gorman return true; 204623b9da55SMel Gorman 204723b9da55SMel Gorman return false; 204823b9da55SMel Gorman } 204923b9da55SMel Gorman 20504f98a2feSRik van Riel /* 205123b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 205223b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 205323b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 205423b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 205523b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 20563e7d3449SMel Gorman */ 20579b4f98cdSJohannes Weiner static inline bool should_continue_reclaim(struct zone *zone, 20583e7d3449SMel Gorman unsigned long nr_reclaimed, 20593e7d3449SMel Gorman unsigned long nr_scanned, 20603e7d3449SMel Gorman struct scan_control *sc) 20613e7d3449SMel Gorman { 20623e7d3449SMel Gorman unsigned long pages_for_compaction; 20633e7d3449SMel Gorman unsigned long inactive_lru_pages; 20643e7d3449SMel Gorman 20653e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 20669e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 20673e7d3449SMel Gorman return false; 20683e7d3449SMel Gorman 20692876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 20702876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 20713e7d3449SMel Gorman /* 20722876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 20732876592fSMel Gorman * full LRU list has been scanned and we are still failing 20742876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 20752876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 20763e7d3449SMel Gorman */ 20773e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 20783e7d3449SMel Gorman return false; 20792876592fSMel Gorman } else { 20802876592fSMel Gorman /* 20812876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 20822876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 20832876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 20842876592fSMel Gorman * pages that were scanned. This will return to the 20852876592fSMel Gorman * caller faster at the risk reclaim/compaction and 20862876592fSMel Gorman * the resulting allocation attempt fails 20872876592fSMel Gorman */ 20882876592fSMel Gorman if (!nr_reclaimed) 20892876592fSMel Gorman return false; 20902876592fSMel Gorman } 20913e7d3449SMel Gorman 20923e7d3449SMel Gorman /* 20933e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 20943e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 20953e7d3449SMel Gorman */ 20963e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 20979b4f98cdSJohannes Weiner inactive_lru_pages = zone_page_state(zone, NR_INACTIVE_FILE); 2098ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 20999b4f98cdSJohannes Weiner inactive_lru_pages += zone_page_state(zone, NR_INACTIVE_ANON); 21003e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 21013e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 21023e7d3449SMel Gorman return true; 21033e7d3449SMel Gorman 21043e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 21059b4f98cdSJohannes Weiner switch (compaction_suitable(zone, sc->order)) { 21063e7d3449SMel Gorman case COMPACT_PARTIAL: 21073e7d3449SMel Gorman case COMPACT_CONTINUE: 21083e7d3449SMel Gorman return false; 21093e7d3449SMel Gorman default: 21103e7d3449SMel Gorman return true; 21113e7d3449SMel Gorman } 21123e7d3449SMel Gorman } 21133e7d3449SMel Gorman 21149e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc) 2115f16015fbSJohannes Weiner { 21169b4f98cdSJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 21179b4f98cdSJohannes Weiner 21189b4f98cdSJohannes Weiner do { 21195660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 21205660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 21215660048cSJohannes Weiner .zone = zone, 21229e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 21235660048cSJohannes Weiner }; 21245660048cSJohannes Weiner struct mem_cgroup *memcg; 21255660048cSJohannes Weiner 21269b4f98cdSJohannes Weiner nr_reclaimed = sc->nr_reclaimed; 21279b4f98cdSJohannes Weiner nr_scanned = sc->nr_scanned; 21289b4f98cdSJohannes Weiner 21295660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 21305660048cSJohannes Weiner do { 21319b4f98cdSJohannes Weiner struct lruvec *lruvec; 21329b4f98cdSJohannes Weiner 21339b4f98cdSJohannes Weiner lruvec = mem_cgroup_zone_lruvec(zone, memcg); 21345660048cSJohannes Weiner 2135f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, sc); 2136f9be23d6SKonstantin Khlebnikov 21375660048cSJohannes Weiner /* 2138a394cb8eSMichal Hocko * Direct reclaim and kswapd have to scan all memory 2139a394cb8eSMichal Hocko * cgroups to fulfill the overall scan target for the 21409b4f98cdSJohannes Weiner * zone. 2141a394cb8eSMichal Hocko * 2142a394cb8eSMichal Hocko * Limit reclaim, on the other hand, only cares about 2143a394cb8eSMichal Hocko * nr_to_reclaim pages to be reclaimed and it will 2144a394cb8eSMichal Hocko * retry with decreasing priority if one round over the 2145a394cb8eSMichal Hocko * whole hierarchy is not sufficient. 21465660048cSJohannes Weiner */ 2147a394cb8eSMichal Hocko if (!global_reclaim(sc) && 2148a394cb8eSMichal Hocko sc->nr_reclaimed >= sc->nr_to_reclaim) { 21495660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 21505660048cSJohannes Weiner break; 21515660048cSJohannes Weiner } 21525660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 21535660048cSJohannes Weiner } while (memcg); 215470ddf637SAnton Vorontsov 215570ddf637SAnton Vorontsov vmpressure(sc->gfp_mask, sc->target_mem_cgroup, 215670ddf637SAnton Vorontsov sc->nr_scanned - nr_scanned, 215770ddf637SAnton Vorontsov sc->nr_reclaimed - nr_reclaimed); 215870ddf637SAnton Vorontsov 21599b4f98cdSJohannes Weiner } while (should_continue_reclaim(zone, sc->nr_reclaimed - nr_reclaimed, 21609b4f98cdSJohannes Weiner sc->nr_scanned - nr_scanned, sc)); 2161f16015fbSJohannes Weiner } 2162f16015fbSJohannes Weiner 2163fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 2164fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 2165fe4b1b24SMel Gorman { 2166fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 2167fe4b1b24SMel Gorman bool watermark_ok; 2168fe4b1b24SMel Gorman 2169fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 2170fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 2171fe4b1b24SMel Gorman return false; 2172fe4b1b24SMel Gorman 2173fe4b1b24SMel Gorman /* 2174fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 2175fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 2176fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 2177fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 2178fe4b1b24SMel Gorman */ 2179fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 2180b40da049SJiang Liu (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 2181fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2182fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 2183fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 2184fe4b1b24SMel Gorman 2185fe4b1b24SMel Gorman /* 2186fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 2187fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 2188fe4b1b24SMel Gorman */ 2189aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 2190fe4b1b24SMel Gorman return watermark_ok; 2191fe4b1b24SMel Gorman 2192fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 2193fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 2194fe4b1b24SMel Gorman return false; 2195fe4b1b24SMel Gorman 2196fe4b1b24SMel Gorman return watermark_ok; 2197fe4b1b24SMel Gorman } 2198fe4b1b24SMel Gorman 21991da177e4SLinus Torvalds /* 22001da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 22011da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 22021da177e4SLinus Torvalds * request. 22031da177e4SLinus Torvalds * 220441858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 220541858966SMel Gorman * Because: 22061da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 22071da177e4SLinus Torvalds * allocation or 220841858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 220941858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 221041858966SMel Gorman * zone defense algorithm. 22111da177e4SLinus Torvalds * 22121da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 22131da177e4SLinus Torvalds * scan then give up on it. 2214e0c23279SMel Gorman * 2215e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 2216fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 22170cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 22180cee34fdSMel Gorman * further reclaim. 22191da177e4SLinus Torvalds */ 22209e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 22211da177e4SLinus Torvalds { 2222dd1a239fSMel Gorman struct zoneref *z; 222354a6eb5cSMel Gorman struct zone *zone; 2224d149e3b2SYing Han unsigned long nr_soft_reclaimed; 2225d149e3b2SYing Han unsigned long nr_soft_scanned; 22260cee34fdSMel Gorman bool aborted_reclaim = false; 22271cfb419bSKAMEZAWA Hiroyuki 2228cc715d99SMel Gorman /* 2229cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2230cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2231cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2232cc715d99SMel Gorman */ 2233cc715d99SMel Gorman if (buffer_heads_over_limit) 2234cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2235cc715d99SMel Gorman 2236d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2237d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 2238f3fe6512SCon Kolivas if (!populated_zone(zone)) 22391da177e4SLinus Torvalds continue; 22401cfb419bSKAMEZAWA Hiroyuki /* 22411cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 22421cfb419bSKAMEZAWA Hiroyuki * to global LRU. 22431cfb419bSKAMEZAWA Hiroyuki */ 224489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 224502a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 22461da177e4SLinus Torvalds continue; 22479e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 22489e3b2f8cSKonstantin Khlebnikov sc->priority != DEF_PRIORITY) 22491da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 2250d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION)) { 2251e0887c19SRik van Riel /* 2252e0c23279SMel Gorman * If we already have plenty of memory free for 2253e0c23279SMel Gorman * compaction in this zone, don't free any more. 2254e0c23279SMel Gorman * Even though compaction is invoked for any 2255e0c23279SMel Gorman * non-zero order, only frequent costly order 2256e0c23279SMel Gorman * reclamation is disruptive enough to become a 2257c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2258c7cfa37bSCopot Alexandru * page allocations. 2259e0887c19SRik van Riel */ 2260fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 22610cee34fdSMel Gorman aborted_reclaim = true; 2262e0887c19SRik van Riel continue; 2263e0887c19SRik van Riel } 2264e0c23279SMel Gorman } 2265ac34a1a3SKAMEZAWA Hiroyuki /* 2266ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 2267ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 2268ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 2269ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 2270ac34a1a3SKAMEZAWA Hiroyuki */ 2271d149e3b2SYing Han nr_soft_scanned = 0; 2272d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 2273d149e3b2SYing Han sc->order, sc->gfp_mask, 2274d149e3b2SYing Han &nr_soft_scanned); 2275d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 2276ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 2277ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2278ac34a1a3SKAMEZAWA Hiroyuki } 2279d149e3b2SYing Han 22809e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 22811da177e4SLinus Torvalds } 2282e0c23279SMel Gorman 22830cee34fdSMel Gorman return aborted_reclaim; 2284d1908362SMinchan Kim } 2285d1908362SMinchan Kim 2286d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone) 2287d1908362SMinchan Kim { 2288d1908362SMinchan Kim return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 2289d1908362SMinchan Kim } 2290d1908362SMinchan Kim 2291929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2292d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2293d1908362SMinchan Kim struct scan_control *sc) 2294d1908362SMinchan Kim { 2295d1908362SMinchan Kim struct zoneref *z; 2296d1908362SMinchan Kim struct zone *zone; 2297d1908362SMinchan Kim 2298d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2299d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2300d1908362SMinchan Kim if (!populated_zone(zone)) 2301d1908362SMinchan Kim continue; 2302d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2303d1908362SMinchan Kim continue; 2304929bea7cSKOSAKI Motohiro if (!zone->all_unreclaimable) 2305929bea7cSKOSAKI Motohiro return false; 2306d1908362SMinchan Kim } 2307d1908362SMinchan Kim 2308929bea7cSKOSAKI Motohiro return true; 23091da177e4SLinus Torvalds } 23101da177e4SLinus Torvalds 23111da177e4SLinus Torvalds /* 23121da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 23131da177e4SLinus Torvalds * 23141da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 23151da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 23161da177e4SLinus Torvalds * 23171da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 23181da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 23195b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 23205b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 23215b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 23225b0830cbSJens Axboe * work, and the allocation attempt will fail. 2323a41f24eaSNishanth Aravamudan * 2324a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2325a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 23261da177e4SLinus Torvalds */ 2327dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2328a09ed5e0SYing Han struct scan_control *sc, 2329a09ed5e0SYing Han struct shrink_control *shrink) 23301da177e4SLinus Torvalds { 233169e05944SAndrew Morton unsigned long total_scanned = 0; 23321da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2333dd1a239fSMel Gorman struct zoneref *z; 233454a6eb5cSMel Gorman struct zone *zone; 233522fba335SKOSAKI Motohiro unsigned long writeback_threshold; 23360cee34fdSMel Gorman bool aborted_reclaim; 23371da177e4SLinus Torvalds 2338873b4771SKeika Kobayashi delayacct_freepages_start(); 2339873b4771SKeika Kobayashi 234089b5fae5SJohannes Weiner if (global_reclaim(sc)) 2341f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 23421da177e4SLinus Torvalds 23439e3b2f8cSKonstantin Khlebnikov do { 234470ddf637SAnton Vorontsov vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup, 234570ddf637SAnton Vorontsov sc->priority); 234666e1707bSBalbir Singh sc->nr_scanned = 0; 23479e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2348e0c23279SMel Gorman 234966e1707bSBalbir Singh /* 23505a1c9cbcSMel Gorman * Don't shrink slabs when reclaiming memory from over limit 23515a1c9cbcSMel Gorman * cgroups but do shrink slab at least once when aborting 23525a1c9cbcSMel Gorman * reclaim for compaction to avoid unevenly scanning file/anon 23535a1c9cbcSMel Gorman * LRU pages over slab pages. 235466e1707bSBalbir Singh */ 235589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2356c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2357d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2358d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2359c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2360c6a8a8c5SKOSAKI Motohiro continue; 2361c6a8a8c5SKOSAKI Motohiro 2362c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2363c6a8a8c5SKOSAKI Motohiro } 2364c6a8a8c5SKOSAKI Motohiro 23651495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 23661da177e4SLinus Torvalds if (reclaim_state) { 2367a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 23681da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 23691da177e4SLinus Torvalds } 237091a45470SKAMEZAWA Hiroyuki } 237166e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2372bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 23731da177e4SLinus Torvalds goto out; 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds /* 23760e50ce3bSMinchan Kim * If we're getting trouble reclaiming, start doing 23770e50ce3bSMinchan Kim * writepage even in laptop mode. 23780e50ce3bSMinchan Kim */ 23790e50ce3bSMinchan Kim if (sc->priority < DEF_PRIORITY - 2) 23800e50ce3bSMinchan Kim sc->may_writepage = 1; 23810e50ce3bSMinchan Kim 23820e50ce3bSMinchan Kim /* 23831da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 23841da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 23851da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 23861da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 23871da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 23881da177e4SLinus Torvalds */ 238922fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 239022fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 23910e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 23920e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 239366e1707bSBalbir Singh sc->may_writepage = 1; 23941da177e4SLinus Torvalds } 23955a1c9cbcSMel Gorman } while (--sc->priority >= 0 && !aborted_reclaim); 2396bb21c7ceSKOSAKI Motohiro 23971da177e4SLinus Torvalds out: 2398873b4771SKeika Kobayashi delayacct_freepages_end(); 2399873b4771SKeika Kobayashi 2400bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2401bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2402bb21c7ceSKOSAKI Motohiro 2403929bea7cSKOSAKI Motohiro /* 2404929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2405929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2406929bea7cSKOSAKI Motohiro * check. 2407929bea7cSKOSAKI Motohiro */ 2408929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2409929bea7cSKOSAKI Motohiro return 0; 2410929bea7cSKOSAKI Motohiro 24110cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 24120cee34fdSMel Gorman if (aborted_reclaim) 24137335084dSMel Gorman return 1; 24147335084dSMel Gorman 2415bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 241689b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2417bb21c7ceSKOSAKI Motohiro return 1; 2418bb21c7ceSKOSAKI Motohiro 2419bb21c7ceSKOSAKI Motohiro return 0; 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds 24225515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 24235515061dSMel Gorman { 24245515061dSMel Gorman struct zone *zone; 24255515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 24265515061dSMel Gorman unsigned long free_pages = 0; 24275515061dSMel Gorman int i; 24285515061dSMel Gorman bool wmark_ok; 24295515061dSMel Gorman 24305515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 24315515061dSMel Gorman zone = &pgdat->node_zones[i]; 24325515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 24335515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 24345515061dSMel Gorman } 24355515061dSMel Gorman 24365515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 24375515061dSMel Gorman 24385515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 24395515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 24405515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 24415515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 24425515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 24435515061dSMel Gorman } 24445515061dSMel Gorman 24455515061dSMel Gorman return wmark_ok; 24465515061dSMel Gorman } 24475515061dSMel Gorman 24485515061dSMel Gorman /* 24495515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 24505515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 24515515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 245250694c28SMel Gorman * when the low watermark is reached. 245350694c28SMel Gorman * 245450694c28SMel Gorman * Returns true if a fatal signal was delivered during throttling. If this 245550694c28SMel Gorman * happens, the page allocator should not consider triggering the OOM killer. 24565515061dSMel Gorman */ 245750694c28SMel Gorman static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 24585515061dSMel Gorman nodemask_t *nodemask) 24595515061dSMel Gorman { 24605515061dSMel Gorman struct zone *zone; 24615515061dSMel Gorman int high_zoneidx = gfp_zone(gfp_mask); 24625515061dSMel Gorman pg_data_t *pgdat; 24635515061dSMel Gorman 24645515061dSMel Gorman /* 24655515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 24665515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 24675515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 24685515061dSMel Gorman * committing a transaction where throttling it could forcing other 24695515061dSMel Gorman * processes to block on log_wait_commit(). 24705515061dSMel Gorman */ 24715515061dSMel Gorman if (current->flags & PF_KTHREAD) 247250694c28SMel Gorman goto out; 247350694c28SMel Gorman 247450694c28SMel Gorman /* 247550694c28SMel Gorman * If a fatal signal is pending, this process should not throttle. 247650694c28SMel Gorman * It should return quickly so it can exit and free its memory 247750694c28SMel Gorman */ 247850694c28SMel Gorman if (fatal_signal_pending(current)) 247950694c28SMel Gorman goto out; 24805515061dSMel Gorman 24815515061dSMel Gorman /* Check if the pfmemalloc reserves are ok */ 24825515061dSMel Gorman first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone); 24835515061dSMel Gorman pgdat = zone->zone_pgdat; 24845515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 248550694c28SMel Gorman goto out; 24865515061dSMel Gorman 248768243e76SMel Gorman /* Account for the throttling */ 248868243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 248968243e76SMel Gorman 24905515061dSMel Gorman /* 24915515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 24925515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 24935515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 24945515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 24955515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 24965515061dSMel Gorman * second before continuing. 24975515061dSMel Gorman */ 24985515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 24995515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 25005515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 250150694c28SMel Gorman 250250694c28SMel Gorman goto check_pending; 25035515061dSMel Gorman } 25045515061dSMel Gorman 25055515061dSMel Gorman /* Throttle until kswapd wakes the process */ 25065515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 25075515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 250850694c28SMel Gorman 250950694c28SMel Gorman check_pending: 251050694c28SMel Gorman if (fatal_signal_pending(current)) 251150694c28SMel Gorman return true; 251250694c28SMel Gorman 251350694c28SMel Gorman out: 251450694c28SMel Gorman return false; 25155515061dSMel Gorman } 25165515061dSMel Gorman 2517dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2518327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 251966e1707bSBalbir Singh { 252033906bc5SMel Gorman unsigned long nr_reclaimed; 252166e1707bSBalbir Singh struct scan_control sc = { 252221caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 252366e1707bSBalbir Singh .may_writepage = !laptop_mode, 252422fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2525a6dc60f8SJohannes Weiner .may_unmap = 1, 25262e2e4259SKOSAKI Motohiro .may_swap = 1, 252766e1707bSBalbir Singh .order = order, 25289e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2529f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2530327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 253166e1707bSBalbir Singh }; 2532a09ed5e0SYing Han struct shrink_control shrink = { 2533a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2534a09ed5e0SYing Han }; 253566e1707bSBalbir Singh 25365515061dSMel Gorman /* 253750694c28SMel Gorman * Do not enter reclaim if fatal signal was delivered while throttled. 253850694c28SMel Gorman * 1 is returned so that the page allocator does not OOM kill at this 253950694c28SMel Gorman * point. 25405515061dSMel Gorman */ 254150694c28SMel Gorman if (throttle_direct_reclaim(gfp_mask, zonelist, nodemask)) 25425515061dSMel Gorman return 1; 25435515061dSMel Gorman 254433906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 254533906bc5SMel Gorman sc.may_writepage, 254633906bc5SMel Gorman gfp_mask); 254733906bc5SMel Gorman 2548a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 254933906bc5SMel Gorman 255033906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 255133906bc5SMel Gorman 255233906bc5SMel Gorman return nr_reclaimed; 255366e1707bSBalbir Singh } 255466e1707bSBalbir Singh 2555c255a458SAndrew Morton #ifdef CONFIG_MEMCG 255666e1707bSBalbir Singh 255772835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 25584e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 25590ae5e89cSYing Han struct zone *zone, 25600ae5e89cSYing Han unsigned long *nr_scanned) 25614e416953SBalbir Singh { 25624e416953SBalbir Singh struct scan_control sc = { 25630ae5e89cSYing Han .nr_scanned = 0, 2564b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 25654e416953SBalbir Singh .may_writepage = !laptop_mode, 25664e416953SBalbir Singh .may_unmap = 1, 25674e416953SBalbir Singh .may_swap = !noswap, 25684e416953SBalbir Singh .order = 0, 25699e3b2f8cSKonstantin Khlebnikov .priority = 0, 257072835c86SJohannes Weiner .target_mem_cgroup = memcg, 25714e416953SBalbir Singh }; 2572f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 25730ae5e89cSYing Han 25744e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 25754e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2576bdce6d9eSKOSAKI Motohiro 25779e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2578bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2579bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2580bdce6d9eSKOSAKI Motohiro 25814e416953SBalbir Singh /* 25824e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 25834e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 25844e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 25854e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 25864e416953SBalbir Singh * the priority and make it zero. 25874e416953SBalbir Singh */ 2588f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, &sc); 2589bdce6d9eSKOSAKI Motohiro 2590bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2591bdce6d9eSKOSAKI Motohiro 25920ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 25934e416953SBalbir Singh return sc.nr_reclaimed; 25944e416953SBalbir Singh } 25954e416953SBalbir Singh 259672835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 25978c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2598185efc0fSJohannes Weiner bool noswap) 259966e1707bSBalbir Singh { 26004e416953SBalbir Singh struct zonelist *zonelist; 2601bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2602889976dbSYing Han int nid; 260366e1707bSBalbir Singh struct scan_control sc = { 260466e1707bSBalbir Singh .may_writepage = !laptop_mode, 2605a6dc60f8SJohannes Weiner .may_unmap = 1, 26062e2e4259SKOSAKI Motohiro .may_swap = !noswap, 260722fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 260866e1707bSBalbir Singh .order = 0, 26099e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 261072835c86SJohannes Weiner .target_mem_cgroup = memcg, 2611327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2612a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2613a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2614a09ed5e0SYing Han }; 2615a09ed5e0SYing Han struct shrink_control shrink = { 2616a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 261766e1707bSBalbir Singh }; 261866e1707bSBalbir Singh 2619889976dbSYing Han /* 2620889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2621889976dbSYing Han * take care of from where we get pages. So the node where we start the 2622889976dbSYing Han * scan does not need to be the current node. 2623889976dbSYing Han */ 262472835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2625889976dbSYing Han 2626889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2627bdce6d9eSKOSAKI Motohiro 2628bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2629bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2630bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2631bdce6d9eSKOSAKI Motohiro 2632a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2633bdce6d9eSKOSAKI Motohiro 2634bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2635bdce6d9eSKOSAKI Motohiro 2636bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 263766e1707bSBalbir Singh } 263866e1707bSBalbir Singh #endif 263966e1707bSBalbir Singh 26409e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2641f16015fbSJohannes Weiner { 2642b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2643b95a2f2dSJohannes Weiner 2644b95a2f2dSJohannes Weiner if (!total_swap_pages) 2645b95a2f2dSJohannes Weiner return; 2646b95a2f2dSJohannes Weiner 2647b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2648b95a2f2dSJohannes Weiner do { 2649c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2650f16015fbSJohannes Weiner 2651c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 26521a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 26539e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2654b95a2f2dSJohannes Weiner 2655b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2656b95a2f2dSJohannes Weiner } while (memcg); 2657f16015fbSJohannes Weiner } 2658f16015fbSJohannes Weiner 265960cefed4SJohannes Weiner static bool zone_balanced(struct zone *zone, int order, 266060cefed4SJohannes Weiner unsigned long balance_gap, int classzone_idx) 266160cefed4SJohannes Weiner { 266260cefed4SJohannes Weiner if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone) + 266360cefed4SJohannes Weiner balance_gap, classzone_idx, 0)) 266460cefed4SJohannes Weiner return false; 266560cefed4SJohannes Weiner 2666d84da3f9SKirill A. Shutemov if (IS_ENABLED(CONFIG_COMPACTION) && order && 2667d84da3f9SKirill A. Shutemov !compaction_suitable(zone, order)) 266860cefed4SJohannes Weiner return false; 266960cefed4SJohannes Weiner 267060cefed4SJohannes Weiner return true; 267160cefed4SJohannes Weiner } 267260cefed4SJohannes Weiner 26731741c877SMel Gorman /* 26744ae0a48bSZlatko Calusic * pgdat_balanced() is used when checking if a node is balanced. 26754ae0a48bSZlatko Calusic * 26764ae0a48bSZlatko Calusic * For order-0, all zones must be balanced! 26774ae0a48bSZlatko Calusic * 26784ae0a48bSZlatko Calusic * For high-order allocations only zones that meet watermarks and are in a 26794ae0a48bSZlatko Calusic * zone allowed by the callers classzone_idx are added to balanced_pages. The 26804ae0a48bSZlatko Calusic * total of balanced pages must be at least 25% of the zones allowed by 26814ae0a48bSZlatko Calusic * classzone_idx for the node to be considered balanced. Forcing all zones to 26824ae0a48bSZlatko Calusic * be balanced for high orders can cause excessive reclaim when there are 26834ae0a48bSZlatko Calusic * imbalanced zones. 26841741c877SMel Gorman * The choice of 25% is due to 26851741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 26861741c877SMel Gorman * reasonable sized machine 26871741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 268825985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 26891741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 26901741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 26911741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 26921741c877SMel Gorman */ 26934ae0a48bSZlatko Calusic static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx) 26941741c877SMel Gorman { 2695b40da049SJiang Liu unsigned long managed_pages = 0; 26964ae0a48bSZlatko Calusic unsigned long balanced_pages = 0; 26971741c877SMel Gorman int i; 26981741c877SMel Gorman 26994ae0a48bSZlatko Calusic /* Check the watermark levels */ 27004ae0a48bSZlatko Calusic for (i = 0; i <= classzone_idx; i++) { 27014ae0a48bSZlatko Calusic struct zone *zone = pgdat->node_zones + i; 27021741c877SMel Gorman 27034ae0a48bSZlatko Calusic if (!populated_zone(zone)) 27044ae0a48bSZlatko Calusic continue; 27054ae0a48bSZlatko Calusic 2706b40da049SJiang Liu managed_pages += zone->managed_pages; 27074ae0a48bSZlatko Calusic 27084ae0a48bSZlatko Calusic /* 27094ae0a48bSZlatko Calusic * A special case here: 27104ae0a48bSZlatko Calusic * 27114ae0a48bSZlatko Calusic * balance_pgdat() skips over all_unreclaimable after 27124ae0a48bSZlatko Calusic * DEF_PRIORITY. Effectively, it considers them balanced so 27134ae0a48bSZlatko Calusic * they must be considered balanced here as well! 27144ae0a48bSZlatko Calusic */ 27154ae0a48bSZlatko Calusic if (zone->all_unreclaimable) { 2716b40da049SJiang Liu balanced_pages += zone->managed_pages; 27174ae0a48bSZlatko Calusic continue; 27184ae0a48bSZlatko Calusic } 27194ae0a48bSZlatko Calusic 27204ae0a48bSZlatko Calusic if (zone_balanced(zone, order, 0, i)) 2721b40da049SJiang Liu balanced_pages += zone->managed_pages; 27224ae0a48bSZlatko Calusic else if (!order) 27234ae0a48bSZlatko Calusic return false; 27244ae0a48bSZlatko Calusic } 27254ae0a48bSZlatko Calusic 27264ae0a48bSZlatko Calusic if (order) 2727b40da049SJiang Liu return balanced_pages >= (managed_pages >> 2); 27284ae0a48bSZlatko Calusic else 27294ae0a48bSZlatko Calusic return true; 27301741c877SMel Gorman } 27311741c877SMel Gorman 27325515061dSMel Gorman /* 27335515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 27345515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 27355515061dSMel Gorman * 27365515061dSMel Gorman * Returns true if kswapd is ready to sleep 27375515061dSMel Gorman */ 27385515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2739dc83edd9SMel Gorman int classzone_idx) 2740f50de2d3SMel Gorman { 2741f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2742f50de2d3SMel Gorman if (remaining) 27435515061dSMel Gorman return false; 27445515061dSMel Gorman 27455515061dSMel Gorman /* 27465515061dSMel Gorman * There is a potential race between when kswapd checks its watermarks 27475515061dSMel Gorman * and a process gets throttled. There is also a potential race if 27485515061dSMel Gorman * processes get throttled, kswapd wakes, a large process exits therby 27495515061dSMel Gorman * balancing the zones that causes kswapd to miss a wakeup. If kswapd 27505515061dSMel Gorman * is going to sleep, no process should be sleeping on pfmemalloc_wait 27515515061dSMel Gorman * so wake them now if necessary. If necessary, processes will wake 27525515061dSMel Gorman * kswapd and get throttled again 27535515061dSMel Gorman */ 27545515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait)) { 27555515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 27565515061dSMel Gorman return false; 27575515061dSMel Gorman } 2758f50de2d3SMel Gorman 27594ae0a48bSZlatko Calusic return pgdat_balanced(pgdat, order, classzone_idx); 2760f50de2d3SMel Gorman } 2761f50de2d3SMel Gorman 27621da177e4SLinus Torvalds /* 276375485363SMel Gorman * kswapd shrinks the zone by the number of pages required to reach 276475485363SMel Gorman * the high watermark. 2765b8e83b94SMel Gorman * 2766b8e83b94SMel Gorman * Returns true if kswapd scanned at least the requested number of pages to 2767283aba9fSMel Gorman * reclaim or if the lack of progress was due to pages under writeback. 2768283aba9fSMel Gorman * This is used to determine if the scanning priority needs to be raised. 276975485363SMel Gorman */ 2770b8e83b94SMel Gorman static bool kswapd_shrink_zone(struct zone *zone, 27717c954f6dSMel Gorman int classzone_idx, 277275485363SMel Gorman struct scan_control *sc, 27732ab44f43SMel Gorman unsigned long lru_pages, 27742ab44f43SMel Gorman unsigned long *nr_attempted) 277575485363SMel Gorman { 277675485363SMel Gorman unsigned long nr_slab; 27777c954f6dSMel Gorman int testorder = sc->order; 27787c954f6dSMel Gorman unsigned long balance_gap; 277975485363SMel Gorman struct reclaim_state *reclaim_state = current->reclaim_state; 278075485363SMel Gorman struct shrink_control shrink = { 278175485363SMel Gorman .gfp_mask = sc->gfp_mask, 278275485363SMel Gorman }; 27837c954f6dSMel Gorman bool lowmem_pressure; 278475485363SMel Gorman 278575485363SMel Gorman /* Reclaim above the high watermark. */ 278675485363SMel Gorman sc->nr_to_reclaim = max(SWAP_CLUSTER_MAX, high_wmark_pages(zone)); 27877c954f6dSMel Gorman 27887c954f6dSMel Gorman /* 27897c954f6dSMel Gorman * Kswapd reclaims only single pages with compaction enabled. Trying 27907c954f6dSMel Gorman * too hard to reclaim until contiguous free pages have become 27917c954f6dSMel Gorman * available can hurt performance by evicting too much useful data 27927c954f6dSMel Gorman * from memory. Do not reclaim more than needed for compaction. 27937c954f6dSMel Gorman */ 27947c954f6dSMel Gorman if (IS_ENABLED(CONFIG_COMPACTION) && sc->order && 27957c954f6dSMel Gorman compaction_suitable(zone, sc->order) != 27967c954f6dSMel Gorman COMPACT_SKIPPED) 27977c954f6dSMel Gorman testorder = 0; 27987c954f6dSMel Gorman 27997c954f6dSMel Gorman /* 28007c954f6dSMel Gorman * We put equal pressure on every zone, unless one zone has way too 28017c954f6dSMel Gorman * many pages free already. The "too many pages" is defined as the 28027c954f6dSMel Gorman * high wmark plus a "gap" where the gap is either the low 28037c954f6dSMel Gorman * watermark or 1% of the zone, whichever is smaller. 28047c954f6dSMel Gorman */ 28057c954f6dSMel Gorman balance_gap = min(low_wmark_pages(zone), 28067c954f6dSMel Gorman (zone->managed_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 28077c954f6dSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 28087c954f6dSMel Gorman 28097c954f6dSMel Gorman /* 28107c954f6dSMel Gorman * If there is no low memory pressure or the zone is balanced then no 28117c954f6dSMel Gorman * reclaim is necessary 28127c954f6dSMel Gorman */ 28137c954f6dSMel Gorman lowmem_pressure = (buffer_heads_over_limit && is_highmem(zone)); 28147c954f6dSMel Gorman if (!lowmem_pressure && zone_balanced(zone, testorder, 28157c954f6dSMel Gorman balance_gap, classzone_idx)) 28167c954f6dSMel Gorman return true; 28177c954f6dSMel Gorman 281875485363SMel Gorman shrink_zone(zone, sc); 281975485363SMel Gorman 282075485363SMel Gorman reclaim_state->reclaimed_slab = 0; 282175485363SMel Gorman nr_slab = shrink_slab(&shrink, sc->nr_scanned, lru_pages); 282275485363SMel Gorman sc->nr_reclaimed += reclaim_state->reclaimed_slab; 282375485363SMel Gorman 28242ab44f43SMel Gorman /* Account for the number of pages attempted to reclaim */ 28252ab44f43SMel Gorman *nr_attempted += sc->nr_to_reclaim; 28262ab44f43SMel Gorman 282775485363SMel Gorman if (nr_slab == 0 && !zone_reclaimable(zone)) 282875485363SMel Gorman zone->all_unreclaimable = 1; 2829b8e83b94SMel Gorman 2830283aba9fSMel Gorman zone_clear_flag(zone, ZONE_WRITEBACK); 2831283aba9fSMel Gorman 28327c954f6dSMel Gorman /* 28337c954f6dSMel Gorman * If a zone reaches its high watermark, consider it to be no longer 28347c954f6dSMel Gorman * congested. It's possible there are dirty pages backed by congested 28357c954f6dSMel Gorman * BDIs but as pressure is relieved, speculatively avoid congestion 28367c954f6dSMel Gorman * waits. 28377c954f6dSMel Gorman */ 28387c954f6dSMel Gorman if (!zone->all_unreclaimable && 28397c954f6dSMel Gorman zone_balanced(zone, testorder, 0, classzone_idx)) { 28407c954f6dSMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 28417c954f6dSMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 28427c954f6dSMel Gorman } 28437c954f6dSMel Gorman 2844b8e83b94SMel Gorman return sc->nr_scanned >= sc->nr_to_reclaim; 284575485363SMel Gorman } 284675485363SMel Gorman 284775485363SMel Gorman /* 28481da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 284941858966SMel Gorman * they are all at high_wmark_pages(zone). 28501da177e4SLinus Torvalds * 28510abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 28521da177e4SLinus Torvalds * 28531da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 28541da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 28551da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 28561da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 28571da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 28581da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 28591da177e4SLinus Torvalds * the zone for when the problem goes away. 28601da177e4SLinus Torvalds * 28611da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 286241858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 286341858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 286441858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 286541858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 286641858966SMel Gorman * of pages is balanced across the zones. 28671da177e4SLinus Torvalds */ 286899504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2869dc83edd9SMel Gorman int *classzone_idx) 28701da177e4SLinus Torvalds { 28711da177e4SLinus Torvalds int i; 287299504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 28730ae5e89cSYing Han unsigned long nr_soft_reclaimed; 28740ae5e89cSYing Han unsigned long nr_soft_scanned; 2875179e9639SAndrew Morton struct scan_control sc = { 2876179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2877b8e83b94SMel Gorman .priority = DEF_PRIORITY, 2878a6dc60f8SJohannes Weiner .may_unmap = 1, 28792e2e4259SKOSAKI Motohiro .may_swap = 1, 2880b8e83b94SMel Gorman .may_writepage = !laptop_mode, 28815ad333ebSAndy Whitcroft .order = order, 2882f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2883179e9639SAndrew Morton }; 2884f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 28851da177e4SLinus Torvalds 28869e3b2f8cSKonstantin Khlebnikov do { 28871da177e4SLinus Torvalds unsigned long lru_pages = 0; 28882ab44f43SMel Gorman unsigned long nr_attempted = 0; 2889b8e83b94SMel Gorman bool raise_priority = true; 28902ab44f43SMel Gorman bool pgdat_needs_compaction = (order > 0); 2891b8e83b94SMel Gorman 2892b8e83b94SMel Gorman sc.nr_reclaimed = 0; 28931da177e4SLinus Torvalds 28941da177e4SLinus Torvalds /* 28951da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 28961da177e4SLinus Torvalds * zone which needs scanning 28971da177e4SLinus Torvalds */ 28981da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 28991da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29001da177e4SLinus Torvalds 2901f3fe6512SCon Kolivas if (!populated_zone(zone)) 29021da177e4SLinus Torvalds continue; 29031da177e4SLinus Torvalds 29049e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 29059e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 29061da177e4SLinus Torvalds continue; 29071da177e4SLinus Torvalds 2908556adecbSRik van Riel /* 2909556adecbSRik van Riel * Do some background aging of the anon list, to give 2910556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2911556adecbSRik van Riel */ 29129e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2913556adecbSRik van Riel 2914cc715d99SMel Gorman /* 2915cc715d99SMel Gorman * If the number of buffer_heads in the machine 2916cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2917cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2918cc715d99SMel Gorman * it to relieve lowmem pressure. 2919cc715d99SMel Gorman */ 2920cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2921cc715d99SMel Gorman end_zone = i; 2922cc715d99SMel Gorman break; 2923cc715d99SMel Gorman } 2924cc715d99SMel Gorman 292560cefed4SJohannes Weiner if (!zone_balanced(zone, order, 0, 0)) { 29261da177e4SLinus Torvalds end_zone = i; 2927e1dbeda6SAndrew Morton break; 2928439423f6SShaohua Li } else { 2929d43006d5SMel Gorman /* 2930d43006d5SMel Gorman * If balanced, clear the dirty and congested 2931d43006d5SMel Gorman * flags 2932d43006d5SMel Gorman */ 2933439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 2934d43006d5SMel Gorman zone_clear_flag(zone, ZONE_TAIL_LRU_DIRTY); 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds } 2937dafcb73eSZlatko Calusic 2938b8e83b94SMel Gorman if (i < 0) 29391da177e4SLinus Torvalds goto out; 2940e1dbeda6SAndrew Morton 29411da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 29421da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29431da177e4SLinus Torvalds 29442ab44f43SMel Gorman if (!populated_zone(zone)) 29452ab44f43SMel Gorman continue; 29462ab44f43SMel Gorman 2947adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 29482ab44f43SMel Gorman 29492ab44f43SMel Gorman /* 29502ab44f43SMel Gorman * If any zone is currently balanced then kswapd will 29512ab44f43SMel Gorman * not call compaction as it is expected that the 29522ab44f43SMel Gorman * necessary pages are already available. 29532ab44f43SMel Gorman */ 29542ab44f43SMel Gorman if (pgdat_needs_compaction && 29552ab44f43SMel Gorman zone_watermark_ok(zone, order, 29562ab44f43SMel Gorman low_wmark_pages(zone), 29572ab44f43SMel Gorman *classzone_idx, 0)) 29582ab44f43SMel Gorman pgdat_needs_compaction = false; 29591da177e4SLinus Torvalds } 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds /* 2962b7ea3c41SMel Gorman * If we're getting trouble reclaiming, start doing writepage 2963b7ea3c41SMel Gorman * even in laptop mode. 2964b7ea3c41SMel Gorman */ 2965b7ea3c41SMel Gorman if (sc.priority < DEF_PRIORITY - 2) 2966b7ea3c41SMel Gorman sc.may_writepage = 1; 2967b7ea3c41SMel Gorman 2968b7ea3c41SMel Gorman /* 29691da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 29701da177e4SLinus Torvalds * at the last zone which needs scanning. 29711da177e4SLinus Torvalds * 29721da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 29731da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 29741da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 29751da177e4SLinus Torvalds * cause too much scanning of the lower zones. 29761da177e4SLinus Torvalds */ 29771da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 29781da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 29791da177e4SLinus Torvalds 2980f3fe6512SCon Kolivas if (!populated_zone(zone)) 29811da177e4SLinus Torvalds continue; 29821da177e4SLinus Torvalds 29839e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 29849e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 29851da177e4SLinus Torvalds continue; 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds sc.nr_scanned = 0; 29884e416953SBalbir Singh 29890ae5e89cSYing Han nr_soft_scanned = 0; 29904e416953SBalbir Singh /* 29914e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 29924e416953SBalbir Singh */ 29930ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 29940ae5e89cSYing Han order, sc.gfp_mask, 29950ae5e89cSYing Han &nr_soft_scanned); 29960ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 299700918b6aSKOSAKI Motohiro 299832a4330dSRik van Riel /* 29997c954f6dSMel Gorman * There should be no need to raise the scanning 30007c954f6dSMel Gorman * priority if enough pages are already being scanned 30017c954f6dSMel Gorman * that that high watermark would be met at 100% 30027c954f6dSMel Gorman * efficiency. 300332a4330dSRik van Riel */ 30047c954f6dSMel Gorman if (kswapd_shrink_zone(zone, end_zone, &sc, 30057c954f6dSMel Gorman lru_pages, &nr_attempted)) 3006b8e83b94SMel Gorman raise_priority = false; 3007b8e83b94SMel Gorman } 3008d7868daeSMel Gorman 30095515061dSMel Gorman /* 30105515061dSMel Gorman * If the low watermark is met there is no need for processes 30115515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 30125515061dSMel Gorman * able to safely make forward progress. Wake them 30135515061dSMel Gorman */ 30145515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 30155515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 30165515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 30175515061dSMel Gorman 30181da177e4SLinus Torvalds /* 3019b8e83b94SMel Gorman * Fragmentation may mean that the system cannot be rebalanced 3020b8e83b94SMel Gorman * for high-order allocations in all zones. If twice the 3021b8e83b94SMel Gorman * allocation size has been reclaimed and the zones are still 3022b8e83b94SMel Gorman * not balanced then recheck the watermarks at order-0 to 3023b8e83b94SMel Gorman * prevent kswapd reclaiming excessively. Assume that a 3024b8e83b94SMel Gorman * process requested a high-order can direct reclaim/compact. 30251da177e4SLinus Torvalds */ 3026b8e83b94SMel Gorman if (order && sc.nr_reclaimed >= 2UL << order) 302773ce02e9SKOSAKI Motohiro order = sc.order = 0; 302873ce02e9SKOSAKI Motohiro 3029b8e83b94SMel Gorman /* Check if kswapd should be suspending */ 3030b8e83b94SMel Gorman if (try_to_freeze() || kthread_should_stop()) 3031b8e83b94SMel Gorman break; 3032b8e83b94SMel Gorman 3033b8e83b94SMel Gorman /* 30342ab44f43SMel Gorman * Compact if necessary and kswapd is reclaiming at least the 30352ab44f43SMel Gorman * high watermark number of pages as requsted 30362ab44f43SMel Gorman */ 30372ab44f43SMel Gorman if (pgdat_needs_compaction && sc.nr_reclaimed > nr_attempted) 30382ab44f43SMel Gorman compact_pgdat(pgdat, order); 30392ab44f43SMel Gorman 30402ab44f43SMel Gorman /* 3041b8e83b94SMel Gorman * Raise priority if scanning rate is too low or there was no 3042b8e83b94SMel Gorman * progress in reclaiming pages 3043b8e83b94SMel Gorman */ 3044b8e83b94SMel Gorman if (raise_priority || !sc.nr_reclaimed) 3045b8e83b94SMel Gorman sc.priority--; 30469aa41348SMel Gorman } while (sc.priority >= 1 && 3047b8e83b94SMel Gorman !pgdat_balanced(pgdat, order, *classzone_idx)); 30481da177e4SLinus Torvalds 3049b8e83b94SMel Gorman out: 30500abdee2bSMel Gorman /* 30515515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 30520abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 30530abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 30540abdee2bSMel Gorman * was awake, order will remain at the higher level 30550abdee2bSMel Gorman */ 3056dc83edd9SMel Gorman *classzone_idx = end_zone; 30570abdee2bSMel Gorman return order; 30581da177e4SLinus Torvalds } 30591da177e4SLinus Torvalds 3060dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 3061f0bc0a60SKOSAKI Motohiro { 3062f0bc0a60SKOSAKI Motohiro long remaining = 0; 3063f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 3064f0bc0a60SKOSAKI Motohiro 3065f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 3066f0bc0a60SKOSAKI Motohiro return; 3067f0bc0a60SKOSAKI Motohiro 3068f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3069f0bc0a60SKOSAKI Motohiro 3070f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 30715515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3072f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 3073f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3074f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 3075f0bc0a60SKOSAKI Motohiro } 3076f0bc0a60SKOSAKI Motohiro 3077f0bc0a60SKOSAKI Motohiro /* 3078f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 3079f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 3080f0bc0a60SKOSAKI Motohiro */ 30815515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 3082f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 3083f0bc0a60SKOSAKI Motohiro 3084f0bc0a60SKOSAKI Motohiro /* 3085f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 3086f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 3087f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 3088f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 3089f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 3090f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 3091f0bc0a60SKOSAKI Motohiro */ 3092f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 30931c7e7f6cSAaditya Kumar 309462997027SMel Gorman /* 309562997027SMel Gorman * Compaction records what page blocks it recently failed to 309662997027SMel Gorman * isolate pages from and skips them in the future scanning. 309762997027SMel Gorman * When kswapd is going to sleep, it is reasonable to assume 309862997027SMel Gorman * that pages and compaction may succeed so reset the cache. 309962997027SMel Gorman */ 310062997027SMel Gorman reset_isolation_suitable(pgdat); 310162997027SMel Gorman 31021c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 3103f0bc0a60SKOSAKI Motohiro schedule(); 31041c7e7f6cSAaditya Kumar 3105f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 3106f0bc0a60SKOSAKI Motohiro } else { 3107f0bc0a60SKOSAKI Motohiro if (remaining) 3108f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 3109f0bc0a60SKOSAKI Motohiro else 3110f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 3111f0bc0a60SKOSAKI Motohiro } 3112f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 3113f0bc0a60SKOSAKI Motohiro } 3114f0bc0a60SKOSAKI Motohiro 31151da177e4SLinus Torvalds /* 31161da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 31171da177e4SLinus Torvalds * from the init process. 31181da177e4SLinus Torvalds * 31191da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 31201da177e4SLinus Torvalds * free memory available even if there is no other activity 31211da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 31221da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 31231da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 31241da177e4SLinus Torvalds * 31251da177e4SLinus Torvalds * If there are applications that are active memory-allocators 31261da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 31271da177e4SLinus Torvalds */ 31281da177e4SLinus Torvalds static int kswapd(void *p) 31291da177e4SLinus Torvalds { 3130215ddd66SMel Gorman unsigned long order, new_order; 3131d2ebd0f6SAlex,Shi unsigned balanced_order; 3132215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 3133d2ebd0f6SAlex,Shi int balanced_classzone_idx; 31341da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 31351da177e4SLinus Torvalds struct task_struct *tsk = current; 3136f0bc0a60SKOSAKI Motohiro 31371da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 31381da177e4SLinus Torvalds .reclaimed_slab = 0, 31391da177e4SLinus Torvalds }; 3140a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 31411da177e4SLinus Torvalds 3142cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 3143cf40bd16SNick Piggin 3144174596a0SRusty Russell if (!cpumask_empty(cpumask)) 3145c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 31461da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 31471da177e4SLinus Torvalds 31481da177e4SLinus Torvalds /* 31491da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 31501da177e4SLinus Torvalds * and that if we need more memory we should get access to it 31511da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 31521da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 31531da177e4SLinus Torvalds * 31541da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 31551da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 31561da177e4SLinus Torvalds * page out something else, and this flag essentially protects 31571da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 31581da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 31591da177e4SLinus Torvalds */ 3160930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 316183144186SRafael J. Wysocki set_freezable(); 31621da177e4SLinus Torvalds 3163215ddd66SMel Gorman order = new_order = 0; 3164d2ebd0f6SAlex,Shi balanced_order = 0; 3165215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 3166d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 31671da177e4SLinus Torvalds for ( ; ; ) { 31686f6313d4SJeff Liu bool ret; 31693e1d1d28SChristoph Lameter 3170215ddd66SMel Gorman /* 3171215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 3172215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 3173215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 3174215ddd66SMel Gorman */ 3175d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 3176d2ebd0f6SAlex,Shi balanced_order == new_order) { 31771da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 317899504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 31791da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 3180215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 3181215ddd66SMel Gorman } 3182215ddd66SMel Gorman 318399504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 31841da177e4SLinus Torvalds /* 31851da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 318699504748SMel Gorman * allocation or has tigher zone constraints 31871da177e4SLinus Torvalds */ 31881da177e4SLinus Torvalds order = new_order; 318999504748SMel Gorman classzone_idx = new_classzone_idx; 31901da177e4SLinus Torvalds } else { 3191d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 3192d2ebd0f6SAlex,Shi balanced_classzone_idx); 31931da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 319499504748SMel Gorman classzone_idx = pgdat->classzone_idx; 3195f0dfcde0SAlex,Shi new_order = order; 3196f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 31974d40502eSMel Gorman pgdat->kswapd_max_order = 0; 3198215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 31991da177e4SLinus Torvalds } 32001da177e4SLinus Torvalds 32018fe23e05SDavid Rientjes ret = try_to_freeze(); 32028fe23e05SDavid Rientjes if (kthread_should_stop()) 32038fe23e05SDavid Rientjes break; 32048fe23e05SDavid Rientjes 32058fe23e05SDavid Rientjes /* 32068fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 32078fe23e05SDavid Rientjes * after returning from the refrigerator 3208b1296cc4SRafael J. Wysocki */ 320933906bc5SMel Gorman if (!ret) { 321033906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 3211d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 3212d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 3213d2ebd0f6SAlex,Shi &balanced_classzone_idx); 32141da177e4SLinus Torvalds } 321533906bc5SMel Gorman } 3216b0a8cc58STakamori Yamaguchi 3217b0a8cc58STakamori Yamaguchi current->reclaim_state = NULL; 32181da177e4SLinus Torvalds return 0; 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds /* 32221da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 32231da177e4SLinus Torvalds */ 322499504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 32251da177e4SLinus Torvalds { 32261da177e4SLinus Torvalds pg_data_t *pgdat; 32271da177e4SLinus Torvalds 3228f3fe6512SCon Kolivas if (!populated_zone(zone)) 32291da177e4SLinus Torvalds return; 32301da177e4SLinus Torvalds 323102a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 32321da177e4SLinus Torvalds return; 323388f5acf8SMel Gorman pgdat = zone->zone_pgdat; 323499504748SMel Gorman if (pgdat->kswapd_max_order < order) { 323588f5acf8SMel Gorman pgdat->kswapd_max_order = order; 323699504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 323799504748SMel Gorman } 32388d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 32391da177e4SLinus Torvalds return; 324088f5acf8SMel Gorman if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) 324188f5acf8SMel Gorman return; 324288f5acf8SMel Gorman 324388f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 32448d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 32451da177e4SLinus Torvalds } 32461da177e4SLinus Torvalds 3247adea02a1SWu Fengguang /* 3248adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 3249adea02a1SWu Fengguang * The less reclaimable pages may be 3250adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 3251adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 3252adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 3253adea02a1SWu Fengguang */ 3254adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 32554f98a2feSRik van Riel { 3256adea02a1SWu Fengguang int nr; 3257adea02a1SWu Fengguang 3258adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 3259adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 3260adea02a1SWu Fengguang 3261ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 3262adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 3263adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 3264adea02a1SWu Fengguang 3265adea02a1SWu Fengguang return nr; 3266adea02a1SWu Fengguang } 3267adea02a1SWu Fengguang 3268adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone) 3269adea02a1SWu Fengguang { 3270adea02a1SWu Fengguang int nr; 3271adea02a1SWu Fengguang 3272adea02a1SWu Fengguang nr = zone_page_state(zone, NR_ACTIVE_FILE) + 3273adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_FILE); 3274adea02a1SWu Fengguang 3275ec8acf20SShaohua Li if (get_nr_swap_pages() > 0) 3276adea02a1SWu Fengguang nr += zone_page_state(zone, NR_ACTIVE_ANON) + 3277adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_ANON); 3278adea02a1SWu Fengguang 3279adea02a1SWu Fengguang return nr; 32804f98a2feSRik van Riel } 32814f98a2feSRik van Riel 3282c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 32831da177e4SLinus Torvalds /* 32847b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3285d6277db4SRafael J. Wysocki * freed pages. 3286d6277db4SRafael J. Wysocki * 3287d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3288d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3289d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 32901da177e4SLinus Torvalds */ 32917b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 32921da177e4SLinus Torvalds { 3293d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3294d6277db4SRafael J. Wysocki struct scan_control sc = { 32957b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 32967b51755cSKOSAKI Motohiro .may_swap = 1, 32977b51755cSKOSAKI Motohiro .may_unmap = 1, 3298d6277db4SRafael J. Wysocki .may_writepage = 1, 32997b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 33007b51755cSKOSAKI Motohiro .hibernation_mode = 1, 33017b51755cSKOSAKI Motohiro .order = 0, 33029e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 33031da177e4SLinus Torvalds }; 3304a09ed5e0SYing Han struct shrink_control shrink = { 3305a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3306a09ed5e0SYing Han }; 33077b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 33087b51755cSKOSAKI Motohiro struct task_struct *p = current; 33097b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 33101da177e4SLinus Torvalds 33117b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 33127b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3313d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 33147b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3315d6277db4SRafael J. Wysocki 3316a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 3317d6277db4SRafael J. Wysocki 33187b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 33197b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 33207b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3321d6277db4SRafael J. Wysocki 33227b51755cSKOSAKI Motohiro return nr_reclaimed; 33231da177e4SLinus Torvalds } 3324c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 33251da177e4SLinus Torvalds 33261da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 33271da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 33281da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 33291da177e4SLinus Torvalds restore their cpu bindings. */ 3330fcb35a9bSGreg Kroah-Hartman static int cpu_callback(struct notifier_block *nfb, unsigned long action, 3331fcb35a9bSGreg Kroah-Hartman void *hcpu) 33321da177e4SLinus Torvalds { 333358c0a4a7SYasunori Goto int nid; 33341da177e4SLinus Torvalds 33358bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 333648fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) { 3337c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3338a70f7302SRusty Russell const struct cpumask *mask; 3339a70f7302SRusty Russell 3340a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3341c5f59f08SMike Travis 33423e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 33431da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3344c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 33451da177e4SLinus Torvalds } 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds return NOTIFY_OK; 33481da177e4SLinus Torvalds } 33491da177e4SLinus Torvalds 33503218ae14SYasunori Goto /* 33513218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 33523218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 33533218ae14SYasunori Goto */ 33543218ae14SYasunori Goto int kswapd_run(int nid) 33553218ae14SYasunori Goto { 33563218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 33573218ae14SYasunori Goto int ret = 0; 33583218ae14SYasunori Goto 33593218ae14SYasunori Goto if (pgdat->kswapd) 33603218ae14SYasunori Goto return 0; 33613218ae14SYasunori Goto 33623218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 33633218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 33643218ae14SYasunori Goto /* failure at boot is fatal */ 33653218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 3366d5dc0ad9SGavin Shan pr_err("Failed to start kswapd on node %d\n", nid); 3367d5dc0ad9SGavin Shan ret = PTR_ERR(pgdat->kswapd); 3368d72515b8SXishi Qiu pgdat->kswapd = NULL; 33693218ae14SYasunori Goto } 33703218ae14SYasunori Goto return ret; 33713218ae14SYasunori Goto } 33723218ae14SYasunori Goto 33738fe23e05SDavid Rientjes /* 3374d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3375d8adde17SJiang Liu * hold lock_memory_hotplug(). 33768fe23e05SDavid Rientjes */ 33778fe23e05SDavid Rientjes void kswapd_stop(int nid) 33788fe23e05SDavid Rientjes { 33798fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 33808fe23e05SDavid Rientjes 3381d8adde17SJiang Liu if (kswapd) { 33828fe23e05SDavid Rientjes kthread_stop(kswapd); 3383d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3384d8adde17SJiang Liu } 33858fe23e05SDavid Rientjes } 33868fe23e05SDavid Rientjes 33871da177e4SLinus Torvalds static int __init kswapd_init(void) 33881da177e4SLinus Torvalds { 33893218ae14SYasunori Goto int nid; 339069e05944SAndrew Morton 33911da177e4SLinus Torvalds swap_setup(); 339248fb2e24SLai Jiangshan for_each_node_state(nid, N_MEMORY) 33933218ae14SYasunori Goto kswapd_run(nid); 33941da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 33951da177e4SLinus Torvalds return 0; 33961da177e4SLinus Torvalds } 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds module_init(kswapd_init) 33999eeff239SChristoph Lameter 34009eeff239SChristoph Lameter #ifdef CONFIG_NUMA 34019eeff239SChristoph Lameter /* 34029eeff239SChristoph Lameter * Zone reclaim mode 34039eeff239SChristoph Lameter * 34049eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 34059eeff239SChristoph Lameter * the watermarks. 34069eeff239SChristoph Lameter */ 34079eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 34089eeff239SChristoph Lameter 34091b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 34107d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 34111b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 34121b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 34131b2ffb78SChristoph Lameter 34149eeff239SChristoph Lameter /* 3415a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3416a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3417a92f7126SChristoph Lameter * a zone. 3418a92f7126SChristoph Lameter */ 3419a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3420a92f7126SChristoph Lameter 34219eeff239SChristoph Lameter /* 34229614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 34239614634fSChristoph Lameter * occur. 34249614634fSChristoph Lameter */ 34259614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 34269614634fSChristoph Lameter 34279614634fSChristoph Lameter /* 34280ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 34290ff38490SChristoph Lameter * slab reclaim needs to occur. 34300ff38490SChristoph Lameter */ 34310ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 34320ff38490SChristoph Lameter 343390afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 343490afa5deSMel Gorman { 343590afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 343690afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 343790afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 343890afa5deSMel Gorman 343990afa5deSMel Gorman /* 344090afa5deSMel Gorman * It's possible for there to be more file mapped pages than 344190afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 344290afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 344390afa5deSMel Gorman */ 344490afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 344590afa5deSMel Gorman } 344690afa5deSMel Gorman 344790afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 344890afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 344990afa5deSMel Gorman { 345090afa5deSMel Gorman long nr_pagecache_reclaimable; 345190afa5deSMel Gorman long delta = 0; 345290afa5deSMel Gorman 345390afa5deSMel Gorman /* 345490afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 345590afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 345690afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 345790afa5deSMel Gorman * a better estimate 345890afa5deSMel Gorman */ 345990afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 346090afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 346190afa5deSMel Gorman else 346290afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 346390afa5deSMel Gorman 346490afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 346590afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 346690afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 346790afa5deSMel Gorman 346890afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 346990afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 347090afa5deSMel Gorman delta = nr_pagecache_reclaimable; 347190afa5deSMel Gorman 347290afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 347390afa5deSMel Gorman } 347490afa5deSMel Gorman 34750ff38490SChristoph Lameter /* 34769eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 34779eeff239SChristoph Lameter */ 3478179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 34799eeff239SChristoph Lameter { 34807fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 348169e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 34829eeff239SChristoph Lameter struct task_struct *p = current; 34839eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3484179e9639SAndrew Morton struct scan_control sc = { 3485179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3486a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 34872e2e4259SKOSAKI Motohiro .may_swap = 1, 348862b726c1SAndrew Morton .nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX), 348921caf2fcSMing Lei .gfp_mask = (gfp_mask = memalloc_noio_flags(gfp_mask)), 3490bd2f6199SJohannes Weiner .order = order, 34919e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3492179e9639SAndrew Morton }; 3493a09ed5e0SYing Han struct shrink_control shrink = { 3494a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3495a09ed5e0SYing Han }; 349615748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 34979eeff239SChristoph Lameter 34989eeff239SChristoph Lameter cond_resched(); 3499d4f7796eSChristoph Lameter /* 3500d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3501d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3502d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3503d4f7796eSChristoph Lameter */ 3504d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 350576ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 35069eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 35079eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3508c84db23cSChristoph Lameter 350990afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3510a92f7126SChristoph Lameter /* 35110ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 35120ff38490SChristoph Lameter * priorities until we have enough memory freed. 3513a92f7126SChristoph Lameter */ 3514a92f7126SChristoph Lameter do { 35159e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 35169e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 35170ff38490SChristoph Lameter } 3518a92f7126SChristoph Lameter 351915748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 352015748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 35212a16e3f4SChristoph Lameter /* 35227fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 35230ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 35240ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 35250ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 35260ff38490SChristoph Lameter * pages. 35272a16e3f4SChristoph Lameter * 35280ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 35290ff38490SChristoph Lameter * take a long time. 35302a16e3f4SChristoph Lameter */ 35314dc4b3d9SKOSAKI Motohiro for (;;) { 35324dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 35334dc4b3d9SKOSAKI Motohiro 35344dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 35351495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 35364dc4b3d9SKOSAKI Motohiro break; 35374dc4b3d9SKOSAKI Motohiro 35384dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 35394dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 35404dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 35414dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 35424dc4b3d9SKOSAKI Motohiro break; 35434dc4b3d9SKOSAKI Motohiro } 354483e33a47SChristoph Lameter 354583e33a47SChristoph Lameter /* 354683e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 354783e33a47SChristoph Lameter * reclaimed from this zone. 354883e33a47SChristoph Lameter */ 354915748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 355015748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 355115748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 35522a16e3f4SChristoph Lameter } 35532a16e3f4SChristoph Lameter 35549eeff239SChristoph Lameter p->reclaim_state = NULL; 3555d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 355676ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3557a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 35589eeff239SChristoph Lameter } 3559179e9639SAndrew Morton 3560179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3561179e9639SAndrew Morton { 3562179e9639SAndrew Morton int node_id; 3563d773ed6bSDavid Rientjes int ret; 3564179e9639SAndrew Morton 3565179e9639SAndrew Morton /* 35660ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 35670ff38490SChristoph Lameter * slab pages if we are over the defined limits. 356834aa1330SChristoph Lameter * 35699614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 35709614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 35719614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 35729614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 35739614634fSChristoph Lameter * unmapped file backed pages. 3574179e9639SAndrew Morton */ 357590afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 357690afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3577fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3578179e9639SAndrew Morton 357993e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable) 3580fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3581d773ed6bSDavid Rientjes 3582179e9639SAndrew Morton /* 3583d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3584179e9639SAndrew Morton */ 3585d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3586fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3587179e9639SAndrew Morton 3588179e9639SAndrew Morton /* 3589179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3590179e9639SAndrew Morton * have associated processors. This will favor the local processor 3591179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3592179e9639SAndrew Morton * as wide as possible. 3593179e9639SAndrew Morton */ 359489fa3024SChristoph Lameter node_id = zone_to_nid(zone); 359537c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3596fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3597d773ed6bSDavid Rientjes 3598d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3599fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3600fa5e084eSMel Gorman 3601d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3602d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3603d773ed6bSDavid Rientjes 360424cf7251SMel Gorman if (!ret) 360524cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 360624cf7251SMel Gorman 3607d773ed6bSDavid Rientjes return ret; 3608179e9639SAndrew Morton } 36099eeff239SChristoph Lameter #endif 3610894bc310SLee Schermerhorn 3611894bc310SLee Schermerhorn /* 3612894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3613894bc310SLee Schermerhorn * @page: the page to test 3614894bc310SLee Schermerhorn * 3615894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 361639b5f29aSHugh Dickins * lists vs unevictable list. 3617894bc310SLee Schermerhorn * 3618894bc310SLee Schermerhorn * Reasons page might not be evictable: 3619ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3620b291f000SNick Piggin * (2) page is part of an mlocked VMA 3621ba9ddf49SLee Schermerhorn * 3622894bc310SLee Schermerhorn */ 362339b5f29aSHugh Dickins int page_evictable(struct page *page) 3624894bc310SLee Schermerhorn { 362539b5f29aSHugh Dickins return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page); 3626894bc310SLee Schermerhorn } 362789e004eaSLee Schermerhorn 362885046579SHugh Dickins #ifdef CONFIG_SHMEM 362989e004eaSLee Schermerhorn /** 363024513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 363124513264SHugh Dickins * @pages: array of pages to check 363224513264SHugh Dickins * @nr_pages: number of pages to check 363389e004eaSLee Schermerhorn * 363424513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 363585046579SHugh Dickins * 363685046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 363789e004eaSLee Schermerhorn */ 363824513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 363989e004eaSLee Schermerhorn { 3640925b7673SJohannes Weiner struct lruvec *lruvec; 364124513264SHugh Dickins struct zone *zone = NULL; 364224513264SHugh Dickins int pgscanned = 0; 364324513264SHugh Dickins int pgrescued = 0; 364489e004eaSLee Schermerhorn int i; 364589e004eaSLee Schermerhorn 364624513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 364724513264SHugh Dickins struct page *page = pages[i]; 364824513264SHugh Dickins struct zone *pagezone; 364989e004eaSLee Schermerhorn 365024513264SHugh Dickins pgscanned++; 365124513264SHugh Dickins pagezone = page_zone(page); 365289e004eaSLee Schermerhorn if (pagezone != zone) { 365389e004eaSLee Schermerhorn if (zone) 365489e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 365589e004eaSLee Schermerhorn zone = pagezone; 365689e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 365789e004eaSLee Schermerhorn } 3658fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 365989e004eaSLee Schermerhorn 366024513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 366124513264SHugh Dickins continue; 366289e004eaSLee Schermerhorn 366339b5f29aSHugh Dickins if (page_evictable(page)) { 366424513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 366524513264SHugh Dickins 366624513264SHugh Dickins VM_BUG_ON(PageActive(page)); 366724513264SHugh Dickins ClearPageUnevictable(page); 3668fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3669fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 367024513264SHugh Dickins pgrescued++; 367189e004eaSLee Schermerhorn } 367289e004eaSLee Schermerhorn } 367324513264SHugh Dickins 367424513264SHugh Dickins if (zone) { 367524513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 367624513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 367724513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 367824513264SHugh Dickins } 367985046579SHugh Dickins } 368085046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3681af936a16SLee Schermerhorn 3682264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3683af936a16SLee Schermerhorn { 3684264e56d8SJohannes Weiner printk_once(KERN_WARNING 368525bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3686264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 368725bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 368825bd91bdSKOSAKI Motohiro current->comm); 3689af936a16SLee Schermerhorn } 3690af936a16SLee Schermerhorn 3691af936a16SLee Schermerhorn /* 3692af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3693af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3694af936a16SLee Schermerhorn */ 3695af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3696af936a16SLee Schermerhorn 3697af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 36988d65af78SAlexey Dobriyan void __user *buffer, 3699af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3700af936a16SLee Schermerhorn { 3701264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 37028d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3703af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3704af936a16SLee Schermerhorn return 0; 3705af936a16SLee Schermerhorn } 3706af936a16SLee Schermerhorn 3707e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3708af936a16SLee Schermerhorn /* 3709af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3710af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3711af936a16SLee Schermerhorn */ 3712af936a16SLee Schermerhorn 371310fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 371410fbcf4cSKay Sievers struct device_attribute *attr, 3715af936a16SLee Schermerhorn char *buf) 3716af936a16SLee Schermerhorn { 3717264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3718af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3719af936a16SLee Schermerhorn } 3720af936a16SLee Schermerhorn 372110fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 372210fbcf4cSKay Sievers struct device_attribute *attr, 3723af936a16SLee Schermerhorn const char *buf, size_t count) 3724af936a16SLee Schermerhorn { 3725264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3726af936a16SLee Schermerhorn return 1; 3727af936a16SLee Schermerhorn } 3728af936a16SLee Schermerhorn 3729af936a16SLee Schermerhorn 373010fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3731af936a16SLee Schermerhorn read_scan_unevictable_node, 3732af936a16SLee Schermerhorn write_scan_unevictable_node); 3733af936a16SLee Schermerhorn 3734af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3735af936a16SLee Schermerhorn { 373610fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3737af936a16SLee Schermerhorn } 3738af936a16SLee Schermerhorn 3739af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3740af936a16SLee Schermerhorn { 374110fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3742af936a16SLee Schermerhorn } 3743e4455abbSThadeu Lima de Souza Cascardo #endif 3744