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; 6378ecba08SHugh Dickins static int least_priority; 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 */ 8818ab4d4cSDan Streetman static PLIST_HEAD(swap_avail_head); 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 998d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 100355cfa73SKAMEZAWA Hiroyuki { 101570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 102355cfa73SKAMEZAWA Hiroyuki } 103355cfa73SKAMEZAWA Hiroyuki 104efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 105c9e44410SKAMEZAWA Hiroyuki static int 106c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 107c9e44410SKAMEZAWA Hiroyuki { 108efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 109c9e44410SKAMEZAWA Hiroyuki struct page *page; 110c9e44410SKAMEZAWA Hiroyuki int ret = 0; 111c9e44410SKAMEZAWA Hiroyuki 112f6ab1f7fSHuang Ying page = find_get_page(swap_address_space(entry), swp_offset(entry)); 113c9e44410SKAMEZAWA Hiroyuki if (!page) 114c9e44410SKAMEZAWA Hiroyuki return 0; 115c9e44410SKAMEZAWA Hiroyuki /* 116c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 117c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 118c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 119c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 120c9e44410SKAMEZAWA Hiroyuki * in usual operations. 121c9e44410SKAMEZAWA Hiroyuki */ 122c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 123c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 124c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 125c9e44410SKAMEZAWA Hiroyuki } 12609cbfeafSKirill A. Shutemov put_page(page); 127c9e44410SKAMEZAWA Hiroyuki return ret; 128c9e44410SKAMEZAWA Hiroyuki } 129355cfa73SKAMEZAWA Hiroyuki 1301da177e4SLinus Torvalds /* 1316a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1326a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1336a6ba831SHugh Dickins */ 1346a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1356a6ba831SHugh Dickins { 1366a6ba831SHugh Dickins struct swap_extent *se; 1379625a5f2SHugh Dickins sector_t start_block; 1389625a5f2SHugh Dickins sector_t nr_blocks; 1396a6ba831SHugh Dickins int err = 0; 1406a6ba831SHugh Dickins 1416a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1429625a5f2SHugh Dickins se = &si->first_swap_extent; 1439625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1449625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1459625a5f2SHugh Dickins if (nr_blocks) { 1469625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 147dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1489625a5f2SHugh Dickins if (err) 1499625a5f2SHugh Dickins return err; 1509625a5f2SHugh Dickins cond_resched(); 1516a6ba831SHugh Dickins } 1526a6ba831SHugh Dickins 1539625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1549625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1559625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1569625a5f2SHugh Dickins 1576a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 158dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1596a6ba831SHugh Dickins if (err) 1606a6ba831SHugh Dickins break; 1616a6ba831SHugh Dickins 1626a6ba831SHugh Dickins cond_resched(); 1636a6ba831SHugh Dickins } 1646a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1656a6ba831SHugh Dickins } 1666a6ba831SHugh Dickins 1677992fde7SHugh Dickins /* 1687992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1697992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1707992fde7SHugh Dickins */ 1717992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1727992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1737992fde7SHugh Dickins { 1747992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1757992fde7SHugh Dickins int found_extent = 0; 1767992fde7SHugh Dickins 1777992fde7SHugh Dickins while (nr_pages) { 1787992fde7SHugh Dickins if (se->start_page <= start_page && 1797992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1807992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1817992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 182858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1837992fde7SHugh Dickins 1847992fde7SHugh Dickins if (nr_blocks > nr_pages) 1857992fde7SHugh Dickins nr_blocks = nr_pages; 1867992fde7SHugh Dickins start_page += nr_blocks; 1877992fde7SHugh Dickins nr_pages -= nr_blocks; 1887992fde7SHugh Dickins 1897992fde7SHugh Dickins if (!found_extent++) 1907992fde7SHugh Dickins si->curr_swap_extent = se; 1917992fde7SHugh Dickins 1927992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1937992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1947992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 195dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1967992fde7SHugh Dickins break; 1977992fde7SHugh Dickins } 1987992fde7SHugh Dickins 199a8ae4991SGeliang Tang se = list_next_entry(se, list); 2007992fde7SHugh Dickins } 2017992fde7SHugh Dickins } 2027992fde7SHugh Dickins 20338d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 20438d8b4e6SHuang Ying #define SWAPFILE_CLUSTER HPAGE_PMD_NR 20538d8b4e6SHuang Ying #else 206048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 20738d8b4e6SHuang Ying #endif 208048c27fdSHugh Dickins #define LATENCY_LIMIT 256 209048c27fdSHugh Dickins 2102a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2112a8f9449SShaohua Li unsigned int flag) 2122a8f9449SShaohua Li { 2132a8f9449SShaohua Li info->flags = flag; 2142a8f9449SShaohua Li } 2152a8f9449SShaohua Li 2162a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2172a8f9449SShaohua Li { 2182a8f9449SShaohua Li return info->data; 2192a8f9449SShaohua Li } 2202a8f9449SShaohua Li 2212a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2222a8f9449SShaohua Li unsigned int c) 2232a8f9449SShaohua Li { 2242a8f9449SShaohua Li info->data = c; 2252a8f9449SShaohua Li } 2262a8f9449SShaohua Li 2272a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2282a8f9449SShaohua Li unsigned int c, unsigned int f) 2292a8f9449SShaohua Li { 2302a8f9449SShaohua Li info->flags = f; 2312a8f9449SShaohua Li info->data = c; 2322a8f9449SShaohua Li } 2332a8f9449SShaohua Li 2342a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2352a8f9449SShaohua Li { 2362a8f9449SShaohua Li return info->data; 2372a8f9449SShaohua Li } 2382a8f9449SShaohua Li 2392a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2402a8f9449SShaohua Li unsigned int n) 2412a8f9449SShaohua Li { 2422a8f9449SShaohua Li info->data = n; 2432a8f9449SShaohua Li } 2442a8f9449SShaohua Li 2452a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 2462a8f9449SShaohua Li unsigned int n, unsigned int f) 2472a8f9449SShaohua Li { 2482a8f9449SShaohua Li info->flags = f; 2492a8f9449SShaohua Li info->data = n; 2502a8f9449SShaohua Li } 2512a8f9449SShaohua Li 2522a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 2532a8f9449SShaohua Li { 2542a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 2552a8f9449SShaohua Li } 2562a8f9449SShaohua Li 2572a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 2582a8f9449SShaohua Li { 2592a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 2602a8f9449SShaohua Li } 2612a8f9449SShaohua Li 2622a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 2632a8f9449SShaohua Li { 2642a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 2652a8f9449SShaohua Li info->data = 0; 2662a8f9449SShaohua Li } 2672a8f9449SShaohua Li 268235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 269235b6217SHuang, Ying unsigned long offset) 270235b6217SHuang, Ying { 271235b6217SHuang, Ying struct swap_cluster_info *ci; 272235b6217SHuang, Ying 273235b6217SHuang, Ying ci = si->cluster_info; 274235b6217SHuang, Ying if (ci) { 275235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 276235b6217SHuang, Ying spin_lock(&ci->lock); 277235b6217SHuang, Ying } 278235b6217SHuang, Ying return ci; 279235b6217SHuang, Ying } 280235b6217SHuang, Ying 281235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 282235b6217SHuang, Ying { 283235b6217SHuang, Ying if (ci) 284235b6217SHuang, Ying spin_unlock(&ci->lock); 285235b6217SHuang, Ying } 286235b6217SHuang, Ying 287235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 288235b6217SHuang, Ying struct swap_info_struct *si, 289235b6217SHuang, Ying unsigned long offset) 290235b6217SHuang, Ying { 291235b6217SHuang, Ying struct swap_cluster_info *ci; 292235b6217SHuang, Ying 293235b6217SHuang, Ying ci = lock_cluster(si, offset); 294235b6217SHuang, Ying if (!ci) 295235b6217SHuang, Ying spin_lock(&si->lock); 296235b6217SHuang, Ying 297235b6217SHuang, Ying return ci; 298235b6217SHuang, Ying } 299235b6217SHuang, Ying 300235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 301235b6217SHuang, Ying struct swap_cluster_info *ci) 302235b6217SHuang, Ying { 303235b6217SHuang, Ying if (ci) 304235b6217SHuang, Ying unlock_cluster(ci); 305235b6217SHuang, Ying else 306235b6217SHuang, Ying spin_unlock(&si->lock); 307235b6217SHuang, Ying } 308235b6217SHuang, Ying 3096b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3106b534915SHuang Ying { 3116b534915SHuang Ying return cluster_is_null(&list->head); 3126b534915SHuang Ying } 3136b534915SHuang Ying 3146b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 3156b534915SHuang Ying { 3166b534915SHuang Ying return cluster_next(&list->head); 3176b534915SHuang Ying } 3186b534915SHuang Ying 3196b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 3206b534915SHuang Ying { 3216b534915SHuang Ying cluster_set_null(&list->head); 3226b534915SHuang Ying cluster_set_null(&list->tail); 3236b534915SHuang Ying } 3246b534915SHuang Ying 3256b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 3266b534915SHuang Ying struct swap_cluster_info *ci, 3276b534915SHuang Ying unsigned int idx) 3286b534915SHuang Ying { 3296b534915SHuang Ying if (cluster_list_empty(list)) { 3306b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 3316b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3326b534915SHuang Ying } else { 333235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 3346b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 3356b534915SHuang Ying 336235b6217SHuang, Ying /* 337235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 338235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 339235b6217SHuang, Ying */ 340235b6217SHuang, Ying ci_tail = ci + tail; 341235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 342235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 3430ef017d1SHuang Ying spin_unlock(&ci_tail->lock); 3446b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3456b534915SHuang Ying } 3466b534915SHuang Ying } 3476b534915SHuang Ying 3486b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 3496b534915SHuang Ying struct swap_cluster_info *ci) 3506b534915SHuang Ying { 3516b534915SHuang Ying unsigned int idx; 3526b534915SHuang Ying 3536b534915SHuang Ying idx = cluster_next(&list->head); 3546b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 3556b534915SHuang Ying cluster_set_null(&list->head); 3566b534915SHuang Ying cluster_set_null(&list->tail); 3576b534915SHuang Ying } else 3586b534915SHuang Ying cluster_set_next_flag(&list->head, 3596b534915SHuang Ying cluster_next(&ci[idx]), 0); 3606b534915SHuang Ying 3616b534915SHuang Ying return idx; 3626b534915SHuang Ying } 3636b534915SHuang Ying 364815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 365815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 366815c2c54SShaohua Li unsigned int idx) 367815c2c54SShaohua Li { 368815c2c54SShaohua Li /* 369815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 370815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 371815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 372815c2c54SShaohua Li * will be cleared after discard 373815c2c54SShaohua Li */ 374815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 375815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 376815c2c54SShaohua Li 3776b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 378815c2c54SShaohua Li 379815c2c54SShaohua Li schedule_work(&si->discard_work); 380815c2c54SShaohua Li } 381815c2c54SShaohua Li 38238d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx) 38338d8b4e6SHuang Ying { 38438d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 38538d8b4e6SHuang Ying 38638d8b4e6SHuang Ying cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE); 38738d8b4e6SHuang Ying cluster_list_add_tail(&si->free_clusters, ci, idx); 38838d8b4e6SHuang Ying } 38938d8b4e6SHuang Ying 390815c2c54SShaohua Li /* 391815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 392815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 393815c2c54SShaohua Li */ 394815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 395815c2c54SShaohua Li { 396235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 397815c2c54SShaohua Li unsigned int idx; 398815c2c54SShaohua Li 399815c2c54SShaohua Li info = si->cluster_info; 400815c2c54SShaohua Li 4016b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 4026b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 403815c2c54SShaohua Li spin_unlock(&si->lock); 404815c2c54SShaohua Li 405815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 406815c2c54SShaohua Li SWAPFILE_CLUSTER); 407815c2c54SShaohua Li 408815c2c54SShaohua Li spin_lock(&si->lock); 409235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 41038d8b4e6SHuang Ying __free_cluster(si, idx); 411815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 412815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 413235b6217SHuang, Ying unlock_cluster(ci); 414815c2c54SShaohua Li } 415815c2c54SShaohua Li } 416815c2c54SShaohua Li 417815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 418815c2c54SShaohua Li { 419815c2c54SShaohua Li struct swap_info_struct *si; 420815c2c54SShaohua Li 421815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 422815c2c54SShaohua Li 423815c2c54SShaohua Li spin_lock(&si->lock); 424815c2c54SShaohua Li swap_do_scheduled_discard(si); 425815c2c54SShaohua Li spin_unlock(&si->lock); 426815c2c54SShaohua Li } 427815c2c54SShaohua Li 42838d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx) 42938d8b4e6SHuang Ying { 43038d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 43138d8b4e6SHuang Ying 43238d8b4e6SHuang Ying VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx); 43338d8b4e6SHuang Ying cluster_list_del_first(&si->free_clusters, ci); 43438d8b4e6SHuang Ying cluster_set_count_flag(ci + idx, 0, 0); 43538d8b4e6SHuang Ying } 43638d8b4e6SHuang Ying 43738d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx) 43838d8b4e6SHuang Ying { 43938d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info + idx; 44038d8b4e6SHuang Ying 44138d8b4e6SHuang Ying VM_BUG_ON(cluster_count(ci) != 0); 44238d8b4e6SHuang Ying /* 44338d8b4e6SHuang Ying * If the swap is discardable, prepare discard the cluster 44438d8b4e6SHuang Ying * instead of free it immediately. The cluster will be freed 44538d8b4e6SHuang Ying * after discard. 44638d8b4e6SHuang Ying */ 44738d8b4e6SHuang Ying if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 44838d8b4e6SHuang Ying (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 44938d8b4e6SHuang Ying swap_cluster_schedule_discard(si, idx); 45038d8b4e6SHuang Ying return; 45138d8b4e6SHuang Ying } 45238d8b4e6SHuang Ying 45338d8b4e6SHuang Ying __free_cluster(si, idx); 45438d8b4e6SHuang Ying } 45538d8b4e6SHuang Ying 4562a8f9449SShaohua Li /* 4572a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 4582a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 4592a8f9449SShaohua Li */ 4602a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 4612a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4622a8f9449SShaohua Li { 4632a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4642a8f9449SShaohua Li 4652a8f9449SShaohua Li if (!cluster_info) 4662a8f9449SShaohua Li return; 46738d8b4e6SHuang Ying if (cluster_is_free(&cluster_info[idx])) 46838d8b4e6SHuang Ying alloc_cluster(p, idx); 4692a8f9449SShaohua Li 4702a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 4712a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4722a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 4732a8f9449SShaohua Li } 4742a8f9449SShaohua Li 4752a8f9449SShaohua Li /* 4762a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 4772a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 4782a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 4792a8f9449SShaohua Li */ 4802a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 4812a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4822a8f9449SShaohua Li { 4832a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4842a8f9449SShaohua Li 4852a8f9449SShaohua Li if (!cluster_info) 4862a8f9449SShaohua Li return; 4872a8f9449SShaohua Li 4882a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 4892a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4902a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 4912a8f9449SShaohua Li 49238d8b4e6SHuang Ying if (cluster_count(&cluster_info[idx]) == 0) 49338d8b4e6SHuang Ying free_cluster(p, idx); 4942a8f9449SShaohua Li } 4952a8f9449SShaohua Li 4962a8f9449SShaohua Li /* 4972a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 4982a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 4992a8f9449SShaohua Li */ 500ebc2a1a6SShaohua Li static bool 501ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 5022a8f9449SShaohua Li unsigned long offset) 5032a8f9449SShaohua Li { 504ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 505ebc2a1a6SShaohua Li bool conflict; 506ebc2a1a6SShaohua Li 5072a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 5086b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 5096b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 5102a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 511ebc2a1a6SShaohua Li 512ebc2a1a6SShaohua Li if (!conflict) 513ebc2a1a6SShaohua Li return false; 514ebc2a1a6SShaohua Li 515ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 516ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 517ebc2a1a6SShaohua Li return true; 518ebc2a1a6SShaohua Li } 519ebc2a1a6SShaohua Li 520ebc2a1a6SShaohua Li /* 521ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 522ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 523ebc2a1a6SShaohua Li */ 52436005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 525ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 526ebc2a1a6SShaohua Li { 527ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 528235b6217SHuang, Ying struct swap_cluster_info *ci; 529ebc2a1a6SShaohua Li bool found_free; 530235b6217SHuang, Ying unsigned long tmp, max; 531ebc2a1a6SShaohua Li 532ebc2a1a6SShaohua Li new_cluster: 533ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 534ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 5356b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 5366b534915SHuang Ying cluster->index = si->free_clusters.head; 537ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 538ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 5396b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 540ebc2a1a6SShaohua Li /* 541ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 542ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 543ebc2a1a6SShaohua Li */ 544ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 545ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 546ebc2a1a6SShaohua Li goto new_cluster; 547ebc2a1a6SShaohua Li } else 54836005baeSTim Chen return false; 549ebc2a1a6SShaohua Li } 550ebc2a1a6SShaohua Li 551ebc2a1a6SShaohua Li found_free = false; 552ebc2a1a6SShaohua Li 553ebc2a1a6SShaohua Li /* 554ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 555ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 556ebc2a1a6SShaohua Li */ 557ebc2a1a6SShaohua Li tmp = cluster->next; 558235b6217SHuang, Ying max = min_t(unsigned long, si->max, 559235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 560235b6217SHuang, Ying if (tmp >= max) { 561235b6217SHuang, Ying cluster_set_null(&cluster->index); 562235b6217SHuang, Ying goto new_cluster; 563235b6217SHuang, Ying } 564235b6217SHuang, Ying ci = lock_cluster(si, tmp); 565235b6217SHuang, Ying while (tmp < max) { 566ebc2a1a6SShaohua Li if (!si->swap_map[tmp]) { 567ebc2a1a6SShaohua Li found_free = true; 568ebc2a1a6SShaohua Li break; 569ebc2a1a6SShaohua Li } 570ebc2a1a6SShaohua Li tmp++; 571ebc2a1a6SShaohua Li } 572235b6217SHuang, Ying unlock_cluster(ci); 573ebc2a1a6SShaohua Li if (!found_free) { 574ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 575ebc2a1a6SShaohua Li goto new_cluster; 576ebc2a1a6SShaohua Li } 577ebc2a1a6SShaohua Li cluster->next = tmp + 1; 578ebc2a1a6SShaohua Li *offset = tmp; 579ebc2a1a6SShaohua Li *scan_base = tmp; 58036005baeSTim Chen return found_free; 5812a8f9449SShaohua Li } 5822a8f9449SShaohua Li 58338d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, 58438d8b4e6SHuang Ying unsigned int nr_entries) 58538d8b4e6SHuang Ying { 58638d8b4e6SHuang Ying unsigned int end = offset + nr_entries - 1; 58738d8b4e6SHuang Ying 58838d8b4e6SHuang Ying if (offset == si->lowest_bit) 58938d8b4e6SHuang Ying si->lowest_bit += nr_entries; 59038d8b4e6SHuang Ying if (end == si->highest_bit) 59138d8b4e6SHuang Ying si->highest_bit -= nr_entries; 59238d8b4e6SHuang Ying si->inuse_pages += nr_entries; 59338d8b4e6SHuang Ying if (si->inuse_pages == si->pages) { 59438d8b4e6SHuang Ying si->lowest_bit = si->max; 59538d8b4e6SHuang Ying si->highest_bit = 0; 59638d8b4e6SHuang Ying spin_lock(&swap_avail_lock); 59738d8b4e6SHuang Ying plist_del(&si->avail_list, &swap_avail_head); 59838d8b4e6SHuang Ying spin_unlock(&swap_avail_lock); 59938d8b4e6SHuang Ying } 60038d8b4e6SHuang Ying } 60138d8b4e6SHuang Ying 60238d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset, 60338d8b4e6SHuang Ying unsigned int nr_entries) 60438d8b4e6SHuang Ying { 60538d8b4e6SHuang Ying unsigned long end = offset + nr_entries - 1; 60638d8b4e6SHuang Ying void (*swap_slot_free_notify)(struct block_device *, unsigned long); 60738d8b4e6SHuang Ying 60838d8b4e6SHuang Ying if (offset < si->lowest_bit) 60938d8b4e6SHuang Ying si->lowest_bit = offset; 61038d8b4e6SHuang Ying if (end > si->highest_bit) { 61138d8b4e6SHuang Ying bool was_full = !si->highest_bit; 61238d8b4e6SHuang Ying 61338d8b4e6SHuang Ying si->highest_bit = end; 61438d8b4e6SHuang Ying if (was_full && (si->flags & SWP_WRITEOK)) { 61538d8b4e6SHuang Ying spin_lock(&swap_avail_lock); 61638d8b4e6SHuang Ying WARN_ON(!plist_node_empty(&si->avail_list)); 61738d8b4e6SHuang Ying if (plist_node_empty(&si->avail_list)) 61838d8b4e6SHuang Ying plist_add(&si->avail_list, &swap_avail_head); 61938d8b4e6SHuang Ying spin_unlock(&swap_avail_lock); 62038d8b4e6SHuang Ying } 62138d8b4e6SHuang Ying } 62238d8b4e6SHuang Ying atomic_long_add(nr_entries, &nr_swap_pages); 62338d8b4e6SHuang Ying si->inuse_pages -= nr_entries; 62438d8b4e6SHuang Ying if (si->flags & SWP_BLKDEV) 62538d8b4e6SHuang Ying swap_slot_free_notify = 62638d8b4e6SHuang Ying si->bdev->bd_disk->fops->swap_slot_free_notify; 62738d8b4e6SHuang Ying else 62838d8b4e6SHuang Ying swap_slot_free_notify = NULL; 62938d8b4e6SHuang Ying while (offset <= end) { 63038d8b4e6SHuang Ying frontswap_invalidate_page(si->type, offset); 63138d8b4e6SHuang Ying if (swap_slot_free_notify) 63238d8b4e6SHuang Ying swap_slot_free_notify(si->bdev, offset); 63338d8b4e6SHuang Ying offset++; 63438d8b4e6SHuang Ying } 63538d8b4e6SHuang Ying } 63638d8b4e6SHuang Ying 63736005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si, 63836005baeSTim Chen unsigned char usage, int nr, 63936005baeSTim Chen swp_entry_t slots[]) 6401da177e4SLinus Torvalds { 641235b6217SHuang, Ying struct swap_cluster_info *ci; 642ebebbbe9SHugh Dickins unsigned long offset; 643c60aa176SHugh Dickins unsigned long scan_base; 6447992fde7SHugh Dickins unsigned long last_in_cluster = 0; 645048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 64636005baeSTim Chen int n_ret = 0; 64736005baeSTim Chen 64836005baeSTim Chen if (nr > SWAP_BATCH) 64936005baeSTim Chen nr = SWAP_BATCH; 6501da177e4SLinus Torvalds 6517dfad418SHugh Dickins /* 6527dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 6537dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 6547dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 6557dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 6567dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 6577dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 6587dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 659c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 6601da177e4SLinus Torvalds */ 6617dfad418SHugh Dickins 66252b7efdbSHugh Dickins si->flags += SWP_SCANNING; 663c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 664ebebbbe9SHugh Dickins 665ebc2a1a6SShaohua Li /* SSD algorithm */ 666ebc2a1a6SShaohua Li if (si->cluster_info) { 66736005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 668815c2c54SShaohua Li goto checks; 66936005baeSTim Chen else 67036005baeSTim Chen goto scan; 671815c2c54SShaohua Li } 672815c2c54SShaohua Li 673ebc2a1a6SShaohua Li if (unlikely(!si->cluster_nr--)) { 674ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 675ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 6762a8f9449SShaohua Li goto checks; 6772a8f9449SShaohua Li } 6782a8f9449SShaohua Li 679ec8acf20SShaohua Li spin_unlock(&si->lock); 6807dfad418SHugh Dickins 681c60aa176SHugh Dickins /* 682c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 683c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 68450088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 68550088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 686c60aa176SHugh Dickins */ 687c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 6887dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 6897dfad418SHugh Dickins 6907dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 6917dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 6921da177e4SLinus Torvalds if (si->swap_map[offset]) 6937dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 6947dfad418SHugh Dickins else if (offset == last_in_cluster) { 695ec8acf20SShaohua Li spin_lock(&si->lock); 696ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 697ebebbbe9SHugh Dickins si->cluster_next = offset; 698ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 699ebebbbe9SHugh Dickins goto checks; 7007dfad418SHugh Dickins } 701048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 702048c27fdSHugh Dickins cond_resched(); 703048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 704048c27fdSHugh Dickins } 7057dfad418SHugh Dickins } 706ebebbbe9SHugh Dickins 707c60aa176SHugh Dickins offset = scan_base; 708ec8acf20SShaohua Li spin_lock(&si->lock); 709ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 7107dfad418SHugh Dickins } 7117dfad418SHugh Dickins 712ebebbbe9SHugh Dickins checks: 713ebc2a1a6SShaohua Li if (si->cluster_info) { 71436005baeSTim Chen while (scan_swap_map_ssd_cluster_conflict(si, offset)) { 71536005baeSTim Chen /* take a break if we already got some slots */ 71636005baeSTim Chen if (n_ret) 71736005baeSTim Chen goto done; 71836005baeSTim Chen if (!scan_swap_map_try_ssd_cluster(si, &offset, 71936005baeSTim Chen &scan_base)) 72036005baeSTim Chen goto scan; 72136005baeSTim Chen } 722ebc2a1a6SShaohua Li } 723ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 72452b7efdbSHugh Dickins goto no_page; 7257dfad418SHugh Dickins if (!si->highest_bit) 7267dfad418SHugh Dickins goto no_page; 727ebebbbe9SHugh Dickins if (offset > si->highest_bit) 728c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 729c9e44410SKAMEZAWA Hiroyuki 730235b6217SHuang, Ying ci = lock_cluster(si, offset); 731b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 732b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 733c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 734235b6217SHuang, Ying unlock_cluster(ci); 735ec8acf20SShaohua Li spin_unlock(&si->lock); 736c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 737ec8acf20SShaohua Li spin_lock(&si->lock); 738c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 739c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 740c9e44410SKAMEZAWA Hiroyuki goto checks; 741c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 742c9e44410SKAMEZAWA Hiroyuki } 743c9e44410SKAMEZAWA Hiroyuki 744235b6217SHuang, Ying if (si->swap_map[offset]) { 745235b6217SHuang, Ying unlock_cluster(ci); 74636005baeSTim Chen if (!n_ret) 747ebebbbe9SHugh Dickins goto scan; 74836005baeSTim Chen else 74936005baeSTim Chen goto done; 750235b6217SHuang, Ying } 7512872bb2dSHuang Ying si->swap_map[offset] = usage; 7522872bb2dSHuang Ying inc_cluster_info_page(si, si->cluster_info, offset); 7532872bb2dSHuang Ying unlock_cluster(ci); 754ebebbbe9SHugh Dickins 75538d8b4e6SHuang Ying swap_range_alloc(si, offset, 1); 7561da177e4SLinus Torvalds si->cluster_next = offset + 1; 75736005baeSTim Chen slots[n_ret++] = swp_entry(si->type, offset); 7587992fde7SHugh Dickins 75936005baeSTim Chen /* got enough slots or reach max slots? */ 76036005baeSTim Chen if ((n_ret == nr) || (offset >= si->highest_bit)) 76136005baeSTim Chen goto done; 76236005baeSTim Chen 76336005baeSTim Chen /* search for next available slot */ 76436005baeSTim Chen 76536005baeSTim Chen /* time to take a break? */ 76636005baeSTim Chen if (unlikely(--latency_ration < 0)) { 76736005baeSTim Chen if (n_ret) 76836005baeSTim Chen goto done; 76936005baeSTim Chen spin_unlock(&si->lock); 77036005baeSTim Chen cond_resched(); 77136005baeSTim Chen spin_lock(&si->lock); 77236005baeSTim Chen latency_ration = LATENCY_LIMIT; 77336005baeSTim Chen } 77436005baeSTim Chen 77536005baeSTim Chen /* try to get more slots in cluster */ 77636005baeSTim Chen if (si->cluster_info) { 77736005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 77836005baeSTim Chen goto checks; 77936005baeSTim Chen else 78036005baeSTim Chen goto done; 78136005baeSTim Chen } 78236005baeSTim Chen /* non-ssd case */ 78336005baeSTim Chen ++offset; 78436005baeSTim Chen 78536005baeSTim Chen /* non-ssd case, still more slots in cluster? */ 78636005baeSTim Chen if (si->cluster_nr && !si->swap_map[offset]) { 78736005baeSTim Chen --si->cluster_nr; 78836005baeSTim Chen goto checks; 78936005baeSTim Chen } 79036005baeSTim Chen 79136005baeSTim Chen done: 79236005baeSTim Chen si->flags -= SWP_SCANNING; 79336005baeSTim Chen return n_ret; 7947dfad418SHugh Dickins 795ebebbbe9SHugh Dickins scan: 796ec8acf20SShaohua Li spin_unlock(&si->lock); 7977dfad418SHugh Dickins while (++offset <= si->highest_bit) { 79852b7efdbSHugh Dickins if (!si->swap_map[offset]) { 799ec8acf20SShaohua Li spin_lock(&si->lock); 80052b7efdbSHugh Dickins goto checks; 8017dfad418SHugh Dickins } 802c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 803ec8acf20SShaohua Li spin_lock(&si->lock); 804c9e44410SKAMEZAWA Hiroyuki goto checks; 805c9e44410SKAMEZAWA Hiroyuki } 806048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 807048c27fdSHugh Dickins cond_resched(); 808048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 809048c27fdSHugh Dickins } 81052b7efdbSHugh Dickins } 811c60aa176SHugh Dickins offset = si->lowest_bit; 812a5998061SJamie Liu while (offset < scan_base) { 813c60aa176SHugh Dickins if (!si->swap_map[offset]) { 814ec8acf20SShaohua Li spin_lock(&si->lock); 815ebebbbe9SHugh Dickins goto checks; 816c60aa176SHugh Dickins } 817c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 818ec8acf20SShaohua Li spin_lock(&si->lock); 819c9e44410SKAMEZAWA Hiroyuki goto checks; 820c9e44410SKAMEZAWA Hiroyuki } 821c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 822c60aa176SHugh Dickins cond_resched(); 823c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 824c60aa176SHugh Dickins } 825a5998061SJamie Liu offset++; 826c60aa176SHugh Dickins } 827ec8acf20SShaohua Li spin_lock(&si->lock); 8287dfad418SHugh Dickins 8297dfad418SHugh Dickins no_page: 83052b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 83136005baeSTim Chen return n_ret; 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 83438d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 83538d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 83638d8b4e6SHuang Ying { 83738d8b4e6SHuang Ying unsigned long idx; 83838d8b4e6SHuang Ying struct swap_cluster_info *ci; 83938d8b4e6SHuang Ying unsigned long offset, i; 84038d8b4e6SHuang Ying unsigned char *map; 84138d8b4e6SHuang Ying 84238d8b4e6SHuang Ying if (cluster_list_empty(&si->free_clusters)) 84338d8b4e6SHuang Ying return 0; 84438d8b4e6SHuang Ying 84538d8b4e6SHuang Ying idx = cluster_list_first(&si->free_clusters); 84638d8b4e6SHuang Ying offset = idx * SWAPFILE_CLUSTER; 84738d8b4e6SHuang Ying ci = lock_cluster(si, offset); 84838d8b4e6SHuang Ying alloc_cluster(si, idx); 84938d8b4e6SHuang Ying cluster_set_count_flag(ci, SWAPFILE_CLUSTER, 0); 85038d8b4e6SHuang Ying 85138d8b4e6SHuang Ying map = si->swap_map + offset; 85238d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 85338d8b4e6SHuang Ying map[i] = SWAP_HAS_CACHE; 85438d8b4e6SHuang Ying unlock_cluster(ci); 85538d8b4e6SHuang Ying swap_range_alloc(si, offset, SWAPFILE_CLUSTER); 85638d8b4e6SHuang Ying *slot = swp_entry(si->type, offset); 85738d8b4e6SHuang Ying 85838d8b4e6SHuang Ying return 1; 85938d8b4e6SHuang Ying } 86038d8b4e6SHuang Ying 86138d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) 86238d8b4e6SHuang Ying { 86338d8b4e6SHuang Ying unsigned long offset = idx * SWAPFILE_CLUSTER; 86438d8b4e6SHuang Ying struct swap_cluster_info *ci; 86538d8b4e6SHuang Ying 86638d8b4e6SHuang Ying ci = lock_cluster(si, offset); 86738d8b4e6SHuang Ying cluster_set_count_flag(ci, 0, 0); 86838d8b4e6SHuang Ying free_cluster(si, idx); 86938d8b4e6SHuang Ying unlock_cluster(ci); 87038d8b4e6SHuang Ying swap_range_free(si, offset, SWAPFILE_CLUSTER); 87138d8b4e6SHuang Ying } 87238d8b4e6SHuang Ying #else 87338d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 87438d8b4e6SHuang Ying { 87538d8b4e6SHuang Ying VM_WARN_ON_ONCE(1); 87638d8b4e6SHuang Ying return 0; 87738d8b4e6SHuang Ying } 87838d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 87938d8b4e6SHuang Ying 88036005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si, 88136005baeSTim Chen unsigned char usage) 88236005baeSTim Chen { 88336005baeSTim Chen swp_entry_t entry; 88436005baeSTim Chen int n_ret; 88536005baeSTim Chen 88636005baeSTim Chen n_ret = scan_swap_map_slots(si, usage, 1, &entry); 88736005baeSTim Chen 88836005baeSTim Chen if (n_ret) 88936005baeSTim Chen return swp_offset(entry); 89036005baeSTim Chen else 89136005baeSTim Chen return 0; 89236005baeSTim Chen 89336005baeSTim Chen } 89436005baeSTim Chen 89538d8b4e6SHuang Ying int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[]) 8961da177e4SLinus Torvalds { 89738d8b4e6SHuang Ying unsigned long nr_pages = cluster ? SWAPFILE_CLUSTER : 1; 898adfab836SDan Streetman struct swap_info_struct *si, *next; 89936005baeSTim Chen long avail_pgs; 90036005baeSTim Chen int n_ret = 0; 9011da177e4SLinus Torvalds 90238d8b4e6SHuang Ying /* Only single cluster request supported */ 90338d8b4e6SHuang Ying WARN_ON_ONCE(n_goal > 1 && cluster); 90438d8b4e6SHuang Ying 90538d8b4e6SHuang Ying avail_pgs = atomic_long_read(&nr_swap_pages) / nr_pages; 90636005baeSTim Chen if (avail_pgs <= 0) 907fb4f88dcSHugh Dickins goto noswap; 90836005baeSTim Chen 90936005baeSTim Chen if (n_goal > SWAP_BATCH) 91036005baeSTim Chen n_goal = SWAP_BATCH; 91136005baeSTim Chen 91236005baeSTim Chen if (n_goal > avail_pgs) 91336005baeSTim Chen n_goal = avail_pgs; 91436005baeSTim Chen 91538d8b4e6SHuang Ying atomic_long_sub(n_goal * nr_pages, &nr_swap_pages); 9161da177e4SLinus Torvalds 91718ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 91818ab4d4cSDan Streetman 91918ab4d4cSDan Streetman start_over: 92018ab4d4cSDan Streetman plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { 92118ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 92218ab4d4cSDan Streetman plist_requeue(&si->avail_list, &swap_avail_head); 92318ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 924ec8acf20SShaohua Li spin_lock(&si->lock); 925adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 92618ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 92718ab4d4cSDan Streetman if (plist_node_empty(&si->avail_list)) { 928ec8acf20SShaohua Li spin_unlock(&si->lock); 92918ab4d4cSDan Streetman goto nextsi; 93018ab4d4cSDan Streetman } 93118ab4d4cSDan Streetman WARN(!si->highest_bit, 93218ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 93318ab4d4cSDan Streetman si->type); 93418ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 93518ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 93618ab4d4cSDan Streetman si->type); 93718ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 93818ab4d4cSDan Streetman spin_unlock(&si->lock); 93918ab4d4cSDan Streetman goto nextsi; 940ec8acf20SShaohua Li } 94138d8b4e6SHuang Ying if (cluster) 94238d8b4e6SHuang Ying n_ret = swap_alloc_cluster(si, swp_entries); 94338d8b4e6SHuang Ying else 94436005baeSTim Chen n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, 94536005baeSTim Chen n_goal, swp_entries); 946ec8acf20SShaohua Li spin_unlock(&si->lock); 94738d8b4e6SHuang Ying if (n_ret || cluster) 94836005baeSTim Chen goto check_out; 94918ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 95018ab4d4cSDan Streetman si->type); 95136005baeSTim Chen 95218ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 95318ab4d4cSDan Streetman nextsi: 954adfab836SDan Streetman /* 955adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 956adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 957adfab836SDan Streetman * callers probably all tried to get a page from the same si 95818ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 95918ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 96018ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 96118ab4d4cSDan Streetman * list may have been modified; so if next is still in the 96236005baeSTim Chen * swap_avail_head list then try it, otherwise start over 96336005baeSTim Chen * if we have not gotten any slots. 964adfab836SDan Streetman */ 96518ab4d4cSDan Streetman if (plist_node_empty(&next->avail_list)) 96618ab4d4cSDan Streetman goto start_over; 967fb4f88dcSHugh Dickins } 968fb4f88dcSHugh Dickins 96918ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 97018ab4d4cSDan Streetman 97136005baeSTim Chen check_out: 97236005baeSTim Chen if (n_ret < n_goal) 97338d8b4e6SHuang Ying atomic_long_add((long)(n_goal - n_ret) * nr_pages, 97438d8b4e6SHuang Ying &nr_swap_pages); 975fb4f88dcSHugh Dickins noswap: 97636005baeSTim Chen return n_ret; 97736005baeSTim Chen } 97836005baeSTim Chen 9792de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 980910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 981910321eaSHugh Dickins { 982910321eaSHugh Dickins struct swap_info_struct *si; 983910321eaSHugh Dickins pgoff_t offset; 984910321eaSHugh Dickins 985910321eaSHugh Dickins si = swap_info[type]; 986ec8acf20SShaohua Li spin_lock(&si->lock); 987910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 988ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 989910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 990910321eaSHugh Dickins offset = scan_swap_map(si, 1); 991910321eaSHugh Dickins if (offset) { 992ec8acf20SShaohua Li spin_unlock(&si->lock); 993910321eaSHugh Dickins return swp_entry(type, offset); 994910321eaSHugh Dickins } 995ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 996910321eaSHugh Dickins } 997ec8acf20SShaohua Li spin_unlock(&si->lock); 998910321eaSHugh Dickins return (swp_entry_t) {0}; 999910321eaSHugh Dickins } 1000910321eaSHugh Dickins 1001e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry) 10021da177e4SLinus Torvalds { 10031da177e4SLinus Torvalds struct swap_info_struct *p; 10041da177e4SLinus Torvalds unsigned long offset, type; 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds if (!entry.val) 10071da177e4SLinus Torvalds goto out; 10081da177e4SLinus Torvalds type = swp_type(entry); 10091da177e4SLinus Torvalds if (type >= nr_swapfiles) 10101da177e4SLinus Torvalds goto bad_nofile; 1011efa90a98SHugh Dickins p = swap_info[type]; 10121da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 10131da177e4SLinus Torvalds goto bad_device; 10141da177e4SLinus Torvalds offset = swp_offset(entry); 10151da177e4SLinus Torvalds if (offset >= p->max) 10161da177e4SLinus Torvalds goto bad_offset; 10171da177e4SLinus Torvalds return p; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds bad_offset: 10206a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val); 10211da177e4SLinus Torvalds goto out; 10221da177e4SLinus Torvalds bad_device: 10236a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val); 10241da177e4SLinus Torvalds goto out; 10251da177e4SLinus Torvalds bad_nofile: 10266a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val); 10271da177e4SLinus Torvalds out: 10281da177e4SLinus Torvalds return NULL; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 1031e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 1032e8c26ab6STim Chen { 1033e8c26ab6STim Chen struct swap_info_struct *p; 1034e8c26ab6STim Chen 1035e8c26ab6STim Chen p = __swap_info_get(entry); 1036e8c26ab6STim Chen if (!p) 1037e8c26ab6STim Chen goto out; 1038e8c26ab6STim Chen if (!p->swap_map[swp_offset(entry)]) 1039e8c26ab6STim Chen goto bad_free; 1040e8c26ab6STim Chen return p; 1041e8c26ab6STim Chen 1042e8c26ab6STim Chen bad_free: 1043e8c26ab6STim Chen pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val); 1044e8c26ab6STim Chen goto out; 1045e8c26ab6STim Chen out: 1046e8c26ab6STim Chen return NULL; 1047e8c26ab6STim Chen } 1048e8c26ab6STim Chen 1049235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 10501da177e4SLinus Torvalds { 1051235b6217SHuang, Ying struct swap_info_struct *p; 1052235b6217SHuang, Ying 1053235b6217SHuang, Ying p = _swap_info_get(entry); 1054235b6217SHuang, Ying if (p) 1055235b6217SHuang, Ying spin_lock(&p->lock); 1056235b6217SHuang, Ying return p; 1057235b6217SHuang, Ying } 1058235b6217SHuang, Ying 10597c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, 10607c00bafeSTim Chen struct swap_info_struct *q) 10617c00bafeSTim Chen { 10627c00bafeSTim Chen struct swap_info_struct *p; 10637c00bafeSTim Chen 10647c00bafeSTim Chen p = _swap_info_get(entry); 10657c00bafeSTim Chen 10667c00bafeSTim Chen if (p != q) { 10677c00bafeSTim Chen if (q != NULL) 10687c00bafeSTim Chen spin_unlock(&q->lock); 10697c00bafeSTim Chen if (p != NULL) 10707c00bafeSTim Chen spin_lock(&p->lock); 10717c00bafeSTim Chen } 10727c00bafeSTim Chen return p; 10737c00bafeSTim Chen } 10747c00bafeSTim Chen 10757c00bafeSTim Chen static unsigned char __swap_entry_free(struct swap_info_struct *p, 10767c00bafeSTim Chen swp_entry_t entry, unsigned char usage) 1077235b6217SHuang, Ying { 1078235b6217SHuang, Ying struct swap_cluster_info *ci; 1079253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 10808d69aaeeSHugh Dickins unsigned char count; 10818d69aaeeSHugh Dickins unsigned char has_cache; 1082235b6217SHuang, Ying 10837c00bafeSTim Chen ci = lock_cluster_or_swap_info(p, offset); 10841da177e4SLinus Torvalds 1085355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 1086235b6217SHuang, Ying 1087253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 1088253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 1089253d553bSHugh Dickins 1090253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 1091253d553bSHugh Dickins VM_BUG_ON(!has_cache); 1092253d553bSHugh Dickins has_cache = 0; 1093aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 1094aaa46865SHugh Dickins /* 1095aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 1096aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 1097aaa46865SHugh Dickins */ 1098aaa46865SHugh Dickins count = 0; 1099570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 1100570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 1101570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 1102570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 1103570a335bSHugh Dickins else 1104570a335bSHugh Dickins count = SWAP_MAP_MAX; 1105570a335bSHugh Dickins } else 1106253d553bSHugh Dickins count--; 1107570a335bSHugh Dickins } 1108253d553bSHugh Dickins 1109253d553bSHugh Dickins usage = count | has_cache; 11107c00bafeSTim Chen p->swap_map[offset] = usage ? : SWAP_HAS_CACHE; 1111253d553bSHugh Dickins 11127c00bafeSTim Chen unlock_cluster_or_swap_info(p, ci); 1113235b6217SHuang, Ying 11147c00bafeSTim Chen return usage; 11157c00bafeSTim Chen } 11167c00bafeSTim Chen 11177c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) 11187c00bafeSTim Chen { 11197c00bafeSTim Chen struct swap_cluster_info *ci; 11207c00bafeSTim Chen unsigned long offset = swp_offset(entry); 11217c00bafeSTim Chen unsigned char count; 11227c00bafeSTim Chen 1123235b6217SHuang, Ying ci = lock_cluster(p, offset); 11247c00bafeSTim Chen count = p->swap_map[offset]; 11257c00bafeSTim Chen VM_BUG_ON(count != SWAP_HAS_CACHE); 11267c00bafeSTim Chen p->swap_map[offset] = 0; 11272a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 1128235b6217SHuang, Ying unlock_cluster(ci); 11297c00bafeSTim Chen 113038d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, 1); 113138d8b4e6SHuang Ying swap_range_free(p, offset, 1); 11321da177e4SLinus Torvalds } 1133253d553bSHugh Dickins 11341da177e4SLinus Torvalds /* 11351da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 11361da177e4SLinus Torvalds * is still around or has not been recycled. 11371da177e4SLinus Torvalds */ 11381da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 11391da177e4SLinus Torvalds { 11401da177e4SLinus Torvalds struct swap_info_struct *p; 11411da177e4SLinus Torvalds 1142235b6217SHuang, Ying p = _swap_info_get(entry); 11437c00bafeSTim Chen if (p) { 11447c00bafeSTim Chen if (!__swap_entry_free(p, entry, 1)) 114567afa38eSTim Chen free_swap_slot(entry); 11467c00bafeSTim Chen } 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds /* 1150cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 1151cb4b86baSKAMEZAWA Hiroyuki */ 115275f6d6d2SMinchan Kim static void swapcache_free(swp_entry_t entry) 1153cb4b86baSKAMEZAWA Hiroyuki { 1154355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 1155355cfa73SKAMEZAWA Hiroyuki 1156235b6217SHuang, Ying p = _swap_info_get(entry); 11577c00bafeSTim Chen if (p) { 11587c00bafeSTim Chen if (!__swap_entry_free(p, entry, SWAP_HAS_CACHE)) 115967afa38eSTim Chen free_swap_slot(entry); 11607c00bafeSTim Chen } 11617c00bafeSTim Chen } 11627c00bafeSTim Chen 116338d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 116475f6d6d2SMinchan Kim static void swapcache_free_cluster(swp_entry_t entry) 116538d8b4e6SHuang Ying { 116638d8b4e6SHuang Ying unsigned long offset = swp_offset(entry); 116738d8b4e6SHuang Ying unsigned long idx = offset / SWAPFILE_CLUSTER; 116838d8b4e6SHuang Ying struct swap_cluster_info *ci; 116938d8b4e6SHuang Ying struct swap_info_struct *si; 117038d8b4e6SHuang Ying unsigned char *map; 1171*a3aea839SHuang Ying unsigned int i, free_entries = 0; 1172*a3aea839SHuang Ying unsigned char val; 117338d8b4e6SHuang Ying 1174*a3aea839SHuang Ying si = _swap_info_get(entry); 117538d8b4e6SHuang Ying if (!si) 117638d8b4e6SHuang Ying return; 117738d8b4e6SHuang Ying 117838d8b4e6SHuang Ying ci = lock_cluster(si, offset); 117938d8b4e6SHuang Ying map = si->swap_map + offset; 118038d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1181*a3aea839SHuang Ying val = map[i]; 1182*a3aea839SHuang Ying VM_BUG_ON(!(val & SWAP_HAS_CACHE)); 1183*a3aea839SHuang Ying if (val == SWAP_HAS_CACHE) 1184*a3aea839SHuang Ying free_entries++; 118538d8b4e6SHuang Ying } 1186*a3aea839SHuang Ying if (!free_entries) { 1187*a3aea839SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 1188*a3aea839SHuang Ying map[i] &= ~SWAP_HAS_CACHE; 1189*a3aea839SHuang Ying } 1190*a3aea839SHuang Ying unlock_cluster(ci); 1191*a3aea839SHuang Ying if (free_entries == SWAPFILE_CLUSTER) { 1192*a3aea839SHuang Ying spin_lock(&si->lock); 1193*a3aea839SHuang Ying ci = lock_cluster(si, offset); 1194*a3aea839SHuang Ying memset(map, 0, SWAPFILE_CLUSTER); 119538d8b4e6SHuang Ying unlock_cluster(ci); 119638d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER); 119738d8b4e6SHuang Ying swap_free_cluster(si, idx); 119838d8b4e6SHuang Ying spin_unlock(&si->lock); 1199*a3aea839SHuang Ying } else if (free_entries) { 1200*a3aea839SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++, entry.val++) { 1201*a3aea839SHuang Ying if (!__swap_entry_free(si, entry, SWAP_HAS_CACHE)) 1202*a3aea839SHuang Ying free_swap_slot(entry); 1203*a3aea839SHuang Ying } 1204*a3aea839SHuang Ying } 120538d8b4e6SHuang Ying } 120675f6d6d2SMinchan Kim #else 120775f6d6d2SMinchan Kim static inline void swapcache_free_cluster(swp_entry_t entry) 120875f6d6d2SMinchan Kim { 120975f6d6d2SMinchan Kim } 121038d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 121138d8b4e6SHuang Ying 121275f6d6d2SMinchan Kim void put_swap_page(struct page *page, swp_entry_t entry) 121375f6d6d2SMinchan Kim { 121475f6d6d2SMinchan Kim if (!PageTransHuge(page)) 121575f6d6d2SMinchan Kim swapcache_free(entry); 121675f6d6d2SMinchan Kim else 121775f6d6d2SMinchan Kim swapcache_free_cluster(entry); 121875f6d6d2SMinchan Kim } 121975f6d6d2SMinchan Kim 1220155b5f88SHuang Ying static int swp_entry_cmp(const void *ent1, const void *ent2) 1221155b5f88SHuang Ying { 1222155b5f88SHuang Ying const swp_entry_t *e1 = ent1, *e2 = ent2; 1223155b5f88SHuang Ying 1224155b5f88SHuang Ying return (int)swp_type(*e1) - (int)swp_type(*e2); 1225155b5f88SHuang Ying } 1226155b5f88SHuang Ying 12277c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n) 12287c00bafeSTim Chen { 12297c00bafeSTim Chen struct swap_info_struct *p, *prev; 12307c00bafeSTim Chen int i; 12317c00bafeSTim Chen 12327c00bafeSTim Chen if (n <= 0) 12337c00bafeSTim Chen return; 12347c00bafeSTim Chen 12357c00bafeSTim Chen prev = NULL; 12367c00bafeSTim Chen p = NULL; 1237155b5f88SHuang Ying 1238155b5f88SHuang Ying /* 1239155b5f88SHuang Ying * Sort swap entries by swap device, so each lock is only taken once. 1240155b5f88SHuang Ying * nr_swapfiles isn't absolutely correct, but the overhead of sort() is 1241155b5f88SHuang Ying * so low that it isn't necessary to optimize further. 1242155b5f88SHuang Ying */ 1243155b5f88SHuang Ying if (nr_swapfiles > 1) 1244155b5f88SHuang Ying sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); 12457c00bafeSTim Chen for (i = 0; i < n; ++i) { 12467c00bafeSTim Chen p = swap_info_get_cont(entries[i], prev); 1247235b6217SHuang, Ying if (p) 12487c00bafeSTim Chen swap_entry_free(p, entries[i]); 12497c00bafeSTim Chen prev = p; 12507c00bafeSTim Chen } 12517c00bafeSTim Chen if (p) 12527c00bafeSTim Chen spin_unlock(&p->lock); 1253cb4b86baSKAMEZAWA Hiroyuki } 1254cb4b86baSKAMEZAWA Hiroyuki 1255cb4b86baSKAMEZAWA Hiroyuki /* 1256c475a8abSHugh Dickins * How many references to page are currently swapped out? 1257570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 1258570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 12591da177e4SLinus Torvalds */ 1260bde05d1cSHugh Dickins int page_swapcount(struct page *page) 12611da177e4SLinus Torvalds { 1262c475a8abSHugh Dickins int count = 0; 12631da177e4SLinus Torvalds struct swap_info_struct *p; 1264235b6217SHuang, Ying struct swap_cluster_info *ci; 12651da177e4SLinus Torvalds swp_entry_t entry; 1266235b6217SHuang, Ying unsigned long offset; 12671da177e4SLinus Torvalds 12684c21e2f2SHugh Dickins entry.val = page_private(page); 1269235b6217SHuang, Ying p = _swap_info_get(entry); 12701da177e4SLinus Torvalds if (p) { 1271235b6217SHuang, Ying offset = swp_offset(entry); 1272235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1273235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 1274235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 12751da177e4SLinus Torvalds } 1276c475a8abSHugh Dickins return count; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 1279322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) 1280322b8afeSHuang Ying { 1281322b8afeSHuang Ying int count = 0; 1282322b8afeSHuang Ying pgoff_t offset = swp_offset(entry); 1283322b8afeSHuang Ying struct swap_cluster_info *ci; 1284322b8afeSHuang Ying 1285322b8afeSHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1286322b8afeSHuang Ying count = swap_count(si->swap_map[offset]); 1287322b8afeSHuang Ying unlock_cluster_or_swap_info(si, ci); 1288322b8afeSHuang Ying return count; 1289322b8afeSHuang Ying } 1290322b8afeSHuang Ying 12911da177e4SLinus Torvalds /* 12928334b962SMinchan Kim * How many references to @entry are currently swapped out? 1293e8c26ab6STim Chen * This does not give an exact answer when swap count is continued, 1294e8c26ab6STim Chen * but does include the high COUNT_CONTINUED flag to allow for that. 1295e8c26ab6STim Chen */ 1296e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry) 1297e8c26ab6STim Chen { 1298e8c26ab6STim Chen int count = 0; 1299e8c26ab6STim Chen struct swap_info_struct *si; 1300e8c26ab6STim Chen 1301e8c26ab6STim Chen si = __swap_info_get(entry); 1302322b8afeSHuang Ying if (si) 1303322b8afeSHuang Ying count = swap_swapcount(si, entry); 1304e8c26ab6STim Chen return count; 1305e8c26ab6STim Chen } 1306e8c26ab6STim Chen 1307e8c26ab6STim Chen /* 1308e8c26ab6STim Chen * How many references to @entry are currently swapped out? 13098334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 13108334b962SMinchan Kim */ 13118334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 13128334b962SMinchan Kim { 13138334b962SMinchan Kim int count, tmp_count, n; 13148334b962SMinchan Kim struct swap_info_struct *p; 1315235b6217SHuang, Ying struct swap_cluster_info *ci; 13168334b962SMinchan Kim struct page *page; 13178334b962SMinchan Kim pgoff_t offset; 13188334b962SMinchan Kim unsigned char *map; 13198334b962SMinchan Kim 1320235b6217SHuang, Ying p = _swap_info_get(entry); 13218334b962SMinchan Kim if (!p) 13228334b962SMinchan Kim return 0; 13238334b962SMinchan Kim 1324235b6217SHuang, Ying offset = swp_offset(entry); 1325235b6217SHuang, Ying 1326235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1327235b6217SHuang, Ying 1328235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 13298334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 13308334b962SMinchan Kim goto out; 13318334b962SMinchan Kim 13328334b962SMinchan Kim count &= ~COUNT_CONTINUED; 13338334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 13348334b962SMinchan Kim 13358334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 13368334b962SMinchan Kim offset &= ~PAGE_MASK; 13378334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 13388334b962SMinchan Kim 13398334b962SMinchan Kim do { 1340a8ae4991SGeliang Tang page = list_next_entry(page, lru); 13418334b962SMinchan Kim map = kmap_atomic(page); 13428334b962SMinchan Kim tmp_count = map[offset]; 13438334b962SMinchan Kim kunmap_atomic(map); 13448334b962SMinchan Kim 13458334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 13468334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 13478334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 13488334b962SMinchan Kim out: 1349235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 13508334b962SMinchan Kim return count; 13518334b962SMinchan Kim } 13528334b962SMinchan Kim 13538334b962SMinchan Kim /* 13547b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 13557b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 13567b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 13577b1fe597SHugh Dickins * later would only waste time away from clustering. 13586d0a07edSAndrea Arcangeli * 13596d0a07edSAndrea Arcangeli * NOTE: total_mapcount should not be relied upon by the caller if 13606d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 13616d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 13621da177e4SLinus Torvalds */ 13636d0a07edSAndrea Arcangeli bool reuse_swap_page(struct page *page, int *total_mapcount) 13641da177e4SLinus Torvalds { 1365c475a8abSHugh Dickins int count; 13661da177e4SLinus Torvalds 1367309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 13685ad64688SHugh Dickins if (unlikely(PageKsm(page))) 13696d0a07edSAndrea Arcangeli return false; 13706d0a07edSAndrea Arcangeli count = page_trans_huge_mapcount(page, total_mapcount); 13717b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 1372c475a8abSHugh Dickins count += page_swapcount(page); 1373f0571429SMinchan Kim if (count != 1) 1374f0571429SMinchan Kim goto out; 1375f0571429SMinchan Kim if (!PageWriteback(page)) { 13767b1fe597SHugh Dickins delete_from_swap_cache(page); 13777b1fe597SHugh Dickins SetPageDirty(page); 1378f0571429SMinchan Kim } else { 1379f0571429SMinchan Kim swp_entry_t entry; 1380f0571429SMinchan Kim struct swap_info_struct *p; 1381f0571429SMinchan Kim 1382f0571429SMinchan Kim entry.val = page_private(page); 1383f0571429SMinchan Kim p = swap_info_get(entry); 1384f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1385f0571429SMinchan Kim spin_unlock(&p->lock); 1386f0571429SMinchan Kim return false; 1387f0571429SMinchan Kim } 1388f0571429SMinchan Kim spin_unlock(&p->lock); 13897b1fe597SHugh Dickins } 13907b1fe597SHugh Dickins } 1391f0571429SMinchan Kim out: 13925ad64688SHugh Dickins return count <= 1; 13931da177e4SLinus Torvalds } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds /* 1396a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1397a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 13981da177e4SLinus Torvalds */ 1399a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 14001da177e4SLinus Torvalds { 1401309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds if (!PageSwapCache(page)) 14041da177e4SLinus Torvalds return 0; 14051da177e4SLinus Torvalds if (PageWriteback(page)) 14061da177e4SLinus Torvalds return 0; 1407a2c43eedSHugh Dickins if (page_swapcount(page)) 14081da177e4SLinus Torvalds return 0; 14091da177e4SLinus Torvalds 1410b73d7fceSHugh Dickins /* 1411b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1412b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1413b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1414b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1415b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1416b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1417b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1418b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1419b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1420b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1421b73d7fceSHugh Dickins * 14222de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1423f90ac398SMel Gorman * to disk so check that here. 1424b73d7fceSHugh Dickins */ 1425f90ac398SMel Gorman if (pm_suspended_storage()) 1426b73d7fceSHugh Dickins return 0; 1427b73d7fceSHugh Dickins 1428a2c43eedSHugh Dickins delete_from_swap_cache(page); 14291da177e4SLinus Torvalds SetPageDirty(page); 1430a2c43eedSHugh Dickins return 1; 143168a22394SRik van Riel } 143268a22394SRik van Riel 143368a22394SRik van Riel /* 14341da177e4SLinus Torvalds * Free the swap entry like above, but also try to 14351da177e4SLinus Torvalds * free the page cache entry if it is the last user. 14361da177e4SLinus Torvalds */ 14372509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds struct swap_info_struct *p; 14401da177e4SLinus Torvalds struct page *page = NULL; 14417c00bafeSTim Chen unsigned char count; 14421da177e4SLinus Torvalds 1443a7420aa5SAndi Kleen if (non_swap_entry(entry)) 14442509ef26SHugh Dickins return 1; 14450697212aSChristoph Lameter 14467c00bafeSTim Chen p = _swap_info_get(entry); 14471da177e4SLinus Torvalds if (p) { 14487c00bafeSTim Chen count = __swap_entry_free(p, entry, 1); 14497c00bafeSTim Chen if (count == SWAP_HAS_CACHE) { 145033806f06SShaohua Li page = find_get_page(swap_address_space(entry), 1451f6ab1f7fSHuang Ying swp_offset(entry)); 14528413ac9dSNick Piggin if (page && !trylock_page(page)) { 145309cbfeafSKirill A. Shutemov put_page(page); 145493fac704SNick Piggin page = NULL; 145593fac704SNick Piggin } 14567c00bafeSTim Chen } else if (!count) 145767afa38eSTim Chen free_swap_slot(entry); 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds if (page) { 1460a2c43eedSHugh Dickins /* 1461a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 1462a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 1463a2c43eedSHugh Dickins */ 146493fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 1465322b8afeSHuang Ying (!page_mapped(page) || mem_cgroup_swap_full(page)) && 1466322b8afeSHuang Ying !swap_swapcount(p, entry)) { 14671da177e4SLinus Torvalds delete_from_swap_cache(page); 14681da177e4SLinus Torvalds SetPageDirty(page); 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds unlock_page(page); 147109cbfeafSKirill A. Shutemov put_page(page); 14721da177e4SLinus Torvalds } 14732509ef26SHugh Dickins return p != NULL; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 1476b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1477f577eb30SRafael J. Wysocki /* 1478915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1479f577eb30SRafael J. Wysocki * 1480915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1481915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1482915bae9eSRafael J. Wysocki * 1483915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1484f577eb30SRafael J. Wysocki */ 14857bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 1486f577eb30SRafael J. Wysocki { 1487915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 1488efa90a98SHugh Dickins int type; 1489f577eb30SRafael J. Wysocki 1490915bae9eSRafael J. Wysocki if (device) 1491915bae9eSRafael J. Wysocki bdev = bdget(device); 1492915bae9eSRafael J. Wysocki 1493f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1494efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1495efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1496f577eb30SRafael J. Wysocki 1497915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1498f577eb30SRafael J. Wysocki continue; 1499b6b5bce3SRafael J. Wysocki 1500915bae9eSRafael J. Wysocki if (!bdev) { 15017bf23687SRafael J. Wysocki if (bdev_p) 1502dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 15037bf23687SRafael J. Wysocki 15046e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1505efa90a98SHugh Dickins return type; 15066e1819d6SRafael J. Wysocki } 1507915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 15089625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 1509915bae9eSRafael J. Wysocki 1510915bae9eSRafael J. Wysocki if (se->start_block == offset) { 15117bf23687SRafael J. Wysocki if (bdev_p) 1512dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 15137bf23687SRafael J. Wysocki 1514f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1515915bae9eSRafael J. Wysocki bdput(bdev); 1516efa90a98SHugh Dickins return type; 1517f577eb30SRafael J. Wysocki } 1518f577eb30SRafael J. Wysocki } 1519915bae9eSRafael J. Wysocki } 1520f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1521915bae9eSRafael J. Wysocki if (bdev) 1522915bae9eSRafael J. Wysocki bdput(bdev); 1523915bae9eSRafael J. Wysocki 1524f577eb30SRafael J. Wysocki return -ENODEV; 1525f577eb30SRafael J. Wysocki } 1526f577eb30SRafael J. Wysocki 1527f577eb30SRafael J. Wysocki /* 152873c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 152973c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 153073c34b6aSHugh Dickins */ 153173c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 153273c34b6aSHugh Dickins { 153373c34b6aSHugh Dickins struct block_device *bdev; 153473c34b6aSHugh Dickins 153573c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 153673c34b6aSHugh Dickins return 0; 153773c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 153873c34b6aSHugh Dickins return 0; 1539d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 154073c34b6aSHugh Dickins } 154173c34b6aSHugh Dickins 154273c34b6aSHugh Dickins /* 1543f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1544f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1545f577eb30SRafael J. Wysocki * 1546f577eb30SRafael J. Wysocki * This is needed for software suspend 1547f577eb30SRafael J. Wysocki */ 1548f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1549f577eb30SRafael J. Wysocki { 1550f577eb30SRafael J. Wysocki unsigned int n = 0; 1551f577eb30SRafael J. Wysocki 1552f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1553efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1554efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1555efa90a98SHugh Dickins 1556ec8acf20SShaohua Li spin_lock(&sis->lock); 1557efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1558efa90a98SHugh Dickins n = sis->pages; 1559f577eb30SRafael J. Wysocki if (free) 1560efa90a98SHugh Dickins n -= sis->inuse_pages; 1561efa90a98SHugh Dickins } 1562ec8acf20SShaohua Li spin_unlock(&sis->lock); 1563f577eb30SRafael J. Wysocki } 1564f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1565f577eb30SRafael J. Wysocki return n; 1566f577eb30SRafael J. Wysocki } 156773c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1568f577eb30SRafael J. Wysocki 15699f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1570179ef71cSCyrill Gorcunov { 15719f8bdb3fSHugh Dickins return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte); 1572179ef71cSCyrill Gorcunov } 1573179ef71cSCyrill Gorcunov 15741da177e4SLinus Torvalds /* 157572866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 157672866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 157772866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 15781da177e4SLinus Torvalds */ 1579044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 15801da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 15811da177e4SLinus Torvalds { 15829e16b7fbSHugh Dickins struct page *swapcache; 158372835c86SJohannes Weiner struct mem_cgroup *memcg; 1584044d66c1SHugh Dickins spinlock_t *ptl; 1585044d66c1SHugh Dickins pte_t *pte; 1586044d66c1SHugh Dickins int ret = 1; 1587044d66c1SHugh Dickins 15889e16b7fbSHugh Dickins swapcache = page; 15899e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 15909e16b7fbSHugh Dickins if (unlikely(!page)) 15919e16b7fbSHugh Dickins return -ENOMEM; 15929e16b7fbSHugh Dickins 1593f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 1594f627c2f5SKirill A. Shutemov &memcg, false)) { 1595044d66c1SHugh Dickins ret = -ENOMEM; 159685d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 159785d9fc89SKAMEZAWA Hiroyuki } 1598044d66c1SHugh Dickins 1599044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 16009f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1601f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1602044d66c1SHugh Dickins ret = 0; 1603044d66c1SHugh Dickins goto out; 1604044d66c1SHugh Dickins } 16058a9f3ccdSBalbir Singh 1606b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1607d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 16081da177e4SLinus Torvalds get_page(page); 16091da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 16101da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 161100501b53SJohannes Weiner if (page == swapcache) { 1612d281ee61SKirill A. Shutemov page_add_anon_rmap(page, vma, addr, false); 1613f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 161400501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1615d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, addr, false); 1616f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 161700501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 161800501b53SJohannes Weiner } 16191da177e4SLinus Torvalds swap_free(entry); 16201da177e4SLinus Torvalds /* 16211da177e4SLinus Torvalds * Move the page to the active list so it is not 16221da177e4SLinus Torvalds * immediately swapped out again after swapon. 16231da177e4SLinus Torvalds */ 16241da177e4SLinus Torvalds activate_page(page); 1625044d66c1SHugh Dickins out: 1626044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 162785d9fc89SKAMEZAWA Hiroyuki out_nolock: 16289e16b7fbSHugh Dickins if (page != swapcache) { 16299e16b7fbSHugh Dickins unlock_page(page); 16309e16b7fbSHugh Dickins put_page(page); 16319e16b7fbSHugh Dickins } 1632044d66c1SHugh Dickins return ret; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 16351da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 16361da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16371da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16381da177e4SLinus Torvalds { 16391da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1640705e87c0SHugh Dickins pte_t *pte; 16418a9f3ccdSBalbir Singh int ret = 0; 16421da177e4SLinus Torvalds 1643044d66c1SHugh Dickins /* 1644044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1645044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1646044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1647044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1648044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1649044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 16502de1a7e4SSeth Jennings * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1651044d66c1SHugh Dickins */ 1652044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 16531da177e4SLinus Torvalds do { 16541da177e4SLinus Torvalds /* 16551da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 16561da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 16571da177e4SLinus Torvalds */ 16589f8bdb3fSHugh Dickins if (unlikely(pte_same_as_swp(*pte, swp_pte))) { 1659044d66c1SHugh Dickins pte_unmap(pte); 1660044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1661044d66c1SHugh Dickins if (ret) 1662044d66c1SHugh Dickins goto out; 1663044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1666044d66c1SHugh Dickins pte_unmap(pte - 1); 1667044d66c1SHugh Dickins out: 16688a9f3ccdSBalbir Singh return ret; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 16721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16731da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16741da177e4SLinus Torvalds { 16751da177e4SLinus Torvalds pmd_t *pmd; 16761da177e4SLinus Torvalds unsigned long next; 16778a9f3ccdSBalbir Singh int ret; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 16801da177e4SLinus Torvalds do { 1681dc644a07SHugh Dickins cond_resched(); 16821da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 16831a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 16841da177e4SLinus Torvalds continue; 16858a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 16868a9f3ccdSBalbir Singh if (ret) 16878a9f3ccdSBalbir Singh return ret; 16881da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 16891da177e4SLinus Torvalds return 0; 16901da177e4SLinus Torvalds } 16911da177e4SLinus Torvalds 1692c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d, 16931da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16941da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16951da177e4SLinus Torvalds { 16961da177e4SLinus Torvalds pud_t *pud; 16971da177e4SLinus Torvalds unsigned long next; 16988a9f3ccdSBalbir Singh int ret; 16991da177e4SLinus Torvalds 1700c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 17011da177e4SLinus Torvalds do { 17021da177e4SLinus Torvalds next = pud_addr_end(addr, end); 17031da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 17041da177e4SLinus Torvalds continue; 17058a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 17068a9f3ccdSBalbir Singh if (ret) 17078a9f3ccdSBalbir Singh return ret; 17081da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 17091da177e4SLinus Torvalds return 0; 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 1712c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd, 1713c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1714c2febafcSKirill A. Shutemov swp_entry_t entry, struct page *page) 1715c2febafcSKirill A. Shutemov { 1716c2febafcSKirill A. Shutemov p4d_t *p4d; 1717c2febafcSKirill A. Shutemov unsigned long next; 1718c2febafcSKirill A. Shutemov int ret; 1719c2febafcSKirill A. Shutemov 1720c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1721c2febafcSKirill A. Shutemov do { 1722c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1723c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1724c2febafcSKirill A. Shutemov continue; 1725c2febafcSKirill A. Shutemov ret = unuse_pud_range(vma, p4d, addr, next, entry, page); 1726c2febafcSKirill A. Shutemov if (ret) 1727c2febafcSKirill A. Shutemov return ret; 1728c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1729c2febafcSKirill A. Shutemov return 0; 1730c2febafcSKirill A. Shutemov } 1731c2febafcSKirill A. Shutemov 17321da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 17331da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 17341da177e4SLinus Torvalds { 17351da177e4SLinus Torvalds pgd_t *pgd; 17361da177e4SLinus Torvalds unsigned long addr, end, next; 17378a9f3ccdSBalbir Singh int ret; 17381da177e4SLinus Torvalds 17393ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 17401da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 17411da177e4SLinus Torvalds if (addr == -EFAULT) 17421da177e4SLinus Torvalds return 0; 17431da177e4SLinus Torvalds else 17441da177e4SLinus Torvalds end = addr + PAGE_SIZE; 17451da177e4SLinus Torvalds } else { 17461da177e4SLinus Torvalds addr = vma->vm_start; 17471da177e4SLinus Torvalds end = vma->vm_end; 17481da177e4SLinus Torvalds } 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 17511da177e4SLinus Torvalds do { 17521da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 17531da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 17541da177e4SLinus Torvalds continue; 1755c2febafcSKirill A. Shutemov ret = unuse_p4d_range(vma, pgd, addr, next, entry, page); 17568a9f3ccdSBalbir Singh if (ret) 17578a9f3ccdSBalbir Singh return ret; 17581da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 17591da177e4SLinus Torvalds return 0; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 17631da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 17641da177e4SLinus Torvalds { 17651da177e4SLinus Torvalds struct vm_area_struct *vma; 17668a9f3ccdSBalbir Singh int ret = 0; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 17691da177e4SLinus Torvalds /* 17707d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 17717d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 17721da177e4SLinus Torvalds */ 1773c475a8abSHugh Dickins activate_page(page); 17741da177e4SLinus Torvalds unlock_page(page); 17751da177e4SLinus Torvalds down_read(&mm->mmap_sem); 17761da177e4SLinus Torvalds lock_page(page); 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 17798a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 17801da177e4SLinus Torvalds break; 1781dc644a07SHugh Dickins cond_resched(); 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds up_read(&mm->mmap_sem); 17848a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* 178838b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 178938b5faf4SDan Magenheimer * from current position to next entry still in use. 17901da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 17911da177e4SLinus Torvalds */ 17926eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 179338b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 17941da177e4SLinus Torvalds { 17956eb396dcSHugh Dickins unsigned int max = si->max; 17966eb396dcSHugh Dickins unsigned int i = prev; 17978d69aaeeSHugh Dickins unsigned char count; 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds /* 18005d337b91SHugh Dickins * No need for swap_lock here: we're just looking 18011da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 18021da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 18035d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 18041da177e4SLinus Torvalds */ 18051da177e4SLinus Torvalds for (;;) { 18061da177e4SLinus Torvalds if (++i >= max) { 18071da177e4SLinus Torvalds if (!prev) { 18081da177e4SLinus Torvalds i = 0; 18091da177e4SLinus Torvalds break; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds /* 18121da177e4SLinus Torvalds * No entries in use at top of swap_map, 18131da177e4SLinus Torvalds * loop back to start and recheck there. 18141da177e4SLinus Torvalds */ 18151da177e4SLinus Torvalds max = prev + 1; 18161da177e4SLinus Torvalds prev = 0; 18171da177e4SLinus Torvalds i = 1; 18181da177e4SLinus Torvalds } 18194db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 1820355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 1821dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 18221da177e4SLinus Torvalds break; 1823dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 1824dc644a07SHugh Dickins cond_resched(); 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds return i; 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds /* 18301da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 18311da177e4SLinus Torvalds * and then search for the process using it. All the necessary 18321da177e4SLinus Torvalds * page table adjustments can then be made atomically. 183338b5faf4SDan Magenheimer * 183438b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 183538b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 18361da177e4SLinus Torvalds */ 183738b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 183838b5faf4SDan Magenheimer unsigned long pages_to_unuse) 18391da177e4SLinus Torvalds { 1840efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 18411da177e4SLinus Torvalds struct mm_struct *start_mm; 1842edfe23daSShaohua Li volatile unsigned char *swap_map; /* swap_map is accessed without 1843edfe23daSShaohua Li * locking. Mark it as volatile 1844edfe23daSShaohua Li * to prevent compiler doing 1845edfe23daSShaohua Li * something odd. 1846edfe23daSShaohua Li */ 18478d69aaeeSHugh Dickins unsigned char swcount; 18481da177e4SLinus Torvalds struct page *page; 18491da177e4SLinus Torvalds swp_entry_t entry; 18506eb396dcSHugh Dickins unsigned int i = 0; 18511da177e4SLinus Torvalds int retval = 0; 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds /* 18541da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 18551da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 18561da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 18571da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 18581da177e4SLinus Torvalds * 18591da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 18601da177e4SLinus Torvalds * freed the previous entry from; but that would take less 18611da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 18621da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 18631da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 18641da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 1865570a335bSHugh Dickins * that. 18661da177e4SLinus Torvalds */ 18671da177e4SLinus Torvalds start_mm = &init_mm; 18683fce371bSVegard Nossum mmget(&init_mm); 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /* 18711da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 18721da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 18731da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 18741da177e4SLinus Torvalds */ 187538b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 18761da177e4SLinus Torvalds if (signal_pending(current)) { 18771da177e4SLinus Torvalds retval = -EINTR; 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds /* 18821da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 18831da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 18841da177e4SLinus Torvalds * page and read the swap into it. 18851da177e4SLinus Torvalds */ 18861da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 18871da177e4SLinus Torvalds entry = swp_entry(type, i); 188802098feaSHugh Dickins page = read_swap_cache_async(entry, 188923955622SShaohua Li GFP_HIGHUSER_MOVABLE, NULL, 0, false); 18901da177e4SLinus Torvalds if (!page) { 18911da177e4SLinus Torvalds /* 18921da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 18931da177e4SLinus Torvalds * has been freed independently, and will not be 18941da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 18951da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 18961da177e4SLinus Torvalds */ 1897edfe23daSShaohua Li swcount = *swap_map; 1898edfe23daSShaohua Li /* 1899edfe23daSShaohua Li * We don't hold lock here, so the swap entry could be 1900edfe23daSShaohua Li * SWAP_MAP_BAD (when the cluster is discarding). 1901edfe23daSShaohua Li * Instead of fail out, We can just skip the swap 1902edfe23daSShaohua Li * entry because swapoff will wait for discarding 1903edfe23daSShaohua Li * finish anyway. 1904edfe23daSShaohua Li */ 1905edfe23daSShaohua Li if (!swcount || swcount == SWAP_MAP_BAD) 19061da177e4SLinus Torvalds continue; 19071da177e4SLinus Torvalds retval = -ENOMEM; 19081da177e4SLinus Torvalds break; 19091da177e4SLinus Torvalds } 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds /* 19121da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 19131da177e4SLinus Torvalds */ 19141da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 19151da177e4SLinus Torvalds mmput(start_mm); 19161da177e4SLinus Torvalds start_mm = &init_mm; 19173fce371bSVegard Nossum mmget(&init_mm); 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds /* 19211da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 19221da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 19231da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 19241da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 19251da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 19261da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 19271da177e4SLinus Torvalds */ 19281da177e4SLinus Torvalds wait_on_page_locked(page); 19291da177e4SLinus Torvalds wait_on_page_writeback(page); 19301da177e4SLinus Torvalds lock_page(page); 19311da177e4SLinus Torvalds wait_on_page_writeback(page); 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds /* 19341da177e4SLinus Torvalds * Remove all references to entry. 19351da177e4SLinus Torvalds */ 19361da177e4SLinus Torvalds swcount = *swap_map; 1937aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 1938aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 1939aaa46865SHugh Dickins /* page has already been unlocked and released */ 1940aaa46865SHugh Dickins if (retval < 0) 1941aaa46865SHugh Dickins break; 1942aaa46865SHugh Dickins continue; 19431da177e4SLinus Torvalds } 1944aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 1945aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 1946aaa46865SHugh Dickins 1947355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 19481da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 19491da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 19501da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 19511da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 19521da177e4SLinus Torvalds struct mm_struct *mm; 19531da177e4SLinus Torvalds 19543fce371bSVegard Nossum mmget(new_start_mm); 19553fce371bSVegard Nossum mmget(prev_mm); 19561da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1957aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 19581da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 19591da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 1960388f7934SVegard Nossum if (!mmget_not_zero(mm)) 19611da177e4SLinus Torvalds continue; 19621da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 19631da177e4SLinus Torvalds mmput(prev_mm); 19641da177e4SLinus Torvalds prev_mm = mm; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds cond_resched(); 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds swcount = *swap_map; 1969355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 19701da177e4SLinus Torvalds ; 1971aaa46865SHugh Dickins else if (mm == &init_mm) 19721da177e4SLinus Torvalds set_start_mm = 1; 1973aaa46865SHugh Dickins else 19741da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1975355cfa73SKAMEZAWA Hiroyuki 197632c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 19771da177e4SLinus Torvalds mmput(new_start_mm); 19783fce371bSVegard Nossum mmget(mm); 19791da177e4SLinus Torvalds new_start_mm = mm; 19801da177e4SLinus Torvalds set_start_mm = 0; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds spin_lock(&mmlist_lock); 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 19851da177e4SLinus Torvalds mmput(prev_mm); 19861da177e4SLinus Torvalds mmput(start_mm); 19871da177e4SLinus Torvalds start_mm = new_start_mm; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds if (retval) { 19901da177e4SLinus Torvalds unlock_page(page); 199109cbfeafSKirill A. Shutemov put_page(page); 19921da177e4SLinus Torvalds break; 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /* 19961da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 19971da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 19981da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 19991da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 20001da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 20011da177e4SLinus Torvalds * delete from cache even if there's another reference, 20021da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 20031da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 20041da177e4SLinus Torvalds * read from disk into another page. Splitting into two 20051da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 20061da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 20075ad64688SHugh Dickins * 20085ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 20095ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 20105ad64688SHugh Dickins * this splitting happens to be just what is needed to 20115ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 20125ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 20131da177e4SLinus Torvalds */ 2014355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 2015355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 20161da177e4SLinus Torvalds struct writeback_control wbc = { 20171da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 20181da177e4SLinus Torvalds }; 20191da177e4SLinus Torvalds 20201da177e4SLinus Torvalds swap_writepage(page, &wbc); 20211da177e4SLinus Torvalds lock_page(page); 20221da177e4SLinus Torvalds wait_on_page_writeback(page); 20231da177e4SLinus Torvalds } 202468bdc8d6SHugh Dickins 202568bdc8d6SHugh Dickins /* 202668bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 202768bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 202868bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 202968bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 203068bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 203168bdc8d6SHugh Dickins */ 203268bdc8d6SHugh Dickins if (PageSwapCache(page) && 203368bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 20341da177e4SLinus Torvalds delete_from_swap_cache(page); 20351da177e4SLinus Torvalds 20361da177e4SLinus Torvalds /* 20371da177e4SLinus Torvalds * So we could skip searching mms once swap count went 20381da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 20392706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 20401da177e4SLinus Torvalds */ 20411da177e4SLinus Torvalds SetPageDirty(page); 20421da177e4SLinus Torvalds unlock_page(page); 204309cbfeafSKirill A. Shutemov put_page(page); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds /* 20461da177e4SLinus Torvalds * Make sure that we aren't completely killing 20471da177e4SLinus Torvalds * interactive performance. 20481da177e4SLinus Torvalds */ 20491da177e4SLinus Torvalds cond_resched(); 205038b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 205138b5faf4SDan Magenheimer if (!--pages_to_unuse) 205238b5faf4SDan Magenheimer break; 205338b5faf4SDan Magenheimer } 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds mmput(start_mm); 20571da177e4SLinus Torvalds return retval; 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /* 20615d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 20625d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 20635d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 20641da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 20651da177e4SLinus Torvalds */ 20661da177e4SLinus Torvalds static void drain_mmlist(void) 20671da177e4SLinus Torvalds { 20681da177e4SLinus Torvalds struct list_head *p, *next; 2069efa90a98SHugh Dickins unsigned int type; 20701da177e4SLinus Torvalds 2071efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 2072efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 20731da177e4SLinus Torvalds return; 20741da177e4SLinus Torvalds spin_lock(&mmlist_lock); 20751da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 20761da177e4SLinus Torvalds list_del_init(p); 20771da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 20781da177e4SLinus Torvalds } 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds /* 20811da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 2082d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 2083d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 2084d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 20851da177e4SLinus Torvalds */ 2086d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 20871da177e4SLinus Torvalds { 2088f29ad6a9SHugh Dickins struct swap_info_struct *sis; 2089f29ad6a9SHugh Dickins struct swap_extent *start_se; 2090f29ad6a9SHugh Dickins struct swap_extent *se; 2091f29ad6a9SHugh Dickins pgoff_t offset; 2092f29ad6a9SHugh Dickins 2093efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 2094f29ad6a9SHugh Dickins *bdev = sis->bdev; 2095f29ad6a9SHugh Dickins 2096f29ad6a9SHugh Dickins offset = swp_offset(entry); 2097f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 2098f29ad6a9SHugh Dickins se = start_se; 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds for ( ; ; ) { 21011da177e4SLinus Torvalds if (se->start_page <= offset && 21021da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 21031da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 21041da177e4SLinus Torvalds } 2105a8ae4991SGeliang Tang se = list_next_entry(se, list); 21061da177e4SLinus Torvalds sis->curr_swap_extent = se; 21071da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 21081da177e4SLinus Torvalds } 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds 21111da177e4SLinus Torvalds /* 2112d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 2113d4906e1aSLee Schermerhorn */ 2114d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 2115d4906e1aSLee Schermerhorn { 2116d4906e1aSLee Schermerhorn swp_entry_t entry; 2117d4906e1aSLee Schermerhorn entry.val = page_private(page); 2118d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 2119d4906e1aSLee Schermerhorn } 2120d4906e1aSLee Schermerhorn 2121d4906e1aSLee Schermerhorn /* 21221da177e4SLinus Torvalds * Free all of a swapdev's extent information 21231da177e4SLinus Torvalds */ 21241da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 21251da177e4SLinus Torvalds { 21269625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 21271da177e4SLinus Torvalds struct swap_extent *se; 21281da177e4SLinus Torvalds 2129a8ae4991SGeliang Tang se = list_first_entry(&sis->first_swap_extent.list, 21301da177e4SLinus Torvalds struct swap_extent, list); 21311da177e4SLinus Torvalds list_del(&se->list); 21321da177e4SLinus Torvalds kfree(se); 21331da177e4SLinus Torvalds } 213462c230bcSMel Gorman 213562c230bcSMel Gorman if (sis->flags & SWP_FILE) { 213662c230bcSMel Gorman struct file *swap_file = sis->swap_file; 213762c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 213862c230bcSMel Gorman 213962c230bcSMel Gorman sis->flags &= ~SWP_FILE; 214062c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 214162c230bcSMel Gorman } 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* 21451da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 214611d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 21471da177e4SLinus Torvalds * 214811d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 21491da177e4SLinus Torvalds */ 2150a509bc1aSMel Gorman int 21511da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 21521da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 21531da177e4SLinus Torvalds { 21541da177e4SLinus Torvalds struct swap_extent *se; 21551da177e4SLinus Torvalds struct swap_extent *new_se; 21561da177e4SLinus Torvalds struct list_head *lh; 21571da177e4SLinus Torvalds 21589625a5f2SHugh Dickins if (start_page == 0) { 21599625a5f2SHugh Dickins se = &sis->first_swap_extent; 21609625a5f2SHugh Dickins sis->curr_swap_extent = se; 21619625a5f2SHugh Dickins se->start_page = 0; 21629625a5f2SHugh Dickins se->nr_pages = nr_pages; 21639625a5f2SHugh Dickins se->start_block = start_block; 21649625a5f2SHugh Dickins return 1; 21659625a5f2SHugh Dickins } else { 21669625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 21671da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 216811d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 216911d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 21701da177e4SLinus Torvalds /* Merge it */ 21711da177e4SLinus Torvalds se->nr_pages += nr_pages; 21721da177e4SLinus Torvalds return 0; 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds } 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds /* 21771da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 21781da177e4SLinus Torvalds */ 21791da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 21801da177e4SLinus Torvalds if (new_se == NULL) 21811da177e4SLinus Torvalds return -ENOMEM; 21821da177e4SLinus Torvalds new_se->start_page = start_page; 21831da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 21841da177e4SLinus Torvalds new_se->start_block = start_block; 21851da177e4SLinus Torvalds 21869625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 218753092a74SHugh Dickins return 1; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 21901da177e4SLinus Torvalds /* 21911da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 21921da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 21931da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 21941da177e4SLinus Torvalds * time for locating where on disk a page belongs. 21951da177e4SLinus Torvalds * 21961da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 21971da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 21981da177e4SLinus Torvalds * swap files identically. 21991da177e4SLinus Torvalds * 22001da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 22011da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 22021da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 22031da177e4SLinus Torvalds * 22041da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 22051da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 22061da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 22071da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 22081da177e4SLinus Torvalds * for swapping. 22091da177e4SLinus Torvalds * 2210b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 22111da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 22121da177e4SLinus Torvalds * which will scribble on the fs. 22131da177e4SLinus Torvalds * 22141da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 22151da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 22161da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 22171da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 22181da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 22191da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 22201da177e4SLinus Torvalds */ 222153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 22221da177e4SLinus Torvalds { 222362c230bcSMel Gorman struct file *swap_file = sis->swap_file; 222462c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 222562c230bcSMel Gorman struct inode *inode = mapping->host; 22261da177e4SLinus Torvalds int ret; 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 22291da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 223053092a74SHugh Dickins *span = sis->pages; 2231a509bc1aSMel Gorman return ret; 22321da177e4SLinus Torvalds } 22331da177e4SLinus Torvalds 223462c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 2235a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 223662c230bcSMel Gorman if (!ret) { 223762c230bcSMel Gorman sis->flags |= SWP_FILE; 223862c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 223962c230bcSMel Gorman *span = sis->pages; 224062c230bcSMel Gorman } 22419625a5f2SHugh Dickins return ret; 2242a509bc1aSMel Gorman } 2243a509bc1aSMel Gorman 2244a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 22451da177e4SLinus Torvalds } 22461da177e4SLinus Torvalds 2247cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 22482a8f9449SShaohua Li unsigned char *swap_map, 22492a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 225040531542SCesar Eduardo Barros { 225140531542SCesar Eduardo Barros if (prio >= 0) 225240531542SCesar Eduardo Barros p->prio = prio; 225340531542SCesar Eduardo Barros else 225440531542SCesar Eduardo Barros p->prio = --least_priority; 225518ab4d4cSDan Streetman /* 225618ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 225718ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 225818ab4d4cSDan Streetman */ 225918ab4d4cSDan Streetman p->list.prio = -p->prio; 226018ab4d4cSDan Streetman p->avail_list.prio = -p->prio; 226140531542SCesar Eduardo Barros p->swap_map = swap_map; 22622a8f9449SShaohua Li p->cluster_info = cluster_info; 226340531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 2264ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 226540531542SCesar Eduardo Barros total_swap_pages += p->pages; 226640531542SCesar Eduardo Barros 2267adfab836SDan Streetman assert_spin_locked(&swap_lock); 2268adfab836SDan Streetman /* 226918ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 227018ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 227118ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 227218ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 227318ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 227418ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 227518ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 227618ab4d4cSDan Streetman * swap_info_struct. 2277adfab836SDan Streetman */ 227818ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 227918ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 228018ab4d4cSDan Streetman plist_add(&p->avail_list, &swap_avail_head); 228118ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 2282cf0cac0aSCesar Eduardo Barros } 2283cf0cac0aSCesar Eduardo Barros 2284cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 2285cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 22862a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2287cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 2288cf0cac0aSCesar Eduardo Barros { 22894f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 2290cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2291ec8acf20SShaohua Li spin_lock(&p->lock); 22922a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 2293ec8acf20SShaohua Li spin_unlock(&p->lock); 2294cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 2295cf0cac0aSCesar Eduardo Barros } 2296cf0cac0aSCesar Eduardo Barros 2297cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 2298cf0cac0aSCesar Eduardo Barros { 2299cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2300ec8acf20SShaohua Li spin_lock(&p->lock); 23012a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 2302ec8acf20SShaohua Li spin_unlock(&p->lock); 230340531542SCesar Eduardo Barros spin_unlock(&swap_lock); 230440531542SCesar Eduardo Barros } 230540531542SCesar Eduardo Barros 230667afa38eSTim Chen bool has_usable_swap(void) 230767afa38eSTim Chen { 230867afa38eSTim Chen bool ret = true; 230967afa38eSTim Chen 231067afa38eSTim Chen spin_lock(&swap_lock); 231167afa38eSTim Chen if (plist_head_empty(&swap_active_head)) 231267afa38eSTim Chen ret = false; 231367afa38eSTim Chen spin_unlock(&swap_lock); 231467afa38eSTim Chen return ret; 231567afa38eSTim Chen } 231667afa38eSTim Chen 2317c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 23181da177e4SLinus Torvalds { 23191da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 23208d69aaeeSHugh Dickins unsigned char *swap_map; 23212a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 23224f89849dSMinchan Kim unsigned long *frontswap_map; 23231da177e4SLinus Torvalds struct file *swap_file, *victim; 23241da177e4SLinus Torvalds struct address_space *mapping; 23251da177e4SLinus Torvalds struct inode *inode; 232691a27b2aSJeff Layton struct filename *pathname; 2327adfab836SDan Streetman int err, found = 0; 23285b808a23SKrzysztof Kozlowski unsigned int old_block_size; 23291da177e4SLinus Torvalds 23301da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 23311da177e4SLinus Torvalds return -EPERM; 23321da177e4SLinus Torvalds 2333191c5424SAl Viro BUG_ON(!current->mm); 2334191c5424SAl Viro 23351da177e4SLinus Torvalds pathname = getname(specialfile); 23361da177e4SLinus Torvalds if (IS_ERR(pathname)) 2337f58b59c1SXiaotian Feng return PTR_ERR(pathname); 23381da177e4SLinus Torvalds 2339669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 23401da177e4SLinus Torvalds err = PTR_ERR(victim); 23411da177e4SLinus Torvalds if (IS_ERR(victim)) 23421da177e4SLinus Torvalds goto out; 23431da177e4SLinus Torvalds 23441da177e4SLinus Torvalds mapping = victim->f_mapping; 23455d337b91SHugh Dickins spin_lock(&swap_lock); 234618ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 234722c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 2348adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 2349adfab836SDan Streetman found = 1; 23501da177e4SLinus Torvalds break; 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds } 2353adfab836SDan Streetman } 2354adfab836SDan Streetman if (!found) { 23551da177e4SLinus Torvalds err = -EINVAL; 23565d337b91SHugh Dickins spin_unlock(&swap_lock); 23571da177e4SLinus Torvalds goto out_dput; 23581da177e4SLinus Torvalds } 2359191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 23601da177e4SLinus Torvalds vm_unacct_memory(p->pages); 23611da177e4SLinus Torvalds else { 23621da177e4SLinus Torvalds err = -ENOMEM; 23635d337b91SHugh Dickins spin_unlock(&swap_lock); 23641da177e4SLinus Torvalds goto out_dput; 23651da177e4SLinus Torvalds } 236618ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 236718ab4d4cSDan Streetman plist_del(&p->avail_list, &swap_avail_head); 236818ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 2369ec8acf20SShaohua Li spin_lock(&p->lock); 237078ecba08SHugh Dickins if (p->prio < 0) { 2371adfab836SDan Streetman struct swap_info_struct *si = p; 2372adfab836SDan Streetman 237318ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2374adfab836SDan Streetman si->prio++; 237518ab4d4cSDan Streetman si->list.prio--; 237618ab4d4cSDan Streetman si->avail_list.prio--; 2377adfab836SDan Streetman } 237878ecba08SHugh Dickins least_priority++; 237978ecba08SHugh Dickins } 238018ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2381ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 23821da177e4SLinus Torvalds total_swap_pages -= p->pages; 23831da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2384ec8acf20SShaohua Li spin_unlock(&p->lock); 23855d337b91SHugh Dickins spin_unlock(&swap_lock); 2386fb4f88dcSHugh Dickins 2387039939a6STim Chen disable_swap_slots_cache_lock(); 2388039939a6STim Chen 2389e1e12d2fSDavid Rientjes set_current_oom_origin(); 2390adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2391e1e12d2fSDavid Rientjes clear_current_oom_origin(); 23921da177e4SLinus Torvalds 23931da177e4SLinus Torvalds if (err) { 23941da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2395cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 2396039939a6STim Chen reenable_swap_slots_cache_unlock(); 23971da177e4SLinus Torvalds goto out_dput; 23981da177e4SLinus Torvalds } 239952b7efdbSHugh Dickins 2400039939a6STim Chen reenable_swap_slots_cache_unlock(); 2401039939a6STim Chen 2402815c2c54SShaohua Li flush_work(&p->discard_work); 2403815c2c54SShaohua Li 24044cd3bb10SHugh Dickins destroy_swap_extents(p); 2405570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2406570a335bSHugh Dickins free_swap_count_continuations(p); 2407570a335bSHugh Dickins 2408fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 24095d337b91SHugh Dickins spin_lock(&swap_lock); 2410ec8acf20SShaohua Li spin_lock(&p->lock); 24111da177e4SLinus Torvalds drain_mmlist(); 24125d337b91SHugh Dickins 24135d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 24145d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 24155d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2416ec8acf20SShaohua Li spin_unlock(&p->lock); 24175d337b91SHugh Dickins spin_unlock(&swap_lock); 241813e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 24195d337b91SHugh Dickins spin_lock(&swap_lock); 2420ec8acf20SShaohua Li spin_lock(&p->lock); 24215d337b91SHugh Dickins } 24225d337b91SHugh Dickins 24231da177e4SLinus Torvalds swap_file = p->swap_file; 24245b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 24251da177e4SLinus Torvalds p->swap_file = NULL; 24261da177e4SLinus Torvalds p->max = 0; 24271da177e4SLinus Torvalds swap_map = p->swap_map; 24281da177e4SLinus Torvalds p->swap_map = NULL; 24292a8f9449SShaohua Li cluster_info = p->cluster_info; 24302a8f9449SShaohua Li p->cluster_info = NULL; 24314f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2432ec8acf20SShaohua Li spin_unlock(&p->lock); 24335d337b91SHugh Dickins spin_unlock(&swap_lock); 2434adfab836SDan Streetman frontswap_invalidate_area(p->type); 243558e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2436fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2437ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2438ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 24391da177e4SLinus Torvalds vfree(swap_map); 244054f180d3SHuang Ying kvfree(cluster_info); 244154f180d3SHuang Ying kvfree(frontswap_map); 24422de1a7e4SSeth Jennings /* Destroy swap account information */ 2443adfab836SDan Streetman swap_cgroup_swapoff(p->type); 24444b3ef9daSHuang, Ying exit_swap_address_space(p->type); 244527a7faa0SKAMEZAWA Hiroyuki 24461da177e4SLinus Torvalds inode = mapping->host; 24471da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 24481da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 24495b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2450e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 24511da177e4SLinus Torvalds } else { 24525955102cSAl Viro inode_lock(inode); 24531da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 24545955102cSAl Viro inode_unlock(inode); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds filp_close(swap_file, NULL); 2457f893ab41SWeijie Yang 2458f893ab41SWeijie Yang /* 2459f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2460f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2461f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2462f893ab41SWeijie Yang */ 2463f893ab41SWeijie Yang spin_lock(&swap_lock); 2464f893ab41SWeijie Yang p->flags = 0; 2465f893ab41SWeijie Yang spin_unlock(&swap_lock); 2466f893ab41SWeijie Yang 24671da177e4SLinus Torvalds err = 0; 246866d7dd51SKay Sievers atomic_inc(&proc_poll_event); 246966d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds out_dput: 24721da177e4SLinus Torvalds filp_close(victim, NULL); 24731da177e4SLinus Torvalds out: 2474f58b59c1SXiaotian Feng putname(pathname); 24751da177e4SLinus Torvalds return err; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 24781da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 247966d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 248066d7dd51SKay Sievers { 2481f1514638SKay Sievers struct seq_file *seq = file->private_data; 248266d7dd51SKay Sievers 248366d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 248466d7dd51SKay Sievers 2485f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2486f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 248766d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 248866d7dd51SKay Sievers } 248966d7dd51SKay Sievers 249066d7dd51SKay Sievers return POLLIN | POLLRDNORM; 249166d7dd51SKay Sievers } 249266d7dd51SKay Sievers 24931da177e4SLinus Torvalds /* iterator */ 24941da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 24951da177e4SLinus Torvalds { 2496efa90a98SHugh Dickins struct swap_info_struct *si; 2497efa90a98SHugh Dickins int type; 24981da177e4SLinus Torvalds loff_t l = *pos; 24991da177e4SLinus Torvalds 2500fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 25011da177e4SLinus Torvalds 2502881e4aabSSuleiman Souhlal if (!l) 2503881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2504881e4aabSSuleiman Souhlal 2505efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2506efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2507efa90a98SHugh Dickins si = swap_info[type]; 2508efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 25091da177e4SLinus Torvalds continue; 2510881e4aabSSuleiman Souhlal if (!--l) 2511efa90a98SHugh Dickins return si; 25121da177e4SLinus Torvalds } 25131da177e4SLinus Torvalds 25141da177e4SLinus Torvalds return NULL; 25151da177e4SLinus Torvalds } 25161da177e4SLinus Torvalds 25171da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 25181da177e4SLinus Torvalds { 2519efa90a98SHugh Dickins struct swap_info_struct *si = v; 2520efa90a98SHugh Dickins int type; 25211da177e4SLinus Torvalds 2522881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2523efa90a98SHugh Dickins type = 0; 2524efa90a98SHugh Dickins else 2525efa90a98SHugh Dickins type = si->type + 1; 2526881e4aabSSuleiman Souhlal 2527efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 2528efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2529efa90a98SHugh Dickins si = swap_info[type]; 2530efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 25311da177e4SLinus Torvalds continue; 25321da177e4SLinus Torvalds ++*pos; 2533efa90a98SHugh Dickins return si; 25341da177e4SLinus Torvalds } 25351da177e4SLinus Torvalds 25361da177e4SLinus Torvalds return NULL; 25371da177e4SLinus Torvalds } 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 25401da177e4SLinus Torvalds { 2541fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 25421da177e4SLinus Torvalds } 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 25451da177e4SLinus Torvalds { 2546efa90a98SHugh Dickins struct swap_info_struct *si = v; 25471da177e4SLinus Torvalds struct file *file; 25481da177e4SLinus Torvalds int len; 25491da177e4SLinus Torvalds 2550efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 25511da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2552881e4aabSSuleiman Souhlal return 0; 2553881e4aabSSuleiman Souhlal } 25541da177e4SLinus Torvalds 2555efa90a98SHugh Dickins file = si->swap_file; 25562726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 25576eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 25581da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2559496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 25601da177e4SLinus Torvalds "partition" : "file\t", 2561efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2562efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2563efa90a98SHugh Dickins si->prio); 25641da177e4SLinus Torvalds return 0; 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 256715ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 25681da177e4SLinus Torvalds .start = swap_start, 25691da177e4SLinus Torvalds .next = swap_next, 25701da177e4SLinus Torvalds .stop = swap_stop, 25711da177e4SLinus Torvalds .show = swap_show 25721da177e4SLinus Torvalds }; 25731da177e4SLinus Torvalds 25741da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 25751da177e4SLinus Torvalds { 2576f1514638SKay Sievers struct seq_file *seq; 257766d7dd51SKay Sievers int ret; 257866d7dd51SKay Sievers 257966d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2580f1514638SKay Sievers if (ret) 258166d7dd51SKay Sievers return ret; 258266d7dd51SKay Sievers 2583f1514638SKay Sievers seq = file->private_data; 2584f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2585f1514638SKay Sievers return 0; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 258815ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 25891da177e4SLinus Torvalds .open = swaps_open, 25901da177e4SLinus Torvalds .read = seq_read, 25911da177e4SLinus Torvalds .llseek = seq_lseek, 25921da177e4SLinus Torvalds .release = seq_release, 259366d7dd51SKay Sievers .poll = swaps_poll, 25941da177e4SLinus Torvalds }; 25951da177e4SLinus Torvalds 25961da177e4SLinus Torvalds static int __init procswaps_init(void) 25971da177e4SLinus Torvalds { 25983d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 25991da177e4SLinus Torvalds return 0; 26001da177e4SLinus Torvalds } 26011da177e4SLinus Torvalds __initcall(procswaps_init); 26021da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 26031da177e4SLinus Torvalds 26041796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 26051796316aSJan Beulich static int __init max_swapfiles_check(void) 26061796316aSJan Beulich { 26071796316aSJan Beulich MAX_SWAPFILES_CHECK(); 26081796316aSJan Beulich return 0; 26091796316aSJan Beulich } 26101796316aSJan Beulich late_initcall(max_swapfiles_check); 26111796316aSJan Beulich #endif 26121796316aSJan Beulich 261353cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 26141da177e4SLinus Torvalds { 26151da177e4SLinus Torvalds struct swap_info_struct *p; 26161da177e4SLinus Torvalds unsigned int type; 2617efa90a98SHugh Dickins 2618efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2619efa90a98SHugh Dickins if (!p) 262053cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2621efa90a98SHugh Dickins 26225d337b91SHugh Dickins spin_lock(&swap_lock); 2623efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2624efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 26251da177e4SLinus Torvalds break; 2626efa90a98SHugh Dickins } 26270697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 26285d337b91SHugh Dickins spin_unlock(&swap_lock); 2629efa90a98SHugh Dickins kfree(p); 2630730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 26311da177e4SLinus Torvalds } 2632efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2633efa90a98SHugh Dickins p->type = type; 2634efa90a98SHugh Dickins swap_info[type] = p; 2635efa90a98SHugh Dickins /* 2636efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2637efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2638efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2639efa90a98SHugh Dickins */ 2640efa90a98SHugh Dickins smp_wmb(); 2641efa90a98SHugh Dickins nr_swapfiles++; 2642efa90a98SHugh Dickins } else { 2643efa90a98SHugh Dickins kfree(p); 2644efa90a98SHugh Dickins p = swap_info[type]; 2645efa90a98SHugh Dickins /* 2646efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2647efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2648efa90a98SHugh Dickins */ 2649efa90a98SHugh Dickins } 26509625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 265118ab4d4cSDan Streetman plist_node_init(&p->list, 0); 265218ab4d4cSDan Streetman plist_node_init(&p->avail_list, 0); 26531da177e4SLinus Torvalds p->flags = SWP_USED; 26545d337b91SHugh Dickins spin_unlock(&swap_lock); 2655ec8acf20SShaohua Li spin_lock_init(&p->lock); 2656efa90a98SHugh Dickins 265753cbb243SCesar Eduardo Barros return p; 265853cbb243SCesar Eduardo Barros } 265953cbb243SCesar Eduardo Barros 26604d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 26614d0e1e10SCesar Eduardo Barros { 26624d0e1e10SCesar Eduardo Barros int error; 26634d0e1e10SCesar Eduardo Barros 26644d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 26654d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 26664d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 26676f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 26684d0e1e10SCesar Eduardo Barros if (error < 0) { 26694d0e1e10SCesar Eduardo Barros p->bdev = NULL; 26706f179af8SHugh Dickins return error; 26714d0e1e10SCesar Eduardo Barros } 26724d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 26734d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 26744d0e1e10SCesar Eduardo Barros if (error < 0) 267587ade72aSCesar Eduardo Barros return error; 26764d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 26774d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 26784d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 26795955102cSAl Viro inode_lock(inode); 268087ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 268187ade72aSCesar Eduardo Barros return -EBUSY; 268287ade72aSCesar Eduardo Barros } else 268387ade72aSCesar Eduardo Barros return -EINVAL; 26844d0e1e10SCesar Eduardo Barros 26854d0e1e10SCesar Eduardo Barros return 0; 26864d0e1e10SCesar Eduardo Barros } 26874d0e1e10SCesar Eduardo Barros 2688ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2689ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2690ca8bd38bSCesar Eduardo Barros struct inode *inode) 2691ca8bd38bSCesar Eduardo Barros { 2692ca8bd38bSCesar Eduardo Barros int i; 2693ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2694ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2695d6bbbd29SRaymond Jennings unsigned long last_page; 2696ca8bd38bSCesar Eduardo Barros 2697ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2698465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 269938719025SCesar Eduardo Barros return 0; 2700ca8bd38bSCesar Eduardo Barros } 2701ca8bd38bSCesar Eduardo Barros 2702ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2703ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2704ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2705ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2706ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2707dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2708dd111be6SJann Horn return 0; 2709ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2710ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2711ca8bd38bSCesar Eduardo Barros } 2712ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2713ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2714465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2715ca8bd38bSCesar Eduardo Barros swap_header->info.version); 271638719025SCesar Eduardo Barros return 0; 2717ca8bd38bSCesar Eduardo Barros } 2718ca8bd38bSCesar Eduardo Barros 2719ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2720ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2721ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2722ca8bd38bSCesar Eduardo Barros 2723ca8bd38bSCesar Eduardo Barros /* 2724ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 27259b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2726a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2727a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 27289b15b817SHugh Dickins * different architectures. In order to find the 2729a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2730ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2731a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2732ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2733ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2734ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 27359b15b817SHugh Dickins * swap pte. 2736ca8bd38bSCesar Eduardo Barros */ 2737ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 27389b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2739d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2740d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2741465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2742d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2743d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2744d6bbbd29SRaymond Jennings } 2745d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2746d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2747ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2748ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2749ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2750ca8bd38bSCesar Eduardo Barros } 2751ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2752ca8bd38bSCesar Eduardo Barros 2753ca8bd38bSCesar Eduardo Barros if (!maxpages) 275438719025SCesar Eduardo Barros return 0; 2755ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2756ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2757465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 275838719025SCesar Eduardo Barros return 0; 2759ca8bd38bSCesar Eduardo Barros } 2760ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 276138719025SCesar Eduardo Barros return 0; 2762ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 276338719025SCesar Eduardo Barros return 0; 2764ca8bd38bSCesar Eduardo Barros 2765ca8bd38bSCesar Eduardo Barros return maxpages; 2766ca8bd38bSCesar Eduardo Barros } 2767ca8bd38bSCesar Eduardo Barros 27684b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS \ 2769235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 27704b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS \ 27714b3ef9daSHuang, Ying DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) 27724b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS \ 27734b3ef9daSHuang, Ying max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) 2774235b6217SHuang, Ying 2775915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2776915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2777915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 27782a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2779915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2780915d4d7bSCesar Eduardo Barros sector_t *span) 2781915d4d7bSCesar Eduardo Barros { 2782235b6217SHuang, Ying unsigned int j, k; 2783915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 2784915d4d7bSCesar Eduardo Barros int nr_extents; 27852a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 2786235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 2787235b6217SHuang, Ying unsigned long i, idx; 2788915d4d7bSCesar Eduardo Barros 2789915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 2790915d4d7bSCesar Eduardo Barros 27916b534915SHuang Ying cluster_list_init(&p->free_clusters); 27926b534915SHuang Ying cluster_list_init(&p->discard_clusters); 27932a8f9449SShaohua Li 2794915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 2795915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 2796bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 2797bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2798915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 2799915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 2800915d4d7bSCesar Eduardo Barros nr_good_pages--; 28012a8f9449SShaohua Li /* 28022a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 28032a8f9449SShaohua Li * operation involved 28042a8f9449SShaohua Li */ 28052a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 2806915d4d7bSCesar Eduardo Barros } 2807915d4d7bSCesar Eduardo Barros } 2808915d4d7bSCesar Eduardo Barros 28092a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 28102a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 28112a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 28122a8f9449SShaohua Li 2813915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 2814915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 28152a8f9449SShaohua Li /* 28162a8f9449SShaohua Li * Not mark the cluster free yet, no list 28172a8f9449SShaohua Li * operation involved 28182a8f9449SShaohua Li */ 28192a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 2820915d4d7bSCesar Eduardo Barros p->max = maxpages; 2821915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 2822915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 2823bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 2824bdb8e3f6SCesar Eduardo Barros return nr_extents; 2825915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 2826915d4d7bSCesar Eduardo Barros } 2827915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 2828465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 2829bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2830915d4d7bSCesar Eduardo Barros } 2831915d4d7bSCesar Eduardo Barros 28322a8f9449SShaohua Li if (!cluster_info) 28332a8f9449SShaohua Li return nr_extents; 28342a8f9449SShaohua Li 2835235b6217SHuang, Ying 28364b3ef9daSHuang, Ying /* 28374b3ef9daSHuang, Ying * Reduce false cache line sharing between cluster_info and 28384b3ef9daSHuang, Ying * sharing same address space. 28394b3ef9daSHuang, Ying */ 2840235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 2841235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 2842235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 2843235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 2844235b6217SHuang, Ying if (idx >= nr_clusters) 2845235b6217SHuang, Ying continue; 2846235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 2847235b6217SHuang, Ying continue; 28482a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 28496b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 28506b534915SHuang Ying idx); 28512a8f9449SShaohua Li } 28522a8f9449SShaohua Li } 2853915d4d7bSCesar Eduardo Barros return nr_extents; 2854915d4d7bSCesar Eduardo Barros } 2855915d4d7bSCesar Eduardo Barros 2856dcf6b7ddSRafael Aquini /* 2857dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 2858dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 2859dcf6b7ddSRafael Aquini */ 2860dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 2861dcf6b7ddSRafael Aquini { 2862dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 2863dcf6b7ddSRafael Aquini 2864dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 2865dcf6b7ddSRafael Aquini return false; 2866dcf6b7ddSRafael Aquini 2867dcf6b7ddSRafael Aquini return true; 2868dcf6b7ddSRafael Aquini } 2869dcf6b7ddSRafael Aquini 287053cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 287153cbb243SCesar Eduardo Barros { 287253cbb243SCesar Eduardo Barros struct swap_info_struct *p; 287391a27b2aSJeff Layton struct filename *name; 287453cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 287553cbb243SCesar Eduardo Barros struct address_space *mapping; 287640531542SCesar Eduardo Barros int prio; 287753cbb243SCesar Eduardo Barros int error; 287853cbb243SCesar Eduardo Barros union swap_header *swap_header; 2879915d4d7bSCesar Eduardo Barros int nr_extents; 288053cbb243SCesar Eduardo Barros sector_t span; 288153cbb243SCesar Eduardo Barros unsigned long maxpages; 288253cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 28832a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 288438b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 288553cbb243SCesar Eduardo Barros struct page *page = NULL; 288653cbb243SCesar Eduardo Barros struct inode *inode = NULL; 288753cbb243SCesar Eduardo Barros 2888d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 2889d15cab97SHugh Dickins return -EINVAL; 2890d15cab97SHugh Dickins 289153cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 289253cbb243SCesar Eduardo Barros return -EPERM; 289353cbb243SCesar Eduardo Barros 289453cbb243SCesar Eduardo Barros p = alloc_swap_info(); 28952542e513SCesar Eduardo Barros if (IS_ERR(p)) 28962542e513SCesar Eduardo Barros return PTR_ERR(p); 289753cbb243SCesar Eduardo Barros 2898815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 2899815c2c54SShaohua Li 29001da177e4SLinus Torvalds name = getname(specialfile); 29011da177e4SLinus Torvalds if (IS_ERR(name)) { 29027de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 29031da177e4SLinus Torvalds name = NULL; 2904bd69010bSCesar Eduardo Barros goto bad_swap; 29051da177e4SLinus Torvalds } 2906669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 29071da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 29087de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 29091da177e4SLinus Torvalds swap_file = NULL; 2910bd69010bSCesar Eduardo Barros goto bad_swap; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds p->swap_file = swap_file; 29141da177e4SLinus Torvalds mapping = swap_file->f_mapping; 29152130781eSCesar Eduardo Barros inode = mapping->host; 29166f179af8SHugh Dickins 29175955102cSAl Viro /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */ 29184d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 29194d0e1e10SCesar Eduardo Barros if (unlikely(error)) 29201da177e4SLinus Torvalds goto bad_swap; 29211da177e4SLinus Torvalds 29221da177e4SLinus Torvalds /* 29231da177e4SLinus Torvalds * Read the swap header. 29241da177e4SLinus Torvalds */ 29251da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 29261da177e4SLinus Torvalds error = -EINVAL; 29271da177e4SLinus Torvalds goto bad_swap; 29281da177e4SLinus Torvalds } 2929090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 29301da177e4SLinus Torvalds if (IS_ERR(page)) { 29311da177e4SLinus Torvalds error = PTR_ERR(page); 29321da177e4SLinus Torvalds goto bad_swap; 29331da177e4SLinus Torvalds } 293481e33971SHugh Dickins swap_header = kmap(page); 29351da177e4SLinus Torvalds 2936ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 2937ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 29381da177e4SLinus Torvalds error = -EINVAL; 29391da177e4SLinus Torvalds goto bad_swap; 29401da177e4SLinus Torvalds } 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 2943803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 294478ecba08SHugh Dickins if (!swap_map) { 29451da177e4SLinus Torvalds error = -ENOMEM; 29461da177e4SLinus Torvalds goto bad_swap; 29471da177e4SLinus Torvalds } 2948f0571429SMinchan Kim 2949f0571429SMinchan Kim if (bdi_cap_stable_pages_required(inode_to_bdi(inode))) 2950f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 2951f0571429SMinchan Kim 29522a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 29536f179af8SHugh Dickins int cpu; 2954235b6217SHuang, Ying unsigned long ci, nr_cluster; 29556f179af8SHugh Dickins 29562a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 29572a8f9449SShaohua Li /* 29582a8f9449SShaohua Li * select a random position to start with to help wear leveling 29592a8f9449SShaohua Li * SSD 29602a8f9449SShaohua Li */ 29612a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 2962235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 29632a8f9449SShaohua Li 296454f180d3SHuang Ying cluster_info = kvzalloc(nr_cluster * sizeof(*cluster_info), 296554f180d3SHuang Ying GFP_KERNEL); 29662a8f9449SShaohua Li if (!cluster_info) { 29672a8f9449SShaohua Li error = -ENOMEM; 29682a8f9449SShaohua Li goto bad_swap; 29692a8f9449SShaohua Li } 2970235b6217SHuang, Ying 2971235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 2972235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 2973235b6217SHuang, Ying 2974ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 2975ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 2976ebc2a1a6SShaohua Li error = -ENOMEM; 2977ebc2a1a6SShaohua Li goto bad_swap; 2978ebc2a1a6SShaohua Li } 29796f179af8SHugh Dickins for_each_possible_cpu(cpu) { 2980ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 29816f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 2982ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 2983ebc2a1a6SShaohua Li } 29842a8f9449SShaohua Li } 29851da177e4SLinus Torvalds 29861421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 29871421ef3cSCesar Eduardo Barros if (error) 29881421ef3cSCesar Eduardo Barros goto bad_swap; 29891421ef3cSCesar Eduardo Barros 2990915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 29912a8f9449SShaohua Li cluster_info, maxpages, &span); 2992915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 299353092a74SHugh Dickins error = nr_extents; 2994e2244ec2SHugh Dickins goto bad_swap; 299553092a74SHugh Dickins } 299638b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 29978ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 299854f180d3SHuang Ying frontswap_map = kvzalloc(BITS_TO_LONGS(maxpages) * sizeof(long), 299954f180d3SHuang Ying GFP_KERNEL); 30001da177e4SLinus Torvalds 30012a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 3002dcf6b7ddSRafael Aquini /* 3003dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 3004dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 3005dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 3006dcf6b7ddSRafael Aquini * swapon(8) releases. 3007dcf6b7ddSRafael Aquini */ 3008dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 3009dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 3010dcf6b7ddSRafael Aquini 3011dcf6b7ddSRafael Aquini /* 3012dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 3013dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 3014dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 3015dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 3016dcf6b7ddSRafael Aquini */ 3017dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 3018dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 3019dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 3020dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 3021dcf6b7ddSRafael Aquini 3022dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 3023dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 3024dcf6b7ddSRafael Aquini int err = discard_swap(p); 3025dcf6b7ddSRafael Aquini if (unlikely(err)) 3026465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 3027dcf6b7ddSRafael Aquini p, err); 3028dcf6b7ddSRafael Aquini } 3029dcf6b7ddSRafael Aquini } 30306a6ba831SHugh Dickins 30314b3ef9daSHuang, Ying error = init_swap_address_space(p->type, maxpages); 30324b3ef9daSHuang, Ying if (error) 30334b3ef9daSHuang, Ying goto bad_swap; 30344b3ef9daSHuang, Ying 3035fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 303640531542SCesar Eduardo Barros prio = -1; 303778ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 303840531542SCesar Eduardo Barros prio = 303978ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 30402a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 3041c69dbfb8SCesar Eduardo Barros 3042756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 304391a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 3044c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 3045c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 304638b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 3047dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 3048dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 304938b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 3050c69dbfb8SCesar Eduardo Barros 3051fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 305266d7dd51SKay Sievers atomic_inc(&proc_poll_event); 305366d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 305466d7dd51SKay Sievers 30559b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 30569b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 30571da177e4SLinus Torvalds error = 0; 30581da177e4SLinus Torvalds goto out; 30591da177e4SLinus Torvalds bad_swap: 3060ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 3061ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 3062bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 3063f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 3064f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 30651da177e4SLinus Torvalds } 30664cd3bb10SHugh Dickins destroy_swap_extents(p); 3067e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 30685d337b91SHugh Dickins spin_lock(&swap_lock); 30691da177e4SLinus Torvalds p->swap_file = NULL; 30701da177e4SLinus Torvalds p->flags = 0; 30715d337b91SHugh Dickins spin_unlock(&swap_lock); 30721da177e4SLinus Torvalds vfree(swap_map); 30732a8f9449SShaohua Li vfree(cluster_info); 307452c50567SMel Gorman if (swap_file) { 30752130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 30765955102cSAl Viro inode_unlock(inode); 30772130781eSCesar Eduardo Barros inode = NULL; 30782130781eSCesar Eduardo Barros } 30791da177e4SLinus Torvalds filp_close(swap_file, NULL); 308052c50567SMel Gorman } 30811da177e4SLinus Torvalds out: 30821da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 30831da177e4SLinus Torvalds kunmap(page); 308409cbfeafSKirill A. Shutemov put_page(page); 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds if (name) 30871da177e4SLinus Torvalds putname(name); 30889b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 30895955102cSAl Viro inode_unlock(inode); 3090039939a6STim Chen if (!error) 3091039939a6STim Chen enable_swap_slots_cache(); 30921da177e4SLinus Torvalds return error; 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 30961da177e4SLinus Torvalds { 3097efa90a98SHugh Dickins unsigned int type; 30981da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 30991da177e4SLinus Torvalds 31005d337b91SHugh Dickins spin_lock(&swap_lock); 3101efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 3102efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 3103efa90a98SHugh Dickins 3104efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 3105efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 31061da177e4SLinus Torvalds } 3107ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 31081da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 31095d337b91SHugh Dickins spin_unlock(&swap_lock); 31101da177e4SLinus Torvalds } 31111da177e4SLinus Torvalds 31121da177e4SLinus Torvalds /* 31131da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 31141da177e4SLinus Torvalds * 3115355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 3116355cfa73SKAMEZAWA Hiroyuki * - success -> 0 3117355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 3118355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 3119355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 3120355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 3121570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 31221da177e4SLinus Torvalds */ 31238d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 31241da177e4SLinus Torvalds { 31251da177e4SLinus Torvalds struct swap_info_struct *p; 3126235b6217SHuang, Ying struct swap_cluster_info *ci; 31271da177e4SLinus Torvalds unsigned long offset, type; 31288d69aaeeSHugh Dickins unsigned char count; 31298d69aaeeSHugh Dickins unsigned char has_cache; 3130253d553bSHugh Dickins int err = -EINVAL; 31311da177e4SLinus Torvalds 3132a7420aa5SAndi Kleen if (non_swap_entry(entry)) 3133253d553bSHugh Dickins goto out; 31340697212aSChristoph Lameter 31351da177e4SLinus Torvalds type = swp_type(entry); 31361da177e4SLinus Torvalds if (type >= nr_swapfiles) 31371da177e4SLinus Torvalds goto bad_file; 3138efa90a98SHugh Dickins p = swap_info[type]; 31391da177e4SLinus Torvalds offset = swp_offset(entry); 3140355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 3141235b6217SHuang, Ying goto out; 3142235b6217SHuang, Ying 3143235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 3144355cfa73SKAMEZAWA Hiroyuki 3145253d553bSHugh Dickins count = p->swap_map[offset]; 3146edfe23daSShaohua Li 3147edfe23daSShaohua Li /* 3148edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 3149edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 3150edfe23daSShaohua Li */ 3151edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 3152edfe23daSShaohua Li err = -ENOENT; 3153edfe23daSShaohua Li goto unlock_out; 3154edfe23daSShaohua Li } 3155edfe23daSShaohua Li 3156253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 3157253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 3158253d553bSHugh Dickins err = 0; 3159355cfa73SKAMEZAWA Hiroyuki 3160253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 3161355cfa73SKAMEZAWA Hiroyuki 3162355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 3163253d553bSHugh Dickins if (!has_cache && count) 3164253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 3165253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 3166253d553bSHugh Dickins err = -EEXIST; 3167253d553bSHugh Dickins else /* no users remaining */ 3168253d553bSHugh Dickins err = -ENOENT; 3169355cfa73SKAMEZAWA Hiroyuki 3170355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 3171253d553bSHugh Dickins 3172570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 3173570a335bSHugh Dickins count += usage; 3174570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 3175253d553bSHugh Dickins err = -EINVAL; 3176570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 3177570a335bSHugh Dickins count = COUNT_CONTINUED; 3178570a335bSHugh Dickins else 3179570a335bSHugh Dickins err = -ENOMEM; 3180253d553bSHugh Dickins } else 3181253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 3182253d553bSHugh Dickins 3183253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 3184253d553bSHugh Dickins 3185355cfa73SKAMEZAWA Hiroyuki unlock_out: 3186235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 31871da177e4SLinus Torvalds out: 3188253d553bSHugh Dickins return err; 31891da177e4SLinus Torvalds 31901da177e4SLinus Torvalds bad_file: 3191465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 31921da177e4SLinus Torvalds goto out; 31931da177e4SLinus Torvalds } 3194253d553bSHugh Dickins 3195355cfa73SKAMEZAWA Hiroyuki /* 3196aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 3197aaa46865SHugh Dickins * (in which case its reference count is never incremented). 3198aaa46865SHugh Dickins */ 3199aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 3200aaa46865SHugh Dickins { 3201aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 3202aaa46865SHugh Dickins } 3203aaa46865SHugh Dickins 3204aaa46865SHugh Dickins /* 320508259d58SHugh Dickins * Increase reference count of swap entry by 1. 320608259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 320708259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 320808259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 320908259d58SHugh Dickins * might occur if a page table entry has got corrupted. 3210355cfa73SKAMEZAWA Hiroyuki */ 3211570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 3212355cfa73SKAMEZAWA Hiroyuki { 3213570a335bSHugh Dickins int err = 0; 3214570a335bSHugh Dickins 3215570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 3216570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 3217570a335bSHugh Dickins return err; 3218355cfa73SKAMEZAWA Hiroyuki } 32191da177e4SLinus Torvalds 3220cb4b86baSKAMEZAWA Hiroyuki /* 3221355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 3222355cfa73SKAMEZAWA Hiroyuki * 322373c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 3224355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 3225355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 3226355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 3227cb4b86baSKAMEZAWA Hiroyuki */ 3228cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 3229cb4b86baSKAMEZAWA Hiroyuki { 3230253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 3231cb4b86baSKAMEZAWA Hiroyuki } 3232cb4b86baSKAMEZAWA Hiroyuki 3233f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 3234f981c595SMel Gorman { 3235f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3236f981c595SMel Gorman return swap_info[swp_type(swap)]; 3237f981c595SMel Gorman } 3238f981c595SMel Gorman 3239f981c595SMel Gorman /* 3240f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 3241f981c595SMel Gorman */ 3242f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 3243f981c595SMel Gorman { 3244309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3245f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 3246f981c595SMel Gorman } 3247f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 3248f981c595SMel Gorman 3249f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 3250f981c595SMel Gorman { 3251f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3252309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3253f981c595SMel Gorman return swp_offset(swap); 3254f981c595SMel Gorman } 3255f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 3256f981c595SMel Gorman 32571da177e4SLinus Torvalds /* 3258570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 3259570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 3260570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 3261570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 3262570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 3263570a335bSHugh Dickins * 3264570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 3265570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 3266570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 3267570a335bSHugh Dickins * 3268570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 3269570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 3270570a335bSHugh Dickins * can be called after dropping locks. 3271570a335bSHugh Dickins */ 3272570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 3273570a335bSHugh Dickins { 3274570a335bSHugh Dickins struct swap_info_struct *si; 3275235b6217SHuang, Ying struct swap_cluster_info *ci; 3276570a335bSHugh Dickins struct page *head; 3277570a335bSHugh Dickins struct page *page; 3278570a335bSHugh Dickins struct page *list_page; 3279570a335bSHugh Dickins pgoff_t offset; 3280570a335bSHugh Dickins unsigned char count; 3281570a335bSHugh Dickins 3282570a335bSHugh Dickins /* 3283570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 3284570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 3285570a335bSHugh Dickins */ 3286570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 3287570a335bSHugh Dickins 3288570a335bSHugh Dickins si = swap_info_get(entry); 3289570a335bSHugh Dickins if (!si) { 3290570a335bSHugh Dickins /* 3291570a335bSHugh Dickins * An acceptable race has occurred since the failing 3292570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 3293570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 3294570a335bSHugh Dickins */ 3295570a335bSHugh Dickins goto outer; 3296570a335bSHugh Dickins } 3297570a335bSHugh Dickins 3298570a335bSHugh Dickins offset = swp_offset(entry); 3299235b6217SHuang, Ying 3300235b6217SHuang, Ying ci = lock_cluster(si, offset); 3301235b6217SHuang, Ying 3302570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 3303570a335bSHugh Dickins 3304570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 3305570a335bSHugh Dickins /* 3306570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 3307570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 3308570a335bSHugh Dickins * over-provisioning. 3309570a335bSHugh Dickins */ 3310570a335bSHugh Dickins goto out; 3311570a335bSHugh Dickins } 3312570a335bSHugh Dickins 3313570a335bSHugh Dickins if (!page) { 3314235b6217SHuang, Ying unlock_cluster(ci); 3315ec8acf20SShaohua Li spin_unlock(&si->lock); 3316570a335bSHugh Dickins return -ENOMEM; 3317570a335bSHugh Dickins } 3318570a335bSHugh Dickins 3319570a335bSHugh Dickins /* 3320570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 3321570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 3322570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 3323570a335bSHugh Dickins */ 3324570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3325570a335bSHugh Dickins offset &= ~PAGE_MASK; 3326570a335bSHugh Dickins 3327570a335bSHugh Dickins /* 3328570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 3329570a335bSHugh Dickins * but it does always reset its private field. 3330570a335bSHugh Dickins */ 3331570a335bSHugh Dickins if (!page_private(head)) { 3332570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3333570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 3334570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 3335570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 3336570a335bSHugh Dickins } 3337570a335bSHugh Dickins 3338570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 3339570a335bSHugh Dickins unsigned char *map; 3340570a335bSHugh Dickins 3341570a335bSHugh Dickins /* 3342570a335bSHugh Dickins * If the previous map said no continuation, but we've found 3343570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 3344570a335bSHugh Dickins */ 3345570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 3346570a335bSHugh Dickins goto out; 3347570a335bSHugh Dickins 33489b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 3349570a335bSHugh Dickins count = *map; 33509b04c5feSCong Wang kunmap_atomic(map); 3351570a335bSHugh Dickins 3352570a335bSHugh Dickins /* 3353570a335bSHugh Dickins * If this continuation count now has some space in it, 3354570a335bSHugh Dickins * free our allocation and use this one. 3355570a335bSHugh Dickins */ 3356570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 3357570a335bSHugh Dickins goto out; 3358570a335bSHugh Dickins } 3359570a335bSHugh Dickins 3360570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 3361570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 3362570a335bSHugh Dickins out: 3363235b6217SHuang, Ying unlock_cluster(ci); 3364ec8acf20SShaohua Li spin_unlock(&si->lock); 3365570a335bSHugh Dickins outer: 3366570a335bSHugh Dickins if (page) 3367570a335bSHugh Dickins __free_page(page); 3368570a335bSHugh Dickins return 0; 3369570a335bSHugh Dickins } 3370570a335bSHugh Dickins 3371570a335bSHugh Dickins /* 3372570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3373570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3374570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3375570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3376570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3377235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3378235b6217SHuang, Ying * lock. 3379570a335bSHugh Dickins */ 3380570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3381570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3382570a335bSHugh Dickins { 3383570a335bSHugh Dickins struct page *head; 3384570a335bSHugh Dickins struct page *page; 3385570a335bSHugh Dickins unsigned char *map; 3386570a335bSHugh Dickins 3387570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3388570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3389570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3390570a335bSHugh Dickins return false; /* need to add count continuation */ 3391570a335bSHugh Dickins } 3392570a335bSHugh Dickins 3393570a335bSHugh Dickins offset &= ~PAGE_MASK; 3394570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 33959b04c5feSCong Wang map = kmap_atomic(page) + offset; 3396570a335bSHugh Dickins 3397570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3398570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3399570a335bSHugh Dickins 3400570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3401570a335bSHugh Dickins /* 3402570a335bSHugh Dickins * Think of how you add 1 to 999 3403570a335bSHugh Dickins */ 3404570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 34059b04c5feSCong Wang kunmap_atomic(map); 3406570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3407570a335bSHugh Dickins BUG_ON(page == head); 34089b04c5feSCong Wang map = kmap_atomic(page) + offset; 3409570a335bSHugh Dickins } 3410570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 34119b04c5feSCong Wang kunmap_atomic(map); 3412570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3413570a335bSHugh Dickins if (page == head) 3414570a335bSHugh Dickins return false; /* add count continuation */ 34159b04c5feSCong Wang map = kmap_atomic(page) + offset; 3416570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3417570a335bSHugh Dickins } 3418570a335bSHugh Dickins *map += 1; 34199b04c5feSCong Wang kunmap_atomic(map); 3420570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3421570a335bSHugh Dickins while (page != head) { 34229b04c5feSCong Wang map = kmap_atomic(page) + offset; 3423570a335bSHugh Dickins *map = COUNT_CONTINUED; 34249b04c5feSCong Wang kunmap_atomic(map); 3425570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3426570a335bSHugh Dickins } 3427570a335bSHugh Dickins return true; /* incremented */ 3428570a335bSHugh Dickins 3429570a335bSHugh Dickins } else { /* decrementing */ 3430570a335bSHugh Dickins /* 3431570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3432570a335bSHugh Dickins */ 3433570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3434570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 34359b04c5feSCong Wang kunmap_atomic(map); 3436570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3437570a335bSHugh Dickins BUG_ON(page == head); 34389b04c5feSCong Wang map = kmap_atomic(page) + offset; 3439570a335bSHugh Dickins } 3440570a335bSHugh Dickins BUG_ON(*map == 0); 3441570a335bSHugh Dickins *map -= 1; 3442570a335bSHugh Dickins if (*map == 0) 3443570a335bSHugh Dickins count = 0; 34449b04c5feSCong Wang kunmap_atomic(map); 3445570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3446570a335bSHugh Dickins while (page != head) { 34479b04c5feSCong Wang map = kmap_atomic(page) + offset; 3448570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3449570a335bSHugh Dickins count = COUNT_CONTINUED; 34509b04c5feSCong Wang kunmap_atomic(map); 3451570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3452570a335bSHugh Dickins } 3453570a335bSHugh Dickins return count == COUNT_CONTINUED; 3454570a335bSHugh Dickins } 3455570a335bSHugh Dickins } 3456570a335bSHugh Dickins 3457570a335bSHugh Dickins /* 3458570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3459570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3460570a335bSHugh Dickins */ 3461570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3462570a335bSHugh Dickins { 3463570a335bSHugh Dickins pgoff_t offset; 3464570a335bSHugh Dickins 3465570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3466570a335bSHugh Dickins struct page *head; 3467570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3468570a335bSHugh Dickins if (page_private(head)) { 34690d576d20SGeliang Tang struct page *page, *next; 34700d576d20SGeliang Tang 34710d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 34720d576d20SGeliang Tang list_del(&page->lru); 3473570a335bSHugh Dickins __free_page(page); 3474570a335bSHugh Dickins } 3475570a335bSHugh Dickins } 3476570a335bSHugh Dickins } 3477570a335bSHugh Dickins } 3478