11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  linux/mm/swapfile.c
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mm.h>
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/hugetlb.h>
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/mman.h>
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel_stat.h>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/swap.h>
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/vmalloc.h>
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/pagemap.h>
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/namei.h>
17072441e21ddcd1140606b7d4ef6eab579a86b0b3Hugh Dickins#include <linux/shmem_fs.h>
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/blkdev.h>
1920137a490f397d9c01fc9fadd83a8d198bda4477Hugh Dickins#include <linux/random.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/writeback.h>
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/proc_fs.h>
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/seq_file.h>
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
245ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins#include <linux/ksm.h>
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/rmap.h>
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/security.h>
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/backing-dev.h>
28fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar#include <linux/mutex.h>
29c59ede7b78db329949d9cdcd7064e22d357560efRandy.Dunlap#include <linux/capability.h>
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/syscalls.h>
318a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh#include <linux/memcontrol.h>
3266d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers#include <linux/poll.h>
3372788c385604523422592249c19cba0187021e9bDavid Rientjes#include <linux/oom.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/pgtable.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <asm/tlbflush.h>
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/swapops.h>
3827a7faa0779dd13729196c1a818c294f44bbd1eeKAMEZAWA Hiroyuki#include <linux/page_cgroup.h>
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
40570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsstatic bool swap_count_continued(struct swap_info_struct *, pgoff_t,
41570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				 unsigned char);
42570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsstatic void free_swap_count_continuations(struct swap_info_struct *);
43d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhornstatic sector_t map_swap_entry(swp_entry_t, struct block_device**);
44570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
457c363b8c6536f26934172d3c46f0bbec01a97c61Adrian Bunkstatic DEFINE_SPINLOCK(swap_lock);
467c363b8c6536f26934172d3c46f0bbec01a97c61Adrian Bunkstatic unsigned int nr_swapfiles;
47b962716b459505a8d83aea313fea0abe76749f42Hugh Dickinslong nr_swap_pages;
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldslong total_swap_pages;
4978ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickinsstatic int least_priority;
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char Bad_file[] = "Bad swap file entry ";
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char Unused_file[] = "Unused swap file entry ";
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char Bad_offset[] = "Bad swap offset entry ";
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic const char Unused_offset[] = "Unused swap offset entry ";
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
567c363b8c6536f26934172d3c46f0bbec01a97c61Adrian Bunkstatic struct swap_list_t swap_list = {-1, -1};
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
58efa90a981bbc891efad96db2a75b5487e00852caHugh Dickinsstatic struct swap_info_struct *swap_info[MAX_SWAPFILES];
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
60fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnarstatic DEFINE_MUTEX(swapon_mutex);
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6266d7dd518ae413a383ab2c6c263cc30617329842Kay Sieversstatic DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
6366d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers/* Activity counter to indicate that a swapon or swapoff has occurred */
6466d7dd518ae413a383ab2c6c263cc30617329842Kay Sieversstatic atomic_t proc_poll_event = ATOMIC_INIT(0);
6566d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
668d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickinsstatic inline unsigned char swap_count(unsigned char ent)
67355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki{
68570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
69355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki}
70355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
71efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins/* returns 1 if swap entry is freed */
72c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyukistatic int
73c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
74c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki{
75efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	swp_entry_t entry = swp_entry(si->type, offset);
76c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	struct page *page;
77c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	int ret = 0;
78c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki
79c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	page = find_get_page(&swapper_space, entry.val);
80c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	if (!page)
81c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		return 0;
82c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	/*
83c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 * This function is called from scan_swap_map() and it's called
84c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
85c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 * We have to use trylock for avoiding deadlock. This is a special
86c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 * case and you should use try_to_free_swap() with explicit lock_page()
87c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 * in usual operations.
88c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	 */
89c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	if (trylock_page(page)) {
90c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		ret = try_to_free_swap(page);
91c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		unlock_page(page);
92c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	}
93c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	page_cache_release(page);
94c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	return ret;
95c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki}
96355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
986a6ba83175c029c7820765bae44692266b29e67aHugh Dickins * swapon tell device that all the old swap contents can be discarded,
996a6ba83175c029c7820765bae44692266b29e67aHugh Dickins * to allow the swap device to optimize its wear-levelling.
1006a6ba83175c029c7820765bae44692266b29e67aHugh Dickins */
1016a6ba83175c029c7820765bae44692266b29e67aHugh Dickinsstatic int discard_swap(struct swap_info_struct *si)
1026a6ba83175c029c7820765bae44692266b29e67aHugh Dickins{
1036a6ba83175c029c7820765bae44692266b29e67aHugh Dickins	struct swap_extent *se;
1049625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	sector_t start_block;
1059625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	sector_t nr_blocks;
1066a6ba83175c029c7820765bae44692266b29e67aHugh Dickins	int err = 0;
1076a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
1089625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	/* Do not discard the swap header page! */
1099625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	se = &si->first_swap_extent;
1109625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1119625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1129625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	if (nr_blocks) {
1139625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		err = blkdev_issue_discard(si->bdev, start_block,
114dd3932eddf428571762596e17b65f5dc92ca361bChristoph Hellwig				nr_blocks, GFP_KERNEL, 0);
1159625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		if (err)
1169625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins			return err;
1179625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		cond_resched();
1189625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	}
1196a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
1209625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1219625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		start_block = se->start_block << (PAGE_SHIFT - 9);
1229625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1236a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
1246a6ba83175c029c7820765bae44692266b29e67aHugh Dickins		err = blkdev_issue_discard(si->bdev, start_block,
125dd3932eddf428571762596e17b65f5dc92ca361bChristoph Hellwig				nr_blocks, GFP_KERNEL, 0);
1266a6ba83175c029c7820765bae44692266b29e67aHugh Dickins		if (err)
1276a6ba83175c029c7820765bae44692266b29e67aHugh Dickins			break;
1286a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
1296a6ba83175c029c7820765bae44692266b29e67aHugh Dickins		cond_resched();
1306a6ba83175c029c7820765bae44692266b29e67aHugh Dickins	}
1316a6ba83175c029c7820765bae44692266b29e67aHugh Dickins	return err;		/* That will often be -EOPNOTSUPP */
1326a6ba83175c029c7820765bae44692266b29e67aHugh Dickins}
1336a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
1347992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins/*
1357992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins * swap allocation tell device that a cluster of swap can now be discarded,
1367992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins * to allow the swap device to optimize its wear-levelling.
1377992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins */
1387992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickinsstatic void discard_swap_cluster(struct swap_info_struct *si,
1397992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				 pgoff_t start_page, pgoff_t nr_pages)
1407992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins{
1417992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	struct swap_extent *se = si->curr_swap_extent;
1427992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	int found_extent = 0;
1437992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1447992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	while (nr_pages) {
1457992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		struct list_head *lh;
1467992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1477992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		if (se->start_page <= start_page &&
1487992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		    start_page < se->start_page + se->nr_pages) {
1497992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			pgoff_t offset = start_page - se->start_page;
1507992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			sector_t start_block = se->start_block + offset;
151858a29900ea2d639759e697be901a60b759cdcfbHugh Dickins			sector_t nr_blocks = se->nr_pages - offset;
1527992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1537992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (nr_blocks > nr_pages)
1547992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				nr_blocks = nr_pages;
1557992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			start_page += nr_blocks;
1567992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			nr_pages -= nr_blocks;
1577992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1587992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (!found_extent++)
1597992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				si->curr_swap_extent = se;
1607992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1617992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			start_block <<= PAGE_SHIFT - 9;
1627992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			nr_blocks <<= PAGE_SHIFT - 9;
1637992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (blkdev_issue_discard(si->bdev, start_block,
164dd3932eddf428571762596e17b65f5dc92ca361bChristoph Hellwig				    nr_blocks, GFP_NOIO, 0))
1657992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				break;
1667992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		}
1677992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1687992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		lh = se->list.next;
1697992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		se = list_entry(lh, struct swap_extent, list);
1707992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	}
1717992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins}
1727992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
1737992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickinsstatic int wait_for_discard(void *word)
1747992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins{
1757992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	schedule();
1767992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	return 0;
1777992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins}
1787992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
179048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins#define SWAPFILE_CLUSTER	256
180048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins#define LATENCY_LIMIT		256
181048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins
18224b8ff7c27d9e975540656e377de44a2a181a01fCesar Eduardo Barrosstatic unsigned long scan_swap_map(struct swap_info_struct *si,
18324b8ff7c27d9e975540656e377de44a2a181a01fCesar Eduardo Barros				   unsigned char usage)
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
185ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	unsigned long offset;
186c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	unsigned long scan_base;
1877992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	unsigned long last_in_cluster = 0;
188048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins	int latency_ration = LATENCY_LIMIT;
1897992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	int found_free_cluster = 0;
1907dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
191886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins	/*
1927dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * We try to cluster swap pages by allocating them sequentially
1937dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
1947dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * way, however, we resort to first-free allocation, starting
1957dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * a new cluster.  This prevents us from scattering swap pages
1967dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * all over the entire swap partition, so that we reduce
1977dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * overall disk seek times between swap pages.  -- sct
1987dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 * But we do now try to find an empty cluster.  -Andrea
199c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	 * And we let swap pages go all over an SSD partition.  Hugh
2007dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	 */
2017dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
20252b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	si->flags += SWP_SCANNING;
203c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	scan_base = offset = si->cluster_next;
204ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins
205ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (unlikely(!si->cluster_nr--)) {
206ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
207ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins			si->cluster_nr = SWAPFILE_CLUSTER - 1;
208ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins			goto checks;
209ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		}
2107992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		if (si->flags & SWP_DISCARDABLE) {
2117992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			/*
2127992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * Start range check on racing allocations, in case
2137992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * they overlap the cluster we eventually decide on
2147992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * (we scan without swap_lock to allow preemption).
2157992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * It's hardly conceivable that cluster_nr could be
2167992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * wrapped during our scan, but don't depend on it.
2177992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 */
2187992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (si->lowest_alloc)
2197992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				goto checks;
2207992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			si->lowest_alloc = si->max;
2217992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			si->highest_alloc = 0;
2227992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		}
2235d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
2247dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
225c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		/*
226c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * If seek is expensive, start searching for new cluster from
227c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * start of partition, to minimize the span of allocated swap.
228c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * But if seek is cheap, search from our current position, so
229c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * that swap is allocated from all over the partition: if the
230c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * Flash Translation Layer only remaps within limited zones,
231c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 * we don't want to wear out the first zone too quickly.
232c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		 */
233c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		if (!(si->flags & SWP_SOLIDSTATE))
234c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			scan_base = offset = si->lowest_bit;
2357dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
2367dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
2377dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins		/* Locate the first empty (unaligned) cluster */
2387dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins		for (; last_in_cluster <= si->highest_bit; offset++) {
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (si->swap_map[offset])
2407dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins				last_in_cluster = offset + SWAPFILE_CLUSTER;
2417dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins			else if (offset == last_in_cluster) {
2425d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins				spin_lock(&swap_lock);
243ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins				offset -= SWAPFILE_CLUSTER - 1;
244ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins				si->cluster_next = offset;
245ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins				si->cluster_nr = SWAPFILE_CLUSTER - 1;
2467992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				found_free_cluster = 1;
247ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins				goto checks;
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
249048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins			if (unlikely(--latency_ration < 0)) {
250048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins				cond_resched();
251048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins				latency_ration = LATENCY_LIMIT;
252048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins			}
2537dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins		}
254ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins
255ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		offset = si->lowest_bit;
256c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
257c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins
258c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		/* Locate the first empty (unaligned) cluster */
259c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		for (; last_in_cluster < scan_base; offset++) {
260c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			if (si->swap_map[offset])
261c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				last_in_cluster = offset + SWAPFILE_CLUSTER;
262c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			else if (offset == last_in_cluster) {
263c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				spin_lock(&swap_lock);
264c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				offset -= SWAPFILE_CLUSTER - 1;
265c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				si->cluster_next = offset;
266c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				si->cluster_nr = SWAPFILE_CLUSTER - 1;
267c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				found_free_cluster = 1;
268c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				goto checks;
269c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			}
270c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			if (unlikely(--latency_ration < 0)) {
271c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				cond_resched();
272c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins				latency_ration = LATENCY_LIMIT;
273c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			}
274c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		}
275c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins
276c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		offset = scan_base;
2775d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_lock(&swap_lock);
278ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		si->cluster_nr = SWAPFILE_CLUSTER - 1;
2797992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		si->lowest_alloc = 0;
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2817dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
282ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickinschecks:
283ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (!(si->flags & SWP_WRITEOK))
28452b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins		goto no_page;
2857dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	if (!si->highest_bit)
2867dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins		goto no_page;
287ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (offset > si->highest_bit)
288c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		scan_base = offset = si->lowest_bit;
289c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki
290b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	/* reuse swap entry of cache-only swap if not busy. */
291b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
292c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		int swap_was_freed;
293c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		spin_unlock(&swap_lock);
294c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		swap_was_freed = __try_to_reclaim_swap(si, offset);
295c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		spin_lock(&swap_lock);
296c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		/* entry was freed successfully, try to use this again */
297c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		if (swap_was_freed)
298c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki			goto checks;
299c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		goto scan; /* check next one */
300c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki	}
301c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki
302ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (si->swap_map[offset])
303ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		goto scan;
304ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins
305ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (offset == si->lowest_bit)
306ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		si->lowest_bit++;
307ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (offset == si->highest_bit)
308ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		si->highest_bit--;
309ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	si->inuse_pages++;
310ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	if (si->inuse_pages == si->pages) {
311ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		si->lowest_bit = si->max;
312ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins		si->highest_bit = 0;
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
314253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	si->swap_map[offset] = usage;
315ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	si->cluster_next = offset + 1;
316ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	si->flags -= SWP_SCANNING;
3177992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
3187992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	if (si->lowest_alloc) {
3197992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		/*
3207992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		 * Only set when SWP_DISCARDABLE, and there's a scan
3217992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		 * for a free cluster in progress or just completed.
3227992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		 */
3237992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		if (found_free_cluster) {
3247992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			/*
3257992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * To optimize wear-levelling, discard the
3267992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * old data of the cluster, taking care not to
3277992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * discard any of its pages that have already
3287992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * been allocated by racing tasks (offset has
3297992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * already stepped over any at the beginning).
3307992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 */
3317992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (offset < si->highest_alloc &&
3327992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			    si->lowest_alloc <= last_in_cluster)
3337992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				last_in_cluster = si->lowest_alloc - 1;
3347992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			si->flags |= SWP_DISCARDING;
3357992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			spin_unlock(&swap_lock);
3367992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
3377992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (offset < last_in_cluster)
3387992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				discard_swap_cluster(si, offset,
3397992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins					last_in_cluster - offset + 1);
3407992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
3417992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			spin_lock(&swap_lock);
3427992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			si->lowest_alloc = 0;
3437992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			si->flags &= ~SWP_DISCARDING;
3447992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
3457992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			smp_mb();	/* wake_up_bit advises this */
3467992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
3477992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins
3487992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		} else if (si->flags & SWP_DISCARDING) {
3497992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			/*
3507992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * Delay using pages allocated by racing tasks
3517992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * until the whole discard has been issued. We
3527992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * could defer that delay until swap_writepage,
3537992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * but it's easier to keep this self-contained.
3547992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 */
3557992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			spin_unlock(&swap_lock);
3567992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
3577992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				wait_for_discard, TASK_UNINTERRUPTIBLE);
3587992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			spin_lock(&swap_lock);
3597992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		} else {
3607992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			/*
3617992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * Note pages allocated by racing tasks while
3627992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * scan for a free cluster is in progress, so
3637992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 * that its final discard can exclude them.
3647992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			 */
3657992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (offset < si->lowest_alloc)
3667992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				si->lowest_alloc = offset;
3677992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins			if (offset > si->highest_alloc)
3687992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins				si->highest_alloc = offset;
3697992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins		}
3707992fde72ce06c73280a1939b7a1e903bc95ef85Hugh Dickins	}
371ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickins	return offset;
3727dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
373ebebbbe904634b0ca1c674457b399f68db5e05b1Hugh Dickinsscan:
3745d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
3757dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	while (++offset <= si->highest_bit) {
37652b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins		if (!si->swap_map[offset]) {
3775d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins			spin_lock(&swap_lock);
37852b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins			goto checks;
37952b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins		}
380c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
381c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki			spin_lock(&swap_lock);
382c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki			goto checks;
383c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		}
384048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins		if (unlikely(--latency_ration < 0)) {
385048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins			cond_resched();
386048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins			latency_ration = LATENCY_LIMIT;
387048c27fd72816b44e096997d1c6901c3abbfd45bHugh Dickins		}
3887dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins	}
389c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	offset = si->lowest_bit;
390c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	while (++offset < scan_base) {
391c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		if (!si->swap_map[offset]) {
392c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			spin_lock(&swap_lock);
393c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			goto checks;
394c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		}
395c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
396c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki			spin_lock(&swap_lock);
397c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki			goto checks;
398c9e444103b5e7a5a3519f9913f59767f92e33bafKAMEZAWA Hiroyuki		}
399c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		if (unlikely(--latency_ration < 0)) {
400c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			cond_resched();
401c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins			latency_ration = LATENCY_LIMIT;
402c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins		}
403c60aa176c6de82703f064082b909496fc4fee956Hugh Dickins	}
4045d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
4057dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickins
4067dfad4183bf9cd92f977caa3c12cc74f0eefc0e6Hugh Dickinsno_page:
40752b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	si->flags -= SWP_SCANNING;
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsswp_entry_t get_swap_page(void)
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
413fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	struct swap_info_struct *si;
414fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	pgoff_t offset;
415fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	int type, next;
416fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	int wrapped = 0;
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4185d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (nr_swap_pages <= 0)
420fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		goto noswap;
421fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	nr_swap_pages--;
422fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins
423fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
424efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		si = swap_info[type];
425fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		next = si->next;
426fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		if (next < 0 ||
427efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		    (!wrapped && si->prio != swap_info[next]->prio)) {
428fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins			next = swap_list.head;
429fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins			wrapped++;
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
431fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins
432fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		if (!si->highest_bit)
433fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins			continue;
434fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		if (!(si->flags & SWP_WRITEOK))
435fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins			continue;
436fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins
437fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		swap_list.next = next;
438355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		/* This is called for allocating swap entry for cache */
439253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		offset = scan_swap_map(si, SWAP_HAS_CACHE);
4405d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		if (offset) {
4415d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins			spin_unlock(&swap_lock);
442fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins			return swp_entry(type, offset);
4435d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		}
444fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins		next = swap_list.next;
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
446fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins
447fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	nr_swap_pages++;
448fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickinsnoswap:
4495d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
450fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins	return (swp_entry_t) {0};
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
453910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins/* The only caller of this function is now susupend routine */
454910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickinsswp_entry_t get_swap_page_of_type(int type)
455910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins{
456910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	struct swap_info_struct *si;
457910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	pgoff_t offset;
458910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins
459910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	spin_lock(&swap_lock);
460910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	si = swap_info[type];
461910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	if (si && (si->flags & SWP_WRITEOK)) {
462910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		nr_swap_pages--;
463910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		/* This is called for allocating swap entry, not cache */
464910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		offset = scan_swap_map(si, 1);
465910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		if (offset) {
466910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins			spin_unlock(&swap_lock);
467910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins			return swp_entry(type, offset);
468910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		}
469910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins		nr_swap_pages++;
470910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	}
471910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	spin_unlock(&swap_lock);
472910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins	return (swp_entry_t) {0};
473910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins}
474910321ea817a202ff70fac666e37e2c8e2f88823Hugh Dickins
47573c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickinsstatic struct swap_info_struct *swap_info_get(swp_entry_t entry)
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
47773c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct swap_info_struct *p;
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long offset, type;
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!entry.val)
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	type = swp_type(entry);
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (type >= nr_swapfiles)
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_nofile;
485efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	p = swap_info[type];
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(p->flags & SWP_USED))
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_device;
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	offset = swp_offset(entry);
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (offset >= p->max)
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_offset;
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!p->swap_map[offset])
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_free;
4935d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return p;
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_free:
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	goto out;
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_offset:
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	goto out;
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_device:
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	goto out;
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_nofile:
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
509886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins}
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5118d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickinsstatic unsigned char swap_entry_free(struct swap_info_struct *p,
5128d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins				     swp_entry_t entry, unsigned char usage)
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
514253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	unsigned long offset = swp_offset(entry);
5158d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char count;
5168d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char has_cache;
517355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
518253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	count = p->swap_map[offset];
519253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	has_cache = count & SWAP_HAS_CACHE;
520253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	count &= ~SWAP_HAS_CACHE;
521355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
522253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	if (usage == SWAP_HAS_CACHE) {
523355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		VM_BUG_ON(!has_cache);
524253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		has_cache = 0;
525aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins	} else if (count == SWAP_MAP_SHMEM) {
526aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		/*
527aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		 * Or we could insist on shmem.c using a special
528aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		 * swap_shmem_free() and free_shmem_swap_and_cache()...
529aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		 */
530aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		count = 0;
531570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
532570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if (count == COUNT_CONTINUED) {
533570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			if (swap_count_continued(p, offset, count))
534570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				count = SWAP_MAP_MAX | COUNT_CONTINUED;
535570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			else
536570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				count = SWAP_MAP_MAX;
537570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		} else
538570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			count--;
539570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
540253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
541253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	if (!count)
542253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		mem_cgroup_uncharge_swap(entry);
543253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
544253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	usage = count | has_cache;
545253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	p->swap_map[offset] = usage;
546355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
547355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	/* free if no reference */
548253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	if (!usage) {
549b3a27d0529c6e5206f1b60f60263e3ecfd0d77cbNitin Gupta		struct gendisk *disk = p->bdev->bd_disk;
550355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (offset < p->lowest_bit)
551355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki			p->lowest_bit = offset;
552355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (offset > p->highest_bit)
553355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki			p->highest_bit = offset;
554efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (swap_list.next >= 0 &&
555efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		    p->prio > swap_info[swap_list.next]->prio)
556efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			swap_list.next = p->type;
557355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		nr_swap_pages++;
558355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		p->inuse_pages--;
559b3a27d0529c6e5206f1b60f60263e3ecfd0d77cbNitin Gupta		if ((p->flags & SWP_BLKDEV) &&
560b3a27d0529c6e5206f1b60f60263e3ecfd0d77cbNitin Gupta				disk->fops->swap_slot_free_notify)
561b3a27d0529c6e5206f1b60f60263e3ecfd0d77cbNitin Gupta			disk->fops->swap_slot_free_notify(p->bdev, offset);
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
563253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
564253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	return usage;
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is still around or has not been recycled.
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid swap_free(swp_entry_t entry)
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
57373c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct swap_info_struct *p;
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = swap_info_get(entry);
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (p) {
577253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		swap_entry_free(p, entry, 1);
5785d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
583cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries.
584cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki */
585cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyukivoid swapcache_free(swp_entry_t entry, struct page *page)
586cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki{
587355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	struct swap_info_struct *p;
5888d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char count;
589355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
590355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	p = swap_info_get(entry);
591355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	if (p) {
592253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
593253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		if (page)
594253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
595355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		spin_unlock(&swap_lock);
596355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	}
597cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki}
598cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki
599cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki/*
600c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins * How many references to page are currently swapped out?
601570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * This does not give an exact answer when swap count is continued,
602570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that.
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
604c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickinsstatic inline int page_swapcount(struct page *page)
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
606c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins	int count = 0;
607c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins	struct swap_info_struct *p;
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swp_entry_t entry;
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6104c21e2f2441dc5fbb957b030333f5a3f2d02dea7Hugh Dickins	entry.val = page_private(page);
6111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = swap_info_get(entry);
6121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (p) {
613355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		count = swap_count(p->swap_map[swp_offset(entry)]);
6145d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
6151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
616c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins	return count;
6171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
6207b1fe59793e61f826bef053107b57b23954833bbHugh Dickins * We can write to an anon page without COW if there are no other references
6217b1fe59793e61f826bef053107b57b23954833bbHugh Dickins * to it.  And as a side-effect, free up its swap: because the old content
6227b1fe59793e61f826bef053107b57b23954833bbHugh Dickins * on disk will never be read, and seeking back there to write new content
6237b1fe59793e61f826bef053107b57b23954833bbHugh Dickins * later would only waste time away from clustering.
6241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6257b1fe59793e61f826bef053107b57b23954833bbHugh Dickinsint reuse_swap_page(struct page *page)
6261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
627c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins	int count;
628c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins
62951726b1222863852c46ca21ed0115b85d1edfd89Hugh Dickins	VM_BUG_ON(!PageLocked(page));
6305ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins	if (unlikely(PageKsm(page)))
6315ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		return 0;
632c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins	count = page_mapcount(page);
6337b1fe59793e61f826bef053107b57b23954833bbHugh Dickins	if (count <= 1 && PageSwapCache(page)) {
634c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins		count += page_swapcount(page);
6357b1fe59793e61f826bef053107b57b23954833bbHugh Dickins		if (count == 1 && !PageWriteback(page)) {
6367b1fe59793e61f826bef053107b57b23954833bbHugh Dickins			delete_from_swap_cache(page);
6377b1fe59793e61f826bef053107b57b23954833bbHugh Dickins			SetPageDirty(page);
6387b1fe59793e61f826bef053107b57b23954833bbHugh Dickins		}
6397b1fe59793e61f826bef053107b57b23954833bbHugh Dickins	}
6405ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins	return count <= 1;
6411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
6421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
644a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins * If swap is getting full, or if there are no more mappings of this page,
645a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins * then try_to_free_swap is called to free its swap space.
6461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
647a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickinsint try_to_free_swap(struct page *page)
6481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
64951726b1222863852c46ca21ed0115b85d1edfd89Hugh Dickins	VM_BUG_ON(!PageLocked(page));
6501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!PageSwapCache(page))
6521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
6531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (PageWriteback(page))
6541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
655a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins	if (page_swapcount(page))
6561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
6571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
658b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	/*
659b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * Once hibernation has begun to create its image of memory,
660b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * there's a danger that one of the calls to try_to_free_swap()
661b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * - most probably a call from __try_to_reclaim_swap() while
662b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * hibernation is allocating its own swap pages for the image,
663b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * but conceivably even a call from memory reclaim - will free
664b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * the swap from a page which has already been recorded in the
665b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * image as a clean swapcache page, and then reuse its swap for
666b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * another page of the image.  On waking from hibernation, the
667b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * original page might be freed under memory pressure, then
668b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 * later read back in from swap, now with the wrong data.
669b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 *
670f90ac3982a78d36f894824636beeef13361d7c59Mel Gorman	 * Hibration suspends storage while it is writing the image
671f90ac3982a78d36f894824636beeef13361d7c59Mel Gorman	 * to disk so check that here.
672b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins	 */
673f90ac3982a78d36f894824636beeef13361d7c59Mel Gorman	if (pm_suspended_storage())
674b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins		return 0;
675b73d7fcecd93dc15eaa3c45c8c587b613f6673c4Hugh Dickins
676a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins	delete_from_swap_cache(page);
677a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins	SetPageDirty(page);
678a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins	return 1;
67968a22394c286a2daf06ee8d65d8835f738faefa5Rik van Riel}
68068a22394c286a2daf06ee8d65d8835f738faefa5Rik van Riel
68168a22394c286a2daf06ee8d65d8835f738faefa5Rik van Riel/*
6821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Free the swap entry like above, but also try to
6831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * free the page cache entry if it is the last user.
6841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
6852509ef26db4699a5d9fa876e90ddfc107afcab84Hugh Dickinsint free_swap_and_cache(swp_entry_t entry)
6861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
6872509ef26db4699a5d9fa876e90ddfc107afcab84Hugh Dickins	struct swap_info_struct *p;
6881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct page *page = NULL;
6891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
690a7420aa54dbf699a5a05feba3c859b6baaa3938cAndi Kleen	if (non_swap_entry(entry))
6912509ef26db4699a5d9fa876e90ddfc107afcab84Hugh Dickins		return 1;
6920697212a411c1dae03c27845f2de2f3adb32c331Christoph Lameter
6931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p = swap_info_get(entry);
6941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (p) {
695253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
69693fac7041f082297b93655a0e49f659cd7520e40Nick Piggin			page = find_get_page(&swapper_space, entry.val);
6978413ac9d8c9a1366a4f57880723126cd24e5a5c3Nick Piggin			if (page && !trylock_page(page)) {
69893fac7041f082297b93655a0e49f659cd7520e40Nick Piggin				page_cache_release(page);
69993fac7041f082297b93655a0e49f659cd7520e40Nick Piggin				page = NULL;
70093fac7041f082297b93655a0e49f659cd7520e40Nick Piggin			}
70193fac7041f082297b93655a0e49f659cd7520e40Nick Piggin		}
7025d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
7031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (page) {
705a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins		/*
706a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins		 * Not mapped elsewhere, or swap space full? Free it!
707a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins		 * Also recheck PageSwapCache now page is locked (above).
708a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins		 */
70993fac7041f082297b93655a0e49f659cd7520e40Nick Piggin		if (PageSwapCache(page) && !PageWriteback(page) &&
710a2c43eed8334e878702fca713b212ae2a11d84b9Hugh Dickins				(!page_mapped(page) || vm_swap_full())) {
7111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			delete_from_swap_cache(page);
7121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			SetPageDirty(page);
7131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
7141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unlock_page(page);
7151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page_cache_release(page);
7161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
7172509ef26db4699a5d9fa876e90ddfc107afcab84Hugh Dickins	return p != NULL;
7181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
7191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
720024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura#ifdef CONFIG_CGROUP_MEM_RES_CTLR
721024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura/**
722024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * mem_cgroup_count_swap_user - count the user of a swap entry
723024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * @ent: the swap entry to be checked
724024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * @pagep: the pointer for the swap cache page of the entry to be stored
725024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura *
726024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * Returns the number of the user of the swap entry. The number is valid only
727024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * for swaps of anonymous pages.
728024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * If the entry is found on swap cache, the page is stored to pagep with
729024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura * refcount of it being incremented.
730024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura */
731024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimuraint mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
732024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura{
733024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	struct page *page;
734024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	struct swap_info_struct *p;
735024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	int count = 0;
736024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura
737024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	page = find_get_page(&swapper_space, ent.val);
738024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	if (page)
739024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura		count += page_mapcount(page);
740024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	p = swap_info_get(ent);
741024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	if (p) {
742024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura		count += swap_count(p->swap_map[swp_offset(ent)]);
743024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura		spin_unlock(&swap_lock);
744024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	}
745024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura
746024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	*pagep = page;
747024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura	return count;
748024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura}
749024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura#endif
750024914477e15ef8b17f271ec47f1bb8a589f0806Daisuke Nishimura
751b0cb1a19d05b8ea8611a9ef48a17fe417f1832e6Rafael J. Wysocki#ifdef CONFIG_HIBERNATION
752f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki/*
753915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki * Find the swap type that corresponds to given device (if any).
754f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki *
755915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting
756915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki * from 0, in which the swap header is expected to be located.
757915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki *
758915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki * This is needed for the suspend to disk (aka swsusp).
759f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki */
7607bf236874292fd073c6bdd27f89c3d9e81a79cbcRafael J. Wysockiint swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
761f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki{
762915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki	struct block_device *bdev = NULL;
763efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	int type;
764f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki
765915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki	if (device)
766915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki		bdev = bdget(device);
767915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki
768f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	spin_lock(&swap_lock);
769efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = 0; type < nr_swapfiles; type++) {
770efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		struct swap_info_struct *sis = swap_info[type];
771f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki
772915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki		if (!(sis->flags & SWP_WRITEOK))
773f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki			continue;
774b6b5bce3571e496504a89ee575d32101e0a98b93Rafael J. Wysocki
775915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki		if (!bdev) {
7767bf236874292fd073c6bdd27f89c3d9e81a79cbcRafael J. Wysocki			if (bdev_p)
777dddac6a7b445de95515f64fdf82fe5dc36c02f26Alan Jenkins				*bdev_p = bdgrab(sis->bdev);
7787bf236874292fd073c6bdd27f89c3d9e81a79cbcRafael J. Wysocki
7796e1819d615f24ce0726a7d0bd3dd0152d7b21654Rafael J. Wysocki			spin_unlock(&swap_lock);
780efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			return type;
7816e1819d615f24ce0726a7d0bd3dd0152d7b21654Rafael J. Wysocki		}
782915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki		if (bdev == sis->bdev) {
7839625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins			struct swap_extent *se = &sis->first_swap_extent;
784915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki
785915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki			if (se->start_block == offset) {
7867bf236874292fd073c6bdd27f89c3d9e81a79cbcRafael J. Wysocki				if (bdev_p)
787dddac6a7b445de95515f64fdf82fe5dc36c02f26Alan Jenkins					*bdev_p = bdgrab(sis->bdev);
7887bf236874292fd073c6bdd27f89c3d9e81a79cbcRafael J. Wysocki
789915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki				spin_unlock(&swap_lock);
790915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki				bdput(bdev);
791efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins				return type;
792915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki			}
793f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki		}
794f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	}
795f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	spin_unlock(&swap_lock);
796915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki	if (bdev)
797915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki		bdput(bdev);
798915bae9ebe41e52d71ad8b06d50e4ab26189f964Rafael J. Wysocki
799f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	return -ENODEV;
800f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki}
801f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki
802f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki/*
80373c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
80473c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins * corresponding to given index in swap_info (swap type).
80573c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins */
80673c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickinssector_t swapdev_block(int type, pgoff_t offset)
80773c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins{
80873c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct block_device *bdev;
80973c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins
81073c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	if ((unsigned int)type >= nr_swapfiles)
81173c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins		return 0;
81273c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	if (!(swap_info[type]->flags & SWP_WRITEOK))
81373c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins		return 0;
814d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn	return map_swap_entry(swp_entry(type, offset), &bdev);
81573c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins}
81673c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins
81773c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins/*
818f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki * Return either the total number of swap pages of given type, or the number
819f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki * of free pages of that type (depending on @free)
820f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki *
821f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki * This is needed for software suspend
822f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki */
823f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysockiunsigned int count_swap_pages(int type, int free)
824f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki{
825f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	unsigned int n = 0;
826f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki
827efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	spin_lock(&swap_lock);
828efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	if ((unsigned int)type < nr_swapfiles) {
829efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		struct swap_info_struct *sis = swap_info[type];
830efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins
831efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (sis->flags & SWP_WRITEOK) {
832efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			n = sis->pages;
833f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki			if (free)
834efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins				n -= sis->inuse_pages;
835f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki		}
836f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	}
837efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	spin_unlock(&swap_lock);
838f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki	return n;
839f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki}
84073c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins#endif /* CONFIG_HIBERNATION */
841f577eb30afdc68233f25d4d82b04102129262365Rafael J. Wysocki
8421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
84372866f6f277ec0ddd6df7a3b6ecdcf59a28de115Hugh Dickins * No need to decide whether this PTE shares the swap entry with others,
84472866f6f277ec0ddd6df7a3b6ecdcf59a28de115Hugh Dickins * just let do_wp_page work it out if a write is requested later - to
84572866f6f277ec0ddd6df7a3b6ecdcf59a28de115Hugh Dickins * force COW, vm_page_prot omits write permission from any private vma.
8461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
847044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickinsstatic int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
8481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long addr, swp_entry_t entry, struct page *page)
8491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
85072835c86ca15d0126354b73d5f29ce9194931c9bJohannes Weiner	struct mem_cgroup *memcg;
851044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	spinlock_t *ptl;
852044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	pte_t *pte;
853044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	int ret = 1;
854044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins
85572835c86ca15d0126354b73d5f29ce9194931c9bJohannes Weiner	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
85672835c86ca15d0126354b73d5f29ce9194931c9bJohannes Weiner					 GFP_KERNEL, &memcg)) {
857044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins		ret = -ENOMEM;
85885d9fc89fb0f0703df6444f260187c088a8d59ffKAMEZAWA Hiroyuki		goto out_nolock;
85985d9fc89fb0f0703df6444f260187c088a8d59ffKAMEZAWA Hiroyuki	}
860044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins
861044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
862044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
863044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins		if (ret > 0)
86472835c86ca15d0126354b73d5f29ce9194931c9bJohannes Weiner			mem_cgroup_cancel_charge_swapin(memcg);
865044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins		ret = 0;
866044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins		goto out;
867044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	}
8688a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh
869b084d4353ff99d824d3bc5a5c2c22c70b1fba722KAMEZAWA Hiroyuki	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
870d559db086ff5be9bcc259e5aa50bf3d881eaf1d1KAMEZAWA Hiroyuki	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
8711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	get_page(page);
8721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	set_pte_at(vma->vm_mm, addr, pte,
8731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
8741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	page_add_anon_rmap(page, vma, addr);
87572835c86ca15d0126354b73d5f29ce9194931c9bJohannes Weiner	mem_cgroup_commit_charge_swapin(page, memcg);
8761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swap_free(entry);
8771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
8781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Move the page to the active list so it is not
8791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * immediately swapped out again after swapon.
8801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
8811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	activate_page(page);
882044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickinsout:
883044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	pte_unmap_unlock(pte, ptl);
88485d9fc89fb0f0703df6444f260187c088a8d59ffKAMEZAWA Hiroyukiout_nolock:
885044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	return ret;
8861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
8871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
8881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
8891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				unsigned long addr, unsigned long end,
8901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swp_entry_t entry, struct page *page)
8911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
8921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pte_t swp_pte = swp_entry_to_pte(entry);
893705e87c0c3c38424f7f30556c85bc20e808d2f59Hugh Dickins	pte_t *pte;
8948a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	int ret = 0;
8951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
896044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	/*
897044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * We don't actually need pte lock while scanning for swp_pte: since
898044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
899044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * page table while we're scanning; though it could get zapped, and on
900044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
901044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * of unmatched parts which look like swp_pte, so unuse_pte must
902044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * recheck under pte lock.  Scanning without pte lock lets it be
903044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
904044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	 */
905044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	pte = pte_offset_map(pmd, addr);
9061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
9071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
9081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * swapoff spends a _lot_ of time in this loop!
9091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Test inline before going to call unuse_pte.
9101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
9111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (unlikely(pte_same(*pte, swp_pte))) {
912044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins			pte_unmap(pte);
913044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins			ret = unuse_pte(vma, pmd, addr, entry, page);
914044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins			if (ret)
915044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins				goto out;
916044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins			pte = pte_offset_map(pmd, addr);
9171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
9181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (pte++, addr += PAGE_SIZE, addr != end);
919044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickins	pte_unmap(pte - 1);
920044d66c1d2b1c5aa50b4d6d68c21c6c93dd678daHugh Dickinsout:
9218a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	return ret;
9221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
9251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				unsigned long addr, unsigned long end,
9261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swp_entry_t entry, struct page *page)
9271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pmd_t *pmd;
9291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long next;
9308a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	int ret;
9311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pmd = pmd_offset(pud, addr);
9331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
9341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		next = pmd_addr_end(addr, end);
9353f04f62f90d46a82dd73027c5fd7a15daed5c33dAndrea Arcangeli		if (unlikely(pmd_trans_huge(*pmd)))
9363f04f62f90d46a82dd73027c5fd7a15daed5c33dAndrea Arcangeli			continue;
9371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (pmd_none_or_clear_bad(pmd))
9381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
9398a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
9408a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		if (ret)
9418a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh			return ret;
9421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (pmd++, addr = next, addr != end);
9431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
9441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
9471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				unsigned long addr, unsigned long end,
9481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swp_entry_t entry, struct page *page)
9491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pud_t *pud;
9511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long next;
9528a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	int ret;
9531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pud = pud_offset(pgd, addr);
9551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
9561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		next = pud_addr_end(addr, end);
9571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (pud_none_or_clear_bad(pud))
9581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
9598a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
9608a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		if (ret)
9618a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh			return ret;
9621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (pud++, addr = next, addr != end);
9631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
9641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int unuse_vma(struct vm_area_struct *vma,
9671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swp_entry_t entry, struct page *page)
9681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pgd_t *pgd;
9701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long addr, end, next;
9718a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	int ret;
9721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9733ca7b3c5b64d35fe02c35b5d44c2c58b49499feeHugh Dickins	if (page_anon_vma(page)) {
9741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr = page_address_in_vma(page, vma);
9751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (addr == -EFAULT)
9761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0;
9771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else
9781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			end = addr + PAGE_SIZE;
9791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
9801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		addr = vma->vm_start;
9811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		end = vma->vm_end;
9821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
9831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pgd = pgd_offset(vma->vm_mm, addr);
9851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
9861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		next = pgd_addr_end(addr, end);
9871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (pgd_none_or_clear_bad(pgd))
9881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
9898a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
9908a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		if (ret)
9918a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh			return ret;
9921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (pgd++, addr = next, addr != end);
9931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
9941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
9951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
9961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int unuse_mm(struct mm_struct *mm,
9971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swp_entry_t entry, struct page *page)
9981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
9991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct vm_area_struct *vma;
10008a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	int ret = 0;
10011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!down_read_trylock(&mm->mmap_sem)) {
10031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
10047d03431cf98aaed635524024273668bb8cedaddaFernando Luis Vazquez Cao		 * Activate page so shrink_inactive_list is unlikely to unmap
10057d03431cf98aaed635524024273668bb8cedaddaFernando Luis Vazquez Cao		 * its ptes while lock is dropped, so swapoff can make progress.
10061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1007c475a8ab625d567eacf5e30ec35d6d8704558062Hugh Dickins		activate_page(page);
10081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unlock_page(page);
10091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		down_read(&mm->mmap_sem);
10101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lock_page(page);
10111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (vma = mm->mmap; vma; vma = vma->vm_next) {
10138a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
10141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
10151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up_read(&mm->mmap_sem);
10178a9f3ccd24741b50200c3f33d62534c7271f3dfcBalbir Singh	return (ret < 0)? ret: 0;
10181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Scan swap_map from current position to next entry still in use.
10221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty.
10231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10246eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickinsstatic unsigned int find_next_to_unuse(struct swap_info_struct *si,
10256eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickins					unsigned int prev)
10261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
10276eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickins	unsigned int max = si->max;
10286eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickins	unsigned int i = prev;
10298d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char count;
10301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10325d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	 * No need for swap_lock here: we're just looking
10331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * for whether an entry is in use, not modifying it; false
10341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * hits are okay, and sys_swapoff() has already prevented new
10355d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	 * allocations from this area (while holding swap_lock).
10361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
10371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (;;) {
10381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (++i >= max) {
10391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!prev) {
10401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				i = 0;
10411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
10421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
10431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
10441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * No entries in use at top of swap_map,
10451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * loop back to start and recheck there.
10461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
10471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			max = prev + 1;
10481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			prev = 0;
10491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			i = 1;
10501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
10511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		count = si->swap_map[i];
1052355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (count && swap_count(count) != SWAP_MAP_BAD)
10531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
10541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
10551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return i;
10561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
10571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
10591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * We completely avoid races by reading each swap page in advance,
10601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and then search for the process using it.  All the necessary
10611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * page table adjustments can then be made atomically.
10621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
10631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int try_to_unuse(unsigned int type)
10641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1065efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	struct swap_info_struct *si = swap_info[type];
10661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mm_struct *start_mm;
10678d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char *swap_map;
10688d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char swcount;
10691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct page *page;
10701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swp_entry_t entry;
10716eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickins	unsigned int i = 0;
10721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int retval = 0;
10731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * When searching mms for an entry, a good strategy is to
10761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * start at the first mm we freed the previous entry from
10771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * (though actually we don't notice whether we or coincidence
10781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * freed the entry).  Initialize this start_mm with a hold.
10791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
10801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * A simpler strategy would be to start at the last mm we
10811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * freed the previous entry from; but that would take less
10821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * advantage of mmlist ordering, which clusters forked mms
10831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * together, child after parent.  If we race with dup_mmap(), we
10841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * prefer to resolve parent before child, lest we miss entries
10851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * duplicated after we scanned child: using last mm would invert
1086570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * that.
10871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
10881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	start_mm = &init_mm;
10891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	atomic_inc(&init_mm.mm_users);
10901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
10911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
10921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Keep on scanning until all entries have gone.  Usually,
10931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * one pass through swap_map is enough, but not necessarily:
10941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * there are races when an instance of an entry might be missed.
10951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
10961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while ((i = find_next_to_unuse(si, i)) != 0) {
10971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (signal_pending(current)) {
10981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			retval = -EINTR;
10991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
11001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
11011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1102886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins		/*
11031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Get a page for the entry, using the existing swap
11041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * cache page if there is one.  Otherwise, get a clean
1105886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins		 * page and read the swap into it.
11061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
11071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swap_map = &si->swap_map[i];
11081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		entry = swp_entry(type, i);
110902098feaa42b2e0087fbbe6c6ab9a23e4653b16aHugh Dickins		page = read_swap_cache_async(entry,
111002098feaa42b2e0087fbbe6c6ab9a23e4653b16aHugh Dickins					GFP_HIGHUSER_MOVABLE, NULL, 0);
11111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (!page) {
11121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/*
11131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * Either swap_duplicate() failed because entry
11141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * has been freed independently, and will not be
11151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * reused since sys_swapoff() already disabled
11161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 * allocation from here, or alloc_page() failed.
11171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 */
11181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (!*swap_map)
11191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				continue;
11201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			retval = -ENOMEM;
11211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
11221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
11231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
11251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Don't hold on to start_mm if it looks like exiting.
11261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
11271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (atomic_read(&start_mm->mm_users) == 1) {
11281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			mmput(start_mm);
11291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			start_mm = &init_mm;
11301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			atomic_inc(&init_mm.mm_users);
11311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
11321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
11341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Wait for and lock page.  When do_swap_page races with
11351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * try_to_unuse, do_swap_page can handle the fault much
11361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * faster than try_to_unuse can locate the entry.  This
11371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
11381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * defer to do_swap_page in such a case - in some tests,
11391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * do_swap_page and try_to_unuse repeatedly compete.
11401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
11411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		wait_on_page_locked(page);
11421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		wait_on_page_writeback(page);
11431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		lock_page(page);
11441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		wait_on_page_writeback(page);
11451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
11471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Remove all references to entry.
11481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
11491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swcount = *swap_map;
1150aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1151aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			retval = shmem_unuse(entry, page);
1152aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			/* page has already been unlocked and released */
1153aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			if (retval < 0)
1154aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins				break;
1155aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			continue;
11561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1157aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins		if (swap_count(swcount) && start_mm != &init_mm)
1158aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			retval = unuse_mm(start_mm, entry, page);
1159aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins
1160355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (swap_count(*swap_map)) {
11611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			int set_start_mm = (*swap_map >= swcount);
11621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct list_head *p = &start_mm->mmlist;
11631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct mm_struct *new_start_mm = start_mm;
11641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct mm_struct *prev_mm = start_mm;
11651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct mm_struct *mm;
11661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			atomic_inc(&new_start_mm->mm_users);
11681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			atomic_inc(&prev_mm->mm_users);
11691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spin_lock(&mmlist_lock);
1170aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins			while (swap_count(*swap_map) && !retval &&
11711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					(p = p->next) != &start_mm->mmlist) {
11721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				mm = list_entry(p, struct mm_struct, mmlist);
117370af7c5c6492ef6ad137dbff6c4568c73edbcaf0Hugh Dickins				if (!atomic_inc_not_zero(&mm->mm_users))
11741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					continue;
11751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				spin_unlock(&mmlist_lock);
11761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				mmput(prev_mm);
11771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				prev_mm = mm;
11781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				cond_resched();
11801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
11811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				swcount = *swap_map;
1182355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki				if (!swap_count(swcount)) /* any usage ? */
11831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					;
1184aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins				else if (mm == &init_mm)
11851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					set_start_mm = 1;
1186aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins				else
11871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					retval = unuse_mm(mm, entry, page);
1188355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
118932c5fc10e79a7053ac5728b01a0bff55cbcb9d49Bo Liu				if (set_start_mm && *swap_map < swcount) {
11901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					mmput(new_start_mm);
11911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					atomic_inc(&mm->mm_users);
11921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					new_start_mm = mm;
11931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					set_start_mm = 0;
11941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				}
11951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				spin_lock(&mmlist_lock);
11961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
11971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			spin_unlock(&mmlist_lock);
11981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			mmput(prev_mm);
11991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			mmput(start_mm);
12001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			start_mm = new_start_mm;
12011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
12021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (retval) {
12031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			unlock_page(page);
12041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			page_cache_release(page);
12051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
12061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
12071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
12091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * If a reference remains (rare), we would like to leave
12101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * the page in the swap cache; but try_to_unmap could
12111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * then re-duplicate the entry once we drop page lock,
12121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * so we might loop indefinitely; also, that page could
12131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * not be swapped out to other storage meanwhile.  So:
12141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * delete from cache even if there's another reference,
12151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * after ensuring that the data has been saved to disk -
12161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * since if the reference remains (rarer), it will be
12171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * read from disk into another page.  Splitting into two
12181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * pages would be incorrect if swap supported "shared
12191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * private" pages, but they are handled by tmpfs files.
12205ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 *
12215ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 * Given how unuse_vma() targets one particular offset
12225ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 * in an anon_vma, once the anon_vma has been determined,
12235ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 * this splitting happens to be just what is needed to
12245ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 * handle where KSM pages have been swapped out: re-reading
12255ad6468801d28c4d4ac9f48ec19297817c915f6aHugh Dickins		 * is unnecessarily slow, but we can fix that later on.
12261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
1227355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (swap_count(*swap_map) &&
1228355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		     PageDirty(page) && PageSwapCache(page)) {
12291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			struct writeback_control wbc = {
12301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				.sync_mode = WB_SYNC_NONE,
12311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			};
12321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			swap_writepage(page, &wbc);
12341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			lock_page(page);
12351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			wait_on_page_writeback(page);
12361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
123768bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins
123868bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		/*
123968bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 * It is conceivable that a racing task removed this page from
124068bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 * swap cache just before we acquired the page lock at the top,
124168bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 * or while we dropped it in unuse_mm().  The page might even
124268bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 * be back in swap cache on another swap area: that we must not
124368bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 * delete, since it may not have been written out to swap yet.
124468bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		 */
124568bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		if (PageSwapCache(page) &&
124668bdc8d64742ccc5e340c5d122ebbab3f0cf2a74Hugh Dickins		    likely(page_private(page) == entry.val))
12472e0e26c76a35de8f8bec6b2b917518cfeb52888aHugh Dickins			delete_from_swap_cache(page);
12481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
12501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * So we could skip searching mms once swap count went
12511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * to 1, we did not mark any present ptes as dirty: must
12522706a1b89b1a3e7434a668d4a9d15f616da96685Anderson Briglia		 * mark page dirty so shrink_page_list will preserve it.
12531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
12541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		SetPageDirty(page);
12551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unlock_page(page);
12561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page_cache_release(page);
12571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
12591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * Make sure that we aren't completely killing
12601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * interactive performance.
12611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
12621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		cond_resched();
12631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
12641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mmput(start_mm);
12661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return retval;
12671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
12705d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know
12715d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins * we can empty the mmlist.  swap_lock must be held on entry and exit.
12725d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be
12731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy.
12741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
12751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void drain_mmlist(void)
12761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
12771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head *p, *next;
1278efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	unsigned int type;
12791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1280efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = 0; type < nr_swapfiles; type++)
1281efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (swap_info[type]->inuse_pages)
12821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return;
12831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_lock(&mmlist_lock);
12841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	list_for_each_safe(p, next, &init_mm.mmlist)
12851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_del_init(p);
12861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	spin_unlock(&mmlist_lock);
12871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
12881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
12891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
12901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1291d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn * corresponds to page offset for the specified swap entry.
1292d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset
1293d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn * into the bdev, not sector offset.
12941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1295d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhornstatic sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
12961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1297f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	struct swap_info_struct *sis;
1298f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	struct swap_extent *start_se;
1299f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	struct swap_extent *se;
1300f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	pgoff_t offset;
1301f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins
1302efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	sis = swap_info[swp_type(entry)];
1303f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	*bdev = sis->bdev;
1304f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins
1305f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	offset = swp_offset(entry);
1306f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	start_se = sis->curr_swap_extent;
1307f29ad6a99b596b8169744d107bf088e8be9e8d0dHugh Dickins	se = start_se;
13081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for ( ; ; ) {
13101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct list_head *lh;
13111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (se->start_page <= offset &&
13131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				offset < (se->start_page + se->nr_pages)) {
13141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return se->start_block + (offset - se->start_page);
13151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
131611d31886dbcb61039ed3789e583d21c6e70960fdHugh Dickins		lh = se->list.next;
13171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		se = list_entry(lh, struct swap_extent, list);
13181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sis->curr_swap_extent = se;
13191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		BUG_ON(se == start_se);		/* It *must* be present */
13201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1324d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry.
1325d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn */
1326d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhornsector_t map_swap_page(struct page *page, struct block_device **bdev)
1327d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn{
1328d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn	swp_entry_t entry;
1329d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn	entry.val = page_private(page);
1330d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn	return map_swap_entry(entry, bdev);
1331d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn}
1332d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn
1333d4906e1aa516cc965292b43b5a26122dd4344e7eLee Schermerhorn/*
13341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Free all of a swapdev's extent information
13351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
13361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void destroy_swap_extents(struct swap_info_struct *sis)
13371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13389625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	while (!list_empty(&sis->first_swap_extent.list)) {
13391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct swap_extent *se;
13401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13419625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		se = list_entry(sis->first_swap_extent.list.next,
13421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				struct swap_extent, list);
13431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		list_del(&se->list);
13441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kfree(se);
13451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
13491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's
135011d31886dbcb61039ed3789e583d21c6e70960fdHugh Dickins * extent list.  The extent list is kept sorted in page order.
13511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
135211d31886dbcb61039ed3789e583d21c6e70960fdHugh Dickins * This function rather assumes that it is called in ascending page order.
13531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
13541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int
13551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsadd_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
13561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned long nr_pages, sector_t start_block)
13571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
13581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct swap_extent *se;
13591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct swap_extent *new_se;
13601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct list_head *lh;
13611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13629625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	if (start_page == 0) {
13639625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		se = &sis->first_swap_extent;
13649625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		sis->curr_swap_extent = se;
13659625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		se->start_page = 0;
13669625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		se->nr_pages = nr_pages;
13679625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		se->start_block = start_block;
13689625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		return 1;
13699625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	} else {
13709625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
13711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		se = list_entry(lh, struct swap_extent, list);
137211d31886dbcb61039ed3789e583d21c6e70960fdHugh Dickins		BUG_ON(se->start_page + se->nr_pages != start_page);
137311d31886dbcb61039ed3789e583d21c6e70960fdHugh Dickins		if (se->start_block + se->nr_pages == start_block) {
13741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* Merge it */
13751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			se->nr_pages += nr_pages;
13761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return 0;
13771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
13781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
13811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * No merge.  Insert a new extent, preserving ordering.
13821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
13831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
13841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (new_se == NULL)
13851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
13861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_se->start_page = start_page;
13871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_se->nr_pages = nr_pages;
13881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_se->start_block = start_block;
13891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13909625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
139153092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	return 1;
13921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
13931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
13941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
13951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages
13961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * onto a contiguous range of disk blocks.  An ordered list of swap extents
13971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage
13981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * time for locating where on disk a page belongs.
13991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed.
14011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG
14021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * swap files identically.
14031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
14051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
14061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * swapfiles are handled *identically* after swapon time.
14071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
14091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
14101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment
14111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks
14121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * for swapping.
14131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
1414b0d9bcd4bb79a7834f8492f2ae5c2655a551f23dHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
14151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile,
14161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * which will scribble on the fs.
14171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
14181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The amount of disk space which a single swap extent represents varies.
14191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
14201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * extents in the list.  To avoid much list walking, we cache the previous
14211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * search location in `curr_swap_extent', and start new searches from there.
14221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This is extremely effective.  The average number of iterations in
14231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * map_swap_page() has been measured at about 0.3 per page.  - akpm.
14241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
142553092a7402f227151a681b0c92ec8598c5618b1aHugh Dickinsstatic int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
14261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
14271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct inode *inode;
14281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned blocks_per_page;
14291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long page_no;
14301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned blkbits;
14311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sector_t probe_block;
14321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sector_t last_block;
143353092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	sector_t lowest_block = -1;
143453092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	sector_t highest_block = 0;
143553092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	int nr_extents = 0;
14361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
14371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	inode = sis->swap_file->f_mapping->host;
14391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (S_ISBLK(inode->i_mode)) {
14401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		ret = add_swap_extent(sis, 0, sis->max, 0);
144153092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		*span = sis->pages;
14429625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins		goto out;
14431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
14441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	blkbits = inode->i_blkbits;
14461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	blocks_per_page = PAGE_SIZE >> blkbits;
14471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
14491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Map all the blocks into the extent list.  This code doesn't try
14501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * to be very smart.
14511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
14521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	probe_block = 0;
14531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	page_no = 0;
14541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	last_block = i_size_read(inode) >> blkbits;
14551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	while ((probe_block + blocks_per_page) <= last_block &&
14561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			page_no < sis->max) {
14571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		unsigned block_in_page;
14581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		sector_t first_block;
14591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		first_block = bmap(inode, probe_block);
14611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (first_block == 0)
14621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto bad_bmap;
14631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
14651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * It must be PAGE_SIZE aligned on-disk
14661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
14671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (first_block & (blocks_per_page - 1)) {
14681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			probe_block++;
14691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto reprobe;
14701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
14711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (block_in_page = 1; block_in_page < blocks_per_page;
14731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					block_in_page++) {
14741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			sector_t block;
14751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
14761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			block = bmap(inode, probe_block + block_in_page);
14771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (block == 0)
14781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto bad_bmap;
14791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (block != first_block + block_in_page) {
14801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				/* Discontiguity */
14811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				probe_block++;
14821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto reprobe;
14831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
14841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
14851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
148653092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		first_block >>= (PAGE_SHIFT - blkbits);
148753092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		if (page_no) {	/* exclude the header page */
148853092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins			if (first_block < lowest_block)
148953092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins				lowest_block = first_block;
149053092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins			if (first_block > highest_block)
149153092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins				highest_block = first_block;
149253092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		}
149353092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins
14941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/*
14951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
14961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 */
149753092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		ret = add_swap_extent(sis, page_no, 1, first_block);
149853092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		if (ret < 0)
14991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto out;
150053092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins		nr_extents += ret;
15011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page_no++;
15021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		probe_block += blocks_per_page;
15031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsreprobe:
15041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		continue;
15051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
150653092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	ret = nr_extents;
150753092a7402f227151a681b0c92ec8598c5618b1aHugh Dickins	*span = 1 + highest_block - lowest_block;
15081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (page_no == 0)
1509e2244ec2efa4ee1edf391d0001d314933e2b2974Hugh Dickins		page_no = 1;	/* force Empty message */
15101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sis->max = page_no;
1511e2244ec2efa4ee1edf391d0001d314933e2b2974Hugh Dickins	sis->pages = page_no - 1;
15121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	sis->highest_bit = page_no - 1;
15139625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickinsout:
15149625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	return ret;
15151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_bmap:
15161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swapon: swapfile has holes\n");
15171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = -EINVAL;
15189625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	goto out;
15191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
15201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
152140531542e2832419566c997af0808513f6f2815dCesar Eduardo Barrosstatic void enable_swap_info(struct swap_info_struct *p, int prio,
152240531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros				unsigned char *swap_map)
152340531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros{
152440531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	int i, prev;
152540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros
152640531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	spin_lock(&swap_lock);
152740531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	if (prio >= 0)
152840531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		p->prio = prio;
152940531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	else
153040531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		p->prio = --least_priority;
153140531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	p->swap_map = swap_map;
153240531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	p->flags |= SWP_WRITEOK;
153340531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	nr_swap_pages += p->pages;
153440531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	total_swap_pages += p->pages;
153540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros
153640531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	/* insert swap space into swap_list: */
153740531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	prev = -1;
153840531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
153940531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		if (p->prio >= swap_info[i]->prio)
154040531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros			break;
154140531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		prev = i;
154240531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	}
154340531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	p->next = i;
154440531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	if (prev < 0)
154540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		swap_list.head = swap_list.next = p->type;
154640531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	else
154740531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		swap_info[prev]->next = p->type;
154840531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	spin_unlock(&swap_lock);
154940531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros}
155040531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros
1551c4ea37c26a691ad0b7e86aa5884aab27830e95c9Heiko CarstensSYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
15521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
155373c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct swap_info_struct *p = NULL;
15548d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char *swap_map;
15551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct file *swap_file, *victim;
15561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct address_space *mapping;
15571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct inode *inode;
155873c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	char *pathname;
155972788c385604523422592249c19cba0187021e9bDavid Rientjes	int oom_score_adj;
15601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i, type, prev;
15611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err;
1562886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins
15631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!capable(CAP_SYS_ADMIN))
15641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EPERM;
15651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	pathname = getname(specialfile);
15671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = PTR_ERR(pathname);
15681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(pathname))
15691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
15701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
15721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	putname(pathname);
15731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = PTR_ERR(victim);
15741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(victim))
15751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out;
15761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mapping = victim->f_mapping;
15781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	prev = -1;
15795d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
1580efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1581efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		p = swap_info[type];
158222c6f8fdb31993cf49bdd4a47b64a7002391e1c7Hugh Dickins		if (p->flags & SWP_WRITEOK) {
15831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (p->swap_file->f_mapping == mapping)
15841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
15851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
15861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		prev = type;
15871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (type < 0) {
15891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = -EINVAL;
15905d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
15911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_dput;
15921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
15931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!security_vm_enough_memory(p->pages))
15941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		vm_unacct_memory(p->pages);
15951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else {
15961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = -ENOMEM;
15975d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
15981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_dput;
15991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1600efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	if (prev < 0)
16011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swap_list.head = p->next;
1602efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	else
1603efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		swap_info[prev]->next = p->next;
16041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (type == swap_list.next) {
16051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* just pick something that's safe... */
16061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swap_list.next = swap_list.head;
16071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
160878ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickins	if (p->prio < 0) {
1609efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		for (i = p->next; i >= 0; i = swap_info[i]->next)
1610efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			swap_info[i]->prio = p->prio--;
161178ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickins		least_priority++;
161278ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickins	}
16131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	nr_swap_pages -= p->pages;
16141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	total_swap_pages -= p->pages;
16151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->flags &= ~SWP_WRITEOK;
16165d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
1617fb4f88dcabdc716c7c350e09cf4a38a419b007e1Hugh Dickins
161872788c385604523422592249c19cba0187021e9bDavid Rientjes	oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
16191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = try_to_unuse(type);
162043362a4977e37db46f86f7e6ab935f0006956632David Rientjes	compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj);
16211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (err) {
162340531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		/*
162440531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		 * reading p->prio and p->swap_map outside the lock is
162540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		 * safe here because only sys_swapon and sys_swapoff
162640531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		 * change them, and there can be no other sys_swapon or
162740531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		 * sys_swapoff for this swap_info_struct at this point.
162840531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		 */
16291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* re-insert swap space back into swap_list */
163040531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		enable_swap_info(p, p->prio, p->swap_map);
16311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto out_dput;
16321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
163352b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins
16345d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	destroy_swap_extents(p);
1635570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (p->flags & SWP_CONTINUED)
1636570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		free_swap_count_continuations(p);
1637570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
1638fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_lock(&swapon_mutex);
16395d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
16405d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	drain_mmlist();
16415d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins
164252b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	/* wait for anyone still in scan_swap_map */
164352b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	p->highest_bit = 0;		/* cuts scans short */
164452b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	while (p->flags >= SWP_SCANNING) {
16455d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
164613e4b57f6a4e23ceb99794a650d777e74831f4a6Nishanth Aravamudan		schedule_timeout_uninterruptible(1);
16475d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_lock(&swap_lock);
164852b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins	}
164952b7efdbe5f5696fc80338560a3fc51e0b0a993cHugh Dickins
16501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swap_file = p->swap_file;
16511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->swap_file = NULL;
16521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->max = 0;
16531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swap_map = p->swap_map;
16541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->swap_map = NULL;
16551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->flags = 0;
16565d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
1657fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_unlock(&swapon_mutex);
16581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	vfree(swap_map);
165927a7faa0779dd13729196c1a818c294f44bbd1eeKAMEZAWA Hiroyuki	/* Destroy swap account informatin */
166027a7faa0779dd13729196c1a818c294f44bbd1eeKAMEZAWA Hiroyuki	swap_cgroup_swapoff(type);
166127a7faa0779dd13729196c1a818c294f44bbd1eeKAMEZAWA Hiroyuki
16621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	inode = mapping->host;
16631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (S_ISBLK(inode->i_mode)) {
16641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		struct block_device *bdev = I_BDEV(inode);
16651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		set_blocksize(bdev, p->old_block_size);
1666e525fd89d380c4a94c0d63913a1dd1a593ed25e7Tejun Heo		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
16671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else {
16681b1dcc1b57a49136f118a0f16367256ff9994a69Jes Sorensen		mutex_lock(&inode->i_mutex);
16691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		inode->i_flags &= ~S_SWAPFILE;
16701b1dcc1b57a49136f118a0f16367256ff9994a69Jes Sorensen		mutex_unlock(&inode->i_mutex);
16711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	filp_close(swap_file, NULL);
16731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = 0;
167466d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	atomic_inc(&proc_poll_event);
167566d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	wake_up_interruptible(&proc_poll_wait);
16761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout_dput:
16781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	filp_close(victim, NULL);
16791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
16801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
16811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
16821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
16831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#ifdef CONFIG_PROC_FS
168466d7dd518ae413a383ab2c6c263cc30617329842Kay Sieversstatic unsigned swaps_poll(struct file *file, poll_table *wait)
168566d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers{
1686f15146380d28b746df3c8b81b392812eb982382aKay Sievers	struct seq_file *seq = file->private_data;
168766d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
168866d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	poll_wait(file, &proc_poll_wait, wait);
168966d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
1690f15146380d28b746df3c8b81b392812eb982382aKay Sievers	if (seq->poll_event != atomic_read(&proc_poll_event)) {
1691f15146380d28b746df3c8b81b392812eb982382aKay Sievers		seq->poll_event = atomic_read(&proc_poll_event);
169266d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers		return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
169366d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	}
169466d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
169566d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	return POLLIN | POLLRDNORM;
169666d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers}
169766d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
16981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* iterator */
16991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void *swap_start(struct seq_file *swap, loff_t *pos)
17001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1701efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	struct swap_info_struct *si;
1702efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	int type;
17031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loff_t l = *pos;
17041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1705fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_lock(&swapon_mutex);
17061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1707881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal	if (!l)
1708881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal		return SEQ_START_TOKEN;
1709881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal
1710efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = 0; type < nr_swapfiles; type++) {
1711efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1712efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		si = swap_info[type];
1713efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (!(si->flags & SWP_USED) || !si->swap_map)
17141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
1715881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal		if (!--l)
1716efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			return si;
17171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
17181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
17201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
17231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1724efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	struct swap_info_struct *si = v;
1725efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	int type;
17261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1727881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal	if (v == SEQ_START_TOKEN)
1728efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		type = 0;
1729efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	else
1730efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		type = si->type + 1;
1731881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal
1732efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (; type < nr_swapfiles; type++) {
1733efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1734efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		si = swap_info[type];
1735efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (!(si->flags & SWP_USED) || !si->swap_map)
17361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
17371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		++*pos;
1738efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		return si;
17391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
17401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
17421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void swap_stop(struct seq_file *swap, void *v)
17451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1746fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_unlock(&swapon_mutex);
17471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int swap_show(struct seq_file *swap, void *v)
17501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1751efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	struct swap_info_struct *si = v;
17521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct file *file;
17531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int len;
17541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1755efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	if (si == SEQ_START_TOKEN) {
1756881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1757881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal		return 0;
1758881e4aabe4c6b3fce93674b67c6adc646fe2683bSuleiman Souhlal	}
17591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1760efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	file = si->swap_file;
1761c32c2f63a9d6c953aaf168c0b2551da9734f76d2Jan Blunck	len = seq_path(swap, &file->f_path, " \t\n\\");
17626eb396dc4a9781c5e7951143ab56ce5710687ab3Hugh Dickins	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
1763886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins			len < 40 ? 40 - len : 1, " ",
1764886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins			S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
17651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				"partition" : "file\t",
1766efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			si->pages << (PAGE_SHIFT - 10),
1767efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			si->inuse_pages << (PAGE_SHIFT - 10),
1768efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			si->prio);
17691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
17701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
177215ad7cdcfd76450d4beebc789ec646664238184dHelge Dellerstatic const struct seq_operations swaps_op = {
17731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.start =	swap_start,
17741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.next =		swap_next,
17751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.stop =		swap_stop,
17761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.show =		swap_show
17771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
17781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
17791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int swaps_open(struct inode *inode, struct file *file)
17801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1781f15146380d28b746df3c8b81b392812eb982382aKay Sievers	struct seq_file *seq;
178266d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	int ret;
178366d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
178466d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	ret = seq_open(file, &swaps_op);
1785f15146380d28b746df3c8b81b392812eb982382aKay Sievers	if (ret)
178666d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers		return ret;
178766d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
1788f15146380d28b746df3c8b81b392812eb982382aKay Sievers	seq = file->private_data;
1789f15146380d28b746df3c8b81b392812eb982382aKay Sievers	seq->poll_event = atomic_read(&proc_poll_event);
1790f15146380d28b746df3c8b81b392812eb982382aKay Sievers	return 0;
17911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
17921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
179315ad7cdcfd76450d4beebc789ec646664238184dHelge Dellerstatic const struct file_operations proc_swaps_operations = {
17941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.open		= swaps_open,
17951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read		= seq_read,
17961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.llseek		= seq_lseek,
17971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.release	= seq_release,
179866d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	.poll		= swaps_poll,
17991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
18001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init procswaps_init(void)
18021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
18033d71f86f4dfccd749e4421f10301f3f3b31da88aDenis V. Lunev	proc_create("swaps", 0, NULL, &proc_swaps_operations);
18041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
18051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
18061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds__initcall(procswaps_init);
18071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif /* CONFIG_PROC_FS */
18081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
18091796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich#ifdef MAX_SWAPFILES_CHECK
18101796316a8b028a148be48ba5d4e7be493a39d173Jan Beulichstatic int __init max_swapfiles_check(void)
18111796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich{
18121796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich	MAX_SWAPFILES_CHECK();
18131796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich	return 0;
18141796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich}
18151796316a8b028a148be48ba5d4e7be493a39d173Jan Beulichlate_initcall(max_swapfiles_check);
18161796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich#endif
18171796316a8b028a148be48ba5d4e7be493a39d173Jan Beulich
181853cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barrosstatic struct swap_info_struct *alloc_swap_info(void)
18191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
182073c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct swap_info_struct *p;
18211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int type;
1822efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins
1823efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	p = kzalloc(sizeof(*p), GFP_KERNEL);
1824efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	if (!p)
182553cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros		return ERR_PTR(-ENOMEM);
1826efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins
18275d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
1828efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = 0; type < nr_swapfiles; type++) {
1829efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if (!(swap_info[type]->flags & SWP_USED))
18301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1831efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	}
18320697212a411c1dae03c27845f2de2f3adb32c331Christoph Lameter	if (type >= MAX_SWAPFILES) {
18335d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins		spin_unlock(&swap_lock);
1834efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		kfree(p);
1835730c0581c82dbc5be2f41a2d85bde6bad11bc8a4Cesar Eduardo Barros		return ERR_PTR(-EPERM);
18361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1837efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	if (type >= nr_swapfiles) {
1838efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		p->type = type;
1839efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		swap_info[type] = p;
1840efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		/*
1841efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 * Write swap_info[type] before nr_swapfiles, in case a
1842efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 * racing procfs swap_start() or swap_next() is reading them.
1843efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 * (We never shrink nr_swapfiles, we never free this entry.)
1844efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 */
1845efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		smp_wmb();
1846efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		nr_swapfiles++;
1847efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	} else {
1848efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		kfree(p);
1849efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		p = swap_info[type];
1850efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		/*
1851efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 * Do not memset this entry: a racing procfs swap_next()
1852efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 * would be relying on p->type to remain valid.
1853efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		 */
1854efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	}
18559625a5f289f7c3c100b59c317e2bcc3c7e2e51fbHugh Dickins	INIT_LIST_HEAD(&p->first_swap_extent.list);
18561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->flags = SWP_USED;
18571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->next = -1;
18585d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
1859efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins
186053cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	return p;
186153cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros}
186253cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros
18634d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barrosstatic int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
18644d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros{
18654d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	int error;
18664d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros
18674d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	if (S_ISBLK(inode->i_mode)) {
18684d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		p->bdev = bdgrab(I_BDEV(inode));
18694d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		error = blkdev_get(p->bdev,
18704d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros				   FMODE_READ | FMODE_WRITE | FMODE_EXCL,
18714d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros				   sys_swapon);
18724d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		if (error < 0) {
18734d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros			p->bdev = NULL;
187487ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros			return -EINVAL;
18754d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		}
18764d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		p->old_block_size = block_size(p->bdev);
18774d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		error = set_blocksize(p->bdev, PAGE_SIZE);
18784d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		if (error < 0)
187987ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros			return error;
18804d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		p->flags |= SWP_BLKDEV;
18814d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	} else if (S_ISREG(inode->i_mode)) {
18824d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		p->bdev = inode->i_sb->s_bdev;
18834d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros		mutex_lock(&inode->i_mutex);
188487ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros		if (IS_SWAPFILE(inode))
188587ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros			return -EBUSY;
188687ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros	} else
188787ade72a799a9a895b4a60918c32b43d3dfc3324Cesar Eduardo Barros		return -EINVAL;
18884d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros
18894d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	return 0;
18904d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros}
18914d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros
1892ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barrosstatic unsigned long read_swap_header(struct swap_info_struct *p,
1893ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros					union swap_header *swap_header,
1894ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros					struct inode *inode)
1895ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros{
1896ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	int i;
1897ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	unsigned long maxpages;
1898ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	unsigned long swapfilepages;
1899ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1900ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1901ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		printk(KERN_ERR "Unable to find swap-space signature\n");
190238719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1903ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	}
1904ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1905ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	/* swap partition endianess hack... */
1906ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (swab32(swap_header->info.version) == 1) {
1907ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		swab32s(&swap_header->info.version);
1908ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		swab32s(&swap_header->info.last_page);
1909ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		swab32s(&swap_header->info.nr_badpages);
1910ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		for (i = 0; i < swap_header->info.nr_badpages; i++)
1911ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros			swab32s(&swap_header->info.badpages[i]);
1912ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	}
1913ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	/* Check the swap header's sub-version */
1914ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (swap_header->info.version != 1) {
1915ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		printk(KERN_WARNING
1916ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		       "Unable to handle swap header version %d\n",
1917ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		       swap_header->info.version);
191838719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1919ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	}
1920ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1921ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	p->lowest_bit  = 1;
1922ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	p->cluster_next = 1;
1923ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	p->cluster_nr = 0;
1924ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1925ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	/*
1926ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 * Find out how many pages are allowed for a single swap
1927a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * device. There are three limiting factors: 1) the number
1928a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * of bits for the swap offset in the swp_entry_t type, and
1929a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * 2) the number of bits in the swap pte as defined by the
1930a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * the different architectures, and 3) the number of free bits
1931a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * in an exceptional radix_tree entry. In order to find the
1932a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * largest possible bit mask, a swap entry with swap type 0
1933ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 * and swap offset ~0UL is created, encoded to a swap pte,
1934a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * decoded to a swp_entry_t again, and finally the swap
1935ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 * offset is extracted. This will mask all the bits from
1936ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 * the initial ~0UL mask that can't be encoded in either
1937ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 * the swp_entry_t or the architecture definition of a
1938a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	 * swap pte.  Then the same is done for a radix_tree entry.
1939ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	 */
1940ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	maxpages = swp_offset(pte_to_swp_entry(
1941a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins			swp_entry_to_pte(swp_entry(0, ~0UL))));
1942a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins	maxpages = swp_offset(radix_to_swp_entry(
1943a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins			swp_to_radix_entry(swp_entry(0, maxpages)))) + 1;
1944a2c16d6cb0e478812829ca84aeabd02e36af35ebHugh Dickins
1945ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (maxpages > swap_header->info.last_page) {
1946ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		maxpages = swap_header->info.last_page + 1;
1947ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		/* p->max is an unsigned int: don't overflow it */
1948ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		if ((unsigned int)maxpages == 0)
1949ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros			maxpages = UINT_MAX;
1950ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	}
1951ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	p->highest_bit = maxpages - 1;
1952ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1953ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (!maxpages)
195438719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1955ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
1956ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (swapfilepages && maxpages > swapfilepages) {
1957ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		printk(KERN_WARNING
1958ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros		       "Swap area shorter than signature indicates\n");
195938719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1960ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	}
1961ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
196238719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1963ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
196438719025384cf7121331bd6d41c062d3c5f7bb91Cesar Eduardo Barros		return 0;
1965ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1966ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	return maxpages;
1967ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros}
1968ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros
1969915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barrosstatic int setup_swap_map_and_extents(struct swap_info_struct *p,
1970915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros					union swap_header *swap_header,
1971915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros					unsigned char *swap_map,
1972915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros					unsigned long maxpages,
1973915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros					sector_t *span)
1974915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros{
1975915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	int i;
1976915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	unsigned int nr_good_pages;
1977915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	int nr_extents;
1978915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros
1979915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	nr_good_pages = maxpages - 1;	/* omit header page */
1980915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros
1981915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	for (i = 0; i < swap_header->info.nr_badpages; i++) {
1982915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		unsigned int page_nr = swap_header->info.badpages[i];
1983bdb8e3f68320f897de3f3a4c363fe2802037f21dCesar Eduardo Barros		if (page_nr == 0 || page_nr > swap_header->info.last_page)
1984bdb8e3f68320f897de3f3a4c363fe2802037f21dCesar Eduardo Barros			return -EINVAL;
1985915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		if (page_nr < maxpages) {
1986915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros			swap_map[page_nr] = SWAP_MAP_BAD;
1987915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros			nr_good_pages--;
1988915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		}
1989915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	}
1990915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros
1991915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	if (nr_good_pages) {
1992915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		swap_map[0] = SWAP_MAP_BAD;
1993915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		p->max = maxpages;
1994915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		p->pages = nr_good_pages;
1995915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		nr_extents = setup_swap_extents(p, span);
1996bdb8e3f68320f897de3f3a4c363fe2802037f21dCesar Eduardo Barros		if (nr_extents < 0)
1997bdb8e3f68320f897de3f3a4c363fe2802037f21dCesar Eduardo Barros			return nr_extents;
1998915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		nr_good_pages = p->pages;
1999915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	}
2000915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	if (!nr_good_pages) {
2001915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		printk(KERN_WARNING "Empty swap-file\n");
2002bdb8e3f68320f897de3f3a4c363fe2802037f21dCesar Eduardo Barros		return -EINVAL;
2003915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	}
2004915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros
2005915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	return nr_extents;
2006915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros}
2007915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros
200853cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo BarrosSYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
200953cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros{
201053cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	struct swap_info_struct *p;
201128b36bd741bd44db30e12b1048a659aa346e9b76Cesar Eduardo Barros	char *name;
201253cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	struct file *swap_file = NULL;
201353cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	struct address_space *mapping;
201440531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	int i;
201540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	int prio;
201653cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	int error;
201753cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	union swap_header *swap_header;
2018915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	int nr_extents;
201953cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	sector_t span;
202053cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	unsigned long maxpages;
202153cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	unsigned char *swap_map = NULL;
202253cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	struct page *page = NULL;
202353cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	struct inode *inode = NULL;
202453cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros
202553cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	if (!capable(CAP_SYS_ADMIN))
202653cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros		return -EPERM;
202753cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros
202853cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros	p = alloc_swap_info();
20292542e5134d2c19a9e6a4e641ef78cac6bccebd9bCesar Eduardo Barros	if (IS_ERR(p))
20302542e5134d2c19a9e6a4e641ef78cac6bccebd9bCesar Eduardo Barros		return PTR_ERR(p);
203153cbb2435f161f2a8b36af8f6d2c46dc59d0d757Cesar Eduardo Barros
20321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	name = getname(specialfile);
20331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(name)) {
20347de7fb6b3422e6d1dac00666a992834085e745dcCesar Eduardo Barros		error = PTR_ERR(name);
20351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		name = NULL;
2036bd69010b042a60ca41a890df1b10019e94746c2fCesar Eduardo Barros		goto bad_swap;
20371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
20391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(swap_file)) {
20407de7fb6b3422e6d1dac00666a992834085e745dcCesar Eduardo Barros		error = PTR_ERR(swap_file);
20411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		swap_file = NULL;
2042bd69010b042a60ca41a890df1b10019e94746c2fCesar Eduardo Barros		goto bad_swap;
20431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->swap_file = swap_file;
20461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mapping = swap_file->f_mapping;
20471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < nr_swapfiles; i++) {
2049efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		struct swap_info_struct *q = swap_info[i];
20501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2051e8e6c2ec403ecfaa226857d8204344c98fe12b7bCesar Eduardo Barros		if (q == p || !q->swap_file)
20521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
20537de7fb6b3422e6d1dac00666a992834085e745dcCesar Eduardo Barros		if (mapping == q->swap_file->f_mapping) {
20547de7fb6b3422e6d1dac00666a992834085e745dcCesar Eduardo Barros			error = -EBUSY;
20551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto bad_swap;
20567de7fb6b3422e6d1dac00666a992834085e745dcCesar Eduardo Barros		}
20571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
20581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20592130781e2aaab66e5a9f2fdc8af35da0153f405cCesar Eduardo Barros	inode = mapping->host;
20602130781e2aaab66e5a9f2fdc8af35da0153f405cCesar Eduardo Barros	/* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
20614d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	error = claim_swapfile(p, inode);
20624d0e1e10752ca487d83d1ab2a1d4ae6d664e1cc0Cesar Eduardo Barros	if (unlikely(error))
20631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_swap;
20641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
20661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Read the swap header.
20671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
20681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!mapping->a_ops->readpage) {
20691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		error = -EINVAL;
20701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_swap;
20711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2072090d2b185d8680fc26a2eaf4245d4171dcf4baf1Pekka Enberg	page = read_mapping_page(mapping, 0, swap_file);
20731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (IS_ERR(page)) {
20741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		error = PTR_ERR(page);
20751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_swap;
20761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
207781e33971271ec8603fe696731ff9967afb99e729Hugh Dickins	swap_header = kmap(page);
20781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2079ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	maxpages = read_swap_header(p, swap_header, inode);
2080ca8bd38bf6f05481c4155fc444178151884f65d0Cesar Eduardo Barros	if (unlikely(!maxpages)) {
20811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		error = -EINVAL;
20821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_swap;
20831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2084886bb7e9c3ed0bb3e4a2b1f336d8c6a6e5a4b782Hugh Dickins
208581e33971271ec8603fe696731ff9967afb99e729Hugh Dickins	/* OK, set up the swap map and apply the bad block list */
2086803d0c8351b47b72b8b018457a33b342557b90a2Cesar Eduardo Barros	swap_map = vzalloc(maxpages);
208781e33971271ec8603fe696731ff9967afb99e729Hugh Dickins	if (!swap_map) {
208881e33971271ec8603fe696731ff9967afb99e729Hugh Dickins		error = -ENOMEM;
208981e33971271ec8603fe696731ff9967afb99e729Hugh Dickins		goto bad_swap;
209081e33971271ec8603fe696731ff9967afb99e729Hugh Dickins	}
20911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
20921421ef3cd15b87ef949e965efeb1e527479d3f75Cesar Eduardo Barros	error = swap_cgroup_swapon(p->type, maxpages);
20931421ef3cd15b87ef949e965efeb1e527479d3f75Cesar Eduardo Barros	if (error)
20941421ef3cd15b87ef949e965efeb1e527479d3f75Cesar Eduardo Barros		goto bad_swap;
20951421ef3cd15b87ef949e965efeb1e527479d3f75Cesar Eduardo Barros
2096915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
2097915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		maxpages, &span);
2098915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros	if (unlikely(nr_extents < 0)) {
2099915d4d7bc0d719f2f0907273c01967d38751c625Cesar Eduardo Barros		error = nr_extents;
21001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_swap;
21011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21033bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman	if (p->bdev) {
21043bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
21053bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman			p->flags |= SWP_SOLIDSTATE;
21063bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman			p->cluster_next = 1 + (random32() % p->highest_bit);
21073bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman		}
21083399446632739fcd05fd8b272b476a69c6e6d14aHugh Dickins		if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
21093bd0f0c763e497c8674b28e3df2732f48683dabdSuresh Jayaraman			p->flags |= SWP_DISCARDABLE;
211020137a490f397d9c01fc9fadd83a8d198bda4477Hugh Dickins	}
21116a6ba83175c029c7820765bae44692266b29e67aHugh Dickins
2112fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_lock(&swapon_mutex);
211340531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	prio = -1;
211478ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickins	if (swap_flags & SWAP_FLAG_PREFER)
211540531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros		prio =
211678ecba081224a2db5876b6b81cfed0b78f58adc7Hugh Dickins		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
211740531542e2832419566c997af0808513f6f2815dCesar Eduardo Barros	enable_swap_info(p, prio, swap_map);
2118c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros
2119c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros	printk(KERN_INFO "Adding %uk swap on %s.  "
2120c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros			"Priority:%d extents:%d across:%lluk %s%s\n",
2121c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros		p->pages<<(PAGE_SHIFT-10), name, p->prio,
2122c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
2123c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2124c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros		(p->flags & SWP_DISCARDABLE) ? "D" : "");
2125c69dbfb84e88503468b6c481aecdb48d76ad5bc6Cesar Eduardo Barros
2126fc0abb1451c64c79ac80665d5ba74450ce274e4dIngo Molnar	mutex_unlock(&swapon_mutex);
212766d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	atomic_inc(&proc_poll_event);
212866d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers	wake_up_interruptible(&proc_poll_wait);
212966d7dd518ae413a383ab2c6c263cc30617329842Kay Sievers
21309b01c350af4fb00fe2ab66ff9bf16058c50b69bdCesar Eduardo Barros	if (S_ISREG(inode->i_mode))
21319b01c350af4fb00fe2ab66ff9bf16058c50b69bdCesar Eduardo Barros		inode->i_flags |= S_SWAPFILE;
21321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	error = 0;
21331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	goto out;
21341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_swap:
2135bd69010b042a60ca41a890df1b10019e94746c2fCesar Eduardo Barros	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
2136f2090d2df51d7cdb2f952dcfdcd8baaac0aec444Cesar Eduardo Barros		set_blocksize(p->bdev, p->old_block_size);
2137f2090d2df51d7cdb2f952dcfdcd8baaac0aec444Cesar Eduardo Barros		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
21381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21394cd3bb10ff0b21b77b5a4cd13b4bd36694e054c4Hugh Dickins	destroy_swap_extents(p);
2140e8e6c2ec403ecfaa226857d8204344c98fe12b7bCesar Eduardo Barros	swap_cgroup_swapoff(p->type);
21415d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
21421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->swap_file = NULL;
21431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	p->flags = 0;
21445d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
21451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	vfree(swap_map);
214652c50567d8ab0a0a87f12cceaa4194967854f0bdMel Gorman	if (swap_file) {
21472130781e2aaab66e5a9f2fdc8af35da0153f405cCesar Eduardo Barros		if (inode && S_ISREG(inode->i_mode)) {
214852c50567d8ab0a0a87f12cceaa4194967854f0bdMel Gorman			mutex_unlock(&inode->i_mutex);
21492130781e2aaab66e5a9f2fdc8af35da0153f405cCesar Eduardo Barros			inode = NULL;
21502130781e2aaab66e5a9f2fdc8af35da0153f405cCesar Eduardo Barros		}
21511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		filp_close(swap_file, NULL);
215252c50567d8ab0a0a87f12cceaa4194967854f0bdMel Gorman	}
21531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
21541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (page && !IS_ERR(page)) {
21551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		kunmap(page);
21561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		page_cache_release(page);
21571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (name)
21591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		putname(name);
21609b01c350af4fb00fe2ab66ff9bf16058c50b69bdCesar Eduardo Barros	if (inode && S_ISREG(inode->i_mode))
21611b1dcc1b57a49136f118a0f16367256ff9994a69Jes Sorensen		mutex_unlock(&inode->i_mutex);
21621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return error;
21631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid si_swapinfo(struct sysinfo *val)
21661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2167efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	unsigned int type;
21681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long nr_to_be_unused = 0;
21691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21705d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
2171efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	for (type = 0; type < nr_swapfiles; type++) {
2172efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		struct swap_info_struct *si = swap_info[type];
2173efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins
2174efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2175efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins			nr_to_be_unused += si->inuse_pages;
21761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
21771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val->freeswap = nr_swap_pages + nr_to_be_unused;
21781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val->totalswap = total_swap_pages + nr_to_be_unused;
21795d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
21801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
21811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
21821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Verify that a swap entry is valid and increment its swap map count.
21841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
2185355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * Returns error code in following case.
2186355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * - success -> 0
2187355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL
2188355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL
2189355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST
2190355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT
2191570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
21921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
21938d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickinsstatic int __swap_duplicate(swp_entry_t entry, unsigned char usage)
21941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
219573c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins	struct swap_info_struct *p;
21961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long offset, type;
21978d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char count;
21988d69aaee80c123b460918816cbfa2e83224c3646Hugh Dickins	unsigned char has_cache;
2199253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	int err = -EINVAL;
22001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2201a7420aa54dbf699a5a05feba3c859b6baaa3938cAndi Kleen	if (non_swap_entry(entry))
2202253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		goto out;
22030697212a411c1dae03c27845f2de2f3adb32c331Christoph Lameter
22041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	type = swp_type(entry);
22051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (type >= nr_swapfiles)
22061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto bad_file;
2207efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	p = swap_info[type];
22081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	offset = swp_offset(entry);
22091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22105d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
2211355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	if (unlikely(offset >= p->max))
2212355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		goto unlock_out;
2213355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
2214253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	count = p->swap_map[offset];
2215253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	has_cache = count & SWAP_HAS_CACHE;
2216253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	count &= ~SWAP_HAS_CACHE;
2217253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	err = 0;
2218355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
2219253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	if (usage == SWAP_HAS_CACHE) {
2220355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
2221355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2222253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		if (!has_cache && count)
2223253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins			has_cache = SWAP_HAS_CACHE;
2224253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		else if (has_cache)		/* someone else added cache */
2225253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins			err = -EEXIST;
2226253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		else				/* no users remaining */
2227253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins			err = -ENOENT;
2228355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki
2229355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	} else if (count || has_cache) {
2230253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
2231570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2232570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			count += usage;
2233570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2234253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins			err = -EINVAL;
2235570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		else if (swap_count_continued(p, offset, count))
2236570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			count = COUNT_CONTINUED;
2237570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		else
2238570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			err = -ENOMEM;
2239355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki	} else
2240253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins		err = -ENOENT;			/* unused swap entry */
2241253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
2242253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	p->swap_map[offset] = count | has_cache;
2243253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
2244355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyukiunlock_out:
22455d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
22461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsout:
2247253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	return err;
22481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsbad_file:
22501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
22511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	goto out;
22521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2253253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins
2254355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki/*
2255aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2256aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins * (in which case its reference count is never incremented).
2257aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins */
2258aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickinsvoid swap_shmem_alloc(swp_entry_t entry)
2259aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins{
2260aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins	__swap_duplicate(entry, SWAP_MAP_SHMEM);
2261aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins}
2262aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins
2263aaa468653b4a0d11c603c48d716f765177a5a9e4Hugh Dickins/*
226408259d58e4fa12ceaece82193c5816152f638ccaHugh Dickins * Increase reference count of swap entry by 1.
226508259d58e4fa12ceaece82193c5816152f638ccaHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
226608259d58e4fa12ceaece82193c5816152f638ccaHugh Dickins * but could not be atomically allocated.  Returns 0, just as if it succeeded,
226708259d58e4fa12ceaece82193c5816152f638ccaHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
226808259d58e4fa12ceaece82193c5816152f638ccaHugh Dickins * might occur if a page table entry has got corrupted.
2269355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki */
2270570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsint swap_duplicate(swp_entry_t entry)
2271355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki{
2272570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	int err = 0;
2273570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2274570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2275570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2276570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	return err;
2277355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki}
22781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2279cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki/*
2280355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache.
2281355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki *
228273c34b6accc8427584f5d7db4d5acb230ed8c912Hugh Dickins * Called when allocating swap cache for existing swap entry,
2283355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success.
2284355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache.
2285355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate().
2286cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki */
2287cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyukiint swapcache_prepare(swp_entry_t entry)
2288cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki{
2289253d553ba75ab26b3e9e2f70cbf6fbf0813f7e86Hugh Dickins	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2290cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki}
2291cb4b86ba47bb0937b71fb825b3ed88adf7a190f0KAMEZAWA Hiroyuki
22921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
22935d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra
22941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused.
22951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
22961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint valid_swaphandles(swp_entry_t entry, unsigned long *offset)
22971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
22988952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	struct swap_info_struct *si;
22993f9e7949f86dfe2bd9a1ad0604f78e7683c059deHugh Dickins	int our_page_cluster = page_cluster;
23008952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	pgoff_t target, toff;
23018952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	pgoff_t base, end;
23028952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	int nr_pages = 0;
23031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23043f9e7949f86dfe2bd9a1ad0604f78e7683c059deHugh Dickins	if (!our_page_cluster)	/* no readahead */
23051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
23068952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins
2307efa90a981bbc891efad96db2a75b5487e00852caHugh Dickins	si = swap_info[swp_type(entry)];
23088952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	target = swp_offset(entry);
23098952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	base = (target >> our_page_cluster) << our_page_cluster;
23108952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	end = base + (1 << our_page_cluster);
23118952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	if (!base)		/* first page is swap header */
23128952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins		base++;
23131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
23145d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_lock(&swap_lock);
23158952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	if (end > si->max)	/* don't go beyond end of map */
23168952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins		end = si->max;
23178952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins
23188952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	/* Count contiguous allocated slots above our target */
23198952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	for (toff = target; ++toff < end; nr_pages++) {
23208952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins		/* Don't read in free or bad pages */
23218952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins		if (!si->swap_map[toff])
23228952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins			break;
2323355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
23241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
23258952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	}
23268952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	/* Count contiguous allocated slots below our target */
23278952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	for (toff = target; --toff >= base; nr_pages++) {
23281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Don't read in free or bad pages */
23298952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins		if (!si->swap_map[toff])
23301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2331355cfa73ddff2fb8fa14e93bd94a057cc022512eKAMEZAWA Hiroyuki		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
23321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
23338952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	}
23345d337b9194b1ce3b6fd5f3cb2799455ed2f9a3d1Hugh Dickins	spin_unlock(&swap_lock);
23358952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins
23368952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	/*
23378952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	 * Indicate starting offset, and return number of pages to get:
23388952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	 * if only 1, say 0, since there's then no readahead to be done.
23398952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	 */
23408952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	*offset = ++toff;
23418952898b0d25223f38daf46b86156fd1c4d17ad0Hugh Dickins	return nr_pages? ++nr_pages: 0;
23421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2343570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2344570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins/*
2345570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated
2346570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2347570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count
2348570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2349570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2350570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins *
2351570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * These continuation pages are seldom referenced: the common paths all work
2352570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * on the original swap_map, only referring to a continuation page when the
2353570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2354570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins *
2355570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2356570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2357570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * can be called after dropping locks.
2358570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins */
2359570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsint add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2360570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins{
2361570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct swap_info_struct *si;
2362570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct page *head;
2363570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct page *page;
2364570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct page *list_page;
2365570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	pgoff_t offset;
2366570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	unsigned char count;
2367570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2368570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	/*
2369570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2370570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2371570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 */
2372570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2373570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2374570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	si = swap_info_get(entry);
2375570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (!si) {
2376570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2377570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * An acceptable race has occurred since the failing
2378570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * __swap_duplicate(): the swap entry has been freed,
2379570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * perhaps even the whole swap_map cleared for swapoff.
2380570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2381570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		goto outer;
2382570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2383570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2384570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	offset = swp_offset(entry);
2385570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2386570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2387570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2388570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2389570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * The higher the swap count, the more likely it is that tasks
2390570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * will race to add swap count continuation: we need to avoid
2391570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * over-provisioning.
2392570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2393570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		goto out;
2394570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2395570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2396570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (!page) {
2397570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		spin_unlock(&swap_lock);
2398570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		return -ENOMEM;
2399570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2400570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2401570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	/*
2402570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2403570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * no architecture is using highmem pages for kernel pagetables: so it
2404570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2405570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 */
2406570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	head = vmalloc_to_page(si->swap_map + offset);
2407570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	offset &= ~PAGE_MASK;
2408570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2409570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	/*
2410570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * Page allocation does not initialize the page's lru field,
2411570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 * but it does always reset its private field.
2412570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	 */
2413570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (!page_private(head)) {
2414570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		BUG_ON(count & COUNT_CONTINUED);
2415570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		INIT_LIST_HEAD(&head->lru);
2416570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		set_page_private(head, SWP_CONTINUED);
2417570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		si->flags |= SWP_CONTINUED;
2418570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2419570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2420570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	list_for_each_entry(list_page, &head->lru, lru) {
2421570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		unsigned char *map;
2422570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2423570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2424570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * If the previous map said no continuation, but we've found
2425570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * a continuation page, free our allocation and use this one.
2426570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2427570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if (!(count & COUNT_CONTINUED))
2428570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			goto out;
2429570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2430570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		map = kmap_atomic(list_page, KM_USER0) + offset;
2431570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		count = *map;
2432570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		kunmap_atomic(map, KM_USER0);
2433570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2434570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2435570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * If this continuation count now has some space in it,
2436570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * free our allocation and use this one.
2437570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2438570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2439570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			goto out;
2440570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2441570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2442570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	list_add_tail(&page->lru, &head->lru);
2443570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	page = NULL;			/* now it's attached, don't free it */
2444570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsout:
2445570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	spin_unlock(&swap_lock);
2446570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsouter:
2447570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (page)
2448570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		__free_page(page);
2449570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	return 0;
2450570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins}
2451570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2452570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins/*
2453570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * swap_count_continued - when the original swap_map count is incremented
2454570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2455570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated;
2456570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * when the original swap_map count is decremented from 0 with continuation,
2457570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * borrow from the continuation and report whether it still holds more.
2458570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2459570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins */
2460570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsstatic bool swap_count_continued(struct swap_info_struct *si,
2461570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				 pgoff_t offset, unsigned char count)
2462570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins{
2463570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct page *head;
2464570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	struct page *page;
2465570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	unsigned char *map;
2466570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2467570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	head = vmalloc_to_page(si->swap_map + offset);
2468570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (page_private(head) != SWP_CONTINUED) {
2469570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		BUG_ON(count & COUNT_CONTINUED);
2470570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		return false;		/* need to add count continuation */
2471570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2472570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2473570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	offset &= ~PAGE_MASK;
2474570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	page = list_entry(head->lru.next, struct page, lru);
2475570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	map = kmap_atomic(page, KM_USER0) + offset;
2476570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2477570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2478570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2479570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2480570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2481570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2482570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * Think of how you add 1 to 999
2483570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2484570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
2485570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			kunmap_atomic(map, KM_USER0);
2486570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			page = list_entry(page->lru.next, struct page, lru);
2487570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			BUG_ON(page == head);
2488570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			map = kmap_atomic(page, KM_USER0) + offset;
2489570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2490570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if (*map == SWAP_CONT_MAX) {
2491570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			kunmap_atomic(map, KM_USER0);
2492570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			page = list_entry(page->lru.next, struct page, lru);
2493570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			if (page == head)
2494570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				return false;	/* add count continuation */
2495570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			map = kmap_atomic(page, KM_USER0) + offset;
2496570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsinit_map:		*map = 0;		/* we didn't zero the page */
2497570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2498570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		*map += 1;
2499570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		kunmap_atomic(map, KM_USER0);
2500570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		page = list_entry(page->lru.prev, struct page, lru);
2501570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		while (page != head) {
2502570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			map = kmap_atomic(page, KM_USER0) + offset;
2503570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			*map = COUNT_CONTINUED;
2504570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			kunmap_atomic(map, KM_USER0);
2505570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			page = list_entry(page->lru.prev, struct page, lru);
2506570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2507570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		return true;			/* incremented */
2508570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2509570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	} else {				/* decrementing */
2510570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		/*
2511570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 * Think of how you subtract 1 from 1000
2512570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		 */
2513570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		BUG_ON(count != COUNT_CONTINUED);
2514570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		while (*map == COUNT_CONTINUED) {
2515570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			kunmap_atomic(map, KM_USER0);
2516570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			page = list_entry(page->lru.next, struct page, lru);
2517570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			BUG_ON(page == head);
2518570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			map = kmap_atomic(page, KM_USER0) + offset;
2519570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2520570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		BUG_ON(*map == 0);
2521570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		*map -= 1;
2522570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if (*map == 0)
2523570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			count = 0;
2524570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		kunmap_atomic(map, KM_USER0);
2525570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		page = list_entry(page->lru.prev, struct page, lru);
2526570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		while (page != head) {
2527570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			map = kmap_atomic(page, KM_USER0) + offset;
2528570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			*map = SWAP_CONT_MAX | count;
2529570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			count = COUNT_CONTINUED;
2530570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			kunmap_atomic(map, KM_USER0);
2531570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			page = list_entry(page->lru.prev, struct page, lru);
2532570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2533570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		return count == COUNT_CONTINUED;
2534570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2535570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins}
2536570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2537570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins/*
2538570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages
2539570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2540570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins */
2541570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickinsstatic void free_swap_count_continuations(struct swap_info_struct *si)
2542570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins{
2543570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	pgoff_t offset;
2544570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins
2545570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2546570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		struct page *head;
2547570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		head = vmalloc_to_page(si->swap_map + offset);
2548570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		if (page_private(head)) {
2549570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			struct list_head *this, *next;
2550570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			list_for_each_safe(this, next, &head->lru) {
2551570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				struct page *page;
2552570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				page = list_entry(this, struct page, lru);
2553570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				list_del(this);
2554570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins				__free_page(page);
2555570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins			}
2556570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins		}
2557570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins	}
2558570a335b8e22579e2a51a68136d2b1f907a20eecHugh Dickins}
2559