xref: /openbmc/linux/mm/swapfile.c (revision 5d5e8f19544a35ae1609bd96ae6b28c9fcd1baf6)
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>
96e84f315SIngo Molnar #include <linux/sched/mm.h>
1029930025SIngo Molnar #include <linux/sched/task.h>
111da177e4SLinus Torvalds #include <linux/hugetlb.h>
121da177e4SLinus Torvalds #include <linux/mman.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/kernel_stat.h>
151da177e4SLinus Torvalds #include <linux/swap.h>
161da177e4SLinus Torvalds #include <linux/vmalloc.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
181da177e4SLinus Torvalds #include <linux/namei.h>
19072441e2SHugh Dickins #include <linux/shmem_fs.h>
201da177e4SLinus Torvalds #include <linux/blkdev.h>
2120137a49SHugh Dickins #include <linux/random.h>
221da177e4SLinus Torvalds #include <linux/writeback.h>
231da177e4SLinus Torvalds #include <linux/proc_fs.h>
241da177e4SLinus Torvalds #include <linux/seq_file.h>
251da177e4SLinus Torvalds #include <linux/init.h>
265ad64688SHugh Dickins #include <linux/ksm.h>
271da177e4SLinus Torvalds #include <linux/rmap.h>
281da177e4SLinus Torvalds #include <linux/security.h>
291da177e4SLinus Torvalds #include <linux/backing-dev.h>
30fc0abb14SIngo Molnar #include <linux/mutex.h>
31c59ede7bSRandy.Dunlap #include <linux/capability.h>
321da177e4SLinus Torvalds #include <linux/syscalls.h>
338a9f3ccdSBalbir Singh #include <linux/memcontrol.h>
3466d7dd51SKay Sievers #include <linux/poll.h>
3572788c38SDavid Rientjes #include <linux/oom.h>
3638b5faf4SDan Magenheimer #include <linux/frontswap.h>
3738b5faf4SDan Magenheimer #include <linux/swapfile.h>
38f981c595SMel Gorman #include <linux/export.h>
3967afa38eSTim Chen #include <linux/swap_slots.h>
40155b5f88SHuang Ying #include <linux/sort.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/pgtable.h>
431da177e4SLinus Torvalds #include <asm/tlbflush.h>
441da177e4SLinus Torvalds #include <linux/swapops.h>
455d1ea48bSJohannes Weiner #include <linux/swap_cgroup.h>
461da177e4SLinus Torvalds 
47570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
48570a335bSHugh Dickins 				 unsigned char);
49570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *);
50d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**);
51570a335bSHugh Dickins 
5238b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock);
537c363b8cSAdrian Bunk static unsigned int nr_swapfiles;
54ec8acf20SShaohua Li atomic_long_t nr_swap_pages;
55fb0fec50SChris Wilson /*
56fb0fec50SChris Wilson  * Some modules use swappable objects and may try to swap them out under
57fb0fec50SChris Wilson  * memory pressure (via the shrinker). Before doing so, they may wish to
58fb0fec50SChris Wilson  * check to see if any swap space is available.
59fb0fec50SChris Wilson  */
60fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages);
61ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
621da177e4SLinus Torvalds long total_swap_pages;
63a2468cc9SAaron Lu static int least_priority = -1;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry ";
661da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry ";
671da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry ";
681da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry ";
691da177e4SLinus Torvalds 
70adfab836SDan Streetman /*
71adfab836SDan Streetman  * all active swap_info_structs
72adfab836SDan Streetman  * protected with swap_lock, and ordered by priority.
73adfab836SDan Streetman  */
7418ab4d4cSDan Streetman PLIST_HEAD(swap_active_head);
7518ab4d4cSDan Streetman 
7618ab4d4cSDan Streetman /*
7718ab4d4cSDan Streetman  * all available (active, not full) swap_info_structs
7818ab4d4cSDan Streetman  * protected with swap_avail_lock, ordered by priority.
7918ab4d4cSDan Streetman  * This is used by get_swap_page() instead of swap_active_head
8018ab4d4cSDan Streetman  * because swap_active_head includes all swap_info_structs,
8118ab4d4cSDan Streetman  * but get_swap_page() doesn't need to look at full ones.
8218ab4d4cSDan Streetman  * This uses its own lock instead of swap_lock because when a
8318ab4d4cSDan Streetman  * swap_info_struct changes between not-full/full, it needs to
8418ab4d4cSDan Streetman  * add/remove itself to/from this list, but the swap_info_struct->lock
8518ab4d4cSDan Streetman  * is held and the locking order requires swap_lock to be taken
8618ab4d4cSDan Streetman  * before any swap_info_struct->lock.
8718ab4d4cSDan Streetman  */
88bfc6b1caSColin Ian King static struct plist_head *swap_avail_heads;
8918ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock);
901da177e4SLinus Torvalds 
9138b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES];
921da177e4SLinus Torvalds 
93fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex);
941da177e4SLinus Torvalds 
9566d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
9666d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */
9766d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0);
9866d7dd51SKay Sievers 
9981a0298bSHuang Ying atomic_t nr_rotate_swap = ATOMIC_INIT(0);
10081a0298bSHuang Ying 
1018d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent)
102355cfa73SKAMEZAWA Hiroyuki {
103955c97f0SDaniel Jordan 	return ent & ~SWAP_HAS_CACHE;	/* may include COUNT_CONTINUED flag */
104355cfa73SKAMEZAWA Hiroyuki }
105355cfa73SKAMEZAWA Hiroyuki 
106efa90a98SHugh Dickins /* returns 1 if swap entry is freed */
107c9e44410SKAMEZAWA Hiroyuki static int
108c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
109c9e44410SKAMEZAWA Hiroyuki {
110efa90a98SHugh Dickins 	swp_entry_t entry = swp_entry(si->type, offset);
111c9e44410SKAMEZAWA Hiroyuki 	struct page *page;
112c9e44410SKAMEZAWA Hiroyuki 	int ret = 0;
113c9e44410SKAMEZAWA Hiroyuki 
114f6ab1f7fSHuang Ying 	page = find_get_page(swap_address_space(entry), swp_offset(entry));
115c9e44410SKAMEZAWA Hiroyuki 	if (!page)
116c9e44410SKAMEZAWA Hiroyuki 		return 0;
117c9e44410SKAMEZAWA Hiroyuki 	/*
118c9e44410SKAMEZAWA Hiroyuki 	 * This function is called from scan_swap_map() and it's called
119c9e44410SKAMEZAWA Hiroyuki 	 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
120c9e44410SKAMEZAWA Hiroyuki 	 * We have to use trylock for avoiding deadlock. This is a special
121c9e44410SKAMEZAWA Hiroyuki 	 * case and you should use try_to_free_swap() with explicit lock_page()
122c9e44410SKAMEZAWA Hiroyuki 	 * in usual operations.
123c9e44410SKAMEZAWA Hiroyuki 	 */
124c9e44410SKAMEZAWA Hiroyuki 	if (trylock_page(page)) {
125c9e44410SKAMEZAWA Hiroyuki 		ret = try_to_free_swap(page);
126c9e44410SKAMEZAWA Hiroyuki 		unlock_page(page);
127c9e44410SKAMEZAWA Hiroyuki 	}
12809cbfeafSKirill A. Shutemov 	put_page(page);
129c9e44410SKAMEZAWA Hiroyuki 	return ret;
130c9e44410SKAMEZAWA Hiroyuki }
131355cfa73SKAMEZAWA Hiroyuki 
1321da177e4SLinus Torvalds /*
1336a6ba831SHugh Dickins  * swapon tell device that all the old swap contents can be discarded,
1346a6ba831SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1356a6ba831SHugh Dickins  */
1366a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si)
1376a6ba831SHugh Dickins {
1386a6ba831SHugh Dickins 	struct swap_extent *se;
1399625a5f2SHugh Dickins 	sector_t start_block;
1409625a5f2SHugh Dickins 	sector_t nr_blocks;
1416a6ba831SHugh Dickins 	int err = 0;
1426a6ba831SHugh Dickins 
1436a6ba831SHugh Dickins 	/* Do not discard the swap header page! */
1449625a5f2SHugh Dickins 	se = &si->first_swap_extent;
1459625a5f2SHugh Dickins 	start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
1469625a5f2SHugh Dickins 	nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
1479625a5f2SHugh Dickins 	if (nr_blocks) {
1489625a5f2SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
149dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1509625a5f2SHugh Dickins 		if (err)
1519625a5f2SHugh Dickins 			return err;
1529625a5f2SHugh Dickins 		cond_resched();
1536a6ba831SHugh Dickins 	}
1546a6ba831SHugh Dickins 
1559625a5f2SHugh Dickins 	list_for_each_entry(se, &si->first_swap_extent.list, list) {
1569625a5f2SHugh Dickins 		start_block = se->start_block << (PAGE_SHIFT - 9);
1579625a5f2SHugh Dickins 		nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
1589625a5f2SHugh Dickins 
1596a6ba831SHugh Dickins 		err = blkdev_issue_discard(si->bdev, start_block,
160dd3932edSChristoph Hellwig 				nr_blocks, GFP_KERNEL, 0);
1616a6ba831SHugh Dickins 		if (err)
1626a6ba831SHugh Dickins 			break;
1636a6ba831SHugh Dickins 
1646a6ba831SHugh Dickins 		cond_resched();
1656a6ba831SHugh Dickins 	}
1666a6ba831SHugh Dickins 	return err;		/* That will often be -EOPNOTSUPP */
1676a6ba831SHugh Dickins }
1686a6ba831SHugh Dickins 
1697992fde7SHugh Dickins /*
1707992fde7SHugh Dickins  * swap allocation tell device that a cluster of swap can now be discarded,
1717992fde7SHugh Dickins  * to allow the swap device to optimize its wear-levelling.
1727992fde7SHugh Dickins  */
1737992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si,
1747992fde7SHugh Dickins 				 pgoff_t start_page, pgoff_t nr_pages)
1757992fde7SHugh Dickins {
1767992fde7SHugh Dickins 	struct swap_extent *se = si->curr_swap_extent;
1777992fde7SHugh Dickins 	int found_extent = 0;
1787992fde7SHugh Dickins 
1797992fde7SHugh Dickins 	while (nr_pages) {
1807992fde7SHugh Dickins 		if (se->start_page <= start_page &&
1817992fde7SHugh Dickins 		    start_page < se->start_page + se->nr_pages) {
1827992fde7SHugh Dickins 			pgoff_t offset = start_page - se->start_page;
1837992fde7SHugh Dickins 			sector_t start_block = se->start_block + offset;
184858a2990SHugh Dickins 			sector_t nr_blocks = se->nr_pages - offset;
1857992fde7SHugh Dickins 
1867992fde7SHugh Dickins 			if (nr_blocks > nr_pages)
1877992fde7SHugh Dickins 				nr_blocks = nr_pages;
1887992fde7SHugh Dickins 			start_page += nr_blocks;
1897992fde7SHugh Dickins 			nr_pages -= nr_blocks;
1907992fde7SHugh Dickins 
1917992fde7SHugh Dickins 			if (!found_extent++)
1927992fde7SHugh Dickins 				si->curr_swap_extent = se;
1937992fde7SHugh Dickins 
1947992fde7SHugh Dickins 			start_block <<= PAGE_SHIFT - 9;
1957992fde7SHugh Dickins 			nr_blocks <<= PAGE_SHIFT - 9;
1967992fde7SHugh Dickins 			if (blkdev_issue_discard(si->bdev, start_block,
197dd3932edSChristoph Hellwig 				    nr_blocks, GFP_NOIO, 0))
1987992fde7SHugh Dickins 				break;
1997992fde7SHugh Dickins 		}
2007992fde7SHugh Dickins 
201a8ae4991SGeliang Tang 		se = list_next_entry(se, list);
2027992fde7SHugh Dickins 	}
2037992fde7SHugh Dickins }
2047992fde7SHugh Dickins 
20538d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP
20638d8b4e6SHuang Ying #define SWAPFILE_CLUSTER	HPAGE_PMD_NR
207a448f2d0SHuang Ying 
208a448f2d0SHuang Ying #define swap_entry_size(size)	(size)
20938d8b4e6SHuang Ying #else
210048c27fdSHugh Dickins #define SWAPFILE_CLUSTER	256
211a448f2d0SHuang Ying 
212a448f2d0SHuang Ying /*
213a448f2d0SHuang Ying  * Define swap_entry_size() as constant to let compiler to optimize
214a448f2d0SHuang Ying  * out some code if !CONFIG_THP_SWAP
215a448f2d0SHuang Ying  */
216a448f2d0SHuang Ying #define swap_entry_size(size)	1
21738d8b4e6SHuang Ying #endif
218048c27fdSHugh Dickins #define LATENCY_LIMIT		256
219048c27fdSHugh Dickins 
2202a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info,
2212a8f9449SShaohua Li 	unsigned int flag)
2222a8f9449SShaohua Li {
2232a8f9449SShaohua Li 	info->flags = flag;
2242a8f9449SShaohua Li }
2252a8f9449SShaohua Li 
2262a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info)
2272a8f9449SShaohua Li {
2282a8f9449SShaohua Li 	return info->data;
2292a8f9449SShaohua Li }
2302a8f9449SShaohua Li 
2312a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info,
2322a8f9449SShaohua Li 				     unsigned int c)
2332a8f9449SShaohua Li {
2342a8f9449SShaohua Li 	info->data = c;
2352a8f9449SShaohua Li }
2362a8f9449SShaohua Li 
2372a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info,
2382a8f9449SShaohua Li 					 unsigned int c, unsigned int f)
2392a8f9449SShaohua Li {
2402a8f9449SShaohua Li 	info->flags = f;
2412a8f9449SShaohua Li 	info->data = c;
2422a8f9449SShaohua Li }
2432a8f9449SShaohua Li 
2442a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info)
2452a8f9449SShaohua Li {
2462a8f9449SShaohua Li 	return info->data;
2472a8f9449SShaohua Li }
2482a8f9449SShaohua Li 
2492a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info,
2502a8f9449SShaohua Li 				    unsigned int n)
2512a8f9449SShaohua Li {
2522a8f9449SShaohua Li 	info->data = n;
2532a8f9449SShaohua Li }
2542a8f9449SShaohua Li 
2552a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info,
2562a8f9449SShaohua Li 					 unsigned int n, unsigned int f)
2572a8f9449SShaohua Li {
2582a8f9449SShaohua Li 	info->flags = f;
2592a8f9449SShaohua Li 	info->data = n;
2602a8f9449SShaohua Li }
2612a8f9449SShaohua Li 
2622a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info)
2632a8f9449SShaohua Li {
2642a8f9449SShaohua Li 	return info->flags & CLUSTER_FLAG_FREE;
2652a8f9449SShaohua Li }
2662a8f9449SShaohua Li 
2672a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info)
2682a8f9449SShaohua Li {
2692a8f9449SShaohua Li 	return info->flags & CLUSTER_FLAG_NEXT_NULL;
2702a8f9449SShaohua Li }
2712a8f9449SShaohua Li 
2722a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info)
2732a8f9449SShaohua Li {
2742a8f9449SShaohua Li 	info->flags = CLUSTER_FLAG_NEXT_NULL;
2752a8f9449SShaohua Li 	info->data = 0;
2762a8f9449SShaohua Li }
2772a8f9449SShaohua Li 
278e0709829SHuang Ying static inline bool cluster_is_huge(struct swap_cluster_info *info)
279e0709829SHuang Ying {
28033ee011eSHuang Ying 	if (IS_ENABLED(CONFIG_THP_SWAP))
281e0709829SHuang Ying 		return info->flags & CLUSTER_FLAG_HUGE;
28233ee011eSHuang Ying 	return false;
283e0709829SHuang Ying }
284e0709829SHuang Ying 
285e0709829SHuang Ying static inline void cluster_clear_huge(struct swap_cluster_info *info)
286e0709829SHuang Ying {
287e0709829SHuang Ying 	info->flags &= ~CLUSTER_FLAG_HUGE;
288e0709829SHuang Ying }
289e0709829SHuang Ying 
290235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si,
291235b6217SHuang, Ying 						     unsigned long offset)
292235b6217SHuang, Ying {
293235b6217SHuang, Ying 	struct swap_cluster_info *ci;
294235b6217SHuang, Ying 
295235b6217SHuang, Ying 	ci = si->cluster_info;
296235b6217SHuang, Ying 	if (ci) {
297235b6217SHuang, Ying 		ci += offset / SWAPFILE_CLUSTER;
298235b6217SHuang, Ying 		spin_lock(&ci->lock);
299235b6217SHuang, Ying 	}
300235b6217SHuang, Ying 	return ci;
301235b6217SHuang, Ying }
302235b6217SHuang, Ying 
303235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci)
304235b6217SHuang, Ying {
305235b6217SHuang, Ying 	if (ci)
306235b6217SHuang, Ying 		spin_unlock(&ci->lock);
307235b6217SHuang, Ying }
308235b6217SHuang, Ying 
30959d98bf3SHuang Ying /*
31059d98bf3SHuang Ying  * Determine the locking method in use for this device.  Return
31159d98bf3SHuang Ying  * swap_cluster_info if SSD-style cluster-based locking is in place.
31259d98bf3SHuang Ying  */
313235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info(
31459d98bf3SHuang Ying 		struct swap_info_struct *si, unsigned long offset)
315235b6217SHuang, Ying {
316235b6217SHuang, Ying 	struct swap_cluster_info *ci;
317235b6217SHuang, Ying 
31859d98bf3SHuang Ying 	/* Try to use fine-grained SSD-style locking if available: */
319235b6217SHuang, Ying 	ci = lock_cluster(si, offset);
32059d98bf3SHuang Ying 	/* Otherwise, fall back to traditional, coarse locking: */
321235b6217SHuang, Ying 	if (!ci)
322235b6217SHuang, Ying 		spin_lock(&si->lock);
323235b6217SHuang, Ying 
324235b6217SHuang, Ying 	return ci;
325235b6217SHuang, Ying }
326235b6217SHuang, Ying 
327235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si,
328235b6217SHuang, Ying 					       struct swap_cluster_info *ci)
329235b6217SHuang, Ying {
330235b6217SHuang, Ying 	if (ci)
331235b6217SHuang, Ying 		unlock_cluster(ci);
332235b6217SHuang, Ying 	else
333235b6217SHuang, Ying 		spin_unlock(&si->lock);
334235b6217SHuang, Ying }
335235b6217SHuang, Ying 
3366b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list)
3376b534915SHuang Ying {
3386b534915SHuang Ying 	return cluster_is_null(&list->head);
3396b534915SHuang Ying }
3406b534915SHuang Ying 
3416b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list)
3426b534915SHuang Ying {
3436b534915SHuang Ying 	return cluster_next(&list->head);
3446b534915SHuang Ying }
3456b534915SHuang Ying 
3466b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list)
3476b534915SHuang Ying {
3486b534915SHuang Ying 	cluster_set_null(&list->head);
3496b534915SHuang Ying 	cluster_set_null(&list->tail);
3506b534915SHuang Ying }
3516b534915SHuang Ying 
3526b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list,
3536b534915SHuang Ying 				  struct swap_cluster_info *ci,
3546b534915SHuang Ying 				  unsigned int idx)
3556b534915SHuang Ying {
3566b534915SHuang Ying 	if (cluster_list_empty(list)) {
3576b534915SHuang Ying 		cluster_set_next_flag(&list->head, idx, 0);
3586b534915SHuang Ying 		cluster_set_next_flag(&list->tail, idx, 0);
3596b534915SHuang Ying 	} else {
360235b6217SHuang, Ying 		struct swap_cluster_info *ci_tail;
3616b534915SHuang Ying 		unsigned int tail = cluster_next(&list->tail);
3626b534915SHuang Ying 
363235b6217SHuang, Ying 		/*
364235b6217SHuang, Ying 		 * Nested cluster lock, but both cluster locks are
365235b6217SHuang, Ying 		 * only acquired when we held swap_info_struct->lock
366235b6217SHuang, Ying 		 */
367235b6217SHuang, Ying 		ci_tail = ci + tail;
368235b6217SHuang, Ying 		spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING);
369235b6217SHuang, Ying 		cluster_set_next(ci_tail, idx);
3700ef017d1SHuang Ying 		spin_unlock(&ci_tail->lock);
3716b534915SHuang Ying 		cluster_set_next_flag(&list->tail, idx, 0);
3726b534915SHuang Ying 	}
3736b534915SHuang Ying }
3746b534915SHuang Ying 
3756b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list,
3766b534915SHuang Ying 					   struct swap_cluster_info *ci)
3776b534915SHuang Ying {
3786b534915SHuang Ying 	unsigned int idx;
3796b534915SHuang Ying 
3806b534915SHuang Ying 	idx = cluster_next(&list->head);
3816b534915SHuang Ying 	if (cluster_next(&list->tail) == idx) {
3826b534915SHuang Ying 		cluster_set_null(&list->head);
3836b534915SHuang Ying 		cluster_set_null(&list->tail);
3846b534915SHuang Ying 	} else
3856b534915SHuang Ying 		cluster_set_next_flag(&list->head,
3866b534915SHuang Ying 				      cluster_next(&ci[idx]), 0);
3876b534915SHuang Ying 
3886b534915SHuang Ying 	return idx;
3896b534915SHuang Ying }
3906b534915SHuang Ying 
391815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */
392815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si,
393815c2c54SShaohua Li 		unsigned int idx)
394815c2c54SShaohua Li {
395815c2c54SShaohua Li 	/*
396815c2c54SShaohua Li 	 * If scan_swap_map() can't find a free cluster, it will check
397815c2c54SShaohua Li 	 * si->swap_map directly. To make sure the discarding cluster isn't
398815c2c54SShaohua Li 	 * taken by scan_swap_map(), mark the swap entries bad (occupied). It
399815c2c54SShaohua Li 	 * will be cleared after discard
400815c2c54SShaohua Li 	 */
401815c2c54SShaohua Li 	memset(si->swap_map + idx * SWAPFILE_CLUSTER,
402815c2c54SShaohua Li 			SWAP_MAP_BAD, SWAPFILE_CLUSTER);
403815c2c54SShaohua Li 
4046b534915SHuang Ying 	cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx);
405815c2c54SShaohua Li 
406815c2c54SShaohua Li 	schedule_work(&si->discard_work);
407815c2c54SShaohua Li }
408815c2c54SShaohua Li 
40938d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx)
41038d8b4e6SHuang Ying {
41138d8b4e6SHuang Ying 	struct swap_cluster_info *ci = si->cluster_info;
41238d8b4e6SHuang Ying 
41338d8b4e6SHuang Ying 	cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE);
41438d8b4e6SHuang Ying 	cluster_list_add_tail(&si->free_clusters, ci, idx);
41538d8b4e6SHuang Ying }
41638d8b4e6SHuang Ying 
417815c2c54SShaohua Li /*
418815c2c54SShaohua Li  * Doing discard actually. After a cluster discard is finished, the cluster
419815c2c54SShaohua Li  * will be added to free cluster list. caller should hold si->lock.
420815c2c54SShaohua Li */
421815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si)
422815c2c54SShaohua Li {
423235b6217SHuang, Ying 	struct swap_cluster_info *info, *ci;
424815c2c54SShaohua Li 	unsigned int idx;
425815c2c54SShaohua Li 
426815c2c54SShaohua Li 	info = si->cluster_info;
427815c2c54SShaohua Li 
4286b534915SHuang Ying 	while (!cluster_list_empty(&si->discard_clusters)) {
4296b534915SHuang Ying 		idx = cluster_list_del_first(&si->discard_clusters, info);
430815c2c54SShaohua Li 		spin_unlock(&si->lock);
431815c2c54SShaohua Li 
432815c2c54SShaohua Li 		discard_swap_cluster(si, idx * SWAPFILE_CLUSTER,
433815c2c54SShaohua Li 				SWAPFILE_CLUSTER);
434815c2c54SShaohua Li 
435815c2c54SShaohua Li 		spin_lock(&si->lock);
436235b6217SHuang, Ying 		ci = lock_cluster(si, idx * SWAPFILE_CLUSTER);
43738d8b4e6SHuang Ying 		__free_cluster(si, idx);
438815c2c54SShaohua Li 		memset(si->swap_map + idx * SWAPFILE_CLUSTER,
439815c2c54SShaohua Li 				0, SWAPFILE_CLUSTER);
440235b6217SHuang, Ying 		unlock_cluster(ci);
441815c2c54SShaohua Li 	}
442815c2c54SShaohua Li }
443815c2c54SShaohua Li 
444815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work)
445815c2c54SShaohua Li {
446815c2c54SShaohua Li 	struct swap_info_struct *si;
447815c2c54SShaohua Li 
448815c2c54SShaohua Li 	si = container_of(work, struct swap_info_struct, discard_work);
449815c2c54SShaohua Li 
450815c2c54SShaohua Li 	spin_lock(&si->lock);
451815c2c54SShaohua Li 	swap_do_scheduled_discard(si);
452815c2c54SShaohua Li 	spin_unlock(&si->lock);
453815c2c54SShaohua Li }
454815c2c54SShaohua Li 
45538d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx)
45638d8b4e6SHuang Ying {
45738d8b4e6SHuang Ying 	struct swap_cluster_info *ci = si->cluster_info;
45838d8b4e6SHuang Ying 
45938d8b4e6SHuang Ying 	VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx);
46038d8b4e6SHuang Ying 	cluster_list_del_first(&si->free_clusters, ci);
46138d8b4e6SHuang Ying 	cluster_set_count_flag(ci + idx, 0, 0);
46238d8b4e6SHuang Ying }
46338d8b4e6SHuang Ying 
46438d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx)
46538d8b4e6SHuang Ying {
46638d8b4e6SHuang Ying 	struct swap_cluster_info *ci = si->cluster_info + idx;
46738d8b4e6SHuang Ying 
46838d8b4e6SHuang Ying 	VM_BUG_ON(cluster_count(ci) != 0);
46938d8b4e6SHuang Ying 	/*
47038d8b4e6SHuang Ying 	 * If the swap is discardable, prepare discard the cluster
47138d8b4e6SHuang Ying 	 * instead of free it immediately. The cluster will be freed
47238d8b4e6SHuang Ying 	 * after discard.
47338d8b4e6SHuang Ying 	 */
47438d8b4e6SHuang Ying 	if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) ==
47538d8b4e6SHuang Ying 	    (SWP_WRITEOK | SWP_PAGE_DISCARD)) {
47638d8b4e6SHuang Ying 		swap_cluster_schedule_discard(si, idx);
47738d8b4e6SHuang Ying 		return;
47838d8b4e6SHuang Ying 	}
47938d8b4e6SHuang Ying 
48038d8b4e6SHuang Ying 	__free_cluster(si, idx);
48138d8b4e6SHuang Ying }
48238d8b4e6SHuang Ying 
4832a8f9449SShaohua Li /*
4842a8f9449SShaohua Li  * The cluster corresponding to page_nr will be used. The cluster will be
4852a8f9449SShaohua Li  * removed from free cluster list and its usage counter will be increased.
4862a8f9449SShaohua Li  */
4872a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p,
4882a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
4892a8f9449SShaohua Li {
4902a8f9449SShaohua Li 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
4912a8f9449SShaohua Li 
4922a8f9449SShaohua Li 	if (!cluster_info)
4932a8f9449SShaohua Li 		return;
49438d8b4e6SHuang Ying 	if (cluster_is_free(&cluster_info[idx]))
49538d8b4e6SHuang Ying 		alloc_cluster(p, idx);
4962a8f9449SShaohua Li 
4972a8f9449SShaohua Li 	VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER);
4982a8f9449SShaohua Li 	cluster_set_count(&cluster_info[idx],
4992a8f9449SShaohua Li 		cluster_count(&cluster_info[idx]) + 1);
5002a8f9449SShaohua Li }
5012a8f9449SShaohua Li 
5022a8f9449SShaohua Li /*
5032a8f9449SShaohua Li  * The cluster corresponding to page_nr decreases one usage. If the usage
5042a8f9449SShaohua Li  * counter becomes 0, which means no page in the cluster is in using, we can
5052a8f9449SShaohua Li  * optionally discard the cluster and add it to free cluster list.
5062a8f9449SShaohua Li  */
5072a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p,
5082a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info, unsigned long page_nr)
5092a8f9449SShaohua Li {
5102a8f9449SShaohua Li 	unsigned long idx = page_nr / SWAPFILE_CLUSTER;
5112a8f9449SShaohua Li 
5122a8f9449SShaohua Li 	if (!cluster_info)
5132a8f9449SShaohua Li 		return;
5142a8f9449SShaohua Li 
5152a8f9449SShaohua Li 	VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0);
5162a8f9449SShaohua Li 	cluster_set_count(&cluster_info[idx],
5172a8f9449SShaohua Li 		cluster_count(&cluster_info[idx]) - 1);
5182a8f9449SShaohua Li 
51938d8b4e6SHuang Ying 	if (cluster_count(&cluster_info[idx]) == 0)
52038d8b4e6SHuang Ying 		free_cluster(p, idx);
5212a8f9449SShaohua Li }
5222a8f9449SShaohua Li 
5232a8f9449SShaohua Li /*
5242a8f9449SShaohua Li  * It's possible scan_swap_map() uses a free cluster in the middle of free
5252a8f9449SShaohua Li  * cluster list. Avoiding such abuse to avoid list corruption.
5262a8f9449SShaohua Li  */
527ebc2a1a6SShaohua Li static bool
528ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si,
5292a8f9449SShaohua Li 	unsigned long offset)
5302a8f9449SShaohua Li {
531ebc2a1a6SShaohua Li 	struct percpu_cluster *percpu_cluster;
532ebc2a1a6SShaohua Li 	bool conflict;
533ebc2a1a6SShaohua Li 
5342a8f9449SShaohua Li 	offset /= SWAPFILE_CLUSTER;
5356b534915SHuang Ying 	conflict = !cluster_list_empty(&si->free_clusters) &&
5366b534915SHuang Ying 		offset != cluster_list_first(&si->free_clusters) &&
5372a8f9449SShaohua Li 		cluster_is_free(&si->cluster_info[offset]);
538ebc2a1a6SShaohua Li 
539ebc2a1a6SShaohua Li 	if (!conflict)
540ebc2a1a6SShaohua Li 		return false;
541ebc2a1a6SShaohua Li 
542ebc2a1a6SShaohua Li 	percpu_cluster = this_cpu_ptr(si->percpu_cluster);
543ebc2a1a6SShaohua Li 	cluster_set_null(&percpu_cluster->index);
544ebc2a1a6SShaohua Li 	return true;
545ebc2a1a6SShaohua Li }
546ebc2a1a6SShaohua Li 
547ebc2a1a6SShaohua Li /*
548ebc2a1a6SShaohua Li  * Try to get a swap entry from current cpu's swap entry pool (a cluster). This
549ebc2a1a6SShaohua Li  * might involve allocating a new cluster for current CPU too.
550ebc2a1a6SShaohua Li  */
55136005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si,
552ebc2a1a6SShaohua Li 	unsigned long *offset, unsigned long *scan_base)
553ebc2a1a6SShaohua Li {
554ebc2a1a6SShaohua Li 	struct percpu_cluster *cluster;
555235b6217SHuang, Ying 	struct swap_cluster_info *ci;
556ebc2a1a6SShaohua Li 	bool found_free;
557235b6217SHuang, Ying 	unsigned long tmp, max;
558ebc2a1a6SShaohua Li 
559ebc2a1a6SShaohua Li new_cluster:
560ebc2a1a6SShaohua Li 	cluster = this_cpu_ptr(si->percpu_cluster);
561ebc2a1a6SShaohua Li 	if (cluster_is_null(&cluster->index)) {
5626b534915SHuang Ying 		if (!cluster_list_empty(&si->free_clusters)) {
5636b534915SHuang Ying 			cluster->index = si->free_clusters.head;
564ebc2a1a6SShaohua Li 			cluster->next = cluster_next(&cluster->index) *
565ebc2a1a6SShaohua Li 					SWAPFILE_CLUSTER;
5666b534915SHuang Ying 		} else if (!cluster_list_empty(&si->discard_clusters)) {
567ebc2a1a6SShaohua Li 			/*
568ebc2a1a6SShaohua Li 			 * we don't have free cluster but have some clusters in
569ebc2a1a6SShaohua Li 			 * discarding, do discard now and reclaim them
570ebc2a1a6SShaohua Li 			 */
571ebc2a1a6SShaohua Li 			swap_do_scheduled_discard(si);
572ebc2a1a6SShaohua Li 			*scan_base = *offset = si->cluster_next;
573ebc2a1a6SShaohua Li 			goto new_cluster;
574ebc2a1a6SShaohua Li 		} else
57536005baeSTim Chen 			return false;
576ebc2a1a6SShaohua Li 	}
577ebc2a1a6SShaohua Li 
578ebc2a1a6SShaohua Li 	found_free = false;
579ebc2a1a6SShaohua Li 
580ebc2a1a6SShaohua Li 	/*
581ebc2a1a6SShaohua Li 	 * Other CPUs can use our cluster if they can't find a free cluster,
582ebc2a1a6SShaohua Li 	 * check if there is still free entry in the cluster
583ebc2a1a6SShaohua Li 	 */
584ebc2a1a6SShaohua Li 	tmp = cluster->next;
585235b6217SHuang, Ying 	max = min_t(unsigned long, si->max,
586235b6217SHuang, Ying 		    (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER);
587235b6217SHuang, Ying 	if (tmp >= max) {
588235b6217SHuang, Ying 		cluster_set_null(&cluster->index);
589235b6217SHuang, Ying 		goto new_cluster;
590235b6217SHuang, Ying 	}
591235b6217SHuang, Ying 	ci = lock_cluster(si, tmp);
592235b6217SHuang, Ying 	while (tmp < max) {
593ebc2a1a6SShaohua Li 		if (!si->swap_map[tmp]) {
594ebc2a1a6SShaohua Li 			found_free = true;
595ebc2a1a6SShaohua Li 			break;
596ebc2a1a6SShaohua Li 		}
597ebc2a1a6SShaohua Li 		tmp++;
598ebc2a1a6SShaohua Li 	}
599235b6217SHuang, Ying 	unlock_cluster(ci);
600ebc2a1a6SShaohua Li 	if (!found_free) {
601ebc2a1a6SShaohua Li 		cluster_set_null(&cluster->index);
602ebc2a1a6SShaohua Li 		goto new_cluster;
603ebc2a1a6SShaohua Li 	}
604ebc2a1a6SShaohua Li 	cluster->next = tmp + 1;
605ebc2a1a6SShaohua Li 	*offset = tmp;
606ebc2a1a6SShaohua Li 	*scan_base = tmp;
60736005baeSTim Chen 	return found_free;
6082a8f9449SShaohua Li }
6092a8f9449SShaohua Li 
610a2468cc9SAaron Lu static void __del_from_avail_list(struct swap_info_struct *p)
611a2468cc9SAaron Lu {
612a2468cc9SAaron Lu 	int nid;
613a2468cc9SAaron Lu 
614a2468cc9SAaron Lu 	for_each_node(nid)
615a2468cc9SAaron Lu 		plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]);
616a2468cc9SAaron Lu }
617a2468cc9SAaron Lu 
618a2468cc9SAaron Lu static void del_from_avail_list(struct swap_info_struct *p)
619a2468cc9SAaron Lu {
620a2468cc9SAaron Lu 	spin_lock(&swap_avail_lock);
621a2468cc9SAaron Lu 	__del_from_avail_list(p);
622a2468cc9SAaron Lu 	spin_unlock(&swap_avail_lock);
623a2468cc9SAaron Lu }
624a2468cc9SAaron Lu 
62538d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset,
62638d8b4e6SHuang Ying 			     unsigned int nr_entries)
62738d8b4e6SHuang Ying {
62838d8b4e6SHuang Ying 	unsigned int end = offset + nr_entries - 1;
62938d8b4e6SHuang Ying 
63038d8b4e6SHuang Ying 	if (offset == si->lowest_bit)
63138d8b4e6SHuang Ying 		si->lowest_bit += nr_entries;
63238d8b4e6SHuang Ying 	if (end == si->highest_bit)
63338d8b4e6SHuang Ying 		si->highest_bit -= nr_entries;
63438d8b4e6SHuang Ying 	si->inuse_pages += nr_entries;
63538d8b4e6SHuang Ying 	if (si->inuse_pages == si->pages) {
63638d8b4e6SHuang Ying 		si->lowest_bit = si->max;
63738d8b4e6SHuang Ying 		si->highest_bit = 0;
638a2468cc9SAaron Lu 		del_from_avail_list(si);
63938d8b4e6SHuang Ying 	}
64038d8b4e6SHuang Ying }
64138d8b4e6SHuang Ying 
642a2468cc9SAaron Lu static void add_to_avail_list(struct swap_info_struct *p)
643a2468cc9SAaron Lu {
644a2468cc9SAaron Lu 	int nid;
645a2468cc9SAaron Lu 
646a2468cc9SAaron Lu 	spin_lock(&swap_avail_lock);
647a2468cc9SAaron Lu 	for_each_node(nid) {
648a2468cc9SAaron Lu 		WARN_ON(!plist_node_empty(&p->avail_lists[nid]));
649a2468cc9SAaron Lu 		plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]);
650a2468cc9SAaron Lu 	}
651a2468cc9SAaron Lu 	spin_unlock(&swap_avail_lock);
652a2468cc9SAaron Lu }
653a2468cc9SAaron Lu 
65438d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset,
65538d8b4e6SHuang Ying 			    unsigned int nr_entries)
65638d8b4e6SHuang Ying {
65738d8b4e6SHuang Ying 	unsigned long end = offset + nr_entries - 1;
65838d8b4e6SHuang Ying 	void (*swap_slot_free_notify)(struct block_device *, unsigned long);
65938d8b4e6SHuang Ying 
66038d8b4e6SHuang Ying 	if (offset < si->lowest_bit)
66138d8b4e6SHuang Ying 		si->lowest_bit = offset;
66238d8b4e6SHuang Ying 	if (end > si->highest_bit) {
66338d8b4e6SHuang Ying 		bool was_full = !si->highest_bit;
66438d8b4e6SHuang Ying 
66538d8b4e6SHuang Ying 		si->highest_bit = end;
666a2468cc9SAaron Lu 		if (was_full && (si->flags & SWP_WRITEOK))
667a2468cc9SAaron Lu 			add_to_avail_list(si);
66838d8b4e6SHuang Ying 	}
66938d8b4e6SHuang Ying 	atomic_long_add(nr_entries, &nr_swap_pages);
67038d8b4e6SHuang Ying 	si->inuse_pages -= nr_entries;
67138d8b4e6SHuang Ying 	if (si->flags & SWP_BLKDEV)
67238d8b4e6SHuang Ying 		swap_slot_free_notify =
67338d8b4e6SHuang Ying 			si->bdev->bd_disk->fops->swap_slot_free_notify;
67438d8b4e6SHuang Ying 	else
67538d8b4e6SHuang Ying 		swap_slot_free_notify = NULL;
67638d8b4e6SHuang Ying 	while (offset <= end) {
67738d8b4e6SHuang Ying 		frontswap_invalidate_page(si->type, offset);
67838d8b4e6SHuang Ying 		if (swap_slot_free_notify)
67938d8b4e6SHuang Ying 			swap_slot_free_notify(si->bdev, offset);
68038d8b4e6SHuang Ying 		offset++;
68138d8b4e6SHuang Ying 	}
68238d8b4e6SHuang Ying }
68338d8b4e6SHuang Ying 
68436005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si,
68536005baeSTim Chen 			       unsigned char usage, int nr,
68636005baeSTim Chen 			       swp_entry_t slots[])
6871da177e4SLinus Torvalds {
688235b6217SHuang, Ying 	struct swap_cluster_info *ci;
689ebebbbe9SHugh Dickins 	unsigned long offset;
690c60aa176SHugh Dickins 	unsigned long scan_base;
6917992fde7SHugh Dickins 	unsigned long last_in_cluster = 0;
692048c27fdSHugh Dickins 	int latency_ration = LATENCY_LIMIT;
69336005baeSTim Chen 	int n_ret = 0;
69436005baeSTim Chen 
69536005baeSTim Chen 	if (nr > SWAP_BATCH)
69636005baeSTim Chen 		nr = SWAP_BATCH;
6971da177e4SLinus Torvalds 
6987dfad418SHugh Dickins 	/*
6997dfad418SHugh Dickins 	 * We try to cluster swap pages by allocating them sequentially
7007dfad418SHugh Dickins 	 * in swap.  Once we've allocated SWAPFILE_CLUSTER pages this
7017dfad418SHugh Dickins 	 * way, however, we resort to first-free allocation, starting
7027dfad418SHugh Dickins 	 * a new cluster.  This prevents us from scattering swap pages
7037dfad418SHugh Dickins 	 * all over the entire swap partition, so that we reduce
7047dfad418SHugh Dickins 	 * overall disk seek times between swap pages.  -- sct
7057dfad418SHugh Dickins 	 * But we do now try to find an empty cluster.  -Andrea
706c60aa176SHugh Dickins 	 * And we let swap pages go all over an SSD partition.  Hugh
7071da177e4SLinus Torvalds 	 */
7087dfad418SHugh Dickins 
70952b7efdbSHugh Dickins 	si->flags += SWP_SCANNING;
710c60aa176SHugh Dickins 	scan_base = offset = si->cluster_next;
711ebebbbe9SHugh Dickins 
712ebc2a1a6SShaohua Li 	/* SSD algorithm */
713ebc2a1a6SShaohua Li 	if (si->cluster_info) {
71436005baeSTim Chen 		if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
715815c2c54SShaohua Li 			goto checks;
71636005baeSTim Chen 		else
71736005baeSTim Chen 			goto scan;
718815c2c54SShaohua Li 	}
719815c2c54SShaohua Li 
720ebc2a1a6SShaohua Li 	if (unlikely(!si->cluster_nr--)) {
721ebc2a1a6SShaohua Li 		if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
722ebc2a1a6SShaohua Li 			si->cluster_nr = SWAPFILE_CLUSTER - 1;
7232a8f9449SShaohua Li 			goto checks;
7242a8f9449SShaohua Li 		}
7252a8f9449SShaohua Li 
726ec8acf20SShaohua Li 		spin_unlock(&si->lock);
7277dfad418SHugh Dickins 
728c60aa176SHugh Dickins 		/*
729c60aa176SHugh Dickins 		 * If seek is expensive, start searching for new cluster from
730c60aa176SHugh Dickins 		 * start of partition, to minimize the span of allocated swap.
73150088c44SChen Yucong 		 * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info
73250088c44SChen Yucong 		 * case, just handled by scan_swap_map_try_ssd_cluster() above.
733c60aa176SHugh Dickins 		 */
734c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
7357dfad418SHugh Dickins 		last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
7367dfad418SHugh Dickins 
7377dfad418SHugh Dickins 		/* Locate the first empty (unaligned) cluster */
7387dfad418SHugh Dickins 		for (; last_in_cluster <= si->highest_bit; offset++) {
7391da177e4SLinus Torvalds 			if (si->swap_map[offset])
7407dfad418SHugh Dickins 				last_in_cluster = offset + SWAPFILE_CLUSTER;
7417dfad418SHugh Dickins 			else if (offset == last_in_cluster) {
742ec8acf20SShaohua Li 				spin_lock(&si->lock);
743ebebbbe9SHugh Dickins 				offset -= SWAPFILE_CLUSTER - 1;
744ebebbbe9SHugh Dickins 				si->cluster_next = offset;
745ebebbbe9SHugh Dickins 				si->cluster_nr = SWAPFILE_CLUSTER - 1;
746ebebbbe9SHugh Dickins 				goto checks;
7477dfad418SHugh Dickins 			}
748048c27fdSHugh Dickins 			if (unlikely(--latency_ration < 0)) {
749048c27fdSHugh Dickins 				cond_resched();
750048c27fdSHugh Dickins 				latency_ration = LATENCY_LIMIT;
751048c27fdSHugh Dickins 			}
7527dfad418SHugh Dickins 		}
753ebebbbe9SHugh Dickins 
754c60aa176SHugh Dickins 		offset = scan_base;
755ec8acf20SShaohua Li 		spin_lock(&si->lock);
756ebebbbe9SHugh Dickins 		si->cluster_nr = SWAPFILE_CLUSTER - 1;
7577dfad418SHugh Dickins 	}
7587dfad418SHugh Dickins 
759ebebbbe9SHugh Dickins checks:
760ebc2a1a6SShaohua Li 	if (si->cluster_info) {
76136005baeSTim Chen 		while (scan_swap_map_ssd_cluster_conflict(si, offset)) {
76236005baeSTim Chen 		/* take a break if we already got some slots */
76336005baeSTim Chen 			if (n_ret)
76436005baeSTim Chen 				goto done;
76536005baeSTim Chen 			if (!scan_swap_map_try_ssd_cluster(si, &offset,
76636005baeSTim Chen 							&scan_base))
76736005baeSTim Chen 				goto scan;
76836005baeSTim Chen 		}
769ebc2a1a6SShaohua Li 	}
770ebebbbe9SHugh Dickins 	if (!(si->flags & SWP_WRITEOK))
77152b7efdbSHugh Dickins 		goto no_page;
7727dfad418SHugh Dickins 	if (!si->highest_bit)
7737dfad418SHugh Dickins 		goto no_page;
774ebebbbe9SHugh Dickins 	if (offset > si->highest_bit)
775c60aa176SHugh Dickins 		scan_base = offset = si->lowest_bit;
776c9e44410SKAMEZAWA Hiroyuki 
777235b6217SHuang, Ying 	ci = lock_cluster(si, offset);
778b73d7fceSHugh Dickins 	/* reuse swap entry of cache-only swap if not busy. */
779b73d7fceSHugh Dickins 	if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
780c9e44410SKAMEZAWA Hiroyuki 		int swap_was_freed;
781235b6217SHuang, Ying 		unlock_cluster(ci);
782ec8acf20SShaohua Li 		spin_unlock(&si->lock);
783c9e44410SKAMEZAWA Hiroyuki 		swap_was_freed = __try_to_reclaim_swap(si, offset);
784ec8acf20SShaohua Li 		spin_lock(&si->lock);
785c9e44410SKAMEZAWA Hiroyuki 		/* entry was freed successfully, try to use this again */
786c9e44410SKAMEZAWA Hiroyuki 		if (swap_was_freed)
787c9e44410SKAMEZAWA Hiroyuki 			goto checks;
788c9e44410SKAMEZAWA Hiroyuki 		goto scan; /* check next one */
789c9e44410SKAMEZAWA Hiroyuki 	}
790c9e44410SKAMEZAWA Hiroyuki 
791235b6217SHuang, Ying 	if (si->swap_map[offset]) {
792235b6217SHuang, Ying 		unlock_cluster(ci);
79336005baeSTim Chen 		if (!n_ret)
794ebebbbe9SHugh Dickins 			goto scan;
79536005baeSTim Chen 		else
79636005baeSTim Chen 			goto done;
797235b6217SHuang, Ying 	}
7982872bb2dSHuang Ying 	si->swap_map[offset] = usage;
7992872bb2dSHuang Ying 	inc_cluster_info_page(si, si->cluster_info, offset);
8002872bb2dSHuang Ying 	unlock_cluster(ci);
801ebebbbe9SHugh Dickins 
80238d8b4e6SHuang Ying 	swap_range_alloc(si, offset, 1);
8031da177e4SLinus Torvalds 	si->cluster_next = offset + 1;
80436005baeSTim Chen 	slots[n_ret++] = swp_entry(si->type, offset);
8057992fde7SHugh Dickins 
80636005baeSTim Chen 	/* got enough slots or reach max slots? */
80736005baeSTim Chen 	if ((n_ret == nr) || (offset >= si->highest_bit))
80836005baeSTim Chen 		goto done;
80936005baeSTim Chen 
81036005baeSTim Chen 	/* search for next available slot */
81136005baeSTim Chen 
81236005baeSTim Chen 	/* time to take a break? */
81336005baeSTim Chen 	if (unlikely(--latency_ration < 0)) {
81436005baeSTim Chen 		if (n_ret)
81536005baeSTim Chen 			goto done;
81636005baeSTim Chen 		spin_unlock(&si->lock);
81736005baeSTim Chen 		cond_resched();
81836005baeSTim Chen 		spin_lock(&si->lock);
81936005baeSTim Chen 		latency_ration = LATENCY_LIMIT;
82036005baeSTim Chen 	}
82136005baeSTim Chen 
82236005baeSTim Chen 	/* try to get more slots in cluster */
82336005baeSTim Chen 	if (si->cluster_info) {
82436005baeSTim Chen 		if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base))
82536005baeSTim Chen 			goto checks;
82636005baeSTim Chen 		else
82736005baeSTim Chen 			goto done;
82836005baeSTim Chen 	}
82936005baeSTim Chen 	/* non-ssd case */
83036005baeSTim Chen 	++offset;
83136005baeSTim Chen 
83236005baeSTim Chen 	/* non-ssd case, still more slots in cluster? */
83336005baeSTim Chen 	if (si->cluster_nr && !si->swap_map[offset]) {
83436005baeSTim Chen 		--si->cluster_nr;
83536005baeSTim Chen 		goto checks;
83636005baeSTim Chen 	}
83736005baeSTim Chen 
83836005baeSTim Chen done:
83936005baeSTim Chen 	si->flags -= SWP_SCANNING;
84036005baeSTim Chen 	return n_ret;
8417dfad418SHugh Dickins 
842ebebbbe9SHugh Dickins scan:
843ec8acf20SShaohua Li 	spin_unlock(&si->lock);
8447dfad418SHugh Dickins 	while (++offset <= si->highest_bit) {
84552b7efdbSHugh Dickins 		if (!si->swap_map[offset]) {
846ec8acf20SShaohua Li 			spin_lock(&si->lock);
84752b7efdbSHugh Dickins 			goto checks;
8487dfad418SHugh Dickins 		}
849c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
850ec8acf20SShaohua Li 			spin_lock(&si->lock);
851c9e44410SKAMEZAWA Hiroyuki 			goto checks;
852c9e44410SKAMEZAWA Hiroyuki 		}
853048c27fdSHugh Dickins 		if (unlikely(--latency_ration < 0)) {
854048c27fdSHugh Dickins 			cond_resched();
855048c27fdSHugh Dickins 			latency_ration = LATENCY_LIMIT;
856048c27fdSHugh Dickins 		}
85752b7efdbSHugh Dickins 	}
858c60aa176SHugh Dickins 	offset = si->lowest_bit;
859a5998061SJamie Liu 	while (offset < scan_base) {
860c60aa176SHugh Dickins 		if (!si->swap_map[offset]) {
861ec8acf20SShaohua Li 			spin_lock(&si->lock);
862ebebbbe9SHugh Dickins 			goto checks;
863c60aa176SHugh Dickins 		}
864c9e44410SKAMEZAWA Hiroyuki 		if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
865ec8acf20SShaohua Li 			spin_lock(&si->lock);
866c9e44410SKAMEZAWA Hiroyuki 			goto checks;
867c9e44410SKAMEZAWA Hiroyuki 		}
868c60aa176SHugh Dickins 		if (unlikely(--latency_ration < 0)) {
869c60aa176SHugh Dickins 			cond_resched();
870c60aa176SHugh Dickins 			latency_ration = LATENCY_LIMIT;
871c60aa176SHugh Dickins 		}
872a5998061SJamie Liu 		offset++;
873c60aa176SHugh Dickins 	}
874ec8acf20SShaohua Li 	spin_lock(&si->lock);
8757dfad418SHugh Dickins 
8767dfad418SHugh Dickins no_page:
87752b7efdbSHugh Dickins 	si->flags -= SWP_SCANNING;
87836005baeSTim Chen 	return n_ret;
8791da177e4SLinus Torvalds }
8801da177e4SLinus Torvalds 
88138d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)
88238d8b4e6SHuang Ying {
88338d8b4e6SHuang Ying 	unsigned long idx;
88438d8b4e6SHuang Ying 	struct swap_cluster_info *ci;
88538d8b4e6SHuang Ying 	unsigned long offset, i;
88638d8b4e6SHuang Ying 	unsigned char *map;
88738d8b4e6SHuang Ying 
888fe5266d5SHuang Ying 	/*
889fe5266d5SHuang Ying 	 * Should not even be attempting cluster allocations when huge
890fe5266d5SHuang Ying 	 * page swap is disabled.  Warn and fail the allocation.
891fe5266d5SHuang Ying 	 */
892fe5266d5SHuang Ying 	if (!IS_ENABLED(CONFIG_THP_SWAP)) {
893fe5266d5SHuang Ying 		VM_WARN_ON_ONCE(1);
894fe5266d5SHuang Ying 		return 0;
895fe5266d5SHuang Ying 	}
896fe5266d5SHuang Ying 
89738d8b4e6SHuang Ying 	if (cluster_list_empty(&si->free_clusters))
89838d8b4e6SHuang Ying 		return 0;
89938d8b4e6SHuang Ying 
90038d8b4e6SHuang Ying 	idx = cluster_list_first(&si->free_clusters);
90138d8b4e6SHuang Ying 	offset = idx * SWAPFILE_CLUSTER;
90238d8b4e6SHuang Ying 	ci = lock_cluster(si, offset);
90338d8b4e6SHuang Ying 	alloc_cluster(si, idx);
904e0709829SHuang Ying 	cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE);
90538d8b4e6SHuang Ying 
90638d8b4e6SHuang Ying 	map = si->swap_map + offset;
90738d8b4e6SHuang Ying 	for (i = 0; i < SWAPFILE_CLUSTER; i++)
90838d8b4e6SHuang Ying 		map[i] = SWAP_HAS_CACHE;
90938d8b4e6SHuang Ying 	unlock_cluster(ci);
91038d8b4e6SHuang Ying 	swap_range_alloc(si, offset, SWAPFILE_CLUSTER);
91138d8b4e6SHuang Ying 	*slot = swp_entry(si->type, offset);
91238d8b4e6SHuang Ying 
91338d8b4e6SHuang Ying 	return 1;
91438d8b4e6SHuang Ying }
91538d8b4e6SHuang Ying 
91638d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)
91738d8b4e6SHuang Ying {
91838d8b4e6SHuang Ying 	unsigned long offset = idx * SWAPFILE_CLUSTER;
91938d8b4e6SHuang Ying 	struct swap_cluster_info *ci;
92038d8b4e6SHuang Ying 
92138d8b4e6SHuang Ying 	ci = lock_cluster(si, offset);
92238d8b4e6SHuang Ying 	cluster_set_count_flag(ci, 0, 0);
92338d8b4e6SHuang Ying 	free_cluster(si, idx);
92438d8b4e6SHuang Ying 	unlock_cluster(ci);
92538d8b4e6SHuang Ying 	swap_range_free(si, offset, SWAPFILE_CLUSTER);
92638d8b4e6SHuang Ying }
92738d8b4e6SHuang Ying 
92836005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si,
92936005baeSTim Chen 				   unsigned char usage)
93036005baeSTim Chen {
93136005baeSTim Chen 	swp_entry_t entry;
93236005baeSTim Chen 	int n_ret;
93336005baeSTim Chen 
93436005baeSTim Chen 	n_ret = scan_swap_map_slots(si, usage, 1, &entry);
93536005baeSTim Chen 
93636005baeSTim Chen 	if (n_ret)
93736005baeSTim Chen 		return swp_offset(entry);
93836005baeSTim Chen 	else
93936005baeSTim Chen 		return 0;
94036005baeSTim Chen 
94136005baeSTim Chen }
94236005baeSTim Chen 
943*5d5e8f19SHuang Ying int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
9441da177e4SLinus Torvalds {
945*5d5e8f19SHuang Ying 	unsigned long size = swap_entry_size(entry_size);
946adfab836SDan Streetman 	struct swap_info_struct *si, *next;
94736005baeSTim Chen 	long avail_pgs;
94836005baeSTim Chen 	int n_ret = 0;
949a2468cc9SAaron Lu 	int node;
9501da177e4SLinus Torvalds 
95138d8b4e6SHuang Ying 	/* Only single cluster request supported */
952*5d5e8f19SHuang Ying 	WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
95338d8b4e6SHuang Ying 
954*5d5e8f19SHuang Ying 	avail_pgs = atomic_long_read(&nr_swap_pages) / size;
95536005baeSTim Chen 	if (avail_pgs <= 0)
956fb4f88dcSHugh Dickins 		goto noswap;
95736005baeSTim Chen 
95836005baeSTim Chen 	if (n_goal > SWAP_BATCH)
95936005baeSTim Chen 		n_goal = SWAP_BATCH;
96036005baeSTim Chen 
96136005baeSTim Chen 	if (n_goal > avail_pgs)
96236005baeSTim Chen 		n_goal = avail_pgs;
96336005baeSTim Chen 
964*5d5e8f19SHuang Ying 	atomic_long_sub(n_goal * size, &nr_swap_pages);
9651da177e4SLinus Torvalds 
96618ab4d4cSDan Streetman 	spin_lock(&swap_avail_lock);
96718ab4d4cSDan Streetman 
96818ab4d4cSDan Streetman start_over:
969a2468cc9SAaron Lu 	node = numa_node_id();
970a2468cc9SAaron Lu 	plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
97118ab4d4cSDan Streetman 		/* requeue si to after same-priority siblings */
972a2468cc9SAaron Lu 		plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]);
97318ab4d4cSDan Streetman 		spin_unlock(&swap_avail_lock);
974ec8acf20SShaohua Li 		spin_lock(&si->lock);
975adfab836SDan Streetman 		if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) {
97618ab4d4cSDan Streetman 			spin_lock(&swap_avail_lock);
977a2468cc9SAaron Lu 			if (plist_node_empty(&si->avail_lists[node])) {
978ec8acf20SShaohua Li 				spin_unlock(&si->lock);
97918ab4d4cSDan Streetman 				goto nextsi;
98018ab4d4cSDan Streetman 			}
98118ab4d4cSDan Streetman 			WARN(!si->highest_bit,
98218ab4d4cSDan Streetman 			     "swap_info %d in list but !highest_bit\n",
98318ab4d4cSDan Streetman 			     si->type);
98418ab4d4cSDan Streetman 			WARN(!(si->flags & SWP_WRITEOK),
98518ab4d4cSDan Streetman 			     "swap_info %d in list but !SWP_WRITEOK\n",
98618ab4d4cSDan Streetman 			     si->type);
987a2468cc9SAaron Lu 			__del_from_avail_list(si);
98818ab4d4cSDan Streetman 			spin_unlock(&si->lock);
98918ab4d4cSDan Streetman 			goto nextsi;
990ec8acf20SShaohua Li 		}
991*5d5e8f19SHuang Ying 		if (size == SWAPFILE_CLUSTER) {
992f0eea189SHuang Ying 			if (!(si->flags & SWP_FILE))
99338d8b4e6SHuang Ying 				n_ret = swap_alloc_cluster(si, swp_entries);
994f0eea189SHuang Ying 		} else
99536005baeSTim Chen 			n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE,
99636005baeSTim Chen 						    n_goal, swp_entries);
997ec8acf20SShaohua Li 		spin_unlock(&si->lock);
998*5d5e8f19SHuang Ying 		if (n_ret || size == SWAPFILE_CLUSTER)
99936005baeSTim Chen 			goto check_out;
100018ab4d4cSDan Streetman 		pr_debug("scan_swap_map of si %d failed to find offset\n",
100118ab4d4cSDan Streetman 			si->type);
100236005baeSTim Chen 
100318ab4d4cSDan Streetman 		spin_lock(&swap_avail_lock);
100418ab4d4cSDan Streetman nextsi:
1005adfab836SDan Streetman 		/*
1006adfab836SDan Streetman 		 * if we got here, it's likely that si was almost full before,
1007adfab836SDan Streetman 		 * and since scan_swap_map() can drop the si->lock, multiple
1008adfab836SDan Streetman 		 * callers probably all tried to get a page from the same si
100918ab4d4cSDan Streetman 		 * and it filled up before we could get one; or, the si filled
101018ab4d4cSDan Streetman 		 * up between us dropping swap_avail_lock and taking si->lock.
101118ab4d4cSDan Streetman 		 * Since we dropped the swap_avail_lock, the swap_avail_head
101218ab4d4cSDan Streetman 		 * list may have been modified; so if next is still in the
101336005baeSTim Chen 		 * swap_avail_head list then try it, otherwise start over
101436005baeSTim Chen 		 * if we have not gotten any slots.
1015adfab836SDan Streetman 		 */
1016a2468cc9SAaron Lu 		if (plist_node_empty(&next->avail_lists[node]))
101718ab4d4cSDan Streetman 			goto start_over;
1018fb4f88dcSHugh Dickins 	}
1019fb4f88dcSHugh Dickins 
102018ab4d4cSDan Streetman 	spin_unlock(&swap_avail_lock);
102118ab4d4cSDan Streetman 
102236005baeSTim Chen check_out:
102336005baeSTim Chen 	if (n_ret < n_goal)
1024*5d5e8f19SHuang Ying 		atomic_long_add((long)(n_goal - n_ret) * size,
102538d8b4e6SHuang Ying 				&nr_swap_pages);
1026fb4f88dcSHugh Dickins noswap:
102736005baeSTim Chen 	return n_ret;
102836005baeSTim Chen }
102936005baeSTim Chen 
10302de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */
1031910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type)
1032910321eaSHugh Dickins {
1033910321eaSHugh Dickins 	struct swap_info_struct *si;
1034910321eaSHugh Dickins 	pgoff_t offset;
1035910321eaSHugh Dickins 
1036910321eaSHugh Dickins 	si = swap_info[type];
1037ec8acf20SShaohua Li 	spin_lock(&si->lock);
1038910321eaSHugh Dickins 	if (si && (si->flags & SWP_WRITEOK)) {
1039ec8acf20SShaohua Li 		atomic_long_dec(&nr_swap_pages);
1040910321eaSHugh Dickins 		/* This is called for allocating swap entry, not cache */
1041910321eaSHugh Dickins 		offset = scan_swap_map(si, 1);
1042910321eaSHugh Dickins 		if (offset) {
1043ec8acf20SShaohua Li 			spin_unlock(&si->lock);
1044910321eaSHugh Dickins 			return swp_entry(type, offset);
1045910321eaSHugh Dickins 		}
1046ec8acf20SShaohua Li 		atomic_long_inc(&nr_swap_pages);
1047910321eaSHugh Dickins 	}
1048ec8acf20SShaohua Li 	spin_unlock(&si->lock);
1049910321eaSHugh Dickins 	return (swp_entry_t) {0};
1050910321eaSHugh Dickins }
1051910321eaSHugh Dickins 
1052e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry)
10531da177e4SLinus Torvalds {
10541da177e4SLinus Torvalds 	struct swap_info_struct *p;
10551da177e4SLinus Torvalds 	unsigned long offset, type;
10561da177e4SLinus Torvalds 
10571da177e4SLinus Torvalds 	if (!entry.val)
10581da177e4SLinus Torvalds 		goto out;
10591da177e4SLinus Torvalds 	type = swp_type(entry);
10601da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
10611da177e4SLinus Torvalds 		goto bad_nofile;
1062efa90a98SHugh Dickins 	p = swap_info[type];
10631da177e4SLinus Torvalds 	if (!(p->flags & SWP_USED))
10641da177e4SLinus Torvalds 		goto bad_device;
10651da177e4SLinus Torvalds 	offset = swp_offset(entry);
10661da177e4SLinus Torvalds 	if (offset >= p->max)
10671da177e4SLinus Torvalds 		goto bad_offset;
10681da177e4SLinus Torvalds 	return p;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds bad_offset:
10716a991fc7SHuang, Ying 	pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val);
10721da177e4SLinus Torvalds 	goto out;
10731da177e4SLinus Torvalds bad_device:
10746a991fc7SHuang, Ying 	pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val);
10751da177e4SLinus Torvalds 	goto out;
10761da177e4SLinus Torvalds bad_nofile:
10776a991fc7SHuang, Ying 	pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val);
10781da177e4SLinus Torvalds out:
10791da177e4SLinus Torvalds 	return NULL;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
1082e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry)
1083e8c26ab6STim Chen {
1084e8c26ab6STim Chen 	struct swap_info_struct *p;
1085e8c26ab6STim Chen 
1086e8c26ab6STim Chen 	p = __swap_info_get(entry);
1087e8c26ab6STim Chen 	if (!p)
1088e8c26ab6STim Chen 		goto out;
1089e8c26ab6STim Chen 	if (!p->swap_map[swp_offset(entry)])
1090e8c26ab6STim Chen 		goto bad_free;
1091e8c26ab6STim Chen 	return p;
1092e8c26ab6STim Chen 
1093e8c26ab6STim Chen bad_free:
1094e8c26ab6STim Chen 	pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val);
1095e8c26ab6STim Chen 	goto out;
1096e8c26ab6STim Chen out:
1097e8c26ab6STim Chen 	return NULL;
1098e8c26ab6STim Chen }
1099e8c26ab6STim Chen 
1100235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry)
11011da177e4SLinus Torvalds {
1102235b6217SHuang, Ying 	struct swap_info_struct *p;
1103235b6217SHuang, Ying 
1104235b6217SHuang, Ying 	p = _swap_info_get(entry);
1105235b6217SHuang, Ying 	if (p)
1106235b6217SHuang, Ying 		spin_lock(&p->lock);
1107235b6217SHuang, Ying 	return p;
1108235b6217SHuang, Ying }
1109235b6217SHuang, Ying 
11107c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
11117c00bafeSTim Chen 					struct swap_info_struct *q)
11127c00bafeSTim Chen {
11137c00bafeSTim Chen 	struct swap_info_struct *p;
11147c00bafeSTim Chen 
11157c00bafeSTim Chen 	p = _swap_info_get(entry);
11167c00bafeSTim Chen 
11177c00bafeSTim Chen 	if (p != q) {
11187c00bafeSTim Chen 		if (q != NULL)
11197c00bafeSTim Chen 			spin_unlock(&q->lock);
11207c00bafeSTim Chen 		if (p != NULL)
11217c00bafeSTim Chen 			spin_lock(&p->lock);
11227c00bafeSTim Chen 	}
11237c00bafeSTim Chen 	return p;
11247c00bafeSTim Chen }
11257c00bafeSTim Chen 
11267c00bafeSTim Chen static unsigned char __swap_entry_free(struct swap_info_struct *p,
11277c00bafeSTim Chen 				       swp_entry_t entry, unsigned char usage)
1128235b6217SHuang, Ying {
1129235b6217SHuang, Ying 	struct swap_cluster_info *ci;
1130253d553bSHugh Dickins 	unsigned long offset = swp_offset(entry);
11318d69aaeeSHugh Dickins 	unsigned char count;
11328d69aaeeSHugh Dickins 	unsigned char has_cache;
1133235b6217SHuang, Ying 
11347c00bafeSTim Chen 	ci = lock_cluster_or_swap_info(p, offset);
11351da177e4SLinus Torvalds 
1136355cfa73SKAMEZAWA Hiroyuki 	count = p->swap_map[offset];
1137235b6217SHuang, Ying 
1138253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
1139253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
1140253d553bSHugh Dickins 
1141253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
1142253d553bSHugh Dickins 		VM_BUG_ON(!has_cache);
1143253d553bSHugh Dickins 		has_cache = 0;
1144aaa46865SHugh Dickins 	} else if (count == SWAP_MAP_SHMEM) {
1145aaa46865SHugh Dickins 		/*
1146aaa46865SHugh Dickins 		 * Or we could insist on shmem.c using a special
1147aaa46865SHugh Dickins 		 * swap_shmem_free() and free_shmem_swap_and_cache()...
1148aaa46865SHugh Dickins 		 */
1149aaa46865SHugh Dickins 		count = 0;
1150570a335bSHugh Dickins 	} else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
1151570a335bSHugh Dickins 		if (count == COUNT_CONTINUED) {
1152570a335bSHugh Dickins 			if (swap_count_continued(p, offset, count))
1153570a335bSHugh Dickins 				count = SWAP_MAP_MAX | COUNT_CONTINUED;
1154570a335bSHugh Dickins 			else
1155570a335bSHugh Dickins 				count = SWAP_MAP_MAX;
1156570a335bSHugh Dickins 		} else
1157253d553bSHugh Dickins 			count--;
1158570a335bSHugh Dickins 	}
1159253d553bSHugh Dickins 
1160253d553bSHugh Dickins 	usage = count | has_cache;
11617c00bafeSTim Chen 	p->swap_map[offset] = usage ? : SWAP_HAS_CACHE;
1162253d553bSHugh Dickins 
11637c00bafeSTim Chen 	unlock_cluster_or_swap_info(p, ci);
1164235b6217SHuang, Ying 
11657c00bafeSTim Chen 	return usage;
11667c00bafeSTim Chen }
11677c00bafeSTim Chen 
11687c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry)
11697c00bafeSTim Chen {
11707c00bafeSTim Chen 	struct swap_cluster_info *ci;
11717c00bafeSTim Chen 	unsigned long offset = swp_offset(entry);
11727c00bafeSTim Chen 	unsigned char count;
11737c00bafeSTim Chen 
1174235b6217SHuang, Ying 	ci = lock_cluster(p, offset);
11757c00bafeSTim Chen 	count = p->swap_map[offset];
11767c00bafeSTim Chen 	VM_BUG_ON(count != SWAP_HAS_CACHE);
11777c00bafeSTim Chen 	p->swap_map[offset] = 0;
11782a8f9449SShaohua Li 	dec_cluster_info_page(p, p->cluster_info, offset);
1179235b6217SHuang, Ying 	unlock_cluster(ci);
11807c00bafeSTim Chen 
118138d8b4e6SHuang Ying 	mem_cgroup_uncharge_swap(entry, 1);
118238d8b4e6SHuang Ying 	swap_range_free(p, offset, 1);
11831da177e4SLinus Torvalds }
1184253d553bSHugh Dickins 
11851da177e4SLinus Torvalds /*
11861da177e4SLinus Torvalds  * Caller has made sure that the swap device corresponding to entry
11871da177e4SLinus Torvalds  * is still around or has not been recycled.
11881da177e4SLinus Torvalds  */
11891da177e4SLinus Torvalds void swap_free(swp_entry_t entry)
11901da177e4SLinus Torvalds {
11911da177e4SLinus Torvalds 	struct swap_info_struct *p;
11921da177e4SLinus Torvalds 
1193235b6217SHuang, Ying 	p = _swap_info_get(entry);
11947c00bafeSTim Chen 	if (p) {
11957c00bafeSTim Chen 		if (!__swap_entry_free(p, entry, 1))
119667afa38eSTim Chen 			free_swap_slot(entry);
11977c00bafeSTim Chen 	}
11981da177e4SLinus Torvalds }
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds /*
1201cb4b86baSKAMEZAWA Hiroyuki  * Called after dropping swapcache to decrease refcnt to swap entries.
1202cb4b86baSKAMEZAWA Hiroyuki  */
1203a448f2d0SHuang Ying void put_swap_page(struct page *page, swp_entry_t entry)
120438d8b4e6SHuang Ying {
120538d8b4e6SHuang Ying 	unsigned long offset = swp_offset(entry);
120638d8b4e6SHuang Ying 	unsigned long idx = offset / SWAPFILE_CLUSTER;
120738d8b4e6SHuang Ying 	struct swap_cluster_info *ci;
120838d8b4e6SHuang Ying 	struct swap_info_struct *si;
120938d8b4e6SHuang Ying 	unsigned char *map;
1210a3aea839SHuang Ying 	unsigned int i, free_entries = 0;
1211a3aea839SHuang Ying 	unsigned char val;
1212a448f2d0SHuang Ying 	int size = swap_entry_size(hpage_nr_pages(page));
1213fe5266d5SHuang Ying 
1214a3aea839SHuang Ying 	si = _swap_info_get(entry);
121538d8b4e6SHuang Ying 	if (!si)
121638d8b4e6SHuang Ying 		return;
121738d8b4e6SHuang Ying 
1218a448f2d0SHuang Ying 	if (size == SWAPFILE_CLUSTER) {
121938d8b4e6SHuang Ying 		ci = lock_cluster(si, offset);
1220e0709829SHuang Ying 		VM_BUG_ON(!cluster_is_huge(ci));
122138d8b4e6SHuang Ying 		map = si->swap_map + offset;
122238d8b4e6SHuang Ying 		for (i = 0; i < SWAPFILE_CLUSTER; i++) {
1223a3aea839SHuang Ying 			val = map[i];
1224a3aea839SHuang Ying 			VM_BUG_ON(!(val & SWAP_HAS_CACHE));
1225a3aea839SHuang Ying 			if (val == SWAP_HAS_CACHE)
1226a3aea839SHuang Ying 				free_entries++;
122738d8b4e6SHuang Ying 		}
1228a3aea839SHuang Ying 		if (!free_entries) {
1229a3aea839SHuang Ying 			for (i = 0; i < SWAPFILE_CLUSTER; i++)
1230a3aea839SHuang Ying 				map[i] &= ~SWAP_HAS_CACHE;
1231a3aea839SHuang Ying 		}
1232e0709829SHuang Ying 		cluster_clear_huge(ci);
1233a3aea839SHuang Ying 		unlock_cluster(ci);
1234a3aea839SHuang Ying 		if (free_entries == SWAPFILE_CLUSTER) {
1235a3aea839SHuang Ying 			spin_lock(&si->lock);
1236a3aea839SHuang Ying 			ci = lock_cluster(si, offset);
1237a3aea839SHuang Ying 			memset(map, 0, SWAPFILE_CLUSTER);
123838d8b4e6SHuang Ying 			unlock_cluster(ci);
123938d8b4e6SHuang Ying 			mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER);
124038d8b4e6SHuang Ying 			swap_free_cluster(si, idx);
124138d8b4e6SHuang Ying 			spin_unlock(&si->lock);
1242a448f2d0SHuang Ying 			return;
1243a448f2d0SHuang Ying 		}
1244a448f2d0SHuang Ying 	}
1245a448f2d0SHuang Ying 	if (size == 1 || free_entries) {
1246a448f2d0SHuang Ying 		for (i = 0; i < size; i++, entry.val++) {
1247a3aea839SHuang Ying 			if (!__swap_entry_free(si, entry, SWAP_HAS_CACHE))
1248a3aea839SHuang Ying 				free_swap_slot(entry);
1249a3aea839SHuang Ying 		}
1250a3aea839SHuang Ying 	}
125138d8b4e6SHuang Ying }
125259807685SHuang Ying 
1253fe5266d5SHuang Ying #ifdef CONFIG_THP_SWAP
125459807685SHuang Ying int split_swap_cluster(swp_entry_t entry)
125559807685SHuang Ying {
125659807685SHuang Ying 	struct swap_info_struct *si;
125759807685SHuang Ying 	struct swap_cluster_info *ci;
125859807685SHuang Ying 	unsigned long offset = swp_offset(entry);
125959807685SHuang Ying 
126059807685SHuang Ying 	si = _swap_info_get(entry);
126159807685SHuang Ying 	if (!si)
126259807685SHuang Ying 		return -EBUSY;
126359807685SHuang Ying 	ci = lock_cluster(si, offset);
126459807685SHuang Ying 	cluster_clear_huge(ci);
126559807685SHuang Ying 	unlock_cluster(ci);
126659807685SHuang Ying 	return 0;
126759807685SHuang Ying }
1268fe5266d5SHuang Ying #endif
126938d8b4e6SHuang Ying 
1270155b5f88SHuang Ying static int swp_entry_cmp(const void *ent1, const void *ent2)
1271155b5f88SHuang Ying {
1272155b5f88SHuang Ying 	const swp_entry_t *e1 = ent1, *e2 = ent2;
1273155b5f88SHuang Ying 
1274155b5f88SHuang Ying 	return (int)swp_type(*e1) - (int)swp_type(*e2);
1275155b5f88SHuang Ying }
1276155b5f88SHuang Ying 
12777c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n)
12787c00bafeSTim Chen {
12797c00bafeSTim Chen 	struct swap_info_struct *p, *prev;
12807c00bafeSTim Chen 	int i;
12817c00bafeSTim Chen 
12827c00bafeSTim Chen 	if (n <= 0)
12837c00bafeSTim Chen 		return;
12847c00bafeSTim Chen 
12857c00bafeSTim Chen 	prev = NULL;
12867c00bafeSTim Chen 	p = NULL;
1287155b5f88SHuang Ying 
1288155b5f88SHuang Ying 	/*
1289155b5f88SHuang Ying 	 * Sort swap entries by swap device, so each lock is only taken once.
1290155b5f88SHuang Ying 	 * nr_swapfiles isn't absolutely correct, but the overhead of sort() is
1291155b5f88SHuang Ying 	 * so low that it isn't necessary to optimize further.
1292155b5f88SHuang Ying 	 */
1293155b5f88SHuang Ying 	if (nr_swapfiles > 1)
1294155b5f88SHuang Ying 		sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL);
12957c00bafeSTim Chen 	for (i = 0; i < n; ++i) {
12967c00bafeSTim Chen 		p = swap_info_get_cont(entries[i], prev);
1297235b6217SHuang, Ying 		if (p)
12987c00bafeSTim Chen 			swap_entry_free(p, entries[i]);
12997c00bafeSTim Chen 		prev = p;
13007c00bafeSTim Chen 	}
13017c00bafeSTim Chen 	if (p)
13027c00bafeSTim Chen 		spin_unlock(&p->lock);
1303cb4b86baSKAMEZAWA Hiroyuki }
1304cb4b86baSKAMEZAWA Hiroyuki 
1305cb4b86baSKAMEZAWA Hiroyuki /*
1306c475a8abSHugh Dickins  * How many references to page are currently swapped out?
1307570a335bSHugh Dickins  * This does not give an exact answer when swap count is continued,
1308570a335bSHugh Dickins  * but does include the high COUNT_CONTINUED flag to allow for that.
13091da177e4SLinus Torvalds  */
1310bde05d1cSHugh Dickins int page_swapcount(struct page *page)
13111da177e4SLinus Torvalds {
1312c475a8abSHugh Dickins 	int count = 0;
13131da177e4SLinus Torvalds 	struct swap_info_struct *p;
1314235b6217SHuang, Ying 	struct swap_cluster_info *ci;
13151da177e4SLinus Torvalds 	swp_entry_t entry;
1316235b6217SHuang, Ying 	unsigned long offset;
13171da177e4SLinus Torvalds 
13184c21e2f2SHugh Dickins 	entry.val = page_private(page);
1319235b6217SHuang, Ying 	p = _swap_info_get(entry);
13201da177e4SLinus Torvalds 	if (p) {
1321235b6217SHuang, Ying 		offset = swp_offset(entry);
1322235b6217SHuang, Ying 		ci = lock_cluster_or_swap_info(p, offset);
1323235b6217SHuang, Ying 		count = swap_count(p->swap_map[offset]);
1324235b6217SHuang, Ying 		unlock_cluster_or_swap_info(p, ci);
13251da177e4SLinus Torvalds 	}
1326c475a8abSHugh Dickins 	return count;
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
1329aa8d22a1SMinchan Kim int __swap_count(struct swap_info_struct *si, swp_entry_t entry)
1330aa8d22a1SMinchan Kim {
1331aa8d22a1SMinchan Kim 	pgoff_t offset = swp_offset(entry);
1332aa8d22a1SMinchan Kim 
1333aa8d22a1SMinchan Kim 	return swap_count(si->swap_map[offset]);
1334aa8d22a1SMinchan Kim }
1335aa8d22a1SMinchan Kim 
1336322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
1337322b8afeSHuang Ying {
1338322b8afeSHuang Ying 	int count = 0;
1339322b8afeSHuang Ying 	pgoff_t offset = swp_offset(entry);
1340322b8afeSHuang Ying 	struct swap_cluster_info *ci;
1341322b8afeSHuang Ying 
1342322b8afeSHuang Ying 	ci = lock_cluster_or_swap_info(si, offset);
1343322b8afeSHuang Ying 	count = swap_count(si->swap_map[offset]);
1344322b8afeSHuang Ying 	unlock_cluster_or_swap_info(si, ci);
1345322b8afeSHuang Ying 	return count;
1346322b8afeSHuang Ying }
1347322b8afeSHuang Ying 
13481da177e4SLinus Torvalds /*
13498334b962SMinchan Kim  * How many references to @entry are currently swapped out?
1350e8c26ab6STim Chen  * This does not give an exact answer when swap count is continued,
1351e8c26ab6STim Chen  * but does include the high COUNT_CONTINUED flag to allow for that.
1352e8c26ab6STim Chen  */
1353e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry)
1354e8c26ab6STim Chen {
1355e8c26ab6STim Chen 	int count = 0;
1356e8c26ab6STim Chen 	struct swap_info_struct *si;
1357e8c26ab6STim Chen 
1358e8c26ab6STim Chen 	si = __swap_info_get(entry);
1359322b8afeSHuang Ying 	if (si)
1360322b8afeSHuang Ying 		count = swap_swapcount(si, entry);
1361e8c26ab6STim Chen 	return count;
1362e8c26ab6STim Chen }
1363e8c26ab6STim Chen 
1364e8c26ab6STim Chen /*
1365e8c26ab6STim Chen  * How many references to @entry are currently swapped out?
13668334b962SMinchan Kim  * This considers COUNT_CONTINUED so it returns exact answer.
13678334b962SMinchan Kim  */
13688334b962SMinchan Kim int swp_swapcount(swp_entry_t entry)
13698334b962SMinchan Kim {
13708334b962SMinchan Kim 	int count, tmp_count, n;
13718334b962SMinchan Kim 	struct swap_info_struct *p;
1372235b6217SHuang, Ying 	struct swap_cluster_info *ci;
13738334b962SMinchan Kim 	struct page *page;
13748334b962SMinchan Kim 	pgoff_t offset;
13758334b962SMinchan Kim 	unsigned char *map;
13768334b962SMinchan Kim 
1377235b6217SHuang, Ying 	p = _swap_info_get(entry);
13788334b962SMinchan Kim 	if (!p)
13798334b962SMinchan Kim 		return 0;
13808334b962SMinchan Kim 
1381235b6217SHuang, Ying 	offset = swp_offset(entry);
1382235b6217SHuang, Ying 
1383235b6217SHuang, Ying 	ci = lock_cluster_or_swap_info(p, offset);
1384235b6217SHuang, Ying 
1385235b6217SHuang, Ying 	count = swap_count(p->swap_map[offset]);
13868334b962SMinchan Kim 	if (!(count & COUNT_CONTINUED))
13878334b962SMinchan Kim 		goto out;
13888334b962SMinchan Kim 
13898334b962SMinchan Kim 	count &= ~COUNT_CONTINUED;
13908334b962SMinchan Kim 	n = SWAP_MAP_MAX + 1;
13918334b962SMinchan Kim 
13928334b962SMinchan Kim 	page = vmalloc_to_page(p->swap_map + offset);
13938334b962SMinchan Kim 	offset &= ~PAGE_MASK;
13948334b962SMinchan Kim 	VM_BUG_ON(page_private(page) != SWP_CONTINUED);
13958334b962SMinchan Kim 
13968334b962SMinchan Kim 	do {
1397a8ae4991SGeliang Tang 		page = list_next_entry(page, lru);
13988334b962SMinchan Kim 		map = kmap_atomic(page);
13998334b962SMinchan Kim 		tmp_count = map[offset];
14008334b962SMinchan Kim 		kunmap_atomic(map);
14018334b962SMinchan Kim 
14028334b962SMinchan Kim 		count += (tmp_count & ~COUNT_CONTINUED) * n;
14038334b962SMinchan Kim 		n *= (SWAP_CONT_MAX + 1);
14048334b962SMinchan Kim 	} while (tmp_count & COUNT_CONTINUED);
14058334b962SMinchan Kim out:
1406235b6217SHuang, Ying 	unlock_cluster_or_swap_info(p, ci);
14078334b962SMinchan Kim 	return count;
14088334b962SMinchan Kim }
14098334b962SMinchan Kim 
1410e0709829SHuang Ying static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,
1411e0709829SHuang Ying 					 swp_entry_t entry)
1412e0709829SHuang Ying {
1413e0709829SHuang Ying 	struct swap_cluster_info *ci;
1414e0709829SHuang Ying 	unsigned char *map = si->swap_map;
1415e0709829SHuang Ying 	unsigned long roffset = swp_offset(entry);
1416e0709829SHuang Ying 	unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER);
1417e0709829SHuang Ying 	int i;
1418e0709829SHuang Ying 	bool ret = false;
1419e0709829SHuang Ying 
1420e0709829SHuang Ying 	ci = lock_cluster_or_swap_info(si, offset);
1421e0709829SHuang Ying 	if (!ci || !cluster_is_huge(ci)) {
1422afa4711eSHuang Ying 		if (swap_count(map[roffset]))
1423e0709829SHuang Ying 			ret = true;
1424e0709829SHuang Ying 		goto unlock_out;
1425e0709829SHuang Ying 	}
1426e0709829SHuang Ying 	for (i = 0; i < SWAPFILE_CLUSTER; i++) {
1427afa4711eSHuang Ying 		if (swap_count(map[offset + i])) {
1428e0709829SHuang Ying 			ret = true;
1429e0709829SHuang Ying 			break;
1430e0709829SHuang Ying 		}
1431e0709829SHuang Ying 	}
1432e0709829SHuang Ying unlock_out:
1433e0709829SHuang Ying 	unlock_cluster_or_swap_info(si, ci);
1434e0709829SHuang Ying 	return ret;
1435e0709829SHuang Ying }
1436e0709829SHuang Ying 
1437e0709829SHuang Ying static bool page_swapped(struct page *page)
1438e0709829SHuang Ying {
1439e0709829SHuang Ying 	swp_entry_t entry;
1440e0709829SHuang Ying 	struct swap_info_struct *si;
1441e0709829SHuang Ying 
1442fe5266d5SHuang Ying 	if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page)))
1443e0709829SHuang Ying 		return page_swapcount(page) != 0;
1444e0709829SHuang Ying 
1445e0709829SHuang Ying 	page = compound_head(page);
1446e0709829SHuang Ying 	entry.val = page_private(page);
1447e0709829SHuang Ying 	si = _swap_info_get(entry);
1448e0709829SHuang Ying 	if (si)
1449e0709829SHuang Ying 		return swap_page_trans_huge_swapped(si, entry);
1450e0709829SHuang Ying 	return false;
1451e0709829SHuang Ying }
1452ba3c4ce6SHuang Ying 
1453ba3c4ce6SHuang Ying static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,
1454ba3c4ce6SHuang Ying 					 int *total_swapcount)
1455ba3c4ce6SHuang Ying {
1456ba3c4ce6SHuang Ying 	int i, map_swapcount, _total_mapcount, _total_swapcount;
1457ba3c4ce6SHuang Ying 	unsigned long offset = 0;
1458ba3c4ce6SHuang Ying 	struct swap_info_struct *si;
1459ba3c4ce6SHuang Ying 	struct swap_cluster_info *ci = NULL;
1460ba3c4ce6SHuang Ying 	unsigned char *map = NULL;
1461ba3c4ce6SHuang Ying 	int mapcount, swapcount = 0;
1462ba3c4ce6SHuang Ying 
1463ba3c4ce6SHuang Ying 	/* hugetlbfs shouldn't call it */
1464ba3c4ce6SHuang Ying 	VM_BUG_ON_PAGE(PageHuge(page), page);
1465ba3c4ce6SHuang Ying 
1466fe5266d5SHuang Ying 	if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) {
1467fe5266d5SHuang Ying 		mapcount = page_trans_huge_mapcount(page, total_mapcount);
1468ba3c4ce6SHuang Ying 		if (PageSwapCache(page))
1469ba3c4ce6SHuang Ying 			swapcount = page_swapcount(page);
1470ba3c4ce6SHuang Ying 		if (total_swapcount)
1471ba3c4ce6SHuang Ying 			*total_swapcount = swapcount;
1472ba3c4ce6SHuang Ying 		return mapcount + swapcount;
1473ba3c4ce6SHuang Ying 	}
1474ba3c4ce6SHuang Ying 
1475ba3c4ce6SHuang Ying 	page = compound_head(page);
1476ba3c4ce6SHuang Ying 
1477ba3c4ce6SHuang Ying 	_total_mapcount = _total_swapcount = map_swapcount = 0;
1478ba3c4ce6SHuang Ying 	if (PageSwapCache(page)) {
1479ba3c4ce6SHuang Ying 		swp_entry_t entry;
1480ba3c4ce6SHuang Ying 
1481ba3c4ce6SHuang Ying 		entry.val = page_private(page);
1482ba3c4ce6SHuang Ying 		si = _swap_info_get(entry);
1483ba3c4ce6SHuang Ying 		if (si) {
1484ba3c4ce6SHuang Ying 			map = si->swap_map;
1485ba3c4ce6SHuang Ying 			offset = swp_offset(entry);
1486ba3c4ce6SHuang Ying 		}
1487ba3c4ce6SHuang Ying 	}
1488ba3c4ce6SHuang Ying 	if (map)
1489ba3c4ce6SHuang Ying 		ci = lock_cluster(si, offset);
1490ba3c4ce6SHuang Ying 	for (i = 0; i < HPAGE_PMD_NR; i++) {
1491ba3c4ce6SHuang Ying 		mapcount = atomic_read(&page[i]._mapcount) + 1;
1492ba3c4ce6SHuang Ying 		_total_mapcount += mapcount;
1493ba3c4ce6SHuang Ying 		if (map) {
1494ba3c4ce6SHuang Ying 			swapcount = swap_count(map[offset + i]);
1495ba3c4ce6SHuang Ying 			_total_swapcount += swapcount;
1496ba3c4ce6SHuang Ying 		}
1497ba3c4ce6SHuang Ying 		map_swapcount = max(map_swapcount, mapcount + swapcount);
1498ba3c4ce6SHuang Ying 	}
1499ba3c4ce6SHuang Ying 	unlock_cluster(ci);
1500ba3c4ce6SHuang Ying 	if (PageDoubleMap(page)) {
1501ba3c4ce6SHuang Ying 		map_swapcount -= 1;
1502ba3c4ce6SHuang Ying 		_total_mapcount -= HPAGE_PMD_NR;
1503ba3c4ce6SHuang Ying 	}
1504ba3c4ce6SHuang Ying 	mapcount = compound_mapcount(page);
1505ba3c4ce6SHuang Ying 	map_swapcount += mapcount;
1506ba3c4ce6SHuang Ying 	_total_mapcount += mapcount;
1507ba3c4ce6SHuang Ying 	if (total_mapcount)
1508ba3c4ce6SHuang Ying 		*total_mapcount = _total_mapcount;
1509ba3c4ce6SHuang Ying 	if (total_swapcount)
1510ba3c4ce6SHuang Ying 		*total_swapcount = _total_swapcount;
1511ba3c4ce6SHuang Ying 
1512ba3c4ce6SHuang Ying 	return map_swapcount;
1513ba3c4ce6SHuang Ying }
1514e0709829SHuang Ying 
15158334b962SMinchan Kim /*
15167b1fe597SHugh Dickins  * We can write to an anon page without COW if there are no other references
15177b1fe597SHugh Dickins  * to it.  And as a side-effect, free up its swap: because the old content
15187b1fe597SHugh Dickins  * on disk will never be read, and seeking back there to write new content
15197b1fe597SHugh Dickins  * later would only waste time away from clustering.
15206d0a07edSAndrea Arcangeli  *
1521ba3c4ce6SHuang Ying  * NOTE: total_map_swapcount should not be relied upon by the caller if
15226d0a07edSAndrea Arcangeli  * reuse_swap_page() returns false, but it may be always overwritten
15236d0a07edSAndrea Arcangeli  * (see the other implementation for CONFIG_SWAP=n).
15241da177e4SLinus Torvalds  */
1525ba3c4ce6SHuang Ying bool reuse_swap_page(struct page *page, int *total_map_swapcount)
15261da177e4SLinus Torvalds {
1527ba3c4ce6SHuang Ying 	int count, total_mapcount, total_swapcount;
15281da177e4SLinus Torvalds 
1529309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
15305ad64688SHugh Dickins 	if (unlikely(PageKsm(page)))
15316d0a07edSAndrea Arcangeli 		return false;
1532ba3c4ce6SHuang Ying 	count = page_trans_huge_map_swapcount(page, &total_mapcount,
1533ba3c4ce6SHuang Ying 					      &total_swapcount);
1534ba3c4ce6SHuang Ying 	if (total_map_swapcount)
1535ba3c4ce6SHuang Ying 		*total_map_swapcount = total_mapcount + total_swapcount;
1536ba3c4ce6SHuang Ying 	if (count == 1 && PageSwapCache(page) &&
1537ba3c4ce6SHuang Ying 	    (likely(!PageTransCompound(page)) ||
1538ba3c4ce6SHuang Ying 	     /* The remaining swap count will be freed soon */
1539ba3c4ce6SHuang Ying 	     total_swapcount == page_swapcount(page))) {
1540f0571429SMinchan Kim 		if (!PageWriteback(page)) {
1541ba3c4ce6SHuang Ying 			page = compound_head(page);
15427b1fe597SHugh Dickins 			delete_from_swap_cache(page);
15437b1fe597SHugh Dickins 			SetPageDirty(page);
1544f0571429SMinchan Kim 		} else {
1545f0571429SMinchan Kim 			swp_entry_t entry;
1546f0571429SMinchan Kim 			struct swap_info_struct *p;
1547f0571429SMinchan Kim 
1548f0571429SMinchan Kim 			entry.val = page_private(page);
1549f0571429SMinchan Kim 			p = swap_info_get(entry);
1550f0571429SMinchan Kim 			if (p->flags & SWP_STABLE_WRITES) {
1551f0571429SMinchan Kim 				spin_unlock(&p->lock);
1552f0571429SMinchan Kim 				return false;
1553f0571429SMinchan Kim 			}
1554f0571429SMinchan Kim 			spin_unlock(&p->lock);
15557b1fe597SHugh Dickins 		}
15567b1fe597SHugh Dickins 	}
1557ba3c4ce6SHuang Ying 
15585ad64688SHugh Dickins 	return count <= 1;
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds /*
1562a2c43eedSHugh Dickins  * If swap is getting full, or if there are no more mappings of this page,
1563a2c43eedSHugh Dickins  * then try_to_free_swap is called to free its swap space.
15641da177e4SLinus Torvalds  */
1565a2c43eedSHugh Dickins int try_to_free_swap(struct page *page)
15661da177e4SLinus Torvalds {
1567309381feSSasha Levin 	VM_BUG_ON_PAGE(!PageLocked(page), page);
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	if (!PageSwapCache(page))
15701da177e4SLinus Torvalds 		return 0;
15711da177e4SLinus Torvalds 	if (PageWriteback(page))
15721da177e4SLinus Torvalds 		return 0;
1573e0709829SHuang Ying 	if (page_swapped(page))
15741da177e4SLinus Torvalds 		return 0;
15751da177e4SLinus Torvalds 
1576b73d7fceSHugh Dickins 	/*
1577b73d7fceSHugh Dickins 	 * Once hibernation has begun to create its image of memory,
1578b73d7fceSHugh Dickins 	 * there's a danger that one of the calls to try_to_free_swap()
1579b73d7fceSHugh Dickins 	 * - most probably a call from __try_to_reclaim_swap() while
1580b73d7fceSHugh Dickins 	 * hibernation is allocating its own swap pages for the image,
1581b73d7fceSHugh Dickins 	 * but conceivably even a call from memory reclaim - will free
1582b73d7fceSHugh Dickins 	 * the swap from a page which has already been recorded in the
1583b73d7fceSHugh Dickins 	 * image as a clean swapcache page, and then reuse its swap for
1584b73d7fceSHugh Dickins 	 * another page of the image.  On waking from hibernation, the
1585b73d7fceSHugh Dickins 	 * original page might be freed under memory pressure, then
1586b73d7fceSHugh Dickins 	 * later read back in from swap, now with the wrong data.
1587b73d7fceSHugh Dickins 	 *
15882de1a7e4SSeth Jennings 	 * Hibernation suspends storage while it is writing the image
1589f90ac398SMel Gorman 	 * to disk so check that here.
1590b73d7fceSHugh Dickins 	 */
1591f90ac398SMel Gorman 	if (pm_suspended_storage())
1592b73d7fceSHugh Dickins 		return 0;
1593b73d7fceSHugh Dickins 
1594e0709829SHuang Ying 	page = compound_head(page);
1595a2c43eedSHugh Dickins 	delete_from_swap_cache(page);
15961da177e4SLinus Torvalds 	SetPageDirty(page);
1597a2c43eedSHugh Dickins 	return 1;
159868a22394SRik van Riel }
159968a22394SRik van Riel 
160068a22394SRik van Riel /*
16011da177e4SLinus Torvalds  * Free the swap entry like above, but also try to
16021da177e4SLinus Torvalds  * free the page cache entry if it is the last user.
16031da177e4SLinus Torvalds  */
16042509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry)
16051da177e4SLinus Torvalds {
16061da177e4SLinus Torvalds 	struct swap_info_struct *p;
16071da177e4SLinus Torvalds 	struct page *page = NULL;
16087c00bafeSTim Chen 	unsigned char count;
16091da177e4SLinus Torvalds 
1610a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
16112509ef26SHugh Dickins 		return 1;
16120697212aSChristoph Lameter 
16137c00bafeSTim Chen 	p = _swap_info_get(entry);
16141da177e4SLinus Torvalds 	if (p) {
16157c00bafeSTim Chen 		count = __swap_entry_free(p, entry, 1);
1616e0709829SHuang Ying 		if (count == SWAP_HAS_CACHE &&
1617e0709829SHuang Ying 		    !swap_page_trans_huge_swapped(p, entry)) {
161833806f06SShaohua Li 			page = find_get_page(swap_address_space(entry),
1619f6ab1f7fSHuang Ying 					     swp_offset(entry));
16208413ac9dSNick Piggin 			if (page && !trylock_page(page)) {
162109cbfeafSKirill A. Shutemov 				put_page(page);
162293fac704SNick Piggin 				page = NULL;
162393fac704SNick Piggin 			}
16247c00bafeSTim Chen 		} else if (!count)
162567afa38eSTim Chen 			free_swap_slot(entry);
16261da177e4SLinus Torvalds 	}
16271da177e4SLinus Torvalds 	if (page) {
1628a2c43eedSHugh Dickins 		/*
1629a2c43eedSHugh Dickins 		 * Not mapped elsewhere, or swap space full? Free it!
1630a2c43eedSHugh Dickins 		 * Also recheck PageSwapCache now page is locked (above).
1631a2c43eedSHugh Dickins 		 */
163293fac704SNick Piggin 		if (PageSwapCache(page) && !PageWriteback(page) &&
1633322b8afeSHuang Ying 		    (!page_mapped(page) || mem_cgroup_swap_full(page)) &&
1634e0709829SHuang Ying 		    !swap_page_trans_huge_swapped(p, entry)) {
1635e0709829SHuang Ying 			page = compound_head(page);
16361da177e4SLinus Torvalds 			delete_from_swap_cache(page);
16371da177e4SLinus Torvalds 			SetPageDirty(page);
16381da177e4SLinus Torvalds 		}
16391da177e4SLinus Torvalds 		unlock_page(page);
164009cbfeafSKirill A. Shutemov 		put_page(page);
16411da177e4SLinus Torvalds 	}
16422509ef26SHugh Dickins 	return p != NULL;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
1645b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION
1646f577eb30SRafael J. Wysocki /*
1647915bae9eSRafael J. Wysocki  * Find the swap type that corresponds to given device (if any).
1648f577eb30SRafael J. Wysocki  *
1649915bae9eSRafael J. Wysocki  * @offset - number of the PAGE_SIZE-sized block of the device, starting
1650915bae9eSRafael J. Wysocki  * from 0, in which the swap header is expected to be located.
1651915bae9eSRafael J. Wysocki  *
1652915bae9eSRafael J. Wysocki  * This is needed for the suspend to disk (aka swsusp).
1653f577eb30SRafael J. Wysocki  */
16547bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
1655f577eb30SRafael J. Wysocki {
1656915bae9eSRafael J. Wysocki 	struct block_device *bdev = NULL;
1657efa90a98SHugh Dickins 	int type;
1658f577eb30SRafael J. Wysocki 
1659915bae9eSRafael J. Wysocki 	if (device)
1660915bae9eSRafael J. Wysocki 		bdev = bdget(device);
1661915bae9eSRafael J. Wysocki 
1662f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
1663efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
1664efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
1665f577eb30SRafael J. Wysocki 
1666915bae9eSRafael J. Wysocki 		if (!(sis->flags & SWP_WRITEOK))
1667f577eb30SRafael J. Wysocki 			continue;
1668b6b5bce3SRafael J. Wysocki 
1669915bae9eSRafael J. Wysocki 		if (!bdev) {
16707bf23687SRafael J. Wysocki 			if (bdev_p)
1671dddac6a7SAlan Jenkins 				*bdev_p = bdgrab(sis->bdev);
16727bf23687SRafael J. Wysocki 
16736e1819d6SRafael J. Wysocki 			spin_unlock(&swap_lock);
1674efa90a98SHugh Dickins 			return type;
16756e1819d6SRafael J. Wysocki 		}
1676915bae9eSRafael J. Wysocki 		if (bdev == sis->bdev) {
16779625a5f2SHugh Dickins 			struct swap_extent *se = &sis->first_swap_extent;
1678915bae9eSRafael J. Wysocki 
1679915bae9eSRafael J. Wysocki 			if (se->start_block == offset) {
16807bf23687SRafael J. Wysocki 				if (bdev_p)
1681dddac6a7SAlan Jenkins 					*bdev_p = bdgrab(sis->bdev);
16827bf23687SRafael J. Wysocki 
1683f577eb30SRafael J. Wysocki 				spin_unlock(&swap_lock);
1684915bae9eSRafael J. Wysocki 				bdput(bdev);
1685efa90a98SHugh Dickins 				return type;
1686f577eb30SRafael J. Wysocki 			}
1687f577eb30SRafael J. Wysocki 		}
1688915bae9eSRafael J. Wysocki 	}
1689f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
1690915bae9eSRafael J. Wysocki 	if (bdev)
1691915bae9eSRafael J. Wysocki 		bdput(bdev);
1692915bae9eSRafael J. Wysocki 
1693f577eb30SRafael J. Wysocki 	return -ENODEV;
1694f577eb30SRafael J. Wysocki }
1695f577eb30SRafael J. Wysocki 
1696f577eb30SRafael J. Wysocki /*
169773c34b6aSHugh Dickins  * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
169873c34b6aSHugh Dickins  * corresponding to given index in swap_info (swap type).
169973c34b6aSHugh Dickins  */
170073c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset)
170173c34b6aSHugh Dickins {
170273c34b6aSHugh Dickins 	struct block_device *bdev;
170373c34b6aSHugh Dickins 
170473c34b6aSHugh Dickins 	if ((unsigned int)type >= nr_swapfiles)
170573c34b6aSHugh Dickins 		return 0;
170673c34b6aSHugh Dickins 	if (!(swap_info[type]->flags & SWP_WRITEOK))
170773c34b6aSHugh Dickins 		return 0;
1708d4906e1aSLee Schermerhorn 	return map_swap_entry(swp_entry(type, offset), &bdev);
170973c34b6aSHugh Dickins }
171073c34b6aSHugh Dickins 
171173c34b6aSHugh Dickins /*
1712f577eb30SRafael J. Wysocki  * Return either the total number of swap pages of given type, or the number
1713f577eb30SRafael J. Wysocki  * of free pages of that type (depending on @free)
1714f577eb30SRafael J. Wysocki  *
1715f577eb30SRafael J. Wysocki  * This is needed for software suspend
1716f577eb30SRafael J. Wysocki  */
1717f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free)
1718f577eb30SRafael J. Wysocki {
1719f577eb30SRafael J. Wysocki 	unsigned int n = 0;
1720f577eb30SRafael J. Wysocki 
1721f577eb30SRafael J. Wysocki 	spin_lock(&swap_lock);
1722efa90a98SHugh Dickins 	if ((unsigned int)type < nr_swapfiles) {
1723efa90a98SHugh Dickins 		struct swap_info_struct *sis = swap_info[type];
1724efa90a98SHugh Dickins 
1725ec8acf20SShaohua Li 		spin_lock(&sis->lock);
1726efa90a98SHugh Dickins 		if (sis->flags & SWP_WRITEOK) {
1727efa90a98SHugh Dickins 			n = sis->pages;
1728f577eb30SRafael J. Wysocki 			if (free)
1729efa90a98SHugh Dickins 				n -= sis->inuse_pages;
1730efa90a98SHugh Dickins 		}
1731ec8acf20SShaohua Li 		spin_unlock(&sis->lock);
1732f577eb30SRafael J. Wysocki 	}
1733f577eb30SRafael J. Wysocki 	spin_unlock(&swap_lock);
1734f577eb30SRafael J. Wysocki 	return n;
1735f577eb30SRafael J. Wysocki }
173673c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */
1737f577eb30SRafael J. Wysocki 
17389f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte)
1739179ef71cSCyrill Gorcunov {
17409f8bdb3fSHugh Dickins 	return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte);
1741179ef71cSCyrill Gorcunov }
1742179ef71cSCyrill Gorcunov 
17431da177e4SLinus Torvalds /*
174472866f6fSHugh Dickins  * No need to decide whether this PTE shares the swap entry with others,
174572866f6fSHugh Dickins  * just let do_wp_page work it out if a write is requested later - to
174672866f6fSHugh Dickins  * force COW, vm_page_prot omits write permission from any private vma.
17471da177e4SLinus Torvalds  */
1748044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
17491da177e4SLinus Torvalds 		unsigned long addr, swp_entry_t entry, struct page *page)
17501da177e4SLinus Torvalds {
17519e16b7fbSHugh Dickins 	struct page *swapcache;
175272835c86SJohannes Weiner 	struct mem_cgroup *memcg;
1753044d66c1SHugh Dickins 	spinlock_t *ptl;
1754044d66c1SHugh Dickins 	pte_t *pte;
1755044d66c1SHugh Dickins 	int ret = 1;
1756044d66c1SHugh Dickins 
17579e16b7fbSHugh Dickins 	swapcache = page;
17589e16b7fbSHugh Dickins 	page = ksm_might_need_to_copy(page, vma, addr);
17599e16b7fbSHugh Dickins 	if (unlikely(!page))
17609e16b7fbSHugh Dickins 		return -ENOMEM;
17619e16b7fbSHugh Dickins 
1762f627c2f5SKirill A. Shutemov 	if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL,
1763f627c2f5SKirill A. Shutemov 				&memcg, false)) {
1764044d66c1SHugh Dickins 		ret = -ENOMEM;
176585d9fc89SKAMEZAWA Hiroyuki 		goto out_nolock;
176685d9fc89SKAMEZAWA Hiroyuki 	}
1767044d66c1SHugh Dickins 
1768044d66c1SHugh Dickins 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
17699f8bdb3fSHugh Dickins 	if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) {
1770f627c2f5SKirill A. Shutemov 		mem_cgroup_cancel_charge(page, memcg, false);
1771044d66c1SHugh Dickins 		ret = 0;
1772044d66c1SHugh Dickins 		goto out;
1773044d66c1SHugh Dickins 	}
17748a9f3ccdSBalbir Singh 
1775b084d435SKAMEZAWA Hiroyuki 	dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
1776d559db08SKAMEZAWA Hiroyuki 	inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
17771da177e4SLinus Torvalds 	get_page(page);
17781da177e4SLinus Torvalds 	set_pte_at(vma->vm_mm, addr, pte,
17791da177e4SLinus Torvalds 		   pte_mkold(mk_pte(page, vma->vm_page_prot)));
178000501b53SJohannes Weiner 	if (page == swapcache) {
1781d281ee61SKirill A. Shutemov 		page_add_anon_rmap(page, vma, addr, false);
1782f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, true, false);
178300501b53SJohannes Weiner 	} else { /* ksm created a completely new copy */
1784d281ee61SKirill A. Shutemov 		page_add_new_anon_rmap(page, vma, addr, false);
1785f627c2f5SKirill A. Shutemov 		mem_cgroup_commit_charge(page, memcg, false, false);
178600501b53SJohannes Weiner 		lru_cache_add_active_or_unevictable(page, vma);
178700501b53SJohannes Weiner 	}
17881da177e4SLinus Torvalds 	swap_free(entry);
17891da177e4SLinus Torvalds 	/*
17901da177e4SLinus Torvalds 	 * Move the page to the active list so it is not
17911da177e4SLinus Torvalds 	 * immediately swapped out again after swapon.
17921da177e4SLinus Torvalds 	 */
17931da177e4SLinus Torvalds 	activate_page(page);
1794044d66c1SHugh Dickins out:
1795044d66c1SHugh Dickins 	pte_unmap_unlock(pte, ptl);
179685d9fc89SKAMEZAWA Hiroyuki out_nolock:
17979e16b7fbSHugh Dickins 	if (page != swapcache) {
17989e16b7fbSHugh Dickins 		unlock_page(page);
17999e16b7fbSHugh Dickins 		put_page(page);
18009e16b7fbSHugh Dickins 	}
1801044d66c1SHugh Dickins 	return ret;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
18051da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
18061da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
18071da177e4SLinus Torvalds {
18081da177e4SLinus Torvalds 	pte_t swp_pte = swp_entry_to_pte(entry);
1809705e87c0SHugh Dickins 	pte_t *pte;
18108a9f3ccdSBalbir Singh 	int ret = 0;
18111da177e4SLinus Torvalds 
1812044d66c1SHugh Dickins 	/*
1813044d66c1SHugh Dickins 	 * We don't actually need pte lock while scanning for swp_pte: since
1814044d66c1SHugh Dickins 	 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
1815044d66c1SHugh Dickins 	 * page table while we're scanning; though it could get zapped, and on
1816044d66c1SHugh Dickins 	 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
1817044d66c1SHugh Dickins 	 * of unmatched parts which look like swp_pte, so unuse_pte must
1818044d66c1SHugh Dickins 	 * recheck under pte lock.  Scanning without pte lock lets it be
18192de1a7e4SSeth Jennings 	 * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
1820044d66c1SHugh Dickins 	 */
1821044d66c1SHugh Dickins 	pte = pte_offset_map(pmd, addr);
18221da177e4SLinus Torvalds 	do {
18231da177e4SLinus Torvalds 		/*
18241da177e4SLinus Torvalds 		 * swapoff spends a _lot_ of time in this loop!
18251da177e4SLinus Torvalds 		 * Test inline before going to call unuse_pte.
18261da177e4SLinus Torvalds 		 */
18279f8bdb3fSHugh Dickins 		if (unlikely(pte_same_as_swp(*pte, swp_pte))) {
1828044d66c1SHugh Dickins 			pte_unmap(pte);
1829044d66c1SHugh Dickins 			ret = unuse_pte(vma, pmd, addr, entry, page);
1830044d66c1SHugh Dickins 			if (ret)
1831044d66c1SHugh Dickins 				goto out;
1832044d66c1SHugh Dickins 			pte = pte_offset_map(pmd, addr);
18331da177e4SLinus Torvalds 		}
18341da177e4SLinus Torvalds 	} while (pte++, addr += PAGE_SIZE, addr != end);
1835044d66c1SHugh Dickins 	pte_unmap(pte - 1);
1836044d66c1SHugh Dickins out:
18378a9f3ccdSBalbir Singh 	return ret;
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
18411da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
18421da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
18431da177e4SLinus Torvalds {
18441da177e4SLinus Torvalds 	pmd_t *pmd;
18451da177e4SLinus Torvalds 	unsigned long next;
18468a9f3ccdSBalbir Singh 	int ret;
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	pmd = pmd_offset(pud, addr);
18491da177e4SLinus Torvalds 	do {
1850dc644a07SHugh Dickins 		cond_resched();
18511da177e4SLinus Torvalds 		next = pmd_addr_end(addr, end);
18521a5a9906SAndrea Arcangeli 		if (pmd_none_or_trans_huge_or_clear_bad(pmd))
18531da177e4SLinus Torvalds 			continue;
18548a9f3ccdSBalbir Singh 		ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
18558a9f3ccdSBalbir Singh 		if (ret)
18568a9f3ccdSBalbir Singh 			return ret;
18571da177e4SLinus Torvalds 	} while (pmd++, addr = next, addr != end);
18581da177e4SLinus Torvalds 	return 0;
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds 
1861c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d,
18621da177e4SLinus Torvalds 				unsigned long addr, unsigned long end,
18631da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
18641da177e4SLinus Torvalds {
18651da177e4SLinus Torvalds 	pud_t *pud;
18661da177e4SLinus Torvalds 	unsigned long next;
18678a9f3ccdSBalbir Singh 	int ret;
18681da177e4SLinus Torvalds 
1869c2febafcSKirill A. Shutemov 	pud = pud_offset(p4d, addr);
18701da177e4SLinus Torvalds 	do {
18711da177e4SLinus Torvalds 		next = pud_addr_end(addr, end);
18721da177e4SLinus Torvalds 		if (pud_none_or_clear_bad(pud))
18731da177e4SLinus Torvalds 			continue;
18748a9f3ccdSBalbir Singh 		ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
18758a9f3ccdSBalbir Singh 		if (ret)
18768a9f3ccdSBalbir Singh 			return ret;
18771da177e4SLinus Torvalds 	} while (pud++, addr = next, addr != end);
18781da177e4SLinus Torvalds 	return 0;
18791da177e4SLinus Torvalds }
18801da177e4SLinus Torvalds 
1881c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd,
1882c2febafcSKirill A. Shutemov 				unsigned long addr, unsigned long end,
1883c2febafcSKirill A. Shutemov 				swp_entry_t entry, struct page *page)
1884c2febafcSKirill A. Shutemov {
1885c2febafcSKirill A. Shutemov 	p4d_t *p4d;
1886c2febafcSKirill A. Shutemov 	unsigned long next;
1887c2febafcSKirill A. Shutemov 	int ret;
1888c2febafcSKirill A. Shutemov 
1889c2febafcSKirill A. Shutemov 	p4d = p4d_offset(pgd, addr);
1890c2febafcSKirill A. Shutemov 	do {
1891c2febafcSKirill A. Shutemov 		next = p4d_addr_end(addr, end);
1892c2febafcSKirill A. Shutemov 		if (p4d_none_or_clear_bad(p4d))
1893c2febafcSKirill A. Shutemov 			continue;
1894c2febafcSKirill A. Shutemov 		ret = unuse_pud_range(vma, p4d, addr, next, entry, page);
1895c2febafcSKirill A. Shutemov 		if (ret)
1896c2febafcSKirill A. Shutemov 			return ret;
1897c2febafcSKirill A. Shutemov 	} while (p4d++, addr = next, addr != end);
1898c2febafcSKirill A. Shutemov 	return 0;
1899c2febafcSKirill A. Shutemov }
1900c2febafcSKirill A. Shutemov 
19011da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma,
19021da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
19031da177e4SLinus Torvalds {
19041da177e4SLinus Torvalds 	pgd_t *pgd;
19051da177e4SLinus Torvalds 	unsigned long addr, end, next;
19068a9f3ccdSBalbir Singh 	int ret;
19071da177e4SLinus Torvalds 
19083ca7b3c5SHugh Dickins 	if (page_anon_vma(page)) {
19091da177e4SLinus Torvalds 		addr = page_address_in_vma(page, vma);
19101da177e4SLinus Torvalds 		if (addr == -EFAULT)
19111da177e4SLinus Torvalds 			return 0;
19121da177e4SLinus Torvalds 		else
19131da177e4SLinus Torvalds 			end = addr + PAGE_SIZE;
19141da177e4SLinus Torvalds 	} else {
19151da177e4SLinus Torvalds 		addr = vma->vm_start;
19161da177e4SLinus Torvalds 		end = vma->vm_end;
19171da177e4SLinus Torvalds 	}
19181da177e4SLinus Torvalds 
19191da177e4SLinus Torvalds 	pgd = pgd_offset(vma->vm_mm, addr);
19201da177e4SLinus Torvalds 	do {
19211da177e4SLinus Torvalds 		next = pgd_addr_end(addr, end);
19221da177e4SLinus Torvalds 		if (pgd_none_or_clear_bad(pgd))
19231da177e4SLinus Torvalds 			continue;
1924c2febafcSKirill A. Shutemov 		ret = unuse_p4d_range(vma, pgd, addr, next, entry, page);
19258a9f3ccdSBalbir Singh 		if (ret)
19268a9f3ccdSBalbir Singh 			return ret;
19271da177e4SLinus Torvalds 	} while (pgd++, addr = next, addr != end);
19281da177e4SLinus Torvalds 	return 0;
19291da177e4SLinus Torvalds }
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm,
19321da177e4SLinus Torvalds 				swp_entry_t entry, struct page *page)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	struct vm_area_struct *vma;
19358a9f3ccdSBalbir Singh 	int ret = 0;
19361da177e4SLinus Torvalds 
19371da177e4SLinus Torvalds 	if (!down_read_trylock(&mm->mmap_sem)) {
19381da177e4SLinus Torvalds 		/*
19397d03431cSFernando Luis Vazquez Cao 		 * Activate page so shrink_inactive_list is unlikely to unmap
19407d03431cSFernando Luis Vazquez Cao 		 * its ptes while lock is dropped, so swapoff can make progress.
19411da177e4SLinus Torvalds 		 */
1942c475a8abSHugh Dickins 		activate_page(page);
19431da177e4SLinus Torvalds 		unlock_page(page);
19441da177e4SLinus Torvalds 		down_read(&mm->mmap_sem);
19451da177e4SLinus Torvalds 		lock_page(page);
19461da177e4SLinus Torvalds 	}
19471da177e4SLinus Torvalds 	for (vma = mm->mmap; vma; vma = vma->vm_next) {
19488a9f3ccdSBalbir Singh 		if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
19491da177e4SLinus Torvalds 			break;
1950dc644a07SHugh Dickins 		cond_resched();
19511da177e4SLinus Torvalds 	}
19521da177e4SLinus Torvalds 	up_read(&mm->mmap_sem);
19538a9f3ccdSBalbir Singh 	return (ret < 0)? ret: 0;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds 
19561da177e4SLinus Torvalds /*
195738b5faf4SDan Magenheimer  * Scan swap_map (or frontswap_map if frontswap parameter is true)
195838b5faf4SDan Magenheimer  * from current position to next entry still in use.
19591da177e4SLinus Torvalds  * Recycle to start on reaching the end, returning 0 when empty.
19601da177e4SLinus Torvalds  */
19616eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si,
196238b5faf4SDan Magenheimer 					unsigned int prev, bool frontswap)
19631da177e4SLinus Torvalds {
19646eb396dcSHugh Dickins 	unsigned int max = si->max;
19656eb396dcSHugh Dickins 	unsigned int i = prev;
19668d69aaeeSHugh Dickins 	unsigned char count;
19671da177e4SLinus Torvalds 
19681da177e4SLinus Torvalds 	/*
19695d337b91SHugh Dickins 	 * No need for swap_lock here: we're just looking
19701da177e4SLinus Torvalds 	 * for whether an entry is in use, not modifying it; false
19711da177e4SLinus Torvalds 	 * hits are okay, and sys_swapoff() has already prevented new
19725d337b91SHugh Dickins 	 * allocations from this area (while holding swap_lock).
19731da177e4SLinus Torvalds 	 */
19741da177e4SLinus Torvalds 	for (;;) {
19751da177e4SLinus Torvalds 		if (++i >= max) {
19761da177e4SLinus Torvalds 			if (!prev) {
19771da177e4SLinus Torvalds 				i = 0;
19781da177e4SLinus Torvalds 				break;
19791da177e4SLinus Torvalds 			}
19801da177e4SLinus Torvalds 			/*
19811da177e4SLinus Torvalds 			 * No entries in use at top of swap_map,
19821da177e4SLinus Torvalds 			 * loop back to start and recheck there.
19831da177e4SLinus Torvalds 			 */
19841da177e4SLinus Torvalds 			max = prev + 1;
19851da177e4SLinus Torvalds 			prev = 0;
19861da177e4SLinus Torvalds 			i = 1;
19871da177e4SLinus Torvalds 		}
19884db0c3c2SJason Low 		count = READ_ONCE(si->swap_map[i]);
1989355cfa73SKAMEZAWA Hiroyuki 		if (count && swap_count(count) != SWAP_MAP_BAD)
1990dc644a07SHugh Dickins 			if (!frontswap || frontswap_test(si, i))
19911da177e4SLinus Torvalds 				break;
1992dc644a07SHugh Dickins 		if ((i % LATENCY_LIMIT) == 0)
1993dc644a07SHugh Dickins 			cond_resched();
19941da177e4SLinus Torvalds 	}
19951da177e4SLinus Torvalds 	return i;
19961da177e4SLinus Torvalds }
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds /*
19991da177e4SLinus Torvalds  * We completely avoid races by reading each swap page in advance,
20001da177e4SLinus Torvalds  * and then search for the process using it.  All the necessary
20011da177e4SLinus Torvalds  * page table adjustments can then be made atomically.
200238b5faf4SDan Magenheimer  *
200338b5faf4SDan Magenheimer  * if the boolean frontswap is true, only unuse pages_to_unuse pages;
200438b5faf4SDan Magenheimer  * pages_to_unuse==0 means all pages; ignored if frontswap is false
20051da177e4SLinus Torvalds  */
200638b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap,
200738b5faf4SDan Magenheimer 		 unsigned long pages_to_unuse)
20081da177e4SLinus Torvalds {
2009efa90a98SHugh Dickins 	struct swap_info_struct *si = swap_info[type];
20101da177e4SLinus Torvalds 	struct mm_struct *start_mm;
2011edfe23daSShaohua Li 	volatile unsigned char *swap_map; /* swap_map is accessed without
2012edfe23daSShaohua Li 					   * locking. Mark it as volatile
2013edfe23daSShaohua Li 					   * to prevent compiler doing
2014edfe23daSShaohua Li 					   * something odd.
2015edfe23daSShaohua Li 					   */
20168d69aaeeSHugh Dickins 	unsigned char swcount;
20171da177e4SLinus Torvalds 	struct page *page;
20181da177e4SLinus Torvalds 	swp_entry_t entry;
20196eb396dcSHugh Dickins 	unsigned int i = 0;
20201da177e4SLinus Torvalds 	int retval = 0;
20211da177e4SLinus Torvalds 
20221da177e4SLinus Torvalds 	/*
20231da177e4SLinus Torvalds 	 * When searching mms for an entry, a good strategy is to
20241da177e4SLinus Torvalds 	 * start at the first mm we freed the previous entry from
20251da177e4SLinus Torvalds 	 * (though actually we don't notice whether we or coincidence
20261da177e4SLinus Torvalds 	 * freed the entry).  Initialize this start_mm with a hold.
20271da177e4SLinus Torvalds 	 *
20281da177e4SLinus Torvalds 	 * A simpler strategy would be to start at the last mm we
20291da177e4SLinus Torvalds 	 * freed the previous entry from; but that would take less
20301da177e4SLinus Torvalds 	 * advantage of mmlist ordering, which clusters forked mms
20311da177e4SLinus Torvalds 	 * together, child after parent.  If we race with dup_mmap(), we
20321da177e4SLinus Torvalds 	 * prefer to resolve parent before child, lest we miss entries
20331da177e4SLinus Torvalds 	 * duplicated after we scanned child: using last mm would invert
2034570a335bSHugh Dickins 	 * that.
20351da177e4SLinus Torvalds 	 */
20361da177e4SLinus Torvalds 	start_mm = &init_mm;
20373fce371bSVegard Nossum 	mmget(&init_mm);
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds 	/*
20401da177e4SLinus Torvalds 	 * Keep on scanning until all entries have gone.  Usually,
20411da177e4SLinus Torvalds 	 * one pass through swap_map is enough, but not necessarily:
20421da177e4SLinus Torvalds 	 * there are races when an instance of an entry might be missed.
20431da177e4SLinus Torvalds 	 */
204438b5faf4SDan Magenheimer 	while ((i = find_next_to_unuse(si, i, frontswap)) != 0) {
20451da177e4SLinus Torvalds 		if (signal_pending(current)) {
20461da177e4SLinus Torvalds 			retval = -EINTR;
20471da177e4SLinus Torvalds 			break;
20481da177e4SLinus Torvalds 		}
20491da177e4SLinus Torvalds 
20501da177e4SLinus Torvalds 		/*
20511da177e4SLinus Torvalds 		 * Get a page for the entry, using the existing swap
20521da177e4SLinus Torvalds 		 * cache page if there is one.  Otherwise, get a clean
20531da177e4SLinus Torvalds 		 * page and read the swap into it.
20541da177e4SLinus Torvalds 		 */
20551da177e4SLinus Torvalds 		swap_map = &si->swap_map[i];
20561da177e4SLinus Torvalds 		entry = swp_entry(type, i);
205702098feaSHugh Dickins 		page = read_swap_cache_async(entry,
205823955622SShaohua Li 					GFP_HIGHUSER_MOVABLE, NULL, 0, false);
20591da177e4SLinus Torvalds 		if (!page) {
20601da177e4SLinus Torvalds 			/*
20611da177e4SLinus Torvalds 			 * Either swap_duplicate() failed because entry
20621da177e4SLinus Torvalds 			 * has been freed independently, and will not be
20631da177e4SLinus Torvalds 			 * reused since sys_swapoff() already disabled
20641da177e4SLinus Torvalds 			 * allocation from here, or alloc_page() failed.
20651da177e4SLinus Torvalds 			 */
2066edfe23daSShaohua Li 			swcount = *swap_map;
2067edfe23daSShaohua Li 			/*
2068edfe23daSShaohua Li 			 * We don't hold lock here, so the swap entry could be
2069edfe23daSShaohua Li 			 * SWAP_MAP_BAD (when the cluster is discarding).
2070edfe23daSShaohua Li 			 * Instead of fail out, We can just skip the swap
2071edfe23daSShaohua Li 			 * entry because swapoff will wait for discarding
2072edfe23daSShaohua Li 			 * finish anyway.
2073edfe23daSShaohua Li 			 */
2074edfe23daSShaohua Li 			if (!swcount || swcount == SWAP_MAP_BAD)
20751da177e4SLinus Torvalds 				continue;
20761da177e4SLinus Torvalds 			retval = -ENOMEM;
20771da177e4SLinus Torvalds 			break;
20781da177e4SLinus Torvalds 		}
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 		/*
20811da177e4SLinus Torvalds 		 * Don't hold on to start_mm if it looks like exiting.
20821da177e4SLinus Torvalds 		 */
20831da177e4SLinus Torvalds 		if (atomic_read(&start_mm->mm_users) == 1) {
20841da177e4SLinus Torvalds 			mmput(start_mm);
20851da177e4SLinus Torvalds 			start_mm = &init_mm;
20863fce371bSVegard Nossum 			mmget(&init_mm);
20871da177e4SLinus Torvalds 		}
20881da177e4SLinus Torvalds 
20891da177e4SLinus Torvalds 		/*
20901da177e4SLinus Torvalds 		 * Wait for and lock page.  When do_swap_page races with
20911da177e4SLinus Torvalds 		 * try_to_unuse, do_swap_page can handle the fault much
20921da177e4SLinus Torvalds 		 * faster than try_to_unuse can locate the entry.  This
20931da177e4SLinus Torvalds 		 * apparently redundant "wait_on_page_locked" lets try_to_unuse
20941da177e4SLinus Torvalds 		 * defer to do_swap_page in such a case - in some tests,
20951da177e4SLinus Torvalds 		 * do_swap_page and try_to_unuse repeatedly compete.
20961da177e4SLinus Torvalds 		 */
20971da177e4SLinus Torvalds 		wait_on_page_locked(page);
20981da177e4SLinus Torvalds 		wait_on_page_writeback(page);
20991da177e4SLinus Torvalds 		lock_page(page);
21001da177e4SLinus Torvalds 		wait_on_page_writeback(page);
21011da177e4SLinus Torvalds 
21021da177e4SLinus Torvalds 		/*
21031da177e4SLinus Torvalds 		 * Remove all references to entry.
21041da177e4SLinus Torvalds 		 */
21051da177e4SLinus Torvalds 		swcount = *swap_map;
2106aaa46865SHugh Dickins 		if (swap_count(swcount) == SWAP_MAP_SHMEM) {
2107aaa46865SHugh Dickins 			retval = shmem_unuse(entry, page);
2108aaa46865SHugh Dickins 			/* page has already been unlocked and released */
2109aaa46865SHugh Dickins 			if (retval < 0)
2110aaa46865SHugh Dickins 				break;
2111aaa46865SHugh Dickins 			continue;
21121da177e4SLinus Torvalds 		}
2113aaa46865SHugh Dickins 		if (swap_count(swcount) && start_mm != &init_mm)
2114aaa46865SHugh Dickins 			retval = unuse_mm(start_mm, entry, page);
2115aaa46865SHugh Dickins 
2116355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map)) {
21171da177e4SLinus Torvalds 			int set_start_mm = (*swap_map >= swcount);
21181da177e4SLinus Torvalds 			struct list_head *p = &start_mm->mmlist;
21191da177e4SLinus Torvalds 			struct mm_struct *new_start_mm = start_mm;
21201da177e4SLinus Torvalds 			struct mm_struct *prev_mm = start_mm;
21211da177e4SLinus Torvalds 			struct mm_struct *mm;
21221da177e4SLinus Torvalds 
21233fce371bSVegard Nossum 			mmget(new_start_mm);
21243fce371bSVegard Nossum 			mmget(prev_mm);
21251da177e4SLinus Torvalds 			spin_lock(&mmlist_lock);
2126aaa46865SHugh Dickins 			while (swap_count(*swap_map) && !retval &&
21271da177e4SLinus Torvalds 					(p = p->next) != &start_mm->mmlist) {
21281da177e4SLinus Torvalds 				mm = list_entry(p, struct mm_struct, mmlist);
2129388f7934SVegard Nossum 				if (!mmget_not_zero(mm))
21301da177e4SLinus Torvalds 					continue;
21311da177e4SLinus Torvalds 				spin_unlock(&mmlist_lock);
21321da177e4SLinus Torvalds 				mmput(prev_mm);
21331da177e4SLinus Torvalds 				prev_mm = mm;
21341da177e4SLinus Torvalds 
21351da177e4SLinus Torvalds 				cond_resched();
21361da177e4SLinus Torvalds 
21371da177e4SLinus Torvalds 				swcount = *swap_map;
2138355cfa73SKAMEZAWA Hiroyuki 				if (!swap_count(swcount)) /* any usage ? */
21391da177e4SLinus Torvalds 					;
2140aaa46865SHugh Dickins 				else if (mm == &init_mm)
21411da177e4SLinus Torvalds 					set_start_mm = 1;
2142aaa46865SHugh Dickins 				else
21431da177e4SLinus Torvalds 					retval = unuse_mm(mm, entry, page);
2144355cfa73SKAMEZAWA Hiroyuki 
214532c5fc10SBo Liu 				if (set_start_mm && *swap_map < swcount) {
21461da177e4SLinus Torvalds 					mmput(new_start_mm);
21473fce371bSVegard Nossum 					mmget(mm);
21481da177e4SLinus Torvalds 					new_start_mm = mm;
21491da177e4SLinus Torvalds 					set_start_mm = 0;
21501da177e4SLinus Torvalds 				}
21511da177e4SLinus Torvalds 				spin_lock(&mmlist_lock);
21521da177e4SLinus Torvalds 			}
21531da177e4SLinus Torvalds 			spin_unlock(&mmlist_lock);
21541da177e4SLinus Torvalds 			mmput(prev_mm);
21551da177e4SLinus Torvalds 			mmput(start_mm);
21561da177e4SLinus Torvalds 			start_mm = new_start_mm;
21571da177e4SLinus Torvalds 		}
21581da177e4SLinus Torvalds 		if (retval) {
21591da177e4SLinus Torvalds 			unlock_page(page);
216009cbfeafSKirill A. Shutemov 			put_page(page);
21611da177e4SLinus Torvalds 			break;
21621da177e4SLinus Torvalds 		}
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 		/*
21651da177e4SLinus Torvalds 		 * If a reference remains (rare), we would like to leave
21661da177e4SLinus Torvalds 		 * the page in the swap cache; but try_to_unmap could
21671da177e4SLinus Torvalds 		 * then re-duplicate the entry once we drop page lock,
21681da177e4SLinus Torvalds 		 * so we might loop indefinitely; also, that page could
21691da177e4SLinus Torvalds 		 * not be swapped out to other storage meanwhile.  So:
21701da177e4SLinus Torvalds 		 * delete from cache even if there's another reference,
21711da177e4SLinus Torvalds 		 * after ensuring that the data has been saved to disk -
21721da177e4SLinus Torvalds 		 * since if the reference remains (rarer), it will be
21731da177e4SLinus Torvalds 		 * read from disk into another page.  Splitting into two
21741da177e4SLinus Torvalds 		 * pages would be incorrect if swap supported "shared
21751da177e4SLinus Torvalds 		 * private" pages, but they are handled by tmpfs files.
21765ad64688SHugh Dickins 		 *
21775ad64688SHugh Dickins 		 * Given how unuse_vma() targets one particular offset
21785ad64688SHugh Dickins 		 * in an anon_vma, once the anon_vma has been determined,
21795ad64688SHugh Dickins 		 * this splitting happens to be just what is needed to
21805ad64688SHugh Dickins 		 * handle where KSM pages have been swapped out: re-reading
21815ad64688SHugh Dickins 		 * is unnecessarily slow, but we can fix that later on.
21821da177e4SLinus Torvalds 		 */
2183355cfa73SKAMEZAWA Hiroyuki 		if (swap_count(*swap_map) &&
2184355cfa73SKAMEZAWA Hiroyuki 		     PageDirty(page) && PageSwapCache(page)) {
21851da177e4SLinus Torvalds 			struct writeback_control wbc = {
21861da177e4SLinus Torvalds 				.sync_mode = WB_SYNC_NONE,
21871da177e4SLinus Torvalds 			};
21881da177e4SLinus Torvalds 
2189e0709829SHuang Ying 			swap_writepage(compound_head(page), &wbc);
21901da177e4SLinus Torvalds 			lock_page(page);
21911da177e4SLinus Torvalds 			wait_on_page_writeback(page);
21921da177e4SLinus Torvalds 		}
219368bdc8d6SHugh Dickins 
219468bdc8d6SHugh Dickins 		/*
219568bdc8d6SHugh Dickins 		 * It is conceivable that a racing task removed this page from
219668bdc8d6SHugh Dickins 		 * swap cache just before we acquired the page lock at the top,
219768bdc8d6SHugh Dickins 		 * or while we dropped it in unuse_mm().  The page might even
219868bdc8d6SHugh Dickins 		 * be back in swap cache on another swap area: that we must not
219968bdc8d6SHugh Dickins 		 * delete, since it may not have been written out to swap yet.
220068bdc8d6SHugh Dickins 		 */
220168bdc8d6SHugh Dickins 		if (PageSwapCache(page) &&
2202e0709829SHuang Ying 		    likely(page_private(page) == entry.val) &&
2203e0709829SHuang Ying 		    !page_swapped(page))
2204e0709829SHuang Ying 			delete_from_swap_cache(compound_head(page));
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 		/*
22071da177e4SLinus Torvalds 		 * So we could skip searching mms once swap count went
22081da177e4SLinus Torvalds 		 * to 1, we did not mark any present ptes as dirty: must
22092706a1b8SAnderson Briglia 		 * mark page dirty so shrink_page_list will preserve it.
22101da177e4SLinus Torvalds 		 */
22111da177e4SLinus Torvalds 		SetPageDirty(page);
22121da177e4SLinus Torvalds 		unlock_page(page);
221309cbfeafSKirill A. Shutemov 		put_page(page);
22141da177e4SLinus Torvalds 
22151da177e4SLinus Torvalds 		/*
22161da177e4SLinus Torvalds 		 * Make sure that we aren't completely killing
22171da177e4SLinus Torvalds 		 * interactive performance.
22181da177e4SLinus Torvalds 		 */
22191da177e4SLinus Torvalds 		cond_resched();
222038b5faf4SDan Magenheimer 		if (frontswap && pages_to_unuse > 0) {
222138b5faf4SDan Magenheimer 			if (!--pages_to_unuse)
222238b5faf4SDan Magenheimer 				break;
222338b5faf4SDan Magenheimer 		}
22241da177e4SLinus Torvalds 	}
22251da177e4SLinus Torvalds 
22261da177e4SLinus Torvalds 	mmput(start_mm);
22271da177e4SLinus Torvalds 	return retval;
22281da177e4SLinus Torvalds }
22291da177e4SLinus Torvalds 
22301da177e4SLinus Torvalds /*
22315d337b91SHugh Dickins  * After a successful try_to_unuse, if no swap is now in use, we know
22325d337b91SHugh Dickins  * we can empty the mmlist.  swap_lock must be held on entry and exit.
22335d337b91SHugh Dickins  * Note that mmlist_lock nests inside swap_lock, and an mm must be
22341da177e4SLinus Torvalds  * added to the mmlist just after page_duplicate - before would be racy.
22351da177e4SLinus Torvalds  */
22361da177e4SLinus Torvalds static void drain_mmlist(void)
22371da177e4SLinus Torvalds {
22381da177e4SLinus Torvalds 	struct list_head *p, *next;
2239efa90a98SHugh Dickins 	unsigned int type;
22401da177e4SLinus Torvalds 
2241efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++)
2242efa90a98SHugh Dickins 		if (swap_info[type]->inuse_pages)
22431da177e4SLinus Torvalds 			return;
22441da177e4SLinus Torvalds 	spin_lock(&mmlist_lock);
22451da177e4SLinus Torvalds 	list_for_each_safe(p, next, &init_mm.mmlist)
22461da177e4SLinus Torvalds 		list_del_init(p);
22471da177e4SLinus Torvalds 	spin_unlock(&mmlist_lock);
22481da177e4SLinus Torvalds }
22491da177e4SLinus Torvalds 
22501da177e4SLinus Torvalds /*
22511da177e4SLinus Torvalds  * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
2252d4906e1aSLee Schermerhorn  * corresponds to page offset for the specified swap entry.
2253d4906e1aSLee Schermerhorn  * Note that the type of this function is sector_t, but it returns page offset
2254d4906e1aSLee Schermerhorn  * into the bdev, not sector offset.
22551da177e4SLinus Torvalds  */
2256d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
22571da177e4SLinus Torvalds {
2258f29ad6a9SHugh Dickins 	struct swap_info_struct *sis;
2259f29ad6a9SHugh Dickins 	struct swap_extent *start_se;
2260f29ad6a9SHugh Dickins 	struct swap_extent *se;
2261f29ad6a9SHugh Dickins 	pgoff_t offset;
2262f29ad6a9SHugh Dickins 
2263efa90a98SHugh Dickins 	sis = swap_info[swp_type(entry)];
2264f29ad6a9SHugh Dickins 	*bdev = sis->bdev;
2265f29ad6a9SHugh Dickins 
2266f29ad6a9SHugh Dickins 	offset = swp_offset(entry);
2267f29ad6a9SHugh Dickins 	start_se = sis->curr_swap_extent;
2268f29ad6a9SHugh Dickins 	se = start_se;
22691da177e4SLinus Torvalds 
22701da177e4SLinus Torvalds 	for ( ; ; ) {
22711da177e4SLinus Torvalds 		if (se->start_page <= offset &&
22721da177e4SLinus Torvalds 				offset < (se->start_page + se->nr_pages)) {
22731da177e4SLinus Torvalds 			return se->start_block + (offset - se->start_page);
22741da177e4SLinus Torvalds 		}
2275a8ae4991SGeliang Tang 		se = list_next_entry(se, list);
22761da177e4SLinus Torvalds 		sis->curr_swap_extent = se;
22771da177e4SLinus Torvalds 		BUG_ON(se == start_se);		/* It *must* be present */
22781da177e4SLinus Torvalds 	}
22791da177e4SLinus Torvalds }
22801da177e4SLinus Torvalds 
22811da177e4SLinus Torvalds /*
2282d4906e1aSLee Schermerhorn  * Returns the page offset into bdev for the specified page's swap entry.
2283d4906e1aSLee Schermerhorn  */
2284d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev)
2285d4906e1aSLee Schermerhorn {
2286d4906e1aSLee Schermerhorn 	swp_entry_t entry;
2287d4906e1aSLee Schermerhorn 	entry.val = page_private(page);
2288d4906e1aSLee Schermerhorn 	return map_swap_entry(entry, bdev);
2289d4906e1aSLee Schermerhorn }
2290d4906e1aSLee Schermerhorn 
2291d4906e1aSLee Schermerhorn /*
22921da177e4SLinus Torvalds  * Free all of a swapdev's extent information
22931da177e4SLinus Torvalds  */
22941da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis)
22951da177e4SLinus Torvalds {
22969625a5f2SHugh Dickins 	while (!list_empty(&sis->first_swap_extent.list)) {
22971da177e4SLinus Torvalds 		struct swap_extent *se;
22981da177e4SLinus Torvalds 
2299a8ae4991SGeliang Tang 		se = list_first_entry(&sis->first_swap_extent.list,
23001da177e4SLinus Torvalds 				struct swap_extent, list);
23011da177e4SLinus Torvalds 		list_del(&se->list);
23021da177e4SLinus Torvalds 		kfree(se);
23031da177e4SLinus Torvalds 	}
230462c230bcSMel Gorman 
230562c230bcSMel Gorman 	if (sis->flags & SWP_FILE) {
230662c230bcSMel Gorman 		struct file *swap_file = sis->swap_file;
230762c230bcSMel Gorman 		struct address_space *mapping = swap_file->f_mapping;
230862c230bcSMel Gorman 
230962c230bcSMel Gorman 		sis->flags &= ~SWP_FILE;
231062c230bcSMel Gorman 		mapping->a_ops->swap_deactivate(swap_file);
231162c230bcSMel Gorman 	}
23121da177e4SLinus Torvalds }
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds /*
23151da177e4SLinus Torvalds  * Add a block range (and the corresponding page range) into this swapdev's
231611d31886SHugh Dickins  * extent list.  The extent list is kept sorted in page order.
23171da177e4SLinus Torvalds  *
231811d31886SHugh Dickins  * This function rather assumes that it is called in ascending page order.
23191da177e4SLinus Torvalds  */
2320a509bc1aSMel Gorman int
23211da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
23221da177e4SLinus Torvalds 		unsigned long nr_pages, sector_t start_block)
23231da177e4SLinus Torvalds {
23241da177e4SLinus Torvalds 	struct swap_extent *se;
23251da177e4SLinus Torvalds 	struct swap_extent *new_se;
23261da177e4SLinus Torvalds 	struct list_head *lh;
23271da177e4SLinus Torvalds 
23289625a5f2SHugh Dickins 	if (start_page == 0) {
23299625a5f2SHugh Dickins 		se = &sis->first_swap_extent;
23309625a5f2SHugh Dickins 		sis->curr_swap_extent = se;
23319625a5f2SHugh Dickins 		se->start_page = 0;
23329625a5f2SHugh Dickins 		se->nr_pages = nr_pages;
23339625a5f2SHugh Dickins 		se->start_block = start_block;
23349625a5f2SHugh Dickins 		return 1;
23359625a5f2SHugh Dickins 	} else {
23369625a5f2SHugh Dickins 		lh = sis->first_swap_extent.list.prev;	/* Highest extent */
23371da177e4SLinus Torvalds 		se = list_entry(lh, struct swap_extent, list);
233811d31886SHugh Dickins 		BUG_ON(se->start_page + se->nr_pages != start_page);
233911d31886SHugh Dickins 		if (se->start_block + se->nr_pages == start_block) {
23401da177e4SLinus Torvalds 			/* Merge it */
23411da177e4SLinus Torvalds 			se->nr_pages += nr_pages;
23421da177e4SLinus Torvalds 			return 0;
23431da177e4SLinus Torvalds 		}
23441da177e4SLinus Torvalds 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	/*
23471da177e4SLinus Torvalds 	 * No merge.  Insert a new extent, preserving ordering.
23481da177e4SLinus Torvalds 	 */
23491da177e4SLinus Torvalds 	new_se = kmalloc(sizeof(*se), GFP_KERNEL);
23501da177e4SLinus Torvalds 	if (new_se == NULL)
23511da177e4SLinus Torvalds 		return -ENOMEM;
23521da177e4SLinus Torvalds 	new_se->start_page = start_page;
23531da177e4SLinus Torvalds 	new_se->nr_pages = nr_pages;
23541da177e4SLinus Torvalds 	new_se->start_block = start_block;
23551da177e4SLinus Torvalds 
23569625a5f2SHugh Dickins 	list_add_tail(&new_se->list, &sis->first_swap_extent.list);
235753092a74SHugh Dickins 	return 1;
23581da177e4SLinus Torvalds }
23591da177e4SLinus Torvalds 
23601da177e4SLinus Torvalds /*
23611da177e4SLinus Torvalds  * A `swap extent' is a simple thing which maps a contiguous range of pages
23621da177e4SLinus Torvalds  * onto a contiguous range of disk blocks.  An ordered list of swap extents
23631da177e4SLinus Torvalds  * is built at swapon time and is then used at swap_writepage/swap_readpage
23641da177e4SLinus Torvalds  * time for locating where on disk a page belongs.
23651da177e4SLinus Torvalds  *
23661da177e4SLinus Torvalds  * If the swapfile is an S_ISBLK block device, a single extent is installed.
23671da177e4SLinus Torvalds  * This is done so that the main operating code can treat S_ISBLK and S_ISREG
23681da177e4SLinus Torvalds  * swap files identically.
23691da177e4SLinus Torvalds  *
23701da177e4SLinus Torvalds  * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
23711da177e4SLinus Torvalds  * extent list operates in PAGE_SIZE disk blocks.  Both S_ISREG and S_ISBLK
23721da177e4SLinus Torvalds  * swapfiles are handled *identically* after swapon time.
23731da177e4SLinus Torvalds  *
23741da177e4SLinus Torvalds  * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
23751da177e4SLinus Torvalds  * and will parse them into an ordered extent list, in PAGE_SIZE chunks.  If
23761da177e4SLinus Torvalds  * some stray blocks are found which do not fall within the PAGE_SIZE alignment
23771da177e4SLinus Torvalds  * requirements, they are simply tossed out - we will never use those blocks
23781da177e4SLinus Torvalds  * for swapping.
23791da177e4SLinus Torvalds  *
2380b0d9bcd4SHugh Dickins  * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon.  This
23811da177e4SLinus Torvalds  * prevents root from shooting her foot off by ftruncating an in-use swapfile,
23821da177e4SLinus Torvalds  * which will scribble on the fs.
23831da177e4SLinus Torvalds  *
23841da177e4SLinus Torvalds  * The amount of disk space which a single swap extent represents varies.
23851da177e4SLinus Torvalds  * Typically it is in the 1-4 megabyte range.  So we can have hundreds of
23861da177e4SLinus Torvalds  * extents in the list.  To avoid much list walking, we cache the previous
23871da177e4SLinus Torvalds  * search location in `curr_swap_extent', and start new searches from there.
23881da177e4SLinus Torvalds  * This is extremely effective.  The average number of iterations in
23891da177e4SLinus Torvalds  * map_swap_page() has been measured at about 0.3 per page.  - akpm.
23901da177e4SLinus Torvalds  */
239153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
23921da177e4SLinus Torvalds {
239362c230bcSMel Gorman 	struct file *swap_file = sis->swap_file;
239462c230bcSMel Gorman 	struct address_space *mapping = swap_file->f_mapping;
239562c230bcSMel Gorman 	struct inode *inode = mapping->host;
23961da177e4SLinus Torvalds 	int ret;
23971da177e4SLinus Torvalds 
23981da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
23991da177e4SLinus Torvalds 		ret = add_swap_extent(sis, 0, sis->max, 0);
240053092a74SHugh Dickins 		*span = sis->pages;
2401a509bc1aSMel Gorman 		return ret;
24021da177e4SLinus Torvalds 	}
24031da177e4SLinus Torvalds 
240462c230bcSMel Gorman 	if (mapping->a_ops->swap_activate) {
2405a509bc1aSMel Gorman 		ret = mapping->a_ops->swap_activate(sis, swap_file, span);
240662c230bcSMel Gorman 		if (!ret) {
240762c230bcSMel Gorman 			sis->flags |= SWP_FILE;
240862c230bcSMel Gorman 			ret = add_swap_extent(sis, 0, sis->max, 0);
240962c230bcSMel Gorman 			*span = sis->pages;
241062c230bcSMel Gorman 		}
24119625a5f2SHugh Dickins 		return ret;
2412a509bc1aSMel Gorman 	}
2413a509bc1aSMel Gorman 
2414a509bc1aSMel Gorman 	return generic_swapfile_activate(sis, swap_file, span);
24151da177e4SLinus Torvalds }
24161da177e4SLinus Torvalds 
2417a2468cc9SAaron Lu static int swap_node(struct swap_info_struct *p)
2418a2468cc9SAaron Lu {
2419a2468cc9SAaron Lu 	struct block_device *bdev;
2420a2468cc9SAaron Lu 
2421a2468cc9SAaron Lu 	if (p->bdev)
2422a2468cc9SAaron Lu 		bdev = p->bdev;
2423a2468cc9SAaron Lu 	else
2424a2468cc9SAaron Lu 		bdev = p->swap_file->f_inode->i_sb->s_bdev;
2425a2468cc9SAaron Lu 
2426a2468cc9SAaron Lu 	return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE;
2427a2468cc9SAaron Lu }
2428a2468cc9SAaron Lu 
2429cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio,
24302a8f9449SShaohua Li 				unsigned char *swap_map,
24312a8f9449SShaohua Li 				struct swap_cluster_info *cluster_info)
243240531542SCesar Eduardo Barros {
2433a2468cc9SAaron Lu 	int i;
2434a2468cc9SAaron Lu 
243540531542SCesar Eduardo Barros 	if (prio >= 0)
243640531542SCesar Eduardo Barros 		p->prio = prio;
243740531542SCesar Eduardo Barros 	else
243840531542SCesar Eduardo Barros 		p->prio = --least_priority;
243918ab4d4cSDan Streetman 	/*
244018ab4d4cSDan Streetman 	 * the plist prio is negated because plist ordering is
244118ab4d4cSDan Streetman 	 * low-to-high, while swap ordering is high-to-low
244218ab4d4cSDan Streetman 	 */
244318ab4d4cSDan Streetman 	p->list.prio = -p->prio;
2444a2468cc9SAaron Lu 	for_each_node(i) {
2445a2468cc9SAaron Lu 		if (p->prio >= 0)
2446a2468cc9SAaron Lu 			p->avail_lists[i].prio = -p->prio;
2447a2468cc9SAaron Lu 		else {
2448a2468cc9SAaron Lu 			if (swap_node(p) == i)
2449a2468cc9SAaron Lu 				p->avail_lists[i].prio = 1;
2450a2468cc9SAaron Lu 			else
2451a2468cc9SAaron Lu 				p->avail_lists[i].prio = -p->prio;
2452a2468cc9SAaron Lu 		}
2453a2468cc9SAaron Lu 	}
245440531542SCesar Eduardo Barros 	p->swap_map = swap_map;
24552a8f9449SShaohua Li 	p->cluster_info = cluster_info;
245640531542SCesar Eduardo Barros 	p->flags |= SWP_WRITEOK;
2457ec8acf20SShaohua Li 	atomic_long_add(p->pages, &nr_swap_pages);
245840531542SCesar Eduardo Barros 	total_swap_pages += p->pages;
245940531542SCesar Eduardo Barros 
2460adfab836SDan Streetman 	assert_spin_locked(&swap_lock);
2461adfab836SDan Streetman 	/*
246218ab4d4cSDan Streetman 	 * both lists are plists, and thus priority ordered.
246318ab4d4cSDan Streetman 	 * swap_active_head needs to be priority ordered for swapoff(),
246418ab4d4cSDan Streetman 	 * which on removal of any swap_info_struct with an auto-assigned
246518ab4d4cSDan Streetman 	 * (i.e. negative) priority increments the auto-assigned priority
246618ab4d4cSDan Streetman 	 * of any lower-priority swap_info_structs.
246718ab4d4cSDan Streetman 	 * swap_avail_head needs to be priority ordered for get_swap_page(),
246818ab4d4cSDan Streetman 	 * which allocates swap pages from the highest available priority
246918ab4d4cSDan Streetman 	 * swap_info_struct.
2470adfab836SDan Streetman 	 */
247118ab4d4cSDan Streetman 	plist_add(&p->list, &swap_active_head);
2472a2468cc9SAaron Lu 	add_to_avail_list(p);
2473cf0cac0aSCesar Eduardo Barros }
2474cf0cac0aSCesar Eduardo Barros 
2475cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio,
2476cf0cac0aSCesar Eduardo Barros 				unsigned char *swap_map,
24772a8f9449SShaohua Li 				struct swap_cluster_info *cluster_info,
2478cf0cac0aSCesar Eduardo Barros 				unsigned long *frontswap_map)
2479cf0cac0aSCesar Eduardo Barros {
24804f89849dSMinchan Kim 	frontswap_init(p->type, frontswap_map);
2481cf0cac0aSCesar Eduardo Barros 	spin_lock(&swap_lock);
2482ec8acf20SShaohua Li 	spin_lock(&p->lock);
24832a8f9449SShaohua Li 	 _enable_swap_info(p, prio, swap_map, cluster_info);
2484ec8acf20SShaohua Li 	spin_unlock(&p->lock);
2485cf0cac0aSCesar Eduardo Barros 	spin_unlock(&swap_lock);
2486cf0cac0aSCesar Eduardo Barros }
2487cf0cac0aSCesar Eduardo Barros 
2488cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p)
2489cf0cac0aSCesar Eduardo Barros {
2490cf0cac0aSCesar Eduardo Barros 	spin_lock(&swap_lock);
2491ec8acf20SShaohua Li 	spin_lock(&p->lock);
24922a8f9449SShaohua Li 	_enable_swap_info(p, p->prio, p->swap_map, p->cluster_info);
2493ec8acf20SShaohua Li 	spin_unlock(&p->lock);
249440531542SCesar Eduardo Barros 	spin_unlock(&swap_lock);
249540531542SCesar Eduardo Barros }
249640531542SCesar Eduardo Barros 
249767afa38eSTim Chen bool has_usable_swap(void)
249867afa38eSTim Chen {
249967afa38eSTim Chen 	bool ret = true;
250067afa38eSTim Chen 
250167afa38eSTim Chen 	spin_lock(&swap_lock);
250267afa38eSTim Chen 	if (plist_head_empty(&swap_active_head))
250367afa38eSTim Chen 		ret = false;
250467afa38eSTim Chen 	spin_unlock(&swap_lock);
250567afa38eSTim Chen 	return ret;
250667afa38eSTim Chen }
250767afa38eSTim Chen 
2508c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
25091da177e4SLinus Torvalds {
25101da177e4SLinus Torvalds 	struct swap_info_struct *p = NULL;
25118d69aaeeSHugh Dickins 	unsigned char *swap_map;
25122a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info;
25134f89849dSMinchan Kim 	unsigned long *frontswap_map;
25141da177e4SLinus Torvalds 	struct file *swap_file, *victim;
25151da177e4SLinus Torvalds 	struct address_space *mapping;
25161da177e4SLinus Torvalds 	struct inode *inode;
251791a27b2aSJeff Layton 	struct filename *pathname;
2518adfab836SDan Streetman 	int err, found = 0;
25195b808a23SKrzysztof Kozlowski 	unsigned int old_block_size;
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds 	if (!capable(CAP_SYS_ADMIN))
25221da177e4SLinus Torvalds 		return -EPERM;
25231da177e4SLinus Torvalds 
2524191c5424SAl Viro 	BUG_ON(!current->mm);
2525191c5424SAl Viro 
25261da177e4SLinus Torvalds 	pathname = getname(specialfile);
25271da177e4SLinus Torvalds 	if (IS_ERR(pathname))
2528f58b59c1SXiaotian Feng 		return PTR_ERR(pathname);
25291da177e4SLinus Torvalds 
2530669abf4eSJeff Layton 	victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0);
25311da177e4SLinus Torvalds 	err = PTR_ERR(victim);
25321da177e4SLinus Torvalds 	if (IS_ERR(victim))
25331da177e4SLinus Torvalds 		goto out;
25341da177e4SLinus Torvalds 
25351da177e4SLinus Torvalds 	mapping = victim->f_mapping;
25365d337b91SHugh Dickins 	spin_lock(&swap_lock);
253718ab4d4cSDan Streetman 	plist_for_each_entry(p, &swap_active_head, list) {
253822c6f8fdSHugh Dickins 		if (p->flags & SWP_WRITEOK) {
2539adfab836SDan Streetman 			if (p->swap_file->f_mapping == mapping) {
2540adfab836SDan Streetman 				found = 1;
25411da177e4SLinus Torvalds 				break;
25421da177e4SLinus Torvalds 			}
25431da177e4SLinus Torvalds 		}
2544adfab836SDan Streetman 	}
2545adfab836SDan Streetman 	if (!found) {
25461da177e4SLinus Torvalds 		err = -EINVAL;
25475d337b91SHugh Dickins 		spin_unlock(&swap_lock);
25481da177e4SLinus Torvalds 		goto out_dput;
25491da177e4SLinus Torvalds 	}
2550191c5424SAl Viro 	if (!security_vm_enough_memory_mm(current->mm, p->pages))
25511da177e4SLinus Torvalds 		vm_unacct_memory(p->pages);
25521da177e4SLinus Torvalds 	else {
25531da177e4SLinus Torvalds 		err = -ENOMEM;
25545d337b91SHugh Dickins 		spin_unlock(&swap_lock);
25551da177e4SLinus Torvalds 		goto out_dput;
25561da177e4SLinus Torvalds 	}
2557a2468cc9SAaron Lu 	del_from_avail_list(p);
2558ec8acf20SShaohua Li 	spin_lock(&p->lock);
255978ecba08SHugh Dickins 	if (p->prio < 0) {
2560adfab836SDan Streetman 		struct swap_info_struct *si = p;
2561a2468cc9SAaron Lu 		int nid;
2562adfab836SDan Streetman 
256318ab4d4cSDan Streetman 		plist_for_each_entry_continue(si, &swap_active_head, list) {
2564adfab836SDan Streetman 			si->prio++;
256518ab4d4cSDan Streetman 			si->list.prio--;
2566a2468cc9SAaron Lu 			for_each_node(nid) {
2567a2468cc9SAaron Lu 				if (si->avail_lists[nid].prio != 1)
2568a2468cc9SAaron Lu 					si->avail_lists[nid].prio--;
2569a2468cc9SAaron Lu 			}
2570adfab836SDan Streetman 		}
257178ecba08SHugh Dickins 		least_priority++;
257278ecba08SHugh Dickins 	}
257318ab4d4cSDan Streetman 	plist_del(&p->list, &swap_active_head);
2574ec8acf20SShaohua Li 	atomic_long_sub(p->pages, &nr_swap_pages);
25751da177e4SLinus Torvalds 	total_swap_pages -= p->pages;
25761da177e4SLinus Torvalds 	p->flags &= ~SWP_WRITEOK;
2577ec8acf20SShaohua Li 	spin_unlock(&p->lock);
25785d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2579fb4f88dcSHugh Dickins 
2580039939a6STim Chen 	disable_swap_slots_cache_lock();
2581039939a6STim Chen 
2582e1e12d2fSDavid Rientjes 	set_current_oom_origin();
2583adfab836SDan Streetman 	err = try_to_unuse(p->type, false, 0); /* force unuse all pages */
2584e1e12d2fSDavid Rientjes 	clear_current_oom_origin();
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 	if (err) {
25871da177e4SLinus Torvalds 		/* re-insert swap space back into swap_list */
2588cf0cac0aSCesar Eduardo Barros 		reinsert_swap_info(p);
2589039939a6STim Chen 		reenable_swap_slots_cache_unlock();
25901da177e4SLinus Torvalds 		goto out_dput;
25911da177e4SLinus Torvalds 	}
259252b7efdbSHugh Dickins 
2593039939a6STim Chen 	reenable_swap_slots_cache_unlock();
2594039939a6STim Chen 
2595815c2c54SShaohua Li 	flush_work(&p->discard_work);
2596815c2c54SShaohua Li 
25974cd3bb10SHugh Dickins 	destroy_swap_extents(p);
2598570a335bSHugh Dickins 	if (p->flags & SWP_CONTINUED)
2599570a335bSHugh Dickins 		free_swap_count_continuations(p);
2600570a335bSHugh Dickins 
260181a0298bSHuang Ying 	if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev)))
260281a0298bSHuang Ying 		atomic_dec(&nr_rotate_swap);
260381a0298bSHuang Ying 
2604fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
26055d337b91SHugh Dickins 	spin_lock(&swap_lock);
2606ec8acf20SShaohua Li 	spin_lock(&p->lock);
26071da177e4SLinus Torvalds 	drain_mmlist();
26085d337b91SHugh Dickins 
26095d337b91SHugh Dickins 	/* wait for anyone still in scan_swap_map */
26105d337b91SHugh Dickins 	p->highest_bit = 0;		/* cuts scans short */
26115d337b91SHugh Dickins 	while (p->flags >= SWP_SCANNING) {
2612ec8acf20SShaohua Li 		spin_unlock(&p->lock);
26135d337b91SHugh Dickins 		spin_unlock(&swap_lock);
261413e4b57fSNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
26155d337b91SHugh Dickins 		spin_lock(&swap_lock);
2616ec8acf20SShaohua Li 		spin_lock(&p->lock);
26175d337b91SHugh Dickins 	}
26185d337b91SHugh Dickins 
26191da177e4SLinus Torvalds 	swap_file = p->swap_file;
26205b808a23SKrzysztof Kozlowski 	old_block_size = p->old_block_size;
26211da177e4SLinus Torvalds 	p->swap_file = NULL;
26221da177e4SLinus Torvalds 	p->max = 0;
26231da177e4SLinus Torvalds 	swap_map = p->swap_map;
26241da177e4SLinus Torvalds 	p->swap_map = NULL;
26252a8f9449SShaohua Li 	cluster_info = p->cluster_info;
26262a8f9449SShaohua Li 	p->cluster_info = NULL;
26274f89849dSMinchan Kim 	frontswap_map = frontswap_map_get(p);
2628ec8acf20SShaohua Li 	spin_unlock(&p->lock);
26295d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2630adfab836SDan Streetman 	frontswap_invalidate_area(p->type);
263158e97ba6SKrzysztof Kozlowski 	frontswap_map_set(p, NULL);
2632fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
2633ebc2a1a6SShaohua Li 	free_percpu(p->percpu_cluster);
2634ebc2a1a6SShaohua Li 	p->percpu_cluster = NULL;
26351da177e4SLinus Torvalds 	vfree(swap_map);
263654f180d3SHuang Ying 	kvfree(cluster_info);
263754f180d3SHuang Ying 	kvfree(frontswap_map);
26382de1a7e4SSeth Jennings 	/* Destroy swap account information */
2639adfab836SDan Streetman 	swap_cgroup_swapoff(p->type);
26404b3ef9daSHuang, Ying 	exit_swap_address_space(p->type);
264127a7faa0SKAMEZAWA Hiroyuki 
26421da177e4SLinus Torvalds 	inode = mapping->host;
26431da177e4SLinus Torvalds 	if (S_ISBLK(inode->i_mode)) {
26441da177e4SLinus Torvalds 		struct block_device *bdev = I_BDEV(inode);
26455b808a23SKrzysztof Kozlowski 		set_blocksize(bdev, old_block_size);
2646e525fd89STejun Heo 		blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
26471da177e4SLinus Torvalds 	} else {
26485955102cSAl Viro 		inode_lock(inode);
26491da177e4SLinus Torvalds 		inode->i_flags &= ~S_SWAPFILE;
26505955102cSAl Viro 		inode_unlock(inode);
26511da177e4SLinus Torvalds 	}
26521da177e4SLinus Torvalds 	filp_close(swap_file, NULL);
2653f893ab41SWeijie Yang 
2654f893ab41SWeijie Yang 	/*
2655f893ab41SWeijie Yang 	 * Clear the SWP_USED flag after all resources are freed so that swapon
2656f893ab41SWeijie Yang 	 * can reuse this swap_info in alloc_swap_info() safely.  It is ok to
2657f893ab41SWeijie Yang 	 * not hold p->lock after we cleared its SWP_WRITEOK.
2658f893ab41SWeijie Yang 	 */
2659f893ab41SWeijie Yang 	spin_lock(&swap_lock);
2660f893ab41SWeijie Yang 	p->flags = 0;
2661f893ab41SWeijie Yang 	spin_unlock(&swap_lock);
2662f893ab41SWeijie Yang 
26631da177e4SLinus Torvalds 	err = 0;
266466d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
266566d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
26661da177e4SLinus Torvalds 
26671da177e4SLinus Torvalds out_dput:
26681da177e4SLinus Torvalds 	filp_close(victim, NULL);
26691da177e4SLinus Torvalds out:
2670f58b59c1SXiaotian Feng 	putname(pathname);
26711da177e4SLinus Torvalds 	return err;
26721da177e4SLinus Torvalds }
26731da177e4SLinus Torvalds 
26741da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS
26759dd95748SAl Viro static __poll_t swaps_poll(struct file *file, poll_table *wait)
267666d7dd51SKay Sievers {
2677f1514638SKay Sievers 	struct seq_file *seq = file->private_data;
267866d7dd51SKay Sievers 
267966d7dd51SKay Sievers 	poll_wait(file, &proc_poll_wait, wait);
268066d7dd51SKay Sievers 
2681f1514638SKay Sievers 	if (seq->poll_event != atomic_read(&proc_poll_event)) {
2682f1514638SKay Sievers 		seq->poll_event = atomic_read(&proc_poll_event);
2683a9a08845SLinus Torvalds 		return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI;
268466d7dd51SKay Sievers 	}
268566d7dd51SKay Sievers 
2686a9a08845SLinus Torvalds 	return EPOLLIN | EPOLLRDNORM;
268766d7dd51SKay Sievers }
268866d7dd51SKay Sievers 
26891da177e4SLinus Torvalds /* iterator */
26901da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos)
26911da177e4SLinus Torvalds {
2692efa90a98SHugh Dickins 	struct swap_info_struct *si;
2693efa90a98SHugh Dickins 	int type;
26941da177e4SLinus Torvalds 	loff_t l = *pos;
26951da177e4SLinus Torvalds 
2696fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
26971da177e4SLinus Torvalds 
2698881e4aabSSuleiman Souhlal 	if (!l)
2699881e4aabSSuleiman Souhlal 		return SEQ_START_TOKEN;
2700881e4aabSSuleiman Souhlal 
2701efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2702efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
2703efa90a98SHugh Dickins 		si = swap_info[type];
2704efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
27051da177e4SLinus Torvalds 			continue;
2706881e4aabSSuleiman Souhlal 		if (!--l)
2707efa90a98SHugh Dickins 			return si;
27081da177e4SLinus Torvalds 	}
27091da177e4SLinus Torvalds 
27101da177e4SLinus Torvalds 	return NULL;
27111da177e4SLinus Torvalds }
27121da177e4SLinus Torvalds 
27131da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
27141da177e4SLinus Torvalds {
2715efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
2716efa90a98SHugh Dickins 	int type;
27171da177e4SLinus Torvalds 
2718881e4aabSSuleiman Souhlal 	if (v == SEQ_START_TOKEN)
2719efa90a98SHugh Dickins 		type = 0;
2720efa90a98SHugh Dickins 	else
2721efa90a98SHugh Dickins 		type = si->type + 1;
2722881e4aabSSuleiman Souhlal 
2723efa90a98SHugh Dickins 	for (; type < nr_swapfiles; type++) {
2724efa90a98SHugh Dickins 		smp_rmb();	/* read nr_swapfiles before swap_info[type] */
2725efa90a98SHugh Dickins 		si = swap_info[type];
2726efa90a98SHugh Dickins 		if (!(si->flags & SWP_USED) || !si->swap_map)
27271da177e4SLinus Torvalds 			continue;
27281da177e4SLinus Torvalds 		++*pos;
2729efa90a98SHugh Dickins 		return si;
27301da177e4SLinus Torvalds 	}
27311da177e4SLinus Torvalds 
27321da177e4SLinus Torvalds 	return NULL;
27331da177e4SLinus Torvalds }
27341da177e4SLinus Torvalds 
27351da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v)
27361da177e4SLinus Torvalds {
2737fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
27381da177e4SLinus Torvalds }
27391da177e4SLinus Torvalds 
27401da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v)
27411da177e4SLinus Torvalds {
2742efa90a98SHugh Dickins 	struct swap_info_struct *si = v;
27431da177e4SLinus Torvalds 	struct file *file;
27441da177e4SLinus Torvalds 	int len;
27451da177e4SLinus Torvalds 
2746efa90a98SHugh Dickins 	if (si == SEQ_START_TOKEN) {
27471da177e4SLinus Torvalds 		seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
2748881e4aabSSuleiman Souhlal 		return 0;
2749881e4aabSSuleiman Souhlal 	}
27501da177e4SLinus Torvalds 
2751efa90a98SHugh Dickins 	file = si->swap_file;
27522726d566SMiklos Szeredi 	len = seq_file_path(swap, file, " \t\n\\");
27536eb396dcSHugh Dickins 	seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
27541da177e4SLinus Torvalds 			len < 40 ? 40 - len : 1, " ",
2755496ad9aaSAl Viro 			S_ISBLK(file_inode(file)->i_mode) ?
27561da177e4SLinus Torvalds 				"partition" : "file\t",
2757efa90a98SHugh Dickins 			si->pages << (PAGE_SHIFT - 10),
2758efa90a98SHugh Dickins 			si->inuse_pages << (PAGE_SHIFT - 10),
2759efa90a98SHugh Dickins 			si->prio);
27601da177e4SLinus Torvalds 	return 0;
27611da177e4SLinus Torvalds }
27621da177e4SLinus Torvalds 
276315ad7cdcSHelge Deller static const struct seq_operations swaps_op = {
27641da177e4SLinus Torvalds 	.start =	swap_start,
27651da177e4SLinus Torvalds 	.next =		swap_next,
27661da177e4SLinus Torvalds 	.stop =		swap_stop,
27671da177e4SLinus Torvalds 	.show =		swap_show
27681da177e4SLinus Torvalds };
27691da177e4SLinus Torvalds 
27701da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file)
27711da177e4SLinus Torvalds {
2772f1514638SKay Sievers 	struct seq_file *seq;
277366d7dd51SKay Sievers 	int ret;
277466d7dd51SKay Sievers 
277566d7dd51SKay Sievers 	ret = seq_open(file, &swaps_op);
2776f1514638SKay Sievers 	if (ret)
277766d7dd51SKay Sievers 		return ret;
277866d7dd51SKay Sievers 
2779f1514638SKay Sievers 	seq = file->private_data;
2780f1514638SKay Sievers 	seq->poll_event = atomic_read(&proc_poll_event);
2781f1514638SKay Sievers 	return 0;
27821da177e4SLinus Torvalds }
27831da177e4SLinus Torvalds 
278415ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = {
27851da177e4SLinus Torvalds 	.open		= swaps_open,
27861da177e4SLinus Torvalds 	.read		= seq_read,
27871da177e4SLinus Torvalds 	.llseek		= seq_lseek,
27881da177e4SLinus Torvalds 	.release	= seq_release,
278966d7dd51SKay Sievers 	.poll		= swaps_poll,
27901da177e4SLinus Torvalds };
27911da177e4SLinus Torvalds 
27921da177e4SLinus Torvalds static int __init procswaps_init(void)
27931da177e4SLinus Torvalds {
27943d71f86fSDenis V. Lunev 	proc_create("swaps", 0, NULL, &proc_swaps_operations);
27951da177e4SLinus Torvalds 	return 0;
27961da177e4SLinus Torvalds }
27971da177e4SLinus Torvalds __initcall(procswaps_init);
27981da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
27991da177e4SLinus Torvalds 
28001796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK
28011796316aSJan Beulich static int __init max_swapfiles_check(void)
28021796316aSJan Beulich {
28031796316aSJan Beulich 	MAX_SWAPFILES_CHECK();
28041796316aSJan Beulich 	return 0;
28051796316aSJan Beulich }
28061796316aSJan Beulich late_initcall(max_swapfiles_check);
28071796316aSJan Beulich #endif
28081796316aSJan Beulich 
280953cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void)
28101da177e4SLinus Torvalds {
28111da177e4SLinus Torvalds 	struct swap_info_struct *p;
28121da177e4SLinus Torvalds 	unsigned int type;
2813a2468cc9SAaron Lu 	int i;
2814efa90a98SHugh Dickins 
2815efa90a98SHugh Dickins 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2816efa90a98SHugh Dickins 	if (!p)
281753cbb243SCesar Eduardo Barros 		return ERR_PTR(-ENOMEM);
2818efa90a98SHugh Dickins 
28195d337b91SHugh Dickins 	spin_lock(&swap_lock);
2820efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
2821efa90a98SHugh Dickins 		if (!(swap_info[type]->flags & SWP_USED))
28221da177e4SLinus Torvalds 			break;
2823efa90a98SHugh Dickins 	}
28240697212aSChristoph Lameter 	if (type >= MAX_SWAPFILES) {
28255d337b91SHugh Dickins 		spin_unlock(&swap_lock);
2826efa90a98SHugh Dickins 		kfree(p);
2827730c0581SCesar Eduardo Barros 		return ERR_PTR(-EPERM);
28281da177e4SLinus Torvalds 	}
2829efa90a98SHugh Dickins 	if (type >= nr_swapfiles) {
2830efa90a98SHugh Dickins 		p->type = type;
2831efa90a98SHugh Dickins 		swap_info[type] = p;
2832efa90a98SHugh Dickins 		/*
2833efa90a98SHugh Dickins 		 * Write swap_info[type] before nr_swapfiles, in case a
2834efa90a98SHugh Dickins 		 * racing procfs swap_start() or swap_next() is reading them.
2835efa90a98SHugh Dickins 		 * (We never shrink nr_swapfiles, we never free this entry.)
2836efa90a98SHugh Dickins 		 */
2837efa90a98SHugh Dickins 		smp_wmb();
2838efa90a98SHugh Dickins 		nr_swapfiles++;
2839efa90a98SHugh Dickins 	} else {
2840efa90a98SHugh Dickins 		kfree(p);
2841efa90a98SHugh Dickins 		p = swap_info[type];
2842efa90a98SHugh Dickins 		/*
2843efa90a98SHugh Dickins 		 * Do not memset this entry: a racing procfs swap_next()
2844efa90a98SHugh Dickins 		 * would be relying on p->type to remain valid.
2845efa90a98SHugh Dickins 		 */
2846efa90a98SHugh Dickins 	}
28479625a5f2SHugh Dickins 	INIT_LIST_HEAD(&p->first_swap_extent.list);
284818ab4d4cSDan Streetman 	plist_node_init(&p->list, 0);
2849a2468cc9SAaron Lu 	for_each_node(i)
2850a2468cc9SAaron Lu 		plist_node_init(&p->avail_lists[i], 0);
28511da177e4SLinus Torvalds 	p->flags = SWP_USED;
28525d337b91SHugh Dickins 	spin_unlock(&swap_lock);
2853ec8acf20SShaohua Li 	spin_lock_init(&p->lock);
28542628bd6fSHuang Ying 	spin_lock_init(&p->cont_lock);
2855efa90a98SHugh Dickins 
285653cbb243SCesar Eduardo Barros 	return p;
285753cbb243SCesar Eduardo Barros }
285853cbb243SCesar Eduardo Barros 
28594d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
28604d0e1e10SCesar Eduardo Barros {
28614d0e1e10SCesar Eduardo Barros 	int error;
28624d0e1e10SCesar Eduardo Barros 
28634d0e1e10SCesar Eduardo Barros 	if (S_ISBLK(inode->i_mode)) {
28644d0e1e10SCesar Eduardo Barros 		p->bdev = bdgrab(I_BDEV(inode));
28654d0e1e10SCesar Eduardo Barros 		error = blkdev_get(p->bdev,
28666f179af8SHugh Dickins 				   FMODE_READ | FMODE_WRITE | FMODE_EXCL, p);
28674d0e1e10SCesar Eduardo Barros 		if (error < 0) {
28684d0e1e10SCesar Eduardo Barros 			p->bdev = NULL;
28696f179af8SHugh Dickins 			return error;
28704d0e1e10SCesar Eduardo Barros 		}
28714d0e1e10SCesar Eduardo Barros 		p->old_block_size = block_size(p->bdev);
28724d0e1e10SCesar Eduardo Barros 		error = set_blocksize(p->bdev, PAGE_SIZE);
28734d0e1e10SCesar Eduardo Barros 		if (error < 0)
287487ade72aSCesar Eduardo Barros 			return error;
28754d0e1e10SCesar Eduardo Barros 		p->flags |= SWP_BLKDEV;
28764d0e1e10SCesar Eduardo Barros 	} else if (S_ISREG(inode->i_mode)) {
28774d0e1e10SCesar Eduardo Barros 		p->bdev = inode->i_sb->s_bdev;
28785955102cSAl Viro 		inode_lock(inode);
287987ade72aSCesar Eduardo Barros 		if (IS_SWAPFILE(inode))
288087ade72aSCesar Eduardo Barros 			return -EBUSY;
288187ade72aSCesar Eduardo Barros 	} else
288287ade72aSCesar Eduardo Barros 		return -EINVAL;
28834d0e1e10SCesar Eduardo Barros 
28844d0e1e10SCesar Eduardo Barros 	return 0;
28854d0e1e10SCesar Eduardo Barros }
28864d0e1e10SCesar Eduardo Barros 
2887377eeaa8SAndi Kleen 
2888377eeaa8SAndi Kleen /*
2889377eeaa8SAndi Kleen  * Find out how many pages are allowed for a single swap device. There
2890377eeaa8SAndi Kleen  * are two limiting factors:
2891377eeaa8SAndi Kleen  * 1) the number of bits for the swap offset in the swp_entry_t type, and
2892377eeaa8SAndi Kleen  * 2) the number of bits in the swap pte, as defined by the different
2893377eeaa8SAndi Kleen  * architectures.
2894377eeaa8SAndi Kleen  *
2895377eeaa8SAndi Kleen  * In order to find the largest possible bit mask, a swap entry with
2896377eeaa8SAndi Kleen  * swap type 0 and swap offset ~0UL is created, encoded to a swap pte,
2897377eeaa8SAndi Kleen  * decoded to a swp_entry_t again, and finally the swap offset is
2898377eeaa8SAndi Kleen  * extracted.
2899377eeaa8SAndi Kleen  *
2900377eeaa8SAndi Kleen  * This will mask all the bits from the initial ~0UL mask that can't
2901377eeaa8SAndi Kleen  * be encoded in either the swp_entry_t or the architecture definition
2902377eeaa8SAndi Kleen  * of a swap pte.
2903377eeaa8SAndi Kleen  */
2904377eeaa8SAndi Kleen unsigned long generic_max_swapfile_size(void)
2905377eeaa8SAndi Kleen {
2906377eeaa8SAndi Kleen 	return swp_offset(pte_to_swp_entry(
2907377eeaa8SAndi Kleen 			swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1;
2908377eeaa8SAndi Kleen }
2909377eeaa8SAndi Kleen 
2910377eeaa8SAndi Kleen /* Can be overridden by an architecture for additional checks. */
2911377eeaa8SAndi Kleen __weak unsigned long max_swapfile_size(void)
2912377eeaa8SAndi Kleen {
2913377eeaa8SAndi Kleen 	return generic_max_swapfile_size();
2914377eeaa8SAndi Kleen }
2915377eeaa8SAndi Kleen 
2916ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p,
2917ca8bd38bSCesar Eduardo Barros 					union swap_header *swap_header,
2918ca8bd38bSCesar Eduardo Barros 					struct inode *inode)
2919ca8bd38bSCesar Eduardo Barros {
2920ca8bd38bSCesar Eduardo Barros 	int i;
2921ca8bd38bSCesar Eduardo Barros 	unsigned long maxpages;
2922ca8bd38bSCesar Eduardo Barros 	unsigned long swapfilepages;
2923d6bbbd29SRaymond Jennings 	unsigned long last_page;
2924ca8bd38bSCesar Eduardo Barros 
2925ca8bd38bSCesar Eduardo Barros 	if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
2926465c47fdSAndrew Morton 		pr_err("Unable to find swap-space signature\n");
292738719025SCesar Eduardo Barros 		return 0;
2928ca8bd38bSCesar Eduardo Barros 	}
2929ca8bd38bSCesar Eduardo Barros 
2930ca8bd38bSCesar Eduardo Barros 	/* swap partition endianess hack... */
2931ca8bd38bSCesar Eduardo Barros 	if (swab32(swap_header->info.version) == 1) {
2932ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.version);
2933ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.last_page);
2934ca8bd38bSCesar Eduardo Barros 		swab32s(&swap_header->info.nr_badpages);
2935dd111be6SJann Horn 		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
2936dd111be6SJann Horn 			return 0;
2937ca8bd38bSCesar Eduardo Barros 		for (i = 0; i < swap_header->info.nr_badpages; i++)
2938ca8bd38bSCesar Eduardo Barros 			swab32s(&swap_header->info.badpages[i]);
2939ca8bd38bSCesar Eduardo Barros 	}
2940ca8bd38bSCesar Eduardo Barros 	/* Check the swap header's sub-version */
2941ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.version != 1) {
2942465c47fdSAndrew Morton 		pr_warn("Unable to handle swap header version %d\n",
2943ca8bd38bSCesar Eduardo Barros 			swap_header->info.version);
294438719025SCesar Eduardo Barros 		return 0;
2945ca8bd38bSCesar Eduardo Barros 	}
2946ca8bd38bSCesar Eduardo Barros 
2947ca8bd38bSCesar Eduardo Barros 	p->lowest_bit  = 1;
2948ca8bd38bSCesar Eduardo Barros 	p->cluster_next = 1;
2949ca8bd38bSCesar Eduardo Barros 	p->cluster_nr = 0;
2950ca8bd38bSCesar Eduardo Barros 
2951377eeaa8SAndi Kleen 	maxpages = max_swapfile_size();
2952d6bbbd29SRaymond Jennings 	last_page = swap_header->info.last_page;
2953a06ad633STom Abraham 	if (!last_page) {
2954a06ad633STom Abraham 		pr_warn("Empty swap-file\n");
2955a06ad633STom Abraham 		return 0;
2956a06ad633STom Abraham 	}
2957d6bbbd29SRaymond Jennings 	if (last_page > maxpages) {
2958465c47fdSAndrew Morton 		pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
2959d6bbbd29SRaymond Jennings 			maxpages << (PAGE_SHIFT - 10),
2960d6bbbd29SRaymond Jennings 			last_page << (PAGE_SHIFT - 10));
2961d6bbbd29SRaymond Jennings 	}
2962d6bbbd29SRaymond Jennings 	if (maxpages > last_page) {
2963d6bbbd29SRaymond Jennings 		maxpages = last_page + 1;
2964ca8bd38bSCesar Eduardo Barros 		/* p->max is an unsigned int: don't overflow it */
2965ca8bd38bSCesar Eduardo Barros 		if ((unsigned int)maxpages == 0)
2966ca8bd38bSCesar Eduardo Barros 			maxpages = UINT_MAX;
2967ca8bd38bSCesar Eduardo Barros 	}
2968ca8bd38bSCesar Eduardo Barros 	p->highest_bit = maxpages - 1;
2969ca8bd38bSCesar Eduardo Barros 
2970ca8bd38bSCesar Eduardo Barros 	if (!maxpages)
297138719025SCesar Eduardo Barros 		return 0;
2972ca8bd38bSCesar Eduardo Barros 	swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
2973ca8bd38bSCesar Eduardo Barros 	if (swapfilepages && maxpages > swapfilepages) {
2974465c47fdSAndrew Morton 		pr_warn("Swap area shorter than signature indicates\n");
297538719025SCesar Eduardo Barros 		return 0;
2976ca8bd38bSCesar Eduardo Barros 	}
2977ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
297838719025SCesar Eduardo Barros 		return 0;
2979ca8bd38bSCesar Eduardo Barros 	if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
298038719025SCesar Eduardo Barros 		return 0;
2981ca8bd38bSCesar Eduardo Barros 
2982ca8bd38bSCesar Eduardo Barros 	return maxpages;
2983ca8bd38bSCesar Eduardo Barros }
2984ca8bd38bSCesar Eduardo Barros 
29854b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS						\
2986235b6217SHuang, Ying 	DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info))
29874b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS						\
29884b3ef9daSHuang, Ying 	DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER)
29894b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS						\
29904b3ef9daSHuang, Ying 	max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS)
2991235b6217SHuang, Ying 
2992915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p,
2993915d4d7bSCesar Eduardo Barros 					union swap_header *swap_header,
2994915d4d7bSCesar Eduardo Barros 					unsigned char *swap_map,
29952a8f9449SShaohua Li 					struct swap_cluster_info *cluster_info,
2996915d4d7bSCesar Eduardo Barros 					unsigned long maxpages,
2997915d4d7bSCesar Eduardo Barros 					sector_t *span)
2998915d4d7bSCesar Eduardo Barros {
2999235b6217SHuang, Ying 	unsigned int j, k;
3000915d4d7bSCesar Eduardo Barros 	unsigned int nr_good_pages;
3001915d4d7bSCesar Eduardo Barros 	int nr_extents;
30022a8f9449SShaohua Li 	unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
3003235b6217SHuang, Ying 	unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS;
3004235b6217SHuang, Ying 	unsigned long i, idx;
3005915d4d7bSCesar Eduardo Barros 
3006915d4d7bSCesar Eduardo Barros 	nr_good_pages = maxpages - 1;	/* omit header page */
3007915d4d7bSCesar Eduardo Barros 
30086b534915SHuang Ying 	cluster_list_init(&p->free_clusters);
30096b534915SHuang Ying 	cluster_list_init(&p->discard_clusters);
30102a8f9449SShaohua Li 
3011915d4d7bSCesar Eduardo Barros 	for (i = 0; i < swap_header->info.nr_badpages; i++) {
3012915d4d7bSCesar Eduardo Barros 		unsigned int page_nr = swap_header->info.badpages[i];
3013bdb8e3f6SCesar Eduardo Barros 		if (page_nr == 0 || page_nr > swap_header->info.last_page)
3014bdb8e3f6SCesar Eduardo Barros 			return -EINVAL;
3015915d4d7bSCesar Eduardo Barros 		if (page_nr < maxpages) {
3016915d4d7bSCesar Eduardo Barros 			swap_map[page_nr] = SWAP_MAP_BAD;
3017915d4d7bSCesar Eduardo Barros 			nr_good_pages--;
30182a8f9449SShaohua Li 			/*
30192a8f9449SShaohua Li 			 * Haven't marked the cluster free yet, no list
30202a8f9449SShaohua Li 			 * operation involved
30212a8f9449SShaohua Li 			 */
30222a8f9449SShaohua Li 			inc_cluster_info_page(p, cluster_info, page_nr);
3023915d4d7bSCesar Eduardo Barros 		}
3024915d4d7bSCesar Eduardo Barros 	}
3025915d4d7bSCesar Eduardo Barros 
30262a8f9449SShaohua Li 	/* Haven't marked the cluster free yet, no list operation involved */
30272a8f9449SShaohua Li 	for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
30282a8f9449SShaohua Li 		inc_cluster_info_page(p, cluster_info, i);
30292a8f9449SShaohua Li 
3030915d4d7bSCesar Eduardo Barros 	if (nr_good_pages) {
3031915d4d7bSCesar Eduardo Barros 		swap_map[0] = SWAP_MAP_BAD;
30322a8f9449SShaohua Li 		/*
30332a8f9449SShaohua Li 		 * Not mark the cluster free yet, no list
30342a8f9449SShaohua Li 		 * operation involved
30352a8f9449SShaohua Li 		 */
30362a8f9449SShaohua Li 		inc_cluster_info_page(p, cluster_info, 0);
3037915d4d7bSCesar Eduardo Barros 		p->max = maxpages;
3038915d4d7bSCesar Eduardo Barros 		p->pages = nr_good_pages;
3039915d4d7bSCesar Eduardo Barros 		nr_extents = setup_swap_extents(p, span);
3040bdb8e3f6SCesar Eduardo Barros 		if (nr_extents < 0)
3041bdb8e3f6SCesar Eduardo Barros 			return nr_extents;
3042915d4d7bSCesar Eduardo Barros 		nr_good_pages = p->pages;
3043915d4d7bSCesar Eduardo Barros 	}
3044915d4d7bSCesar Eduardo Barros 	if (!nr_good_pages) {
3045465c47fdSAndrew Morton 		pr_warn("Empty swap-file\n");
3046bdb8e3f6SCesar Eduardo Barros 		return -EINVAL;
3047915d4d7bSCesar Eduardo Barros 	}
3048915d4d7bSCesar Eduardo Barros 
30492a8f9449SShaohua Li 	if (!cluster_info)
30502a8f9449SShaohua Li 		return nr_extents;
30512a8f9449SShaohua Li 
3052235b6217SHuang, Ying 
30534b3ef9daSHuang, Ying 	/*
30544b3ef9daSHuang, Ying 	 * Reduce false cache line sharing between cluster_info and
30554b3ef9daSHuang, Ying 	 * sharing same address space.
30564b3ef9daSHuang, Ying 	 */
3057235b6217SHuang, Ying 	for (k = 0; k < SWAP_CLUSTER_COLS; k++) {
3058235b6217SHuang, Ying 		j = (k + col) % SWAP_CLUSTER_COLS;
3059235b6217SHuang, Ying 		for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) {
3060235b6217SHuang, Ying 			idx = i * SWAP_CLUSTER_COLS + j;
3061235b6217SHuang, Ying 			if (idx >= nr_clusters)
3062235b6217SHuang, Ying 				continue;
3063235b6217SHuang, Ying 			if (cluster_count(&cluster_info[idx]))
3064235b6217SHuang, Ying 				continue;
30652a8f9449SShaohua Li 			cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE);
30666b534915SHuang Ying 			cluster_list_add_tail(&p->free_clusters, cluster_info,
30676b534915SHuang Ying 					      idx);
30682a8f9449SShaohua Li 		}
30692a8f9449SShaohua Li 	}
3070915d4d7bSCesar Eduardo Barros 	return nr_extents;
3071915d4d7bSCesar Eduardo Barros }
3072915d4d7bSCesar Eduardo Barros 
3073dcf6b7ddSRafael Aquini /*
3074dcf6b7ddSRafael Aquini  * Helper to sys_swapon determining if a given swap
3075dcf6b7ddSRafael Aquini  * backing device queue supports DISCARD operations.
3076dcf6b7ddSRafael Aquini  */
3077dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si)
3078dcf6b7ddSRafael Aquini {
3079dcf6b7ddSRafael Aquini 	struct request_queue *q = bdev_get_queue(si->bdev);
3080dcf6b7ddSRafael Aquini 
3081dcf6b7ddSRafael Aquini 	if (!q || !blk_queue_discard(q))
3082dcf6b7ddSRafael Aquini 		return false;
3083dcf6b7ddSRafael Aquini 
3084dcf6b7ddSRafael Aquini 	return true;
3085dcf6b7ddSRafael Aquini }
3086dcf6b7ddSRafael Aquini 
308753cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
308853cbb243SCesar Eduardo Barros {
308953cbb243SCesar Eduardo Barros 	struct swap_info_struct *p;
309091a27b2aSJeff Layton 	struct filename *name;
309153cbb243SCesar Eduardo Barros 	struct file *swap_file = NULL;
309253cbb243SCesar Eduardo Barros 	struct address_space *mapping;
309340531542SCesar Eduardo Barros 	int prio;
309453cbb243SCesar Eduardo Barros 	int error;
309553cbb243SCesar Eduardo Barros 	union swap_header *swap_header;
3096915d4d7bSCesar Eduardo Barros 	int nr_extents;
309753cbb243SCesar Eduardo Barros 	sector_t span;
309853cbb243SCesar Eduardo Barros 	unsigned long maxpages;
309953cbb243SCesar Eduardo Barros 	unsigned char *swap_map = NULL;
31002a8f9449SShaohua Li 	struct swap_cluster_info *cluster_info = NULL;
310138b5faf4SDan Magenheimer 	unsigned long *frontswap_map = NULL;
310253cbb243SCesar Eduardo Barros 	struct page *page = NULL;
310353cbb243SCesar Eduardo Barros 	struct inode *inode = NULL;
31047cbf3192SOmar Sandoval 	bool inced_nr_rotate_swap = false;
310553cbb243SCesar Eduardo Barros 
3106d15cab97SHugh Dickins 	if (swap_flags & ~SWAP_FLAGS_VALID)
3107d15cab97SHugh Dickins 		return -EINVAL;
3108d15cab97SHugh Dickins 
310953cbb243SCesar Eduardo Barros 	if (!capable(CAP_SYS_ADMIN))
311053cbb243SCesar Eduardo Barros 		return -EPERM;
311153cbb243SCesar Eduardo Barros 
3112a2468cc9SAaron Lu 	if (!swap_avail_heads)
3113a2468cc9SAaron Lu 		return -ENOMEM;
3114a2468cc9SAaron Lu 
311553cbb243SCesar Eduardo Barros 	p = alloc_swap_info();
31162542e513SCesar Eduardo Barros 	if (IS_ERR(p))
31172542e513SCesar Eduardo Barros 		return PTR_ERR(p);
311853cbb243SCesar Eduardo Barros 
3119815c2c54SShaohua Li 	INIT_WORK(&p->discard_work, swap_discard_work);
3120815c2c54SShaohua Li 
31211da177e4SLinus Torvalds 	name = getname(specialfile);
31221da177e4SLinus Torvalds 	if (IS_ERR(name)) {
31237de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(name);
31241da177e4SLinus Torvalds 		name = NULL;
3125bd69010bSCesar Eduardo Barros 		goto bad_swap;
31261da177e4SLinus Torvalds 	}
3127669abf4eSJeff Layton 	swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0);
31281da177e4SLinus Torvalds 	if (IS_ERR(swap_file)) {
31297de7fb6bSCesar Eduardo Barros 		error = PTR_ERR(swap_file);
31301da177e4SLinus Torvalds 		swap_file = NULL;
3131bd69010bSCesar Eduardo Barros 		goto bad_swap;
31321da177e4SLinus Torvalds 	}
31331da177e4SLinus Torvalds 
31341da177e4SLinus Torvalds 	p->swap_file = swap_file;
31351da177e4SLinus Torvalds 	mapping = swap_file->f_mapping;
31362130781eSCesar Eduardo Barros 	inode = mapping->host;
31376f179af8SHugh Dickins 
31385955102cSAl Viro 	/* If S_ISREG(inode->i_mode) will do inode_lock(inode); */
31394d0e1e10SCesar Eduardo Barros 	error = claim_swapfile(p, inode);
31404d0e1e10SCesar Eduardo Barros 	if (unlikely(error))
31411da177e4SLinus Torvalds 		goto bad_swap;
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds 	/*
31441da177e4SLinus Torvalds 	 * Read the swap header.
31451da177e4SLinus Torvalds 	 */
31461da177e4SLinus Torvalds 	if (!mapping->a_ops->readpage) {
31471da177e4SLinus Torvalds 		error = -EINVAL;
31481da177e4SLinus Torvalds 		goto bad_swap;
31491da177e4SLinus Torvalds 	}
3150090d2b18SPekka Enberg 	page = read_mapping_page(mapping, 0, swap_file);
31511da177e4SLinus Torvalds 	if (IS_ERR(page)) {
31521da177e4SLinus Torvalds 		error = PTR_ERR(page);
31531da177e4SLinus Torvalds 		goto bad_swap;
31541da177e4SLinus Torvalds 	}
315581e33971SHugh Dickins 	swap_header = kmap(page);
31561da177e4SLinus Torvalds 
3157ca8bd38bSCesar Eduardo Barros 	maxpages = read_swap_header(p, swap_header, inode);
3158ca8bd38bSCesar Eduardo Barros 	if (unlikely(!maxpages)) {
31591da177e4SLinus Torvalds 		error = -EINVAL;
31601da177e4SLinus Torvalds 		goto bad_swap;
31611da177e4SLinus Torvalds 	}
31621da177e4SLinus Torvalds 
31631da177e4SLinus Torvalds 	/* OK, set up the swap map and apply the bad block list */
3164803d0c83SCesar Eduardo Barros 	swap_map = vzalloc(maxpages);
316578ecba08SHugh Dickins 	if (!swap_map) {
31661da177e4SLinus Torvalds 		error = -ENOMEM;
31671da177e4SLinus Torvalds 		goto bad_swap;
31681da177e4SLinus Torvalds 	}
3169f0571429SMinchan Kim 
3170f0571429SMinchan Kim 	if (bdi_cap_stable_pages_required(inode_to_bdi(inode)))
3171f0571429SMinchan Kim 		p->flags |= SWP_STABLE_WRITES;
3172f0571429SMinchan Kim 
3173539a6feaSMinchan Kim 	if (bdi_cap_synchronous_io(inode_to_bdi(inode)))
3174539a6feaSMinchan Kim 		p->flags |= SWP_SYNCHRONOUS_IO;
3175539a6feaSMinchan Kim 
31762a8f9449SShaohua Li 	if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) {
31776f179af8SHugh Dickins 		int cpu;
3178235b6217SHuang, Ying 		unsigned long ci, nr_cluster;
31796f179af8SHugh Dickins 
31802a8f9449SShaohua Li 		p->flags |= SWP_SOLIDSTATE;
31812a8f9449SShaohua Li 		/*
31822a8f9449SShaohua Li 		 * select a random position to start with to help wear leveling
31832a8f9449SShaohua Li 		 * SSD
31842a8f9449SShaohua Li 		 */
31852a8f9449SShaohua Li 		p->cluster_next = 1 + (prandom_u32() % p->highest_bit);
3186235b6217SHuang, Ying 		nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER);
31872a8f9449SShaohua Li 
3188778e1cddSKees Cook 		cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info),
318954f180d3SHuang Ying 					GFP_KERNEL);
31902a8f9449SShaohua Li 		if (!cluster_info) {
31912a8f9449SShaohua Li 			error = -ENOMEM;
31922a8f9449SShaohua Li 			goto bad_swap;
31932a8f9449SShaohua Li 		}
3194235b6217SHuang, Ying 
3195235b6217SHuang, Ying 		for (ci = 0; ci < nr_cluster; ci++)
3196235b6217SHuang, Ying 			spin_lock_init(&((cluster_info + ci)->lock));
3197235b6217SHuang, Ying 
3198ebc2a1a6SShaohua Li 		p->percpu_cluster = alloc_percpu(struct percpu_cluster);
3199ebc2a1a6SShaohua Li 		if (!p->percpu_cluster) {
3200ebc2a1a6SShaohua Li 			error = -ENOMEM;
3201ebc2a1a6SShaohua Li 			goto bad_swap;
3202ebc2a1a6SShaohua Li 		}
32036f179af8SHugh Dickins 		for_each_possible_cpu(cpu) {
3204ebc2a1a6SShaohua Li 			struct percpu_cluster *cluster;
32056f179af8SHugh Dickins 			cluster = per_cpu_ptr(p->percpu_cluster, cpu);
3206ebc2a1a6SShaohua Li 			cluster_set_null(&cluster->index);
3207ebc2a1a6SShaohua Li 		}
32087cbf3192SOmar Sandoval 	} else {
320981a0298bSHuang Ying 		atomic_inc(&nr_rotate_swap);
32107cbf3192SOmar Sandoval 		inced_nr_rotate_swap = true;
32117cbf3192SOmar Sandoval 	}
32121da177e4SLinus Torvalds 
32131421ef3cSCesar Eduardo Barros 	error = swap_cgroup_swapon(p->type, maxpages);
32141421ef3cSCesar Eduardo Barros 	if (error)
32151421ef3cSCesar Eduardo Barros 		goto bad_swap;
32161421ef3cSCesar Eduardo Barros 
3217915d4d7bSCesar Eduardo Barros 	nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
32182a8f9449SShaohua Li 		cluster_info, maxpages, &span);
3219915d4d7bSCesar Eduardo Barros 	if (unlikely(nr_extents < 0)) {
322053092a74SHugh Dickins 		error = nr_extents;
3221e2244ec2SHugh Dickins 		goto bad_swap;
322253092a74SHugh Dickins 	}
322338b5faf4SDan Magenheimer 	/* frontswap enabled? set up bit-per-page map for frontswap */
32248ea1d2a1SVlastimil Babka 	if (IS_ENABLED(CONFIG_FRONTSWAP))
3225778e1cddSKees Cook 		frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages),
3226778e1cddSKees Cook 					 sizeof(long),
322754f180d3SHuang Ying 					 GFP_KERNEL);
32281da177e4SLinus Torvalds 
32292a8f9449SShaohua Li 	if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) {
3230dcf6b7ddSRafael Aquini 		/*
3231dcf6b7ddSRafael Aquini 		 * When discard is enabled for swap with no particular
3232dcf6b7ddSRafael Aquini 		 * policy flagged, we set all swap discard flags here in
3233dcf6b7ddSRafael Aquini 		 * order to sustain backward compatibility with older
3234dcf6b7ddSRafael Aquini 		 * swapon(8) releases.
3235dcf6b7ddSRafael Aquini 		 */
3236dcf6b7ddSRafael Aquini 		p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD |
3237dcf6b7ddSRafael Aquini 			     SWP_PAGE_DISCARD);
3238dcf6b7ddSRafael Aquini 
3239dcf6b7ddSRafael Aquini 		/*
3240dcf6b7ddSRafael Aquini 		 * By flagging sys_swapon, a sysadmin can tell us to
3241dcf6b7ddSRafael Aquini 		 * either do single-time area discards only, or to just
3242dcf6b7ddSRafael Aquini 		 * perform discards for released swap page-clusters.
3243dcf6b7ddSRafael Aquini 		 * Now it's time to adjust the p->flags accordingly.
3244dcf6b7ddSRafael Aquini 		 */
3245dcf6b7ddSRafael Aquini 		if (swap_flags & SWAP_FLAG_DISCARD_ONCE)
3246dcf6b7ddSRafael Aquini 			p->flags &= ~SWP_PAGE_DISCARD;
3247dcf6b7ddSRafael Aquini 		else if (swap_flags & SWAP_FLAG_DISCARD_PAGES)
3248dcf6b7ddSRafael Aquini 			p->flags &= ~SWP_AREA_DISCARD;
3249dcf6b7ddSRafael Aquini 
3250dcf6b7ddSRafael Aquini 		/* issue a swapon-time discard if it's still required */
3251dcf6b7ddSRafael Aquini 		if (p->flags & SWP_AREA_DISCARD) {
3252dcf6b7ddSRafael Aquini 			int err = discard_swap(p);
3253dcf6b7ddSRafael Aquini 			if (unlikely(err))
3254465c47fdSAndrew Morton 				pr_err("swapon: discard_swap(%p): %d\n",
3255dcf6b7ddSRafael Aquini 					p, err);
3256dcf6b7ddSRafael Aquini 		}
3257dcf6b7ddSRafael Aquini 	}
32586a6ba831SHugh Dickins 
32594b3ef9daSHuang, Ying 	error = init_swap_address_space(p->type, maxpages);
32604b3ef9daSHuang, Ying 	if (error)
32614b3ef9daSHuang, Ying 		goto bad_swap;
32624b3ef9daSHuang, Ying 
3263fc0abb14SIngo Molnar 	mutex_lock(&swapon_mutex);
326440531542SCesar Eduardo Barros 	prio = -1;
326578ecba08SHugh Dickins 	if (swap_flags & SWAP_FLAG_PREFER)
326640531542SCesar Eduardo Barros 		prio =
326778ecba08SHugh Dickins 		  (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
32682a8f9449SShaohua Li 	enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map);
3269c69dbfb8SCesar Eduardo Barros 
3270756a025fSJoe Perches 	pr_info("Adding %uk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s%s\n",
327191a27b2aSJeff Layton 		p->pages<<(PAGE_SHIFT-10), name->name, p->prio,
3272c69dbfb8SCesar Eduardo Barros 		nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
3273c69dbfb8SCesar Eduardo Barros 		(p->flags & SWP_SOLIDSTATE) ? "SS" : "",
327438b5faf4SDan Magenheimer 		(p->flags & SWP_DISCARDABLE) ? "D" : "",
3275dcf6b7ddSRafael Aquini 		(p->flags & SWP_AREA_DISCARD) ? "s" : "",
3276dcf6b7ddSRafael Aquini 		(p->flags & SWP_PAGE_DISCARD) ? "c" : "",
327738b5faf4SDan Magenheimer 		(frontswap_map) ? "FS" : "");
3278c69dbfb8SCesar Eduardo Barros 
3279fc0abb14SIngo Molnar 	mutex_unlock(&swapon_mutex);
328066d7dd51SKay Sievers 	atomic_inc(&proc_poll_event);
328166d7dd51SKay Sievers 	wake_up_interruptible(&proc_poll_wait);
328266d7dd51SKay Sievers 
32839b01c350SCesar Eduardo Barros 	if (S_ISREG(inode->i_mode))
32849b01c350SCesar Eduardo Barros 		inode->i_flags |= S_SWAPFILE;
32851da177e4SLinus Torvalds 	error = 0;
32861da177e4SLinus Torvalds 	goto out;
32871da177e4SLinus Torvalds bad_swap:
3288ebc2a1a6SShaohua Li 	free_percpu(p->percpu_cluster);
3289ebc2a1a6SShaohua Li 	p->percpu_cluster = NULL;
3290bd69010bSCesar Eduardo Barros 	if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
3291f2090d2dSCesar Eduardo Barros 		set_blocksize(p->bdev, p->old_block_size);
3292f2090d2dSCesar Eduardo Barros 		blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
32931da177e4SLinus Torvalds 	}
32944cd3bb10SHugh Dickins 	destroy_swap_extents(p);
3295e8e6c2ecSCesar Eduardo Barros 	swap_cgroup_swapoff(p->type);
32965d337b91SHugh Dickins 	spin_lock(&swap_lock);
32971da177e4SLinus Torvalds 	p->swap_file = NULL;
32981da177e4SLinus Torvalds 	p->flags = 0;
32995d337b91SHugh Dickins 	spin_unlock(&swap_lock);
33001da177e4SLinus Torvalds 	vfree(swap_map);
33018606a1a9SDarrick J. Wong 	kvfree(cluster_info);
3302b6b1fd2aSDavid Rientjes 	kvfree(frontswap_map);
33037cbf3192SOmar Sandoval 	if (inced_nr_rotate_swap)
33047cbf3192SOmar Sandoval 		atomic_dec(&nr_rotate_swap);
330552c50567SMel Gorman 	if (swap_file) {
33062130781eSCesar Eduardo Barros 		if (inode && S_ISREG(inode->i_mode)) {
33075955102cSAl Viro 			inode_unlock(inode);
33082130781eSCesar Eduardo Barros 			inode = NULL;
33092130781eSCesar Eduardo Barros 		}
33101da177e4SLinus Torvalds 		filp_close(swap_file, NULL);
331152c50567SMel Gorman 	}
33121da177e4SLinus Torvalds out:
33131da177e4SLinus Torvalds 	if (page && !IS_ERR(page)) {
33141da177e4SLinus Torvalds 		kunmap(page);
331509cbfeafSKirill A. Shutemov 		put_page(page);
33161da177e4SLinus Torvalds 	}
33171da177e4SLinus Torvalds 	if (name)
33181da177e4SLinus Torvalds 		putname(name);
33199b01c350SCesar Eduardo Barros 	if (inode && S_ISREG(inode->i_mode))
33205955102cSAl Viro 		inode_unlock(inode);
3321039939a6STim Chen 	if (!error)
3322039939a6STim Chen 		enable_swap_slots_cache();
33231da177e4SLinus Torvalds 	return error;
33241da177e4SLinus Torvalds }
33251da177e4SLinus Torvalds 
33261da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val)
33271da177e4SLinus Torvalds {
3328efa90a98SHugh Dickins 	unsigned int type;
33291da177e4SLinus Torvalds 	unsigned long nr_to_be_unused = 0;
33301da177e4SLinus Torvalds 
33315d337b91SHugh Dickins 	spin_lock(&swap_lock);
3332efa90a98SHugh Dickins 	for (type = 0; type < nr_swapfiles; type++) {
3333efa90a98SHugh Dickins 		struct swap_info_struct *si = swap_info[type];
3334efa90a98SHugh Dickins 
3335efa90a98SHugh Dickins 		if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
3336efa90a98SHugh Dickins 			nr_to_be_unused += si->inuse_pages;
33371da177e4SLinus Torvalds 	}
3338ec8acf20SShaohua Li 	val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
33391da177e4SLinus Torvalds 	val->totalswap = total_swap_pages + nr_to_be_unused;
33405d337b91SHugh Dickins 	spin_unlock(&swap_lock);
33411da177e4SLinus Torvalds }
33421da177e4SLinus Torvalds 
33431da177e4SLinus Torvalds /*
33441da177e4SLinus Torvalds  * Verify that a swap entry is valid and increment its swap map count.
33451da177e4SLinus Torvalds  *
3346355cfa73SKAMEZAWA Hiroyuki  * Returns error code in following case.
3347355cfa73SKAMEZAWA Hiroyuki  * - success -> 0
3348355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is invalid -> EINVAL
3349355cfa73SKAMEZAWA Hiroyuki  * - swp_entry is migration entry -> EINVAL
3350355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but there is already one. -> EEXIST
3351355cfa73SKAMEZAWA Hiroyuki  * - swap-cache reference is requested but the entry is not used. -> ENOENT
3352570a335bSHugh Dickins  * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
33531da177e4SLinus Torvalds  */
33548d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
33551da177e4SLinus Torvalds {
33561da177e4SLinus Torvalds 	struct swap_info_struct *p;
3357235b6217SHuang, Ying 	struct swap_cluster_info *ci;
33581da177e4SLinus Torvalds 	unsigned long offset, type;
33598d69aaeeSHugh Dickins 	unsigned char count;
33608d69aaeeSHugh Dickins 	unsigned char has_cache;
3361253d553bSHugh Dickins 	int err = -EINVAL;
33621da177e4SLinus Torvalds 
3363a7420aa5SAndi Kleen 	if (non_swap_entry(entry))
3364253d553bSHugh Dickins 		goto out;
33650697212aSChristoph Lameter 
33661da177e4SLinus Torvalds 	type = swp_type(entry);
33671da177e4SLinus Torvalds 	if (type >= nr_swapfiles)
33681da177e4SLinus Torvalds 		goto bad_file;
3369efa90a98SHugh Dickins 	p = swap_info[type];
33701da177e4SLinus Torvalds 	offset = swp_offset(entry);
3371355cfa73SKAMEZAWA Hiroyuki 	if (unlikely(offset >= p->max))
3372235b6217SHuang, Ying 		goto out;
3373235b6217SHuang, Ying 
3374235b6217SHuang, Ying 	ci = lock_cluster_or_swap_info(p, offset);
3375355cfa73SKAMEZAWA Hiroyuki 
3376253d553bSHugh Dickins 	count = p->swap_map[offset];
3377edfe23daSShaohua Li 
3378edfe23daSShaohua Li 	/*
3379edfe23daSShaohua Li 	 * swapin_readahead() doesn't check if a swap entry is valid, so the
3380edfe23daSShaohua Li 	 * swap entry could be SWAP_MAP_BAD. Check here with lock held.
3381edfe23daSShaohua Li 	 */
3382edfe23daSShaohua Li 	if (unlikely(swap_count(count) == SWAP_MAP_BAD)) {
3383edfe23daSShaohua Li 		err = -ENOENT;
3384edfe23daSShaohua Li 		goto unlock_out;
3385edfe23daSShaohua Li 	}
3386edfe23daSShaohua Li 
3387253d553bSHugh Dickins 	has_cache = count & SWAP_HAS_CACHE;
3388253d553bSHugh Dickins 	count &= ~SWAP_HAS_CACHE;
3389253d553bSHugh Dickins 	err = 0;
3390355cfa73SKAMEZAWA Hiroyuki 
3391253d553bSHugh Dickins 	if (usage == SWAP_HAS_CACHE) {
3392355cfa73SKAMEZAWA Hiroyuki 
3393355cfa73SKAMEZAWA Hiroyuki 		/* set SWAP_HAS_CACHE if there is no cache and entry is used */
3394253d553bSHugh Dickins 		if (!has_cache && count)
3395253d553bSHugh Dickins 			has_cache = SWAP_HAS_CACHE;
3396253d553bSHugh Dickins 		else if (has_cache)		/* someone else added cache */
3397253d553bSHugh Dickins 			err = -EEXIST;
3398253d553bSHugh Dickins 		else				/* no users remaining */
3399253d553bSHugh Dickins 			err = -ENOENT;
3400355cfa73SKAMEZAWA Hiroyuki 
3401355cfa73SKAMEZAWA Hiroyuki 	} else if (count || has_cache) {
3402253d553bSHugh Dickins 
3403570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
3404570a335bSHugh Dickins 			count += usage;
3405570a335bSHugh Dickins 		else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
3406253d553bSHugh Dickins 			err = -EINVAL;
3407570a335bSHugh Dickins 		else if (swap_count_continued(p, offset, count))
3408570a335bSHugh Dickins 			count = COUNT_CONTINUED;
3409570a335bSHugh Dickins 		else
3410570a335bSHugh Dickins 			err = -ENOMEM;
3411253d553bSHugh Dickins 	} else
3412253d553bSHugh Dickins 		err = -ENOENT;			/* unused swap entry */
3413253d553bSHugh Dickins 
3414253d553bSHugh Dickins 	p->swap_map[offset] = count | has_cache;
3415253d553bSHugh Dickins 
3416355cfa73SKAMEZAWA Hiroyuki unlock_out:
3417235b6217SHuang, Ying 	unlock_cluster_or_swap_info(p, ci);
34181da177e4SLinus Torvalds out:
3419253d553bSHugh Dickins 	return err;
34201da177e4SLinus Torvalds 
34211da177e4SLinus Torvalds bad_file:
3422465c47fdSAndrew Morton 	pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val);
34231da177e4SLinus Torvalds 	goto out;
34241da177e4SLinus Torvalds }
3425253d553bSHugh Dickins 
3426355cfa73SKAMEZAWA Hiroyuki /*
3427aaa46865SHugh Dickins  * Help swapoff by noting that swap entry belongs to shmem/tmpfs
3428aaa46865SHugh Dickins  * (in which case its reference count is never incremented).
3429aaa46865SHugh Dickins  */
3430aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry)
3431aaa46865SHugh Dickins {
3432aaa46865SHugh Dickins 	__swap_duplicate(entry, SWAP_MAP_SHMEM);
3433aaa46865SHugh Dickins }
3434aaa46865SHugh Dickins 
3435aaa46865SHugh Dickins /*
343608259d58SHugh Dickins  * Increase reference count of swap entry by 1.
343708259d58SHugh Dickins  * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
343808259d58SHugh Dickins  * but could not be atomically allocated.  Returns 0, just as if it succeeded,
343908259d58SHugh Dickins  * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
344008259d58SHugh Dickins  * might occur if a page table entry has got corrupted.
3441355cfa73SKAMEZAWA Hiroyuki  */
3442570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry)
3443355cfa73SKAMEZAWA Hiroyuki {
3444570a335bSHugh Dickins 	int err = 0;
3445570a335bSHugh Dickins 
3446570a335bSHugh Dickins 	while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
3447570a335bSHugh Dickins 		err = add_swap_count_continuation(entry, GFP_ATOMIC);
3448570a335bSHugh Dickins 	return err;
3449355cfa73SKAMEZAWA Hiroyuki }
34501da177e4SLinus Torvalds 
3451cb4b86baSKAMEZAWA Hiroyuki /*
3452355cfa73SKAMEZAWA Hiroyuki  * @entry: swap entry for which we allocate swap cache.
3453355cfa73SKAMEZAWA Hiroyuki  *
345473c34b6aSHugh Dickins  * Called when allocating swap cache for existing swap entry,
3455355cfa73SKAMEZAWA Hiroyuki  * This can return error codes. Returns 0 at success.
3456355cfa73SKAMEZAWA Hiroyuki  * -EBUSY means there is a swap cache.
3457355cfa73SKAMEZAWA Hiroyuki  * Note: return code is different from swap_duplicate().
3458cb4b86baSKAMEZAWA Hiroyuki  */
3459cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry)
3460cb4b86baSKAMEZAWA Hiroyuki {
3461253d553bSHugh Dickins 	return __swap_duplicate(entry, SWAP_HAS_CACHE);
3462cb4b86baSKAMEZAWA Hiroyuki }
3463cb4b86baSKAMEZAWA Hiroyuki 
34640bcac06fSMinchan Kim struct swap_info_struct *swp_swap_info(swp_entry_t entry)
34650bcac06fSMinchan Kim {
34660bcac06fSMinchan Kim 	return swap_info[swp_type(entry)];
34670bcac06fSMinchan Kim }
34680bcac06fSMinchan Kim 
3469f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page)
3470f981c595SMel Gorman {
34710bcac06fSMinchan Kim 	swp_entry_t entry = { .val = page_private(page) };
34720bcac06fSMinchan Kim 	return swp_swap_info(entry);
3473f981c595SMel Gorman }
3474f981c595SMel Gorman 
3475f981c595SMel Gorman /*
3476f981c595SMel Gorman  * out-of-line __page_file_ methods to avoid include hell.
3477f981c595SMel Gorman  */
3478f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page)
3479f981c595SMel Gorman {
3480f981c595SMel Gorman 	return page_swap_info(page)->swap_file->f_mapping;
3481f981c595SMel Gorman }
3482f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping);
3483f981c595SMel Gorman 
3484f981c595SMel Gorman pgoff_t __page_file_index(struct page *page)
3485f981c595SMel Gorman {
3486f981c595SMel Gorman 	swp_entry_t swap = { .val = page_private(page) };
3487f981c595SMel Gorman 	return swp_offset(swap);
3488f981c595SMel Gorman }
3489f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index);
3490f981c595SMel Gorman 
34911da177e4SLinus Torvalds /*
3492570a335bSHugh Dickins  * add_swap_count_continuation - called when a swap count is duplicated
3493570a335bSHugh Dickins  * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
3494570a335bSHugh Dickins  * page of the original vmalloc'ed swap_map, to hold the continuation count
3495570a335bSHugh Dickins  * (for that entry and for its neighbouring PAGE_SIZE swap entries).  Called
3496570a335bSHugh Dickins  * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
3497570a335bSHugh Dickins  *
3498570a335bSHugh Dickins  * These continuation pages are seldom referenced: the common paths all work
3499570a335bSHugh Dickins  * on the original swap_map, only referring to a continuation page when the
3500570a335bSHugh Dickins  * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
3501570a335bSHugh Dickins  *
3502570a335bSHugh Dickins  * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
3503570a335bSHugh Dickins  * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
3504570a335bSHugh Dickins  * can be called after dropping locks.
3505570a335bSHugh Dickins  */
3506570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
3507570a335bSHugh Dickins {
3508570a335bSHugh Dickins 	struct swap_info_struct *si;
3509235b6217SHuang, Ying 	struct swap_cluster_info *ci;
3510570a335bSHugh Dickins 	struct page *head;
3511570a335bSHugh Dickins 	struct page *page;
3512570a335bSHugh Dickins 	struct page *list_page;
3513570a335bSHugh Dickins 	pgoff_t offset;
3514570a335bSHugh Dickins 	unsigned char count;
3515570a335bSHugh Dickins 
3516570a335bSHugh Dickins 	/*
3517570a335bSHugh Dickins 	 * When debugging, it's easier to use __GFP_ZERO here; but it's better
3518570a335bSHugh Dickins 	 * for latency not to zero a page while GFP_ATOMIC and holding locks.
3519570a335bSHugh Dickins 	 */
3520570a335bSHugh Dickins 	page = alloc_page(gfp_mask | __GFP_HIGHMEM);
3521570a335bSHugh Dickins 
3522570a335bSHugh Dickins 	si = swap_info_get(entry);
3523570a335bSHugh Dickins 	if (!si) {
3524570a335bSHugh Dickins 		/*
3525570a335bSHugh Dickins 		 * An acceptable race has occurred since the failing
3526570a335bSHugh Dickins 		 * __swap_duplicate(): the swap entry has been freed,
3527570a335bSHugh Dickins 		 * perhaps even the whole swap_map cleared for swapoff.
3528570a335bSHugh Dickins 		 */
3529570a335bSHugh Dickins 		goto outer;
3530570a335bSHugh Dickins 	}
3531570a335bSHugh Dickins 
3532570a335bSHugh Dickins 	offset = swp_offset(entry);
3533235b6217SHuang, Ying 
3534235b6217SHuang, Ying 	ci = lock_cluster(si, offset);
3535235b6217SHuang, Ying 
3536570a335bSHugh Dickins 	count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
3537570a335bSHugh Dickins 
3538570a335bSHugh Dickins 	if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
3539570a335bSHugh Dickins 		/*
3540570a335bSHugh Dickins 		 * The higher the swap count, the more likely it is that tasks
3541570a335bSHugh Dickins 		 * will race to add swap count continuation: we need to avoid
3542570a335bSHugh Dickins 		 * over-provisioning.
3543570a335bSHugh Dickins 		 */
3544570a335bSHugh Dickins 		goto out;
3545570a335bSHugh Dickins 	}
3546570a335bSHugh Dickins 
3547570a335bSHugh Dickins 	if (!page) {
3548235b6217SHuang, Ying 		unlock_cluster(ci);
3549ec8acf20SShaohua Li 		spin_unlock(&si->lock);
3550570a335bSHugh Dickins 		return -ENOMEM;
3551570a335bSHugh Dickins 	}
3552570a335bSHugh Dickins 
3553570a335bSHugh Dickins 	/*
3554570a335bSHugh Dickins 	 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
3555570a335bSHugh Dickins 	 * no architecture is using highmem pages for kernel page tables: so it
3556570a335bSHugh Dickins 	 * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps.
3557570a335bSHugh Dickins 	 */
3558570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
3559570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
3560570a335bSHugh Dickins 
35612628bd6fSHuang Ying 	spin_lock(&si->cont_lock);
3562570a335bSHugh Dickins 	/*
3563570a335bSHugh Dickins 	 * Page allocation does not initialize the page's lru field,
3564570a335bSHugh Dickins 	 * but it does always reset its private field.
3565570a335bSHugh Dickins 	 */
3566570a335bSHugh Dickins 	if (!page_private(head)) {
3567570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
3568570a335bSHugh Dickins 		INIT_LIST_HEAD(&head->lru);
3569570a335bSHugh Dickins 		set_page_private(head, SWP_CONTINUED);
3570570a335bSHugh Dickins 		si->flags |= SWP_CONTINUED;
3571570a335bSHugh Dickins 	}
3572570a335bSHugh Dickins 
3573570a335bSHugh Dickins 	list_for_each_entry(list_page, &head->lru, lru) {
3574570a335bSHugh Dickins 		unsigned char *map;
3575570a335bSHugh Dickins 
3576570a335bSHugh Dickins 		/*
3577570a335bSHugh Dickins 		 * If the previous map said no continuation, but we've found
3578570a335bSHugh Dickins 		 * a continuation page, free our allocation and use this one.
3579570a335bSHugh Dickins 		 */
3580570a335bSHugh Dickins 		if (!(count & COUNT_CONTINUED))
35812628bd6fSHuang Ying 			goto out_unlock_cont;
3582570a335bSHugh Dickins 
35839b04c5feSCong Wang 		map = kmap_atomic(list_page) + offset;
3584570a335bSHugh Dickins 		count = *map;
35859b04c5feSCong Wang 		kunmap_atomic(map);
3586570a335bSHugh Dickins 
3587570a335bSHugh Dickins 		/*
3588570a335bSHugh Dickins 		 * If this continuation count now has some space in it,
3589570a335bSHugh Dickins 		 * free our allocation and use this one.
3590570a335bSHugh Dickins 		 */
3591570a335bSHugh Dickins 		if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
35922628bd6fSHuang Ying 			goto out_unlock_cont;
3593570a335bSHugh Dickins 	}
3594570a335bSHugh Dickins 
3595570a335bSHugh Dickins 	list_add_tail(&page->lru, &head->lru);
3596570a335bSHugh Dickins 	page = NULL;			/* now it's attached, don't free it */
35972628bd6fSHuang Ying out_unlock_cont:
35982628bd6fSHuang Ying 	spin_unlock(&si->cont_lock);
3599570a335bSHugh Dickins out:
3600235b6217SHuang, Ying 	unlock_cluster(ci);
3601ec8acf20SShaohua Li 	spin_unlock(&si->lock);
3602570a335bSHugh Dickins outer:
3603570a335bSHugh Dickins 	if (page)
3604570a335bSHugh Dickins 		__free_page(page);
3605570a335bSHugh Dickins 	return 0;
3606570a335bSHugh Dickins }
3607570a335bSHugh Dickins 
3608570a335bSHugh Dickins /*
3609570a335bSHugh Dickins  * swap_count_continued - when the original swap_map count is incremented
3610570a335bSHugh Dickins  * from SWAP_MAP_MAX, check if there is already a continuation page to carry
3611570a335bSHugh Dickins  * into, carry if so, or else fail until a new continuation page is allocated;
3612570a335bSHugh Dickins  * when the original swap_map count is decremented from 0 with continuation,
3613570a335bSHugh Dickins  * borrow from the continuation and report whether it still holds more.
3614235b6217SHuang, Ying  * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster
3615235b6217SHuang, Ying  * lock.
3616570a335bSHugh Dickins  */
3617570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si,
3618570a335bSHugh Dickins 				 pgoff_t offset, unsigned char count)
3619570a335bSHugh Dickins {
3620570a335bSHugh Dickins 	struct page *head;
3621570a335bSHugh Dickins 	struct page *page;
3622570a335bSHugh Dickins 	unsigned char *map;
36232628bd6fSHuang Ying 	bool ret;
3624570a335bSHugh Dickins 
3625570a335bSHugh Dickins 	head = vmalloc_to_page(si->swap_map + offset);
3626570a335bSHugh Dickins 	if (page_private(head) != SWP_CONTINUED) {
3627570a335bSHugh Dickins 		BUG_ON(count & COUNT_CONTINUED);
3628570a335bSHugh Dickins 		return false;		/* need to add count continuation */
3629570a335bSHugh Dickins 	}
3630570a335bSHugh Dickins 
36312628bd6fSHuang Ying 	spin_lock(&si->cont_lock);
3632570a335bSHugh Dickins 	offset &= ~PAGE_MASK;
3633570a335bSHugh Dickins 	page = list_entry(head->lru.next, struct page, lru);
36349b04c5feSCong Wang 	map = kmap_atomic(page) + offset;
3635570a335bSHugh Dickins 
3636570a335bSHugh Dickins 	if (count == SWAP_MAP_MAX)	/* initial increment from swap_map */
3637570a335bSHugh Dickins 		goto init_map;		/* jump over SWAP_CONT_MAX checks */
3638570a335bSHugh Dickins 
3639570a335bSHugh Dickins 	if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
3640570a335bSHugh Dickins 		/*
3641570a335bSHugh Dickins 		 * Think of how you add 1 to 999
3642570a335bSHugh Dickins 		 */
3643570a335bSHugh Dickins 		while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
36449b04c5feSCong Wang 			kunmap_atomic(map);
3645570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
3646570a335bSHugh Dickins 			BUG_ON(page == head);
36479b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
3648570a335bSHugh Dickins 		}
3649570a335bSHugh Dickins 		if (*map == SWAP_CONT_MAX) {
36509b04c5feSCong Wang 			kunmap_atomic(map);
3651570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
36522628bd6fSHuang Ying 			if (page == head) {
36532628bd6fSHuang Ying 				ret = false;	/* add count continuation */
36542628bd6fSHuang Ying 				goto out;
36552628bd6fSHuang Ying 			}
36569b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
3657570a335bSHugh Dickins init_map:		*map = 0;		/* we didn't zero the page */
3658570a335bSHugh Dickins 		}
3659570a335bSHugh Dickins 		*map += 1;
36609b04c5feSCong Wang 		kunmap_atomic(map);
3661570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
3662570a335bSHugh Dickins 		while (page != head) {
36639b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
3664570a335bSHugh Dickins 			*map = COUNT_CONTINUED;
36659b04c5feSCong Wang 			kunmap_atomic(map);
3666570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
3667570a335bSHugh Dickins 		}
36682628bd6fSHuang Ying 		ret = true;			/* incremented */
3669570a335bSHugh Dickins 
3670570a335bSHugh Dickins 	} else {				/* decrementing */
3671570a335bSHugh Dickins 		/*
3672570a335bSHugh Dickins 		 * Think of how you subtract 1 from 1000
3673570a335bSHugh Dickins 		 */
3674570a335bSHugh Dickins 		BUG_ON(count != COUNT_CONTINUED);
3675570a335bSHugh Dickins 		while (*map == COUNT_CONTINUED) {
36769b04c5feSCong Wang 			kunmap_atomic(map);
3677570a335bSHugh Dickins 			page = list_entry(page->lru.next, struct page, lru);
3678570a335bSHugh Dickins 			BUG_ON(page == head);
36799b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
3680570a335bSHugh Dickins 		}
3681570a335bSHugh Dickins 		BUG_ON(*map == 0);
3682570a335bSHugh Dickins 		*map -= 1;
3683570a335bSHugh Dickins 		if (*map == 0)
3684570a335bSHugh Dickins 			count = 0;
36859b04c5feSCong Wang 		kunmap_atomic(map);
3686570a335bSHugh Dickins 		page = list_entry(page->lru.prev, struct page, lru);
3687570a335bSHugh Dickins 		while (page != head) {
36889b04c5feSCong Wang 			map = kmap_atomic(page) + offset;
3689570a335bSHugh Dickins 			*map = SWAP_CONT_MAX | count;
3690570a335bSHugh Dickins 			count = COUNT_CONTINUED;
36919b04c5feSCong Wang 			kunmap_atomic(map);
3692570a335bSHugh Dickins 			page = list_entry(page->lru.prev, struct page, lru);
3693570a335bSHugh Dickins 		}
36942628bd6fSHuang Ying 		ret = count == COUNT_CONTINUED;
3695570a335bSHugh Dickins 	}
36962628bd6fSHuang Ying out:
36972628bd6fSHuang Ying 	spin_unlock(&si->cont_lock);
36982628bd6fSHuang Ying 	return ret;
3699570a335bSHugh Dickins }
3700570a335bSHugh Dickins 
3701570a335bSHugh Dickins /*
3702570a335bSHugh Dickins  * free_swap_count_continuations - swapoff free all the continuation pages
3703570a335bSHugh Dickins  * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
3704570a335bSHugh Dickins  */
3705570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si)
3706570a335bSHugh Dickins {
3707570a335bSHugh Dickins 	pgoff_t offset;
3708570a335bSHugh Dickins 
3709570a335bSHugh Dickins 	for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
3710570a335bSHugh Dickins 		struct page *head;
3711570a335bSHugh Dickins 		head = vmalloc_to_page(si->swap_map + offset);
3712570a335bSHugh Dickins 		if (page_private(head)) {
37130d576d20SGeliang Tang 			struct page *page, *next;
37140d576d20SGeliang Tang 
37150d576d20SGeliang Tang 			list_for_each_entry_safe(page, next, &head->lru, lru) {
37160d576d20SGeliang Tang 				list_del(&page->lru);
3717570a335bSHugh Dickins 				__free_page(page);
3718570a335bSHugh Dickins 			}
3719570a335bSHugh Dickins 		}
3720570a335bSHugh Dickins 	}
3721570a335bSHugh Dickins }
3722a2468cc9SAaron Lu 
37232cf85583STejun Heo #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP)
37242cf85583STejun Heo void mem_cgroup_throttle_swaprate(struct mem_cgroup *memcg, int node,
37252cf85583STejun Heo 				  gfp_t gfp_mask)
37262cf85583STejun Heo {
37272cf85583STejun Heo 	struct swap_info_struct *si, *next;
37282cf85583STejun Heo 	if (!(gfp_mask & __GFP_IO) || !memcg)
37292cf85583STejun Heo 		return;
37302cf85583STejun Heo 
37312cf85583STejun Heo 	if (!blk_cgroup_congested())
37322cf85583STejun Heo 		return;
37332cf85583STejun Heo 
37342cf85583STejun Heo 	/*
37352cf85583STejun Heo 	 * We've already scheduled a throttle, avoid taking the global swap
37362cf85583STejun Heo 	 * lock.
37372cf85583STejun Heo 	 */
37382cf85583STejun Heo 	if (current->throttle_queue)
37392cf85583STejun Heo 		return;
37402cf85583STejun Heo 
37412cf85583STejun Heo 	spin_lock(&swap_avail_lock);
37422cf85583STejun Heo 	plist_for_each_entry_safe(si, next, &swap_avail_heads[node],
37432cf85583STejun Heo 				  avail_lists[node]) {
37442cf85583STejun Heo 		if (si->bdev) {
37452cf85583STejun Heo 			blkcg_schedule_throttle(bdev_get_queue(si->bdev),
37462cf85583STejun Heo 						true);
37472cf85583STejun Heo 			break;
37482cf85583STejun Heo 		}
37492cf85583STejun Heo 	}
37502cf85583STejun Heo 	spin_unlock(&swap_avail_lock);
37512cf85583STejun Heo }
37522cf85583STejun Heo #endif
37532cf85583STejun Heo 
3754a2468cc9SAaron Lu static int __init swapfile_init(void)
3755a2468cc9SAaron Lu {
3756a2468cc9SAaron Lu 	int nid;
3757a2468cc9SAaron Lu 
3758a2468cc9SAaron Lu 	swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head),
3759a2468cc9SAaron Lu 					 GFP_KERNEL);
3760a2468cc9SAaron Lu 	if (!swap_avail_heads) {
3761a2468cc9SAaron Lu 		pr_emerg("Not enough memory for swap heads, swap is disabled\n");
3762a2468cc9SAaron Lu 		return -ENOMEM;
3763a2468cc9SAaron Lu 	}
3764a2468cc9SAaron Lu 
3765a2468cc9SAaron Lu 	for_each_node(nid)
3766a2468cc9SAaron Lu 		plist_head_init(&swap_avail_heads[nid]);
3767a2468cc9SAaron Lu 
3768a2468cc9SAaron Lu 	return 0;
3769a2468cc9SAaron Lu }
3770a2468cc9SAaron Lu subsys_initcall(swapfile_init);
3771