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 815f53e762SKOSAKI Motohiro /* 82f16015fbSJohannes Weiner * The memory cgroup that hit its limit and as a result is the 83f16015fbSJohannes Weiner * primary target of this reclaim invocation. 84f16015fbSJohannes Weiner */ 85f16015fbSJohannes Weiner struct mem_cgroup *target_mem_cgroup; 8666e1707bSBalbir Singh 87327c0e96SKAMEZAWA Hiroyuki /* 88327c0e96SKAMEZAWA Hiroyuki * Nodemask of nodes allowed by the caller. If NULL, all nodes 89327c0e96SKAMEZAWA Hiroyuki * are scanned. 90327c0e96SKAMEZAWA Hiroyuki */ 91327c0e96SKAMEZAWA Hiroyuki nodemask_t *nodemask; 921da177e4SLinus Torvalds }; 931da177e4SLinus Torvalds 94f16015fbSJohannes Weiner struct mem_cgroup_zone { 95f16015fbSJohannes Weiner struct mem_cgroup *mem_cgroup; 96f16015fbSJohannes Weiner struct zone *zone; 97f16015fbSJohannes Weiner }; 98f16015fbSJohannes Weiner 991da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 1021da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 1031da177e4SLinus Torvalds do { \ 1041da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1051da177e4SLinus Torvalds struct page *prev; \ 1061da177e4SLinus Torvalds \ 1071da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1081da177e4SLinus Torvalds prefetch(&prev->_field); \ 1091da177e4SLinus Torvalds } \ 1101da177e4SLinus Torvalds } while (0) 1111da177e4SLinus Torvalds #else 1121da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1131da177e4SLinus Torvalds #endif 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1161da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1171da177e4SLinus Torvalds do { \ 1181da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1191da177e4SLinus Torvalds struct page *prev; \ 1201da177e4SLinus Torvalds \ 1211da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1221da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1231da177e4SLinus Torvalds } \ 1241da177e4SLinus Torvalds } while (0) 1251da177e4SLinus Torvalds #else 1261da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1271da177e4SLinus Torvalds #endif 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* 1301da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds int vm_swappiness = 60; 133bd1e22b8SAndrew Morton long vm_total_pages; /* The total number of pages which the VM controls */ 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1361da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1371da177e4SLinus Torvalds 13800f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 13989b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14089b5fae5SJohannes Weiner { 141f16015fbSJohannes Weiner return !sc->target_mem_cgroup; 14289b5fae5SJohannes Weiner } 14391a45470SKAMEZAWA Hiroyuki #else 14489b5fae5SJohannes Weiner static bool global_reclaim(struct scan_control *sc) 14589b5fae5SJohannes Weiner { 14689b5fae5SJohannes Weiner return true; 14789b5fae5SJohannes Weiner } 14891a45470SKAMEZAWA Hiroyuki #endif 14991a45470SKAMEZAWA Hiroyuki 150f16015fbSJohannes Weiner static struct zone_reclaim_stat *get_reclaim_stat(struct mem_cgroup_zone *mz) 1516e901571SKOSAKI Motohiro { 15289abfab1SHugh Dickins return &mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup)->reclaim_stat; 1536e901571SKOSAKI Motohiro } 1546e901571SKOSAKI Motohiro 155f16015fbSJohannes Weiner static unsigned long zone_nr_lru_pages(struct mem_cgroup_zone *mz, 156f16015fbSJohannes Weiner enum lru_list lru) 157c9f299d9SKOSAKI Motohiro { 158c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 159f16015fbSJohannes Weiner return mem_cgroup_zone_nr_lru_pages(mz->mem_cgroup, 160f16015fbSJohannes Weiner zone_to_nid(mz->zone), 161f16015fbSJohannes Weiner zone_idx(mz->zone), 162f16015fbSJohannes Weiner BIT(lru)); 163a3d8e054SKOSAKI Motohiro 164f16015fbSJohannes Weiner return zone_page_state(mz->zone, NR_LRU_BASE + lru); 165c9f299d9SKOSAKI Motohiro } 166c9f299d9SKOSAKI Motohiro 167c9f299d9SKOSAKI Motohiro 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1701da177e4SLinus Torvalds */ 1718e1f936bSRusty Russell void register_shrinker(struct shrinker *shrinker) 1721da177e4SLinus Torvalds { 17383aeeadaSKonstantin Khlebnikov atomic_long_set(&shrinker->nr_in_batch, 0); 1741da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1751da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1761da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1771da177e4SLinus Torvalds } 1788e1f936bSRusty Russell EXPORT_SYMBOL(register_shrinker); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* 1811da177e4SLinus Torvalds * Remove one 1821da177e4SLinus Torvalds */ 1838e1f936bSRusty Russell void unregister_shrinker(struct shrinker *shrinker) 1841da177e4SLinus Torvalds { 1851da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1861da177e4SLinus Torvalds list_del(&shrinker->list); 1871da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1881da177e4SLinus Torvalds } 1898e1f936bSRusty Russell EXPORT_SYMBOL(unregister_shrinker); 1901da177e4SLinus Torvalds 1911495f230SYing Han static inline int do_shrinker_shrink(struct shrinker *shrinker, 1921495f230SYing Han struct shrink_control *sc, 1931495f230SYing Han unsigned long nr_to_scan) 1941495f230SYing Han { 1951495f230SYing Han sc->nr_to_scan = nr_to_scan; 1961495f230SYing Han return (*shrinker->shrink)(shrinker, sc); 1971495f230SYing Han } 1981495f230SYing Han 1991da177e4SLinus Torvalds #define SHRINK_BATCH 128 2001da177e4SLinus Torvalds /* 2011da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 2021da177e4SLinus Torvalds * 2031da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 2041da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 2051da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 2061da177e4SLinus Torvalds * generated by these structures. 2071da177e4SLinus Torvalds * 208183ff22bSSimon Arlott * If the vm encountered mapped pages on the LRU it increase the pressure on 2091da177e4SLinus Torvalds * slab to avoid swapping. 2101da177e4SLinus Torvalds * 2111da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 2121da177e4SLinus Torvalds * 2131da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 2141da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 2151da177e4SLinus Torvalds * slab reclaim versus page reclaim. 216b15e0905Sakpm@osdl.org * 217b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 2181da177e4SLinus Torvalds */ 219a09ed5e0SYing Han unsigned long shrink_slab(struct shrink_control *shrink, 2201495f230SYing Han unsigned long nr_pages_scanned, 22169e05944SAndrew Morton unsigned long lru_pages) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds struct shrinker *shrinker; 22469e05944SAndrew Morton unsigned long ret = 0; 2251da177e4SLinus Torvalds 2261495f230SYing Han if (nr_pages_scanned == 0) 2271495f230SYing Han nr_pages_scanned = SWAP_CLUSTER_MAX; 2281da177e4SLinus Torvalds 229f06590bdSMinchan Kim if (!down_read_trylock(&shrinker_rwsem)) { 230f06590bdSMinchan Kim /* Assume we'll be able to shrink next time */ 231f06590bdSMinchan Kim ret = 1; 232f06590bdSMinchan Kim goto out; 233f06590bdSMinchan Kim } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 2361da177e4SLinus Torvalds unsigned long long delta; 237635697c6SKonstantin Khlebnikov long total_scan; 238635697c6SKonstantin Khlebnikov long max_pass; 23909576073SDave Chinner int shrink_ret = 0; 240acf92b48SDave Chinner long nr; 241acf92b48SDave Chinner long new_nr; 242e9299f50SDave Chinner long batch_size = shrinker->batch ? shrinker->batch 243e9299f50SDave Chinner : SHRINK_BATCH; 2441da177e4SLinus Torvalds 245635697c6SKonstantin Khlebnikov max_pass = do_shrinker_shrink(shrinker, shrink, 0); 246635697c6SKonstantin Khlebnikov if (max_pass <= 0) 247635697c6SKonstantin Khlebnikov continue; 248635697c6SKonstantin Khlebnikov 249acf92b48SDave Chinner /* 250acf92b48SDave Chinner * copy the current shrinker scan count into a local variable 251acf92b48SDave Chinner * and zero it so that other concurrent shrinker invocations 252acf92b48SDave Chinner * don't also do this scanning work. 253acf92b48SDave Chinner */ 25483aeeadaSKonstantin Khlebnikov nr = atomic_long_xchg(&shrinker->nr_in_batch, 0); 255acf92b48SDave Chinner 256acf92b48SDave Chinner total_scan = nr; 2571495f230SYing Han delta = (4 * nr_pages_scanned) / shrinker->seeks; 258ea164d73SAndrea Arcangeli delta *= max_pass; 2591da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 260acf92b48SDave Chinner total_scan += delta; 261acf92b48SDave Chinner if (total_scan < 0) { 26288c3bd70SDavid Rientjes printk(KERN_ERR "shrink_slab: %pF negative objects to " 26388c3bd70SDavid Rientjes "delete nr=%ld\n", 264acf92b48SDave Chinner shrinker->shrink, total_scan); 265acf92b48SDave Chinner total_scan = max_pass; 266ea164d73SAndrea Arcangeli } 267ea164d73SAndrea Arcangeli 268ea164d73SAndrea Arcangeli /* 2693567b59aSDave Chinner * We need to avoid excessive windup on filesystem shrinkers 2703567b59aSDave Chinner * due to large numbers of GFP_NOFS allocations causing the 2713567b59aSDave Chinner * shrinkers to return -1 all the time. This results in a large 2723567b59aSDave Chinner * nr being built up so when a shrink that can do some work 2733567b59aSDave Chinner * comes along it empties the entire cache due to nr >>> 2743567b59aSDave Chinner * max_pass. This is bad for sustaining a working set in 2753567b59aSDave Chinner * memory. 2763567b59aSDave Chinner * 2773567b59aSDave Chinner * Hence only allow the shrinker to scan the entire cache when 2783567b59aSDave Chinner * a large delta change is calculated directly. 2793567b59aSDave Chinner */ 2803567b59aSDave Chinner if (delta < max_pass / 4) 2813567b59aSDave Chinner total_scan = min(total_scan, max_pass / 2); 2823567b59aSDave Chinner 2833567b59aSDave Chinner /* 284ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 285ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 286ea164d73SAndrea Arcangeli * freeable entries. 287ea164d73SAndrea Arcangeli */ 288acf92b48SDave Chinner if (total_scan > max_pass * 2) 289acf92b48SDave Chinner total_scan = max_pass * 2; 2901da177e4SLinus Torvalds 291acf92b48SDave Chinner trace_mm_shrink_slab_start(shrinker, shrink, nr, 29209576073SDave Chinner nr_pages_scanned, lru_pages, 29309576073SDave Chinner max_pass, delta, total_scan); 29409576073SDave Chinner 295e9299f50SDave Chinner while (total_scan >= batch_size) { 296b15e0905Sakpm@osdl.org int nr_before; 2971da177e4SLinus Torvalds 2981495f230SYing Han nr_before = do_shrinker_shrink(shrinker, shrink, 0); 2991495f230SYing Han shrink_ret = do_shrinker_shrink(shrinker, shrink, 300e9299f50SDave Chinner batch_size); 3011da177e4SLinus Torvalds if (shrink_ret == -1) 3021da177e4SLinus Torvalds break; 303b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 304b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 305e9299f50SDave Chinner count_vm_events(SLABS_SCANNED, batch_size); 306e9299f50SDave Chinner total_scan -= batch_size; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds cond_resched(); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 311acf92b48SDave Chinner /* 312acf92b48SDave Chinner * move the unused scan count back into the shrinker in a 313acf92b48SDave Chinner * manner that handles concurrent updates. If we exhausted the 314acf92b48SDave Chinner * scan, there is no need to do an update. 315acf92b48SDave Chinner */ 31683aeeadaSKonstantin Khlebnikov if (total_scan > 0) 31783aeeadaSKonstantin Khlebnikov new_nr = atomic_long_add_return(total_scan, 31883aeeadaSKonstantin Khlebnikov &shrinker->nr_in_batch); 31983aeeadaSKonstantin Khlebnikov else 32083aeeadaSKonstantin Khlebnikov new_nr = atomic_long_read(&shrinker->nr_in_batch); 321acf92b48SDave Chinner 322acf92b48SDave Chinner trace_mm_shrink_slab_end(shrinker, shrink_ret, nr, new_nr); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds up_read(&shrinker_rwsem); 325f06590bdSMinchan Kim out: 326f06590bdSMinchan Kim cond_resched(); 327b15e0905Sakpm@osdl.org return ret; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 3311da177e4SLinus Torvalds { 332ceddc3a5SJohannes Weiner /* 333ceddc3a5SJohannes Weiner * A freeable page cache page is referenced only by the caller 334ceddc3a5SJohannes Weiner * that isolated the page, the page cache radix tree and 335ceddc3a5SJohannes Weiner * optional buffer heads at page->private. 336ceddc3a5SJohannes Weiner */ 337edcf4748SJohannes Weiner return page_count(page) - page_has_private(page) == 2; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 3407d3579e8SKOSAKI Motohiro static int may_write_to_queue(struct backing_dev_info *bdi, 3417d3579e8SKOSAKI Motohiro struct scan_control *sc) 3421da177e4SLinus Torvalds { 343930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 3441da177e4SLinus Torvalds return 1; 3451da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 3461da177e4SLinus Torvalds return 1; 3471da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 3481da177e4SLinus Torvalds return 1; 3491da177e4SLinus Torvalds return 0; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* 3531da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 3541da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 3551da177e4SLinus Torvalds * fsync(), msync() or close(). 3561da177e4SLinus Torvalds * 3571da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 3581da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 3591da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 3601da177e4SLinus Torvalds * 3611da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 3621da177e4SLinus Torvalds * __GFP_FS. 3631da177e4SLinus Torvalds */ 3641da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 3651da177e4SLinus Torvalds struct page *page, int error) 3661da177e4SLinus Torvalds { 3677eaceaccSJens Axboe lock_page(page); 3683e9f45bdSGuillaume Chazarain if (page_mapping(page) == mapping) 3693e9f45bdSGuillaume Chazarain mapping_set_error(mapping, error); 3701da177e4SLinus Torvalds unlock_page(page); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 37304e62a29SChristoph Lameter /* possible outcome of pageout() */ 37404e62a29SChristoph Lameter typedef enum { 37504e62a29SChristoph Lameter /* failed to write page out, page is locked */ 37604e62a29SChristoph Lameter PAGE_KEEP, 37704e62a29SChristoph Lameter /* move page to the active list, page is locked */ 37804e62a29SChristoph Lameter PAGE_ACTIVATE, 37904e62a29SChristoph Lameter /* page has been sent to the disk successfully, page is unlocked */ 38004e62a29SChristoph Lameter PAGE_SUCCESS, 38104e62a29SChristoph Lameter /* page is clean and locked */ 38204e62a29SChristoph Lameter PAGE_CLEAN, 38304e62a29SChristoph Lameter } pageout_t; 38404e62a29SChristoph Lameter 3851da177e4SLinus Torvalds /* 3861742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3871742f19fSAndrew Morton * Calls ->writepage(). 3881da177e4SLinus Torvalds */ 389c661b078SAndy Whitcroft static pageout_t pageout(struct page *page, struct address_space *mapping, 3907d3579e8SKOSAKI Motohiro struct scan_control *sc) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds /* 3931da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3941da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3951da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3961da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3971da177e4SLinus Torvalds * PagePrivate for that. 3981da177e4SLinus Torvalds * 3996aceb53bSVincent Li * If this process is currently in __generic_file_aio_write() against 4001da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 4011da177e4SLinus Torvalds * will block. 4021da177e4SLinus Torvalds * 4031da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 4041da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 4051da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 4061da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 4091da177e4SLinus Torvalds return PAGE_KEEP; 4101da177e4SLinus Torvalds if (!mapping) { 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Some data journaling orphaned pages can have 4131da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 4141da177e4SLinus Torvalds */ 415266cf658SDavid Howells if (page_has_private(page)) { 4161da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 4171da177e4SLinus Torvalds ClearPageDirty(page); 418d40cee24SHarvey Harrison printk("%s: orphaned page\n", __func__); 4191da177e4SLinus Torvalds return PAGE_CLEAN; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds return PAGE_KEEP; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 4251da177e4SLinus Torvalds return PAGE_ACTIVATE; 4260e093d99SMel Gorman if (!may_write_to_queue(mapping->backing_dev_info, sc)) 4271da177e4SLinus Torvalds return PAGE_KEEP; 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 4301da177e4SLinus Torvalds int res; 4311da177e4SLinus Torvalds struct writeback_control wbc = { 4321da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 4331da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 434111ebb6eSOGAWA Hirofumi .range_start = 0, 435111ebb6eSOGAWA Hirofumi .range_end = LLONG_MAX, 4361da177e4SLinus Torvalds .for_reclaim = 1, 4371da177e4SLinus Torvalds }; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds SetPageReclaim(page); 4401da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 4411da177e4SLinus Torvalds if (res < 0) 4421da177e4SLinus Torvalds handle_write_error(mapping, page, res); 443994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 4441da177e4SLinus Torvalds ClearPageReclaim(page); 4451da177e4SLinus Torvalds return PAGE_ACTIVATE; 4461da177e4SLinus Torvalds } 447c661b078SAndy Whitcroft 4481da177e4SLinus Torvalds if (!PageWriteback(page)) { 4491da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 4501da177e4SLinus Torvalds ClearPageReclaim(page); 4511da177e4SLinus Torvalds } 45223b9da55SMel Gorman trace_mm_vmscan_writepage(page, trace_reclaim_flags(page)); 453e129b5c2SAndrew Morton inc_zone_page_state(page, NR_VMSCAN_WRITE); 4541da177e4SLinus Torvalds return PAGE_SUCCESS; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds return PAGE_CLEAN; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 460a649fd92SAndrew Morton /* 461e286781dSNick Piggin * Same as remove_mapping, but if the page is removed from the mapping, it 462e286781dSNick Piggin * gets returned with a refcount of 0. 463a649fd92SAndrew Morton */ 464e286781dSNick Piggin static int __remove_mapping(struct address_space *mapping, struct page *page) 46549d2e9ccSChristoph Lameter { 46628e4d965SNick Piggin BUG_ON(!PageLocked(page)); 46728e4d965SNick Piggin BUG_ON(mapping != page_mapping(page)); 46849d2e9ccSChristoph Lameter 46919fd6231SNick Piggin spin_lock_irq(&mapping->tree_lock); 47049d2e9ccSChristoph Lameter /* 4710fd0e6b0SNick Piggin * The non racy check for a busy page. 4720fd0e6b0SNick Piggin * 4730fd0e6b0SNick Piggin * Must be careful with the order of the tests. When someone has 4740fd0e6b0SNick Piggin * a ref to the page, it may be possible that they dirty it then 4750fd0e6b0SNick Piggin * drop the reference. So if PageDirty is tested before page_count 4760fd0e6b0SNick Piggin * here, then the following race may occur: 4770fd0e6b0SNick Piggin * 4780fd0e6b0SNick Piggin * get_user_pages(&page); 4790fd0e6b0SNick Piggin * [user mapping goes away] 4800fd0e6b0SNick Piggin * write_to(page); 4810fd0e6b0SNick Piggin * !PageDirty(page) [good] 4820fd0e6b0SNick Piggin * SetPageDirty(page); 4830fd0e6b0SNick Piggin * put_page(page); 4840fd0e6b0SNick Piggin * !page_count(page) [good, discard it] 4850fd0e6b0SNick Piggin * 4860fd0e6b0SNick Piggin * [oops, our write_to data is lost] 4870fd0e6b0SNick Piggin * 4880fd0e6b0SNick Piggin * Reversing the order of the tests ensures such a situation cannot 4890fd0e6b0SNick Piggin * escape unnoticed. The smp_rmb is needed to ensure the page->flags 4900fd0e6b0SNick Piggin * load is not satisfied before that of page->_count. 4910fd0e6b0SNick Piggin * 4920fd0e6b0SNick Piggin * Note that if SetPageDirty is always performed via set_page_dirty, 4930fd0e6b0SNick Piggin * and thus under tree_lock, then this ordering is not required. 49449d2e9ccSChristoph Lameter */ 495e286781dSNick Piggin if (!page_freeze_refs(page, 2)) 49649d2e9ccSChristoph Lameter goto cannot_free; 497e286781dSNick Piggin /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ 498e286781dSNick Piggin if (unlikely(PageDirty(page))) { 499e286781dSNick Piggin page_unfreeze_refs(page, 2); 50049d2e9ccSChristoph Lameter goto cannot_free; 501e286781dSNick Piggin } 50249d2e9ccSChristoph Lameter 50349d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 50449d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 50549d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 50619fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 507cb4b86baSKAMEZAWA Hiroyuki swapcache_free(swap, page); 508e286781dSNick Piggin } else { 5096072d13cSLinus Torvalds void (*freepage)(struct page *); 5106072d13cSLinus Torvalds 5116072d13cSLinus Torvalds freepage = mapping->a_ops->freepage; 5126072d13cSLinus Torvalds 513e64a782fSMinchan Kim __delete_from_page_cache(page); 51419fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 515e767e056SDaisuke Nishimura mem_cgroup_uncharge_cache_page(page); 5166072d13cSLinus Torvalds 5176072d13cSLinus Torvalds if (freepage != NULL) 5186072d13cSLinus Torvalds freepage(page); 519e286781dSNick Piggin } 520e286781dSNick Piggin 52149d2e9ccSChristoph Lameter return 1; 52249d2e9ccSChristoph Lameter 52349d2e9ccSChristoph Lameter cannot_free: 52419fd6231SNick Piggin spin_unlock_irq(&mapping->tree_lock); 52549d2e9ccSChristoph Lameter return 0; 52649d2e9ccSChristoph Lameter } 52749d2e9ccSChristoph Lameter 5281da177e4SLinus Torvalds /* 529e286781dSNick Piggin * Attempt to detach a locked page from its ->mapping. If it is dirty or if 530e286781dSNick Piggin * someone else has a ref on the page, abort and return 0. If it was 531e286781dSNick Piggin * successfully detached, return 1. Assumes the caller has a single ref on 532e286781dSNick Piggin * this page. 533e286781dSNick Piggin */ 534e286781dSNick Piggin int remove_mapping(struct address_space *mapping, struct page *page) 535e286781dSNick Piggin { 536e286781dSNick Piggin if (__remove_mapping(mapping, page)) { 537e286781dSNick Piggin /* 538e286781dSNick Piggin * Unfreezing the refcount with 1 rather than 2 effectively 539e286781dSNick Piggin * drops the pagecache ref for us without requiring another 540e286781dSNick Piggin * atomic operation. 541e286781dSNick Piggin */ 542e286781dSNick Piggin page_unfreeze_refs(page, 1); 543e286781dSNick Piggin return 1; 544e286781dSNick Piggin } 545e286781dSNick Piggin return 0; 546e286781dSNick Piggin } 547e286781dSNick Piggin 548894bc310SLee Schermerhorn /** 549894bc310SLee Schermerhorn * putback_lru_page - put previously isolated page onto appropriate LRU list 550894bc310SLee Schermerhorn * @page: page to be put back to appropriate lru list 551894bc310SLee Schermerhorn * 552894bc310SLee Schermerhorn * Add previously isolated @page to appropriate LRU list. 553894bc310SLee Schermerhorn * Page may still be unevictable for other reasons. 554894bc310SLee Schermerhorn * 555894bc310SLee Schermerhorn * lru_lock must not be held, interrupts must be enabled. 556894bc310SLee Schermerhorn */ 557894bc310SLee Schermerhorn void putback_lru_page(struct page *page) 558894bc310SLee Schermerhorn { 559894bc310SLee Schermerhorn int lru; 560894bc310SLee Schermerhorn int active = !!TestClearPageActive(page); 561bbfd28eeSLee Schermerhorn int was_unevictable = PageUnevictable(page); 562894bc310SLee Schermerhorn 563894bc310SLee Schermerhorn VM_BUG_ON(PageLRU(page)); 564894bc310SLee Schermerhorn 565894bc310SLee Schermerhorn redo: 566894bc310SLee Schermerhorn ClearPageUnevictable(page); 567894bc310SLee Schermerhorn 568894bc310SLee Schermerhorn if (page_evictable(page, NULL)) { 569894bc310SLee Schermerhorn /* 570894bc310SLee Schermerhorn * For evictable pages, we can use the cache. 571894bc310SLee Schermerhorn * In event of a race, worst case is we end up with an 572894bc310SLee Schermerhorn * unevictable page on [in]active list. 573894bc310SLee Schermerhorn * We know how to handle that. 574894bc310SLee Schermerhorn */ 575401a8e1cSJohannes Weiner lru = active + page_lru_base_type(page); 576894bc310SLee Schermerhorn lru_cache_add_lru(page, lru); 577894bc310SLee Schermerhorn } else { 578894bc310SLee Schermerhorn /* 579894bc310SLee Schermerhorn * Put unevictable pages directly on zone's unevictable 580894bc310SLee Schermerhorn * list. 581894bc310SLee Schermerhorn */ 582894bc310SLee Schermerhorn lru = LRU_UNEVICTABLE; 583894bc310SLee Schermerhorn add_page_to_unevictable_list(page); 5846a7b9548SJohannes Weiner /* 58521ee9f39SMinchan Kim * When racing with an mlock or AS_UNEVICTABLE clearing 58621ee9f39SMinchan Kim * (page is unlocked) make sure that if the other thread 58721ee9f39SMinchan Kim * does not observe our setting of PG_lru and fails 58824513264SHugh Dickins * isolation/check_move_unevictable_pages, 58921ee9f39SMinchan Kim * we see PG_mlocked/AS_UNEVICTABLE cleared below and move 5906a7b9548SJohannes Weiner * the page back to the evictable list. 5916a7b9548SJohannes Weiner * 59221ee9f39SMinchan Kim * The other side is TestClearPageMlocked() or shmem_lock(). 5936a7b9548SJohannes Weiner */ 5946a7b9548SJohannes Weiner smp_mb(); 595894bc310SLee Schermerhorn } 596894bc310SLee Schermerhorn 597894bc310SLee Schermerhorn /* 598894bc310SLee Schermerhorn * page's status can change while we move it among lru. If an evictable 599894bc310SLee Schermerhorn * page is on unevictable list, it never be freed. To avoid that, 600894bc310SLee Schermerhorn * check after we added it to the list, again. 601894bc310SLee Schermerhorn */ 602894bc310SLee Schermerhorn if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { 603894bc310SLee Schermerhorn if (!isolate_lru_page(page)) { 604894bc310SLee Schermerhorn put_page(page); 605894bc310SLee Schermerhorn goto redo; 606894bc310SLee Schermerhorn } 607894bc310SLee Schermerhorn /* This means someone else dropped this page from LRU 608894bc310SLee Schermerhorn * So, it will be freed or putback to LRU again. There is 609894bc310SLee Schermerhorn * nothing to do here. 610894bc310SLee Schermerhorn */ 611894bc310SLee Schermerhorn } 612894bc310SLee Schermerhorn 613bbfd28eeSLee Schermerhorn if (was_unevictable && lru != LRU_UNEVICTABLE) 614bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGRESCUED); 615bbfd28eeSLee Schermerhorn else if (!was_unevictable && lru == LRU_UNEVICTABLE) 616bbfd28eeSLee Schermerhorn count_vm_event(UNEVICTABLE_PGCULLED); 617bbfd28eeSLee Schermerhorn 618894bc310SLee Schermerhorn put_page(page); /* drop ref from isolate */ 619894bc310SLee Schermerhorn } 620894bc310SLee Schermerhorn 621dfc8d636SJohannes Weiner enum page_references { 622dfc8d636SJohannes Weiner PAGEREF_RECLAIM, 623dfc8d636SJohannes Weiner PAGEREF_RECLAIM_CLEAN, 62464574746SJohannes Weiner PAGEREF_KEEP, 625dfc8d636SJohannes Weiner PAGEREF_ACTIVATE, 626dfc8d636SJohannes Weiner }; 627dfc8d636SJohannes Weiner 628dfc8d636SJohannes Weiner static enum page_references page_check_references(struct page *page, 629f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 630dfc8d636SJohannes Weiner struct scan_control *sc) 631dfc8d636SJohannes Weiner { 63264574746SJohannes Weiner int referenced_ptes, referenced_page; 633dfc8d636SJohannes Weiner unsigned long vm_flags; 634dfc8d636SJohannes Weiner 635c3ac9a8aSJohannes Weiner referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup, 636c3ac9a8aSJohannes Weiner &vm_flags); 63764574746SJohannes Weiner referenced_page = TestClearPageReferenced(page); 638dfc8d636SJohannes Weiner 639dfc8d636SJohannes Weiner /* 640dfc8d636SJohannes Weiner * Mlock lost the isolation race with us. Let try_to_unmap() 641dfc8d636SJohannes Weiner * move the page to the unevictable list. 642dfc8d636SJohannes Weiner */ 643dfc8d636SJohannes Weiner if (vm_flags & VM_LOCKED) 644dfc8d636SJohannes Weiner return PAGEREF_RECLAIM; 645dfc8d636SJohannes Weiner 64664574746SJohannes Weiner if (referenced_ptes) { 647e4898273SMichal Hocko if (PageSwapBacked(page)) 64864574746SJohannes Weiner return PAGEREF_ACTIVATE; 64964574746SJohannes Weiner /* 65064574746SJohannes Weiner * All mapped pages start out with page table 65164574746SJohannes Weiner * references from the instantiating fault, so we need 65264574746SJohannes Weiner * to look twice if a mapped file page is used more 65364574746SJohannes Weiner * than once. 65464574746SJohannes Weiner * 65564574746SJohannes Weiner * Mark it and spare it for another trip around the 65664574746SJohannes Weiner * inactive list. Another page table reference will 65764574746SJohannes Weiner * lead to its activation. 65864574746SJohannes Weiner * 65964574746SJohannes Weiner * Note: the mark is set for activated pages as well 66064574746SJohannes Weiner * so that recently deactivated but used pages are 66164574746SJohannes Weiner * quickly recovered. 66264574746SJohannes Weiner */ 66364574746SJohannes Weiner SetPageReferenced(page); 66464574746SJohannes Weiner 66534dbc67aSKonstantin Khlebnikov if (referenced_page || referenced_ptes > 1) 666dfc8d636SJohannes Weiner return PAGEREF_ACTIVATE; 667dfc8d636SJohannes Weiner 668c909e993SKonstantin Khlebnikov /* 669c909e993SKonstantin Khlebnikov * Activate file-backed executable pages after first usage. 670c909e993SKonstantin Khlebnikov */ 671c909e993SKonstantin Khlebnikov if (vm_flags & VM_EXEC) 672c909e993SKonstantin Khlebnikov return PAGEREF_ACTIVATE; 673c909e993SKonstantin Khlebnikov 67464574746SJohannes Weiner return PAGEREF_KEEP; 67564574746SJohannes Weiner } 67664574746SJohannes Weiner 677dfc8d636SJohannes Weiner /* Reclaim if clean, defer dirty pages to writeback */ 6782e30244aSKOSAKI Motohiro if (referenced_page && !PageSwapBacked(page)) 679dfc8d636SJohannes Weiner return PAGEREF_RECLAIM_CLEAN; 68064574746SJohannes Weiner 68164574746SJohannes Weiner return PAGEREF_RECLAIM; 682dfc8d636SJohannes Weiner } 683dfc8d636SJohannes Weiner 684e286781dSNick Piggin /* 6851742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 6861da177e4SLinus Torvalds */ 6871742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 688f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 689f84f6e2bSMel Gorman struct scan_control *sc, 69092df3a72SMel Gorman int priority, 69192df3a72SMel Gorman unsigned long *ret_nr_dirty, 69292df3a72SMel Gorman unsigned long *ret_nr_writeback) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds LIST_HEAD(ret_pages); 695abe4c3b5SMel Gorman LIST_HEAD(free_pages); 6961da177e4SLinus Torvalds int pgactivate = 0; 6970e093d99SMel Gorman unsigned long nr_dirty = 0; 6980e093d99SMel Gorman unsigned long nr_congested = 0; 69905ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 70092df3a72SMel Gorman unsigned long nr_writeback = 0; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds cond_resched(); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds while (!list_empty(page_list)) { 705dfc8d636SJohannes Weiner enum page_references references; 7061da177e4SLinus Torvalds struct address_space *mapping; 7071da177e4SLinus Torvalds struct page *page; 7081da177e4SLinus Torvalds int may_enter_fs; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds cond_resched(); 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds page = lru_to_page(page_list); 7131da177e4SLinus Torvalds list_del(&page->lru); 7141da177e4SLinus Torvalds 715529ae9aaSNick Piggin if (!trylock_page(page)) 7161da177e4SLinus Torvalds goto keep; 7171da177e4SLinus Torvalds 718725d704eSNick Piggin VM_BUG_ON(PageActive(page)); 719f16015fbSJohannes Weiner VM_BUG_ON(page_zone(page) != mz->zone); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds sc->nr_scanned++; 72280e43426SChristoph Lameter 723b291f000SNick Piggin if (unlikely(!page_evictable(page, NULL))) 724b291f000SNick Piggin goto cull_mlocked; 725894bc310SLee Schermerhorn 726a6dc60f8SJohannes Weiner if (!sc->may_unmap && page_mapped(page)) 72780e43426SChristoph Lameter goto keep_locked; 72880e43426SChristoph Lameter 7291da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 7301da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 7311da177e4SLinus Torvalds sc->nr_scanned++; 7321da177e4SLinus Torvalds 733c661b078SAndy Whitcroft may_enter_fs = (sc->gfp_mask & __GFP_FS) || 734c661b078SAndy Whitcroft (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 735c661b078SAndy Whitcroft 736c661b078SAndy Whitcroft if (PageWriteback(page)) { 73792df3a72SMel Gorman nr_writeback++; 7387d3579e8SKOSAKI Motohiro unlock_page(page); 73941ac1999SMel Gorman goto keep; 740c661b078SAndy Whitcroft } 7411da177e4SLinus Torvalds 742f16015fbSJohannes Weiner references = page_check_references(page, mz, sc); 743dfc8d636SJohannes Weiner switch (references) { 744dfc8d636SJohannes Weiner case PAGEREF_ACTIVATE: 7451da177e4SLinus Torvalds goto activate_locked; 74664574746SJohannes Weiner case PAGEREF_KEEP: 74764574746SJohannes Weiner goto keep_locked; 748dfc8d636SJohannes Weiner case PAGEREF_RECLAIM: 749dfc8d636SJohannes Weiner case PAGEREF_RECLAIM_CLEAN: 750dfc8d636SJohannes Weiner ; /* try to reclaim the page below */ 751dfc8d636SJohannes Weiner } 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* 7541da177e4SLinus Torvalds * Anonymous process memory has backing store? 7551da177e4SLinus Torvalds * Try to allocate it some swap space here. 7561da177e4SLinus Torvalds */ 757b291f000SNick Piggin if (PageAnon(page) && !PageSwapCache(page)) { 75863eb6b93SHugh Dickins if (!(sc->gfp_mask & __GFP_IO)) 75963eb6b93SHugh Dickins goto keep_locked; 760ac47b003SHugh Dickins if (!add_to_swap(page)) 7611da177e4SLinus Torvalds goto activate_locked; 76263eb6b93SHugh Dickins may_enter_fs = 1; 763b291f000SNick Piggin } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds mapping = page_mapping(page); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* 7681da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 7691da177e4SLinus Torvalds * processes. Try to unmap it here. 7701da177e4SLinus Torvalds */ 7711da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 77214fa31b8SAndi Kleen switch (try_to_unmap(page, TTU_UNMAP)) { 7731da177e4SLinus Torvalds case SWAP_FAIL: 7741da177e4SLinus Torvalds goto activate_locked; 7751da177e4SLinus Torvalds case SWAP_AGAIN: 7761da177e4SLinus Torvalds goto keep_locked; 777b291f000SNick Piggin case SWAP_MLOCK: 778b291f000SNick Piggin goto cull_mlocked; 7791da177e4SLinus Torvalds case SWAP_SUCCESS: 7801da177e4SLinus Torvalds ; /* try to free the page below */ 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds if (PageDirty(page)) { 7850e093d99SMel Gorman nr_dirty++; 7860e093d99SMel Gorman 787ee72886dSMel Gorman /* 788ee72886dSMel Gorman * Only kswapd can writeback filesystem pages to 789f84f6e2bSMel Gorman * avoid risk of stack overflow but do not writeback 790f84f6e2bSMel Gorman * unless under significant pressure. 791ee72886dSMel Gorman */ 792f84f6e2bSMel Gorman if (page_is_file_cache(page) && 793f84f6e2bSMel Gorman (!current_is_kswapd() || priority >= DEF_PRIORITY - 2)) { 79449ea7eb6SMel Gorman /* 79549ea7eb6SMel Gorman * Immediately reclaim when written back. 79649ea7eb6SMel Gorman * Similar in principal to deactivate_page() 79749ea7eb6SMel Gorman * except we already have the page isolated 79849ea7eb6SMel Gorman * and know it's dirty 79949ea7eb6SMel Gorman */ 80049ea7eb6SMel Gorman inc_zone_page_state(page, NR_VMSCAN_IMMEDIATE); 80149ea7eb6SMel Gorman SetPageReclaim(page); 80249ea7eb6SMel Gorman 803ee72886dSMel Gorman goto keep_locked; 804ee72886dSMel Gorman } 805ee72886dSMel Gorman 806dfc8d636SJohannes Weiner if (references == PAGEREF_RECLAIM_CLEAN) 8071da177e4SLinus Torvalds goto keep_locked; 8084dd4b920SAndrew Morton if (!may_enter_fs) 8091da177e4SLinus Torvalds goto keep_locked; 81052a8363eSChristoph Lameter if (!sc->may_writepage) 8111da177e4SLinus Torvalds goto keep_locked; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 8147d3579e8SKOSAKI Motohiro switch (pageout(page, mapping, sc)) { 8151da177e4SLinus Torvalds case PAGE_KEEP: 8160e093d99SMel Gorman nr_congested++; 8171da177e4SLinus Torvalds goto keep_locked; 8181da177e4SLinus Torvalds case PAGE_ACTIVATE: 8191da177e4SLinus Torvalds goto activate_locked; 8201da177e4SLinus Torvalds case PAGE_SUCCESS: 8217d3579e8SKOSAKI Motohiro if (PageWriteback(page)) 82241ac1999SMel Gorman goto keep; 8237d3579e8SKOSAKI Motohiro if (PageDirty(page)) 8241da177e4SLinus Torvalds goto keep; 8257d3579e8SKOSAKI Motohiro 8261da177e4SLinus Torvalds /* 8271da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 8281da177e4SLinus Torvalds * ahead and try to reclaim the page. 8291da177e4SLinus Torvalds */ 830529ae9aaSNick Piggin if (!trylock_page(page)) 8311da177e4SLinus Torvalds goto keep; 8321da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 8331da177e4SLinus Torvalds goto keep_locked; 8341da177e4SLinus Torvalds mapping = page_mapping(page); 8351da177e4SLinus Torvalds case PAGE_CLEAN: 8361da177e4SLinus Torvalds ; /* try to free the page below */ 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds /* 8411da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 8421da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 8431da177e4SLinus Torvalds * the page as well. 8441da177e4SLinus Torvalds * 8451da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 8461da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 8471da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 8481da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 8491da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 8501da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 8511da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 8521da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 8531da177e4SLinus Torvalds * 8541da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 8551da177e4SLinus Torvalds * the pages which were not successfully invalidated in 8561da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 8571da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 8581da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 8591da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 8601da177e4SLinus Torvalds */ 861266cf658SDavid Howells if (page_has_private(page)) { 8621da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 8631da177e4SLinus Torvalds goto activate_locked; 864e286781dSNick Piggin if (!mapping && page_count(page) == 1) { 865e286781dSNick Piggin unlock_page(page); 866e286781dSNick Piggin if (put_page_testzero(page)) 8671da177e4SLinus Torvalds goto free_it; 868e286781dSNick Piggin else { 869e286781dSNick Piggin /* 870e286781dSNick Piggin * rare race with speculative reference. 871e286781dSNick Piggin * the speculative reference will free 872e286781dSNick Piggin * this page shortly, so we may 873e286781dSNick Piggin * increment nr_reclaimed here (and 874e286781dSNick Piggin * leave it off the LRU). 875e286781dSNick Piggin */ 876e286781dSNick Piggin nr_reclaimed++; 877e286781dSNick Piggin continue; 878e286781dSNick Piggin } 879e286781dSNick Piggin } 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds 882e286781dSNick Piggin if (!mapping || !__remove_mapping(mapping, page)) 88349d2e9ccSChristoph Lameter goto keep_locked; 8841da177e4SLinus Torvalds 885a978d6f5SNick Piggin /* 886a978d6f5SNick Piggin * At this point, we have no other references and there is 887a978d6f5SNick Piggin * no way to pick any more up (removed from LRU, removed 888a978d6f5SNick Piggin * from pagecache). Can use non-atomic bitops now (and 889a978d6f5SNick Piggin * we obviously don't have to worry about waking up a process 890a978d6f5SNick Piggin * waiting on the page lock, because there are no references. 891a978d6f5SNick Piggin */ 892a978d6f5SNick Piggin __clear_page_locked(page); 893e286781dSNick Piggin free_it: 89405ff5137SAndrew Morton nr_reclaimed++; 895abe4c3b5SMel Gorman 896abe4c3b5SMel Gorman /* 897abe4c3b5SMel Gorman * Is there need to periodically free_page_list? It would 898abe4c3b5SMel Gorman * appear not as the counts should be low 899abe4c3b5SMel Gorman */ 900abe4c3b5SMel Gorman list_add(&page->lru, &free_pages); 9011da177e4SLinus Torvalds continue; 9021da177e4SLinus Torvalds 903b291f000SNick Piggin cull_mlocked: 90463d6c5adSHugh Dickins if (PageSwapCache(page)) 90563d6c5adSHugh Dickins try_to_free_swap(page); 906b291f000SNick Piggin unlock_page(page); 907b291f000SNick Piggin putback_lru_page(page); 908b291f000SNick Piggin continue; 909b291f000SNick Piggin 9101da177e4SLinus Torvalds activate_locked: 91168a22394SRik van Riel /* Not a candidate for swapping, so reclaim swap space. */ 91268a22394SRik van Riel if (PageSwapCache(page) && vm_swap_full()) 913a2c43eedSHugh Dickins try_to_free_swap(page); 914894bc310SLee Schermerhorn VM_BUG_ON(PageActive(page)); 9151da177e4SLinus Torvalds SetPageActive(page); 9161da177e4SLinus Torvalds pgactivate++; 9171da177e4SLinus Torvalds keep_locked: 9181da177e4SLinus Torvalds unlock_page(page); 9191da177e4SLinus Torvalds keep: 9201da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 921b291f000SNick Piggin VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); 9221da177e4SLinus Torvalds } 923abe4c3b5SMel Gorman 9240e093d99SMel Gorman /* 9250e093d99SMel Gorman * Tag a zone as congested if all the dirty pages encountered were 9260e093d99SMel Gorman * backed by a congested BDI. In this case, reclaimers should just 9270e093d99SMel Gorman * back off and wait for congestion to clear because further reclaim 9280e093d99SMel Gorman * will encounter the same problem 9290e093d99SMel Gorman */ 93089b5fae5SJohannes Weiner if (nr_dirty && nr_dirty == nr_congested && global_reclaim(sc)) 931f16015fbSJohannes Weiner zone_set_flag(mz->zone, ZONE_CONGESTED); 9320e093d99SMel Gorman 933cc59850eSKonstantin Khlebnikov free_hot_cold_page_list(&free_pages, 1); 934abe4c3b5SMel Gorman 9351da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 936f8891e5eSChristoph Lameter count_vm_events(PGACTIVATE, pgactivate); 93792df3a72SMel Gorman *ret_nr_dirty += nr_dirty; 93892df3a72SMel Gorman *ret_nr_writeback += nr_writeback; 93905ff5137SAndrew Morton return nr_reclaimed; 9401da177e4SLinus Torvalds } 9411da177e4SLinus Torvalds 9425ad333ebSAndy Whitcroft /* 9435ad333ebSAndy Whitcroft * Attempt to remove the specified page from its LRU. Only take this page 9445ad333ebSAndy Whitcroft * if it is of the appropriate PageActive status. Pages which are being 9455ad333ebSAndy Whitcroft * freed elsewhere are also ignored. 9465ad333ebSAndy Whitcroft * 9475ad333ebSAndy Whitcroft * page: page to consider 9485ad333ebSAndy Whitcroft * mode: one of the LRU isolation modes defined above 9495ad333ebSAndy Whitcroft * 9505ad333ebSAndy Whitcroft * returns 0 on success, -ve errno on failure. 9515ad333ebSAndy Whitcroft */ 9524356f21dSMinchan Kim int __isolate_lru_page(struct page *page, isolate_mode_t mode, int file) 9535ad333ebSAndy Whitcroft { 9544356f21dSMinchan Kim bool all_lru_mode; 9555ad333ebSAndy Whitcroft int ret = -EINVAL; 9565ad333ebSAndy Whitcroft 9575ad333ebSAndy Whitcroft /* Only take pages on the LRU. */ 9585ad333ebSAndy Whitcroft if (!PageLRU(page)) 9595ad333ebSAndy Whitcroft return ret; 9605ad333ebSAndy Whitcroft 9614356f21dSMinchan Kim all_lru_mode = (mode & (ISOLATE_ACTIVE|ISOLATE_INACTIVE)) == 9624356f21dSMinchan Kim (ISOLATE_ACTIVE|ISOLATE_INACTIVE); 9634356f21dSMinchan Kim 9645ad333ebSAndy Whitcroft /* 9655ad333ebSAndy Whitcroft * When checking the active state, we need to be sure we are 9665ad333ebSAndy Whitcroft * dealing with comparible boolean values. Take the logical not 9675ad333ebSAndy Whitcroft * of each. 9685ad333ebSAndy Whitcroft */ 9694356f21dSMinchan Kim if (!all_lru_mode && !PageActive(page) != !(mode & ISOLATE_ACTIVE)) 9705ad333ebSAndy Whitcroft return ret; 9715ad333ebSAndy Whitcroft 9724356f21dSMinchan Kim if (!all_lru_mode && !!page_is_file_cache(page) != file) 9734f98a2feSRik van Riel return ret; 9744f98a2feSRik van Riel 975c53919adSMel Gorman /* Do not give back unevictable pages for compaction */ 976894bc310SLee Schermerhorn if (PageUnevictable(page)) 977894bc310SLee Schermerhorn return ret; 978894bc310SLee Schermerhorn 9795ad333ebSAndy Whitcroft ret = -EBUSY; 98008e552c6SKAMEZAWA Hiroyuki 981c8244935SMel Gorman /* 982c8244935SMel Gorman * To minimise LRU disruption, the caller can indicate that it only 983c8244935SMel Gorman * wants to isolate pages it will be able to operate on without 984c8244935SMel Gorman * blocking - clean pages for the most part. 985c8244935SMel Gorman * 986c8244935SMel Gorman * ISOLATE_CLEAN means that only clean pages should be isolated. This 987c8244935SMel Gorman * is used by reclaim when it is cannot write to backing storage 988c8244935SMel Gorman * 989c8244935SMel Gorman * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages 990c8244935SMel Gorman * that it is possible to migrate without blocking 991c8244935SMel Gorman */ 992c8244935SMel Gorman if (mode & (ISOLATE_CLEAN|ISOLATE_ASYNC_MIGRATE)) { 993c8244935SMel Gorman /* All the caller can do on PageWriteback is block */ 994c8244935SMel Gorman if (PageWriteback(page)) 99539deaf85SMinchan Kim return ret; 99639deaf85SMinchan Kim 997c8244935SMel Gorman if (PageDirty(page)) { 998c8244935SMel Gorman struct address_space *mapping; 999c8244935SMel Gorman 1000c8244935SMel Gorman /* ISOLATE_CLEAN means only clean pages */ 1001c8244935SMel Gorman if (mode & ISOLATE_CLEAN) 1002c8244935SMel Gorman return ret; 1003c8244935SMel Gorman 1004c8244935SMel Gorman /* 1005c8244935SMel Gorman * Only pages without mappings or that have a 1006c8244935SMel Gorman * ->migratepage callback are possible to migrate 1007c8244935SMel Gorman * without blocking 1008c8244935SMel Gorman */ 1009c8244935SMel Gorman mapping = page_mapping(page); 1010c8244935SMel Gorman if (mapping && !mapping->a_ops->migratepage) 1011c8244935SMel Gorman return ret; 1012c8244935SMel Gorman } 1013c8244935SMel Gorman } 1014c8244935SMel Gorman 1015f80c0673SMinchan Kim if ((mode & ISOLATE_UNMAPPED) && page_mapped(page)) 1016f80c0673SMinchan Kim return ret; 1017f80c0673SMinchan Kim 10185ad333ebSAndy Whitcroft if (likely(get_page_unless_zero(page))) { 10195ad333ebSAndy Whitcroft /* 10205ad333ebSAndy Whitcroft * Be careful not to clear PageLRU until after we're 10215ad333ebSAndy Whitcroft * sure the page is not being freed elsewhere -- the 10225ad333ebSAndy Whitcroft * page release code relies on it. 10235ad333ebSAndy Whitcroft */ 10245ad333ebSAndy Whitcroft ClearPageLRU(page); 10255ad333ebSAndy Whitcroft ret = 0; 10265ad333ebSAndy Whitcroft } 10275ad333ebSAndy Whitcroft 10285ad333ebSAndy Whitcroft return ret; 10295ad333ebSAndy Whitcroft } 10305ad333ebSAndy Whitcroft 103149d2e9ccSChristoph Lameter /* 10321da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10331da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10341da177e4SLinus Torvalds * and working on them outside the LRU lock. 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10371da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10381da177e4SLinus Torvalds * 10391da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10401da177e4SLinus Torvalds * 10411da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 1042f626012dSHugh Dickins * @mz: The mem_cgroup_zone to pull pages from. 10431da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 1044f626012dSHugh Dickins * @nr_scanned: The number of pages that were scanned. 1045fe2c2a10SRik van Riel * @sc: The scan_control struct for this reclaim session 10465ad333ebSAndy Whitcroft * @mode: One of the LRU isolation modes 1047*3cb99451SKonstantin Khlebnikov * @lru: LRU list id for isolating 10481da177e4SLinus Torvalds * 10491da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10501da177e4SLinus Torvalds */ 105169e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 1052f626012dSHugh Dickins struct mem_cgroup_zone *mz, struct list_head *dst, 1053fe2c2a10SRik van Riel unsigned long *nr_scanned, struct scan_control *sc, 1054*3cb99451SKonstantin Khlebnikov isolate_mode_t mode, enum lru_list lru) 10551da177e4SLinus Torvalds { 1056f626012dSHugh Dickins struct lruvec *lruvec; 1057f626012dSHugh Dickins struct list_head *src; 105869e05944SAndrew Morton unsigned long nr_taken = 0; 1059c9b02d97SWu Fengguang unsigned long scan; 1060*3cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1061f626012dSHugh Dickins 1062f626012dSHugh Dickins lruvec = mem_cgroup_zone_lruvec(mz->zone, mz->mem_cgroup); 1063f626012dSHugh Dickins src = &lruvec->lists[lru]; 10641da177e4SLinus Torvalds 1065c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 10665ad333ebSAndy Whitcroft struct page *page; 10675ad333ebSAndy Whitcroft 10681da177e4SLinus Torvalds page = lru_to_page(src); 10691da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10701da177e4SLinus Torvalds 1071725d704eSNick Piggin VM_BUG_ON(!PageLRU(page)); 10728d438f96SNick Piggin 10734f98a2feSRik van Riel switch (__isolate_lru_page(page, mode, file)) { 10745ad333ebSAndy Whitcroft case 0: 1075925b7673SJohannes Weiner mem_cgroup_lru_del(page); 10765ad333ebSAndy Whitcroft list_move(&page->lru, dst); 10772c888cfbSRik van Riel nr_taken += hpage_nr_pages(page); 10785ad333ebSAndy Whitcroft break; 10797c8ee9a8SNick Piggin 10805ad333ebSAndy Whitcroft case -EBUSY: 10815ad333ebSAndy Whitcroft /* else it is being freed elsewhere */ 10825ad333ebSAndy Whitcroft list_move(&page->lru, src); 10835ad333ebSAndy Whitcroft continue; 10845ad333ebSAndy Whitcroft 10855ad333ebSAndy Whitcroft default: 10865ad333ebSAndy Whitcroft BUG(); 10875ad333ebSAndy Whitcroft } 10885ad333ebSAndy Whitcroft } 10891da177e4SLinus Torvalds 1090f626012dSHugh Dickins *nr_scanned = scan; 1091a8a94d15SMel Gorman 1092fe2c2a10SRik van Riel trace_mm_vmscan_lru_isolate(sc->order, 1093a8a94d15SMel Gorman nr_to_scan, scan, 1094a8a94d15SMel Gorman nr_taken, 1095ea4d349fSTao Ma mode, file); 10961da177e4SLinus Torvalds return nr_taken; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 109962695a84SNick Piggin /** 110062695a84SNick Piggin * isolate_lru_page - tries to isolate a page from its LRU list 110162695a84SNick Piggin * @page: page to isolate from its LRU list 110262695a84SNick Piggin * 110362695a84SNick Piggin * Isolates a @page from an LRU list, clears PageLRU and adjusts the 110462695a84SNick Piggin * vmstat statistic corresponding to whatever LRU list the page was on. 110562695a84SNick Piggin * 110662695a84SNick Piggin * Returns 0 if the page was removed from an LRU list. 110762695a84SNick Piggin * Returns -EBUSY if the page was not on an LRU list. 110862695a84SNick Piggin * 110962695a84SNick Piggin * The returned page will have PageLRU() cleared. If it was found on 1110894bc310SLee Schermerhorn * the active list, it will have PageActive set. If it was found on 1111894bc310SLee Schermerhorn * the unevictable list, it will have the PageUnevictable bit set. That flag 1112894bc310SLee Schermerhorn * may need to be cleared by the caller before letting the page go. 111362695a84SNick Piggin * 111462695a84SNick Piggin * The vmstat statistic corresponding to the list on which the page was 111562695a84SNick Piggin * found will be decremented. 111662695a84SNick Piggin * 111762695a84SNick Piggin * Restrictions: 111862695a84SNick Piggin * (1) Must be called with an elevated refcount on the page. This is a 111962695a84SNick Piggin * fundamentnal difference from isolate_lru_pages (which is called 112062695a84SNick Piggin * without a stable reference). 112162695a84SNick Piggin * (2) the lru_lock must not be held. 112262695a84SNick Piggin * (3) interrupts must be enabled. 112362695a84SNick Piggin */ 112462695a84SNick Piggin int isolate_lru_page(struct page *page) 112562695a84SNick Piggin { 112662695a84SNick Piggin int ret = -EBUSY; 112762695a84SNick Piggin 11280c917313SKonstantin Khlebnikov VM_BUG_ON(!page_count(page)); 11290c917313SKonstantin Khlebnikov 113062695a84SNick Piggin if (PageLRU(page)) { 113162695a84SNick Piggin struct zone *zone = page_zone(page); 113262695a84SNick Piggin 113362695a84SNick Piggin spin_lock_irq(&zone->lru_lock); 11340c917313SKonstantin Khlebnikov if (PageLRU(page)) { 1135894bc310SLee Schermerhorn int lru = page_lru(page); 113662695a84SNick Piggin ret = 0; 11370c917313SKonstantin Khlebnikov get_page(page); 113862695a84SNick Piggin ClearPageLRU(page); 11394f98a2feSRik van Riel 11404f98a2feSRik van Riel del_page_from_lru_list(zone, page, lru); 114162695a84SNick Piggin } 114262695a84SNick Piggin spin_unlock_irq(&zone->lru_lock); 114362695a84SNick Piggin } 114462695a84SNick Piggin return ret; 114562695a84SNick Piggin } 114662695a84SNick Piggin 11475ad333ebSAndy Whitcroft /* 114835cd7815SRik van Riel * Are there way too many processes in the direct reclaim path already? 114935cd7815SRik van Riel */ 115035cd7815SRik van Riel static int too_many_isolated(struct zone *zone, int file, 115135cd7815SRik van Riel struct scan_control *sc) 115235cd7815SRik van Riel { 115335cd7815SRik van Riel unsigned long inactive, isolated; 115435cd7815SRik van Riel 115535cd7815SRik van Riel if (current_is_kswapd()) 115635cd7815SRik van Riel return 0; 115735cd7815SRik van Riel 115889b5fae5SJohannes Weiner if (!global_reclaim(sc)) 115935cd7815SRik van Riel return 0; 116035cd7815SRik van Riel 116135cd7815SRik van Riel if (file) { 116235cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 116335cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_FILE); 116435cd7815SRik van Riel } else { 116535cd7815SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_ANON); 116635cd7815SRik van Riel isolated = zone_page_state(zone, NR_ISOLATED_ANON); 116735cd7815SRik van Riel } 116835cd7815SRik van Riel 116935cd7815SRik van Riel return isolated > inactive; 117035cd7815SRik van Riel } 117135cd7815SRik van Riel 117266635629SMel Gorman static noinline_for_stack void 11733f79768fSHugh Dickins putback_inactive_pages(struct mem_cgroup_zone *mz, 117466635629SMel Gorman struct list_head *page_list) 117566635629SMel Gorman { 1176f16015fbSJohannes Weiner struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 11773f79768fSHugh Dickins struct zone *zone = mz->zone; 11783f79768fSHugh Dickins LIST_HEAD(pages_to_free); 117966635629SMel Gorman 118066635629SMel Gorman /* 118166635629SMel Gorman * Put back any unfreeable pages. 118266635629SMel Gorman */ 118366635629SMel Gorman while (!list_empty(page_list)) { 11843f79768fSHugh Dickins struct page *page = lru_to_page(page_list); 118566635629SMel Gorman int lru; 11863f79768fSHugh Dickins 118766635629SMel Gorman VM_BUG_ON(PageLRU(page)); 118866635629SMel Gorman list_del(&page->lru); 118966635629SMel Gorman if (unlikely(!page_evictable(page, NULL))) { 119066635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 119166635629SMel Gorman putback_lru_page(page); 119266635629SMel Gorman spin_lock_irq(&zone->lru_lock); 119366635629SMel Gorman continue; 119466635629SMel Gorman } 11957a608572SLinus Torvalds SetPageLRU(page); 119666635629SMel Gorman lru = page_lru(page); 11977a608572SLinus Torvalds add_page_to_lru_list(zone, page, lru); 119866635629SMel Gorman if (is_active_lru(lru)) { 119966635629SMel Gorman int file = is_file_lru(lru); 12009992af10SRik van Riel int numpages = hpage_nr_pages(page); 12019992af10SRik van Riel reclaim_stat->recent_rotated[file] += numpages; 120266635629SMel Gorman } 12032bcf8879SHugh Dickins if (put_page_testzero(page)) { 12042bcf8879SHugh Dickins __ClearPageLRU(page); 12052bcf8879SHugh Dickins __ClearPageActive(page); 12062bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 12072bcf8879SHugh Dickins 12082bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 120966635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 12102bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 121166635629SMel Gorman spin_lock_irq(&zone->lru_lock); 12122bcf8879SHugh Dickins } else 12132bcf8879SHugh Dickins list_add(&page->lru, &pages_to_free); 121466635629SMel Gorman } 121566635629SMel Gorman } 121666635629SMel Gorman 12173f79768fSHugh Dickins /* 12183f79768fSHugh Dickins * To save our caller's stack, now use input list for pages to free. 12193f79768fSHugh Dickins */ 12203f79768fSHugh Dickins list_splice(&pages_to_free, page_list); 122166635629SMel Gorman } 122266635629SMel Gorman 1223f16015fbSJohannes Weiner static noinline_for_stack void 1224f16015fbSJohannes Weiner update_isolated_counts(struct mem_cgroup_zone *mz, 12253f79768fSHugh Dickins struct list_head *page_list, 12261489fa14SMel Gorman unsigned long *nr_anon, 12273f79768fSHugh Dickins unsigned long *nr_file) 12281489fa14SMel Gorman { 1229f16015fbSJohannes Weiner struct zone *zone = mz->zone; 12301489fa14SMel Gorman unsigned int count[NR_LRU_LISTS] = { 0, }; 12313f79768fSHugh Dickins unsigned long nr_active = 0; 12323f79768fSHugh Dickins struct page *page; 12333f79768fSHugh Dickins int lru; 12341489fa14SMel Gorman 12353f79768fSHugh Dickins /* 12363f79768fSHugh Dickins * Count pages and clear active flags 12373f79768fSHugh Dickins */ 12383f79768fSHugh Dickins list_for_each_entry(page, page_list, lru) { 12393f79768fSHugh Dickins int numpages = hpage_nr_pages(page); 12403f79768fSHugh Dickins lru = page_lru_base_type(page); 12413f79768fSHugh Dickins if (PageActive(page)) { 12423f79768fSHugh Dickins lru += LRU_ACTIVE; 12433f79768fSHugh Dickins ClearPageActive(page); 12443f79768fSHugh Dickins nr_active += numpages; 12453f79768fSHugh Dickins } 12463f79768fSHugh Dickins count[lru] += numpages; 12473f79768fSHugh Dickins } 12483f79768fSHugh Dickins 1249d563c050SHillf Danton preempt_disable(); 12501489fa14SMel Gorman __count_vm_events(PGDEACTIVATE, nr_active); 12511489fa14SMel Gorman 12521489fa14SMel Gorman __mod_zone_page_state(zone, NR_ACTIVE_FILE, 12531489fa14SMel Gorman -count[LRU_ACTIVE_FILE]); 12541489fa14SMel Gorman __mod_zone_page_state(zone, NR_INACTIVE_FILE, 12551489fa14SMel Gorman -count[LRU_INACTIVE_FILE]); 12561489fa14SMel Gorman __mod_zone_page_state(zone, NR_ACTIVE_ANON, 12571489fa14SMel Gorman -count[LRU_ACTIVE_ANON]); 12581489fa14SMel Gorman __mod_zone_page_state(zone, NR_INACTIVE_ANON, 12591489fa14SMel Gorman -count[LRU_INACTIVE_ANON]); 12601489fa14SMel Gorman 12611489fa14SMel Gorman *nr_anon = count[LRU_ACTIVE_ANON] + count[LRU_INACTIVE_ANON]; 12621489fa14SMel Gorman *nr_file = count[LRU_ACTIVE_FILE] + count[LRU_INACTIVE_FILE]; 12631489fa14SMel Gorman 1264d563c050SHillf Danton __mod_zone_page_state(zone, NR_ISOLATED_ANON, *nr_anon); 1265d563c050SHillf Danton __mod_zone_page_state(zone, NR_ISOLATED_FILE, *nr_file); 1266d563c050SHillf Danton preempt_enable(); 12671489fa14SMel Gorman } 12681489fa14SMel Gorman 126966635629SMel Gorman /* 12701742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 12711742f19fSAndrew Morton * of reclaimed pages 12721da177e4SLinus Torvalds */ 127366635629SMel Gorman static noinline_for_stack unsigned long 1274f16015fbSJohannes Weiner shrink_inactive_list(unsigned long nr_to_scan, struct mem_cgroup_zone *mz, 1275*3cb99451SKonstantin Khlebnikov struct scan_control *sc, int priority, enum lru_list lru) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds LIST_HEAD(page_list); 1278e247dbceSKOSAKI Motohiro unsigned long nr_scanned; 127905ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1280e247dbceSKOSAKI Motohiro unsigned long nr_taken; 1281e247dbceSKOSAKI Motohiro unsigned long nr_anon; 1282e247dbceSKOSAKI Motohiro unsigned long nr_file; 128392df3a72SMel Gorman unsigned long nr_dirty = 0; 128492df3a72SMel Gorman unsigned long nr_writeback = 0; 128561317289SHillf Danton isolate_mode_t isolate_mode = ISOLATE_INACTIVE; 1286*3cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1287f16015fbSJohannes Weiner struct zone *zone = mz->zone; 1288d563c050SHillf Danton struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 128978dc583dSKOSAKI Motohiro 129035cd7815SRik van Riel while (unlikely(too_many_isolated(zone, file, sc))) { 129158355c78SKOSAKI Motohiro congestion_wait(BLK_RW_ASYNC, HZ/10); 129235cd7815SRik van Riel 129335cd7815SRik van Riel /* We are about to die and free our memory. Return now. */ 129435cd7815SRik van Riel if (fatal_signal_pending(current)) 129535cd7815SRik van Riel return SWAP_CLUSTER_MAX; 129635cd7815SRik van Riel } 129735cd7815SRik van Riel 12981da177e4SLinus Torvalds lru_add_drain(); 1299f80c0673SMinchan Kim 1300f80c0673SMinchan Kim if (!sc->may_unmap) 130161317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1302f80c0673SMinchan Kim if (!sc->may_writepage) 130361317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1304f80c0673SMinchan Kim 13051da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 13061da177e4SLinus Torvalds 1307fe2c2a10SRik van Riel nr_taken = isolate_lru_pages(nr_to_scan, mz, &page_list, &nr_scanned, 1308*3cb99451SKonstantin Khlebnikov sc, isolate_mode, lru); 130989b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1310e247dbceSKOSAKI Motohiro zone->pages_scanned += nr_scanned; 1311b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1312b35ea17bSKOSAKI Motohiro __count_zone_vm_events(PGSCAN_KSWAPD, zone, 1313e247dbceSKOSAKI Motohiro nr_scanned); 1314b35ea17bSKOSAKI Motohiro else 1315b35ea17bSKOSAKI Motohiro __count_zone_vm_events(PGSCAN_DIRECT, zone, 1316e247dbceSKOSAKI Motohiro nr_scanned); 1317b35ea17bSKOSAKI Motohiro } 131866635629SMel Gorman spin_unlock_irq(&zone->lru_lock); 1319d563c050SHillf Danton 1320d563c050SHillf Danton if (nr_taken == 0) 132166635629SMel Gorman return 0; 1322b35ea17bSKOSAKI Motohiro 13233f79768fSHugh Dickins update_isolated_counts(mz, &page_list, &nr_anon, &nr_file); 13243f79768fSHugh Dickins 1325f16015fbSJohannes Weiner nr_reclaimed = shrink_page_list(&page_list, mz, sc, priority, 132692df3a72SMel Gorman &nr_dirty, &nr_writeback); 1327c661b078SAndy Whitcroft 13283f79768fSHugh Dickins spin_lock_irq(&zone->lru_lock); 13293f79768fSHugh Dickins 1330d563c050SHillf Danton reclaim_stat->recent_scanned[0] += nr_anon; 1331d563c050SHillf Danton reclaim_stat->recent_scanned[1] += nr_file; 1332d563c050SHillf Danton 1333904249aaSYing Han if (global_reclaim(sc)) { 1334b35ea17bSKOSAKI Motohiro if (current_is_kswapd()) 1335904249aaSYing Han __count_zone_vm_events(PGSTEAL_KSWAPD, zone, 1336904249aaSYing Han nr_reclaimed); 1337904249aaSYing Han else 1338904249aaSYing Han __count_zone_vm_events(PGSTEAL_DIRECT, zone, 1339904249aaSYing Han nr_reclaimed); 1340904249aaSYing Han } 1341a74609faSNick Piggin 13423f79768fSHugh Dickins putback_inactive_pages(mz, &page_list); 13433f79768fSHugh Dickins 13443f79768fSHugh Dickins __mod_zone_page_state(zone, NR_ISOLATED_ANON, -nr_anon); 13453f79768fSHugh Dickins __mod_zone_page_state(zone, NR_ISOLATED_FILE, -nr_file); 13463f79768fSHugh Dickins 13473f79768fSHugh Dickins spin_unlock_irq(&zone->lru_lock); 13483f79768fSHugh Dickins 13493f79768fSHugh Dickins free_hot_cold_page_list(&page_list, 1); 1350e11da5b4SMel Gorman 135192df3a72SMel Gorman /* 135292df3a72SMel Gorman * If reclaim is isolating dirty pages under writeback, it implies 135392df3a72SMel Gorman * that the long-lived page allocation rate is exceeding the page 135492df3a72SMel Gorman * laundering rate. Either the global limits are not being effective 135592df3a72SMel Gorman * at throttling processes due to the page distribution throughout 135692df3a72SMel Gorman * zones or there is heavy usage of a slow backing device. The 135792df3a72SMel Gorman * only option is to throttle from reclaim context which is not ideal 135892df3a72SMel Gorman * as there is no guarantee the dirtying process is throttled in the 135992df3a72SMel Gorman * same way balance_dirty_pages() manages. 136092df3a72SMel Gorman * 136192df3a72SMel Gorman * This scales the number of dirty pages that must be under writeback 136292df3a72SMel Gorman * before throttling depending on priority. It is a simple backoff 136392df3a72SMel Gorman * function that has the most effect in the range DEF_PRIORITY to 136492df3a72SMel Gorman * DEF_PRIORITY-2 which is the priority reclaim is considered to be 136592df3a72SMel Gorman * in trouble and reclaim is considered to be in trouble. 136692df3a72SMel Gorman * 136792df3a72SMel Gorman * DEF_PRIORITY 100% isolated pages must be PageWriteback to throttle 136892df3a72SMel Gorman * DEF_PRIORITY-1 50% must be PageWriteback 136992df3a72SMel Gorman * DEF_PRIORITY-2 25% must be PageWriteback, kswapd in trouble 137092df3a72SMel Gorman * ... 137192df3a72SMel Gorman * DEF_PRIORITY-6 For SWAP_CLUSTER_MAX isolated pages, throttle if any 137292df3a72SMel Gorman * isolated page is PageWriteback 137392df3a72SMel Gorman */ 137492df3a72SMel Gorman if (nr_writeback && nr_writeback >= (nr_taken >> (DEF_PRIORITY-priority))) 137592df3a72SMel Gorman wait_iff_congested(zone, BLK_RW_ASYNC, HZ/10); 137692df3a72SMel Gorman 1377e11da5b4SMel Gorman trace_mm_vmscan_lru_shrink_inactive(zone->zone_pgdat->node_id, 1378e11da5b4SMel Gorman zone_idx(zone), 1379e11da5b4SMel Gorman nr_scanned, nr_reclaimed, 1380e11da5b4SMel Gorman priority, 138123b9da55SMel Gorman trace_shrink_flags(file)); 138205ff5137SAndrew Morton return nr_reclaimed; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13853bb1a852SMartin Bligh /* 13861cfb419bSKAMEZAWA Hiroyuki * This moves pages from the active list to the inactive list. 13871cfb419bSKAMEZAWA Hiroyuki * 13881cfb419bSKAMEZAWA Hiroyuki * We move them the other way if the page is referenced by one or more 13891cfb419bSKAMEZAWA Hiroyuki * processes, from rmap. 13901cfb419bSKAMEZAWA Hiroyuki * 13911cfb419bSKAMEZAWA Hiroyuki * If the pages are mostly unmapped, the processing is fast and it is 13921cfb419bSKAMEZAWA Hiroyuki * appropriate to hold zone->lru_lock across the whole operation. But if 13931cfb419bSKAMEZAWA Hiroyuki * the pages are mapped, the processing is slow (page_referenced()) so we 13941cfb419bSKAMEZAWA Hiroyuki * should drop zone->lru_lock around each page. It's impossible to balance 13951cfb419bSKAMEZAWA Hiroyuki * this, so instead we remove the pages from the LRU while processing them. 13961cfb419bSKAMEZAWA Hiroyuki * It is safe to rely on PG_active against the non-LRU pages in here because 13971cfb419bSKAMEZAWA Hiroyuki * nobody will play with that bit on a non-LRU page. 13981cfb419bSKAMEZAWA Hiroyuki * 13991cfb419bSKAMEZAWA Hiroyuki * The downside is that we have to touch page->_count against each page. 14001cfb419bSKAMEZAWA Hiroyuki * But we had to alter page->flags anyway. 14011cfb419bSKAMEZAWA Hiroyuki */ 14021cfb419bSKAMEZAWA Hiroyuki 14033eb4140fSWu Fengguang static void move_active_pages_to_lru(struct zone *zone, 14043eb4140fSWu Fengguang struct list_head *list, 14052bcf8879SHugh Dickins struct list_head *pages_to_free, 14063eb4140fSWu Fengguang enum lru_list lru) 14073eb4140fSWu Fengguang { 14083eb4140fSWu Fengguang unsigned long pgmoved = 0; 14093eb4140fSWu Fengguang struct page *page; 14103eb4140fSWu Fengguang 14113eb4140fSWu Fengguang while (!list_empty(list)) { 1412925b7673SJohannes Weiner struct lruvec *lruvec; 1413925b7673SJohannes Weiner 14143eb4140fSWu Fengguang page = lru_to_page(list); 14153eb4140fSWu Fengguang 14163eb4140fSWu Fengguang VM_BUG_ON(PageLRU(page)); 14173eb4140fSWu Fengguang SetPageLRU(page); 14183eb4140fSWu Fengguang 1419925b7673SJohannes Weiner lruvec = mem_cgroup_lru_add_list(zone, page, lru); 1420925b7673SJohannes Weiner list_move(&page->lru, &lruvec->lists[lru]); 14212c888cfbSRik van Riel pgmoved += hpage_nr_pages(page); 14223eb4140fSWu Fengguang 14232bcf8879SHugh Dickins if (put_page_testzero(page)) { 14242bcf8879SHugh Dickins __ClearPageLRU(page); 14252bcf8879SHugh Dickins __ClearPageActive(page); 14262bcf8879SHugh Dickins del_page_from_lru_list(zone, page, lru); 14272bcf8879SHugh Dickins 14282bcf8879SHugh Dickins if (unlikely(PageCompound(page))) { 14293eb4140fSWu Fengguang spin_unlock_irq(&zone->lru_lock); 14302bcf8879SHugh Dickins (*get_compound_page_dtor(page))(page); 14313eb4140fSWu Fengguang spin_lock_irq(&zone->lru_lock); 14322bcf8879SHugh Dickins } else 14332bcf8879SHugh Dickins list_add(&page->lru, pages_to_free); 14343eb4140fSWu Fengguang } 14353eb4140fSWu Fengguang } 14363eb4140fSWu Fengguang __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); 14373eb4140fSWu Fengguang if (!is_active_lru(lru)) 14383eb4140fSWu Fengguang __count_vm_events(PGDEACTIVATE, pgmoved); 14393eb4140fSWu Fengguang } 14401cfb419bSKAMEZAWA Hiroyuki 1441f626012dSHugh Dickins static void shrink_active_list(unsigned long nr_to_scan, 1442f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 1443f16015fbSJohannes Weiner struct scan_control *sc, 1444*3cb99451SKonstantin Khlebnikov int priority, enum lru_list lru) 14451cfb419bSKAMEZAWA Hiroyuki { 144644c241f1SKOSAKI Motohiro unsigned long nr_taken; 1447f626012dSHugh Dickins unsigned long nr_scanned; 14486fe6b7e3SWu Fengguang unsigned long vm_flags; 14491cfb419bSKAMEZAWA Hiroyuki LIST_HEAD(l_hold); /* The pages which were snipped off */ 14508cab4754SWu Fengguang LIST_HEAD(l_active); 1451b69408e8SChristoph Lameter LIST_HEAD(l_inactive); 14521cfb419bSKAMEZAWA Hiroyuki struct page *page; 1453f16015fbSJohannes Weiner struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 145444c241f1SKOSAKI Motohiro unsigned long nr_rotated = 0; 145561317289SHillf Danton isolate_mode_t isolate_mode = ISOLATE_ACTIVE; 1456*3cb99451SKonstantin Khlebnikov int file = is_file_lru(lru); 1457f16015fbSJohannes Weiner struct zone *zone = mz->zone; 14581cfb419bSKAMEZAWA Hiroyuki 14591da177e4SLinus Torvalds lru_add_drain(); 1460f80c0673SMinchan Kim 1461f80c0673SMinchan Kim if (!sc->may_unmap) 146261317289SHillf Danton isolate_mode |= ISOLATE_UNMAPPED; 1463f80c0673SMinchan Kim if (!sc->may_writepage) 146461317289SHillf Danton isolate_mode |= ISOLATE_CLEAN; 1465f80c0673SMinchan Kim 14661da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 1467925b7673SJohannes Weiner 1468fe2c2a10SRik van Riel nr_taken = isolate_lru_pages(nr_to_scan, mz, &l_hold, &nr_scanned, sc, 1469*3cb99451SKonstantin Khlebnikov isolate_mode, lru); 147089b5fae5SJohannes Weiner if (global_reclaim(sc)) 1471f626012dSHugh Dickins zone->pages_scanned += nr_scanned; 147289b5fae5SJohannes Weiner 1473b7c46d15SJohannes Weiner reclaim_stat->recent_scanned[file] += nr_taken; 14741cfb419bSKAMEZAWA Hiroyuki 1475f626012dSHugh Dickins __count_zone_vm_events(PGREFILL, zone, nr_scanned); 1476*3cb99451SKonstantin Khlebnikov __mod_zone_page_state(zone, NR_LRU_BASE + lru, -nr_taken); 1477a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, nr_taken); 14781da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 14811da177e4SLinus Torvalds cond_resched(); 14821da177e4SLinus Torvalds page = lru_to_page(&l_hold); 14831da177e4SLinus Torvalds list_del(&page->lru); 14847e9cd484SRik van Riel 1485894bc310SLee Schermerhorn if (unlikely(!page_evictable(page, NULL))) { 1486894bc310SLee Schermerhorn putback_lru_page(page); 1487894bc310SLee Schermerhorn continue; 1488894bc310SLee Schermerhorn } 1489894bc310SLee Schermerhorn 1490cc715d99SMel Gorman if (unlikely(buffer_heads_over_limit)) { 1491cc715d99SMel Gorman if (page_has_private(page) && trylock_page(page)) { 1492cc715d99SMel Gorman if (page_has_private(page)) 1493cc715d99SMel Gorman try_to_release_page(page, 0); 1494cc715d99SMel Gorman unlock_page(page); 1495cc715d99SMel Gorman } 1496cc715d99SMel Gorman } 1497cc715d99SMel Gorman 1498c3ac9a8aSJohannes Weiner if (page_referenced(page, 0, sc->target_mem_cgroup, 1499c3ac9a8aSJohannes Weiner &vm_flags)) { 15009992af10SRik van Riel nr_rotated += hpage_nr_pages(page); 15018cab4754SWu Fengguang /* 15028cab4754SWu Fengguang * Identify referenced, file-backed active pages and 15038cab4754SWu Fengguang * give them one more trip around the active list. So 15048cab4754SWu Fengguang * that executable code get better chances to stay in 15058cab4754SWu Fengguang * memory under moderate memory pressure. Anon pages 15068cab4754SWu Fengguang * are not likely to be evicted by use-once streaming 15078cab4754SWu Fengguang * IO, plus JVM can create lots of anon VM_EXEC pages, 15088cab4754SWu Fengguang * so we ignore them here. 15098cab4754SWu Fengguang */ 151041e20983SWu Fengguang if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) { 15118cab4754SWu Fengguang list_add(&page->lru, &l_active); 15128cab4754SWu Fengguang continue; 15138cab4754SWu Fengguang } 15148cab4754SWu Fengguang } 15157e9cd484SRik van Riel 15165205e56eSKOSAKI Motohiro ClearPageActive(page); /* we are de-activating */ 15171da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 1520b555749aSAndrew Morton /* 15218cab4754SWu Fengguang * Move pages back to the lru list. 1522b555749aSAndrew Morton */ 15232a1dc509SJohannes Weiner spin_lock_irq(&zone->lru_lock); 15244f98a2feSRik van Riel /* 15258cab4754SWu Fengguang * Count referenced pages from currently used mappings as rotated, 15268cab4754SWu Fengguang * even though only some of them are actually re-activated. This 15278cab4754SWu Fengguang * helps balance scan pressure between file and anonymous pages in 15288cab4754SWu Fengguang * get_scan_ratio. 1529556adecbSRik van Riel */ 1530b7c46d15SJohannes Weiner reclaim_stat->recent_rotated[file] += nr_rotated; 1531556adecbSRik van Riel 1532*3cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_active, &l_hold, lru); 1533*3cb99451SKonstantin Khlebnikov move_active_pages_to_lru(zone, &l_inactive, &l_hold, lru - LRU_ACTIVE); 1534a731286dSKOSAKI Motohiro __mod_zone_page_state(zone, NR_ISOLATED_ANON + file, -nr_taken); 1535f8891e5eSChristoph Lameter spin_unlock_irq(&zone->lru_lock); 15362bcf8879SHugh Dickins 15372bcf8879SHugh Dickins free_hot_cold_page_list(&l_hold, 1); 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 154074e3f3c3SMinchan Kim #ifdef CONFIG_SWAP 154114797e23SKOSAKI Motohiro static int inactive_anon_is_low_global(struct zone *zone) 1542f89eb90eSKOSAKI Motohiro { 1543f89eb90eSKOSAKI Motohiro unsigned long active, inactive; 1544f89eb90eSKOSAKI Motohiro 1545f89eb90eSKOSAKI Motohiro active = zone_page_state(zone, NR_ACTIVE_ANON); 1546f89eb90eSKOSAKI Motohiro inactive = zone_page_state(zone, NR_INACTIVE_ANON); 1547f89eb90eSKOSAKI Motohiro 1548f89eb90eSKOSAKI Motohiro if (inactive * zone->inactive_ratio < active) 1549f89eb90eSKOSAKI Motohiro return 1; 1550f89eb90eSKOSAKI Motohiro 1551f89eb90eSKOSAKI Motohiro return 0; 1552f89eb90eSKOSAKI Motohiro } 1553f89eb90eSKOSAKI Motohiro 155414797e23SKOSAKI Motohiro /** 155514797e23SKOSAKI Motohiro * inactive_anon_is_low - check if anonymous pages need to be deactivated 155614797e23SKOSAKI Motohiro * @zone: zone to check 155714797e23SKOSAKI Motohiro * @sc: scan control of this context 155814797e23SKOSAKI Motohiro * 155914797e23SKOSAKI Motohiro * Returns true if the zone does not have enough inactive anon pages, 156014797e23SKOSAKI Motohiro * meaning some active anon pages need to be deactivated. 156114797e23SKOSAKI Motohiro */ 1562f16015fbSJohannes Weiner static int inactive_anon_is_low(struct mem_cgroup_zone *mz) 156314797e23SKOSAKI Motohiro { 156474e3f3c3SMinchan Kim /* 156574e3f3c3SMinchan Kim * If we don't have swap space, anonymous page deactivation 156674e3f3c3SMinchan Kim * is pointless. 156774e3f3c3SMinchan Kim */ 156874e3f3c3SMinchan Kim if (!total_swap_pages) 156974e3f3c3SMinchan Kim return 0; 157074e3f3c3SMinchan Kim 1571c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1572f16015fbSJohannes Weiner return mem_cgroup_inactive_anon_is_low(mz->mem_cgroup, 1573f16015fbSJohannes Weiner mz->zone); 1574f16015fbSJohannes Weiner 1575f16015fbSJohannes Weiner return inactive_anon_is_low_global(mz->zone); 157614797e23SKOSAKI Motohiro } 157774e3f3c3SMinchan Kim #else 1578f16015fbSJohannes Weiner static inline int inactive_anon_is_low(struct mem_cgroup_zone *mz) 157974e3f3c3SMinchan Kim { 158074e3f3c3SMinchan Kim return 0; 158174e3f3c3SMinchan Kim } 158274e3f3c3SMinchan Kim #endif 158314797e23SKOSAKI Motohiro 158456e49d21SRik van Riel static int inactive_file_is_low_global(struct zone *zone) 158556e49d21SRik van Riel { 158656e49d21SRik van Riel unsigned long active, inactive; 158756e49d21SRik van Riel 158856e49d21SRik van Riel active = zone_page_state(zone, NR_ACTIVE_FILE); 158956e49d21SRik van Riel inactive = zone_page_state(zone, NR_INACTIVE_FILE); 159056e49d21SRik van Riel 159156e49d21SRik van Riel return (active > inactive); 159256e49d21SRik van Riel } 159356e49d21SRik van Riel 159456e49d21SRik van Riel /** 159556e49d21SRik van Riel * inactive_file_is_low - check if file pages need to be deactivated 1596f16015fbSJohannes Weiner * @mz: memory cgroup and zone to check 159756e49d21SRik van Riel * 159856e49d21SRik van Riel * When the system is doing streaming IO, memory pressure here 159956e49d21SRik van Riel * ensures that active file pages get deactivated, until more 160056e49d21SRik van Riel * than half of the file pages are on the inactive list. 160156e49d21SRik van Riel * 160256e49d21SRik van Riel * Once we get to that situation, protect the system's working 160356e49d21SRik van Riel * set from being evicted by disabling active file page aging. 160456e49d21SRik van Riel * 160556e49d21SRik van Riel * This uses a different ratio than the anonymous pages, because 160656e49d21SRik van Riel * the page cache uses a use-once replacement algorithm. 160756e49d21SRik van Riel */ 1608f16015fbSJohannes Weiner static int inactive_file_is_low(struct mem_cgroup_zone *mz) 160956e49d21SRik van Riel { 1610c3c787e8SHugh Dickins if (!mem_cgroup_disabled()) 1611f16015fbSJohannes Weiner return mem_cgroup_inactive_file_is_low(mz->mem_cgroup, 1612f16015fbSJohannes Weiner mz->zone); 161356e49d21SRik van Riel 1614f16015fbSJohannes Weiner return inactive_file_is_low_global(mz->zone); 161556e49d21SRik van Riel } 161656e49d21SRik van Riel 1617f16015fbSJohannes Weiner static int inactive_list_is_low(struct mem_cgroup_zone *mz, int file) 1618b39415b2SRik van Riel { 1619b39415b2SRik van Riel if (file) 1620f16015fbSJohannes Weiner return inactive_file_is_low(mz); 1621b39415b2SRik van Riel else 1622f16015fbSJohannes Weiner return inactive_anon_is_low(mz); 1623b39415b2SRik van Riel } 1624b39415b2SRik van Riel 16254f98a2feSRik van Riel static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, 1626f16015fbSJohannes Weiner struct mem_cgroup_zone *mz, 1627f16015fbSJohannes Weiner struct scan_control *sc, int priority) 1628b69408e8SChristoph Lameter { 16294f98a2feSRik van Riel int file = is_file_lru(lru); 16304f98a2feSRik van Riel 1631b39415b2SRik van Riel if (is_active_lru(lru)) { 1632f16015fbSJohannes Weiner if (inactive_list_is_low(mz, file)) 1633*3cb99451SKonstantin Khlebnikov shrink_active_list(nr_to_scan, mz, sc, priority, lru); 1634556adecbSRik van Riel return 0; 1635556adecbSRik van Riel } 1636556adecbSRik van Riel 1637*3cb99451SKonstantin Khlebnikov return shrink_inactive_list(nr_to_scan, mz, sc, priority, lru); 1638b69408e8SChristoph Lameter } 1639b69408e8SChristoph Lameter 1640f16015fbSJohannes Weiner static int vmscan_swappiness(struct mem_cgroup_zone *mz, 1641f16015fbSJohannes Weiner struct scan_control *sc) 16421f4c025bSKAMEZAWA Hiroyuki { 164389b5fae5SJohannes Weiner if (global_reclaim(sc)) 16441f4c025bSKAMEZAWA Hiroyuki return vm_swappiness; 1645f16015fbSJohannes Weiner return mem_cgroup_swappiness(mz->mem_cgroup); 16461f4c025bSKAMEZAWA Hiroyuki } 16471f4c025bSKAMEZAWA Hiroyuki 16481da177e4SLinus Torvalds /* 16494f98a2feSRik van Riel * Determine how aggressively the anon and file LRU lists should be 16504f98a2feSRik van Riel * scanned. The relative value of each set of LRU lists is determined 16514f98a2feSRik van Riel * by looking at the fraction of the pages scanned we did rotate back 16524f98a2feSRik van Riel * onto the active list instead of evict. 16534f98a2feSRik van Riel * 165476a33fc3SShaohua Li * nr[0] = anon pages to scan; nr[1] = file pages to scan 16554f98a2feSRik van Riel */ 1656f16015fbSJohannes Weiner static void get_scan_count(struct mem_cgroup_zone *mz, struct scan_control *sc, 165776a33fc3SShaohua Li unsigned long *nr, int priority) 16584f98a2feSRik van Riel { 16594f98a2feSRik van Riel unsigned long anon, file, free; 16604f98a2feSRik van Riel unsigned long anon_prio, file_prio; 16614f98a2feSRik van Riel unsigned long ap, fp; 1662f16015fbSJohannes Weiner struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(mz); 166376a33fc3SShaohua Li u64 fraction[2], denominator; 16644111304dSHugh Dickins enum lru_list lru; 166576a33fc3SShaohua Li int noswap = 0; 1666a4d3e9e7SJohannes Weiner bool force_scan = false; 1667246e87a9SKAMEZAWA Hiroyuki 1668f11c0ca5SJohannes Weiner /* 1669f11c0ca5SJohannes Weiner * If the zone or memcg is small, nr[l] can be 0. This 1670f11c0ca5SJohannes Weiner * results in no scanning on this priority and a potential 1671f11c0ca5SJohannes Weiner * priority drop. Global direct reclaim can go to the next 1672f11c0ca5SJohannes Weiner * zone and tends to have no problems. Global kswapd is for 1673f11c0ca5SJohannes Weiner * zone balancing and it needs to scan a minimum amount. When 1674f11c0ca5SJohannes Weiner * reclaiming for a memcg, a priority drop can cause high 1675f11c0ca5SJohannes Weiner * latencies, so it's better to scan a minimum amount there as 1676f11c0ca5SJohannes Weiner * well. 1677f11c0ca5SJohannes Weiner */ 1678b95a2f2dSJohannes Weiner if (current_is_kswapd() && mz->zone->all_unreclaimable) 1679a4d3e9e7SJohannes Weiner force_scan = true; 168089b5fae5SJohannes Weiner if (!global_reclaim(sc)) 1681a4d3e9e7SJohannes Weiner force_scan = true; 168276a33fc3SShaohua Li 168376a33fc3SShaohua Li /* If we have no swap space, do not bother scanning anon pages. */ 168476a33fc3SShaohua Li if (!sc->may_swap || (nr_swap_pages <= 0)) { 168576a33fc3SShaohua Li noswap = 1; 168676a33fc3SShaohua Li fraction[0] = 0; 168776a33fc3SShaohua Li fraction[1] = 1; 168876a33fc3SShaohua Li denominator = 1; 168976a33fc3SShaohua Li goto out; 169076a33fc3SShaohua Li } 16914f98a2feSRik van Riel 1692f16015fbSJohannes Weiner anon = zone_nr_lru_pages(mz, LRU_ACTIVE_ANON) + 1693f16015fbSJohannes Weiner zone_nr_lru_pages(mz, LRU_INACTIVE_ANON); 1694f16015fbSJohannes Weiner file = zone_nr_lru_pages(mz, LRU_ACTIVE_FILE) + 1695f16015fbSJohannes Weiner zone_nr_lru_pages(mz, LRU_INACTIVE_FILE); 1696a4d3e9e7SJohannes Weiner 169789b5fae5SJohannes Weiner if (global_reclaim(sc)) { 1698f16015fbSJohannes Weiner free = zone_page_state(mz->zone, NR_FREE_PAGES); 1699eeee9a8cSKOSAKI Motohiro /* If we have very few page cache pages, 1700eeee9a8cSKOSAKI Motohiro force-scan anon pages. */ 1701f16015fbSJohannes Weiner if (unlikely(file + free <= high_wmark_pages(mz->zone))) { 170276a33fc3SShaohua Li fraction[0] = 1; 170376a33fc3SShaohua Li fraction[1] = 0; 170476a33fc3SShaohua Li denominator = 1; 170576a33fc3SShaohua Li goto out; 17064f98a2feSRik van Riel } 1707eeee9a8cSKOSAKI Motohiro } 17084f98a2feSRik van Riel 17094f98a2feSRik van Riel /* 171058c37f6eSKOSAKI Motohiro * With swappiness at 100, anonymous and file have the same priority. 171158c37f6eSKOSAKI Motohiro * This scanning priority is essentially the inverse of IO cost. 171258c37f6eSKOSAKI Motohiro */ 1713f16015fbSJohannes Weiner anon_prio = vmscan_swappiness(mz, sc); 1714f16015fbSJohannes Weiner file_prio = 200 - vmscan_swappiness(mz, sc); 171558c37f6eSKOSAKI Motohiro 171658c37f6eSKOSAKI Motohiro /* 17174f98a2feSRik van Riel * OK, so we have swap space and a fair amount of page cache 17184f98a2feSRik van Riel * pages. We use the recently rotated / recently scanned 17194f98a2feSRik van Riel * ratios to determine how valuable each cache is. 17204f98a2feSRik van Riel * 17214f98a2feSRik van Riel * Because workloads change over time (and to avoid overflow) 17224f98a2feSRik van Riel * we keep these statistics as a floating average, which ends 17234f98a2feSRik van Riel * up weighing recent references more than old ones. 17244f98a2feSRik van Riel * 17254f98a2feSRik van Riel * anon in [0], file in [1] 17264f98a2feSRik van Riel */ 1727f16015fbSJohannes Weiner spin_lock_irq(&mz->zone->lru_lock); 172858c37f6eSKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { 17296e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[0] /= 2; 17306e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[0] /= 2; 17314f98a2feSRik van Riel } 17324f98a2feSRik van Riel 17336e901571SKOSAKI Motohiro if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { 17346e901571SKOSAKI Motohiro reclaim_stat->recent_scanned[1] /= 2; 17356e901571SKOSAKI Motohiro reclaim_stat->recent_rotated[1] /= 2; 17364f98a2feSRik van Riel } 17374f98a2feSRik van Riel 17384f98a2feSRik van Riel /* 173900d8089cSRik van Riel * The amount of pressure on anon vs file pages is inversely 174000d8089cSRik van Riel * proportional to the fraction of recently scanned pages on 174100d8089cSRik van Riel * each list that were recently referenced and in active use. 17424f98a2feSRik van Riel */ 1743fe35004fSSatoru Moriya ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1); 17446e901571SKOSAKI Motohiro ap /= reclaim_stat->recent_rotated[0] + 1; 17454f98a2feSRik van Riel 1746fe35004fSSatoru Moriya fp = file_prio * (reclaim_stat->recent_scanned[1] + 1); 17476e901571SKOSAKI Motohiro fp /= reclaim_stat->recent_rotated[1] + 1; 1748f16015fbSJohannes Weiner spin_unlock_irq(&mz->zone->lru_lock); 17494f98a2feSRik van Riel 175076a33fc3SShaohua Li fraction[0] = ap; 175176a33fc3SShaohua Li fraction[1] = fp; 175276a33fc3SShaohua Li denominator = ap + fp + 1; 175376a33fc3SShaohua Li out: 17544111304dSHugh Dickins for_each_evictable_lru(lru) { 17554111304dSHugh Dickins int file = is_file_lru(lru); 175676a33fc3SShaohua Li unsigned long scan; 175776a33fc3SShaohua Li 17584111304dSHugh Dickins scan = zone_nr_lru_pages(mz, lru); 1759fe35004fSSatoru Moriya if (priority || noswap || !vmscan_swappiness(mz, sc)) { 176076a33fc3SShaohua Li scan >>= priority; 1761f11c0ca5SJohannes Weiner if (!scan && force_scan) 1762f11c0ca5SJohannes Weiner scan = SWAP_CLUSTER_MAX; 176376a33fc3SShaohua Li scan = div64_u64(scan * fraction[file], denominator); 17644f98a2feSRik van Riel } 17654111304dSHugh Dickins nr[lru] = scan; 176676a33fc3SShaohua Li } 17676e08a369SWu Fengguang } 17684f98a2feSRik van Riel 176923b9da55SMel Gorman /* Use reclaim/compaction for costly allocs or under memory pressure */ 177023b9da55SMel Gorman static bool in_reclaim_compaction(int priority, struct scan_control *sc) 177123b9da55SMel Gorman { 177223b9da55SMel Gorman if (COMPACTION_BUILD && sc->order && 177323b9da55SMel Gorman (sc->order > PAGE_ALLOC_COSTLY_ORDER || 177423b9da55SMel Gorman priority < DEF_PRIORITY - 2)) 177523b9da55SMel Gorman return true; 177623b9da55SMel Gorman 177723b9da55SMel Gorman return false; 177823b9da55SMel Gorman } 177923b9da55SMel Gorman 17804f98a2feSRik van Riel /* 178123b9da55SMel Gorman * Reclaim/compaction is used for high-order allocation requests. It reclaims 178223b9da55SMel Gorman * order-0 pages before compacting the zone. should_continue_reclaim() returns 178323b9da55SMel Gorman * true if more pages should be reclaimed such that when the page allocator 178423b9da55SMel Gorman * calls try_to_compact_zone() that it will have enough free pages to succeed. 178523b9da55SMel Gorman * It will give up earlier than that if there is difficulty reclaiming pages. 17863e7d3449SMel Gorman */ 1787f16015fbSJohannes Weiner static inline bool should_continue_reclaim(struct mem_cgroup_zone *mz, 17883e7d3449SMel Gorman unsigned long nr_reclaimed, 17893e7d3449SMel Gorman unsigned long nr_scanned, 179023b9da55SMel Gorman int priority, 17913e7d3449SMel Gorman struct scan_control *sc) 17923e7d3449SMel Gorman { 17933e7d3449SMel Gorman unsigned long pages_for_compaction; 17943e7d3449SMel Gorman unsigned long inactive_lru_pages; 17953e7d3449SMel Gorman 17963e7d3449SMel Gorman /* If not in reclaim/compaction mode, stop */ 179723b9da55SMel Gorman if (!in_reclaim_compaction(priority, sc)) 17983e7d3449SMel Gorman return false; 17993e7d3449SMel Gorman 18002876592fSMel Gorman /* Consider stopping depending on scan and reclaim activity */ 18012876592fSMel Gorman if (sc->gfp_mask & __GFP_REPEAT) { 18023e7d3449SMel Gorman /* 18032876592fSMel Gorman * For __GFP_REPEAT allocations, stop reclaiming if the 18042876592fSMel Gorman * full LRU list has been scanned and we are still failing 18052876592fSMel Gorman * to reclaim pages. This full LRU scan is potentially 18062876592fSMel Gorman * expensive but a __GFP_REPEAT caller really wants to succeed 18073e7d3449SMel Gorman */ 18083e7d3449SMel Gorman if (!nr_reclaimed && !nr_scanned) 18093e7d3449SMel Gorman return false; 18102876592fSMel Gorman } else { 18112876592fSMel Gorman /* 18122876592fSMel Gorman * For non-__GFP_REPEAT allocations which can presumably 18132876592fSMel Gorman * fail without consequence, stop if we failed to reclaim 18142876592fSMel Gorman * any pages from the last SWAP_CLUSTER_MAX number of 18152876592fSMel Gorman * pages that were scanned. This will return to the 18162876592fSMel Gorman * caller faster at the risk reclaim/compaction and 18172876592fSMel Gorman * the resulting allocation attempt fails 18182876592fSMel Gorman */ 18192876592fSMel Gorman if (!nr_reclaimed) 18202876592fSMel Gorman return false; 18212876592fSMel Gorman } 18223e7d3449SMel Gorman 18233e7d3449SMel Gorman /* 18243e7d3449SMel Gorman * If we have not reclaimed enough pages for compaction and the 18253e7d3449SMel Gorman * inactive lists are large enough, continue reclaiming 18263e7d3449SMel Gorman */ 18273e7d3449SMel Gorman pages_for_compaction = (2UL << sc->order); 1828f16015fbSJohannes Weiner inactive_lru_pages = zone_nr_lru_pages(mz, LRU_INACTIVE_FILE); 182986cfd3a4SMinchan Kim if (nr_swap_pages > 0) 1830f16015fbSJohannes Weiner inactive_lru_pages += zone_nr_lru_pages(mz, LRU_INACTIVE_ANON); 18313e7d3449SMel Gorman if (sc->nr_reclaimed < pages_for_compaction && 18323e7d3449SMel Gorman inactive_lru_pages > pages_for_compaction) 18333e7d3449SMel Gorman return true; 18343e7d3449SMel Gorman 18353e7d3449SMel Gorman /* If compaction would go ahead or the allocation would succeed, stop */ 1836f16015fbSJohannes Weiner switch (compaction_suitable(mz->zone, sc->order)) { 18373e7d3449SMel Gorman case COMPACT_PARTIAL: 18383e7d3449SMel Gorman case COMPACT_CONTINUE: 18393e7d3449SMel Gorman return false; 18403e7d3449SMel Gorman default: 18413e7d3449SMel Gorman return true; 18423e7d3449SMel Gorman } 18433e7d3449SMel Gorman } 18443e7d3449SMel Gorman 18453e7d3449SMel Gorman /* 18461da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 18471da177e4SLinus Torvalds */ 1848f16015fbSJohannes Weiner static void shrink_mem_cgroup_zone(int priority, struct mem_cgroup_zone *mz, 184969e05944SAndrew Morton struct scan_control *sc) 18501da177e4SLinus Torvalds { 1851b69408e8SChristoph Lameter unsigned long nr[NR_LRU_LISTS]; 18528695949aSChristoph Lameter unsigned long nr_to_scan; 18534111304dSHugh Dickins enum lru_list lru; 1854f0fdc5e8SJohannes Weiner unsigned long nr_reclaimed, nr_scanned; 185522fba335SKOSAKI Motohiro unsigned long nr_to_reclaim = sc->nr_to_reclaim; 18563da367c3SShaohua Li struct blk_plug plug; 18571da177e4SLinus Torvalds 18583e7d3449SMel Gorman restart: 18593e7d3449SMel Gorman nr_reclaimed = 0; 1860f0fdc5e8SJohannes Weiner nr_scanned = sc->nr_scanned; 1861f16015fbSJohannes Weiner get_scan_count(mz, sc, nr, priority); 18621cfb419bSKAMEZAWA Hiroyuki 18633da367c3SShaohua Li blk_start_plug(&plug); 1864556adecbSRik van Riel while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || 1865556adecbSRik van Riel nr[LRU_INACTIVE_FILE]) { 18664111304dSHugh Dickins for_each_evictable_lru(lru) { 18674111304dSHugh Dickins if (nr[lru]) { 1868ece74b2eSKOSAKI Motohiro nr_to_scan = min_t(unsigned long, 18694111304dSHugh Dickins nr[lru], SWAP_CLUSTER_MAX); 18704111304dSHugh Dickins nr[lru] -= nr_to_scan; 1871b69408e8SChristoph Lameter 18724111304dSHugh Dickins nr_reclaimed += shrink_list(lru, nr_to_scan, 1873f16015fbSJohannes Weiner mz, sc, priority); 18741da177e4SLinus Torvalds } 18751da177e4SLinus Torvalds } 1876a79311c1SRik van Riel /* 1877a79311c1SRik van Riel * On large memory systems, scan >> priority can become 1878a79311c1SRik van Riel * really large. This is fine for the starting priority; 1879a79311c1SRik van Riel * we want to put equal scanning pressure on each zone. 1880a79311c1SRik van Riel * However, if the VM has a harder time of freeing pages, 1881a79311c1SRik van Riel * with multiple processes reclaiming pages, the total 1882a79311c1SRik van Riel * freeing target can get unreasonably large. 1883a79311c1SRik van Riel */ 188441c93088SYing Han if (nr_reclaimed >= nr_to_reclaim && priority < DEF_PRIORITY) 1885a79311c1SRik van Riel break; 18861da177e4SLinus Torvalds } 18873da367c3SShaohua Li blk_finish_plug(&plug); 18883e7d3449SMel Gorman sc->nr_reclaimed += nr_reclaimed; 188901dbe5c9SKOSAKI Motohiro 1890556adecbSRik van Riel /* 1891556adecbSRik van Riel * Even if we did not try to evict anon pages at all, we want to 1892556adecbSRik van Riel * rebalance the anon lru active/inactive ratio. 1893556adecbSRik van Riel */ 1894f16015fbSJohannes Weiner if (inactive_anon_is_low(mz)) 1895*3cb99451SKonstantin Khlebnikov shrink_active_list(SWAP_CLUSTER_MAX, mz, 1896*3cb99451SKonstantin Khlebnikov sc, priority, LRU_ACTIVE_ANON); 1897556adecbSRik van Riel 18983e7d3449SMel Gorman /* reclaim/compaction might need reclaim to continue */ 1899f16015fbSJohannes Weiner if (should_continue_reclaim(mz, nr_reclaimed, 190023b9da55SMel Gorman sc->nr_scanned - nr_scanned, 190123b9da55SMel Gorman priority, sc)) 19023e7d3449SMel Gorman goto restart; 19033e7d3449SMel Gorman 1904232ea4d6SAndrew Morton throttle_vm_writeout(sc->gfp_mask); 19051da177e4SLinus Torvalds } 19061da177e4SLinus Torvalds 1907f16015fbSJohannes Weiner static void shrink_zone(int priority, struct zone *zone, 1908f16015fbSJohannes Weiner struct scan_control *sc) 1909f16015fbSJohannes Weiner { 19105660048cSJohannes Weiner struct mem_cgroup *root = sc->target_mem_cgroup; 19115660048cSJohannes Weiner struct mem_cgroup_reclaim_cookie reclaim = { 19125660048cSJohannes Weiner .zone = zone, 19135660048cSJohannes Weiner .priority = priority, 19145660048cSJohannes Weiner }; 19155660048cSJohannes Weiner struct mem_cgroup *memcg; 19165660048cSJohannes Weiner 19175660048cSJohannes Weiner memcg = mem_cgroup_iter(root, NULL, &reclaim); 19185660048cSJohannes Weiner do { 19195660048cSJohannes Weiner struct mem_cgroup_zone mz = { 19205660048cSJohannes Weiner .mem_cgroup = memcg, 19215660048cSJohannes Weiner .zone = zone, 19225660048cSJohannes Weiner }; 19235660048cSJohannes Weiner 19245660048cSJohannes Weiner shrink_mem_cgroup_zone(priority, &mz, sc); 19255660048cSJohannes Weiner /* 19265660048cSJohannes Weiner * Limit reclaim has historically picked one memcg and 19275660048cSJohannes Weiner * scanned it with decreasing priority levels until 19285660048cSJohannes Weiner * nr_to_reclaim had been reclaimed. This priority 19295660048cSJohannes Weiner * cycle is thus over after a single memcg. 1930b95a2f2dSJohannes Weiner * 1931b95a2f2dSJohannes Weiner * Direct reclaim and kswapd, on the other hand, have 1932b95a2f2dSJohannes Weiner * to scan all memory cgroups to fulfill the overall 1933b95a2f2dSJohannes Weiner * scan target for the zone. 19345660048cSJohannes Weiner */ 19355660048cSJohannes Weiner if (!global_reclaim(sc)) { 19365660048cSJohannes Weiner mem_cgroup_iter_break(root, memcg); 19375660048cSJohannes Weiner break; 19385660048cSJohannes Weiner } 19395660048cSJohannes Weiner memcg = mem_cgroup_iter(root, memcg, &reclaim); 19405660048cSJohannes Weiner } while (memcg); 1941f16015fbSJohannes Weiner } 1942f16015fbSJohannes Weiner 1943fe4b1b24SMel Gorman /* Returns true if compaction should go ahead for a high-order request */ 1944fe4b1b24SMel Gorman static inline bool compaction_ready(struct zone *zone, struct scan_control *sc) 1945fe4b1b24SMel Gorman { 1946fe4b1b24SMel Gorman unsigned long balance_gap, watermark; 1947fe4b1b24SMel Gorman bool watermark_ok; 1948fe4b1b24SMel Gorman 1949fe4b1b24SMel Gorman /* Do not consider compaction for orders reclaim is meant to satisfy */ 1950fe4b1b24SMel Gorman if (sc->order <= PAGE_ALLOC_COSTLY_ORDER) 1951fe4b1b24SMel Gorman return false; 1952fe4b1b24SMel Gorman 1953fe4b1b24SMel Gorman /* 1954fe4b1b24SMel Gorman * Compaction takes time to run and there are potentially other 1955fe4b1b24SMel Gorman * callers using the pages just freed. Continue reclaiming until 1956fe4b1b24SMel Gorman * there is a buffer of free pages available to give compaction 1957fe4b1b24SMel Gorman * a reasonable chance of completing and allocating the page 1958fe4b1b24SMel Gorman */ 1959fe4b1b24SMel Gorman balance_gap = min(low_wmark_pages(zone), 1960fe4b1b24SMel Gorman (zone->present_pages + KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 1961fe4b1b24SMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 1962fe4b1b24SMel Gorman watermark = high_wmark_pages(zone) + balance_gap + (2UL << sc->order); 1963fe4b1b24SMel Gorman watermark_ok = zone_watermark_ok_safe(zone, 0, watermark, 0, 0); 1964fe4b1b24SMel Gorman 1965fe4b1b24SMel Gorman /* 1966fe4b1b24SMel Gorman * If compaction is deferred, reclaim up to a point where 1967fe4b1b24SMel Gorman * compaction will have a chance of success when re-enabled 1968fe4b1b24SMel Gorman */ 1969aff62249SRik van Riel if (compaction_deferred(zone, sc->order)) 1970fe4b1b24SMel Gorman return watermark_ok; 1971fe4b1b24SMel Gorman 1972fe4b1b24SMel Gorman /* If compaction is not ready to start, keep reclaiming */ 1973fe4b1b24SMel Gorman if (!compaction_suitable(zone, sc->order)) 1974fe4b1b24SMel Gorman return false; 1975fe4b1b24SMel Gorman 1976fe4b1b24SMel Gorman return watermark_ok; 1977fe4b1b24SMel Gorman } 1978fe4b1b24SMel Gorman 19791da177e4SLinus Torvalds /* 19801da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 19811da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 19821da177e4SLinus Torvalds * request. 19831da177e4SLinus Torvalds * 198441858966SMel Gorman * We reclaim from a zone even if that zone is over high_wmark_pages(zone). 198541858966SMel Gorman * Because: 19861da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 19871da177e4SLinus Torvalds * allocation or 198841858966SMel Gorman * b) The target zone may be at high_wmark_pages(zone) but the lower zones 198941858966SMel Gorman * must go *over* high_wmark_pages(zone) to satisfy the `incremental min' 199041858966SMel Gorman * zone defense algorithm. 19911da177e4SLinus Torvalds * 19921da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 19931da177e4SLinus Torvalds * scan then give up on it. 1994e0c23279SMel Gorman * 1995e0c23279SMel Gorman * This function returns true if a zone is being reclaimed for a costly 1996fe4b1b24SMel Gorman * high-order allocation and compaction is ready to begin. This indicates to 19970cee34fdSMel Gorman * the caller that it should consider retrying the allocation instead of 19980cee34fdSMel Gorman * further reclaim. 19991da177e4SLinus Torvalds */ 2000e0c23279SMel Gorman static bool shrink_zones(int priority, struct zonelist *zonelist, 200169e05944SAndrew Morton struct scan_control *sc) 20021da177e4SLinus Torvalds { 2003dd1a239fSMel Gorman struct zoneref *z; 200454a6eb5cSMel Gorman struct zone *zone; 2005d149e3b2SYing Han unsigned long nr_soft_reclaimed; 2006d149e3b2SYing Han unsigned long nr_soft_scanned; 20070cee34fdSMel Gorman bool aborted_reclaim = false; 20081cfb419bSKAMEZAWA Hiroyuki 2009cc715d99SMel Gorman /* 2010cc715d99SMel Gorman * If the number of buffer_heads in the machine exceeds the maximum 2011cc715d99SMel Gorman * allowed level, force direct reclaim to scan the highmem zone as 2012cc715d99SMel Gorman * highmem pages could be pinning lowmem pages storing buffer_heads 2013cc715d99SMel Gorman */ 2014cc715d99SMel Gorman if (buffer_heads_over_limit) 2015cc715d99SMel Gorman sc->gfp_mask |= __GFP_HIGHMEM; 2016cc715d99SMel Gorman 2017d4debc66SMel Gorman for_each_zone_zonelist_nodemask(zone, z, zonelist, 2018d4debc66SMel Gorman gfp_zone(sc->gfp_mask), sc->nodemask) { 2019f3fe6512SCon Kolivas if (!populated_zone(zone)) 20201da177e4SLinus Torvalds continue; 20211cfb419bSKAMEZAWA Hiroyuki /* 20221cfb419bSKAMEZAWA Hiroyuki * Take care memory controller reclaiming has small influence 20231cfb419bSKAMEZAWA Hiroyuki * to global LRU. 20241cfb419bSKAMEZAWA Hiroyuki */ 202589b5fae5SJohannes Weiner if (global_reclaim(sc)) { 202602a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 20271da177e4SLinus Torvalds continue; 202893e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable && priority != DEF_PRIORITY) 20291da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 2030e0887c19SRik van Riel if (COMPACTION_BUILD) { 2031e0887c19SRik van Riel /* 2032e0c23279SMel Gorman * If we already have plenty of memory free for 2033e0c23279SMel Gorman * compaction in this zone, don't free any more. 2034e0c23279SMel Gorman * Even though compaction is invoked for any 2035e0c23279SMel Gorman * non-zero order, only frequent costly order 2036e0c23279SMel Gorman * reclamation is disruptive enough to become a 2037c7cfa37bSCopot Alexandru * noticeable problem, like transparent huge 2038c7cfa37bSCopot Alexandru * page allocations. 2039e0887c19SRik van Riel */ 2040fe4b1b24SMel Gorman if (compaction_ready(zone, sc)) { 20410cee34fdSMel Gorman aborted_reclaim = true; 2042e0887c19SRik van Riel continue; 2043e0887c19SRik van Riel } 2044e0c23279SMel Gorman } 2045ac34a1a3SKAMEZAWA Hiroyuki /* 2046ac34a1a3SKAMEZAWA Hiroyuki * This steals pages from memory cgroups over softlimit 2047ac34a1a3SKAMEZAWA Hiroyuki * and returns the number of reclaimed pages and 2048ac34a1a3SKAMEZAWA Hiroyuki * scanned pages. This works for global memory pressure 2049ac34a1a3SKAMEZAWA Hiroyuki * and balancing, not for a memcg's limit. 2050ac34a1a3SKAMEZAWA Hiroyuki */ 2051d149e3b2SYing Han nr_soft_scanned = 0; 2052d149e3b2SYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 2053d149e3b2SYing Han sc->order, sc->gfp_mask, 2054d149e3b2SYing Han &nr_soft_scanned); 2055d149e3b2SYing Han sc->nr_reclaimed += nr_soft_reclaimed; 2056ac34a1a3SKAMEZAWA Hiroyuki sc->nr_scanned += nr_soft_scanned; 2057ac34a1a3SKAMEZAWA Hiroyuki /* need some check for avoid more shrink_zone() */ 2058ac34a1a3SKAMEZAWA Hiroyuki } 2059d149e3b2SYing Han 2060a79311c1SRik van Riel shrink_zone(priority, zone, sc); 20611da177e4SLinus Torvalds } 2062e0c23279SMel Gorman 20630cee34fdSMel Gorman return aborted_reclaim; 2064d1908362SMinchan Kim } 2065d1908362SMinchan Kim 2066d1908362SMinchan Kim static bool zone_reclaimable(struct zone *zone) 2067d1908362SMinchan Kim { 2068d1908362SMinchan Kim return zone->pages_scanned < zone_reclaimable_pages(zone) * 6; 2069d1908362SMinchan Kim } 2070d1908362SMinchan Kim 2071929bea7cSKOSAKI Motohiro /* All zones in zonelist are unreclaimable? */ 2072d1908362SMinchan Kim static bool all_unreclaimable(struct zonelist *zonelist, 2073d1908362SMinchan Kim struct scan_control *sc) 2074d1908362SMinchan Kim { 2075d1908362SMinchan Kim struct zoneref *z; 2076d1908362SMinchan Kim struct zone *zone; 2077d1908362SMinchan Kim 2078d1908362SMinchan Kim for_each_zone_zonelist_nodemask(zone, z, zonelist, 2079d1908362SMinchan Kim gfp_zone(sc->gfp_mask), sc->nodemask) { 2080d1908362SMinchan Kim if (!populated_zone(zone)) 2081d1908362SMinchan Kim continue; 2082d1908362SMinchan Kim if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2083d1908362SMinchan Kim continue; 2084929bea7cSKOSAKI Motohiro if (!zone->all_unreclaimable) 2085929bea7cSKOSAKI Motohiro return false; 2086d1908362SMinchan Kim } 2087d1908362SMinchan Kim 2088929bea7cSKOSAKI Motohiro return true; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 20911da177e4SLinus Torvalds /* 20921da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 20931da177e4SLinus Torvalds * 20941da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 20951da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 20961da177e4SLinus Torvalds * 20971da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 20981da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 20995b0830cbSJens Axboe * caller can't do much about. We kick the writeback threads and take explicit 21005b0830cbSJens Axboe * naps in the hope that some of these pages can be written. But if the 21015b0830cbSJens Axboe * allocating task holds filesystem locks which prevent writeout this might not 21025b0830cbSJens Axboe * work, and the allocation attempt will fail. 2103a41f24eaSNishanth Aravamudan * 2104a41f24eaSNishanth Aravamudan * returns: 0, if no pages reclaimed 2105a41f24eaSNishanth Aravamudan * else, the number of pages reclaimed 21061da177e4SLinus Torvalds */ 2107dac1d27bSMel Gorman static unsigned long do_try_to_free_pages(struct zonelist *zonelist, 2108a09ed5e0SYing Han struct scan_control *sc, 2109a09ed5e0SYing Han struct shrink_control *shrink) 21101da177e4SLinus Torvalds { 21111da177e4SLinus Torvalds int priority; 211269e05944SAndrew Morton unsigned long total_scanned = 0; 21131da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 2114dd1a239fSMel Gorman struct zoneref *z; 211554a6eb5cSMel Gorman struct zone *zone; 211622fba335SKOSAKI Motohiro unsigned long writeback_threshold; 21170cee34fdSMel Gorman bool aborted_reclaim; 21181da177e4SLinus Torvalds 2119873b4771SKeika Kobayashi delayacct_freepages_start(); 2120873b4771SKeika Kobayashi 212189b5fae5SJohannes Weiner if (global_reclaim(sc)) 2122f8891e5eSChristoph Lameter count_vm_event(ALLOCSTALL); 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 212566e1707bSBalbir Singh sc->nr_scanned = 0; 21260cee34fdSMel Gorman aborted_reclaim = shrink_zones(priority, zonelist, sc); 2127e0c23279SMel Gorman 212866e1707bSBalbir Singh /* 212966e1707bSBalbir Singh * Don't shrink slabs when reclaiming memory from 213066e1707bSBalbir Singh * over limit cgroups 213166e1707bSBalbir Singh */ 213289b5fae5SJohannes Weiner if (global_reclaim(sc)) { 2133c6a8a8c5SKOSAKI Motohiro unsigned long lru_pages = 0; 2134d4debc66SMel Gorman for_each_zone_zonelist(zone, z, zonelist, 2135d4debc66SMel Gorman gfp_zone(sc->gfp_mask)) { 2136c6a8a8c5SKOSAKI Motohiro if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 2137c6a8a8c5SKOSAKI Motohiro continue; 2138c6a8a8c5SKOSAKI Motohiro 2139c6a8a8c5SKOSAKI Motohiro lru_pages += zone_reclaimable_pages(zone); 2140c6a8a8c5SKOSAKI Motohiro } 2141c6a8a8c5SKOSAKI Motohiro 21421495f230SYing Han shrink_slab(shrink, sc->nr_scanned, lru_pages); 21431da177e4SLinus Torvalds if (reclaim_state) { 2144a79311c1SRik van Riel sc->nr_reclaimed += reclaim_state->reclaimed_slab; 21451da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 21461da177e4SLinus Torvalds } 214791a45470SKAMEZAWA Hiroyuki } 214866e1707bSBalbir Singh total_scanned += sc->nr_scanned; 2149bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed >= sc->nr_to_reclaim) 21501da177e4SLinus Torvalds goto out; 21511da177e4SLinus Torvalds 21521da177e4SLinus Torvalds /* 21531da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 21541da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 21551da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 21561da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 21571da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 21581da177e4SLinus Torvalds */ 215922fba335SKOSAKI Motohiro writeback_threshold = sc->nr_to_reclaim + sc->nr_to_reclaim / 2; 216022fba335SKOSAKI Motohiro if (total_scanned > writeback_threshold) { 21610e175a18SCurt Wohlgemuth wakeup_flusher_threads(laptop_mode ? 0 : total_scanned, 21620e175a18SCurt Wohlgemuth WB_REASON_TRY_TO_FREE_PAGES); 216366e1707bSBalbir Singh sc->may_writepage = 1; 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 21677b51755cSKOSAKI Motohiro if (!sc->hibernation_mode && sc->nr_scanned && 21680e093d99SMel Gorman priority < DEF_PRIORITY - 2) { 21690e093d99SMel Gorman struct zone *preferred_zone; 21700e093d99SMel Gorman 21710e093d99SMel Gorman first_zones_zonelist(zonelist, gfp_zone(sc->gfp_mask), 2172f33261d7SDavid Rientjes &cpuset_current_mems_allowed, 2173f33261d7SDavid Rientjes &preferred_zone); 21740e093d99SMel Gorman wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/10); 21750e093d99SMel Gorman } 21761da177e4SLinus Torvalds } 2177bb21c7ceSKOSAKI Motohiro 21781da177e4SLinus Torvalds out: 2179873b4771SKeika Kobayashi delayacct_freepages_end(); 2180873b4771SKeika Kobayashi 2181bb21c7ceSKOSAKI Motohiro if (sc->nr_reclaimed) 2182bb21c7ceSKOSAKI Motohiro return sc->nr_reclaimed; 2183bb21c7ceSKOSAKI Motohiro 2184929bea7cSKOSAKI Motohiro /* 2185929bea7cSKOSAKI Motohiro * As hibernation is going on, kswapd is freezed so that it can't mark 2186929bea7cSKOSAKI Motohiro * the zone into all_unreclaimable. Thus bypassing all_unreclaimable 2187929bea7cSKOSAKI Motohiro * check. 2188929bea7cSKOSAKI Motohiro */ 2189929bea7cSKOSAKI Motohiro if (oom_killer_disabled) 2190929bea7cSKOSAKI Motohiro return 0; 2191929bea7cSKOSAKI Motohiro 21920cee34fdSMel Gorman /* Aborted reclaim to try compaction? don't OOM, then */ 21930cee34fdSMel Gorman if (aborted_reclaim) 21947335084dSMel Gorman return 1; 21957335084dSMel Gorman 2196bb21c7ceSKOSAKI Motohiro /* top priority shrink_zones still had more to do? don't OOM, then */ 219789b5fae5SJohannes Weiner if (global_reclaim(sc) && !all_unreclaimable(zonelist, sc)) 2198bb21c7ceSKOSAKI Motohiro return 1; 2199bb21c7ceSKOSAKI Motohiro 2200bb21c7ceSKOSAKI Motohiro return 0; 22011da177e4SLinus Torvalds } 22021da177e4SLinus Torvalds 2203dac1d27bSMel Gorman unsigned long try_to_free_pages(struct zonelist *zonelist, int order, 2204327c0e96SKAMEZAWA Hiroyuki gfp_t gfp_mask, nodemask_t *nodemask) 220566e1707bSBalbir Singh { 220633906bc5SMel Gorman unsigned long nr_reclaimed; 220766e1707bSBalbir Singh struct scan_control sc = { 220866e1707bSBalbir Singh .gfp_mask = gfp_mask, 220966e1707bSBalbir Singh .may_writepage = !laptop_mode, 221022fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 2211a6dc60f8SJohannes Weiner .may_unmap = 1, 22122e2e4259SKOSAKI Motohiro .may_swap = 1, 221366e1707bSBalbir Singh .order = order, 2214f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2215327c0e96SKAMEZAWA Hiroyuki .nodemask = nodemask, 221666e1707bSBalbir Singh }; 2217a09ed5e0SYing Han struct shrink_control shrink = { 2218a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2219a09ed5e0SYing Han }; 222066e1707bSBalbir Singh 222133906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_begin(order, 222233906bc5SMel Gorman sc.may_writepage, 222333906bc5SMel Gorman gfp_mask); 222433906bc5SMel Gorman 2225a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 222633906bc5SMel Gorman 222733906bc5SMel Gorman trace_mm_vmscan_direct_reclaim_end(nr_reclaimed); 222833906bc5SMel Gorman 222933906bc5SMel Gorman return nr_reclaimed; 223066e1707bSBalbir Singh } 223166e1707bSBalbir Singh 223200f0b825SBalbir Singh #ifdef CONFIG_CGROUP_MEM_RES_CTLR 223366e1707bSBalbir Singh 223472835c86SJohannes Weiner unsigned long mem_cgroup_shrink_node_zone(struct mem_cgroup *memcg, 22354e416953SBalbir Singh gfp_t gfp_mask, bool noswap, 22360ae5e89cSYing Han struct zone *zone, 22370ae5e89cSYing Han unsigned long *nr_scanned) 22384e416953SBalbir Singh { 22394e416953SBalbir Singh struct scan_control sc = { 22400ae5e89cSYing Han .nr_scanned = 0, 2241b8f5c566SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 22424e416953SBalbir Singh .may_writepage = !laptop_mode, 22434e416953SBalbir Singh .may_unmap = 1, 22444e416953SBalbir Singh .may_swap = !noswap, 22454e416953SBalbir Singh .order = 0, 224672835c86SJohannes Weiner .target_mem_cgroup = memcg, 22474e416953SBalbir Singh }; 22485660048cSJohannes Weiner struct mem_cgroup_zone mz = { 224972835c86SJohannes Weiner .mem_cgroup = memcg, 22505660048cSJohannes Weiner .zone = zone, 22515660048cSJohannes Weiner }; 22520ae5e89cSYing Han 22534e416953SBalbir Singh sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 22544e416953SBalbir Singh (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); 2255bdce6d9eSKOSAKI Motohiro 2256bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_begin(0, 2257bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2258bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2259bdce6d9eSKOSAKI Motohiro 22604e416953SBalbir Singh /* 22614e416953SBalbir Singh * NOTE: Although we can get the priority field, using it 22624e416953SBalbir Singh * here is not a good idea, since it limits the pages we can scan. 22634e416953SBalbir Singh * if we don't reclaim here, the shrink_zone from balance_pgdat 22644e416953SBalbir Singh * will pick up pages from other mem cgroup's as well. We hack 22654e416953SBalbir Singh * the priority and make it zero. 22664e416953SBalbir Singh */ 22675660048cSJohannes Weiner shrink_mem_cgroup_zone(0, &mz, &sc); 2268bdce6d9eSKOSAKI Motohiro 2269bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed); 2270bdce6d9eSKOSAKI Motohiro 22710ae5e89cSYing Han *nr_scanned = sc.nr_scanned; 22724e416953SBalbir Singh return sc.nr_reclaimed; 22734e416953SBalbir Singh } 22744e416953SBalbir Singh 227572835c86SJohannes Weiner unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg, 22768c7c6e34SKAMEZAWA Hiroyuki gfp_t gfp_mask, 2277185efc0fSJohannes Weiner bool noswap) 227866e1707bSBalbir Singh { 22794e416953SBalbir Singh struct zonelist *zonelist; 2280bdce6d9eSKOSAKI Motohiro unsigned long nr_reclaimed; 2281889976dbSYing Han int nid; 228266e1707bSBalbir Singh struct scan_control sc = { 228366e1707bSBalbir Singh .may_writepage = !laptop_mode, 2284a6dc60f8SJohannes Weiner .may_unmap = 1, 22852e2e4259SKOSAKI Motohiro .may_swap = !noswap, 228622fba335SKOSAKI Motohiro .nr_to_reclaim = SWAP_CLUSTER_MAX, 228766e1707bSBalbir Singh .order = 0, 228872835c86SJohannes Weiner .target_mem_cgroup = memcg, 2289327c0e96SKAMEZAWA Hiroyuki .nodemask = NULL, /* we don't care the placement */ 2290a09ed5e0SYing Han .gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | 2291a09ed5e0SYing Han (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK), 2292a09ed5e0SYing Han }; 2293a09ed5e0SYing Han struct shrink_control shrink = { 2294a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 229566e1707bSBalbir Singh }; 229666e1707bSBalbir Singh 2297889976dbSYing Han /* 2298889976dbSYing Han * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't 2299889976dbSYing Han * take care of from where we get pages. So the node where we start the 2300889976dbSYing Han * scan does not need to be the current node. 2301889976dbSYing Han */ 230272835c86SJohannes Weiner nid = mem_cgroup_select_victim_node(memcg); 2303889976dbSYing Han 2304889976dbSYing Han zonelist = NODE_DATA(nid)->node_zonelists; 2305bdce6d9eSKOSAKI Motohiro 2306bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_begin(0, 2307bdce6d9eSKOSAKI Motohiro sc.may_writepage, 2308bdce6d9eSKOSAKI Motohiro sc.gfp_mask); 2309bdce6d9eSKOSAKI Motohiro 2310a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2311bdce6d9eSKOSAKI Motohiro 2312bdce6d9eSKOSAKI Motohiro trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed); 2313bdce6d9eSKOSAKI Motohiro 2314bdce6d9eSKOSAKI Motohiro return nr_reclaimed; 231566e1707bSBalbir Singh } 231666e1707bSBalbir Singh #endif 231766e1707bSBalbir Singh 2318f16015fbSJohannes Weiner static void age_active_anon(struct zone *zone, struct scan_control *sc, 2319f16015fbSJohannes Weiner int priority) 2320f16015fbSJohannes Weiner { 2321b95a2f2dSJohannes Weiner struct mem_cgroup *memcg; 2322b95a2f2dSJohannes Weiner 2323b95a2f2dSJohannes Weiner if (!total_swap_pages) 2324b95a2f2dSJohannes Weiner return; 2325b95a2f2dSJohannes Weiner 2326b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, NULL, NULL); 2327b95a2f2dSJohannes Weiner do { 2328f16015fbSJohannes Weiner struct mem_cgroup_zone mz = { 2329b95a2f2dSJohannes Weiner .mem_cgroup = memcg, 2330f16015fbSJohannes Weiner .zone = zone, 2331f16015fbSJohannes Weiner }; 2332f16015fbSJohannes Weiner 2333f16015fbSJohannes Weiner if (inactive_anon_is_low(&mz)) 2334b95a2f2dSJohannes Weiner shrink_active_list(SWAP_CLUSTER_MAX, &mz, 2335*3cb99451SKonstantin Khlebnikov sc, priority, LRU_ACTIVE_ANON); 2336b95a2f2dSJohannes Weiner 2337b95a2f2dSJohannes Weiner memcg = mem_cgroup_iter(NULL, memcg, NULL); 2338b95a2f2dSJohannes Weiner } while (memcg); 2339f16015fbSJohannes Weiner } 2340f16015fbSJohannes Weiner 23411741c877SMel Gorman /* 23421741c877SMel Gorman * pgdat_balanced is used when checking if a node is balanced for high-order 23431741c877SMel Gorman * allocations. Only zones that meet watermarks and are in a zone allowed 23441741c877SMel Gorman * by the callers classzone_idx are added to balanced_pages. The total of 23451741c877SMel Gorman * balanced pages must be at least 25% of the zones allowed by classzone_idx 23461741c877SMel Gorman * for the node to be considered balanced. Forcing all zones to be balanced 23471741c877SMel Gorman * for high orders can cause excessive reclaim when there are imbalanced zones. 23481741c877SMel Gorman * The choice of 25% is due to 23491741c877SMel Gorman * o a 16M DMA zone that is balanced will not balance a zone on any 23501741c877SMel Gorman * reasonable sized machine 23511741c877SMel Gorman * o On all other machines, the top zone must be at least a reasonable 235225985edcSLucas De Marchi * percentage of the middle zones. For example, on 32-bit x86, highmem 23531741c877SMel Gorman * would need to be at least 256M for it to be balance a whole node. 23541741c877SMel Gorman * Similarly, on x86-64 the Normal zone would need to be at least 1G 23551741c877SMel Gorman * to balance a node on its own. These seemed like reasonable ratios. 23561741c877SMel Gorman */ 23571741c877SMel Gorman static bool pgdat_balanced(pg_data_t *pgdat, unsigned long balanced_pages, 23581741c877SMel Gorman int classzone_idx) 23591741c877SMel Gorman { 23601741c877SMel Gorman unsigned long present_pages = 0; 23611741c877SMel Gorman int i; 23621741c877SMel Gorman 23631741c877SMel Gorman for (i = 0; i <= classzone_idx; i++) 23641741c877SMel Gorman present_pages += pgdat->node_zones[i].present_pages; 23651741c877SMel Gorman 23664746efdeSShaohua Li /* A special case here: if zone has no page, we think it's balanced */ 23674746efdeSShaohua Li return balanced_pages >= (present_pages >> 2); 23681741c877SMel Gorman } 23691741c877SMel Gorman 2370f50de2d3SMel Gorman /* is kswapd sleeping prematurely? */ 2371dc83edd9SMel Gorman static bool sleeping_prematurely(pg_data_t *pgdat, int order, long remaining, 2372dc83edd9SMel Gorman int classzone_idx) 2373f50de2d3SMel Gorman { 2374bb3ab596SKOSAKI Motohiro int i; 23751741c877SMel Gorman unsigned long balanced = 0; 23761741c877SMel Gorman bool all_zones_ok = true; 2377f50de2d3SMel Gorman 2378f50de2d3SMel Gorman /* If a direct reclaimer woke kswapd within HZ/10, it's premature */ 2379f50de2d3SMel Gorman if (remaining) 2380dc83edd9SMel Gorman return true; 2381f50de2d3SMel Gorman 23820abdee2bSMel Gorman /* Check the watermark levels */ 238308951e54SMel Gorman for (i = 0; i <= classzone_idx; i++) { 2384bb3ab596SKOSAKI Motohiro struct zone *zone = pgdat->node_zones + i; 2385bb3ab596SKOSAKI Motohiro 2386bb3ab596SKOSAKI Motohiro if (!populated_zone(zone)) 2387bb3ab596SKOSAKI Motohiro continue; 2388bb3ab596SKOSAKI Motohiro 2389355b09c4SMel Gorman /* 2390355b09c4SMel Gorman * balance_pgdat() skips over all_unreclaimable after 2391355b09c4SMel Gorman * DEF_PRIORITY. Effectively, it considers them balanced so 2392355b09c4SMel Gorman * they must be considered balanced here as well if kswapd 2393355b09c4SMel Gorman * is to sleep 2394355b09c4SMel Gorman */ 2395355b09c4SMel Gorman if (zone->all_unreclaimable) { 2396355b09c4SMel Gorman balanced += zone->present_pages; 2397de3fab39SKOSAKI Motohiro continue; 2398355b09c4SMel Gorman } 2399de3fab39SKOSAKI Motohiro 240088f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, high_wmark_pages(zone), 2401da175d06SMel Gorman i, 0)) 24021741c877SMel Gorman all_zones_ok = false; 24031741c877SMel Gorman else 24041741c877SMel Gorman balanced += zone->present_pages; 2405bb3ab596SKOSAKI Motohiro } 2406f50de2d3SMel Gorman 24071741c877SMel Gorman /* 24081741c877SMel Gorman * For high-order requests, the balanced zones must contain at least 24091741c877SMel Gorman * 25% of the nodes pages for kswapd to sleep. For order-0, all zones 24101741c877SMel Gorman * must be balanced 24111741c877SMel Gorman */ 24121741c877SMel Gorman if (order) 2413afc7e326SJohannes Weiner return !pgdat_balanced(pgdat, balanced, classzone_idx); 24141741c877SMel Gorman else 24151741c877SMel Gorman return !all_zones_ok; 2416f50de2d3SMel Gorman } 2417f50de2d3SMel Gorman 24181da177e4SLinus Torvalds /* 24191da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 242041858966SMel Gorman * they are all at high_wmark_pages(zone). 24211da177e4SLinus Torvalds * 24220abdee2bSMel Gorman * Returns the final order kswapd was reclaiming at 24231da177e4SLinus Torvalds * 24241da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 24251da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 24261da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 24271da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 24281da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 24291da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 24301da177e4SLinus Torvalds * the zone for when the problem goes away. 24311da177e4SLinus Torvalds * 24321da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 243341858966SMel Gorman * zones which have free_pages > high_wmark_pages(zone), but once a zone is 243441858966SMel Gorman * found to have free_pages <= high_wmark_pages(zone), we scan that zone and the 243541858966SMel Gorman * lower zones regardless of the number of free pages in the lower zones. This 243641858966SMel Gorman * interoperates with the page allocator fallback scheme to ensure that aging 243741858966SMel Gorman * of pages is balanced across the zones. 24381da177e4SLinus Torvalds */ 243999504748SMel Gorman static unsigned long balance_pgdat(pg_data_t *pgdat, int order, 2440dc83edd9SMel Gorman int *classzone_idx) 24411da177e4SLinus Torvalds { 24421da177e4SLinus Torvalds int all_zones_ok; 24431741c877SMel Gorman unsigned long balanced; 24441da177e4SLinus Torvalds int priority; 24451da177e4SLinus Torvalds int i; 244699504748SMel Gorman int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 244769e05944SAndrew Morton unsigned long total_scanned; 24481da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 24490ae5e89cSYing Han unsigned long nr_soft_reclaimed; 24500ae5e89cSYing Han unsigned long nr_soft_scanned; 2451179e9639SAndrew Morton struct scan_control sc = { 2452179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 2453a6dc60f8SJohannes Weiner .may_unmap = 1, 24542e2e4259SKOSAKI Motohiro .may_swap = 1, 245522fba335SKOSAKI Motohiro /* 245622fba335SKOSAKI Motohiro * kswapd doesn't want to be bailed out while reclaim. because 245722fba335SKOSAKI Motohiro * we want to put equal scanning pressure on each zone. 245822fba335SKOSAKI Motohiro */ 245922fba335SKOSAKI Motohiro .nr_to_reclaim = ULONG_MAX, 24605ad333ebSAndy Whitcroft .order = order, 2461f16015fbSJohannes Weiner .target_mem_cgroup = NULL, 2462179e9639SAndrew Morton }; 2463a09ed5e0SYing Han struct shrink_control shrink = { 2464a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2465a09ed5e0SYing Han }; 24661da177e4SLinus Torvalds loop_again: 24671da177e4SLinus Torvalds total_scanned = 0; 2468a79311c1SRik van Riel sc.nr_reclaimed = 0; 2469c0bbbc73SChristoph Lameter sc.may_writepage = !laptop_mode; 2470f8891e5eSChristoph Lameter count_vm_event(PAGEOUTRUN); 24711da177e4SLinus Torvalds 24721da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 24731da177e4SLinus Torvalds unsigned long lru_pages = 0; 2474bb3ab596SKOSAKI Motohiro int has_under_min_watermark_zone = 0; 24751da177e4SLinus Torvalds 24761da177e4SLinus Torvalds all_zones_ok = 1; 24771741c877SMel Gorman balanced = 0; 24781da177e4SLinus Torvalds 24791da177e4SLinus Torvalds /* 24801da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 24811da177e4SLinus Torvalds * zone which needs scanning 24821da177e4SLinus Torvalds */ 24831da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 24841da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 24851da177e4SLinus Torvalds 2486f3fe6512SCon Kolivas if (!populated_zone(zone)) 24871da177e4SLinus Torvalds continue; 24881da177e4SLinus Torvalds 248993e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable && priority != DEF_PRIORITY) 24901da177e4SLinus Torvalds continue; 24911da177e4SLinus Torvalds 2492556adecbSRik van Riel /* 2493556adecbSRik van Riel * Do some background aging of the anon list, to give 2494556adecbSRik van Riel * pages a chance to be referenced before reclaiming. 2495556adecbSRik van Riel */ 2496f16015fbSJohannes Weiner age_active_anon(zone, &sc, priority); 2497556adecbSRik van Riel 2498cc715d99SMel Gorman /* 2499cc715d99SMel Gorman * If the number of buffer_heads in the machine 2500cc715d99SMel Gorman * exceeds the maximum allowed level and this node 2501cc715d99SMel Gorman * has a highmem zone, force kswapd to reclaim from 2502cc715d99SMel Gorman * it to relieve lowmem pressure. 2503cc715d99SMel Gorman */ 2504cc715d99SMel Gorman if (buffer_heads_over_limit && is_highmem_idx(i)) { 2505cc715d99SMel Gorman end_zone = i; 2506cc715d99SMel Gorman break; 2507cc715d99SMel Gorman } 2508cc715d99SMel Gorman 250988f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 251041858966SMel Gorman high_wmark_pages(zone), 0, 0)) { 25111da177e4SLinus Torvalds end_zone = i; 2512e1dbeda6SAndrew Morton break; 2513439423f6SShaohua Li } else { 2514439423f6SShaohua Li /* If balanced, clear the congested flag */ 2515439423f6SShaohua Li zone_clear_flag(zone, ZONE_CONGESTED); 25161da177e4SLinus Torvalds } 25171da177e4SLinus Torvalds } 2518e1dbeda6SAndrew Morton if (i < 0) 25191da177e4SLinus Torvalds goto out; 2520e1dbeda6SAndrew Morton 25211da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 25221da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 25231da177e4SLinus Torvalds 2524adea02a1SWu Fengguang lru_pages += zone_reclaimable_pages(zone); 25251da177e4SLinus Torvalds } 25261da177e4SLinus Torvalds 25271da177e4SLinus Torvalds /* 25281da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 25291da177e4SLinus Torvalds * at the last zone which needs scanning. 25301da177e4SLinus Torvalds * 25311da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 25321da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 25331da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 25341da177e4SLinus Torvalds * cause too much scanning of the lower zones. 25351da177e4SLinus Torvalds */ 25361da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 25371da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 2538fe2c2a10SRik van Riel int nr_slab, testorder; 25398afdceceSMel Gorman unsigned long balance_gap; 25401da177e4SLinus Torvalds 2541f3fe6512SCon Kolivas if (!populated_zone(zone)) 25421da177e4SLinus Torvalds continue; 25431da177e4SLinus Torvalds 254493e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable && priority != DEF_PRIORITY) 25451da177e4SLinus Torvalds continue; 25461da177e4SLinus Torvalds 25471da177e4SLinus Torvalds sc.nr_scanned = 0; 25484e416953SBalbir Singh 25490ae5e89cSYing Han nr_soft_scanned = 0; 25504e416953SBalbir Singh /* 25514e416953SBalbir Singh * Call soft limit reclaim before calling shrink_zone. 25524e416953SBalbir Singh */ 25530ae5e89cSYing Han nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone, 25540ae5e89cSYing Han order, sc.gfp_mask, 25550ae5e89cSYing Han &nr_soft_scanned); 25560ae5e89cSYing Han sc.nr_reclaimed += nr_soft_reclaimed; 25570ae5e89cSYing Han total_scanned += nr_soft_scanned; 255800918b6aSKOSAKI Motohiro 255932a4330dSRik van Riel /* 25608afdceceSMel Gorman * We put equal pressure on every zone, unless 25618afdceceSMel Gorman * one zone has way too many pages free 25628afdceceSMel Gorman * already. The "too many pages" is defined 25638afdceceSMel Gorman * as the high wmark plus a "gap" where the 25648afdceceSMel Gorman * gap is either the low watermark or 1% 25658afdceceSMel Gorman * of the zone, whichever is smaller. 256632a4330dSRik van Riel */ 25678afdceceSMel Gorman balance_gap = min(low_wmark_pages(zone), 25688afdceceSMel Gorman (zone->present_pages + 25698afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO-1) / 25708afdceceSMel Gorman KSWAPD_ZONE_BALANCE_GAP_RATIO); 2571fe2c2a10SRik van Riel /* 2572fe2c2a10SRik van Riel * Kswapd reclaims only single pages with compaction 2573fe2c2a10SRik van Riel * enabled. Trying too hard to reclaim until contiguous 2574fe2c2a10SRik van Riel * free pages have become available can hurt performance 2575fe2c2a10SRik van Riel * by evicting too much useful data from memory. 2576fe2c2a10SRik van Riel * Do not reclaim more than needed for compaction. 2577fe2c2a10SRik van Riel */ 2578fe2c2a10SRik van Riel testorder = order; 2579fe2c2a10SRik van Riel if (COMPACTION_BUILD && order && 2580fe2c2a10SRik van Riel compaction_suitable(zone, order) != 2581fe2c2a10SRik van Riel COMPACT_SKIPPED) 2582fe2c2a10SRik van Riel testorder = 0; 2583fe2c2a10SRik van Riel 2584cc715d99SMel Gorman if ((buffer_heads_over_limit && is_highmem_idx(i)) || 2585643ac9fcSHugh Dickins !zone_watermark_ok_safe(zone, testorder, 25868afdceceSMel Gorman high_wmark_pages(zone) + balance_gap, 2587d7868daeSMel Gorman end_zone, 0)) { 2588a79311c1SRik van Riel shrink_zone(priority, zone, &sc); 2589d7868daeSMel Gorman 25901da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 25911495f230SYing Han nr_slab = shrink_slab(&shrink, sc.nr_scanned, lru_pages); 2592a79311c1SRik van Riel sc.nr_reclaimed += reclaim_state->reclaimed_slab; 25931da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 25945a03b051SAndrea Arcangeli 2595d7868daeSMel Gorman if (nr_slab == 0 && !zone_reclaimable(zone)) 259693e4a89aSKOSAKI Motohiro zone->all_unreclaimable = 1; 2597d7868daeSMel Gorman } 2598d7868daeSMel Gorman 25991da177e4SLinus Torvalds /* 26001da177e4SLinus Torvalds * If we've done a decent amount of scanning and 26011da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 26021da177e4SLinus Torvalds * even in laptop mode 26031da177e4SLinus Torvalds */ 26041da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 2605a79311c1SRik van Riel total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) 26061da177e4SLinus Torvalds sc.may_writepage = 1; 2607bb3ab596SKOSAKI Motohiro 2608215ddd66SMel Gorman if (zone->all_unreclaimable) { 2609215ddd66SMel Gorman if (end_zone && end_zone == i) 2610215ddd66SMel Gorman end_zone--; 2611d7868daeSMel Gorman continue; 2612215ddd66SMel Gorman } 2613d7868daeSMel Gorman 2614fe2c2a10SRik van Riel if (!zone_watermark_ok_safe(zone, testorder, 261545973d74SMinchan Kim high_wmark_pages(zone), end_zone, 0)) { 261645973d74SMinchan Kim all_zones_ok = 0; 2617bb3ab596SKOSAKI Motohiro /* 261845973d74SMinchan Kim * We are still under min water mark. This 261945973d74SMinchan Kim * means that we have a GFP_ATOMIC allocation 262045973d74SMinchan Kim * failure risk. Hurry up! 2621bb3ab596SKOSAKI Motohiro */ 262288f5acf8SMel Gorman if (!zone_watermark_ok_safe(zone, order, 262345973d74SMinchan Kim min_wmark_pages(zone), end_zone, 0)) 2624bb3ab596SKOSAKI Motohiro has_under_min_watermark_zone = 1; 26250e093d99SMel Gorman } else { 26260e093d99SMel Gorman /* 26270e093d99SMel Gorman * If a zone reaches its high watermark, 26280e093d99SMel Gorman * consider it to be no longer congested. It's 26290e093d99SMel Gorman * possible there are dirty pages backed by 26300e093d99SMel Gorman * congested BDIs but as pressure is relieved, 26310e093d99SMel Gorman * spectulatively avoid congestion waits 26320e093d99SMel Gorman */ 26330e093d99SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 2634dc83edd9SMel Gorman if (i <= *classzone_idx) 26351741c877SMel Gorman balanced += zone->present_pages; 263645973d74SMinchan Kim } 2637bb3ab596SKOSAKI Motohiro 26381da177e4SLinus Torvalds } 2639dc83edd9SMel Gorman if (all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx))) 26401da177e4SLinus Torvalds break; /* kswapd: all done */ 26411da177e4SLinus Torvalds /* 26421da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 26431da177e4SLinus Torvalds * another pass across the zones. 26441da177e4SLinus Torvalds */ 2645bb3ab596SKOSAKI Motohiro if (total_scanned && (priority < DEF_PRIORITY - 2)) { 2646bb3ab596SKOSAKI Motohiro if (has_under_min_watermark_zone) 2647bb3ab596SKOSAKI Motohiro count_vm_event(KSWAPD_SKIP_CONGESTION_WAIT); 2648bb3ab596SKOSAKI Motohiro else 26498aa7e847SJens Axboe congestion_wait(BLK_RW_ASYNC, HZ/10); 2650bb3ab596SKOSAKI Motohiro } 26511da177e4SLinus Torvalds 26521da177e4SLinus Torvalds /* 26531da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 26541da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 26551da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 26561da177e4SLinus Torvalds * on zone->*_priority. 26571da177e4SLinus Torvalds */ 2658a79311c1SRik van Riel if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) 26591da177e4SLinus Torvalds break; 26601da177e4SLinus Torvalds } 26611da177e4SLinus Torvalds out: 266299504748SMel Gorman 266399504748SMel Gorman /* 266499504748SMel Gorman * order-0: All zones must meet high watermark for a balanced node 26651741c877SMel Gorman * high-order: Balanced zones must make up at least 25% of the node 26661741c877SMel Gorman * for the node to be balanced 266799504748SMel Gorman */ 2668dc83edd9SMel Gorman if (!(all_zones_ok || (order && pgdat_balanced(pgdat, balanced, *classzone_idx)))) { 26691da177e4SLinus Torvalds cond_resched(); 26708357376dSRafael J. Wysocki 26718357376dSRafael J. Wysocki try_to_freeze(); 26728357376dSRafael J. Wysocki 267373ce02e9SKOSAKI Motohiro /* 267473ce02e9SKOSAKI Motohiro * Fragmentation may mean that the system cannot be 267573ce02e9SKOSAKI Motohiro * rebalanced for high-order allocations in all zones. 267673ce02e9SKOSAKI Motohiro * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, 267773ce02e9SKOSAKI Motohiro * it means the zones have been fully scanned and are still 267873ce02e9SKOSAKI Motohiro * not balanced. For high-order allocations, there is 267973ce02e9SKOSAKI Motohiro * little point trying all over again as kswapd may 268073ce02e9SKOSAKI Motohiro * infinite loop. 268173ce02e9SKOSAKI Motohiro * 268273ce02e9SKOSAKI Motohiro * Instead, recheck all watermarks at order-0 as they 268373ce02e9SKOSAKI Motohiro * are the most important. If watermarks are ok, kswapd will go 268473ce02e9SKOSAKI Motohiro * back to sleep. High-order users can still perform direct 268573ce02e9SKOSAKI Motohiro * reclaim if they wish. 268673ce02e9SKOSAKI Motohiro */ 268773ce02e9SKOSAKI Motohiro if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) 268873ce02e9SKOSAKI Motohiro order = sc.order = 0; 268973ce02e9SKOSAKI Motohiro 26901da177e4SLinus Torvalds goto loop_again; 26911da177e4SLinus Torvalds } 26921da177e4SLinus Torvalds 269399504748SMel Gorman /* 269499504748SMel Gorman * If kswapd was reclaiming at a higher order, it has the option of 269599504748SMel Gorman * sleeping without all zones being balanced. Before it does, it must 269699504748SMel Gorman * ensure that the watermarks for order-0 on *all* zones are met and 269799504748SMel Gorman * that the congestion flags are cleared. The congestion flag must 269899504748SMel Gorman * be cleared as kswapd is the only mechanism that clears the flag 269999504748SMel Gorman * and it is potentially going to sleep here. 270099504748SMel Gorman */ 270199504748SMel Gorman if (order) { 27027be62de9SRik van Riel int zones_need_compaction = 1; 27037be62de9SRik van Riel 270499504748SMel Gorman for (i = 0; i <= end_zone; i++) { 270599504748SMel Gorman struct zone *zone = pgdat->node_zones + i; 270699504748SMel Gorman 270799504748SMel Gorman if (!populated_zone(zone)) 270899504748SMel Gorman continue; 270999504748SMel Gorman 271099504748SMel Gorman if (zone->all_unreclaimable && priority != DEF_PRIORITY) 271199504748SMel Gorman continue; 271299504748SMel Gorman 2713fe2c2a10SRik van Riel /* Would compaction fail due to lack of free memory? */ 2714496b919bSRik van Riel if (COMPACTION_BUILD && 2715496b919bSRik van Riel compaction_suitable(zone, order) == COMPACT_SKIPPED) 2716fe2c2a10SRik van Riel goto loop_again; 2717fe2c2a10SRik van Riel 271899504748SMel Gorman /* Confirm the zone is balanced for order-0 */ 271999504748SMel Gorman if (!zone_watermark_ok(zone, 0, 272099504748SMel Gorman high_wmark_pages(zone), 0, 0)) { 272199504748SMel Gorman order = sc.order = 0; 272299504748SMel Gorman goto loop_again; 272399504748SMel Gorman } 272499504748SMel Gorman 27257be62de9SRik van Riel /* Check if the memory needs to be defragmented. */ 27267be62de9SRik van Riel if (zone_watermark_ok(zone, order, 27277be62de9SRik van Riel low_wmark_pages(zone), *classzone_idx, 0)) 27287be62de9SRik van Riel zones_need_compaction = 0; 27297be62de9SRik van Riel 273099504748SMel Gorman /* If balanced, clear the congested flag */ 273199504748SMel Gorman zone_clear_flag(zone, ZONE_CONGESTED); 273299504748SMel Gorman } 27337be62de9SRik van Riel 27347be62de9SRik van Riel if (zones_need_compaction) 27357be62de9SRik van Riel compact_pgdat(pgdat, order); 273699504748SMel Gorman } 273799504748SMel Gorman 27380abdee2bSMel Gorman /* 27390abdee2bSMel Gorman * Return the order we were reclaiming at so sleeping_prematurely() 27400abdee2bSMel Gorman * makes a decision on the order we were last reclaiming at. However, 27410abdee2bSMel Gorman * if another caller entered the allocator slow path while kswapd 27420abdee2bSMel Gorman * was awake, order will remain at the higher level 27430abdee2bSMel Gorman */ 2744dc83edd9SMel Gorman *classzone_idx = end_zone; 27450abdee2bSMel Gorman return order; 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 2748dc83edd9SMel Gorman static void kswapd_try_to_sleep(pg_data_t *pgdat, int order, int classzone_idx) 2749f0bc0a60SKOSAKI Motohiro { 2750f0bc0a60SKOSAKI Motohiro long remaining = 0; 2751f0bc0a60SKOSAKI Motohiro DEFINE_WAIT(wait); 2752f0bc0a60SKOSAKI Motohiro 2753f0bc0a60SKOSAKI Motohiro if (freezing(current) || kthread_should_stop()) 2754f0bc0a60SKOSAKI Motohiro return; 2755f0bc0a60SKOSAKI Motohiro 2756f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2757f0bc0a60SKOSAKI Motohiro 2758f0bc0a60SKOSAKI Motohiro /* Try to sleep for a short interval */ 2759dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2760f0bc0a60SKOSAKI Motohiro remaining = schedule_timeout(HZ/10); 2761f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2762f0bc0a60SKOSAKI Motohiro prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 2763f0bc0a60SKOSAKI Motohiro } 2764f0bc0a60SKOSAKI Motohiro 2765f0bc0a60SKOSAKI Motohiro /* 2766f0bc0a60SKOSAKI Motohiro * After a short sleep, check if it was a premature sleep. If not, then 2767f0bc0a60SKOSAKI Motohiro * go fully to sleep until explicitly woken up. 2768f0bc0a60SKOSAKI Motohiro */ 2769dc83edd9SMel Gorman if (!sleeping_prematurely(pgdat, order, remaining, classzone_idx)) { 2770f0bc0a60SKOSAKI Motohiro trace_mm_vmscan_kswapd_sleep(pgdat->node_id); 2771f0bc0a60SKOSAKI Motohiro 2772f0bc0a60SKOSAKI Motohiro /* 2773f0bc0a60SKOSAKI Motohiro * vmstat counters are not perfectly accurate and the estimated 2774f0bc0a60SKOSAKI Motohiro * value for counters such as NR_FREE_PAGES can deviate from the 2775f0bc0a60SKOSAKI Motohiro * true value by nr_online_cpus * threshold. To avoid the zone 2776f0bc0a60SKOSAKI Motohiro * watermarks being breached while under pressure, we reduce the 2777f0bc0a60SKOSAKI Motohiro * per-cpu vmstat threshold while kswapd is awake and restore 2778f0bc0a60SKOSAKI Motohiro * them before going back to sleep. 2779f0bc0a60SKOSAKI Motohiro */ 2780f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold); 2781f0bc0a60SKOSAKI Motohiro schedule(); 2782f0bc0a60SKOSAKI Motohiro set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold); 2783f0bc0a60SKOSAKI Motohiro } else { 2784f0bc0a60SKOSAKI Motohiro if (remaining) 2785f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY); 2786f0bc0a60SKOSAKI Motohiro else 2787f0bc0a60SKOSAKI Motohiro count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY); 2788f0bc0a60SKOSAKI Motohiro } 2789f0bc0a60SKOSAKI Motohiro finish_wait(&pgdat->kswapd_wait, &wait); 2790f0bc0a60SKOSAKI Motohiro } 2791f0bc0a60SKOSAKI Motohiro 27921da177e4SLinus Torvalds /* 27931da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 27941da177e4SLinus Torvalds * from the init process. 27951da177e4SLinus Torvalds * 27961da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 27971da177e4SLinus Torvalds * free memory available even if there is no other activity 27981da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 27991da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 28001da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 28011da177e4SLinus Torvalds * 28021da177e4SLinus Torvalds * If there are applications that are active memory-allocators 28031da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 28041da177e4SLinus Torvalds */ 28051da177e4SLinus Torvalds static int kswapd(void *p) 28061da177e4SLinus Torvalds { 2807215ddd66SMel Gorman unsigned long order, new_order; 2808d2ebd0f6SAlex,Shi unsigned balanced_order; 2809215ddd66SMel Gorman int classzone_idx, new_classzone_idx; 2810d2ebd0f6SAlex,Shi int balanced_classzone_idx; 28111da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 28121da177e4SLinus Torvalds struct task_struct *tsk = current; 2813f0bc0a60SKOSAKI Motohiro 28141da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 28151da177e4SLinus Torvalds .reclaimed_slab = 0, 28161da177e4SLinus Torvalds }; 2817a70f7302SRusty Russell const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); 28181da177e4SLinus Torvalds 2819cf40bd16SNick Piggin lockdep_set_current_reclaim_state(GFP_KERNEL); 2820cf40bd16SNick Piggin 2821174596a0SRusty Russell if (!cpumask_empty(cpumask)) 2822c5f59f08SMike Travis set_cpus_allowed_ptr(tsk, cpumask); 28231da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 28241da177e4SLinus Torvalds 28251da177e4SLinus Torvalds /* 28261da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 28271da177e4SLinus Torvalds * and that if we need more memory we should get access to it 28281da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 28291da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 28301da177e4SLinus Torvalds * 28311da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 28321da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 28331da177e4SLinus Torvalds * page out something else, and this flag essentially protects 28341da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 28351da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 28361da177e4SLinus Torvalds */ 2837930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 283883144186SRafael J. Wysocki set_freezable(); 28391da177e4SLinus Torvalds 2840215ddd66SMel Gorman order = new_order = 0; 2841d2ebd0f6SAlex,Shi balanced_order = 0; 2842215ddd66SMel Gorman classzone_idx = new_classzone_idx = pgdat->nr_zones - 1; 2843d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 28441da177e4SLinus Torvalds for ( ; ; ) { 28458fe23e05SDavid Rientjes int ret; 28463e1d1d28SChristoph Lameter 2847215ddd66SMel Gorman /* 2848215ddd66SMel Gorman * If the last balance_pgdat was unsuccessful it's unlikely a 2849215ddd66SMel Gorman * new request of a similar or harder type will succeed soon 2850215ddd66SMel Gorman * so consider going to sleep on the basis we reclaimed at 2851215ddd66SMel Gorman */ 2852d2ebd0f6SAlex,Shi if (balanced_classzone_idx >= new_classzone_idx && 2853d2ebd0f6SAlex,Shi balanced_order == new_order) { 28541da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 285599504748SMel Gorman new_classzone_idx = pgdat->classzone_idx; 28561da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 2857215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 2858215ddd66SMel Gorman } 2859215ddd66SMel Gorman 286099504748SMel Gorman if (order < new_order || classzone_idx > new_classzone_idx) { 28611da177e4SLinus Torvalds /* 28621da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 286399504748SMel Gorman * allocation or has tigher zone constraints 28641da177e4SLinus Torvalds */ 28651da177e4SLinus Torvalds order = new_order; 286699504748SMel Gorman classzone_idx = new_classzone_idx; 28671da177e4SLinus Torvalds } else { 2868d2ebd0f6SAlex,Shi kswapd_try_to_sleep(pgdat, balanced_order, 2869d2ebd0f6SAlex,Shi balanced_classzone_idx); 28701da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 287199504748SMel Gorman classzone_idx = pgdat->classzone_idx; 2872f0dfcde0SAlex,Shi new_order = order; 2873f0dfcde0SAlex,Shi new_classzone_idx = classzone_idx; 28744d40502eSMel Gorman pgdat->kswapd_max_order = 0; 2875215ddd66SMel Gorman pgdat->classzone_idx = pgdat->nr_zones - 1; 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds 28788fe23e05SDavid Rientjes ret = try_to_freeze(); 28798fe23e05SDavid Rientjes if (kthread_should_stop()) 28808fe23e05SDavid Rientjes break; 28818fe23e05SDavid Rientjes 28828fe23e05SDavid Rientjes /* 28838fe23e05SDavid Rientjes * We can speed up thawing tasks if we don't call balance_pgdat 28848fe23e05SDavid Rientjes * after returning from the refrigerator 2885b1296cc4SRafael J. Wysocki */ 288633906bc5SMel Gorman if (!ret) { 288733906bc5SMel Gorman trace_mm_vmscan_kswapd_wake(pgdat->node_id, order); 2888d2ebd0f6SAlex,Shi balanced_classzone_idx = classzone_idx; 2889d2ebd0f6SAlex,Shi balanced_order = balance_pgdat(pgdat, order, 2890d2ebd0f6SAlex,Shi &balanced_classzone_idx); 28911da177e4SLinus Torvalds } 289233906bc5SMel Gorman } 28931da177e4SLinus Torvalds return 0; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* 28971da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 28981da177e4SLinus Torvalds */ 289999504748SMel Gorman void wakeup_kswapd(struct zone *zone, int order, enum zone_type classzone_idx) 29001da177e4SLinus Torvalds { 29011da177e4SLinus Torvalds pg_data_t *pgdat; 29021da177e4SLinus Torvalds 2903f3fe6512SCon Kolivas if (!populated_zone(zone)) 29041da177e4SLinus Torvalds return; 29051da177e4SLinus Torvalds 290602a0e53dSPaul Jackson if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) 29071da177e4SLinus Torvalds return; 290888f5acf8SMel Gorman pgdat = zone->zone_pgdat; 290999504748SMel Gorman if (pgdat->kswapd_max_order < order) { 291088f5acf8SMel Gorman pgdat->kswapd_max_order = order; 291199504748SMel Gorman pgdat->classzone_idx = min(pgdat->classzone_idx, classzone_idx); 291299504748SMel Gorman } 29138d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 29141da177e4SLinus Torvalds return; 291588f5acf8SMel Gorman if (zone_watermark_ok_safe(zone, order, low_wmark_pages(zone), 0, 0)) 291688f5acf8SMel Gorman return; 291788f5acf8SMel Gorman 291888f5acf8SMel Gorman trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, zone_idx(zone), order); 29198d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 29201da177e4SLinus Torvalds } 29211da177e4SLinus Torvalds 2922adea02a1SWu Fengguang /* 2923adea02a1SWu Fengguang * The reclaimable count would be mostly accurate. 2924adea02a1SWu Fengguang * The less reclaimable pages may be 2925adea02a1SWu Fengguang * - mlocked pages, which will be moved to unevictable list when encountered 2926adea02a1SWu Fengguang * - mapped pages, which may require several travels to be reclaimed 2927adea02a1SWu Fengguang * - dirty pages, which is not "instantly" reclaimable 2928adea02a1SWu Fengguang */ 2929adea02a1SWu Fengguang unsigned long global_reclaimable_pages(void) 29304f98a2feSRik van Riel { 2931adea02a1SWu Fengguang int nr; 2932adea02a1SWu Fengguang 2933adea02a1SWu Fengguang nr = global_page_state(NR_ACTIVE_FILE) + 2934adea02a1SWu Fengguang global_page_state(NR_INACTIVE_FILE); 2935adea02a1SWu Fengguang 2936adea02a1SWu Fengguang if (nr_swap_pages > 0) 2937adea02a1SWu Fengguang nr += global_page_state(NR_ACTIVE_ANON) + 2938adea02a1SWu Fengguang global_page_state(NR_INACTIVE_ANON); 2939adea02a1SWu Fengguang 2940adea02a1SWu Fengguang return nr; 2941adea02a1SWu Fengguang } 2942adea02a1SWu Fengguang 2943adea02a1SWu Fengguang unsigned long zone_reclaimable_pages(struct zone *zone) 2944adea02a1SWu Fengguang { 2945adea02a1SWu Fengguang int nr; 2946adea02a1SWu Fengguang 2947adea02a1SWu Fengguang nr = zone_page_state(zone, NR_ACTIVE_FILE) + 2948adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_FILE); 2949adea02a1SWu Fengguang 2950adea02a1SWu Fengguang if (nr_swap_pages > 0) 2951adea02a1SWu Fengguang nr += zone_page_state(zone, NR_ACTIVE_ANON) + 2952adea02a1SWu Fengguang zone_page_state(zone, NR_INACTIVE_ANON); 2953adea02a1SWu Fengguang 2954adea02a1SWu Fengguang return nr; 29554f98a2feSRik van Riel } 29564f98a2feSRik van Riel 2957c6f37f12SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 29581da177e4SLinus Torvalds /* 29597b51755cSKOSAKI Motohiro * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of 2960d6277db4SRafael J. Wysocki * freed pages. 2961d6277db4SRafael J. Wysocki * 2962d6277db4SRafael J. Wysocki * Rather than trying to age LRUs the aim is to preserve the overall 2963d6277db4SRafael J. Wysocki * LRU order by reclaiming preferentially 2964d6277db4SRafael J. Wysocki * inactive > active > active referenced > active mapped 29651da177e4SLinus Torvalds */ 29667b51755cSKOSAKI Motohiro unsigned long shrink_all_memory(unsigned long nr_to_reclaim) 29671da177e4SLinus Torvalds { 2968d6277db4SRafael J. Wysocki struct reclaim_state reclaim_state; 2969d6277db4SRafael J. Wysocki struct scan_control sc = { 29707b51755cSKOSAKI Motohiro .gfp_mask = GFP_HIGHUSER_MOVABLE, 29717b51755cSKOSAKI Motohiro .may_swap = 1, 29727b51755cSKOSAKI Motohiro .may_unmap = 1, 2973d6277db4SRafael J. Wysocki .may_writepage = 1, 29747b51755cSKOSAKI Motohiro .nr_to_reclaim = nr_to_reclaim, 29757b51755cSKOSAKI Motohiro .hibernation_mode = 1, 29767b51755cSKOSAKI Motohiro .order = 0, 29771da177e4SLinus Torvalds }; 2978a09ed5e0SYing Han struct shrink_control shrink = { 2979a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 2980a09ed5e0SYing Han }; 29817b51755cSKOSAKI Motohiro struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask); 29827b51755cSKOSAKI Motohiro struct task_struct *p = current; 29837b51755cSKOSAKI Motohiro unsigned long nr_reclaimed; 29841da177e4SLinus Torvalds 29857b51755cSKOSAKI Motohiro p->flags |= PF_MEMALLOC; 29867b51755cSKOSAKI Motohiro lockdep_set_current_reclaim_state(sc.gfp_mask); 2987d6277db4SRafael J. Wysocki reclaim_state.reclaimed_slab = 0; 29887b51755cSKOSAKI Motohiro p->reclaim_state = &reclaim_state; 2989d6277db4SRafael J. Wysocki 2990a09ed5e0SYing Han nr_reclaimed = do_try_to_free_pages(zonelist, &sc, &shrink); 2991d6277db4SRafael J. Wysocki 29927b51755cSKOSAKI Motohiro p->reclaim_state = NULL; 29937b51755cSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 29947b51755cSKOSAKI Motohiro p->flags &= ~PF_MEMALLOC; 2995d6277db4SRafael J. Wysocki 29967b51755cSKOSAKI Motohiro return nr_reclaimed; 29971da177e4SLinus Torvalds } 2998c6f37f12SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 30011da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 30021da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 30031da177e4SLinus Torvalds restore their cpu bindings. */ 30049c7b216dSChandra Seetharaman static int __devinit cpu_callback(struct notifier_block *nfb, 300569e05944SAndrew Morton unsigned long action, void *hcpu) 30061da177e4SLinus Torvalds { 300758c0a4a7SYasunori Goto int nid; 30081da177e4SLinus Torvalds 30098bb78442SRafael J. Wysocki if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { 301058c0a4a7SYasunori Goto for_each_node_state(nid, N_HIGH_MEMORY) { 3011c5f59f08SMike Travis pg_data_t *pgdat = NODE_DATA(nid); 3012a70f7302SRusty Russell const struct cpumask *mask; 3013a70f7302SRusty Russell 3014a70f7302SRusty Russell mask = cpumask_of_node(pgdat->node_id); 3015c5f59f08SMike Travis 30163e597945SRusty Russell if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) 30171da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 3018c5f59f08SMike Travis set_cpus_allowed_ptr(pgdat->kswapd, mask); 30191da177e4SLinus Torvalds } 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds return NOTIFY_OK; 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 30243218ae14SYasunori Goto /* 30253218ae14SYasunori Goto * This kswapd start function will be called by init and node-hot-add. 30263218ae14SYasunori Goto * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. 30273218ae14SYasunori Goto */ 30283218ae14SYasunori Goto int kswapd_run(int nid) 30293218ae14SYasunori Goto { 30303218ae14SYasunori Goto pg_data_t *pgdat = NODE_DATA(nid); 30313218ae14SYasunori Goto int ret = 0; 30323218ae14SYasunori Goto 30333218ae14SYasunori Goto if (pgdat->kswapd) 30343218ae14SYasunori Goto return 0; 30353218ae14SYasunori Goto 30363218ae14SYasunori Goto pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); 30373218ae14SYasunori Goto if (IS_ERR(pgdat->kswapd)) { 30383218ae14SYasunori Goto /* failure at boot is fatal */ 30393218ae14SYasunori Goto BUG_ON(system_state == SYSTEM_BOOTING); 30403218ae14SYasunori Goto printk("Failed to start kswapd on node %d\n",nid); 30413218ae14SYasunori Goto ret = -1; 30423218ae14SYasunori Goto } 30433218ae14SYasunori Goto return ret; 30443218ae14SYasunori Goto } 30453218ae14SYasunori Goto 30468fe23e05SDavid Rientjes /* 30478fe23e05SDavid Rientjes * Called by memory hotplug when all memory in a node is offlined. 30488fe23e05SDavid Rientjes */ 30498fe23e05SDavid Rientjes void kswapd_stop(int nid) 30508fe23e05SDavid Rientjes { 30518fe23e05SDavid Rientjes struct task_struct *kswapd = NODE_DATA(nid)->kswapd; 30528fe23e05SDavid Rientjes 30538fe23e05SDavid Rientjes if (kswapd) 30548fe23e05SDavid Rientjes kthread_stop(kswapd); 30558fe23e05SDavid Rientjes } 30568fe23e05SDavid Rientjes 30571da177e4SLinus Torvalds static int __init kswapd_init(void) 30581da177e4SLinus Torvalds { 30593218ae14SYasunori Goto int nid; 306069e05944SAndrew Morton 30611da177e4SLinus Torvalds swap_setup(); 30629422ffbaSChristoph Lameter for_each_node_state(nid, N_HIGH_MEMORY) 30633218ae14SYasunori Goto kswapd_run(nid); 30641da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 30651da177e4SLinus Torvalds return 0; 30661da177e4SLinus Torvalds } 30671da177e4SLinus Torvalds 30681da177e4SLinus Torvalds module_init(kswapd_init) 30699eeff239SChristoph Lameter 30709eeff239SChristoph Lameter #ifdef CONFIG_NUMA 30719eeff239SChristoph Lameter /* 30729eeff239SChristoph Lameter * Zone reclaim mode 30739eeff239SChristoph Lameter * 30749eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 30759eeff239SChristoph Lameter * the watermarks. 30769eeff239SChristoph Lameter */ 30779eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 30789eeff239SChristoph Lameter 30791b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 30807d03431cSFernando Luis Vazquez Cao #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ 30811b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 30821b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 30831b2ffb78SChristoph Lameter 30849eeff239SChristoph Lameter /* 3085a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 3086a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 3087a92f7126SChristoph Lameter * a zone. 3088a92f7126SChristoph Lameter */ 3089a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 3090a92f7126SChristoph Lameter 30919eeff239SChristoph Lameter /* 30929614634fSChristoph Lameter * Percentage of pages in a zone that must be unmapped for zone_reclaim to 30939614634fSChristoph Lameter * occur. 30949614634fSChristoph Lameter */ 30959614634fSChristoph Lameter int sysctl_min_unmapped_ratio = 1; 30969614634fSChristoph Lameter 30979614634fSChristoph Lameter /* 30980ff38490SChristoph Lameter * If the number of slab pages in a zone grows beyond this percentage then 30990ff38490SChristoph Lameter * slab reclaim needs to occur. 31000ff38490SChristoph Lameter */ 31010ff38490SChristoph Lameter int sysctl_min_slab_ratio = 5; 31020ff38490SChristoph Lameter 310390afa5deSMel Gorman static inline unsigned long zone_unmapped_file_pages(struct zone *zone) 310490afa5deSMel Gorman { 310590afa5deSMel Gorman unsigned long file_mapped = zone_page_state(zone, NR_FILE_MAPPED); 310690afa5deSMel Gorman unsigned long file_lru = zone_page_state(zone, NR_INACTIVE_FILE) + 310790afa5deSMel Gorman zone_page_state(zone, NR_ACTIVE_FILE); 310890afa5deSMel Gorman 310990afa5deSMel Gorman /* 311090afa5deSMel Gorman * It's possible for there to be more file mapped pages than 311190afa5deSMel Gorman * accounted for by the pages on the file LRU lists because 311290afa5deSMel Gorman * tmpfs pages accounted for as ANON can also be FILE_MAPPED 311390afa5deSMel Gorman */ 311490afa5deSMel Gorman return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0; 311590afa5deSMel Gorman } 311690afa5deSMel Gorman 311790afa5deSMel Gorman /* Work out how many page cache pages we can reclaim in this reclaim_mode */ 311890afa5deSMel Gorman static long zone_pagecache_reclaimable(struct zone *zone) 311990afa5deSMel Gorman { 312090afa5deSMel Gorman long nr_pagecache_reclaimable; 312190afa5deSMel Gorman long delta = 0; 312290afa5deSMel Gorman 312390afa5deSMel Gorman /* 312490afa5deSMel Gorman * If RECLAIM_SWAP is set, then all file pages are considered 312590afa5deSMel Gorman * potentially reclaimable. Otherwise, we have to worry about 312690afa5deSMel Gorman * pages like swapcache and zone_unmapped_file_pages() provides 312790afa5deSMel Gorman * a better estimate 312890afa5deSMel Gorman */ 312990afa5deSMel Gorman if (zone_reclaim_mode & RECLAIM_SWAP) 313090afa5deSMel Gorman nr_pagecache_reclaimable = zone_page_state(zone, NR_FILE_PAGES); 313190afa5deSMel Gorman else 313290afa5deSMel Gorman nr_pagecache_reclaimable = zone_unmapped_file_pages(zone); 313390afa5deSMel Gorman 313490afa5deSMel Gorman /* If we can't clean pages, remove dirty pages from consideration */ 313590afa5deSMel Gorman if (!(zone_reclaim_mode & RECLAIM_WRITE)) 313690afa5deSMel Gorman delta += zone_page_state(zone, NR_FILE_DIRTY); 313790afa5deSMel Gorman 313890afa5deSMel Gorman /* Watch for any possible underflows due to delta */ 313990afa5deSMel Gorman if (unlikely(delta > nr_pagecache_reclaimable)) 314090afa5deSMel Gorman delta = nr_pagecache_reclaimable; 314190afa5deSMel Gorman 314290afa5deSMel Gorman return nr_pagecache_reclaimable - delta; 314390afa5deSMel Gorman } 314490afa5deSMel Gorman 31450ff38490SChristoph Lameter /* 31469eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 31479eeff239SChristoph Lameter */ 3148179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 31499eeff239SChristoph Lameter { 31507fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 315169e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 31529eeff239SChristoph Lameter struct task_struct *p = current; 31539eeff239SChristoph Lameter struct reclaim_state reclaim_state; 31548695949aSChristoph Lameter int priority; 3155179e9639SAndrew Morton struct scan_control sc = { 3156179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 3157a6dc60f8SJohannes Weiner .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), 31582e2e4259SKOSAKI Motohiro .may_swap = 1, 315922fba335SKOSAKI Motohiro .nr_to_reclaim = max_t(unsigned long, nr_pages, 316022fba335SKOSAKI Motohiro SWAP_CLUSTER_MAX), 3161179e9639SAndrew Morton .gfp_mask = gfp_mask, 3162bd2f6199SJohannes Weiner .order = order, 3163179e9639SAndrew Morton }; 3164a09ed5e0SYing Han struct shrink_control shrink = { 3165a09ed5e0SYing Han .gfp_mask = sc.gfp_mask, 3166a09ed5e0SYing Han }; 316715748048SKOSAKI Motohiro unsigned long nr_slab_pages0, nr_slab_pages1; 31689eeff239SChristoph Lameter 31699eeff239SChristoph Lameter cond_resched(); 3170d4f7796eSChristoph Lameter /* 3171d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 3172d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 3173d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 3174d4f7796eSChristoph Lameter */ 3175d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 317676ca542dSKOSAKI Motohiro lockdep_set_current_reclaim_state(gfp_mask); 31779eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 31789eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 3179c84db23cSChristoph Lameter 318090afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) > zone->min_unmapped_pages) { 3181a92f7126SChristoph Lameter /* 31820ff38490SChristoph Lameter * Free memory by calling shrink zone with increasing 31830ff38490SChristoph Lameter * priorities until we have enough memory freed. 3184a92f7126SChristoph Lameter */ 31858695949aSChristoph Lameter priority = ZONE_RECLAIM_PRIORITY; 3186a92f7126SChristoph Lameter do { 3187a79311c1SRik van Riel shrink_zone(priority, zone, &sc); 31888695949aSChristoph Lameter priority--; 3189a79311c1SRik van Riel } while (priority >= 0 && sc.nr_reclaimed < nr_pages); 31900ff38490SChristoph Lameter } 3191a92f7126SChristoph Lameter 319215748048SKOSAKI Motohiro nr_slab_pages0 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 319315748048SKOSAKI Motohiro if (nr_slab_pages0 > zone->min_slab_pages) { 31942a16e3f4SChristoph Lameter /* 31957fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 31960ff38490SChristoph Lameter * many pages were freed in this zone. So we take the current 31970ff38490SChristoph Lameter * number of slab pages and shake the slab until it is reduced 31980ff38490SChristoph Lameter * by the same nr_pages that we used for reclaiming unmapped 31990ff38490SChristoph Lameter * pages. 32002a16e3f4SChristoph Lameter * 32010ff38490SChristoph Lameter * Note that shrink_slab will free memory on all zones and may 32020ff38490SChristoph Lameter * take a long time. 32032a16e3f4SChristoph Lameter */ 32044dc4b3d9SKOSAKI Motohiro for (;;) { 32054dc4b3d9SKOSAKI Motohiro unsigned long lru_pages = zone_reclaimable_pages(zone); 32064dc4b3d9SKOSAKI Motohiro 32074dc4b3d9SKOSAKI Motohiro /* No reclaimable slab or very low memory pressure */ 32081495f230SYing Han if (!shrink_slab(&shrink, sc.nr_scanned, lru_pages)) 32094dc4b3d9SKOSAKI Motohiro break; 32104dc4b3d9SKOSAKI Motohiro 32114dc4b3d9SKOSAKI Motohiro /* Freed enough memory */ 32124dc4b3d9SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, 32134dc4b3d9SKOSAKI Motohiro NR_SLAB_RECLAIMABLE); 32144dc4b3d9SKOSAKI Motohiro if (nr_slab_pages1 + nr_pages <= nr_slab_pages0) 32154dc4b3d9SKOSAKI Motohiro break; 32164dc4b3d9SKOSAKI Motohiro } 321783e33a47SChristoph Lameter 321883e33a47SChristoph Lameter /* 321983e33a47SChristoph Lameter * Update nr_reclaimed by the number of slab pages we 322083e33a47SChristoph Lameter * reclaimed from this zone. 322183e33a47SChristoph Lameter */ 322215748048SKOSAKI Motohiro nr_slab_pages1 = zone_page_state(zone, NR_SLAB_RECLAIMABLE); 322315748048SKOSAKI Motohiro if (nr_slab_pages1 < nr_slab_pages0) 322415748048SKOSAKI Motohiro sc.nr_reclaimed += nr_slab_pages0 - nr_slab_pages1; 32252a16e3f4SChristoph Lameter } 32262a16e3f4SChristoph Lameter 32279eeff239SChristoph Lameter p->reclaim_state = NULL; 3228d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 322976ca542dSKOSAKI Motohiro lockdep_clear_current_reclaim_state(); 3230a79311c1SRik van Riel return sc.nr_reclaimed >= nr_pages; 32319eeff239SChristoph Lameter } 3232179e9639SAndrew Morton 3233179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 3234179e9639SAndrew Morton { 3235179e9639SAndrew Morton int node_id; 3236d773ed6bSDavid Rientjes int ret; 3237179e9639SAndrew Morton 3238179e9639SAndrew Morton /* 32390ff38490SChristoph Lameter * Zone reclaim reclaims unmapped file backed pages and 32400ff38490SChristoph Lameter * slab pages if we are over the defined limits. 324134aa1330SChristoph Lameter * 32429614634fSChristoph Lameter * A small portion of unmapped file backed pages is needed for 32439614634fSChristoph Lameter * file I/O otherwise pages read by file I/O will be immediately 32449614634fSChristoph Lameter * thrown out if the zone is overallocated. So we do not reclaim 32459614634fSChristoph Lameter * if less than a specified percentage of the zone is used by 32469614634fSChristoph Lameter * unmapped file backed pages. 3247179e9639SAndrew Morton */ 324890afa5deSMel Gorman if (zone_pagecache_reclaimable(zone) <= zone->min_unmapped_pages && 324990afa5deSMel Gorman zone_page_state(zone, NR_SLAB_RECLAIMABLE) <= zone->min_slab_pages) 3250fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3251179e9639SAndrew Morton 325293e4a89aSKOSAKI Motohiro if (zone->all_unreclaimable) 3253fa5e084eSMel Gorman return ZONE_RECLAIM_FULL; 3254d773ed6bSDavid Rientjes 3255179e9639SAndrew Morton /* 3256d773ed6bSDavid Rientjes * Do not scan if the allocation should not be delayed. 3257179e9639SAndrew Morton */ 3258d773ed6bSDavid Rientjes if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) 3259fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3260179e9639SAndrew Morton 3261179e9639SAndrew Morton /* 3262179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 3263179e9639SAndrew Morton * have associated processors. This will favor the local processor 3264179e9639SAndrew Morton * over remote processors and spread off node memory allocations 3265179e9639SAndrew Morton * as wide as possible. 3266179e9639SAndrew Morton */ 326789fa3024SChristoph Lameter node_id = zone_to_nid(zone); 326837c0708dSChristoph Lameter if (node_state(node_id, N_CPU) && node_id != numa_node_id()) 3269fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3270d773ed6bSDavid Rientjes 3271d773ed6bSDavid Rientjes if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) 3272fa5e084eSMel Gorman return ZONE_RECLAIM_NOSCAN; 3273fa5e084eSMel Gorman 3274d773ed6bSDavid Rientjes ret = __zone_reclaim(zone, gfp_mask, order); 3275d773ed6bSDavid Rientjes zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); 3276d773ed6bSDavid Rientjes 327724cf7251SMel Gorman if (!ret) 327824cf7251SMel Gorman count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED); 327924cf7251SMel Gorman 3280d773ed6bSDavid Rientjes return ret; 3281179e9639SAndrew Morton } 32829eeff239SChristoph Lameter #endif 3283894bc310SLee Schermerhorn 3284894bc310SLee Schermerhorn /* 3285894bc310SLee Schermerhorn * page_evictable - test whether a page is evictable 3286894bc310SLee Schermerhorn * @page: the page to test 3287894bc310SLee Schermerhorn * @vma: the VMA in which the page is or will be mapped, may be NULL 3288894bc310SLee Schermerhorn * 3289894bc310SLee Schermerhorn * Test whether page is evictable--i.e., should be placed on active/inactive 3290b291f000SNick Piggin * lists vs unevictable list. The vma argument is !NULL when called from the 3291b291f000SNick Piggin * fault path to determine how to instantate a new page. 3292894bc310SLee Schermerhorn * 3293894bc310SLee Schermerhorn * Reasons page might not be evictable: 3294ba9ddf49SLee Schermerhorn * (1) page's mapping marked unevictable 3295b291f000SNick Piggin * (2) page is part of an mlocked VMA 3296ba9ddf49SLee Schermerhorn * 3297894bc310SLee Schermerhorn */ 3298894bc310SLee Schermerhorn int page_evictable(struct page *page, struct vm_area_struct *vma) 3299894bc310SLee Schermerhorn { 3300894bc310SLee Schermerhorn 3301ba9ddf49SLee Schermerhorn if (mapping_unevictable(page_mapping(page))) 3302ba9ddf49SLee Schermerhorn return 0; 3303ba9ddf49SLee Schermerhorn 3304096a7cf4SYing Han if (PageMlocked(page) || (vma && mlocked_vma_newpage(vma, page))) 3305b291f000SNick Piggin return 0; 3306894bc310SLee Schermerhorn 3307894bc310SLee Schermerhorn return 1; 3308894bc310SLee Schermerhorn } 330989e004eaSLee Schermerhorn 331085046579SHugh Dickins #ifdef CONFIG_SHMEM 331189e004eaSLee Schermerhorn /** 331224513264SHugh Dickins * check_move_unevictable_pages - check pages for evictability and move to appropriate zone lru list 331324513264SHugh Dickins * @pages: array of pages to check 331424513264SHugh Dickins * @nr_pages: number of pages to check 331589e004eaSLee Schermerhorn * 331624513264SHugh Dickins * Checks pages for evictability and moves them to the appropriate lru list. 331785046579SHugh Dickins * 331885046579SHugh Dickins * This function is only used for SysV IPC SHM_UNLOCK. 331989e004eaSLee Schermerhorn */ 332024513264SHugh Dickins void check_move_unevictable_pages(struct page **pages, int nr_pages) 332189e004eaSLee Schermerhorn { 3322925b7673SJohannes Weiner struct lruvec *lruvec; 332324513264SHugh Dickins struct zone *zone = NULL; 332424513264SHugh Dickins int pgscanned = 0; 332524513264SHugh Dickins int pgrescued = 0; 332689e004eaSLee Schermerhorn int i; 332789e004eaSLee Schermerhorn 332824513264SHugh Dickins for (i = 0; i < nr_pages; i++) { 332924513264SHugh Dickins struct page *page = pages[i]; 333024513264SHugh Dickins struct zone *pagezone; 333189e004eaSLee Schermerhorn 333224513264SHugh Dickins pgscanned++; 333324513264SHugh Dickins pagezone = page_zone(page); 333489e004eaSLee Schermerhorn if (pagezone != zone) { 333589e004eaSLee Schermerhorn if (zone) 333689e004eaSLee Schermerhorn spin_unlock_irq(&zone->lru_lock); 333789e004eaSLee Schermerhorn zone = pagezone; 333889e004eaSLee Schermerhorn spin_lock_irq(&zone->lru_lock); 333989e004eaSLee Schermerhorn } 334089e004eaSLee Schermerhorn 334124513264SHugh Dickins if (!PageLRU(page) || !PageUnevictable(page)) 334224513264SHugh Dickins continue; 334389e004eaSLee Schermerhorn 334424513264SHugh Dickins if (page_evictable(page, NULL)) { 334524513264SHugh Dickins enum lru_list lru = page_lru_base_type(page); 334624513264SHugh Dickins 334724513264SHugh Dickins VM_BUG_ON(PageActive(page)); 334824513264SHugh Dickins ClearPageUnevictable(page); 334924513264SHugh Dickins __dec_zone_state(zone, NR_UNEVICTABLE); 335024513264SHugh Dickins lruvec = mem_cgroup_lru_move_lists(zone, page, 335124513264SHugh Dickins LRU_UNEVICTABLE, lru); 335224513264SHugh Dickins list_move(&page->lru, &lruvec->lists[lru]); 335324513264SHugh Dickins __inc_zone_state(zone, NR_INACTIVE_ANON + lru); 335424513264SHugh Dickins pgrescued++; 335589e004eaSLee Schermerhorn } 335689e004eaSLee Schermerhorn } 335724513264SHugh Dickins 335824513264SHugh Dickins if (zone) { 335924513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued); 336024513264SHugh Dickins __count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned); 336124513264SHugh Dickins spin_unlock_irq(&zone->lru_lock); 336224513264SHugh Dickins } 336385046579SHugh Dickins } 336485046579SHugh Dickins #endif /* CONFIG_SHMEM */ 3365af936a16SLee Schermerhorn 3366264e56d8SJohannes Weiner static void warn_scan_unevictable_pages(void) 3367af936a16SLee Schermerhorn { 3368264e56d8SJohannes Weiner printk_once(KERN_WARNING 336925bd91bdSKOSAKI Motohiro "%s: The scan_unevictable_pages sysctl/node-interface has been " 3370264e56d8SJohannes Weiner "disabled for lack of a legitimate use case. If you have " 337125bd91bdSKOSAKI Motohiro "one, please send an email to linux-mm@kvack.org.\n", 337225bd91bdSKOSAKI Motohiro current->comm); 3373af936a16SLee Schermerhorn } 3374af936a16SLee Schermerhorn 3375af936a16SLee Schermerhorn /* 3376af936a16SLee Schermerhorn * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of 3377af936a16SLee Schermerhorn * all nodes' unevictable lists for evictable pages 3378af936a16SLee Schermerhorn */ 3379af936a16SLee Schermerhorn unsigned long scan_unevictable_pages; 3380af936a16SLee Schermerhorn 3381af936a16SLee Schermerhorn int scan_unevictable_handler(struct ctl_table *table, int write, 33828d65af78SAlexey Dobriyan void __user *buffer, 3383af936a16SLee Schermerhorn size_t *length, loff_t *ppos) 3384af936a16SLee Schermerhorn { 3385264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 33868d65af78SAlexey Dobriyan proc_doulongvec_minmax(table, write, buffer, length, ppos); 3387af936a16SLee Schermerhorn scan_unevictable_pages = 0; 3388af936a16SLee Schermerhorn return 0; 3389af936a16SLee Schermerhorn } 3390af936a16SLee Schermerhorn 3391e4455abbSThadeu Lima de Souza Cascardo #ifdef CONFIG_NUMA 3392af936a16SLee Schermerhorn /* 3393af936a16SLee Schermerhorn * per node 'scan_unevictable_pages' attribute. On demand re-scan of 3394af936a16SLee Schermerhorn * a specified node's per zone unevictable lists for evictable pages. 3395af936a16SLee Schermerhorn */ 3396af936a16SLee Schermerhorn 339710fbcf4cSKay Sievers static ssize_t read_scan_unevictable_node(struct device *dev, 339810fbcf4cSKay Sievers struct device_attribute *attr, 3399af936a16SLee Schermerhorn char *buf) 3400af936a16SLee Schermerhorn { 3401264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3402af936a16SLee Schermerhorn return sprintf(buf, "0\n"); /* always zero; should fit... */ 3403af936a16SLee Schermerhorn } 3404af936a16SLee Schermerhorn 340510fbcf4cSKay Sievers static ssize_t write_scan_unevictable_node(struct device *dev, 340610fbcf4cSKay Sievers struct device_attribute *attr, 3407af936a16SLee Schermerhorn const char *buf, size_t count) 3408af936a16SLee Schermerhorn { 3409264e56d8SJohannes Weiner warn_scan_unevictable_pages(); 3410af936a16SLee Schermerhorn return 1; 3411af936a16SLee Schermerhorn } 3412af936a16SLee Schermerhorn 3413af936a16SLee Schermerhorn 341410fbcf4cSKay Sievers static DEVICE_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, 3415af936a16SLee Schermerhorn read_scan_unevictable_node, 3416af936a16SLee Schermerhorn write_scan_unevictable_node); 3417af936a16SLee Schermerhorn 3418af936a16SLee Schermerhorn int scan_unevictable_register_node(struct node *node) 3419af936a16SLee Schermerhorn { 342010fbcf4cSKay Sievers return device_create_file(&node->dev, &dev_attr_scan_unevictable_pages); 3421af936a16SLee Schermerhorn } 3422af936a16SLee Schermerhorn 3423af936a16SLee Schermerhorn void scan_unevictable_unregister_node(struct node *node) 3424af936a16SLee Schermerhorn { 342510fbcf4cSKay Sievers device_remove_file(&node->dev, &dev_attr_scan_unevictable_pages); 3426af936a16SLee Schermerhorn } 3427e4455abbSThadeu Lima de Souza Cascardo #endif 3428