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 13600f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 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)) { 72392df3a72SMel Gorman nr_writeback++; 7247d3579e8SKOSAKI Motohiro unlock_page(page); 72541ac1999SMel Gorman goto keep; 726c661b078SAndy Whitcroft } 7271da177e4SLinus Torvalds 7286a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 729dfc8d636SJohannes Weiner switch (references) { 730dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 7311da177e4SLinus Torvalds goto activate_locked; 73264574746SJohannes Weiner case PAGEREF_KEEP: 73364574746SJohannes Weiner goto keep_locked; 734dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 735dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 736dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 737dfc8d636SJohannes Weiner } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* 7401da177e4SLinus Torvalds * Anonymous process memory has backing store? 7411da177e4SLinus Torvalds * Try to allocate it some swap space here. 7421da177e4SLinus Torvalds */ 743b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 74463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 74563eb6b93SHugh Dickins goto keep_locked; 746ac47b003SHugh Dickins if (!add_to_swap(page)) 7471da177e4SLinus Torvalds goto activate_locked; 74863eb6b93SHugh Dickins may_enter_fs = 1; 749b291f000SNick Piggin } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds mapping = page_mapping(page); 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 7551da177e4SLinus Torvalds * processes. Try to unmap it here. 7561da177e4SLinus Torvalds */ 7571da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 75814fa31b8SAndi Kleen switch (try_to_unmap(page, TTU_UNMAP)) { 7591da177e4SLinus Torvalds case SWAP_FAIL: 7601da177e4SLinus Torvalds goto activate_locked; 7611da177e4SLinus Torvalds case SWAP_AGAIN: 7621da177e4SLinus Torvalds goto keep_locked; 763b291f000SNick Piggin case SWAP_MLOCK: 764b291f000SNick Piggin goto cull_mlocked; 7651da177e4SLinus Torvalds case SWAP_SUCCESS: 7661da177e4SLinus Torvalds ; /* try to free the page below */ 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds } 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds if (PageDirty(page)) { 7710e093d99SMel Gorman nr_dirty++; 7720e093d99SMel Gorman 773ee72886dSMel Gorman /* 774ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 775f84f6e2bSMel Gorman * avoid risk of stack overflow but do not writeback 776f84f6e2bSMel Gorman * unless under significant pressure. 777ee72886dSMel Gorman */ 778f84f6e2bSMel Gorman if (page_is_file_cache(page) && 7799e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 7809e3b2f8cSKonstantin Khlebnikov sc->priority >= DEF_PRIORITY - 2)) { 78149ea7eb6SMel Gorman /* 78249ea7eb6SMel Gorman * Immediately reclaim when written back. 78349ea7eb6SMel Gorman * Similar in principal to deactivate_page() 78449ea7eb6SMel Gorman * except we already have the page isolated 78549ea7eb6SMel Gorman * and know it's dirty 78649ea7eb6SMel Gorman */ 78749ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 78849ea7eb6SMel Gorman SetPageReclaim(page); 78949ea7eb6SMel Gorman 790ee72886dSMel Gorman goto keep_locked; 791ee72886dSMel Gorman } 792ee72886dSMel Gorman 793dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 7941da177e4SLinus Torvalds goto keep_locked; 7954dd4b920SAndrew Morton if (!may_enter_fs) 7961da177e4SLinus Torvalds goto keep_locked; 79752a8363eSChristoph Lameter if (!sc->may_writepage) 7981da177e4SLinus Torvalds goto keep_locked; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 8017d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 8021da177e4SLinus Torvalds case PAGE_KEEP: 8030e093d99SMel Gorman nr_congested++; 8041da177e4SLinus Torvalds goto keep_locked; 8051da177e4SLinus Torvalds case PAGE_ACTIVATE: 8061da177e4SLinus Torvalds goto activate_locked; 8071da177e4SLinus Torvalds case PAGE_SUCCESS: 8087d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 80941ac1999SMel Gorman goto keep; 8107d3579e8SKOSAKI Motohiro if (PageDirty(page)) 8111da177e4SLinus Torvalds goto keep; 8127d3579e8SKOSAKI Motohiro 8131da177e4SLinus Torvalds /* 8141da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 8151da177e4SLinus Torvalds * ahead and try to reclaim the page. 8161da177e4SLinus Torvalds */ 817529ae9aaSNick Piggin if (!trylock_page(page)) 8181da177e4SLinus Torvalds goto keep; 8191da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 8201da177e4SLinus Torvalds goto keep_locked; 8211da177e4SLinus Torvalds mapping = page_mapping(page); 8221da177e4SLinus Torvalds case PAGE_CLEAN: 8231da177e4SLinus Torvalds ; /* try to free the page below */ 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 8291da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 8301da177e4SLinus Torvalds * the page as well. 8311da177e4SLinus Torvalds * 8321da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 8331da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 8341da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 8351da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 8361da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 8371da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 8381da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 8391da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 8401da177e4SLinus Torvalds * 8411da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 8421da177e4SLinus Torvalds * the pages which were not successfully invalidated in 8431da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 8441da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 8451da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 8461da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 8471da177e4SLinus Torvalds */ 848266cf658SDavid Howells if (page_has_private(page)) { 8491da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 8501da177e4SLinus Torvalds goto activate_locked; 851e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 852e286781dSNick Piggin unlock_page(page); 853e286781dSNick Piggin if (put_page_testzero(page)) 8541da177e4SLinus Torvalds goto free_it; 855e286781dSNick Piggin else { 856e286781dSNick Piggin /* 857e286781dSNick Piggin * rare race with speculative reference. 858e286781dSNick Piggin * the speculative reference will free 859e286781dSNick Piggin * this page shortly, so we may 860e286781dSNick Piggin * increment nr_reclaimed here (and 861e286781dSNick Piggin * leave it off the LRU). 862e286781dSNick Piggin */ 863e286781dSNick Piggin nr_reclaimed++; 864e286781dSNick Piggin continue; 865e286781dSNick Piggin } 866e286781dSNick Piggin } 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 869e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 87049d2e9ccSChristoph Lameter goto keep_locked; 8711da177e4SLinus Torvalds 872a978d6f5SNick Piggin /* 873a978d6f5SNick Piggin * At this point, we have no other references and there is 874a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 875a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 876a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 877a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 878a978d6f5SNick Piggin */ 879a978d6f5SNick Piggin __clear_page_locked(page); 880e286781dSNick Piggin free_it: 88105ff5137SAndrew Morton nr_reclaimed++; 882abe4c3b5SMel Gorman 883abe4c3b5SMel Gorman /* 884abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 885abe4c3b5SMel Gorman * appear not as the counts should be low 886abe4c3b5SMel Gorman */ 887abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 8881da177e4SLinus Torvalds continue; 8891da177e4SLinus Torvalds 890b291f000SNick Piggin cull_mlocked: 89163d6c5adSHugh Dickins if (PageSwapCache(page)) 89263d6c5adSHugh Dickins try_to_free_swap(page); 893b291f000SNick Piggin unlock_page(page); 894b291f000SNick Piggin putback_lru_page(page); 895b291f000SNick Piggin continue; 896b291f000SNick Piggin 8971da177e4SLinus Torvalds activate_locked: 89868a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 89968a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 900a2c43eedSHugh Dickins try_to_free_swap(page); 901894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 9021da177e4SLinus Torvalds SetPageActive(page); 9031da177e4SLinus Torvalds pgactivate++; 9041da177e4SLinus Torvalds keep_locked: 9051da177e4SLinus Torvalds unlock_page(page); 9061da177e4SLinus Torvalds keep: 9071da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 908b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 9091da177e4SLinus Torvalds } 910abe4c3b5SMel Gorman 9110e093d99SMel Gorman /* 9120e093d99SMel Gorman * Tag a zone as congested if all the dirty pages encountered were 9130e093d99SMel Gorman * backed by a congested BDI. In this case, reclaimers should just 9140e093d99SMel Gorman * back off and wait for congestion to clear because further reclaim 9150e093d99SMel Gorman * will encounter the same problem 9160e093d99SMel Gorman */ 91789b5fae5SJohannes Weiner if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc)) 9186a18adb3SKonstantin Khlebnikov zone_set_flag(zone, ZONE_CONGESTED); 9190e093d99SMel Gorman 920cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 921abe4c3b5SMel Gorman 9221da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 923f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 92492df3a72SMel Gorman *ret_nr_dirty += nr_dirty; 92592df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 92605ff5137SAndrew Morton return nr_reclaimed; 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9295ad333ebSAndy Whitcroft /* 9305ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 9315ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 9325ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 9335ad333ebSAndy Whitcroft * 9345ad333ebSAndy Whitcroft * page: page to consider 9355ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 9365ad333ebSAndy Whitcroft * 9375ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 9385ad333ebSAndy Whitcroft */ 939f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 9405ad333ebSAndy Whitcroft { 9415ad333ebSAndy Whitcroft int ret = -EINVAL; 9425ad333ebSAndy Whitcroft 9435ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 9445ad333ebSAndy Whitcroft if (!PageLRU(page)) 9455ad333ebSAndy Whitcroft return ret; 9465ad333ebSAndy Whitcroft 947c53919adSMel Gorman /* Do not give back unevictable pages for compaction */ 948894bc310SLee Schermerhorn if (PageUnevictable(page)) 949894bc310SLee Schermerhorn return ret; 950894bc310SLee Schermerhorn 9515ad333ebSAndy Whitcroft ret = -EBUSY; 95208e552c6SKAMEZAWA Hiroyuki 953c8244935SMel Gorman /* 954c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 955c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 956c8244935SMel Gorman * blocking - clean pages for the most part. 957c8244935SMel Gorman * 958c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 959c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 960c8244935SMel Gorman * 961c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 962c8244935SMel Gorman * that it is possible to migrate without blocking 963c8244935SMel Gorman */ 964c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 965c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 966c8244935SMel Gorman if (PageWriteback(page)) 96739deaf85SMinchan Kim return ret; 96839deaf85SMinchan Kim 969c8244935SMel Gorman if (PageDirty(page)) { 970c8244935SMel Gorman struct address_space *mapping; 971c8244935SMel Gorman 972c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 973c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 974c8244935SMel Gorman return ret; 975c8244935SMel Gorman 976c8244935SMel Gorman /* 977c8244935SMel Gorman * Only pages without mappings or that have a 978c8244935SMel Gorman * ->migratepage callback are possible to migrate 979c8244935SMel Gorman * without blocking 980c8244935SMel Gorman */ 981c8244935SMel Gorman mapping = page_mapping(page); 982c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 983c8244935SMel Gorman return ret; 984c8244935SMel Gorman } 985c8244935SMel Gorman } 986c8244935SMel Gorman 987f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 988f80c0673SMinchan Kim return ret; 989f80c0673SMinchan Kim 9905ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 9915ad333ebSAndy Whitcroft /* 9925ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 9935ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 9945ad333ebSAndy Whitcroft * page release code relies on it. 9955ad333ebSAndy Whitcroft */ 9965ad333ebSAndy Whitcroft ClearPageLRU(page); 9975ad333ebSAndy Whitcroft ret = 0; 9985ad333ebSAndy Whitcroft } 9995ad333ebSAndy Whitcroft 10005ad333ebSAndy Whitcroft return ret; 10015ad333ebSAndy Whitcroft } 10025ad333ebSAndy Whitcroft 100349d2e9ccSChristoph Lameter /* 10041da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10051da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10061da177e4SLinus Torvalds * and working on them outside the LRU lock. 10071da177e4SLinus Torvalds * 10081da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10091da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10101da177e4SLinus Torvalds * 10111da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10121da177e4SLinus Torvalds * 10131da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 10145dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 10151da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1016f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1017fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 10185ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 10193cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 10201da177e4SLinus Torvalds * 10211da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10221da177e4SLinus Torvalds */ 102369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 10245dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1025fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 10263cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 10271da177e4SLinus Torvalds { 1028*75b00af7SHugh Dickins struct list_head *src = &lruvec->lists[lru]; 102969e05944SAndrew Morton unsigned long nr_taken = 0; 1030c9b02d97SWu Fengguang unsigned long scan; 10311da177e4SLinus Torvalds 1032c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 10335ad333ebSAndy Whitcroft struct page *page; 10345ad333ebSAndy Whitcroft 10351da177e4SLinus Torvalds page = lru_to_page(src); 10361da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10371da177e4SLinus Torvalds 1038725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 10398d438f96SNick Piggin 1040f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 10415ad333ebSAndy Whitcroft case 0: 1042bbf808edSKonstantin Khlebnikov mem_cgroup_lru_del_list(page, lru); 10435ad333ebSAndy Whitcroft list_move(&page->lru, dst); 10442c888cfbSRik van Riel nr_taken += hpage_nr_pages(page); 10455ad333ebSAndy Whitcroft break; 10467c8ee9a8SNick Piggin 10475ad333ebSAndy Whitcroft case -EBUSY: 10485ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 10495ad333ebSAndy Whitcroft list_move(&page->lru, src); 10505ad333ebSAndy Whitcroft continue; 10515ad333ebSAndy Whitcroft 10525ad333ebSAndy Whitcroft default: 10535ad333ebSAndy Whitcroft BUG(); 10545ad333ebSAndy Whitcroft } 10555ad333ebSAndy Whitcroft } 10561da177e4SLinus Torvalds 1057f626012dSHugh Dickins *nr_scanned = scan; 1058*75b00af7SHugh Dickins trace_mm_vmscan_lru_isolate(sc->order, nr_to_scan, scan, 1059*75b00af7SHugh Dickins nr_taken, mode, is_file_lru(lru)); 10601da177e4SLinus Torvalds return nr_taken; 10611da177e4SLinus Torvalds } 10621da177e4SLinus Torvalds 106362695a84SNick Piggin /** 106462695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 106562695a84SNick Piggin * @page: page to isolate from its LRU list 106662695a84SNick Piggin * 106762695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 106862695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 106962695a84SNick Piggin * 107062695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 107162695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 107262695a84SNick Piggin * 107362695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1074894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1075894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1076894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 107762695a84SNick Piggin * 107862695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 107962695a84SNick Piggin * found will be decremented. 108062695a84SNick Piggin * 108162695a84SNick Piggin * Restrictions: 108262695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 108362695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 108462695a84SNick Piggin * without a stable reference). 108562695a84SNick Piggin * (2) the lru_lock must not be held. 108662695a84SNick Piggin * (3) interrupts must be enabled. 108762695a84SNick Piggin */ 108862695a84SNick Piggin int isolate_lru_page(struct page *page) 108962695a84SNick Piggin { 109062695a84SNick Piggin int ret = -EBUSY; 109162695a84SNick Piggin 10920c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 10930c917313SKonstantin Khlebnikov 109462695a84SNick Piggin if (PageLRU(page)) { 109562695a84SNick Piggin struct zone *zone = page_zone(page); 109662695a84SNick Piggin 109762695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 10980c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1099894bc310SLee Schermerhorn int lru = page_lru(page); 110062695a84SNick Piggin ret = 0; 11010c917313SKonstantin Khlebnikov get_page(page); 110262695a84SNick Piggin ClearPageLRU(page); 11034f98a2feSRik van Riel 11044f98a2feSRik van Riel del_page_from_lru_list(zone, page, lru); 110562695a84SNick Piggin } 110662695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 110762695a84SNick Piggin } 110862695a84SNick Piggin return ret; 110962695a84SNick Piggin } 111062695a84SNick Piggin 11115ad333ebSAndy Whitcroft /* 111235cd7815SRik van Riel * Are there way too many processes in the direct reclaim path already? 111335cd7815SRik van Riel */ 111435cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 111535cd7815SRik van Riel struct scan_control *sc) 111635cd7815SRik van Riel { 111735cd7815SRik van Riel unsigned long inactive, isolated; 111835cd7815SRik van Riel 111935cd7815SRik van Riel if (current_is_kswapd()) 112035cd7815SRik van Riel return 0; 112135cd7815SRik van Riel 112289b5fae5SJohannes Weiner if (!global_reclaim(sc)) 112335cd7815SRik van Riel return 0; 112435cd7815SRik van Riel 112535cd7815SRik van Riel if (file) { 112635cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 112735cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 112835cd7815SRik van Riel } else { 112935cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 113035cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 113135cd7815SRik van Riel } 113235cd7815SRik van Riel 113335cd7815SRik van Riel return isolated > inactive; 113435cd7815SRik van Riel } 113535cd7815SRik van Riel 113666635629SMel Gorman static noinline_for_stack void 1137*75b00af7SHugh Dickins putback_inactive_pages(struct lruvec *lruvec, struct list_head *page_list) 113866635629SMel Gorman { 113927ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 114027ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 11413f79768fSHugh Dickins LIST_HEAD(pages_to_free); 114266635629SMel Gorman 114366635629SMel Gorman /* 114466635629SMel Gorman * Put back any unfreeable pages. 114566635629SMel Gorman */ 114666635629SMel Gorman while (!list_empty(page_list)) { 11473f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 114866635629SMel Gorman int lru; 11493f79768fSHugh Dickins 115066635629SMel Gorman VM_BUG_ON(PageLRU(page)); 115166635629SMel Gorman list_del(&page->lru); 115266635629SMel Gorman if (unlikely(!page_evictable(page, NULL))) { 115366635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 115466635629SMel Gorman putback_lru_page(page); 115566635629SMel Gorman spin_lock_irq(&zone->lru_lock); 115666635629SMel Gorman continue; 115766635629SMel Gorman } 11587a608572SLinus Torvalds SetPageLRU(page); 115966635629SMel Gorman lru = page_lru(page); 11607a608572SLinus Torvalds add_page_to_lru_list(zone, page, lru); 116166635629SMel Gorman if (is_active_lru(lru)) { 116266635629SMel Gorman int file = is_file_lru(lru); 11639992af10SRik van Riel int numpages = hpage_nr_pages(page); 11649992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 116566635629SMel Gorman } 11662bcf8879SHugh Dickins if (put_page_testzero(page)) { 11672bcf8879SHugh Dickins __ClearPageLRU(page); 11682bcf8879SHugh Dickins __ClearPageActive(page); 11692bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 11702bcf8879SHugh Dickins 11712bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 117266635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 11732bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 117466635629SMel Gorman spin_lock_irq(&zone->lru_lock); 11752bcf8879SHugh Dickins } else 11762bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 117766635629SMel Gorman } 117866635629SMel Gorman } 117966635629SMel Gorman 11803f79768fSHugh Dickins /* 11813f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 11823f79768fSHugh Dickins */ 11833f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 118466635629SMel Gorman } 118566635629SMel Gorman 118666635629SMel Gorman /* 11871742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 11881742f19fSAndrew Morton * of reclaimed pages 11891da177e4SLinus Torvalds */ 119066635629SMel Gorman static noinline_for_stack unsigned long 11911a93be0eSKonstantin Khlebnikov shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec, 11929e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 11931da177e4SLinus Torvalds { 11941da177e4SLinus Torvalds LIST_HEAD(page_list); 1195e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 119605ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1197e247dbceSKOSAKI Motohiro unsigned long nr_taken; 119892df3a72SMel Gorman unsigned long nr_dirty = 0; 119992df3a72SMel Gorman unsigned long nr_writeback = 0; 1200f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 12013cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 12021a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 12031a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 120478dc583dSKOSAKI Motohiro 120535cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 120658355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 120735cd7815SRik van Riel 120835cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 120935cd7815SRik van Riel if (fatal_signal_pending(current)) 121035cd7815SRik van Riel return SWAP_CLUSTER_MAX; 121135cd7815SRik van Riel } 121235cd7815SRik van Riel 12131da177e4SLinus Torvalds lru_add_drain(); 1214f80c0673SMinchan Kim 1215f80c0673SMinchan Kim if (!sc->may_unmap) 121661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1217f80c0673SMinchan Kim if (!sc->may_writepage) 121861317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1219f80c0673SMinchan Kim 12201da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12211da177e4SLinus Torvalds 12225dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 12235dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 122495d918fcSKonstantin Khlebnikov 122595d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 122695d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 122795d918fcSKonstantin Khlebnikov 122889b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1229e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1230b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1231*75b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scanned); 1232b35ea17bSKOSAKI Motohiro else 1233*75b00af7SHugh Dickins __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scanned); 1234b35ea17bSKOSAKI Motohiro } 123566635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1236d563c050SHillf Danton 1237d563c050SHillf Danton if (nr_taken == 0) 123866635629SMel Gorman return 0; 1239b35ea17bSKOSAKI Motohiro 12406a18adb3SKonstantin Khlebnikov nr_reclaimed = shrink_page_list(&page_list, zone, sc, 124192df3a72SMel Gorman &nr_dirty, &nr_writeback); 1242c661b078SAndy Whitcroft 12433f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 12443f79768fSHugh Dickins 124595d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1246d563c050SHillf Danton 1247904249aaSYing Han if (global_reclaim(sc)) { 1248b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1249904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1250904249aaSYing Han nr_reclaimed); 1251904249aaSYing Han else 1252904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1253904249aaSYing Han nr_reclaimed); 1254904249aaSYing Han } 1255a74609faSNick Piggin 125627ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 12573f79768fSHugh Dickins 125895d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 12593f79768fSHugh Dickins 12603f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 12613f79768fSHugh Dickins 12623f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1263e11da5b4SMel Gorman 126492df3a72SMel Gorman /* 126592df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 126692df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 126792df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 126892df3a72SMel Gorman * at throttling processes due to the page distribution throughout 126992df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 127092df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 127192df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 127292df3a72SMel Gorman * same way balance_dirty_pages() manages. 127392df3a72SMel Gorman * 127492df3a72SMel Gorman * This scales the number of dirty pages that must be under writeback 127592df3a72SMel Gorman * before throttling depending on priority. It is a simple backoff 127692df3a72SMel Gorman * function that has the most effect in the range DEF_PRIORITY to 127792df3a72SMel Gorman * DEF_PRIORITY-2 which is the priority reclaim is considered to be 127892df3a72SMel Gorman * in trouble and reclaim is considered to be in trouble. 127992df3a72SMel Gorman * 128092df3a72SMel Gorman * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle 128192df3a72SMel Gorman * DEF_PRIORITY-1 50% must be PageWriteback 128292df3a72SMel Gorman * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble 128392df3a72SMel Gorman * ... 128492df3a72SMel Gorman * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any 128592df3a72SMel Gorman * isolated page is PageWriteback 128692df3a72SMel Gorman */ 12879e3b2f8cSKonstantin Khlebnikov if (nr_writeback && nr_writeback >= 12889e3b2f8cSKonstantin Khlebnikov (nr_taken >> (DEF_PRIORITY - sc->priority))) 128992df3a72SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 129092df3a72SMel Gorman 1291e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1292e11da5b4SMel Gorman zone_idx(zone), 1293e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 12949e3b2f8cSKonstantin Khlebnikov sc->priority, 129523b9da55SMel Gorman trace_shrink_flags(file)); 129605ff5137SAndrew Morton return nr_reclaimed; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12993bb1a852SMartin Bligh /* 13001cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 13011cfb419bSKAMEZAWA Hiroyuki * 13021cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 13031cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 13041cfb419bSKAMEZAWA Hiroyuki * 13051cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 13061cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 13071cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 13081cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 13091cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 13101cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 13111cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 13121cfb419bSKAMEZAWA Hiroyuki * 13131cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 13141cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 13151cfb419bSKAMEZAWA Hiroyuki */ 13161cfb419bSKAMEZAWA Hiroyuki 13173eb4140fSWu Fengguang static void move_active_pages_to_lru(struct zone *zone, 13183eb4140fSWu Fengguang struct list_head *list, 13192bcf8879SHugh Dickins struct list_head *pages_to_free, 13203eb4140fSWu Fengguang enum lru_list lru) 13213eb4140fSWu Fengguang { 13223eb4140fSWu Fengguang unsigned long pgmoved = 0; 13233eb4140fSWu Fengguang struct page *page; 13243eb4140fSWu Fengguang 13253eb4140fSWu Fengguang while (!list_empty(list)) { 1326925b7673SJohannes Weiner struct lruvec *lruvec; 1327925b7673SJohannes Weiner 13283eb4140fSWu Fengguang page = lru_to_page(list); 13293eb4140fSWu Fengguang 13303eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 13313eb4140fSWu Fengguang SetPageLRU(page); 13323eb4140fSWu Fengguang 1333925b7673SJohannes Weiner lruvec = mem_cgroup_lru_add_list(zone, page, lru); 1334925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 13352c888cfbSRik van Riel pgmoved += hpage_nr_pages(page); 13363eb4140fSWu Fengguang 13372bcf8879SHugh Dickins if (put_page_testzero(page)) { 13382bcf8879SHugh Dickins __ClearPageLRU(page); 13392bcf8879SHugh Dickins __ClearPageActive(page); 13402bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 13412bcf8879SHugh Dickins 13422bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 13433eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 13442bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 13453eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 13462bcf8879SHugh Dickins } else 13472bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 13483eb4140fSWu Fengguang } 13493eb4140fSWu Fengguang } 13503eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 13513eb4140fSWu Fengguang if (!is_active_lru(lru)) 13523eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 13533eb4140fSWu Fengguang } 13541cfb419bSKAMEZAWA Hiroyuki 1355f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 13561a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, 1357f16015fbSJohannes Weiner struct scan_control *sc, 13589e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 13591cfb419bSKAMEZAWA Hiroyuki { 136044c241f1SKOSAKI Motohiro unsigned long nr_taken; 1361f626012dSHugh Dickins unsigned long nr_scanned; 13626fe6b7e3SWu Fengguang unsigned long vm_flags; 13631cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 13648cab4754SWu Fengguang LIST_HEAD(l_active); 1365b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 13661cfb419bSKAMEZAWA Hiroyuki struct page *page; 13671a93be0eSKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 136844c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1369f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 13703cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 13711a93be0eSKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 13721cfb419bSKAMEZAWA Hiroyuki 13731da177e4SLinus Torvalds lru_add_drain(); 1374f80c0673SMinchan Kim 1375f80c0673SMinchan Kim if (!sc->may_unmap) 137661317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1377f80c0673SMinchan Kim if (!sc->may_writepage) 137861317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1379f80c0673SMinchan Kim 13801da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1381925b7673SJohannes Weiner 13825dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 13835dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 138489b5fae5SJohannes Weiner if (global_reclaim(sc)) 1385f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 138689b5fae5SJohannes Weiner 1387b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 13881cfb419bSKAMEZAWA Hiroyuki 1389f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 13903cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1391a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 13921da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 13951da177e4SLinus Torvalds cond_resched(); 13961da177e4SLinus Torvalds page = lru_to_page(&l_hold); 13971da177e4SLinus Torvalds list_del(&page->lru); 13987e9cd484SRik van Riel 1399894bc310SLee Schermerhorn if (unlikely(!page_evictable(page, NULL))) { 1400894bc310SLee Schermerhorn putback_lru_page(page); 1401894bc310SLee Schermerhorn continue; 1402894bc310SLee Schermerhorn } 1403894bc310SLee Schermerhorn 1404cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1405cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1406cc715d99SMel Gorman if (page_has_private(page)) 1407cc715d99SMel Gorman try_to_release_page(page, 0); 1408cc715d99SMel Gorman unlock_page(page); 1409cc715d99SMel Gorman } 1410cc715d99SMel Gorman } 1411cc715d99SMel Gorman 1412c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1413c3ac9a8aSJohannes Weiner &vm_flags)) { 14149992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 14158cab4754SWu Fengguang /* 14168cab4754SWu Fengguang * Identify referenced, file-backed active pages and 14178cab4754SWu Fengguang * give them one more trip around the active list. So 14188cab4754SWu Fengguang * that executable code get better chances to stay in 14198cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 14208cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 14218cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 14228cab4754SWu Fengguang * so we ignore them here. 14238cab4754SWu Fengguang */ 142441e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 14258cab4754SWu Fengguang list_add(&page->lru, &l_active); 14268cab4754SWu Fengguang continue; 14278cab4754SWu Fengguang } 14288cab4754SWu Fengguang } 14297e9cd484SRik van Riel 14305205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 14311da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 1434b555749aSAndrew Morton /* 14358cab4754SWu Fengguang * Move pages back to the lru list. 1436b555749aSAndrew Morton */ 14372a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 14384f98a2feSRik van Riel /* 14398cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 14408cab4754SWu Fengguang * even though only some of them are actually re-activated. This 14418cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 14428cab4754SWu Fengguang * get_scan_ratio. 1443556adecbSRik van Riel */ 1444b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1445556adecbSRik van Riel 14463cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_active, &l_hold, lru); 14473cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1448a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1449f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 14502bcf8879SHugh Dickins 14512bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 145474e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 145514797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1456f89eb90eSKOSAKI Motohiro { 1457f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1458f89eb90eSKOSAKI Motohiro 1459f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1460f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1461f89eb90eSKOSAKI Motohiro 1462f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1463f89eb90eSKOSAKI Motohiro return 1; 1464f89eb90eSKOSAKI Motohiro 1465f89eb90eSKOSAKI Motohiro return 0; 1466f89eb90eSKOSAKI Motohiro } 1467f89eb90eSKOSAKI Motohiro 146814797e23SKOSAKI Motohiro /** 146914797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1470c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 147114797e23SKOSAKI Motohiro * 147214797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 147314797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 147414797e23SKOSAKI Motohiro */ 1475c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 147614797e23SKOSAKI Motohiro { 147774e3f3c3SMinchan Kim /* 147874e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 147974e3f3c3SMinchan Kim * is pointless. 148074e3f3c3SMinchan Kim */ 148174e3f3c3SMinchan Kim if (!total_swap_pages) 148274e3f3c3SMinchan Kim return 0; 148374e3f3c3SMinchan Kim 1484c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1485c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1486f16015fbSJohannes Weiner 1487c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 148814797e23SKOSAKI Motohiro } 148974e3f3c3SMinchan Kim #else 1490c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 149174e3f3c3SMinchan Kim { 149274e3f3c3SMinchan Kim return 0; 149374e3f3c3SMinchan Kim } 149474e3f3c3SMinchan Kim #endif 149514797e23SKOSAKI Motohiro 149656e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone) 149756e49d21SRik van Riel { 149856e49d21SRik van Riel unsigned long active, inactive; 149956e49d21SRik van Riel 150056e49d21SRik van Riel active = zone_page_state(zone, NR_ACTIVE_FILE); 150156e49d21SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 150256e49d21SRik van Riel 150356e49d21SRik van Riel return (active > inactive); 150456e49d21SRik van Riel } 150556e49d21SRik van Riel 150656e49d21SRik van Riel /** 150756e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1508c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 150956e49d21SRik van Riel * 151056e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 151156e49d21SRik van Riel * ensures that active file pages get deactivated, until more 151256e49d21SRik van Riel * than half of the file pages are on the inactive list. 151356e49d21SRik van Riel * 151456e49d21SRik van Riel * Once we get to that situation, protect the system's working 151556e49d21SRik van Riel * set from being evicted by disabling active file page aging. 151656e49d21SRik van Riel * 151756e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 151856e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 151956e49d21SRik van Riel */ 1520c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 152156e49d21SRik van Riel { 1522c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1523c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_file_is_low(lruvec); 152456e49d21SRik van Riel 1525c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low_global(lruvec_zone(lruvec)); 152656e49d21SRik van Riel } 152756e49d21SRik van Riel 1528*75b00af7SHugh Dickins static int inactive_list_is_low(struct lruvec *lruvec, enum lru_list lru) 1529b39415b2SRik van Riel { 1530*75b00af7SHugh Dickins if (is_file_lru(lru)) 1531c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1532b39415b2SRik van Riel else 1533c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1534b39415b2SRik van Riel } 1535b39415b2SRik van Riel 15364f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 15371a93be0eSKonstantin Khlebnikov struct lruvec *lruvec, struct scan_control *sc) 1538b69408e8SChristoph Lameter { 1539b39415b2SRik van Riel if (is_active_lru(lru)) { 1540*75b00af7SHugh Dickins if (inactive_list_is_low(lruvec, lru)) 15411a93be0eSKonstantin Khlebnikov shrink_active_list(nr_to_scan, lruvec, sc, lru); 1542556adecbSRik van Riel return 0; 1543556adecbSRik van Riel } 1544556adecbSRik van Riel 15451a93be0eSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, lruvec, sc, lru); 1546b69408e8SChristoph Lameter } 1547b69408e8SChristoph Lameter 15483d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 15491f4c025bSKAMEZAWA Hiroyuki { 155089b5fae5SJohannes Weiner if (global_reclaim(sc)) 15511f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 15523d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 15531f4c025bSKAMEZAWA Hiroyuki } 15541f4c025bSKAMEZAWA Hiroyuki 15551da177e4SLinus Torvalds /* 15564f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 15574f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 15584f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 15594f98a2feSRik van Riel * onto the active list instead of evict. 15604f98a2feSRik van Riel * 156176a33fc3SShaohua Li * nr[0] = anon pages to scan; nr[1] = file pages to scan 15624f98a2feSRik van Riel */ 156390126375SKonstantin Khlebnikov static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, 15649e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 15654f98a2feSRik van Riel { 15664f98a2feSRik van Riel unsigned long anon, file, free; 15674f98a2feSRik van Riel unsigned long anon_prio, file_prio; 15684f98a2feSRik van Riel unsigned long ap, fp; 156990126375SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 157076a33fc3SShaohua Li u64 fraction[2], denominator; 15714111304dSHugh Dickins enum lru_list lru; 157276a33fc3SShaohua Li int noswap = 0; 1573a4d3e9e7SJohannes Weiner bool force_scan = false; 157490126375SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 1575246e87a9SKAMEZAWA Hiroyuki 1576f11c0ca5SJohannes Weiner /* 1577f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1578f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1579f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1580f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1581f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1582f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1583f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1584f11c0ca5SJohannes Weiner * well. 1585f11c0ca5SJohannes Weiner */ 158690126375SKonstantin Khlebnikov if (current_is_kswapd() && zone->all_unreclaimable) 1587a4d3e9e7SJohannes Weiner force_scan = true; 158889b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1589a4d3e9e7SJohannes Weiner force_scan = true; 159076a33fc3SShaohua Li 159176a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 159276a33fc3SShaohua Li if (!sc->may_swap || (nr_swap_pages <= 0)) { 159376a33fc3SShaohua Li noswap = 1; 159476a33fc3SShaohua Li fraction[0] = 0; 159576a33fc3SShaohua Li fraction[1] = 1; 159676a33fc3SShaohua Li denominator = 1; 159776a33fc3SShaohua Li goto out; 159876a33fc3SShaohua Li } 15994f98a2feSRik van Riel 16004d7dcca2SHugh Dickins anon = get_lru_size(lruvec, LRU_ACTIVE_ANON) + 16014d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_ANON); 16024d7dcca2SHugh Dickins file = get_lru_size(lruvec, LRU_ACTIVE_FILE) + 16034d7dcca2SHugh Dickins get_lru_size(lruvec, LRU_INACTIVE_FILE); 1604a4d3e9e7SJohannes Weiner 160589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 160690126375SKonstantin Khlebnikov free = zone_page_state(zone, NR_FREE_PAGES); 1607eeee9a8cSKOSAKI Motohiro /* If we have very few page cache pages, 1608eeee9a8cSKOSAKI Motohiro force-scan anon pages. */ 160990126375SKonstantin Khlebnikov if (unlikely(file + free <= high_wmark_pages(zone))) { 161076a33fc3SShaohua Li fraction[0] = 1; 161176a33fc3SShaohua Li fraction[1] = 0; 161276a33fc3SShaohua Li denominator = 1; 161376a33fc3SShaohua Li goto out; 16144f98a2feSRik van Riel } 1615eeee9a8cSKOSAKI Motohiro } 16164f98a2feSRik van Riel 16174f98a2feSRik van Riel /* 161858c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 161958c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 162058c37f6eSKOSAKI Motohiro */ 16213d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 1622*75b00af7SHugh Dickins file_prio = 200 - anon_prio; 162358c37f6eSKOSAKI Motohiro 162458c37f6eSKOSAKI Motohiro /* 16254f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 16264f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 16274f98a2feSRik van Riel * ratios to determine how valuable each cache is. 16284f98a2feSRik van Riel * 16294f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 16304f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 16314f98a2feSRik van Riel * up weighing recent references more than old ones. 16324f98a2feSRik van Riel * 16334f98a2feSRik van Riel * anon in [0], file in [1] 16344f98a2feSRik van Riel */ 163590126375SKonstantin Khlebnikov spin_lock_irq(&zone->lru_lock); 163658c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 16376e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 16386e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 16394f98a2feSRik van Riel } 16404f98a2feSRik van Riel 16416e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 16426e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 16436e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 16444f98a2feSRik van Riel } 16454f98a2feSRik van Riel 16464f98a2feSRik van Riel /* 164700d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 164800d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 164900d8089cSRik van Riel * each list that were recently referenced and in active use. 16504f98a2feSRik van Riel */ 1651fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 16526e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 16534f98a2feSRik van Riel 1654fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 16556e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 165690126375SKonstantin Khlebnikov spin_unlock_irq(&zone->lru_lock); 16574f98a2feSRik van Riel 165876a33fc3SShaohua Li fraction[0] = ap; 165976a33fc3SShaohua Li fraction[1] = fp; 166076a33fc3SShaohua Li denominator = ap + fp + 1; 166176a33fc3SShaohua Li out: 16624111304dSHugh Dickins for_each_evictable_lru(lru) { 16634111304dSHugh Dickins int file = is_file_lru(lru); 166476a33fc3SShaohua Li unsigned long scan; 166576a33fc3SShaohua Li 16664d7dcca2SHugh Dickins scan = get_lru_size(lruvec, lru); 16679e3b2f8cSKonstantin Khlebnikov if (sc->priority || noswap || !vmscan_swappiness(sc)) { 16689e3b2f8cSKonstantin Khlebnikov scan >>= sc->priority; 1669f11c0ca5SJohannes Weiner if (!scan && force_scan) 1670f11c0ca5SJohannes Weiner scan = SWAP_CLUSTER_MAX; 167176a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 16724f98a2feSRik van Riel } 16734111304dSHugh Dickins nr[lru] = scan; 167476a33fc3SShaohua Li } 16756e08a369SWu Fengguang } 16764f98a2feSRik van Riel 167723b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 16789e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 167923b9da55SMel Gorman { 168023b9da55SMel Gorman if (COMPACTION_BUILD && sc->order && 168123b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 16829e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 168323b9da55SMel Gorman return true; 168423b9da55SMel Gorman 168523b9da55SMel Gorman return false; 168623b9da55SMel Gorman } 168723b9da55SMel Gorman 16884f98a2feSRik van Riel /* 168923b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 169023b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 169123b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 169223b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 169323b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 16943e7d3449SMel Gorman */ 169590bdcfafSKonstantin Khlebnikov static inline bool should_continue_reclaim(struct lruvec *lruvec, 16963e7d3449SMel Gorman unsigned long nr_reclaimed, 16973e7d3449SMel Gorman unsigned long nr_scanned, 16983e7d3449SMel Gorman struct scan_control *sc) 16993e7d3449SMel Gorman { 17003e7d3449SMel Gorman unsigned long pages_for_compaction; 17013e7d3449SMel Gorman unsigned long inactive_lru_pages; 17023e7d3449SMel Gorman 17033e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 17049e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 17053e7d3449SMel Gorman return false; 17063e7d3449SMel Gorman 17072876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 17082876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 17093e7d3449SMel Gorman /* 17102876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 17112876592fSMel Gorman * full LRU list has been scanned and we are still failing 17122876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 17132876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 17143e7d3449SMel Gorman */ 17153e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 17163e7d3449SMel Gorman return false; 17172876592fSMel Gorman } else { 17182876592fSMel Gorman /* 17192876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 17202876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 17212876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 17222876592fSMel Gorman * pages that were scanned. This will return to the 17232876592fSMel Gorman * caller faster at the risk reclaim/compaction and 17242876592fSMel Gorman * the resulting allocation attempt fails 17252876592fSMel Gorman */ 17262876592fSMel Gorman if (!nr_reclaimed) 17272876592fSMel Gorman return false; 17282876592fSMel Gorman } 17293e7d3449SMel Gorman 17303e7d3449SMel Gorman /* 17313e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 17323e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 17333e7d3449SMel Gorman */ 17343e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 17354d7dcca2SHugh Dickins inactive_lru_pages = get_lru_size(lruvec, LRU_INACTIVE_FILE); 173686cfd3a4SMinchan Kim if (nr_swap_pages > 0) 17374d7dcca2SHugh Dickins inactive_lru_pages += get_lru_size(lruvec, LRU_INACTIVE_ANON); 17383e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 17393e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 17403e7d3449SMel Gorman return true; 17413e7d3449SMel Gorman 17423e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 174390bdcfafSKonstantin Khlebnikov switch (compaction_suitable(lruvec_zone(lruvec), sc->order)) { 17443e7d3449SMel Gorman case COMPACT_PARTIAL: 17453e7d3449SMel Gorman case COMPACT_CONTINUE: 17463e7d3449SMel Gorman return false; 17473e7d3449SMel Gorman default: 17483e7d3449SMel Gorman return true; 17493e7d3449SMel Gorman } 17503e7d3449SMel Gorman } 17513e7d3449SMel Gorman 17523e7d3449SMel Gorman /* 17531da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 17541da177e4SLinus Torvalds */ 1755f9be23d6SKonstantin Khlebnikov static void shrink_lruvec(struct lruvec *lruvec, struct scan_control *sc) 17561da177e4SLinus Torvalds { 1757b69408e8SChristoph Lameter unsigned long nr[NR_LRU_LISTS]; 17588695949aSChristoph Lameter unsigned long nr_to_scan; 17594111304dSHugh Dickins enum lru_list lru; 1760f0fdc5e8SJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 176122fba335SKOSAKI Motohiro unsigned long nr_to_reclaim = sc->nr_to_reclaim; 17623da367c3SShaohua Li struct blk_plug plug; 17631da177e4SLinus Torvalds 17643e7d3449SMel Gorman restart: 17653e7d3449SMel Gorman nr_reclaimed = 0; 1766f0fdc5e8SJohannes Weiner nr_scanned = sc->nr_scanned; 176790126375SKonstantin Khlebnikov get_scan_count(lruvec, sc, nr); 17681cfb419bSKAMEZAWA Hiroyuki 17693da367c3SShaohua Li blk_start_plug(&plug); 1770556adecbSRik van Riel while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 1771556adecbSRik van Riel nr[LRU_INACTIVE_FILE]) { 17724111304dSHugh Dickins for_each_evictable_lru(lru) { 17734111304dSHugh Dickins if (nr[lru]) { 1774ece74b2eSKOSAKI Motohiro nr_to_scan = min_t(unsigned long, 17754111304dSHugh Dickins nr[lru], SWAP_CLUSTER_MAX); 17764111304dSHugh Dickins nr[lru] -= nr_to_scan; 1777b69408e8SChristoph Lameter 17784111304dSHugh Dickins nr_reclaimed += shrink_list(lru, nr_to_scan, 17791a93be0eSKonstantin Khlebnikov lruvec, sc); 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds } 1782a79311c1SRik van Riel /* 1783a79311c1SRik van Riel * On large memory systems, scan >> priority can become 1784a79311c1SRik van Riel * really large. This is fine for the starting priority; 1785a79311c1SRik van Riel * we want to put equal scanning pressure on each zone. 1786a79311c1SRik van Riel * However, if the VM has a harder time of freeing pages, 1787a79311c1SRik van Riel * with multiple processes reclaiming pages, the total 1788a79311c1SRik van Riel * freeing target can get unreasonably large. 1789a79311c1SRik van Riel */ 17909e3b2f8cSKonstantin Khlebnikov if (nr_reclaimed >= nr_to_reclaim && 17919e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY) 1792a79311c1SRik van Riel break; 17931da177e4SLinus Torvalds } 17943da367c3SShaohua Li blk_finish_plug(&plug); 17953e7d3449SMel Gorman sc->nr_reclaimed += nr_reclaimed; 179601dbe5c9SKOSAKI Motohiro 1797556adecbSRik van Riel /* 1798556adecbSRik van Riel * Even if we did not try to evict anon pages at all, we want to 1799556adecbSRik van Riel * rebalance the anon lru active/inactive ratio. 1800556adecbSRik van Riel */ 1801c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 18021a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 18039e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 1804556adecbSRik van Riel 18053e7d3449SMel Gorman /* reclaim/compaction might need reclaim to continue */ 180690bdcfafSKonstantin Khlebnikov if (should_continue_reclaim(lruvec, nr_reclaimed, 18079e3b2f8cSKonstantin Khlebnikov sc->nr_scanned - nr_scanned, sc)) 18083e7d3449SMel Gorman goto restart; 18093e7d3449SMel Gorman 1810232ea4d6SAndrew Morton throttle_vm_writeout(sc->gfp_mask); 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 18139e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc) 1814f16015fbSJohannes Weiner { 18155660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 18165660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 18175660048cSJohannes Weiner .zone = zone, 18189e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 18195660048cSJohannes Weiner }; 18205660048cSJohannes Weiner struct mem_cgroup *memcg; 18215660048cSJohannes Weiner 18225660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 18235660048cSJohannes Weiner do { 1824f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 18255660048cSJohannes Weiner 1826f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, sc); 1827f9be23d6SKonstantin Khlebnikov 18285660048cSJohannes Weiner /* 18295660048cSJohannes Weiner * Limit reclaim has historically picked one memcg and 18305660048cSJohannes Weiner * scanned it with decreasing priority levels until 18315660048cSJohannes Weiner * nr_to_reclaim had been reclaimed. This priority 18325660048cSJohannes Weiner * cycle is thus over after a single memcg. 1833b95a2f2dSJohannes Weiner * 1834b95a2f2dSJohannes Weiner * Direct reclaim and kswapd, on the other hand, have 1835b95a2f2dSJohannes Weiner * to scan all memory cgroups to fulfill the overall 1836b95a2f2dSJohannes Weiner * scan target for the zone. 18375660048cSJohannes Weiner */ 18385660048cSJohannes Weiner if (!global_reclaim(sc)) { 18395660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 18405660048cSJohannes Weiner break; 18415660048cSJohannes Weiner } 18425660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 18435660048cSJohannes Weiner } while (memcg); 1844f16015fbSJohannes Weiner } 1845f16015fbSJohannes Weiner 1846fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 1847fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 1848fe4b1b24SMel Gorman { 1849fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 1850fe4b1b24SMel Gorman bool watermark_ok; 1851fe4b1b24SMel Gorman 1852fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 1853fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 1854fe4b1b24SMel Gorman return false; 1855fe4b1b24SMel Gorman 1856fe4b1b24SMel Gorman /* 1857fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 1858fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 1859fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 1860fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 1861fe4b1b24SMel Gorman */ 1862fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 1863fe4b1b24SMel Gorman (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 1864fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 1865fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 1866fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 1867fe4b1b24SMel Gorman 1868fe4b1b24SMel Gorman /* 1869fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 1870fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 1871fe4b1b24SMel Gorman */ 1872aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 1873fe4b1b24SMel Gorman return watermark_ok; 1874fe4b1b24SMel Gorman 1875fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 1876fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 1877fe4b1b24SMel Gorman return false; 1878fe4b1b24SMel Gorman 1879fe4b1b24SMel Gorman return watermark_ok; 1880fe4b1b24SMel Gorman } 1881fe4b1b24SMel Gorman 18821da177e4SLinus Torvalds /* 18831da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 18841da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 18851da177e4SLinus Torvalds * request. 18861da177e4SLinus Torvalds * 188741858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 188841858966SMel Gorman * Because: 18891da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 18901da177e4SLinus Torvalds * allocation or 189141858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 189241858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 189341858966SMel Gorman * zone defense algorithm. 18941da177e4SLinus Torvalds * 18951da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 18961da177e4SLinus Torvalds * scan then give up on it. 1897e0c23279SMel Gorman * 1898e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 1899fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 19000cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 19010cee34fdSMel Gorman * further reclaim. 19021da177e4SLinus Torvalds */ 19039e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 19041da177e4SLinus Torvalds { 1905dd1a239fSMel Gorman struct zoneref *z; 190654a6eb5cSMel Gorman struct zone *zone; 1907d149e3b2SYing Han unsigned long nr_soft_reclaimed; 1908d149e3b2SYing Han unsigned long nr_soft_scanned; 19090cee34fdSMel Gorman bool aborted_reclaim = false; 19101cfb419bSKAMEZAWA Hiroyuki 1911cc715d99SMel Gorman /* 1912cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 1913cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 1914cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 1915cc715d99SMel Gorman */ 1916cc715d99SMel Gorman if (buffer_heads_over_limit) 1917cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 1918cc715d99SMel Gorman 1919d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 1920d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 1921f3fe6512SCon Kolivas if (!populated_zone(zone)) 19221da177e4SLinus Torvalds continue; 19231cfb419bSKAMEZAWA Hiroyuki /* 19241cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 19251cfb419bSKAMEZAWA Hiroyuki * to global LRU. 19261cfb419bSKAMEZAWA Hiroyuki */ 192789b5fae5SJohannes Weiner if (global_reclaim(sc)) { 192802a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 19291da177e4SLinus Torvalds continue; 19309e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 19319e3b2f8cSKonstantin Khlebnikov sc->priority != DEF_PRIORITY) 19321da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 1933e0887c19SRik van Riel if (COMPACTION_BUILD) { 1934e0887c19SRik van Riel /* 1935e0c23279SMel Gorman * If we already have plenty of memory free for 1936e0c23279SMel Gorman * compaction in this zone, don't free any more. 1937e0c23279SMel Gorman * Even though compaction is invoked for any 1938e0c23279SMel Gorman * non-zero order, only frequent costly order 1939e0c23279SMel Gorman * reclamation is disruptive enough to become a 1940c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 1941c7cfa37bSCopot Alexandru * page allocations. 1942e0887c19SRik van Riel */ 1943fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 19440cee34fdSMel Gorman aborted_reclaim = true; 1945e0887c19SRik van Riel continue; 1946e0887c19SRik van Riel } 1947e0c23279SMel Gorman } 1948ac34a1a3SKAMEZAWA Hiroyuki /* 1949ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 1950ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 1951ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 1952ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 1953ac34a1a3SKAMEZAWA Hiroyuki */ 1954d149e3b2SYing Han nr_soft_scanned = 0; 1955d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 1956d149e3b2SYing Han sc->order, sc->gfp_mask, 1957d149e3b2SYing Han &nr_soft_scanned); 1958d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 1959ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 1960ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 1961ac34a1a3SKAMEZAWA Hiroyuki } 1962d149e3b2SYing Han 19639e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 19641da177e4SLinus Torvalds } 1965e0c23279SMel Gorman 19660cee34fdSMel Gorman return aborted_reclaim; 1967d1908362SMinchan Kim } 1968d1908362SMinchan Kim 1969d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone) 1970d1908362SMinchan Kim { 1971d1908362SMinchan Kim return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 1972d1908362SMinchan Kim } 1973d1908362SMinchan Kim 1974929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 1975d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 1976d1908362SMinchan Kim struct scan_control *sc) 1977d1908362SMinchan Kim { 1978d1908362SMinchan Kim struct zoneref *z; 1979d1908362SMinchan Kim struct zone *zone; 1980d1908362SMinchan Kim 1981d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 1982d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 1983d1908362SMinchan Kim if (!populated_zone(zone)) 1984d1908362SMinchan Kim continue; 1985d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 1986d1908362SMinchan Kim continue; 1987929bea7cSKOSAKI Motohiro if (!zone->all_unreclaimable) 1988929bea7cSKOSAKI Motohiro return false; 1989d1908362SMinchan Kim } 1990d1908362SMinchan Kim 1991929bea7cSKOSAKI Motohiro return true; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds 19941da177e4SLinus Torvalds /* 19951da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 19961da177e4SLinus Torvalds * 19971da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 19981da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 19991da177e4SLinus Torvalds * 20001da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 20011da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 20025b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 20035b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 20045b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 20055b0830cbSJens Axboe * work, and the allocation attempt will fail. 2006a41f24eaSNishanth Aravamudan * 2007a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2008a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 20091da177e4SLinus Torvalds */ 2010dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2011a09ed5e0SYing Han struct scan_control *sc, 2012a09ed5e0SYing Han struct shrink_control *shrink) 20131da177e4SLinus Torvalds { 201469e05944SAndrew Morton unsigned long total_scanned = 0; 20151da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2016dd1a239fSMel Gorman struct zoneref *z; 201754a6eb5cSMel Gorman struct zone *zone; 201822fba335SKOSAKI Motohiro unsigned long writeback_threshold; 20190cee34fdSMel Gorman bool aborted_reclaim; 20201da177e4SLinus Torvalds 2021873b4771SKeika Kobayashi delayacct_freepages_start(); 2022873b4771SKeika Kobayashi 202389b5fae5SJohannes Weiner if (global_reclaim(sc)) 2024f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 20251da177e4SLinus Torvalds 20269e3b2f8cSKonstantin Khlebnikov do { 202766e1707bSBalbir Singh sc->nr_scanned = 0; 20289e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2029e0c23279SMel Gorman 203066e1707bSBalbir Singh /* 203166e1707bSBalbir Singh * Don't shrink slabs when reclaiming memory from 203266e1707bSBalbir Singh * over limit cgroups 203366e1707bSBalbir Singh */ 203489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2035c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2036d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2037d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2038c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2039c6a8a8c5SKOSAKI Motohiro continue; 2040c6a8a8c5SKOSAKI Motohiro 2041c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2042c6a8a8c5SKOSAKI Motohiro } 2043c6a8a8c5SKOSAKI Motohiro 20441495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 20451da177e4SLinus Torvalds if (reclaim_state) { 2046a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 20471da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 20481da177e4SLinus Torvalds } 204991a45470SKAMEZAWA Hiroyuki } 205066e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2051bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 20521da177e4SLinus Torvalds goto out; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds /* 20551da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 20561da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 20571da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 20581da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 20591da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 20601da177e4SLinus Torvalds */ 206122fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 206222fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 20630e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 20640e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 206566e1707bSBalbir Singh sc->may_writepage = 1; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 20697b51755cSKOSAKI Motohiro if (!sc->hibernation_mode && sc->nr_scanned && 20709e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2) { 20710e093d99SMel Gorman struct zone *preferred_zone; 20720e093d99SMel Gorman 20730e093d99SMel Gorman first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), 2074f33261d7SDavid Rientjes &cpuset_current_mems_allowed, 2075f33261d7SDavid Rientjes &preferred_zone); 20760e093d99SMel Gorman wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); 20770e093d99SMel Gorman } 20789e3b2f8cSKonstantin Khlebnikov } while (--sc->priority >= 0); 2079bb21c7ceSKOSAKI Motohiro 20801da177e4SLinus Torvalds out: 2081873b4771SKeika Kobayashi delayacct_freepages_end(); 2082873b4771SKeika Kobayashi 2083bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2084bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2085bb21c7ceSKOSAKI Motohiro 2086929bea7cSKOSAKI Motohiro /* 2087929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2088929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2089929bea7cSKOSAKI Motohiro * check. 2090929bea7cSKOSAKI Motohiro */ 2091929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2092929bea7cSKOSAKI Motohiro return 0; 2093929bea7cSKOSAKI Motohiro 20940cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 20950cee34fdSMel Gorman if (aborted_reclaim) 20967335084dSMel Gorman return 1; 20977335084dSMel Gorman 2098bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 209989b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2100bb21c7ceSKOSAKI Motohiro return 1; 2101bb21c7ceSKOSAKI Motohiro 2102bb21c7ceSKOSAKI Motohiro return 0; 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 2105dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2106327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 210766e1707bSBalbir Singh { 210833906bc5SMel Gorman unsigned long nr_reclaimed; 210966e1707bSBalbir Singh struct scan_control sc = { 211066e1707bSBalbir Singh .gfp_mask = gfp_mask, 211166e1707bSBalbir Singh .may_writepage = !laptop_mode, 211222fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2113a6dc60f8SJohannes Weiner .may_unmap = 1, 21142e2e4259SKOSAKI Motohiro .may_swap = 1, 211566e1707bSBalbir Singh .order = order, 21169e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2117f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2118327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 211966e1707bSBalbir Singh }; 2120a09ed5e0SYing Han struct shrink_control shrink = { 2121a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2122a09ed5e0SYing Han }; 212366e1707bSBalbir Singh 212433906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 212533906bc5SMel Gorman sc.may_writepage, 212633906bc5SMel Gorman gfp_mask); 212733906bc5SMel Gorman 2128a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 212933906bc5SMel Gorman 213033906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 213133906bc5SMel Gorman 213233906bc5SMel Gorman return nr_reclaimed; 213366e1707bSBalbir Singh } 213466e1707bSBalbir Singh 213500f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 213666e1707bSBalbir Singh 213772835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 21384e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 21390ae5e89cSYing Han struct zone *zone, 21400ae5e89cSYing Han unsigned long *nr_scanned) 21414e416953SBalbir Singh { 21424e416953SBalbir Singh struct scan_control sc = { 21430ae5e89cSYing Han .nr_scanned = 0, 2144b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 21454e416953SBalbir Singh .may_writepage = !laptop_mode, 21464e416953SBalbir Singh .may_unmap = 1, 21474e416953SBalbir Singh .may_swap = !noswap, 21484e416953SBalbir Singh .order = 0, 21499e3b2f8cSKonstantin Khlebnikov .priority = 0, 215072835c86SJohannes Weiner .target_mem_cgroup = memcg, 21514e416953SBalbir Singh }; 2152f9be23d6SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 21530ae5e89cSYing Han 21544e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 21554e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2156bdce6d9eSKOSAKI Motohiro 21579e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2158bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2159bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2160bdce6d9eSKOSAKI Motohiro 21614e416953SBalbir Singh /* 21624e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 21634e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 21644e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 21654e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 21664e416953SBalbir Singh * the priority and make it zero. 21674e416953SBalbir Singh */ 2168f9be23d6SKonstantin Khlebnikov shrink_lruvec(lruvec, &sc); 2169bdce6d9eSKOSAKI Motohiro 2170bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2171bdce6d9eSKOSAKI Motohiro 21720ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 21734e416953SBalbir Singh return sc.nr_reclaimed; 21744e416953SBalbir Singh } 21754e416953SBalbir Singh 217672835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 21778c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2178185efc0fSJohannes Weiner bool noswap) 217966e1707bSBalbir Singh { 21804e416953SBalbir Singh struct zonelist *zonelist; 2181bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2182889976dbSYing Han int nid; 218366e1707bSBalbir Singh struct scan_control sc = { 218466e1707bSBalbir Singh .may_writepage = !laptop_mode, 2185a6dc60f8SJohannes Weiner .may_unmap = 1, 21862e2e4259SKOSAKI Motohiro .may_swap = !noswap, 218722fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 218866e1707bSBalbir Singh .order = 0, 21899e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 219072835c86SJohannes Weiner .target_mem_cgroup = memcg, 2191327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2192a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2193a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2194a09ed5e0SYing Han }; 2195a09ed5e0SYing Han struct shrink_control shrink = { 2196a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 219766e1707bSBalbir Singh }; 219866e1707bSBalbir Singh 2199889976dbSYing Han /* 2200889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2201889976dbSYing Han * take care of from where we get pages. So the node where we start the 2202889976dbSYing Han * scan does not need to be the current node. 2203889976dbSYing Han */ 220472835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2205889976dbSYing Han 2206889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2207bdce6d9eSKOSAKI Motohiro 2208bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2209bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2210bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2211bdce6d9eSKOSAKI Motohiro 2212a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2213bdce6d9eSKOSAKI Motohiro 2214bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2215bdce6d9eSKOSAKI Motohiro 2216bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 221766e1707bSBalbir Singh } 221866e1707bSBalbir Singh #endif 221966e1707bSBalbir Singh 22209e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2221f16015fbSJohannes Weiner { 2222b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2223b95a2f2dSJohannes Weiner 2224b95a2f2dSJohannes Weiner if (!total_swap_pages) 2225b95a2f2dSJohannes Weiner return; 2226b95a2f2dSJohannes Weiner 2227b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2228b95a2f2dSJohannes Weiner do { 2229c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2230f16015fbSJohannes Weiner 2231c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 22321a93be0eSKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, lruvec, 22339e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2234b95a2f2dSJohannes Weiner 2235b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2236b95a2f2dSJohannes Weiner } while (memcg); 2237f16015fbSJohannes Weiner } 2238f16015fbSJohannes Weiner 22391741c877SMel Gorman /* 22401741c877SMel Gorman * pgdat_balanced is used when checking if a node is balanced for high-order 22411741c877SMel Gorman * allocations. Only zones that meet watermarks and are in a zone allowed 22421741c877SMel Gorman * by the callers classzone_idx are added to balanced_pages. The total of 22431741c877SMel Gorman * balanced pages must be at least 25% of the zones allowed by classzone_idx 22441741c877SMel Gorman * for the node to be considered balanced. Forcing all zones to be balanced 22451741c877SMel Gorman * for high orders can cause excessive reclaim when there are imbalanced zones. 22461741c877SMel Gorman * The choice of 25% is due to 22471741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 22481741c877SMel Gorman * reasonable sized machine 22491741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 225025985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 22511741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 22521741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 22531741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 22541741c877SMel Gorman */ 22551741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages, 22561741c877SMel Gorman int classzone_idx) 22571741c877SMel Gorman { 22581741c877SMel Gorman unsigned long present_pages = 0; 22591741c877SMel Gorman int i; 22601741c877SMel Gorman 22611741c877SMel Gorman for (i = 0; i <= classzone_idx; i++) 22621741c877SMel Gorman present_pages += pgdat->node_zones[i].present_pages; 22631741c877SMel Gorman 22644746efdeSShaohua Li /* A special case here: if zone has no page, we think it's balanced */ 22654746efdeSShaohua Li return balanced_pages >= (present_pages >> 2); 22661741c877SMel Gorman } 22671741c877SMel Gorman 2268f50de2d3SMel Gorman /* is kswapd sleeping prematurely? */ 2269dc83edd9SMel Gorman static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining, 2270dc83edd9SMel Gorman int classzone_idx) 2271f50de2d3SMel Gorman { 2272bb3ab596SKOSAKI Motohiro int i; 22731741c877SMel Gorman unsigned long balanced = 0; 22741741c877SMel Gorman bool all_zones_ok = true; 2275f50de2d3SMel Gorman 2276f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2277f50de2d3SMel Gorman if (remaining) 2278dc83edd9SMel Gorman return true; 2279f50de2d3SMel Gorman 22800abdee2bSMel Gorman /* Check the watermark levels */ 228108951e54SMel Gorman for (i = 0; i <= classzone_idx; i++) { 2282bb3ab596SKOSAKI Motohiro struct zone *zone = pgdat->node_zones + i; 2283bb3ab596SKOSAKI Motohiro 2284bb3ab596SKOSAKI Motohiro if (!populated_zone(zone)) 2285bb3ab596SKOSAKI Motohiro continue; 2286bb3ab596SKOSAKI Motohiro 2287355b09c4SMel Gorman /* 2288355b09c4SMel Gorman * balance_pgdat() skips over all_unreclaimable after 2289355b09c4SMel Gorman * DEF_PRIORITY. Effectively, it considers them balanced so 2290355b09c4SMel Gorman * they must be considered balanced here as well if kswapd 2291355b09c4SMel Gorman * is to sleep 2292355b09c4SMel Gorman */ 2293355b09c4SMel Gorman if (zone->all_unreclaimable) { 2294355b09c4SMel Gorman balanced += zone->present_pages; 2295de3fab39SKOSAKI Motohiro continue; 2296355b09c4SMel Gorman } 2297de3fab39SKOSAKI Motohiro 229888f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone), 2299da175d06SMel Gorman i, 0)) 23001741c877SMel Gorman all_zones_ok = false; 23011741c877SMel Gorman else 23021741c877SMel Gorman balanced += zone->present_pages; 2303bb3ab596SKOSAKI Motohiro } 2304f50de2d3SMel Gorman 23051741c877SMel Gorman /* 23061741c877SMel Gorman * For high-order requests, the balanced zones must contain at least 23071741c877SMel Gorman * 25% of the nodes pages for kswapd to sleep. For order-0, all zones 23081741c877SMel Gorman * must be balanced 23091741c877SMel Gorman */ 23101741c877SMel Gorman if (order) 2311afc7e326SJohannes Weiner return !pgdat_balanced(pgdat, balanced, classzone_idx); 23121741c877SMel Gorman else 23131741c877SMel Gorman return !all_zones_ok; 2314f50de2d3SMel Gorman } 2315f50de2d3SMel Gorman 23161da177e4SLinus Torvalds /* 23171da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 231841858966SMel Gorman * they are all at high_wmark_pages(zone). 23191da177e4SLinus Torvalds * 23200abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 23211da177e4SLinus Torvalds * 23221da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 23231da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 23241da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 23251da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 23261da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 23271da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 23281da177e4SLinus Torvalds * the zone for when the problem goes away. 23291da177e4SLinus Torvalds * 23301da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 233141858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 233241858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 233341858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 233441858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 233541858966SMel Gorman * of pages is balanced across the zones. 23361da177e4SLinus Torvalds */ 233799504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2338dc83edd9SMel Gorman int *classzone_idx) 23391da177e4SLinus Torvalds { 23401da177e4SLinus Torvalds int all_zones_ok; 23411741c877SMel Gorman unsigned long balanced; 23421da177e4SLinus Torvalds int i; 234399504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 234469e05944SAndrew Morton unsigned long total_scanned; 23451da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 23460ae5e89cSYing Han unsigned long nr_soft_reclaimed; 23470ae5e89cSYing Han unsigned long nr_soft_scanned; 2348179e9639SAndrew Morton struct scan_control sc = { 2349179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2350a6dc60f8SJohannes Weiner .may_unmap = 1, 23512e2e4259SKOSAKI Motohiro .may_swap = 1, 235222fba335SKOSAKI Motohiro /* 235322fba335SKOSAKI Motohiro * kswapd doesn't want to be bailed out while reclaim. because 235422fba335SKOSAKI Motohiro * we want to put equal scanning pressure on each zone. 235522fba335SKOSAKI Motohiro */ 235622fba335SKOSAKI Motohiro .nr_to_reclaim = ULONG_MAX, 23575ad333ebSAndy Whitcroft .order = order, 2358f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2359179e9639SAndrew Morton }; 2360a09ed5e0SYing Han struct shrink_control shrink = { 2361a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2362a09ed5e0SYing Han }; 23631da177e4SLinus Torvalds loop_again: 23641da177e4SLinus Torvalds total_scanned = 0; 23659e3b2f8cSKonstantin Khlebnikov sc.priority = DEF_PRIORITY; 2366a79311c1SRik van Riel sc.nr_reclaimed = 0; 2367c0bbbc73SChristoph Lameter sc.may_writepage = !laptop_mode; 2368f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 23691da177e4SLinus Torvalds 23709e3b2f8cSKonstantin Khlebnikov do { 23711da177e4SLinus Torvalds unsigned long lru_pages = 0; 2372bb3ab596SKOSAKI Motohiro int has_under_min_watermark_zone = 0; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds all_zones_ok = 1; 23751741c877SMel Gorman balanced = 0; 23761da177e4SLinus Torvalds 23771da177e4SLinus Torvalds /* 23781da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 23791da177e4SLinus Torvalds * zone which needs scanning 23801da177e4SLinus Torvalds */ 23811da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 23821da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 23831da177e4SLinus Torvalds 2384f3fe6512SCon Kolivas if (!populated_zone(zone)) 23851da177e4SLinus Torvalds continue; 23861da177e4SLinus Torvalds 23879e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 23889e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 23891da177e4SLinus Torvalds continue; 23901da177e4SLinus Torvalds 2391556adecbSRik van Riel /* 2392556adecbSRik van Riel * Do some background aging of the anon list, to give 2393556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2394556adecbSRik van Riel */ 23959e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2396556adecbSRik van Riel 2397cc715d99SMel Gorman /* 2398cc715d99SMel Gorman * If the number of buffer_heads in the machine 2399cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2400cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2401cc715d99SMel Gorman * it to relieve lowmem pressure. 2402cc715d99SMel Gorman */ 2403cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2404cc715d99SMel Gorman end_zone = i; 2405cc715d99SMel Gorman break; 2406cc715d99SMel Gorman } 2407cc715d99SMel Gorman 240888f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 240941858966SMel Gorman high_wmark_pages(zone), 0, 0)) { 24101da177e4SLinus Torvalds end_zone = i; 2411e1dbeda6SAndrew Morton break; 2412439423f6SShaohua Li } else { 2413439423f6SShaohua Li /* If balanced, clear the congested flag */ 2414439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds } 2417e1dbeda6SAndrew Morton if (i < 0) 24181da177e4SLinus Torvalds goto out; 2419e1dbeda6SAndrew Morton 24201da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 24211da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 24221da177e4SLinus Torvalds 2423adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 24261da177e4SLinus Torvalds /* 24271da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 24281da177e4SLinus Torvalds * at the last zone which needs scanning. 24291da177e4SLinus Torvalds * 24301da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 24311da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 24321da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 24331da177e4SLinus Torvalds * cause too much scanning of the lower zones. 24341da177e4SLinus Torvalds */ 24351da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 24361da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 2437fe2c2a10SRik van Riel int nr_slab, testorder; 24388afdceceSMel Gorman unsigned long balance_gap; 24391da177e4SLinus Torvalds 2440f3fe6512SCon Kolivas if (!populated_zone(zone)) 24411da177e4SLinus Torvalds continue; 24421da177e4SLinus Torvalds 24439e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 24449e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 24451da177e4SLinus Torvalds continue; 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds sc.nr_scanned = 0; 24484e416953SBalbir Singh 24490ae5e89cSYing Han nr_soft_scanned = 0; 24504e416953SBalbir Singh /* 24514e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 24524e416953SBalbir Singh */ 24530ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 24540ae5e89cSYing Han order, sc.gfp_mask, 24550ae5e89cSYing Han &nr_soft_scanned); 24560ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 24570ae5e89cSYing Han total_scanned += nr_soft_scanned; 245800918b6aSKOSAKI Motohiro 245932a4330dSRik van Riel /* 24608afdceceSMel Gorman * We put equal pressure on every zone, unless 24618afdceceSMel Gorman * one zone has way too many pages free 24628afdceceSMel Gorman * already. The "too many pages" is defined 24638afdceceSMel Gorman * as the high wmark plus a "gap" where the 24648afdceceSMel Gorman * gap is either the low watermark or 1% 24658afdceceSMel Gorman * of the zone, whichever is smaller. 246632a4330dSRik van Riel */ 24678afdceceSMel Gorman balance_gap = min(low_wmark_pages(zone), 24688afdceceSMel Gorman (zone->present_pages + 24698afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 24708afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2471fe2c2a10SRik van Riel /* 2472fe2c2a10SRik van Riel * Kswapd reclaims only single pages with compaction 2473fe2c2a10SRik van Riel * enabled. Trying too hard to reclaim until contiguous 2474fe2c2a10SRik van Riel * free pages have become available can hurt performance 2475fe2c2a10SRik van Riel * by evicting too much useful data from memory. 2476fe2c2a10SRik van Riel * Do not reclaim more than needed for compaction. 2477fe2c2a10SRik van Riel */ 2478fe2c2a10SRik van Riel testorder = order; 2479fe2c2a10SRik van Riel if (COMPACTION_BUILD && order && 2480fe2c2a10SRik van Riel compaction_suitable(zone, order) != 2481fe2c2a10SRik van Riel COMPACT_SKIPPED) 2482fe2c2a10SRik van Riel testorder = 0; 2483fe2c2a10SRik van Riel 2484cc715d99SMel Gorman if ((buffer_heads_over_limit && is_highmem_idx(i)) || 2485643ac9fcSHugh Dickins !zone_watermark_ok_safe(zone, testorder, 24868afdceceSMel Gorman high_wmark_pages(zone) + balance_gap, 2487d7868daeSMel Gorman end_zone, 0)) { 24889e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 2489d7868daeSMel Gorman 24901da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 24911495f230SYing Han nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages); 2492a79311c1SRik van Riel sc.nr_reclaimed += reclaim_state->reclaimed_slab; 24931da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 24945a03b051SAndrea Arcangeli 2495d7868daeSMel Gorman if (nr_slab == 0 && !zone_reclaimable(zone)) 249693e4a89aSKOSAKI Motohiro zone->all_unreclaimable = 1; 2497d7868daeSMel Gorman } 2498d7868daeSMel Gorman 24991da177e4SLinus Torvalds /* 25001da177e4SLinus Torvalds * If we've done a decent amount of scanning and 25011da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 25021da177e4SLinus Torvalds * even in laptop mode 25031da177e4SLinus Torvalds */ 25041da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 2505a79311c1SRik van Riel total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) 25061da177e4SLinus Torvalds sc.may_writepage = 1; 2507bb3ab596SKOSAKI Motohiro 2508215ddd66SMel Gorman if (zone->all_unreclaimable) { 2509215ddd66SMel Gorman if (end_zone && end_zone == i) 2510215ddd66SMel Gorman end_zone--; 2511d7868daeSMel Gorman continue; 2512215ddd66SMel Gorman } 2513d7868daeSMel Gorman 2514fe2c2a10SRik van Riel if (!zone_watermark_ok_safe(zone, testorder, 251545973d74SMinchan Kim high_wmark_pages(zone), end_zone, 0)) { 251645973d74SMinchan Kim all_zones_ok = 0; 2517bb3ab596SKOSAKI Motohiro /* 251845973d74SMinchan Kim * We are still under min water mark. This 251945973d74SMinchan Kim * means that we have a GFP_ATOMIC allocation 252045973d74SMinchan Kim * failure risk. Hurry up! 2521bb3ab596SKOSAKI Motohiro */ 252288f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 252345973d74SMinchan Kim min_wmark_pages(zone), end_zone, 0)) 2524bb3ab596SKOSAKI Motohiro has_under_min_watermark_zone = 1; 25250e093d99SMel Gorman } else { 25260e093d99SMel Gorman /* 25270e093d99SMel Gorman * If a zone reaches its high watermark, 25280e093d99SMel Gorman * consider it to be no longer congested. It's 25290e093d99SMel Gorman * possible there are dirty pages backed by 25300e093d99SMel Gorman * congested BDIs but as pressure is relieved, 25310e093d99SMel Gorman * spectulatively avoid congestion waits 25320e093d99SMel Gorman */ 25330e093d99SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 2534dc83edd9SMel Gorman if (i <= *classzone_idx) 25351741c877SMel Gorman balanced += zone->present_pages; 253645973d74SMinchan Kim } 2537bb3ab596SKOSAKI Motohiro 25381da177e4SLinus Torvalds } 2539dc83edd9SMel Gorman if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx))) 25401da177e4SLinus Torvalds break; /* kswapd: all done */ 25411da177e4SLinus Torvalds /* 25421da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 25431da177e4SLinus Torvalds * another pass across the zones. 25441da177e4SLinus Torvalds */ 25459e3b2f8cSKonstantin Khlebnikov if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) { 2546bb3ab596SKOSAKI Motohiro if (has_under_min_watermark_zone) 2547bb3ab596SKOSAKI Motohiro count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); 2548bb3ab596SKOSAKI Motohiro else 25498aa7e847SJens Axboe congestion_wait(BLK_RW_ASYNC, HZ/10); 2550bb3ab596SKOSAKI Motohiro } 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds /* 25531da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 25541da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 25551da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 25561da177e4SLinus Torvalds * on zone->*_priority. 25571da177e4SLinus Torvalds */ 2558a79311c1SRik van Riel if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) 25591da177e4SLinus Torvalds break; 25609e3b2f8cSKonstantin Khlebnikov } while (--sc.priority >= 0); 25611da177e4SLinus Torvalds out: 256299504748SMel Gorman 256399504748SMel Gorman /* 256499504748SMel Gorman * order-0: All zones must meet high watermark for a balanced node 25651741c877SMel Gorman * high-order: Balanced zones must make up at least 25% of the node 25661741c877SMel Gorman * for the node to be balanced 256799504748SMel Gorman */ 2568dc83edd9SMel Gorman if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) { 25691da177e4SLinus Torvalds cond_resched(); 25708357376dSRafael J. Wysocki 25718357376dSRafael J. Wysocki try_to_freeze(); 25728357376dSRafael J. Wysocki 257373ce02e9SKOSAKI Motohiro /* 257473ce02e9SKOSAKI Motohiro * Fragmentation may mean that the system cannot be 257573ce02e9SKOSAKI Motohiro * rebalanced for high-order allocations in all zones. 257673ce02e9SKOSAKI Motohiro * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, 257773ce02e9SKOSAKI Motohiro * it means the zones have been fully scanned and are still 257873ce02e9SKOSAKI Motohiro * not balanced. For high-order allocations, there is 257973ce02e9SKOSAKI Motohiro * little point trying all over again as kswapd may 258073ce02e9SKOSAKI Motohiro * infinite loop. 258173ce02e9SKOSAKI Motohiro * 258273ce02e9SKOSAKI Motohiro * Instead, recheck all watermarks at order-0 as they 258373ce02e9SKOSAKI Motohiro * are the most important. If watermarks are ok, kswapd will go 258473ce02e9SKOSAKI Motohiro * back to sleep. High-order users can still perform direct 258573ce02e9SKOSAKI Motohiro * reclaim if they wish. 258673ce02e9SKOSAKI Motohiro */ 258773ce02e9SKOSAKI Motohiro if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) 258873ce02e9SKOSAKI Motohiro order = sc.order = 0; 258973ce02e9SKOSAKI Motohiro 25901da177e4SLinus Torvalds goto loop_again; 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds 259399504748SMel Gorman /* 259499504748SMel Gorman * If kswapd was reclaiming at a higher order, it has the option of 259599504748SMel Gorman * sleeping without all zones being balanced. Before it does, it must 259699504748SMel Gorman * ensure that the watermarks for order-0 on *all* zones are met and 259799504748SMel Gorman * that the congestion flags are cleared. The congestion flag must 259899504748SMel Gorman * be cleared as kswapd is the only mechanism that clears the flag 259999504748SMel Gorman * and it is potentially going to sleep here. 260099504748SMel Gorman */ 260199504748SMel Gorman if (order) { 26027be62de9SRik van Riel int zones_need_compaction = 1; 26037be62de9SRik van Riel 260499504748SMel Gorman for (i = 0; i <= end_zone; i++) { 260599504748SMel Gorman struct zone *zone = pgdat->node_zones + i; 260699504748SMel Gorman 260799504748SMel Gorman if (!populated_zone(zone)) 260899504748SMel Gorman continue; 260999504748SMel Gorman 26109e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 26119e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 261299504748SMel Gorman continue; 261399504748SMel Gorman 2614fe2c2a10SRik van Riel /* Would compaction fail due to lack of free memory? */ 2615496b919bSRik van Riel if (COMPACTION_BUILD && 2616496b919bSRik van Riel compaction_suitable(zone, order) == COMPACT_SKIPPED) 2617fe2c2a10SRik van Riel goto loop_again; 2618fe2c2a10SRik van Riel 261999504748SMel Gorman /* Confirm the zone is balanced for order-0 */ 262099504748SMel Gorman if (!zone_watermark_ok(zone, 0, 262199504748SMel Gorman high_wmark_pages(zone), 0, 0)) { 262299504748SMel Gorman order = sc.order = 0; 262399504748SMel Gorman goto loop_again; 262499504748SMel Gorman } 262599504748SMel Gorman 26267be62de9SRik van Riel /* Check if the memory needs to be defragmented. */ 26277be62de9SRik van Riel if (zone_watermark_ok(zone, order, 26287be62de9SRik van Riel low_wmark_pages(zone), *classzone_idx, 0)) 26297be62de9SRik van Riel zones_need_compaction = 0; 26307be62de9SRik van Riel 263199504748SMel Gorman /* If balanced, clear the congested flag */ 263299504748SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 263399504748SMel Gorman } 26347be62de9SRik van Riel 26357be62de9SRik van Riel if (zones_need_compaction) 26367be62de9SRik van Riel compact_pgdat(pgdat, order); 263799504748SMel Gorman } 263899504748SMel Gorman 26390abdee2bSMel Gorman /* 26400abdee2bSMel Gorman * Return the order we were reclaiming at so sleeping_prematurely() 26410abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 26420abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 26430abdee2bSMel Gorman * was awake, order will remain at the higher level 26440abdee2bSMel Gorman */ 2645dc83edd9SMel Gorman *classzone_idx = end_zone; 26460abdee2bSMel Gorman return order; 26471da177e4SLinus Torvalds } 26481da177e4SLinus Torvalds 2649dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 2650f0bc0a60SKOSAKI Motohiro { 2651f0bc0a60SKOSAKI Motohiro long remaining = 0; 2652f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 2653f0bc0a60SKOSAKI Motohiro 2654f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 2655f0bc0a60SKOSAKI Motohiro return; 2656f0bc0a60SKOSAKI Motohiro 2657f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2658f0bc0a60SKOSAKI Motohiro 2659f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 2660dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2661f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 2662f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2663f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2664f0bc0a60SKOSAKI Motohiro } 2665f0bc0a60SKOSAKI Motohiro 2666f0bc0a60SKOSAKI Motohiro /* 2667f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 2668f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 2669f0bc0a60SKOSAKI Motohiro */ 2670dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2671f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 2672f0bc0a60SKOSAKI Motohiro 2673f0bc0a60SKOSAKI Motohiro /* 2674f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 2675f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 2676f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 2677f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 2678f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 2679f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 2680f0bc0a60SKOSAKI Motohiro */ 2681f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 2682f0bc0a60SKOSAKI Motohiro schedule(); 2683f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 2684f0bc0a60SKOSAKI Motohiro } else { 2685f0bc0a60SKOSAKI Motohiro if (remaining) 2686f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 2687f0bc0a60SKOSAKI Motohiro else 2688f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 2689f0bc0a60SKOSAKI Motohiro } 2690f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2691f0bc0a60SKOSAKI Motohiro } 2692f0bc0a60SKOSAKI Motohiro 26931da177e4SLinus Torvalds /* 26941da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 26951da177e4SLinus Torvalds * from the init process. 26961da177e4SLinus Torvalds * 26971da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 26981da177e4SLinus Torvalds * free memory available even if there is no other activity 26991da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 27001da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 27011da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 27021da177e4SLinus Torvalds * 27031da177e4SLinus Torvalds * If there are applications that are active memory-allocators 27041da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 27051da177e4SLinus Torvalds */ 27061da177e4SLinus Torvalds static int kswapd(void *p) 27071da177e4SLinus Torvalds { 2708215ddd66SMel Gorman unsigned long order, new_order; 2709d2ebd0f6SAlex,Shi unsigned balanced_order; 2710215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 2711d2ebd0f6SAlex,Shi int balanced_classzone_idx; 27121da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 27131da177e4SLinus Torvalds struct task_struct *tsk = current; 2714f0bc0a60SKOSAKI Motohiro 27151da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 27161da177e4SLinus Torvalds .reclaimed_slab = 0, 27171da177e4SLinus Torvalds }; 2718a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 27191da177e4SLinus Torvalds 2720cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 2721cf40bd16SNick Piggin 2722174596a0SRusty Russell if (!cpumask_empty(cpumask)) 2723c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 27241da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds /* 27271da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 27281da177e4SLinus Torvalds * and that if we need more memory we should get access to it 27291da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 27301da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 27311da177e4SLinus Torvalds * 27321da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 27331da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 27341da177e4SLinus Torvalds * page out something else, and this flag essentially protects 27351da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 27361da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 27371da177e4SLinus Torvalds */ 2738930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 273983144186SRafael J. Wysocki set_freezable(); 27401da177e4SLinus Torvalds 2741215ddd66SMel Gorman order = new_order = 0; 2742d2ebd0f6SAlex,Shi balanced_order = 0; 2743215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 2744d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 27451da177e4SLinus Torvalds for ( ; ; ) { 27468fe23e05SDavid Rientjes int ret; 27473e1d1d28SChristoph Lameter 2748215ddd66SMel Gorman /* 2749215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 2750215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 2751215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 2752215ddd66SMel Gorman */ 2753d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 2754d2ebd0f6SAlex,Shi balanced_order == new_order) { 27551da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 275699504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 27571da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 2758215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 2759215ddd66SMel Gorman } 2760215ddd66SMel Gorman 276199504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 27621da177e4SLinus Torvalds /* 27631da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 276499504748SMel Gorman * allocation or has tigher zone constraints 27651da177e4SLinus Torvalds */ 27661da177e4SLinus Torvalds order = new_order; 276799504748SMel Gorman classzone_idx = new_classzone_idx; 27681da177e4SLinus Torvalds } else { 2769d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 2770d2ebd0f6SAlex,Shi balanced_classzone_idx); 27711da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 277299504748SMel Gorman classzone_idx = pgdat->classzone_idx; 2773f0dfcde0SAlex,Shi new_order = order; 2774f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 27754d40502eSMel Gorman pgdat->kswapd_max_order = 0; 2776215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 27771da177e4SLinus Torvalds } 27781da177e4SLinus Torvalds 27798fe23e05SDavid Rientjes ret = try_to_freeze(); 27808fe23e05SDavid Rientjes if (kthread_should_stop()) 27818fe23e05SDavid Rientjes break; 27828fe23e05SDavid Rientjes 27838fe23e05SDavid Rientjes /* 27848fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 27858fe23e05SDavid Rientjes * after returning from the refrigerator 2786b1296cc4SRafael J. Wysocki */ 278733906bc5SMel Gorman if (!ret) { 278833906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 2789d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 2790d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 2791d2ebd0f6SAlex,Shi &balanced_classzone_idx); 27921da177e4SLinus Torvalds } 279333906bc5SMel Gorman } 27941da177e4SLinus Torvalds return 0; 27951da177e4SLinus Torvalds } 27961da177e4SLinus Torvalds 27971da177e4SLinus Torvalds /* 27981da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 27991da177e4SLinus Torvalds */ 280099504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 28011da177e4SLinus Torvalds { 28021da177e4SLinus Torvalds pg_data_t *pgdat; 28031da177e4SLinus Torvalds 2804f3fe6512SCon Kolivas if (!populated_zone(zone)) 28051da177e4SLinus Torvalds return; 28061da177e4SLinus Torvalds 280702a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 28081da177e4SLinus Torvalds return; 280988f5acf8SMel Gorman pgdat = zone->zone_pgdat; 281099504748SMel Gorman if (pgdat->kswapd_max_order < order) { 281188f5acf8SMel Gorman pgdat->kswapd_max_order = order; 281299504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 281399504748SMel Gorman } 28148d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 28151da177e4SLinus Torvalds return; 281688f5acf8SMel Gorman if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) 281788f5acf8SMel Gorman return; 281888f5acf8SMel Gorman 281988f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 28208d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 2823adea02a1SWu Fengguang /* 2824adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 2825adea02a1SWu Fengguang * The less reclaimable pages may be 2826adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 2827adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 2828adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 2829adea02a1SWu Fengguang */ 2830adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 28314f98a2feSRik van Riel { 2832adea02a1SWu Fengguang int nr; 2833adea02a1SWu Fengguang 2834adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 2835adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 2836adea02a1SWu Fengguang 2837adea02a1SWu Fengguang if (nr_swap_pages > 0) 2838adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 2839adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 2840adea02a1SWu Fengguang 2841adea02a1SWu Fengguang return nr; 2842adea02a1SWu Fengguang } 2843adea02a1SWu Fengguang 2844adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone) 2845adea02a1SWu Fengguang { 2846adea02a1SWu Fengguang int nr; 2847adea02a1SWu Fengguang 2848adea02a1SWu Fengguang nr = zone_page_state(zone, NR_ACTIVE_FILE) + 2849adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_FILE); 2850adea02a1SWu Fengguang 2851adea02a1SWu Fengguang if (nr_swap_pages > 0) 2852adea02a1SWu Fengguang nr += zone_page_state(zone, NR_ACTIVE_ANON) + 2853adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_ANON); 2854adea02a1SWu Fengguang 2855adea02a1SWu Fengguang return nr; 28564f98a2feSRik van Riel } 28574f98a2feSRik van Riel 2858c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 28591da177e4SLinus Torvalds /* 28607b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 2861d6277db4SRafael J. Wysocki * freed pages. 2862d6277db4SRafael J. Wysocki * 2863d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 2864d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 2865d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 28661da177e4SLinus Torvalds */ 28677b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 28681da177e4SLinus Torvalds { 2869d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 2870d6277db4SRafael J. Wysocki struct scan_control sc = { 28717b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 28727b51755cSKOSAKI Motohiro .may_swap = 1, 28737b51755cSKOSAKI Motohiro .may_unmap = 1, 2874d6277db4SRafael J. Wysocki .may_writepage = 1, 28757b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 28767b51755cSKOSAKI Motohiro .hibernation_mode = 1, 28777b51755cSKOSAKI Motohiro .order = 0, 28789e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 28791da177e4SLinus Torvalds }; 2880a09ed5e0SYing Han struct shrink_control shrink = { 2881a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2882a09ed5e0SYing Han }; 28837b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 28847b51755cSKOSAKI Motohiro struct task_struct *p = current; 28857b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 28861da177e4SLinus Torvalds 28877b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 28887b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 2889d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 28907b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 2891d6277db4SRafael J. Wysocki 2892a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2893d6277db4SRafael J. Wysocki 28947b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 28957b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 28967b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 2897d6277db4SRafael J. Wysocki 28987b51755cSKOSAKI Motohiro return nr_reclaimed; 28991da177e4SLinus Torvalds } 2900c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 29011da177e4SLinus Torvalds 29021da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 29031da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 29041da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 29051da177e4SLinus Torvalds restore their cpu bindings. */ 29069c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb, 290769e05944SAndrew Morton unsigned long action, void *hcpu) 29081da177e4SLinus Torvalds { 290958c0a4a7SYasunori Goto int nid; 29101da177e4SLinus Torvalds 29118bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 291258c0a4a7SYasunori Goto for_each_node_state(nid, N_HIGH_MEMORY) { 2913c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 2914a70f7302SRusty Russell const struct cpumask *mask; 2915a70f7302SRusty Russell 2916a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 2917c5f59f08SMike Travis 29183e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 29191da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 2920c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 29211da177e4SLinus Torvalds } 29221da177e4SLinus Torvalds } 29231da177e4SLinus Torvalds return NOTIFY_OK; 29241da177e4SLinus Torvalds } 29251da177e4SLinus Torvalds 29263218ae14SYasunori Goto /* 29273218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 29283218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 29293218ae14SYasunori Goto */ 29303218ae14SYasunori Goto int kswapd_run(int nid) 29313218ae14SYasunori Goto { 29323218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 29333218ae14SYasunori Goto int ret = 0; 29343218ae14SYasunori Goto 29353218ae14SYasunori Goto if (pgdat->kswapd) 29363218ae14SYasunori Goto return 0; 29373218ae14SYasunori Goto 29383218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 29393218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 29403218ae14SYasunori Goto /* failure at boot is fatal */ 29413218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 29423218ae14SYasunori Goto printk("Failed to start kswapd on node %d\n",nid); 29433218ae14SYasunori Goto ret = -1; 29443218ae14SYasunori Goto } 29453218ae14SYasunori Goto return ret; 29463218ae14SYasunori Goto } 29473218ae14SYasunori Goto 29488fe23e05SDavid Rientjes /* 29498fe23e05SDavid Rientjes * Called by memory hotplug when all memory in a node is offlined. 29508fe23e05SDavid Rientjes */ 29518fe23e05SDavid Rientjes void kswapd_stop(int nid) 29528fe23e05SDavid Rientjes { 29538fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 29548fe23e05SDavid Rientjes 29558fe23e05SDavid Rientjes if (kswapd) 29568fe23e05SDavid Rientjes kthread_stop(kswapd); 29578fe23e05SDavid Rientjes } 29588fe23e05SDavid Rientjes 29591da177e4SLinus Torvalds static int __init kswapd_init(void) 29601da177e4SLinus Torvalds { 29613218ae14SYasunori Goto int nid; 296269e05944SAndrew Morton 29631da177e4SLinus Torvalds swap_setup(); 29649422ffbaSChristoph Lameter for_each_node_state(nid, N_HIGH_MEMORY) 29653218ae14SYasunori Goto kswapd_run(nid); 29661da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 29671da177e4SLinus Torvalds return 0; 29681da177e4SLinus Torvalds } 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds module_init(kswapd_init) 29719eeff239SChristoph Lameter 29729eeff239SChristoph Lameter #ifdef CONFIG_NUMA 29739eeff239SChristoph Lameter /* 29749eeff239SChristoph Lameter * Zone reclaim mode 29759eeff239SChristoph Lameter * 29769eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 29779eeff239SChristoph Lameter * the watermarks. 29789eeff239SChristoph Lameter */ 29799eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 29809eeff239SChristoph Lameter 29811b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 29827d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 29831b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 29841b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 29851b2ffb78SChristoph Lameter 29869eeff239SChristoph Lameter /* 2987a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 2988a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 2989a92f7126SChristoph Lameter * a zone. 2990a92f7126SChristoph Lameter */ 2991a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 2992a92f7126SChristoph Lameter 29939eeff239SChristoph Lameter /* 29949614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 29959614634fSChristoph Lameter * occur. 29969614634fSChristoph Lameter */ 29979614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 29989614634fSChristoph Lameter 29999614634fSChristoph Lameter /* 30000ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 30010ff38490SChristoph Lameter * slab reclaim needs to occur. 30020ff38490SChristoph Lameter */ 30030ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 30040ff38490SChristoph Lameter 300590afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 300690afa5deSMel Gorman { 300790afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 300890afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 300990afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 301090afa5deSMel Gorman 301190afa5deSMel Gorman /* 301290afa5deSMel Gorman * It's possible for there to be more file mapped pages than 301390afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 301490afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 301590afa5deSMel Gorman */ 301690afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 301790afa5deSMel Gorman } 301890afa5deSMel Gorman 301990afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 302090afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 302190afa5deSMel Gorman { 302290afa5deSMel Gorman long nr_pagecache_reclaimable; 302390afa5deSMel Gorman long delta = 0; 302490afa5deSMel Gorman 302590afa5deSMel Gorman /* 302690afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 302790afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 302890afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 302990afa5deSMel Gorman * a better estimate 303090afa5deSMel Gorman */ 303190afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 303290afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 303390afa5deSMel Gorman else 303490afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 303590afa5deSMel Gorman 303690afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 303790afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 303890afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 303990afa5deSMel Gorman 304090afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 304190afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 304290afa5deSMel Gorman delta = nr_pagecache_reclaimable; 304390afa5deSMel Gorman 304490afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 304590afa5deSMel Gorman } 304690afa5deSMel Gorman 30470ff38490SChristoph Lameter /* 30489eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 30499eeff239SChristoph Lameter */ 3050179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 30519eeff239SChristoph Lameter { 30527fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 305369e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 30549eeff239SChristoph Lameter struct task_struct *p = current; 30559eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3056179e9639SAndrew Morton struct scan_control sc = { 3057179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3058a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 30592e2e4259SKOSAKI Motohiro .may_swap = 1, 306022fba335SKOSAKI Motohiro .nr_to_reclaim = max_t(unsigned long, nr_pages, 306122fba335SKOSAKI Motohiro SWAP_CLUSTER_MAX), 3062179e9639SAndrew Morton .gfp_mask = gfp_mask, 3063bd2f6199SJohannes Weiner .order = order, 30649e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3065179e9639SAndrew Morton }; 3066a09ed5e0SYing Han struct shrink_control shrink = { 3067a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3068a09ed5e0SYing Han }; 306915748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 30709eeff239SChristoph Lameter 30719eeff239SChristoph Lameter cond_resched(); 3072d4f7796eSChristoph Lameter /* 3073d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3074d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3075d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3076d4f7796eSChristoph Lameter */ 3077d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 307876ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 30799eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 30809eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3081c84db23cSChristoph Lameter 308290afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3083a92f7126SChristoph Lameter /* 30840ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 30850ff38490SChristoph Lameter * priorities until we have enough memory freed. 3086a92f7126SChristoph Lameter */ 3087a92f7126SChristoph Lameter do { 30889e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 30899e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 30900ff38490SChristoph Lameter } 3091a92f7126SChristoph Lameter 309215748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 309315748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 30942a16e3f4SChristoph Lameter /* 30957fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 30960ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 30970ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 30980ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 30990ff38490SChristoph Lameter * pages. 31002a16e3f4SChristoph Lameter * 31010ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 31020ff38490SChristoph Lameter * take a long time. 31032a16e3f4SChristoph Lameter */ 31044dc4b3d9SKOSAKI Motohiro for (;;) { 31054dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 31064dc4b3d9SKOSAKI Motohiro 31074dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 31081495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 31094dc4b3d9SKOSAKI Motohiro break; 31104dc4b3d9SKOSAKI Motohiro 31114dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 31124dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 31134dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 31144dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 31154dc4b3d9SKOSAKI Motohiro break; 31164dc4b3d9SKOSAKI Motohiro } 311783e33a47SChristoph Lameter 311883e33a47SChristoph Lameter /* 311983e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 312083e33a47SChristoph Lameter * reclaimed from this zone. 312183e33a47SChristoph Lameter */ 312215748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 312315748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 312415748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 31252a16e3f4SChristoph Lameter } 31262a16e3f4SChristoph Lameter 31279eeff239SChristoph Lameter p->reclaim_state = NULL; 3128d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 312976ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3130a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 31319eeff239SChristoph Lameter } 3132179e9639SAndrew Morton 3133179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3134179e9639SAndrew Morton { 3135179e9639SAndrew Morton int node_id; 3136d773ed6bSDavid Rientjes int ret; 3137179e9639SAndrew Morton 3138179e9639SAndrew Morton /* 31390ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 31400ff38490SChristoph Lameter * slab pages if we are over the defined limits. 314134aa1330SChristoph Lameter * 31429614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 31439614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 31449614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 31459614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 31469614634fSChristoph Lameter * unmapped file backed pages. 3147179e9639SAndrew Morton */ 314890afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 314990afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3150fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3151179e9639SAndrew Morton 315293e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable) 3153fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3154d773ed6bSDavid Rientjes 3155179e9639SAndrew Morton /* 3156d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3157179e9639SAndrew Morton */ 3158d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3159fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3160179e9639SAndrew Morton 3161179e9639SAndrew Morton /* 3162179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3163179e9639SAndrew Morton * have associated processors. This will favor the local processor 3164179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3165179e9639SAndrew Morton * as wide as possible. 3166179e9639SAndrew Morton */ 316789fa3024SChristoph Lameter node_id = zone_to_nid(zone); 316837c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3169fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3170d773ed6bSDavid Rientjes 3171d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3172fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3173fa5e084eSMel Gorman 3174d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3175d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3176d773ed6bSDavid Rientjes 317724cf7251SMel Gorman if (!ret) 317824cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 317924cf7251SMel Gorman 3180d773ed6bSDavid Rientjes return ret; 3181179e9639SAndrew Morton } 31829eeff239SChristoph Lameter #endif 3183894bc310SLee Schermerhorn 3184894bc310SLee Schermerhorn /* 3185894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3186894bc310SLee Schermerhorn * @page: the page to test 3187894bc310SLee Schermerhorn * @vma: the VMA in which the page is or will be mapped, may be NULL 3188894bc310SLee Schermerhorn * 3189894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 3190b291f000SNick Piggin * lists vs unevictable list. The vma argument is !NULL when called from the 3191b291f000SNick Piggin * fault path to determine how to instantate a new page. 3192894bc310SLee Schermerhorn * 3193894bc310SLee Schermerhorn * Reasons page might not be evictable: 3194ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3195b291f000SNick Piggin * (2) page is part of an mlocked VMA 3196ba9ddf49SLee Schermerhorn * 3197894bc310SLee Schermerhorn */ 3198894bc310SLee Schermerhorn int page_evictable(struct page *page, struct vm_area_struct *vma) 3199894bc310SLee Schermerhorn { 3200894bc310SLee Schermerhorn 3201ba9ddf49SLee Schermerhorn if (mapping_unevictable(page_mapping(page))) 3202ba9ddf49SLee Schermerhorn return 0; 3203ba9ddf49SLee Schermerhorn 3204096a7cf4SYing Han if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page))) 3205b291f000SNick Piggin return 0; 3206894bc310SLee Schermerhorn 3207894bc310SLee Schermerhorn return 1; 3208894bc310SLee Schermerhorn } 320989e004eaSLee Schermerhorn 321085046579SHugh Dickins #ifdef CONFIG_SHMEM 321189e004eaSLee Schermerhorn /** 321224513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 321324513264SHugh Dickins * @pages: array of pages to check 321424513264SHugh Dickins * @nr_pages: number of pages to check 321589e004eaSLee Schermerhorn * 321624513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 321785046579SHugh Dickins * 321885046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 321989e004eaSLee Schermerhorn */ 322024513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 322189e004eaSLee Schermerhorn { 3222925b7673SJohannes Weiner struct lruvec *lruvec; 322324513264SHugh Dickins struct zone *zone = NULL; 322424513264SHugh Dickins int pgscanned = 0; 322524513264SHugh Dickins int pgrescued = 0; 322689e004eaSLee Schermerhorn int i; 322789e004eaSLee Schermerhorn 322824513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 322924513264SHugh Dickins struct page *page = pages[i]; 323024513264SHugh Dickins struct zone *pagezone; 323189e004eaSLee Schermerhorn 323224513264SHugh Dickins pgscanned++; 323324513264SHugh Dickins pagezone = page_zone(page); 323489e004eaSLee Schermerhorn if (pagezone != zone) { 323589e004eaSLee Schermerhorn if (zone) 323689e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 323789e004eaSLee Schermerhorn zone = pagezone; 323889e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 323989e004eaSLee Schermerhorn } 324089e004eaSLee Schermerhorn 324124513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 324224513264SHugh Dickins continue; 324389e004eaSLee Schermerhorn 324424513264SHugh Dickins if (page_evictable(page, NULL)) { 324524513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 324624513264SHugh Dickins 324724513264SHugh Dickins VM_BUG_ON(PageActive(page)); 324824513264SHugh Dickins ClearPageUnevictable(page); 324924513264SHugh Dickins __dec_zone_state(zone, NR_UNEVICTABLE); 325024513264SHugh Dickins lruvec = mem_cgroup_lru_move_lists(zone, page, 325124513264SHugh Dickins LRU_UNEVICTABLE, lru); 325224513264SHugh Dickins list_move(&page->lru, &lruvec->lists[lru]); 325324513264SHugh Dickins __inc_zone_state(zone, NR_INACTIVE_ANON + lru); 325424513264SHugh Dickins pgrescued++; 325589e004eaSLee Schermerhorn } 325689e004eaSLee Schermerhorn } 325724513264SHugh Dickins 325824513264SHugh Dickins if (zone) { 325924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 326024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 326124513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 326224513264SHugh Dickins } 326385046579SHugh Dickins } 326485046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3265af936a16SLee Schermerhorn 3266264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3267af936a16SLee Schermerhorn { 3268264e56d8SJohannes Weiner printk_once(KERN_WARNING 326925bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3270264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 327125bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 327225bd91bdSKOSAKI Motohiro current->comm); 3273af936a16SLee Schermerhorn } 3274af936a16SLee Schermerhorn 3275af936a16SLee Schermerhorn /* 3276af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3277af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3278af936a16SLee Schermerhorn */ 3279af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3280af936a16SLee Schermerhorn 3281af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 32828d65af78SAlexey Dobriyan void __user *buffer, 3283af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3284af936a16SLee Schermerhorn { 3285264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 32868d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3287af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3288af936a16SLee Schermerhorn return 0; 3289af936a16SLee Schermerhorn } 3290af936a16SLee Schermerhorn 3291e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3292af936a16SLee Schermerhorn /* 3293af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3294af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3295af936a16SLee Schermerhorn */ 3296af936a16SLee Schermerhorn 329710fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 329810fbcf4cSKay Sievers struct device_attribute *attr, 3299af936a16SLee Schermerhorn char *buf) 3300af936a16SLee Schermerhorn { 3301264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3302af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3303af936a16SLee Schermerhorn } 3304af936a16SLee Schermerhorn 330510fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 330610fbcf4cSKay Sievers struct device_attribute *attr, 3307af936a16SLee Schermerhorn const char *buf, size_t count) 3308af936a16SLee Schermerhorn { 3309264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3310af936a16SLee Schermerhorn return 1; 3311af936a16SLee Schermerhorn } 3312af936a16SLee Schermerhorn 3313af936a16SLee Schermerhorn 331410fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3315af936a16SLee Schermerhorn read_scan_unevictable_node, 3316af936a16SLee Schermerhorn write_scan_unevictable_node); 3317af936a16SLee Schermerhorn 3318af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3319af936a16SLee Schermerhorn { 332010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3321af936a16SLee Schermerhorn } 3322af936a16SLee Schermerhorn 3323af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3324af936a16SLee Schermerhorn { 332510fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3326af936a16SLee Schermerhorn } 3327e4455abbSThadeu Lima de Souza Cascardo #endif 3328