xref: /openbmc/linux/mm/swapfile.c (revision 570a335b8e22579e2a51a68136d2b1f907a20eec)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swapfile.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/hugetlb.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
141da177e4SLinus Torvalds #include <linux/vmalloc.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/shm.h>
181da177e4SLinus Torvalds #include <linux/blkdev.h>
1920137a49SHugh Dickins #include <linux/random.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
211da177e4SLinus Torvalds #include <linux/proc_fs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/rmap.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28fc0abb14SIngo Molnar #include <linux/mutex.h>
29c59ede7bSRandy.Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/syscalls.h>
318a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <asm/pgtable.h>
341da177e4SLinus Torvalds #include <asm/tlbflush.h>
351da177e4SLinus Torvalds #include <linux/swapops.h>
3627a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h>
371da177e4SLinus Torvalds 
38*570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
39*570a335bSHugh Dickins 				 unsigned char);
40*570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
41*570a335bSHugh Dickins 
427c363b8cSAdrian Bunk static DEFINE_SPINLOCK(swap_lock);
437c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
44b962716bSHugh Dickins long nr_swap_pages;
451da177e4SLinus Torvalds long total_swap_pages;
4678ecba08SHugh Dickins static int least_priority;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
491da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
501da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
511da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
521da177e4SLinus Torvalds 
537c363b8cSAdrian Bunk static struct swap_list_t swap_list = {-1, -1};
541da177e4SLinus Torvalds 
55efa90a98SHugh Dickins static struct swap_info_struct *swap_info[MAX_SWAPFILES];
561da177e4SLinus Torvalds 
57fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
581da177e4SLinus Torvalds 
598d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
60355cfa73SKAMEZAWA Hiroyuki {
61*570a335bSHugh Dickins 	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
62355cfa73SKAMEZAWA Hiroyuki }
63355cfa73SKAMEZAWA Hiroyuki 
64efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
65c9e44410SKAMEZAWA Hiroyuki static int
66c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
67c9e44410SKAMEZAWA Hiroyuki {
68efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
69c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
70c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
71c9e44410SKAMEZAWA Hiroyuki 
72c9e44410SKAMEZAWA Hiroyuki 	page = find_get_page(&swapper_space, entry.val);
73c9e44410SKAMEZAWA Hiroyuki 	if (!page)
74c9e44410SKAMEZAWA Hiroyuki 		return 0;
75c9e44410SKAMEZAWA Hiroyuki 	/*
76c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
77c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
78c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
79c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
80c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
81c9e44410SKAMEZAWA Hiroyuki 	 */
82c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
83c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
84c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
85c9e44410SKAMEZAWA Hiroyuki 	}
86c9e44410SKAMEZAWA Hiroyuki 	page_cache_release(page);
87c9e44410SKAMEZAWA Hiroyuki 	return ret;
88c9e44410SKAMEZAWA Hiroyuki }
89355cfa73SKAMEZAWA Hiroyuki 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * We need this because the bdev->unplug_fn can sleep and we cannot
925d337b91SHugh Dickins  * hold swap_lock while calling the unplug_fn. And swap_lock
93fc0abb14SIngo Molnar  * cannot be turned into a mutex.
941da177e4SLinus Torvalds  */
951da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem);
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	swp_entry_t entry;
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	down_read(&swap_unplug_sem);
1024c21e2f2SHugh Dickins 	entry.val = page_private(page);
1031da177e4SLinus Torvalds 	if (PageSwapCache(page)) {
104efa90a98SHugh Dickins 		struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
1051da177e4SLinus Torvalds 		struct backing_dev_info *bdi;
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 		/*
1081da177e4SLinus Torvalds 		 * If the page is removed from swapcache from under us (with a
1091da177e4SLinus Torvalds 		 * racy try_to_unuse/swapoff) we need an additional reference
1104c21e2f2SHugh Dickins 		 * count to avoid reading garbage from page_private(page) above.
1114c21e2f2SHugh Dickins 		 * If the WARN_ON triggers during a swapoff it maybe the race
1121da177e4SLinus Torvalds 		 * condition and it's harmless. However if it triggers without
1131da177e4SLinus Torvalds 		 * swapoff it signals a problem.
1141da177e4SLinus Torvalds 		 */
1151da177e4SLinus Torvalds 		WARN_ON(page_count(page) <= 1);
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 		bdi = bdev->bd_inode->i_mapping->backing_dev_info;
118ba32311eSMcMullan, Jason 		blk_run_backing_dev(bdi, page);
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds 	up_read(&swap_unplug_sem);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1236a6ba831SHugh Dickins /*
1246a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1256a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1266a6ba831SHugh Dickins  */
1276a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1286a6ba831SHugh Dickins {
1296a6ba831SHugh Dickins 	struct swap_extent *se;
1309625a5f2SHugh Dickins 	sector_t start_block;
1319625a5f2SHugh Dickins 	sector_t nr_blocks;
1326a6ba831SHugh Dickins 	int err = 0;
1336a6ba831SHugh Dickins 
1346a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1359625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1369625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1379625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1389625a5f2SHugh Dickins 	if (nr_blocks) {
1399625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
1409625a5f2SHugh Dickins 				nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
1419625a5f2SHugh Dickins 		if (err)
1429625a5f2SHugh Dickins 			return err;
1439625a5f2SHugh Dickins 		cond_resched();
1446a6ba831SHugh Dickins 	}
1456a6ba831SHugh Dickins 
1469625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1479625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1489625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1499625a5f2SHugh Dickins 
1506a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
1519625a5f2SHugh Dickins 				nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
1526a6ba831SHugh Dickins 		if (err)
1536a6ba831SHugh Dickins 			break;
1546a6ba831SHugh Dickins 
1556a6ba831SHugh Dickins 		cond_resched();
1566a6ba831SHugh Dickins 	}
1576a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1586a6ba831SHugh Dickins }
1596a6ba831SHugh Dickins 
1607992fde7SHugh Dickins /*
1617992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1627992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1637992fde7SHugh Dickins  */
1647992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1657992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1667992fde7SHugh Dickins {
1677992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1687992fde7SHugh Dickins 	int found_extent = 0;
1697992fde7SHugh Dickins 
1707992fde7SHugh Dickins 	while (nr_pages) {
1717992fde7SHugh Dickins 		struct list_head *lh;
1727992fde7SHugh Dickins 
1737992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1747992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1757992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1767992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
177858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1787992fde7SHugh Dickins 
1797992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1807992fde7SHugh Dickins 				nr_blocks = nr_pages;
1817992fde7SHugh Dickins 			start_page += nr_blocks;
1827992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1837992fde7SHugh Dickins 
1847992fde7SHugh Dickins 			if (!found_extent++)
1857992fde7SHugh Dickins 				si->curr_swap_extent = se;
1867992fde7SHugh Dickins 
1877992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1887992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1897992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
1909625a5f2SHugh Dickins 				    nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
1917992fde7SHugh Dickins 				break;
1927992fde7SHugh Dickins 		}
1937992fde7SHugh Dickins 
1947992fde7SHugh Dickins 		lh = se->list.next;
1957992fde7SHugh Dickins 		se = list_entry(lh, struct swap_extent, list);
1967992fde7SHugh Dickins 	}
1977992fde7SHugh Dickins }
1987992fde7SHugh Dickins 
1997992fde7SHugh Dickins static int wait_for_discard(void *word)
2007992fde7SHugh Dickins {
2017992fde7SHugh Dickins 	schedule();
2027992fde7SHugh Dickins 	return 0;
2037992fde7SHugh Dickins }
2047992fde7SHugh Dickins 
205048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
206048c27fdSHugh Dickins #define LATENCY_LIMIT		256
207048c27fdSHugh Dickins 
208355cfa73SKAMEZAWA Hiroyuki static inline unsigned long scan_swap_map(struct swap_info_struct *si,
2098d69aaeeSHugh Dickins 					  unsigned char usage)
2101da177e4SLinus Torvalds {
211ebebbbe9SHugh Dickins 	unsigned long offset;
212c60aa176SHugh Dickins 	unsigned long scan_base;
2137992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
214048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
2157992fde7SHugh Dickins 	int found_free_cluster = 0;
2161da177e4SLinus Torvalds 
2177dfad418SHugh Dickins 	/*
2187dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
2197dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
2207dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
2217dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
2227dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
2237dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
2247dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
225c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
2261da177e4SLinus Torvalds 	 */
2277dfad418SHugh Dickins 
22852b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
229c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
230ebebbbe9SHugh Dickins 
231ebebbbe9SHugh Dickins 	if (unlikely(!si->cluster_nr--)) {
232ebebbbe9SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
2337dfad418SHugh Dickins 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
234ebebbbe9SHugh Dickins 			goto checks;
235ebebbbe9SHugh Dickins 		}
2367992fde7SHugh Dickins 		if (si->flags & SWP_DISCARDABLE) {
2377992fde7SHugh Dickins 			/*
2387992fde7SHugh Dickins 			 * Start range check on racing allocations, in case
2397992fde7SHugh Dickins 			 * they overlap the cluster we eventually decide on
2407992fde7SHugh Dickins 			 * (we scan without swap_lock to allow preemption).
2417992fde7SHugh Dickins 			 * It's hardly conceivable that cluster_nr could be
2427992fde7SHugh Dickins 			 * wrapped during our scan, but don't depend on it.
2437992fde7SHugh Dickins 			 */
2447992fde7SHugh Dickins 			if (si->lowest_alloc)
2457992fde7SHugh Dickins 				goto checks;
2467992fde7SHugh Dickins 			si->lowest_alloc = si->max;
2477992fde7SHugh Dickins 			si->highest_alloc = 0;
2487992fde7SHugh Dickins 		}
2495d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2507dfad418SHugh Dickins 
251c60aa176SHugh Dickins 		/*
252c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
253c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
254c60aa176SHugh Dickins 		 * But if seek is cheap, search from our current position, so
255c60aa176SHugh Dickins 		 * that swap is allocated from all over the partition: if the
256c60aa176SHugh Dickins 		 * Flash Translation Layer only remaps within limited zones,
257c60aa176SHugh Dickins 		 * we don't want to wear out the first zone too quickly.
258c60aa176SHugh Dickins 		 */
259c60aa176SHugh Dickins 		if (!(si->flags & SWP_SOLIDSTATE))
260c60aa176SHugh Dickins 			scan_base = offset = si->lowest_bit;
2617dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
2627dfad418SHugh Dickins 
2637dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
2647dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
2651da177e4SLinus Torvalds 			if (si->swap_map[offset])
2667dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
2677dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
2685d337b91SHugh Dickins 				spin_lock(&swap_lock);
269ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
270ebebbbe9SHugh Dickins 				si->cluster_next = offset;
271ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
2727992fde7SHugh Dickins 				found_free_cluster = 1;
273ebebbbe9SHugh Dickins 				goto checks;
2747dfad418SHugh Dickins 			}
275048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
276048c27fdSHugh Dickins 				cond_resched();
277048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
278048c27fdSHugh Dickins 			}
2797dfad418SHugh Dickins 		}
280ebebbbe9SHugh Dickins 
281ebebbbe9SHugh Dickins 		offset = si->lowest_bit;
282c60aa176SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
283c60aa176SHugh Dickins 
284c60aa176SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
285c60aa176SHugh Dickins 		for (; last_in_cluster < scan_base; offset++) {
286c60aa176SHugh Dickins 			if (si->swap_map[offset])
287c60aa176SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
288c60aa176SHugh Dickins 			else if (offset == last_in_cluster) {
289c60aa176SHugh Dickins 				spin_lock(&swap_lock);
290c60aa176SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
291c60aa176SHugh Dickins 				si->cluster_next = offset;
292c60aa176SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
293c60aa176SHugh Dickins 				found_free_cluster = 1;
294c60aa176SHugh Dickins 				goto checks;
295c60aa176SHugh Dickins 			}
296c60aa176SHugh Dickins 			if (unlikely(--latency_ration < 0)) {
297c60aa176SHugh Dickins 				cond_resched();
298c60aa176SHugh Dickins 				latency_ration = LATENCY_LIMIT;
299c60aa176SHugh Dickins 			}
300c60aa176SHugh Dickins 		}
301c60aa176SHugh Dickins 
302c60aa176SHugh Dickins 		offset = scan_base;
3035d337b91SHugh Dickins 		spin_lock(&swap_lock);
304ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
3057992fde7SHugh Dickins 		si->lowest_alloc = 0;
3067dfad418SHugh Dickins 	}
3077dfad418SHugh Dickins 
308ebebbbe9SHugh Dickins checks:
309ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
31052b7efdbSHugh Dickins 		goto no_page;
3117dfad418SHugh Dickins 	if (!si->highest_bit)
3127dfad418SHugh Dickins 		goto no_page;
313ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
314c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
315c9e44410SKAMEZAWA Hiroyuki 
316c9e44410SKAMEZAWA Hiroyuki 	/* reuse swap entry of cache-only swap if not busy. */
317c9e44410SKAMEZAWA Hiroyuki 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
318c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
319c9e44410SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
320c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
321c9e44410SKAMEZAWA Hiroyuki 		spin_lock(&swap_lock);
322c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
323c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
324c9e44410SKAMEZAWA Hiroyuki 			goto checks;
325c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
326c9e44410SKAMEZAWA Hiroyuki 	}
327c9e44410SKAMEZAWA Hiroyuki 
328ebebbbe9SHugh Dickins 	if (si->swap_map[offset])
329ebebbbe9SHugh Dickins 		goto scan;
330ebebbbe9SHugh Dickins 
33152b7efdbSHugh Dickins 	if (offset == si->lowest_bit)
3321da177e4SLinus Torvalds 		si->lowest_bit++;
3331da177e4SLinus Torvalds 	if (offset == si->highest_bit)
3341da177e4SLinus Torvalds 		si->highest_bit--;
3357dfad418SHugh Dickins 	si->inuse_pages++;
3367dfad418SHugh Dickins 	if (si->inuse_pages == si->pages) {
3371da177e4SLinus Torvalds 		si->lowest_bit = si->max;
3381da177e4SLinus Torvalds 		si->highest_bit = 0;
3391da177e4SLinus Torvalds 	}
340253d553bSHugh Dickins 	si->swap_map[offset] = usage;
3411da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
34252b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
3437992fde7SHugh Dickins 
3447992fde7SHugh Dickins 	if (si->lowest_alloc) {
3457992fde7SHugh Dickins 		/*
3467992fde7SHugh Dickins 		 * Only set when SWP_DISCARDABLE, and there's a scan
3477992fde7SHugh Dickins 		 * for a free cluster in progress or just completed.
3487992fde7SHugh Dickins 		 */
3497992fde7SHugh Dickins 		if (found_free_cluster) {
3507992fde7SHugh Dickins 			/*
3517992fde7SHugh Dickins 			 * To optimize wear-levelling, discard the
3527992fde7SHugh Dickins 			 * old data of the cluster, taking care not to
3537992fde7SHugh Dickins 			 * discard any of its pages that have already
3547992fde7SHugh Dickins 			 * been allocated by racing tasks (offset has
3557992fde7SHugh Dickins 			 * already stepped over any at the beginning).
3567992fde7SHugh Dickins 			 */
3577992fde7SHugh Dickins 			if (offset < si->highest_alloc &&
3587992fde7SHugh Dickins 			    si->lowest_alloc <= last_in_cluster)
3597992fde7SHugh Dickins 				last_in_cluster = si->lowest_alloc - 1;
3607992fde7SHugh Dickins 			si->flags |= SWP_DISCARDING;
3617992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3627992fde7SHugh Dickins 
3637992fde7SHugh Dickins 			if (offset < last_in_cluster)
3647992fde7SHugh Dickins 				discard_swap_cluster(si, offset,
3657992fde7SHugh Dickins 					last_in_cluster - offset + 1);
3667992fde7SHugh Dickins 
3677992fde7SHugh Dickins 			spin_lock(&swap_lock);
3687992fde7SHugh Dickins 			si->lowest_alloc = 0;
3697992fde7SHugh Dickins 			si->flags &= ~SWP_DISCARDING;
3707992fde7SHugh Dickins 
3717992fde7SHugh Dickins 			smp_mb();	/* wake_up_bit advises this */
3727992fde7SHugh Dickins 			wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
3737992fde7SHugh Dickins 
3747992fde7SHugh Dickins 		} else if (si->flags & SWP_DISCARDING) {
3757992fde7SHugh Dickins 			/*
3767992fde7SHugh Dickins 			 * Delay using pages allocated by racing tasks
3777992fde7SHugh Dickins 			 * until the whole discard has been issued. We
3787992fde7SHugh Dickins 			 * could defer that delay until swap_writepage,
3797992fde7SHugh Dickins 			 * but it's easier to keep this self-contained.
3807992fde7SHugh Dickins 			 */
3817992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3827992fde7SHugh Dickins 			wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
3837992fde7SHugh Dickins 				wait_for_discard, TASK_UNINTERRUPTIBLE);
3847992fde7SHugh Dickins 			spin_lock(&swap_lock);
3857992fde7SHugh Dickins 		} else {
3867992fde7SHugh Dickins 			/*
3877992fde7SHugh Dickins 			 * Note pages allocated by racing tasks while
3887992fde7SHugh Dickins 			 * scan for a free cluster is in progress, so
3897992fde7SHugh Dickins 			 * that its final discard can exclude them.
3907992fde7SHugh Dickins 			 */
3917992fde7SHugh Dickins 			if (offset < si->lowest_alloc)
3927992fde7SHugh Dickins 				si->lowest_alloc = offset;
3937992fde7SHugh Dickins 			if (offset > si->highest_alloc)
3947992fde7SHugh Dickins 				si->highest_alloc = offset;
3957992fde7SHugh Dickins 		}
3967992fde7SHugh Dickins 	}
3971da177e4SLinus Torvalds 	return offset;
3987dfad418SHugh Dickins 
399ebebbbe9SHugh Dickins scan:
4005d337b91SHugh Dickins 	spin_unlock(&swap_lock);
4017dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
40252b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
4035d337b91SHugh Dickins 			spin_lock(&swap_lock);
40452b7efdbSHugh Dickins 			goto checks;
4057dfad418SHugh Dickins 		}
406c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
407c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
408c9e44410SKAMEZAWA Hiroyuki 			goto checks;
409c9e44410SKAMEZAWA Hiroyuki 		}
410048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
411048c27fdSHugh Dickins 			cond_resched();
412048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
413048c27fdSHugh Dickins 		}
41452b7efdbSHugh Dickins 	}
415c60aa176SHugh Dickins 	offset = si->lowest_bit;
416c60aa176SHugh Dickins 	while (++offset < scan_base) {
417c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
4185d337b91SHugh Dickins 			spin_lock(&swap_lock);
419ebebbbe9SHugh Dickins 			goto checks;
420c60aa176SHugh Dickins 		}
421c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
422c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
423c9e44410SKAMEZAWA Hiroyuki 			goto checks;
424c9e44410SKAMEZAWA Hiroyuki 		}
425c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
426c60aa176SHugh Dickins 			cond_resched();
427c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
428c60aa176SHugh Dickins 		}
429c60aa176SHugh Dickins 	}
430c60aa176SHugh Dickins 	spin_lock(&swap_lock);
4317dfad418SHugh Dickins 
4327dfad418SHugh Dickins no_page:
43352b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
4341da177e4SLinus Torvalds 	return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
4381da177e4SLinus Torvalds {
439fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
440fb4f88dcSHugh Dickins 	pgoff_t offset;
441fb4f88dcSHugh Dickins 	int type, next;
442fb4f88dcSHugh Dickins 	int wrapped = 0;
4431da177e4SLinus Torvalds 
4445d337b91SHugh Dickins 	spin_lock(&swap_lock);
4451da177e4SLinus Torvalds 	if (nr_swap_pages <= 0)
446fb4f88dcSHugh Dickins 		goto noswap;
447fb4f88dcSHugh Dickins 	nr_swap_pages--;
4481da177e4SLinus Torvalds 
449fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
450efa90a98SHugh Dickins 		si = swap_info[type];
451fb4f88dcSHugh Dickins 		next = si->next;
452fb4f88dcSHugh Dickins 		if (next < 0 ||
453efa90a98SHugh Dickins 		    (!wrapped && si->prio != swap_info[next]->prio)) {
454fb4f88dcSHugh Dickins 			next = swap_list.head;
455fb4f88dcSHugh Dickins 			wrapped++;
4561da177e4SLinus Torvalds 		}
457fb4f88dcSHugh Dickins 
458fb4f88dcSHugh Dickins 		if (!si->highest_bit)
459fb4f88dcSHugh Dickins 			continue;
460fb4f88dcSHugh Dickins 		if (!(si->flags & SWP_WRITEOK))
461fb4f88dcSHugh Dickins 			continue;
462fb4f88dcSHugh Dickins 
463fb4f88dcSHugh Dickins 		swap_list.next = next;
464355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry for cache */
465253d553bSHugh Dickins 		offset = scan_swap_map(si, SWAP_HAS_CACHE);
4665d337b91SHugh Dickins 		if (offset) {
4675d337b91SHugh Dickins 			spin_unlock(&swap_lock);
468fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
4695d337b91SHugh Dickins 		}
470fb4f88dcSHugh Dickins 		next = swap_list.next;
471fb4f88dcSHugh Dickins 	}
472fb4f88dcSHugh Dickins 
473fb4f88dcSHugh Dickins 	nr_swap_pages++;
474fb4f88dcSHugh Dickins noswap:
4755d337b91SHugh Dickins 	spin_unlock(&swap_lock);
476fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
479355cfa73SKAMEZAWA Hiroyuki /* The only caller of this function is now susupend routine */
4803a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type)
4813a291a20SRafael J. Wysocki {
4823a291a20SRafael J. Wysocki 	struct swap_info_struct *si;
4833a291a20SRafael J. Wysocki 	pgoff_t offset;
4843a291a20SRafael J. Wysocki 
4853a291a20SRafael J. Wysocki 	spin_lock(&swap_lock);
486efa90a98SHugh Dickins 	si = swap_info[type];
487efa90a98SHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
4883a291a20SRafael J. Wysocki 		nr_swap_pages--;
489355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry, not cache */
490253d553bSHugh Dickins 		offset = scan_swap_map(si, 1);
4913a291a20SRafael J. Wysocki 		if (offset) {
4923a291a20SRafael J. Wysocki 			spin_unlock(&swap_lock);
4933a291a20SRafael J. Wysocki 			return swp_entry(type, offset);
4943a291a20SRafael J. Wysocki 		}
4953a291a20SRafael J. Wysocki 		nr_swap_pages++;
4963a291a20SRafael J. Wysocki 	}
4973a291a20SRafael J. Wysocki 	spin_unlock(&swap_lock);
4983a291a20SRafael J. Wysocki 	return (swp_entry_t) {0};
4993a291a20SRafael J. Wysocki }
5003a291a20SRafael J. Wysocki 
5011da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	struct swap_info_struct *p;
5041da177e4SLinus Torvalds 	unsigned long offset, type;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	if (!entry.val)
5071da177e4SLinus Torvalds 		goto out;
5081da177e4SLinus Torvalds 	type = swp_type(entry);
5091da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
5101da177e4SLinus Torvalds 		goto bad_nofile;
511efa90a98SHugh Dickins 	p = swap_info[type];
5121da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
5131da177e4SLinus Torvalds 		goto bad_device;
5141da177e4SLinus Torvalds 	offset = swp_offset(entry);
5151da177e4SLinus Torvalds 	if (offset >= p->max)
5161da177e4SLinus Torvalds 		goto bad_offset;
5171da177e4SLinus Torvalds 	if (!p->swap_map[offset])
5181da177e4SLinus Torvalds 		goto bad_free;
5195d337b91SHugh Dickins 	spin_lock(&swap_lock);
5201da177e4SLinus Torvalds 	return p;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds bad_free:
5231da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
5241da177e4SLinus Torvalds 	goto out;
5251da177e4SLinus Torvalds bad_offset:
5261da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
5271da177e4SLinus Torvalds 	goto out;
5281da177e4SLinus Torvalds bad_device:
5291da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
5301da177e4SLinus Torvalds 	goto out;
5311da177e4SLinus Torvalds bad_nofile:
5321da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
5331da177e4SLinus Torvalds out:
5341da177e4SLinus Torvalds 	return NULL;
5351da177e4SLinus Torvalds }
5361da177e4SLinus Torvalds 
5378d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p,
5388d69aaeeSHugh Dickins 				     swp_entry_t entry, unsigned char usage)
5391da177e4SLinus Torvalds {
540253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
5418d69aaeeSHugh Dickins 	unsigned char count;
5428d69aaeeSHugh Dickins 	unsigned char has_cache;
5431da177e4SLinus Torvalds 
544355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
545253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
546253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
547253d553bSHugh Dickins 
548253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
549253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
550253d553bSHugh Dickins 		has_cache = 0;
551*570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
552*570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
553*570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
554*570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
555*570a335bSHugh Dickins 			else
556*570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
557*570a335bSHugh Dickins 		} else
558253d553bSHugh Dickins 			count--;
559*570a335bSHugh Dickins 	}
560253d553bSHugh Dickins 
561253d553bSHugh Dickins 	if (!count)
562253d553bSHugh Dickins 		mem_cgroup_uncharge_swap(entry);
563253d553bSHugh Dickins 
564253d553bSHugh Dickins 	usage = count | has_cache;
565253d553bSHugh Dickins 	p->swap_map[offset] = usage;
566253d553bSHugh Dickins 
567355cfa73SKAMEZAWA Hiroyuki 	/* free if no reference */
568253d553bSHugh Dickins 	if (!usage) {
5691da177e4SLinus Torvalds 		if (offset < p->lowest_bit)
5701da177e4SLinus Torvalds 			p->lowest_bit = offset;
5711da177e4SLinus Torvalds 		if (offset > p->highest_bit)
5721da177e4SLinus Torvalds 			p->highest_bit = offset;
573efa90a98SHugh Dickins 		if (swap_list.next >= 0 &&
574efa90a98SHugh Dickins 		    p->prio > swap_info[swap_list.next]->prio)
575efa90a98SHugh Dickins 			swap_list.next = p->type;
5761da177e4SLinus Torvalds 		nr_swap_pages++;
5771da177e4SLinus Torvalds 		p->inuse_pages--;
5781da177e4SLinus Torvalds 	}
579253d553bSHugh Dickins 
580253d553bSHugh Dickins 	return usage;
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
5841da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
5851da177e4SLinus Torvalds  * is still around or has not been recycled.
5861da177e4SLinus Torvalds  */
5871da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds 	struct swap_info_struct *p;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	p = swap_info_get(entry);
5921da177e4SLinus Torvalds 	if (p) {
593253d553bSHugh Dickins 		swap_entry_free(p, entry, 1);
5945d337b91SHugh Dickins 		spin_unlock(&swap_lock);
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds /*
599cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
600cb4b86baSKAMEZAWA Hiroyuki  */
601cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page)
602cb4b86baSKAMEZAWA Hiroyuki {
603355cfa73SKAMEZAWA Hiroyuki 	struct swap_info_struct *p;
6048d69aaeeSHugh Dickins 	unsigned char count;
605355cfa73SKAMEZAWA Hiroyuki 
606355cfa73SKAMEZAWA Hiroyuki 	p = swap_info_get(entry);
607355cfa73SKAMEZAWA Hiroyuki 	if (p) {
608253d553bSHugh Dickins 		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
609253d553bSHugh Dickins 		if (page)
610253d553bSHugh Dickins 			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
611355cfa73SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
612355cfa73SKAMEZAWA Hiroyuki 	}
613cb4b86baSKAMEZAWA Hiroyuki }
614cb4b86baSKAMEZAWA Hiroyuki 
615cb4b86baSKAMEZAWA Hiroyuki /*
616c475a8abSHugh Dickins  * How many references to page are currently swapped out?
617*570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
618*570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
6191da177e4SLinus Torvalds  */
620c475a8abSHugh Dickins static inline int page_swapcount(struct page *page)
6211da177e4SLinus Torvalds {
622c475a8abSHugh Dickins 	int count = 0;
6231da177e4SLinus Torvalds 	struct swap_info_struct *p;
6241da177e4SLinus Torvalds 	swp_entry_t entry;
6251da177e4SLinus Torvalds 
6264c21e2f2SHugh Dickins 	entry.val = page_private(page);
6271da177e4SLinus Torvalds 	p = swap_info_get(entry);
6281da177e4SLinus Torvalds 	if (p) {
629355cfa73SKAMEZAWA Hiroyuki 		count = swap_count(p->swap_map[swp_offset(entry)]);
6305d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6311da177e4SLinus Torvalds 	}
632c475a8abSHugh Dickins 	return count;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds /*
6367b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
6377b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
6387b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
6397b1fe597SHugh Dickins  * later would only waste time away from clustering.
6401da177e4SLinus Torvalds  */
6417b1fe597SHugh Dickins int reuse_swap_page(struct page *page)
6421da177e4SLinus Torvalds {
643c475a8abSHugh Dickins 	int count;
6441da177e4SLinus Torvalds 
64551726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
646c475a8abSHugh Dickins 	count = page_mapcount(page);
6477b1fe597SHugh Dickins 	if (count <= 1 && PageSwapCache(page)) {
648c475a8abSHugh Dickins 		count += page_swapcount(page);
6497b1fe597SHugh Dickins 		if (count == 1 && !PageWriteback(page)) {
6507b1fe597SHugh Dickins 			delete_from_swap_cache(page);
6517b1fe597SHugh Dickins 			SetPageDirty(page);
6527b1fe597SHugh Dickins 		}
6537b1fe597SHugh Dickins 	}
654c475a8abSHugh Dickins 	return count == 1;
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds /*
658a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
659a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
6601da177e4SLinus Torvalds  */
661a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
6621da177e4SLinus Torvalds {
66351726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	if (!PageSwapCache(page))
6661da177e4SLinus Torvalds 		return 0;
6671da177e4SLinus Torvalds 	if (PageWriteback(page))
6681da177e4SLinus Torvalds 		return 0;
669a2c43eedSHugh Dickins 	if (page_swapcount(page))
6701da177e4SLinus Torvalds 		return 0;
6711da177e4SLinus Torvalds 
672a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
6731da177e4SLinus Torvalds 	SetPageDirty(page);
674a2c43eedSHugh Dickins 	return 1;
67568a22394SRik van Riel }
67668a22394SRik van Riel 
67768a22394SRik van Riel /*
6781da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
6791da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
6801da177e4SLinus Torvalds  */
6812509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct swap_info_struct *p;
6841da177e4SLinus Torvalds 	struct page *page = NULL;
6851da177e4SLinus Torvalds 
686a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
6872509ef26SHugh Dickins 		return 1;
6880697212aSChristoph Lameter 
6891da177e4SLinus Torvalds 	p = swap_info_get(entry);
6901da177e4SLinus Torvalds 	if (p) {
691253d553bSHugh Dickins 		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
69293fac704SNick Piggin 			page = find_get_page(&swapper_space, entry.val);
6938413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
69493fac704SNick Piggin 				page_cache_release(page);
69593fac704SNick Piggin 				page = NULL;
69693fac704SNick Piggin 			}
69793fac704SNick Piggin 		}
6985d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 	if (page) {
701a2c43eedSHugh Dickins 		/*
702a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
703a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
704a2c43eedSHugh Dickins 		 */
70593fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
706a2c43eedSHugh Dickins 				(!page_mapped(page) || vm_swap_full())) {
7071da177e4SLinus Torvalds 			delete_from_swap_cache(page);
7081da177e4SLinus Torvalds 			SetPageDirty(page);
7091da177e4SLinus Torvalds 		}
7101da177e4SLinus Torvalds 		unlock_page(page);
7111da177e4SLinus Torvalds 		page_cache_release(page);
7121da177e4SLinus Torvalds 	}
7132509ef26SHugh Dickins 	return p != NULL;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
716b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
717f577eb30SRafael J. Wysocki /*
718915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
719f577eb30SRafael J. Wysocki  *
720915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
721915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
722915bae9eSRafael J. Wysocki  *
723915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
724f577eb30SRafael J. Wysocki  */
7257bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
726f577eb30SRafael J. Wysocki {
727915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
728efa90a98SHugh Dickins 	int type;
729f577eb30SRafael J. Wysocki 
730915bae9eSRafael J. Wysocki 	if (device)
731915bae9eSRafael J. Wysocki 		bdev = bdget(device);
732915bae9eSRafael J. Wysocki 
733f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
734efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
735efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
736f577eb30SRafael J. Wysocki 
737915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
738f577eb30SRafael J. Wysocki 			continue;
739b6b5bce3SRafael J. Wysocki 
740915bae9eSRafael J. Wysocki 		if (!bdev) {
7417bf23687SRafael J. Wysocki 			if (bdev_p)
742dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
7437bf23687SRafael J. Wysocki 
7446e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
745efa90a98SHugh Dickins 			return type;
7466e1819d6SRafael J. Wysocki 		}
747915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
7489625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
749915bae9eSRafael J. Wysocki 
750915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
7517bf23687SRafael J. Wysocki 				if (bdev_p)
752dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
7537bf23687SRafael J. Wysocki 
754f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
755915bae9eSRafael J. Wysocki 				bdput(bdev);
756efa90a98SHugh Dickins 				return type;
757f577eb30SRafael J. Wysocki 			}
758f577eb30SRafael J. Wysocki 		}
759915bae9eSRafael J. Wysocki 	}
760f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
761915bae9eSRafael J. Wysocki 	if (bdev)
762915bae9eSRafael J. Wysocki 		bdput(bdev);
763915bae9eSRafael J. Wysocki 
764f577eb30SRafael J. Wysocki 	return -ENODEV;
765f577eb30SRafael J. Wysocki }
766f577eb30SRafael J. Wysocki 
767f577eb30SRafael J. Wysocki /*
76873c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
76973c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
77073c34b6aSHugh Dickins  */
77173c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
77273c34b6aSHugh Dickins {
77373c34b6aSHugh Dickins 	struct block_device *bdev;
77473c34b6aSHugh Dickins 
77573c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
77673c34b6aSHugh Dickins 		return 0;
77773c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
77873c34b6aSHugh Dickins 		return 0;
77973c34b6aSHugh Dickins 	return map_swap_page(swp_entry(type, offset), &bdev);
78073c34b6aSHugh Dickins }
78173c34b6aSHugh Dickins 
78273c34b6aSHugh Dickins /*
783f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
784f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
785f577eb30SRafael J. Wysocki  *
786f577eb30SRafael J. Wysocki  * This is needed for software suspend
787f577eb30SRafael J. Wysocki  */
788f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
789f577eb30SRafael J. Wysocki {
790f577eb30SRafael J. Wysocki 	unsigned int n = 0;
791f577eb30SRafael J. Wysocki 
792f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
793efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
794efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
795efa90a98SHugh Dickins 
796efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
797efa90a98SHugh Dickins 			n = sis->pages;
798f577eb30SRafael J. Wysocki 			if (free)
799efa90a98SHugh Dickins 				n -= sis->inuse_pages;
800efa90a98SHugh Dickins 		}
801f577eb30SRafael J. Wysocki 	}
802f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
803f577eb30SRafael J. Wysocki 	return n;
804f577eb30SRafael J. Wysocki }
80573c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
806f577eb30SRafael J. Wysocki 
8071da177e4SLinus Torvalds /*
80872866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
80972866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
81072866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
8111da177e4SLinus Torvalds  */
812044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
8131da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
8141da177e4SLinus Torvalds {
8157a81b88cSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr = NULL;
816044d66c1SHugh Dickins 	spinlock_t *ptl;
817044d66c1SHugh Dickins 	pte_t *pte;
818044d66c1SHugh Dickins 	int ret = 1;
819044d66c1SHugh Dickins 
82085d9fc89SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
821044d66c1SHugh Dickins 		ret = -ENOMEM;
82285d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
82385d9fc89SKAMEZAWA Hiroyuki 	}
824044d66c1SHugh Dickins 
825044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
826044d66c1SHugh Dickins 	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
827044d66c1SHugh Dickins 		if (ret > 0)
8287a81b88cSKAMEZAWA Hiroyuki 			mem_cgroup_cancel_charge_swapin(ptr);
829044d66c1SHugh Dickins 		ret = 0;
830044d66c1SHugh Dickins 		goto out;
831044d66c1SHugh Dickins 	}
8328a9f3ccdSBalbir Singh 
8334294621fSHugh Dickins 	inc_mm_counter(vma->vm_mm, anon_rss);
8341da177e4SLinus Torvalds 	get_page(page);
8351da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
8361da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
8371da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, addr);
8387a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
8391da177e4SLinus Torvalds 	swap_free(entry);
8401da177e4SLinus Torvalds 	/*
8411da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
8421da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
8431da177e4SLinus Torvalds 	 */
8441da177e4SLinus Torvalds 	activate_page(page);
845044d66c1SHugh Dickins out:
846044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
84785d9fc89SKAMEZAWA Hiroyuki out_nolock:
848044d66c1SHugh Dickins 	return ret;
8491da177e4SLinus Torvalds }
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
8521da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
8531da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
8541da177e4SLinus Torvalds {
8551da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
856705e87c0SHugh Dickins 	pte_t *pte;
8578a9f3ccdSBalbir Singh 	int ret = 0;
8581da177e4SLinus Torvalds 
859044d66c1SHugh Dickins 	/*
860044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
861044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
862044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
863044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
864044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
865044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
866044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
867044d66c1SHugh Dickins 	 */
868044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
8691da177e4SLinus Torvalds 	do {
8701da177e4SLinus Torvalds 		/*
8711da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
8721da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
8731da177e4SLinus Torvalds 		 */
8741da177e4SLinus Torvalds 		if (unlikely(pte_same(*pte, swp_pte))) {
875044d66c1SHugh Dickins 			pte_unmap(pte);
876044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
877044d66c1SHugh Dickins 			if (ret)
878044d66c1SHugh Dickins 				goto out;
879044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
8801da177e4SLinus Torvalds 		}
8811da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
882044d66c1SHugh Dickins 	pte_unmap(pte - 1);
883044d66c1SHugh Dickins out:
8848a9f3ccdSBalbir Singh 	return ret;
8851da177e4SLinus Torvalds }
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
8881da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
8891da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
8901da177e4SLinus Torvalds {
8911da177e4SLinus Torvalds 	pmd_t *pmd;
8921da177e4SLinus Torvalds 	unsigned long next;
8938a9f3ccdSBalbir Singh 	int ret;
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
8961da177e4SLinus Torvalds 	do {
8971da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
8981da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
8991da177e4SLinus Torvalds 			continue;
9008a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
9018a9f3ccdSBalbir Singh 		if (ret)
9028a9f3ccdSBalbir Singh 			return ret;
9031da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
9041da177e4SLinus Torvalds 	return 0;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
9081da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9091da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	pud_t *pud;
9121da177e4SLinus Torvalds 	unsigned long next;
9138a9f3ccdSBalbir Singh 	int ret;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
9161da177e4SLinus Torvalds 	do {
9171da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9181da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
9191da177e4SLinus Torvalds 			continue;
9208a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
9218a9f3ccdSBalbir Singh 		if (ret)
9228a9f3ccdSBalbir Singh 			return ret;
9231da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
9241da177e4SLinus Torvalds 	return 0;
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
9281da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9291da177e4SLinus Torvalds {
9301da177e4SLinus Torvalds 	pgd_t *pgd;
9311da177e4SLinus Torvalds 	unsigned long addr, end, next;
9328a9f3ccdSBalbir Singh 	int ret;
9331da177e4SLinus Torvalds 
9341da177e4SLinus Torvalds 	if (page->mapping) {
9351da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
9361da177e4SLinus Torvalds 		if (addr == -EFAULT)
9371da177e4SLinus Torvalds 			return 0;
9381da177e4SLinus Torvalds 		else
9391da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
9401da177e4SLinus Torvalds 	} else {
9411da177e4SLinus Torvalds 		addr = vma->vm_start;
9421da177e4SLinus Torvalds 		end = vma->vm_end;
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
9461da177e4SLinus Torvalds 	do {
9471da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
9481da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
9491da177e4SLinus Torvalds 			continue;
9508a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
9518a9f3ccdSBalbir Singh 		if (ret)
9528a9f3ccdSBalbir Singh 			return ret;
9531da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
9541da177e4SLinus Torvalds 	return 0;
9551da177e4SLinus Torvalds }
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
9581da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	struct vm_area_struct *vma;
9618a9f3ccdSBalbir Singh 	int ret = 0;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
9641da177e4SLinus Torvalds 		/*
9657d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
9667d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
9671da177e4SLinus Torvalds 		 */
968c475a8abSHugh Dickins 		activate_page(page);
9691da177e4SLinus Torvalds 		unlock_page(page);
9701da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
9711da177e4SLinus Torvalds 		lock_page(page);
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
9748a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
9751da177e4SLinus Torvalds 			break;
9761da177e4SLinus Torvalds 	}
9771da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
9788a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds /*
9821da177e4SLinus Torvalds  * Scan swap_map from current position to next entry still in use.
9831da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
9841da177e4SLinus Torvalds  */
9856eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
9866eb396dcSHugh Dickins 					unsigned int prev)
9871da177e4SLinus Torvalds {
9886eb396dcSHugh Dickins 	unsigned int max = si->max;
9896eb396dcSHugh Dickins 	unsigned int i = prev;
9908d69aaeeSHugh Dickins 	unsigned char count;
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	/*
9935d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
9941da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
9951da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
9965d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
9971da177e4SLinus Torvalds 	 */
9981da177e4SLinus Torvalds 	for (;;) {
9991da177e4SLinus Torvalds 		if (++i >= max) {
10001da177e4SLinus Torvalds 			if (!prev) {
10011da177e4SLinus Torvalds 				i = 0;
10021da177e4SLinus Torvalds 				break;
10031da177e4SLinus Torvalds 			}
10041da177e4SLinus Torvalds 			/*
10051da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
10061da177e4SLinus Torvalds 			 * loop back to start and recheck there.
10071da177e4SLinus Torvalds 			 */
10081da177e4SLinus Torvalds 			max = prev + 1;
10091da177e4SLinus Torvalds 			prev = 0;
10101da177e4SLinus Torvalds 			i = 1;
10111da177e4SLinus Torvalds 		}
10121da177e4SLinus Torvalds 		count = si->swap_map[i];
1013355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
10141da177e4SLinus Torvalds 			break;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	return i;
10171da177e4SLinus Torvalds }
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds /*
10201da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
10211da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
10221da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
10231da177e4SLinus Torvalds  */
10241da177e4SLinus Torvalds static int try_to_unuse(unsigned int type)
10251da177e4SLinus Torvalds {
1026efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
10271da177e4SLinus Torvalds 	struct mm_struct *start_mm;
10288d69aaeeSHugh Dickins 	unsigned char *swap_map;
10298d69aaeeSHugh Dickins 	unsigned char swcount;
10301da177e4SLinus Torvalds 	struct page *page;
10311da177e4SLinus Torvalds 	swp_entry_t entry;
10326eb396dcSHugh Dickins 	unsigned int i = 0;
10331da177e4SLinus Torvalds 	int retval = 0;
10341da177e4SLinus Torvalds 	int shmem;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/*
10371da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
10381da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
10391da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
10401da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
10411da177e4SLinus Torvalds 	 *
10421da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
10431da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
10441da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
10451da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
10461da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
10471da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
1048*570a335bSHugh Dickins 	 * that.
10491da177e4SLinus Torvalds 	 */
10501da177e4SLinus Torvalds 	start_mm = &init_mm;
10511da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	/*
10541da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
10551da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
10561da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
10571da177e4SLinus Torvalds 	 */
10581da177e4SLinus Torvalds 	while ((i = find_next_to_unuse(si, i)) != 0) {
10591da177e4SLinus Torvalds 		if (signal_pending(current)) {
10601da177e4SLinus Torvalds 			retval = -EINTR;
10611da177e4SLinus Torvalds 			break;
10621da177e4SLinus Torvalds 		}
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 		/*
10651da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
10661da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
10671da177e4SLinus Torvalds 		 * page and read the swap into it.
10681da177e4SLinus Torvalds 		 */
10691da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
10701da177e4SLinus Torvalds 		entry = swp_entry(type, i);
107102098feaSHugh Dickins 		page = read_swap_cache_async(entry,
107202098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
10731da177e4SLinus Torvalds 		if (!page) {
10741da177e4SLinus Torvalds 			/*
10751da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
10761da177e4SLinus Torvalds 			 * has been freed independently, and will not be
10771da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
10781da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
10791da177e4SLinus Torvalds 			 */
10801da177e4SLinus Torvalds 			if (!*swap_map)
10811da177e4SLinus Torvalds 				continue;
10821da177e4SLinus Torvalds 			retval = -ENOMEM;
10831da177e4SLinus Torvalds 			break;
10841da177e4SLinus Torvalds 		}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 		/*
10871da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
10881da177e4SLinus Torvalds 		 */
10891da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
10901da177e4SLinus Torvalds 			mmput(start_mm);
10911da177e4SLinus Torvalds 			start_mm = &init_mm;
10921da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
10931da177e4SLinus Torvalds 		}
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 		/*
10961da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
10971da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
10981da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
10991da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
11001da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
11011da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
11021da177e4SLinus Torvalds 		 */
11031da177e4SLinus Torvalds 		wait_on_page_locked(page);
11041da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11051da177e4SLinus Torvalds 		lock_page(page);
11061da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 		/*
11091da177e4SLinus Torvalds 		 * Remove all references to entry.
11101da177e4SLinus Torvalds 		 * Whenever we reach init_mm, there's no address space
11111da177e4SLinus Torvalds 		 * to search, but use it as a reminder to search shmem.
11121da177e4SLinus Torvalds 		 */
11131da177e4SLinus Torvalds 		shmem = 0;
11141da177e4SLinus Torvalds 		swcount = *swap_map;
1115355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(swcount)) {
11161da177e4SLinus Torvalds 			if (start_mm == &init_mm)
11171da177e4SLinus Torvalds 				shmem = shmem_unuse(entry, page);
11181da177e4SLinus Torvalds 			else
11191da177e4SLinus Torvalds 				retval = unuse_mm(start_mm, entry, page);
11201da177e4SLinus Torvalds 		}
1121355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
11221da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
11231da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
11241da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
11251da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
11261da177e4SLinus Torvalds 			struct mm_struct *mm;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
11291da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
11301da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
1131355cfa73SKAMEZAWA Hiroyuki 			while (swap_count(*swap_map) && !retval && !shmem &&
11321da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
11331da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
113470af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
11351da177e4SLinus Torvalds 					continue;
11361da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
11371da177e4SLinus Torvalds 				mmput(prev_mm);
11381da177e4SLinus Torvalds 				prev_mm = mm;
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds 				cond_resched();
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 				swcount = *swap_map;
1143355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
11441da177e4SLinus Torvalds 					;
11451da177e4SLinus Torvalds 				else if (mm == &init_mm) {
11461da177e4SLinus Torvalds 					set_start_mm = 1;
11471da177e4SLinus Torvalds 					shmem = shmem_unuse(entry, page);
11481da177e4SLinus Torvalds 				} else
11491da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
1150355cfa73SKAMEZAWA Hiroyuki 
115132c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
11521da177e4SLinus Torvalds 					mmput(new_start_mm);
11531da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
11541da177e4SLinus Torvalds 					new_start_mm = mm;
11551da177e4SLinus Torvalds 					set_start_mm = 0;
11561da177e4SLinus Torvalds 				}
11571da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
11581da177e4SLinus Torvalds 			}
11591da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
11601da177e4SLinus Torvalds 			mmput(prev_mm);
11611da177e4SLinus Torvalds 			mmput(start_mm);
11621da177e4SLinus Torvalds 			start_mm = new_start_mm;
11631da177e4SLinus Torvalds 		}
11642e0e26c7SHugh Dickins 		if (shmem) {
11652e0e26c7SHugh Dickins 			/* page has already been unlocked and released */
11662e0e26c7SHugh Dickins 			if (shmem > 0)
11672e0e26c7SHugh Dickins 				continue;
11682e0e26c7SHugh Dickins 			retval = shmem;
11692e0e26c7SHugh Dickins 			break;
11702e0e26c7SHugh Dickins 		}
11711da177e4SLinus Torvalds 		if (retval) {
11721da177e4SLinus Torvalds 			unlock_page(page);
11731da177e4SLinus Torvalds 			page_cache_release(page);
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 		}
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 		/*
11781da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
11791da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
11801da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
11811da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
11821da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
11831da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
11841da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
11851da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
11861da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
11871da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
11881da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
11891da177e4SLinus Torvalds 		 */
1190355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
1191355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
11921da177e4SLinus Torvalds 			struct writeback_control wbc = {
11931da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
11941da177e4SLinus Torvalds 			};
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
11971da177e4SLinus Torvalds 			lock_page(page);
11981da177e4SLinus Torvalds 			wait_on_page_writeback(page);
11991da177e4SLinus Torvalds 		}
120068bdc8d6SHugh Dickins 
120168bdc8d6SHugh Dickins 		/*
120268bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
120368bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
120468bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
120568bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
120668bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
120768bdc8d6SHugh Dickins 		 */
120868bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
120968bdc8d6SHugh Dickins 		    likely(page_private(page) == entry.val))
12101da177e4SLinus Torvalds 			delete_from_swap_cache(page);
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds 		/*
12131da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
12141da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
12152706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
12161da177e4SLinus Torvalds 		 */
12171da177e4SLinus Torvalds 		SetPageDirty(page);
12181da177e4SLinus Torvalds 		unlock_page(page);
12191da177e4SLinus Torvalds 		page_cache_release(page);
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 		/*
12221da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
12231da177e4SLinus Torvalds 		 * interactive performance.
12241da177e4SLinus Torvalds 		 */
12251da177e4SLinus Torvalds 		cond_resched();
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	mmput(start_mm);
12291da177e4SLinus Torvalds 	return retval;
12301da177e4SLinus Torvalds }
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds /*
12335d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
12345d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
12355d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
12361da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
12371da177e4SLinus Torvalds  */
12381da177e4SLinus Torvalds static void drain_mmlist(void)
12391da177e4SLinus Torvalds {
12401da177e4SLinus Torvalds 	struct list_head *p, *next;
1241efa90a98SHugh Dickins 	unsigned int type;
12421da177e4SLinus Torvalds 
1243efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
1244efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
12451da177e4SLinus Torvalds 			return;
12461da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
12471da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
12481da177e4SLinus Torvalds 		list_del_init(p);
12491da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
12501da177e4SLinus Torvalds }
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds /*
12531da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1254f29ad6a9SHugh Dickins  * corresponds to page offset `offset'.  Note that the type of this function
1255f29ad6a9SHugh Dickins  * is sector_t, but it returns page offset into the bdev, not sector offset.
12561da177e4SLinus Torvalds  */
1257f29ad6a9SHugh Dickins sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev)
12581da177e4SLinus Torvalds {
1259f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
1260f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
1261f29ad6a9SHugh Dickins 	struct swap_extent *se;
1262f29ad6a9SHugh Dickins 	pgoff_t offset;
1263f29ad6a9SHugh Dickins 
1264efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
1265f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
1266f29ad6a9SHugh Dickins 
1267f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
1268f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
1269f29ad6a9SHugh Dickins 	se = start_se;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	for ( ; ; ) {
12721da177e4SLinus Torvalds 		struct list_head *lh;
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 		if (se->start_page <= offset &&
12751da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
12761da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
12771da177e4SLinus Torvalds 		}
127811d31886SHugh Dickins 		lh = se->list.next;
12791da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
12801da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
12811da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds }
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds /*
12861da177e4SLinus Torvalds  * Free all of a swapdev's extent information
12871da177e4SLinus Torvalds  */
12881da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
12891da177e4SLinus Torvalds {
12909625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
12911da177e4SLinus Torvalds 		struct swap_extent *se;
12921da177e4SLinus Torvalds 
12939625a5f2SHugh Dickins 		se = list_entry(sis->first_swap_extent.list.next,
12941da177e4SLinus Torvalds 				struct swap_extent, list);
12951da177e4SLinus Torvalds 		list_del(&se->list);
12961da177e4SLinus Torvalds 		kfree(se);
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds /*
13011da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
130211d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
13031da177e4SLinus Torvalds  *
130411d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
13051da177e4SLinus Torvalds  */
13061da177e4SLinus Torvalds static int
13071da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
13081da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	struct swap_extent *se;
13111da177e4SLinus Torvalds 	struct swap_extent *new_se;
13121da177e4SLinus Torvalds 	struct list_head *lh;
13131da177e4SLinus Torvalds 
13149625a5f2SHugh Dickins 	if (start_page == 0) {
13159625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
13169625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
13179625a5f2SHugh Dickins 		se->start_page = 0;
13189625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
13199625a5f2SHugh Dickins 		se->start_block = start_block;
13209625a5f2SHugh Dickins 		return 1;
13219625a5f2SHugh Dickins 	} else {
13229625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
13231da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
132411d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
132511d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
13261da177e4SLinus Torvalds 			/* Merge it */
13271da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
13281da177e4SLinus Torvalds 			return 0;
13291da177e4SLinus Torvalds 		}
13301da177e4SLinus Torvalds 	}
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 	/*
13331da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
13341da177e4SLinus Torvalds 	 */
13351da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
13361da177e4SLinus Torvalds 	if (new_se == NULL)
13371da177e4SLinus Torvalds 		return -ENOMEM;
13381da177e4SLinus Torvalds 	new_se->start_page = start_page;
13391da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
13401da177e4SLinus Torvalds 	new_se->start_block = start_block;
13411da177e4SLinus Torvalds 
13429625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
134353092a74SHugh Dickins 	return 1;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds /*
13471da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
13481da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
13491da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
13501da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
13511da177e4SLinus Torvalds  *
13521da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
13531da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
13541da177e4SLinus Torvalds  * swap files identically.
13551da177e4SLinus Torvalds  *
13561da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
13571da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
13581da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
13591da177e4SLinus Torvalds  *
13601da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
13611da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
13621da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
13631da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
13641da177e4SLinus Torvalds  * for swapping.
13651da177e4SLinus Torvalds  *
1366b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
13671da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
13681da177e4SLinus Torvalds  * which will scribble on the fs.
13691da177e4SLinus Torvalds  *
13701da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
13711da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
13721da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
13731da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
13741da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
13751da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
13761da177e4SLinus Torvalds  */
137753092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
13781da177e4SLinus Torvalds {
13791da177e4SLinus Torvalds 	struct inode *inode;
13801da177e4SLinus Torvalds 	unsigned blocks_per_page;
13811da177e4SLinus Torvalds 	unsigned long page_no;
13821da177e4SLinus Torvalds 	unsigned blkbits;
13831da177e4SLinus Torvalds 	sector_t probe_block;
13841da177e4SLinus Torvalds 	sector_t last_block;
138553092a74SHugh Dickins 	sector_t lowest_block = -1;
138653092a74SHugh Dickins 	sector_t highest_block = 0;
138753092a74SHugh Dickins 	int nr_extents = 0;
13881da177e4SLinus Torvalds 	int ret;
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 	inode = sis->swap_file->f_mapping->host;
13911da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
13921da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
139353092a74SHugh Dickins 		*span = sis->pages;
13949625a5f2SHugh Dickins 		goto out;
13951da177e4SLinus Torvalds 	}
13961da177e4SLinus Torvalds 
13971da177e4SLinus Torvalds 	blkbits = inode->i_blkbits;
13981da177e4SLinus Torvalds 	blocks_per_page = PAGE_SIZE >> blkbits;
13991da177e4SLinus Torvalds 
14001da177e4SLinus Torvalds 	/*
14011da177e4SLinus Torvalds 	 * Map all the blocks into the extent list.  This code doesn't try
14021da177e4SLinus Torvalds 	 * to be very smart.
14031da177e4SLinus Torvalds 	 */
14041da177e4SLinus Torvalds 	probe_block = 0;
14051da177e4SLinus Torvalds 	page_no = 0;
14061da177e4SLinus Torvalds 	last_block = i_size_read(inode) >> blkbits;
14071da177e4SLinus Torvalds 	while ((probe_block + blocks_per_page) <= last_block &&
14081da177e4SLinus Torvalds 			page_no < sis->max) {
14091da177e4SLinus Torvalds 		unsigned block_in_page;
14101da177e4SLinus Torvalds 		sector_t first_block;
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds 		first_block = bmap(inode, probe_block);
14131da177e4SLinus Torvalds 		if (first_block == 0)
14141da177e4SLinus Torvalds 			goto bad_bmap;
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds 		/*
14171da177e4SLinus Torvalds 		 * It must be PAGE_SIZE aligned on-disk
14181da177e4SLinus Torvalds 		 */
14191da177e4SLinus Torvalds 		if (first_block & (blocks_per_page - 1)) {
14201da177e4SLinus Torvalds 			probe_block++;
14211da177e4SLinus Torvalds 			goto reprobe;
14221da177e4SLinus Torvalds 		}
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 		for (block_in_page = 1; block_in_page < blocks_per_page;
14251da177e4SLinus Torvalds 					block_in_page++) {
14261da177e4SLinus Torvalds 			sector_t block;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 			block = bmap(inode, probe_block + block_in_page);
14291da177e4SLinus Torvalds 			if (block == 0)
14301da177e4SLinus Torvalds 				goto bad_bmap;
14311da177e4SLinus Torvalds 			if (block != first_block + block_in_page) {
14321da177e4SLinus Torvalds 				/* Discontiguity */
14331da177e4SLinus Torvalds 				probe_block++;
14341da177e4SLinus Torvalds 				goto reprobe;
14351da177e4SLinus Torvalds 			}
14361da177e4SLinus Torvalds 		}
14371da177e4SLinus Torvalds 
143853092a74SHugh Dickins 		first_block >>= (PAGE_SHIFT - blkbits);
143953092a74SHugh Dickins 		if (page_no) {	/* exclude the header page */
144053092a74SHugh Dickins 			if (first_block < lowest_block)
144153092a74SHugh Dickins 				lowest_block = first_block;
144253092a74SHugh Dickins 			if (first_block > highest_block)
144353092a74SHugh Dickins 				highest_block = first_block;
144453092a74SHugh Dickins 		}
144553092a74SHugh Dickins 
14461da177e4SLinus Torvalds 		/*
14471da177e4SLinus Torvalds 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
14481da177e4SLinus Torvalds 		 */
144953092a74SHugh Dickins 		ret = add_swap_extent(sis, page_no, 1, first_block);
145053092a74SHugh Dickins 		if (ret < 0)
14511da177e4SLinus Torvalds 			goto out;
145253092a74SHugh Dickins 		nr_extents += ret;
14531da177e4SLinus Torvalds 		page_no++;
14541da177e4SLinus Torvalds 		probe_block += blocks_per_page;
14551da177e4SLinus Torvalds reprobe:
14561da177e4SLinus Torvalds 		continue;
14571da177e4SLinus Torvalds 	}
145853092a74SHugh Dickins 	ret = nr_extents;
145953092a74SHugh Dickins 	*span = 1 + highest_block - lowest_block;
14601da177e4SLinus Torvalds 	if (page_no == 0)
1461e2244ec2SHugh Dickins 		page_no = 1;	/* force Empty message */
14621da177e4SLinus Torvalds 	sis->max = page_no;
1463e2244ec2SHugh Dickins 	sis->pages = page_no - 1;
14641da177e4SLinus Torvalds 	sis->highest_bit = page_no - 1;
14659625a5f2SHugh Dickins out:
14669625a5f2SHugh Dickins 	return ret;
14671da177e4SLinus Torvalds bad_bmap:
14681da177e4SLinus Torvalds 	printk(KERN_ERR "swapon: swapfile has holes\n");
14691da177e4SLinus Torvalds 	ret = -EINVAL;
14709625a5f2SHugh Dickins 	goto out;
14711da177e4SLinus Torvalds }
14721da177e4SLinus Torvalds 
1473c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
14741da177e4SLinus Torvalds {
14751da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
14768d69aaeeSHugh Dickins 	unsigned char *swap_map;
14771da177e4SLinus Torvalds 	struct file *swap_file, *victim;
14781da177e4SLinus Torvalds 	struct address_space *mapping;
14791da177e4SLinus Torvalds 	struct inode *inode;
14801da177e4SLinus Torvalds 	char *pathname;
14811da177e4SLinus Torvalds 	int i, type, prev;
14821da177e4SLinus Torvalds 	int err;
14831da177e4SLinus Torvalds 
14841da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
14851da177e4SLinus Torvalds 		return -EPERM;
14861da177e4SLinus Torvalds 
14871da177e4SLinus Torvalds 	pathname = getname(specialfile);
14881da177e4SLinus Torvalds 	err = PTR_ERR(pathname);
14891da177e4SLinus Torvalds 	if (IS_ERR(pathname))
14901da177e4SLinus Torvalds 		goto out;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
14931da177e4SLinus Torvalds 	putname(pathname);
14941da177e4SLinus Torvalds 	err = PTR_ERR(victim);
14951da177e4SLinus Torvalds 	if (IS_ERR(victim))
14961da177e4SLinus Torvalds 		goto out;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	mapping = victim->f_mapping;
14991da177e4SLinus Torvalds 	prev = -1;
15005d337b91SHugh Dickins 	spin_lock(&swap_lock);
1501efa90a98SHugh Dickins 	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1502efa90a98SHugh Dickins 		p = swap_info[type];
150322c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
15041da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
15051da177e4SLinus Torvalds 				break;
15061da177e4SLinus Torvalds 		}
15071da177e4SLinus Torvalds 		prev = type;
15081da177e4SLinus Torvalds 	}
15091da177e4SLinus Torvalds 	if (type < 0) {
15101da177e4SLinus Torvalds 		err = -EINVAL;
15115d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15121da177e4SLinus Torvalds 		goto out_dput;
15131da177e4SLinus Torvalds 	}
15141da177e4SLinus Torvalds 	if (!security_vm_enough_memory(p->pages))
15151da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
15161da177e4SLinus Torvalds 	else {
15171da177e4SLinus Torvalds 		err = -ENOMEM;
15185d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15191da177e4SLinus Torvalds 		goto out_dput;
15201da177e4SLinus Torvalds 	}
1521efa90a98SHugh Dickins 	if (prev < 0)
15221da177e4SLinus Torvalds 		swap_list.head = p->next;
1523efa90a98SHugh Dickins 	else
1524efa90a98SHugh Dickins 		swap_info[prev]->next = p->next;
15251da177e4SLinus Torvalds 	if (type == swap_list.next) {
15261da177e4SLinus Torvalds 		/* just pick something that's safe... */
15271da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
15281da177e4SLinus Torvalds 	}
152978ecba08SHugh Dickins 	if (p->prio < 0) {
1530efa90a98SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i]->next)
1531efa90a98SHugh Dickins 			swap_info[i]->prio = p->prio--;
153278ecba08SHugh Dickins 		least_priority++;
153378ecba08SHugh Dickins 	}
15341da177e4SLinus Torvalds 	nr_swap_pages -= p->pages;
15351da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
15361da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
15375d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1538fb4f88dcSHugh Dickins 
153935451beeSHugh Dickins 	current->flags |= PF_OOM_ORIGIN;
15401da177e4SLinus Torvalds 	err = try_to_unuse(type);
154135451beeSHugh Dickins 	current->flags &= ~PF_OOM_ORIGIN;
15421da177e4SLinus Torvalds 
15431da177e4SLinus Torvalds 	if (err) {
15441da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
15455d337b91SHugh Dickins 		spin_lock(&swap_lock);
154678ecba08SHugh Dickins 		if (p->prio < 0)
154778ecba08SHugh Dickins 			p->prio = --least_priority;
154878ecba08SHugh Dickins 		prev = -1;
1549efa90a98SHugh Dickins 		for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1550efa90a98SHugh Dickins 			if (p->prio >= swap_info[i]->prio)
15511da177e4SLinus Torvalds 				break;
155278ecba08SHugh Dickins 			prev = i;
155378ecba08SHugh Dickins 		}
15541da177e4SLinus Torvalds 		p->next = i;
15551da177e4SLinus Torvalds 		if (prev < 0)
1556efa90a98SHugh Dickins 			swap_list.head = swap_list.next = type;
15571da177e4SLinus Torvalds 		else
1558efa90a98SHugh Dickins 			swap_info[prev]->next = type;
15591da177e4SLinus Torvalds 		nr_swap_pages += p->pages;
15601da177e4SLinus Torvalds 		total_swap_pages += p->pages;
15611da177e4SLinus Torvalds 		p->flags |= SWP_WRITEOK;
15625d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15631da177e4SLinus Torvalds 		goto out_dput;
15641da177e4SLinus Torvalds 	}
156552b7efdbSHugh Dickins 
156652b7efdbSHugh Dickins 	/* wait for any unplug function to finish */
156752b7efdbSHugh Dickins 	down_write(&swap_unplug_sem);
156852b7efdbSHugh Dickins 	up_write(&swap_unplug_sem);
156952b7efdbSHugh Dickins 
15704cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1571*570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
1572*570a335bSHugh Dickins 		free_swap_count_continuations(p);
1573*570a335bSHugh Dickins 
1574fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
15755d337b91SHugh Dickins 	spin_lock(&swap_lock);
15761da177e4SLinus Torvalds 	drain_mmlist();
15775d337b91SHugh Dickins 
15785d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
15795d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
15805d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
15815d337b91SHugh Dickins 		spin_unlock(&swap_lock);
158213e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
15835d337b91SHugh Dickins 		spin_lock(&swap_lock);
15845d337b91SHugh Dickins 	}
15855d337b91SHugh Dickins 
15861da177e4SLinus Torvalds 	swap_file = p->swap_file;
15871da177e4SLinus Torvalds 	p->swap_file = NULL;
15881da177e4SLinus Torvalds 	p->max = 0;
15891da177e4SLinus Torvalds 	swap_map = p->swap_map;
15901da177e4SLinus Torvalds 	p->swap_map = NULL;
15911da177e4SLinus Torvalds 	p->flags = 0;
15925d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1593fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
15941da177e4SLinus Torvalds 	vfree(swap_map);
159527a7faa0SKAMEZAWA Hiroyuki 	/* Destroy swap account informatin */
159627a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
159727a7faa0SKAMEZAWA Hiroyuki 
15981da177e4SLinus Torvalds 	inode = mapping->host;
15991da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
16001da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
16011da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
16021da177e4SLinus Torvalds 		bd_release(bdev);
16031da177e4SLinus Torvalds 	} else {
16041b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
16051da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
16061b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
16071da177e4SLinus Torvalds 	}
16081da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
16091da177e4SLinus Torvalds 	err = 0;
16101da177e4SLinus Torvalds 
16111da177e4SLinus Torvalds out_dput:
16121da177e4SLinus Torvalds 	filp_close(victim, NULL);
16131da177e4SLinus Torvalds out:
16141da177e4SLinus Torvalds 	return err;
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
16181da177e4SLinus Torvalds /* iterator */
16191da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
16201da177e4SLinus Torvalds {
1621efa90a98SHugh Dickins 	struct swap_info_struct *si;
1622efa90a98SHugh Dickins 	int type;
16231da177e4SLinus Torvalds 	loff_t l = *pos;
16241da177e4SLinus Torvalds 
1625fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16261da177e4SLinus Torvalds 
1627881e4aabSSuleiman Souhlal 	if (!l)
1628881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1629881e4aabSSuleiman Souhlal 
1630efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1631efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1632efa90a98SHugh Dickins 		si = swap_info[type];
1633efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
16341da177e4SLinus Torvalds 			continue;
1635881e4aabSSuleiman Souhlal 		if (!--l)
1636efa90a98SHugh Dickins 			return si;
16371da177e4SLinus Torvalds 	}
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	return NULL;
16401da177e4SLinus Torvalds }
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
16431da177e4SLinus Torvalds {
1644efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
1645efa90a98SHugh Dickins 	int type;
16461da177e4SLinus Torvalds 
1647881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1648efa90a98SHugh Dickins 		type = 0;
1649efa90a98SHugh Dickins 	else
1650efa90a98SHugh Dickins 		type = si->type + 1;
1651881e4aabSSuleiman Souhlal 
1652efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
1653efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1654efa90a98SHugh Dickins 		si = swap_info[type];
1655efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
16561da177e4SLinus Torvalds 			continue;
16571da177e4SLinus Torvalds 		++*pos;
1658efa90a98SHugh Dickins 		return si;
16591da177e4SLinus Torvalds 	}
16601da177e4SLinus Torvalds 
16611da177e4SLinus Torvalds 	return NULL;
16621da177e4SLinus Torvalds }
16631da177e4SLinus Torvalds 
16641da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
16651da177e4SLinus Torvalds {
1666fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
16701da177e4SLinus Torvalds {
1671efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
16721da177e4SLinus Torvalds 	struct file *file;
16731da177e4SLinus Torvalds 	int len;
16741da177e4SLinus Torvalds 
1675efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
16761da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1677881e4aabSSuleiman Souhlal 		return 0;
1678881e4aabSSuleiman Souhlal 	}
16791da177e4SLinus Torvalds 
1680efa90a98SHugh Dickins 	file = si->swap_file;
1681c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
16826eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
16831da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
1684d3ac7f89SJosef "Jeff" Sipek 			S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
16851da177e4SLinus Torvalds 				"partition" : "file\t",
1686efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
1687efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
1688efa90a98SHugh Dickins 			si->prio);
16891da177e4SLinus Torvalds 	return 0;
16901da177e4SLinus Torvalds }
16911da177e4SLinus Torvalds 
169215ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
16931da177e4SLinus Torvalds 	.start =	swap_start,
16941da177e4SLinus Torvalds 	.next =		swap_next,
16951da177e4SLinus Torvalds 	.stop =		swap_stop,
16961da177e4SLinus Torvalds 	.show =		swap_show
16971da177e4SLinus Torvalds };
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
17001da177e4SLinus Torvalds {
17011da177e4SLinus Torvalds 	return seq_open(file, &swaps_op);
17021da177e4SLinus Torvalds }
17031da177e4SLinus Torvalds 
170415ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
17051da177e4SLinus Torvalds 	.open		= swaps_open,
17061da177e4SLinus Torvalds 	.read		= seq_read,
17071da177e4SLinus Torvalds 	.llseek		= seq_lseek,
17081da177e4SLinus Torvalds 	.release	= seq_release,
17091da177e4SLinus Torvalds };
17101da177e4SLinus Torvalds 
17111da177e4SLinus Torvalds static int __init procswaps_init(void)
17121da177e4SLinus Torvalds {
17133d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
17141da177e4SLinus Torvalds 	return 0;
17151da177e4SLinus Torvalds }
17161da177e4SLinus Torvalds __initcall(procswaps_init);
17171da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
17181da177e4SLinus Torvalds 
17191796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
17201796316aSJan Beulich static int __init max_swapfiles_check(void)
17211796316aSJan Beulich {
17221796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
17231796316aSJan Beulich 	return 0;
17241796316aSJan Beulich }
17251796316aSJan Beulich late_initcall(max_swapfiles_check);
17261796316aSJan Beulich #endif
17271796316aSJan Beulich 
17281da177e4SLinus Torvalds /*
17291da177e4SLinus Torvalds  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
17301da177e4SLinus Torvalds  *
17311da177e4SLinus Torvalds  * The swapon system call
17321da177e4SLinus Torvalds  */
1733c4ea37c2SHeiko Carstens SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
17341da177e4SLinus Torvalds {
17351da177e4SLinus Torvalds 	struct swap_info_struct *p;
17361da177e4SLinus Torvalds 	char *name = NULL;
17371da177e4SLinus Torvalds 	struct block_device *bdev = NULL;
17381da177e4SLinus Torvalds 	struct file *swap_file = NULL;
17391da177e4SLinus Torvalds 	struct address_space *mapping;
17401da177e4SLinus Torvalds 	unsigned int type;
17411da177e4SLinus Torvalds 	int i, prev;
17421da177e4SLinus Torvalds 	int error;
17431da177e4SLinus Torvalds 	union swap_header *swap_header = NULL;
17446eb396dcSHugh Dickins 	unsigned int nr_good_pages = 0;
17456eb396dcSHugh Dickins 	int nr_extents = 0;
174653092a74SHugh Dickins 	sector_t span;
17471da177e4SLinus Torvalds 	unsigned long maxpages = 1;
174873fd8748SHugh Dickins 	unsigned long swapfilepages;
17498d69aaeeSHugh Dickins 	unsigned char *swap_map = NULL;
17501da177e4SLinus Torvalds 	struct page *page = NULL;
17511da177e4SLinus Torvalds 	struct inode *inode = NULL;
17521da177e4SLinus Torvalds 	int did_down = 0;
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
17551da177e4SLinus Torvalds 		return -EPERM;
1756efa90a98SHugh Dickins 
1757efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1758efa90a98SHugh Dickins 	if (!p)
1759efa90a98SHugh Dickins 		return -ENOMEM;
1760efa90a98SHugh Dickins 
17615d337b91SHugh Dickins 	spin_lock(&swap_lock);
1762efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1763efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
17641da177e4SLinus Torvalds 			break;
1765efa90a98SHugh Dickins 	}
17661da177e4SLinus Torvalds 	error = -EPERM;
17670697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
17685d337b91SHugh Dickins 		spin_unlock(&swap_lock);
1769efa90a98SHugh Dickins 		kfree(p);
17701da177e4SLinus Torvalds 		goto out;
17711da177e4SLinus Torvalds 	}
1772efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
1773efa90a98SHugh Dickins 		p->type = type;
1774efa90a98SHugh Dickins 		swap_info[type] = p;
1775efa90a98SHugh Dickins 		/*
1776efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
1777efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
1778efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
1779efa90a98SHugh Dickins 		 */
1780efa90a98SHugh Dickins 		smp_wmb();
1781efa90a98SHugh Dickins 		nr_swapfiles++;
1782efa90a98SHugh Dickins 	} else {
1783efa90a98SHugh Dickins 		kfree(p);
1784efa90a98SHugh Dickins 		p = swap_info[type];
1785efa90a98SHugh Dickins 		/*
1786efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
1787efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
1788efa90a98SHugh Dickins 		 */
1789efa90a98SHugh Dickins 	}
17909625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
17911da177e4SLinus Torvalds 	p->flags = SWP_USED;
17921da177e4SLinus Torvalds 	p->next = -1;
17935d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1794efa90a98SHugh Dickins 
17951da177e4SLinus Torvalds 	name = getname(specialfile);
17961da177e4SLinus Torvalds 	error = PTR_ERR(name);
17971da177e4SLinus Torvalds 	if (IS_ERR(name)) {
17981da177e4SLinus Torvalds 		name = NULL;
17991da177e4SLinus Torvalds 		goto bad_swap_2;
18001da177e4SLinus Torvalds 	}
18011da177e4SLinus Torvalds 	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
18021da177e4SLinus Torvalds 	error = PTR_ERR(swap_file);
18031da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
18041da177e4SLinus Torvalds 		swap_file = NULL;
18051da177e4SLinus Torvalds 		goto bad_swap_2;
18061da177e4SLinus Torvalds 	}
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	p->swap_file = swap_file;
18091da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
18101da177e4SLinus Torvalds 	inode = mapping->host;
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds 	error = -EBUSY;
18131da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
1814efa90a98SHugh Dickins 		struct swap_info_struct *q = swap_info[i];
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds 		if (i == type || !q->swap_file)
18171da177e4SLinus Torvalds 			continue;
18181da177e4SLinus Torvalds 		if (mapping == q->swap_file->f_mapping)
18191da177e4SLinus Torvalds 			goto bad_swap;
18201da177e4SLinus Torvalds 	}
18211da177e4SLinus Torvalds 
18221da177e4SLinus Torvalds 	error = -EINVAL;
18231da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
18241da177e4SLinus Torvalds 		bdev = I_BDEV(inode);
18251da177e4SLinus Torvalds 		error = bd_claim(bdev, sys_swapon);
18261da177e4SLinus Torvalds 		if (error < 0) {
18271da177e4SLinus Torvalds 			bdev = NULL;
1828f7b3a435SRob Landley 			error = -EINVAL;
18291da177e4SLinus Torvalds 			goto bad_swap;
18301da177e4SLinus Torvalds 		}
18311da177e4SLinus Torvalds 		p->old_block_size = block_size(bdev);
18321da177e4SLinus Torvalds 		error = set_blocksize(bdev, PAGE_SIZE);
18331da177e4SLinus Torvalds 		if (error < 0)
18341da177e4SLinus Torvalds 			goto bad_swap;
18351da177e4SLinus Torvalds 		p->bdev = bdev;
18361da177e4SLinus Torvalds 	} else if (S_ISREG(inode->i_mode)) {
18371da177e4SLinus Torvalds 		p->bdev = inode->i_sb->s_bdev;
18381b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
18391da177e4SLinus Torvalds 		did_down = 1;
18401da177e4SLinus Torvalds 		if (IS_SWAPFILE(inode)) {
18411da177e4SLinus Torvalds 			error = -EBUSY;
18421da177e4SLinus Torvalds 			goto bad_swap;
18431da177e4SLinus Torvalds 		}
18441da177e4SLinus Torvalds 	} else {
18451da177e4SLinus Torvalds 		goto bad_swap;
18461da177e4SLinus Torvalds 	}
18471da177e4SLinus Torvalds 
184873fd8748SHugh Dickins 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
18491da177e4SLinus Torvalds 
18501da177e4SLinus Torvalds 	/*
18511da177e4SLinus Torvalds 	 * Read the swap header.
18521da177e4SLinus Torvalds 	 */
18531da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
18541da177e4SLinus Torvalds 		error = -EINVAL;
18551da177e4SLinus Torvalds 		goto bad_swap;
18561da177e4SLinus Torvalds 	}
1857090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
18581da177e4SLinus Torvalds 	if (IS_ERR(page)) {
18591da177e4SLinus Torvalds 		error = PTR_ERR(page);
18601da177e4SLinus Torvalds 		goto bad_swap;
18611da177e4SLinus Torvalds 	}
186281e33971SHugh Dickins 	swap_header = kmap(page);
18631da177e4SLinus Torvalds 
186481e33971SHugh Dickins 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1865e97a3111SJesper Juhl 		printk(KERN_ERR "Unable to find swap-space signature\n");
18661da177e4SLinus Torvalds 		error = -EINVAL;
18671da177e4SLinus Torvalds 		goto bad_swap;
18681da177e4SLinus Torvalds 	}
18691da177e4SLinus Torvalds 
1870797df574SChris Dearman 	/* swap partition endianess hack... */
1871797df574SChris Dearman 	if (swab32(swap_header->info.version) == 1) {
1872797df574SChris Dearman 		swab32s(&swap_header->info.version);
1873797df574SChris Dearman 		swab32s(&swap_header->info.last_page);
1874797df574SChris Dearman 		swab32s(&swap_header->info.nr_badpages);
1875797df574SChris Dearman 		for (i = 0; i < swap_header->info.nr_badpages; i++)
1876797df574SChris Dearman 			swab32s(&swap_header->info.badpages[i]);
1877797df574SChris Dearman 	}
187881e33971SHugh Dickins 	/* Check the swap header's sub-version */
18791da177e4SLinus Torvalds 	if (swap_header->info.version != 1) {
18801da177e4SLinus Torvalds 		printk(KERN_WARNING
18811da177e4SLinus Torvalds 		       "Unable to handle swap header version %d\n",
18821da177e4SLinus Torvalds 		       swap_header->info.version);
18831da177e4SLinus Torvalds 		error = -EINVAL;
18841da177e4SLinus Torvalds 		goto bad_swap;
18851da177e4SLinus Torvalds 	}
18861da177e4SLinus Torvalds 
18871da177e4SLinus Torvalds 	p->lowest_bit  = 1;
188852b7efdbSHugh Dickins 	p->cluster_next = 1;
1889efa90a98SHugh Dickins 	p->cluster_nr = 0;
189052b7efdbSHugh Dickins 
18911da177e4SLinus Torvalds 	/*
18921da177e4SLinus Torvalds 	 * Find out how many pages are allowed for a single swap
18931da177e4SLinus Torvalds 	 * device. There are two limiting factors: 1) the number of
18941da177e4SLinus Torvalds 	 * bits for the swap offset in the swp_entry_t type and
18951da177e4SLinus Torvalds 	 * 2) the number of bits in the a swap pte as defined by
18961da177e4SLinus Torvalds 	 * the different architectures. In order to find the
18971da177e4SLinus Torvalds 	 * largest possible bit mask a swap entry with swap type 0
18981da177e4SLinus Torvalds 	 * and swap offset ~0UL is created, encoded to a swap pte,
18991da177e4SLinus Torvalds 	 * decoded to a swp_entry_t again and finally the swap
19001da177e4SLinus Torvalds 	 * offset is extracted. This will mask all the bits from
19011da177e4SLinus Torvalds 	 * the initial ~0UL mask that can't be encoded in either
19021da177e4SLinus Torvalds 	 * the swp_entry_t or the architecture definition of a
19031da177e4SLinus Torvalds 	 * swap pte.
19041da177e4SLinus Torvalds 	 */
190581e33971SHugh Dickins 	maxpages = swp_offset(pte_to_swp_entry(
190681e33971SHugh Dickins 			swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
19071da177e4SLinus Torvalds 	if (maxpages > swap_header->info.last_page)
19081da177e4SLinus Torvalds 		maxpages = swap_header->info.last_page;
19091da177e4SLinus Torvalds 	p->highest_bit = maxpages - 1;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	error = -EINVAL;
1912e2244ec2SHugh Dickins 	if (!maxpages)
1913e2244ec2SHugh Dickins 		goto bad_swap;
191473fd8748SHugh Dickins 	if (swapfilepages && maxpages > swapfilepages) {
19155d1854e1SEric Sandeen 		printk(KERN_WARNING
19165d1854e1SEric Sandeen 		       "Swap area shorter than signature indicates\n");
19175d1854e1SEric Sandeen 		goto bad_swap;
19185d1854e1SEric Sandeen 	}
1919e2244ec2SHugh Dickins 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1920e2244ec2SHugh Dickins 		goto bad_swap;
19211da177e4SLinus Torvalds 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
19221da177e4SLinus Torvalds 		goto bad_swap;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
19258d69aaeeSHugh Dickins 	swap_map = vmalloc(maxpages);
192678ecba08SHugh Dickins 	if (!swap_map) {
19271da177e4SLinus Torvalds 		error = -ENOMEM;
19281da177e4SLinus Torvalds 		goto bad_swap;
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
19318d69aaeeSHugh Dickins 	memset(swap_map, 0, maxpages);
19321da177e4SLinus Torvalds 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
1933cd105df4STobias Klauser 		int page_nr = swap_header->info.badpages[i];
193481e33971SHugh Dickins 		if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
19351da177e4SLinus Torvalds 			error = -EINVAL;
193681e33971SHugh Dickins 			goto bad_swap;
193781e33971SHugh Dickins 		}
193878ecba08SHugh Dickins 		swap_map[page_nr] = SWAP_MAP_BAD;
19391da177e4SLinus Torvalds 	}
194027a7faa0SKAMEZAWA Hiroyuki 
194127a7faa0SKAMEZAWA Hiroyuki 	error = swap_cgroup_swapon(type, maxpages);
194227a7faa0SKAMEZAWA Hiroyuki 	if (error)
194327a7faa0SKAMEZAWA Hiroyuki 		goto bad_swap;
194427a7faa0SKAMEZAWA Hiroyuki 
19451da177e4SLinus Torvalds 	nr_good_pages = swap_header->info.last_page -
19461da177e4SLinus Torvalds 			swap_header->info.nr_badpages -
19471da177e4SLinus Torvalds 			1 /* header page */;
19481da177e4SLinus Torvalds 
1949e2244ec2SHugh Dickins 	if (nr_good_pages) {
195078ecba08SHugh Dickins 		swap_map[0] = SWAP_MAP_BAD;
1951e2244ec2SHugh Dickins 		p->max = maxpages;
1952e2244ec2SHugh Dickins 		p->pages = nr_good_pages;
195353092a74SHugh Dickins 		nr_extents = setup_swap_extents(p, &span);
195453092a74SHugh Dickins 		if (nr_extents < 0) {
195553092a74SHugh Dickins 			error = nr_extents;
1956e2244ec2SHugh Dickins 			goto bad_swap;
195753092a74SHugh Dickins 		}
1958e2244ec2SHugh Dickins 		nr_good_pages = p->pages;
1959e2244ec2SHugh Dickins 	}
19601da177e4SLinus Torvalds 	if (!nr_good_pages) {
19611da177e4SLinus Torvalds 		printk(KERN_WARNING "Empty swap-file\n");
19621da177e4SLinus Torvalds 		error = -EINVAL;
19631da177e4SLinus Torvalds 		goto bad_swap;
19641da177e4SLinus Torvalds 	}
19651da177e4SLinus Torvalds 
19663bd0f0c7SSuresh Jayaraman 	if (p->bdev) {
196720137a49SHugh Dickins 		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
196820137a49SHugh Dickins 			p->flags |= SWP_SOLIDSTATE;
196920137a49SHugh Dickins 			p->cluster_next = 1 + (random32() % p->highest_bit);
197020137a49SHugh Dickins 		}
19716a6ba831SHugh Dickins 		if (discard_swap(p) == 0)
19726a6ba831SHugh Dickins 			p->flags |= SWP_DISCARDABLE;
19733bd0f0c7SSuresh Jayaraman 	}
19746a6ba831SHugh Dickins 
1975fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
19765d337b91SHugh Dickins 	spin_lock(&swap_lock);
197778ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
197878ecba08SHugh Dickins 		p->prio =
197978ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
198078ecba08SHugh Dickins 	else
198178ecba08SHugh Dickins 		p->prio = --least_priority;
198278ecba08SHugh Dickins 	p->swap_map = swap_map;
198322c6f8fdSHugh Dickins 	p->flags |= SWP_WRITEOK;
19841da177e4SLinus Torvalds 	nr_swap_pages += nr_good_pages;
19851da177e4SLinus Torvalds 	total_swap_pages += nr_good_pages;
198653092a74SHugh Dickins 
19876eb396dcSHugh Dickins 	printk(KERN_INFO "Adding %uk swap on %s.  "
198820137a49SHugh Dickins 			"Priority:%d extents:%d across:%lluk %s%s\n",
198953092a74SHugh Dickins 		nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
19906a6ba831SHugh Dickins 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
199120137a49SHugh Dickins 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
19926a6ba831SHugh Dickins 		(p->flags & SWP_DISCARDABLE) ? "D" : "");
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds 	/* insert swap space into swap_list: */
19951da177e4SLinus Torvalds 	prev = -1;
1996efa90a98SHugh Dickins 	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1997efa90a98SHugh Dickins 		if (p->prio >= swap_info[i]->prio)
19981da177e4SLinus Torvalds 			break;
19991da177e4SLinus Torvalds 		prev = i;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds 	p->next = i;
2002efa90a98SHugh Dickins 	if (prev < 0)
2003efa90a98SHugh Dickins 		swap_list.head = swap_list.next = type;
2004efa90a98SHugh Dickins 	else
2005efa90a98SHugh Dickins 		swap_info[prev]->next = type;
20065d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2007fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
20081da177e4SLinus Torvalds 	error = 0;
20091da177e4SLinus Torvalds 	goto out;
20101da177e4SLinus Torvalds bad_swap:
20111da177e4SLinus Torvalds 	if (bdev) {
20121da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
20131da177e4SLinus Torvalds 		bd_release(bdev);
20141da177e4SLinus Torvalds 	}
20154cd3bb10SHugh Dickins 	destroy_swap_extents(p);
201627a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
20171da177e4SLinus Torvalds bad_swap_2:
20185d337b91SHugh Dickins 	spin_lock(&swap_lock);
20191da177e4SLinus Torvalds 	p->swap_file = NULL;
20201da177e4SLinus Torvalds 	p->flags = 0;
20215d337b91SHugh Dickins 	spin_unlock(&swap_lock);
20221da177e4SLinus Torvalds 	vfree(swap_map);
20231da177e4SLinus Torvalds 	if (swap_file)
20241da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
20251da177e4SLinus Torvalds out:
20261da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
20271da177e4SLinus Torvalds 		kunmap(page);
20281da177e4SLinus Torvalds 		page_cache_release(page);
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 	if (name)
20311da177e4SLinus Torvalds 		putname(name);
20321da177e4SLinus Torvalds 	if (did_down) {
20331da177e4SLinus Torvalds 		if (!error)
20341da177e4SLinus Torvalds 			inode->i_flags |= S_SWAPFILE;
20351b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
20361da177e4SLinus Torvalds 	}
20371da177e4SLinus Torvalds 	return error;
20381da177e4SLinus Torvalds }
20391da177e4SLinus Torvalds 
20401da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
20411da177e4SLinus Torvalds {
2042efa90a98SHugh Dickins 	unsigned int type;
20431da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
20441da177e4SLinus Torvalds 
20455d337b91SHugh Dickins 	spin_lock(&swap_lock);
2046efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2047efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
2048efa90a98SHugh Dickins 
2049efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2050efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
20511da177e4SLinus Torvalds 	}
20521da177e4SLinus Torvalds 	val->freeswap = nr_swap_pages + nr_to_be_unused;
20531da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
20545d337b91SHugh Dickins 	spin_unlock(&swap_lock);
20551da177e4SLinus Torvalds }
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds /*
20581da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
20591da177e4SLinus Torvalds  *
2060355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
2061355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
2062355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
2063355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
2064355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
2065355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
2066*570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
20671da177e4SLinus Torvalds  */
20688d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
20691da177e4SLinus Torvalds {
20701da177e4SLinus Torvalds 	struct swap_info_struct *p;
20711da177e4SLinus Torvalds 	unsigned long offset, type;
20728d69aaeeSHugh Dickins 	unsigned char count;
20738d69aaeeSHugh Dickins 	unsigned char has_cache;
2074253d553bSHugh Dickins 	int err = -EINVAL;
20751da177e4SLinus Torvalds 
2076a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
2077253d553bSHugh Dickins 		goto out;
20780697212aSChristoph Lameter 
20791da177e4SLinus Torvalds 	type = swp_type(entry);
20801da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
20811da177e4SLinus Torvalds 		goto bad_file;
2082efa90a98SHugh Dickins 	p = swap_info[type];
20831da177e4SLinus Torvalds 	offset = swp_offset(entry);
20841da177e4SLinus Torvalds 
20855d337b91SHugh Dickins 	spin_lock(&swap_lock);
2086355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
2087355cfa73SKAMEZAWA Hiroyuki 		goto unlock_out;
2088355cfa73SKAMEZAWA Hiroyuki 
2089253d553bSHugh Dickins 	count = p->swap_map[offset];
2090253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
2091253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
2092253d553bSHugh Dickins 	err = 0;
2093355cfa73SKAMEZAWA Hiroyuki 
2094253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
2095355cfa73SKAMEZAWA Hiroyuki 
2096355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2097253d553bSHugh Dickins 		if (!has_cache && count)
2098253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
2099253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
2100253d553bSHugh Dickins 			err = -EEXIST;
2101253d553bSHugh Dickins 		else				/* no users remaining */
2102253d553bSHugh Dickins 			err = -ENOENT;
2103355cfa73SKAMEZAWA Hiroyuki 
2104355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
2105253d553bSHugh Dickins 
2106*570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2107*570a335bSHugh Dickins 			count += usage;
2108*570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2109253d553bSHugh Dickins 			err = -EINVAL;
2110*570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
2111*570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2112*570a335bSHugh Dickins 		else
2113*570a335bSHugh Dickins 			err = -ENOMEM;
2114253d553bSHugh Dickins 	} else
2115253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
2116253d553bSHugh Dickins 
2117253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
2118253d553bSHugh Dickins 
2119355cfa73SKAMEZAWA Hiroyuki unlock_out:
21205d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21211da177e4SLinus Torvalds out:
2122253d553bSHugh Dickins 	return err;
21231da177e4SLinus Torvalds 
21241da177e4SLinus Torvalds bad_file:
21251da177e4SLinus Torvalds 	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
21261da177e4SLinus Torvalds 	goto out;
21271da177e4SLinus Torvalds }
2128253d553bSHugh Dickins 
2129355cfa73SKAMEZAWA Hiroyuki /*
2130355cfa73SKAMEZAWA Hiroyuki  * increase reference count of swap entry by 1.
2131355cfa73SKAMEZAWA Hiroyuki  */
2132*570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
2133355cfa73SKAMEZAWA Hiroyuki {
2134*570a335bSHugh Dickins 	int err = 0;
2135*570a335bSHugh Dickins 
2136*570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2137*570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2138*570a335bSHugh Dickins 	return err;
2139355cfa73SKAMEZAWA Hiroyuki }
21401da177e4SLinus Torvalds 
2141cb4b86baSKAMEZAWA Hiroyuki /*
2142355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
2143355cfa73SKAMEZAWA Hiroyuki  *
214473c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
2145355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
2146355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
2147355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
2148cb4b86baSKAMEZAWA Hiroyuki  */
2149cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
2150cb4b86baSKAMEZAWA Hiroyuki {
2151253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2152cb4b86baSKAMEZAWA Hiroyuki }
2153cb4b86baSKAMEZAWA Hiroyuki 
21541da177e4SLinus Torvalds /*
21555d337b91SHugh Dickins  * swap_lock prevents swap_map being freed. Don't grab an extra
21561da177e4SLinus Torvalds  * reference on the swaphandle, it doesn't matter if it becomes unused.
21571da177e4SLinus Torvalds  */
21581da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
21591da177e4SLinus Torvalds {
21608952898bSHugh Dickins 	struct swap_info_struct *si;
21613f9e7949SHugh Dickins 	int our_page_cluster = page_cluster;
21628952898bSHugh Dickins 	pgoff_t target, toff;
21638952898bSHugh Dickins 	pgoff_t base, end;
21648952898bSHugh Dickins 	int nr_pages = 0;
21651da177e4SLinus Torvalds 
21663f9e7949SHugh Dickins 	if (!our_page_cluster)	/* no readahead */
21671da177e4SLinus Torvalds 		return 0;
21688952898bSHugh Dickins 
2169efa90a98SHugh Dickins 	si = swap_info[swp_type(entry)];
21708952898bSHugh Dickins 	target = swp_offset(entry);
21718952898bSHugh Dickins 	base = (target >> our_page_cluster) << our_page_cluster;
21728952898bSHugh Dickins 	end = base + (1 << our_page_cluster);
21738952898bSHugh Dickins 	if (!base)		/* first page is swap header */
21748952898bSHugh Dickins 		base++;
21751da177e4SLinus Torvalds 
21765d337b91SHugh Dickins 	spin_lock(&swap_lock);
21778952898bSHugh Dickins 	if (end > si->max)	/* don't go beyond end of map */
21788952898bSHugh Dickins 		end = si->max;
21798952898bSHugh Dickins 
21808952898bSHugh Dickins 	/* Count contiguous allocated slots above our target */
21818952898bSHugh Dickins 	for (toff = target; ++toff < end; nr_pages++) {
21821da177e4SLinus Torvalds 		/* Don't read in free or bad pages */
21838952898bSHugh Dickins 		if (!si->swap_map[toff])
21841da177e4SLinus Torvalds 			break;
2185355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
21861da177e4SLinus Torvalds 			break;
21878952898bSHugh Dickins 	}
21888952898bSHugh Dickins 	/* Count contiguous allocated slots below our target */
21898952898bSHugh Dickins 	for (toff = target; --toff >= base; nr_pages++) {
21908952898bSHugh Dickins 		/* Don't read in free or bad pages */
21918952898bSHugh Dickins 		if (!si->swap_map[toff])
21928952898bSHugh Dickins 			break;
2193355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
21948952898bSHugh Dickins 			break;
21958952898bSHugh Dickins 	}
21965d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21978952898bSHugh Dickins 
21988952898bSHugh Dickins 	/*
21998952898bSHugh Dickins 	 * Indicate starting offset, and return number of pages to get:
22008952898bSHugh Dickins 	 * if only 1, say 0, since there's then no readahead to be done.
22018952898bSHugh Dickins 	 */
22028952898bSHugh Dickins 	*offset = ++toff;
22038952898bSHugh Dickins 	return nr_pages? ++nr_pages: 0;
22041da177e4SLinus Torvalds }
2205*570a335bSHugh Dickins 
2206*570a335bSHugh Dickins /*
2207*570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
2208*570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2209*570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
2210*570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2211*570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2212*570a335bSHugh Dickins  *
2213*570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
2214*570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
2215*570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2216*570a335bSHugh Dickins  *
2217*570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2218*570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2219*570a335bSHugh Dickins  * can be called after dropping locks.
2220*570a335bSHugh Dickins  */
2221*570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2222*570a335bSHugh Dickins {
2223*570a335bSHugh Dickins 	struct swap_info_struct *si;
2224*570a335bSHugh Dickins 	struct page *head;
2225*570a335bSHugh Dickins 	struct page *page;
2226*570a335bSHugh Dickins 	struct page *list_page;
2227*570a335bSHugh Dickins 	pgoff_t offset;
2228*570a335bSHugh Dickins 	unsigned char count;
2229*570a335bSHugh Dickins 
2230*570a335bSHugh Dickins 	/*
2231*570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2232*570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2233*570a335bSHugh Dickins 	 */
2234*570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2235*570a335bSHugh Dickins 
2236*570a335bSHugh Dickins 	si = swap_info_get(entry);
2237*570a335bSHugh Dickins 	if (!si) {
2238*570a335bSHugh Dickins 		/*
2239*570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
2240*570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
2241*570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
2242*570a335bSHugh Dickins 		 */
2243*570a335bSHugh Dickins 		goto outer;
2244*570a335bSHugh Dickins 	}
2245*570a335bSHugh Dickins 
2246*570a335bSHugh Dickins 	offset = swp_offset(entry);
2247*570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2248*570a335bSHugh Dickins 
2249*570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2250*570a335bSHugh Dickins 		/*
2251*570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
2252*570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
2253*570a335bSHugh Dickins 		 * over-provisioning.
2254*570a335bSHugh Dickins 		 */
2255*570a335bSHugh Dickins 		goto out;
2256*570a335bSHugh Dickins 	}
2257*570a335bSHugh Dickins 
2258*570a335bSHugh Dickins 	if (!page) {
2259*570a335bSHugh Dickins 		spin_unlock(&swap_lock);
2260*570a335bSHugh Dickins 		return -ENOMEM;
2261*570a335bSHugh Dickins 	}
2262*570a335bSHugh Dickins 
2263*570a335bSHugh Dickins 	/*
2264*570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2265*570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel pagetables: so it
2266*570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2267*570a335bSHugh Dickins 	 */
2268*570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2269*570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2270*570a335bSHugh Dickins 
2271*570a335bSHugh Dickins 	/*
2272*570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
2273*570a335bSHugh Dickins 	 * but it does always reset its private field.
2274*570a335bSHugh Dickins 	 */
2275*570a335bSHugh Dickins 	if (!page_private(head)) {
2276*570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2277*570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
2278*570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
2279*570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
2280*570a335bSHugh Dickins 	}
2281*570a335bSHugh Dickins 
2282*570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
2283*570a335bSHugh Dickins 		unsigned char *map;
2284*570a335bSHugh Dickins 
2285*570a335bSHugh Dickins 		/*
2286*570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
2287*570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
2288*570a335bSHugh Dickins 		 */
2289*570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
2290*570a335bSHugh Dickins 			goto out;
2291*570a335bSHugh Dickins 
2292*570a335bSHugh Dickins 		map = kmap_atomic(list_page, KM_USER0) + offset;
2293*570a335bSHugh Dickins 		count = *map;
2294*570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2295*570a335bSHugh Dickins 
2296*570a335bSHugh Dickins 		/*
2297*570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
2298*570a335bSHugh Dickins 		 * free our allocation and use this one.
2299*570a335bSHugh Dickins 		 */
2300*570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2301*570a335bSHugh Dickins 			goto out;
2302*570a335bSHugh Dickins 	}
2303*570a335bSHugh Dickins 
2304*570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
2305*570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
2306*570a335bSHugh Dickins out:
2307*570a335bSHugh Dickins 	spin_unlock(&swap_lock);
2308*570a335bSHugh Dickins outer:
2309*570a335bSHugh Dickins 	if (page)
2310*570a335bSHugh Dickins 		__free_page(page);
2311*570a335bSHugh Dickins 	return 0;
2312*570a335bSHugh Dickins }
2313*570a335bSHugh Dickins 
2314*570a335bSHugh Dickins /*
2315*570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
2316*570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2317*570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
2318*570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
2319*570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
2320*570a335bSHugh Dickins  * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2321*570a335bSHugh Dickins  */
2322*570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
2323*570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
2324*570a335bSHugh Dickins {
2325*570a335bSHugh Dickins 	struct page *head;
2326*570a335bSHugh Dickins 	struct page *page;
2327*570a335bSHugh Dickins 	unsigned char *map;
2328*570a335bSHugh Dickins 
2329*570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2330*570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
2331*570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2332*570a335bSHugh Dickins 		return false;		/* need to add count continuation */
2333*570a335bSHugh Dickins 	}
2334*570a335bSHugh Dickins 
2335*570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2336*570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
2337*570a335bSHugh Dickins 	map = kmap_atomic(page, KM_USER0) + offset;
2338*570a335bSHugh Dickins 
2339*570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2340*570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2341*570a335bSHugh Dickins 
2342*570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2343*570a335bSHugh Dickins 		/*
2344*570a335bSHugh Dickins 		 * Think of how you add 1 to 999
2345*570a335bSHugh Dickins 		 */
2346*570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
2347*570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2348*570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2349*570a335bSHugh Dickins 			BUG_ON(page == head);
2350*570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2351*570a335bSHugh Dickins 		}
2352*570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
2353*570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2354*570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2355*570a335bSHugh Dickins 			if (page == head)
2356*570a335bSHugh Dickins 				return false;	/* add count continuation */
2357*570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2358*570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
2359*570a335bSHugh Dickins 		}
2360*570a335bSHugh Dickins 		*map += 1;
2361*570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2362*570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2363*570a335bSHugh Dickins 		while (page != head) {
2364*570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2365*570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
2366*570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2367*570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2368*570a335bSHugh Dickins 		}
2369*570a335bSHugh Dickins 		return true;			/* incremented */
2370*570a335bSHugh Dickins 
2371*570a335bSHugh Dickins 	} else {				/* decrementing */
2372*570a335bSHugh Dickins 		/*
2373*570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
2374*570a335bSHugh Dickins 		 */
2375*570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
2376*570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
2377*570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2378*570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2379*570a335bSHugh Dickins 			BUG_ON(page == head);
2380*570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2381*570a335bSHugh Dickins 		}
2382*570a335bSHugh Dickins 		BUG_ON(*map == 0);
2383*570a335bSHugh Dickins 		*map -= 1;
2384*570a335bSHugh Dickins 		if (*map == 0)
2385*570a335bSHugh Dickins 			count = 0;
2386*570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2387*570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2388*570a335bSHugh Dickins 		while (page != head) {
2389*570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2390*570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
2391*570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2392*570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2393*570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2394*570a335bSHugh Dickins 		}
2395*570a335bSHugh Dickins 		return count == COUNT_CONTINUED;
2396*570a335bSHugh Dickins 	}
2397*570a335bSHugh Dickins }
2398*570a335bSHugh Dickins 
2399*570a335bSHugh Dickins /*
2400*570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
2401*570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2402*570a335bSHugh Dickins  */
2403*570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
2404*570a335bSHugh Dickins {
2405*570a335bSHugh Dickins 	pgoff_t offset;
2406*570a335bSHugh Dickins 
2407*570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2408*570a335bSHugh Dickins 		struct page *head;
2409*570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
2410*570a335bSHugh Dickins 		if (page_private(head)) {
2411*570a335bSHugh Dickins 			struct list_head *this, *next;
2412*570a335bSHugh Dickins 			list_for_each_safe(this, next, &head->lru) {
2413*570a335bSHugh Dickins 				struct page *page;
2414*570a335bSHugh Dickins 				page = list_entry(this, struct page, lru);
2415*570a335bSHugh Dickins 				list_del(this);
2416*570a335bSHugh Dickins 				__free_page(page);
2417*570a335bSHugh Dickins 			}
2418*570a335bSHugh Dickins 		}
2419*570a335bSHugh Dickins 	}
2420*570a335bSHugh Dickins }
2421