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 97f16015fbSJohannes Weiner struct mem_cgroup_zone { 98f16015fbSJohannes Weiner struct mem_cgroup *mem_cgroup; 99f16015fbSJohannes Weiner struct zone *zone; 100f16015fbSJohannes Weiner }; 101f16015fbSJohannes Weiner 1021da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1051da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1061da177e4SLinus Torvalds do { \ 1071da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1081da177e4SLinus Torvalds struct page *prev; \ 1091da177e4SLinus Torvalds \ 1101da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1111da177e4SLinus Torvalds prefetch(&prev->_field); \ 1121da177e4SLinus Torvalds } \ 1131da177e4SLinus Torvalds } while (0) 1141da177e4SLinus Torvalds #else 1151da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1161da177e4SLinus Torvalds #endif 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1191da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1201da177e4SLinus Torvalds do { \ 1211da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1221da177e4SLinus Torvalds struct page *prev; \ 1231da177e4SLinus Torvalds \ 1241da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1251da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1261da177e4SLinus Torvalds } \ 1271da177e4SLinus Torvalds } while (0) 1281da177e4SLinus Torvalds #else 1291da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1301da177e4SLinus Torvalds #endif 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* 1331da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1341da177e4SLinus Torvalds */ 1351da177e4SLinus Torvalds int vm_swappiness = 60; 136bd1e22b8SAndrew Morton long vm_total_pages; /* The total number of pages which the VM controls */ 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1391da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1401da177e4SLinus Torvalds 14100f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 14289b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14389b5fae5SJohannes Weiner { 144f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14589b5fae5SJohannes Weiner } 14691a45470SKAMEZAWA Hiroyuki #else 14789b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14889b5fae5SJohannes Weiner { 14989b5fae5SJohannes Weiner return true; 15089b5fae5SJohannes Weiner } 15191a45470SKAMEZAWA Hiroyuki #endif 15291a45470SKAMEZAWA Hiroyuki 153f16015fbSJohannes Weiner static struct zone_reclaim_stat *get_reclaim_stat(struct mem_cgroup_zone *mz) 1546e901571SKOSAKI Motohiro { 15589abfab1SHugh Dickins return &mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup)->reclaim_stat; 1566e901571SKOSAKI Motohiro } 1576e901571SKOSAKI Motohiro 158074291feSKonstantin Khlebnikov static unsigned long get_lruvec_size(struct lruvec *lruvec, enum lru_list lru) 159c9f299d9SKOSAKI Motohiro { 160c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 161074291feSKonstantin Khlebnikov return mem_cgroup_get_lruvec_size(lruvec, lru); 162a3d8e054SKOSAKI Motohiro 163074291feSKonstantin Khlebnikov return zone_page_state(lruvec_zone(lruvec), NR_LRU_BASE + lru); 164c9f299d9SKOSAKI Motohiro } 165c9f299d9SKOSAKI Motohiro 1661da177e4SLinus Torvalds /* 1671da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1681da177e4SLinus Torvalds */ 1698e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker) 1701da177e4SLinus Torvalds { 17183aeeadaSKonstantin Khlebnikov atomic_long_set(&shrinker->nr_in_batch, 0); 1721da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1731da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1741da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1751da177e4SLinus Torvalds } 1768e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* 1791da177e4SLinus Torvalds * Remove one 1801da177e4SLinus Torvalds */ 1818e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1841da177e4SLinus Torvalds list_del(&shrinker->list); 1851da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1861da177e4SLinus Torvalds } 1878e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 1881da177e4SLinus Torvalds 1891495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker, 1901495f230SYing Han struct shrink_control *sc, 1911495f230SYing Han unsigned long nr_to_scan) 1921495f230SYing Han { 1931495f230SYing Han sc->nr_to_scan = nr_to_scan; 1941495f230SYing Han return (*shrinker->shrink)(shrinker, sc); 1951495f230SYing Han } 1961495f230SYing Han 1971da177e4SLinus Torvalds #define SHRINK_BATCH 128 1981da177e4SLinus Torvalds /* 1991da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 2001da177e4SLinus Torvalds * 2011da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 2021da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 2031da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 2041da177e4SLinus Torvalds * generated by these structures. 2051da177e4SLinus Torvalds * 206183ff22bSSimon Arlott * If the vm encountered mapped pages on the LRU it increase the pressure on 2071da177e4SLinus Torvalds * slab to avoid swapping. 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 2121da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 2131da177e4SLinus Torvalds * slab reclaim versus page reclaim. 214b15e0905Sakpm@osdl.org * 215b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 2161da177e4SLinus Torvalds */ 217a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink, 2181495f230SYing Han unsigned long nr_pages_scanned, 21969e05944SAndrew Morton unsigned long lru_pages) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds struct shrinker *shrinker; 22269e05944SAndrew Morton unsigned long ret = 0; 2231da177e4SLinus Torvalds 2241495f230SYing Han if (nr_pages_scanned == 0) 2251495f230SYing Han nr_pages_scanned = SWAP_CLUSTER_MAX; 2261da177e4SLinus Torvalds 227f06590bdSMinchan Kim if (!down_read_trylock(&shrinker_rwsem)) { 228f06590bdSMinchan Kim /* Assume we'll be able to shrink next time */ 229f06590bdSMinchan Kim ret = 1; 230f06590bdSMinchan Kim goto out; 231f06590bdSMinchan Kim } 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 2341da177e4SLinus Torvalds unsigned long long delta; 235635697c6SKonstantin Khlebnikov long total_scan; 236635697c6SKonstantin Khlebnikov long max_pass; 23709576073SDave Chinner int shrink_ret = 0; 238acf92b48SDave Chinner long nr; 239acf92b48SDave Chinner long new_nr; 240e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 241e9299f50SDave Chinner : SHRINK_BATCH; 2421da177e4SLinus Torvalds 243635697c6SKonstantin Khlebnikov max_pass = do_shrinker_shrink(shrinker, shrink, 0); 244635697c6SKonstantin Khlebnikov if (max_pass <= 0) 245635697c6SKonstantin Khlebnikov continue; 246635697c6SKonstantin Khlebnikov 247acf92b48SDave Chinner /* 248acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 249acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 250acf92b48SDave Chinner * don't also do this scanning work. 251acf92b48SDave Chinner */ 25283aeeadaSKonstantin Khlebnikov nr = atomic_long_xchg(&shrinker->nr_in_batch, 0); 253acf92b48SDave Chinner 254acf92b48SDave Chinner total_scan = nr; 2551495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 256ea164d73SAndrea Arcangeli delta *= max_pass; 2571da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 258acf92b48SDave Chinner total_scan += delta; 259acf92b48SDave Chinner if (total_scan < 0) { 26088c3bd70SDavid Rientjes printk(KERN_ERR "shrink_slab: %pF negative objects to " 26188c3bd70SDavid Rientjes "delete nr=%ld\n", 262acf92b48SDave Chinner shrinker->shrink, total_scan); 263acf92b48SDave Chinner total_scan = max_pass; 264ea164d73SAndrea Arcangeli } 265ea164d73SAndrea Arcangeli 266ea164d73SAndrea Arcangeli /* 2673567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2683567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2693567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2703567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2713567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2723567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2733567b59aSDave Chinner * memory. 2743567b59aSDave Chinner * 2753567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2763567b59aSDave Chinner * a large delta change is calculated directly. 2773567b59aSDave Chinner */ 2783567b59aSDave Chinner if (delta < max_pass / 4) 2793567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2803567b59aSDave Chinner 2813567b59aSDave Chinner /* 282ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 283ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 284ea164d73SAndrea Arcangeli * freeable entries. 285ea164d73SAndrea Arcangeli */ 286acf92b48SDave Chinner if (total_scan > max_pass * 2) 287acf92b48SDave Chinner total_scan = max_pass * 2; 2881da177e4SLinus Torvalds 289acf92b48SDave Chinner trace_mm_shrink_slab_start(shrinker, shrink, nr, 29009576073SDave Chinner nr_pages_scanned, lru_pages, 29109576073SDave Chinner max_pass, delta, total_scan); 29209576073SDave Chinner 293e9299f50SDave Chinner while (total_scan >= batch_size) { 294b15e0905Sakpm@osdl.org int nr_before; 2951da177e4SLinus Torvalds 2961495f230SYing Han nr_before = do_shrinker_shrink(shrinker, shrink, 0); 2971495f230SYing Han shrink_ret = do_shrinker_shrink(shrinker, shrink, 298e9299f50SDave Chinner batch_size); 2991da177e4SLinus Torvalds if (shrink_ret == -1) 3001da177e4SLinus Torvalds break; 301b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 302b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 303e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 304e9299f50SDave Chinner total_scan -= batch_size; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds cond_resched(); 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 309acf92b48SDave Chinner /* 310acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 311acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 312acf92b48SDave Chinner * scan, there is no need to do an update. 313acf92b48SDave Chinner */ 31483aeeadaSKonstantin Khlebnikov if (total_scan > 0) 31583aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 31683aeeadaSKonstantin Khlebnikov &shrinker->nr_in_batch); 31783aeeadaSKonstantin Khlebnikov else 31883aeeadaSKonstantin Khlebnikov new_nr = atomic_long_read(&shrinker->nr_in_batch); 319acf92b48SDave Chinner 320acf92b48SDave Chinner trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr); 3211da177e4SLinus Torvalds } 3221da177e4SLinus Torvalds up_read(&shrinker_rwsem); 323f06590bdSMinchan Kim out: 324f06590bdSMinchan Kim cond_resched(); 325b15e0905Sakpm@osdl.org return ret; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3291da177e4SLinus Torvalds { 330ceddc3a5SJohannes Weiner /* 331ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 332ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 333ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 334ceddc3a5SJohannes Weiner */ 335edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3361da177e4SLinus Torvalds } 3371da177e4SLinus Torvalds 3387d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3397d3579e8SKOSAKI Motohiro struct scan_control *sc) 3401da177e4SLinus Torvalds { 341930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3421da177e4SLinus Torvalds return 1; 3431da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3441da177e4SLinus Torvalds return 1; 3451da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3461da177e4SLinus Torvalds return 1; 3471da177e4SLinus Torvalds return 0; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* 3511da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3521da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3531da177e4SLinus Torvalds * fsync(), msync() or close(). 3541da177e4SLinus Torvalds * 3551da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 3561da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 3571da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 3581da177e4SLinus Torvalds * 3591da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 3601da177e4SLinus Torvalds * __GFP_FS. 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 3631da177e4SLinus Torvalds struct page *page, int error) 3641da177e4SLinus Torvalds { 3657eaceaccSJens Axboe lock_page(page); 3663e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 3673e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 3681da177e4SLinus Torvalds unlock_page(page); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 37104e62a29SChristoph Lameter /* possible outcome of pageout() */ 37204e62a29SChristoph Lameter typedef enum { 37304e62a29SChristoph Lameter /* failed to write page out, page is locked */ 37404e62a29SChristoph Lameter PAGE_KEEP, 37504e62a29SChristoph Lameter /* move page to the active list, page is locked */ 37604e62a29SChristoph Lameter PAGE_ACTIVATE, 37704e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 37804e62a29SChristoph Lameter PAGE_SUCCESS, 37904e62a29SChristoph Lameter /* page is clean and locked */ 38004e62a29SChristoph Lameter PAGE_CLEAN, 38104e62a29SChristoph Lameter } pageout_t; 38204e62a29SChristoph Lameter 3831da177e4SLinus Torvalds /* 3841742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3851742f19fSAndrew Morton * Calls ->writepage(). 3861da177e4SLinus Torvalds */ 387c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 3887d3579e8SKOSAKI Motohiro struct scan_control *sc) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds /* 3911da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3921da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3931da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3941da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3951da177e4SLinus Torvalds * PagePrivate for that. 3961da177e4SLinus Torvalds * 3976aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 3981da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 3991da177e4SLinus Torvalds * will block. 4001da177e4SLinus Torvalds * 4011da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4021da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4031da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4041da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4051da177e4SLinus Torvalds */ 4061da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4071da177e4SLinus Torvalds return PAGE_KEEP; 4081da177e4SLinus Torvalds if (!mapping) { 4091da177e4SLinus Torvalds /* 4101da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4111da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4121da177e4SLinus Torvalds */ 413266cf658SDavid Howells if (page_has_private(page)) { 4141da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4151da177e4SLinus Torvalds ClearPageDirty(page); 416d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4171da177e4SLinus Torvalds return PAGE_CLEAN; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds return PAGE_KEEP; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4231da177e4SLinus Torvalds return PAGE_ACTIVATE; 4240e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4251da177e4SLinus Torvalds return PAGE_KEEP; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4281da177e4SLinus Torvalds int res; 4291da177e4SLinus Torvalds struct writeback_control wbc = { 4301da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4311da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 432111ebb6eSOGAWA Hirofumi .range_start = 0, 433111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4341da177e4SLinus Torvalds .for_reclaim = 1, 4351da177e4SLinus Torvalds }; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds SetPageReclaim(page); 4381da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4391da177e4SLinus Torvalds if (res < 0) 4401da177e4SLinus Torvalds handle_write_error(mapping, page, res); 441994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4421da177e4SLinus Torvalds ClearPageReclaim(page); 4431da177e4SLinus Torvalds return PAGE_ACTIVATE; 4441da177e4SLinus Torvalds } 445c661b078SAndy Whitcroft 4461da177e4SLinus Torvalds if (!PageWriteback(page)) { 4471da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4481da177e4SLinus Torvalds ClearPageReclaim(page); 4491da177e4SLinus Torvalds } 45023b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 451e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4521da177e4SLinus Torvalds return PAGE_SUCCESS; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds return PAGE_CLEAN; 4561da177e4SLinus Torvalds } 4571da177e4SLinus Torvalds 458a649fd92SAndrew Morton /* 459e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 460e286781dSNick Piggin * gets returned with a refcount of 0. 461a649fd92SAndrew Morton */ 462e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 46349d2e9ccSChristoph Lameter { 46428e4d965SNick Piggin BUG_ON(!PageLocked(page)); 46528e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 46649d2e9ccSChristoph Lameter 46719fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 46849d2e9ccSChristoph Lameter /* 4690fd0e6b0SNick Piggin * The non racy check for a busy page. 4700fd0e6b0SNick Piggin * 4710fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 4720fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 4730fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 4740fd0e6b0SNick Piggin * here, then the following race may occur: 4750fd0e6b0SNick Piggin * 4760fd0e6b0SNick Piggin * get_user_pages(&page); 4770fd0e6b0SNick Piggin * [user mapping goes away] 4780fd0e6b0SNick Piggin * write_to(page); 4790fd0e6b0SNick Piggin * !PageDirty(page) [good] 4800fd0e6b0SNick Piggin * SetPageDirty(page); 4810fd0e6b0SNick Piggin * put_page(page); 4820fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 4830fd0e6b0SNick Piggin * 4840fd0e6b0SNick Piggin * [oops, our write_to data is lost] 4850fd0e6b0SNick Piggin * 4860fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 4870fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 4880fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 4890fd0e6b0SNick Piggin * 4900fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 4910fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 49249d2e9ccSChristoph Lameter */ 493e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 49449d2e9ccSChristoph Lameter goto cannot_free; 495e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 496e286781dSNick Piggin if (unlikely(PageDirty(page))) { 497e286781dSNick Piggin page_unfreeze_refs(page, 2); 49849d2e9ccSChristoph Lameter goto cannot_free; 499e286781dSNick Piggin } 50049d2e9ccSChristoph Lameter 50149d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 50249d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 50349d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 50419fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 505cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 506e286781dSNick Piggin } else { 5076072d13cSLinus Torvalds void (*freepage)(struct page *); 5086072d13cSLinus Torvalds 5096072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5106072d13cSLinus Torvalds 511e64a782fSMinchan Kim __delete_from_page_cache(page); 51219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 513e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5146072d13cSLinus Torvalds 5156072d13cSLinus Torvalds if (freepage != NULL) 5166072d13cSLinus Torvalds freepage(page); 517e286781dSNick Piggin } 518e286781dSNick Piggin 51949d2e9ccSChristoph Lameter return 1; 52049d2e9ccSChristoph Lameter 52149d2e9ccSChristoph Lameter cannot_free: 52219fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 52349d2e9ccSChristoph Lameter return 0; 52449d2e9ccSChristoph Lameter } 52549d2e9ccSChristoph Lameter 5261da177e4SLinus Torvalds /* 527e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 528e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 529e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 530e286781dSNick Piggin * this page. 531e286781dSNick Piggin */ 532e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 533e286781dSNick Piggin { 534e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 535e286781dSNick Piggin /* 536e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 537e286781dSNick Piggin * drops the pagecache ref for us without requiring another 538e286781dSNick Piggin * atomic operation. 539e286781dSNick Piggin */ 540e286781dSNick Piggin page_unfreeze_refs(page, 1); 541e286781dSNick Piggin return 1; 542e286781dSNick Piggin } 543e286781dSNick Piggin return 0; 544e286781dSNick Piggin } 545e286781dSNick Piggin 546894bc310SLee Schermerhorn /** 547894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 548894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 549894bc310SLee Schermerhorn * 550894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 551894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 552894bc310SLee Schermerhorn * 553894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 554894bc310SLee Schermerhorn */ 555894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 556894bc310SLee Schermerhorn { 557894bc310SLee Schermerhorn int lru; 558894bc310SLee Schermerhorn int active = !!TestClearPageActive(page); 559bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 560894bc310SLee Schermerhorn 561894bc310SLee Schermerhorn VM_BUG_ON(PageLRU(page)); 562894bc310SLee Schermerhorn 563894bc310SLee Schermerhorn redo: 564894bc310SLee Schermerhorn ClearPageUnevictable(page); 565894bc310SLee Schermerhorn 566894bc310SLee Schermerhorn if (page_evictable(page, NULL)) { 567894bc310SLee Schermerhorn /* 568894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 569894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 570894bc310SLee Schermerhorn * unevictable page on [in]active list. 571894bc310SLee Schermerhorn * We know how to handle that. 572894bc310SLee Schermerhorn */ 573401a8e1cSJohannes Weiner lru = active + page_lru_base_type(page); 574894bc310SLee Schermerhorn lru_cache_add_lru(page, lru); 575894bc310SLee Schermerhorn } else { 576894bc310SLee Schermerhorn /* 577894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 578894bc310SLee Schermerhorn * list. 579894bc310SLee Schermerhorn */ 580894bc310SLee Schermerhorn lru = LRU_UNEVICTABLE; 581894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 5826a7b9548SJohannes Weiner /* 58321ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 58421ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 58521ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 58624513264SHugh Dickins * isolation/check_move_unevictable_pages, 58721ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 5886a7b9548SJohannes Weiner * the page back to the evictable list. 5896a7b9548SJohannes Weiner * 59021ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 5916a7b9548SJohannes Weiner */ 5926a7b9548SJohannes Weiner smp_mb(); 593894bc310SLee Schermerhorn } 594894bc310SLee Schermerhorn 595894bc310SLee Schermerhorn /* 596894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 597894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 598894bc310SLee Schermerhorn * check after we added it to the list, again. 599894bc310SLee Schermerhorn */ 600894bc310SLee Schermerhorn if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { 601894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 602894bc310SLee Schermerhorn put_page(page); 603894bc310SLee Schermerhorn goto redo; 604894bc310SLee Schermerhorn } 605894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 606894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 607894bc310SLee Schermerhorn * nothing to do here. 608894bc310SLee Schermerhorn */ 609894bc310SLee Schermerhorn } 610894bc310SLee Schermerhorn 611bbfd28eeSLee Schermerhorn if (was_unevictable && lru != LRU_UNEVICTABLE) 612bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 613bbfd28eeSLee Schermerhorn else if (!was_unevictable && lru == LRU_UNEVICTABLE) 614bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 615bbfd28eeSLee Schermerhorn 616894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 617894bc310SLee Schermerhorn } 618894bc310SLee Schermerhorn 619dfc8d636SJohannes Weiner enum page_references { 620dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 621dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 62264574746SJohannes Weiner PAGEREF_KEEP, 623dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 624dfc8d636SJohannes Weiner }; 625dfc8d636SJohannes Weiner 626dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 627dfc8d636SJohannes Weiner struct scan_control *sc) 628dfc8d636SJohannes Weiner { 62964574746SJohannes Weiner int referenced_ptes, referenced_page; 630dfc8d636SJohannes Weiner unsigned long vm_flags; 631dfc8d636SJohannes Weiner 632c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 633c3ac9a8aSJohannes Weiner &vm_flags); 63464574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 635dfc8d636SJohannes Weiner 636dfc8d636SJohannes Weiner /* 637dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 638dfc8d636SJohannes Weiner * move the page to the unevictable list. 639dfc8d636SJohannes Weiner */ 640dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 641dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 642dfc8d636SJohannes Weiner 64364574746SJohannes Weiner if (referenced_ptes) { 644e4898273SMichal Hocko if (PageSwapBacked(page)) 64564574746SJohannes Weiner return PAGEREF_ACTIVATE; 64664574746SJohannes Weiner /* 64764574746SJohannes Weiner * All mapped pages start out with page table 64864574746SJohannes Weiner * references from the instantiating fault, so we need 64964574746SJohannes Weiner * to look twice if a mapped file page is used more 65064574746SJohannes Weiner * than once. 65164574746SJohannes Weiner * 65264574746SJohannes Weiner * Mark it and spare it for another trip around the 65364574746SJohannes Weiner * inactive list. Another page table reference will 65464574746SJohannes Weiner * lead to its activation. 65564574746SJohannes Weiner * 65664574746SJohannes Weiner * Note: the mark is set for activated pages as well 65764574746SJohannes Weiner * so that recently deactivated but used pages are 65864574746SJohannes Weiner * quickly recovered. 65964574746SJohannes Weiner */ 66064574746SJohannes Weiner SetPageReferenced(page); 66164574746SJohannes Weiner 66234dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 663dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 664dfc8d636SJohannes Weiner 665c909e993SKonstantin Khlebnikov /* 666c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 667c909e993SKonstantin Khlebnikov */ 668c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 669c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 670c909e993SKonstantin Khlebnikov 67164574746SJohannes Weiner return PAGEREF_KEEP; 67264574746SJohannes Weiner } 67364574746SJohannes Weiner 674dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 6752e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 676dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 67764574746SJohannes Weiner 67864574746SJohannes Weiner return PAGEREF_RECLAIM; 679dfc8d636SJohannes Weiner } 680dfc8d636SJohannes Weiner 681e286781dSNick Piggin /* 6821742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 6831da177e4SLinus Torvalds */ 6841742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 6856a18adb3SKonstantin Khlebnikov struct zone *zone, 686f84f6e2bSMel Gorman struct scan_control *sc, 68792df3a72SMel Gorman unsigned long *ret_nr_dirty, 68892df3a72SMel Gorman unsigned long *ret_nr_writeback) 6891da177e4SLinus Torvalds { 6901da177e4SLinus Torvalds LIST_HEAD(ret_pages); 691abe4c3b5SMel Gorman LIST_HEAD(free_pages); 6921da177e4SLinus Torvalds int pgactivate = 0; 6930e093d99SMel Gorman unsigned long nr_dirty = 0; 6940e093d99SMel Gorman unsigned long nr_congested = 0; 69505ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 69692df3a72SMel Gorman unsigned long nr_writeback = 0; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds cond_resched(); 6991da177e4SLinus Torvalds 7001da177e4SLinus Torvalds while (!list_empty(page_list)) { 701dfc8d636SJohannes Weiner enum page_references references; 7021da177e4SLinus Torvalds struct address_space *mapping; 7031da177e4SLinus Torvalds struct page *page; 7041da177e4SLinus Torvalds int may_enter_fs; 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds cond_resched(); 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds page = lru_to_page(page_list); 7091da177e4SLinus Torvalds list_del(&page->lru); 7101da177e4SLinus Torvalds 711529ae9aaSNick Piggin if (!trylock_page(page)) 7121da177e4SLinus Torvalds goto keep; 7131da177e4SLinus Torvalds 714725d704eSNick Piggin VM_BUG_ON(PageActive(page)); 7156a18adb3SKonstantin Khlebnikov VM_BUG_ON(page_zone(page) != zone); 7161da177e4SLinus Torvalds 7171da177e4SLinus Torvalds sc->nr_scanned++; 71880e43426SChristoph Lameter 719b291f000SNick Piggin if (unlikely(!page_evictable(page, NULL))) 720b291f000SNick Piggin goto cull_mlocked; 721894bc310SLee Schermerhorn 722a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 72380e43426SChristoph Lameter goto keep_locked; 72480e43426SChristoph Lameter 7251da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 7261da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 7271da177e4SLinus Torvalds sc->nr_scanned++; 7281da177e4SLinus Torvalds 729c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 730c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 731c661b078SAndy Whitcroft 732c661b078SAndy Whitcroft if (PageWriteback(page)) { 73392df3a72SMel Gorman nr_writeback++; 7347d3579e8SKOSAKI Motohiro unlock_page(page); 73541ac1999SMel Gorman goto keep; 736c661b078SAndy Whitcroft } 7371da177e4SLinus Torvalds 7386a18adb3SKonstantin Khlebnikov references = page_check_references(page, sc); 739dfc8d636SJohannes Weiner switch (references) { 740dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 7411da177e4SLinus Torvalds goto activate_locked; 74264574746SJohannes Weiner case PAGEREF_KEEP: 74364574746SJohannes Weiner goto keep_locked; 744dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 745dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 746dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 747dfc8d636SJohannes Weiner } 7481da177e4SLinus Torvalds 7491da177e4SLinus Torvalds /* 7501da177e4SLinus Torvalds * Anonymous process memory has backing store? 7511da177e4SLinus Torvalds * Try to allocate it some swap space here. 7521da177e4SLinus Torvalds */ 753b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 75463eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 75563eb6b93SHugh Dickins goto keep_locked; 756ac47b003SHugh Dickins if (!add_to_swap(page)) 7571da177e4SLinus Torvalds goto activate_locked; 75863eb6b93SHugh Dickins may_enter_fs = 1; 759b291f000SNick Piggin } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds mapping = page_mapping(page); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 7651da177e4SLinus Torvalds * processes. Try to unmap it here. 7661da177e4SLinus Torvalds */ 7671da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 76814fa31b8SAndi Kleen switch (try_to_unmap(page, TTU_UNMAP)) { 7691da177e4SLinus Torvalds case SWAP_FAIL: 7701da177e4SLinus Torvalds goto activate_locked; 7711da177e4SLinus Torvalds case SWAP_AGAIN: 7721da177e4SLinus Torvalds goto keep_locked; 773b291f000SNick Piggin case SWAP_MLOCK: 774b291f000SNick Piggin goto cull_mlocked; 7751da177e4SLinus Torvalds case SWAP_SUCCESS: 7761da177e4SLinus Torvalds ; /* try to free the page below */ 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds if (PageDirty(page)) { 7810e093d99SMel Gorman nr_dirty++; 7820e093d99SMel Gorman 783ee72886dSMel Gorman /* 784ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 785f84f6e2bSMel Gorman * avoid risk of stack overflow but do not writeback 786f84f6e2bSMel Gorman * unless under significant pressure. 787ee72886dSMel Gorman */ 788f84f6e2bSMel Gorman if (page_is_file_cache(page) && 7899e3b2f8cSKonstantin Khlebnikov (!current_is_kswapd() || 7909e3b2f8cSKonstantin Khlebnikov sc->priority >= DEF_PRIORITY - 2)) { 79149ea7eb6SMel Gorman /* 79249ea7eb6SMel Gorman * Immediately reclaim when written back. 79349ea7eb6SMel Gorman * Similar in principal to deactivate_page() 79449ea7eb6SMel Gorman * except we already have the page isolated 79549ea7eb6SMel Gorman * and know it's dirty 79649ea7eb6SMel Gorman */ 79749ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 79849ea7eb6SMel Gorman SetPageReclaim(page); 79949ea7eb6SMel Gorman 800ee72886dSMel Gorman goto keep_locked; 801ee72886dSMel Gorman } 802ee72886dSMel Gorman 803dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 8041da177e4SLinus Torvalds goto keep_locked; 8054dd4b920SAndrew Morton if (!may_enter_fs) 8061da177e4SLinus Torvalds goto keep_locked; 80752a8363eSChristoph Lameter if (!sc->may_writepage) 8081da177e4SLinus Torvalds goto keep_locked; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 8117d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 8121da177e4SLinus Torvalds case PAGE_KEEP: 8130e093d99SMel Gorman nr_congested++; 8141da177e4SLinus Torvalds goto keep_locked; 8151da177e4SLinus Torvalds case PAGE_ACTIVATE: 8161da177e4SLinus Torvalds goto activate_locked; 8171da177e4SLinus Torvalds case PAGE_SUCCESS: 8187d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 81941ac1999SMel Gorman goto keep; 8207d3579e8SKOSAKI Motohiro if (PageDirty(page)) 8211da177e4SLinus Torvalds goto keep; 8227d3579e8SKOSAKI Motohiro 8231da177e4SLinus Torvalds /* 8241da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 8251da177e4SLinus Torvalds * ahead and try to reclaim the page. 8261da177e4SLinus Torvalds */ 827529ae9aaSNick Piggin if (!trylock_page(page)) 8281da177e4SLinus Torvalds goto keep; 8291da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 8301da177e4SLinus Torvalds goto keep_locked; 8311da177e4SLinus Torvalds mapping = page_mapping(page); 8321da177e4SLinus Torvalds case PAGE_CLEAN: 8331da177e4SLinus Torvalds ; /* try to free the page below */ 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds /* 8381da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 8391da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 8401da177e4SLinus Torvalds * the page as well. 8411da177e4SLinus Torvalds * 8421da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 8431da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 8441da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 8451da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 8461da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 8471da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 8481da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 8491da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 8501da177e4SLinus Torvalds * 8511da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 8521da177e4SLinus Torvalds * the pages which were not successfully invalidated in 8531da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 8541da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 8551da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 8561da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 8571da177e4SLinus Torvalds */ 858266cf658SDavid Howells if (page_has_private(page)) { 8591da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 8601da177e4SLinus Torvalds goto activate_locked; 861e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 862e286781dSNick Piggin unlock_page(page); 863e286781dSNick Piggin if (put_page_testzero(page)) 8641da177e4SLinus Torvalds goto free_it; 865e286781dSNick Piggin else { 866e286781dSNick Piggin /* 867e286781dSNick Piggin * rare race with speculative reference. 868e286781dSNick Piggin * the speculative reference will free 869e286781dSNick Piggin * this page shortly, so we may 870e286781dSNick Piggin * increment nr_reclaimed here (and 871e286781dSNick Piggin * leave it off the LRU). 872e286781dSNick Piggin */ 873e286781dSNick Piggin nr_reclaimed++; 874e286781dSNick Piggin continue; 875e286781dSNick Piggin } 876e286781dSNick Piggin } 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 88049d2e9ccSChristoph Lameter goto keep_locked; 8811da177e4SLinus Torvalds 882a978d6f5SNick Piggin /* 883a978d6f5SNick Piggin * At this point, we have no other references and there is 884a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 885a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 886a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 887a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 888a978d6f5SNick Piggin */ 889a978d6f5SNick Piggin __clear_page_locked(page); 890e286781dSNick Piggin free_it: 89105ff5137SAndrew Morton nr_reclaimed++; 892abe4c3b5SMel Gorman 893abe4c3b5SMel Gorman /* 894abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 895abe4c3b5SMel Gorman * appear not as the counts should be low 896abe4c3b5SMel Gorman */ 897abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 8981da177e4SLinus Torvalds continue; 8991da177e4SLinus Torvalds 900b291f000SNick Piggin cull_mlocked: 90163d6c5adSHugh Dickins if (PageSwapCache(page)) 90263d6c5adSHugh Dickins try_to_free_swap(page); 903b291f000SNick Piggin unlock_page(page); 904b291f000SNick Piggin putback_lru_page(page); 905b291f000SNick Piggin continue; 906b291f000SNick Piggin 9071da177e4SLinus Torvalds activate_locked: 90868a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 90968a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 910a2c43eedSHugh Dickins try_to_free_swap(page); 911894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 9121da177e4SLinus Torvalds SetPageActive(page); 9131da177e4SLinus Torvalds pgactivate++; 9141da177e4SLinus Torvalds keep_locked: 9151da177e4SLinus Torvalds unlock_page(page); 9161da177e4SLinus Torvalds keep: 9171da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 918b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 9191da177e4SLinus Torvalds } 920abe4c3b5SMel Gorman 9210e093d99SMel Gorman /* 9220e093d99SMel Gorman * Tag a zone as congested if all the dirty pages encountered were 9230e093d99SMel Gorman * backed by a congested BDI. In this case, reclaimers should just 9240e093d99SMel Gorman * back off and wait for congestion to clear because further reclaim 9250e093d99SMel Gorman * will encounter the same problem 9260e093d99SMel Gorman */ 92789b5fae5SJohannes Weiner if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc)) 9286a18adb3SKonstantin Khlebnikov zone_set_flag(zone, ZONE_CONGESTED); 9290e093d99SMel Gorman 930cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 931abe4c3b5SMel Gorman 9321da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 933f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 93492df3a72SMel Gorman *ret_nr_dirty += nr_dirty; 93592df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 93605ff5137SAndrew Morton return nr_reclaimed; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9395ad333ebSAndy Whitcroft /* 9405ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 9415ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 9425ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 9435ad333ebSAndy Whitcroft * 9445ad333ebSAndy Whitcroft * page: page to consider 9455ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 9465ad333ebSAndy Whitcroft * 9475ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 9485ad333ebSAndy Whitcroft */ 949f3fd4a61SKonstantin Khlebnikov int __isolate_lru_page(struct page *page, isolate_mode_t mode) 9505ad333ebSAndy Whitcroft { 9515ad333ebSAndy Whitcroft int ret = -EINVAL; 9525ad333ebSAndy Whitcroft 9535ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 9545ad333ebSAndy Whitcroft if (!PageLRU(page)) 9555ad333ebSAndy Whitcroft return ret; 9565ad333ebSAndy Whitcroft 957c53919adSMel Gorman /* Do not give back unevictable pages for compaction */ 958894bc310SLee Schermerhorn if (PageUnevictable(page)) 959894bc310SLee Schermerhorn return ret; 960894bc310SLee Schermerhorn 9615ad333ebSAndy Whitcroft ret = -EBUSY; 96208e552c6SKAMEZAWA Hiroyuki 963c8244935SMel Gorman /* 964c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 965c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 966c8244935SMel Gorman * blocking - clean pages for the most part. 967c8244935SMel Gorman * 968c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 969c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 970c8244935SMel Gorman * 971c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 972c8244935SMel Gorman * that it is possible to migrate without blocking 973c8244935SMel Gorman */ 974c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 975c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 976c8244935SMel Gorman if (PageWriteback(page)) 97739deaf85SMinchan Kim return ret; 97839deaf85SMinchan Kim 979c8244935SMel Gorman if (PageDirty(page)) { 980c8244935SMel Gorman struct address_space *mapping; 981c8244935SMel Gorman 982c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 983c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 984c8244935SMel Gorman return ret; 985c8244935SMel Gorman 986c8244935SMel Gorman /* 987c8244935SMel Gorman * Only pages without mappings or that have a 988c8244935SMel Gorman * ->migratepage callback are possible to migrate 989c8244935SMel Gorman * without blocking 990c8244935SMel Gorman */ 991c8244935SMel Gorman mapping = page_mapping(page); 992c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 993c8244935SMel Gorman return ret; 994c8244935SMel Gorman } 995c8244935SMel Gorman } 996c8244935SMel Gorman 997f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 998f80c0673SMinchan Kim return ret; 999f80c0673SMinchan Kim 10005ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 10015ad333ebSAndy Whitcroft /* 10025ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 10035ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 10045ad333ebSAndy Whitcroft * page release code relies on it. 10055ad333ebSAndy Whitcroft */ 10065ad333ebSAndy Whitcroft ClearPageLRU(page); 10075ad333ebSAndy Whitcroft ret = 0; 10085ad333ebSAndy Whitcroft } 10095ad333ebSAndy Whitcroft 10105ad333ebSAndy Whitcroft return ret; 10115ad333ebSAndy Whitcroft } 10125ad333ebSAndy Whitcroft 101349d2e9ccSChristoph Lameter /* 10141da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10151da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10161da177e4SLinus Torvalds * and working on them outside the LRU lock. 10171da177e4SLinus Torvalds * 10181da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10191da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10201da177e4SLinus Torvalds * 10211da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10221da177e4SLinus Torvalds * 10231da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 10245dc35979SKonstantin Khlebnikov * @lruvec: The LRU vector to pull pages from. 10251da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1026f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1027fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 10285ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 10293cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 10301da177e4SLinus Torvalds * 10311da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10321da177e4SLinus Torvalds */ 103369e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 10345dc35979SKonstantin Khlebnikov struct lruvec *lruvec, struct list_head *dst, 1035fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 10363cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 10371da177e4SLinus Torvalds { 1038f626012dSHugh Dickins struct list_head *src; 103969e05944SAndrew Morton unsigned long nr_taken = 0; 1040c9b02d97SWu Fengguang unsigned long scan; 10413cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1042f626012dSHugh Dickins 1043f626012dSHugh Dickins src = &lruvec->lists[lru]; 10441da177e4SLinus Torvalds 1045c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 10465ad333ebSAndy Whitcroft struct page *page; 10475ad333ebSAndy Whitcroft 10481da177e4SLinus Torvalds page = lru_to_page(src); 10491da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10501da177e4SLinus Torvalds 1051725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 10528d438f96SNick Piggin 1053f3fd4a61SKonstantin Khlebnikov switch (__isolate_lru_page(page, mode)) { 10545ad333ebSAndy Whitcroft case 0: 1055bbf808edSKonstantin Khlebnikov mem_cgroup_lru_del_list(page, lru); 10565ad333ebSAndy Whitcroft list_move(&page->lru, dst); 10572c888cfbSRik van Riel nr_taken += hpage_nr_pages(page); 10585ad333ebSAndy Whitcroft break; 10597c8ee9a8SNick Piggin 10605ad333ebSAndy Whitcroft case -EBUSY: 10615ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 10625ad333ebSAndy Whitcroft list_move(&page->lru, src); 10635ad333ebSAndy Whitcroft continue; 10645ad333ebSAndy Whitcroft 10655ad333ebSAndy Whitcroft default: 10665ad333ebSAndy Whitcroft BUG(); 10675ad333ebSAndy Whitcroft } 10685ad333ebSAndy Whitcroft } 10691da177e4SLinus Torvalds 1070f626012dSHugh Dickins *nr_scanned = scan; 1071a8a94d15SMel Gorman 1072fe2c2a10SRik van Riel trace_mm_vmscan_lru_isolate(sc->order, 1073a8a94d15SMel Gorman nr_to_scan, scan, 1074a8a94d15SMel Gorman nr_taken, 1075ea4d349fSTao Ma mode, file); 10761da177e4SLinus Torvalds return nr_taken; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds 107962695a84SNick Piggin /** 108062695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 108162695a84SNick Piggin * @page: page to isolate from its LRU list 108262695a84SNick Piggin * 108362695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 108462695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 108562695a84SNick Piggin * 108662695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 108762695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 108862695a84SNick Piggin * 108962695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1090894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1091894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1092894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 109362695a84SNick Piggin * 109462695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 109562695a84SNick Piggin * found will be decremented. 109662695a84SNick Piggin * 109762695a84SNick Piggin * Restrictions: 109862695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 109962695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 110062695a84SNick Piggin * without a stable reference). 110162695a84SNick Piggin * (2) the lru_lock must not be held. 110262695a84SNick Piggin * (3) interrupts must be enabled. 110362695a84SNick Piggin */ 110462695a84SNick Piggin int isolate_lru_page(struct page *page) 110562695a84SNick Piggin { 110662695a84SNick Piggin int ret = -EBUSY; 110762695a84SNick Piggin 11080c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 11090c917313SKonstantin Khlebnikov 111062695a84SNick Piggin if (PageLRU(page)) { 111162695a84SNick Piggin struct zone *zone = page_zone(page); 111262695a84SNick Piggin 111362695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 11140c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1115894bc310SLee Schermerhorn int lru = page_lru(page); 111662695a84SNick Piggin ret = 0; 11170c917313SKonstantin Khlebnikov get_page(page); 111862695a84SNick Piggin ClearPageLRU(page); 11194f98a2feSRik van Riel 11204f98a2feSRik van Riel del_page_from_lru_list(zone, page, lru); 112162695a84SNick Piggin } 112262695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 112362695a84SNick Piggin } 112462695a84SNick Piggin return ret; 112562695a84SNick Piggin } 112662695a84SNick Piggin 11275ad333ebSAndy Whitcroft /* 112835cd7815SRik van Riel * Are there way too many processes in the direct reclaim path already? 112935cd7815SRik van Riel */ 113035cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 113135cd7815SRik van Riel struct scan_control *sc) 113235cd7815SRik van Riel { 113335cd7815SRik van Riel unsigned long inactive, isolated; 113435cd7815SRik van Riel 113535cd7815SRik van Riel if (current_is_kswapd()) 113635cd7815SRik van Riel return 0; 113735cd7815SRik van Riel 113889b5fae5SJohannes Weiner if (!global_reclaim(sc)) 113935cd7815SRik van Riel return 0; 114035cd7815SRik van Riel 114135cd7815SRik van Riel if (file) { 114235cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 114335cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 114435cd7815SRik van Riel } else { 114535cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 114635cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 114735cd7815SRik van Riel } 114835cd7815SRik van Riel 114935cd7815SRik van Riel return isolated > inactive; 115035cd7815SRik van Riel } 115135cd7815SRik van Riel 115266635629SMel Gorman static noinline_for_stack void 115327ac81d8SKonstantin Khlebnikov putback_inactive_pages(struct lruvec *lruvec, 115466635629SMel Gorman struct list_head *page_list) 115566635629SMel Gorman { 115627ac81d8SKonstantin Khlebnikov struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat; 115727ac81d8SKonstantin Khlebnikov struct zone *zone = lruvec_zone(lruvec); 11583f79768fSHugh Dickins LIST_HEAD(pages_to_free); 115966635629SMel Gorman 116066635629SMel Gorman /* 116166635629SMel Gorman * Put back any unfreeable pages. 116266635629SMel Gorman */ 116366635629SMel Gorman while (!list_empty(page_list)) { 11643f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 116566635629SMel Gorman int lru; 11663f79768fSHugh Dickins 116766635629SMel Gorman VM_BUG_ON(PageLRU(page)); 116866635629SMel Gorman list_del(&page->lru); 116966635629SMel Gorman if (unlikely(!page_evictable(page, NULL))) { 117066635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 117166635629SMel Gorman putback_lru_page(page); 117266635629SMel Gorman spin_lock_irq(&zone->lru_lock); 117366635629SMel Gorman continue; 117466635629SMel Gorman } 11757a608572SLinus Torvalds SetPageLRU(page); 117666635629SMel Gorman lru = page_lru(page); 11777a608572SLinus Torvalds add_page_to_lru_list(zone, page, lru); 117866635629SMel Gorman if (is_active_lru(lru)) { 117966635629SMel Gorman int file = is_file_lru(lru); 11809992af10SRik van Riel int numpages = hpage_nr_pages(page); 11819992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 118266635629SMel Gorman } 11832bcf8879SHugh Dickins if (put_page_testzero(page)) { 11842bcf8879SHugh Dickins __ClearPageLRU(page); 11852bcf8879SHugh Dickins __ClearPageActive(page); 11862bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 11872bcf8879SHugh Dickins 11882bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 118966635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 11902bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 119166635629SMel Gorman spin_lock_irq(&zone->lru_lock); 11922bcf8879SHugh Dickins } else 11932bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 119466635629SMel Gorman } 119566635629SMel Gorman } 119666635629SMel Gorman 11973f79768fSHugh Dickins /* 11983f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 11993f79768fSHugh Dickins */ 12003f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 120166635629SMel Gorman } 120266635629SMel Gorman 120366635629SMel Gorman /* 12041742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 12051742f19fSAndrew Morton * of reclaimed pages 12061da177e4SLinus Torvalds */ 120766635629SMel Gorman static noinline_for_stack unsigned long 1208f16015fbSJohannes Weiner shrink_inactive_list(unsigned long nr_to_scan, struct mem_cgroup_zone *mz, 12099e3b2f8cSKonstantin Khlebnikov struct scan_control *sc, enum lru_list lru) 12101da177e4SLinus Torvalds { 12111da177e4SLinus Torvalds LIST_HEAD(page_list); 1212e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 121305ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1214e247dbceSKOSAKI Motohiro unsigned long nr_taken; 121592df3a72SMel Gorman unsigned long nr_dirty = 0; 121692df3a72SMel Gorman unsigned long nr_writeback = 0; 1217f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 12183cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1219f16015fbSJohannes Weiner struct zone *zone = mz->zone; 1220d563c050SHillf Danton struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 12215dc35979SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, mz->mem_cgroup); 122278dc583dSKOSAKI Motohiro 122335cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 122458355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 122535cd7815SRik van Riel 122635cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 122735cd7815SRik van Riel if (fatal_signal_pending(current)) 122835cd7815SRik van Riel return SWAP_CLUSTER_MAX; 122935cd7815SRik van Riel } 123035cd7815SRik van Riel 12311da177e4SLinus Torvalds lru_add_drain(); 1232f80c0673SMinchan Kim 1233f80c0673SMinchan Kim if (!sc->may_unmap) 123461317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1235f80c0673SMinchan Kim if (!sc->may_writepage) 123661317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1237f80c0673SMinchan Kim 12381da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12391da177e4SLinus Torvalds 12405dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list, 12415dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 124295d918fcSKonstantin Khlebnikov 124395d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 124495d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 124595d918fcSKonstantin Khlebnikov 124689b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1247e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1248b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1249b35ea17bSKOSAKI Motohiro __count_zone_vm_events(PGSCAN_KSWAPD, zone, 1250e247dbceSKOSAKI Motohiro nr_scanned); 1251b35ea17bSKOSAKI Motohiro else 1252b35ea17bSKOSAKI Motohiro __count_zone_vm_events(PGSCAN_DIRECT, zone, 1253e247dbceSKOSAKI Motohiro nr_scanned); 1254b35ea17bSKOSAKI Motohiro } 125566635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1256d563c050SHillf Danton 1257d563c050SHillf Danton if (nr_taken == 0) 125866635629SMel Gorman return 0; 1259b35ea17bSKOSAKI Motohiro 12606a18adb3SKonstantin Khlebnikov nr_reclaimed = shrink_page_list(&page_list, zone, sc, 126192df3a72SMel Gorman &nr_dirty, &nr_writeback); 1262c661b078SAndy Whitcroft 12633f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 12643f79768fSHugh Dickins 126595d918fcSKonstantin Khlebnikov reclaim_stat->recent_scanned[file] += nr_taken; 1266d563c050SHillf Danton 1267904249aaSYing Han if (global_reclaim(sc)) { 1268b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1269904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1270904249aaSYing Han nr_reclaimed); 1271904249aaSYing Han else 1272904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1273904249aaSYing Han nr_reclaimed); 1274904249aaSYing Han } 1275a74609faSNick Piggin 127627ac81d8SKonstantin Khlebnikov putback_inactive_pages(lruvec, &page_list); 12773f79768fSHugh Dickins 127895d918fcSKonstantin Khlebnikov __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 12793f79768fSHugh Dickins 12803f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 12813f79768fSHugh Dickins 12823f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1283e11da5b4SMel Gorman 128492df3a72SMel Gorman /* 128592df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 128692df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 128792df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 128892df3a72SMel Gorman * at throttling processes due to the page distribution throughout 128992df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 129092df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 129192df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 129292df3a72SMel Gorman * same way balance_dirty_pages() manages. 129392df3a72SMel Gorman * 129492df3a72SMel Gorman * This scales the number of dirty pages that must be under writeback 129592df3a72SMel Gorman * before throttling depending on priority. It is a simple backoff 129692df3a72SMel Gorman * function that has the most effect in the range DEF_PRIORITY to 129792df3a72SMel Gorman * DEF_PRIORITY-2 which is the priority reclaim is considered to be 129892df3a72SMel Gorman * in trouble and reclaim is considered to be in trouble. 129992df3a72SMel Gorman * 130092df3a72SMel Gorman * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle 130192df3a72SMel Gorman * DEF_PRIORITY-1 50% must be PageWriteback 130292df3a72SMel Gorman * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble 130392df3a72SMel Gorman * ... 130492df3a72SMel Gorman * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any 130592df3a72SMel Gorman * isolated page is PageWriteback 130692df3a72SMel Gorman */ 13079e3b2f8cSKonstantin Khlebnikov if (nr_writeback && nr_writeback >= 13089e3b2f8cSKonstantin Khlebnikov (nr_taken >> (DEF_PRIORITY - sc->priority))) 130992df3a72SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 131092df3a72SMel Gorman 1311e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1312e11da5b4SMel Gorman zone_idx(zone), 1313e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 13149e3b2f8cSKonstantin Khlebnikov sc->priority, 131523b9da55SMel Gorman trace_shrink_flags(file)); 131605ff5137SAndrew Morton return nr_reclaimed; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13193bb1a852SMartin Bligh /* 13201cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 13211cfb419bSKAMEZAWA Hiroyuki * 13221cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 13231cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 13241cfb419bSKAMEZAWA Hiroyuki * 13251cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 13261cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 13271cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 13281cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 13291cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 13301cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 13311cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 13321cfb419bSKAMEZAWA Hiroyuki * 13331cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 13341cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 13351cfb419bSKAMEZAWA Hiroyuki */ 13361cfb419bSKAMEZAWA Hiroyuki 13373eb4140fSWu Fengguang static void move_active_pages_to_lru(struct zone *zone, 13383eb4140fSWu Fengguang struct list_head *list, 13392bcf8879SHugh Dickins struct list_head *pages_to_free, 13403eb4140fSWu Fengguang enum lru_list lru) 13413eb4140fSWu Fengguang { 13423eb4140fSWu Fengguang unsigned long pgmoved = 0; 13433eb4140fSWu Fengguang struct page *page; 13443eb4140fSWu Fengguang 13453eb4140fSWu Fengguang while (!list_empty(list)) { 1346925b7673SJohannes Weiner struct lruvec *lruvec; 1347925b7673SJohannes Weiner 13483eb4140fSWu Fengguang page = lru_to_page(list); 13493eb4140fSWu Fengguang 13503eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 13513eb4140fSWu Fengguang SetPageLRU(page); 13523eb4140fSWu Fengguang 1353925b7673SJohannes Weiner lruvec = mem_cgroup_lru_add_list(zone, page, lru); 1354925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 13552c888cfbSRik van Riel pgmoved += hpage_nr_pages(page); 13563eb4140fSWu Fengguang 13572bcf8879SHugh Dickins if (put_page_testzero(page)) { 13582bcf8879SHugh Dickins __ClearPageLRU(page); 13592bcf8879SHugh Dickins __ClearPageActive(page); 13602bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 13612bcf8879SHugh Dickins 13622bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 13633eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 13642bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 13653eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 13662bcf8879SHugh Dickins } else 13672bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 13683eb4140fSWu Fengguang } 13693eb4140fSWu Fengguang } 13703eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 13713eb4140fSWu Fengguang if (!is_active_lru(lru)) 13723eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 13733eb4140fSWu Fengguang } 13741cfb419bSKAMEZAWA Hiroyuki 1375f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 1376f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 1377f16015fbSJohannes Weiner struct scan_control *sc, 13789e3b2f8cSKonstantin Khlebnikov enum lru_list lru) 13791cfb419bSKAMEZAWA Hiroyuki { 138044c241f1SKOSAKI Motohiro unsigned long nr_taken; 1381f626012dSHugh Dickins unsigned long nr_scanned; 13826fe6b7e3SWu Fengguang unsigned long vm_flags; 13831cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 13848cab4754SWu Fengguang LIST_HEAD(l_active); 1385b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 13861cfb419bSKAMEZAWA Hiroyuki struct page *page; 1387f16015fbSJohannes Weiner struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 138844c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 1389f3fd4a61SKonstantin Khlebnikov isolate_mode_t isolate_mode = 0; 13903cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1391f16015fbSJohannes Weiner struct zone *zone = mz->zone; 13925dc35979SKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, mz->mem_cgroup); 13931cfb419bSKAMEZAWA Hiroyuki 13941da177e4SLinus Torvalds lru_add_drain(); 1395f80c0673SMinchan Kim 1396f80c0673SMinchan Kim if (!sc->may_unmap) 139761317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1398f80c0673SMinchan Kim if (!sc->may_writepage) 139961317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1400f80c0673SMinchan Kim 14011da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1402925b7673SJohannes Weiner 14035dc35979SKonstantin Khlebnikov nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold, 14045dc35979SKonstantin Khlebnikov &nr_scanned, sc, isolate_mode, lru); 140589b5fae5SJohannes Weiner if (global_reclaim(sc)) 1406f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 140789b5fae5SJohannes Weiner 1408b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 14091cfb419bSKAMEZAWA Hiroyuki 1410f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 14113cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1412a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 14131da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 14161da177e4SLinus Torvalds cond_resched(); 14171da177e4SLinus Torvalds page = lru_to_page(&l_hold); 14181da177e4SLinus Torvalds list_del(&page->lru); 14197e9cd484SRik van Riel 1420894bc310SLee Schermerhorn if (unlikely(!page_evictable(page, NULL))) { 1421894bc310SLee Schermerhorn putback_lru_page(page); 1422894bc310SLee Schermerhorn continue; 1423894bc310SLee Schermerhorn } 1424894bc310SLee Schermerhorn 1425cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1426cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1427cc715d99SMel Gorman if (page_has_private(page)) 1428cc715d99SMel Gorman try_to_release_page(page, 0); 1429cc715d99SMel Gorman unlock_page(page); 1430cc715d99SMel Gorman } 1431cc715d99SMel Gorman } 1432cc715d99SMel Gorman 1433c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1434c3ac9a8aSJohannes Weiner &vm_flags)) { 14359992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 14368cab4754SWu Fengguang /* 14378cab4754SWu Fengguang * Identify referenced, file-backed active pages and 14388cab4754SWu Fengguang * give them one more trip around the active list. So 14398cab4754SWu Fengguang * that executable code get better chances to stay in 14408cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 14418cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 14428cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 14438cab4754SWu Fengguang * so we ignore them here. 14448cab4754SWu Fengguang */ 144541e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 14468cab4754SWu Fengguang list_add(&page->lru, &l_active); 14478cab4754SWu Fengguang continue; 14488cab4754SWu Fengguang } 14498cab4754SWu Fengguang } 14507e9cd484SRik van Riel 14515205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 14521da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds 1455b555749aSAndrew Morton /* 14568cab4754SWu Fengguang * Move pages back to the lru list. 1457b555749aSAndrew Morton */ 14582a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 14594f98a2feSRik van Riel /* 14608cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 14618cab4754SWu Fengguang * even though only some of them are actually re-activated. This 14628cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 14638cab4754SWu Fengguang * get_scan_ratio. 1464556adecbSRik van Riel */ 1465b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1466556adecbSRik van Riel 14673cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_active, &l_hold, lru); 14683cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1469a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1470f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 14712bcf8879SHugh Dickins 14722bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds 147574e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 147614797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1477f89eb90eSKOSAKI Motohiro { 1478f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1479f89eb90eSKOSAKI Motohiro 1480f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1481f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1482f89eb90eSKOSAKI Motohiro 1483f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1484f89eb90eSKOSAKI Motohiro return 1; 1485f89eb90eSKOSAKI Motohiro 1486f89eb90eSKOSAKI Motohiro return 0; 1487f89eb90eSKOSAKI Motohiro } 1488f89eb90eSKOSAKI Motohiro 148914797e23SKOSAKI Motohiro /** 149014797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 1491*c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 149214797e23SKOSAKI Motohiro * 149314797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 149414797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 149514797e23SKOSAKI Motohiro */ 1496*c56d5c7dSKonstantin Khlebnikov static int inactive_anon_is_low(struct lruvec *lruvec) 149714797e23SKOSAKI Motohiro { 149874e3f3c3SMinchan Kim /* 149974e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 150074e3f3c3SMinchan Kim * is pointless. 150174e3f3c3SMinchan Kim */ 150274e3f3c3SMinchan Kim if (!total_swap_pages) 150374e3f3c3SMinchan Kim return 0; 150474e3f3c3SMinchan Kim 1505c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1506*c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_anon_is_low(lruvec); 1507f16015fbSJohannes Weiner 1508*c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low_global(lruvec_zone(lruvec)); 150914797e23SKOSAKI Motohiro } 151074e3f3c3SMinchan Kim #else 1511*c56d5c7dSKonstantin Khlebnikov static inline int inactive_anon_is_low(struct lruvec *lruvec) 151274e3f3c3SMinchan Kim { 151374e3f3c3SMinchan Kim return 0; 151474e3f3c3SMinchan Kim } 151574e3f3c3SMinchan Kim #endif 151614797e23SKOSAKI Motohiro 151756e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone) 151856e49d21SRik van Riel { 151956e49d21SRik van Riel unsigned long active, inactive; 152056e49d21SRik van Riel 152156e49d21SRik van Riel active = zone_page_state(zone, NR_ACTIVE_FILE); 152256e49d21SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 152356e49d21SRik van Riel 152456e49d21SRik van Riel return (active > inactive); 152556e49d21SRik van Riel } 152656e49d21SRik van Riel 152756e49d21SRik van Riel /** 152856e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1529*c56d5c7dSKonstantin Khlebnikov * @lruvec: LRU vector to check 153056e49d21SRik van Riel * 153156e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 153256e49d21SRik van Riel * ensures that active file pages get deactivated, until more 153356e49d21SRik van Riel * than half of the file pages are on the inactive list. 153456e49d21SRik van Riel * 153556e49d21SRik van Riel * Once we get to that situation, protect the system's working 153656e49d21SRik van Riel * set from being evicted by disabling active file page aging. 153756e49d21SRik van Riel * 153856e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 153956e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 154056e49d21SRik van Riel */ 1541*c56d5c7dSKonstantin Khlebnikov static int inactive_file_is_low(struct lruvec *lruvec) 154256e49d21SRik van Riel { 1543c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1544*c56d5c7dSKonstantin Khlebnikov return mem_cgroup_inactive_file_is_low(lruvec); 154556e49d21SRik van Riel 1546*c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low_global(lruvec_zone(lruvec)); 154756e49d21SRik van Riel } 154856e49d21SRik van Riel 1549*c56d5c7dSKonstantin Khlebnikov static int inactive_list_is_low(struct lruvec *lruvec, int file) 1550b39415b2SRik van Riel { 1551b39415b2SRik van Riel if (file) 1552*c56d5c7dSKonstantin Khlebnikov return inactive_file_is_low(lruvec); 1553b39415b2SRik van Riel else 1554*c56d5c7dSKonstantin Khlebnikov return inactive_anon_is_low(lruvec); 1555b39415b2SRik van Riel } 1556b39415b2SRik van Riel 15574f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 1558f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 15599e3b2f8cSKonstantin Khlebnikov struct scan_control *sc) 1560b69408e8SChristoph Lameter { 15614f98a2feSRik van Riel int file = is_file_lru(lru); 15624f98a2feSRik van Riel 1563b39415b2SRik van Riel if (is_active_lru(lru)) { 1564*c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(mz->zone, 1565*c56d5c7dSKonstantin Khlebnikov mz->mem_cgroup); 1566*c56d5c7dSKonstantin Khlebnikov 1567*c56d5c7dSKonstantin Khlebnikov if (inactive_list_is_low(lruvec, file)) 15689e3b2f8cSKonstantin Khlebnikov shrink_active_list(nr_to_scan, mz, sc, lru); 1569556adecbSRik van Riel return 0; 1570556adecbSRik van Riel } 1571556adecbSRik van Riel 15729e3b2f8cSKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, mz, sc, lru); 1573b69408e8SChristoph Lameter } 1574b69408e8SChristoph Lameter 15753d58ab5cSKonstantin Khlebnikov static int vmscan_swappiness(struct scan_control *sc) 15761f4c025bSKAMEZAWA Hiroyuki { 157789b5fae5SJohannes Weiner if (global_reclaim(sc)) 15781f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 15793d58ab5cSKonstantin Khlebnikov return mem_cgroup_swappiness(sc->target_mem_cgroup); 15801f4c025bSKAMEZAWA Hiroyuki } 15811f4c025bSKAMEZAWA Hiroyuki 15821da177e4SLinus Torvalds /* 15834f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 15844f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 15854f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 15864f98a2feSRik van Riel * onto the active list instead of evict. 15874f98a2feSRik van Riel * 158876a33fc3SShaohua Li * nr[0] = anon pages to scan; nr[1] = file pages to scan 15894f98a2feSRik van Riel */ 1590f16015fbSJohannes Weiner static void get_scan_count(struct mem_cgroup_zone *mz, struct scan_control *sc, 15919e3b2f8cSKonstantin Khlebnikov unsigned long *nr) 15924f98a2feSRik van Riel { 15934f98a2feSRik van Riel unsigned long anon, file, free; 15944f98a2feSRik van Riel unsigned long anon_prio, file_prio; 15954f98a2feSRik van Riel unsigned long ap, fp; 1596f16015fbSJohannes Weiner struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 159776a33fc3SShaohua Li u64 fraction[2], denominator; 15984111304dSHugh Dickins enum lru_list lru; 159976a33fc3SShaohua Li int noswap = 0; 1600a4d3e9e7SJohannes Weiner bool force_scan = false; 1601074291feSKonstantin Khlebnikov struct lruvec *lruvec; 1602074291feSKonstantin Khlebnikov 1603074291feSKonstantin Khlebnikov lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup); 1604246e87a9SKAMEZAWA Hiroyuki 1605f11c0ca5SJohannes Weiner /* 1606f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1607f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1608f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1609f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1610f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1611f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1612f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1613f11c0ca5SJohannes Weiner * well. 1614f11c0ca5SJohannes Weiner */ 1615b95a2f2dSJohannes Weiner if (current_is_kswapd() && mz->zone->all_unreclaimable) 1616a4d3e9e7SJohannes Weiner force_scan = true; 161789b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1618a4d3e9e7SJohannes Weiner force_scan = true; 161976a33fc3SShaohua Li 162076a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 162176a33fc3SShaohua Li if (!sc->may_swap || (nr_swap_pages <= 0)) { 162276a33fc3SShaohua Li noswap = 1; 162376a33fc3SShaohua Li fraction[0] = 0; 162476a33fc3SShaohua Li fraction[1] = 1; 162576a33fc3SShaohua Li denominator = 1; 162676a33fc3SShaohua Li goto out; 162776a33fc3SShaohua Li } 16284f98a2feSRik van Riel 1629074291feSKonstantin Khlebnikov anon = get_lruvec_size(lruvec, LRU_ACTIVE_ANON) + 1630074291feSKonstantin Khlebnikov get_lruvec_size(lruvec, LRU_INACTIVE_ANON); 1631074291feSKonstantin Khlebnikov file = get_lruvec_size(lruvec, LRU_ACTIVE_FILE) + 1632074291feSKonstantin Khlebnikov get_lruvec_size(lruvec, LRU_INACTIVE_FILE); 1633a4d3e9e7SJohannes Weiner 163489b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1635f16015fbSJohannes Weiner free = zone_page_state(mz->zone, NR_FREE_PAGES); 1636eeee9a8cSKOSAKI Motohiro /* If we have very few page cache pages, 1637eeee9a8cSKOSAKI Motohiro force-scan anon pages. */ 1638f16015fbSJohannes Weiner if (unlikely(file + free <= high_wmark_pages(mz->zone))) { 163976a33fc3SShaohua Li fraction[0] = 1; 164076a33fc3SShaohua Li fraction[1] = 0; 164176a33fc3SShaohua Li denominator = 1; 164276a33fc3SShaohua Li goto out; 16434f98a2feSRik van Riel } 1644eeee9a8cSKOSAKI Motohiro } 16454f98a2feSRik van Riel 16464f98a2feSRik van Riel /* 164758c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 164858c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 164958c37f6eSKOSAKI Motohiro */ 16503d58ab5cSKonstantin Khlebnikov anon_prio = vmscan_swappiness(sc); 16513d58ab5cSKonstantin Khlebnikov file_prio = 200 - vmscan_swappiness(sc); 165258c37f6eSKOSAKI Motohiro 165358c37f6eSKOSAKI Motohiro /* 16544f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 16554f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 16564f98a2feSRik van Riel * ratios to determine how valuable each cache is. 16574f98a2feSRik van Riel * 16584f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 16594f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 16604f98a2feSRik van Riel * up weighing recent references more than old ones. 16614f98a2feSRik van Riel * 16624f98a2feSRik van Riel * anon in [0], file in [1] 16634f98a2feSRik van Riel */ 1664f16015fbSJohannes Weiner spin_lock_irq(&mz->zone->lru_lock); 166558c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 16666e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 16676e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 16684f98a2feSRik van Riel } 16694f98a2feSRik van Riel 16706e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 16716e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 16726e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 16734f98a2feSRik van Riel } 16744f98a2feSRik van Riel 16754f98a2feSRik van Riel /* 167600d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 167700d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 167800d8089cSRik van Riel * each list that were recently referenced and in active use. 16794f98a2feSRik van Riel */ 1680fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 16816e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 16824f98a2feSRik van Riel 1683fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 16846e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 1685f16015fbSJohannes Weiner spin_unlock_irq(&mz->zone->lru_lock); 16864f98a2feSRik van Riel 168776a33fc3SShaohua Li fraction[0] = ap; 168876a33fc3SShaohua Li fraction[1] = fp; 168976a33fc3SShaohua Li denominator = ap + fp + 1; 169076a33fc3SShaohua Li out: 16914111304dSHugh Dickins for_each_evictable_lru(lru) { 16924111304dSHugh Dickins int file = is_file_lru(lru); 169376a33fc3SShaohua Li unsigned long scan; 169476a33fc3SShaohua Li 1695074291feSKonstantin Khlebnikov scan = get_lruvec_size(lruvec, lru); 16969e3b2f8cSKonstantin Khlebnikov if (sc->priority || noswap || !vmscan_swappiness(sc)) { 16979e3b2f8cSKonstantin Khlebnikov scan >>= sc->priority; 1698f11c0ca5SJohannes Weiner if (!scan && force_scan) 1699f11c0ca5SJohannes Weiner scan = SWAP_CLUSTER_MAX; 170076a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 17014f98a2feSRik van Riel } 17024111304dSHugh Dickins nr[lru] = scan; 170376a33fc3SShaohua Li } 17046e08a369SWu Fengguang } 17054f98a2feSRik van Riel 170623b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 17079e3b2f8cSKonstantin Khlebnikov static bool in_reclaim_compaction(struct scan_control *sc) 170823b9da55SMel Gorman { 170923b9da55SMel Gorman if (COMPACTION_BUILD && sc->order && 171023b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 17119e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2)) 171223b9da55SMel Gorman return true; 171323b9da55SMel Gorman 171423b9da55SMel Gorman return false; 171523b9da55SMel Gorman } 171623b9da55SMel Gorman 17174f98a2feSRik van Riel /* 171823b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 171923b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 172023b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 172123b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 172223b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 17233e7d3449SMel Gorman */ 1724f16015fbSJohannes Weiner static inline bool should_continue_reclaim(struct mem_cgroup_zone *mz, 17253e7d3449SMel Gorman unsigned long nr_reclaimed, 17263e7d3449SMel Gorman unsigned long nr_scanned, 17273e7d3449SMel Gorman struct scan_control *sc) 17283e7d3449SMel Gorman { 17293e7d3449SMel Gorman unsigned long pages_for_compaction; 17303e7d3449SMel Gorman unsigned long inactive_lru_pages; 1731074291feSKonstantin Khlebnikov struct lruvec *lruvec; 17323e7d3449SMel Gorman 17333e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 17349e3b2f8cSKonstantin Khlebnikov if (!in_reclaim_compaction(sc)) 17353e7d3449SMel Gorman return false; 17363e7d3449SMel Gorman 17372876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 17382876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 17393e7d3449SMel Gorman /* 17402876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 17412876592fSMel Gorman * full LRU list has been scanned and we are still failing 17422876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 17432876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 17443e7d3449SMel Gorman */ 17453e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 17463e7d3449SMel Gorman return false; 17472876592fSMel Gorman } else { 17482876592fSMel Gorman /* 17492876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 17502876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 17512876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 17522876592fSMel Gorman * pages that were scanned. This will return to the 17532876592fSMel Gorman * caller faster at the risk reclaim/compaction and 17542876592fSMel Gorman * the resulting allocation attempt fails 17552876592fSMel Gorman */ 17562876592fSMel Gorman if (!nr_reclaimed) 17572876592fSMel Gorman return false; 17582876592fSMel Gorman } 17593e7d3449SMel Gorman 17603e7d3449SMel Gorman /* 17613e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 17623e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 17633e7d3449SMel Gorman */ 1764074291feSKonstantin Khlebnikov lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup); 17653e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 1766074291feSKonstantin Khlebnikov inactive_lru_pages = get_lruvec_size(lruvec, LRU_INACTIVE_FILE); 176786cfd3a4SMinchan Kim if (nr_swap_pages > 0) 1768074291feSKonstantin Khlebnikov inactive_lru_pages += get_lruvec_size(lruvec, 1769074291feSKonstantin Khlebnikov LRU_INACTIVE_ANON); 17703e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 17713e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 17723e7d3449SMel Gorman return true; 17733e7d3449SMel Gorman 17743e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 1775f16015fbSJohannes Weiner switch (compaction_suitable(mz->zone, sc->order)) { 17763e7d3449SMel Gorman case COMPACT_PARTIAL: 17773e7d3449SMel Gorman case COMPACT_CONTINUE: 17783e7d3449SMel Gorman return false; 17793e7d3449SMel Gorman default: 17803e7d3449SMel Gorman return true; 17813e7d3449SMel Gorman } 17823e7d3449SMel Gorman } 17833e7d3449SMel Gorman 17843e7d3449SMel Gorman /* 17851da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 17861da177e4SLinus Torvalds */ 17879e3b2f8cSKonstantin Khlebnikov static void shrink_mem_cgroup_zone(struct mem_cgroup_zone *mz, 178869e05944SAndrew Morton struct scan_control *sc) 17891da177e4SLinus Torvalds { 1790b69408e8SChristoph Lameter unsigned long nr[NR_LRU_LISTS]; 17918695949aSChristoph Lameter unsigned long nr_to_scan; 17924111304dSHugh Dickins enum lru_list lru; 1793f0fdc5e8SJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 179422fba335SKOSAKI Motohiro unsigned long nr_to_reclaim = sc->nr_to_reclaim; 17953da367c3SShaohua Li struct blk_plug plug; 1796*c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec; 1797*c56d5c7dSKonstantin Khlebnikov 1798*c56d5c7dSKonstantin Khlebnikov lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup); 17991da177e4SLinus Torvalds 18003e7d3449SMel Gorman restart: 18013e7d3449SMel Gorman nr_reclaimed = 0; 1802f0fdc5e8SJohannes Weiner nr_scanned = sc->nr_scanned; 18039e3b2f8cSKonstantin Khlebnikov get_scan_count(mz, sc, nr); 18041cfb419bSKAMEZAWA Hiroyuki 18053da367c3SShaohua Li blk_start_plug(&plug); 1806556adecbSRik van Riel while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 1807556adecbSRik van Riel nr[LRU_INACTIVE_FILE]) { 18084111304dSHugh Dickins for_each_evictable_lru(lru) { 18094111304dSHugh Dickins if (nr[lru]) { 1810ece74b2eSKOSAKI Motohiro nr_to_scan = min_t(unsigned long, 18114111304dSHugh Dickins nr[lru], SWAP_CLUSTER_MAX); 18124111304dSHugh Dickins nr[lru] -= nr_to_scan; 1813b69408e8SChristoph Lameter 18144111304dSHugh Dickins nr_reclaimed += shrink_list(lru, nr_to_scan, 18159e3b2f8cSKonstantin Khlebnikov mz, sc); 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds } 1818a79311c1SRik van Riel /* 1819a79311c1SRik van Riel * On large memory systems, scan >> priority can become 1820a79311c1SRik van Riel * really large. This is fine for the starting priority; 1821a79311c1SRik van Riel * we want to put equal scanning pressure on each zone. 1822a79311c1SRik van Riel * However, if the VM has a harder time of freeing pages, 1823a79311c1SRik van Riel * with multiple processes reclaiming pages, the total 1824a79311c1SRik van Riel * freeing target can get unreasonably large. 1825a79311c1SRik van Riel */ 18269e3b2f8cSKonstantin Khlebnikov if (nr_reclaimed >= nr_to_reclaim && 18279e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY) 1828a79311c1SRik van Riel break; 18291da177e4SLinus Torvalds } 18303da367c3SShaohua Li blk_finish_plug(&plug); 18313e7d3449SMel Gorman sc->nr_reclaimed += nr_reclaimed; 183201dbe5c9SKOSAKI Motohiro 1833556adecbSRik van Riel /* 1834556adecbSRik van Riel * Even if we did not try to evict anon pages at all, we want to 1835556adecbSRik van Riel * rebalance the anon lru active/inactive ratio. 1836556adecbSRik van Riel */ 1837*c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 18383cb99451SKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, mz, 18399e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 1840556adecbSRik van Riel 18413e7d3449SMel Gorman /* reclaim/compaction might need reclaim to continue */ 1842f16015fbSJohannes Weiner if (should_continue_reclaim(mz, nr_reclaimed, 18439e3b2f8cSKonstantin Khlebnikov sc->nr_scanned - nr_scanned, sc)) 18443e7d3449SMel Gorman goto restart; 18453e7d3449SMel Gorman 1846232ea4d6SAndrew Morton throttle_vm_writeout(sc->gfp_mask); 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18499e3b2f8cSKonstantin Khlebnikov static void shrink_zone(struct zone *zone, struct scan_control *sc) 1850f16015fbSJohannes Weiner { 18515660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 18525660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 18535660048cSJohannes Weiner .zone = zone, 18549e3b2f8cSKonstantin Khlebnikov .priority = sc->priority, 18555660048cSJohannes Weiner }; 18565660048cSJohannes Weiner struct mem_cgroup *memcg; 18575660048cSJohannes Weiner 18585660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 18595660048cSJohannes Weiner do { 18605660048cSJohannes Weiner struct mem_cgroup_zone mz = { 18615660048cSJohannes Weiner .mem_cgroup = memcg, 18625660048cSJohannes Weiner .zone = zone, 18635660048cSJohannes Weiner }; 18645660048cSJohannes Weiner 18659e3b2f8cSKonstantin Khlebnikov shrink_mem_cgroup_zone(&mz, sc); 18665660048cSJohannes Weiner /* 18675660048cSJohannes Weiner * Limit reclaim has historically picked one memcg and 18685660048cSJohannes Weiner * scanned it with decreasing priority levels until 18695660048cSJohannes Weiner * nr_to_reclaim had been reclaimed. This priority 18705660048cSJohannes Weiner * cycle is thus over after a single memcg. 1871b95a2f2dSJohannes Weiner * 1872b95a2f2dSJohannes Weiner * Direct reclaim and kswapd, on the other hand, have 1873b95a2f2dSJohannes Weiner * to scan all memory cgroups to fulfill the overall 1874b95a2f2dSJohannes Weiner * scan target for the zone. 18755660048cSJohannes Weiner */ 18765660048cSJohannes Weiner if (!global_reclaim(sc)) { 18775660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 18785660048cSJohannes Weiner break; 18795660048cSJohannes Weiner } 18805660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 18815660048cSJohannes Weiner } while (memcg); 1882f16015fbSJohannes Weiner } 1883f16015fbSJohannes Weiner 1884fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 1885fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 1886fe4b1b24SMel Gorman { 1887fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 1888fe4b1b24SMel Gorman bool watermark_ok; 1889fe4b1b24SMel Gorman 1890fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 1891fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 1892fe4b1b24SMel Gorman return false; 1893fe4b1b24SMel Gorman 1894fe4b1b24SMel Gorman /* 1895fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 1896fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 1897fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 1898fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 1899fe4b1b24SMel Gorman */ 1900fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 1901fe4b1b24SMel Gorman (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 1902fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 1903fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 1904fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 1905fe4b1b24SMel Gorman 1906fe4b1b24SMel Gorman /* 1907fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 1908fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 1909fe4b1b24SMel Gorman */ 1910aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 1911fe4b1b24SMel Gorman return watermark_ok; 1912fe4b1b24SMel Gorman 1913fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 1914fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 1915fe4b1b24SMel Gorman return false; 1916fe4b1b24SMel Gorman 1917fe4b1b24SMel Gorman return watermark_ok; 1918fe4b1b24SMel Gorman } 1919fe4b1b24SMel Gorman 19201da177e4SLinus Torvalds /* 19211da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 19221da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 19231da177e4SLinus Torvalds * request. 19241da177e4SLinus Torvalds * 192541858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 192641858966SMel Gorman * Because: 19271da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 19281da177e4SLinus Torvalds * allocation or 192941858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 193041858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 193141858966SMel Gorman * zone defense algorithm. 19321da177e4SLinus Torvalds * 19331da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 19341da177e4SLinus Torvalds * scan then give up on it. 1935e0c23279SMel Gorman * 1936e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 1937fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 19380cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 19390cee34fdSMel Gorman * further reclaim. 19401da177e4SLinus Torvalds */ 19419e3b2f8cSKonstantin Khlebnikov static bool shrink_zones(struct zonelist *zonelist, struct scan_control *sc) 19421da177e4SLinus Torvalds { 1943dd1a239fSMel Gorman struct zoneref *z; 194454a6eb5cSMel Gorman struct zone *zone; 1945d149e3b2SYing Han unsigned long nr_soft_reclaimed; 1946d149e3b2SYing Han unsigned long nr_soft_scanned; 19470cee34fdSMel Gorman bool aborted_reclaim = false; 19481cfb419bSKAMEZAWA Hiroyuki 1949cc715d99SMel Gorman /* 1950cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 1951cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 1952cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 1953cc715d99SMel Gorman */ 1954cc715d99SMel Gorman if (buffer_heads_over_limit) 1955cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 1956cc715d99SMel Gorman 1957d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 1958d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 1959f3fe6512SCon Kolivas if (!populated_zone(zone)) 19601da177e4SLinus Torvalds continue; 19611cfb419bSKAMEZAWA Hiroyuki /* 19621cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 19631cfb419bSKAMEZAWA Hiroyuki * to global LRU. 19641cfb419bSKAMEZAWA Hiroyuki */ 196589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 196602a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 19671da177e4SLinus Torvalds continue; 19689e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 19699e3b2f8cSKonstantin Khlebnikov sc->priority != DEF_PRIORITY) 19701da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 1971e0887c19SRik van Riel if (COMPACTION_BUILD) { 1972e0887c19SRik van Riel /* 1973e0c23279SMel Gorman * If we already have plenty of memory free for 1974e0c23279SMel Gorman * compaction in this zone, don't free any more. 1975e0c23279SMel Gorman * Even though compaction is invoked for any 1976e0c23279SMel Gorman * non-zero order, only frequent costly order 1977e0c23279SMel Gorman * reclamation is disruptive enough to become a 1978c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 1979c7cfa37bSCopot Alexandru * page allocations. 1980e0887c19SRik van Riel */ 1981fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 19820cee34fdSMel Gorman aborted_reclaim = true; 1983e0887c19SRik van Riel continue; 1984e0887c19SRik van Riel } 1985e0c23279SMel Gorman } 1986ac34a1a3SKAMEZAWA Hiroyuki /* 1987ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 1988ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 1989ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 1990ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 1991ac34a1a3SKAMEZAWA Hiroyuki */ 1992d149e3b2SYing Han nr_soft_scanned = 0; 1993d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 1994d149e3b2SYing Han sc->order, sc->gfp_mask, 1995d149e3b2SYing Han &nr_soft_scanned); 1996d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 1997ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 1998ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 1999ac34a1a3SKAMEZAWA Hiroyuki } 2000d149e3b2SYing Han 20019e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, sc); 20021da177e4SLinus Torvalds } 2003e0c23279SMel Gorman 20040cee34fdSMel Gorman return aborted_reclaim; 2005d1908362SMinchan Kim } 2006d1908362SMinchan Kim 2007d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone) 2008d1908362SMinchan Kim { 2009d1908362SMinchan Kim return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 2010d1908362SMinchan Kim } 2011d1908362SMinchan Kim 2012929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2013d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2014d1908362SMinchan Kim struct scan_control *sc) 2015d1908362SMinchan Kim { 2016d1908362SMinchan Kim struct zoneref *z; 2017d1908362SMinchan Kim struct zone *zone; 2018d1908362SMinchan Kim 2019d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2020d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2021d1908362SMinchan Kim if (!populated_zone(zone)) 2022d1908362SMinchan Kim continue; 2023d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2024d1908362SMinchan Kim continue; 2025929bea7cSKOSAKI Motohiro if (!zone->all_unreclaimable) 2026929bea7cSKOSAKI Motohiro return false; 2027d1908362SMinchan Kim } 2028d1908362SMinchan Kim 2029929bea7cSKOSAKI Motohiro return true; 20301da177e4SLinus Torvalds } 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds /* 20331da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 20341da177e4SLinus Torvalds * 20351da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 20361da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 20371da177e4SLinus Torvalds * 20381da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 20391da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 20405b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 20415b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 20425b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 20435b0830cbSJens Axboe * work, and the allocation attempt will fail. 2044a41f24eaSNishanth Aravamudan * 2045a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2046a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 20471da177e4SLinus Torvalds */ 2048dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2049a09ed5e0SYing Han struct scan_control *sc, 2050a09ed5e0SYing Han struct shrink_control *shrink) 20511da177e4SLinus Torvalds { 205269e05944SAndrew Morton unsigned long total_scanned = 0; 20531da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2054dd1a239fSMel Gorman struct zoneref *z; 205554a6eb5cSMel Gorman struct zone *zone; 205622fba335SKOSAKI Motohiro unsigned long writeback_threshold; 20570cee34fdSMel Gorman bool aborted_reclaim; 20581da177e4SLinus Torvalds 2059873b4771SKeika Kobayashi delayacct_freepages_start(); 2060873b4771SKeika Kobayashi 206189b5fae5SJohannes Weiner if (global_reclaim(sc)) 2062f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 20631da177e4SLinus Torvalds 20649e3b2f8cSKonstantin Khlebnikov do { 206566e1707bSBalbir Singh sc->nr_scanned = 0; 20669e3b2f8cSKonstantin Khlebnikov aborted_reclaim = shrink_zones(zonelist, sc); 2067e0c23279SMel Gorman 206866e1707bSBalbir Singh /* 206966e1707bSBalbir Singh * Don't shrink slabs when reclaiming memory from 207066e1707bSBalbir Singh * over limit cgroups 207166e1707bSBalbir Singh */ 207289b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2073c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2074d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2075d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2076c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2077c6a8a8c5SKOSAKI Motohiro continue; 2078c6a8a8c5SKOSAKI Motohiro 2079c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2080c6a8a8c5SKOSAKI Motohiro } 2081c6a8a8c5SKOSAKI Motohiro 20821495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 20831da177e4SLinus Torvalds if (reclaim_state) { 2084a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 20851da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 20861da177e4SLinus Torvalds } 208791a45470SKAMEZAWA Hiroyuki } 208866e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2089bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 20901da177e4SLinus Torvalds goto out; 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds /* 20931da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 20941da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 20951da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 20961da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 20971da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 20981da177e4SLinus Torvalds */ 209922fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 210022fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 21010e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 21020e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 210366e1707bSBalbir Singh sc->may_writepage = 1; 21041da177e4SLinus Torvalds } 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 21077b51755cSKOSAKI Motohiro if (!sc->hibernation_mode && sc->nr_scanned && 21089e3b2f8cSKonstantin Khlebnikov sc->priority < DEF_PRIORITY - 2) { 21090e093d99SMel Gorman struct zone *preferred_zone; 21100e093d99SMel Gorman 21110e093d99SMel Gorman first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), 2112f33261d7SDavid Rientjes &cpuset_current_mems_allowed, 2113f33261d7SDavid Rientjes &preferred_zone); 21140e093d99SMel Gorman wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); 21150e093d99SMel Gorman } 21169e3b2f8cSKonstantin Khlebnikov } while (--sc->priority >= 0); 2117bb21c7ceSKOSAKI Motohiro 21181da177e4SLinus Torvalds out: 2119873b4771SKeika Kobayashi delayacct_freepages_end(); 2120873b4771SKeika Kobayashi 2121bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2122bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2123bb21c7ceSKOSAKI Motohiro 2124929bea7cSKOSAKI Motohiro /* 2125929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2126929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2127929bea7cSKOSAKI Motohiro * check. 2128929bea7cSKOSAKI Motohiro */ 2129929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2130929bea7cSKOSAKI Motohiro return 0; 2131929bea7cSKOSAKI Motohiro 21320cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 21330cee34fdSMel Gorman if (aborted_reclaim) 21347335084dSMel Gorman return 1; 21357335084dSMel Gorman 2136bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 213789b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2138bb21c7ceSKOSAKI Motohiro return 1; 2139bb21c7ceSKOSAKI Motohiro 2140bb21c7ceSKOSAKI Motohiro return 0; 21411da177e4SLinus Torvalds } 21421da177e4SLinus Torvalds 2143dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2144327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 214566e1707bSBalbir Singh { 214633906bc5SMel Gorman unsigned long nr_reclaimed; 214766e1707bSBalbir Singh struct scan_control sc = { 214866e1707bSBalbir Singh .gfp_mask = gfp_mask, 214966e1707bSBalbir Singh .may_writepage = !laptop_mode, 215022fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2151a6dc60f8SJohannes Weiner .may_unmap = 1, 21522e2e4259SKOSAKI Motohiro .may_swap = 1, 215366e1707bSBalbir Singh .order = order, 21549e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 2155f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2156327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 215766e1707bSBalbir Singh }; 2158a09ed5e0SYing Han struct shrink_control shrink = { 2159a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2160a09ed5e0SYing Han }; 216166e1707bSBalbir Singh 216233906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 216333906bc5SMel Gorman sc.may_writepage, 216433906bc5SMel Gorman gfp_mask); 216533906bc5SMel Gorman 2166a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 216733906bc5SMel Gorman 216833906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 216933906bc5SMel Gorman 217033906bc5SMel Gorman return nr_reclaimed; 217166e1707bSBalbir Singh } 217266e1707bSBalbir Singh 217300f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 217466e1707bSBalbir Singh 217572835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 21764e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 21770ae5e89cSYing Han struct zone *zone, 21780ae5e89cSYing Han unsigned long *nr_scanned) 21794e416953SBalbir Singh { 21804e416953SBalbir Singh struct scan_control sc = { 21810ae5e89cSYing Han .nr_scanned = 0, 2182b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 21834e416953SBalbir Singh .may_writepage = !laptop_mode, 21844e416953SBalbir Singh .may_unmap = 1, 21854e416953SBalbir Singh .may_swap = !noswap, 21864e416953SBalbir Singh .order = 0, 21879e3b2f8cSKonstantin Khlebnikov .priority = 0, 218872835c86SJohannes Weiner .target_mem_cgroup = memcg, 21894e416953SBalbir Singh }; 21905660048cSJohannes Weiner struct mem_cgroup_zone mz = { 219172835c86SJohannes Weiner .mem_cgroup = memcg, 21925660048cSJohannes Weiner .zone = zone, 21935660048cSJohannes Weiner }; 21940ae5e89cSYing Han 21954e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 21964e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2197bdce6d9eSKOSAKI Motohiro 21989e3b2f8cSKonstantin Khlebnikov trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order, 2199bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2200bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2201bdce6d9eSKOSAKI Motohiro 22024e416953SBalbir Singh /* 22034e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 22044e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 22054e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 22064e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 22074e416953SBalbir Singh * the priority and make it zero. 22084e416953SBalbir Singh */ 22099e3b2f8cSKonstantin Khlebnikov shrink_mem_cgroup_zone(&mz, &sc); 2210bdce6d9eSKOSAKI Motohiro 2211bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2212bdce6d9eSKOSAKI Motohiro 22130ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 22144e416953SBalbir Singh return sc.nr_reclaimed; 22154e416953SBalbir Singh } 22164e416953SBalbir Singh 221772835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 22188c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2219185efc0fSJohannes Weiner bool noswap) 222066e1707bSBalbir Singh { 22214e416953SBalbir Singh struct zonelist *zonelist; 2222bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2223889976dbSYing Han int nid; 222466e1707bSBalbir Singh struct scan_control sc = { 222566e1707bSBalbir Singh .may_writepage = !laptop_mode, 2226a6dc60f8SJohannes Weiner .may_unmap = 1, 22272e2e4259SKOSAKI Motohiro .may_swap = !noswap, 222822fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 222966e1707bSBalbir Singh .order = 0, 22309e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 223172835c86SJohannes Weiner .target_mem_cgroup = memcg, 2232327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2233a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2234a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2235a09ed5e0SYing Han }; 2236a09ed5e0SYing Han struct shrink_control shrink = { 2237a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 223866e1707bSBalbir Singh }; 223966e1707bSBalbir Singh 2240889976dbSYing Han /* 2241889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2242889976dbSYing Han * take care of from where we get pages. So the node where we start the 2243889976dbSYing Han * scan does not need to be the current node. 2244889976dbSYing Han */ 224572835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2246889976dbSYing Han 2247889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2248bdce6d9eSKOSAKI Motohiro 2249bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2250bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2251bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2252bdce6d9eSKOSAKI Motohiro 2253a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2254bdce6d9eSKOSAKI Motohiro 2255bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2256bdce6d9eSKOSAKI Motohiro 2257bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 225866e1707bSBalbir Singh } 225966e1707bSBalbir Singh #endif 226066e1707bSBalbir Singh 22619e3b2f8cSKonstantin Khlebnikov static void age_active_anon(struct zone *zone, struct scan_control *sc) 2262f16015fbSJohannes Weiner { 2263b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2264b95a2f2dSJohannes Weiner 2265b95a2f2dSJohannes Weiner if (!total_swap_pages) 2266b95a2f2dSJohannes Weiner return; 2267b95a2f2dSJohannes Weiner 2268b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2269b95a2f2dSJohannes Weiner do { 2270*c56d5c7dSKonstantin Khlebnikov struct lruvec *lruvec = mem_cgroup_zone_lruvec(zone, memcg); 2271f16015fbSJohannes Weiner struct mem_cgroup_zone mz = { 2272b95a2f2dSJohannes Weiner .mem_cgroup = memcg, 2273f16015fbSJohannes Weiner .zone = zone, 2274f16015fbSJohannes Weiner }; 2275f16015fbSJohannes Weiner 2276*c56d5c7dSKonstantin Khlebnikov if (inactive_anon_is_low(lruvec)) 2277b95a2f2dSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, &mz, 22789e3b2f8cSKonstantin Khlebnikov sc, LRU_ACTIVE_ANON); 2279b95a2f2dSJohannes Weiner 2280b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2281b95a2f2dSJohannes Weiner } while (memcg); 2282f16015fbSJohannes Weiner } 2283f16015fbSJohannes Weiner 22841741c877SMel Gorman /* 22851741c877SMel Gorman * pgdat_balanced is used when checking if a node is balanced for high-order 22861741c877SMel Gorman * allocations. Only zones that meet watermarks and are in a zone allowed 22871741c877SMel Gorman * by the callers classzone_idx are added to balanced_pages. The total of 22881741c877SMel Gorman * balanced pages must be at least 25% of the zones allowed by classzone_idx 22891741c877SMel Gorman * for the node to be considered balanced. Forcing all zones to be balanced 22901741c877SMel Gorman * for high orders can cause excessive reclaim when there are imbalanced zones. 22911741c877SMel Gorman * The choice of 25% is due to 22921741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 22931741c877SMel Gorman * reasonable sized machine 22941741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 229525985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 22961741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 22971741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 22981741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 22991741c877SMel Gorman */ 23001741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages, 23011741c877SMel Gorman int classzone_idx) 23021741c877SMel Gorman { 23031741c877SMel Gorman unsigned long present_pages = 0; 23041741c877SMel Gorman int i; 23051741c877SMel Gorman 23061741c877SMel Gorman for (i = 0; i <= classzone_idx; i++) 23071741c877SMel Gorman present_pages += pgdat->node_zones[i].present_pages; 23081741c877SMel Gorman 23094746efdeSShaohua Li /* A special case here: if zone has no page, we think it's balanced */ 23104746efdeSShaohua Li return balanced_pages >= (present_pages >> 2); 23111741c877SMel Gorman } 23121741c877SMel Gorman 2313f50de2d3SMel Gorman /* is kswapd sleeping prematurely? */ 2314dc83edd9SMel Gorman static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining, 2315dc83edd9SMel Gorman int classzone_idx) 2316f50de2d3SMel Gorman { 2317bb3ab596SKOSAKI Motohiro int i; 23181741c877SMel Gorman unsigned long balanced = 0; 23191741c877SMel Gorman bool all_zones_ok = true; 2320f50de2d3SMel Gorman 2321f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2322f50de2d3SMel Gorman if (remaining) 2323dc83edd9SMel Gorman return true; 2324f50de2d3SMel Gorman 23250abdee2bSMel Gorman /* Check the watermark levels */ 232608951e54SMel Gorman for (i = 0; i <= classzone_idx; i++) { 2327bb3ab596SKOSAKI Motohiro struct zone *zone = pgdat->node_zones + i; 2328bb3ab596SKOSAKI Motohiro 2329bb3ab596SKOSAKI Motohiro if (!populated_zone(zone)) 2330bb3ab596SKOSAKI Motohiro continue; 2331bb3ab596SKOSAKI Motohiro 2332355b09c4SMel Gorman /* 2333355b09c4SMel Gorman * balance_pgdat() skips over all_unreclaimable after 2334355b09c4SMel Gorman * DEF_PRIORITY. Effectively, it considers them balanced so 2335355b09c4SMel Gorman * they must be considered balanced here as well if kswapd 2336355b09c4SMel Gorman * is to sleep 2337355b09c4SMel Gorman */ 2338355b09c4SMel Gorman if (zone->all_unreclaimable) { 2339355b09c4SMel Gorman balanced += zone->present_pages; 2340de3fab39SKOSAKI Motohiro continue; 2341355b09c4SMel Gorman } 2342de3fab39SKOSAKI Motohiro 234388f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone), 2344da175d06SMel Gorman i, 0)) 23451741c877SMel Gorman all_zones_ok = false; 23461741c877SMel Gorman else 23471741c877SMel Gorman balanced += zone->present_pages; 2348bb3ab596SKOSAKI Motohiro } 2349f50de2d3SMel Gorman 23501741c877SMel Gorman /* 23511741c877SMel Gorman * For high-order requests, the balanced zones must contain at least 23521741c877SMel Gorman * 25% of the nodes pages for kswapd to sleep. For order-0, all zones 23531741c877SMel Gorman * must be balanced 23541741c877SMel Gorman */ 23551741c877SMel Gorman if (order) 2356afc7e326SJohannes Weiner return !pgdat_balanced(pgdat, balanced, classzone_idx); 23571741c877SMel Gorman else 23581741c877SMel Gorman return !all_zones_ok; 2359f50de2d3SMel Gorman } 2360f50de2d3SMel Gorman 23611da177e4SLinus Torvalds /* 23621da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 236341858966SMel Gorman * they are all at high_wmark_pages(zone). 23641da177e4SLinus Torvalds * 23650abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 23661da177e4SLinus Torvalds * 23671da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 23681da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 23691da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 23701da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 23711da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 23721da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 23731da177e4SLinus Torvalds * the zone for when the problem goes away. 23741da177e4SLinus Torvalds * 23751da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 237641858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 237741858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 237841858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 237941858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 238041858966SMel Gorman * of pages is balanced across the zones. 23811da177e4SLinus Torvalds */ 238299504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2383dc83edd9SMel Gorman int *classzone_idx) 23841da177e4SLinus Torvalds { 23851da177e4SLinus Torvalds int all_zones_ok; 23861741c877SMel Gorman unsigned long balanced; 23871da177e4SLinus Torvalds int i; 238899504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 238969e05944SAndrew Morton unsigned long total_scanned; 23901da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 23910ae5e89cSYing Han unsigned long nr_soft_reclaimed; 23920ae5e89cSYing Han unsigned long nr_soft_scanned; 2393179e9639SAndrew Morton struct scan_control sc = { 2394179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2395a6dc60f8SJohannes Weiner .may_unmap = 1, 23962e2e4259SKOSAKI Motohiro .may_swap = 1, 239722fba335SKOSAKI Motohiro /* 239822fba335SKOSAKI Motohiro * kswapd doesn't want to be bailed out while reclaim. because 239922fba335SKOSAKI Motohiro * we want to put equal scanning pressure on each zone. 240022fba335SKOSAKI Motohiro */ 240122fba335SKOSAKI Motohiro .nr_to_reclaim = ULONG_MAX, 24025ad333ebSAndy Whitcroft .order = order, 2403f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2404179e9639SAndrew Morton }; 2405a09ed5e0SYing Han struct shrink_control shrink = { 2406a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2407a09ed5e0SYing Han }; 24081da177e4SLinus Torvalds loop_again: 24091da177e4SLinus Torvalds total_scanned = 0; 24109e3b2f8cSKonstantin Khlebnikov sc.priority = DEF_PRIORITY; 2411a79311c1SRik van Riel sc.nr_reclaimed = 0; 2412c0bbbc73SChristoph Lameter sc.may_writepage = !laptop_mode; 2413f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 24141da177e4SLinus Torvalds 24159e3b2f8cSKonstantin Khlebnikov do { 24161da177e4SLinus Torvalds unsigned long lru_pages = 0; 2417bb3ab596SKOSAKI Motohiro int has_under_min_watermark_zone = 0; 24181da177e4SLinus Torvalds 24191da177e4SLinus Torvalds all_zones_ok = 1; 24201741c877SMel Gorman balanced = 0; 24211da177e4SLinus Torvalds 24221da177e4SLinus Torvalds /* 24231da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 24241da177e4SLinus Torvalds * zone which needs scanning 24251da177e4SLinus Torvalds */ 24261da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 24271da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 24281da177e4SLinus Torvalds 2429f3fe6512SCon Kolivas if (!populated_zone(zone)) 24301da177e4SLinus Torvalds continue; 24311da177e4SLinus Torvalds 24329e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 24339e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 24341da177e4SLinus Torvalds continue; 24351da177e4SLinus Torvalds 2436556adecbSRik van Riel /* 2437556adecbSRik van Riel * Do some background aging of the anon list, to give 2438556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2439556adecbSRik van Riel */ 24409e3b2f8cSKonstantin Khlebnikov age_active_anon(zone, &sc); 2441556adecbSRik van Riel 2442cc715d99SMel Gorman /* 2443cc715d99SMel Gorman * If the number of buffer_heads in the machine 2444cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2445cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2446cc715d99SMel Gorman * it to relieve lowmem pressure. 2447cc715d99SMel Gorman */ 2448cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2449cc715d99SMel Gorman end_zone = i; 2450cc715d99SMel Gorman break; 2451cc715d99SMel Gorman } 2452cc715d99SMel Gorman 245388f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 245441858966SMel Gorman high_wmark_pages(zone), 0, 0)) { 24551da177e4SLinus Torvalds end_zone = i; 2456e1dbeda6SAndrew Morton break; 2457439423f6SShaohua Li } else { 2458439423f6SShaohua Li /* If balanced, clear the congested flag */ 2459439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 24601da177e4SLinus Torvalds } 24611da177e4SLinus Torvalds } 2462e1dbeda6SAndrew Morton if (i < 0) 24631da177e4SLinus Torvalds goto out; 2464e1dbeda6SAndrew Morton 24651da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 24661da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 24671da177e4SLinus Torvalds 2468adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds /* 24721da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 24731da177e4SLinus Torvalds * at the last zone which needs scanning. 24741da177e4SLinus Torvalds * 24751da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 24761da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 24771da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 24781da177e4SLinus Torvalds * cause too much scanning of the lower zones. 24791da177e4SLinus Torvalds */ 24801da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 24811da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 2482fe2c2a10SRik van Riel int nr_slab, testorder; 24838afdceceSMel Gorman unsigned long balance_gap; 24841da177e4SLinus Torvalds 2485f3fe6512SCon Kolivas if (!populated_zone(zone)) 24861da177e4SLinus Torvalds continue; 24871da177e4SLinus Torvalds 24889e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 24899e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 24901da177e4SLinus Torvalds continue; 24911da177e4SLinus Torvalds 24921da177e4SLinus Torvalds sc.nr_scanned = 0; 24934e416953SBalbir Singh 24940ae5e89cSYing Han nr_soft_scanned = 0; 24954e416953SBalbir Singh /* 24964e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 24974e416953SBalbir Singh */ 24980ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 24990ae5e89cSYing Han order, sc.gfp_mask, 25000ae5e89cSYing Han &nr_soft_scanned); 25010ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 25020ae5e89cSYing Han total_scanned += nr_soft_scanned; 250300918b6aSKOSAKI Motohiro 250432a4330dSRik van Riel /* 25058afdceceSMel Gorman * We put equal pressure on every zone, unless 25068afdceceSMel Gorman * one zone has way too many pages free 25078afdceceSMel Gorman * already. The "too many pages" is defined 25088afdceceSMel Gorman * as the high wmark plus a "gap" where the 25098afdceceSMel Gorman * gap is either the low watermark or 1% 25108afdceceSMel Gorman * of the zone, whichever is smaller. 251132a4330dSRik van Riel */ 25128afdceceSMel Gorman balance_gap = min(low_wmark_pages(zone), 25138afdceceSMel Gorman (zone->present_pages + 25148afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 25158afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2516fe2c2a10SRik van Riel /* 2517fe2c2a10SRik van Riel * Kswapd reclaims only single pages with compaction 2518fe2c2a10SRik van Riel * enabled. Trying too hard to reclaim until contiguous 2519fe2c2a10SRik van Riel * free pages have become available can hurt performance 2520fe2c2a10SRik van Riel * by evicting too much useful data from memory. 2521fe2c2a10SRik van Riel * Do not reclaim more than needed for compaction. 2522fe2c2a10SRik van Riel */ 2523fe2c2a10SRik van Riel testorder = order; 2524fe2c2a10SRik van Riel if (COMPACTION_BUILD && order && 2525fe2c2a10SRik van Riel compaction_suitable(zone, order) != 2526fe2c2a10SRik van Riel COMPACT_SKIPPED) 2527fe2c2a10SRik van Riel testorder = 0; 2528fe2c2a10SRik van Riel 2529cc715d99SMel Gorman if ((buffer_heads_over_limit && is_highmem_idx(i)) || 2530643ac9fcSHugh Dickins !zone_watermark_ok_safe(zone, testorder, 25318afdceceSMel Gorman high_wmark_pages(zone) + balance_gap, 2532d7868daeSMel Gorman end_zone, 0)) { 25339e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 2534d7868daeSMel Gorman 25351da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 25361495f230SYing Han nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages); 2537a79311c1SRik van Riel sc.nr_reclaimed += reclaim_state->reclaimed_slab; 25381da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 25395a03b051SAndrea Arcangeli 2540d7868daeSMel Gorman if (nr_slab == 0 && !zone_reclaimable(zone)) 254193e4a89aSKOSAKI Motohiro zone->all_unreclaimable = 1; 2542d7868daeSMel Gorman } 2543d7868daeSMel Gorman 25441da177e4SLinus Torvalds /* 25451da177e4SLinus Torvalds * If we've done a decent amount of scanning and 25461da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 25471da177e4SLinus Torvalds * even in laptop mode 25481da177e4SLinus Torvalds */ 25491da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 2550a79311c1SRik van Riel total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) 25511da177e4SLinus Torvalds sc.may_writepage = 1; 2552bb3ab596SKOSAKI Motohiro 2553215ddd66SMel Gorman if (zone->all_unreclaimable) { 2554215ddd66SMel Gorman if (end_zone && end_zone == i) 2555215ddd66SMel Gorman end_zone--; 2556d7868daeSMel Gorman continue; 2557215ddd66SMel Gorman } 2558d7868daeSMel Gorman 2559fe2c2a10SRik van Riel if (!zone_watermark_ok_safe(zone, testorder, 256045973d74SMinchan Kim high_wmark_pages(zone), end_zone, 0)) { 256145973d74SMinchan Kim all_zones_ok = 0; 2562bb3ab596SKOSAKI Motohiro /* 256345973d74SMinchan Kim * We are still under min water mark. This 256445973d74SMinchan Kim * means that we have a GFP_ATOMIC allocation 256545973d74SMinchan Kim * failure risk. Hurry up! 2566bb3ab596SKOSAKI Motohiro */ 256788f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 256845973d74SMinchan Kim min_wmark_pages(zone), end_zone, 0)) 2569bb3ab596SKOSAKI Motohiro has_under_min_watermark_zone = 1; 25700e093d99SMel Gorman } else { 25710e093d99SMel Gorman /* 25720e093d99SMel Gorman * If a zone reaches its high watermark, 25730e093d99SMel Gorman * consider it to be no longer congested. It's 25740e093d99SMel Gorman * possible there are dirty pages backed by 25750e093d99SMel Gorman * congested BDIs but as pressure is relieved, 25760e093d99SMel Gorman * spectulatively avoid congestion waits 25770e093d99SMel Gorman */ 25780e093d99SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 2579dc83edd9SMel Gorman if (i <= *classzone_idx) 25801741c877SMel Gorman balanced += zone->present_pages; 258145973d74SMinchan Kim } 2582bb3ab596SKOSAKI Motohiro 25831da177e4SLinus Torvalds } 2584dc83edd9SMel Gorman if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx))) 25851da177e4SLinus Torvalds break; /* kswapd: all done */ 25861da177e4SLinus Torvalds /* 25871da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 25881da177e4SLinus Torvalds * another pass across the zones. 25891da177e4SLinus Torvalds */ 25909e3b2f8cSKonstantin Khlebnikov if (total_scanned && (sc.priority < DEF_PRIORITY - 2)) { 2591bb3ab596SKOSAKI Motohiro if (has_under_min_watermark_zone) 2592bb3ab596SKOSAKI Motohiro count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); 2593bb3ab596SKOSAKI Motohiro else 25948aa7e847SJens Axboe congestion_wait(BLK_RW_ASYNC, HZ/10); 2595bb3ab596SKOSAKI Motohiro } 25961da177e4SLinus Torvalds 25971da177e4SLinus Torvalds /* 25981da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 25991da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 26001da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 26011da177e4SLinus Torvalds * on zone->*_priority. 26021da177e4SLinus Torvalds */ 2603a79311c1SRik van Riel if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) 26041da177e4SLinus Torvalds break; 26059e3b2f8cSKonstantin Khlebnikov } while (--sc.priority >= 0); 26061da177e4SLinus Torvalds out: 260799504748SMel Gorman 260899504748SMel Gorman /* 260999504748SMel Gorman * order-0: All zones must meet high watermark for a balanced node 26101741c877SMel Gorman * high-order: Balanced zones must make up at least 25% of the node 26111741c877SMel Gorman * for the node to be balanced 261299504748SMel Gorman */ 2613dc83edd9SMel Gorman if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) { 26141da177e4SLinus Torvalds cond_resched(); 26158357376dSRafael J. Wysocki 26168357376dSRafael J. Wysocki try_to_freeze(); 26178357376dSRafael J. Wysocki 261873ce02e9SKOSAKI Motohiro /* 261973ce02e9SKOSAKI Motohiro * Fragmentation may mean that the system cannot be 262073ce02e9SKOSAKI Motohiro * rebalanced for high-order allocations in all zones. 262173ce02e9SKOSAKI Motohiro * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, 262273ce02e9SKOSAKI Motohiro * it means the zones have been fully scanned and are still 262373ce02e9SKOSAKI Motohiro * not balanced. For high-order allocations, there is 262473ce02e9SKOSAKI Motohiro * little point trying all over again as kswapd may 262573ce02e9SKOSAKI Motohiro * infinite loop. 262673ce02e9SKOSAKI Motohiro * 262773ce02e9SKOSAKI Motohiro * Instead, recheck all watermarks at order-0 as they 262873ce02e9SKOSAKI Motohiro * are the most important. If watermarks are ok, kswapd will go 262973ce02e9SKOSAKI Motohiro * back to sleep. High-order users can still perform direct 263073ce02e9SKOSAKI Motohiro * reclaim if they wish. 263173ce02e9SKOSAKI Motohiro */ 263273ce02e9SKOSAKI Motohiro if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) 263373ce02e9SKOSAKI Motohiro order = sc.order = 0; 263473ce02e9SKOSAKI Motohiro 26351da177e4SLinus Torvalds goto loop_again; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds 263899504748SMel Gorman /* 263999504748SMel Gorman * If kswapd was reclaiming at a higher order, it has the option of 264099504748SMel Gorman * sleeping without all zones being balanced. Before it does, it must 264199504748SMel Gorman * ensure that the watermarks for order-0 on *all* zones are met and 264299504748SMel Gorman * that the congestion flags are cleared. The congestion flag must 264399504748SMel Gorman * be cleared as kswapd is the only mechanism that clears the flag 264499504748SMel Gorman * and it is potentially going to sleep here. 264599504748SMel Gorman */ 264699504748SMel Gorman if (order) { 26477be62de9SRik van Riel int zones_need_compaction = 1; 26487be62de9SRik van Riel 264999504748SMel Gorman for (i = 0; i <= end_zone; i++) { 265099504748SMel Gorman struct zone *zone = pgdat->node_zones + i; 265199504748SMel Gorman 265299504748SMel Gorman if (!populated_zone(zone)) 265399504748SMel Gorman continue; 265499504748SMel Gorman 26559e3b2f8cSKonstantin Khlebnikov if (zone->all_unreclaimable && 26569e3b2f8cSKonstantin Khlebnikov sc.priority != DEF_PRIORITY) 265799504748SMel Gorman continue; 265899504748SMel Gorman 2659fe2c2a10SRik van Riel /* Would compaction fail due to lack of free memory? */ 2660496b919bSRik van Riel if (COMPACTION_BUILD && 2661496b919bSRik van Riel compaction_suitable(zone, order) == COMPACT_SKIPPED) 2662fe2c2a10SRik van Riel goto loop_again; 2663fe2c2a10SRik van Riel 266499504748SMel Gorman /* Confirm the zone is balanced for order-0 */ 266599504748SMel Gorman if (!zone_watermark_ok(zone, 0, 266699504748SMel Gorman high_wmark_pages(zone), 0, 0)) { 266799504748SMel Gorman order = sc.order = 0; 266899504748SMel Gorman goto loop_again; 266999504748SMel Gorman } 267099504748SMel Gorman 26717be62de9SRik van Riel /* Check if the memory needs to be defragmented. */ 26727be62de9SRik van Riel if (zone_watermark_ok(zone, order, 26737be62de9SRik van Riel low_wmark_pages(zone), *classzone_idx, 0)) 26747be62de9SRik van Riel zones_need_compaction = 0; 26757be62de9SRik van Riel 267699504748SMel Gorman /* If balanced, clear the congested flag */ 267799504748SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 267899504748SMel Gorman } 26797be62de9SRik van Riel 26807be62de9SRik van Riel if (zones_need_compaction) 26817be62de9SRik van Riel compact_pgdat(pgdat, order); 268299504748SMel Gorman } 268399504748SMel Gorman 26840abdee2bSMel Gorman /* 26850abdee2bSMel Gorman * Return the order we were reclaiming at so sleeping_prematurely() 26860abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 26870abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 26880abdee2bSMel Gorman * was awake, order will remain at the higher level 26890abdee2bSMel Gorman */ 2690dc83edd9SMel Gorman *classzone_idx = end_zone; 26910abdee2bSMel Gorman return order; 26921da177e4SLinus Torvalds } 26931da177e4SLinus Torvalds 2694dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 2695f0bc0a60SKOSAKI Motohiro { 2696f0bc0a60SKOSAKI Motohiro long remaining = 0; 2697f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 2698f0bc0a60SKOSAKI Motohiro 2699f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 2700f0bc0a60SKOSAKI Motohiro return; 2701f0bc0a60SKOSAKI Motohiro 2702f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2703f0bc0a60SKOSAKI Motohiro 2704f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 2705dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2706f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 2707f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2708f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2709f0bc0a60SKOSAKI Motohiro } 2710f0bc0a60SKOSAKI Motohiro 2711f0bc0a60SKOSAKI Motohiro /* 2712f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 2713f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 2714f0bc0a60SKOSAKI Motohiro */ 2715dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2716f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 2717f0bc0a60SKOSAKI Motohiro 2718f0bc0a60SKOSAKI Motohiro /* 2719f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 2720f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 2721f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 2722f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 2723f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 2724f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 2725f0bc0a60SKOSAKI Motohiro */ 2726f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 2727f0bc0a60SKOSAKI Motohiro schedule(); 2728f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 2729f0bc0a60SKOSAKI Motohiro } else { 2730f0bc0a60SKOSAKI Motohiro if (remaining) 2731f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 2732f0bc0a60SKOSAKI Motohiro else 2733f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 2734f0bc0a60SKOSAKI Motohiro } 2735f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2736f0bc0a60SKOSAKI Motohiro } 2737f0bc0a60SKOSAKI Motohiro 27381da177e4SLinus Torvalds /* 27391da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 27401da177e4SLinus Torvalds * from the init process. 27411da177e4SLinus Torvalds * 27421da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 27431da177e4SLinus Torvalds * free memory available even if there is no other activity 27441da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 27451da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 27461da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 27471da177e4SLinus Torvalds * 27481da177e4SLinus Torvalds * If there are applications that are active memory-allocators 27491da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 27501da177e4SLinus Torvalds */ 27511da177e4SLinus Torvalds static int kswapd(void *p) 27521da177e4SLinus Torvalds { 2753215ddd66SMel Gorman unsigned long order, new_order; 2754d2ebd0f6SAlex,Shi unsigned balanced_order; 2755215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 2756d2ebd0f6SAlex,Shi int balanced_classzone_idx; 27571da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 27581da177e4SLinus Torvalds struct task_struct *tsk = current; 2759f0bc0a60SKOSAKI Motohiro 27601da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 27611da177e4SLinus Torvalds .reclaimed_slab = 0, 27621da177e4SLinus Torvalds }; 2763a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 27641da177e4SLinus Torvalds 2765cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 2766cf40bd16SNick Piggin 2767174596a0SRusty Russell if (!cpumask_empty(cpumask)) 2768c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 27691da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 27701da177e4SLinus Torvalds 27711da177e4SLinus Torvalds /* 27721da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 27731da177e4SLinus Torvalds * and that if we need more memory we should get access to it 27741da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 27751da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 27761da177e4SLinus Torvalds * 27771da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 27781da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 27791da177e4SLinus Torvalds * page out something else, and this flag essentially protects 27801da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 27811da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 27821da177e4SLinus Torvalds */ 2783930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 278483144186SRafael J. Wysocki set_freezable(); 27851da177e4SLinus Torvalds 2786215ddd66SMel Gorman order = new_order = 0; 2787d2ebd0f6SAlex,Shi balanced_order = 0; 2788215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 2789d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 27901da177e4SLinus Torvalds for ( ; ; ) { 27918fe23e05SDavid Rientjes int ret; 27923e1d1d28SChristoph Lameter 2793215ddd66SMel Gorman /* 2794215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 2795215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 2796215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 2797215ddd66SMel Gorman */ 2798d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 2799d2ebd0f6SAlex,Shi balanced_order == new_order) { 28001da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 280199504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 28021da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 2803215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 2804215ddd66SMel Gorman } 2805215ddd66SMel Gorman 280699504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 28071da177e4SLinus Torvalds /* 28081da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 280999504748SMel Gorman * allocation or has tigher zone constraints 28101da177e4SLinus Torvalds */ 28111da177e4SLinus Torvalds order = new_order; 281299504748SMel Gorman classzone_idx = new_classzone_idx; 28131da177e4SLinus Torvalds } else { 2814d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 2815d2ebd0f6SAlex,Shi balanced_classzone_idx); 28161da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 281799504748SMel Gorman classzone_idx = pgdat->classzone_idx; 2818f0dfcde0SAlex,Shi new_order = order; 2819f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 28204d40502eSMel Gorman pgdat->kswapd_max_order = 0; 2821215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds 28248fe23e05SDavid Rientjes ret = try_to_freeze(); 28258fe23e05SDavid Rientjes if (kthread_should_stop()) 28268fe23e05SDavid Rientjes break; 28278fe23e05SDavid Rientjes 28288fe23e05SDavid Rientjes /* 28298fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 28308fe23e05SDavid Rientjes * after returning from the refrigerator 2831b1296cc4SRafael J. Wysocki */ 283233906bc5SMel Gorman if (!ret) { 283333906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 2834d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 2835d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 2836d2ebd0f6SAlex,Shi &balanced_classzone_idx); 28371da177e4SLinus Torvalds } 283833906bc5SMel Gorman } 28391da177e4SLinus Torvalds return 0; 28401da177e4SLinus Torvalds } 28411da177e4SLinus Torvalds 28421da177e4SLinus Torvalds /* 28431da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 28441da177e4SLinus Torvalds */ 284599504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 28461da177e4SLinus Torvalds { 28471da177e4SLinus Torvalds pg_data_t *pgdat; 28481da177e4SLinus Torvalds 2849f3fe6512SCon Kolivas if (!populated_zone(zone)) 28501da177e4SLinus Torvalds return; 28511da177e4SLinus Torvalds 285202a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 28531da177e4SLinus Torvalds return; 285488f5acf8SMel Gorman pgdat = zone->zone_pgdat; 285599504748SMel Gorman if (pgdat->kswapd_max_order < order) { 285688f5acf8SMel Gorman pgdat->kswapd_max_order = order; 285799504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 285899504748SMel Gorman } 28598d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 28601da177e4SLinus Torvalds return; 286188f5acf8SMel Gorman if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) 286288f5acf8SMel Gorman return; 286388f5acf8SMel Gorman 286488f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 28658d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 28661da177e4SLinus Torvalds } 28671da177e4SLinus Torvalds 2868adea02a1SWu Fengguang /* 2869adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 2870adea02a1SWu Fengguang * The less reclaimable pages may be 2871adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 2872adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 2873adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 2874adea02a1SWu Fengguang */ 2875adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 28764f98a2feSRik van Riel { 2877adea02a1SWu Fengguang int nr; 2878adea02a1SWu Fengguang 2879adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 2880adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 2881adea02a1SWu Fengguang 2882adea02a1SWu Fengguang if (nr_swap_pages > 0) 2883adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 2884adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 2885adea02a1SWu Fengguang 2886adea02a1SWu Fengguang return nr; 2887adea02a1SWu Fengguang } 2888adea02a1SWu Fengguang 2889adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone) 2890adea02a1SWu Fengguang { 2891adea02a1SWu Fengguang int nr; 2892adea02a1SWu Fengguang 2893adea02a1SWu Fengguang nr = zone_page_state(zone, NR_ACTIVE_FILE) + 2894adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_FILE); 2895adea02a1SWu Fengguang 2896adea02a1SWu Fengguang if (nr_swap_pages > 0) 2897adea02a1SWu Fengguang nr += zone_page_state(zone, NR_ACTIVE_ANON) + 2898adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_ANON); 2899adea02a1SWu Fengguang 2900adea02a1SWu Fengguang return nr; 29014f98a2feSRik van Riel } 29024f98a2feSRik van Riel 2903c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 29041da177e4SLinus Torvalds /* 29057b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 2906d6277db4SRafael J. Wysocki * freed pages. 2907d6277db4SRafael J. Wysocki * 2908d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 2909d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 2910d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 29111da177e4SLinus Torvalds */ 29127b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 29131da177e4SLinus Torvalds { 2914d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 2915d6277db4SRafael J. Wysocki struct scan_control sc = { 29167b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 29177b51755cSKOSAKI Motohiro .may_swap = 1, 29187b51755cSKOSAKI Motohiro .may_unmap = 1, 2919d6277db4SRafael J. Wysocki .may_writepage = 1, 29207b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 29217b51755cSKOSAKI Motohiro .hibernation_mode = 1, 29227b51755cSKOSAKI Motohiro .order = 0, 29239e3b2f8cSKonstantin Khlebnikov .priority = DEF_PRIORITY, 29241da177e4SLinus Torvalds }; 2925a09ed5e0SYing Han struct shrink_control shrink = { 2926a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2927a09ed5e0SYing Han }; 29287b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 29297b51755cSKOSAKI Motohiro struct task_struct *p = current; 29307b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 29311da177e4SLinus Torvalds 29327b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 29337b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 2934d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 29357b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 2936d6277db4SRafael J. Wysocki 2937a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2938d6277db4SRafael J. Wysocki 29397b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 29407b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 29417b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 2942d6277db4SRafael J. Wysocki 29437b51755cSKOSAKI Motohiro return nr_reclaimed; 29441da177e4SLinus Torvalds } 2945c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 29461da177e4SLinus Torvalds 29471da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 29481da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 29491da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 29501da177e4SLinus Torvalds restore their cpu bindings. */ 29519c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb, 295269e05944SAndrew Morton unsigned long action, void *hcpu) 29531da177e4SLinus Torvalds { 295458c0a4a7SYasunori Goto int nid; 29551da177e4SLinus Torvalds 29568bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 295758c0a4a7SYasunori Goto for_each_node_state(nid, N_HIGH_MEMORY) { 2958c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 2959a70f7302SRusty Russell const struct cpumask *mask; 2960a70f7302SRusty Russell 2961a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 2962c5f59f08SMike Travis 29633e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 29641da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 2965c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 29661da177e4SLinus Torvalds } 29671da177e4SLinus Torvalds } 29681da177e4SLinus Torvalds return NOTIFY_OK; 29691da177e4SLinus Torvalds } 29701da177e4SLinus Torvalds 29713218ae14SYasunori Goto /* 29723218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 29733218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 29743218ae14SYasunori Goto */ 29753218ae14SYasunori Goto int kswapd_run(int nid) 29763218ae14SYasunori Goto { 29773218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 29783218ae14SYasunori Goto int ret = 0; 29793218ae14SYasunori Goto 29803218ae14SYasunori Goto if (pgdat->kswapd) 29813218ae14SYasunori Goto return 0; 29823218ae14SYasunori Goto 29833218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 29843218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 29853218ae14SYasunori Goto /* failure at boot is fatal */ 29863218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 29873218ae14SYasunori Goto printk("Failed to start kswapd on node %d\n",nid); 29883218ae14SYasunori Goto ret = -1; 29893218ae14SYasunori Goto } 29903218ae14SYasunori Goto return ret; 29913218ae14SYasunori Goto } 29923218ae14SYasunori Goto 29938fe23e05SDavid Rientjes /* 29948fe23e05SDavid Rientjes * Called by memory hotplug when all memory in a node is offlined. 29958fe23e05SDavid Rientjes */ 29968fe23e05SDavid Rientjes void kswapd_stop(int nid) 29978fe23e05SDavid Rientjes { 29988fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 29998fe23e05SDavid Rientjes 30008fe23e05SDavid Rientjes if (kswapd) 30018fe23e05SDavid Rientjes kthread_stop(kswapd); 30028fe23e05SDavid Rientjes } 30038fe23e05SDavid Rientjes 30041da177e4SLinus Torvalds static int __init kswapd_init(void) 30051da177e4SLinus Torvalds { 30063218ae14SYasunori Goto int nid; 300769e05944SAndrew Morton 30081da177e4SLinus Torvalds swap_setup(); 30099422ffbaSChristoph Lameter for_each_node_state(nid, N_HIGH_MEMORY) 30103218ae14SYasunori Goto kswapd_run(nid); 30111da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 30121da177e4SLinus Torvalds return 0; 30131da177e4SLinus Torvalds } 30141da177e4SLinus Torvalds 30151da177e4SLinus Torvalds module_init(kswapd_init) 30169eeff239SChristoph Lameter 30179eeff239SChristoph Lameter #ifdef CONFIG_NUMA 30189eeff239SChristoph Lameter /* 30199eeff239SChristoph Lameter * Zone reclaim mode 30209eeff239SChristoph Lameter * 30219eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 30229eeff239SChristoph Lameter * the watermarks. 30239eeff239SChristoph Lameter */ 30249eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 30259eeff239SChristoph Lameter 30261b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 30277d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 30281b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 30291b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 30301b2ffb78SChristoph Lameter 30319eeff239SChristoph Lameter /* 3032a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3033a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3034a92f7126SChristoph Lameter * a zone. 3035a92f7126SChristoph Lameter */ 3036a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3037a92f7126SChristoph Lameter 30389eeff239SChristoph Lameter /* 30399614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 30409614634fSChristoph Lameter * occur. 30419614634fSChristoph Lameter */ 30429614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 30439614634fSChristoph Lameter 30449614634fSChristoph Lameter /* 30450ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 30460ff38490SChristoph Lameter * slab reclaim needs to occur. 30470ff38490SChristoph Lameter */ 30480ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 30490ff38490SChristoph Lameter 305090afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 305190afa5deSMel Gorman { 305290afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 305390afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 305490afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 305590afa5deSMel Gorman 305690afa5deSMel Gorman /* 305790afa5deSMel Gorman * It's possible for there to be more file mapped pages than 305890afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 305990afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 306090afa5deSMel Gorman */ 306190afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 306290afa5deSMel Gorman } 306390afa5deSMel Gorman 306490afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 306590afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 306690afa5deSMel Gorman { 306790afa5deSMel Gorman long nr_pagecache_reclaimable; 306890afa5deSMel Gorman long delta = 0; 306990afa5deSMel Gorman 307090afa5deSMel Gorman /* 307190afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 307290afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 307390afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 307490afa5deSMel Gorman * a better estimate 307590afa5deSMel Gorman */ 307690afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 307790afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 307890afa5deSMel Gorman else 307990afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 308090afa5deSMel Gorman 308190afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 308290afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 308390afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 308490afa5deSMel Gorman 308590afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 308690afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 308790afa5deSMel Gorman delta = nr_pagecache_reclaimable; 308890afa5deSMel Gorman 308990afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 309090afa5deSMel Gorman } 309190afa5deSMel Gorman 30920ff38490SChristoph Lameter /* 30939eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 30949eeff239SChristoph Lameter */ 3095179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 30969eeff239SChristoph Lameter { 30977fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 309869e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 30999eeff239SChristoph Lameter struct task_struct *p = current; 31009eeff239SChristoph Lameter struct reclaim_state reclaim_state; 3101179e9639SAndrew Morton struct scan_control sc = { 3102179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3103a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 31042e2e4259SKOSAKI Motohiro .may_swap = 1, 310522fba335SKOSAKI Motohiro .nr_to_reclaim = max_t(unsigned long, nr_pages, 310622fba335SKOSAKI Motohiro SWAP_CLUSTER_MAX), 3107179e9639SAndrew Morton .gfp_mask = gfp_mask, 3108bd2f6199SJohannes Weiner .order = order, 31099e3b2f8cSKonstantin Khlebnikov .priority = ZONE_RECLAIM_PRIORITY, 3110179e9639SAndrew Morton }; 3111a09ed5e0SYing Han struct shrink_control shrink = { 3112a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3113a09ed5e0SYing Han }; 311415748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 31159eeff239SChristoph Lameter 31169eeff239SChristoph Lameter cond_resched(); 3117d4f7796eSChristoph Lameter /* 3118d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3119d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3120d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3121d4f7796eSChristoph Lameter */ 3122d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 312376ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 31249eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 31259eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3126c84db23cSChristoph Lameter 312790afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3128a92f7126SChristoph Lameter /* 31290ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 31300ff38490SChristoph Lameter * priorities until we have enough memory freed. 3131a92f7126SChristoph Lameter */ 3132a92f7126SChristoph Lameter do { 31339e3b2f8cSKonstantin Khlebnikov shrink_zone(zone, &sc); 31349e3b2f8cSKonstantin Khlebnikov } while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0); 31350ff38490SChristoph Lameter } 3136a92f7126SChristoph Lameter 313715748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 313815748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 31392a16e3f4SChristoph Lameter /* 31407fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 31410ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 31420ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 31430ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 31440ff38490SChristoph Lameter * pages. 31452a16e3f4SChristoph Lameter * 31460ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 31470ff38490SChristoph Lameter * take a long time. 31482a16e3f4SChristoph Lameter */ 31494dc4b3d9SKOSAKI Motohiro for (;;) { 31504dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 31514dc4b3d9SKOSAKI Motohiro 31524dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 31531495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 31544dc4b3d9SKOSAKI Motohiro break; 31554dc4b3d9SKOSAKI Motohiro 31564dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 31574dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 31584dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 31594dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 31604dc4b3d9SKOSAKI Motohiro break; 31614dc4b3d9SKOSAKI Motohiro } 316283e33a47SChristoph Lameter 316383e33a47SChristoph Lameter /* 316483e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 316583e33a47SChristoph Lameter * reclaimed from this zone. 316683e33a47SChristoph Lameter */ 316715748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 316815748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 316915748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 31702a16e3f4SChristoph Lameter } 31712a16e3f4SChristoph Lameter 31729eeff239SChristoph Lameter p->reclaim_state = NULL; 3173d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 317476ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3175a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 31769eeff239SChristoph Lameter } 3177179e9639SAndrew Morton 3178179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3179179e9639SAndrew Morton { 3180179e9639SAndrew Morton int node_id; 3181d773ed6bSDavid Rientjes int ret; 3182179e9639SAndrew Morton 3183179e9639SAndrew Morton /* 31840ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 31850ff38490SChristoph Lameter * slab pages if we are over the defined limits. 318634aa1330SChristoph Lameter * 31879614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 31889614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 31899614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 31909614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 31919614634fSChristoph Lameter * unmapped file backed pages. 3192179e9639SAndrew Morton */ 319390afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 319490afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3195fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3196179e9639SAndrew Morton 319793e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable) 3198fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3199d773ed6bSDavid Rientjes 3200179e9639SAndrew Morton /* 3201d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3202179e9639SAndrew Morton */ 3203d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3204fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3205179e9639SAndrew Morton 3206179e9639SAndrew Morton /* 3207179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3208179e9639SAndrew Morton * have associated processors. This will favor the local processor 3209179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3210179e9639SAndrew Morton * as wide as possible. 3211179e9639SAndrew Morton */ 321289fa3024SChristoph Lameter node_id = zone_to_nid(zone); 321337c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3214fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3215d773ed6bSDavid Rientjes 3216d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3217fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3218fa5e084eSMel Gorman 3219d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3220d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3221d773ed6bSDavid Rientjes 322224cf7251SMel Gorman if (!ret) 322324cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 322424cf7251SMel Gorman 3225d773ed6bSDavid Rientjes return ret; 3226179e9639SAndrew Morton } 32279eeff239SChristoph Lameter #endif 3228894bc310SLee Schermerhorn 3229894bc310SLee Schermerhorn /* 3230894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3231894bc310SLee Schermerhorn * @page: the page to test 3232894bc310SLee Schermerhorn * @vma: the VMA in which the page is or will be mapped, may be NULL 3233894bc310SLee Schermerhorn * 3234894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 3235b291f000SNick Piggin * lists vs unevictable list. The vma argument is !NULL when called from the 3236b291f000SNick Piggin * fault path to determine how to instantate a new page. 3237894bc310SLee Schermerhorn * 3238894bc310SLee Schermerhorn * Reasons page might not be evictable: 3239ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3240b291f000SNick Piggin * (2) page is part of an mlocked VMA 3241ba9ddf49SLee Schermerhorn * 3242894bc310SLee Schermerhorn */ 3243894bc310SLee Schermerhorn int page_evictable(struct page *page, struct vm_area_struct *vma) 3244894bc310SLee Schermerhorn { 3245894bc310SLee Schermerhorn 3246ba9ddf49SLee Schermerhorn if (mapping_unevictable(page_mapping(page))) 3247ba9ddf49SLee Schermerhorn return 0; 3248ba9ddf49SLee Schermerhorn 3249096a7cf4SYing Han if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page))) 3250b291f000SNick Piggin return 0; 3251894bc310SLee Schermerhorn 3252894bc310SLee Schermerhorn return 1; 3253894bc310SLee Schermerhorn } 325489e004eaSLee Schermerhorn 325585046579SHugh Dickins #ifdef CONFIG_SHMEM 325689e004eaSLee Schermerhorn /** 325724513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 325824513264SHugh Dickins * @pages: array of pages to check 325924513264SHugh Dickins * @nr_pages: number of pages to check 326089e004eaSLee Schermerhorn * 326124513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 326285046579SHugh Dickins * 326385046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 326489e004eaSLee Schermerhorn */ 326524513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 326689e004eaSLee Schermerhorn { 3267925b7673SJohannes Weiner struct lruvec *lruvec; 326824513264SHugh Dickins struct zone *zone = NULL; 326924513264SHugh Dickins int pgscanned = 0; 327024513264SHugh Dickins int pgrescued = 0; 327189e004eaSLee Schermerhorn int i; 327289e004eaSLee Schermerhorn 327324513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 327424513264SHugh Dickins struct page *page = pages[i]; 327524513264SHugh Dickins struct zone *pagezone; 327689e004eaSLee Schermerhorn 327724513264SHugh Dickins pgscanned++; 327824513264SHugh Dickins pagezone = page_zone(page); 327989e004eaSLee Schermerhorn if (pagezone != zone) { 328089e004eaSLee Schermerhorn if (zone) 328189e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 328289e004eaSLee Schermerhorn zone = pagezone; 328389e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 328489e004eaSLee Schermerhorn } 328589e004eaSLee Schermerhorn 328624513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 328724513264SHugh Dickins continue; 328889e004eaSLee Schermerhorn 328924513264SHugh Dickins if (page_evictable(page, NULL)) { 329024513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 329124513264SHugh Dickins 329224513264SHugh Dickins VM_BUG_ON(PageActive(page)); 329324513264SHugh Dickins ClearPageUnevictable(page); 329424513264SHugh Dickins __dec_zone_state(zone, NR_UNEVICTABLE); 329524513264SHugh Dickins lruvec = mem_cgroup_lru_move_lists(zone, page, 329624513264SHugh Dickins LRU_UNEVICTABLE, lru); 329724513264SHugh Dickins list_move(&page->lru, &lruvec->lists[lru]); 329824513264SHugh Dickins __inc_zone_state(zone, NR_INACTIVE_ANON + lru); 329924513264SHugh Dickins pgrescued++; 330089e004eaSLee Schermerhorn } 330189e004eaSLee Schermerhorn } 330224513264SHugh Dickins 330324513264SHugh Dickins if (zone) { 330424513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 330524513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 330624513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 330724513264SHugh Dickins } 330885046579SHugh Dickins } 330985046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3310af936a16SLee Schermerhorn 3311264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3312af936a16SLee Schermerhorn { 3313264e56d8SJohannes Weiner printk_once(KERN_WARNING 331425bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3315264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 331625bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 331725bd91bdSKOSAKI Motohiro current->comm); 3318af936a16SLee Schermerhorn } 3319af936a16SLee Schermerhorn 3320af936a16SLee Schermerhorn /* 3321af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3322af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3323af936a16SLee Schermerhorn */ 3324af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3325af936a16SLee Schermerhorn 3326af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 33278d65af78SAlexey Dobriyan void __user *buffer, 3328af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3329af936a16SLee Schermerhorn { 3330264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 33318d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3332af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3333af936a16SLee Schermerhorn return 0; 3334af936a16SLee Schermerhorn } 3335af936a16SLee Schermerhorn 3336e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3337af936a16SLee Schermerhorn /* 3338af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3339af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3340af936a16SLee Schermerhorn */ 3341af936a16SLee Schermerhorn 334210fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 334310fbcf4cSKay Sievers struct device_attribute *attr, 3344af936a16SLee Schermerhorn char *buf) 3345af936a16SLee Schermerhorn { 3346264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3347af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3348af936a16SLee Schermerhorn } 3349af936a16SLee Schermerhorn 335010fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 335110fbcf4cSKay Sievers struct device_attribute *attr, 3352af936a16SLee Schermerhorn const char *buf, size_t count) 3353af936a16SLee Schermerhorn { 3354264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3355af936a16SLee Schermerhorn return 1; 3356af936a16SLee Schermerhorn } 3357af936a16SLee Schermerhorn 3358af936a16SLee Schermerhorn 335910fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3360af936a16SLee Schermerhorn read_scan_unevictable_node, 3361af936a16SLee Schermerhorn write_scan_unevictable_node); 3362af936a16SLee Schermerhorn 3363af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3364af936a16SLee Schermerhorn { 336510fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3366af936a16SLee Schermerhorn } 3367af936a16SLee Schermerhorn 3368af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3369af936a16SLee Schermerhorn { 337010fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3371af936a16SLee Schermerhorn } 3372e4455abbSThadeu Lima de Souza Cascardo #endif 3373