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> 22e129b5c2SAndrew Morton #include <linux/vmstat.h> 231da177e4SLinus Torvalds #include <linux/file.h> 241da177e4SLinus Torvalds #include <linux/writeback.h> 251da177e4SLinus Torvalds #include <linux/blkdev.h> 261da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 271da177e4SLinus Torvalds buffer_heads_over_limit */ 281da177e4SLinus Torvalds #include <linux/mm_inline.h> 291da177e4SLinus Torvalds #include <linux/backing-dev.h> 301da177e4SLinus Torvalds #include <linux/rmap.h> 311da177e4SLinus Torvalds #include <linux/topology.h> 321da177e4SLinus Torvalds #include <linux/cpu.h> 331da177e4SLinus Torvalds #include <linux/cpuset.h> 343e7d3449SMel Gorman #include <linux/compaction.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/rwsem.h> 37248a0301SRafael J. Wysocki #include <linux/delay.h> 383218ae14SYasunori Goto #include <linux/kthread.h> 397dfb7103SNigel Cunningham #include <linux/freezer.h> 4066e1707bSBalbir Singh #include <linux/memcontrol.h> 41873b4771SKeika Kobayashi #include <linux/delayacct.h> 42af936a16SLee Schermerhorn #include <linux/sysctl.h> 43929bea7cSKOSAKI Motohiro #include <linux/oom.h> 44268bb0ceSLinus Torvalds #include <linux/prefetch.h> 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #include <asm/tlbflush.h> 471da177e4SLinus Torvalds #include <asm/div64.h> 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #include <linux/swapops.h> 501da177e4SLinus Torvalds 510f8053a5SNick Piggin #include "internal.h" 520f8053a5SNick Piggin 5333906bc5SMel Gorman #define CREATE_TRACE_POINTS 5433906bc5SMel Gorman #include <trace/events/vmscan.h> 5533906bc5SMel Gorman 561da177e4SLinus Torvalds struct scan_control { 571da177e4SLinus Torvalds /* Incremented by the number of inactive pages that were scanned */ 581da177e4SLinus Torvalds unsigned long nr_scanned; 591da177e4SLinus Torvalds 60a79311c1SRik van Riel /* Number of pages freed so far during a call to shrink_zones() */ 61a79311c1SRik van Riel unsigned long nr_reclaimed; 62a79311c1SRik van Riel 6322fba335SKOSAKI Motohiro /* How many pages shrink_list() should reclaim */ 6422fba335SKOSAKI Motohiro unsigned long nr_to_reclaim; 6522fba335SKOSAKI Motohiro 667b51755cSKOSAKI Motohiro unsigned long hibernation_mode; 677b51755cSKOSAKI Motohiro 681da177e4SLinus Torvalds /* This context's GFP mask */ 696daa0e28SAl Viro gfp_t gfp_mask; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds int may_writepage; 721da177e4SLinus Torvalds 73a6dc60f8SJohannes Weiner /* Can mapped pages be reclaimed? */ 74a6dc60f8SJohannes Weiner int may_unmap; 75f1fd1067SChristoph Lameter 762e2e4259SKOSAKI Motohiro /* Can pages be swapped as part of reclaim? */ 772e2e4259SKOSAKI Motohiro int may_swap; 782e2e4259SKOSAKI Motohiro 795ad333ebSAndy Whitcroft int order; 8066e1707bSBalbir Singh 819e3b2f8cSKonstantin Khlebnikov /* Scan (total_size >> priority) pages at once */ 829e3b2f8cSKonstantin Khlebnikov int priority; 839e3b2f8cSKonstantin Khlebnikov 845f53e762SKOSAKI Motohiro /* 85f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 86f16015fbSJohannes Weiner * primary target of this reclaim invocation. 87f16015fbSJohannes Weiner */ 88f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8966e1707bSBalbir Singh 90327c0e96SKAMEZAWA Hiroyuki /* 91327c0e96SKAMEZAWA Hiroyuki * Nodemask of nodes allowed by the caller. If NULL, all nodes 92327c0e96SKAMEZAWA Hiroyuki * are scanned. 93327c0e96SKAMEZAWA Hiroyuki */ 94327c0e96SKAMEZAWA Hiroyuki nodemask_t *nodemask; 951da177e4SLinus Torvalds }; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1001da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1011da177e4SLinus Torvalds do { \ 1021da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1031da177e4SLinus Torvalds struct page *prev; \ 1041da177e4SLinus Torvalds \ 1051da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1061da177e4SLinus Torvalds prefetch(&prev->_field); \ 1071da177e4SLinus Torvalds } \ 1081da177e4SLinus Torvalds } while (0) 1091da177e4SLinus Torvalds #else 1101da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1111da177e4SLinus Torvalds #endif 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1141da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1151da177e4SLinus Torvalds do { \ 1161da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1171da177e4SLinus Torvalds struct page *prev; \ 1181da177e4SLinus Torvalds \ 1191da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1201da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1211da177e4SLinus Torvalds } \ 1221da177e4SLinus Torvalds } while (0) 1231da177e4SLinus Torvalds #else 1241da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1251da177e4SLinus Torvalds #endif 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /* 1281da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds int vm_swappiness = 60; 131bd1e22b8SAndrew Morton long vm_total_pages; /* The total number of pages which the VM controls */ 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1341da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1351da177e4SLinus Torvalds 136c255a458SAndrew Morton #ifdef CONFIG_MEMCG 13789b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 13889b5fae5SJohannes Weiner { 139f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14089b5fae5SJohannes Weiner } 14191a45470SKAMEZAWA Hiroyuki #else 14289b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14389b5fae5SJohannes Weiner { 14489b5fae5SJohannes Weiner return true; 14589b5fae5SJohannes Weiner } 14691a45470SKAMEZAWA Hiroyuki #endif 14791a45470SKAMEZAWA Hiroyuki 1484d7dcca2SHugh Dickins static unsigned long get_lru_size(struct lruvec *lruvec, enum lru_list lru) 149c9f299d9SKOSAKI Motohiro { 150c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1514d7dcca2SHugh Dickins return mem_cgroup_get_lru_size(lruvec, lru); 152a3d8e054SKOSAKI Motohiro 153074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 154c9f299d9SKOSAKI Motohiro } 155c9f299d9SKOSAKI Motohiro 1561da177e4SLinus Torvalds /* 1571da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1581da177e4SLinus Torvalds */ 1598e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker) 1601da177e4SLinus Torvalds { 16183aeeadaSKonstantin Khlebnikov atomic_long_set(&shrinker->nr_in_batch, 0); 1621da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1631da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1641da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1651da177e4SLinus Torvalds } 1668e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * Remove one 1701da177e4SLinus Torvalds */ 1718e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1741da177e4SLinus Torvalds list_del(&shrinker->list); 1751da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1761da177e4SLinus Torvalds } 1778e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 1781da177e4SLinus Torvalds 1791495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker, 1801495f230SYing Han struct shrink_control *sc, 1811495f230SYing Han unsigned long nr_to_scan) 1821495f230SYing Han { 1831495f230SYing Han sc->nr_to_scan = nr_to_scan; 1841495f230SYing Han return (*shrinker->shrink)(shrinker, sc); 1851495f230SYing Han } 1861495f230SYing Han 1871da177e4SLinus Torvalds #define SHRINK_BATCH 128 1881da177e4SLinus Torvalds /* 1891da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 1901da177e4SLinus Torvalds * 1911da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 1921da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 1931da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 1941da177e4SLinus Torvalds * generated by these structures. 1951da177e4SLinus Torvalds * 196183ff22bSSimon Arlott * If the vm encountered mapped pages on the LRU it increase the pressure on 1971da177e4SLinus Torvalds * slab to avoid swapping. 1981da177e4SLinus Torvalds * 1991da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 2001da177e4SLinus Torvalds * 2011da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 2021da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 2031da177e4SLinus Torvalds * slab reclaim versus page reclaim. 204b15e0905Sakpm@osdl.org * 205b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 2061da177e4SLinus Torvalds */ 207a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink, 2081495f230SYing Han unsigned long nr_pages_scanned, 20969e05944SAndrew Morton unsigned long lru_pages) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds struct shrinker *shrinker; 21269e05944SAndrew Morton unsigned long ret = 0; 2131da177e4SLinus Torvalds 2141495f230SYing Han if (nr_pages_scanned == 0) 2151495f230SYing Han nr_pages_scanned = SWAP_CLUSTER_MAX; 2161da177e4SLinus Torvalds 217f06590bdSMinchan Kim if (!down_read_trylock(&shrinker_rwsem)) { 218f06590bdSMinchan Kim /* Assume we'll be able to shrink next time */ 219f06590bdSMinchan Kim ret = 1; 220f06590bdSMinchan Kim goto out; 221f06590bdSMinchan Kim } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 2241da177e4SLinus Torvalds unsigned long long delta; 225635697c6SKonstantin Khlebnikov long total_scan; 226635697c6SKonstantin Khlebnikov long max_pass; 22709576073SDave Chinner int shrink_ret = 0; 228acf92b48SDave Chinner long nr; 229acf92b48SDave Chinner long new_nr; 230e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 231e9299f50SDave Chinner : SHRINK_BATCH; 2321da177e4SLinus Torvalds 233635697c6SKonstantin Khlebnikov max_pass = do_shrinker_shrink(shrinker, shrink, 0); 234635697c6SKonstantin Khlebnikov if (max_pass <= 0) 235635697c6SKonstantin Khlebnikov continue; 236635697c6SKonstantin Khlebnikov 237acf92b48SDave Chinner /* 238acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 239acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 240acf92b48SDave Chinner * don't also do this scanning work. 241acf92b48SDave Chinner */ 24283aeeadaSKonstantin Khlebnikov nr = atomic_long_xchg(&shrinker->nr_in_batch, 0); 243acf92b48SDave Chinner 244acf92b48SDave Chinner total_scan = nr; 2451495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 246ea164d73SAndrea Arcangeli delta *= max_pass; 2471da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 248acf92b48SDave Chinner total_scan += delta; 249acf92b48SDave Chinner if (total_scan < 0) { 25088c3bd70SDavid Rientjes printk(KERN_ERR "shrink_slab: %pF negative objects to " 25188c3bd70SDavid Rientjes "delete nr=%ld\n", 252acf92b48SDave Chinner shrinker->shrink, total_scan); 253acf92b48SDave Chinner total_scan = max_pass; 254ea164d73SAndrea Arcangeli } 255ea164d73SAndrea Arcangeli 256ea164d73SAndrea Arcangeli /* 2573567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2583567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2593567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2603567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2613567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2623567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2633567b59aSDave Chinner * memory. 2643567b59aSDave Chinner * 2653567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2663567b59aSDave Chinner * a large delta change is calculated directly. 2673567b59aSDave Chinner */ 2683567b59aSDave Chinner if (delta < max_pass / 4) 2693567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2703567b59aSDave Chinner 2713567b59aSDave Chinner /* 272ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 273ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 274ea164d73SAndrea Arcangeli * freeable entries. 275ea164d73SAndrea Arcangeli */ 276acf92b48SDave Chinner if (total_scan > max_pass * 2) 277acf92b48SDave Chinner total_scan = max_pass * 2; 2781da177e4SLinus Torvalds 279acf92b48SDave Chinner trace_mm_shrink_slab_start(shrinker, shrink, nr, 28009576073SDave Chinner nr_pages_scanned, lru_pages, 28109576073SDave Chinner max_pass, delta, total_scan); 28209576073SDave Chinner 283e9299f50SDave Chinner while (total_scan >= batch_size) { 284b15e0905Sakpm@osdl.org int nr_before; 2851da177e4SLinus Torvalds 2861495f230SYing Han nr_before = do_shrinker_shrink(shrinker, shrink, 0); 2871495f230SYing Han shrink_ret = do_shrinker_shrink(shrinker, shrink, 288e9299f50SDave Chinner batch_size); 2891da177e4SLinus Torvalds if (shrink_ret == -1) 2901da177e4SLinus Torvalds break; 291b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 292b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 293e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 294e9299f50SDave Chinner total_scan -= batch_size; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds cond_resched(); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds 299acf92b48SDave Chinner /* 300acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 301acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 302acf92b48SDave Chinner * scan, there is no need to do an update. 303acf92b48SDave Chinner */ 30483aeeadaSKonstantin Khlebnikov if (total_scan > 0) 30583aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 30683aeeadaSKonstantin Khlebnikov &shrinker->nr_in_batch); 30783aeeadaSKonstantin Khlebnikov else 30883aeeadaSKonstantin Khlebnikov new_nr = atomic_long_read(&shrinker->nr_in_batch); 309acf92b48SDave Chinner 310acf92b48SDave Chinner trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds up_read(&shrinker_rwsem); 313f06590bdSMinchan Kim out: 314f06590bdSMinchan Kim cond_resched(); 315b15e0905Sakpm@osdl.org return ret; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3191da177e4SLinus Torvalds { 320ceddc3a5SJohannes Weiner /* 321ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 322ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 323ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 324ceddc3a5SJohannes Weiner */ 325edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3287d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3297d3579e8SKOSAKI Motohiro struct scan_control *sc) 3301da177e4SLinus Torvalds { 331930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3321da177e4SLinus Torvalds return 1; 3331da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3341da177e4SLinus Torvalds return 1; 3351da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3361da177e4SLinus Torvalds return 1; 3371da177e4SLinus Torvalds return 0; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3421da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3431da177e4SLinus Torvalds * fsync(), msync() or close(). 3441da177e4SLinus Torvalds * 3451da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 3461da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 3471da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 3481da177e4SLinus Torvalds * 3491da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 3501da177e4SLinus Torvalds * __GFP_FS. 3511da177e4SLinus Torvalds */ 3521da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 3531da177e4SLinus Torvalds struct page *page, int error) 3541da177e4SLinus Torvalds { 3557eaceaccSJens Axboe lock_page(page); 3563e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 3573e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 3581da177e4SLinus Torvalds unlock_page(page); 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 36104e62a29SChristoph Lameter /* possible outcome of pageout() */ 36204e62a29SChristoph Lameter typedef enum { 36304e62a29SChristoph Lameter /* failed to write page out, page is locked */ 36404e62a29SChristoph Lameter PAGE_KEEP, 36504e62a29SChristoph Lameter /* move page to the active list, page is locked */ 36604e62a29SChristoph Lameter PAGE_ACTIVATE, 36704e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 36804e62a29SChristoph Lameter PAGE_SUCCESS, 36904e62a29SChristoph Lameter /* page is clean and locked */ 37004e62a29SChristoph Lameter PAGE_CLEAN, 37104e62a29SChristoph Lameter } pageout_t; 37204e62a29SChristoph Lameter 3731da177e4SLinus Torvalds /* 3741742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3751742f19fSAndrew Morton * Calls ->writepage(). 3761da177e4SLinus Torvalds */ 377c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 3787d3579e8SKOSAKI Motohiro struct scan_control *sc) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds /* 3811da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3821da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3831da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3841da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3851da177e4SLinus Torvalds * PagePrivate for that. 3861da177e4SLinus Torvalds * 3876aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 3881da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 3891da177e4SLinus Torvalds * will block. 3901da177e4SLinus Torvalds * 3911da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 3921da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 3931da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 3941da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 3951da177e4SLinus Torvalds */ 3961da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 3971da177e4SLinus Torvalds return PAGE_KEEP; 3981da177e4SLinus Torvalds if (!mapping) { 3991da177e4SLinus Torvalds /* 4001da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4011da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4021da177e4SLinus Torvalds */ 403266cf658SDavid Howells if (page_has_private(page)) { 4041da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4051da177e4SLinus Torvalds ClearPageDirty(page); 406d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4071da177e4SLinus Torvalds return PAGE_CLEAN; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds return PAGE_KEEP; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4131da177e4SLinus Torvalds return PAGE_ACTIVATE; 4140e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4151da177e4SLinus Torvalds return PAGE_KEEP; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4181da177e4SLinus Torvalds int res; 4191da177e4SLinus Torvalds struct writeback_control wbc = { 4201da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4211da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 422111ebb6eSOGAWA Hirofumi .range_start = 0, 423111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4241da177e4SLinus Torvalds .for_reclaim = 1, 4251da177e4SLinus Torvalds }; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds SetPageReclaim(page); 4281da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4291da177e4SLinus Torvalds if (res < 0) 4301da177e4SLinus Torvalds handle_write_error(mapping, page, res); 431994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4321da177e4SLinus Torvalds ClearPageReclaim(page); 4331da177e4SLinus Torvalds return PAGE_ACTIVATE; 4341da177e4SLinus Torvalds } 435c661b078SAndy Whitcroft 4361da177e4SLinus Torvalds if (!PageWriteback(page)) { 4371da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4381da177e4SLinus Torvalds ClearPageReclaim(page); 4391da177e4SLinus Torvalds } 44023b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 441e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4421da177e4SLinus Torvalds return PAGE_SUCCESS; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds return PAGE_CLEAN; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 448a649fd92SAndrew Morton /* 449e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 450e286781dSNick Piggin * gets returned with a refcount of 0. 451a649fd92SAndrew Morton */ 452e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 45349d2e9ccSChristoph Lameter { 45428e4d965SNick Piggin BUG_ON(!PageLocked(page)); 45528e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 45649d2e9ccSChristoph Lameter 45719fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 45849d2e9ccSChristoph Lameter /* 4590fd0e6b0SNick Piggin * The non racy check for a busy page. 4600fd0e6b0SNick Piggin * 4610fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 4620fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 4630fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 4640fd0e6b0SNick Piggin * here, then the following race may occur: 4650fd0e6b0SNick Piggin * 4660fd0e6b0SNick Piggin * get_user_pages(&page); 4670fd0e6b0SNick Piggin * [user mapping goes away] 4680fd0e6b0SNick Piggin * write_to(page); 4690fd0e6b0SNick Piggin * !PageDirty(page) [good] 4700fd0e6b0SNick Piggin * SetPageDirty(page); 4710fd0e6b0SNick Piggin * put_page(page); 4720fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 4730fd0e6b0SNick Piggin * 4740fd0e6b0SNick Piggin * [oops, our write_to data is lost] 4750fd0e6b0SNick Piggin * 4760fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 4770fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 4780fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 4790fd0e6b0SNick Piggin * 4800fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 4810fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 48249d2e9ccSChristoph Lameter */ 483e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 48449d2e9ccSChristoph Lameter goto cannot_free; 485e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 486e286781dSNick Piggin if (unlikely(PageDirty(page))) { 487e286781dSNick Piggin page_unfreeze_refs(page, 2); 48849d2e9ccSChristoph Lameter goto cannot_free; 489e286781dSNick Piggin } 49049d2e9ccSChristoph Lameter 49149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 49249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 49349d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 49419fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 495cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 496e286781dSNick Piggin } else { 4976072d13cSLinus Torvalds void (*freepage)(struct page *); 4986072d13cSLinus Torvalds 4996072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5006072d13cSLinus Torvalds 501e64a782fSMinchan Kim __delete_from_page_cache(page); 50219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 503e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5046072d13cSLinus Torvalds 5056072d13cSLinus Torvalds if (freepage != NULL) 5066072d13cSLinus Torvalds freepage(page); 507e286781dSNick Piggin } 508e286781dSNick Piggin 50949d2e9ccSChristoph Lameter return 1; 51049d2e9ccSChristoph Lameter 51149d2e9ccSChristoph Lameter cannot_free: 51219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 51349d2e9ccSChristoph Lameter return 0; 51449d2e9ccSChristoph Lameter } 51549d2e9ccSChristoph Lameter 5161da177e4SLinus Torvalds /* 517e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 518e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 519e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 520e286781dSNick Piggin * this page. 521e286781dSNick Piggin */ 522e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 523e286781dSNick Piggin { 524e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 525e286781dSNick Piggin /* 526e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 527e286781dSNick Piggin * drops the pagecache ref for us without requiring another 528e286781dSNick Piggin * atomic operation. 529e286781dSNick Piggin */ 530e286781dSNick Piggin page_unfreeze_refs(page, 1); 531e286781dSNick Piggin return 1; 532e286781dSNick Piggin } 533e286781dSNick Piggin return 0; 534e286781dSNick Piggin } 535e286781dSNick Piggin 536894bc310SLee Schermerhorn /** 537894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 538894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 539894bc310SLee Schermerhorn * 540894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 541894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 542894bc310SLee Schermerhorn * 543894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 544894bc310SLee Schermerhorn */ 545894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 546894bc310SLee Schermerhorn { 547894bc310SLee Schermerhorn int lru; 548894bc310SLee Schermerhorn int active = !!TestClearPageActive(page); 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 556894bc310SLee Schermerhorn if (page_evictable(page, NULL)) { 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 */ 563401a8e1cSJohannes Weiner lru = active + page_lru_base_type(page); 564894bc310SLee Schermerhorn lru_cache_add_lru(page, lru); 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 */ 590894bc310SLee Schermerhorn if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { 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 671e286781dSNick Piggin /* 6721742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 6731da177e4SLinus Torvalds */ 6741742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 6756a18adb3SKonstantin Khlebnikov struct zone *zone, 676f84f6e2bSMel Gorman struct scan_control *sc, 67792df3a72SMel Gorman unsigned long *ret_nr_dirty, 67892df3a72SMel Gorman unsigned long *ret_nr_writeback) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds LIST_HEAD(ret_pages); 681abe4c3b5SMel Gorman LIST_HEAD(free_pages); 6821da177e4SLinus Torvalds int pgactivate = 0; 6830e093d99SMel Gorman unsigned long nr_dirty = 0; 6840e093d99SMel Gorman unsigned long nr_congested = 0; 68505ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 68692df3a72SMel Gorman unsigned long nr_writeback = 0; 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds cond_resched(); 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds while (!list_empty(page_list)) { 691dfc8d636SJohannes Weiner enum page_references references; 6921da177e4SLinus Torvalds struct address_space *mapping; 6931da177e4SLinus Torvalds struct page *page; 6941da177e4SLinus Torvalds int may_enter_fs; 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds cond_resched(); 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds page = lru_to_page(page_list); 6991da177e4SLinus Torvalds list_del(&page->lru); 7001da177e4SLinus Torvalds 701529ae9aaSNick Piggin if (!trylock_page(page)) 7021da177e4SLinus Torvalds goto keep; 7031da177e4SLinus Torvalds 704725d704eSNick Piggin VM_BUG_ON(PageActive(page)); 7056a18adb3SKonstantin Khlebnikov VM_BUG_ON(page_zone(page) != zone); 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds sc->nr_scanned++; 70880e43426SChristoph Lameter 709b291f000SNick Piggin if (unlikely(!page_evictable(page, NULL))) 710b291f000SNick Piggin goto cull_mlocked; 711894bc310SLee Schermerhorn 712a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 71380e43426SChristoph Lameter goto keep_locked; 71480e43426SChristoph Lameter 7151da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 7161da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 7171da177e4SLinus Torvalds sc->nr_scanned++; 7181da177e4SLinus Torvalds 719c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 720c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 721c661b078SAndy Whitcroft 722c661b078SAndy Whitcroft if (PageWriteback(page)) { 723e62e384eSMichal Hocko /* 724e62e384eSMichal Hocko * memcg doesn't have any dirty pages throttling so we 725e62e384eSMichal Hocko * could easily OOM just because too many pages are in 726*c3b94f44SHugh Dickins * writeback and there is nothing else to reclaim. 727e62e384eSMichal Hocko * 728*c3b94f44SHugh Dickins * Check __GFP_IO, certainly because a loop driver 729e62e384eSMichal Hocko * thread might enter reclaim, and deadlock if it waits 730e62e384eSMichal Hocko * on a page for which it is needed to do the write 731e62e384eSMichal Hocko * (loop masks off __GFP_IO|__GFP_FS for this reason); 732e62e384eSMichal Hocko * but more thought would probably show more reasons. 733*c3b94f44SHugh Dickins * 734*c3b94f44SHugh Dickins * Don't require __GFP_FS, since we're not going into 735*c3b94f44SHugh Dickins * the FS, just waiting on its writeback completion. 736*c3b94f44SHugh Dickins * Worryingly, ext4 gfs2 and xfs allocate pages with 737*c3b94f44SHugh Dickins * grab_cache_page_write_begin(,,AOP_FLAG_NOFS), so 738*c3b94f44SHugh Dickins * testing may_enter_fs here is liable to OOM on them. 739e62e384eSMichal Hocko */ 740*c3b94f44SHugh Dickins if (global_reclaim(sc) || 741*c3b94f44SHugh Dickins !PageReclaim(page) || !(sc->gfp_mask & __GFP_IO)) { 742*c3b94f44SHugh Dickins /* 743*c3b94f44SHugh Dickins * This is slightly racy - end_page_writeback() 744*c3b94f44SHugh Dickins * might have just cleared PageReclaim, then 745*c3b94f44SHugh Dickins * setting PageReclaim here end up interpreted 746*c3b94f44SHugh Dickins * as PageReadahead - but that does not matter 747*c3b94f44SHugh Dickins * enough to care. What we do want is for this 748*c3b94f44SHugh Dickins * page to have PageReclaim set next time memcg 749*c3b94f44SHugh Dickins * reclaim reaches the tests above, so it will 750*c3b94f44SHugh Dickins * then wait_on_page_writeback() to avoid OOM; 751*c3b94f44SHugh Dickins * and it's also appropriate in global reclaim. 752*c3b94f44SHugh Dickins */ 753*c3b94f44SHugh Dickins SetPageReclaim(page); 75492df3a72SMel Gorman nr_writeback++; 755*c3b94f44SHugh Dickins goto keep_locked; 756c661b078SAndy Whitcroft } 757*c3b94f44SHugh Dickins wait_on_page_writeback(page); 758e62e384eSMichal Hocko } 7591da177e4SLinus Torvalds 7606a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 761dfc8d636SJohannes Weiner switch (references) { 762dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 7631da177e4SLinus Torvalds goto activate_locked; 76464574746SJohannes Weiner case PAGEREF_KEEP: 76564574746SJohannes Weiner goto keep_locked; 766dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 767dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 768dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 769dfc8d636SJohannes Weiner } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /* 7721da177e4SLinus Torvalds * Anonymous process memory has backing store? 7731da177e4SLinus Torvalds * Try to allocate it some swap space here. 7741da177e4SLinus Torvalds */ 775b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 77663eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 77763eb6b93SHugh Dickins goto keep_locked; 778ac47b003SHugh Dickins if (!add_to_swap(page)) 7791da177e4SLinus Torvalds goto activate_locked; 78063eb6b93SHugh Dickins may_enter_fs = 1; 781b291f000SNick Piggin } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds mapping = page_mapping(page); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 7871da177e4SLinus Torvalds * processes. Try to unmap it here. 7881da177e4SLinus Torvalds */ 7891da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 79014fa31b8SAndi Kleen switch (try_to_unmap(page, TTU_UNMAP)) { 7911da177e4SLinus Torvalds case SWAP_FAIL: 7921da177e4SLinus Torvalds goto activate_locked; 7931da177e4SLinus Torvalds case SWAP_AGAIN: 7941da177e4SLinus Torvalds goto keep_locked; 795b291f000SNick Piggin case SWAP_MLOCK: 796b291f000SNick Piggin goto cull_mlocked; 7971da177e4SLinus Torvalds case SWAP_SUCCESS: 7981da177e4SLinus Torvalds ; /* try to free the page below */ 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds if (PageDirty(page)) { 8030e093d99SMel Gorman nr_dirty++; 8040e093d99SMel Gorman 805ee72886dSMel Gorman /* 806ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 807f84f6e2bSMel Gorman * avoid risk of stack overflow but do not writeback 808f84f6e2bSMel Gorman * unless under significant pressure. 809ee72886dSMel Gorman */ 810f84f6e2bSMel Gorman if (page_is_file_cache(page) && 8119e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 8129e3b2f8cSKonstantin Khlebnikov sc->priority >= DEF_PRIORITY - 2)) { 81349ea7eb6SMel Gorman /* 81449ea7eb6SMel Gorman * Immediately reclaim when written back. 81549ea7eb6SMel Gorman * Similar in principal to deactivate_page() 81649ea7eb6SMel Gorman * except we already have the page isolated 81749ea7eb6SMel Gorman * and know it's dirty 81849ea7eb6SMel Gorman */ 81949ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 82049ea7eb6SMel Gorman SetPageReclaim(page); 82149ea7eb6SMel Gorman 822ee72886dSMel Gorman goto keep_locked; 823ee72886dSMel Gorman } 824ee72886dSMel Gorman 825dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 8261da177e4SLinus Torvalds goto keep_locked; 8274dd4b920SAndrew Morton if (!may_enter_fs) 8281da177e4SLinus Torvalds goto keep_locked; 82952a8363eSChristoph Lameter if (!sc->may_writepage) 8301da177e4SLinus Torvalds goto keep_locked; 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 8337d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 8341da177e4SLinus Torvalds case PAGE_KEEP: 8350e093d99SMel Gorman nr_congested++; 8361da177e4SLinus Torvalds goto keep_locked; 8371da177e4SLinus Torvalds case PAGE_ACTIVATE: 8381da177e4SLinus Torvalds goto activate_locked; 8391da177e4SLinus Torvalds case PAGE_SUCCESS: 8407d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 84141ac1999SMel Gorman goto keep; 8427d3579e8SKOSAKI Motohiro if (PageDirty(page)) 8431da177e4SLinus Torvalds goto keep; 8447d3579e8SKOSAKI Motohiro 8451da177e4SLinus Torvalds /* 8461da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 8471da177e4SLinus Torvalds * ahead and try to reclaim the page. 8481da177e4SLinus Torvalds */ 849529ae9aaSNick Piggin if (!trylock_page(page)) 8501da177e4SLinus Torvalds goto keep; 8511da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 8521da177e4SLinus Torvalds goto keep_locked; 8531da177e4SLinus Torvalds mapping = page_mapping(page); 8541da177e4SLinus Torvalds case PAGE_CLEAN: 8551da177e4SLinus Torvalds ; /* try to free the page below */ 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* 8601da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 8611da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 8621da177e4SLinus Torvalds * the page as well. 8631da177e4SLinus Torvalds * 8641da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 8651da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 8661da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 8671da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 8681da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 8691da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 8701da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 8711da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 8721da177e4SLinus Torvalds * 8731da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 8741da177e4SLinus Torvalds * the pages which were not successfully invalidated in 8751da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 8761da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 8771da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 8781da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 8791da177e4SLinus Torvalds */ 880266cf658SDavid Howells if (page_has_private(page)) { 8811da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 8821da177e4SLinus Torvalds goto activate_locked; 883e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 884e286781dSNick Piggin unlock_page(page); 885e286781dSNick Piggin if (put_page_testzero(page)) 8861da177e4SLinus Torvalds goto free_it; 887e286781dSNick Piggin else { 888e286781dSNick Piggin /* 889e286781dSNick Piggin * rare race with speculative reference. 890e286781dSNick Piggin * the speculative reference will free 891e286781dSNick Piggin * this page shortly, so we may 892e286781dSNick Piggin * increment nr_reclaimed here (and 893e286781dSNick Piggin * leave it off the LRU). 894e286781dSNick Piggin */ 895e286781dSNick Piggin nr_reclaimed++; 896e286781dSNick Piggin continue; 897e286781dSNick Piggin } 898e286781dSNick Piggin } 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 901e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 90249d2e9ccSChristoph Lameter goto keep_locked; 9031da177e4SLinus Torvalds 904a978d6f5SNick Piggin /* 905a978d6f5SNick Piggin * At this point, we have no other references and there is 906a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 907a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 908a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 909a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 910a978d6f5SNick Piggin */ 911a978d6f5SNick Piggin __clear_page_locked(page); 912e286781dSNick Piggin free_it: 91305ff5137SAndrew Morton nr_reclaimed++; 914abe4c3b5SMel Gorman 915abe4c3b5SMel Gorman /* 916abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 917abe4c3b5SMel Gorman * appear not as the counts should be low 918abe4c3b5SMel Gorman */ 919abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 9201da177e4SLinus Torvalds continue; 9211da177e4SLinus Torvalds 922b291f000SNick Piggin cull_mlocked: 92363d6c5adSHugh Dickins if (PageSwapCache(page)) 92463d6c5adSHugh Dickins try_to_free_swap(page); 925b291f000SNick Piggin unlock_page(page); 926b291f000SNick Piggin putback_lru_page(page); 927b291f000SNick Piggin continue; 928b291f000SNick Piggin 9291da177e4SLinus Torvalds activate_locked: 93068a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 93168a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 932a2c43eedSHugh Dickins try_to_free_swap(page); 933894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 9341da177e4SLinus Torvalds SetPageActive(page); 9351da177e4SLinus Torvalds pgactivate++; 9361da177e4SLinus Torvalds keep_locked: 9371da177e4SLinus Torvalds unlock_page(page); 9381da177e4SLinus Torvalds keep: 9391da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 940b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 9411da177e4SLinus Torvalds } 942abe4c3b5SMel Gorman 9430e093d99SMel Gorman /* 9440e093d99SMel Gorman * Tag a zone as congested if all the dirty pages encountered were 9450e093d99SMel Gorman * backed by a congested BDI. In this case, reclaimers should just 9460e093d99SMel Gorman * back off and wait for congestion to clear because further reclaim 9470e093d99SMel Gorman * will encounter the same problem 9480e093d99SMel Gorman */ 94989b5fae5SJohannes Weiner if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc)) 9506a18adb3SKonstantin Khlebnikov zone_set_flag(zone, ZONE_CONGESTED); 9510e093d99SMel Gorman 952cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 953abe4c3b5SMel Gorman 9541da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 955f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 95692df3a72SMel Gorman *ret_nr_dirty += nr_dirty; 95792df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 95805ff5137SAndrew Morton return nr_reclaimed; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9615ad333ebSAndy Whitcroft /* 9625ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 9635ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 9645ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 9655ad333ebSAndy Whitcroft * 9665ad333ebSAndy Whitcroft * page: page to consider 9675ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 9685ad333ebSAndy Whitcroft * 9695ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 9705ad333ebSAndy Whitcroft */ 971f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 9725ad333ebSAndy Whitcroft { 9735ad333ebSAndy Whitcroft int ret = -EINVAL; 9745ad333ebSAndy Whitcroft 9755ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 9765ad333ebSAndy Whitcroft if (!PageLRU(page)) 9775ad333ebSAndy Whitcroft return ret; 9785ad333ebSAndy Whitcroft 979c53919adSMel Gorman /* Do not give back unevictable pages for compaction */ 980894bc310SLee Schermerhorn if (PageUnevictable(page)) 981894bc310SLee Schermerhorn return ret; 982894bc310SLee Schermerhorn 9835ad333ebSAndy Whitcroft ret = -EBUSY; 98408e552c6SKAMEZAWA Hiroyuki 985c8244935SMel Gorman /* 986c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 987c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 988c8244935SMel Gorman * blocking - clean pages for the most part. 989c8244935SMel Gorman * 990c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 991c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 992c8244935SMel Gorman * 993c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 994c8244935SMel Gorman * that it is possible to migrate without blocking 995c8244935SMel Gorman */ 996c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 997c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 998c8244935SMel Gorman if (PageWriteback(page)) 99939deaf85SMinchan Kim return ret; 100039deaf85SMinchan Kim 1001c8244935SMel Gorman if (PageDirty(page)) { 1002c8244935SMel Gorman struct address_space *mapping; 1003c8244935SMel Gorman 1004c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1005c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1006c8244935SMel Gorman return ret; 1007c8244935SMel Gorman 1008c8244935SMel Gorman /* 1009c8244935SMel Gorman * Only pages without mappings or that have a 1010c8244935SMel Gorman * ->migratepage callback are possible to migrate 1011c8244935SMel Gorman * without blocking 1012c8244935SMel Gorman */ 1013c8244935SMel Gorman mapping = page_mapping(page); 1014c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1015c8244935SMel Gorman return ret; 1016c8244935SMel Gorman } 1017c8244935SMel Gorman } 1018c8244935SMel Gorman 1019f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1020f80c0673SMinchan Kim return ret; 1021f80c0673SMinchan Kim 10225ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 10235ad333ebSAndy Whitcroft /* 10245ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 10255ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 10265ad333ebSAndy Whitcroft * page release code relies on it. 10275ad333ebSAndy Whitcroft */ 10285ad333ebSAndy Whitcroft ClearPageLRU(page); 10295ad333ebSAndy Whitcroft ret = 0; 10305ad333ebSAndy Whitcroft } 10315ad333ebSAndy Whitcroft 10325ad333ebSAndy Whitcroft return ret; 10335ad333ebSAndy Whitcroft } 10345ad333ebSAndy Whitcroft 103549d2e9ccSChristoph Lameter /* 10361da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10371da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10381da177e4SLinus Torvalds * and working on them outside the LRU lock. 10391da177e4SLinus Torvalds * 10401da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10411da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10441da177e4SLinus Torvalds * 10451da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 10465dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 10471da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1048f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1049fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 10505ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 10513cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 10521da177e4SLinus Torvalds * 10531da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10541da177e4SLinus Torvalds */ 105569e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 10565dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1057fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 10583cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 10591da177e4SLinus Torvalds { 106075b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 106169e05944SAndrew Morton unsigned long nr_taken = 0; 1062c9b02d97SWu Fengguang unsigned long scan; 10631da177e4SLinus Torvalds 1064c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 10655ad333ebSAndy Whitcroft struct page *page; 1066fa9add64SHugh Dickins int nr_pages; 10675ad333ebSAndy Whitcroft 10681da177e4SLinus Torvalds page = lru_to_page(src); 10691da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10701da177e4SLinus Torvalds 1071725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 10728d438f96SNick Piggin 1073f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 10745ad333ebSAndy Whitcroft case 0: 1075fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1076fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, -nr_pages); 10775ad333ebSAndy Whitcroft list_move(&page->lru, dst); 1078fa9add64SHugh Dickins nr_taken += nr_pages; 10795ad333ebSAndy Whitcroft break; 10807c8ee9a8SNick Piggin 10815ad333ebSAndy Whitcroft case -EBUSY: 10825ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 10835ad333ebSAndy Whitcroft list_move(&page->lru, src); 10845ad333ebSAndy Whitcroft continue; 10855ad333ebSAndy Whitcroft 10865ad333ebSAndy Whitcroft default: 10875ad333ebSAndy Whitcroft BUG(); 10885ad333ebSAndy Whitcroft } 10895ad333ebSAndy Whitcroft } 10901da177e4SLinus Torvalds 1091f626012dSHugh Dickins *nr_scanned = scan; 109275b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 109375b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 10941da177e4SLinus Torvalds return nr_taken; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 109762695a84SNick Piggin /** 109862695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 109962695a84SNick Piggin * @page: page to isolate from its LRU list 110062695a84SNick Piggin * 110162695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 110262695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 110362695a84SNick Piggin * 110462695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 110562695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 110662695a84SNick Piggin * 110762695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1108894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1109894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1110894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 111162695a84SNick Piggin * 111262695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 111362695a84SNick Piggin * found will be decremented. 111462695a84SNick Piggin * 111562695a84SNick Piggin * Restrictions: 111662695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 111762695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 111862695a84SNick Piggin * without a stable reference). 111962695a84SNick Piggin * (2) the lru_lock must not be held. 112062695a84SNick Piggin * (3) interrupts must be enabled. 112162695a84SNick Piggin */ 112262695a84SNick Piggin int isolate_lru_page(struct page *page) 112362695a84SNick Piggin { 112462695a84SNick Piggin int ret = -EBUSY; 112562695a84SNick Piggin 11260c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 11270c917313SKonstantin Khlebnikov 112862695a84SNick Piggin if (PageLRU(page)) { 112962695a84SNick Piggin struct zone *zone = page_zone(page); 1130fa9add64SHugh Dickins struct lruvec *lruvec; 113162695a84SNick Piggin 113262695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 1133fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 11340c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1135894bc310SLee Schermerhorn int lru = page_lru(page); 11360c917313SKonstantin Khlebnikov get_page(page); 113762695a84SNick Piggin ClearPageLRU(page); 1138fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 1139fa9add64SHugh Dickins ret = 0; 114062695a84SNick Piggin } 114162695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 114262695a84SNick Piggin } 114362695a84SNick Piggin return ret; 114462695a84SNick Piggin } 114562695a84SNick Piggin 11465ad333ebSAndy Whitcroft /* 114735cd7815SRik van Riel * Are there way too many processes in the direct reclaim path already? 114835cd7815SRik van Riel */ 114935cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 115035cd7815SRik van Riel struct scan_control *sc) 115135cd7815SRik van Riel { 115235cd7815SRik van Riel unsigned long inactive, isolated; 115335cd7815SRik van Riel 115435cd7815SRik van Riel if (current_is_kswapd()) 115535cd7815SRik van Riel return 0; 115635cd7815SRik van Riel 115789b5fae5SJohannes Weiner if (!global_reclaim(sc)) 115835cd7815SRik van Riel return 0; 115935cd7815SRik van Riel 116035cd7815SRik van Riel if (file) { 116135cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 116235cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 116335cd7815SRik van Riel } else { 116435cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 116535cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 116635cd7815SRik van Riel } 116735cd7815SRik van Riel 116835cd7815SRik van Riel return isolated > inactive; 116935cd7815SRik van Riel } 117035cd7815SRik van Riel 117166635629SMel Gorman static noinline_for_stack void 117275b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 117366635629SMel Gorman { 117427ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 117527ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 11763f79768fSHugh Dickins LIST_HEAD(pages_to_free); 117766635629SMel Gorman 117866635629SMel Gorman /* 117966635629SMel Gorman * Put back any unfreeable pages. 118066635629SMel Gorman */ 118166635629SMel Gorman while (!list_empty(page_list)) { 11823f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 118366635629SMel Gorman int lru; 11843f79768fSHugh Dickins 118566635629SMel Gorman VM_BUG_ON(PageLRU(page)); 118666635629SMel Gorman list_del(&page->lru); 118766635629SMel Gorman if (unlikely(!page_evictable(page, NULL))) { 118866635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 118966635629SMel Gorman putback_lru_page(page); 119066635629SMel Gorman spin_lock_irq(&zone->lru_lock); 119166635629SMel Gorman continue; 119266635629SMel Gorman } 1193fa9add64SHugh Dickins 1194fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 1195fa9add64SHugh Dickins 11967a608572SLinus Torvalds SetPageLRU(page); 119766635629SMel Gorman lru = page_lru(page); 1198fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 1199fa9add64SHugh Dickins 120066635629SMel Gorman if (is_active_lru(lru)) { 120166635629SMel Gorman int file = is_file_lru(lru); 12029992af10SRik van Riel int numpages = hpage_nr_pages(page); 12039992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 120466635629SMel Gorman } 12052bcf8879SHugh Dickins if (put_page_testzero(page)) { 12062bcf8879SHugh Dickins __ClearPageLRU(page); 12072bcf8879SHugh Dickins __ClearPageActive(page); 1208fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 12092bcf8879SHugh Dickins 12102bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 121166635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 12122bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 121366635629SMel Gorman spin_lock_irq(&zone->lru_lock); 12142bcf8879SHugh Dickins } else 12152bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 121666635629SMel Gorman } 121766635629SMel Gorman } 121866635629SMel Gorman 12193f79768fSHugh Dickins /* 12203f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 12213f79768fSHugh Dickins */ 12223f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 122366635629SMel Gorman } 122466635629SMel Gorman 122566635629SMel Gorman /* 12261742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 12271742f19fSAndrew Morton * of reclaimed pages 12281da177e4SLinus Torvalds */ 122966635629SMel Gorman static noinline_for_stack unsigned long 12301a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 12319e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds LIST_HEAD(page_list); 1234e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 123505ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1236e247dbceSKOSAKI Motohiro unsigned long nr_taken; 123792df3a72SMel Gorman unsigned long nr_dirty = 0; 123892df3a72SMel Gorman unsigned long nr_writeback = 0; 1239f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 12403cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 12411a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 12421a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 124378dc583dSKOSAKI Motohiro 124435cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 124558355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 124635cd7815SRik van Riel 124735cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 124835cd7815SRik van Riel if (fatal_signal_pending(current)) 124935cd7815SRik van Riel return SWAP_CLUSTER_MAX; 125035cd7815SRik van Riel } 125135cd7815SRik van Riel 12521da177e4SLinus Torvalds lru_add_drain(); 1253f80c0673SMinchan Kim 1254f80c0673SMinchan Kim if (!sc->may_unmap) 125561317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1256f80c0673SMinchan Kim if (!sc->may_writepage) 125761317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1258f80c0673SMinchan Kim 12591da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12601da177e4SLinus Torvalds 12615dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 12625dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 126395d918fcSKonstantin Khlebnikov 126495d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 126595d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 126695d918fcSKonstantin Khlebnikov 126789b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1268e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1269b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 127075b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1271b35ea17bSKOSAKI Motohiro else 127275b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1273b35ea17bSKOSAKI Motohiro } 127466635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1275d563c050SHillf Danton 1276d563c050SHillf Danton if (nr_taken == 0) 127766635629SMel Gorman return 0; 1278b35ea17bSKOSAKI Motohiro 12796a18adb3SKonstantin Khlebnikov nr_reclaimed = shrink_page_list(&page_list, zone, sc, 128092df3a72SMel Gorman &nr_dirty, &nr_writeback); 1281c661b078SAndy Whitcroft 12823f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 12833f79768fSHugh Dickins 128495d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1285d563c050SHillf Danton 1286904249aaSYing Han if (global_reclaim(sc)) { 1287b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1288904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1289904249aaSYing Han nr_reclaimed); 1290904249aaSYing Han else 1291904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1292904249aaSYing Han nr_reclaimed); 1293904249aaSYing Han } 1294a74609faSNick Piggin 129527ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 12963f79768fSHugh Dickins 129795d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 12983f79768fSHugh Dickins 12993f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 13003f79768fSHugh Dickins 13013f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1302e11da5b4SMel Gorman 130392df3a72SMel Gorman /* 130492df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 130592df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 130692df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 130792df3a72SMel Gorman * at throttling processes due to the page distribution throughout 130892df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 130992df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 131092df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 131192df3a72SMel Gorman * same way balance_dirty_pages() manages. 131292df3a72SMel Gorman * 131392df3a72SMel Gorman * This scales the number of dirty pages that must be under writeback 131492df3a72SMel Gorman * before throttling depending on priority. It is a simple backoff 131592df3a72SMel Gorman * function that has the most effect in the range DEF_PRIORITY to 131692df3a72SMel Gorman * DEF_PRIORITY-2 which is the priority reclaim is considered to be 131792df3a72SMel Gorman * in trouble and reclaim is considered to be in trouble. 131892df3a72SMel Gorman * 131992df3a72SMel Gorman * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle 132092df3a72SMel Gorman * DEF_PRIORITY-1 50% must be PageWriteback 132192df3a72SMel Gorman * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble 132292df3a72SMel Gorman * ... 132392df3a72SMel Gorman * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any 132492df3a72SMel Gorman * isolated page is PageWriteback 132592df3a72SMel Gorman */ 13269e3b2f8cSKonstantin Khlebnikov if (nr_writeback && nr_writeback >= 13279e3b2f8cSKonstantin Khlebnikov (nr_taken >> (DEF_PRIORITY - sc->priority))) 132892df3a72SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 132992df3a72SMel Gorman 1330e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1331e11da5b4SMel Gorman zone_idx(zone), 1332e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 13339e3b2f8cSKonstantin Khlebnikov sc->priority, 133423b9da55SMel Gorman trace_shrink_flags(file)); 133505ff5137SAndrew Morton return nr_reclaimed; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13383bb1a852SMartin Bligh /* 13391cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 13401cfb419bSKAMEZAWA Hiroyuki * 13411cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 13421cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 13431cfb419bSKAMEZAWA Hiroyuki * 13441cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 13451cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 13461cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 13471cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 13481cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 13491cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 13501cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 13511cfb419bSKAMEZAWA Hiroyuki * 13521cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 13531cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 13541cfb419bSKAMEZAWA Hiroyuki */ 13551cfb419bSKAMEZAWA Hiroyuki 1356fa9add64SHugh Dickins static void move_active_pages_to_lru(struct lruvec *lruvec, 13573eb4140fSWu Fengguang struct list_head *list, 13582bcf8879SHugh Dickins struct list_head *pages_to_free, 13593eb4140fSWu Fengguang enum lru_list lru) 13603eb4140fSWu Fengguang { 1361fa9add64SHugh Dickins struct zone *zone = lruvec_zone(lruvec); 13623eb4140fSWu Fengguang unsigned long pgmoved = 0; 13633eb4140fSWu Fengguang struct page *page; 1364fa9add64SHugh Dickins int nr_pages; 13653eb4140fSWu Fengguang 13663eb4140fSWu Fengguang while (!list_empty(list)) { 13673eb4140fSWu Fengguang page = lru_to_page(list); 1368fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 13693eb4140fSWu Fengguang 13703eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 13713eb4140fSWu Fengguang SetPageLRU(page); 13723eb4140fSWu Fengguang 1373fa9add64SHugh Dickins nr_pages = hpage_nr_pages(page); 1374fa9add64SHugh Dickins mem_cgroup_update_lru_size(lruvec, lru, nr_pages); 1375925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 1376fa9add64SHugh Dickins pgmoved += nr_pages; 13773eb4140fSWu Fengguang 13782bcf8879SHugh Dickins if (put_page_testzero(page)) { 13792bcf8879SHugh Dickins __ClearPageLRU(page); 13802bcf8879SHugh Dickins __ClearPageActive(page); 1381fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, lru); 13822bcf8879SHugh Dickins 13832bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 13843eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 13852bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 13863eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 13872bcf8879SHugh Dickins } else 13882bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 13893eb4140fSWu Fengguang } 13903eb4140fSWu Fengguang } 13913eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 13923eb4140fSWu Fengguang if (!is_active_lru(lru)) 13933eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 13943eb4140fSWu Fengguang } 13951cfb419bSKAMEZAWA Hiroyuki 1396f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 13971a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1398f16015fbSJohannes Weiner struct scan_control *sc, 13999e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 14001cfb419bSKAMEZAWA Hiroyuki { 140144c241f1SKOSAKI Motohiro unsigned long nr_taken; 1402f626012dSHugh Dickins unsigned long nr_scanned; 14036fe6b7e3SWu Fengguang unsigned long vm_flags; 14041cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 14058cab4754SWu Fengguang LIST_HEAD(l_active); 1406b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 14071cfb419bSKAMEZAWA Hiroyuki struct page *page; 14081a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 140944c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1410f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 14113cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 14121a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 14131cfb419bSKAMEZAWA Hiroyuki 14141da177e4SLinus Torvalds lru_add_drain(); 1415f80c0673SMinchan Kim 1416f80c0673SMinchan Kim if (!sc->may_unmap) 141761317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1418f80c0673SMinchan Kim if (!sc->may_writepage) 141961317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1420f80c0673SMinchan Kim 14211da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1422925b7673SJohannes Weiner 14235dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 14245dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 142589b5fae5SJohannes Weiner if (global_reclaim(sc)) 1426f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 142789b5fae5SJohannes Weiner 1428b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 14291cfb419bSKAMEZAWA Hiroyuki 1430f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 14313cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1432a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 14331da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 14361da177e4SLinus Torvalds cond_resched(); 14371da177e4SLinus Torvalds page = lru_to_page(&l_hold); 14381da177e4SLinus Torvalds list_del(&page->lru); 14397e9cd484SRik van Riel 1440894bc310SLee Schermerhorn if (unlikely(!page_evictable(page, NULL))) { 1441894bc310SLee Schermerhorn putback_lru_page(page); 1442894bc310SLee Schermerhorn continue; 1443894bc310SLee Schermerhorn } 1444894bc310SLee Schermerhorn 1445cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1446cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1447cc715d99SMel Gorman if (page_has_private(page)) 1448cc715d99SMel Gorman try_to_release_page(page, 0); 1449cc715d99SMel Gorman unlock_page(page); 1450cc715d99SMel Gorman } 1451cc715d99SMel Gorman } 1452cc715d99SMel Gorman 1453c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1454c3ac9a8aSJohannes Weiner &vm_flags)) { 14559992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 14568cab4754SWu Fengguang /* 14578cab4754SWu Fengguang * Identify referenced, file-backed active pages and 14588cab4754SWu Fengguang * give them one more trip around the active list. So 14598cab4754SWu Fengguang * that executable code get better chances to stay in 14608cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 14618cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 14628cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 14638cab4754SWu Fengguang * so we ignore them here. 14648cab4754SWu Fengguang */ 146541e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 14668cab4754SWu Fengguang list_add(&page->lru, &l_active); 14678cab4754SWu Fengguang continue; 14688cab4754SWu Fengguang } 14698cab4754SWu Fengguang } 14707e9cd484SRik van Riel 14715205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 14721da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 1475b555749aSAndrew Morton /* 14768cab4754SWu Fengguang * Move pages back to the lru list. 1477b555749aSAndrew Morton */ 14782a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 14794f98a2feSRik van Riel /* 14808cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 14818cab4754SWu Fengguang * even though only some of them are actually re-activated. This 14828cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 14838cab4754SWu Fengguang * get_scan_ratio. 1484556adecbSRik van Riel */ 1485b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1486556adecbSRik van Riel 1487fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_active, &l_hold, lru); 1488fa9add64SHugh Dickins move_active_pages_to_lru(lruvec, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1489a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1490f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 14912bcf8879SHugh Dickins 14922bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds 149574e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 149614797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1497f89eb90eSKOSAKI Motohiro { 1498f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1499f89eb90eSKOSAKI Motohiro 1500f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1501f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1502f89eb90eSKOSAKI Motohiro 1503f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1504f89eb90eSKOSAKI Motohiro return 1; 1505f89eb90eSKOSAKI Motohiro 1506f89eb90eSKOSAKI Motohiro return 0; 1507f89eb90eSKOSAKI Motohiro } 1508f89eb90eSKOSAKI Motohiro 150914797e23SKOSAKI Motohiro /** 151014797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1511c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 151214797e23SKOSAKI Motohiro * 151314797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 151414797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 151514797e23SKOSAKI Motohiro */ 1516c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 151714797e23SKOSAKI Motohiro { 151874e3f3c3SMinchan Kim /* 151974e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 152074e3f3c3SMinchan Kim * is pointless. 152174e3f3c3SMinchan Kim */ 152274e3f3c3SMinchan Kim if (!total_swap_pages) 152374e3f3c3SMinchan Kim return 0; 152474e3f3c3SMinchan Kim 1525c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1526c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1527f16015fbSJohannes Weiner 1528c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 152914797e23SKOSAKI Motohiro } 153074e3f3c3SMinchan Kim #else 1531c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 153274e3f3c3SMinchan Kim { 153374e3f3c3SMinchan Kim return 0; 153474e3f3c3SMinchan Kim } 153574e3f3c3SMinchan Kim #endif 153614797e23SKOSAKI Motohiro 153756e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone) 153856e49d21SRik van Riel { 153956e49d21SRik van Riel unsigned long active, inactive; 154056e49d21SRik van Riel 154156e49d21SRik van Riel active = zone_page_state(zone, NR_ACTIVE_FILE); 154256e49d21SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 154356e49d21SRik van Riel 154456e49d21SRik van Riel return (active > inactive); 154556e49d21SRik van Riel } 154656e49d21SRik van Riel 154756e49d21SRik van Riel /** 154856e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1549c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 155056e49d21SRik van Riel * 155156e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 155256e49d21SRik van Riel * ensures that active file pages get deactivated, until more 155356e49d21SRik van Riel * than half of the file pages are on the inactive list. 155456e49d21SRik van Riel * 155556e49d21SRik van Riel * Once we get to that situation, protect the system's working 155656e49d21SRik van Riel * set from being evicted by disabling active file page aging. 155756e49d21SRik van Riel * 155856e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 155956e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 156056e49d21SRik van Riel */ 1561c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 156256e49d21SRik van Riel { 1563c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1564c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_file_is_low(lruvec); 156556e49d21SRik van Riel 1566c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low_global(lruvec_zone(lruvec)); 156756e49d21SRik van Riel } 156856e49d21SRik van Riel 156975b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1570b39415b2SRik van Riel { 157175b00af7SHugh Dickins if (is_file_lru(lru)) 1572c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1573b39415b2SRik van Riel else 1574c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1575b39415b2SRik van Riel } 1576b39415b2SRik van Riel 15774f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 15781a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1579b69408e8SChristoph Lameter { 1580b39415b2SRik van Riel if (is_active_lru(lru)) { 158175b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 15821a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1583556adecbSRik van Riel return 0; 1584556adecbSRik van Riel } 1585556adecbSRik van Riel 15861a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1587b69408e8SChristoph Lameter } 1588b69408e8SChristoph Lameter 15893d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 15901f4c025bSKAMEZAWA Hiroyuki { 159189b5fae5SJohannes Weiner if (global_reclaim(sc)) 15921f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 15933d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 15941f4c025bSKAMEZAWA Hiroyuki } 15951f4c025bSKAMEZAWA Hiroyuki 15961da177e4SLinus Torvalds /* 15974f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 15984f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 15994f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 16004f98a2feSRik van Riel * onto the active list instead of evict. 16014f98a2feSRik van Riel * 1602be7bd59dSWanpeng Li * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan 1603be7bd59dSWanpeng Li * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan 16044f98a2feSRik van Riel */ 160590126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 16069e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 16074f98a2feSRik van Riel { 16084f98a2feSRik van Riel unsigned long anon, file, free; 16094f98a2feSRik van Riel unsigned long anon_prio, file_prio; 16104f98a2feSRik van Riel unsigned long ap, fp; 161190126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 161276a33fc3SShaohua Li u64 fraction[2], denominator; 16134111304dSHugh Dickins enum lru_list lru; 161476a33fc3SShaohua Li int noswap = 0; 1615a4d3e9e7SJohannes Weiner bool force_scan = false; 161690126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 1617246e87a9SKAMEZAWA Hiroyuki 1618f11c0ca5SJohannes Weiner /* 1619f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1620f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1621f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1622f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1623f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1624f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1625f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1626f11c0ca5SJohannes Weiner * well. 1627f11c0ca5SJohannes Weiner */ 162890126375SKonstantin Khlebnikov if (current_is_kswapd() && zone->all_unreclaimable) 1629a4d3e9e7SJohannes Weiner force_scan = true; 163089b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1631a4d3e9e7SJohannes Weiner force_scan = true; 163276a33fc3SShaohua Li 163376a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 163476a33fc3SShaohua Li if (!sc->may_swap || (nr_swap_pages <= 0)) { 163576a33fc3SShaohua Li noswap = 1; 163676a33fc3SShaohua Li fraction[0] = 0; 163776a33fc3SShaohua Li fraction[1] = 1; 163876a33fc3SShaohua Li denominator = 1; 163976a33fc3SShaohua Li goto out; 164076a33fc3SShaohua Li } 16414f98a2feSRik van Riel 16424d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 16434d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 16444d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 16454d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1646a4d3e9e7SJohannes Weiner 164789b5fae5SJohannes Weiner if (global_reclaim(sc)) { 164890126375SKonstantin Khlebnikov free = zone_page_state(zone, NR_FREE_PAGES); 1649eeee9a8cSKOSAKI Motohiro /* If we have very few page cache pages, 1650eeee9a8cSKOSAKI Motohiro force-scan anon pages. */ 165190126375SKonstantin Khlebnikov if (unlikely(file + free <= high_wmark_pages(zone))) { 165276a33fc3SShaohua Li fraction[0] = 1; 165376a33fc3SShaohua Li fraction[1] = 0; 165476a33fc3SShaohua Li denominator = 1; 165576a33fc3SShaohua Li goto out; 16564f98a2feSRik van Riel } 1657eeee9a8cSKOSAKI Motohiro } 16584f98a2feSRik van Riel 16594f98a2feSRik van Riel /* 166058c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 166158c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 166258c37f6eSKOSAKI Motohiro */ 16633d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 166475b00af7SHugh Dickins file_prio = 200 - anon_prio; 166558c37f6eSKOSAKI Motohiro 166658c37f6eSKOSAKI Motohiro /* 16674f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 16684f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 16694f98a2feSRik van Riel * ratios to determine how valuable each cache is. 16704f98a2feSRik van Riel * 16714f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 16724f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 16734f98a2feSRik van Riel * up weighing recent references more than old ones. 16744f98a2feSRik van Riel * 16754f98a2feSRik van Riel * anon in [0], file in [1] 16764f98a2feSRik van Riel */ 167790126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 167858c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 16796e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 16806e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 16814f98a2feSRik van Riel } 16824f98a2feSRik van Riel 16836e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 16846e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 16856e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 16864f98a2feSRik van Riel } 16874f98a2feSRik van Riel 16884f98a2feSRik van Riel /* 168900d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 169000d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 169100d8089cSRik van Riel * each list that were recently referenced and in active use. 16924f98a2feSRik van Riel */ 1693fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 16946e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 16954f98a2feSRik van Riel 1696fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 16976e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 169890126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 16994f98a2feSRik van Riel 170076a33fc3SShaohua Li fraction[0] = ap; 170176a33fc3SShaohua Li fraction[1] = fp; 170276a33fc3SShaohua Li denominator = ap + fp + 1; 170376a33fc3SShaohua Li out: 17044111304dSHugh Dickins for_each_evictable_lru(lru) { 17054111304dSHugh Dickins int file = is_file_lru(lru); 170676a33fc3SShaohua Li unsigned long scan; 170776a33fc3SShaohua Li 17084d7dcca2SHugh Dickins scan = get_lru_size(lruvec, lru); 17099e3b2f8cSKonstantin Khlebnikov if (sc->priority || noswap || !vmscan_swappiness(sc)) { 17109e3b2f8cSKonstantin Khlebnikov scan >>= sc->priority; 1711f11c0ca5SJohannes Weiner if (!scan && force_scan) 1712f11c0ca5SJohannes Weiner scan = SWAP_CLUSTER_MAX; 171376a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 17144f98a2feSRik van Riel } 17154111304dSHugh Dickins nr[lru] = scan; 171676a33fc3SShaohua Li } 17176e08a369SWu Fengguang } 17184f98a2feSRik van Riel 171923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 17209e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 172123b9da55SMel Gorman { 172223b9da55SMel Gorman if (COMPACTION_BUILD && sc->order && 172323b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 17249e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 172523b9da55SMel Gorman return true; 172623b9da55SMel Gorman 172723b9da55SMel Gorman return false; 172823b9da55SMel Gorman } 172923b9da55SMel Gorman 17304f98a2feSRik van Riel /* 173123b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 173223b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 173323b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 173423b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 173523b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 17363e7d3449SMel Gorman */ 173790bdcfafSKonstantin Khlebnikov static inline bool should_continue_reclaim(struct lruvec *lruvec, 17383e7d3449SMel Gorman unsigned long nr_reclaimed, 17393e7d3449SMel Gorman unsigned long nr_scanned, 17403e7d3449SMel Gorman struct scan_control *sc) 17413e7d3449SMel Gorman { 17423e7d3449SMel Gorman unsigned long pages_for_compaction; 17433e7d3449SMel Gorman unsigned long inactive_lru_pages; 17443e7d3449SMel Gorman 17453e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 17469e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 17473e7d3449SMel Gorman return false; 17483e7d3449SMel Gorman 17492876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 17502876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 17513e7d3449SMel Gorman /* 17522876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 17532876592fSMel Gorman * full LRU list has been scanned and we are still failing 17542876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 17552876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 17563e7d3449SMel Gorman */ 17573e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 17583e7d3449SMel Gorman return false; 17592876592fSMel Gorman } else { 17602876592fSMel Gorman /* 17612876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 17622876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 17632876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 17642876592fSMel Gorman * pages that were scanned. This will return to the 17652876592fSMel Gorman * caller faster at the risk reclaim/compaction and 17662876592fSMel Gorman * the resulting allocation attempt fails 17672876592fSMel Gorman */ 17682876592fSMel Gorman if (!nr_reclaimed) 17692876592fSMel Gorman return false; 17702876592fSMel Gorman } 17713e7d3449SMel Gorman 17723e7d3449SMel Gorman /* 17733e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 17743e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 17753e7d3449SMel Gorman */ 17763e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 17774d7dcca2SHugh Dickins inactive_lru_pages = get_lru_size(lruvec, LRU_INACTIVE_FILE); 177886cfd3a4SMinchan Kim if (nr_swap_pages > 0) 17794d7dcca2SHugh Dickins inactive_lru_pages += get_lru_size(lruvec, LRU_INACTIVE_ANON); 17803e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 17813e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 17823e7d3449SMel Gorman return true; 17833e7d3449SMel Gorman 17843e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 178590bdcfafSKonstantin Khlebnikov switch (compaction_suitable(lruvec_zone(lruvec), sc->order)) { 17863e7d3449SMel Gorman case COMPACT_PARTIAL: 17873e7d3449SMel Gorman case COMPACT_CONTINUE: 17883e7d3449SMel Gorman return false; 17893e7d3449SMel Gorman default: 17903e7d3449SMel Gorman return true; 17913e7d3449SMel Gorman } 17923e7d3449SMel Gorman } 17933e7d3449SMel Gorman 17943e7d3449SMel Gorman /* 17951da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 17961da177e4SLinus Torvalds */ 1797f9be23d6SKonstantin Khlebnikov static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 17981da177e4SLinus Torvalds { 1799b69408e8SChristoph Lameter unsigned long nr[NR_LRU_LISTS]; 18008695949aSChristoph Lameter unsigned long nr_to_scan; 18014111304dSHugh Dickins enum lru_list lru; 1802f0fdc5e8SJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 180322fba335SKOSAKI Motohiro unsigned long nr_to_reclaim = sc->nr_to_reclaim; 18043da367c3SShaohua Li struct blk_plug plug; 18051da177e4SLinus Torvalds 18063e7d3449SMel Gorman restart: 18073e7d3449SMel Gorman nr_reclaimed = 0; 1808f0fdc5e8SJohannes Weiner nr_scanned = sc->nr_scanned; 180990126375SKonstantin Khlebnikov get_scan_count(lruvec, sc, nr); 18101cfb419bSKAMEZAWA Hiroyuki 18113da367c3SShaohua Li blk_start_plug(&plug); 1812556adecbSRik van Riel while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 1813556adecbSRik van Riel nr[LRU_INACTIVE_FILE]) { 18144111304dSHugh Dickins for_each_evictable_lru(lru) { 18154111304dSHugh Dickins if (nr[lru]) { 1816ece74b2eSKOSAKI Motohiro nr_to_scan = min_t(unsigned long, 18174111304dSHugh Dickins nr[lru], SWAP_CLUSTER_MAX); 18184111304dSHugh Dickins nr[lru] -= nr_to_scan; 1819b69408e8SChristoph Lameter 18204111304dSHugh Dickins nr_reclaimed += shrink_list(lru, nr_to_scan, 18211a93be0eSKonstantin Khlebnikov lruvec, sc); 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds } 1824a79311c1SRik van Riel /* 1825a79311c1SRik van Riel * On large memory systems, scan >> priority can become 1826a79311c1SRik van Riel * really large. This is fine for the starting priority; 1827a79311c1SRik van Riel * we want to put equal scanning pressure on each zone. 1828a79311c1SRik van Riel * However, if the VM has a harder time of freeing pages, 1829a79311c1SRik van Riel * with multiple processes reclaiming pages, the total 1830a79311c1SRik van Riel * freeing target can get unreasonably large. 1831a79311c1SRik van Riel */ 18329e3b2f8cSKonstantin Khlebnikov if (nr_reclaimed >= nr_to_reclaim && 18339e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY) 1834a79311c1SRik van Riel break; 18351da177e4SLinus Torvalds } 18363da367c3SShaohua Li blk_finish_plug(&plug); 18373e7d3449SMel Gorman sc->nr_reclaimed += nr_reclaimed; 183801dbe5c9SKOSAKI Motohiro 1839556adecbSRik van Riel /* 1840556adecbSRik van Riel * Even if we did not try to evict anon pages at all, we want to 1841556adecbSRik van Riel * rebalance the anon lru active/inactive ratio. 1842556adecbSRik van Riel */ 1843c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 18441a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 18459e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 1846556adecbSRik van Riel 18473e7d3449SMel Gorman /* reclaim/compaction might need reclaim to continue */ 184890bdcfafSKonstantin Khlebnikov if (should_continue_reclaim(lruvec, nr_reclaimed, 18499e3b2f8cSKonstantin Khlebnikov sc->nr_scanned - nr_scanned, sc)) 18503e7d3449SMel Gorman goto restart; 18513e7d3449SMel Gorman 1852232ea4d6SAndrew Morton throttle_vm_writeout(sc->gfp_mask); 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18559e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc) 1856f16015fbSJohannes Weiner { 18575660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 18585660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 18595660048cSJohannes Weiner .zone = zone, 18609e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 18615660048cSJohannes Weiner }; 18625660048cSJohannes Weiner struct mem_cgroup *memcg; 18635660048cSJohannes Weiner 18645660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 18655660048cSJohannes Weiner do { 1866f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 18675660048cSJohannes Weiner 1868f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, sc); 1869f9be23d6SKonstantin Khlebnikov 18705660048cSJohannes Weiner /* 18715660048cSJohannes Weiner * Limit reclaim has historically picked one memcg and 18725660048cSJohannes Weiner * scanned it with decreasing priority levels until 18735660048cSJohannes Weiner * nr_to_reclaim had been reclaimed. This priority 18745660048cSJohannes Weiner * cycle is thus over after a single memcg. 1875b95a2f2dSJohannes Weiner * 1876b95a2f2dSJohannes Weiner * Direct reclaim and kswapd, on the other hand, have 1877b95a2f2dSJohannes Weiner * to scan all memory cgroups to fulfill the overall 1878b95a2f2dSJohannes Weiner * scan target for the zone. 18795660048cSJohannes Weiner */ 18805660048cSJohannes Weiner if (!global_reclaim(sc)) { 18815660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 18825660048cSJohannes Weiner break; 18835660048cSJohannes Weiner } 18845660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 18855660048cSJohannes Weiner } while (memcg); 1886f16015fbSJohannes Weiner } 1887f16015fbSJohannes Weiner 1888fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 1889fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 1890fe4b1b24SMel Gorman { 1891fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 1892fe4b1b24SMel Gorman bool watermark_ok; 1893fe4b1b24SMel Gorman 1894fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 1895fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 1896fe4b1b24SMel Gorman return false; 1897fe4b1b24SMel Gorman 1898fe4b1b24SMel Gorman /* 1899fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 1900fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 1901fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 1902fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 1903fe4b1b24SMel Gorman */ 1904fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 1905fe4b1b24SMel Gorman (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 1906fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 1907fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 1908fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 1909fe4b1b24SMel Gorman 1910fe4b1b24SMel Gorman /* 1911fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 1912fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 1913fe4b1b24SMel Gorman */ 1914aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 1915fe4b1b24SMel Gorman return watermark_ok; 1916fe4b1b24SMel Gorman 1917fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 1918fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 1919fe4b1b24SMel Gorman return false; 1920fe4b1b24SMel Gorman 1921fe4b1b24SMel Gorman return watermark_ok; 1922fe4b1b24SMel Gorman } 1923fe4b1b24SMel Gorman 19241da177e4SLinus Torvalds /* 19251da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 19261da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 19271da177e4SLinus Torvalds * request. 19281da177e4SLinus Torvalds * 192941858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 193041858966SMel Gorman * Because: 19311da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 19321da177e4SLinus Torvalds * allocation or 193341858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 193441858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 193541858966SMel Gorman * zone defense algorithm. 19361da177e4SLinus Torvalds * 19371da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 19381da177e4SLinus Torvalds * scan then give up on it. 1939e0c23279SMel Gorman * 1940e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 1941fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 19420cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 19430cee34fdSMel Gorman * further reclaim. 19441da177e4SLinus Torvalds */ 19459e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 19461da177e4SLinus Torvalds { 1947dd1a239fSMel Gorman struct zoneref *z; 194854a6eb5cSMel Gorman struct zone *zone; 1949d149e3b2SYing Han unsigned long nr_soft_reclaimed; 1950d149e3b2SYing Han unsigned long nr_soft_scanned; 19510cee34fdSMel Gorman bool aborted_reclaim = false; 19521cfb419bSKAMEZAWA Hiroyuki 1953cc715d99SMel Gorman /* 1954cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 1955cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 1956cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 1957cc715d99SMel Gorman */ 1958cc715d99SMel Gorman if (buffer_heads_over_limit) 1959cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 1960cc715d99SMel Gorman 1961d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 1962d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 1963f3fe6512SCon Kolivas if (!populated_zone(zone)) 19641da177e4SLinus Torvalds continue; 19651cfb419bSKAMEZAWA Hiroyuki /* 19661cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 19671cfb419bSKAMEZAWA Hiroyuki * to global LRU. 19681cfb419bSKAMEZAWA Hiroyuki */ 196989b5fae5SJohannes Weiner if (global_reclaim(sc)) { 197002a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 19711da177e4SLinus Torvalds continue; 19729e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 19739e3b2f8cSKonstantin Khlebnikov sc->priority != DEF_PRIORITY) 19741da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 1975e0887c19SRik van Riel if (COMPACTION_BUILD) { 1976e0887c19SRik van Riel /* 1977e0c23279SMel Gorman * If we already have plenty of memory free for 1978e0c23279SMel Gorman * compaction in this zone, don't free any more. 1979e0c23279SMel Gorman * Even though compaction is invoked for any 1980e0c23279SMel Gorman * non-zero order, only frequent costly order 1981e0c23279SMel Gorman * reclamation is disruptive enough to become a 1982c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 1983c7cfa37bSCopot Alexandru * page allocations. 1984e0887c19SRik van Riel */ 1985fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 19860cee34fdSMel Gorman aborted_reclaim = true; 1987e0887c19SRik van Riel continue; 1988e0887c19SRik van Riel } 1989e0c23279SMel Gorman } 1990ac34a1a3SKAMEZAWA Hiroyuki /* 1991ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 1992ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 1993ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 1994ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 1995ac34a1a3SKAMEZAWA Hiroyuki */ 1996d149e3b2SYing Han nr_soft_scanned = 0; 1997d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 1998d149e3b2SYing Han sc->order, sc->gfp_mask, 1999d149e3b2SYing Han &nr_soft_scanned); 2000d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 2001ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 2002ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2003ac34a1a3SKAMEZAWA Hiroyuki } 2004d149e3b2SYing Han 20059e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 20061da177e4SLinus Torvalds } 2007e0c23279SMel Gorman 20080cee34fdSMel Gorman return aborted_reclaim; 2009d1908362SMinchan Kim } 2010d1908362SMinchan Kim 2011d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone) 2012d1908362SMinchan Kim { 2013d1908362SMinchan Kim return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 2014d1908362SMinchan Kim } 2015d1908362SMinchan Kim 2016929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2017d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2018d1908362SMinchan Kim struct scan_control *sc) 2019d1908362SMinchan Kim { 2020d1908362SMinchan Kim struct zoneref *z; 2021d1908362SMinchan Kim struct zone *zone; 2022d1908362SMinchan Kim 2023d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2024d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2025d1908362SMinchan Kim if (!populated_zone(zone)) 2026d1908362SMinchan Kim continue; 2027d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2028d1908362SMinchan Kim continue; 2029929bea7cSKOSAKI Motohiro if (!zone->all_unreclaimable) 2030929bea7cSKOSAKI Motohiro return false; 2031d1908362SMinchan Kim } 2032d1908362SMinchan Kim 2033929bea7cSKOSAKI Motohiro return true; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* 20371da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 20381da177e4SLinus Torvalds * 20391da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 20401da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 20411da177e4SLinus Torvalds * 20421da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 20431da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 20445b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 20455b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 20465b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 20475b0830cbSJens Axboe * work, and the allocation attempt will fail. 2048a41f24eaSNishanth Aravamudan * 2049a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2050a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 20511da177e4SLinus Torvalds */ 2052dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2053a09ed5e0SYing Han struct scan_control *sc, 2054a09ed5e0SYing Han struct shrink_control *shrink) 20551da177e4SLinus Torvalds { 205669e05944SAndrew Morton unsigned long total_scanned = 0; 20571da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2058dd1a239fSMel Gorman struct zoneref *z; 205954a6eb5cSMel Gorman struct zone *zone; 206022fba335SKOSAKI Motohiro unsigned long writeback_threshold; 20610cee34fdSMel Gorman bool aborted_reclaim; 20621da177e4SLinus Torvalds 2063873b4771SKeika Kobayashi delayacct_freepages_start(); 2064873b4771SKeika Kobayashi 206589b5fae5SJohannes Weiner if (global_reclaim(sc)) 2066f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 20671da177e4SLinus Torvalds 20689e3b2f8cSKonstantin Khlebnikov do { 206966e1707bSBalbir Singh sc->nr_scanned = 0; 20709e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2071e0c23279SMel Gorman 207266e1707bSBalbir Singh /* 207366e1707bSBalbir Singh * Don't shrink slabs when reclaiming memory from 207466e1707bSBalbir Singh * over limit cgroups 207566e1707bSBalbir Singh */ 207689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2077c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2078d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2079d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2080c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2081c6a8a8c5SKOSAKI Motohiro continue; 2082c6a8a8c5SKOSAKI Motohiro 2083c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2084c6a8a8c5SKOSAKI Motohiro } 2085c6a8a8c5SKOSAKI Motohiro 20861495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 20871da177e4SLinus Torvalds if (reclaim_state) { 2088a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 20891da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 20901da177e4SLinus Torvalds } 209191a45470SKAMEZAWA Hiroyuki } 209266e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2093bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 20941da177e4SLinus Torvalds goto out; 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds /* 20971da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 20981da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 20991da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 21001da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 21011da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 21021da177e4SLinus Torvalds */ 210322fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 210422fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 21050e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 21060e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 210766e1707bSBalbir Singh sc->may_writepage = 1; 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds 21101da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 21117b51755cSKOSAKI Motohiro if (!sc->hibernation_mode && sc->nr_scanned && 21129e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2) { 21130e093d99SMel Gorman struct zone *preferred_zone; 21140e093d99SMel Gorman 21150e093d99SMel Gorman first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), 2116f33261d7SDavid Rientjes &cpuset_current_mems_allowed, 2117f33261d7SDavid Rientjes &preferred_zone); 21180e093d99SMel Gorman wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); 21190e093d99SMel Gorman } 21209e3b2f8cSKonstantin Khlebnikov } while (--sc->priority >= 0); 2121bb21c7ceSKOSAKI Motohiro 21221da177e4SLinus Torvalds out: 2123873b4771SKeika Kobayashi delayacct_freepages_end(); 2124873b4771SKeika Kobayashi 2125bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2126bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2127bb21c7ceSKOSAKI Motohiro 2128929bea7cSKOSAKI Motohiro /* 2129929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2130929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2131929bea7cSKOSAKI Motohiro * check. 2132929bea7cSKOSAKI Motohiro */ 2133929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2134929bea7cSKOSAKI Motohiro return 0; 2135929bea7cSKOSAKI Motohiro 21360cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 21370cee34fdSMel Gorman if (aborted_reclaim) 21387335084dSMel Gorman return 1; 21397335084dSMel Gorman 2140bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 214189b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2142bb21c7ceSKOSAKI Motohiro return 1; 2143bb21c7ceSKOSAKI Motohiro 2144bb21c7ceSKOSAKI Motohiro return 0; 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 21475515061dSMel Gorman static bool pfmemalloc_watermark_ok(pg_data_t *pgdat) 21485515061dSMel Gorman { 21495515061dSMel Gorman struct zone *zone; 21505515061dSMel Gorman unsigned long pfmemalloc_reserve = 0; 21515515061dSMel Gorman unsigned long free_pages = 0; 21525515061dSMel Gorman int i; 21535515061dSMel Gorman bool wmark_ok; 21545515061dSMel Gorman 21555515061dSMel Gorman for (i = 0; i <= ZONE_NORMAL; i++) { 21565515061dSMel Gorman zone = &pgdat->node_zones[i]; 21575515061dSMel Gorman pfmemalloc_reserve += min_wmark_pages(zone); 21585515061dSMel Gorman free_pages += zone_page_state(zone, NR_FREE_PAGES); 21595515061dSMel Gorman } 21605515061dSMel Gorman 21615515061dSMel Gorman wmark_ok = free_pages > pfmemalloc_reserve / 2; 21625515061dSMel Gorman 21635515061dSMel Gorman /* kswapd must be awake if processes are being throttled */ 21645515061dSMel Gorman if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { 21655515061dSMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, 21665515061dSMel Gorman (enum zone_type)ZONE_NORMAL); 21675515061dSMel Gorman wake_up_interruptible(&pgdat->kswapd_wait); 21685515061dSMel Gorman } 21695515061dSMel Gorman 21705515061dSMel Gorman return wmark_ok; 21715515061dSMel Gorman } 21725515061dSMel Gorman 21735515061dSMel Gorman /* 21745515061dSMel Gorman * Throttle direct reclaimers if backing storage is backed by the network 21755515061dSMel Gorman * and the PFMEMALLOC reserve for the preferred node is getting dangerously 21765515061dSMel Gorman * depleted. kswapd will continue to make progress and wake the processes 21775515061dSMel Gorman * when the low watermark is reached 21785515061dSMel Gorman */ 21795515061dSMel Gorman static void throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist, 21805515061dSMel Gorman nodemask_t *nodemask) 21815515061dSMel Gorman { 21825515061dSMel Gorman struct zone *zone; 21835515061dSMel Gorman int high_zoneidx = gfp_zone(gfp_mask); 21845515061dSMel Gorman pg_data_t *pgdat; 21855515061dSMel Gorman 21865515061dSMel Gorman /* 21875515061dSMel Gorman * Kernel threads should not be throttled as they may be indirectly 21885515061dSMel Gorman * responsible for cleaning pages necessary for reclaim to make forward 21895515061dSMel Gorman * progress. kjournald for example may enter direct reclaim while 21905515061dSMel Gorman * committing a transaction where throttling it could forcing other 21915515061dSMel Gorman * processes to block on log_wait_commit(). 21925515061dSMel Gorman */ 21935515061dSMel Gorman if (current->flags & PF_KTHREAD) 21945515061dSMel Gorman return; 21955515061dSMel Gorman 21965515061dSMel Gorman /* Check if the pfmemalloc reserves are ok */ 21975515061dSMel Gorman first_zones_zonelist(zonelist, high_zoneidx, NULL, &zone); 21985515061dSMel Gorman pgdat = zone->zone_pgdat; 21995515061dSMel Gorman if (pfmemalloc_watermark_ok(pgdat)) 22005515061dSMel Gorman return; 22015515061dSMel Gorman 220268243e76SMel Gorman /* Account for the throttling */ 220368243e76SMel Gorman count_vm_event(PGSCAN_DIRECT_THROTTLE); 220468243e76SMel Gorman 22055515061dSMel Gorman /* 22065515061dSMel Gorman * If the caller cannot enter the filesystem, it's possible that it 22075515061dSMel Gorman * is due to the caller holding an FS lock or performing a journal 22085515061dSMel Gorman * transaction in the case of a filesystem like ext[3|4]. In this case, 22095515061dSMel Gorman * it is not safe to block on pfmemalloc_wait as kswapd could be 22105515061dSMel Gorman * blocked waiting on the same lock. Instead, throttle for up to a 22115515061dSMel Gorman * second before continuing. 22125515061dSMel Gorman */ 22135515061dSMel Gorman if (!(gfp_mask & __GFP_FS)) { 22145515061dSMel Gorman wait_event_interruptible_timeout(pgdat->pfmemalloc_wait, 22155515061dSMel Gorman pfmemalloc_watermark_ok(pgdat), HZ); 22165515061dSMel Gorman return; 22175515061dSMel Gorman } 22185515061dSMel Gorman 22195515061dSMel Gorman /* Throttle until kswapd wakes the process */ 22205515061dSMel Gorman wait_event_killable(zone->zone_pgdat->pfmemalloc_wait, 22215515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)); 22225515061dSMel Gorman } 22235515061dSMel Gorman 2224dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2225327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 222666e1707bSBalbir Singh { 222733906bc5SMel Gorman unsigned long nr_reclaimed; 222866e1707bSBalbir Singh struct scan_control sc = { 222966e1707bSBalbir Singh .gfp_mask = gfp_mask, 223066e1707bSBalbir Singh .may_writepage = !laptop_mode, 223122fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2232a6dc60f8SJohannes Weiner .may_unmap = 1, 22332e2e4259SKOSAKI Motohiro .may_swap = 1, 223466e1707bSBalbir Singh .order = order, 22359e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2236f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2237327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 223866e1707bSBalbir Singh }; 2239a09ed5e0SYing Han struct shrink_control shrink = { 2240a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2241a09ed5e0SYing Han }; 224266e1707bSBalbir Singh 22435515061dSMel Gorman throttle_direct_reclaim(gfp_mask, zonelist, nodemask); 22445515061dSMel Gorman 22455515061dSMel Gorman /* 22465515061dSMel Gorman * Do not enter reclaim if fatal signal is pending. 1 is returned so 22475515061dSMel Gorman * that the page allocator does not consider triggering OOM 22485515061dSMel Gorman */ 22495515061dSMel Gorman if (fatal_signal_pending(current)) 22505515061dSMel Gorman return 1; 22515515061dSMel Gorman 225233906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 225333906bc5SMel Gorman sc.may_writepage, 225433906bc5SMel Gorman gfp_mask); 225533906bc5SMel Gorman 2256a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 225733906bc5SMel Gorman 225833906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 225933906bc5SMel Gorman 226033906bc5SMel Gorman return nr_reclaimed; 226166e1707bSBalbir Singh } 226266e1707bSBalbir Singh 2263c255a458SAndrew Morton #ifdef CONFIG_MEMCG 226466e1707bSBalbir Singh 226572835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 22664e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 22670ae5e89cSYing Han struct zone *zone, 22680ae5e89cSYing Han unsigned long *nr_scanned) 22694e416953SBalbir Singh { 22704e416953SBalbir Singh struct scan_control sc = { 22710ae5e89cSYing Han .nr_scanned = 0, 2272b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 22734e416953SBalbir Singh .may_writepage = !laptop_mode, 22744e416953SBalbir Singh .may_unmap = 1, 22754e416953SBalbir Singh .may_swap = !noswap, 22764e416953SBalbir Singh .order = 0, 22779e3b2f8cSKonstantin Khlebnikov .priority = 0, 227872835c86SJohannes Weiner .target_mem_cgroup = memcg, 22794e416953SBalbir Singh }; 2280f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 22810ae5e89cSYing Han 22824e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 22834e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2284bdce6d9eSKOSAKI Motohiro 22859e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2286bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2287bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2288bdce6d9eSKOSAKI Motohiro 22894e416953SBalbir Singh /* 22904e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 22914e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 22924e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 22934e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 22944e416953SBalbir Singh * the priority and make it zero. 22954e416953SBalbir Singh */ 2296f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, &sc); 2297bdce6d9eSKOSAKI Motohiro 2298bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2299bdce6d9eSKOSAKI Motohiro 23000ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 23014e416953SBalbir Singh return sc.nr_reclaimed; 23024e416953SBalbir Singh } 23034e416953SBalbir Singh 230472835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 23058c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2306185efc0fSJohannes Weiner bool noswap) 230766e1707bSBalbir Singh { 23084e416953SBalbir Singh struct zonelist *zonelist; 2309bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2310889976dbSYing Han int nid; 231166e1707bSBalbir Singh struct scan_control sc = { 231266e1707bSBalbir Singh .may_writepage = !laptop_mode, 2313a6dc60f8SJohannes Weiner .may_unmap = 1, 23142e2e4259SKOSAKI Motohiro .may_swap = !noswap, 231522fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 231666e1707bSBalbir Singh .order = 0, 23179e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 231872835c86SJohannes Weiner .target_mem_cgroup = memcg, 2319327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2320a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2321a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2322a09ed5e0SYing Han }; 2323a09ed5e0SYing Han struct shrink_control shrink = { 2324a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 232566e1707bSBalbir Singh }; 232666e1707bSBalbir Singh 2327889976dbSYing Han /* 2328889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2329889976dbSYing Han * take care of from where we get pages. So the node where we start the 2330889976dbSYing Han * scan does not need to be the current node. 2331889976dbSYing Han */ 233272835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2333889976dbSYing Han 2334889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2335bdce6d9eSKOSAKI Motohiro 2336bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2337bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2338bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2339bdce6d9eSKOSAKI Motohiro 2340a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2341bdce6d9eSKOSAKI Motohiro 2342bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2343bdce6d9eSKOSAKI Motohiro 2344bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 234566e1707bSBalbir Singh } 234666e1707bSBalbir Singh #endif 234766e1707bSBalbir Singh 23489e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2349f16015fbSJohannes Weiner { 2350b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2351b95a2f2dSJohannes Weiner 2352b95a2f2dSJohannes Weiner if (!total_swap_pages) 2353b95a2f2dSJohannes Weiner return; 2354b95a2f2dSJohannes Weiner 2355b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2356b95a2f2dSJohannes Weiner do { 2357c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2358f16015fbSJohannes Weiner 2359c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 23601a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 23619e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2362b95a2f2dSJohannes Weiner 2363b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2364b95a2f2dSJohannes Weiner } while (memcg); 2365f16015fbSJohannes Weiner } 2366f16015fbSJohannes Weiner 23671741c877SMel Gorman /* 23681741c877SMel Gorman * pgdat_balanced is used when checking if a node is balanced for high-order 23691741c877SMel Gorman * allocations. Only zones that meet watermarks and are in a zone allowed 23701741c877SMel Gorman * by the callers classzone_idx are added to balanced_pages. The total of 23711741c877SMel Gorman * balanced pages must be at least 25% of the zones allowed by classzone_idx 23721741c877SMel Gorman * for the node to be considered balanced. Forcing all zones to be balanced 23731741c877SMel Gorman * for high orders can cause excessive reclaim when there are imbalanced zones. 23741741c877SMel Gorman * The choice of 25% is due to 23751741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 23761741c877SMel Gorman * reasonable sized machine 23771741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 237825985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 23791741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 23801741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 23811741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 23821741c877SMel Gorman */ 23831741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages, 23841741c877SMel Gorman int classzone_idx) 23851741c877SMel Gorman { 23861741c877SMel Gorman unsigned long present_pages = 0; 23871741c877SMel Gorman int i; 23881741c877SMel Gorman 23891741c877SMel Gorman for (i = 0; i <= classzone_idx; i++) 23901741c877SMel Gorman present_pages += pgdat->node_zones[i].present_pages; 23911741c877SMel Gorman 23924746efdeSShaohua Li /* A special case here: if zone has no page, we think it's balanced */ 23934746efdeSShaohua Li return balanced_pages >= (present_pages >> 2); 23941741c877SMel Gorman } 23951741c877SMel Gorman 23965515061dSMel Gorman /* 23975515061dSMel Gorman * Prepare kswapd for sleeping. This verifies that there are no processes 23985515061dSMel Gorman * waiting in throttle_direct_reclaim() and that watermarks have been met. 23995515061dSMel Gorman * 24005515061dSMel Gorman * Returns true if kswapd is ready to sleep 24015515061dSMel Gorman */ 24025515061dSMel Gorman static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, long remaining, 2403dc83edd9SMel Gorman int classzone_idx) 2404f50de2d3SMel Gorman { 2405bb3ab596SKOSAKI Motohiro int i; 24061741c877SMel Gorman unsigned long balanced = 0; 24071741c877SMel Gorman bool all_zones_ok = true; 2408f50de2d3SMel Gorman 2409f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2410f50de2d3SMel Gorman if (remaining) 24115515061dSMel Gorman return false; 24125515061dSMel Gorman 24135515061dSMel Gorman /* 24145515061dSMel Gorman * There is a potential race between when kswapd checks its watermarks 24155515061dSMel Gorman * and a process gets throttled. There is also a potential race if 24165515061dSMel Gorman * processes get throttled, kswapd wakes, a large process exits therby 24175515061dSMel Gorman * balancing the zones that causes kswapd to miss a wakeup. If kswapd 24185515061dSMel Gorman * is going to sleep, no process should be sleeping on pfmemalloc_wait 24195515061dSMel Gorman * so wake them now if necessary. If necessary, processes will wake 24205515061dSMel Gorman * kswapd and get throttled again 24215515061dSMel Gorman */ 24225515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait)) { 24235515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 24245515061dSMel Gorman return false; 24255515061dSMel Gorman } 2426f50de2d3SMel Gorman 24270abdee2bSMel Gorman /* Check the watermark levels */ 242808951e54SMel Gorman for (i = 0; i <= classzone_idx; i++) { 2429bb3ab596SKOSAKI Motohiro struct zone *zone = pgdat->node_zones + i; 2430bb3ab596SKOSAKI Motohiro 2431bb3ab596SKOSAKI Motohiro if (!populated_zone(zone)) 2432bb3ab596SKOSAKI Motohiro continue; 2433bb3ab596SKOSAKI Motohiro 2434355b09c4SMel Gorman /* 2435355b09c4SMel Gorman * balance_pgdat() skips over all_unreclaimable after 2436355b09c4SMel Gorman * DEF_PRIORITY. Effectively, it considers them balanced so 2437355b09c4SMel Gorman * they must be considered balanced here as well if kswapd 2438355b09c4SMel Gorman * is to sleep 2439355b09c4SMel Gorman */ 2440355b09c4SMel Gorman if (zone->all_unreclaimable) { 2441355b09c4SMel Gorman balanced += zone->present_pages; 2442de3fab39SKOSAKI Motohiro continue; 2443355b09c4SMel Gorman } 2444de3fab39SKOSAKI Motohiro 244588f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone), 2446da175d06SMel Gorman i, 0)) 24471741c877SMel Gorman all_zones_ok = false; 24481741c877SMel Gorman else 24491741c877SMel Gorman balanced += zone->present_pages; 2450bb3ab596SKOSAKI Motohiro } 2451f50de2d3SMel Gorman 24521741c877SMel Gorman /* 24531741c877SMel Gorman * For high-order requests, the balanced zones must contain at least 24541741c877SMel Gorman * 25% of the nodes pages for kswapd to sleep. For order-0, all zones 24551741c877SMel Gorman * must be balanced 24561741c877SMel Gorman */ 24571741c877SMel Gorman if (order) 24585515061dSMel Gorman return pgdat_balanced(pgdat, balanced, classzone_idx); 24591741c877SMel Gorman else 24605515061dSMel Gorman return all_zones_ok; 2461f50de2d3SMel Gorman } 2462f50de2d3SMel Gorman 24631da177e4SLinus Torvalds /* 24641da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 246541858966SMel Gorman * they are all at high_wmark_pages(zone). 24661da177e4SLinus Torvalds * 24670abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 24681da177e4SLinus Torvalds * 24691da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 24701da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 24711da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 24721da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 24731da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 24741da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 24751da177e4SLinus Torvalds * the zone for when the problem goes away. 24761da177e4SLinus Torvalds * 24771da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 247841858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 247941858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 248041858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 248141858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 248241858966SMel Gorman * of pages is balanced across the zones. 24831da177e4SLinus Torvalds */ 248499504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2485dc83edd9SMel Gorman int *classzone_idx) 24861da177e4SLinus Torvalds { 24871da177e4SLinus Torvalds int all_zones_ok; 24881741c877SMel Gorman unsigned long balanced; 24891da177e4SLinus Torvalds int i; 249099504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 249169e05944SAndrew Morton unsigned long total_scanned; 24921da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 24930ae5e89cSYing Han unsigned long nr_soft_reclaimed; 24940ae5e89cSYing Han unsigned long nr_soft_scanned; 2495179e9639SAndrew Morton struct scan_control sc = { 2496179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2497a6dc60f8SJohannes Weiner .may_unmap = 1, 24982e2e4259SKOSAKI Motohiro .may_swap = 1, 249922fba335SKOSAKI Motohiro /* 250022fba335SKOSAKI Motohiro * kswapd doesn't want to be bailed out while reclaim. because 250122fba335SKOSAKI Motohiro * we want to put equal scanning pressure on each zone. 250222fba335SKOSAKI Motohiro */ 250322fba335SKOSAKI Motohiro .nr_to_reclaim = ULONG_MAX, 25045ad333ebSAndy Whitcroft .order = order, 2505f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2506179e9639SAndrew Morton }; 2507a09ed5e0SYing Han struct shrink_control shrink = { 2508a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2509a09ed5e0SYing Han }; 25101da177e4SLinus Torvalds loop_again: 25111da177e4SLinus Torvalds total_scanned = 0; 25129e3b2f8cSKonstantin Khlebnikov sc.priority = DEF_PRIORITY; 2513a79311c1SRik van Riel sc.nr_reclaimed = 0; 2514c0bbbc73SChristoph Lameter sc.may_writepage = !laptop_mode; 2515f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 25161da177e4SLinus Torvalds 25179e3b2f8cSKonstantin Khlebnikov do { 25181da177e4SLinus Torvalds unsigned long lru_pages = 0; 2519bb3ab596SKOSAKI Motohiro int has_under_min_watermark_zone = 0; 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds all_zones_ok = 1; 25221741c877SMel Gorman balanced = 0; 25231da177e4SLinus Torvalds 25241da177e4SLinus Torvalds /* 25251da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 25261da177e4SLinus Torvalds * zone which needs scanning 25271da177e4SLinus Torvalds */ 25281da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 25291da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 25301da177e4SLinus Torvalds 2531f3fe6512SCon Kolivas if (!populated_zone(zone)) 25321da177e4SLinus Torvalds continue; 25331da177e4SLinus Torvalds 25349e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 25359e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 25361da177e4SLinus Torvalds continue; 25371da177e4SLinus Torvalds 2538556adecbSRik van Riel /* 2539556adecbSRik van Riel * Do some background aging of the anon list, to give 2540556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2541556adecbSRik van Riel */ 25429e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2543556adecbSRik van Riel 2544cc715d99SMel Gorman /* 2545cc715d99SMel Gorman * If the number of buffer_heads in the machine 2546cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2547cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2548cc715d99SMel Gorman * it to relieve lowmem pressure. 2549cc715d99SMel Gorman */ 2550cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2551cc715d99SMel Gorman end_zone = i; 2552cc715d99SMel Gorman break; 2553cc715d99SMel Gorman } 2554cc715d99SMel Gorman 255588f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 255641858966SMel Gorman high_wmark_pages(zone), 0, 0)) { 25571da177e4SLinus Torvalds end_zone = i; 2558e1dbeda6SAndrew Morton break; 2559439423f6SShaohua Li } else { 2560439423f6SShaohua Li /* If balanced, clear the congested flag */ 2561439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds } 2564e1dbeda6SAndrew Morton if (i < 0) 25651da177e4SLinus Torvalds goto out; 2566e1dbeda6SAndrew Morton 25671da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 25681da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 25691da177e4SLinus Torvalds 2570adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 25711da177e4SLinus Torvalds } 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds /* 25741da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 25751da177e4SLinus Torvalds * at the last zone which needs scanning. 25761da177e4SLinus Torvalds * 25771da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 25781da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 25791da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 25801da177e4SLinus Torvalds * cause too much scanning of the lower zones. 25811da177e4SLinus Torvalds */ 25821da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 25831da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 2584fe2c2a10SRik van Riel int nr_slab, testorder; 25858afdceceSMel Gorman unsigned long balance_gap; 25861da177e4SLinus Torvalds 2587f3fe6512SCon Kolivas if (!populated_zone(zone)) 25881da177e4SLinus Torvalds continue; 25891da177e4SLinus Torvalds 25909e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 25919e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 25921da177e4SLinus Torvalds continue; 25931da177e4SLinus Torvalds 25941da177e4SLinus Torvalds sc.nr_scanned = 0; 25954e416953SBalbir Singh 25960ae5e89cSYing Han nr_soft_scanned = 0; 25974e416953SBalbir Singh /* 25984e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 25994e416953SBalbir Singh */ 26000ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 26010ae5e89cSYing Han order, sc.gfp_mask, 26020ae5e89cSYing Han &nr_soft_scanned); 26030ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 26040ae5e89cSYing Han total_scanned += nr_soft_scanned; 260500918b6aSKOSAKI Motohiro 260632a4330dSRik van Riel /* 26078afdceceSMel Gorman * We put equal pressure on every zone, unless 26088afdceceSMel Gorman * one zone has way too many pages free 26098afdceceSMel Gorman * already. The "too many pages" is defined 26108afdceceSMel Gorman * as the high wmark plus a "gap" where the 26118afdceceSMel Gorman * gap is either the low watermark or 1% 26128afdceceSMel Gorman * of the zone, whichever is smaller. 261332a4330dSRik van Riel */ 26148afdceceSMel Gorman balance_gap = min(low_wmark_pages(zone), 26158afdceceSMel Gorman (zone->present_pages + 26168afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 26178afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2618fe2c2a10SRik van Riel /* 2619fe2c2a10SRik van Riel * Kswapd reclaims only single pages with compaction 2620fe2c2a10SRik van Riel * enabled. Trying too hard to reclaim until contiguous 2621fe2c2a10SRik van Riel * free pages have become available can hurt performance 2622fe2c2a10SRik van Riel * by evicting too much useful data from memory. 2623fe2c2a10SRik van Riel * Do not reclaim more than needed for compaction. 2624fe2c2a10SRik van Riel */ 2625fe2c2a10SRik van Riel testorder = order; 2626fe2c2a10SRik van Riel if (COMPACTION_BUILD && order && 2627fe2c2a10SRik van Riel compaction_suitable(zone, order) != 2628fe2c2a10SRik van Riel COMPACT_SKIPPED) 2629fe2c2a10SRik van Riel testorder = 0; 2630fe2c2a10SRik van Riel 2631cc715d99SMel Gorman if ((buffer_heads_over_limit && is_highmem_idx(i)) || 2632643ac9fcSHugh Dickins !zone_watermark_ok_safe(zone, testorder, 26338afdceceSMel Gorman high_wmark_pages(zone) + balance_gap, 2634d7868daeSMel Gorman end_zone, 0)) { 26359e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 2636d7868daeSMel Gorman 26371da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 26381495f230SYing Han nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages); 2639a79311c1SRik van Riel sc.nr_reclaimed += reclaim_state->reclaimed_slab; 26401da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 26415a03b051SAndrea Arcangeli 2642d7868daeSMel Gorman if (nr_slab == 0 && !zone_reclaimable(zone)) 264393e4a89aSKOSAKI Motohiro zone->all_unreclaimable = 1; 2644d7868daeSMel Gorman } 2645d7868daeSMel Gorman 26461da177e4SLinus Torvalds /* 26471da177e4SLinus Torvalds * If we've done a decent amount of scanning and 26481da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 26491da177e4SLinus Torvalds * even in laptop mode 26501da177e4SLinus Torvalds */ 26511da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 2652a79311c1SRik van Riel total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) 26531da177e4SLinus Torvalds sc.may_writepage = 1; 2654bb3ab596SKOSAKI Motohiro 2655215ddd66SMel Gorman if (zone->all_unreclaimable) { 2656215ddd66SMel Gorman if (end_zone && end_zone == i) 2657215ddd66SMel Gorman end_zone--; 2658d7868daeSMel Gorman continue; 2659215ddd66SMel Gorman } 2660d7868daeSMel Gorman 2661fe2c2a10SRik van Riel if (!zone_watermark_ok_safe(zone, testorder, 266245973d74SMinchan Kim high_wmark_pages(zone), end_zone, 0)) { 266345973d74SMinchan Kim all_zones_ok = 0; 2664bb3ab596SKOSAKI Motohiro /* 266545973d74SMinchan Kim * We are still under min water mark. This 266645973d74SMinchan Kim * means that we have a GFP_ATOMIC allocation 266745973d74SMinchan Kim * failure risk. Hurry up! 2668bb3ab596SKOSAKI Motohiro */ 266988f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 267045973d74SMinchan Kim min_wmark_pages(zone), end_zone, 0)) 2671bb3ab596SKOSAKI Motohiro has_under_min_watermark_zone = 1; 26720e093d99SMel Gorman } else { 26730e093d99SMel Gorman /* 26740e093d99SMel Gorman * If a zone reaches its high watermark, 26750e093d99SMel Gorman * consider it to be no longer congested. It's 26760e093d99SMel Gorman * possible there are dirty pages backed by 26770e093d99SMel Gorman * congested BDIs but as pressure is relieved, 2678ab8704b8SWanpeng Li * speculatively avoid congestion waits 26790e093d99SMel Gorman */ 26800e093d99SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 2681dc83edd9SMel Gorman if (i <= *classzone_idx) 26821741c877SMel Gorman balanced += zone->present_pages; 268345973d74SMinchan Kim } 2684bb3ab596SKOSAKI Motohiro 26851da177e4SLinus Torvalds } 26865515061dSMel Gorman 26875515061dSMel Gorman /* 26885515061dSMel Gorman * If the low watermark is met there is no need for processes 26895515061dSMel Gorman * to be throttled on pfmemalloc_wait as they should not be 26905515061dSMel Gorman * able to safely make forward progress. Wake them 26915515061dSMel Gorman */ 26925515061dSMel Gorman if (waitqueue_active(&pgdat->pfmemalloc_wait) && 26935515061dSMel Gorman pfmemalloc_watermark_ok(pgdat)) 26945515061dSMel Gorman wake_up(&pgdat->pfmemalloc_wait); 26955515061dSMel Gorman 2696dc83edd9SMel Gorman if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx))) 26971da177e4SLinus Torvalds break; /* kswapd: all done */ 26981da177e4SLinus Torvalds /* 26991da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 27001da177e4SLinus Torvalds * another pass across the zones. 27011da177e4SLinus Torvalds */ 27029e3b2f8cSKonstantin Khlebnikov if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) { 2703bb3ab596SKOSAKI Motohiro if (has_under_min_watermark_zone) 2704bb3ab596SKOSAKI Motohiro count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); 2705bb3ab596SKOSAKI Motohiro else 27068aa7e847SJens Axboe congestion_wait(BLK_RW_ASYNC, HZ/10); 2707bb3ab596SKOSAKI Motohiro } 27081da177e4SLinus Torvalds 27091da177e4SLinus Torvalds /* 27101da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 27111da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 27121da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 27131da177e4SLinus Torvalds * on zone->*_priority. 27141da177e4SLinus Torvalds */ 2715a79311c1SRik van Riel if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) 27161da177e4SLinus Torvalds break; 27179e3b2f8cSKonstantin Khlebnikov } while (--sc.priority >= 0); 27181da177e4SLinus Torvalds out: 271999504748SMel Gorman 272099504748SMel Gorman /* 272199504748SMel Gorman * order-0: All zones must meet high watermark for a balanced node 27221741c877SMel Gorman * high-order: Balanced zones must make up at least 25% of the node 27231741c877SMel Gorman * for the node to be balanced 272499504748SMel Gorman */ 2725dc83edd9SMel Gorman if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) { 27261da177e4SLinus Torvalds cond_resched(); 27278357376dSRafael J. Wysocki 27288357376dSRafael J. Wysocki try_to_freeze(); 27298357376dSRafael J. Wysocki 273073ce02e9SKOSAKI Motohiro /* 273173ce02e9SKOSAKI Motohiro * Fragmentation may mean that the system cannot be 273273ce02e9SKOSAKI Motohiro * rebalanced for high-order allocations in all zones. 273373ce02e9SKOSAKI Motohiro * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, 273473ce02e9SKOSAKI Motohiro * it means the zones have been fully scanned and are still 273573ce02e9SKOSAKI Motohiro * not balanced. For high-order allocations, there is 273673ce02e9SKOSAKI Motohiro * little point trying all over again as kswapd may 273773ce02e9SKOSAKI Motohiro * infinite loop. 273873ce02e9SKOSAKI Motohiro * 273973ce02e9SKOSAKI Motohiro * Instead, recheck all watermarks at order-0 as they 274073ce02e9SKOSAKI Motohiro * are the most important. If watermarks are ok, kswapd will go 274173ce02e9SKOSAKI Motohiro * back to sleep. High-order users can still perform direct 274273ce02e9SKOSAKI Motohiro * reclaim if they wish. 274373ce02e9SKOSAKI Motohiro */ 274473ce02e9SKOSAKI Motohiro if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) 274573ce02e9SKOSAKI Motohiro order = sc.order = 0; 274673ce02e9SKOSAKI Motohiro 27471da177e4SLinus Torvalds goto loop_again; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 275099504748SMel Gorman /* 275199504748SMel Gorman * If kswapd was reclaiming at a higher order, it has the option of 275299504748SMel Gorman * sleeping without all zones being balanced. Before it does, it must 275399504748SMel Gorman * ensure that the watermarks for order-0 on *all* zones are met and 275499504748SMel Gorman * that the congestion flags are cleared. The congestion flag must 275599504748SMel Gorman * be cleared as kswapd is the only mechanism that clears the flag 275699504748SMel Gorman * and it is potentially going to sleep here. 275799504748SMel Gorman */ 275899504748SMel Gorman if (order) { 27597be62de9SRik van Riel int zones_need_compaction = 1; 27607be62de9SRik van Riel 276199504748SMel Gorman for (i = 0; i <= end_zone; i++) { 276299504748SMel Gorman struct zone *zone = pgdat->node_zones + i; 276399504748SMel Gorman 276499504748SMel Gorman if (!populated_zone(zone)) 276599504748SMel Gorman continue; 276699504748SMel Gorman 27679e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 27689e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 276999504748SMel Gorman continue; 277099504748SMel Gorman 2771fe2c2a10SRik van Riel /* Would compaction fail due to lack of free memory? */ 2772496b919bSRik van Riel if (COMPACTION_BUILD && 2773496b919bSRik van Riel compaction_suitable(zone, order) == COMPACT_SKIPPED) 2774fe2c2a10SRik van Riel goto loop_again; 2775fe2c2a10SRik van Riel 277699504748SMel Gorman /* Confirm the zone is balanced for order-0 */ 277799504748SMel Gorman if (!zone_watermark_ok(zone, 0, 277899504748SMel Gorman high_wmark_pages(zone), 0, 0)) { 277999504748SMel Gorman order = sc.order = 0; 278099504748SMel Gorman goto loop_again; 278199504748SMel Gorman } 278299504748SMel Gorman 27837be62de9SRik van Riel /* Check if the memory needs to be defragmented. */ 27847be62de9SRik van Riel if (zone_watermark_ok(zone, order, 27857be62de9SRik van Riel low_wmark_pages(zone), *classzone_idx, 0)) 27867be62de9SRik van Riel zones_need_compaction = 0; 27877be62de9SRik van Riel 278899504748SMel Gorman /* If balanced, clear the congested flag */ 278999504748SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 279099504748SMel Gorman } 27917be62de9SRik van Riel 27927be62de9SRik van Riel if (zones_need_compaction) 27937be62de9SRik van Riel compact_pgdat(pgdat, order); 279499504748SMel Gorman } 279599504748SMel Gorman 27960abdee2bSMel Gorman /* 27975515061dSMel Gorman * Return the order we were reclaiming at so prepare_kswapd_sleep() 27980abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 27990abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 28000abdee2bSMel Gorman * was awake, order will remain at the higher level 28010abdee2bSMel Gorman */ 2802dc83edd9SMel Gorman *classzone_idx = end_zone; 28030abdee2bSMel Gorman return order; 28041da177e4SLinus Torvalds } 28051da177e4SLinus Torvalds 2806dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 2807f0bc0a60SKOSAKI Motohiro { 2808f0bc0a60SKOSAKI Motohiro long remaining = 0; 2809f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 2810f0bc0a60SKOSAKI Motohiro 2811f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 2812f0bc0a60SKOSAKI Motohiro return; 2813f0bc0a60SKOSAKI Motohiro 2814f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2815f0bc0a60SKOSAKI Motohiro 2816f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 28175515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 2818f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 2819f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2820f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2821f0bc0a60SKOSAKI Motohiro } 2822f0bc0a60SKOSAKI Motohiro 2823f0bc0a60SKOSAKI Motohiro /* 2824f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 2825f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 2826f0bc0a60SKOSAKI Motohiro */ 28275515061dSMel Gorman if (prepare_kswapd_sleep(pgdat, order, remaining, classzone_idx)) { 2828f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 2829f0bc0a60SKOSAKI Motohiro 2830f0bc0a60SKOSAKI Motohiro /* 2831f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 2832f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 2833f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 2834f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 2835f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 2836f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 2837f0bc0a60SKOSAKI Motohiro */ 2838f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 28391c7e7f6cSAaditya Kumar 28401c7e7f6cSAaditya Kumar if (!kthread_should_stop()) 2841f0bc0a60SKOSAKI Motohiro schedule(); 28421c7e7f6cSAaditya Kumar 2843f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 2844f0bc0a60SKOSAKI Motohiro } else { 2845f0bc0a60SKOSAKI Motohiro if (remaining) 2846f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 2847f0bc0a60SKOSAKI Motohiro else 2848f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 2849f0bc0a60SKOSAKI Motohiro } 2850f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2851f0bc0a60SKOSAKI Motohiro } 2852f0bc0a60SKOSAKI Motohiro 28531da177e4SLinus Torvalds /* 28541da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 28551da177e4SLinus Torvalds * from the init process. 28561da177e4SLinus Torvalds * 28571da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 28581da177e4SLinus Torvalds * free memory available even if there is no other activity 28591da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 28601da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 28611da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 28621da177e4SLinus Torvalds * 28631da177e4SLinus Torvalds * If there are applications that are active memory-allocators 28641da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 28651da177e4SLinus Torvalds */ 28661da177e4SLinus Torvalds static int kswapd(void *p) 28671da177e4SLinus Torvalds { 2868215ddd66SMel Gorman unsigned long order, new_order; 2869d2ebd0f6SAlex,Shi unsigned balanced_order; 2870215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 2871d2ebd0f6SAlex,Shi int balanced_classzone_idx; 28721da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 28731da177e4SLinus Torvalds struct task_struct *tsk = current; 2874f0bc0a60SKOSAKI Motohiro 28751da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 28761da177e4SLinus Torvalds .reclaimed_slab = 0, 28771da177e4SLinus Torvalds }; 2878a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 28791da177e4SLinus Torvalds 2880cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 2881cf40bd16SNick Piggin 2882174596a0SRusty Russell if (!cpumask_empty(cpumask)) 2883c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 28841da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 28851da177e4SLinus Torvalds 28861da177e4SLinus Torvalds /* 28871da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 28881da177e4SLinus Torvalds * and that if we need more memory we should get access to it 28891da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 28901da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 28911da177e4SLinus Torvalds * 28921da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 28931da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 28941da177e4SLinus Torvalds * page out something else, and this flag essentially protects 28951da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 28961da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 28971da177e4SLinus Torvalds */ 2898930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 289983144186SRafael J. Wysocki set_freezable(); 29001da177e4SLinus Torvalds 2901215ddd66SMel Gorman order = new_order = 0; 2902d2ebd0f6SAlex,Shi balanced_order = 0; 2903215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 2904d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 29051da177e4SLinus Torvalds for ( ; ; ) { 29068fe23e05SDavid Rientjes int ret; 29073e1d1d28SChristoph Lameter 2908215ddd66SMel Gorman /* 2909215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 2910215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 2911215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 2912215ddd66SMel Gorman */ 2913d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 2914d2ebd0f6SAlex,Shi balanced_order == new_order) { 29151da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 291699504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 29171da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 2918215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 2919215ddd66SMel Gorman } 2920215ddd66SMel Gorman 292199504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 29221da177e4SLinus Torvalds /* 29231da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 292499504748SMel Gorman * allocation or has tigher zone constraints 29251da177e4SLinus Torvalds */ 29261da177e4SLinus Torvalds order = new_order; 292799504748SMel Gorman classzone_idx = new_classzone_idx; 29281da177e4SLinus Torvalds } else { 2929d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 2930d2ebd0f6SAlex,Shi balanced_classzone_idx); 29311da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 293299504748SMel Gorman classzone_idx = pgdat->classzone_idx; 2933f0dfcde0SAlex,Shi new_order = order; 2934f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 29354d40502eSMel Gorman pgdat->kswapd_max_order = 0; 2936215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 29371da177e4SLinus Torvalds } 29381da177e4SLinus Torvalds 29398fe23e05SDavid Rientjes ret = try_to_freeze(); 29408fe23e05SDavid Rientjes if (kthread_should_stop()) 29418fe23e05SDavid Rientjes break; 29428fe23e05SDavid Rientjes 29438fe23e05SDavid Rientjes /* 29448fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 29458fe23e05SDavid Rientjes * after returning from the refrigerator 2946b1296cc4SRafael J. Wysocki */ 294733906bc5SMel Gorman if (!ret) { 294833906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 2949d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 2950d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 2951d2ebd0f6SAlex,Shi &balanced_classzone_idx); 29521da177e4SLinus Torvalds } 295333906bc5SMel Gorman } 29541da177e4SLinus Torvalds return 0; 29551da177e4SLinus Torvalds } 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds /* 29581da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 29591da177e4SLinus Torvalds */ 296099504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds pg_data_t *pgdat; 29631da177e4SLinus Torvalds 2964f3fe6512SCon Kolivas if (!populated_zone(zone)) 29651da177e4SLinus Torvalds return; 29661da177e4SLinus Torvalds 296702a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 29681da177e4SLinus Torvalds return; 296988f5acf8SMel Gorman pgdat = zone->zone_pgdat; 297099504748SMel Gorman if (pgdat->kswapd_max_order < order) { 297188f5acf8SMel Gorman pgdat->kswapd_max_order = order; 297299504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 297399504748SMel Gorman } 29748d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 29751da177e4SLinus Torvalds return; 297688f5acf8SMel Gorman if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) 297788f5acf8SMel Gorman return; 297888f5acf8SMel Gorman 297988f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 29808d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 29811da177e4SLinus Torvalds } 29821da177e4SLinus Torvalds 2983adea02a1SWu Fengguang /* 2984adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 2985adea02a1SWu Fengguang * The less reclaimable pages may be 2986adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 2987adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 2988adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 2989adea02a1SWu Fengguang */ 2990adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 29914f98a2feSRik van Riel { 2992adea02a1SWu Fengguang int nr; 2993adea02a1SWu Fengguang 2994adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 2995adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 2996adea02a1SWu Fengguang 2997adea02a1SWu Fengguang if (nr_swap_pages > 0) 2998adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 2999adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 3000adea02a1SWu Fengguang 3001adea02a1SWu Fengguang return nr; 3002adea02a1SWu Fengguang } 3003adea02a1SWu Fengguang 3004adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone) 3005adea02a1SWu Fengguang { 3006adea02a1SWu Fengguang int nr; 3007adea02a1SWu Fengguang 3008adea02a1SWu Fengguang nr = zone_page_state(zone, NR_ACTIVE_FILE) + 3009adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_FILE); 3010adea02a1SWu Fengguang 3011adea02a1SWu Fengguang if (nr_swap_pages > 0) 3012adea02a1SWu Fengguang nr += zone_page_state(zone, NR_ACTIVE_ANON) + 3013adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_ANON); 3014adea02a1SWu Fengguang 3015adea02a1SWu Fengguang return nr; 30164f98a2feSRik van Riel } 30174f98a2feSRik van Riel 3018c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 30191da177e4SLinus Torvalds /* 30207b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 3021d6277db4SRafael J. Wysocki * freed pages. 3022d6277db4SRafael J. Wysocki * 3023d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 3024d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 3025d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 30261da177e4SLinus Torvalds */ 30277b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 30281da177e4SLinus Torvalds { 3029d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 3030d6277db4SRafael J. Wysocki struct scan_control sc = { 30317b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 30327b51755cSKOSAKI Motohiro .may_swap = 1, 30337b51755cSKOSAKI Motohiro .may_unmap = 1, 3034d6277db4SRafael J. Wysocki .may_writepage = 1, 30357b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 30367b51755cSKOSAKI Motohiro .hibernation_mode = 1, 30377b51755cSKOSAKI Motohiro .order = 0, 30389e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 30391da177e4SLinus Torvalds }; 3040a09ed5e0SYing Han struct shrink_control shrink = { 3041a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3042a09ed5e0SYing Han }; 30437b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 30447b51755cSKOSAKI Motohiro struct task_struct *p = current; 30457b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 30461da177e4SLinus Torvalds 30477b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 30487b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 3049d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 30507b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 3051d6277db4SRafael J. Wysocki 3052a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 3053d6277db4SRafael J. Wysocki 30547b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 30557b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 30567b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 3057d6277db4SRafael J. Wysocki 30587b51755cSKOSAKI Motohiro return nr_reclaimed; 30591da177e4SLinus Torvalds } 3060c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 30611da177e4SLinus Torvalds 30621da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 30631da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 30641da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 30651da177e4SLinus Torvalds restore their cpu bindings. */ 30669c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb, 306769e05944SAndrew Morton unsigned long action, void *hcpu) 30681da177e4SLinus Torvalds { 306958c0a4a7SYasunori Goto int nid; 30701da177e4SLinus Torvalds 30718bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 307258c0a4a7SYasunori Goto for_each_node_state(nid, N_HIGH_MEMORY) { 3073c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3074a70f7302SRusty Russell const struct cpumask *mask; 3075a70f7302SRusty Russell 3076a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3077c5f59f08SMike Travis 30783e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 30791da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3080c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 30811da177e4SLinus Torvalds } 30821da177e4SLinus Torvalds } 30831da177e4SLinus Torvalds return NOTIFY_OK; 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 30863218ae14SYasunori Goto /* 30873218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 30883218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 30893218ae14SYasunori Goto */ 30903218ae14SYasunori Goto int kswapd_run(int nid) 30913218ae14SYasunori Goto { 30923218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 30933218ae14SYasunori Goto int ret = 0; 30943218ae14SYasunori Goto 30953218ae14SYasunori Goto if (pgdat->kswapd) 30963218ae14SYasunori Goto return 0; 30973218ae14SYasunori Goto 30983218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 30993218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 31003218ae14SYasunori Goto /* failure at boot is fatal */ 31013218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 31023218ae14SYasunori Goto printk("Failed to start kswapd on node %d\n",nid); 31033218ae14SYasunori Goto ret = -1; 31043218ae14SYasunori Goto } 31053218ae14SYasunori Goto return ret; 31063218ae14SYasunori Goto } 31073218ae14SYasunori Goto 31088fe23e05SDavid Rientjes /* 3109d8adde17SJiang Liu * Called by memory hotplug when all memory in a node is offlined. Caller must 3110d8adde17SJiang Liu * hold lock_memory_hotplug(). 31118fe23e05SDavid Rientjes */ 31128fe23e05SDavid Rientjes void kswapd_stop(int nid) 31138fe23e05SDavid Rientjes { 31148fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 31158fe23e05SDavid Rientjes 3116d8adde17SJiang Liu if (kswapd) { 31178fe23e05SDavid Rientjes kthread_stop(kswapd); 3118d8adde17SJiang Liu NODE_DATA(nid)->kswapd = NULL; 3119d8adde17SJiang Liu } 31208fe23e05SDavid Rientjes } 31218fe23e05SDavid Rientjes 31221da177e4SLinus Torvalds static int __init kswapd_init(void) 31231da177e4SLinus Torvalds { 31243218ae14SYasunori Goto int nid; 312569e05944SAndrew Morton 31261da177e4SLinus Torvalds swap_setup(); 31279422ffbaSChristoph Lameter for_each_node_state(nid, N_HIGH_MEMORY) 31283218ae14SYasunori Goto kswapd_run(nid); 31291da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 31301da177e4SLinus Torvalds return 0; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds module_init(kswapd_init) 31349eeff239SChristoph Lameter 31359eeff239SChristoph Lameter #ifdef CONFIG_NUMA 31369eeff239SChristoph Lameter /* 31379eeff239SChristoph Lameter * Zone reclaim mode 31389eeff239SChristoph Lameter * 31399eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 31409eeff239SChristoph Lameter * the watermarks. 31419eeff239SChristoph Lameter */ 31429eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 31439eeff239SChristoph Lameter 31441b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 31457d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 31461b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 31471b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 31481b2ffb78SChristoph Lameter 31499eeff239SChristoph Lameter /* 3150a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3151a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3152a92f7126SChristoph Lameter * a zone. 3153a92f7126SChristoph Lameter */ 3154a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3155a92f7126SChristoph Lameter 31569eeff239SChristoph Lameter /* 31579614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 31589614634fSChristoph Lameter * occur. 31599614634fSChristoph Lameter */ 31609614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 31619614634fSChristoph Lameter 31629614634fSChristoph Lameter /* 31630ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 31640ff38490SChristoph Lameter * slab reclaim needs to occur. 31650ff38490SChristoph Lameter */ 31660ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 31670ff38490SChristoph Lameter 316890afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 316990afa5deSMel Gorman { 317090afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 317190afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 317290afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 317390afa5deSMel Gorman 317490afa5deSMel Gorman /* 317590afa5deSMel Gorman * It's possible for there to be more file mapped pages than 317690afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 317790afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 317890afa5deSMel Gorman */ 317990afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 318090afa5deSMel Gorman } 318190afa5deSMel Gorman 318290afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 318390afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 318490afa5deSMel Gorman { 318590afa5deSMel Gorman long nr_pagecache_reclaimable; 318690afa5deSMel Gorman long delta = 0; 318790afa5deSMel Gorman 318890afa5deSMel Gorman /* 318990afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 319090afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 319190afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 319290afa5deSMel Gorman * a better estimate 319390afa5deSMel Gorman */ 319490afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 319590afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 319690afa5deSMel Gorman else 319790afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 319890afa5deSMel Gorman 319990afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 320090afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 320190afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 320290afa5deSMel Gorman 320390afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 320490afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 320590afa5deSMel Gorman delta = nr_pagecache_reclaimable; 320690afa5deSMel Gorman 320790afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 320890afa5deSMel Gorman } 320990afa5deSMel Gorman 32100ff38490SChristoph Lameter /* 32119eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 32129eeff239SChristoph Lameter */ 3213179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 32149eeff239SChristoph Lameter { 32157fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 321669e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 32179eeff239SChristoph Lameter struct task_struct *p = current; 32189eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3219179e9639SAndrew Morton struct scan_control sc = { 3220179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3221a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 32222e2e4259SKOSAKI Motohiro .may_swap = 1, 322322fba335SKOSAKI Motohiro .nr_to_reclaim = max_t(unsigned long, nr_pages, 322422fba335SKOSAKI Motohiro SWAP_CLUSTER_MAX), 3225179e9639SAndrew Morton .gfp_mask = gfp_mask, 3226bd2f6199SJohannes Weiner .order = order, 32279e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3228179e9639SAndrew Morton }; 3229a09ed5e0SYing Han struct shrink_control shrink = { 3230a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3231a09ed5e0SYing Han }; 323215748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 32339eeff239SChristoph Lameter 32349eeff239SChristoph Lameter cond_resched(); 3235d4f7796eSChristoph Lameter /* 3236d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3237d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3238d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3239d4f7796eSChristoph Lameter */ 3240d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 324176ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 32429eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 32439eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3244c84db23cSChristoph Lameter 324590afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3246a92f7126SChristoph Lameter /* 32470ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 32480ff38490SChristoph Lameter * priorities until we have enough memory freed. 3249a92f7126SChristoph Lameter */ 3250a92f7126SChristoph Lameter do { 32519e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 32529e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 32530ff38490SChristoph Lameter } 3254a92f7126SChristoph Lameter 325515748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 325615748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 32572a16e3f4SChristoph Lameter /* 32587fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 32590ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 32600ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 32610ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 32620ff38490SChristoph Lameter * pages. 32632a16e3f4SChristoph Lameter * 32640ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 32650ff38490SChristoph Lameter * take a long time. 32662a16e3f4SChristoph Lameter */ 32674dc4b3d9SKOSAKI Motohiro for (;;) { 32684dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 32694dc4b3d9SKOSAKI Motohiro 32704dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 32711495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 32724dc4b3d9SKOSAKI Motohiro break; 32734dc4b3d9SKOSAKI Motohiro 32744dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 32754dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 32764dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 32774dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 32784dc4b3d9SKOSAKI Motohiro break; 32794dc4b3d9SKOSAKI Motohiro } 328083e33a47SChristoph Lameter 328183e33a47SChristoph Lameter /* 328283e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 328383e33a47SChristoph Lameter * reclaimed from this zone. 328483e33a47SChristoph Lameter */ 328515748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 328615748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 328715748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 32882a16e3f4SChristoph Lameter } 32892a16e3f4SChristoph Lameter 32909eeff239SChristoph Lameter p->reclaim_state = NULL; 3291d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 329276ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3293a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 32949eeff239SChristoph Lameter } 3295179e9639SAndrew Morton 3296179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3297179e9639SAndrew Morton { 3298179e9639SAndrew Morton int node_id; 3299d773ed6bSDavid Rientjes int ret; 3300179e9639SAndrew Morton 3301179e9639SAndrew Morton /* 33020ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 33030ff38490SChristoph Lameter * slab pages if we are over the defined limits. 330434aa1330SChristoph Lameter * 33059614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 33069614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 33079614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 33089614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 33099614634fSChristoph Lameter * unmapped file backed pages. 3310179e9639SAndrew Morton */ 331190afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 331290afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3313fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3314179e9639SAndrew Morton 331593e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable) 3316fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3317d773ed6bSDavid Rientjes 3318179e9639SAndrew Morton /* 3319d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3320179e9639SAndrew Morton */ 3321d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3322fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3323179e9639SAndrew Morton 3324179e9639SAndrew Morton /* 3325179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3326179e9639SAndrew Morton * have associated processors. This will favor the local processor 3327179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3328179e9639SAndrew Morton * as wide as possible. 3329179e9639SAndrew Morton */ 333089fa3024SChristoph Lameter node_id = zone_to_nid(zone); 333137c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3332fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3333d773ed6bSDavid Rientjes 3334d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3335fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3336fa5e084eSMel Gorman 3337d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3338d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3339d773ed6bSDavid Rientjes 334024cf7251SMel Gorman if (!ret) 334124cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 334224cf7251SMel Gorman 3343d773ed6bSDavid Rientjes return ret; 3344179e9639SAndrew Morton } 33459eeff239SChristoph Lameter #endif 3346894bc310SLee Schermerhorn 3347894bc310SLee Schermerhorn /* 3348894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3349894bc310SLee Schermerhorn * @page: the page to test 3350894bc310SLee Schermerhorn * @vma: the VMA in which the page is or will be mapped, may be NULL 3351894bc310SLee Schermerhorn * 3352894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 3353b291f000SNick Piggin * lists vs unevictable list. The vma argument is !NULL when called from the 3354b291f000SNick Piggin * fault path to determine how to instantate a new page. 3355894bc310SLee Schermerhorn * 3356894bc310SLee Schermerhorn * Reasons page might not be evictable: 3357ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3358b291f000SNick Piggin * (2) page is part of an mlocked VMA 3359ba9ddf49SLee Schermerhorn * 3360894bc310SLee Schermerhorn */ 3361894bc310SLee Schermerhorn int page_evictable(struct page *page, struct vm_area_struct *vma) 3362894bc310SLee Schermerhorn { 3363894bc310SLee Schermerhorn 3364ba9ddf49SLee Schermerhorn if (mapping_unevictable(page_mapping(page))) 3365ba9ddf49SLee Schermerhorn return 0; 3366ba9ddf49SLee Schermerhorn 3367096a7cf4SYing Han if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page))) 3368b291f000SNick Piggin return 0; 3369894bc310SLee Schermerhorn 3370894bc310SLee Schermerhorn return 1; 3371894bc310SLee Schermerhorn } 337289e004eaSLee Schermerhorn 337385046579SHugh Dickins #ifdef CONFIG_SHMEM 337489e004eaSLee Schermerhorn /** 337524513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 337624513264SHugh Dickins * @pages: array of pages to check 337724513264SHugh Dickins * @nr_pages: number of pages to check 337889e004eaSLee Schermerhorn * 337924513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 338085046579SHugh Dickins * 338185046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 338289e004eaSLee Schermerhorn */ 338324513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 338489e004eaSLee Schermerhorn { 3385925b7673SJohannes Weiner struct lruvec *lruvec; 338624513264SHugh Dickins struct zone *zone = NULL; 338724513264SHugh Dickins int pgscanned = 0; 338824513264SHugh Dickins int pgrescued = 0; 338989e004eaSLee Schermerhorn int i; 339089e004eaSLee Schermerhorn 339124513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 339224513264SHugh Dickins struct page *page = pages[i]; 339324513264SHugh Dickins struct zone *pagezone; 339489e004eaSLee Schermerhorn 339524513264SHugh Dickins pgscanned++; 339624513264SHugh Dickins pagezone = page_zone(page); 339789e004eaSLee Schermerhorn if (pagezone != zone) { 339889e004eaSLee Schermerhorn if (zone) 339989e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 340089e004eaSLee Schermerhorn zone = pagezone; 340189e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 340289e004eaSLee Schermerhorn } 3403fa9add64SHugh Dickins lruvec = mem_cgroup_page_lruvec(page, zone); 340489e004eaSLee Schermerhorn 340524513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 340624513264SHugh Dickins continue; 340789e004eaSLee Schermerhorn 340824513264SHugh Dickins if (page_evictable(page, NULL)) { 340924513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 341024513264SHugh Dickins 341124513264SHugh Dickins VM_BUG_ON(PageActive(page)); 341224513264SHugh Dickins ClearPageUnevictable(page); 3413fa9add64SHugh Dickins del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE); 3414fa9add64SHugh Dickins add_page_to_lru_list(page, lruvec, lru); 341524513264SHugh Dickins pgrescued++; 341689e004eaSLee Schermerhorn } 341789e004eaSLee Schermerhorn } 341824513264SHugh Dickins 341924513264SHugh Dickins if (zone) { 342024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 342124513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 342224513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 342324513264SHugh Dickins } 342485046579SHugh Dickins } 342585046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3426af936a16SLee Schermerhorn 3427264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3428af936a16SLee Schermerhorn { 3429264e56d8SJohannes Weiner printk_once(KERN_WARNING 343025bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3431264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 343225bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 343325bd91bdSKOSAKI Motohiro current->comm); 3434af936a16SLee Schermerhorn } 3435af936a16SLee Schermerhorn 3436af936a16SLee Schermerhorn /* 3437af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3438af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3439af936a16SLee Schermerhorn */ 3440af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3441af936a16SLee Schermerhorn 3442af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 34438d65af78SAlexey Dobriyan void __user *buffer, 3444af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3445af936a16SLee Schermerhorn { 3446264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 34478d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3448af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3449af936a16SLee Schermerhorn return 0; 3450af936a16SLee Schermerhorn } 3451af936a16SLee Schermerhorn 3452e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3453af936a16SLee Schermerhorn /* 3454af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3455af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3456af936a16SLee Schermerhorn */ 3457af936a16SLee Schermerhorn 345810fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 345910fbcf4cSKay Sievers struct device_attribute *attr, 3460af936a16SLee Schermerhorn char *buf) 3461af936a16SLee Schermerhorn { 3462264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3463af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3464af936a16SLee Schermerhorn } 3465af936a16SLee Schermerhorn 346610fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 346710fbcf4cSKay Sievers struct device_attribute *attr, 3468af936a16SLee Schermerhorn const char *buf, size_t count) 3469af936a16SLee Schermerhorn { 3470264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3471af936a16SLee Schermerhorn return 1; 3472af936a16SLee Schermerhorn } 3473af936a16SLee Schermerhorn 3474af936a16SLee Schermerhorn 347510fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3476af936a16SLee Schermerhorn read_scan_unevictable_node, 3477af936a16SLee Schermerhorn write_scan_unevictable_node); 3478af936a16SLee Schermerhorn 3479af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3480af936a16SLee Schermerhorn { 348110fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3482af936a16SLee Schermerhorn } 3483af936a16SLee Schermerhorn 3484af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3485af936a16SLee Schermerhorn { 348610fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3487af936a16SLee Schermerhorn } 3488e4455abbSThadeu Lima de Souza Cascardo #endif 3489