xref: /openbmc/linux/mm/swapfile.c (revision 72788c385604523422592249c19cba0187021e9b)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swapfile.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/hugetlb.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
141da177e4SLinus Torvalds #include <linux/vmalloc.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
171da177e4SLinus Torvalds #include <linux/shm.h>
181da177e4SLinus Torvalds #include <linux/blkdev.h>
1920137a49SHugh Dickins #include <linux/random.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
211da177e4SLinus Torvalds #include <linux/proc_fs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/module.h>
255ad64688SHugh Dickins #include <linux/ksm.h>
261da177e4SLinus Torvalds #include <linux/rmap.h>
271da177e4SLinus Torvalds #include <linux/security.h>
281da177e4SLinus Torvalds #include <linux/backing-dev.h>
29fc0abb14SIngo Molnar #include <linux/mutex.h>
30c59ede7bSRandy.Dunlap #include <linux/capability.h>
311da177e4SLinus Torvalds #include <linux/syscalls.h>
328a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
3366d7dd51SKay Sievers #include <linux/poll.h>
34*72788c38SDavid Rientjes #include <linux/oom.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <asm/pgtable.h>
371da177e4SLinus Torvalds #include <asm/tlbflush.h>
381da177e4SLinus Torvalds #include <linux/swapops.h>
3927a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h>
401da177e4SLinus Torvalds 
41570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
42570a335bSHugh Dickins 				 unsigned char);
43570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
44d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**);
45570a335bSHugh Dickins 
467c363b8cSAdrian Bunk static DEFINE_SPINLOCK(swap_lock);
477c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
48b962716bSHugh Dickins long nr_swap_pages;
491da177e4SLinus Torvalds long total_swap_pages;
5078ecba08SHugh Dickins static int least_priority;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
531da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
541da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
551da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
561da177e4SLinus Torvalds 
577c363b8cSAdrian Bunk static struct swap_list_t swap_list = {-1, -1};
581da177e4SLinus Torvalds 
59efa90a98SHugh Dickins static struct swap_info_struct *swap_info[MAX_SWAPFILES];
601da177e4SLinus Torvalds 
61fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
621da177e4SLinus Torvalds 
6366d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
6466d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */
6566d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0);
6666d7dd51SKay Sievers 
678d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
68355cfa73SKAMEZAWA Hiroyuki {
69570a335bSHugh Dickins 	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
70355cfa73SKAMEZAWA Hiroyuki }
71355cfa73SKAMEZAWA Hiroyuki 
72efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
73c9e44410SKAMEZAWA Hiroyuki static int
74c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
75c9e44410SKAMEZAWA Hiroyuki {
76efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
77c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
78c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
79c9e44410SKAMEZAWA Hiroyuki 
80c9e44410SKAMEZAWA Hiroyuki 	page = find_get_page(&swapper_space, entry.val);
81c9e44410SKAMEZAWA Hiroyuki 	if (!page)
82c9e44410SKAMEZAWA Hiroyuki 		return 0;
83c9e44410SKAMEZAWA Hiroyuki 	/*
84c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
85c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
86c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
87c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
88c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
89c9e44410SKAMEZAWA Hiroyuki 	 */
90c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
91c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
92c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
93c9e44410SKAMEZAWA Hiroyuki 	}
94c9e44410SKAMEZAWA Hiroyuki 	page_cache_release(page);
95c9e44410SKAMEZAWA Hiroyuki 	return ret;
96c9e44410SKAMEZAWA Hiroyuki }
97355cfa73SKAMEZAWA Hiroyuki 
981da177e4SLinus Torvalds /*
996a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1006a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1016a6ba831SHugh Dickins  */
1026a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1036a6ba831SHugh Dickins {
1046a6ba831SHugh Dickins 	struct swap_extent *se;
1059625a5f2SHugh Dickins 	sector_t start_block;
1069625a5f2SHugh Dickins 	sector_t nr_blocks;
1076a6ba831SHugh Dickins 	int err = 0;
1086a6ba831SHugh Dickins 
1096a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1109625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1119625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1129625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1139625a5f2SHugh Dickins 	if (nr_blocks) {
1149625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
115dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1169625a5f2SHugh Dickins 		if (err)
1179625a5f2SHugh Dickins 			return err;
1189625a5f2SHugh Dickins 		cond_resched();
1196a6ba831SHugh Dickins 	}
1206a6ba831SHugh Dickins 
1219625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1229625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1239625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1249625a5f2SHugh Dickins 
1256a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
126dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1276a6ba831SHugh Dickins 		if (err)
1286a6ba831SHugh Dickins 			break;
1296a6ba831SHugh Dickins 
1306a6ba831SHugh Dickins 		cond_resched();
1316a6ba831SHugh Dickins 	}
1326a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1336a6ba831SHugh Dickins }
1346a6ba831SHugh Dickins 
1357992fde7SHugh Dickins /*
1367992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1377992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1387992fde7SHugh Dickins  */
1397992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1407992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1417992fde7SHugh Dickins {
1427992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1437992fde7SHugh Dickins 	int found_extent = 0;
1447992fde7SHugh Dickins 
1457992fde7SHugh Dickins 	while (nr_pages) {
1467992fde7SHugh Dickins 		struct list_head *lh;
1477992fde7SHugh Dickins 
1487992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1497992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1507992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1517992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
152858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1537992fde7SHugh Dickins 
1547992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1557992fde7SHugh Dickins 				nr_blocks = nr_pages;
1567992fde7SHugh Dickins 			start_page += nr_blocks;
1577992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1587992fde7SHugh Dickins 
1597992fde7SHugh Dickins 			if (!found_extent++)
1607992fde7SHugh Dickins 				si->curr_swap_extent = se;
1617992fde7SHugh Dickins 
1627992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1637992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1647992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
165dd3932edSChristoph Hellwig 				    nr_blocks, GFP_NOIO, 0))
1667992fde7SHugh Dickins 				break;
1677992fde7SHugh Dickins 		}
1687992fde7SHugh Dickins 
1697992fde7SHugh Dickins 		lh = se->list.next;
1707992fde7SHugh Dickins 		se = list_entry(lh, struct swap_extent, list);
1717992fde7SHugh Dickins 	}
1727992fde7SHugh Dickins }
1737992fde7SHugh Dickins 
1747992fde7SHugh Dickins static int wait_for_discard(void *word)
1757992fde7SHugh Dickins {
1767992fde7SHugh Dickins 	schedule();
1777992fde7SHugh Dickins 	return 0;
1787992fde7SHugh Dickins }
1797992fde7SHugh Dickins 
180048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
181048c27fdSHugh Dickins #define LATENCY_LIMIT		256
182048c27fdSHugh Dickins 
18324b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si,
1848d69aaeeSHugh Dickins 				   unsigned char usage)
1851da177e4SLinus Torvalds {
186ebebbbe9SHugh Dickins 	unsigned long offset;
187c60aa176SHugh Dickins 	unsigned long scan_base;
1887992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
189048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
1907992fde7SHugh Dickins 	int found_free_cluster = 0;
1911da177e4SLinus Torvalds 
1927dfad418SHugh Dickins 	/*
1937dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
1947dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
1957dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
1967dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
1977dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
1987dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
1997dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
200c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
2011da177e4SLinus Torvalds 	 */
2027dfad418SHugh Dickins 
20352b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
204c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
205ebebbbe9SHugh Dickins 
206ebebbbe9SHugh Dickins 	if (unlikely(!si->cluster_nr--)) {
207ebebbbe9SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
2087dfad418SHugh Dickins 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
209ebebbbe9SHugh Dickins 			goto checks;
210ebebbbe9SHugh Dickins 		}
2117992fde7SHugh Dickins 		if (si->flags & SWP_DISCARDABLE) {
2127992fde7SHugh Dickins 			/*
2137992fde7SHugh Dickins 			 * Start range check on racing allocations, in case
2147992fde7SHugh Dickins 			 * they overlap the cluster we eventually decide on
2157992fde7SHugh Dickins 			 * (we scan without swap_lock to allow preemption).
2167992fde7SHugh Dickins 			 * It's hardly conceivable that cluster_nr could be
2177992fde7SHugh Dickins 			 * wrapped during our scan, but don't depend on it.
2187992fde7SHugh Dickins 			 */
2197992fde7SHugh Dickins 			if (si->lowest_alloc)
2207992fde7SHugh Dickins 				goto checks;
2217992fde7SHugh Dickins 			si->lowest_alloc = si->max;
2227992fde7SHugh Dickins 			si->highest_alloc = 0;
2237992fde7SHugh Dickins 		}
2245d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2257dfad418SHugh Dickins 
226c60aa176SHugh Dickins 		/*
227c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
228c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
229c60aa176SHugh Dickins 		 * But if seek is cheap, search from our current position, so
230c60aa176SHugh Dickins 		 * that swap is allocated from all over the partition: if the
231c60aa176SHugh Dickins 		 * Flash Translation Layer only remaps within limited zones,
232c60aa176SHugh Dickins 		 * we don't want to wear out the first zone too quickly.
233c60aa176SHugh Dickins 		 */
234c60aa176SHugh Dickins 		if (!(si->flags & SWP_SOLIDSTATE))
235c60aa176SHugh Dickins 			scan_base = offset = si->lowest_bit;
2367dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
2377dfad418SHugh Dickins 
2387dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
2397dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
2401da177e4SLinus Torvalds 			if (si->swap_map[offset])
2417dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
2427dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
2435d337b91SHugh Dickins 				spin_lock(&swap_lock);
244ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
245ebebbbe9SHugh Dickins 				si->cluster_next = offset;
246ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
2477992fde7SHugh Dickins 				found_free_cluster = 1;
248ebebbbe9SHugh Dickins 				goto checks;
2497dfad418SHugh Dickins 			}
250048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
251048c27fdSHugh Dickins 				cond_resched();
252048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
253048c27fdSHugh Dickins 			}
2547dfad418SHugh Dickins 		}
255ebebbbe9SHugh Dickins 
256ebebbbe9SHugh Dickins 		offset = si->lowest_bit;
257c60aa176SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
258c60aa176SHugh Dickins 
259c60aa176SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
260c60aa176SHugh Dickins 		for (; last_in_cluster < scan_base; offset++) {
261c60aa176SHugh Dickins 			if (si->swap_map[offset])
262c60aa176SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
263c60aa176SHugh Dickins 			else if (offset == last_in_cluster) {
264c60aa176SHugh Dickins 				spin_lock(&swap_lock);
265c60aa176SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
266c60aa176SHugh Dickins 				si->cluster_next = offset;
267c60aa176SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
268c60aa176SHugh Dickins 				found_free_cluster = 1;
269c60aa176SHugh Dickins 				goto checks;
270c60aa176SHugh Dickins 			}
271c60aa176SHugh Dickins 			if (unlikely(--latency_ration < 0)) {
272c60aa176SHugh Dickins 				cond_resched();
273c60aa176SHugh Dickins 				latency_ration = LATENCY_LIMIT;
274c60aa176SHugh Dickins 			}
275c60aa176SHugh Dickins 		}
276c60aa176SHugh Dickins 
277c60aa176SHugh Dickins 		offset = scan_base;
2785d337b91SHugh Dickins 		spin_lock(&swap_lock);
279ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
2807992fde7SHugh Dickins 		si->lowest_alloc = 0;
2817dfad418SHugh Dickins 	}
2827dfad418SHugh Dickins 
283ebebbbe9SHugh Dickins checks:
284ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
28552b7efdbSHugh Dickins 		goto no_page;
2867dfad418SHugh Dickins 	if (!si->highest_bit)
2877dfad418SHugh Dickins 		goto no_page;
288ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
289c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
290c9e44410SKAMEZAWA Hiroyuki 
291b73d7fceSHugh Dickins 	/* reuse swap entry of cache-only swap if not busy. */
292b73d7fceSHugh Dickins 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
293c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
294c9e44410SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
295c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
296c9e44410SKAMEZAWA Hiroyuki 		spin_lock(&swap_lock);
297c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
298c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
299c9e44410SKAMEZAWA Hiroyuki 			goto checks;
300c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
301c9e44410SKAMEZAWA Hiroyuki 	}
302c9e44410SKAMEZAWA Hiroyuki 
303ebebbbe9SHugh Dickins 	if (si->swap_map[offset])
304ebebbbe9SHugh Dickins 		goto scan;
305ebebbbe9SHugh Dickins 
30652b7efdbSHugh Dickins 	if (offset == si->lowest_bit)
3071da177e4SLinus Torvalds 		si->lowest_bit++;
3081da177e4SLinus Torvalds 	if (offset == si->highest_bit)
3091da177e4SLinus Torvalds 		si->highest_bit--;
3107dfad418SHugh Dickins 	si->inuse_pages++;
3117dfad418SHugh Dickins 	if (si->inuse_pages == si->pages) {
3121da177e4SLinus Torvalds 		si->lowest_bit = si->max;
3131da177e4SLinus Torvalds 		si->highest_bit = 0;
3141da177e4SLinus Torvalds 	}
315253d553bSHugh Dickins 	si->swap_map[offset] = usage;
3161da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
31752b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
3187992fde7SHugh Dickins 
3197992fde7SHugh Dickins 	if (si->lowest_alloc) {
3207992fde7SHugh Dickins 		/*
3217992fde7SHugh Dickins 		 * Only set when SWP_DISCARDABLE, and there's a scan
3227992fde7SHugh Dickins 		 * for a free cluster in progress or just completed.
3237992fde7SHugh Dickins 		 */
3247992fde7SHugh Dickins 		if (found_free_cluster) {
3257992fde7SHugh Dickins 			/*
3267992fde7SHugh Dickins 			 * To optimize wear-levelling, discard the
3277992fde7SHugh Dickins 			 * old data of the cluster, taking care not to
3287992fde7SHugh Dickins 			 * discard any of its pages that have already
3297992fde7SHugh Dickins 			 * been allocated by racing tasks (offset has
3307992fde7SHugh Dickins 			 * already stepped over any at the beginning).
3317992fde7SHugh Dickins 			 */
3327992fde7SHugh Dickins 			if (offset < si->highest_alloc &&
3337992fde7SHugh Dickins 			    si->lowest_alloc <= last_in_cluster)
3347992fde7SHugh Dickins 				last_in_cluster = si->lowest_alloc - 1;
3357992fde7SHugh Dickins 			si->flags |= SWP_DISCARDING;
3367992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3377992fde7SHugh Dickins 
3387992fde7SHugh Dickins 			if (offset < last_in_cluster)
3397992fde7SHugh Dickins 				discard_swap_cluster(si, offset,
3407992fde7SHugh Dickins 					last_in_cluster - offset + 1);
3417992fde7SHugh Dickins 
3427992fde7SHugh Dickins 			spin_lock(&swap_lock);
3437992fde7SHugh Dickins 			si->lowest_alloc = 0;
3447992fde7SHugh Dickins 			si->flags &= ~SWP_DISCARDING;
3457992fde7SHugh Dickins 
3467992fde7SHugh Dickins 			smp_mb();	/* wake_up_bit advises this */
3477992fde7SHugh Dickins 			wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
3487992fde7SHugh Dickins 
3497992fde7SHugh Dickins 		} else if (si->flags & SWP_DISCARDING) {
3507992fde7SHugh Dickins 			/*
3517992fde7SHugh Dickins 			 * Delay using pages allocated by racing tasks
3527992fde7SHugh Dickins 			 * until the whole discard has been issued. We
3537992fde7SHugh Dickins 			 * could defer that delay until swap_writepage,
3547992fde7SHugh Dickins 			 * but it's easier to keep this self-contained.
3557992fde7SHugh Dickins 			 */
3567992fde7SHugh Dickins 			spin_unlock(&swap_lock);
3577992fde7SHugh Dickins 			wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
3587992fde7SHugh Dickins 				wait_for_discard, TASK_UNINTERRUPTIBLE);
3597992fde7SHugh Dickins 			spin_lock(&swap_lock);
3607992fde7SHugh Dickins 		} else {
3617992fde7SHugh Dickins 			/*
3627992fde7SHugh Dickins 			 * Note pages allocated by racing tasks while
3637992fde7SHugh Dickins 			 * scan for a free cluster is in progress, so
3647992fde7SHugh Dickins 			 * that its final discard can exclude them.
3657992fde7SHugh Dickins 			 */
3667992fde7SHugh Dickins 			if (offset < si->lowest_alloc)
3677992fde7SHugh Dickins 				si->lowest_alloc = offset;
3687992fde7SHugh Dickins 			if (offset > si->highest_alloc)
3697992fde7SHugh Dickins 				si->highest_alloc = offset;
3707992fde7SHugh Dickins 		}
3717992fde7SHugh Dickins 	}
3721da177e4SLinus Torvalds 	return offset;
3737dfad418SHugh Dickins 
374ebebbbe9SHugh Dickins scan:
3755d337b91SHugh Dickins 	spin_unlock(&swap_lock);
3767dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
37752b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
3785d337b91SHugh Dickins 			spin_lock(&swap_lock);
37952b7efdbSHugh Dickins 			goto checks;
3807dfad418SHugh Dickins 		}
381c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
382c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
383c9e44410SKAMEZAWA Hiroyuki 			goto checks;
384c9e44410SKAMEZAWA Hiroyuki 		}
385048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
386048c27fdSHugh Dickins 			cond_resched();
387048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
388048c27fdSHugh Dickins 		}
38952b7efdbSHugh Dickins 	}
390c60aa176SHugh Dickins 	offset = si->lowest_bit;
391c60aa176SHugh Dickins 	while (++offset < scan_base) {
392c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
3935d337b91SHugh Dickins 			spin_lock(&swap_lock);
394ebebbbe9SHugh Dickins 			goto checks;
395c60aa176SHugh Dickins 		}
396c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
397c9e44410SKAMEZAWA Hiroyuki 			spin_lock(&swap_lock);
398c9e44410SKAMEZAWA Hiroyuki 			goto checks;
399c9e44410SKAMEZAWA Hiroyuki 		}
400c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
401c60aa176SHugh Dickins 			cond_resched();
402c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
403c60aa176SHugh Dickins 		}
404c60aa176SHugh Dickins 	}
405c60aa176SHugh Dickins 	spin_lock(&swap_lock);
4067dfad418SHugh Dickins 
4077dfad418SHugh Dickins no_page:
40852b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
4091da177e4SLinus Torvalds 	return 0;
4101da177e4SLinus Torvalds }
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
4131da177e4SLinus Torvalds {
414fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
415fb4f88dcSHugh Dickins 	pgoff_t offset;
416fb4f88dcSHugh Dickins 	int type, next;
417fb4f88dcSHugh Dickins 	int wrapped = 0;
4181da177e4SLinus Torvalds 
4195d337b91SHugh Dickins 	spin_lock(&swap_lock);
4201da177e4SLinus Torvalds 	if (nr_swap_pages <= 0)
421fb4f88dcSHugh Dickins 		goto noswap;
422fb4f88dcSHugh Dickins 	nr_swap_pages--;
4231da177e4SLinus Torvalds 
424fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
425efa90a98SHugh Dickins 		si = swap_info[type];
426fb4f88dcSHugh Dickins 		next = si->next;
427fb4f88dcSHugh Dickins 		if (next < 0 ||
428efa90a98SHugh Dickins 		    (!wrapped && si->prio != swap_info[next]->prio)) {
429fb4f88dcSHugh Dickins 			next = swap_list.head;
430fb4f88dcSHugh Dickins 			wrapped++;
4311da177e4SLinus Torvalds 		}
432fb4f88dcSHugh Dickins 
433fb4f88dcSHugh Dickins 		if (!si->highest_bit)
434fb4f88dcSHugh Dickins 			continue;
435fb4f88dcSHugh Dickins 		if (!(si->flags & SWP_WRITEOK))
436fb4f88dcSHugh Dickins 			continue;
437fb4f88dcSHugh Dickins 
438fb4f88dcSHugh Dickins 		swap_list.next = next;
439355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry for cache */
440253d553bSHugh Dickins 		offset = scan_swap_map(si, SWAP_HAS_CACHE);
4415d337b91SHugh Dickins 		if (offset) {
4425d337b91SHugh Dickins 			spin_unlock(&swap_lock);
443fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
4445d337b91SHugh Dickins 		}
445fb4f88dcSHugh Dickins 		next = swap_list.next;
446fb4f88dcSHugh Dickins 	}
447fb4f88dcSHugh Dickins 
448fb4f88dcSHugh Dickins 	nr_swap_pages++;
449fb4f88dcSHugh Dickins noswap:
4505d337b91SHugh Dickins 	spin_unlock(&swap_lock);
451fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
454910321eaSHugh Dickins /* The only caller of this function is now susupend routine */
455910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type)
456910321eaSHugh Dickins {
457910321eaSHugh Dickins 	struct swap_info_struct *si;
458910321eaSHugh Dickins 	pgoff_t offset;
459910321eaSHugh Dickins 
460910321eaSHugh Dickins 	spin_lock(&swap_lock);
461910321eaSHugh Dickins 	si = swap_info[type];
462910321eaSHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
463910321eaSHugh Dickins 		nr_swap_pages--;
464910321eaSHugh Dickins 		/* This is called for allocating swap entry, not cache */
465910321eaSHugh Dickins 		offset = scan_swap_map(si, 1);
466910321eaSHugh Dickins 		if (offset) {
467910321eaSHugh Dickins 			spin_unlock(&swap_lock);
468910321eaSHugh Dickins 			return swp_entry(type, offset);
469910321eaSHugh Dickins 		}
470910321eaSHugh Dickins 		nr_swap_pages++;
471910321eaSHugh Dickins 	}
472910321eaSHugh Dickins 	spin_unlock(&swap_lock);
473910321eaSHugh Dickins 	return (swp_entry_t) {0};
474910321eaSHugh Dickins }
475910321eaSHugh Dickins 
4761da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	struct swap_info_struct *p;
4791da177e4SLinus Torvalds 	unsigned long offset, type;
4801da177e4SLinus Torvalds 
4811da177e4SLinus Torvalds 	if (!entry.val)
4821da177e4SLinus Torvalds 		goto out;
4831da177e4SLinus Torvalds 	type = swp_type(entry);
4841da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
4851da177e4SLinus Torvalds 		goto bad_nofile;
486efa90a98SHugh Dickins 	p = swap_info[type];
4871da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
4881da177e4SLinus Torvalds 		goto bad_device;
4891da177e4SLinus Torvalds 	offset = swp_offset(entry);
4901da177e4SLinus Torvalds 	if (offset >= p->max)
4911da177e4SLinus Torvalds 		goto bad_offset;
4921da177e4SLinus Torvalds 	if (!p->swap_map[offset])
4931da177e4SLinus Torvalds 		goto bad_free;
4945d337b91SHugh Dickins 	spin_lock(&swap_lock);
4951da177e4SLinus Torvalds 	return p;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds bad_free:
4981da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
4991da177e4SLinus Torvalds 	goto out;
5001da177e4SLinus Torvalds bad_offset:
5011da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
5021da177e4SLinus Torvalds 	goto out;
5031da177e4SLinus Torvalds bad_device:
5041da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
5051da177e4SLinus Torvalds 	goto out;
5061da177e4SLinus Torvalds bad_nofile:
5071da177e4SLinus Torvalds 	printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
5081da177e4SLinus Torvalds out:
5091da177e4SLinus Torvalds 	return NULL;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5128d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p,
5138d69aaeeSHugh Dickins 				     swp_entry_t entry, unsigned char usage)
5141da177e4SLinus Torvalds {
515253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
5168d69aaeeSHugh Dickins 	unsigned char count;
5178d69aaeeSHugh Dickins 	unsigned char has_cache;
5181da177e4SLinus Torvalds 
519355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
520253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
521253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
522253d553bSHugh Dickins 
523253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
524253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
525253d553bSHugh Dickins 		has_cache = 0;
526aaa46865SHugh Dickins 	} else if (count == SWAP_MAP_SHMEM) {
527aaa46865SHugh Dickins 		/*
528aaa46865SHugh Dickins 		 * Or we could insist on shmem.c using a special
529aaa46865SHugh Dickins 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
530aaa46865SHugh Dickins 		 */
531aaa46865SHugh Dickins 		count = 0;
532570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
533570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
534570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
535570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
536570a335bSHugh Dickins 			else
537570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
538570a335bSHugh Dickins 		} else
539253d553bSHugh Dickins 			count--;
540570a335bSHugh Dickins 	}
541253d553bSHugh Dickins 
542253d553bSHugh Dickins 	if (!count)
543253d553bSHugh Dickins 		mem_cgroup_uncharge_swap(entry);
544253d553bSHugh Dickins 
545253d553bSHugh Dickins 	usage = count | has_cache;
546253d553bSHugh Dickins 	p->swap_map[offset] = usage;
547253d553bSHugh Dickins 
548355cfa73SKAMEZAWA Hiroyuki 	/* free if no reference */
549253d553bSHugh Dickins 	if (!usage) {
550b3a27d05SNitin Gupta 		struct gendisk *disk = p->bdev->bd_disk;
5511da177e4SLinus Torvalds 		if (offset < p->lowest_bit)
5521da177e4SLinus Torvalds 			p->lowest_bit = offset;
5531da177e4SLinus Torvalds 		if (offset > p->highest_bit)
5541da177e4SLinus Torvalds 			p->highest_bit = offset;
555efa90a98SHugh Dickins 		if (swap_list.next >= 0 &&
556efa90a98SHugh Dickins 		    p->prio > swap_info[swap_list.next]->prio)
557efa90a98SHugh Dickins 			swap_list.next = p->type;
5581da177e4SLinus Torvalds 		nr_swap_pages++;
5591da177e4SLinus Torvalds 		p->inuse_pages--;
560b3a27d05SNitin Gupta 		if ((p->flags & SWP_BLKDEV) &&
561b3a27d05SNitin Gupta 				disk->fops->swap_slot_free_notify)
562b3a27d05SNitin Gupta 			disk->fops->swap_slot_free_notify(p->bdev, offset);
5631da177e4SLinus Torvalds 	}
564253d553bSHugh Dickins 
565253d553bSHugh Dickins 	return usage;
5661da177e4SLinus Torvalds }
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /*
5691da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
5701da177e4SLinus Torvalds  * is still around or has not been recycled.
5711da177e4SLinus Torvalds  */
5721da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	struct swap_info_struct *p;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	p = swap_info_get(entry);
5771da177e4SLinus Torvalds 	if (p) {
578253d553bSHugh Dickins 		swap_entry_free(p, entry, 1);
5795d337b91SHugh Dickins 		spin_unlock(&swap_lock);
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds }
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds /*
584cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
585cb4b86baSKAMEZAWA Hiroyuki  */
586cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page)
587cb4b86baSKAMEZAWA Hiroyuki {
588355cfa73SKAMEZAWA Hiroyuki 	struct swap_info_struct *p;
5898d69aaeeSHugh Dickins 	unsigned char count;
590355cfa73SKAMEZAWA Hiroyuki 
591355cfa73SKAMEZAWA Hiroyuki 	p = swap_info_get(entry);
592355cfa73SKAMEZAWA Hiroyuki 	if (p) {
593253d553bSHugh Dickins 		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
594253d553bSHugh Dickins 		if (page)
595253d553bSHugh Dickins 			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
596355cfa73SKAMEZAWA Hiroyuki 		spin_unlock(&swap_lock);
597355cfa73SKAMEZAWA Hiroyuki 	}
598cb4b86baSKAMEZAWA Hiroyuki }
599cb4b86baSKAMEZAWA Hiroyuki 
600cb4b86baSKAMEZAWA Hiroyuki /*
601c475a8abSHugh Dickins  * How many references to page are currently swapped out?
602570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
603570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
6041da177e4SLinus Torvalds  */
605c475a8abSHugh Dickins static inline int page_swapcount(struct page *page)
6061da177e4SLinus Torvalds {
607c475a8abSHugh Dickins 	int count = 0;
6081da177e4SLinus Torvalds 	struct swap_info_struct *p;
6091da177e4SLinus Torvalds 	swp_entry_t entry;
6101da177e4SLinus Torvalds 
6114c21e2f2SHugh Dickins 	entry.val = page_private(page);
6121da177e4SLinus Torvalds 	p = swap_info_get(entry);
6131da177e4SLinus Torvalds 	if (p) {
614355cfa73SKAMEZAWA Hiroyuki 		count = swap_count(p->swap_map[swp_offset(entry)]);
6155d337b91SHugh Dickins 		spin_unlock(&swap_lock);
6161da177e4SLinus Torvalds 	}
617c475a8abSHugh Dickins 	return count;
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds /*
6217b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
6227b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
6237b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
6247b1fe597SHugh Dickins  * later would only waste time away from clustering.
6251da177e4SLinus Torvalds  */
6267b1fe597SHugh Dickins int reuse_swap_page(struct page *page)
6271da177e4SLinus Torvalds {
628c475a8abSHugh Dickins 	int count;
6291da177e4SLinus Torvalds 
63051726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6315ad64688SHugh Dickins 	if (unlikely(PageKsm(page)))
6325ad64688SHugh Dickins 		return 0;
633c475a8abSHugh Dickins 	count = page_mapcount(page);
6347b1fe597SHugh Dickins 	if (count <= 1 && PageSwapCache(page)) {
635c475a8abSHugh Dickins 		count += page_swapcount(page);
6367b1fe597SHugh Dickins 		if (count == 1 && !PageWriteback(page)) {
6377b1fe597SHugh Dickins 			delete_from_swap_cache(page);
6387b1fe597SHugh Dickins 			SetPageDirty(page);
6397b1fe597SHugh Dickins 		}
6407b1fe597SHugh Dickins 	}
6415ad64688SHugh Dickins 	return count <= 1;
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds /*
645a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
646a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
6471da177e4SLinus Torvalds  */
648a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
6491da177e4SLinus Torvalds {
65051726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	if (!PageSwapCache(page))
6531da177e4SLinus Torvalds 		return 0;
6541da177e4SLinus Torvalds 	if (PageWriteback(page))
6551da177e4SLinus Torvalds 		return 0;
656a2c43eedSHugh Dickins 	if (page_swapcount(page))
6571da177e4SLinus Torvalds 		return 0;
6581da177e4SLinus Torvalds 
659b73d7fceSHugh Dickins 	/*
660b73d7fceSHugh Dickins 	 * Once hibernation has begun to create its image of memory,
661b73d7fceSHugh Dickins 	 * there's a danger that one of the calls to try_to_free_swap()
662b73d7fceSHugh Dickins 	 * - most probably a call from __try_to_reclaim_swap() while
663b73d7fceSHugh Dickins 	 * hibernation is allocating its own swap pages for the image,
664b73d7fceSHugh Dickins 	 * but conceivably even a call from memory reclaim - will free
665b73d7fceSHugh Dickins 	 * the swap from a page which has already been recorded in the
666b73d7fceSHugh Dickins 	 * image as a clean swapcache page, and then reuse its swap for
667b73d7fceSHugh Dickins 	 * another page of the image.  On waking from hibernation, the
668b73d7fceSHugh Dickins 	 * original page might be freed under memory pressure, then
669b73d7fceSHugh Dickins 	 * later read back in from swap, now with the wrong data.
670b73d7fceSHugh Dickins 	 *
671b73d7fceSHugh Dickins 	 * Hibernation clears bits from gfp_allowed_mask to prevent
672b73d7fceSHugh Dickins 	 * memory reclaim from writing to disk, so check that here.
673b73d7fceSHugh Dickins 	 */
674b73d7fceSHugh Dickins 	if (!(gfp_allowed_mask & __GFP_IO))
675b73d7fceSHugh Dickins 		return 0;
676b73d7fceSHugh Dickins 
677a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
6781da177e4SLinus Torvalds 	SetPageDirty(page);
679a2c43eedSHugh Dickins 	return 1;
68068a22394SRik van Riel }
68168a22394SRik van Riel 
68268a22394SRik van Riel /*
6831da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
6841da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
6851da177e4SLinus Torvalds  */
6862509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct swap_info_struct *p;
6891da177e4SLinus Torvalds 	struct page *page = NULL;
6901da177e4SLinus Torvalds 
691a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
6922509ef26SHugh Dickins 		return 1;
6930697212aSChristoph Lameter 
6941da177e4SLinus Torvalds 	p = swap_info_get(entry);
6951da177e4SLinus Torvalds 	if (p) {
696253d553bSHugh Dickins 		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
69793fac704SNick Piggin 			page = find_get_page(&swapper_space, entry.val);
6988413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
69993fac704SNick Piggin 				page_cache_release(page);
70093fac704SNick Piggin 				page = NULL;
70193fac704SNick Piggin 			}
70293fac704SNick Piggin 		}
7035d337b91SHugh Dickins 		spin_unlock(&swap_lock);
7041da177e4SLinus Torvalds 	}
7051da177e4SLinus Torvalds 	if (page) {
706a2c43eedSHugh Dickins 		/*
707a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
708a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
709a2c43eedSHugh Dickins 		 */
71093fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
711a2c43eedSHugh Dickins 				(!page_mapped(page) || vm_swap_full())) {
7121da177e4SLinus Torvalds 			delete_from_swap_cache(page);
7131da177e4SLinus Torvalds 			SetPageDirty(page);
7141da177e4SLinus Torvalds 		}
7151da177e4SLinus Torvalds 		unlock_page(page);
7161da177e4SLinus Torvalds 		page_cache_release(page);
7171da177e4SLinus Torvalds 	}
7182509ef26SHugh Dickins 	return p != NULL;
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
72102491447SDaisuke Nishimura #ifdef CONFIG_CGROUP_MEM_RES_CTLR
72202491447SDaisuke Nishimura /**
72302491447SDaisuke Nishimura  * mem_cgroup_count_swap_user - count the user of a swap entry
72402491447SDaisuke Nishimura  * @ent: the swap entry to be checked
72502491447SDaisuke Nishimura  * @pagep: the pointer for the swap cache page of the entry to be stored
72602491447SDaisuke Nishimura  *
72702491447SDaisuke Nishimura  * Returns the number of the user of the swap entry. The number is valid only
72802491447SDaisuke Nishimura  * for swaps of anonymous pages.
72902491447SDaisuke Nishimura  * If the entry is found on swap cache, the page is stored to pagep with
73002491447SDaisuke Nishimura  * refcount of it being incremented.
73102491447SDaisuke Nishimura  */
73202491447SDaisuke Nishimura int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
73302491447SDaisuke Nishimura {
73402491447SDaisuke Nishimura 	struct page *page;
73502491447SDaisuke Nishimura 	struct swap_info_struct *p;
73602491447SDaisuke Nishimura 	int count = 0;
73702491447SDaisuke Nishimura 
73802491447SDaisuke Nishimura 	page = find_get_page(&swapper_space, ent.val);
73902491447SDaisuke Nishimura 	if (page)
74002491447SDaisuke Nishimura 		count += page_mapcount(page);
74102491447SDaisuke Nishimura 	p = swap_info_get(ent);
74202491447SDaisuke Nishimura 	if (p) {
74302491447SDaisuke Nishimura 		count += swap_count(p->swap_map[swp_offset(ent)]);
74402491447SDaisuke Nishimura 		spin_unlock(&swap_lock);
74502491447SDaisuke Nishimura 	}
74602491447SDaisuke Nishimura 
74702491447SDaisuke Nishimura 	*pagep = page;
74802491447SDaisuke Nishimura 	return count;
74902491447SDaisuke Nishimura }
75002491447SDaisuke Nishimura #endif
75102491447SDaisuke Nishimura 
752b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
753f577eb30SRafael J. Wysocki /*
754915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
755f577eb30SRafael J. Wysocki  *
756915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
757915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
758915bae9eSRafael J. Wysocki  *
759915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
760f577eb30SRafael J. Wysocki  */
7617bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
762f577eb30SRafael J. Wysocki {
763915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
764efa90a98SHugh Dickins 	int type;
765f577eb30SRafael J. Wysocki 
766915bae9eSRafael J. Wysocki 	if (device)
767915bae9eSRafael J. Wysocki 		bdev = bdget(device);
768915bae9eSRafael J. Wysocki 
769f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
770efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
771efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
772f577eb30SRafael J. Wysocki 
773915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
774f577eb30SRafael J. Wysocki 			continue;
775b6b5bce3SRafael J. Wysocki 
776915bae9eSRafael J. Wysocki 		if (!bdev) {
7777bf23687SRafael J. Wysocki 			if (bdev_p)
778dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
7797bf23687SRafael J. Wysocki 
7806e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
781efa90a98SHugh Dickins 			return type;
7826e1819d6SRafael J. Wysocki 		}
783915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
7849625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
785915bae9eSRafael J. Wysocki 
786915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
7877bf23687SRafael J. Wysocki 				if (bdev_p)
788dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
7897bf23687SRafael J. Wysocki 
790f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
791915bae9eSRafael J. Wysocki 				bdput(bdev);
792efa90a98SHugh Dickins 				return type;
793f577eb30SRafael J. Wysocki 			}
794f577eb30SRafael J. Wysocki 		}
795915bae9eSRafael J. Wysocki 	}
796f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
797915bae9eSRafael J. Wysocki 	if (bdev)
798915bae9eSRafael J. Wysocki 		bdput(bdev);
799915bae9eSRafael J. Wysocki 
800f577eb30SRafael J. Wysocki 	return -ENODEV;
801f577eb30SRafael J. Wysocki }
802f577eb30SRafael J. Wysocki 
803f577eb30SRafael J. Wysocki /*
80473c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
80573c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
80673c34b6aSHugh Dickins  */
80773c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
80873c34b6aSHugh Dickins {
80973c34b6aSHugh Dickins 	struct block_device *bdev;
81073c34b6aSHugh Dickins 
81173c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
81273c34b6aSHugh Dickins 		return 0;
81373c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
81473c34b6aSHugh Dickins 		return 0;
815d4906e1aSLee Schermerhorn 	return map_swap_entry(swp_entry(type, offset), &bdev);
81673c34b6aSHugh Dickins }
81773c34b6aSHugh Dickins 
81873c34b6aSHugh Dickins /*
819f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
820f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
821f577eb30SRafael J. Wysocki  *
822f577eb30SRafael J. Wysocki  * This is needed for software suspend
823f577eb30SRafael J. Wysocki  */
824f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
825f577eb30SRafael J. Wysocki {
826f577eb30SRafael J. Wysocki 	unsigned int n = 0;
827f577eb30SRafael J. Wysocki 
828f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
829efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
830efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
831efa90a98SHugh Dickins 
832efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
833efa90a98SHugh Dickins 			n = sis->pages;
834f577eb30SRafael J. Wysocki 			if (free)
835efa90a98SHugh Dickins 				n -= sis->inuse_pages;
836efa90a98SHugh Dickins 		}
837f577eb30SRafael J. Wysocki 	}
838f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
839f577eb30SRafael J. Wysocki 	return n;
840f577eb30SRafael J. Wysocki }
84173c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
842f577eb30SRafael J. Wysocki 
8431da177e4SLinus Torvalds /*
84472866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
84572866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
84672866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
8471da177e4SLinus Torvalds  */
848044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
8491da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
8501da177e4SLinus Torvalds {
85156039efaSKAMEZAWA Hiroyuki 	struct mem_cgroup *ptr;
852044d66c1SHugh Dickins 	spinlock_t *ptl;
853044d66c1SHugh Dickins 	pte_t *pte;
854044d66c1SHugh Dickins 	int ret = 1;
855044d66c1SHugh Dickins 
85685d9fc89SKAMEZAWA Hiroyuki 	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
857044d66c1SHugh Dickins 		ret = -ENOMEM;
85885d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
85985d9fc89SKAMEZAWA Hiroyuki 	}
860044d66c1SHugh Dickins 
861044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
862044d66c1SHugh Dickins 	if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
863044d66c1SHugh Dickins 		if (ret > 0)
8647a81b88cSKAMEZAWA Hiroyuki 			mem_cgroup_cancel_charge_swapin(ptr);
865044d66c1SHugh Dickins 		ret = 0;
866044d66c1SHugh Dickins 		goto out;
867044d66c1SHugh Dickins 	}
8688a9f3ccdSBalbir Singh 
869b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
870d559db08SKAMEZAWA Hiroyuki 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
8711da177e4SLinus Torvalds 	get_page(page);
8721da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
8731da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
8741da177e4SLinus Torvalds 	page_add_anon_rmap(page, vma, addr);
8757a81b88cSKAMEZAWA Hiroyuki 	mem_cgroup_commit_charge_swapin(page, ptr);
8761da177e4SLinus Torvalds 	swap_free(entry);
8771da177e4SLinus Torvalds 	/*
8781da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
8791da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
8801da177e4SLinus Torvalds 	 */
8811da177e4SLinus Torvalds 	activate_page(page);
882044d66c1SHugh Dickins out:
883044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
88485d9fc89SKAMEZAWA Hiroyuki out_nolock:
885044d66c1SHugh Dickins 	return ret;
8861da177e4SLinus Torvalds }
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
8891da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
8901da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
8911da177e4SLinus Torvalds {
8921da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
893705e87c0SHugh Dickins 	pte_t *pte;
8948a9f3ccdSBalbir Singh 	int ret = 0;
8951da177e4SLinus Torvalds 
896044d66c1SHugh Dickins 	/*
897044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
898044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
899044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
900044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
901044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
902044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
903044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
904044d66c1SHugh Dickins 	 */
905044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
9061da177e4SLinus Torvalds 	do {
9071da177e4SLinus Torvalds 		/*
9081da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
9091da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
9101da177e4SLinus Torvalds 		 */
9111da177e4SLinus Torvalds 		if (unlikely(pte_same(*pte, swp_pte))) {
912044d66c1SHugh Dickins 			pte_unmap(pte);
913044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
914044d66c1SHugh Dickins 			if (ret)
915044d66c1SHugh Dickins 				goto out;
916044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
9171da177e4SLinus Torvalds 		}
9181da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
919044d66c1SHugh Dickins 	pte_unmap(pte - 1);
920044d66c1SHugh Dickins out:
9218a9f3ccdSBalbir Singh 	return ret;
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
9251da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9261da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9271da177e4SLinus Torvalds {
9281da177e4SLinus Torvalds 	pmd_t *pmd;
9291da177e4SLinus Torvalds 	unsigned long next;
9308a9f3ccdSBalbir Singh 	int ret;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
9331da177e4SLinus Torvalds 	do {
9341da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
9353f04f62fSAndrea Arcangeli 		if (unlikely(pmd_trans_huge(*pmd)))
9363f04f62fSAndrea Arcangeli 			continue;
9371da177e4SLinus Torvalds 		if (pmd_none_or_clear_bad(pmd))
9381da177e4SLinus Torvalds 			continue;
9398a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
9408a9f3ccdSBalbir Singh 		if (ret)
9418a9f3ccdSBalbir Singh 			return ret;
9421da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
9431da177e4SLinus Torvalds 	return 0;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds 
9461da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
9471da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
9481da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	pud_t *pud;
9511da177e4SLinus Torvalds 	unsigned long next;
9528a9f3ccdSBalbir Singh 	int ret;
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
9551da177e4SLinus Torvalds 	do {
9561da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
9571da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
9581da177e4SLinus Torvalds 			continue;
9598a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
9608a9f3ccdSBalbir Singh 		if (ret)
9618a9f3ccdSBalbir Singh 			return ret;
9621da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
9631da177e4SLinus Torvalds 	return 0;
9641da177e4SLinus Torvalds }
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
9671da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9681da177e4SLinus Torvalds {
9691da177e4SLinus Torvalds 	pgd_t *pgd;
9701da177e4SLinus Torvalds 	unsigned long addr, end, next;
9718a9f3ccdSBalbir Singh 	int ret;
9721da177e4SLinus Torvalds 
9733ca7b3c5SHugh Dickins 	if (page_anon_vma(page)) {
9741da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
9751da177e4SLinus Torvalds 		if (addr == -EFAULT)
9761da177e4SLinus Torvalds 			return 0;
9771da177e4SLinus Torvalds 		else
9781da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
9791da177e4SLinus Torvalds 	} else {
9801da177e4SLinus Torvalds 		addr = vma->vm_start;
9811da177e4SLinus Torvalds 		end = vma->vm_end;
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds 
9841da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
9851da177e4SLinus Torvalds 	do {
9861da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
9871da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
9881da177e4SLinus Torvalds 			continue;
9898a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
9908a9f3ccdSBalbir Singh 		if (ret)
9918a9f3ccdSBalbir Singh 			return ret;
9921da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
9931da177e4SLinus Torvalds 	return 0;
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
9971da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
9981da177e4SLinus Torvalds {
9991da177e4SLinus Torvalds 	struct vm_area_struct *vma;
10008a9f3ccdSBalbir Singh 	int ret = 0;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
10031da177e4SLinus Torvalds 		/*
10047d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
10057d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
10061da177e4SLinus Torvalds 		 */
1007c475a8abSHugh Dickins 		activate_page(page);
10081da177e4SLinus Torvalds 		unlock_page(page);
10091da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
10101da177e4SLinus Torvalds 		lock_page(page);
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
10138a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
10141da177e4SLinus Torvalds 			break;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
10178a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds /*
10211da177e4SLinus Torvalds  * Scan swap_map from current position to next entry still in use.
10221da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
10231da177e4SLinus Torvalds  */
10246eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
10256eb396dcSHugh Dickins 					unsigned int prev)
10261da177e4SLinus Torvalds {
10276eb396dcSHugh Dickins 	unsigned int max = si->max;
10286eb396dcSHugh Dickins 	unsigned int i = prev;
10298d69aaeeSHugh Dickins 	unsigned char count;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	/*
10325d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
10331da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
10341da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
10355d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
10361da177e4SLinus Torvalds 	 */
10371da177e4SLinus Torvalds 	for (;;) {
10381da177e4SLinus Torvalds 		if (++i >= max) {
10391da177e4SLinus Torvalds 			if (!prev) {
10401da177e4SLinus Torvalds 				i = 0;
10411da177e4SLinus Torvalds 				break;
10421da177e4SLinus Torvalds 			}
10431da177e4SLinus Torvalds 			/*
10441da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
10451da177e4SLinus Torvalds 			 * loop back to start and recheck there.
10461da177e4SLinus Torvalds 			 */
10471da177e4SLinus Torvalds 			max = prev + 1;
10481da177e4SLinus Torvalds 			prev = 0;
10491da177e4SLinus Torvalds 			i = 1;
10501da177e4SLinus Torvalds 		}
10511da177e4SLinus Torvalds 		count = si->swap_map[i];
1052355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
10531da177e4SLinus Torvalds 			break;
10541da177e4SLinus Torvalds 	}
10551da177e4SLinus Torvalds 	return i;
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds /*
10591da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
10601da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
10611da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
10621da177e4SLinus Torvalds  */
10631da177e4SLinus Torvalds static int try_to_unuse(unsigned int type)
10641da177e4SLinus Torvalds {
1065efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
10661da177e4SLinus Torvalds 	struct mm_struct *start_mm;
10678d69aaeeSHugh Dickins 	unsigned char *swap_map;
10688d69aaeeSHugh Dickins 	unsigned char swcount;
10691da177e4SLinus Torvalds 	struct page *page;
10701da177e4SLinus Torvalds 	swp_entry_t entry;
10716eb396dcSHugh Dickins 	unsigned int i = 0;
10721da177e4SLinus Torvalds 	int retval = 0;
10731da177e4SLinus Torvalds 
10741da177e4SLinus Torvalds 	/*
10751da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
10761da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
10771da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
10781da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
10791da177e4SLinus Torvalds 	 *
10801da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
10811da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
10821da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
10831da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
10841da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
10851da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
1086570a335bSHugh Dickins 	 * that.
10871da177e4SLinus Torvalds 	 */
10881da177e4SLinus Torvalds 	start_mm = &init_mm;
10891da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 	/*
10921da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
10931da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
10941da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
10951da177e4SLinus Torvalds 	 */
10961da177e4SLinus Torvalds 	while ((i = find_next_to_unuse(si, i)) != 0) {
10971da177e4SLinus Torvalds 		if (signal_pending(current)) {
10981da177e4SLinus Torvalds 			retval = -EINTR;
10991da177e4SLinus Torvalds 			break;
11001da177e4SLinus Torvalds 		}
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 		/*
11031da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
11041da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
11051da177e4SLinus Torvalds 		 * page and read the swap into it.
11061da177e4SLinus Torvalds 		 */
11071da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
11081da177e4SLinus Torvalds 		entry = swp_entry(type, i);
110902098feaSHugh Dickins 		page = read_swap_cache_async(entry,
111002098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
11111da177e4SLinus Torvalds 		if (!page) {
11121da177e4SLinus Torvalds 			/*
11131da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
11141da177e4SLinus Torvalds 			 * has been freed independently, and will not be
11151da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
11161da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
11171da177e4SLinus Torvalds 			 */
11181da177e4SLinus Torvalds 			if (!*swap_map)
11191da177e4SLinus Torvalds 				continue;
11201da177e4SLinus Torvalds 			retval = -ENOMEM;
11211da177e4SLinus Torvalds 			break;
11221da177e4SLinus Torvalds 		}
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds 		/*
11251da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
11261da177e4SLinus Torvalds 		 */
11271da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
11281da177e4SLinus Torvalds 			mmput(start_mm);
11291da177e4SLinus Torvalds 			start_mm = &init_mm;
11301da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
11311da177e4SLinus Torvalds 		}
11321da177e4SLinus Torvalds 
11331da177e4SLinus Torvalds 		/*
11341da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
11351da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
11361da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
11371da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
11381da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
11391da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
11401da177e4SLinus Torvalds 		 */
11411da177e4SLinus Torvalds 		wait_on_page_locked(page);
11421da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11431da177e4SLinus Torvalds 		lock_page(page);
11441da177e4SLinus Torvalds 		wait_on_page_writeback(page);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 		/*
11471da177e4SLinus Torvalds 		 * Remove all references to entry.
11481da177e4SLinus Torvalds 		 */
11491da177e4SLinus Torvalds 		swcount = *swap_map;
1150aaa46865SHugh Dickins 		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1151aaa46865SHugh Dickins 			retval = shmem_unuse(entry, page);
1152aaa46865SHugh Dickins 			/* page has already been unlocked and released */
1153aaa46865SHugh Dickins 			if (retval < 0)
1154aaa46865SHugh Dickins 				break;
1155aaa46865SHugh Dickins 			continue;
11561da177e4SLinus Torvalds 		}
1157aaa46865SHugh Dickins 		if (swap_count(swcount) && start_mm != &init_mm)
1158aaa46865SHugh Dickins 			retval = unuse_mm(start_mm, entry, page);
1159aaa46865SHugh Dickins 
1160355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
11611da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
11621da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
11631da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
11641da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
11651da177e4SLinus Torvalds 			struct mm_struct *mm;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
11681da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
11691da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
1170aaa46865SHugh Dickins 			while (swap_count(*swap_map) && !retval &&
11711da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
11721da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
117370af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
11741da177e4SLinus Torvalds 					continue;
11751da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
11761da177e4SLinus Torvalds 				mmput(prev_mm);
11771da177e4SLinus Torvalds 				prev_mm = mm;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 				cond_resched();
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 				swcount = *swap_map;
1182355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
11831da177e4SLinus Torvalds 					;
1184aaa46865SHugh Dickins 				else if (mm == &init_mm)
11851da177e4SLinus Torvalds 					set_start_mm = 1;
1186aaa46865SHugh Dickins 				else
11871da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
1188355cfa73SKAMEZAWA Hiroyuki 
118932c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
11901da177e4SLinus Torvalds 					mmput(new_start_mm);
11911da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
11921da177e4SLinus Torvalds 					new_start_mm = mm;
11931da177e4SLinus Torvalds 					set_start_mm = 0;
11941da177e4SLinus Torvalds 				}
11951da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
11961da177e4SLinus Torvalds 			}
11971da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
11981da177e4SLinus Torvalds 			mmput(prev_mm);
11991da177e4SLinus Torvalds 			mmput(start_mm);
12001da177e4SLinus Torvalds 			start_mm = new_start_mm;
12011da177e4SLinus Torvalds 		}
12021da177e4SLinus Torvalds 		if (retval) {
12031da177e4SLinus Torvalds 			unlock_page(page);
12041da177e4SLinus Torvalds 			page_cache_release(page);
12051da177e4SLinus Torvalds 			break;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 		/*
12091da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
12101da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
12111da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
12121da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
12131da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
12141da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
12151da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
12161da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
12171da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
12181da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
12191da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
12205ad64688SHugh Dickins 		 *
12215ad64688SHugh Dickins 		 * Given how unuse_vma() targets one particular offset
12225ad64688SHugh Dickins 		 * in an anon_vma, once the anon_vma has been determined,
12235ad64688SHugh Dickins 		 * this splitting happens to be just what is needed to
12245ad64688SHugh Dickins 		 * handle where KSM pages have been swapped out: re-reading
12255ad64688SHugh Dickins 		 * is unnecessarily slow, but we can fix that later on.
12261da177e4SLinus Torvalds 		 */
1227355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
1228355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
12291da177e4SLinus Torvalds 			struct writeback_control wbc = {
12301da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
12311da177e4SLinus Torvalds 			};
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
12341da177e4SLinus Torvalds 			lock_page(page);
12351da177e4SLinus Torvalds 			wait_on_page_writeback(page);
12361da177e4SLinus Torvalds 		}
123768bdc8d6SHugh Dickins 
123868bdc8d6SHugh Dickins 		/*
123968bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
124068bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
124168bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
124268bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
124368bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
124468bdc8d6SHugh Dickins 		 */
124568bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
124668bdc8d6SHugh Dickins 		    likely(page_private(page) == entry.val))
12471da177e4SLinus Torvalds 			delete_from_swap_cache(page);
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 		/*
12501da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
12511da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
12522706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
12531da177e4SLinus Torvalds 		 */
12541da177e4SLinus Torvalds 		SetPageDirty(page);
12551da177e4SLinus Torvalds 		unlock_page(page);
12561da177e4SLinus Torvalds 		page_cache_release(page);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		/*
12591da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
12601da177e4SLinus Torvalds 		 * interactive performance.
12611da177e4SLinus Torvalds 		 */
12621da177e4SLinus Torvalds 		cond_resched();
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	mmput(start_mm);
12661da177e4SLinus Torvalds 	return retval;
12671da177e4SLinus Torvalds }
12681da177e4SLinus Torvalds 
12691da177e4SLinus Torvalds /*
12705d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
12715d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
12725d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
12731da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
12741da177e4SLinus Torvalds  */
12751da177e4SLinus Torvalds static void drain_mmlist(void)
12761da177e4SLinus Torvalds {
12771da177e4SLinus Torvalds 	struct list_head *p, *next;
1278efa90a98SHugh Dickins 	unsigned int type;
12791da177e4SLinus Torvalds 
1280efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
1281efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
12821da177e4SLinus Torvalds 			return;
12831da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
12841da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
12851da177e4SLinus Torvalds 		list_del_init(p);
12861da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds /*
12901da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1291d4906e1aSLee Schermerhorn  * corresponds to page offset for the specified swap entry.
1292d4906e1aSLee Schermerhorn  * Note that the type of this function is sector_t, but it returns page offset
1293d4906e1aSLee Schermerhorn  * into the bdev, not sector offset.
12941da177e4SLinus Torvalds  */
1295d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
12961da177e4SLinus Torvalds {
1297f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
1298f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
1299f29ad6a9SHugh Dickins 	struct swap_extent *se;
1300f29ad6a9SHugh Dickins 	pgoff_t offset;
1301f29ad6a9SHugh Dickins 
1302efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
1303f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
1304f29ad6a9SHugh Dickins 
1305f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
1306f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
1307f29ad6a9SHugh Dickins 	se = start_se;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	for ( ; ; ) {
13101da177e4SLinus Torvalds 		struct list_head *lh;
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 		if (se->start_page <= offset &&
13131da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
13141da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
13151da177e4SLinus Torvalds 		}
131611d31886SHugh Dickins 		lh = se->list.next;
13171da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
13181da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
13191da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
13201da177e4SLinus Torvalds 	}
13211da177e4SLinus Torvalds }
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds /*
1324d4906e1aSLee Schermerhorn  * Returns the page offset into bdev for the specified page's swap entry.
1325d4906e1aSLee Schermerhorn  */
1326d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev)
1327d4906e1aSLee Schermerhorn {
1328d4906e1aSLee Schermerhorn 	swp_entry_t entry;
1329d4906e1aSLee Schermerhorn 	entry.val = page_private(page);
1330d4906e1aSLee Schermerhorn 	return map_swap_entry(entry, bdev);
1331d4906e1aSLee Schermerhorn }
1332d4906e1aSLee Schermerhorn 
1333d4906e1aSLee Schermerhorn /*
13341da177e4SLinus Torvalds  * Free all of a swapdev's extent information
13351da177e4SLinus Torvalds  */
13361da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
13371da177e4SLinus Torvalds {
13389625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
13391da177e4SLinus Torvalds 		struct swap_extent *se;
13401da177e4SLinus Torvalds 
13419625a5f2SHugh Dickins 		se = list_entry(sis->first_swap_extent.list.next,
13421da177e4SLinus Torvalds 				struct swap_extent, list);
13431da177e4SLinus Torvalds 		list_del(&se->list);
13441da177e4SLinus Torvalds 		kfree(se);
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds }
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds /*
13491da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
135011d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
13511da177e4SLinus Torvalds  *
135211d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
13531da177e4SLinus Torvalds  */
13541da177e4SLinus Torvalds static int
13551da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
13561da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
13571da177e4SLinus Torvalds {
13581da177e4SLinus Torvalds 	struct swap_extent *se;
13591da177e4SLinus Torvalds 	struct swap_extent *new_se;
13601da177e4SLinus Torvalds 	struct list_head *lh;
13611da177e4SLinus Torvalds 
13629625a5f2SHugh Dickins 	if (start_page == 0) {
13639625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
13649625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
13659625a5f2SHugh Dickins 		se->start_page = 0;
13669625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
13679625a5f2SHugh Dickins 		se->start_block = start_block;
13689625a5f2SHugh Dickins 		return 1;
13699625a5f2SHugh Dickins 	} else {
13709625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
13711da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
137211d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
137311d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
13741da177e4SLinus Torvalds 			/* Merge it */
13751da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
13761da177e4SLinus Torvalds 			return 0;
13771da177e4SLinus Torvalds 		}
13781da177e4SLinus Torvalds 	}
13791da177e4SLinus Torvalds 
13801da177e4SLinus Torvalds 	/*
13811da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
13821da177e4SLinus Torvalds 	 */
13831da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
13841da177e4SLinus Torvalds 	if (new_se == NULL)
13851da177e4SLinus Torvalds 		return -ENOMEM;
13861da177e4SLinus Torvalds 	new_se->start_page = start_page;
13871da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
13881da177e4SLinus Torvalds 	new_se->start_block = start_block;
13891da177e4SLinus Torvalds 
13909625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
139153092a74SHugh Dickins 	return 1;
13921da177e4SLinus Torvalds }
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds /*
13951da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
13961da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
13971da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
13981da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
13991da177e4SLinus Torvalds  *
14001da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
14011da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
14021da177e4SLinus Torvalds  * swap files identically.
14031da177e4SLinus Torvalds  *
14041da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
14051da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
14061da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
14071da177e4SLinus Torvalds  *
14081da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
14091da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
14101da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
14111da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
14121da177e4SLinus Torvalds  * for swapping.
14131da177e4SLinus Torvalds  *
1414b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
14151da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
14161da177e4SLinus Torvalds  * which will scribble on the fs.
14171da177e4SLinus Torvalds  *
14181da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
14191da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
14201da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
14211da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
14221da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
14231da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
14241da177e4SLinus Torvalds  */
142553092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
14261da177e4SLinus Torvalds {
14271da177e4SLinus Torvalds 	struct inode *inode;
14281da177e4SLinus Torvalds 	unsigned blocks_per_page;
14291da177e4SLinus Torvalds 	unsigned long page_no;
14301da177e4SLinus Torvalds 	unsigned blkbits;
14311da177e4SLinus Torvalds 	sector_t probe_block;
14321da177e4SLinus Torvalds 	sector_t last_block;
143353092a74SHugh Dickins 	sector_t lowest_block = -1;
143453092a74SHugh Dickins 	sector_t highest_block = 0;
143553092a74SHugh Dickins 	int nr_extents = 0;
14361da177e4SLinus Torvalds 	int ret;
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	inode = sis->swap_file->f_mapping->host;
14391da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
14401da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
144153092a74SHugh Dickins 		*span = sis->pages;
14429625a5f2SHugh Dickins 		goto out;
14431da177e4SLinus Torvalds 	}
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds 	blkbits = inode->i_blkbits;
14461da177e4SLinus Torvalds 	blocks_per_page = PAGE_SIZE >> blkbits;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 	/*
14491da177e4SLinus Torvalds 	 * Map all the blocks into the extent list.  This code doesn't try
14501da177e4SLinus Torvalds 	 * to be very smart.
14511da177e4SLinus Torvalds 	 */
14521da177e4SLinus Torvalds 	probe_block = 0;
14531da177e4SLinus Torvalds 	page_no = 0;
14541da177e4SLinus Torvalds 	last_block = i_size_read(inode) >> blkbits;
14551da177e4SLinus Torvalds 	while ((probe_block + blocks_per_page) <= last_block &&
14561da177e4SLinus Torvalds 			page_no < sis->max) {
14571da177e4SLinus Torvalds 		unsigned block_in_page;
14581da177e4SLinus Torvalds 		sector_t first_block;
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds 		first_block = bmap(inode, probe_block);
14611da177e4SLinus Torvalds 		if (first_block == 0)
14621da177e4SLinus Torvalds 			goto bad_bmap;
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 		/*
14651da177e4SLinus Torvalds 		 * It must be PAGE_SIZE aligned on-disk
14661da177e4SLinus Torvalds 		 */
14671da177e4SLinus Torvalds 		if (first_block & (blocks_per_page - 1)) {
14681da177e4SLinus Torvalds 			probe_block++;
14691da177e4SLinus Torvalds 			goto reprobe;
14701da177e4SLinus Torvalds 		}
14711da177e4SLinus Torvalds 
14721da177e4SLinus Torvalds 		for (block_in_page = 1; block_in_page < blocks_per_page;
14731da177e4SLinus Torvalds 					block_in_page++) {
14741da177e4SLinus Torvalds 			sector_t block;
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds 			block = bmap(inode, probe_block + block_in_page);
14771da177e4SLinus Torvalds 			if (block == 0)
14781da177e4SLinus Torvalds 				goto bad_bmap;
14791da177e4SLinus Torvalds 			if (block != first_block + block_in_page) {
14801da177e4SLinus Torvalds 				/* Discontiguity */
14811da177e4SLinus Torvalds 				probe_block++;
14821da177e4SLinus Torvalds 				goto reprobe;
14831da177e4SLinus Torvalds 			}
14841da177e4SLinus Torvalds 		}
14851da177e4SLinus Torvalds 
148653092a74SHugh Dickins 		first_block >>= (PAGE_SHIFT - blkbits);
148753092a74SHugh Dickins 		if (page_no) {	/* exclude the header page */
148853092a74SHugh Dickins 			if (first_block < lowest_block)
148953092a74SHugh Dickins 				lowest_block = first_block;
149053092a74SHugh Dickins 			if (first_block > highest_block)
149153092a74SHugh Dickins 				highest_block = first_block;
149253092a74SHugh Dickins 		}
149353092a74SHugh Dickins 
14941da177e4SLinus Torvalds 		/*
14951da177e4SLinus Torvalds 		 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
14961da177e4SLinus Torvalds 		 */
149753092a74SHugh Dickins 		ret = add_swap_extent(sis, page_no, 1, first_block);
149853092a74SHugh Dickins 		if (ret < 0)
14991da177e4SLinus Torvalds 			goto out;
150053092a74SHugh Dickins 		nr_extents += ret;
15011da177e4SLinus Torvalds 		page_no++;
15021da177e4SLinus Torvalds 		probe_block += blocks_per_page;
15031da177e4SLinus Torvalds reprobe:
15041da177e4SLinus Torvalds 		continue;
15051da177e4SLinus Torvalds 	}
150653092a74SHugh Dickins 	ret = nr_extents;
150753092a74SHugh Dickins 	*span = 1 + highest_block - lowest_block;
15081da177e4SLinus Torvalds 	if (page_no == 0)
1509e2244ec2SHugh Dickins 		page_no = 1;	/* force Empty message */
15101da177e4SLinus Torvalds 	sis->max = page_no;
1511e2244ec2SHugh Dickins 	sis->pages = page_no - 1;
15121da177e4SLinus Torvalds 	sis->highest_bit = page_no - 1;
15139625a5f2SHugh Dickins out:
15149625a5f2SHugh Dickins 	return ret;
15151da177e4SLinus Torvalds bad_bmap:
15161da177e4SLinus Torvalds 	printk(KERN_ERR "swapon: swapfile has holes\n");
15171da177e4SLinus Torvalds 	ret = -EINVAL;
15189625a5f2SHugh Dickins 	goto out;
15191da177e4SLinus Torvalds }
15201da177e4SLinus Torvalds 
152140531542SCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio,
152240531542SCesar Eduardo Barros 				unsigned char *swap_map)
152340531542SCesar Eduardo Barros {
152440531542SCesar Eduardo Barros 	int i, prev;
152540531542SCesar Eduardo Barros 
152640531542SCesar Eduardo Barros 	spin_lock(&swap_lock);
152740531542SCesar Eduardo Barros 	if (prio >= 0)
152840531542SCesar Eduardo Barros 		p->prio = prio;
152940531542SCesar Eduardo Barros 	else
153040531542SCesar Eduardo Barros 		p->prio = --least_priority;
153140531542SCesar Eduardo Barros 	p->swap_map = swap_map;
153240531542SCesar Eduardo Barros 	p->flags |= SWP_WRITEOK;
153340531542SCesar Eduardo Barros 	nr_swap_pages += p->pages;
153440531542SCesar Eduardo Barros 	total_swap_pages += p->pages;
153540531542SCesar Eduardo Barros 
153640531542SCesar Eduardo Barros 	/* insert swap space into swap_list: */
153740531542SCesar Eduardo Barros 	prev = -1;
153840531542SCesar Eduardo Barros 	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
153940531542SCesar Eduardo Barros 		if (p->prio >= swap_info[i]->prio)
154040531542SCesar Eduardo Barros 			break;
154140531542SCesar Eduardo Barros 		prev = i;
154240531542SCesar Eduardo Barros 	}
154340531542SCesar Eduardo Barros 	p->next = i;
154440531542SCesar Eduardo Barros 	if (prev < 0)
154540531542SCesar Eduardo Barros 		swap_list.head = swap_list.next = p->type;
154640531542SCesar Eduardo Barros 	else
154740531542SCesar Eduardo Barros 		swap_info[prev]->next = p->type;
154840531542SCesar Eduardo Barros 	spin_unlock(&swap_lock);
154940531542SCesar Eduardo Barros }
155040531542SCesar Eduardo Barros 
1551c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
15521da177e4SLinus Torvalds {
15531da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
15548d69aaeeSHugh Dickins 	unsigned char *swap_map;
15551da177e4SLinus Torvalds 	struct file *swap_file, *victim;
15561da177e4SLinus Torvalds 	struct address_space *mapping;
15571da177e4SLinus Torvalds 	struct inode *inode;
15581da177e4SLinus Torvalds 	char *pathname;
1559*72788c38SDavid Rientjes 	int oom_score_adj;
15601da177e4SLinus Torvalds 	int i, type, prev;
15611da177e4SLinus Torvalds 	int err;
15621da177e4SLinus Torvalds 
15631da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
15641da177e4SLinus Torvalds 		return -EPERM;
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	pathname = getname(specialfile);
15671da177e4SLinus Torvalds 	err = PTR_ERR(pathname);
15681da177e4SLinus Torvalds 	if (IS_ERR(pathname))
15691da177e4SLinus Torvalds 		goto out;
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
15721da177e4SLinus Torvalds 	putname(pathname);
15731da177e4SLinus Torvalds 	err = PTR_ERR(victim);
15741da177e4SLinus Torvalds 	if (IS_ERR(victim))
15751da177e4SLinus Torvalds 		goto out;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	mapping = victim->f_mapping;
15781da177e4SLinus Torvalds 	prev = -1;
15795d337b91SHugh Dickins 	spin_lock(&swap_lock);
1580efa90a98SHugh Dickins 	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1581efa90a98SHugh Dickins 		p = swap_info[type];
158222c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
15831da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
15841da177e4SLinus Torvalds 				break;
15851da177e4SLinus Torvalds 		}
15861da177e4SLinus Torvalds 		prev = type;
15871da177e4SLinus Torvalds 	}
15881da177e4SLinus Torvalds 	if (type < 0) {
15891da177e4SLinus Torvalds 		err = -EINVAL;
15905d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15911da177e4SLinus Torvalds 		goto out_dput;
15921da177e4SLinus Torvalds 	}
15931da177e4SLinus Torvalds 	if (!security_vm_enough_memory(p->pages))
15941da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
15951da177e4SLinus Torvalds 	else {
15961da177e4SLinus Torvalds 		err = -ENOMEM;
15975d337b91SHugh Dickins 		spin_unlock(&swap_lock);
15981da177e4SLinus Torvalds 		goto out_dput;
15991da177e4SLinus Torvalds 	}
1600efa90a98SHugh Dickins 	if (prev < 0)
16011da177e4SLinus Torvalds 		swap_list.head = p->next;
1602efa90a98SHugh Dickins 	else
1603efa90a98SHugh Dickins 		swap_info[prev]->next = p->next;
16041da177e4SLinus Torvalds 	if (type == swap_list.next) {
16051da177e4SLinus Torvalds 		/* just pick something that's safe... */
16061da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
16071da177e4SLinus Torvalds 	}
160878ecba08SHugh Dickins 	if (p->prio < 0) {
1609efa90a98SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i]->next)
1610efa90a98SHugh Dickins 			swap_info[i]->prio = p->prio--;
161178ecba08SHugh Dickins 		least_priority++;
161278ecba08SHugh Dickins 	}
16131da177e4SLinus Torvalds 	nr_swap_pages -= p->pages;
16141da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
16151da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
16165d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1617fb4f88dcSHugh Dickins 
1618*72788c38SDavid Rientjes 	oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
16191da177e4SLinus Torvalds 	err = try_to_unuse(type);
1620*72788c38SDavid Rientjes 	test_set_oom_score_adj(oom_score_adj);
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds 	if (err) {
162340531542SCesar Eduardo Barros 		/*
162440531542SCesar Eduardo Barros 		 * reading p->prio and p->swap_map outside the lock is
162540531542SCesar Eduardo Barros 		 * safe here because only sys_swapon and sys_swapoff
162640531542SCesar Eduardo Barros 		 * change them, and there can be no other sys_swapon or
162740531542SCesar Eduardo Barros 		 * sys_swapoff for this swap_info_struct at this point.
162840531542SCesar Eduardo Barros 		 */
16291da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
163040531542SCesar Eduardo Barros 		enable_swap_info(p, p->prio, p->swap_map);
16311da177e4SLinus Torvalds 		goto out_dput;
16321da177e4SLinus Torvalds 	}
163352b7efdbSHugh Dickins 
16344cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1635570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
1636570a335bSHugh Dickins 		free_swap_count_continuations(p);
1637570a335bSHugh Dickins 
1638fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
16395d337b91SHugh Dickins 	spin_lock(&swap_lock);
16401da177e4SLinus Torvalds 	drain_mmlist();
16415d337b91SHugh Dickins 
16425d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
16435d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
16445d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
16455d337b91SHugh Dickins 		spin_unlock(&swap_lock);
164613e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
16475d337b91SHugh Dickins 		spin_lock(&swap_lock);
16485d337b91SHugh Dickins 	}
16495d337b91SHugh Dickins 
16501da177e4SLinus Torvalds 	swap_file = p->swap_file;
16511da177e4SLinus Torvalds 	p->swap_file = NULL;
16521da177e4SLinus Torvalds 	p->max = 0;
16531da177e4SLinus Torvalds 	swap_map = p->swap_map;
16541da177e4SLinus Torvalds 	p->swap_map = NULL;
16551da177e4SLinus Torvalds 	p->flags = 0;
16565d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1657fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
16581da177e4SLinus Torvalds 	vfree(swap_map);
165927a7faa0SKAMEZAWA Hiroyuki 	/* Destroy swap account informatin */
166027a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
166127a7faa0SKAMEZAWA Hiroyuki 
16621da177e4SLinus Torvalds 	inode = mapping->host;
16631da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
16641da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
16651da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
1666e525fd89STejun Heo 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
16671da177e4SLinus Torvalds 	} else {
16681b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
16691da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
16701b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
16711da177e4SLinus Torvalds 	}
16721da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
16731da177e4SLinus Torvalds 	err = 0;
167466d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
167566d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds out_dput:
16781da177e4SLinus Torvalds 	filp_close(victim, NULL);
16791da177e4SLinus Torvalds out:
16801da177e4SLinus Torvalds 	return err;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
168466d7dd51SKay Sievers struct proc_swaps {
168566d7dd51SKay Sievers 	struct seq_file seq;
168666d7dd51SKay Sievers 	int event;
168766d7dd51SKay Sievers };
168866d7dd51SKay Sievers 
168966d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait)
169066d7dd51SKay Sievers {
169166d7dd51SKay Sievers 	struct proc_swaps *s = file->private_data;
169266d7dd51SKay Sievers 
169366d7dd51SKay Sievers 	poll_wait(file, &proc_poll_wait, wait);
169466d7dd51SKay Sievers 
169566d7dd51SKay Sievers 	if (s->event != atomic_read(&proc_poll_event)) {
169666d7dd51SKay Sievers 		s->event = atomic_read(&proc_poll_event);
169766d7dd51SKay Sievers 		return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
169866d7dd51SKay Sievers 	}
169966d7dd51SKay Sievers 
170066d7dd51SKay Sievers 	return POLLIN | POLLRDNORM;
170166d7dd51SKay Sievers }
170266d7dd51SKay Sievers 
17031da177e4SLinus Torvalds /* iterator */
17041da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
17051da177e4SLinus Torvalds {
1706efa90a98SHugh Dickins 	struct swap_info_struct *si;
1707efa90a98SHugh Dickins 	int type;
17081da177e4SLinus Torvalds 	loff_t l = *pos;
17091da177e4SLinus Torvalds 
1710fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
17111da177e4SLinus Torvalds 
1712881e4aabSSuleiman Souhlal 	if (!l)
1713881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1714881e4aabSSuleiman Souhlal 
1715efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1716efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1717efa90a98SHugh Dickins 		si = swap_info[type];
1718efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
17191da177e4SLinus Torvalds 			continue;
1720881e4aabSSuleiman Souhlal 		if (!--l)
1721efa90a98SHugh Dickins 			return si;
17221da177e4SLinus Torvalds 	}
17231da177e4SLinus Torvalds 
17241da177e4SLinus Torvalds 	return NULL;
17251da177e4SLinus Torvalds }
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
17281da177e4SLinus Torvalds {
1729efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
1730efa90a98SHugh Dickins 	int type;
17311da177e4SLinus Torvalds 
1732881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1733efa90a98SHugh Dickins 		type = 0;
1734efa90a98SHugh Dickins 	else
1735efa90a98SHugh Dickins 		type = si->type + 1;
1736881e4aabSSuleiman Souhlal 
1737efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
1738efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1739efa90a98SHugh Dickins 		si = swap_info[type];
1740efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
17411da177e4SLinus Torvalds 			continue;
17421da177e4SLinus Torvalds 		++*pos;
1743efa90a98SHugh Dickins 		return si;
17441da177e4SLinus Torvalds 	}
17451da177e4SLinus Torvalds 
17461da177e4SLinus Torvalds 	return NULL;
17471da177e4SLinus Torvalds }
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
17501da177e4SLinus Torvalds {
1751fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
17521da177e4SLinus Torvalds }
17531da177e4SLinus Torvalds 
17541da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
17551da177e4SLinus Torvalds {
1756efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
17571da177e4SLinus Torvalds 	struct file *file;
17581da177e4SLinus Torvalds 	int len;
17591da177e4SLinus Torvalds 
1760efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
17611da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1762881e4aabSSuleiman Souhlal 		return 0;
1763881e4aabSSuleiman Souhlal 	}
17641da177e4SLinus Torvalds 
1765efa90a98SHugh Dickins 	file = si->swap_file;
1766c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
17676eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
17681da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
1769d3ac7f89SJosef "Jeff" Sipek 			S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
17701da177e4SLinus Torvalds 				"partition" : "file\t",
1771efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
1772efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
1773efa90a98SHugh Dickins 			si->prio);
17741da177e4SLinus Torvalds 	return 0;
17751da177e4SLinus Torvalds }
17761da177e4SLinus Torvalds 
177715ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
17781da177e4SLinus Torvalds 	.start =	swap_start,
17791da177e4SLinus Torvalds 	.next =		swap_next,
17801da177e4SLinus Torvalds 	.stop =		swap_stop,
17811da177e4SLinus Torvalds 	.show =		swap_show
17821da177e4SLinus Torvalds };
17831da177e4SLinus Torvalds 
17841da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
17851da177e4SLinus Torvalds {
178666d7dd51SKay Sievers 	struct proc_swaps *s;
178766d7dd51SKay Sievers 	int ret;
178866d7dd51SKay Sievers 
178966d7dd51SKay Sievers 	s = kmalloc(sizeof(struct proc_swaps), GFP_KERNEL);
179066d7dd51SKay Sievers 	if (!s)
179166d7dd51SKay Sievers 		return -ENOMEM;
179266d7dd51SKay Sievers 
179366d7dd51SKay Sievers 	file->private_data = s;
179466d7dd51SKay Sievers 
179566d7dd51SKay Sievers 	ret = seq_open(file, &swaps_op);
179666d7dd51SKay Sievers 	if (ret) {
179766d7dd51SKay Sievers 		kfree(s);
179866d7dd51SKay Sievers 		return ret;
179966d7dd51SKay Sievers 	}
180066d7dd51SKay Sievers 
180166d7dd51SKay Sievers 	s->seq.private = s;
180266d7dd51SKay Sievers 	s->event = atomic_read(&proc_poll_event);
180366d7dd51SKay Sievers 	return ret;
18041da177e4SLinus Torvalds }
18051da177e4SLinus Torvalds 
180615ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
18071da177e4SLinus Torvalds 	.open		= swaps_open,
18081da177e4SLinus Torvalds 	.read		= seq_read,
18091da177e4SLinus Torvalds 	.llseek		= seq_lseek,
18101da177e4SLinus Torvalds 	.release	= seq_release,
181166d7dd51SKay Sievers 	.poll		= swaps_poll,
18121da177e4SLinus Torvalds };
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds static int __init procswaps_init(void)
18151da177e4SLinus Torvalds {
18163d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
18171da177e4SLinus Torvalds 	return 0;
18181da177e4SLinus Torvalds }
18191da177e4SLinus Torvalds __initcall(procswaps_init);
18201da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
18211da177e4SLinus Torvalds 
18221796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
18231796316aSJan Beulich static int __init max_swapfiles_check(void)
18241796316aSJan Beulich {
18251796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
18261796316aSJan Beulich 	return 0;
18271796316aSJan Beulich }
18281796316aSJan Beulich late_initcall(max_swapfiles_check);
18291796316aSJan Beulich #endif
18301796316aSJan Beulich 
183153cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void)
18321da177e4SLinus Torvalds {
18331da177e4SLinus Torvalds 	struct swap_info_struct *p;
18341da177e4SLinus Torvalds 	unsigned int type;
1835efa90a98SHugh Dickins 
1836efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
1837efa90a98SHugh Dickins 	if (!p)
183853cbb243SCesar Eduardo Barros 		return ERR_PTR(-ENOMEM);
1839efa90a98SHugh Dickins 
18405d337b91SHugh Dickins 	spin_lock(&swap_lock);
1841efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1842efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
18431da177e4SLinus Torvalds 			break;
1844efa90a98SHugh Dickins 	}
18450697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
18465d337b91SHugh Dickins 		spin_unlock(&swap_lock);
1847efa90a98SHugh Dickins 		kfree(p);
1848730c0581SCesar Eduardo Barros 		return ERR_PTR(-EPERM);
18491da177e4SLinus Torvalds 	}
1850efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
1851efa90a98SHugh Dickins 		p->type = type;
1852efa90a98SHugh Dickins 		swap_info[type] = p;
1853efa90a98SHugh Dickins 		/*
1854efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
1855efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
1856efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
1857efa90a98SHugh Dickins 		 */
1858efa90a98SHugh Dickins 		smp_wmb();
1859efa90a98SHugh Dickins 		nr_swapfiles++;
1860efa90a98SHugh Dickins 	} else {
1861efa90a98SHugh Dickins 		kfree(p);
1862efa90a98SHugh Dickins 		p = swap_info[type];
1863efa90a98SHugh Dickins 		/*
1864efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
1865efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
1866efa90a98SHugh Dickins 		 */
1867efa90a98SHugh Dickins 	}
18689625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
18691da177e4SLinus Torvalds 	p->flags = SWP_USED;
18701da177e4SLinus Torvalds 	p->next = -1;
18715d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1872efa90a98SHugh Dickins 
187353cbb243SCesar Eduardo Barros 	return p;
187453cbb243SCesar Eduardo Barros }
187553cbb243SCesar Eduardo Barros 
18764d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
18774d0e1e10SCesar Eduardo Barros {
18784d0e1e10SCesar Eduardo Barros 	int error;
18794d0e1e10SCesar Eduardo Barros 
18804d0e1e10SCesar Eduardo Barros 	if (S_ISBLK(inode->i_mode)) {
18814d0e1e10SCesar Eduardo Barros 		p->bdev = bdgrab(I_BDEV(inode));
18824d0e1e10SCesar Eduardo Barros 		error = blkdev_get(p->bdev,
18834d0e1e10SCesar Eduardo Barros 				   FMODE_READ | FMODE_WRITE | FMODE_EXCL,
18844d0e1e10SCesar Eduardo Barros 				   sys_swapon);
18854d0e1e10SCesar Eduardo Barros 		if (error < 0) {
18864d0e1e10SCesar Eduardo Barros 			p->bdev = NULL;
188787ade72aSCesar Eduardo Barros 			return -EINVAL;
18884d0e1e10SCesar Eduardo Barros 		}
18894d0e1e10SCesar Eduardo Barros 		p->old_block_size = block_size(p->bdev);
18904d0e1e10SCesar Eduardo Barros 		error = set_blocksize(p->bdev, PAGE_SIZE);
18914d0e1e10SCesar Eduardo Barros 		if (error < 0)
189287ade72aSCesar Eduardo Barros 			return error;
18934d0e1e10SCesar Eduardo Barros 		p->flags |= SWP_BLKDEV;
18944d0e1e10SCesar Eduardo Barros 	} else if (S_ISREG(inode->i_mode)) {
18954d0e1e10SCesar Eduardo Barros 		p->bdev = inode->i_sb->s_bdev;
18964d0e1e10SCesar Eduardo Barros 		mutex_lock(&inode->i_mutex);
189787ade72aSCesar Eduardo Barros 		if (IS_SWAPFILE(inode))
189887ade72aSCesar Eduardo Barros 			return -EBUSY;
189987ade72aSCesar Eduardo Barros 	} else
190087ade72aSCesar Eduardo Barros 		return -EINVAL;
19014d0e1e10SCesar Eduardo Barros 
19024d0e1e10SCesar Eduardo Barros 	return 0;
19034d0e1e10SCesar Eduardo Barros }
19044d0e1e10SCesar Eduardo Barros 
1905ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p,
1906ca8bd38bSCesar Eduardo Barros 					union swap_header *swap_header,
1907ca8bd38bSCesar Eduardo Barros 					struct inode *inode)
1908ca8bd38bSCesar Eduardo Barros {
1909ca8bd38bSCesar Eduardo Barros 	int i;
1910ca8bd38bSCesar Eduardo Barros 	unsigned long maxpages;
1911ca8bd38bSCesar Eduardo Barros 	unsigned long swapfilepages;
1912ca8bd38bSCesar Eduardo Barros 
1913ca8bd38bSCesar Eduardo Barros 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1914ca8bd38bSCesar Eduardo Barros 		printk(KERN_ERR "Unable to find swap-space signature\n");
191538719025SCesar Eduardo Barros 		return 0;
1916ca8bd38bSCesar Eduardo Barros 	}
1917ca8bd38bSCesar Eduardo Barros 
1918ca8bd38bSCesar Eduardo Barros 	/* swap partition endianess hack... */
1919ca8bd38bSCesar Eduardo Barros 	if (swab32(swap_header->info.version) == 1) {
1920ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.version);
1921ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.last_page);
1922ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.nr_badpages);
1923ca8bd38bSCesar Eduardo Barros 		for (i = 0; i < swap_header->info.nr_badpages; i++)
1924ca8bd38bSCesar Eduardo Barros 			swab32s(&swap_header->info.badpages[i]);
1925ca8bd38bSCesar Eduardo Barros 	}
1926ca8bd38bSCesar Eduardo Barros 	/* Check the swap header's sub-version */
1927ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.version != 1) {
1928ca8bd38bSCesar Eduardo Barros 		printk(KERN_WARNING
1929ca8bd38bSCesar Eduardo Barros 		       "Unable to handle swap header version %d\n",
1930ca8bd38bSCesar Eduardo Barros 		       swap_header->info.version);
193138719025SCesar Eduardo Barros 		return 0;
1932ca8bd38bSCesar Eduardo Barros 	}
1933ca8bd38bSCesar Eduardo Barros 
1934ca8bd38bSCesar Eduardo Barros 	p->lowest_bit  = 1;
1935ca8bd38bSCesar Eduardo Barros 	p->cluster_next = 1;
1936ca8bd38bSCesar Eduardo Barros 	p->cluster_nr = 0;
1937ca8bd38bSCesar Eduardo Barros 
1938ca8bd38bSCesar Eduardo Barros 	/*
1939ca8bd38bSCesar Eduardo Barros 	 * Find out how many pages are allowed for a single swap
1940ca8bd38bSCesar Eduardo Barros 	 * device. There are two limiting factors: 1) the number of
1941ca8bd38bSCesar Eduardo Barros 	 * bits for the swap offset in the swp_entry_t type and
1942ca8bd38bSCesar Eduardo Barros 	 * 2) the number of bits in the a swap pte as defined by
1943ca8bd38bSCesar Eduardo Barros 	 * the different architectures. In order to find the
1944ca8bd38bSCesar Eduardo Barros 	 * largest possible bit mask a swap entry with swap type 0
1945ca8bd38bSCesar Eduardo Barros 	 * and swap offset ~0UL is created, encoded to a swap pte,
1946ca8bd38bSCesar Eduardo Barros 	 * decoded to a swp_entry_t again and finally the swap
1947ca8bd38bSCesar Eduardo Barros 	 * offset is extracted. This will mask all the bits from
1948ca8bd38bSCesar Eduardo Barros 	 * the initial ~0UL mask that can't be encoded in either
1949ca8bd38bSCesar Eduardo Barros 	 * the swp_entry_t or the architecture definition of a
1950ca8bd38bSCesar Eduardo Barros 	 * swap pte.
1951ca8bd38bSCesar Eduardo Barros 	 */
1952ca8bd38bSCesar Eduardo Barros 	maxpages = swp_offset(pte_to_swp_entry(
1953ca8bd38bSCesar Eduardo Barros 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
1954ca8bd38bSCesar Eduardo Barros 	if (maxpages > swap_header->info.last_page) {
1955ca8bd38bSCesar Eduardo Barros 		maxpages = swap_header->info.last_page + 1;
1956ca8bd38bSCesar Eduardo Barros 		/* p->max is an unsigned int: don't overflow it */
1957ca8bd38bSCesar Eduardo Barros 		if ((unsigned int)maxpages == 0)
1958ca8bd38bSCesar Eduardo Barros 			maxpages = UINT_MAX;
1959ca8bd38bSCesar Eduardo Barros 	}
1960ca8bd38bSCesar Eduardo Barros 	p->highest_bit = maxpages - 1;
1961ca8bd38bSCesar Eduardo Barros 
1962ca8bd38bSCesar Eduardo Barros 	if (!maxpages)
196338719025SCesar Eduardo Barros 		return 0;
1964ca8bd38bSCesar Eduardo Barros 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
1965ca8bd38bSCesar Eduardo Barros 	if (swapfilepages && maxpages > swapfilepages) {
1966ca8bd38bSCesar Eduardo Barros 		printk(KERN_WARNING
1967ca8bd38bSCesar Eduardo Barros 		       "Swap area shorter than signature indicates\n");
196838719025SCesar Eduardo Barros 		return 0;
1969ca8bd38bSCesar Eduardo Barros 	}
1970ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
197138719025SCesar Eduardo Barros 		return 0;
1972ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
197338719025SCesar Eduardo Barros 		return 0;
1974ca8bd38bSCesar Eduardo Barros 
1975ca8bd38bSCesar Eduardo Barros 	return maxpages;
1976ca8bd38bSCesar Eduardo Barros }
1977ca8bd38bSCesar Eduardo Barros 
1978915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p,
1979915d4d7bSCesar Eduardo Barros 					union swap_header *swap_header,
1980915d4d7bSCesar Eduardo Barros 					unsigned char *swap_map,
1981915d4d7bSCesar Eduardo Barros 					unsigned long maxpages,
1982915d4d7bSCesar Eduardo Barros 					sector_t *span)
1983915d4d7bSCesar Eduardo Barros {
1984915d4d7bSCesar Eduardo Barros 	int i;
1985915d4d7bSCesar Eduardo Barros 	unsigned int nr_good_pages;
1986915d4d7bSCesar Eduardo Barros 	int nr_extents;
1987915d4d7bSCesar Eduardo Barros 
1988915d4d7bSCesar Eduardo Barros 	nr_good_pages = maxpages - 1;	/* omit header page */
1989915d4d7bSCesar Eduardo Barros 
1990915d4d7bSCesar Eduardo Barros 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
1991915d4d7bSCesar Eduardo Barros 		unsigned int page_nr = swap_header->info.badpages[i];
1992bdb8e3f6SCesar Eduardo Barros 		if (page_nr == 0 || page_nr > swap_header->info.last_page)
1993bdb8e3f6SCesar Eduardo Barros 			return -EINVAL;
1994915d4d7bSCesar Eduardo Barros 		if (page_nr < maxpages) {
1995915d4d7bSCesar Eduardo Barros 			swap_map[page_nr] = SWAP_MAP_BAD;
1996915d4d7bSCesar Eduardo Barros 			nr_good_pages--;
1997915d4d7bSCesar Eduardo Barros 		}
1998915d4d7bSCesar Eduardo Barros 	}
1999915d4d7bSCesar Eduardo Barros 
2000915d4d7bSCesar Eduardo Barros 	if (nr_good_pages) {
2001915d4d7bSCesar Eduardo Barros 		swap_map[0] = SWAP_MAP_BAD;
2002915d4d7bSCesar Eduardo Barros 		p->max = maxpages;
2003915d4d7bSCesar Eduardo Barros 		p->pages = nr_good_pages;
2004915d4d7bSCesar Eduardo Barros 		nr_extents = setup_swap_extents(p, span);
2005bdb8e3f6SCesar Eduardo Barros 		if (nr_extents < 0)
2006bdb8e3f6SCesar Eduardo Barros 			return nr_extents;
2007915d4d7bSCesar Eduardo Barros 		nr_good_pages = p->pages;
2008915d4d7bSCesar Eduardo Barros 	}
2009915d4d7bSCesar Eduardo Barros 	if (!nr_good_pages) {
2010915d4d7bSCesar Eduardo Barros 		printk(KERN_WARNING "Empty swap-file\n");
2011bdb8e3f6SCesar Eduardo Barros 		return -EINVAL;
2012915d4d7bSCesar Eduardo Barros 	}
2013915d4d7bSCesar Eduardo Barros 
2014915d4d7bSCesar Eduardo Barros 	return nr_extents;
2015915d4d7bSCesar Eduardo Barros }
2016915d4d7bSCesar Eduardo Barros 
201753cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
201853cbb243SCesar Eduardo Barros {
201953cbb243SCesar Eduardo Barros 	struct swap_info_struct *p;
202028b36bd7SCesar Eduardo Barros 	char *name;
202153cbb243SCesar Eduardo Barros 	struct file *swap_file = NULL;
202253cbb243SCesar Eduardo Barros 	struct address_space *mapping;
202340531542SCesar Eduardo Barros 	int i;
202440531542SCesar Eduardo Barros 	int prio;
202553cbb243SCesar Eduardo Barros 	int error;
202653cbb243SCesar Eduardo Barros 	union swap_header *swap_header;
2027915d4d7bSCesar Eduardo Barros 	int nr_extents;
202853cbb243SCesar Eduardo Barros 	sector_t span;
202953cbb243SCesar Eduardo Barros 	unsigned long maxpages;
203053cbb243SCesar Eduardo Barros 	unsigned char *swap_map = NULL;
203153cbb243SCesar Eduardo Barros 	struct page *page = NULL;
203253cbb243SCesar Eduardo Barros 	struct inode *inode = NULL;
203353cbb243SCesar Eduardo Barros 
203453cbb243SCesar Eduardo Barros 	if (!capable(CAP_SYS_ADMIN))
203553cbb243SCesar Eduardo Barros 		return -EPERM;
203653cbb243SCesar Eduardo Barros 
203753cbb243SCesar Eduardo Barros 	p = alloc_swap_info();
20382542e513SCesar Eduardo Barros 	if (IS_ERR(p))
20392542e513SCesar Eduardo Barros 		return PTR_ERR(p);
204053cbb243SCesar Eduardo Barros 
20411da177e4SLinus Torvalds 	name = getname(specialfile);
20421da177e4SLinus Torvalds 	if (IS_ERR(name)) {
20437de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(name);
20441da177e4SLinus Torvalds 		name = NULL;
2045bd69010bSCesar Eduardo Barros 		goto bad_swap;
20461da177e4SLinus Torvalds 	}
20471da177e4SLinus Torvalds 	swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
20481da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
20497de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(swap_file);
20501da177e4SLinus Torvalds 		swap_file = NULL;
2051bd69010bSCesar Eduardo Barros 		goto bad_swap;
20521da177e4SLinus Torvalds 	}
20531da177e4SLinus Torvalds 
20541da177e4SLinus Torvalds 	p->swap_file = swap_file;
20551da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
20561da177e4SLinus Torvalds 
20571da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
2058efa90a98SHugh Dickins 		struct swap_info_struct *q = swap_info[i];
20591da177e4SLinus Torvalds 
2060e8e6c2ecSCesar Eduardo Barros 		if (q == p || !q->swap_file)
20611da177e4SLinus Torvalds 			continue;
20627de7fb6bSCesar Eduardo Barros 		if (mapping == q->swap_file->f_mapping) {
20637de7fb6bSCesar Eduardo Barros 			error = -EBUSY;
20641da177e4SLinus Torvalds 			goto bad_swap;
20651da177e4SLinus Torvalds 		}
20667de7fb6bSCesar Eduardo Barros 	}
20671da177e4SLinus Torvalds 
20682130781eSCesar Eduardo Barros 	inode = mapping->host;
20692130781eSCesar Eduardo Barros 	/* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
20704d0e1e10SCesar Eduardo Barros 	error = claim_swapfile(p, inode);
20714d0e1e10SCesar Eduardo Barros 	if (unlikely(error))
20721da177e4SLinus Torvalds 		goto bad_swap;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 	/*
20751da177e4SLinus Torvalds 	 * Read the swap header.
20761da177e4SLinus Torvalds 	 */
20771da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
20781da177e4SLinus Torvalds 		error = -EINVAL;
20791da177e4SLinus Torvalds 		goto bad_swap;
20801da177e4SLinus Torvalds 	}
2081090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
20821da177e4SLinus Torvalds 	if (IS_ERR(page)) {
20831da177e4SLinus Torvalds 		error = PTR_ERR(page);
20841da177e4SLinus Torvalds 		goto bad_swap;
20851da177e4SLinus Torvalds 	}
208681e33971SHugh Dickins 	swap_header = kmap(page);
20871da177e4SLinus Torvalds 
2088ca8bd38bSCesar Eduardo Barros 	maxpages = read_swap_header(p, swap_header, inode);
2089ca8bd38bSCesar Eduardo Barros 	if (unlikely(!maxpages)) {
20901da177e4SLinus Torvalds 		error = -EINVAL;
20911da177e4SLinus Torvalds 		goto bad_swap;
20921da177e4SLinus Torvalds 	}
20931da177e4SLinus Torvalds 
20941da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
2095803d0c83SCesar Eduardo Barros 	swap_map = vzalloc(maxpages);
209678ecba08SHugh Dickins 	if (!swap_map) {
20971da177e4SLinus Torvalds 		error = -ENOMEM;
20981da177e4SLinus Torvalds 		goto bad_swap;
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 
21011421ef3cSCesar Eduardo Barros 	error = swap_cgroup_swapon(p->type, maxpages);
21021421ef3cSCesar Eduardo Barros 	if (error)
21031421ef3cSCesar Eduardo Barros 		goto bad_swap;
21041421ef3cSCesar Eduardo Barros 
2105915d4d7bSCesar Eduardo Barros 	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
2106915d4d7bSCesar Eduardo Barros 		maxpages, &span);
2107915d4d7bSCesar Eduardo Barros 	if (unlikely(nr_extents < 0)) {
210853092a74SHugh Dickins 		error = nr_extents;
2109e2244ec2SHugh Dickins 		goto bad_swap;
211053092a74SHugh Dickins 	}
21111da177e4SLinus Torvalds 
21123bd0f0c7SSuresh Jayaraman 	if (p->bdev) {
211320137a49SHugh Dickins 		if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
211420137a49SHugh Dickins 			p->flags |= SWP_SOLIDSTATE;
211520137a49SHugh Dickins 			p->cluster_next = 1 + (random32() % p->highest_bit);
211620137a49SHugh Dickins 		}
211733994466SHugh Dickins 		if (discard_swap(p) == 0 && (swap_flags & SWAP_FLAG_DISCARD))
21186a6ba831SHugh Dickins 			p->flags |= SWP_DISCARDABLE;
21193bd0f0c7SSuresh Jayaraman 	}
21206a6ba831SHugh Dickins 
2121fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
212240531542SCesar Eduardo Barros 	prio = -1;
212378ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
212440531542SCesar Eduardo Barros 		prio =
212578ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
212640531542SCesar Eduardo Barros 	enable_swap_info(p, prio, swap_map);
2127c69dbfb8SCesar Eduardo Barros 
2128c69dbfb8SCesar Eduardo Barros 	printk(KERN_INFO "Adding %uk swap on %s.  "
2129c69dbfb8SCesar Eduardo Barros 			"Priority:%d extents:%d across:%lluk %s%s\n",
2130c69dbfb8SCesar Eduardo Barros 		p->pages<<(PAGE_SHIFT-10), name, p->prio,
2131c69dbfb8SCesar Eduardo Barros 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
2132c69dbfb8SCesar Eduardo Barros 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2133c69dbfb8SCesar Eduardo Barros 		(p->flags & SWP_DISCARDABLE) ? "D" : "");
2134c69dbfb8SCesar Eduardo Barros 
2135fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
213666d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
213766d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
213866d7dd51SKay Sievers 
21399b01c350SCesar Eduardo Barros 	if (S_ISREG(inode->i_mode))
21409b01c350SCesar Eduardo Barros 		inode->i_flags |= S_SWAPFILE;
21411da177e4SLinus Torvalds 	error = 0;
21421da177e4SLinus Torvalds 	goto out;
21431da177e4SLinus Torvalds bad_swap:
2144bd69010bSCesar Eduardo Barros 	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
2145f2090d2dSCesar Eduardo Barros 		set_blocksize(p->bdev, p->old_block_size);
2146f2090d2dSCesar Eduardo Barros 		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
21471da177e4SLinus Torvalds 	}
21484cd3bb10SHugh Dickins 	destroy_swap_extents(p);
2149e8e6c2ecSCesar Eduardo Barros 	swap_cgroup_swapoff(p->type);
21505d337b91SHugh Dickins 	spin_lock(&swap_lock);
21511da177e4SLinus Torvalds 	p->swap_file = NULL;
21521da177e4SLinus Torvalds 	p->flags = 0;
21535d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21541da177e4SLinus Torvalds 	vfree(swap_map);
215552c50567SMel Gorman 	if (swap_file) {
21562130781eSCesar Eduardo Barros 		if (inode && S_ISREG(inode->i_mode)) {
215752c50567SMel Gorman 			mutex_unlock(&inode->i_mutex);
21582130781eSCesar Eduardo Barros 			inode = NULL;
21592130781eSCesar Eduardo Barros 		}
21601da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
216152c50567SMel Gorman 	}
21621da177e4SLinus Torvalds out:
21631da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
21641da177e4SLinus Torvalds 		kunmap(page);
21651da177e4SLinus Torvalds 		page_cache_release(page);
21661da177e4SLinus Torvalds 	}
21671da177e4SLinus Torvalds 	if (name)
21681da177e4SLinus Torvalds 		putname(name);
21699b01c350SCesar Eduardo Barros 	if (inode && S_ISREG(inode->i_mode))
21701b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
21711da177e4SLinus Torvalds 	return error;
21721da177e4SLinus Torvalds }
21731da177e4SLinus Torvalds 
21741da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
21751da177e4SLinus Torvalds {
2176efa90a98SHugh Dickins 	unsigned int type;
21771da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
21781da177e4SLinus Torvalds 
21795d337b91SHugh Dickins 	spin_lock(&swap_lock);
2180efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2181efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
2182efa90a98SHugh Dickins 
2183efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2184efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
21851da177e4SLinus Torvalds 	}
21861da177e4SLinus Torvalds 	val->freeswap = nr_swap_pages + nr_to_be_unused;
21871da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
21885d337b91SHugh Dickins 	spin_unlock(&swap_lock);
21891da177e4SLinus Torvalds }
21901da177e4SLinus Torvalds 
21911da177e4SLinus Torvalds /*
21921da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
21931da177e4SLinus Torvalds  *
2194355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
2195355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
2196355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
2197355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
2198355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
2199355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
2200570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
22011da177e4SLinus Torvalds  */
22028d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
22031da177e4SLinus Torvalds {
22041da177e4SLinus Torvalds 	struct swap_info_struct *p;
22051da177e4SLinus Torvalds 	unsigned long offset, type;
22068d69aaeeSHugh Dickins 	unsigned char count;
22078d69aaeeSHugh Dickins 	unsigned char has_cache;
2208253d553bSHugh Dickins 	int err = -EINVAL;
22091da177e4SLinus Torvalds 
2210a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
2211253d553bSHugh Dickins 		goto out;
22120697212aSChristoph Lameter 
22131da177e4SLinus Torvalds 	type = swp_type(entry);
22141da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
22151da177e4SLinus Torvalds 		goto bad_file;
2216efa90a98SHugh Dickins 	p = swap_info[type];
22171da177e4SLinus Torvalds 	offset = swp_offset(entry);
22181da177e4SLinus Torvalds 
22195d337b91SHugh Dickins 	spin_lock(&swap_lock);
2220355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
2221355cfa73SKAMEZAWA Hiroyuki 		goto unlock_out;
2222355cfa73SKAMEZAWA Hiroyuki 
2223253d553bSHugh Dickins 	count = p->swap_map[offset];
2224253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
2225253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
2226253d553bSHugh Dickins 	err = 0;
2227355cfa73SKAMEZAWA Hiroyuki 
2228253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
2229355cfa73SKAMEZAWA Hiroyuki 
2230355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2231253d553bSHugh Dickins 		if (!has_cache && count)
2232253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
2233253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
2234253d553bSHugh Dickins 			err = -EEXIST;
2235253d553bSHugh Dickins 		else				/* no users remaining */
2236253d553bSHugh Dickins 			err = -ENOENT;
2237355cfa73SKAMEZAWA Hiroyuki 
2238355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
2239253d553bSHugh Dickins 
2240570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2241570a335bSHugh Dickins 			count += usage;
2242570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2243253d553bSHugh Dickins 			err = -EINVAL;
2244570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
2245570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2246570a335bSHugh Dickins 		else
2247570a335bSHugh Dickins 			err = -ENOMEM;
2248253d553bSHugh Dickins 	} else
2249253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
2250253d553bSHugh Dickins 
2251253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
2252253d553bSHugh Dickins 
2253355cfa73SKAMEZAWA Hiroyuki unlock_out:
22545d337b91SHugh Dickins 	spin_unlock(&swap_lock);
22551da177e4SLinus Torvalds out:
2256253d553bSHugh Dickins 	return err;
22571da177e4SLinus Torvalds 
22581da177e4SLinus Torvalds bad_file:
22591da177e4SLinus Torvalds 	printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
22601da177e4SLinus Torvalds 	goto out;
22611da177e4SLinus Torvalds }
2262253d553bSHugh Dickins 
2263355cfa73SKAMEZAWA Hiroyuki /*
2264aaa46865SHugh Dickins  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2265aaa46865SHugh Dickins  * (in which case its reference count is never incremented).
2266aaa46865SHugh Dickins  */
2267aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry)
2268aaa46865SHugh Dickins {
2269aaa46865SHugh Dickins 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
2270aaa46865SHugh Dickins }
2271aaa46865SHugh Dickins 
2272aaa46865SHugh Dickins /*
227308259d58SHugh Dickins  * Increase reference count of swap entry by 1.
227408259d58SHugh Dickins  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
227508259d58SHugh Dickins  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
227608259d58SHugh Dickins  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
227708259d58SHugh Dickins  * might occur if a page table entry has got corrupted.
2278355cfa73SKAMEZAWA Hiroyuki  */
2279570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
2280355cfa73SKAMEZAWA Hiroyuki {
2281570a335bSHugh Dickins 	int err = 0;
2282570a335bSHugh Dickins 
2283570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2284570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2285570a335bSHugh Dickins 	return err;
2286355cfa73SKAMEZAWA Hiroyuki }
22871da177e4SLinus Torvalds 
2288cb4b86baSKAMEZAWA Hiroyuki /*
2289355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
2290355cfa73SKAMEZAWA Hiroyuki  *
229173c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
2292355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
2293355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
2294355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
2295cb4b86baSKAMEZAWA Hiroyuki  */
2296cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
2297cb4b86baSKAMEZAWA Hiroyuki {
2298253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2299cb4b86baSKAMEZAWA Hiroyuki }
2300cb4b86baSKAMEZAWA Hiroyuki 
23011da177e4SLinus Torvalds /*
23025d337b91SHugh Dickins  * swap_lock prevents swap_map being freed. Don't grab an extra
23031da177e4SLinus Torvalds  * reference on the swaphandle, it doesn't matter if it becomes unused.
23041da177e4SLinus Torvalds  */
23051da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
23061da177e4SLinus Torvalds {
23078952898bSHugh Dickins 	struct swap_info_struct *si;
23083f9e7949SHugh Dickins 	int our_page_cluster = page_cluster;
23098952898bSHugh Dickins 	pgoff_t target, toff;
23108952898bSHugh Dickins 	pgoff_t base, end;
23118952898bSHugh Dickins 	int nr_pages = 0;
23121da177e4SLinus Torvalds 
23133f9e7949SHugh Dickins 	if (!our_page_cluster)	/* no readahead */
23141da177e4SLinus Torvalds 		return 0;
23158952898bSHugh Dickins 
2316efa90a98SHugh Dickins 	si = swap_info[swp_type(entry)];
23178952898bSHugh Dickins 	target = swp_offset(entry);
23188952898bSHugh Dickins 	base = (target >> our_page_cluster) << our_page_cluster;
23198952898bSHugh Dickins 	end = base + (1 << our_page_cluster);
23208952898bSHugh Dickins 	if (!base)		/* first page is swap header */
23218952898bSHugh Dickins 		base++;
23221da177e4SLinus Torvalds 
23235d337b91SHugh Dickins 	spin_lock(&swap_lock);
23248952898bSHugh Dickins 	if (end > si->max)	/* don't go beyond end of map */
23258952898bSHugh Dickins 		end = si->max;
23268952898bSHugh Dickins 
23278952898bSHugh Dickins 	/* Count contiguous allocated slots above our target */
23288952898bSHugh Dickins 	for (toff = target; ++toff < end; nr_pages++) {
23291da177e4SLinus Torvalds 		/* Don't read in free or bad pages */
23308952898bSHugh Dickins 		if (!si->swap_map[toff])
23311da177e4SLinus Torvalds 			break;
2332355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
23331da177e4SLinus Torvalds 			break;
23348952898bSHugh Dickins 	}
23358952898bSHugh Dickins 	/* Count contiguous allocated slots below our target */
23368952898bSHugh Dickins 	for (toff = target; --toff >= base; nr_pages++) {
23378952898bSHugh Dickins 		/* Don't read in free or bad pages */
23388952898bSHugh Dickins 		if (!si->swap_map[toff])
23398952898bSHugh Dickins 			break;
2340355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
23418952898bSHugh Dickins 			break;
23428952898bSHugh Dickins 	}
23435d337b91SHugh Dickins 	spin_unlock(&swap_lock);
23448952898bSHugh Dickins 
23458952898bSHugh Dickins 	/*
23468952898bSHugh Dickins 	 * Indicate starting offset, and return number of pages to get:
23478952898bSHugh Dickins 	 * if only 1, say 0, since there's then no readahead to be done.
23488952898bSHugh Dickins 	 */
23498952898bSHugh Dickins 	*offset = ++toff;
23508952898bSHugh Dickins 	return nr_pages? ++nr_pages: 0;
23511da177e4SLinus Torvalds }
2352570a335bSHugh Dickins 
2353570a335bSHugh Dickins /*
2354570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
2355570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2356570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
2357570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2358570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2359570a335bSHugh Dickins  *
2360570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
2361570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
2362570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2363570a335bSHugh Dickins  *
2364570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2365570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2366570a335bSHugh Dickins  * can be called after dropping locks.
2367570a335bSHugh Dickins  */
2368570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2369570a335bSHugh Dickins {
2370570a335bSHugh Dickins 	struct swap_info_struct *si;
2371570a335bSHugh Dickins 	struct page *head;
2372570a335bSHugh Dickins 	struct page *page;
2373570a335bSHugh Dickins 	struct page *list_page;
2374570a335bSHugh Dickins 	pgoff_t offset;
2375570a335bSHugh Dickins 	unsigned char count;
2376570a335bSHugh Dickins 
2377570a335bSHugh Dickins 	/*
2378570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2379570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2380570a335bSHugh Dickins 	 */
2381570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2382570a335bSHugh Dickins 
2383570a335bSHugh Dickins 	si = swap_info_get(entry);
2384570a335bSHugh Dickins 	if (!si) {
2385570a335bSHugh Dickins 		/*
2386570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
2387570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
2388570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
2389570a335bSHugh Dickins 		 */
2390570a335bSHugh Dickins 		goto outer;
2391570a335bSHugh Dickins 	}
2392570a335bSHugh Dickins 
2393570a335bSHugh Dickins 	offset = swp_offset(entry);
2394570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2395570a335bSHugh Dickins 
2396570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2397570a335bSHugh Dickins 		/*
2398570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
2399570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
2400570a335bSHugh Dickins 		 * over-provisioning.
2401570a335bSHugh Dickins 		 */
2402570a335bSHugh Dickins 		goto out;
2403570a335bSHugh Dickins 	}
2404570a335bSHugh Dickins 
2405570a335bSHugh Dickins 	if (!page) {
2406570a335bSHugh Dickins 		spin_unlock(&swap_lock);
2407570a335bSHugh Dickins 		return -ENOMEM;
2408570a335bSHugh Dickins 	}
2409570a335bSHugh Dickins 
2410570a335bSHugh Dickins 	/*
2411570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2412570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel pagetables: so it
2413570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2414570a335bSHugh Dickins 	 */
2415570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2416570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2417570a335bSHugh Dickins 
2418570a335bSHugh Dickins 	/*
2419570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
2420570a335bSHugh Dickins 	 * but it does always reset its private field.
2421570a335bSHugh Dickins 	 */
2422570a335bSHugh Dickins 	if (!page_private(head)) {
2423570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2424570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
2425570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
2426570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
2427570a335bSHugh Dickins 	}
2428570a335bSHugh Dickins 
2429570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
2430570a335bSHugh Dickins 		unsigned char *map;
2431570a335bSHugh Dickins 
2432570a335bSHugh Dickins 		/*
2433570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
2434570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
2435570a335bSHugh Dickins 		 */
2436570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
2437570a335bSHugh Dickins 			goto out;
2438570a335bSHugh Dickins 
2439570a335bSHugh Dickins 		map = kmap_atomic(list_page, KM_USER0) + offset;
2440570a335bSHugh Dickins 		count = *map;
2441570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2442570a335bSHugh Dickins 
2443570a335bSHugh Dickins 		/*
2444570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
2445570a335bSHugh Dickins 		 * free our allocation and use this one.
2446570a335bSHugh Dickins 		 */
2447570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2448570a335bSHugh Dickins 			goto out;
2449570a335bSHugh Dickins 	}
2450570a335bSHugh Dickins 
2451570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
2452570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
2453570a335bSHugh Dickins out:
2454570a335bSHugh Dickins 	spin_unlock(&swap_lock);
2455570a335bSHugh Dickins outer:
2456570a335bSHugh Dickins 	if (page)
2457570a335bSHugh Dickins 		__free_page(page);
2458570a335bSHugh Dickins 	return 0;
2459570a335bSHugh Dickins }
2460570a335bSHugh Dickins 
2461570a335bSHugh Dickins /*
2462570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
2463570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2464570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
2465570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
2466570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
2467570a335bSHugh Dickins  * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2468570a335bSHugh Dickins  */
2469570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
2470570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
2471570a335bSHugh Dickins {
2472570a335bSHugh Dickins 	struct page *head;
2473570a335bSHugh Dickins 	struct page *page;
2474570a335bSHugh Dickins 	unsigned char *map;
2475570a335bSHugh Dickins 
2476570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2477570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
2478570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2479570a335bSHugh Dickins 		return false;		/* need to add count continuation */
2480570a335bSHugh Dickins 	}
2481570a335bSHugh Dickins 
2482570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2483570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
2484570a335bSHugh Dickins 	map = kmap_atomic(page, KM_USER0) + offset;
2485570a335bSHugh Dickins 
2486570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2487570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2488570a335bSHugh Dickins 
2489570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2490570a335bSHugh Dickins 		/*
2491570a335bSHugh Dickins 		 * Think of how you add 1 to 999
2492570a335bSHugh Dickins 		 */
2493570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
2494570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2495570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2496570a335bSHugh Dickins 			BUG_ON(page == head);
2497570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2498570a335bSHugh Dickins 		}
2499570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
2500570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2501570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2502570a335bSHugh Dickins 			if (page == head)
2503570a335bSHugh Dickins 				return false;	/* add count continuation */
2504570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2505570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
2506570a335bSHugh Dickins 		}
2507570a335bSHugh Dickins 		*map += 1;
2508570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2509570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2510570a335bSHugh Dickins 		while (page != head) {
2511570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2512570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
2513570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2514570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2515570a335bSHugh Dickins 		}
2516570a335bSHugh Dickins 		return true;			/* incremented */
2517570a335bSHugh Dickins 
2518570a335bSHugh Dickins 	} else {				/* decrementing */
2519570a335bSHugh Dickins 		/*
2520570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
2521570a335bSHugh Dickins 		 */
2522570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
2523570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
2524570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2525570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2526570a335bSHugh Dickins 			BUG_ON(page == head);
2527570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2528570a335bSHugh Dickins 		}
2529570a335bSHugh Dickins 		BUG_ON(*map == 0);
2530570a335bSHugh Dickins 		*map -= 1;
2531570a335bSHugh Dickins 		if (*map == 0)
2532570a335bSHugh Dickins 			count = 0;
2533570a335bSHugh Dickins 		kunmap_atomic(map, KM_USER0);
2534570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2535570a335bSHugh Dickins 		while (page != head) {
2536570a335bSHugh Dickins 			map = kmap_atomic(page, KM_USER0) + offset;
2537570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
2538570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2539570a335bSHugh Dickins 			kunmap_atomic(map, KM_USER0);
2540570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2541570a335bSHugh Dickins 		}
2542570a335bSHugh Dickins 		return count == COUNT_CONTINUED;
2543570a335bSHugh Dickins 	}
2544570a335bSHugh Dickins }
2545570a335bSHugh Dickins 
2546570a335bSHugh Dickins /*
2547570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
2548570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2549570a335bSHugh Dickins  */
2550570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
2551570a335bSHugh Dickins {
2552570a335bSHugh Dickins 	pgoff_t offset;
2553570a335bSHugh Dickins 
2554570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2555570a335bSHugh Dickins 		struct page *head;
2556570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
2557570a335bSHugh Dickins 		if (page_private(head)) {
2558570a335bSHugh Dickins 			struct list_head *this, *next;
2559570a335bSHugh Dickins 			list_for_each_safe(this, next, &head->lru) {
2560570a335bSHugh Dickins 				struct page *page;
2561570a335bSHugh Dickins 				page = list_entry(this, struct page, lru);
2562570a335bSHugh Dickins 				list_del(this);
2563570a335bSHugh Dickins 				__free_page(page);
2564570a335bSHugh Dickins 			}
2565570a335bSHugh Dickins 		}
2566570a335bSHugh Dickins 	}
2567570a335bSHugh Dickins }
2568