xref: /openbmc/linux/mm/vmscan.c (revision ee27497df36823f2793212cad0997c044eb0e1eb)
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 
741da177e4SLinus Torvalds 	/* This context's SWAP_CLUSTER_MAX. If freeing memory for
751da177e4SLinus Torvalds 	 * suspend, we effectively ignore SWAP_CLUSTER_MAX.
761da177e4SLinus Torvalds 	 * In this context, it doesn't matter that we scan the
771da177e4SLinus Torvalds 	 * whole list at once. */
781da177e4SLinus Torvalds 	int swap_cluster_max;
791da177e4SLinus Torvalds };
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds /*
821da177e4SLinus Torvalds  * The list of shrinker callbacks used by to apply pressure to
831da177e4SLinus Torvalds  * ageable caches.
841da177e4SLinus Torvalds  */
851da177e4SLinus Torvalds struct shrinker {
861da177e4SLinus Torvalds 	shrinker_t		shrinker;
871da177e4SLinus Torvalds 	struct list_head	list;
881da177e4SLinus Torvalds 	int			seeks;	/* seeks to recreate an obj */
891da177e4SLinus Torvalds 	long			nr;	/* objs pending delete */
901da177e4SLinus Torvalds };
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru))
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCH
951da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field)			\
961da177e4SLinus Torvalds 	do {								\
971da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
981da177e4SLinus Torvalds 			struct page *prev;				\
991da177e4SLinus Torvalds 									\
1001da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1011da177e4SLinus Torvalds 			prefetch(&prev->_field);			\
1021da177e4SLinus Torvalds 		}							\
1031da177e4SLinus Torvalds 	} while (0)
1041da177e4SLinus Torvalds #else
1051da177e4SLinus Torvalds #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #ifdef ARCH_HAS_PREFETCHW
1091da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field)			\
1101da177e4SLinus Torvalds 	do {								\
1111da177e4SLinus Torvalds 		if ((_page)->lru.prev != _base) {			\
1121da177e4SLinus Torvalds 			struct page *prev;				\
1131da177e4SLinus Torvalds 									\
1141da177e4SLinus Torvalds 			prev = lru_to_page(&(_page->lru));		\
1151da177e4SLinus Torvalds 			prefetchw(&prev->_field);			\
1161da177e4SLinus Torvalds 		}							\
1171da177e4SLinus Torvalds 	} while (0)
1181da177e4SLinus Torvalds #else
1191da177e4SLinus Torvalds #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
1201da177e4SLinus Torvalds #endif
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * From 0 .. 100.  Higher means more swappy.
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds int vm_swappiness = 60;
1261da177e4SLinus Torvalds static long total_memory;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds static LIST_HEAD(shrinker_list);
1291da177e4SLinus Torvalds static DECLARE_RWSEM(shrinker_rwsem);
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /*
1321da177e4SLinus Torvalds  * Add a shrinker callback to be called from the vm
1331da177e4SLinus Torvalds  */
1341da177e4SLinus Torvalds struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds         struct shrinker *shrinker;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds         shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL);
1391da177e4SLinus Torvalds         if (shrinker) {
1401da177e4SLinus Torvalds 	        shrinker->shrinker = theshrinker;
1411da177e4SLinus Torvalds 	        shrinker->seeks = seeks;
1421da177e4SLinus Torvalds 	        shrinker->nr = 0;
1431da177e4SLinus Torvalds 	        down_write(&shrinker_rwsem);
1441da177e4SLinus Torvalds 	        list_add_tail(&shrinker->list, &shrinker_list);
1451da177e4SLinus Torvalds 	        up_write(&shrinker_rwsem);
1461da177e4SLinus Torvalds 	}
1471da177e4SLinus Torvalds 	return shrinker;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds EXPORT_SYMBOL(set_shrinker);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /*
1521da177e4SLinus Torvalds  * Remove one
1531da177e4SLinus Torvalds  */
1541da177e4SLinus Torvalds void remove_shrinker(struct shrinker *shrinker)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	down_write(&shrinker_rwsem);
1571da177e4SLinus Torvalds 	list_del(&shrinker->list);
1581da177e4SLinus Torvalds 	up_write(&shrinker_rwsem);
1591da177e4SLinus Torvalds 	kfree(shrinker);
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds EXPORT_SYMBOL(remove_shrinker);
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds #define SHRINK_BATCH 128
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * Call the shrink functions to age shrinkable caches
1661da177e4SLinus Torvalds  *
1671da177e4SLinus Torvalds  * Here we assume it costs one seek to replace a lru page and that it also
1681da177e4SLinus Torvalds  * takes a seek to recreate a cache object.  With this in mind we age equal
1691da177e4SLinus Torvalds  * percentages of the lru and ageable caches.  This should balance the seeks
1701da177e4SLinus Torvalds  * generated by these structures.
1711da177e4SLinus Torvalds  *
1721da177e4SLinus Torvalds  * If the vm encounted mapped pages on the LRU it increase the pressure on
1731da177e4SLinus Torvalds  * slab to avoid swapping.
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits.
1761da177e4SLinus Torvalds  *
1771da177e4SLinus Torvalds  * `lru_pages' represents the number of on-LRU pages in all the zones which
1781da177e4SLinus Torvalds  * are eligible for the caller's allocation attempt.  It is used for balancing
1791da177e4SLinus Torvalds  * slab reclaim versus page reclaim.
180b15e0905Sakpm@osdl.org  *
181b15e0905Sakpm@osdl.org  * Returns the number of slab objects which we shrunk.
1821da177e4SLinus Torvalds  */
1839d0243bcSAndrew Morton int shrink_slab(unsigned long scanned, gfp_t gfp_mask, unsigned long lru_pages)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	struct shrinker *shrinker;
186b15e0905Sakpm@osdl.org 	int ret = 0;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	if (scanned == 0)
1891da177e4SLinus Torvalds 		scanned = SWAP_CLUSTER_MAX;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	if (!down_read_trylock(&shrinker_rwsem))
192b15e0905Sakpm@osdl.org 		return 1;	/* Assume we'll be able to shrink next time */
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	list_for_each_entry(shrinker, &shrinker_list, list) {
1951da177e4SLinus Torvalds 		unsigned long long delta;
1961da177e4SLinus Torvalds 		unsigned long total_scan;
197ea164d73SAndrea Arcangeli 		unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 		delta = (4 * scanned) / shrinker->seeks;
200ea164d73SAndrea Arcangeli 		delta *= max_pass;
2011da177e4SLinus Torvalds 		do_div(delta, lru_pages + 1);
2021da177e4SLinus Torvalds 		shrinker->nr += delta;
203ea164d73SAndrea Arcangeli 		if (shrinker->nr < 0) {
204ea164d73SAndrea Arcangeli 			printk(KERN_ERR "%s: nr=%ld\n",
205ea164d73SAndrea Arcangeli 					__FUNCTION__, shrinker->nr);
206ea164d73SAndrea Arcangeli 			shrinker->nr = max_pass;
207ea164d73SAndrea Arcangeli 		}
208ea164d73SAndrea Arcangeli 
209ea164d73SAndrea Arcangeli 		/*
210ea164d73SAndrea Arcangeli 		 * Avoid risking looping forever due to too large nr value:
211ea164d73SAndrea Arcangeli 		 * never try to free more than twice the estimate number of
212ea164d73SAndrea Arcangeli 		 * freeable entries.
213ea164d73SAndrea Arcangeli 		 */
214ea164d73SAndrea Arcangeli 		if (shrinker->nr > max_pass * 2)
215ea164d73SAndrea Arcangeli 			shrinker->nr = max_pass * 2;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 		total_scan = shrinker->nr;
2181da177e4SLinus Torvalds 		shrinker->nr = 0;
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 		while (total_scan >= SHRINK_BATCH) {
2211da177e4SLinus Torvalds 			long this_scan = SHRINK_BATCH;
2221da177e4SLinus Torvalds 			int shrink_ret;
223b15e0905Sakpm@osdl.org 			int nr_before;
2241da177e4SLinus Torvalds 
225b15e0905Sakpm@osdl.org 			nr_before = (*shrinker->shrinker)(0, gfp_mask);
2261da177e4SLinus Torvalds 			shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask);
2271da177e4SLinus Torvalds 			if (shrink_ret == -1)
2281da177e4SLinus Torvalds 				break;
229b15e0905Sakpm@osdl.org 			if (shrink_ret < nr_before)
230b15e0905Sakpm@osdl.org 				ret += nr_before - shrink_ret;
2311da177e4SLinus Torvalds 			mod_page_state(slabs_scanned, this_scan);
2321da177e4SLinus Torvalds 			total_scan -= this_scan;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 			cond_resched();
2351da177e4SLinus Torvalds 		}
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 		shrinker->nr += total_scan;
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 	up_read(&shrinker_rwsem);
240b15e0905Sakpm@osdl.org 	return ret;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /* Called without lock on whether page is mapped, so answer is unstable */
2441da177e4SLinus Torvalds static inline int page_mapping_inuse(struct page *page)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct address_space *mapping;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* Page is in somebody's page tables. */
2491da177e4SLinus Torvalds 	if (page_mapped(page))
2501da177e4SLinus Torvalds 		return 1;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* Be more reluctant to reclaim swapcache than pagecache */
2531da177e4SLinus Torvalds 	if (PageSwapCache(page))
2541da177e4SLinus Torvalds 		return 1;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	mapping = page_mapping(page);
2571da177e4SLinus Torvalds 	if (!mapping)
2581da177e4SLinus Torvalds 		return 0;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	/* File is mmap'd by somebody? */
2611da177e4SLinus Torvalds 	return mapping_mapped(mapping);
2621da177e4SLinus Torvalds }
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds static inline int is_page_cache_freeable(struct page *page)
2651da177e4SLinus Torvalds {
2661da177e4SLinus Torvalds 	return page_count(page) - !!PagePrivate(page) == 2;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static int may_write_to_queue(struct backing_dev_info *bdi)
2701da177e4SLinus Torvalds {
271930d9152SChristoph Lameter 	if (current->flags & PF_SWAPWRITE)
2721da177e4SLinus Torvalds 		return 1;
2731da177e4SLinus Torvalds 	if (!bdi_write_congested(bdi))
2741da177e4SLinus Torvalds 		return 1;
2751da177e4SLinus Torvalds 	if (bdi == current->backing_dev_info)
2761da177e4SLinus Torvalds 		return 1;
2771da177e4SLinus Torvalds 	return 0;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds /*
2811da177e4SLinus Torvalds  * We detected a synchronous write error writing a page out.  Probably
2821da177e4SLinus Torvalds  * -ENOSPC.  We need to propagate that into the address_space for a subsequent
2831da177e4SLinus Torvalds  * fsync(), msync() or close().
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * The tricky part is that after writepage we cannot touch the mapping: nothing
2861da177e4SLinus Torvalds  * prevents it from being freed up.  But we have a ref on the page and once
2871da177e4SLinus Torvalds  * that page is locked, the mapping is pinned.
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  * We're allowed to run sleeping lock_page() here because we know the caller has
2901da177e4SLinus Torvalds  * __GFP_FS.
2911da177e4SLinus Torvalds  */
2921da177e4SLinus Torvalds static void handle_write_error(struct address_space *mapping,
2931da177e4SLinus Torvalds 				struct page *page, int error)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	lock_page(page);
2961da177e4SLinus Torvalds 	if (page_mapping(page) == mapping) {
2971da177e4SLinus Torvalds 		if (error == -ENOSPC)
2981da177e4SLinus Torvalds 			set_bit(AS_ENOSPC, &mapping->flags);
2991da177e4SLinus Torvalds 		else
3001da177e4SLinus Torvalds 			set_bit(AS_EIO, &mapping->flags);
3011da177e4SLinus Torvalds 	}
3021da177e4SLinus Torvalds 	unlock_page(page);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds /*
3061da177e4SLinus Torvalds  * pageout is called by shrink_list() for each dirty page. Calls ->writepage().
3071da177e4SLinus Torvalds  */
3081da177e4SLinus Torvalds static pageout_t pageout(struct page *page, struct address_space *mapping)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	/*
3111da177e4SLinus Torvalds 	 * If the page is dirty, only perform writeback if that write
3121da177e4SLinus Torvalds 	 * will be non-blocking.  To prevent this allocation from being
3131da177e4SLinus Torvalds 	 * stalled by pagecache activity.  But note that there may be
3141da177e4SLinus Torvalds 	 * stalls if we need to run get_block().  We could test
3151da177e4SLinus Torvalds 	 * PagePrivate for that.
3161da177e4SLinus Torvalds 	 *
3171da177e4SLinus Torvalds 	 * If this process is currently in generic_file_write() against
3181da177e4SLinus Torvalds 	 * this page's queue, we can perform writeback even if that
3191da177e4SLinus Torvalds 	 * will block.
3201da177e4SLinus Torvalds 	 *
3211da177e4SLinus Torvalds 	 * If the page is swapcache, write it back even if that would
3221da177e4SLinus Torvalds 	 * block, for some throttling. This happens by accident, because
3231da177e4SLinus Torvalds 	 * swap_backing_dev_info is bust: it doesn't reflect the
3241da177e4SLinus Torvalds 	 * congestion state of the swapdevs.  Easy to fix, if needed.
3251da177e4SLinus Torvalds 	 * See swapfile.c:page_queue_congested().
3261da177e4SLinus Torvalds 	 */
3271da177e4SLinus Torvalds 	if (!is_page_cache_freeable(page))
3281da177e4SLinus Torvalds 		return PAGE_KEEP;
3291da177e4SLinus Torvalds 	if (!mapping) {
3301da177e4SLinus Torvalds 		/*
3311da177e4SLinus Torvalds 		 * Some data journaling orphaned pages can have
3321da177e4SLinus Torvalds 		 * page->mapping == NULL while being dirty with clean buffers.
3331da177e4SLinus Torvalds 		 */
334323aca6cSakpm@osdl.org 		if (PagePrivate(page)) {
3351da177e4SLinus Torvalds 			if (try_to_free_buffers(page)) {
3361da177e4SLinus Torvalds 				ClearPageDirty(page);
3371da177e4SLinus Torvalds 				printk("%s: orphaned page\n", __FUNCTION__);
3381da177e4SLinus Torvalds 				return PAGE_CLEAN;
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 		}
3411da177e4SLinus Torvalds 		return PAGE_KEEP;
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 	if (mapping->a_ops->writepage == NULL)
3441da177e4SLinus Torvalds 		return PAGE_ACTIVATE;
3451da177e4SLinus Torvalds 	if (!may_write_to_queue(mapping->backing_dev_info))
3461da177e4SLinus Torvalds 		return PAGE_KEEP;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (clear_page_dirty_for_io(page)) {
3491da177e4SLinus Torvalds 		int res;
3501da177e4SLinus Torvalds 		struct writeback_control wbc = {
3511da177e4SLinus Torvalds 			.sync_mode = WB_SYNC_NONE,
3521da177e4SLinus Torvalds 			.nr_to_write = SWAP_CLUSTER_MAX,
3531da177e4SLinus Torvalds 			.nonblocking = 1,
3541da177e4SLinus Torvalds 			.for_reclaim = 1,
3551da177e4SLinus Torvalds 		};
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 		SetPageReclaim(page);
3581da177e4SLinus Torvalds 		res = mapping->a_ops->writepage(page, &wbc);
3591da177e4SLinus Torvalds 		if (res < 0)
3601da177e4SLinus Torvalds 			handle_write_error(mapping, page, res);
361994fc28cSZach Brown 		if (res == AOP_WRITEPAGE_ACTIVATE) {
3621da177e4SLinus Torvalds 			ClearPageReclaim(page);
3631da177e4SLinus Torvalds 			return PAGE_ACTIVATE;
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 		if (!PageWriteback(page)) {
3661da177e4SLinus Torvalds 			/* synchronous write or broken a_ops? */
3671da177e4SLinus Torvalds 			ClearPageReclaim(page);
3681da177e4SLinus Torvalds 		}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		return PAGE_SUCCESS;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	return PAGE_CLEAN;
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
37649d2e9ccSChristoph Lameter static int remove_mapping(struct address_space *mapping, struct page *page)
37749d2e9ccSChristoph Lameter {
37849d2e9ccSChristoph Lameter 	if (!mapping)
37949d2e9ccSChristoph Lameter 		return 0;		/* truncate got there first */
38049d2e9ccSChristoph Lameter 
38149d2e9ccSChristoph Lameter 	write_lock_irq(&mapping->tree_lock);
38249d2e9ccSChristoph Lameter 
38349d2e9ccSChristoph Lameter 	/*
38449d2e9ccSChristoph Lameter 	 * The non-racy check for busy page.  It is critical to check
38549d2e9ccSChristoph Lameter 	 * PageDirty _after_ making sure that the page is freeable and
38649d2e9ccSChristoph Lameter 	 * not in use by anybody. 	(pagecache + us == 2)
38749d2e9ccSChristoph Lameter 	 */
38849d2e9ccSChristoph Lameter 	if (unlikely(page_count(page) != 2))
38949d2e9ccSChristoph Lameter 		goto cannot_free;
39049d2e9ccSChristoph Lameter 	smp_rmb();
39149d2e9ccSChristoph Lameter 	if (unlikely(PageDirty(page)))
39249d2e9ccSChristoph Lameter 		goto cannot_free;
39349d2e9ccSChristoph Lameter 
39449d2e9ccSChristoph Lameter 	if (PageSwapCache(page)) {
39549d2e9ccSChristoph Lameter 		swp_entry_t swap = { .val = page_private(page) };
39649d2e9ccSChristoph Lameter 		__delete_from_swap_cache(page);
39749d2e9ccSChristoph Lameter 		write_unlock_irq(&mapping->tree_lock);
39849d2e9ccSChristoph Lameter 		swap_free(swap);
39949d2e9ccSChristoph Lameter 		__put_page(page);	/* The pagecache ref */
40049d2e9ccSChristoph Lameter 		return 1;
40149d2e9ccSChristoph Lameter 	}
40249d2e9ccSChristoph Lameter 
40349d2e9ccSChristoph Lameter 	__remove_from_page_cache(page);
40449d2e9ccSChristoph Lameter 	write_unlock_irq(&mapping->tree_lock);
40549d2e9ccSChristoph Lameter 	__put_page(page);
40649d2e9ccSChristoph Lameter 	return 1;
40749d2e9ccSChristoph Lameter 
40849d2e9ccSChristoph Lameter cannot_free:
40949d2e9ccSChristoph Lameter 	write_unlock_irq(&mapping->tree_lock);
41049d2e9ccSChristoph Lameter 	return 0;
41149d2e9ccSChristoph Lameter }
41249d2e9ccSChristoph Lameter 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  * shrink_list adds the number of reclaimed pages to sc->nr_reclaimed
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds static int shrink_list(struct list_head *page_list, struct scan_control *sc)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	LIST_HEAD(ret_pages);
4191da177e4SLinus Torvalds 	struct pagevec freed_pvec;
4201da177e4SLinus Torvalds 	int pgactivate = 0;
4211da177e4SLinus Torvalds 	int reclaimed = 0;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	cond_resched();
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	pagevec_init(&freed_pvec, 1);
4261da177e4SLinus Torvalds 	while (!list_empty(page_list)) {
4271da177e4SLinus Torvalds 		struct address_space *mapping;
4281da177e4SLinus Torvalds 		struct page *page;
4291da177e4SLinus Torvalds 		int may_enter_fs;
4301da177e4SLinus Torvalds 		int referenced;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 		cond_resched();
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 		page = lru_to_page(page_list);
4351da177e4SLinus Torvalds 		list_del(&page->lru);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 		if (TestSetPageLocked(page))
4381da177e4SLinus Torvalds 			goto keep;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 		BUG_ON(PageActive(page));
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds 		sc->nr_scanned++;
4431da177e4SLinus Torvalds 		/* Double the slab pressure for mapped and swapcache pages */
4441da177e4SLinus Torvalds 		if (page_mapped(page) || PageSwapCache(page))
4451da177e4SLinus Torvalds 			sc->nr_scanned++;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 		if (PageWriteback(page))
4481da177e4SLinus Torvalds 			goto keep_locked;
4491da177e4SLinus Torvalds 
450f7b7fd8fSRik van Riel 		referenced = page_referenced(page, 1);
4511da177e4SLinus Torvalds 		/* In active use or really unfreeable?  Activate it. */
4521da177e4SLinus Torvalds 		if (referenced && page_mapping_inuse(page))
4531da177e4SLinus Torvalds 			goto activate_locked;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds #ifdef CONFIG_SWAP
4561da177e4SLinus Torvalds 		/*
4571da177e4SLinus Torvalds 		 * Anonymous process memory has backing store?
4581da177e4SLinus Torvalds 		 * Try to allocate it some swap space here.
4591da177e4SLinus Torvalds 		 */
460c340010eSLee Schermerhorn 		if (PageAnon(page) && !PageSwapCache(page)) {
4611480a540SChristoph Lameter 			if (!add_to_swap(page, GFP_ATOMIC))
4621da177e4SLinus Torvalds 				goto activate_locked;
4631da177e4SLinus Torvalds 		}
4641da177e4SLinus Torvalds #endif /* CONFIG_SWAP */
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		mapping = page_mapping(page);
4671da177e4SLinus Torvalds 		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
4681da177e4SLinus Torvalds 			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 		/*
4711da177e4SLinus Torvalds 		 * The page is mapped into the page tables of one or more
4721da177e4SLinus Torvalds 		 * processes. Try to unmap it here.
4731da177e4SLinus Torvalds 		 */
4741da177e4SLinus Torvalds 		if (page_mapped(page) && mapping) {
4751da177e4SLinus Torvalds 			switch (try_to_unmap(page)) {
4761da177e4SLinus Torvalds 			case SWAP_FAIL:
4771da177e4SLinus Torvalds 				goto activate_locked;
4781da177e4SLinus Torvalds 			case SWAP_AGAIN:
4791da177e4SLinus Torvalds 				goto keep_locked;
4801da177e4SLinus Torvalds 			case SWAP_SUCCESS:
4811da177e4SLinus Torvalds 				; /* try to free the page below */
4821da177e4SLinus Torvalds 			}
4831da177e4SLinus Torvalds 		}
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 		if (PageDirty(page)) {
4861da177e4SLinus Torvalds 			if (referenced)
4871da177e4SLinus Torvalds 				goto keep_locked;
4881da177e4SLinus Torvalds 			if (!may_enter_fs)
4891da177e4SLinus Torvalds 				goto keep_locked;
4901da177e4SLinus Torvalds 			if (laptop_mode && !sc->may_writepage)
4911da177e4SLinus Torvalds 				goto keep_locked;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 			/* Page is dirty, try to write it out here */
4941da177e4SLinus Torvalds 			switch(pageout(page, mapping)) {
4951da177e4SLinus Torvalds 			case PAGE_KEEP:
4961da177e4SLinus Torvalds 				goto keep_locked;
4971da177e4SLinus Torvalds 			case PAGE_ACTIVATE:
4981da177e4SLinus Torvalds 				goto activate_locked;
4991da177e4SLinus Torvalds 			case PAGE_SUCCESS:
5001da177e4SLinus Torvalds 				if (PageWriteback(page) || PageDirty(page))
5011da177e4SLinus Torvalds 					goto keep;
5021da177e4SLinus Torvalds 				/*
5031da177e4SLinus Torvalds 				 * A synchronous write - probably a ramdisk.  Go
5041da177e4SLinus Torvalds 				 * ahead and try to reclaim the page.
5051da177e4SLinus Torvalds 				 */
5061da177e4SLinus Torvalds 				if (TestSetPageLocked(page))
5071da177e4SLinus Torvalds 					goto keep;
5081da177e4SLinus Torvalds 				if (PageDirty(page) || PageWriteback(page))
5091da177e4SLinus Torvalds 					goto keep_locked;
5101da177e4SLinus Torvalds 				mapping = page_mapping(page);
5111da177e4SLinus Torvalds 			case PAGE_CLEAN:
5121da177e4SLinus Torvalds 				; /* try to free the page below */
5131da177e4SLinus Torvalds 			}
5141da177e4SLinus Torvalds 		}
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 		/*
5171da177e4SLinus Torvalds 		 * If the page has buffers, try to free the buffer mappings
5181da177e4SLinus Torvalds 		 * associated with this page. If we succeed we try to free
5191da177e4SLinus Torvalds 		 * the page as well.
5201da177e4SLinus Torvalds 		 *
5211da177e4SLinus Torvalds 		 * We do this even if the page is PageDirty().
5221da177e4SLinus Torvalds 		 * try_to_release_page() does not perform I/O, but it is
5231da177e4SLinus Torvalds 		 * possible for a page to have PageDirty set, but it is actually
5241da177e4SLinus Torvalds 		 * clean (all its buffers are clean).  This happens if the
5251da177e4SLinus Torvalds 		 * buffers were written out directly, with submit_bh(). ext3
5261da177e4SLinus Torvalds 		 * will do this, as well as the blockdev mapping.
5271da177e4SLinus Torvalds 		 * try_to_release_page() will discover that cleanness and will
5281da177e4SLinus Torvalds 		 * drop the buffers and mark the page clean - it can be freed.
5291da177e4SLinus Torvalds 		 *
5301da177e4SLinus Torvalds 		 * Rarely, pages can have buffers and no ->mapping.  These are
5311da177e4SLinus Torvalds 		 * the pages which were not successfully invalidated in
5321da177e4SLinus Torvalds 		 * truncate_complete_page().  We try to drop those buffers here
5331da177e4SLinus Torvalds 		 * and if that worked, and the page is no longer mapped into
5341da177e4SLinus Torvalds 		 * process address space (page_count == 1) it can be freed.
5351da177e4SLinus Torvalds 		 * Otherwise, leave the page on the LRU so it is swappable.
5361da177e4SLinus Torvalds 		 */
5371da177e4SLinus Torvalds 		if (PagePrivate(page)) {
5381da177e4SLinus Torvalds 			if (!try_to_release_page(page, sc->gfp_mask))
5391da177e4SLinus Torvalds 				goto activate_locked;
5401da177e4SLinus Torvalds 			if (!mapping && page_count(page) == 1)
5411da177e4SLinus Torvalds 				goto free_it;
5421da177e4SLinus Torvalds 		}
5431da177e4SLinus Torvalds 
54449d2e9ccSChristoph Lameter 		if (!remove_mapping(mapping, page))
54549d2e9ccSChristoph Lameter 			goto keep_locked;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds free_it:
5481da177e4SLinus Torvalds 		unlock_page(page);
5491da177e4SLinus Torvalds 		reclaimed++;
5501da177e4SLinus Torvalds 		if (!pagevec_add(&freed_pvec, page))
5511da177e4SLinus Torvalds 			__pagevec_release_nonlru(&freed_pvec);
5521da177e4SLinus Torvalds 		continue;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds activate_locked:
5551da177e4SLinus Torvalds 		SetPageActive(page);
5561da177e4SLinus Torvalds 		pgactivate++;
5571da177e4SLinus Torvalds keep_locked:
5581da177e4SLinus Torvalds 		unlock_page(page);
5591da177e4SLinus Torvalds keep:
5601da177e4SLinus Torvalds 		list_add(&page->lru, &ret_pages);
5611da177e4SLinus Torvalds 		BUG_ON(PageLRU(page));
5621da177e4SLinus Torvalds 	}
5631da177e4SLinus Torvalds 	list_splice(&ret_pages, page_list);
5641da177e4SLinus Torvalds 	if (pagevec_count(&freed_pvec))
5651da177e4SLinus Torvalds 		__pagevec_release_nonlru(&freed_pvec);
5661da177e4SLinus Torvalds 	mod_page_state(pgactivate, pgactivate);
5671da177e4SLinus Torvalds 	sc->nr_reclaimed += reclaimed;
5681da177e4SLinus Torvalds 	return reclaimed;
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
5717cbe34cfSChristoph Lameter #ifdef CONFIG_MIGRATION
5728419c318SChristoph Lameter static inline void move_to_lru(struct page *page)
5738419c318SChristoph Lameter {
5748419c318SChristoph Lameter 	list_del(&page->lru);
5758419c318SChristoph Lameter 	if (PageActive(page)) {
5768419c318SChristoph Lameter 		/*
5778419c318SChristoph Lameter 		 * lru_cache_add_active checks that
5788419c318SChristoph Lameter 		 * the PG_active bit is off.
5798419c318SChristoph Lameter 		 */
5808419c318SChristoph Lameter 		ClearPageActive(page);
5818419c318SChristoph Lameter 		lru_cache_add_active(page);
5828419c318SChristoph Lameter 	} else {
5838419c318SChristoph Lameter 		lru_cache_add(page);
5848419c318SChristoph Lameter 	}
5858419c318SChristoph Lameter 	put_page(page);
5868419c318SChristoph Lameter }
5878419c318SChristoph Lameter 
5888419c318SChristoph Lameter /*
5898419c318SChristoph Lameter  * Add isolated pages on the list back to the LRU
5908419c318SChristoph Lameter  *
5918419c318SChristoph Lameter  * returns the number of pages put back.
5928419c318SChristoph Lameter  */
5938419c318SChristoph Lameter int putback_lru_pages(struct list_head *l)
5948419c318SChristoph Lameter {
5958419c318SChristoph Lameter 	struct page *page;
5968419c318SChristoph Lameter 	struct page *page2;
5978419c318SChristoph Lameter 	int count = 0;
5988419c318SChristoph Lameter 
5998419c318SChristoph Lameter 	list_for_each_entry_safe(page, page2, l, lru) {
6008419c318SChristoph Lameter 		move_to_lru(page);
6018419c318SChristoph Lameter 		count++;
6028419c318SChristoph Lameter 	}
6038419c318SChristoph Lameter 	return count;
6048419c318SChristoph Lameter }
6058419c318SChristoph Lameter 
6061da177e4SLinus Torvalds /*
60749d2e9ccSChristoph Lameter  * swapout a single page
60849d2e9ccSChristoph Lameter  * page is locked upon entry, unlocked on exit
60949d2e9ccSChristoph Lameter  *
61049d2e9ccSChristoph Lameter  * return codes:
61149d2e9ccSChristoph Lameter  *	0 = complete
61249d2e9ccSChristoph Lameter  *	1 = retry
61349d2e9ccSChristoph Lameter  */
61449d2e9ccSChristoph Lameter static int swap_page(struct page *page)
61549d2e9ccSChristoph Lameter {
61649d2e9ccSChristoph Lameter 	struct address_space *mapping = page_mapping(page);
61749d2e9ccSChristoph Lameter 
61849d2e9ccSChristoph Lameter 	if (page_mapped(page) && mapping)
61949d2e9ccSChristoph Lameter 		if (try_to_unmap(page) != SWAP_SUCCESS)
62049d2e9ccSChristoph Lameter 			goto unlock_retry;
62149d2e9ccSChristoph Lameter 
62249d2e9ccSChristoph Lameter 	if (PageDirty(page)) {
62349d2e9ccSChristoph Lameter 		/* Page is dirty, try to write it out here */
62449d2e9ccSChristoph Lameter 		switch(pageout(page, mapping)) {
62549d2e9ccSChristoph Lameter 		case PAGE_KEEP:
62649d2e9ccSChristoph Lameter 		case PAGE_ACTIVATE:
62749d2e9ccSChristoph Lameter 			goto unlock_retry;
62849d2e9ccSChristoph Lameter 
62949d2e9ccSChristoph Lameter 		case PAGE_SUCCESS:
63049d2e9ccSChristoph Lameter 			goto retry;
63149d2e9ccSChristoph Lameter 
63249d2e9ccSChristoph Lameter 		case PAGE_CLEAN:
63349d2e9ccSChristoph Lameter 			; /* try to free the page below */
63449d2e9ccSChristoph Lameter 		}
63549d2e9ccSChristoph Lameter 	}
63649d2e9ccSChristoph Lameter 
63749d2e9ccSChristoph Lameter 	if (PagePrivate(page)) {
63849d2e9ccSChristoph Lameter 		if (!try_to_release_page(page, GFP_KERNEL) ||
63949d2e9ccSChristoph Lameter 		    (!mapping && page_count(page) == 1))
64049d2e9ccSChristoph Lameter 			goto unlock_retry;
64149d2e9ccSChristoph Lameter 	}
64249d2e9ccSChristoph Lameter 
64349d2e9ccSChristoph Lameter 	if (remove_mapping(mapping, page)) {
64449d2e9ccSChristoph Lameter 		/* Success */
64549d2e9ccSChristoph Lameter 		unlock_page(page);
64649d2e9ccSChristoph Lameter 		return 0;
64749d2e9ccSChristoph Lameter 	}
64849d2e9ccSChristoph Lameter 
64949d2e9ccSChristoph Lameter unlock_retry:
65049d2e9ccSChristoph Lameter 	unlock_page(page);
65149d2e9ccSChristoph Lameter 
65249d2e9ccSChristoph Lameter retry:
65349d2e9ccSChristoph Lameter 	return 1;
65449d2e9ccSChristoph Lameter }
65549d2e9ccSChristoph Lameter /*
65649d2e9ccSChristoph Lameter  * migrate_pages
65749d2e9ccSChristoph Lameter  *
65849d2e9ccSChristoph Lameter  * Two lists are passed to this function. The first list
65949d2e9ccSChristoph Lameter  * contains the pages isolated from the LRU to be migrated.
66049d2e9ccSChristoph Lameter  * The second list contains new pages that the pages isolated
66149d2e9ccSChristoph Lameter  * can be moved to. If the second list is NULL then all
66249d2e9ccSChristoph Lameter  * pages are swapped out.
66349d2e9ccSChristoph Lameter  *
66449d2e9ccSChristoph Lameter  * The function returns after 10 attempts or if no pages
66549d2e9ccSChristoph Lameter  * are movable anymore because t has become empty
66649d2e9ccSChristoph Lameter  * or no retryable pages exist anymore.
66749d2e9ccSChristoph Lameter  *
66849d2e9ccSChristoph Lameter  * SIMPLIFIED VERSION: This implementation of migrate_pages
66949d2e9ccSChristoph Lameter  * is only swapping out pages and never touches the second
67049d2e9ccSChristoph Lameter  * list. The direct migration patchset
67149d2e9ccSChristoph Lameter  * extends this function to avoid the use of swap.
67249d2e9ccSChristoph Lameter  */
67349d2e9ccSChristoph Lameter int migrate_pages(struct list_head *l, struct list_head *t)
67449d2e9ccSChristoph Lameter {
67549d2e9ccSChristoph Lameter 	int retry;
67649d2e9ccSChristoph Lameter 	LIST_HEAD(failed);
67749d2e9ccSChristoph Lameter 	int nr_failed = 0;
67849d2e9ccSChristoph Lameter 	int pass = 0;
67949d2e9ccSChristoph Lameter 	struct page *page;
68049d2e9ccSChristoph Lameter 	struct page *page2;
68149d2e9ccSChristoph Lameter 	int swapwrite = current->flags & PF_SWAPWRITE;
68249d2e9ccSChristoph Lameter 
68349d2e9ccSChristoph Lameter 	if (!swapwrite)
68449d2e9ccSChristoph Lameter 		current->flags |= PF_SWAPWRITE;
68549d2e9ccSChristoph Lameter 
68649d2e9ccSChristoph Lameter redo:
68749d2e9ccSChristoph Lameter 	retry = 0;
68849d2e9ccSChristoph Lameter 
68949d2e9ccSChristoph Lameter 	list_for_each_entry_safe(page, page2, l, lru) {
69049d2e9ccSChristoph Lameter 		cond_resched();
69149d2e9ccSChristoph Lameter 
692*ee27497dSChristoph Lameter 		if (page_count(page) == 1) {
693*ee27497dSChristoph Lameter 			/* page was freed from under us. So we are done. */
694*ee27497dSChristoph Lameter 			move_to_lru(page);
695*ee27497dSChristoph Lameter 			continue;
696*ee27497dSChristoph Lameter 		}
69749d2e9ccSChristoph Lameter 		/*
69849d2e9ccSChristoph Lameter 		 * Skip locked pages during the first two passes to give the
6997cbe34cfSChristoph Lameter 		 * functions holding the lock time to release the page. Later we
7007cbe34cfSChristoph Lameter 		 * use lock_page() to have a higher chance of acquiring the
7017cbe34cfSChristoph Lameter 		 * lock.
70249d2e9ccSChristoph Lameter 		 */
70349d2e9ccSChristoph Lameter 		if (pass > 2)
70449d2e9ccSChristoph Lameter 			lock_page(page);
70549d2e9ccSChristoph Lameter 		else
70649d2e9ccSChristoph Lameter 			if (TestSetPageLocked(page))
70749d2e9ccSChristoph Lameter 				goto retry_later;
70849d2e9ccSChristoph Lameter 
70949d2e9ccSChristoph Lameter 		/*
71049d2e9ccSChristoph Lameter 		 * Only wait on writeback if we have already done a pass where
71149d2e9ccSChristoph Lameter 		 * we we may have triggered writeouts for lots of pages.
71249d2e9ccSChristoph Lameter 		 */
7137cbe34cfSChristoph Lameter 		if (pass > 0) {
71449d2e9ccSChristoph Lameter 			wait_on_page_writeback(page);
7157cbe34cfSChristoph Lameter 		} else {
71649d2e9ccSChristoph Lameter 			if (PageWriteback(page)) {
71749d2e9ccSChristoph Lameter 				unlock_page(page);
71849d2e9ccSChristoph Lameter 				goto retry_later;
71949d2e9ccSChristoph Lameter 			}
7207cbe34cfSChristoph Lameter 		}
72149d2e9ccSChristoph Lameter 
72249d2e9ccSChristoph Lameter 		if (PageAnon(page) && !PageSwapCache(page)) {
7231480a540SChristoph Lameter 			if (!add_to_swap(page, GFP_KERNEL)) {
72449d2e9ccSChristoph Lameter 				unlock_page(page);
72549d2e9ccSChristoph Lameter 				list_move(&page->lru, &failed);
72649d2e9ccSChristoph Lameter 				nr_failed++;
72749d2e9ccSChristoph Lameter 				continue;
72849d2e9ccSChristoph Lameter 			}
72949d2e9ccSChristoph Lameter 		}
73049d2e9ccSChristoph Lameter 
73149d2e9ccSChristoph Lameter 		/*
73249d2e9ccSChristoph Lameter 		 * Page is properly locked and writeback is complete.
73349d2e9ccSChristoph Lameter 		 * Try to migrate the page.
73449d2e9ccSChristoph Lameter 		 */
7357cbe34cfSChristoph Lameter 		if (!swap_page(page))
7367cbe34cfSChristoph Lameter 			continue;
73749d2e9ccSChristoph Lameter retry_later:
73849d2e9ccSChristoph Lameter 		retry++;
73949d2e9ccSChristoph Lameter 	}
74049d2e9ccSChristoph Lameter 	if (retry && pass++ < 10)
74149d2e9ccSChristoph Lameter 		goto redo;
74249d2e9ccSChristoph Lameter 
74349d2e9ccSChristoph Lameter 	if (!swapwrite)
74449d2e9ccSChristoph Lameter 		current->flags &= ~PF_SWAPWRITE;
74549d2e9ccSChristoph Lameter 
74649d2e9ccSChristoph Lameter 	if (!list_empty(&failed))
74749d2e9ccSChristoph Lameter 		list_splice(&failed, l);
74849d2e9ccSChristoph Lameter 
74949d2e9ccSChristoph Lameter 	return nr_failed + retry;
75049d2e9ccSChristoph Lameter }
7518419c318SChristoph Lameter 
7528419c318SChristoph Lameter static void lru_add_drain_per_cpu(void *dummy)
7538419c318SChristoph Lameter {
7548419c318SChristoph Lameter 	lru_add_drain();
7558419c318SChristoph Lameter }
7568419c318SChristoph Lameter 
7578419c318SChristoph Lameter /*
7588419c318SChristoph Lameter  * Isolate one page from the LRU lists and put it on the
7598419c318SChristoph Lameter  * indicated list. Do necessary cache draining if the
7608419c318SChristoph Lameter  * page is not on the LRU lists yet.
7618419c318SChristoph Lameter  *
7628419c318SChristoph Lameter  * Result:
7638419c318SChristoph Lameter  *  0 = page not on LRU list
7648419c318SChristoph Lameter  *  1 = page removed from LRU list and added to the specified list.
7658419c318SChristoph Lameter  * -ENOENT = page is being freed elsewhere.
7668419c318SChristoph Lameter  */
7678419c318SChristoph Lameter int isolate_lru_page(struct page *page)
7688419c318SChristoph Lameter {
7698419c318SChristoph Lameter 	int rc = 0;
7708419c318SChristoph Lameter 	struct zone *zone = page_zone(page);
7718419c318SChristoph Lameter 
7728419c318SChristoph Lameter redo:
7738419c318SChristoph Lameter 	spin_lock_irq(&zone->lru_lock);
7748419c318SChristoph Lameter 	rc = __isolate_lru_page(page);
7758419c318SChristoph Lameter 	if (rc == 1) {
7768419c318SChristoph Lameter 		if (PageActive(page))
7778419c318SChristoph Lameter 			del_page_from_active_list(zone, page);
7788419c318SChristoph Lameter 		else
7798419c318SChristoph Lameter 			del_page_from_inactive_list(zone, page);
7808419c318SChristoph Lameter 	}
7818419c318SChristoph Lameter 	spin_unlock_irq(&zone->lru_lock);
7828419c318SChristoph Lameter 	if (rc == 0) {
7838419c318SChristoph Lameter 		/*
7848419c318SChristoph Lameter 		 * Maybe this page is still waiting for a cpu to drain it
7858419c318SChristoph Lameter 		 * from one of the lru lists?
7868419c318SChristoph Lameter 		 */
7878419c318SChristoph Lameter 		rc = schedule_on_each_cpu(lru_add_drain_per_cpu, NULL);
7888419c318SChristoph Lameter 		if (rc == 0 && PageLRU(page))
7898419c318SChristoph Lameter 			goto redo;
7908419c318SChristoph Lameter 	}
7918419c318SChristoph Lameter 	return rc;
7928419c318SChristoph Lameter }
7937cbe34cfSChristoph Lameter #endif
79449d2e9ccSChristoph Lameter 
79549d2e9ccSChristoph Lameter /*
7961da177e4SLinus Torvalds  * zone->lru_lock is heavily contended.  Some of the functions that
7971da177e4SLinus Torvalds  * shrink the lists perform better by taking out a batch of pages
7981da177e4SLinus Torvalds  * and working on them outside the LRU lock.
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * For pagecache intensive workloads, this function is the hottest
8011da177e4SLinus Torvalds  * spot in the kernel (apart from copy_*_user functions).
8021da177e4SLinus Torvalds  *
8031da177e4SLinus Torvalds  * Appropriate locks must be held before calling this function.
8041da177e4SLinus Torvalds  *
8051da177e4SLinus Torvalds  * @nr_to_scan:	The number of pages to look through on the list.
8061da177e4SLinus Torvalds  * @src:	The LRU list to pull pages off.
8071da177e4SLinus Torvalds  * @dst:	The temp list to put pages on to.
8081da177e4SLinus Torvalds  * @scanned:	The number of pages that were scanned.
8091da177e4SLinus Torvalds  *
8101da177e4SLinus Torvalds  * returns how many pages were moved onto *@dst.
8111da177e4SLinus Torvalds  */
8121da177e4SLinus Torvalds static int isolate_lru_pages(int nr_to_scan, struct list_head *src,
8131da177e4SLinus Torvalds 			     struct list_head *dst, int *scanned)
8141da177e4SLinus Torvalds {
8151da177e4SLinus Torvalds 	int nr_taken = 0;
8161da177e4SLinus Torvalds 	struct page *page;
8171da177e4SLinus Torvalds 	int scan = 0;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	while (scan++ < nr_to_scan && !list_empty(src)) {
8201da177e4SLinus Torvalds 		page = lru_to_page(src);
8211da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, src, flags);
8221da177e4SLinus Torvalds 
82321eac81fSChristoph Lameter 		switch (__isolate_lru_page(page)) {
82421eac81fSChristoph Lameter 		case 1:
82521eac81fSChristoph Lameter 			/* Succeeded to isolate page */
82621eac81fSChristoph Lameter 			list_move(&page->lru, dst);
8271da177e4SLinus Torvalds 			nr_taken++;
82821eac81fSChristoph Lameter 			break;
82921eac81fSChristoph Lameter 		case -ENOENT:
83021eac81fSChristoph Lameter 			/* Not possible to isolate */
83121eac81fSChristoph Lameter 			list_move(&page->lru, src);
83221eac81fSChristoph Lameter 			break;
83321eac81fSChristoph Lameter 		default:
83421eac81fSChristoph Lameter 			BUG();
8351da177e4SLinus Torvalds 		}
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	*scanned = scan;
8391da177e4SLinus Torvalds 	return nr_taken;
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds /*
8431da177e4SLinus Torvalds  * shrink_cache() adds the number of pages reclaimed to sc->nr_reclaimed
8441da177e4SLinus Torvalds  */
8451da177e4SLinus Torvalds static void shrink_cache(struct zone *zone, struct scan_control *sc)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds 	LIST_HEAD(page_list);
8481da177e4SLinus Torvalds 	struct pagevec pvec;
8491da177e4SLinus Torvalds 	int max_scan = sc->nr_to_scan;
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds 	pagevec_init(&pvec, 1);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	lru_add_drain();
8541da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
8551da177e4SLinus Torvalds 	while (max_scan > 0) {
8561da177e4SLinus Torvalds 		struct page *page;
8571da177e4SLinus Torvalds 		int nr_taken;
8581da177e4SLinus Torvalds 		int nr_scan;
8591da177e4SLinus Torvalds 		int nr_freed;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 		nr_taken = isolate_lru_pages(sc->swap_cluster_max,
8621da177e4SLinus Torvalds 					     &zone->inactive_list,
8631da177e4SLinus Torvalds 					     &page_list, &nr_scan);
8641da177e4SLinus Torvalds 		zone->nr_inactive -= nr_taken;
8651da177e4SLinus Torvalds 		zone->pages_scanned += nr_scan;
8661da177e4SLinus Torvalds 		spin_unlock_irq(&zone->lru_lock);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 		if (nr_taken == 0)
8691da177e4SLinus Torvalds 			goto done;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 		max_scan -= nr_scan;
8721da177e4SLinus Torvalds 		nr_freed = shrink_list(&page_list, sc);
8731da177e4SLinus Torvalds 
874a74609faSNick Piggin 		local_irq_disable();
875a74609faSNick Piggin 		if (current_is_kswapd()) {
876a74609faSNick Piggin 			__mod_page_state_zone(zone, pgscan_kswapd, nr_scan);
877a74609faSNick Piggin 			__mod_page_state(kswapd_steal, nr_freed);
878a74609faSNick Piggin 		} else
879a74609faSNick Piggin 			__mod_page_state_zone(zone, pgscan_direct, nr_scan);
880a74609faSNick Piggin 		__mod_page_state_zone(zone, pgsteal, nr_freed);
881a74609faSNick Piggin 
882a74609faSNick Piggin 		spin_lock(&zone->lru_lock);
8831da177e4SLinus Torvalds 		/*
8841da177e4SLinus Torvalds 		 * Put back any unfreeable pages.
8851da177e4SLinus Torvalds 		 */
8861da177e4SLinus Torvalds 		while (!list_empty(&page_list)) {
8871da177e4SLinus Torvalds 			page = lru_to_page(&page_list);
8881da177e4SLinus Torvalds 			if (TestSetPageLRU(page))
8891da177e4SLinus Torvalds 				BUG();
8901da177e4SLinus Torvalds 			list_del(&page->lru);
8911da177e4SLinus Torvalds 			if (PageActive(page))
8921da177e4SLinus Torvalds 				add_page_to_active_list(zone, page);
8931da177e4SLinus Torvalds 			else
8941da177e4SLinus Torvalds 				add_page_to_inactive_list(zone, page);
8951da177e4SLinus Torvalds 			if (!pagevec_add(&pvec, page)) {
8961da177e4SLinus Torvalds 				spin_unlock_irq(&zone->lru_lock);
8971da177e4SLinus Torvalds 				__pagevec_release(&pvec);
8981da177e4SLinus Torvalds 				spin_lock_irq(&zone->lru_lock);
8991da177e4SLinus Torvalds 			}
9001da177e4SLinus Torvalds 		}
9011da177e4SLinus Torvalds   	}
9021da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
9031da177e4SLinus Torvalds done:
9041da177e4SLinus Torvalds 	pagevec_release(&pvec);
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds /*
9081da177e4SLinus Torvalds  * This moves pages from the active list to the inactive list.
9091da177e4SLinus Torvalds  *
9101da177e4SLinus Torvalds  * We move them the other way if the page is referenced by one or more
9111da177e4SLinus Torvalds  * processes, from rmap.
9121da177e4SLinus Torvalds  *
9131da177e4SLinus Torvalds  * If the pages are mostly unmapped, the processing is fast and it is
9141da177e4SLinus Torvalds  * appropriate to hold zone->lru_lock across the whole operation.  But if
9151da177e4SLinus Torvalds  * the pages are mapped, the processing is slow (page_referenced()) so we
9161da177e4SLinus Torvalds  * should drop zone->lru_lock around each page.  It's impossible to balance
9171da177e4SLinus Torvalds  * this, so instead we remove the pages from the LRU while processing them.
9181da177e4SLinus Torvalds  * It is safe to rely on PG_active against the non-LRU pages in here because
9191da177e4SLinus Torvalds  * nobody will play with that bit on a non-LRU page.
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  * The downside is that we have to touch page->_count against each page.
9221da177e4SLinus Torvalds  * But we had to alter page->flags anyway.
9231da177e4SLinus Torvalds  */
9241da177e4SLinus Torvalds static void
9251da177e4SLinus Torvalds refill_inactive_zone(struct zone *zone, struct scan_control *sc)
9261da177e4SLinus Torvalds {
9271da177e4SLinus Torvalds 	int pgmoved;
9281da177e4SLinus Torvalds 	int pgdeactivate = 0;
9291da177e4SLinus Torvalds 	int pgscanned;
9301da177e4SLinus Torvalds 	int nr_pages = sc->nr_to_scan;
9311da177e4SLinus Torvalds 	LIST_HEAD(l_hold);	/* The pages which were snipped off */
9321da177e4SLinus Torvalds 	LIST_HEAD(l_inactive);	/* Pages to go onto the inactive_list */
9331da177e4SLinus Torvalds 	LIST_HEAD(l_active);	/* Pages to go onto the active_list */
9341da177e4SLinus Torvalds 	struct page *page;
9351da177e4SLinus Torvalds 	struct pagevec pvec;
9361da177e4SLinus Torvalds 	int reclaim_mapped = 0;
9371da177e4SLinus Torvalds 	long mapped_ratio;
9381da177e4SLinus Torvalds 	long distress;
9391da177e4SLinus Torvalds 	long swap_tendency;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	lru_add_drain();
9421da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
9431da177e4SLinus Torvalds 	pgmoved = isolate_lru_pages(nr_pages, &zone->active_list,
9441da177e4SLinus Torvalds 				    &l_hold, &pgscanned);
9451da177e4SLinus Torvalds 	zone->pages_scanned += pgscanned;
9461da177e4SLinus Torvalds 	zone->nr_active -= pgmoved;
9471da177e4SLinus Torvalds 	spin_unlock_irq(&zone->lru_lock);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	/*
9501da177e4SLinus Torvalds 	 * `distress' is a measure of how much trouble we're having reclaiming
9511da177e4SLinus Torvalds 	 * pages.  0 -> no problems.  100 -> great trouble.
9521da177e4SLinus Torvalds 	 */
9531da177e4SLinus Torvalds 	distress = 100 >> zone->prev_priority;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/*
9561da177e4SLinus Torvalds 	 * The point of this algorithm is to decide when to start reclaiming
9571da177e4SLinus Torvalds 	 * mapped memory instead of just pagecache.  Work out how much memory
9581da177e4SLinus Torvalds 	 * is mapped.
9591da177e4SLinus Torvalds 	 */
9601da177e4SLinus Torvalds 	mapped_ratio = (sc->nr_mapped * 100) / total_memory;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds 	/*
9631da177e4SLinus Torvalds 	 * Now decide how much we really want to unmap some pages.  The mapped
9641da177e4SLinus Torvalds 	 * ratio is downgraded - just because there's a lot of mapped memory
9651da177e4SLinus Torvalds 	 * doesn't necessarily mean that page reclaim isn't succeeding.
9661da177e4SLinus Torvalds 	 *
9671da177e4SLinus Torvalds 	 * The distress ratio is important - we don't want to start going oom.
9681da177e4SLinus Torvalds 	 *
9691da177e4SLinus Torvalds 	 * A 100% value of vm_swappiness overrides this algorithm altogether.
9701da177e4SLinus Torvalds 	 */
9711da177e4SLinus Torvalds 	swap_tendency = mapped_ratio / 2 + distress + vm_swappiness;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	/*
9741da177e4SLinus Torvalds 	 * Now use this metric to decide whether to start moving mapped memory
9751da177e4SLinus Torvalds 	 * onto the inactive list.
9761da177e4SLinus Torvalds 	 */
9771da177e4SLinus Torvalds 	if (swap_tendency >= 100)
9781da177e4SLinus Torvalds 		reclaim_mapped = 1;
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	while (!list_empty(&l_hold)) {
9811da177e4SLinus Torvalds 		cond_resched();
9821da177e4SLinus Torvalds 		page = lru_to_page(&l_hold);
9831da177e4SLinus Torvalds 		list_del(&page->lru);
9841da177e4SLinus Torvalds 		if (page_mapped(page)) {
9851da177e4SLinus Torvalds 			if (!reclaim_mapped ||
9861da177e4SLinus Torvalds 			    (total_swap_pages == 0 && PageAnon(page)) ||
987f7b7fd8fSRik van Riel 			    page_referenced(page, 0)) {
9881da177e4SLinus Torvalds 				list_add(&page->lru, &l_active);
9891da177e4SLinus Torvalds 				continue;
9901da177e4SLinus Torvalds 			}
9911da177e4SLinus Torvalds 		}
9921da177e4SLinus Torvalds 		list_add(&page->lru, &l_inactive);
9931da177e4SLinus Torvalds 	}
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds 	pagevec_init(&pvec, 1);
9961da177e4SLinus Torvalds 	pgmoved = 0;
9971da177e4SLinus Torvalds 	spin_lock_irq(&zone->lru_lock);
9981da177e4SLinus Torvalds 	while (!list_empty(&l_inactive)) {
9991da177e4SLinus Torvalds 		page = lru_to_page(&l_inactive);
10001da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, &l_inactive, flags);
10011da177e4SLinus Torvalds 		if (TestSetPageLRU(page))
10021da177e4SLinus Torvalds 			BUG();
10031da177e4SLinus Torvalds 		if (!TestClearPageActive(page))
10041da177e4SLinus Torvalds 			BUG();
10051da177e4SLinus Torvalds 		list_move(&page->lru, &zone->inactive_list);
10061da177e4SLinus Torvalds 		pgmoved++;
10071da177e4SLinus Torvalds 		if (!pagevec_add(&pvec, page)) {
10081da177e4SLinus Torvalds 			zone->nr_inactive += pgmoved;
10091da177e4SLinus Torvalds 			spin_unlock_irq(&zone->lru_lock);
10101da177e4SLinus Torvalds 			pgdeactivate += pgmoved;
10111da177e4SLinus Torvalds 			pgmoved = 0;
10121da177e4SLinus Torvalds 			if (buffer_heads_over_limit)
10131da177e4SLinus Torvalds 				pagevec_strip(&pvec);
10141da177e4SLinus Torvalds 			__pagevec_release(&pvec);
10151da177e4SLinus Torvalds 			spin_lock_irq(&zone->lru_lock);
10161da177e4SLinus Torvalds 		}
10171da177e4SLinus Torvalds 	}
10181da177e4SLinus Torvalds 	zone->nr_inactive += pgmoved;
10191da177e4SLinus Torvalds 	pgdeactivate += pgmoved;
10201da177e4SLinus Torvalds 	if (buffer_heads_over_limit) {
10211da177e4SLinus Torvalds 		spin_unlock_irq(&zone->lru_lock);
10221da177e4SLinus Torvalds 		pagevec_strip(&pvec);
10231da177e4SLinus Torvalds 		spin_lock_irq(&zone->lru_lock);
10241da177e4SLinus Torvalds 	}
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	pgmoved = 0;
10271da177e4SLinus Torvalds 	while (!list_empty(&l_active)) {
10281da177e4SLinus Torvalds 		page = lru_to_page(&l_active);
10291da177e4SLinus Torvalds 		prefetchw_prev_lru_page(page, &l_active, flags);
10301da177e4SLinus Torvalds 		if (TestSetPageLRU(page))
10311da177e4SLinus Torvalds 			BUG();
10321da177e4SLinus Torvalds 		BUG_ON(!PageActive(page));
10331da177e4SLinus Torvalds 		list_move(&page->lru, &zone->active_list);
10341da177e4SLinus Torvalds 		pgmoved++;
10351da177e4SLinus Torvalds 		if (!pagevec_add(&pvec, page)) {
10361da177e4SLinus Torvalds 			zone->nr_active += pgmoved;
10371da177e4SLinus Torvalds 			pgmoved = 0;
10381da177e4SLinus Torvalds 			spin_unlock_irq(&zone->lru_lock);
10391da177e4SLinus Torvalds 			__pagevec_release(&pvec);
10401da177e4SLinus Torvalds 			spin_lock_irq(&zone->lru_lock);
10411da177e4SLinus Torvalds 		}
10421da177e4SLinus Torvalds 	}
10431da177e4SLinus Torvalds 	zone->nr_active += pgmoved;
1044a74609faSNick Piggin 	spin_unlock(&zone->lru_lock);
10451da177e4SLinus Torvalds 
1046a74609faSNick Piggin 	__mod_page_state_zone(zone, pgrefill, pgscanned);
1047a74609faSNick Piggin 	__mod_page_state(pgdeactivate, pgdeactivate);
1048a74609faSNick Piggin 	local_irq_enable();
1049a74609faSNick Piggin 
1050a74609faSNick Piggin 	pagevec_release(&pvec);
10511da177e4SLinus Torvalds }
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds /*
10541da177e4SLinus Torvalds  * This is a basic per-zone page freer.  Used by both kswapd and direct reclaim.
10551da177e4SLinus Torvalds  */
10561da177e4SLinus Torvalds static void
10571da177e4SLinus Torvalds shrink_zone(struct zone *zone, struct scan_control *sc)
10581da177e4SLinus Torvalds {
10591da177e4SLinus Torvalds 	unsigned long nr_active;
10601da177e4SLinus Torvalds 	unsigned long nr_inactive;
10611da177e4SLinus Torvalds 
106253e9a615SMartin Hicks 	atomic_inc(&zone->reclaim_in_progress);
106353e9a615SMartin Hicks 
10641da177e4SLinus Torvalds 	/*
10651da177e4SLinus Torvalds 	 * Add one to `nr_to_scan' just to make sure that the kernel will
10661da177e4SLinus Torvalds 	 * slowly sift through the active list.
10671da177e4SLinus Torvalds 	 */
10681da177e4SLinus Torvalds 	zone->nr_scan_active += (zone->nr_active >> sc->priority) + 1;
10691da177e4SLinus Torvalds 	nr_active = zone->nr_scan_active;
10701da177e4SLinus Torvalds 	if (nr_active >= sc->swap_cluster_max)
10711da177e4SLinus Torvalds 		zone->nr_scan_active = 0;
10721da177e4SLinus Torvalds 	else
10731da177e4SLinus Torvalds 		nr_active = 0;
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	zone->nr_scan_inactive += (zone->nr_inactive >> sc->priority) + 1;
10761da177e4SLinus Torvalds 	nr_inactive = zone->nr_scan_inactive;
10771da177e4SLinus Torvalds 	if (nr_inactive >= sc->swap_cluster_max)
10781da177e4SLinus Torvalds 		zone->nr_scan_inactive = 0;
10791da177e4SLinus Torvalds 	else
10801da177e4SLinus Torvalds 		nr_inactive = 0;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	while (nr_active || nr_inactive) {
10831da177e4SLinus Torvalds 		if (nr_active) {
10841da177e4SLinus Torvalds 			sc->nr_to_scan = min(nr_active,
10851da177e4SLinus Torvalds 					(unsigned long)sc->swap_cluster_max);
10861da177e4SLinus Torvalds 			nr_active -= sc->nr_to_scan;
10871da177e4SLinus Torvalds 			refill_inactive_zone(zone, sc);
10881da177e4SLinus Torvalds 		}
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 		if (nr_inactive) {
10911da177e4SLinus Torvalds 			sc->nr_to_scan = min(nr_inactive,
10921da177e4SLinus Torvalds 					(unsigned long)sc->swap_cluster_max);
10931da177e4SLinus Torvalds 			nr_inactive -= sc->nr_to_scan;
10941da177e4SLinus Torvalds 			shrink_cache(zone, sc);
10951da177e4SLinus Torvalds 		}
10961da177e4SLinus Torvalds 	}
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	throttle_vm_writeout();
109953e9a615SMartin Hicks 
110053e9a615SMartin Hicks 	atomic_dec(&zone->reclaim_in_progress);
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds /*
11041da177e4SLinus Torvalds  * This is the direct reclaim path, for page-allocating processes.  We only
11051da177e4SLinus Torvalds  * try to reclaim pages from zones which will satisfy the caller's allocation
11061da177e4SLinus Torvalds  * request.
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  * We reclaim from a zone even if that zone is over pages_high.  Because:
11091da177e4SLinus Torvalds  * a) The caller may be trying to free *extra* pages to satisfy a higher-order
11101da177e4SLinus Torvalds  *    allocation or
11111da177e4SLinus Torvalds  * b) The zones may be over pages_high but they must go *over* pages_high to
11121da177e4SLinus Torvalds  *    satisfy the `incremental min' zone defense algorithm.
11131da177e4SLinus Torvalds  *
11141da177e4SLinus Torvalds  * Returns the number of reclaimed pages.
11151da177e4SLinus Torvalds  *
11161da177e4SLinus Torvalds  * If a zone is deemed to be full of pinned pages then just give it a light
11171da177e4SLinus Torvalds  * scan then give up on it.
11181da177e4SLinus Torvalds  */
11191da177e4SLinus Torvalds static void
11201da177e4SLinus Torvalds shrink_caches(struct zone **zones, struct scan_control *sc)
11211da177e4SLinus Torvalds {
11221da177e4SLinus Torvalds 	int i;
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 	for (i = 0; zones[i] != NULL; i++) {
11251da177e4SLinus Torvalds 		struct zone *zone = zones[i];
11261da177e4SLinus Torvalds 
1127f3fe6512SCon Kolivas 		if (!populated_zone(zone))
11281da177e4SLinus Torvalds 			continue;
11291da177e4SLinus Torvalds 
11309bf2229fSPaul Jackson 		if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
11311da177e4SLinus Torvalds 			continue;
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 		zone->temp_priority = sc->priority;
11341da177e4SLinus Torvalds 		if (zone->prev_priority > sc->priority)
11351da177e4SLinus Torvalds 			zone->prev_priority = sc->priority;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		if (zone->all_unreclaimable && sc->priority != DEF_PRIORITY)
11381da177e4SLinus Torvalds 			continue;	/* Let kswapd poll it */
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 		shrink_zone(zone, sc);
11411da177e4SLinus Torvalds 	}
11421da177e4SLinus Torvalds }
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds /*
11451da177e4SLinus Torvalds  * This is the main entry point to direct page reclaim.
11461da177e4SLinus Torvalds  *
11471da177e4SLinus Torvalds  * If a full scan of the inactive list fails to free enough memory then we
11481da177e4SLinus Torvalds  * are "out of memory" and something needs to be killed.
11491da177e4SLinus Torvalds  *
11501da177e4SLinus Torvalds  * If the caller is !__GFP_FS then the probability of a failure is reasonably
11511da177e4SLinus Torvalds  * high - the zone may be full of dirty or under-writeback pages, which this
11521da177e4SLinus Torvalds  * caller can't do much about.  We kick pdflush and take explicit naps in the
11531da177e4SLinus Torvalds  * hope that some of these pages can be written.  But if the allocating task
11541da177e4SLinus Torvalds  * holds filesystem locks which prevent writeout this might not work, and the
11551da177e4SLinus Torvalds  * allocation attempt will fail.
11561da177e4SLinus Torvalds  */
11576daa0e28SAl Viro int try_to_free_pages(struct zone **zones, gfp_t gfp_mask)
11581da177e4SLinus Torvalds {
11591da177e4SLinus Torvalds 	int priority;
11601da177e4SLinus Torvalds 	int ret = 0;
11611da177e4SLinus Torvalds 	int total_scanned = 0, total_reclaimed = 0;
11621da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
11631da177e4SLinus Torvalds 	struct scan_control sc;
11641da177e4SLinus Torvalds 	unsigned long lru_pages = 0;
11651da177e4SLinus Torvalds 	int i;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	sc.gfp_mask = gfp_mask;
11681da177e4SLinus Torvalds 	sc.may_writepage = 0;
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	inc_page_state(allocstall);
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 	for (i = 0; zones[i] != NULL; i++) {
11731da177e4SLinus Torvalds 		struct zone *zone = zones[i];
11741da177e4SLinus Torvalds 
11759bf2229fSPaul Jackson 		if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
11761da177e4SLinus Torvalds 			continue;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 		zone->temp_priority = DEF_PRIORITY;
11791da177e4SLinus Torvalds 		lru_pages += zone->nr_active + zone->nr_inactive;
11801da177e4SLinus Torvalds 	}
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	for (priority = DEF_PRIORITY; priority >= 0; priority--) {
11831da177e4SLinus Torvalds 		sc.nr_mapped = read_page_state(nr_mapped);
11841da177e4SLinus Torvalds 		sc.nr_scanned = 0;
11851da177e4SLinus Torvalds 		sc.nr_reclaimed = 0;
11861da177e4SLinus Torvalds 		sc.priority = priority;
11871da177e4SLinus Torvalds 		sc.swap_cluster_max = SWAP_CLUSTER_MAX;
1188f7b7fd8fSRik van Riel 		if (!priority)
1189f7b7fd8fSRik van Riel 			disable_swap_token();
11901da177e4SLinus Torvalds 		shrink_caches(zones, &sc);
11911da177e4SLinus Torvalds 		shrink_slab(sc.nr_scanned, gfp_mask, lru_pages);
11921da177e4SLinus Torvalds 		if (reclaim_state) {
11931da177e4SLinus Torvalds 			sc.nr_reclaimed += reclaim_state->reclaimed_slab;
11941da177e4SLinus Torvalds 			reclaim_state->reclaimed_slab = 0;
11951da177e4SLinus Torvalds 		}
11961da177e4SLinus Torvalds 		total_scanned += sc.nr_scanned;
11971da177e4SLinus Torvalds 		total_reclaimed += sc.nr_reclaimed;
11981da177e4SLinus Torvalds 		if (total_reclaimed >= sc.swap_cluster_max) {
11991da177e4SLinus Torvalds 			ret = 1;
12001da177e4SLinus Torvalds 			goto out;
12011da177e4SLinus Torvalds 		}
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 		/*
12041da177e4SLinus Torvalds 		 * Try to write back as many pages as we just scanned.  This
12051da177e4SLinus Torvalds 		 * tends to cause slow streaming writers to write data to the
12061da177e4SLinus Torvalds 		 * disk smoothly, at the dirtying rate, which is nice.   But
12071da177e4SLinus Torvalds 		 * that's undesirable in laptop mode, where we *want* lumpy
12081da177e4SLinus Torvalds 		 * writeout.  So in laptop mode, write out the whole world.
12091da177e4SLinus Torvalds 		 */
12101da177e4SLinus Torvalds 		if (total_scanned > sc.swap_cluster_max + sc.swap_cluster_max/2) {
1211687a21ceSPekka J Enberg 			wakeup_pdflush(laptop_mode ? 0 : total_scanned);
12121da177e4SLinus Torvalds 			sc.may_writepage = 1;
12131da177e4SLinus Torvalds 		}
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds 		/* Take a nap, wait for some writeback to complete */
12161da177e4SLinus Torvalds 		if (sc.nr_scanned && priority < DEF_PRIORITY - 2)
12171da177e4SLinus Torvalds 			blk_congestion_wait(WRITE, HZ/10);
12181da177e4SLinus Torvalds 	}
12191da177e4SLinus Torvalds out:
12201da177e4SLinus Torvalds 	for (i = 0; zones[i] != 0; i++) {
12211da177e4SLinus Torvalds 		struct zone *zone = zones[i];
12221da177e4SLinus Torvalds 
12239bf2229fSPaul Jackson 		if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
12241da177e4SLinus Torvalds 			continue;
12251da177e4SLinus Torvalds 
12261da177e4SLinus Torvalds 		zone->prev_priority = zone->temp_priority;
12271da177e4SLinus Torvalds 	}
12281da177e4SLinus Torvalds 	return ret;
12291da177e4SLinus Torvalds }
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds /*
12321da177e4SLinus Torvalds  * For kswapd, balance_pgdat() will work across all this node's zones until
12331da177e4SLinus Torvalds  * they are all at pages_high.
12341da177e4SLinus Torvalds  *
12351da177e4SLinus Torvalds  * If `nr_pages' is non-zero then it is the number of pages which are to be
12361da177e4SLinus Torvalds  * reclaimed, regardless of the zone occupancies.  This is a software suspend
12371da177e4SLinus Torvalds  * special.
12381da177e4SLinus Torvalds  *
12391da177e4SLinus Torvalds  * Returns the number of pages which were actually freed.
12401da177e4SLinus Torvalds  *
12411da177e4SLinus Torvalds  * There is special handling here for zones which are full of pinned pages.
12421da177e4SLinus Torvalds  * This can happen if the pages are all mlocked, or if they are all used by
12431da177e4SLinus Torvalds  * device drivers (say, ZONE_DMA).  Or if they are all in use by hugetlb.
12441da177e4SLinus Torvalds  * What we do is to detect the case where all pages in the zone have been
12451da177e4SLinus Torvalds  * scanned twice and there has been zero successful reclaim.  Mark the zone as
12461da177e4SLinus Torvalds  * dead and from now on, only perform a short scan.  Basically we're polling
12471da177e4SLinus Torvalds  * the zone for when the problem goes away.
12481da177e4SLinus Torvalds  *
12491da177e4SLinus Torvalds  * kswapd scans the zones in the highmem->normal->dma direction.  It skips
12501da177e4SLinus Torvalds  * zones which have free_pages > pages_high, but once a zone is found to have
12511da177e4SLinus Torvalds  * free_pages <= pages_high, we scan that zone and the lower zones regardless
12521da177e4SLinus Torvalds  * of the number of free pages in the lower zones.  This interoperates with
12531da177e4SLinus Torvalds  * the page allocator fallback scheme to ensure that aging of pages is balanced
12541da177e4SLinus Torvalds  * across the zones.
12551da177e4SLinus Torvalds  */
12561da177e4SLinus Torvalds static int balance_pgdat(pg_data_t *pgdat, int nr_pages, int order)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	int to_free = nr_pages;
12591da177e4SLinus Torvalds 	int all_zones_ok;
12601da177e4SLinus Torvalds 	int priority;
12611da177e4SLinus Torvalds 	int i;
12621da177e4SLinus Torvalds 	int total_scanned, total_reclaimed;
12631da177e4SLinus Torvalds 	struct reclaim_state *reclaim_state = current->reclaim_state;
12641da177e4SLinus Torvalds 	struct scan_control sc;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds loop_again:
12671da177e4SLinus Torvalds 	total_scanned = 0;
12681da177e4SLinus Torvalds 	total_reclaimed = 0;
12691da177e4SLinus Torvalds 	sc.gfp_mask = GFP_KERNEL;
12701da177e4SLinus Torvalds 	sc.may_writepage = 0;
12711da177e4SLinus Torvalds 	sc.nr_mapped = read_page_state(nr_mapped);
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds 	inc_page_state(pageoutrun);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	for (i = 0; i < pgdat->nr_zones; i++) {
12761da177e4SLinus Torvalds 		struct zone *zone = pgdat->node_zones + i;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 		zone->temp_priority = DEF_PRIORITY;
12791da177e4SLinus Torvalds 	}
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	for (priority = DEF_PRIORITY; priority >= 0; priority--) {
12821da177e4SLinus Torvalds 		int end_zone = 0;	/* Inclusive.  0 = ZONE_DMA */
12831da177e4SLinus Torvalds 		unsigned long lru_pages = 0;
12841da177e4SLinus Torvalds 
1285f7b7fd8fSRik van Riel 		/* The swap token gets in the way of swapout... */
1286f7b7fd8fSRik van Riel 		if (!priority)
1287f7b7fd8fSRik van Riel 			disable_swap_token();
1288f7b7fd8fSRik van Riel 
12891da177e4SLinus Torvalds 		all_zones_ok = 1;
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 		if (nr_pages == 0) {
12921da177e4SLinus Torvalds 			/*
12931da177e4SLinus Torvalds 			 * Scan in the highmem->dma direction for the highest
12941da177e4SLinus Torvalds 			 * zone which needs scanning
12951da177e4SLinus Torvalds 			 */
12961da177e4SLinus Torvalds 			for (i = pgdat->nr_zones - 1; i >= 0; i--) {
12971da177e4SLinus Torvalds 				struct zone *zone = pgdat->node_zones + i;
12981da177e4SLinus Torvalds 
1299f3fe6512SCon Kolivas 				if (!populated_zone(zone))
13001da177e4SLinus Torvalds 					continue;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 				if (zone->all_unreclaimable &&
13031da177e4SLinus Torvalds 						priority != DEF_PRIORITY)
13041da177e4SLinus Torvalds 					continue;
13051da177e4SLinus Torvalds 
13061da177e4SLinus Torvalds 				if (!zone_watermark_ok(zone, order,
13077fb1d9fcSRohit Seth 						zone->pages_high, 0, 0)) {
13081da177e4SLinus Torvalds 					end_zone = i;
13091da177e4SLinus Torvalds 					goto scan;
13101da177e4SLinus Torvalds 				}
13111da177e4SLinus Torvalds 			}
13121da177e4SLinus Torvalds 			goto out;
13131da177e4SLinus Torvalds 		} else {
13141da177e4SLinus Torvalds 			end_zone = pgdat->nr_zones - 1;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds scan:
13171da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
13181da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds 			lru_pages += zone->nr_active + zone->nr_inactive;
13211da177e4SLinus Torvalds 		}
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 		/*
13241da177e4SLinus Torvalds 		 * Now scan the zone in the dma->highmem direction, stopping
13251da177e4SLinus Torvalds 		 * at the last zone which needs scanning.
13261da177e4SLinus Torvalds 		 *
13271da177e4SLinus Torvalds 		 * We do this because the page allocator works in the opposite
13281da177e4SLinus Torvalds 		 * direction.  This prevents the page allocator from allocating
13291da177e4SLinus Torvalds 		 * pages behind kswapd's direction of progress, which would
13301da177e4SLinus Torvalds 		 * cause too much scanning of the lower zones.
13311da177e4SLinus Torvalds 		 */
13321da177e4SLinus Torvalds 		for (i = 0; i <= end_zone; i++) {
13331da177e4SLinus Torvalds 			struct zone *zone = pgdat->node_zones + i;
1334b15e0905Sakpm@osdl.org 			int nr_slab;
13351da177e4SLinus Torvalds 
1336f3fe6512SCon Kolivas 			if (!populated_zone(zone))
13371da177e4SLinus Torvalds 				continue;
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds 			if (zone->all_unreclaimable && priority != DEF_PRIORITY)
13401da177e4SLinus Torvalds 				continue;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 			if (nr_pages == 0) {	/* Not software suspend */
13431da177e4SLinus Torvalds 				if (!zone_watermark_ok(zone, order,
13447fb1d9fcSRohit Seth 						zone->pages_high, end_zone, 0))
13451da177e4SLinus Torvalds 					all_zones_ok = 0;
13461da177e4SLinus Torvalds 			}
13471da177e4SLinus Torvalds 			zone->temp_priority = priority;
13481da177e4SLinus Torvalds 			if (zone->prev_priority > priority)
13491da177e4SLinus Torvalds 				zone->prev_priority = priority;
13501da177e4SLinus Torvalds 			sc.nr_scanned = 0;
13511da177e4SLinus Torvalds 			sc.nr_reclaimed = 0;
13521da177e4SLinus Torvalds 			sc.priority = priority;
13531da177e4SLinus Torvalds 			sc.swap_cluster_max = nr_pages? nr_pages : SWAP_CLUSTER_MAX;
13541e7e5a90SMartin Hicks 			atomic_inc(&zone->reclaim_in_progress);
13551da177e4SLinus Torvalds 			shrink_zone(zone, &sc);
13561e7e5a90SMartin Hicks 			atomic_dec(&zone->reclaim_in_progress);
13571da177e4SLinus Torvalds 			reclaim_state->reclaimed_slab = 0;
1358b15e0905Sakpm@osdl.org 			nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL,
1359b15e0905Sakpm@osdl.org 						lru_pages);
13601da177e4SLinus Torvalds 			sc.nr_reclaimed += reclaim_state->reclaimed_slab;
13611da177e4SLinus Torvalds 			total_reclaimed += sc.nr_reclaimed;
13621da177e4SLinus Torvalds 			total_scanned += sc.nr_scanned;
13631da177e4SLinus Torvalds 			if (zone->all_unreclaimable)
13641da177e4SLinus Torvalds 				continue;
1365b15e0905Sakpm@osdl.org 			if (nr_slab == 0 && zone->pages_scanned >=
1366b15e0905Sakpm@osdl.org 				    (zone->nr_active + zone->nr_inactive) * 4)
13671da177e4SLinus Torvalds 				zone->all_unreclaimable = 1;
13681da177e4SLinus Torvalds 			/*
13691da177e4SLinus Torvalds 			 * If we've done a decent amount of scanning and
13701da177e4SLinus Torvalds 			 * the reclaim ratio is low, start doing writepage
13711da177e4SLinus Torvalds 			 * even in laptop mode
13721da177e4SLinus Torvalds 			 */
13731da177e4SLinus Torvalds 			if (total_scanned > SWAP_CLUSTER_MAX * 2 &&
13741da177e4SLinus Torvalds 			    total_scanned > total_reclaimed+total_reclaimed/2)
13751da177e4SLinus Torvalds 				sc.may_writepage = 1;
13761da177e4SLinus Torvalds 		}
13771da177e4SLinus Torvalds 		if (nr_pages && to_free > total_reclaimed)
13781da177e4SLinus Torvalds 			continue;	/* swsusp: need to do more work */
13791da177e4SLinus Torvalds 		if (all_zones_ok)
13801da177e4SLinus Torvalds 			break;		/* kswapd: all done */
13811da177e4SLinus Torvalds 		/*
13821da177e4SLinus Torvalds 		 * OK, kswapd is getting into trouble.  Take a nap, then take
13831da177e4SLinus Torvalds 		 * another pass across the zones.
13841da177e4SLinus Torvalds 		 */
13851da177e4SLinus Torvalds 		if (total_scanned && priority < DEF_PRIORITY - 2)
13861da177e4SLinus Torvalds 			blk_congestion_wait(WRITE, HZ/10);
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 		/*
13891da177e4SLinus Torvalds 		 * We do this so kswapd doesn't build up large priorities for
13901da177e4SLinus Torvalds 		 * example when it is freeing in parallel with allocators. It
13911da177e4SLinus Torvalds 		 * matches the direct reclaim path behaviour in terms of impact
13921da177e4SLinus Torvalds 		 * on zone->*_priority.
13931da177e4SLinus Torvalds 		 */
13941da177e4SLinus Torvalds 		if ((total_reclaimed >= SWAP_CLUSTER_MAX) && (!nr_pages))
13951da177e4SLinus Torvalds 			break;
13961da177e4SLinus Torvalds 	}
13971da177e4SLinus Torvalds out:
13981da177e4SLinus Torvalds 	for (i = 0; i < pgdat->nr_zones; i++) {
13991da177e4SLinus Torvalds 		struct zone *zone = pgdat->node_zones + i;
14001da177e4SLinus Torvalds 
14011da177e4SLinus Torvalds 		zone->prev_priority = zone->temp_priority;
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 	if (!all_zones_ok) {
14041da177e4SLinus Torvalds 		cond_resched();
14051da177e4SLinus Torvalds 		goto loop_again;
14061da177e4SLinus Torvalds 	}
14071da177e4SLinus Torvalds 
14081da177e4SLinus Torvalds 	return total_reclaimed;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds /*
14121da177e4SLinus Torvalds  * The background pageout daemon, started as a kernel thread
14131da177e4SLinus Torvalds  * from the init process.
14141da177e4SLinus Torvalds  *
14151da177e4SLinus Torvalds  * This basically trickles out pages so that we have _some_
14161da177e4SLinus Torvalds  * free memory available even if there is no other activity
14171da177e4SLinus Torvalds  * that frees anything up. This is needed for things like routing
14181da177e4SLinus Torvalds  * etc, where we otherwise might have all activity going on in
14191da177e4SLinus Torvalds  * asynchronous contexts that cannot page things out.
14201da177e4SLinus Torvalds  *
14211da177e4SLinus Torvalds  * If there are applications that are active memory-allocators
14221da177e4SLinus Torvalds  * (most normal use), this basically shouldn't matter.
14231da177e4SLinus Torvalds  */
14241da177e4SLinus Torvalds static int kswapd(void *p)
14251da177e4SLinus Torvalds {
14261da177e4SLinus Torvalds 	unsigned long order;
14271da177e4SLinus Torvalds 	pg_data_t *pgdat = (pg_data_t*)p;
14281da177e4SLinus Torvalds 	struct task_struct *tsk = current;
14291da177e4SLinus Torvalds 	DEFINE_WAIT(wait);
14301da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
14311da177e4SLinus Torvalds 		.reclaimed_slab = 0,
14321da177e4SLinus Torvalds 	};
14331da177e4SLinus Torvalds 	cpumask_t cpumask;
14341da177e4SLinus Torvalds 
14351da177e4SLinus Torvalds 	daemonize("kswapd%d", pgdat->node_id);
14361da177e4SLinus Torvalds 	cpumask = node_to_cpumask(pgdat->node_id);
14371da177e4SLinus Torvalds 	if (!cpus_empty(cpumask))
14381da177e4SLinus Torvalds 		set_cpus_allowed(tsk, cpumask);
14391da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	/*
14421da177e4SLinus Torvalds 	 * Tell the memory management that we're a "memory allocator",
14431da177e4SLinus Torvalds 	 * and that if we need more memory we should get access to it
14441da177e4SLinus Torvalds 	 * regardless (see "__alloc_pages()"). "kswapd" should
14451da177e4SLinus Torvalds 	 * never get caught in the normal page freeing logic.
14461da177e4SLinus Torvalds 	 *
14471da177e4SLinus Torvalds 	 * (Kswapd normally doesn't need memory anyway, but sometimes
14481da177e4SLinus Torvalds 	 * you need a small amount of memory in order to be able to
14491da177e4SLinus Torvalds 	 * page out something else, and this flag essentially protects
14501da177e4SLinus Torvalds 	 * us from recursively trying to free more memory as we're
14511da177e4SLinus Torvalds 	 * trying to free the first piece of memory in the first place).
14521da177e4SLinus Torvalds 	 */
1453930d9152SChristoph Lameter 	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	order = 0;
14561da177e4SLinus Torvalds 	for ( ; ; ) {
14571da177e4SLinus Torvalds 		unsigned long new_order;
14583e1d1d28SChristoph Lameter 
14593e1d1d28SChristoph Lameter 		try_to_freeze();
14601da177e4SLinus Torvalds 
14611da177e4SLinus Torvalds 		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
14621da177e4SLinus Torvalds 		new_order = pgdat->kswapd_max_order;
14631da177e4SLinus Torvalds 		pgdat->kswapd_max_order = 0;
14641da177e4SLinus Torvalds 		if (order < new_order) {
14651da177e4SLinus Torvalds 			/*
14661da177e4SLinus Torvalds 			 * Don't sleep if someone wants a larger 'order'
14671da177e4SLinus Torvalds 			 * allocation
14681da177e4SLinus Torvalds 			 */
14691da177e4SLinus Torvalds 			order = new_order;
14701da177e4SLinus Torvalds 		} else {
14711da177e4SLinus Torvalds 			schedule();
14721da177e4SLinus Torvalds 			order = pgdat->kswapd_max_order;
14731da177e4SLinus Torvalds 		}
14741da177e4SLinus Torvalds 		finish_wait(&pgdat->kswapd_wait, &wait);
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 		balance_pgdat(pgdat, 0, order);
14771da177e4SLinus Torvalds 	}
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds /*
14821da177e4SLinus Torvalds  * A zone is low on free memory, so wake its kswapd task to service it.
14831da177e4SLinus Torvalds  */
14841da177e4SLinus Torvalds void wakeup_kswapd(struct zone *zone, int order)
14851da177e4SLinus Torvalds {
14861da177e4SLinus Torvalds 	pg_data_t *pgdat;
14871da177e4SLinus Torvalds 
1488f3fe6512SCon Kolivas 	if (!populated_zone(zone))
14891da177e4SLinus Torvalds 		return;
14901da177e4SLinus Torvalds 
14911da177e4SLinus Torvalds 	pgdat = zone->zone_pgdat;
14927fb1d9fcSRohit Seth 	if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0))
14931da177e4SLinus Torvalds 		return;
14941da177e4SLinus Torvalds 	if (pgdat->kswapd_max_order < order)
14951da177e4SLinus Torvalds 		pgdat->kswapd_max_order = order;
14969bf2229fSPaul Jackson 	if (!cpuset_zone_allowed(zone, __GFP_HARDWALL))
14971da177e4SLinus Torvalds 		return;
14988d0986e2SCon Kolivas 	if (!waitqueue_active(&pgdat->kswapd_wait))
14991da177e4SLinus Torvalds 		return;
15008d0986e2SCon Kolivas 	wake_up_interruptible(&pgdat->kswapd_wait);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds #ifdef CONFIG_PM
15041da177e4SLinus Torvalds /*
15051da177e4SLinus Torvalds  * Try to free `nr_pages' of memory, system-wide.  Returns the number of freed
15061da177e4SLinus Torvalds  * pages.
15071da177e4SLinus Torvalds  */
15081da177e4SLinus Torvalds int shrink_all_memory(int nr_pages)
15091da177e4SLinus Torvalds {
15101da177e4SLinus Torvalds 	pg_data_t *pgdat;
15111da177e4SLinus Torvalds 	int nr_to_free = nr_pages;
15121da177e4SLinus Torvalds 	int ret = 0;
15131da177e4SLinus Torvalds 	struct reclaim_state reclaim_state = {
15141da177e4SLinus Torvalds 		.reclaimed_slab = 0,
15151da177e4SLinus Torvalds 	};
15161da177e4SLinus Torvalds 
15171da177e4SLinus Torvalds 	current->reclaim_state = &reclaim_state;
15181da177e4SLinus Torvalds 	for_each_pgdat(pgdat) {
15191da177e4SLinus Torvalds 		int freed;
15201da177e4SLinus Torvalds 		freed = balance_pgdat(pgdat, nr_to_free, 0);
15211da177e4SLinus Torvalds 		ret += freed;
15221da177e4SLinus Torvalds 		nr_to_free -= freed;
15231da177e4SLinus Torvalds 		if (nr_to_free <= 0)
15241da177e4SLinus Torvalds 			break;
15251da177e4SLinus Torvalds 	}
15261da177e4SLinus Torvalds 	current->reclaim_state = NULL;
15271da177e4SLinus Torvalds 	return ret;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds #endif
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
15321da177e4SLinus Torvalds /* It's optimal to keep kswapds on the same CPUs as their memory, but
15331da177e4SLinus Torvalds    not required for correctness.  So if the last cpu in a node goes
15341da177e4SLinus Torvalds    away, we get changed to run anywhere: as the first one comes back,
15351da177e4SLinus Torvalds    restore their cpu bindings. */
15361da177e4SLinus Torvalds static int __devinit cpu_callback(struct notifier_block *nfb,
15371da177e4SLinus Torvalds 				  unsigned long action,
15381da177e4SLinus Torvalds 				  void *hcpu)
15391da177e4SLinus Torvalds {
15401da177e4SLinus Torvalds 	pg_data_t *pgdat;
15411da177e4SLinus Torvalds 	cpumask_t mask;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	if (action == CPU_ONLINE) {
15441da177e4SLinus Torvalds 		for_each_pgdat(pgdat) {
15451da177e4SLinus Torvalds 			mask = node_to_cpumask(pgdat->node_id);
15461da177e4SLinus Torvalds 			if (any_online_cpu(mask) != NR_CPUS)
15471da177e4SLinus Torvalds 				/* One of our CPUs online: restore mask */
15481da177e4SLinus Torvalds 				set_cpus_allowed(pgdat->kswapd, mask);
15491da177e4SLinus Torvalds 		}
15501da177e4SLinus Torvalds 	}
15511da177e4SLinus Torvalds 	return NOTIFY_OK;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds #endif /* CONFIG_HOTPLUG_CPU */
15541da177e4SLinus Torvalds 
15551da177e4SLinus Torvalds static int __init kswapd_init(void)
15561da177e4SLinus Torvalds {
15571da177e4SLinus Torvalds 	pg_data_t *pgdat;
15581da177e4SLinus Torvalds 	swap_setup();
15591da177e4SLinus Torvalds 	for_each_pgdat(pgdat)
15601da177e4SLinus Torvalds 		pgdat->kswapd
15611da177e4SLinus Torvalds 		= find_task_by_pid(kernel_thread(kswapd, pgdat, CLONE_KERNEL));
15621da177e4SLinus Torvalds 	total_memory = nr_free_pagecache_pages();
15631da177e4SLinus Torvalds 	hotcpu_notifier(cpu_callback, 0);
15641da177e4SLinus Torvalds 	return 0;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds module_init(kswapd_init)
1568