11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/swapfile.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/mm.h> 96e84f315SIngo Molnar #include <linux/sched/mm.h> 1029930025SIngo Molnar #include <linux/sched/task.h> 111da177e4SLinus Torvalds #include <linux/hugetlb.h> 121da177e4SLinus Torvalds #include <linux/mman.h> 131da177e4SLinus Torvalds #include <linux/slab.h> 141da177e4SLinus Torvalds #include <linux/kernel_stat.h> 151da177e4SLinus Torvalds #include <linux/swap.h> 161da177e4SLinus Torvalds #include <linux/vmalloc.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/namei.h> 19072441e2SHugh Dickins #include <linux/shmem_fs.h> 201da177e4SLinus Torvalds #include <linux/blkdev.h> 2120137a49SHugh Dickins #include <linux/random.h> 221da177e4SLinus Torvalds #include <linux/writeback.h> 231da177e4SLinus Torvalds #include <linux/proc_fs.h> 241da177e4SLinus Torvalds #include <linux/seq_file.h> 251da177e4SLinus Torvalds #include <linux/init.h> 265ad64688SHugh Dickins #include <linux/ksm.h> 271da177e4SLinus Torvalds #include <linux/rmap.h> 281da177e4SLinus Torvalds #include <linux/security.h> 291da177e4SLinus Torvalds #include <linux/backing-dev.h> 30fc0abb14SIngo Molnar #include <linux/mutex.h> 31c59ede7bSRandy.Dunlap #include <linux/capability.h> 321da177e4SLinus Torvalds #include <linux/syscalls.h> 338a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3466d7dd51SKay Sievers #include <linux/poll.h> 3572788c38SDavid Rientjes #include <linux/oom.h> 3638b5faf4SDan Magenheimer #include <linux/frontswap.h> 3738b5faf4SDan Magenheimer #include <linux/swapfile.h> 38f981c595SMel Gorman #include <linux/export.h> 3967afa38eSTim Chen #include <linux/swap_slots.h> 40155b5f88SHuang Ying #include <linux/sort.h> 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds #include <asm/pgtable.h> 431da177e4SLinus Torvalds #include <asm/tlbflush.h> 441da177e4SLinus Torvalds #include <linux/swapops.h> 455d1ea48bSJohannes Weiner #include <linux/swap_cgroup.h> 461da177e4SLinus Torvalds 47570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 48570a335bSHugh Dickins unsigned char); 49570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 50d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 51570a335bSHugh Dickins 5238b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 537c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 54ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 55fb0fec50SChris Wilson /* 56fb0fec50SChris Wilson * Some modules use swappable objects and may try to swap them out under 57fb0fec50SChris Wilson * memory pressure (via the shrinker). Before doing so, they may wish to 58fb0fec50SChris Wilson * check to see if any swap space is available. 59fb0fec50SChris Wilson */ 60fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages); 61ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 621da177e4SLinus Torvalds long total_swap_pages; 63a2468cc9SAaron Lu static int least_priority = -1; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 661da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 671da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 681da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 691da177e4SLinus Torvalds 70adfab836SDan Streetman /* 71adfab836SDan Streetman * all active swap_info_structs 72adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 73adfab836SDan Streetman */ 7418ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 7518ab4d4cSDan Streetman 7618ab4d4cSDan Streetman /* 7718ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 7818ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 7918ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 8018ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 8118ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 8218ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 8318ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 8418ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 8518ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 8618ab4d4cSDan Streetman * before any swap_info_struct->lock. 8718ab4d4cSDan Streetman */ 88a2468cc9SAaron Lu struct plist_head *swap_avail_heads; 8918ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 901da177e4SLinus Torvalds 9138b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 921da177e4SLinus Torvalds 93fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 941da177e4SLinus Torvalds 9566d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 9666d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 9766d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 9866d7dd51SKay Sievers 9981a0298bSHuang Ying atomic_t nr_rotate_swap = ATOMIC_INIT(0); 10081a0298bSHuang Ying 1018d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 102355cfa73SKAMEZAWA Hiroyuki { 103570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 104355cfa73SKAMEZAWA Hiroyuki } 105355cfa73SKAMEZAWA Hiroyuki 106efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 107c9e44410SKAMEZAWA Hiroyuki static int 108c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 109c9e44410SKAMEZAWA Hiroyuki { 110efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 111c9e44410SKAMEZAWA Hiroyuki struct page *page; 112c9e44410SKAMEZAWA Hiroyuki int ret = 0; 113c9e44410SKAMEZAWA Hiroyuki 114f6ab1f7fSHuang Ying page = find_get_page(swap_address_space(entry), swp_offset(entry)); 115c9e44410SKAMEZAWA Hiroyuki if (!page) 116c9e44410SKAMEZAWA Hiroyuki return 0; 117c9e44410SKAMEZAWA Hiroyuki /* 118c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 119c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 120c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 121c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 122c9e44410SKAMEZAWA Hiroyuki * in usual operations. 123c9e44410SKAMEZAWA Hiroyuki */ 124c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 125c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 126c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 127c9e44410SKAMEZAWA Hiroyuki } 12809cbfeafSKirill A. Shutemov put_page(page); 129c9e44410SKAMEZAWA Hiroyuki return ret; 130c9e44410SKAMEZAWA Hiroyuki } 131355cfa73SKAMEZAWA Hiroyuki 1321da177e4SLinus Torvalds /* 1336a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1346a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1356a6ba831SHugh Dickins */ 1366a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1376a6ba831SHugh Dickins { 1386a6ba831SHugh Dickins struct swap_extent *se; 1399625a5f2SHugh Dickins sector_t start_block; 1409625a5f2SHugh Dickins sector_t nr_blocks; 1416a6ba831SHugh Dickins int err = 0; 1426a6ba831SHugh Dickins 1436a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1449625a5f2SHugh Dickins se = &si->first_swap_extent; 1459625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1469625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1479625a5f2SHugh Dickins if (nr_blocks) { 1489625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 149dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1509625a5f2SHugh Dickins if (err) 1519625a5f2SHugh Dickins return err; 1529625a5f2SHugh Dickins cond_resched(); 1536a6ba831SHugh Dickins } 1546a6ba831SHugh Dickins 1559625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1569625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1579625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1589625a5f2SHugh Dickins 1596a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 160dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1616a6ba831SHugh Dickins if (err) 1626a6ba831SHugh Dickins break; 1636a6ba831SHugh Dickins 1646a6ba831SHugh Dickins cond_resched(); 1656a6ba831SHugh Dickins } 1666a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1676a6ba831SHugh Dickins } 1686a6ba831SHugh Dickins 1697992fde7SHugh Dickins /* 1707992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1717992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1727992fde7SHugh Dickins */ 1737992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1747992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1757992fde7SHugh Dickins { 1767992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1777992fde7SHugh Dickins int found_extent = 0; 1787992fde7SHugh Dickins 1797992fde7SHugh Dickins while (nr_pages) { 1807992fde7SHugh Dickins if (se->start_page <= start_page && 1817992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1827992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1837992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 184858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1857992fde7SHugh Dickins 1867992fde7SHugh Dickins if (nr_blocks > nr_pages) 1877992fde7SHugh Dickins nr_blocks = nr_pages; 1887992fde7SHugh Dickins start_page += nr_blocks; 1897992fde7SHugh Dickins nr_pages -= nr_blocks; 1907992fde7SHugh Dickins 1917992fde7SHugh Dickins if (!found_extent++) 1927992fde7SHugh Dickins si->curr_swap_extent = se; 1937992fde7SHugh Dickins 1947992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1957992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1967992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 197dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1987992fde7SHugh Dickins break; 1997992fde7SHugh Dickins } 2007992fde7SHugh Dickins 201a8ae4991SGeliang Tang se = list_next_entry(se, list); 2027992fde7SHugh Dickins } 2037992fde7SHugh Dickins } 2047992fde7SHugh Dickins 20538d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 20638d8b4e6SHuang Ying #define SWAPFILE_CLUSTER HPAGE_PMD_NR 20738d8b4e6SHuang Ying #else 208048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 20938d8b4e6SHuang Ying #endif 210048c27fdSHugh Dickins #define LATENCY_LIMIT 256 211048c27fdSHugh Dickins 2122a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2132a8f9449SShaohua Li unsigned int flag) 2142a8f9449SShaohua Li { 2152a8f9449SShaohua Li info->flags = flag; 2162a8f9449SShaohua Li } 2172a8f9449SShaohua Li 2182a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2192a8f9449SShaohua Li { 2202a8f9449SShaohua Li return info->data; 2212a8f9449SShaohua Li } 2222a8f9449SShaohua Li 2232a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2242a8f9449SShaohua Li unsigned int c) 2252a8f9449SShaohua Li { 2262a8f9449SShaohua Li info->data = c; 2272a8f9449SShaohua Li } 2282a8f9449SShaohua Li 2292a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2302a8f9449SShaohua Li unsigned int c, unsigned int f) 2312a8f9449SShaohua Li { 2322a8f9449SShaohua Li info->flags = f; 2332a8f9449SShaohua Li info->data = c; 2342a8f9449SShaohua Li } 2352a8f9449SShaohua Li 2362a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2372a8f9449SShaohua Li { 2382a8f9449SShaohua Li return info->data; 2392a8f9449SShaohua Li } 2402a8f9449SShaohua Li 2412a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2422a8f9449SShaohua Li unsigned int n) 2432a8f9449SShaohua Li { 2442a8f9449SShaohua Li info->data = n; 2452a8f9449SShaohua Li } 2462a8f9449SShaohua Li 2472a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 2482a8f9449SShaohua Li unsigned int n, unsigned int f) 2492a8f9449SShaohua Li { 2502a8f9449SShaohua Li info->flags = f; 2512a8f9449SShaohua Li info->data = n; 2522a8f9449SShaohua Li } 2532a8f9449SShaohua Li 2542a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 2552a8f9449SShaohua Li { 2562a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 2572a8f9449SShaohua Li } 2582a8f9449SShaohua Li 2592a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 2602a8f9449SShaohua Li { 2612a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 2622a8f9449SShaohua Li } 2632a8f9449SShaohua Li 2642a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 2652a8f9449SShaohua Li { 2662a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 2672a8f9449SShaohua Li info->data = 0; 2682a8f9449SShaohua Li } 2692a8f9449SShaohua Li 270e0709829SHuang Ying static inline bool cluster_is_huge(struct swap_cluster_info *info) 271e0709829SHuang Ying { 272e0709829SHuang Ying return info->flags & CLUSTER_FLAG_HUGE; 273e0709829SHuang Ying } 274e0709829SHuang Ying 275e0709829SHuang Ying static inline void cluster_clear_huge(struct swap_cluster_info *info) 276e0709829SHuang Ying { 277e0709829SHuang Ying info->flags &= ~CLUSTER_FLAG_HUGE; 278e0709829SHuang Ying } 279e0709829SHuang Ying 280235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 281235b6217SHuang, Ying unsigned long offset) 282235b6217SHuang, Ying { 283235b6217SHuang, Ying struct swap_cluster_info *ci; 284235b6217SHuang, Ying 285235b6217SHuang, Ying ci = si->cluster_info; 286235b6217SHuang, Ying if (ci) { 287235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 288235b6217SHuang, Ying spin_lock(&ci->lock); 289235b6217SHuang, Ying } 290235b6217SHuang, Ying return ci; 291235b6217SHuang, Ying } 292235b6217SHuang, Ying 293235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 294235b6217SHuang, Ying { 295235b6217SHuang, Ying if (ci) 296235b6217SHuang, Ying spin_unlock(&ci->lock); 297235b6217SHuang, Ying } 298235b6217SHuang, Ying 299235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 300235b6217SHuang, Ying struct swap_info_struct *si, 301235b6217SHuang, Ying unsigned long offset) 302235b6217SHuang, Ying { 303235b6217SHuang, Ying struct swap_cluster_info *ci; 304235b6217SHuang, Ying 305235b6217SHuang, Ying ci = lock_cluster(si, offset); 306235b6217SHuang, Ying if (!ci) 307235b6217SHuang, Ying spin_lock(&si->lock); 308235b6217SHuang, Ying 309235b6217SHuang, Ying return ci; 310235b6217SHuang, Ying } 311235b6217SHuang, Ying 312235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 313235b6217SHuang, Ying struct swap_cluster_info *ci) 314235b6217SHuang, Ying { 315235b6217SHuang, Ying if (ci) 316235b6217SHuang, Ying unlock_cluster(ci); 317235b6217SHuang, Ying else 318235b6217SHuang, Ying spin_unlock(&si->lock); 319235b6217SHuang, Ying } 320235b6217SHuang, Ying 3216b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3226b534915SHuang Ying { 3236b534915SHuang Ying return cluster_is_null(&list->head); 3246b534915SHuang Ying } 3256b534915SHuang Ying 3266b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 3276b534915SHuang Ying { 3286b534915SHuang Ying return cluster_next(&list->head); 3296b534915SHuang Ying } 3306b534915SHuang Ying 3316b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 3326b534915SHuang Ying { 3336b534915SHuang Ying cluster_set_null(&list->head); 3346b534915SHuang Ying cluster_set_null(&list->tail); 3356b534915SHuang Ying } 3366b534915SHuang Ying 3376b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 3386b534915SHuang Ying struct swap_cluster_info *ci, 3396b534915SHuang Ying unsigned int idx) 3406b534915SHuang Ying { 3416b534915SHuang Ying if (cluster_list_empty(list)) { 3426b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 3436b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3446b534915SHuang Ying } else { 345235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 3466b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 3476b534915SHuang Ying 348235b6217SHuang, Ying /* 349235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 350235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 351235b6217SHuang, Ying */ 352235b6217SHuang, Ying ci_tail = ci + tail; 353235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 354235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 3550ef017d1SHuang Ying spin_unlock(&ci_tail->lock); 3566b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 3576b534915SHuang Ying } 3586b534915SHuang Ying } 3596b534915SHuang Ying 3606b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 3616b534915SHuang Ying struct swap_cluster_info *ci) 3626b534915SHuang Ying { 3636b534915SHuang Ying unsigned int idx; 3646b534915SHuang Ying 3656b534915SHuang Ying idx = cluster_next(&list->head); 3666b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 3676b534915SHuang Ying cluster_set_null(&list->head); 3686b534915SHuang Ying cluster_set_null(&list->tail); 3696b534915SHuang Ying } else 3706b534915SHuang Ying cluster_set_next_flag(&list->head, 3716b534915SHuang Ying cluster_next(&ci[idx]), 0); 3726b534915SHuang Ying 3736b534915SHuang Ying return idx; 3746b534915SHuang Ying } 3756b534915SHuang Ying 376815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 377815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 378815c2c54SShaohua Li unsigned int idx) 379815c2c54SShaohua Li { 380815c2c54SShaohua Li /* 381815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 382815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 383815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 384815c2c54SShaohua Li * will be cleared after discard 385815c2c54SShaohua Li */ 386815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 387815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 388815c2c54SShaohua Li 3896b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 390815c2c54SShaohua Li 391815c2c54SShaohua Li schedule_work(&si->discard_work); 392815c2c54SShaohua Li } 393815c2c54SShaohua Li 39438d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx) 39538d8b4e6SHuang Ying { 39638d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 39738d8b4e6SHuang Ying 39838d8b4e6SHuang Ying cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE); 39938d8b4e6SHuang Ying cluster_list_add_tail(&si->free_clusters, ci, idx); 40038d8b4e6SHuang Ying } 40138d8b4e6SHuang Ying 402815c2c54SShaohua Li /* 403815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 404815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 405815c2c54SShaohua Li */ 406815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 407815c2c54SShaohua Li { 408235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 409815c2c54SShaohua Li unsigned int idx; 410815c2c54SShaohua Li 411815c2c54SShaohua Li info = si->cluster_info; 412815c2c54SShaohua Li 4136b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 4146b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 415815c2c54SShaohua Li spin_unlock(&si->lock); 416815c2c54SShaohua Li 417815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 418815c2c54SShaohua Li SWAPFILE_CLUSTER); 419815c2c54SShaohua Li 420815c2c54SShaohua Li spin_lock(&si->lock); 421235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 42238d8b4e6SHuang Ying __free_cluster(si, idx); 423815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 424815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 425235b6217SHuang, Ying unlock_cluster(ci); 426815c2c54SShaohua Li } 427815c2c54SShaohua Li } 428815c2c54SShaohua Li 429815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 430815c2c54SShaohua Li { 431815c2c54SShaohua Li struct swap_info_struct *si; 432815c2c54SShaohua Li 433815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 434815c2c54SShaohua Li 435815c2c54SShaohua Li spin_lock(&si->lock); 436815c2c54SShaohua Li swap_do_scheduled_discard(si); 437815c2c54SShaohua Li spin_unlock(&si->lock); 438815c2c54SShaohua Li } 439815c2c54SShaohua Li 44038d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx) 44138d8b4e6SHuang Ying { 44238d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 44338d8b4e6SHuang Ying 44438d8b4e6SHuang Ying VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx); 44538d8b4e6SHuang Ying cluster_list_del_first(&si->free_clusters, ci); 44638d8b4e6SHuang Ying cluster_set_count_flag(ci + idx, 0, 0); 44738d8b4e6SHuang Ying } 44838d8b4e6SHuang Ying 44938d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx) 45038d8b4e6SHuang Ying { 45138d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info + idx; 45238d8b4e6SHuang Ying 45338d8b4e6SHuang Ying VM_BUG_ON(cluster_count(ci) != 0); 45438d8b4e6SHuang Ying /* 45538d8b4e6SHuang Ying * If the swap is discardable, prepare discard the cluster 45638d8b4e6SHuang Ying * instead of free it immediately. The cluster will be freed 45738d8b4e6SHuang Ying * after discard. 45838d8b4e6SHuang Ying */ 45938d8b4e6SHuang Ying if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 46038d8b4e6SHuang Ying (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 46138d8b4e6SHuang Ying swap_cluster_schedule_discard(si, idx); 46238d8b4e6SHuang Ying return; 46338d8b4e6SHuang Ying } 46438d8b4e6SHuang Ying 46538d8b4e6SHuang Ying __free_cluster(si, idx); 46638d8b4e6SHuang Ying } 46738d8b4e6SHuang Ying 4682a8f9449SShaohua Li /* 4692a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 4702a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 4712a8f9449SShaohua Li */ 4722a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 4732a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4742a8f9449SShaohua Li { 4752a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4762a8f9449SShaohua Li 4772a8f9449SShaohua Li if (!cluster_info) 4782a8f9449SShaohua Li return; 47938d8b4e6SHuang Ying if (cluster_is_free(&cluster_info[idx])) 48038d8b4e6SHuang Ying alloc_cluster(p, idx); 4812a8f9449SShaohua Li 4822a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 4832a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 4842a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 4852a8f9449SShaohua Li } 4862a8f9449SShaohua Li 4872a8f9449SShaohua Li /* 4882a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 4892a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 4902a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 4912a8f9449SShaohua Li */ 4922a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 4932a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 4942a8f9449SShaohua Li { 4952a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 4962a8f9449SShaohua Li 4972a8f9449SShaohua Li if (!cluster_info) 4982a8f9449SShaohua Li return; 4992a8f9449SShaohua Li 5002a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 5012a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 5022a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 5032a8f9449SShaohua Li 50438d8b4e6SHuang Ying if (cluster_count(&cluster_info[idx]) == 0) 50538d8b4e6SHuang Ying free_cluster(p, idx); 5062a8f9449SShaohua Li } 5072a8f9449SShaohua Li 5082a8f9449SShaohua Li /* 5092a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 5102a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 5112a8f9449SShaohua Li */ 512ebc2a1a6SShaohua Li static bool 513ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 5142a8f9449SShaohua Li unsigned long offset) 5152a8f9449SShaohua Li { 516ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 517ebc2a1a6SShaohua Li bool conflict; 518ebc2a1a6SShaohua Li 5192a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 5206b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 5216b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 5222a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 523ebc2a1a6SShaohua Li 524ebc2a1a6SShaohua Li if (!conflict) 525ebc2a1a6SShaohua Li return false; 526ebc2a1a6SShaohua Li 527ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 528ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 529ebc2a1a6SShaohua Li return true; 530ebc2a1a6SShaohua Li } 531ebc2a1a6SShaohua Li 532ebc2a1a6SShaohua Li /* 533ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 534ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 535ebc2a1a6SShaohua Li */ 53636005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 537ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 538ebc2a1a6SShaohua Li { 539ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 540235b6217SHuang, Ying struct swap_cluster_info *ci; 541ebc2a1a6SShaohua Li bool found_free; 542235b6217SHuang, Ying unsigned long tmp, max; 543ebc2a1a6SShaohua Li 544ebc2a1a6SShaohua Li new_cluster: 545ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 546ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 5476b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 5486b534915SHuang Ying cluster->index = si->free_clusters.head; 549ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 550ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 5516b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 552ebc2a1a6SShaohua Li /* 553ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 554ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 555ebc2a1a6SShaohua Li */ 556ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 557ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 558ebc2a1a6SShaohua Li goto new_cluster; 559ebc2a1a6SShaohua Li } else 56036005baeSTim Chen return false; 561ebc2a1a6SShaohua Li } 562ebc2a1a6SShaohua Li 563ebc2a1a6SShaohua Li found_free = false; 564ebc2a1a6SShaohua Li 565ebc2a1a6SShaohua Li /* 566ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 567ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 568ebc2a1a6SShaohua Li */ 569ebc2a1a6SShaohua Li tmp = cluster->next; 570235b6217SHuang, Ying max = min_t(unsigned long, si->max, 571235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 572235b6217SHuang, Ying if (tmp >= max) { 573235b6217SHuang, Ying cluster_set_null(&cluster->index); 574235b6217SHuang, Ying goto new_cluster; 575235b6217SHuang, Ying } 576235b6217SHuang, Ying ci = lock_cluster(si, tmp); 577235b6217SHuang, Ying while (tmp < max) { 578ebc2a1a6SShaohua Li if (!si->swap_map[tmp]) { 579ebc2a1a6SShaohua Li found_free = true; 580ebc2a1a6SShaohua Li break; 581ebc2a1a6SShaohua Li } 582ebc2a1a6SShaohua Li tmp++; 583ebc2a1a6SShaohua Li } 584235b6217SHuang, Ying unlock_cluster(ci); 585ebc2a1a6SShaohua Li if (!found_free) { 586ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 587ebc2a1a6SShaohua Li goto new_cluster; 588ebc2a1a6SShaohua Li } 589ebc2a1a6SShaohua Li cluster->next = tmp + 1; 590ebc2a1a6SShaohua Li *offset = tmp; 591ebc2a1a6SShaohua Li *scan_base = tmp; 59236005baeSTim Chen return found_free; 5932a8f9449SShaohua Li } 5942a8f9449SShaohua Li 595a2468cc9SAaron Lu static void __del_from_avail_list(struct swap_info_struct *p) 596a2468cc9SAaron Lu { 597a2468cc9SAaron Lu int nid; 598a2468cc9SAaron Lu 599a2468cc9SAaron Lu for_each_node(nid) 600a2468cc9SAaron Lu plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]); 601a2468cc9SAaron Lu } 602a2468cc9SAaron Lu 603a2468cc9SAaron Lu static void del_from_avail_list(struct swap_info_struct *p) 604a2468cc9SAaron Lu { 605a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 606a2468cc9SAaron Lu __del_from_avail_list(p); 607a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 608a2468cc9SAaron Lu } 609a2468cc9SAaron Lu 61038d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, 61138d8b4e6SHuang Ying unsigned int nr_entries) 61238d8b4e6SHuang Ying { 61338d8b4e6SHuang Ying unsigned int end = offset + nr_entries - 1; 61438d8b4e6SHuang Ying 61538d8b4e6SHuang Ying if (offset == si->lowest_bit) 61638d8b4e6SHuang Ying si->lowest_bit += nr_entries; 61738d8b4e6SHuang Ying if (end == si->highest_bit) 61838d8b4e6SHuang Ying si->highest_bit -= nr_entries; 61938d8b4e6SHuang Ying si->inuse_pages += nr_entries; 62038d8b4e6SHuang Ying if (si->inuse_pages == si->pages) { 62138d8b4e6SHuang Ying si->lowest_bit = si->max; 62238d8b4e6SHuang Ying si->highest_bit = 0; 623a2468cc9SAaron Lu del_from_avail_list(si); 62438d8b4e6SHuang Ying } 62538d8b4e6SHuang Ying } 62638d8b4e6SHuang Ying 627a2468cc9SAaron Lu static void add_to_avail_list(struct swap_info_struct *p) 628a2468cc9SAaron Lu { 629a2468cc9SAaron Lu int nid; 630a2468cc9SAaron Lu 631a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 632a2468cc9SAaron Lu for_each_node(nid) { 633a2468cc9SAaron Lu WARN_ON(!plist_node_empty(&p->avail_lists[nid])); 634a2468cc9SAaron Lu plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]); 635a2468cc9SAaron Lu } 636a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 637a2468cc9SAaron Lu } 638a2468cc9SAaron Lu 63938d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset, 64038d8b4e6SHuang Ying unsigned int nr_entries) 64138d8b4e6SHuang Ying { 64238d8b4e6SHuang Ying unsigned long end = offset + nr_entries - 1; 64338d8b4e6SHuang Ying void (*swap_slot_free_notify)(struct block_device *, unsigned long); 64438d8b4e6SHuang Ying 64538d8b4e6SHuang Ying if (offset < si->lowest_bit) 64638d8b4e6SHuang Ying si->lowest_bit = offset; 64738d8b4e6SHuang Ying if (end > si->highest_bit) { 64838d8b4e6SHuang Ying bool was_full = !si->highest_bit; 64938d8b4e6SHuang Ying 65038d8b4e6SHuang Ying si->highest_bit = end; 651a2468cc9SAaron Lu if (was_full && (si->flags & SWP_WRITEOK)) 652a2468cc9SAaron Lu add_to_avail_list(si); 65338d8b4e6SHuang Ying } 65438d8b4e6SHuang Ying atomic_long_add(nr_entries, &nr_swap_pages); 65538d8b4e6SHuang Ying si->inuse_pages -= nr_entries; 65638d8b4e6SHuang Ying if (si->flags & SWP_BLKDEV) 65738d8b4e6SHuang Ying swap_slot_free_notify = 65838d8b4e6SHuang Ying si->bdev->bd_disk->fops->swap_slot_free_notify; 65938d8b4e6SHuang Ying else 66038d8b4e6SHuang Ying swap_slot_free_notify = NULL; 66138d8b4e6SHuang Ying while (offset <= end) { 66238d8b4e6SHuang Ying frontswap_invalidate_page(si->type, offset); 66338d8b4e6SHuang Ying if (swap_slot_free_notify) 66438d8b4e6SHuang Ying swap_slot_free_notify(si->bdev, offset); 66538d8b4e6SHuang Ying offset++; 66638d8b4e6SHuang Ying } 66738d8b4e6SHuang Ying } 66838d8b4e6SHuang Ying 66936005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si, 67036005baeSTim Chen unsigned char usage, int nr, 67136005baeSTim Chen swp_entry_t slots[]) 6721da177e4SLinus Torvalds { 673235b6217SHuang, Ying struct swap_cluster_info *ci; 674ebebbbe9SHugh Dickins unsigned long offset; 675c60aa176SHugh Dickins unsigned long scan_base; 6767992fde7SHugh Dickins unsigned long last_in_cluster = 0; 677048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 67836005baeSTim Chen int n_ret = 0; 67936005baeSTim Chen 68036005baeSTim Chen if (nr > SWAP_BATCH) 68136005baeSTim Chen nr = SWAP_BATCH; 6821da177e4SLinus Torvalds 6837dfad418SHugh Dickins /* 6847dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 6857dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 6867dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 6877dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 6887dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 6897dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 6907dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 691c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 6921da177e4SLinus Torvalds */ 6937dfad418SHugh Dickins 69452b7efdbSHugh Dickins si->flags += SWP_SCANNING; 695c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 696ebebbbe9SHugh Dickins 697ebc2a1a6SShaohua Li /* SSD algorithm */ 698ebc2a1a6SShaohua Li if (si->cluster_info) { 69936005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 700815c2c54SShaohua Li goto checks; 70136005baeSTim Chen else 70236005baeSTim Chen goto scan; 703815c2c54SShaohua Li } 704815c2c54SShaohua Li 705ebc2a1a6SShaohua Li if (unlikely(!si->cluster_nr--)) { 706ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 707ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 7082a8f9449SShaohua Li goto checks; 7092a8f9449SShaohua Li } 7102a8f9449SShaohua Li 711ec8acf20SShaohua Li spin_unlock(&si->lock); 7127dfad418SHugh Dickins 713c60aa176SHugh Dickins /* 714c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 715c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 71650088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 71750088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 718c60aa176SHugh Dickins */ 719c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 7207dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 7217dfad418SHugh Dickins 7227dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 7237dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 7241da177e4SLinus Torvalds if (si->swap_map[offset]) 7257dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 7267dfad418SHugh Dickins else if (offset == last_in_cluster) { 727ec8acf20SShaohua Li spin_lock(&si->lock); 728ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 729ebebbbe9SHugh Dickins si->cluster_next = offset; 730ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 731ebebbbe9SHugh Dickins goto checks; 7327dfad418SHugh Dickins } 733048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 734048c27fdSHugh Dickins cond_resched(); 735048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 736048c27fdSHugh Dickins } 7377dfad418SHugh Dickins } 738ebebbbe9SHugh Dickins 739c60aa176SHugh Dickins offset = scan_base; 740ec8acf20SShaohua Li spin_lock(&si->lock); 741ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 7427dfad418SHugh Dickins } 7437dfad418SHugh Dickins 744ebebbbe9SHugh Dickins checks: 745ebc2a1a6SShaohua Li if (si->cluster_info) { 74636005baeSTim Chen while (scan_swap_map_ssd_cluster_conflict(si, offset)) { 74736005baeSTim Chen /* take a break if we already got some slots */ 74836005baeSTim Chen if (n_ret) 74936005baeSTim Chen goto done; 75036005baeSTim Chen if (!scan_swap_map_try_ssd_cluster(si, &offset, 75136005baeSTim Chen &scan_base)) 75236005baeSTim Chen goto scan; 75336005baeSTim Chen } 754ebc2a1a6SShaohua Li } 755ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 75652b7efdbSHugh Dickins goto no_page; 7577dfad418SHugh Dickins if (!si->highest_bit) 7587dfad418SHugh Dickins goto no_page; 759ebebbbe9SHugh Dickins if (offset > si->highest_bit) 760c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 761c9e44410SKAMEZAWA Hiroyuki 762235b6217SHuang, Ying ci = lock_cluster(si, offset); 763b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 764b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 765c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 766235b6217SHuang, Ying unlock_cluster(ci); 767ec8acf20SShaohua Li spin_unlock(&si->lock); 768c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 769ec8acf20SShaohua Li spin_lock(&si->lock); 770c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 771c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 772c9e44410SKAMEZAWA Hiroyuki goto checks; 773c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 774c9e44410SKAMEZAWA Hiroyuki } 775c9e44410SKAMEZAWA Hiroyuki 776235b6217SHuang, Ying if (si->swap_map[offset]) { 777235b6217SHuang, Ying unlock_cluster(ci); 77836005baeSTim Chen if (!n_ret) 779ebebbbe9SHugh Dickins goto scan; 78036005baeSTim Chen else 78136005baeSTim Chen goto done; 782235b6217SHuang, Ying } 7832872bb2dSHuang Ying si->swap_map[offset] = usage; 7842872bb2dSHuang Ying inc_cluster_info_page(si, si->cluster_info, offset); 7852872bb2dSHuang Ying unlock_cluster(ci); 786ebebbbe9SHugh Dickins 78738d8b4e6SHuang Ying swap_range_alloc(si, offset, 1); 7881da177e4SLinus Torvalds si->cluster_next = offset + 1; 78936005baeSTim Chen slots[n_ret++] = swp_entry(si->type, offset); 7907992fde7SHugh Dickins 79136005baeSTim Chen /* got enough slots or reach max slots? */ 79236005baeSTim Chen if ((n_ret == nr) || (offset >= si->highest_bit)) 79336005baeSTim Chen goto done; 79436005baeSTim Chen 79536005baeSTim Chen /* search for next available slot */ 79636005baeSTim Chen 79736005baeSTim Chen /* time to take a break? */ 79836005baeSTim Chen if (unlikely(--latency_ration < 0)) { 79936005baeSTim Chen if (n_ret) 80036005baeSTim Chen goto done; 80136005baeSTim Chen spin_unlock(&si->lock); 80236005baeSTim Chen cond_resched(); 80336005baeSTim Chen spin_lock(&si->lock); 80436005baeSTim Chen latency_ration = LATENCY_LIMIT; 80536005baeSTim Chen } 80636005baeSTim Chen 80736005baeSTim Chen /* try to get more slots in cluster */ 80836005baeSTim Chen if (si->cluster_info) { 80936005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 81036005baeSTim Chen goto checks; 81136005baeSTim Chen else 81236005baeSTim Chen goto done; 81336005baeSTim Chen } 81436005baeSTim Chen /* non-ssd case */ 81536005baeSTim Chen ++offset; 81636005baeSTim Chen 81736005baeSTim Chen /* non-ssd case, still more slots in cluster? */ 81836005baeSTim Chen if (si->cluster_nr && !si->swap_map[offset]) { 81936005baeSTim Chen --si->cluster_nr; 82036005baeSTim Chen goto checks; 82136005baeSTim Chen } 82236005baeSTim Chen 82336005baeSTim Chen done: 82436005baeSTim Chen si->flags -= SWP_SCANNING; 82536005baeSTim Chen return n_ret; 8267dfad418SHugh Dickins 827ebebbbe9SHugh Dickins scan: 828ec8acf20SShaohua Li spin_unlock(&si->lock); 8297dfad418SHugh Dickins while (++offset <= si->highest_bit) { 83052b7efdbSHugh Dickins if (!si->swap_map[offset]) { 831ec8acf20SShaohua Li spin_lock(&si->lock); 83252b7efdbSHugh Dickins goto checks; 8337dfad418SHugh Dickins } 834c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 835ec8acf20SShaohua Li spin_lock(&si->lock); 836c9e44410SKAMEZAWA Hiroyuki goto checks; 837c9e44410SKAMEZAWA Hiroyuki } 838048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 839048c27fdSHugh Dickins cond_resched(); 840048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 841048c27fdSHugh Dickins } 84252b7efdbSHugh Dickins } 843c60aa176SHugh Dickins offset = si->lowest_bit; 844a5998061SJamie Liu while (offset < scan_base) { 845c60aa176SHugh Dickins if (!si->swap_map[offset]) { 846ec8acf20SShaohua Li spin_lock(&si->lock); 847ebebbbe9SHugh Dickins goto checks; 848c60aa176SHugh Dickins } 849c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 850ec8acf20SShaohua Li spin_lock(&si->lock); 851c9e44410SKAMEZAWA Hiroyuki goto checks; 852c9e44410SKAMEZAWA Hiroyuki } 853c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 854c60aa176SHugh Dickins cond_resched(); 855c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 856c60aa176SHugh Dickins } 857a5998061SJamie Liu offset++; 858c60aa176SHugh Dickins } 859ec8acf20SShaohua Li spin_lock(&si->lock); 8607dfad418SHugh Dickins 8617dfad418SHugh Dickins no_page: 86252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 86336005baeSTim Chen return n_ret; 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds 86638d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 86738d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 86838d8b4e6SHuang Ying { 86938d8b4e6SHuang Ying unsigned long idx; 87038d8b4e6SHuang Ying struct swap_cluster_info *ci; 87138d8b4e6SHuang Ying unsigned long offset, i; 87238d8b4e6SHuang Ying unsigned char *map; 87338d8b4e6SHuang Ying 87438d8b4e6SHuang Ying if (cluster_list_empty(&si->free_clusters)) 87538d8b4e6SHuang Ying return 0; 87638d8b4e6SHuang Ying 87738d8b4e6SHuang Ying idx = cluster_list_first(&si->free_clusters); 87838d8b4e6SHuang Ying offset = idx * SWAPFILE_CLUSTER; 87938d8b4e6SHuang Ying ci = lock_cluster(si, offset); 88038d8b4e6SHuang Ying alloc_cluster(si, idx); 881e0709829SHuang Ying cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE); 88238d8b4e6SHuang Ying 88338d8b4e6SHuang Ying map = si->swap_map + offset; 88438d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 88538d8b4e6SHuang Ying map[i] = SWAP_HAS_CACHE; 88638d8b4e6SHuang Ying unlock_cluster(ci); 88738d8b4e6SHuang Ying swap_range_alloc(si, offset, SWAPFILE_CLUSTER); 88838d8b4e6SHuang Ying *slot = swp_entry(si->type, offset); 88938d8b4e6SHuang Ying 89038d8b4e6SHuang Ying return 1; 89138d8b4e6SHuang Ying } 89238d8b4e6SHuang Ying 89338d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) 89438d8b4e6SHuang Ying { 89538d8b4e6SHuang Ying unsigned long offset = idx * SWAPFILE_CLUSTER; 89638d8b4e6SHuang Ying struct swap_cluster_info *ci; 89738d8b4e6SHuang Ying 89838d8b4e6SHuang Ying ci = lock_cluster(si, offset); 89938d8b4e6SHuang Ying cluster_set_count_flag(ci, 0, 0); 90038d8b4e6SHuang Ying free_cluster(si, idx); 90138d8b4e6SHuang Ying unlock_cluster(ci); 90238d8b4e6SHuang Ying swap_range_free(si, offset, SWAPFILE_CLUSTER); 90338d8b4e6SHuang Ying } 90438d8b4e6SHuang Ying #else 90538d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 90638d8b4e6SHuang Ying { 90738d8b4e6SHuang Ying VM_WARN_ON_ONCE(1); 90838d8b4e6SHuang Ying return 0; 90938d8b4e6SHuang Ying } 91038d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 91138d8b4e6SHuang Ying 91236005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si, 91336005baeSTim Chen unsigned char usage) 91436005baeSTim Chen { 91536005baeSTim Chen swp_entry_t entry; 91636005baeSTim Chen int n_ret; 91736005baeSTim Chen 91836005baeSTim Chen n_ret = scan_swap_map_slots(si, usage, 1, &entry); 91936005baeSTim Chen 92036005baeSTim Chen if (n_ret) 92136005baeSTim Chen return swp_offset(entry); 92236005baeSTim Chen else 92336005baeSTim Chen return 0; 92436005baeSTim Chen 92536005baeSTim Chen } 92636005baeSTim Chen 92738d8b4e6SHuang Ying int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[]) 9281da177e4SLinus Torvalds { 92938d8b4e6SHuang Ying unsigned long nr_pages = cluster ? SWAPFILE_CLUSTER : 1; 930adfab836SDan Streetman struct swap_info_struct *si, *next; 93136005baeSTim Chen long avail_pgs; 93236005baeSTim Chen int n_ret = 0; 933a2468cc9SAaron Lu int node; 9341da177e4SLinus Torvalds 93538d8b4e6SHuang Ying /* Only single cluster request supported */ 93638d8b4e6SHuang Ying WARN_ON_ONCE(n_goal > 1 && cluster); 93738d8b4e6SHuang Ying 93838d8b4e6SHuang Ying avail_pgs = atomic_long_read(&nr_swap_pages) / nr_pages; 93936005baeSTim Chen if (avail_pgs <= 0) 940fb4f88dcSHugh Dickins goto noswap; 94136005baeSTim Chen 94236005baeSTim Chen if (n_goal > SWAP_BATCH) 94336005baeSTim Chen n_goal = SWAP_BATCH; 94436005baeSTim Chen 94536005baeSTim Chen if (n_goal > avail_pgs) 94636005baeSTim Chen n_goal = avail_pgs; 94736005baeSTim Chen 94838d8b4e6SHuang Ying atomic_long_sub(n_goal * nr_pages, &nr_swap_pages); 9491da177e4SLinus Torvalds 95018ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 95118ab4d4cSDan Streetman 95218ab4d4cSDan Streetman start_over: 953a2468cc9SAaron Lu node = numa_node_id(); 954a2468cc9SAaron Lu plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { 95518ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 956a2468cc9SAaron Lu plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); 95718ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 958ec8acf20SShaohua Li spin_lock(&si->lock); 959adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 96018ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 961a2468cc9SAaron Lu if (plist_node_empty(&si->avail_lists[node])) { 962ec8acf20SShaohua Li spin_unlock(&si->lock); 96318ab4d4cSDan Streetman goto nextsi; 96418ab4d4cSDan Streetman } 96518ab4d4cSDan Streetman WARN(!si->highest_bit, 96618ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 96718ab4d4cSDan Streetman si->type); 96818ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 96918ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 97018ab4d4cSDan Streetman si->type); 971a2468cc9SAaron Lu __del_from_avail_list(si); 97218ab4d4cSDan Streetman spin_unlock(&si->lock); 97318ab4d4cSDan Streetman goto nextsi; 974ec8acf20SShaohua Li } 975f0eea189SHuang Ying if (cluster) { 976f0eea189SHuang Ying if (!(si->flags & SWP_FILE)) 97738d8b4e6SHuang Ying n_ret = swap_alloc_cluster(si, swp_entries); 978f0eea189SHuang Ying } else 97936005baeSTim Chen n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, 98036005baeSTim Chen n_goal, swp_entries); 981ec8acf20SShaohua Li spin_unlock(&si->lock); 98238d8b4e6SHuang Ying if (n_ret || cluster) 98336005baeSTim Chen goto check_out; 98418ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 98518ab4d4cSDan Streetman si->type); 98636005baeSTim Chen 98718ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 98818ab4d4cSDan Streetman nextsi: 989adfab836SDan Streetman /* 990adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 991adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 992adfab836SDan Streetman * callers probably all tried to get a page from the same si 99318ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 99418ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 99518ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 99618ab4d4cSDan Streetman * list may have been modified; so if next is still in the 99736005baeSTim Chen * swap_avail_head list then try it, otherwise start over 99836005baeSTim Chen * if we have not gotten any slots. 999adfab836SDan Streetman */ 1000a2468cc9SAaron Lu if (plist_node_empty(&next->avail_lists[node])) 100118ab4d4cSDan Streetman goto start_over; 1002fb4f88dcSHugh Dickins } 1003fb4f88dcSHugh Dickins 100418ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 100518ab4d4cSDan Streetman 100636005baeSTim Chen check_out: 100736005baeSTim Chen if (n_ret < n_goal) 100838d8b4e6SHuang Ying atomic_long_add((long)(n_goal - n_ret) * nr_pages, 100938d8b4e6SHuang Ying &nr_swap_pages); 1010fb4f88dcSHugh Dickins noswap: 101136005baeSTim Chen return n_ret; 101236005baeSTim Chen } 101336005baeSTim Chen 10142de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 1015910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 1016910321eaSHugh Dickins { 1017910321eaSHugh Dickins struct swap_info_struct *si; 1018910321eaSHugh Dickins pgoff_t offset; 1019910321eaSHugh Dickins 1020910321eaSHugh Dickins si = swap_info[type]; 1021ec8acf20SShaohua Li spin_lock(&si->lock); 1022910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 1023ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 1024910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 1025910321eaSHugh Dickins offset = scan_swap_map(si, 1); 1026910321eaSHugh Dickins if (offset) { 1027ec8acf20SShaohua Li spin_unlock(&si->lock); 1028910321eaSHugh Dickins return swp_entry(type, offset); 1029910321eaSHugh Dickins } 1030ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 1031910321eaSHugh Dickins } 1032ec8acf20SShaohua Li spin_unlock(&si->lock); 1033910321eaSHugh Dickins return (swp_entry_t) {0}; 1034910321eaSHugh Dickins } 1035910321eaSHugh Dickins 1036e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry) 10371da177e4SLinus Torvalds { 10381da177e4SLinus Torvalds struct swap_info_struct *p; 10391da177e4SLinus Torvalds unsigned long offset, type; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds if (!entry.val) 10421da177e4SLinus Torvalds goto out; 10431da177e4SLinus Torvalds type = swp_type(entry); 10441da177e4SLinus Torvalds if (type >= nr_swapfiles) 10451da177e4SLinus Torvalds goto bad_nofile; 1046efa90a98SHugh Dickins p = swap_info[type]; 10471da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 10481da177e4SLinus Torvalds goto bad_device; 10491da177e4SLinus Torvalds offset = swp_offset(entry); 10501da177e4SLinus Torvalds if (offset >= p->max) 10511da177e4SLinus Torvalds goto bad_offset; 10521da177e4SLinus Torvalds return p; 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds bad_offset: 10556a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val); 10561da177e4SLinus Torvalds goto out; 10571da177e4SLinus Torvalds bad_device: 10586a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val); 10591da177e4SLinus Torvalds goto out; 10601da177e4SLinus Torvalds bad_nofile: 10616a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val); 10621da177e4SLinus Torvalds out: 10631da177e4SLinus Torvalds return NULL; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 1066e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 1067e8c26ab6STim Chen { 1068e8c26ab6STim Chen struct swap_info_struct *p; 1069e8c26ab6STim Chen 1070e8c26ab6STim Chen p = __swap_info_get(entry); 1071e8c26ab6STim Chen if (!p) 1072e8c26ab6STim Chen goto out; 1073e8c26ab6STim Chen if (!p->swap_map[swp_offset(entry)]) 1074e8c26ab6STim Chen goto bad_free; 1075e8c26ab6STim Chen return p; 1076e8c26ab6STim Chen 1077e8c26ab6STim Chen bad_free: 1078e8c26ab6STim Chen pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val); 1079e8c26ab6STim Chen goto out; 1080e8c26ab6STim Chen out: 1081e8c26ab6STim Chen return NULL; 1082e8c26ab6STim Chen } 1083e8c26ab6STim Chen 1084235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 10851da177e4SLinus Torvalds { 1086235b6217SHuang, Ying struct swap_info_struct *p; 1087235b6217SHuang, Ying 1088235b6217SHuang, Ying p = _swap_info_get(entry); 1089235b6217SHuang, Ying if (p) 1090235b6217SHuang, Ying spin_lock(&p->lock); 1091235b6217SHuang, Ying return p; 1092235b6217SHuang, Ying } 1093235b6217SHuang, Ying 10947c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, 10957c00bafeSTim Chen struct swap_info_struct *q) 10967c00bafeSTim Chen { 10977c00bafeSTim Chen struct swap_info_struct *p; 10987c00bafeSTim Chen 10997c00bafeSTim Chen p = _swap_info_get(entry); 11007c00bafeSTim Chen 11017c00bafeSTim Chen if (p != q) { 11027c00bafeSTim Chen if (q != NULL) 11037c00bafeSTim Chen spin_unlock(&q->lock); 11047c00bafeSTim Chen if (p != NULL) 11057c00bafeSTim Chen spin_lock(&p->lock); 11067c00bafeSTim Chen } 11077c00bafeSTim Chen return p; 11087c00bafeSTim Chen } 11097c00bafeSTim Chen 11107c00bafeSTim Chen static unsigned char __swap_entry_free(struct swap_info_struct *p, 11117c00bafeSTim Chen swp_entry_t entry, unsigned char usage) 1112235b6217SHuang, Ying { 1113235b6217SHuang, Ying struct swap_cluster_info *ci; 1114253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 11158d69aaeeSHugh Dickins unsigned char count; 11168d69aaeeSHugh Dickins unsigned char has_cache; 1117235b6217SHuang, Ying 11187c00bafeSTim Chen ci = lock_cluster_or_swap_info(p, offset); 11191da177e4SLinus Torvalds 1120355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 1121235b6217SHuang, Ying 1122253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 1123253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 1124253d553bSHugh Dickins 1125253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 1126253d553bSHugh Dickins VM_BUG_ON(!has_cache); 1127253d553bSHugh Dickins has_cache = 0; 1128aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 1129aaa46865SHugh Dickins /* 1130aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 1131aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 1132aaa46865SHugh Dickins */ 1133aaa46865SHugh Dickins count = 0; 1134570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 1135570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 1136570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 1137570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 1138570a335bSHugh Dickins else 1139570a335bSHugh Dickins count = SWAP_MAP_MAX; 1140570a335bSHugh Dickins } else 1141253d553bSHugh Dickins count--; 1142570a335bSHugh Dickins } 1143253d553bSHugh Dickins 1144253d553bSHugh Dickins usage = count | has_cache; 11457c00bafeSTim Chen p->swap_map[offset] = usage ? : SWAP_HAS_CACHE; 1146253d553bSHugh Dickins 11477c00bafeSTim Chen unlock_cluster_or_swap_info(p, ci); 1148235b6217SHuang, Ying 11497c00bafeSTim Chen return usage; 11507c00bafeSTim Chen } 11517c00bafeSTim Chen 11527c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) 11537c00bafeSTim Chen { 11547c00bafeSTim Chen struct swap_cluster_info *ci; 11557c00bafeSTim Chen unsigned long offset = swp_offset(entry); 11567c00bafeSTim Chen unsigned char count; 11577c00bafeSTim Chen 1158235b6217SHuang, Ying ci = lock_cluster(p, offset); 11597c00bafeSTim Chen count = p->swap_map[offset]; 11607c00bafeSTim Chen VM_BUG_ON(count != SWAP_HAS_CACHE); 11617c00bafeSTim Chen p->swap_map[offset] = 0; 11622a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 1163235b6217SHuang, Ying unlock_cluster(ci); 11647c00bafeSTim Chen 116538d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, 1); 116638d8b4e6SHuang Ying swap_range_free(p, offset, 1); 11671da177e4SLinus Torvalds } 1168253d553bSHugh Dickins 11691da177e4SLinus Torvalds /* 11701da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 11711da177e4SLinus Torvalds * is still around or has not been recycled. 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds struct swap_info_struct *p; 11761da177e4SLinus Torvalds 1177235b6217SHuang, Ying p = _swap_info_get(entry); 11787c00bafeSTim Chen if (p) { 11797c00bafeSTim Chen if (!__swap_entry_free(p, entry, 1)) 118067afa38eSTim Chen free_swap_slot(entry); 11817c00bafeSTim Chen } 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds /* 1185cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 1186cb4b86baSKAMEZAWA Hiroyuki */ 118775f6d6d2SMinchan Kim static void swapcache_free(swp_entry_t entry) 1188cb4b86baSKAMEZAWA Hiroyuki { 1189355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 1190355cfa73SKAMEZAWA Hiroyuki 1191235b6217SHuang, Ying p = _swap_info_get(entry); 11927c00bafeSTim Chen if (p) { 11937c00bafeSTim Chen if (!__swap_entry_free(p, entry, SWAP_HAS_CACHE)) 119467afa38eSTim Chen free_swap_slot(entry); 11957c00bafeSTim Chen } 11967c00bafeSTim Chen } 11977c00bafeSTim Chen 119838d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 119975f6d6d2SMinchan Kim static void swapcache_free_cluster(swp_entry_t entry) 120038d8b4e6SHuang Ying { 120138d8b4e6SHuang Ying unsigned long offset = swp_offset(entry); 120238d8b4e6SHuang Ying unsigned long idx = offset / SWAPFILE_CLUSTER; 120338d8b4e6SHuang Ying struct swap_cluster_info *ci; 120438d8b4e6SHuang Ying struct swap_info_struct *si; 120538d8b4e6SHuang Ying unsigned char *map; 1206a3aea839SHuang Ying unsigned int i, free_entries = 0; 1207a3aea839SHuang Ying unsigned char val; 120838d8b4e6SHuang Ying 1209a3aea839SHuang Ying si = _swap_info_get(entry); 121038d8b4e6SHuang Ying if (!si) 121138d8b4e6SHuang Ying return; 121238d8b4e6SHuang Ying 121338d8b4e6SHuang Ying ci = lock_cluster(si, offset); 1214e0709829SHuang Ying VM_BUG_ON(!cluster_is_huge(ci)); 121538d8b4e6SHuang Ying map = si->swap_map + offset; 121638d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1217a3aea839SHuang Ying val = map[i]; 1218a3aea839SHuang Ying VM_BUG_ON(!(val & SWAP_HAS_CACHE)); 1219a3aea839SHuang Ying if (val == SWAP_HAS_CACHE) 1220a3aea839SHuang Ying free_entries++; 122138d8b4e6SHuang Ying } 1222a3aea839SHuang Ying if (!free_entries) { 1223a3aea839SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 1224a3aea839SHuang Ying map[i] &= ~SWAP_HAS_CACHE; 1225a3aea839SHuang Ying } 1226e0709829SHuang Ying cluster_clear_huge(ci); 1227a3aea839SHuang Ying unlock_cluster(ci); 1228a3aea839SHuang Ying if (free_entries == SWAPFILE_CLUSTER) { 1229a3aea839SHuang Ying spin_lock(&si->lock); 1230a3aea839SHuang Ying ci = lock_cluster(si, offset); 1231a3aea839SHuang Ying memset(map, 0, SWAPFILE_CLUSTER); 123238d8b4e6SHuang Ying unlock_cluster(ci); 123338d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER); 123438d8b4e6SHuang Ying swap_free_cluster(si, idx); 123538d8b4e6SHuang Ying spin_unlock(&si->lock); 1236a3aea839SHuang Ying } else if (free_entries) { 1237a3aea839SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++, entry.val++) { 1238a3aea839SHuang Ying if (!__swap_entry_free(si, entry, SWAP_HAS_CACHE)) 1239a3aea839SHuang Ying free_swap_slot(entry); 1240a3aea839SHuang Ying } 1241a3aea839SHuang Ying } 124238d8b4e6SHuang Ying } 124359807685SHuang Ying 124459807685SHuang Ying int split_swap_cluster(swp_entry_t entry) 124559807685SHuang Ying { 124659807685SHuang Ying struct swap_info_struct *si; 124759807685SHuang Ying struct swap_cluster_info *ci; 124859807685SHuang Ying unsigned long offset = swp_offset(entry); 124959807685SHuang Ying 125059807685SHuang Ying si = _swap_info_get(entry); 125159807685SHuang Ying if (!si) 125259807685SHuang Ying return -EBUSY; 125359807685SHuang Ying ci = lock_cluster(si, offset); 125459807685SHuang Ying cluster_clear_huge(ci); 125559807685SHuang Ying unlock_cluster(ci); 125659807685SHuang Ying return 0; 125759807685SHuang Ying } 125875f6d6d2SMinchan Kim #else 125975f6d6d2SMinchan Kim static inline void swapcache_free_cluster(swp_entry_t entry) 126075f6d6d2SMinchan Kim { 126175f6d6d2SMinchan Kim } 126238d8b4e6SHuang Ying #endif /* CONFIG_THP_SWAP */ 126338d8b4e6SHuang Ying 126475f6d6d2SMinchan Kim void put_swap_page(struct page *page, swp_entry_t entry) 126575f6d6d2SMinchan Kim { 126675f6d6d2SMinchan Kim if (!PageTransHuge(page)) 126775f6d6d2SMinchan Kim swapcache_free(entry); 126875f6d6d2SMinchan Kim else 126975f6d6d2SMinchan Kim swapcache_free_cluster(entry); 127075f6d6d2SMinchan Kim } 127175f6d6d2SMinchan Kim 1272155b5f88SHuang Ying static int swp_entry_cmp(const void *ent1, const void *ent2) 1273155b5f88SHuang Ying { 1274155b5f88SHuang Ying const swp_entry_t *e1 = ent1, *e2 = ent2; 1275155b5f88SHuang Ying 1276155b5f88SHuang Ying return (int)swp_type(*e1) - (int)swp_type(*e2); 1277155b5f88SHuang Ying } 1278155b5f88SHuang Ying 12797c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n) 12807c00bafeSTim Chen { 12817c00bafeSTim Chen struct swap_info_struct *p, *prev; 12827c00bafeSTim Chen int i; 12837c00bafeSTim Chen 12847c00bafeSTim Chen if (n <= 0) 12857c00bafeSTim Chen return; 12867c00bafeSTim Chen 12877c00bafeSTim Chen prev = NULL; 12887c00bafeSTim Chen p = NULL; 1289155b5f88SHuang Ying 1290155b5f88SHuang Ying /* 1291155b5f88SHuang Ying * Sort swap entries by swap device, so each lock is only taken once. 1292155b5f88SHuang Ying * nr_swapfiles isn't absolutely correct, but the overhead of sort() is 1293155b5f88SHuang Ying * so low that it isn't necessary to optimize further. 1294155b5f88SHuang Ying */ 1295155b5f88SHuang Ying if (nr_swapfiles > 1) 1296155b5f88SHuang Ying sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); 12977c00bafeSTim Chen for (i = 0; i < n; ++i) { 12987c00bafeSTim Chen p = swap_info_get_cont(entries[i], prev); 1299235b6217SHuang, Ying if (p) 13007c00bafeSTim Chen swap_entry_free(p, entries[i]); 13017c00bafeSTim Chen prev = p; 13027c00bafeSTim Chen } 13037c00bafeSTim Chen if (p) 13047c00bafeSTim Chen spin_unlock(&p->lock); 1305cb4b86baSKAMEZAWA Hiroyuki } 1306cb4b86baSKAMEZAWA Hiroyuki 1307cb4b86baSKAMEZAWA Hiroyuki /* 1308c475a8abSHugh Dickins * How many references to page are currently swapped out? 1309570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 1310570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 13111da177e4SLinus Torvalds */ 1312bde05d1cSHugh Dickins int page_swapcount(struct page *page) 13131da177e4SLinus Torvalds { 1314c475a8abSHugh Dickins int count = 0; 13151da177e4SLinus Torvalds struct swap_info_struct *p; 1316235b6217SHuang, Ying struct swap_cluster_info *ci; 13171da177e4SLinus Torvalds swp_entry_t entry; 1318235b6217SHuang, Ying unsigned long offset; 13191da177e4SLinus Torvalds 13204c21e2f2SHugh Dickins entry.val = page_private(page); 1321235b6217SHuang, Ying p = _swap_info_get(entry); 13221da177e4SLinus Torvalds if (p) { 1323235b6217SHuang, Ying offset = swp_offset(entry); 1324235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1325235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 1326235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 13271da177e4SLinus Torvalds } 1328c475a8abSHugh Dickins return count; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds 1331322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) 1332322b8afeSHuang Ying { 1333322b8afeSHuang Ying int count = 0; 1334322b8afeSHuang Ying pgoff_t offset = swp_offset(entry); 1335322b8afeSHuang Ying struct swap_cluster_info *ci; 1336322b8afeSHuang Ying 1337322b8afeSHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1338322b8afeSHuang Ying count = swap_count(si->swap_map[offset]); 1339322b8afeSHuang Ying unlock_cluster_or_swap_info(si, ci); 1340322b8afeSHuang Ying return count; 1341322b8afeSHuang Ying } 1342322b8afeSHuang Ying 13431da177e4SLinus Torvalds /* 13448334b962SMinchan Kim * How many references to @entry are currently swapped out? 1345e8c26ab6STim Chen * This does not give an exact answer when swap count is continued, 1346e8c26ab6STim Chen * but does include the high COUNT_CONTINUED flag to allow for that. 1347e8c26ab6STim Chen */ 1348e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry) 1349e8c26ab6STim Chen { 1350e8c26ab6STim Chen int count = 0; 1351e8c26ab6STim Chen struct swap_info_struct *si; 1352e8c26ab6STim Chen 1353e8c26ab6STim Chen si = __swap_info_get(entry); 1354322b8afeSHuang Ying if (si) 1355322b8afeSHuang Ying count = swap_swapcount(si, entry); 1356e8c26ab6STim Chen return count; 1357e8c26ab6STim Chen } 1358e8c26ab6STim Chen 1359e8c26ab6STim Chen /* 1360e8c26ab6STim Chen * How many references to @entry are currently swapped out? 13618334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 13628334b962SMinchan Kim */ 13638334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 13648334b962SMinchan Kim { 13658334b962SMinchan Kim int count, tmp_count, n; 13668334b962SMinchan Kim struct swap_info_struct *p; 1367235b6217SHuang, Ying struct swap_cluster_info *ci; 13688334b962SMinchan Kim struct page *page; 13698334b962SMinchan Kim pgoff_t offset; 13708334b962SMinchan Kim unsigned char *map; 13718334b962SMinchan Kim 1372235b6217SHuang, Ying p = _swap_info_get(entry); 13738334b962SMinchan Kim if (!p) 13748334b962SMinchan Kim return 0; 13758334b962SMinchan Kim 1376235b6217SHuang, Ying offset = swp_offset(entry); 1377235b6217SHuang, Ying 1378235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1379235b6217SHuang, Ying 1380235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 13818334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 13828334b962SMinchan Kim goto out; 13838334b962SMinchan Kim 13848334b962SMinchan Kim count &= ~COUNT_CONTINUED; 13858334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 13868334b962SMinchan Kim 13878334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 13888334b962SMinchan Kim offset &= ~PAGE_MASK; 13898334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 13908334b962SMinchan Kim 13918334b962SMinchan Kim do { 1392a8ae4991SGeliang Tang page = list_next_entry(page, lru); 13938334b962SMinchan Kim map = kmap_atomic(page); 13948334b962SMinchan Kim tmp_count = map[offset]; 13958334b962SMinchan Kim kunmap_atomic(map); 13968334b962SMinchan Kim 13978334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 13988334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 13998334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 14008334b962SMinchan Kim out: 1401235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 14028334b962SMinchan Kim return count; 14038334b962SMinchan Kim } 14048334b962SMinchan Kim 1405e0709829SHuang Ying #ifdef CONFIG_THP_SWAP 1406e0709829SHuang Ying static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, 1407e0709829SHuang Ying swp_entry_t entry) 1408e0709829SHuang Ying { 1409e0709829SHuang Ying struct swap_cluster_info *ci; 1410e0709829SHuang Ying unsigned char *map = si->swap_map; 1411e0709829SHuang Ying unsigned long roffset = swp_offset(entry); 1412e0709829SHuang Ying unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER); 1413e0709829SHuang Ying int i; 1414e0709829SHuang Ying bool ret = false; 1415e0709829SHuang Ying 1416e0709829SHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1417e0709829SHuang Ying if (!ci || !cluster_is_huge(ci)) { 1418e0709829SHuang Ying if (map[roffset] != SWAP_HAS_CACHE) 1419e0709829SHuang Ying ret = true; 1420e0709829SHuang Ying goto unlock_out; 1421e0709829SHuang Ying } 1422e0709829SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1423e0709829SHuang Ying if (map[offset + i] != SWAP_HAS_CACHE) { 1424e0709829SHuang Ying ret = true; 1425e0709829SHuang Ying break; 1426e0709829SHuang Ying } 1427e0709829SHuang Ying } 1428e0709829SHuang Ying unlock_out: 1429e0709829SHuang Ying unlock_cluster_or_swap_info(si, ci); 1430e0709829SHuang Ying return ret; 1431e0709829SHuang Ying } 1432e0709829SHuang Ying 1433e0709829SHuang Ying static bool page_swapped(struct page *page) 1434e0709829SHuang Ying { 1435e0709829SHuang Ying swp_entry_t entry; 1436e0709829SHuang Ying struct swap_info_struct *si; 1437e0709829SHuang Ying 1438e0709829SHuang Ying if (likely(!PageTransCompound(page))) 1439e0709829SHuang Ying return page_swapcount(page) != 0; 1440e0709829SHuang Ying 1441e0709829SHuang Ying page = compound_head(page); 1442e0709829SHuang Ying entry.val = page_private(page); 1443e0709829SHuang Ying si = _swap_info_get(entry); 1444e0709829SHuang Ying if (si) 1445e0709829SHuang Ying return swap_page_trans_huge_swapped(si, entry); 1446e0709829SHuang Ying return false; 1447e0709829SHuang Ying } 1448ba3c4ce6SHuang Ying 1449ba3c4ce6SHuang Ying static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount, 1450ba3c4ce6SHuang Ying int *total_swapcount) 1451ba3c4ce6SHuang Ying { 1452ba3c4ce6SHuang Ying int i, map_swapcount, _total_mapcount, _total_swapcount; 1453ba3c4ce6SHuang Ying unsigned long offset = 0; 1454ba3c4ce6SHuang Ying struct swap_info_struct *si; 1455ba3c4ce6SHuang Ying struct swap_cluster_info *ci = NULL; 1456ba3c4ce6SHuang Ying unsigned char *map = NULL; 1457ba3c4ce6SHuang Ying int mapcount, swapcount = 0; 1458ba3c4ce6SHuang Ying 1459ba3c4ce6SHuang Ying /* hugetlbfs shouldn't call it */ 1460ba3c4ce6SHuang Ying VM_BUG_ON_PAGE(PageHuge(page), page); 1461ba3c4ce6SHuang Ying 1462ba3c4ce6SHuang Ying if (likely(!PageTransCompound(page))) { 1463ba3c4ce6SHuang Ying mapcount = atomic_read(&page->_mapcount) + 1; 1464ba3c4ce6SHuang Ying if (total_mapcount) 1465ba3c4ce6SHuang Ying *total_mapcount = mapcount; 1466ba3c4ce6SHuang Ying if (PageSwapCache(page)) 1467ba3c4ce6SHuang Ying swapcount = page_swapcount(page); 1468ba3c4ce6SHuang Ying if (total_swapcount) 1469ba3c4ce6SHuang Ying *total_swapcount = swapcount; 1470ba3c4ce6SHuang Ying return mapcount + swapcount; 1471ba3c4ce6SHuang Ying } 1472ba3c4ce6SHuang Ying 1473ba3c4ce6SHuang Ying page = compound_head(page); 1474ba3c4ce6SHuang Ying 1475ba3c4ce6SHuang Ying _total_mapcount = _total_swapcount = map_swapcount = 0; 1476ba3c4ce6SHuang Ying if (PageSwapCache(page)) { 1477ba3c4ce6SHuang Ying swp_entry_t entry; 1478ba3c4ce6SHuang Ying 1479ba3c4ce6SHuang Ying entry.val = page_private(page); 1480ba3c4ce6SHuang Ying si = _swap_info_get(entry); 1481ba3c4ce6SHuang Ying if (si) { 1482ba3c4ce6SHuang Ying map = si->swap_map; 1483ba3c4ce6SHuang Ying offset = swp_offset(entry); 1484ba3c4ce6SHuang Ying } 1485ba3c4ce6SHuang Ying } 1486ba3c4ce6SHuang Ying if (map) 1487ba3c4ce6SHuang Ying ci = lock_cluster(si, offset); 1488ba3c4ce6SHuang Ying for (i = 0; i < HPAGE_PMD_NR; i++) { 1489ba3c4ce6SHuang Ying mapcount = atomic_read(&page[i]._mapcount) + 1; 1490ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1491ba3c4ce6SHuang Ying if (map) { 1492ba3c4ce6SHuang Ying swapcount = swap_count(map[offset + i]); 1493ba3c4ce6SHuang Ying _total_swapcount += swapcount; 1494ba3c4ce6SHuang Ying } 1495ba3c4ce6SHuang Ying map_swapcount = max(map_swapcount, mapcount + swapcount); 1496ba3c4ce6SHuang Ying } 1497ba3c4ce6SHuang Ying unlock_cluster(ci); 1498ba3c4ce6SHuang Ying if (PageDoubleMap(page)) { 1499ba3c4ce6SHuang Ying map_swapcount -= 1; 1500ba3c4ce6SHuang Ying _total_mapcount -= HPAGE_PMD_NR; 1501ba3c4ce6SHuang Ying } 1502ba3c4ce6SHuang Ying mapcount = compound_mapcount(page); 1503ba3c4ce6SHuang Ying map_swapcount += mapcount; 1504ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1505ba3c4ce6SHuang Ying if (total_mapcount) 1506ba3c4ce6SHuang Ying *total_mapcount = _total_mapcount; 1507ba3c4ce6SHuang Ying if (total_swapcount) 1508ba3c4ce6SHuang Ying *total_swapcount = _total_swapcount; 1509ba3c4ce6SHuang Ying 1510ba3c4ce6SHuang Ying return map_swapcount; 1511ba3c4ce6SHuang Ying } 1512e0709829SHuang Ying #else 1513e0709829SHuang Ying #define swap_page_trans_huge_swapped(si, entry) swap_swapcount(si, entry) 1514e0709829SHuang Ying #define page_swapped(page) (page_swapcount(page) != 0) 1515ba3c4ce6SHuang Ying 1516ba3c4ce6SHuang Ying static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount, 1517ba3c4ce6SHuang Ying int *total_swapcount) 1518ba3c4ce6SHuang Ying { 1519ba3c4ce6SHuang Ying int mapcount, swapcount = 0; 1520ba3c4ce6SHuang Ying 1521ba3c4ce6SHuang Ying /* hugetlbfs shouldn't call it */ 1522ba3c4ce6SHuang Ying VM_BUG_ON_PAGE(PageHuge(page), page); 1523ba3c4ce6SHuang Ying 1524ba3c4ce6SHuang Ying mapcount = page_trans_huge_mapcount(page, total_mapcount); 1525ba3c4ce6SHuang Ying if (PageSwapCache(page)) 1526ba3c4ce6SHuang Ying swapcount = page_swapcount(page); 1527ba3c4ce6SHuang Ying if (total_swapcount) 1528ba3c4ce6SHuang Ying *total_swapcount = swapcount; 1529ba3c4ce6SHuang Ying return mapcount + swapcount; 1530ba3c4ce6SHuang Ying } 1531e0709829SHuang Ying #endif 1532e0709829SHuang Ying 15338334b962SMinchan Kim /* 15347b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 15357b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 15367b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 15377b1fe597SHugh Dickins * later would only waste time away from clustering. 15386d0a07edSAndrea Arcangeli * 1539ba3c4ce6SHuang Ying * NOTE: total_map_swapcount should not be relied upon by the caller if 15406d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 15416d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 15421da177e4SLinus Torvalds */ 1543ba3c4ce6SHuang Ying bool reuse_swap_page(struct page *page, int *total_map_swapcount) 15441da177e4SLinus Torvalds { 1545ba3c4ce6SHuang Ying int count, total_mapcount, total_swapcount; 15461da177e4SLinus Torvalds 1547309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 15485ad64688SHugh Dickins if (unlikely(PageKsm(page))) 15496d0a07edSAndrea Arcangeli return false; 1550ba3c4ce6SHuang Ying count = page_trans_huge_map_swapcount(page, &total_mapcount, 1551ba3c4ce6SHuang Ying &total_swapcount); 1552ba3c4ce6SHuang Ying if (total_map_swapcount) 1553ba3c4ce6SHuang Ying *total_map_swapcount = total_mapcount + total_swapcount; 1554ba3c4ce6SHuang Ying if (count == 1 && PageSwapCache(page) && 1555ba3c4ce6SHuang Ying (likely(!PageTransCompound(page)) || 1556ba3c4ce6SHuang Ying /* The remaining swap count will be freed soon */ 1557ba3c4ce6SHuang Ying total_swapcount == page_swapcount(page))) { 1558f0571429SMinchan Kim if (!PageWriteback(page)) { 1559ba3c4ce6SHuang Ying page = compound_head(page); 15607b1fe597SHugh Dickins delete_from_swap_cache(page); 15617b1fe597SHugh Dickins SetPageDirty(page); 1562f0571429SMinchan Kim } else { 1563f0571429SMinchan Kim swp_entry_t entry; 1564f0571429SMinchan Kim struct swap_info_struct *p; 1565f0571429SMinchan Kim 1566f0571429SMinchan Kim entry.val = page_private(page); 1567f0571429SMinchan Kim p = swap_info_get(entry); 1568f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1569f0571429SMinchan Kim spin_unlock(&p->lock); 1570f0571429SMinchan Kim return false; 1571f0571429SMinchan Kim } 1572f0571429SMinchan Kim spin_unlock(&p->lock); 15737b1fe597SHugh Dickins } 15747b1fe597SHugh Dickins } 1575ba3c4ce6SHuang Ying 15765ad64688SHugh Dickins return count <= 1; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds /* 1580a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1581a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 15821da177e4SLinus Torvalds */ 1583a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 15841da177e4SLinus Torvalds { 1585309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds if (!PageSwapCache(page)) 15881da177e4SLinus Torvalds return 0; 15891da177e4SLinus Torvalds if (PageWriteback(page)) 15901da177e4SLinus Torvalds return 0; 1591e0709829SHuang Ying if (page_swapped(page)) 15921da177e4SLinus Torvalds return 0; 15931da177e4SLinus Torvalds 1594b73d7fceSHugh Dickins /* 1595b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1596b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1597b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1598b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1599b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1600b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1601b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1602b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1603b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1604b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1605b73d7fceSHugh Dickins * 16062de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1607f90ac398SMel Gorman * to disk so check that here. 1608b73d7fceSHugh Dickins */ 1609f90ac398SMel Gorman if (pm_suspended_storage()) 1610b73d7fceSHugh Dickins return 0; 1611b73d7fceSHugh Dickins 1612e0709829SHuang Ying page = compound_head(page); 1613a2c43eedSHugh Dickins delete_from_swap_cache(page); 16141da177e4SLinus Torvalds SetPageDirty(page); 1615a2c43eedSHugh Dickins return 1; 161668a22394SRik van Riel } 161768a22394SRik van Riel 161868a22394SRik van Riel /* 16191da177e4SLinus Torvalds * Free the swap entry like above, but also try to 16201da177e4SLinus Torvalds * free the page cache entry if it is the last user. 16211da177e4SLinus Torvalds */ 16222509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds struct swap_info_struct *p; 16251da177e4SLinus Torvalds struct page *page = NULL; 16267c00bafeSTim Chen unsigned char count; 16271da177e4SLinus Torvalds 1628a7420aa5SAndi Kleen if (non_swap_entry(entry)) 16292509ef26SHugh Dickins return 1; 16300697212aSChristoph Lameter 16317c00bafeSTim Chen p = _swap_info_get(entry); 16321da177e4SLinus Torvalds if (p) { 16337c00bafeSTim Chen count = __swap_entry_free(p, entry, 1); 1634e0709829SHuang Ying if (count == SWAP_HAS_CACHE && 1635e0709829SHuang Ying !swap_page_trans_huge_swapped(p, entry)) { 163633806f06SShaohua Li page = find_get_page(swap_address_space(entry), 1637f6ab1f7fSHuang Ying swp_offset(entry)); 16388413ac9dSNick Piggin if (page && !trylock_page(page)) { 163909cbfeafSKirill A. Shutemov put_page(page); 164093fac704SNick Piggin page = NULL; 164193fac704SNick Piggin } 16427c00bafeSTim Chen } else if (!count) 164367afa38eSTim Chen free_swap_slot(entry); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds if (page) { 1646a2c43eedSHugh Dickins /* 1647a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 1648a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 1649a2c43eedSHugh Dickins */ 165093fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 1651322b8afeSHuang Ying (!page_mapped(page) || mem_cgroup_swap_full(page)) && 1652e0709829SHuang Ying !swap_page_trans_huge_swapped(p, entry)) { 1653e0709829SHuang Ying page = compound_head(page); 16541da177e4SLinus Torvalds delete_from_swap_cache(page); 16551da177e4SLinus Torvalds SetPageDirty(page); 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds unlock_page(page); 165809cbfeafSKirill A. Shutemov put_page(page); 16591da177e4SLinus Torvalds } 16602509ef26SHugh Dickins return p != NULL; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 1663b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1664f577eb30SRafael J. Wysocki /* 1665915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1666f577eb30SRafael J. Wysocki * 1667915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1668915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1669915bae9eSRafael J. Wysocki * 1670915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1671f577eb30SRafael J. Wysocki */ 16727bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 1673f577eb30SRafael J. Wysocki { 1674915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 1675efa90a98SHugh Dickins int type; 1676f577eb30SRafael J. Wysocki 1677915bae9eSRafael J. Wysocki if (device) 1678915bae9eSRafael J. Wysocki bdev = bdget(device); 1679915bae9eSRafael J. Wysocki 1680f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1681efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1682efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1683f577eb30SRafael J. Wysocki 1684915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1685f577eb30SRafael J. Wysocki continue; 1686b6b5bce3SRafael J. Wysocki 1687915bae9eSRafael J. Wysocki if (!bdev) { 16887bf23687SRafael J. Wysocki if (bdev_p) 1689dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 16907bf23687SRafael J. Wysocki 16916e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1692efa90a98SHugh Dickins return type; 16936e1819d6SRafael J. Wysocki } 1694915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 16959625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 1696915bae9eSRafael J. Wysocki 1697915bae9eSRafael J. Wysocki if (se->start_block == offset) { 16987bf23687SRafael J. Wysocki if (bdev_p) 1699dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 17007bf23687SRafael J. Wysocki 1701f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1702915bae9eSRafael J. Wysocki bdput(bdev); 1703efa90a98SHugh Dickins return type; 1704f577eb30SRafael J. Wysocki } 1705f577eb30SRafael J. Wysocki } 1706915bae9eSRafael J. Wysocki } 1707f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1708915bae9eSRafael J. Wysocki if (bdev) 1709915bae9eSRafael J. Wysocki bdput(bdev); 1710915bae9eSRafael J. Wysocki 1711f577eb30SRafael J. Wysocki return -ENODEV; 1712f577eb30SRafael J. Wysocki } 1713f577eb30SRafael J. Wysocki 1714f577eb30SRafael J. Wysocki /* 171573c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 171673c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 171773c34b6aSHugh Dickins */ 171873c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 171973c34b6aSHugh Dickins { 172073c34b6aSHugh Dickins struct block_device *bdev; 172173c34b6aSHugh Dickins 172273c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 172373c34b6aSHugh Dickins return 0; 172473c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 172573c34b6aSHugh Dickins return 0; 1726d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 172773c34b6aSHugh Dickins } 172873c34b6aSHugh Dickins 172973c34b6aSHugh Dickins /* 1730f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1731f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1732f577eb30SRafael J. Wysocki * 1733f577eb30SRafael J. Wysocki * This is needed for software suspend 1734f577eb30SRafael J. Wysocki */ 1735f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1736f577eb30SRafael J. Wysocki { 1737f577eb30SRafael J. Wysocki unsigned int n = 0; 1738f577eb30SRafael J. Wysocki 1739f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1740efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1741efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1742efa90a98SHugh Dickins 1743ec8acf20SShaohua Li spin_lock(&sis->lock); 1744efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1745efa90a98SHugh Dickins n = sis->pages; 1746f577eb30SRafael J. Wysocki if (free) 1747efa90a98SHugh Dickins n -= sis->inuse_pages; 1748efa90a98SHugh Dickins } 1749ec8acf20SShaohua Li spin_unlock(&sis->lock); 1750f577eb30SRafael J. Wysocki } 1751f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1752f577eb30SRafael J. Wysocki return n; 1753f577eb30SRafael J. Wysocki } 175473c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1755f577eb30SRafael J. Wysocki 17569f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1757179ef71cSCyrill Gorcunov { 17589f8bdb3fSHugh Dickins return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte); 1759179ef71cSCyrill Gorcunov } 1760179ef71cSCyrill Gorcunov 17611da177e4SLinus Torvalds /* 176272866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 176372866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 176472866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 17651da177e4SLinus Torvalds */ 1766044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 17671da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 17681da177e4SLinus Torvalds { 17699e16b7fbSHugh Dickins struct page *swapcache; 177072835c86SJohannes Weiner struct mem_cgroup *memcg; 1771044d66c1SHugh Dickins spinlock_t *ptl; 1772044d66c1SHugh Dickins pte_t *pte; 1773044d66c1SHugh Dickins int ret = 1; 1774044d66c1SHugh Dickins 17759e16b7fbSHugh Dickins swapcache = page; 17769e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 17779e16b7fbSHugh Dickins if (unlikely(!page)) 17789e16b7fbSHugh Dickins return -ENOMEM; 17799e16b7fbSHugh Dickins 1780f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 1781f627c2f5SKirill A. Shutemov &memcg, false)) { 1782044d66c1SHugh Dickins ret = -ENOMEM; 178385d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 178485d9fc89SKAMEZAWA Hiroyuki } 1785044d66c1SHugh Dickins 1786044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 17879f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1788f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1789044d66c1SHugh Dickins ret = 0; 1790044d66c1SHugh Dickins goto out; 1791044d66c1SHugh Dickins } 17928a9f3ccdSBalbir Singh 1793b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1794d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 17951da177e4SLinus Torvalds get_page(page); 17961da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 17971da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 179800501b53SJohannes Weiner if (page == swapcache) { 1799d281ee61SKirill A. Shutemov page_add_anon_rmap(page, vma, addr, false); 1800f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 180100501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1802d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, addr, false); 1803f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 180400501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 180500501b53SJohannes Weiner } 18061da177e4SLinus Torvalds swap_free(entry); 18071da177e4SLinus Torvalds /* 18081da177e4SLinus Torvalds * Move the page to the active list so it is not 18091da177e4SLinus Torvalds * immediately swapped out again after swapon. 18101da177e4SLinus Torvalds */ 18111da177e4SLinus Torvalds activate_page(page); 1812044d66c1SHugh Dickins out: 1813044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 181485d9fc89SKAMEZAWA Hiroyuki out_nolock: 18159e16b7fbSHugh Dickins if (page != swapcache) { 18169e16b7fbSHugh Dickins unlock_page(page); 18179e16b7fbSHugh Dickins put_page(page); 18189e16b7fbSHugh Dickins } 1819044d66c1SHugh Dickins return ret; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 18221da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 18231da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18241da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 18251da177e4SLinus Torvalds { 18261da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1827705e87c0SHugh Dickins pte_t *pte; 18288a9f3ccdSBalbir Singh int ret = 0; 18291da177e4SLinus Torvalds 1830044d66c1SHugh Dickins /* 1831044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1832044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1833044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1834044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1835044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1836044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 18372de1a7e4SSeth Jennings * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1838044d66c1SHugh Dickins */ 1839044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 18401da177e4SLinus Torvalds do { 18411da177e4SLinus Torvalds /* 18421da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 18431da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 18441da177e4SLinus Torvalds */ 18459f8bdb3fSHugh Dickins if (unlikely(pte_same_as_swp(*pte, swp_pte))) { 1846044d66c1SHugh Dickins pte_unmap(pte); 1847044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1848044d66c1SHugh Dickins if (ret) 1849044d66c1SHugh Dickins goto out; 1850044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1853044d66c1SHugh Dickins pte_unmap(pte - 1); 1854044d66c1SHugh Dickins out: 18558a9f3ccdSBalbir Singh return ret; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 18591da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18601da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 18611da177e4SLinus Torvalds { 18621da177e4SLinus Torvalds pmd_t *pmd; 18631da177e4SLinus Torvalds unsigned long next; 18648a9f3ccdSBalbir Singh int ret; 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 18671da177e4SLinus Torvalds do { 1868dc644a07SHugh Dickins cond_resched(); 18691da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 18701a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 18711da177e4SLinus Torvalds continue; 18728a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 18738a9f3ccdSBalbir Singh if (ret) 18748a9f3ccdSBalbir Singh return ret; 18751da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 18761da177e4SLinus Torvalds return 0; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 1879c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d, 18801da177e4SLinus Torvalds unsigned long addr, unsigned long end, 18811da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 18821da177e4SLinus Torvalds { 18831da177e4SLinus Torvalds pud_t *pud; 18841da177e4SLinus Torvalds unsigned long next; 18858a9f3ccdSBalbir Singh int ret; 18861da177e4SLinus Torvalds 1887c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 18881da177e4SLinus Torvalds do { 18891da177e4SLinus Torvalds next = pud_addr_end(addr, end); 18901da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 18911da177e4SLinus Torvalds continue; 18928a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 18938a9f3ccdSBalbir Singh if (ret) 18948a9f3ccdSBalbir Singh return ret; 18951da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 18961da177e4SLinus Torvalds return 0; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 1899c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd, 1900c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 1901c2febafcSKirill A. Shutemov swp_entry_t entry, struct page *page) 1902c2febafcSKirill A. Shutemov { 1903c2febafcSKirill A. Shutemov p4d_t *p4d; 1904c2febafcSKirill A. Shutemov unsigned long next; 1905c2febafcSKirill A. Shutemov int ret; 1906c2febafcSKirill A. Shutemov 1907c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 1908c2febafcSKirill A. Shutemov do { 1909c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 1910c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 1911c2febafcSKirill A. Shutemov continue; 1912c2febafcSKirill A. Shutemov ret = unuse_pud_range(vma, p4d, addr, next, entry, page); 1913c2febafcSKirill A. Shutemov if (ret) 1914c2febafcSKirill A. Shutemov return ret; 1915c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 1916c2febafcSKirill A. Shutemov return 0; 1917c2febafcSKirill A. Shutemov } 1918c2febafcSKirill A. Shutemov 19191da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 19201da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 19211da177e4SLinus Torvalds { 19221da177e4SLinus Torvalds pgd_t *pgd; 19231da177e4SLinus Torvalds unsigned long addr, end, next; 19248a9f3ccdSBalbir Singh int ret; 19251da177e4SLinus Torvalds 19263ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 19271da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 19281da177e4SLinus Torvalds if (addr == -EFAULT) 19291da177e4SLinus Torvalds return 0; 19301da177e4SLinus Torvalds else 19311da177e4SLinus Torvalds end = addr + PAGE_SIZE; 19321da177e4SLinus Torvalds } else { 19331da177e4SLinus Torvalds addr = vma->vm_start; 19341da177e4SLinus Torvalds end = vma->vm_end; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 19381da177e4SLinus Torvalds do { 19391da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 19401da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 19411da177e4SLinus Torvalds continue; 1942c2febafcSKirill A. Shutemov ret = unuse_p4d_range(vma, pgd, addr, next, entry, page); 19438a9f3ccdSBalbir Singh if (ret) 19448a9f3ccdSBalbir Singh return ret; 19451da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 19461da177e4SLinus Torvalds return 0; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 19501da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 19511da177e4SLinus Torvalds { 19521da177e4SLinus Torvalds struct vm_area_struct *vma; 19538a9f3ccdSBalbir Singh int ret = 0; 19541da177e4SLinus Torvalds 19551da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 19561da177e4SLinus Torvalds /* 19577d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 19587d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 19591da177e4SLinus Torvalds */ 1960c475a8abSHugh Dickins activate_page(page); 19611da177e4SLinus Torvalds unlock_page(page); 19621da177e4SLinus Torvalds down_read(&mm->mmap_sem); 19631da177e4SLinus Torvalds lock_page(page); 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 19668a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 19671da177e4SLinus Torvalds break; 1968dc644a07SHugh Dickins cond_resched(); 19691da177e4SLinus Torvalds } 19701da177e4SLinus Torvalds up_read(&mm->mmap_sem); 19718a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 19741da177e4SLinus Torvalds /* 197538b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 197638b5faf4SDan Magenheimer * from current position to next entry still in use. 19771da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 19781da177e4SLinus Torvalds */ 19796eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 198038b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 19811da177e4SLinus Torvalds { 19826eb396dcSHugh Dickins unsigned int max = si->max; 19836eb396dcSHugh Dickins unsigned int i = prev; 19848d69aaeeSHugh Dickins unsigned char count; 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds /* 19875d337b91SHugh Dickins * No need for swap_lock here: we're just looking 19881da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 19891da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 19905d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 19911da177e4SLinus Torvalds */ 19921da177e4SLinus Torvalds for (;;) { 19931da177e4SLinus Torvalds if (++i >= max) { 19941da177e4SLinus Torvalds if (!prev) { 19951da177e4SLinus Torvalds i = 0; 19961da177e4SLinus Torvalds break; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds /* 19991da177e4SLinus Torvalds * No entries in use at top of swap_map, 20001da177e4SLinus Torvalds * loop back to start and recheck there. 20011da177e4SLinus Torvalds */ 20021da177e4SLinus Torvalds max = prev + 1; 20031da177e4SLinus Torvalds prev = 0; 20041da177e4SLinus Torvalds i = 1; 20051da177e4SLinus Torvalds } 20064db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 2007355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 2008dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 20091da177e4SLinus Torvalds break; 2010dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 2011dc644a07SHugh Dickins cond_resched(); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds return i; 20141da177e4SLinus Torvalds } 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds /* 20171da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 20181da177e4SLinus Torvalds * and then search for the process using it. All the necessary 20191da177e4SLinus Torvalds * page table adjustments can then be made atomically. 202038b5faf4SDan Magenheimer * 202138b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 202238b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 20231da177e4SLinus Torvalds */ 202438b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 202538b5faf4SDan Magenheimer unsigned long pages_to_unuse) 20261da177e4SLinus Torvalds { 2027efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 20281da177e4SLinus Torvalds struct mm_struct *start_mm; 2029edfe23daSShaohua Li volatile unsigned char *swap_map; /* swap_map is accessed without 2030edfe23daSShaohua Li * locking. Mark it as volatile 2031edfe23daSShaohua Li * to prevent compiler doing 2032edfe23daSShaohua Li * something odd. 2033edfe23daSShaohua Li */ 20348d69aaeeSHugh Dickins unsigned char swcount; 20351da177e4SLinus Torvalds struct page *page; 20361da177e4SLinus Torvalds swp_entry_t entry; 20376eb396dcSHugh Dickins unsigned int i = 0; 20381da177e4SLinus Torvalds int retval = 0; 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* 20411da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 20421da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 20431da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 20441da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 20451da177e4SLinus Torvalds * 20461da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 20471da177e4SLinus Torvalds * freed the previous entry from; but that would take less 20481da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 20491da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 20501da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 20511da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 2052570a335bSHugh Dickins * that. 20531da177e4SLinus Torvalds */ 20541da177e4SLinus Torvalds start_mm = &init_mm; 20553fce371bSVegard Nossum mmget(&init_mm); 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds /* 20581da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 20591da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 20601da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 20611da177e4SLinus Torvalds */ 206238b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 20631da177e4SLinus Torvalds if (signal_pending(current)) { 20641da177e4SLinus Torvalds retval = -EINTR; 20651da177e4SLinus Torvalds break; 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds /* 20691da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 20701da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 20711da177e4SLinus Torvalds * page and read the swap into it. 20721da177e4SLinus Torvalds */ 20731da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 20741da177e4SLinus Torvalds entry = swp_entry(type, i); 207502098feaSHugh Dickins page = read_swap_cache_async(entry, 207623955622SShaohua Li GFP_HIGHUSER_MOVABLE, NULL, 0, false); 20771da177e4SLinus Torvalds if (!page) { 20781da177e4SLinus Torvalds /* 20791da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 20801da177e4SLinus Torvalds * has been freed independently, and will not be 20811da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 20821da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 20831da177e4SLinus Torvalds */ 2084edfe23daSShaohua Li swcount = *swap_map; 2085edfe23daSShaohua Li /* 2086edfe23daSShaohua Li * We don't hold lock here, so the swap entry could be 2087edfe23daSShaohua Li * SWAP_MAP_BAD (when the cluster is discarding). 2088edfe23daSShaohua Li * Instead of fail out, We can just skip the swap 2089edfe23daSShaohua Li * entry because swapoff will wait for discarding 2090edfe23daSShaohua Li * finish anyway. 2091edfe23daSShaohua Li */ 2092edfe23daSShaohua Li if (!swcount || swcount == SWAP_MAP_BAD) 20931da177e4SLinus Torvalds continue; 20941da177e4SLinus Torvalds retval = -ENOMEM; 20951da177e4SLinus Torvalds break; 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds /* 20991da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 21001da177e4SLinus Torvalds */ 21011da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 21021da177e4SLinus Torvalds mmput(start_mm); 21031da177e4SLinus Torvalds start_mm = &init_mm; 21043fce371bSVegard Nossum mmget(&init_mm); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds 21071da177e4SLinus Torvalds /* 21081da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 21091da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 21101da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 21111da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 21121da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 21131da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 21141da177e4SLinus Torvalds */ 21151da177e4SLinus Torvalds wait_on_page_locked(page); 21161da177e4SLinus Torvalds wait_on_page_writeback(page); 21171da177e4SLinus Torvalds lock_page(page); 21181da177e4SLinus Torvalds wait_on_page_writeback(page); 21191da177e4SLinus Torvalds 21201da177e4SLinus Torvalds /* 21211da177e4SLinus Torvalds * Remove all references to entry. 21221da177e4SLinus Torvalds */ 21231da177e4SLinus Torvalds swcount = *swap_map; 2124aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 2125aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 2126aaa46865SHugh Dickins /* page has already been unlocked and released */ 2127aaa46865SHugh Dickins if (retval < 0) 2128aaa46865SHugh Dickins break; 2129aaa46865SHugh Dickins continue; 21301da177e4SLinus Torvalds } 2131aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 2132aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 2133aaa46865SHugh Dickins 2134355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 21351da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 21361da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 21371da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 21381da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 21391da177e4SLinus Torvalds struct mm_struct *mm; 21401da177e4SLinus Torvalds 21413fce371bSVegard Nossum mmget(new_start_mm); 21423fce371bSVegard Nossum mmget(prev_mm); 21431da177e4SLinus Torvalds spin_lock(&mmlist_lock); 2144aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 21451da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 21461da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 2147388f7934SVegard Nossum if (!mmget_not_zero(mm)) 21481da177e4SLinus Torvalds continue; 21491da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 21501da177e4SLinus Torvalds mmput(prev_mm); 21511da177e4SLinus Torvalds prev_mm = mm; 21521da177e4SLinus Torvalds 21531da177e4SLinus Torvalds cond_resched(); 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds swcount = *swap_map; 2156355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 21571da177e4SLinus Torvalds ; 2158aaa46865SHugh Dickins else if (mm == &init_mm) 21591da177e4SLinus Torvalds set_start_mm = 1; 2160aaa46865SHugh Dickins else 21611da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 2162355cfa73SKAMEZAWA Hiroyuki 216332c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 21641da177e4SLinus Torvalds mmput(new_start_mm); 21653fce371bSVegard Nossum mmget(mm); 21661da177e4SLinus Torvalds new_start_mm = mm; 21671da177e4SLinus Torvalds set_start_mm = 0; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds spin_lock(&mmlist_lock); 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 21721da177e4SLinus Torvalds mmput(prev_mm); 21731da177e4SLinus Torvalds mmput(start_mm); 21741da177e4SLinus Torvalds start_mm = new_start_mm; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds if (retval) { 21771da177e4SLinus Torvalds unlock_page(page); 217809cbfeafSKirill A. Shutemov put_page(page); 21791da177e4SLinus Torvalds break; 21801da177e4SLinus Torvalds } 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* 21831da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 21841da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 21851da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 21861da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 21871da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 21881da177e4SLinus Torvalds * delete from cache even if there's another reference, 21891da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 21901da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 21911da177e4SLinus Torvalds * read from disk into another page. Splitting into two 21921da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 21931da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 21945ad64688SHugh Dickins * 21955ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 21965ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 21975ad64688SHugh Dickins * this splitting happens to be just what is needed to 21985ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 21995ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 22001da177e4SLinus Torvalds */ 2201355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 2202355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 22031da177e4SLinus Torvalds struct writeback_control wbc = { 22041da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 22051da177e4SLinus Torvalds }; 22061da177e4SLinus Torvalds 2207e0709829SHuang Ying swap_writepage(compound_head(page), &wbc); 22081da177e4SLinus Torvalds lock_page(page); 22091da177e4SLinus Torvalds wait_on_page_writeback(page); 22101da177e4SLinus Torvalds } 221168bdc8d6SHugh Dickins 221268bdc8d6SHugh Dickins /* 221368bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 221468bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 221568bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 221668bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 221768bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 221868bdc8d6SHugh Dickins */ 221968bdc8d6SHugh Dickins if (PageSwapCache(page) && 2220e0709829SHuang Ying likely(page_private(page) == entry.val) && 2221e0709829SHuang Ying !page_swapped(page)) 2222e0709829SHuang Ying delete_from_swap_cache(compound_head(page)); 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds /* 22251da177e4SLinus Torvalds * So we could skip searching mms once swap count went 22261da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 22272706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 22281da177e4SLinus Torvalds */ 22291da177e4SLinus Torvalds SetPageDirty(page); 22301da177e4SLinus Torvalds unlock_page(page); 223109cbfeafSKirill A. Shutemov put_page(page); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds /* 22341da177e4SLinus Torvalds * Make sure that we aren't completely killing 22351da177e4SLinus Torvalds * interactive performance. 22361da177e4SLinus Torvalds */ 22371da177e4SLinus Torvalds cond_resched(); 223838b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 223938b5faf4SDan Magenheimer if (!--pages_to_unuse) 224038b5faf4SDan Magenheimer break; 224138b5faf4SDan Magenheimer } 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds mmput(start_mm); 22451da177e4SLinus Torvalds return retval; 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds /* 22495d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 22505d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 22515d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 22521da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 22531da177e4SLinus Torvalds */ 22541da177e4SLinus Torvalds static void drain_mmlist(void) 22551da177e4SLinus Torvalds { 22561da177e4SLinus Torvalds struct list_head *p, *next; 2257efa90a98SHugh Dickins unsigned int type; 22581da177e4SLinus Torvalds 2259efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 2260efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 22611da177e4SLinus Torvalds return; 22621da177e4SLinus Torvalds spin_lock(&mmlist_lock); 22631da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 22641da177e4SLinus Torvalds list_del_init(p); 22651da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 22661da177e4SLinus Torvalds } 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds /* 22691da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 2270d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 2271d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 2272d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 22731da177e4SLinus Torvalds */ 2274d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 22751da177e4SLinus Torvalds { 2276f29ad6a9SHugh Dickins struct swap_info_struct *sis; 2277f29ad6a9SHugh Dickins struct swap_extent *start_se; 2278f29ad6a9SHugh Dickins struct swap_extent *se; 2279f29ad6a9SHugh Dickins pgoff_t offset; 2280f29ad6a9SHugh Dickins 2281efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 2282f29ad6a9SHugh Dickins *bdev = sis->bdev; 2283f29ad6a9SHugh Dickins 2284f29ad6a9SHugh Dickins offset = swp_offset(entry); 2285f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 2286f29ad6a9SHugh Dickins se = start_se; 22871da177e4SLinus Torvalds 22881da177e4SLinus Torvalds for ( ; ; ) { 22891da177e4SLinus Torvalds if (se->start_page <= offset && 22901da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 22911da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 22921da177e4SLinus Torvalds } 2293a8ae4991SGeliang Tang se = list_next_entry(se, list); 22941da177e4SLinus Torvalds sis->curr_swap_extent = se; 22951da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 22991da177e4SLinus Torvalds /* 2300d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 2301d4906e1aSLee Schermerhorn */ 2302d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 2303d4906e1aSLee Schermerhorn { 2304d4906e1aSLee Schermerhorn swp_entry_t entry; 2305d4906e1aSLee Schermerhorn entry.val = page_private(page); 2306d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 2307d4906e1aSLee Schermerhorn } 2308d4906e1aSLee Schermerhorn 2309d4906e1aSLee Schermerhorn /* 23101da177e4SLinus Torvalds * Free all of a swapdev's extent information 23111da177e4SLinus Torvalds */ 23121da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 23131da177e4SLinus Torvalds { 23149625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 23151da177e4SLinus Torvalds struct swap_extent *se; 23161da177e4SLinus Torvalds 2317a8ae4991SGeliang Tang se = list_first_entry(&sis->first_swap_extent.list, 23181da177e4SLinus Torvalds struct swap_extent, list); 23191da177e4SLinus Torvalds list_del(&se->list); 23201da177e4SLinus Torvalds kfree(se); 23211da177e4SLinus Torvalds } 232262c230bcSMel Gorman 232362c230bcSMel Gorman if (sis->flags & SWP_FILE) { 232462c230bcSMel Gorman struct file *swap_file = sis->swap_file; 232562c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 232662c230bcSMel Gorman 232762c230bcSMel Gorman sis->flags &= ~SWP_FILE; 232862c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 232962c230bcSMel Gorman } 23301da177e4SLinus Torvalds } 23311da177e4SLinus Torvalds 23321da177e4SLinus Torvalds /* 23331da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 233411d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 23351da177e4SLinus Torvalds * 233611d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 23371da177e4SLinus Torvalds */ 2338a509bc1aSMel Gorman int 23391da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 23401da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 23411da177e4SLinus Torvalds { 23421da177e4SLinus Torvalds struct swap_extent *se; 23431da177e4SLinus Torvalds struct swap_extent *new_se; 23441da177e4SLinus Torvalds struct list_head *lh; 23451da177e4SLinus Torvalds 23469625a5f2SHugh Dickins if (start_page == 0) { 23479625a5f2SHugh Dickins se = &sis->first_swap_extent; 23489625a5f2SHugh Dickins sis->curr_swap_extent = se; 23499625a5f2SHugh Dickins se->start_page = 0; 23509625a5f2SHugh Dickins se->nr_pages = nr_pages; 23519625a5f2SHugh Dickins se->start_block = start_block; 23529625a5f2SHugh Dickins return 1; 23539625a5f2SHugh Dickins } else { 23549625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 23551da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 235611d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 235711d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 23581da177e4SLinus Torvalds /* Merge it */ 23591da177e4SLinus Torvalds se->nr_pages += nr_pages; 23601da177e4SLinus Torvalds return 0; 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds } 23631da177e4SLinus Torvalds 23641da177e4SLinus Torvalds /* 23651da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 23661da177e4SLinus Torvalds */ 23671da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 23681da177e4SLinus Torvalds if (new_se == NULL) 23691da177e4SLinus Torvalds return -ENOMEM; 23701da177e4SLinus Torvalds new_se->start_page = start_page; 23711da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 23721da177e4SLinus Torvalds new_se->start_block = start_block; 23731da177e4SLinus Torvalds 23749625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 237553092a74SHugh Dickins return 1; 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 23781da177e4SLinus Torvalds /* 23791da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 23801da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 23811da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 23821da177e4SLinus Torvalds * time for locating where on disk a page belongs. 23831da177e4SLinus Torvalds * 23841da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 23851da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 23861da177e4SLinus Torvalds * swap files identically. 23871da177e4SLinus Torvalds * 23881da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 23891da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 23901da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 23911da177e4SLinus Torvalds * 23921da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 23931da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 23941da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 23951da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 23961da177e4SLinus Torvalds * for swapping. 23971da177e4SLinus Torvalds * 2398b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 23991da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 24001da177e4SLinus Torvalds * which will scribble on the fs. 24011da177e4SLinus Torvalds * 24021da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 24031da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 24041da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 24051da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 24061da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 24071da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 24081da177e4SLinus Torvalds */ 240953092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 24101da177e4SLinus Torvalds { 241162c230bcSMel Gorman struct file *swap_file = sis->swap_file; 241262c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 241362c230bcSMel Gorman struct inode *inode = mapping->host; 24141da177e4SLinus Torvalds int ret; 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 24171da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 241853092a74SHugh Dickins *span = sis->pages; 2419a509bc1aSMel Gorman return ret; 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds 242262c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 2423a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 242462c230bcSMel Gorman if (!ret) { 242562c230bcSMel Gorman sis->flags |= SWP_FILE; 242662c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 242762c230bcSMel Gorman *span = sis->pages; 242862c230bcSMel Gorman } 24299625a5f2SHugh Dickins return ret; 2430a509bc1aSMel Gorman } 2431a509bc1aSMel Gorman 2432a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 24331da177e4SLinus Torvalds } 24341da177e4SLinus Torvalds 2435a2468cc9SAaron Lu static int swap_node(struct swap_info_struct *p) 2436a2468cc9SAaron Lu { 2437a2468cc9SAaron Lu struct block_device *bdev; 2438a2468cc9SAaron Lu 2439a2468cc9SAaron Lu if (p->bdev) 2440a2468cc9SAaron Lu bdev = p->bdev; 2441a2468cc9SAaron Lu else 2442a2468cc9SAaron Lu bdev = p->swap_file->f_inode->i_sb->s_bdev; 2443a2468cc9SAaron Lu 2444a2468cc9SAaron Lu return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE; 2445a2468cc9SAaron Lu } 2446a2468cc9SAaron Lu 2447cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 24482a8f9449SShaohua Li unsigned char *swap_map, 24492a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 245040531542SCesar Eduardo Barros { 2451a2468cc9SAaron Lu int i; 2452a2468cc9SAaron Lu 245340531542SCesar Eduardo Barros if (prio >= 0) 245440531542SCesar Eduardo Barros p->prio = prio; 245540531542SCesar Eduardo Barros else 245640531542SCesar Eduardo Barros p->prio = --least_priority; 245718ab4d4cSDan Streetman /* 245818ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 245918ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 246018ab4d4cSDan Streetman */ 246118ab4d4cSDan Streetman p->list.prio = -p->prio; 2462a2468cc9SAaron Lu for_each_node(i) { 2463a2468cc9SAaron Lu if (p->prio >= 0) 2464a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2465a2468cc9SAaron Lu else { 2466a2468cc9SAaron Lu if (swap_node(p) == i) 2467a2468cc9SAaron Lu p->avail_lists[i].prio = 1; 2468a2468cc9SAaron Lu else 2469a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2470a2468cc9SAaron Lu } 2471a2468cc9SAaron Lu } 247240531542SCesar Eduardo Barros p->swap_map = swap_map; 24732a8f9449SShaohua Li p->cluster_info = cluster_info; 247440531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 2475ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 247640531542SCesar Eduardo Barros total_swap_pages += p->pages; 247740531542SCesar Eduardo Barros 2478adfab836SDan Streetman assert_spin_locked(&swap_lock); 2479adfab836SDan Streetman /* 248018ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 248118ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 248218ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 248318ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 248418ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 248518ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 248618ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 248718ab4d4cSDan Streetman * swap_info_struct. 2488adfab836SDan Streetman */ 248918ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 2490a2468cc9SAaron Lu add_to_avail_list(p); 2491cf0cac0aSCesar Eduardo Barros } 2492cf0cac0aSCesar Eduardo Barros 2493cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 2494cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 24952a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2496cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 2497cf0cac0aSCesar Eduardo Barros { 24984f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 2499cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2500ec8acf20SShaohua Li spin_lock(&p->lock); 25012a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 2502ec8acf20SShaohua Li spin_unlock(&p->lock); 2503cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 2504cf0cac0aSCesar Eduardo Barros } 2505cf0cac0aSCesar Eduardo Barros 2506cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 2507cf0cac0aSCesar Eduardo Barros { 2508cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2509ec8acf20SShaohua Li spin_lock(&p->lock); 25102a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 2511ec8acf20SShaohua Li spin_unlock(&p->lock); 251240531542SCesar Eduardo Barros spin_unlock(&swap_lock); 251340531542SCesar Eduardo Barros } 251440531542SCesar Eduardo Barros 251567afa38eSTim Chen bool has_usable_swap(void) 251667afa38eSTim Chen { 251767afa38eSTim Chen bool ret = true; 251867afa38eSTim Chen 251967afa38eSTim Chen spin_lock(&swap_lock); 252067afa38eSTim Chen if (plist_head_empty(&swap_active_head)) 252167afa38eSTim Chen ret = false; 252267afa38eSTim Chen spin_unlock(&swap_lock); 252367afa38eSTim Chen return ret; 252467afa38eSTim Chen } 252567afa38eSTim Chen 2526c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 25271da177e4SLinus Torvalds { 25281da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 25298d69aaeeSHugh Dickins unsigned char *swap_map; 25302a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 25314f89849dSMinchan Kim unsigned long *frontswap_map; 25321da177e4SLinus Torvalds struct file *swap_file, *victim; 25331da177e4SLinus Torvalds struct address_space *mapping; 25341da177e4SLinus Torvalds struct inode *inode; 253591a27b2aSJeff Layton struct filename *pathname; 2536adfab836SDan Streetman int err, found = 0; 25375b808a23SKrzysztof Kozlowski unsigned int old_block_size; 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 25401da177e4SLinus Torvalds return -EPERM; 25411da177e4SLinus Torvalds 2542191c5424SAl Viro BUG_ON(!current->mm); 2543191c5424SAl Viro 25441da177e4SLinus Torvalds pathname = getname(specialfile); 25451da177e4SLinus Torvalds if (IS_ERR(pathname)) 2546f58b59c1SXiaotian Feng return PTR_ERR(pathname); 25471da177e4SLinus Torvalds 2548669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 25491da177e4SLinus Torvalds err = PTR_ERR(victim); 25501da177e4SLinus Torvalds if (IS_ERR(victim)) 25511da177e4SLinus Torvalds goto out; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds mapping = victim->f_mapping; 25545d337b91SHugh Dickins spin_lock(&swap_lock); 255518ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 255622c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 2557adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 2558adfab836SDan Streetman found = 1; 25591da177e4SLinus Torvalds break; 25601da177e4SLinus Torvalds } 25611da177e4SLinus Torvalds } 2562adfab836SDan Streetman } 2563adfab836SDan Streetman if (!found) { 25641da177e4SLinus Torvalds err = -EINVAL; 25655d337b91SHugh Dickins spin_unlock(&swap_lock); 25661da177e4SLinus Torvalds goto out_dput; 25671da177e4SLinus Torvalds } 2568191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 25691da177e4SLinus Torvalds vm_unacct_memory(p->pages); 25701da177e4SLinus Torvalds else { 25711da177e4SLinus Torvalds err = -ENOMEM; 25725d337b91SHugh Dickins spin_unlock(&swap_lock); 25731da177e4SLinus Torvalds goto out_dput; 25741da177e4SLinus Torvalds } 2575a2468cc9SAaron Lu del_from_avail_list(p); 2576ec8acf20SShaohua Li spin_lock(&p->lock); 257778ecba08SHugh Dickins if (p->prio < 0) { 2578adfab836SDan Streetman struct swap_info_struct *si = p; 2579a2468cc9SAaron Lu int nid; 2580adfab836SDan Streetman 258118ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2582adfab836SDan Streetman si->prio++; 258318ab4d4cSDan Streetman si->list.prio--; 2584a2468cc9SAaron Lu for_each_node(nid) { 2585a2468cc9SAaron Lu if (si->avail_lists[nid].prio != 1) 2586a2468cc9SAaron Lu si->avail_lists[nid].prio--; 2587a2468cc9SAaron Lu } 2588adfab836SDan Streetman } 258978ecba08SHugh Dickins least_priority++; 259078ecba08SHugh Dickins } 259118ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2592ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 25931da177e4SLinus Torvalds total_swap_pages -= p->pages; 25941da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2595ec8acf20SShaohua Li spin_unlock(&p->lock); 25965d337b91SHugh Dickins spin_unlock(&swap_lock); 2597fb4f88dcSHugh Dickins 2598039939a6STim Chen disable_swap_slots_cache_lock(); 2599039939a6STim Chen 2600e1e12d2fSDavid Rientjes set_current_oom_origin(); 2601adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2602e1e12d2fSDavid Rientjes clear_current_oom_origin(); 26031da177e4SLinus Torvalds 26041da177e4SLinus Torvalds if (err) { 26051da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2606cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 2607039939a6STim Chen reenable_swap_slots_cache_unlock(); 26081da177e4SLinus Torvalds goto out_dput; 26091da177e4SLinus Torvalds } 261052b7efdbSHugh Dickins 2611039939a6STim Chen reenable_swap_slots_cache_unlock(); 2612039939a6STim Chen 2613815c2c54SShaohua Li flush_work(&p->discard_work); 2614815c2c54SShaohua Li 26154cd3bb10SHugh Dickins destroy_swap_extents(p); 2616570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2617570a335bSHugh Dickins free_swap_count_continuations(p); 2618570a335bSHugh Dickins 261981a0298bSHuang Ying if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev))) 262081a0298bSHuang Ying atomic_dec(&nr_rotate_swap); 262181a0298bSHuang Ying 2622fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 26235d337b91SHugh Dickins spin_lock(&swap_lock); 2624ec8acf20SShaohua Li spin_lock(&p->lock); 26251da177e4SLinus Torvalds drain_mmlist(); 26265d337b91SHugh Dickins 26275d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 26285d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 26295d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2630ec8acf20SShaohua Li spin_unlock(&p->lock); 26315d337b91SHugh Dickins spin_unlock(&swap_lock); 263213e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 26335d337b91SHugh Dickins spin_lock(&swap_lock); 2634ec8acf20SShaohua Li spin_lock(&p->lock); 26355d337b91SHugh Dickins } 26365d337b91SHugh Dickins 26371da177e4SLinus Torvalds swap_file = p->swap_file; 26385b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 26391da177e4SLinus Torvalds p->swap_file = NULL; 26401da177e4SLinus Torvalds p->max = 0; 26411da177e4SLinus Torvalds swap_map = p->swap_map; 26421da177e4SLinus Torvalds p->swap_map = NULL; 26432a8f9449SShaohua Li cluster_info = p->cluster_info; 26442a8f9449SShaohua Li p->cluster_info = NULL; 26454f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2646ec8acf20SShaohua Li spin_unlock(&p->lock); 26475d337b91SHugh Dickins spin_unlock(&swap_lock); 2648adfab836SDan Streetman frontswap_invalidate_area(p->type); 264958e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2650fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2651ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2652ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 26531da177e4SLinus Torvalds vfree(swap_map); 265454f180d3SHuang Ying kvfree(cluster_info); 265554f180d3SHuang Ying kvfree(frontswap_map); 26562de1a7e4SSeth Jennings /* Destroy swap account information */ 2657adfab836SDan Streetman swap_cgroup_swapoff(p->type); 26584b3ef9daSHuang, Ying exit_swap_address_space(p->type); 265927a7faa0SKAMEZAWA Hiroyuki 26601da177e4SLinus Torvalds inode = mapping->host; 26611da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 26621da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 26635b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2664e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 26651da177e4SLinus Torvalds } else { 26665955102cSAl Viro inode_lock(inode); 26671da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 26685955102cSAl Viro inode_unlock(inode); 26691da177e4SLinus Torvalds } 26701da177e4SLinus Torvalds filp_close(swap_file, NULL); 2671f893ab41SWeijie Yang 2672f893ab41SWeijie Yang /* 2673f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2674f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2675f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2676f893ab41SWeijie Yang */ 2677f893ab41SWeijie Yang spin_lock(&swap_lock); 2678f893ab41SWeijie Yang p->flags = 0; 2679f893ab41SWeijie Yang spin_unlock(&swap_lock); 2680f893ab41SWeijie Yang 26811da177e4SLinus Torvalds err = 0; 268266d7dd51SKay Sievers atomic_inc(&proc_poll_event); 268366d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 26841da177e4SLinus Torvalds 26851da177e4SLinus Torvalds out_dput: 26861da177e4SLinus Torvalds filp_close(victim, NULL); 26871da177e4SLinus Torvalds out: 2688f58b59c1SXiaotian Feng putname(pathname); 26891da177e4SLinus Torvalds return err; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26921da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 269366d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 269466d7dd51SKay Sievers { 2695f1514638SKay Sievers struct seq_file *seq = file->private_data; 269666d7dd51SKay Sievers 269766d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 269866d7dd51SKay Sievers 2699f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2700f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 270166d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 270266d7dd51SKay Sievers } 270366d7dd51SKay Sievers 270466d7dd51SKay Sievers return POLLIN | POLLRDNORM; 270566d7dd51SKay Sievers } 270666d7dd51SKay Sievers 27071da177e4SLinus Torvalds /* iterator */ 27081da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 27091da177e4SLinus Torvalds { 2710efa90a98SHugh Dickins struct swap_info_struct *si; 2711efa90a98SHugh Dickins int type; 27121da177e4SLinus Torvalds loff_t l = *pos; 27131da177e4SLinus Torvalds 2714fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 27151da177e4SLinus Torvalds 2716881e4aabSSuleiman Souhlal if (!l) 2717881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2718881e4aabSSuleiman Souhlal 2719efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2720efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2721efa90a98SHugh Dickins si = swap_info[type]; 2722efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27231da177e4SLinus Torvalds continue; 2724881e4aabSSuleiman Souhlal if (!--l) 2725efa90a98SHugh Dickins return si; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27281da177e4SLinus Torvalds return NULL; 27291da177e4SLinus Torvalds } 27301da177e4SLinus Torvalds 27311da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 27321da177e4SLinus Torvalds { 2733efa90a98SHugh Dickins struct swap_info_struct *si = v; 2734efa90a98SHugh Dickins int type; 27351da177e4SLinus Torvalds 2736881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2737efa90a98SHugh Dickins type = 0; 2738efa90a98SHugh Dickins else 2739efa90a98SHugh Dickins type = si->type + 1; 2740881e4aabSSuleiman Souhlal 2741efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 2742efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2743efa90a98SHugh Dickins si = swap_info[type]; 2744efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27451da177e4SLinus Torvalds continue; 27461da177e4SLinus Torvalds ++*pos; 2747efa90a98SHugh Dickins return si; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds return NULL; 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds 27531da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 27541da177e4SLinus Torvalds { 2755fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 27591da177e4SLinus Torvalds { 2760efa90a98SHugh Dickins struct swap_info_struct *si = v; 27611da177e4SLinus Torvalds struct file *file; 27621da177e4SLinus Torvalds int len; 27631da177e4SLinus Torvalds 2764efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 27651da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2766881e4aabSSuleiman Souhlal return 0; 2767881e4aabSSuleiman Souhlal } 27681da177e4SLinus Torvalds 2769efa90a98SHugh Dickins file = si->swap_file; 27702726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 27716eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 27721da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2773496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 27741da177e4SLinus Torvalds "partition" : "file\t", 2775efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2776efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2777efa90a98SHugh Dickins si->prio); 27781da177e4SLinus Torvalds return 0; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 278115ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 27821da177e4SLinus Torvalds .start = swap_start, 27831da177e4SLinus Torvalds .next = swap_next, 27841da177e4SLinus Torvalds .stop = swap_stop, 27851da177e4SLinus Torvalds .show = swap_show 27861da177e4SLinus Torvalds }; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 27891da177e4SLinus Torvalds { 2790f1514638SKay Sievers struct seq_file *seq; 279166d7dd51SKay Sievers int ret; 279266d7dd51SKay Sievers 279366d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2794f1514638SKay Sievers if (ret) 279566d7dd51SKay Sievers return ret; 279666d7dd51SKay Sievers 2797f1514638SKay Sievers seq = file->private_data; 2798f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2799f1514638SKay Sievers return 0; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds 280215ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 28031da177e4SLinus Torvalds .open = swaps_open, 28041da177e4SLinus Torvalds .read = seq_read, 28051da177e4SLinus Torvalds .llseek = seq_lseek, 28061da177e4SLinus Torvalds .release = seq_release, 280766d7dd51SKay Sievers .poll = swaps_poll, 28081da177e4SLinus Torvalds }; 28091da177e4SLinus Torvalds 28101da177e4SLinus Torvalds static int __init procswaps_init(void) 28111da177e4SLinus Torvalds { 28123d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 28131da177e4SLinus Torvalds return 0; 28141da177e4SLinus Torvalds } 28151da177e4SLinus Torvalds __initcall(procswaps_init); 28161da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 28171da177e4SLinus Torvalds 28181796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 28191796316aSJan Beulich static int __init max_swapfiles_check(void) 28201796316aSJan Beulich { 28211796316aSJan Beulich MAX_SWAPFILES_CHECK(); 28221796316aSJan Beulich return 0; 28231796316aSJan Beulich } 28241796316aSJan Beulich late_initcall(max_swapfiles_check); 28251796316aSJan Beulich #endif 28261796316aSJan Beulich 282753cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 28281da177e4SLinus Torvalds { 28291da177e4SLinus Torvalds struct swap_info_struct *p; 28301da177e4SLinus Torvalds unsigned int type; 2831a2468cc9SAaron Lu int i; 2832efa90a98SHugh Dickins 2833efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2834efa90a98SHugh Dickins if (!p) 283553cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2836efa90a98SHugh Dickins 28375d337b91SHugh Dickins spin_lock(&swap_lock); 2838efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2839efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 28401da177e4SLinus Torvalds break; 2841efa90a98SHugh Dickins } 28420697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 28435d337b91SHugh Dickins spin_unlock(&swap_lock); 2844efa90a98SHugh Dickins kfree(p); 2845730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 28461da177e4SLinus Torvalds } 2847efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2848efa90a98SHugh Dickins p->type = type; 2849efa90a98SHugh Dickins swap_info[type] = p; 2850efa90a98SHugh Dickins /* 2851efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2852efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2853efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2854efa90a98SHugh Dickins */ 2855efa90a98SHugh Dickins smp_wmb(); 2856efa90a98SHugh Dickins nr_swapfiles++; 2857efa90a98SHugh Dickins } else { 2858efa90a98SHugh Dickins kfree(p); 2859efa90a98SHugh Dickins p = swap_info[type]; 2860efa90a98SHugh Dickins /* 2861efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2862efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2863efa90a98SHugh Dickins */ 2864efa90a98SHugh Dickins } 28659625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 286618ab4d4cSDan Streetman plist_node_init(&p->list, 0); 2867a2468cc9SAaron Lu for_each_node(i) 2868a2468cc9SAaron Lu plist_node_init(&p->avail_lists[i], 0); 28691da177e4SLinus Torvalds p->flags = SWP_USED; 28705d337b91SHugh Dickins spin_unlock(&swap_lock); 2871ec8acf20SShaohua Li spin_lock_init(&p->lock); 2872efa90a98SHugh Dickins 287353cbb243SCesar Eduardo Barros return p; 287453cbb243SCesar Eduardo Barros } 287553cbb243SCesar Eduardo Barros 28764d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 28774d0e1e10SCesar Eduardo Barros { 28784d0e1e10SCesar Eduardo Barros int error; 28794d0e1e10SCesar Eduardo Barros 28804d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 28814d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 28824d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 28836f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 28844d0e1e10SCesar Eduardo Barros if (error < 0) { 28854d0e1e10SCesar Eduardo Barros p->bdev = NULL; 28866f179af8SHugh Dickins return error; 28874d0e1e10SCesar Eduardo Barros } 28884d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 28894d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 28904d0e1e10SCesar Eduardo Barros if (error < 0) 289187ade72aSCesar Eduardo Barros return error; 28924d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 28934d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 28944d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 28955955102cSAl Viro inode_lock(inode); 289687ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 289787ade72aSCesar Eduardo Barros return -EBUSY; 289887ade72aSCesar Eduardo Barros } else 289987ade72aSCesar Eduardo Barros return -EINVAL; 29004d0e1e10SCesar Eduardo Barros 29014d0e1e10SCesar Eduardo Barros return 0; 29024d0e1e10SCesar Eduardo Barros } 29034d0e1e10SCesar Eduardo Barros 2904ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2905ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2906ca8bd38bSCesar Eduardo Barros struct inode *inode) 2907ca8bd38bSCesar Eduardo Barros { 2908ca8bd38bSCesar Eduardo Barros int i; 2909ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2910ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2911d6bbbd29SRaymond Jennings unsigned long last_page; 2912ca8bd38bSCesar Eduardo Barros 2913ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2914465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 291538719025SCesar Eduardo Barros return 0; 2916ca8bd38bSCesar Eduardo Barros } 2917ca8bd38bSCesar Eduardo Barros 2918ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2919ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2920ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2921ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2922ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2923dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2924dd111be6SJann Horn return 0; 2925ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2926ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2927ca8bd38bSCesar Eduardo Barros } 2928ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2929ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2930465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2931ca8bd38bSCesar Eduardo Barros swap_header->info.version); 293238719025SCesar Eduardo Barros return 0; 2933ca8bd38bSCesar Eduardo Barros } 2934ca8bd38bSCesar Eduardo Barros 2935ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2936ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2937ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2938ca8bd38bSCesar Eduardo Barros 2939ca8bd38bSCesar Eduardo Barros /* 2940ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 29419b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2942a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2943a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 29449b15b817SHugh Dickins * different architectures. In order to find the 2945a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2946ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2947a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2948ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2949ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2950ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 29519b15b817SHugh Dickins * swap pte. 2952ca8bd38bSCesar Eduardo Barros */ 2953ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 29549b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2955d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2956d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2957465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2958d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2959d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2960d6bbbd29SRaymond Jennings } 2961d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2962d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2963ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2964ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2965ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2966ca8bd38bSCesar Eduardo Barros } 2967ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2968ca8bd38bSCesar Eduardo Barros 2969ca8bd38bSCesar Eduardo Barros if (!maxpages) 297038719025SCesar Eduardo Barros return 0; 2971ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2972ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2973465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 297438719025SCesar Eduardo Barros return 0; 2975ca8bd38bSCesar Eduardo Barros } 2976ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 297738719025SCesar Eduardo Barros return 0; 2978ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 297938719025SCesar Eduardo Barros return 0; 2980ca8bd38bSCesar Eduardo Barros 2981ca8bd38bSCesar Eduardo Barros return maxpages; 2982ca8bd38bSCesar Eduardo Barros } 2983ca8bd38bSCesar Eduardo Barros 29844b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS \ 2985235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 29864b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS \ 29874b3ef9daSHuang, Ying DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) 29884b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS \ 29894b3ef9daSHuang, Ying max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) 2990235b6217SHuang, Ying 2991915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2992915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2993915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 29942a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2995915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2996915d4d7bSCesar Eduardo Barros sector_t *span) 2997915d4d7bSCesar Eduardo Barros { 2998235b6217SHuang, Ying unsigned int j, k; 2999915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 3000915d4d7bSCesar Eduardo Barros int nr_extents; 30012a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 3002235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 3003235b6217SHuang, Ying unsigned long i, idx; 3004915d4d7bSCesar Eduardo Barros 3005915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 3006915d4d7bSCesar Eduardo Barros 30076b534915SHuang Ying cluster_list_init(&p->free_clusters); 30086b534915SHuang Ying cluster_list_init(&p->discard_clusters); 30092a8f9449SShaohua Li 3010915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 3011915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 3012bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 3013bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3014915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 3015915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 3016915d4d7bSCesar Eduardo Barros nr_good_pages--; 30172a8f9449SShaohua Li /* 30182a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 30192a8f9449SShaohua Li * operation involved 30202a8f9449SShaohua Li */ 30212a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 3022915d4d7bSCesar Eduardo Barros } 3023915d4d7bSCesar Eduardo Barros } 3024915d4d7bSCesar Eduardo Barros 30252a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 30262a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 30272a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 30282a8f9449SShaohua Li 3029915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 3030915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 30312a8f9449SShaohua Li /* 30322a8f9449SShaohua Li * Not mark the cluster free yet, no list 30332a8f9449SShaohua Li * operation involved 30342a8f9449SShaohua Li */ 30352a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 3036915d4d7bSCesar Eduardo Barros p->max = maxpages; 3037915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 3038915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 3039bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 3040bdb8e3f6SCesar Eduardo Barros return nr_extents; 3041915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 3042915d4d7bSCesar Eduardo Barros } 3043915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 3044465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 3045bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3046915d4d7bSCesar Eduardo Barros } 3047915d4d7bSCesar Eduardo Barros 30482a8f9449SShaohua Li if (!cluster_info) 30492a8f9449SShaohua Li return nr_extents; 30502a8f9449SShaohua Li 3051235b6217SHuang, Ying 30524b3ef9daSHuang, Ying /* 30534b3ef9daSHuang, Ying * Reduce false cache line sharing between cluster_info and 30544b3ef9daSHuang, Ying * sharing same address space. 30554b3ef9daSHuang, Ying */ 3056235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 3057235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 3058235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 3059235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 3060235b6217SHuang, Ying if (idx >= nr_clusters) 3061235b6217SHuang, Ying continue; 3062235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 3063235b6217SHuang, Ying continue; 30642a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 30656b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 30666b534915SHuang Ying idx); 30672a8f9449SShaohua Li } 30682a8f9449SShaohua Li } 3069915d4d7bSCesar Eduardo Barros return nr_extents; 3070915d4d7bSCesar Eduardo Barros } 3071915d4d7bSCesar Eduardo Barros 3072dcf6b7ddSRafael Aquini /* 3073dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 3074dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 3075dcf6b7ddSRafael Aquini */ 3076dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 3077dcf6b7ddSRafael Aquini { 3078dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 3079dcf6b7ddSRafael Aquini 3080dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 3081dcf6b7ddSRafael Aquini return false; 3082dcf6b7ddSRafael Aquini 3083dcf6b7ddSRafael Aquini return true; 3084dcf6b7ddSRafael Aquini } 3085dcf6b7ddSRafael Aquini 308653cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 308753cbb243SCesar Eduardo Barros { 308853cbb243SCesar Eduardo Barros struct swap_info_struct *p; 308991a27b2aSJeff Layton struct filename *name; 309053cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 309153cbb243SCesar Eduardo Barros struct address_space *mapping; 309240531542SCesar Eduardo Barros int prio; 309353cbb243SCesar Eduardo Barros int error; 309453cbb243SCesar Eduardo Barros union swap_header *swap_header; 3095915d4d7bSCesar Eduardo Barros int nr_extents; 309653cbb243SCesar Eduardo Barros sector_t span; 309753cbb243SCesar Eduardo Barros unsigned long maxpages; 309853cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 30992a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 310038b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 310153cbb243SCesar Eduardo Barros struct page *page = NULL; 310253cbb243SCesar Eduardo Barros struct inode *inode = NULL; 310353cbb243SCesar Eduardo Barros 3104d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 3105d15cab97SHugh Dickins return -EINVAL; 3106d15cab97SHugh Dickins 310753cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 310853cbb243SCesar Eduardo Barros return -EPERM; 310953cbb243SCesar Eduardo Barros 3110a2468cc9SAaron Lu if (!swap_avail_heads) 3111a2468cc9SAaron Lu return -ENOMEM; 3112a2468cc9SAaron Lu 311353cbb243SCesar Eduardo Barros p = alloc_swap_info(); 31142542e513SCesar Eduardo Barros if (IS_ERR(p)) 31152542e513SCesar Eduardo Barros return PTR_ERR(p); 311653cbb243SCesar Eduardo Barros 3117815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 3118815c2c54SShaohua Li 31191da177e4SLinus Torvalds name = getname(specialfile); 31201da177e4SLinus Torvalds if (IS_ERR(name)) { 31217de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 31221da177e4SLinus Torvalds name = NULL; 3123bd69010bSCesar Eduardo Barros goto bad_swap; 31241da177e4SLinus Torvalds } 3125669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 31261da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 31277de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 31281da177e4SLinus Torvalds swap_file = NULL; 3129bd69010bSCesar Eduardo Barros goto bad_swap; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 31321da177e4SLinus Torvalds p->swap_file = swap_file; 31331da177e4SLinus Torvalds mapping = swap_file->f_mapping; 31342130781eSCesar Eduardo Barros inode = mapping->host; 31356f179af8SHugh Dickins 31365955102cSAl Viro /* If S_ISREG(inode->i_mode) will do inode_lock(inode); */ 31374d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 31384d0e1e10SCesar Eduardo Barros if (unlikely(error)) 31391da177e4SLinus Torvalds goto bad_swap; 31401da177e4SLinus Torvalds 31411da177e4SLinus Torvalds /* 31421da177e4SLinus Torvalds * Read the swap header. 31431da177e4SLinus Torvalds */ 31441da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 31451da177e4SLinus Torvalds error = -EINVAL; 31461da177e4SLinus Torvalds goto bad_swap; 31471da177e4SLinus Torvalds } 3148090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 31491da177e4SLinus Torvalds if (IS_ERR(page)) { 31501da177e4SLinus Torvalds error = PTR_ERR(page); 31511da177e4SLinus Torvalds goto bad_swap; 31521da177e4SLinus Torvalds } 315381e33971SHugh Dickins swap_header = kmap(page); 31541da177e4SLinus Torvalds 3155ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 3156ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 31571da177e4SLinus Torvalds error = -EINVAL; 31581da177e4SLinus Torvalds goto bad_swap; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 31611da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 3162803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 316378ecba08SHugh Dickins if (!swap_map) { 31641da177e4SLinus Torvalds error = -ENOMEM; 31651da177e4SLinus Torvalds goto bad_swap; 31661da177e4SLinus Torvalds } 3167f0571429SMinchan Kim 3168f0571429SMinchan Kim if (bdi_cap_stable_pages_required(inode_to_bdi(inode))) 3169f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 3170f0571429SMinchan Kim 31712a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 31726f179af8SHugh Dickins int cpu; 3173235b6217SHuang, Ying unsigned long ci, nr_cluster; 31746f179af8SHugh Dickins 31752a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 31762a8f9449SShaohua Li /* 31772a8f9449SShaohua Li * select a random position to start with to help wear leveling 31782a8f9449SShaohua Li * SSD 31792a8f9449SShaohua Li */ 31802a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 3181235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 31822a8f9449SShaohua Li 318354f180d3SHuang Ying cluster_info = kvzalloc(nr_cluster * sizeof(*cluster_info), 318454f180d3SHuang Ying GFP_KERNEL); 31852a8f9449SShaohua Li if (!cluster_info) { 31862a8f9449SShaohua Li error = -ENOMEM; 31872a8f9449SShaohua Li goto bad_swap; 31882a8f9449SShaohua Li } 3189235b6217SHuang, Ying 3190235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 3191235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 3192235b6217SHuang, Ying 3193ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 3194ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 3195ebc2a1a6SShaohua Li error = -ENOMEM; 3196ebc2a1a6SShaohua Li goto bad_swap; 3197ebc2a1a6SShaohua Li } 31986f179af8SHugh Dickins for_each_possible_cpu(cpu) { 3199ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 32006f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 3201ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 3202ebc2a1a6SShaohua Li } 320381a0298bSHuang Ying } else 320481a0298bSHuang Ying atomic_inc(&nr_rotate_swap); 32051da177e4SLinus Torvalds 32061421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 32071421ef3cSCesar Eduardo Barros if (error) 32081421ef3cSCesar Eduardo Barros goto bad_swap; 32091421ef3cSCesar Eduardo Barros 3210915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 32112a8f9449SShaohua Li cluster_info, maxpages, &span); 3212915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 321353092a74SHugh Dickins error = nr_extents; 3214e2244ec2SHugh Dickins goto bad_swap; 321553092a74SHugh Dickins } 321638b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 32178ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 321854f180d3SHuang Ying frontswap_map = kvzalloc(BITS_TO_LONGS(maxpages) * sizeof(long), 321954f180d3SHuang Ying GFP_KERNEL); 32201da177e4SLinus Torvalds 32212a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 3222dcf6b7ddSRafael Aquini /* 3223dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 3224dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 3225dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 3226dcf6b7ddSRafael Aquini * swapon(8) releases. 3227dcf6b7ddSRafael Aquini */ 3228dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 3229dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 3230dcf6b7ddSRafael Aquini 3231dcf6b7ddSRafael Aquini /* 3232dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 3233dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 3234dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 3235dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 3236dcf6b7ddSRafael Aquini */ 3237dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 3238dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 3239dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 3240dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 3241dcf6b7ddSRafael Aquini 3242dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 3243dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 3244dcf6b7ddSRafael Aquini int err = discard_swap(p); 3245dcf6b7ddSRafael Aquini if (unlikely(err)) 3246465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 3247dcf6b7ddSRafael Aquini p, err); 3248dcf6b7ddSRafael Aquini } 3249dcf6b7ddSRafael Aquini } 32506a6ba831SHugh Dickins 32514b3ef9daSHuang, Ying error = init_swap_address_space(p->type, maxpages); 32524b3ef9daSHuang, Ying if (error) 32534b3ef9daSHuang, Ying goto bad_swap; 32544b3ef9daSHuang, Ying 3255fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 325640531542SCesar Eduardo Barros prio = -1; 325778ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 325840531542SCesar Eduardo Barros prio = 325978ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 32602a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 3261c69dbfb8SCesar Eduardo Barros 3262756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 326391a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 3264c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 3265c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 326638b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 3267dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 3268dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 326938b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 3270c69dbfb8SCesar Eduardo Barros 3271fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 327266d7dd51SKay Sievers atomic_inc(&proc_poll_event); 327366d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 327466d7dd51SKay Sievers 32759b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 32769b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 32771da177e4SLinus Torvalds error = 0; 32781da177e4SLinus Torvalds goto out; 32791da177e4SLinus Torvalds bad_swap: 3280ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 3281ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 3282bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 3283f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 3284f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 32851da177e4SLinus Torvalds } 32864cd3bb10SHugh Dickins destroy_swap_extents(p); 3287e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 32885d337b91SHugh Dickins spin_lock(&swap_lock); 32891da177e4SLinus Torvalds p->swap_file = NULL; 32901da177e4SLinus Torvalds p->flags = 0; 32915d337b91SHugh Dickins spin_unlock(&swap_lock); 32921da177e4SLinus Torvalds vfree(swap_map); 3293*8606a1a9SDarrick J. Wong kvfree(cluster_info); 329452c50567SMel Gorman if (swap_file) { 32952130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 32965955102cSAl Viro inode_unlock(inode); 32972130781eSCesar Eduardo Barros inode = NULL; 32982130781eSCesar Eduardo Barros } 32991da177e4SLinus Torvalds filp_close(swap_file, NULL); 330052c50567SMel Gorman } 33011da177e4SLinus Torvalds out: 33021da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 33031da177e4SLinus Torvalds kunmap(page); 330409cbfeafSKirill A. Shutemov put_page(page); 33051da177e4SLinus Torvalds } 33061da177e4SLinus Torvalds if (name) 33071da177e4SLinus Torvalds putname(name); 33089b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 33095955102cSAl Viro inode_unlock(inode); 3310039939a6STim Chen if (!error) 3311039939a6STim Chen enable_swap_slots_cache(); 33121da177e4SLinus Torvalds return error; 33131da177e4SLinus Torvalds } 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 33161da177e4SLinus Torvalds { 3317efa90a98SHugh Dickins unsigned int type; 33181da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 33191da177e4SLinus Torvalds 33205d337b91SHugh Dickins spin_lock(&swap_lock); 3321efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 3322efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 3323efa90a98SHugh Dickins 3324efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 3325efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 33261da177e4SLinus Torvalds } 3327ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 33281da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 33295d337b91SHugh Dickins spin_unlock(&swap_lock); 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 33321da177e4SLinus Torvalds /* 33331da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 33341da177e4SLinus Torvalds * 3335355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 3336355cfa73SKAMEZAWA Hiroyuki * - success -> 0 3337355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 3338355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 3339355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 3340355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 3341570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 33421da177e4SLinus Torvalds */ 33438d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 33441da177e4SLinus Torvalds { 33451da177e4SLinus Torvalds struct swap_info_struct *p; 3346235b6217SHuang, Ying struct swap_cluster_info *ci; 33471da177e4SLinus Torvalds unsigned long offset, type; 33488d69aaeeSHugh Dickins unsigned char count; 33498d69aaeeSHugh Dickins unsigned char has_cache; 3350253d553bSHugh Dickins int err = -EINVAL; 33511da177e4SLinus Torvalds 3352a7420aa5SAndi Kleen if (non_swap_entry(entry)) 3353253d553bSHugh Dickins goto out; 33540697212aSChristoph Lameter 33551da177e4SLinus Torvalds type = swp_type(entry); 33561da177e4SLinus Torvalds if (type >= nr_swapfiles) 33571da177e4SLinus Torvalds goto bad_file; 3358efa90a98SHugh Dickins p = swap_info[type]; 33591da177e4SLinus Torvalds offset = swp_offset(entry); 3360355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 3361235b6217SHuang, Ying goto out; 3362235b6217SHuang, Ying 3363235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 3364355cfa73SKAMEZAWA Hiroyuki 3365253d553bSHugh Dickins count = p->swap_map[offset]; 3366edfe23daSShaohua Li 3367edfe23daSShaohua Li /* 3368edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 3369edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 3370edfe23daSShaohua Li */ 3371edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 3372edfe23daSShaohua Li err = -ENOENT; 3373edfe23daSShaohua Li goto unlock_out; 3374edfe23daSShaohua Li } 3375edfe23daSShaohua Li 3376253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 3377253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 3378253d553bSHugh Dickins err = 0; 3379355cfa73SKAMEZAWA Hiroyuki 3380253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 3381355cfa73SKAMEZAWA Hiroyuki 3382355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 3383253d553bSHugh Dickins if (!has_cache && count) 3384253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 3385253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 3386253d553bSHugh Dickins err = -EEXIST; 3387253d553bSHugh Dickins else /* no users remaining */ 3388253d553bSHugh Dickins err = -ENOENT; 3389355cfa73SKAMEZAWA Hiroyuki 3390355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 3391253d553bSHugh Dickins 3392570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 3393570a335bSHugh Dickins count += usage; 3394570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 3395253d553bSHugh Dickins err = -EINVAL; 3396570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 3397570a335bSHugh Dickins count = COUNT_CONTINUED; 3398570a335bSHugh Dickins else 3399570a335bSHugh Dickins err = -ENOMEM; 3400253d553bSHugh Dickins } else 3401253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 3402253d553bSHugh Dickins 3403253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 3404253d553bSHugh Dickins 3405355cfa73SKAMEZAWA Hiroyuki unlock_out: 3406235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 34071da177e4SLinus Torvalds out: 3408253d553bSHugh Dickins return err; 34091da177e4SLinus Torvalds 34101da177e4SLinus Torvalds bad_file: 3411465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 34121da177e4SLinus Torvalds goto out; 34131da177e4SLinus Torvalds } 3414253d553bSHugh Dickins 3415355cfa73SKAMEZAWA Hiroyuki /* 3416aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 3417aaa46865SHugh Dickins * (in which case its reference count is never incremented). 3418aaa46865SHugh Dickins */ 3419aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 3420aaa46865SHugh Dickins { 3421aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 3422aaa46865SHugh Dickins } 3423aaa46865SHugh Dickins 3424aaa46865SHugh Dickins /* 342508259d58SHugh Dickins * Increase reference count of swap entry by 1. 342608259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 342708259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 342808259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 342908259d58SHugh Dickins * might occur if a page table entry has got corrupted. 3430355cfa73SKAMEZAWA Hiroyuki */ 3431570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 3432355cfa73SKAMEZAWA Hiroyuki { 3433570a335bSHugh Dickins int err = 0; 3434570a335bSHugh Dickins 3435570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 3436570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 3437570a335bSHugh Dickins return err; 3438355cfa73SKAMEZAWA Hiroyuki } 34391da177e4SLinus Torvalds 3440cb4b86baSKAMEZAWA Hiroyuki /* 3441355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 3442355cfa73SKAMEZAWA Hiroyuki * 344373c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 3444355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 3445355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 3446355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 3447cb4b86baSKAMEZAWA Hiroyuki */ 3448cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 3449cb4b86baSKAMEZAWA Hiroyuki { 3450253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 3451cb4b86baSKAMEZAWA Hiroyuki } 3452cb4b86baSKAMEZAWA Hiroyuki 3453f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 3454f981c595SMel Gorman { 3455f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3456f981c595SMel Gorman return swap_info[swp_type(swap)]; 3457f981c595SMel Gorman } 3458f981c595SMel Gorman 3459f981c595SMel Gorman /* 3460f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 3461f981c595SMel Gorman */ 3462f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 3463f981c595SMel Gorman { 3464309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3465f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 3466f981c595SMel Gorman } 3467f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 3468f981c595SMel Gorman 3469f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 3470f981c595SMel Gorman { 3471f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3472309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 3473f981c595SMel Gorman return swp_offset(swap); 3474f981c595SMel Gorman } 3475f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 3476f981c595SMel Gorman 34771da177e4SLinus Torvalds /* 3478570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 3479570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 3480570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 3481570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 3482570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 3483570a335bSHugh Dickins * 3484570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 3485570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 3486570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 3487570a335bSHugh Dickins * 3488570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 3489570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 3490570a335bSHugh Dickins * can be called after dropping locks. 3491570a335bSHugh Dickins */ 3492570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 3493570a335bSHugh Dickins { 3494570a335bSHugh Dickins struct swap_info_struct *si; 3495235b6217SHuang, Ying struct swap_cluster_info *ci; 3496570a335bSHugh Dickins struct page *head; 3497570a335bSHugh Dickins struct page *page; 3498570a335bSHugh Dickins struct page *list_page; 3499570a335bSHugh Dickins pgoff_t offset; 3500570a335bSHugh Dickins unsigned char count; 3501570a335bSHugh Dickins 3502570a335bSHugh Dickins /* 3503570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 3504570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 3505570a335bSHugh Dickins */ 3506570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 3507570a335bSHugh Dickins 3508570a335bSHugh Dickins si = swap_info_get(entry); 3509570a335bSHugh Dickins if (!si) { 3510570a335bSHugh Dickins /* 3511570a335bSHugh Dickins * An acceptable race has occurred since the failing 3512570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 3513570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 3514570a335bSHugh Dickins */ 3515570a335bSHugh Dickins goto outer; 3516570a335bSHugh Dickins } 3517570a335bSHugh Dickins 3518570a335bSHugh Dickins offset = swp_offset(entry); 3519235b6217SHuang, Ying 3520235b6217SHuang, Ying ci = lock_cluster(si, offset); 3521235b6217SHuang, Ying 3522570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 3523570a335bSHugh Dickins 3524570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 3525570a335bSHugh Dickins /* 3526570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 3527570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 3528570a335bSHugh Dickins * over-provisioning. 3529570a335bSHugh Dickins */ 3530570a335bSHugh Dickins goto out; 3531570a335bSHugh Dickins } 3532570a335bSHugh Dickins 3533570a335bSHugh Dickins if (!page) { 3534235b6217SHuang, Ying unlock_cluster(ci); 3535ec8acf20SShaohua Li spin_unlock(&si->lock); 3536570a335bSHugh Dickins return -ENOMEM; 3537570a335bSHugh Dickins } 3538570a335bSHugh Dickins 3539570a335bSHugh Dickins /* 3540570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 3541570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 3542570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 3543570a335bSHugh Dickins */ 3544570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3545570a335bSHugh Dickins offset &= ~PAGE_MASK; 3546570a335bSHugh Dickins 3547570a335bSHugh Dickins /* 3548570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 3549570a335bSHugh Dickins * but it does always reset its private field. 3550570a335bSHugh Dickins */ 3551570a335bSHugh Dickins if (!page_private(head)) { 3552570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3553570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 3554570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 3555570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 3556570a335bSHugh Dickins } 3557570a335bSHugh Dickins 3558570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 3559570a335bSHugh Dickins unsigned char *map; 3560570a335bSHugh Dickins 3561570a335bSHugh Dickins /* 3562570a335bSHugh Dickins * If the previous map said no continuation, but we've found 3563570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 3564570a335bSHugh Dickins */ 3565570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 3566570a335bSHugh Dickins goto out; 3567570a335bSHugh Dickins 35689b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 3569570a335bSHugh Dickins count = *map; 35709b04c5feSCong Wang kunmap_atomic(map); 3571570a335bSHugh Dickins 3572570a335bSHugh Dickins /* 3573570a335bSHugh Dickins * If this continuation count now has some space in it, 3574570a335bSHugh Dickins * free our allocation and use this one. 3575570a335bSHugh Dickins */ 3576570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 3577570a335bSHugh Dickins goto out; 3578570a335bSHugh Dickins } 3579570a335bSHugh Dickins 3580570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 3581570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 3582570a335bSHugh Dickins out: 3583235b6217SHuang, Ying unlock_cluster(ci); 3584ec8acf20SShaohua Li spin_unlock(&si->lock); 3585570a335bSHugh Dickins outer: 3586570a335bSHugh Dickins if (page) 3587570a335bSHugh Dickins __free_page(page); 3588570a335bSHugh Dickins return 0; 3589570a335bSHugh Dickins } 3590570a335bSHugh Dickins 3591570a335bSHugh Dickins /* 3592570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3593570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3594570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3595570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3596570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3597235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3598235b6217SHuang, Ying * lock. 3599570a335bSHugh Dickins */ 3600570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3601570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3602570a335bSHugh Dickins { 3603570a335bSHugh Dickins struct page *head; 3604570a335bSHugh Dickins struct page *page; 3605570a335bSHugh Dickins unsigned char *map; 3606570a335bSHugh Dickins 3607570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3608570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3609570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3610570a335bSHugh Dickins return false; /* need to add count continuation */ 3611570a335bSHugh Dickins } 3612570a335bSHugh Dickins 3613570a335bSHugh Dickins offset &= ~PAGE_MASK; 3614570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 36159b04c5feSCong Wang map = kmap_atomic(page) + offset; 3616570a335bSHugh Dickins 3617570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3618570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3619570a335bSHugh Dickins 3620570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3621570a335bSHugh Dickins /* 3622570a335bSHugh Dickins * Think of how you add 1 to 999 3623570a335bSHugh Dickins */ 3624570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 36259b04c5feSCong Wang kunmap_atomic(map); 3626570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3627570a335bSHugh Dickins BUG_ON(page == head); 36289b04c5feSCong Wang map = kmap_atomic(page) + offset; 3629570a335bSHugh Dickins } 3630570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 36319b04c5feSCong Wang kunmap_atomic(map); 3632570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3633570a335bSHugh Dickins if (page == head) 3634570a335bSHugh Dickins return false; /* add count continuation */ 36359b04c5feSCong Wang map = kmap_atomic(page) + offset; 3636570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3637570a335bSHugh Dickins } 3638570a335bSHugh Dickins *map += 1; 36399b04c5feSCong Wang kunmap_atomic(map); 3640570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3641570a335bSHugh Dickins while (page != head) { 36429b04c5feSCong Wang map = kmap_atomic(page) + offset; 3643570a335bSHugh Dickins *map = COUNT_CONTINUED; 36449b04c5feSCong Wang kunmap_atomic(map); 3645570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3646570a335bSHugh Dickins } 3647570a335bSHugh Dickins return true; /* incremented */ 3648570a335bSHugh Dickins 3649570a335bSHugh Dickins } else { /* decrementing */ 3650570a335bSHugh Dickins /* 3651570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3652570a335bSHugh Dickins */ 3653570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3654570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 36559b04c5feSCong Wang kunmap_atomic(map); 3656570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 3657570a335bSHugh Dickins BUG_ON(page == head); 36589b04c5feSCong Wang map = kmap_atomic(page) + offset; 3659570a335bSHugh Dickins } 3660570a335bSHugh Dickins BUG_ON(*map == 0); 3661570a335bSHugh Dickins *map -= 1; 3662570a335bSHugh Dickins if (*map == 0) 3663570a335bSHugh Dickins count = 0; 36649b04c5feSCong Wang kunmap_atomic(map); 3665570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3666570a335bSHugh Dickins while (page != head) { 36679b04c5feSCong Wang map = kmap_atomic(page) + offset; 3668570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3669570a335bSHugh Dickins count = COUNT_CONTINUED; 36709b04c5feSCong Wang kunmap_atomic(map); 3671570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 3672570a335bSHugh Dickins } 3673570a335bSHugh Dickins return count == COUNT_CONTINUED; 3674570a335bSHugh Dickins } 3675570a335bSHugh Dickins } 3676570a335bSHugh Dickins 3677570a335bSHugh Dickins /* 3678570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3679570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3680570a335bSHugh Dickins */ 3681570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3682570a335bSHugh Dickins { 3683570a335bSHugh Dickins pgoff_t offset; 3684570a335bSHugh Dickins 3685570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3686570a335bSHugh Dickins struct page *head; 3687570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3688570a335bSHugh Dickins if (page_private(head)) { 36890d576d20SGeliang Tang struct page *page, *next; 36900d576d20SGeliang Tang 36910d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 36920d576d20SGeliang Tang list_del(&page->lru); 3693570a335bSHugh Dickins __free_page(page); 3694570a335bSHugh Dickins } 3695570a335bSHugh Dickins } 3696570a335bSHugh Dickins } 3697570a335bSHugh Dickins } 3698a2468cc9SAaron Lu 3699a2468cc9SAaron Lu static int __init swapfile_init(void) 3700a2468cc9SAaron Lu { 3701a2468cc9SAaron Lu int nid; 3702a2468cc9SAaron Lu 3703a2468cc9SAaron Lu swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head), 3704a2468cc9SAaron Lu GFP_KERNEL); 3705a2468cc9SAaron Lu if (!swap_avail_heads) { 3706a2468cc9SAaron Lu pr_emerg("Not enough memory for swap heads, swap is disabled\n"); 3707a2468cc9SAaron Lu return -ENOMEM; 3708a2468cc9SAaron Lu } 3709a2468cc9SAaron Lu 3710a2468cc9SAaron Lu for_each_node(nid) 3711a2468cc9SAaron Lu plist_head_init(&swap_avail_heads[nid]); 3712a2468cc9SAaron Lu 3713a2468cc9SAaron Lu return 0; 3714a2468cc9SAaron Lu } 3715a2468cc9SAaron Lu subsys_initcall(swapfile_init); 3716