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> 161da177e4SLinus Torvalds #include <linux/slab.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> 221da177e4SLinus Torvalds #include <linux/file.h> 231da177e4SLinus Torvalds #include <linux/writeback.h> 241da177e4SLinus Torvalds #include <linux/blkdev.h> 251da177e4SLinus Torvalds #include <linux/buffer_head.h> /* for try_to_release_page(), 261da177e4SLinus Torvalds buffer_heads_over_limit */ 271da177e4SLinus Torvalds #include <linux/mm_inline.h> 281da177e4SLinus Torvalds #include <linux/pagevec.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> 341da177e4SLinus Torvalds #include <linux/notifier.h> 351da177e4SLinus Torvalds #include <linux/rwsem.h> 36*248a0301SRafael J. Wysocki #include <linux/delay.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/tlbflush.h> 391da177e4SLinus Torvalds #include <asm/div64.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <linux/swapops.h> 421da177e4SLinus Torvalds 430f8053a5SNick Piggin #include "internal.h" 440f8053a5SNick Piggin 451da177e4SLinus Torvalds /* possible outcome of pageout() */ 461da177e4SLinus Torvalds typedef enum { 471da177e4SLinus Torvalds /* failed to write page out, page is locked */ 481da177e4SLinus Torvalds PAGE_KEEP, 491da177e4SLinus Torvalds /* move page to the active list, page is locked */ 501da177e4SLinus Torvalds PAGE_ACTIVATE, 511da177e4SLinus Torvalds /* page has been sent to the disk successfully, page is unlocked */ 521da177e4SLinus Torvalds PAGE_SUCCESS, 531da177e4SLinus Torvalds /* page is clean and locked */ 541da177e4SLinus Torvalds PAGE_CLEAN, 551da177e4SLinus Torvalds } pageout_t; 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds struct scan_control { 581da177e4SLinus Torvalds /* Incremented by the number of inactive pages that were scanned */ 591da177e4SLinus Torvalds unsigned long nr_scanned; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds unsigned long nr_mapped; /* From page_state */ 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds /* This context's GFP mask */ 646daa0e28SAl Viro gfp_t gfp_mask; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds int may_writepage; 671da177e4SLinus Torvalds 68f1fd1067SChristoph Lameter /* Can pages be swapped as part of reclaim? */ 69f1fd1067SChristoph Lameter int may_swap; 70f1fd1067SChristoph Lameter 711da177e4SLinus Torvalds /* This context's SWAP_CLUSTER_MAX. If freeing memory for 721da177e4SLinus Torvalds * suspend, we effectively ignore SWAP_CLUSTER_MAX. 731da177e4SLinus Torvalds * In this context, it doesn't matter that we scan the 741da177e4SLinus Torvalds * whole list at once. */ 751da177e4SLinus Torvalds int swap_cluster_max; 761da177e4SLinus Torvalds }; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds /* 791da177e4SLinus Torvalds * The list of shrinker callbacks used by to apply pressure to 801da177e4SLinus Torvalds * ageable caches. 811da177e4SLinus Torvalds */ 821da177e4SLinus Torvalds struct shrinker { 831da177e4SLinus Torvalds shrinker_t shrinker; 841da177e4SLinus Torvalds struct list_head list; 851da177e4SLinus Torvalds int seeks; /* seeks to recreate an obj */ 861da177e4SLinus Torvalds long nr; /* objs pending delete */ 871da177e4SLinus Torvalds }; 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 921da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 931da177e4SLinus Torvalds do { \ 941da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 951da177e4SLinus Torvalds struct page *prev; \ 961da177e4SLinus Torvalds \ 971da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 981da177e4SLinus Torvalds prefetch(&prev->_field); \ 991da177e4SLinus Torvalds } \ 1001da177e4SLinus Torvalds } while (0) 1011da177e4SLinus Torvalds #else 1021da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1031da177e4SLinus Torvalds #endif 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1061da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1071da177e4SLinus Torvalds do { \ 1081da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1091da177e4SLinus Torvalds struct page *prev; \ 1101da177e4SLinus Torvalds \ 1111da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1121da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1131da177e4SLinus Torvalds } \ 1141da177e4SLinus Torvalds } while (0) 1151da177e4SLinus Torvalds #else 1161da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1171da177e4SLinus Torvalds #endif 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* 1201da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1211da177e4SLinus Torvalds */ 1221da177e4SLinus Torvalds int vm_swappiness = 60; 1231da177e4SLinus Torvalds static long total_memory; 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1261da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds /* 1291da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1301da177e4SLinus Torvalds */ 1311da177e4SLinus Torvalds struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds struct shrinker *shrinker; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL); 1361da177e4SLinus Torvalds if (shrinker) { 1371da177e4SLinus Torvalds shrinker->shrinker = theshrinker; 1381da177e4SLinus Torvalds shrinker->seeks = seeks; 1391da177e4SLinus Torvalds shrinker->nr = 0; 1401da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1411da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1421da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds return shrinker; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds EXPORT_SYMBOL(set_shrinker); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* 1491da177e4SLinus Torvalds * Remove one 1501da177e4SLinus Torvalds */ 1511da177e4SLinus Torvalds void remove_shrinker(struct shrinker *shrinker) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1541da177e4SLinus Torvalds list_del(&shrinker->list); 1551da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1561da177e4SLinus Torvalds kfree(shrinker); 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds EXPORT_SYMBOL(remove_shrinker); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds #define SHRINK_BATCH 128 1611da177e4SLinus Torvalds /* 1621da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 1631da177e4SLinus Torvalds * 1641da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 1651da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 1661da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 1671da177e4SLinus Torvalds * generated by these structures. 1681da177e4SLinus Torvalds * 1691da177e4SLinus Torvalds * If the vm encounted mapped pages on the LRU it increase the pressure on 1701da177e4SLinus Torvalds * slab to avoid swapping. 1711da177e4SLinus Torvalds * 1721da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 1731da177e4SLinus Torvalds * 1741da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 1751da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 1761da177e4SLinus Torvalds * slab reclaim versus page reclaim. 177b15e0905Sakpm@osdl.org * 178b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 1791da177e4SLinus Torvalds */ 18069e05944SAndrew Morton unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, 18169e05944SAndrew Morton unsigned long lru_pages) 1821da177e4SLinus Torvalds { 1831da177e4SLinus Torvalds struct shrinker *shrinker; 18469e05944SAndrew Morton unsigned long ret = 0; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds if (scanned == 0) 1871da177e4SLinus Torvalds scanned = SWAP_CLUSTER_MAX; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds if (!down_read_trylock(&shrinker_rwsem)) 190b15e0905Sakpm@osdl.org return 1; /* Assume we'll be able to shrink next time */ 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 1931da177e4SLinus Torvalds unsigned long long delta; 1941da177e4SLinus Torvalds unsigned long total_scan; 195ea164d73SAndrea Arcangeli unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask); 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds delta = (4 * scanned) / shrinker->seeks; 198ea164d73SAndrea Arcangeli delta *= max_pass; 1991da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 2001da177e4SLinus Torvalds shrinker->nr += delta; 201ea164d73SAndrea Arcangeli if (shrinker->nr < 0) { 202ea164d73SAndrea Arcangeli printk(KERN_ERR "%s: nr=%ld\n", 203ea164d73SAndrea Arcangeli __FUNCTION__, shrinker->nr); 204ea164d73SAndrea Arcangeli shrinker->nr = max_pass; 205ea164d73SAndrea Arcangeli } 206ea164d73SAndrea Arcangeli 207ea164d73SAndrea Arcangeli /* 208ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 209ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 210ea164d73SAndrea Arcangeli * freeable entries. 211ea164d73SAndrea Arcangeli */ 212ea164d73SAndrea Arcangeli if (shrinker->nr > max_pass * 2) 213ea164d73SAndrea Arcangeli shrinker->nr = max_pass * 2; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds total_scan = shrinker->nr; 2161da177e4SLinus Torvalds shrinker->nr = 0; 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds while (total_scan >= SHRINK_BATCH) { 2191da177e4SLinus Torvalds long this_scan = SHRINK_BATCH; 2201da177e4SLinus Torvalds int shrink_ret; 221b15e0905Sakpm@osdl.org int nr_before; 2221da177e4SLinus Torvalds 223b15e0905Sakpm@osdl.org nr_before = (*shrinker->shrinker)(0, gfp_mask); 2241da177e4SLinus Torvalds shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask); 2251da177e4SLinus Torvalds if (shrink_ret == -1) 2261da177e4SLinus Torvalds break; 227b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 228b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 2291da177e4SLinus Torvalds mod_page_state(slabs_scanned, this_scan); 2301da177e4SLinus Torvalds total_scan -= this_scan; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds cond_resched(); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds shrinker->nr += total_scan; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds up_read(&shrinker_rwsem); 238b15e0905Sakpm@osdl.org return ret; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* Called without lock on whether page is mapped, so answer is unstable */ 2421da177e4SLinus Torvalds static inline int page_mapping_inuse(struct page *page) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds struct address_space *mapping; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /* Page is in somebody's page tables. */ 2471da177e4SLinus Torvalds if (page_mapped(page)) 2481da177e4SLinus Torvalds return 1; 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds /* Be more reluctant to reclaim swapcache than pagecache */ 2511da177e4SLinus Torvalds if (PageSwapCache(page)) 2521da177e4SLinus Torvalds return 1; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds mapping = page_mapping(page); 2551da177e4SLinus Torvalds if (!mapping) 2561da177e4SLinus Torvalds return 0; 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /* File is mmap'd by somebody? */ 2591da177e4SLinus Torvalds return mapping_mapped(mapping); 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds return page_count(page) - !!PagePrivate(page) == 2; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static int may_write_to_queue(struct backing_dev_info *bdi) 2681da177e4SLinus Torvalds { 269930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 2701da177e4SLinus Torvalds return 1; 2711da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 2721da177e4SLinus Torvalds return 1; 2731da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 2741da177e4SLinus Torvalds return 1; 2751da177e4SLinus Torvalds return 0; 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /* 2791da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 2801da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 2811da177e4SLinus Torvalds * fsync(), msync() or close(). 2821da177e4SLinus Torvalds * 2831da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 2841da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 2851da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 2861da177e4SLinus Torvalds * 2871da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 2881da177e4SLinus Torvalds * __GFP_FS. 2891da177e4SLinus Torvalds */ 2901da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 2911da177e4SLinus Torvalds struct page *page, int error) 2921da177e4SLinus Torvalds { 2931da177e4SLinus Torvalds lock_page(page); 2941da177e4SLinus Torvalds if (page_mapping(page) == mapping) { 2951da177e4SLinus Torvalds if (error == -ENOSPC) 2961da177e4SLinus Torvalds set_bit(AS_ENOSPC, &mapping->flags); 2971da177e4SLinus Torvalds else 2981da177e4SLinus Torvalds set_bit(AS_EIO, &mapping->flags); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds unlock_page(page); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* 3041742f19fSAndrew Morton * pageout is called by shrink_page_list() for each dirty page. 3051742f19fSAndrew Morton * Calls ->writepage(). 3061da177e4SLinus Torvalds */ 3071da177e4SLinus Torvalds static pageout_t pageout(struct page *page, struct address_space *mapping) 3081da177e4SLinus Torvalds { 3091da177e4SLinus Torvalds /* 3101da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3111da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3121da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3131da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3141da177e4SLinus Torvalds * PagePrivate for that. 3151da177e4SLinus Torvalds * 3161da177e4SLinus Torvalds * If this process is currently in generic_file_write() against 3171da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 3181da177e4SLinus Torvalds * will block. 3191da177e4SLinus Torvalds * 3201da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 3211da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 3221da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 3231da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 3241da177e4SLinus Torvalds * See swapfile.c:page_queue_congested(). 3251da177e4SLinus Torvalds */ 3261da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 3271da177e4SLinus Torvalds return PAGE_KEEP; 3281da177e4SLinus Torvalds if (!mapping) { 3291da177e4SLinus Torvalds /* 3301da177e4SLinus Torvalds * Some data journaling orphaned pages can have 3311da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 3321da177e4SLinus Torvalds */ 333323aca6cSakpm@osdl.org if (PagePrivate(page)) { 3341da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 3351da177e4SLinus Torvalds ClearPageDirty(page); 3361da177e4SLinus Torvalds printk("%s: orphaned page\n", __FUNCTION__); 3371da177e4SLinus Torvalds return PAGE_CLEAN; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds return PAGE_KEEP; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 3431da177e4SLinus Torvalds return PAGE_ACTIVATE; 3441da177e4SLinus Torvalds if (!may_write_to_queue(mapping->backing_dev_info)) 3451da177e4SLinus Torvalds return PAGE_KEEP; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 3481da177e4SLinus Torvalds int res; 3491da177e4SLinus Torvalds struct writeback_control wbc = { 3501da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 3511da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 3521da177e4SLinus Torvalds .nonblocking = 1, 3531da177e4SLinus Torvalds .for_reclaim = 1, 3541da177e4SLinus Torvalds }; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds SetPageReclaim(page); 3571da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 3581da177e4SLinus Torvalds if (res < 0) 3591da177e4SLinus Torvalds handle_write_error(mapping, page, res); 360994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 3611da177e4SLinus Torvalds ClearPageReclaim(page); 3621da177e4SLinus Torvalds return PAGE_ACTIVATE; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds if (!PageWriteback(page)) { 3651da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 3661da177e4SLinus Torvalds ClearPageReclaim(page); 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds return PAGE_SUCCESS; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds return PAGE_CLEAN; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 37549d2e9ccSChristoph Lameter static int remove_mapping(struct address_space *mapping, struct page *page) 37649d2e9ccSChristoph Lameter { 37749d2e9ccSChristoph Lameter if (!mapping) 37849d2e9ccSChristoph Lameter return 0; /* truncate got there first */ 37949d2e9ccSChristoph Lameter 38049d2e9ccSChristoph Lameter write_lock_irq(&mapping->tree_lock); 38149d2e9ccSChristoph Lameter 38249d2e9ccSChristoph Lameter /* 38349d2e9ccSChristoph Lameter * The non-racy check for busy page. It is critical to check 38449d2e9ccSChristoph Lameter * PageDirty _after_ making sure that the page is freeable and 38549d2e9ccSChristoph Lameter * not in use by anybody. (pagecache + us == 2) 38649d2e9ccSChristoph Lameter */ 38749d2e9ccSChristoph Lameter if (unlikely(page_count(page) != 2)) 38849d2e9ccSChristoph Lameter goto cannot_free; 38949d2e9ccSChristoph Lameter smp_rmb(); 39049d2e9ccSChristoph Lameter if (unlikely(PageDirty(page))) 39149d2e9ccSChristoph Lameter goto cannot_free; 39249d2e9ccSChristoph Lameter 39349d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 39449d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 39549d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 39649d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 39749d2e9ccSChristoph Lameter swap_free(swap); 39849d2e9ccSChristoph Lameter __put_page(page); /* The pagecache ref */ 39949d2e9ccSChristoph Lameter return 1; 40049d2e9ccSChristoph Lameter } 40149d2e9ccSChristoph Lameter 40249d2e9ccSChristoph Lameter __remove_from_page_cache(page); 40349d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 40449d2e9ccSChristoph Lameter __put_page(page); 40549d2e9ccSChristoph Lameter return 1; 40649d2e9ccSChristoph Lameter 40749d2e9ccSChristoph Lameter cannot_free: 40849d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 40949d2e9ccSChristoph Lameter return 0; 41049d2e9ccSChristoph Lameter } 41149d2e9ccSChristoph Lameter 4121da177e4SLinus Torvalds /* 4131742f19fSAndrew Morton * shrink_page_list() returns the number of reclaimed pages 4141da177e4SLinus Torvalds */ 4151742f19fSAndrew Morton static unsigned long shrink_page_list(struct list_head *page_list, 41669e05944SAndrew Morton struct scan_control *sc) 4171da177e4SLinus Torvalds { 4181da177e4SLinus Torvalds LIST_HEAD(ret_pages); 4191da177e4SLinus Torvalds struct pagevec freed_pvec; 4201da177e4SLinus Torvalds int pgactivate = 0; 42105ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds cond_resched(); 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds pagevec_init(&freed_pvec, 1); 4261da177e4SLinus Torvalds while (!list_empty(page_list)) { 4271da177e4SLinus Torvalds struct address_space *mapping; 4281da177e4SLinus Torvalds struct page *page; 4291da177e4SLinus Torvalds int may_enter_fs; 4301da177e4SLinus Torvalds int referenced; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds cond_resched(); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds page = lru_to_page(page_list); 4351da177e4SLinus Torvalds list_del(&page->lru); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds if (TestSetPageLocked(page)) 4381da177e4SLinus Torvalds goto keep; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds BUG_ON(PageActive(page)); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds sc->nr_scanned++; 44380e43426SChristoph Lameter 44480e43426SChristoph Lameter if (!sc->may_swap && page_mapped(page)) 44580e43426SChristoph Lameter goto keep_locked; 44680e43426SChristoph Lameter 4471da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 4481da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 4491da177e4SLinus Torvalds sc->nr_scanned++; 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds if (PageWriteback(page)) 4521da177e4SLinus Torvalds goto keep_locked; 4531da177e4SLinus Torvalds 454f7b7fd8fSRik van Riel referenced = page_referenced(page, 1); 4551da177e4SLinus Torvalds /* In active use or really unfreeable? Activate it. */ 4561da177e4SLinus Torvalds if (referenced && page_mapping_inuse(page)) 4571da177e4SLinus Torvalds goto activate_locked; 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds #ifdef CONFIG_SWAP 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Anonymous process memory has backing store? 4621da177e4SLinus Torvalds * Try to allocate it some swap space here. 4631da177e4SLinus Torvalds */ 4646e5ef1a9SChristoph Lameter if (PageAnon(page) && !PageSwapCache(page)) 4651480a540SChristoph Lameter if (!add_to_swap(page, GFP_ATOMIC)) 4661da177e4SLinus Torvalds goto activate_locked; 4671da177e4SLinus Torvalds #endif /* CONFIG_SWAP */ 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds mapping = page_mapping(page); 4701da177e4SLinus Torvalds may_enter_fs = (sc->gfp_mask & __GFP_FS) || 4711da177e4SLinus Torvalds (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds /* 4741da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 4751da177e4SLinus Torvalds * processes. Try to unmap it here. 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 478a48d07afSChristoph Lameter switch (try_to_unmap(page, 0)) { 4791da177e4SLinus Torvalds case SWAP_FAIL: 4801da177e4SLinus Torvalds goto activate_locked; 4811da177e4SLinus Torvalds case SWAP_AGAIN: 4821da177e4SLinus Torvalds goto keep_locked; 4831da177e4SLinus Torvalds case SWAP_SUCCESS: 4841da177e4SLinus Torvalds ; /* try to free the page below */ 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (PageDirty(page)) { 4891da177e4SLinus Torvalds if (referenced) 4901da177e4SLinus Torvalds goto keep_locked; 4911da177e4SLinus Torvalds if (!may_enter_fs) 4921da177e4SLinus Torvalds goto keep_locked; 49352a8363eSChristoph Lameter if (!sc->may_writepage) 4941da177e4SLinus Torvalds goto keep_locked; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 4971da177e4SLinus Torvalds switch(pageout(page, mapping)) { 4981da177e4SLinus Torvalds case PAGE_KEEP: 4991da177e4SLinus Torvalds goto keep_locked; 5001da177e4SLinus Torvalds case PAGE_ACTIVATE: 5011da177e4SLinus Torvalds goto activate_locked; 5021da177e4SLinus Torvalds case PAGE_SUCCESS: 5031da177e4SLinus Torvalds if (PageWriteback(page) || PageDirty(page)) 5041da177e4SLinus Torvalds goto keep; 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 5071da177e4SLinus Torvalds * ahead and try to reclaim the page. 5081da177e4SLinus Torvalds */ 5091da177e4SLinus Torvalds if (TestSetPageLocked(page)) 5101da177e4SLinus Torvalds goto keep; 5111da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 5121da177e4SLinus Torvalds goto keep_locked; 5131da177e4SLinus Torvalds mapping = page_mapping(page); 5141da177e4SLinus Torvalds case PAGE_CLEAN: 5151da177e4SLinus Torvalds ; /* try to free the page below */ 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* 5201da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 5211da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 5221da177e4SLinus Torvalds * the page as well. 5231da177e4SLinus Torvalds * 5241da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 5251da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 5261da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 5271da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 5281da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 5291da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 5301da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 5311da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 5321da177e4SLinus Torvalds * 5331da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 5341da177e4SLinus Torvalds * the pages which were not successfully invalidated in 5351da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 5361da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 5371da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 5381da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 5391da177e4SLinus Torvalds */ 5401da177e4SLinus Torvalds if (PagePrivate(page)) { 5411da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 5421da177e4SLinus Torvalds goto activate_locked; 5431da177e4SLinus Torvalds if (!mapping && page_count(page) == 1) 5441da177e4SLinus Torvalds goto free_it; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 54749d2e9ccSChristoph Lameter if (!remove_mapping(mapping, page)) 54849d2e9ccSChristoph Lameter goto keep_locked; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds free_it: 5511da177e4SLinus Torvalds unlock_page(page); 55205ff5137SAndrew Morton nr_reclaimed++; 5531da177e4SLinus Torvalds if (!pagevec_add(&freed_pvec, page)) 5541da177e4SLinus Torvalds __pagevec_release_nonlru(&freed_pvec); 5551da177e4SLinus Torvalds continue; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds activate_locked: 5581da177e4SLinus Torvalds SetPageActive(page); 5591da177e4SLinus Torvalds pgactivate++; 5601da177e4SLinus Torvalds keep_locked: 5611da177e4SLinus Torvalds unlock_page(page); 5621da177e4SLinus Torvalds keep: 5631da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 5641da177e4SLinus Torvalds BUG_ON(PageLRU(page)); 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 5671da177e4SLinus Torvalds if (pagevec_count(&freed_pvec)) 5681da177e4SLinus Torvalds __pagevec_release_nonlru(&freed_pvec); 5691da177e4SLinus Torvalds mod_page_state(pgactivate, pgactivate); 57005ff5137SAndrew Morton return nr_reclaimed; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5737cbe34cfSChristoph Lameter #ifdef CONFIG_MIGRATION 5748419c318SChristoph Lameter static inline void move_to_lru(struct page *page) 5758419c318SChristoph Lameter { 5768419c318SChristoph Lameter list_del(&page->lru); 5778419c318SChristoph Lameter if (PageActive(page)) { 5788419c318SChristoph Lameter /* 5798419c318SChristoph Lameter * lru_cache_add_active checks that 5808419c318SChristoph Lameter * the PG_active bit is off. 5818419c318SChristoph Lameter */ 5828419c318SChristoph Lameter ClearPageActive(page); 5838419c318SChristoph Lameter lru_cache_add_active(page); 5848419c318SChristoph Lameter } else { 5858419c318SChristoph Lameter lru_cache_add(page); 5868419c318SChristoph Lameter } 5878419c318SChristoph Lameter put_page(page); 5888419c318SChristoph Lameter } 5898419c318SChristoph Lameter 5908419c318SChristoph Lameter /* 591053837fcSNick Piggin * Add isolated pages on the list back to the LRU. 5928419c318SChristoph Lameter * 5938419c318SChristoph Lameter * returns the number of pages put back. 5948419c318SChristoph Lameter */ 59569e05944SAndrew Morton unsigned long putback_lru_pages(struct list_head *l) 5968419c318SChristoph Lameter { 5978419c318SChristoph Lameter struct page *page; 5988419c318SChristoph Lameter struct page *page2; 59969e05944SAndrew Morton unsigned long count = 0; 6008419c318SChristoph Lameter 6018419c318SChristoph Lameter list_for_each_entry_safe(page, page2, l, lru) { 6028419c318SChristoph Lameter move_to_lru(page); 6038419c318SChristoph Lameter count++; 6048419c318SChristoph Lameter } 6058419c318SChristoph Lameter return count; 6068419c318SChristoph Lameter } 6078419c318SChristoph Lameter 6081da177e4SLinus Torvalds /* 609e965f963SChristoph Lameter * Non migratable page 610e965f963SChristoph Lameter */ 611e965f963SChristoph Lameter int fail_migrate_page(struct page *newpage, struct page *page) 612e965f963SChristoph Lameter { 613e965f963SChristoph Lameter return -EIO; 614e965f963SChristoph Lameter } 615e965f963SChristoph Lameter EXPORT_SYMBOL(fail_migrate_page); 616e965f963SChristoph Lameter 617e965f963SChristoph Lameter /* 61849d2e9ccSChristoph Lameter * swapout a single page 61949d2e9ccSChristoph Lameter * page is locked upon entry, unlocked on exit 62049d2e9ccSChristoph Lameter */ 62149d2e9ccSChristoph Lameter static int swap_page(struct page *page) 62249d2e9ccSChristoph Lameter { 62349d2e9ccSChristoph Lameter struct address_space *mapping = page_mapping(page); 62449d2e9ccSChristoph Lameter 62549d2e9ccSChristoph Lameter if (page_mapped(page) && mapping) 626418aade4SChristoph Lameter if (try_to_unmap(page, 1) != SWAP_SUCCESS) 62749d2e9ccSChristoph Lameter goto unlock_retry; 62849d2e9ccSChristoph Lameter 62949d2e9ccSChristoph Lameter if (PageDirty(page)) { 63049d2e9ccSChristoph Lameter /* Page is dirty, try to write it out here */ 63149d2e9ccSChristoph Lameter switch(pageout(page, mapping)) { 63249d2e9ccSChristoph Lameter case PAGE_KEEP: 63349d2e9ccSChristoph Lameter case PAGE_ACTIVATE: 63449d2e9ccSChristoph Lameter goto unlock_retry; 63549d2e9ccSChristoph Lameter 63649d2e9ccSChristoph Lameter case PAGE_SUCCESS: 63749d2e9ccSChristoph Lameter goto retry; 63849d2e9ccSChristoph Lameter 63949d2e9ccSChristoph Lameter case PAGE_CLEAN: 64049d2e9ccSChristoph Lameter ; /* try to free the page below */ 64149d2e9ccSChristoph Lameter } 64249d2e9ccSChristoph Lameter } 64349d2e9ccSChristoph Lameter 64449d2e9ccSChristoph Lameter if (PagePrivate(page)) { 64549d2e9ccSChristoph Lameter if (!try_to_release_page(page, GFP_KERNEL) || 64649d2e9ccSChristoph Lameter (!mapping && page_count(page) == 1)) 64749d2e9ccSChristoph Lameter goto unlock_retry; 64849d2e9ccSChristoph Lameter } 64949d2e9ccSChristoph Lameter 65049d2e9ccSChristoph Lameter if (remove_mapping(mapping, page)) { 65149d2e9ccSChristoph Lameter /* Success */ 65249d2e9ccSChristoph Lameter unlock_page(page); 65349d2e9ccSChristoph Lameter return 0; 65449d2e9ccSChristoph Lameter } 65549d2e9ccSChristoph Lameter 65649d2e9ccSChristoph Lameter unlock_retry: 65749d2e9ccSChristoph Lameter unlock_page(page); 65849d2e9ccSChristoph Lameter 65949d2e9ccSChristoph Lameter retry: 660d0d96328SChristoph Lameter return -EAGAIN; 66149d2e9ccSChristoph Lameter } 662e965f963SChristoph Lameter EXPORT_SYMBOL(swap_page); 663a48d07afSChristoph Lameter 664a48d07afSChristoph Lameter /* 665a48d07afSChristoph Lameter * Page migration was first developed in the context of the memory hotplug 666a48d07afSChristoph Lameter * project. The main authors of the migration code are: 667a48d07afSChristoph Lameter * 668a48d07afSChristoph Lameter * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> 669a48d07afSChristoph Lameter * Hirokazu Takahashi <taka@valinux.co.jp> 670a48d07afSChristoph Lameter * Dave Hansen <haveblue@us.ibm.com> 671a48d07afSChristoph Lameter * Christoph Lameter <clameter@sgi.com> 672a48d07afSChristoph Lameter */ 673a48d07afSChristoph Lameter 674a48d07afSChristoph Lameter /* 675a48d07afSChristoph Lameter * Remove references for a page and establish the new page with the correct 676a48d07afSChristoph Lameter * basic settings to be able to stop accesses to the page. 677a48d07afSChristoph Lameter */ 678e965f963SChristoph Lameter int migrate_page_remove_references(struct page *newpage, 679a48d07afSChristoph Lameter struct page *page, int nr_refs) 680a48d07afSChristoph Lameter { 681a48d07afSChristoph Lameter struct address_space *mapping = page_mapping(page); 682a48d07afSChristoph Lameter struct page **radix_pointer; 683a48d07afSChristoph Lameter 684a48d07afSChristoph Lameter /* 685a48d07afSChristoph Lameter * Avoid doing any of the following work if the page count 686a48d07afSChristoph Lameter * indicates that the page is in use or truncate has removed 687a48d07afSChristoph Lameter * the page. 688a48d07afSChristoph Lameter */ 689a48d07afSChristoph Lameter if (!mapping || page_mapcount(page) + nr_refs != page_count(page)) 6904983da07SChristoph Lameter return -EAGAIN; 691a48d07afSChristoph Lameter 692a48d07afSChristoph Lameter /* 693a48d07afSChristoph Lameter * Establish swap ptes for anonymous pages or destroy pte 694a48d07afSChristoph Lameter * maps for files. 695a48d07afSChristoph Lameter * 696a48d07afSChristoph Lameter * In order to reestablish file backed mappings the fault handlers 697a48d07afSChristoph Lameter * will take the radix tree_lock which may then be used to stop 698a48d07afSChristoph Lameter * processses from accessing this page until the new page is ready. 699a48d07afSChristoph Lameter * 700a48d07afSChristoph Lameter * A process accessing via a swap pte (an anonymous page) will take a 701a48d07afSChristoph Lameter * page_lock on the old page which will block the process until the 702a48d07afSChristoph Lameter * migration attempt is complete. At that time the PageSwapCache bit 703a48d07afSChristoph Lameter * will be examined. If the page was migrated then the PageSwapCache 704a48d07afSChristoph Lameter * bit will be clear and the operation to retrieve the page will be 705a48d07afSChristoph Lameter * retried which will find the new page in the radix tree. Then a new 706a48d07afSChristoph Lameter * direct mapping may be generated based on the radix tree contents. 707a48d07afSChristoph Lameter * 708a48d07afSChristoph Lameter * If the page was not migrated then the PageSwapCache bit 709a48d07afSChristoph Lameter * is still set and the operation may continue. 710a48d07afSChristoph Lameter */ 7114983da07SChristoph Lameter if (try_to_unmap(page, 1) == SWAP_FAIL) 7124983da07SChristoph Lameter /* A vma has VM_LOCKED set -> Permanent failure */ 7134983da07SChristoph Lameter return -EPERM; 714a48d07afSChristoph Lameter 715a48d07afSChristoph Lameter /* 716a48d07afSChristoph Lameter * Give up if we were unable to remove all mappings. 717a48d07afSChristoph Lameter */ 718a48d07afSChristoph Lameter if (page_mapcount(page)) 7194983da07SChristoph Lameter return -EAGAIN; 720a48d07afSChristoph Lameter 721a48d07afSChristoph Lameter write_lock_irq(&mapping->tree_lock); 722a48d07afSChristoph Lameter 723a48d07afSChristoph Lameter radix_pointer = (struct page **)radix_tree_lookup_slot( 724a48d07afSChristoph Lameter &mapping->page_tree, 725a48d07afSChristoph Lameter page_index(page)); 726a48d07afSChristoph Lameter 727a48d07afSChristoph Lameter if (!page_mapping(page) || page_count(page) != nr_refs || 728a48d07afSChristoph Lameter *radix_pointer != page) { 729a48d07afSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 7304983da07SChristoph Lameter return -EAGAIN; 731a48d07afSChristoph Lameter } 732a48d07afSChristoph Lameter 733a48d07afSChristoph Lameter /* 734a48d07afSChristoph Lameter * Now we know that no one else is looking at the page. 735a48d07afSChristoph Lameter * 736a48d07afSChristoph Lameter * Certain minimal information about a page must be available 737a48d07afSChristoph Lameter * in order for other subsystems to properly handle the page if they 738a48d07afSChristoph Lameter * find it through the radix tree update before we are finished 739a48d07afSChristoph Lameter * copying the page. 740a48d07afSChristoph Lameter */ 741a48d07afSChristoph Lameter get_page(newpage); 742a48d07afSChristoph Lameter newpage->index = page->index; 743a48d07afSChristoph Lameter newpage->mapping = page->mapping; 744a48d07afSChristoph Lameter if (PageSwapCache(page)) { 745a48d07afSChristoph Lameter SetPageSwapCache(newpage); 746a48d07afSChristoph Lameter set_page_private(newpage, page_private(page)); 747a48d07afSChristoph Lameter } 748a48d07afSChristoph Lameter 749a48d07afSChristoph Lameter *radix_pointer = newpage; 750a48d07afSChristoph Lameter __put_page(page); 751a48d07afSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 752a48d07afSChristoph Lameter 753a48d07afSChristoph Lameter return 0; 754a48d07afSChristoph Lameter } 755e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page_remove_references); 756a48d07afSChristoph Lameter 757a48d07afSChristoph Lameter /* 758a48d07afSChristoph Lameter * Copy the page to its new location 759a48d07afSChristoph Lameter */ 760a48d07afSChristoph Lameter void migrate_page_copy(struct page *newpage, struct page *page) 761a48d07afSChristoph Lameter { 762a48d07afSChristoph Lameter copy_highpage(newpage, page); 763a48d07afSChristoph Lameter 764a48d07afSChristoph Lameter if (PageError(page)) 765a48d07afSChristoph Lameter SetPageError(newpage); 766a48d07afSChristoph Lameter if (PageReferenced(page)) 767a48d07afSChristoph Lameter SetPageReferenced(newpage); 768a48d07afSChristoph Lameter if (PageUptodate(page)) 769a48d07afSChristoph Lameter SetPageUptodate(newpage); 770a48d07afSChristoph Lameter if (PageActive(page)) 771a48d07afSChristoph Lameter SetPageActive(newpage); 772a48d07afSChristoph Lameter if (PageChecked(page)) 773a48d07afSChristoph Lameter SetPageChecked(newpage); 774a48d07afSChristoph Lameter if (PageMappedToDisk(page)) 775a48d07afSChristoph Lameter SetPageMappedToDisk(newpage); 776a48d07afSChristoph Lameter 777a48d07afSChristoph Lameter if (PageDirty(page)) { 778a48d07afSChristoph Lameter clear_page_dirty_for_io(page); 779a48d07afSChristoph Lameter set_page_dirty(newpage); 780a48d07afSChristoph Lameter } 781a48d07afSChristoph Lameter 782a48d07afSChristoph Lameter ClearPageSwapCache(page); 783a48d07afSChristoph Lameter ClearPageActive(page); 784a48d07afSChristoph Lameter ClearPagePrivate(page); 785a48d07afSChristoph Lameter set_page_private(page, 0); 786a48d07afSChristoph Lameter page->mapping = NULL; 787a48d07afSChristoph Lameter 788a48d07afSChristoph Lameter /* 789a48d07afSChristoph Lameter * If any waiters have accumulated on the new page then 790a48d07afSChristoph Lameter * wake them up. 791a48d07afSChristoph Lameter */ 792a48d07afSChristoph Lameter if (PageWriteback(newpage)) 793a48d07afSChristoph Lameter end_page_writeback(newpage); 794a48d07afSChristoph Lameter } 795e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page_copy); 796a48d07afSChristoph Lameter 797a48d07afSChristoph Lameter /* 798a48d07afSChristoph Lameter * Common logic to directly migrate a single page suitable for 799a48d07afSChristoph Lameter * pages that do not use PagePrivate. 800a48d07afSChristoph Lameter * 801a48d07afSChristoph Lameter * Pages are locked upon entry and exit. 802a48d07afSChristoph Lameter */ 803a48d07afSChristoph Lameter int migrate_page(struct page *newpage, struct page *page) 804a48d07afSChristoph Lameter { 8054983da07SChristoph Lameter int rc; 8064983da07SChristoph Lameter 807a48d07afSChristoph Lameter BUG_ON(PageWriteback(page)); /* Writeback must be complete */ 808a48d07afSChristoph Lameter 8094983da07SChristoph Lameter rc = migrate_page_remove_references(newpage, page, 2); 8104983da07SChristoph Lameter 8114983da07SChristoph Lameter if (rc) 8124983da07SChristoph Lameter return rc; 813a48d07afSChristoph Lameter 814a48d07afSChristoph Lameter migrate_page_copy(newpage, page); 815a48d07afSChristoph Lameter 816a3351e52SChristoph Lameter /* 817a3351e52SChristoph Lameter * Remove auxiliary swap entries and replace 818a3351e52SChristoph Lameter * them with real ptes. 819a3351e52SChristoph Lameter * 820a3351e52SChristoph Lameter * Note that a real pte entry will allow processes that are not 821a3351e52SChristoph Lameter * waiting on the page lock to use the new page via the page tables 822a3351e52SChristoph Lameter * before the new page is unlocked. 823a3351e52SChristoph Lameter */ 824a3351e52SChristoph Lameter remove_from_swap(newpage); 825a48d07afSChristoph Lameter return 0; 826a48d07afSChristoph Lameter } 827e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page); 828a48d07afSChristoph Lameter 82949d2e9ccSChristoph Lameter /* 83049d2e9ccSChristoph Lameter * migrate_pages 83149d2e9ccSChristoph Lameter * 83249d2e9ccSChristoph Lameter * Two lists are passed to this function. The first list 83349d2e9ccSChristoph Lameter * contains the pages isolated from the LRU to be migrated. 83449d2e9ccSChristoph Lameter * The second list contains new pages that the pages isolated 83549d2e9ccSChristoph Lameter * can be moved to. If the second list is NULL then all 83649d2e9ccSChristoph Lameter * pages are swapped out. 83749d2e9ccSChristoph Lameter * 83849d2e9ccSChristoph Lameter * The function returns after 10 attempts or if no pages 839418aade4SChristoph Lameter * are movable anymore because to has become empty 84049d2e9ccSChristoph Lameter * or no retryable pages exist anymore. 84149d2e9ccSChristoph Lameter * 842d0d96328SChristoph Lameter * Return: Number of pages not migrated when "to" ran empty. 84349d2e9ccSChristoph Lameter */ 84469e05944SAndrew Morton unsigned long migrate_pages(struct list_head *from, struct list_head *to, 845d4984711SChristoph Lameter struct list_head *moved, struct list_head *failed) 84649d2e9ccSChristoph Lameter { 84769e05944SAndrew Morton unsigned long retry; 84869e05944SAndrew Morton unsigned long nr_failed = 0; 84949d2e9ccSChristoph Lameter int pass = 0; 85049d2e9ccSChristoph Lameter struct page *page; 85149d2e9ccSChristoph Lameter struct page *page2; 85249d2e9ccSChristoph Lameter int swapwrite = current->flags & PF_SWAPWRITE; 853d0d96328SChristoph Lameter int rc; 85449d2e9ccSChristoph Lameter 85549d2e9ccSChristoph Lameter if (!swapwrite) 85649d2e9ccSChristoph Lameter current->flags |= PF_SWAPWRITE; 85749d2e9ccSChristoph Lameter 85849d2e9ccSChristoph Lameter redo: 85949d2e9ccSChristoph Lameter retry = 0; 86049d2e9ccSChristoph Lameter 861d4984711SChristoph Lameter list_for_each_entry_safe(page, page2, from, lru) { 862a48d07afSChristoph Lameter struct page *newpage = NULL; 863a48d07afSChristoph Lameter struct address_space *mapping; 864a48d07afSChristoph Lameter 86549d2e9ccSChristoph Lameter cond_resched(); 86649d2e9ccSChristoph Lameter 867d0d96328SChristoph Lameter rc = 0; 868d0d96328SChristoph Lameter if (page_count(page) == 1) 869ee27497dSChristoph Lameter /* page was freed from under us. So we are done. */ 870d0d96328SChristoph Lameter goto next; 871d0d96328SChristoph Lameter 872a48d07afSChristoph Lameter if (to && list_empty(to)) 873a48d07afSChristoph Lameter break; 874a48d07afSChristoph Lameter 87549d2e9ccSChristoph Lameter /* 87649d2e9ccSChristoph Lameter * Skip locked pages during the first two passes to give the 8777cbe34cfSChristoph Lameter * functions holding the lock time to release the page. Later we 8787cbe34cfSChristoph Lameter * use lock_page() to have a higher chance of acquiring the 8797cbe34cfSChristoph Lameter * lock. 88049d2e9ccSChristoph Lameter */ 881d0d96328SChristoph Lameter rc = -EAGAIN; 88249d2e9ccSChristoph Lameter if (pass > 2) 88349d2e9ccSChristoph Lameter lock_page(page); 88449d2e9ccSChristoph Lameter else 88549d2e9ccSChristoph Lameter if (TestSetPageLocked(page)) 886d0d96328SChristoph Lameter goto next; 88749d2e9ccSChristoph Lameter 88849d2e9ccSChristoph Lameter /* 88949d2e9ccSChristoph Lameter * Only wait on writeback if we have already done a pass where 89049d2e9ccSChristoph Lameter * we we may have triggered writeouts for lots of pages. 89149d2e9ccSChristoph Lameter */ 8927cbe34cfSChristoph Lameter if (pass > 0) { 89349d2e9ccSChristoph Lameter wait_on_page_writeback(page); 8947cbe34cfSChristoph Lameter } else { 895d0d96328SChristoph Lameter if (PageWriteback(page)) 896d0d96328SChristoph Lameter goto unlock_page; 8977cbe34cfSChristoph Lameter } 89849d2e9ccSChristoph Lameter 899d0d96328SChristoph Lameter /* 900d0d96328SChristoph Lameter * Anonymous pages must have swap cache references otherwise 901d0d96328SChristoph Lameter * the information contained in the page maps cannot be 902d0d96328SChristoph Lameter * preserved. 903d0d96328SChristoph Lameter */ 90449d2e9ccSChristoph Lameter if (PageAnon(page) && !PageSwapCache(page)) { 9051480a540SChristoph Lameter if (!add_to_swap(page, GFP_KERNEL)) { 906d0d96328SChristoph Lameter rc = -ENOMEM; 907d0d96328SChristoph Lameter goto unlock_page; 90849d2e9ccSChristoph Lameter } 90949d2e9ccSChristoph Lameter } 91049d2e9ccSChristoph Lameter 911a48d07afSChristoph Lameter if (!to) { 912d0d96328SChristoph Lameter rc = swap_page(page); 913d0d96328SChristoph Lameter goto next; 914a48d07afSChristoph Lameter } 915a48d07afSChristoph Lameter 916a48d07afSChristoph Lameter newpage = lru_to_page(to); 917a48d07afSChristoph Lameter lock_page(newpage); 918a48d07afSChristoph Lameter 919a48d07afSChristoph Lameter /* 920a48d07afSChristoph Lameter * Pages are properly locked and writeback is complete. 921a48d07afSChristoph Lameter * Try to migrate the page. 922a48d07afSChristoph Lameter */ 923a48d07afSChristoph Lameter mapping = page_mapping(page); 924a48d07afSChristoph Lameter if (!mapping) 925a48d07afSChristoph Lameter goto unlock_both; 926a48d07afSChristoph Lameter 927e965f963SChristoph Lameter if (mapping->a_ops->migratepage) { 928418aade4SChristoph Lameter /* 929418aade4SChristoph Lameter * Most pages have a mapping and most filesystems 930418aade4SChristoph Lameter * should provide a migration function. Anonymous 931418aade4SChristoph Lameter * pages are part of swap space which also has its 932418aade4SChristoph Lameter * own migration function. This is the most common 933418aade4SChristoph Lameter * path for page migration. 934418aade4SChristoph Lameter */ 935e965f963SChristoph Lameter rc = mapping->a_ops->migratepage(newpage, page); 936e965f963SChristoph Lameter goto unlock_both; 937e965f963SChristoph Lameter } 938e965f963SChristoph Lameter 939a48d07afSChristoph Lameter /* 940418aade4SChristoph Lameter * Default handling if a filesystem does not provide 941418aade4SChristoph Lameter * a migration function. We can only migrate clean 942418aade4SChristoph Lameter * pages so try to write out any dirty pages first. 943a48d07afSChristoph Lameter */ 944a48d07afSChristoph Lameter if (PageDirty(page)) { 945a48d07afSChristoph Lameter switch (pageout(page, mapping)) { 946a48d07afSChristoph Lameter case PAGE_KEEP: 947a48d07afSChristoph Lameter case PAGE_ACTIVATE: 948a48d07afSChristoph Lameter goto unlock_both; 949a48d07afSChristoph Lameter 950a48d07afSChristoph Lameter case PAGE_SUCCESS: 951a48d07afSChristoph Lameter unlock_page(newpage); 952a48d07afSChristoph Lameter goto next; 953a48d07afSChristoph Lameter 954a48d07afSChristoph Lameter case PAGE_CLEAN: 955a48d07afSChristoph Lameter ; /* try to migrate the page below */ 956a48d07afSChristoph Lameter } 957a48d07afSChristoph Lameter } 958418aade4SChristoph Lameter 959a48d07afSChristoph Lameter /* 960418aade4SChristoph Lameter * Buffers are managed in a filesystem specific way. 961418aade4SChristoph Lameter * We must have no buffers or drop them. 962a48d07afSChristoph Lameter */ 963a48d07afSChristoph Lameter if (!page_has_buffers(page) || 964a48d07afSChristoph Lameter try_to_release_page(page, GFP_KERNEL)) { 965a48d07afSChristoph Lameter rc = migrate_page(newpage, page); 966a48d07afSChristoph Lameter goto unlock_both; 967a48d07afSChristoph Lameter } 968a48d07afSChristoph Lameter 969a48d07afSChristoph Lameter /* 970a48d07afSChristoph Lameter * On early passes with mapped pages simply 971a48d07afSChristoph Lameter * retry. There may be a lock held for some 972a48d07afSChristoph Lameter * buffers that may go away. Later 973a48d07afSChristoph Lameter * swap them out. 974a48d07afSChristoph Lameter */ 975a48d07afSChristoph Lameter if (pass > 4) { 976418aade4SChristoph Lameter /* 977418aade4SChristoph Lameter * Persistently unable to drop buffers..... As a 978418aade4SChristoph Lameter * measure of last resort we fall back to 979418aade4SChristoph Lameter * swap_page(). 980418aade4SChristoph Lameter */ 981a48d07afSChristoph Lameter unlock_page(newpage); 982a48d07afSChristoph Lameter newpage = NULL; 983a48d07afSChristoph Lameter rc = swap_page(page); 984a48d07afSChristoph Lameter goto next; 985a48d07afSChristoph Lameter } 986a48d07afSChristoph Lameter 987a48d07afSChristoph Lameter unlock_both: 988a48d07afSChristoph Lameter unlock_page(newpage); 989d0d96328SChristoph Lameter 990d0d96328SChristoph Lameter unlock_page: 991d0d96328SChristoph Lameter unlock_page(page); 992d0d96328SChristoph Lameter 993d0d96328SChristoph Lameter next: 994d0d96328SChristoph Lameter if (rc == -EAGAIN) { 99549d2e9ccSChristoph Lameter retry++; 996d0d96328SChristoph Lameter } else if (rc) { 997d0d96328SChristoph Lameter /* Permanent failure */ 998d0d96328SChristoph Lameter list_move(&page->lru, failed); 999d0d96328SChristoph Lameter nr_failed++; 1000d0d96328SChristoph Lameter } else { 1001a48d07afSChristoph Lameter if (newpage) { 1002a48d07afSChristoph Lameter /* Successful migration. Return page to LRU */ 1003a48d07afSChristoph Lameter move_to_lru(newpage); 1004a48d07afSChristoph Lameter } 1005d0d96328SChristoph Lameter list_move(&page->lru, moved); 1006d0d96328SChristoph Lameter } 100749d2e9ccSChristoph Lameter } 100849d2e9ccSChristoph Lameter if (retry && pass++ < 10) 100949d2e9ccSChristoph Lameter goto redo; 101049d2e9ccSChristoph Lameter 101149d2e9ccSChristoph Lameter if (!swapwrite) 101249d2e9ccSChristoph Lameter current->flags &= ~PF_SWAPWRITE; 101349d2e9ccSChristoph Lameter 101449d2e9ccSChristoph Lameter return nr_failed + retry; 101549d2e9ccSChristoph Lameter } 10168419c318SChristoph Lameter 10178419c318SChristoph Lameter /* 10188419c318SChristoph Lameter * Isolate one page from the LRU lists and put it on the 1019053837fcSNick Piggin * indicated list with elevated refcount. 10208419c318SChristoph Lameter * 10218419c318SChristoph Lameter * Result: 10228419c318SChristoph Lameter * 0 = page not on LRU list 10238419c318SChristoph Lameter * 1 = page removed from LRU list and added to the specified list. 10248419c318SChristoph Lameter */ 10258419c318SChristoph Lameter int isolate_lru_page(struct page *page) 10268419c318SChristoph Lameter { 1027053837fcSNick Piggin int ret = 0; 10288419c318SChristoph Lameter 1029053837fcSNick Piggin if (PageLRU(page)) { 1030053837fcSNick Piggin struct zone *zone = page_zone(page); 10318419c318SChristoph Lameter spin_lock_irq(&zone->lru_lock); 10328d438f96SNick Piggin if (PageLRU(page)) { 1033053837fcSNick Piggin ret = 1; 1034053837fcSNick Piggin get_page(page); 10358d438f96SNick Piggin ClearPageLRU(page); 10368419c318SChristoph Lameter if (PageActive(page)) 10378419c318SChristoph Lameter del_page_from_active_list(zone, page); 10388419c318SChristoph Lameter else 10398419c318SChristoph Lameter del_page_from_inactive_list(zone, page); 10408419c318SChristoph Lameter } 10418419c318SChristoph Lameter spin_unlock_irq(&zone->lru_lock); 10428419c318SChristoph Lameter } 1043053837fcSNick Piggin 1044053837fcSNick Piggin return ret; 10458419c318SChristoph Lameter } 10467cbe34cfSChristoph Lameter #endif 104749d2e9ccSChristoph Lameter 104849d2e9ccSChristoph Lameter /* 10491da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10501da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10511da177e4SLinus Torvalds * and working on them outside the LRU lock. 10521da177e4SLinus Torvalds * 10531da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10541da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10551da177e4SLinus Torvalds * 10561da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10571da177e4SLinus Torvalds * 10581da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 10591da177e4SLinus Torvalds * @src: The LRU list to pull pages off. 10601da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 10611da177e4SLinus Torvalds * @scanned: The number of pages that were scanned. 10621da177e4SLinus Torvalds * 10631da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10641da177e4SLinus Torvalds */ 106569e05944SAndrew Morton static unsigned long isolate_lru_pages(unsigned long nr_to_scan, 106669e05944SAndrew Morton struct list_head *src, struct list_head *dst, 106769e05944SAndrew Morton unsigned long *scanned) 10681da177e4SLinus Torvalds { 106969e05944SAndrew Morton unsigned long nr_taken = 0; 10701da177e4SLinus Torvalds struct page *page; 1071c9b02d97SWu Fengguang unsigned long scan; 10721da177e4SLinus Torvalds 1073c9b02d97SWu Fengguang for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { 10747c8ee9a8SNick Piggin struct list_head *target; 10751da177e4SLinus Torvalds page = lru_to_page(src); 10761da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10771da177e4SLinus Torvalds 10788d438f96SNick Piggin BUG_ON(!PageLRU(page)); 10798d438f96SNick Piggin 1080053837fcSNick Piggin list_del(&page->lru); 10817c8ee9a8SNick Piggin target = src; 10827c8ee9a8SNick Piggin if (likely(get_page_unless_zero(page))) { 1083053837fcSNick Piggin /* 10847c8ee9a8SNick Piggin * Be careful not to clear PageLRU until after we're 10857c8ee9a8SNick Piggin * sure the page is not being freed elsewhere -- the 10867c8ee9a8SNick Piggin * page release code relies on it. 108746453a6eSNick Piggin */ 10888d438f96SNick Piggin ClearPageLRU(page); 10897c8ee9a8SNick Piggin target = dst; 1090053837fcSNick Piggin nr_taken++; 10917c8ee9a8SNick Piggin } /* else it is being freed elsewhere */ 10927c8ee9a8SNick Piggin 10937c8ee9a8SNick Piggin list_add(&page->lru, target); 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds *scanned = scan; 10971da177e4SLinus Torvalds return nr_taken; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011742f19fSAndrew Morton * shrink_inactive_list() is a helper for shrink_zone(). It returns the number 11021742f19fSAndrew Morton * of reclaimed pages 11031da177e4SLinus Torvalds */ 11041742f19fSAndrew Morton static unsigned long shrink_inactive_list(unsigned long max_scan, 11051742f19fSAndrew Morton struct zone *zone, struct scan_control *sc) 11061da177e4SLinus Torvalds { 11071da177e4SLinus Torvalds LIST_HEAD(page_list); 11081da177e4SLinus Torvalds struct pagevec pvec; 110969e05944SAndrew Morton unsigned long nr_scanned = 0; 111005ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds pagevec_init(&pvec, 1); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds lru_add_drain(); 11151da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 111669e05944SAndrew Morton do { 11171da177e4SLinus Torvalds struct page *page; 111869e05944SAndrew Morton unsigned long nr_taken; 111969e05944SAndrew Morton unsigned long nr_scan; 112069e05944SAndrew Morton unsigned long nr_freed; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds nr_taken = isolate_lru_pages(sc->swap_cluster_max, 11231da177e4SLinus Torvalds &zone->inactive_list, 11241da177e4SLinus Torvalds &page_list, &nr_scan); 11251da177e4SLinus Torvalds zone->nr_inactive -= nr_taken; 11261da177e4SLinus Torvalds zone->pages_scanned += nr_scan; 11271da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 11281da177e4SLinus Torvalds 112969e05944SAndrew Morton nr_scanned += nr_scan; 11301742f19fSAndrew Morton nr_freed = shrink_page_list(&page_list, sc); 113105ff5137SAndrew Morton nr_reclaimed += nr_freed; 1132a74609faSNick Piggin local_irq_disable(); 1133a74609faSNick Piggin if (current_is_kswapd()) { 1134a74609faSNick Piggin __mod_page_state_zone(zone, pgscan_kswapd, nr_scan); 1135a74609faSNick Piggin __mod_page_state(kswapd_steal, nr_freed); 1136a74609faSNick Piggin } else 1137a74609faSNick Piggin __mod_page_state_zone(zone, pgscan_direct, nr_scan); 1138a74609faSNick Piggin __mod_page_state_zone(zone, pgsteal, nr_freed); 1139a74609faSNick Piggin 1140fb8d14e1SWu Fengguang if (nr_taken == 0) 1141fb8d14e1SWu Fengguang goto done; 1142fb8d14e1SWu Fengguang 1143a74609faSNick Piggin spin_lock(&zone->lru_lock); 11441da177e4SLinus Torvalds /* 11451da177e4SLinus Torvalds * Put back any unfreeable pages. 11461da177e4SLinus Torvalds */ 11471da177e4SLinus Torvalds while (!list_empty(&page_list)) { 11481da177e4SLinus Torvalds page = lru_to_page(&page_list); 11498d438f96SNick Piggin BUG_ON(PageLRU(page)); 11508d438f96SNick Piggin SetPageLRU(page); 11511da177e4SLinus Torvalds list_del(&page->lru); 11521da177e4SLinus Torvalds if (PageActive(page)) 11531da177e4SLinus Torvalds add_page_to_active_list(zone, page); 11541da177e4SLinus Torvalds else 11551da177e4SLinus Torvalds add_page_to_inactive_list(zone, page); 11561da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 11571da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 11581da177e4SLinus Torvalds __pagevec_release(&pvec); 11591da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds } 116269e05944SAndrew Morton } while (nr_scanned < max_scan); 1163fb8d14e1SWu Fengguang spin_unlock(&zone->lru_lock); 11641da177e4SLinus Torvalds done: 1165fb8d14e1SWu Fengguang local_irq_enable(); 11661da177e4SLinus Torvalds pagevec_release(&pvec); 116705ff5137SAndrew Morton return nr_reclaimed; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * This moves pages from the active list to the inactive list. 11721da177e4SLinus Torvalds * 11731da177e4SLinus Torvalds * We move them the other way if the page is referenced by one or more 11741da177e4SLinus Torvalds * processes, from rmap. 11751da177e4SLinus Torvalds * 11761da177e4SLinus Torvalds * If the pages are mostly unmapped, the processing is fast and it is 11771da177e4SLinus Torvalds * appropriate to hold zone->lru_lock across the whole operation. But if 11781da177e4SLinus Torvalds * the pages are mapped, the processing is slow (page_referenced()) so we 11791da177e4SLinus Torvalds * should drop zone->lru_lock around each page. It's impossible to balance 11801da177e4SLinus Torvalds * this, so instead we remove the pages from the LRU while processing them. 11811da177e4SLinus Torvalds * It is safe to rely on PG_active against the non-LRU pages in here because 11821da177e4SLinus Torvalds * nobody will play with that bit on a non-LRU page. 11831da177e4SLinus Torvalds * 11841da177e4SLinus Torvalds * The downside is that we have to touch page->_count against each page. 11851da177e4SLinus Torvalds * But we had to alter page->flags anyway. 11861da177e4SLinus Torvalds */ 11871742f19fSAndrew Morton static void shrink_active_list(unsigned long nr_pages, struct zone *zone, 118869e05944SAndrew Morton struct scan_control *sc) 11891da177e4SLinus Torvalds { 119069e05944SAndrew Morton unsigned long pgmoved; 11911da177e4SLinus Torvalds int pgdeactivate = 0; 119269e05944SAndrew Morton unsigned long pgscanned; 11931da177e4SLinus Torvalds LIST_HEAD(l_hold); /* The pages which were snipped off */ 11941da177e4SLinus Torvalds LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */ 11951da177e4SLinus Torvalds LIST_HEAD(l_active); /* Pages to go onto the active_list */ 11961da177e4SLinus Torvalds struct page *page; 11971da177e4SLinus Torvalds struct pagevec pvec; 11981da177e4SLinus Torvalds int reclaim_mapped = 0; 11992903fb16SChristoph Lameter 12006e5ef1a9SChristoph Lameter if (sc->may_swap) { 12011da177e4SLinus Torvalds long mapped_ratio; 12021da177e4SLinus Torvalds long distress; 12031da177e4SLinus Torvalds long swap_tendency; 12041da177e4SLinus Torvalds 12052903fb16SChristoph Lameter /* 12062903fb16SChristoph Lameter * `distress' is a measure of how much trouble we're having 12072903fb16SChristoph Lameter * reclaiming pages. 0 -> no problems. 100 -> great trouble. 12082903fb16SChristoph Lameter */ 12092903fb16SChristoph Lameter distress = 100 >> zone->prev_priority; 12102903fb16SChristoph Lameter 12112903fb16SChristoph Lameter /* 12122903fb16SChristoph Lameter * The point of this algorithm is to decide when to start 12132903fb16SChristoph Lameter * reclaiming mapped memory instead of just pagecache. Work out 12142903fb16SChristoph Lameter * how much memory 12152903fb16SChristoph Lameter * is mapped. 12162903fb16SChristoph Lameter */ 12172903fb16SChristoph Lameter mapped_ratio = (sc->nr_mapped * 100) / total_memory; 12182903fb16SChristoph Lameter 12192903fb16SChristoph Lameter /* 12202903fb16SChristoph Lameter * Now decide how much we really want to unmap some pages. The 12212903fb16SChristoph Lameter * mapped ratio is downgraded - just because there's a lot of 12222903fb16SChristoph Lameter * mapped memory doesn't necessarily mean that page reclaim 12232903fb16SChristoph Lameter * isn't succeeding. 12242903fb16SChristoph Lameter * 12252903fb16SChristoph Lameter * The distress ratio is important - we don't want to start 12262903fb16SChristoph Lameter * going oom. 12272903fb16SChristoph Lameter * 12282903fb16SChristoph Lameter * A 100% value of vm_swappiness overrides this algorithm 12292903fb16SChristoph Lameter * altogether. 12302903fb16SChristoph Lameter */ 12312903fb16SChristoph Lameter swap_tendency = mapped_ratio / 2 + distress + vm_swappiness; 12322903fb16SChristoph Lameter 12332903fb16SChristoph Lameter /* 12342903fb16SChristoph Lameter * Now use this metric to decide whether to start moving mapped 12352903fb16SChristoph Lameter * memory onto the inactive list. 12362903fb16SChristoph Lameter */ 12372903fb16SChristoph Lameter if (swap_tendency >= 100) 12382903fb16SChristoph Lameter reclaim_mapped = 1; 12392903fb16SChristoph Lameter } 12402903fb16SChristoph Lameter 12411da177e4SLinus Torvalds lru_add_drain(); 12421da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12431da177e4SLinus Torvalds pgmoved = isolate_lru_pages(nr_pages, &zone->active_list, 12441da177e4SLinus Torvalds &l_hold, &pgscanned); 12451da177e4SLinus Torvalds zone->pages_scanned += pgscanned; 12461da177e4SLinus Torvalds zone->nr_active -= pgmoved; 12471da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 12501da177e4SLinus Torvalds cond_resched(); 12511da177e4SLinus Torvalds page = lru_to_page(&l_hold); 12521da177e4SLinus Torvalds list_del(&page->lru); 12531da177e4SLinus Torvalds if (page_mapped(page)) { 12541da177e4SLinus Torvalds if (!reclaim_mapped || 12551da177e4SLinus Torvalds (total_swap_pages == 0 && PageAnon(page)) || 1256f7b7fd8fSRik van Riel page_referenced(page, 0)) { 12571da177e4SLinus Torvalds list_add(&page->lru, &l_active); 12581da177e4SLinus Torvalds continue; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 12621da177e4SLinus Torvalds } 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds pagevec_init(&pvec, 1); 12651da177e4SLinus Torvalds pgmoved = 0; 12661da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12671da177e4SLinus Torvalds while (!list_empty(&l_inactive)) { 12681da177e4SLinus Torvalds page = lru_to_page(&l_inactive); 12691da177e4SLinus Torvalds prefetchw_prev_lru_page(page, &l_inactive, flags); 12708d438f96SNick Piggin BUG_ON(PageLRU(page)); 12718d438f96SNick Piggin SetPageLRU(page); 12724c84cacfSNick Piggin BUG_ON(!PageActive(page)); 12734c84cacfSNick Piggin ClearPageActive(page); 12744c84cacfSNick Piggin 12751da177e4SLinus Torvalds list_move(&page->lru, &zone->inactive_list); 12761da177e4SLinus Torvalds pgmoved++; 12771da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 12781da177e4SLinus Torvalds zone->nr_inactive += pgmoved; 12791da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12801da177e4SLinus Torvalds pgdeactivate += pgmoved; 12811da177e4SLinus Torvalds pgmoved = 0; 12821da177e4SLinus Torvalds if (buffer_heads_over_limit) 12831da177e4SLinus Torvalds pagevec_strip(&pvec); 12841da177e4SLinus Torvalds __pagevec_release(&pvec); 12851da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds } 12881da177e4SLinus Torvalds zone->nr_inactive += pgmoved; 12891da177e4SLinus Torvalds pgdeactivate += pgmoved; 12901da177e4SLinus Torvalds if (buffer_heads_over_limit) { 12911da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12921da177e4SLinus Torvalds pagevec_strip(&pvec); 12931da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds pgmoved = 0; 12971da177e4SLinus Torvalds while (!list_empty(&l_active)) { 12981da177e4SLinus Torvalds page = lru_to_page(&l_active); 12991da177e4SLinus Torvalds prefetchw_prev_lru_page(page, &l_active, flags); 13008d438f96SNick Piggin BUG_ON(PageLRU(page)); 13018d438f96SNick Piggin SetPageLRU(page); 13021da177e4SLinus Torvalds BUG_ON(!PageActive(page)); 13031da177e4SLinus Torvalds list_move(&page->lru, &zone->active_list); 13041da177e4SLinus Torvalds pgmoved++; 13051da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 13061da177e4SLinus Torvalds zone->nr_active += pgmoved; 13071da177e4SLinus Torvalds pgmoved = 0; 13081da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 13091da177e4SLinus Torvalds __pagevec_release(&pvec); 13101da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds zone->nr_active += pgmoved; 1314a74609faSNick Piggin spin_unlock(&zone->lru_lock); 13151da177e4SLinus Torvalds 1316a74609faSNick Piggin __mod_page_state_zone(zone, pgrefill, pgscanned); 1317a74609faSNick Piggin __mod_page_state(pgdeactivate, pgdeactivate); 1318a74609faSNick Piggin local_irq_enable(); 1319a74609faSNick Piggin 1320a74609faSNick Piggin pagevec_release(&pvec); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /* 13241da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 13251da177e4SLinus Torvalds */ 132605ff5137SAndrew Morton static unsigned long shrink_zone(int priority, struct zone *zone, 132769e05944SAndrew Morton struct scan_control *sc) 13281da177e4SLinus Torvalds { 13291da177e4SLinus Torvalds unsigned long nr_active; 13301da177e4SLinus Torvalds unsigned long nr_inactive; 13318695949aSChristoph Lameter unsigned long nr_to_scan; 133205ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 13331da177e4SLinus Torvalds 133453e9a615SMartin Hicks atomic_inc(&zone->reclaim_in_progress); 133553e9a615SMartin Hicks 13361da177e4SLinus Torvalds /* 13371da177e4SLinus Torvalds * Add one to `nr_to_scan' just to make sure that the kernel will 13381da177e4SLinus Torvalds * slowly sift through the active list. 13391da177e4SLinus Torvalds */ 13408695949aSChristoph Lameter zone->nr_scan_active += (zone->nr_active >> priority) + 1; 13411da177e4SLinus Torvalds nr_active = zone->nr_scan_active; 13421da177e4SLinus Torvalds if (nr_active >= sc->swap_cluster_max) 13431da177e4SLinus Torvalds zone->nr_scan_active = 0; 13441da177e4SLinus Torvalds else 13451da177e4SLinus Torvalds nr_active = 0; 13461da177e4SLinus Torvalds 13478695949aSChristoph Lameter zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1; 13481da177e4SLinus Torvalds nr_inactive = zone->nr_scan_inactive; 13491da177e4SLinus Torvalds if (nr_inactive >= sc->swap_cluster_max) 13501da177e4SLinus Torvalds zone->nr_scan_inactive = 0; 13511da177e4SLinus Torvalds else 13521da177e4SLinus Torvalds nr_inactive = 0; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds while (nr_active || nr_inactive) { 13551da177e4SLinus Torvalds if (nr_active) { 13568695949aSChristoph Lameter nr_to_scan = min(nr_active, 13571da177e4SLinus Torvalds (unsigned long)sc->swap_cluster_max); 13588695949aSChristoph Lameter nr_active -= nr_to_scan; 13591742f19fSAndrew Morton shrink_active_list(nr_to_scan, zone, sc); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds if (nr_inactive) { 13638695949aSChristoph Lameter nr_to_scan = min(nr_inactive, 13641da177e4SLinus Torvalds (unsigned long)sc->swap_cluster_max); 13658695949aSChristoph Lameter nr_inactive -= nr_to_scan; 13661742f19fSAndrew Morton nr_reclaimed += shrink_inactive_list(nr_to_scan, zone, 13671742f19fSAndrew Morton sc); 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds throttle_vm_writeout(); 137253e9a615SMartin Hicks 137353e9a615SMartin Hicks atomic_dec(&zone->reclaim_in_progress); 137405ff5137SAndrew Morton return nr_reclaimed; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds /* 13781da177e4SLinus Torvalds * This is the direct reclaim path, for page-allocating processes. We only 13791da177e4SLinus Torvalds * try to reclaim pages from zones which will satisfy the caller's allocation 13801da177e4SLinus Torvalds * request. 13811da177e4SLinus Torvalds * 13821da177e4SLinus Torvalds * We reclaim from a zone even if that zone is over pages_high. Because: 13831da177e4SLinus Torvalds * a) The caller may be trying to free *extra* pages to satisfy a higher-order 13841da177e4SLinus Torvalds * allocation or 13851da177e4SLinus Torvalds * b) The zones may be over pages_high but they must go *over* pages_high to 13861da177e4SLinus Torvalds * satisfy the `incremental min' zone defense algorithm. 13871da177e4SLinus Torvalds * 13881da177e4SLinus Torvalds * Returns the number of reclaimed pages. 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * If a zone is deemed to be full of pinned pages then just give it a light 13911da177e4SLinus Torvalds * scan then give up on it. 13921da177e4SLinus Torvalds */ 13931742f19fSAndrew Morton static unsigned long shrink_zones(int priority, struct zone **zones, 139469e05944SAndrew Morton struct scan_control *sc) 13951da177e4SLinus Torvalds { 139605ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 13971da177e4SLinus Torvalds int i; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds for (i = 0; zones[i] != NULL; i++) { 14001da177e4SLinus Torvalds struct zone *zone = zones[i]; 14011da177e4SLinus Torvalds 1402f3fe6512SCon Kolivas if (!populated_zone(zone)) 14031da177e4SLinus Torvalds continue; 14041da177e4SLinus Torvalds 14059bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 14061da177e4SLinus Torvalds continue; 14071da177e4SLinus Torvalds 14088695949aSChristoph Lameter zone->temp_priority = priority; 14098695949aSChristoph Lameter if (zone->prev_priority > priority) 14108695949aSChristoph Lameter zone->prev_priority = priority; 14111da177e4SLinus Torvalds 14128695949aSChristoph Lameter if (zone->all_unreclaimable && priority != DEF_PRIORITY) 14131da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 14141da177e4SLinus Torvalds 141505ff5137SAndrew Morton nr_reclaimed += shrink_zone(priority, zone, sc); 14161da177e4SLinus Torvalds } 141705ff5137SAndrew Morton return nr_reclaimed; 14181da177e4SLinus Torvalds } 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /* 14211da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 14221da177e4SLinus Torvalds * 14231da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 14241da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 14251da177e4SLinus Torvalds * 14261da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 14271da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 14281da177e4SLinus Torvalds * caller can't do much about. We kick pdflush and take explicit naps in the 14291da177e4SLinus Torvalds * hope that some of these pages can be written. But if the allocating task 14301da177e4SLinus Torvalds * holds filesystem locks which prevent writeout this might not work, and the 14311da177e4SLinus Torvalds * allocation attempt will fail. 14321da177e4SLinus Torvalds */ 143369e05944SAndrew Morton unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask) 14341da177e4SLinus Torvalds { 14351da177e4SLinus Torvalds int priority; 14361da177e4SLinus Torvalds int ret = 0; 143769e05944SAndrew Morton unsigned long total_scanned = 0; 143805ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 14391da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 14401da177e4SLinus Torvalds unsigned long lru_pages = 0; 14411da177e4SLinus Torvalds int i; 1442179e9639SAndrew Morton struct scan_control sc = { 1443179e9639SAndrew Morton .gfp_mask = gfp_mask, 1444179e9639SAndrew Morton .may_writepage = !laptop_mode, 1445179e9639SAndrew Morton .swap_cluster_max = SWAP_CLUSTER_MAX, 1446179e9639SAndrew Morton .may_swap = 1, 1447179e9639SAndrew Morton }; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds inc_page_state(allocstall); 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds for (i = 0; zones[i] != NULL; i++) { 14521da177e4SLinus Torvalds struct zone *zone = zones[i]; 14531da177e4SLinus Torvalds 14549bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 14551da177e4SLinus Torvalds continue; 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds zone->temp_priority = DEF_PRIORITY; 14581da177e4SLinus Torvalds lru_pages += zone->nr_active + zone->nr_inactive; 14591da177e4SLinus Torvalds } 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 14621da177e4SLinus Torvalds sc.nr_mapped = read_page_state(nr_mapped); 14631da177e4SLinus Torvalds sc.nr_scanned = 0; 1464f7b7fd8fSRik van Riel if (!priority) 1465f7b7fd8fSRik van Riel disable_swap_token(); 14661742f19fSAndrew Morton nr_reclaimed += shrink_zones(priority, zones, &sc); 14671da177e4SLinus Torvalds shrink_slab(sc.nr_scanned, gfp_mask, lru_pages); 14681da177e4SLinus Torvalds if (reclaim_state) { 146905ff5137SAndrew Morton nr_reclaimed += reclaim_state->reclaimed_slab; 14701da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 14711da177e4SLinus Torvalds } 14721da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 147305ff5137SAndrew Morton if (nr_reclaimed >= sc.swap_cluster_max) { 14741da177e4SLinus Torvalds ret = 1; 14751da177e4SLinus Torvalds goto out; 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds /* 14791da177e4SLinus Torvalds * Try to write back as many pages as we just scanned. This 14801da177e4SLinus Torvalds * tends to cause slow streaming writers to write data to the 14811da177e4SLinus Torvalds * disk smoothly, at the dirtying rate, which is nice. But 14821da177e4SLinus Torvalds * that's undesirable in laptop mode, where we *want* lumpy 14831da177e4SLinus Torvalds * writeout. So in laptop mode, write out the whole world. 14841da177e4SLinus Torvalds */ 1485179e9639SAndrew Morton if (total_scanned > sc.swap_cluster_max + 1486179e9639SAndrew Morton sc.swap_cluster_max / 2) { 1487687a21ceSPekka J Enberg wakeup_pdflush(laptop_mode ? 0 : total_scanned); 14881da177e4SLinus Torvalds sc.may_writepage = 1; 14891da177e4SLinus Torvalds } 14901da177e4SLinus Torvalds 14911da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 14921da177e4SLinus Torvalds if (sc.nr_scanned && priority < DEF_PRIORITY - 2) 14931da177e4SLinus Torvalds blk_congestion_wait(WRITE, HZ/10); 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds out: 14961da177e4SLinus Torvalds for (i = 0; zones[i] != 0; i++) { 14971da177e4SLinus Torvalds struct zone *zone = zones[i]; 14981da177e4SLinus Torvalds 14999bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 15001da177e4SLinus Torvalds continue; 15011da177e4SLinus Torvalds 15021da177e4SLinus Torvalds zone->prev_priority = zone->temp_priority; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds return ret; 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 15091da177e4SLinus Torvalds * they are all at pages_high. 15101da177e4SLinus Torvalds * 15111da177e4SLinus Torvalds * If `nr_pages' is non-zero then it is the number of pages which are to be 15121da177e4SLinus Torvalds * reclaimed, regardless of the zone occupancies. This is a software suspend 15131da177e4SLinus Torvalds * special. 15141da177e4SLinus Torvalds * 15151da177e4SLinus Torvalds * Returns the number of pages which were actually freed. 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 15181da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 15191da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 15201da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 15211da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 15221da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 15231da177e4SLinus Torvalds * the zone for when the problem goes away. 15241da177e4SLinus Torvalds * 15251da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 15261da177e4SLinus Torvalds * zones which have free_pages > pages_high, but once a zone is found to have 15271da177e4SLinus Torvalds * free_pages <= pages_high, we scan that zone and the lower zones regardless 15281da177e4SLinus Torvalds * of the number of free pages in the lower zones. This interoperates with 15291da177e4SLinus Torvalds * the page allocator fallback scheme to ensure that aging of pages is balanced 15301da177e4SLinus Torvalds * across the zones. 15311da177e4SLinus Torvalds */ 153269e05944SAndrew Morton static unsigned long balance_pgdat(pg_data_t *pgdat, unsigned long nr_pages, 153369e05944SAndrew Morton int order) 15341da177e4SLinus Torvalds { 153569e05944SAndrew Morton unsigned long to_free = nr_pages; 15361da177e4SLinus Torvalds int all_zones_ok; 15371da177e4SLinus Torvalds int priority; 15381da177e4SLinus Torvalds int i; 153969e05944SAndrew Morton unsigned long total_scanned; 154005ff5137SAndrew Morton unsigned long nr_reclaimed; 15411da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 1542179e9639SAndrew Morton struct scan_control sc = { 1543179e9639SAndrew Morton .gfp_mask = GFP_KERNEL, 1544179e9639SAndrew Morton .may_swap = 1, 1545179e9639SAndrew Morton .swap_cluster_max = nr_pages ? nr_pages : SWAP_CLUSTER_MAX, 1546179e9639SAndrew Morton }; 15471da177e4SLinus Torvalds 15481da177e4SLinus Torvalds loop_again: 15491da177e4SLinus Torvalds total_scanned = 0; 155005ff5137SAndrew Morton nr_reclaimed = 0; 1551179e9639SAndrew Morton sc.may_writepage = !laptop_mode, 15521da177e4SLinus Torvalds sc.nr_mapped = read_page_state(nr_mapped); 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds inc_page_state(pageoutrun); 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds for (i = 0; i < pgdat->nr_zones; i++) { 15571da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds zone->temp_priority = DEF_PRIORITY; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 15631da177e4SLinus Torvalds int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 15641da177e4SLinus Torvalds unsigned long lru_pages = 0; 15651da177e4SLinus Torvalds 1566f7b7fd8fSRik van Riel /* The swap token gets in the way of swapout... */ 1567f7b7fd8fSRik van Riel if (!priority) 1568f7b7fd8fSRik van Riel disable_swap_token(); 1569f7b7fd8fSRik van Riel 15701da177e4SLinus Torvalds all_zones_ok = 1; 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds if (nr_pages == 0) { 15731da177e4SLinus Torvalds /* 15741da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 15751da177e4SLinus Torvalds * zone which needs scanning 15761da177e4SLinus Torvalds */ 15771da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 15781da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 15791da177e4SLinus Torvalds 1580f3fe6512SCon Kolivas if (!populated_zone(zone)) 15811da177e4SLinus Torvalds continue; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds if (zone->all_unreclaimable && 15841da177e4SLinus Torvalds priority != DEF_PRIORITY) 15851da177e4SLinus Torvalds continue; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds if (!zone_watermark_ok(zone, order, 15887fb1d9fcSRohit Seth zone->pages_high, 0, 0)) { 15891da177e4SLinus Torvalds end_zone = i; 15901da177e4SLinus Torvalds goto scan; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds } 15931da177e4SLinus Torvalds goto out; 15941da177e4SLinus Torvalds } else { 15951da177e4SLinus Torvalds end_zone = pgdat->nr_zones - 1; 15961da177e4SLinus Torvalds } 15971da177e4SLinus Torvalds scan: 15981da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 15991da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds lru_pages += zone->nr_active + zone->nr_inactive; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* 16051da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 16061da177e4SLinus Torvalds * at the last zone which needs scanning. 16071da177e4SLinus Torvalds * 16081da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 16091da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 16101da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 16111da177e4SLinus Torvalds * cause too much scanning of the lower zones. 16121da177e4SLinus Torvalds */ 16131da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 16141da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 1615b15e0905Sakpm@osdl.org int nr_slab; 16161da177e4SLinus Torvalds 1617f3fe6512SCon Kolivas if (!populated_zone(zone)) 16181da177e4SLinus Torvalds continue; 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds if (zone->all_unreclaimable && priority != DEF_PRIORITY) 16211da177e4SLinus Torvalds continue; 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds if (nr_pages == 0) { /* Not software suspend */ 16241da177e4SLinus Torvalds if (!zone_watermark_ok(zone, order, 16257fb1d9fcSRohit Seth zone->pages_high, end_zone, 0)) 16261da177e4SLinus Torvalds all_zones_ok = 0; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds zone->temp_priority = priority; 16291da177e4SLinus Torvalds if (zone->prev_priority > priority) 16301da177e4SLinus Torvalds zone->prev_priority = priority; 16311da177e4SLinus Torvalds sc.nr_scanned = 0; 163205ff5137SAndrew Morton nr_reclaimed += shrink_zone(priority, zone, &sc); 16331da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 1634b15e0905Sakpm@osdl.org nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, 1635b15e0905Sakpm@osdl.org lru_pages); 163605ff5137SAndrew Morton nr_reclaimed += reclaim_state->reclaimed_slab; 16371da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 16381da177e4SLinus Torvalds if (zone->all_unreclaimable) 16391da177e4SLinus Torvalds continue; 1640b15e0905Sakpm@osdl.org if (nr_slab == 0 && zone->pages_scanned >= 1641b15e0905Sakpm@osdl.org (zone->nr_active + zone->nr_inactive) * 4) 16421da177e4SLinus Torvalds zone->all_unreclaimable = 1; 16431da177e4SLinus Torvalds /* 16441da177e4SLinus Torvalds * If we've done a decent amount of scanning and 16451da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 16461da177e4SLinus Torvalds * even in laptop mode 16471da177e4SLinus Torvalds */ 16481da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 164905ff5137SAndrew Morton total_scanned > nr_reclaimed + nr_reclaimed / 2) 16501da177e4SLinus Torvalds sc.may_writepage = 1; 16511da177e4SLinus Torvalds } 165205ff5137SAndrew Morton if (nr_pages && to_free > nr_reclaimed) 16531da177e4SLinus Torvalds continue; /* swsusp: need to do more work */ 16541da177e4SLinus Torvalds if (all_zones_ok) 16551da177e4SLinus Torvalds break; /* kswapd: all done */ 16561da177e4SLinus Torvalds /* 16571da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 16581da177e4SLinus Torvalds * another pass across the zones. 16591da177e4SLinus Torvalds */ 16601da177e4SLinus Torvalds if (total_scanned && priority < DEF_PRIORITY - 2) 16611da177e4SLinus Torvalds blk_congestion_wait(WRITE, HZ/10); 16621da177e4SLinus Torvalds 16631da177e4SLinus Torvalds /* 16641da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 16651da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 16661da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 16671da177e4SLinus Torvalds * on zone->*_priority. 16681da177e4SLinus Torvalds */ 166905ff5137SAndrew Morton if ((nr_reclaimed >= SWAP_CLUSTER_MAX) && !nr_pages) 16701da177e4SLinus Torvalds break; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds out: 16731da177e4SLinus Torvalds for (i = 0; i < pgdat->nr_zones; i++) { 16741da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds zone->prev_priority = zone->temp_priority; 16771da177e4SLinus Torvalds } 16781da177e4SLinus Torvalds if (!all_zones_ok) { 16791da177e4SLinus Torvalds cond_resched(); 16801da177e4SLinus Torvalds goto loop_again; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 168305ff5137SAndrew Morton return nr_reclaimed; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 16881da177e4SLinus Torvalds * from the init process. 16891da177e4SLinus Torvalds * 16901da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 16911da177e4SLinus Torvalds * free memory available even if there is no other activity 16921da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 16931da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 16941da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 16951da177e4SLinus Torvalds * 16961da177e4SLinus Torvalds * If there are applications that are active memory-allocators 16971da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 16981da177e4SLinus Torvalds */ 16991da177e4SLinus Torvalds static int kswapd(void *p) 17001da177e4SLinus Torvalds { 17011da177e4SLinus Torvalds unsigned long order; 17021da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 17031da177e4SLinus Torvalds struct task_struct *tsk = current; 17041da177e4SLinus Torvalds DEFINE_WAIT(wait); 17051da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 17061da177e4SLinus Torvalds .reclaimed_slab = 0, 17071da177e4SLinus Torvalds }; 17081da177e4SLinus Torvalds cpumask_t cpumask; 17091da177e4SLinus Torvalds 17101da177e4SLinus Torvalds daemonize("kswapd%d", pgdat->node_id); 17111da177e4SLinus Torvalds cpumask = node_to_cpumask(pgdat->node_id); 17121da177e4SLinus Torvalds if (!cpus_empty(cpumask)) 17131da177e4SLinus Torvalds set_cpus_allowed(tsk, cpumask); 17141da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds /* 17171da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 17181da177e4SLinus Torvalds * and that if we need more memory we should get access to it 17191da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 17201da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 17211da177e4SLinus Torvalds * 17221da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 17231da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 17241da177e4SLinus Torvalds * page out something else, and this flag essentially protects 17251da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 17261da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 17271da177e4SLinus Torvalds */ 1728930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds order = 0; 17311da177e4SLinus Torvalds for ( ; ; ) { 17321da177e4SLinus Torvalds unsigned long new_order; 17333e1d1d28SChristoph Lameter 17343e1d1d28SChristoph Lameter try_to_freeze(); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 17371da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 17381da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 17391da177e4SLinus Torvalds if (order < new_order) { 17401da177e4SLinus Torvalds /* 17411da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 17421da177e4SLinus Torvalds * allocation 17431da177e4SLinus Torvalds */ 17441da177e4SLinus Torvalds order = new_order; 17451da177e4SLinus Torvalds } else { 17461da177e4SLinus Torvalds schedule(); 17471da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds finish_wait(&pgdat->kswapd_wait, &wait); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds balance_pgdat(pgdat, 0, order); 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds return 0; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds /* 17571da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds void wakeup_kswapd(struct zone *zone, int order) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds pg_data_t *pgdat; 17621da177e4SLinus Torvalds 1763f3fe6512SCon Kolivas if (!populated_zone(zone)) 17641da177e4SLinus Torvalds return; 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds pgdat = zone->zone_pgdat; 17677fb1d9fcSRohit Seth if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0)) 17681da177e4SLinus Torvalds return; 17691da177e4SLinus Torvalds if (pgdat->kswapd_max_order < order) 17701da177e4SLinus Torvalds pgdat->kswapd_max_order = order; 17719bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 17721da177e4SLinus Torvalds return; 17738d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 17741da177e4SLinus Torvalds return; 17758d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 17761da177e4SLinus Torvalds } 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds #ifdef CONFIG_PM 17791da177e4SLinus Torvalds /* 17801da177e4SLinus Torvalds * Try to free `nr_pages' of memory, system-wide. Returns the number of freed 17811da177e4SLinus Torvalds * pages. 17821da177e4SLinus Torvalds */ 178369e05944SAndrew Morton unsigned long shrink_all_memory(unsigned long nr_pages) 17841da177e4SLinus Torvalds { 17851da177e4SLinus Torvalds pg_data_t *pgdat; 178669e05944SAndrew Morton unsigned long nr_to_free = nr_pages; 178769e05944SAndrew Morton unsigned long ret = 0; 1788*248a0301SRafael J. Wysocki unsigned retry = 2; 17891da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 17901da177e4SLinus Torvalds .reclaimed_slab = 0, 17911da177e4SLinus Torvalds }; 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 1794*248a0301SRafael J. Wysocki repeat: 17951da177e4SLinus Torvalds for_each_pgdat(pgdat) { 179669e05944SAndrew Morton unsigned long freed; 179769e05944SAndrew Morton 17981da177e4SLinus Torvalds freed = balance_pgdat(pgdat, nr_to_free, 0); 17991da177e4SLinus Torvalds ret += freed; 18001da177e4SLinus Torvalds nr_to_free -= freed; 180169e05944SAndrew Morton if ((long)nr_to_free <= 0) 18021da177e4SLinus Torvalds break; 18031da177e4SLinus Torvalds } 1804*248a0301SRafael J. Wysocki if (retry-- && ret < nr_pages) { 1805*248a0301SRafael J. Wysocki blk_congestion_wait(WRITE, HZ/5); 1806*248a0301SRafael J. Wysocki goto repeat; 1807*248a0301SRafael J. Wysocki } 18081da177e4SLinus Torvalds current->reclaim_state = NULL; 18091da177e4SLinus Torvalds return ret; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds #endif 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 18141da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 18151da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 18161da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 18171da177e4SLinus Torvalds restore their cpu bindings. */ 18181da177e4SLinus Torvalds static int __devinit cpu_callback(struct notifier_block *nfb, 181969e05944SAndrew Morton unsigned long action, void *hcpu) 18201da177e4SLinus Torvalds { 18211da177e4SLinus Torvalds pg_data_t *pgdat; 18221da177e4SLinus Torvalds cpumask_t mask; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds if (action == CPU_ONLINE) { 18251da177e4SLinus Torvalds for_each_pgdat(pgdat) { 18261da177e4SLinus Torvalds mask = node_to_cpumask(pgdat->node_id); 18271da177e4SLinus Torvalds if (any_online_cpu(mask) != NR_CPUS) 18281da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 18291da177e4SLinus Torvalds set_cpus_allowed(pgdat->kswapd, mask); 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds return NOTIFY_OK; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */ 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds static int __init kswapd_init(void) 18371da177e4SLinus Torvalds { 18381da177e4SLinus Torvalds pg_data_t *pgdat; 183969e05944SAndrew Morton 18401da177e4SLinus Torvalds swap_setup(); 184169e05944SAndrew Morton for_each_pgdat(pgdat) { 184269e05944SAndrew Morton pid_t pid; 184369e05944SAndrew Morton 184469e05944SAndrew Morton pid = kernel_thread(kswapd, pgdat, CLONE_KERNEL); 184569e05944SAndrew Morton BUG_ON(pid < 0); 184669e05944SAndrew Morton pgdat->kswapd = find_task_by_pid(pid); 184769e05944SAndrew Morton } 18481da177e4SLinus Torvalds total_memory = nr_free_pagecache_pages(); 18491da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 18501da177e4SLinus Torvalds return 0; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds module_init(kswapd_init) 18549eeff239SChristoph Lameter 18559eeff239SChristoph Lameter #ifdef CONFIG_NUMA 18569eeff239SChristoph Lameter /* 18579eeff239SChristoph Lameter * Zone reclaim mode 18589eeff239SChristoph Lameter * 18599eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 18609eeff239SChristoph Lameter * the watermarks. 18619eeff239SChristoph Lameter * 18629eeff239SChristoph Lameter * In the future we may add flags to the mode. However, the page allocator 18639eeff239SChristoph Lameter * should only have to check that zone_reclaim_mode != 0 before calling 18649eeff239SChristoph Lameter * zone_reclaim(). 18659eeff239SChristoph Lameter */ 18669eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 18679eeff239SChristoph Lameter 18681b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 18691b2ffb78SChristoph Lameter #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */ 18701b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 18711b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 18722a16e3f4SChristoph Lameter #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */ 18731b2ffb78SChristoph Lameter 18749eeff239SChristoph Lameter /* 18759eeff239SChristoph Lameter * Mininum time between zone reclaim scans 18769eeff239SChristoph Lameter */ 18772a11ff06SChristoph Lameter int zone_reclaim_interval __read_mostly = 30*HZ; 1878a92f7126SChristoph Lameter 1879a92f7126SChristoph Lameter /* 1880a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 1881a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 1882a92f7126SChristoph Lameter * a zone. 1883a92f7126SChristoph Lameter */ 1884a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 1885a92f7126SChristoph Lameter 18869eeff239SChristoph Lameter /* 18879eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 18889eeff239SChristoph Lameter */ 1889179e9639SAndrew Morton static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 18909eeff239SChristoph Lameter { 18917fb2d46dSChristoph Lameter /* Minimum pages needed in order to stay on node */ 189269e05944SAndrew Morton const unsigned long nr_pages = 1 << order; 18939eeff239SChristoph Lameter struct task_struct *p = current; 18949eeff239SChristoph Lameter struct reclaim_state reclaim_state; 18958695949aSChristoph Lameter int priority; 189605ff5137SAndrew Morton unsigned long nr_reclaimed = 0; 1897179e9639SAndrew Morton struct scan_control sc = { 1898179e9639SAndrew Morton .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), 1899179e9639SAndrew Morton .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP), 1900179e9639SAndrew Morton .nr_mapped = read_page_state(nr_mapped), 190169e05944SAndrew Morton .swap_cluster_max = max_t(unsigned long, nr_pages, 190269e05944SAndrew Morton SWAP_CLUSTER_MAX), 1903179e9639SAndrew Morton .gfp_mask = gfp_mask, 1904179e9639SAndrew Morton }; 19059eeff239SChristoph Lameter 19069eeff239SChristoph Lameter disable_swap_token(); 19079eeff239SChristoph Lameter cond_resched(); 1908d4f7796eSChristoph Lameter /* 1909d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 1910d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 1911d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 1912d4f7796eSChristoph Lameter */ 1913d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 19149eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 19159eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 1916c84db23cSChristoph Lameter 1917a92f7126SChristoph Lameter /* 1918a92f7126SChristoph Lameter * Free memory by calling shrink zone with increasing priorities 1919a92f7126SChristoph Lameter * until we have enough memory freed. 1920a92f7126SChristoph Lameter */ 19218695949aSChristoph Lameter priority = ZONE_RECLAIM_PRIORITY; 1922a92f7126SChristoph Lameter do { 192305ff5137SAndrew Morton nr_reclaimed += shrink_zone(priority, zone, &sc); 19248695949aSChristoph Lameter priority--; 192505ff5137SAndrew Morton } while (priority >= 0 && nr_reclaimed < nr_pages); 1926a92f7126SChristoph Lameter 192705ff5137SAndrew Morton if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) { 19282a16e3f4SChristoph Lameter /* 19297fb2d46dSChristoph Lameter * shrink_slab() does not currently allow us to determine how 19307fb2d46dSChristoph Lameter * many pages were freed in this zone. So we just shake the slab 19317fb2d46dSChristoph Lameter * a bit and then go off node for this particular allocation 19327fb2d46dSChristoph Lameter * despite possibly having freed enough memory to allocate in 19337fb2d46dSChristoph Lameter * this zone. If we freed local memory then the next 19347fb2d46dSChristoph Lameter * allocations will be local again. 19352a16e3f4SChristoph Lameter * 19362a16e3f4SChristoph Lameter * shrink_slab will free memory on all zones and may take 19372a16e3f4SChristoph Lameter * a long time. 19382a16e3f4SChristoph Lameter */ 19392a16e3f4SChristoph Lameter shrink_slab(sc.nr_scanned, gfp_mask, order); 19402a16e3f4SChristoph Lameter } 19412a16e3f4SChristoph Lameter 19429eeff239SChristoph Lameter p->reclaim_state = NULL; 1943d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 19449eeff239SChristoph Lameter 19457fb2d46dSChristoph Lameter if (nr_reclaimed == 0) { 19467fb2d46dSChristoph Lameter /* 19477fb2d46dSChristoph Lameter * We were unable to reclaim enough pages to stay on node. We 19487fb2d46dSChristoph Lameter * now allow off node accesses for a certain time period before 19497fb2d46dSChristoph Lameter * trying again to reclaim pages from the local zone. 19507fb2d46dSChristoph Lameter */ 19519eeff239SChristoph Lameter zone->last_unsuccessful_zone_reclaim = jiffies; 19527fb2d46dSChristoph Lameter } 19539eeff239SChristoph Lameter 195405ff5137SAndrew Morton return nr_reclaimed >= nr_pages; 19559eeff239SChristoph Lameter } 1956179e9639SAndrew Morton 1957179e9639SAndrew Morton int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 1958179e9639SAndrew Morton { 1959179e9639SAndrew Morton cpumask_t mask; 1960179e9639SAndrew Morton int node_id; 1961179e9639SAndrew Morton 1962179e9639SAndrew Morton /* 1963179e9639SAndrew Morton * Do not reclaim if there was a recent unsuccessful attempt at zone 1964179e9639SAndrew Morton * reclaim. In that case we let allocations go off node for the 1965179e9639SAndrew Morton * zone_reclaim_interval. Otherwise we would scan for each off-node 1966179e9639SAndrew Morton * page allocation. 1967179e9639SAndrew Morton */ 1968179e9639SAndrew Morton if (time_before(jiffies, 1969179e9639SAndrew Morton zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval)) 1970179e9639SAndrew Morton return 0; 1971179e9639SAndrew Morton 1972179e9639SAndrew Morton /* 1973179e9639SAndrew Morton * Avoid concurrent zone reclaims, do not reclaim in a zone that does 1974179e9639SAndrew Morton * not have reclaimable pages and if we should not delay the allocation 1975179e9639SAndrew Morton * then do not scan. 1976179e9639SAndrew Morton */ 1977179e9639SAndrew Morton if (!(gfp_mask & __GFP_WAIT) || 1978179e9639SAndrew Morton zone->all_unreclaimable || 1979179e9639SAndrew Morton atomic_read(&zone->reclaim_in_progress) > 0 || 1980179e9639SAndrew Morton (current->flags & PF_MEMALLOC)) 1981179e9639SAndrew Morton return 0; 1982179e9639SAndrew Morton 1983179e9639SAndrew Morton /* 1984179e9639SAndrew Morton * Only run zone reclaim on the local zone or on zones that do not 1985179e9639SAndrew Morton * have associated processors. This will favor the local processor 1986179e9639SAndrew Morton * over remote processors and spread off node memory allocations 1987179e9639SAndrew Morton * as wide as possible. 1988179e9639SAndrew Morton */ 1989179e9639SAndrew Morton node_id = zone->zone_pgdat->node_id; 1990179e9639SAndrew Morton mask = node_to_cpumask(node_id); 1991179e9639SAndrew Morton if (!cpus_empty(mask) && node_id != numa_node_id()) 1992179e9639SAndrew Morton return 0; 1993179e9639SAndrew Morton return __zone_reclaim(zone, gfp_mask, order); 1994179e9639SAndrew Morton } 19959eeff239SChristoph Lameter #endif 1996