11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/swapfile.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/mm.h> 96e84f315SIngo Molnar #include <linux/sched/mm.h> 1029930025SIngo Molnar #include <linux/sched/task.h> 111da177e4SLinus Torvalds #include <linux/hugetlb.h> 121da177e4SLinus Torvalds #include <linux/mman.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 141da177e4SLinus Torvalds #include <linux/kernel_stat.h> 151da177e4SLinus Torvalds #include <linux/swap.h> 161da177e4SLinus Torvalds #include <linux/vmalloc.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/namei.h> 19072441e2SHugh Dickins #include <linux/shmem_fs.h> 201da177e4SLinus Torvalds #include <linux/blkdev.h> 2120137a49SHugh Dickins #include <linux/random.h> 221da177e4SLinus Torvalds #include <linux/writeback.h> 231da177e4SLinus Torvalds #include <linux/proc_fs.h> 241da177e4SLinus Torvalds #include <linux/seq_file.h> 251da177e4SLinus Torvalds #include <linux/init.h> 265ad64688SHugh Dickins #include <linux/ksm.h> 271da177e4SLinus Torvalds #include <linux/rmap.h> 281da177e4SLinus Torvalds #include <linux/security.h> 291da177e4SLinus Torvalds #include <linux/backing-dev.h> 30fc0abb14SIngo Molnar #include <linux/mutex.h> 31c59ede7bSRandy.Dunlap #include <linux/capability.h> 321da177e4SLinus Torvalds #include <linux/syscalls.h> 338a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3466d7dd51SKay Sievers #include <linux/poll.h> 3572788c38SDavid Rientjes #include <linux/oom.h> 3638b5faf4SDan Magenheimer #include <linux/frontswap.h> 3738b5faf4SDan Magenheimer #include <linux/swapfile.h> 38f981c595SMel Gorman #include <linux/export.h> 3967afa38eSTim Chen #include <linux/swap_slots.h> 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds #include <asm/pgtable.h> 421da177e4SLinus Torvalds #include <asm/tlbflush.h> 431da177e4SLinus Torvalds #include <linux/swapops.h> 445d1ea48bSJohannes Weiner #include <linux/swap_cgroup.h> 451da177e4SLinus Torvalds 46570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 47570a335bSHugh Dickins unsigned char); 48570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 49d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 50570a335bSHugh Dickins 5138b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 527c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 53ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 54fb0fec50SChris Wilson /* 55fb0fec50SChris Wilson * Some modules use swappable objects and may try to swap them out under 56fb0fec50SChris Wilson * memory pressure (via the shrinker). Before doing so, they may wish to 57fb0fec50SChris Wilson * check to see if any swap space is available. 58fb0fec50SChris Wilson */ 59fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages); 60ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 611da177e4SLinus Torvalds long total_swap_pages; 6278ecba08SHugh Dickins static int least_priority; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 651da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 661da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 671da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 681da177e4SLinus Torvalds 69adfab836SDan Streetman /* 70adfab836SDan Streetman * all active swap_info_structs 71adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 72adfab836SDan Streetman */ 7318ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 7418ab4d4cSDan Streetman 7518ab4d4cSDan Streetman /* 7618ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 7718ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 7818ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 7918ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 8018ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 8118ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 8218ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 8318ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 8418ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 8518ab4d4cSDan Streetman * before any swap_info_struct->lock. 8618ab4d4cSDan Streetman */ 8718ab4d4cSDan Streetman static PLIST_HEAD(swap_avail_head); 8818ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 891da177e4SLinus Torvalds 9038b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 911da177e4SLinus Torvalds 92fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 931da177e4SLinus Torvalds 9466d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 9566d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 9666d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 9766d7dd51SKay Sievers 988d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 99355cfa73SKAMEZAWA Hiroyuki { 100570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 101355cfa73SKAMEZAWA Hiroyuki } 102355cfa73SKAMEZAWA Hiroyuki 103efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 104c9e44410SKAMEZAWA Hiroyuki static int 105c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 106c9e44410SKAMEZAWA Hiroyuki { 107efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 108c9e44410SKAMEZAWA Hiroyuki struct page *page; 109c9e44410SKAMEZAWA Hiroyuki int ret = 0; 110c9e44410SKAMEZAWA Hiroyuki 111f6ab1f7fSHuang Ying page = find_get_page(swap_address_space(entry), swp_offset(entry)); 112c9e44410SKAMEZAWA Hiroyuki if (!page) 113c9e44410SKAMEZAWA Hiroyuki return 0; 114c9e44410SKAMEZAWA Hiroyuki /* 115c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 116c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 117c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 118c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 119c9e44410SKAMEZAWA Hiroyuki * in usual operations. 120c9e44410SKAMEZAWA Hiroyuki */ 121c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 122c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 123c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 124c9e44410SKAMEZAWA Hiroyuki } 12509cbfeafSKirill A. Shutemov put_page(page); 126c9e44410SKAMEZAWA Hiroyuki return ret; 127c9e44410SKAMEZAWA Hiroyuki } 128355cfa73SKAMEZAWA Hiroyuki 1291da177e4SLinus Torvalds /* 1306a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1316a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1326a6ba831SHugh Dickins */ 1336a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1346a6ba831SHugh Dickins { 1356a6ba831SHugh Dickins struct swap_extent *se; 1369625a5f2SHugh Dickins sector_t start_block; 1379625a5f2SHugh Dickins sector_t nr_blocks; 1386a6ba831SHugh Dickins int err = 0; 1396a6ba831SHugh Dickins 1406a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1419625a5f2SHugh Dickins se = &si->first_swap_extent; 1429625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1439625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1449625a5f2SHugh Dickins if (nr_blocks) { 1459625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 146dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1479625a5f2SHugh Dickins if (err) 1489625a5f2SHugh Dickins return err; 1499625a5f2SHugh Dickins cond_resched(); 1506a6ba831SHugh Dickins } 1516a6ba831SHugh Dickins 1529625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1539625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1549625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1559625a5f2SHugh Dickins 1566a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 157dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1586a6ba831SHugh Dickins if (err) 1596a6ba831SHugh Dickins break; 1606a6ba831SHugh Dickins 1616a6ba831SHugh Dickins cond_resched(); 1626a6ba831SHugh Dickins } 1636a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1646a6ba831SHugh Dickins } 1656a6ba831SHugh Dickins 1667992fde7SHugh Dickins /* 1677992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1687992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1697992fde7SHugh Dickins */ 1707992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1717992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1727992fde7SHugh Dickins { 1737992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1747992fde7SHugh Dickins int found_extent = 0; 1757992fde7SHugh Dickins 1767992fde7SHugh Dickins while (nr_pages) { 1777992fde7SHugh Dickins if (se->start_page <= start_page && 1787992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1797992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1807992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 181858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1827992fde7SHugh Dickins 1837992fde7SHugh Dickins if (nr_blocks > nr_pages) 1847992fde7SHugh Dickins nr_blocks = nr_pages; 1857992fde7SHugh Dickins start_page += nr_blocks; 1867992fde7SHugh Dickins nr_pages -= nr_blocks; 1877992fde7SHugh Dickins 1887992fde7SHugh Dickins if (!found_extent++) 1897992fde7SHugh Dickins si->curr_swap_extent = se; 1907992fde7SHugh Dickins 1917992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1927992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1937992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 194dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1957992fde7SHugh Dickins break; 1967992fde7SHugh Dickins } 1977992fde7SHugh Dickins 198a8ae4991SGeliang Tang se = list_next_entry(se, list); 1997992fde7SHugh Dickins } 2007992fde7SHugh Dickins } 2017992fde7SHugh Dickins 202*38d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 203*38d8b4e6SHuang Ying #define SWAPFILE_CLUSTER HPAGE_PMD_NR 204*38d8b4e6SHuang Ying #else 205048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 206*38d8b4e6SHuang Ying #endif 207048c27fdSHugh Dickins #define LATENCY_LIMIT 256 208048c27fdSHugh Dickins 2092a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2102a8f9449SShaohua Li unsigned int flag) 2112a8f9449SShaohua Li { 2122a8f9449SShaohua Li info->flags = flag; 2132a8f9449SShaohua Li } 2142a8f9449SShaohua Li 2152a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2162a8f9449SShaohua Li { 2172a8f9449SShaohua Li return info->data; 2182a8f9449SShaohua Li } 2192a8f9449SShaohua Li 2202a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2212a8f9449SShaohua Li unsigned int c) 2222a8f9449SShaohua Li { 2232a8f9449SShaohua Li info->data = c; 2242a8f9449SShaohua Li } 2252a8f9449SShaohua Li 2262a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2272a8f9449SShaohua Li unsigned int c, unsigned int f) 2282a8f9449SShaohua Li { 2292a8f9449SShaohua Li info->flags = f; 2302a8f9449SShaohua Li info->data = c; 2312a8f9449SShaohua Li } 2322a8f9449SShaohua Li 2332a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2342a8f9449SShaohua Li { 2352a8f9449SShaohua Li return info->data; 2362a8f9449SShaohua Li } 2372a8f9449SShaohua Li 2382a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2392a8f9449SShaohua Li unsigned int n) 2402a8f9449SShaohua Li { 2412a8f9449SShaohua Li info->data = n; 2422a8f9449SShaohua Li } 2432a8f9449SShaohua Li 2442a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 2452a8f9449SShaohua Li unsigned int n, unsigned int f) 2462a8f9449SShaohua Li { 2472a8f9449SShaohua Li info->flags = f; 2482a8f9449SShaohua Li info->data = n; 2492a8f9449SShaohua Li } 2502a8f9449SShaohua Li 2512a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 2522a8f9449SShaohua Li { 2532a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 2542a8f9449SShaohua Li } 2552a8f9449SShaohua Li 2562a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 2572a8f9449SShaohua Li { 2582a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 2592a8f9449SShaohua Li } 2602a8f9449SShaohua Li 2612a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 2622a8f9449SShaohua Li { 2632a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 2642a8f9449SShaohua Li info->data = 0; 2652a8f9449SShaohua Li } 2662a8f9449SShaohua Li 267235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 268235b6217SHuang, Ying unsigned long offset) 269235b6217SHuang, Ying { 270235b6217SHuang, Ying struct swap_cluster_info *ci; 271235b6217SHuang, Ying 272235b6217SHuang, Ying ci = si->cluster_info; 273235b6217SHuang, Ying if (ci) { 274235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 275235b6217SHuang, Ying spin_lock(&ci->lock); 276235b6217SHuang, Ying } 277235b6217SHuang, Ying return ci; 278235b6217SHuang, Ying } 279235b6217SHuang, Ying 280235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 281235b6217SHuang, Ying { 282235b6217SHuang, Ying if (ci) 283235b6217SHuang, Ying spin_unlock(&ci->lock); 284235b6217SHuang, Ying } 285235b6217SHuang, Ying 286235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 287235b6217SHuang, Ying struct swap_info_struct *si, 288235b6217SHuang, Ying unsigned long offset) 289235b6217SHuang, Ying { 290235b6217SHuang, Ying struct swap_cluster_info *ci; 291235b6217SHuang, Ying 292235b6217SHuang, Ying ci = lock_cluster(si, offset); 293235b6217SHuang, Ying if (!ci) 294235b6217SHuang, Ying spin_lock(&si->lock); 295235b6217SHuang, Ying 296235b6217SHuang, Ying return ci; 297235b6217SHuang, Ying } 298235b6217SHuang, Ying 299235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 300235b6217SHuang, Ying struct swap_cluster_info *ci) 301235b6217SHuang, Ying { 302235b6217SHuang, Ying if (ci) 303235b6217SHuang, Ying unlock_cluster(ci); 304235b6217SHuang, Ying else 305235b6217SHuang, Ying spin_unlock(&si->lock); 306235b6217SHuang, Ying } 307235b6217SHuang, Ying 3086b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3096b534915SHuang Ying { 3106b534915SHuang Ying return cluster_is_null(&list->head); 3116b534915SHuang Ying } 3126b534915SHuang Ying 3136b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 3146b534915SHuang Ying { 3156b534915SHuang Ying return cluster_next(&list->head); 3166b534915SHuang Ying } 3176b534915SHuang Ying 3186b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 3196b534915SHuang Ying { 3206b534915SHuang Ying cluster_set_null(&list->head); 3216b534915SHuang Ying cluster_set_null(&list->tail); 3226b534915SHuang Ying } 3236b534915SHuang Ying 3246b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 3256b534915SHuang Ying struct swap_cluster_info *ci, 3266b534915SHuang Ying unsigned int idx) 3276b534915SHuang Ying { 3286b534915SHuang Ying if (cluster_list_empty(list)) { 3296b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 3306b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3316b534915SHuang Ying } else { 332235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 3336b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 3346b534915SHuang Ying 335235b6217SHuang, Ying /* 336235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 337235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 338235b6217SHuang, Ying */ 339235b6217SHuang, Ying ci_tail = ci + tail; 340235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 341235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 3420ef017d1SHuang Ying spin_unlock(&ci_tail->lock); 3436b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3446b534915SHuang Ying } 3456b534915SHuang Ying } 3466b534915SHuang Ying 3476b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 3486b534915SHuang Ying struct swap_cluster_info *ci) 3496b534915SHuang Ying { 3506b534915SHuang Ying unsigned int idx; 3516b534915SHuang Ying 3526b534915SHuang Ying idx = cluster_next(&list->head); 3536b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 3546b534915SHuang Ying cluster_set_null(&list->head); 3556b534915SHuang Ying cluster_set_null(&list->tail); 3566b534915SHuang Ying } else 3576b534915SHuang Ying cluster_set_next_flag(&list->head, 3586b534915SHuang Ying cluster_next(&ci[idx]), 0); 3596b534915SHuang Ying 3606b534915SHuang Ying return idx; 3616b534915SHuang Ying } 3626b534915SHuang Ying 363815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 364815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 365815c2c54SShaohua Li unsigned int idx) 366815c2c54SShaohua Li { 367815c2c54SShaohua Li /* 368815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 369815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 370815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 371815c2c54SShaohua Li * will be cleared after discard 372815c2c54SShaohua Li */ 373815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 374815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 375815c2c54SShaohua Li 3766b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 377815c2c54SShaohua Li 378815c2c54SShaohua Li schedule_work(&si->discard_work); 379815c2c54SShaohua Li } 380815c2c54SShaohua Li 381*38d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx) 382*38d8b4e6SHuang Ying { 383*38d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 384*38d8b4e6SHuang Ying 385*38d8b4e6SHuang Ying cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE); 386*38d8b4e6SHuang Ying cluster_list_add_tail(&si->free_clusters, ci, idx); 387*38d8b4e6SHuang Ying } 388*38d8b4e6SHuang Ying 389815c2c54SShaohua Li /* 390815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 391815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 392815c2c54SShaohua Li */ 393815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 394815c2c54SShaohua Li { 395235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 396815c2c54SShaohua Li unsigned int idx; 397815c2c54SShaohua Li 398815c2c54SShaohua Li info = si->cluster_info; 399815c2c54SShaohua Li 4006b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 4016b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 402815c2c54SShaohua Li spin_unlock(&si->lock); 403815c2c54SShaohua Li 404815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 405815c2c54SShaohua Li SWAPFILE_CLUSTER); 406815c2c54SShaohua Li 407815c2c54SShaohua Li spin_lock(&si->lock); 408235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 409*38d8b4e6SHuang Ying __free_cluster(si, idx); 410815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 411815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 412235b6217SHuang, Ying unlock_cluster(ci); 413815c2c54SShaohua Li } 414815c2c54SShaohua Li } 415815c2c54SShaohua Li 416815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 417815c2c54SShaohua Li { 418815c2c54SShaohua Li struct swap_info_struct *si; 419815c2c54SShaohua Li 420815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 421815c2c54SShaohua Li 422815c2c54SShaohua Li spin_lock(&si->lock); 423815c2c54SShaohua Li swap_do_scheduled_discard(si); 424815c2c54SShaohua Li spin_unlock(&si->lock); 425815c2c54SShaohua Li } 426815c2c54SShaohua Li 427*38d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx) 428*38d8b4e6SHuang Ying { 429*38d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 430*38d8b4e6SHuang Ying 431*38d8b4e6SHuang Ying VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx); 432*38d8b4e6SHuang Ying cluster_list_del_first(&si->free_clusters, ci); 433*38d8b4e6SHuang Ying cluster_set_count_flag(ci + idx, 0, 0); 434*38d8b4e6SHuang Ying } 435*38d8b4e6SHuang Ying 436*38d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx) 437*38d8b4e6SHuang Ying { 438*38d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info + idx; 439*38d8b4e6SHuang Ying 440*38d8b4e6SHuang Ying VM_BUG_ON(cluster_count(ci) != 0); 441*38d8b4e6SHuang Ying /* 442*38d8b4e6SHuang Ying * If the swap is discardable, prepare discard the cluster 443*38d8b4e6SHuang Ying * instead of free it immediately. The cluster will be freed 444*38d8b4e6SHuang Ying * after discard. 445*38d8b4e6SHuang Ying */ 446*38d8b4e6SHuang Ying if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 447*38d8b4e6SHuang Ying (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 448*38d8b4e6SHuang Ying swap_cluster_schedule_discard(si, idx); 449*38d8b4e6SHuang Ying return; 450*38d8b4e6SHuang Ying } 451*38d8b4e6SHuang Ying 452*38d8b4e6SHuang Ying __free_cluster(si, idx); 453*38d8b4e6SHuang Ying } 454*38d8b4e6SHuang Ying 4552a8f9449SShaohua Li /* 4562a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 4572a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 4582a8f9449SShaohua Li */ 4592a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 4602a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4612a8f9449SShaohua Li { 4622a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4632a8f9449SShaohua Li 4642a8f9449SShaohua Li if (!cluster_info) 4652a8f9449SShaohua Li return; 466*38d8b4e6SHuang Ying if (cluster_is_free(&cluster_info[idx])) 467*38d8b4e6SHuang Ying alloc_cluster(p, idx); 4682a8f9449SShaohua Li 4692a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 4702a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4712a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 4722a8f9449SShaohua Li } 4732a8f9449SShaohua Li 4742a8f9449SShaohua Li /* 4752a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 4762a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 4772a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 4782a8f9449SShaohua Li */ 4792a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 4802a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4812a8f9449SShaohua Li { 4822a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4832a8f9449SShaohua Li 4842a8f9449SShaohua Li if (!cluster_info) 4852a8f9449SShaohua Li return; 4862a8f9449SShaohua Li 4872a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 4882a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4892a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 4902a8f9449SShaohua Li 491*38d8b4e6SHuang Ying if (cluster_count(&cluster_info[idx]) == 0) 492*38d8b4e6SHuang Ying free_cluster(p, idx); 4932a8f9449SShaohua Li } 4942a8f9449SShaohua Li 4952a8f9449SShaohua Li /* 4962a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 4972a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 4982a8f9449SShaohua Li */ 499ebc2a1a6SShaohua Li static bool 500ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 5012a8f9449SShaohua Li unsigned long offset) 5022a8f9449SShaohua Li { 503ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 504ebc2a1a6SShaohua Li bool conflict; 505ebc2a1a6SShaohua Li 5062a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 5076b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 5086b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 5092a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 510ebc2a1a6SShaohua Li 511ebc2a1a6SShaohua Li if (!conflict) 512ebc2a1a6SShaohua Li return false; 513ebc2a1a6SShaohua Li 514ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 515ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 516ebc2a1a6SShaohua Li return true; 517ebc2a1a6SShaohua Li } 518ebc2a1a6SShaohua Li 519ebc2a1a6SShaohua Li /* 520ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 521ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 522ebc2a1a6SShaohua Li */ 52336005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 524ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 525ebc2a1a6SShaohua Li { 526ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 527235b6217SHuang, Ying struct swap_cluster_info *ci; 528ebc2a1a6SShaohua Li bool found_free; 529235b6217SHuang, Ying unsigned long tmp, max; 530ebc2a1a6SShaohua Li 531ebc2a1a6SShaohua Li new_cluster: 532ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 533ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 5346b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 5356b534915SHuang Ying cluster->index = si->free_clusters.head; 536ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 537ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 5386b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 539ebc2a1a6SShaohua Li /* 540ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 541ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 542ebc2a1a6SShaohua Li */ 543ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 544ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 545ebc2a1a6SShaohua Li goto new_cluster; 546ebc2a1a6SShaohua Li } else 54736005baeSTim Chen return false; 548ebc2a1a6SShaohua Li } 549ebc2a1a6SShaohua Li 550ebc2a1a6SShaohua Li found_free = false; 551ebc2a1a6SShaohua Li 552ebc2a1a6SShaohua Li /* 553ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 554ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 555ebc2a1a6SShaohua Li */ 556ebc2a1a6SShaohua Li tmp = cluster->next; 557235b6217SHuang, Ying max = min_t(unsigned long, si->max, 558235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 559235b6217SHuang, Ying if (tmp >= max) { 560235b6217SHuang, Ying cluster_set_null(&cluster->index); 561235b6217SHuang, Ying goto new_cluster; 562235b6217SHuang, Ying } 563235b6217SHuang, Ying ci = lock_cluster(si, tmp); 564235b6217SHuang, Ying while (tmp < max) { 565ebc2a1a6SShaohua Li if (!si->swap_map[tmp]) { 566ebc2a1a6SShaohua Li found_free = true; 567ebc2a1a6SShaohua Li break; 568ebc2a1a6SShaohua Li } 569ebc2a1a6SShaohua Li tmp++; 570ebc2a1a6SShaohua Li } 571235b6217SHuang, Ying unlock_cluster(ci); 572ebc2a1a6SShaohua Li if (!found_free) { 573ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 574ebc2a1a6SShaohua Li goto new_cluster; 575ebc2a1a6SShaohua Li } 576ebc2a1a6SShaohua Li cluster->next = tmp + 1; 577ebc2a1a6SShaohua Li *offset = tmp; 578ebc2a1a6SShaohua Li *scan_base = tmp; 57936005baeSTim Chen return found_free; 5802a8f9449SShaohua Li } 5812a8f9449SShaohua Li 582*38d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, 583*38d8b4e6SHuang Ying unsigned int nr_entries) 584*38d8b4e6SHuang Ying { 585*38d8b4e6SHuang Ying unsigned int end = offset + nr_entries - 1; 586*38d8b4e6SHuang Ying 587*38d8b4e6SHuang Ying if (offset == si->lowest_bit) 588*38d8b4e6SHuang Ying si->lowest_bit += nr_entries; 589*38d8b4e6SHuang Ying if (end == si->highest_bit) 590*38d8b4e6SHuang Ying si->highest_bit -= nr_entries; 591*38d8b4e6SHuang Ying si->inuse_pages += nr_entries; 592*38d8b4e6SHuang Ying if (si->inuse_pages == si->pages) { 593*38d8b4e6SHuang Ying si->lowest_bit = si->max; 594*38d8b4e6SHuang Ying si->highest_bit = 0; 595*38d8b4e6SHuang Ying spin_lock(&swap_avail_lock); 596*38d8b4e6SHuang Ying plist_del(&si->avail_list, &swap_avail_head); 597*38d8b4e6SHuang Ying spin_unlock(&swap_avail_lock); 598*38d8b4e6SHuang Ying } 599*38d8b4e6SHuang Ying } 600*38d8b4e6SHuang Ying 601*38d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset, 602*38d8b4e6SHuang Ying unsigned int nr_entries) 603*38d8b4e6SHuang Ying { 604*38d8b4e6SHuang Ying unsigned long end = offset + nr_entries - 1; 605*38d8b4e6SHuang Ying void (*swap_slot_free_notify)(struct block_device *, unsigned long); 606*38d8b4e6SHuang Ying 607*38d8b4e6SHuang Ying if (offset < si->lowest_bit) 608*38d8b4e6SHuang Ying si->lowest_bit = offset; 609*38d8b4e6SHuang Ying if (end > si->highest_bit) { 610*38d8b4e6SHuang Ying bool was_full = !si->highest_bit; 611*38d8b4e6SHuang Ying 612*38d8b4e6SHuang Ying si->highest_bit = end; 613*38d8b4e6SHuang Ying if (was_full && (si->flags & SWP_WRITEOK)) { 614*38d8b4e6SHuang Ying spin_lock(&swap_avail_lock); 615*38d8b4e6SHuang Ying WARN_ON(!plist_node_empty(&si->avail_list)); 616*38d8b4e6SHuang Ying if (plist_node_empty(&si->avail_list)) 617*38d8b4e6SHuang Ying plist_add(&si->avail_list, &swap_avail_head); 618*38d8b4e6SHuang Ying spin_unlock(&swap_avail_lock); 619*38d8b4e6SHuang Ying } 620*38d8b4e6SHuang Ying } 621*38d8b4e6SHuang Ying atomic_long_add(nr_entries, &nr_swap_pages); 622*38d8b4e6SHuang Ying si->inuse_pages -= nr_entries; 623*38d8b4e6SHuang Ying if (si->flags & SWP_BLKDEV) 624*38d8b4e6SHuang Ying swap_slot_free_notify = 625*38d8b4e6SHuang Ying si->bdev->bd_disk->fops->swap_slot_free_notify; 626*38d8b4e6SHuang Ying else 627*38d8b4e6SHuang Ying swap_slot_free_notify = NULL; 628*38d8b4e6SHuang Ying while (offset <= end) { 629*38d8b4e6SHuang Ying frontswap_invalidate_page(si->type, offset); 630*38d8b4e6SHuang Ying if (swap_slot_free_notify) 631*38d8b4e6SHuang Ying swap_slot_free_notify(si->bdev, offset); 632*38d8b4e6SHuang Ying offset++; 633*38d8b4e6SHuang Ying } 634*38d8b4e6SHuang Ying } 635*38d8b4e6SHuang Ying 63636005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si, 63736005baeSTim Chen unsigned char usage, int nr, 63836005baeSTim Chen swp_entry_t slots[]) 6391da177e4SLinus Torvalds { 640235b6217SHuang, Ying struct swap_cluster_info *ci; 641ebebbbe9SHugh Dickins unsigned long offset; 642c60aa176SHugh Dickins unsigned long scan_base; 6437992fde7SHugh Dickins unsigned long last_in_cluster = 0; 644048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 64536005baeSTim Chen int n_ret = 0; 64636005baeSTim Chen 64736005baeSTim Chen if (nr > SWAP_BATCH) 64836005baeSTim Chen nr = SWAP_BATCH; 6491da177e4SLinus Torvalds 6507dfad418SHugh Dickins /* 6517dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 6527dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 6537dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 6547dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 6557dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 6567dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 6577dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 658c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 6591da177e4SLinus Torvalds */ 6607dfad418SHugh Dickins 66152b7efdbSHugh Dickins si->flags += SWP_SCANNING; 662c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 663ebebbbe9SHugh Dickins 664ebc2a1a6SShaohua Li /* SSD algorithm */ 665ebc2a1a6SShaohua Li if (si->cluster_info) { 66636005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 667815c2c54SShaohua Li goto checks; 66836005baeSTim Chen else 66936005baeSTim Chen goto scan; 670815c2c54SShaohua Li } 671815c2c54SShaohua Li 672ebc2a1a6SShaohua Li if (unlikely(!si->cluster_nr--)) { 673ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 674ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 6752a8f9449SShaohua Li goto checks; 6762a8f9449SShaohua Li } 6772a8f9449SShaohua Li 678ec8acf20SShaohua Li spin_unlock(&si->lock); 6797dfad418SHugh Dickins 680c60aa176SHugh Dickins /* 681c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 682c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 68350088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 68450088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 685c60aa176SHugh Dickins */ 686c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 6877dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 6887dfad418SHugh Dickins 6897dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 6907dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 6911da177e4SLinus Torvalds if (si->swap_map[offset]) 6927dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 6937dfad418SHugh Dickins else if (offset == last_in_cluster) { 694ec8acf20SShaohua Li spin_lock(&si->lock); 695ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 696ebebbbe9SHugh Dickins si->cluster_next = offset; 697ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 698ebebbbe9SHugh Dickins goto checks; 6997dfad418SHugh Dickins } 700048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 701048c27fdSHugh Dickins cond_resched(); 702048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 703048c27fdSHugh Dickins } 7047dfad418SHugh Dickins } 705ebebbbe9SHugh Dickins 706c60aa176SHugh Dickins offset = scan_base; 707ec8acf20SShaohua Li spin_lock(&si->lock); 708ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 7097dfad418SHugh Dickins } 7107dfad418SHugh Dickins 711ebebbbe9SHugh Dickins checks: 712ebc2a1a6SShaohua Li if (si->cluster_info) { 71336005baeSTim Chen while (scan_swap_map_ssd_cluster_conflict(si, offset)) { 71436005baeSTim Chen /* take a break if we already got some slots */ 71536005baeSTim Chen if (n_ret) 71636005baeSTim Chen goto done; 71736005baeSTim Chen if (!scan_swap_map_try_ssd_cluster(si, &offset, 71836005baeSTim Chen &scan_base)) 71936005baeSTim Chen goto scan; 72036005baeSTim Chen } 721ebc2a1a6SShaohua Li } 722ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 72352b7efdbSHugh Dickins goto no_page; 7247dfad418SHugh Dickins if (!si->highest_bit) 7257dfad418SHugh Dickins goto no_page; 726ebebbbe9SHugh Dickins if (offset > si->highest_bit) 727c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 728c9e44410SKAMEZAWA Hiroyuki 729235b6217SHuang, Ying ci = lock_cluster(si, offset); 730b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 731b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 732c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 733235b6217SHuang, Ying unlock_cluster(ci); 734ec8acf20SShaohua Li spin_unlock(&si->lock); 735c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 736ec8acf20SShaohua Li spin_lock(&si->lock); 737c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 738c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 739c9e44410SKAMEZAWA Hiroyuki goto checks; 740c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 741c9e44410SKAMEZAWA Hiroyuki } 742c9e44410SKAMEZAWA Hiroyuki 743235b6217SHuang, Ying if (si->swap_map[offset]) { 744235b6217SHuang, Ying unlock_cluster(ci); 74536005baeSTim Chen if (!n_ret) 746ebebbbe9SHugh Dickins goto scan; 74736005baeSTim Chen else 74836005baeSTim Chen goto done; 749235b6217SHuang, Ying } 7502872bb2dSHuang Ying si->swap_map[offset] = usage; 7512872bb2dSHuang Ying inc_cluster_info_page(si, si->cluster_info, offset); 7522872bb2dSHuang Ying unlock_cluster(ci); 753ebebbbe9SHugh Dickins 754*38d8b4e6SHuang Ying swap_range_alloc(si, offset, 1); 7551da177e4SLinus Torvalds si->cluster_next = offset + 1; 75636005baeSTim Chen slots[n_ret++] = swp_entry(si->type, offset); 7577992fde7SHugh Dickins 75836005baeSTim Chen /* got enough slots or reach max slots? */ 75936005baeSTim Chen if ((n_ret == nr) || (offset >= si->highest_bit)) 76036005baeSTim Chen goto done; 76136005baeSTim Chen 76236005baeSTim Chen /* search for next available slot */ 76336005baeSTim Chen 76436005baeSTim Chen /* time to take a break? */ 76536005baeSTim Chen if (unlikely(--latency_ration < 0)) { 76636005baeSTim Chen if (n_ret) 76736005baeSTim Chen goto done; 76836005baeSTim Chen spin_unlock(&si->lock); 76936005baeSTim Chen cond_resched(); 77036005baeSTim Chen spin_lock(&si->lock); 77136005baeSTim Chen latency_ration = LATENCY_LIMIT; 77236005baeSTim Chen } 77336005baeSTim Chen 77436005baeSTim Chen /* try to get more slots in cluster */ 77536005baeSTim Chen if (si->cluster_info) { 77636005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 77736005baeSTim Chen goto checks; 77836005baeSTim Chen else 77936005baeSTim Chen goto done; 78036005baeSTim Chen } 78136005baeSTim Chen /* non-ssd case */ 78236005baeSTim Chen ++offset; 78336005baeSTim Chen 78436005baeSTim Chen /* non-ssd case, still more slots in cluster? */ 78536005baeSTim Chen if (si->cluster_nr && !si->swap_map[offset]) { 78636005baeSTim Chen --si->cluster_nr; 78736005baeSTim Chen goto checks; 78836005baeSTim Chen } 78936005baeSTim Chen 79036005baeSTim Chen done: 79136005baeSTim Chen si->flags -= SWP_SCANNING; 79236005baeSTim Chen return n_ret; 7937dfad418SHugh Dickins 794ebebbbe9SHugh Dickins scan: 795ec8acf20SShaohua Li spin_unlock(&si->lock); 7967dfad418SHugh Dickins while (++offset <= si->highest_bit) { 79752b7efdbSHugh Dickins if (!si->swap_map[offset]) { 798ec8acf20SShaohua Li spin_lock(&si->lock); 79952b7efdbSHugh Dickins goto checks; 8007dfad418SHugh Dickins } 801c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 802ec8acf20SShaohua Li spin_lock(&si->lock); 803c9e44410SKAMEZAWA Hiroyuki goto checks; 804c9e44410SKAMEZAWA Hiroyuki } 805048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 806048c27fdSHugh Dickins cond_resched(); 807048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 808048c27fdSHugh Dickins } 80952b7efdbSHugh Dickins } 810c60aa176SHugh Dickins offset = si->lowest_bit; 811a5998061SJamie Liu while (offset < scan_base) { 812c60aa176SHugh Dickins if (!si->swap_map[offset]) { 813ec8acf20SShaohua Li spin_lock(&si->lock); 814ebebbbe9SHugh Dickins goto checks; 815c60aa176SHugh Dickins } 816c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 817ec8acf20SShaohua Li spin_lock(&si->lock); 818c9e44410SKAMEZAWA Hiroyuki goto checks; 819c9e44410SKAMEZAWA Hiroyuki } 820c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 821c60aa176SHugh Dickins cond_resched(); 822c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 823c60aa176SHugh Dickins } 824a5998061SJamie Liu offset++; 825c60aa176SHugh Dickins } 826ec8acf20SShaohua Li spin_lock(&si->lock); 8277dfad418SHugh Dickins 8287dfad418SHugh Dickins no_page: 82952b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 83036005baeSTim Chen return n_ret; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds 833*38d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 834*38d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 835*38d8b4e6SHuang Ying { 836*38d8b4e6SHuang Ying unsigned long idx; 837*38d8b4e6SHuang Ying struct swap_cluster_info *ci; 838*38d8b4e6SHuang Ying unsigned long offset, i; 839*38d8b4e6SHuang Ying unsigned char *map; 840*38d8b4e6SHuang Ying 841*38d8b4e6SHuang Ying if (cluster_list_empty(&si->free_clusters)) 842*38d8b4e6SHuang Ying return 0; 843*38d8b4e6SHuang Ying 844*38d8b4e6SHuang Ying idx = cluster_list_first(&si->free_clusters); 845*38d8b4e6SHuang Ying offset = idx * SWAPFILE_CLUSTER; 846*38d8b4e6SHuang Ying ci = lock_cluster(si, offset); 847*38d8b4e6SHuang Ying alloc_cluster(si, idx); 848*38d8b4e6SHuang Ying cluster_set_count_flag(ci, SWAPFILE_CLUSTER, 0); 849*38d8b4e6SHuang Ying 850*38d8b4e6SHuang Ying map = si->swap_map + offset; 851*38d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 852*38d8b4e6SHuang Ying map[i] = SWAP_HAS_CACHE; 853*38d8b4e6SHuang Ying unlock_cluster(ci); 854*38d8b4e6SHuang Ying swap_range_alloc(si, offset, SWAPFILE_CLUSTER); 855*38d8b4e6SHuang Ying *slot = swp_entry(si->type, offset); 856*38d8b4e6SHuang Ying 857*38d8b4e6SHuang Ying return 1; 858*38d8b4e6SHuang Ying } 859*38d8b4e6SHuang Ying 860*38d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) 861*38d8b4e6SHuang Ying { 862*38d8b4e6SHuang Ying unsigned long offset = idx * SWAPFILE_CLUSTER; 863*38d8b4e6SHuang Ying struct swap_cluster_info *ci; 864*38d8b4e6SHuang Ying 865*38d8b4e6SHuang Ying ci = lock_cluster(si, offset); 866*38d8b4e6SHuang Ying cluster_set_count_flag(ci, 0, 0); 867*38d8b4e6SHuang Ying free_cluster(si, idx); 868*38d8b4e6SHuang Ying unlock_cluster(ci); 869*38d8b4e6SHuang Ying swap_range_free(si, offset, SWAPFILE_CLUSTER); 870*38d8b4e6SHuang Ying } 871*38d8b4e6SHuang Ying #else 872*38d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 873*38d8b4e6SHuang Ying { 874*38d8b4e6SHuang Ying VM_WARN_ON_ONCE(1); 875*38d8b4e6SHuang Ying return 0; 876*38d8b4e6SHuang Ying } 877*38d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 878*38d8b4e6SHuang Ying 87936005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si, 88036005baeSTim Chen unsigned char usage) 88136005baeSTim Chen { 88236005baeSTim Chen swp_entry_t entry; 88336005baeSTim Chen int n_ret; 88436005baeSTim Chen 88536005baeSTim Chen n_ret = scan_swap_map_slots(si, usage, 1, &entry); 88636005baeSTim Chen 88736005baeSTim Chen if (n_ret) 88836005baeSTim Chen return swp_offset(entry); 88936005baeSTim Chen else 89036005baeSTim Chen return 0; 89136005baeSTim Chen 89236005baeSTim Chen } 89336005baeSTim Chen 894*38d8b4e6SHuang Ying int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[]) 8951da177e4SLinus Torvalds { 896*38d8b4e6SHuang Ying unsigned long nr_pages = cluster ? SWAPFILE_CLUSTER : 1; 897adfab836SDan Streetman struct swap_info_struct *si, *next; 89836005baeSTim Chen long avail_pgs; 89936005baeSTim Chen int n_ret = 0; 9001da177e4SLinus Torvalds 901*38d8b4e6SHuang Ying /* Only single cluster request supported */ 902*38d8b4e6SHuang Ying WARN_ON_ONCE(n_goal > 1 && cluster); 903*38d8b4e6SHuang Ying 904*38d8b4e6SHuang Ying avail_pgs = atomic_long_read(&nr_swap_pages) / nr_pages; 90536005baeSTim Chen if (avail_pgs <= 0) 906fb4f88dcSHugh Dickins goto noswap; 90736005baeSTim Chen 90836005baeSTim Chen if (n_goal > SWAP_BATCH) 90936005baeSTim Chen n_goal = SWAP_BATCH; 91036005baeSTim Chen 91136005baeSTim Chen if (n_goal > avail_pgs) 91236005baeSTim Chen n_goal = avail_pgs; 91336005baeSTim Chen 914*38d8b4e6SHuang Ying atomic_long_sub(n_goal * nr_pages, &nr_swap_pages); 9151da177e4SLinus Torvalds 91618ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 91718ab4d4cSDan Streetman 91818ab4d4cSDan Streetman start_over: 91918ab4d4cSDan Streetman plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { 92018ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 92118ab4d4cSDan Streetman plist_requeue(&si->avail_list, &swap_avail_head); 92218ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 923ec8acf20SShaohua Li spin_lock(&si->lock); 924adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 92518ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 92618ab4d4cSDan Streetman if (plist_node_empty(&si->avail_list)) { 927ec8acf20SShaohua Li spin_unlock(&si->lock); 92818ab4d4cSDan Streetman goto nextsi; 92918ab4d4cSDan Streetman } 93018ab4d4cSDan Streetman WARN(!si->highest_bit, 93118ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 93218ab4d4cSDan Streetman si->type); 93318ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 93418ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 93518ab4d4cSDan Streetman si->type); 93618ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 93718ab4d4cSDan Streetman spin_unlock(&si->lock); 93818ab4d4cSDan Streetman goto nextsi; 939ec8acf20SShaohua Li } 940*38d8b4e6SHuang Ying if (cluster) 941*38d8b4e6SHuang Ying n_ret = swap_alloc_cluster(si, swp_entries); 942*38d8b4e6SHuang Ying else 94336005baeSTim Chen n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, 94436005baeSTim Chen n_goal, swp_entries); 945ec8acf20SShaohua Li spin_unlock(&si->lock); 946*38d8b4e6SHuang Ying if (n_ret || cluster) 94736005baeSTim Chen goto check_out; 94818ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 94918ab4d4cSDan Streetman si->type); 95036005baeSTim Chen 95118ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 95218ab4d4cSDan Streetman nextsi: 953adfab836SDan Streetman /* 954adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 955adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 956adfab836SDan Streetman * callers probably all tried to get a page from the same si 95718ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 95818ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 95918ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 96018ab4d4cSDan Streetman * list may have been modified; so if next is still in the 96136005baeSTim Chen * swap_avail_head list then try it, otherwise start over 96236005baeSTim Chen * if we have not gotten any slots. 963adfab836SDan Streetman */ 96418ab4d4cSDan Streetman if (plist_node_empty(&next->avail_list)) 96518ab4d4cSDan Streetman goto start_over; 966fb4f88dcSHugh Dickins } 967fb4f88dcSHugh Dickins 96818ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 96918ab4d4cSDan Streetman 97036005baeSTim Chen check_out: 97136005baeSTim Chen if (n_ret < n_goal) 972*38d8b4e6SHuang Ying atomic_long_add((long)(n_goal - n_ret) * nr_pages, 973*38d8b4e6SHuang Ying &nr_swap_pages); 974fb4f88dcSHugh Dickins noswap: 97536005baeSTim Chen return n_ret; 97636005baeSTim Chen } 97736005baeSTim Chen 9782de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 979910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 980910321eaSHugh Dickins { 981910321eaSHugh Dickins struct swap_info_struct *si; 982910321eaSHugh Dickins pgoff_t offset; 983910321eaSHugh Dickins 984910321eaSHugh Dickins si = swap_info[type]; 985ec8acf20SShaohua Li spin_lock(&si->lock); 986910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 987ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 988910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 989910321eaSHugh Dickins offset = scan_swap_map(si, 1); 990910321eaSHugh Dickins if (offset) { 991ec8acf20SShaohua Li spin_unlock(&si->lock); 992910321eaSHugh Dickins return swp_entry(type, offset); 993910321eaSHugh Dickins } 994ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 995910321eaSHugh Dickins } 996ec8acf20SShaohua Li spin_unlock(&si->lock); 997910321eaSHugh Dickins return (swp_entry_t) {0}; 998910321eaSHugh Dickins } 999910321eaSHugh Dickins 1000e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry) 10011da177e4SLinus Torvalds { 10021da177e4SLinus Torvalds struct swap_info_struct *p; 10031da177e4SLinus Torvalds unsigned long offset, type; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds if (!entry.val) 10061da177e4SLinus Torvalds goto out; 10071da177e4SLinus Torvalds type = swp_type(entry); 10081da177e4SLinus Torvalds if (type >= nr_swapfiles) 10091da177e4SLinus Torvalds goto bad_nofile; 1010efa90a98SHugh Dickins p = swap_info[type]; 10111da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 10121da177e4SLinus Torvalds goto bad_device; 10131da177e4SLinus Torvalds offset = swp_offset(entry); 10141da177e4SLinus Torvalds if (offset >= p->max) 10151da177e4SLinus Torvalds goto bad_offset; 10161da177e4SLinus Torvalds return p; 10171da177e4SLinus Torvalds 10181da177e4SLinus Torvalds bad_offset: 10196a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val); 10201da177e4SLinus Torvalds goto out; 10211da177e4SLinus Torvalds bad_device: 10226a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val); 10231da177e4SLinus Torvalds goto out; 10241da177e4SLinus Torvalds bad_nofile: 10256a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val); 10261da177e4SLinus Torvalds out: 10271da177e4SLinus Torvalds return NULL; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 1030e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 1031e8c26ab6STim Chen { 1032e8c26ab6STim Chen struct swap_info_struct *p; 1033e8c26ab6STim Chen 1034e8c26ab6STim Chen p = __swap_info_get(entry); 1035e8c26ab6STim Chen if (!p) 1036e8c26ab6STim Chen goto out; 1037e8c26ab6STim Chen if (!p->swap_map[swp_offset(entry)]) 1038e8c26ab6STim Chen goto bad_free; 1039e8c26ab6STim Chen return p; 1040e8c26ab6STim Chen 1041e8c26ab6STim Chen bad_free: 1042e8c26ab6STim Chen pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val); 1043e8c26ab6STim Chen goto out; 1044e8c26ab6STim Chen out: 1045e8c26ab6STim Chen return NULL; 1046e8c26ab6STim Chen } 1047e8c26ab6STim Chen 1048235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 10491da177e4SLinus Torvalds { 1050235b6217SHuang, Ying struct swap_info_struct *p; 1051235b6217SHuang, Ying 1052235b6217SHuang, Ying p = _swap_info_get(entry); 1053235b6217SHuang, Ying if (p) 1054235b6217SHuang, Ying spin_lock(&p->lock); 1055235b6217SHuang, Ying return p; 1056235b6217SHuang, Ying } 1057235b6217SHuang, Ying 10587c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, 10597c00bafeSTim Chen struct swap_info_struct *q) 10607c00bafeSTim Chen { 10617c00bafeSTim Chen struct swap_info_struct *p; 10627c00bafeSTim Chen 10637c00bafeSTim Chen p = _swap_info_get(entry); 10647c00bafeSTim Chen 10657c00bafeSTim Chen if (p != q) { 10667c00bafeSTim Chen if (q != NULL) 10677c00bafeSTim Chen spin_unlock(&q->lock); 10687c00bafeSTim Chen if (p != NULL) 10697c00bafeSTim Chen spin_lock(&p->lock); 10707c00bafeSTim Chen } 10717c00bafeSTim Chen return p; 10727c00bafeSTim Chen } 10737c00bafeSTim Chen 10747c00bafeSTim Chen static unsigned char __swap_entry_free(struct swap_info_struct *p, 10757c00bafeSTim Chen swp_entry_t entry, unsigned char usage) 1076235b6217SHuang, Ying { 1077235b6217SHuang, Ying struct swap_cluster_info *ci; 1078253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 10798d69aaeeSHugh Dickins unsigned char count; 10808d69aaeeSHugh Dickins unsigned char has_cache; 1081235b6217SHuang, Ying 10827c00bafeSTim Chen ci = lock_cluster_or_swap_info(p, offset); 10831da177e4SLinus Torvalds 1084355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 1085235b6217SHuang, Ying 1086253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 1087253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 1088253d553bSHugh Dickins 1089253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 1090253d553bSHugh Dickins VM_BUG_ON(!has_cache); 1091253d553bSHugh Dickins has_cache = 0; 1092aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 1093aaa46865SHugh Dickins /* 1094aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 1095aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 1096aaa46865SHugh Dickins */ 1097aaa46865SHugh Dickins count = 0; 1098570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 1099570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 1100570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 1101570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 1102570a335bSHugh Dickins else 1103570a335bSHugh Dickins count = SWAP_MAP_MAX; 1104570a335bSHugh Dickins } else 1105253d553bSHugh Dickins count--; 1106570a335bSHugh Dickins } 1107253d553bSHugh Dickins 1108253d553bSHugh Dickins usage = count | has_cache; 11097c00bafeSTim Chen p->swap_map[offset] = usage ? : SWAP_HAS_CACHE; 1110253d553bSHugh Dickins 11117c00bafeSTim Chen unlock_cluster_or_swap_info(p, ci); 1112235b6217SHuang, Ying 11137c00bafeSTim Chen return usage; 11147c00bafeSTim Chen } 11157c00bafeSTim Chen 11167c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) 11177c00bafeSTim Chen { 11187c00bafeSTim Chen struct swap_cluster_info *ci; 11197c00bafeSTim Chen unsigned long offset = swp_offset(entry); 11207c00bafeSTim Chen unsigned char count; 11217c00bafeSTim Chen 1122235b6217SHuang, Ying ci = lock_cluster(p, offset); 11237c00bafeSTim Chen count = p->swap_map[offset]; 11247c00bafeSTim Chen VM_BUG_ON(count != SWAP_HAS_CACHE); 11257c00bafeSTim Chen p->swap_map[offset] = 0; 11262a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 1127235b6217SHuang, Ying unlock_cluster(ci); 11287c00bafeSTim Chen 1129*38d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, 1); 1130*38d8b4e6SHuang Ying swap_range_free(p, offset, 1); 11311da177e4SLinus Torvalds } 1132253d553bSHugh Dickins 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 11351da177e4SLinus Torvalds * is still around or has not been recycled. 11361da177e4SLinus Torvalds */ 11371da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 11381da177e4SLinus Torvalds { 11391da177e4SLinus Torvalds struct swap_info_struct *p; 11401da177e4SLinus Torvalds 1141235b6217SHuang, Ying p = _swap_info_get(entry); 11427c00bafeSTim Chen if (p) { 11437c00bafeSTim Chen if (!__swap_entry_free(p, entry, 1)) 114467afa38eSTim Chen free_swap_slot(entry); 11457c00bafeSTim Chen } 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds /* 1149cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 1150cb4b86baSKAMEZAWA Hiroyuki */ 11510a31bc97SJohannes Weiner void swapcache_free(swp_entry_t entry) 1152cb4b86baSKAMEZAWA Hiroyuki { 1153355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 1154355cfa73SKAMEZAWA Hiroyuki 1155235b6217SHuang, Ying p = _swap_info_get(entry); 11567c00bafeSTim Chen if (p) { 11577c00bafeSTim Chen if (!__swap_entry_free(p, entry, SWAP_HAS_CACHE)) 115867afa38eSTim Chen free_swap_slot(entry); 11597c00bafeSTim Chen } 11607c00bafeSTim Chen } 11617c00bafeSTim Chen 1162*38d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 1163*38d8b4e6SHuang Ying void swapcache_free_cluster(swp_entry_t entry) 1164*38d8b4e6SHuang Ying { 1165*38d8b4e6SHuang Ying unsigned long offset = swp_offset(entry); 1166*38d8b4e6SHuang Ying unsigned long idx = offset / SWAPFILE_CLUSTER; 1167*38d8b4e6SHuang Ying struct swap_cluster_info *ci; 1168*38d8b4e6SHuang Ying struct swap_info_struct *si; 1169*38d8b4e6SHuang Ying unsigned char *map; 1170*38d8b4e6SHuang Ying unsigned int i; 1171*38d8b4e6SHuang Ying 1172*38d8b4e6SHuang Ying si = swap_info_get(entry); 1173*38d8b4e6SHuang Ying if (!si) 1174*38d8b4e6SHuang Ying return; 1175*38d8b4e6SHuang Ying 1176*38d8b4e6SHuang Ying ci = lock_cluster(si, offset); 1177*38d8b4e6SHuang Ying map = si->swap_map + offset; 1178*38d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1179*38d8b4e6SHuang Ying VM_BUG_ON(map[i] != SWAP_HAS_CACHE); 1180*38d8b4e6SHuang Ying map[i] = 0; 1181*38d8b4e6SHuang Ying } 1182*38d8b4e6SHuang Ying unlock_cluster(ci); 1183*38d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER); 1184*38d8b4e6SHuang Ying swap_free_cluster(si, idx); 1185*38d8b4e6SHuang Ying spin_unlock(&si->lock); 1186*38d8b4e6SHuang Ying } 1187*38d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 1188*38d8b4e6SHuang Ying 11897c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n) 11907c00bafeSTim Chen { 11917c00bafeSTim Chen struct swap_info_struct *p, *prev; 11927c00bafeSTim Chen int i; 11937c00bafeSTim Chen 11947c00bafeSTim Chen if (n <= 0) 11957c00bafeSTim Chen return; 11967c00bafeSTim Chen 11977c00bafeSTim Chen prev = NULL; 11987c00bafeSTim Chen p = NULL; 11997c00bafeSTim Chen for (i = 0; i < n; ++i) { 12007c00bafeSTim Chen p = swap_info_get_cont(entries[i], prev); 1201235b6217SHuang, Ying if (p) 12027c00bafeSTim Chen swap_entry_free(p, entries[i]); 12037c00bafeSTim Chen prev = p; 12047c00bafeSTim Chen } 12057c00bafeSTim Chen if (p) 12067c00bafeSTim Chen spin_unlock(&p->lock); 1207cb4b86baSKAMEZAWA Hiroyuki } 1208cb4b86baSKAMEZAWA Hiroyuki 1209cb4b86baSKAMEZAWA Hiroyuki /* 1210c475a8abSHugh Dickins * How many references to page are currently swapped out? 1211570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 1212570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 12131da177e4SLinus Torvalds */ 1214bde05d1cSHugh Dickins int page_swapcount(struct page *page) 12151da177e4SLinus Torvalds { 1216c475a8abSHugh Dickins int count = 0; 12171da177e4SLinus Torvalds struct swap_info_struct *p; 1218235b6217SHuang, Ying struct swap_cluster_info *ci; 12191da177e4SLinus Torvalds swp_entry_t entry; 1220235b6217SHuang, Ying unsigned long offset; 12211da177e4SLinus Torvalds 12224c21e2f2SHugh Dickins entry.val = page_private(page); 1223235b6217SHuang, Ying p = _swap_info_get(entry); 12241da177e4SLinus Torvalds if (p) { 1225235b6217SHuang, Ying offset = swp_offset(entry); 1226235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1227235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 1228235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 12291da177e4SLinus Torvalds } 1230c475a8abSHugh Dickins return count; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 1233322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) 1234322b8afeSHuang Ying { 1235322b8afeSHuang Ying int count = 0; 1236322b8afeSHuang Ying pgoff_t offset = swp_offset(entry); 1237322b8afeSHuang Ying struct swap_cluster_info *ci; 1238322b8afeSHuang Ying 1239322b8afeSHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1240322b8afeSHuang Ying count = swap_count(si->swap_map[offset]); 1241322b8afeSHuang Ying unlock_cluster_or_swap_info(si, ci); 1242322b8afeSHuang Ying return count; 1243322b8afeSHuang Ying } 1244322b8afeSHuang Ying 12451da177e4SLinus Torvalds /* 12468334b962SMinchan Kim * How many references to @entry are currently swapped out? 1247e8c26ab6STim Chen * This does not give an exact answer when swap count is continued, 1248e8c26ab6STim Chen * but does include the high COUNT_CONTINUED flag to allow for that. 1249e8c26ab6STim Chen */ 1250e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry) 1251e8c26ab6STim Chen { 1252e8c26ab6STim Chen int count = 0; 1253e8c26ab6STim Chen struct swap_info_struct *si; 1254e8c26ab6STim Chen 1255e8c26ab6STim Chen si = __swap_info_get(entry); 1256322b8afeSHuang Ying if (si) 1257322b8afeSHuang Ying count = swap_swapcount(si, entry); 1258e8c26ab6STim Chen return count; 1259e8c26ab6STim Chen } 1260e8c26ab6STim Chen 1261e8c26ab6STim Chen /* 1262e8c26ab6STim Chen * How many references to @entry are currently swapped out? 12638334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 12648334b962SMinchan Kim */ 12658334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 12668334b962SMinchan Kim { 12678334b962SMinchan Kim int count, tmp_count, n; 12688334b962SMinchan Kim struct swap_info_struct *p; 1269235b6217SHuang, Ying struct swap_cluster_info *ci; 12708334b962SMinchan Kim struct page *page; 12718334b962SMinchan Kim pgoff_t offset; 12728334b962SMinchan Kim unsigned char *map; 12738334b962SMinchan Kim 1274235b6217SHuang, Ying p = _swap_info_get(entry); 12758334b962SMinchan Kim if (!p) 12768334b962SMinchan Kim return 0; 12778334b962SMinchan Kim 1278235b6217SHuang, Ying offset = swp_offset(entry); 1279235b6217SHuang, Ying 1280235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1281235b6217SHuang, Ying 1282235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 12838334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 12848334b962SMinchan Kim goto out; 12858334b962SMinchan Kim 12868334b962SMinchan Kim count &= ~COUNT_CONTINUED; 12878334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 12888334b962SMinchan Kim 12898334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 12908334b962SMinchan Kim offset &= ~PAGE_MASK; 12918334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 12928334b962SMinchan Kim 12938334b962SMinchan Kim do { 1294a8ae4991SGeliang Tang page = list_next_entry(page, lru); 12958334b962SMinchan Kim map = kmap_atomic(page); 12968334b962SMinchan Kim tmp_count = map[offset]; 12978334b962SMinchan Kim kunmap_atomic(map); 12988334b962SMinchan Kim 12998334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 13008334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 13018334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 13028334b962SMinchan Kim out: 1303235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 13048334b962SMinchan Kim return count; 13058334b962SMinchan Kim } 13068334b962SMinchan Kim 13078334b962SMinchan Kim /* 13087b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 13097b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 13107b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 13117b1fe597SHugh Dickins * later would only waste time away from clustering. 13126d0a07edSAndrea Arcangeli * 13136d0a07edSAndrea Arcangeli * NOTE: total_mapcount should not be relied upon by the caller if 13146d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 13156d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 13161da177e4SLinus Torvalds */ 13176d0a07edSAndrea Arcangeli bool reuse_swap_page(struct page *page, int *total_mapcount) 13181da177e4SLinus Torvalds { 1319c475a8abSHugh Dickins int count; 13201da177e4SLinus Torvalds 1321309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 13225ad64688SHugh Dickins if (unlikely(PageKsm(page))) 13236d0a07edSAndrea Arcangeli return false; 13246d0a07edSAndrea Arcangeli count = page_trans_huge_mapcount(page, total_mapcount); 13257b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 1326c475a8abSHugh Dickins count += page_swapcount(page); 1327f0571429SMinchan Kim if (count != 1) 1328f0571429SMinchan Kim goto out; 1329f0571429SMinchan Kim if (!PageWriteback(page)) { 13307b1fe597SHugh Dickins delete_from_swap_cache(page); 13317b1fe597SHugh Dickins SetPageDirty(page); 1332f0571429SMinchan Kim } else { 1333f0571429SMinchan Kim swp_entry_t entry; 1334f0571429SMinchan Kim struct swap_info_struct *p; 1335f0571429SMinchan Kim 1336f0571429SMinchan Kim entry.val = page_private(page); 1337f0571429SMinchan Kim p = swap_info_get(entry); 1338f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1339f0571429SMinchan Kim spin_unlock(&p->lock); 1340f0571429SMinchan Kim return false; 1341f0571429SMinchan Kim } 1342f0571429SMinchan Kim spin_unlock(&p->lock); 13437b1fe597SHugh Dickins } 13447b1fe597SHugh Dickins } 1345f0571429SMinchan Kim out: 13465ad64688SHugh Dickins return count <= 1; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds /* 1350a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1351a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 13521da177e4SLinus Torvalds */ 1353a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 13541da177e4SLinus Torvalds { 1355309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds if (!PageSwapCache(page)) 13581da177e4SLinus Torvalds return 0; 13591da177e4SLinus Torvalds if (PageWriteback(page)) 13601da177e4SLinus Torvalds return 0; 1361a2c43eedSHugh Dickins if (page_swapcount(page)) 13621da177e4SLinus Torvalds return 0; 13631da177e4SLinus Torvalds 1364b73d7fceSHugh Dickins /* 1365b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1366b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1367b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1368b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1369b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1370b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1371b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1372b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1373b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1374b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1375b73d7fceSHugh Dickins * 13762de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1377f90ac398SMel Gorman * to disk so check that here. 1378b73d7fceSHugh Dickins */ 1379f90ac398SMel Gorman if (pm_suspended_storage()) 1380b73d7fceSHugh Dickins return 0; 1381b73d7fceSHugh Dickins 1382a2c43eedSHugh Dickins delete_from_swap_cache(page); 13831da177e4SLinus Torvalds SetPageDirty(page); 1384a2c43eedSHugh Dickins return 1; 138568a22394SRik van Riel } 138668a22394SRik van Riel 138768a22394SRik van Riel /* 13881da177e4SLinus Torvalds * Free the swap entry like above, but also try to 13891da177e4SLinus Torvalds * free the page cache entry if it is the last user. 13901da177e4SLinus Torvalds */ 13912509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 13921da177e4SLinus Torvalds { 13931da177e4SLinus Torvalds struct swap_info_struct *p; 13941da177e4SLinus Torvalds struct page *page = NULL; 13957c00bafeSTim Chen unsigned char count; 13961da177e4SLinus Torvalds 1397a7420aa5SAndi Kleen if (non_swap_entry(entry)) 13982509ef26SHugh Dickins return 1; 13990697212aSChristoph Lameter 14007c00bafeSTim Chen p = _swap_info_get(entry); 14011da177e4SLinus Torvalds if (p) { 14027c00bafeSTim Chen count = __swap_entry_free(p, entry, 1); 14037c00bafeSTim Chen if (count == SWAP_HAS_CACHE) { 140433806f06SShaohua Li page = find_get_page(swap_address_space(entry), 1405f6ab1f7fSHuang Ying swp_offset(entry)); 14068413ac9dSNick Piggin if (page && !trylock_page(page)) { 140709cbfeafSKirill A. Shutemov put_page(page); 140893fac704SNick Piggin page = NULL; 140993fac704SNick Piggin } 14107c00bafeSTim Chen } else if (!count) 141167afa38eSTim Chen free_swap_slot(entry); 14121da177e4SLinus Torvalds } 14131da177e4SLinus Torvalds if (page) { 1414a2c43eedSHugh Dickins /* 1415a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 1416a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 1417a2c43eedSHugh Dickins */ 141893fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 1419322b8afeSHuang Ying (!page_mapped(page) || mem_cgroup_swap_full(page)) && 1420322b8afeSHuang Ying !swap_swapcount(p, entry)) { 14211da177e4SLinus Torvalds delete_from_swap_cache(page); 14221da177e4SLinus Torvalds SetPageDirty(page); 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds unlock_page(page); 142509cbfeafSKirill A. Shutemov put_page(page); 14261da177e4SLinus Torvalds } 14272509ef26SHugh Dickins return p != NULL; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 1430b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1431f577eb30SRafael J. Wysocki /* 1432915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1433f577eb30SRafael J. Wysocki * 1434915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1435915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1436915bae9eSRafael J. Wysocki * 1437915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1438f577eb30SRafael J. Wysocki */ 14397bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 1440f577eb30SRafael J. Wysocki { 1441915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 1442efa90a98SHugh Dickins int type; 1443f577eb30SRafael J. Wysocki 1444915bae9eSRafael J. Wysocki if (device) 1445915bae9eSRafael J. Wysocki bdev = bdget(device); 1446915bae9eSRafael J. Wysocki 1447f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1448efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1449efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1450f577eb30SRafael J. Wysocki 1451915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1452f577eb30SRafael J. Wysocki continue; 1453b6b5bce3SRafael J. Wysocki 1454915bae9eSRafael J. Wysocki if (!bdev) { 14557bf23687SRafael J. Wysocki if (bdev_p) 1456dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 14577bf23687SRafael J. Wysocki 14586e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1459efa90a98SHugh Dickins return type; 14606e1819d6SRafael J. Wysocki } 1461915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 14629625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 1463915bae9eSRafael J. Wysocki 1464915bae9eSRafael J. Wysocki if (se->start_block == offset) { 14657bf23687SRafael J. Wysocki if (bdev_p) 1466dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 14677bf23687SRafael J. Wysocki 1468f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1469915bae9eSRafael J. Wysocki bdput(bdev); 1470efa90a98SHugh Dickins return type; 1471f577eb30SRafael J. Wysocki } 1472f577eb30SRafael J. Wysocki } 1473915bae9eSRafael J. Wysocki } 1474f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1475915bae9eSRafael J. Wysocki if (bdev) 1476915bae9eSRafael J. Wysocki bdput(bdev); 1477915bae9eSRafael J. Wysocki 1478f577eb30SRafael J. Wysocki return -ENODEV; 1479f577eb30SRafael J. Wysocki } 1480f577eb30SRafael J. Wysocki 1481f577eb30SRafael J. Wysocki /* 148273c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 148373c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 148473c34b6aSHugh Dickins */ 148573c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 148673c34b6aSHugh Dickins { 148773c34b6aSHugh Dickins struct block_device *bdev; 148873c34b6aSHugh Dickins 148973c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 149073c34b6aSHugh Dickins return 0; 149173c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 149273c34b6aSHugh Dickins return 0; 1493d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 149473c34b6aSHugh Dickins } 149573c34b6aSHugh Dickins 149673c34b6aSHugh Dickins /* 1497f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1498f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1499f577eb30SRafael J. Wysocki * 1500f577eb30SRafael J. Wysocki * This is needed for software suspend 1501f577eb30SRafael J. Wysocki */ 1502f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1503f577eb30SRafael J. Wysocki { 1504f577eb30SRafael J. Wysocki unsigned int n = 0; 1505f577eb30SRafael J. Wysocki 1506f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1507efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1508efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1509efa90a98SHugh Dickins 1510ec8acf20SShaohua Li spin_lock(&sis->lock); 1511efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1512efa90a98SHugh Dickins n = sis->pages; 1513f577eb30SRafael J. Wysocki if (free) 1514efa90a98SHugh Dickins n -= sis->inuse_pages; 1515efa90a98SHugh Dickins } 1516ec8acf20SShaohua Li spin_unlock(&sis->lock); 1517f577eb30SRafael J. Wysocki } 1518f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1519f577eb30SRafael J. Wysocki return n; 1520f577eb30SRafael J. Wysocki } 152173c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1522f577eb30SRafael J. Wysocki 15239f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1524179ef71cSCyrill Gorcunov { 15259f8bdb3fSHugh Dickins return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte); 1526179ef71cSCyrill Gorcunov } 1527179ef71cSCyrill Gorcunov 15281da177e4SLinus Torvalds /* 152972866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 153072866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 153172866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 15321da177e4SLinus Torvalds */ 1533044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 15341da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 15351da177e4SLinus Torvalds { 15369e16b7fbSHugh Dickins struct page *swapcache; 153772835c86SJohannes Weiner struct mem_cgroup *memcg; 1538044d66c1SHugh Dickins spinlock_t *ptl; 1539044d66c1SHugh Dickins pte_t *pte; 1540044d66c1SHugh Dickins int ret = 1; 1541044d66c1SHugh Dickins 15429e16b7fbSHugh Dickins swapcache = page; 15439e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 15449e16b7fbSHugh Dickins if (unlikely(!page)) 15459e16b7fbSHugh Dickins return -ENOMEM; 15469e16b7fbSHugh Dickins 1547f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 1548f627c2f5SKirill A. Shutemov &memcg, false)) { 1549044d66c1SHugh Dickins ret = -ENOMEM; 155085d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 155185d9fc89SKAMEZAWA Hiroyuki } 1552044d66c1SHugh Dickins 1553044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 15549f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1555f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1556044d66c1SHugh Dickins ret = 0; 1557044d66c1SHugh Dickins goto out; 1558044d66c1SHugh Dickins } 15598a9f3ccdSBalbir Singh 1560b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1561d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 15621da177e4SLinus Torvalds get_page(page); 15631da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 15641da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 156500501b53SJohannes Weiner if (page == swapcache) { 1566d281ee61SKirill A. Shutemov page_add_anon_rmap(page, vma, addr, false); 1567f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 156800501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1569d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, addr, false); 1570f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 157100501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 157200501b53SJohannes Weiner } 15731da177e4SLinus Torvalds swap_free(entry); 15741da177e4SLinus Torvalds /* 15751da177e4SLinus Torvalds * Move the page to the active list so it is not 15761da177e4SLinus Torvalds * immediately swapped out again after swapon. 15771da177e4SLinus Torvalds */ 15781da177e4SLinus Torvalds activate_page(page); 1579044d66c1SHugh Dickins out: 1580044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 158185d9fc89SKAMEZAWA Hiroyuki out_nolock: 15829e16b7fbSHugh Dickins if (page != swapcache) { 15839e16b7fbSHugh Dickins unlock_page(page); 15849e16b7fbSHugh Dickins put_page(page); 15859e16b7fbSHugh Dickins } 1586044d66c1SHugh Dickins return ret; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 15901da177e4SLinus Torvalds unsigned long addr, unsigned long end, 15911da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 15921da177e4SLinus Torvalds { 15931da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1594705e87c0SHugh Dickins pte_t *pte; 15958a9f3ccdSBalbir Singh int ret = 0; 15961da177e4SLinus Torvalds 1597044d66c1SHugh Dickins /* 1598044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1599044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1600044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1601044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1602044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1603044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 16042de1a7e4SSeth Jennings * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1605044d66c1SHugh Dickins */ 1606044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 16071da177e4SLinus Torvalds do { 16081da177e4SLinus Torvalds /* 16091da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 16101da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 16111da177e4SLinus Torvalds */ 16129f8bdb3fSHugh Dickins if (unlikely(pte_same_as_swp(*pte, swp_pte))) { 1613044d66c1SHugh Dickins pte_unmap(pte); 1614044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1615044d66c1SHugh Dickins if (ret) 1616044d66c1SHugh Dickins goto out; 1617044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1620044d66c1SHugh Dickins pte_unmap(pte - 1); 1621044d66c1SHugh Dickins out: 16228a9f3ccdSBalbir Singh return ret; 16231da177e4SLinus Torvalds } 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 16261da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16271da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16281da177e4SLinus Torvalds { 16291da177e4SLinus Torvalds pmd_t *pmd; 16301da177e4SLinus Torvalds unsigned long next; 16318a9f3ccdSBalbir Singh int ret; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 16341da177e4SLinus Torvalds do { 1635dc644a07SHugh Dickins cond_resched(); 16361da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 16371a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 16381da177e4SLinus Torvalds continue; 16398a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 16408a9f3ccdSBalbir Singh if (ret) 16418a9f3ccdSBalbir Singh return ret; 16421da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 16431da177e4SLinus Torvalds return 0; 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds 1646c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d, 16471da177e4SLinus Torvalds unsigned long addr, unsigned long end, 16481da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds pud_t *pud; 16511da177e4SLinus Torvalds unsigned long next; 16528a9f3ccdSBalbir Singh int ret; 16531da177e4SLinus Torvalds 1654c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 16551da177e4SLinus Torvalds do { 16561da177e4SLinus Torvalds next = pud_addr_end(addr, end); 16571da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 16581da177e4SLinus Torvalds continue; 16598a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 16608a9f3ccdSBalbir Singh if (ret) 16618a9f3ccdSBalbir Singh return ret; 16621da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 16631da177e4SLinus Torvalds return 0; 16641da177e4SLinus Torvalds } 16651da177e4SLinus Torvalds 1666c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd, 1667c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1668c2febafcSKirill A. Shutemov swp_entry_t entry, struct page *page) 1669c2febafcSKirill A. Shutemov { 1670c2febafcSKirill A. Shutemov p4d_t *p4d; 1671c2febafcSKirill A. Shutemov unsigned long next; 1672c2febafcSKirill A. Shutemov int ret; 1673c2febafcSKirill A. Shutemov 1674c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1675c2febafcSKirill A. Shutemov do { 1676c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1677c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1678c2febafcSKirill A. Shutemov continue; 1679c2febafcSKirill A. Shutemov ret = unuse_pud_range(vma, p4d, addr, next, entry, page); 1680c2febafcSKirill A. Shutemov if (ret) 1681c2febafcSKirill A. Shutemov return ret; 1682c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1683c2febafcSKirill A. Shutemov return 0; 1684c2febafcSKirill A. Shutemov } 1685c2febafcSKirill A. Shutemov 16861da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 16871da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 16881da177e4SLinus Torvalds { 16891da177e4SLinus Torvalds pgd_t *pgd; 16901da177e4SLinus Torvalds unsigned long addr, end, next; 16918a9f3ccdSBalbir Singh int ret; 16921da177e4SLinus Torvalds 16933ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 16941da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 16951da177e4SLinus Torvalds if (addr == -EFAULT) 16961da177e4SLinus Torvalds return 0; 16971da177e4SLinus Torvalds else 16981da177e4SLinus Torvalds end = addr + PAGE_SIZE; 16991da177e4SLinus Torvalds } else { 17001da177e4SLinus Torvalds addr = vma->vm_start; 17011da177e4SLinus Torvalds end = vma->vm_end; 17021da177e4SLinus Torvalds } 17031da177e4SLinus Torvalds 17041da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 17051da177e4SLinus Torvalds do { 17061da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 17071da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 17081da177e4SLinus Torvalds continue; 1709c2febafcSKirill A. Shutemov ret = unuse_p4d_range(vma, pgd, addr, next, entry, page); 17108a9f3ccdSBalbir Singh if (ret) 17118a9f3ccdSBalbir Singh return ret; 17121da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 17131da177e4SLinus Torvalds return 0; 17141da177e4SLinus Torvalds } 17151da177e4SLinus Torvalds 17161da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 17171da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 17181da177e4SLinus Torvalds { 17191da177e4SLinus Torvalds struct vm_area_struct *vma; 17208a9f3ccdSBalbir Singh int ret = 0; 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 17231da177e4SLinus Torvalds /* 17247d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 17257d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 17261da177e4SLinus Torvalds */ 1727c475a8abSHugh Dickins activate_page(page); 17281da177e4SLinus Torvalds unlock_page(page); 17291da177e4SLinus Torvalds down_read(&mm->mmap_sem); 17301da177e4SLinus Torvalds lock_page(page); 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 17338a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 17341da177e4SLinus Torvalds break; 1735dc644a07SHugh Dickins cond_resched(); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds up_read(&mm->mmap_sem); 17388a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds /* 174238b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 174338b5faf4SDan Magenheimer * from current position to next entry still in use. 17441da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 17451da177e4SLinus Torvalds */ 17466eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 174738b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 17481da177e4SLinus Torvalds { 17496eb396dcSHugh Dickins unsigned int max = si->max; 17506eb396dcSHugh Dickins unsigned int i = prev; 17518d69aaeeSHugh Dickins unsigned char count; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds /* 17545d337b91SHugh Dickins * No need for swap_lock here: we're just looking 17551da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 17561da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 17575d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds for (;;) { 17601da177e4SLinus Torvalds if (++i >= max) { 17611da177e4SLinus Torvalds if (!prev) { 17621da177e4SLinus Torvalds i = 0; 17631da177e4SLinus Torvalds break; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds /* 17661da177e4SLinus Torvalds * No entries in use at top of swap_map, 17671da177e4SLinus Torvalds * loop back to start and recheck there. 17681da177e4SLinus Torvalds */ 17691da177e4SLinus Torvalds max = prev + 1; 17701da177e4SLinus Torvalds prev = 0; 17711da177e4SLinus Torvalds i = 1; 17721da177e4SLinus Torvalds } 17734db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 1774355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 1775dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 17761da177e4SLinus Torvalds break; 1777dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 1778dc644a07SHugh Dickins cond_resched(); 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds return i; 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* 17841da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 17851da177e4SLinus Torvalds * and then search for the process using it. All the necessary 17861da177e4SLinus Torvalds * page table adjustments can then be made atomically. 178738b5faf4SDan Magenheimer * 178838b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 178938b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 17901da177e4SLinus Torvalds */ 179138b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 179238b5faf4SDan Magenheimer unsigned long pages_to_unuse) 17931da177e4SLinus Torvalds { 1794efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 17951da177e4SLinus Torvalds struct mm_struct *start_mm; 1796edfe23daSShaohua Li volatile unsigned char *swap_map; /* swap_map is accessed without 1797edfe23daSShaohua Li * locking. Mark it as volatile 1798edfe23daSShaohua Li * to prevent compiler doing 1799edfe23daSShaohua Li * something odd. 1800edfe23daSShaohua Li */ 18018d69aaeeSHugh Dickins unsigned char swcount; 18021da177e4SLinus Torvalds struct page *page; 18031da177e4SLinus Torvalds swp_entry_t entry; 18046eb396dcSHugh Dickins unsigned int i = 0; 18051da177e4SLinus Torvalds int retval = 0; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 18091da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 18101da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 18111da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 18121da177e4SLinus Torvalds * 18131da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 18141da177e4SLinus Torvalds * freed the previous entry from; but that would take less 18151da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 18161da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 18171da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 18181da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 1819570a335bSHugh Dickins * that. 18201da177e4SLinus Torvalds */ 18211da177e4SLinus Torvalds start_mm = &init_mm; 18223fce371bSVegard Nossum mmget(&init_mm); 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds /* 18251da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 18261da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 18271da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 18281da177e4SLinus Torvalds */ 182938b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 18301da177e4SLinus Torvalds if (signal_pending(current)) { 18311da177e4SLinus Torvalds retval = -EINTR; 18321da177e4SLinus Torvalds break; 18331da177e4SLinus Torvalds } 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds /* 18361da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 18371da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 18381da177e4SLinus Torvalds * page and read the swap into it. 18391da177e4SLinus Torvalds */ 18401da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 18411da177e4SLinus Torvalds entry = swp_entry(type, i); 184202098feaSHugh Dickins page = read_swap_cache_async(entry, 184302098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 18441da177e4SLinus Torvalds if (!page) { 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 18471da177e4SLinus Torvalds * has been freed independently, and will not be 18481da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 18491da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 18501da177e4SLinus Torvalds */ 1851edfe23daSShaohua Li swcount = *swap_map; 1852edfe23daSShaohua Li /* 1853edfe23daSShaohua Li * We don't hold lock here, so the swap entry could be 1854edfe23daSShaohua Li * SWAP_MAP_BAD (when the cluster is discarding). 1855edfe23daSShaohua Li * Instead of fail out, We can just skip the swap 1856edfe23daSShaohua Li * entry because swapoff will wait for discarding 1857edfe23daSShaohua Li * finish anyway. 1858edfe23daSShaohua Li */ 1859edfe23daSShaohua Li if (!swcount || swcount == SWAP_MAP_BAD) 18601da177e4SLinus Torvalds continue; 18611da177e4SLinus Torvalds retval = -ENOMEM; 18621da177e4SLinus Torvalds break; 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds /* 18661da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 18671da177e4SLinus Torvalds */ 18681da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 18691da177e4SLinus Torvalds mmput(start_mm); 18701da177e4SLinus Torvalds start_mm = &init_mm; 18713fce371bSVegard Nossum mmget(&init_mm); 18721da177e4SLinus Torvalds } 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds /* 18751da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 18761da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 18771da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 18781da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 18791da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 18801da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 18811da177e4SLinus Torvalds */ 18821da177e4SLinus Torvalds wait_on_page_locked(page); 18831da177e4SLinus Torvalds wait_on_page_writeback(page); 18841da177e4SLinus Torvalds lock_page(page); 18851da177e4SLinus Torvalds wait_on_page_writeback(page); 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds /* 18881da177e4SLinus Torvalds * Remove all references to entry. 18891da177e4SLinus Torvalds */ 18901da177e4SLinus Torvalds swcount = *swap_map; 1891aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 1892aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 1893aaa46865SHugh Dickins /* page has already been unlocked and released */ 1894aaa46865SHugh Dickins if (retval < 0) 1895aaa46865SHugh Dickins break; 1896aaa46865SHugh Dickins continue; 18971da177e4SLinus Torvalds } 1898aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 1899aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 1900aaa46865SHugh Dickins 1901355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 19021da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 19031da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 19041da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 19051da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 19061da177e4SLinus Torvalds struct mm_struct *mm; 19071da177e4SLinus Torvalds 19083fce371bSVegard Nossum mmget(new_start_mm); 19093fce371bSVegard Nossum mmget(prev_mm); 19101da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1911aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 19121da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 19131da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 1914388f7934SVegard Nossum if (!mmget_not_zero(mm)) 19151da177e4SLinus Torvalds continue; 19161da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 19171da177e4SLinus Torvalds mmput(prev_mm); 19181da177e4SLinus Torvalds prev_mm = mm; 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds cond_resched(); 19211da177e4SLinus Torvalds 19221da177e4SLinus Torvalds swcount = *swap_map; 1923355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 19241da177e4SLinus Torvalds ; 1925aaa46865SHugh Dickins else if (mm == &init_mm) 19261da177e4SLinus Torvalds set_start_mm = 1; 1927aaa46865SHugh Dickins else 19281da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1929355cfa73SKAMEZAWA Hiroyuki 193032c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 19311da177e4SLinus Torvalds mmput(new_start_mm); 19323fce371bSVegard Nossum mmget(mm); 19331da177e4SLinus Torvalds new_start_mm = mm; 19341da177e4SLinus Torvalds set_start_mm = 0; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds spin_lock(&mmlist_lock); 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 19391da177e4SLinus Torvalds mmput(prev_mm); 19401da177e4SLinus Torvalds mmput(start_mm); 19411da177e4SLinus Torvalds start_mm = new_start_mm; 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds if (retval) { 19441da177e4SLinus Torvalds unlock_page(page); 194509cbfeafSKirill A. Shutemov put_page(page); 19461da177e4SLinus Torvalds break; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds /* 19501da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 19511da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 19521da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 19531da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 19541da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 19551da177e4SLinus Torvalds * delete from cache even if there's another reference, 19561da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 19571da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 19581da177e4SLinus Torvalds * read from disk into another page. Splitting into two 19591da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 19601da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 19615ad64688SHugh Dickins * 19625ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 19635ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 19645ad64688SHugh Dickins * this splitting happens to be just what is needed to 19655ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 19665ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 19671da177e4SLinus Torvalds */ 1968355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 1969355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 19701da177e4SLinus Torvalds struct writeback_control wbc = { 19711da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 19721da177e4SLinus Torvalds }; 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds swap_writepage(page, &wbc); 19751da177e4SLinus Torvalds lock_page(page); 19761da177e4SLinus Torvalds wait_on_page_writeback(page); 19771da177e4SLinus Torvalds } 197868bdc8d6SHugh Dickins 197968bdc8d6SHugh Dickins /* 198068bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 198168bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 198268bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 198368bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 198468bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 198568bdc8d6SHugh Dickins */ 198668bdc8d6SHugh Dickins if (PageSwapCache(page) && 198768bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 19881da177e4SLinus Torvalds delete_from_swap_cache(page); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /* 19911da177e4SLinus Torvalds * So we could skip searching mms once swap count went 19921da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 19932706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 19941da177e4SLinus Torvalds */ 19951da177e4SLinus Torvalds SetPageDirty(page); 19961da177e4SLinus Torvalds unlock_page(page); 199709cbfeafSKirill A. Shutemov put_page(page); 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds /* 20001da177e4SLinus Torvalds * Make sure that we aren't completely killing 20011da177e4SLinus Torvalds * interactive performance. 20021da177e4SLinus Torvalds */ 20031da177e4SLinus Torvalds cond_resched(); 200438b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 200538b5faf4SDan Magenheimer if (!--pages_to_unuse) 200638b5faf4SDan Magenheimer break; 200738b5faf4SDan Magenheimer } 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds mmput(start_mm); 20111da177e4SLinus Torvalds return retval; 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds /* 20155d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 20165d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 20175d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 20181da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 20191da177e4SLinus Torvalds */ 20201da177e4SLinus Torvalds static void drain_mmlist(void) 20211da177e4SLinus Torvalds { 20221da177e4SLinus Torvalds struct list_head *p, *next; 2023efa90a98SHugh Dickins unsigned int type; 20241da177e4SLinus Torvalds 2025efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 2026efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 20271da177e4SLinus Torvalds return; 20281da177e4SLinus Torvalds spin_lock(&mmlist_lock); 20291da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 20301da177e4SLinus Torvalds list_del_init(p); 20311da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds /* 20351da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 2036d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 2037d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 2038d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 20391da177e4SLinus Torvalds */ 2040d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 20411da177e4SLinus Torvalds { 2042f29ad6a9SHugh Dickins struct swap_info_struct *sis; 2043f29ad6a9SHugh Dickins struct swap_extent *start_se; 2044f29ad6a9SHugh Dickins struct swap_extent *se; 2045f29ad6a9SHugh Dickins pgoff_t offset; 2046f29ad6a9SHugh Dickins 2047efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 2048f29ad6a9SHugh Dickins *bdev = sis->bdev; 2049f29ad6a9SHugh Dickins 2050f29ad6a9SHugh Dickins offset = swp_offset(entry); 2051f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 2052f29ad6a9SHugh Dickins se = start_se; 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds for ( ; ; ) { 20551da177e4SLinus Torvalds if (se->start_page <= offset && 20561da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 20571da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 20581da177e4SLinus Torvalds } 2059a8ae4991SGeliang Tang se = list_next_entry(se, list); 20601da177e4SLinus Torvalds sis->curr_swap_extent = se; 20611da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds /* 2066d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 2067d4906e1aSLee Schermerhorn */ 2068d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 2069d4906e1aSLee Schermerhorn { 2070d4906e1aSLee Schermerhorn swp_entry_t entry; 2071d4906e1aSLee Schermerhorn entry.val = page_private(page); 2072d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 2073d4906e1aSLee Schermerhorn } 2074d4906e1aSLee Schermerhorn 2075d4906e1aSLee Schermerhorn /* 20761da177e4SLinus Torvalds * Free all of a swapdev's extent information 20771da177e4SLinus Torvalds */ 20781da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 20791da177e4SLinus Torvalds { 20809625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 20811da177e4SLinus Torvalds struct swap_extent *se; 20821da177e4SLinus Torvalds 2083a8ae4991SGeliang Tang se = list_first_entry(&sis->first_swap_extent.list, 20841da177e4SLinus Torvalds struct swap_extent, list); 20851da177e4SLinus Torvalds list_del(&se->list); 20861da177e4SLinus Torvalds kfree(se); 20871da177e4SLinus Torvalds } 208862c230bcSMel Gorman 208962c230bcSMel Gorman if (sis->flags & SWP_FILE) { 209062c230bcSMel Gorman struct file *swap_file = sis->swap_file; 209162c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 209262c230bcSMel Gorman 209362c230bcSMel Gorman sis->flags &= ~SWP_FILE; 209462c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 209562c230bcSMel Gorman } 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* 20991da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 210011d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 21011da177e4SLinus Torvalds * 210211d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 21031da177e4SLinus Torvalds */ 2104a509bc1aSMel Gorman int 21051da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 21061da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 21071da177e4SLinus Torvalds { 21081da177e4SLinus Torvalds struct swap_extent *se; 21091da177e4SLinus Torvalds struct swap_extent *new_se; 21101da177e4SLinus Torvalds struct list_head *lh; 21111da177e4SLinus Torvalds 21129625a5f2SHugh Dickins if (start_page == 0) { 21139625a5f2SHugh Dickins se = &sis->first_swap_extent; 21149625a5f2SHugh Dickins sis->curr_swap_extent = se; 21159625a5f2SHugh Dickins se->start_page = 0; 21169625a5f2SHugh Dickins se->nr_pages = nr_pages; 21179625a5f2SHugh Dickins se->start_block = start_block; 21189625a5f2SHugh Dickins return 1; 21199625a5f2SHugh Dickins } else { 21209625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 21211da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 212211d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 212311d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 21241da177e4SLinus Torvalds /* Merge it */ 21251da177e4SLinus Torvalds se->nr_pages += nr_pages; 21261da177e4SLinus Torvalds return 0; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds /* 21311da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 21321da177e4SLinus Torvalds */ 21331da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 21341da177e4SLinus Torvalds if (new_se == NULL) 21351da177e4SLinus Torvalds return -ENOMEM; 21361da177e4SLinus Torvalds new_se->start_page = start_page; 21371da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 21381da177e4SLinus Torvalds new_se->start_block = start_block; 21391da177e4SLinus Torvalds 21409625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 214153092a74SHugh Dickins return 1; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds /* 21451da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 21461da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 21471da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 21481da177e4SLinus Torvalds * time for locating where on disk a page belongs. 21491da177e4SLinus Torvalds * 21501da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 21511da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 21521da177e4SLinus Torvalds * swap files identically. 21531da177e4SLinus Torvalds * 21541da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 21551da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 21561da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 21571da177e4SLinus Torvalds * 21581da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 21591da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 21601da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 21611da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 21621da177e4SLinus Torvalds * for swapping. 21631da177e4SLinus Torvalds * 2164b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 21651da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 21661da177e4SLinus Torvalds * which will scribble on the fs. 21671da177e4SLinus Torvalds * 21681da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 21691da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 21701da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 21711da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 21721da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 21731da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 21741da177e4SLinus Torvalds */ 217553092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 21761da177e4SLinus Torvalds { 217762c230bcSMel Gorman struct file *swap_file = sis->swap_file; 217862c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 217962c230bcSMel Gorman struct inode *inode = mapping->host; 21801da177e4SLinus Torvalds int ret; 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 21831da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 218453092a74SHugh Dickins *span = sis->pages; 2185a509bc1aSMel Gorman return ret; 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 218862c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 2189a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 219062c230bcSMel Gorman if (!ret) { 219162c230bcSMel Gorman sis->flags |= SWP_FILE; 219262c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 219362c230bcSMel Gorman *span = sis->pages; 219462c230bcSMel Gorman } 21959625a5f2SHugh Dickins return ret; 2196a509bc1aSMel Gorman } 2197a509bc1aSMel Gorman 2198a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 21991da177e4SLinus Torvalds } 22001da177e4SLinus Torvalds 2201cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 22022a8f9449SShaohua Li unsigned char *swap_map, 22032a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 220440531542SCesar Eduardo Barros { 220540531542SCesar Eduardo Barros if (prio >= 0) 220640531542SCesar Eduardo Barros p->prio = prio; 220740531542SCesar Eduardo Barros else 220840531542SCesar Eduardo Barros p->prio = --least_priority; 220918ab4d4cSDan Streetman /* 221018ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 221118ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 221218ab4d4cSDan Streetman */ 221318ab4d4cSDan Streetman p->list.prio = -p->prio; 221418ab4d4cSDan Streetman p->avail_list.prio = -p->prio; 221540531542SCesar Eduardo Barros p->swap_map = swap_map; 22162a8f9449SShaohua Li p->cluster_info = cluster_info; 221740531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 2218ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 221940531542SCesar Eduardo Barros total_swap_pages += p->pages; 222040531542SCesar Eduardo Barros 2221adfab836SDan Streetman assert_spin_locked(&swap_lock); 2222adfab836SDan Streetman /* 222318ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 222418ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 222518ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 222618ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 222718ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 222818ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 222918ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 223018ab4d4cSDan Streetman * swap_info_struct. 2231adfab836SDan Streetman */ 223218ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 223318ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 223418ab4d4cSDan Streetman plist_add(&p->avail_list, &swap_avail_head); 223518ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 2236cf0cac0aSCesar Eduardo Barros } 2237cf0cac0aSCesar Eduardo Barros 2238cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 2239cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 22402a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2241cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 2242cf0cac0aSCesar Eduardo Barros { 22434f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 2244cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2245ec8acf20SShaohua Li spin_lock(&p->lock); 22462a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 2247ec8acf20SShaohua Li spin_unlock(&p->lock); 2248cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 2249cf0cac0aSCesar Eduardo Barros } 2250cf0cac0aSCesar Eduardo Barros 2251cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 2252cf0cac0aSCesar Eduardo Barros { 2253cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2254ec8acf20SShaohua Li spin_lock(&p->lock); 22552a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 2256ec8acf20SShaohua Li spin_unlock(&p->lock); 225740531542SCesar Eduardo Barros spin_unlock(&swap_lock); 225840531542SCesar Eduardo Barros } 225940531542SCesar Eduardo Barros 226067afa38eSTim Chen bool has_usable_swap(void) 226167afa38eSTim Chen { 226267afa38eSTim Chen bool ret = true; 226367afa38eSTim Chen 226467afa38eSTim Chen spin_lock(&swap_lock); 226567afa38eSTim Chen if (plist_head_empty(&swap_active_head)) 226667afa38eSTim Chen ret = false; 226767afa38eSTim Chen spin_unlock(&swap_lock); 226867afa38eSTim Chen return ret; 226967afa38eSTim Chen } 227067afa38eSTim Chen 2271c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 22748d69aaeeSHugh Dickins unsigned char *swap_map; 22752a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 22764f89849dSMinchan Kim unsigned long *frontswap_map; 22771da177e4SLinus Torvalds struct file *swap_file, *victim; 22781da177e4SLinus Torvalds struct address_space *mapping; 22791da177e4SLinus Torvalds struct inode *inode; 228091a27b2aSJeff Layton struct filename *pathname; 2281adfab836SDan Streetman int err, found = 0; 22825b808a23SKrzysztof Kozlowski unsigned int old_block_size; 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 22851da177e4SLinus Torvalds return -EPERM; 22861da177e4SLinus Torvalds 2287191c5424SAl Viro BUG_ON(!current->mm); 2288191c5424SAl Viro 22891da177e4SLinus Torvalds pathname = getname(specialfile); 22901da177e4SLinus Torvalds if (IS_ERR(pathname)) 2291f58b59c1SXiaotian Feng return PTR_ERR(pathname); 22921da177e4SLinus Torvalds 2293669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 22941da177e4SLinus Torvalds err = PTR_ERR(victim); 22951da177e4SLinus Torvalds if (IS_ERR(victim)) 22961da177e4SLinus Torvalds goto out; 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds mapping = victim->f_mapping; 22995d337b91SHugh Dickins spin_lock(&swap_lock); 230018ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 230122c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 2302adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 2303adfab836SDan Streetman found = 1; 23041da177e4SLinus Torvalds break; 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds } 2307adfab836SDan Streetman } 2308adfab836SDan Streetman if (!found) { 23091da177e4SLinus Torvalds err = -EINVAL; 23105d337b91SHugh Dickins spin_unlock(&swap_lock); 23111da177e4SLinus Torvalds goto out_dput; 23121da177e4SLinus Torvalds } 2313191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 23141da177e4SLinus Torvalds vm_unacct_memory(p->pages); 23151da177e4SLinus Torvalds else { 23161da177e4SLinus Torvalds err = -ENOMEM; 23175d337b91SHugh Dickins spin_unlock(&swap_lock); 23181da177e4SLinus Torvalds goto out_dput; 23191da177e4SLinus Torvalds } 232018ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 232118ab4d4cSDan Streetman plist_del(&p->avail_list, &swap_avail_head); 232218ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 2323ec8acf20SShaohua Li spin_lock(&p->lock); 232478ecba08SHugh Dickins if (p->prio < 0) { 2325adfab836SDan Streetman struct swap_info_struct *si = p; 2326adfab836SDan Streetman 232718ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2328adfab836SDan Streetman si->prio++; 232918ab4d4cSDan Streetman si->list.prio--; 233018ab4d4cSDan Streetman si->avail_list.prio--; 2331adfab836SDan Streetman } 233278ecba08SHugh Dickins least_priority++; 233378ecba08SHugh Dickins } 233418ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2335ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 23361da177e4SLinus Torvalds total_swap_pages -= p->pages; 23371da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2338ec8acf20SShaohua Li spin_unlock(&p->lock); 23395d337b91SHugh Dickins spin_unlock(&swap_lock); 2340fb4f88dcSHugh Dickins 2341039939a6STim Chen disable_swap_slots_cache_lock(); 2342039939a6STim Chen 2343e1e12d2fSDavid Rientjes set_current_oom_origin(); 2344adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2345e1e12d2fSDavid Rientjes clear_current_oom_origin(); 23461da177e4SLinus Torvalds 23471da177e4SLinus Torvalds if (err) { 23481da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2349cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 2350039939a6STim Chen reenable_swap_slots_cache_unlock(); 23511da177e4SLinus Torvalds goto out_dput; 23521da177e4SLinus Torvalds } 235352b7efdbSHugh Dickins 2354039939a6STim Chen reenable_swap_slots_cache_unlock(); 2355039939a6STim Chen 2356815c2c54SShaohua Li flush_work(&p->discard_work); 2357815c2c54SShaohua Li 23584cd3bb10SHugh Dickins destroy_swap_extents(p); 2359570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2360570a335bSHugh Dickins free_swap_count_continuations(p); 2361570a335bSHugh Dickins 2362fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 23635d337b91SHugh Dickins spin_lock(&swap_lock); 2364ec8acf20SShaohua Li spin_lock(&p->lock); 23651da177e4SLinus Torvalds drain_mmlist(); 23665d337b91SHugh Dickins 23675d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 23685d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 23695d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2370ec8acf20SShaohua Li spin_unlock(&p->lock); 23715d337b91SHugh Dickins spin_unlock(&swap_lock); 237213e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 23735d337b91SHugh Dickins spin_lock(&swap_lock); 2374ec8acf20SShaohua Li spin_lock(&p->lock); 23755d337b91SHugh Dickins } 23765d337b91SHugh Dickins 23771da177e4SLinus Torvalds swap_file = p->swap_file; 23785b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 23791da177e4SLinus Torvalds p->swap_file = NULL; 23801da177e4SLinus Torvalds p->max = 0; 23811da177e4SLinus Torvalds swap_map = p->swap_map; 23821da177e4SLinus Torvalds p->swap_map = NULL; 23832a8f9449SShaohua Li cluster_info = p->cluster_info; 23842a8f9449SShaohua Li p->cluster_info = NULL; 23854f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2386ec8acf20SShaohua Li spin_unlock(&p->lock); 23875d337b91SHugh Dickins spin_unlock(&swap_lock); 2388adfab836SDan Streetman frontswap_invalidate_area(p->type); 238958e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2390fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2391ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2392ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 23931da177e4SLinus Torvalds vfree(swap_map); 239454f180d3SHuang Ying kvfree(cluster_info); 239554f180d3SHuang Ying kvfree(frontswap_map); 23962de1a7e4SSeth Jennings /* Destroy swap account information */ 2397adfab836SDan Streetman swap_cgroup_swapoff(p->type); 23984b3ef9daSHuang, Ying exit_swap_address_space(p->type); 239927a7faa0SKAMEZAWA Hiroyuki 24001da177e4SLinus Torvalds inode = mapping->host; 24011da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 24021da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 24035b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2404e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 24051da177e4SLinus Torvalds } else { 24065955102cSAl Viro inode_lock(inode); 24071da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 24085955102cSAl Viro inode_unlock(inode); 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds filp_close(swap_file, NULL); 2411f893ab41SWeijie Yang 2412f893ab41SWeijie Yang /* 2413f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2414f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2415f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2416f893ab41SWeijie Yang */ 2417f893ab41SWeijie Yang spin_lock(&swap_lock); 2418f893ab41SWeijie Yang p->flags = 0; 2419f893ab41SWeijie Yang spin_unlock(&swap_lock); 2420f893ab41SWeijie Yang 24211da177e4SLinus Torvalds err = 0; 242266d7dd51SKay Sievers atomic_inc(&proc_poll_event); 242366d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 24241da177e4SLinus Torvalds 24251da177e4SLinus Torvalds out_dput: 24261da177e4SLinus Torvalds filp_close(victim, NULL); 24271da177e4SLinus Torvalds out: 2428f58b59c1SXiaotian Feng putname(pathname); 24291da177e4SLinus Torvalds return err; 24301da177e4SLinus Torvalds } 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 243366d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 243466d7dd51SKay Sievers { 2435f1514638SKay Sievers struct seq_file *seq = file->private_data; 243666d7dd51SKay Sievers 243766d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 243866d7dd51SKay Sievers 2439f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2440f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 244166d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 244266d7dd51SKay Sievers } 244366d7dd51SKay Sievers 244466d7dd51SKay Sievers return POLLIN | POLLRDNORM; 244566d7dd51SKay Sievers } 244666d7dd51SKay Sievers 24471da177e4SLinus Torvalds /* iterator */ 24481da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 24491da177e4SLinus Torvalds { 2450efa90a98SHugh Dickins struct swap_info_struct *si; 2451efa90a98SHugh Dickins int type; 24521da177e4SLinus Torvalds loff_t l = *pos; 24531da177e4SLinus Torvalds 2454fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 24551da177e4SLinus Torvalds 2456881e4aabSSuleiman Souhlal if (!l) 2457881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2458881e4aabSSuleiman Souhlal 2459efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2460efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2461efa90a98SHugh Dickins si = swap_info[type]; 2462efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 24631da177e4SLinus Torvalds continue; 2464881e4aabSSuleiman Souhlal if (!--l) 2465efa90a98SHugh Dickins return si; 24661da177e4SLinus Torvalds } 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds return NULL; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds 24711da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 24721da177e4SLinus Torvalds { 2473efa90a98SHugh Dickins struct swap_info_struct *si = v; 2474efa90a98SHugh Dickins int type; 24751da177e4SLinus Torvalds 2476881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2477efa90a98SHugh Dickins type = 0; 2478efa90a98SHugh Dickins else 2479efa90a98SHugh Dickins type = si->type + 1; 2480881e4aabSSuleiman Souhlal 2481efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 2482efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2483efa90a98SHugh Dickins si = swap_info[type]; 2484efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 24851da177e4SLinus Torvalds continue; 24861da177e4SLinus Torvalds ++*pos; 2487efa90a98SHugh Dickins return si; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 24901da177e4SLinus Torvalds return NULL; 24911da177e4SLinus Torvalds } 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 24941da177e4SLinus Torvalds { 2495fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 24961da177e4SLinus Torvalds } 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 24991da177e4SLinus Torvalds { 2500efa90a98SHugh Dickins struct swap_info_struct *si = v; 25011da177e4SLinus Torvalds struct file *file; 25021da177e4SLinus Torvalds int len; 25031da177e4SLinus Torvalds 2504efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 25051da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2506881e4aabSSuleiman Souhlal return 0; 2507881e4aabSSuleiman Souhlal } 25081da177e4SLinus Torvalds 2509efa90a98SHugh Dickins file = si->swap_file; 25102726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 25116eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 25121da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2513496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 25141da177e4SLinus Torvalds "partition" : "file\t", 2515efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2516efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2517efa90a98SHugh Dickins si->prio); 25181da177e4SLinus Torvalds return 0; 25191da177e4SLinus Torvalds } 25201da177e4SLinus Torvalds 252115ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 25221da177e4SLinus Torvalds .start = swap_start, 25231da177e4SLinus Torvalds .next = swap_next, 25241da177e4SLinus Torvalds .stop = swap_stop, 25251da177e4SLinus Torvalds .show = swap_show 25261da177e4SLinus Torvalds }; 25271da177e4SLinus Torvalds 25281da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 25291da177e4SLinus Torvalds { 2530f1514638SKay Sievers struct seq_file *seq; 253166d7dd51SKay Sievers int ret; 253266d7dd51SKay Sievers 253366d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2534f1514638SKay Sievers if (ret) 253566d7dd51SKay Sievers return ret; 253666d7dd51SKay Sievers 2537f1514638SKay Sievers seq = file->private_data; 2538f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2539f1514638SKay Sievers return 0; 25401da177e4SLinus Torvalds } 25411da177e4SLinus Torvalds 254215ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 25431da177e4SLinus Torvalds .open = swaps_open, 25441da177e4SLinus Torvalds .read = seq_read, 25451da177e4SLinus Torvalds .llseek = seq_lseek, 25461da177e4SLinus Torvalds .release = seq_release, 254766d7dd51SKay Sievers .poll = swaps_poll, 25481da177e4SLinus Torvalds }; 25491da177e4SLinus Torvalds 25501da177e4SLinus Torvalds static int __init procswaps_init(void) 25511da177e4SLinus Torvalds { 25523d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 25531da177e4SLinus Torvalds return 0; 25541da177e4SLinus Torvalds } 25551da177e4SLinus Torvalds __initcall(procswaps_init); 25561da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 25571da177e4SLinus Torvalds 25581796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 25591796316aSJan Beulich static int __init max_swapfiles_check(void) 25601796316aSJan Beulich { 25611796316aSJan Beulich MAX_SWAPFILES_CHECK(); 25621796316aSJan Beulich return 0; 25631796316aSJan Beulich } 25641796316aSJan Beulich late_initcall(max_swapfiles_check); 25651796316aSJan Beulich #endif 25661796316aSJan Beulich 256753cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 25681da177e4SLinus Torvalds { 25691da177e4SLinus Torvalds struct swap_info_struct *p; 25701da177e4SLinus Torvalds unsigned int type; 2571efa90a98SHugh Dickins 2572efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2573efa90a98SHugh Dickins if (!p) 257453cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2575efa90a98SHugh Dickins 25765d337b91SHugh Dickins spin_lock(&swap_lock); 2577efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2578efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 25791da177e4SLinus Torvalds break; 2580efa90a98SHugh Dickins } 25810697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 25825d337b91SHugh Dickins spin_unlock(&swap_lock); 2583efa90a98SHugh Dickins kfree(p); 2584730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 25851da177e4SLinus Torvalds } 2586efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2587efa90a98SHugh Dickins p->type = type; 2588efa90a98SHugh Dickins swap_info[type] = p; 2589efa90a98SHugh Dickins /* 2590efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2591efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2592efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2593efa90a98SHugh Dickins */ 2594efa90a98SHugh Dickins smp_wmb(); 2595efa90a98SHugh Dickins nr_swapfiles++; 2596efa90a98SHugh Dickins } else { 2597efa90a98SHugh Dickins kfree(p); 2598efa90a98SHugh Dickins p = swap_info[type]; 2599efa90a98SHugh Dickins /* 2600efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2601efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2602efa90a98SHugh Dickins */ 2603efa90a98SHugh Dickins } 26049625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 260518ab4d4cSDan Streetman plist_node_init(&p->list, 0); 260618ab4d4cSDan Streetman plist_node_init(&p->avail_list, 0); 26071da177e4SLinus Torvalds p->flags = SWP_USED; 26085d337b91SHugh Dickins spin_unlock(&swap_lock); 2609ec8acf20SShaohua Li spin_lock_init(&p->lock); 2610efa90a98SHugh Dickins 261153cbb243SCesar Eduardo Barros return p; 261253cbb243SCesar Eduardo Barros } 261353cbb243SCesar Eduardo Barros 26144d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 26154d0e1e10SCesar Eduardo Barros { 26164d0e1e10SCesar Eduardo Barros int error; 26174d0e1e10SCesar Eduardo Barros 26184d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 26194d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 26204d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 26216f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 26224d0e1e10SCesar Eduardo Barros if (error < 0) { 26234d0e1e10SCesar Eduardo Barros p->bdev = NULL; 26246f179af8SHugh Dickins return error; 26254d0e1e10SCesar Eduardo Barros } 26264d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 26274d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 26284d0e1e10SCesar Eduardo Barros if (error < 0) 262987ade72aSCesar Eduardo Barros return error; 26304d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 26314d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 26324d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 26335955102cSAl Viro inode_lock(inode); 263487ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 263587ade72aSCesar Eduardo Barros return -EBUSY; 263687ade72aSCesar Eduardo Barros } else 263787ade72aSCesar Eduardo Barros return -EINVAL; 26384d0e1e10SCesar Eduardo Barros 26394d0e1e10SCesar Eduardo Barros return 0; 26404d0e1e10SCesar Eduardo Barros } 26414d0e1e10SCesar Eduardo Barros 2642ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2643ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2644ca8bd38bSCesar Eduardo Barros struct inode *inode) 2645ca8bd38bSCesar Eduardo Barros { 2646ca8bd38bSCesar Eduardo Barros int i; 2647ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2648ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2649d6bbbd29SRaymond Jennings unsigned long last_page; 2650ca8bd38bSCesar Eduardo Barros 2651ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2652465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 265338719025SCesar Eduardo Barros return 0; 2654ca8bd38bSCesar Eduardo Barros } 2655ca8bd38bSCesar Eduardo Barros 2656ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2657ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2658ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2659ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2660ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2661dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2662dd111be6SJann Horn return 0; 2663ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2664ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2665ca8bd38bSCesar Eduardo Barros } 2666ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2667ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2668465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2669ca8bd38bSCesar Eduardo Barros swap_header->info.version); 267038719025SCesar Eduardo Barros return 0; 2671ca8bd38bSCesar Eduardo Barros } 2672ca8bd38bSCesar Eduardo Barros 2673ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2674ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2675ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2676ca8bd38bSCesar Eduardo Barros 2677ca8bd38bSCesar Eduardo Barros /* 2678ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 26799b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2680a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2681a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 26829b15b817SHugh Dickins * different architectures. In order to find the 2683a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2684ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2685a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2686ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2687ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2688ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 26899b15b817SHugh Dickins * swap pte. 2690ca8bd38bSCesar Eduardo Barros */ 2691ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 26929b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2693d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2694d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2695465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2696d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2697d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2698d6bbbd29SRaymond Jennings } 2699d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2700d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2701ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2702ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2703ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2704ca8bd38bSCesar Eduardo Barros } 2705ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2706ca8bd38bSCesar Eduardo Barros 2707ca8bd38bSCesar Eduardo Barros if (!maxpages) 270838719025SCesar Eduardo Barros return 0; 2709ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2710ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2711465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 271238719025SCesar Eduardo Barros return 0; 2713ca8bd38bSCesar Eduardo Barros } 2714ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 271538719025SCesar Eduardo Barros return 0; 2716ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 271738719025SCesar Eduardo Barros return 0; 2718ca8bd38bSCesar Eduardo Barros 2719ca8bd38bSCesar Eduardo Barros return maxpages; 2720ca8bd38bSCesar Eduardo Barros } 2721ca8bd38bSCesar Eduardo Barros 27224b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS \ 2723235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 27244b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS \ 27254b3ef9daSHuang, Ying DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) 27264b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS \ 27274b3ef9daSHuang, Ying max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) 2728235b6217SHuang, Ying 2729915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2730915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2731915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 27322a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2733915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2734915d4d7bSCesar Eduardo Barros sector_t *span) 2735915d4d7bSCesar Eduardo Barros { 2736235b6217SHuang, Ying unsigned int j, k; 2737915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 2738915d4d7bSCesar Eduardo Barros int nr_extents; 27392a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 2740235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 2741235b6217SHuang, Ying unsigned long i, idx; 2742915d4d7bSCesar Eduardo Barros 2743915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 2744915d4d7bSCesar Eduardo Barros 27456b534915SHuang Ying cluster_list_init(&p->free_clusters); 27466b534915SHuang Ying cluster_list_init(&p->discard_clusters); 27472a8f9449SShaohua Li 2748915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 2749915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 2750bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 2751bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2752915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 2753915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 2754915d4d7bSCesar Eduardo Barros nr_good_pages--; 27552a8f9449SShaohua Li /* 27562a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 27572a8f9449SShaohua Li * operation involved 27582a8f9449SShaohua Li */ 27592a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 2760915d4d7bSCesar Eduardo Barros } 2761915d4d7bSCesar Eduardo Barros } 2762915d4d7bSCesar Eduardo Barros 27632a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 27642a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 27652a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 27662a8f9449SShaohua Li 2767915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 2768915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 27692a8f9449SShaohua Li /* 27702a8f9449SShaohua Li * Not mark the cluster free yet, no list 27712a8f9449SShaohua Li * operation involved 27722a8f9449SShaohua Li */ 27732a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 2774915d4d7bSCesar Eduardo Barros p->max = maxpages; 2775915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 2776915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 2777bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 2778bdb8e3f6SCesar Eduardo Barros return nr_extents; 2779915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 2780915d4d7bSCesar Eduardo Barros } 2781915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 2782465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 2783bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2784915d4d7bSCesar Eduardo Barros } 2785915d4d7bSCesar Eduardo Barros 27862a8f9449SShaohua Li if (!cluster_info) 27872a8f9449SShaohua Li return nr_extents; 27882a8f9449SShaohua Li 2789235b6217SHuang, Ying 27904b3ef9daSHuang, Ying /* 27914b3ef9daSHuang, Ying * Reduce false cache line sharing between cluster_info and 27924b3ef9daSHuang, Ying * sharing same address space. 27934b3ef9daSHuang, Ying */ 2794235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 2795235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 2796235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 2797235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 2798235b6217SHuang, Ying if (idx >= nr_clusters) 2799235b6217SHuang, Ying continue; 2800235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 2801235b6217SHuang, Ying continue; 28022a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 28036b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 28046b534915SHuang Ying idx); 28052a8f9449SShaohua Li } 28062a8f9449SShaohua Li } 2807915d4d7bSCesar Eduardo Barros return nr_extents; 2808915d4d7bSCesar Eduardo Barros } 2809915d4d7bSCesar Eduardo Barros 2810dcf6b7ddSRafael Aquini /* 2811dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 2812dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 2813dcf6b7ddSRafael Aquini */ 2814dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 2815dcf6b7ddSRafael Aquini { 2816dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 2817dcf6b7ddSRafael Aquini 2818dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 2819dcf6b7ddSRafael Aquini return false; 2820dcf6b7ddSRafael Aquini 2821dcf6b7ddSRafael Aquini return true; 2822dcf6b7ddSRafael Aquini } 2823dcf6b7ddSRafael Aquini 282453cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 282553cbb243SCesar Eduardo Barros { 282653cbb243SCesar Eduardo Barros struct swap_info_struct *p; 282791a27b2aSJeff Layton struct filename *name; 282853cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 282953cbb243SCesar Eduardo Barros struct address_space *mapping; 283040531542SCesar Eduardo Barros int prio; 283153cbb243SCesar Eduardo Barros int error; 283253cbb243SCesar Eduardo Barros union swap_header *swap_header; 2833915d4d7bSCesar Eduardo Barros int nr_extents; 283453cbb243SCesar Eduardo Barros sector_t span; 283553cbb243SCesar Eduardo Barros unsigned long maxpages; 283653cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 28372a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 283838b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 283953cbb243SCesar Eduardo Barros struct page *page = NULL; 284053cbb243SCesar Eduardo Barros struct inode *inode = NULL; 284153cbb243SCesar Eduardo Barros 2842d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 2843d15cab97SHugh Dickins return -EINVAL; 2844d15cab97SHugh Dickins 284553cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 284653cbb243SCesar Eduardo Barros return -EPERM; 284753cbb243SCesar Eduardo Barros 284853cbb243SCesar Eduardo Barros p = alloc_swap_info(); 28492542e513SCesar Eduardo Barros if (IS_ERR(p)) 28502542e513SCesar Eduardo Barros return PTR_ERR(p); 285153cbb243SCesar Eduardo Barros 2852815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 2853815c2c54SShaohua Li 28541da177e4SLinus Torvalds name = getname(specialfile); 28551da177e4SLinus Torvalds if (IS_ERR(name)) { 28567de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 28571da177e4SLinus Torvalds name = NULL; 2858bd69010bSCesar Eduardo Barros goto bad_swap; 28591da177e4SLinus Torvalds } 2860669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 28611da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 28627de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 28631da177e4SLinus Torvalds swap_file = NULL; 2864bd69010bSCesar Eduardo Barros goto bad_swap; 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds p->swap_file = swap_file; 28681da177e4SLinus Torvalds mapping = swap_file->f_mapping; 28692130781eSCesar Eduardo Barros inode = mapping->host; 28706f179af8SHugh Dickins 28715955102cSAl Viro /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */ 28724d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 28734d0e1e10SCesar Eduardo Barros if (unlikely(error)) 28741da177e4SLinus Torvalds goto bad_swap; 28751da177e4SLinus Torvalds 28761da177e4SLinus Torvalds /* 28771da177e4SLinus Torvalds * Read the swap header. 28781da177e4SLinus Torvalds */ 28791da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 28801da177e4SLinus Torvalds error = -EINVAL; 28811da177e4SLinus Torvalds goto bad_swap; 28821da177e4SLinus Torvalds } 2883090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 28841da177e4SLinus Torvalds if (IS_ERR(page)) { 28851da177e4SLinus Torvalds error = PTR_ERR(page); 28861da177e4SLinus Torvalds goto bad_swap; 28871da177e4SLinus Torvalds } 288881e33971SHugh Dickins swap_header = kmap(page); 28891da177e4SLinus Torvalds 2890ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 2891ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 28921da177e4SLinus Torvalds error = -EINVAL; 28931da177e4SLinus Torvalds goto bad_swap; 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds 28961da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 2897803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 289878ecba08SHugh Dickins if (!swap_map) { 28991da177e4SLinus Torvalds error = -ENOMEM; 29001da177e4SLinus Torvalds goto bad_swap; 29011da177e4SLinus Torvalds } 2902f0571429SMinchan Kim 2903f0571429SMinchan Kim if (bdi_cap_stable_pages_required(inode_to_bdi(inode))) 2904f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 2905f0571429SMinchan Kim 29062a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 29076f179af8SHugh Dickins int cpu; 2908235b6217SHuang, Ying unsigned long ci, nr_cluster; 29096f179af8SHugh Dickins 29102a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 29112a8f9449SShaohua Li /* 29122a8f9449SShaohua Li * select a random position to start with to help wear leveling 29132a8f9449SShaohua Li * SSD 29142a8f9449SShaohua Li */ 29152a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 2916235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 29172a8f9449SShaohua Li 291854f180d3SHuang Ying cluster_info = kvzalloc(nr_cluster * sizeof(*cluster_info), 291954f180d3SHuang Ying GFP_KERNEL); 29202a8f9449SShaohua Li if (!cluster_info) { 29212a8f9449SShaohua Li error = -ENOMEM; 29222a8f9449SShaohua Li goto bad_swap; 29232a8f9449SShaohua Li } 2924235b6217SHuang, Ying 2925235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 2926235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 2927235b6217SHuang, Ying 2928ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 2929ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 2930ebc2a1a6SShaohua Li error = -ENOMEM; 2931ebc2a1a6SShaohua Li goto bad_swap; 2932ebc2a1a6SShaohua Li } 29336f179af8SHugh Dickins for_each_possible_cpu(cpu) { 2934ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 29356f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 2936ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 2937ebc2a1a6SShaohua Li } 29382a8f9449SShaohua Li } 29391da177e4SLinus Torvalds 29401421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 29411421ef3cSCesar Eduardo Barros if (error) 29421421ef3cSCesar Eduardo Barros goto bad_swap; 29431421ef3cSCesar Eduardo Barros 2944915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 29452a8f9449SShaohua Li cluster_info, maxpages, &span); 2946915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 294753092a74SHugh Dickins error = nr_extents; 2948e2244ec2SHugh Dickins goto bad_swap; 294953092a74SHugh Dickins } 295038b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 29518ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 295254f180d3SHuang Ying frontswap_map = kvzalloc(BITS_TO_LONGS(maxpages) * sizeof(long), 295354f180d3SHuang Ying GFP_KERNEL); 29541da177e4SLinus Torvalds 29552a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 2956dcf6b7ddSRafael Aquini /* 2957dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 2958dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 2959dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 2960dcf6b7ddSRafael Aquini * swapon(8) releases. 2961dcf6b7ddSRafael Aquini */ 2962dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 2963dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 2964dcf6b7ddSRafael Aquini 2965dcf6b7ddSRafael Aquini /* 2966dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 2967dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 2968dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 2969dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 2970dcf6b7ddSRafael Aquini */ 2971dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 2972dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 2973dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 2974dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 2975dcf6b7ddSRafael Aquini 2976dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 2977dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 2978dcf6b7ddSRafael Aquini int err = discard_swap(p); 2979dcf6b7ddSRafael Aquini if (unlikely(err)) 2980465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 2981dcf6b7ddSRafael Aquini p, err); 2982dcf6b7ddSRafael Aquini } 2983dcf6b7ddSRafael Aquini } 29846a6ba831SHugh Dickins 29854b3ef9daSHuang, Ying error = init_swap_address_space(p->type, maxpages); 29864b3ef9daSHuang, Ying if (error) 29874b3ef9daSHuang, Ying goto bad_swap; 29884b3ef9daSHuang, Ying 2989fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 299040531542SCesar Eduardo Barros prio = -1; 299178ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 299240531542SCesar Eduardo Barros prio = 299378ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 29942a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 2995c69dbfb8SCesar Eduardo Barros 2996756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 299791a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 2998c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 2999c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 300038b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 3001dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 3002dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 300338b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 3004c69dbfb8SCesar Eduardo Barros 3005fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 300666d7dd51SKay Sievers atomic_inc(&proc_poll_event); 300766d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 300866d7dd51SKay Sievers 30099b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 30109b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 30111da177e4SLinus Torvalds error = 0; 30121da177e4SLinus Torvalds goto out; 30131da177e4SLinus Torvalds bad_swap: 3014ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 3015ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 3016bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 3017f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 3018f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 30191da177e4SLinus Torvalds } 30204cd3bb10SHugh Dickins destroy_swap_extents(p); 3021e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 30225d337b91SHugh Dickins spin_lock(&swap_lock); 30231da177e4SLinus Torvalds p->swap_file = NULL; 30241da177e4SLinus Torvalds p->flags = 0; 30255d337b91SHugh Dickins spin_unlock(&swap_lock); 30261da177e4SLinus Torvalds vfree(swap_map); 30272a8f9449SShaohua Li vfree(cluster_info); 302852c50567SMel Gorman if (swap_file) { 30292130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 30305955102cSAl Viro inode_unlock(inode); 30312130781eSCesar Eduardo Barros inode = NULL; 30322130781eSCesar Eduardo Barros } 30331da177e4SLinus Torvalds filp_close(swap_file, NULL); 303452c50567SMel Gorman } 30351da177e4SLinus Torvalds out: 30361da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 30371da177e4SLinus Torvalds kunmap(page); 303809cbfeafSKirill A. Shutemov put_page(page); 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds if (name) 30411da177e4SLinus Torvalds putname(name); 30429b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 30435955102cSAl Viro inode_unlock(inode); 3044039939a6STim Chen if (!error) 3045039939a6STim Chen enable_swap_slots_cache(); 30461da177e4SLinus Torvalds return error; 30471da177e4SLinus Torvalds } 30481da177e4SLinus Torvalds 30491da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 30501da177e4SLinus Torvalds { 3051efa90a98SHugh Dickins unsigned int type; 30521da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 30531da177e4SLinus Torvalds 30545d337b91SHugh Dickins spin_lock(&swap_lock); 3055efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 3056efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 3057efa90a98SHugh Dickins 3058efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 3059efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 30601da177e4SLinus Torvalds } 3061ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 30621da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 30635d337b91SHugh Dickins spin_unlock(&swap_lock); 30641da177e4SLinus Torvalds } 30651da177e4SLinus Torvalds 30661da177e4SLinus Torvalds /* 30671da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 30681da177e4SLinus Torvalds * 3069355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 3070355cfa73SKAMEZAWA Hiroyuki * - success -> 0 3071355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 3072355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 3073355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 3074355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 3075570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 30761da177e4SLinus Torvalds */ 30778d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 30781da177e4SLinus Torvalds { 30791da177e4SLinus Torvalds struct swap_info_struct *p; 3080235b6217SHuang, Ying struct swap_cluster_info *ci; 30811da177e4SLinus Torvalds unsigned long offset, type; 30828d69aaeeSHugh Dickins unsigned char count; 30838d69aaeeSHugh Dickins unsigned char has_cache; 3084253d553bSHugh Dickins int err = -EINVAL; 30851da177e4SLinus Torvalds 3086a7420aa5SAndi Kleen if (non_swap_entry(entry)) 3087253d553bSHugh Dickins goto out; 30880697212aSChristoph Lameter 30891da177e4SLinus Torvalds type = swp_type(entry); 30901da177e4SLinus Torvalds if (type >= nr_swapfiles) 30911da177e4SLinus Torvalds goto bad_file; 3092efa90a98SHugh Dickins p = swap_info[type]; 30931da177e4SLinus Torvalds offset = swp_offset(entry); 3094355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 3095235b6217SHuang, Ying goto out; 3096235b6217SHuang, Ying 3097235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 3098355cfa73SKAMEZAWA Hiroyuki 3099253d553bSHugh Dickins count = p->swap_map[offset]; 3100edfe23daSShaohua Li 3101edfe23daSShaohua Li /* 3102edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 3103edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 3104edfe23daSShaohua Li */ 3105edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 3106edfe23daSShaohua Li err = -ENOENT; 3107edfe23daSShaohua Li goto unlock_out; 3108edfe23daSShaohua Li } 3109edfe23daSShaohua Li 3110253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 3111253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 3112253d553bSHugh Dickins err = 0; 3113355cfa73SKAMEZAWA Hiroyuki 3114253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 3115355cfa73SKAMEZAWA Hiroyuki 3116355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 3117253d553bSHugh Dickins if (!has_cache && count) 3118253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 3119253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 3120253d553bSHugh Dickins err = -EEXIST; 3121253d553bSHugh Dickins else /* no users remaining */ 3122253d553bSHugh Dickins err = -ENOENT; 3123355cfa73SKAMEZAWA Hiroyuki 3124355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 3125253d553bSHugh Dickins 3126570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 3127570a335bSHugh Dickins count += usage; 3128570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 3129253d553bSHugh Dickins err = -EINVAL; 3130570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 3131570a335bSHugh Dickins count = COUNT_CONTINUED; 3132570a335bSHugh Dickins else 3133570a335bSHugh Dickins err = -ENOMEM; 3134253d553bSHugh Dickins } else 3135253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 3136253d553bSHugh Dickins 3137253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 3138253d553bSHugh Dickins 3139355cfa73SKAMEZAWA Hiroyuki unlock_out: 3140235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 31411da177e4SLinus Torvalds out: 3142253d553bSHugh Dickins return err; 31431da177e4SLinus Torvalds 31441da177e4SLinus Torvalds bad_file: 3145465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 31461da177e4SLinus Torvalds goto out; 31471da177e4SLinus Torvalds } 3148253d553bSHugh Dickins 3149355cfa73SKAMEZAWA Hiroyuki /* 3150aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 3151aaa46865SHugh Dickins * (in which case its reference count is never incremented). 3152aaa46865SHugh Dickins */ 3153aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 3154aaa46865SHugh Dickins { 3155aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 3156aaa46865SHugh Dickins } 3157aaa46865SHugh Dickins 3158aaa46865SHugh Dickins /* 315908259d58SHugh Dickins * Increase reference count of swap entry by 1. 316008259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 316108259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 316208259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 316308259d58SHugh Dickins * might occur if a page table entry has got corrupted. 3164355cfa73SKAMEZAWA Hiroyuki */ 3165570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 3166355cfa73SKAMEZAWA Hiroyuki { 3167570a335bSHugh Dickins int err = 0; 3168570a335bSHugh Dickins 3169570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 3170570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 3171570a335bSHugh Dickins return err; 3172355cfa73SKAMEZAWA Hiroyuki } 31731da177e4SLinus Torvalds 3174cb4b86baSKAMEZAWA Hiroyuki /* 3175355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 3176355cfa73SKAMEZAWA Hiroyuki * 317773c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 3178355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 3179355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 3180355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 3181cb4b86baSKAMEZAWA Hiroyuki */ 3182cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 3183cb4b86baSKAMEZAWA Hiroyuki { 3184253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 3185cb4b86baSKAMEZAWA Hiroyuki } 3186cb4b86baSKAMEZAWA Hiroyuki 3187f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 3188f981c595SMel Gorman { 3189f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3190f981c595SMel Gorman return swap_info[swp_type(swap)]; 3191f981c595SMel Gorman } 3192f981c595SMel Gorman 3193f981c595SMel Gorman /* 3194f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 3195f981c595SMel Gorman */ 3196f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 3197f981c595SMel Gorman { 3198309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3199f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 3200f981c595SMel Gorman } 3201f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 3202f981c595SMel Gorman 3203f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 3204f981c595SMel Gorman { 3205f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3206309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3207f981c595SMel Gorman return swp_offset(swap); 3208f981c595SMel Gorman } 3209f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 3210f981c595SMel Gorman 32111da177e4SLinus Torvalds /* 3212570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 3213570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 3214570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 3215570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 3216570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 3217570a335bSHugh Dickins * 3218570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 3219570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 3220570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 3221570a335bSHugh Dickins * 3222570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 3223570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 3224570a335bSHugh Dickins * can be called after dropping locks. 3225570a335bSHugh Dickins */ 3226570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 3227570a335bSHugh Dickins { 3228570a335bSHugh Dickins struct swap_info_struct *si; 3229235b6217SHuang, Ying struct swap_cluster_info *ci; 3230570a335bSHugh Dickins struct page *head; 3231570a335bSHugh Dickins struct page *page; 3232570a335bSHugh Dickins struct page *list_page; 3233570a335bSHugh Dickins pgoff_t offset; 3234570a335bSHugh Dickins unsigned char count; 3235570a335bSHugh Dickins 3236570a335bSHugh Dickins /* 3237570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 3238570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 3239570a335bSHugh Dickins */ 3240570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 3241570a335bSHugh Dickins 3242570a335bSHugh Dickins si = swap_info_get(entry); 3243570a335bSHugh Dickins if (!si) { 3244570a335bSHugh Dickins /* 3245570a335bSHugh Dickins * An acceptable race has occurred since the failing 3246570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 3247570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 3248570a335bSHugh Dickins */ 3249570a335bSHugh Dickins goto outer; 3250570a335bSHugh Dickins } 3251570a335bSHugh Dickins 3252570a335bSHugh Dickins offset = swp_offset(entry); 3253235b6217SHuang, Ying 3254235b6217SHuang, Ying ci = lock_cluster(si, offset); 3255235b6217SHuang, Ying 3256570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 3257570a335bSHugh Dickins 3258570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 3259570a335bSHugh Dickins /* 3260570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 3261570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 3262570a335bSHugh Dickins * over-provisioning. 3263570a335bSHugh Dickins */ 3264570a335bSHugh Dickins goto out; 3265570a335bSHugh Dickins } 3266570a335bSHugh Dickins 3267570a335bSHugh Dickins if (!page) { 3268235b6217SHuang, Ying unlock_cluster(ci); 3269ec8acf20SShaohua Li spin_unlock(&si->lock); 3270570a335bSHugh Dickins return -ENOMEM; 3271570a335bSHugh Dickins } 3272570a335bSHugh Dickins 3273570a335bSHugh Dickins /* 3274570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 3275570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 3276570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 3277570a335bSHugh Dickins */ 3278570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3279570a335bSHugh Dickins offset &= ~PAGE_MASK; 3280570a335bSHugh Dickins 3281570a335bSHugh Dickins /* 3282570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 3283570a335bSHugh Dickins * but it does always reset its private field. 3284570a335bSHugh Dickins */ 3285570a335bSHugh Dickins if (!page_private(head)) { 3286570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3287570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 3288570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 3289570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 3290570a335bSHugh Dickins } 3291570a335bSHugh Dickins 3292570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 3293570a335bSHugh Dickins unsigned char *map; 3294570a335bSHugh Dickins 3295570a335bSHugh Dickins /* 3296570a335bSHugh Dickins * If the previous map said no continuation, but we've found 3297570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 3298570a335bSHugh Dickins */ 3299570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 3300570a335bSHugh Dickins goto out; 3301570a335bSHugh Dickins 33029b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 3303570a335bSHugh Dickins count = *map; 33049b04c5feSCong Wang kunmap_atomic(map); 3305570a335bSHugh Dickins 3306570a335bSHugh Dickins /* 3307570a335bSHugh Dickins * If this continuation count now has some space in it, 3308570a335bSHugh Dickins * free our allocation and use this one. 3309570a335bSHugh Dickins */ 3310570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 3311570a335bSHugh Dickins goto out; 3312570a335bSHugh Dickins } 3313570a335bSHugh Dickins 3314570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 3315570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 3316570a335bSHugh Dickins out: 3317235b6217SHuang, Ying unlock_cluster(ci); 3318ec8acf20SShaohua Li spin_unlock(&si->lock); 3319570a335bSHugh Dickins outer: 3320570a335bSHugh Dickins if (page) 3321570a335bSHugh Dickins __free_page(page); 3322570a335bSHugh Dickins return 0; 3323570a335bSHugh Dickins } 3324570a335bSHugh Dickins 3325570a335bSHugh Dickins /* 3326570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3327570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3328570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3329570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3330570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3331235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3332235b6217SHuang, Ying * lock. 3333570a335bSHugh Dickins */ 3334570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3335570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3336570a335bSHugh Dickins { 3337570a335bSHugh Dickins struct page *head; 3338570a335bSHugh Dickins struct page *page; 3339570a335bSHugh Dickins unsigned char *map; 3340570a335bSHugh Dickins 3341570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3342570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3343570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3344570a335bSHugh Dickins return false; /* need to add count continuation */ 3345570a335bSHugh Dickins } 3346570a335bSHugh Dickins 3347570a335bSHugh Dickins offset &= ~PAGE_MASK; 3348570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 33499b04c5feSCong Wang map = kmap_atomic(page) + offset; 3350570a335bSHugh Dickins 3351570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3352570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3353570a335bSHugh Dickins 3354570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3355570a335bSHugh Dickins /* 3356570a335bSHugh Dickins * Think of how you add 1 to 999 3357570a335bSHugh Dickins */ 3358570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 33599b04c5feSCong Wang kunmap_atomic(map); 3360570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3361570a335bSHugh Dickins BUG_ON(page == head); 33629b04c5feSCong Wang map = kmap_atomic(page) + offset; 3363570a335bSHugh Dickins } 3364570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 33659b04c5feSCong Wang kunmap_atomic(map); 3366570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3367570a335bSHugh Dickins if (page == head) 3368570a335bSHugh Dickins return false; /* add count continuation */ 33699b04c5feSCong Wang map = kmap_atomic(page) + offset; 3370570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3371570a335bSHugh Dickins } 3372570a335bSHugh Dickins *map += 1; 33739b04c5feSCong Wang kunmap_atomic(map); 3374570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3375570a335bSHugh Dickins while (page != head) { 33769b04c5feSCong Wang map = kmap_atomic(page) + offset; 3377570a335bSHugh Dickins *map = COUNT_CONTINUED; 33789b04c5feSCong Wang kunmap_atomic(map); 3379570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3380570a335bSHugh Dickins } 3381570a335bSHugh Dickins return true; /* incremented */ 3382570a335bSHugh Dickins 3383570a335bSHugh Dickins } else { /* decrementing */ 3384570a335bSHugh Dickins /* 3385570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3386570a335bSHugh Dickins */ 3387570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3388570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 33899b04c5feSCong Wang kunmap_atomic(map); 3390570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3391570a335bSHugh Dickins BUG_ON(page == head); 33929b04c5feSCong Wang map = kmap_atomic(page) + offset; 3393570a335bSHugh Dickins } 3394570a335bSHugh Dickins BUG_ON(*map == 0); 3395570a335bSHugh Dickins *map -= 1; 3396570a335bSHugh Dickins if (*map == 0) 3397570a335bSHugh Dickins count = 0; 33989b04c5feSCong Wang kunmap_atomic(map); 3399570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3400570a335bSHugh Dickins while (page != head) { 34019b04c5feSCong Wang map = kmap_atomic(page) + offset; 3402570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3403570a335bSHugh Dickins count = COUNT_CONTINUED; 34049b04c5feSCong Wang kunmap_atomic(map); 3405570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3406570a335bSHugh Dickins } 3407570a335bSHugh Dickins return count == COUNT_CONTINUED; 3408570a335bSHugh Dickins } 3409570a335bSHugh Dickins } 3410570a335bSHugh Dickins 3411570a335bSHugh Dickins /* 3412570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3413570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3414570a335bSHugh Dickins */ 3415570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3416570a335bSHugh Dickins { 3417570a335bSHugh Dickins pgoff_t offset; 3418570a335bSHugh Dickins 3419570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3420570a335bSHugh Dickins struct page *head; 3421570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3422570a335bSHugh Dickins if (page_private(head)) { 34230d576d20SGeliang Tang struct page *page, *next; 34240d576d20SGeliang Tang 34250d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 34260d576d20SGeliang Tang list_del(&page->lru); 3427570a335bSHugh Dickins __free_page(page); 3428570a335bSHugh Dickins } 3429570a335bSHugh Dickins } 3430570a335bSHugh Dickins } 3431570a335bSHugh Dickins } 3432