xref: /openbmc/linux/mm/swapfile.c (revision edfe23dac3e2981277087b05bec7fec7790d1835)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/mm/swapfile.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
51da177e4SLinus Torvalds  *  Swap reorganised 29.12.95, Stephen Tweedie
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/mm.h>
91da177e4SLinus Torvalds #include <linux/hugetlb.h>
101da177e4SLinus Torvalds #include <linux/mman.h>
111da177e4SLinus Torvalds #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/kernel_stat.h>
131da177e4SLinus Torvalds #include <linux/swap.h>
141da177e4SLinus Torvalds #include <linux/vmalloc.h>
151da177e4SLinus Torvalds #include <linux/pagemap.h>
161da177e4SLinus Torvalds #include <linux/namei.h>
17072441e2SHugh Dickins #include <linux/shmem_fs.h>
181da177e4SLinus Torvalds #include <linux/blkdev.h>
1920137a49SHugh Dickins #include <linux/random.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
211da177e4SLinus Torvalds #include <linux/proc_fs.h>
221da177e4SLinus Torvalds #include <linux/seq_file.h>
231da177e4SLinus Torvalds #include <linux/init.h>
245ad64688SHugh Dickins #include <linux/ksm.h>
251da177e4SLinus Torvalds #include <linux/rmap.h>
261da177e4SLinus Torvalds #include <linux/security.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
28fc0abb14SIngo Molnar #include <linux/mutex.h>
29c59ede7bSRandy.Dunlap #include <linux/capability.h>
301da177e4SLinus Torvalds #include <linux/syscalls.h>
318a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
3266d7dd51SKay Sievers #include <linux/poll.h>
3372788c38SDavid Rientjes #include <linux/oom.h>
3438b5faf4SDan Magenheimer #include <linux/frontswap.h>
3538b5faf4SDan Magenheimer #include <linux/swapfile.h>
36f981c595SMel Gorman #include <linux/export.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <asm/pgtable.h>
391da177e4SLinus Torvalds #include <asm/tlbflush.h>
401da177e4SLinus Torvalds #include <linux/swapops.h>
4127a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h>
421da177e4SLinus Torvalds 
43570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
44570a335bSHugh Dickins 				 unsigned char);
45570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
46d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**);
47570a335bSHugh Dickins 
4838b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock);
497c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
50ec8acf20SShaohua Li atomic_long_t nr_swap_pages;
51ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
521da177e4SLinus Torvalds long total_swap_pages;
5378ecba08SHugh Dickins static int least_priority;
54ec8acf20SShaohua Li static atomic_t highest_priority_index = ATOMIC_INIT(-1);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
571da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
581da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
591da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
601da177e4SLinus Torvalds 
6138b5faf4SDan Magenheimer struct swap_list_t swap_list = {-1, -1};
621da177e4SLinus Torvalds 
6338b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES];
641da177e4SLinus Torvalds 
65fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
661da177e4SLinus Torvalds 
6766d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
6866d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */
6966d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0);
7066d7dd51SKay Sievers 
718d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
72355cfa73SKAMEZAWA Hiroyuki {
73570a335bSHugh Dickins 	return ent & ~SWAP_HAS_CACHE;	/* may include SWAP_HAS_CONT flag */
74355cfa73SKAMEZAWA Hiroyuki }
75355cfa73SKAMEZAWA Hiroyuki 
76efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
77c9e44410SKAMEZAWA Hiroyuki static int
78c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
79c9e44410SKAMEZAWA Hiroyuki {
80efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
81c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
82c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
83c9e44410SKAMEZAWA Hiroyuki 
8433806f06SShaohua Li 	page = find_get_page(swap_address_space(entry), entry.val);
85c9e44410SKAMEZAWA Hiroyuki 	if (!page)
86c9e44410SKAMEZAWA Hiroyuki 		return 0;
87c9e44410SKAMEZAWA Hiroyuki 	/*
88c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
89c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
90c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
91c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
92c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
93c9e44410SKAMEZAWA Hiroyuki 	 */
94c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
95c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
96c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
97c9e44410SKAMEZAWA Hiroyuki 	}
98c9e44410SKAMEZAWA Hiroyuki 	page_cache_release(page);
99c9e44410SKAMEZAWA Hiroyuki 	return ret;
100c9e44410SKAMEZAWA Hiroyuki }
101355cfa73SKAMEZAWA Hiroyuki 
1021da177e4SLinus Torvalds /*
1036a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1046a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1056a6ba831SHugh Dickins  */
1066a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1076a6ba831SHugh Dickins {
1086a6ba831SHugh Dickins 	struct swap_extent *se;
1099625a5f2SHugh Dickins 	sector_t start_block;
1109625a5f2SHugh Dickins 	sector_t nr_blocks;
1116a6ba831SHugh Dickins 	int err = 0;
1126a6ba831SHugh Dickins 
1136a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1149625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1159625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1169625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1179625a5f2SHugh Dickins 	if (nr_blocks) {
1189625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
119dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1209625a5f2SHugh Dickins 		if (err)
1219625a5f2SHugh Dickins 			return err;
1229625a5f2SHugh Dickins 		cond_resched();
1236a6ba831SHugh Dickins 	}
1246a6ba831SHugh Dickins 
1259625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1269625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1279625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1289625a5f2SHugh Dickins 
1296a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
130dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1316a6ba831SHugh Dickins 		if (err)
1326a6ba831SHugh Dickins 			break;
1336a6ba831SHugh Dickins 
1346a6ba831SHugh Dickins 		cond_resched();
1356a6ba831SHugh Dickins 	}
1366a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1376a6ba831SHugh Dickins }
1386a6ba831SHugh Dickins 
1397992fde7SHugh Dickins /*
1407992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1417992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1427992fde7SHugh Dickins  */
1437992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1447992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1457992fde7SHugh Dickins {
1467992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1477992fde7SHugh Dickins 	int found_extent = 0;
1487992fde7SHugh Dickins 
1497992fde7SHugh Dickins 	while (nr_pages) {
1507992fde7SHugh Dickins 		struct list_head *lh;
1517992fde7SHugh Dickins 
1527992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1537992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1547992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1557992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
156858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1577992fde7SHugh Dickins 
1587992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1597992fde7SHugh Dickins 				nr_blocks = nr_pages;
1607992fde7SHugh Dickins 			start_page += nr_blocks;
1617992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1627992fde7SHugh Dickins 
1637992fde7SHugh Dickins 			if (!found_extent++)
1647992fde7SHugh Dickins 				si->curr_swap_extent = se;
1657992fde7SHugh Dickins 
1667992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1677992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1687992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
169dd3932edSChristoph Hellwig 				    nr_blocks, GFP_NOIO, 0))
1707992fde7SHugh Dickins 				break;
1717992fde7SHugh Dickins 		}
1727992fde7SHugh Dickins 
1737992fde7SHugh Dickins 		lh = se->list.next;
1747992fde7SHugh Dickins 		se = list_entry(lh, struct swap_extent, list);
1757992fde7SHugh Dickins 	}
1767992fde7SHugh Dickins }
1777992fde7SHugh Dickins 
178048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
179048c27fdSHugh Dickins #define LATENCY_LIMIT		256
180048c27fdSHugh Dickins 
1812a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info,
1822a8f9449SShaohua Li 	unsigned int flag)
1832a8f9449SShaohua Li {
1842a8f9449SShaohua Li 	info->flags = flag;
1852a8f9449SShaohua Li }
1862a8f9449SShaohua Li 
1872a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info)
1882a8f9449SShaohua Li {
1892a8f9449SShaohua Li 	return info->data;
1902a8f9449SShaohua Li }
1912a8f9449SShaohua Li 
1922a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info,
1932a8f9449SShaohua Li 				     unsigned int c)
1942a8f9449SShaohua Li {
1952a8f9449SShaohua Li 	info->data = c;
1962a8f9449SShaohua Li }
1972a8f9449SShaohua Li 
1982a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info,
1992a8f9449SShaohua Li 					 unsigned int c, unsigned int f)
2002a8f9449SShaohua Li {
2012a8f9449SShaohua Li 	info->flags = f;
2022a8f9449SShaohua Li 	info->data = c;
2032a8f9449SShaohua Li }
2042a8f9449SShaohua Li 
2052a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info)
2062a8f9449SShaohua Li {
2072a8f9449SShaohua Li 	return info->data;
2082a8f9449SShaohua Li }
2092a8f9449SShaohua Li 
2102a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info,
2112a8f9449SShaohua Li 				    unsigned int n)
2122a8f9449SShaohua Li {
2132a8f9449SShaohua Li 	info->data = n;
2142a8f9449SShaohua Li }
2152a8f9449SShaohua Li 
2162a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info,
2172a8f9449SShaohua Li 					 unsigned int n, unsigned int f)
2182a8f9449SShaohua Li {
2192a8f9449SShaohua Li 	info->flags = f;
2202a8f9449SShaohua Li 	info->data = n;
2212a8f9449SShaohua Li }
2222a8f9449SShaohua Li 
2232a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info)
2242a8f9449SShaohua Li {
2252a8f9449SShaohua Li 	return info->flags & CLUSTER_FLAG_FREE;
2262a8f9449SShaohua Li }
2272a8f9449SShaohua Li 
2282a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info)
2292a8f9449SShaohua Li {
2302a8f9449SShaohua Li 	return info->flags & CLUSTER_FLAG_NEXT_NULL;
2312a8f9449SShaohua Li }
2322a8f9449SShaohua Li 
2332a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info)
2342a8f9449SShaohua Li {
2352a8f9449SShaohua Li 	info->flags = CLUSTER_FLAG_NEXT_NULL;
2362a8f9449SShaohua Li 	info->data = 0;
2372a8f9449SShaohua Li }
2382a8f9449SShaohua Li 
239815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */
240815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si,
241815c2c54SShaohua Li 		unsigned int idx)
242815c2c54SShaohua Li {
243815c2c54SShaohua Li 	/*
244815c2c54SShaohua Li 	 * If scan_swap_map() can't find a free cluster, it will check
245815c2c54SShaohua Li 	 * si->swap_map directly. To make sure the discarding cluster isn't
246815c2c54SShaohua Li 	 * taken by scan_swap_map(), mark the swap entries bad (occupied). It
247815c2c54SShaohua Li 	 * will be cleared after discard
248815c2c54SShaohua Li 	 */
249815c2c54SShaohua Li 	memset(si->swap_map + idx * SWAPFILE_CLUSTER,
250815c2c54SShaohua Li 			SWAP_MAP_BAD, SWAPFILE_CLUSTER);
251815c2c54SShaohua Li 
252815c2c54SShaohua Li 	if (cluster_is_null(&si->discard_cluster_head)) {
253815c2c54SShaohua Li 		cluster_set_next_flag(&si->discard_cluster_head,
254815c2c54SShaohua Li 						idx, 0);
255815c2c54SShaohua Li 		cluster_set_next_flag(&si->discard_cluster_tail,
256815c2c54SShaohua Li 						idx, 0);
257815c2c54SShaohua Li 	} else {
258815c2c54SShaohua Li 		unsigned int tail = cluster_next(&si->discard_cluster_tail);
259815c2c54SShaohua Li 		cluster_set_next(&si->cluster_info[tail], idx);
260815c2c54SShaohua Li 		cluster_set_next_flag(&si->discard_cluster_tail,
261815c2c54SShaohua Li 						idx, 0);
262815c2c54SShaohua Li 	}
263815c2c54SShaohua Li 
264815c2c54SShaohua Li 	schedule_work(&si->discard_work);
265815c2c54SShaohua Li }
266815c2c54SShaohua Li 
267815c2c54SShaohua Li /*
268815c2c54SShaohua Li  * Doing discard actually. After a cluster discard is finished, the cluster
269815c2c54SShaohua Li  * will be added to free cluster list. caller should hold si->lock.
270815c2c54SShaohua Li */
271815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si)
272815c2c54SShaohua Li {
273815c2c54SShaohua Li 	struct swap_cluster_info *info;
274815c2c54SShaohua Li 	unsigned int idx;
275815c2c54SShaohua Li 
276815c2c54SShaohua Li 	info = si->cluster_info;
277815c2c54SShaohua Li 
278815c2c54SShaohua Li 	while (!cluster_is_null(&si->discard_cluster_head)) {
279815c2c54SShaohua Li 		idx = cluster_next(&si->discard_cluster_head);
280815c2c54SShaohua Li 
281815c2c54SShaohua Li 		cluster_set_next_flag(&si->discard_cluster_head,
282815c2c54SShaohua Li 						cluster_next(&info[idx]), 0);
283815c2c54SShaohua Li 		if (cluster_next(&si->discard_cluster_tail) == idx) {
284815c2c54SShaohua Li 			cluster_set_null(&si->discard_cluster_head);
285815c2c54SShaohua Li 			cluster_set_null(&si->discard_cluster_tail);
286815c2c54SShaohua Li 		}
287815c2c54SShaohua Li 		spin_unlock(&si->lock);
288815c2c54SShaohua Li 
289815c2c54SShaohua Li 		discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
290815c2c54SShaohua Li 				SWAPFILE_CLUSTER);
291815c2c54SShaohua Li 
292815c2c54SShaohua Li 		spin_lock(&si->lock);
293815c2c54SShaohua Li 		cluster_set_flag(&info[idx], CLUSTER_FLAG_FREE);
294815c2c54SShaohua Li 		if (cluster_is_null(&si->free_cluster_head)) {
295815c2c54SShaohua Li 			cluster_set_next_flag(&si->free_cluster_head,
296815c2c54SShaohua Li 						idx, 0);
297815c2c54SShaohua Li 			cluster_set_next_flag(&si->free_cluster_tail,
298815c2c54SShaohua Li 						idx, 0);
299815c2c54SShaohua Li 		} else {
300815c2c54SShaohua Li 			unsigned int tail;
301815c2c54SShaohua Li 
302815c2c54SShaohua Li 			tail = cluster_next(&si->free_cluster_tail);
303815c2c54SShaohua Li 			cluster_set_next(&info[tail], idx);
304815c2c54SShaohua Li 			cluster_set_next_flag(&si->free_cluster_tail,
305815c2c54SShaohua Li 						idx, 0);
306815c2c54SShaohua Li 		}
307815c2c54SShaohua Li 		memset(si->swap_map + idx * SWAPFILE_CLUSTER,
308815c2c54SShaohua Li 				0, SWAPFILE_CLUSTER);
309815c2c54SShaohua Li 	}
310815c2c54SShaohua Li }
311815c2c54SShaohua Li 
312815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work)
313815c2c54SShaohua Li {
314815c2c54SShaohua Li 	struct swap_info_struct *si;
315815c2c54SShaohua Li 
316815c2c54SShaohua Li 	si = container_of(work, struct swap_info_struct, discard_work);
317815c2c54SShaohua Li 
318815c2c54SShaohua Li 	spin_lock(&si->lock);
319815c2c54SShaohua Li 	swap_do_scheduled_discard(si);
320815c2c54SShaohua Li 	spin_unlock(&si->lock);
321815c2c54SShaohua Li }
322815c2c54SShaohua Li 
3232a8f9449SShaohua Li /*
3242a8f9449SShaohua Li  * The cluster corresponding to page_nr will be used. The cluster will be
3252a8f9449SShaohua Li  * removed from free cluster list and its usage counter will be increased.
3262a8f9449SShaohua Li  */
3272a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p,
3282a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
3292a8f9449SShaohua Li {
3302a8f9449SShaohua Li 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
3312a8f9449SShaohua Li 
3322a8f9449SShaohua Li 	if (!cluster_info)
3332a8f9449SShaohua Li 		return;
3342a8f9449SShaohua Li 	if (cluster_is_free(&cluster_info[idx])) {
3352a8f9449SShaohua Li 		VM_BUG_ON(cluster_next(&p->free_cluster_head) != idx);
3362a8f9449SShaohua Li 		cluster_set_next_flag(&p->free_cluster_head,
3372a8f9449SShaohua Li 			cluster_next(&cluster_info[idx]), 0);
3382a8f9449SShaohua Li 		if (cluster_next(&p->free_cluster_tail) == idx) {
3392a8f9449SShaohua Li 			cluster_set_null(&p->free_cluster_tail);
3402a8f9449SShaohua Li 			cluster_set_null(&p->free_cluster_head);
3412a8f9449SShaohua Li 		}
3422a8f9449SShaohua Li 		cluster_set_count_flag(&cluster_info[idx], 0, 0);
3432a8f9449SShaohua Li 	}
3442a8f9449SShaohua Li 
3452a8f9449SShaohua Li 	VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
3462a8f9449SShaohua Li 	cluster_set_count(&cluster_info[idx],
3472a8f9449SShaohua Li 		cluster_count(&cluster_info[idx]) + 1);
3482a8f9449SShaohua Li }
3492a8f9449SShaohua Li 
3502a8f9449SShaohua Li /*
3512a8f9449SShaohua Li  * The cluster corresponding to page_nr decreases one usage. If the usage
3522a8f9449SShaohua Li  * counter becomes 0, which means no page in the cluster is in using, we can
3532a8f9449SShaohua Li  * optionally discard the cluster and add it to free cluster list.
3542a8f9449SShaohua Li  */
3552a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p,
3562a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
3572a8f9449SShaohua Li {
3582a8f9449SShaohua Li 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
3592a8f9449SShaohua Li 
3602a8f9449SShaohua Li 	if (!cluster_info)
3612a8f9449SShaohua Li 		return;
3622a8f9449SShaohua Li 
3632a8f9449SShaohua Li 	VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
3642a8f9449SShaohua Li 	cluster_set_count(&cluster_info[idx],
3652a8f9449SShaohua Li 		cluster_count(&cluster_info[idx]) - 1);
3662a8f9449SShaohua Li 
3672a8f9449SShaohua Li 	if (cluster_count(&cluster_info[idx]) == 0) {
368815c2c54SShaohua Li 		/*
369815c2c54SShaohua Li 		 * If the swap is discardable, prepare discard the cluster
370815c2c54SShaohua Li 		 * instead of free it immediately. The cluster will be freed
371815c2c54SShaohua Li 		 * after discard.
372815c2c54SShaohua Li 		 */
373*edfe23daSShaohua Li 		if ((p->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
374*edfe23daSShaohua Li 				 (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
375815c2c54SShaohua Li 			swap_cluster_schedule_discard(p, idx);
376815c2c54SShaohua Li 			return;
377815c2c54SShaohua Li 		}
378815c2c54SShaohua Li 
3792a8f9449SShaohua Li 		cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
3802a8f9449SShaohua Li 		if (cluster_is_null(&p->free_cluster_head)) {
3812a8f9449SShaohua Li 			cluster_set_next_flag(&p->free_cluster_head, idx, 0);
3822a8f9449SShaohua Li 			cluster_set_next_flag(&p->free_cluster_tail, idx, 0);
3832a8f9449SShaohua Li 		} else {
3842a8f9449SShaohua Li 			unsigned int tail = cluster_next(&p->free_cluster_tail);
3852a8f9449SShaohua Li 			cluster_set_next(&cluster_info[tail], idx);
3862a8f9449SShaohua Li 			cluster_set_next_flag(&p->free_cluster_tail, idx, 0);
3872a8f9449SShaohua Li 		}
3882a8f9449SShaohua Li 	}
3892a8f9449SShaohua Li }
3902a8f9449SShaohua Li 
3912a8f9449SShaohua Li /*
3922a8f9449SShaohua Li  * It's possible scan_swap_map() uses a free cluster in the middle of free
3932a8f9449SShaohua Li  * cluster list. Avoiding such abuse to avoid list corruption.
3942a8f9449SShaohua Li  */
3952a8f9449SShaohua Li static inline bool scan_swap_map_recheck_cluster(struct swap_info_struct *si,
3962a8f9449SShaohua Li 	unsigned long offset)
3972a8f9449SShaohua Li {
3982a8f9449SShaohua Li 	offset /= SWAPFILE_CLUSTER;
3992a8f9449SShaohua Li 	return !cluster_is_null(&si->free_cluster_head) &&
4002a8f9449SShaohua Li 		offset != cluster_next(&si->free_cluster_head) &&
4012a8f9449SShaohua Li 		cluster_is_free(&si->cluster_info[offset]);
4022a8f9449SShaohua Li }
4032a8f9449SShaohua Li 
40424b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si,
4058d69aaeeSHugh Dickins 				   unsigned char usage)
4061da177e4SLinus Torvalds {
407ebebbbe9SHugh Dickins 	unsigned long offset;
408c60aa176SHugh Dickins 	unsigned long scan_base;
4097992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
410048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
4111da177e4SLinus Torvalds 
4127dfad418SHugh Dickins 	/*
4137dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
4147dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
4157dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
4167dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
4177dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
4187dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
4197dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
420c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
4211da177e4SLinus Torvalds 	 */
4227dfad418SHugh Dickins 
42352b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
424c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
425ebebbbe9SHugh Dickins 
426ebebbbe9SHugh Dickins 	if (unlikely(!si->cluster_nr--)) {
427ebebbbe9SHugh Dickins 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
4287dfad418SHugh Dickins 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
429ebebbbe9SHugh Dickins 			goto checks;
430ebebbbe9SHugh Dickins 		}
4312a8f9449SShaohua Li check_cluster:
4322a8f9449SShaohua Li 		if (!cluster_is_null(&si->free_cluster_head)) {
4332a8f9449SShaohua Li 			offset = cluster_next(&si->free_cluster_head) *
4342a8f9449SShaohua Li 						SWAPFILE_CLUSTER;
4352a8f9449SShaohua Li 			last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
4362a8f9449SShaohua Li 			si->cluster_next = offset;
4372a8f9449SShaohua Li 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
4382a8f9449SShaohua Li 			goto checks;
4392a8f9449SShaohua Li 		} else if (si->cluster_info) {
4402a8f9449SShaohua Li 			/*
441815c2c54SShaohua Li 			 * we don't have free cluster but have some clusters in
442815c2c54SShaohua Li 			 * discarding, do discard now and reclaim them
443815c2c54SShaohua Li 			 */
444815c2c54SShaohua Li 			if (!cluster_is_null(&si->discard_cluster_head)) {
445815c2c54SShaohua Li 				si->cluster_nr = 0;
446815c2c54SShaohua Li 				swap_do_scheduled_discard(si);
447815c2c54SShaohua Li 				scan_base = offset = si->cluster_next;
448815c2c54SShaohua Li 				if (!si->cluster_nr)
449815c2c54SShaohua Li 					goto check_cluster;
450815c2c54SShaohua Li 				si->cluster_nr--;
451815c2c54SShaohua Li 				goto checks;
452815c2c54SShaohua Li 			}
453815c2c54SShaohua Li 
454815c2c54SShaohua Li 			/*
4552a8f9449SShaohua Li 			 * Checking free cluster is fast enough, we can do the
4562a8f9449SShaohua Li 			 * check every time
4572a8f9449SShaohua Li 			 */
4582a8f9449SShaohua Li 			si->cluster_nr = 0;
4592a8f9449SShaohua Li 			goto checks;
4602a8f9449SShaohua Li 		}
4612a8f9449SShaohua Li 
462ec8acf20SShaohua Li 		spin_unlock(&si->lock);
4637dfad418SHugh Dickins 
464c60aa176SHugh Dickins 		/*
465c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
466c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
467c60aa176SHugh Dickins 		 * But if seek is cheap, search from our current position, so
468c60aa176SHugh Dickins 		 * that swap is allocated from all over the partition: if the
469c60aa176SHugh Dickins 		 * Flash Translation Layer only remaps within limited zones,
470c60aa176SHugh Dickins 		 * we don't want to wear out the first zone too quickly.
471c60aa176SHugh Dickins 		 */
472c60aa176SHugh Dickins 		if (!(si->flags & SWP_SOLIDSTATE))
473c60aa176SHugh Dickins 			scan_base = offset = si->lowest_bit;
4747dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
4757dfad418SHugh Dickins 
4767dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
4777dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
4781da177e4SLinus Torvalds 			if (si->swap_map[offset])
4797dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
4807dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
481ec8acf20SShaohua Li 				spin_lock(&si->lock);
482ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
483ebebbbe9SHugh Dickins 				si->cluster_next = offset;
484ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
485ebebbbe9SHugh Dickins 				goto checks;
4867dfad418SHugh Dickins 			}
487048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
488048c27fdSHugh Dickins 				cond_resched();
489048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
490048c27fdSHugh Dickins 			}
4917dfad418SHugh Dickins 		}
492ebebbbe9SHugh Dickins 
493ebebbbe9SHugh Dickins 		offset = si->lowest_bit;
494c60aa176SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
495c60aa176SHugh Dickins 
496c60aa176SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
497c60aa176SHugh Dickins 		for (; last_in_cluster < scan_base; offset++) {
498c60aa176SHugh Dickins 			if (si->swap_map[offset])
499c60aa176SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
500c60aa176SHugh Dickins 			else if (offset == last_in_cluster) {
501ec8acf20SShaohua Li 				spin_lock(&si->lock);
502c60aa176SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
503c60aa176SHugh Dickins 				si->cluster_next = offset;
504c60aa176SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
505c60aa176SHugh Dickins 				goto checks;
506c60aa176SHugh Dickins 			}
507c60aa176SHugh Dickins 			if (unlikely(--latency_ration < 0)) {
508c60aa176SHugh Dickins 				cond_resched();
509c60aa176SHugh Dickins 				latency_ration = LATENCY_LIMIT;
510c60aa176SHugh Dickins 			}
511c60aa176SHugh Dickins 		}
512c60aa176SHugh Dickins 
513c60aa176SHugh Dickins 		offset = scan_base;
514ec8acf20SShaohua Li 		spin_lock(&si->lock);
515ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
5167dfad418SHugh Dickins 	}
5177dfad418SHugh Dickins 
518ebebbbe9SHugh Dickins checks:
5192a8f9449SShaohua Li 	if (scan_swap_map_recheck_cluster(si, offset))
5202a8f9449SShaohua Li 		goto check_cluster;
521ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
52252b7efdbSHugh Dickins 		goto no_page;
5237dfad418SHugh Dickins 	if (!si->highest_bit)
5247dfad418SHugh Dickins 		goto no_page;
525ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
526c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
527c9e44410SKAMEZAWA Hiroyuki 
528b73d7fceSHugh Dickins 	/* reuse swap entry of cache-only swap if not busy. */
529b73d7fceSHugh Dickins 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
530c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
531ec8acf20SShaohua Li 		spin_unlock(&si->lock);
532c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
533ec8acf20SShaohua Li 		spin_lock(&si->lock);
534c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
535c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
536c9e44410SKAMEZAWA Hiroyuki 			goto checks;
537c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
538c9e44410SKAMEZAWA Hiroyuki 	}
539c9e44410SKAMEZAWA Hiroyuki 
540ebebbbe9SHugh Dickins 	if (si->swap_map[offset])
541ebebbbe9SHugh Dickins 		goto scan;
542ebebbbe9SHugh Dickins 
54352b7efdbSHugh Dickins 	if (offset == si->lowest_bit)
5441da177e4SLinus Torvalds 		si->lowest_bit++;
5451da177e4SLinus Torvalds 	if (offset == si->highest_bit)
5461da177e4SLinus Torvalds 		si->highest_bit--;
5477dfad418SHugh Dickins 	si->inuse_pages++;
5487dfad418SHugh Dickins 	if (si->inuse_pages == si->pages) {
5491da177e4SLinus Torvalds 		si->lowest_bit = si->max;
5501da177e4SLinus Torvalds 		si->highest_bit = 0;
5511da177e4SLinus Torvalds 	}
552253d553bSHugh Dickins 	si->swap_map[offset] = usage;
5532a8f9449SShaohua Li 	inc_cluster_info_page(si, si->cluster_info, offset);
5541da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
55552b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
5567992fde7SHugh Dickins 
5571da177e4SLinus Torvalds 	return offset;
5587dfad418SHugh Dickins 
559ebebbbe9SHugh Dickins scan:
560ec8acf20SShaohua Li 	spin_unlock(&si->lock);
5617dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
56252b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
563ec8acf20SShaohua Li 			spin_lock(&si->lock);
56452b7efdbSHugh Dickins 			goto checks;
5657dfad418SHugh Dickins 		}
566c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
567ec8acf20SShaohua Li 			spin_lock(&si->lock);
568c9e44410SKAMEZAWA Hiroyuki 			goto checks;
569c9e44410SKAMEZAWA Hiroyuki 		}
570048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
571048c27fdSHugh Dickins 			cond_resched();
572048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
573048c27fdSHugh Dickins 		}
57452b7efdbSHugh Dickins 	}
575c60aa176SHugh Dickins 	offset = si->lowest_bit;
576c60aa176SHugh Dickins 	while (++offset < scan_base) {
577c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
578ec8acf20SShaohua Li 			spin_lock(&si->lock);
579ebebbbe9SHugh Dickins 			goto checks;
580c60aa176SHugh Dickins 		}
581c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
582ec8acf20SShaohua Li 			spin_lock(&si->lock);
583c9e44410SKAMEZAWA Hiroyuki 			goto checks;
584c9e44410SKAMEZAWA Hiroyuki 		}
585c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
586c60aa176SHugh Dickins 			cond_resched();
587c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
588c60aa176SHugh Dickins 		}
589c60aa176SHugh Dickins 	}
590ec8acf20SShaohua Li 	spin_lock(&si->lock);
5917dfad418SHugh Dickins 
5927dfad418SHugh Dickins no_page:
59352b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
5941da177e4SLinus Torvalds 	return 0;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds swp_entry_t get_swap_page(void)
5981da177e4SLinus Torvalds {
599fb4f88dcSHugh Dickins 	struct swap_info_struct *si;
600fb4f88dcSHugh Dickins 	pgoff_t offset;
601fb4f88dcSHugh Dickins 	int type, next;
602fb4f88dcSHugh Dickins 	int wrapped = 0;
603ec8acf20SShaohua Li 	int hp_index;
6041da177e4SLinus Torvalds 
6055d337b91SHugh Dickins 	spin_lock(&swap_lock);
606ec8acf20SShaohua Li 	if (atomic_long_read(&nr_swap_pages) <= 0)
607fb4f88dcSHugh Dickins 		goto noswap;
608ec8acf20SShaohua Li 	atomic_long_dec(&nr_swap_pages);
6091da177e4SLinus Torvalds 
610fb4f88dcSHugh Dickins 	for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
611ec8acf20SShaohua Li 		hp_index = atomic_xchg(&highest_priority_index, -1);
612ec8acf20SShaohua Li 		/*
613ec8acf20SShaohua Li 		 * highest_priority_index records current highest priority swap
614ec8acf20SShaohua Li 		 * type which just frees swap entries. If its priority is
615ec8acf20SShaohua Li 		 * higher than that of swap_list.next swap type, we use it.  It
616ec8acf20SShaohua Li 		 * isn't protected by swap_lock, so it can be an invalid value
617ec8acf20SShaohua Li 		 * if the corresponding swap type is swapoff. We double check
618ec8acf20SShaohua Li 		 * the flags here. It's even possible the swap type is swapoff
619ec8acf20SShaohua Li 		 * and swapon again and its priority is changed. In such rare
620ec8acf20SShaohua Li 		 * case, low prority swap type might be used, but eventually
621ec8acf20SShaohua Li 		 * high priority swap will be used after several rounds of
622ec8acf20SShaohua Li 		 * swap.
623ec8acf20SShaohua Li 		 */
624ec8acf20SShaohua Li 		if (hp_index != -1 && hp_index != type &&
625ec8acf20SShaohua Li 		    swap_info[type]->prio < swap_info[hp_index]->prio &&
626ec8acf20SShaohua Li 		    (swap_info[hp_index]->flags & SWP_WRITEOK)) {
627ec8acf20SShaohua Li 			type = hp_index;
628ec8acf20SShaohua Li 			swap_list.next = type;
629ec8acf20SShaohua Li 		}
630ec8acf20SShaohua Li 
631efa90a98SHugh Dickins 		si = swap_info[type];
632fb4f88dcSHugh Dickins 		next = si->next;
633fb4f88dcSHugh Dickins 		if (next < 0 ||
634efa90a98SHugh Dickins 		    (!wrapped && si->prio != swap_info[next]->prio)) {
635fb4f88dcSHugh Dickins 			next = swap_list.head;
636fb4f88dcSHugh Dickins 			wrapped++;
6371da177e4SLinus Torvalds 		}
638fb4f88dcSHugh Dickins 
639ec8acf20SShaohua Li 		spin_lock(&si->lock);
640ec8acf20SShaohua Li 		if (!si->highest_bit) {
641ec8acf20SShaohua Li 			spin_unlock(&si->lock);
642fb4f88dcSHugh Dickins 			continue;
643ec8acf20SShaohua Li 		}
644ec8acf20SShaohua Li 		if (!(si->flags & SWP_WRITEOK)) {
645ec8acf20SShaohua Li 			spin_unlock(&si->lock);
646fb4f88dcSHugh Dickins 			continue;
647ec8acf20SShaohua Li 		}
648fb4f88dcSHugh Dickins 
649fb4f88dcSHugh Dickins 		swap_list.next = next;
650ec8acf20SShaohua Li 
651ec8acf20SShaohua Li 		spin_unlock(&swap_lock);
652355cfa73SKAMEZAWA Hiroyuki 		/* This is called for allocating swap entry for cache */
653253d553bSHugh Dickins 		offset = scan_swap_map(si, SWAP_HAS_CACHE);
654ec8acf20SShaohua Li 		spin_unlock(&si->lock);
655ec8acf20SShaohua Li 		if (offset)
656fb4f88dcSHugh Dickins 			return swp_entry(type, offset);
657ec8acf20SShaohua Li 		spin_lock(&swap_lock);
658fb4f88dcSHugh Dickins 		next = swap_list.next;
659fb4f88dcSHugh Dickins 	}
660fb4f88dcSHugh Dickins 
661ec8acf20SShaohua Li 	atomic_long_inc(&nr_swap_pages);
662fb4f88dcSHugh Dickins noswap:
6635d337b91SHugh Dickins 	spin_unlock(&swap_lock);
664fb4f88dcSHugh Dickins 	return (swp_entry_t) {0};
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
667910321eaSHugh Dickins /* The only caller of this function is now susupend routine */
668910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type)
669910321eaSHugh Dickins {
670910321eaSHugh Dickins 	struct swap_info_struct *si;
671910321eaSHugh Dickins 	pgoff_t offset;
672910321eaSHugh Dickins 
673910321eaSHugh Dickins 	si = swap_info[type];
674ec8acf20SShaohua Li 	spin_lock(&si->lock);
675910321eaSHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
676ec8acf20SShaohua Li 		atomic_long_dec(&nr_swap_pages);
677910321eaSHugh Dickins 		/* This is called for allocating swap entry, not cache */
678910321eaSHugh Dickins 		offset = scan_swap_map(si, 1);
679910321eaSHugh Dickins 		if (offset) {
680ec8acf20SShaohua Li 			spin_unlock(&si->lock);
681910321eaSHugh Dickins 			return swp_entry(type, offset);
682910321eaSHugh Dickins 		}
683ec8acf20SShaohua Li 		atomic_long_inc(&nr_swap_pages);
684910321eaSHugh Dickins 	}
685ec8acf20SShaohua Li 	spin_unlock(&si->lock);
686910321eaSHugh Dickins 	return (swp_entry_t) {0};
687910321eaSHugh Dickins }
688910321eaSHugh Dickins 
6891da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry)
6901da177e4SLinus Torvalds {
6911da177e4SLinus Torvalds 	struct swap_info_struct *p;
6921da177e4SLinus Torvalds 	unsigned long offset, type;
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	if (!entry.val)
6951da177e4SLinus Torvalds 		goto out;
6961da177e4SLinus Torvalds 	type = swp_type(entry);
6971da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
6981da177e4SLinus Torvalds 		goto bad_nofile;
699efa90a98SHugh Dickins 	p = swap_info[type];
7001da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
7011da177e4SLinus Torvalds 		goto bad_device;
7021da177e4SLinus Torvalds 	offset = swp_offset(entry);
7031da177e4SLinus Torvalds 	if (offset >= p->max)
7041da177e4SLinus Torvalds 		goto bad_offset;
7051da177e4SLinus Torvalds 	if (!p->swap_map[offset])
7061da177e4SLinus Torvalds 		goto bad_free;
707ec8acf20SShaohua Li 	spin_lock(&p->lock);
7081da177e4SLinus Torvalds 	return p;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds bad_free:
711465c47fdSAndrew Morton 	pr_err("swap_free: %s%08lx\n", Unused_offset, entry.val);
7121da177e4SLinus Torvalds 	goto out;
7131da177e4SLinus Torvalds bad_offset:
714465c47fdSAndrew Morton 	pr_err("swap_free: %s%08lx\n", Bad_offset, entry.val);
7151da177e4SLinus Torvalds 	goto out;
7161da177e4SLinus Torvalds bad_device:
717465c47fdSAndrew Morton 	pr_err("swap_free: %s%08lx\n", Unused_file, entry.val);
7181da177e4SLinus Torvalds 	goto out;
7191da177e4SLinus Torvalds bad_nofile:
720465c47fdSAndrew Morton 	pr_err("swap_free: %s%08lx\n", Bad_file, entry.val);
7211da177e4SLinus Torvalds out:
7221da177e4SLinus Torvalds 	return NULL;
7231da177e4SLinus Torvalds }
7241da177e4SLinus Torvalds 
725ec8acf20SShaohua Li /*
726ec8acf20SShaohua Li  * This swap type frees swap entry, check if it is the highest priority swap
727ec8acf20SShaohua Li  * type which just frees swap entry. get_swap_page() uses
728ec8acf20SShaohua Li  * highest_priority_index to search highest priority swap type. The
729ec8acf20SShaohua Li  * swap_info_struct.lock can't protect us if there are multiple swap types
730ec8acf20SShaohua Li  * active, so we use atomic_cmpxchg.
731ec8acf20SShaohua Li  */
732ec8acf20SShaohua Li static void set_highest_priority_index(int type)
733ec8acf20SShaohua Li {
734ec8acf20SShaohua Li 	int old_hp_index, new_hp_index;
735ec8acf20SShaohua Li 
736ec8acf20SShaohua Li 	do {
737ec8acf20SShaohua Li 		old_hp_index = atomic_read(&highest_priority_index);
738ec8acf20SShaohua Li 		if (old_hp_index != -1 &&
739ec8acf20SShaohua Li 			swap_info[old_hp_index]->prio >= swap_info[type]->prio)
740ec8acf20SShaohua Li 			break;
741ec8acf20SShaohua Li 		new_hp_index = type;
742ec8acf20SShaohua Li 	} while (atomic_cmpxchg(&highest_priority_index,
743ec8acf20SShaohua Li 		old_hp_index, new_hp_index) != old_hp_index);
744ec8acf20SShaohua Li }
745ec8acf20SShaohua Li 
7468d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p,
7478d69aaeeSHugh Dickins 				     swp_entry_t entry, unsigned char usage)
7481da177e4SLinus Torvalds {
749253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
7508d69aaeeSHugh Dickins 	unsigned char count;
7518d69aaeeSHugh Dickins 	unsigned char has_cache;
7521da177e4SLinus Torvalds 
753355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
754253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
755253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
756253d553bSHugh Dickins 
757253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
758253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
759253d553bSHugh Dickins 		has_cache = 0;
760aaa46865SHugh Dickins 	} else if (count == SWAP_MAP_SHMEM) {
761aaa46865SHugh Dickins 		/*
762aaa46865SHugh Dickins 		 * Or we could insist on shmem.c using a special
763aaa46865SHugh Dickins 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
764aaa46865SHugh Dickins 		 */
765aaa46865SHugh Dickins 		count = 0;
766570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
767570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
768570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
769570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
770570a335bSHugh Dickins 			else
771570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
772570a335bSHugh Dickins 		} else
773253d553bSHugh Dickins 			count--;
774570a335bSHugh Dickins 	}
775253d553bSHugh Dickins 
776253d553bSHugh Dickins 	if (!count)
777253d553bSHugh Dickins 		mem_cgroup_uncharge_swap(entry);
778253d553bSHugh Dickins 
779253d553bSHugh Dickins 	usage = count | has_cache;
780253d553bSHugh Dickins 	p->swap_map[offset] = usage;
781253d553bSHugh Dickins 
782355cfa73SKAMEZAWA Hiroyuki 	/* free if no reference */
783253d553bSHugh Dickins 	if (!usage) {
7842a8f9449SShaohua Li 		dec_cluster_info_page(p, p->cluster_info, offset);
7851da177e4SLinus Torvalds 		if (offset < p->lowest_bit)
7861da177e4SLinus Torvalds 			p->lowest_bit = offset;
7871da177e4SLinus Torvalds 		if (offset > p->highest_bit)
7881da177e4SLinus Torvalds 			p->highest_bit = offset;
789ec8acf20SShaohua Li 		set_highest_priority_index(p->type);
790ec8acf20SShaohua Li 		atomic_long_inc(&nr_swap_pages);
7911da177e4SLinus Torvalds 		p->inuse_pages--;
79238b5faf4SDan Magenheimer 		frontswap_invalidate_page(p->type, offset);
79373744923SMel Gorman 		if (p->flags & SWP_BLKDEV) {
79473744923SMel Gorman 			struct gendisk *disk = p->bdev->bd_disk;
79573744923SMel Gorman 			if (disk->fops->swap_slot_free_notify)
79673744923SMel Gorman 				disk->fops->swap_slot_free_notify(p->bdev,
79773744923SMel Gorman 								  offset);
79873744923SMel Gorman 		}
7991da177e4SLinus Torvalds 	}
800253d553bSHugh Dickins 
801253d553bSHugh Dickins 	return usage;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds /*
8051da177e4SLinus Torvalds  * Caller has made sure that the swapdevice corresponding to entry
8061da177e4SLinus Torvalds  * is still around or has not been recycled.
8071da177e4SLinus Torvalds  */
8081da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
8091da177e4SLinus Torvalds {
8101da177e4SLinus Torvalds 	struct swap_info_struct *p;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	p = swap_info_get(entry);
8131da177e4SLinus Torvalds 	if (p) {
814253d553bSHugh Dickins 		swap_entry_free(p, entry, 1);
815ec8acf20SShaohua Li 		spin_unlock(&p->lock);
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds /*
820cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
821cb4b86baSKAMEZAWA Hiroyuki  */
822cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page)
823cb4b86baSKAMEZAWA Hiroyuki {
824355cfa73SKAMEZAWA Hiroyuki 	struct swap_info_struct *p;
8258d69aaeeSHugh Dickins 	unsigned char count;
826355cfa73SKAMEZAWA Hiroyuki 
827355cfa73SKAMEZAWA Hiroyuki 	p = swap_info_get(entry);
828355cfa73SKAMEZAWA Hiroyuki 	if (p) {
829253d553bSHugh Dickins 		count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
830253d553bSHugh Dickins 		if (page)
831253d553bSHugh Dickins 			mem_cgroup_uncharge_swapcache(page, entry, count != 0);
832ec8acf20SShaohua Li 		spin_unlock(&p->lock);
833355cfa73SKAMEZAWA Hiroyuki 	}
834cb4b86baSKAMEZAWA Hiroyuki }
835cb4b86baSKAMEZAWA Hiroyuki 
836cb4b86baSKAMEZAWA Hiroyuki /*
837c475a8abSHugh Dickins  * How many references to page are currently swapped out?
838570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
839570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
8401da177e4SLinus Torvalds  */
841bde05d1cSHugh Dickins int page_swapcount(struct page *page)
8421da177e4SLinus Torvalds {
843c475a8abSHugh Dickins 	int count = 0;
8441da177e4SLinus Torvalds 	struct swap_info_struct *p;
8451da177e4SLinus Torvalds 	swp_entry_t entry;
8461da177e4SLinus Torvalds 
8474c21e2f2SHugh Dickins 	entry.val = page_private(page);
8481da177e4SLinus Torvalds 	p = swap_info_get(entry);
8491da177e4SLinus Torvalds 	if (p) {
850355cfa73SKAMEZAWA Hiroyuki 		count = swap_count(p->swap_map[swp_offset(entry)]);
851ec8acf20SShaohua Li 		spin_unlock(&p->lock);
8521da177e4SLinus Torvalds 	}
853c475a8abSHugh Dickins 	return count;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
8561da177e4SLinus Torvalds /*
8577b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
8587b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
8597b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
8607b1fe597SHugh Dickins  * later would only waste time away from clustering.
8611da177e4SLinus Torvalds  */
8627b1fe597SHugh Dickins int reuse_swap_page(struct page *page)
8631da177e4SLinus Torvalds {
864c475a8abSHugh Dickins 	int count;
8651da177e4SLinus Torvalds 
86651726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
8675ad64688SHugh Dickins 	if (unlikely(PageKsm(page)))
8685ad64688SHugh Dickins 		return 0;
869c475a8abSHugh Dickins 	count = page_mapcount(page);
8707b1fe597SHugh Dickins 	if (count <= 1 && PageSwapCache(page)) {
871c475a8abSHugh Dickins 		count += page_swapcount(page);
8727b1fe597SHugh Dickins 		if (count == 1 && !PageWriteback(page)) {
8737b1fe597SHugh Dickins 			delete_from_swap_cache(page);
8747b1fe597SHugh Dickins 			SetPageDirty(page);
8757b1fe597SHugh Dickins 		}
8767b1fe597SHugh Dickins 	}
8775ad64688SHugh Dickins 	return count <= 1;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds /*
881a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
882a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
8831da177e4SLinus Torvalds  */
884a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
8851da177e4SLinus Torvalds {
88651726b12SHugh Dickins 	VM_BUG_ON(!PageLocked(page));
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (!PageSwapCache(page))
8891da177e4SLinus Torvalds 		return 0;
8901da177e4SLinus Torvalds 	if (PageWriteback(page))
8911da177e4SLinus Torvalds 		return 0;
892a2c43eedSHugh Dickins 	if (page_swapcount(page))
8931da177e4SLinus Torvalds 		return 0;
8941da177e4SLinus Torvalds 
895b73d7fceSHugh Dickins 	/*
896b73d7fceSHugh Dickins 	 * Once hibernation has begun to create its image of memory,
897b73d7fceSHugh Dickins 	 * there's a danger that one of the calls to try_to_free_swap()
898b73d7fceSHugh Dickins 	 * - most probably a call from __try_to_reclaim_swap() while
899b73d7fceSHugh Dickins 	 * hibernation is allocating its own swap pages for the image,
900b73d7fceSHugh Dickins 	 * but conceivably even a call from memory reclaim - will free
901b73d7fceSHugh Dickins 	 * the swap from a page which has already been recorded in the
902b73d7fceSHugh Dickins 	 * image as a clean swapcache page, and then reuse its swap for
903b73d7fceSHugh Dickins 	 * another page of the image.  On waking from hibernation, the
904b73d7fceSHugh Dickins 	 * original page might be freed under memory pressure, then
905b73d7fceSHugh Dickins 	 * later read back in from swap, now with the wrong data.
906b73d7fceSHugh Dickins 	 *
907f90ac398SMel Gorman 	 * Hibration suspends storage while it is writing the image
908f90ac398SMel Gorman 	 * to disk so check that here.
909b73d7fceSHugh Dickins 	 */
910f90ac398SMel Gorman 	if (pm_suspended_storage())
911b73d7fceSHugh Dickins 		return 0;
912b73d7fceSHugh Dickins 
913a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
9141da177e4SLinus Torvalds 	SetPageDirty(page);
915a2c43eedSHugh Dickins 	return 1;
91668a22394SRik van Riel }
91768a22394SRik van Riel 
91868a22394SRik van Riel /*
9191da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
9201da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
9211da177e4SLinus Torvalds  */
9222509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
9231da177e4SLinus Torvalds {
9241da177e4SLinus Torvalds 	struct swap_info_struct *p;
9251da177e4SLinus Torvalds 	struct page *page = NULL;
9261da177e4SLinus Torvalds 
927a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
9282509ef26SHugh Dickins 		return 1;
9290697212aSChristoph Lameter 
9301da177e4SLinus Torvalds 	p = swap_info_get(entry);
9311da177e4SLinus Torvalds 	if (p) {
932253d553bSHugh Dickins 		if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
93333806f06SShaohua Li 			page = find_get_page(swap_address_space(entry),
93433806f06SShaohua Li 						entry.val);
9358413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
93693fac704SNick Piggin 				page_cache_release(page);
93793fac704SNick Piggin 				page = NULL;
93893fac704SNick Piggin 			}
93993fac704SNick Piggin 		}
940ec8acf20SShaohua Li 		spin_unlock(&p->lock);
9411da177e4SLinus Torvalds 	}
9421da177e4SLinus Torvalds 	if (page) {
943a2c43eedSHugh Dickins 		/*
944a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
945a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
946a2c43eedSHugh Dickins 		 */
94793fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
948a2c43eedSHugh Dickins 				(!page_mapped(page) || vm_swap_full())) {
9491da177e4SLinus Torvalds 			delete_from_swap_cache(page);
9501da177e4SLinus Torvalds 			SetPageDirty(page);
9511da177e4SLinus Torvalds 		}
9521da177e4SLinus Torvalds 		unlock_page(page);
9531da177e4SLinus Torvalds 		page_cache_release(page);
9541da177e4SLinus Torvalds 	}
9552509ef26SHugh Dickins 	return p != NULL;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
959f577eb30SRafael J. Wysocki /*
960915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
961f577eb30SRafael J. Wysocki  *
962915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
963915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
964915bae9eSRafael J. Wysocki  *
965915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
966f577eb30SRafael J. Wysocki  */
9677bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
968f577eb30SRafael J. Wysocki {
969915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
970efa90a98SHugh Dickins 	int type;
971f577eb30SRafael J. Wysocki 
972915bae9eSRafael J. Wysocki 	if (device)
973915bae9eSRafael J. Wysocki 		bdev = bdget(device);
974915bae9eSRafael J. Wysocki 
975f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
976efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
977efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
978f577eb30SRafael J. Wysocki 
979915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
980f577eb30SRafael J. Wysocki 			continue;
981b6b5bce3SRafael J. Wysocki 
982915bae9eSRafael J. Wysocki 		if (!bdev) {
9837bf23687SRafael J. Wysocki 			if (bdev_p)
984dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
9857bf23687SRafael J. Wysocki 
9866e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
987efa90a98SHugh Dickins 			return type;
9886e1819d6SRafael J. Wysocki 		}
989915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
9909625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
991915bae9eSRafael J. Wysocki 
992915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
9937bf23687SRafael J. Wysocki 				if (bdev_p)
994dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
9957bf23687SRafael J. Wysocki 
996f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
997915bae9eSRafael J. Wysocki 				bdput(bdev);
998efa90a98SHugh Dickins 				return type;
999f577eb30SRafael J. Wysocki 			}
1000f577eb30SRafael J. Wysocki 		}
1001915bae9eSRafael J. Wysocki 	}
1002f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
1003915bae9eSRafael J. Wysocki 	if (bdev)
1004915bae9eSRafael J. Wysocki 		bdput(bdev);
1005915bae9eSRafael J. Wysocki 
1006f577eb30SRafael J. Wysocki 	return -ENODEV;
1007f577eb30SRafael J. Wysocki }
1008f577eb30SRafael J. Wysocki 
1009f577eb30SRafael J. Wysocki /*
101073c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
101173c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
101273c34b6aSHugh Dickins  */
101373c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
101473c34b6aSHugh Dickins {
101573c34b6aSHugh Dickins 	struct block_device *bdev;
101673c34b6aSHugh Dickins 
101773c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
101873c34b6aSHugh Dickins 		return 0;
101973c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
102073c34b6aSHugh Dickins 		return 0;
1021d4906e1aSLee Schermerhorn 	return map_swap_entry(swp_entry(type, offset), &bdev);
102273c34b6aSHugh Dickins }
102373c34b6aSHugh Dickins 
102473c34b6aSHugh Dickins /*
1025f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
1026f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
1027f577eb30SRafael J. Wysocki  *
1028f577eb30SRafael J. Wysocki  * This is needed for software suspend
1029f577eb30SRafael J. Wysocki  */
1030f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
1031f577eb30SRafael J. Wysocki {
1032f577eb30SRafael J. Wysocki 	unsigned int n = 0;
1033f577eb30SRafael J. Wysocki 
1034f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
1035efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
1036efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
1037efa90a98SHugh Dickins 
1038ec8acf20SShaohua Li 		spin_lock(&sis->lock);
1039efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
1040efa90a98SHugh Dickins 			n = sis->pages;
1041f577eb30SRafael J. Wysocki 			if (free)
1042efa90a98SHugh Dickins 				n -= sis->inuse_pages;
1043efa90a98SHugh Dickins 		}
1044ec8acf20SShaohua Li 		spin_unlock(&sis->lock);
1045f577eb30SRafael J. Wysocki 	}
1046f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
1047f577eb30SRafael J. Wysocki 	return n;
1048f577eb30SRafael J. Wysocki }
104973c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
1050f577eb30SRafael J. Wysocki 
1051179ef71cSCyrill Gorcunov static inline int maybe_same_pte(pte_t pte, pte_t swp_pte)
1052179ef71cSCyrill Gorcunov {
1053179ef71cSCyrill Gorcunov #ifdef CONFIG_MEM_SOFT_DIRTY
1054179ef71cSCyrill Gorcunov 	/*
1055179ef71cSCyrill Gorcunov 	 * When pte keeps soft dirty bit the pte generated
1056179ef71cSCyrill Gorcunov 	 * from swap entry does not has it, still it's same
1057179ef71cSCyrill Gorcunov 	 * pte from logical point of view.
1058179ef71cSCyrill Gorcunov 	 */
1059179ef71cSCyrill Gorcunov 	pte_t swp_pte_dirty = pte_swp_mksoft_dirty(swp_pte);
1060179ef71cSCyrill Gorcunov 	return pte_same(pte, swp_pte) || pte_same(pte, swp_pte_dirty);
1061179ef71cSCyrill Gorcunov #else
1062179ef71cSCyrill Gorcunov 	return pte_same(pte, swp_pte);
1063179ef71cSCyrill Gorcunov #endif
1064179ef71cSCyrill Gorcunov }
1065179ef71cSCyrill Gorcunov 
10661da177e4SLinus Torvalds /*
106772866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
106872866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
106972866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
10701da177e4SLinus Torvalds  */
1071044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
10721da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
10731da177e4SLinus Torvalds {
10749e16b7fbSHugh Dickins 	struct page *swapcache;
107572835c86SJohannes Weiner 	struct mem_cgroup *memcg;
1076044d66c1SHugh Dickins 	spinlock_t *ptl;
1077044d66c1SHugh Dickins 	pte_t *pte;
1078044d66c1SHugh Dickins 	int ret = 1;
1079044d66c1SHugh Dickins 
10809e16b7fbSHugh Dickins 	swapcache = page;
10819e16b7fbSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, addr);
10829e16b7fbSHugh Dickins 	if (unlikely(!page))
10839e16b7fbSHugh Dickins 		return -ENOMEM;
10849e16b7fbSHugh Dickins 
108572835c86SJohannes Weiner 	if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
108672835c86SJohannes Weiner 					 GFP_KERNEL, &memcg)) {
1087044d66c1SHugh Dickins 		ret = -ENOMEM;
108885d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
108985d9fc89SKAMEZAWA Hiroyuki 	}
1090044d66c1SHugh Dickins 
1091044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
1092179ef71cSCyrill Gorcunov 	if (unlikely(!maybe_same_pte(*pte, swp_entry_to_pte(entry)))) {
109372835c86SJohannes Weiner 		mem_cgroup_cancel_charge_swapin(memcg);
1094044d66c1SHugh Dickins 		ret = 0;
1095044d66c1SHugh Dickins 		goto out;
1096044d66c1SHugh Dickins 	}
10978a9f3ccdSBalbir Singh 
1098b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
1099d559db08SKAMEZAWA Hiroyuki 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
11001da177e4SLinus Torvalds 	get_page(page);
11011da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
11021da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
11039e16b7fbSHugh Dickins 	if (page == swapcache)
11041da177e4SLinus Torvalds 		page_add_anon_rmap(page, vma, addr);
11059e16b7fbSHugh Dickins 	else /* ksm created a completely new copy */
11069e16b7fbSHugh Dickins 		page_add_new_anon_rmap(page, vma, addr);
110772835c86SJohannes Weiner 	mem_cgroup_commit_charge_swapin(page, memcg);
11081da177e4SLinus Torvalds 	swap_free(entry);
11091da177e4SLinus Torvalds 	/*
11101da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
11111da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
11121da177e4SLinus Torvalds 	 */
11131da177e4SLinus Torvalds 	activate_page(page);
1114044d66c1SHugh Dickins out:
1115044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
111685d9fc89SKAMEZAWA Hiroyuki out_nolock:
11179e16b7fbSHugh Dickins 	if (page != swapcache) {
11189e16b7fbSHugh Dickins 		unlock_page(page);
11199e16b7fbSHugh Dickins 		put_page(page);
11209e16b7fbSHugh Dickins 	}
1121044d66c1SHugh Dickins 	return ret;
11221da177e4SLinus Torvalds }
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
11251da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
11261da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
11271da177e4SLinus Torvalds {
11281da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
1129705e87c0SHugh Dickins 	pte_t *pte;
11308a9f3ccdSBalbir Singh 	int ret = 0;
11311da177e4SLinus Torvalds 
1132044d66c1SHugh Dickins 	/*
1133044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
1134044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
1135044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
1136044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
1137044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
1138044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
1139044d66c1SHugh Dickins 	 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
1140044d66c1SHugh Dickins 	 */
1141044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
11421da177e4SLinus Torvalds 	do {
11431da177e4SLinus Torvalds 		/*
11441da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
11451da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
11461da177e4SLinus Torvalds 		 */
1147179ef71cSCyrill Gorcunov 		if (unlikely(maybe_same_pte(*pte, swp_pte))) {
1148044d66c1SHugh Dickins 			pte_unmap(pte);
1149044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
1150044d66c1SHugh Dickins 			if (ret)
1151044d66c1SHugh Dickins 				goto out;
1152044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
11531da177e4SLinus Torvalds 		}
11541da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1155044d66c1SHugh Dickins 	pte_unmap(pte - 1);
1156044d66c1SHugh Dickins out:
11578a9f3ccdSBalbir Singh 	return ret;
11581da177e4SLinus Torvalds }
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
11611da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
11621da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
11631da177e4SLinus Torvalds {
11641da177e4SLinus Torvalds 	pmd_t *pmd;
11651da177e4SLinus Torvalds 	unsigned long next;
11668a9f3ccdSBalbir Singh 	int ret;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
11691da177e4SLinus Torvalds 	do {
11701da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
11711a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
11721da177e4SLinus Torvalds 			continue;
11738a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
11748a9f3ccdSBalbir Singh 		if (ret)
11758a9f3ccdSBalbir Singh 			return ret;
11761da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
11771da177e4SLinus Torvalds 	return 0;
11781da177e4SLinus Torvalds }
11791da177e4SLinus Torvalds 
11801da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
11811da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
11821da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
11831da177e4SLinus Torvalds {
11841da177e4SLinus Torvalds 	pud_t *pud;
11851da177e4SLinus Torvalds 	unsigned long next;
11868a9f3ccdSBalbir Singh 	int ret;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds 	pud = pud_offset(pgd, addr);
11891da177e4SLinus Torvalds 	do {
11901da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
11911da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
11921da177e4SLinus Torvalds 			continue;
11938a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
11948a9f3ccdSBalbir Singh 		if (ret)
11958a9f3ccdSBalbir Singh 			return ret;
11961da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
11971da177e4SLinus Torvalds 	return 0;
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
12011da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
12021da177e4SLinus Torvalds {
12031da177e4SLinus Torvalds 	pgd_t *pgd;
12041da177e4SLinus Torvalds 	unsigned long addr, end, next;
12058a9f3ccdSBalbir Singh 	int ret;
12061da177e4SLinus Torvalds 
12073ca7b3c5SHugh Dickins 	if (page_anon_vma(page)) {
12081da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
12091da177e4SLinus Torvalds 		if (addr == -EFAULT)
12101da177e4SLinus Torvalds 			return 0;
12111da177e4SLinus Torvalds 		else
12121da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
12131da177e4SLinus Torvalds 	} else {
12141da177e4SLinus Torvalds 		addr = vma->vm_start;
12151da177e4SLinus Torvalds 		end = vma->vm_end;
12161da177e4SLinus Torvalds 	}
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
12191da177e4SLinus Torvalds 	do {
12201da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
12211da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
12221da177e4SLinus Torvalds 			continue;
12238a9f3ccdSBalbir Singh 		ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
12248a9f3ccdSBalbir Singh 		if (ret)
12258a9f3ccdSBalbir Singh 			return ret;
12261da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
12271da177e4SLinus Torvalds 	return 0;
12281da177e4SLinus Torvalds }
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
12311da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
12321da177e4SLinus Torvalds {
12331da177e4SLinus Torvalds 	struct vm_area_struct *vma;
12348a9f3ccdSBalbir Singh 	int ret = 0;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
12371da177e4SLinus Torvalds 		/*
12387d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
12397d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
12401da177e4SLinus Torvalds 		 */
1241c475a8abSHugh Dickins 		activate_page(page);
12421da177e4SLinus Torvalds 		unlock_page(page);
12431da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
12441da177e4SLinus Torvalds 		lock_page(page);
12451da177e4SLinus Torvalds 	}
12461da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
12478a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
12481da177e4SLinus Torvalds 			break;
12491da177e4SLinus Torvalds 	}
12501da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
12518a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /*
125538b5faf4SDan Magenheimer  * Scan swap_map (or frontswap_map if frontswap parameter is true)
125638b5faf4SDan Magenheimer  * from current position to next entry still in use.
12571da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
12581da177e4SLinus Torvalds  */
12596eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
126038b5faf4SDan Magenheimer 					unsigned int prev, bool frontswap)
12611da177e4SLinus Torvalds {
12626eb396dcSHugh Dickins 	unsigned int max = si->max;
12636eb396dcSHugh Dickins 	unsigned int i = prev;
12648d69aaeeSHugh Dickins 	unsigned char count;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 	/*
12675d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
12681da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
12691da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
12705d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
12711da177e4SLinus Torvalds 	 */
12721da177e4SLinus Torvalds 	for (;;) {
12731da177e4SLinus Torvalds 		if (++i >= max) {
12741da177e4SLinus Torvalds 			if (!prev) {
12751da177e4SLinus Torvalds 				i = 0;
12761da177e4SLinus Torvalds 				break;
12771da177e4SLinus Torvalds 			}
12781da177e4SLinus Torvalds 			/*
12791da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
12801da177e4SLinus Torvalds 			 * loop back to start and recheck there.
12811da177e4SLinus Torvalds 			 */
12821da177e4SLinus Torvalds 			max = prev + 1;
12831da177e4SLinus Torvalds 			prev = 0;
12841da177e4SLinus Torvalds 			i = 1;
12851da177e4SLinus Torvalds 		}
128638b5faf4SDan Magenheimer 		if (frontswap) {
128738b5faf4SDan Magenheimer 			if (frontswap_test(si, i))
128838b5faf4SDan Magenheimer 				break;
128938b5faf4SDan Magenheimer 			else
129038b5faf4SDan Magenheimer 				continue;
129138b5faf4SDan Magenheimer 		}
1292*edfe23daSShaohua Li 		count = ACCESS_ONCE(si->swap_map[i]);
1293355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
12941da177e4SLinus Torvalds 			break;
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 	return i;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds /*
13001da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
13011da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
13021da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
130338b5faf4SDan Magenheimer  *
130438b5faf4SDan Magenheimer  * if the boolean frontswap is true, only unuse pages_to_unuse pages;
130538b5faf4SDan Magenheimer  * pages_to_unuse==0 means all pages; ignored if frontswap is false
13061da177e4SLinus Torvalds  */
130738b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap,
130838b5faf4SDan Magenheimer 		 unsigned long pages_to_unuse)
13091da177e4SLinus Torvalds {
1310efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
13111da177e4SLinus Torvalds 	struct mm_struct *start_mm;
1312*edfe23daSShaohua Li 	volatile unsigned char *swap_map; /* swap_map is accessed without
1313*edfe23daSShaohua Li 					   * locking. Mark it as volatile
1314*edfe23daSShaohua Li 					   * to prevent compiler doing
1315*edfe23daSShaohua Li 					   * something odd.
1316*edfe23daSShaohua Li 					   */
13178d69aaeeSHugh Dickins 	unsigned char swcount;
13181da177e4SLinus Torvalds 	struct page *page;
13191da177e4SLinus Torvalds 	swp_entry_t entry;
13206eb396dcSHugh Dickins 	unsigned int i = 0;
13211da177e4SLinus Torvalds 	int retval = 0;
13221da177e4SLinus Torvalds 
13231da177e4SLinus Torvalds 	/*
13241da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
13251da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
13261da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
13271da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
13281da177e4SLinus Torvalds 	 *
13291da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
13301da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
13311da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
13321da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
13331da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
13341da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
1335570a335bSHugh Dickins 	 * that.
13361da177e4SLinus Torvalds 	 */
13371da177e4SLinus Torvalds 	start_mm = &init_mm;
13381da177e4SLinus Torvalds 	atomic_inc(&init_mm.mm_users);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	/*
13411da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
13421da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
13431da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
13441da177e4SLinus Torvalds 	 */
134538b5faf4SDan Magenheimer 	while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
13461da177e4SLinus Torvalds 		if (signal_pending(current)) {
13471da177e4SLinus Torvalds 			retval = -EINTR;
13481da177e4SLinus Torvalds 			break;
13491da177e4SLinus Torvalds 		}
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 		/*
13521da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
13531da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
13541da177e4SLinus Torvalds 		 * page and read the swap into it.
13551da177e4SLinus Torvalds 		 */
13561da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
13571da177e4SLinus Torvalds 		entry = swp_entry(type, i);
135802098feaSHugh Dickins 		page = read_swap_cache_async(entry,
135902098feaSHugh Dickins 					GFP_HIGHUSER_MOVABLE, NULL, 0);
13601da177e4SLinus Torvalds 		if (!page) {
13611da177e4SLinus Torvalds 			/*
13621da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
13631da177e4SLinus Torvalds 			 * has been freed independently, and will not be
13641da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
13651da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
13661da177e4SLinus Torvalds 			 */
1367*edfe23daSShaohua Li 			swcount = *swap_map;
1368*edfe23daSShaohua Li 			/*
1369*edfe23daSShaohua Li 			 * We don't hold lock here, so the swap entry could be
1370*edfe23daSShaohua Li 			 * SWAP_MAP_BAD (when the cluster is discarding).
1371*edfe23daSShaohua Li 			 * Instead of fail out, We can just skip the swap
1372*edfe23daSShaohua Li 			 * entry because swapoff will wait for discarding
1373*edfe23daSShaohua Li 			 * finish anyway.
1374*edfe23daSShaohua Li 			 */
1375*edfe23daSShaohua Li 			if (!swcount || swcount == SWAP_MAP_BAD)
13761da177e4SLinus Torvalds 				continue;
13771da177e4SLinus Torvalds 			retval = -ENOMEM;
13781da177e4SLinus Torvalds 			break;
13791da177e4SLinus Torvalds 		}
13801da177e4SLinus Torvalds 
13811da177e4SLinus Torvalds 		/*
13821da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
13831da177e4SLinus Torvalds 		 */
13841da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
13851da177e4SLinus Torvalds 			mmput(start_mm);
13861da177e4SLinus Torvalds 			start_mm = &init_mm;
13871da177e4SLinus Torvalds 			atomic_inc(&init_mm.mm_users);
13881da177e4SLinus Torvalds 		}
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 		/*
13911da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
13921da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
13931da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
13941da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
13951da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
13961da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
13971da177e4SLinus Torvalds 		 */
13981da177e4SLinus Torvalds 		wait_on_page_locked(page);
13991da177e4SLinus Torvalds 		wait_on_page_writeback(page);
14001da177e4SLinus Torvalds 		lock_page(page);
14011da177e4SLinus Torvalds 		wait_on_page_writeback(page);
14021da177e4SLinus Torvalds 
14031da177e4SLinus Torvalds 		/*
14041da177e4SLinus Torvalds 		 * Remove all references to entry.
14051da177e4SLinus Torvalds 		 */
14061da177e4SLinus Torvalds 		swcount = *swap_map;
1407aaa46865SHugh Dickins 		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1408aaa46865SHugh Dickins 			retval = shmem_unuse(entry, page);
1409aaa46865SHugh Dickins 			/* page has already been unlocked and released */
1410aaa46865SHugh Dickins 			if (retval < 0)
1411aaa46865SHugh Dickins 				break;
1412aaa46865SHugh Dickins 			continue;
14131da177e4SLinus Torvalds 		}
1414aaa46865SHugh Dickins 		if (swap_count(swcount) && start_mm != &init_mm)
1415aaa46865SHugh Dickins 			retval = unuse_mm(start_mm, entry, page);
1416aaa46865SHugh Dickins 
1417355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
14181da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
14191da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
14201da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
14211da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
14221da177e4SLinus Torvalds 			struct mm_struct *mm;
14231da177e4SLinus Torvalds 
14241da177e4SLinus Torvalds 			atomic_inc(&new_start_mm->mm_users);
14251da177e4SLinus Torvalds 			atomic_inc(&prev_mm->mm_users);
14261da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
1427aaa46865SHugh Dickins 			while (swap_count(*swap_map) && !retval &&
14281da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
14291da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
143070af7c5cSHugh Dickins 				if (!atomic_inc_not_zero(&mm->mm_users))
14311da177e4SLinus Torvalds 					continue;
14321da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
14331da177e4SLinus Torvalds 				mmput(prev_mm);
14341da177e4SLinus Torvalds 				prev_mm = mm;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds 				cond_resched();
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 				swcount = *swap_map;
1439355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
14401da177e4SLinus Torvalds 					;
1441aaa46865SHugh Dickins 				else if (mm == &init_mm)
14421da177e4SLinus Torvalds 					set_start_mm = 1;
1443aaa46865SHugh Dickins 				else
14441da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
1445355cfa73SKAMEZAWA Hiroyuki 
144632c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
14471da177e4SLinus Torvalds 					mmput(new_start_mm);
14481da177e4SLinus Torvalds 					atomic_inc(&mm->mm_users);
14491da177e4SLinus Torvalds 					new_start_mm = mm;
14501da177e4SLinus Torvalds 					set_start_mm = 0;
14511da177e4SLinus Torvalds 				}
14521da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
14531da177e4SLinus Torvalds 			}
14541da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
14551da177e4SLinus Torvalds 			mmput(prev_mm);
14561da177e4SLinus Torvalds 			mmput(start_mm);
14571da177e4SLinus Torvalds 			start_mm = new_start_mm;
14581da177e4SLinus Torvalds 		}
14591da177e4SLinus Torvalds 		if (retval) {
14601da177e4SLinus Torvalds 			unlock_page(page);
14611da177e4SLinus Torvalds 			page_cache_release(page);
14621da177e4SLinus Torvalds 			break;
14631da177e4SLinus Torvalds 		}
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 		/*
14661da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
14671da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
14681da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
14691da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
14701da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
14711da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
14721da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
14731da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
14741da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
14751da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
14761da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
14775ad64688SHugh Dickins 		 *
14785ad64688SHugh Dickins 		 * Given how unuse_vma() targets one particular offset
14795ad64688SHugh Dickins 		 * in an anon_vma, once the anon_vma has been determined,
14805ad64688SHugh Dickins 		 * this splitting happens to be just what is needed to
14815ad64688SHugh Dickins 		 * handle where KSM pages have been swapped out: re-reading
14825ad64688SHugh Dickins 		 * is unnecessarily slow, but we can fix that later on.
14831da177e4SLinus Torvalds 		 */
1484355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
1485355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
14861da177e4SLinus Torvalds 			struct writeback_control wbc = {
14871da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
14881da177e4SLinus Torvalds 			};
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds 			swap_writepage(page, &wbc);
14911da177e4SLinus Torvalds 			lock_page(page);
14921da177e4SLinus Torvalds 			wait_on_page_writeback(page);
14931da177e4SLinus Torvalds 		}
149468bdc8d6SHugh Dickins 
149568bdc8d6SHugh Dickins 		/*
149668bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
149768bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
149868bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
149968bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
150068bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
150168bdc8d6SHugh Dickins 		 */
150268bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
150368bdc8d6SHugh Dickins 		    likely(page_private(page) == entry.val))
15041da177e4SLinus Torvalds 			delete_from_swap_cache(page);
15051da177e4SLinus Torvalds 
15061da177e4SLinus Torvalds 		/*
15071da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
15081da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
15092706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
15101da177e4SLinus Torvalds 		 */
15111da177e4SLinus Torvalds 		SetPageDirty(page);
15121da177e4SLinus Torvalds 		unlock_page(page);
15131da177e4SLinus Torvalds 		page_cache_release(page);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 		/*
15161da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
15171da177e4SLinus Torvalds 		 * interactive performance.
15181da177e4SLinus Torvalds 		 */
15191da177e4SLinus Torvalds 		cond_resched();
152038b5faf4SDan Magenheimer 		if (frontswap && pages_to_unuse > 0) {
152138b5faf4SDan Magenheimer 			if (!--pages_to_unuse)
152238b5faf4SDan Magenheimer 				break;
152338b5faf4SDan Magenheimer 		}
15241da177e4SLinus Torvalds 	}
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	mmput(start_mm);
15271da177e4SLinus Torvalds 	return retval;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15301da177e4SLinus Torvalds /*
15315d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
15325d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
15335d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
15341da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
15351da177e4SLinus Torvalds  */
15361da177e4SLinus Torvalds static void drain_mmlist(void)
15371da177e4SLinus Torvalds {
15381da177e4SLinus Torvalds 	struct list_head *p, *next;
1539efa90a98SHugh Dickins 	unsigned int type;
15401da177e4SLinus Torvalds 
1541efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
1542efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
15431da177e4SLinus Torvalds 			return;
15441da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
15451da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
15461da177e4SLinus Torvalds 		list_del_init(p);
15471da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
15481da177e4SLinus Torvalds }
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /*
15511da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1552d4906e1aSLee Schermerhorn  * corresponds to page offset for the specified swap entry.
1553d4906e1aSLee Schermerhorn  * Note that the type of this function is sector_t, but it returns page offset
1554d4906e1aSLee Schermerhorn  * into the bdev, not sector offset.
15551da177e4SLinus Torvalds  */
1556d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
15571da177e4SLinus Torvalds {
1558f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
1559f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
1560f29ad6a9SHugh Dickins 	struct swap_extent *se;
1561f29ad6a9SHugh Dickins 	pgoff_t offset;
1562f29ad6a9SHugh Dickins 
1563efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
1564f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
1565f29ad6a9SHugh Dickins 
1566f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
1567f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
1568f29ad6a9SHugh Dickins 	se = start_se;
15691da177e4SLinus Torvalds 
15701da177e4SLinus Torvalds 	for ( ; ; ) {
15711da177e4SLinus Torvalds 		struct list_head *lh;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 		if (se->start_page <= offset &&
15741da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
15751da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
15761da177e4SLinus Torvalds 		}
157711d31886SHugh Dickins 		lh = se->list.next;
15781da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
15791da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
15801da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
15811da177e4SLinus Torvalds 	}
15821da177e4SLinus Torvalds }
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds /*
1585d4906e1aSLee Schermerhorn  * Returns the page offset into bdev for the specified page's swap entry.
1586d4906e1aSLee Schermerhorn  */
1587d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev)
1588d4906e1aSLee Schermerhorn {
1589d4906e1aSLee Schermerhorn 	swp_entry_t entry;
1590d4906e1aSLee Schermerhorn 	entry.val = page_private(page);
1591d4906e1aSLee Schermerhorn 	return map_swap_entry(entry, bdev);
1592d4906e1aSLee Schermerhorn }
1593d4906e1aSLee Schermerhorn 
1594d4906e1aSLee Schermerhorn /*
15951da177e4SLinus Torvalds  * Free all of a swapdev's extent information
15961da177e4SLinus Torvalds  */
15971da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
15981da177e4SLinus Torvalds {
15999625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
16001da177e4SLinus Torvalds 		struct swap_extent *se;
16011da177e4SLinus Torvalds 
16029625a5f2SHugh Dickins 		se = list_entry(sis->first_swap_extent.list.next,
16031da177e4SLinus Torvalds 				struct swap_extent, list);
16041da177e4SLinus Torvalds 		list_del(&se->list);
16051da177e4SLinus Torvalds 		kfree(se);
16061da177e4SLinus Torvalds 	}
160762c230bcSMel Gorman 
160862c230bcSMel Gorman 	if (sis->flags & SWP_FILE) {
160962c230bcSMel Gorman 		struct file *swap_file = sis->swap_file;
161062c230bcSMel Gorman 		struct address_space *mapping = swap_file->f_mapping;
161162c230bcSMel Gorman 
161262c230bcSMel Gorman 		sis->flags &= ~SWP_FILE;
161362c230bcSMel Gorman 		mapping->a_ops->swap_deactivate(swap_file);
161462c230bcSMel Gorman 	}
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds /*
16181da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
161911d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
16201da177e4SLinus Torvalds  *
162111d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
16221da177e4SLinus Torvalds  */
1623a509bc1aSMel Gorman int
16241da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
16251da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
16261da177e4SLinus Torvalds {
16271da177e4SLinus Torvalds 	struct swap_extent *se;
16281da177e4SLinus Torvalds 	struct swap_extent *new_se;
16291da177e4SLinus Torvalds 	struct list_head *lh;
16301da177e4SLinus Torvalds 
16319625a5f2SHugh Dickins 	if (start_page == 0) {
16329625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
16339625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
16349625a5f2SHugh Dickins 		se->start_page = 0;
16359625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
16369625a5f2SHugh Dickins 		se->start_block = start_block;
16379625a5f2SHugh Dickins 		return 1;
16389625a5f2SHugh Dickins 	} else {
16399625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
16401da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
164111d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
164211d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
16431da177e4SLinus Torvalds 			/* Merge it */
16441da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
16451da177e4SLinus Torvalds 			return 0;
16461da177e4SLinus Torvalds 		}
16471da177e4SLinus Torvalds 	}
16481da177e4SLinus Torvalds 
16491da177e4SLinus Torvalds 	/*
16501da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
16511da177e4SLinus Torvalds 	 */
16521da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
16531da177e4SLinus Torvalds 	if (new_se == NULL)
16541da177e4SLinus Torvalds 		return -ENOMEM;
16551da177e4SLinus Torvalds 	new_se->start_page = start_page;
16561da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
16571da177e4SLinus Torvalds 	new_se->start_block = start_block;
16581da177e4SLinus Torvalds 
16599625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
166053092a74SHugh Dickins 	return 1;
16611da177e4SLinus Torvalds }
16621da177e4SLinus Torvalds 
16631da177e4SLinus Torvalds /*
16641da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
16651da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
16661da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
16671da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
16681da177e4SLinus Torvalds  *
16691da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
16701da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
16711da177e4SLinus Torvalds  * swap files identically.
16721da177e4SLinus Torvalds  *
16731da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
16741da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
16751da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
16761da177e4SLinus Torvalds  *
16771da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
16781da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
16791da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
16801da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
16811da177e4SLinus Torvalds  * for swapping.
16821da177e4SLinus Torvalds  *
1683b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
16841da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
16851da177e4SLinus Torvalds  * which will scribble on the fs.
16861da177e4SLinus Torvalds  *
16871da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
16881da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
16891da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
16901da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
16911da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
16921da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
16931da177e4SLinus Torvalds  */
169453092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
16951da177e4SLinus Torvalds {
169662c230bcSMel Gorman 	struct file *swap_file = sis->swap_file;
169762c230bcSMel Gorman 	struct address_space *mapping = swap_file->f_mapping;
169862c230bcSMel Gorman 	struct inode *inode = mapping->host;
16991da177e4SLinus Torvalds 	int ret;
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
17021da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
170353092a74SHugh Dickins 		*span = sis->pages;
1704a509bc1aSMel Gorman 		return ret;
17051da177e4SLinus Torvalds 	}
17061da177e4SLinus Torvalds 
170762c230bcSMel Gorman 	if (mapping->a_ops->swap_activate) {
1708a509bc1aSMel Gorman 		ret = mapping->a_ops->swap_activate(sis, swap_file, span);
170962c230bcSMel Gorman 		if (!ret) {
171062c230bcSMel Gorman 			sis->flags |= SWP_FILE;
171162c230bcSMel Gorman 			ret = add_swap_extent(sis, 0, sis->max, 0);
171262c230bcSMel Gorman 			*span = sis->pages;
171362c230bcSMel Gorman 		}
17149625a5f2SHugh Dickins 		return ret;
1715a509bc1aSMel Gorman 	}
1716a509bc1aSMel Gorman 
1717a509bc1aSMel Gorman 	return generic_swapfile_activate(sis, swap_file, span);
17181da177e4SLinus Torvalds }
17191da177e4SLinus Torvalds 
1720cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio,
17212a8f9449SShaohua Li 				unsigned char *swap_map,
17222a8f9449SShaohua Li 				struct swap_cluster_info *cluster_info)
172340531542SCesar Eduardo Barros {
172440531542SCesar Eduardo Barros 	int i, prev;
172540531542SCesar Eduardo Barros 
172640531542SCesar Eduardo Barros 	if (prio >= 0)
172740531542SCesar Eduardo Barros 		p->prio = prio;
172840531542SCesar Eduardo Barros 	else
172940531542SCesar Eduardo Barros 		p->prio = --least_priority;
173040531542SCesar Eduardo Barros 	p->swap_map = swap_map;
17312a8f9449SShaohua Li 	p->cluster_info = cluster_info;
173240531542SCesar Eduardo Barros 	p->flags |= SWP_WRITEOK;
1733ec8acf20SShaohua Li 	atomic_long_add(p->pages, &nr_swap_pages);
173440531542SCesar Eduardo Barros 	total_swap_pages += p->pages;
173540531542SCesar Eduardo Barros 
173640531542SCesar Eduardo Barros 	/* insert swap space into swap_list: */
173740531542SCesar Eduardo Barros 	prev = -1;
173840531542SCesar Eduardo Barros 	for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
173940531542SCesar Eduardo Barros 		if (p->prio >= swap_info[i]->prio)
174040531542SCesar Eduardo Barros 			break;
174140531542SCesar Eduardo Barros 		prev = i;
174240531542SCesar Eduardo Barros 	}
174340531542SCesar Eduardo Barros 	p->next = i;
174440531542SCesar Eduardo Barros 	if (prev < 0)
174540531542SCesar Eduardo Barros 		swap_list.head = swap_list.next = p->type;
174640531542SCesar Eduardo Barros 	else
174740531542SCesar Eduardo Barros 		swap_info[prev]->next = p->type;
1748cf0cac0aSCesar Eduardo Barros }
1749cf0cac0aSCesar Eduardo Barros 
1750cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio,
1751cf0cac0aSCesar Eduardo Barros 				unsigned char *swap_map,
17522a8f9449SShaohua Li 				struct swap_cluster_info *cluster_info,
1753cf0cac0aSCesar Eduardo Barros 				unsigned long *frontswap_map)
1754cf0cac0aSCesar Eduardo Barros {
17554f89849dSMinchan Kim 	frontswap_init(p->type, frontswap_map);
1756cf0cac0aSCesar Eduardo Barros 	spin_lock(&swap_lock);
1757ec8acf20SShaohua Li 	spin_lock(&p->lock);
17582a8f9449SShaohua Li 	 _enable_swap_info(p, prio, swap_map, cluster_info);
1759ec8acf20SShaohua Li 	spin_unlock(&p->lock);
1760cf0cac0aSCesar Eduardo Barros 	spin_unlock(&swap_lock);
1761cf0cac0aSCesar Eduardo Barros }
1762cf0cac0aSCesar Eduardo Barros 
1763cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p)
1764cf0cac0aSCesar Eduardo Barros {
1765cf0cac0aSCesar Eduardo Barros 	spin_lock(&swap_lock);
1766ec8acf20SShaohua Li 	spin_lock(&p->lock);
17672a8f9449SShaohua Li 	_enable_swap_info(p, p->prio, p->swap_map, p->cluster_info);
1768ec8acf20SShaohua Li 	spin_unlock(&p->lock);
176940531542SCesar Eduardo Barros 	spin_unlock(&swap_lock);
177040531542SCesar Eduardo Barros }
177140531542SCesar Eduardo Barros 
1772c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
17758d69aaeeSHugh Dickins 	unsigned char *swap_map;
17762a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info;
17774f89849dSMinchan Kim 	unsigned long *frontswap_map;
17781da177e4SLinus Torvalds 	struct file *swap_file, *victim;
17791da177e4SLinus Torvalds 	struct address_space *mapping;
17801da177e4SLinus Torvalds 	struct inode *inode;
178191a27b2aSJeff Layton 	struct filename *pathname;
17821da177e4SLinus Torvalds 	int i, type, prev;
17831da177e4SLinus Torvalds 	int err;
17841da177e4SLinus Torvalds 
17851da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
17861da177e4SLinus Torvalds 		return -EPERM;
17871da177e4SLinus Torvalds 
1788191c5424SAl Viro 	BUG_ON(!current->mm);
1789191c5424SAl Viro 
17901da177e4SLinus Torvalds 	pathname = getname(specialfile);
17911da177e4SLinus Torvalds 	if (IS_ERR(pathname))
1792f58b59c1SXiaotian Feng 		return PTR_ERR(pathname);
17931da177e4SLinus Torvalds 
1794669abf4eSJeff Layton 	victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
17951da177e4SLinus Torvalds 	err = PTR_ERR(victim);
17961da177e4SLinus Torvalds 	if (IS_ERR(victim))
17971da177e4SLinus Torvalds 		goto out;
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 	mapping = victim->f_mapping;
18001da177e4SLinus Torvalds 	prev = -1;
18015d337b91SHugh Dickins 	spin_lock(&swap_lock);
1802efa90a98SHugh Dickins 	for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1803efa90a98SHugh Dickins 		p = swap_info[type];
180422c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
18051da177e4SLinus Torvalds 			if (p->swap_file->f_mapping == mapping)
18061da177e4SLinus Torvalds 				break;
18071da177e4SLinus Torvalds 		}
18081da177e4SLinus Torvalds 		prev = type;
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds 	if (type < 0) {
18111da177e4SLinus Torvalds 		err = -EINVAL;
18125d337b91SHugh Dickins 		spin_unlock(&swap_lock);
18131da177e4SLinus Torvalds 		goto out_dput;
18141da177e4SLinus Torvalds 	}
1815191c5424SAl Viro 	if (!security_vm_enough_memory_mm(current->mm, p->pages))
18161da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
18171da177e4SLinus Torvalds 	else {
18181da177e4SLinus Torvalds 		err = -ENOMEM;
18195d337b91SHugh Dickins 		spin_unlock(&swap_lock);
18201da177e4SLinus Torvalds 		goto out_dput;
18211da177e4SLinus Torvalds 	}
1822efa90a98SHugh Dickins 	if (prev < 0)
18231da177e4SLinus Torvalds 		swap_list.head = p->next;
1824efa90a98SHugh Dickins 	else
1825efa90a98SHugh Dickins 		swap_info[prev]->next = p->next;
18261da177e4SLinus Torvalds 	if (type == swap_list.next) {
18271da177e4SLinus Torvalds 		/* just pick something that's safe... */
18281da177e4SLinus Torvalds 		swap_list.next = swap_list.head;
18291da177e4SLinus Torvalds 	}
1830ec8acf20SShaohua Li 	spin_lock(&p->lock);
183178ecba08SHugh Dickins 	if (p->prio < 0) {
1832efa90a98SHugh Dickins 		for (i = p->next; i >= 0; i = swap_info[i]->next)
1833efa90a98SHugh Dickins 			swap_info[i]->prio = p->prio--;
183478ecba08SHugh Dickins 		least_priority++;
183578ecba08SHugh Dickins 	}
1836ec8acf20SShaohua Li 	atomic_long_sub(p->pages, &nr_swap_pages);
18371da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
18381da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
1839ec8acf20SShaohua Li 	spin_unlock(&p->lock);
18405d337b91SHugh Dickins 	spin_unlock(&swap_lock);
1841fb4f88dcSHugh Dickins 
1842e1e12d2fSDavid Rientjes 	set_current_oom_origin();
184338b5faf4SDan Magenheimer 	err = try_to_unuse(type, false, 0); /* force all pages to be unused */
1844e1e12d2fSDavid Rientjes 	clear_current_oom_origin();
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	if (err) {
18471da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
1848cf0cac0aSCesar Eduardo Barros 		reinsert_swap_info(p);
18491da177e4SLinus Torvalds 		goto out_dput;
18501da177e4SLinus Torvalds 	}
185152b7efdbSHugh Dickins 
1852815c2c54SShaohua Li 	flush_work(&p->discard_work);
1853815c2c54SShaohua Li 
18544cd3bb10SHugh Dickins 	destroy_swap_extents(p);
1855570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
1856570a335bSHugh Dickins 		free_swap_count_continuations(p);
1857570a335bSHugh Dickins 
1858fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
18595d337b91SHugh Dickins 	spin_lock(&swap_lock);
1860ec8acf20SShaohua Li 	spin_lock(&p->lock);
18611da177e4SLinus Torvalds 	drain_mmlist();
18625d337b91SHugh Dickins 
18635d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
18645d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
18655d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
1866ec8acf20SShaohua Li 		spin_unlock(&p->lock);
18675d337b91SHugh Dickins 		spin_unlock(&swap_lock);
186813e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
18695d337b91SHugh Dickins 		spin_lock(&swap_lock);
1870ec8acf20SShaohua Li 		spin_lock(&p->lock);
18715d337b91SHugh Dickins 	}
18725d337b91SHugh Dickins 
18731da177e4SLinus Torvalds 	swap_file = p->swap_file;
18741da177e4SLinus Torvalds 	p->swap_file = NULL;
18751da177e4SLinus Torvalds 	p->max = 0;
18761da177e4SLinus Torvalds 	swap_map = p->swap_map;
18771da177e4SLinus Torvalds 	p->swap_map = NULL;
18782a8f9449SShaohua Li 	cluster_info = p->cluster_info;
18792a8f9449SShaohua Li 	p->cluster_info = NULL;
18801da177e4SLinus Torvalds 	p->flags = 0;
18814f89849dSMinchan Kim 	frontswap_map = frontswap_map_get(p);
18824f89849dSMinchan Kim 	frontswap_map_set(p, NULL);
1883ec8acf20SShaohua Li 	spin_unlock(&p->lock);
18845d337b91SHugh Dickins 	spin_unlock(&swap_lock);
18854f89849dSMinchan Kim 	frontswap_invalidate_area(type);
1886fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
18871da177e4SLinus Torvalds 	vfree(swap_map);
18882a8f9449SShaohua Li 	vfree(cluster_info);
18894f89849dSMinchan Kim 	vfree(frontswap_map);
189027a7faa0SKAMEZAWA Hiroyuki 	/* Destroy swap account informatin */
189127a7faa0SKAMEZAWA Hiroyuki 	swap_cgroup_swapoff(type);
189227a7faa0SKAMEZAWA Hiroyuki 
18931da177e4SLinus Torvalds 	inode = mapping->host;
18941da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
18951da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
18961da177e4SLinus Torvalds 		set_blocksize(bdev, p->old_block_size);
1897e525fd89STejun Heo 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
18981da177e4SLinus Torvalds 	} else {
18991b1dcc1bSJes Sorensen 		mutex_lock(&inode->i_mutex);
19001da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
19011b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
19021da177e4SLinus Torvalds 	}
19031da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
19041da177e4SLinus Torvalds 	err = 0;
190566d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
190666d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
19071da177e4SLinus Torvalds 
19081da177e4SLinus Torvalds out_dput:
19091da177e4SLinus Torvalds 	filp_close(victim, NULL);
19101da177e4SLinus Torvalds out:
1911f58b59c1SXiaotian Feng 	putname(pathname);
19121da177e4SLinus Torvalds 	return err;
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds 
19151da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
191666d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait)
191766d7dd51SKay Sievers {
1918f1514638SKay Sievers 	struct seq_file *seq = file->private_data;
191966d7dd51SKay Sievers 
192066d7dd51SKay Sievers 	poll_wait(file, &proc_poll_wait, wait);
192166d7dd51SKay Sievers 
1922f1514638SKay Sievers 	if (seq->poll_event != atomic_read(&proc_poll_event)) {
1923f1514638SKay Sievers 		seq->poll_event = atomic_read(&proc_poll_event);
192466d7dd51SKay Sievers 		return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
192566d7dd51SKay Sievers 	}
192666d7dd51SKay Sievers 
192766d7dd51SKay Sievers 	return POLLIN | POLLRDNORM;
192866d7dd51SKay Sievers }
192966d7dd51SKay Sievers 
19301da177e4SLinus Torvalds /* iterator */
19311da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
19321da177e4SLinus Torvalds {
1933efa90a98SHugh Dickins 	struct swap_info_struct *si;
1934efa90a98SHugh Dickins 	int type;
19351da177e4SLinus Torvalds 	loff_t l = *pos;
19361da177e4SLinus Torvalds 
1937fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
19381da177e4SLinus Torvalds 
1939881e4aabSSuleiman Souhlal 	if (!l)
1940881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
1941881e4aabSSuleiman Souhlal 
1942efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1943efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1944efa90a98SHugh Dickins 		si = swap_info[type];
1945efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
19461da177e4SLinus Torvalds 			continue;
1947881e4aabSSuleiman Souhlal 		if (!--l)
1948efa90a98SHugh Dickins 			return si;
19491da177e4SLinus Torvalds 	}
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds 	return NULL;
19521da177e4SLinus Torvalds }
19531da177e4SLinus Torvalds 
19541da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
19551da177e4SLinus Torvalds {
1956efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
1957efa90a98SHugh Dickins 	int type;
19581da177e4SLinus Torvalds 
1959881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
1960efa90a98SHugh Dickins 		type = 0;
1961efa90a98SHugh Dickins 	else
1962efa90a98SHugh Dickins 		type = si->type + 1;
1963881e4aabSSuleiman Souhlal 
1964efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
1965efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
1966efa90a98SHugh Dickins 		si = swap_info[type];
1967efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
19681da177e4SLinus Torvalds 			continue;
19691da177e4SLinus Torvalds 		++*pos;
1970efa90a98SHugh Dickins 		return si;
19711da177e4SLinus Torvalds 	}
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds 	return NULL;
19741da177e4SLinus Torvalds }
19751da177e4SLinus Torvalds 
19761da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
19771da177e4SLinus Torvalds {
1978fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
19791da177e4SLinus Torvalds }
19801da177e4SLinus Torvalds 
19811da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
19821da177e4SLinus Torvalds {
1983efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
19841da177e4SLinus Torvalds 	struct file *file;
19851da177e4SLinus Torvalds 	int len;
19861da177e4SLinus Torvalds 
1987efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
19881da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1989881e4aabSSuleiman Souhlal 		return 0;
1990881e4aabSSuleiman Souhlal 	}
19911da177e4SLinus Torvalds 
1992efa90a98SHugh Dickins 	file = si->swap_file;
1993c32c2f63SJan Blunck 	len = seq_path(swap, &file->f_path, " \t\n\\");
19946eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
19951da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
1996496ad9aaSAl Viro 			S_ISBLK(file_inode(file)->i_mode) ?
19971da177e4SLinus Torvalds 				"partition" : "file\t",
1998efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
1999efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
2000efa90a98SHugh Dickins 			si->prio);
20011da177e4SLinus Torvalds 	return 0;
20021da177e4SLinus Torvalds }
20031da177e4SLinus Torvalds 
200415ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
20051da177e4SLinus Torvalds 	.start =	swap_start,
20061da177e4SLinus Torvalds 	.next =		swap_next,
20071da177e4SLinus Torvalds 	.stop =		swap_stop,
20081da177e4SLinus Torvalds 	.show =		swap_show
20091da177e4SLinus Torvalds };
20101da177e4SLinus Torvalds 
20111da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
20121da177e4SLinus Torvalds {
2013f1514638SKay Sievers 	struct seq_file *seq;
201466d7dd51SKay Sievers 	int ret;
201566d7dd51SKay Sievers 
201666d7dd51SKay Sievers 	ret = seq_open(file, &swaps_op);
2017f1514638SKay Sievers 	if (ret)
201866d7dd51SKay Sievers 		return ret;
201966d7dd51SKay Sievers 
2020f1514638SKay Sievers 	seq = file->private_data;
2021f1514638SKay Sievers 	seq->poll_event = atomic_read(&proc_poll_event);
2022f1514638SKay Sievers 	return 0;
20231da177e4SLinus Torvalds }
20241da177e4SLinus Torvalds 
202515ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
20261da177e4SLinus Torvalds 	.open		= swaps_open,
20271da177e4SLinus Torvalds 	.read		= seq_read,
20281da177e4SLinus Torvalds 	.llseek		= seq_lseek,
20291da177e4SLinus Torvalds 	.release	= seq_release,
203066d7dd51SKay Sievers 	.poll		= swaps_poll,
20311da177e4SLinus Torvalds };
20321da177e4SLinus Torvalds 
20331da177e4SLinus Torvalds static int __init procswaps_init(void)
20341da177e4SLinus Torvalds {
20353d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
20361da177e4SLinus Torvalds 	return 0;
20371da177e4SLinus Torvalds }
20381da177e4SLinus Torvalds __initcall(procswaps_init);
20391da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
20401da177e4SLinus Torvalds 
20411796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
20421796316aSJan Beulich static int __init max_swapfiles_check(void)
20431796316aSJan Beulich {
20441796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
20451796316aSJan Beulich 	return 0;
20461796316aSJan Beulich }
20471796316aSJan Beulich late_initcall(max_swapfiles_check);
20481796316aSJan Beulich #endif
20491796316aSJan Beulich 
205053cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void)
20511da177e4SLinus Torvalds {
20521da177e4SLinus Torvalds 	struct swap_info_struct *p;
20531da177e4SLinus Torvalds 	unsigned int type;
2054efa90a98SHugh Dickins 
2055efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2056efa90a98SHugh Dickins 	if (!p)
205753cbb243SCesar Eduardo Barros 		return ERR_PTR(-ENOMEM);
2058efa90a98SHugh Dickins 
20595d337b91SHugh Dickins 	spin_lock(&swap_lock);
2060efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2061efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
20621da177e4SLinus Torvalds 			break;
2063efa90a98SHugh Dickins 	}
20640697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
20655d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2066efa90a98SHugh Dickins 		kfree(p);
2067730c0581SCesar Eduardo Barros 		return ERR_PTR(-EPERM);
20681da177e4SLinus Torvalds 	}
2069efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
2070efa90a98SHugh Dickins 		p->type = type;
2071efa90a98SHugh Dickins 		swap_info[type] = p;
2072efa90a98SHugh Dickins 		/*
2073efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
2074efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
2075efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
2076efa90a98SHugh Dickins 		 */
2077efa90a98SHugh Dickins 		smp_wmb();
2078efa90a98SHugh Dickins 		nr_swapfiles++;
2079efa90a98SHugh Dickins 	} else {
2080efa90a98SHugh Dickins 		kfree(p);
2081efa90a98SHugh Dickins 		p = swap_info[type];
2082efa90a98SHugh Dickins 		/*
2083efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
2084efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
2085efa90a98SHugh Dickins 		 */
2086efa90a98SHugh Dickins 	}
20879625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
20881da177e4SLinus Torvalds 	p->flags = SWP_USED;
20891da177e4SLinus Torvalds 	p->next = -1;
20905d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2091ec8acf20SShaohua Li 	spin_lock_init(&p->lock);
2092efa90a98SHugh Dickins 
209353cbb243SCesar Eduardo Barros 	return p;
209453cbb243SCesar Eduardo Barros }
209553cbb243SCesar Eduardo Barros 
20964d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
20974d0e1e10SCesar Eduardo Barros {
20984d0e1e10SCesar Eduardo Barros 	int error;
20994d0e1e10SCesar Eduardo Barros 
21004d0e1e10SCesar Eduardo Barros 	if (S_ISBLK(inode->i_mode)) {
21014d0e1e10SCesar Eduardo Barros 		p->bdev = bdgrab(I_BDEV(inode));
21024d0e1e10SCesar Eduardo Barros 		error = blkdev_get(p->bdev,
21034d0e1e10SCesar Eduardo Barros 				   FMODE_READ | FMODE_WRITE | FMODE_EXCL,
21044d0e1e10SCesar Eduardo Barros 				   sys_swapon);
21054d0e1e10SCesar Eduardo Barros 		if (error < 0) {
21064d0e1e10SCesar Eduardo Barros 			p->bdev = NULL;
210787ade72aSCesar Eduardo Barros 			return -EINVAL;
21084d0e1e10SCesar Eduardo Barros 		}
21094d0e1e10SCesar Eduardo Barros 		p->old_block_size = block_size(p->bdev);
21104d0e1e10SCesar Eduardo Barros 		error = set_blocksize(p->bdev, PAGE_SIZE);
21114d0e1e10SCesar Eduardo Barros 		if (error < 0)
211287ade72aSCesar Eduardo Barros 			return error;
21134d0e1e10SCesar Eduardo Barros 		p->flags |= SWP_BLKDEV;
21144d0e1e10SCesar Eduardo Barros 	} else if (S_ISREG(inode->i_mode)) {
21154d0e1e10SCesar Eduardo Barros 		p->bdev = inode->i_sb->s_bdev;
21164d0e1e10SCesar Eduardo Barros 		mutex_lock(&inode->i_mutex);
211787ade72aSCesar Eduardo Barros 		if (IS_SWAPFILE(inode))
211887ade72aSCesar Eduardo Barros 			return -EBUSY;
211987ade72aSCesar Eduardo Barros 	} else
212087ade72aSCesar Eduardo Barros 		return -EINVAL;
21214d0e1e10SCesar Eduardo Barros 
21224d0e1e10SCesar Eduardo Barros 	return 0;
21234d0e1e10SCesar Eduardo Barros }
21244d0e1e10SCesar Eduardo Barros 
2125ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p,
2126ca8bd38bSCesar Eduardo Barros 					union swap_header *swap_header,
2127ca8bd38bSCesar Eduardo Barros 					struct inode *inode)
2128ca8bd38bSCesar Eduardo Barros {
2129ca8bd38bSCesar Eduardo Barros 	int i;
2130ca8bd38bSCesar Eduardo Barros 	unsigned long maxpages;
2131ca8bd38bSCesar Eduardo Barros 	unsigned long swapfilepages;
2132d6bbbd29SRaymond Jennings 	unsigned long last_page;
2133ca8bd38bSCesar Eduardo Barros 
2134ca8bd38bSCesar Eduardo Barros 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
2135465c47fdSAndrew Morton 		pr_err("Unable to find swap-space signature\n");
213638719025SCesar Eduardo Barros 		return 0;
2137ca8bd38bSCesar Eduardo Barros 	}
2138ca8bd38bSCesar Eduardo Barros 
2139ca8bd38bSCesar Eduardo Barros 	/* swap partition endianess hack... */
2140ca8bd38bSCesar Eduardo Barros 	if (swab32(swap_header->info.version) == 1) {
2141ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.version);
2142ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.last_page);
2143ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.nr_badpages);
2144ca8bd38bSCesar Eduardo Barros 		for (i = 0; i < swap_header->info.nr_badpages; i++)
2145ca8bd38bSCesar Eduardo Barros 			swab32s(&swap_header->info.badpages[i]);
2146ca8bd38bSCesar Eduardo Barros 	}
2147ca8bd38bSCesar Eduardo Barros 	/* Check the swap header's sub-version */
2148ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.version != 1) {
2149465c47fdSAndrew Morton 		pr_warn("Unable to handle swap header version %d\n",
2150ca8bd38bSCesar Eduardo Barros 			swap_header->info.version);
215138719025SCesar Eduardo Barros 		return 0;
2152ca8bd38bSCesar Eduardo Barros 	}
2153ca8bd38bSCesar Eduardo Barros 
2154ca8bd38bSCesar Eduardo Barros 	p->lowest_bit  = 1;
2155ca8bd38bSCesar Eduardo Barros 	p->cluster_next = 1;
2156ca8bd38bSCesar Eduardo Barros 	p->cluster_nr = 0;
2157ca8bd38bSCesar Eduardo Barros 
2158ca8bd38bSCesar Eduardo Barros 	/*
2159ca8bd38bSCesar Eduardo Barros 	 * Find out how many pages are allowed for a single swap
21609b15b817SHugh Dickins 	 * device. There are two limiting factors: 1) the number
2161a2c16d6cSHugh Dickins 	 * of bits for the swap offset in the swp_entry_t type, and
2162a2c16d6cSHugh Dickins 	 * 2) the number of bits in the swap pte as defined by the
21639b15b817SHugh Dickins 	 * different architectures. In order to find the
2164a2c16d6cSHugh Dickins 	 * largest possible bit mask, a swap entry with swap type 0
2165ca8bd38bSCesar Eduardo Barros 	 * and swap offset ~0UL is created, encoded to a swap pte,
2166a2c16d6cSHugh Dickins 	 * decoded to a swp_entry_t again, and finally the swap
2167ca8bd38bSCesar Eduardo Barros 	 * offset is extracted. This will mask all the bits from
2168ca8bd38bSCesar Eduardo Barros 	 * the initial ~0UL mask that can't be encoded in either
2169ca8bd38bSCesar Eduardo Barros 	 * the swp_entry_t or the architecture definition of a
21709b15b817SHugh Dickins 	 * swap pte.
2171ca8bd38bSCesar Eduardo Barros 	 */
2172ca8bd38bSCesar Eduardo Barros 	maxpages = swp_offset(pte_to_swp_entry(
21739b15b817SHugh Dickins 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2174d6bbbd29SRaymond Jennings 	last_page = swap_header->info.last_page;
2175d6bbbd29SRaymond Jennings 	if (last_page > maxpages) {
2176465c47fdSAndrew Morton 		pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
2177d6bbbd29SRaymond Jennings 			maxpages << (PAGE_SHIFT - 10),
2178d6bbbd29SRaymond Jennings 			last_page << (PAGE_SHIFT - 10));
2179d6bbbd29SRaymond Jennings 	}
2180d6bbbd29SRaymond Jennings 	if (maxpages > last_page) {
2181d6bbbd29SRaymond Jennings 		maxpages = last_page + 1;
2182ca8bd38bSCesar Eduardo Barros 		/* p->max is an unsigned int: don't overflow it */
2183ca8bd38bSCesar Eduardo Barros 		if ((unsigned int)maxpages == 0)
2184ca8bd38bSCesar Eduardo Barros 			maxpages = UINT_MAX;
2185ca8bd38bSCesar Eduardo Barros 	}
2186ca8bd38bSCesar Eduardo Barros 	p->highest_bit = maxpages - 1;
2187ca8bd38bSCesar Eduardo Barros 
2188ca8bd38bSCesar Eduardo Barros 	if (!maxpages)
218938719025SCesar Eduardo Barros 		return 0;
2190ca8bd38bSCesar Eduardo Barros 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
2191ca8bd38bSCesar Eduardo Barros 	if (swapfilepages && maxpages > swapfilepages) {
2192465c47fdSAndrew Morton 		pr_warn("Swap area shorter than signature indicates\n");
219338719025SCesar Eduardo Barros 		return 0;
2194ca8bd38bSCesar Eduardo Barros 	}
2195ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
219638719025SCesar Eduardo Barros 		return 0;
2197ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
219838719025SCesar Eduardo Barros 		return 0;
2199ca8bd38bSCesar Eduardo Barros 
2200ca8bd38bSCesar Eduardo Barros 	return maxpages;
2201ca8bd38bSCesar Eduardo Barros }
2202ca8bd38bSCesar Eduardo Barros 
2203915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p,
2204915d4d7bSCesar Eduardo Barros 					union swap_header *swap_header,
2205915d4d7bSCesar Eduardo Barros 					unsigned char *swap_map,
22062a8f9449SShaohua Li 					struct swap_cluster_info *cluster_info,
2207915d4d7bSCesar Eduardo Barros 					unsigned long maxpages,
2208915d4d7bSCesar Eduardo Barros 					sector_t *span)
2209915d4d7bSCesar Eduardo Barros {
2210915d4d7bSCesar Eduardo Barros 	int i;
2211915d4d7bSCesar Eduardo Barros 	unsigned int nr_good_pages;
2212915d4d7bSCesar Eduardo Barros 	int nr_extents;
22132a8f9449SShaohua Li 	unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
22142a8f9449SShaohua Li 	unsigned long idx = p->cluster_next / SWAPFILE_CLUSTER;
2215915d4d7bSCesar Eduardo Barros 
2216915d4d7bSCesar Eduardo Barros 	nr_good_pages = maxpages - 1;	/* omit header page */
2217915d4d7bSCesar Eduardo Barros 
22182a8f9449SShaohua Li 	cluster_set_null(&p->free_cluster_head);
22192a8f9449SShaohua Li 	cluster_set_null(&p->free_cluster_tail);
2220815c2c54SShaohua Li 	cluster_set_null(&p->discard_cluster_head);
2221815c2c54SShaohua Li 	cluster_set_null(&p->discard_cluster_tail);
22222a8f9449SShaohua Li 
2223915d4d7bSCesar Eduardo Barros 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
2224915d4d7bSCesar Eduardo Barros 		unsigned int page_nr = swap_header->info.badpages[i];
2225bdb8e3f6SCesar Eduardo Barros 		if (page_nr == 0 || page_nr > swap_header->info.last_page)
2226bdb8e3f6SCesar Eduardo Barros 			return -EINVAL;
2227915d4d7bSCesar Eduardo Barros 		if (page_nr < maxpages) {
2228915d4d7bSCesar Eduardo Barros 			swap_map[page_nr] = SWAP_MAP_BAD;
2229915d4d7bSCesar Eduardo Barros 			nr_good_pages--;
22302a8f9449SShaohua Li 			/*
22312a8f9449SShaohua Li 			 * Haven't marked the cluster free yet, no list
22322a8f9449SShaohua Li 			 * operation involved
22332a8f9449SShaohua Li 			 */
22342a8f9449SShaohua Li 			inc_cluster_info_page(p, cluster_info, page_nr);
2235915d4d7bSCesar Eduardo Barros 		}
2236915d4d7bSCesar Eduardo Barros 	}
2237915d4d7bSCesar Eduardo Barros 
22382a8f9449SShaohua Li 	/* Haven't marked the cluster free yet, no list operation involved */
22392a8f9449SShaohua Li 	for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
22402a8f9449SShaohua Li 		inc_cluster_info_page(p, cluster_info, i);
22412a8f9449SShaohua Li 
2242915d4d7bSCesar Eduardo Barros 	if (nr_good_pages) {
2243915d4d7bSCesar Eduardo Barros 		swap_map[0] = SWAP_MAP_BAD;
22442a8f9449SShaohua Li 		/*
22452a8f9449SShaohua Li 		 * Not mark the cluster free yet, no list
22462a8f9449SShaohua Li 		 * operation involved
22472a8f9449SShaohua Li 		 */
22482a8f9449SShaohua Li 		inc_cluster_info_page(p, cluster_info, 0);
2249915d4d7bSCesar Eduardo Barros 		p->max = maxpages;
2250915d4d7bSCesar Eduardo Barros 		p->pages = nr_good_pages;
2251915d4d7bSCesar Eduardo Barros 		nr_extents = setup_swap_extents(p, span);
2252bdb8e3f6SCesar Eduardo Barros 		if (nr_extents < 0)
2253bdb8e3f6SCesar Eduardo Barros 			return nr_extents;
2254915d4d7bSCesar Eduardo Barros 		nr_good_pages = p->pages;
2255915d4d7bSCesar Eduardo Barros 	}
2256915d4d7bSCesar Eduardo Barros 	if (!nr_good_pages) {
2257465c47fdSAndrew Morton 		pr_warn("Empty swap-file\n");
2258bdb8e3f6SCesar Eduardo Barros 		return -EINVAL;
2259915d4d7bSCesar Eduardo Barros 	}
2260915d4d7bSCesar Eduardo Barros 
22612a8f9449SShaohua Li 	if (!cluster_info)
22622a8f9449SShaohua Li 		return nr_extents;
22632a8f9449SShaohua Li 
22642a8f9449SShaohua Li 	for (i = 0; i < nr_clusters; i++) {
22652a8f9449SShaohua Li 		if (!cluster_count(&cluster_info[idx])) {
22662a8f9449SShaohua Li 			cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
22672a8f9449SShaohua Li 			if (cluster_is_null(&p->free_cluster_head)) {
22682a8f9449SShaohua Li 				cluster_set_next_flag(&p->free_cluster_head,
22692a8f9449SShaohua Li 								idx, 0);
22702a8f9449SShaohua Li 				cluster_set_next_flag(&p->free_cluster_tail,
22712a8f9449SShaohua Li 								idx, 0);
22722a8f9449SShaohua Li 			} else {
22732a8f9449SShaohua Li 				unsigned int tail;
22742a8f9449SShaohua Li 
22752a8f9449SShaohua Li 				tail = cluster_next(&p->free_cluster_tail);
22762a8f9449SShaohua Li 				cluster_set_next(&cluster_info[tail], idx);
22772a8f9449SShaohua Li 				cluster_set_next_flag(&p->free_cluster_tail,
22782a8f9449SShaohua Li 								idx, 0);
22792a8f9449SShaohua Li 			}
22802a8f9449SShaohua Li 		}
22812a8f9449SShaohua Li 		idx++;
22822a8f9449SShaohua Li 		if (idx == nr_clusters)
22832a8f9449SShaohua Li 			idx = 0;
22842a8f9449SShaohua Li 	}
2285915d4d7bSCesar Eduardo Barros 	return nr_extents;
2286915d4d7bSCesar Eduardo Barros }
2287915d4d7bSCesar Eduardo Barros 
2288dcf6b7ddSRafael Aquini /*
2289dcf6b7ddSRafael Aquini  * Helper to sys_swapon determining if a given swap
2290dcf6b7ddSRafael Aquini  * backing device queue supports DISCARD operations.
2291dcf6b7ddSRafael Aquini  */
2292dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si)
2293dcf6b7ddSRafael Aquini {
2294dcf6b7ddSRafael Aquini 	struct request_queue *q = bdev_get_queue(si->bdev);
2295dcf6b7ddSRafael Aquini 
2296dcf6b7ddSRafael Aquini 	if (!q || !blk_queue_discard(q))
2297dcf6b7ddSRafael Aquini 		return false;
2298dcf6b7ddSRafael Aquini 
2299dcf6b7ddSRafael Aquini 	return true;
2300dcf6b7ddSRafael Aquini }
2301dcf6b7ddSRafael Aquini 
230253cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
230353cbb243SCesar Eduardo Barros {
230453cbb243SCesar Eduardo Barros 	struct swap_info_struct *p;
230591a27b2aSJeff Layton 	struct filename *name;
230653cbb243SCesar Eduardo Barros 	struct file *swap_file = NULL;
230753cbb243SCesar Eduardo Barros 	struct address_space *mapping;
230840531542SCesar Eduardo Barros 	int i;
230940531542SCesar Eduardo Barros 	int prio;
231053cbb243SCesar Eduardo Barros 	int error;
231153cbb243SCesar Eduardo Barros 	union swap_header *swap_header;
2312915d4d7bSCesar Eduardo Barros 	int nr_extents;
231353cbb243SCesar Eduardo Barros 	sector_t span;
231453cbb243SCesar Eduardo Barros 	unsigned long maxpages;
231553cbb243SCesar Eduardo Barros 	unsigned char *swap_map = NULL;
23162a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info = NULL;
231738b5faf4SDan Magenheimer 	unsigned long *frontswap_map = NULL;
231853cbb243SCesar Eduardo Barros 	struct page *page = NULL;
231953cbb243SCesar Eduardo Barros 	struct inode *inode = NULL;
232053cbb243SCesar Eduardo Barros 
2321d15cab97SHugh Dickins 	if (swap_flags & ~SWAP_FLAGS_VALID)
2322d15cab97SHugh Dickins 		return -EINVAL;
2323d15cab97SHugh Dickins 
232453cbb243SCesar Eduardo Barros 	if (!capable(CAP_SYS_ADMIN))
232553cbb243SCesar Eduardo Barros 		return -EPERM;
232653cbb243SCesar Eduardo Barros 
232753cbb243SCesar Eduardo Barros 	p = alloc_swap_info();
23282542e513SCesar Eduardo Barros 	if (IS_ERR(p))
23292542e513SCesar Eduardo Barros 		return PTR_ERR(p);
233053cbb243SCesar Eduardo Barros 
2331815c2c54SShaohua Li 	INIT_WORK(&p->discard_work, swap_discard_work);
2332815c2c54SShaohua Li 
23331da177e4SLinus Torvalds 	name = getname(specialfile);
23341da177e4SLinus Torvalds 	if (IS_ERR(name)) {
23357de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(name);
23361da177e4SLinus Torvalds 		name = NULL;
2337bd69010bSCesar Eduardo Barros 		goto bad_swap;
23381da177e4SLinus Torvalds 	}
2339669abf4eSJeff Layton 	swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
23401da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
23417de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(swap_file);
23421da177e4SLinus Torvalds 		swap_file = NULL;
2343bd69010bSCesar Eduardo Barros 		goto bad_swap;
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	p->swap_file = swap_file;
23471da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 	for (i = 0; i < nr_swapfiles; i++) {
2350efa90a98SHugh Dickins 		struct swap_info_struct *q = swap_info[i];
23511da177e4SLinus Torvalds 
2352e8e6c2ecSCesar Eduardo Barros 		if (q == p || !q->swap_file)
23531da177e4SLinus Torvalds 			continue;
23547de7fb6bSCesar Eduardo Barros 		if (mapping == q->swap_file->f_mapping) {
23557de7fb6bSCesar Eduardo Barros 			error = -EBUSY;
23561da177e4SLinus Torvalds 			goto bad_swap;
23571da177e4SLinus Torvalds 		}
23587de7fb6bSCesar Eduardo Barros 	}
23591da177e4SLinus Torvalds 
23602130781eSCesar Eduardo Barros 	inode = mapping->host;
23612130781eSCesar Eduardo Barros 	/* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
23624d0e1e10SCesar Eduardo Barros 	error = claim_swapfile(p, inode);
23634d0e1e10SCesar Eduardo Barros 	if (unlikely(error))
23641da177e4SLinus Torvalds 		goto bad_swap;
23651da177e4SLinus Torvalds 
23661da177e4SLinus Torvalds 	/*
23671da177e4SLinus Torvalds 	 * Read the swap header.
23681da177e4SLinus Torvalds 	 */
23691da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
23701da177e4SLinus Torvalds 		error = -EINVAL;
23711da177e4SLinus Torvalds 		goto bad_swap;
23721da177e4SLinus Torvalds 	}
2373090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
23741da177e4SLinus Torvalds 	if (IS_ERR(page)) {
23751da177e4SLinus Torvalds 		error = PTR_ERR(page);
23761da177e4SLinus Torvalds 		goto bad_swap;
23771da177e4SLinus Torvalds 	}
237881e33971SHugh Dickins 	swap_header = kmap(page);
23791da177e4SLinus Torvalds 
2380ca8bd38bSCesar Eduardo Barros 	maxpages = read_swap_header(p, swap_header, inode);
2381ca8bd38bSCesar Eduardo Barros 	if (unlikely(!maxpages)) {
23821da177e4SLinus Torvalds 		error = -EINVAL;
23831da177e4SLinus Torvalds 		goto bad_swap;
23841da177e4SLinus Torvalds 	}
23851da177e4SLinus Torvalds 
23861da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
2387803d0c83SCesar Eduardo Barros 	swap_map = vzalloc(maxpages);
238878ecba08SHugh Dickins 	if (!swap_map) {
23891da177e4SLinus Torvalds 		error = -ENOMEM;
23901da177e4SLinus Torvalds 		goto bad_swap;
23911da177e4SLinus Torvalds 	}
23922a8f9449SShaohua Li 	if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
23932a8f9449SShaohua Li 		p->flags |= SWP_SOLIDSTATE;
23942a8f9449SShaohua Li 		/*
23952a8f9449SShaohua Li 		 * select a random position to start with to help wear leveling
23962a8f9449SShaohua Li 		 * SSD
23972a8f9449SShaohua Li 		 */
23982a8f9449SShaohua Li 		p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
23992a8f9449SShaohua Li 
24002a8f9449SShaohua Li 		cluster_info = vzalloc(DIV_ROUND_UP(maxpages,
24012a8f9449SShaohua Li 			SWAPFILE_CLUSTER) * sizeof(*cluster_info));
24022a8f9449SShaohua Li 		if (!cluster_info) {
24032a8f9449SShaohua Li 			error = -ENOMEM;
24042a8f9449SShaohua Li 			goto bad_swap;
24052a8f9449SShaohua Li 		}
24062a8f9449SShaohua Li 	}
24071da177e4SLinus Torvalds 
24081421ef3cSCesar Eduardo Barros 	error = swap_cgroup_swapon(p->type, maxpages);
24091421ef3cSCesar Eduardo Barros 	if (error)
24101421ef3cSCesar Eduardo Barros 		goto bad_swap;
24111421ef3cSCesar Eduardo Barros 
2412915d4d7bSCesar Eduardo Barros 	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
24132a8f9449SShaohua Li 		cluster_info, maxpages, &span);
2414915d4d7bSCesar Eduardo Barros 	if (unlikely(nr_extents < 0)) {
241553092a74SHugh Dickins 		error = nr_extents;
2416e2244ec2SHugh Dickins 		goto bad_swap;
241753092a74SHugh Dickins 	}
241838b5faf4SDan Magenheimer 	/* frontswap enabled? set up bit-per-page map for frontswap */
241938b5faf4SDan Magenheimer 	if (frontswap_enabled)
24207b57976dSAkinobu Mita 		frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long));
24211da177e4SLinus Torvalds 
24222a8f9449SShaohua Li 	if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
2423dcf6b7ddSRafael Aquini 		/*
2424dcf6b7ddSRafael Aquini 		 * When discard is enabled for swap with no particular
2425dcf6b7ddSRafael Aquini 		 * policy flagged, we set all swap discard flags here in
2426dcf6b7ddSRafael Aquini 		 * order to sustain backward compatibility with older
2427dcf6b7ddSRafael Aquini 		 * swapon(8) releases.
2428dcf6b7ddSRafael Aquini 		 */
2429dcf6b7ddSRafael Aquini 		p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
2430dcf6b7ddSRafael Aquini 			     SWP_PAGE_DISCARD);
2431dcf6b7ddSRafael Aquini 
2432dcf6b7ddSRafael Aquini 		/*
2433dcf6b7ddSRafael Aquini 		 * By flagging sys_swapon, a sysadmin can tell us to
2434dcf6b7ddSRafael Aquini 		 * either do single-time area discards only, or to just
2435dcf6b7ddSRafael Aquini 		 * perform discards for released swap page-clusters.
2436dcf6b7ddSRafael Aquini 		 * Now it's time to adjust the p->flags accordingly.
2437dcf6b7ddSRafael Aquini 		 */
2438dcf6b7ddSRafael Aquini 		if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
2439dcf6b7ddSRafael Aquini 			p->flags &= ~SWP_PAGE_DISCARD;
2440dcf6b7ddSRafael Aquini 		else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
2441dcf6b7ddSRafael Aquini 			p->flags &= ~SWP_AREA_DISCARD;
2442dcf6b7ddSRafael Aquini 
2443dcf6b7ddSRafael Aquini 		/* issue a swapon-time discard if it's still required */
2444dcf6b7ddSRafael Aquini 		if (p->flags & SWP_AREA_DISCARD) {
2445dcf6b7ddSRafael Aquini 			int err = discard_swap(p);
2446dcf6b7ddSRafael Aquini 			if (unlikely(err))
2447465c47fdSAndrew Morton 				pr_err("swapon: discard_swap(%p): %d\n",
2448dcf6b7ddSRafael Aquini 					p, err);
2449dcf6b7ddSRafael Aquini 		}
2450dcf6b7ddSRafael Aquini 	}
24516a6ba831SHugh Dickins 
2452fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
245340531542SCesar Eduardo Barros 	prio = -1;
245478ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
245540531542SCesar Eduardo Barros 		prio =
245678ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
24572a8f9449SShaohua Li 	enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
2458c69dbfb8SCesar Eduardo Barros 
2459465c47fdSAndrew Morton 	pr_info("Adding %uk swap on %s.  "
2460dcf6b7ddSRafael Aquini 			"Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
246191a27b2aSJeff Layton 		p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
2462c69dbfb8SCesar Eduardo Barros 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
2463c69dbfb8SCesar Eduardo Barros 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
246438b5faf4SDan Magenheimer 		(p->flags & SWP_DISCARDABLE) ? "D" : "",
2465dcf6b7ddSRafael Aquini 		(p->flags & SWP_AREA_DISCARD) ? "s" : "",
2466dcf6b7ddSRafael Aquini 		(p->flags & SWP_PAGE_DISCARD) ? "c" : "",
246738b5faf4SDan Magenheimer 		(frontswap_map) ? "FS" : "");
2468c69dbfb8SCesar Eduardo Barros 
2469fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
247066d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
247166d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
247266d7dd51SKay Sievers 
24739b01c350SCesar Eduardo Barros 	if (S_ISREG(inode->i_mode))
24749b01c350SCesar Eduardo Barros 		inode->i_flags |= S_SWAPFILE;
24751da177e4SLinus Torvalds 	error = 0;
24761da177e4SLinus Torvalds 	goto out;
24771da177e4SLinus Torvalds bad_swap:
2478bd69010bSCesar Eduardo Barros 	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
2479f2090d2dSCesar Eduardo Barros 		set_blocksize(p->bdev, p->old_block_size);
2480f2090d2dSCesar Eduardo Barros 		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
24811da177e4SLinus Torvalds 	}
24824cd3bb10SHugh Dickins 	destroy_swap_extents(p);
2483e8e6c2ecSCesar Eduardo Barros 	swap_cgroup_swapoff(p->type);
24845d337b91SHugh Dickins 	spin_lock(&swap_lock);
24851da177e4SLinus Torvalds 	p->swap_file = NULL;
24861da177e4SLinus Torvalds 	p->flags = 0;
24875d337b91SHugh Dickins 	spin_unlock(&swap_lock);
24881da177e4SLinus Torvalds 	vfree(swap_map);
24892a8f9449SShaohua Li 	vfree(cluster_info);
249052c50567SMel Gorman 	if (swap_file) {
24912130781eSCesar Eduardo Barros 		if (inode && S_ISREG(inode->i_mode)) {
249252c50567SMel Gorman 			mutex_unlock(&inode->i_mutex);
24932130781eSCesar Eduardo Barros 			inode = NULL;
24942130781eSCesar Eduardo Barros 		}
24951da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
249652c50567SMel Gorman 	}
24971da177e4SLinus Torvalds out:
24981da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
24991da177e4SLinus Torvalds 		kunmap(page);
25001da177e4SLinus Torvalds 		page_cache_release(page);
25011da177e4SLinus Torvalds 	}
25021da177e4SLinus Torvalds 	if (name)
25031da177e4SLinus Torvalds 		putname(name);
25049b01c350SCesar Eduardo Barros 	if (inode && S_ISREG(inode->i_mode))
25051b1dcc1bSJes Sorensen 		mutex_unlock(&inode->i_mutex);
25061da177e4SLinus Torvalds 	return error;
25071da177e4SLinus Torvalds }
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
25101da177e4SLinus Torvalds {
2511efa90a98SHugh Dickins 	unsigned int type;
25121da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
25131da177e4SLinus Torvalds 
25145d337b91SHugh Dickins 	spin_lock(&swap_lock);
2515efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2516efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
2517efa90a98SHugh Dickins 
2518efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2519efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
25201da177e4SLinus Torvalds 	}
2521ec8acf20SShaohua Li 	val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
25221da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
25235d337b91SHugh Dickins 	spin_unlock(&swap_lock);
25241da177e4SLinus Torvalds }
25251da177e4SLinus Torvalds 
25261da177e4SLinus Torvalds /*
25271da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
25281da177e4SLinus Torvalds  *
2529355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
2530355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
2531355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
2532355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
2533355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
2534355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
2535570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
25361da177e4SLinus Torvalds  */
25378d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
25381da177e4SLinus Torvalds {
25391da177e4SLinus Torvalds 	struct swap_info_struct *p;
25401da177e4SLinus Torvalds 	unsigned long offset, type;
25418d69aaeeSHugh Dickins 	unsigned char count;
25428d69aaeeSHugh Dickins 	unsigned char has_cache;
2543253d553bSHugh Dickins 	int err = -EINVAL;
25441da177e4SLinus Torvalds 
2545a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
2546253d553bSHugh Dickins 		goto out;
25470697212aSChristoph Lameter 
25481da177e4SLinus Torvalds 	type = swp_type(entry);
25491da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
25501da177e4SLinus Torvalds 		goto bad_file;
2551efa90a98SHugh Dickins 	p = swap_info[type];
25521da177e4SLinus Torvalds 	offset = swp_offset(entry);
25531da177e4SLinus Torvalds 
2554ec8acf20SShaohua Li 	spin_lock(&p->lock);
2555355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
2556355cfa73SKAMEZAWA Hiroyuki 		goto unlock_out;
2557355cfa73SKAMEZAWA Hiroyuki 
2558253d553bSHugh Dickins 	count = p->swap_map[offset];
2559*edfe23daSShaohua Li 
2560*edfe23daSShaohua Li 	/*
2561*edfe23daSShaohua Li 	 * swapin_readahead() doesn't check if a swap entry is valid, so the
2562*edfe23daSShaohua Li 	 * swap entry could be SWAP_MAP_BAD. Check here with lock held.
2563*edfe23daSShaohua Li 	 */
2564*edfe23daSShaohua Li 	if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
2565*edfe23daSShaohua Li 		err = -ENOENT;
2566*edfe23daSShaohua Li 		goto unlock_out;
2567*edfe23daSShaohua Li 	}
2568*edfe23daSShaohua Li 
2569253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
2570253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
2571253d553bSHugh Dickins 	err = 0;
2572355cfa73SKAMEZAWA Hiroyuki 
2573253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
2574355cfa73SKAMEZAWA Hiroyuki 
2575355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
2576253d553bSHugh Dickins 		if (!has_cache && count)
2577253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
2578253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
2579253d553bSHugh Dickins 			err = -EEXIST;
2580253d553bSHugh Dickins 		else				/* no users remaining */
2581253d553bSHugh Dickins 			err = -ENOENT;
2582355cfa73SKAMEZAWA Hiroyuki 
2583355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
2584253d553bSHugh Dickins 
2585570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2586570a335bSHugh Dickins 			count += usage;
2587570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
2588253d553bSHugh Dickins 			err = -EINVAL;
2589570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
2590570a335bSHugh Dickins 			count = COUNT_CONTINUED;
2591570a335bSHugh Dickins 		else
2592570a335bSHugh Dickins 			err = -ENOMEM;
2593253d553bSHugh Dickins 	} else
2594253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
2595253d553bSHugh Dickins 
2596253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
2597253d553bSHugh Dickins 
2598355cfa73SKAMEZAWA Hiroyuki unlock_out:
2599ec8acf20SShaohua Li 	spin_unlock(&p->lock);
26001da177e4SLinus Torvalds out:
2601253d553bSHugh Dickins 	return err;
26021da177e4SLinus Torvalds 
26031da177e4SLinus Torvalds bad_file:
2604465c47fdSAndrew Morton 	pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val);
26051da177e4SLinus Torvalds 	goto out;
26061da177e4SLinus Torvalds }
2607253d553bSHugh Dickins 
2608355cfa73SKAMEZAWA Hiroyuki /*
2609aaa46865SHugh Dickins  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2610aaa46865SHugh Dickins  * (in which case its reference count is never incremented).
2611aaa46865SHugh Dickins  */
2612aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry)
2613aaa46865SHugh Dickins {
2614aaa46865SHugh Dickins 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
2615aaa46865SHugh Dickins }
2616aaa46865SHugh Dickins 
2617aaa46865SHugh Dickins /*
261808259d58SHugh Dickins  * Increase reference count of swap entry by 1.
261908259d58SHugh Dickins  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
262008259d58SHugh Dickins  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
262108259d58SHugh Dickins  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
262208259d58SHugh Dickins  * might occur if a page table entry has got corrupted.
2623355cfa73SKAMEZAWA Hiroyuki  */
2624570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
2625355cfa73SKAMEZAWA Hiroyuki {
2626570a335bSHugh Dickins 	int err = 0;
2627570a335bSHugh Dickins 
2628570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2629570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
2630570a335bSHugh Dickins 	return err;
2631355cfa73SKAMEZAWA Hiroyuki }
26321da177e4SLinus Torvalds 
2633cb4b86baSKAMEZAWA Hiroyuki /*
2634355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
2635355cfa73SKAMEZAWA Hiroyuki  *
263673c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
2637355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
2638355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
2639355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
2640cb4b86baSKAMEZAWA Hiroyuki  */
2641cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
2642cb4b86baSKAMEZAWA Hiroyuki {
2643253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
2644cb4b86baSKAMEZAWA Hiroyuki }
2645cb4b86baSKAMEZAWA Hiroyuki 
2646f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page)
2647f981c595SMel Gorman {
2648f981c595SMel Gorman 	swp_entry_t swap = { .val = page_private(page) };
2649f981c595SMel Gorman 	BUG_ON(!PageSwapCache(page));
2650f981c595SMel Gorman 	return swap_info[swp_type(swap)];
2651f981c595SMel Gorman }
2652f981c595SMel Gorman 
2653f981c595SMel Gorman /*
2654f981c595SMel Gorman  * out-of-line __page_file_ methods to avoid include hell.
2655f981c595SMel Gorman  */
2656f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page)
2657f981c595SMel Gorman {
2658f981c595SMel Gorman 	VM_BUG_ON(!PageSwapCache(page));
2659f981c595SMel Gorman 	return page_swap_info(page)->swap_file->f_mapping;
2660f981c595SMel Gorman }
2661f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping);
2662f981c595SMel Gorman 
2663f981c595SMel Gorman pgoff_t __page_file_index(struct page *page)
2664f981c595SMel Gorman {
2665f981c595SMel Gorman 	swp_entry_t swap = { .val = page_private(page) };
2666f981c595SMel Gorman 	VM_BUG_ON(!PageSwapCache(page));
2667f981c595SMel Gorman 	return swp_offset(swap);
2668f981c595SMel Gorman }
2669f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index);
2670f981c595SMel Gorman 
26711da177e4SLinus Torvalds /*
2672570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
2673570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2674570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
2675570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
2676570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2677570a335bSHugh Dickins  *
2678570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
2679570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
2680570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2681570a335bSHugh Dickins  *
2682570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2683570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2684570a335bSHugh Dickins  * can be called after dropping locks.
2685570a335bSHugh Dickins  */
2686570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2687570a335bSHugh Dickins {
2688570a335bSHugh Dickins 	struct swap_info_struct *si;
2689570a335bSHugh Dickins 	struct page *head;
2690570a335bSHugh Dickins 	struct page *page;
2691570a335bSHugh Dickins 	struct page *list_page;
2692570a335bSHugh Dickins 	pgoff_t offset;
2693570a335bSHugh Dickins 	unsigned char count;
2694570a335bSHugh Dickins 
2695570a335bSHugh Dickins 	/*
2696570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2697570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2698570a335bSHugh Dickins 	 */
2699570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2700570a335bSHugh Dickins 
2701570a335bSHugh Dickins 	si = swap_info_get(entry);
2702570a335bSHugh Dickins 	if (!si) {
2703570a335bSHugh Dickins 		/*
2704570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
2705570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
2706570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
2707570a335bSHugh Dickins 		 */
2708570a335bSHugh Dickins 		goto outer;
2709570a335bSHugh Dickins 	}
2710570a335bSHugh Dickins 
2711570a335bSHugh Dickins 	offset = swp_offset(entry);
2712570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2713570a335bSHugh Dickins 
2714570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2715570a335bSHugh Dickins 		/*
2716570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
2717570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
2718570a335bSHugh Dickins 		 * over-provisioning.
2719570a335bSHugh Dickins 		 */
2720570a335bSHugh Dickins 		goto out;
2721570a335bSHugh Dickins 	}
2722570a335bSHugh Dickins 
2723570a335bSHugh Dickins 	if (!page) {
2724ec8acf20SShaohua Li 		spin_unlock(&si->lock);
2725570a335bSHugh Dickins 		return -ENOMEM;
2726570a335bSHugh Dickins 	}
2727570a335bSHugh Dickins 
2728570a335bSHugh Dickins 	/*
2729570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2730570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel pagetables: so it
2731570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2732570a335bSHugh Dickins 	 */
2733570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2734570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2735570a335bSHugh Dickins 
2736570a335bSHugh Dickins 	/*
2737570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
2738570a335bSHugh Dickins 	 * but it does always reset its private field.
2739570a335bSHugh Dickins 	 */
2740570a335bSHugh Dickins 	if (!page_private(head)) {
2741570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2742570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
2743570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
2744570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
2745570a335bSHugh Dickins 	}
2746570a335bSHugh Dickins 
2747570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
2748570a335bSHugh Dickins 		unsigned char *map;
2749570a335bSHugh Dickins 
2750570a335bSHugh Dickins 		/*
2751570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
2752570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
2753570a335bSHugh Dickins 		 */
2754570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
2755570a335bSHugh Dickins 			goto out;
2756570a335bSHugh Dickins 
27579b04c5feSCong Wang 		map = kmap_atomic(list_page) + offset;
2758570a335bSHugh Dickins 		count = *map;
27599b04c5feSCong Wang 		kunmap_atomic(map);
2760570a335bSHugh Dickins 
2761570a335bSHugh Dickins 		/*
2762570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
2763570a335bSHugh Dickins 		 * free our allocation and use this one.
2764570a335bSHugh Dickins 		 */
2765570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2766570a335bSHugh Dickins 			goto out;
2767570a335bSHugh Dickins 	}
2768570a335bSHugh Dickins 
2769570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
2770570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
2771570a335bSHugh Dickins out:
2772ec8acf20SShaohua Li 	spin_unlock(&si->lock);
2773570a335bSHugh Dickins outer:
2774570a335bSHugh Dickins 	if (page)
2775570a335bSHugh Dickins 		__free_page(page);
2776570a335bSHugh Dickins 	return 0;
2777570a335bSHugh Dickins }
2778570a335bSHugh Dickins 
2779570a335bSHugh Dickins /*
2780570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
2781570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2782570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
2783570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
2784570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
2785570a335bSHugh Dickins  * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2786570a335bSHugh Dickins  */
2787570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
2788570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
2789570a335bSHugh Dickins {
2790570a335bSHugh Dickins 	struct page *head;
2791570a335bSHugh Dickins 	struct page *page;
2792570a335bSHugh Dickins 	unsigned char *map;
2793570a335bSHugh Dickins 
2794570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
2795570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
2796570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
2797570a335bSHugh Dickins 		return false;		/* need to add count continuation */
2798570a335bSHugh Dickins 	}
2799570a335bSHugh Dickins 
2800570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
2801570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
28029b04c5feSCong Wang 	map = kmap_atomic(page) + offset;
2803570a335bSHugh Dickins 
2804570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
2805570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
2806570a335bSHugh Dickins 
2807570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2808570a335bSHugh Dickins 		/*
2809570a335bSHugh Dickins 		 * Think of how you add 1 to 999
2810570a335bSHugh Dickins 		 */
2811570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
28129b04c5feSCong Wang 			kunmap_atomic(map);
2813570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2814570a335bSHugh Dickins 			BUG_ON(page == head);
28159b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2816570a335bSHugh Dickins 		}
2817570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
28189b04c5feSCong Wang 			kunmap_atomic(map);
2819570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2820570a335bSHugh Dickins 			if (page == head)
2821570a335bSHugh Dickins 				return false;	/* add count continuation */
28229b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2823570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
2824570a335bSHugh Dickins 		}
2825570a335bSHugh Dickins 		*map += 1;
28269b04c5feSCong Wang 		kunmap_atomic(map);
2827570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2828570a335bSHugh Dickins 		while (page != head) {
28299b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2830570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
28319b04c5feSCong Wang 			kunmap_atomic(map);
2832570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2833570a335bSHugh Dickins 		}
2834570a335bSHugh Dickins 		return true;			/* incremented */
2835570a335bSHugh Dickins 
2836570a335bSHugh Dickins 	} else {				/* decrementing */
2837570a335bSHugh Dickins 		/*
2838570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
2839570a335bSHugh Dickins 		 */
2840570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
2841570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
28429b04c5feSCong Wang 			kunmap_atomic(map);
2843570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
2844570a335bSHugh Dickins 			BUG_ON(page == head);
28459b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2846570a335bSHugh Dickins 		}
2847570a335bSHugh Dickins 		BUG_ON(*map == 0);
2848570a335bSHugh Dickins 		*map -= 1;
2849570a335bSHugh Dickins 		if (*map == 0)
2850570a335bSHugh Dickins 			count = 0;
28519b04c5feSCong Wang 		kunmap_atomic(map);
2852570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
2853570a335bSHugh Dickins 		while (page != head) {
28549b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
2855570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
2856570a335bSHugh Dickins 			count = COUNT_CONTINUED;
28579b04c5feSCong Wang 			kunmap_atomic(map);
2858570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
2859570a335bSHugh Dickins 		}
2860570a335bSHugh Dickins 		return count == COUNT_CONTINUED;
2861570a335bSHugh Dickins 	}
2862570a335bSHugh Dickins }
2863570a335bSHugh Dickins 
2864570a335bSHugh Dickins /*
2865570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
2866570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2867570a335bSHugh Dickins  */
2868570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
2869570a335bSHugh Dickins {
2870570a335bSHugh Dickins 	pgoff_t offset;
2871570a335bSHugh Dickins 
2872570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2873570a335bSHugh Dickins 		struct page *head;
2874570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
2875570a335bSHugh Dickins 		if (page_private(head)) {
2876570a335bSHugh Dickins 			struct list_head *this, *next;
2877570a335bSHugh Dickins 			list_for_each_safe(this, next, &head->lru) {
2878570a335bSHugh Dickins 				struct page *page;
2879570a335bSHugh Dickins 				page = list_entry(this, struct page, lru);
2880570a335bSHugh Dickins 				list_del(this);
2881570a335bSHugh Dickins 				__free_page(page);
2882570a335bSHugh Dickins 			}
2883570a335bSHugh Dickins 		}
2884570a335bSHugh Dickins 	}
2885570a335bSHugh Dickins }
2886