xref: /openbmc/linux/mm/swapfile.c (revision 3399446632739fcd05fd8b272b476a69c6e6d14a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swapfile.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/hugetlb.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
141da177e4SLinus Torvalds #include <linux/vmalloc.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/shm.h>
181da177e4SLinus Torvalds #include <linux/blkdev.h>
1920137a49SHugh Dickins #include <linux/random.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
211da177e4SLinus Torvalds #include <linux/proc_fs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/module.h>
255ad64688SHugh Dickins #include <linux/ksm.h>
261da177e4SLinus Torvalds #include <linux/rmap.h>
271da177e4SLinus Torvalds #include <linux/security.h>
281da177e4SLinus Torvalds #include <linux/backing-dev.h>
29fc0abb14SIngo Molnar #include <linux/mutex.h>
30c59ede7bSRandy.Dunlap #include <linux/capability.h>
311da177e4SLinus Torvalds #include <linux/syscalls.h>
328a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <asm/pgtable.h>
351da177e4SLinus Torvalds #include <asm/tlbflush.h>
361da177e4SLinus Torvalds #include <linux/swapops.h>
3727a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h>
381da177e4SLinus Torvalds 
39570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
40570a335bSHugh Dickins 				 unsigned char);
41570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
42d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**);
43570a335bSHugh Dickins 
447c363b8cSAdrian Bunk static DEFINE_SPINLOCK(swap_lock);
457c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
46b962716bSHugh Dickins long nr_swap_pages;
471da177e4SLinus Torvalds long total_swap_pages;
4878ecba08SHugh Dickins static int least_priority;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
511da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
521da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
531da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
541da177e4SLinus Torvalds 
557c363b8cSAdrian Bunk static struct swap_list_t swap_list = {-1, -1};
561da177e4SLinus Torvalds 
57efa90a98SHugh Dickins static struct swap_info_struct *swap_info[MAX_SWAPFILES];
581da177e4SLinus Torvalds 
59fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
601da177e4SLinus Torvalds 
618d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
62355cfa73SKAMEZAWA Hiroyuki {
63570a335bSHugh Dickins 	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
64355cfa73SKAMEZAWA Hiroyuki }
65355cfa73SKAMEZAWA Hiroyuki 
66efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
67c9e44410SKAMEZAWA Hiroyuki static int
68c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
69c9e44410SKAMEZAWA Hiroyuki {
70efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
71c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
72c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
73c9e44410SKAMEZAWA Hiroyuki 
74c9e44410SKAMEZAWA Hiroyuki 	page = find_get_page(&swapper_space, entry.val);
75c9e44410SKAMEZAWA Hiroyuki 	if (!page)
76c9e44410SKAMEZAWA Hiroyuki 		return 0;
77c9e44410SKAMEZAWA Hiroyuki 	/*
78c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
79c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
80c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
81c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
82c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
83c9e44410SKAMEZAWA Hiroyuki 	 */
84c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
85c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
86c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
87c9e44410SKAMEZAWA Hiroyuki 	}
88c9e44410SKAMEZAWA Hiroyuki 	page_cache_release(page);
89c9e44410SKAMEZAWA Hiroyuki 	return ret;
90c9e44410SKAMEZAWA Hiroyuki }
91355cfa73SKAMEZAWA Hiroyuki 
921da177e4SLinus Torvalds /*
931da177e4SLinus Torvalds  * We need this because the bdev->unplug_fn can sleep and we cannot
945d337b91SHugh Dickins  * hold swap_lock while calling the unplug_fn. And swap_lock
95fc0abb14SIngo Molnar  * cannot be turned into a mutex.
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	swp_entry_t entry;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	down_read(&swap_unplug_sem);
1044c21e2f2SHugh Dickins 	entry.val = page_private(page);
1051da177e4SLinus Torvalds 	if (PageSwapCache(page)) {
106efa90a98SHugh Dickins 		struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
1071da177e4SLinus Torvalds 		struct backing_dev_info *bdi;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 		/*
1101da177e4SLinus Torvalds 		 * If the page is removed from swapcache from under us (with a
1111da177e4SLinus Torvalds 		 * racy try_to_unuse/swapoff) we need an additional reference
1124c21e2f2SHugh Dickins 		 * count to avoid reading garbage from page_private(page) above.
1134c21e2f2SHugh Dickins 		 * If the WARN_ON triggers during a swapoff it maybe the race
1141da177e4SLinus Torvalds 		 * condition and it's harmless. However if it triggers without
1151da177e4SLinus Torvalds 		 * swapoff it signals a problem.
1161da177e4SLinus Torvalds 		 */
1171da177e4SLinus Torvalds 		WARN_ON(page_count(page) <= 1);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 		bdi = bdev->bd_inode->i_mapping->backing_dev_info;
120ba32311eSMcMullan, Jason 		blk_run_backing_dev(bdi, page);
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds 	up_read(&swap_unplug_sem);
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1256a6ba831SHugh Dickins /*
1266a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1276a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1286a6ba831SHugh Dickins  */
1296a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1306a6ba831SHugh Dickins {
1316a6ba831SHugh Dickins 	struct swap_extent *se;
1329625a5f2SHugh Dickins 	sector_t start_block;
1339625a5f2SHugh Dickins 	sector_t nr_blocks;
1346a6ba831SHugh Dickins 	int err = 0;
1356a6ba831SHugh Dickins 
1366a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1379625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1389625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1399625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1409625a5f2SHugh Dickins 	if (nr_blocks) {
1419625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
1428f2ae0faSChristoph Hellwig 				nr_blocks, GFP_KERNEL, BLKDEV_IFL_WAIT);
1439625a5f2SHugh Dickins 		if (err)
1449625a5f2SHugh Dickins 			return err;
1459625a5f2SHugh Dickins 		cond_resched();
1466a6ba831SHugh Dickins 	}
1476a6ba831SHugh Dickins 
1489625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1499625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1509625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1519625a5f2SHugh Dickins 
1526a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
1538f2ae0faSChristoph Hellwig 				nr_blocks, GFP_KERNEL, BLKDEV_IFL_WAIT);
1546a6ba831SHugh Dickins 		if (err)
1556a6ba831SHugh Dickins 			break;
1566a6ba831SHugh Dickins 
1576a6ba831SHugh Dickins 		cond_resched();
1586a6ba831SHugh Dickins 	}
1596a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1606a6ba831SHugh Dickins }
1616a6ba831SHugh Dickins 
1627992fde7SHugh Dickins /*
1637992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1647992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1657992fde7SHugh Dickins  */
1667992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1677992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1687992fde7SHugh Dickins {
1697992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1707992fde7SHugh Dickins 	int found_extent = 0;
1717992fde7SHugh Dickins 
1727992fde7SHugh Dickins 	while (nr_pages) {
1737992fde7SHugh Dickins 		struct list_head *lh;
1747992fde7SHugh Dickins 
1757992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1767992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1777992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1787992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
179858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1807992fde7SHugh Dickins 
1817992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1827992fde7SHugh Dickins 				nr_blocks = nr_pages;
1837992fde7SHugh Dickins 			start_page += nr_blocks;
1847992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1857992fde7SHugh Dickins 
1867992fde7SHugh Dickins 			if (!found_extent++)
1877992fde7SHugh Dickins 				si->curr_swap_extent = se;
1887992fde7SHugh Dickins 
1897992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1907992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1917992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
1928f2ae0faSChristoph Hellwig 				    nr_blocks, GFP_NOIO, BLKDEV_IFL_WAIT))
1937992fde7SHugh Dickins 				break;
1947992fde7SHugh Dickins 		}
1957992fde7SHugh Dickins 
1967992fde7SHugh Dickins 		lh = se->list.next;
1977992fde7SHugh Dickins 		se = list_entry(lh, struct swap_extent, list);
1987992fde7SHugh Dickins 	}
1997992fde7SHugh Dickins }
2007992fde7SHugh Dickins 
2017992fde7SHugh Dickins static int wait_for_discard(void *word)
2027992fde7SHugh Dickins {
2037992fde7SHugh Dickins 	schedule();
2047992fde7SHugh Dickins 	return 0;
2057992fde7SHugh Dickins }
2067992fde7SHugh Dickins 
207048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
208048c27fdSHugh Dickins #define LATENCY_LIMIT		256
209048c27fdSHugh Dickins 
210355cfa73SKAMEZAWA Hiroyuki static inline unsigned long scan_swap_map(struct swap_info_struct *si,
2118d69aaeeSHugh Dickins 					  unsigned char usage)
2121da177e4SLinus Torvalds {
213ebebbbe9SHugh Dickins 	unsigned long offset;
214c60aa176SHugh Dickins 	unsigned long scan_base;
2157992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
216048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
2177992fde7SHugh Dickins 	int found_free_cluster = 0;
2181da177e4SLinus Torvalds 
2197dfad418SHugh Dickins 	/*
2207dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
2217dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
2227dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
2237dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
2247dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
2257dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
2267dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
227c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
2281da177e4SLinus Torvalds 	 */
2297dfad418SHugh Dickins 
23052b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
231c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
232ebebbbe9SHugh Dickins 
233ebebbbe9SHugh Dickins 	if (unlikely(!si->cluster_nr--)) {
234ebebbbe9SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
2357dfad418SHugh Dickins 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
236ebebbbe9SHugh Dickins 			goto checks;
237ebebbbe9SHugh Dickins 		}
2387992fde7SHugh Dickins 		if (si->flags & SWP_DISCARDABLE) {
2397992fde7SHugh Dickins 			/*
2407992fde7SHugh Dickins 			 * Start range check on racing allocations, in case
2417992fde7SHugh Dickins 			 * they overlap the cluster we eventually decide on
2427992fde7SHugh Dickins 			 * (we scan without swap_lock to allow preemption).
2437992fde7SHugh Dickins 			 * It's hardly conceivable that cluster_nr could be
2447992fde7SHugh Dickins 			 * wrapped during our scan, but don't depend on it.
2457992fde7SHugh Dickins 			 */
2467992fde7SHugh Dickins 			if (si->lowest_alloc)
2477992fde7SHugh Dickins 				goto checks;
2487992fde7SHugh Dickins 			si->lowest_alloc = si->max;
2497992fde7SHugh Dickins 			si->highest_alloc = 0;
2507992fde7SHugh Dickins 		}
2515d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2527dfad418SHugh Dickins 
253c60aa176SHugh Dickins 		/*
254c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
255c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
256c60aa176SHugh Dickins 		 * But if seek is cheap, search from our current position, so
257c60aa176SHugh Dickins 		 * that swap is allocated from all over the partition: if the
258c60aa176SHugh Dickins 		 * Flash Translation Layer only remaps within limited zones,
259c60aa176SHugh Dickins 		 * we don't want to wear out the first zone too quickly.
260c60aa176SHugh Dickins 		 */
261c60aa176SHugh Dickins 		if (!(si->flags & SWP_SOLIDSTATE))
262c60aa176SHugh Dickins 			scan_base = offset = si->lowest_bit;
2637dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
2647dfad418SHugh Dickins 
2657dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
2667dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
2671da177e4SLinus Torvalds 			if (si->swap_map[offset])
2687dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
2697dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
2705d337b91SHugh Dickins 				spin_lock(&swap_lock);
271ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
272ebebbbe9SHugh Dickins 				si->cluster_next = offset;
273ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
2747992fde7SHugh Dickins 				found_free_cluster = 1;
275ebebbbe9SHugh Dickins 				goto checks;
2767dfad418SHugh Dickins 			}
277048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
278048c27fdSHugh Dickins 				cond_resched();
279048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
280048c27fdSHugh Dickins 			}
2817dfad418SHugh Dickins 		}
282ebebbbe9SHugh Dickins 
283ebebbbe9SHugh Dickins 		offset = si->lowest_bit;
284c60aa176SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
285c60aa176SHugh Dickins 
286c60aa176SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
287c60aa176SHugh Dickins 		for (; last_in_cluster < scan_base; offset++) {
288c60aa176SHugh Dickins 			if (si->swap_map[offset])
289c60aa176SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
290c60aa176SHugh Dickins 			else if (offset == last_in_cluster) {
291c60aa176SHugh Dickins 				spin_lock(&swap_lock);
292c60aa176SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
293c60aa176SHugh Dickins 				si->cluster_next = offset;
294c60aa176SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
295c60aa176SHugh Dickins 				found_free_cluster = 1;
296c60aa176SHugh Dickins 				goto checks;
297c60aa176SHugh Dickins 			}
298c60aa176SHugh Dickins 			if (unlikely(--latency_ration < 0)) {
299c60aa176SHugh Dickins 				cond_resched();
300c60aa176SHugh Dickins 				latency_ration = LATENCY_LIMIT;
301c60aa176SHugh Dickins 			}
302c60aa176SHugh Dickins 		}
303c60aa176SHugh Dickins 
304c60aa176SHugh Dickins 		offset = scan_base;
3055d337b91SHugh Dickins 		spin_lock(&swap_lock);
306ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
3077992fde7SHugh Dickins 		si->lowest_alloc = 0;
3087dfad418SHugh Dickins 	}
3097dfad418SHugh Dickins 
310ebebbbe9SHugh Dickins checks:
311ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
31252b7efdbSHugh Dickins 		goto no_page;
3137dfad418SHugh Dickins 	if (!si->highest_bit)
3147dfad418SHugh Dickins 		goto no_page;
315ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
316c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
317c9e44410SKAMEZAWA Hiroyuki 
318b73d7fceSHugh Dickins 	/* reuse swap entry of cache-only swap if not busy. */
319b73d7fceSHugh Dickins 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
320c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
321c9e44410SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
322c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
323c9e44410SKAMEZAWA Hiroyuki 		spin_lock(&swap_lock);
324c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
325c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
326c9e44410SKAMEZAWA Hiroyuki 			goto checks;
327c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
328c9e44410SKAMEZAWA Hiroyuki 	}
329c9e44410SKAMEZAWA Hiroyuki 
330ebebbbe9SHugh Dickins 	if (si->swap_map[offset])
331ebebbbe9SHugh Dickins 		goto scan;
332ebebbbe9SHugh Dickins 
33352b7efdbSHugh Dickins 	if (offset == si->lowest_bit)
3341da177e4SLinus Torvalds 		si->lowest_bit++;
3351da177e4SLinus Torvalds 	if (offset == si->highest_bit)
3361da177e4SLinus Torvalds 		si->highest_bit--;
3377dfad418SHugh Dickins 	si->inuse_pages++;
3387dfad418SHugh Dickins 	if (si->inuse_pages == si->pages) {
3391da177e4SLinus Torvalds 		si->lowest_bit = si->max;
3401da177e4SLinus Torvalds 		si->highest_bit = 0;
3411da177e4SLinus Torvalds 	}
342253d553bSHugh Dickins 	si->swap_map[offset] = usage;
3431da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
34452b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
3457992fde7SHugh Dickins 
3467992fde7SHugh Dickins 	if (si->lowest_alloc) {
3477992fde7SHugh Dickins 		/*
3487992fde7SHugh Dickins 		 * Only set when SWP_DISCARDABLE, and there's a scan
3497992fde7SHugh Dickins 		 * for a free cluster in progress or just completed.
3507992fde7SHugh Dickins 		 */
3517992fde7SHugh Dickins 		if (found_free_cluster) {
3527992fde7SHugh Dickins 			/*
3537992fde7SHugh Dickins 			 * To optimize wear-levelling, discard the
3547992fde7SHugh Dickins 			 * old data of the cluster, taking care not to
3557992fde7SHugh Dickins 			 * discard any of its pages that have already
3567992fde7SHugh Dickins 			 * been allocated by racing tasks (offset has
3577992fde7SHugh Dickins 			 * already stepped over any at the beginning).
3587992fde7SHugh Dickins 			 */
3597992fde7SHugh Dickins 			if (offset < si->highest_alloc &&
3607992fde7SHugh Dickins 			    si->lowest_alloc <= last_in_cluster)
3617992fde7SHugh Dickins 				last_in_cluster = si->lowest_alloc - 1;
3627992fde7SHugh Dickins 			si->flags |= SWP_DISCARDING;
3637992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3647992fde7SHugh Dickins 
3657992fde7SHugh Dickins 			if (offset < last_in_cluster)
3667992fde7SHugh Dickins 				discard_swap_cluster(si, offset,
3677992fde7SHugh Dickins 					last_in_cluster - offset + 1);
3687992fde7SHugh Dickins 
3697992fde7SHugh Dickins 			spin_lock(&swap_lock);
3707992fde7SHugh Dickins 			si->lowest_alloc = 0;
3717992fde7SHugh Dickins 			si->flags &= ~SWP_DISCARDING;
3727992fde7SHugh Dickins 
3737992fde7SHugh Dickins 			smp_mb();	/* wake_up_bit advises this */
3747992fde7SHugh Dickins 			wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
3757992fde7SHugh Dickins 
3767992fde7SHugh Dickins 		} else if (si->flags & SWP_DISCARDING) {
3777992fde7SHugh Dickins 			/*
3787992fde7SHugh Dickins 			 * Delay using pages allocated by racing tasks
3797992fde7SHugh Dickins 			 * until the whole discard has been issued. We
3807992fde7SHugh Dickins 			 * could defer that delay until swap_writepage,
3817992fde7SHugh Dickins 			 * but it's easier to keep this self-contained.
3827992fde7SHugh Dickins 			 */
3837992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3847992fde7SHugh Dickins 			wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
3857992fde7SHugh Dickins 				wait_for_discard, TASK_UNINTERRUPTIBLE);
3867992fde7SHugh Dickins 			spin_lock(&swap_lock);
3877992fde7SHugh Dickins 		} else {
3887992fde7SHugh Dickins 			/*
3897992fde7SHugh Dickins 			 * Note pages allocated by racing tasks while
3907992fde7SHugh Dickins 			 * scan for a free cluster is in progress, so
3917992fde7SHugh Dickins 			 * that its final discard can exclude them.
3927992fde7SHugh Dickins 			 */
3937992fde7SHugh Dickins 			if (offset < si->lowest_alloc)
3947992fde7SHugh Dickins 				si->lowest_alloc = offset;
3957992fde7SHugh Dickins 			if (offset > si->highest_alloc)
3967992fde7SHugh Dickins 				si->highest_alloc = offset;
3977992fde7SHugh Dickins 		}
3987992fde7SHugh Dickins 	}
3991da177e4SLinus Torvalds 	return offset;
4007dfad418SHugh Dickins 
401ebebbbe9SHugh Dickins scan:
4025d337b91SHugh Dickins 	spin_unlock(&swap_lock);
4037dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
40452b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
4055d337b91SHugh Dickins 			spin_lock(&swap_lock);
40652b7efdbSHugh Dickins 			goto checks;
4077dfad418SHugh Dickins 		}
408c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
409c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
410c9e44410SKAMEZAWA Hiroyuki 			goto checks;
411c9e44410SKAMEZAWA Hiroyuki 		}
412048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
413048c27fdSHugh Dickins 			cond_resched();
414048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
415048c27fdSHugh Dickins 		}
41652b7efdbSHugh Dickins 	}
417c60aa176SHugh Dickins 	offset = si->lowest_bit;
418c60aa176SHugh Dickins 	while (++offset < scan_base) {
419c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
4205d337b91SHugh Dickins 			spin_lock(&swap_lock);
421ebebbbe9SHugh Dickins 			goto checks;
422c60aa176SHugh Dickins 		}
423c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
424c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
425c9e44410SKAMEZAWA Hiroyuki 			goto checks;
426c9e44410SKAMEZAWA Hiroyuki 		}
427c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
428c60aa176SHugh Dickins 			cond_resched();
429c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
430c60aa176SHugh Dickins 		}
431c60aa176SHugh Dickins 	}
432c60aa176SHugh Dickins 	spin_lock(&swap_lock);
4337dfad418SHugh Dickins 
4347dfad418SHugh Dickins no_page:
43552b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
4361da177e4SLinus Torvalds 	return 0;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
4401da177e4SLinus Torvalds {
441fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
442fb4f88dcSHugh Dickins 	pgoff_t offset;
443fb4f88dcSHugh Dickins 	int type, next;
444fb4f88dcSHugh Dickins 	int wrapped = 0;
4451da177e4SLinus Torvalds 
4465d337b91SHugh Dickins 	spin_lock(&swap_lock);
4471da177e4SLinus Torvalds 	if (nr_swap_pages <= 0)
448fb4f88dcSHugh Dickins 		goto noswap;
449fb4f88dcSHugh Dickins 	nr_swap_pages--;
4501da177e4SLinus Torvalds 
451fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
452efa90a98SHugh Dickins 		si = swap_info[type];
453fb4f88dcSHugh Dickins 		next = si->next;
454fb4f88dcSHugh Dickins 		if (next < 0 ||
455efa90a98SHugh Dickins 		    (!wrapped && si->prio != swap_info[next]->prio)) {
456fb4f88dcSHugh Dickins 			next = swap_list.head;
457fb4f88dcSHugh Dickins 			wrapped++;
4581da177e4SLinus Torvalds 		}
459fb4f88dcSHugh Dickins 
460fb4f88dcSHugh Dickins 		if (!si->highest_bit)
461fb4f88dcSHugh Dickins 			continue;
462fb4f88dcSHugh Dickins 		if (!(si->flags & SWP_WRITEOK))
463fb4f88dcSHugh Dickins 			continue;
464fb4f88dcSHugh Dickins 
465fb4f88dcSHugh Dickins 		swap_list.next = next;
466355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry for cache */
467253d553bSHugh Dickins 		offset = scan_swap_map(si, SWAP_HAS_CACHE);
4685d337b91SHugh Dickins 		if (offset) {
4695d337b91SHugh Dickins 			spin_unlock(&swap_lock);
470fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
4715d337b91SHugh Dickins 		}
472fb4f88dcSHugh Dickins 		next = swap_list.next;
473fb4f88dcSHugh Dickins 	}
474fb4f88dcSHugh Dickins 
475fb4f88dcSHugh Dickins 	nr_swap_pages++;
476fb4f88dcSHugh Dickins noswap:
4775d337b91SHugh Dickins 	spin_unlock(&swap_lock);
478fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
4791da177e4SLinus Torvalds }
4801da177e4SLinus Torvalds 
481910321eaSHugh Dickins /* The only caller of this function is now susupend routine */
482910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type)
483910321eaSHugh Dickins {
484910321eaSHugh Dickins 	struct swap_info_struct *si;
485910321eaSHugh Dickins 	pgoff_t offset;
486910321eaSHugh Dickins 
487910321eaSHugh Dickins 	spin_lock(&swap_lock);
488910321eaSHugh Dickins 	si = swap_info[type];
489910321eaSHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
490910321eaSHugh Dickins 		nr_swap_pages--;
491910321eaSHugh Dickins 		/* This is called for allocating swap entry, not cache */
492910321eaSHugh Dickins 		offset = scan_swap_map(si, 1);
493910321eaSHugh Dickins 		if (offset) {
494910321eaSHugh Dickins 			spin_unlock(&swap_lock);
495910321eaSHugh Dickins 			return swp_entry(type, offset);
496910321eaSHugh Dickins 		}
497910321eaSHugh Dickins 		nr_swap_pages++;
498910321eaSHugh Dickins 	}
499910321eaSHugh Dickins 	spin_unlock(&swap_lock);
500910321eaSHugh Dickins 	return (swp_entry_t) {0};
501910321eaSHugh Dickins }
502910321eaSHugh Dickins 
5031da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	struct swap_info_struct *p;
5061da177e4SLinus Torvalds 	unsigned long offset, type;
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (!entry.val)
5091da177e4SLinus Torvalds 		goto out;
5101da177e4SLinus Torvalds 	type = swp_type(entry);
5111da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
5121da177e4SLinus Torvalds 		goto bad_nofile;
513efa90a98SHugh Dickins 	p = swap_info[type];
5141da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
5151da177e4SLinus Torvalds 		goto bad_device;
5161da177e4SLinus Torvalds 	offset = swp_offset(entry);
5171da177e4SLinus Torvalds 	if (offset >= p->max)
5181da177e4SLinus Torvalds 		goto bad_offset;
5191da177e4SLinus Torvalds 	if (!p->swap_map[offset])
5201da177e4SLinus Torvalds 		goto bad_free;
5215d337b91SHugh Dickins 	spin_lock(&swap_lock);
5221da177e4SLinus Torvalds 	return p;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds bad_free:
5251da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
5261da177e4SLinus Torvalds 	goto out;
5271da177e4SLinus Torvalds bad_offset:
5281da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
5291da177e4SLinus Torvalds 	goto out;
5301da177e4SLinus Torvalds bad_device:
5311da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
5321da177e4SLinus Torvalds 	goto out;
5331da177e4SLinus Torvalds bad_nofile:
5341da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
5351da177e4SLinus Torvalds out:
5361da177e4SLinus Torvalds 	return NULL;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5398d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p,
5408d69aaeeSHugh Dickins 				     swp_entry_t entry, unsigned char usage)
5411da177e4SLinus Torvalds {
542253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
5438d69aaeeSHugh Dickins 	unsigned char count;
5448d69aaeeSHugh Dickins 	unsigned char has_cache;
5451da177e4SLinus Torvalds 
546355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
547253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
548253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
549253d553bSHugh Dickins 
550253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
551253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
552253d553bSHugh Dickins 		has_cache = 0;
553aaa46865SHugh Dickins 	} else if (count == SWAP_MAP_SHMEM) {
554aaa46865SHugh Dickins 		/*
555aaa46865SHugh Dickins 		 * Or we could insist on shmem.c using a special
556aaa46865SHugh Dickins 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
557aaa46865SHugh Dickins 		 */
558aaa46865SHugh Dickins 		count = 0;
559570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
560570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
561570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
562570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
563570a335bSHugh Dickins 			else
564570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
565570a335bSHugh Dickins 		} else
566253d553bSHugh Dickins 			count--;
567570a335bSHugh Dickins 	}
568253d553bSHugh Dickins 
569253d553bSHugh Dickins 	if (!count)
570253d553bSHugh Dickins 		mem_cgroup_uncharge_swap(entry);
571253d553bSHugh Dickins 
572253d553bSHugh Dickins 	usage = count | has_cache;
573253d553bSHugh Dickins 	p->swap_map[offset] = usage;
574253d553bSHugh Dickins 
575355cfa73SKAMEZAWA Hiroyuki 	/* free if no reference */
576253d553bSHugh Dickins 	if (!usage) {
577b3a27d05SNitin Gupta 		struct gendisk *disk = p->bdev->bd_disk;
5781da177e4SLinus Torvalds 		if (offset < p->lowest_bit)
5791da177e4SLinus Torvalds 			p->lowest_bit = offset;
5801da177e4SLinus Torvalds 		if (offset > p->highest_bit)
5811da177e4SLinus Torvalds 			p->highest_bit = offset;
582efa90a98SHugh Dickins 		if (swap_list.next >= 0 &&
583efa90a98SHugh Dickins 		    p->prio > swap_info[swap_list.next]->prio)
584efa90a98SHugh Dickins 			swap_list.next = p->type;
5851da177e4SLinus Torvalds 		nr_swap_pages++;
5861da177e4SLinus Torvalds 		p->inuse_pages--;
587b3a27d05SNitin Gupta 		if ((p->flags & SWP_BLKDEV) &&
588b3a27d05SNitin Gupta 				disk->fops->swap_slot_free_notify)
589b3a27d05SNitin Gupta 			disk->fops->swap_slot_free_notify(p->bdev, offset);
5901da177e4SLinus Torvalds 	}
591253d553bSHugh Dickins 
592253d553bSHugh Dickins 	return usage;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds /*
5961da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
5971da177e4SLinus Torvalds  * is still around or has not been recycled.
5981da177e4SLinus Torvalds  */
5991da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	struct swap_info_struct *p;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	p = swap_info_get(entry);
6041da177e4SLinus Torvalds 	if (p) {
605253d553bSHugh Dickins 		swap_entry_free(p, entry, 1);
6065d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /*
611cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
612cb4b86baSKAMEZAWA Hiroyuki  */
613cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page)
614cb4b86baSKAMEZAWA Hiroyuki {
615355cfa73SKAMEZAWA Hiroyuki 	struct swap_info_struct *p;
6168d69aaeeSHugh Dickins 	unsigned char count;
617355cfa73SKAMEZAWA Hiroyuki 
618355cfa73SKAMEZAWA Hiroyuki 	p = swap_info_get(entry);
619355cfa73SKAMEZAWA Hiroyuki 	if (p) {
620253d553bSHugh Dickins 		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
621253d553bSHugh Dickins 		if (page)
622253d553bSHugh Dickins 			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
623355cfa73SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
624355cfa73SKAMEZAWA Hiroyuki 	}
625cb4b86baSKAMEZAWA Hiroyuki }
626cb4b86baSKAMEZAWA Hiroyuki 
627cb4b86baSKAMEZAWA Hiroyuki /*
628c475a8abSHugh Dickins  * How many references to page are currently swapped out?
629570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
630570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
6311da177e4SLinus Torvalds  */
632c475a8abSHugh Dickins static inline int page_swapcount(struct page *page)
6331da177e4SLinus Torvalds {
634c475a8abSHugh Dickins 	int count = 0;
6351da177e4SLinus Torvalds 	struct swap_info_struct *p;
6361da177e4SLinus Torvalds 	swp_entry_t entry;
6371da177e4SLinus Torvalds 
6384c21e2f2SHugh Dickins 	entry.val = page_private(page);
6391da177e4SLinus Torvalds 	p = swap_info_get(entry);
6401da177e4SLinus Torvalds 	if (p) {
641355cfa73SKAMEZAWA Hiroyuki 		count = swap_count(p->swap_map[swp_offset(entry)]);
6425d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6431da177e4SLinus Torvalds 	}
644c475a8abSHugh Dickins 	return count;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds /*
6487b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
6497b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
6507b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
6517b1fe597SHugh Dickins  * later would only waste time away from clustering.
6521da177e4SLinus Torvalds  */
6537b1fe597SHugh Dickins int reuse_swap_page(struct page *page)
6541da177e4SLinus Torvalds {
655c475a8abSHugh Dickins 	int count;
6561da177e4SLinus Torvalds 
65751726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6585ad64688SHugh Dickins 	if (unlikely(PageKsm(page)))
6595ad64688SHugh Dickins 		return 0;
660c475a8abSHugh Dickins 	count = page_mapcount(page);
6617b1fe597SHugh Dickins 	if (count <= 1 && PageSwapCache(page)) {
662c475a8abSHugh Dickins 		count += page_swapcount(page);
6637b1fe597SHugh Dickins 		if (count == 1 && !PageWriteback(page)) {
6647b1fe597SHugh Dickins 			delete_from_swap_cache(page);
6657b1fe597SHugh Dickins 			SetPageDirty(page);
6667b1fe597SHugh Dickins 		}
6677b1fe597SHugh Dickins 	}
6685ad64688SHugh Dickins 	return count <= 1;
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /*
672a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
673a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
6741da177e4SLinus Torvalds  */
675a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
6761da177e4SLinus Torvalds {
67751726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (!PageSwapCache(page))
6801da177e4SLinus Torvalds 		return 0;
6811da177e4SLinus Torvalds 	if (PageWriteback(page))
6821da177e4SLinus Torvalds 		return 0;
683a2c43eedSHugh Dickins 	if (page_swapcount(page))
6841da177e4SLinus Torvalds 		return 0;
6851da177e4SLinus Torvalds 
686b73d7fceSHugh Dickins 	/*
687b73d7fceSHugh Dickins 	 * Once hibernation has begun to create its image of memory,
688b73d7fceSHugh Dickins 	 * there's a danger that one of the calls to try_to_free_swap()
689b73d7fceSHugh Dickins 	 * - most probably a call from __try_to_reclaim_swap() while
690b73d7fceSHugh Dickins 	 * hibernation is allocating its own swap pages for the image,
691b73d7fceSHugh Dickins 	 * but conceivably even a call from memory reclaim - will free
692b73d7fceSHugh Dickins 	 * the swap from a page which has already been recorded in the
693b73d7fceSHugh Dickins 	 * image as a clean swapcache page, and then reuse its swap for
694b73d7fceSHugh Dickins 	 * another page of the image.  On waking from hibernation, the
695b73d7fceSHugh Dickins 	 * original page might be freed under memory pressure, then
696b73d7fceSHugh Dickins 	 * later read back in from swap, now with the wrong data.
697b73d7fceSHugh Dickins 	 *
698b73d7fceSHugh Dickins 	 * Hibernation clears bits from gfp_allowed_mask to prevent
699b73d7fceSHugh Dickins 	 * memory reclaim from writing to disk, so check that here.
700b73d7fceSHugh Dickins 	 */
701b73d7fceSHugh Dickins 	if (!(gfp_allowed_mask & __GFP_IO))
702b73d7fceSHugh Dickins 		return 0;
703b73d7fceSHugh Dickins 
704a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
7051da177e4SLinus Torvalds 	SetPageDirty(page);
706a2c43eedSHugh Dickins 	return 1;
70768a22394SRik van Riel }
70868a22394SRik van Riel 
70968a22394SRik van Riel /*
7101da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
7111da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
7121da177e4SLinus Torvalds  */
7132509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	struct swap_info_struct *p;
7161da177e4SLinus Torvalds 	struct page *page = NULL;
7171da177e4SLinus Torvalds 
718a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
7192509ef26SHugh Dickins 		return 1;
7200697212aSChristoph Lameter 
7211da177e4SLinus Torvalds 	p = swap_info_get(entry);
7221da177e4SLinus Torvalds 	if (p) {
723253d553bSHugh Dickins 		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
72493fac704SNick Piggin 			page = find_get_page(&swapper_space, entry.val);
7258413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
72693fac704SNick Piggin 				page_cache_release(page);
72793fac704SNick Piggin 				page = NULL;
72893fac704SNick Piggin 			}
72993fac704SNick Piggin 		}
7305d337b91SHugh Dickins 		spin_unlock(&swap_lock);
7311da177e4SLinus Torvalds 	}
7321da177e4SLinus Torvalds 	if (page) {
733a2c43eedSHugh Dickins 		/*
734a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
735a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
736a2c43eedSHugh Dickins 		 */
73793fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
738a2c43eedSHugh Dickins 				(!page_mapped(page) || vm_swap_full())) {
7391da177e4SLinus Torvalds 			delete_from_swap_cache(page);
7401da177e4SLinus Torvalds 			SetPageDirty(page);
7411da177e4SLinus Torvalds 		}
7421da177e4SLinus Torvalds 		unlock_page(page);
7431da177e4SLinus Torvalds 		page_cache_release(page);
7441da177e4SLinus Torvalds 	}
7452509ef26SHugh Dickins 	return p != NULL;
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
74802491447SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
74902491447SDaisuke Nishimura /**
75002491447SDaisuke Nishimura  * mem_cgroup_count_swap_user - count the user of a swap entry
75102491447SDaisuke Nishimura  * @ent: the swap entry to be checked
75202491447SDaisuke Nishimura  * @pagep: the pointer for the swap cache page of the entry to be stored
75302491447SDaisuke Nishimura  *
75402491447SDaisuke Nishimura  * Returns the number of the user of the swap entry. The number is valid only
75502491447SDaisuke Nishimura  * for swaps of anonymous pages.
75602491447SDaisuke Nishimura  * If the entry is found on swap cache, the page is stored to pagep with
75702491447SDaisuke Nishimura  * refcount of it being incremented.
75802491447SDaisuke Nishimura  */
75902491447SDaisuke Nishimura int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
76002491447SDaisuke Nishimura {
76102491447SDaisuke Nishimura 	struct page *page;
76202491447SDaisuke Nishimura 	struct swap_info_struct *p;
76302491447SDaisuke Nishimura 	int count = 0;
76402491447SDaisuke Nishimura 
76502491447SDaisuke Nishimura 	page = find_get_page(&swapper_space, ent.val);
76602491447SDaisuke Nishimura 	if (page)
76702491447SDaisuke Nishimura 		count += page_mapcount(page);
76802491447SDaisuke Nishimura 	p = swap_info_get(ent);
76902491447SDaisuke Nishimura 	if (p) {
77002491447SDaisuke Nishimura 		count += swap_count(p->swap_map[swp_offset(ent)]);
77102491447SDaisuke Nishimura 		spin_unlock(&swap_lock);
77202491447SDaisuke Nishimura 	}
77302491447SDaisuke Nishimura 
77402491447SDaisuke Nishimura 	*pagep = page;
77502491447SDaisuke Nishimura 	return count;
77602491447SDaisuke Nishimura }
77702491447SDaisuke Nishimura #endif
77802491447SDaisuke Nishimura 
779b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
780f577eb30SRafael J. Wysocki /*
781915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
782f577eb30SRafael J. Wysocki  *
783915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
784915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
785915bae9eSRafael J. Wysocki  *
786915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
787f577eb30SRafael J. Wysocki  */
7887bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
789f577eb30SRafael J. Wysocki {
790915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
791efa90a98SHugh Dickins 	int type;
792f577eb30SRafael J. Wysocki 
793915bae9eSRafael J. Wysocki 	if (device)
794915bae9eSRafael J. Wysocki 		bdev = bdget(device);
795915bae9eSRafael J. Wysocki 
796f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
797efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
798efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
799f577eb30SRafael J. Wysocki 
800915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
801f577eb30SRafael J. Wysocki 			continue;
802b6b5bce3SRafael J. Wysocki 
803915bae9eSRafael J. Wysocki 		if (!bdev) {
8047bf23687SRafael J. Wysocki 			if (bdev_p)
805dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
8067bf23687SRafael J. Wysocki 
8076e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
808efa90a98SHugh Dickins 			return type;
8096e1819d6SRafael J. Wysocki 		}
810915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
8119625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
812915bae9eSRafael J. Wysocki 
813915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
8147bf23687SRafael J. Wysocki 				if (bdev_p)
815dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
8167bf23687SRafael J. Wysocki 
817f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
818915bae9eSRafael J. Wysocki 				bdput(bdev);
819efa90a98SHugh Dickins 				return type;
820f577eb30SRafael J. Wysocki 			}
821f577eb30SRafael J. Wysocki 		}
822915bae9eSRafael J. Wysocki 	}
823f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
824915bae9eSRafael J. Wysocki 	if (bdev)
825915bae9eSRafael J. Wysocki 		bdput(bdev);
826915bae9eSRafael J. Wysocki 
827f577eb30SRafael J. Wysocki 	return -ENODEV;
828f577eb30SRafael J. Wysocki }
829f577eb30SRafael J. Wysocki 
830f577eb30SRafael J. Wysocki /*
83173c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
83273c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
83373c34b6aSHugh Dickins  */
83473c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
83573c34b6aSHugh Dickins {
83673c34b6aSHugh Dickins 	struct block_device *bdev;
83773c34b6aSHugh Dickins 
83873c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
83973c34b6aSHugh Dickins 		return 0;
84073c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
84173c34b6aSHugh Dickins 		return 0;
842d4906e1aSLee Schermerhorn 	return map_swap_entry(swp_entry(type, offset), &bdev);
84373c34b6aSHugh Dickins }
84473c34b6aSHugh Dickins 
84573c34b6aSHugh Dickins /*
846f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
847f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
848f577eb30SRafael J. Wysocki  *
849f577eb30SRafael J. Wysocki  * This is needed for software suspend
850f577eb30SRafael J. Wysocki  */
851f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
852f577eb30SRafael J. Wysocki {
853f577eb30SRafael J. Wysocki 	unsigned int n = 0;
854f577eb30SRafael J. Wysocki 
855f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
856efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
857efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
858efa90a98SHugh Dickins 
859efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
860efa90a98SHugh Dickins 			n = sis->pages;
861f577eb30SRafael J. Wysocki 			if (free)
862efa90a98SHugh Dickins 				n -= sis->inuse_pages;
863efa90a98SHugh Dickins 		}
864f577eb30SRafael J. Wysocki 	}
865f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
866f577eb30SRafael J. Wysocki 	return n;
867f577eb30SRafael J. Wysocki }
86873c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
869f577eb30SRafael J. Wysocki 
8701da177e4SLinus Torvalds /*
87172866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
87272866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
87372866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
8741da177e4SLinus Torvalds  */
875044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
8761da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
8771da177e4SLinus Torvalds {
8787a81b88cSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr = NULL;
879044d66c1SHugh Dickins 	spinlock_t *ptl;
880044d66c1SHugh Dickins 	pte_t *pte;
881044d66c1SHugh Dickins 	int ret = 1;
882044d66c1SHugh Dickins 
88385d9fc89SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
884044d66c1SHugh Dickins 		ret = -ENOMEM;
88585d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
88685d9fc89SKAMEZAWA Hiroyuki 	}
887044d66c1SHugh Dickins 
888044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
889044d66c1SHugh Dickins 	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
890044d66c1SHugh Dickins 		if (ret > 0)
8917a81b88cSKAMEZAWA Hiroyuki 			mem_cgroup_cancel_charge_swapin(ptr);
892044d66c1SHugh Dickins 		ret = 0;
893044d66c1SHugh Dickins 		goto out;
894044d66c1SHugh Dickins 	}
8958a9f3ccdSBalbir Singh 
896b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
897d559db08SKAMEZAWA Hiroyuki 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
8981da177e4SLinus Torvalds 	get_page(page);
8991da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
9001da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
9011da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, addr);
9027a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
9031da177e4SLinus Torvalds 	swap_free(entry);
9041da177e4SLinus Torvalds 	/*
9051da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
9061da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
9071da177e4SLinus Torvalds 	 */
9081da177e4SLinus Torvalds 	activate_page(page);
909044d66c1SHugh Dickins out:
910044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
91185d9fc89SKAMEZAWA Hiroyuki out_nolock:
912044d66c1SHugh Dickins 	return ret;
9131da177e4SLinus Torvalds }
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
9161da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9171da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
920705e87c0SHugh Dickins 	pte_t *pte;
9218a9f3ccdSBalbir Singh 	int ret = 0;
9221da177e4SLinus Torvalds 
923044d66c1SHugh Dickins 	/*
924044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
925044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
926044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
927044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
928044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
929044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
930044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
931044d66c1SHugh Dickins 	 */
932044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
9331da177e4SLinus Torvalds 	do {
9341da177e4SLinus Torvalds 		/*
9351da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
9361da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
9371da177e4SLinus Torvalds 		 */
9381da177e4SLinus Torvalds 		if (unlikely(pte_same(*pte, swp_pte))) {
939044d66c1SHugh Dickins 			pte_unmap(pte);
940044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
941044d66c1SHugh Dickins 			if (ret)
942044d66c1SHugh Dickins 				goto out;
943044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
9441da177e4SLinus Torvalds 		}
9451da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
946044d66c1SHugh Dickins 	pte_unmap(pte - 1);
947044d66c1SHugh Dickins out:
9488a9f3ccdSBalbir Singh 	return ret;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
9521da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9531da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9541da177e4SLinus Torvalds {
9551da177e4SLinus Torvalds 	pmd_t *pmd;
9561da177e4SLinus Torvalds 	unsigned long next;
9578a9f3ccdSBalbir Singh 	int ret;
9581da177e4SLinus Torvalds 
9591da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
9601da177e4SLinus Torvalds 	do {
9611da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
9621da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
9631da177e4SLinus Torvalds 			continue;
9648a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
9658a9f3ccdSBalbir Singh 		if (ret)
9668a9f3ccdSBalbir Singh 			return ret;
9671da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
9681da177e4SLinus Torvalds 	return 0;
9691da177e4SLinus Torvalds }
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
9721da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9731da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9741da177e4SLinus Torvalds {
9751da177e4SLinus Torvalds 	pud_t *pud;
9761da177e4SLinus Torvalds 	unsigned long next;
9778a9f3ccdSBalbir Singh 	int ret;
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
9801da177e4SLinus Torvalds 	do {
9811da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9821da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
9831da177e4SLinus Torvalds 			continue;
9848a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
9858a9f3ccdSBalbir Singh 		if (ret)
9868a9f3ccdSBalbir Singh 			return ret;
9871da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
9881da177e4SLinus Torvalds 	return 0;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
9921da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	pgd_t *pgd;
9951da177e4SLinus Torvalds 	unsigned long addr, end, next;
9968a9f3ccdSBalbir Singh 	int ret;
9971da177e4SLinus Torvalds 
9983ca7b3c5SHugh Dickins 	if (page_anon_vma(page)) {
9991da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
10001da177e4SLinus Torvalds 		if (addr == -EFAULT)
10011da177e4SLinus Torvalds 			return 0;
10021da177e4SLinus Torvalds 		else
10031da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
10041da177e4SLinus Torvalds 	} else {
10051da177e4SLinus Torvalds 		addr = vma->vm_start;
10061da177e4SLinus Torvalds 		end = vma->vm_end;
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
10101da177e4SLinus Torvalds 	do {
10111da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
10121da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
10131da177e4SLinus Torvalds 			continue;
10148a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
10158a9f3ccdSBalbir Singh 		if (ret)
10168a9f3ccdSBalbir Singh 			return ret;
10171da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
10181da177e4SLinus Torvalds 	return 0;
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
10221da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
10231da177e4SLinus Torvalds {
10241da177e4SLinus Torvalds 	struct vm_area_struct *vma;
10258a9f3ccdSBalbir Singh 	int ret = 0;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
10281da177e4SLinus Torvalds 		/*
10297d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
10307d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
10311da177e4SLinus Torvalds 		 */
1032c475a8abSHugh Dickins 		activate_page(page);
10331da177e4SLinus Torvalds 		unlock_page(page);
10341da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
10351da177e4SLinus Torvalds 		lock_page(page);
10361da177e4SLinus Torvalds 	}
10371da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
10388a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
10391da177e4SLinus Torvalds 			break;
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
10428a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
10431da177e4SLinus Torvalds }
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds /*
10461da177e4SLinus Torvalds  * Scan swap_map from current position to next entry still in use.
10471da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
10481da177e4SLinus Torvalds  */
10496eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
10506eb396dcSHugh Dickins 					unsigned int prev)
10511da177e4SLinus Torvalds {
10526eb396dcSHugh Dickins 	unsigned int max = si->max;
10536eb396dcSHugh Dickins 	unsigned int i = prev;
10548d69aaeeSHugh Dickins 	unsigned char count;
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	/*
10575d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
10581da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
10591da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
10605d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
10611da177e4SLinus Torvalds 	 */
10621da177e4SLinus Torvalds 	for (;;) {
10631da177e4SLinus Torvalds 		if (++i >= max) {
10641da177e4SLinus Torvalds 			if (!prev) {
10651da177e4SLinus Torvalds 				i = 0;
10661da177e4SLinus Torvalds 				break;
10671da177e4SLinus Torvalds 			}
10681da177e4SLinus Torvalds 			/*
10691da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
10701da177e4SLinus Torvalds 			 * loop back to start and recheck there.
10711da177e4SLinus Torvalds 			 */
10721da177e4SLinus Torvalds 			max = prev + 1;
10731da177e4SLinus Torvalds 			prev = 0;
10741da177e4SLinus Torvalds 			i = 1;
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 		count = si->swap_map[i];
1077355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
10781da177e4SLinus Torvalds 			break;
10791da177e4SLinus Torvalds 	}
10801da177e4SLinus Torvalds 	return i;
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
10831da177e4SLinus Torvalds /*
10841da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
10851da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
10861da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
10871da177e4SLinus Torvalds  */
10881da177e4SLinus Torvalds static int try_to_unuse(unsigned int type)
10891da177e4SLinus Torvalds {
1090efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
10911da177e4SLinus Torvalds 	struct mm_struct *start_mm;
10928d69aaeeSHugh Dickins 	unsigned char *swap_map;
10938d69aaeeSHugh Dickins 	unsigned char swcount;
10941da177e4SLinus Torvalds 	struct page *page;
10951da177e4SLinus Torvalds 	swp_entry_t entry;
10966eb396dcSHugh Dickins 	unsigned int i = 0;
10971da177e4SLinus Torvalds 	int retval = 0;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	/*
11001da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
11011da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
11021da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
11031da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
11041da177e4SLinus Torvalds 	 *
11051da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
11061da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
11071da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
11081da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
11091da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
11101da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
1111570a335bSHugh Dickins 	 * that.
11121da177e4SLinus Torvalds 	 */
11131da177e4SLinus Torvalds 	start_mm = &init_mm;
11141da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	/*
11171da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
11181da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
11191da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
11201da177e4SLinus Torvalds 	 */
11211da177e4SLinus Torvalds 	while ((i = find_next_to_unuse(si, i)) != 0) {
11221da177e4SLinus Torvalds 		if (signal_pending(current)) {
11231da177e4SLinus Torvalds 			retval = -EINTR;
11241da177e4SLinus Torvalds 			break;
11251da177e4SLinus Torvalds 		}
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds 		/*
11281da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
11291da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
11301da177e4SLinus Torvalds 		 * page and read the swap into it.
11311da177e4SLinus Torvalds 		 */
11321da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
11331da177e4SLinus Torvalds 		entry = swp_entry(type, i);
113402098feaSHugh Dickins 		page = read_swap_cache_async(entry,
113502098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
11361da177e4SLinus Torvalds 		if (!page) {
11371da177e4SLinus Torvalds 			/*
11381da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
11391da177e4SLinus Torvalds 			 * has been freed independently, and will not be
11401da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
11411da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
11421da177e4SLinus Torvalds 			 */
11431da177e4SLinus Torvalds 			if (!*swap_map)
11441da177e4SLinus Torvalds 				continue;
11451da177e4SLinus Torvalds 			retval = -ENOMEM;
11461da177e4SLinus Torvalds 			break;
11471da177e4SLinus Torvalds 		}
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		/*
11501da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
11511da177e4SLinus Torvalds 		 */
11521da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
11531da177e4SLinus Torvalds 			mmput(start_mm);
11541da177e4SLinus Torvalds 			start_mm = &init_mm;
11551da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
11561da177e4SLinus Torvalds 		}
11571da177e4SLinus Torvalds 
11581da177e4SLinus Torvalds 		/*
11591da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
11601da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
11611da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
11621da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
11631da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
11641da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
11651da177e4SLinus Torvalds 		 */
11661da177e4SLinus Torvalds 		wait_on_page_locked(page);
11671da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11681da177e4SLinus Torvalds 		lock_page(page);
11691da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 		/*
11721da177e4SLinus Torvalds 		 * Remove all references to entry.
11731da177e4SLinus Torvalds 		 */
11741da177e4SLinus Torvalds 		swcount = *swap_map;
1175aaa46865SHugh Dickins 		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1176aaa46865SHugh Dickins 			retval = shmem_unuse(entry, page);
1177aaa46865SHugh Dickins 			/* page has already been unlocked and released */
1178aaa46865SHugh Dickins 			if (retval < 0)
1179aaa46865SHugh Dickins 				break;
1180aaa46865SHugh Dickins 			continue;
11811da177e4SLinus Torvalds 		}
1182aaa46865SHugh Dickins 		if (swap_count(swcount) && start_mm != &init_mm)
1183aaa46865SHugh Dickins 			retval = unuse_mm(start_mm, entry, page);
1184aaa46865SHugh Dickins 
1185355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
11861da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
11871da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
11881da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
11891da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
11901da177e4SLinus Torvalds 			struct mm_struct *mm;
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
11931da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
11941da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
1195aaa46865SHugh Dickins 			while (swap_count(*swap_map) && !retval &&
11961da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
11971da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
119870af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
11991da177e4SLinus Torvalds 					continue;
12001da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
12011da177e4SLinus Torvalds 				mmput(prev_mm);
12021da177e4SLinus Torvalds 				prev_mm = mm;
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds 				cond_resched();
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds 				swcount = *swap_map;
1207355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
12081da177e4SLinus Torvalds 					;
1209aaa46865SHugh Dickins 				else if (mm == &init_mm)
12101da177e4SLinus Torvalds 					set_start_mm = 1;
1211aaa46865SHugh Dickins 				else
12121da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
1213355cfa73SKAMEZAWA Hiroyuki 
121432c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
12151da177e4SLinus Torvalds 					mmput(new_start_mm);
12161da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
12171da177e4SLinus Torvalds 					new_start_mm = mm;
12181da177e4SLinus Torvalds 					set_start_mm = 0;
12191da177e4SLinus Torvalds 				}
12201da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
12211da177e4SLinus Torvalds 			}
12221da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
12231da177e4SLinus Torvalds 			mmput(prev_mm);
12241da177e4SLinus Torvalds 			mmput(start_mm);
12251da177e4SLinus Torvalds 			start_mm = new_start_mm;
12261da177e4SLinus Torvalds 		}
12271da177e4SLinus Torvalds 		if (retval) {
12281da177e4SLinus Torvalds 			unlock_page(page);
12291da177e4SLinus Torvalds 			page_cache_release(page);
12301da177e4SLinus Torvalds 			break;
12311da177e4SLinus Torvalds 		}
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		/*
12341da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
12351da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
12361da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
12371da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
12381da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
12391da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
12401da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
12411da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
12421da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
12431da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
12441da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
12455ad64688SHugh Dickins 		 *
12465ad64688SHugh Dickins 		 * Given how unuse_vma() targets one particular offset
12475ad64688SHugh Dickins 		 * in an anon_vma, once the anon_vma has been determined,
12485ad64688SHugh Dickins 		 * this splitting happens to be just what is needed to
12495ad64688SHugh Dickins 		 * handle where KSM pages have been swapped out: re-reading
12505ad64688SHugh Dickins 		 * is unnecessarily slow, but we can fix that later on.
12511da177e4SLinus Torvalds 		 */
1252355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
1253355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
12541da177e4SLinus Torvalds 			struct writeback_control wbc = {
12551da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
12561da177e4SLinus Torvalds 			};
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
12591da177e4SLinus Torvalds 			lock_page(page);
12601da177e4SLinus Torvalds 			wait_on_page_writeback(page);
12611da177e4SLinus Torvalds 		}
126268bdc8d6SHugh Dickins 
126368bdc8d6SHugh Dickins 		/*
126468bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
126568bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
126668bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
126768bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
126868bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
126968bdc8d6SHugh Dickins 		 */
127068bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
127168bdc8d6SHugh Dickins 		    likely(page_private(page) == entry.val))
12721da177e4SLinus Torvalds 			delete_from_swap_cache(page);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds 		/*
12751da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
12761da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
12772706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
12781da177e4SLinus Torvalds 		 */
12791da177e4SLinus Torvalds 		SetPageDirty(page);
12801da177e4SLinus Torvalds 		unlock_page(page);
12811da177e4SLinus Torvalds 		page_cache_release(page);
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 		/*
12841da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
12851da177e4SLinus Torvalds 		 * interactive performance.
12861da177e4SLinus Torvalds 		 */
12871da177e4SLinus Torvalds 		cond_resched();
12881da177e4SLinus Torvalds 	}
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	mmput(start_mm);
12911da177e4SLinus Torvalds 	return retval;
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds 
12941da177e4SLinus Torvalds /*
12955d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
12965d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
12975d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
12981da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
12991da177e4SLinus Torvalds  */
13001da177e4SLinus Torvalds static void drain_mmlist(void)
13011da177e4SLinus Torvalds {
13021da177e4SLinus Torvalds 	struct list_head *p, *next;
1303efa90a98SHugh Dickins 	unsigned int type;
13041da177e4SLinus Torvalds 
1305efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
1306efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
13071da177e4SLinus Torvalds 			return;
13081da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
13091da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
13101da177e4SLinus Torvalds 		list_del_init(p);
13111da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13141da177e4SLinus Torvalds /*
13151da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1316d4906e1aSLee Schermerhorn  * corresponds to page offset for the specified swap entry.
1317d4906e1aSLee Schermerhorn  * Note that the type of this function is sector_t, but it returns page offset
1318d4906e1aSLee Schermerhorn  * into the bdev, not sector offset.
13191da177e4SLinus Torvalds  */
1320d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
13211da177e4SLinus Torvalds {
1322f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
1323f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
1324f29ad6a9SHugh Dickins 	struct swap_extent *se;
1325f29ad6a9SHugh Dickins 	pgoff_t offset;
1326f29ad6a9SHugh Dickins 
1327efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
1328f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
1329f29ad6a9SHugh Dickins 
1330f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
1331f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
1332f29ad6a9SHugh Dickins 	se = start_se;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	for ( ; ; ) {
13351da177e4SLinus Torvalds 		struct list_head *lh;
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 		if (se->start_page <= offset &&
13381da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
13391da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
13401da177e4SLinus Torvalds 		}
134111d31886SHugh Dickins 		lh = se->list.next;
13421da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
13431da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
13441da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
1349d4906e1aSLee Schermerhorn  * Returns the page offset into bdev for the specified page's swap entry.
1350d4906e1aSLee Schermerhorn  */
1351d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev)
1352d4906e1aSLee Schermerhorn {
1353d4906e1aSLee Schermerhorn 	swp_entry_t entry;
1354d4906e1aSLee Schermerhorn 	entry.val = page_private(page);
1355d4906e1aSLee Schermerhorn 	return map_swap_entry(entry, bdev);
1356d4906e1aSLee Schermerhorn }
1357d4906e1aSLee Schermerhorn 
1358d4906e1aSLee Schermerhorn /*
13591da177e4SLinus Torvalds  * Free all of a swapdev's extent information
13601da177e4SLinus Torvalds  */
13611da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
13621da177e4SLinus Torvalds {
13639625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
13641da177e4SLinus Torvalds 		struct swap_extent *se;
13651da177e4SLinus Torvalds 
13669625a5f2SHugh Dickins 		se = list_entry(sis->first_swap_extent.list.next,
13671da177e4SLinus Torvalds 				struct swap_extent, list);
13681da177e4SLinus Torvalds 		list_del(&se->list);
13691da177e4SLinus Torvalds 		kfree(se);
13701da177e4SLinus Torvalds 	}
13711da177e4SLinus Torvalds }
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds /*
13741da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
137511d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
13761da177e4SLinus Torvalds  *
137711d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
13781da177e4SLinus Torvalds  */
13791da177e4SLinus Torvalds static int
13801da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
13811da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
13821da177e4SLinus Torvalds {
13831da177e4SLinus Torvalds 	struct swap_extent *se;
13841da177e4SLinus Torvalds 	struct swap_extent *new_se;
13851da177e4SLinus Torvalds 	struct list_head *lh;
13861da177e4SLinus Torvalds 
13879625a5f2SHugh Dickins 	if (start_page == 0) {
13889625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
13899625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
13909625a5f2SHugh Dickins 		se->start_page = 0;
13919625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
13929625a5f2SHugh Dickins 		se->start_block = start_block;
13939625a5f2SHugh Dickins 		return 1;
13949625a5f2SHugh Dickins 	} else {
13959625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
13961da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
139711d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
139811d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
13991da177e4SLinus Torvalds 			/* Merge it */
14001da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
14011da177e4SLinus Torvalds 			return 0;
14021da177e4SLinus Torvalds 		}
14031da177e4SLinus Torvalds 	}
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/*
14061da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
14071da177e4SLinus Torvalds 	 */
14081da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
14091da177e4SLinus Torvalds 	if (new_se == NULL)
14101da177e4SLinus Torvalds 		return -ENOMEM;
14111da177e4SLinus Torvalds 	new_se->start_page = start_page;
14121da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
14131da177e4SLinus Torvalds 	new_se->start_block = start_block;
14141da177e4SLinus Torvalds 
14159625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
141653092a74SHugh Dickins 	return 1;
14171da177e4SLinus Torvalds }
14181da177e4SLinus Torvalds 
14191da177e4SLinus Torvalds /*
14201da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
14211da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
14221da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
14231da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
14241da177e4SLinus Torvalds  *
14251da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
14261da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
14271da177e4SLinus Torvalds  * swap files identically.
14281da177e4SLinus Torvalds  *
14291da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
14301da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
14311da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
14321da177e4SLinus Torvalds  *
14331da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
14341da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
14351da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
14361da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
14371da177e4SLinus Torvalds  * for swapping.
14381da177e4SLinus Torvalds  *
1439b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
14401da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
14411da177e4SLinus Torvalds  * which will scribble on the fs.
14421da177e4SLinus Torvalds  *
14431da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
14441da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
14451da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
14461da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
14471da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
14481da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
14491da177e4SLinus Torvalds  */
145053092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
14511da177e4SLinus Torvalds {
14521da177e4SLinus Torvalds 	struct inode *inode;
14531da177e4SLinus Torvalds 	unsigned blocks_per_page;
14541da177e4SLinus Torvalds 	unsigned long page_no;
14551da177e4SLinus Torvalds 	unsigned blkbits;
14561da177e4SLinus Torvalds 	sector_t probe_block;
14571da177e4SLinus Torvalds 	sector_t last_block;
145853092a74SHugh Dickins 	sector_t lowest_block = -1;
145953092a74SHugh Dickins 	sector_t highest_block = 0;
146053092a74SHugh Dickins 	int nr_extents = 0;
14611da177e4SLinus Torvalds 	int ret;
14621da177e4SLinus Torvalds 
14631da177e4SLinus Torvalds 	inode = sis->swap_file->f_mapping->host;
14641da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
14651da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
146653092a74SHugh Dickins 		*span = sis->pages;
14679625a5f2SHugh Dickins 		goto out;
14681da177e4SLinus Torvalds 	}
14691da177e4SLinus Torvalds 
14701da177e4SLinus Torvalds 	blkbits = inode->i_blkbits;
14711da177e4SLinus Torvalds 	blocks_per_page = PAGE_SIZE >> blkbits;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	/*
14741da177e4SLinus Torvalds 	 * Map all the blocks into the extent list.  This code doesn't try
14751da177e4SLinus Torvalds 	 * to be very smart.
14761da177e4SLinus Torvalds 	 */
14771da177e4SLinus Torvalds 	probe_block = 0;
14781da177e4SLinus Torvalds 	page_no = 0;
14791da177e4SLinus Torvalds 	last_block = i_size_read(inode) >> blkbits;
14801da177e4SLinus Torvalds 	while ((probe_block + blocks_per_page) <= last_block &&
14811da177e4SLinus Torvalds 			page_no < sis->max) {
14821da177e4SLinus Torvalds 		unsigned block_in_page;
14831da177e4SLinus Torvalds 		sector_t first_block;
14841da177e4SLinus Torvalds 
14851da177e4SLinus Torvalds 		first_block = bmap(inode, probe_block);
14861da177e4SLinus Torvalds 		if (first_block == 0)
14871da177e4SLinus Torvalds 			goto bad_bmap;
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 		/*
14901da177e4SLinus Torvalds 		 * It must be PAGE_SIZE aligned on-disk
14911da177e4SLinus Torvalds 		 */
14921da177e4SLinus Torvalds 		if (first_block & (blocks_per_page - 1)) {
14931da177e4SLinus Torvalds 			probe_block++;
14941da177e4SLinus Torvalds 			goto reprobe;
14951da177e4SLinus Torvalds 		}
14961da177e4SLinus Torvalds 
14971da177e4SLinus Torvalds 		for (block_in_page = 1; block_in_page < blocks_per_page;
14981da177e4SLinus Torvalds 					block_in_page++) {
14991da177e4SLinus Torvalds 			sector_t block;
15001da177e4SLinus Torvalds 
15011da177e4SLinus Torvalds 			block = bmap(inode, probe_block + block_in_page);
15021da177e4SLinus Torvalds 			if (block == 0)
15031da177e4SLinus Torvalds 				goto bad_bmap;
15041da177e4SLinus Torvalds 			if (block != first_block + block_in_page) {
15051da177e4SLinus Torvalds 				/* Discontiguity */
15061da177e4SLinus Torvalds 				probe_block++;
15071da177e4SLinus Torvalds 				goto reprobe;
15081da177e4SLinus Torvalds 			}
15091da177e4SLinus Torvalds 		}
15101da177e4SLinus Torvalds 
151153092a74SHugh Dickins 		first_block >>= (PAGE_SHIFT - blkbits);
151253092a74SHugh Dickins 		if (page_no) {	/* exclude the header page */
151353092a74SHugh Dickins 			if (first_block < lowest_block)
151453092a74SHugh Dickins 				lowest_block = first_block;
151553092a74SHugh Dickins 			if (first_block > highest_block)
151653092a74SHugh Dickins 				highest_block = first_block;
151753092a74SHugh Dickins 		}
151853092a74SHugh Dickins 
15191da177e4SLinus Torvalds 		/*
15201da177e4SLinus Torvalds 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
15211da177e4SLinus Torvalds 		 */
152253092a74SHugh Dickins 		ret = add_swap_extent(sis, page_no, 1, first_block);
152353092a74SHugh Dickins 		if (ret < 0)
15241da177e4SLinus Torvalds 			goto out;
152553092a74SHugh Dickins 		nr_extents += ret;
15261da177e4SLinus Torvalds 		page_no++;
15271da177e4SLinus Torvalds 		probe_block += blocks_per_page;
15281da177e4SLinus Torvalds reprobe:
15291da177e4SLinus Torvalds 		continue;
15301da177e4SLinus Torvalds 	}
153153092a74SHugh Dickins 	ret = nr_extents;
153253092a74SHugh Dickins 	*span = 1 + highest_block - lowest_block;
15331da177e4SLinus Torvalds 	if (page_no == 0)
1534e2244ec2SHugh Dickins 		page_no = 1;	/* force Empty message */
15351da177e4SLinus Torvalds 	sis->max = page_no;
1536e2244ec2SHugh Dickins 	sis->pages = page_no - 1;
15371da177e4SLinus Torvalds 	sis->highest_bit = page_no - 1;
15389625a5f2SHugh Dickins out:
15399625a5f2SHugh Dickins 	return ret;
15401da177e4SLinus Torvalds bad_bmap:
15411da177e4SLinus Torvalds 	printk(KERN_ERR "swapon: swapfile has holes\n");
15421da177e4SLinus Torvalds 	ret = -EINVAL;
15439625a5f2SHugh Dickins 	goto out;
15441da177e4SLinus Torvalds }
15451da177e4SLinus Torvalds 
1546c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
15471da177e4SLinus Torvalds {
15481da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
15498d69aaeeSHugh Dickins 	unsigned char *swap_map;
15501da177e4SLinus Torvalds 	struct file *swap_file, *victim;
15511da177e4SLinus Torvalds 	struct address_space *mapping;
15521da177e4SLinus Torvalds 	struct inode *inode;
15531da177e4SLinus Torvalds 	char *pathname;
15541da177e4SLinus Torvalds 	int i, type, prev;
15551da177e4SLinus Torvalds 	int err;
15561da177e4SLinus Torvalds 
15571da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15581da177e4SLinus Torvalds 		return -EPERM;
15591da177e4SLinus Torvalds 
15601da177e4SLinus Torvalds 	pathname = getname(specialfile);
15611da177e4SLinus Torvalds 	err = PTR_ERR(pathname);
15621da177e4SLinus Torvalds 	if (IS_ERR(pathname))
15631da177e4SLinus Torvalds 		goto out;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
15661da177e4SLinus Torvalds 	putname(pathname);
15671da177e4SLinus Torvalds 	err = PTR_ERR(victim);
15681da177e4SLinus Torvalds 	if (IS_ERR(victim))
15691da177e4SLinus Torvalds 		goto out;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	mapping = victim->f_mapping;
15721da177e4SLinus Torvalds 	prev = -1;
15735d337b91SHugh Dickins 	spin_lock(&swap_lock);
1574efa90a98SHugh Dickins 	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1575efa90a98SHugh Dickins 		p = swap_info[type];
157622c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
15771da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
15781da177e4SLinus Torvalds 				break;
15791da177e4SLinus Torvalds 		}
15801da177e4SLinus Torvalds 		prev = type;
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds 	if (type < 0) {
15831da177e4SLinus Torvalds 		err = -EINVAL;
15845d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15851da177e4SLinus Torvalds 		goto out_dput;
15861da177e4SLinus Torvalds 	}
15871da177e4SLinus Torvalds 	if (!security_vm_enough_memory(p->pages))
15881da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
15891da177e4SLinus Torvalds 	else {
15901da177e4SLinus Torvalds 		err = -ENOMEM;
15915d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15921da177e4SLinus Torvalds 		goto out_dput;
15931da177e4SLinus Torvalds 	}
1594efa90a98SHugh Dickins 	if (prev < 0)
15951da177e4SLinus Torvalds 		swap_list.head = p->next;
1596efa90a98SHugh Dickins 	else
1597efa90a98SHugh Dickins 		swap_info[prev]->next = p->next;
15981da177e4SLinus Torvalds 	if (type == swap_list.next) {
15991da177e4SLinus Torvalds 		/* just pick something that's safe... */
16001da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
16011da177e4SLinus Torvalds 	}
160278ecba08SHugh Dickins 	if (p->prio < 0) {
1603efa90a98SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i]->next)
1604efa90a98SHugh Dickins 			swap_info[i]->prio = p->prio--;
160578ecba08SHugh Dickins 		least_priority++;
160678ecba08SHugh Dickins 	}
16071da177e4SLinus Torvalds 	nr_swap_pages -= p->pages;
16081da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
16091da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
16105d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1611fb4f88dcSHugh Dickins 
161235451beeSHugh Dickins 	current->flags |= PF_OOM_ORIGIN;
16131da177e4SLinus Torvalds 	err = try_to_unuse(type);
161435451beeSHugh Dickins 	current->flags &= ~PF_OOM_ORIGIN;
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds 	if (err) {
16171da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
16185d337b91SHugh Dickins 		spin_lock(&swap_lock);
161978ecba08SHugh Dickins 		if (p->prio < 0)
162078ecba08SHugh Dickins 			p->prio = --least_priority;
162178ecba08SHugh Dickins 		prev = -1;
1622efa90a98SHugh Dickins 		for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1623efa90a98SHugh Dickins 			if (p->prio >= swap_info[i]->prio)
16241da177e4SLinus Torvalds 				break;
162578ecba08SHugh Dickins 			prev = i;
162678ecba08SHugh Dickins 		}
16271da177e4SLinus Torvalds 		p->next = i;
16281da177e4SLinus Torvalds 		if (prev < 0)
1629efa90a98SHugh Dickins 			swap_list.head = swap_list.next = type;
16301da177e4SLinus Torvalds 		else
1631efa90a98SHugh Dickins 			swap_info[prev]->next = type;
16321da177e4SLinus Torvalds 		nr_swap_pages += p->pages;
16331da177e4SLinus Torvalds 		total_swap_pages += p->pages;
16341da177e4SLinus Torvalds 		p->flags |= SWP_WRITEOK;
16355d337b91SHugh Dickins 		spin_unlock(&swap_lock);
16361da177e4SLinus Torvalds 		goto out_dput;
16371da177e4SLinus Torvalds 	}
163852b7efdbSHugh Dickins 
163952b7efdbSHugh Dickins 	/* wait for any unplug function to finish */
164052b7efdbSHugh Dickins 	down_write(&swap_unplug_sem);
164152b7efdbSHugh Dickins 	up_write(&swap_unplug_sem);
164252b7efdbSHugh Dickins 
16434cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1644570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
1645570a335bSHugh Dickins 		free_swap_count_continuations(p);
1646570a335bSHugh Dickins 
1647fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16485d337b91SHugh Dickins 	spin_lock(&swap_lock);
16491da177e4SLinus Torvalds 	drain_mmlist();
16505d337b91SHugh Dickins 
16515d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
16525d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
16535d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
16545d337b91SHugh Dickins 		spin_unlock(&swap_lock);
165513e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
16565d337b91SHugh Dickins 		spin_lock(&swap_lock);
16575d337b91SHugh Dickins 	}
16585d337b91SHugh Dickins 
16591da177e4SLinus Torvalds 	swap_file = p->swap_file;
16601da177e4SLinus Torvalds 	p->swap_file = NULL;
16611da177e4SLinus Torvalds 	p->max = 0;
16621da177e4SLinus Torvalds 	swap_map = p->swap_map;
16631da177e4SLinus Torvalds 	p->swap_map = NULL;
16641da177e4SLinus Torvalds 	p->flags = 0;
16655d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1666fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
16671da177e4SLinus Torvalds 	vfree(swap_map);
166827a7faa0SKAMEZAWA Hiroyuki 	/* Destroy swap account informatin */
166927a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
167027a7faa0SKAMEZAWA Hiroyuki 
16711da177e4SLinus Torvalds 	inode = mapping->host;
16721da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
16731da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
16741da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
16751da177e4SLinus Torvalds 		bd_release(bdev);
16761da177e4SLinus Torvalds 	} else {
16771b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
16781da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
16791b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
16801da177e4SLinus Torvalds 	}
16811da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
16821da177e4SLinus Torvalds 	err = 0;
16831da177e4SLinus Torvalds 
16841da177e4SLinus Torvalds out_dput:
16851da177e4SLinus Torvalds 	filp_close(victim, NULL);
16861da177e4SLinus Torvalds out:
16871da177e4SLinus Torvalds 	return err;
16881da177e4SLinus Torvalds }
16891da177e4SLinus Torvalds 
16901da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
16911da177e4SLinus Torvalds /* iterator */
16921da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
16931da177e4SLinus Torvalds {
1694efa90a98SHugh Dickins 	struct swap_info_struct *si;
1695efa90a98SHugh Dickins 	int type;
16961da177e4SLinus Torvalds 	loff_t l = *pos;
16971da177e4SLinus Torvalds 
1698fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16991da177e4SLinus Torvalds 
1700881e4aabSSuleiman Souhlal 	if (!l)
1701881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1702881e4aabSSuleiman Souhlal 
1703efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1704efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1705efa90a98SHugh Dickins 		si = swap_info[type];
1706efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
17071da177e4SLinus Torvalds 			continue;
1708881e4aabSSuleiman Souhlal 		if (!--l)
1709efa90a98SHugh Dickins 			return si;
17101da177e4SLinus Torvalds 	}
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	return NULL;
17131da177e4SLinus Torvalds }
17141da177e4SLinus Torvalds 
17151da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
17161da177e4SLinus Torvalds {
1717efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
1718efa90a98SHugh Dickins 	int type;
17191da177e4SLinus Torvalds 
1720881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1721efa90a98SHugh Dickins 		type = 0;
1722efa90a98SHugh Dickins 	else
1723efa90a98SHugh Dickins 		type = si->type + 1;
1724881e4aabSSuleiman Souhlal 
1725efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
1726efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1727efa90a98SHugh Dickins 		si = swap_info[type];
1728efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
17291da177e4SLinus Torvalds 			continue;
17301da177e4SLinus Torvalds 		++*pos;
1731efa90a98SHugh Dickins 		return si;
17321da177e4SLinus Torvalds 	}
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds 	return NULL;
17351da177e4SLinus Torvalds }
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
17381da177e4SLinus Torvalds {
1739fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
17431da177e4SLinus Torvalds {
1744efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
17451da177e4SLinus Torvalds 	struct file *file;
17461da177e4SLinus Torvalds 	int len;
17471da177e4SLinus Torvalds 
1748efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
17491da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1750881e4aabSSuleiman Souhlal 		return 0;
1751881e4aabSSuleiman Souhlal 	}
17521da177e4SLinus Torvalds 
1753efa90a98SHugh Dickins 	file = si->swap_file;
1754c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
17556eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
17561da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
1757d3ac7f89SJosef "Jeff" Sipek 			S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
17581da177e4SLinus Torvalds 				"partition" : "file\t",
1759efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
1760efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
1761efa90a98SHugh Dickins 			si->prio);
17621da177e4SLinus Torvalds 	return 0;
17631da177e4SLinus Torvalds }
17641da177e4SLinus Torvalds 
176515ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
17661da177e4SLinus Torvalds 	.start =	swap_start,
17671da177e4SLinus Torvalds 	.next =		swap_next,
17681da177e4SLinus Torvalds 	.stop =		swap_stop,
17691da177e4SLinus Torvalds 	.show =		swap_show
17701da177e4SLinus Torvalds };
17711da177e4SLinus Torvalds 
17721da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	return seq_open(file, &swaps_op);
17751da177e4SLinus Torvalds }
17761da177e4SLinus Torvalds 
177715ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
17781da177e4SLinus Torvalds 	.open		= swaps_open,
17791da177e4SLinus Torvalds 	.read		= seq_read,
17801da177e4SLinus Torvalds 	.llseek		= seq_lseek,
17811da177e4SLinus Torvalds 	.release	= seq_release,
17821da177e4SLinus Torvalds };
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds static int __init procswaps_init(void)
17851da177e4SLinus Torvalds {
17863d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
17871da177e4SLinus Torvalds 	return 0;
17881da177e4SLinus Torvalds }
17891da177e4SLinus Torvalds __initcall(procswaps_init);
17901da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
17911da177e4SLinus Torvalds 
17921796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
17931796316aSJan Beulich static int __init max_swapfiles_check(void)
17941796316aSJan Beulich {
17951796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
17961796316aSJan Beulich 	return 0;
17971796316aSJan Beulich }
17981796316aSJan Beulich late_initcall(max_swapfiles_check);
17991796316aSJan Beulich #endif
18001796316aSJan Beulich 
18011da177e4SLinus Torvalds /*
18021da177e4SLinus Torvalds  * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
18031da177e4SLinus Torvalds  *
18041da177e4SLinus Torvalds  * The swapon system call
18051da177e4SLinus Torvalds  */
1806c4ea37c2SHeiko Carstens SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
18071da177e4SLinus Torvalds {
18081da177e4SLinus Torvalds 	struct swap_info_struct *p;
18091da177e4SLinus Torvalds 	char *name = NULL;
18101da177e4SLinus Torvalds 	struct block_device *bdev = NULL;
18111da177e4SLinus Torvalds 	struct file *swap_file = NULL;
18121da177e4SLinus Torvalds 	struct address_space *mapping;
18131da177e4SLinus Torvalds 	unsigned int type;
18141da177e4SLinus Torvalds 	int i, prev;
18151da177e4SLinus Torvalds 	int error;
1816ad2bd7e0SHugh Dickins 	union swap_header *swap_header;
1817ad2bd7e0SHugh Dickins 	unsigned int nr_good_pages;
18186eb396dcSHugh Dickins 	int nr_extents = 0;
181953092a74SHugh Dickins 	sector_t span;
1820ad2bd7e0SHugh Dickins 	unsigned long maxpages;
182173fd8748SHugh Dickins 	unsigned long swapfilepages;
18228d69aaeeSHugh Dickins 	unsigned char *swap_map = NULL;
18231da177e4SLinus Torvalds 	struct page *page = NULL;
18241da177e4SLinus Torvalds 	struct inode *inode = NULL;
18251da177e4SLinus Torvalds 	int did_down = 0;
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
18281da177e4SLinus Torvalds 		return -EPERM;
1829efa90a98SHugh Dickins 
1830efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1831efa90a98SHugh Dickins 	if (!p)
1832efa90a98SHugh Dickins 		return -ENOMEM;
1833efa90a98SHugh Dickins 
18345d337b91SHugh Dickins 	spin_lock(&swap_lock);
1835efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1836efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
18371da177e4SLinus Torvalds 			break;
1838efa90a98SHugh Dickins 	}
18391da177e4SLinus Torvalds 	error = -EPERM;
18400697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
18415d337b91SHugh Dickins 		spin_unlock(&swap_lock);
1842efa90a98SHugh Dickins 		kfree(p);
18431da177e4SLinus Torvalds 		goto out;
18441da177e4SLinus Torvalds 	}
1845efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
1846efa90a98SHugh Dickins 		p->type = type;
1847efa90a98SHugh Dickins 		swap_info[type] = p;
1848efa90a98SHugh Dickins 		/*
1849efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
1850efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
1851efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
1852efa90a98SHugh Dickins 		 */
1853efa90a98SHugh Dickins 		smp_wmb();
1854efa90a98SHugh Dickins 		nr_swapfiles++;
1855efa90a98SHugh Dickins 	} else {
1856efa90a98SHugh Dickins 		kfree(p);
1857efa90a98SHugh Dickins 		p = swap_info[type];
1858efa90a98SHugh Dickins 		/*
1859efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
1860efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
1861efa90a98SHugh Dickins 		 */
1862efa90a98SHugh Dickins 	}
18639625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
18641da177e4SLinus Torvalds 	p->flags = SWP_USED;
18651da177e4SLinus Torvalds 	p->next = -1;
18665d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1867efa90a98SHugh Dickins 
18681da177e4SLinus Torvalds 	name = getname(specialfile);
18691da177e4SLinus Torvalds 	error = PTR_ERR(name);
18701da177e4SLinus Torvalds 	if (IS_ERR(name)) {
18711da177e4SLinus Torvalds 		name = NULL;
18721da177e4SLinus Torvalds 		goto bad_swap_2;
18731da177e4SLinus Torvalds 	}
18741da177e4SLinus Torvalds 	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
18751da177e4SLinus Torvalds 	error = PTR_ERR(swap_file);
18761da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
18771da177e4SLinus Torvalds 		swap_file = NULL;
18781da177e4SLinus Torvalds 		goto bad_swap_2;
18791da177e4SLinus Torvalds 	}
18801da177e4SLinus Torvalds 
18811da177e4SLinus Torvalds 	p->swap_file = swap_file;
18821da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
18831da177e4SLinus Torvalds 	inode = mapping->host;
18841da177e4SLinus Torvalds 
18851da177e4SLinus Torvalds 	error = -EBUSY;
18861da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
1887efa90a98SHugh Dickins 		struct swap_info_struct *q = swap_info[i];
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 		if (i == type || !q->swap_file)
18901da177e4SLinus Torvalds 			continue;
18911da177e4SLinus Torvalds 		if (mapping == q->swap_file->f_mapping)
18921da177e4SLinus Torvalds 			goto bad_swap;
18931da177e4SLinus Torvalds 	}
18941da177e4SLinus Torvalds 
18951da177e4SLinus Torvalds 	error = -EINVAL;
18961da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
18971da177e4SLinus Torvalds 		bdev = I_BDEV(inode);
18981da177e4SLinus Torvalds 		error = bd_claim(bdev, sys_swapon);
18991da177e4SLinus Torvalds 		if (error < 0) {
19001da177e4SLinus Torvalds 			bdev = NULL;
1901f7b3a435SRob Landley 			error = -EINVAL;
19021da177e4SLinus Torvalds 			goto bad_swap;
19031da177e4SLinus Torvalds 		}
19041da177e4SLinus Torvalds 		p->old_block_size = block_size(bdev);
19051da177e4SLinus Torvalds 		error = set_blocksize(bdev, PAGE_SIZE);
19061da177e4SLinus Torvalds 		if (error < 0)
19071da177e4SLinus Torvalds 			goto bad_swap;
19081da177e4SLinus Torvalds 		p->bdev = bdev;
1909b2725643SNitin Gupta 		p->flags |= SWP_BLKDEV;
19101da177e4SLinus Torvalds 	} else if (S_ISREG(inode->i_mode)) {
19111da177e4SLinus Torvalds 		p->bdev = inode->i_sb->s_bdev;
19121b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
19131da177e4SLinus Torvalds 		did_down = 1;
19141da177e4SLinus Torvalds 		if (IS_SWAPFILE(inode)) {
19151da177e4SLinus Torvalds 			error = -EBUSY;
19161da177e4SLinus Torvalds 			goto bad_swap;
19171da177e4SLinus Torvalds 		}
19181da177e4SLinus Torvalds 	} else {
19191da177e4SLinus Torvalds 		goto bad_swap;
19201da177e4SLinus Torvalds 	}
19211da177e4SLinus Torvalds 
192273fd8748SHugh Dickins 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	/*
19251da177e4SLinus Torvalds 	 * Read the swap header.
19261da177e4SLinus Torvalds 	 */
19271da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
19281da177e4SLinus Torvalds 		error = -EINVAL;
19291da177e4SLinus Torvalds 		goto bad_swap;
19301da177e4SLinus Torvalds 	}
1931090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
19321da177e4SLinus Torvalds 	if (IS_ERR(page)) {
19331da177e4SLinus Torvalds 		error = PTR_ERR(page);
19341da177e4SLinus Torvalds 		goto bad_swap;
19351da177e4SLinus Torvalds 	}
193681e33971SHugh Dickins 	swap_header = kmap(page);
19371da177e4SLinus Torvalds 
193881e33971SHugh Dickins 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1939e97a3111SJesper Juhl 		printk(KERN_ERR "Unable to find swap-space signature\n");
19401da177e4SLinus Torvalds 		error = -EINVAL;
19411da177e4SLinus Torvalds 		goto bad_swap;
19421da177e4SLinus Torvalds 	}
19431da177e4SLinus Torvalds 
1944797df574SChris Dearman 	/* swap partition endianess hack... */
1945797df574SChris Dearman 	if (swab32(swap_header->info.version) == 1) {
1946797df574SChris Dearman 		swab32s(&swap_header->info.version);
1947797df574SChris Dearman 		swab32s(&swap_header->info.last_page);
1948797df574SChris Dearman 		swab32s(&swap_header->info.nr_badpages);
1949797df574SChris Dearman 		for (i = 0; i < swap_header->info.nr_badpages; i++)
1950797df574SChris Dearman 			swab32s(&swap_header->info.badpages[i]);
1951797df574SChris Dearman 	}
195281e33971SHugh Dickins 	/* Check the swap header's sub-version */
19531da177e4SLinus Torvalds 	if (swap_header->info.version != 1) {
19541da177e4SLinus Torvalds 		printk(KERN_WARNING
19551da177e4SLinus Torvalds 		       "Unable to handle swap header version %d\n",
19561da177e4SLinus Torvalds 		       swap_header->info.version);
19571da177e4SLinus Torvalds 		error = -EINVAL;
19581da177e4SLinus Torvalds 		goto bad_swap;
19591da177e4SLinus Torvalds 	}
19601da177e4SLinus Torvalds 
19611da177e4SLinus Torvalds 	p->lowest_bit  = 1;
196252b7efdbSHugh Dickins 	p->cluster_next = 1;
1963efa90a98SHugh Dickins 	p->cluster_nr = 0;
196452b7efdbSHugh Dickins 
19651da177e4SLinus Torvalds 	/*
19661da177e4SLinus Torvalds 	 * Find out how many pages are allowed for a single swap
19671da177e4SLinus Torvalds 	 * device. There are two limiting factors: 1) the number of
19681da177e4SLinus Torvalds 	 * bits for the swap offset in the swp_entry_t type and
19691da177e4SLinus Torvalds 	 * 2) the number of bits in the a swap pte as defined by
19701da177e4SLinus Torvalds 	 * the different architectures. In order to find the
19711da177e4SLinus Torvalds 	 * largest possible bit mask a swap entry with swap type 0
19721da177e4SLinus Torvalds 	 * and swap offset ~0UL is created, encoded to a swap pte,
19731da177e4SLinus Torvalds 	 * decoded to a swp_entry_t again and finally the swap
19741da177e4SLinus Torvalds 	 * offset is extracted. This will mask all the bits from
19751da177e4SLinus Torvalds 	 * the initial ~0UL mask that can't be encoded in either
19761da177e4SLinus Torvalds 	 * the swp_entry_t or the architecture definition of a
19771da177e4SLinus Torvalds 	 * swap pte.
19781da177e4SLinus Torvalds 	 */
197981e33971SHugh Dickins 	maxpages = swp_offset(pte_to_swp_entry(
1980ad2bd7e0SHugh Dickins 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
1981ad2bd7e0SHugh Dickins 	if (maxpages > swap_header->info.last_page) {
1982ad2bd7e0SHugh Dickins 		maxpages = swap_header->info.last_page + 1;
1983ad2bd7e0SHugh Dickins 		/* p->max is an unsigned int: don't overflow it */
1984ad2bd7e0SHugh Dickins 		if ((unsigned int)maxpages == 0)
1985ad2bd7e0SHugh Dickins 			maxpages = UINT_MAX;
1986ad2bd7e0SHugh Dickins 	}
19871da177e4SLinus Torvalds 	p->highest_bit = maxpages - 1;
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds 	error = -EINVAL;
1990e2244ec2SHugh Dickins 	if (!maxpages)
1991e2244ec2SHugh Dickins 		goto bad_swap;
199273fd8748SHugh Dickins 	if (swapfilepages && maxpages > swapfilepages) {
19935d1854e1SEric Sandeen 		printk(KERN_WARNING
19945d1854e1SEric Sandeen 		       "Swap area shorter than signature indicates\n");
19955d1854e1SEric Sandeen 		goto bad_swap;
19965d1854e1SEric Sandeen 	}
1997e2244ec2SHugh Dickins 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1998e2244ec2SHugh Dickins 		goto bad_swap;
19991da177e4SLinus Torvalds 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
20001da177e4SLinus Torvalds 		goto bad_swap;
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
20038d69aaeeSHugh Dickins 	swap_map = vmalloc(maxpages);
200478ecba08SHugh Dickins 	if (!swap_map) {
20051da177e4SLinus Torvalds 		error = -ENOMEM;
20061da177e4SLinus Torvalds 		goto bad_swap;
20071da177e4SLinus Torvalds 	}
20081da177e4SLinus Torvalds 
20098d69aaeeSHugh Dickins 	memset(swap_map, 0, maxpages);
2010ad2bd7e0SHugh Dickins 	nr_good_pages = maxpages - 1;	/* omit header page */
2011ad2bd7e0SHugh Dickins 
20121da177e4SLinus Torvalds 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
2013ad2bd7e0SHugh Dickins 		unsigned int page_nr = swap_header->info.badpages[i];
2014ad2bd7e0SHugh Dickins 		if (page_nr == 0 || page_nr > swap_header->info.last_page) {
20151da177e4SLinus Torvalds 			error = -EINVAL;
201681e33971SHugh Dickins 			goto bad_swap;
201781e33971SHugh Dickins 		}
2018ad2bd7e0SHugh Dickins 		if (page_nr < maxpages) {
201978ecba08SHugh Dickins 			swap_map[page_nr] = SWAP_MAP_BAD;
2020ad2bd7e0SHugh Dickins 			nr_good_pages--;
2021ad2bd7e0SHugh Dickins 		}
20221da177e4SLinus Torvalds 	}
202327a7faa0SKAMEZAWA Hiroyuki 
202427a7faa0SKAMEZAWA Hiroyuki 	error = swap_cgroup_swapon(type, maxpages);
202527a7faa0SKAMEZAWA Hiroyuki 	if (error)
202627a7faa0SKAMEZAWA Hiroyuki 		goto bad_swap;
202727a7faa0SKAMEZAWA Hiroyuki 
2028e2244ec2SHugh Dickins 	if (nr_good_pages) {
202978ecba08SHugh Dickins 		swap_map[0] = SWAP_MAP_BAD;
2030e2244ec2SHugh Dickins 		p->max = maxpages;
2031e2244ec2SHugh Dickins 		p->pages = nr_good_pages;
203253092a74SHugh Dickins 		nr_extents = setup_swap_extents(p, &span);
203353092a74SHugh Dickins 		if (nr_extents < 0) {
203453092a74SHugh Dickins 			error = nr_extents;
2035e2244ec2SHugh Dickins 			goto bad_swap;
203653092a74SHugh Dickins 		}
2037e2244ec2SHugh Dickins 		nr_good_pages = p->pages;
2038e2244ec2SHugh Dickins 	}
20391da177e4SLinus Torvalds 	if (!nr_good_pages) {
20401da177e4SLinus Torvalds 		printk(KERN_WARNING "Empty swap-file\n");
20411da177e4SLinus Torvalds 		error = -EINVAL;
20421da177e4SLinus Torvalds 		goto bad_swap;
20431da177e4SLinus Torvalds 	}
20441da177e4SLinus Torvalds 
20453bd0f0c7SSuresh Jayaraman 	if (p->bdev) {
204620137a49SHugh Dickins 		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
204720137a49SHugh Dickins 			p->flags |= SWP_SOLIDSTATE;
204820137a49SHugh Dickins 			p->cluster_next = 1 + (random32() % p->highest_bit);
204920137a49SHugh Dickins 		}
2050*33994466SHugh Dickins 		if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
20516a6ba831SHugh Dickins 			p->flags |= SWP_DISCARDABLE;
20523bd0f0c7SSuresh Jayaraman 	}
20536a6ba831SHugh Dickins 
2054fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
20555d337b91SHugh Dickins 	spin_lock(&swap_lock);
205678ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
205778ecba08SHugh Dickins 		p->prio =
205878ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
205978ecba08SHugh Dickins 	else
206078ecba08SHugh Dickins 		p->prio = --least_priority;
206178ecba08SHugh Dickins 	p->swap_map = swap_map;
206222c6f8fdSHugh Dickins 	p->flags |= SWP_WRITEOK;
20631da177e4SLinus Torvalds 	nr_swap_pages += nr_good_pages;
20641da177e4SLinus Torvalds 	total_swap_pages += nr_good_pages;
206553092a74SHugh Dickins 
20666eb396dcSHugh Dickins 	printk(KERN_INFO "Adding %uk swap on %s.  "
206720137a49SHugh Dickins 			"Priority:%d extents:%d across:%lluk %s%s\n",
206853092a74SHugh Dickins 		nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
20696a6ba831SHugh Dickins 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
207020137a49SHugh Dickins 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
20716a6ba831SHugh Dickins 		(p->flags & SWP_DISCARDABLE) ? "D" : "");
20721da177e4SLinus Torvalds 
20731da177e4SLinus Torvalds 	/* insert swap space into swap_list: */
20741da177e4SLinus Torvalds 	prev = -1;
2075efa90a98SHugh Dickins 	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
2076efa90a98SHugh Dickins 		if (p->prio >= swap_info[i]->prio)
20771da177e4SLinus Torvalds 			break;
20781da177e4SLinus Torvalds 		prev = i;
20791da177e4SLinus Torvalds 	}
20801da177e4SLinus Torvalds 	p->next = i;
2081efa90a98SHugh Dickins 	if (prev < 0)
2082efa90a98SHugh Dickins 		swap_list.head = swap_list.next = type;
2083efa90a98SHugh Dickins 	else
2084efa90a98SHugh Dickins 		swap_info[prev]->next = type;
20855d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2086fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
20871da177e4SLinus Torvalds 	error = 0;
20881da177e4SLinus Torvalds 	goto out;
20891da177e4SLinus Torvalds bad_swap:
20901da177e4SLinus Torvalds 	if (bdev) {
20911da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
20921da177e4SLinus Torvalds 		bd_release(bdev);
20931da177e4SLinus Torvalds 	}
20944cd3bb10SHugh Dickins 	destroy_swap_extents(p);
209527a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
20961da177e4SLinus Torvalds bad_swap_2:
20975d337b91SHugh Dickins 	spin_lock(&swap_lock);
20981da177e4SLinus Torvalds 	p->swap_file = NULL;
20991da177e4SLinus Torvalds 	p->flags = 0;
21005d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21011da177e4SLinus Torvalds 	vfree(swap_map);
21021da177e4SLinus Torvalds 	if (swap_file)
21031da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
21041da177e4SLinus Torvalds out:
21051da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
21061da177e4SLinus Torvalds 		kunmap(page);
21071da177e4SLinus Torvalds 		page_cache_release(page);
21081da177e4SLinus Torvalds 	}
21091da177e4SLinus Torvalds 	if (name)
21101da177e4SLinus Torvalds 		putname(name);
21111da177e4SLinus Torvalds 	if (did_down) {
21121da177e4SLinus Torvalds 		if (!error)
21131da177e4SLinus Torvalds 			inode->i_flags |= S_SWAPFILE;
21141b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
21151da177e4SLinus Torvalds 	}
21161da177e4SLinus Torvalds 	return error;
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
21201da177e4SLinus Torvalds {
2121efa90a98SHugh Dickins 	unsigned int type;
21221da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
21231da177e4SLinus Torvalds 
21245d337b91SHugh Dickins 	spin_lock(&swap_lock);
2125efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2126efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
2127efa90a98SHugh Dickins 
2128efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2129efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
21301da177e4SLinus Torvalds 	}
21311da177e4SLinus Torvalds 	val->freeswap = nr_swap_pages + nr_to_be_unused;
21321da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
21335d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21341da177e4SLinus Torvalds }
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds /*
21371da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
21381da177e4SLinus Torvalds  *
2139355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
2140355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
2141355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
2142355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
2143355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
2144355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
2145570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
21461da177e4SLinus Torvalds  */
21478d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
21481da177e4SLinus Torvalds {
21491da177e4SLinus Torvalds 	struct swap_info_struct *p;
21501da177e4SLinus Torvalds 	unsigned long offset, type;
21518d69aaeeSHugh Dickins 	unsigned char count;
21528d69aaeeSHugh Dickins 	unsigned char has_cache;
2153253d553bSHugh Dickins 	int err = -EINVAL;
21541da177e4SLinus Torvalds 
2155a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
2156253d553bSHugh Dickins 		goto out;
21570697212aSChristoph Lameter 
21581da177e4SLinus Torvalds 	type = swp_type(entry);
21591da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
21601da177e4SLinus Torvalds 		goto bad_file;
2161efa90a98SHugh Dickins 	p = swap_info[type];
21621da177e4SLinus Torvalds 	offset = swp_offset(entry);
21631da177e4SLinus Torvalds 
21645d337b91SHugh Dickins 	spin_lock(&swap_lock);
2165355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
2166355cfa73SKAMEZAWA Hiroyuki 		goto unlock_out;
2167355cfa73SKAMEZAWA Hiroyuki 
2168253d553bSHugh Dickins 	count = p->swap_map[offset];
2169253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
2170253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
2171253d553bSHugh Dickins 	err = 0;
2172355cfa73SKAMEZAWA Hiroyuki 
2173253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
2174355cfa73SKAMEZAWA Hiroyuki 
2175355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2176253d553bSHugh Dickins 		if (!has_cache && count)
2177253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
2178253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
2179253d553bSHugh Dickins 			err = -EEXIST;
2180253d553bSHugh Dickins 		else				/* no users remaining */
2181253d553bSHugh Dickins 			err = -ENOENT;
2182355cfa73SKAMEZAWA Hiroyuki 
2183355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
2184253d553bSHugh Dickins 
2185570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2186570a335bSHugh Dickins 			count += usage;
2187570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2188253d553bSHugh Dickins 			err = -EINVAL;
2189570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
2190570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2191570a335bSHugh Dickins 		else
2192570a335bSHugh Dickins 			err = -ENOMEM;
2193253d553bSHugh Dickins 	} else
2194253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
2195253d553bSHugh Dickins 
2196253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
2197253d553bSHugh Dickins 
2198355cfa73SKAMEZAWA Hiroyuki unlock_out:
21995d337b91SHugh Dickins 	spin_unlock(&swap_lock);
22001da177e4SLinus Torvalds out:
2201253d553bSHugh Dickins 	return err;
22021da177e4SLinus Torvalds 
22031da177e4SLinus Torvalds bad_file:
22041da177e4SLinus Torvalds 	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
22051da177e4SLinus Torvalds 	goto out;
22061da177e4SLinus Torvalds }
2207253d553bSHugh Dickins 
2208355cfa73SKAMEZAWA Hiroyuki /*
2209aaa46865SHugh Dickins  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2210aaa46865SHugh Dickins  * (in which case its reference count is never incremented).
2211aaa46865SHugh Dickins  */
2212aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry)
2213aaa46865SHugh Dickins {
2214aaa46865SHugh Dickins 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
2215aaa46865SHugh Dickins }
2216aaa46865SHugh Dickins 
2217aaa46865SHugh Dickins /*
221808259d58SHugh Dickins  * Increase reference count of swap entry by 1.
221908259d58SHugh Dickins  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
222008259d58SHugh Dickins  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
222108259d58SHugh Dickins  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
222208259d58SHugh Dickins  * might occur if a page table entry has got corrupted.
2223355cfa73SKAMEZAWA Hiroyuki  */
2224570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
2225355cfa73SKAMEZAWA Hiroyuki {
2226570a335bSHugh Dickins 	int err = 0;
2227570a335bSHugh Dickins 
2228570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2229570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2230570a335bSHugh Dickins 	return err;
2231355cfa73SKAMEZAWA Hiroyuki }
22321da177e4SLinus Torvalds 
2233cb4b86baSKAMEZAWA Hiroyuki /*
2234355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
2235355cfa73SKAMEZAWA Hiroyuki  *
223673c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
2237355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
2238355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
2239355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
2240cb4b86baSKAMEZAWA Hiroyuki  */
2241cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
2242cb4b86baSKAMEZAWA Hiroyuki {
2243253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2244cb4b86baSKAMEZAWA Hiroyuki }
2245cb4b86baSKAMEZAWA Hiroyuki 
22461da177e4SLinus Torvalds /*
22475d337b91SHugh Dickins  * swap_lock prevents swap_map being freed. Don't grab an extra
22481da177e4SLinus Torvalds  * reference on the swaphandle, it doesn't matter if it becomes unused.
22491da177e4SLinus Torvalds  */
22501da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
22511da177e4SLinus Torvalds {
22528952898bSHugh Dickins 	struct swap_info_struct *si;
22533f9e7949SHugh Dickins 	int our_page_cluster = page_cluster;
22548952898bSHugh Dickins 	pgoff_t target, toff;
22558952898bSHugh Dickins 	pgoff_t base, end;
22568952898bSHugh Dickins 	int nr_pages = 0;
22571da177e4SLinus Torvalds 
22583f9e7949SHugh Dickins 	if (!our_page_cluster)	/* no readahead */
22591da177e4SLinus Torvalds 		return 0;
22608952898bSHugh Dickins 
2261efa90a98SHugh Dickins 	si = swap_info[swp_type(entry)];
22628952898bSHugh Dickins 	target = swp_offset(entry);
22638952898bSHugh Dickins 	base = (target >> our_page_cluster) << our_page_cluster;
22648952898bSHugh Dickins 	end = base + (1 << our_page_cluster);
22658952898bSHugh Dickins 	if (!base)		/* first page is swap header */
22668952898bSHugh Dickins 		base++;
22671da177e4SLinus Torvalds 
22685d337b91SHugh Dickins 	spin_lock(&swap_lock);
22698952898bSHugh Dickins 	if (end > si->max)	/* don't go beyond end of map */
22708952898bSHugh Dickins 		end = si->max;
22718952898bSHugh Dickins 
22728952898bSHugh Dickins 	/* Count contiguous allocated slots above our target */
22738952898bSHugh Dickins 	for (toff = target; ++toff < end; nr_pages++) {
22741da177e4SLinus Torvalds 		/* Don't read in free or bad pages */
22758952898bSHugh Dickins 		if (!si->swap_map[toff])
22761da177e4SLinus Torvalds 			break;
2277355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
22781da177e4SLinus Torvalds 			break;
22798952898bSHugh Dickins 	}
22808952898bSHugh Dickins 	/* Count contiguous allocated slots below our target */
22818952898bSHugh Dickins 	for (toff = target; --toff >= base; nr_pages++) {
22828952898bSHugh Dickins 		/* Don't read in free or bad pages */
22838952898bSHugh Dickins 		if (!si->swap_map[toff])
22848952898bSHugh Dickins 			break;
2285355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
22868952898bSHugh Dickins 			break;
22878952898bSHugh Dickins 	}
22885d337b91SHugh Dickins 	spin_unlock(&swap_lock);
22898952898bSHugh Dickins 
22908952898bSHugh Dickins 	/*
22918952898bSHugh Dickins 	 * Indicate starting offset, and return number of pages to get:
22928952898bSHugh Dickins 	 * if only 1, say 0, since there's then no readahead to be done.
22938952898bSHugh Dickins 	 */
22948952898bSHugh Dickins 	*offset = ++toff;
22958952898bSHugh Dickins 	return nr_pages? ++nr_pages: 0;
22961da177e4SLinus Torvalds }
2297570a335bSHugh Dickins 
2298570a335bSHugh Dickins /*
2299570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
2300570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2301570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
2302570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2303570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2304570a335bSHugh Dickins  *
2305570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
2306570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
2307570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2308570a335bSHugh Dickins  *
2309570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2310570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2311570a335bSHugh Dickins  * can be called after dropping locks.
2312570a335bSHugh Dickins  */
2313570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2314570a335bSHugh Dickins {
2315570a335bSHugh Dickins 	struct swap_info_struct *si;
2316570a335bSHugh Dickins 	struct page *head;
2317570a335bSHugh Dickins 	struct page *page;
2318570a335bSHugh Dickins 	struct page *list_page;
2319570a335bSHugh Dickins 	pgoff_t offset;
2320570a335bSHugh Dickins 	unsigned char count;
2321570a335bSHugh Dickins 
2322570a335bSHugh Dickins 	/*
2323570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2324570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2325570a335bSHugh Dickins 	 */
2326570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2327570a335bSHugh Dickins 
2328570a335bSHugh Dickins 	si = swap_info_get(entry);
2329570a335bSHugh Dickins 	if (!si) {
2330570a335bSHugh Dickins 		/*
2331570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
2332570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
2333570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
2334570a335bSHugh Dickins 		 */
2335570a335bSHugh Dickins 		goto outer;
2336570a335bSHugh Dickins 	}
2337570a335bSHugh Dickins 
2338570a335bSHugh Dickins 	offset = swp_offset(entry);
2339570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2340570a335bSHugh Dickins 
2341570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2342570a335bSHugh Dickins 		/*
2343570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
2344570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
2345570a335bSHugh Dickins 		 * over-provisioning.
2346570a335bSHugh Dickins 		 */
2347570a335bSHugh Dickins 		goto out;
2348570a335bSHugh Dickins 	}
2349570a335bSHugh Dickins 
2350570a335bSHugh Dickins 	if (!page) {
2351570a335bSHugh Dickins 		spin_unlock(&swap_lock);
2352570a335bSHugh Dickins 		return -ENOMEM;
2353570a335bSHugh Dickins 	}
2354570a335bSHugh Dickins 
2355570a335bSHugh Dickins 	/*
2356570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2357570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel pagetables: so it
2358570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2359570a335bSHugh Dickins 	 */
2360570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2361570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2362570a335bSHugh Dickins 
2363570a335bSHugh Dickins 	/*
2364570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
2365570a335bSHugh Dickins 	 * but it does always reset its private field.
2366570a335bSHugh Dickins 	 */
2367570a335bSHugh Dickins 	if (!page_private(head)) {
2368570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2369570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
2370570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
2371570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
2372570a335bSHugh Dickins 	}
2373570a335bSHugh Dickins 
2374570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
2375570a335bSHugh Dickins 		unsigned char *map;
2376570a335bSHugh Dickins 
2377570a335bSHugh Dickins 		/*
2378570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
2379570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
2380570a335bSHugh Dickins 		 */
2381570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
2382570a335bSHugh Dickins 			goto out;
2383570a335bSHugh Dickins 
2384570a335bSHugh Dickins 		map = kmap_atomic(list_page, KM_USER0) + offset;
2385570a335bSHugh Dickins 		count = *map;
2386570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2387570a335bSHugh Dickins 
2388570a335bSHugh Dickins 		/*
2389570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
2390570a335bSHugh Dickins 		 * free our allocation and use this one.
2391570a335bSHugh Dickins 		 */
2392570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2393570a335bSHugh Dickins 			goto out;
2394570a335bSHugh Dickins 	}
2395570a335bSHugh Dickins 
2396570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
2397570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
2398570a335bSHugh Dickins out:
2399570a335bSHugh Dickins 	spin_unlock(&swap_lock);
2400570a335bSHugh Dickins outer:
2401570a335bSHugh Dickins 	if (page)
2402570a335bSHugh Dickins 		__free_page(page);
2403570a335bSHugh Dickins 	return 0;
2404570a335bSHugh Dickins }
2405570a335bSHugh Dickins 
2406570a335bSHugh Dickins /*
2407570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
2408570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2409570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
2410570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
2411570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
2412570a335bSHugh Dickins  * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2413570a335bSHugh Dickins  */
2414570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
2415570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
2416570a335bSHugh Dickins {
2417570a335bSHugh Dickins 	struct page *head;
2418570a335bSHugh Dickins 	struct page *page;
2419570a335bSHugh Dickins 	unsigned char *map;
2420570a335bSHugh Dickins 
2421570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2422570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
2423570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2424570a335bSHugh Dickins 		return false;		/* need to add count continuation */
2425570a335bSHugh Dickins 	}
2426570a335bSHugh Dickins 
2427570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2428570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
2429570a335bSHugh Dickins 	map = kmap_atomic(page, KM_USER0) + offset;
2430570a335bSHugh Dickins 
2431570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2432570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2433570a335bSHugh Dickins 
2434570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2435570a335bSHugh Dickins 		/*
2436570a335bSHugh Dickins 		 * Think of how you add 1 to 999
2437570a335bSHugh Dickins 		 */
2438570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
2439570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2440570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2441570a335bSHugh Dickins 			BUG_ON(page == head);
2442570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2443570a335bSHugh Dickins 		}
2444570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
2445570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2446570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2447570a335bSHugh Dickins 			if (page == head)
2448570a335bSHugh Dickins 				return false;	/* add count continuation */
2449570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2450570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
2451570a335bSHugh Dickins 		}
2452570a335bSHugh Dickins 		*map += 1;
2453570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2454570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2455570a335bSHugh Dickins 		while (page != head) {
2456570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2457570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
2458570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2459570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2460570a335bSHugh Dickins 		}
2461570a335bSHugh Dickins 		return true;			/* incremented */
2462570a335bSHugh Dickins 
2463570a335bSHugh Dickins 	} else {				/* decrementing */
2464570a335bSHugh Dickins 		/*
2465570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
2466570a335bSHugh Dickins 		 */
2467570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
2468570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
2469570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2470570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2471570a335bSHugh Dickins 			BUG_ON(page == head);
2472570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2473570a335bSHugh Dickins 		}
2474570a335bSHugh Dickins 		BUG_ON(*map == 0);
2475570a335bSHugh Dickins 		*map -= 1;
2476570a335bSHugh Dickins 		if (*map == 0)
2477570a335bSHugh Dickins 			count = 0;
2478570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2479570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2480570a335bSHugh Dickins 		while (page != head) {
2481570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2482570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
2483570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2484570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2485570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2486570a335bSHugh Dickins 		}
2487570a335bSHugh Dickins 		return count == COUNT_CONTINUED;
2488570a335bSHugh Dickins 	}
2489570a335bSHugh Dickins }
2490570a335bSHugh Dickins 
2491570a335bSHugh Dickins /*
2492570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
2493570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2494570a335bSHugh Dickins  */
2495570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
2496570a335bSHugh Dickins {
2497570a335bSHugh Dickins 	pgoff_t offset;
2498570a335bSHugh Dickins 
2499570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2500570a335bSHugh Dickins 		struct page *head;
2501570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
2502570a335bSHugh Dickins 		if (page_private(head)) {
2503570a335bSHugh Dickins 			struct list_head *this, *next;
2504570a335bSHugh Dickins 			list_for_each_safe(this, next, &head->lru) {
2505570a335bSHugh Dickins 				struct page *page;
2506570a335bSHugh Dickins 				page = list_entry(this, struct page, lru);
2507570a335bSHugh Dickins 				list_del(this);
2508570a335bSHugh Dickins 				__free_page(page);
2509570a335bSHugh Dickins 			}
2510570a335bSHugh Dickins 		}
2511570a335bSHugh Dickins 	}
2512570a335bSHugh Dickins }
2513