xref: /openbmc/linux/mm/swapfile.c (revision 737449236240e30a7bbe99f4d5586b8ed1416763)
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>
17072441e2SHugh Dickins #include <linux/shmem_fs.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>
245ad64688SHugh Dickins #include <linux/ksm.h>
251da177e4SLinus Torvalds #include <linux/rmap.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28fc0abb14SIngo Molnar #include <linux/mutex.h>
29c59ede7bSRandy.Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/syscalls.h>
318a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
3266d7dd51SKay Sievers #include <linux/poll.h>
3372788c38SDavid Rientjes #include <linux/oom.h>
3438b5faf4SDan Magenheimer #include <linux/frontswap.h>
3538b5faf4SDan Magenheimer #include <linux/swapfile.h>
36f981c595SMel Gorman #include <linux/export.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <asm/pgtable.h>
391da177e4SLinus Torvalds #include <asm/tlbflush.h>
401da177e4SLinus Torvalds #include <linux/swapops.h>
4127a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h>
421da177e4SLinus Torvalds 
43570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
44570a335bSHugh Dickins 				 unsigned char);
45570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
46d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**);
47570a335bSHugh Dickins 
4838b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock);
497c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
50b962716bSHugh Dickins long nr_swap_pages;
511da177e4SLinus Torvalds long total_swap_pages;
5278ecba08SHugh Dickins static int least_priority;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
551da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
561da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
571da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
581da177e4SLinus Torvalds 
5938b5faf4SDan Magenheimer struct swap_list_t swap_list = {-1, -1};
601da177e4SLinus Torvalds 
6138b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES];
621da177e4SLinus Torvalds 
63fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
641da177e4SLinus Torvalds 
6566d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
6666d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */
6766d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0);
6866d7dd51SKay Sievers 
698d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
70355cfa73SKAMEZAWA Hiroyuki {
71570a335bSHugh Dickins 	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
72355cfa73SKAMEZAWA Hiroyuki }
73355cfa73SKAMEZAWA Hiroyuki 
74efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
75c9e44410SKAMEZAWA Hiroyuki static int
76c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
77c9e44410SKAMEZAWA Hiroyuki {
78efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
79c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
80c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
81c9e44410SKAMEZAWA Hiroyuki 
82c9e44410SKAMEZAWA Hiroyuki 	page = find_get_page(&swapper_space, entry.val);
83c9e44410SKAMEZAWA Hiroyuki 	if (!page)
84c9e44410SKAMEZAWA Hiroyuki 		return 0;
85c9e44410SKAMEZAWA Hiroyuki 	/*
86c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
87c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
88c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
89c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
90c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
91c9e44410SKAMEZAWA Hiroyuki 	 */
92c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
93c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
94c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
95c9e44410SKAMEZAWA Hiroyuki 	}
96c9e44410SKAMEZAWA Hiroyuki 	page_cache_release(page);
97c9e44410SKAMEZAWA Hiroyuki 	return ret;
98c9e44410SKAMEZAWA Hiroyuki }
99355cfa73SKAMEZAWA Hiroyuki 
1001da177e4SLinus Torvalds /*
1016a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1026a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1036a6ba831SHugh Dickins  */
1046a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1056a6ba831SHugh Dickins {
1066a6ba831SHugh Dickins 	struct swap_extent *se;
1079625a5f2SHugh Dickins 	sector_t start_block;
1089625a5f2SHugh Dickins 	sector_t nr_blocks;
1096a6ba831SHugh Dickins 	int err = 0;
1106a6ba831SHugh Dickins 
1116a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1129625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1139625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1149625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1159625a5f2SHugh Dickins 	if (nr_blocks) {
1169625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
117dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1189625a5f2SHugh Dickins 		if (err)
1199625a5f2SHugh Dickins 			return err;
1209625a5f2SHugh Dickins 		cond_resched();
1216a6ba831SHugh Dickins 	}
1226a6ba831SHugh Dickins 
1239625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1249625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1259625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1269625a5f2SHugh Dickins 
1276a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
128dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1296a6ba831SHugh Dickins 		if (err)
1306a6ba831SHugh Dickins 			break;
1316a6ba831SHugh Dickins 
1326a6ba831SHugh Dickins 		cond_resched();
1336a6ba831SHugh Dickins 	}
1346a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1356a6ba831SHugh Dickins }
1366a6ba831SHugh Dickins 
1377992fde7SHugh Dickins /*
1387992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1397992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1407992fde7SHugh Dickins  */
1417992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1427992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1437992fde7SHugh Dickins {
1447992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1457992fde7SHugh Dickins 	int found_extent = 0;
1467992fde7SHugh Dickins 
1477992fde7SHugh Dickins 	while (nr_pages) {
1487992fde7SHugh Dickins 		struct list_head *lh;
1497992fde7SHugh Dickins 
1507992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1517992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1527992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1537992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
154858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1557992fde7SHugh Dickins 
1567992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1577992fde7SHugh Dickins 				nr_blocks = nr_pages;
1587992fde7SHugh Dickins 			start_page += nr_blocks;
1597992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1607992fde7SHugh Dickins 
1617992fde7SHugh Dickins 			if (!found_extent++)
1627992fde7SHugh Dickins 				si->curr_swap_extent = se;
1637992fde7SHugh Dickins 
1647992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1657992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1667992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
167dd3932edSChristoph Hellwig 				    nr_blocks, GFP_NOIO, 0))
1687992fde7SHugh Dickins 				break;
1697992fde7SHugh Dickins 		}
1707992fde7SHugh Dickins 
1717992fde7SHugh Dickins 		lh = se->list.next;
1727992fde7SHugh Dickins 		se = list_entry(lh, struct swap_extent, list);
1737992fde7SHugh Dickins 	}
1747992fde7SHugh Dickins }
1757992fde7SHugh Dickins 
1767992fde7SHugh Dickins static int wait_for_discard(void *word)
1777992fde7SHugh Dickins {
1787992fde7SHugh Dickins 	schedule();
1797992fde7SHugh Dickins 	return 0;
1807992fde7SHugh Dickins }
1817992fde7SHugh Dickins 
182048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
183048c27fdSHugh Dickins #define LATENCY_LIMIT		256
184048c27fdSHugh Dickins 
18524b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si,
1868d69aaeeSHugh Dickins 				   unsigned char usage)
1871da177e4SLinus Torvalds {
188ebebbbe9SHugh Dickins 	unsigned long offset;
189c60aa176SHugh Dickins 	unsigned long scan_base;
1907992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
191048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
1927992fde7SHugh Dickins 	int found_free_cluster = 0;
1931da177e4SLinus Torvalds 
1947dfad418SHugh Dickins 	/*
1957dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
1967dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
1977dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
1987dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
1997dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
2007dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
2017dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
202c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
2031da177e4SLinus Torvalds 	 */
2047dfad418SHugh Dickins 
20552b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
206c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
207ebebbbe9SHugh Dickins 
208ebebbbe9SHugh Dickins 	if (unlikely(!si->cluster_nr--)) {
209ebebbbe9SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
2107dfad418SHugh Dickins 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
211ebebbbe9SHugh Dickins 			goto checks;
212ebebbbe9SHugh Dickins 		}
2137992fde7SHugh Dickins 		if (si->flags & SWP_DISCARDABLE) {
2147992fde7SHugh Dickins 			/*
2157992fde7SHugh Dickins 			 * Start range check on racing allocations, in case
2167992fde7SHugh Dickins 			 * they overlap the cluster we eventually decide on
2177992fde7SHugh Dickins 			 * (we scan without swap_lock to allow preemption).
2187992fde7SHugh Dickins 			 * It's hardly conceivable that cluster_nr could be
2197992fde7SHugh Dickins 			 * wrapped during our scan, but don't depend on it.
2207992fde7SHugh Dickins 			 */
2217992fde7SHugh Dickins 			if (si->lowest_alloc)
2227992fde7SHugh Dickins 				goto checks;
2237992fde7SHugh Dickins 			si->lowest_alloc = si->max;
2247992fde7SHugh Dickins 			si->highest_alloc = 0;
2257992fde7SHugh Dickins 		}
2265d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2277dfad418SHugh Dickins 
228c60aa176SHugh Dickins 		/*
229c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
230c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
231c60aa176SHugh Dickins 		 * But if seek is cheap, search from our current position, so
232c60aa176SHugh Dickins 		 * that swap is allocated from all over the partition: if the
233c60aa176SHugh Dickins 		 * Flash Translation Layer only remaps within limited zones,
234c60aa176SHugh Dickins 		 * we don't want to wear out the first zone too quickly.
235c60aa176SHugh Dickins 		 */
236c60aa176SHugh Dickins 		if (!(si->flags & SWP_SOLIDSTATE))
237c60aa176SHugh Dickins 			scan_base = offset = si->lowest_bit;
2387dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
2397dfad418SHugh Dickins 
2407dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
2417dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
2421da177e4SLinus Torvalds 			if (si->swap_map[offset])
2437dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
2447dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
2455d337b91SHugh Dickins 				spin_lock(&swap_lock);
246ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
247ebebbbe9SHugh Dickins 				si->cluster_next = offset;
248ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
2497992fde7SHugh Dickins 				found_free_cluster = 1;
250ebebbbe9SHugh Dickins 				goto checks;
2517dfad418SHugh Dickins 			}
252048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
253048c27fdSHugh Dickins 				cond_resched();
254048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
255048c27fdSHugh Dickins 			}
2567dfad418SHugh Dickins 		}
257ebebbbe9SHugh Dickins 
258ebebbbe9SHugh Dickins 		offset = si->lowest_bit;
259c60aa176SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
260c60aa176SHugh Dickins 
261c60aa176SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
262c60aa176SHugh Dickins 		for (; last_in_cluster < scan_base; offset++) {
263c60aa176SHugh Dickins 			if (si->swap_map[offset])
264c60aa176SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
265c60aa176SHugh Dickins 			else if (offset == last_in_cluster) {
266c60aa176SHugh Dickins 				spin_lock(&swap_lock);
267c60aa176SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
268c60aa176SHugh Dickins 				si->cluster_next = offset;
269c60aa176SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
270c60aa176SHugh Dickins 				found_free_cluster = 1;
271c60aa176SHugh Dickins 				goto checks;
272c60aa176SHugh Dickins 			}
273c60aa176SHugh Dickins 			if (unlikely(--latency_ration < 0)) {
274c60aa176SHugh Dickins 				cond_resched();
275c60aa176SHugh Dickins 				latency_ration = LATENCY_LIMIT;
276c60aa176SHugh Dickins 			}
277c60aa176SHugh Dickins 		}
278c60aa176SHugh Dickins 
279c60aa176SHugh Dickins 		offset = scan_base;
2805d337b91SHugh Dickins 		spin_lock(&swap_lock);
281ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
2827992fde7SHugh Dickins 		si->lowest_alloc = 0;
2837dfad418SHugh Dickins 	}
2847dfad418SHugh Dickins 
285ebebbbe9SHugh Dickins checks:
286ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
28752b7efdbSHugh Dickins 		goto no_page;
2887dfad418SHugh Dickins 	if (!si->highest_bit)
2897dfad418SHugh Dickins 		goto no_page;
290ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
291c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
292c9e44410SKAMEZAWA Hiroyuki 
293b73d7fceSHugh Dickins 	/* reuse swap entry of cache-only swap if not busy. */
294b73d7fceSHugh Dickins 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
295c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
296c9e44410SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
297c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
298c9e44410SKAMEZAWA Hiroyuki 		spin_lock(&swap_lock);
299c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
300c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
301c9e44410SKAMEZAWA Hiroyuki 			goto checks;
302c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
303c9e44410SKAMEZAWA Hiroyuki 	}
304c9e44410SKAMEZAWA Hiroyuki 
305ebebbbe9SHugh Dickins 	if (si->swap_map[offset])
306ebebbbe9SHugh Dickins 		goto scan;
307ebebbbe9SHugh Dickins 
30852b7efdbSHugh Dickins 	if (offset == si->lowest_bit)
3091da177e4SLinus Torvalds 		si->lowest_bit++;
3101da177e4SLinus Torvalds 	if (offset == si->highest_bit)
3111da177e4SLinus Torvalds 		si->highest_bit--;
3127dfad418SHugh Dickins 	si->inuse_pages++;
3137dfad418SHugh Dickins 	if (si->inuse_pages == si->pages) {
3141da177e4SLinus Torvalds 		si->lowest_bit = si->max;
3151da177e4SLinus Torvalds 		si->highest_bit = 0;
3161da177e4SLinus Torvalds 	}
317253d553bSHugh Dickins 	si->swap_map[offset] = usage;
3181da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
31952b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
3207992fde7SHugh Dickins 
3217992fde7SHugh Dickins 	if (si->lowest_alloc) {
3227992fde7SHugh Dickins 		/*
3237992fde7SHugh Dickins 		 * Only set when SWP_DISCARDABLE, and there's a scan
3247992fde7SHugh Dickins 		 * for a free cluster in progress or just completed.
3257992fde7SHugh Dickins 		 */
3267992fde7SHugh Dickins 		if (found_free_cluster) {
3277992fde7SHugh Dickins 			/*
3287992fde7SHugh Dickins 			 * To optimize wear-levelling, discard the
3297992fde7SHugh Dickins 			 * old data of the cluster, taking care not to
3307992fde7SHugh Dickins 			 * discard any of its pages that have already
3317992fde7SHugh Dickins 			 * been allocated by racing tasks (offset has
3327992fde7SHugh Dickins 			 * already stepped over any at the beginning).
3337992fde7SHugh Dickins 			 */
3347992fde7SHugh Dickins 			if (offset < si->highest_alloc &&
3357992fde7SHugh Dickins 			    si->lowest_alloc <= last_in_cluster)
3367992fde7SHugh Dickins 				last_in_cluster = si->lowest_alloc - 1;
3377992fde7SHugh Dickins 			si->flags |= SWP_DISCARDING;
3387992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3397992fde7SHugh Dickins 
3407992fde7SHugh Dickins 			if (offset < last_in_cluster)
3417992fde7SHugh Dickins 				discard_swap_cluster(si, offset,
3427992fde7SHugh Dickins 					last_in_cluster - offset + 1);
3437992fde7SHugh Dickins 
3447992fde7SHugh Dickins 			spin_lock(&swap_lock);
3457992fde7SHugh Dickins 			si->lowest_alloc = 0;
3467992fde7SHugh Dickins 			si->flags &= ~SWP_DISCARDING;
3477992fde7SHugh Dickins 
3487992fde7SHugh Dickins 			smp_mb();	/* wake_up_bit advises this */
3497992fde7SHugh Dickins 			wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
3507992fde7SHugh Dickins 
3517992fde7SHugh Dickins 		} else if (si->flags & SWP_DISCARDING) {
3527992fde7SHugh Dickins 			/*
3537992fde7SHugh Dickins 			 * Delay using pages allocated by racing tasks
3547992fde7SHugh Dickins 			 * until the whole discard has been issued. We
3557992fde7SHugh Dickins 			 * could defer that delay until swap_writepage,
3567992fde7SHugh Dickins 			 * but it's easier to keep this self-contained.
3577992fde7SHugh Dickins 			 */
3587992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3597992fde7SHugh Dickins 			wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
3607992fde7SHugh Dickins 				wait_for_discard, TASK_UNINTERRUPTIBLE);
3617992fde7SHugh Dickins 			spin_lock(&swap_lock);
3627992fde7SHugh Dickins 		} else {
3637992fde7SHugh Dickins 			/*
3647992fde7SHugh Dickins 			 * Note pages allocated by racing tasks while
3657992fde7SHugh Dickins 			 * scan for a free cluster is in progress, so
3667992fde7SHugh Dickins 			 * that its final discard can exclude them.
3677992fde7SHugh Dickins 			 */
3687992fde7SHugh Dickins 			if (offset < si->lowest_alloc)
3697992fde7SHugh Dickins 				si->lowest_alloc = offset;
3707992fde7SHugh Dickins 			if (offset > si->highest_alloc)
3717992fde7SHugh Dickins 				si->highest_alloc = offset;
3727992fde7SHugh Dickins 		}
3737992fde7SHugh Dickins 	}
3741da177e4SLinus Torvalds 	return offset;
3757dfad418SHugh Dickins 
376ebebbbe9SHugh Dickins scan:
3775d337b91SHugh Dickins 	spin_unlock(&swap_lock);
3787dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
37952b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
3805d337b91SHugh Dickins 			spin_lock(&swap_lock);
38152b7efdbSHugh Dickins 			goto checks;
3827dfad418SHugh Dickins 		}
383c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
384c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
385c9e44410SKAMEZAWA Hiroyuki 			goto checks;
386c9e44410SKAMEZAWA Hiroyuki 		}
387048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
388048c27fdSHugh Dickins 			cond_resched();
389048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
390048c27fdSHugh Dickins 		}
39152b7efdbSHugh Dickins 	}
392c60aa176SHugh Dickins 	offset = si->lowest_bit;
393c60aa176SHugh Dickins 	while (++offset < scan_base) {
394c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
3955d337b91SHugh Dickins 			spin_lock(&swap_lock);
396ebebbbe9SHugh Dickins 			goto checks;
397c60aa176SHugh Dickins 		}
398c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
399c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
400c9e44410SKAMEZAWA Hiroyuki 			goto checks;
401c9e44410SKAMEZAWA Hiroyuki 		}
402c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
403c60aa176SHugh Dickins 			cond_resched();
404c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
405c60aa176SHugh Dickins 		}
406c60aa176SHugh Dickins 	}
407c60aa176SHugh Dickins 	spin_lock(&swap_lock);
4087dfad418SHugh Dickins 
4097dfad418SHugh Dickins no_page:
41052b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
4111da177e4SLinus Torvalds 	return 0;
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
4151da177e4SLinus Torvalds {
416fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
417fb4f88dcSHugh Dickins 	pgoff_t offset;
418fb4f88dcSHugh Dickins 	int type, next;
419fb4f88dcSHugh Dickins 	int wrapped = 0;
4201da177e4SLinus Torvalds 
4215d337b91SHugh Dickins 	spin_lock(&swap_lock);
4221da177e4SLinus Torvalds 	if (nr_swap_pages <= 0)
423fb4f88dcSHugh Dickins 		goto noswap;
424fb4f88dcSHugh Dickins 	nr_swap_pages--;
4251da177e4SLinus Torvalds 
426fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
427efa90a98SHugh Dickins 		si = swap_info[type];
428fb4f88dcSHugh Dickins 		next = si->next;
429fb4f88dcSHugh Dickins 		if (next < 0 ||
430efa90a98SHugh Dickins 		    (!wrapped && si->prio != swap_info[next]->prio)) {
431fb4f88dcSHugh Dickins 			next = swap_list.head;
432fb4f88dcSHugh Dickins 			wrapped++;
4331da177e4SLinus Torvalds 		}
434fb4f88dcSHugh Dickins 
435fb4f88dcSHugh Dickins 		if (!si->highest_bit)
436fb4f88dcSHugh Dickins 			continue;
437fb4f88dcSHugh Dickins 		if (!(si->flags & SWP_WRITEOK))
438fb4f88dcSHugh Dickins 			continue;
439fb4f88dcSHugh Dickins 
440fb4f88dcSHugh Dickins 		swap_list.next = next;
441355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry for cache */
442253d553bSHugh Dickins 		offset = scan_swap_map(si, SWAP_HAS_CACHE);
4435d337b91SHugh Dickins 		if (offset) {
4445d337b91SHugh Dickins 			spin_unlock(&swap_lock);
445fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
4465d337b91SHugh Dickins 		}
447fb4f88dcSHugh Dickins 		next = swap_list.next;
448fb4f88dcSHugh Dickins 	}
449fb4f88dcSHugh Dickins 
450fb4f88dcSHugh Dickins 	nr_swap_pages++;
451fb4f88dcSHugh Dickins noswap:
4525d337b91SHugh Dickins 	spin_unlock(&swap_lock);
453fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456910321eaSHugh Dickins /* The only caller of this function is now susupend routine */
457910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type)
458910321eaSHugh Dickins {
459910321eaSHugh Dickins 	struct swap_info_struct *si;
460910321eaSHugh Dickins 	pgoff_t offset;
461910321eaSHugh Dickins 
462910321eaSHugh Dickins 	spin_lock(&swap_lock);
463910321eaSHugh Dickins 	si = swap_info[type];
464910321eaSHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
465910321eaSHugh Dickins 		nr_swap_pages--;
466910321eaSHugh Dickins 		/* This is called for allocating swap entry, not cache */
467910321eaSHugh Dickins 		offset = scan_swap_map(si, 1);
468910321eaSHugh Dickins 		if (offset) {
469910321eaSHugh Dickins 			spin_unlock(&swap_lock);
470910321eaSHugh Dickins 			return swp_entry(type, offset);
471910321eaSHugh Dickins 		}
472910321eaSHugh Dickins 		nr_swap_pages++;
473910321eaSHugh Dickins 	}
474910321eaSHugh Dickins 	spin_unlock(&swap_lock);
475910321eaSHugh Dickins 	return (swp_entry_t) {0};
476910321eaSHugh Dickins }
477910321eaSHugh Dickins 
4781da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	struct swap_info_struct *p;
4811da177e4SLinus Torvalds 	unsigned long offset, type;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (!entry.val)
4841da177e4SLinus Torvalds 		goto out;
4851da177e4SLinus Torvalds 	type = swp_type(entry);
4861da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
4871da177e4SLinus Torvalds 		goto bad_nofile;
488efa90a98SHugh Dickins 	p = swap_info[type];
4891da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
4901da177e4SLinus Torvalds 		goto bad_device;
4911da177e4SLinus Torvalds 	offset = swp_offset(entry);
4921da177e4SLinus Torvalds 	if (offset >= p->max)
4931da177e4SLinus Torvalds 		goto bad_offset;
4941da177e4SLinus Torvalds 	if (!p->swap_map[offset])
4951da177e4SLinus Torvalds 		goto bad_free;
4965d337b91SHugh Dickins 	spin_lock(&swap_lock);
4971da177e4SLinus Torvalds 	return p;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds bad_free:
5001da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
5011da177e4SLinus Torvalds 	goto out;
5021da177e4SLinus Torvalds bad_offset:
5031da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
5041da177e4SLinus Torvalds 	goto out;
5051da177e4SLinus Torvalds bad_device:
5061da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
5071da177e4SLinus Torvalds 	goto out;
5081da177e4SLinus Torvalds bad_nofile:
5091da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
5101da177e4SLinus Torvalds out:
5111da177e4SLinus Torvalds 	return NULL;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5148d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p,
5158d69aaeeSHugh Dickins 				     swp_entry_t entry, unsigned char usage)
5161da177e4SLinus Torvalds {
517253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
5188d69aaeeSHugh Dickins 	unsigned char count;
5198d69aaeeSHugh Dickins 	unsigned char has_cache;
5201da177e4SLinus Torvalds 
521355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
522253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
523253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
524253d553bSHugh Dickins 
525253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
526253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
527253d553bSHugh Dickins 		has_cache = 0;
528aaa46865SHugh Dickins 	} else if (count == SWAP_MAP_SHMEM) {
529aaa46865SHugh Dickins 		/*
530aaa46865SHugh Dickins 		 * Or we could insist on shmem.c using a special
531aaa46865SHugh Dickins 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
532aaa46865SHugh Dickins 		 */
533aaa46865SHugh Dickins 		count = 0;
534570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
535570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
536570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
537570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
538570a335bSHugh Dickins 			else
539570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
540570a335bSHugh Dickins 		} else
541253d553bSHugh Dickins 			count--;
542570a335bSHugh Dickins 	}
543253d553bSHugh Dickins 
544253d553bSHugh Dickins 	if (!count)
545253d553bSHugh Dickins 		mem_cgroup_uncharge_swap(entry);
546253d553bSHugh Dickins 
547253d553bSHugh Dickins 	usage = count | has_cache;
548253d553bSHugh Dickins 	p->swap_map[offset] = usage;
549253d553bSHugh Dickins 
550355cfa73SKAMEZAWA Hiroyuki 	/* free if no reference */
551253d553bSHugh Dickins 	if (!usage) {
5521da177e4SLinus Torvalds 		if (offset < p->lowest_bit)
5531da177e4SLinus Torvalds 			p->lowest_bit = offset;
5541da177e4SLinus Torvalds 		if (offset > p->highest_bit)
5551da177e4SLinus Torvalds 			p->highest_bit = offset;
556efa90a98SHugh Dickins 		if (swap_list.next >= 0 &&
557efa90a98SHugh Dickins 		    p->prio > swap_info[swap_list.next]->prio)
558efa90a98SHugh Dickins 			swap_list.next = p->type;
5591da177e4SLinus Torvalds 		nr_swap_pages++;
5601da177e4SLinus Torvalds 		p->inuse_pages--;
56138b5faf4SDan Magenheimer 		frontswap_invalidate_page(p->type, offset);
562*73744923SMel Gorman 		if (p->flags & SWP_BLKDEV) {
563*73744923SMel Gorman 			struct gendisk *disk = p->bdev->bd_disk;
564*73744923SMel Gorman 			if (disk->fops->swap_slot_free_notify)
565*73744923SMel Gorman 				disk->fops->swap_slot_free_notify(p->bdev,
566*73744923SMel Gorman 								  offset);
567*73744923SMel Gorman 		}
5681da177e4SLinus Torvalds 	}
569253d553bSHugh Dickins 
570253d553bSHugh Dickins 	return usage;
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /*
5741da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
5751da177e4SLinus Torvalds  * is still around or has not been recycled.
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct swap_info_struct *p;
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	p = swap_info_get(entry);
5821da177e4SLinus Torvalds 	if (p) {
583253d553bSHugh Dickins 		swap_entry_free(p, entry, 1);
5845d337b91SHugh Dickins 		spin_unlock(&swap_lock);
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /*
589cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
590cb4b86baSKAMEZAWA Hiroyuki  */
591cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page)
592cb4b86baSKAMEZAWA Hiroyuki {
593355cfa73SKAMEZAWA Hiroyuki 	struct swap_info_struct *p;
5948d69aaeeSHugh Dickins 	unsigned char count;
595355cfa73SKAMEZAWA Hiroyuki 
596355cfa73SKAMEZAWA Hiroyuki 	p = swap_info_get(entry);
597355cfa73SKAMEZAWA Hiroyuki 	if (p) {
598253d553bSHugh Dickins 		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
599253d553bSHugh Dickins 		if (page)
600253d553bSHugh Dickins 			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
601355cfa73SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
602355cfa73SKAMEZAWA Hiroyuki 	}
603cb4b86baSKAMEZAWA Hiroyuki }
604cb4b86baSKAMEZAWA Hiroyuki 
605cb4b86baSKAMEZAWA Hiroyuki /*
606c475a8abSHugh Dickins  * How many references to page are currently swapped out?
607570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
608570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
6091da177e4SLinus Torvalds  */
610bde05d1cSHugh Dickins int page_swapcount(struct page *page)
6111da177e4SLinus Torvalds {
612c475a8abSHugh Dickins 	int count = 0;
6131da177e4SLinus Torvalds 	struct swap_info_struct *p;
6141da177e4SLinus Torvalds 	swp_entry_t entry;
6151da177e4SLinus Torvalds 
6164c21e2f2SHugh Dickins 	entry.val = page_private(page);
6171da177e4SLinus Torvalds 	p = swap_info_get(entry);
6181da177e4SLinus Torvalds 	if (p) {
619355cfa73SKAMEZAWA Hiroyuki 		count = swap_count(p->swap_map[swp_offset(entry)]);
6205d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6211da177e4SLinus Torvalds 	}
622c475a8abSHugh Dickins 	return count;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /*
6267b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
6277b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
6287b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
6297b1fe597SHugh Dickins  * later would only waste time away from clustering.
6301da177e4SLinus Torvalds  */
6317b1fe597SHugh Dickins int reuse_swap_page(struct page *page)
6321da177e4SLinus Torvalds {
633c475a8abSHugh Dickins 	int count;
6341da177e4SLinus Torvalds 
63551726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6365ad64688SHugh Dickins 	if (unlikely(PageKsm(page)))
6375ad64688SHugh Dickins 		return 0;
638c475a8abSHugh Dickins 	count = page_mapcount(page);
6397b1fe597SHugh Dickins 	if (count <= 1 && PageSwapCache(page)) {
640c475a8abSHugh Dickins 		count += page_swapcount(page);
6417b1fe597SHugh Dickins 		if (count == 1 && !PageWriteback(page)) {
6427b1fe597SHugh Dickins 			delete_from_swap_cache(page);
6437b1fe597SHugh Dickins 			SetPageDirty(page);
6447b1fe597SHugh Dickins 		}
6457b1fe597SHugh Dickins 	}
6465ad64688SHugh Dickins 	return count <= 1;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /*
650a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
651a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
6521da177e4SLinus Torvalds  */
653a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
6541da177e4SLinus Torvalds {
65551726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	if (!PageSwapCache(page))
6581da177e4SLinus Torvalds 		return 0;
6591da177e4SLinus Torvalds 	if (PageWriteback(page))
6601da177e4SLinus Torvalds 		return 0;
661a2c43eedSHugh Dickins 	if (page_swapcount(page))
6621da177e4SLinus Torvalds 		return 0;
6631da177e4SLinus Torvalds 
664b73d7fceSHugh Dickins 	/*
665b73d7fceSHugh Dickins 	 * Once hibernation has begun to create its image of memory,
666b73d7fceSHugh Dickins 	 * there's a danger that one of the calls to try_to_free_swap()
667b73d7fceSHugh Dickins 	 * - most probably a call from __try_to_reclaim_swap() while
668b73d7fceSHugh Dickins 	 * hibernation is allocating its own swap pages for the image,
669b73d7fceSHugh Dickins 	 * but conceivably even a call from memory reclaim - will free
670b73d7fceSHugh Dickins 	 * the swap from a page which has already been recorded in the
671b73d7fceSHugh Dickins 	 * image as a clean swapcache page, and then reuse its swap for
672b73d7fceSHugh Dickins 	 * another page of the image.  On waking from hibernation, the
673b73d7fceSHugh Dickins 	 * original page might be freed under memory pressure, then
674b73d7fceSHugh Dickins 	 * later read back in from swap, now with the wrong data.
675b73d7fceSHugh Dickins 	 *
676f90ac398SMel Gorman 	 * Hibration suspends storage while it is writing the image
677f90ac398SMel Gorman 	 * to disk so check that here.
678b73d7fceSHugh Dickins 	 */
679f90ac398SMel Gorman 	if (pm_suspended_storage())
680b73d7fceSHugh Dickins 		return 0;
681b73d7fceSHugh Dickins 
682a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
6831da177e4SLinus Torvalds 	SetPageDirty(page);
684a2c43eedSHugh Dickins 	return 1;
68568a22394SRik van Riel }
68668a22394SRik van Riel 
68768a22394SRik van Riel /*
6881da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
6891da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
6901da177e4SLinus Torvalds  */
6912509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	struct swap_info_struct *p;
6941da177e4SLinus Torvalds 	struct page *page = NULL;
6951da177e4SLinus Torvalds 
696a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
6972509ef26SHugh Dickins 		return 1;
6980697212aSChristoph Lameter 
6991da177e4SLinus Torvalds 	p = swap_info_get(entry);
7001da177e4SLinus Torvalds 	if (p) {
701253d553bSHugh Dickins 		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
70293fac704SNick Piggin 			page = find_get_page(&swapper_space, entry.val);
7038413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
70493fac704SNick Piggin 				page_cache_release(page);
70593fac704SNick Piggin 				page = NULL;
70693fac704SNick Piggin 			}
70793fac704SNick Piggin 		}
7085d337b91SHugh Dickins 		spin_unlock(&swap_lock);
7091da177e4SLinus Torvalds 	}
7101da177e4SLinus Torvalds 	if (page) {
711a2c43eedSHugh Dickins 		/*
712a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
713a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
714a2c43eedSHugh Dickins 		 */
71593fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
716a2c43eedSHugh Dickins 				(!page_mapped(page) || vm_swap_full())) {
7171da177e4SLinus Torvalds 			delete_from_swap_cache(page);
7181da177e4SLinus Torvalds 			SetPageDirty(page);
7191da177e4SLinus Torvalds 		}
7201da177e4SLinus Torvalds 		unlock_page(page);
7211da177e4SLinus Torvalds 		page_cache_release(page);
7221da177e4SLinus Torvalds 	}
7232509ef26SHugh Dickins 	return p != NULL;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
727f577eb30SRafael J. Wysocki /*
728915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
729f577eb30SRafael J. Wysocki  *
730915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
731915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
732915bae9eSRafael J. Wysocki  *
733915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
734f577eb30SRafael J. Wysocki  */
7357bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
736f577eb30SRafael J. Wysocki {
737915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
738efa90a98SHugh Dickins 	int type;
739f577eb30SRafael J. Wysocki 
740915bae9eSRafael J. Wysocki 	if (device)
741915bae9eSRafael J. Wysocki 		bdev = bdget(device);
742915bae9eSRafael J. Wysocki 
743f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
744efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
745efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
746f577eb30SRafael J. Wysocki 
747915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
748f577eb30SRafael J. Wysocki 			continue;
749b6b5bce3SRafael J. Wysocki 
750915bae9eSRafael J. Wysocki 		if (!bdev) {
7517bf23687SRafael J. Wysocki 			if (bdev_p)
752dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
7537bf23687SRafael J. Wysocki 
7546e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
755efa90a98SHugh Dickins 			return type;
7566e1819d6SRafael J. Wysocki 		}
757915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
7589625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
759915bae9eSRafael J. Wysocki 
760915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
7617bf23687SRafael J. Wysocki 				if (bdev_p)
762dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
7637bf23687SRafael J. Wysocki 
764f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
765915bae9eSRafael J. Wysocki 				bdput(bdev);
766efa90a98SHugh Dickins 				return type;
767f577eb30SRafael J. Wysocki 			}
768f577eb30SRafael J. Wysocki 		}
769915bae9eSRafael J. Wysocki 	}
770f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
771915bae9eSRafael J. Wysocki 	if (bdev)
772915bae9eSRafael J. Wysocki 		bdput(bdev);
773915bae9eSRafael J. Wysocki 
774f577eb30SRafael J. Wysocki 	return -ENODEV;
775f577eb30SRafael J. Wysocki }
776f577eb30SRafael J. Wysocki 
777f577eb30SRafael J. Wysocki /*
77873c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
77973c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
78073c34b6aSHugh Dickins  */
78173c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
78273c34b6aSHugh Dickins {
78373c34b6aSHugh Dickins 	struct block_device *bdev;
78473c34b6aSHugh Dickins 
78573c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
78673c34b6aSHugh Dickins 		return 0;
78773c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
78873c34b6aSHugh Dickins 		return 0;
789d4906e1aSLee Schermerhorn 	return map_swap_entry(swp_entry(type, offset), &bdev);
79073c34b6aSHugh Dickins }
79173c34b6aSHugh Dickins 
79273c34b6aSHugh Dickins /*
793f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
794f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
795f577eb30SRafael J. Wysocki  *
796f577eb30SRafael J. Wysocki  * This is needed for software suspend
797f577eb30SRafael J. Wysocki  */
798f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
799f577eb30SRafael J. Wysocki {
800f577eb30SRafael J. Wysocki 	unsigned int n = 0;
801f577eb30SRafael J. Wysocki 
802f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
803efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
804efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
805efa90a98SHugh Dickins 
806efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
807efa90a98SHugh Dickins 			n = sis->pages;
808f577eb30SRafael J. Wysocki 			if (free)
809efa90a98SHugh Dickins 				n -= sis->inuse_pages;
810efa90a98SHugh Dickins 		}
811f577eb30SRafael J. Wysocki 	}
812f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
813f577eb30SRafael J. Wysocki 	return n;
814f577eb30SRafael J. Wysocki }
81573c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
816f577eb30SRafael J. Wysocki 
8171da177e4SLinus Torvalds /*
81872866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
81972866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
82072866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
8211da177e4SLinus Torvalds  */
822044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
8231da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
8241da177e4SLinus Torvalds {
82572835c86SJohannes Weiner 	struct mem_cgroup *memcg;
826044d66c1SHugh Dickins 	spinlock_t *ptl;
827044d66c1SHugh Dickins 	pte_t *pte;
828044d66c1SHugh Dickins 	int ret = 1;
829044d66c1SHugh Dickins 
83072835c86SJohannes Weiner 	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
83172835c86SJohannes Weiner 					 GFP_KERNEL, &memcg)) {
832044d66c1SHugh Dickins 		ret = -ENOMEM;
83385d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
83485d9fc89SKAMEZAWA Hiroyuki 	}
835044d66c1SHugh Dickins 
836044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
837044d66c1SHugh Dickins 	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
838044d66c1SHugh Dickins 		if (ret > 0)
83972835c86SJohannes Weiner 			mem_cgroup_cancel_charge_swapin(memcg);
840044d66c1SHugh Dickins 		ret = 0;
841044d66c1SHugh Dickins 		goto out;
842044d66c1SHugh Dickins 	}
8438a9f3ccdSBalbir Singh 
844b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
845d559db08SKAMEZAWA Hiroyuki 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
8461da177e4SLinus Torvalds 	get_page(page);
8471da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
8481da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
8491da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, addr);
85072835c86SJohannes Weiner 	mem_cgroup_commit_charge_swapin(page, memcg);
8511da177e4SLinus Torvalds 	swap_free(entry);
8521da177e4SLinus Torvalds 	/*
8531da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
8541da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
8551da177e4SLinus Torvalds 	 */
8561da177e4SLinus Torvalds 	activate_page(page);
857044d66c1SHugh Dickins out:
858044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
85985d9fc89SKAMEZAWA Hiroyuki out_nolock:
860044d66c1SHugh Dickins 	return ret;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
8641da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
8651da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
868705e87c0SHugh Dickins 	pte_t *pte;
8698a9f3ccdSBalbir Singh 	int ret = 0;
8701da177e4SLinus Torvalds 
871044d66c1SHugh Dickins 	/*
872044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
873044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
874044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
875044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
876044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
877044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
878044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
879044d66c1SHugh Dickins 	 */
880044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
8811da177e4SLinus Torvalds 	do {
8821da177e4SLinus Torvalds 		/*
8831da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
8841da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
8851da177e4SLinus Torvalds 		 */
8861da177e4SLinus Torvalds 		if (unlikely(pte_same(*pte, swp_pte))) {
887044d66c1SHugh Dickins 			pte_unmap(pte);
888044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
889044d66c1SHugh Dickins 			if (ret)
890044d66c1SHugh Dickins 				goto out;
891044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
8921da177e4SLinus Torvalds 		}
8931da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
894044d66c1SHugh Dickins 	pte_unmap(pte - 1);
895044d66c1SHugh Dickins out:
8968a9f3ccdSBalbir Singh 	return ret;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
9001da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9011da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9021da177e4SLinus Torvalds {
9031da177e4SLinus Torvalds 	pmd_t *pmd;
9041da177e4SLinus Torvalds 	unsigned long next;
9058a9f3ccdSBalbir Singh 	int ret;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
9081da177e4SLinus Torvalds 	do {
9091da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
9101a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
9111da177e4SLinus Torvalds 			continue;
9128a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
9138a9f3ccdSBalbir Singh 		if (ret)
9148a9f3ccdSBalbir Singh 			return ret;
9151da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
9161da177e4SLinus Torvalds 	return 0;
9171da177e4SLinus Torvalds }
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
9201da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9211da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	pud_t *pud;
9241da177e4SLinus Torvalds 	unsigned long next;
9258a9f3ccdSBalbir Singh 	int ret;
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
9281da177e4SLinus Torvalds 	do {
9291da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9301da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
9311da177e4SLinus Torvalds 			continue;
9328a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
9338a9f3ccdSBalbir Singh 		if (ret)
9348a9f3ccdSBalbir Singh 			return ret;
9351da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
9361da177e4SLinus Torvalds 	return 0;
9371da177e4SLinus Torvalds }
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
9401da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9411da177e4SLinus Torvalds {
9421da177e4SLinus Torvalds 	pgd_t *pgd;
9431da177e4SLinus Torvalds 	unsigned long addr, end, next;
9448a9f3ccdSBalbir Singh 	int ret;
9451da177e4SLinus Torvalds 
9463ca7b3c5SHugh Dickins 	if (page_anon_vma(page)) {
9471da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
9481da177e4SLinus Torvalds 		if (addr == -EFAULT)
9491da177e4SLinus Torvalds 			return 0;
9501da177e4SLinus Torvalds 		else
9511da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
9521da177e4SLinus Torvalds 	} else {
9531da177e4SLinus Torvalds 		addr = vma->vm_start;
9541da177e4SLinus Torvalds 		end = vma->vm_end;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
9581da177e4SLinus Torvalds 	do {
9591da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
9601da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
9611da177e4SLinus Torvalds 			continue;
9628a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
9638a9f3ccdSBalbir Singh 		if (ret)
9648a9f3ccdSBalbir Singh 			return ret;
9651da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
9661da177e4SLinus Torvalds 	return 0;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
9701da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9711da177e4SLinus Torvalds {
9721da177e4SLinus Torvalds 	struct vm_area_struct *vma;
9738a9f3ccdSBalbir Singh 	int ret = 0;
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
9761da177e4SLinus Torvalds 		/*
9777d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
9787d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
9791da177e4SLinus Torvalds 		 */
980c475a8abSHugh Dickins 		activate_page(page);
9811da177e4SLinus Torvalds 		unlock_page(page);
9821da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
9831da177e4SLinus Torvalds 		lock_page(page);
9841da177e4SLinus Torvalds 	}
9851da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
9868a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
9871da177e4SLinus Torvalds 			break;
9881da177e4SLinus Torvalds 	}
9891da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
9908a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9931da177e4SLinus Torvalds /*
99438b5faf4SDan Magenheimer  * Scan swap_map (or frontswap_map if frontswap parameter is true)
99538b5faf4SDan Magenheimer  * from current position to next entry still in use.
9961da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
9971da177e4SLinus Torvalds  */
9986eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
99938b5faf4SDan Magenheimer 					unsigned int prev, bool frontswap)
10001da177e4SLinus Torvalds {
10016eb396dcSHugh Dickins 	unsigned int max = si->max;
10026eb396dcSHugh Dickins 	unsigned int i = prev;
10038d69aaeeSHugh Dickins 	unsigned char count;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/*
10065d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
10071da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
10081da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
10095d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
10101da177e4SLinus Torvalds 	 */
10111da177e4SLinus Torvalds 	for (;;) {
10121da177e4SLinus Torvalds 		if (++i >= max) {
10131da177e4SLinus Torvalds 			if (!prev) {
10141da177e4SLinus Torvalds 				i = 0;
10151da177e4SLinus Torvalds 				break;
10161da177e4SLinus Torvalds 			}
10171da177e4SLinus Torvalds 			/*
10181da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
10191da177e4SLinus Torvalds 			 * loop back to start and recheck there.
10201da177e4SLinus Torvalds 			 */
10211da177e4SLinus Torvalds 			max = prev + 1;
10221da177e4SLinus Torvalds 			prev = 0;
10231da177e4SLinus Torvalds 			i = 1;
10241da177e4SLinus Torvalds 		}
102538b5faf4SDan Magenheimer 		if (frontswap) {
102638b5faf4SDan Magenheimer 			if (frontswap_test(si, i))
102738b5faf4SDan Magenheimer 				break;
102838b5faf4SDan Magenheimer 			else
102938b5faf4SDan Magenheimer 				continue;
103038b5faf4SDan Magenheimer 		}
10311da177e4SLinus Torvalds 		count = si->swap_map[i];
1032355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
10331da177e4SLinus Torvalds 			break;
10341da177e4SLinus Torvalds 	}
10351da177e4SLinus Torvalds 	return i;
10361da177e4SLinus Torvalds }
10371da177e4SLinus Torvalds 
10381da177e4SLinus Torvalds /*
10391da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
10401da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
10411da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
104238b5faf4SDan Magenheimer  *
104338b5faf4SDan Magenheimer  * if the boolean frontswap is true, only unuse pages_to_unuse pages;
104438b5faf4SDan Magenheimer  * pages_to_unuse==0 means all pages; ignored if frontswap is false
10451da177e4SLinus Torvalds  */
104638b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap,
104738b5faf4SDan Magenheimer 		 unsigned long pages_to_unuse)
10481da177e4SLinus Torvalds {
1049efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
10501da177e4SLinus Torvalds 	struct mm_struct *start_mm;
10518d69aaeeSHugh Dickins 	unsigned char *swap_map;
10528d69aaeeSHugh Dickins 	unsigned char swcount;
10531da177e4SLinus Torvalds 	struct page *page;
10541da177e4SLinus Torvalds 	swp_entry_t entry;
10556eb396dcSHugh Dickins 	unsigned int i = 0;
10561da177e4SLinus Torvalds 	int retval = 0;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	/*
10591da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
10601da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
10611da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
10621da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
10631da177e4SLinus Torvalds 	 *
10641da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
10651da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
10661da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
10671da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
10681da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
10691da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
1070570a335bSHugh Dickins 	 * that.
10711da177e4SLinus Torvalds 	 */
10721da177e4SLinus Torvalds 	start_mm = &init_mm;
10731da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	/*
10761da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
10771da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
10781da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
10791da177e4SLinus Torvalds 	 */
108038b5faf4SDan Magenheimer 	while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
10811da177e4SLinus Torvalds 		if (signal_pending(current)) {
10821da177e4SLinus Torvalds 			retval = -EINTR;
10831da177e4SLinus Torvalds 			break;
10841da177e4SLinus Torvalds 		}
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 		/*
10871da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
10881da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
10891da177e4SLinus Torvalds 		 * page and read the swap into it.
10901da177e4SLinus Torvalds 		 */
10911da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
10921da177e4SLinus Torvalds 		entry = swp_entry(type, i);
109302098feaSHugh Dickins 		page = read_swap_cache_async(entry,
109402098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
10951da177e4SLinus Torvalds 		if (!page) {
10961da177e4SLinus Torvalds 			/*
10971da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
10981da177e4SLinus Torvalds 			 * has been freed independently, and will not be
10991da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
11001da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
11011da177e4SLinus Torvalds 			 */
11021da177e4SLinus Torvalds 			if (!*swap_map)
11031da177e4SLinus Torvalds 				continue;
11041da177e4SLinus Torvalds 			retval = -ENOMEM;
11051da177e4SLinus Torvalds 			break;
11061da177e4SLinus Torvalds 		}
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 		/*
11091da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
11101da177e4SLinus Torvalds 		 */
11111da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
11121da177e4SLinus Torvalds 			mmput(start_mm);
11131da177e4SLinus Torvalds 			start_mm = &init_mm;
11141da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
11151da177e4SLinus Torvalds 		}
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 		/*
11181da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
11191da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
11201da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
11211da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
11221da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
11231da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
11241da177e4SLinus Torvalds 		 */
11251da177e4SLinus Torvalds 		wait_on_page_locked(page);
11261da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11271da177e4SLinus Torvalds 		lock_page(page);
11281da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds 		/*
11311da177e4SLinus Torvalds 		 * Remove all references to entry.
11321da177e4SLinus Torvalds 		 */
11331da177e4SLinus Torvalds 		swcount = *swap_map;
1134aaa46865SHugh Dickins 		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1135aaa46865SHugh Dickins 			retval = shmem_unuse(entry, page);
1136aaa46865SHugh Dickins 			/* page has already been unlocked and released */
1137aaa46865SHugh Dickins 			if (retval < 0)
1138aaa46865SHugh Dickins 				break;
1139aaa46865SHugh Dickins 			continue;
11401da177e4SLinus Torvalds 		}
1141aaa46865SHugh Dickins 		if (swap_count(swcount) && start_mm != &init_mm)
1142aaa46865SHugh Dickins 			retval = unuse_mm(start_mm, entry, page);
1143aaa46865SHugh Dickins 
1144355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
11451da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
11461da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
11471da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
11481da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
11491da177e4SLinus Torvalds 			struct mm_struct *mm;
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
11521da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
11531da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
1154aaa46865SHugh Dickins 			while (swap_count(*swap_map) && !retval &&
11551da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
11561da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
115770af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
11581da177e4SLinus Torvalds 					continue;
11591da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
11601da177e4SLinus Torvalds 				mmput(prev_mm);
11611da177e4SLinus Torvalds 				prev_mm = mm;
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 				cond_resched();
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 				swcount = *swap_map;
1166355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
11671da177e4SLinus Torvalds 					;
1168aaa46865SHugh Dickins 				else if (mm == &init_mm)
11691da177e4SLinus Torvalds 					set_start_mm = 1;
1170aaa46865SHugh Dickins 				else
11711da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
1172355cfa73SKAMEZAWA Hiroyuki 
117332c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
11741da177e4SLinus Torvalds 					mmput(new_start_mm);
11751da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
11761da177e4SLinus Torvalds 					new_start_mm = mm;
11771da177e4SLinus Torvalds 					set_start_mm = 0;
11781da177e4SLinus Torvalds 				}
11791da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
11801da177e4SLinus Torvalds 			}
11811da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
11821da177e4SLinus Torvalds 			mmput(prev_mm);
11831da177e4SLinus Torvalds 			mmput(start_mm);
11841da177e4SLinus Torvalds 			start_mm = new_start_mm;
11851da177e4SLinus Torvalds 		}
11861da177e4SLinus Torvalds 		if (retval) {
11871da177e4SLinus Torvalds 			unlock_page(page);
11881da177e4SLinus Torvalds 			page_cache_release(page);
11891da177e4SLinus Torvalds 			break;
11901da177e4SLinus Torvalds 		}
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 		/*
11931da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
11941da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
11951da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
11961da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
11971da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
11981da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
11991da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
12001da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
12011da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
12021da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
12031da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
12045ad64688SHugh Dickins 		 *
12055ad64688SHugh Dickins 		 * Given how unuse_vma() targets one particular offset
12065ad64688SHugh Dickins 		 * in an anon_vma, once the anon_vma has been determined,
12075ad64688SHugh Dickins 		 * this splitting happens to be just what is needed to
12085ad64688SHugh Dickins 		 * handle where KSM pages have been swapped out: re-reading
12095ad64688SHugh Dickins 		 * is unnecessarily slow, but we can fix that later on.
12101da177e4SLinus Torvalds 		 */
1211355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
1212355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
12131da177e4SLinus Torvalds 			struct writeback_control wbc = {
12141da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
12151da177e4SLinus Torvalds 			};
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
12181da177e4SLinus Torvalds 			lock_page(page);
12191da177e4SLinus Torvalds 			wait_on_page_writeback(page);
12201da177e4SLinus Torvalds 		}
122168bdc8d6SHugh Dickins 
122268bdc8d6SHugh Dickins 		/*
122368bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
122468bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
122568bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
122668bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
122768bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
122868bdc8d6SHugh Dickins 		 */
122968bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
123068bdc8d6SHugh Dickins 		    likely(page_private(page) == entry.val))
12311da177e4SLinus Torvalds 			delete_from_swap_cache(page);
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 		/*
12341da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
12351da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
12362706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
12371da177e4SLinus Torvalds 		 */
12381da177e4SLinus Torvalds 		SetPageDirty(page);
12391da177e4SLinus Torvalds 		unlock_page(page);
12401da177e4SLinus Torvalds 		page_cache_release(page);
12411da177e4SLinus Torvalds 
12421da177e4SLinus Torvalds 		/*
12431da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
12441da177e4SLinus Torvalds 		 * interactive performance.
12451da177e4SLinus Torvalds 		 */
12461da177e4SLinus Torvalds 		cond_resched();
124738b5faf4SDan Magenheimer 		if (frontswap && pages_to_unuse > 0) {
124838b5faf4SDan Magenheimer 			if (!--pages_to_unuse)
124938b5faf4SDan Magenheimer 				break;
125038b5faf4SDan Magenheimer 		}
12511da177e4SLinus Torvalds 	}
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	mmput(start_mm);
12541da177e4SLinus Torvalds 	return retval;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds /*
12585d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
12595d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
12605d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
12611da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
12621da177e4SLinus Torvalds  */
12631da177e4SLinus Torvalds static void drain_mmlist(void)
12641da177e4SLinus Torvalds {
12651da177e4SLinus Torvalds 	struct list_head *p, *next;
1266efa90a98SHugh Dickins 	unsigned int type;
12671da177e4SLinus Torvalds 
1268efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
1269efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
12701da177e4SLinus Torvalds 			return;
12711da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
12721da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
12731da177e4SLinus Torvalds 		list_del_init(p);
12741da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12771da177e4SLinus Torvalds /*
12781da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1279d4906e1aSLee Schermerhorn  * corresponds to page offset for the specified swap entry.
1280d4906e1aSLee Schermerhorn  * Note that the type of this function is sector_t, but it returns page offset
1281d4906e1aSLee Schermerhorn  * into the bdev, not sector offset.
12821da177e4SLinus Torvalds  */
1283d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
12841da177e4SLinus Torvalds {
1285f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
1286f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
1287f29ad6a9SHugh Dickins 	struct swap_extent *se;
1288f29ad6a9SHugh Dickins 	pgoff_t offset;
1289f29ad6a9SHugh Dickins 
1290efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
1291f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
1292f29ad6a9SHugh Dickins 
1293f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
1294f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
1295f29ad6a9SHugh Dickins 	se = start_se;
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	for ( ; ; ) {
12981da177e4SLinus Torvalds 		struct list_head *lh;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds 		if (se->start_page <= offset &&
13011da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
13021da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
13031da177e4SLinus Torvalds 		}
130411d31886SHugh Dickins 		lh = se->list.next;
13051da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
13061da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
13071da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
13081da177e4SLinus Torvalds 	}
13091da177e4SLinus Torvalds }
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds /*
1312d4906e1aSLee Schermerhorn  * Returns the page offset into bdev for the specified page's swap entry.
1313d4906e1aSLee Schermerhorn  */
1314d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev)
1315d4906e1aSLee Schermerhorn {
1316d4906e1aSLee Schermerhorn 	swp_entry_t entry;
1317d4906e1aSLee Schermerhorn 	entry.val = page_private(page);
1318d4906e1aSLee Schermerhorn 	return map_swap_entry(entry, bdev);
1319d4906e1aSLee Schermerhorn }
1320d4906e1aSLee Schermerhorn 
1321d4906e1aSLee Schermerhorn /*
13221da177e4SLinus Torvalds  * Free all of a swapdev's extent information
13231da177e4SLinus Torvalds  */
13241da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
13251da177e4SLinus Torvalds {
13269625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
13271da177e4SLinus Torvalds 		struct swap_extent *se;
13281da177e4SLinus Torvalds 
13299625a5f2SHugh Dickins 		se = list_entry(sis->first_swap_extent.list.next,
13301da177e4SLinus Torvalds 				struct swap_extent, list);
13311da177e4SLinus Torvalds 		list_del(&se->list);
13321da177e4SLinus Torvalds 		kfree(se);
13331da177e4SLinus Torvalds 	}
133462c230bcSMel Gorman 
133562c230bcSMel Gorman 	if (sis->flags & SWP_FILE) {
133662c230bcSMel Gorman 		struct file *swap_file = sis->swap_file;
133762c230bcSMel Gorman 		struct address_space *mapping = swap_file->f_mapping;
133862c230bcSMel Gorman 
133962c230bcSMel Gorman 		sis->flags &= ~SWP_FILE;
134062c230bcSMel Gorman 		mapping->a_ops->swap_deactivate(swap_file);
134162c230bcSMel Gorman 	}
13421da177e4SLinus Torvalds }
13431da177e4SLinus Torvalds 
13441da177e4SLinus Torvalds /*
13451da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
134611d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
13471da177e4SLinus Torvalds  *
134811d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
13491da177e4SLinus Torvalds  */
1350a509bc1aSMel Gorman int
13511da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
13521da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
13531da177e4SLinus Torvalds {
13541da177e4SLinus Torvalds 	struct swap_extent *se;
13551da177e4SLinus Torvalds 	struct swap_extent *new_se;
13561da177e4SLinus Torvalds 	struct list_head *lh;
13571da177e4SLinus Torvalds 
13589625a5f2SHugh Dickins 	if (start_page == 0) {
13599625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
13609625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
13619625a5f2SHugh Dickins 		se->start_page = 0;
13629625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
13639625a5f2SHugh Dickins 		se->start_block = start_block;
13649625a5f2SHugh Dickins 		return 1;
13659625a5f2SHugh Dickins 	} else {
13669625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
13671da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
136811d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
136911d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
13701da177e4SLinus Torvalds 			/* Merge it */
13711da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
13721da177e4SLinus Torvalds 			return 0;
13731da177e4SLinus Torvalds 		}
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/*
13771da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
13781da177e4SLinus Torvalds 	 */
13791da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
13801da177e4SLinus Torvalds 	if (new_se == NULL)
13811da177e4SLinus Torvalds 		return -ENOMEM;
13821da177e4SLinus Torvalds 	new_se->start_page = start_page;
13831da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
13841da177e4SLinus Torvalds 	new_se->start_block = start_block;
13851da177e4SLinus Torvalds 
13869625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
138753092a74SHugh Dickins 	return 1;
13881da177e4SLinus Torvalds }
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds /*
13911da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
13921da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
13931da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
13941da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
13951da177e4SLinus Torvalds  *
13961da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
13971da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
13981da177e4SLinus Torvalds  * swap files identically.
13991da177e4SLinus Torvalds  *
14001da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
14011da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
14021da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
14031da177e4SLinus Torvalds  *
14041da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
14051da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
14061da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
14071da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
14081da177e4SLinus Torvalds  * for swapping.
14091da177e4SLinus Torvalds  *
1410b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
14111da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
14121da177e4SLinus Torvalds  * which will scribble on the fs.
14131da177e4SLinus Torvalds  *
14141da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
14151da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
14161da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
14171da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
14181da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
14191da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
14201da177e4SLinus Torvalds  */
142153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
14221da177e4SLinus Torvalds {
142362c230bcSMel Gorman 	struct file *swap_file = sis->swap_file;
142462c230bcSMel Gorman 	struct address_space *mapping = swap_file->f_mapping;
142562c230bcSMel Gorman 	struct inode *inode = mapping->host;
14261da177e4SLinus Torvalds 	int ret;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
14291da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
143053092a74SHugh Dickins 		*span = sis->pages;
1431a509bc1aSMel Gorman 		return ret;
14321da177e4SLinus Torvalds 	}
14331da177e4SLinus Torvalds 
143462c230bcSMel Gorman 	if (mapping->a_ops->swap_activate) {
1435a509bc1aSMel Gorman 		ret = mapping->a_ops->swap_activate(sis, swap_file, span);
143662c230bcSMel Gorman 		if (!ret) {
143762c230bcSMel Gorman 			sis->flags |= SWP_FILE;
143862c230bcSMel Gorman 			ret = add_swap_extent(sis, 0, sis->max, 0);
143962c230bcSMel Gorman 			*span = sis->pages;
144062c230bcSMel Gorman 		}
14419625a5f2SHugh Dickins 		return ret;
1442a509bc1aSMel Gorman 	}
1443a509bc1aSMel Gorman 
1444a509bc1aSMel Gorman 	return generic_swapfile_activate(sis, swap_file, span);
14451da177e4SLinus Torvalds }
14461da177e4SLinus Torvalds 
144740531542SCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio,
144838b5faf4SDan Magenheimer 				unsigned char *swap_map,
144938b5faf4SDan Magenheimer 				unsigned long *frontswap_map)
145040531542SCesar Eduardo Barros {
145140531542SCesar Eduardo Barros 	int i, prev;
145240531542SCesar Eduardo Barros 
145340531542SCesar Eduardo Barros 	spin_lock(&swap_lock);
145440531542SCesar Eduardo Barros 	if (prio >= 0)
145540531542SCesar Eduardo Barros 		p->prio = prio;
145640531542SCesar Eduardo Barros 	else
145740531542SCesar Eduardo Barros 		p->prio = --least_priority;
145840531542SCesar Eduardo Barros 	p->swap_map = swap_map;
145938b5faf4SDan Magenheimer 	frontswap_map_set(p, frontswap_map);
146040531542SCesar Eduardo Barros 	p->flags |= SWP_WRITEOK;
146140531542SCesar Eduardo Barros 	nr_swap_pages += p->pages;
146240531542SCesar Eduardo Barros 	total_swap_pages += p->pages;
146340531542SCesar Eduardo Barros 
146440531542SCesar Eduardo Barros 	/* insert swap space into swap_list: */
146540531542SCesar Eduardo Barros 	prev = -1;
146640531542SCesar Eduardo Barros 	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
146740531542SCesar Eduardo Barros 		if (p->prio >= swap_info[i]->prio)
146840531542SCesar Eduardo Barros 			break;
146940531542SCesar Eduardo Barros 		prev = i;
147040531542SCesar Eduardo Barros 	}
147140531542SCesar Eduardo Barros 	p->next = i;
147240531542SCesar Eduardo Barros 	if (prev < 0)
147340531542SCesar Eduardo Barros 		swap_list.head = swap_list.next = p->type;
147440531542SCesar Eduardo Barros 	else
147540531542SCesar Eduardo Barros 		swap_info[prev]->next = p->type;
147638b5faf4SDan Magenheimer 	frontswap_init(p->type);
147740531542SCesar Eduardo Barros 	spin_unlock(&swap_lock);
147840531542SCesar Eduardo Barros }
147940531542SCesar Eduardo Barros 
1480c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
14811da177e4SLinus Torvalds {
14821da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
14838d69aaeeSHugh Dickins 	unsigned char *swap_map;
14841da177e4SLinus Torvalds 	struct file *swap_file, *victim;
14851da177e4SLinus Torvalds 	struct address_space *mapping;
14861da177e4SLinus Torvalds 	struct inode *inode;
14871da177e4SLinus Torvalds 	char *pathname;
148872788c38SDavid Rientjes 	int oom_score_adj;
14891da177e4SLinus Torvalds 	int i, type, prev;
14901da177e4SLinus Torvalds 	int err;
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
14931da177e4SLinus Torvalds 		return -EPERM;
14941da177e4SLinus Torvalds 
1495191c5424SAl Viro 	BUG_ON(!current->mm);
1496191c5424SAl Viro 
14971da177e4SLinus Torvalds 	pathname = getname(specialfile);
14981da177e4SLinus Torvalds 	err = PTR_ERR(pathname);
14991da177e4SLinus Torvalds 	if (IS_ERR(pathname))
15001da177e4SLinus Torvalds 		goto out;
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
15031da177e4SLinus Torvalds 	putname(pathname);
15041da177e4SLinus Torvalds 	err = PTR_ERR(victim);
15051da177e4SLinus Torvalds 	if (IS_ERR(victim))
15061da177e4SLinus Torvalds 		goto out;
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds 	mapping = victim->f_mapping;
15091da177e4SLinus Torvalds 	prev = -1;
15105d337b91SHugh Dickins 	spin_lock(&swap_lock);
1511efa90a98SHugh Dickins 	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1512efa90a98SHugh Dickins 		p = swap_info[type];
151322c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
15141da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
15151da177e4SLinus Torvalds 				break;
15161da177e4SLinus Torvalds 		}
15171da177e4SLinus Torvalds 		prev = type;
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 	if (type < 0) {
15201da177e4SLinus Torvalds 		err = -EINVAL;
15215d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15221da177e4SLinus Torvalds 		goto out_dput;
15231da177e4SLinus Torvalds 	}
1524191c5424SAl Viro 	if (!security_vm_enough_memory_mm(current->mm, p->pages))
15251da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
15261da177e4SLinus Torvalds 	else {
15271da177e4SLinus Torvalds 		err = -ENOMEM;
15285d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15291da177e4SLinus Torvalds 		goto out_dput;
15301da177e4SLinus Torvalds 	}
1531efa90a98SHugh Dickins 	if (prev < 0)
15321da177e4SLinus Torvalds 		swap_list.head = p->next;
1533efa90a98SHugh Dickins 	else
1534efa90a98SHugh Dickins 		swap_info[prev]->next = p->next;
15351da177e4SLinus Torvalds 	if (type == swap_list.next) {
15361da177e4SLinus Torvalds 		/* just pick something that's safe... */
15371da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
15381da177e4SLinus Torvalds 	}
153978ecba08SHugh Dickins 	if (p->prio < 0) {
1540efa90a98SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i]->next)
1541efa90a98SHugh Dickins 			swap_info[i]->prio = p->prio--;
154278ecba08SHugh Dickins 		least_priority++;
154378ecba08SHugh Dickins 	}
15441da177e4SLinus Torvalds 	nr_swap_pages -= p->pages;
15451da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
15461da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
15475d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1548fb4f88dcSHugh Dickins 
154972788c38SDavid Rientjes 	oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
155038b5faf4SDan Magenheimer 	err = try_to_unuse(type, false, 0); /* force all pages to be unused */
155143362a49SDavid Rientjes 	compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj);
15521da177e4SLinus Torvalds 
15531da177e4SLinus Torvalds 	if (err) {
155440531542SCesar Eduardo Barros 		/*
155540531542SCesar Eduardo Barros 		 * reading p->prio and p->swap_map outside the lock is
155640531542SCesar Eduardo Barros 		 * safe here because only sys_swapon and sys_swapoff
155740531542SCesar Eduardo Barros 		 * change them, and there can be no other sys_swapon or
155840531542SCesar Eduardo Barros 		 * sys_swapoff for this swap_info_struct at this point.
155940531542SCesar Eduardo Barros 		 */
15601da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
156138b5faf4SDan Magenheimer 		enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p));
15621da177e4SLinus Torvalds 		goto out_dput;
15631da177e4SLinus Torvalds 	}
156452b7efdbSHugh Dickins 
15654cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1566570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
1567570a335bSHugh Dickins 		free_swap_count_continuations(p);
1568570a335bSHugh Dickins 
1569fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
15705d337b91SHugh Dickins 	spin_lock(&swap_lock);
15711da177e4SLinus Torvalds 	drain_mmlist();
15725d337b91SHugh Dickins 
15735d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
15745d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
15755d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
15765d337b91SHugh Dickins 		spin_unlock(&swap_lock);
157713e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
15785d337b91SHugh Dickins 		spin_lock(&swap_lock);
15795d337b91SHugh Dickins 	}
15805d337b91SHugh Dickins 
15811da177e4SLinus Torvalds 	swap_file = p->swap_file;
15821da177e4SLinus Torvalds 	p->swap_file = NULL;
15831da177e4SLinus Torvalds 	p->max = 0;
15841da177e4SLinus Torvalds 	swap_map = p->swap_map;
15851da177e4SLinus Torvalds 	p->swap_map = NULL;
15861da177e4SLinus Torvalds 	p->flags = 0;
158738b5faf4SDan Magenheimer 	frontswap_invalidate_area(type);
15885d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1589fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
15901da177e4SLinus Torvalds 	vfree(swap_map);
159138b5faf4SDan Magenheimer 	vfree(frontswap_map_get(p));
159227a7faa0SKAMEZAWA Hiroyuki 	/* Destroy swap account informatin */
159327a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
159427a7faa0SKAMEZAWA Hiroyuki 
15951da177e4SLinus Torvalds 	inode = mapping->host;
15961da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
15971da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
15981da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
1599e525fd89STejun Heo 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
16001da177e4SLinus Torvalds 	} else {
16011b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
16021da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
16031b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
16041da177e4SLinus Torvalds 	}
16051da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
16061da177e4SLinus Torvalds 	err = 0;
160766d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
160866d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds out_dput:
16111da177e4SLinus Torvalds 	filp_close(victim, NULL);
16121da177e4SLinus Torvalds out:
16131da177e4SLinus Torvalds 	return err;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
16161da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
161766d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait)
161866d7dd51SKay Sievers {
1619f1514638SKay Sievers 	struct seq_file *seq = file->private_data;
162066d7dd51SKay Sievers 
162166d7dd51SKay Sievers 	poll_wait(file, &proc_poll_wait, wait);
162266d7dd51SKay Sievers 
1623f1514638SKay Sievers 	if (seq->poll_event != atomic_read(&proc_poll_event)) {
1624f1514638SKay Sievers 		seq->poll_event = atomic_read(&proc_poll_event);
162566d7dd51SKay Sievers 		return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
162666d7dd51SKay Sievers 	}
162766d7dd51SKay Sievers 
162866d7dd51SKay Sievers 	return POLLIN | POLLRDNORM;
162966d7dd51SKay Sievers }
163066d7dd51SKay Sievers 
16311da177e4SLinus Torvalds /* iterator */
16321da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
16331da177e4SLinus Torvalds {
1634efa90a98SHugh Dickins 	struct swap_info_struct *si;
1635efa90a98SHugh Dickins 	int type;
16361da177e4SLinus Torvalds 	loff_t l = *pos;
16371da177e4SLinus Torvalds 
1638fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16391da177e4SLinus Torvalds 
1640881e4aabSSuleiman Souhlal 	if (!l)
1641881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1642881e4aabSSuleiman Souhlal 
1643efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1644efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1645efa90a98SHugh Dickins 		si = swap_info[type];
1646efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
16471da177e4SLinus Torvalds 			continue;
1648881e4aabSSuleiman Souhlal 		if (!--l)
1649efa90a98SHugh Dickins 			return si;
16501da177e4SLinus Torvalds 	}
16511da177e4SLinus Torvalds 
16521da177e4SLinus Torvalds 	return NULL;
16531da177e4SLinus Torvalds }
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
16561da177e4SLinus Torvalds {
1657efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
1658efa90a98SHugh Dickins 	int type;
16591da177e4SLinus Torvalds 
1660881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1661efa90a98SHugh Dickins 		type = 0;
1662efa90a98SHugh Dickins 	else
1663efa90a98SHugh Dickins 		type = si->type + 1;
1664881e4aabSSuleiman Souhlal 
1665efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
1666efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1667efa90a98SHugh Dickins 		si = swap_info[type];
1668efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
16691da177e4SLinus Torvalds 			continue;
16701da177e4SLinus Torvalds 		++*pos;
1671efa90a98SHugh Dickins 		return si;
16721da177e4SLinus Torvalds 	}
16731da177e4SLinus Torvalds 
16741da177e4SLinus Torvalds 	return NULL;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
16781da177e4SLinus Torvalds {
1679fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16821da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
16831da177e4SLinus Torvalds {
1684efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
16851da177e4SLinus Torvalds 	struct file *file;
16861da177e4SLinus Torvalds 	int len;
16871da177e4SLinus Torvalds 
1688efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
16891da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1690881e4aabSSuleiman Souhlal 		return 0;
1691881e4aabSSuleiman Souhlal 	}
16921da177e4SLinus Torvalds 
1693efa90a98SHugh Dickins 	file = si->swap_file;
1694c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
16956eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
16961da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
1697d3ac7f89SJosef "Jeff" Sipek 			S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
16981da177e4SLinus Torvalds 				"partition" : "file\t",
1699efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
1700efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
1701efa90a98SHugh Dickins 			si->prio);
17021da177e4SLinus Torvalds 	return 0;
17031da177e4SLinus Torvalds }
17041da177e4SLinus Torvalds 
170515ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
17061da177e4SLinus Torvalds 	.start =	swap_start,
17071da177e4SLinus Torvalds 	.next =		swap_next,
17081da177e4SLinus Torvalds 	.stop =		swap_stop,
17091da177e4SLinus Torvalds 	.show =		swap_show
17101da177e4SLinus Torvalds };
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
17131da177e4SLinus Torvalds {
1714f1514638SKay Sievers 	struct seq_file *seq;
171566d7dd51SKay Sievers 	int ret;
171666d7dd51SKay Sievers 
171766d7dd51SKay Sievers 	ret = seq_open(file, &swaps_op);
1718f1514638SKay Sievers 	if (ret)
171966d7dd51SKay Sievers 		return ret;
172066d7dd51SKay Sievers 
1721f1514638SKay Sievers 	seq = file->private_data;
1722f1514638SKay Sievers 	seq->poll_event = atomic_read(&proc_poll_event);
1723f1514638SKay Sievers 	return 0;
17241da177e4SLinus Torvalds }
17251da177e4SLinus Torvalds 
172615ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
17271da177e4SLinus Torvalds 	.open		= swaps_open,
17281da177e4SLinus Torvalds 	.read		= seq_read,
17291da177e4SLinus Torvalds 	.llseek		= seq_lseek,
17301da177e4SLinus Torvalds 	.release	= seq_release,
173166d7dd51SKay Sievers 	.poll		= swaps_poll,
17321da177e4SLinus Torvalds };
17331da177e4SLinus Torvalds 
17341da177e4SLinus Torvalds static int __init procswaps_init(void)
17351da177e4SLinus Torvalds {
17363d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
17371da177e4SLinus Torvalds 	return 0;
17381da177e4SLinus Torvalds }
17391da177e4SLinus Torvalds __initcall(procswaps_init);
17401da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
17411da177e4SLinus Torvalds 
17421796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
17431796316aSJan Beulich static int __init max_swapfiles_check(void)
17441796316aSJan Beulich {
17451796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
17461796316aSJan Beulich 	return 0;
17471796316aSJan Beulich }
17481796316aSJan Beulich late_initcall(max_swapfiles_check);
17491796316aSJan Beulich #endif
17501796316aSJan Beulich 
175153cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void)
17521da177e4SLinus Torvalds {
17531da177e4SLinus Torvalds 	struct swap_info_struct *p;
17541da177e4SLinus Torvalds 	unsigned int type;
1755efa90a98SHugh Dickins 
1756efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1757efa90a98SHugh Dickins 	if (!p)
175853cbb243SCesar Eduardo Barros 		return ERR_PTR(-ENOMEM);
1759efa90a98SHugh Dickins 
17605d337b91SHugh Dickins 	spin_lock(&swap_lock);
1761efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1762efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
17631da177e4SLinus Torvalds 			break;
1764efa90a98SHugh Dickins 	}
17650697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
17665d337b91SHugh Dickins 		spin_unlock(&swap_lock);
1767efa90a98SHugh Dickins 		kfree(p);
1768730c0581SCesar Eduardo Barros 		return ERR_PTR(-EPERM);
17691da177e4SLinus Torvalds 	}
1770efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
1771efa90a98SHugh Dickins 		p->type = type;
1772efa90a98SHugh Dickins 		swap_info[type] = p;
1773efa90a98SHugh Dickins 		/*
1774efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
1775efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
1776efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
1777efa90a98SHugh Dickins 		 */
1778efa90a98SHugh Dickins 		smp_wmb();
1779efa90a98SHugh Dickins 		nr_swapfiles++;
1780efa90a98SHugh Dickins 	} else {
1781efa90a98SHugh Dickins 		kfree(p);
1782efa90a98SHugh Dickins 		p = swap_info[type];
1783efa90a98SHugh Dickins 		/*
1784efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
1785efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
1786efa90a98SHugh Dickins 		 */
1787efa90a98SHugh Dickins 	}
17889625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
17891da177e4SLinus Torvalds 	p->flags = SWP_USED;
17901da177e4SLinus Torvalds 	p->next = -1;
17915d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1792efa90a98SHugh Dickins 
179353cbb243SCesar Eduardo Barros 	return p;
179453cbb243SCesar Eduardo Barros }
179553cbb243SCesar Eduardo Barros 
17964d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
17974d0e1e10SCesar Eduardo Barros {
17984d0e1e10SCesar Eduardo Barros 	int error;
17994d0e1e10SCesar Eduardo Barros 
18004d0e1e10SCesar Eduardo Barros 	if (S_ISBLK(inode->i_mode)) {
18014d0e1e10SCesar Eduardo Barros 		p->bdev = bdgrab(I_BDEV(inode));
18024d0e1e10SCesar Eduardo Barros 		error = blkdev_get(p->bdev,
18034d0e1e10SCesar Eduardo Barros 				   FMODE_READ | FMODE_WRITE | FMODE_EXCL,
18044d0e1e10SCesar Eduardo Barros 				   sys_swapon);
18054d0e1e10SCesar Eduardo Barros 		if (error < 0) {
18064d0e1e10SCesar Eduardo Barros 			p->bdev = NULL;
180787ade72aSCesar Eduardo Barros 			return -EINVAL;
18084d0e1e10SCesar Eduardo Barros 		}
18094d0e1e10SCesar Eduardo Barros 		p->old_block_size = block_size(p->bdev);
18104d0e1e10SCesar Eduardo Barros 		error = set_blocksize(p->bdev, PAGE_SIZE);
18114d0e1e10SCesar Eduardo Barros 		if (error < 0)
181287ade72aSCesar Eduardo Barros 			return error;
18134d0e1e10SCesar Eduardo Barros 		p->flags |= SWP_BLKDEV;
18144d0e1e10SCesar Eduardo Barros 	} else if (S_ISREG(inode->i_mode)) {
18154d0e1e10SCesar Eduardo Barros 		p->bdev = inode->i_sb->s_bdev;
18164d0e1e10SCesar Eduardo Barros 		mutex_lock(&inode->i_mutex);
181787ade72aSCesar Eduardo Barros 		if (IS_SWAPFILE(inode))
181887ade72aSCesar Eduardo Barros 			return -EBUSY;
181987ade72aSCesar Eduardo Barros 	} else
182087ade72aSCesar Eduardo Barros 		return -EINVAL;
18214d0e1e10SCesar Eduardo Barros 
18224d0e1e10SCesar Eduardo Barros 	return 0;
18234d0e1e10SCesar Eduardo Barros }
18244d0e1e10SCesar Eduardo Barros 
1825ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p,
1826ca8bd38bSCesar Eduardo Barros 					union swap_header *swap_header,
1827ca8bd38bSCesar Eduardo Barros 					struct inode *inode)
1828ca8bd38bSCesar Eduardo Barros {
1829ca8bd38bSCesar Eduardo Barros 	int i;
1830ca8bd38bSCesar Eduardo Barros 	unsigned long maxpages;
1831ca8bd38bSCesar Eduardo Barros 	unsigned long swapfilepages;
1832ca8bd38bSCesar Eduardo Barros 
1833ca8bd38bSCesar Eduardo Barros 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1834ca8bd38bSCesar Eduardo Barros 		printk(KERN_ERR "Unable to find swap-space signature\n");
183538719025SCesar Eduardo Barros 		return 0;
1836ca8bd38bSCesar Eduardo Barros 	}
1837ca8bd38bSCesar Eduardo Barros 
1838ca8bd38bSCesar Eduardo Barros 	/* swap partition endianess hack... */
1839ca8bd38bSCesar Eduardo Barros 	if (swab32(swap_header->info.version) == 1) {
1840ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.version);
1841ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.last_page);
1842ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.nr_badpages);
1843ca8bd38bSCesar Eduardo Barros 		for (i = 0; i < swap_header->info.nr_badpages; i++)
1844ca8bd38bSCesar Eduardo Barros 			swab32s(&swap_header->info.badpages[i]);
1845ca8bd38bSCesar Eduardo Barros 	}
1846ca8bd38bSCesar Eduardo Barros 	/* Check the swap header's sub-version */
1847ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.version != 1) {
1848ca8bd38bSCesar Eduardo Barros 		printk(KERN_WARNING
1849ca8bd38bSCesar Eduardo Barros 		       "Unable to handle swap header version %d\n",
1850ca8bd38bSCesar Eduardo Barros 		       swap_header->info.version);
185138719025SCesar Eduardo Barros 		return 0;
1852ca8bd38bSCesar Eduardo Barros 	}
1853ca8bd38bSCesar Eduardo Barros 
1854ca8bd38bSCesar Eduardo Barros 	p->lowest_bit  = 1;
1855ca8bd38bSCesar Eduardo Barros 	p->cluster_next = 1;
1856ca8bd38bSCesar Eduardo Barros 	p->cluster_nr = 0;
1857ca8bd38bSCesar Eduardo Barros 
1858ca8bd38bSCesar Eduardo Barros 	/*
1859ca8bd38bSCesar Eduardo Barros 	 * Find out how many pages are allowed for a single swap
18609b15b817SHugh Dickins 	 * device. There are two limiting factors: 1) the number
1861a2c16d6cSHugh Dickins 	 * of bits for the swap offset in the swp_entry_t type, and
1862a2c16d6cSHugh Dickins 	 * 2) the number of bits in the swap pte as defined by the
18639b15b817SHugh Dickins 	 * different architectures. In order to find the
1864a2c16d6cSHugh Dickins 	 * largest possible bit mask, a swap entry with swap type 0
1865ca8bd38bSCesar Eduardo Barros 	 * and swap offset ~0UL is created, encoded to a swap pte,
1866a2c16d6cSHugh Dickins 	 * decoded to a swp_entry_t again, and finally the swap
1867ca8bd38bSCesar Eduardo Barros 	 * offset is extracted. This will mask all the bits from
1868ca8bd38bSCesar Eduardo Barros 	 * the initial ~0UL mask that can't be encoded in either
1869ca8bd38bSCesar Eduardo Barros 	 * the swp_entry_t or the architecture definition of a
18709b15b817SHugh Dickins 	 * swap pte.
1871ca8bd38bSCesar Eduardo Barros 	 */
1872ca8bd38bSCesar Eduardo Barros 	maxpages = swp_offset(pte_to_swp_entry(
18739b15b817SHugh Dickins 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
1874ca8bd38bSCesar Eduardo Barros 	if (maxpages > swap_header->info.last_page) {
1875ca8bd38bSCesar Eduardo Barros 		maxpages = swap_header->info.last_page + 1;
1876ca8bd38bSCesar Eduardo Barros 		/* p->max is an unsigned int: don't overflow it */
1877ca8bd38bSCesar Eduardo Barros 		if ((unsigned int)maxpages == 0)
1878ca8bd38bSCesar Eduardo Barros 			maxpages = UINT_MAX;
1879ca8bd38bSCesar Eduardo Barros 	}
1880ca8bd38bSCesar Eduardo Barros 	p->highest_bit = maxpages - 1;
1881ca8bd38bSCesar Eduardo Barros 
1882ca8bd38bSCesar Eduardo Barros 	if (!maxpages)
188338719025SCesar Eduardo Barros 		return 0;
1884ca8bd38bSCesar Eduardo Barros 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
1885ca8bd38bSCesar Eduardo Barros 	if (swapfilepages && maxpages > swapfilepages) {
1886ca8bd38bSCesar Eduardo Barros 		printk(KERN_WARNING
1887ca8bd38bSCesar Eduardo Barros 		       "Swap area shorter than signature indicates\n");
188838719025SCesar Eduardo Barros 		return 0;
1889ca8bd38bSCesar Eduardo Barros 	}
1890ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
189138719025SCesar Eduardo Barros 		return 0;
1892ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
189338719025SCesar Eduardo Barros 		return 0;
1894ca8bd38bSCesar Eduardo Barros 
1895ca8bd38bSCesar Eduardo Barros 	return maxpages;
1896ca8bd38bSCesar Eduardo Barros }
1897ca8bd38bSCesar Eduardo Barros 
1898915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p,
1899915d4d7bSCesar Eduardo Barros 					union swap_header *swap_header,
1900915d4d7bSCesar Eduardo Barros 					unsigned char *swap_map,
1901915d4d7bSCesar Eduardo Barros 					unsigned long maxpages,
1902915d4d7bSCesar Eduardo Barros 					sector_t *span)
1903915d4d7bSCesar Eduardo Barros {
1904915d4d7bSCesar Eduardo Barros 	int i;
1905915d4d7bSCesar Eduardo Barros 	unsigned int nr_good_pages;
1906915d4d7bSCesar Eduardo Barros 	int nr_extents;
1907915d4d7bSCesar Eduardo Barros 
1908915d4d7bSCesar Eduardo Barros 	nr_good_pages = maxpages - 1;	/* omit header page */
1909915d4d7bSCesar Eduardo Barros 
1910915d4d7bSCesar Eduardo Barros 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
1911915d4d7bSCesar Eduardo Barros 		unsigned int page_nr = swap_header->info.badpages[i];
1912bdb8e3f6SCesar Eduardo Barros 		if (page_nr == 0 || page_nr > swap_header->info.last_page)
1913bdb8e3f6SCesar Eduardo Barros 			return -EINVAL;
1914915d4d7bSCesar Eduardo Barros 		if (page_nr < maxpages) {
1915915d4d7bSCesar Eduardo Barros 			swap_map[page_nr] = SWAP_MAP_BAD;
1916915d4d7bSCesar Eduardo Barros 			nr_good_pages--;
1917915d4d7bSCesar Eduardo Barros 		}
1918915d4d7bSCesar Eduardo Barros 	}
1919915d4d7bSCesar Eduardo Barros 
1920915d4d7bSCesar Eduardo Barros 	if (nr_good_pages) {
1921915d4d7bSCesar Eduardo Barros 		swap_map[0] = SWAP_MAP_BAD;
1922915d4d7bSCesar Eduardo Barros 		p->max = maxpages;
1923915d4d7bSCesar Eduardo Barros 		p->pages = nr_good_pages;
1924915d4d7bSCesar Eduardo Barros 		nr_extents = setup_swap_extents(p, span);
1925bdb8e3f6SCesar Eduardo Barros 		if (nr_extents < 0)
1926bdb8e3f6SCesar Eduardo Barros 			return nr_extents;
1927915d4d7bSCesar Eduardo Barros 		nr_good_pages = p->pages;
1928915d4d7bSCesar Eduardo Barros 	}
1929915d4d7bSCesar Eduardo Barros 	if (!nr_good_pages) {
1930915d4d7bSCesar Eduardo Barros 		printk(KERN_WARNING "Empty swap-file\n");
1931bdb8e3f6SCesar Eduardo Barros 		return -EINVAL;
1932915d4d7bSCesar Eduardo Barros 	}
1933915d4d7bSCesar Eduardo Barros 
1934915d4d7bSCesar Eduardo Barros 	return nr_extents;
1935915d4d7bSCesar Eduardo Barros }
1936915d4d7bSCesar Eduardo Barros 
193753cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
193853cbb243SCesar Eduardo Barros {
193953cbb243SCesar Eduardo Barros 	struct swap_info_struct *p;
194028b36bd7SCesar Eduardo Barros 	char *name;
194153cbb243SCesar Eduardo Barros 	struct file *swap_file = NULL;
194253cbb243SCesar Eduardo Barros 	struct address_space *mapping;
194340531542SCesar Eduardo Barros 	int i;
194440531542SCesar Eduardo Barros 	int prio;
194553cbb243SCesar Eduardo Barros 	int error;
194653cbb243SCesar Eduardo Barros 	union swap_header *swap_header;
1947915d4d7bSCesar Eduardo Barros 	int nr_extents;
194853cbb243SCesar Eduardo Barros 	sector_t span;
194953cbb243SCesar Eduardo Barros 	unsigned long maxpages;
195053cbb243SCesar Eduardo Barros 	unsigned char *swap_map = NULL;
195138b5faf4SDan Magenheimer 	unsigned long *frontswap_map = NULL;
195253cbb243SCesar Eduardo Barros 	struct page *page = NULL;
195353cbb243SCesar Eduardo Barros 	struct inode *inode = NULL;
195453cbb243SCesar Eduardo Barros 
1955d15cab97SHugh Dickins 	if (swap_flags & ~SWAP_FLAGS_VALID)
1956d15cab97SHugh Dickins 		return -EINVAL;
1957d15cab97SHugh Dickins 
195853cbb243SCesar Eduardo Barros 	if (!capable(CAP_SYS_ADMIN))
195953cbb243SCesar Eduardo Barros 		return -EPERM;
196053cbb243SCesar Eduardo Barros 
196153cbb243SCesar Eduardo Barros 	p = alloc_swap_info();
19622542e513SCesar Eduardo Barros 	if (IS_ERR(p))
19632542e513SCesar Eduardo Barros 		return PTR_ERR(p);
196453cbb243SCesar Eduardo Barros 
19651da177e4SLinus Torvalds 	name = getname(specialfile);
19661da177e4SLinus Torvalds 	if (IS_ERR(name)) {
19677de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(name);
19681da177e4SLinus Torvalds 		name = NULL;
1969bd69010bSCesar Eduardo Barros 		goto bad_swap;
19701da177e4SLinus Torvalds 	}
19711da177e4SLinus Torvalds 	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
19721da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
19737de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(swap_file);
19741da177e4SLinus Torvalds 		swap_file = NULL;
1975bd69010bSCesar Eduardo Barros 		goto bad_swap;
19761da177e4SLinus Torvalds 	}
19771da177e4SLinus Torvalds 
19781da177e4SLinus Torvalds 	p->swap_file = swap_file;
19791da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
1982efa90a98SHugh Dickins 		struct swap_info_struct *q = swap_info[i];
19831da177e4SLinus Torvalds 
1984e8e6c2ecSCesar Eduardo Barros 		if (q == p || !q->swap_file)
19851da177e4SLinus Torvalds 			continue;
19867de7fb6bSCesar Eduardo Barros 		if (mapping == q->swap_file->f_mapping) {
19877de7fb6bSCesar Eduardo Barros 			error = -EBUSY;
19881da177e4SLinus Torvalds 			goto bad_swap;
19891da177e4SLinus Torvalds 		}
19907de7fb6bSCesar Eduardo Barros 	}
19911da177e4SLinus Torvalds 
19922130781eSCesar Eduardo Barros 	inode = mapping->host;
19932130781eSCesar Eduardo Barros 	/* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
19944d0e1e10SCesar Eduardo Barros 	error = claim_swapfile(p, inode);
19954d0e1e10SCesar Eduardo Barros 	if (unlikely(error))
19961da177e4SLinus Torvalds 		goto bad_swap;
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds 	/*
19991da177e4SLinus Torvalds 	 * Read the swap header.
20001da177e4SLinus Torvalds 	 */
20011da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
20021da177e4SLinus Torvalds 		error = -EINVAL;
20031da177e4SLinus Torvalds 		goto bad_swap;
20041da177e4SLinus Torvalds 	}
2005090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
20061da177e4SLinus Torvalds 	if (IS_ERR(page)) {
20071da177e4SLinus Torvalds 		error = PTR_ERR(page);
20081da177e4SLinus Torvalds 		goto bad_swap;
20091da177e4SLinus Torvalds 	}
201081e33971SHugh Dickins 	swap_header = kmap(page);
20111da177e4SLinus Torvalds 
2012ca8bd38bSCesar Eduardo Barros 	maxpages = read_swap_header(p, swap_header, inode);
2013ca8bd38bSCesar Eduardo Barros 	if (unlikely(!maxpages)) {
20141da177e4SLinus Torvalds 		error = -EINVAL;
20151da177e4SLinus Torvalds 		goto bad_swap;
20161da177e4SLinus Torvalds 	}
20171da177e4SLinus Torvalds 
20181da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
2019803d0c83SCesar Eduardo Barros 	swap_map = vzalloc(maxpages);
202078ecba08SHugh Dickins 	if (!swap_map) {
20211da177e4SLinus Torvalds 		error = -ENOMEM;
20221da177e4SLinus Torvalds 		goto bad_swap;
20231da177e4SLinus Torvalds 	}
20241da177e4SLinus Torvalds 
20251421ef3cSCesar Eduardo Barros 	error = swap_cgroup_swapon(p->type, maxpages);
20261421ef3cSCesar Eduardo Barros 	if (error)
20271421ef3cSCesar Eduardo Barros 		goto bad_swap;
20281421ef3cSCesar Eduardo Barros 
2029915d4d7bSCesar Eduardo Barros 	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
2030915d4d7bSCesar Eduardo Barros 		maxpages, &span);
2031915d4d7bSCesar Eduardo Barros 	if (unlikely(nr_extents < 0)) {
203253092a74SHugh Dickins 		error = nr_extents;
2033e2244ec2SHugh Dickins 		goto bad_swap;
203453092a74SHugh Dickins 	}
203538b5faf4SDan Magenheimer 	/* frontswap enabled? set up bit-per-page map for frontswap */
203638b5faf4SDan Magenheimer 	if (frontswap_enabled)
203738b5faf4SDan Magenheimer 		frontswap_map = vzalloc(maxpages / sizeof(long));
20381da177e4SLinus Torvalds 
20393bd0f0c7SSuresh Jayaraman 	if (p->bdev) {
204020137a49SHugh Dickins 		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
204120137a49SHugh Dickins 			p->flags |= SWP_SOLIDSTATE;
204220137a49SHugh Dickins 			p->cluster_next = 1 + (random32() % p->highest_bit);
204320137a49SHugh Dickins 		}
2044052b1987SShaohua Li 		if ((swap_flags & SWAP_FLAG_DISCARD) && discard_swap(p) == 0)
20456a6ba831SHugh Dickins 			p->flags |= SWP_DISCARDABLE;
20463bd0f0c7SSuresh Jayaraman 	}
20476a6ba831SHugh Dickins 
2048fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
204940531542SCesar Eduardo Barros 	prio = -1;
205078ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
205140531542SCesar Eduardo Barros 		prio =
205278ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
205338b5faf4SDan Magenheimer 	enable_swap_info(p, prio, swap_map, frontswap_map);
2054c69dbfb8SCesar Eduardo Barros 
2055c69dbfb8SCesar Eduardo Barros 	printk(KERN_INFO "Adding %uk swap on %s.  "
205638b5faf4SDan Magenheimer 			"Priority:%d extents:%d across:%lluk %s%s%s\n",
2057c69dbfb8SCesar Eduardo Barros 		p->pages<<(PAGE_SHIFT-10), name, p->prio,
2058c69dbfb8SCesar Eduardo Barros 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
2059c69dbfb8SCesar Eduardo Barros 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
206038b5faf4SDan Magenheimer 		(p->flags & SWP_DISCARDABLE) ? "D" : "",
206138b5faf4SDan Magenheimer 		(frontswap_map) ? "FS" : "");
2062c69dbfb8SCesar Eduardo Barros 
2063fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
206466d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
206566d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
206666d7dd51SKay Sievers 
20679b01c350SCesar Eduardo Barros 	if (S_ISREG(inode->i_mode))
20689b01c350SCesar Eduardo Barros 		inode->i_flags |= S_SWAPFILE;
20691da177e4SLinus Torvalds 	error = 0;
20701da177e4SLinus Torvalds 	goto out;
20711da177e4SLinus Torvalds bad_swap:
2072bd69010bSCesar Eduardo Barros 	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
2073f2090d2dSCesar Eduardo Barros 		set_blocksize(p->bdev, p->old_block_size);
2074f2090d2dSCesar Eduardo Barros 		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
20751da177e4SLinus Torvalds 	}
20764cd3bb10SHugh Dickins 	destroy_swap_extents(p);
2077e8e6c2ecSCesar Eduardo Barros 	swap_cgroup_swapoff(p->type);
20785d337b91SHugh Dickins 	spin_lock(&swap_lock);
20791da177e4SLinus Torvalds 	p->swap_file = NULL;
20801da177e4SLinus Torvalds 	p->flags = 0;
20815d337b91SHugh Dickins 	spin_unlock(&swap_lock);
20821da177e4SLinus Torvalds 	vfree(swap_map);
208352c50567SMel Gorman 	if (swap_file) {
20842130781eSCesar Eduardo Barros 		if (inode && S_ISREG(inode->i_mode)) {
208552c50567SMel Gorman 			mutex_unlock(&inode->i_mutex);
20862130781eSCesar Eduardo Barros 			inode = NULL;
20872130781eSCesar Eduardo Barros 		}
20881da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
208952c50567SMel Gorman 	}
20901da177e4SLinus Torvalds out:
20911da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
20921da177e4SLinus Torvalds 		kunmap(page);
20931da177e4SLinus Torvalds 		page_cache_release(page);
20941da177e4SLinus Torvalds 	}
20951da177e4SLinus Torvalds 	if (name)
20961da177e4SLinus Torvalds 		putname(name);
20979b01c350SCesar Eduardo Barros 	if (inode && S_ISREG(inode->i_mode))
20981b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
20991da177e4SLinus Torvalds 	return error;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
21031da177e4SLinus Torvalds {
2104efa90a98SHugh Dickins 	unsigned int type;
21051da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
21061da177e4SLinus Torvalds 
21075d337b91SHugh Dickins 	spin_lock(&swap_lock);
2108efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2109efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
2110efa90a98SHugh Dickins 
2111efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2112efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
21131da177e4SLinus Torvalds 	}
21141da177e4SLinus Torvalds 	val->freeswap = nr_swap_pages + nr_to_be_unused;
21151da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
21165d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21171da177e4SLinus Torvalds }
21181da177e4SLinus Torvalds 
21191da177e4SLinus Torvalds /*
21201da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
21211da177e4SLinus Torvalds  *
2122355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
2123355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
2124355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
2125355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
2126355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
2127355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
2128570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
21291da177e4SLinus Torvalds  */
21308d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
21311da177e4SLinus Torvalds {
21321da177e4SLinus Torvalds 	struct swap_info_struct *p;
21331da177e4SLinus Torvalds 	unsigned long offset, type;
21348d69aaeeSHugh Dickins 	unsigned char count;
21358d69aaeeSHugh Dickins 	unsigned char has_cache;
2136253d553bSHugh Dickins 	int err = -EINVAL;
21371da177e4SLinus Torvalds 
2138a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
2139253d553bSHugh Dickins 		goto out;
21400697212aSChristoph Lameter 
21411da177e4SLinus Torvalds 	type = swp_type(entry);
21421da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
21431da177e4SLinus Torvalds 		goto bad_file;
2144efa90a98SHugh Dickins 	p = swap_info[type];
21451da177e4SLinus Torvalds 	offset = swp_offset(entry);
21461da177e4SLinus Torvalds 
21475d337b91SHugh Dickins 	spin_lock(&swap_lock);
2148355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
2149355cfa73SKAMEZAWA Hiroyuki 		goto unlock_out;
2150355cfa73SKAMEZAWA Hiroyuki 
2151253d553bSHugh Dickins 	count = p->swap_map[offset];
2152253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
2153253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
2154253d553bSHugh Dickins 	err = 0;
2155355cfa73SKAMEZAWA Hiroyuki 
2156253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
2157355cfa73SKAMEZAWA Hiroyuki 
2158355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2159253d553bSHugh Dickins 		if (!has_cache && count)
2160253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
2161253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
2162253d553bSHugh Dickins 			err = -EEXIST;
2163253d553bSHugh Dickins 		else				/* no users remaining */
2164253d553bSHugh Dickins 			err = -ENOENT;
2165355cfa73SKAMEZAWA Hiroyuki 
2166355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
2167253d553bSHugh Dickins 
2168570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2169570a335bSHugh Dickins 			count += usage;
2170570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2171253d553bSHugh Dickins 			err = -EINVAL;
2172570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
2173570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2174570a335bSHugh Dickins 		else
2175570a335bSHugh Dickins 			err = -ENOMEM;
2176253d553bSHugh Dickins 	} else
2177253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
2178253d553bSHugh Dickins 
2179253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
2180253d553bSHugh Dickins 
2181355cfa73SKAMEZAWA Hiroyuki unlock_out:
21825d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21831da177e4SLinus Torvalds out:
2184253d553bSHugh Dickins 	return err;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds bad_file:
21871da177e4SLinus Torvalds 	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
21881da177e4SLinus Torvalds 	goto out;
21891da177e4SLinus Torvalds }
2190253d553bSHugh Dickins 
2191355cfa73SKAMEZAWA Hiroyuki /*
2192aaa46865SHugh Dickins  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2193aaa46865SHugh Dickins  * (in which case its reference count is never incremented).
2194aaa46865SHugh Dickins  */
2195aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry)
2196aaa46865SHugh Dickins {
2197aaa46865SHugh Dickins 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
2198aaa46865SHugh Dickins }
2199aaa46865SHugh Dickins 
2200aaa46865SHugh Dickins /*
220108259d58SHugh Dickins  * Increase reference count of swap entry by 1.
220208259d58SHugh Dickins  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
220308259d58SHugh Dickins  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
220408259d58SHugh Dickins  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
220508259d58SHugh Dickins  * might occur if a page table entry has got corrupted.
2206355cfa73SKAMEZAWA Hiroyuki  */
2207570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
2208355cfa73SKAMEZAWA Hiroyuki {
2209570a335bSHugh Dickins 	int err = 0;
2210570a335bSHugh Dickins 
2211570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2212570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2213570a335bSHugh Dickins 	return err;
2214355cfa73SKAMEZAWA Hiroyuki }
22151da177e4SLinus Torvalds 
2216cb4b86baSKAMEZAWA Hiroyuki /*
2217355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
2218355cfa73SKAMEZAWA Hiroyuki  *
221973c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
2220355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
2221355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
2222355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
2223cb4b86baSKAMEZAWA Hiroyuki  */
2224cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
2225cb4b86baSKAMEZAWA Hiroyuki {
2226253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2227cb4b86baSKAMEZAWA Hiroyuki }
2228cb4b86baSKAMEZAWA Hiroyuki 
2229f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page)
2230f981c595SMel Gorman {
2231f981c595SMel Gorman 	swp_entry_t swap = { .val = page_private(page) };
2232f981c595SMel Gorman 	BUG_ON(!PageSwapCache(page));
2233f981c595SMel Gorman 	return swap_info[swp_type(swap)];
2234f981c595SMel Gorman }
2235f981c595SMel Gorman 
2236f981c595SMel Gorman /*
2237f981c595SMel Gorman  * out-of-line __page_file_ methods to avoid include hell.
2238f981c595SMel Gorman  */
2239f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page)
2240f981c595SMel Gorman {
2241f981c595SMel Gorman 	VM_BUG_ON(!PageSwapCache(page));
2242f981c595SMel Gorman 	return page_swap_info(page)->swap_file->f_mapping;
2243f981c595SMel Gorman }
2244f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping);
2245f981c595SMel Gorman 
2246f981c595SMel Gorman pgoff_t __page_file_index(struct page *page)
2247f981c595SMel Gorman {
2248f981c595SMel Gorman 	swp_entry_t swap = { .val = page_private(page) };
2249f981c595SMel Gorman 	VM_BUG_ON(!PageSwapCache(page));
2250f981c595SMel Gorman 	return swp_offset(swap);
2251f981c595SMel Gorman }
2252f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index);
2253f981c595SMel Gorman 
22541da177e4SLinus Torvalds /*
2255570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
2256570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2257570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
2258570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2259570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2260570a335bSHugh Dickins  *
2261570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
2262570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
2263570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2264570a335bSHugh Dickins  *
2265570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2266570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2267570a335bSHugh Dickins  * can be called after dropping locks.
2268570a335bSHugh Dickins  */
2269570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2270570a335bSHugh Dickins {
2271570a335bSHugh Dickins 	struct swap_info_struct *si;
2272570a335bSHugh Dickins 	struct page *head;
2273570a335bSHugh Dickins 	struct page *page;
2274570a335bSHugh Dickins 	struct page *list_page;
2275570a335bSHugh Dickins 	pgoff_t offset;
2276570a335bSHugh Dickins 	unsigned char count;
2277570a335bSHugh Dickins 
2278570a335bSHugh Dickins 	/*
2279570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2280570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2281570a335bSHugh Dickins 	 */
2282570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2283570a335bSHugh Dickins 
2284570a335bSHugh Dickins 	si = swap_info_get(entry);
2285570a335bSHugh Dickins 	if (!si) {
2286570a335bSHugh Dickins 		/*
2287570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
2288570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
2289570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
2290570a335bSHugh Dickins 		 */
2291570a335bSHugh Dickins 		goto outer;
2292570a335bSHugh Dickins 	}
2293570a335bSHugh Dickins 
2294570a335bSHugh Dickins 	offset = swp_offset(entry);
2295570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2296570a335bSHugh Dickins 
2297570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2298570a335bSHugh Dickins 		/*
2299570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
2300570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
2301570a335bSHugh Dickins 		 * over-provisioning.
2302570a335bSHugh Dickins 		 */
2303570a335bSHugh Dickins 		goto out;
2304570a335bSHugh Dickins 	}
2305570a335bSHugh Dickins 
2306570a335bSHugh Dickins 	if (!page) {
2307570a335bSHugh Dickins 		spin_unlock(&swap_lock);
2308570a335bSHugh Dickins 		return -ENOMEM;
2309570a335bSHugh Dickins 	}
2310570a335bSHugh Dickins 
2311570a335bSHugh Dickins 	/*
2312570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2313570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel pagetables: so it
2314570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2315570a335bSHugh Dickins 	 */
2316570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2317570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2318570a335bSHugh Dickins 
2319570a335bSHugh Dickins 	/*
2320570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
2321570a335bSHugh Dickins 	 * but it does always reset its private field.
2322570a335bSHugh Dickins 	 */
2323570a335bSHugh Dickins 	if (!page_private(head)) {
2324570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2325570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
2326570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
2327570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
2328570a335bSHugh Dickins 	}
2329570a335bSHugh Dickins 
2330570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
2331570a335bSHugh Dickins 		unsigned char *map;
2332570a335bSHugh Dickins 
2333570a335bSHugh Dickins 		/*
2334570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
2335570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
2336570a335bSHugh Dickins 		 */
2337570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
2338570a335bSHugh Dickins 			goto out;
2339570a335bSHugh Dickins 
23409b04c5feSCong Wang 		map = kmap_atomic(list_page) + offset;
2341570a335bSHugh Dickins 		count = *map;
23429b04c5feSCong Wang 		kunmap_atomic(map);
2343570a335bSHugh Dickins 
2344570a335bSHugh Dickins 		/*
2345570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
2346570a335bSHugh Dickins 		 * free our allocation and use this one.
2347570a335bSHugh Dickins 		 */
2348570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2349570a335bSHugh Dickins 			goto out;
2350570a335bSHugh Dickins 	}
2351570a335bSHugh Dickins 
2352570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
2353570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
2354570a335bSHugh Dickins out:
2355570a335bSHugh Dickins 	spin_unlock(&swap_lock);
2356570a335bSHugh Dickins outer:
2357570a335bSHugh Dickins 	if (page)
2358570a335bSHugh Dickins 		__free_page(page);
2359570a335bSHugh Dickins 	return 0;
2360570a335bSHugh Dickins }
2361570a335bSHugh Dickins 
2362570a335bSHugh Dickins /*
2363570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
2364570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2365570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
2366570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
2367570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
2368570a335bSHugh Dickins  * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2369570a335bSHugh Dickins  */
2370570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
2371570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
2372570a335bSHugh Dickins {
2373570a335bSHugh Dickins 	struct page *head;
2374570a335bSHugh Dickins 	struct page *page;
2375570a335bSHugh Dickins 	unsigned char *map;
2376570a335bSHugh Dickins 
2377570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2378570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
2379570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2380570a335bSHugh Dickins 		return false;		/* need to add count continuation */
2381570a335bSHugh Dickins 	}
2382570a335bSHugh Dickins 
2383570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2384570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
23859b04c5feSCong Wang 	map = kmap_atomic(page) + offset;
2386570a335bSHugh Dickins 
2387570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2388570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2389570a335bSHugh Dickins 
2390570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2391570a335bSHugh Dickins 		/*
2392570a335bSHugh Dickins 		 * Think of how you add 1 to 999
2393570a335bSHugh Dickins 		 */
2394570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
23959b04c5feSCong Wang 			kunmap_atomic(map);
2396570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2397570a335bSHugh Dickins 			BUG_ON(page == head);
23989b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2399570a335bSHugh Dickins 		}
2400570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
24019b04c5feSCong Wang 			kunmap_atomic(map);
2402570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2403570a335bSHugh Dickins 			if (page == head)
2404570a335bSHugh Dickins 				return false;	/* add count continuation */
24059b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2406570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
2407570a335bSHugh Dickins 		}
2408570a335bSHugh Dickins 		*map += 1;
24099b04c5feSCong Wang 		kunmap_atomic(map);
2410570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2411570a335bSHugh Dickins 		while (page != head) {
24129b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2413570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
24149b04c5feSCong Wang 			kunmap_atomic(map);
2415570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2416570a335bSHugh Dickins 		}
2417570a335bSHugh Dickins 		return true;			/* incremented */
2418570a335bSHugh Dickins 
2419570a335bSHugh Dickins 	} else {				/* decrementing */
2420570a335bSHugh Dickins 		/*
2421570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
2422570a335bSHugh Dickins 		 */
2423570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
2424570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
24259b04c5feSCong Wang 			kunmap_atomic(map);
2426570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2427570a335bSHugh Dickins 			BUG_ON(page == head);
24289b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2429570a335bSHugh Dickins 		}
2430570a335bSHugh Dickins 		BUG_ON(*map == 0);
2431570a335bSHugh Dickins 		*map -= 1;
2432570a335bSHugh Dickins 		if (*map == 0)
2433570a335bSHugh Dickins 			count = 0;
24349b04c5feSCong Wang 		kunmap_atomic(map);
2435570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2436570a335bSHugh Dickins 		while (page != head) {
24379b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2438570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
2439570a335bSHugh Dickins 			count = COUNT_CONTINUED;
24409b04c5feSCong Wang 			kunmap_atomic(map);
2441570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2442570a335bSHugh Dickins 		}
2443570a335bSHugh Dickins 		return count == COUNT_CONTINUED;
2444570a335bSHugh Dickins 	}
2445570a335bSHugh Dickins }
2446570a335bSHugh Dickins 
2447570a335bSHugh Dickins /*
2448570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
2449570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2450570a335bSHugh Dickins  */
2451570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
2452570a335bSHugh Dickins {
2453570a335bSHugh Dickins 	pgoff_t offset;
2454570a335bSHugh Dickins 
2455570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2456570a335bSHugh Dickins 		struct page *head;
2457570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
2458570a335bSHugh Dickins 		if (page_private(head)) {
2459570a335bSHugh Dickins 			struct list_head *this, *next;
2460570a335bSHugh Dickins 			list_for_each_safe(this, next, &head->lru) {
2461570a335bSHugh Dickins 				struct page *page;
2462570a335bSHugh Dickins 				page = list_entry(this, struct page, lru);
2463570a335bSHugh Dickins 				list_del(this);
2464570a335bSHugh Dickins 				__free_page(page);
2465570a335bSHugh Dickins 			}
2466570a335bSHugh Dickins 		}
2467570a335bSHugh Dickins 	}
2468570a335bSHugh Dickins }
2469