11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/swapfile.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/mm.h> 91da177e4SLinus Torvalds #include <linux/hugetlb.h> 101da177e4SLinus Torvalds #include <linux/mman.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/kernel_stat.h> 131da177e4SLinus Torvalds #include <linux/swap.h> 141da177e4SLinus Torvalds #include <linux/vmalloc.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 17072441e2SHugh Dickins #include <linux/shmem_fs.h> 181da177e4SLinus Torvalds #include <linux/blkdev.h> 1920137a49SHugh Dickins #include <linux/random.h> 201da177e4SLinus Torvalds #include <linux/writeback.h> 211da177e4SLinus Torvalds #include <linux/proc_fs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 231da177e4SLinus Torvalds #include <linux/init.h> 245ad64688SHugh Dickins #include <linux/ksm.h> 251da177e4SLinus Torvalds #include <linux/rmap.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28fc0abb14SIngo Molnar #include <linux/mutex.h> 29c59ede7bSRandy.Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/syscalls.h> 318a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3266d7dd51SKay Sievers #include <linux/poll.h> 3372788c38SDavid Rientjes #include <linux/oom.h> 3438b5faf4SDan Magenheimer #include <linux/frontswap.h> 3538b5faf4SDan Magenheimer #include <linux/swapfile.h> 36f981c595SMel Gorman #include <linux/export.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/pgtable.h> 391da177e4SLinus Torvalds #include <asm/tlbflush.h> 401da177e4SLinus Torvalds #include <linux/swapops.h> 415d1ea48bSJohannes Weiner #include <linux/swap_cgroup.h> 421da177e4SLinus Torvalds 43570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 44570a335bSHugh Dickins unsigned char); 45570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 46d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 47570a335bSHugh Dickins 4838b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 497c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 50ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 51fb0fec50SChris Wilson /* 52fb0fec50SChris Wilson * Some modules use swappable objects and may try to swap them out under 53fb0fec50SChris Wilson * memory pressure (via the shrinker). Before doing so, they may wish to 54fb0fec50SChris Wilson * check to see if any swap space is available. 55fb0fec50SChris Wilson */ 56fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages); 57ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 581da177e4SLinus Torvalds long total_swap_pages; 5978ecba08SHugh Dickins static int least_priority; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 621da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 631da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 641da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 651da177e4SLinus Torvalds 66adfab836SDan Streetman /* 67adfab836SDan Streetman * all active swap_info_structs 68adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 69adfab836SDan Streetman */ 7018ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 7118ab4d4cSDan Streetman 7218ab4d4cSDan Streetman /* 7318ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 7418ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 7518ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 7618ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 7718ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 7818ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 7918ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 8018ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 8118ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 8218ab4d4cSDan Streetman * before any swap_info_struct->lock. 8318ab4d4cSDan Streetman */ 8418ab4d4cSDan Streetman static PLIST_HEAD(swap_avail_head); 8518ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 861da177e4SLinus Torvalds 8738b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 881da177e4SLinus Torvalds 89fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 901da177e4SLinus Torvalds 9166d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 9266d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 9366d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 9466d7dd51SKay Sievers 958d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 96355cfa73SKAMEZAWA Hiroyuki { 97570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 98355cfa73SKAMEZAWA Hiroyuki } 99355cfa73SKAMEZAWA Hiroyuki 100efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 101c9e44410SKAMEZAWA Hiroyuki static int 102c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 103c9e44410SKAMEZAWA Hiroyuki { 104efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 105c9e44410SKAMEZAWA Hiroyuki struct page *page; 106c9e44410SKAMEZAWA Hiroyuki int ret = 0; 107c9e44410SKAMEZAWA Hiroyuki 108f6ab1f7fSHuang Ying page = find_get_page(swap_address_space(entry), swp_offset(entry)); 109c9e44410SKAMEZAWA Hiroyuki if (!page) 110c9e44410SKAMEZAWA Hiroyuki return 0; 111c9e44410SKAMEZAWA Hiroyuki /* 112c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 113c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 114c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 115c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 116c9e44410SKAMEZAWA Hiroyuki * in usual operations. 117c9e44410SKAMEZAWA Hiroyuki */ 118c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 119c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 120c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 121c9e44410SKAMEZAWA Hiroyuki } 12209cbfeafSKirill A. Shutemov put_page(page); 123c9e44410SKAMEZAWA Hiroyuki return ret; 124c9e44410SKAMEZAWA Hiroyuki } 125355cfa73SKAMEZAWA Hiroyuki 1261da177e4SLinus Torvalds /* 1276a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1286a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1296a6ba831SHugh Dickins */ 1306a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1316a6ba831SHugh Dickins { 1326a6ba831SHugh Dickins struct swap_extent *se; 1339625a5f2SHugh Dickins sector_t start_block; 1349625a5f2SHugh Dickins sector_t nr_blocks; 1356a6ba831SHugh Dickins int err = 0; 1366a6ba831SHugh Dickins 1376a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1389625a5f2SHugh Dickins se = &si->first_swap_extent; 1399625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1409625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1419625a5f2SHugh Dickins if (nr_blocks) { 1429625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 143dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1449625a5f2SHugh Dickins if (err) 1459625a5f2SHugh Dickins return err; 1469625a5f2SHugh Dickins cond_resched(); 1476a6ba831SHugh Dickins } 1486a6ba831SHugh Dickins 1499625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1509625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1519625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1529625a5f2SHugh Dickins 1536a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 154dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1556a6ba831SHugh Dickins if (err) 1566a6ba831SHugh Dickins break; 1576a6ba831SHugh Dickins 1586a6ba831SHugh Dickins cond_resched(); 1596a6ba831SHugh Dickins } 1606a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1616a6ba831SHugh Dickins } 1626a6ba831SHugh Dickins 1637992fde7SHugh Dickins /* 1647992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1657992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1667992fde7SHugh Dickins */ 1677992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1687992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1697992fde7SHugh Dickins { 1707992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1717992fde7SHugh Dickins int found_extent = 0; 1727992fde7SHugh Dickins 1737992fde7SHugh Dickins while (nr_pages) { 1747992fde7SHugh Dickins if (se->start_page <= start_page && 1757992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1767992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1777992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 178858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1797992fde7SHugh Dickins 1807992fde7SHugh Dickins if (nr_blocks > nr_pages) 1817992fde7SHugh Dickins nr_blocks = nr_pages; 1827992fde7SHugh Dickins start_page += nr_blocks; 1837992fde7SHugh Dickins nr_pages -= nr_blocks; 1847992fde7SHugh Dickins 1857992fde7SHugh Dickins if (!found_extent++) 1867992fde7SHugh Dickins si->curr_swap_extent = se; 1877992fde7SHugh Dickins 1887992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1897992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1907992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 191dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1927992fde7SHugh Dickins break; 1937992fde7SHugh Dickins } 1947992fde7SHugh Dickins 195a8ae4991SGeliang Tang se = list_next_entry(se, list); 1967992fde7SHugh Dickins } 1977992fde7SHugh Dickins } 1987992fde7SHugh Dickins 199048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 200048c27fdSHugh Dickins #define LATENCY_LIMIT 256 201048c27fdSHugh Dickins 2022a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2032a8f9449SShaohua Li unsigned int flag) 2042a8f9449SShaohua Li { 2052a8f9449SShaohua Li info->flags = flag; 2062a8f9449SShaohua Li } 2072a8f9449SShaohua Li 2082a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2092a8f9449SShaohua Li { 2102a8f9449SShaohua Li return info->data; 2112a8f9449SShaohua Li } 2122a8f9449SShaohua Li 2132a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2142a8f9449SShaohua Li unsigned int c) 2152a8f9449SShaohua Li { 2162a8f9449SShaohua Li info->data = c; 2172a8f9449SShaohua Li } 2182a8f9449SShaohua Li 2192a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2202a8f9449SShaohua Li unsigned int c, unsigned int f) 2212a8f9449SShaohua Li { 2222a8f9449SShaohua Li info->flags = f; 2232a8f9449SShaohua Li info->data = c; 2242a8f9449SShaohua Li } 2252a8f9449SShaohua Li 2262a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2272a8f9449SShaohua Li { 2282a8f9449SShaohua Li return info->data; 2292a8f9449SShaohua Li } 2302a8f9449SShaohua Li 2312a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2322a8f9449SShaohua Li unsigned int n) 2332a8f9449SShaohua Li { 2342a8f9449SShaohua Li info->data = n; 2352a8f9449SShaohua Li } 2362a8f9449SShaohua Li 2372a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 2382a8f9449SShaohua Li unsigned int n, unsigned int f) 2392a8f9449SShaohua Li { 2402a8f9449SShaohua Li info->flags = f; 2412a8f9449SShaohua Li info->data = n; 2422a8f9449SShaohua Li } 2432a8f9449SShaohua Li 2442a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 2452a8f9449SShaohua Li { 2462a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 2472a8f9449SShaohua Li } 2482a8f9449SShaohua Li 2492a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 2502a8f9449SShaohua Li { 2512a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 2522a8f9449SShaohua Li } 2532a8f9449SShaohua Li 2542a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 2552a8f9449SShaohua Li { 2562a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 2572a8f9449SShaohua Li info->data = 0; 2582a8f9449SShaohua Li } 2592a8f9449SShaohua Li 260*235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 261*235b6217SHuang, Ying unsigned long offset) 262*235b6217SHuang, Ying { 263*235b6217SHuang, Ying struct swap_cluster_info *ci; 264*235b6217SHuang, Ying 265*235b6217SHuang, Ying ci = si->cluster_info; 266*235b6217SHuang, Ying if (ci) { 267*235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 268*235b6217SHuang, Ying spin_lock(&ci->lock); 269*235b6217SHuang, Ying } 270*235b6217SHuang, Ying return ci; 271*235b6217SHuang, Ying } 272*235b6217SHuang, Ying 273*235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 274*235b6217SHuang, Ying { 275*235b6217SHuang, Ying if (ci) 276*235b6217SHuang, Ying spin_unlock(&ci->lock); 277*235b6217SHuang, Ying } 278*235b6217SHuang, Ying 279*235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 280*235b6217SHuang, Ying struct swap_info_struct *si, 281*235b6217SHuang, Ying unsigned long offset) 282*235b6217SHuang, Ying { 283*235b6217SHuang, Ying struct swap_cluster_info *ci; 284*235b6217SHuang, Ying 285*235b6217SHuang, Ying ci = lock_cluster(si, offset); 286*235b6217SHuang, Ying if (!ci) 287*235b6217SHuang, Ying spin_lock(&si->lock); 288*235b6217SHuang, Ying 289*235b6217SHuang, Ying return ci; 290*235b6217SHuang, Ying } 291*235b6217SHuang, Ying 292*235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 293*235b6217SHuang, Ying struct swap_cluster_info *ci) 294*235b6217SHuang, Ying { 295*235b6217SHuang, Ying if (ci) 296*235b6217SHuang, Ying unlock_cluster(ci); 297*235b6217SHuang, Ying else 298*235b6217SHuang, Ying spin_unlock(&si->lock); 299*235b6217SHuang, Ying } 300*235b6217SHuang, Ying 3016b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3026b534915SHuang Ying { 3036b534915SHuang Ying return cluster_is_null(&list->head); 3046b534915SHuang Ying } 3056b534915SHuang Ying 3066b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 3076b534915SHuang Ying { 3086b534915SHuang Ying return cluster_next(&list->head); 3096b534915SHuang Ying } 3106b534915SHuang Ying 3116b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 3126b534915SHuang Ying { 3136b534915SHuang Ying cluster_set_null(&list->head); 3146b534915SHuang Ying cluster_set_null(&list->tail); 3156b534915SHuang Ying } 3166b534915SHuang Ying 3176b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 3186b534915SHuang Ying struct swap_cluster_info *ci, 3196b534915SHuang Ying unsigned int idx) 3206b534915SHuang Ying { 3216b534915SHuang Ying if (cluster_list_empty(list)) { 3226b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 3236b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3246b534915SHuang Ying } else { 325*235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 3266b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 3276b534915SHuang Ying 328*235b6217SHuang, Ying /* 329*235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 330*235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 331*235b6217SHuang, Ying */ 332*235b6217SHuang, Ying ci_tail = ci + tail; 333*235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 334*235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 335*235b6217SHuang, Ying unlock_cluster(ci_tail); 3366b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3376b534915SHuang Ying } 3386b534915SHuang Ying } 3396b534915SHuang Ying 3406b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 3416b534915SHuang Ying struct swap_cluster_info *ci) 3426b534915SHuang Ying { 3436b534915SHuang Ying unsigned int idx; 3446b534915SHuang Ying 3456b534915SHuang Ying idx = cluster_next(&list->head); 3466b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 3476b534915SHuang Ying cluster_set_null(&list->head); 3486b534915SHuang Ying cluster_set_null(&list->tail); 3496b534915SHuang Ying } else 3506b534915SHuang Ying cluster_set_next_flag(&list->head, 3516b534915SHuang Ying cluster_next(&ci[idx]), 0); 3526b534915SHuang Ying 3536b534915SHuang Ying return idx; 3546b534915SHuang Ying } 3556b534915SHuang Ying 356815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 357815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 358815c2c54SShaohua Li unsigned int idx) 359815c2c54SShaohua Li { 360815c2c54SShaohua Li /* 361815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 362815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 363815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 364815c2c54SShaohua Li * will be cleared after discard 365815c2c54SShaohua Li */ 366815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 367815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 368815c2c54SShaohua Li 3696b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 370815c2c54SShaohua Li 371815c2c54SShaohua Li schedule_work(&si->discard_work); 372815c2c54SShaohua Li } 373815c2c54SShaohua Li 374815c2c54SShaohua Li /* 375815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 376815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 377815c2c54SShaohua Li */ 378815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 379815c2c54SShaohua Li { 380*235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 381815c2c54SShaohua Li unsigned int idx; 382815c2c54SShaohua Li 383815c2c54SShaohua Li info = si->cluster_info; 384815c2c54SShaohua Li 3856b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 3866b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 387815c2c54SShaohua Li spin_unlock(&si->lock); 388815c2c54SShaohua Li 389815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 390815c2c54SShaohua Li SWAPFILE_CLUSTER); 391815c2c54SShaohua Li 392815c2c54SShaohua Li spin_lock(&si->lock); 393*235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 394*235b6217SHuang, Ying cluster_set_flag(ci, CLUSTER_FLAG_FREE); 395*235b6217SHuang, Ying unlock_cluster(ci); 3966b534915SHuang Ying cluster_list_add_tail(&si->free_clusters, info, idx); 397*235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 398815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 399815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 400*235b6217SHuang, Ying unlock_cluster(ci); 401815c2c54SShaohua Li } 402815c2c54SShaohua Li } 403815c2c54SShaohua Li 404815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 405815c2c54SShaohua Li { 406815c2c54SShaohua Li struct swap_info_struct *si; 407815c2c54SShaohua Li 408815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 409815c2c54SShaohua Li 410815c2c54SShaohua Li spin_lock(&si->lock); 411815c2c54SShaohua Li swap_do_scheduled_discard(si); 412815c2c54SShaohua Li spin_unlock(&si->lock); 413815c2c54SShaohua Li } 414815c2c54SShaohua Li 4152a8f9449SShaohua Li /* 4162a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 4172a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 4182a8f9449SShaohua Li */ 4192a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 4202a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4212a8f9449SShaohua Li { 4222a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4232a8f9449SShaohua Li 4242a8f9449SShaohua Li if (!cluster_info) 4252a8f9449SShaohua Li return; 4262a8f9449SShaohua Li if (cluster_is_free(&cluster_info[idx])) { 4276b534915SHuang Ying VM_BUG_ON(cluster_list_first(&p->free_clusters) != idx); 4286b534915SHuang Ying cluster_list_del_first(&p->free_clusters, cluster_info); 4292a8f9449SShaohua Li cluster_set_count_flag(&cluster_info[idx], 0, 0); 4302a8f9449SShaohua Li } 4312a8f9449SShaohua Li 4322a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 4332a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4342a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 4352a8f9449SShaohua Li } 4362a8f9449SShaohua Li 4372a8f9449SShaohua Li /* 4382a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 4392a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 4402a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 4412a8f9449SShaohua Li */ 4422a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 4432a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4442a8f9449SShaohua Li { 4452a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4462a8f9449SShaohua Li 4472a8f9449SShaohua Li if (!cluster_info) 4482a8f9449SShaohua Li return; 4492a8f9449SShaohua Li 4502a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 4512a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4522a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 4532a8f9449SShaohua Li 4542a8f9449SShaohua Li if (cluster_count(&cluster_info[idx]) == 0) { 455815c2c54SShaohua Li /* 456815c2c54SShaohua Li * If the swap is discardable, prepare discard the cluster 457815c2c54SShaohua Li * instead of free it immediately. The cluster will be freed 458815c2c54SShaohua Li * after discard. 459815c2c54SShaohua Li */ 460edfe23daSShaohua Li if ((p->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 461edfe23daSShaohua Li (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 462815c2c54SShaohua Li swap_cluster_schedule_discard(p, idx); 463815c2c54SShaohua Li return; 464815c2c54SShaohua Li } 465815c2c54SShaohua Li 4662a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 4676b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, idx); 4682a8f9449SShaohua Li } 4692a8f9449SShaohua Li } 4702a8f9449SShaohua Li 4712a8f9449SShaohua Li /* 4722a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 4732a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 4742a8f9449SShaohua Li */ 475ebc2a1a6SShaohua Li static bool 476ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 4772a8f9449SShaohua Li unsigned long offset) 4782a8f9449SShaohua Li { 479ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 480ebc2a1a6SShaohua Li bool conflict; 481ebc2a1a6SShaohua Li 4822a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 4836b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 4846b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 4852a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 486ebc2a1a6SShaohua Li 487ebc2a1a6SShaohua Li if (!conflict) 488ebc2a1a6SShaohua Li return false; 489ebc2a1a6SShaohua Li 490ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 491ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 492ebc2a1a6SShaohua Li return true; 493ebc2a1a6SShaohua Li } 494ebc2a1a6SShaohua Li 495ebc2a1a6SShaohua Li /* 496ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 497ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 498ebc2a1a6SShaohua Li */ 499ebc2a1a6SShaohua Li static void scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 500ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 501ebc2a1a6SShaohua Li { 502ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 503*235b6217SHuang, Ying struct swap_cluster_info *ci; 504ebc2a1a6SShaohua Li bool found_free; 505*235b6217SHuang, Ying unsigned long tmp, max; 506ebc2a1a6SShaohua Li 507ebc2a1a6SShaohua Li new_cluster: 508ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 509ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 5106b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 5116b534915SHuang Ying cluster->index = si->free_clusters.head; 512ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 513ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 5146b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 515ebc2a1a6SShaohua Li /* 516ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 517ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 518ebc2a1a6SShaohua Li */ 519ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 520ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 521ebc2a1a6SShaohua Li goto new_cluster; 522ebc2a1a6SShaohua Li } else 523ebc2a1a6SShaohua Li return; 524ebc2a1a6SShaohua Li } 525ebc2a1a6SShaohua Li 526ebc2a1a6SShaohua Li found_free = false; 527ebc2a1a6SShaohua Li 528ebc2a1a6SShaohua Li /* 529ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 530ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 531ebc2a1a6SShaohua Li */ 532ebc2a1a6SShaohua Li tmp = cluster->next; 533*235b6217SHuang, Ying max = min_t(unsigned long, si->max, 534*235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 535*235b6217SHuang, Ying if (tmp >= max) { 536*235b6217SHuang, Ying cluster_set_null(&cluster->index); 537*235b6217SHuang, Ying goto new_cluster; 538*235b6217SHuang, Ying } 539*235b6217SHuang, Ying ci = lock_cluster(si, tmp); 540*235b6217SHuang, Ying while (tmp < max) { 541ebc2a1a6SShaohua Li if (!si->swap_map[tmp]) { 542ebc2a1a6SShaohua Li found_free = true; 543ebc2a1a6SShaohua Li break; 544ebc2a1a6SShaohua Li } 545ebc2a1a6SShaohua Li tmp++; 546ebc2a1a6SShaohua Li } 547*235b6217SHuang, Ying unlock_cluster(ci); 548ebc2a1a6SShaohua Li if (!found_free) { 549ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 550ebc2a1a6SShaohua Li goto new_cluster; 551ebc2a1a6SShaohua Li } 552ebc2a1a6SShaohua Li cluster->next = tmp + 1; 553ebc2a1a6SShaohua Li *offset = tmp; 554ebc2a1a6SShaohua Li *scan_base = tmp; 5552a8f9449SShaohua Li } 5562a8f9449SShaohua Li 55724b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si, 5588d69aaeeSHugh Dickins unsigned char usage) 5591da177e4SLinus Torvalds { 560*235b6217SHuang, Ying struct swap_cluster_info *ci; 561ebebbbe9SHugh Dickins unsigned long offset; 562c60aa176SHugh Dickins unsigned long scan_base; 5637992fde7SHugh Dickins unsigned long last_in_cluster = 0; 564048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 5651da177e4SLinus Torvalds 5667dfad418SHugh Dickins /* 5677dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 5687dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 5697dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 5707dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 5717dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 5727dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 5737dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 574c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 5751da177e4SLinus Torvalds */ 5767dfad418SHugh Dickins 57752b7efdbSHugh Dickins si->flags += SWP_SCANNING; 578c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 579ebebbbe9SHugh Dickins 580ebc2a1a6SShaohua Li /* SSD algorithm */ 581ebc2a1a6SShaohua Li if (si->cluster_info) { 582ebc2a1a6SShaohua Li scan_swap_map_try_ssd_cluster(si, &offset, &scan_base); 583815c2c54SShaohua Li goto checks; 584815c2c54SShaohua Li } 585815c2c54SShaohua Li 586ebc2a1a6SShaohua Li if (unlikely(!si->cluster_nr--)) { 587ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 588ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 5892a8f9449SShaohua Li goto checks; 5902a8f9449SShaohua Li } 5912a8f9449SShaohua Li 592ec8acf20SShaohua Li spin_unlock(&si->lock); 5937dfad418SHugh Dickins 594c60aa176SHugh Dickins /* 595c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 596c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 59750088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 59850088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 599c60aa176SHugh Dickins */ 600c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 6017dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 6027dfad418SHugh Dickins 6037dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 6047dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 6051da177e4SLinus Torvalds if (si->swap_map[offset]) 6067dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 6077dfad418SHugh Dickins else if (offset == last_in_cluster) { 608ec8acf20SShaohua Li spin_lock(&si->lock); 609ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 610ebebbbe9SHugh Dickins si->cluster_next = offset; 611ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 612ebebbbe9SHugh Dickins goto checks; 6137dfad418SHugh Dickins } 614048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 615048c27fdSHugh Dickins cond_resched(); 616048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 617048c27fdSHugh Dickins } 6187dfad418SHugh Dickins } 619ebebbbe9SHugh Dickins 620c60aa176SHugh Dickins offset = scan_base; 621ec8acf20SShaohua Li spin_lock(&si->lock); 622ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 6237dfad418SHugh Dickins } 6247dfad418SHugh Dickins 625ebebbbe9SHugh Dickins checks: 626ebc2a1a6SShaohua Li if (si->cluster_info) { 627ebc2a1a6SShaohua Li while (scan_swap_map_ssd_cluster_conflict(si, offset)) 628ebc2a1a6SShaohua Li scan_swap_map_try_ssd_cluster(si, &offset, &scan_base); 629ebc2a1a6SShaohua Li } 630ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 63152b7efdbSHugh Dickins goto no_page; 6327dfad418SHugh Dickins if (!si->highest_bit) 6337dfad418SHugh Dickins goto no_page; 634ebebbbe9SHugh Dickins if (offset > si->highest_bit) 635c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 636c9e44410SKAMEZAWA Hiroyuki 637*235b6217SHuang, Ying ci = lock_cluster(si, offset); 638b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 639b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 640c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 641*235b6217SHuang, Ying unlock_cluster(ci); 642ec8acf20SShaohua Li spin_unlock(&si->lock); 643c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 644ec8acf20SShaohua Li spin_lock(&si->lock); 645c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 646c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 647c9e44410SKAMEZAWA Hiroyuki goto checks; 648c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 649c9e44410SKAMEZAWA Hiroyuki } 650c9e44410SKAMEZAWA Hiroyuki 651*235b6217SHuang, Ying if (si->swap_map[offset]) { 652*235b6217SHuang, Ying unlock_cluster(ci); 653ebebbbe9SHugh Dickins goto scan; 654*235b6217SHuang, Ying } 655ebebbbe9SHugh Dickins 65652b7efdbSHugh Dickins if (offset == si->lowest_bit) 6571da177e4SLinus Torvalds si->lowest_bit++; 6581da177e4SLinus Torvalds if (offset == si->highest_bit) 6591da177e4SLinus Torvalds si->highest_bit--; 6607dfad418SHugh Dickins si->inuse_pages++; 6617dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 6621da177e4SLinus Torvalds si->lowest_bit = si->max; 6631da177e4SLinus Torvalds si->highest_bit = 0; 66418ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 66518ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 66618ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 6671da177e4SLinus Torvalds } 668253d553bSHugh Dickins si->swap_map[offset] = usage; 6692a8f9449SShaohua Li inc_cluster_info_page(si, si->cluster_info, offset); 670*235b6217SHuang, Ying unlock_cluster(ci); 6711da177e4SLinus Torvalds si->cluster_next = offset + 1; 67252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 6737992fde7SHugh Dickins 6741da177e4SLinus Torvalds return offset; 6757dfad418SHugh Dickins 676ebebbbe9SHugh Dickins scan: 677ec8acf20SShaohua Li spin_unlock(&si->lock); 6787dfad418SHugh Dickins while (++offset <= si->highest_bit) { 67952b7efdbSHugh Dickins if (!si->swap_map[offset]) { 680ec8acf20SShaohua Li spin_lock(&si->lock); 68152b7efdbSHugh Dickins goto checks; 6827dfad418SHugh Dickins } 683c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 684ec8acf20SShaohua Li spin_lock(&si->lock); 685c9e44410SKAMEZAWA Hiroyuki goto checks; 686c9e44410SKAMEZAWA Hiroyuki } 687048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 688048c27fdSHugh Dickins cond_resched(); 689048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 690048c27fdSHugh Dickins } 69152b7efdbSHugh Dickins } 692c60aa176SHugh Dickins offset = si->lowest_bit; 693a5998061SJamie Liu while (offset < scan_base) { 694c60aa176SHugh Dickins if (!si->swap_map[offset]) { 695ec8acf20SShaohua Li spin_lock(&si->lock); 696ebebbbe9SHugh Dickins goto checks; 697c60aa176SHugh Dickins } 698c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 699ec8acf20SShaohua Li spin_lock(&si->lock); 700c9e44410SKAMEZAWA Hiroyuki goto checks; 701c9e44410SKAMEZAWA Hiroyuki } 702c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 703c60aa176SHugh Dickins cond_resched(); 704c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 705c60aa176SHugh Dickins } 706a5998061SJamie Liu offset++; 707c60aa176SHugh Dickins } 708ec8acf20SShaohua Li spin_lock(&si->lock); 7097dfad418SHugh Dickins 7107dfad418SHugh Dickins no_page: 71152b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 7121da177e4SLinus Torvalds return 0; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 7161da177e4SLinus Torvalds { 717adfab836SDan Streetman struct swap_info_struct *si, *next; 718fb4f88dcSHugh Dickins pgoff_t offset; 7191da177e4SLinus Torvalds 720ec8acf20SShaohua Li if (atomic_long_read(&nr_swap_pages) <= 0) 721fb4f88dcSHugh Dickins goto noswap; 722ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 7231da177e4SLinus Torvalds 72418ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 72518ab4d4cSDan Streetman 72618ab4d4cSDan Streetman start_over: 72718ab4d4cSDan Streetman plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { 72818ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 72918ab4d4cSDan Streetman plist_requeue(&si->avail_list, &swap_avail_head); 73018ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 731ec8acf20SShaohua Li spin_lock(&si->lock); 732adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 73318ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 73418ab4d4cSDan Streetman if (plist_node_empty(&si->avail_list)) { 735ec8acf20SShaohua Li spin_unlock(&si->lock); 73618ab4d4cSDan Streetman goto nextsi; 73718ab4d4cSDan Streetman } 73818ab4d4cSDan Streetman WARN(!si->highest_bit, 73918ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 74018ab4d4cSDan Streetman si->type); 74118ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 74218ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 74318ab4d4cSDan Streetman si->type); 74418ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 74518ab4d4cSDan Streetman spin_unlock(&si->lock); 74618ab4d4cSDan Streetman goto nextsi; 747ec8acf20SShaohua Li } 748fb4f88dcSHugh Dickins 749355cfa73SKAMEZAWA Hiroyuki /* This is called for allocating swap entry for cache */ 750253d553bSHugh Dickins offset = scan_swap_map(si, SWAP_HAS_CACHE); 751ec8acf20SShaohua Li spin_unlock(&si->lock); 752ec8acf20SShaohua Li if (offset) 753adfab836SDan Streetman return swp_entry(si->type, offset); 75418ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 75518ab4d4cSDan Streetman si->type); 75618ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 75718ab4d4cSDan Streetman nextsi: 758adfab836SDan Streetman /* 759adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 760adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 761adfab836SDan Streetman * callers probably all tried to get a page from the same si 76218ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 76318ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 76418ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 76518ab4d4cSDan Streetman * list may have been modified; so if next is still in the 76618ab4d4cSDan Streetman * swap_avail_head list then try it, otherwise start over. 767adfab836SDan Streetman */ 76818ab4d4cSDan Streetman if (plist_node_empty(&next->avail_list)) 76918ab4d4cSDan Streetman goto start_over; 770fb4f88dcSHugh Dickins } 771fb4f88dcSHugh Dickins 77218ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 77318ab4d4cSDan Streetman 774ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 775fb4f88dcSHugh Dickins noswap: 776fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 7792de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 780910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 781910321eaSHugh Dickins { 782910321eaSHugh Dickins struct swap_info_struct *si; 783910321eaSHugh Dickins pgoff_t offset; 784910321eaSHugh Dickins 785910321eaSHugh Dickins si = swap_info[type]; 786ec8acf20SShaohua Li spin_lock(&si->lock); 787910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 788ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 789910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 790910321eaSHugh Dickins offset = scan_swap_map(si, 1); 791910321eaSHugh Dickins if (offset) { 792ec8acf20SShaohua Li spin_unlock(&si->lock); 793910321eaSHugh Dickins return swp_entry(type, offset); 794910321eaSHugh Dickins } 795ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 796910321eaSHugh Dickins } 797ec8acf20SShaohua Li spin_unlock(&si->lock); 798910321eaSHugh Dickins return (swp_entry_t) {0}; 799910321eaSHugh Dickins } 800910321eaSHugh Dickins 801*235b6217SHuang, Ying static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds struct swap_info_struct *p; 8041da177e4SLinus Torvalds unsigned long offset, type; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds if (!entry.val) 8071da177e4SLinus Torvalds goto out; 8081da177e4SLinus Torvalds type = swp_type(entry); 8091da177e4SLinus Torvalds if (type >= nr_swapfiles) 8101da177e4SLinus Torvalds goto bad_nofile; 811efa90a98SHugh Dickins p = swap_info[type]; 8121da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 8131da177e4SLinus Torvalds goto bad_device; 8141da177e4SLinus Torvalds offset = swp_offset(entry); 8151da177e4SLinus Torvalds if (offset >= p->max) 8161da177e4SLinus Torvalds goto bad_offset; 8171da177e4SLinus Torvalds if (!p->swap_map[offset]) 8181da177e4SLinus Torvalds goto bad_free; 8191da177e4SLinus Torvalds return p; 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds bad_free: 8226a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val); 8231da177e4SLinus Torvalds goto out; 8241da177e4SLinus Torvalds bad_offset: 8256a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val); 8261da177e4SLinus Torvalds goto out; 8271da177e4SLinus Torvalds bad_device: 8286a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val); 8291da177e4SLinus Torvalds goto out; 8301da177e4SLinus Torvalds bad_nofile: 8316a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val); 8321da177e4SLinus Torvalds out: 8331da177e4SLinus Torvalds return NULL; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 836*235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 8371da177e4SLinus Torvalds { 838*235b6217SHuang, Ying struct swap_info_struct *p; 839*235b6217SHuang, Ying 840*235b6217SHuang, Ying p = _swap_info_get(entry); 841*235b6217SHuang, Ying if (p) 842*235b6217SHuang, Ying spin_lock(&p->lock); 843*235b6217SHuang, Ying return p; 844*235b6217SHuang, Ying } 845*235b6217SHuang, Ying 846*235b6217SHuang, Ying static unsigned char swap_entry_free(struct swap_info_struct *p, 847*235b6217SHuang, Ying swp_entry_t entry, unsigned char usage, 848*235b6217SHuang, Ying bool swap_info_locked) 849*235b6217SHuang, Ying { 850*235b6217SHuang, Ying struct swap_cluster_info *ci; 851253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 8528d69aaeeSHugh Dickins unsigned char count; 8538d69aaeeSHugh Dickins unsigned char has_cache; 854*235b6217SHuang, Ying bool lock_swap_info = false; 855*235b6217SHuang, Ying 856*235b6217SHuang, Ying if (!swap_info_locked) { 857*235b6217SHuang, Ying count = p->swap_map[offset]; 858*235b6217SHuang, Ying if (!p->cluster_info || count == usage || count == SWAP_MAP_SHMEM) { 859*235b6217SHuang, Ying lock_swap_info: 860*235b6217SHuang, Ying swap_info_locked = true; 861*235b6217SHuang, Ying lock_swap_info = true; 862*235b6217SHuang, Ying spin_lock(&p->lock); 863*235b6217SHuang, Ying } 864*235b6217SHuang, Ying } 865*235b6217SHuang, Ying 866*235b6217SHuang, Ying ci = lock_cluster(p, offset); 8671da177e4SLinus Torvalds 868355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 869*235b6217SHuang, Ying 870*235b6217SHuang, Ying if (!swap_info_locked && (count == usage || count == SWAP_MAP_SHMEM)) { 871*235b6217SHuang, Ying unlock_cluster(ci); 872*235b6217SHuang, Ying goto lock_swap_info; 873*235b6217SHuang, Ying } 874*235b6217SHuang, Ying 875253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 876253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 877253d553bSHugh Dickins 878253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 879253d553bSHugh Dickins VM_BUG_ON(!has_cache); 880253d553bSHugh Dickins has_cache = 0; 881aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 882aaa46865SHugh Dickins /* 883aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 884aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 885aaa46865SHugh Dickins */ 886aaa46865SHugh Dickins count = 0; 887570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 888570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 889570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 890570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 891570a335bSHugh Dickins else 892570a335bSHugh Dickins count = SWAP_MAP_MAX; 893570a335bSHugh Dickins } else 894253d553bSHugh Dickins count--; 895570a335bSHugh Dickins } 896253d553bSHugh Dickins 897253d553bSHugh Dickins usage = count | has_cache; 898253d553bSHugh Dickins p->swap_map[offset] = usage; 899253d553bSHugh Dickins 900*235b6217SHuang, Ying unlock_cluster(ci); 901*235b6217SHuang, Ying 902355cfa73SKAMEZAWA Hiroyuki /* free if no reference */ 903253d553bSHugh Dickins if (!usage) { 904*235b6217SHuang, Ying VM_BUG_ON(!swap_info_locked); 90537e84351SVladimir Davydov mem_cgroup_uncharge_swap(entry); 906*235b6217SHuang, Ying ci = lock_cluster(p, offset); 9072a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 908*235b6217SHuang, Ying unlock_cluster(ci); 9091da177e4SLinus Torvalds if (offset < p->lowest_bit) 9101da177e4SLinus Torvalds p->lowest_bit = offset; 91118ab4d4cSDan Streetman if (offset > p->highest_bit) { 91218ab4d4cSDan Streetman bool was_full = !p->highest_bit; 9131da177e4SLinus Torvalds p->highest_bit = offset; 91418ab4d4cSDan Streetman if (was_full && (p->flags & SWP_WRITEOK)) { 91518ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 91618ab4d4cSDan Streetman WARN_ON(!plist_node_empty(&p->avail_list)); 91718ab4d4cSDan Streetman if (plist_node_empty(&p->avail_list)) 91818ab4d4cSDan Streetman plist_add(&p->avail_list, 91918ab4d4cSDan Streetman &swap_avail_head); 92018ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 92118ab4d4cSDan Streetman } 92218ab4d4cSDan Streetman } 923ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 9241da177e4SLinus Torvalds p->inuse_pages--; 92538b5faf4SDan Magenheimer frontswap_invalidate_page(p->type, offset); 92673744923SMel Gorman if (p->flags & SWP_BLKDEV) { 92773744923SMel Gorman struct gendisk *disk = p->bdev->bd_disk; 92873744923SMel Gorman if (disk->fops->swap_slot_free_notify) 92973744923SMel Gorman disk->fops->swap_slot_free_notify(p->bdev, 93073744923SMel Gorman offset); 93173744923SMel Gorman } 9321da177e4SLinus Torvalds } 933253d553bSHugh Dickins 934*235b6217SHuang, Ying if (lock_swap_info) 935*235b6217SHuang, Ying spin_unlock(&p->lock); 936*235b6217SHuang, Ying 937253d553bSHugh Dickins return usage; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds /* 9411da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 9421da177e4SLinus Torvalds * is still around or has not been recycled. 9431da177e4SLinus Torvalds */ 9441da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 9451da177e4SLinus Torvalds { 9461da177e4SLinus Torvalds struct swap_info_struct *p; 9471da177e4SLinus Torvalds 948*235b6217SHuang, Ying p = _swap_info_get(entry); 949*235b6217SHuang, Ying if (p) 950*235b6217SHuang, Ying swap_entry_free(p, entry, 1, false); 9511da177e4SLinus Torvalds } 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds /* 954cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 955cb4b86baSKAMEZAWA Hiroyuki */ 9560a31bc97SJohannes Weiner void swapcache_free(swp_entry_t entry) 957cb4b86baSKAMEZAWA Hiroyuki { 958355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 959355cfa73SKAMEZAWA Hiroyuki 960*235b6217SHuang, Ying p = _swap_info_get(entry); 961*235b6217SHuang, Ying if (p) 962*235b6217SHuang, Ying swap_entry_free(p, entry, SWAP_HAS_CACHE, false); 963cb4b86baSKAMEZAWA Hiroyuki } 964cb4b86baSKAMEZAWA Hiroyuki 965cb4b86baSKAMEZAWA Hiroyuki /* 966c475a8abSHugh Dickins * How many references to page are currently swapped out? 967570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 968570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 9691da177e4SLinus Torvalds */ 970bde05d1cSHugh Dickins int page_swapcount(struct page *page) 9711da177e4SLinus Torvalds { 972c475a8abSHugh Dickins int count = 0; 9731da177e4SLinus Torvalds struct swap_info_struct *p; 974*235b6217SHuang, Ying struct swap_cluster_info *ci; 9751da177e4SLinus Torvalds swp_entry_t entry; 976*235b6217SHuang, Ying unsigned long offset; 9771da177e4SLinus Torvalds 9784c21e2f2SHugh Dickins entry.val = page_private(page); 979*235b6217SHuang, Ying p = _swap_info_get(entry); 9801da177e4SLinus Torvalds if (p) { 981*235b6217SHuang, Ying offset = swp_offset(entry); 982*235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 983*235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 984*235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 9851da177e4SLinus Torvalds } 986c475a8abSHugh Dickins return count; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds /* 9908334b962SMinchan Kim * How many references to @entry are currently swapped out? 9918334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 9928334b962SMinchan Kim */ 9938334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 9948334b962SMinchan Kim { 9958334b962SMinchan Kim int count, tmp_count, n; 9968334b962SMinchan Kim struct swap_info_struct *p; 997*235b6217SHuang, Ying struct swap_cluster_info *ci; 9988334b962SMinchan Kim struct page *page; 9998334b962SMinchan Kim pgoff_t offset; 10008334b962SMinchan Kim unsigned char *map; 10018334b962SMinchan Kim 1002*235b6217SHuang, Ying p = _swap_info_get(entry); 10038334b962SMinchan Kim if (!p) 10048334b962SMinchan Kim return 0; 10058334b962SMinchan Kim 1006*235b6217SHuang, Ying offset = swp_offset(entry); 1007*235b6217SHuang, Ying 1008*235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1009*235b6217SHuang, Ying 1010*235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 10118334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 10128334b962SMinchan Kim goto out; 10138334b962SMinchan Kim 10148334b962SMinchan Kim count &= ~COUNT_CONTINUED; 10158334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 10168334b962SMinchan Kim 10178334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 10188334b962SMinchan Kim offset &= ~PAGE_MASK; 10198334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 10208334b962SMinchan Kim 10218334b962SMinchan Kim do { 1022a8ae4991SGeliang Tang page = list_next_entry(page, lru); 10238334b962SMinchan Kim map = kmap_atomic(page); 10248334b962SMinchan Kim tmp_count = map[offset]; 10258334b962SMinchan Kim kunmap_atomic(map); 10268334b962SMinchan Kim 10278334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 10288334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 10298334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 10308334b962SMinchan Kim out: 1031*235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 10328334b962SMinchan Kim return count; 10338334b962SMinchan Kim } 10348334b962SMinchan Kim 10358334b962SMinchan Kim /* 10367b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 10377b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 10387b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 10397b1fe597SHugh Dickins * later would only waste time away from clustering. 10406d0a07edSAndrea Arcangeli * 10416d0a07edSAndrea Arcangeli * NOTE: total_mapcount should not be relied upon by the caller if 10426d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 10436d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 10441da177e4SLinus Torvalds */ 10456d0a07edSAndrea Arcangeli bool reuse_swap_page(struct page *page, int *total_mapcount) 10461da177e4SLinus Torvalds { 1047c475a8abSHugh Dickins int count; 10481da177e4SLinus Torvalds 1049309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 10505ad64688SHugh Dickins if (unlikely(PageKsm(page))) 10516d0a07edSAndrea Arcangeli return false; 10526d0a07edSAndrea Arcangeli count = page_trans_huge_mapcount(page, total_mapcount); 10537b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 1054c475a8abSHugh Dickins count += page_swapcount(page); 1055f0571429SMinchan Kim if (count != 1) 1056f0571429SMinchan Kim goto out; 1057f0571429SMinchan Kim if (!PageWriteback(page)) { 10587b1fe597SHugh Dickins delete_from_swap_cache(page); 10597b1fe597SHugh Dickins SetPageDirty(page); 1060f0571429SMinchan Kim } else { 1061f0571429SMinchan Kim swp_entry_t entry; 1062f0571429SMinchan Kim struct swap_info_struct *p; 1063f0571429SMinchan Kim 1064f0571429SMinchan Kim entry.val = page_private(page); 1065f0571429SMinchan Kim p = swap_info_get(entry); 1066f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1067f0571429SMinchan Kim spin_unlock(&p->lock); 1068f0571429SMinchan Kim return false; 1069f0571429SMinchan Kim } 1070f0571429SMinchan Kim spin_unlock(&p->lock); 10717b1fe597SHugh Dickins } 10727b1fe597SHugh Dickins } 1073f0571429SMinchan Kim out: 10745ad64688SHugh Dickins return count <= 1; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds /* 1078a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1079a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 10801da177e4SLinus Torvalds */ 1081a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 10821da177e4SLinus Torvalds { 1083309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds if (!PageSwapCache(page)) 10861da177e4SLinus Torvalds return 0; 10871da177e4SLinus Torvalds if (PageWriteback(page)) 10881da177e4SLinus Torvalds return 0; 1089a2c43eedSHugh Dickins if (page_swapcount(page)) 10901da177e4SLinus Torvalds return 0; 10911da177e4SLinus Torvalds 1092b73d7fceSHugh Dickins /* 1093b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1094b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1095b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1096b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1097b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1098b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1099b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1100b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1101b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1102b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1103b73d7fceSHugh Dickins * 11042de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1105f90ac398SMel Gorman * to disk so check that here. 1106b73d7fceSHugh Dickins */ 1107f90ac398SMel Gorman if (pm_suspended_storage()) 1108b73d7fceSHugh Dickins return 0; 1109b73d7fceSHugh Dickins 1110a2c43eedSHugh Dickins delete_from_swap_cache(page); 11111da177e4SLinus Torvalds SetPageDirty(page); 1112a2c43eedSHugh Dickins return 1; 111368a22394SRik van Riel } 111468a22394SRik van Riel 111568a22394SRik van Riel /* 11161da177e4SLinus Torvalds * Free the swap entry like above, but also try to 11171da177e4SLinus Torvalds * free the page cache entry if it is the last user. 11181da177e4SLinus Torvalds */ 11192509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 11201da177e4SLinus Torvalds { 11211da177e4SLinus Torvalds struct swap_info_struct *p; 11221da177e4SLinus Torvalds struct page *page = NULL; 11231da177e4SLinus Torvalds 1124a7420aa5SAndi Kleen if (non_swap_entry(entry)) 11252509ef26SHugh Dickins return 1; 11260697212aSChristoph Lameter 11271da177e4SLinus Torvalds p = swap_info_get(entry); 11281da177e4SLinus Torvalds if (p) { 1129*235b6217SHuang, Ying if (swap_entry_free(p, entry, 1, true) == SWAP_HAS_CACHE) { 113033806f06SShaohua Li page = find_get_page(swap_address_space(entry), 1131f6ab1f7fSHuang Ying swp_offset(entry)); 11328413ac9dSNick Piggin if (page && !trylock_page(page)) { 113309cbfeafSKirill A. Shutemov put_page(page); 113493fac704SNick Piggin page = NULL; 113593fac704SNick Piggin } 113693fac704SNick Piggin } 1137ec8acf20SShaohua Li spin_unlock(&p->lock); 11381da177e4SLinus Torvalds } 11391da177e4SLinus Torvalds if (page) { 1140a2c43eedSHugh Dickins /* 1141a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 1142a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 1143a2c43eedSHugh Dickins */ 114493fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 11455ccc5abaSVladimir Davydov (!page_mapped(page) || mem_cgroup_swap_full(page))) { 11461da177e4SLinus Torvalds delete_from_swap_cache(page); 11471da177e4SLinus Torvalds SetPageDirty(page); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds unlock_page(page); 115009cbfeafSKirill A. Shutemov put_page(page); 11511da177e4SLinus Torvalds } 11522509ef26SHugh Dickins return p != NULL; 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 1155b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1156f577eb30SRafael J. Wysocki /* 1157915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1158f577eb30SRafael J. Wysocki * 1159915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1160915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1161915bae9eSRafael J. Wysocki * 1162915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1163f577eb30SRafael J. Wysocki */ 11647bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 1165f577eb30SRafael J. Wysocki { 1166915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 1167efa90a98SHugh Dickins int type; 1168f577eb30SRafael J. Wysocki 1169915bae9eSRafael J. Wysocki if (device) 1170915bae9eSRafael J. Wysocki bdev = bdget(device); 1171915bae9eSRafael J. Wysocki 1172f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1173efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1174efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1175f577eb30SRafael J. Wysocki 1176915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1177f577eb30SRafael J. Wysocki continue; 1178b6b5bce3SRafael J. Wysocki 1179915bae9eSRafael J. Wysocki if (!bdev) { 11807bf23687SRafael J. Wysocki if (bdev_p) 1181dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 11827bf23687SRafael J. Wysocki 11836e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1184efa90a98SHugh Dickins return type; 11856e1819d6SRafael J. Wysocki } 1186915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 11879625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 1188915bae9eSRafael J. Wysocki 1189915bae9eSRafael J. Wysocki if (se->start_block == offset) { 11907bf23687SRafael J. Wysocki if (bdev_p) 1191dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 11927bf23687SRafael J. Wysocki 1193f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1194915bae9eSRafael J. Wysocki bdput(bdev); 1195efa90a98SHugh Dickins return type; 1196f577eb30SRafael J. Wysocki } 1197f577eb30SRafael J. Wysocki } 1198915bae9eSRafael J. Wysocki } 1199f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1200915bae9eSRafael J. Wysocki if (bdev) 1201915bae9eSRafael J. Wysocki bdput(bdev); 1202915bae9eSRafael J. Wysocki 1203f577eb30SRafael J. Wysocki return -ENODEV; 1204f577eb30SRafael J. Wysocki } 1205f577eb30SRafael J. Wysocki 1206f577eb30SRafael J. Wysocki /* 120773c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 120873c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 120973c34b6aSHugh Dickins */ 121073c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 121173c34b6aSHugh Dickins { 121273c34b6aSHugh Dickins struct block_device *bdev; 121373c34b6aSHugh Dickins 121473c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 121573c34b6aSHugh Dickins return 0; 121673c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 121773c34b6aSHugh Dickins return 0; 1218d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 121973c34b6aSHugh Dickins } 122073c34b6aSHugh Dickins 122173c34b6aSHugh Dickins /* 1222f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1223f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1224f577eb30SRafael J. Wysocki * 1225f577eb30SRafael J. Wysocki * This is needed for software suspend 1226f577eb30SRafael J. Wysocki */ 1227f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1228f577eb30SRafael J. Wysocki { 1229f577eb30SRafael J. Wysocki unsigned int n = 0; 1230f577eb30SRafael J. Wysocki 1231f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1232efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1233efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1234efa90a98SHugh Dickins 1235ec8acf20SShaohua Li spin_lock(&sis->lock); 1236efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1237efa90a98SHugh Dickins n = sis->pages; 1238f577eb30SRafael J. Wysocki if (free) 1239efa90a98SHugh Dickins n -= sis->inuse_pages; 1240efa90a98SHugh Dickins } 1241ec8acf20SShaohua Li spin_unlock(&sis->lock); 1242f577eb30SRafael J. Wysocki } 1243f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1244f577eb30SRafael J. Wysocki return n; 1245f577eb30SRafael J. Wysocki } 124673c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1247f577eb30SRafael J. Wysocki 12489f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1249179ef71cSCyrill Gorcunov { 12509f8bdb3fSHugh Dickins return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte); 1251179ef71cSCyrill Gorcunov } 1252179ef71cSCyrill Gorcunov 12531da177e4SLinus Torvalds /* 125472866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 125572866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 125672866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 12571da177e4SLinus Torvalds */ 1258044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 12591da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 12601da177e4SLinus Torvalds { 12619e16b7fbSHugh Dickins struct page *swapcache; 126272835c86SJohannes Weiner struct mem_cgroup *memcg; 1263044d66c1SHugh Dickins spinlock_t *ptl; 1264044d66c1SHugh Dickins pte_t *pte; 1265044d66c1SHugh Dickins int ret = 1; 1266044d66c1SHugh Dickins 12679e16b7fbSHugh Dickins swapcache = page; 12689e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 12699e16b7fbSHugh Dickins if (unlikely(!page)) 12709e16b7fbSHugh Dickins return -ENOMEM; 12719e16b7fbSHugh Dickins 1272f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 1273f627c2f5SKirill A. Shutemov &memcg, false)) { 1274044d66c1SHugh Dickins ret = -ENOMEM; 127585d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 127685d9fc89SKAMEZAWA Hiroyuki } 1277044d66c1SHugh Dickins 1278044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 12799f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1280f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1281044d66c1SHugh Dickins ret = 0; 1282044d66c1SHugh Dickins goto out; 1283044d66c1SHugh Dickins } 12848a9f3ccdSBalbir Singh 1285b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1286d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 12871da177e4SLinus Torvalds get_page(page); 12881da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 12891da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 129000501b53SJohannes Weiner if (page == swapcache) { 1291d281ee61SKirill A. Shutemov page_add_anon_rmap(page, vma, addr, false); 1292f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 129300501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1294d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, addr, false); 1295f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 129600501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 129700501b53SJohannes Weiner } 12981da177e4SLinus Torvalds swap_free(entry); 12991da177e4SLinus Torvalds /* 13001da177e4SLinus Torvalds * Move the page to the active list so it is not 13011da177e4SLinus Torvalds * immediately swapped out again after swapon. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds activate_page(page); 1304044d66c1SHugh Dickins out: 1305044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 130685d9fc89SKAMEZAWA Hiroyuki out_nolock: 13079e16b7fbSHugh Dickins if (page != swapcache) { 13089e16b7fbSHugh Dickins unlock_page(page); 13099e16b7fbSHugh Dickins put_page(page); 13109e16b7fbSHugh Dickins } 1311044d66c1SHugh Dickins return ret; 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 13151da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13161da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 13171da177e4SLinus Torvalds { 13181da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1319705e87c0SHugh Dickins pte_t *pte; 13208a9f3ccdSBalbir Singh int ret = 0; 13211da177e4SLinus Torvalds 1322044d66c1SHugh Dickins /* 1323044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1324044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1325044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1326044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1327044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1328044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 13292de1a7e4SSeth Jennings * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1330044d66c1SHugh Dickins */ 1331044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 13321da177e4SLinus Torvalds do { 13331da177e4SLinus Torvalds /* 13341da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 13351da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 13361da177e4SLinus Torvalds */ 13379f8bdb3fSHugh Dickins if (unlikely(pte_same_as_swp(*pte, swp_pte))) { 1338044d66c1SHugh Dickins pte_unmap(pte); 1339044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1340044d66c1SHugh Dickins if (ret) 1341044d66c1SHugh Dickins goto out; 1342044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1345044d66c1SHugh Dickins pte_unmap(pte - 1); 1346044d66c1SHugh Dickins out: 13478a9f3ccdSBalbir Singh return ret; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 13511da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13521da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds pmd_t *pmd; 13551da177e4SLinus Torvalds unsigned long next; 13568a9f3ccdSBalbir Singh int ret; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 13591da177e4SLinus Torvalds do { 1360dc644a07SHugh Dickins cond_resched(); 13611da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 13621a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 13631da177e4SLinus Torvalds continue; 13648a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 13658a9f3ccdSBalbir Singh if (ret) 13668a9f3ccdSBalbir Singh return ret; 13671da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 13681da177e4SLinus Torvalds return 0; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 13721da177e4SLinus Torvalds unsigned long addr, unsigned long end, 13731da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 13741da177e4SLinus Torvalds { 13751da177e4SLinus Torvalds pud_t *pud; 13761da177e4SLinus Torvalds unsigned long next; 13778a9f3ccdSBalbir Singh int ret; 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 13801da177e4SLinus Torvalds do { 13811da177e4SLinus Torvalds next = pud_addr_end(addr, end); 13821da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 13831da177e4SLinus Torvalds continue; 13848a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 13858a9f3ccdSBalbir Singh if (ret) 13868a9f3ccdSBalbir Singh return ret; 13871da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 13881da177e4SLinus Torvalds return 0; 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 13921da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds pgd_t *pgd; 13951da177e4SLinus Torvalds unsigned long addr, end, next; 13968a9f3ccdSBalbir Singh int ret; 13971da177e4SLinus Torvalds 13983ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 13991da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 14001da177e4SLinus Torvalds if (addr == -EFAULT) 14011da177e4SLinus Torvalds return 0; 14021da177e4SLinus Torvalds else 14031da177e4SLinus Torvalds end = addr + PAGE_SIZE; 14041da177e4SLinus Torvalds } else { 14051da177e4SLinus Torvalds addr = vma->vm_start; 14061da177e4SLinus Torvalds end = vma->vm_end; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 14101da177e4SLinus Torvalds do { 14111da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 14121da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 14131da177e4SLinus Torvalds continue; 14148a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 14158a9f3ccdSBalbir Singh if (ret) 14168a9f3ccdSBalbir Singh return ret; 14171da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 14181da177e4SLinus Torvalds return 0; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 14221da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 14231da177e4SLinus Torvalds { 14241da177e4SLinus Torvalds struct vm_area_struct *vma; 14258a9f3ccdSBalbir Singh int ret = 0; 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 14281da177e4SLinus Torvalds /* 14297d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 14307d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 14311da177e4SLinus Torvalds */ 1432c475a8abSHugh Dickins activate_page(page); 14331da177e4SLinus Torvalds unlock_page(page); 14341da177e4SLinus Torvalds down_read(&mm->mmap_sem); 14351da177e4SLinus Torvalds lock_page(page); 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 14388a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 14391da177e4SLinus Torvalds break; 1440dc644a07SHugh Dickins cond_resched(); 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds up_read(&mm->mmap_sem); 14438a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds /* 144738b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 144838b5faf4SDan Magenheimer * from current position to next entry still in use. 14491da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 14501da177e4SLinus Torvalds */ 14516eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 145238b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 14531da177e4SLinus Torvalds { 14546eb396dcSHugh Dickins unsigned int max = si->max; 14556eb396dcSHugh Dickins unsigned int i = prev; 14568d69aaeeSHugh Dickins unsigned char count; 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds /* 14595d337b91SHugh Dickins * No need for swap_lock here: we're just looking 14601da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 14611da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 14625d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 14631da177e4SLinus Torvalds */ 14641da177e4SLinus Torvalds for (;;) { 14651da177e4SLinus Torvalds if (++i >= max) { 14661da177e4SLinus Torvalds if (!prev) { 14671da177e4SLinus Torvalds i = 0; 14681da177e4SLinus Torvalds break; 14691da177e4SLinus Torvalds } 14701da177e4SLinus Torvalds /* 14711da177e4SLinus Torvalds * No entries in use at top of swap_map, 14721da177e4SLinus Torvalds * loop back to start and recheck there. 14731da177e4SLinus Torvalds */ 14741da177e4SLinus Torvalds max = prev + 1; 14751da177e4SLinus Torvalds prev = 0; 14761da177e4SLinus Torvalds i = 1; 14771da177e4SLinus Torvalds } 14784db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 1479355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 1480dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 14811da177e4SLinus Torvalds break; 1482dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 1483dc644a07SHugh Dickins cond_resched(); 14841da177e4SLinus Torvalds } 14851da177e4SLinus Torvalds return i; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 14901da177e4SLinus Torvalds * and then search for the process using it. All the necessary 14911da177e4SLinus Torvalds * page table adjustments can then be made atomically. 149238b5faf4SDan Magenheimer * 149338b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 149438b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 14951da177e4SLinus Torvalds */ 149638b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 149738b5faf4SDan Magenheimer unsigned long pages_to_unuse) 14981da177e4SLinus Torvalds { 1499efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 15001da177e4SLinus Torvalds struct mm_struct *start_mm; 1501edfe23daSShaohua Li volatile unsigned char *swap_map; /* swap_map is accessed without 1502edfe23daSShaohua Li * locking. Mark it as volatile 1503edfe23daSShaohua Li * to prevent compiler doing 1504edfe23daSShaohua Li * something odd. 1505edfe23daSShaohua Li */ 15068d69aaeeSHugh Dickins unsigned char swcount; 15071da177e4SLinus Torvalds struct page *page; 15081da177e4SLinus Torvalds swp_entry_t entry; 15096eb396dcSHugh Dickins unsigned int i = 0; 15101da177e4SLinus Torvalds int retval = 0; 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds /* 15131da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 15141da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 15151da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 15161da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 15171da177e4SLinus Torvalds * 15181da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 15191da177e4SLinus Torvalds * freed the previous entry from; but that would take less 15201da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 15211da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 15221da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 15231da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 1524570a335bSHugh Dickins * that. 15251da177e4SLinus Torvalds */ 15261da177e4SLinus Torvalds start_mm = &init_mm; 15271da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* 15301da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 15311da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 15321da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 15331da177e4SLinus Torvalds */ 153438b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 15351da177e4SLinus Torvalds if (signal_pending(current)) { 15361da177e4SLinus Torvalds retval = -EINTR; 15371da177e4SLinus Torvalds break; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds 15401da177e4SLinus Torvalds /* 15411da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 15421da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 15431da177e4SLinus Torvalds * page and read the swap into it. 15441da177e4SLinus Torvalds */ 15451da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 15461da177e4SLinus Torvalds entry = swp_entry(type, i); 154702098feaSHugh Dickins page = read_swap_cache_async(entry, 154802098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 15491da177e4SLinus Torvalds if (!page) { 15501da177e4SLinus Torvalds /* 15511da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 15521da177e4SLinus Torvalds * has been freed independently, and will not be 15531da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 15541da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 15551da177e4SLinus Torvalds */ 1556edfe23daSShaohua Li swcount = *swap_map; 1557edfe23daSShaohua Li /* 1558edfe23daSShaohua Li * We don't hold lock here, so the swap entry could be 1559edfe23daSShaohua Li * SWAP_MAP_BAD (when the cluster is discarding). 1560edfe23daSShaohua Li * Instead of fail out, We can just skip the swap 1561edfe23daSShaohua Li * entry because swapoff will wait for discarding 1562edfe23daSShaohua Li * finish anyway. 1563edfe23daSShaohua Li */ 1564edfe23daSShaohua Li if (!swcount || swcount == SWAP_MAP_BAD) 15651da177e4SLinus Torvalds continue; 15661da177e4SLinus Torvalds retval = -ENOMEM; 15671da177e4SLinus Torvalds break; 15681da177e4SLinus Torvalds } 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds /* 15711da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 15721da177e4SLinus Torvalds */ 15731da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 15741da177e4SLinus Torvalds mmput(start_mm); 15751da177e4SLinus Torvalds start_mm = &init_mm; 15761da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds /* 15801da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 15811da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 15821da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 15831da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 15841da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 15851da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 15861da177e4SLinus Torvalds */ 15871da177e4SLinus Torvalds wait_on_page_locked(page); 15881da177e4SLinus Torvalds wait_on_page_writeback(page); 15891da177e4SLinus Torvalds lock_page(page); 15901da177e4SLinus Torvalds wait_on_page_writeback(page); 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds /* 15931da177e4SLinus Torvalds * Remove all references to entry. 15941da177e4SLinus Torvalds */ 15951da177e4SLinus Torvalds swcount = *swap_map; 1596aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 1597aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 1598aaa46865SHugh Dickins /* page has already been unlocked and released */ 1599aaa46865SHugh Dickins if (retval < 0) 1600aaa46865SHugh Dickins break; 1601aaa46865SHugh Dickins continue; 16021da177e4SLinus Torvalds } 1603aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 1604aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 1605aaa46865SHugh Dickins 1606355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 16071da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 16081da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 16091da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 16101da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 16111da177e4SLinus Torvalds struct mm_struct *mm; 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 16141da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 16151da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1616aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 16171da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 16181da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 161970af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 16201da177e4SLinus Torvalds continue; 16211da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 16221da177e4SLinus Torvalds mmput(prev_mm); 16231da177e4SLinus Torvalds prev_mm = mm; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds cond_resched(); 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds swcount = *swap_map; 1628355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 16291da177e4SLinus Torvalds ; 1630aaa46865SHugh Dickins else if (mm == &init_mm) 16311da177e4SLinus Torvalds set_start_mm = 1; 1632aaa46865SHugh Dickins else 16331da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1634355cfa73SKAMEZAWA Hiroyuki 163532c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 16361da177e4SLinus Torvalds mmput(new_start_mm); 16371da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 16381da177e4SLinus Torvalds new_start_mm = mm; 16391da177e4SLinus Torvalds set_start_mm = 0; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds spin_lock(&mmlist_lock); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 16441da177e4SLinus Torvalds mmput(prev_mm); 16451da177e4SLinus Torvalds mmput(start_mm); 16461da177e4SLinus Torvalds start_mm = new_start_mm; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds if (retval) { 16491da177e4SLinus Torvalds unlock_page(page); 165009cbfeafSKirill A. Shutemov put_page(page); 16511da177e4SLinus Torvalds break; 16521da177e4SLinus Torvalds } 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds /* 16551da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 16561da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 16571da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 16581da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 16591da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 16601da177e4SLinus Torvalds * delete from cache even if there's another reference, 16611da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 16621da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 16631da177e4SLinus Torvalds * read from disk into another page. Splitting into two 16641da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 16651da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 16665ad64688SHugh Dickins * 16675ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 16685ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 16695ad64688SHugh Dickins * this splitting happens to be just what is needed to 16705ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 16715ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 16721da177e4SLinus Torvalds */ 1673355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 1674355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 16751da177e4SLinus Torvalds struct writeback_control wbc = { 16761da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 16771da177e4SLinus Torvalds }; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds swap_writepage(page, &wbc); 16801da177e4SLinus Torvalds lock_page(page); 16811da177e4SLinus Torvalds wait_on_page_writeback(page); 16821da177e4SLinus Torvalds } 168368bdc8d6SHugh Dickins 168468bdc8d6SHugh Dickins /* 168568bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 168668bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 168768bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 168868bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 168968bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 169068bdc8d6SHugh Dickins */ 169168bdc8d6SHugh Dickins if (PageSwapCache(page) && 169268bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 16931da177e4SLinus Torvalds delete_from_swap_cache(page); 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds /* 16961da177e4SLinus Torvalds * So we could skip searching mms once swap count went 16971da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 16982706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 16991da177e4SLinus Torvalds */ 17001da177e4SLinus Torvalds SetPageDirty(page); 17011da177e4SLinus Torvalds unlock_page(page); 170209cbfeafSKirill A. Shutemov put_page(page); 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds /* 17051da177e4SLinus Torvalds * Make sure that we aren't completely killing 17061da177e4SLinus Torvalds * interactive performance. 17071da177e4SLinus Torvalds */ 17081da177e4SLinus Torvalds cond_resched(); 170938b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 171038b5faf4SDan Magenheimer if (!--pages_to_unuse) 171138b5faf4SDan Magenheimer break; 171238b5faf4SDan Magenheimer } 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds mmput(start_mm); 17161da177e4SLinus Torvalds return retval; 17171da177e4SLinus Torvalds } 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds /* 17205d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 17215d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 17225d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 17231da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 17241da177e4SLinus Torvalds */ 17251da177e4SLinus Torvalds static void drain_mmlist(void) 17261da177e4SLinus Torvalds { 17271da177e4SLinus Torvalds struct list_head *p, *next; 1728efa90a98SHugh Dickins unsigned int type; 17291da177e4SLinus Torvalds 1730efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 1731efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 17321da177e4SLinus Torvalds return; 17331da177e4SLinus Torvalds spin_lock(&mmlist_lock); 17341da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 17351da177e4SLinus Torvalds list_del_init(p); 17361da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds /* 17401da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 1741d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 1742d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 1743d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 17441da177e4SLinus Torvalds */ 1745d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 17461da177e4SLinus Torvalds { 1747f29ad6a9SHugh Dickins struct swap_info_struct *sis; 1748f29ad6a9SHugh Dickins struct swap_extent *start_se; 1749f29ad6a9SHugh Dickins struct swap_extent *se; 1750f29ad6a9SHugh Dickins pgoff_t offset; 1751f29ad6a9SHugh Dickins 1752efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 1753f29ad6a9SHugh Dickins *bdev = sis->bdev; 1754f29ad6a9SHugh Dickins 1755f29ad6a9SHugh Dickins offset = swp_offset(entry); 1756f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 1757f29ad6a9SHugh Dickins se = start_se; 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds for ( ; ; ) { 17601da177e4SLinus Torvalds if (se->start_page <= offset && 17611da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 17621da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 17631da177e4SLinus Torvalds } 1764a8ae4991SGeliang Tang se = list_next_entry(se, list); 17651da177e4SLinus Torvalds sis->curr_swap_extent = se; 17661da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 17671da177e4SLinus Torvalds } 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds /* 1771d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 1772d4906e1aSLee Schermerhorn */ 1773d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 1774d4906e1aSLee Schermerhorn { 1775d4906e1aSLee Schermerhorn swp_entry_t entry; 1776d4906e1aSLee Schermerhorn entry.val = page_private(page); 1777d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 1778d4906e1aSLee Schermerhorn } 1779d4906e1aSLee Schermerhorn 1780d4906e1aSLee Schermerhorn /* 17811da177e4SLinus Torvalds * Free all of a swapdev's extent information 17821da177e4SLinus Torvalds */ 17831da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 17841da177e4SLinus Torvalds { 17859625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 17861da177e4SLinus Torvalds struct swap_extent *se; 17871da177e4SLinus Torvalds 1788a8ae4991SGeliang Tang se = list_first_entry(&sis->first_swap_extent.list, 17891da177e4SLinus Torvalds struct swap_extent, list); 17901da177e4SLinus Torvalds list_del(&se->list); 17911da177e4SLinus Torvalds kfree(se); 17921da177e4SLinus Torvalds } 179362c230bcSMel Gorman 179462c230bcSMel Gorman if (sis->flags & SWP_FILE) { 179562c230bcSMel Gorman struct file *swap_file = sis->swap_file; 179662c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 179762c230bcSMel Gorman 179862c230bcSMel Gorman sis->flags &= ~SWP_FILE; 179962c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 180062c230bcSMel Gorman } 18011da177e4SLinus Torvalds } 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /* 18041da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 180511d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 18061da177e4SLinus Torvalds * 180711d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 18081da177e4SLinus Torvalds */ 1809a509bc1aSMel Gorman int 18101da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 18111da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 18121da177e4SLinus Torvalds { 18131da177e4SLinus Torvalds struct swap_extent *se; 18141da177e4SLinus Torvalds struct swap_extent *new_se; 18151da177e4SLinus Torvalds struct list_head *lh; 18161da177e4SLinus Torvalds 18179625a5f2SHugh Dickins if (start_page == 0) { 18189625a5f2SHugh Dickins se = &sis->first_swap_extent; 18199625a5f2SHugh Dickins sis->curr_swap_extent = se; 18209625a5f2SHugh Dickins se->start_page = 0; 18219625a5f2SHugh Dickins se->nr_pages = nr_pages; 18229625a5f2SHugh Dickins se->start_block = start_block; 18239625a5f2SHugh Dickins return 1; 18249625a5f2SHugh Dickins } else { 18259625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 18261da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 182711d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 182811d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 18291da177e4SLinus Torvalds /* Merge it */ 18301da177e4SLinus Torvalds se->nr_pages += nr_pages; 18311da177e4SLinus Torvalds return 0; 18321da177e4SLinus Torvalds } 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds /* 18361da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 18371da177e4SLinus Torvalds */ 18381da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 18391da177e4SLinus Torvalds if (new_se == NULL) 18401da177e4SLinus Torvalds return -ENOMEM; 18411da177e4SLinus Torvalds new_se->start_page = start_page; 18421da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 18431da177e4SLinus Torvalds new_se->start_block = start_block; 18441da177e4SLinus Torvalds 18459625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 184653092a74SHugh Dickins return 1; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds /* 18501da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 18511da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 18521da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 18531da177e4SLinus Torvalds * time for locating where on disk a page belongs. 18541da177e4SLinus Torvalds * 18551da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 18561da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 18571da177e4SLinus Torvalds * swap files identically. 18581da177e4SLinus Torvalds * 18591da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 18601da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 18611da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 18621da177e4SLinus Torvalds * 18631da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 18641da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 18651da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 18661da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 18671da177e4SLinus Torvalds * for swapping. 18681da177e4SLinus Torvalds * 1869b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 18701da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 18711da177e4SLinus Torvalds * which will scribble on the fs. 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 18741da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 18751da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 18761da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 18771da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 18781da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 18791da177e4SLinus Torvalds */ 188053092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 18811da177e4SLinus Torvalds { 188262c230bcSMel Gorman struct file *swap_file = sis->swap_file; 188362c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 188462c230bcSMel Gorman struct inode *inode = mapping->host; 18851da177e4SLinus Torvalds int ret; 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 18881da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 188953092a74SHugh Dickins *span = sis->pages; 1890a509bc1aSMel Gorman return ret; 18911da177e4SLinus Torvalds } 18921da177e4SLinus Torvalds 189362c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 1894a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 189562c230bcSMel Gorman if (!ret) { 189662c230bcSMel Gorman sis->flags |= SWP_FILE; 189762c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 189862c230bcSMel Gorman *span = sis->pages; 189962c230bcSMel Gorman } 19009625a5f2SHugh Dickins return ret; 1901a509bc1aSMel Gorman } 1902a509bc1aSMel Gorman 1903a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 19072a8f9449SShaohua Li unsigned char *swap_map, 19082a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 190940531542SCesar Eduardo Barros { 191040531542SCesar Eduardo Barros if (prio >= 0) 191140531542SCesar Eduardo Barros p->prio = prio; 191240531542SCesar Eduardo Barros else 191340531542SCesar Eduardo Barros p->prio = --least_priority; 191418ab4d4cSDan Streetman /* 191518ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 191618ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 191718ab4d4cSDan Streetman */ 191818ab4d4cSDan Streetman p->list.prio = -p->prio; 191918ab4d4cSDan Streetman p->avail_list.prio = -p->prio; 192040531542SCesar Eduardo Barros p->swap_map = swap_map; 19212a8f9449SShaohua Li p->cluster_info = cluster_info; 192240531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 1923ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 192440531542SCesar Eduardo Barros total_swap_pages += p->pages; 192540531542SCesar Eduardo Barros 1926adfab836SDan Streetman assert_spin_locked(&swap_lock); 1927adfab836SDan Streetman /* 192818ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 192918ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 193018ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 193118ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 193218ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 193318ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 193418ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 193518ab4d4cSDan Streetman * swap_info_struct. 1936adfab836SDan Streetman */ 193718ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 193818ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 193918ab4d4cSDan Streetman plist_add(&p->avail_list, &swap_avail_head); 194018ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 1941cf0cac0aSCesar Eduardo Barros } 1942cf0cac0aSCesar Eduardo Barros 1943cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 1944cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 19452a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 1946cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 1947cf0cac0aSCesar Eduardo Barros { 19484f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 1949cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1950ec8acf20SShaohua Li spin_lock(&p->lock); 19512a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 1952ec8acf20SShaohua Li spin_unlock(&p->lock); 1953cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 1954cf0cac0aSCesar Eduardo Barros } 1955cf0cac0aSCesar Eduardo Barros 1956cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 1957cf0cac0aSCesar Eduardo Barros { 1958cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1959ec8acf20SShaohua Li spin_lock(&p->lock); 19602a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 1961ec8acf20SShaohua Li spin_unlock(&p->lock); 196240531542SCesar Eduardo Barros spin_unlock(&swap_lock); 196340531542SCesar Eduardo Barros } 196440531542SCesar Eduardo Barros 1965c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 19661da177e4SLinus Torvalds { 19671da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 19688d69aaeeSHugh Dickins unsigned char *swap_map; 19692a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 19704f89849dSMinchan Kim unsigned long *frontswap_map; 19711da177e4SLinus Torvalds struct file *swap_file, *victim; 19721da177e4SLinus Torvalds struct address_space *mapping; 19731da177e4SLinus Torvalds struct inode *inode; 197491a27b2aSJeff Layton struct filename *pathname; 1975adfab836SDan Streetman int err, found = 0; 19765b808a23SKrzysztof Kozlowski unsigned int old_block_size; 19771da177e4SLinus Torvalds 19781da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 19791da177e4SLinus Torvalds return -EPERM; 19801da177e4SLinus Torvalds 1981191c5424SAl Viro BUG_ON(!current->mm); 1982191c5424SAl Viro 19831da177e4SLinus Torvalds pathname = getname(specialfile); 19841da177e4SLinus Torvalds if (IS_ERR(pathname)) 1985f58b59c1SXiaotian Feng return PTR_ERR(pathname); 19861da177e4SLinus Torvalds 1987669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 19881da177e4SLinus Torvalds err = PTR_ERR(victim); 19891da177e4SLinus Torvalds if (IS_ERR(victim)) 19901da177e4SLinus Torvalds goto out; 19911da177e4SLinus Torvalds 19921da177e4SLinus Torvalds mapping = victim->f_mapping; 19935d337b91SHugh Dickins spin_lock(&swap_lock); 199418ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 199522c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 1996adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 1997adfab836SDan Streetman found = 1; 19981da177e4SLinus Torvalds break; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds } 2001adfab836SDan Streetman } 2002adfab836SDan Streetman if (!found) { 20031da177e4SLinus Torvalds err = -EINVAL; 20045d337b91SHugh Dickins spin_unlock(&swap_lock); 20051da177e4SLinus Torvalds goto out_dput; 20061da177e4SLinus Torvalds } 2007191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 20081da177e4SLinus Torvalds vm_unacct_memory(p->pages); 20091da177e4SLinus Torvalds else { 20101da177e4SLinus Torvalds err = -ENOMEM; 20115d337b91SHugh Dickins spin_unlock(&swap_lock); 20121da177e4SLinus Torvalds goto out_dput; 20131da177e4SLinus Torvalds } 201418ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 201518ab4d4cSDan Streetman plist_del(&p->avail_list, &swap_avail_head); 201618ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 2017ec8acf20SShaohua Li spin_lock(&p->lock); 201878ecba08SHugh Dickins if (p->prio < 0) { 2019adfab836SDan Streetman struct swap_info_struct *si = p; 2020adfab836SDan Streetman 202118ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2022adfab836SDan Streetman si->prio++; 202318ab4d4cSDan Streetman si->list.prio--; 202418ab4d4cSDan Streetman si->avail_list.prio--; 2025adfab836SDan Streetman } 202678ecba08SHugh Dickins least_priority++; 202778ecba08SHugh Dickins } 202818ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2029ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 20301da177e4SLinus Torvalds total_swap_pages -= p->pages; 20311da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2032ec8acf20SShaohua Li spin_unlock(&p->lock); 20335d337b91SHugh Dickins spin_unlock(&swap_lock); 2034fb4f88dcSHugh Dickins 2035e1e12d2fSDavid Rientjes set_current_oom_origin(); 2036adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2037e1e12d2fSDavid Rientjes clear_current_oom_origin(); 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds if (err) { 20401da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2041cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 20421da177e4SLinus Torvalds goto out_dput; 20431da177e4SLinus Torvalds } 204452b7efdbSHugh Dickins 2045815c2c54SShaohua Li flush_work(&p->discard_work); 2046815c2c54SShaohua Li 20474cd3bb10SHugh Dickins destroy_swap_extents(p); 2048570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2049570a335bSHugh Dickins free_swap_count_continuations(p); 2050570a335bSHugh Dickins 2051fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 20525d337b91SHugh Dickins spin_lock(&swap_lock); 2053ec8acf20SShaohua Li spin_lock(&p->lock); 20541da177e4SLinus Torvalds drain_mmlist(); 20555d337b91SHugh Dickins 20565d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 20575d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 20585d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2059ec8acf20SShaohua Li spin_unlock(&p->lock); 20605d337b91SHugh Dickins spin_unlock(&swap_lock); 206113e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 20625d337b91SHugh Dickins spin_lock(&swap_lock); 2063ec8acf20SShaohua Li spin_lock(&p->lock); 20645d337b91SHugh Dickins } 20655d337b91SHugh Dickins 20661da177e4SLinus Torvalds swap_file = p->swap_file; 20675b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 20681da177e4SLinus Torvalds p->swap_file = NULL; 20691da177e4SLinus Torvalds p->max = 0; 20701da177e4SLinus Torvalds swap_map = p->swap_map; 20711da177e4SLinus Torvalds p->swap_map = NULL; 20722a8f9449SShaohua Li cluster_info = p->cluster_info; 20732a8f9449SShaohua Li p->cluster_info = NULL; 20744f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2075ec8acf20SShaohua Li spin_unlock(&p->lock); 20765d337b91SHugh Dickins spin_unlock(&swap_lock); 2077adfab836SDan Streetman frontswap_invalidate_area(p->type); 207858e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2079fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2080ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2081ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 20821da177e4SLinus Torvalds vfree(swap_map); 20832a8f9449SShaohua Li vfree(cluster_info); 20844f89849dSMinchan Kim vfree(frontswap_map); 20852de1a7e4SSeth Jennings /* Destroy swap account information */ 2086adfab836SDan Streetman swap_cgroup_swapoff(p->type); 208727a7faa0SKAMEZAWA Hiroyuki 20881da177e4SLinus Torvalds inode = mapping->host; 20891da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 20901da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 20915b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2092e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 20931da177e4SLinus Torvalds } else { 20945955102cSAl Viro inode_lock(inode); 20951da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 20965955102cSAl Viro inode_unlock(inode); 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds filp_close(swap_file, NULL); 2099f893ab41SWeijie Yang 2100f893ab41SWeijie Yang /* 2101f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2102f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2103f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2104f893ab41SWeijie Yang */ 2105f893ab41SWeijie Yang spin_lock(&swap_lock); 2106f893ab41SWeijie Yang p->flags = 0; 2107f893ab41SWeijie Yang spin_unlock(&swap_lock); 2108f893ab41SWeijie Yang 21091da177e4SLinus Torvalds err = 0; 211066d7dd51SKay Sievers atomic_inc(&proc_poll_event); 211166d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds out_dput: 21141da177e4SLinus Torvalds filp_close(victim, NULL); 21151da177e4SLinus Torvalds out: 2116f58b59c1SXiaotian Feng putname(pathname); 21171da177e4SLinus Torvalds return err; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 212166d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 212266d7dd51SKay Sievers { 2123f1514638SKay Sievers struct seq_file *seq = file->private_data; 212466d7dd51SKay Sievers 212566d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 212666d7dd51SKay Sievers 2127f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2128f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 212966d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 213066d7dd51SKay Sievers } 213166d7dd51SKay Sievers 213266d7dd51SKay Sievers return POLLIN | POLLRDNORM; 213366d7dd51SKay Sievers } 213466d7dd51SKay Sievers 21351da177e4SLinus Torvalds /* iterator */ 21361da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 21371da177e4SLinus Torvalds { 2138efa90a98SHugh Dickins struct swap_info_struct *si; 2139efa90a98SHugh Dickins int type; 21401da177e4SLinus Torvalds loff_t l = *pos; 21411da177e4SLinus Torvalds 2142fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 21431da177e4SLinus Torvalds 2144881e4aabSSuleiman Souhlal if (!l) 2145881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2146881e4aabSSuleiman Souhlal 2147efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2148efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2149efa90a98SHugh Dickins si = swap_info[type]; 2150efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 21511da177e4SLinus Torvalds continue; 2152881e4aabSSuleiman Souhlal if (!--l) 2153efa90a98SHugh Dickins return si; 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 21561da177e4SLinus Torvalds return NULL; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 21601da177e4SLinus Torvalds { 2161efa90a98SHugh Dickins struct swap_info_struct *si = v; 2162efa90a98SHugh Dickins int type; 21631da177e4SLinus Torvalds 2164881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2165efa90a98SHugh Dickins type = 0; 2166efa90a98SHugh Dickins else 2167efa90a98SHugh Dickins type = si->type + 1; 2168881e4aabSSuleiman Souhlal 2169efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 2170efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2171efa90a98SHugh Dickins si = swap_info[type]; 2172efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 21731da177e4SLinus Torvalds continue; 21741da177e4SLinus Torvalds ++*pos; 2175efa90a98SHugh Dickins return si; 21761da177e4SLinus Torvalds } 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds return NULL; 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 21821da177e4SLinus Torvalds { 2183fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 21861da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 21871da177e4SLinus Torvalds { 2188efa90a98SHugh Dickins struct swap_info_struct *si = v; 21891da177e4SLinus Torvalds struct file *file; 21901da177e4SLinus Torvalds int len; 21911da177e4SLinus Torvalds 2192efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 21931da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2194881e4aabSSuleiman Souhlal return 0; 2195881e4aabSSuleiman Souhlal } 21961da177e4SLinus Torvalds 2197efa90a98SHugh Dickins file = si->swap_file; 21982726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 21996eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 22001da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2201496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 22021da177e4SLinus Torvalds "partition" : "file\t", 2203efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2204efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2205efa90a98SHugh Dickins si->prio); 22061da177e4SLinus Torvalds return 0; 22071da177e4SLinus Torvalds } 22081da177e4SLinus Torvalds 220915ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 22101da177e4SLinus Torvalds .start = swap_start, 22111da177e4SLinus Torvalds .next = swap_next, 22121da177e4SLinus Torvalds .stop = swap_stop, 22131da177e4SLinus Torvalds .show = swap_show 22141da177e4SLinus Torvalds }; 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 22171da177e4SLinus Torvalds { 2218f1514638SKay Sievers struct seq_file *seq; 221966d7dd51SKay Sievers int ret; 222066d7dd51SKay Sievers 222166d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2222f1514638SKay Sievers if (ret) 222366d7dd51SKay Sievers return ret; 222466d7dd51SKay Sievers 2225f1514638SKay Sievers seq = file->private_data; 2226f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2227f1514638SKay Sievers return 0; 22281da177e4SLinus Torvalds } 22291da177e4SLinus Torvalds 223015ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 22311da177e4SLinus Torvalds .open = swaps_open, 22321da177e4SLinus Torvalds .read = seq_read, 22331da177e4SLinus Torvalds .llseek = seq_lseek, 22341da177e4SLinus Torvalds .release = seq_release, 223566d7dd51SKay Sievers .poll = swaps_poll, 22361da177e4SLinus Torvalds }; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds static int __init procswaps_init(void) 22391da177e4SLinus Torvalds { 22403d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 22411da177e4SLinus Torvalds return 0; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds __initcall(procswaps_init); 22441da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 22451da177e4SLinus Torvalds 22461796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 22471796316aSJan Beulich static int __init max_swapfiles_check(void) 22481796316aSJan Beulich { 22491796316aSJan Beulich MAX_SWAPFILES_CHECK(); 22501796316aSJan Beulich return 0; 22511796316aSJan Beulich } 22521796316aSJan Beulich late_initcall(max_swapfiles_check); 22531796316aSJan Beulich #endif 22541796316aSJan Beulich 225553cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 22561da177e4SLinus Torvalds { 22571da177e4SLinus Torvalds struct swap_info_struct *p; 22581da177e4SLinus Torvalds unsigned int type; 2259efa90a98SHugh Dickins 2260efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2261efa90a98SHugh Dickins if (!p) 226253cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2263efa90a98SHugh Dickins 22645d337b91SHugh Dickins spin_lock(&swap_lock); 2265efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2266efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 22671da177e4SLinus Torvalds break; 2268efa90a98SHugh Dickins } 22690697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 22705d337b91SHugh Dickins spin_unlock(&swap_lock); 2271efa90a98SHugh Dickins kfree(p); 2272730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 22731da177e4SLinus Torvalds } 2274efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2275efa90a98SHugh Dickins p->type = type; 2276efa90a98SHugh Dickins swap_info[type] = p; 2277efa90a98SHugh Dickins /* 2278efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2279efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2280efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2281efa90a98SHugh Dickins */ 2282efa90a98SHugh Dickins smp_wmb(); 2283efa90a98SHugh Dickins nr_swapfiles++; 2284efa90a98SHugh Dickins } else { 2285efa90a98SHugh Dickins kfree(p); 2286efa90a98SHugh Dickins p = swap_info[type]; 2287efa90a98SHugh Dickins /* 2288efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2289efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2290efa90a98SHugh Dickins */ 2291efa90a98SHugh Dickins } 22929625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 229318ab4d4cSDan Streetman plist_node_init(&p->list, 0); 229418ab4d4cSDan Streetman plist_node_init(&p->avail_list, 0); 22951da177e4SLinus Torvalds p->flags = SWP_USED; 22965d337b91SHugh Dickins spin_unlock(&swap_lock); 2297ec8acf20SShaohua Li spin_lock_init(&p->lock); 2298efa90a98SHugh Dickins 229953cbb243SCesar Eduardo Barros return p; 230053cbb243SCesar Eduardo Barros } 230153cbb243SCesar Eduardo Barros 23024d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 23034d0e1e10SCesar Eduardo Barros { 23044d0e1e10SCesar Eduardo Barros int error; 23054d0e1e10SCesar Eduardo Barros 23064d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 23074d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 23084d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 23096f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 23104d0e1e10SCesar Eduardo Barros if (error < 0) { 23114d0e1e10SCesar Eduardo Barros p->bdev = NULL; 23126f179af8SHugh Dickins return error; 23134d0e1e10SCesar Eduardo Barros } 23144d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 23154d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 23164d0e1e10SCesar Eduardo Barros if (error < 0) 231787ade72aSCesar Eduardo Barros return error; 23184d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 23194d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 23204d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 23215955102cSAl Viro inode_lock(inode); 232287ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 232387ade72aSCesar Eduardo Barros return -EBUSY; 232487ade72aSCesar Eduardo Barros } else 232587ade72aSCesar Eduardo Barros return -EINVAL; 23264d0e1e10SCesar Eduardo Barros 23274d0e1e10SCesar Eduardo Barros return 0; 23284d0e1e10SCesar Eduardo Barros } 23294d0e1e10SCesar Eduardo Barros 2330ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2331ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2332ca8bd38bSCesar Eduardo Barros struct inode *inode) 2333ca8bd38bSCesar Eduardo Barros { 2334ca8bd38bSCesar Eduardo Barros int i; 2335ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2336ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2337d6bbbd29SRaymond Jennings unsigned long last_page; 2338ca8bd38bSCesar Eduardo Barros 2339ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2340465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 234138719025SCesar Eduardo Barros return 0; 2342ca8bd38bSCesar Eduardo Barros } 2343ca8bd38bSCesar Eduardo Barros 2344ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2345ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2346ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2347ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2348ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2349dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2350dd111be6SJann Horn return 0; 2351ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2352ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2353ca8bd38bSCesar Eduardo Barros } 2354ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2355ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2356465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2357ca8bd38bSCesar Eduardo Barros swap_header->info.version); 235838719025SCesar Eduardo Barros return 0; 2359ca8bd38bSCesar Eduardo Barros } 2360ca8bd38bSCesar Eduardo Barros 2361ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2362ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2363ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2364ca8bd38bSCesar Eduardo Barros 2365ca8bd38bSCesar Eduardo Barros /* 2366ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 23679b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2368a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2369a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 23709b15b817SHugh Dickins * different architectures. In order to find the 2371a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2372ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2373a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2374ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2375ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2376ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 23779b15b817SHugh Dickins * swap pte. 2378ca8bd38bSCesar Eduardo Barros */ 2379ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 23809b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2381d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2382d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2383465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2384d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2385d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2386d6bbbd29SRaymond Jennings } 2387d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2388d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2389ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2390ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2391ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2392ca8bd38bSCesar Eduardo Barros } 2393ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2394ca8bd38bSCesar Eduardo Barros 2395ca8bd38bSCesar Eduardo Barros if (!maxpages) 239638719025SCesar Eduardo Barros return 0; 2397ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2398ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2399465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 240038719025SCesar Eduardo Barros return 0; 2401ca8bd38bSCesar Eduardo Barros } 2402ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 240338719025SCesar Eduardo Barros return 0; 2404ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 240538719025SCesar Eduardo Barros return 0; 2406ca8bd38bSCesar Eduardo Barros 2407ca8bd38bSCesar Eduardo Barros return maxpages; 2408ca8bd38bSCesar Eduardo Barros } 2409ca8bd38bSCesar Eduardo Barros 2410*235b6217SHuang, Ying #define SWAP_CLUSTER_COLS \ 2411*235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 2412*235b6217SHuang, Ying 2413915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2414915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2415915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 24162a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2417915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2418915d4d7bSCesar Eduardo Barros sector_t *span) 2419915d4d7bSCesar Eduardo Barros { 2420*235b6217SHuang, Ying unsigned int j, k; 2421915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 2422915d4d7bSCesar Eduardo Barros int nr_extents; 24232a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 2424*235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 2425*235b6217SHuang, Ying unsigned long i, idx; 2426915d4d7bSCesar Eduardo Barros 2427915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 2428915d4d7bSCesar Eduardo Barros 24296b534915SHuang Ying cluster_list_init(&p->free_clusters); 24306b534915SHuang Ying cluster_list_init(&p->discard_clusters); 24312a8f9449SShaohua Li 2432915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 2433915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 2434bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 2435bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2436915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 2437915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 2438915d4d7bSCesar Eduardo Barros nr_good_pages--; 24392a8f9449SShaohua Li /* 24402a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 24412a8f9449SShaohua Li * operation involved 24422a8f9449SShaohua Li */ 24432a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 2444915d4d7bSCesar Eduardo Barros } 2445915d4d7bSCesar Eduardo Barros } 2446915d4d7bSCesar Eduardo Barros 24472a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 24482a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 24492a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 24502a8f9449SShaohua Li 2451915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 2452915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 24532a8f9449SShaohua Li /* 24542a8f9449SShaohua Li * Not mark the cluster free yet, no list 24552a8f9449SShaohua Li * operation involved 24562a8f9449SShaohua Li */ 24572a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 2458915d4d7bSCesar Eduardo Barros p->max = maxpages; 2459915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 2460915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 2461bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 2462bdb8e3f6SCesar Eduardo Barros return nr_extents; 2463915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 2464915d4d7bSCesar Eduardo Barros } 2465915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 2466465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 2467bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2468915d4d7bSCesar Eduardo Barros } 2469915d4d7bSCesar Eduardo Barros 24702a8f9449SShaohua Li if (!cluster_info) 24712a8f9449SShaohua Li return nr_extents; 24722a8f9449SShaohua Li 2473*235b6217SHuang, Ying 2474*235b6217SHuang, Ying /* Reduce false cache line sharing between cluster_info */ 2475*235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 2476*235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 2477*235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 2478*235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 2479*235b6217SHuang, Ying if (idx >= nr_clusters) 2480*235b6217SHuang, Ying continue; 2481*235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 2482*235b6217SHuang, Ying continue; 24832a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 24846b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 24856b534915SHuang Ying idx); 24862a8f9449SShaohua Li } 24872a8f9449SShaohua Li } 2488915d4d7bSCesar Eduardo Barros return nr_extents; 2489915d4d7bSCesar Eduardo Barros } 2490915d4d7bSCesar Eduardo Barros 2491dcf6b7ddSRafael Aquini /* 2492dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 2493dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 2494dcf6b7ddSRafael Aquini */ 2495dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 2496dcf6b7ddSRafael Aquini { 2497dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 2498dcf6b7ddSRafael Aquini 2499dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 2500dcf6b7ddSRafael Aquini return false; 2501dcf6b7ddSRafael Aquini 2502dcf6b7ddSRafael Aquini return true; 2503dcf6b7ddSRafael Aquini } 2504dcf6b7ddSRafael Aquini 250553cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 250653cbb243SCesar Eduardo Barros { 250753cbb243SCesar Eduardo Barros struct swap_info_struct *p; 250891a27b2aSJeff Layton struct filename *name; 250953cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 251053cbb243SCesar Eduardo Barros struct address_space *mapping; 251140531542SCesar Eduardo Barros int prio; 251253cbb243SCesar Eduardo Barros int error; 251353cbb243SCesar Eduardo Barros union swap_header *swap_header; 2514915d4d7bSCesar Eduardo Barros int nr_extents; 251553cbb243SCesar Eduardo Barros sector_t span; 251653cbb243SCesar Eduardo Barros unsigned long maxpages; 251753cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 25182a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 251938b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 252053cbb243SCesar Eduardo Barros struct page *page = NULL; 252153cbb243SCesar Eduardo Barros struct inode *inode = NULL; 252253cbb243SCesar Eduardo Barros 2523d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 2524d15cab97SHugh Dickins return -EINVAL; 2525d15cab97SHugh Dickins 252653cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 252753cbb243SCesar Eduardo Barros return -EPERM; 252853cbb243SCesar Eduardo Barros 252953cbb243SCesar Eduardo Barros p = alloc_swap_info(); 25302542e513SCesar Eduardo Barros if (IS_ERR(p)) 25312542e513SCesar Eduardo Barros return PTR_ERR(p); 253253cbb243SCesar Eduardo Barros 2533815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 2534815c2c54SShaohua Li 25351da177e4SLinus Torvalds name = getname(specialfile); 25361da177e4SLinus Torvalds if (IS_ERR(name)) { 25377de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 25381da177e4SLinus Torvalds name = NULL; 2539bd69010bSCesar Eduardo Barros goto bad_swap; 25401da177e4SLinus Torvalds } 2541669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 25421da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 25437de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 25441da177e4SLinus Torvalds swap_file = NULL; 2545bd69010bSCesar Eduardo Barros goto bad_swap; 25461da177e4SLinus Torvalds } 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds p->swap_file = swap_file; 25491da177e4SLinus Torvalds mapping = swap_file->f_mapping; 25502130781eSCesar Eduardo Barros inode = mapping->host; 25516f179af8SHugh Dickins 25525955102cSAl Viro /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */ 25534d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 25544d0e1e10SCesar Eduardo Barros if (unlikely(error)) 25551da177e4SLinus Torvalds goto bad_swap; 25561da177e4SLinus Torvalds 25571da177e4SLinus Torvalds /* 25581da177e4SLinus Torvalds * Read the swap header. 25591da177e4SLinus Torvalds */ 25601da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 25611da177e4SLinus Torvalds error = -EINVAL; 25621da177e4SLinus Torvalds goto bad_swap; 25631da177e4SLinus Torvalds } 2564090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 25651da177e4SLinus Torvalds if (IS_ERR(page)) { 25661da177e4SLinus Torvalds error = PTR_ERR(page); 25671da177e4SLinus Torvalds goto bad_swap; 25681da177e4SLinus Torvalds } 256981e33971SHugh Dickins swap_header = kmap(page); 25701da177e4SLinus Torvalds 2571ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 2572ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 25731da177e4SLinus Torvalds error = -EINVAL; 25741da177e4SLinus Torvalds goto bad_swap; 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 25771da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 2578803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 257978ecba08SHugh Dickins if (!swap_map) { 25801da177e4SLinus Torvalds error = -ENOMEM; 25811da177e4SLinus Torvalds goto bad_swap; 25821da177e4SLinus Torvalds } 2583f0571429SMinchan Kim 2584f0571429SMinchan Kim if (bdi_cap_stable_pages_required(inode_to_bdi(inode))) 2585f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 2586f0571429SMinchan Kim 25872a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 25886f179af8SHugh Dickins int cpu; 2589*235b6217SHuang, Ying unsigned long ci, nr_cluster; 25906f179af8SHugh Dickins 25912a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 25922a8f9449SShaohua Li /* 25932a8f9449SShaohua Li * select a random position to start with to help wear leveling 25942a8f9449SShaohua Li * SSD 25952a8f9449SShaohua Li */ 25962a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 2597*235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 25982a8f9449SShaohua Li 2599*235b6217SHuang, Ying cluster_info = vzalloc(nr_cluster * sizeof(*cluster_info)); 26002a8f9449SShaohua Li if (!cluster_info) { 26012a8f9449SShaohua Li error = -ENOMEM; 26022a8f9449SShaohua Li goto bad_swap; 26032a8f9449SShaohua Li } 2604*235b6217SHuang, Ying 2605*235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 2606*235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 2607*235b6217SHuang, Ying 2608ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 2609ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 2610ebc2a1a6SShaohua Li error = -ENOMEM; 2611ebc2a1a6SShaohua Li goto bad_swap; 2612ebc2a1a6SShaohua Li } 26136f179af8SHugh Dickins for_each_possible_cpu(cpu) { 2614ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 26156f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 2616ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 2617ebc2a1a6SShaohua Li } 26182a8f9449SShaohua Li } 26191da177e4SLinus Torvalds 26201421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 26211421ef3cSCesar Eduardo Barros if (error) 26221421ef3cSCesar Eduardo Barros goto bad_swap; 26231421ef3cSCesar Eduardo Barros 2624915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 26252a8f9449SShaohua Li cluster_info, maxpages, &span); 2626915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 262753092a74SHugh Dickins error = nr_extents; 2628e2244ec2SHugh Dickins goto bad_swap; 262953092a74SHugh Dickins } 263038b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 26318ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 26327b57976dSAkinobu Mita frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long)); 26331da177e4SLinus Torvalds 26342a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 2635dcf6b7ddSRafael Aquini /* 2636dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 2637dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 2638dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 2639dcf6b7ddSRafael Aquini * swapon(8) releases. 2640dcf6b7ddSRafael Aquini */ 2641dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 2642dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 2643dcf6b7ddSRafael Aquini 2644dcf6b7ddSRafael Aquini /* 2645dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 2646dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 2647dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 2648dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 2649dcf6b7ddSRafael Aquini */ 2650dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 2651dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 2652dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 2653dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 2654dcf6b7ddSRafael Aquini 2655dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 2656dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 2657dcf6b7ddSRafael Aquini int err = discard_swap(p); 2658dcf6b7ddSRafael Aquini if (unlikely(err)) 2659465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 2660dcf6b7ddSRafael Aquini p, err); 2661dcf6b7ddSRafael Aquini } 2662dcf6b7ddSRafael Aquini } 26636a6ba831SHugh Dickins 2664fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 266540531542SCesar Eduardo Barros prio = -1; 266678ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 266740531542SCesar Eduardo Barros prio = 266878ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 26692a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 2670c69dbfb8SCesar Eduardo Barros 2671756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 267291a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 2673c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 2674c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 267538b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 2676dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 2677dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 267838b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 2679c69dbfb8SCesar Eduardo Barros 2680fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 268166d7dd51SKay Sievers atomic_inc(&proc_poll_event); 268266d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 268366d7dd51SKay Sievers 26849b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 26859b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 26861da177e4SLinus Torvalds error = 0; 26871da177e4SLinus Torvalds goto out; 26881da177e4SLinus Torvalds bad_swap: 2689ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2690ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 2691bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 2692f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 2693f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 26941da177e4SLinus Torvalds } 26954cd3bb10SHugh Dickins destroy_swap_extents(p); 2696e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 26975d337b91SHugh Dickins spin_lock(&swap_lock); 26981da177e4SLinus Torvalds p->swap_file = NULL; 26991da177e4SLinus Torvalds p->flags = 0; 27005d337b91SHugh Dickins spin_unlock(&swap_lock); 27011da177e4SLinus Torvalds vfree(swap_map); 27022a8f9449SShaohua Li vfree(cluster_info); 270352c50567SMel Gorman if (swap_file) { 27042130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 27055955102cSAl Viro inode_unlock(inode); 27062130781eSCesar Eduardo Barros inode = NULL; 27072130781eSCesar Eduardo Barros } 27081da177e4SLinus Torvalds filp_close(swap_file, NULL); 270952c50567SMel Gorman } 27101da177e4SLinus Torvalds out: 27111da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 27121da177e4SLinus Torvalds kunmap(page); 271309cbfeafSKirill A. Shutemov put_page(page); 27141da177e4SLinus Torvalds } 27151da177e4SLinus Torvalds if (name) 27161da177e4SLinus Torvalds putname(name); 27179b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 27185955102cSAl Viro inode_unlock(inode); 27191da177e4SLinus Torvalds return error; 27201da177e4SLinus Torvalds } 27211da177e4SLinus Torvalds 27221da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 27231da177e4SLinus Torvalds { 2724efa90a98SHugh Dickins unsigned int type; 27251da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 27261da177e4SLinus Torvalds 27275d337b91SHugh Dickins spin_lock(&swap_lock); 2728efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2729efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 2730efa90a98SHugh Dickins 2731efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 2732efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 27331da177e4SLinus Torvalds } 2734ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 27351da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 27365d337b91SHugh Dickins spin_unlock(&swap_lock); 27371da177e4SLinus Torvalds } 27381da177e4SLinus Torvalds 27391da177e4SLinus Torvalds /* 27401da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 27411da177e4SLinus Torvalds * 2742355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 2743355cfa73SKAMEZAWA Hiroyuki * - success -> 0 2744355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 2745355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 2746355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 2747355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 2748570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 27491da177e4SLinus Torvalds */ 27508d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 27511da177e4SLinus Torvalds { 27521da177e4SLinus Torvalds struct swap_info_struct *p; 2753*235b6217SHuang, Ying struct swap_cluster_info *ci; 27541da177e4SLinus Torvalds unsigned long offset, type; 27558d69aaeeSHugh Dickins unsigned char count; 27568d69aaeeSHugh Dickins unsigned char has_cache; 2757253d553bSHugh Dickins int err = -EINVAL; 27581da177e4SLinus Torvalds 2759a7420aa5SAndi Kleen if (non_swap_entry(entry)) 2760253d553bSHugh Dickins goto out; 27610697212aSChristoph Lameter 27621da177e4SLinus Torvalds type = swp_type(entry); 27631da177e4SLinus Torvalds if (type >= nr_swapfiles) 27641da177e4SLinus Torvalds goto bad_file; 2765efa90a98SHugh Dickins p = swap_info[type]; 27661da177e4SLinus Torvalds offset = swp_offset(entry); 2767355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 2768*235b6217SHuang, Ying goto out; 2769*235b6217SHuang, Ying 2770*235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 2771355cfa73SKAMEZAWA Hiroyuki 2772253d553bSHugh Dickins count = p->swap_map[offset]; 2773edfe23daSShaohua Li 2774edfe23daSShaohua Li /* 2775edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 2776edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 2777edfe23daSShaohua Li */ 2778edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 2779edfe23daSShaohua Li err = -ENOENT; 2780edfe23daSShaohua Li goto unlock_out; 2781edfe23daSShaohua Li } 2782edfe23daSShaohua Li 2783253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 2784253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 2785253d553bSHugh Dickins err = 0; 2786355cfa73SKAMEZAWA Hiroyuki 2787253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 2788355cfa73SKAMEZAWA Hiroyuki 2789355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 2790253d553bSHugh Dickins if (!has_cache && count) 2791253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 2792253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 2793253d553bSHugh Dickins err = -EEXIST; 2794253d553bSHugh Dickins else /* no users remaining */ 2795253d553bSHugh Dickins err = -ENOENT; 2796355cfa73SKAMEZAWA Hiroyuki 2797355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 2798253d553bSHugh Dickins 2799570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 2800570a335bSHugh Dickins count += usage; 2801570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 2802253d553bSHugh Dickins err = -EINVAL; 2803570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 2804570a335bSHugh Dickins count = COUNT_CONTINUED; 2805570a335bSHugh Dickins else 2806570a335bSHugh Dickins err = -ENOMEM; 2807253d553bSHugh Dickins } else 2808253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 2809253d553bSHugh Dickins 2810253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 2811253d553bSHugh Dickins 2812355cfa73SKAMEZAWA Hiroyuki unlock_out: 2813*235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 28141da177e4SLinus Torvalds out: 2815253d553bSHugh Dickins return err; 28161da177e4SLinus Torvalds 28171da177e4SLinus Torvalds bad_file: 2818465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 28191da177e4SLinus Torvalds goto out; 28201da177e4SLinus Torvalds } 2821253d553bSHugh Dickins 2822355cfa73SKAMEZAWA Hiroyuki /* 2823aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 2824aaa46865SHugh Dickins * (in which case its reference count is never incremented). 2825aaa46865SHugh Dickins */ 2826aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 2827aaa46865SHugh Dickins { 2828aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 2829aaa46865SHugh Dickins } 2830aaa46865SHugh Dickins 2831aaa46865SHugh Dickins /* 283208259d58SHugh Dickins * Increase reference count of swap entry by 1. 283308259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 283408259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 283508259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 283608259d58SHugh Dickins * might occur if a page table entry has got corrupted. 2837355cfa73SKAMEZAWA Hiroyuki */ 2838570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 2839355cfa73SKAMEZAWA Hiroyuki { 2840570a335bSHugh Dickins int err = 0; 2841570a335bSHugh Dickins 2842570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 2843570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 2844570a335bSHugh Dickins return err; 2845355cfa73SKAMEZAWA Hiroyuki } 28461da177e4SLinus Torvalds 2847cb4b86baSKAMEZAWA Hiroyuki /* 2848355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 2849355cfa73SKAMEZAWA Hiroyuki * 285073c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 2851355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 2852355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 2853355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 2854cb4b86baSKAMEZAWA Hiroyuki */ 2855cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 2856cb4b86baSKAMEZAWA Hiroyuki { 2857253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 2858cb4b86baSKAMEZAWA Hiroyuki } 2859cb4b86baSKAMEZAWA Hiroyuki 2860f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 2861f981c595SMel Gorman { 2862f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2863f981c595SMel Gorman return swap_info[swp_type(swap)]; 2864f981c595SMel Gorman } 2865f981c595SMel Gorman 2866f981c595SMel Gorman /* 2867f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 2868f981c595SMel Gorman */ 2869f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 2870f981c595SMel Gorman { 2871309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 2872f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 2873f981c595SMel Gorman } 2874f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 2875f981c595SMel Gorman 2876f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 2877f981c595SMel Gorman { 2878f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2879309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 2880f981c595SMel Gorman return swp_offset(swap); 2881f981c595SMel Gorman } 2882f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 2883f981c595SMel Gorman 28841da177e4SLinus Torvalds /* 2885570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 2886570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 2887570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 2888570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 2889570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 2890570a335bSHugh Dickins * 2891570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 2892570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 2893570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 2894570a335bSHugh Dickins * 2895570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 2896570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 2897570a335bSHugh Dickins * can be called after dropping locks. 2898570a335bSHugh Dickins */ 2899570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 2900570a335bSHugh Dickins { 2901570a335bSHugh Dickins struct swap_info_struct *si; 2902*235b6217SHuang, Ying struct swap_cluster_info *ci; 2903570a335bSHugh Dickins struct page *head; 2904570a335bSHugh Dickins struct page *page; 2905570a335bSHugh Dickins struct page *list_page; 2906570a335bSHugh Dickins pgoff_t offset; 2907570a335bSHugh Dickins unsigned char count; 2908570a335bSHugh Dickins 2909570a335bSHugh Dickins /* 2910570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 2911570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 2912570a335bSHugh Dickins */ 2913570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 2914570a335bSHugh Dickins 2915570a335bSHugh Dickins si = swap_info_get(entry); 2916570a335bSHugh Dickins if (!si) { 2917570a335bSHugh Dickins /* 2918570a335bSHugh Dickins * An acceptable race has occurred since the failing 2919570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 2920570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 2921570a335bSHugh Dickins */ 2922570a335bSHugh Dickins goto outer; 2923570a335bSHugh Dickins } 2924570a335bSHugh Dickins 2925570a335bSHugh Dickins offset = swp_offset(entry); 2926*235b6217SHuang, Ying 2927*235b6217SHuang, Ying ci = lock_cluster(si, offset); 2928*235b6217SHuang, Ying 2929570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 2930570a335bSHugh Dickins 2931570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 2932570a335bSHugh Dickins /* 2933570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 2934570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 2935570a335bSHugh Dickins * over-provisioning. 2936570a335bSHugh Dickins */ 2937570a335bSHugh Dickins goto out; 2938570a335bSHugh Dickins } 2939570a335bSHugh Dickins 2940570a335bSHugh Dickins if (!page) { 2941*235b6217SHuang, Ying unlock_cluster(ci); 2942ec8acf20SShaohua Li spin_unlock(&si->lock); 2943570a335bSHugh Dickins return -ENOMEM; 2944570a335bSHugh Dickins } 2945570a335bSHugh Dickins 2946570a335bSHugh Dickins /* 2947570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 2948570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 2949570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 2950570a335bSHugh Dickins */ 2951570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2952570a335bSHugh Dickins offset &= ~PAGE_MASK; 2953570a335bSHugh Dickins 2954570a335bSHugh Dickins /* 2955570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 2956570a335bSHugh Dickins * but it does always reset its private field. 2957570a335bSHugh Dickins */ 2958570a335bSHugh Dickins if (!page_private(head)) { 2959570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 2960570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 2961570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 2962570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 2963570a335bSHugh Dickins } 2964570a335bSHugh Dickins 2965570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 2966570a335bSHugh Dickins unsigned char *map; 2967570a335bSHugh Dickins 2968570a335bSHugh Dickins /* 2969570a335bSHugh Dickins * If the previous map said no continuation, but we've found 2970570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 2971570a335bSHugh Dickins */ 2972570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 2973570a335bSHugh Dickins goto out; 2974570a335bSHugh Dickins 29759b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 2976570a335bSHugh Dickins count = *map; 29779b04c5feSCong Wang kunmap_atomic(map); 2978570a335bSHugh Dickins 2979570a335bSHugh Dickins /* 2980570a335bSHugh Dickins * If this continuation count now has some space in it, 2981570a335bSHugh Dickins * free our allocation and use this one. 2982570a335bSHugh Dickins */ 2983570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 2984570a335bSHugh Dickins goto out; 2985570a335bSHugh Dickins } 2986570a335bSHugh Dickins 2987570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 2988570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 2989570a335bSHugh Dickins out: 2990*235b6217SHuang, Ying unlock_cluster(ci); 2991ec8acf20SShaohua Li spin_unlock(&si->lock); 2992570a335bSHugh Dickins outer: 2993570a335bSHugh Dickins if (page) 2994570a335bSHugh Dickins __free_page(page); 2995570a335bSHugh Dickins return 0; 2996570a335bSHugh Dickins } 2997570a335bSHugh Dickins 2998570a335bSHugh Dickins /* 2999570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3000570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3001570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3002570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3003570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3004*235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3005*235b6217SHuang, Ying * lock. 3006570a335bSHugh Dickins */ 3007570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3008570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3009570a335bSHugh Dickins { 3010570a335bSHugh Dickins struct page *head; 3011570a335bSHugh Dickins struct page *page; 3012570a335bSHugh Dickins unsigned char *map; 3013570a335bSHugh Dickins 3014570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3015570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3016570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3017570a335bSHugh Dickins return false; /* need to add count continuation */ 3018570a335bSHugh Dickins } 3019570a335bSHugh Dickins 3020570a335bSHugh Dickins offset &= ~PAGE_MASK; 3021570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 30229b04c5feSCong Wang map = kmap_atomic(page) + offset; 3023570a335bSHugh Dickins 3024570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3025570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3026570a335bSHugh Dickins 3027570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3028570a335bSHugh Dickins /* 3029570a335bSHugh Dickins * Think of how you add 1 to 999 3030570a335bSHugh Dickins */ 3031570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 30329b04c5feSCong Wang kunmap_atomic(map); 3033570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3034570a335bSHugh Dickins BUG_ON(page == head); 30359b04c5feSCong Wang map = kmap_atomic(page) + offset; 3036570a335bSHugh Dickins } 3037570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 30389b04c5feSCong Wang kunmap_atomic(map); 3039570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3040570a335bSHugh Dickins if (page == head) 3041570a335bSHugh Dickins return false; /* add count continuation */ 30429b04c5feSCong Wang map = kmap_atomic(page) + offset; 3043570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3044570a335bSHugh Dickins } 3045570a335bSHugh Dickins *map += 1; 30469b04c5feSCong Wang kunmap_atomic(map); 3047570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3048570a335bSHugh Dickins while (page != head) { 30499b04c5feSCong Wang map = kmap_atomic(page) + offset; 3050570a335bSHugh Dickins *map = COUNT_CONTINUED; 30519b04c5feSCong Wang kunmap_atomic(map); 3052570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3053570a335bSHugh Dickins } 3054570a335bSHugh Dickins return true; /* incremented */ 3055570a335bSHugh Dickins 3056570a335bSHugh Dickins } else { /* decrementing */ 3057570a335bSHugh Dickins /* 3058570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3059570a335bSHugh Dickins */ 3060570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3061570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 30629b04c5feSCong Wang kunmap_atomic(map); 3063570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3064570a335bSHugh Dickins BUG_ON(page == head); 30659b04c5feSCong Wang map = kmap_atomic(page) + offset; 3066570a335bSHugh Dickins } 3067570a335bSHugh Dickins BUG_ON(*map == 0); 3068570a335bSHugh Dickins *map -= 1; 3069570a335bSHugh Dickins if (*map == 0) 3070570a335bSHugh Dickins count = 0; 30719b04c5feSCong Wang kunmap_atomic(map); 3072570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3073570a335bSHugh Dickins while (page != head) { 30749b04c5feSCong Wang map = kmap_atomic(page) + offset; 3075570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3076570a335bSHugh Dickins count = COUNT_CONTINUED; 30779b04c5feSCong Wang kunmap_atomic(map); 3078570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3079570a335bSHugh Dickins } 3080570a335bSHugh Dickins return count == COUNT_CONTINUED; 3081570a335bSHugh Dickins } 3082570a335bSHugh Dickins } 3083570a335bSHugh Dickins 3084570a335bSHugh Dickins /* 3085570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3086570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3087570a335bSHugh Dickins */ 3088570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3089570a335bSHugh Dickins { 3090570a335bSHugh Dickins pgoff_t offset; 3091570a335bSHugh Dickins 3092570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3093570a335bSHugh Dickins struct page *head; 3094570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3095570a335bSHugh Dickins if (page_private(head)) { 30960d576d20SGeliang Tang struct page *page, *next; 30970d576d20SGeliang Tang 30980d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 30990d576d20SGeliang Tang list_del(&page->lru); 3100570a335bSHugh Dickins __free_page(page); 3101570a335bSHugh Dickins } 3102570a335bSHugh Dickins } 3103570a335bSHugh Dickins } 3104570a335bSHugh Dickins } 3105