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/pgtable.h> 441da177e4SLinus Torvalds #include <asm/tlbflush.h> 451da177e4SLinus Torvalds #include <linux/swapops.h> 465d1ea48bSJohannes Weiner #include <linux/swap_cgroup.h> 471da177e4SLinus Torvalds 48570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 49570a335bSHugh Dickins unsigned char); 50570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 51d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 52570a335bSHugh Dickins 5338b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 547c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 55ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 56fb0fec50SChris Wilson /* 57fb0fec50SChris Wilson * Some modules use swappable objects and may try to swap them out under 58fb0fec50SChris Wilson * memory pressure (via the shrinker). Before doing so, they may wish to 59fb0fec50SChris Wilson * check to see if any swap space is available. 60fb0fec50SChris Wilson */ 61fb0fec50SChris Wilson EXPORT_SYMBOL_GPL(nr_swap_pages); 62ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 631da177e4SLinus Torvalds long total_swap_pages; 64a2468cc9SAaron Lu static int least_priority = -1; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 671da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 681da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 691da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 701da177e4SLinus Torvalds 71adfab836SDan Streetman /* 72adfab836SDan Streetman * all active swap_info_structs 73adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 74adfab836SDan Streetman */ 7518ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 7618ab4d4cSDan Streetman 7718ab4d4cSDan Streetman /* 7818ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 7918ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 8018ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 8118ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 8218ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 8318ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 8418ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 8518ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 8618ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 8718ab4d4cSDan Streetman * before any swap_info_struct->lock. 8818ab4d4cSDan Streetman */ 89bfc6b1caSColin Ian King static struct plist_head *swap_avail_heads; 9018ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 911da177e4SLinus Torvalds 9238b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 931da177e4SLinus Torvalds 94fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 951da177e4SLinus Torvalds 9666d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 9766d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 9866d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 9966d7dd51SKay Sievers 10081a0298bSHuang Ying atomic_t nr_rotate_swap = ATOMIC_INIT(0); 10181a0298bSHuang Ying 102c10d38ccSDaniel Jordan static struct swap_info_struct *swap_type_to_swap_info(int type) 103c10d38ccSDaniel Jordan { 104c10d38ccSDaniel Jordan if (type >= READ_ONCE(nr_swapfiles)) 105c10d38ccSDaniel Jordan return NULL; 106c10d38ccSDaniel Jordan 107c10d38ccSDaniel Jordan smp_rmb(); /* Pairs with smp_wmb in alloc_swap_info. */ 108c10d38ccSDaniel Jordan return READ_ONCE(swap_info[type]); 109c10d38ccSDaniel Jordan } 110c10d38ccSDaniel Jordan 1118d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 112355cfa73SKAMEZAWA Hiroyuki { 113955c97f0SDaniel Jordan return ent & ~SWAP_HAS_CACHE; /* may include COUNT_CONTINUED flag */ 114355cfa73SKAMEZAWA Hiroyuki } 115355cfa73SKAMEZAWA Hiroyuki 116bcd49e86SHuang Ying /* Reclaim the swap entry anyway if possible */ 117bcd49e86SHuang Ying #define TTRS_ANYWAY 0x1 118bcd49e86SHuang Ying /* 119bcd49e86SHuang Ying * Reclaim the swap entry if there are no more mappings of the 120bcd49e86SHuang Ying * corresponding page 121bcd49e86SHuang Ying */ 122bcd49e86SHuang Ying #define TTRS_UNMAPPED 0x2 123bcd49e86SHuang Ying /* Reclaim the swap entry if swap is getting full*/ 124bcd49e86SHuang Ying #define TTRS_FULL 0x4 125bcd49e86SHuang Ying 126efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 127bcd49e86SHuang Ying static int __try_to_reclaim_swap(struct swap_info_struct *si, 128bcd49e86SHuang Ying unsigned long offset, unsigned long flags) 129c9e44410SKAMEZAWA Hiroyuki { 130efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 131c9e44410SKAMEZAWA Hiroyuki struct page *page; 132c9e44410SKAMEZAWA Hiroyuki int ret = 0; 133c9e44410SKAMEZAWA Hiroyuki 134bcd49e86SHuang Ying page = find_get_page(swap_address_space(entry), offset); 135c9e44410SKAMEZAWA Hiroyuki if (!page) 136c9e44410SKAMEZAWA Hiroyuki return 0; 137c9e44410SKAMEZAWA Hiroyuki /* 138bcd49e86SHuang Ying * When this function is called from scan_swap_map_slots() and it's 139bcd49e86SHuang Ying * called by vmscan.c at reclaiming pages. So, we hold a lock on a page, 140bcd49e86SHuang Ying * here. We have to use trylock for avoiding deadlock. This is a special 141c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 142c9e44410SKAMEZAWA Hiroyuki * in usual operations. 143c9e44410SKAMEZAWA Hiroyuki */ 144c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 145bcd49e86SHuang Ying if ((flags & TTRS_ANYWAY) || 146bcd49e86SHuang Ying ((flags & TTRS_UNMAPPED) && !page_mapped(page)) || 147bcd49e86SHuang Ying ((flags & TTRS_FULL) && mem_cgroup_swap_full(page))) 148c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 149c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 150c9e44410SKAMEZAWA Hiroyuki } 15109cbfeafSKirill A. Shutemov put_page(page); 152c9e44410SKAMEZAWA Hiroyuki return ret; 153c9e44410SKAMEZAWA Hiroyuki } 154355cfa73SKAMEZAWA Hiroyuki 1554efaceb1SAaron Lu static inline struct swap_extent *first_se(struct swap_info_struct *sis) 1564efaceb1SAaron Lu { 1574efaceb1SAaron Lu struct rb_node *rb = rb_first(&sis->swap_extent_root); 1584efaceb1SAaron Lu return rb_entry(rb, struct swap_extent, rb_node); 1594efaceb1SAaron Lu } 1604efaceb1SAaron Lu 1614efaceb1SAaron Lu static inline struct swap_extent *next_se(struct swap_extent *se) 1624efaceb1SAaron Lu { 1634efaceb1SAaron Lu struct rb_node *rb = rb_next(&se->rb_node); 1644efaceb1SAaron Lu return rb ? rb_entry(rb, struct swap_extent, rb_node) : NULL; 1654efaceb1SAaron Lu } 1664efaceb1SAaron Lu 1671da177e4SLinus Torvalds /* 1686a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1696a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1706a6ba831SHugh Dickins */ 1716a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1726a6ba831SHugh Dickins { 1736a6ba831SHugh Dickins struct swap_extent *se; 1749625a5f2SHugh Dickins sector_t start_block; 1759625a5f2SHugh Dickins sector_t nr_blocks; 1766a6ba831SHugh Dickins int err = 0; 1776a6ba831SHugh Dickins 1786a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1794efaceb1SAaron Lu se = first_se(si); 1809625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1819625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1829625a5f2SHugh Dickins if (nr_blocks) { 1839625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 184dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1859625a5f2SHugh Dickins if (err) 1869625a5f2SHugh Dickins return err; 1879625a5f2SHugh Dickins cond_resched(); 1886a6ba831SHugh Dickins } 1896a6ba831SHugh Dickins 1904efaceb1SAaron Lu for (se = next_se(se); se; se = next_se(se)) { 1919625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1929625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1939625a5f2SHugh Dickins 1946a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 195dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1966a6ba831SHugh Dickins if (err) 1976a6ba831SHugh Dickins break; 1986a6ba831SHugh Dickins 1996a6ba831SHugh Dickins cond_resched(); 2006a6ba831SHugh Dickins } 2016a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 2026a6ba831SHugh Dickins } 2036a6ba831SHugh Dickins 2044efaceb1SAaron Lu static struct swap_extent * 2054efaceb1SAaron Lu offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset) 2064efaceb1SAaron Lu { 2074efaceb1SAaron Lu struct swap_extent *se; 2084efaceb1SAaron Lu struct rb_node *rb; 2094efaceb1SAaron Lu 2104efaceb1SAaron Lu rb = sis->swap_extent_root.rb_node; 2114efaceb1SAaron Lu while (rb) { 2124efaceb1SAaron Lu se = rb_entry(rb, struct swap_extent, rb_node); 2134efaceb1SAaron Lu if (offset < se->start_page) 2144efaceb1SAaron Lu rb = rb->rb_left; 2154efaceb1SAaron Lu else if (offset >= se->start_page + se->nr_pages) 2164efaceb1SAaron Lu rb = rb->rb_right; 2174efaceb1SAaron Lu else 2184efaceb1SAaron Lu return se; 2194efaceb1SAaron Lu } 2204efaceb1SAaron Lu /* It *must* be present */ 2214efaceb1SAaron Lu BUG(); 2224efaceb1SAaron Lu } 2234efaceb1SAaron Lu 2247992fde7SHugh Dickins /* 2257992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 2267992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 2277992fde7SHugh Dickins */ 2287992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 2297992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 2307992fde7SHugh Dickins { 2314efaceb1SAaron Lu struct swap_extent *se = offset_to_swap_extent(si, start_page); 2327992fde7SHugh Dickins 2337992fde7SHugh Dickins while (nr_pages) { 2347992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 2357992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 236858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 2377992fde7SHugh Dickins 2387992fde7SHugh Dickins if (nr_blocks > nr_pages) 2397992fde7SHugh Dickins nr_blocks = nr_pages; 2407992fde7SHugh Dickins start_page += nr_blocks; 2417992fde7SHugh Dickins nr_pages -= nr_blocks; 2427992fde7SHugh Dickins 2437992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 2447992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 2457992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 246dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 2477992fde7SHugh Dickins break; 2487992fde7SHugh Dickins 2494efaceb1SAaron Lu se = next_se(se); 2507992fde7SHugh Dickins } 2517992fde7SHugh Dickins } 2527992fde7SHugh Dickins 25338d8b4e6SHuang Ying #ifdef CONFIG_THP_SWAP 25438d8b4e6SHuang Ying #define SWAPFILE_CLUSTER HPAGE_PMD_NR 255a448f2d0SHuang Ying 256a448f2d0SHuang Ying #define swap_entry_size(size) (size) 25738d8b4e6SHuang Ying #else 258048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 259a448f2d0SHuang Ying 260a448f2d0SHuang Ying /* 261a448f2d0SHuang Ying * Define swap_entry_size() as constant to let compiler to optimize 262a448f2d0SHuang Ying * out some code if !CONFIG_THP_SWAP 263a448f2d0SHuang Ying */ 264a448f2d0SHuang Ying #define swap_entry_size(size) 1 26538d8b4e6SHuang Ying #endif 266048c27fdSHugh Dickins #define LATENCY_LIMIT 256 267048c27fdSHugh Dickins 2682a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2692a8f9449SShaohua Li unsigned int flag) 2702a8f9449SShaohua Li { 2712a8f9449SShaohua Li info->flags = flag; 2722a8f9449SShaohua Li } 2732a8f9449SShaohua Li 2742a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2752a8f9449SShaohua Li { 2762a8f9449SShaohua Li return info->data; 2772a8f9449SShaohua Li } 2782a8f9449SShaohua Li 2792a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2802a8f9449SShaohua Li unsigned int c) 2812a8f9449SShaohua Li { 2822a8f9449SShaohua Li info->data = c; 2832a8f9449SShaohua Li } 2842a8f9449SShaohua Li 2852a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2862a8f9449SShaohua Li unsigned int c, unsigned int f) 2872a8f9449SShaohua Li { 2882a8f9449SShaohua Li info->flags = f; 2892a8f9449SShaohua Li info->data = c; 2902a8f9449SShaohua Li } 2912a8f9449SShaohua Li 2922a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2932a8f9449SShaohua Li { 2942a8f9449SShaohua Li return info->data; 2952a8f9449SShaohua Li } 2962a8f9449SShaohua Li 2972a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2982a8f9449SShaohua Li unsigned int n) 2992a8f9449SShaohua Li { 3002a8f9449SShaohua Li info->data = n; 3012a8f9449SShaohua Li } 3022a8f9449SShaohua Li 3032a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 3042a8f9449SShaohua Li unsigned int n, unsigned int f) 3052a8f9449SShaohua Li { 3062a8f9449SShaohua Li info->flags = f; 3072a8f9449SShaohua Li info->data = n; 3082a8f9449SShaohua Li } 3092a8f9449SShaohua Li 3102a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 3112a8f9449SShaohua Li { 3122a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 3132a8f9449SShaohua Li } 3142a8f9449SShaohua Li 3152a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 3162a8f9449SShaohua Li { 3172a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 3182a8f9449SShaohua Li } 3192a8f9449SShaohua Li 3202a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 3212a8f9449SShaohua Li { 3222a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 3232a8f9449SShaohua Li info->data = 0; 3242a8f9449SShaohua Li } 3252a8f9449SShaohua Li 326e0709829SHuang Ying static inline bool cluster_is_huge(struct swap_cluster_info *info) 327e0709829SHuang Ying { 32833ee011eSHuang Ying if (IS_ENABLED(CONFIG_THP_SWAP)) 329e0709829SHuang Ying return info->flags & CLUSTER_FLAG_HUGE; 33033ee011eSHuang Ying return false; 331e0709829SHuang Ying } 332e0709829SHuang Ying 333e0709829SHuang Ying static inline void cluster_clear_huge(struct swap_cluster_info *info) 334e0709829SHuang Ying { 335e0709829SHuang Ying info->flags &= ~CLUSTER_FLAG_HUGE; 336e0709829SHuang Ying } 337e0709829SHuang Ying 338235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si, 339235b6217SHuang, Ying unsigned long offset) 340235b6217SHuang, Ying { 341235b6217SHuang, Ying struct swap_cluster_info *ci; 342235b6217SHuang, Ying 343235b6217SHuang, Ying ci = si->cluster_info; 344235b6217SHuang, Ying if (ci) { 345235b6217SHuang, Ying ci += offset / SWAPFILE_CLUSTER; 346235b6217SHuang, Ying spin_lock(&ci->lock); 347235b6217SHuang, Ying } 348235b6217SHuang, Ying return ci; 349235b6217SHuang, Ying } 350235b6217SHuang, Ying 351235b6217SHuang, Ying static inline void unlock_cluster(struct swap_cluster_info *ci) 352235b6217SHuang, Ying { 353235b6217SHuang, Ying if (ci) 354235b6217SHuang, Ying spin_unlock(&ci->lock); 355235b6217SHuang, Ying } 356235b6217SHuang, Ying 35759d98bf3SHuang Ying /* 35859d98bf3SHuang Ying * Determine the locking method in use for this device. Return 35959d98bf3SHuang Ying * swap_cluster_info if SSD-style cluster-based locking is in place. 36059d98bf3SHuang Ying */ 361235b6217SHuang, Ying static inline struct swap_cluster_info *lock_cluster_or_swap_info( 36259d98bf3SHuang Ying struct swap_info_struct *si, unsigned long offset) 363235b6217SHuang, Ying { 364235b6217SHuang, Ying struct swap_cluster_info *ci; 365235b6217SHuang, Ying 36659d98bf3SHuang Ying /* Try to use fine-grained SSD-style locking if available: */ 367235b6217SHuang, Ying ci = lock_cluster(si, offset); 36859d98bf3SHuang Ying /* Otherwise, fall back to traditional, coarse locking: */ 369235b6217SHuang, Ying if (!ci) 370235b6217SHuang, Ying spin_lock(&si->lock); 371235b6217SHuang, Ying 372235b6217SHuang, Ying return ci; 373235b6217SHuang, Ying } 374235b6217SHuang, Ying 375235b6217SHuang, Ying static inline void unlock_cluster_or_swap_info(struct swap_info_struct *si, 376235b6217SHuang, Ying struct swap_cluster_info *ci) 377235b6217SHuang, Ying { 378235b6217SHuang, Ying if (ci) 379235b6217SHuang, Ying unlock_cluster(ci); 380235b6217SHuang, Ying else 381235b6217SHuang, Ying spin_unlock(&si->lock); 382235b6217SHuang, Ying } 383235b6217SHuang, Ying 3846b534915SHuang Ying static inline bool cluster_list_empty(struct swap_cluster_list *list) 3856b534915SHuang Ying { 3866b534915SHuang Ying return cluster_is_null(&list->head); 3876b534915SHuang Ying } 3886b534915SHuang Ying 3896b534915SHuang Ying static inline unsigned int cluster_list_first(struct swap_cluster_list *list) 3906b534915SHuang Ying { 3916b534915SHuang Ying return cluster_next(&list->head); 3926b534915SHuang Ying } 3936b534915SHuang Ying 3946b534915SHuang Ying static void cluster_list_init(struct swap_cluster_list *list) 3956b534915SHuang Ying { 3966b534915SHuang Ying cluster_set_null(&list->head); 3976b534915SHuang Ying cluster_set_null(&list->tail); 3986b534915SHuang Ying } 3996b534915SHuang Ying 4006b534915SHuang Ying static void cluster_list_add_tail(struct swap_cluster_list *list, 4016b534915SHuang Ying struct swap_cluster_info *ci, 4026b534915SHuang Ying unsigned int idx) 4036b534915SHuang Ying { 4046b534915SHuang Ying if (cluster_list_empty(list)) { 4056b534915SHuang Ying cluster_set_next_flag(&list->head, idx, 0); 4066b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 4076b534915SHuang Ying } else { 408235b6217SHuang, Ying struct swap_cluster_info *ci_tail; 4096b534915SHuang Ying unsigned int tail = cluster_next(&list->tail); 4106b534915SHuang Ying 411235b6217SHuang, Ying /* 412235b6217SHuang, Ying * Nested cluster lock, but both cluster locks are 413235b6217SHuang, Ying * only acquired when we held swap_info_struct->lock 414235b6217SHuang, Ying */ 415235b6217SHuang, Ying ci_tail = ci + tail; 416235b6217SHuang, Ying spin_lock_nested(&ci_tail->lock, SINGLE_DEPTH_NESTING); 417235b6217SHuang, Ying cluster_set_next(ci_tail, idx); 4180ef017d1SHuang Ying spin_unlock(&ci_tail->lock); 4196b534915SHuang Ying cluster_set_next_flag(&list->tail, idx, 0); 4206b534915SHuang Ying } 4216b534915SHuang Ying } 4226b534915SHuang Ying 4236b534915SHuang Ying static unsigned int cluster_list_del_first(struct swap_cluster_list *list, 4246b534915SHuang Ying struct swap_cluster_info *ci) 4256b534915SHuang Ying { 4266b534915SHuang Ying unsigned int idx; 4276b534915SHuang Ying 4286b534915SHuang Ying idx = cluster_next(&list->head); 4296b534915SHuang Ying if (cluster_next(&list->tail) == idx) { 4306b534915SHuang Ying cluster_set_null(&list->head); 4316b534915SHuang Ying cluster_set_null(&list->tail); 4326b534915SHuang Ying } else 4336b534915SHuang Ying cluster_set_next_flag(&list->head, 4346b534915SHuang Ying cluster_next(&ci[idx]), 0); 4356b534915SHuang Ying 4366b534915SHuang Ying return idx; 4376b534915SHuang Ying } 4386b534915SHuang Ying 439815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 440815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 441815c2c54SShaohua Li unsigned int idx) 442815c2c54SShaohua Li { 443815c2c54SShaohua Li /* 444815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 445815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 446815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 447815c2c54SShaohua Li * will be cleared after discard 448815c2c54SShaohua Li */ 449815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 450815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 451815c2c54SShaohua Li 4526b534915SHuang Ying cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); 453815c2c54SShaohua Li 454815c2c54SShaohua Li schedule_work(&si->discard_work); 455815c2c54SShaohua Li } 456815c2c54SShaohua Li 45738d8b4e6SHuang Ying static void __free_cluster(struct swap_info_struct *si, unsigned long idx) 45838d8b4e6SHuang Ying { 45938d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 46038d8b4e6SHuang Ying 46138d8b4e6SHuang Ying cluster_set_flag(ci + idx, CLUSTER_FLAG_FREE); 46238d8b4e6SHuang Ying cluster_list_add_tail(&si->free_clusters, ci, idx); 46338d8b4e6SHuang Ying } 46438d8b4e6SHuang Ying 465815c2c54SShaohua Li /* 466815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 467815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 468815c2c54SShaohua Li */ 469815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 470815c2c54SShaohua Li { 471235b6217SHuang, Ying struct swap_cluster_info *info, *ci; 472815c2c54SShaohua Li unsigned int idx; 473815c2c54SShaohua Li 474815c2c54SShaohua Li info = si->cluster_info; 475815c2c54SShaohua Li 4766b534915SHuang Ying while (!cluster_list_empty(&si->discard_clusters)) { 4776b534915SHuang Ying idx = cluster_list_del_first(&si->discard_clusters, info); 478815c2c54SShaohua Li spin_unlock(&si->lock); 479815c2c54SShaohua Li 480815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 481815c2c54SShaohua Li SWAPFILE_CLUSTER); 482815c2c54SShaohua Li 483815c2c54SShaohua Li spin_lock(&si->lock); 484235b6217SHuang, Ying ci = lock_cluster(si, idx * SWAPFILE_CLUSTER); 48538d8b4e6SHuang Ying __free_cluster(si, idx); 486815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 487815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 488235b6217SHuang, Ying unlock_cluster(ci); 489815c2c54SShaohua Li } 490815c2c54SShaohua Li } 491815c2c54SShaohua Li 492815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 493815c2c54SShaohua Li { 494815c2c54SShaohua Li struct swap_info_struct *si; 495815c2c54SShaohua Li 496815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 497815c2c54SShaohua Li 498815c2c54SShaohua Li spin_lock(&si->lock); 499815c2c54SShaohua Li swap_do_scheduled_discard(si); 500815c2c54SShaohua Li spin_unlock(&si->lock); 501815c2c54SShaohua Li } 502815c2c54SShaohua Li 50338d8b4e6SHuang Ying static void alloc_cluster(struct swap_info_struct *si, unsigned long idx) 50438d8b4e6SHuang Ying { 50538d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info; 50638d8b4e6SHuang Ying 50738d8b4e6SHuang Ying VM_BUG_ON(cluster_list_first(&si->free_clusters) != idx); 50838d8b4e6SHuang Ying cluster_list_del_first(&si->free_clusters, ci); 50938d8b4e6SHuang Ying cluster_set_count_flag(ci + idx, 0, 0); 51038d8b4e6SHuang Ying } 51138d8b4e6SHuang Ying 51238d8b4e6SHuang Ying static void free_cluster(struct swap_info_struct *si, unsigned long idx) 51338d8b4e6SHuang Ying { 51438d8b4e6SHuang Ying struct swap_cluster_info *ci = si->cluster_info + idx; 51538d8b4e6SHuang Ying 51638d8b4e6SHuang Ying VM_BUG_ON(cluster_count(ci) != 0); 51738d8b4e6SHuang Ying /* 51838d8b4e6SHuang Ying * If the swap is discardable, prepare discard the cluster 51938d8b4e6SHuang Ying * instead of free it immediately. The cluster will be freed 52038d8b4e6SHuang Ying * after discard. 52138d8b4e6SHuang Ying */ 52238d8b4e6SHuang Ying if ((si->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 52338d8b4e6SHuang Ying (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 52438d8b4e6SHuang Ying swap_cluster_schedule_discard(si, idx); 52538d8b4e6SHuang Ying return; 52638d8b4e6SHuang Ying } 52738d8b4e6SHuang Ying 52838d8b4e6SHuang Ying __free_cluster(si, idx); 52938d8b4e6SHuang Ying } 53038d8b4e6SHuang Ying 5312a8f9449SShaohua Li /* 5322a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 5332a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 5342a8f9449SShaohua Li */ 5352a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 5362a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 5372a8f9449SShaohua Li { 5382a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 5392a8f9449SShaohua Li 5402a8f9449SShaohua Li if (!cluster_info) 5412a8f9449SShaohua Li return; 54238d8b4e6SHuang Ying if (cluster_is_free(&cluster_info[idx])) 54338d8b4e6SHuang Ying alloc_cluster(p, idx); 5442a8f9449SShaohua Li 5452a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 5462a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 5472a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 5482a8f9449SShaohua Li } 5492a8f9449SShaohua Li 5502a8f9449SShaohua Li /* 5512a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 5522a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 5532a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 5542a8f9449SShaohua Li */ 5552a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 5562a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 5572a8f9449SShaohua Li { 5582a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 5592a8f9449SShaohua Li 5602a8f9449SShaohua Li if (!cluster_info) 5612a8f9449SShaohua Li return; 5622a8f9449SShaohua Li 5632a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 5642a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 5652a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 5662a8f9449SShaohua Li 56738d8b4e6SHuang Ying if (cluster_count(&cluster_info[idx]) == 0) 56838d8b4e6SHuang Ying free_cluster(p, idx); 5692a8f9449SShaohua Li } 5702a8f9449SShaohua Li 5712a8f9449SShaohua Li /* 5722a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 5732a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 5742a8f9449SShaohua Li */ 575ebc2a1a6SShaohua Li static bool 576ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 5772a8f9449SShaohua Li unsigned long offset) 5782a8f9449SShaohua Li { 579ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 580ebc2a1a6SShaohua Li bool conflict; 581ebc2a1a6SShaohua Li 5822a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 5836b534915SHuang Ying conflict = !cluster_list_empty(&si->free_clusters) && 5846b534915SHuang Ying offset != cluster_list_first(&si->free_clusters) && 5852a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 586ebc2a1a6SShaohua Li 587ebc2a1a6SShaohua Li if (!conflict) 588ebc2a1a6SShaohua Li return false; 589ebc2a1a6SShaohua Li 590ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 591ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 592ebc2a1a6SShaohua Li return true; 593ebc2a1a6SShaohua Li } 594ebc2a1a6SShaohua Li 595ebc2a1a6SShaohua Li /* 596ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 597ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 598ebc2a1a6SShaohua Li */ 59936005baeSTim Chen static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 600ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 601ebc2a1a6SShaohua Li { 602ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 603235b6217SHuang, Ying struct swap_cluster_info *ci; 604235b6217SHuang, Ying unsigned long tmp, max; 605ebc2a1a6SShaohua Li 606ebc2a1a6SShaohua Li new_cluster: 607ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 608ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 6096b534915SHuang Ying if (!cluster_list_empty(&si->free_clusters)) { 6106b534915SHuang Ying cluster->index = si->free_clusters.head; 611ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 612ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 6136b534915SHuang Ying } else if (!cluster_list_empty(&si->discard_clusters)) { 614ebc2a1a6SShaohua Li /* 615ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 616ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 617ebc2a1a6SShaohua Li */ 618ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 619ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 620ebc2a1a6SShaohua Li goto new_cluster; 621ebc2a1a6SShaohua Li } else 62236005baeSTim Chen return false; 623ebc2a1a6SShaohua Li } 624ebc2a1a6SShaohua Li 625ebc2a1a6SShaohua Li /* 626ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 627ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 628ebc2a1a6SShaohua Li */ 629ebc2a1a6SShaohua Li tmp = cluster->next; 630235b6217SHuang, Ying max = min_t(unsigned long, si->max, 631235b6217SHuang, Ying (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); 632*7b9e2de1SWei Yang if (tmp < max) { 633235b6217SHuang, Ying ci = lock_cluster(si, tmp); 634235b6217SHuang, Ying while (tmp < max) { 6350fd0e19eSWei Yang if (!si->swap_map[tmp]) 636ebc2a1a6SShaohua Li break; 637ebc2a1a6SShaohua Li tmp++; 638ebc2a1a6SShaohua Li } 639235b6217SHuang, Ying unlock_cluster(ci); 640*7b9e2de1SWei Yang } 6410fd0e19eSWei Yang if (tmp >= max) { 642ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 643ebc2a1a6SShaohua Li goto new_cluster; 644ebc2a1a6SShaohua Li } 645ebc2a1a6SShaohua Li cluster->next = tmp + 1; 646ebc2a1a6SShaohua Li *offset = tmp; 647ebc2a1a6SShaohua Li *scan_base = tmp; 648fdff1debSWei Yang return true; 6492a8f9449SShaohua Li } 6502a8f9449SShaohua Li 651a2468cc9SAaron Lu static void __del_from_avail_list(struct swap_info_struct *p) 652a2468cc9SAaron Lu { 653a2468cc9SAaron Lu int nid; 654a2468cc9SAaron Lu 655a2468cc9SAaron Lu for_each_node(nid) 656a2468cc9SAaron Lu plist_del(&p->avail_lists[nid], &swap_avail_heads[nid]); 657a2468cc9SAaron Lu } 658a2468cc9SAaron Lu 659a2468cc9SAaron Lu static void del_from_avail_list(struct swap_info_struct *p) 660a2468cc9SAaron Lu { 661a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 662a2468cc9SAaron Lu __del_from_avail_list(p); 663a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 664a2468cc9SAaron Lu } 665a2468cc9SAaron Lu 66638d8b4e6SHuang Ying static void swap_range_alloc(struct swap_info_struct *si, unsigned long offset, 66738d8b4e6SHuang Ying unsigned int nr_entries) 66838d8b4e6SHuang Ying { 66938d8b4e6SHuang Ying unsigned int end = offset + nr_entries - 1; 67038d8b4e6SHuang Ying 67138d8b4e6SHuang Ying if (offset == si->lowest_bit) 67238d8b4e6SHuang Ying si->lowest_bit += nr_entries; 67338d8b4e6SHuang Ying if (end == si->highest_bit) 67438d8b4e6SHuang Ying si->highest_bit -= nr_entries; 67538d8b4e6SHuang Ying si->inuse_pages += nr_entries; 67638d8b4e6SHuang Ying if (si->inuse_pages == si->pages) { 67738d8b4e6SHuang Ying si->lowest_bit = si->max; 67838d8b4e6SHuang Ying si->highest_bit = 0; 679a2468cc9SAaron Lu del_from_avail_list(si); 68038d8b4e6SHuang Ying } 68138d8b4e6SHuang Ying } 68238d8b4e6SHuang Ying 683a2468cc9SAaron Lu static void add_to_avail_list(struct swap_info_struct *p) 684a2468cc9SAaron Lu { 685a2468cc9SAaron Lu int nid; 686a2468cc9SAaron Lu 687a2468cc9SAaron Lu spin_lock(&swap_avail_lock); 688a2468cc9SAaron Lu for_each_node(nid) { 689a2468cc9SAaron Lu WARN_ON(!plist_node_empty(&p->avail_lists[nid])); 690a2468cc9SAaron Lu plist_add(&p->avail_lists[nid], &swap_avail_heads[nid]); 691a2468cc9SAaron Lu } 692a2468cc9SAaron Lu spin_unlock(&swap_avail_lock); 693a2468cc9SAaron Lu } 694a2468cc9SAaron Lu 69538d8b4e6SHuang Ying static void swap_range_free(struct swap_info_struct *si, unsigned long offset, 69638d8b4e6SHuang Ying unsigned int nr_entries) 69738d8b4e6SHuang Ying { 69838d8b4e6SHuang Ying unsigned long end = offset + nr_entries - 1; 69938d8b4e6SHuang Ying void (*swap_slot_free_notify)(struct block_device *, unsigned long); 70038d8b4e6SHuang Ying 70138d8b4e6SHuang Ying if (offset < si->lowest_bit) 70238d8b4e6SHuang Ying si->lowest_bit = offset; 70338d8b4e6SHuang Ying if (end > si->highest_bit) { 70438d8b4e6SHuang Ying bool was_full = !si->highest_bit; 70538d8b4e6SHuang Ying 70638d8b4e6SHuang Ying si->highest_bit = end; 707a2468cc9SAaron Lu if (was_full && (si->flags & SWP_WRITEOK)) 708a2468cc9SAaron Lu add_to_avail_list(si); 70938d8b4e6SHuang Ying } 71038d8b4e6SHuang Ying atomic_long_add(nr_entries, &nr_swap_pages); 71138d8b4e6SHuang Ying si->inuse_pages -= nr_entries; 71238d8b4e6SHuang Ying if (si->flags & SWP_BLKDEV) 71338d8b4e6SHuang Ying swap_slot_free_notify = 71438d8b4e6SHuang Ying si->bdev->bd_disk->fops->swap_slot_free_notify; 71538d8b4e6SHuang Ying else 71638d8b4e6SHuang Ying swap_slot_free_notify = NULL; 71738d8b4e6SHuang Ying while (offset <= end) { 71838d8b4e6SHuang Ying frontswap_invalidate_page(si->type, offset); 71938d8b4e6SHuang Ying if (swap_slot_free_notify) 72038d8b4e6SHuang Ying swap_slot_free_notify(si->bdev, offset); 72138d8b4e6SHuang Ying offset++; 72238d8b4e6SHuang Ying } 72338d8b4e6SHuang Ying } 72438d8b4e6SHuang Ying 72536005baeSTim Chen static int scan_swap_map_slots(struct swap_info_struct *si, 72636005baeSTim Chen unsigned char usage, int nr, 72736005baeSTim Chen swp_entry_t slots[]) 7281da177e4SLinus Torvalds { 729235b6217SHuang, Ying struct swap_cluster_info *ci; 730ebebbbe9SHugh Dickins unsigned long offset; 731c60aa176SHugh Dickins unsigned long scan_base; 7327992fde7SHugh Dickins unsigned long last_in_cluster = 0; 733048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 73436005baeSTim Chen int n_ret = 0; 73536005baeSTim Chen 7367dfad418SHugh Dickins /* 7377dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 7387dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 7397dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 7407dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 7417dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 7427dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 7437dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 744c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 7451da177e4SLinus Torvalds */ 7467dfad418SHugh Dickins 74752b7efdbSHugh Dickins si->flags += SWP_SCANNING; 748c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 749ebebbbe9SHugh Dickins 750ebc2a1a6SShaohua Li /* SSD algorithm */ 751ebc2a1a6SShaohua Li if (si->cluster_info) { 752bd2d18daSWei Yang if (!scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 75336005baeSTim Chen goto scan; 754f4eaf51aSWei Yang } else if (unlikely(!si->cluster_nr--)) { 755ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 756ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 7572a8f9449SShaohua Li goto checks; 7582a8f9449SShaohua Li } 7592a8f9449SShaohua Li 760ec8acf20SShaohua Li spin_unlock(&si->lock); 7617dfad418SHugh Dickins 762c60aa176SHugh Dickins /* 763c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 764c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 76550088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 76650088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 767c60aa176SHugh Dickins */ 768c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 7697dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 7707dfad418SHugh Dickins 7717dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 7727dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 7731da177e4SLinus Torvalds if (si->swap_map[offset]) 7747dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 7757dfad418SHugh Dickins else if (offset == last_in_cluster) { 776ec8acf20SShaohua Li spin_lock(&si->lock); 777ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 778ebebbbe9SHugh Dickins si->cluster_next = offset; 779ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 780ebebbbe9SHugh Dickins goto checks; 7817dfad418SHugh Dickins } 782048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 783048c27fdSHugh Dickins cond_resched(); 784048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 785048c27fdSHugh Dickins } 7867dfad418SHugh Dickins } 787ebebbbe9SHugh Dickins 788c60aa176SHugh Dickins offset = scan_base; 789ec8acf20SShaohua Li spin_lock(&si->lock); 790ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 7917dfad418SHugh Dickins } 7927dfad418SHugh Dickins 793ebebbbe9SHugh Dickins checks: 794ebc2a1a6SShaohua Li if (si->cluster_info) { 79536005baeSTim Chen while (scan_swap_map_ssd_cluster_conflict(si, offset)) { 79636005baeSTim Chen /* take a break if we already got some slots */ 79736005baeSTim Chen if (n_ret) 79836005baeSTim Chen goto done; 79936005baeSTim Chen if (!scan_swap_map_try_ssd_cluster(si, &offset, 80036005baeSTim Chen &scan_base)) 80136005baeSTim Chen goto scan; 80236005baeSTim Chen } 803ebc2a1a6SShaohua Li } 804ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 80552b7efdbSHugh Dickins goto no_page; 8067dfad418SHugh Dickins if (!si->highest_bit) 8077dfad418SHugh Dickins goto no_page; 808ebebbbe9SHugh Dickins if (offset > si->highest_bit) 809c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 810c9e44410SKAMEZAWA Hiroyuki 811235b6217SHuang, Ying ci = lock_cluster(si, offset); 812b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 813b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 814c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 815235b6217SHuang, Ying unlock_cluster(ci); 816ec8acf20SShaohua Li spin_unlock(&si->lock); 817bcd49e86SHuang Ying swap_was_freed = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); 818ec8acf20SShaohua Li spin_lock(&si->lock); 819c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 820c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 821c9e44410SKAMEZAWA Hiroyuki goto checks; 822c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 823c9e44410SKAMEZAWA Hiroyuki } 824c9e44410SKAMEZAWA Hiroyuki 825235b6217SHuang, Ying if (si->swap_map[offset]) { 826235b6217SHuang, Ying unlock_cluster(ci); 82736005baeSTim Chen if (!n_ret) 828ebebbbe9SHugh Dickins goto scan; 82936005baeSTim Chen else 83036005baeSTim Chen goto done; 831235b6217SHuang, Ying } 8322872bb2dSHuang Ying si->swap_map[offset] = usage; 8332872bb2dSHuang Ying inc_cluster_info_page(si, si->cluster_info, offset); 8342872bb2dSHuang Ying unlock_cluster(ci); 835ebebbbe9SHugh Dickins 83638d8b4e6SHuang Ying swap_range_alloc(si, offset, 1); 8371da177e4SLinus Torvalds si->cluster_next = offset + 1; 83836005baeSTim Chen slots[n_ret++] = swp_entry(si->type, offset); 8397992fde7SHugh Dickins 84036005baeSTim Chen /* got enough slots or reach max slots? */ 84136005baeSTim Chen if ((n_ret == nr) || (offset >= si->highest_bit)) 84236005baeSTim Chen goto done; 84336005baeSTim Chen 84436005baeSTim Chen /* search for next available slot */ 84536005baeSTim Chen 84636005baeSTim Chen /* time to take a break? */ 84736005baeSTim Chen if (unlikely(--latency_ration < 0)) { 84836005baeSTim Chen if (n_ret) 84936005baeSTim Chen goto done; 85036005baeSTim Chen spin_unlock(&si->lock); 85136005baeSTim Chen cond_resched(); 85236005baeSTim Chen spin_lock(&si->lock); 85336005baeSTim Chen latency_ration = LATENCY_LIMIT; 85436005baeSTim Chen } 85536005baeSTim Chen 85636005baeSTim Chen /* try to get more slots in cluster */ 85736005baeSTim Chen if (si->cluster_info) { 85836005baeSTim Chen if (scan_swap_map_try_ssd_cluster(si, &offset, &scan_base)) 85936005baeSTim Chen goto checks; 860f4eaf51aSWei Yang } else if (si->cluster_nr && !si->swap_map[++offset]) { 86136005baeSTim Chen /* non-ssd case, still more slots in cluster? */ 86236005baeSTim Chen --si->cluster_nr; 86336005baeSTim Chen goto checks; 86436005baeSTim Chen } 86536005baeSTim Chen 86636005baeSTim Chen done: 86736005baeSTim Chen si->flags -= SWP_SCANNING; 86836005baeSTim Chen return n_ret; 8697dfad418SHugh Dickins 870ebebbbe9SHugh Dickins scan: 871ec8acf20SShaohua Li spin_unlock(&si->lock); 8727dfad418SHugh Dickins while (++offset <= si->highest_bit) { 87352b7efdbSHugh Dickins if (!si->swap_map[offset]) { 874ec8acf20SShaohua Li spin_lock(&si->lock); 87552b7efdbSHugh Dickins goto checks; 8767dfad418SHugh Dickins } 877c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 878ec8acf20SShaohua Li spin_lock(&si->lock); 879c9e44410SKAMEZAWA Hiroyuki goto checks; 880c9e44410SKAMEZAWA Hiroyuki } 881048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 882048c27fdSHugh Dickins cond_resched(); 883048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 884048c27fdSHugh Dickins } 88552b7efdbSHugh Dickins } 886c60aa176SHugh Dickins offset = si->lowest_bit; 887a5998061SJamie Liu while (offset < scan_base) { 888c60aa176SHugh Dickins if (!si->swap_map[offset]) { 889ec8acf20SShaohua Li spin_lock(&si->lock); 890ebebbbe9SHugh Dickins goto checks; 891c60aa176SHugh Dickins } 892c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 893ec8acf20SShaohua Li spin_lock(&si->lock); 894c9e44410SKAMEZAWA Hiroyuki goto checks; 895c9e44410SKAMEZAWA Hiroyuki } 896c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 897c60aa176SHugh Dickins cond_resched(); 898c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 899c60aa176SHugh Dickins } 900a5998061SJamie Liu offset++; 901c60aa176SHugh Dickins } 902ec8acf20SShaohua Li spin_lock(&si->lock); 9037dfad418SHugh Dickins 9047dfad418SHugh Dickins no_page: 90552b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 90636005baeSTim Chen return n_ret; 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 90938d8b4e6SHuang Ying static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot) 91038d8b4e6SHuang Ying { 91138d8b4e6SHuang Ying unsigned long idx; 91238d8b4e6SHuang Ying struct swap_cluster_info *ci; 91338d8b4e6SHuang Ying unsigned long offset, i; 91438d8b4e6SHuang Ying unsigned char *map; 91538d8b4e6SHuang Ying 916fe5266d5SHuang Ying /* 917fe5266d5SHuang Ying * Should not even be attempting cluster allocations when huge 918fe5266d5SHuang Ying * page swap is disabled. Warn and fail the allocation. 919fe5266d5SHuang Ying */ 920fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP)) { 921fe5266d5SHuang Ying VM_WARN_ON_ONCE(1); 922fe5266d5SHuang Ying return 0; 923fe5266d5SHuang Ying } 924fe5266d5SHuang Ying 92538d8b4e6SHuang Ying if (cluster_list_empty(&si->free_clusters)) 92638d8b4e6SHuang Ying return 0; 92738d8b4e6SHuang Ying 92838d8b4e6SHuang Ying idx = cluster_list_first(&si->free_clusters); 92938d8b4e6SHuang Ying offset = idx * SWAPFILE_CLUSTER; 93038d8b4e6SHuang Ying ci = lock_cluster(si, offset); 93138d8b4e6SHuang Ying alloc_cluster(si, idx); 932e0709829SHuang Ying cluster_set_count_flag(ci, SWAPFILE_CLUSTER, CLUSTER_FLAG_HUGE); 93338d8b4e6SHuang Ying 93438d8b4e6SHuang Ying map = si->swap_map + offset; 93538d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) 93638d8b4e6SHuang Ying map[i] = SWAP_HAS_CACHE; 93738d8b4e6SHuang Ying unlock_cluster(ci); 93838d8b4e6SHuang Ying swap_range_alloc(si, offset, SWAPFILE_CLUSTER); 93938d8b4e6SHuang Ying *slot = swp_entry(si->type, offset); 94038d8b4e6SHuang Ying 94138d8b4e6SHuang Ying return 1; 94238d8b4e6SHuang Ying } 94338d8b4e6SHuang Ying 94438d8b4e6SHuang Ying static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) 94538d8b4e6SHuang Ying { 94638d8b4e6SHuang Ying unsigned long offset = idx * SWAPFILE_CLUSTER; 94738d8b4e6SHuang Ying struct swap_cluster_info *ci; 94838d8b4e6SHuang Ying 94938d8b4e6SHuang Ying ci = lock_cluster(si, offset); 950979aafa5SHuang Ying memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER); 95138d8b4e6SHuang Ying cluster_set_count_flag(ci, 0, 0); 95238d8b4e6SHuang Ying free_cluster(si, idx); 95338d8b4e6SHuang Ying unlock_cluster(ci); 95438d8b4e6SHuang Ying swap_range_free(si, offset, SWAPFILE_CLUSTER); 95538d8b4e6SHuang Ying } 95638d8b4e6SHuang Ying 95736005baeSTim Chen static unsigned long scan_swap_map(struct swap_info_struct *si, 95836005baeSTim Chen unsigned char usage) 95936005baeSTim Chen { 96036005baeSTim Chen swp_entry_t entry; 96136005baeSTim Chen int n_ret; 96236005baeSTim Chen 96336005baeSTim Chen n_ret = scan_swap_map_slots(si, usage, 1, &entry); 96436005baeSTim Chen 96536005baeSTim Chen if (n_ret) 96636005baeSTim Chen return swp_offset(entry); 96736005baeSTim Chen else 96836005baeSTim Chen return 0; 96936005baeSTim Chen 97036005baeSTim Chen } 97136005baeSTim Chen 9725d5e8f19SHuang Ying int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) 9731da177e4SLinus Torvalds { 9745d5e8f19SHuang Ying unsigned long size = swap_entry_size(entry_size); 975adfab836SDan Streetman struct swap_info_struct *si, *next; 97636005baeSTim Chen long avail_pgs; 97736005baeSTim Chen int n_ret = 0; 978a2468cc9SAaron Lu int node; 9791da177e4SLinus Torvalds 98038d8b4e6SHuang Ying /* Only single cluster request supported */ 9815d5e8f19SHuang Ying WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER); 98238d8b4e6SHuang Ying 9835d5e8f19SHuang Ying avail_pgs = atomic_long_read(&nr_swap_pages) / size; 98436005baeSTim Chen if (avail_pgs <= 0) 985fb4f88dcSHugh Dickins goto noswap; 98636005baeSTim Chen 98708d3090fSWei Yang n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs); 98836005baeSTim Chen 9895d5e8f19SHuang Ying atomic_long_sub(n_goal * size, &nr_swap_pages); 9901da177e4SLinus Torvalds 99118ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 99218ab4d4cSDan Streetman 99318ab4d4cSDan Streetman start_over: 994a2468cc9SAaron Lu node = numa_node_id(); 995a2468cc9SAaron Lu plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { 99618ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 997a2468cc9SAaron Lu plist_requeue(&si->avail_lists[node], &swap_avail_heads[node]); 99818ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 999ec8acf20SShaohua Li spin_lock(&si->lock); 1000adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 100118ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 1002a2468cc9SAaron Lu if (plist_node_empty(&si->avail_lists[node])) { 1003ec8acf20SShaohua Li spin_unlock(&si->lock); 100418ab4d4cSDan Streetman goto nextsi; 100518ab4d4cSDan Streetman } 100618ab4d4cSDan Streetman WARN(!si->highest_bit, 100718ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 100818ab4d4cSDan Streetman si->type); 100918ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 101018ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 101118ab4d4cSDan Streetman si->type); 1012a2468cc9SAaron Lu __del_from_avail_list(si); 101318ab4d4cSDan Streetman spin_unlock(&si->lock); 101418ab4d4cSDan Streetman goto nextsi; 1015ec8acf20SShaohua Li } 10165d5e8f19SHuang Ying if (size == SWAPFILE_CLUSTER) { 1017bc4ae27dSOmar Sandoval if (!(si->flags & SWP_FS)) 101838d8b4e6SHuang Ying n_ret = swap_alloc_cluster(si, swp_entries); 1019f0eea189SHuang Ying } else 102036005baeSTim Chen n_ret = scan_swap_map_slots(si, SWAP_HAS_CACHE, 102136005baeSTim Chen n_goal, swp_entries); 1022ec8acf20SShaohua Li spin_unlock(&si->lock); 10235d5e8f19SHuang Ying if (n_ret || size == SWAPFILE_CLUSTER) 102436005baeSTim Chen goto check_out; 102518ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 102618ab4d4cSDan Streetman si->type); 102736005baeSTim Chen 102818ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 102918ab4d4cSDan Streetman nextsi: 1030adfab836SDan Streetman /* 1031adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 1032adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 1033adfab836SDan Streetman * callers probably all tried to get a page from the same si 103418ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 103518ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 103618ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 103718ab4d4cSDan Streetman * list may have been modified; so if next is still in the 103836005baeSTim Chen * swap_avail_head list then try it, otherwise start over 103936005baeSTim Chen * if we have not gotten any slots. 1040adfab836SDan Streetman */ 1041a2468cc9SAaron Lu if (plist_node_empty(&next->avail_lists[node])) 104218ab4d4cSDan Streetman goto start_over; 1043fb4f88dcSHugh Dickins } 1044fb4f88dcSHugh Dickins 104518ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 104618ab4d4cSDan Streetman 104736005baeSTim Chen check_out: 104836005baeSTim Chen if (n_ret < n_goal) 10495d5e8f19SHuang Ying atomic_long_add((long)(n_goal - n_ret) * size, 105038d8b4e6SHuang Ying &nr_swap_pages); 1051fb4f88dcSHugh Dickins noswap: 105236005baeSTim Chen return n_ret; 105336005baeSTim Chen } 105436005baeSTim Chen 10552de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 1056910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 1057910321eaSHugh Dickins { 1058c10d38ccSDaniel Jordan struct swap_info_struct *si = swap_type_to_swap_info(type); 1059910321eaSHugh Dickins pgoff_t offset; 1060910321eaSHugh Dickins 1061c10d38ccSDaniel Jordan if (!si) 1062c10d38ccSDaniel Jordan goto fail; 1063c10d38ccSDaniel Jordan 1064ec8acf20SShaohua Li spin_lock(&si->lock); 1065c10d38ccSDaniel Jordan if (si->flags & SWP_WRITEOK) { 1066ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 1067910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 1068910321eaSHugh Dickins offset = scan_swap_map(si, 1); 1069910321eaSHugh Dickins if (offset) { 1070ec8acf20SShaohua Li spin_unlock(&si->lock); 1071910321eaSHugh Dickins return swp_entry(type, offset); 1072910321eaSHugh Dickins } 1073ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 1074910321eaSHugh Dickins } 1075ec8acf20SShaohua Li spin_unlock(&si->lock); 1076c10d38ccSDaniel Jordan fail: 1077910321eaSHugh Dickins return (swp_entry_t) {0}; 1078910321eaSHugh Dickins } 1079910321eaSHugh Dickins 1080e8c26ab6STim Chen static struct swap_info_struct *__swap_info_get(swp_entry_t entry) 10811da177e4SLinus Torvalds { 10821da177e4SLinus Torvalds struct swap_info_struct *p; 1083eb085574SHuang Ying unsigned long offset; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds if (!entry.val) 10861da177e4SLinus Torvalds goto out; 1087eb085574SHuang Ying p = swp_swap_info(entry); 1088c10d38ccSDaniel Jordan if (!p) 10891da177e4SLinus Torvalds goto bad_nofile; 10901da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 10911da177e4SLinus Torvalds goto bad_device; 10921da177e4SLinus Torvalds offset = swp_offset(entry); 10931da177e4SLinus Torvalds if (offset >= p->max) 10941da177e4SLinus Torvalds goto bad_offset; 10951da177e4SLinus Torvalds return p; 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds bad_offset: 10986a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_offset, entry.val); 10991da177e4SLinus Torvalds goto out; 11001da177e4SLinus Torvalds bad_device: 11016a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Unused_file, entry.val); 11021da177e4SLinus Torvalds goto out; 11031da177e4SLinus Torvalds bad_nofile: 11046a991fc7SHuang, Ying pr_err("swap_info_get: %s%08lx\n", Bad_file, entry.val); 11051da177e4SLinus Torvalds out: 11061da177e4SLinus Torvalds return NULL; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 1109e8c26ab6STim Chen static struct swap_info_struct *_swap_info_get(swp_entry_t entry) 1110e8c26ab6STim Chen { 1111e8c26ab6STim Chen struct swap_info_struct *p; 1112e8c26ab6STim Chen 1113e8c26ab6STim Chen p = __swap_info_get(entry); 1114e8c26ab6STim Chen if (!p) 1115e8c26ab6STim Chen goto out; 1116e8c26ab6STim Chen if (!p->swap_map[swp_offset(entry)]) 1117e8c26ab6STim Chen goto bad_free; 1118e8c26ab6STim Chen return p; 1119e8c26ab6STim Chen 1120e8c26ab6STim Chen bad_free: 1121e8c26ab6STim Chen pr_err("swap_info_get: %s%08lx\n", Unused_offset, entry.val); 1122e8c26ab6STim Chen goto out; 1123e8c26ab6STim Chen out: 1124e8c26ab6STim Chen return NULL; 1125e8c26ab6STim Chen } 1126e8c26ab6STim Chen 1127235b6217SHuang, Ying static struct swap_info_struct *swap_info_get(swp_entry_t entry) 11281da177e4SLinus Torvalds { 1129235b6217SHuang, Ying struct swap_info_struct *p; 1130235b6217SHuang, Ying 1131235b6217SHuang, Ying p = _swap_info_get(entry); 1132235b6217SHuang, Ying if (p) 1133235b6217SHuang, Ying spin_lock(&p->lock); 1134235b6217SHuang, Ying return p; 1135235b6217SHuang, Ying } 1136235b6217SHuang, Ying 11377c00bafeSTim Chen static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry, 11387c00bafeSTim Chen struct swap_info_struct *q) 11397c00bafeSTim Chen { 11407c00bafeSTim Chen struct swap_info_struct *p; 11417c00bafeSTim Chen 11427c00bafeSTim Chen p = _swap_info_get(entry); 11437c00bafeSTim Chen 11447c00bafeSTim Chen if (p != q) { 11457c00bafeSTim Chen if (q != NULL) 11467c00bafeSTim Chen spin_unlock(&q->lock); 11477c00bafeSTim Chen if (p != NULL) 11487c00bafeSTim Chen spin_lock(&p->lock); 11497c00bafeSTim Chen } 11507c00bafeSTim Chen return p; 11517c00bafeSTim Chen } 11527c00bafeSTim Chen 1153b32d5f32SHuang Ying static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, 1154b32d5f32SHuang Ying unsigned long offset, 1155b32d5f32SHuang Ying unsigned char usage) 1156235b6217SHuang, Ying { 11578d69aaeeSHugh Dickins unsigned char count; 11588d69aaeeSHugh Dickins unsigned char has_cache; 1159235b6217SHuang, Ying 1160355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 1161235b6217SHuang, Ying 1162253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 1163253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 1164253d553bSHugh Dickins 1165253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 1166253d553bSHugh Dickins VM_BUG_ON(!has_cache); 1167253d553bSHugh Dickins has_cache = 0; 1168aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 1169aaa46865SHugh Dickins /* 1170aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 1171aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 1172aaa46865SHugh Dickins */ 1173aaa46865SHugh Dickins count = 0; 1174570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 1175570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 1176570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 1177570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 1178570a335bSHugh Dickins else 1179570a335bSHugh Dickins count = SWAP_MAP_MAX; 1180570a335bSHugh Dickins } else 1181253d553bSHugh Dickins count--; 1182570a335bSHugh Dickins } 1183253d553bSHugh Dickins 1184253d553bSHugh Dickins usage = count | has_cache; 11857c00bafeSTim Chen p->swap_map[offset] = usage ? : SWAP_HAS_CACHE; 1186253d553bSHugh Dickins 1187b32d5f32SHuang Ying return usage; 1188b32d5f32SHuang Ying } 1189b32d5f32SHuang Ying 1190eb085574SHuang Ying /* 1191eb085574SHuang Ying * Check whether swap entry is valid in the swap device. If so, 1192eb085574SHuang Ying * return pointer to swap_info_struct, and keep the swap entry valid 1193eb085574SHuang Ying * via preventing the swap device from being swapoff, until 1194eb085574SHuang Ying * put_swap_device() is called. Otherwise return NULL. 1195eb085574SHuang Ying * 1196eb085574SHuang Ying * The entirety of the RCU read critical section must come before the 1197eb085574SHuang Ying * return from or after the call to synchronize_rcu() in 1198eb085574SHuang Ying * enable_swap_info() or swapoff(). So if "si->flags & SWP_VALID" is 1199eb085574SHuang Ying * true, the si->map, si->cluster_info, etc. must be valid in the 1200eb085574SHuang Ying * critical section. 1201eb085574SHuang Ying * 1202eb085574SHuang Ying * Notice that swapoff or swapoff+swapon can still happen before the 1203eb085574SHuang Ying * rcu_read_lock() in get_swap_device() or after the rcu_read_unlock() 1204eb085574SHuang Ying * in put_swap_device() if there isn't any other way to prevent 1205eb085574SHuang Ying * swapoff, such as page lock, page table lock, etc. The caller must 1206eb085574SHuang Ying * be prepared for that. For example, the following situation is 1207eb085574SHuang Ying * possible. 1208eb085574SHuang Ying * 1209eb085574SHuang Ying * CPU1 CPU2 1210eb085574SHuang Ying * do_swap_page() 1211eb085574SHuang Ying * ... swapoff+swapon 1212eb085574SHuang Ying * __read_swap_cache_async() 1213eb085574SHuang Ying * swapcache_prepare() 1214eb085574SHuang Ying * __swap_duplicate() 1215eb085574SHuang Ying * // check swap_map 1216eb085574SHuang Ying * // verify PTE not changed 1217eb085574SHuang Ying * 1218eb085574SHuang Ying * In __swap_duplicate(), the swap_map need to be checked before 1219eb085574SHuang Ying * changing partly because the specified swap entry may be for another 1220eb085574SHuang Ying * swap device which has been swapoff. And in do_swap_page(), after 1221eb085574SHuang Ying * the page is read from the swap device, the PTE is verified not 1222eb085574SHuang Ying * changed with the page table locked to check whether the swap device 1223eb085574SHuang Ying * has been swapoff or swapoff+swapon. 1224eb085574SHuang Ying */ 1225eb085574SHuang Ying struct swap_info_struct *get_swap_device(swp_entry_t entry) 1226eb085574SHuang Ying { 1227eb085574SHuang Ying struct swap_info_struct *si; 1228eb085574SHuang Ying unsigned long offset; 1229eb085574SHuang Ying 1230eb085574SHuang Ying if (!entry.val) 1231eb085574SHuang Ying goto out; 1232eb085574SHuang Ying si = swp_swap_info(entry); 1233eb085574SHuang Ying if (!si) 1234eb085574SHuang Ying goto bad_nofile; 1235eb085574SHuang Ying 1236eb085574SHuang Ying rcu_read_lock(); 1237eb085574SHuang Ying if (!(si->flags & SWP_VALID)) 1238eb085574SHuang Ying goto unlock_out; 1239eb085574SHuang Ying offset = swp_offset(entry); 1240eb085574SHuang Ying if (offset >= si->max) 1241eb085574SHuang Ying goto unlock_out; 1242eb085574SHuang Ying 1243eb085574SHuang Ying return si; 1244eb085574SHuang Ying bad_nofile: 1245eb085574SHuang Ying pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val); 1246eb085574SHuang Ying out: 1247eb085574SHuang Ying return NULL; 1248eb085574SHuang Ying unlock_out: 1249eb085574SHuang Ying rcu_read_unlock(); 1250eb085574SHuang Ying return NULL; 1251eb085574SHuang Ying } 1252eb085574SHuang Ying 1253b32d5f32SHuang Ying static unsigned char __swap_entry_free(struct swap_info_struct *p, 1254b32d5f32SHuang Ying swp_entry_t entry, unsigned char usage) 1255b32d5f32SHuang Ying { 1256b32d5f32SHuang Ying struct swap_cluster_info *ci; 1257b32d5f32SHuang Ying unsigned long offset = swp_offset(entry); 1258b32d5f32SHuang Ying 1259b32d5f32SHuang Ying ci = lock_cluster_or_swap_info(p, offset); 1260b32d5f32SHuang Ying usage = __swap_entry_free_locked(p, offset, usage); 12617c00bafeSTim Chen unlock_cluster_or_swap_info(p, ci); 126210e364daSHuang Ying if (!usage) 126310e364daSHuang Ying free_swap_slot(entry); 1264235b6217SHuang, Ying 12657c00bafeSTim Chen return usage; 12667c00bafeSTim Chen } 12677c00bafeSTim Chen 12687c00bafeSTim Chen static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) 12697c00bafeSTim Chen { 12707c00bafeSTim Chen struct swap_cluster_info *ci; 12717c00bafeSTim Chen unsigned long offset = swp_offset(entry); 12727c00bafeSTim Chen unsigned char count; 12737c00bafeSTim Chen 1274235b6217SHuang, Ying ci = lock_cluster(p, offset); 12757c00bafeSTim Chen count = p->swap_map[offset]; 12767c00bafeSTim Chen VM_BUG_ON(count != SWAP_HAS_CACHE); 12777c00bafeSTim Chen p->swap_map[offset] = 0; 12782a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 1279235b6217SHuang, Ying unlock_cluster(ci); 12807c00bafeSTim Chen 128138d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, 1); 128238d8b4e6SHuang Ying swap_range_free(p, offset, 1); 12831da177e4SLinus Torvalds } 1284253d553bSHugh Dickins 12851da177e4SLinus Torvalds /* 12861da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 12871da177e4SLinus Torvalds * is still around or has not been recycled. 12881da177e4SLinus Torvalds */ 12891da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 12901da177e4SLinus Torvalds { 12911da177e4SLinus Torvalds struct swap_info_struct *p; 12921da177e4SLinus Torvalds 1293235b6217SHuang, Ying p = _swap_info_get(entry); 129410e364daSHuang Ying if (p) 129510e364daSHuang Ying __swap_entry_free(p, entry, 1); 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds /* 1299cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 1300cb4b86baSKAMEZAWA Hiroyuki */ 1301a448f2d0SHuang Ying void put_swap_page(struct page *page, swp_entry_t entry) 130238d8b4e6SHuang Ying { 130338d8b4e6SHuang Ying unsigned long offset = swp_offset(entry); 130438d8b4e6SHuang Ying unsigned long idx = offset / SWAPFILE_CLUSTER; 130538d8b4e6SHuang Ying struct swap_cluster_info *ci; 130638d8b4e6SHuang Ying struct swap_info_struct *si; 130738d8b4e6SHuang Ying unsigned char *map; 1308a3aea839SHuang Ying unsigned int i, free_entries = 0; 1309a3aea839SHuang Ying unsigned char val; 1310a448f2d0SHuang Ying int size = swap_entry_size(hpage_nr_pages(page)); 1311fe5266d5SHuang Ying 1312a3aea839SHuang Ying si = _swap_info_get(entry); 131338d8b4e6SHuang Ying if (!si) 131438d8b4e6SHuang Ying return; 131538d8b4e6SHuang Ying 1316c2343d27SHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1317a448f2d0SHuang Ying if (size == SWAPFILE_CLUSTER) { 1318e0709829SHuang Ying VM_BUG_ON(!cluster_is_huge(ci)); 131938d8b4e6SHuang Ying map = si->swap_map + offset; 132038d8b4e6SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1321a3aea839SHuang Ying val = map[i]; 1322a3aea839SHuang Ying VM_BUG_ON(!(val & SWAP_HAS_CACHE)); 1323a3aea839SHuang Ying if (val == SWAP_HAS_CACHE) 1324a3aea839SHuang Ying free_entries++; 132538d8b4e6SHuang Ying } 1326e0709829SHuang Ying cluster_clear_huge(ci); 1327a3aea839SHuang Ying if (free_entries == SWAPFILE_CLUSTER) { 1328c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 1329a3aea839SHuang Ying spin_lock(&si->lock); 133038d8b4e6SHuang Ying mem_cgroup_uncharge_swap(entry, SWAPFILE_CLUSTER); 133138d8b4e6SHuang Ying swap_free_cluster(si, idx); 133238d8b4e6SHuang Ying spin_unlock(&si->lock); 1333a448f2d0SHuang Ying return; 1334a448f2d0SHuang Ying } 1335a448f2d0SHuang Ying } 1336a448f2d0SHuang Ying for (i = 0; i < size; i++, entry.val++) { 1337c2343d27SHuang Ying if (!__swap_entry_free_locked(si, offset + i, SWAP_HAS_CACHE)) { 1338c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 1339a3aea839SHuang Ying free_swap_slot(entry); 1340c2343d27SHuang Ying if (i == size - 1) 1341c2343d27SHuang Ying return; 1342c2343d27SHuang Ying lock_cluster_or_swap_info(si, offset); 1343a3aea839SHuang Ying } 1344a3aea839SHuang Ying } 1345c2343d27SHuang Ying unlock_cluster_or_swap_info(si, ci); 134638d8b4e6SHuang Ying } 134759807685SHuang Ying 1348fe5266d5SHuang Ying #ifdef CONFIG_THP_SWAP 134959807685SHuang Ying int split_swap_cluster(swp_entry_t entry) 135059807685SHuang Ying { 135159807685SHuang Ying struct swap_info_struct *si; 135259807685SHuang Ying struct swap_cluster_info *ci; 135359807685SHuang Ying unsigned long offset = swp_offset(entry); 135459807685SHuang Ying 135559807685SHuang Ying si = _swap_info_get(entry); 135659807685SHuang Ying if (!si) 135759807685SHuang Ying return -EBUSY; 135859807685SHuang Ying ci = lock_cluster(si, offset); 135959807685SHuang Ying cluster_clear_huge(ci); 136059807685SHuang Ying unlock_cluster(ci); 136159807685SHuang Ying return 0; 136259807685SHuang Ying } 1363fe5266d5SHuang Ying #endif 136438d8b4e6SHuang Ying 1365155b5f88SHuang Ying static int swp_entry_cmp(const void *ent1, const void *ent2) 1366155b5f88SHuang Ying { 1367155b5f88SHuang Ying const swp_entry_t *e1 = ent1, *e2 = ent2; 1368155b5f88SHuang Ying 1369155b5f88SHuang Ying return (int)swp_type(*e1) - (int)swp_type(*e2); 1370155b5f88SHuang Ying } 1371155b5f88SHuang Ying 13727c00bafeSTim Chen void swapcache_free_entries(swp_entry_t *entries, int n) 13737c00bafeSTim Chen { 13747c00bafeSTim Chen struct swap_info_struct *p, *prev; 13757c00bafeSTim Chen int i; 13767c00bafeSTim Chen 13777c00bafeSTim Chen if (n <= 0) 13787c00bafeSTim Chen return; 13797c00bafeSTim Chen 13807c00bafeSTim Chen prev = NULL; 13817c00bafeSTim Chen p = NULL; 1382155b5f88SHuang Ying 1383155b5f88SHuang Ying /* 1384155b5f88SHuang Ying * Sort swap entries by swap device, so each lock is only taken once. 1385155b5f88SHuang Ying * nr_swapfiles isn't absolutely correct, but the overhead of sort() is 1386155b5f88SHuang Ying * so low that it isn't necessary to optimize further. 1387155b5f88SHuang Ying */ 1388155b5f88SHuang Ying if (nr_swapfiles > 1) 1389155b5f88SHuang Ying sort(entries, n, sizeof(entries[0]), swp_entry_cmp, NULL); 13907c00bafeSTim Chen for (i = 0; i < n; ++i) { 13917c00bafeSTim Chen p = swap_info_get_cont(entries[i], prev); 1392235b6217SHuang, Ying if (p) 13937c00bafeSTim Chen swap_entry_free(p, entries[i]); 13947c00bafeSTim Chen prev = p; 13957c00bafeSTim Chen } 13967c00bafeSTim Chen if (p) 13977c00bafeSTim Chen spin_unlock(&p->lock); 1398cb4b86baSKAMEZAWA Hiroyuki } 1399cb4b86baSKAMEZAWA Hiroyuki 1400cb4b86baSKAMEZAWA Hiroyuki /* 1401c475a8abSHugh Dickins * How many references to page are currently swapped out? 1402570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 1403570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 14041da177e4SLinus Torvalds */ 1405bde05d1cSHugh Dickins int page_swapcount(struct page *page) 14061da177e4SLinus Torvalds { 1407c475a8abSHugh Dickins int count = 0; 14081da177e4SLinus Torvalds struct swap_info_struct *p; 1409235b6217SHuang, Ying struct swap_cluster_info *ci; 14101da177e4SLinus Torvalds swp_entry_t entry; 1411235b6217SHuang, Ying unsigned long offset; 14121da177e4SLinus Torvalds 14134c21e2f2SHugh Dickins entry.val = page_private(page); 1414235b6217SHuang, Ying p = _swap_info_get(entry); 14151da177e4SLinus Torvalds if (p) { 1416235b6217SHuang, Ying offset = swp_offset(entry); 1417235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1418235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 1419235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 14201da177e4SLinus Torvalds } 1421c475a8abSHugh Dickins return count; 14221da177e4SLinus Torvalds } 14231da177e4SLinus Torvalds 1424eb085574SHuang Ying int __swap_count(swp_entry_t entry) 1425aa8d22a1SMinchan Kim { 1426eb085574SHuang Ying struct swap_info_struct *si; 1427aa8d22a1SMinchan Kim pgoff_t offset = swp_offset(entry); 1428eb085574SHuang Ying int count = 0; 1429aa8d22a1SMinchan Kim 1430eb085574SHuang Ying si = get_swap_device(entry); 1431eb085574SHuang Ying if (si) { 1432eb085574SHuang Ying count = swap_count(si->swap_map[offset]); 1433eb085574SHuang Ying put_swap_device(si); 1434eb085574SHuang Ying } 1435eb085574SHuang Ying return count; 1436aa8d22a1SMinchan Kim } 1437aa8d22a1SMinchan Kim 1438322b8afeSHuang Ying static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) 1439322b8afeSHuang Ying { 1440322b8afeSHuang Ying int count = 0; 1441322b8afeSHuang Ying pgoff_t offset = swp_offset(entry); 1442322b8afeSHuang Ying struct swap_cluster_info *ci; 1443322b8afeSHuang Ying 1444322b8afeSHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1445322b8afeSHuang Ying count = swap_count(si->swap_map[offset]); 1446322b8afeSHuang Ying unlock_cluster_or_swap_info(si, ci); 1447322b8afeSHuang Ying return count; 1448322b8afeSHuang Ying } 1449322b8afeSHuang Ying 14501da177e4SLinus Torvalds /* 14518334b962SMinchan Kim * How many references to @entry are currently swapped out? 1452e8c26ab6STim Chen * This does not give an exact answer when swap count is continued, 1453e8c26ab6STim Chen * but does include the high COUNT_CONTINUED flag to allow for that. 1454e8c26ab6STim Chen */ 1455e8c26ab6STim Chen int __swp_swapcount(swp_entry_t entry) 1456e8c26ab6STim Chen { 1457e8c26ab6STim Chen int count = 0; 1458e8c26ab6STim Chen struct swap_info_struct *si; 1459e8c26ab6STim Chen 1460eb085574SHuang Ying si = get_swap_device(entry); 1461eb085574SHuang Ying if (si) { 1462322b8afeSHuang Ying count = swap_swapcount(si, entry); 1463eb085574SHuang Ying put_swap_device(si); 1464eb085574SHuang Ying } 1465e8c26ab6STim Chen return count; 1466e8c26ab6STim Chen } 1467e8c26ab6STim Chen 1468e8c26ab6STim Chen /* 1469e8c26ab6STim Chen * How many references to @entry are currently swapped out? 14708334b962SMinchan Kim * This considers COUNT_CONTINUED so it returns exact answer. 14718334b962SMinchan Kim */ 14728334b962SMinchan Kim int swp_swapcount(swp_entry_t entry) 14738334b962SMinchan Kim { 14748334b962SMinchan Kim int count, tmp_count, n; 14758334b962SMinchan Kim struct swap_info_struct *p; 1476235b6217SHuang, Ying struct swap_cluster_info *ci; 14778334b962SMinchan Kim struct page *page; 14788334b962SMinchan Kim pgoff_t offset; 14798334b962SMinchan Kim unsigned char *map; 14808334b962SMinchan Kim 1481235b6217SHuang, Ying p = _swap_info_get(entry); 14828334b962SMinchan Kim if (!p) 14838334b962SMinchan Kim return 0; 14848334b962SMinchan Kim 1485235b6217SHuang, Ying offset = swp_offset(entry); 1486235b6217SHuang, Ying 1487235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 1488235b6217SHuang, Ying 1489235b6217SHuang, Ying count = swap_count(p->swap_map[offset]); 14908334b962SMinchan Kim if (!(count & COUNT_CONTINUED)) 14918334b962SMinchan Kim goto out; 14928334b962SMinchan Kim 14938334b962SMinchan Kim count &= ~COUNT_CONTINUED; 14948334b962SMinchan Kim n = SWAP_MAP_MAX + 1; 14958334b962SMinchan Kim 14968334b962SMinchan Kim page = vmalloc_to_page(p->swap_map + offset); 14978334b962SMinchan Kim offset &= ~PAGE_MASK; 14988334b962SMinchan Kim VM_BUG_ON(page_private(page) != SWP_CONTINUED); 14998334b962SMinchan Kim 15008334b962SMinchan Kim do { 1501a8ae4991SGeliang Tang page = list_next_entry(page, lru); 15028334b962SMinchan Kim map = kmap_atomic(page); 15038334b962SMinchan Kim tmp_count = map[offset]; 15048334b962SMinchan Kim kunmap_atomic(map); 15058334b962SMinchan Kim 15068334b962SMinchan Kim count += (tmp_count & ~COUNT_CONTINUED) * n; 15078334b962SMinchan Kim n *= (SWAP_CONT_MAX + 1); 15088334b962SMinchan Kim } while (tmp_count & COUNT_CONTINUED); 15098334b962SMinchan Kim out: 1510235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 15118334b962SMinchan Kim return count; 15128334b962SMinchan Kim } 15138334b962SMinchan Kim 1514e0709829SHuang Ying static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, 1515e0709829SHuang Ying swp_entry_t entry) 1516e0709829SHuang Ying { 1517e0709829SHuang Ying struct swap_cluster_info *ci; 1518e0709829SHuang Ying unsigned char *map = si->swap_map; 1519e0709829SHuang Ying unsigned long roffset = swp_offset(entry); 1520e0709829SHuang Ying unsigned long offset = round_down(roffset, SWAPFILE_CLUSTER); 1521e0709829SHuang Ying int i; 1522e0709829SHuang Ying bool ret = false; 1523e0709829SHuang Ying 1524e0709829SHuang Ying ci = lock_cluster_or_swap_info(si, offset); 1525e0709829SHuang Ying if (!ci || !cluster_is_huge(ci)) { 1526afa4711eSHuang Ying if (swap_count(map[roffset])) 1527e0709829SHuang Ying ret = true; 1528e0709829SHuang Ying goto unlock_out; 1529e0709829SHuang Ying } 1530e0709829SHuang Ying for (i = 0; i < SWAPFILE_CLUSTER; i++) { 1531afa4711eSHuang Ying if (swap_count(map[offset + i])) { 1532e0709829SHuang Ying ret = true; 1533e0709829SHuang Ying break; 1534e0709829SHuang Ying } 1535e0709829SHuang Ying } 1536e0709829SHuang Ying unlock_out: 1537e0709829SHuang Ying unlock_cluster_or_swap_info(si, ci); 1538e0709829SHuang Ying return ret; 1539e0709829SHuang Ying } 1540e0709829SHuang Ying 1541e0709829SHuang Ying static bool page_swapped(struct page *page) 1542e0709829SHuang Ying { 1543e0709829SHuang Ying swp_entry_t entry; 1544e0709829SHuang Ying struct swap_info_struct *si; 1545e0709829SHuang Ying 1546fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) 1547e0709829SHuang Ying return page_swapcount(page) != 0; 1548e0709829SHuang Ying 1549e0709829SHuang Ying page = compound_head(page); 1550e0709829SHuang Ying entry.val = page_private(page); 1551e0709829SHuang Ying si = _swap_info_get(entry); 1552e0709829SHuang Ying if (si) 1553e0709829SHuang Ying return swap_page_trans_huge_swapped(si, entry); 1554e0709829SHuang Ying return false; 1555e0709829SHuang Ying } 1556ba3c4ce6SHuang Ying 1557ba3c4ce6SHuang Ying static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount, 1558ba3c4ce6SHuang Ying int *total_swapcount) 1559ba3c4ce6SHuang Ying { 1560ba3c4ce6SHuang Ying int i, map_swapcount, _total_mapcount, _total_swapcount; 1561ba3c4ce6SHuang Ying unsigned long offset = 0; 1562ba3c4ce6SHuang Ying struct swap_info_struct *si; 1563ba3c4ce6SHuang Ying struct swap_cluster_info *ci = NULL; 1564ba3c4ce6SHuang Ying unsigned char *map = NULL; 1565ba3c4ce6SHuang Ying int mapcount, swapcount = 0; 1566ba3c4ce6SHuang Ying 1567ba3c4ce6SHuang Ying /* hugetlbfs shouldn't call it */ 1568ba3c4ce6SHuang Ying VM_BUG_ON_PAGE(PageHuge(page), page); 1569ba3c4ce6SHuang Ying 1570fe5266d5SHuang Ying if (!IS_ENABLED(CONFIG_THP_SWAP) || likely(!PageTransCompound(page))) { 1571fe5266d5SHuang Ying mapcount = page_trans_huge_mapcount(page, total_mapcount); 1572ba3c4ce6SHuang Ying if (PageSwapCache(page)) 1573ba3c4ce6SHuang Ying swapcount = page_swapcount(page); 1574ba3c4ce6SHuang Ying if (total_swapcount) 1575ba3c4ce6SHuang Ying *total_swapcount = swapcount; 1576ba3c4ce6SHuang Ying return mapcount + swapcount; 1577ba3c4ce6SHuang Ying } 1578ba3c4ce6SHuang Ying 1579ba3c4ce6SHuang Ying page = compound_head(page); 1580ba3c4ce6SHuang Ying 1581ba3c4ce6SHuang Ying _total_mapcount = _total_swapcount = map_swapcount = 0; 1582ba3c4ce6SHuang Ying if (PageSwapCache(page)) { 1583ba3c4ce6SHuang Ying swp_entry_t entry; 1584ba3c4ce6SHuang Ying 1585ba3c4ce6SHuang Ying entry.val = page_private(page); 1586ba3c4ce6SHuang Ying si = _swap_info_get(entry); 1587ba3c4ce6SHuang Ying if (si) { 1588ba3c4ce6SHuang Ying map = si->swap_map; 1589ba3c4ce6SHuang Ying offset = swp_offset(entry); 1590ba3c4ce6SHuang Ying } 1591ba3c4ce6SHuang Ying } 1592ba3c4ce6SHuang Ying if (map) 1593ba3c4ce6SHuang Ying ci = lock_cluster(si, offset); 1594ba3c4ce6SHuang Ying for (i = 0; i < HPAGE_PMD_NR; i++) { 1595ba3c4ce6SHuang Ying mapcount = atomic_read(&page[i]._mapcount) + 1; 1596ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1597ba3c4ce6SHuang Ying if (map) { 1598ba3c4ce6SHuang Ying swapcount = swap_count(map[offset + i]); 1599ba3c4ce6SHuang Ying _total_swapcount += swapcount; 1600ba3c4ce6SHuang Ying } 1601ba3c4ce6SHuang Ying map_swapcount = max(map_swapcount, mapcount + swapcount); 1602ba3c4ce6SHuang Ying } 1603ba3c4ce6SHuang Ying unlock_cluster(ci); 1604ba3c4ce6SHuang Ying if (PageDoubleMap(page)) { 1605ba3c4ce6SHuang Ying map_swapcount -= 1; 1606ba3c4ce6SHuang Ying _total_mapcount -= HPAGE_PMD_NR; 1607ba3c4ce6SHuang Ying } 1608ba3c4ce6SHuang Ying mapcount = compound_mapcount(page); 1609ba3c4ce6SHuang Ying map_swapcount += mapcount; 1610ba3c4ce6SHuang Ying _total_mapcount += mapcount; 1611ba3c4ce6SHuang Ying if (total_mapcount) 1612ba3c4ce6SHuang Ying *total_mapcount = _total_mapcount; 1613ba3c4ce6SHuang Ying if (total_swapcount) 1614ba3c4ce6SHuang Ying *total_swapcount = _total_swapcount; 1615ba3c4ce6SHuang Ying 1616ba3c4ce6SHuang Ying return map_swapcount; 1617ba3c4ce6SHuang Ying } 1618e0709829SHuang Ying 16198334b962SMinchan Kim /* 16207b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 16217b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 16227b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 16237b1fe597SHugh Dickins * later would only waste time away from clustering. 16246d0a07edSAndrea Arcangeli * 1625ba3c4ce6SHuang Ying * NOTE: total_map_swapcount should not be relied upon by the caller if 16266d0a07edSAndrea Arcangeli * reuse_swap_page() returns false, but it may be always overwritten 16276d0a07edSAndrea Arcangeli * (see the other implementation for CONFIG_SWAP=n). 16281da177e4SLinus Torvalds */ 1629ba3c4ce6SHuang Ying bool reuse_swap_page(struct page *page, int *total_map_swapcount) 16301da177e4SLinus Torvalds { 1631ba3c4ce6SHuang Ying int count, total_mapcount, total_swapcount; 16321da177e4SLinus Torvalds 1633309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 16345ad64688SHugh Dickins if (unlikely(PageKsm(page))) 16356d0a07edSAndrea Arcangeli return false; 1636ba3c4ce6SHuang Ying count = page_trans_huge_map_swapcount(page, &total_mapcount, 1637ba3c4ce6SHuang Ying &total_swapcount); 1638ba3c4ce6SHuang Ying if (total_map_swapcount) 1639ba3c4ce6SHuang Ying *total_map_swapcount = total_mapcount + total_swapcount; 1640ba3c4ce6SHuang Ying if (count == 1 && PageSwapCache(page) && 1641ba3c4ce6SHuang Ying (likely(!PageTransCompound(page)) || 1642ba3c4ce6SHuang Ying /* The remaining swap count will be freed soon */ 1643ba3c4ce6SHuang Ying total_swapcount == page_swapcount(page))) { 1644f0571429SMinchan Kim if (!PageWriteback(page)) { 1645ba3c4ce6SHuang Ying page = compound_head(page); 16467b1fe597SHugh Dickins delete_from_swap_cache(page); 16477b1fe597SHugh Dickins SetPageDirty(page); 1648f0571429SMinchan Kim } else { 1649f0571429SMinchan Kim swp_entry_t entry; 1650f0571429SMinchan Kim struct swap_info_struct *p; 1651f0571429SMinchan Kim 1652f0571429SMinchan Kim entry.val = page_private(page); 1653f0571429SMinchan Kim p = swap_info_get(entry); 1654f0571429SMinchan Kim if (p->flags & SWP_STABLE_WRITES) { 1655f0571429SMinchan Kim spin_unlock(&p->lock); 1656f0571429SMinchan Kim return false; 1657f0571429SMinchan Kim } 1658f0571429SMinchan Kim spin_unlock(&p->lock); 16597b1fe597SHugh Dickins } 16607b1fe597SHugh Dickins } 1661ba3c4ce6SHuang Ying 16625ad64688SHugh Dickins return count <= 1; 16631da177e4SLinus Torvalds } 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds /* 1666a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 1667a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 16681da177e4SLinus Torvalds */ 1669a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 16701da177e4SLinus Torvalds { 1671309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds if (!PageSwapCache(page)) 16741da177e4SLinus Torvalds return 0; 16751da177e4SLinus Torvalds if (PageWriteback(page)) 16761da177e4SLinus Torvalds return 0; 1677e0709829SHuang Ying if (page_swapped(page)) 16781da177e4SLinus Torvalds return 0; 16791da177e4SLinus Torvalds 1680b73d7fceSHugh Dickins /* 1681b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 1682b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 1683b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 1684b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 1685b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 1686b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 1687b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 1688b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 1689b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 1690b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 1691b73d7fceSHugh Dickins * 16922de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 1693f90ac398SMel Gorman * to disk so check that here. 1694b73d7fceSHugh Dickins */ 1695f90ac398SMel Gorman if (pm_suspended_storage()) 1696b73d7fceSHugh Dickins return 0; 1697b73d7fceSHugh Dickins 1698e0709829SHuang Ying page = compound_head(page); 1699a2c43eedSHugh Dickins delete_from_swap_cache(page); 17001da177e4SLinus Torvalds SetPageDirty(page); 1701a2c43eedSHugh Dickins return 1; 170268a22394SRik van Riel } 170368a22394SRik van Riel 170468a22394SRik van Riel /* 17051da177e4SLinus Torvalds * Free the swap entry like above, but also try to 17061da177e4SLinus Torvalds * free the page cache entry if it is the last user. 17071da177e4SLinus Torvalds */ 17082509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 17091da177e4SLinus Torvalds { 17101da177e4SLinus Torvalds struct swap_info_struct *p; 17117c00bafeSTim Chen unsigned char count; 17121da177e4SLinus Torvalds 1713a7420aa5SAndi Kleen if (non_swap_entry(entry)) 17142509ef26SHugh Dickins return 1; 17150697212aSChristoph Lameter 17167c00bafeSTim Chen p = _swap_info_get(entry); 17171da177e4SLinus Torvalds if (p) { 17187c00bafeSTim Chen count = __swap_entry_free(p, entry, 1); 1719e0709829SHuang Ying if (count == SWAP_HAS_CACHE && 1720bcd49e86SHuang Ying !swap_page_trans_huge_swapped(p, entry)) 1721bcd49e86SHuang Ying __try_to_reclaim_swap(p, swp_offset(entry), 1722bcd49e86SHuang Ying TTRS_UNMAPPED | TTRS_FULL); 17231da177e4SLinus Torvalds } 17242509ef26SHugh Dickins return p != NULL; 17251da177e4SLinus Torvalds } 17261da177e4SLinus Torvalds 1727b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 1728f577eb30SRafael J. Wysocki /* 1729915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 1730f577eb30SRafael J. Wysocki * 1731915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 1732915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 1733915bae9eSRafael J. Wysocki * 1734915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 1735f577eb30SRafael J. Wysocki */ 17367bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 1737f577eb30SRafael J. Wysocki { 1738915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 1739efa90a98SHugh Dickins int type; 1740f577eb30SRafael J. Wysocki 1741915bae9eSRafael J. Wysocki if (device) 1742915bae9eSRafael J. Wysocki bdev = bdget(device); 1743915bae9eSRafael J. Wysocki 1744f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1745efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1746efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1747f577eb30SRafael J. Wysocki 1748915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1749f577eb30SRafael J. Wysocki continue; 1750b6b5bce3SRafael J. Wysocki 1751915bae9eSRafael J. Wysocki if (!bdev) { 17527bf23687SRafael J. Wysocki if (bdev_p) 1753dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 17547bf23687SRafael J. Wysocki 17556e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1756efa90a98SHugh Dickins return type; 17576e1819d6SRafael J. Wysocki } 1758915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 17594efaceb1SAaron Lu struct swap_extent *se = first_se(sis); 1760915bae9eSRafael J. Wysocki 1761915bae9eSRafael J. Wysocki if (se->start_block == offset) { 17627bf23687SRafael J. Wysocki if (bdev_p) 1763dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 17647bf23687SRafael J. Wysocki 1765f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1766915bae9eSRafael J. Wysocki bdput(bdev); 1767efa90a98SHugh Dickins return type; 1768f577eb30SRafael J. Wysocki } 1769f577eb30SRafael J. Wysocki } 1770915bae9eSRafael J. Wysocki } 1771f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1772915bae9eSRafael J. Wysocki if (bdev) 1773915bae9eSRafael J. Wysocki bdput(bdev); 1774915bae9eSRafael J. Wysocki 1775f577eb30SRafael J. Wysocki return -ENODEV; 1776f577eb30SRafael J. Wysocki } 1777f577eb30SRafael J. Wysocki 1778f577eb30SRafael J. Wysocki /* 177973c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 178073c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 178173c34b6aSHugh Dickins */ 178273c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 178373c34b6aSHugh Dickins { 178473c34b6aSHugh Dickins struct block_device *bdev; 1785c10d38ccSDaniel Jordan struct swap_info_struct *si = swap_type_to_swap_info(type); 178673c34b6aSHugh Dickins 1787c10d38ccSDaniel Jordan if (!si || !(si->flags & SWP_WRITEOK)) 178873c34b6aSHugh Dickins return 0; 1789d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 179073c34b6aSHugh Dickins } 179173c34b6aSHugh Dickins 179273c34b6aSHugh Dickins /* 1793f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1794f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1795f577eb30SRafael J. Wysocki * 1796f577eb30SRafael J. Wysocki * This is needed for software suspend 1797f577eb30SRafael J. Wysocki */ 1798f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1799f577eb30SRafael J. Wysocki { 1800f577eb30SRafael J. Wysocki unsigned int n = 0; 1801f577eb30SRafael J. Wysocki 1802f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1803efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1804efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1805efa90a98SHugh Dickins 1806ec8acf20SShaohua Li spin_lock(&sis->lock); 1807efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1808efa90a98SHugh Dickins n = sis->pages; 1809f577eb30SRafael J. Wysocki if (free) 1810efa90a98SHugh Dickins n -= sis->inuse_pages; 1811efa90a98SHugh Dickins } 1812ec8acf20SShaohua Li spin_unlock(&sis->lock); 1813f577eb30SRafael J. Wysocki } 1814f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1815f577eb30SRafael J. Wysocki return n; 1816f577eb30SRafael J. Wysocki } 181773c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1818f577eb30SRafael J. Wysocki 18199f8bdb3fSHugh Dickins static inline int pte_same_as_swp(pte_t pte, pte_t swp_pte) 1820179ef71cSCyrill Gorcunov { 18219f8bdb3fSHugh Dickins return pte_same(pte_swp_clear_soft_dirty(pte), swp_pte); 1822179ef71cSCyrill Gorcunov } 1823179ef71cSCyrill Gorcunov 18241da177e4SLinus Torvalds /* 182572866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 182672866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 182772866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 18281da177e4SLinus Torvalds */ 1829044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 18301da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 18311da177e4SLinus Torvalds { 18329e16b7fbSHugh Dickins struct page *swapcache; 183372835c86SJohannes Weiner struct mem_cgroup *memcg; 1834044d66c1SHugh Dickins spinlock_t *ptl; 1835044d66c1SHugh Dickins pte_t *pte; 1836044d66c1SHugh Dickins int ret = 1; 1837044d66c1SHugh Dickins 18389e16b7fbSHugh Dickins swapcache = page; 18399e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 18409e16b7fbSHugh Dickins if (unlikely(!page)) 18419e16b7fbSHugh Dickins return -ENOMEM; 18429e16b7fbSHugh Dickins 1843f627c2f5SKirill A. Shutemov if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, 1844f627c2f5SKirill A. Shutemov &memcg, false)) { 1845044d66c1SHugh Dickins ret = -ENOMEM; 184685d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 184785d9fc89SKAMEZAWA Hiroyuki } 1848044d66c1SHugh Dickins 1849044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 18509f8bdb3fSHugh Dickins if (unlikely(!pte_same_as_swp(*pte, swp_entry_to_pte(entry)))) { 1851f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1852044d66c1SHugh Dickins ret = 0; 1853044d66c1SHugh Dickins goto out; 1854044d66c1SHugh Dickins } 18558a9f3ccdSBalbir Singh 1856b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1857d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 18581da177e4SLinus Torvalds get_page(page); 18591da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 18601da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 186100501b53SJohannes Weiner if (page == swapcache) { 1862d281ee61SKirill A. Shutemov page_add_anon_rmap(page, vma, addr, false); 1863f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 186400501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 1865d281ee61SKirill A. Shutemov page_add_new_anon_rmap(page, vma, addr, false); 1866f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, false); 186700501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 186800501b53SJohannes Weiner } 18691da177e4SLinus Torvalds swap_free(entry); 18701da177e4SLinus Torvalds /* 18711da177e4SLinus Torvalds * Move the page to the active list so it is not 18721da177e4SLinus Torvalds * immediately swapped out again after swapon. 18731da177e4SLinus Torvalds */ 18741da177e4SLinus Torvalds activate_page(page); 1875044d66c1SHugh Dickins out: 1876044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 187785d9fc89SKAMEZAWA Hiroyuki out_nolock: 18789e16b7fbSHugh Dickins if (page != swapcache) { 18799e16b7fbSHugh Dickins unlock_page(page); 18809e16b7fbSHugh Dickins put_page(page); 18819e16b7fbSHugh Dickins } 1882044d66c1SHugh Dickins return ret; 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds 18851da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 18861da177e4SLinus Torvalds unsigned long addr, unsigned long end, 1887b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 1888b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 18891da177e4SLinus Torvalds { 1890b56a2d8aSVineeth Remanan Pillai struct page *page; 1891b56a2d8aSVineeth Remanan Pillai swp_entry_t entry; 1892705e87c0SHugh Dickins pte_t *pte; 1893b56a2d8aSVineeth Remanan Pillai struct swap_info_struct *si; 1894b56a2d8aSVineeth Remanan Pillai unsigned long offset; 18958a9f3ccdSBalbir Singh int ret = 0; 1896b56a2d8aSVineeth Remanan Pillai volatile unsigned char *swap_map; 18971da177e4SLinus Torvalds 1898b56a2d8aSVineeth Remanan Pillai si = swap_info[type]; 1899044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 19001da177e4SLinus Torvalds do { 1901b56a2d8aSVineeth Remanan Pillai struct vm_fault vmf; 1902b56a2d8aSVineeth Remanan Pillai 1903b56a2d8aSVineeth Remanan Pillai if (!is_swap_pte(*pte)) 1904b56a2d8aSVineeth Remanan Pillai continue; 1905b56a2d8aSVineeth Remanan Pillai 1906b56a2d8aSVineeth Remanan Pillai entry = pte_to_swp_entry(*pte); 1907b56a2d8aSVineeth Remanan Pillai if (swp_type(entry) != type) 1908b56a2d8aSVineeth Remanan Pillai continue; 1909b56a2d8aSVineeth Remanan Pillai 1910b56a2d8aSVineeth Remanan Pillai offset = swp_offset(entry); 1911b56a2d8aSVineeth Remanan Pillai if (frontswap && !frontswap_test(si, offset)) 1912b56a2d8aSVineeth Remanan Pillai continue; 1913b56a2d8aSVineeth Remanan Pillai 1914044d66c1SHugh Dickins pte_unmap(pte); 1915b56a2d8aSVineeth Remanan Pillai swap_map = &si->swap_map[offset]; 1916ebc5951eSAndrea Righi page = lookup_swap_cache(entry, vma, addr); 1917ebc5951eSAndrea Righi if (!page) { 1918b56a2d8aSVineeth Remanan Pillai vmf.vma = vma; 1919b56a2d8aSVineeth Remanan Pillai vmf.address = addr; 1920b56a2d8aSVineeth Remanan Pillai vmf.pmd = pmd; 1921ebc5951eSAndrea Righi page = swapin_readahead(entry, GFP_HIGHUSER_MOVABLE, 1922ebc5951eSAndrea Righi &vmf); 1923ebc5951eSAndrea Righi } 1924b56a2d8aSVineeth Remanan Pillai if (!page) { 1925b56a2d8aSVineeth Remanan Pillai if (*swap_map == 0 || *swap_map == SWAP_MAP_BAD) 1926b56a2d8aSVineeth Remanan Pillai goto try_next; 1927b56a2d8aSVineeth Remanan Pillai return -ENOMEM; 19281da177e4SLinus Torvalds } 1929b56a2d8aSVineeth Remanan Pillai 1930b56a2d8aSVineeth Remanan Pillai lock_page(page); 1931b56a2d8aSVineeth Remanan Pillai wait_on_page_writeback(page); 1932b56a2d8aSVineeth Remanan Pillai ret = unuse_pte(vma, pmd, addr, entry, page); 1933b56a2d8aSVineeth Remanan Pillai if (ret < 0) { 1934b56a2d8aSVineeth Remanan Pillai unlock_page(page); 1935b56a2d8aSVineeth Remanan Pillai put_page(page); 1936b56a2d8aSVineeth Remanan Pillai goto out; 1937b56a2d8aSVineeth Remanan Pillai } 1938b56a2d8aSVineeth Remanan Pillai 1939b56a2d8aSVineeth Remanan Pillai try_to_free_swap(page); 1940b56a2d8aSVineeth Remanan Pillai unlock_page(page); 1941b56a2d8aSVineeth Remanan Pillai put_page(page); 1942b56a2d8aSVineeth Remanan Pillai 1943b56a2d8aSVineeth Remanan Pillai if (*fs_pages_to_unuse && !--(*fs_pages_to_unuse)) { 1944b56a2d8aSVineeth Remanan Pillai ret = FRONTSWAP_PAGES_UNUSED; 1945b56a2d8aSVineeth Remanan Pillai goto out; 1946b56a2d8aSVineeth Remanan Pillai } 1947b56a2d8aSVineeth Remanan Pillai try_next: 1948b56a2d8aSVineeth Remanan Pillai pte = pte_offset_map(pmd, addr); 19491da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1950044d66c1SHugh Dickins pte_unmap(pte - 1); 1951b56a2d8aSVineeth Remanan Pillai 1952b56a2d8aSVineeth Remanan Pillai ret = 0; 1953044d66c1SHugh Dickins out: 19548a9f3ccdSBalbir Singh return ret; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 19581da177e4SLinus Torvalds unsigned long addr, unsigned long end, 1959b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 1960b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 19611da177e4SLinus Torvalds { 19621da177e4SLinus Torvalds pmd_t *pmd; 19631da177e4SLinus Torvalds unsigned long next; 19648a9f3ccdSBalbir Singh int ret; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 19671da177e4SLinus Torvalds do { 1968dc644a07SHugh Dickins cond_resched(); 19691da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 19701a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 19711da177e4SLinus Torvalds continue; 1972b56a2d8aSVineeth Remanan Pillai ret = unuse_pte_range(vma, pmd, addr, next, type, 1973b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 19748a9f3ccdSBalbir Singh if (ret) 19758a9f3ccdSBalbir Singh return ret; 19761da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 19771da177e4SLinus Torvalds return 0; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 1980c2febafcSKirill A. Shutemov static inline int unuse_pud_range(struct vm_area_struct *vma, p4d_t *p4d, 19811da177e4SLinus Torvalds unsigned long addr, unsigned long end, 1982b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 1983b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 19841da177e4SLinus Torvalds { 19851da177e4SLinus Torvalds pud_t *pud; 19861da177e4SLinus Torvalds unsigned long next; 19878a9f3ccdSBalbir Singh int ret; 19881da177e4SLinus Torvalds 1989c2febafcSKirill A. Shutemov pud = pud_offset(p4d, addr); 19901da177e4SLinus Torvalds do { 19911da177e4SLinus Torvalds next = pud_addr_end(addr, end); 19921da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 19931da177e4SLinus Torvalds continue; 1994b56a2d8aSVineeth Remanan Pillai ret = unuse_pmd_range(vma, pud, addr, next, type, 1995b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 19968a9f3ccdSBalbir Singh if (ret) 19978a9f3ccdSBalbir Singh return ret; 19981da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 19991da177e4SLinus Torvalds return 0; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 2002c2febafcSKirill A. Shutemov static inline int unuse_p4d_range(struct vm_area_struct *vma, pgd_t *pgd, 2003c2febafcSKirill A. Shutemov unsigned long addr, unsigned long end, 2004b56a2d8aSVineeth Remanan Pillai unsigned int type, bool frontswap, 2005b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 2006c2febafcSKirill A. Shutemov { 2007c2febafcSKirill A. Shutemov p4d_t *p4d; 2008c2febafcSKirill A. Shutemov unsigned long next; 2009c2febafcSKirill A. Shutemov int ret; 2010c2febafcSKirill A. Shutemov 2011c2febafcSKirill A. Shutemov p4d = p4d_offset(pgd, addr); 2012c2febafcSKirill A. Shutemov do { 2013c2febafcSKirill A. Shutemov next = p4d_addr_end(addr, end); 2014c2febafcSKirill A. Shutemov if (p4d_none_or_clear_bad(p4d)) 2015c2febafcSKirill A. Shutemov continue; 2016b56a2d8aSVineeth Remanan Pillai ret = unuse_pud_range(vma, p4d, addr, next, type, 2017b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 2018c2febafcSKirill A. Shutemov if (ret) 2019c2febafcSKirill A. Shutemov return ret; 2020c2febafcSKirill A. Shutemov } while (p4d++, addr = next, addr != end); 2021c2febafcSKirill A. Shutemov return 0; 2022c2febafcSKirill A. Shutemov } 2023c2febafcSKirill A. Shutemov 2024b56a2d8aSVineeth Remanan Pillai static int unuse_vma(struct vm_area_struct *vma, unsigned int type, 2025b56a2d8aSVineeth Remanan Pillai bool frontswap, unsigned long *fs_pages_to_unuse) 20261da177e4SLinus Torvalds { 20271da177e4SLinus Torvalds pgd_t *pgd; 20281da177e4SLinus Torvalds unsigned long addr, end, next; 20298a9f3ccdSBalbir Singh int ret; 20301da177e4SLinus Torvalds 20311da177e4SLinus Torvalds addr = vma->vm_start; 20321da177e4SLinus Torvalds end = vma->vm_end; 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 20351da177e4SLinus Torvalds do { 20361da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 20371da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 20381da177e4SLinus Torvalds continue; 2039b56a2d8aSVineeth Remanan Pillai ret = unuse_p4d_range(vma, pgd, addr, next, type, 2040b56a2d8aSVineeth Remanan Pillai frontswap, fs_pages_to_unuse); 20418a9f3ccdSBalbir Singh if (ret) 20428a9f3ccdSBalbir Singh return ret; 20431da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 20441da177e4SLinus Torvalds return 0; 20451da177e4SLinus Torvalds } 20461da177e4SLinus Torvalds 2047b56a2d8aSVineeth Remanan Pillai static int unuse_mm(struct mm_struct *mm, unsigned int type, 2048b56a2d8aSVineeth Remanan Pillai bool frontswap, unsigned long *fs_pages_to_unuse) 20491da177e4SLinus Torvalds { 20501da177e4SLinus Torvalds struct vm_area_struct *vma; 20518a9f3ccdSBalbir Singh int ret = 0; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds down_read(&mm->mmap_sem); 20541da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 2055b56a2d8aSVineeth Remanan Pillai if (vma->anon_vma) { 2056b56a2d8aSVineeth Remanan Pillai ret = unuse_vma(vma, type, frontswap, 2057b56a2d8aSVineeth Remanan Pillai fs_pages_to_unuse); 2058b56a2d8aSVineeth Remanan Pillai if (ret) 20591da177e4SLinus Torvalds break; 2060b56a2d8aSVineeth Remanan Pillai } 2061dc644a07SHugh Dickins cond_resched(); 20621da177e4SLinus Torvalds } 20631da177e4SLinus Torvalds up_read(&mm->mmap_sem); 2064b56a2d8aSVineeth Remanan Pillai return ret; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds /* 206838b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 2069b56a2d8aSVineeth Remanan Pillai * from current position to next entry still in use. Return 0 2070b56a2d8aSVineeth Remanan Pillai * if there are no inuse entries after prev till end of the map. 20711da177e4SLinus Torvalds */ 20726eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 207338b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 20741da177e4SLinus Torvalds { 2075b56a2d8aSVineeth Remanan Pillai unsigned int i; 20768d69aaeeSHugh Dickins unsigned char count; 20771da177e4SLinus Torvalds 20781da177e4SLinus Torvalds /* 20795d337b91SHugh Dickins * No need for swap_lock here: we're just looking 20801da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 20811da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 20825d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 20831da177e4SLinus Torvalds */ 2084b56a2d8aSVineeth Remanan Pillai for (i = prev + 1; i < si->max; i++) { 20854db0c3c2SJason Low count = READ_ONCE(si->swap_map[i]); 2086355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 2087dc644a07SHugh Dickins if (!frontswap || frontswap_test(si, i)) 20881da177e4SLinus Torvalds break; 2089dc644a07SHugh Dickins if ((i % LATENCY_LIMIT) == 0) 2090dc644a07SHugh Dickins cond_resched(); 20911da177e4SLinus Torvalds } 2092b56a2d8aSVineeth Remanan Pillai 2093b56a2d8aSVineeth Remanan Pillai if (i == si->max) 2094b56a2d8aSVineeth Remanan Pillai i = 0; 2095b56a2d8aSVineeth Remanan Pillai 20961da177e4SLinus Torvalds return i; 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /* 2100b56a2d8aSVineeth Remanan Pillai * If the boolean frontswap is true, only unuse pages_to_unuse pages; 210138b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 21021da177e4SLinus Torvalds */ 210338b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 210438b5faf4SDan Magenheimer unsigned long pages_to_unuse) 21051da177e4SLinus Torvalds { 2106b56a2d8aSVineeth Remanan Pillai struct mm_struct *prev_mm; 2107b56a2d8aSVineeth Remanan Pillai struct mm_struct *mm; 2108b56a2d8aSVineeth Remanan Pillai struct list_head *p; 2109b56a2d8aSVineeth Remanan Pillai int retval = 0; 2110efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 21111da177e4SLinus Torvalds struct page *page; 21121da177e4SLinus Torvalds swp_entry_t entry; 2113b56a2d8aSVineeth Remanan Pillai unsigned int i; 21141da177e4SLinus Torvalds 211521820948SQian Cai if (!READ_ONCE(si->inuse_pages)) 2116b56a2d8aSVineeth Remanan Pillai return 0; 21171da177e4SLinus Torvalds 2118b56a2d8aSVineeth Remanan Pillai if (!frontswap) 2119b56a2d8aSVineeth Remanan Pillai pages_to_unuse = 0; 2120b56a2d8aSVineeth Remanan Pillai 2121b56a2d8aSVineeth Remanan Pillai retry: 2122b56a2d8aSVineeth Remanan Pillai retval = shmem_unuse(type, frontswap, &pages_to_unuse); 2123b56a2d8aSVineeth Remanan Pillai if (retval) 2124b56a2d8aSVineeth Remanan Pillai goto out; 2125b56a2d8aSVineeth Remanan Pillai 2126b56a2d8aSVineeth Remanan Pillai prev_mm = &init_mm; 2127b56a2d8aSVineeth Remanan Pillai mmget(prev_mm); 2128b56a2d8aSVineeth Remanan Pillai 2129b56a2d8aSVineeth Remanan Pillai spin_lock(&mmlist_lock); 2130b56a2d8aSVineeth Remanan Pillai p = &init_mm.mmlist; 213121820948SQian Cai while (READ_ONCE(si->inuse_pages) && 213264165b1aSHugh Dickins !signal_pending(current) && 213364165b1aSHugh Dickins (p = p->next) != &init_mm.mmlist) { 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 2136388f7934SVegard Nossum if (!mmget_not_zero(mm)) 21371da177e4SLinus Torvalds continue; 21381da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 21391da177e4SLinus Torvalds mmput(prev_mm); 21401da177e4SLinus Torvalds prev_mm = mm; 2141b56a2d8aSVineeth Remanan Pillai retval = unuse_mm(mm, type, frontswap, &pages_to_unuse); 21421da177e4SLinus Torvalds 21431da177e4SLinus Torvalds if (retval) { 2144b56a2d8aSVineeth Remanan Pillai mmput(prev_mm); 2145b56a2d8aSVineeth Remanan Pillai goto out; 21461da177e4SLinus Torvalds } 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds /* 21491da177e4SLinus Torvalds * Make sure that we aren't completely killing 21501da177e4SLinus Torvalds * interactive performance. 21511da177e4SLinus Torvalds */ 21521da177e4SLinus Torvalds cond_resched(); 2153b56a2d8aSVineeth Remanan Pillai spin_lock(&mmlist_lock); 215438b5faf4SDan Magenheimer } 2155b56a2d8aSVineeth Remanan Pillai spin_unlock(&mmlist_lock); 2156b56a2d8aSVineeth Remanan Pillai 2157b56a2d8aSVineeth Remanan Pillai mmput(prev_mm); 2158b56a2d8aSVineeth Remanan Pillai 2159b56a2d8aSVineeth Remanan Pillai i = 0; 216021820948SQian Cai while (READ_ONCE(si->inuse_pages) && 216164165b1aSHugh Dickins !signal_pending(current) && 216264165b1aSHugh Dickins (i = find_next_to_unuse(si, i, frontswap)) != 0) { 2163b56a2d8aSVineeth Remanan Pillai 2164b56a2d8aSVineeth Remanan Pillai entry = swp_entry(type, i); 2165b56a2d8aSVineeth Remanan Pillai page = find_get_page(swap_address_space(entry), i); 2166b56a2d8aSVineeth Remanan Pillai if (!page) 2167b56a2d8aSVineeth Remanan Pillai continue; 2168b56a2d8aSVineeth Remanan Pillai 2169b56a2d8aSVineeth Remanan Pillai /* 2170b56a2d8aSVineeth Remanan Pillai * It is conceivable that a racing task removed this page from 2171b56a2d8aSVineeth Remanan Pillai * swap cache just before we acquired the page lock. The page 2172b56a2d8aSVineeth Remanan Pillai * might even be back in swap cache on another swap area. But 2173b56a2d8aSVineeth Remanan Pillai * that is okay, try_to_free_swap() only removes stale pages. 2174b56a2d8aSVineeth Remanan Pillai */ 2175b56a2d8aSVineeth Remanan Pillai lock_page(page); 2176b56a2d8aSVineeth Remanan Pillai wait_on_page_writeback(page); 2177b56a2d8aSVineeth Remanan Pillai try_to_free_swap(page); 2178b56a2d8aSVineeth Remanan Pillai unlock_page(page); 2179b56a2d8aSVineeth Remanan Pillai put_page(page); 2180b56a2d8aSVineeth Remanan Pillai 2181b56a2d8aSVineeth Remanan Pillai /* 2182b56a2d8aSVineeth Remanan Pillai * For frontswap, we just need to unuse pages_to_unuse, if 2183b56a2d8aSVineeth Remanan Pillai * it was specified. Need not check frontswap again here as 2184b56a2d8aSVineeth Remanan Pillai * we already zeroed out pages_to_unuse if not frontswap. 2185b56a2d8aSVineeth Remanan Pillai */ 2186b56a2d8aSVineeth Remanan Pillai if (pages_to_unuse && --pages_to_unuse == 0) 2187b56a2d8aSVineeth Remanan Pillai goto out; 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds 2190b56a2d8aSVineeth Remanan Pillai /* 2191b56a2d8aSVineeth Remanan Pillai * Lets check again to see if there are still swap entries in the map. 2192b56a2d8aSVineeth Remanan Pillai * If yes, we would need to do retry the unuse logic again. 2193b56a2d8aSVineeth Remanan Pillai * Under global memory pressure, swap entries can be reinserted back 2194b56a2d8aSVineeth Remanan Pillai * into process space after the mmlist loop above passes over them. 2195dd862debSHugh Dickins * 2196af53d3e9SHugh Dickins * Limit the number of retries? No: when mmget_not_zero() above fails, 2197af53d3e9SHugh Dickins * that mm is likely to be freeing swap from exit_mmap(), which proceeds 2198af53d3e9SHugh Dickins * at its own independent pace; and even shmem_writepage() could have 2199af53d3e9SHugh Dickins * been preempted after get_swap_page(), temporarily hiding that swap. 2200af53d3e9SHugh Dickins * It's easy and robust (though cpu-intensive) just to keep retrying. 2201b56a2d8aSVineeth Remanan Pillai */ 220221820948SQian Cai if (READ_ONCE(si->inuse_pages)) { 220364165b1aSHugh Dickins if (!signal_pending(current)) 2204b56a2d8aSVineeth Remanan Pillai goto retry; 220564165b1aSHugh Dickins retval = -EINTR; 220664165b1aSHugh Dickins } 2207b56a2d8aSVineeth Remanan Pillai out: 2208b56a2d8aSVineeth Remanan Pillai return (retval == FRONTSWAP_PAGES_UNUSED) ? 0 : retval; 22091da177e4SLinus Torvalds } 22101da177e4SLinus Torvalds 22111da177e4SLinus Torvalds /* 22125d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 22135d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 22145d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 22151da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 22161da177e4SLinus Torvalds */ 22171da177e4SLinus Torvalds static void drain_mmlist(void) 22181da177e4SLinus Torvalds { 22191da177e4SLinus Torvalds struct list_head *p, *next; 2220efa90a98SHugh Dickins unsigned int type; 22211da177e4SLinus Torvalds 2222efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 2223efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 22241da177e4SLinus Torvalds return; 22251da177e4SLinus Torvalds spin_lock(&mmlist_lock); 22261da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 22271da177e4SLinus Torvalds list_del_init(p); 22281da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 22291da177e4SLinus Torvalds } 22301da177e4SLinus Torvalds 22311da177e4SLinus Torvalds /* 22321da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 2233d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 2234d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 2235d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 22361da177e4SLinus Torvalds */ 2237d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 22381da177e4SLinus Torvalds { 2239f29ad6a9SHugh Dickins struct swap_info_struct *sis; 2240f29ad6a9SHugh Dickins struct swap_extent *se; 2241f29ad6a9SHugh Dickins pgoff_t offset; 2242f29ad6a9SHugh Dickins 2243c10d38ccSDaniel Jordan sis = swp_swap_info(entry); 2244f29ad6a9SHugh Dickins *bdev = sis->bdev; 2245f29ad6a9SHugh Dickins 2246f29ad6a9SHugh Dickins offset = swp_offset(entry); 22474efaceb1SAaron Lu se = offset_to_swap_extent(sis, offset); 22481da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 22491da177e4SLinus Torvalds } 22501da177e4SLinus Torvalds 22511da177e4SLinus Torvalds /* 2252d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 2253d4906e1aSLee Schermerhorn */ 2254d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 2255d4906e1aSLee Schermerhorn { 2256d4906e1aSLee Schermerhorn swp_entry_t entry; 2257d4906e1aSLee Schermerhorn entry.val = page_private(page); 2258d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 2259d4906e1aSLee Schermerhorn } 2260d4906e1aSLee Schermerhorn 2261d4906e1aSLee Schermerhorn /* 22621da177e4SLinus Torvalds * Free all of a swapdev's extent information 22631da177e4SLinus Torvalds */ 22641da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 22651da177e4SLinus Torvalds { 22664efaceb1SAaron Lu while (!RB_EMPTY_ROOT(&sis->swap_extent_root)) { 22674efaceb1SAaron Lu struct rb_node *rb = sis->swap_extent_root.rb_node; 22684efaceb1SAaron Lu struct swap_extent *se = rb_entry(rb, struct swap_extent, rb_node); 22691da177e4SLinus Torvalds 22704efaceb1SAaron Lu rb_erase(rb, &sis->swap_extent_root); 22711da177e4SLinus Torvalds kfree(se); 22721da177e4SLinus Torvalds } 227362c230bcSMel Gorman 2274bc4ae27dSOmar Sandoval if (sis->flags & SWP_ACTIVATED) { 227562c230bcSMel Gorman struct file *swap_file = sis->swap_file; 227662c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 227762c230bcSMel Gorman 2278bc4ae27dSOmar Sandoval sis->flags &= ~SWP_ACTIVATED; 2279bc4ae27dSOmar Sandoval if (mapping->a_ops->swap_deactivate) 228062c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 228162c230bcSMel Gorman } 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 22864efaceb1SAaron Lu * extent tree. 22871da177e4SLinus Torvalds * 228811d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 22891da177e4SLinus Torvalds */ 2290a509bc1aSMel Gorman int 22911da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 22921da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 22931da177e4SLinus Torvalds { 22944efaceb1SAaron Lu struct rb_node **link = &sis->swap_extent_root.rb_node, *parent = NULL; 22951da177e4SLinus Torvalds struct swap_extent *se; 22961da177e4SLinus Torvalds struct swap_extent *new_se; 22971da177e4SLinus Torvalds 22984efaceb1SAaron Lu /* 22994efaceb1SAaron Lu * place the new node at the right most since the 23004efaceb1SAaron Lu * function is called in ascending page order. 23014efaceb1SAaron Lu */ 23024efaceb1SAaron Lu while (*link) { 23034efaceb1SAaron Lu parent = *link; 23044efaceb1SAaron Lu link = &parent->rb_right; 23054efaceb1SAaron Lu } 23064efaceb1SAaron Lu 23074efaceb1SAaron Lu if (parent) { 23084efaceb1SAaron Lu se = rb_entry(parent, struct swap_extent, rb_node); 230911d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 231011d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 23111da177e4SLinus Torvalds /* Merge it */ 23121da177e4SLinus Torvalds se->nr_pages += nr_pages; 23131da177e4SLinus Torvalds return 0; 23141da177e4SLinus Torvalds } 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23174efaceb1SAaron Lu /* No merge, insert a new extent. */ 23181da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 23191da177e4SLinus Torvalds if (new_se == NULL) 23201da177e4SLinus Torvalds return -ENOMEM; 23211da177e4SLinus Torvalds new_se->start_page = start_page; 23221da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 23231da177e4SLinus Torvalds new_se->start_block = start_block; 23241da177e4SLinus Torvalds 23254efaceb1SAaron Lu rb_link_node(&new_se->rb_node, parent, link); 23264efaceb1SAaron Lu rb_insert_color(&new_se->rb_node, &sis->swap_extent_root); 232753092a74SHugh Dickins return 1; 23281da177e4SLinus Torvalds } 2329aa8aa8a3SOmar Sandoval EXPORT_SYMBOL_GPL(add_swap_extent); 23301da177e4SLinus Torvalds 23311da177e4SLinus Torvalds /* 23321da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 23331da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 23341da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 23351da177e4SLinus Torvalds * time for locating where on disk a page belongs. 23361da177e4SLinus Torvalds * 23371da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 23381da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 23391da177e4SLinus Torvalds * swap files identically. 23401da177e4SLinus Torvalds * 23411da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 23421da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 23431da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 23441da177e4SLinus Torvalds * 23451da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 23461da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 23471da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 23481da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 23491da177e4SLinus Torvalds * for swapping. 23501da177e4SLinus Torvalds * 23511638045cSDarrick J. Wong * For all swap devices we set S_SWAPFILE across the life of the swapon. This 23521638045cSDarrick J. Wong * prevents users from writing to the swap device, which will corrupt memory. 23531da177e4SLinus Torvalds * 23541da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 23551da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 23561da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 23571da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 23581da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 23591da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 23601da177e4SLinus Torvalds */ 236153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 23621da177e4SLinus Torvalds { 236362c230bcSMel Gorman struct file *swap_file = sis->swap_file; 236462c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 236562c230bcSMel Gorman struct inode *inode = mapping->host; 23661da177e4SLinus Torvalds int ret; 23671da177e4SLinus Torvalds 23681da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 23691da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 237053092a74SHugh Dickins *span = sis->pages; 2371a509bc1aSMel Gorman return ret; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 237462c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 2375a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 2376bc4ae27dSOmar Sandoval if (ret >= 0) 2377bc4ae27dSOmar Sandoval sis->flags |= SWP_ACTIVATED; 237862c230bcSMel Gorman if (!ret) { 2379bc4ae27dSOmar Sandoval sis->flags |= SWP_FS; 238062c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 238162c230bcSMel Gorman *span = sis->pages; 238262c230bcSMel Gorman } 23839625a5f2SHugh Dickins return ret; 2384a509bc1aSMel Gorman } 2385a509bc1aSMel Gorman 2386a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds 2389a2468cc9SAaron Lu static int swap_node(struct swap_info_struct *p) 2390a2468cc9SAaron Lu { 2391a2468cc9SAaron Lu struct block_device *bdev; 2392a2468cc9SAaron Lu 2393a2468cc9SAaron Lu if (p->bdev) 2394a2468cc9SAaron Lu bdev = p->bdev; 2395a2468cc9SAaron Lu else 2396a2468cc9SAaron Lu bdev = p->swap_file->f_inode->i_sb->s_bdev; 2397a2468cc9SAaron Lu 2398a2468cc9SAaron Lu return bdev ? bdev->bd_disk->node_id : NUMA_NO_NODE; 2399a2468cc9SAaron Lu } 2400a2468cc9SAaron Lu 2401eb085574SHuang Ying static void setup_swap_info(struct swap_info_struct *p, int prio, 24022a8f9449SShaohua Li unsigned char *swap_map, 24032a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 240440531542SCesar Eduardo Barros { 2405a2468cc9SAaron Lu int i; 2406a2468cc9SAaron Lu 240740531542SCesar Eduardo Barros if (prio >= 0) 240840531542SCesar Eduardo Barros p->prio = prio; 240940531542SCesar Eduardo Barros else 241040531542SCesar Eduardo Barros p->prio = --least_priority; 241118ab4d4cSDan Streetman /* 241218ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 241318ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 241418ab4d4cSDan Streetman */ 241518ab4d4cSDan Streetman p->list.prio = -p->prio; 2416a2468cc9SAaron Lu for_each_node(i) { 2417a2468cc9SAaron Lu if (p->prio >= 0) 2418a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2419a2468cc9SAaron Lu else { 2420a2468cc9SAaron Lu if (swap_node(p) == i) 2421a2468cc9SAaron Lu p->avail_lists[i].prio = 1; 2422a2468cc9SAaron Lu else 2423a2468cc9SAaron Lu p->avail_lists[i].prio = -p->prio; 2424a2468cc9SAaron Lu } 2425a2468cc9SAaron Lu } 242640531542SCesar Eduardo Barros p->swap_map = swap_map; 24272a8f9449SShaohua Li p->cluster_info = cluster_info; 2428eb085574SHuang Ying } 2429eb085574SHuang Ying 2430eb085574SHuang Ying static void _enable_swap_info(struct swap_info_struct *p) 2431eb085574SHuang Ying { 2432eb085574SHuang Ying p->flags |= SWP_WRITEOK | SWP_VALID; 2433ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 243440531542SCesar Eduardo Barros total_swap_pages += p->pages; 243540531542SCesar Eduardo Barros 2436adfab836SDan Streetman assert_spin_locked(&swap_lock); 2437adfab836SDan Streetman /* 243818ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 243918ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 244018ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 244118ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 244218ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 244318ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 244418ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 244518ab4d4cSDan Streetman * swap_info_struct. 2446adfab836SDan Streetman */ 244718ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 2448a2468cc9SAaron Lu add_to_avail_list(p); 2449cf0cac0aSCesar Eduardo Barros } 2450cf0cac0aSCesar Eduardo Barros 2451cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 2452cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 24532a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2454cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 2455cf0cac0aSCesar Eduardo Barros { 24564f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 2457cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2458ec8acf20SShaohua Li spin_lock(&p->lock); 2459eb085574SHuang Ying setup_swap_info(p, prio, swap_map, cluster_info); 2460eb085574SHuang Ying spin_unlock(&p->lock); 2461eb085574SHuang Ying spin_unlock(&swap_lock); 2462eb085574SHuang Ying /* 2463eb085574SHuang Ying * Guarantee swap_map, cluster_info, etc. fields are valid 2464eb085574SHuang Ying * between get/put_swap_device() if SWP_VALID bit is set 2465eb085574SHuang Ying */ 2466eb085574SHuang Ying synchronize_rcu(); 2467eb085574SHuang Ying spin_lock(&swap_lock); 2468eb085574SHuang Ying spin_lock(&p->lock); 2469eb085574SHuang Ying _enable_swap_info(p); 2470ec8acf20SShaohua Li spin_unlock(&p->lock); 2471cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 2472cf0cac0aSCesar Eduardo Barros } 2473cf0cac0aSCesar Eduardo Barros 2474cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 2475cf0cac0aSCesar Eduardo Barros { 2476cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 2477ec8acf20SShaohua Li spin_lock(&p->lock); 2478eb085574SHuang Ying setup_swap_info(p, p->prio, p->swap_map, p->cluster_info); 2479eb085574SHuang Ying _enable_swap_info(p); 2480ec8acf20SShaohua Li spin_unlock(&p->lock); 248140531542SCesar Eduardo Barros spin_unlock(&swap_lock); 248240531542SCesar Eduardo Barros } 248340531542SCesar Eduardo Barros 248467afa38eSTim Chen bool has_usable_swap(void) 248567afa38eSTim Chen { 248667afa38eSTim Chen bool ret = true; 248767afa38eSTim Chen 248867afa38eSTim Chen spin_lock(&swap_lock); 248967afa38eSTim Chen if (plist_head_empty(&swap_active_head)) 249067afa38eSTim Chen ret = false; 249167afa38eSTim Chen spin_unlock(&swap_lock); 249267afa38eSTim Chen return ret; 249367afa38eSTim Chen } 249467afa38eSTim Chen 2495c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 24961da177e4SLinus Torvalds { 24971da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 24988d69aaeeSHugh Dickins unsigned char *swap_map; 24992a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 25004f89849dSMinchan Kim unsigned long *frontswap_map; 25011da177e4SLinus Torvalds struct file *swap_file, *victim; 25021da177e4SLinus Torvalds struct address_space *mapping; 25031da177e4SLinus Torvalds struct inode *inode; 250491a27b2aSJeff Layton struct filename *pathname; 2505adfab836SDan Streetman int err, found = 0; 25065b808a23SKrzysztof Kozlowski unsigned int old_block_size; 25071da177e4SLinus Torvalds 25081da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 25091da177e4SLinus Torvalds return -EPERM; 25101da177e4SLinus Torvalds 2511191c5424SAl Viro BUG_ON(!current->mm); 2512191c5424SAl Viro 25131da177e4SLinus Torvalds pathname = getname(specialfile); 25141da177e4SLinus Torvalds if (IS_ERR(pathname)) 2515f58b59c1SXiaotian Feng return PTR_ERR(pathname); 25161da177e4SLinus Torvalds 2517669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 25181da177e4SLinus Torvalds err = PTR_ERR(victim); 25191da177e4SLinus Torvalds if (IS_ERR(victim)) 25201da177e4SLinus Torvalds goto out; 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds mapping = victim->f_mapping; 25235d337b91SHugh Dickins spin_lock(&swap_lock); 252418ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 252522c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 2526adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 2527adfab836SDan Streetman found = 1; 25281da177e4SLinus Torvalds break; 25291da177e4SLinus Torvalds } 25301da177e4SLinus Torvalds } 2531adfab836SDan Streetman } 2532adfab836SDan Streetman if (!found) { 25331da177e4SLinus Torvalds err = -EINVAL; 25345d337b91SHugh Dickins spin_unlock(&swap_lock); 25351da177e4SLinus Torvalds goto out_dput; 25361da177e4SLinus Torvalds } 2537191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 25381da177e4SLinus Torvalds vm_unacct_memory(p->pages); 25391da177e4SLinus Torvalds else { 25401da177e4SLinus Torvalds err = -ENOMEM; 25415d337b91SHugh Dickins spin_unlock(&swap_lock); 25421da177e4SLinus Torvalds goto out_dput; 25431da177e4SLinus Torvalds } 2544a2468cc9SAaron Lu del_from_avail_list(p); 2545ec8acf20SShaohua Li spin_lock(&p->lock); 254678ecba08SHugh Dickins if (p->prio < 0) { 2547adfab836SDan Streetman struct swap_info_struct *si = p; 2548a2468cc9SAaron Lu int nid; 2549adfab836SDan Streetman 255018ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 2551adfab836SDan Streetman si->prio++; 255218ab4d4cSDan Streetman si->list.prio--; 2553a2468cc9SAaron Lu for_each_node(nid) { 2554a2468cc9SAaron Lu if (si->avail_lists[nid].prio != 1) 2555a2468cc9SAaron Lu si->avail_lists[nid].prio--; 2556a2468cc9SAaron Lu } 2557adfab836SDan Streetman } 255878ecba08SHugh Dickins least_priority++; 255978ecba08SHugh Dickins } 256018ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 2561ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 25621da177e4SLinus Torvalds total_swap_pages -= p->pages; 25631da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 2564ec8acf20SShaohua Li spin_unlock(&p->lock); 25655d337b91SHugh Dickins spin_unlock(&swap_lock); 2566fb4f88dcSHugh Dickins 2567039939a6STim Chen disable_swap_slots_cache_lock(); 2568039939a6STim Chen 2569e1e12d2fSDavid Rientjes set_current_oom_origin(); 2570adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 2571e1e12d2fSDavid Rientjes clear_current_oom_origin(); 25721da177e4SLinus Torvalds 25731da177e4SLinus Torvalds if (err) { 25741da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 2575cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 2576039939a6STim Chen reenable_swap_slots_cache_unlock(); 25771da177e4SLinus Torvalds goto out_dput; 25781da177e4SLinus Torvalds } 257952b7efdbSHugh Dickins 2580039939a6STim Chen reenable_swap_slots_cache_unlock(); 2581039939a6STim Chen 2582eb085574SHuang Ying spin_lock(&swap_lock); 2583eb085574SHuang Ying spin_lock(&p->lock); 2584eb085574SHuang Ying p->flags &= ~SWP_VALID; /* mark swap device as invalid */ 2585eb085574SHuang Ying spin_unlock(&p->lock); 2586eb085574SHuang Ying spin_unlock(&swap_lock); 2587eb085574SHuang Ying /* 2588eb085574SHuang Ying * wait for swap operations protected by get/put_swap_device() 2589eb085574SHuang Ying * to complete 2590eb085574SHuang Ying */ 2591eb085574SHuang Ying synchronize_rcu(); 2592eb085574SHuang Ying 2593815c2c54SShaohua Li flush_work(&p->discard_work); 2594815c2c54SShaohua Li 25954cd3bb10SHugh Dickins destroy_swap_extents(p); 2596570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 2597570a335bSHugh Dickins free_swap_count_continuations(p); 2598570a335bSHugh Dickins 259981a0298bSHuang Ying if (!p->bdev || !blk_queue_nonrot(bdev_get_queue(p->bdev))) 260081a0298bSHuang Ying atomic_dec(&nr_rotate_swap); 260181a0298bSHuang Ying 2602fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 26035d337b91SHugh Dickins spin_lock(&swap_lock); 2604ec8acf20SShaohua Li spin_lock(&p->lock); 26051da177e4SLinus Torvalds drain_mmlist(); 26065d337b91SHugh Dickins 26075d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 26085d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 26095d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 2610ec8acf20SShaohua Li spin_unlock(&p->lock); 26115d337b91SHugh Dickins spin_unlock(&swap_lock); 261213e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 26135d337b91SHugh Dickins spin_lock(&swap_lock); 2614ec8acf20SShaohua Li spin_lock(&p->lock); 26155d337b91SHugh Dickins } 26165d337b91SHugh Dickins 26171da177e4SLinus Torvalds swap_file = p->swap_file; 26185b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 26191da177e4SLinus Torvalds p->swap_file = NULL; 26201da177e4SLinus Torvalds p->max = 0; 26211da177e4SLinus Torvalds swap_map = p->swap_map; 26221da177e4SLinus Torvalds p->swap_map = NULL; 26232a8f9449SShaohua Li cluster_info = p->cluster_info; 26242a8f9449SShaohua Li p->cluster_info = NULL; 26254f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 2626ec8acf20SShaohua Li spin_unlock(&p->lock); 26275d337b91SHugh Dickins spin_unlock(&swap_lock); 2628adfab836SDan Streetman frontswap_invalidate_area(p->type); 262958e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 2630fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 2631ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2632ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 26331da177e4SLinus Torvalds vfree(swap_map); 263454f180d3SHuang Ying kvfree(cluster_info); 263554f180d3SHuang Ying kvfree(frontswap_map); 26362de1a7e4SSeth Jennings /* Destroy swap account information */ 2637adfab836SDan Streetman swap_cgroup_swapoff(p->type); 26384b3ef9daSHuang, Ying exit_swap_address_space(p->type); 263927a7faa0SKAMEZAWA Hiroyuki 26401da177e4SLinus Torvalds inode = mapping->host; 26411da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 26421da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 26431638045cSDarrick J. Wong 26445b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 2645e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 26461638045cSDarrick J. Wong } 26471638045cSDarrick J. Wong 26485955102cSAl Viro inode_lock(inode); 26491da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 26505955102cSAl Viro inode_unlock(inode); 26511da177e4SLinus Torvalds filp_close(swap_file, NULL); 2652f893ab41SWeijie Yang 2653f893ab41SWeijie Yang /* 2654f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 2655f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 2656f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 2657f893ab41SWeijie Yang */ 2658f893ab41SWeijie Yang spin_lock(&swap_lock); 2659f893ab41SWeijie Yang p->flags = 0; 2660f893ab41SWeijie Yang spin_unlock(&swap_lock); 2661f893ab41SWeijie Yang 26621da177e4SLinus Torvalds err = 0; 266366d7dd51SKay Sievers atomic_inc(&proc_poll_event); 266466d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 26651da177e4SLinus Torvalds 26661da177e4SLinus Torvalds out_dput: 26671da177e4SLinus Torvalds filp_close(victim, NULL); 26681da177e4SLinus Torvalds out: 2669f58b59c1SXiaotian Feng putname(pathname); 26701da177e4SLinus Torvalds return err; 26711da177e4SLinus Torvalds } 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 26749dd95748SAl Viro static __poll_t swaps_poll(struct file *file, poll_table *wait) 267566d7dd51SKay Sievers { 2676f1514638SKay Sievers struct seq_file *seq = file->private_data; 267766d7dd51SKay Sievers 267866d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 267966d7dd51SKay Sievers 2680f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 2681f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2682a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM | EPOLLERR | EPOLLPRI; 268366d7dd51SKay Sievers } 268466d7dd51SKay Sievers 2685a9a08845SLinus Torvalds return EPOLLIN | EPOLLRDNORM; 268666d7dd51SKay Sievers } 268766d7dd51SKay Sievers 26881da177e4SLinus Torvalds /* iterator */ 26891da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 26901da177e4SLinus Torvalds { 2691efa90a98SHugh Dickins struct swap_info_struct *si; 2692efa90a98SHugh Dickins int type; 26931da177e4SLinus Torvalds loff_t l = *pos; 26941da177e4SLinus Torvalds 2695fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 26961da177e4SLinus Torvalds 2697881e4aabSSuleiman Souhlal if (!l) 2698881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 2699881e4aabSSuleiman Souhlal 2700c10d38ccSDaniel Jordan for (type = 0; (si = swap_type_to_swap_info(type)); type++) { 2701efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27021da177e4SLinus Torvalds continue; 2703881e4aabSSuleiman Souhlal if (!--l) 2704efa90a98SHugh Dickins return si; 27051da177e4SLinus Torvalds } 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds return NULL; 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 27111da177e4SLinus Torvalds { 2712efa90a98SHugh Dickins struct swap_info_struct *si = v; 2713efa90a98SHugh Dickins int type; 27141da177e4SLinus Torvalds 2715881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2716efa90a98SHugh Dickins type = 0; 2717efa90a98SHugh Dickins else 2718efa90a98SHugh Dickins type = si->type + 1; 2719881e4aabSSuleiman Souhlal 272010c8d69fSVasily Averin ++(*pos); 2721c10d38ccSDaniel Jordan for (; (si = swap_type_to_swap_info(type)); type++) { 2722efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 27231da177e4SLinus Torvalds continue; 2724efa90a98SHugh Dickins return si; 27251da177e4SLinus Torvalds } 27261da177e4SLinus Torvalds 27271da177e4SLinus Torvalds return NULL; 27281da177e4SLinus Torvalds } 27291da177e4SLinus Torvalds 27301da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 27311da177e4SLinus Torvalds { 2732fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 27361da177e4SLinus Torvalds { 2737efa90a98SHugh Dickins struct swap_info_struct *si = v; 27381da177e4SLinus Torvalds struct file *file; 27391da177e4SLinus Torvalds int len; 27401da177e4SLinus Torvalds 2741efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 27421da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2743881e4aabSSuleiman Souhlal return 0; 2744881e4aabSSuleiman Souhlal } 27451da177e4SLinus Torvalds 2746efa90a98SHugh Dickins file = si->swap_file; 27472726d566SMiklos Szeredi len = seq_file_path(swap, file, " \t\n\\"); 27486eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 27491da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2750496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 27511da177e4SLinus Torvalds "partition" : "file\t", 2752efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2753efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2754efa90a98SHugh Dickins si->prio); 27551da177e4SLinus Torvalds return 0; 27561da177e4SLinus Torvalds } 27571da177e4SLinus Torvalds 275815ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 27591da177e4SLinus Torvalds .start = swap_start, 27601da177e4SLinus Torvalds .next = swap_next, 27611da177e4SLinus Torvalds .stop = swap_stop, 27621da177e4SLinus Torvalds .show = swap_show 27631da177e4SLinus Torvalds }; 27641da177e4SLinus Torvalds 27651da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 27661da177e4SLinus Torvalds { 2767f1514638SKay Sievers struct seq_file *seq; 276866d7dd51SKay Sievers int ret; 276966d7dd51SKay Sievers 277066d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2771f1514638SKay Sievers if (ret) 277266d7dd51SKay Sievers return ret; 277366d7dd51SKay Sievers 2774f1514638SKay Sievers seq = file->private_data; 2775f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2776f1514638SKay Sievers return 0; 27771da177e4SLinus Torvalds } 27781da177e4SLinus Torvalds 277997a32539SAlexey Dobriyan static const struct proc_ops swaps_proc_ops = { 2780d919b33dSAlexey Dobriyan .proc_flags = PROC_ENTRY_PERMANENT, 278197a32539SAlexey Dobriyan .proc_open = swaps_open, 278297a32539SAlexey Dobriyan .proc_read = seq_read, 278397a32539SAlexey Dobriyan .proc_lseek = seq_lseek, 278497a32539SAlexey Dobriyan .proc_release = seq_release, 278597a32539SAlexey Dobriyan .proc_poll = swaps_poll, 27861da177e4SLinus Torvalds }; 27871da177e4SLinus Torvalds 27881da177e4SLinus Torvalds static int __init procswaps_init(void) 27891da177e4SLinus Torvalds { 279097a32539SAlexey Dobriyan proc_create("swaps", 0, NULL, &swaps_proc_ops); 27911da177e4SLinus Torvalds return 0; 27921da177e4SLinus Torvalds } 27931da177e4SLinus Torvalds __initcall(procswaps_init); 27941da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 27951da177e4SLinus Torvalds 27961796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 27971796316aSJan Beulich static int __init max_swapfiles_check(void) 27981796316aSJan Beulich { 27991796316aSJan Beulich MAX_SWAPFILES_CHECK(); 28001796316aSJan Beulich return 0; 28011796316aSJan Beulich } 28021796316aSJan Beulich late_initcall(max_swapfiles_check); 28031796316aSJan Beulich #endif 28041796316aSJan Beulich 280553cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds struct swap_info_struct *p; 28081da177e4SLinus Torvalds unsigned int type; 2809a2468cc9SAaron Lu int i; 2810efa90a98SHugh Dickins 281196008744SGustavo A. R. Silva p = kvzalloc(struct_size(p, avail_lists, nr_node_ids), GFP_KERNEL); 2812efa90a98SHugh Dickins if (!p) 281353cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2814efa90a98SHugh Dickins 28155d337b91SHugh Dickins spin_lock(&swap_lock); 2816efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2817efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 28181da177e4SLinus Torvalds break; 2819efa90a98SHugh Dickins } 28200697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 28215d337b91SHugh Dickins spin_unlock(&swap_lock); 2822873d7bcfSVasily Averin kvfree(p); 2823730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 28241da177e4SLinus Torvalds } 2825efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2826efa90a98SHugh Dickins p->type = type; 2827c10d38ccSDaniel Jordan WRITE_ONCE(swap_info[type], p); 2828efa90a98SHugh Dickins /* 2829efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2830efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2831efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2832efa90a98SHugh Dickins */ 2833efa90a98SHugh Dickins smp_wmb(); 2834c10d38ccSDaniel Jordan WRITE_ONCE(nr_swapfiles, nr_swapfiles + 1); 2835efa90a98SHugh Dickins } else { 2836873d7bcfSVasily Averin kvfree(p); 2837efa90a98SHugh Dickins p = swap_info[type]; 2838efa90a98SHugh Dickins /* 2839efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2840efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2841efa90a98SHugh Dickins */ 2842efa90a98SHugh Dickins } 28434efaceb1SAaron Lu p->swap_extent_root = RB_ROOT; 284418ab4d4cSDan Streetman plist_node_init(&p->list, 0); 2845a2468cc9SAaron Lu for_each_node(i) 2846a2468cc9SAaron Lu plist_node_init(&p->avail_lists[i], 0); 28471da177e4SLinus Torvalds p->flags = SWP_USED; 28485d337b91SHugh Dickins spin_unlock(&swap_lock); 2849ec8acf20SShaohua Li spin_lock_init(&p->lock); 28502628bd6fSHuang Ying spin_lock_init(&p->cont_lock); 2851efa90a98SHugh Dickins 285253cbb243SCesar Eduardo Barros return p; 285353cbb243SCesar Eduardo Barros } 285453cbb243SCesar Eduardo Barros 28554d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 28564d0e1e10SCesar Eduardo Barros { 28574d0e1e10SCesar Eduardo Barros int error; 28584d0e1e10SCesar Eduardo Barros 28594d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 28604d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 28614d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 28626f179af8SHugh Dickins FMODE_READ | FMODE_WRITE | FMODE_EXCL, p); 28634d0e1e10SCesar Eduardo Barros if (error < 0) { 28644d0e1e10SCesar Eduardo Barros p->bdev = NULL; 28656f179af8SHugh Dickins return error; 28664d0e1e10SCesar Eduardo Barros } 28674d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 28684d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 28694d0e1e10SCesar Eduardo Barros if (error < 0) 287087ade72aSCesar Eduardo Barros return error; 287112d2966dSNaohiro Aota /* 287212d2966dSNaohiro Aota * Zoned block devices contain zones that have a sequential 287312d2966dSNaohiro Aota * write only restriction. Hence zoned block devices are not 287412d2966dSNaohiro Aota * suitable for swapping. Disallow them here. 287512d2966dSNaohiro Aota */ 287612d2966dSNaohiro Aota if (blk_queue_is_zoned(p->bdev->bd_queue)) 287712d2966dSNaohiro Aota return -EINVAL; 28784d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 28794d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 28804d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 28811638045cSDarrick J. Wong } 28821638045cSDarrick J. Wong 28834d0e1e10SCesar Eduardo Barros return 0; 28844d0e1e10SCesar Eduardo Barros } 28854d0e1e10SCesar Eduardo Barros 2886377eeaa8SAndi Kleen 2887377eeaa8SAndi Kleen /* 2888377eeaa8SAndi Kleen * Find out how many pages are allowed for a single swap device. There 2889377eeaa8SAndi Kleen * are two limiting factors: 2890377eeaa8SAndi Kleen * 1) the number of bits for the swap offset in the swp_entry_t type, and 2891377eeaa8SAndi Kleen * 2) the number of bits in the swap pte, as defined by the different 2892377eeaa8SAndi Kleen * architectures. 2893377eeaa8SAndi Kleen * 2894377eeaa8SAndi Kleen * In order to find the largest possible bit mask, a swap entry with 2895377eeaa8SAndi Kleen * swap type 0 and swap offset ~0UL is created, encoded to a swap pte, 2896377eeaa8SAndi Kleen * decoded to a swp_entry_t again, and finally the swap offset is 2897377eeaa8SAndi Kleen * extracted. 2898377eeaa8SAndi Kleen * 2899377eeaa8SAndi Kleen * This will mask all the bits from the initial ~0UL mask that can't 2900377eeaa8SAndi Kleen * be encoded in either the swp_entry_t or the architecture definition 2901377eeaa8SAndi Kleen * of a swap pte. 2902377eeaa8SAndi Kleen */ 2903377eeaa8SAndi Kleen unsigned long generic_max_swapfile_size(void) 2904377eeaa8SAndi Kleen { 2905377eeaa8SAndi Kleen return swp_offset(pte_to_swp_entry( 2906377eeaa8SAndi Kleen swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2907377eeaa8SAndi Kleen } 2908377eeaa8SAndi Kleen 2909377eeaa8SAndi Kleen /* Can be overridden by an architecture for additional checks. */ 2910377eeaa8SAndi Kleen __weak unsigned long max_swapfile_size(void) 2911377eeaa8SAndi Kleen { 2912377eeaa8SAndi Kleen return generic_max_swapfile_size(); 2913377eeaa8SAndi Kleen } 2914377eeaa8SAndi Kleen 2915ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2916ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2917ca8bd38bSCesar Eduardo Barros struct inode *inode) 2918ca8bd38bSCesar Eduardo Barros { 2919ca8bd38bSCesar Eduardo Barros int i; 2920ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2921ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2922d6bbbd29SRaymond Jennings unsigned long last_page; 2923ca8bd38bSCesar Eduardo Barros 2924ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2925465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 292638719025SCesar Eduardo Barros return 0; 2927ca8bd38bSCesar Eduardo Barros } 2928ca8bd38bSCesar Eduardo Barros 2929ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2930ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2931ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2932ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2933ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2934dd111be6SJann Horn if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 2935dd111be6SJann Horn return 0; 2936ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2937ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2938ca8bd38bSCesar Eduardo Barros } 2939ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2940ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2941465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2942ca8bd38bSCesar Eduardo Barros swap_header->info.version); 294338719025SCesar Eduardo Barros return 0; 2944ca8bd38bSCesar Eduardo Barros } 2945ca8bd38bSCesar Eduardo Barros 2946ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2947ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2948ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2949ca8bd38bSCesar Eduardo Barros 2950377eeaa8SAndi Kleen maxpages = max_swapfile_size(); 2951d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2952a06ad633STom Abraham if (!last_page) { 2953a06ad633STom Abraham pr_warn("Empty swap-file\n"); 2954a06ad633STom Abraham return 0; 2955a06ad633STom Abraham } 2956d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2957465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2958d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2959d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2960d6bbbd29SRaymond Jennings } 2961d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2962d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2963ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2964ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2965ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2966ca8bd38bSCesar Eduardo Barros } 2967ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2968ca8bd38bSCesar Eduardo Barros 2969ca8bd38bSCesar Eduardo Barros if (!maxpages) 297038719025SCesar Eduardo Barros return 0; 2971ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2972ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2973465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 297438719025SCesar Eduardo Barros return 0; 2975ca8bd38bSCesar Eduardo Barros } 2976ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 297738719025SCesar Eduardo Barros return 0; 2978ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 297938719025SCesar Eduardo Barros return 0; 2980ca8bd38bSCesar Eduardo Barros 2981ca8bd38bSCesar Eduardo Barros return maxpages; 2982ca8bd38bSCesar Eduardo Barros } 2983ca8bd38bSCesar Eduardo Barros 29844b3ef9daSHuang, Ying #define SWAP_CLUSTER_INFO_COLS \ 2985235b6217SHuang, Ying DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) 29864b3ef9daSHuang, Ying #define SWAP_CLUSTER_SPACE_COLS \ 29874b3ef9daSHuang, Ying DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) 29884b3ef9daSHuang, Ying #define SWAP_CLUSTER_COLS \ 29894b3ef9daSHuang, Ying max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) 2990235b6217SHuang, Ying 2991915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2992915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2993915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 29942a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2995915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2996915d4d7bSCesar Eduardo Barros sector_t *span) 2997915d4d7bSCesar Eduardo Barros { 2998235b6217SHuang, Ying unsigned int j, k; 2999915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 3000915d4d7bSCesar Eduardo Barros int nr_extents; 30012a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 3002235b6217SHuang, Ying unsigned long col = p->cluster_next / SWAPFILE_CLUSTER % SWAP_CLUSTER_COLS; 3003235b6217SHuang, Ying unsigned long i, idx; 3004915d4d7bSCesar Eduardo Barros 3005915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 3006915d4d7bSCesar Eduardo Barros 30076b534915SHuang Ying cluster_list_init(&p->free_clusters); 30086b534915SHuang Ying cluster_list_init(&p->discard_clusters); 30092a8f9449SShaohua Li 3010915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 3011915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 3012bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 3013bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3014915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 3015915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 3016915d4d7bSCesar Eduardo Barros nr_good_pages--; 30172a8f9449SShaohua Li /* 30182a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 30192a8f9449SShaohua Li * operation involved 30202a8f9449SShaohua Li */ 30212a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 3022915d4d7bSCesar Eduardo Barros } 3023915d4d7bSCesar Eduardo Barros } 3024915d4d7bSCesar Eduardo Barros 30252a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 30262a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 30272a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 30282a8f9449SShaohua Li 3029915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 3030915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 30312a8f9449SShaohua Li /* 30322a8f9449SShaohua Li * Not mark the cluster free yet, no list 30332a8f9449SShaohua Li * operation involved 30342a8f9449SShaohua Li */ 30352a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 3036915d4d7bSCesar Eduardo Barros p->max = maxpages; 3037915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 3038915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 3039bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 3040bdb8e3f6SCesar Eduardo Barros return nr_extents; 3041915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 3042915d4d7bSCesar Eduardo Barros } 3043915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 3044465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 3045bdb8e3f6SCesar Eduardo Barros return -EINVAL; 3046915d4d7bSCesar Eduardo Barros } 3047915d4d7bSCesar Eduardo Barros 30482a8f9449SShaohua Li if (!cluster_info) 30492a8f9449SShaohua Li return nr_extents; 30502a8f9449SShaohua Li 3051235b6217SHuang, Ying 30524b3ef9daSHuang, Ying /* 30534b3ef9daSHuang, Ying * Reduce false cache line sharing between cluster_info and 30544b3ef9daSHuang, Ying * sharing same address space. 30554b3ef9daSHuang, Ying */ 3056235b6217SHuang, Ying for (k = 0; k < SWAP_CLUSTER_COLS; k++) { 3057235b6217SHuang, Ying j = (k + col) % SWAP_CLUSTER_COLS; 3058235b6217SHuang, Ying for (i = 0; i < DIV_ROUND_UP(nr_clusters, SWAP_CLUSTER_COLS); i++) { 3059235b6217SHuang, Ying idx = i * SWAP_CLUSTER_COLS + j; 3060235b6217SHuang, Ying if (idx >= nr_clusters) 3061235b6217SHuang, Ying continue; 3062235b6217SHuang, Ying if (cluster_count(&cluster_info[idx])) 3063235b6217SHuang, Ying continue; 30642a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 30656b534915SHuang Ying cluster_list_add_tail(&p->free_clusters, cluster_info, 30666b534915SHuang Ying idx); 30672a8f9449SShaohua Li } 30682a8f9449SShaohua Li } 3069915d4d7bSCesar Eduardo Barros return nr_extents; 3070915d4d7bSCesar Eduardo Barros } 3071915d4d7bSCesar Eduardo Barros 3072dcf6b7ddSRafael Aquini /* 3073dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 3074dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 3075dcf6b7ddSRafael Aquini */ 3076dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 3077dcf6b7ddSRafael Aquini { 3078dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 3079dcf6b7ddSRafael Aquini 3080dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 3081dcf6b7ddSRafael Aquini return false; 3082dcf6b7ddSRafael Aquini 3083dcf6b7ddSRafael Aquini return true; 3084dcf6b7ddSRafael Aquini } 3085dcf6b7ddSRafael Aquini 308653cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 308753cbb243SCesar Eduardo Barros { 308853cbb243SCesar Eduardo Barros struct swap_info_struct *p; 308991a27b2aSJeff Layton struct filename *name; 309053cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 309153cbb243SCesar Eduardo Barros struct address_space *mapping; 309240531542SCesar Eduardo Barros int prio; 309353cbb243SCesar Eduardo Barros int error; 309453cbb243SCesar Eduardo Barros union swap_header *swap_header; 3095915d4d7bSCesar Eduardo Barros int nr_extents; 309653cbb243SCesar Eduardo Barros sector_t span; 309753cbb243SCesar Eduardo Barros unsigned long maxpages; 309853cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 30992a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 310038b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 310153cbb243SCesar Eduardo Barros struct page *page = NULL; 310253cbb243SCesar Eduardo Barros struct inode *inode = NULL; 31037cbf3192SOmar Sandoval bool inced_nr_rotate_swap = false; 310453cbb243SCesar Eduardo Barros 3105d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 3106d15cab97SHugh Dickins return -EINVAL; 3107d15cab97SHugh Dickins 310853cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 310953cbb243SCesar Eduardo Barros return -EPERM; 311053cbb243SCesar Eduardo Barros 3111a2468cc9SAaron Lu if (!swap_avail_heads) 3112a2468cc9SAaron Lu return -ENOMEM; 3113a2468cc9SAaron Lu 311453cbb243SCesar Eduardo Barros p = alloc_swap_info(); 31152542e513SCesar Eduardo Barros if (IS_ERR(p)) 31162542e513SCesar Eduardo Barros return PTR_ERR(p); 311753cbb243SCesar Eduardo Barros 3118815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 3119815c2c54SShaohua Li 31201da177e4SLinus Torvalds name = getname(specialfile); 31211da177e4SLinus Torvalds if (IS_ERR(name)) { 31227de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 31231da177e4SLinus Torvalds name = NULL; 3124bd69010bSCesar Eduardo Barros goto bad_swap; 31251da177e4SLinus Torvalds } 3126669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 31271da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 31287de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 31291da177e4SLinus Torvalds swap_file = NULL; 3130bd69010bSCesar Eduardo Barros goto bad_swap; 31311da177e4SLinus Torvalds } 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds p->swap_file = swap_file; 31341da177e4SLinus Torvalds mapping = swap_file->f_mapping; 31352130781eSCesar Eduardo Barros inode = mapping->host; 31366f179af8SHugh Dickins 31374d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 31384d0e1e10SCesar Eduardo Barros if (unlikely(error)) 31391da177e4SLinus Torvalds goto bad_swap; 31401da177e4SLinus Torvalds 3141d795a90eSNaohiro Aota inode_lock(inode); 3142d795a90eSNaohiro Aota if (IS_SWAPFILE(inode)) { 3143d795a90eSNaohiro Aota error = -EBUSY; 3144d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 3145d795a90eSNaohiro Aota } 3146d795a90eSNaohiro Aota 31471da177e4SLinus Torvalds /* 31481da177e4SLinus Torvalds * Read the swap header. 31491da177e4SLinus Torvalds */ 31501da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 31511da177e4SLinus Torvalds error = -EINVAL; 3152d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31531da177e4SLinus Torvalds } 3154090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 31551da177e4SLinus Torvalds if (IS_ERR(page)) { 31561da177e4SLinus Torvalds error = PTR_ERR(page); 3157d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31581da177e4SLinus Torvalds } 315981e33971SHugh Dickins swap_header = kmap(page); 31601da177e4SLinus Torvalds 3161ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 3162ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 31631da177e4SLinus Torvalds error = -EINVAL; 3164d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31651da177e4SLinus Torvalds } 31661da177e4SLinus Torvalds 31671da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 3168803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 316978ecba08SHugh Dickins if (!swap_map) { 31701da177e4SLinus Torvalds error = -ENOMEM; 3171d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31721da177e4SLinus Torvalds } 3173f0571429SMinchan Kim 3174f0571429SMinchan Kim if (bdi_cap_stable_pages_required(inode_to_bdi(inode))) 3175f0571429SMinchan Kim p->flags |= SWP_STABLE_WRITES; 3176f0571429SMinchan Kim 3177539a6feaSMinchan Kim if (bdi_cap_synchronous_io(inode_to_bdi(inode))) 3178539a6feaSMinchan Kim p->flags |= SWP_SYNCHRONOUS_IO; 3179539a6feaSMinchan Kim 31802a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 31816f179af8SHugh Dickins int cpu; 3182235b6217SHuang, Ying unsigned long ci, nr_cluster; 31836f179af8SHugh Dickins 31842a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 31852a8f9449SShaohua Li /* 31862a8f9449SShaohua Li * select a random position to start with to help wear leveling 31872a8f9449SShaohua Li * SSD 31882a8f9449SShaohua Li */ 31892a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 3190235b6217SHuang, Ying nr_cluster = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 31912a8f9449SShaohua Li 3192778e1cddSKees Cook cluster_info = kvcalloc(nr_cluster, sizeof(*cluster_info), 319354f180d3SHuang Ying GFP_KERNEL); 31942a8f9449SShaohua Li if (!cluster_info) { 31952a8f9449SShaohua Li error = -ENOMEM; 3196d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 31972a8f9449SShaohua Li } 3198235b6217SHuang, Ying 3199235b6217SHuang, Ying for (ci = 0; ci < nr_cluster; ci++) 3200235b6217SHuang, Ying spin_lock_init(&((cluster_info + ci)->lock)); 3201235b6217SHuang, Ying 3202ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 3203ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 3204ebc2a1a6SShaohua Li error = -ENOMEM; 3205d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 3206ebc2a1a6SShaohua Li } 32076f179af8SHugh Dickins for_each_possible_cpu(cpu) { 3208ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 32096f179af8SHugh Dickins cluster = per_cpu_ptr(p->percpu_cluster, cpu); 3210ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 3211ebc2a1a6SShaohua Li } 32127cbf3192SOmar Sandoval } else { 321381a0298bSHuang Ying atomic_inc(&nr_rotate_swap); 32147cbf3192SOmar Sandoval inced_nr_rotate_swap = true; 32157cbf3192SOmar Sandoval } 32161da177e4SLinus Torvalds 32171421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 32181421ef3cSCesar Eduardo Barros if (error) 3219d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32201421ef3cSCesar Eduardo Barros 3221915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 32222a8f9449SShaohua Li cluster_info, maxpages, &span); 3223915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 322453092a74SHugh Dickins error = nr_extents; 3225d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 322653092a74SHugh Dickins } 322738b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 32288ea1d2a1SVlastimil Babka if (IS_ENABLED(CONFIG_FRONTSWAP)) 3229778e1cddSKees Cook frontswap_map = kvcalloc(BITS_TO_LONGS(maxpages), 3230778e1cddSKees Cook sizeof(long), 323154f180d3SHuang Ying GFP_KERNEL); 32321da177e4SLinus Torvalds 32332a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 3234dcf6b7ddSRafael Aquini /* 3235dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 3236dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 3237dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 3238dcf6b7ddSRafael Aquini * swapon(8) releases. 3239dcf6b7ddSRafael Aquini */ 3240dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 3241dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 3242dcf6b7ddSRafael Aquini 3243dcf6b7ddSRafael Aquini /* 3244dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 3245dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 3246dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 3247dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 3248dcf6b7ddSRafael Aquini */ 3249dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 3250dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 3251dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 3252dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 3253dcf6b7ddSRafael Aquini 3254dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 3255dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 3256dcf6b7ddSRafael Aquini int err = discard_swap(p); 3257dcf6b7ddSRafael Aquini if (unlikely(err)) 3258465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 3259dcf6b7ddSRafael Aquini p, err); 3260dcf6b7ddSRafael Aquini } 3261dcf6b7ddSRafael Aquini } 32626a6ba831SHugh Dickins 32634b3ef9daSHuang, Ying error = init_swap_address_space(p->type, maxpages); 32644b3ef9daSHuang, Ying if (error) 3265d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 32664b3ef9daSHuang, Ying 3267dc617f29SDarrick J. Wong /* 3268dc617f29SDarrick J. Wong * Flush any pending IO and dirty mappings before we start using this 3269dc617f29SDarrick J. Wong * swap device. 3270dc617f29SDarrick J. Wong */ 3271dc617f29SDarrick J. Wong inode->i_flags |= S_SWAPFILE; 3272dc617f29SDarrick J. Wong error = inode_drain_writes(inode); 3273dc617f29SDarrick J. Wong if (error) { 3274dc617f29SDarrick J. Wong inode->i_flags &= ~S_SWAPFILE; 3275d795a90eSNaohiro Aota goto bad_swap_unlock_inode; 3276dc617f29SDarrick J. Wong } 3277dc617f29SDarrick J. Wong 3278fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 327940531542SCesar Eduardo Barros prio = -1; 328078ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 328140531542SCesar Eduardo Barros prio = 328278ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 32832a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 3284c69dbfb8SCesar Eduardo Barros 3285756a025fSJoe Perches pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 328691a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 3287c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 3288c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 328938b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 3290dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 3291dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 329238b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 3293c69dbfb8SCesar Eduardo Barros 3294fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 329566d7dd51SKay Sievers atomic_inc(&proc_poll_event); 329666d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 329766d7dd51SKay Sievers 32981da177e4SLinus Torvalds error = 0; 32991da177e4SLinus Torvalds goto out; 3300d795a90eSNaohiro Aota bad_swap_unlock_inode: 3301d795a90eSNaohiro Aota inode_unlock(inode); 33021da177e4SLinus Torvalds bad_swap: 3303ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 3304ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 3305bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 3306f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 3307f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 33081da177e4SLinus Torvalds } 3309d795a90eSNaohiro Aota inode = NULL; 33104cd3bb10SHugh Dickins destroy_swap_extents(p); 3311e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 33125d337b91SHugh Dickins spin_lock(&swap_lock); 33131da177e4SLinus Torvalds p->swap_file = NULL; 33141da177e4SLinus Torvalds p->flags = 0; 33155d337b91SHugh Dickins spin_unlock(&swap_lock); 33161da177e4SLinus Torvalds vfree(swap_map); 33178606a1a9SDarrick J. Wong kvfree(cluster_info); 3318b6b1fd2aSDavid Rientjes kvfree(frontswap_map); 33197cbf3192SOmar Sandoval if (inced_nr_rotate_swap) 33207cbf3192SOmar Sandoval atomic_dec(&nr_rotate_swap); 3321d795a90eSNaohiro Aota if (swap_file) 33221da177e4SLinus Torvalds filp_close(swap_file, NULL); 33231da177e4SLinus Torvalds out: 33241da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 33251da177e4SLinus Torvalds kunmap(page); 332609cbfeafSKirill A. Shutemov put_page(page); 33271da177e4SLinus Torvalds } 33281da177e4SLinus Torvalds if (name) 33291da177e4SLinus Torvalds putname(name); 33301638045cSDarrick J. Wong if (inode) 33315955102cSAl Viro inode_unlock(inode); 3332039939a6STim Chen if (!error) 3333039939a6STim Chen enable_swap_slots_cache(); 33341da177e4SLinus Torvalds return error; 33351da177e4SLinus Torvalds } 33361da177e4SLinus Torvalds 33371da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 33381da177e4SLinus Torvalds { 3339efa90a98SHugh Dickins unsigned int type; 33401da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 33411da177e4SLinus Torvalds 33425d337b91SHugh Dickins spin_lock(&swap_lock); 3343efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 3344efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 3345efa90a98SHugh Dickins 3346efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 3347efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 33481da177e4SLinus Torvalds } 3349ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 33501da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 33515d337b91SHugh Dickins spin_unlock(&swap_lock); 33521da177e4SLinus Torvalds } 33531da177e4SLinus Torvalds 33541da177e4SLinus Torvalds /* 33551da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 33561da177e4SLinus Torvalds * 3357355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 3358355cfa73SKAMEZAWA Hiroyuki * - success -> 0 3359355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 3360355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 3361355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 3362355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 3363570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 33641da177e4SLinus Torvalds */ 33658d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 33661da177e4SLinus Torvalds { 33671da177e4SLinus Torvalds struct swap_info_struct *p; 3368235b6217SHuang, Ying struct swap_cluster_info *ci; 3369c10d38ccSDaniel Jordan unsigned long offset; 33708d69aaeeSHugh Dickins unsigned char count; 33718d69aaeeSHugh Dickins unsigned char has_cache; 3372253d553bSHugh Dickins int err = -EINVAL; 33731da177e4SLinus Torvalds 3374eb085574SHuang Ying p = get_swap_device(entry); 3375c10d38ccSDaniel Jordan if (!p) 3376eb085574SHuang Ying goto out; 3377c10d38ccSDaniel Jordan 33781da177e4SLinus Torvalds offset = swp_offset(entry); 3379235b6217SHuang, Ying ci = lock_cluster_or_swap_info(p, offset); 3380355cfa73SKAMEZAWA Hiroyuki 3381253d553bSHugh Dickins count = p->swap_map[offset]; 3382edfe23daSShaohua Li 3383edfe23daSShaohua Li /* 3384edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 3385edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 3386edfe23daSShaohua Li */ 3387edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 3388edfe23daSShaohua Li err = -ENOENT; 3389edfe23daSShaohua Li goto unlock_out; 3390edfe23daSShaohua Li } 3391edfe23daSShaohua Li 3392253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 3393253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 3394253d553bSHugh Dickins err = 0; 3395355cfa73SKAMEZAWA Hiroyuki 3396253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 3397355cfa73SKAMEZAWA Hiroyuki 3398355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 3399253d553bSHugh Dickins if (!has_cache && count) 3400253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 3401253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 3402253d553bSHugh Dickins err = -EEXIST; 3403253d553bSHugh Dickins else /* no users remaining */ 3404253d553bSHugh Dickins err = -ENOENT; 3405355cfa73SKAMEZAWA Hiroyuki 3406355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 3407253d553bSHugh Dickins 3408570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 3409570a335bSHugh Dickins count += usage; 3410570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 3411253d553bSHugh Dickins err = -EINVAL; 3412570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 3413570a335bSHugh Dickins count = COUNT_CONTINUED; 3414570a335bSHugh Dickins else 3415570a335bSHugh Dickins err = -ENOMEM; 3416253d553bSHugh Dickins } else 3417253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 3418253d553bSHugh Dickins 3419253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 3420253d553bSHugh Dickins 3421355cfa73SKAMEZAWA Hiroyuki unlock_out: 3422235b6217SHuang, Ying unlock_cluster_or_swap_info(p, ci); 34231da177e4SLinus Torvalds out: 3424eb085574SHuang Ying if (p) 3425eb085574SHuang Ying put_swap_device(p); 3426253d553bSHugh Dickins return err; 34271da177e4SLinus Torvalds } 3428253d553bSHugh Dickins 3429355cfa73SKAMEZAWA Hiroyuki /* 3430aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 3431aaa46865SHugh Dickins * (in which case its reference count is never incremented). 3432aaa46865SHugh Dickins */ 3433aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 3434aaa46865SHugh Dickins { 3435aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 3436aaa46865SHugh Dickins } 3437aaa46865SHugh Dickins 3438aaa46865SHugh Dickins /* 343908259d58SHugh Dickins * Increase reference count of swap entry by 1. 344008259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 344108259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 344208259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 344308259d58SHugh Dickins * might occur if a page table entry has got corrupted. 3444355cfa73SKAMEZAWA Hiroyuki */ 3445570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 3446355cfa73SKAMEZAWA Hiroyuki { 3447570a335bSHugh Dickins int err = 0; 3448570a335bSHugh Dickins 3449570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 3450570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 3451570a335bSHugh Dickins return err; 3452355cfa73SKAMEZAWA Hiroyuki } 34531da177e4SLinus Torvalds 3454cb4b86baSKAMEZAWA Hiroyuki /* 3455355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 3456355cfa73SKAMEZAWA Hiroyuki * 345773c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 3458355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 34593eeba135SChen Wandun * -EEXIST means there is a swap cache. 3460355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 3461cb4b86baSKAMEZAWA Hiroyuki */ 3462cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 3463cb4b86baSKAMEZAWA Hiroyuki { 3464253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 3465cb4b86baSKAMEZAWA Hiroyuki } 3466cb4b86baSKAMEZAWA Hiroyuki 34670bcac06fSMinchan Kim struct swap_info_struct *swp_swap_info(swp_entry_t entry) 34680bcac06fSMinchan Kim { 3469c10d38ccSDaniel Jordan return swap_type_to_swap_info(swp_type(entry)); 34700bcac06fSMinchan Kim } 34710bcac06fSMinchan Kim 3472f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 3473f981c595SMel Gorman { 34740bcac06fSMinchan Kim swp_entry_t entry = { .val = page_private(page) }; 34750bcac06fSMinchan Kim return swp_swap_info(entry); 3476f981c595SMel Gorman } 3477f981c595SMel Gorman 3478f981c595SMel Gorman /* 3479f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 3480f981c595SMel Gorman */ 3481f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 3482f981c595SMel Gorman { 3483f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 3484f981c595SMel Gorman } 3485f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 3486f981c595SMel Gorman 3487f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 3488f981c595SMel Gorman { 3489f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 3490f981c595SMel Gorman return swp_offset(swap); 3491f981c595SMel Gorman } 3492f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 3493f981c595SMel Gorman 34941da177e4SLinus Torvalds /* 3495570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 3496570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 3497570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 3498570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 3499570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 3500570a335bSHugh Dickins * 3501570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 3502570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 3503570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 3504570a335bSHugh Dickins * 3505570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 3506570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 3507570a335bSHugh Dickins * can be called after dropping locks. 3508570a335bSHugh Dickins */ 3509570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 3510570a335bSHugh Dickins { 3511570a335bSHugh Dickins struct swap_info_struct *si; 3512235b6217SHuang, Ying struct swap_cluster_info *ci; 3513570a335bSHugh Dickins struct page *head; 3514570a335bSHugh Dickins struct page *page; 3515570a335bSHugh Dickins struct page *list_page; 3516570a335bSHugh Dickins pgoff_t offset; 3517570a335bSHugh Dickins unsigned char count; 3518eb085574SHuang Ying int ret = 0; 3519570a335bSHugh Dickins 3520570a335bSHugh Dickins /* 3521570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 3522570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 3523570a335bSHugh Dickins */ 3524570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 3525570a335bSHugh Dickins 3526eb085574SHuang Ying si = get_swap_device(entry); 3527570a335bSHugh Dickins if (!si) { 3528570a335bSHugh Dickins /* 3529570a335bSHugh Dickins * An acceptable race has occurred since the failing 3530eb085574SHuang Ying * __swap_duplicate(): the swap device may be swapoff 3531570a335bSHugh Dickins */ 3532570a335bSHugh Dickins goto outer; 3533570a335bSHugh Dickins } 3534eb085574SHuang Ying spin_lock(&si->lock); 3535570a335bSHugh Dickins 3536570a335bSHugh Dickins offset = swp_offset(entry); 3537235b6217SHuang, Ying 3538235b6217SHuang, Ying ci = lock_cluster(si, offset); 3539235b6217SHuang, Ying 3540570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 3541570a335bSHugh Dickins 3542570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 3543570a335bSHugh Dickins /* 3544570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 3545570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 3546570a335bSHugh Dickins * over-provisioning. 3547570a335bSHugh Dickins */ 3548570a335bSHugh Dickins goto out; 3549570a335bSHugh Dickins } 3550570a335bSHugh Dickins 3551570a335bSHugh Dickins if (!page) { 3552eb085574SHuang Ying ret = -ENOMEM; 3553eb085574SHuang Ying goto out; 3554570a335bSHugh Dickins } 3555570a335bSHugh Dickins 3556570a335bSHugh Dickins /* 3557570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 3558570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 3559570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 3560570a335bSHugh Dickins */ 3561570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3562570a335bSHugh Dickins offset &= ~PAGE_MASK; 3563570a335bSHugh Dickins 35642628bd6fSHuang Ying spin_lock(&si->cont_lock); 3565570a335bSHugh Dickins /* 3566570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 3567570a335bSHugh Dickins * but it does always reset its private field. 3568570a335bSHugh Dickins */ 3569570a335bSHugh Dickins if (!page_private(head)) { 3570570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3571570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 3572570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 3573570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 3574570a335bSHugh Dickins } 3575570a335bSHugh Dickins 3576570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 3577570a335bSHugh Dickins unsigned char *map; 3578570a335bSHugh Dickins 3579570a335bSHugh Dickins /* 3580570a335bSHugh Dickins * If the previous map said no continuation, but we've found 3581570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 3582570a335bSHugh Dickins */ 3583570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 35842628bd6fSHuang Ying goto out_unlock_cont; 3585570a335bSHugh Dickins 35869b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 3587570a335bSHugh Dickins count = *map; 35889b04c5feSCong Wang kunmap_atomic(map); 3589570a335bSHugh Dickins 3590570a335bSHugh Dickins /* 3591570a335bSHugh Dickins * If this continuation count now has some space in it, 3592570a335bSHugh Dickins * free our allocation and use this one. 3593570a335bSHugh Dickins */ 3594570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 35952628bd6fSHuang Ying goto out_unlock_cont; 3596570a335bSHugh Dickins } 3597570a335bSHugh Dickins 3598570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 3599570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 36002628bd6fSHuang Ying out_unlock_cont: 36012628bd6fSHuang Ying spin_unlock(&si->cont_lock); 3602570a335bSHugh Dickins out: 3603235b6217SHuang, Ying unlock_cluster(ci); 3604ec8acf20SShaohua Li spin_unlock(&si->lock); 3605eb085574SHuang Ying put_swap_device(si); 3606570a335bSHugh Dickins outer: 3607570a335bSHugh Dickins if (page) 3608570a335bSHugh Dickins __free_page(page); 3609eb085574SHuang Ying return ret; 3610570a335bSHugh Dickins } 3611570a335bSHugh Dickins 3612570a335bSHugh Dickins /* 3613570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 3614570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 3615570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 3616570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 3617570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 3618235b6217SHuang, Ying * Called while __swap_duplicate() or swap_entry_free() holds swap or cluster 3619235b6217SHuang, Ying * lock. 3620570a335bSHugh Dickins */ 3621570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 3622570a335bSHugh Dickins pgoff_t offset, unsigned char count) 3623570a335bSHugh Dickins { 3624570a335bSHugh Dickins struct page *head; 3625570a335bSHugh Dickins struct page *page; 3626570a335bSHugh Dickins unsigned char *map; 36272628bd6fSHuang Ying bool ret; 3628570a335bSHugh Dickins 3629570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3630570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 3631570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 3632570a335bSHugh Dickins return false; /* need to add count continuation */ 3633570a335bSHugh Dickins } 3634570a335bSHugh Dickins 36352628bd6fSHuang Ying spin_lock(&si->cont_lock); 3636570a335bSHugh Dickins offset &= ~PAGE_MASK; 3637213516acSchenqiwu page = list_next_entry(head, lru); 36389b04c5feSCong Wang map = kmap_atomic(page) + offset; 3639570a335bSHugh Dickins 3640570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 3641570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 3642570a335bSHugh Dickins 3643570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 3644570a335bSHugh Dickins /* 3645570a335bSHugh Dickins * Think of how you add 1 to 999 3646570a335bSHugh Dickins */ 3647570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 36489b04c5feSCong Wang kunmap_atomic(map); 3649213516acSchenqiwu page = list_next_entry(page, lru); 3650570a335bSHugh Dickins BUG_ON(page == head); 36519b04c5feSCong Wang map = kmap_atomic(page) + offset; 3652570a335bSHugh Dickins } 3653570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 36549b04c5feSCong Wang kunmap_atomic(map); 3655213516acSchenqiwu page = list_next_entry(page, lru); 36562628bd6fSHuang Ying if (page == head) { 36572628bd6fSHuang Ying ret = false; /* add count continuation */ 36582628bd6fSHuang Ying goto out; 36592628bd6fSHuang Ying } 36609b04c5feSCong Wang map = kmap_atomic(page) + offset; 3661570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 3662570a335bSHugh Dickins } 3663570a335bSHugh Dickins *map += 1; 36649b04c5feSCong Wang kunmap_atomic(map); 3665213516acSchenqiwu while ((page = list_prev_entry(page, lru)) != head) { 36669b04c5feSCong Wang map = kmap_atomic(page) + offset; 3667570a335bSHugh Dickins *map = COUNT_CONTINUED; 36689b04c5feSCong Wang kunmap_atomic(map); 3669570a335bSHugh Dickins } 36702628bd6fSHuang Ying ret = true; /* incremented */ 3671570a335bSHugh Dickins 3672570a335bSHugh Dickins } else { /* decrementing */ 3673570a335bSHugh Dickins /* 3674570a335bSHugh Dickins * Think of how you subtract 1 from 1000 3675570a335bSHugh Dickins */ 3676570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 3677570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 36789b04c5feSCong Wang kunmap_atomic(map); 3679213516acSchenqiwu page = list_next_entry(page, lru); 3680570a335bSHugh Dickins BUG_ON(page == head); 36819b04c5feSCong Wang map = kmap_atomic(page) + offset; 3682570a335bSHugh Dickins } 3683570a335bSHugh Dickins BUG_ON(*map == 0); 3684570a335bSHugh Dickins *map -= 1; 3685570a335bSHugh Dickins if (*map == 0) 3686570a335bSHugh Dickins count = 0; 36879b04c5feSCong Wang kunmap_atomic(map); 3688213516acSchenqiwu while ((page = list_prev_entry(page, lru)) != head) { 36899b04c5feSCong Wang map = kmap_atomic(page) + offset; 3690570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 3691570a335bSHugh Dickins count = COUNT_CONTINUED; 36929b04c5feSCong Wang kunmap_atomic(map); 3693570a335bSHugh Dickins } 36942628bd6fSHuang Ying ret = count == COUNT_CONTINUED; 3695570a335bSHugh Dickins } 36962628bd6fSHuang Ying out: 36972628bd6fSHuang Ying spin_unlock(&si->cont_lock); 36982628bd6fSHuang Ying return ret; 3699570a335bSHugh Dickins } 3700570a335bSHugh Dickins 3701570a335bSHugh Dickins /* 3702570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 3703570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 3704570a335bSHugh Dickins */ 3705570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 3706570a335bSHugh Dickins { 3707570a335bSHugh Dickins pgoff_t offset; 3708570a335bSHugh Dickins 3709570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 3710570a335bSHugh Dickins struct page *head; 3711570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 3712570a335bSHugh Dickins if (page_private(head)) { 37130d576d20SGeliang Tang struct page *page, *next; 37140d576d20SGeliang Tang 37150d576d20SGeliang Tang list_for_each_entry_safe(page, next, &head->lru, lru) { 37160d576d20SGeliang Tang list_del(&page->lru); 3717570a335bSHugh Dickins __free_page(page); 3718570a335bSHugh Dickins } 3719570a335bSHugh Dickins } 3720570a335bSHugh Dickins } 3721570a335bSHugh Dickins } 3722a2468cc9SAaron Lu 37232cf85583STejun Heo #if defined(CONFIG_MEMCG) && defined(CONFIG_BLK_CGROUP) 37242cf85583STejun Heo void mem_cgroup_throttle_swaprate(struct mem_cgroup *memcg, int node, 37252cf85583STejun Heo gfp_t gfp_mask) 37262cf85583STejun Heo { 37272cf85583STejun Heo struct swap_info_struct *si, *next; 37282cf85583STejun Heo if (!(gfp_mask & __GFP_IO) || !memcg) 37292cf85583STejun Heo return; 37302cf85583STejun Heo 37312cf85583STejun Heo if (!blk_cgroup_congested()) 37322cf85583STejun Heo return; 37332cf85583STejun Heo 37342cf85583STejun Heo /* 37352cf85583STejun Heo * We've already scheduled a throttle, avoid taking the global swap 37362cf85583STejun Heo * lock. 37372cf85583STejun Heo */ 37382cf85583STejun Heo if (current->throttle_queue) 37392cf85583STejun Heo return; 37402cf85583STejun Heo 37412cf85583STejun Heo spin_lock(&swap_avail_lock); 37422cf85583STejun Heo plist_for_each_entry_safe(si, next, &swap_avail_heads[node], 37432cf85583STejun Heo avail_lists[node]) { 37442cf85583STejun Heo if (si->bdev) { 37452cf85583STejun Heo blkcg_schedule_throttle(bdev_get_queue(si->bdev), 37462cf85583STejun Heo true); 37472cf85583STejun Heo break; 37482cf85583STejun Heo } 37492cf85583STejun Heo } 37502cf85583STejun Heo spin_unlock(&swap_avail_lock); 37512cf85583STejun Heo } 37522cf85583STejun Heo #endif 37532cf85583STejun Heo 3754a2468cc9SAaron Lu static int __init swapfile_init(void) 3755a2468cc9SAaron Lu { 3756a2468cc9SAaron Lu int nid; 3757a2468cc9SAaron Lu 3758a2468cc9SAaron Lu swap_avail_heads = kmalloc_array(nr_node_ids, sizeof(struct plist_head), 3759a2468cc9SAaron Lu GFP_KERNEL); 3760a2468cc9SAaron Lu if (!swap_avail_heads) { 3761a2468cc9SAaron Lu pr_emerg("Not enough memory for swap heads, swap is disabled\n"); 3762a2468cc9SAaron Lu return -ENOMEM; 3763a2468cc9SAaron Lu } 3764a2468cc9SAaron Lu 3765a2468cc9SAaron Lu for_each_node(nid) 3766a2468cc9SAaron Lu plist_head_init(&swap_avail_heads[nid]); 3767a2468cc9SAaron Lu 3768a2468cc9SAaron Lu return 0; 3769a2468cc9SAaron Lu } 3770a2468cc9SAaron Lu subsys_initcall(swapfile_init); 3771