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> 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds #include <asm/tlbflush.h> 381da177e4SLinus Torvalds #include <asm/div64.h> 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <linux/swapops.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* possible outcome of pageout() */ 431da177e4SLinus Torvalds typedef enum { 441da177e4SLinus Torvalds /* failed to write page out, page is locked */ 451da177e4SLinus Torvalds PAGE_KEEP, 461da177e4SLinus Torvalds /* move page to the active list, page is locked */ 471da177e4SLinus Torvalds PAGE_ACTIVATE, 481da177e4SLinus Torvalds /* page has been sent to the disk successfully, page is unlocked */ 491da177e4SLinus Torvalds PAGE_SUCCESS, 501da177e4SLinus Torvalds /* page is clean and locked */ 511da177e4SLinus Torvalds PAGE_CLEAN, 521da177e4SLinus Torvalds } pageout_t; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds struct scan_control { 551da177e4SLinus Torvalds /* Ask refill_inactive_zone, or shrink_cache to scan this many pages */ 561da177e4SLinus Torvalds unsigned long nr_to_scan; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* Incremented by the number of inactive pages that were scanned */ 591da177e4SLinus Torvalds unsigned long nr_scanned; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds /* Incremented by the number of pages reclaimed */ 621da177e4SLinus Torvalds unsigned long nr_reclaimed; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds unsigned long nr_mapped; /* From page_state */ 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* Ask shrink_caches, or shrink_zone to scan at this priority */ 671da177e4SLinus Torvalds unsigned int priority; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* This context's GFP mask */ 706daa0e28SAl Viro gfp_t gfp_mask; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds int may_writepage; 731da177e4SLinus Torvalds 74f1fd1067SChristoph Lameter /* Can pages be swapped as part of reclaim? */ 75f1fd1067SChristoph Lameter int may_swap; 76f1fd1067SChristoph Lameter 771da177e4SLinus Torvalds /* This context's SWAP_CLUSTER_MAX. If freeing memory for 781da177e4SLinus Torvalds * suspend, we effectively ignore SWAP_CLUSTER_MAX. 791da177e4SLinus Torvalds * In this context, it doesn't matter that we scan the 801da177e4SLinus Torvalds * whole list at once. */ 811da177e4SLinus Torvalds int swap_cluster_max; 821da177e4SLinus Torvalds }; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* 851da177e4SLinus Torvalds * The list of shrinker callbacks used by to apply pressure to 861da177e4SLinus Torvalds * ageable caches. 871da177e4SLinus Torvalds */ 881da177e4SLinus Torvalds struct shrinker { 891da177e4SLinus Torvalds shrinker_t shrinker; 901da177e4SLinus Torvalds struct list_head list; 911da177e4SLinus Torvalds int seeks; /* seeks to recreate an obj */ 921da177e4SLinus Torvalds long nr; /* objs pending delete */ 931da177e4SLinus Torvalds }; 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH 981da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) \ 991da177e4SLinus Torvalds do { \ 1001da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1011da177e4SLinus Torvalds struct page *prev; \ 1021da177e4SLinus Torvalds \ 1031da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1041da177e4SLinus Torvalds prefetch(&prev->_field); \ 1051da177e4SLinus Torvalds } \ 1061da177e4SLinus Torvalds } while (0) 1071da177e4SLinus Torvalds #else 1081da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) 1091da177e4SLinus Torvalds #endif 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW 1121da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) \ 1131da177e4SLinus Torvalds do { \ 1141da177e4SLinus Torvalds if ((_page)->lru.prev != _base) { \ 1151da177e4SLinus Torvalds struct page *prev; \ 1161da177e4SLinus Torvalds \ 1171da177e4SLinus Torvalds prev = lru_to_page(&(_page->lru)); \ 1181da177e4SLinus Torvalds prefetchw(&prev->_field); \ 1191da177e4SLinus Torvalds } \ 1201da177e4SLinus Torvalds } while (0) 1211da177e4SLinus Torvalds #else 1221da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) 1231da177e4SLinus Torvalds #endif 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds /* 1261da177e4SLinus Torvalds * From 0 .. 100. Higher means more swappy. 1271da177e4SLinus Torvalds */ 1281da177e4SLinus Torvalds int vm_swappiness = 60; 1291da177e4SLinus Torvalds static long total_memory; 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static LIST_HEAD(shrinker_list); 1321da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds /* 1351da177e4SLinus Torvalds * Add a shrinker callback to be called from the vm 1361da177e4SLinus Torvalds */ 1371da177e4SLinus Torvalds struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker) 1381da177e4SLinus Torvalds { 1391da177e4SLinus Torvalds struct shrinker *shrinker; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL); 1421da177e4SLinus Torvalds if (shrinker) { 1431da177e4SLinus Torvalds shrinker->shrinker = theshrinker; 1441da177e4SLinus Torvalds shrinker->seeks = seeks; 1451da177e4SLinus Torvalds shrinker->nr = 0; 1461da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1471da177e4SLinus Torvalds list_add_tail(&shrinker->list, &shrinker_list); 1481da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds return shrinker; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds EXPORT_SYMBOL(set_shrinker); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* 1551da177e4SLinus Torvalds * Remove one 1561da177e4SLinus Torvalds */ 1571da177e4SLinus Torvalds void remove_shrinker(struct shrinker *shrinker) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds down_write(&shrinker_rwsem); 1601da177e4SLinus Torvalds list_del(&shrinker->list); 1611da177e4SLinus Torvalds up_write(&shrinker_rwsem); 1621da177e4SLinus Torvalds kfree(shrinker); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds EXPORT_SYMBOL(remove_shrinker); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds #define SHRINK_BATCH 128 1671da177e4SLinus Torvalds /* 1681da177e4SLinus Torvalds * Call the shrink functions to age shrinkable caches 1691da177e4SLinus Torvalds * 1701da177e4SLinus Torvalds * Here we assume it costs one seek to replace a lru page and that it also 1711da177e4SLinus Torvalds * takes a seek to recreate a cache object. With this in mind we age equal 1721da177e4SLinus Torvalds * percentages of the lru and ageable caches. This should balance the seeks 1731da177e4SLinus Torvalds * generated by these structures. 1741da177e4SLinus Torvalds * 1751da177e4SLinus Torvalds * If the vm encounted mapped pages on the LRU it increase the pressure on 1761da177e4SLinus Torvalds * slab to avoid swapping. 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. 1791da177e4SLinus Torvalds * 1801da177e4SLinus Torvalds * `lru_pages' represents the number of on-LRU pages in all the zones which 1811da177e4SLinus Torvalds * are eligible for the caller's allocation attempt. It is used for balancing 1821da177e4SLinus Torvalds * slab reclaim versus page reclaim. 183b15e0905Sakpm@osdl.org * 184b15e0905Sakpm@osdl.org * Returns the number of slab objects which we shrunk. 1851da177e4SLinus Torvalds */ 1869d0243bcSAndrew Morton int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds struct shrinker *shrinker; 189b15e0905Sakpm@osdl.org int ret = 0; 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds if (scanned == 0) 1921da177e4SLinus Torvalds scanned = SWAP_CLUSTER_MAX; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds if (!down_read_trylock(&shrinker_rwsem)) 195b15e0905Sakpm@osdl.org return 1; /* Assume we'll be able to shrink next time */ 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds list_for_each_entry(shrinker, &shrinker_list, list) { 1981da177e4SLinus Torvalds unsigned long long delta; 1991da177e4SLinus Torvalds unsigned long total_scan; 200ea164d73SAndrea Arcangeli unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask); 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds delta = (4 * scanned) / shrinker->seeks; 203ea164d73SAndrea Arcangeli delta *= max_pass; 2041da177e4SLinus Torvalds do_div(delta, lru_pages + 1); 2051da177e4SLinus Torvalds shrinker->nr += delta; 206ea164d73SAndrea Arcangeli if (shrinker->nr < 0) { 207ea164d73SAndrea Arcangeli printk(KERN_ERR "%s: nr=%ld\n", 208ea164d73SAndrea Arcangeli __FUNCTION__, shrinker->nr); 209ea164d73SAndrea Arcangeli shrinker->nr = max_pass; 210ea164d73SAndrea Arcangeli } 211ea164d73SAndrea Arcangeli 212ea164d73SAndrea Arcangeli /* 213ea164d73SAndrea Arcangeli * Avoid risking looping forever due to too large nr value: 214ea164d73SAndrea Arcangeli * never try to free more than twice the estimate number of 215ea164d73SAndrea Arcangeli * freeable entries. 216ea164d73SAndrea Arcangeli */ 217ea164d73SAndrea Arcangeli if (shrinker->nr > max_pass * 2) 218ea164d73SAndrea Arcangeli shrinker->nr = max_pass * 2; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds total_scan = shrinker->nr; 2211da177e4SLinus Torvalds shrinker->nr = 0; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds while (total_scan >= SHRINK_BATCH) { 2241da177e4SLinus Torvalds long this_scan = SHRINK_BATCH; 2251da177e4SLinus Torvalds int shrink_ret; 226b15e0905Sakpm@osdl.org int nr_before; 2271da177e4SLinus Torvalds 228b15e0905Sakpm@osdl.org nr_before = (*shrinker->shrinker)(0, gfp_mask); 2291da177e4SLinus Torvalds shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask); 2301da177e4SLinus Torvalds if (shrink_ret == -1) 2311da177e4SLinus Torvalds break; 232b15e0905Sakpm@osdl.org if (shrink_ret < nr_before) 233b15e0905Sakpm@osdl.org ret += nr_before - shrink_ret; 2341da177e4SLinus Torvalds mod_page_state(slabs_scanned, this_scan); 2351da177e4SLinus Torvalds total_scan -= this_scan; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds cond_resched(); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds shrinker->nr += total_scan; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds up_read(&shrinker_rwsem); 243b15e0905Sakpm@osdl.org return ret; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /* Called without lock on whether page is mapped, so answer is unstable */ 2471da177e4SLinus Torvalds static inline int page_mapping_inuse(struct page *page) 2481da177e4SLinus Torvalds { 2491da177e4SLinus Torvalds struct address_space *mapping; 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /* Page is in somebody's page tables. */ 2521da177e4SLinus Torvalds if (page_mapped(page)) 2531da177e4SLinus Torvalds return 1; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* Be more reluctant to reclaim swapcache than pagecache */ 2561da177e4SLinus Torvalds if (PageSwapCache(page)) 2571da177e4SLinus Torvalds return 1; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds mapping = page_mapping(page); 2601da177e4SLinus Torvalds if (!mapping) 2611da177e4SLinus Torvalds return 0; 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /* File is mmap'd by somebody? */ 2641da177e4SLinus Torvalds return mapping_mapped(mapping); 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds return page_count(page) - !!PagePrivate(page) == 2; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static int may_write_to_queue(struct backing_dev_info *bdi) 2731da177e4SLinus Torvalds { 274930d9152SChristoph Lameter if (current->flags & PF_SWAPWRITE) 2751da177e4SLinus Torvalds return 1; 2761da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 2771da177e4SLinus Torvalds return 1; 2781da177e4SLinus Torvalds if (bdi == current->backing_dev_info) 2791da177e4SLinus Torvalds return 1; 2801da177e4SLinus Torvalds return 0; 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds /* 2841da177e4SLinus Torvalds * We detected a synchronous write error writing a page out. Probably 2851da177e4SLinus Torvalds * -ENOSPC. We need to propagate that into the address_space for a subsequent 2861da177e4SLinus Torvalds * fsync(), msync() or close(). 2871da177e4SLinus Torvalds * 2881da177e4SLinus Torvalds * The tricky part is that after writepage we cannot touch the mapping: nothing 2891da177e4SLinus Torvalds * prevents it from being freed up. But we have a ref on the page and once 2901da177e4SLinus Torvalds * that page is locked, the mapping is pinned. 2911da177e4SLinus Torvalds * 2921da177e4SLinus Torvalds * We're allowed to run sleeping lock_page() here because we know the caller has 2931da177e4SLinus Torvalds * __GFP_FS. 2941da177e4SLinus Torvalds */ 2951da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping, 2961da177e4SLinus Torvalds struct page *page, int error) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds lock_page(page); 2991da177e4SLinus Torvalds if (page_mapping(page) == mapping) { 3001da177e4SLinus Torvalds if (error == -ENOSPC) 3011da177e4SLinus Torvalds set_bit(AS_ENOSPC, &mapping->flags); 3021da177e4SLinus Torvalds else 3031da177e4SLinus Torvalds set_bit(AS_EIO, &mapping->flags); 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds unlock_page(page); 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 3091da177e4SLinus Torvalds * pageout is called by shrink_list() for each dirty page. Calls ->writepage(). 3101da177e4SLinus Torvalds */ 3111da177e4SLinus Torvalds static pageout_t pageout(struct page *page, struct address_space *mapping) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * If the page is dirty, only perform writeback if that write 3151da177e4SLinus Torvalds * will be non-blocking. To prevent this allocation from being 3161da177e4SLinus Torvalds * stalled by pagecache activity. But note that there may be 3171da177e4SLinus Torvalds * stalls if we need to run get_block(). We could test 3181da177e4SLinus Torvalds * PagePrivate for that. 3191da177e4SLinus Torvalds * 3201da177e4SLinus Torvalds * If this process is currently in generic_file_write() against 3211da177e4SLinus Torvalds * this page's queue, we can perform writeback even if that 3221da177e4SLinus Torvalds * will block. 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * If the page is swapcache, write it back even if that would 3251da177e4SLinus Torvalds * block, for some throttling. This happens by accident, because 3261da177e4SLinus Torvalds * swap_backing_dev_info is bust: it doesn't reflect the 3271da177e4SLinus Torvalds * congestion state of the swapdevs. Easy to fix, if needed. 3281da177e4SLinus Torvalds * See swapfile.c:page_queue_congested(). 3291da177e4SLinus Torvalds */ 3301da177e4SLinus Torvalds if (!is_page_cache_freeable(page)) 3311da177e4SLinus Torvalds return PAGE_KEEP; 3321da177e4SLinus Torvalds if (!mapping) { 3331da177e4SLinus Torvalds /* 3341da177e4SLinus Torvalds * Some data journaling orphaned pages can have 3351da177e4SLinus Torvalds * page->mapping == NULL while being dirty with clean buffers. 3361da177e4SLinus Torvalds */ 337323aca6cSakpm@osdl.org if (PagePrivate(page)) { 3381da177e4SLinus Torvalds if (try_to_free_buffers(page)) { 3391da177e4SLinus Torvalds ClearPageDirty(page); 3401da177e4SLinus Torvalds printk("%s: orphaned page\n", __FUNCTION__); 3411da177e4SLinus Torvalds return PAGE_CLEAN; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds return PAGE_KEEP; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds if (mapping->a_ops->writepage == NULL) 3471da177e4SLinus Torvalds return PAGE_ACTIVATE; 3481da177e4SLinus Torvalds if (!may_write_to_queue(mapping->backing_dev_info)) 3491da177e4SLinus Torvalds return PAGE_KEEP; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds if (clear_page_dirty_for_io(page)) { 3521da177e4SLinus Torvalds int res; 3531da177e4SLinus Torvalds struct writeback_control wbc = { 3541da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 3551da177e4SLinus Torvalds .nr_to_write = SWAP_CLUSTER_MAX, 3561da177e4SLinus Torvalds .nonblocking = 1, 3571da177e4SLinus Torvalds .for_reclaim = 1, 3581da177e4SLinus Torvalds }; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds SetPageReclaim(page); 3611da177e4SLinus Torvalds res = mapping->a_ops->writepage(page, &wbc); 3621da177e4SLinus Torvalds if (res < 0) 3631da177e4SLinus Torvalds handle_write_error(mapping, page, res); 364994fc28cSZach Brown if (res == AOP_WRITEPAGE_ACTIVATE) { 3651da177e4SLinus Torvalds ClearPageReclaim(page); 3661da177e4SLinus Torvalds return PAGE_ACTIVATE; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds if (!PageWriteback(page)) { 3691da177e4SLinus Torvalds /* synchronous write or broken a_ops? */ 3701da177e4SLinus Torvalds ClearPageReclaim(page); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds return PAGE_SUCCESS; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds return PAGE_CLEAN; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds 37949d2e9ccSChristoph Lameter static int remove_mapping(struct address_space *mapping, struct page *page) 38049d2e9ccSChristoph Lameter { 38149d2e9ccSChristoph Lameter if (!mapping) 38249d2e9ccSChristoph Lameter return 0; /* truncate got there first */ 38349d2e9ccSChristoph Lameter 38449d2e9ccSChristoph Lameter write_lock_irq(&mapping->tree_lock); 38549d2e9ccSChristoph Lameter 38649d2e9ccSChristoph Lameter /* 38749d2e9ccSChristoph Lameter * The non-racy check for busy page. It is critical to check 38849d2e9ccSChristoph Lameter * PageDirty _after_ making sure that the page is freeable and 38949d2e9ccSChristoph Lameter * not in use by anybody. (pagecache + us == 2) 39049d2e9ccSChristoph Lameter */ 39149d2e9ccSChristoph Lameter if (unlikely(page_count(page) != 2)) 39249d2e9ccSChristoph Lameter goto cannot_free; 39349d2e9ccSChristoph Lameter smp_rmb(); 39449d2e9ccSChristoph Lameter if (unlikely(PageDirty(page))) 39549d2e9ccSChristoph Lameter goto cannot_free; 39649d2e9ccSChristoph Lameter 39749d2e9ccSChristoph Lameter if (PageSwapCache(page)) { 39849d2e9ccSChristoph Lameter swp_entry_t swap = { .val = page_private(page) }; 39949d2e9ccSChristoph Lameter __delete_from_swap_cache(page); 40049d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 40149d2e9ccSChristoph Lameter swap_free(swap); 40249d2e9ccSChristoph Lameter __put_page(page); /* The pagecache ref */ 40349d2e9ccSChristoph Lameter return 1; 40449d2e9ccSChristoph Lameter } 40549d2e9ccSChristoph Lameter 40649d2e9ccSChristoph Lameter __remove_from_page_cache(page); 40749d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 40849d2e9ccSChristoph Lameter __put_page(page); 40949d2e9ccSChristoph Lameter return 1; 41049d2e9ccSChristoph Lameter 41149d2e9ccSChristoph Lameter cannot_free: 41249d2e9ccSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 41349d2e9ccSChristoph Lameter return 0; 41449d2e9ccSChristoph Lameter } 41549d2e9ccSChristoph Lameter 4161da177e4SLinus Torvalds /* 4171da177e4SLinus Torvalds * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed 4181da177e4SLinus Torvalds */ 4191da177e4SLinus Torvalds static int shrink_list(struct list_head *page_list, struct scan_control *sc) 4201da177e4SLinus Torvalds { 4211da177e4SLinus Torvalds LIST_HEAD(ret_pages); 4221da177e4SLinus Torvalds struct pagevec freed_pvec; 4231da177e4SLinus Torvalds int pgactivate = 0; 4241da177e4SLinus Torvalds int reclaimed = 0; 4251da177e4SLinus Torvalds 4261da177e4SLinus Torvalds cond_resched(); 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds pagevec_init(&freed_pvec, 1); 4291da177e4SLinus Torvalds while (!list_empty(page_list)) { 4301da177e4SLinus Torvalds struct address_space *mapping; 4311da177e4SLinus Torvalds struct page *page; 4321da177e4SLinus Torvalds int may_enter_fs; 4331da177e4SLinus Torvalds int referenced; 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds cond_resched(); 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds page = lru_to_page(page_list); 4381da177e4SLinus Torvalds list_del(&page->lru); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds if (TestSetPageLocked(page)) 4411da177e4SLinus Torvalds goto keep; 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds BUG_ON(PageActive(page)); 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds sc->nr_scanned++; 44680e43426SChristoph Lameter 44780e43426SChristoph Lameter if (!sc->may_swap && page_mapped(page)) 44880e43426SChristoph Lameter goto keep_locked; 44980e43426SChristoph Lameter 4501da177e4SLinus Torvalds /* Double the slab pressure for mapped and swapcache pages */ 4511da177e4SLinus Torvalds if (page_mapped(page) || PageSwapCache(page)) 4521da177e4SLinus Torvalds sc->nr_scanned++; 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds if (PageWriteback(page)) 4551da177e4SLinus Torvalds goto keep_locked; 4561da177e4SLinus Torvalds 457f7b7fd8fSRik van Riel referenced = page_referenced(page, 1); 4581da177e4SLinus Torvalds /* In active use or really unfreeable? Activate it. */ 4591da177e4SLinus Torvalds if (referenced && page_mapping_inuse(page)) 4601da177e4SLinus Torvalds goto activate_locked; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds #ifdef CONFIG_SWAP 4631da177e4SLinus Torvalds /* 4641da177e4SLinus Torvalds * Anonymous process memory has backing store? 4651da177e4SLinus Torvalds * Try to allocate it some swap space here. 4661da177e4SLinus Torvalds */ 467c340010eSLee Schermerhorn if (PageAnon(page) && !PageSwapCache(page)) { 468f1fd1067SChristoph Lameter if (!sc->may_swap) 469f1fd1067SChristoph Lameter goto keep_locked; 4701480a540SChristoph Lameter if (!add_to_swap(page, GFP_ATOMIC)) 4711da177e4SLinus Torvalds goto activate_locked; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds #endif /* CONFIG_SWAP */ 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds mapping = page_mapping(page); 4761da177e4SLinus Torvalds may_enter_fs = (sc->gfp_mask & __GFP_FS) || 4771da177e4SLinus Torvalds (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* 4801da177e4SLinus Torvalds * The page is mapped into the page tables of one or more 4811da177e4SLinus Torvalds * processes. Try to unmap it here. 4821da177e4SLinus Torvalds */ 4831da177e4SLinus Torvalds if (page_mapped(page) && mapping) { 484aa3f18b3SChristoph Lameter /* 485aa3f18b3SChristoph Lameter * No unmapping if we do not swap 486aa3f18b3SChristoph Lameter */ 487aa3f18b3SChristoph Lameter if (!sc->may_swap) 488aa3f18b3SChristoph Lameter goto keep_locked; 489aa3f18b3SChristoph Lameter 490a48d07afSChristoph Lameter switch (try_to_unmap(page, 0)) { 4911da177e4SLinus Torvalds case SWAP_FAIL: 4921da177e4SLinus Torvalds goto activate_locked; 4931da177e4SLinus Torvalds case SWAP_AGAIN: 4941da177e4SLinus Torvalds goto keep_locked; 4951da177e4SLinus Torvalds case SWAP_SUCCESS: 4961da177e4SLinus Torvalds ; /* try to free the page below */ 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds if (PageDirty(page)) { 5011da177e4SLinus Torvalds if (referenced) 5021da177e4SLinus Torvalds goto keep_locked; 5031da177e4SLinus Torvalds if (!may_enter_fs) 5041da177e4SLinus Torvalds goto keep_locked; 50552a8363eSChristoph Lameter if (!sc->may_writepage) 5061da177e4SLinus Torvalds goto keep_locked; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds /* Page is dirty, try to write it out here */ 5091da177e4SLinus Torvalds switch(pageout(page, mapping)) { 5101da177e4SLinus Torvalds case PAGE_KEEP: 5111da177e4SLinus Torvalds goto keep_locked; 5121da177e4SLinus Torvalds case PAGE_ACTIVATE: 5131da177e4SLinus Torvalds goto activate_locked; 5141da177e4SLinus Torvalds case PAGE_SUCCESS: 5151da177e4SLinus Torvalds if (PageWriteback(page) || PageDirty(page)) 5161da177e4SLinus Torvalds goto keep; 5171da177e4SLinus Torvalds /* 5181da177e4SLinus Torvalds * A synchronous write - probably a ramdisk. Go 5191da177e4SLinus Torvalds * ahead and try to reclaim the page. 5201da177e4SLinus Torvalds */ 5211da177e4SLinus Torvalds if (TestSetPageLocked(page)) 5221da177e4SLinus Torvalds goto keep; 5231da177e4SLinus Torvalds if (PageDirty(page) || PageWriteback(page)) 5241da177e4SLinus Torvalds goto keep_locked; 5251da177e4SLinus Torvalds mapping = page_mapping(page); 5261da177e4SLinus Torvalds case PAGE_CLEAN: 5271da177e4SLinus Torvalds ; /* try to free the page below */ 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 5311da177e4SLinus Torvalds /* 5321da177e4SLinus Torvalds * If the page has buffers, try to free the buffer mappings 5331da177e4SLinus Torvalds * associated with this page. If we succeed we try to free 5341da177e4SLinus Torvalds * the page as well. 5351da177e4SLinus Torvalds * 5361da177e4SLinus Torvalds * We do this even if the page is PageDirty(). 5371da177e4SLinus Torvalds * try_to_release_page() does not perform I/O, but it is 5381da177e4SLinus Torvalds * possible for a page to have PageDirty set, but it is actually 5391da177e4SLinus Torvalds * clean (all its buffers are clean). This happens if the 5401da177e4SLinus Torvalds * buffers were written out directly, with submit_bh(). ext3 5411da177e4SLinus Torvalds * will do this, as well as the blockdev mapping. 5421da177e4SLinus Torvalds * try_to_release_page() will discover that cleanness and will 5431da177e4SLinus Torvalds * drop the buffers and mark the page clean - it can be freed. 5441da177e4SLinus Torvalds * 5451da177e4SLinus Torvalds * Rarely, pages can have buffers and no ->mapping. These are 5461da177e4SLinus Torvalds * the pages which were not successfully invalidated in 5471da177e4SLinus Torvalds * truncate_complete_page(). We try to drop those buffers here 5481da177e4SLinus Torvalds * and if that worked, and the page is no longer mapped into 5491da177e4SLinus Torvalds * process address space (page_count == 1) it can be freed. 5501da177e4SLinus Torvalds * Otherwise, leave the page on the LRU so it is swappable. 5511da177e4SLinus Torvalds */ 5521da177e4SLinus Torvalds if (PagePrivate(page)) { 5531da177e4SLinus Torvalds if (!try_to_release_page(page, sc->gfp_mask)) 5541da177e4SLinus Torvalds goto activate_locked; 5551da177e4SLinus Torvalds if (!mapping && page_count(page) == 1) 5561da177e4SLinus Torvalds goto free_it; 5571da177e4SLinus Torvalds } 5581da177e4SLinus Torvalds 55949d2e9ccSChristoph Lameter if (!remove_mapping(mapping, page)) 56049d2e9ccSChristoph Lameter goto keep_locked; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds free_it: 5631da177e4SLinus Torvalds unlock_page(page); 5641da177e4SLinus Torvalds reclaimed++; 5651da177e4SLinus Torvalds if (!pagevec_add(&freed_pvec, page)) 5661da177e4SLinus Torvalds __pagevec_release_nonlru(&freed_pvec); 5671da177e4SLinus Torvalds continue; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds activate_locked: 5701da177e4SLinus Torvalds SetPageActive(page); 5711da177e4SLinus Torvalds pgactivate++; 5721da177e4SLinus Torvalds keep_locked: 5731da177e4SLinus Torvalds unlock_page(page); 5741da177e4SLinus Torvalds keep: 5751da177e4SLinus Torvalds list_add(&page->lru, &ret_pages); 5761da177e4SLinus Torvalds BUG_ON(PageLRU(page)); 5771da177e4SLinus Torvalds } 5781da177e4SLinus Torvalds list_splice(&ret_pages, page_list); 5791da177e4SLinus Torvalds if (pagevec_count(&freed_pvec)) 5801da177e4SLinus Torvalds __pagevec_release_nonlru(&freed_pvec); 5811da177e4SLinus Torvalds mod_page_state(pgactivate, pgactivate); 5821da177e4SLinus Torvalds sc->nr_reclaimed += reclaimed; 5831da177e4SLinus Torvalds return reclaimed; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5867cbe34cfSChristoph Lameter #ifdef CONFIG_MIGRATION 5878419c318SChristoph Lameter static inline void move_to_lru(struct page *page) 5888419c318SChristoph Lameter { 5898419c318SChristoph Lameter list_del(&page->lru); 5908419c318SChristoph Lameter if (PageActive(page)) { 5918419c318SChristoph Lameter /* 5928419c318SChristoph Lameter * lru_cache_add_active checks that 5938419c318SChristoph Lameter * the PG_active bit is off. 5948419c318SChristoph Lameter */ 5958419c318SChristoph Lameter ClearPageActive(page); 5968419c318SChristoph Lameter lru_cache_add_active(page); 5978419c318SChristoph Lameter } else { 5988419c318SChristoph Lameter lru_cache_add(page); 5998419c318SChristoph Lameter } 6008419c318SChristoph Lameter put_page(page); 6018419c318SChristoph Lameter } 6028419c318SChristoph Lameter 6038419c318SChristoph Lameter /* 604053837fcSNick Piggin * Add isolated pages on the list back to the LRU. 6058419c318SChristoph Lameter * 6068419c318SChristoph Lameter * returns the number of pages put back. 6078419c318SChristoph Lameter */ 6088419c318SChristoph Lameter int putback_lru_pages(struct list_head *l) 6098419c318SChristoph Lameter { 6108419c318SChristoph Lameter struct page *page; 6118419c318SChristoph Lameter struct page *page2; 6128419c318SChristoph Lameter int count = 0; 6138419c318SChristoph Lameter 6148419c318SChristoph Lameter list_for_each_entry_safe(page, page2, l, lru) { 6158419c318SChristoph Lameter move_to_lru(page); 6168419c318SChristoph Lameter count++; 6178419c318SChristoph Lameter } 6188419c318SChristoph Lameter return count; 6198419c318SChristoph Lameter } 6208419c318SChristoph Lameter 6211da177e4SLinus Torvalds /* 622e965f963SChristoph Lameter * Non migratable page 623e965f963SChristoph Lameter */ 624e965f963SChristoph Lameter int fail_migrate_page(struct page *newpage, struct page *page) 625e965f963SChristoph Lameter { 626e965f963SChristoph Lameter return -EIO; 627e965f963SChristoph Lameter } 628e965f963SChristoph Lameter EXPORT_SYMBOL(fail_migrate_page); 629e965f963SChristoph Lameter 630e965f963SChristoph Lameter /* 63149d2e9ccSChristoph Lameter * swapout a single page 63249d2e9ccSChristoph Lameter * page is locked upon entry, unlocked on exit 63349d2e9ccSChristoph Lameter */ 63449d2e9ccSChristoph Lameter static int swap_page(struct page *page) 63549d2e9ccSChristoph Lameter { 63649d2e9ccSChristoph Lameter struct address_space *mapping = page_mapping(page); 63749d2e9ccSChristoph Lameter 63849d2e9ccSChristoph Lameter if (page_mapped(page) && mapping) 639418aade4SChristoph Lameter if (try_to_unmap(page, 1) != SWAP_SUCCESS) 64049d2e9ccSChristoph Lameter goto unlock_retry; 64149d2e9ccSChristoph Lameter 64249d2e9ccSChristoph Lameter if (PageDirty(page)) { 64349d2e9ccSChristoph Lameter /* Page is dirty, try to write it out here */ 64449d2e9ccSChristoph Lameter switch(pageout(page, mapping)) { 64549d2e9ccSChristoph Lameter case PAGE_KEEP: 64649d2e9ccSChristoph Lameter case PAGE_ACTIVATE: 64749d2e9ccSChristoph Lameter goto unlock_retry; 64849d2e9ccSChristoph Lameter 64949d2e9ccSChristoph Lameter case PAGE_SUCCESS: 65049d2e9ccSChristoph Lameter goto retry; 65149d2e9ccSChristoph Lameter 65249d2e9ccSChristoph Lameter case PAGE_CLEAN: 65349d2e9ccSChristoph Lameter ; /* try to free the page below */ 65449d2e9ccSChristoph Lameter } 65549d2e9ccSChristoph Lameter } 65649d2e9ccSChristoph Lameter 65749d2e9ccSChristoph Lameter if (PagePrivate(page)) { 65849d2e9ccSChristoph Lameter if (!try_to_release_page(page, GFP_KERNEL) || 65949d2e9ccSChristoph Lameter (!mapping && page_count(page) == 1)) 66049d2e9ccSChristoph Lameter goto unlock_retry; 66149d2e9ccSChristoph Lameter } 66249d2e9ccSChristoph Lameter 66349d2e9ccSChristoph Lameter if (remove_mapping(mapping, page)) { 66449d2e9ccSChristoph Lameter /* Success */ 66549d2e9ccSChristoph Lameter unlock_page(page); 66649d2e9ccSChristoph Lameter return 0; 66749d2e9ccSChristoph Lameter } 66849d2e9ccSChristoph Lameter 66949d2e9ccSChristoph Lameter unlock_retry: 67049d2e9ccSChristoph Lameter unlock_page(page); 67149d2e9ccSChristoph Lameter 67249d2e9ccSChristoph Lameter retry: 673d0d96328SChristoph Lameter return -EAGAIN; 67449d2e9ccSChristoph Lameter } 675e965f963SChristoph Lameter EXPORT_SYMBOL(swap_page); 676a48d07afSChristoph Lameter 677a48d07afSChristoph Lameter /* 678a48d07afSChristoph Lameter * Page migration was first developed in the context of the memory hotplug 679a48d07afSChristoph Lameter * project. The main authors of the migration code are: 680a48d07afSChristoph Lameter * 681a48d07afSChristoph Lameter * IWAMOTO Toshihiro <iwamoto@valinux.co.jp> 682a48d07afSChristoph Lameter * Hirokazu Takahashi <taka@valinux.co.jp> 683a48d07afSChristoph Lameter * Dave Hansen <haveblue@us.ibm.com> 684a48d07afSChristoph Lameter * Christoph Lameter <clameter@sgi.com> 685a48d07afSChristoph Lameter */ 686a48d07afSChristoph Lameter 687a48d07afSChristoph Lameter /* 688a48d07afSChristoph Lameter * Remove references for a page and establish the new page with the correct 689a48d07afSChristoph Lameter * basic settings to be able to stop accesses to the page. 690a48d07afSChristoph Lameter */ 691e965f963SChristoph Lameter int migrate_page_remove_references(struct page *newpage, 692a48d07afSChristoph Lameter struct page *page, int nr_refs) 693a48d07afSChristoph Lameter { 694a48d07afSChristoph Lameter struct address_space *mapping = page_mapping(page); 695a48d07afSChristoph Lameter struct page **radix_pointer; 696a48d07afSChristoph Lameter 697a48d07afSChristoph Lameter /* 698a48d07afSChristoph Lameter * Avoid doing any of the following work if the page count 699a48d07afSChristoph Lameter * indicates that the page is in use or truncate has removed 700a48d07afSChristoph Lameter * the page. 701a48d07afSChristoph Lameter */ 702a48d07afSChristoph Lameter if (!mapping || page_mapcount(page) + nr_refs != page_count(page)) 703*4983da07SChristoph Lameter return -EAGAIN; 704a48d07afSChristoph Lameter 705a48d07afSChristoph Lameter /* 706a48d07afSChristoph Lameter * Establish swap ptes for anonymous pages or destroy pte 707a48d07afSChristoph Lameter * maps for files. 708a48d07afSChristoph Lameter * 709a48d07afSChristoph Lameter * In order to reestablish file backed mappings the fault handlers 710a48d07afSChristoph Lameter * will take the radix tree_lock which may then be used to stop 711a48d07afSChristoph Lameter * processses from accessing this page until the new page is ready. 712a48d07afSChristoph Lameter * 713a48d07afSChristoph Lameter * A process accessing via a swap pte (an anonymous page) will take a 714a48d07afSChristoph Lameter * page_lock on the old page which will block the process until the 715a48d07afSChristoph Lameter * migration attempt is complete. At that time the PageSwapCache bit 716a48d07afSChristoph Lameter * will be examined. If the page was migrated then the PageSwapCache 717a48d07afSChristoph Lameter * bit will be clear and the operation to retrieve the page will be 718a48d07afSChristoph Lameter * retried which will find the new page in the radix tree. Then a new 719a48d07afSChristoph Lameter * direct mapping may be generated based on the radix tree contents. 720a48d07afSChristoph Lameter * 721a48d07afSChristoph Lameter * If the page was not migrated then the PageSwapCache bit 722a48d07afSChristoph Lameter * is still set and the operation may continue. 723a48d07afSChristoph Lameter */ 724*4983da07SChristoph Lameter if (try_to_unmap(page, 1) == SWAP_FAIL) 725*4983da07SChristoph Lameter /* A vma has VM_LOCKED set -> Permanent failure */ 726*4983da07SChristoph Lameter return -EPERM; 727a48d07afSChristoph Lameter 728a48d07afSChristoph Lameter /* 729a48d07afSChristoph Lameter * Give up if we were unable to remove all mappings. 730a48d07afSChristoph Lameter */ 731a48d07afSChristoph Lameter if (page_mapcount(page)) 732*4983da07SChristoph Lameter return -EAGAIN; 733a48d07afSChristoph Lameter 734a48d07afSChristoph Lameter write_lock_irq(&mapping->tree_lock); 735a48d07afSChristoph Lameter 736a48d07afSChristoph Lameter radix_pointer = (struct page **)radix_tree_lookup_slot( 737a48d07afSChristoph Lameter &mapping->page_tree, 738a48d07afSChristoph Lameter page_index(page)); 739a48d07afSChristoph Lameter 740a48d07afSChristoph Lameter if (!page_mapping(page) || page_count(page) != nr_refs || 741a48d07afSChristoph Lameter *radix_pointer != page) { 742a48d07afSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 743*4983da07SChristoph Lameter return -EAGAIN; 744a48d07afSChristoph Lameter } 745a48d07afSChristoph Lameter 746a48d07afSChristoph Lameter /* 747a48d07afSChristoph Lameter * Now we know that no one else is looking at the page. 748a48d07afSChristoph Lameter * 749a48d07afSChristoph Lameter * Certain minimal information about a page must be available 750a48d07afSChristoph Lameter * in order for other subsystems to properly handle the page if they 751a48d07afSChristoph Lameter * find it through the radix tree update before we are finished 752a48d07afSChristoph Lameter * copying the page. 753a48d07afSChristoph Lameter */ 754a48d07afSChristoph Lameter get_page(newpage); 755a48d07afSChristoph Lameter newpage->index = page->index; 756a48d07afSChristoph Lameter newpage->mapping = page->mapping; 757a48d07afSChristoph Lameter if (PageSwapCache(page)) { 758a48d07afSChristoph Lameter SetPageSwapCache(newpage); 759a48d07afSChristoph Lameter set_page_private(newpage, page_private(page)); 760a48d07afSChristoph Lameter } 761a48d07afSChristoph Lameter 762a48d07afSChristoph Lameter *radix_pointer = newpage; 763a48d07afSChristoph Lameter __put_page(page); 764a48d07afSChristoph Lameter write_unlock_irq(&mapping->tree_lock); 765a48d07afSChristoph Lameter 766a48d07afSChristoph Lameter return 0; 767a48d07afSChristoph Lameter } 768e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page_remove_references); 769a48d07afSChristoph Lameter 770a48d07afSChristoph Lameter /* 771a48d07afSChristoph Lameter * Copy the page to its new location 772a48d07afSChristoph Lameter */ 773a48d07afSChristoph Lameter void migrate_page_copy(struct page *newpage, struct page *page) 774a48d07afSChristoph Lameter { 775a48d07afSChristoph Lameter copy_highpage(newpage, page); 776a48d07afSChristoph Lameter 777a48d07afSChristoph Lameter if (PageError(page)) 778a48d07afSChristoph Lameter SetPageError(newpage); 779a48d07afSChristoph Lameter if (PageReferenced(page)) 780a48d07afSChristoph Lameter SetPageReferenced(newpage); 781a48d07afSChristoph Lameter if (PageUptodate(page)) 782a48d07afSChristoph Lameter SetPageUptodate(newpage); 783a48d07afSChristoph Lameter if (PageActive(page)) 784a48d07afSChristoph Lameter SetPageActive(newpage); 785a48d07afSChristoph Lameter if (PageChecked(page)) 786a48d07afSChristoph Lameter SetPageChecked(newpage); 787a48d07afSChristoph Lameter if (PageMappedToDisk(page)) 788a48d07afSChristoph Lameter SetPageMappedToDisk(newpage); 789a48d07afSChristoph Lameter 790a48d07afSChristoph Lameter if (PageDirty(page)) { 791a48d07afSChristoph Lameter clear_page_dirty_for_io(page); 792a48d07afSChristoph Lameter set_page_dirty(newpage); 793a48d07afSChristoph Lameter } 794a48d07afSChristoph Lameter 795a48d07afSChristoph Lameter ClearPageSwapCache(page); 796a48d07afSChristoph Lameter ClearPageActive(page); 797a48d07afSChristoph Lameter ClearPagePrivate(page); 798a48d07afSChristoph Lameter set_page_private(page, 0); 799a48d07afSChristoph Lameter page->mapping = NULL; 800a48d07afSChristoph Lameter 801a48d07afSChristoph Lameter /* 802a48d07afSChristoph Lameter * If any waiters have accumulated on the new page then 803a48d07afSChristoph Lameter * wake them up. 804a48d07afSChristoph Lameter */ 805a48d07afSChristoph Lameter if (PageWriteback(newpage)) 806a48d07afSChristoph Lameter end_page_writeback(newpage); 807a48d07afSChristoph Lameter } 808e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page_copy); 809a48d07afSChristoph Lameter 810a48d07afSChristoph Lameter /* 811a48d07afSChristoph Lameter * Common logic to directly migrate a single page suitable for 812a48d07afSChristoph Lameter * pages that do not use PagePrivate. 813a48d07afSChristoph Lameter * 814a48d07afSChristoph Lameter * Pages are locked upon entry and exit. 815a48d07afSChristoph Lameter */ 816a48d07afSChristoph Lameter int migrate_page(struct page *newpage, struct page *page) 817a48d07afSChristoph Lameter { 818*4983da07SChristoph Lameter int rc; 819*4983da07SChristoph Lameter 820a48d07afSChristoph Lameter BUG_ON(PageWriteback(page)); /* Writeback must be complete */ 821a48d07afSChristoph Lameter 822*4983da07SChristoph Lameter rc = migrate_page_remove_references(newpage, page, 2); 823*4983da07SChristoph Lameter 824*4983da07SChristoph Lameter if (rc) 825*4983da07SChristoph Lameter return rc; 826a48d07afSChristoph Lameter 827a48d07afSChristoph Lameter migrate_page_copy(newpage, page); 828a48d07afSChristoph Lameter 829a3351e52SChristoph Lameter /* 830a3351e52SChristoph Lameter * Remove auxiliary swap entries and replace 831a3351e52SChristoph Lameter * them with real ptes. 832a3351e52SChristoph Lameter * 833a3351e52SChristoph Lameter * Note that a real pte entry will allow processes that are not 834a3351e52SChristoph Lameter * waiting on the page lock to use the new page via the page tables 835a3351e52SChristoph Lameter * before the new page is unlocked. 836a3351e52SChristoph Lameter */ 837a3351e52SChristoph Lameter remove_from_swap(newpage); 838a48d07afSChristoph Lameter return 0; 839a48d07afSChristoph Lameter } 840e965f963SChristoph Lameter EXPORT_SYMBOL(migrate_page); 841a48d07afSChristoph Lameter 84249d2e9ccSChristoph Lameter /* 84349d2e9ccSChristoph Lameter * migrate_pages 84449d2e9ccSChristoph Lameter * 84549d2e9ccSChristoph Lameter * Two lists are passed to this function. The first list 84649d2e9ccSChristoph Lameter * contains the pages isolated from the LRU to be migrated. 84749d2e9ccSChristoph Lameter * The second list contains new pages that the pages isolated 84849d2e9ccSChristoph Lameter * can be moved to. If the second list is NULL then all 84949d2e9ccSChristoph Lameter * pages are swapped out. 85049d2e9ccSChristoph Lameter * 85149d2e9ccSChristoph Lameter * The function returns after 10 attempts or if no pages 852418aade4SChristoph Lameter * are movable anymore because to has become empty 85349d2e9ccSChristoph Lameter * or no retryable pages exist anymore. 85449d2e9ccSChristoph Lameter * 855d0d96328SChristoph Lameter * Return: Number of pages not migrated when "to" ran empty. 85649d2e9ccSChristoph Lameter */ 857d4984711SChristoph Lameter int migrate_pages(struct list_head *from, struct list_head *to, 858d4984711SChristoph Lameter struct list_head *moved, struct list_head *failed) 85949d2e9ccSChristoph Lameter { 86049d2e9ccSChristoph Lameter int retry; 86149d2e9ccSChristoph Lameter int nr_failed = 0; 86249d2e9ccSChristoph Lameter int pass = 0; 86349d2e9ccSChristoph Lameter struct page *page; 86449d2e9ccSChristoph Lameter struct page *page2; 86549d2e9ccSChristoph Lameter int swapwrite = current->flags & PF_SWAPWRITE; 866d0d96328SChristoph Lameter int rc; 86749d2e9ccSChristoph Lameter 86849d2e9ccSChristoph Lameter if (!swapwrite) 86949d2e9ccSChristoph Lameter current->flags |= PF_SWAPWRITE; 87049d2e9ccSChristoph Lameter 87149d2e9ccSChristoph Lameter redo: 87249d2e9ccSChristoph Lameter retry = 0; 87349d2e9ccSChristoph Lameter 874d4984711SChristoph Lameter list_for_each_entry_safe(page, page2, from, lru) { 875a48d07afSChristoph Lameter struct page *newpage = NULL; 876a48d07afSChristoph Lameter struct address_space *mapping; 877a48d07afSChristoph Lameter 87849d2e9ccSChristoph Lameter cond_resched(); 87949d2e9ccSChristoph Lameter 880d0d96328SChristoph Lameter rc = 0; 881d0d96328SChristoph Lameter if (page_count(page) == 1) 882ee27497dSChristoph Lameter /* page was freed from under us. So we are done. */ 883d0d96328SChristoph Lameter goto next; 884d0d96328SChristoph Lameter 885a48d07afSChristoph Lameter if (to && list_empty(to)) 886a48d07afSChristoph Lameter break; 887a48d07afSChristoph Lameter 88849d2e9ccSChristoph Lameter /* 88949d2e9ccSChristoph Lameter * Skip locked pages during the first two passes to give the 8907cbe34cfSChristoph Lameter * functions holding the lock time to release the page. Later we 8917cbe34cfSChristoph Lameter * use lock_page() to have a higher chance of acquiring the 8927cbe34cfSChristoph Lameter * lock. 89349d2e9ccSChristoph Lameter */ 894d0d96328SChristoph Lameter rc = -EAGAIN; 89549d2e9ccSChristoph Lameter if (pass > 2) 89649d2e9ccSChristoph Lameter lock_page(page); 89749d2e9ccSChristoph Lameter else 89849d2e9ccSChristoph Lameter if (TestSetPageLocked(page)) 899d0d96328SChristoph Lameter goto next; 90049d2e9ccSChristoph Lameter 90149d2e9ccSChristoph Lameter /* 90249d2e9ccSChristoph Lameter * Only wait on writeback if we have already done a pass where 90349d2e9ccSChristoph Lameter * we we may have triggered writeouts for lots of pages. 90449d2e9ccSChristoph Lameter */ 9057cbe34cfSChristoph Lameter if (pass > 0) { 90649d2e9ccSChristoph Lameter wait_on_page_writeback(page); 9077cbe34cfSChristoph Lameter } else { 908d0d96328SChristoph Lameter if (PageWriteback(page)) 909d0d96328SChristoph Lameter goto unlock_page; 9107cbe34cfSChristoph Lameter } 91149d2e9ccSChristoph Lameter 912d0d96328SChristoph Lameter /* 913d0d96328SChristoph Lameter * Anonymous pages must have swap cache references otherwise 914d0d96328SChristoph Lameter * the information contained in the page maps cannot be 915d0d96328SChristoph Lameter * preserved. 916d0d96328SChristoph Lameter */ 91749d2e9ccSChristoph Lameter if (PageAnon(page) && !PageSwapCache(page)) { 9181480a540SChristoph Lameter if (!add_to_swap(page, GFP_KERNEL)) { 919d0d96328SChristoph Lameter rc = -ENOMEM; 920d0d96328SChristoph Lameter goto unlock_page; 92149d2e9ccSChristoph Lameter } 92249d2e9ccSChristoph Lameter } 92349d2e9ccSChristoph Lameter 924a48d07afSChristoph Lameter if (!to) { 925d0d96328SChristoph Lameter rc = swap_page(page); 926d0d96328SChristoph Lameter goto next; 927a48d07afSChristoph Lameter } 928a48d07afSChristoph Lameter 929a48d07afSChristoph Lameter newpage = lru_to_page(to); 930a48d07afSChristoph Lameter lock_page(newpage); 931a48d07afSChristoph Lameter 932a48d07afSChristoph Lameter /* 933a48d07afSChristoph Lameter * Pages are properly locked and writeback is complete. 934a48d07afSChristoph Lameter * Try to migrate the page. 935a48d07afSChristoph Lameter */ 936a48d07afSChristoph Lameter mapping = page_mapping(page); 937a48d07afSChristoph Lameter if (!mapping) 938a48d07afSChristoph Lameter goto unlock_both; 939a48d07afSChristoph Lameter 940e965f963SChristoph Lameter if (mapping->a_ops->migratepage) { 941418aade4SChristoph Lameter /* 942418aade4SChristoph Lameter * Most pages have a mapping and most filesystems 943418aade4SChristoph Lameter * should provide a migration function. Anonymous 944418aade4SChristoph Lameter * pages are part of swap space which also has its 945418aade4SChristoph Lameter * own migration function. This is the most common 946418aade4SChristoph Lameter * path for page migration. 947418aade4SChristoph Lameter */ 948e965f963SChristoph Lameter rc = mapping->a_ops->migratepage(newpage, page); 949e965f963SChristoph Lameter goto unlock_both; 950e965f963SChristoph Lameter } 951e965f963SChristoph Lameter 952a48d07afSChristoph Lameter /* 953418aade4SChristoph Lameter * Default handling if a filesystem does not provide 954418aade4SChristoph Lameter * a migration function. We can only migrate clean 955418aade4SChristoph Lameter * pages so try to write out any dirty pages first. 956a48d07afSChristoph Lameter */ 957a48d07afSChristoph Lameter if (PageDirty(page)) { 958a48d07afSChristoph Lameter switch (pageout(page, mapping)) { 959a48d07afSChristoph Lameter case PAGE_KEEP: 960a48d07afSChristoph Lameter case PAGE_ACTIVATE: 961a48d07afSChristoph Lameter goto unlock_both; 962a48d07afSChristoph Lameter 963a48d07afSChristoph Lameter case PAGE_SUCCESS: 964a48d07afSChristoph Lameter unlock_page(newpage); 965a48d07afSChristoph Lameter goto next; 966a48d07afSChristoph Lameter 967a48d07afSChristoph Lameter case PAGE_CLEAN: 968a48d07afSChristoph Lameter ; /* try to migrate the page below */ 969a48d07afSChristoph Lameter } 970a48d07afSChristoph Lameter } 971418aade4SChristoph Lameter 972a48d07afSChristoph Lameter /* 973418aade4SChristoph Lameter * Buffers are managed in a filesystem specific way. 974418aade4SChristoph Lameter * We must have no buffers or drop them. 975a48d07afSChristoph Lameter */ 976a48d07afSChristoph Lameter if (!page_has_buffers(page) || 977a48d07afSChristoph Lameter try_to_release_page(page, GFP_KERNEL)) { 978a48d07afSChristoph Lameter rc = migrate_page(newpage, page); 979a48d07afSChristoph Lameter goto unlock_both; 980a48d07afSChristoph Lameter } 981a48d07afSChristoph Lameter 982a48d07afSChristoph Lameter /* 983a48d07afSChristoph Lameter * On early passes with mapped pages simply 984a48d07afSChristoph Lameter * retry. There may be a lock held for some 985a48d07afSChristoph Lameter * buffers that may go away. Later 986a48d07afSChristoph Lameter * swap them out. 987a48d07afSChristoph Lameter */ 988a48d07afSChristoph Lameter if (pass > 4) { 989418aade4SChristoph Lameter /* 990418aade4SChristoph Lameter * Persistently unable to drop buffers..... As a 991418aade4SChristoph Lameter * measure of last resort we fall back to 992418aade4SChristoph Lameter * swap_page(). 993418aade4SChristoph Lameter */ 994a48d07afSChristoph Lameter unlock_page(newpage); 995a48d07afSChristoph Lameter newpage = NULL; 996a48d07afSChristoph Lameter rc = swap_page(page); 997a48d07afSChristoph Lameter goto next; 998a48d07afSChristoph Lameter } 999a48d07afSChristoph Lameter 1000a48d07afSChristoph Lameter unlock_both: 1001a48d07afSChristoph Lameter unlock_page(newpage); 1002d0d96328SChristoph Lameter 1003d0d96328SChristoph Lameter unlock_page: 1004d0d96328SChristoph Lameter unlock_page(page); 1005d0d96328SChristoph Lameter 1006d0d96328SChristoph Lameter next: 1007d0d96328SChristoph Lameter if (rc == -EAGAIN) { 100849d2e9ccSChristoph Lameter retry++; 1009d0d96328SChristoph Lameter } else if (rc) { 1010d0d96328SChristoph Lameter /* Permanent failure */ 1011d0d96328SChristoph Lameter list_move(&page->lru, failed); 1012d0d96328SChristoph Lameter nr_failed++; 1013d0d96328SChristoph Lameter } else { 1014a48d07afSChristoph Lameter if (newpage) { 1015a48d07afSChristoph Lameter /* Successful migration. Return page to LRU */ 1016a48d07afSChristoph Lameter move_to_lru(newpage); 1017a48d07afSChristoph Lameter } 1018d0d96328SChristoph Lameter list_move(&page->lru, moved); 1019d0d96328SChristoph Lameter } 102049d2e9ccSChristoph Lameter } 102149d2e9ccSChristoph Lameter if (retry && pass++ < 10) 102249d2e9ccSChristoph Lameter goto redo; 102349d2e9ccSChristoph Lameter 102449d2e9ccSChristoph Lameter if (!swapwrite) 102549d2e9ccSChristoph Lameter current->flags &= ~PF_SWAPWRITE; 102649d2e9ccSChristoph Lameter 102749d2e9ccSChristoph Lameter return nr_failed + retry; 102849d2e9ccSChristoph Lameter } 10298419c318SChristoph Lameter 10308419c318SChristoph Lameter /* 10318419c318SChristoph Lameter * Isolate one page from the LRU lists and put it on the 1032053837fcSNick Piggin * indicated list with elevated refcount. 10338419c318SChristoph Lameter * 10348419c318SChristoph Lameter * Result: 10358419c318SChristoph Lameter * 0 = page not on LRU list 10368419c318SChristoph Lameter * 1 = page removed from LRU list and added to the specified list. 10378419c318SChristoph Lameter */ 10388419c318SChristoph Lameter int isolate_lru_page(struct page *page) 10398419c318SChristoph Lameter { 1040053837fcSNick Piggin int ret = 0; 10418419c318SChristoph Lameter 1042053837fcSNick Piggin if (PageLRU(page)) { 1043053837fcSNick Piggin struct zone *zone = page_zone(page); 10448419c318SChristoph Lameter spin_lock_irq(&zone->lru_lock); 1045053837fcSNick Piggin if (TestClearPageLRU(page)) { 1046053837fcSNick Piggin ret = 1; 1047053837fcSNick Piggin get_page(page); 10488419c318SChristoph Lameter if (PageActive(page)) 10498419c318SChristoph Lameter del_page_from_active_list(zone, page); 10508419c318SChristoph Lameter else 10518419c318SChristoph Lameter del_page_from_inactive_list(zone, page); 10528419c318SChristoph Lameter } 10538419c318SChristoph Lameter spin_unlock_irq(&zone->lru_lock); 10548419c318SChristoph Lameter } 1055053837fcSNick Piggin 1056053837fcSNick Piggin return ret; 10578419c318SChristoph Lameter } 10587cbe34cfSChristoph Lameter #endif 105949d2e9ccSChristoph Lameter 106049d2e9ccSChristoph Lameter /* 10611da177e4SLinus Torvalds * zone->lru_lock is heavily contended. Some of the functions that 10621da177e4SLinus Torvalds * shrink the lists perform better by taking out a batch of pages 10631da177e4SLinus Torvalds * and working on them outside the LRU lock. 10641da177e4SLinus Torvalds * 10651da177e4SLinus Torvalds * For pagecache intensive workloads, this function is the hottest 10661da177e4SLinus Torvalds * spot in the kernel (apart from copy_*_user functions). 10671da177e4SLinus Torvalds * 10681da177e4SLinus Torvalds * Appropriate locks must be held before calling this function. 10691da177e4SLinus Torvalds * 10701da177e4SLinus Torvalds * @nr_to_scan: The number of pages to look through on the list. 10711da177e4SLinus Torvalds * @src: The LRU list to pull pages off. 10721da177e4SLinus Torvalds * @dst: The temp list to put pages on to. 10731da177e4SLinus Torvalds * @scanned: The number of pages that were scanned. 10741da177e4SLinus Torvalds * 10751da177e4SLinus Torvalds * returns how many pages were moved onto *@dst. 10761da177e4SLinus Torvalds */ 10771da177e4SLinus Torvalds static int isolate_lru_pages(int nr_to_scan, struct list_head *src, 10781da177e4SLinus Torvalds struct list_head *dst, int *scanned) 10791da177e4SLinus Torvalds { 10801da177e4SLinus Torvalds int nr_taken = 0; 10811da177e4SLinus Torvalds struct page *page; 10821da177e4SLinus Torvalds int scan = 0; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds while (scan++ < nr_to_scan && !list_empty(src)) { 10851da177e4SLinus Torvalds page = lru_to_page(src); 10861da177e4SLinus Torvalds prefetchw_prev_lru_page(page, src, flags); 10871da177e4SLinus Torvalds 1088053837fcSNick Piggin if (!TestClearPageLRU(page)) 108921eac81fSChristoph Lameter BUG(); 1090053837fcSNick Piggin list_del(&page->lru); 1091053837fcSNick Piggin if (get_page_testone(page)) { 1092053837fcSNick Piggin /* 1093053837fcSNick Piggin * It is being freed elsewhere 1094053837fcSNick Piggin */ 1095053837fcSNick Piggin __put_page(page); 1096053837fcSNick Piggin SetPageLRU(page); 1097053837fcSNick Piggin list_add(&page->lru, src); 1098053837fcSNick Piggin continue; 1099053837fcSNick Piggin } else { 1100053837fcSNick Piggin list_add(&page->lru, dst); 1101053837fcSNick Piggin nr_taken++; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds *scanned = scan; 11061da177e4SLinus Torvalds return nr_taken; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /* 11101da177e4SLinus Torvalds * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed 11111da177e4SLinus Torvalds */ 11121da177e4SLinus Torvalds static void shrink_cache(struct zone *zone, struct scan_control *sc) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds LIST_HEAD(page_list); 11151da177e4SLinus Torvalds struct pagevec pvec; 11161da177e4SLinus Torvalds int max_scan = sc->nr_to_scan; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds pagevec_init(&pvec, 1); 11191da177e4SLinus Torvalds 11201da177e4SLinus Torvalds lru_add_drain(); 11211da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 11221da177e4SLinus Torvalds while (max_scan > 0) { 11231da177e4SLinus Torvalds struct page *page; 11241da177e4SLinus Torvalds int nr_taken; 11251da177e4SLinus Torvalds int nr_scan; 11261da177e4SLinus Torvalds int nr_freed; 11271da177e4SLinus Torvalds 11281da177e4SLinus Torvalds nr_taken = isolate_lru_pages(sc->swap_cluster_max, 11291da177e4SLinus Torvalds &zone->inactive_list, 11301da177e4SLinus Torvalds &page_list, &nr_scan); 11311da177e4SLinus Torvalds zone->nr_inactive -= nr_taken; 11321da177e4SLinus Torvalds zone->pages_scanned += nr_scan; 11331da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 11341da177e4SLinus Torvalds 11351da177e4SLinus Torvalds if (nr_taken == 0) 11361da177e4SLinus Torvalds goto done; 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds max_scan -= nr_scan; 11391da177e4SLinus Torvalds nr_freed = shrink_list(&page_list, sc); 11401da177e4SLinus Torvalds 1141a74609faSNick Piggin local_irq_disable(); 1142a74609faSNick Piggin if (current_is_kswapd()) { 1143a74609faSNick Piggin __mod_page_state_zone(zone, pgscan_kswapd, nr_scan); 1144a74609faSNick Piggin __mod_page_state(kswapd_steal, nr_freed); 1145a74609faSNick Piggin } else 1146a74609faSNick Piggin __mod_page_state_zone(zone, pgscan_direct, nr_scan); 1147a74609faSNick Piggin __mod_page_state_zone(zone, pgsteal, nr_freed); 1148a74609faSNick Piggin 1149a74609faSNick Piggin spin_lock(&zone->lru_lock); 11501da177e4SLinus Torvalds /* 11511da177e4SLinus Torvalds * Put back any unfreeable pages. 11521da177e4SLinus Torvalds */ 11531da177e4SLinus Torvalds while (!list_empty(&page_list)) { 11541da177e4SLinus Torvalds page = lru_to_page(&page_list); 11551da177e4SLinus Torvalds if (TestSetPageLRU(page)) 11561da177e4SLinus Torvalds BUG(); 11571da177e4SLinus Torvalds list_del(&page->lru); 11581da177e4SLinus Torvalds if (PageActive(page)) 11591da177e4SLinus Torvalds add_page_to_active_list(zone, page); 11601da177e4SLinus Torvalds else 11611da177e4SLinus Torvalds add_page_to_inactive_list(zone, page); 11621da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 11631da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 11641da177e4SLinus Torvalds __pagevec_release(&pvec); 11651da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 11701da177e4SLinus Torvalds done: 11711da177e4SLinus Torvalds pagevec_release(&pvec); 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds /* 11751da177e4SLinus Torvalds * This moves pages from the active list to the inactive list. 11761da177e4SLinus Torvalds * 11771da177e4SLinus Torvalds * We move them the other way if the page is referenced by one or more 11781da177e4SLinus Torvalds * processes, from rmap. 11791da177e4SLinus Torvalds * 11801da177e4SLinus Torvalds * If the pages are mostly unmapped, the processing is fast and it is 11811da177e4SLinus Torvalds * appropriate to hold zone->lru_lock across the whole operation. But if 11821da177e4SLinus Torvalds * the pages are mapped, the processing is slow (page_referenced()) so we 11831da177e4SLinus Torvalds * should drop zone->lru_lock around each page. It's impossible to balance 11841da177e4SLinus Torvalds * this, so instead we remove the pages from the LRU while processing them. 11851da177e4SLinus Torvalds * It is safe to rely on PG_active against the non-LRU pages in here because 11861da177e4SLinus Torvalds * nobody will play with that bit on a non-LRU page. 11871da177e4SLinus Torvalds * 11881da177e4SLinus Torvalds * The downside is that we have to touch page->_count against each page. 11891da177e4SLinus Torvalds * But we had to alter page->flags anyway. 11901da177e4SLinus Torvalds */ 11911da177e4SLinus Torvalds static void 11921da177e4SLinus Torvalds refill_inactive_zone(struct zone *zone, struct scan_control *sc) 11931da177e4SLinus Torvalds { 11941da177e4SLinus Torvalds int pgmoved; 11951da177e4SLinus Torvalds int pgdeactivate = 0; 11961da177e4SLinus Torvalds int pgscanned; 11971da177e4SLinus Torvalds int nr_pages = sc->nr_to_scan; 11981da177e4SLinus Torvalds LIST_HEAD(l_hold); /* The pages which were snipped off */ 11991da177e4SLinus Torvalds LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */ 12001da177e4SLinus Torvalds LIST_HEAD(l_active); /* Pages to go onto the active_list */ 12011da177e4SLinus Torvalds struct page *page; 12021da177e4SLinus Torvalds struct pagevec pvec; 12031da177e4SLinus Torvalds int reclaim_mapped = 0; 12042903fb16SChristoph Lameter 12052903fb16SChristoph Lameter if (unlikely(sc->may_swap)) { 12061da177e4SLinus Torvalds long mapped_ratio; 12071da177e4SLinus Torvalds long distress; 12081da177e4SLinus Torvalds long swap_tendency; 12091da177e4SLinus Torvalds 12102903fb16SChristoph Lameter /* 12112903fb16SChristoph Lameter * `distress' is a measure of how much trouble we're having 12122903fb16SChristoph Lameter * reclaiming pages. 0 -> no problems. 100 -> great trouble. 12132903fb16SChristoph Lameter */ 12142903fb16SChristoph Lameter distress = 100 >> zone->prev_priority; 12152903fb16SChristoph Lameter 12162903fb16SChristoph Lameter /* 12172903fb16SChristoph Lameter * The point of this algorithm is to decide when to start 12182903fb16SChristoph Lameter * reclaiming mapped memory instead of just pagecache. Work out 12192903fb16SChristoph Lameter * how much memory 12202903fb16SChristoph Lameter * is mapped. 12212903fb16SChristoph Lameter */ 12222903fb16SChristoph Lameter mapped_ratio = (sc->nr_mapped * 100) / total_memory; 12232903fb16SChristoph Lameter 12242903fb16SChristoph Lameter /* 12252903fb16SChristoph Lameter * Now decide how much we really want to unmap some pages. The 12262903fb16SChristoph Lameter * mapped ratio is downgraded - just because there's a lot of 12272903fb16SChristoph Lameter * mapped memory doesn't necessarily mean that page reclaim 12282903fb16SChristoph Lameter * isn't succeeding. 12292903fb16SChristoph Lameter * 12302903fb16SChristoph Lameter * The distress ratio is important - we don't want to start 12312903fb16SChristoph Lameter * going oom. 12322903fb16SChristoph Lameter * 12332903fb16SChristoph Lameter * A 100% value of vm_swappiness overrides this algorithm 12342903fb16SChristoph Lameter * altogether. 12352903fb16SChristoph Lameter */ 12362903fb16SChristoph Lameter swap_tendency = mapped_ratio / 2 + distress + vm_swappiness; 12372903fb16SChristoph Lameter 12382903fb16SChristoph Lameter /* 12392903fb16SChristoph Lameter * Now use this metric to decide whether to start moving mapped 12402903fb16SChristoph Lameter * memory onto the inactive list. 12412903fb16SChristoph Lameter */ 12422903fb16SChristoph Lameter if (swap_tendency >= 100) 12432903fb16SChristoph Lameter reclaim_mapped = 1; 12442903fb16SChristoph Lameter } 12452903fb16SChristoph Lameter 12461da177e4SLinus Torvalds lru_add_drain(); 12471da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12481da177e4SLinus Torvalds pgmoved = isolate_lru_pages(nr_pages, &zone->active_list, 12491da177e4SLinus Torvalds &l_hold, &pgscanned); 12501da177e4SLinus Torvalds zone->pages_scanned += pgscanned; 12511da177e4SLinus Torvalds zone->nr_active -= pgmoved; 12521da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds while (!list_empty(&l_hold)) { 12551da177e4SLinus Torvalds cond_resched(); 12561da177e4SLinus Torvalds page = lru_to_page(&l_hold); 12571da177e4SLinus Torvalds list_del(&page->lru); 12581da177e4SLinus Torvalds if (page_mapped(page)) { 12591da177e4SLinus Torvalds if (!reclaim_mapped || 12601da177e4SLinus Torvalds (total_swap_pages == 0 && PageAnon(page)) || 1261f7b7fd8fSRik van Riel page_referenced(page, 0)) { 12621da177e4SLinus Torvalds list_add(&page->lru, &l_active); 12631da177e4SLinus Torvalds continue; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds } 12661da177e4SLinus Torvalds list_add(&page->lru, &l_inactive); 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds pagevec_init(&pvec, 1); 12701da177e4SLinus Torvalds pgmoved = 0; 12711da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12721da177e4SLinus Torvalds while (!list_empty(&l_inactive)) { 12731da177e4SLinus Torvalds page = lru_to_page(&l_inactive); 12741da177e4SLinus Torvalds prefetchw_prev_lru_page(page, &l_inactive, flags); 12751da177e4SLinus Torvalds if (TestSetPageLRU(page)) 12761da177e4SLinus Torvalds BUG(); 12771da177e4SLinus Torvalds if (!TestClearPageActive(page)) 12781da177e4SLinus Torvalds BUG(); 12791da177e4SLinus Torvalds list_move(&page->lru, &zone->inactive_list); 12801da177e4SLinus Torvalds pgmoved++; 12811da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 12821da177e4SLinus Torvalds zone->nr_inactive += pgmoved; 12831da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12841da177e4SLinus Torvalds pgdeactivate += pgmoved; 12851da177e4SLinus Torvalds pgmoved = 0; 12861da177e4SLinus Torvalds if (buffer_heads_over_limit) 12871da177e4SLinus Torvalds pagevec_strip(&pvec); 12881da177e4SLinus Torvalds __pagevec_release(&pvec); 12891da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds } 12921da177e4SLinus Torvalds zone->nr_inactive += pgmoved; 12931da177e4SLinus Torvalds pgdeactivate += pgmoved; 12941da177e4SLinus Torvalds if (buffer_heads_over_limit) { 12951da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 12961da177e4SLinus Torvalds pagevec_strip(&pvec); 12971da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds pgmoved = 0; 13011da177e4SLinus Torvalds while (!list_empty(&l_active)) { 13021da177e4SLinus Torvalds page = lru_to_page(&l_active); 13031da177e4SLinus Torvalds prefetchw_prev_lru_page(page, &l_active, flags); 13041da177e4SLinus Torvalds if (TestSetPageLRU(page)) 13051da177e4SLinus Torvalds BUG(); 13061da177e4SLinus Torvalds BUG_ON(!PageActive(page)); 13071da177e4SLinus Torvalds list_move(&page->lru, &zone->active_list); 13081da177e4SLinus Torvalds pgmoved++; 13091da177e4SLinus Torvalds if (!pagevec_add(&pvec, page)) { 13101da177e4SLinus Torvalds zone->nr_active += pgmoved; 13111da177e4SLinus Torvalds pgmoved = 0; 13121da177e4SLinus Torvalds spin_unlock_irq(&zone->lru_lock); 13131da177e4SLinus Torvalds __pagevec_release(&pvec); 13141da177e4SLinus Torvalds spin_lock_irq(&zone->lru_lock); 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds zone->nr_active += pgmoved; 1318a74609faSNick Piggin spin_unlock(&zone->lru_lock); 13191da177e4SLinus Torvalds 1320a74609faSNick Piggin __mod_page_state_zone(zone, pgrefill, pgscanned); 1321a74609faSNick Piggin __mod_page_state(pgdeactivate, pgdeactivate); 1322a74609faSNick Piggin local_irq_enable(); 1323a74609faSNick Piggin 1324a74609faSNick Piggin pagevec_release(&pvec); 13251da177e4SLinus Torvalds } 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds /* 13281da177e4SLinus Torvalds * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. 13291da177e4SLinus Torvalds */ 13301da177e4SLinus Torvalds static void 13311da177e4SLinus Torvalds shrink_zone(struct zone *zone, struct scan_control *sc) 13321da177e4SLinus Torvalds { 13331da177e4SLinus Torvalds unsigned long nr_active; 13341da177e4SLinus Torvalds unsigned long nr_inactive; 13351da177e4SLinus Torvalds 133653e9a615SMartin Hicks atomic_inc(&zone->reclaim_in_progress); 133753e9a615SMartin Hicks 13381da177e4SLinus Torvalds /* 13391da177e4SLinus Torvalds * Add one to `nr_to_scan' just to make sure that the kernel will 13401da177e4SLinus Torvalds * slowly sift through the active list. 13411da177e4SLinus Torvalds */ 13421da177e4SLinus Torvalds zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1; 13431da177e4SLinus Torvalds nr_active = zone->nr_scan_active; 13441da177e4SLinus Torvalds if (nr_active >= sc->swap_cluster_max) 13451da177e4SLinus Torvalds zone->nr_scan_active = 0; 13461da177e4SLinus Torvalds else 13471da177e4SLinus Torvalds nr_active = 0; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1; 13501da177e4SLinus Torvalds nr_inactive = zone->nr_scan_inactive; 13511da177e4SLinus Torvalds if (nr_inactive >= sc->swap_cluster_max) 13521da177e4SLinus Torvalds zone->nr_scan_inactive = 0; 13531da177e4SLinus Torvalds else 13541da177e4SLinus Torvalds nr_inactive = 0; 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds while (nr_active || nr_inactive) { 13571da177e4SLinus Torvalds if (nr_active) { 13581da177e4SLinus Torvalds sc->nr_to_scan = min(nr_active, 13591da177e4SLinus Torvalds (unsigned long)sc->swap_cluster_max); 13601da177e4SLinus Torvalds nr_active -= sc->nr_to_scan; 13611da177e4SLinus Torvalds refill_inactive_zone(zone, sc); 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds if (nr_inactive) { 13651da177e4SLinus Torvalds sc->nr_to_scan = min(nr_inactive, 13661da177e4SLinus Torvalds (unsigned long)sc->swap_cluster_max); 13671da177e4SLinus Torvalds nr_inactive -= sc->nr_to_scan; 13681da177e4SLinus Torvalds shrink_cache(zone, sc); 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds throttle_vm_writeout(); 137353e9a615SMartin Hicks 137453e9a615SMartin Hicks atomic_dec(&zone->reclaim_in_progress); 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 */ 13931da177e4SLinus Torvalds static void 13941da177e4SLinus Torvalds shrink_caches(struct zone **zones, struct scan_control *sc) 13951da177e4SLinus Torvalds { 13961da177e4SLinus Torvalds int i; 13971da177e4SLinus Torvalds 13981da177e4SLinus Torvalds for (i = 0; zones[i] != NULL; i++) { 13991da177e4SLinus Torvalds struct zone *zone = zones[i]; 14001da177e4SLinus Torvalds 1401f3fe6512SCon Kolivas if (!populated_zone(zone)) 14021da177e4SLinus Torvalds continue; 14031da177e4SLinus Torvalds 14049bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 14051da177e4SLinus Torvalds continue; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds zone->temp_priority = sc->priority; 14081da177e4SLinus Torvalds if (zone->prev_priority > sc->priority) 14091da177e4SLinus Torvalds zone->prev_priority = sc->priority; 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY) 14121da177e4SLinus Torvalds continue; /* Let kswapd poll it */ 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds shrink_zone(zone, sc); 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * This is the main entry point to direct page reclaim. 14201da177e4SLinus Torvalds * 14211da177e4SLinus Torvalds * If a full scan of the inactive list fails to free enough memory then we 14221da177e4SLinus Torvalds * are "out of memory" and something needs to be killed. 14231da177e4SLinus Torvalds * 14241da177e4SLinus Torvalds * If the caller is !__GFP_FS then the probability of a failure is reasonably 14251da177e4SLinus Torvalds * high - the zone may be full of dirty or under-writeback pages, which this 14261da177e4SLinus Torvalds * caller can't do much about. We kick pdflush and take explicit naps in the 14271da177e4SLinus Torvalds * hope that some of these pages can be written. But if the allocating task 14281da177e4SLinus Torvalds * holds filesystem locks which prevent writeout this might not work, and the 14291da177e4SLinus Torvalds * allocation attempt will fail. 14301da177e4SLinus Torvalds */ 14316daa0e28SAl Viro int try_to_free_pages(struct zone **zones, gfp_t gfp_mask) 14321da177e4SLinus Torvalds { 14331da177e4SLinus Torvalds int priority; 14341da177e4SLinus Torvalds int ret = 0; 14351da177e4SLinus Torvalds int total_scanned = 0, total_reclaimed = 0; 14361da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 14371da177e4SLinus Torvalds struct scan_control sc; 14381da177e4SLinus Torvalds unsigned long lru_pages = 0; 14391da177e4SLinus Torvalds int i; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds sc.gfp_mask = gfp_mask; 144252a8363eSChristoph Lameter sc.may_writepage = !laptop_mode; 1443f1fd1067SChristoph Lameter sc.may_swap = 1; 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds inc_page_state(allocstall); 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds for (i = 0; zones[i] != NULL; i++) { 14481da177e4SLinus Torvalds struct zone *zone = zones[i]; 14491da177e4SLinus Torvalds 14509bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 14511da177e4SLinus Torvalds continue; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds zone->temp_priority = DEF_PRIORITY; 14541da177e4SLinus Torvalds lru_pages += zone->nr_active + zone->nr_inactive; 14551da177e4SLinus Torvalds } 14561da177e4SLinus Torvalds 14571da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 14581da177e4SLinus Torvalds sc.nr_mapped = read_page_state(nr_mapped); 14591da177e4SLinus Torvalds sc.nr_scanned = 0; 14601da177e4SLinus Torvalds sc.nr_reclaimed = 0; 14611da177e4SLinus Torvalds sc.priority = priority; 14621da177e4SLinus Torvalds sc.swap_cluster_max = SWAP_CLUSTER_MAX; 1463f7b7fd8fSRik van Riel if (!priority) 1464f7b7fd8fSRik van Riel disable_swap_token(); 14651da177e4SLinus Torvalds shrink_caches(zones, &sc); 14661da177e4SLinus Torvalds shrink_slab(sc.nr_scanned, gfp_mask, lru_pages); 14671da177e4SLinus Torvalds if (reclaim_state) { 14681da177e4SLinus Torvalds sc.nr_reclaimed += reclaim_state->reclaimed_slab; 14691da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 14721da177e4SLinus Torvalds total_reclaimed += sc.nr_reclaimed; 14731da177e4SLinus Torvalds if (total_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 */ 14851da177e4SLinus Torvalds if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) { 1486687a21ceSPekka J Enberg wakeup_pdflush(laptop_mode ? 0 : total_scanned); 14871da177e4SLinus Torvalds sc.may_writepage = 1; 14881da177e4SLinus Torvalds } 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds /* Take a nap, wait for some writeback to complete */ 14911da177e4SLinus Torvalds if (sc.nr_scanned && priority < DEF_PRIORITY - 2) 14921da177e4SLinus Torvalds blk_congestion_wait(WRITE, HZ/10); 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds out: 14951da177e4SLinus Torvalds for (i = 0; zones[i] != 0; i++) { 14961da177e4SLinus Torvalds struct zone *zone = zones[i]; 14971da177e4SLinus Torvalds 14989bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 14991da177e4SLinus Torvalds continue; 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds zone->prev_priority = zone->temp_priority; 15021da177e4SLinus Torvalds } 15031da177e4SLinus Torvalds return ret; 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /* 15071da177e4SLinus Torvalds * For kswapd, balance_pgdat() will work across all this node's zones until 15081da177e4SLinus Torvalds * they are all at pages_high. 15091da177e4SLinus Torvalds * 15101da177e4SLinus Torvalds * If `nr_pages' is non-zero then it is the number of pages which are to be 15111da177e4SLinus Torvalds * reclaimed, regardless of the zone occupancies. This is a software suspend 15121da177e4SLinus Torvalds * special. 15131da177e4SLinus Torvalds * 15141da177e4SLinus Torvalds * Returns the number of pages which were actually freed. 15151da177e4SLinus Torvalds * 15161da177e4SLinus Torvalds * There is special handling here for zones which are full of pinned pages. 15171da177e4SLinus Torvalds * This can happen if the pages are all mlocked, or if they are all used by 15181da177e4SLinus Torvalds * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. 15191da177e4SLinus Torvalds * What we do is to detect the case where all pages in the zone have been 15201da177e4SLinus Torvalds * scanned twice and there has been zero successful reclaim. Mark the zone as 15211da177e4SLinus Torvalds * dead and from now on, only perform a short scan. Basically we're polling 15221da177e4SLinus Torvalds * the zone for when the problem goes away. 15231da177e4SLinus Torvalds * 15241da177e4SLinus Torvalds * kswapd scans the zones in the highmem->normal->dma direction. It skips 15251da177e4SLinus Torvalds * zones which have free_pages > pages_high, but once a zone is found to have 15261da177e4SLinus Torvalds * free_pages <= pages_high, we scan that zone and the lower zones regardless 15271da177e4SLinus Torvalds * of the number of free pages in the lower zones. This interoperates with 15281da177e4SLinus Torvalds * the page allocator fallback scheme to ensure that aging of pages is balanced 15291da177e4SLinus Torvalds * across the zones. 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order) 15321da177e4SLinus Torvalds { 15331da177e4SLinus Torvalds int to_free = nr_pages; 15341da177e4SLinus Torvalds int all_zones_ok; 15351da177e4SLinus Torvalds int priority; 15361da177e4SLinus Torvalds int i; 15371da177e4SLinus Torvalds int total_scanned, total_reclaimed; 15381da177e4SLinus Torvalds struct reclaim_state *reclaim_state = current->reclaim_state; 15391da177e4SLinus Torvalds struct scan_control sc; 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds loop_again: 15421da177e4SLinus Torvalds total_scanned = 0; 15431da177e4SLinus Torvalds total_reclaimed = 0; 15441da177e4SLinus Torvalds sc.gfp_mask = GFP_KERNEL; 154552a8363eSChristoph Lameter sc.may_writepage = !laptop_mode; 1546f1fd1067SChristoph Lameter sc.may_swap = 1; 15471da177e4SLinus Torvalds sc.nr_mapped = read_page_state(nr_mapped); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds inc_page_state(pageoutrun); 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds for (i = 0; i < pgdat->nr_zones; i++) { 15521da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds zone->temp_priority = DEF_PRIORITY; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds for (priority = DEF_PRIORITY; priority >= 0; priority--) { 15581da177e4SLinus Torvalds int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ 15591da177e4SLinus Torvalds unsigned long lru_pages = 0; 15601da177e4SLinus Torvalds 1561f7b7fd8fSRik van Riel /* The swap token gets in the way of swapout... */ 1562f7b7fd8fSRik van Riel if (!priority) 1563f7b7fd8fSRik van Riel disable_swap_token(); 1564f7b7fd8fSRik van Riel 15651da177e4SLinus Torvalds all_zones_ok = 1; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (nr_pages == 0) { 15681da177e4SLinus Torvalds /* 15691da177e4SLinus Torvalds * Scan in the highmem->dma direction for the highest 15701da177e4SLinus Torvalds * zone which needs scanning 15711da177e4SLinus Torvalds */ 15721da177e4SLinus Torvalds for (i = pgdat->nr_zones - 1; i >= 0; i--) { 15731da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 15741da177e4SLinus Torvalds 1575f3fe6512SCon Kolivas if (!populated_zone(zone)) 15761da177e4SLinus Torvalds continue; 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds if (zone->all_unreclaimable && 15791da177e4SLinus Torvalds priority != DEF_PRIORITY) 15801da177e4SLinus Torvalds continue; 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds if (!zone_watermark_ok(zone, order, 15837fb1d9fcSRohit Seth zone->pages_high, 0, 0)) { 15841da177e4SLinus Torvalds end_zone = i; 15851da177e4SLinus Torvalds goto scan; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds goto out; 15891da177e4SLinus Torvalds } else { 15901da177e4SLinus Torvalds end_zone = pgdat->nr_zones - 1; 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds scan: 15931da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 15941da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds lru_pages += zone->nr_active + zone->nr_inactive; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds 15991da177e4SLinus Torvalds /* 16001da177e4SLinus Torvalds * Now scan the zone in the dma->highmem direction, stopping 16011da177e4SLinus Torvalds * at the last zone which needs scanning. 16021da177e4SLinus Torvalds * 16031da177e4SLinus Torvalds * We do this because the page allocator works in the opposite 16041da177e4SLinus Torvalds * direction. This prevents the page allocator from allocating 16051da177e4SLinus Torvalds * pages behind kswapd's direction of progress, which would 16061da177e4SLinus Torvalds * cause too much scanning of the lower zones. 16071da177e4SLinus Torvalds */ 16081da177e4SLinus Torvalds for (i = 0; i <= end_zone; i++) { 16091da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 1610b15e0905Sakpm@osdl.org int nr_slab; 16111da177e4SLinus Torvalds 1612f3fe6512SCon Kolivas if (!populated_zone(zone)) 16131da177e4SLinus Torvalds continue; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds if (zone->all_unreclaimable && priority != DEF_PRIORITY) 16161da177e4SLinus Torvalds continue; 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds if (nr_pages == 0) { /* Not software suspend */ 16191da177e4SLinus Torvalds if (!zone_watermark_ok(zone, order, 16207fb1d9fcSRohit Seth zone->pages_high, end_zone, 0)) 16211da177e4SLinus Torvalds all_zones_ok = 0; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds zone->temp_priority = priority; 16241da177e4SLinus Torvalds if (zone->prev_priority > priority) 16251da177e4SLinus Torvalds zone->prev_priority = priority; 16261da177e4SLinus Torvalds sc.nr_scanned = 0; 16271da177e4SLinus Torvalds sc.nr_reclaimed = 0; 16281da177e4SLinus Torvalds sc.priority = priority; 16291da177e4SLinus Torvalds sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX; 16301da177e4SLinus Torvalds shrink_zone(zone, &sc); 16311da177e4SLinus Torvalds reclaim_state->reclaimed_slab = 0; 1632b15e0905Sakpm@osdl.org nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, 1633b15e0905Sakpm@osdl.org lru_pages); 16341da177e4SLinus Torvalds sc.nr_reclaimed += reclaim_state->reclaimed_slab; 16351da177e4SLinus Torvalds total_reclaimed += sc.nr_reclaimed; 16361da177e4SLinus Torvalds total_scanned += sc.nr_scanned; 16371da177e4SLinus Torvalds if (zone->all_unreclaimable) 16381da177e4SLinus Torvalds continue; 1639b15e0905Sakpm@osdl.org if (nr_slab == 0 && zone->pages_scanned >= 1640b15e0905Sakpm@osdl.org (zone->nr_active + zone->nr_inactive) * 4) 16411da177e4SLinus Torvalds zone->all_unreclaimable = 1; 16421da177e4SLinus Torvalds /* 16431da177e4SLinus Torvalds * If we've done a decent amount of scanning and 16441da177e4SLinus Torvalds * the reclaim ratio is low, start doing writepage 16451da177e4SLinus Torvalds * even in laptop mode 16461da177e4SLinus Torvalds */ 16471da177e4SLinus Torvalds if (total_scanned > SWAP_CLUSTER_MAX * 2 && 16481da177e4SLinus Torvalds total_scanned > total_reclaimed+total_reclaimed/2) 16491da177e4SLinus Torvalds sc.may_writepage = 1; 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds if (nr_pages && to_free > total_reclaimed) 16521da177e4SLinus Torvalds continue; /* swsusp: need to do more work */ 16531da177e4SLinus Torvalds if (all_zones_ok) 16541da177e4SLinus Torvalds break; /* kswapd: all done */ 16551da177e4SLinus Torvalds /* 16561da177e4SLinus Torvalds * OK, kswapd is getting into trouble. Take a nap, then take 16571da177e4SLinus Torvalds * another pass across the zones. 16581da177e4SLinus Torvalds */ 16591da177e4SLinus Torvalds if (total_scanned && priority < DEF_PRIORITY - 2) 16601da177e4SLinus Torvalds blk_congestion_wait(WRITE, HZ/10); 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds /* 16631da177e4SLinus Torvalds * We do this so kswapd doesn't build up large priorities for 16641da177e4SLinus Torvalds * example when it is freeing in parallel with allocators. It 16651da177e4SLinus Torvalds * matches the direct reclaim path behaviour in terms of impact 16661da177e4SLinus Torvalds * on zone->*_priority. 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages)) 16691da177e4SLinus Torvalds break; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds out: 16721da177e4SLinus Torvalds for (i = 0; i < pgdat->nr_zones; i++) { 16731da177e4SLinus Torvalds struct zone *zone = pgdat->node_zones + i; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds zone->prev_priority = zone->temp_priority; 16761da177e4SLinus Torvalds } 16771da177e4SLinus Torvalds if (!all_zones_ok) { 16781da177e4SLinus Torvalds cond_resched(); 16791da177e4SLinus Torvalds goto loop_again; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds return total_reclaimed; 16831da177e4SLinus Torvalds } 16841da177e4SLinus Torvalds 16851da177e4SLinus Torvalds /* 16861da177e4SLinus Torvalds * The background pageout daemon, started as a kernel thread 16871da177e4SLinus Torvalds * from the init process. 16881da177e4SLinus Torvalds * 16891da177e4SLinus Torvalds * This basically trickles out pages so that we have _some_ 16901da177e4SLinus Torvalds * free memory available even if there is no other activity 16911da177e4SLinus Torvalds * that frees anything up. This is needed for things like routing 16921da177e4SLinus Torvalds * etc, where we otherwise might have all activity going on in 16931da177e4SLinus Torvalds * asynchronous contexts that cannot page things out. 16941da177e4SLinus Torvalds * 16951da177e4SLinus Torvalds * If there are applications that are active memory-allocators 16961da177e4SLinus Torvalds * (most normal use), this basically shouldn't matter. 16971da177e4SLinus Torvalds */ 16981da177e4SLinus Torvalds static int kswapd(void *p) 16991da177e4SLinus Torvalds { 17001da177e4SLinus Torvalds unsigned long order; 17011da177e4SLinus Torvalds pg_data_t *pgdat = (pg_data_t*)p; 17021da177e4SLinus Torvalds struct task_struct *tsk = current; 17031da177e4SLinus Torvalds DEFINE_WAIT(wait); 17041da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 17051da177e4SLinus Torvalds .reclaimed_slab = 0, 17061da177e4SLinus Torvalds }; 17071da177e4SLinus Torvalds cpumask_t cpumask; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds daemonize("kswapd%d", pgdat->node_id); 17101da177e4SLinus Torvalds cpumask = node_to_cpumask(pgdat->node_id); 17111da177e4SLinus Torvalds if (!cpus_empty(cpumask)) 17121da177e4SLinus Torvalds set_cpus_allowed(tsk, cpumask); 17131da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds /* 17161da177e4SLinus Torvalds * Tell the memory management that we're a "memory allocator", 17171da177e4SLinus Torvalds * and that if we need more memory we should get access to it 17181da177e4SLinus Torvalds * regardless (see "__alloc_pages()"). "kswapd" should 17191da177e4SLinus Torvalds * never get caught in the normal page freeing logic. 17201da177e4SLinus Torvalds * 17211da177e4SLinus Torvalds * (Kswapd normally doesn't need memory anyway, but sometimes 17221da177e4SLinus Torvalds * you need a small amount of memory in order to be able to 17231da177e4SLinus Torvalds * page out something else, and this flag essentially protects 17241da177e4SLinus Torvalds * us from recursively trying to free more memory as we're 17251da177e4SLinus Torvalds * trying to free the first piece of memory in the first place). 17261da177e4SLinus Torvalds */ 1727930d9152SChristoph Lameter tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds order = 0; 17301da177e4SLinus Torvalds for ( ; ; ) { 17311da177e4SLinus Torvalds unsigned long new_order; 17323e1d1d28SChristoph Lameter 17333e1d1d28SChristoph Lameter try_to_freeze(); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); 17361da177e4SLinus Torvalds new_order = pgdat->kswapd_max_order; 17371da177e4SLinus Torvalds pgdat->kswapd_max_order = 0; 17381da177e4SLinus Torvalds if (order < new_order) { 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * Don't sleep if someone wants a larger 'order' 17411da177e4SLinus Torvalds * allocation 17421da177e4SLinus Torvalds */ 17431da177e4SLinus Torvalds order = new_order; 17441da177e4SLinus Torvalds } else { 17451da177e4SLinus Torvalds schedule(); 17461da177e4SLinus Torvalds order = pgdat->kswapd_max_order; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds finish_wait(&pgdat->kswapd_wait, &wait); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds balance_pgdat(pgdat, 0, order); 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds return 0; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds /* 17561da177e4SLinus Torvalds * A zone is low on free memory, so wake its kswapd task to service it. 17571da177e4SLinus Torvalds */ 17581da177e4SLinus Torvalds void wakeup_kswapd(struct zone *zone, int order) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds pg_data_t *pgdat; 17611da177e4SLinus Torvalds 1762f3fe6512SCon Kolivas if (!populated_zone(zone)) 17631da177e4SLinus Torvalds return; 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds pgdat = zone->zone_pgdat; 17667fb1d9fcSRohit Seth if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0)) 17671da177e4SLinus Torvalds return; 17681da177e4SLinus Torvalds if (pgdat->kswapd_max_order < order) 17691da177e4SLinus Torvalds pgdat->kswapd_max_order = order; 17709bf2229fSPaul Jackson if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) 17711da177e4SLinus Torvalds return; 17728d0986e2SCon Kolivas if (!waitqueue_active(&pgdat->kswapd_wait)) 17731da177e4SLinus Torvalds return; 17748d0986e2SCon Kolivas wake_up_interruptible(&pgdat->kswapd_wait); 17751da177e4SLinus Torvalds } 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds #ifdef CONFIG_PM 17781da177e4SLinus Torvalds /* 17791da177e4SLinus Torvalds * Try to free `nr_pages' of memory, system-wide. Returns the number of freed 17801da177e4SLinus Torvalds * pages. 17811da177e4SLinus Torvalds */ 17821da177e4SLinus Torvalds int shrink_all_memory(int nr_pages) 17831da177e4SLinus Torvalds { 17841da177e4SLinus Torvalds pg_data_t *pgdat; 17851da177e4SLinus Torvalds int nr_to_free = nr_pages; 17861da177e4SLinus Torvalds int ret = 0; 17871da177e4SLinus Torvalds struct reclaim_state reclaim_state = { 17881da177e4SLinus Torvalds .reclaimed_slab = 0, 17891da177e4SLinus Torvalds }; 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds current->reclaim_state = &reclaim_state; 17921da177e4SLinus Torvalds for_each_pgdat(pgdat) { 17931da177e4SLinus Torvalds int freed; 17941da177e4SLinus Torvalds freed = balance_pgdat(pgdat, nr_to_free, 0); 17951da177e4SLinus Torvalds ret += freed; 17961da177e4SLinus Torvalds nr_to_free -= freed; 17971da177e4SLinus Torvalds if (nr_to_free <= 0) 17981da177e4SLinus Torvalds break; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds current->reclaim_state = NULL; 18011da177e4SLinus Torvalds return ret; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds #endif 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 18061da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but 18071da177e4SLinus Torvalds not required for correctness. So if the last cpu in a node goes 18081da177e4SLinus Torvalds away, we get changed to run anywhere: as the first one comes back, 18091da177e4SLinus Torvalds restore their cpu bindings. */ 18101da177e4SLinus Torvalds static int __devinit cpu_callback(struct notifier_block *nfb, 18111da177e4SLinus Torvalds unsigned long action, 18121da177e4SLinus Torvalds void *hcpu) 18131da177e4SLinus Torvalds { 18141da177e4SLinus Torvalds pg_data_t *pgdat; 18151da177e4SLinus Torvalds cpumask_t mask; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds if (action == CPU_ONLINE) { 18181da177e4SLinus Torvalds for_each_pgdat(pgdat) { 18191da177e4SLinus Torvalds mask = node_to_cpumask(pgdat->node_id); 18201da177e4SLinus Torvalds if (any_online_cpu(mask) != NR_CPUS) 18211da177e4SLinus Torvalds /* One of our CPUs online: restore mask */ 18221da177e4SLinus Torvalds set_cpus_allowed(pgdat->kswapd, mask); 18231da177e4SLinus Torvalds } 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds return NOTIFY_OK; 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */ 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds static int __init kswapd_init(void) 18301da177e4SLinus Torvalds { 18311da177e4SLinus Torvalds pg_data_t *pgdat; 18321da177e4SLinus Torvalds swap_setup(); 18331da177e4SLinus Torvalds for_each_pgdat(pgdat) 18341da177e4SLinus Torvalds pgdat->kswapd 18351da177e4SLinus Torvalds = find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL)); 18361da177e4SLinus Torvalds total_memory = nr_free_pagecache_pages(); 18371da177e4SLinus Torvalds hotcpu_notifier(cpu_callback, 0); 18381da177e4SLinus Torvalds return 0; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds module_init(kswapd_init) 18429eeff239SChristoph Lameter 18439eeff239SChristoph Lameter #ifdef CONFIG_NUMA 18449eeff239SChristoph Lameter /* 18459eeff239SChristoph Lameter * Zone reclaim mode 18469eeff239SChristoph Lameter * 18479eeff239SChristoph Lameter * If non-zero call zone_reclaim when the number of free pages falls below 18489eeff239SChristoph Lameter * the watermarks. 18499eeff239SChristoph Lameter * 18509eeff239SChristoph Lameter * In the future we may add flags to the mode. However, the page allocator 18519eeff239SChristoph Lameter * should only have to check that zone_reclaim_mode != 0 before calling 18529eeff239SChristoph Lameter * zone_reclaim(). 18539eeff239SChristoph Lameter */ 18549eeff239SChristoph Lameter int zone_reclaim_mode __read_mostly; 18559eeff239SChristoph Lameter 18561b2ffb78SChristoph Lameter #define RECLAIM_OFF 0 18571b2ffb78SChristoph Lameter #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */ 18581b2ffb78SChristoph Lameter #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ 18591b2ffb78SChristoph Lameter #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ 18602a16e3f4SChristoph Lameter #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */ 18611b2ffb78SChristoph Lameter 18629eeff239SChristoph Lameter /* 18639eeff239SChristoph Lameter * Mininum time between zone reclaim scans 18649eeff239SChristoph Lameter */ 18652a11ff06SChristoph Lameter int zone_reclaim_interval __read_mostly = 30*HZ; 1866a92f7126SChristoph Lameter 1867a92f7126SChristoph Lameter /* 1868a92f7126SChristoph Lameter * Priority for ZONE_RECLAIM. This determines the fraction of pages 1869a92f7126SChristoph Lameter * of a node considered for each zone_reclaim. 4 scans 1/16th of 1870a92f7126SChristoph Lameter * a zone. 1871a92f7126SChristoph Lameter */ 1872a92f7126SChristoph Lameter #define ZONE_RECLAIM_PRIORITY 4 1873a92f7126SChristoph Lameter 18749eeff239SChristoph Lameter /* 18759eeff239SChristoph Lameter * Try to free up some pages from this zone through reclaim. 18769eeff239SChristoph Lameter */ 18779eeff239SChristoph Lameter int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) 18789eeff239SChristoph Lameter { 187989288623SChristoph Lameter int nr_pages; 18809eeff239SChristoph Lameter struct task_struct *p = current; 18819eeff239SChristoph Lameter struct reclaim_state reclaim_state; 188289288623SChristoph Lameter struct scan_control sc; 188342c722d4SChristoph Lameter cpumask_t mask; 188442c722d4SChristoph Lameter int node_id; 188589288623SChristoph Lameter 188689288623SChristoph Lameter if (time_before(jiffies, 18872a11ff06SChristoph Lameter zone->last_unsuccessful_zone_reclaim + zone_reclaim_interval)) 188889288623SChristoph Lameter return 0; 18899eeff239SChristoph Lameter 18909eeff239SChristoph Lameter if (!(gfp_mask & __GFP_WAIT) || 18919eeff239SChristoph Lameter zone->all_unreclaimable || 1892a6bf5270SChristoph Lameter atomic_read(&zone->reclaim_in_progress) > 0 || 1893a6bf5270SChristoph Lameter (p->flags & PF_MEMALLOC)) 18949eeff239SChristoph Lameter return 0; 18959eeff239SChristoph Lameter 189642c722d4SChristoph Lameter node_id = zone->zone_pgdat->node_id; 189742c722d4SChristoph Lameter mask = node_to_cpumask(node_id); 189842c722d4SChristoph Lameter if (!cpus_empty(mask) && node_id != numa_node_id()) 189942c722d4SChristoph Lameter return 0; 190042c722d4SChristoph Lameter 19011b2ffb78SChristoph Lameter sc.may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE); 19021b2ffb78SChristoph Lameter sc.may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP); 190389288623SChristoph Lameter sc.nr_scanned = 0; 190489288623SChristoph Lameter sc.nr_reclaimed = 0; 1905a92f7126SChristoph Lameter sc.priority = ZONE_RECLAIM_PRIORITY + 1; 190689288623SChristoph Lameter sc.nr_mapped = read_page_state(nr_mapped); 190789288623SChristoph Lameter sc.gfp_mask = gfp_mask; 19089eeff239SChristoph Lameter 19099eeff239SChristoph Lameter disable_swap_token(); 19109eeff239SChristoph Lameter 191189288623SChristoph Lameter nr_pages = 1 << order; 19129eeff239SChristoph Lameter if (nr_pages > SWAP_CLUSTER_MAX) 19139eeff239SChristoph Lameter sc.swap_cluster_max = nr_pages; 19149eeff239SChristoph Lameter else 19159eeff239SChristoph Lameter sc.swap_cluster_max = SWAP_CLUSTER_MAX; 19169eeff239SChristoph Lameter 19179eeff239SChristoph Lameter cond_resched(); 1918d4f7796eSChristoph Lameter /* 1919d4f7796eSChristoph Lameter * We need to be able to allocate from the reserves for RECLAIM_SWAP 1920d4f7796eSChristoph Lameter * and we also need to be able to write out pages for RECLAIM_WRITE 1921d4f7796eSChristoph Lameter * and RECLAIM_SWAP. 1922d4f7796eSChristoph Lameter */ 1923d4f7796eSChristoph Lameter p->flags |= PF_MEMALLOC | PF_SWAPWRITE; 19249eeff239SChristoph Lameter reclaim_state.reclaimed_slab = 0; 19259eeff239SChristoph Lameter p->reclaim_state = &reclaim_state; 1926c84db23cSChristoph Lameter 1927a92f7126SChristoph Lameter /* 1928a92f7126SChristoph Lameter * Free memory by calling shrink zone with increasing priorities 1929a92f7126SChristoph Lameter * until we have enough memory freed. 1930a92f7126SChristoph Lameter */ 1931a92f7126SChristoph Lameter do { 1932a92f7126SChristoph Lameter sc.priority--; 19339eeff239SChristoph Lameter shrink_zone(zone, &sc); 1934c84db23cSChristoph Lameter 1935a92f7126SChristoph Lameter } while (sc.nr_reclaimed < nr_pages && sc.priority > 0); 1936a92f7126SChristoph Lameter 19372a16e3f4SChristoph Lameter if (sc.nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) { 19382a16e3f4SChristoph Lameter /* 19392a16e3f4SChristoph Lameter * shrink_slab does not currently allow us to determine 19402a16e3f4SChristoph Lameter * how many pages were freed in the zone. So we just 19412a16e3f4SChristoph Lameter * shake the slab and then go offnode for a single allocation. 19422a16e3f4SChristoph Lameter * 19432a16e3f4SChristoph Lameter * shrink_slab will free memory on all zones and may take 19442a16e3f4SChristoph Lameter * a long time. 19452a16e3f4SChristoph Lameter */ 19462a16e3f4SChristoph Lameter shrink_slab(sc.nr_scanned, gfp_mask, order); 19472a16e3f4SChristoph Lameter } 19482a16e3f4SChristoph Lameter 19499eeff239SChristoph Lameter p->reclaim_state = NULL; 1950d4f7796eSChristoph Lameter current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); 19519eeff239SChristoph Lameter 19529eeff239SChristoph Lameter if (sc.nr_reclaimed == 0) 19539eeff239SChristoph Lameter zone->last_unsuccessful_zone_reclaim = jiffies; 19549eeff239SChristoph Lameter 1955c84db23cSChristoph Lameter return sc.nr_reclaimed >= nr_pages; 19569eeff239SChristoph Lameter } 19579eeff239SChristoph Lameter #endif 19589eeff239SChristoph Lameter 1959