1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/mm/swapfile.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 61da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/mm.h> 106e84f315SIngo Molnar #include <linux/sched/mm.h> 1129930025SIngo Molnar #include <linux/sched/task.h> 121da177e4SLinus Torvalds #include <linux/hugetlb.h> 131da177e4SLinus Torvalds #include <linux/mman.h> 141da177e4SLinus Torvalds #include <linux/slab.h> 151da177e4SLinus Torvalds #include <linux/kernel_stat.h> 161da177e4SLinus Torvalds #include <linux/swap.h> 171da177e4SLinus Torvalds #include <linux/vmalloc.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 191da177e4SLinus Torvalds #include <linux/namei.h> 20072441e2SHugh Dickins #include <linux/shmem_fs.h> 211da177e4SLinus Torvalds #include <linux/blkdev.h> 2220137a49SHugh Dickins #include <linux/random.h> 231da177e4SLinus Torvalds #include <linux/writeback.h> 241da177e4SLinus Torvalds #include <linux/proc_fs.h> 251da177e4SLinus Torvalds #include <linux/seq_file.h> 261da177e4SLinus Torvalds #include <linux/init.h> 275ad64688SHugh Dickins #include <linux/ksm.h> 281da177e4SLinus Torvalds #include <linux/rmap.h> 291da177e4SLinus Torvalds #include <linux/security.h> 301da177e4SLinus Torvalds #include <linux/backing-dev.h> 31fc0abb14SIngo Molnar #include <linux/mutex.h> 32c59ede7bSRandy.Dunlap #include <linux/capability.h> 331da177e4SLinus Torvalds #include <linux/syscalls.h> 348a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3566d7dd51SKay Sievers #include <linux/poll.h> 3672788c38SDavid Rientjes #include <linux/oom.h> 3738b5faf4SDan Magenheimer #include <linux/frontswap.h> 3838b5faf4SDan Magenheimer #include <linux/swapfile.h> 39f981c595SMel Gorman #include <linux/export.h> 4067afa38eSTim Chen #include <linux/swap_slots.h> 41155b5f88SHuang Ying #include <linux/sort.h> 421da177e4SLinus Torvalds 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 *); 50570a335bSHugh Dickins 5138b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 527c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 53ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 54fb0fec50SChris Wilson /* 55fb0fec50SChris Wilson * Some modules use swappable objects and may try to swap them out under 56fb0fec50SChris Wilson * memory pressure (via the shrinker). Before doing so, they may wish to 57fb0fec50SChris Wilson * check to see if any swap space is available. 58fb0fec50SChris Wilson */ 59fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages); 60ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 611da177e4SLinus Torvalds long total_swap_pages; 62a2468cc9SAaron Lu static int least_priority = -1; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 651da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 661da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 671da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 681da177e4SLinus Torvalds 69adfab836SDan Streetman /* 70adfab836SDan Streetman * all active swap_info_structs 71adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 72adfab836SDan Streetman */ 7318ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 7418ab4d4cSDan Streetman 7518ab4d4cSDan Streetman /* 7618ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 7718ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 7818ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 7918ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 8018ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 8118ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 8218ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 8318ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 8418ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 8518ab4d4cSDan Streetman * before any swap_info_struct->lock. 8618ab4d4cSDan Streetman */ 87bfc6b1caSColin Ian King static struct plist_head *swap_avail_heads; 8818ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 891da177e4SLinus Torvalds 9038b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 911da177e4SLinus Torvalds 92fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 931da177e4SLinus Torvalds 9466d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 9566d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 9666d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 9766d7dd51SKay Sievers 9881a0298bSHuang Ying atomic_t nr_rotate_swap = ATOMIC_INIT(0); 9981a0298bSHuang Ying 100c10d38ccSDaniel Jordan static struct swap_info_struct *swap_type_to_swap_info(int type) 101c10d38ccSDaniel Jordan { 102c10d38ccSDaniel Jordan if (type >= READ_ONCE(nr_swapfiles)) 103c10d38ccSDaniel Jordan return NULL; 104c10d38ccSDaniel Jordan 105c10d38ccSDaniel Jordan smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */ 106c10d38ccSDaniel Jordan return READ_ONCE(swap_info[type]); 107c10d38ccSDaniel Jordan } 108c10d38ccSDaniel Jordan 1098d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 110355cfa73SKAMEZAWA Hiroyuki { 111955c97f0SDaniel Jordan return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */ 112355cfa73SKAMEZAWA Hiroyuki } 113355cfa73SKAMEZAWA Hiroyuki 114bcd49e86SHuang Ying /* Reclaim the swap entry anyway if possible */ 115bcd49e86SHuang Ying #define TTRS_ANYWAY 0x1 116bcd49e86SHuang Ying /* 117bcd49e86SHuang Ying * Reclaim the swap entry if there are no more mappings of the 118bcd49e86SHuang Ying * corresponding page 119bcd49e86SHuang Ying */ 120bcd49e86SHuang Ying #define TTRS_UNMAPPED 0x2 121bcd49e86SHuang Ying /* Reclaim the swap entry if swap is getting full*/ 122bcd49e86SHuang Ying #define TTRS_FULL 0x4 123bcd49e86SHuang Ying 124efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 125bcd49e86SHuang Ying static int __try_to_reclaim_swap(struct swap_info_struct *si, 126bcd49e86SHuang Ying unsigned long offset, unsigned long flags) 127c9e44410SKAMEZAWA Hiroyuki { 128efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 129c9e44410SKAMEZAWA Hiroyuki struct page *page; 130c9e44410SKAMEZAWA Hiroyuki int ret = 0; 131c9e44410SKAMEZAWA Hiroyuki 132bcd49e86SHuang Ying page = find_get_page(swap_address_space(entry), offset); 133c9e44410SKAMEZAWA Hiroyuki if (!page) 134c9e44410SKAMEZAWA Hiroyuki return 0; 135c9e44410SKAMEZAWA Hiroyuki /* 136bcd49e86SHuang Ying * When this function is called from scan_swap_map_slots() and it's 137bcd49e86SHuang Ying * called by vmscan.c at reclaiming pages. So, we hold a lock on a page, 138bcd49e86SHuang Ying * here. We have to use trylock for avoiding deadlock. This is a special 139c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 140c9e44410SKAMEZAWA Hiroyuki * in usual operations. 141c9e44410SKAMEZAWA Hiroyuki */ 142c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 143bcd49e86SHuang Ying if ((flags & TTRS_ANYWAY) || 144bcd49e86SHuang Ying ((flags & TTRS_UNMAPPED) && !page_mapped(page)) || 145bcd49e86SHuang Ying ((flags & TTRS_FULL) && mem_cgroup_swap_full(page))) 146c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 147c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 148c9e44410SKAMEZAWA Hiroyuki } 14909cbfeafSKirill A. Shutemov put_page(page); 150c9e44410SKAMEZAWA Hiroyuki return ret; 151c9e44410SKAMEZAWA Hiroyuki } 152355cfa73SKAMEZAWA Hiroyuki 1534efaceb1SAaron Lu static inline struct swap_extent *first_se(struct swap_info_struct *sis) 1544efaceb1SAaron Lu { 1554efaceb1SAaron Lu struct rb_node *rb = rb_first(&sis->swap_extent_root); 1564efaceb1SAaron Lu return rb_entry(rb, struct swap_extent, rb_node); 1574efaceb1SAaron Lu } 1584efaceb1SAaron Lu 1594efaceb1SAaron Lu static inline struct swap_extent *next_se(struct swap_extent *se) 1604efaceb1SAaron Lu { 1614efaceb1SAaron Lu struct rb_node *rb = rb_next(&se->rb_node); 1624efaceb1SAaron Lu return rb ? rb_entry(rb, struct swap_extent, rb_node) : NULL; 1634efaceb1SAaron Lu } 1644efaceb1SAaron Lu 1651da177e4SLinus Torvalds /* 1666a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1676a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1686a6ba831SHugh Dickins */ 1696a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1706a6ba831SHugh Dickins { 1716a6ba831SHugh Dickins struct swap_extent *se; 1729625a5f2SHugh Dickins sector_t start_block; 1739625a5f2SHugh Dickins sector_t nr_blocks; 1746a6ba831SHugh Dickins int err = 0; 1756a6ba831SHugh Dickins 1766a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1774efaceb1SAaron Lu se = first_se(si); 1789625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1799625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1809625a5f2SHugh Dickins if (nr_blocks) { 1819625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 182dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1839625a5f2SHugh Dickins if (err) 1849625a5f2SHugh Dickins return err; 1859625a5f2SHugh Dickins cond_resched(); 1866a6ba831SHugh Dickins } 1876a6ba831SHugh Dickins 1884efaceb1SAaron Lu for (se = next_se(se); se; se = next_se(se)) { 1899625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1909625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1919625a5f2SHugh Dickins 1926a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 193dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1946a6ba831SHugh Dickins if (err) 1956a6ba831SHugh Dickins break; 1966a6ba831SHugh Dickins 1976a6ba831SHugh Dickins cond_resched(); 1986a6ba831SHugh Dickins } 1996a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 2006a6ba831SHugh Dickins } 2016a6ba831SHugh Dickins 2024efaceb1SAaron Lu static struct swap_extent * 2034efaceb1SAaron Lu offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset) 2044efaceb1SAaron Lu { 2054efaceb1SAaron Lu struct swap_extent *se; 2064efaceb1SAaron Lu struct rb_node *rb; 2074efaceb1SAaron Lu 2084efaceb1SAaron Lu rb = sis->swap_extent_root.rb_node; 2094efaceb1SAaron Lu while (rb) { 2104efaceb1SAaron Lu se = rb_entry(rb, struct swap_extent, rb_node); 2114efaceb1SAaron Lu if (offset < se->start_page) 2124efaceb1SAaron Lu rb = rb->rb_left; 2134efaceb1SAaron Lu else if (offset >= se->start_page + se->nr_pages) 2144efaceb1SAaron Lu rb = rb->rb_right; 2154efaceb1SAaron Lu else 2164efaceb1SAaron Lu return se; 2174efaceb1SAaron Lu } 2184efaceb1SAaron Lu /* It *must* be present */ 2194efaceb1SAaron Lu BUG(); 2204efaceb1SAaron Lu } 2214efaceb1SAaron Lu 222caf6912fSJens Axboe sector_t swap_page_sector(struct page *page) 223caf6912fSJens Axboe { 224caf6912fSJens Axboe struct swap_info_struct *sis = page_swap_info(page); 225caf6912fSJens Axboe struct swap_extent *se; 226caf6912fSJens Axboe sector_t sector; 227caf6912fSJens Axboe pgoff_t offset; 228caf6912fSJens Axboe 229caf6912fSJens Axboe offset = __page_file_index(page); 230caf6912fSJens Axboe se = offset_to_swap_extent(sis, offset); 231caf6912fSJens Axboe sector = se->start_block + (offset - se->start_page); 232caf6912fSJens Axboe return sector << (PAGE_SHIFT - 9); 233caf6912fSJens Axboe } 234caf6912fSJens Axboe 2357992fde7SHugh Dickins /* 2367992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 2377992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 2387992fde7SHugh Dickins */ 2397992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 2407992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 2417992fde7SHugh Dickins { 2424efaceb1SAaron Lu struct swap_extent *se = offset_to_swap_extent(si, start_page); 2437992fde7SHugh Dickins 2447992fde7SHugh Dickins while (nr_pages) { 2457992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 2467992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 247858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 2487992fde7SHugh Dickins 2497992fde7SHugh Dickins if (nr_blocks > nr_pages) 2507992fde7SHugh Dickins nr_blocks = nr_pages; 2517992fde7SHugh Dickins start_page += nr_blocks; 2527992fde7SHugh Dickins nr_pages -= nr_blocks; 2537992fde7SHugh Dickins 2547992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 2557992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 2567992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 257dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 2587992fde7SHugh Dickins break; 2597992fde7SHugh Dickins 2604efaceb1SAaron Lu se = next_se(se); 2617992fde7SHugh Dickins } 2627992fde7SHugh Dickins } 2637992fde7SHugh Dickins 26438d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 26538d8b4e6SHuang Ying #define SWAPFILE_CLUSTER HPAGE_PMD_NR 266a448f2d0SHuang Ying 267a448f2d0SHuang Ying #define swap_entry_size(size) (size) 26838d8b4e6SHuang Ying #else 269048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 270a448f2d0SHuang Ying 271a448f2d0SHuang Ying /* 272a448f2d0SHuang Ying * Define swap_entry_size() as constant to let compiler to optimize 273a448f2d0SHuang Ying * out some code if !CONFIG_THP_SWAP 274a448f2d0SHuang Ying */ 275a448f2d0SHuang Ying #define swap_entry_size(size) 1 27638d8b4e6SHuang Ying #endif 277048c27fdSHugh Dickins #define LATENCY_LIMIT 256 278048c27fdSHugh Dickins 2792a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2802a8f9449SShaohua Li unsigned int flag) 2812a8f9449SShaohua Li { 2822a8f9449SShaohua Li info->flags = flag; 2832a8f9449SShaohua Li } 2842a8f9449SShaohua Li 2852a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2862a8f9449SShaohua Li { 2872a8f9449SShaohua Li return info->data; 2882a8f9449SShaohua Li } 2892a8f9449SShaohua Li 2902a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2912a8f9449SShaohua Li unsigned int c) 2922a8f9449SShaohua Li { 2932a8f9449SShaohua Li info->data = c; 2942a8f9449SShaohua Li } 2952a8f9449SShaohua Li 2962a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2972a8f9449SShaohua Li unsigned int c, unsigned int f) 2982a8f9449SShaohua Li { 2992a8f9449SShaohua Li info->flags = f; 3002a8f9449SShaohua Li info->data = c; 3012a8f9449SShaohua Li } 3022a8f9449SShaohua Li 3032a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 3042a8f9449SShaohua Li { 3052a8f9449SShaohua Li return info->data; 3062a8f9449SShaohua Li } 3072a8f9449SShaohua Li 3082a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 3092a8f9449SShaohua Li unsigned int n) 3102a8f9449SShaohua Li { 3112a8f9449SShaohua Li info->data = n; 3122a8f9449SShaohua Li } 3132a8f9449SShaohua Li 3142a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 3152a8f9449SShaohua Li unsigned int n, unsigned int f) 3162a8f9449SShaohua Li { 3172a8f9449SShaohua Li info->flags = f; 3182a8f9449SShaohua Li info->data = n; 3192a8f9449SShaohua Li } 3202a8f9449SShaohua Li 3212a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 3222a8f9449SShaohua Li { 3232a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 3242a8f9449SShaohua Li } 3252a8f9449SShaohua Li 3262a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 3272a8f9449SShaohua Li { 3282a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 3292a8f9449SShaohua Li } 3302a8f9449SShaohua Li 3312a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 3322a8f9449SShaohua Li { 3332a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 3342a8f9449SShaohua Li info->data = 0; 3352a8f9449SShaohua Li } 3362a8f9449SShaohua Li 337e0709829SHuang Ying static inline bool cluster_is_huge(struct swap_cluster_info *info) 338e0709829SHuang Ying { 33933ee011eSHuang Ying if (IS_ENABLED(CONFIG_THP_SWAP)) 340e0709829SHuang Ying return info->flags & CLUSTER_FLAG_HUGE; 34133ee011eSHuang Ying return false; 342e0709829SHuang Ying } 343e0709829SHuang Ying 344e0709829SHuang Ying static inline void cluster_clear_huge(struct swap_cluster_info *info) 345e0709829SHuang Ying { 346e0709829SHuang Ying info->flags &= ~CLUSTER_FLAG_HUGE; 347e0709829SHuang Ying } 348e0709829SHuang Ying 349235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 350235b6217SHuang, Ying unsigned long offset) 351235b6217SHuang, Ying { 352235b6217SHuang, Ying struct swap_cluster_info *ci; 353235b6217SHuang, Ying 354235b6217SHuang, Ying ci = si->cluster_info; 355235b6217SHuang, Ying if (ci) { 356235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 357235b6217SHuang, Ying spin_lock(&ci->lock); 358235b6217SHuang, Ying } 359235b6217SHuang, Ying return ci; 360235b6217SHuang, Ying } 361235b6217SHuang, Ying 362235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 363235b6217SHuang, Ying { 364235b6217SHuang, Ying if (ci) 365235b6217SHuang, Ying spin_unlock(&ci->lock); 366235b6217SHuang, Ying } 367235b6217SHuang, Ying 36859d98bf3SHuang Ying /* 36959d98bf3SHuang Ying * Determine the locking method in use for this device. Return 37059d98bf3SHuang Ying * swap_cluster_info if SSD-style cluster-based locking is in place. 37159d98bf3SHuang Ying */ 372235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 37359d98bf3SHuang Ying struct swap_info_struct *si, unsigned long offset) 374235b6217SHuang, Ying { 375235b6217SHuang, Ying struct swap_cluster_info *ci; 376235b6217SHuang, Ying 37759d98bf3SHuang Ying /* Try to use fine-grained SSD-style locking if available: */ 378235b6217SHuang, Ying ci = lock_cluster(si, offset); 37959d98bf3SHuang Ying /* Otherwise, fall back to traditional, coarse locking: */ 380235b6217SHuang, Ying if (!ci) 381235b6217SHuang, Ying spin_lock(&si->lock); 382235b6217SHuang, Ying 383235b6217SHuang, Ying return ci; 384235b6217SHuang, Ying } 385235b6217SHuang, Ying 386235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 387235b6217SHuang, Ying struct swap_cluster_info *ci) 388235b6217SHuang, Ying { 389235b6217SHuang, Ying if (ci) 390235b6217SHuang, Ying unlock_cluster(ci); 391235b6217SHuang, Ying else 392235b6217SHuang, Ying spin_unlock(&si->lock); 393235b6217SHuang, Ying } 394235b6217SHuang, Ying 3956b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3966b534915SHuang Ying { 3976b534915SHuang Ying return cluster_is_null(&list->head); 3986b534915SHuang Ying } 3996b534915SHuang Ying 4006b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 4016b534915SHuang Ying { 4026b534915SHuang Ying return cluster_next(&list->head); 4036b534915SHuang Ying } 4046b534915SHuang Ying 4056b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 4066b534915SHuang Ying { 4076b534915SHuang Ying cluster_set_null(&list->head); 4086b534915SHuang Ying cluster_set_null(&list->tail); 4096b534915SHuang Ying } 4106b534915SHuang Ying 4116b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 4126b534915SHuang Ying struct swap_cluster_info *ci, 4136b534915SHuang Ying unsigned int idx) 4146b534915SHuang Ying { 4156b534915SHuang Ying if (cluster_list_empty(list)) { 4166b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 4176b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 4186b534915SHuang Ying } else { 419235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 4206b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 4216b534915SHuang Ying 422235b6217SHuang, Ying /* 423235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 424235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 425235b6217SHuang, Ying */ 426235b6217SHuang, Ying ci_tail = ci + tail; 427235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 428235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 4290ef017d1SHuang Ying spin_unlock(&ci_tail->lock); 4306b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 4316b534915SHuang Ying } 4326b534915SHuang Ying } 4336b534915SHuang Ying 4346b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 4356b534915SHuang Ying struct swap_cluster_info *ci) 4366b534915SHuang Ying { 4376b534915SHuang Ying unsigned int idx; 4386b534915SHuang Ying 4396b534915SHuang Ying idx = cluster_next(&list->head); 4406b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 4416b534915SHuang Ying cluster_set_null(&list->head); 4426b534915SHuang Ying cluster_set_null(&list->tail); 4436b534915SHuang Ying } else 4446b534915SHuang Ying cluster_set_next_flag(&list->head, 4456b534915SHuang Ying cluster_next(&ci[idx]), 0); 4466b534915SHuang Ying 4476b534915SHuang Ying return idx; 4486b534915SHuang Ying } 4496b534915SHuang Ying 450815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 451815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 452815c2c54SShaohua Li unsigned int idx) 453815c2c54SShaohua Li { 454815c2c54SShaohua Li /* 455815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 456815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 457815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 458815c2c54SShaohua Li * will be cleared after discard 459815c2c54SShaohua Li */ 460815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 461815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 462815c2c54SShaohua Li 4636b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 464815c2c54SShaohua Li 465815c2c54SShaohua Li schedule_work(&si->discard_work); 466815c2c54SShaohua Li } 467815c2c54SShaohua Li 46838d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx) 46938d8b4e6SHuang Ying { 47038d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 47138d8b4e6SHuang Ying 47238d8b4e6SHuang Ying cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE); 47338d8b4e6SHuang Ying cluster_list_add_tail(&si->free_clusters, ci, idx); 47438d8b4e6SHuang Ying } 47538d8b4e6SHuang Ying 476815c2c54SShaohua Li /* 477815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 478815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 479815c2c54SShaohua Li */ 480815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 481815c2c54SShaohua Li { 482235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 483815c2c54SShaohua Li unsigned int idx; 484815c2c54SShaohua Li 485815c2c54SShaohua Li info = si->cluster_info; 486815c2c54SShaohua Li 4876b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 4886b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 489815c2c54SShaohua Li spin_unlock(&si->lock); 490815c2c54SShaohua Li 491815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 492815c2c54SShaohua Li SWAPFILE_CLUSTER); 493815c2c54SShaohua Li 494815c2c54SShaohua Li spin_lock(&si->lock); 495235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 49638d8b4e6SHuang Ying __free_cluster(si, idx); 497815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 498815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 499235b6217SHuang, Ying unlock_cluster(ci); 500815c2c54SShaohua Li } 501815c2c54SShaohua Li } 502815c2c54SShaohua Li 503815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 504815c2c54SShaohua Li { 505815c2c54SShaohua Li struct swap_info_struct *si; 506815c2c54SShaohua Li 507815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 508815c2c54SShaohua Li 509815c2c54SShaohua Li spin_lock(&si->lock); 510815c2c54SShaohua Li swap_do_scheduled_discard(si); 511815c2c54SShaohua Li spin_unlock(&si->lock); 512815c2c54SShaohua Li } 513815c2c54SShaohua Li 51438d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx) 51538d8b4e6SHuang Ying { 51638d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 51738d8b4e6SHuang Ying 51838d8b4e6SHuang Ying VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx); 51938d8b4e6SHuang Ying cluster_list_del_first(&si->free_clusters, ci); 52038d8b4e6SHuang Ying cluster_set_count_flag(ci + idx, 0, 0); 52138d8b4e6SHuang Ying } 52238d8b4e6SHuang Ying 52338d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx) 52438d8b4e6SHuang Ying { 52538d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info + idx; 52638d8b4e6SHuang Ying 52738d8b4e6SHuang Ying VM_BUG_ON(cluster_count(ci) != 0); 52838d8b4e6SHuang Ying /* 52938d8b4e6SHuang Ying * If the swap is discardable, prepare discard the cluster 53038d8b4e6SHuang Ying * instead of free it immediately. The cluster will be freed 53138d8b4e6SHuang Ying * after discard. 53238d8b4e6SHuang Ying */ 53338d8b4e6SHuang Ying if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 53438d8b4e6SHuang Ying (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 53538d8b4e6SHuang Ying swap_cluster_schedule_discard(si, idx); 53638d8b4e6SHuang Ying return; 53738d8b4e6SHuang Ying } 53838d8b4e6SHuang Ying 53938d8b4e6SHuang Ying __free_cluster(si, idx); 54038d8b4e6SHuang Ying } 54138d8b4e6SHuang Ying 5422a8f9449SShaohua Li /* 5432a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 5442a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 5452a8f9449SShaohua Li */ 5462a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 5472a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 5482a8f9449SShaohua Li { 5492a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 5502a8f9449SShaohua Li 5512a8f9449SShaohua Li if (!cluster_info) 5522a8f9449SShaohua Li return; 55338d8b4e6SHuang Ying if (cluster_is_free(&cluster_info[idx])) 55438d8b4e6SHuang Ying alloc_cluster(p, idx); 5552a8f9449SShaohua Li 5562a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 5572a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 5582a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 5592a8f9449SShaohua Li } 5602a8f9449SShaohua Li 5612a8f9449SShaohua Li /* 5622a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 5632a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 5642a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 5652a8f9449SShaohua Li */ 5662a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 5672a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 5682a8f9449SShaohua Li { 5692a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 5702a8f9449SShaohua Li 5712a8f9449SShaohua Li if (!cluster_info) 5722a8f9449SShaohua Li return; 5732a8f9449SShaohua Li 5742a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 5752a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 5762a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 5772a8f9449SShaohua Li 57838d8b4e6SHuang Ying if (cluster_count(&cluster_info[idx]) == 0) 57938d8b4e6SHuang Ying free_cluster(p, idx); 5802a8f9449SShaohua Li } 5812a8f9449SShaohua Li 5822a8f9449SShaohua Li /* 5832a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 5842a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 5852a8f9449SShaohua Li */ 586ebc2a1a6SShaohua Li static bool 587ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 5882a8f9449SShaohua Li unsigned long offset) 5892a8f9449SShaohua Li { 590ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 591ebc2a1a6SShaohua Li bool conflict; 592ebc2a1a6SShaohua Li 5932a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 5946b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 5956b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 5962a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 597ebc2a1a6SShaohua Li 598ebc2a1a6SShaohua Li if (!conflict) 599ebc2a1a6SShaohua Li return false; 600ebc2a1a6SShaohua Li 601ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 602ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 603ebc2a1a6SShaohua Li return true; 604ebc2a1a6SShaohua Li } 605ebc2a1a6SShaohua Li 606ebc2a1a6SShaohua Li /* 607ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 608ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 609ebc2a1a6SShaohua Li */ 61036005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 611ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 612ebc2a1a6SShaohua Li { 613ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 614235b6217SHuang, Ying struct swap_cluster_info *ci; 615235b6217SHuang, Ying unsigned long tmp, max; 616ebc2a1a6SShaohua Li 617ebc2a1a6SShaohua Li new_cluster: 618ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 619ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 6206b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 6216b534915SHuang Ying cluster->index = si->free_clusters.head; 622ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 623ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 6246b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 625ebc2a1a6SShaohua Li /* 626ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 62749070588SHuang Ying * discarding, do discard now and reclaim them, then 62849070588SHuang Ying * reread cluster_next_cpu since we dropped si->lock 629ebc2a1a6SShaohua Li */ 630ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 63149070588SHuang Ying *scan_base = this_cpu_read(*si->cluster_next_cpu); 63249070588SHuang Ying *offset = *scan_base; 633ebc2a1a6SShaohua Li goto new_cluster; 634ebc2a1a6SShaohua Li } else 63536005baeSTim Chen return false; 636ebc2a1a6SShaohua Li } 637ebc2a1a6SShaohua Li 638ebc2a1a6SShaohua Li /* 639ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 640ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 641ebc2a1a6SShaohua Li */ 642ebc2a1a6SShaohua Li tmp = cluster->next; 643235b6217SHuang, Ying max = min_t(unsigned long, si->max, 644235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 6457b9e2de1SWei Yang if (tmp < max) { 646235b6217SHuang, Ying ci = lock_cluster(si, tmp); 647235b6217SHuang, Ying while (tmp < max) { 6480fd0e19eSWei Yang if (!si->swap_map[tmp]) 649ebc2a1a6SShaohua Li break; 650ebc2a1a6SShaohua Li tmp++; 651ebc2a1a6SShaohua Li } 652235b6217SHuang, Ying unlock_cluster(ci); 6537b9e2de1SWei Yang } 6540fd0e19eSWei Yang if (tmp >= max) { 655ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 656ebc2a1a6SShaohua Li goto new_cluster; 657ebc2a1a6SShaohua Li } 658ebc2a1a6SShaohua Li cluster->next = tmp + 1; 659ebc2a1a6SShaohua Li *offset = tmp; 660ebc2a1a6SShaohua Li *scan_base = tmp; 661fdff1debSWei Yang return true; 6622a8f9449SShaohua Li } 6632a8f9449SShaohua Li 664a2468cc9SAaron Lu static void __del_from_avail_list(struct swap_info_struct *p) 665a2468cc9SAaron Lu { 666a2468cc9SAaron Lu int nid; 667a2468cc9SAaron Lu 668a2468cc9SAaron Lu for_each_node(nid) 669a2468cc9SAaron Lu plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]); 670a2468cc9SAaron Lu } 671a2468cc9SAaron Lu 672a2468cc9SAaron Lu static void del_from_avail_list(struct swap_info_struct *p) 673a2468cc9SAaron Lu { 674a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 675a2468cc9SAaron Lu __del_from_avail_list(p); 676a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 677a2468cc9SAaron Lu } 678a2468cc9SAaron Lu 67938d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, 68038d8b4e6SHuang Ying unsigned int nr_entries) 68138d8b4e6SHuang Ying { 68238d8b4e6SHuang Ying unsigned int end = offset + nr_entries - 1; 68338d8b4e6SHuang Ying 68438d8b4e6SHuang Ying if (offset == si->lowest_bit) 68538d8b4e6SHuang Ying si->lowest_bit += nr_entries; 68638d8b4e6SHuang Ying if (end == si->highest_bit) 687a449bf58SQian Cai WRITE_ONCE(si->highest_bit, si->highest_bit - nr_entries); 68838d8b4e6SHuang Ying si->inuse_pages += nr_entries; 68938d8b4e6SHuang Ying if (si->inuse_pages == si->pages) { 69038d8b4e6SHuang Ying si->lowest_bit = si->max; 69138d8b4e6SHuang Ying si->highest_bit = 0; 692a2468cc9SAaron Lu del_from_avail_list(si); 69338d8b4e6SHuang Ying } 69438d8b4e6SHuang Ying } 69538d8b4e6SHuang Ying 696a2468cc9SAaron Lu static void add_to_avail_list(struct swap_info_struct *p) 697a2468cc9SAaron Lu { 698a2468cc9SAaron Lu int nid; 699a2468cc9SAaron Lu 700a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 701a2468cc9SAaron Lu for_each_node(nid) { 702a2468cc9SAaron Lu WARN_ON(!plist_node_empty(&p->avail_lists[nid])); 703a2468cc9SAaron Lu plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]); 704a2468cc9SAaron Lu } 705a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 706a2468cc9SAaron Lu } 707a2468cc9SAaron Lu 70838d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset, 70938d8b4e6SHuang Ying unsigned int nr_entries) 71038d8b4e6SHuang Ying { 7113852f676SJoonsoo Kim unsigned long begin = offset; 71238d8b4e6SHuang Ying unsigned long end = offset + nr_entries - 1; 71338d8b4e6SHuang Ying void (*swap_slot_free_notify)(struct block_device *, unsigned long); 71438d8b4e6SHuang Ying 71538d8b4e6SHuang Ying if (offset < si->lowest_bit) 71638d8b4e6SHuang Ying si->lowest_bit = offset; 71738d8b4e6SHuang Ying if (end > si->highest_bit) { 71838d8b4e6SHuang Ying bool was_full = !si->highest_bit; 71938d8b4e6SHuang Ying 720a449bf58SQian Cai WRITE_ONCE(si->highest_bit, end); 721a2468cc9SAaron Lu if (was_full && (si->flags & SWP_WRITEOK)) 722a2468cc9SAaron Lu add_to_avail_list(si); 72338d8b4e6SHuang Ying } 72438d8b4e6SHuang Ying atomic_long_add(nr_entries, &nr_swap_pages); 72538d8b4e6SHuang Ying si->inuse_pages -= nr_entries; 72638d8b4e6SHuang Ying if (si->flags & SWP_BLKDEV) 72738d8b4e6SHuang Ying swap_slot_free_notify = 72838d8b4e6SHuang Ying si->bdev->bd_disk->fops->swap_slot_free_notify; 72938d8b4e6SHuang Ying else 73038d8b4e6SHuang Ying swap_slot_free_notify = NULL; 73138d8b4e6SHuang Ying while (offset <= end) { 7328a84802eSSteven Price arch_swap_invalidate_page(si->type, offset); 73338d8b4e6SHuang Ying frontswap_invalidate_page(si->type, offset); 73438d8b4e6SHuang Ying if (swap_slot_free_notify) 73538d8b4e6SHuang Ying swap_slot_free_notify(si->bdev, offset); 73638d8b4e6SHuang Ying offset++; 73738d8b4e6SHuang Ying } 7383852f676SJoonsoo Kim clear_shadow_from_swap_cache(si->type, begin, end); 73938d8b4e6SHuang Ying } 74038d8b4e6SHuang Ying 74149070588SHuang Ying static void set_cluster_next(struct swap_info_struct *si, unsigned long next) 74249070588SHuang Ying { 74349070588SHuang Ying unsigned long prev; 74449070588SHuang Ying 74549070588SHuang Ying if (!(si->flags & SWP_SOLIDSTATE)) { 74649070588SHuang Ying si->cluster_next = next; 74749070588SHuang Ying return; 74849070588SHuang Ying } 74949070588SHuang Ying 75049070588SHuang Ying prev = this_cpu_read(*si->cluster_next_cpu); 75149070588SHuang Ying /* 75249070588SHuang Ying * Cross the swap address space size aligned trunk, choose 75349070588SHuang Ying * another trunk randomly to avoid lock contention on swap 75449070588SHuang Ying * address space if possible. 75549070588SHuang Ying */ 75649070588SHuang Ying if ((prev >> SWAP_ADDRESS_SPACE_SHIFT) != 75749070588SHuang Ying (next >> SWAP_ADDRESS_SPACE_SHIFT)) { 75849070588SHuang Ying /* No free swap slots available */ 75949070588SHuang Ying if (si->highest_bit <= si->lowest_bit) 76049070588SHuang Ying return; 76149070588SHuang Ying next = si->lowest_bit + 76249070588SHuang Ying prandom_u32_max(si->highest_bit - si->lowest_bit + 1); 76349070588SHuang Ying next = ALIGN_DOWN(next, SWAP_ADDRESS_SPACE_PAGES); 76449070588SHuang Ying next = max_t(unsigned int, next, si->lowest_bit); 76549070588SHuang Ying } 76649070588SHuang Ying this_cpu_write(*si->cluster_next_cpu, next); 76749070588SHuang Ying } 76849070588SHuang Ying 76936005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si, 77036005baeSTim Chen unsigned char usage, int nr, 77136005baeSTim Chen swp_entry_t slots[]) 7721da177e4SLinus Torvalds { 773235b6217SHuang, Ying struct swap_cluster_info *ci; 774ebebbbe9SHugh Dickins unsigned long offset; 775c60aa176SHugh Dickins unsigned long scan_base; 7767992fde7SHugh Dickins unsigned long last_in_cluster = 0; 777048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 77836005baeSTim Chen int n_ret = 0; 779ed43af10SHuang Ying bool scanned_many = false; 78036005baeSTim Chen 7817dfad418SHugh Dickins /* 7827dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 7837dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 7847dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 7857dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 7867dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 7877dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 7887dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 789c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 7901da177e4SLinus Torvalds */ 7917dfad418SHugh Dickins 79252b7efdbSHugh Dickins si->flags += SWP_SCANNING; 79349070588SHuang Ying /* 79449070588SHuang Ying * Use percpu scan base for SSD to reduce lock contention on 79549070588SHuang Ying * cluster and swap cache. For HDD, sequential access is more 79649070588SHuang Ying * important. 79749070588SHuang Ying */ 79849070588SHuang Ying if (si->flags & SWP_SOLIDSTATE) 79949070588SHuang Ying scan_base = this_cpu_read(*si->cluster_next_cpu); 80049070588SHuang Ying else 80149070588SHuang Ying scan_base = si->cluster_next; 80249070588SHuang Ying offset = scan_base; 803ebebbbe9SHugh Dickins 804ebc2a1a6SShaohua Li /* SSD algorithm */ 805ebc2a1a6SShaohua Li if (si->cluster_info) { 806bd2d18daSWei Yang if (!scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 80736005baeSTim Chen goto scan; 808f4eaf51aSWei Yang } else if (unlikely(!si->cluster_nr--)) { 809ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 810ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 8112a8f9449SShaohua Li goto checks; 8122a8f9449SShaohua Li } 8132a8f9449SShaohua Li 814ec8acf20SShaohua Li spin_unlock(&si->lock); 8157dfad418SHugh Dickins 816c60aa176SHugh Dickins /* 817c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 818c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 81950088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 82050088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 821c60aa176SHugh Dickins */ 822c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 8237dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 8247dfad418SHugh Dickins 8257dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 8267dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 8271da177e4SLinus Torvalds if (si->swap_map[offset]) 8287dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 8297dfad418SHugh Dickins else if (offset == last_in_cluster) { 830ec8acf20SShaohua Li spin_lock(&si->lock); 831ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 832ebebbbe9SHugh Dickins si->cluster_next = offset; 833ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 834ebebbbe9SHugh Dickins goto checks; 8357dfad418SHugh Dickins } 836048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 837048c27fdSHugh Dickins cond_resched(); 838048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 839048c27fdSHugh Dickins } 8407dfad418SHugh Dickins } 841ebebbbe9SHugh Dickins 842c60aa176SHugh Dickins offset = scan_base; 843ec8acf20SShaohua Li spin_lock(&si->lock); 844ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 8457dfad418SHugh Dickins } 8467dfad418SHugh Dickins 847ebebbbe9SHugh Dickins checks: 848ebc2a1a6SShaohua Li if (si->cluster_info) { 84936005baeSTim Chen while (scan_swap_map_ssd_cluster_conflict(si, offset)) { 85036005baeSTim Chen /* take a break if we already got some slots */ 85136005baeSTim Chen if (n_ret) 85236005baeSTim Chen goto done; 85336005baeSTim Chen if (!scan_swap_map_try_ssd_cluster(si, &offset, 85436005baeSTim Chen &scan_base)) 85536005baeSTim Chen goto scan; 85636005baeSTim Chen } 857ebc2a1a6SShaohua Li } 858ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 85952b7efdbSHugh Dickins goto no_page; 8607dfad418SHugh Dickins if (!si->highest_bit) 8617dfad418SHugh Dickins goto no_page; 862ebebbbe9SHugh Dickins if (offset > si->highest_bit) 863c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 864c9e44410SKAMEZAWA Hiroyuki 865235b6217SHuang, Ying ci = lock_cluster(si, offset); 866b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 867b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 868c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 869235b6217SHuang, Ying unlock_cluster(ci); 870ec8acf20SShaohua Li spin_unlock(&si->lock); 871bcd49e86SHuang Ying swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); 872ec8acf20SShaohua Li spin_lock(&si->lock); 873c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 874c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 875c9e44410SKAMEZAWA Hiroyuki goto checks; 876c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 877c9e44410SKAMEZAWA Hiroyuki } 878c9e44410SKAMEZAWA Hiroyuki 879235b6217SHuang, Ying if (si->swap_map[offset]) { 880235b6217SHuang, Ying unlock_cluster(ci); 88136005baeSTim Chen if (!n_ret) 882ebebbbe9SHugh Dickins goto scan; 88336005baeSTim Chen else 88436005baeSTim Chen goto done; 885235b6217SHuang, Ying } 886a449bf58SQian Cai WRITE_ONCE(si->swap_map[offset], usage); 8872872bb2dSHuang Ying inc_cluster_info_page(si, si->cluster_info, offset); 8882872bb2dSHuang Ying unlock_cluster(ci); 889ebebbbe9SHugh Dickins 89038d8b4e6SHuang Ying swap_range_alloc(si, offset, 1); 89136005baeSTim Chen slots[n_ret++] = swp_entry(si->type, offset); 8927992fde7SHugh Dickins 89336005baeSTim Chen /* got enough slots or reach max slots? */ 89436005baeSTim Chen if ((n_ret == nr) || (offset >= si->highest_bit)) 89536005baeSTim Chen goto done; 89636005baeSTim Chen 89736005baeSTim Chen /* search for next available slot */ 89836005baeSTim Chen 89936005baeSTim Chen /* time to take a break? */ 90036005baeSTim Chen if (unlikely(--latency_ration < 0)) { 90136005baeSTim Chen if (n_ret) 90236005baeSTim Chen goto done; 90336005baeSTim Chen spin_unlock(&si->lock); 90436005baeSTim Chen cond_resched(); 90536005baeSTim Chen spin_lock(&si->lock); 90636005baeSTim Chen latency_ration = LATENCY_LIMIT; 90736005baeSTim Chen } 90836005baeSTim Chen 90936005baeSTim Chen /* try to get more slots in cluster */ 91036005baeSTim Chen if (si->cluster_info) { 91136005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 91236005baeSTim Chen goto checks; 913f4eaf51aSWei Yang } else if (si->cluster_nr && !si->swap_map[++offset]) { 91436005baeSTim Chen /* non-ssd case, still more slots in cluster? */ 91536005baeSTim Chen --si->cluster_nr; 91636005baeSTim Chen goto checks; 91736005baeSTim Chen } 91836005baeSTim Chen 919ed43af10SHuang Ying /* 920ed43af10SHuang Ying * Even if there's no free clusters available (fragmented), 921ed43af10SHuang Ying * try to scan a little more quickly with lock held unless we 922ed43af10SHuang Ying * have scanned too many slots already. 923ed43af10SHuang Ying */ 924ed43af10SHuang Ying if (!scanned_many) { 925ed43af10SHuang Ying unsigned long scan_limit; 926ed43af10SHuang Ying 927ed43af10SHuang Ying if (offset < scan_base) 928ed43af10SHuang Ying scan_limit = scan_base; 929ed43af10SHuang Ying else 930ed43af10SHuang Ying scan_limit = si->highest_bit; 931ed43af10SHuang Ying for (; offset <= scan_limit && --latency_ration > 0; 932ed43af10SHuang Ying offset++) { 933ed43af10SHuang Ying if (!si->swap_map[offset]) 934ed43af10SHuang Ying goto checks; 935ed43af10SHuang Ying } 936ed43af10SHuang Ying } 937ed43af10SHuang Ying 93836005baeSTim Chen done: 93949070588SHuang Ying set_cluster_next(si, offset + 1); 94036005baeSTim Chen si->flags -= SWP_SCANNING; 94136005baeSTim Chen return n_ret; 9427dfad418SHugh Dickins 943ebebbbe9SHugh Dickins scan: 944ec8acf20SShaohua Li spin_unlock(&si->lock); 945a449bf58SQian Cai while (++offset <= READ_ONCE(si->highest_bit)) { 946a449bf58SQian Cai if (data_race(!si->swap_map[offset])) { 947ec8acf20SShaohua Li spin_lock(&si->lock); 94852b7efdbSHugh Dickins goto checks; 9497dfad418SHugh Dickins } 950a449bf58SQian Cai if (vm_swap_full() && 951a449bf58SQian Cai READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { 952ec8acf20SShaohua Li spin_lock(&si->lock); 953c9e44410SKAMEZAWA Hiroyuki goto checks; 954c9e44410SKAMEZAWA Hiroyuki } 955048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 956048c27fdSHugh Dickins cond_resched(); 957048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 958ed43af10SHuang Ying scanned_many = true; 959048c27fdSHugh Dickins } 96052b7efdbSHugh Dickins } 961c60aa176SHugh Dickins offset = si->lowest_bit; 962a5998061SJamie Liu while (offset < scan_base) { 963a449bf58SQian Cai if (data_race(!si->swap_map[offset])) { 964ec8acf20SShaohua Li spin_lock(&si->lock); 965ebebbbe9SHugh Dickins goto checks; 966c60aa176SHugh Dickins } 967a449bf58SQian Cai if (vm_swap_full() && 968a449bf58SQian Cai READ_ONCE(si->swap_map[offset]) == SWAP_HAS_CACHE) { 969ec8acf20SShaohua Li spin_lock(&si->lock); 970c9e44410SKAMEZAWA Hiroyuki goto checks; 971c9e44410SKAMEZAWA Hiroyuki } 972c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 973c60aa176SHugh Dickins cond_resched(); 974c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 975ed43af10SHuang Ying scanned_many = true; 976c60aa176SHugh Dickins } 977a5998061SJamie Liu offset++; 978c60aa176SHugh Dickins } 979ec8acf20SShaohua Li spin_lock(&si->lock); 9807dfad418SHugh Dickins 9817dfad418SHugh Dickins no_page: 98252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 98336005baeSTim Chen return n_ret; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 98638d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 98738d8b4e6SHuang Ying { 98838d8b4e6SHuang Ying unsigned long idx; 98938d8b4e6SHuang Ying struct swap_cluster_info *ci; 990661c7566SMiaohe Lin unsigned long offset; 99138d8b4e6SHuang Ying 992fe5266d5SHuang Ying /* 993fe5266d5SHuang Ying * Should not even be attempting cluster allocations when huge 994fe5266d5SHuang Ying * page swap is disabled. Warn and fail the allocation. 995fe5266d5SHuang Ying */ 996fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP)) { 997fe5266d5SHuang Ying VM_WARN_ON_ONCE(1); 998fe5266d5SHuang Ying return 0; 999fe5266d5SHuang Ying } 1000fe5266d5SHuang Ying 100138d8b4e6SHuang Ying if (cluster_list_empty(&si->free_clusters)) 100238d8b4e6SHuang Ying return 0; 100338d8b4e6SHuang Ying 100438d8b4e6SHuang Ying idx = cluster_list_first(&si->free_clusters); 100538d8b4e6SHuang Ying offset = idx * SWAPFILE_CLUSTER; 100638d8b4e6SHuang Ying ci = lock_cluster(si, offset); 100738d8b4e6SHuang Ying alloc_cluster(si, idx); 1008e0709829SHuang Ying cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE); 100938d8b4e6SHuang Ying 1010661c7566SMiaohe Lin memset(si->swap_map + offset, SWAP_HAS_CACHE, SWAPFILE_CLUSTER); 101138d8b4e6SHuang Ying unlock_cluster(ci); 101238d8b4e6SHuang Ying swap_range_alloc(si, offset, SWAPFILE_CLUSTER); 101338d8b4e6SHuang Ying *slot = swp_entry(si->type, offset); 101438d8b4e6SHuang Ying 101538d8b4e6SHuang Ying return 1; 101638d8b4e6SHuang Ying } 101738d8b4e6SHuang Ying 101838d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) 101938d8b4e6SHuang Ying { 102038d8b4e6SHuang Ying unsigned long offset = idx * SWAPFILE_CLUSTER; 102138d8b4e6SHuang Ying struct swap_cluster_info *ci; 102238d8b4e6SHuang Ying 102338d8b4e6SHuang Ying ci = lock_cluster(si, offset); 1024979aafa5SHuang Ying memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER); 102538d8b4e6SHuang Ying cluster_set_count_flag(ci, 0, 0); 102638d8b4e6SHuang Ying free_cluster(si, idx); 102738d8b4e6SHuang Ying unlock_cluster(ci); 102838d8b4e6SHuang Ying swap_range_free(si, offset, SWAPFILE_CLUSTER); 102938d8b4e6SHuang Ying } 103038d8b4e6SHuang Ying 103136005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si, 103236005baeSTim Chen unsigned char usage) 103336005baeSTim Chen { 103436005baeSTim Chen swp_entry_t entry; 103536005baeSTim Chen int n_ret; 103636005baeSTim Chen 103736005baeSTim Chen n_ret = scan_swap_map_slots(si, usage, 1, &entry); 103836005baeSTim Chen 103936005baeSTim Chen if (n_ret) 104036005baeSTim Chen return swp_offset(entry); 104136005baeSTim Chen else 104236005baeSTim Chen return 0; 104336005baeSTim Chen 104436005baeSTim Chen } 104536005baeSTim Chen 10465d5e8f19SHuang Ying int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) 10471da177e4SLinus Torvalds { 10485d5e8f19SHuang Ying unsigned long size = swap_entry_size(entry_size); 1049adfab836SDan Streetman struct swap_info_struct *si, *next; 105036005baeSTim Chen long avail_pgs; 105136005baeSTim Chen int n_ret = 0; 1052a2468cc9SAaron Lu int node; 10531da177e4SLinus Torvalds 105438d8b4e6SHuang Ying /* Only single cluster request supported */ 10555d5e8f19SHuang Ying WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER); 105638d8b4e6SHuang Ying 1057b50da6e9SZhaoyang Huang spin_lock(&swap_avail_lock); 1058b50da6e9SZhaoyang Huang 10595d5e8f19SHuang Ying avail_pgs = atomic_long_read(&nr_swap_pages) / size; 1060b50da6e9SZhaoyang Huang if (avail_pgs <= 0) { 1061b50da6e9SZhaoyang Huang spin_unlock(&swap_avail_lock); 1062fb4f88dcSHugh Dickins goto noswap; 1063b50da6e9SZhaoyang Huang } 106436005baeSTim Chen 106508d3090fSWei Yang n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs); 106636005baeSTim Chen 10675d5e8f19SHuang Ying atomic_long_sub(n_goal * size, &nr_swap_pages); 10681da177e4SLinus Torvalds 106918ab4d4cSDan Streetman start_over: 1070a2468cc9SAaron Lu node = numa_node_id(); 1071a2468cc9SAaron Lu plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { 107218ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 1073a2468cc9SAaron Lu plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); 107418ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 1075ec8acf20SShaohua Li spin_lock(&si->lock); 1076adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 107718ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 1078a2468cc9SAaron Lu if (plist_node_empty(&si->avail_lists[node])) { 1079ec8acf20SShaohua Li spin_unlock(&si->lock); 108018ab4d4cSDan Streetman goto nextsi; 108118ab4d4cSDan Streetman } 108218ab4d4cSDan Streetman WARN(!si->highest_bit, 108318ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 108418ab4d4cSDan Streetman si->type); 108518ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 108618ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 108718ab4d4cSDan Streetman si->type); 1088a2468cc9SAaron Lu __del_from_avail_list(si); 108918ab4d4cSDan Streetman spin_unlock(&si->lock); 109018ab4d4cSDan Streetman goto nextsi; 1091ec8acf20SShaohua Li } 10925d5e8f19SHuang Ying if (size == SWAPFILE_CLUSTER) { 109341663430SGao Xiang if (si->flags & SWP_BLKDEV) 109438d8b4e6SHuang Ying n_ret = swap_alloc_cluster(si, swp_entries); 1095f0eea189SHuang Ying } else 109636005baeSTim Chen n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, 109736005baeSTim Chen n_goal, swp_entries); 1098ec8acf20SShaohua Li spin_unlock(&si->lock); 10995d5e8f19SHuang Ying if (n_ret || size == SWAPFILE_CLUSTER) 110036005baeSTim Chen goto check_out; 110118ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 110218ab4d4cSDan Streetman si->type); 110336005baeSTim Chen 110418ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 110518ab4d4cSDan Streetman nextsi: 1106adfab836SDan Streetman /* 1107adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 1108adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 1109adfab836SDan Streetman * callers probably all tried to get a page from the same si 111018ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 111118ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 111218ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 111318ab4d4cSDan Streetman * list may have been modified; so if next is still in the 111436005baeSTim Chen * swap_avail_head list then try it, otherwise start over 111536005baeSTim Chen * if we have not gotten any slots. 1116adfab836SDan Streetman */ 1117a2468cc9SAaron Lu if (plist_node_empty(&next->avail_lists[node])) 111818ab4d4cSDan Streetman goto start_over; 1119fb4f88dcSHugh Dickins } 1120fb4f88dcSHugh Dickins 112118ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 112218ab4d4cSDan Streetman 112336005baeSTim Chen check_out: 112436005baeSTim Chen if (n_ret < n_goal) 11255d5e8f19SHuang Ying atomic_long_add((long)(n_goal - n_ret) * size, 112638d8b4e6SHuang Ying &nr_swap_pages); 1127fb4f88dcSHugh Dickins noswap: 112836005baeSTim Chen return n_ret; 112936005baeSTim Chen } 113036005baeSTim Chen 11312de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 1132910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 1133910321eaSHugh Dickins { 1134c10d38ccSDaniel Jordan struct swap_info_struct *si = swap_type_to_swap_info(type); 1135910321eaSHugh Dickins pgoff_t offset; 1136910321eaSHugh Dickins 1137c10d38ccSDaniel Jordan if (!si) 1138c10d38ccSDaniel Jordan goto fail; 1139c10d38ccSDaniel Jordan 1140ec8acf20SShaohua Li spin_lock(&si->lock); 1141c10d38ccSDaniel Jordan if (si->flags & SWP_WRITEOK) { 1142910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 1143910321eaSHugh Dickins offset = scan_swap_map(si, 1); 1144910321eaSHugh Dickins if (offset) { 1145b50da6e9SZhaoyang Huang atomic_long_dec(&nr_swap_pages); 1146ec8acf20SShaohua Li spin_unlock(&si->lock); 1147910321eaSHugh Dickins return swp_entry(type, offset); 1148910321eaSHugh Dickins } 1149910321eaSHugh Dickins } 1150ec8acf20SShaohua Li spin_unlock(&si->lock); 1151c10d38ccSDaniel Jordan fail: 1152910321eaSHugh Dickins return (swp_entry_t) {0}; 1153910321eaSHugh Dickins } 1154910321eaSHugh Dickins 1155e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry) 11561da177e4SLinus Torvalds { 11571da177e4SLinus Torvalds struct swap_info_struct *p; 1158eb085574SHuang Ying unsigned long offset; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (!entry.val) 11611da177e4SLinus Torvalds goto out; 1162eb085574SHuang Ying p = swp_swap_info(entry); 1163c10d38ccSDaniel Jordan if (!p) 11641da177e4SLinus Torvalds goto bad_nofile; 1165a449bf58SQian Cai if (data_race(!(p->flags & SWP_USED))) 11661da177e4SLinus Torvalds goto bad_device; 11671da177e4SLinus Torvalds offset = swp_offset(entry); 11681da177e4SLinus Torvalds if (offset >= p->max) 11691da177e4SLinus Torvalds goto bad_offset; 11701da177e4SLinus Torvalds return p; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds bad_offset: 1173cf532faaSStephen Zhang pr_err("%s: %s%08lx\n", __func__, Bad_offset, entry.val); 11741da177e4SLinus Torvalds goto out; 11751da177e4SLinus Torvalds bad_device: 1176cf532faaSStephen Zhang pr_err("%s: %s%08lx\n", __func__, Unused_file, entry.val); 11771da177e4SLinus Torvalds goto out; 11781da177e4SLinus Torvalds bad_nofile: 1179cf532faaSStephen Zhang pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val); 11801da177e4SLinus Torvalds out: 11811da177e4SLinus Torvalds return NULL; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 1184e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 1185e8c26ab6STim Chen { 1186e8c26ab6STim Chen struct swap_info_struct *p; 1187e8c26ab6STim Chen 1188e8c26ab6STim Chen p = __swap_info_get(entry); 1189e8c26ab6STim Chen if (!p) 1190e8c26ab6STim Chen goto out; 1191a449bf58SQian Cai if (data_race(!p->swap_map[swp_offset(entry)])) 1192e8c26ab6STim Chen goto bad_free; 1193e8c26ab6STim Chen return p; 1194e8c26ab6STim Chen 1195e8c26ab6STim Chen bad_free: 1196cf532faaSStephen Zhang pr_err("%s: %s%08lx\n", __func__, Unused_offset, entry.val); 1197e8c26ab6STim Chen out: 1198e8c26ab6STim Chen return NULL; 1199e8c26ab6STim Chen } 1200e8c26ab6STim Chen 1201235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 12021da177e4SLinus Torvalds { 1203235b6217SHuang, Ying struct swap_info_struct *p; 1204235b6217SHuang, Ying 1205235b6217SHuang, Ying p = _swap_info_get(entry); 1206235b6217SHuang, Ying if (p) 1207235b6217SHuang, Ying spin_lock(&p->lock); 1208235b6217SHuang, Ying return p; 1209235b6217SHuang, Ying } 1210235b6217SHuang, Ying 12117c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, 12127c00bafeSTim Chen struct swap_info_struct *q) 12137c00bafeSTim Chen { 12147c00bafeSTim Chen struct swap_info_struct *p; 12157c00bafeSTim Chen 12167c00bafeSTim Chen p = _swap_info_get(entry); 12177c00bafeSTim Chen 12187c00bafeSTim Chen if (p != q) { 12197c00bafeSTim Chen if (q != NULL) 12207c00bafeSTim Chen spin_unlock(&q->lock); 12217c00bafeSTim Chen if (p != NULL) 12227c00bafeSTim Chen spin_lock(&p->lock); 12237c00bafeSTim Chen } 12247c00bafeSTim Chen return p; 12257c00bafeSTim Chen } 12267c00bafeSTim Chen 1227b32d5f32SHuang Ying static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, 1228b32d5f32SHuang Ying unsigned long offset, 1229b32d5f32SHuang Ying unsigned char usage) 1230235b6217SHuang, Ying { 12318d69aaeeSHugh Dickins unsigned char count; 12328d69aaeeSHugh Dickins unsigned char has_cache; 1233235b6217SHuang, Ying 1234355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 1235235b6217SHuang, Ying 1236253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 1237253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 1238253d553bSHugh Dickins 1239253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 1240253d553bSHugh Dickins VM_BUG_ON(!has_cache); 1241253d553bSHugh Dickins has_cache = 0; 1242aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 1243aaa46865SHugh Dickins /* 1244aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 1245aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 1246aaa46865SHugh Dickins */ 1247aaa46865SHugh Dickins count = 0; 1248570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 1249570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 1250570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 1251570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 1252570a335bSHugh Dickins else 1253570a335bSHugh Dickins count = SWAP_MAP_MAX; 1254570a335bSHugh Dickins } else 1255253d553bSHugh Dickins count--; 1256570a335bSHugh Dickins } 1257253d553bSHugh Dickins 1258253d553bSHugh Dickins usage = count | has_cache; 1259a449bf58SQian Cai if (usage) 1260a449bf58SQian Cai WRITE_ONCE(p->swap_map[offset], usage); 1261a449bf58SQian Cai else 1262a449bf58SQian Cai WRITE_ONCE(p->swap_map[offset], SWAP_HAS_CACHE); 1263253d553bSHugh Dickins 1264b32d5f32SHuang Ying return usage; 1265b32d5f32SHuang Ying } 1266b32d5f32SHuang Ying 1267eb085574SHuang Ying /* 1268eb085574SHuang Ying * Check whether swap entry is valid in the swap device. If so, 1269eb085574SHuang Ying * return pointer to swap_info_struct, and keep the swap entry valid 1270eb085574SHuang Ying * via preventing the swap device from being swapoff, until 1271eb085574SHuang Ying * put_swap_device() is called. Otherwise return NULL. 1272eb085574SHuang Ying * 1273eb085574SHuang Ying * The entirety of the RCU read critical section must come before the 1274eb085574SHuang Ying * return from or after the call to synchronize_rcu() in 1275eb085574SHuang Ying * enable_swap_info() or swapoff(). So if "si->flags & SWP_VALID" is 1276eb085574SHuang Ying * true, the si->map, si->cluster_info, etc. must be valid in the 1277eb085574SHuang Ying * critical section. 1278eb085574SHuang Ying * 1279eb085574SHuang Ying * Notice that swapoff or swapoff+swapon can still happen before the 1280eb085574SHuang Ying * rcu_read_lock() in get_swap_device() or after the rcu_read_unlock() 1281eb085574SHuang Ying * in put_swap_device() if there isn't any other way to prevent 1282eb085574SHuang Ying * swapoff, such as page lock, page table lock, etc. The caller must 1283eb085574SHuang Ying * be prepared for that. For example, the following situation is 1284eb085574SHuang Ying * possible. 1285eb085574SHuang Ying * 1286eb085574SHuang Ying * CPU1 CPU2 1287eb085574SHuang Ying * do_swap_page() 1288eb085574SHuang Ying * ... swapoff+swapon 1289eb085574SHuang Ying * __read_swap_cache_async() 1290eb085574SHuang Ying * swapcache_prepare() 1291eb085574SHuang Ying * __swap_duplicate() 1292eb085574SHuang Ying * // check swap_map 1293eb085574SHuang Ying * // verify PTE not changed 1294eb085574SHuang Ying * 1295eb085574SHuang Ying * In __swap_duplicate(), the swap_map need to be checked before 1296eb085574SHuang Ying * changing partly because the specified swap entry may be for another 1297eb085574SHuang Ying * swap device which has been swapoff. And in do_swap_page(), after 1298eb085574SHuang Ying * the page is read from the swap device, the PTE is verified not 1299eb085574SHuang Ying * changed with the page table locked to check whether the swap device 1300eb085574SHuang Ying * has been swapoff or swapoff+swapon. 1301eb085574SHuang Ying */ 1302eb085574SHuang Ying struct swap_info_struct *get_swap_device(swp_entry_t entry) 1303eb085574SHuang Ying { 1304eb085574SHuang Ying struct swap_info_struct *si; 1305eb085574SHuang Ying unsigned long offset; 1306eb085574SHuang Ying 1307eb085574SHuang Ying if (!entry.val) 1308eb085574SHuang Ying goto out; 1309eb085574SHuang Ying si = swp_swap_info(entry); 1310eb085574SHuang Ying if (!si) 1311eb085574SHuang Ying goto bad_nofile; 1312eb085574SHuang Ying 1313eb085574SHuang Ying rcu_read_lock(); 1314a449bf58SQian Cai if (data_race(!(si->flags & SWP_VALID))) 1315eb085574SHuang Ying goto unlock_out; 1316eb085574SHuang Ying offset = swp_offset(entry); 1317eb085574SHuang Ying if (offset >= si->max) 1318eb085574SHuang Ying goto unlock_out; 1319eb085574SHuang Ying 1320eb085574SHuang Ying return si; 1321eb085574SHuang Ying bad_nofile: 1322eb085574SHuang Ying pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val); 1323eb085574SHuang Ying out: 1324eb085574SHuang Ying return NULL; 1325eb085574SHuang Ying unlock_out: 1326eb085574SHuang Ying rcu_read_unlock(); 1327eb085574SHuang Ying return NULL; 1328eb085574SHuang Ying } 1329eb085574SHuang Ying 1330b32d5f32SHuang Ying static unsigned char __swap_entry_free(struct swap_info_struct *p, 133133e16272SWei Yang swp_entry_t entry) 1332b32d5f32SHuang Ying { 1333b32d5f32SHuang Ying struct swap_cluster_info *ci; 1334b32d5f32SHuang Ying unsigned long offset = swp_offset(entry); 133533e16272SWei Yang unsigned char usage; 1336b32d5f32SHuang Ying 1337b32d5f32SHuang Ying ci = lock_cluster_or_swap_info(p, offset); 133833e16272SWei Yang usage = __swap_entry_free_locked(p, offset, 1); 13397c00bafeSTim Chen unlock_cluster_or_swap_info(p, ci); 134010e364daSHuang Ying if (!usage) 134110e364daSHuang Ying free_swap_slot(entry); 1342235b6217SHuang, Ying 13437c00bafeSTim Chen return usage; 13447c00bafeSTim Chen } 13457c00bafeSTim Chen 13467c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) 13477c00bafeSTim Chen { 13487c00bafeSTim Chen struct swap_cluster_info *ci; 13497c00bafeSTim Chen unsigned long offset = swp_offset(entry); 13507c00bafeSTim Chen unsigned char count; 13517c00bafeSTim Chen 1352235b6217SHuang, Ying ci = lock_cluster(p, offset); 13537c00bafeSTim Chen count = p->swap_map[offset]; 13547c00bafeSTim Chen VM_BUG_ON(count != SWAP_HAS_CACHE); 13557c00bafeSTim Chen p->swap_map[offset] = 0; 13562a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 1357235b6217SHuang, Ying unlock_cluster(ci); 13587c00bafeSTim Chen 135938d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, 1); 136038d8b4e6SHuang Ying swap_range_free(p, offset, 1); 13611da177e4SLinus Torvalds } 1362253d553bSHugh Dickins 13631da177e4SLinus Torvalds /* 13641da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 13651da177e4SLinus Torvalds * is still around or has not been recycled. 13661da177e4SLinus Torvalds */ 13671da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 13681da177e4SLinus Torvalds { 13691da177e4SLinus Torvalds struct swap_info_struct *p; 13701da177e4SLinus Torvalds 1371235b6217SHuang, Ying p = _swap_info_get(entry); 137210e364daSHuang Ying if (p) 137333e16272SWei Yang __swap_entry_free(p, entry); 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds /* 1377cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 1378cb4b86baSKAMEZAWA Hiroyuki */ 1379a448f2d0SHuang Ying void put_swap_page(struct page *page, swp_entry_t entry) 138038d8b4e6SHuang Ying { 138138d8b4e6SHuang Ying unsigned long offset = swp_offset(entry); 138238d8b4e6SHuang Ying unsigned long idx = offset / SWAPFILE_CLUSTER; 138338d8b4e6SHuang Ying struct swap_cluster_info *ci; 138438d8b4e6SHuang Ying struct swap_info_struct *si; 138538d8b4e6SHuang Ying unsigned char *map; 1386a3aea839SHuang Ying unsigned int i, free_entries = 0; 1387a3aea839SHuang Ying unsigned char val; 13886c357848SMatthew Wilcox (Oracle) int size = swap_entry_size(thp_nr_pages(page)); 1389fe5266d5SHuang Ying 1390a3aea839SHuang Ying si = _swap_info_get(entry); 139138d8b4e6SHuang Ying if (!si) 139238d8b4e6SHuang Ying return; 139338d8b4e6SHuang Ying 1394c2343d27SHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1395a448f2d0SHuang Ying if (size == SWAPFILE_CLUSTER) { 1396e0709829SHuang Ying VM_BUG_ON(!cluster_is_huge(ci)); 139738d8b4e6SHuang Ying map = si->swap_map + offset; 139838d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1399a3aea839SHuang Ying val = map[i]; 1400a3aea839SHuang Ying VM_BUG_ON(!(val & SWAP_HAS_CACHE)); 1401a3aea839SHuang Ying if (val == SWAP_HAS_CACHE) 1402a3aea839SHuang Ying free_entries++; 140338d8b4e6SHuang Ying } 1404e0709829SHuang Ying cluster_clear_huge(ci); 1405a3aea839SHuang Ying if (free_entries == SWAPFILE_CLUSTER) { 1406c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 1407a3aea839SHuang Ying spin_lock(&si->lock); 140838d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER); 140938d8b4e6SHuang Ying swap_free_cluster(si, idx); 141038d8b4e6SHuang Ying spin_unlock(&si->lock); 1411a448f2d0SHuang Ying return; 1412a448f2d0SHuang Ying } 1413a448f2d0SHuang Ying } 1414a448f2d0SHuang Ying for (i = 0; i < size; i++, entry.val++) { 1415c2343d27SHuang Ying if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { 1416c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 1417a3aea839SHuang Ying free_swap_slot(entry); 1418c2343d27SHuang Ying if (i == size - 1) 1419c2343d27SHuang Ying return; 1420c2343d27SHuang Ying lock_cluster_or_swap_info(si, offset); 1421a3aea839SHuang Ying } 1422a3aea839SHuang Ying } 1423c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 142438d8b4e6SHuang Ying } 142559807685SHuang Ying 1426fe5266d5SHuang Ying #ifdef CONFIG_THP_SWAP 142759807685SHuang Ying int split_swap_cluster(swp_entry_t entry) 142859807685SHuang Ying { 142959807685SHuang Ying struct swap_info_struct *si; 143059807685SHuang Ying struct swap_cluster_info *ci; 143159807685SHuang Ying unsigned long offset = swp_offset(entry); 143259807685SHuang Ying 143359807685SHuang Ying si = _swap_info_get(entry); 143459807685SHuang Ying if (!si) 143559807685SHuang Ying return -EBUSY; 143659807685SHuang Ying ci = lock_cluster(si, offset); 143759807685SHuang Ying cluster_clear_huge(ci); 143859807685SHuang Ying unlock_cluster(ci); 143959807685SHuang Ying return 0; 144059807685SHuang Ying } 1441fe5266d5SHuang Ying #endif 144238d8b4e6SHuang Ying 1443155b5f88SHuang Ying static int swp_entry_cmp(const void *ent1, const void *ent2) 1444155b5f88SHuang Ying { 1445155b5f88SHuang Ying const swp_entry_t *e1 = ent1, *e2 = ent2; 1446155b5f88SHuang Ying 1447155b5f88SHuang Ying return (int)swp_type(*e1) - (int)swp_type(*e2); 1448155b5f88SHuang Ying } 1449155b5f88SHuang Ying 14507c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n) 14517c00bafeSTim Chen { 14527c00bafeSTim Chen struct swap_info_struct *p, *prev; 14537c00bafeSTim Chen int i; 14547c00bafeSTim Chen 14557c00bafeSTim Chen if (n <= 0) 14567c00bafeSTim Chen return; 14577c00bafeSTim Chen 14587c00bafeSTim Chen prev = NULL; 14597c00bafeSTim Chen p = NULL; 1460155b5f88SHuang Ying 1461155b5f88SHuang Ying /* 1462155b5f88SHuang Ying * Sort swap entries by swap device, so each lock is only taken once. 1463155b5f88SHuang Ying * nr_swapfiles isn't absolutely correct, but the overhead of sort() is 1464155b5f88SHuang Ying * so low that it isn't necessary to optimize further. 1465155b5f88SHuang Ying */ 1466155b5f88SHuang Ying if (nr_swapfiles > 1) 1467155b5f88SHuang Ying sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); 14687c00bafeSTim Chen for (i = 0; i < n; ++i) { 14697c00bafeSTim Chen p = swap_info_get_cont(entries[i], prev); 1470235b6217SHuang, Ying if (p) 14717c00bafeSTim Chen swap_entry_free(p, entries[i]); 14727c00bafeSTim Chen prev = p; 14737c00bafeSTim Chen } 14747c00bafeSTim Chen if (p) 14757c00bafeSTim Chen spin_unlock(&p->lock); 1476cb4b86baSKAMEZAWA Hiroyuki } 1477cb4b86baSKAMEZAWA Hiroyuki 1478cb4b86baSKAMEZAWA Hiroyuki /* 1479c475a8abSHugh Dickins * How many references to page are currently swapped out? 1480570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 1481570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 14821da177e4SLinus Torvalds */ 1483bde05d1cSHugh Dickins int page_swapcount(struct page *page) 14841da177e4SLinus Torvalds { 1485c475a8abSHugh Dickins int count = 0; 14861da177e4SLinus Torvalds struct swap_info_struct *p; 1487235b6217SHuang, Ying struct swap_cluster_info *ci; 14881da177e4SLinus Torvalds swp_entry_t entry; 1489235b6217SHuang, Ying unsigned long offset; 14901da177e4SLinus Torvalds 14914c21e2f2SHugh Dickins entry.val = page_private(page); 1492235b6217SHuang, Ying p = _swap_info_get(entry); 14931da177e4SLinus Torvalds if (p) { 1494235b6217SHuang, Ying offset = swp_offset(entry); 1495235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1496235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 1497235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 14981da177e4SLinus Torvalds } 1499c475a8abSHugh Dickins return count; 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 1502eb085574SHuang Ying int __swap_count(swp_entry_t entry) 1503aa8d22a1SMinchan Kim { 1504eb085574SHuang Ying struct swap_info_struct *si; 1505aa8d22a1SMinchan Kim pgoff_t offset = swp_offset(entry); 1506eb085574SHuang Ying int count = 0; 1507aa8d22a1SMinchan Kim 1508eb085574SHuang Ying si = get_swap_device(entry); 1509eb085574SHuang Ying if (si) { 1510eb085574SHuang Ying count = swap_count(si->swap_map[offset]); 1511eb085574SHuang Ying put_swap_device(si); 1512eb085574SHuang Ying } 1513eb085574SHuang Ying return count; 1514aa8d22a1SMinchan Kim } 1515aa8d22a1SMinchan Kim 1516322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) 1517322b8afeSHuang Ying { 1518322b8afeSHuang Ying int count = 0; 1519322b8afeSHuang Ying pgoff_t offset = swp_offset(entry); 1520322b8afeSHuang Ying struct swap_cluster_info *ci; 1521322b8afeSHuang Ying 1522322b8afeSHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1523322b8afeSHuang Ying count = swap_count(si->swap_map[offset]); 1524322b8afeSHuang Ying unlock_cluster_or_swap_info(si, ci); 1525322b8afeSHuang Ying return count; 1526322b8afeSHuang Ying } 1527322b8afeSHuang Ying 15281da177e4SLinus Torvalds /* 15298334b962SMinchan Kim * How many references to @entry are currently swapped out? 1530e8c26ab6STim Chen * This does not give an exact answer when swap count is continued, 1531e8c26ab6STim Chen * but does include the high COUNT_CONTINUED flag to allow for that. 1532e8c26ab6STim Chen */ 1533e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry) 1534e8c26ab6STim Chen { 1535e8c26ab6STim Chen int count = 0; 1536e8c26ab6STim Chen struct swap_info_struct *si; 1537e8c26ab6STim Chen 1538eb085574SHuang Ying si = get_swap_device(entry); 1539eb085574SHuang Ying if (si) { 1540322b8afeSHuang Ying count = swap_swapcount(si, entry); 1541eb085574SHuang Ying put_swap_device(si); 1542eb085574SHuang Ying } 1543e8c26ab6STim Chen return count; 1544e8c26ab6STim Chen } 1545e8c26ab6STim Chen 1546e8c26ab6STim Chen /* 1547e8c26ab6STim Chen * How many references to @entry are currently swapped out? 15488334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 15498334b962SMinchan Kim */ 15508334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 15518334b962SMinchan Kim { 15528334b962SMinchan Kim int count, tmp_count, n; 15538334b962SMinchan Kim struct swap_info_struct *p; 1554235b6217SHuang, Ying struct swap_cluster_info *ci; 15558334b962SMinchan Kim struct page *page; 15568334b962SMinchan Kim pgoff_t offset; 15578334b962SMinchan Kim unsigned char *map; 15588334b962SMinchan Kim 1559235b6217SHuang, Ying p = _swap_info_get(entry); 15608334b962SMinchan Kim if (!p) 15618334b962SMinchan Kim return 0; 15628334b962SMinchan Kim 1563235b6217SHuang, Ying offset = swp_offset(entry); 1564235b6217SHuang, Ying 1565235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1566235b6217SHuang, Ying 1567235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 15688334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 15698334b962SMinchan Kim goto out; 15708334b962SMinchan Kim 15718334b962SMinchan Kim count &= ~COUNT_CONTINUED; 15728334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 15738334b962SMinchan Kim 15748334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 15758334b962SMinchan Kim offset &= ~PAGE_MASK; 15768334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 15778334b962SMinchan Kim 15788334b962SMinchan Kim do { 1579a8ae4991SGeliang Tang page = list_next_entry(page, lru); 15808334b962SMinchan Kim map = kmap_atomic(page); 15818334b962SMinchan Kim tmp_count = map[offset]; 15828334b962SMinchan Kim kunmap_atomic(map); 15838334b962SMinchan Kim 15848334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 15858334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 15868334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 15878334b962SMinchan Kim out: 1588235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 15898334b962SMinchan Kim return count; 15908334b962SMinchan Kim } 15918334b962SMinchan Kim 1592e0709829SHuang Ying static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, 1593e0709829SHuang Ying swp_entry_t entry) 1594e0709829SHuang Ying { 1595e0709829SHuang Ying struct swap_cluster_info *ci; 1596e0709829SHuang Ying unsigned char *map = si->swap_map; 1597e0709829SHuang Ying unsigned long roffset = swp_offset(entry); 1598e0709829SHuang Ying unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER); 1599e0709829SHuang Ying int i; 1600e0709829SHuang Ying bool ret = false; 1601e0709829SHuang Ying 1602e0709829SHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1603e0709829SHuang Ying if (!ci || !cluster_is_huge(ci)) { 1604afa4711eSHuang Ying if (swap_count(map[roffset])) 1605e0709829SHuang Ying ret = true; 1606e0709829SHuang Ying goto unlock_out; 1607e0709829SHuang Ying } 1608e0709829SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1609afa4711eSHuang Ying if (swap_count(map[offset + i])) { 1610e0709829SHuang Ying ret = true; 1611e0709829SHuang Ying break; 1612e0709829SHuang Ying } 1613e0709829SHuang Ying } 1614e0709829SHuang Ying unlock_out: 1615e0709829SHuang Ying unlock_cluster_or_swap_info(si, ci); 1616e0709829SHuang Ying return ret; 1617e0709829SHuang Ying } 1618e0709829SHuang Ying 1619e0709829SHuang Ying static bool page_swapped(struct page *page) 1620e0709829SHuang Ying { 1621e0709829SHuang Ying swp_entry_t entry; 1622e0709829SHuang Ying struct swap_info_struct *si; 1623e0709829SHuang Ying 1624fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) 1625e0709829SHuang Ying return page_swapcount(page) != 0; 1626e0709829SHuang Ying 1627e0709829SHuang Ying page = compound_head(page); 1628e0709829SHuang Ying entry.val = page_private(page); 1629e0709829SHuang Ying si = _swap_info_get(entry); 1630e0709829SHuang Ying if (si) 1631e0709829SHuang Ying return swap_page_trans_huge_swapped(si, entry); 1632e0709829SHuang Ying return false; 1633e0709829SHuang Ying } 1634ba3c4ce6SHuang Ying 1635ba3c4ce6SHuang Ying static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount, 1636ba3c4ce6SHuang Ying int *total_swapcount) 1637ba3c4ce6SHuang Ying { 1638ba3c4ce6SHuang Ying int i, map_swapcount, _total_mapcount, _total_swapcount; 1639ba3c4ce6SHuang Ying unsigned long offset = 0; 1640ba3c4ce6SHuang Ying struct swap_info_struct *si; 1641ba3c4ce6SHuang Ying struct swap_cluster_info *ci = NULL; 1642ba3c4ce6SHuang Ying unsigned char *map = NULL; 1643ba3c4ce6SHuang Ying int mapcount, swapcount = 0; 1644ba3c4ce6SHuang Ying 1645ba3c4ce6SHuang Ying /* hugetlbfs shouldn't call it */ 1646ba3c4ce6SHuang Ying VM_BUG_ON_PAGE(PageHuge(page), page); 1647ba3c4ce6SHuang Ying 1648fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) { 1649fe5266d5SHuang Ying mapcount = page_trans_huge_mapcount(page, total_mapcount); 1650ba3c4ce6SHuang Ying if (PageSwapCache(page)) 1651ba3c4ce6SHuang Ying swapcount = page_swapcount(page); 1652ba3c4ce6SHuang Ying if (total_swapcount) 1653ba3c4ce6SHuang Ying *total_swapcount = swapcount; 1654ba3c4ce6SHuang Ying return mapcount + swapcount; 1655ba3c4ce6SHuang Ying } 1656ba3c4ce6SHuang Ying 1657ba3c4ce6SHuang Ying page = compound_head(page); 1658ba3c4ce6SHuang Ying 1659ba3c4ce6SHuang Ying _total_mapcount = _total_swapcount = map_swapcount = 0; 1660ba3c4ce6SHuang Ying if (PageSwapCache(page)) { 1661ba3c4ce6SHuang Ying swp_entry_t entry; 1662ba3c4ce6SHuang Ying 1663ba3c4ce6SHuang Ying entry.val = page_private(page); 1664ba3c4ce6SHuang Ying si = _swap_info_get(entry); 1665ba3c4ce6SHuang Ying if (si) { 1666ba3c4ce6SHuang Ying map = si->swap_map; 1667ba3c4ce6SHuang Ying offset = swp_offset(entry); 1668ba3c4ce6SHuang Ying } 1669ba3c4ce6SHuang Ying } 1670ba3c4ce6SHuang Ying if (map) 1671ba3c4ce6SHuang Ying ci = lock_cluster(si, offset); 1672ba3c4ce6SHuang Ying for (i = 0; i < HPAGE_PMD_NR; i++) { 1673ba3c4ce6SHuang Ying mapcount = atomic_read(&page[i]._mapcount) + 1; 1674ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1675ba3c4ce6SHuang Ying if (map) { 1676ba3c4ce6SHuang Ying swapcount = swap_count(map[offset + i]); 1677ba3c4ce6SHuang Ying _total_swapcount += swapcount; 1678ba3c4ce6SHuang Ying } 1679ba3c4ce6SHuang Ying map_swapcount = max(map_swapcount, mapcount + swapcount); 1680ba3c4ce6SHuang Ying } 1681ba3c4ce6SHuang Ying unlock_cluster(ci); 1682ba3c4ce6SHuang Ying if (PageDoubleMap(page)) { 1683ba3c4ce6SHuang Ying map_swapcount -= 1; 1684ba3c4ce6SHuang Ying _total_mapcount -= HPAGE_PMD_NR; 1685ba3c4ce6SHuang Ying } 1686ba3c4ce6SHuang Ying mapcount = compound_mapcount(page); 1687ba3c4ce6SHuang Ying map_swapcount += mapcount; 1688ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1689ba3c4ce6SHuang Ying if (total_mapcount) 1690ba3c4ce6SHuang Ying *total_mapcount = _total_mapcount; 1691ba3c4ce6SHuang Ying if (total_swapcount) 1692ba3c4ce6SHuang Ying *total_swapcount = _total_swapcount; 1693ba3c4ce6SHuang Ying 1694ba3c4ce6SHuang Ying return map_swapcount; 1695ba3c4ce6SHuang Ying } 1696e0709829SHuang Ying 16978334b962SMinchan Kim /* 16987b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 16997b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 17007b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 17017b1fe597SHugh Dickins * later would only waste time away from clustering. 17026d0a07edSAndrea Arcangeli * 1703ba3c4ce6SHuang Ying * NOTE: total_map_swapcount should not be relied upon by the caller if 17046d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 17056d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 17061da177e4SLinus Torvalds */ 1707ba3c4ce6SHuang Ying bool reuse_swap_page(struct page *page, int *total_map_swapcount) 17081da177e4SLinus Torvalds { 1709ba3c4ce6SHuang Ying int count, total_mapcount, total_swapcount; 17101da177e4SLinus Torvalds 1711309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 17125ad64688SHugh Dickins if (unlikely(PageKsm(page))) 17136d0a07edSAndrea Arcangeli return false; 1714ba3c4ce6SHuang Ying count = page_trans_huge_map_swapcount(page, &total_mapcount, 1715ba3c4ce6SHuang Ying &total_swapcount); 1716ba3c4ce6SHuang Ying if (total_map_swapcount) 1717ba3c4ce6SHuang Ying *total_map_swapcount = total_mapcount + total_swapcount; 1718ba3c4ce6SHuang Ying if (count == 1 && PageSwapCache(page) && 1719ba3c4ce6SHuang Ying (likely(!PageTransCompound(page)) || 1720ba3c4ce6SHuang Ying /* The remaining swap count will be freed soon */ 1721ba3c4ce6SHuang Ying total_swapcount == page_swapcount(page))) { 1722f0571429SMinchan Kim if (!PageWriteback(page)) { 1723ba3c4ce6SHuang Ying page = compound_head(page); 17247b1fe597SHugh Dickins delete_from_swap_cache(page); 17257b1fe597SHugh Dickins SetPageDirty(page); 1726f0571429SMinchan Kim } else { 1727f0571429SMinchan Kim swp_entry_t entry; 1728f0571429SMinchan Kim struct swap_info_struct *p; 1729f0571429SMinchan Kim 1730f0571429SMinchan Kim entry.val = page_private(page); 1731f0571429SMinchan Kim p = swap_info_get(entry); 1732f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1733f0571429SMinchan Kim spin_unlock(&p->lock); 1734f0571429SMinchan Kim return false; 1735f0571429SMinchan Kim } 1736f0571429SMinchan Kim spin_unlock(&p->lock); 17377b1fe597SHugh Dickins } 17387b1fe597SHugh Dickins } 1739ba3c4ce6SHuang Ying 17405ad64688SHugh Dickins return count <= 1; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* 1744a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1745a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 17461da177e4SLinus Torvalds */ 1747a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 17481da177e4SLinus Torvalds { 1749309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds if (!PageSwapCache(page)) 17521da177e4SLinus Torvalds return 0; 17531da177e4SLinus Torvalds if (PageWriteback(page)) 17541da177e4SLinus Torvalds return 0; 1755e0709829SHuang Ying if (page_swapped(page)) 17561da177e4SLinus Torvalds return 0; 17571da177e4SLinus Torvalds 1758b73d7fceSHugh Dickins /* 1759b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1760b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1761b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1762b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1763b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1764b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1765b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1766b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1767b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1768b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1769b73d7fceSHugh Dickins * 17702de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1771f90ac398SMel Gorman * to disk so check that here. 1772b73d7fceSHugh Dickins */ 1773f90ac398SMel Gorman if (pm_suspended_storage()) 1774b73d7fceSHugh Dickins return 0; 1775b73d7fceSHugh Dickins 1776e0709829SHuang Ying page = compound_head(page); 1777a2c43eedSHugh Dickins delete_from_swap_cache(page); 17781da177e4SLinus Torvalds SetPageDirty(page); 1779a2c43eedSHugh Dickins return 1; 178068a22394SRik van Riel } 178168a22394SRik van Riel 178268a22394SRik van Riel /* 17831da177e4SLinus Torvalds * Free the swap entry like above, but also try to 17841da177e4SLinus Torvalds * free the page cache entry if it is the last user. 17851da177e4SLinus Torvalds */ 17862509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds struct swap_info_struct *p; 17897c00bafeSTim Chen unsigned char count; 17901da177e4SLinus Torvalds 1791a7420aa5SAndi Kleen if (non_swap_entry(entry)) 17922509ef26SHugh Dickins return 1; 17930697212aSChristoph Lameter 17947c00bafeSTim Chen p = _swap_info_get(entry); 17951da177e4SLinus Torvalds if (p) { 179633e16272SWei Yang count = __swap_entry_free(p, entry); 1797e0709829SHuang Ying if (count == SWAP_HAS_CACHE && 1798bcd49e86SHuang Ying !swap_page_trans_huge_swapped(p, entry)) 1799bcd49e86SHuang Ying __try_to_reclaim_swap(p, swp_offset(entry), 1800bcd49e86SHuang Ying TTRS_UNMAPPED | TTRS_FULL); 18011da177e4SLinus Torvalds } 18022509ef26SHugh Dickins return p != NULL; 18031da177e4SLinus Torvalds } 18041da177e4SLinus Torvalds 1805b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1806f577eb30SRafael J. Wysocki /* 1807915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1808f577eb30SRafael J. Wysocki * 1809915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1810915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1811915bae9eSRafael J. Wysocki * 1812915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1813f577eb30SRafael J. Wysocki */ 181421bd9005SChristoph Hellwig int swap_type_of(dev_t device, sector_t offset) 1815f577eb30SRafael J. Wysocki { 1816efa90a98SHugh Dickins int type; 1817f577eb30SRafael J. Wysocki 181821bd9005SChristoph Hellwig if (!device) 181921bd9005SChristoph Hellwig return -1; 1820915bae9eSRafael J. Wysocki 1821f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1822efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1823efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1824f577eb30SRafael J. Wysocki 1825915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1826f577eb30SRafael J. Wysocki continue; 1827b6b5bce3SRafael J. Wysocki 182821bd9005SChristoph Hellwig if (device == sis->bdev->bd_dev) { 18294efaceb1SAaron Lu struct swap_extent *se = first_se(sis); 1830915bae9eSRafael J. Wysocki 1831915bae9eSRafael J. Wysocki if (se->start_block == offset) { 1832f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1833efa90a98SHugh Dickins return type; 1834f577eb30SRafael J. Wysocki } 1835f577eb30SRafael J. Wysocki } 1836915bae9eSRafael J. Wysocki } 1837f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 183821bd9005SChristoph Hellwig return -ENODEV; 183921bd9005SChristoph Hellwig } 1840915bae9eSRafael J. Wysocki 184121bd9005SChristoph Hellwig int find_first_swap(dev_t *device) 184221bd9005SChristoph Hellwig { 184321bd9005SChristoph Hellwig int type; 184421bd9005SChristoph Hellwig 184521bd9005SChristoph Hellwig spin_lock(&swap_lock); 184621bd9005SChristoph Hellwig for (type = 0; type < nr_swapfiles; type++) { 184721bd9005SChristoph Hellwig struct swap_info_struct *sis = swap_info[type]; 184821bd9005SChristoph Hellwig 184921bd9005SChristoph Hellwig if (!(sis->flags & SWP_WRITEOK)) 185021bd9005SChristoph Hellwig continue; 185121bd9005SChristoph Hellwig *device = sis->bdev->bd_dev; 185221bd9005SChristoph Hellwig spin_unlock(&swap_lock); 185321bd9005SChristoph Hellwig return type; 185421bd9005SChristoph Hellwig } 185521bd9005SChristoph Hellwig spin_unlock(&swap_lock); 1856f577eb30SRafael J. Wysocki return -ENODEV; 1857f577eb30SRafael J. Wysocki } 1858f577eb30SRafael J. Wysocki 1859f577eb30SRafael J. Wysocki /* 186073c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 186173c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 186273c34b6aSHugh Dickins */ 186373c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 186473c34b6aSHugh Dickins { 1865c10d38ccSDaniel Jordan struct swap_info_struct *si = swap_type_to_swap_info(type); 1866f885056aSChristoph Hellwig struct swap_extent *se; 186773c34b6aSHugh Dickins 1868c10d38ccSDaniel Jordan if (!si || !(si->flags & SWP_WRITEOK)) 186973c34b6aSHugh Dickins return 0; 1870f885056aSChristoph Hellwig se = offset_to_swap_extent(si, offset); 1871f885056aSChristoph Hellwig return se->start_block + (offset - se->start_page); 187273c34b6aSHugh Dickins } 187373c34b6aSHugh Dickins 187473c34b6aSHugh Dickins /* 1875f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1876f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1877f577eb30SRafael J. Wysocki * 1878f577eb30SRafael J. Wysocki * This is needed for software suspend 1879f577eb30SRafael J. Wysocki */ 1880f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1881f577eb30SRafael J. Wysocki { 1882f577eb30SRafael J. Wysocki unsigned int n = 0; 1883f577eb30SRafael J. Wysocki 1884f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1885efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1886efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1887efa90a98SHugh Dickins 1888ec8acf20SShaohua Li spin_lock(&sis->lock); 1889efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1890efa90a98SHugh Dickins n = sis->pages; 1891f577eb30SRafael J. Wysocki if (free) 1892efa90a98SHugh Dickins n -= sis->inuse_pages; 1893efa90a98SHugh Dickins } 1894ec8acf20SShaohua Li spin_unlock(&sis->lock); 1895f577eb30SRafael J. Wysocki } 1896f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1897f577eb30SRafael J. Wysocki return n; 1898f577eb30SRafael J. Wysocki } 189973c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1900f577eb30SRafael J. Wysocki 19019f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1902179ef71cSCyrill Gorcunov { 1903*099dd687SPeter Xu return pte_same(pte_swp_clear_flags(pte), swp_pte); 1904179ef71cSCyrill Gorcunov } 1905179ef71cSCyrill Gorcunov 19061da177e4SLinus Torvalds /* 190772866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 190872866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 190972866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 19101da177e4SLinus Torvalds */ 1911044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 19121da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 19131da177e4SLinus Torvalds { 19149e16b7fbSHugh Dickins struct page *swapcache; 1915044d66c1SHugh Dickins spinlock_t *ptl; 1916044d66c1SHugh Dickins pte_t *pte; 1917044d66c1SHugh Dickins int ret = 1; 1918044d66c1SHugh Dickins 19199e16b7fbSHugh Dickins swapcache = page; 19209e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 19219e16b7fbSHugh Dickins if (unlikely(!page)) 19229e16b7fbSHugh Dickins return -ENOMEM; 19239e16b7fbSHugh Dickins 1924044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 19259f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1926044d66c1SHugh Dickins ret = 0; 1927044d66c1SHugh Dickins goto out; 1928044d66c1SHugh Dickins } 19298a9f3ccdSBalbir Singh 1930b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1931d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 19321da177e4SLinus Torvalds get_page(page); 19331da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 19341da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 193500501b53SJohannes Weiner if (page == swapcache) { 1936be5d0a74SJohannes Weiner page_add_anon_rmap(page, vma, addr, false); 193700501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1938be5d0a74SJohannes Weiner page_add_new_anon_rmap(page, vma, addr, false); 1939b518154eSJoonsoo Kim lru_cache_add_inactive_or_unevictable(page, vma); 194000501b53SJohannes Weiner } 19411da177e4SLinus Torvalds swap_free(entry); 1942044d66c1SHugh Dickins out: 1943044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 19449e16b7fbSHugh Dickins if (page != swapcache) { 19459e16b7fbSHugh Dickins unlock_page(page); 19469e16b7fbSHugh Dickins put_page(page); 19479e16b7fbSHugh Dickins } 1948044d66c1SHugh Dickins return ret; 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 19511da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 19521da177e4SLinus Torvalds unsigned long addr, unsigned long end, 1953b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 1954b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 19551da177e4SLinus Torvalds { 1956b56a2d8aSVineeth Remanan Pillai struct page *page; 1957b56a2d8aSVineeth Remanan Pillai swp_entry_t entry; 1958705e87c0SHugh Dickins pte_t *pte; 1959b56a2d8aSVineeth Remanan Pillai struct swap_info_struct *si; 1960b56a2d8aSVineeth Remanan Pillai unsigned long offset; 19618a9f3ccdSBalbir Singh int ret = 0; 1962b56a2d8aSVineeth Remanan Pillai volatile unsigned char *swap_map; 19631da177e4SLinus Torvalds 1964b56a2d8aSVineeth Remanan Pillai si = swap_info[type]; 1965044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 19661da177e4SLinus Torvalds do { 1967b56a2d8aSVineeth Remanan Pillai if (!is_swap_pte(*pte)) 1968b56a2d8aSVineeth Remanan Pillai continue; 1969b56a2d8aSVineeth Remanan Pillai 1970b56a2d8aSVineeth Remanan Pillai entry = pte_to_swp_entry(*pte); 1971b56a2d8aSVineeth Remanan Pillai if (swp_type(entry) != type) 1972b56a2d8aSVineeth Remanan Pillai continue; 1973b56a2d8aSVineeth Remanan Pillai 1974b56a2d8aSVineeth Remanan Pillai offset = swp_offset(entry); 1975b56a2d8aSVineeth Remanan Pillai if (frontswap && !frontswap_test(si, offset)) 1976b56a2d8aSVineeth Remanan Pillai continue; 1977b56a2d8aSVineeth Remanan Pillai 1978044d66c1SHugh Dickins pte_unmap(pte); 1979b56a2d8aSVineeth Remanan Pillai swap_map = &si->swap_map[offset]; 1980ebc5951eSAndrea Righi page = lookup_swap_cache(entry, vma, addr); 1981ebc5951eSAndrea Righi if (!page) { 19828c63ca5bSWill Deacon struct vm_fault vmf = { 19838c63ca5bSWill Deacon .vma = vma, 19848c63ca5bSWill Deacon .address = addr, 19858c63ca5bSWill Deacon .pmd = pmd, 19868c63ca5bSWill Deacon }; 19878c63ca5bSWill Deacon 1988ebc5951eSAndrea Righi page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 1989ebc5951eSAndrea Righi &vmf); 1990ebc5951eSAndrea Righi } 1991b56a2d8aSVineeth Remanan Pillai if (!page) { 1992b56a2d8aSVineeth Remanan Pillai if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD) 1993b56a2d8aSVineeth Remanan Pillai goto try_next; 1994b56a2d8aSVineeth Remanan Pillai return -ENOMEM; 19951da177e4SLinus Torvalds } 1996b56a2d8aSVineeth Remanan Pillai 1997b56a2d8aSVineeth Remanan Pillai lock_page(page); 1998b56a2d8aSVineeth Remanan Pillai wait_on_page_writeback(page); 1999b56a2d8aSVineeth Remanan Pillai ret = unuse_pte(vma, pmd, addr, entry, page); 2000b56a2d8aSVineeth Remanan Pillai if (ret < 0) { 2001b56a2d8aSVineeth Remanan Pillai unlock_page(page); 2002b56a2d8aSVineeth Remanan Pillai put_page(page); 2003b56a2d8aSVineeth Remanan Pillai goto out; 2004b56a2d8aSVineeth Remanan Pillai } 2005b56a2d8aSVineeth Remanan Pillai 2006b56a2d8aSVineeth Remanan Pillai try_to_free_swap(page); 2007b56a2d8aSVineeth Remanan Pillai unlock_page(page); 2008b56a2d8aSVineeth Remanan Pillai put_page(page); 2009b56a2d8aSVineeth Remanan Pillai 2010b56a2d8aSVineeth Remanan Pillai if (*fs_pages_to_unuse && !--(*fs_pages_to_unuse)) { 2011b56a2d8aSVineeth Remanan Pillai ret = FRONTSWAP_PAGES_UNUSED; 2012b56a2d8aSVineeth Remanan Pillai goto out; 2013b56a2d8aSVineeth Remanan Pillai } 2014b56a2d8aSVineeth Remanan Pillai try_next: 2015b56a2d8aSVineeth Remanan Pillai pte = pte_offset_map(pmd, addr); 20161da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 2017044d66c1SHugh Dickins pte_unmap(pte - 1); 2018b56a2d8aSVineeth Remanan Pillai 2019b56a2d8aSVineeth Remanan Pillai ret = 0; 2020044d66c1SHugh Dickins out: 20218a9f3ccdSBalbir Singh return ret; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds 20241da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 20251da177e4SLinus Torvalds unsigned long addr, unsigned long end, 2026b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 2027b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds pmd_t *pmd; 20301da177e4SLinus Torvalds unsigned long next; 20318a9f3ccdSBalbir Singh int ret; 20321da177e4SLinus Torvalds 20331da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 20341da177e4SLinus Torvalds do { 2035dc644a07SHugh Dickins cond_resched(); 20361da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 20371a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 20381da177e4SLinus Torvalds continue; 2039b56a2d8aSVineeth Remanan Pillai ret = unuse_pte_range(vma, pmd, addr, next, type, 2040b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 20418a9f3ccdSBalbir Singh if (ret) 20428a9f3ccdSBalbir Singh return ret; 20431da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 20441da177e4SLinus Torvalds return 0; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 2047c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d, 20481da177e4SLinus Torvalds unsigned long addr, unsigned long end, 2049b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 2050b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 20511da177e4SLinus Torvalds { 20521da177e4SLinus Torvalds pud_t *pud; 20531da177e4SLinus Torvalds unsigned long next; 20548a9f3ccdSBalbir Singh int ret; 20551da177e4SLinus Torvalds 2056c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 20571da177e4SLinus Torvalds do { 20581da177e4SLinus Torvalds next = pud_addr_end(addr, end); 20591da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 20601da177e4SLinus Torvalds continue; 2061b56a2d8aSVineeth Remanan Pillai ret = unuse_pmd_range(vma, pud, addr, next, type, 2062b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 20638a9f3ccdSBalbir Singh if (ret) 20648a9f3ccdSBalbir Singh return ret; 20651da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 20661da177e4SLinus Torvalds return 0; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd, 2070c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2071b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 2072b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 2073c2febafcSKirill A. Shutemov { 2074c2febafcSKirill A. Shutemov p4d_t *p4d; 2075c2febafcSKirill A. Shutemov unsigned long next; 2076c2febafcSKirill A. Shutemov int ret; 2077c2febafcSKirill A. Shutemov 2078c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 2079c2febafcSKirill A. Shutemov do { 2080c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2081c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 2082c2febafcSKirill A. Shutemov continue; 2083b56a2d8aSVineeth Remanan Pillai ret = unuse_pud_range(vma, p4d, addr, next, type, 2084b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 2085c2febafcSKirill A. Shutemov if (ret) 2086c2febafcSKirill A. Shutemov return ret; 2087c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2088c2febafcSKirill A. Shutemov return 0; 2089c2febafcSKirill A. Shutemov } 2090c2febafcSKirill A. Shutemov 2091b56a2d8aSVineeth Remanan Pillai static int unuse_vma(struct vm_area_struct *vma, unsigned int type, 2092b56a2d8aSVineeth Remanan Pillai bool frontswap, unsigned long *fs_pages_to_unuse) 20931da177e4SLinus Torvalds { 20941da177e4SLinus Torvalds pgd_t *pgd; 20951da177e4SLinus Torvalds unsigned long addr, end, next; 20968a9f3ccdSBalbir Singh int ret; 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds addr = vma->vm_start; 20991da177e4SLinus Torvalds end = vma->vm_end; 21001da177e4SLinus Torvalds 21011da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 21021da177e4SLinus Torvalds do { 21031da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 21041da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 21051da177e4SLinus Torvalds continue; 2106b56a2d8aSVineeth Remanan Pillai ret = unuse_p4d_range(vma, pgd, addr, next, type, 2107b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 21088a9f3ccdSBalbir Singh if (ret) 21098a9f3ccdSBalbir Singh return ret; 21101da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 21111da177e4SLinus Torvalds return 0; 21121da177e4SLinus Torvalds } 21131da177e4SLinus Torvalds 2114b56a2d8aSVineeth Remanan Pillai static int unuse_mm(struct mm_struct *mm, unsigned int type, 2115b56a2d8aSVineeth Remanan Pillai bool frontswap, unsigned long *fs_pages_to_unuse) 21161da177e4SLinus Torvalds { 21171da177e4SLinus Torvalds struct vm_area_struct *vma; 21188a9f3ccdSBalbir Singh int ret = 0; 21191da177e4SLinus Torvalds 2120d8ed45c5SMichel Lespinasse mmap_read_lock(mm); 21211da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 2122b56a2d8aSVineeth Remanan Pillai if (vma->anon_vma) { 2123b56a2d8aSVineeth Remanan Pillai ret = unuse_vma(vma, type, frontswap, 2124b56a2d8aSVineeth Remanan Pillai fs_pages_to_unuse); 2125b56a2d8aSVineeth Remanan Pillai if (ret) 21261da177e4SLinus Torvalds break; 2127b56a2d8aSVineeth Remanan Pillai } 2128dc644a07SHugh Dickins cond_resched(); 21291da177e4SLinus Torvalds } 2130d8ed45c5SMichel Lespinasse mmap_read_unlock(mm); 2131b56a2d8aSVineeth Remanan Pillai return ret; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 21341da177e4SLinus Torvalds /* 213538b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 2136b56a2d8aSVineeth Remanan Pillai * from current position to next entry still in use. Return 0 2137b56a2d8aSVineeth Remanan Pillai * if there are no inuse entries after prev till end of the map. 21381da177e4SLinus Torvalds */ 21396eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 214038b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 21411da177e4SLinus Torvalds { 2142b56a2d8aSVineeth Remanan Pillai unsigned int i; 21438d69aaeeSHugh Dickins unsigned char count; 21441da177e4SLinus Torvalds 21451da177e4SLinus Torvalds /* 21465d337b91SHugh Dickins * No need for swap_lock here: we're just looking 21471da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 21481da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 21495d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 21501da177e4SLinus Torvalds */ 2151b56a2d8aSVineeth Remanan Pillai for (i = prev + 1; i < si->max; i++) { 21524db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 2153355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 2154dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 21551da177e4SLinus Torvalds break; 2156dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 2157dc644a07SHugh Dickins cond_resched(); 21581da177e4SLinus Torvalds } 2159b56a2d8aSVineeth Remanan Pillai 2160b56a2d8aSVineeth Remanan Pillai if (i == si->max) 2161b56a2d8aSVineeth Remanan Pillai i = 0; 2162b56a2d8aSVineeth Remanan Pillai 21631da177e4SLinus Torvalds return i; 21641da177e4SLinus Torvalds } 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /* 2167b56a2d8aSVineeth Remanan Pillai * If the boolean frontswap is true, only unuse pages_to_unuse pages; 216838b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 21691da177e4SLinus Torvalds */ 217038b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 217138b5faf4SDan Magenheimer unsigned long pages_to_unuse) 21721da177e4SLinus Torvalds { 2173b56a2d8aSVineeth Remanan Pillai struct mm_struct *prev_mm; 2174b56a2d8aSVineeth Remanan Pillai struct mm_struct *mm; 2175b56a2d8aSVineeth Remanan Pillai struct list_head *p; 2176b56a2d8aSVineeth Remanan Pillai int retval = 0; 2177efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 21781da177e4SLinus Torvalds struct page *page; 21791da177e4SLinus Torvalds swp_entry_t entry; 2180b56a2d8aSVineeth Remanan Pillai unsigned int i; 21811da177e4SLinus Torvalds 218221820948SQian Cai if (!READ_ONCE(si->inuse_pages)) 2183b56a2d8aSVineeth Remanan Pillai return 0; 21841da177e4SLinus Torvalds 2185b56a2d8aSVineeth Remanan Pillai if (!frontswap) 2186b56a2d8aSVineeth Remanan Pillai pages_to_unuse = 0; 2187b56a2d8aSVineeth Remanan Pillai 2188b56a2d8aSVineeth Remanan Pillai retry: 2189b56a2d8aSVineeth Remanan Pillai retval = shmem_unuse(type, frontswap, &pages_to_unuse); 2190b56a2d8aSVineeth Remanan Pillai if (retval) 2191b56a2d8aSVineeth Remanan Pillai goto out; 2192b56a2d8aSVineeth Remanan Pillai 2193b56a2d8aSVineeth Remanan Pillai prev_mm = &init_mm; 2194b56a2d8aSVineeth Remanan Pillai mmget(prev_mm); 2195b56a2d8aSVineeth Remanan Pillai 2196b56a2d8aSVineeth Remanan Pillai spin_lock(&mmlist_lock); 2197b56a2d8aSVineeth Remanan Pillai p = &init_mm.mmlist; 219821820948SQian Cai while (READ_ONCE(si->inuse_pages) && 219964165b1aSHugh Dickins !signal_pending(current) && 220064165b1aSHugh Dickins (p = p->next) != &init_mm.mmlist) { 22011da177e4SLinus Torvalds 22021da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 2203388f7934SVegard Nossum if (!mmget_not_zero(mm)) 22041da177e4SLinus Torvalds continue; 22051da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 22061da177e4SLinus Torvalds mmput(prev_mm); 22071da177e4SLinus Torvalds prev_mm = mm; 2208b56a2d8aSVineeth Remanan Pillai retval = unuse_mm(mm, type, frontswap, &pages_to_unuse); 22091da177e4SLinus Torvalds 22101da177e4SLinus Torvalds if (retval) { 2211b56a2d8aSVineeth Remanan Pillai mmput(prev_mm); 2212b56a2d8aSVineeth Remanan Pillai goto out; 22131da177e4SLinus Torvalds } 22141da177e4SLinus Torvalds 22151da177e4SLinus Torvalds /* 22161da177e4SLinus Torvalds * Make sure that we aren't completely killing 22171da177e4SLinus Torvalds * interactive performance. 22181da177e4SLinus Torvalds */ 22191da177e4SLinus Torvalds cond_resched(); 2220b56a2d8aSVineeth Remanan Pillai spin_lock(&mmlist_lock); 222138b5faf4SDan Magenheimer } 2222b56a2d8aSVineeth Remanan Pillai spin_unlock(&mmlist_lock); 2223b56a2d8aSVineeth Remanan Pillai 2224b56a2d8aSVineeth Remanan Pillai mmput(prev_mm); 2225b56a2d8aSVineeth Remanan Pillai 2226b56a2d8aSVineeth Remanan Pillai i = 0; 222721820948SQian Cai while (READ_ONCE(si->inuse_pages) && 222864165b1aSHugh Dickins !signal_pending(current) && 222964165b1aSHugh Dickins (i = find_next_to_unuse(si, i, frontswap)) != 0) { 2230b56a2d8aSVineeth Remanan Pillai 2231b56a2d8aSVineeth Remanan Pillai entry = swp_entry(type, i); 2232b56a2d8aSVineeth Remanan Pillai page = find_get_page(swap_address_space(entry), i); 2233b56a2d8aSVineeth Remanan Pillai if (!page) 2234b56a2d8aSVineeth Remanan Pillai continue; 2235b56a2d8aSVineeth Remanan Pillai 2236b56a2d8aSVineeth Remanan Pillai /* 2237b56a2d8aSVineeth Remanan Pillai * It is conceivable that a racing task removed this page from 2238b56a2d8aSVineeth Remanan Pillai * swap cache just before we acquired the page lock. The page 2239b56a2d8aSVineeth Remanan Pillai * might even be back in swap cache on another swap area. But 2240b56a2d8aSVineeth Remanan Pillai * that is okay, try_to_free_swap() only removes stale pages. 2241b56a2d8aSVineeth Remanan Pillai */ 2242b56a2d8aSVineeth Remanan Pillai lock_page(page); 2243b56a2d8aSVineeth Remanan Pillai wait_on_page_writeback(page); 2244b56a2d8aSVineeth Remanan Pillai try_to_free_swap(page); 2245b56a2d8aSVineeth Remanan Pillai unlock_page(page); 2246b56a2d8aSVineeth Remanan Pillai put_page(page); 2247b56a2d8aSVineeth Remanan Pillai 2248b56a2d8aSVineeth Remanan Pillai /* 2249b56a2d8aSVineeth Remanan Pillai * For frontswap, we just need to unuse pages_to_unuse, if 2250b56a2d8aSVineeth Remanan Pillai * it was specified. Need not check frontswap again here as 2251b56a2d8aSVineeth Remanan Pillai * we already zeroed out pages_to_unuse if not frontswap. 2252b56a2d8aSVineeth Remanan Pillai */ 2253b56a2d8aSVineeth Remanan Pillai if (pages_to_unuse && --pages_to_unuse == 0) 2254b56a2d8aSVineeth Remanan Pillai goto out; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 2257b56a2d8aSVineeth Remanan Pillai /* 2258b56a2d8aSVineeth Remanan Pillai * Lets check again to see if there are still swap entries in the map. 2259b56a2d8aSVineeth Remanan Pillai * If yes, we would need to do retry the unuse logic again. 2260b56a2d8aSVineeth Remanan Pillai * Under global memory pressure, swap entries can be reinserted back 2261b56a2d8aSVineeth Remanan Pillai * into process space after the mmlist loop above passes over them. 2262dd862debSHugh Dickins * 2263af53d3e9SHugh Dickins * Limit the number of retries? No: when mmget_not_zero() above fails, 2264af53d3e9SHugh Dickins * that mm is likely to be freeing swap from exit_mmap(), which proceeds 2265af53d3e9SHugh Dickins * at its own independent pace; and even shmem_writepage() could have 2266af53d3e9SHugh Dickins * been preempted after get_swap_page(), temporarily hiding that swap. 2267af53d3e9SHugh Dickins * It's easy and robust (though cpu-intensive) just to keep retrying. 2268b56a2d8aSVineeth Remanan Pillai */ 226921820948SQian Cai if (READ_ONCE(si->inuse_pages)) { 227064165b1aSHugh Dickins if (!signal_pending(current)) 2271b56a2d8aSVineeth Remanan Pillai goto retry; 227264165b1aSHugh Dickins retval = -EINTR; 227364165b1aSHugh Dickins } 2274b56a2d8aSVineeth Remanan Pillai out: 2275b56a2d8aSVineeth Remanan Pillai return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds /* 22795d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 22805d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 22815d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 22821da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 22831da177e4SLinus Torvalds */ 22841da177e4SLinus Torvalds static void drain_mmlist(void) 22851da177e4SLinus Torvalds { 22861da177e4SLinus Torvalds struct list_head *p, *next; 2287efa90a98SHugh Dickins unsigned int type; 22881da177e4SLinus Torvalds 2289efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 2290efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 22911da177e4SLinus Torvalds return; 22921da177e4SLinus Torvalds spin_lock(&mmlist_lock); 22931da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 22941da177e4SLinus Torvalds list_del_init(p); 22951da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 22961da177e4SLinus Torvalds } 22971da177e4SLinus Torvalds 22981da177e4SLinus Torvalds /* 22991da177e4SLinus Torvalds * Free all of a swapdev's extent information 23001da177e4SLinus Torvalds */ 23011da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 23021da177e4SLinus Torvalds { 23034efaceb1SAaron Lu while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) { 23044efaceb1SAaron Lu struct rb_node *rb = sis->swap_extent_root.rb_node; 23054efaceb1SAaron Lu struct swap_extent *se = rb_entry(rb, struct swap_extent, rb_node); 23061da177e4SLinus Torvalds 23074efaceb1SAaron Lu rb_erase(rb, &sis->swap_extent_root); 23081da177e4SLinus Torvalds kfree(se); 23091da177e4SLinus Torvalds } 231062c230bcSMel Gorman 2311bc4ae27dSOmar Sandoval if (sis->flags & SWP_ACTIVATED) { 231262c230bcSMel Gorman struct file *swap_file = sis->swap_file; 231362c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 231462c230bcSMel Gorman 2315bc4ae27dSOmar Sandoval sis->flags &= ~SWP_ACTIVATED; 2316bc4ae27dSOmar Sandoval if (mapping->a_ops->swap_deactivate) 231762c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 231862c230bcSMel Gorman } 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds /* 23221da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 23234efaceb1SAaron Lu * extent tree. 23241da177e4SLinus Torvalds * 232511d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 23261da177e4SLinus Torvalds */ 2327a509bc1aSMel Gorman int 23281da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 23291da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 23301da177e4SLinus Torvalds { 23314efaceb1SAaron Lu struct rb_node **link = &sis->swap_extent_root.rb_node, *parent = NULL; 23321da177e4SLinus Torvalds struct swap_extent *se; 23331da177e4SLinus Torvalds struct swap_extent *new_se; 23341da177e4SLinus Torvalds 23354efaceb1SAaron Lu /* 23364efaceb1SAaron Lu * place the new node at the right most since the 23374efaceb1SAaron Lu * function is called in ascending page order. 23384efaceb1SAaron Lu */ 23394efaceb1SAaron Lu while (*link) { 23404efaceb1SAaron Lu parent = *link; 23414efaceb1SAaron Lu link = &parent->rb_right; 23424efaceb1SAaron Lu } 23434efaceb1SAaron Lu 23444efaceb1SAaron Lu if (parent) { 23454efaceb1SAaron Lu se = rb_entry(parent, struct swap_extent, rb_node); 234611d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 234711d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 23481da177e4SLinus Torvalds /* Merge it */ 23491da177e4SLinus Torvalds se->nr_pages += nr_pages; 23501da177e4SLinus Torvalds return 0; 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds } 23531da177e4SLinus Torvalds 23544efaceb1SAaron Lu /* No merge, insert a new extent. */ 23551da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 23561da177e4SLinus Torvalds if (new_se == NULL) 23571da177e4SLinus Torvalds return -ENOMEM; 23581da177e4SLinus Torvalds new_se->start_page = start_page; 23591da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 23601da177e4SLinus Torvalds new_se->start_block = start_block; 23611da177e4SLinus Torvalds 23624efaceb1SAaron Lu rb_link_node(&new_se->rb_node, parent, link); 23634efaceb1SAaron Lu rb_insert_color(&new_se->rb_node, &sis->swap_extent_root); 236453092a74SHugh Dickins return 1; 23651da177e4SLinus Torvalds } 2366aa8aa8a3SOmar Sandoval EXPORT_SYMBOL_GPL(add_swap_extent); 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds /* 23691da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 23701da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 23711da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 23721da177e4SLinus Torvalds * time for locating where on disk a page belongs. 23731da177e4SLinus Torvalds * 23741da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 23751da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 23761da177e4SLinus Torvalds * swap files identically. 23771da177e4SLinus Torvalds * 23781da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 23791da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 23801da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 23811da177e4SLinus Torvalds * 23821da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 23831da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 23841da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 23851da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 23861da177e4SLinus Torvalds * for swapping. 23871da177e4SLinus Torvalds * 23881638045cSDarrick J. Wong * For all swap devices we set S_SWAPFILE across the life of the swapon. This 23891638045cSDarrick J. Wong * prevents users from writing to the swap device, which will corrupt memory. 23901da177e4SLinus Torvalds * 23911da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 23921da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 23931da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 23941da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 23951da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 23961da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 23971da177e4SLinus Torvalds */ 239853092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 23991da177e4SLinus Torvalds { 240062c230bcSMel Gorman struct file *swap_file = sis->swap_file; 240162c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 240262c230bcSMel Gorman struct inode *inode = mapping->host; 24031da177e4SLinus Torvalds int ret; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 24061da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 240753092a74SHugh Dickins *span = sis->pages; 2408a509bc1aSMel Gorman return ret; 24091da177e4SLinus Torvalds } 24101da177e4SLinus Torvalds 241162c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 2412a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 2413bc4ae27dSOmar Sandoval if (ret >= 0) 2414bc4ae27dSOmar Sandoval sis->flags |= SWP_ACTIVATED; 241562c230bcSMel Gorman if (!ret) { 241632646315SGao Xiang sis->flags |= SWP_FS_OPS; 241762c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 241862c230bcSMel Gorman *span = sis->pages; 241962c230bcSMel Gorman } 24209625a5f2SHugh Dickins return ret; 2421a509bc1aSMel Gorman } 2422a509bc1aSMel Gorman 2423a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 24241da177e4SLinus Torvalds } 24251da177e4SLinus Torvalds 2426a2468cc9SAaron Lu static int swap_node(struct swap_info_struct *p) 2427a2468cc9SAaron Lu { 2428a2468cc9SAaron Lu struct block_device *bdev; 2429a2468cc9SAaron Lu 2430a2468cc9SAaron Lu if (p->bdev) 2431a2468cc9SAaron Lu bdev = p->bdev; 2432a2468cc9SAaron Lu else 2433a2468cc9SAaron Lu bdev = p->swap_file->f_inode->i_sb->s_bdev; 2434a2468cc9SAaron Lu 2435a2468cc9SAaron Lu return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE; 2436a2468cc9SAaron Lu } 2437a2468cc9SAaron Lu 2438eb085574SHuang Ying static void setup_swap_info(struct swap_info_struct *p, int prio, 24392a8f9449SShaohua Li unsigned char *swap_map, 24402a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 244140531542SCesar Eduardo Barros { 2442a2468cc9SAaron Lu int i; 2443a2468cc9SAaron Lu 244440531542SCesar Eduardo Barros if (prio >= 0) 244540531542SCesar Eduardo Barros p->prio = prio; 244640531542SCesar Eduardo Barros else 244740531542SCesar Eduardo Barros p->prio = --least_priority; 244818ab4d4cSDan Streetman /* 244918ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 245018ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 245118ab4d4cSDan Streetman */ 245218ab4d4cSDan Streetman p->list.prio = -p->prio; 2453a2468cc9SAaron Lu for_each_node(i) { 2454a2468cc9SAaron Lu if (p->prio >= 0) 2455a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2456a2468cc9SAaron Lu else { 2457a2468cc9SAaron Lu if (swap_node(p) == i) 2458a2468cc9SAaron Lu p->avail_lists[i].prio = 1; 2459a2468cc9SAaron Lu else 2460a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2461a2468cc9SAaron Lu } 2462a2468cc9SAaron Lu } 246340531542SCesar Eduardo Barros p->swap_map = swap_map; 24642a8f9449SShaohua Li p->cluster_info = cluster_info; 2465eb085574SHuang Ying } 2466eb085574SHuang Ying 2467eb085574SHuang Ying static void _enable_swap_info(struct swap_info_struct *p) 2468eb085574SHuang Ying { 2469eb085574SHuang Ying p->flags |= SWP_WRITEOK | SWP_VALID; 2470ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 247140531542SCesar Eduardo Barros total_swap_pages += p->pages; 247240531542SCesar Eduardo Barros 2473adfab836SDan Streetman assert_spin_locked(&swap_lock); 2474adfab836SDan Streetman /* 247518ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 247618ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 247718ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 247818ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 247918ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 248018ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 248118ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 248218ab4d4cSDan Streetman * swap_info_struct. 2483adfab836SDan Streetman */ 248418ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 2485a2468cc9SAaron Lu add_to_avail_list(p); 2486cf0cac0aSCesar Eduardo Barros } 2487cf0cac0aSCesar Eduardo Barros 2488cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 2489cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 24902a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2491cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 2492cf0cac0aSCesar Eduardo Barros { 24934f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 2494cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2495ec8acf20SShaohua Li spin_lock(&p->lock); 2496eb085574SHuang Ying setup_swap_info(p, prio, swap_map, cluster_info); 2497eb085574SHuang Ying spin_unlock(&p->lock); 2498eb085574SHuang Ying spin_unlock(&swap_lock); 2499eb085574SHuang Ying /* 2500eb085574SHuang Ying * Guarantee swap_map, cluster_info, etc. fields are valid 2501eb085574SHuang Ying * between get/put_swap_device() if SWP_VALID bit is set 2502eb085574SHuang Ying */ 2503eb085574SHuang Ying synchronize_rcu(); 2504eb085574SHuang Ying spin_lock(&swap_lock); 2505eb085574SHuang Ying spin_lock(&p->lock); 2506eb085574SHuang Ying _enable_swap_info(p); 2507ec8acf20SShaohua Li spin_unlock(&p->lock); 2508cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 2509cf0cac0aSCesar Eduardo Barros } 2510cf0cac0aSCesar Eduardo Barros 2511cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 2512cf0cac0aSCesar Eduardo Barros { 2513cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2514ec8acf20SShaohua Li spin_lock(&p->lock); 2515eb085574SHuang Ying setup_swap_info(p, p->prio, p->swap_map, p->cluster_info); 2516eb085574SHuang Ying _enable_swap_info(p); 2517ec8acf20SShaohua Li spin_unlock(&p->lock); 251840531542SCesar Eduardo Barros spin_unlock(&swap_lock); 251940531542SCesar Eduardo Barros } 252040531542SCesar Eduardo Barros 252167afa38eSTim Chen bool has_usable_swap(void) 252267afa38eSTim Chen { 252367afa38eSTim Chen bool ret = true; 252467afa38eSTim Chen 252567afa38eSTim Chen spin_lock(&swap_lock); 252667afa38eSTim Chen if (plist_head_empty(&swap_active_head)) 252767afa38eSTim Chen ret = false; 252867afa38eSTim Chen spin_unlock(&swap_lock); 252967afa38eSTim Chen return ret; 253067afa38eSTim Chen } 253167afa38eSTim Chen 2532c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 25331da177e4SLinus Torvalds { 25341da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 25358d69aaeeSHugh Dickins unsigned char *swap_map; 25362a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 25374f89849dSMinchan Kim unsigned long *frontswap_map; 25381da177e4SLinus Torvalds struct file *swap_file, *victim; 25391da177e4SLinus Torvalds struct address_space *mapping; 25401da177e4SLinus Torvalds struct inode *inode; 254191a27b2aSJeff Layton struct filename *pathname; 2542adfab836SDan Streetman int err, found = 0; 25435b808a23SKrzysztof Kozlowski unsigned int old_block_size; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 25461da177e4SLinus Torvalds return -EPERM; 25471da177e4SLinus Torvalds 2548191c5424SAl Viro BUG_ON(!current->mm); 2549191c5424SAl Viro 25501da177e4SLinus Torvalds pathname = getname(specialfile); 25511da177e4SLinus Torvalds if (IS_ERR(pathname)) 2552f58b59c1SXiaotian Feng return PTR_ERR(pathname); 25531da177e4SLinus Torvalds 2554669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 25551da177e4SLinus Torvalds err = PTR_ERR(victim); 25561da177e4SLinus Torvalds if (IS_ERR(victim)) 25571da177e4SLinus Torvalds goto out; 25581da177e4SLinus Torvalds 25591da177e4SLinus Torvalds mapping = victim->f_mapping; 25605d337b91SHugh Dickins spin_lock(&swap_lock); 256118ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 256222c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 2563adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 2564adfab836SDan Streetman found = 1; 25651da177e4SLinus Torvalds break; 25661da177e4SLinus Torvalds } 25671da177e4SLinus Torvalds } 2568adfab836SDan Streetman } 2569adfab836SDan Streetman if (!found) { 25701da177e4SLinus Torvalds err = -EINVAL; 25715d337b91SHugh Dickins spin_unlock(&swap_lock); 25721da177e4SLinus Torvalds goto out_dput; 25731da177e4SLinus Torvalds } 2574191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 25751da177e4SLinus Torvalds vm_unacct_memory(p->pages); 25761da177e4SLinus Torvalds else { 25771da177e4SLinus Torvalds err = -ENOMEM; 25785d337b91SHugh Dickins spin_unlock(&swap_lock); 25791da177e4SLinus Torvalds goto out_dput; 25801da177e4SLinus Torvalds } 2581a2468cc9SAaron Lu del_from_avail_list(p); 2582ec8acf20SShaohua Li spin_lock(&p->lock); 258378ecba08SHugh Dickins if (p->prio < 0) { 2584adfab836SDan Streetman struct swap_info_struct *si = p; 2585a2468cc9SAaron Lu int nid; 2586adfab836SDan Streetman 258718ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2588adfab836SDan Streetman si->prio++; 258918ab4d4cSDan Streetman si->list.prio--; 2590a2468cc9SAaron Lu for_each_node(nid) { 2591a2468cc9SAaron Lu if (si->avail_lists[nid].prio != 1) 2592a2468cc9SAaron Lu si->avail_lists[nid].prio--; 2593a2468cc9SAaron Lu } 2594adfab836SDan Streetman } 259578ecba08SHugh Dickins least_priority++; 259678ecba08SHugh Dickins } 259718ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2598ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 25991da177e4SLinus Torvalds total_swap_pages -= p->pages; 26001da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2601ec8acf20SShaohua Li spin_unlock(&p->lock); 26025d337b91SHugh Dickins spin_unlock(&swap_lock); 2603fb4f88dcSHugh Dickins 2604039939a6STim Chen disable_swap_slots_cache_lock(); 2605039939a6STim Chen 2606e1e12d2fSDavid Rientjes set_current_oom_origin(); 2607adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2608e1e12d2fSDavid Rientjes clear_current_oom_origin(); 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds if (err) { 26111da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2612cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 2613039939a6STim Chen reenable_swap_slots_cache_unlock(); 26141da177e4SLinus Torvalds goto out_dput; 26151da177e4SLinus Torvalds } 261652b7efdbSHugh Dickins 2617039939a6STim Chen reenable_swap_slots_cache_unlock(); 2618039939a6STim Chen 2619eb085574SHuang Ying spin_lock(&swap_lock); 2620eb085574SHuang Ying spin_lock(&p->lock); 2621eb085574SHuang Ying p->flags &= ~SWP_VALID; /* mark swap device as invalid */ 2622eb085574SHuang Ying spin_unlock(&p->lock); 2623eb085574SHuang Ying spin_unlock(&swap_lock); 2624eb085574SHuang Ying /* 2625eb085574SHuang Ying * wait for swap operations protected by get/put_swap_device() 2626eb085574SHuang Ying * to complete 2627eb085574SHuang Ying */ 2628eb085574SHuang Ying synchronize_rcu(); 2629eb085574SHuang Ying 2630815c2c54SShaohua Li flush_work(&p->discard_work); 2631815c2c54SShaohua Li 26324cd3bb10SHugh Dickins destroy_swap_extents(p); 2633570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2634570a335bSHugh Dickins free_swap_count_continuations(p); 2635570a335bSHugh Dickins 263681a0298bSHuang Ying if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev))) 263781a0298bSHuang Ying atomic_dec(&nr_rotate_swap); 263881a0298bSHuang Ying 2639fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 26405d337b91SHugh Dickins spin_lock(&swap_lock); 2641ec8acf20SShaohua Li spin_lock(&p->lock); 26421da177e4SLinus Torvalds drain_mmlist(); 26435d337b91SHugh Dickins 26445d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 26455d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 26465d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2647ec8acf20SShaohua Li spin_unlock(&p->lock); 26485d337b91SHugh Dickins spin_unlock(&swap_lock); 264913e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 26505d337b91SHugh Dickins spin_lock(&swap_lock); 2651ec8acf20SShaohua Li spin_lock(&p->lock); 26525d337b91SHugh Dickins } 26535d337b91SHugh Dickins 26541da177e4SLinus Torvalds swap_file = p->swap_file; 26555b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 26561da177e4SLinus Torvalds p->swap_file = NULL; 26571da177e4SLinus Torvalds p->max = 0; 26581da177e4SLinus Torvalds swap_map = p->swap_map; 26591da177e4SLinus Torvalds p->swap_map = NULL; 26602a8f9449SShaohua Li cluster_info = p->cluster_info; 26612a8f9449SShaohua Li p->cluster_info = NULL; 26624f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2663ec8acf20SShaohua Li spin_unlock(&p->lock); 26645d337b91SHugh Dickins spin_unlock(&swap_lock); 26658a84802eSSteven Price arch_swap_invalidate_area(p->type); 2666adfab836SDan Streetman frontswap_invalidate_area(p->type); 266758e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2668fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2669ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2670ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 267149070588SHuang Ying free_percpu(p->cluster_next_cpu); 267249070588SHuang Ying p->cluster_next_cpu = NULL; 26731da177e4SLinus Torvalds vfree(swap_map); 267454f180d3SHuang Ying kvfree(cluster_info); 267554f180d3SHuang Ying kvfree(frontswap_map); 26762de1a7e4SSeth Jennings /* Destroy swap account information */ 2677adfab836SDan Streetman swap_cgroup_swapoff(p->type); 26784b3ef9daSHuang, Ying exit_swap_address_space(p->type); 267927a7faa0SKAMEZAWA Hiroyuki 26801da177e4SLinus Torvalds inode = mapping->host; 26811da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 26821da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 26831638045cSDarrick J. Wong 26845b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2685e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 26861638045cSDarrick J. Wong } 26871638045cSDarrick J. Wong 26885955102cSAl Viro inode_lock(inode); 26891da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 26905955102cSAl Viro inode_unlock(inode); 26911da177e4SLinus Torvalds filp_close(swap_file, NULL); 2692f893ab41SWeijie Yang 2693f893ab41SWeijie Yang /* 2694f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2695f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2696f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2697f893ab41SWeijie Yang */ 2698f893ab41SWeijie Yang spin_lock(&swap_lock); 2699f893ab41SWeijie Yang p->flags = 0; 2700f893ab41SWeijie Yang spin_unlock(&swap_lock); 2701f893ab41SWeijie Yang 27021da177e4SLinus Torvalds err = 0; 270366d7dd51SKay Sievers atomic_inc(&proc_poll_event); 270466d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 27051da177e4SLinus Torvalds 27061da177e4SLinus Torvalds out_dput: 27071da177e4SLinus Torvalds filp_close(victim, NULL); 27081da177e4SLinus Torvalds out: 2709f58b59c1SXiaotian Feng putname(pathname); 27101da177e4SLinus Torvalds return err; 27111da177e4SLinus Torvalds } 27121da177e4SLinus Torvalds 27131da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 27149dd95748SAl Viro static __poll_t swaps_poll(struct file *file, poll_table *wait) 271566d7dd51SKay Sievers { 2716f1514638SKay Sievers struct seq_file *seq = file->private_data; 271766d7dd51SKay Sievers 271866d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 271966d7dd51SKay Sievers 2720f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2721f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2722a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; 272366d7dd51SKay Sievers } 272466d7dd51SKay Sievers 2725a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM; 272666d7dd51SKay Sievers } 272766d7dd51SKay Sievers 27281da177e4SLinus Torvalds /* iterator */ 27291da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 27301da177e4SLinus Torvalds { 2731efa90a98SHugh Dickins struct swap_info_struct *si; 2732efa90a98SHugh Dickins int type; 27331da177e4SLinus Torvalds loff_t l = *pos; 27341da177e4SLinus Torvalds 2735fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 27361da177e4SLinus Torvalds 2737881e4aabSSuleiman Souhlal if (!l) 2738881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2739881e4aabSSuleiman Souhlal 2740c10d38ccSDaniel Jordan for (type = 0; (si = swap_type_to_swap_info(type)); type++) { 2741efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27421da177e4SLinus Torvalds continue; 2743881e4aabSSuleiman Souhlal if (!--l) 2744efa90a98SHugh Dickins return si; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 27471da177e4SLinus Torvalds return NULL; 27481da177e4SLinus Torvalds } 27491da177e4SLinus Torvalds 27501da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 27511da177e4SLinus Torvalds { 2752efa90a98SHugh Dickins struct swap_info_struct *si = v; 2753efa90a98SHugh Dickins int type; 27541da177e4SLinus Torvalds 2755881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2756efa90a98SHugh Dickins type = 0; 2757efa90a98SHugh Dickins else 2758efa90a98SHugh Dickins type = si->type + 1; 2759881e4aabSSuleiman Souhlal 276010c8d69fSVasily Averin ++(*pos); 2761c10d38ccSDaniel Jordan for (; (si = swap_type_to_swap_info(type)); type++) { 2762efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27631da177e4SLinus Torvalds continue; 2764efa90a98SHugh Dickins return si; 27651da177e4SLinus Torvalds } 27661da177e4SLinus Torvalds 27671da177e4SLinus Torvalds return NULL; 27681da177e4SLinus Torvalds } 27691da177e4SLinus Torvalds 27701da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 27711da177e4SLinus Torvalds { 2772fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 27731da177e4SLinus Torvalds } 27741da177e4SLinus Torvalds 27751da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 27761da177e4SLinus Torvalds { 2777efa90a98SHugh Dickins struct swap_info_struct *si = v; 27781da177e4SLinus Torvalds struct file *file; 27791da177e4SLinus Torvalds int len; 27806f793940SRandy Dunlap unsigned int bytes, inuse; 27811da177e4SLinus Torvalds 2782efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 27836f793940SRandy Dunlap seq_puts(swap, "Filename\t\t\t\tType\t\tSize\t\tUsed\t\tPriority\n"); 2784881e4aabSSuleiman Souhlal return 0; 2785881e4aabSSuleiman Souhlal } 27861da177e4SLinus Torvalds 27876f793940SRandy Dunlap bytes = si->pages << (PAGE_SHIFT - 10); 27886f793940SRandy Dunlap inuse = si->inuse_pages << (PAGE_SHIFT - 10); 27896f793940SRandy Dunlap 2790efa90a98SHugh Dickins file = si->swap_file; 27912726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 27926f793940SRandy Dunlap seq_printf(swap, "%*s%s\t%u\t%s%u\t%s%d\n", 27931da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2794496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 27951da177e4SLinus Torvalds "partition" : "file\t", 27966f793940SRandy Dunlap bytes, bytes < 10000000 ? "\t" : "", 27976f793940SRandy Dunlap inuse, inuse < 10000000 ? "\t" : "", 2798efa90a98SHugh Dickins si->prio); 27991da177e4SLinus Torvalds return 0; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds 280215ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 28031da177e4SLinus Torvalds .start = swap_start, 28041da177e4SLinus Torvalds .next = swap_next, 28051da177e4SLinus Torvalds .stop = swap_stop, 28061da177e4SLinus Torvalds .show = swap_show 28071da177e4SLinus Torvalds }; 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 28101da177e4SLinus Torvalds { 2811f1514638SKay Sievers struct seq_file *seq; 281266d7dd51SKay Sievers int ret; 281366d7dd51SKay Sievers 281466d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2815f1514638SKay Sievers if (ret) 281666d7dd51SKay Sievers return ret; 281766d7dd51SKay Sievers 2818f1514638SKay Sievers seq = file->private_data; 2819f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2820f1514638SKay Sievers return 0; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 282397a32539SAlexey Dobriyan static const struct proc_ops swaps_proc_ops = { 2824d919b33dSAlexey Dobriyan .proc_flags = PROC_ENTRY_PERMANENT, 282597a32539SAlexey Dobriyan .proc_open = swaps_open, 282697a32539SAlexey Dobriyan .proc_read = seq_read, 282797a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 282897a32539SAlexey Dobriyan .proc_release = seq_release, 282997a32539SAlexey Dobriyan .proc_poll = swaps_poll, 28301da177e4SLinus Torvalds }; 28311da177e4SLinus Torvalds 28321da177e4SLinus Torvalds static int __init procswaps_init(void) 28331da177e4SLinus Torvalds { 283497a32539SAlexey Dobriyan proc_create("swaps", 0, NULL, &swaps_proc_ops); 28351da177e4SLinus Torvalds return 0; 28361da177e4SLinus Torvalds } 28371da177e4SLinus Torvalds __initcall(procswaps_init); 28381da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 28391da177e4SLinus Torvalds 28401796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 28411796316aSJan Beulich static int __init max_swapfiles_check(void) 28421796316aSJan Beulich { 28431796316aSJan Beulich MAX_SWAPFILES_CHECK(); 28441796316aSJan Beulich return 0; 28451796316aSJan Beulich } 28461796316aSJan Beulich late_initcall(max_swapfiles_check); 28471796316aSJan Beulich #endif 28481796316aSJan Beulich 284953cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 28501da177e4SLinus Torvalds { 28511da177e4SLinus Torvalds struct swap_info_struct *p; 2852b11a76b3SQian Cai struct swap_info_struct *defer = NULL; 28531da177e4SLinus Torvalds unsigned int type; 2854a2468cc9SAaron Lu int i; 2855efa90a98SHugh Dickins 285696008744SGustavo A. R. Silva p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL); 2857efa90a98SHugh Dickins if (!p) 285853cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2859efa90a98SHugh Dickins 28605d337b91SHugh Dickins spin_lock(&swap_lock); 2861efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2862efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 28631da177e4SLinus Torvalds break; 2864efa90a98SHugh Dickins } 28650697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 28665d337b91SHugh Dickins spin_unlock(&swap_lock); 2867873d7bcfSVasily Averin kvfree(p); 2868730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 28691da177e4SLinus Torvalds } 2870efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2871efa90a98SHugh Dickins p->type = type; 2872c10d38ccSDaniel Jordan WRITE_ONCE(swap_info[type], p); 2873efa90a98SHugh Dickins /* 2874efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2875efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2876efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2877efa90a98SHugh Dickins */ 2878efa90a98SHugh Dickins smp_wmb(); 2879c10d38ccSDaniel Jordan WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1); 2880efa90a98SHugh Dickins } else { 2881b11a76b3SQian Cai defer = p; 2882efa90a98SHugh Dickins p = swap_info[type]; 2883efa90a98SHugh Dickins /* 2884efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2885efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2886efa90a98SHugh Dickins */ 2887efa90a98SHugh Dickins } 28884efaceb1SAaron Lu p->swap_extent_root = RB_ROOT; 288918ab4d4cSDan Streetman plist_node_init(&p->list, 0); 2890a2468cc9SAaron Lu for_each_node(i) 2891a2468cc9SAaron Lu plist_node_init(&p->avail_lists[i], 0); 28921da177e4SLinus Torvalds p->flags = SWP_USED; 28935d337b91SHugh Dickins spin_unlock(&swap_lock); 2894b11a76b3SQian Cai kvfree(defer); 2895ec8acf20SShaohua Li spin_lock_init(&p->lock); 28962628bd6fSHuang Ying spin_lock_init(&p->cont_lock); 2897efa90a98SHugh Dickins 289853cbb243SCesar Eduardo Barros return p; 289953cbb243SCesar Eduardo Barros } 290053cbb243SCesar Eduardo Barros 29014d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 29024d0e1e10SCesar Eduardo Barros { 29034d0e1e10SCesar Eduardo Barros int error; 29044d0e1e10SCesar Eduardo Barros 29054d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 2906ef16e1d9SChristoph Hellwig p->bdev = blkdev_get_by_dev(inode->i_rdev, 29076f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 2908ef16e1d9SChristoph Hellwig if (IS_ERR(p->bdev)) { 2909ef16e1d9SChristoph Hellwig error = PTR_ERR(p->bdev); 29104d0e1e10SCesar Eduardo Barros p->bdev = NULL; 29116f179af8SHugh Dickins return error; 29124d0e1e10SCesar Eduardo Barros } 29134d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 29144d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 29154d0e1e10SCesar Eduardo Barros if (error < 0) 291687ade72aSCesar Eduardo Barros return error; 291712d2966dSNaohiro Aota /* 291812d2966dSNaohiro Aota * Zoned block devices contain zones that have a sequential 291912d2966dSNaohiro Aota * write only restriction. Hence zoned block devices are not 292012d2966dSNaohiro Aota * suitable for swapping. Disallow them here. 292112d2966dSNaohiro Aota */ 2922e556f6baSChristoph Hellwig if (blk_queue_is_zoned(p->bdev->bd_disk->queue)) 292312d2966dSNaohiro Aota return -EINVAL; 29244d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 29254d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 29264d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 29271638045cSDarrick J. Wong } 29281638045cSDarrick J. Wong 29294d0e1e10SCesar Eduardo Barros return 0; 29304d0e1e10SCesar Eduardo Barros } 29314d0e1e10SCesar Eduardo Barros 2932377eeaa8SAndi Kleen 2933377eeaa8SAndi Kleen /* 2934377eeaa8SAndi Kleen * Find out how many pages are allowed for a single swap device. There 2935377eeaa8SAndi Kleen * are two limiting factors: 2936377eeaa8SAndi Kleen * 1) the number of bits for the swap offset in the swp_entry_t type, and 2937377eeaa8SAndi Kleen * 2) the number of bits in the swap pte, as defined by the different 2938377eeaa8SAndi Kleen * architectures. 2939377eeaa8SAndi Kleen * 2940377eeaa8SAndi Kleen * In order to find the largest possible bit mask, a swap entry with 2941377eeaa8SAndi Kleen * swap type 0 and swap offset ~0UL is created, encoded to a swap pte, 2942377eeaa8SAndi Kleen * decoded to a swp_entry_t again, and finally the swap offset is 2943377eeaa8SAndi Kleen * extracted. 2944377eeaa8SAndi Kleen * 2945377eeaa8SAndi Kleen * This will mask all the bits from the initial ~0UL mask that can't 2946377eeaa8SAndi Kleen * be encoded in either the swp_entry_t or the architecture definition 2947377eeaa8SAndi Kleen * of a swap pte. 2948377eeaa8SAndi Kleen */ 2949377eeaa8SAndi Kleen unsigned long generic_max_swapfile_size(void) 2950377eeaa8SAndi Kleen { 2951377eeaa8SAndi Kleen return swp_offset(pte_to_swp_entry( 2952377eeaa8SAndi Kleen swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2953377eeaa8SAndi Kleen } 2954377eeaa8SAndi Kleen 2955377eeaa8SAndi Kleen /* Can be overridden by an architecture for additional checks. */ 2956377eeaa8SAndi Kleen __weak unsigned long max_swapfile_size(void) 2957377eeaa8SAndi Kleen { 2958377eeaa8SAndi Kleen return generic_max_swapfile_size(); 2959377eeaa8SAndi Kleen } 2960377eeaa8SAndi Kleen 2961ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2962ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2963ca8bd38bSCesar Eduardo Barros struct inode *inode) 2964ca8bd38bSCesar Eduardo Barros { 2965ca8bd38bSCesar Eduardo Barros int i; 2966ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2967ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2968d6bbbd29SRaymond Jennings unsigned long last_page; 2969ca8bd38bSCesar Eduardo Barros 2970ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2971465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 297238719025SCesar Eduardo Barros return 0; 2973ca8bd38bSCesar Eduardo Barros } 2974ca8bd38bSCesar Eduardo Barros 2975ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2976ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2977ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2978ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2979ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2980dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2981dd111be6SJann Horn return 0; 2982ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2983ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2984ca8bd38bSCesar Eduardo Barros } 2985ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2986ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2987465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2988ca8bd38bSCesar Eduardo Barros swap_header->info.version); 298938719025SCesar Eduardo Barros return 0; 2990ca8bd38bSCesar Eduardo Barros } 2991ca8bd38bSCesar Eduardo Barros 2992ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2993ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2994ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2995ca8bd38bSCesar Eduardo Barros 2996377eeaa8SAndi Kleen maxpages = max_swapfile_size(); 2997d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2998a06ad633STom Abraham if (!last_page) { 2999a06ad633STom Abraham pr_warn("Empty swap-file\n"); 3000a06ad633STom Abraham return 0; 3001a06ad633STom Abraham } 3002d6bbbd29SRaymond Jennings if (last_page > maxpages) { 3003465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 3004d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 3005d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 3006d6bbbd29SRaymond Jennings } 3007d6bbbd29SRaymond Jennings if (maxpages > last_page) { 3008d6bbbd29SRaymond Jennings maxpages = last_page + 1; 3009ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 3010ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 3011ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 3012ca8bd38bSCesar Eduardo Barros } 3013ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 3014ca8bd38bSCesar Eduardo Barros 3015ca8bd38bSCesar Eduardo Barros if (!maxpages) 301638719025SCesar Eduardo Barros return 0; 3017ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 3018ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 3019465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 302038719025SCesar Eduardo Barros return 0; 3021ca8bd38bSCesar Eduardo Barros } 3022ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 302338719025SCesar Eduardo Barros return 0; 3024ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 302538719025SCesar Eduardo Barros return 0; 3026ca8bd38bSCesar Eduardo Barros 3027ca8bd38bSCesar Eduardo Barros return maxpages; 3028ca8bd38bSCesar Eduardo Barros } 3029ca8bd38bSCesar Eduardo Barros 30304b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS \ 3031235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 30324b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS \ 30334b3ef9daSHuang, Ying DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) 30344b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS \ 30354b3ef9daSHuang, Ying max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) 3036235b6217SHuang, Ying 3037915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 3038915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 3039915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 30402a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 3041915d4d7bSCesar Eduardo Barros unsigned long maxpages, 3042915d4d7bSCesar Eduardo Barros sector_t *span) 3043915d4d7bSCesar Eduardo Barros { 3044235b6217SHuang, Ying unsigned int j, k; 3045915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 3046915d4d7bSCesar Eduardo Barros int nr_extents; 30472a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 3048235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 3049235b6217SHuang, Ying unsigned long i, idx; 3050915d4d7bSCesar Eduardo Barros 3051915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 3052915d4d7bSCesar Eduardo Barros 30536b534915SHuang Ying cluster_list_init(&p->free_clusters); 30546b534915SHuang Ying cluster_list_init(&p->discard_clusters); 30552a8f9449SShaohua Li 3056915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 3057915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 3058bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 3059bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3060915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 3061915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 3062915d4d7bSCesar Eduardo Barros nr_good_pages--; 30632a8f9449SShaohua Li /* 30642a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 30652a8f9449SShaohua Li * operation involved 30662a8f9449SShaohua Li */ 30672a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 3068915d4d7bSCesar Eduardo Barros } 3069915d4d7bSCesar Eduardo Barros } 3070915d4d7bSCesar Eduardo Barros 30712a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 30722a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 30732a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 30742a8f9449SShaohua Li 3075915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 3076915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 30772a8f9449SShaohua Li /* 30782a8f9449SShaohua Li * Not mark the cluster free yet, no list 30792a8f9449SShaohua Li * operation involved 30802a8f9449SShaohua Li */ 30812a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 3082915d4d7bSCesar Eduardo Barros p->max = maxpages; 3083915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 3084915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 3085bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 3086bdb8e3f6SCesar Eduardo Barros return nr_extents; 3087915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 3088915d4d7bSCesar Eduardo Barros } 3089915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 3090465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 3091bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3092915d4d7bSCesar Eduardo Barros } 3093915d4d7bSCesar Eduardo Barros 30942a8f9449SShaohua Li if (!cluster_info) 30952a8f9449SShaohua Li return nr_extents; 30962a8f9449SShaohua Li 3097235b6217SHuang, Ying 30984b3ef9daSHuang, Ying /* 30994b3ef9daSHuang, Ying * Reduce false cache line sharing between cluster_info and 31004b3ef9daSHuang, Ying * sharing same address space. 31014b3ef9daSHuang, Ying */ 3102235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 3103235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 3104235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 3105235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 3106235b6217SHuang, Ying if (idx >= nr_clusters) 3107235b6217SHuang, Ying continue; 3108235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 3109235b6217SHuang, Ying continue; 31102a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 31116b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 31126b534915SHuang Ying idx); 31132a8f9449SShaohua Li } 31142a8f9449SShaohua Li } 3115915d4d7bSCesar Eduardo Barros return nr_extents; 3116915d4d7bSCesar Eduardo Barros } 3117915d4d7bSCesar Eduardo Barros 3118dcf6b7ddSRafael Aquini /* 3119dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 3120dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 3121dcf6b7ddSRafael Aquini */ 3122dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 3123dcf6b7ddSRafael Aquini { 3124dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 3125dcf6b7ddSRafael Aquini 3126dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 3127dcf6b7ddSRafael Aquini return false; 3128dcf6b7ddSRafael Aquini 3129dcf6b7ddSRafael Aquini return true; 3130dcf6b7ddSRafael Aquini } 3131dcf6b7ddSRafael Aquini 313253cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 313353cbb243SCesar Eduardo Barros { 313453cbb243SCesar Eduardo Barros struct swap_info_struct *p; 313591a27b2aSJeff Layton struct filename *name; 313653cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 313753cbb243SCesar Eduardo Barros struct address_space *mapping; 313840531542SCesar Eduardo Barros int prio; 313953cbb243SCesar Eduardo Barros int error; 314053cbb243SCesar Eduardo Barros union swap_header *swap_header; 3141915d4d7bSCesar Eduardo Barros int nr_extents; 314253cbb243SCesar Eduardo Barros sector_t span; 314353cbb243SCesar Eduardo Barros unsigned long maxpages; 314453cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 31452a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 314638b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 314753cbb243SCesar Eduardo Barros struct page *page = NULL; 314853cbb243SCesar Eduardo Barros struct inode *inode = NULL; 31497cbf3192SOmar Sandoval bool inced_nr_rotate_swap = false; 315053cbb243SCesar Eduardo Barros 3151d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 3152d15cab97SHugh Dickins return -EINVAL; 3153d15cab97SHugh Dickins 315453cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 315553cbb243SCesar Eduardo Barros return -EPERM; 315653cbb243SCesar Eduardo Barros 3157a2468cc9SAaron Lu if (!swap_avail_heads) 3158a2468cc9SAaron Lu return -ENOMEM; 3159a2468cc9SAaron Lu 316053cbb243SCesar Eduardo Barros p = alloc_swap_info(); 31612542e513SCesar Eduardo Barros if (IS_ERR(p)) 31622542e513SCesar Eduardo Barros return PTR_ERR(p); 316353cbb243SCesar Eduardo Barros 3164815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 3165815c2c54SShaohua Li 31661da177e4SLinus Torvalds name = getname(specialfile); 31671da177e4SLinus Torvalds if (IS_ERR(name)) { 31687de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 31691da177e4SLinus Torvalds name = NULL; 3170bd69010bSCesar Eduardo Barros goto bad_swap; 31711da177e4SLinus Torvalds } 3172669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 31731da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 31747de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 31751da177e4SLinus Torvalds swap_file = NULL; 3176bd69010bSCesar Eduardo Barros goto bad_swap; 31771da177e4SLinus Torvalds } 31781da177e4SLinus Torvalds 31791da177e4SLinus Torvalds p->swap_file = swap_file; 31801da177e4SLinus Torvalds mapping = swap_file->f_mapping; 31812130781eSCesar Eduardo Barros inode = mapping->host; 31826f179af8SHugh Dickins 31834d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 31844d0e1e10SCesar Eduardo Barros if (unlikely(error)) 31851da177e4SLinus Torvalds goto bad_swap; 31861da177e4SLinus Torvalds 3187d795a90eSNaohiro Aota inode_lock(inode); 3188d795a90eSNaohiro Aota if (IS_SWAPFILE(inode)) { 3189d795a90eSNaohiro Aota error = -EBUSY; 3190d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 3191d795a90eSNaohiro Aota } 3192d795a90eSNaohiro Aota 31931da177e4SLinus Torvalds /* 31941da177e4SLinus Torvalds * Read the swap header. 31951da177e4SLinus Torvalds */ 31961da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 31971da177e4SLinus Torvalds error = -EINVAL; 3198d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31991da177e4SLinus Torvalds } 3200090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 32011da177e4SLinus Torvalds if (IS_ERR(page)) { 32021da177e4SLinus Torvalds error = PTR_ERR(page); 3203d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32041da177e4SLinus Torvalds } 320581e33971SHugh Dickins swap_header = kmap(page); 32061da177e4SLinus Torvalds 3207ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 3208ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 32091da177e4SLinus Torvalds error = -EINVAL; 3210d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32111da177e4SLinus Torvalds } 32121da177e4SLinus Torvalds 32131da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 3214803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 321578ecba08SHugh Dickins if (!swap_map) { 32161da177e4SLinus Torvalds error = -ENOMEM; 3217d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32181da177e4SLinus Torvalds } 3219f0571429SMinchan Kim 32201cb039f3SChristoph Hellwig if (p->bdev && blk_queue_stable_writes(p->bdev->bd_disk->queue)) 3221f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 3222f0571429SMinchan Kim 3223a8b456d0SChristoph Hellwig if (p->bdev && p->bdev->bd_disk->fops->rw_page) 3224539a6feaSMinchan Kim p->flags |= SWP_SYNCHRONOUS_IO; 3225539a6feaSMinchan Kim 32262a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 32276f179af8SHugh Dickins int cpu; 3228235b6217SHuang, Ying unsigned long ci, nr_cluster; 32296f179af8SHugh Dickins 32302a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 323149070588SHuang Ying p->cluster_next_cpu = alloc_percpu(unsigned int); 323249070588SHuang Ying if (!p->cluster_next_cpu) { 323349070588SHuang Ying error = -ENOMEM; 323449070588SHuang Ying goto bad_swap_unlock_inode; 323549070588SHuang Ying } 32362a8f9449SShaohua Li /* 32372a8f9449SShaohua Li * select a random position to start with to help wear leveling 32382a8f9449SShaohua Li * SSD 32392a8f9449SShaohua Li */ 324049070588SHuang Ying for_each_possible_cpu(cpu) { 324149070588SHuang Ying per_cpu(*p->cluster_next_cpu, cpu) = 324249070588SHuang Ying 1 + prandom_u32_max(p->highest_bit); 324349070588SHuang Ying } 3244235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 32452a8f9449SShaohua Li 3246778e1cddSKees Cook cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info), 324754f180d3SHuang Ying GFP_KERNEL); 32482a8f9449SShaohua Li if (!cluster_info) { 32492a8f9449SShaohua Li error = -ENOMEM; 3250d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32512a8f9449SShaohua Li } 3252235b6217SHuang, Ying 3253235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 3254235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 3255235b6217SHuang, Ying 3256ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 3257ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 3258ebc2a1a6SShaohua Li error = -ENOMEM; 3259d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 3260ebc2a1a6SShaohua Li } 32616f179af8SHugh Dickins for_each_possible_cpu(cpu) { 3262ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 32636f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 3264ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 3265ebc2a1a6SShaohua Li } 32667cbf3192SOmar Sandoval } else { 326781a0298bSHuang Ying atomic_inc(&nr_rotate_swap); 32687cbf3192SOmar Sandoval inced_nr_rotate_swap = true; 32697cbf3192SOmar Sandoval } 32701da177e4SLinus Torvalds 32711421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 32721421ef3cSCesar Eduardo Barros if (error) 3273d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32741421ef3cSCesar Eduardo Barros 3275915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 32762a8f9449SShaohua Li cluster_info, maxpages, &span); 3277915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 327853092a74SHugh Dickins error = nr_extents; 3279d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 328053092a74SHugh Dickins } 328138b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 32828ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 3283778e1cddSKees Cook frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages), 3284778e1cddSKees Cook sizeof(long), 328554f180d3SHuang Ying GFP_KERNEL); 32861da177e4SLinus Torvalds 32872a8f9449SShaohua Li if (p->bdev && (swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 3288dcf6b7ddSRafael Aquini /* 3289dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 3290dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 3291dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 3292dcf6b7ddSRafael Aquini * swapon(8) releases. 3293dcf6b7ddSRafael Aquini */ 3294dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 3295dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 3296dcf6b7ddSRafael Aquini 3297dcf6b7ddSRafael Aquini /* 3298dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 3299dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 3300dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 3301dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 3302dcf6b7ddSRafael Aquini */ 3303dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 3304dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 3305dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 3306dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 3307dcf6b7ddSRafael Aquini 3308dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 3309dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 3310dcf6b7ddSRafael Aquini int err = discard_swap(p); 3311dcf6b7ddSRafael Aquini if (unlikely(err)) 3312465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 3313dcf6b7ddSRafael Aquini p, err); 3314dcf6b7ddSRafael Aquini } 3315dcf6b7ddSRafael Aquini } 33166a6ba831SHugh Dickins 33174b3ef9daSHuang, Ying error = init_swap_address_space(p->type, maxpages); 33184b3ef9daSHuang, Ying if (error) 3319d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 33204b3ef9daSHuang, Ying 3321dc617f29SDarrick J. Wong /* 3322dc617f29SDarrick J. Wong * Flush any pending IO and dirty mappings before we start using this 3323dc617f29SDarrick J. Wong * swap device. 3324dc617f29SDarrick J. Wong */ 3325dc617f29SDarrick J. Wong inode->i_flags |= S_SWAPFILE; 3326dc617f29SDarrick J. Wong error = inode_drain_writes(inode); 3327dc617f29SDarrick J. Wong if (error) { 3328dc617f29SDarrick J. Wong inode->i_flags &= ~S_SWAPFILE; 3329822bca52SMiaohe Lin goto free_swap_address_space; 3330dc617f29SDarrick J. Wong } 3331dc617f29SDarrick J. Wong 3332fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 333340531542SCesar Eduardo Barros prio = -1; 333478ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 333540531542SCesar Eduardo Barros prio = 333678ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 33372a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 3338c69dbfb8SCesar Eduardo Barros 3339756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 334091a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 3341c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 3342c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 334338b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 3344dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 3345dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 334638b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 3347c69dbfb8SCesar Eduardo Barros 3348fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 334966d7dd51SKay Sievers atomic_inc(&proc_poll_event); 335066d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 335166d7dd51SKay Sievers 33521da177e4SLinus Torvalds error = 0; 33531da177e4SLinus Torvalds goto out; 3354822bca52SMiaohe Lin free_swap_address_space: 3355822bca52SMiaohe Lin exit_swap_address_space(p->type); 3356d795a90eSNaohiro Aota bad_swap_unlock_inode: 3357d795a90eSNaohiro Aota inode_unlock(inode); 33581da177e4SLinus Torvalds bad_swap: 3359ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 3360ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 336149070588SHuang Ying free_percpu(p->cluster_next_cpu); 336249070588SHuang Ying p->cluster_next_cpu = NULL; 3363bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 3364f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 3365f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 33661da177e4SLinus Torvalds } 3367d795a90eSNaohiro Aota inode = NULL; 33684cd3bb10SHugh Dickins destroy_swap_extents(p); 3369e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 33705d337b91SHugh Dickins spin_lock(&swap_lock); 33711da177e4SLinus Torvalds p->swap_file = NULL; 33721da177e4SLinus Torvalds p->flags = 0; 33735d337b91SHugh Dickins spin_unlock(&swap_lock); 33741da177e4SLinus Torvalds vfree(swap_map); 33758606a1a9SDarrick J. Wong kvfree(cluster_info); 3376b6b1fd2aSDavid Rientjes kvfree(frontswap_map); 33777cbf3192SOmar Sandoval if (inced_nr_rotate_swap) 33787cbf3192SOmar Sandoval atomic_dec(&nr_rotate_swap); 3379d795a90eSNaohiro Aota if (swap_file) 33801da177e4SLinus Torvalds filp_close(swap_file, NULL); 33811da177e4SLinus Torvalds out: 33821da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 33831da177e4SLinus Torvalds kunmap(page); 338409cbfeafSKirill A. Shutemov put_page(page); 33851da177e4SLinus Torvalds } 33861da177e4SLinus Torvalds if (name) 33871da177e4SLinus Torvalds putname(name); 33881638045cSDarrick J. Wong if (inode) 33895955102cSAl Viro inode_unlock(inode); 3390039939a6STim Chen if (!error) 3391039939a6STim Chen enable_swap_slots_cache(); 33921da177e4SLinus Torvalds return error; 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds 33951da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 33961da177e4SLinus Torvalds { 3397efa90a98SHugh Dickins unsigned int type; 33981da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 33991da177e4SLinus Torvalds 34005d337b91SHugh Dickins spin_lock(&swap_lock); 3401efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 3402efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 3403efa90a98SHugh Dickins 3404efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 3405efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 34061da177e4SLinus Torvalds } 3407ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 34081da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 34095d337b91SHugh Dickins spin_unlock(&swap_lock); 34101da177e4SLinus Torvalds } 34111da177e4SLinus Torvalds 34121da177e4SLinus Torvalds /* 34131da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 34141da177e4SLinus Torvalds * 3415355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 3416355cfa73SKAMEZAWA Hiroyuki * - success -> 0 3417355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 3418355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 3419355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 3420355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 3421570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 34221da177e4SLinus Torvalds */ 34238d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 34241da177e4SLinus Torvalds { 34251da177e4SLinus Torvalds struct swap_info_struct *p; 3426235b6217SHuang, Ying struct swap_cluster_info *ci; 3427c10d38ccSDaniel Jordan unsigned long offset; 34288d69aaeeSHugh Dickins unsigned char count; 34298d69aaeeSHugh Dickins unsigned char has_cache; 34309d9a0334SMiaohe Lin int err; 34311da177e4SLinus Torvalds 3432eb085574SHuang Ying p = get_swap_device(entry); 3433c10d38ccSDaniel Jordan if (!p) 34349d9a0334SMiaohe Lin return -EINVAL; 3435c10d38ccSDaniel Jordan 34361da177e4SLinus Torvalds offset = swp_offset(entry); 3437235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 3438355cfa73SKAMEZAWA Hiroyuki 3439253d553bSHugh Dickins count = p->swap_map[offset]; 3440edfe23daSShaohua Li 3441edfe23daSShaohua Li /* 3442edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 3443edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 3444edfe23daSShaohua Li */ 3445edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 3446edfe23daSShaohua Li err = -ENOENT; 3447edfe23daSShaohua Li goto unlock_out; 3448edfe23daSShaohua Li } 3449edfe23daSShaohua Li 3450253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 3451253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 3452253d553bSHugh Dickins err = 0; 3453355cfa73SKAMEZAWA Hiroyuki 3454253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 3455355cfa73SKAMEZAWA Hiroyuki 3456355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 3457253d553bSHugh Dickins if (!has_cache && count) 3458253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 3459253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 3460253d553bSHugh Dickins err = -EEXIST; 3461253d553bSHugh Dickins else /* no users remaining */ 3462253d553bSHugh Dickins err = -ENOENT; 3463355cfa73SKAMEZAWA Hiroyuki 3464355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 3465253d553bSHugh Dickins 3466570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 3467570a335bSHugh Dickins count += usage; 3468570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 3469253d553bSHugh Dickins err = -EINVAL; 3470570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 3471570a335bSHugh Dickins count = COUNT_CONTINUED; 3472570a335bSHugh Dickins else 3473570a335bSHugh Dickins err = -ENOMEM; 3474253d553bSHugh Dickins } else 3475253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 3476253d553bSHugh Dickins 3477a449bf58SQian Cai WRITE_ONCE(p->swap_map[offset], count | has_cache); 3478253d553bSHugh Dickins 3479355cfa73SKAMEZAWA Hiroyuki unlock_out: 3480235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 3481eb085574SHuang Ying if (p) 3482eb085574SHuang Ying put_swap_device(p); 3483253d553bSHugh Dickins return err; 34841da177e4SLinus Torvalds } 3485253d553bSHugh Dickins 3486355cfa73SKAMEZAWA Hiroyuki /* 3487aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 3488aaa46865SHugh Dickins * (in which case its reference count is never incremented). 3489aaa46865SHugh Dickins */ 3490aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 3491aaa46865SHugh Dickins { 3492aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 3493aaa46865SHugh Dickins } 3494aaa46865SHugh Dickins 3495aaa46865SHugh Dickins /* 349608259d58SHugh Dickins * Increase reference count of swap entry by 1. 349708259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 349808259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 349908259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 350008259d58SHugh Dickins * might occur if a page table entry has got corrupted. 3501355cfa73SKAMEZAWA Hiroyuki */ 3502570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 3503355cfa73SKAMEZAWA Hiroyuki { 3504570a335bSHugh Dickins int err = 0; 3505570a335bSHugh Dickins 3506570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 3507570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 3508570a335bSHugh Dickins return err; 3509355cfa73SKAMEZAWA Hiroyuki } 35101da177e4SLinus Torvalds 3511cb4b86baSKAMEZAWA Hiroyuki /* 3512355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 3513355cfa73SKAMEZAWA Hiroyuki * 351473c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 3515355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 35163eeba135SChen Wandun * -EEXIST means there is a swap cache. 3517355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 3518cb4b86baSKAMEZAWA Hiroyuki */ 3519cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 3520cb4b86baSKAMEZAWA Hiroyuki { 3521253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 3522cb4b86baSKAMEZAWA Hiroyuki } 3523cb4b86baSKAMEZAWA Hiroyuki 35240bcac06fSMinchan Kim struct swap_info_struct *swp_swap_info(swp_entry_t entry) 35250bcac06fSMinchan Kim { 3526c10d38ccSDaniel Jordan return swap_type_to_swap_info(swp_type(entry)); 35270bcac06fSMinchan Kim } 35280bcac06fSMinchan Kim 3529f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 3530f981c595SMel Gorman { 35310bcac06fSMinchan Kim swp_entry_t entry = { .val = page_private(page) }; 35320bcac06fSMinchan Kim return swp_swap_info(entry); 3533f981c595SMel Gorman } 3534f981c595SMel Gorman 3535f981c595SMel Gorman /* 3536f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 3537f981c595SMel Gorman */ 3538f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 3539f981c595SMel Gorman { 3540f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 3541f981c595SMel Gorman } 3542f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 3543f981c595SMel Gorman 3544f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 3545f981c595SMel Gorman { 3546f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3547f981c595SMel Gorman return swp_offset(swap); 3548f981c595SMel Gorman } 3549f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 3550f981c595SMel Gorman 35511da177e4SLinus Torvalds /* 3552570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 3553570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 3554570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 3555570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 3556570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 3557570a335bSHugh Dickins * 3558570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 3559570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 3560570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 3561570a335bSHugh Dickins * 3562570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 3563570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 3564570a335bSHugh Dickins * can be called after dropping locks. 3565570a335bSHugh Dickins */ 3566570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 3567570a335bSHugh Dickins { 3568570a335bSHugh Dickins struct swap_info_struct *si; 3569235b6217SHuang, Ying struct swap_cluster_info *ci; 3570570a335bSHugh Dickins struct page *head; 3571570a335bSHugh Dickins struct page *page; 3572570a335bSHugh Dickins struct page *list_page; 3573570a335bSHugh Dickins pgoff_t offset; 3574570a335bSHugh Dickins unsigned char count; 3575eb085574SHuang Ying int ret = 0; 3576570a335bSHugh Dickins 3577570a335bSHugh Dickins /* 3578570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 3579570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 3580570a335bSHugh Dickins */ 3581570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 3582570a335bSHugh Dickins 3583eb085574SHuang Ying si = get_swap_device(entry); 3584570a335bSHugh Dickins if (!si) { 3585570a335bSHugh Dickins /* 3586570a335bSHugh Dickins * An acceptable race has occurred since the failing 3587eb085574SHuang Ying * __swap_duplicate(): the swap device may be swapoff 3588570a335bSHugh Dickins */ 3589570a335bSHugh Dickins goto outer; 3590570a335bSHugh Dickins } 3591eb085574SHuang Ying spin_lock(&si->lock); 3592570a335bSHugh Dickins 3593570a335bSHugh Dickins offset = swp_offset(entry); 3594235b6217SHuang, Ying 3595235b6217SHuang, Ying ci = lock_cluster(si, offset); 3596235b6217SHuang, Ying 3597d8aa24e0SMiaohe Lin count = swap_count(si->swap_map[offset]); 3598570a335bSHugh Dickins 3599570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 3600570a335bSHugh Dickins /* 3601570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 3602570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 3603570a335bSHugh Dickins * over-provisioning. 3604570a335bSHugh Dickins */ 3605570a335bSHugh Dickins goto out; 3606570a335bSHugh Dickins } 3607570a335bSHugh Dickins 3608570a335bSHugh Dickins if (!page) { 3609eb085574SHuang Ying ret = -ENOMEM; 3610eb085574SHuang Ying goto out; 3611570a335bSHugh Dickins } 3612570a335bSHugh Dickins 3613570a335bSHugh Dickins /* 3614570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 3615570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 3616570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 3617570a335bSHugh Dickins */ 3618570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3619570a335bSHugh Dickins offset &= ~PAGE_MASK; 3620570a335bSHugh Dickins 36212628bd6fSHuang Ying spin_lock(&si->cont_lock); 3622570a335bSHugh Dickins /* 3623570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 3624570a335bSHugh Dickins * but it does always reset its private field. 3625570a335bSHugh Dickins */ 3626570a335bSHugh Dickins if (!page_private(head)) { 3627570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3628570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 3629570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 3630570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 3631570a335bSHugh Dickins } 3632570a335bSHugh Dickins 3633570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 3634570a335bSHugh Dickins unsigned char *map; 3635570a335bSHugh Dickins 3636570a335bSHugh Dickins /* 3637570a335bSHugh Dickins * If the previous map said no continuation, but we've found 3638570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 3639570a335bSHugh Dickins */ 3640570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 36412628bd6fSHuang Ying goto out_unlock_cont; 3642570a335bSHugh Dickins 36439b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 3644570a335bSHugh Dickins count = *map; 36459b04c5feSCong Wang kunmap_atomic(map); 3646570a335bSHugh Dickins 3647570a335bSHugh Dickins /* 3648570a335bSHugh Dickins * If this continuation count now has some space in it, 3649570a335bSHugh Dickins * free our allocation and use this one. 3650570a335bSHugh Dickins */ 3651570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 36522628bd6fSHuang Ying goto out_unlock_cont; 3653570a335bSHugh Dickins } 3654570a335bSHugh Dickins 3655570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 3656570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 36572628bd6fSHuang Ying out_unlock_cont: 36582628bd6fSHuang Ying spin_unlock(&si->cont_lock); 3659570a335bSHugh Dickins out: 3660235b6217SHuang, Ying unlock_cluster(ci); 3661ec8acf20SShaohua Li spin_unlock(&si->lock); 3662eb085574SHuang Ying put_swap_device(si); 3663570a335bSHugh Dickins outer: 3664570a335bSHugh Dickins if (page) 3665570a335bSHugh Dickins __free_page(page); 3666eb085574SHuang Ying return ret; 3667570a335bSHugh Dickins } 3668570a335bSHugh Dickins 3669570a335bSHugh Dickins /* 3670570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3671570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3672570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3673570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3674570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3675235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3676235b6217SHuang, Ying * lock. 3677570a335bSHugh Dickins */ 3678570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3679570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3680570a335bSHugh Dickins { 3681570a335bSHugh Dickins struct page *head; 3682570a335bSHugh Dickins struct page *page; 3683570a335bSHugh Dickins unsigned char *map; 36842628bd6fSHuang Ying bool ret; 3685570a335bSHugh Dickins 3686570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3687570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3688570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3689570a335bSHugh Dickins return false; /* need to add count continuation */ 3690570a335bSHugh Dickins } 3691570a335bSHugh Dickins 36922628bd6fSHuang Ying spin_lock(&si->cont_lock); 3693570a335bSHugh Dickins offset &= ~PAGE_MASK; 3694213516acSchenqiwu page = list_next_entry(head, lru); 36959b04c5feSCong Wang map = kmap_atomic(page) + offset; 3696570a335bSHugh Dickins 3697570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3698570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3699570a335bSHugh Dickins 3700570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3701570a335bSHugh Dickins /* 3702570a335bSHugh Dickins * Think of how you add 1 to 999 3703570a335bSHugh Dickins */ 3704570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 37059b04c5feSCong Wang kunmap_atomic(map); 3706213516acSchenqiwu page = list_next_entry(page, lru); 3707570a335bSHugh Dickins BUG_ON(page == head); 37089b04c5feSCong Wang map = kmap_atomic(page) + offset; 3709570a335bSHugh Dickins } 3710570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 37119b04c5feSCong Wang kunmap_atomic(map); 3712213516acSchenqiwu page = list_next_entry(page, lru); 37132628bd6fSHuang Ying if (page == head) { 37142628bd6fSHuang Ying ret = false; /* add count continuation */ 37152628bd6fSHuang Ying goto out; 37162628bd6fSHuang Ying } 37179b04c5feSCong Wang map = kmap_atomic(page) + offset; 3718570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3719570a335bSHugh Dickins } 3720570a335bSHugh Dickins *map += 1; 37219b04c5feSCong Wang kunmap_atomic(map); 3722213516acSchenqiwu while ((page = list_prev_entry(page, lru)) != head) { 37239b04c5feSCong Wang map = kmap_atomic(page) + offset; 3724570a335bSHugh Dickins *map = COUNT_CONTINUED; 37259b04c5feSCong Wang kunmap_atomic(map); 3726570a335bSHugh Dickins } 37272628bd6fSHuang Ying ret = true; /* incremented */ 3728570a335bSHugh Dickins 3729570a335bSHugh Dickins } else { /* decrementing */ 3730570a335bSHugh Dickins /* 3731570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3732570a335bSHugh Dickins */ 3733570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3734570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 37359b04c5feSCong Wang kunmap_atomic(map); 3736213516acSchenqiwu page = list_next_entry(page, lru); 3737570a335bSHugh Dickins BUG_ON(page == head); 37389b04c5feSCong Wang map = kmap_atomic(page) + offset; 3739570a335bSHugh Dickins } 3740570a335bSHugh Dickins BUG_ON(*map == 0); 3741570a335bSHugh Dickins *map -= 1; 3742570a335bSHugh Dickins if (*map == 0) 3743570a335bSHugh Dickins count = 0; 37449b04c5feSCong Wang kunmap_atomic(map); 3745213516acSchenqiwu while ((page = list_prev_entry(page, lru)) != head) { 37469b04c5feSCong Wang map = kmap_atomic(page) + offset; 3747570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3748570a335bSHugh Dickins count = COUNT_CONTINUED; 37499b04c5feSCong Wang kunmap_atomic(map); 3750570a335bSHugh Dickins } 37512628bd6fSHuang Ying ret = count == COUNT_CONTINUED; 3752570a335bSHugh Dickins } 37532628bd6fSHuang Ying out: 37542628bd6fSHuang Ying spin_unlock(&si->cont_lock); 37552628bd6fSHuang Ying return ret; 3756570a335bSHugh Dickins } 3757570a335bSHugh Dickins 3758570a335bSHugh Dickins /* 3759570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3760570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3761570a335bSHugh Dickins */ 3762570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3763570a335bSHugh Dickins { 3764570a335bSHugh Dickins pgoff_t offset; 3765570a335bSHugh Dickins 3766570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3767570a335bSHugh Dickins struct page *head; 3768570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3769570a335bSHugh Dickins if (page_private(head)) { 37700d576d20SGeliang Tang struct page *page, *next; 37710d576d20SGeliang Tang 37720d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 37730d576d20SGeliang Tang list_del(&page->lru); 3774570a335bSHugh Dickins __free_page(page); 3775570a335bSHugh Dickins } 3776570a335bSHugh Dickins } 3777570a335bSHugh Dickins } 3778570a335bSHugh Dickins } 3779a2468cc9SAaron Lu 37802cf85583STejun Heo #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) 37816caa6a07SJohannes Weiner void cgroup_throttle_swaprate(struct page *page, gfp_t gfp_mask) 37822cf85583STejun Heo { 37832cf85583STejun Heo struct swap_info_struct *si, *next; 37846caa6a07SJohannes Weiner int nid = page_to_nid(page); 37856caa6a07SJohannes Weiner 37866caa6a07SJohannes Weiner if (!(gfp_mask & __GFP_IO)) 37872cf85583STejun Heo return; 37882cf85583STejun Heo 37892cf85583STejun Heo if (!blk_cgroup_congested()) 37902cf85583STejun Heo return; 37912cf85583STejun Heo 37922cf85583STejun Heo /* 37932cf85583STejun Heo * We've already scheduled a throttle, avoid taking the global swap 37942cf85583STejun Heo * lock. 37952cf85583STejun Heo */ 37962cf85583STejun Heo if (current->throttle_queue) 37972cf85583STejun Heo return; 37982cf85583STejun Heo 37992cf85583STejun Heo spin_lock(&swap_avail_lock); 38006caa6a07SJohannes Weiner plist_for_each_entry_safe(si, next, &swap_avail_heads[nid], 38016caa6a07SJohannes Weiner avail_lists[nid]) { 38022cf85583STejun Heo if (si->bdev) { 38036caa6a07SJohannes Weiner blkcg_schedule_throttle(bdev_get_queue(si->bdev), true); 38042cf85583STejun Heo break; 38052cf85583STejun Heo } 38062cf85583STejun Heo } 38072cf85583STejun Heo spin_unlock(&swap_avail_lock); 38082cf85583STejun Heo } 38092cf85583STejun Heo #endif 38102cf85583STejun Heo 3811a2468cc9SAaron Lu static int __init swapfile_init(void) 3812a2468cc9SAaron Lu { 3813a2468cc9SAaron Lu int nid; 3814a2468cc9SAaron Lu 3815a2468cc9SAaron Lu swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head), 3816a2468cc9SAaron Lu GFP_KERNEL); 3817a2468cc9SAaron Lu if (!swap_avail_heads) { 3818a2468cc9SAaron Lu pr_emerg("Not enough memory for swap heads, swap is disabled\n"); 3819a2468cc9SAaron Lu return -ENOMEM; 3820a2468cc9SAaron Lu } 3821a2468cc9SAaron Lu 3822a2468cc9SAaron Lu for_each_node(nid) 3823a2468cc9SAaron Lu plist_head_init(&swap_avail_heads[nid]); 3824a2468cc9SAaron Lu 3825a2468cc9SAaron Lu return 0; 3826a2468cc9SAaron Lu } 3827a2468cc9SAaron Lu subsys_initcall(swapfile_init); 3828