11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/swapfile.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/mm.h> 91da177e4SLinus Torvalds #include <linux/hugetlb.h> 101da177e4SLinus Torvalds #include <linux/mman.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/kernel_stat.h> 131da177e4SLinus Torvalds #include <linux/swap.h> 141da177e4SLinus Torvalds #include <linux/vmalloc.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 17072441e2SHugh Dickins #include <linux/shmem_fs.h> 181da177e4SLinus Torvalds #include <linux/blkdev.h> 1920137a49SHugh Dickins #include <linux/random.h> 201da177e4SLinus Torvalds #include <linux/writeback.h> 211da177e4SLinus Torvalds #include <linux/proc_fs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 231da177e4SLinus Torvalds #include <linux/init.h> 245ad64688SHugh Dickins #include <linux/ksm.h> 251da177e4SLinus Torvalds #include <linux/rmap.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28fc0abb14SIngo Molnar #include <linux/mutex.h> 29c59ede7bSRandy.Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/syscalls.h> 318a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3266d7dd51SKay Sievers #include <linux/poll.h> 3372788c38SDavid Rientjes #include <linux/oom.h> 3438b5faf4SDan Magenheimer #include <linux/frontswap.h> 3538b5faf4SDan Magenheimer #include <linux/swapfile.h> 36f981c595SMel Gorman #include <linux/export.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/pgtable.h> 391da177e4SLinus Torvalds #include <asm/tlbflush.h> 401da177e4SLinus Torvalds #include <linux/swapops.h> 4127a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h> 421da177e4SLinus Torvalds 43570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 44570a335bSHugh Dickins unsigned char); 45570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 46d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 47570a335bSHugh Dickins 4838b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 497c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 50ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 51ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 521da177e4SLinus Torvalds long total_swap_pages; 5378ecba08SHugh Dickins static int least_priority; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 561da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 571da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 581da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 591da177e4SLinus Torvalds 60adfab836SDan Streetman /* 61adfab836SDan Streetman * all active swap_info_structs 62adfab836SDan Streetman * protected with swap_lock, and ordered by priority. 63adfab836SDan Streetman */ 6418ab4d4cSDan Streetman PLIST_HEAD(swap_active_head); 6518ab4d4cSDan Streetman 6618ab4d4cSDan Streetman /* 6718ab4d4cSDan Streetman * all available (active, not full) swap_info_structs 6818ab4d4cSDan Streetman * protected with swap_avail_lock, ordered by priority. 6918ab4d4cSDan Streetman * This is used by get_swap_page() instead of swap_active_head 7018ab4d4cSDan Streetman * because swap_active_head includes all swap_info_structs, 7118ab4d4cSDan Streetman * but get_swap_page() doesn't need to look at full ones. 7218ab4d4cSDan Streetman * This uses its own lock instead of swap_lock because when a 7318ab4d4cSDan Streetman * swap_info_struct changes between not-full/full, it needs to 7418ab4d4cSDan Streetman * add/remove itself to/from this list, but the swap_info_struct->lock 7518ab4d4cSDan Streetman * is held and the locking order requires swap_lock to be taken 7618ab4d4cSDan Streetman * before any swap_info_struct->lock. 7718ab4d4cSDan Streetman */ 7818ab4d4cSDan Streetman static PLIST_HEAD(swap_avail_head); 7918ab4d4cSDan Streetman static DEFINE_SPINLOCK(swap_avail_lock); 801da177e4SLinus Torvalds 8138b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 821da177e4SLinus Torvalds 83fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 841da177e4SLinus Torvalds 8566d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 8666d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 8766d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 8866d7dd51SKay Sievers 898d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 90355cfa73SKAMEZAWA Hiroyuki { 91570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 92355cfa73SKAMEZAWA Hiroyuki } 93355cfa73SKAMEZAWA Hiroyuki 94efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 95c9e44410SKAMEZAWA Hiroyuki static int 96c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 97c9e44410SKAMEZAWA Hiroyuki { 98efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 99c9e44410SKAMEZAWA Hiroyuki struct page *page; 100c9e44410SKAMEZAWA Hiroyuki int ret = 0; 101c9e44410SKAMEZAWA Hiroyuki 10233806f06SShaohua Li page = find_get_page(swap_address_space(entry), entry.val); 103c9e44410SKAMEZAWA Hiroyuki if (!page) 104c9e44410SKAMEZAWA Hiroyuki return 0; 105c9e44410SKAMEZAWA Hiroyuki /* 106c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 107c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 108c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 109c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 110c9e44410SKAMEZAWA Hiroyuki * in usual operations. 111c9e44410SKAMEZAWA Hiroyuki */ 112c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 113c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 114c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 115c9e44410SKAMEZAWA Hiroyuki } 116c9e44410SKAMEZAWA Hiroyuki page_cache_release(page); 117c9e44410SKAMEZAWA Hiroyuki return ret; 118c9e44410SKAMEZAWA Hiroyuki } 119355cfa73SKAMEZAWA Hiroyuki 1201da177e4SLinus Torvalds /* 1216a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1226a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1236a6ba831SHugh Dickins */ 1246a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1256a6ba831SHugh Dickins { 1266a6ba831SHugh Dickins struct swap_extent *se; 1279625a5f2SHugh Dickins sector_t start_block; 1289625a5f2SHugh Dickins sector_t nr_blocks; 1296a6ba831SHugh Dickins int err = 0; 1306a6ba831SHugh Dickins 1316a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1329625a5f2SHugh Dickins se = &si->first_swap_extent; 1339625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1349625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1359625a5f2SHugh Dickins if (nr_blocks) { 1369625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 137dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1389625a5f2SHugh Dickins if (err) 1399625a5f2SHugh Dickins return err; 1409625a5f2SHugh Dickins cond_resched(); 1416a6ba831SHugh Dickins } 1426a6ba831SHugh Dickins 1439625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1449625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1459625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1469625a5f2SHugh Dickins 1476a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 148dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1496a6ba831SHugh Dickins if (err) 1506a6ba831SHugh Dickins break; 1516a6ba831SHugh Dickins 1526a6ba831SHugh Dickins cond_resched(); 1536a6ba831SHugh Dickins } 1546a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1556a6ba831SHugh Dickins } 1566a6ba831SHugh Dickins 1577992fde7SHugh Dickins /* 1587992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1597992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1607992fde7SHugh Dickins */ 1617992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1627992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1637992fde7SHugh Dickins { 1647992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1657992fde7SHugh Dickins int found_extent = 0; 1667992fde7SHugh Dickins 1677992fde7SHugh Dickins while (nr_pages) { 1687992fde7SHugh Dickins struct list_head *lh; 1697992fde7SHugh Dickins 1707992fde7SHugh Dickins if (se->start_page <= start_page && 1717992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1727992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1737992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 174858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1757992fde7SHugh Dickins 1767992fde7SHugh Dickins if (nr_blocks > nr_pages) 1777992fde7SHugh Dickins nr_blocks = nr_pages; 1787992fde7SHugh Dickins start_page += nr_blocks; 1797992fde7SHugh Dickins nr_pages -= nr_blocks; 1807992fde7SHugh Dickins 1817992fde7SHugh Dickins if (!found_extent++) 1827992fde7SHugh Dickins si->curr_swap_extent = se; 1837992fde7SHugh Dickins 1847992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1857992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1867992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 187dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1887992fde7SHugh Dickins break; 1897992fde7SHugh Dickins } 1907992fde7SHugh Dickins 1917992fde7SHugh Dickins lh = se->list.next; 1927992fde7SHugh Dickins se = list_entry(lh, struct swap_extent, list); 1937992fde7SHugh Dickins } 1947992fde7SHugh Dickins } 1957992fde7SHugh Dickins 196048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 197048c27fdSHugh Dickins #define LATENCY_LIMIT 256 198048c27fdSHugh Dickins 1992a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 2002a8f9449SShaohua Li unsigned int flag) 2012a8f9449SShaohua Li { 2022a8f9449SShaohua Li info->flags = flag; 2032a8f9449SShaohua Li } 2042a8f9449SShaohua Li 2052a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 2062a8f9449SShaohua Li { 2072a8f9449SShaohua Li return info->data; 2082a8f9449SShaohua Li } 2092a8f9449SShaohua Li 2102a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 2112a8f9449SShaohua Li unsigned int c) 2122a8f9449SShaohua Li { 2132a8f9449SShaohua Li info->data = c; 2142a8f9449SShaohua Li } 2152a8f9449SShaohua Li 2162a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 2172a8f9449SShaohua Li unsigned int c, unsigned int f) 2182a8f9449SShaohua Li { 2192a8f9449SShaohua Li info->flags = f; 2202a8f9449SShaohua Li info->data = c; 2212a8f9449SShaohua Li } 2222a8f9449SShaohua Li 2232a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 2242a8f9449SShaohua Li { 2252a8f9449SShaohua Li return info->data; 2262a8f9449SShaohua Li } 2272a8f9449SShaohua Li 2282a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 2292a8f9449SShaohua Li unsigned int n) 2302a8f9449SShaohua Li { 2312a8f9449SShaohua Li info->data = n; 2322a8f9449SShaohua Li } 2332a8f9449SShaohua Li 2342a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 2352a8f9449SShaohua Li unsigned int n, unsigned int f) 2362a8f9449SShaohua Li { 2372a8f9449SShaohua Li info->flags = f; 2382a8f9449SShaohua Li info->data = n; 2392a8f9449SShaohua Li } 2402a8f9449SShaohua Li 2412a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 2422a8f9449SShaohua Li { 2432a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 2442a8f9449SShaohua Li } 2452a8f9449SShaohua Li 2462a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 2472a8f9449SShaohua Li { 2482a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 2492a8f9449SShaohua Li } 2502a8f9449SShaohua Li 2512a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 2522a8f9449SShaohua Li { 2532a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 2542a8f9449SShaohua Li info->data = 0; 2552a8f9449SShaohua Li } 2562a8f9449SShaohua Li 257815c2c54SShaohua Li /* Add a cluster to discard list and schedule it to do discard */ 258815c2c54SShaohua Li static void swap_cluster_schedule_discard(struct swap_info_struct *si, 259815c2c54SShaohua Li unsigned int idx) 260815c2c54SShaohua Li { 261815c2c54SShaohua Li /* 262815c2c54SShaohua Li * If scan_swap_map() can't find a free cluster, it will check 263815c2c54SShaohua Li * si->swap_map directly. To make sure the discarding cluster isn't 264815c2c54SShaohua Li * taken by scan_swap_map(), mark the swap entries bad (occupied). It 265815c2c54SShaohua Li * will be cleared after discard 266815c2c54SShaohua Li */ 267815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 268815c2c54SShaohua Li SWAP_MAP_BAD, SWAPFILE_CLUSTER); 269815c2c54SShaohua Li 270815c2c54SShaohua Li if (cluster_is_null(&si->discard_cluster_head)) { 271815c2c54SShaohua Li cluster_set_next_flag(&si->discard_cluster_head, 272815c2c54SShaohua Li idx, 0); 273815c2c54SShaohua Li cluster_set_next_flag(&si->discard_cluster_tail, 274815c2c54SShaohua Li idx, 0); 275815c2c54SShaohua Li } else { 276815c2c54SShaohua Li unsigned int tail = cluster_next(&si->discard_cluster_tail); 277815c2c54SShaohua Li cluster_set_next(&si->cluster_info[tail], idx); 278815c2c54SShaohua Li cluster_set_next_flag(&si->discard_cluster_tail, 279815c2c54SShaohua Li idx, 0); 280815c2c54SShaohua Li } 281815c2c54SShaohua Li 282815c2c54SShaohua Li schedule_work(&si->discard_work); 283815c2c54SShaohua Li } 284815c2c54SShaohua Li 285815c2c54SShaohua Li /* 286815c2c54SShaohua Li * Doing discard actually. After a cluster discard is finished, the cluster 287815c2c54SShaohua Li * will be added to free cluster list. caller should hold si->lock. 288815c2c54SShaohua Li */ 289815c2c54SShaohua Li static void swap_do_scheduled_discard(struct swap_info_struct *si) 290815c2c54SShaohua Li { 291815c2c54SShaohua Li struct swap_cluster_info *info; 292815c2c54SShaohua Li unsigned int idx; 293815c2c54SShaohua Li 294815c2c54SShaohua Li info = si->cluster_info; 295815c2c54SShaohua Li 296815c2c54SShaohua Li while (!cluster_is_null(&si->discard_cluster_head)) { 297815c2c54SShaohua Li idx = cluster_next(&si->discard_cluster_head); 298815c2c54SShaohua Li 299815c2c54SShaohua Li cluster_set_next_flag(&si->discard_cluster_head, 300815c2c54SShaohua Li cluster_next(&info[idx]), 0); 301815c2c54SShaohua Li if (cluster_next(&si->discard_cluster_tail) == idx) { 302815c2c54SShaohua Li cluster_set_null(&si->discard_cluster_head); 303815c2c54SShaohua Li cluster_set_null(&si->discard_cluster_tail); 304815c2c54SShaohua Li } 305815c2c54SShaohua Li spin_unlock(&si->lock); 306815c2c54SShaohua Li 307815c2c54SShaohua Li discard_swap_cluster(si, idx * SWAPFILE_CLUSTER, 308815c2c54SShaohua Li SWAPFILE_CLUSTER); 309815c2c54SShaohua Li 310815c2c54SShaohua Li spin_lock(&si->lock); 311815c2c54SShaohua Li cluster_set_flag(&info[idx], CLUSTER_FLAG_FREE); 312815c2c54SShaohua Li if (cluster_is_null(&si->free_cluster_head)) { 313815c2c54SShaohua Li cluster_set_next_flag(&si->free_cluster_head, 314815c2c54SShaohua Li idx, 0); 315815c2c54SShaohua Li cluster_set_next_flag(&si->free_cluster_tail, 316815c2c54SShaohua Li idx, 0); 317815c2c54SShaohua Li } else { 318815c2c54SShaohua Li unsigned int tail; 319815c2c54SShaohua Li 320815c2c54SShaohua Li tail = cluster_next(&si->free_cluster_tail); 321815c2c54SShaohua Li cluster_set_next(&info[tail], idx); 322815c2c54SShaohua Li cluster_set_next_flag(&si->free_cluster_tail, 323815c2c54SShaohua Li idx, 0); 324815c2c54SShaohua Li } 325815c2c54SShaohua Li memset(si->swap_map + idx * SWAPFILE_CLUSTER, 326815c2c54SShaohua Li 0, SWAPFILE_CLUSTER); 327815c2c54SShaohua Li } 328815c2c54SShaohua Li } 329815c2c54SShaohua Li 330815c2c54SShaohua Li static void swap_discard_work(struct work_struct *work) 331815c2c54SShaohua Li { 332815c2c54SShaohua Li struct swap_info_struct *si; 333815c2c54SShaohua Li 334815c2c54SShaohua Li si = container_of(work, struct swap_info_struct, discard_work); 335815c2c54SShaohua Li 336815c2c54SShaohua Li spin_lock(&si->lock); 337815c2c54SShaohua Li swap_do_scheduled_discard(si); 338815c2c54SShaohua Li spin_unlock(&si->lock); 339815c2c54SShaohua Li } 340815c2c54SShaohua Li 3412a8f9449SShaohua Li /* 3422a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 3432a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 3442a8f9449SShaohua Li */ 3452a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 3462a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 3472a8f9449SShaohua Li { 3482a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 3492a8f9449SShaohua Li 3502a8f9449SShaohua Li if (!cluster_info) 3512a8f9449SShaohua Li return; 3522a8f9449SShaohua Li if (cluster_is_free(&cluster_info[idx])) { 3532a8f9449SShaohua Li VM_BUG_ON(cluster_next(&p->free_cluster_head) != idx); 3542a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, 3552a8f9449SShaohua Li cluster_next(&cluster_info[idx]), 0); 3562a8f9449SShaohua Li if (cluster_next(&p->free_cluster_tail) == idx) { 3572a8f9449SShaohua Li cluster_set_null(&p->free_cluster_tail); 3582a8f9449SShaohua Li cluster_set_null(&p->free_cluster_head); 3592a8f9449SShaohua Li } 3602a8f9449SShaohua Li cluster_set_count_flag(&cluster_info[idx], 0, 0); 3612a8f9449SShaohua Li } 3622a8f9449SShaohua Li 3632a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 3642a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 3652a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 3662a8f9449SShaohua Li } 3672a8f9449SShaohua Li 3682a8f9449SShaohua Li /* 3692a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 3702a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 3712a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 3722a8f9449SShaohua Li */ 3732a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 3742a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 3752a8f9449SShaohua Li { 3762a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 3772a8f9449SShaohua Li 3782a8f9449SShaohua Li if (!cluster_info) 3792a8f9449SShaohua Li return; 3802a8f9449SShaohua Li 3812a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 3822a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 3832a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 3842a8f9449SShaohua Li 3852a8f9449SShaohua Li if (cluster_count(&cluster_info[idx]) == 0) { 386815c2c54SShaohua Li /* 387815c2c54SShaohua Li * If the swap is discardable, prepare discard the cluster 388815c2c54SShaohua Li * instead of free it immediately. The cluster will be freed 389815c2c54SShaohua Li * after discard. 390815c2c54SShaohua Li */ 391edfe23daSShaohua Li if ((p->flags & (SWP_WRITEOK | SWP_PAGE_DISCARD)) == 392edfe23daSShaohua Li (SWP_WRITEOK | SWP_PAGE_DISCARD)) { 393815c2c54SShaohua Li swap_cluster_schedule_discard(p, idx); 394815c2c54SShaohua Li return; 395815c2c54SShaohua Li } 396815c2c54SShaohua Li 3972a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 3982a8f9449SShaohua Li if (cluster_is_null(&p->free_cluster_head)) { 3992a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, idx, 0); 4002a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, idx, 0); 4012a8f9449SShaohua Li } else { 4022a8f9449SShaohua Li unsigned int tail = cluster_next(&p->free_cluster_tail); 4032a8f9449SShaohua Li cluster_set_next(&cluster_info[tail], idx); 4042a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, idx, 0); 4052a8f9449SShaohua Li } 4062a8f9449SShaohua Li } 4072a8f9449SShaohua Li } 4082a8f9449SShaohua Li 4092a8f9449SShaohua Li /* 4102a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 4112a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 4122a8f9449SShaohua Li */ 413ebc2a1a6SShaohua Li static bool 414ebc2a1a6SShaohua Li scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, 4152a8f9449SShaohua Li unsigned long offset) 4162a8f9449SShaohua Li { 417ebc2a1a6SShaohua Li struct percpu_cluster *percpu_cluster; 418ebc2a1a6SShaohua Li bool conflict; 419ebc2a1a6SShaohua Li 4202a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 421ebc2a1a6SShaohua Li conflict = !cluster_is_null(&si->free_cluster_head) && 4222a8f9449SShaohua Li offset != cluster_next(&si->free_cluster_head) && 4232a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 424ebc2a1a6SShaohua Li 425ebc2a1a6SShaohua Li if (!conflict) 426ebc2a1a6SShaohua Li return false; 427ebc2a1a6SShaohua Li 428ebc2a1a6SShaohua Li percpu_cluster = this_cpu_ptr(si->percpu_cluster); 429ebc2a1a6SShaohua Li cluster_set_null(&percpu_cluster->index); 430ebc2a1a6SShaohua Li return true; 431ebc2a1a6SShaohua Li } 432ebc2a1a6SShaohua Li 433ebc2a1a6SShaohua Li /* 434ebc2a1a6SShaohua Li * Try to get a swap entry from current cpu's swap entry pool (a cluster). This 435ebc2a1a6SShaohua Li * might involve allocating a new cluster for current CPU too. 436ebc2a1a6SShaohua Li */ 437ebc2a1a6SShaohua Li static void scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, 438ebc2a1a6SShaohua Li unsigned long *offset, unsigned long *scan_base) 439ebc2a1a6SShaohua Li { 440ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 441ebc2a1a6SShaohua Li bool found_free; 442ebc2a1a6SShaohua Li unsigned long tmp; 443ebc2a1a6SShaohua Li 444ebc2a1a6SShaohua Li new_cluster: 445ebc2a1a6SShaohua Li cluster = this_cpu_ptr(si->percpu_cluster); 446ebc2a1a6SShaohua Li if (cluster_is_null(&cluster->index)) { 447ebc2a1a6SShaohua Li if (!cluster_is_null(&si->free_cluster_head)) { 448ebc2a1a6SShaohua Li cluster->index = si->free_cluster_head; 449ebc2a1a6SShaohua Li cluster->next = cluster_next(&cluster->index) * 450ebc2a1a6SShaohua Li SWAPFILE_CLUSTER; 451ebc2a1a6SShaohua Li } else if (!cluster_is_null(&si->discard_cluster_head)) { 452ebc2a1a6SShaohua Li /* 453ebc2a1a6SShaohua Li * we don't have free cluster but have some clusters in 454ebc2a1a6SShaohua Li * discarding, do discard now and reclaim them 455ebc2a1a6SShaohua Li */ 456ebc2a1a6SShaohua Li swap_do_scheduled_discard(si); 457ebc2a1a6SShaohua Li *scan_base = *offset = si->cluster_next; 458ebc2a1a6SShaohua Li goto new_cluster; 459ebc2a1a6SShaohua Li } else 460ebc2a1a6SShaohua Li return; 461ebc2a1a6SShaohua Li } 462ebc2a1a6SShaohua Li 463ebc2a1a6SShaohua Li found_free = false; 464ebc2a1a6SShaohua Li 465ebc2a1a6SShaohua Li /* 466ebc2a1a6SShaohua Li * Other CPUs can use our cluster if they can't find a free cluster, 467ebc2a1a6SShaohua Li * check if there is still free entry in the cluster 468ebc2a1a6SShaohua Li */ 469ebc2a1a6SShaohua Li tmp = cluster->next; 470ebc2a1a6SShaohua Li while (tmp < si->max && tmp < (cluster_next(&cluster->index) + 1) * 471ebc2a1a6SShaohua Li SWAPFILE_CLUSTER) { 472ebc2a1a6SShaohua Li if (!si->swap_map[tmp]) { 473ebc2a1a6SShaohua Li found_free = true; 474ebc2a1a6SShaohua Li break; 475ebc2a1a6SShaohua Li } 476ebc2a1a6SShaohua Li tmp++; 477ebc2a1a6SShaohua Li } 478ebc2a1a6SShaohua Li if (!found_free) { 479ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 480ebc2a1a6SShaohua Li goto new_cluster; 481ebc2a1a6SShaohua Li } 482ebc2a1a6SShaohua Li cluster->next = tmp + 1; 483ebc2a1a6SShaohua Li *offset = tmp; 484ebc2a1a6SShaohua Li *scan_base = tmp; 4852a8f9449SShaohua Li } 4862a8f9449SShaohua Li 48724b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si, 4888d69aaeeSHugh Dickins unsigned char usage) 4891da177e4SLinus Torvalds { 490ebebbbe9SHugh Dickins unsigned long offset; 491c60aa176SHugh Dickins unsigned long scan_base; 4927992fde7SHugh Dickins unsigned long last_in_cluster = 0; 493048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 4941da177e4SLinus Torvalds 4957dfad418SHugh Dickins /* 4967dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 4977dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 4987dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 4997dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 5007dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 5017dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 5027dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 503c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 5041da177e4SLinus Torvalds */ 5057dfad418SHugh Dickins 50652b7efdbSHugh Dickins si->flags += SWP_SCANNING; 507c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 508ebebbbe9SHugh Dickins 509ebc2a1a6SShaohua Li /* SSD algorithm */ 510ebc2a1a6SShaohua Li if (si->cluster_info) { 511ebc2a1a6SShaohua Li scan_swap_map_try_ssd_cluster(si, &offset, &scan_base); 512815c2c54SShaohua Li goto checks; 513815c2c54SShaohua Li } 514815c2c54SShaohua Li 515ebc2a1a6SShaohua Li if (unlikely(!si->cluster_nr--)) { 516ebc2a1a6SShaohua Li if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 517ebc2a1a6SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 5182a8f9449SShaohua Li goto checks; 5192a8f9449SShaohua Li } 5202a8f9449SShaohua Li 521ec8acf20SShaohua Li spin_unlock(&si->lock); 5227dfad418SHugh Dickins 523c60aa176SHugh Dickins /* 524c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 525c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 52650088c44SChen Yucong * If seek is cheap, that is the SWP_SOLIDSTATE si->cluster_info 52750088c44SChen Yucong * case, just handled by scan_swap_map_try_ssd_cluster() above. 528c60aa176SHugh Dickins */ 529c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 5307dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 5317dfad418SHugh Dickins 5327dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 5337dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 5341da177e4SLinus Torvalds if (si->swap_map[offset]) 5357dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 5367dfad418SHugh Dickins else if (offset == last_in_cluster) { 537ec8acf20SShaohua Li spin_lock(&si->lock); 538ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 539ebebbbe9SHugh Dickins si->cluster_next = offset; 540ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 541ebebbbe9SHugh Dickins goto checks; 5427dfad418SHugh Dickins } 543048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 544048c27fdSHugh Dickins cond_resched(); 545048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 546048c27fdSHugh Dickins } 5477dfad418SHugh Dickins } 548ebebbbe9SHugh Dickins 549c60aa176SHugh Dickins offset = scan_base; 550ec8acf20SShaohua Li spin_lock(&si->lock); 551ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 5527dfad418SHugh Dickins } 5537dfad418SHugh Dickins 554ebebbbe9SHugh Dickins checks: 555ebc2a1a6SShaohua Li if (si->cluster_info) { 556ebc2a1a6SShaohua Li while (scan_swap_map_ssd_cluster_conflict(si, offset)) 557ebc2a1a6SShaohua Li scan_swap_map_try_ssd_cluster(si, &offset, &scan_base); 558ebc2a1a6SShaohua Li } 559ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 56052b7efdbSHugh Dickins goto no_page; 5617dfad418SHugh Dickins if (!si->highest_bit) 5627dfad418SHugh Dickins goto no_page; 563ebebbbe9SHugh Dickins if (offset > si->highest_bit) 564c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 565c9e44410SKAMEZAWA Hiroyuki 566b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 567b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 568c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 569ec8acf20SShaohua Li spin_unlock(&si->lock); 570c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 571ec8acf20SShaohua Li spin_lock(&si->lock); 572c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 573c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 574c9e44410SKAMEZAWA Hiroyuki goto checks; 575c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 576c9e44410SKAMEZAWA Hiroyuki } 577c9e44410SKAMEZAWA Hiroyuki 578ebebbbe9SHugh Dickins if (si->swap_map[offset]) 579ebebbbe9SHugh Dickins goto scan; 580ebebbbe9SHugh Dickins 58152b7efdbSHugh Dickins if (offset == si->lowest_bit) 5821da177e4SLinus Torvalds si->lowest_bit++; 5831da177e4SLinus Torvalds if (offset == si->highest_bit) 5841da177e4SLinus Torvalds si->highest_bit--; 5857dfad418SHugh Dickins si->inuse_pages++; 5867dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 5871da177e4SLinus Torvalds si->lowest_bit = si->max; 5881da177e4SLinus Torvalds si->highest_bit = 0; 58918ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 59018ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 59118ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 5921da177e4SLinus Torvalds } 593253d553bSHugh Dickins si->swap_map[offset] = usage; 5942a8f9449SShaohua Li inc_cluster_info_page(si, si->cluster_info, offset); 5951da177e4SLinus Torvalds si->cluster_next = offset + 1; 59652b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 5977992fde7SHugh Dickins 5981da177e4SLinus Torvalds return offset; 5997dfad418SHugh Dickins 600ebebbbe9SHugh Dickins scan: 601ec8acf20SShaohua Li spin_unlock(&si->lock); 6027dfad418SHugh Dickins while (++offset <= si->highest_bit) { 60352b7efdbSHugh Dickins if (!si->swap_map[offset]) { 604ec8acf20SShaohua Li spin_lock(&si->lock); 60552b7efdbSHugh Dickins goto checks; 6067dfad418SHugh Dickins } 607c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 608ec8acf20SShaohua Li spin_lock(&si->lock); 609c9e44410SKAMEZAWA Hiroyuki goto checks; 610c9e44410SKAMEZAWA Hiroyuki } 611048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 612048c27fdSHugh Dickins cond_resched(); 613048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 614048c27fdSHugh Dickins } 61552b7efdbSHugh Dickins } 616c60aa176SHugh Dickins offset = si->lowest_bit; 617a5998061SJamie Liu while (offset < scan_base) { 618c60aa176SHugh Dickins if (!si->swap_map[offset]) { 619ec8acf20SShaohua Li spin_lock(&si->lock); 620ebebbbe9SHugh Dickins goto checks; 621c60aa176SHugh Dickins } 622c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 623ec8acf20SShaohua Li spin_lock(&si->lock); 624c9e44410SKAMEZAWA Hiroyuki goto checks; 625c9e44410SKAMEZAWA Hiroyuki } 626c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 627c60aa176SHugh Dickins cond_resched(); 628c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 629c60aa176SHugh Dickins } 630a5998061SJamie Liu offset++; 631c60aa176SHugh Dickins } 632ec8acf20SShaohua Li spin_lock(&si->lock); 6337dfad418SHugh Dickins 6347dfad418SHugh Dickins no_page: 63552b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 6361da177e4SLinus Torvalds return 0; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 6401da177e4SLinus Torvalds { 641adfab836SDan Streetman struct swap_info_struct *si, *next; 642fb4f88dcSHugh Dickins pgoff_t offset; 6431da177e4SLinus Torvalds 644ec8acf20SShaohua Li if (atomic_long_read(&nr_swap_pages) <= 0) 645fb4f88dcSHugh Dickins goto noswap; 646ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 6471da177e4SLinus Torvalds 64818ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 64918ab4d4cSDan Streetman 65018ab4d4cSDan Streetman start_over: 65118ab4d4cSDan Streetman plist_for_each_entry_safe(si, next, &swap_avail_head, avail_list) { 65218ab4d4cSDan Streetman /* requeue si to after same-priority siblings */ 65318ab4d4cSDan Streetman plist_requeue(&si->avail_list, &swap_avail_head); 65418ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 655ec8acf20SShaohua Li spin_lock(&si->lock); 656adfab836SDan Streetman if (!si->highest_bit || !(si->flags & SWP_WRITEOK)) { 65718ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 65818ab4d4cSDan Streetman if (plist_node_empty(&si->avail_list)) { 659ec8acf20SShaohua Li spin_unlock(&si->lock); 66018ab4d4cSDan Streetman goto nextsi; 66118ab4d4cSDan Streetman } 66218ab4d4cSDan Streetman WARN(!si->highest_bit, 66318ab4d4cSDan Streetman "swap_info %d in list but !highest_bit\n", 66418ab4d4cSDan Streetman si->type); 66518ab4d4cSDan Streetman WARN(!(si->flags & SWP_WRITEOK), 66618ab4d4cSDan Streetman "swap_info %d in list but !SWP_WRITEOK\n", 66718ab4d4cSDan Streetman si->type); 66818ab4d4cSDan Streetman plist_del(&si->avail_list, &swap_avail_head); 66918ab4d4cSDan Streetman spin_unlock(&si->lock); 67018ab4d4cSDan Streetman goto nextsi; 671ec8acf20SShaohua Li } 672fb4f88dcSHugh Dickins 673355cfa73SKAMEZAWA Hiroyuki /* This is called for allocating swap entry for cache */ 674253d553bSHugh Dickins offset = scan_swap_map(si, SWAP_HAS_CACHE); 675ec8acf20SShaohua Li spin_unlock(&si->lock); 676ec8acf20SShaohua Li if (offset) 677adfab836SDan Streetman return swp_entry(si->type, offset); 67818ab4d4cSDan Streetman pr_debug("scan_swap_map of si %d failed to find offset\n", 67918ab4d4cSDan Streetman si->type); 68018ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 68118ab4d4cSDan Streetman nextsi: 682adfab836SDan Streetman /* 683adfab836SDan Streetman * if we got here, it's likely that si was almost full before, 684adfab836SDan Streetman * and since scan_swap_map() can drop the si->lock, multiple 685adfab836SDan Streetman * callers probably all tried to get a page from the same si 68618ab4d4cSDan Streetman * and it filled up before we could get one; or, the si filled 68718ab4d4cSDan Streetman * up between us dropping swap_avail_lock and taking si->lock. 68818ab4d4cSDan Streetman * Since we dropped the swap_avail_lock, the swap_avail_head 68918ab4d4cSDan Streetman * list may have been modified; so if next is still in the 69018ab4d4cSDan Streetman * swap_avail_head list then try it, otherwise start over. 691adfab836SDan Streetman */ 69218ab4d4cSDan Streetman if (plist_node_empty(&next->avail_list)) 69318ab4d4cSDan Streetman goto start_over; 694fb4f88dcSHugh Dickins } 695fb4f88dcSHugh Dickins 69618ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 69718ab4d4cSDan Streetman 698ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 699fb4f88dcSHugh Dickins noswap: 700fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 7032de1a7e4SSeth Jennings /* The only caller of this function is now suspend routine */ 704910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 705910321eaSHugh Dickins { 706910321eaSHugh Dickins struct swap_info_struct *si; 707910321eaSHugh Dickins pgoff_t offset; 708910321eaSHugh Dickins 709910321eaSHugh Dickins si = swap_info[type]; 710ec8acf20SShaohua Li spin_lock(&si->lock); 711910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 712ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 713910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 714910321eaSHugh Dickins offset = scan_swap_map(si, 1); 715910321eaSHugh Dickins if (offset) { 716ec8acf20SShaohua Li spin_unlock(&si->lock); 717910321eaSHugh Dickins return swp_entry(type, offset); 718910321eaSHugh Dickins } 719ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 720910321eaSHugh Dickins } 721ec8acf20SShaohua Li spin_unlock(&si->lock); 722910321eaSHugh Dickins return (swp_entry_t) {0}; 723910321eaSHugh Dickins } 724910321eaSHugh Dickins 7251da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry) 7261da177e4SLinus Torvalds { 7271da177e4SLinus Torvalds struct swap_info_struct *p; 7281da177e4SLinus Torvalds unsigned long offset, type; 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (!entry.val) 7311da177e4SLinus Torvalds goto out; 7321da177e4SLinus Torvalds type = swp_type(entry); 7331da177e4SLinus Torvalds if (type >= nr_swapfiles) 7341da177e4SLinus Torvalds goto bad_nofile; 735efa90a98SHugh Dickins p = swap_info[type]; 7361da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 7371da177e4SLinus Torvalds goto bad_device; 7381da177e4SLinus Torvalds offset = swp_offset(entry); 7391da177e4SLinus Torvalds if (offset >= p->max) 7401da177e4SLinus Torvalds goto bad_offset; 7411da177e4SLinus Torvalds if (!p->swap_map[offset]) 7421da177e4SLinus Torvalds goto bad_free; 743ec8acf20SShaohua Li spin_lock(&p->lock); 7441da177e4SLinus Torvalds return p; 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds bad_free: 747465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Unused_offset, entry.val); 7481da177e4SLinus Torvalds goto out; 7491da177e4SLinus Torvalds bad_offset: 750465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Bad_offset, entry.val); 7511da177e4SLinus Torvalds goto out; 7521da177e4SLinus Torvalds bad_device: 753465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Unused_file, entry.val); 7541da177e4SLinus Torvalds goto out; 7551da177e4SLinus Torvalds bad_nofile: 756465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Bad_file, entry.val); 7571da177e4SLinus Torvalds out: 7581da177e4SLinus Torvalds return NULL; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7618d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p, 7628d69aaeeSHugh Dickins swp_entry_t entry, unsigned char usage) 7631da177e4SLinus Torvalds { 764253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 7658d69aaeeSHugh Dickins unsigned char count; 7668d69aaeeSHugh Dickins unsigned char has_cache; 7671da177e4SLinus Torvalds 768355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 769253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 770253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 771253d553bSHugh Dickins 772253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 773253d553bSHugh Dickins VM_BUG_ON(!has_cache); 774253d553bSHugh Dickins has_cache = 0; 775aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 776aaa46865SHugh Dickins /* 777aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 778aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 779aaa46865SHugh Dickins */ 780aaa46865SHugh Dickins count = 0; 781570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 782570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 783570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 784570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 785570a335bSHugh Dickins else 786570a335bSHugh Dickins count = SWAP_MAP_MAX; 787570a335bSHugh Dickins } else 788253d553bSHugh Dickins count--; 789570a335bSHugh Dickins } 790253d553bSHugh Dickins 791253d553bSHugh Dickins if (!count) 792253d553bSHugh Dickins mem_cgroup_uncharge_swap(entry); 793253d553bSHugh Dickins 794253d553bSHugh Dickins usage = count | has_cache; 795253d553bSHugh Dickins p->swap_map[offset] = usage; 796253d553bSHugh Dickins 797355cfa73SKAMEZAWA Hiroyuki /* free if no reference */ 798253d553bSHugh Dickins if (!usage) { 7992a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 8001da177e4SLinus Torvalds if (offset < p->lowest_bit) 8011da177e4SLinus Torvalds p->lowest_bit = offset; 80218ab4d4cSDan Streetman if (offset > p->highest_bit) { 80318ab4d4cSDan Streetman bool was_full = !p->highest_bit; 8041da177e4SLinus Torvalds p->highest_bit = offset; 80518ab4d4cSDan Streetman if (was_full && (p->flags & SWP_WRITEOK)) { 80618ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 80718ab4d4cSDan Streetman WARN_ON(!plist_node_empty(&p->avail_list)); 80818ab4d4cSDan Streetman if (plist_node_empty(&p->avail_list)) 80918ab4d4cSDan Streetman plist_add(&p->avail_list, 81018ab4d4cSDan Streetman &swap_avail_head); 81118ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 81218ab4d4cSDan Streetman } 81318ab4d4cSDan Streetman } 814ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 8151da177e4SLinus Torvalds p->inuse_pages--; 81638b5faf4SDan Magenheimer frontswap_invalidate_page(p->type, offset); 81773744923SMel Gorman if (p->flags & SWP_BLKDEV) { 81873744923SMel Gorman struct gendisk *disk = p->bdev->bd_disk; 81973744923SMel Gorman if (disk->fops->swap_slot_free_notify) 82073744923SMel Gorman disk->fops->swap_slot_free_notify(p->bdev, 82173744923SMel Gorman offset); 82273744923SMel Gorman } 8231da177e4SLinus Torvalds } 824253d553bSHugh Dickins 825253d553bSHugh Dickins return usage; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * Caller has made sure that the swap device corresponding to entry 8301da177e4SLinus Torvalds * is still around or has not been recycled. 8311da177e4SLinus Torvalds */ 8321da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds struct swap_info_struct *p; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds p = swap_info_get(entry); 8371da177e4SLinus Torvalds if (p) { 838253d553bSHugh Dickins swap_entry_free(p, entry, 1); 839ec8acf20SShaohua Li spin_unlock(&p->lock); 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* 844cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 845cb4b86baSKAMEZAWA Hiroyuki */ 846*0a31bc97SJohannes Weiner void swapcache_free(swp_entry_t entry) 847cb4b86baSKAMEZAWA Hiroyuki { 848355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 849355cfa73SKAMEZAWA Hiroyuki 850355cfa73SKAMEZAWA Hiroyuki p = swap_info_get(entry); 851355cfa73SKAMEZAWA Hiroyuki if (p) { 852*0a31bc97SJohannes Weiner swap_entry_free(p, entry, SWAP_HAS_CACHE); 853ec8acf20SShaohua Li spin_unlock(&p->lock); 854355cfa73SKAMEZAWA Hiroyuki } 855cb4b86baSKAMEZAWA Hiroyuki } 856cb4b86baSKAMEZAWA Hiroyuki 857cb4b86baSKAMEZAWA Hiroyuki /* 858c475a8abSHugh Dickins * How many references to page are currently swapped out? 859570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 860570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 8611da177e4SLinus Torvalds */ 862bde05d1cSHugh Dickins int page_swapcount(struct page *page) 8631da177e4SLinus Torvalds { 864c475a8abSHugh Dickins int count = 0; 8651da177e4SLinus Torvalds struct swap_info_struct *p; 8661da177e4SLinus Torvalds swp_entry_t entry; 8671da177e4SLinus Torvalds 8684c21e2f2SHugh Dickins entry.val = page_private(page); 8691da177e4SLinus Torvalds p = swap_info_get(entry); 8701da177e4SLinus Torvalds if (p) { 871355cfa73SKAMEZAWA Hiroyuki count = swap_count(p->swap_map[swp_offset(entry)]); 872ec8acf20SShaohua Li spin_unlock(&p->lock); 8731da177e4SLinus Torvalds } 874c475a8abSHugh Dickins return count; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds /* 8787b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 8797b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 8807b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 8817b1fe597SHugh Dickins * later would only waste time away from clustering. 8821da177e4SLinus Torvalds */ 8837b1fe597SHugh Dickins int reuse_swap_page(struct page *page) 8841da177e4SLinus Torvalds { 885c475a8abSHugh Dickins int count; 8861da177e4SLinus Torvalds 887309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 8885ad64688SHugh Dickins if (unlikely(PageKsm(page))) 8895ad64688SHugh Dickins return 0; 890c475a8abSHugh Dickins count = page_mapcount(page); 8917b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 892c475a8abSHugh Dickins count += page_swapcount(page); 8937b1fe597SHugh Dickins if (count == 1 && !PageWriteback(page)) { 8947b1fe597SHugh Dickins delete_from_swap_cache(page); 8957b1fe597SHugh Dickins SetPageDirty(page); 8967b1fe597SHugh Dickins } 8977b1fe597SHugh Dickins } 8985ad64688SHugh Dickins return count <= 1; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* 902a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 903a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 9041da177e4SLinus Torvalds */ 905a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 9061da177e4SLinus Torvalds { 907309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds if (!PageSwapCache(page)) 9101da177e4SLinus Torvalds return 0; 9111da177e4SLinus Torvalds if (PageWriteback(page)) 9121da177e4SLinus Torvalds return 0; 913a2c43eedSHugh Dickins if (page_swapcount(page)) 9141da177e4SLinus Torvalds return 0; 9151da177e4SLinus Torvalds 916b73d7fceSHugh Dickins /* 917b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 918b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 919b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 920b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 921b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 922b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 923b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 924b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 925b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 926b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 927b73d7fceSHugh Dickins * 9282de1a7e4SSeth Jennings * Hibernation suspends storage while it is writing the image 929f90ac398SMel Gorman * to disk so check that here. 930b73d7fceSHugh Dickins */ 931f90ac398SMel Gorman if (pm_suspended_storage()) 932b73d7fceSHugh Dickins return 0; 933b73d7fceSHugh Dickins 934a2c43eedSHugh Dickins delete_from_swap_cache(page); 9351da177e4SLinus Torvalds SetPageDirty(page); 936a2c43eedSHugh Dickins return 1; 93768a22394SRik van Riel } 93868a22394SRik van Riel 93968a22394SRik van Riel /* 9401da177e4SLinus Torvalds * Free the swap entry like above, but also try to 9411da177e4SLinus Torvalds * free the page cache entry if it is the last user. 9421da177e4SLinus Torvalds */ 9432509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 9441da177e4SLinus Torvalds { 9451da177e4SLinus Torvalds struct swap_info_struct *p; 9461da177e4SLinus Torvalds struct page *page = NULL; 9471da177e4SLinus Torvalds 948a7420aa5SAndi Kleen if (non_swap_entry(entry)) 9492509ef26SHugh Dickins return 1; 9500697212aSChristoph Lameter 9511da177e4SLinus Torvalds p = swap_info_get(entry); 9521da177e4SLinus Torvalds if (p) { 953253d553bSHugh Dickins if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) { 95433806f06SShaohua Li page = find_get_page(swap_address_space(entry), 95533806f06SShaohua Li entry.val); 9568413ac9dSNick Piggin if (page && !trylock_page(page)) { 95793fac704SNick Piggin page_cache_release(page); 95893fac704SNick Piggin page = NULL; 95993fac704SNick Piggin } 96093fac704SNick Piggin } 961ec8acf20SShaohua Li spin_unlock(&p->lock); 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds if (page) { 964a2c43eedSHugh Dickins /* 965a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 966a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 967a2c43eedSHugh Dickins */ 96893fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 969a2c43eedSHugh Dickins (!page_mapped(page) || vm_swap_full())) { 9701da177e4SLinus Torvalds delete_from_swap_cache(page); 9711da177e4SLinus Torvalds SetPageDirty(page); 9721da177e4SLinus Torvalds } 9731da177e4SLinus Torvalds unlock_page(page); 9741da177e4SLinus Torvalds page_cache_release(page); 9751da177e4SLinus Torvalds } 9762509ef26SHugh Dickins return p != NULL; 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds 979b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 980f577eb30SRafael J. Wysocki /* 981915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 982f577eb30SRafael J. Wysocki * 983915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 984915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 985915bae9eSRafael J. Wysocki * 986915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 987f577eb30SRafael J. Wysocki */ 9887bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 989f577eb30SRafael J. Wysocki { 990915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 991efa90a98SHugh Dickins int type; 992f577eb30SRafael J. Wysocki 993915bae9eSRafael J. Wysocki if (device) 994915bae9eSRafael J. Wysocki bdev = bdget(device); 995915bae9eSRafael J. Wysocki 996f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 997efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 998efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 999f577eb30SRafael J. Wysocki 1000915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 1001f577eb30SRafael J. Wysocki continue; 1002b6b5bce3SRafael J. Wysocki 1003915bae9eSRafael J. Wysocki if (!bdev) { 10047bf23687SRafael J. Wysocki if (bdev_p) 1005dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 10067bf23687SRafael J. Wysocki 10076e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 1008efa90a98SHugh Dickins return type; 10096e1819d6SRafael J. Wysocki } 1010915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 10119625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 1012915bae9eSRafael J. Wysocki 1013915bae9eSRafael J. Wysocki if (se->start_block == offset) { 10147bf23687SRafael J. Wysocki if (bdev_p) 1015dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 10167bf23687SRafael J. Wysocki 1017f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1018915bae9eSRafael J. Wysocki bdput(bdev); 1019efa90a98SHugh Dickins return type; 1020f577eb30SRafael J. Wysocki } 1021f577eb30SRafael J. Wysocki } 1022915bae9eSRafael J. Wysocki } 1023f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1024915bae9eSRafael J. Wysocki if (bdev) 1025915bae9eSRafael J. Wysocki bdput(bdev); 1026915bae9eSRafael J. Wysocki 1027f577eb30SRafael J. Wysocki return -ENODEV; 1028f577eb30SRafael J. Wysocki } 1029f577eb30SRafael J. Wysocki 1030f577eb30SRafael J. Wysocki /* 103173c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 103273c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 103373c34b6aSHugh Dickins */ 103473c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 103573c34b6aSHugh Dickins { 103673c34b6aSHugh Dickins struct block_device *bdev; 103773c34b6aSHugh Dickins 103873c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 103973c34b6aSHugh Dickins return 0; 104073c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 104173c34b6aSHugh Dickins return 0; 1042d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 104373c34b6aSHugh Dickins } 104473c34b6aSHugh Dickins 104573c34b6aSHugh Dickins /* 1046f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 1047f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 1048f577eb30SRafael J. Wysocki * 1049f577eb30SRafael J. Wysocki * This is needed for software suspend 1050f577eb30SRafael J. Wysocki */ 1051f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1052f577eb30SRafael J. Wysocki { 1053f577eb30SRafael J. Wysocki unsigned int n = 0; 1054f577eb30SRafael J. Wysocki 1055f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1056efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1057efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1058efa90a98SHugh Dickins 1059ec8acf20SShaohua Li spin_lock(&sis->lock); 1060efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1061efa90a98SHugh Dickins n = sis->pages; 1062f577eb30SRafael J. Wysocki if (free) 1063efa90a98SHugh Dickins n -= sis->inuse_pages; 1064efa90a98SHugh Dickins } 1065ec8acf20SShaohua Li spin_unlock(&sis->lock); 1066f577eb30SRafael J. Wysocki } 1067f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1068f577eb30SRafael J. Wysocki return n; 1069f577eb30SRafael J. Wysocki } 107073c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1071f577eb30SRafael J. Wysocki 1072179ef71cSCyrill Gorcunov static inline int maybe_same_pte(pte_t pte, pte_t swp_pte) 1073179ef71cSCyrill Gorcunov { 1074179ef71cSCyrill Gorcunov #ifdef CONFIG_MEM_SOFT_DIRTY 1075179ef71cSCyrill Gorcunov /* 1076179ef71cSCyrill Gorcunov * When pte keeps soft dirty bit the pte generated 1077179ef71cSCyrill Gorcunov * from swap entry does not has it, still it's same 1078179ef71cSCyrill Gorcunov * pte from logical point of view. 1079179ef71cSCyrill Gorcunov */ 1080179ef71cSCyrill Gorcunov pte_t swp_pte_dirty = pte_swp_mksoft_dirty(swp_pte); 1081179ef71cSCyrill Gorcunov return pte_same(pte, swp_pte) || pte_same(pte, swp_pte_dirty); 1082179ef71cSCyrill Gorcunov #else 1083179ef71cSCyrill Gorcunov return pte_same(pte, swp_pte); 1084179ef71cSCyrill Gorcunov #endif 1085179ef71cSCyrill Gorcunov } 1086179ef71cSCyrill Gorcunov 10871da177e4SLinus Torvalds /* 108872866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 108972866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 109072866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 10911da177e4SLinus Torvalds */ 1092044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 10931da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 10941da177e4SLinus Torvalds { 10959e16b7fbSHugh Dickins struct page *swapcache; 109672835c86SJohannes Weiner struct mem_cgroup *memcg; 1097044d66c1SHugh Dickins spinlock_t *ptl; 1098044d66c1SHugh Dickins pte_t *pte; 1099044d66c1SHugh Dickins int ret = 1; 1100044d66c1SHugh Dickins 11019e16b7fbSHugh Dickins swapcache = page; 11029e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 11039e16b7fbSHugh Dickins if (unlikely(!page)) 11049e16b7fbSHugh Dickins return -ENOMEM; 11059e16b7fbSHugh Dickins 110600501b53SJohannes Weiner if (mem_cgroup_try_charge(page, vma->vm_mm, GFP_KERNEL, &memcg)) { 1107044d66c1SHugh Dickins ret = -ENOMEM; 110885d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 110985d9fc89SKAMEZAWA Hiroyuki } 1110044d66c1SHugh Dickins 1111044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1112179ef71cSCyrill Gorcunov if (unlikely(!maybe_same_pte(*pte, swp_entry_to_pte(entry)))) { 111300501b53SJohannes Weiner mem_cgroup_cancel_charge(page, memcg); 1114044d66c1SHugh Dickins ret = 0; 1115044d66c1SHugh Dickins goto out; 1116044d66c1SHugh Dickins } 11178a9f3ccdSBalbir Singh 1118b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1119d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 11201da177e4SLinus Torvalds get_page(page); 11211da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 11221da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 112300501b53SJohannes Weiner if (page == swapcache) { 11241da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 112500501b53SJohannes Weiner mem_cgroup_commit_charge(page, memcg, true); 112600501b53SJohannes Weiner } else { /* ksm created a completely new copy */ 11279e16b7fbSHugh Dickins page_add_new_anon_rmap(page, vma, addr); 112800501b53SJohannes Weiner mem_cgroup_commit_charge(page, memcg, false); 112900501b53SJohannes Weiner lru_cache_add_active_or_unevictable(page, vma); 113000501b53SJohannes Weiner } 11311da177e4SLinus Torvalds swap_free(entry); 11321da177e4SLinus Torvalds /* 11331da177e4SLinus Torvalds * Move the page to the active list so it is not 11341da177e4SLinus Torvalds * immediately swapped out again after swapon. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds activate_page(page); 1137044d66c1SHugh Dickins out: 1138044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 113985d9fc89SKAMEZAWA Hiroyuki out_nolock: 11409e16b7fbSHugh Dickins if (page != swapcache) { 11419e16b7fbSHugh Dickins unlock_page(page); 11429e16b7fbSHugh Dickins put_page(page); 11439e16b7fbSHugh Dickins } 1144044d66c1SHugh Dickins return ret; 11451da177e4SLinus Torvalds } 11461da177e4SLinus Torvalds 11471da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 11481da177e4SLinus Torvalds unsigned long addr, unsigned long end, 11491da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 11501da177e4SLinus Torvalds { 11511da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1152705e87c0SHugh Dickins pte_t *pte; 11538a9f3ccdSBalbir Singh int ret = 0; 11541da177e4SLinus Torvalds 1155044d66c1SHugh Dickins /* 1156044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1157044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1158044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1159044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1160044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1161044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 11622de1a7e4SSeth Jennings * preemptable whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1163044d66c1SHugh Dickins */ 1164044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 11651da177e4SLinus Torvalds do { 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 11681da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 11691da177e4SLinus Torvalds */ 1170179ef71cSCyrill Gorcunov if (unlikely(maybe_same_pte(*pte, swp_pte))) { 1171044d66c1SHugh Dickins pte_unmap(pte); 1172044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1173044d66c1SHugh Dickins if (ret) 1174044d66c1SHugh Dickins goto out; 1175044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1178044d66c1SHugh Dickins pte_unmap(pte - 1); 1179044d66c1SHugh Dickins out: 11808a9f3ccdSBalbir Singh return ret; 11811da177e4SLinus Torvalds } 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 11841da177e4SLinus Torvalds unsigned long addr, unsigned long end, 11851da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 11861da177e4SLinus Torvalds { 11871da177e4SLinus Torvalds pmd_t *pmd; 11881da177e4SLinus Torvalds unsigned long next; 11898a9f3ccdSBalbir Singh int ret; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11921da177e4SLinus Torvalds do { 11931da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 11941a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11951da177e4SLinus Torvalds continue; 11968a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 11978a9f3ccdSBalbir Singh if (ret) 11988a9f3ccdSBalbir Singh return ret; 11991da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 12001da177e4SLinus Torvalds return 0; 12011da177e4SLinus Torvalds } 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 12041da177e4SLinus Torvalds unsigned long addr, unsigned long end, 12051da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 12061da177e4SLinus Torvalds { 12071da177e4SLinus Torvalds pud_t *pud; 12081da177e4SLinus Torvalds unsigned long next; 12098a9f3ccdSBalbir Singh int ret; 12101da177e4SLinus Torvalds 12111da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 12121da177e4SLinus Torvalds do { 12131da177e4SLinus Torvalds next = pud_addr_end(addr, end); 12141da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 12151da177e4SLinus Torvalds continue; 12168a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 12178a9f3ccdSBalbir Singh if (ret) 12188a9f3ccdSBalbir Singh return ret; 12191da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 12201da177e4SLinus Torvalds return 0; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 12241da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 12251da177e4SLinus Torvalds { 12261da177e4SLinus Torvalds pgd_t *pgd; 12271da177e4SLinus Torvalds unsigned long addr, end, next; 12288a9f3ccdSBalbir Singh int ret; 12291da177e4SLinus Torvalds 12303ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 12311da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 12321da177e4SLinus Torvalds if (addr == -EFAULT) 12331da177e4SLinus Torvalds return 0; 12341da177e4SLinus Torvalds else 12351da177e4SLinus Torvalds end = addr + PAGE_SIZE; 12361da177e4SLinus Torvalds } else { 12371da177e4SLinus Torvalds addr = vma->vm_start; 12381da177e4SLinus Torvalds end = vma->vm_end; 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 12421da177e4SLinus Torvalds do { 12431da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 12441da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 12451da177e4SLinus Torvalds continue; 12468a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 12478a9f3ccdSBalbir Singh if (ret) 12488a9f3ccdSBalbir Singh return ret; 12491da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 12501da177e4SLinus Torvalds return 0; 12511da177e4SLinus Torvalds } 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 12541da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 12551da177e4SLinus Torvalds { 12561da177e4SLinus Torvalds struct vm_area_struct *vma; 12578a9f3ccdSBalbir Singh int ret = 0; 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 12601da177e4SLinus Torvalds /* 12617d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 12627d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 12631da177e4SLinus Torvalds */ 1264c475a8abSHugh Dickins activate_page(page); 12651da177e4SLinus Torvalds unlock_page(page); 12661da177e4SLinus Torvalds down_read(&mm->mmap_sem); 12671da177e4SLinus Torvalds lock_page(page); 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 12708a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 12711da177e4SLinus Torvalds break; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds up_read(&mm->mmap_sem); 12748a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* 127838b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 127938b5faf4SDan Magenheimer * from current position to next entry still in use. 12801da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 12811da177e4SLinus Torvalds */ 12826eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 128338b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 12841da177e4SLinus Torvalds { 12856eb396dcSHugh Dickins unsigned int max = si->max; 12866eb396dcSHugh Dickins unsigned int i = prev; 12878d69aaeeSHugh Dickins unsigned char count; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds /* 12905d337b91SHugh Dickins * No need for swap_lock here: we're just looking 12911da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 12921da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 12935d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 12941da177e4SLinus Torvalds */ 12951da177e4SLinus Torvalds for (;;) { 12961da177e4SLinus Torvalds if (++i >= max) { 12971da177e4SLinus Torvalds if (!prev) { 12981da177e4SLinus Torvalds i = 0; 12991da177e4SLinus Torvalds break; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds /* 13021da177e4SLinus Torvalds * No entries in use at top of swap_map, 13031da177e4SLinus Torvalds * loop back to start and recheck there. 13041da177e4SLinus Torvalds */ 13051da177e4SLinus Torvalds max = prev + 1; 13061da177e4SLinus Torvalds prev = 0; 13071da177e4SLinus Torvalds i = 1; 13081da177e4SLinus Torvalds } 130938b5faf4SDan Magenheimer if (frontswap) { 131038b5faf4SDan Magenheimer if (frontswap_test(si, i)) 131138b5faf4SDan Magenheimer break; 131238b5faf4SDan Magenheimer else 131338b5faf4SDan Magenheimer continue; 131438b5faf4SDan Magenheimer } 1315edfe23daSShaohua Li count = ACCESS_ONCE(si->swap_map[i]); 1316355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 13171da177e4SLinus Torvalds break; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds return i; 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 13241da177e4SLinus Torvalds * and then search for the process using it. All the necessary 13251da177e4SLinus Torvalds * page table adjustments can then be made atomically. 132638b5faf4SDan Magenheimer * 132738b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 132838b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 13291da177e4SLinus Torvalds */ 133038b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 133138b5faf4SDan Magenheimer unsigned long pages_to_unuse) 13321da177e4SLinus Torvalds { 1333efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 13341da177e4SLinus Torvalds struct mm_struct *start_mm; 1335edfe23daSShaohua Li volatile unsigned char *swap_map; /* swap_map is accessed without 1336edfe23daSShaohua Li * locking. Mark it as volatile 1337edfe23daSShaohua Li * to prevent compiler doing 1338edfe23daSShaohua Li * something odd. 1339edfe23daSShaohua Li */ 13408d69aaeeSHugh Dickins unsigned char swcount; 13411da177e4SLinus Torvalds struct page *page; 13421da177e4SLinus Torvalds swp_entry_t entry; 13436eb396dcSHugh Dickins unsigned int i = 0; 13441da177e4SLinus Torvalds int retval = 0; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* 13471da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 13481da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 13491da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 13501da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 13511da177e4SLinus Torvalds * 13521da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 13531da177e4SLinus Torvalds * freed the previous entry from; but that would take less 13541da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 13551da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 13561da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 13571da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 1358570a335bSHugh Dickins * that. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds start_mm = &init_mm; 13611da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds /* 13641da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 13651da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 13661da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 13671da177e4SLinus Torvalds */ 136838b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 13691da177e4SLinus Torvalds if (signal_pending(current)) { 13701da177e4SLinus Torvalds retval = -EINTR; 13711da177e4SLinus Torvalds break; 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* 13751da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 13761da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 13771da177e4SLinus Torvalds * page and read the swap into it. 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 13801da177e4SLinus Torvalds entry = swp_entry(type, i); 138102098feaSHugh Dickins page = read_swap_cache_async(entry, 138202098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 13831da177e4SLinus Torvalds if (!page) { 13841da177e4SLinus Torvalds /* 13851da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 13861da177e4SLinus Torvalds * has been freed independently, and will not be 13871da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 13881da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 13891da177e4SLinus Torvalds */ 1390edfe23daSShaohua Li swcount = *swap_map; 1391edfe23daSShaohua Li /* 1392edfe23daSShaohua Li * We don't hold lock here, so the swap entry could be 1393edfe23daSShaohua Li * SWAP_MAP_BAD (when the cluster is discarding). 1394edfe23daSShaohua Li * Instead of fail out, We can just skip the swap 1395edfe23daSShaohua Li * entry because swapoff will wait for discarding 1396edfe23daSShaohua Li * finish anyway. 1397edfe23daSShaohua Li */ 1398edfe23daSShaohua Li if (!swcount || swcount == SWAP_MAP_BAD) 13991da177e4SLinus Torvalds continue; 14001da177e4SLinus Torvalds retval = -ENOMEM; 14011da177e4SLinus Torvalds break; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /* 14051da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 14061da177e4SLinus Torvalds */ 14071da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 14081da177e4SLinus Torvalds mmput(start_mm); 14091da177e4SLinus Torvalds start_mm = &init_mm; 14101da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 14151da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 14161da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 14171da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 14181da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 14191da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 14201da177e4SLinus Torvalds */ 14211da177e4SLinus Torvalds wait_on_page_locked(page); 14221da177e4SLinus Torvalds wait_on_page_writeback(page); 14231da177e4SLinus Torvalds lock_page(page); 14241da177e4SLinus Torvalds wait_on_page_writeback(page); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds /* 14271da177e4SLinus Torvalds * Remove all references to entry. 14281da177e4SLinus Torvalds */ 14291da177e4SLinus Torvalds swcount = *swap_map; 1430aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 1431aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 1432aaa46865SHugh Dickins /* page has already been unlocked and released */ 1433aaa46865SHugh Dickins if (retval < 0) 1434aaa46865SHugh Dickins break; 1435aaa46865SHugh Dickins continue; 14361da177e4SLinus Torvalds } 1437aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 1438aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 1439aaa46865SHugh Dickins 1440355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 14411da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 14421da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 14431da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 14441da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 14451da177e4SLinus Torvalds struct mm_struct *mm; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 14481da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 14491da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1450aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 14511da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 14521da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 145370af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 14541da177e4SLinus Torvalds continue; 14551da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 14561da177e4SLinus Torvalds mmput(prev_mm); 14571da177e4SLinus Torvalds prev_mm = mm; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds cond_resched(); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds swcount = *swap_map; 1462355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 14631da177e4SLinus Torvalds ; 1464aaa46865SHugh Dickins else if (mm == &init_mm) 14651da177e4SLinus Torvalds set_start_mm = 1; 1466aaa46865SHugh Dickins else 14671da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1468355cfa73SKAMEZAWA Hiroyuki 146932c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 14701da177e4SLinus Torvalds mmput(new_start_mm); 14711da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 14721da177e4SLinus Torvalds new_start_mm = mm; 14731da177e4SLinus Torvalds set_start_mm = 0; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds spin_lock(&mmlist_lock); 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 14781da177e4SLinus Torvalds mmput(prev_mm); 14791da177e4SLinus Torvalds mmput(start_mm); 14801da177e4SLinus Torvalds start_mm = new_start_mm; 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds if (retval) { 14831da177e4SLinus Torvalds unlock_page(page); 14841da177e4SLinus Torvalds page_cache_release(page); 14851da177e4SLinus Torvalds break; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* 14891da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 14901da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 14911da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 14921da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 14931da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 14941da177e4SLinus Torvalds * delete from cache even if there's another reference, 14951da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 14961da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 14971da177e4SLinus Torvalds * read from disk into another page. Splitting into two 14981da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 14991da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 15005ad64688SHugh Dickins * 15015ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 15025ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 15035ad64688SHugh Dickins * this splitting happens to be just what is needed to 15045ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 15055ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 15061da177e4SLinus Torvalds */ 1507355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 1508355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 15091da177e4SLinus Torvalds struct writeback_control wbc = { 15101da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 15111da177e4SLinus Torvalds }; 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds swap_writepage(page, &wbc); 15141da177e4SLinus Torvalds lock_page(page); 15151da177e4SLinus Torvalds wait_on_page_writeback(page); 15161da177e4SLinus Torvalds } 151768bdc8d6SHugh Dickins 151868bdc8d6SHugh Dickins /* 151968bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 152068bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 152168bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 152268bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 152368bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 152468bdc8d6SHugh Dickins */ 152568bdc8d6SHugh Dickins if (PageSwapCache(page) && 152668bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 15271da177e4SLinus Torvalds delete_from_swap_cache(page); 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds /* 15301da177e4SLinus Torvalds * So we could skip searching mms once swap count went 15311da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 15322706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 15331da177e4SLinus Torvalds */ 15341da177e4SLinus Torvalds SetPageDirty(page); 15351da177e4SLinus Torvalds unlock_page(page); 15361da177e4SLinus Torvalds page_cache_release(page); 15371da177e4SLinus Torvalds 15381da177e4SLinus Torvalds /* 15391da177e4SLinus Torvalds * Make sure that we aren't completely killing 15401da177e4SLinus Torvalds * interactive performance. 15411da177e4SLinus Torvalds */ 15421da177e4SLinus Torvalds cond_resched(); 154338b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 154438b5faf4SDan Magenheimer if (!--pages_to_unuse) 154538b5faf4SDan Magenheimer break; 154638b5faf4SDan Magenheimer } 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds mmput(start_mm); 15501da177e4SLinus Torvalds return retval; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds /* 15545d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 15555d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 15565d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 15571da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 15581da177e4SLinus Torvalds */ 15591da177e4SLinus Torvalds static void drain_mmlist(void) 15601da177e4SLinus Torvalds { 15611da177e4SLinus Torvalds struct list_head *p, *next; 1562efa90a98SHugh Dickins unsigned int type; 15631da177e4SLinus Torvalds 1564efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 1565efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 15661da177e4SLinus Torvalds return; 15671da177e4SLinus Torvalds spin_lock(&mmlist_lock); 15681da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 15691da177e4SLinus Torvalds list_del_init(p); 15701da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 15711da177e4SLinus Torvalds } 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds /* 15741da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 1575d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 1576d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 1577d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 15781da177e4SLinus Torvalds */ 1579d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 15801da177e4SLinus Torvalds { 1581f29ad6a9SHugh Dickins struct swap_info_struct *sis; 1582f29ad6a9SHugh Dickins struct swap_extent *start_se; 1583f29ad6a9SHugh Dickins struct swap_extent *se; 1584f29ad6a9SHugh Dickins pgoff_t offset; 1585f29ad6a9SHugh Dickins 1586efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 1587f29ad6a9SHugh Dickins *bdev = sis->bdev; 1588f29ad6a9SHugh Dickins 1589f29ad6a9SHugh Dickins offset = swp_offset(entry); 1590f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 1591f29ad6a9SHugh Dickins se = start_se; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds for ( ; ; ) { 15941da177e4SLinus Torvalds struct list_head *lh; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds if (se->start_page <= offset && 15971da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 15981da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 15991da177e4SLinus Torvalds } 160011d31886SHugh Dickins lh = se->list.next; 16011da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 16021da177e4SLinus Torvalds sis->curr_swap_extent = se; 16031da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* 1608d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 1609d4906e1aSLee Schermerhorn */ 1610d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 1611d4906e1aSLee Schermerhorn { 1612d4906e1aSLee Schermerhorn swp_entry_t entry; 1613d4906e1aSLee Schermerhorn entry.val = page_private(page); 1614d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 1615d4906e1aSLee Schermerhorn } 1616d4906e1aSLee Schermerhorn 1617d4906e1aSLee Schermerhorn /* 16181da177e4SLinus Torvalds * Free all of a swapdev's extent information 16191da177e4SLinus Torvalds */ 16201da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 16211da177e4SLinus Torvalds { 16229625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 16231da177e4SLinus Torvalds struct swap_extent *se; 16241da177e4SLinus Torvalds 16259625a5f2SHugh Dickins se = list_entry(sis->first_swap_extent.list.next, 16261da177e4SLinus Torvalds struct swap_extent, list); 16271da177e4SLinus Torvalds list_del(&se->list); 16281da177e4SLinus Torvalds kfree(se); 16291da177e4SLinus Torvalds } 163062c230bcSMel Gorman 163162c230bcSMel Gorman if (sis->flags & SWP_FILE) { 163262c230bcSMel Gorman struct file *swap_file = sis->swap_file; 163362c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 163462c230bcSMel Gorman 163562c230bcSMel Gorman sis->flags &= ~SWP_FILE; 163662c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 163762c230bcSMel Gorman } 16381da177e4SLinus Torvalds } 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 164211d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 16431da177e4SLinus Torvalds * 164411d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 16451da177e4SLinus Torvalds */ 1646a509bc1aSMel Gorman int 16471da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 16481da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds struct swap_extent *se; 16511da177e4SLinus Torvalds struct swap_extent *new_se; 16521da177e4SLinus Torvalds struct list_head *lh; 16531da177e4SLinus Torvalds 16549625a5f2SHugh Dickins if (start_page == 0) { 16559625a5f2SHugh Dickins se = &sis->first_swap_extent; 16569625a5f2SHugh Dickins sis->curr_swap_extent = se; 16579625a5f2SHugh Dickins se->start_page = 0; 16589625a5f2SHugh Dickins se->nr_pages = nr_pages; 16599625a5f2SHugh Dickins se->start_block = start_block; 16609625a5f2SHugh Dickins return 1; 16619625a5f2SHugh Dickins } else { 16629625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 16631da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 166411d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 166511d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 16661da177e4SLinus Torvalds /* Merge it */ 16671da177e4SLinus Torvalds se->nr_pages += nr_pages; 16681da177e4SLinus Torvalds return 0; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* 16731da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 16741da177e4SLinus Torvalds */ 16751da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 16761da177e4SLinus Torvalds if (new_se == NULL) 16771da177e4SLinus Torvalds return -ENOMEM; 16781da177e4SLinus Torvalds new_se->start_page = start_page; 16791da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 16801da177e4SLinus Torvalds new_se->start_block = start_block; 16811da177e4SLinus Torvalds 16829625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 168353092a74SHugh Dickins return 1; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds /* 16871da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 16881da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 16891da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 16901da177e4SLinus Torvalds * time for locating where on disk a page belongs. 16911da177e4SLinus Torvalds * 16921da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 16931da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 16941da177e4SLinus Torvalds * swap files identically. 16951da177e4SLinus Torvalds * 16961da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 16971da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 16981da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 16991da177e4SLinus Torvalds * 17001da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 17011da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 17021da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 17031da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 17041da177e4SLinus Torvalds * for swapping. 17051da177e4SLinus Torvalds * 1706b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 17071da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 17081da177e4SLinus Torvalds * which will scribble on the fs. 17091da177e4SLinus Torvalds * 17101da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 17111da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 17121da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 17131da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 17141da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 17151da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 17161da177e4SLinus Torvalds */ 171753092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 17181da177e4SLinus Torvalds { 171962c230bcSMel Gorman struct file *swap_file = sis->swap_file; 172062c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 172162c230bcSMel Gorman struct inode *inode = mapping->host; 17221da177e4SLinus Torvalds int ret; 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 17251da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 172653092a74SHugh Dickins *span = sis->pages; 1727a509bc1aSMel Gorman return ret; 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds 173062c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 1731a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 173262c230bcSMel Gorman if (!ret) { 173362c230bcSMel Gorman sis->flags |= SWP_FILE; 173462c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 173562c230bcSMel Gorman *span = sis->pages; 173662c230bcSMel Gorman } 17379625a5f2SHugh Dickins return ret; 1738a509bc1aSMel Gorman } 1739a509bc1aSMel Gorman 1740a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 1743cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 17442a8f9449SShaohua Li unsigned char *swap_map, 17452a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 174640531542SCesar Eduardo Barros { 174740531542SCesar Eduardo Barros if (prio >= 0) 174840531542SCesar Eduardo Barros p->prio = prio; 174940531542SCesar Eduardo Barros else 175040531542SCesar Eduardo Barros p->prio = --least_priority; 175118ab4d4cSDan Streetman /* 175218ab4d4cSDan Streetman * the plist prio is negated because plist ordering is 175318ab4d4cSDan Streetman * low-to-high, while swap ordering is high-to-low 175418ab4d4cSDan Streetman */ 175518ab4d4cSDan Streetman p->list.prio = -p->prio; 175618ab4d4cSDan Streetman p->avail_list.prio = -p->prio; 175740531542SCesar Eduardo Barros p->swap_map = swap_map; 17582a8f9449SShaohua Li p->cluster_info = cluster_info; 175940531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 1760ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 176140531542SCesar Eduardo Barros total_swap_pages += p->pages; 176240531542SCesar Eduardo Barros 1763adfab836SDan Streetman assert_spin_locked(&swap_lock); 1764adfab836SDan Streetman /* 176518ab4d4cSDan Streetman * both lists are plists, and thus priority ordered. 176618ab4d4cSDan Streetman * swap_active_head needs to be priority ordered for swapoff(), 176718ab4d4cSDan Streetman * which on removal of any swap_info_struct with an auto-assigned 176818ab4d4cSDan Streetman * (i.e. negative) priority increments the auto-assigned priority 176918ab4d4cSDan Streetman * of any lower-priority swap_info_structs. 177018ab4d4cSDan Streetman * swap_avail_head needs to be priority ordered for get_swap_page(), 177118ab4d4cSDan Streetman * which allocates swap pages from the highest available priority 177218ab4d4cSDan Streetman * swap_info_struct. 1773adfab836SDan Streetman */ 177418ab4d4cSDan Streetman plist_add(&p->list, &swap_active_head); 177518ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 177618ab4d4cSDan Streetman plist_add(&p->avail_list, &swap_avail_head); 177718ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 1778cf0cac0aSCesar Eduardo Barros } 1779cf0cac0aSCesar Eduardo Barros 1780cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 1781cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 17822a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 1783cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 1784cf0cac0aSCesar Eduardo Barros { 17854f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 1786cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1787ec8acf20SShaohua Li spin_lock(&p->lock); 17882a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 1789ec8acf20SShaohua Li spin_unlock(&p->lock); 1790cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 1791cf0cac0aSCesar Eduardo Barros } 1792cf0cac0aSCesar Eduardo Barros 1793cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 1794cf0cac0aSCesar Eduardo Barros { 1795cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1796ec8acf20SShaohua Li spin_lock(&p->lock); 17972a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 1798ec8acf20SShaohua Li spin_unlock(&p->lock); 179940531542SCesar Eduardo Barros spin_unlock(&swap_lock); 180040531542SCesar Eduardo Barros } 180140531542SCesar Eduardo Barros 1802c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 18031da177e4SLinus Torvalds { 18041da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 18058d69aaeeSHugh Dickins unsigned char *swap_map; 18062a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 18074f89849dSMinchan Kim unsigned long *frontswap_map; 18081da177e4SLinus Torvalds struct file *swap_file, *victim; 18091da177e4SLinus Torvalds struct address_space *mapping; 18101da177e4SLinus Torvalds struct inode *inode; 181191a27b2aSJeff Layton struct filename *pathname; 1812adfab836SDan Streetman int err, found = 0; 18135b808a23SKrzysztof Kozlowski unsigned int old_block_size; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 18161da177e4SLinus Torvalds return -EPERM; 18171da177e4SLinus Torvalds 1818191c5424SAl Viro BUG_ON(!current->mm); 1819191c5424SAl Viro 18201da177e4SLinus Torvalds pathname = getname(specialfile); 18211da177e4SLinus Torvalds if (IS_ERR(pathname)) 1822f58b59c1SXiaotian Feng return PTR_ERR(pathname); 18231da177e4SLinus Torvalds 1824669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 18251da177e4SLinus Torvalds err = PTR_ERR(victim); 18261da177e4SLinus Torvalds if (IS_ERR(victim)) 18271da177e4SLinus Torvalds goto out; 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds mapping = victim->f_mapping; 18305d337b91SHugh Dickins spin_lock(&swap_lock); 183118ab4d4cSDan Streetman plist_for_each_entry(p, &swap_active_head, list) { 183222c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 1833adfab836SDan Streetman if (p->swap_file->f_mapping == mapping) { 1834adfab836SDan Streetman found = 1; 18351da177e4SLinus Torvalds break; 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds } 1838adfab836SDan Streetman } 1839adfab836SDan Streetman if (!found) { 18401da177e4SLinus Torvalds err = -EINVAL; 18415d337b91SHugh Dickins spin_unlock(&swap_lock); 18421da177e4SLinus Torvalds goto out_dput; 18431da177e4SLinus Torvalds } 1844191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 18451da177e4SLinus Torvalds vm_unacct_memory(p->pages); 18461da177e4SLinus Torvalds else { 18471da177e4SLinus Torvalds err = -ENOMEM; 18485d337b91SHugh Dickins spin_unlock(&swap_lock); 18491da177e4SLinus Torvalds goto out_dput; 18501da177e4SLinus Torvalds } 185118ab4d4cSDan Streetman spin_lock(&swap_avail_lock); 185218ab4d4cSDan Streetman plist_del(&p->avail_list, &swap_avail_head); 185318ab4d4cSDan Streetman spin_unlock(&swap_avail_lock); 1854ec8acf20SShaohua Li spin_lock(&p->lock); 185578ecba08SHugh Dickins if (p->prio < 0) { 1856adfab836SDan Streetman struct swap_info_struct *si = p; 1857adfab836SDan Streetman 185818ab4d4cSDan Streetman plist_for_each_entry_continue(si, &swap_active_head, list) { 1859adfab836SDan Streetman si->prio++; 186018ab4d4cSDan Streetman si->list.prio--; 186118ab4d4cSDan Streetman si->avail_list.prio--; 1862adfab836SDan Streetman } 186378ecba08SHugh Dickins least_priority++; 186478ecba08SHugh Dickins } 186518ab4d4cSDan Streetman plist_del(&p->list, &swap_active_head); 1866ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 18671da177e4SLinus Torvalds total_swap_pages -= p->pages; 18681da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 1869ec8acf20SShaohua Li spin_unlock(&p->lock); 18705d337b91SHugh Dickins spin_unlock(&swap_lock); 1871fb4f88dcSHugh Dickins 1872e1e12d2fSDavid Rientjes set_current_oom_origin(); 1873adfab836SDan Streetman err = try_to_unuse(p->type, false, 0); /* force unuse all pages */ 1874e1e12d2fSDavid Rientjes clear_current_oom_origin(); 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds if (err) { 18771da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 1878cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 18791da177e4SLinus Torvalds goto out_dput; 18801da177e4SLinus Torvalds } 188152b7efdbSHugh Dickins 1882815c2c54SShaohua Li flush_work(&p->discard_work); 1883815c2c54SShaohua Li 18844cd3bb10SHugh Dickins destroy_swap_extents(p); 1885570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 1886570a335bSHugh Dickins free_swap_count_continuations(p); 1887570a335bSHugh Dickins 1888fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 18895d337b91SHugh Dickins spin_lock(&swap_lock); 1890ec8acf20SShaohua Li spin_lock(&p->lock); 18911da177e4SLinus Torvalds drain_mmlist(); 18925d337b91SHugh Dickins 18935d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 18945d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 18955d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 1896ec8acf20SShaohua Li spin_unlock(&p->lock); 18975d337b91SHugh Dickins spin_unlock(&swap_lock); 189813e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 18995d337b91SHugh Dickins spin_lock(&swap_lock); 1900ec8acf20SShaohua Li spin_lock(&p->lock); 19015d337b91SHugh Dickins } 19025d337b91SHugh Dickins 19031da177e4SLinus Torvalds swap_file = p->swap_file; 19045b808a23SKrzysztof Kozlowski old_block_size = p->old_block_size; 19051da177e4SLinus Torvalds p->swap_file = NULL; 19061da177e4SLinus Torvalds p->max = 0; 19071da177e4SLinus Torvalds swap_map = p->swap_map; 19081da177e4SLinus Torvalds p->swap_map = NULL; 19092a8f9449SShaohua Li cluster_info = p->cluster_info; 19102a8f9449SShaohua Li p->cluster_info = NULL; 19114f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 1912ec8acf20SShaohua Li spin_unlock(&p->lock); 19135d337b91SHugh Dickins spin_unlock(&swap_lock); 1914adfab836SDan Streetman frontswap_invalidate_area(p->type); 191558e97ba6SKrzysztof Kozlowski frontswap_map_set(p, NULL); 1916fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 1917ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 1918ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 19191da177e4SLinus Torvalds vfree(swap_map); 19202a8f9449SShaohua Li vfree(cluster_info); 19214f89849dSMinchan Kim vfree(frontswap_map); 19222de1a7e4SSeth Jennings /* Destroy swap account information */ 1923adfab836SDan Streetman swap_cgroup_swapoff(p->type); 192427a7faa0SKAMEZAWA Hiroyuki 19251da177e4SLinus Torvalds inode = mapping->host; 19261da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 19271da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 19285b808a23SKrzysztof Kozlowski set_blocksize(bdev, old_block_size); 1929e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 19301da177e4SLinus Torvalds } else { 19311b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 19321da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 19331b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds filp_close(swap_file, NULL); 1936f893ab41SWeijie Yang 1937f893ab41SWeijie Yang /* 1938f893ab41SWeijie Yang * Clear the SWP_USED flag after all resources are freed so that swapon 1939f893ab41SWeijie Yang * can reuse this swap_info in alloc_swap_info() safely. It is ok to 1940f893ab41SWeijie Yang * not hold p->lock after we cleared its SWP_WRITEOK. 1941f893ab41SWeijie Yang */ 1942f893ab41SWeijie Yang spin_lock(&swap_lock); 1943f893ab41SWeijie Yang p->flags = 0; 1944f893ab41SWeijie Yang spin_unlock(&swap_lock); 1945f893ab41SWeijie Yang 19461da177e4SLinus Torvalds err = 0; 194766d7dd51SKay Sievers atomic_inc(&proc_poll_event); 194866d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 19491da177e4SLinus Torvalds 19501da177e4SLinus Torvalds out_dput: 19511da177e4SLinus Torvalds filp_close(victim, NULL); 19521da177e4SLinus Torvalds out: 1953f58b59c1SXiaotian Feng putname(pathname); 19541da177e4SLinus Torvalds return err; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 195866d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 195966d7dd51SKay Sievers { 1960f1514638SKay Sievers struct seq_file *seq = file->private_data; 196166d7dd51SKay Sievers 196266d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 196366d7dd51SKay Sievers 1964f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 1965f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 196666d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 196766d7dd51SKay Sievers } 196866d7dd51SKay Sievers 196966d7dd51SKay Sievers return POLLIN | POLLRDNORM; 197066d7dd51SKay Sievers } 197166d7dd51SKay Sievers 19721da177e4SLinus Torvalds /* iterator */ 19731da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 19741da177e4SLinus Torvalds { 1975efa90a98SHugh Dickins struct swap_info_struct *si; 1976efa90a98SHugh Dickins int type; 19771da177e4SLinus Torvalds loff_t l = *pos; 19781da177e4SLinus Torvalds 1979fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 19801da177e4SLinus Torvalds 1981881e4aabSSuleiman Souhlal if (!l) 1982881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1983881e4aabSSuleiman Souhlal 1984efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1985efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 1986efa90a98SHugh Dickins si = swap_info[type]; 1987efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 19881da177e4SLinus Torvalds continue; 1989881e4aabSSuleiman Souhlal if (!--l) 1990efa90a98SHugh Dickins return si; 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 19931da177e4SLinus Torvalds return NULL; 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds 19961da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 19971da177e4SLinus Torvalds { 1998efa90a98SHugh Dickins struct swap_info_struct *si = v; 1999efa90a98SHugh Dickins int type; 20001da177e4SLinus Torvalds 2001881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 2002efa90a98SHugh Dickins type = 0; 2003efa90a98SHugh Dickins else 2004efa90a98SHugh Dickins type = si->type + 1; 2005881e4aabSSuleiman Souhlal 2006efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 2007efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 2008efa90a98SHugh Dickins si = swap_info[type]; 2009efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 20101da177e4SLinus Torvalds continue; 20111da177e4SLinus Torvalds ++*pos; 2012efa90a98SHugh Dickins return si; 20131da177e4SLinus Torvalds } 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds return NULL; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 20191da177e4SLinus Torvalds { 2020fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 20211da177e4SLinus Torvalds } 20221da177e4SLinus Torvalds 20231da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 20241da177e4SLinus Torvalds { 2025efa90a98SHugh Dickins struct swap_info_struct *si = v; 20261da177e4SLinus Torvalds struct file *file; 20271da177e4SLinus Torvalds int len; 20281da177e4SLinus Torvalds 2029efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 20301da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 2031881e4aabSSuleiman Souhlal return 0; 2032881e4aabSSuleiman Souhlal } 20331da177e4SLinus Torvalds 2034efa90a98SHugh Dickins file = si->swap_file; 2035c32c2f63SJan Blunck len = seq_path(swap, &file->f_path, " \t\n\\"); 20366eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 20371da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 2038496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 20391da177e4SLinus Torvalds "partition" : "file\t", 2040efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 2041efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 2042efa90a98SHugh Dickins si->prio); 20431da177e4SLinus Torvalds return 0; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 204615ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 20471da177e4SLinus Torvalds .start = swap_start, 20481da177e4SLinus Torvalds .next = swap_next, 20491da177e4SLinus Torvalds .stop = swap_stop, 20501da177e4SLinus Torvalds .show = swap_show 20511da177e4SLinus Torvalds }; 20521da177e4SLinus Torvalds 20531da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 20541da177e4SLinus Torvalds { 2055f1514638SKay Sievers struct seq_file *seq; 205666d7dd51SKay Sievers int ret; 205766d7dd51SKay Sievers 205866d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 2059f1514638SKay Sievers if (ret) 206066d7dd51SKay Sievers return ret; 206166d7dd51SKay Sievers 2062f1514638SKay Sievers seq = file->private_data; 2063f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 2064f1514638SKay Sievers return 0; 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 206715ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 20681da177e4SLinus Torvalds .open = swaps_open, 20691da177e4SLinus Torvalds .read = seq_read, 20701da177e4SLinus Torvalds .llseek = seq_lseek, 20711da177e4SLinus Torvalds .release = seq_release, 207266d7dd51SKay Sievers .poll = swaps_poll, 20731da177e4SLinus Torvalds }; 20741da177e4SLinus Torvalds 20751da177e4SLinus Torvalds static int __init procswaps_init(void) 20761da177e4SLinus Torvalds { 20773d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 20781da177e4SLinus Torvalds return 0; 20791da177e4SLinus Torvalds } 20801da177e4SLinus Torvalds __initcall(procswaps_init); 20811da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 20821da177e4SLinus Torvalds 20831796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 20841796316aSJan Beulich static int __init max_swapfiles_check(void) 20851796316aSJan Beulich { 20861796316aSJan Beulich MAX_SWAPFILES_CHECK(); 20871796316aSJan Beulich return 0; 20881796316aSJan Beulich } 20891796316aSJan Beulich late_initcall(max_swapfiles_check); 20901796316aSJan Beulich #endif 20911796316aSJan Beulich 209253cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 20931da177e4SLinus Torvalds { 20941da177e4SLinus Torvalds struct swap_info_struct *p; 20951da177e4SLinus Torvalds unsigned int type; 2096efa90a98SHugh Dickins 2097efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2098efa90a98SHugh Dickins if (!p) 209953cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2100efa90a98SHugh Dickins 21015d337b91SHugh Dickins spin_lock(&swap_lock); 2102efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2103efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 21041da177e4SLinus Torvalds break; 2105efa90a98SHugh Dickins } 21060697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 21075d337b91SHugh Dickins spin_unlock(&swap_lock); 2108efa90a98SHugh Dickins kfree(p); 2109730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 21101da177e4SLinus Torvalds } 2111efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2112efa90a98SHugh Dickins p->type = type; 2113efa90a98SHugh Dickins swap_info[type] = p; 2114efa90a98SHugh Dickins /* 2115efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2116efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2117efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2118efa90a98SHugh Dickins */ 2119efa90a98SHugh Dickins smp_wmb(); 2120efa90a98SHugh Dickins nr_swapfiles++; 2121efa90a98SHugh Dickins } else { 2122efa90a98SHugh Dickins kfree(p); 2123efa90a98SHugh Dickins p = swap_info[type]; 2124efa90a98SHugh Dickins /* 2125efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2126efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2127efa90a98SHugh Dickins */ 2128efa90a98SHugh Dickins } 21299625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 213018ab4d4cSDan Streetman plist_node_init(&p->list, 0); 213118ab4d4cSDan Streetman plist_node_init(&p->avail_list, 0); 21321da177e4SLinus Torvalds p->flags = SWP_USED; 21335d337b91SHugh Dickins spin_unlock(&swap_lock); 2134ec8acf20SShaohua Li spin_lock_init(&p->lock); 2135efa90a98SHugh Dickins 213653cbb243SCesar Eduardo Barros return p; 213753cbb243SCesar Eduardo Barros } 213853cbb243SCesar Eduardo Barros 21394d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 21404d0e1e10SCesar Eduardo Barros { 21414d0e1e10SCesar Eduardo Barros int error; 21424d0e1e10SCesar Eduardo Barros 21434d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 21444d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 21454d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 21464d0e1e10SCesar Eduardo Barros FMODE_READ | FMODE_WRITE | FMODE_EXCL, 21474d0e1e10SCesar Eduardo Barros sys_swapon); 21484d0e1e10SCesar Eduardo Barros if (error < 0) { 21494d0e1e10SCesar Eduardo Barros p->bdev = NULL; 215087ade72aSCesar Eduardo Barros return -EINVAL; 21514d0e1e10SCesar Eduardo Barros } 21524d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 21534d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 21544d0e1e10SCesar Eduardo Barros if (error < 0) 215587ade72aSCesar Eduardo Barros return error; 21564d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 21574d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 21584d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 21594d0e1e10SCesar Eduardo Barros mutex_lock(&inode->i_mutex); 216087ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 216187ade72aSCesar Eduardo Barros return -EBUSY; 216287ade72aSCesar Eduardo Barros } else 216387ade72aSCesar Eduardo Barros return -EINVAL; 21644d0e1e10SCesar Eduardo Barros 21654d0e1e10SCesar Eduardo Barros return 0; 21664d0e1e10SCesar Eduardo Barros } 21674d0e1e10SCesar Eduardo Barros 2168ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2169ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2170ca8bd38bSCesar Eduardo Barros struct inode *inode) 2171ca8bd38bSCesar Eduardo Barros { 2172ca8bd38bSCesar Eduardo Barros int i; 2173ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2174ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2175d6bbbd29SRaymond Jennings unsigned long last_page; 2176ca8bd38bSCesar Eduardo Barros 2177ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2178465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 217938719025SCesar Eduardo Barros return 0; 2180ca8bd38bSCesar Eduardo Barros } 2181ca8bd38bSCesar Eduardo Barros 2182ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2183ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2184ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2185ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2186ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2187ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2188ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2189ca8bd38bSCesar Eduardo Barros } 2190ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2191ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2192465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2193ca8bd38bSCesar Eduardo Barros swap_header->info.version); 219438719025SCesar Eduardo Barros return 0; 2195ca8bd38bSCesar Eduardo Barros } 2196ca8bd38bSCesar Eduardo Barros 2197ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2198ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2199ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2200ca8bd38bSCesar Eduardo Barros 2201ca8bd38bSCesar Eduardo Barros /* 2202ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 22039b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2204a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2205a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 22069b15b817SHugh Dickins * different architectures. In order to find the 2207a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2208ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2209a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2210ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2211ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2212ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 22139b15b817SHugh Dickins * swap pte. 2214ca8bd38bSCesar Eduardo Barros */ 2215ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 22169b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2217d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2218d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2219465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2220d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2221d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2222d6bbbd29SRaymond Jennings } 2223d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2224d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2225ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2226ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2227ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2228ca8bd38bSCesar Eduardo Barros } 2229ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2230ca8bd38bSCesar Eduardo Barros 2231ca8bd38bSCesar Eduardo Barros if (!maxpages) 223238719025SCesar Eduardo Barros return 0; 2233ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2234ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2235465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 223638719025SCesar Eduardo Barros return 0; 2237ca8bd38bSCesar Eduardo Barros } 2238ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 223938719025SCesar Eduardo Barros return 0; 2240ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 224138719025SCesar Eduardo Barros return 0; 2242ca8bd38bSCesar Eduardo Barros 2243ca8bd38bSCesar Eduardo Barros return maxpages; 2244ca8bd38bSCesar Eduardo Barros } 2245ca8bd38bSCesar Eduardo Barros 2246915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2247915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2248915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 22492a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2250915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2251915d4d7bSCesar Eduardo Barros sector_t *span) 2252915d4d7bSCesar Eduardo Barros { 2253915d4d7bSCesar Eduardo Barros int i; 2254915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 2255915d4d7bSCesar Eduardo Barros int nr_extents; 22562a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 22572a8f9449SShaohua Li unsigned long idx = p->cluster_next / SWAPFILE_CLUSTER; 2258915d4d7bSCesar Eduardo Barros 2259915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 2260915d4d7bSCesar Eduardo Barros 22612a8f9449SShaohua Li cluster_set_null(&p->free_cluster_head); 22622a8f9449SShaohua Li cluster_set_null(&p->free_cluster_tail); 2263815c2c54SShaohua Li cluster_set_null(&p->discard_cluster_head); 2264815c2c54SShaohua Li cluster_set_null(&p->discard_cluster_tail); 22652a8f9449SShaohua Li 2266915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 2267915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 2268bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 2269bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2270915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 2271915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 2272915d4d7bSCesar Eduardo Barros nr_good_pages--; 22732a8f9449SShaohua Li /* 22742a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 22752a8f9449SShaohua Li * operation involved 22762a8f9449SShaohua Li */ 22772a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 2278915d4d7bSCesar Eduardo Barros } 2279915d4d7bSCesar Eduardo Barros } 2280915d4d7bSCesar Eduardo Barros 22812a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 22822a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 22832a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 22842a8f9449SShaohua Li 2285915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 2286915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 22872a8f9449SShaohua Li /* 22882a8f9449SShaohua Li * Not mark the cluster free yet, no list 22892a8f9449SShaohua Li * operation involved 22902a8f9449SShaohua Li */ 22912a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 2292915d4d7bSCesar Eduardo Barros p->max = maxpages; 2293915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 2294915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 2295bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 2296bdb8e3f6SCesar Eduardo Barros return nr_extents; 2297915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 2298915d4d7bSCesar Eduardo Barros } 2299915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 2300465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 2301bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2302915d4d7bSCesar Eduardo Barros } 2303915d4d7bSCesar Eduardo Barros 23042a8f9449SShaohua Li if (!cluster_info) 23052a8f9449SShaohua Li return nr_extents; 23062a8f9449SShaohua Li 23072a8f9449SShaohua Li for (i = 0; i < nr_clusters; i++) { 23082a8f9449SShaohua Li if (!cluster_count(&cluster_info[idx])) { 23092a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 23102a8f9449SShaohua Li if (cluster_is_null(&p->free_cluster_head)) { 23112a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, 23122a8f9449SShaohua Li idx, 0); 23132a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, 23142a8f9449SShaohua Li idx, 0); 23152a8f9449SShaohua Li } else { 23162a8f9449SShaohua Li unsigned int tail; 23172a8f9449SShaohua Li 23182a8f9449SShaohua Li tail = cluster_next(&p->free_cluster_tail); 23192a8f9449SShaohua Li cluster_set_next(&cluster_info[tail], idx); 23202a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, 23212a8f9449SShaohua Li idx, 0); 23222a8f9449SShaohua Li } 23232a8f9449SShaohua Li } 23242a8f9449SShaohua Li idx++; 23252a8f9449SShaohua Li if (idx == nr_clusters) 23262a8f9449SShaohua Li idx = 0; 23272a8f9449SShaohua Li } 2328915d4d7bSCesar Eduardo Barros return nr_extents; 2329915d4d7bSCesar Eduardo Barros } 2330915d4d7bSCesar Eduardo Barros 2331dcf6b7ddSRafael Aquini /* 2332dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 2333dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 2334dcf6b7ddSRafael Aquini */ 2335dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 2336dcf6b7ddSRafael Aquini { 2337dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 2338dcf6b7ddSRafael Aquini 2339dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 2340dcf6b7ddSRafael Aquini return false; 2341dcf6b7ddSRafael Aquini 2342dcf6b7ddSRafael Aquini return true; 2343dcf6b7ddSRafael Aquini } 2344dcf6b7ddSRafael Aquini 234553cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 234653cbb243SCesar Eduardo Barros { 234753cbb243SCesar Eduardo Barros struct swap_info_struct *p; 234891a27b2aSJeff Layton struct filename *name; 234953cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 235053cbb243SCesar Eduardo Barros struct address_space *mapping; 235140531542SCesar Eduardo Barros int i; 235240531542SCesar Eduardo Barros int prio; 235353cbb243SCesar Eduardo Barros int error; 235453cbb243SCesar Eduardo Barros union swap_header *swap_header; 2355915d4d7bSCesar Eduardo Barros int nr_extents; 235653cbb243SCesar Eduardo Barros sector_t span; 235753cbb243SCesar Eduardo Barros unsigned long maxpages; 235853cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 23592a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 236038b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 236153cbb243SCesar Eduardo Barros struct page *page = NULL; 236253cbb243SCesar Eduardo Barros struct inode *inode = NULL; 236353cbb243SCesar Eduardo Barros 2364d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 2365d15cab97SHugh Dickins return -EINVAL; 2366d15cab97SHugh Dickins 236753cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 236853cbb243SCesar Eduardo Barros return -EPERM; 236953cbb243SCesar Eduardo Barros 237053cbb243SCesar Eduardo Barros p = alloc_swap_info(); 23712542e513SCesar Eduardo Barros if (IS_ERR(p)) 23722542e513SCesar Eduardo Barros return PTR_ERR(p); 237353cbb243SCesar Eduardo Barros 2374815c2c54SShaohua Li INIT_WORK(&p->discard_work, swap_discard_work); 2375815c2c54SShaohua Li 23761da177e4SLinus Torvalds name = getname(specialfile); 23771da177e4SLinus Torvalds if (IS_ERR(name)) { 23787de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 23791da177e4SLinus Torvalds name = NULL; 2380bd69010bSCesar Eduardo Barros goto bad_swap; 23811da177e4SLinus Torvalds } 2382669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 23831da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 23847de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 23851da177e4SLinus Torvalds swap_file = NULL; 2386bd69010bSCesar Eduardo Barros goto bad_swap; 23871da177e4SLinus Torvalds } 23881da177e4SLinus Torvalds 23891da177e4SLinus Torvalds p->swap_file = swap_file; 23901da177e4SLinus Torvalds mapping = swap_file->f_mapping; 23911da177e4SLinus Torvalds 23921da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 2393efa90a98SHugh Dickins struct swap_info_struct *q = swap_info[i]; 23941da177e4SLinus Torvalds 2395e8e6c2ecSCesar Eduardo Barros if (q == p || !q->swap_file) 23961da177e4SLinus Torvalds continue; 23977de7fb6bSCesar Eduardo Barros if (mapping == q->swap_file->f_mapping) { 23987de7fb6bSCesar Eduardo Barros error = -EBUSY; 23991da177e4SLinus Torvalds goto bad_swap; 24001da177e4SLinus Torvalds } 24017de7fb6bSCesar Eduardo Barros } 24021da177e4SLinus Torvalds 24032130781eSCesar Eduardo Barros inode = mapping->host; 24042130781eSCesar Eduardo Barros /* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */ 24054d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 24064d0e1e10SCesar Eduardo Barros if (unlikely(error)) 24071da177e4SLinus Torvalds goto bad_swap; 24081da177e4SLinus Torvalds 24091da177e4SLinus Torvalds /* 24101da177e4SLinus Torvalds * Read the swap header. 24111da177e4SLinus Torvalds */ 24121da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 24131da177e4SLinus Torvalds error = -EINVAL; 24141da177e4SLinus Torvalds goto bad_swap; 24151da177e4SLinus Torvalds } 2416090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 24171da177e4SLinus Torvalds if (IS_ERR(page)) { 24181da177e4SLinus Torvalds error = PTR_ERR(page); 24191da177e4SLinus Torvalds goto bad_swap; 24201da177e4SLinus Torvalds } 242181e33971SHugh Dickins swap_header = kmap(page); 24221da177e4SLinus Torvalds 2423ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 2424ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 24251da177e4SLinus Torvalds error = -EINVAL; 24261da177e4SLinus Torvalds goto bad_swap; 24271da177e4SLinus Torvalds } 24281da177e4SLinus Torvalds 24291da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 2430803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 243178ecba08SHugh Dickins if (!swap_map) { 24321da177e4SLinus Torvalds error = -ENOMEM; 24331da177e4SLinus Torvalds goto bad_swap; 24341da177e4SLinus Torvalds } 24352a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 24362a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 24372a8f9449SShaohua Li /* 24382a8f9449SShaohua Li * select a random position to start with to help wear leveling 24392a8f9449SShaohua Li * SSD 24402a8f9449SShaohua Li */ 24412a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 24422a8f9449SShaohua Li 24432a8f9449SShaohua Li cluster_info = vzalloc(DIV_ROUND_UP(maxpages, 24442a8f9449SShaohua Li SWAPFILE_CLUSTER) * sizeof(*cluster_info)); 24452a8f9449SShaohua Li if (!cluster_info) { 24462a8f9449SShaohua Li error = -ENOMEM; 24472a8f9449SShaohua Li goto bad_swap; 24482a8f9449SShaohua Li } 2449ebc2a1a6SShaohua Li p->percpu_cluster = alloc_percpu(struct percpu_cluster); 2450ebc2a1a6SShaohua Li if (!p->percpu_cluster) { 2451ebc2a1a6SShaohua Li error = -ENOMEM; 2452ebc2a1a6SShaohua Li goto bad_swap; 2453ebc2a1a6SShaohua Li } 2454ebc2a1a6SShaohua Li for_each_possible_cpu(i) { 2455ebc2a1a6SShaohua Li struct percpu_cluster *cluster; 2456ebc2a1a6SShaohua Li cluster = per_cpu_ptr(p->percpu_cluster, i); 2457ebc2a1a6SShaohua Li cluster_set_null(&cluster->index); 2458ebc2a1a6SShaohua Li } 24592a8f9449SShaohua Li } 24601da177e4SLinus Torvalds 24611421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 24621421ef3cSCesar Eduardo Barros if (error) 24631421ef3cSCesar Eduardo Barros goto bad_swap; 24641421ef3cSCesar Eduardo Barros 2465915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 24662a8f9449SShaohua Li cluster_info, maxpages, &span); 2467915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 246853092a74SHugh Dickins error = nr_extents; 2469e2244ec2SHugh Dickins goto bad_swap; 247053092a74SHugh Dickins } 247138b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 247238b5faf4SDan Magenheimer if (frontswap_enabled) 24737b57976dSAkinobu Mita frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long)); 24741da177e4SLinus Torvalds 24752a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 2476dcf6b7ddSRafael Aquini /* 2477dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 2478dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 2479dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 2480dcf6b7ddSRafael Aquini * swapon(8) releases. 2481dcf6b7ddSRafael Aquini */ 2482dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 2483dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 2484dcf6b7ddSRafael Aquini 2485dcf6b7ddSRafael Aquini /* 2486dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 2487dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 2488dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 2489dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 2490dcf6b7ddSRafael Aquini */ 2491dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 2492dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 2493dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 2494dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 2495dcf6b7ddSRafael Aquini 2496dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 2497dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 2498dcf6b7ddSRafael Aquini int err = discard_swap(p); 2499dcf6b7ddSRafael Aquini if (unlikely(err)) 2500465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 2501dcf6b7ddSRafael Aquini p, err); 2502dcf6b7ddSRafael Aquini } 2503dcf6b7ddSRafael Aquini } 25046a6ba831SHugh Dickins 2505fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 250640531542SCesar Eduardo Barros prio = -1; 250778ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 250840531542SCesar Eduardo Barros prio = 250978ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 25102a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 2511c69dbfb8SCesar Eduardo Barros 2512465c47fdSAndrew Morton pr_info("Adding %uk swap on %s. " 2513dcf6b7ddSRafael Aquini "Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 251491a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 2515c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 2516c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 251738b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 2518dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 2519dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 252038b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 2521c69dbfb8SCesar Eduardo Barros 2522fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 252366d7dd51SKay Sievers atomic_inc(&proc_poll_event); 252466d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 252566d7dd51SKay Sievers 25269b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 25279b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 25281da177e4SLinus Torvalds error = 0; 25291da177e4SLinus Torvalds goto out; 25301da177e4SLinus Torvalds bad_swap: 2531ebc2a1a6SShaohua Li free_percpu(p->percpu_cluster); 2532ebc2a1a6SShaohua Li p->percpu_cluster = NULL; 2533bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 2534f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 2535f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 25361da177e4SLinus Torvalds } 25374cd3bb10SHugh Dickins destroy_swap_extents(p); 2538e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 25395d337b91SHugh Dickins spin_lock(&swap_lock); 25401da177e4SLinus Torvalds p->swap_file = NULL; 25411da177e4SLinus Torvalds p->flags = 0; 25425d337b91SHugh Dickins spin_unlock(&swap_lock); 25431da177e4SLinus Torvalds vfree(swap_map); 25442a8f9449SShaohua Li vfree(cluster_info); 254552c50567SMel Gorman if (swap_file) { 25462130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 254752c50567SMel Gorman mutex_unlock(&inode->i_mutex); 25482130781eSCesar Eduardo Barros inode = NULL; 25492130781eSCesar Eduardo Barros } 25501da177e4SLinus Torvalds filp_close(swap_file, NULL); 255152c50567SMel Gorman } 25521da177e4SLinus Torvalds out: 25531da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 25541da177e4SLinus Torvalds kunmap(page); 25551da177e4SLinus Torvalds page_cache_release(page); 25561da177e4SLinus Torvalds } 25571da177e4SLinus Torvalds if (name) 25581da177e4SLinus Torvalds putname(name); 25599b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 25601b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 25611da177e4SLinus Torvalds return error; 25621da177e4SLinus Torvalds } 25631da177e4SLinus Torvalds 25641da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 25651da177e4SLinus Torvalds { 2566efa90a98SHugh Dickins unsigned int type; 25671da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 25681da177e4SLinus Torvalds 25695d337b91SHugh Dickins spin_lock(&swap_lock); 2570efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2571efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 2572efa90a98SHugh Dickins 2573efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 2574efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 25751da177e4SLinus Torvalds } 2576ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 25771da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 25785d337b91SHugh Dickins spin_unlock(&swap_lock); 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 25831da177e4SLinus Torvalds * 2584355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 2585355cfa73SKAMEZAWA Hiroyuki * - success -> 0 2586355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 2587355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 2588355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 2589355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 2590570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 25911da177e4SLinus Torvalds */ 25928d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 25931da177e4SLinus Torvalds { 25941da177e4SLinus Torvalds struct swap_info_struct *p; 25951da177e4SLinus Torvalds unsigned long offset, type; 25968d69aaeeSHugh Dickins unsigned char count; 25978d69aaeeSHugh Dickins unsigned char has_cache; 2598253d553bSHugh Dickins int err = -EINVAL; 25991da177e4SLinus Torvalds 2600a7420aa5SAndi Kleen if (non_swap_entry(entry)) 2601253d553bSHugh Dickins goto out; 26020697212aSChristoph Lameter 26031da177e4SLinus Torvalds type = swp_type(entry); 26041da177e4SLinus Torvalds if (type >= nr_swapfiles) 26051da177e4SLinus Torvalds goto bad_file; 2606efa90a98SHugh Dickins p = swap_info[type]; 26071da177e4SLinus Torvalds offset = swp_offset(entry); 26081da177e4SLinus Torvalds 2609ec8acf20SShaohua Li spin_lock(&p->lock); 2610355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 2611355cfa73SKAMEZAWA Hiroyuki goto unlock_out; 2612355cfa73SKAMEZAWA Hiroyuki 2613253d553bSHugh Dickins count = p->swap_map[offset]; 2614edfe23daSShaohua Li 2615edfe23daSShaohua Li /* 2616edfe23daSShaohua Li * swapin_readahead() doesn't check if a swap entry is valid, so the 2617edfe23daSShaohua Li * swap entry could be SWAP_MAP_BAD. Check here with lock held. 2618edfe23daSShaohua Li */ 2619edfe23daSShaohua Li if (unlikely(swap_count(count) == SWAP_MAP_BAD)) { 2620edfe23daSShaohua Li err = -ENOENT; 2621edfe23daSShaohua Li goto unlock_out; 2622edfe23daSShaohua Li } 2623edfe23daSShaohua Li 2624253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 2625253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 2626253d553bSHugh Dickins err = 0; 2627355cfa73SKAMEZAWA Hiroyuki 2628253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 2629355cfa73SKAMEZAWA Hiroyuki 2630355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 2631253d553bSHugh Dickins if (!has_cache && count) 2632253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 2633253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 2634253d553bSHugh Dickins err = -EEXIST; 2635253d553bSHugh Dickins else /* no users remaining */ 2636253d553bSHugh Dickins err = -ENOENT; 2637355cfa73SKAMEZAWA Hiroyuki 2638355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 2639253d553bSHugh Dickins 2640570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 2641570a335bSHugh Dickins count += usage; 2642570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 2643253d553bSHugh Dickins err = -EINVAL; 2644570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 2645570a335bSHugh Dickins count = COUNT_CONTINUED; 2646570a335bSHugh Dickins else 2647570a335bSHugh Dickins err = -ENOMEM; 2648253d553bSHugh Dickins } else 2649253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 2650253d553bSHugh Dickins 2651253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 2652253d553bSHugh Dickins 2653355cfa73SKAMEZAWA Hiroyuki unlock_out: 2654ec8acf20SShaohua Li spin_unlock(&p->lock); 26551da177e4SLinus Torvalds out: 2656253d553bSHugh Dickins return err; 26571da177e4SLinus Torvalds 26581da177e4SLinus Torvalds bad_file: 2659465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 26601da177e4SLinus Torvalds goto out; 26611da177e4SLinus Torvalds } 2662253d553bSHugh Dickins 2663355cfa73SKAMEZAWA Hiroyuki /* 2664aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 2665aaa46865SHugh Dickins * (in which case its reference count is never incremented). 2666aaa46865SHugh Dickins */ 2667aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 2668aaa46865SHugh Dickins { 2669aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 2670aaa46865SHugh Dickins } 2671aaa46865SHugh Dickins 2672aaa46865SHugh Dickins /* 267308259d58SHugh Dickins * Increase reference count of swap entry by 1. 267408259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 267508259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 267608259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 267708259d58SHugh Dickins * might occur if a page table entry has got corrupted. 2678355cfa73SKAMEZAWA Hiroyuki */ 2679570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 2680355cfa73SKAMEZAWA Hiroyuki { 2681570a335bSHugh Dickins int err = 0; 2682570a335bSHugh Dickins 2683570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 2684570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 2685570a335bSHugh Dickins return err; 2686355cfa73SKAMEZAWA Hiroyuki } 26871da177e4SLinus Torvalds 2688cb4b86baSKAMEZAWA Hiroyuki /* 2689355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 2690355cfa73SKAMEZAWA Hiroyuki * 269173c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 2692355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 2693355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 2694355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 2695cb4b86baSKAMEZAWA Hiroyuki */ 2696cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 2697cb4b86baSKAMEZAWA Hiroyuki { 2698253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 2699cb4b86baSKAMEZAWA Hiroyuki } 2700cb4b86baSKAMEZAWA Hiroyuki 2701f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 2702f981c595SMel Gorman { 2703f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2704f981c595SMel Gorman BUG_ON(!PageSwapCache(page)); 2705f981c595SMel Gorman return swap_info[swp_type(swap)]; 2706f981c595SMel Gorman } 2707f981c595SMel Gorman 2708f981c595SMel Gorman /* 2709f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 2710f981c595SMel Gorman */ 2711f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 2712f981c595SMel Gorman { 2713309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 2714f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 2715f981c595SMel Gorman } 2716f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 2717f981c595SMel Gorman 2718f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 2719f981c595SMel Gorman { 2720f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2721309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapCache(page), page); 2722f981c595SMel Gorman return swp_offset(swap); 2723f981c595SMel Gorman } 2724f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 2725f981c595SMel Gorman 27261da177e4SLinus Torvalds /* 2727570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 2728570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 2729570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 2730570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 2731570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 2732570a335bSHugh Dickins * 2733570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 2734570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 2735570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 2736570a335bSHugh Dickins * 2737570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 2738570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 2739570a335bSHugh Dickins * can be called after dropping locks. 2740570a335bSHugh Dickins */ 2741570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 2742570a335bSHugh Dickins { 2743570a335bSHugh Dickins struct swap_info_struct *si; 2744570a335bSHugh Dickins struct page *head; 2745570a335bSHugh Dickins struct page *page; 2746570a335bSHugh Dickins struct page *list_page; 2747570a335bSHugh Dickins pgoff_t offset; 2748570a335bSHugh Dickins unsigned char count; 2749570a335bSHugh Dickins 2750570a335bSHugh Dickins /* 2751570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 2752570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 2753570a335bSHugh Dickins */ 2754570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 2755570a335bSHugh Dickins 2756570a335bSHugh Dickins si = swap_info_get(entry); 2757570a335bSHugh Dickins if (!si) { 2758570a335bSHugh Dickins /* 2759570a335bSHugh Dickins * An acceptable race has occurred since the failing 2760570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 2761570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 2762570a335bSHugh Dickins */ 2763570a335bSHugh Dickins goto outer; 2764570a335bSHugh Dickins } 2765570a335bSHugh Dickins 2766570a335bSHugh Dickins offset = swp_offset(entry); 2767570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 2768570a335bSHugh Dickins 2769570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 2770570a335bSHugh Dickins /* 2771570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 2772570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 2773570a335bSHugh Dickins * over-provisioning. 2774570a335bSHugh Dickins */ 2775570a335bSHugh Dickins goto out; 2776570a335bSHugh Dickins } 2777570a335bSHugh Dickins 2778570a335bSHugh Dickins if (!page) { 2779ec8acf20SShaohua Li spin_unlock(&si->lock); 2780570a335bSHugh Dickins return -ENOMEM; 2781570a335bSHugh Dickins } 2782570a335bSHugh Dickins 2783570a335bSHugh Dickins /* 2784570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 2785570a335bSHugh Dickins * no architecture is using highmem pages for kernel page tables: so it 2786570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic page table kmaps. 2787570a335bSHugh Dickins */ 2788570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2789570a335bSHugh Dickins offset &= ~PAGE_MASK; 2790570a335bSHugh Dickins 2791570a335bSHugh Dickins /* 2792570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 2793570a335bSHugh Dickins * but it does always reset its private field. 2794570a335bSHugh Dickins */ 2795570a335bSHugh Dickins if (!page_private(head)) { 2796570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 2797570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 2798570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 2799570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 2800570a335bSHugh Dickins } 2801570a335bSHugh Dickins 2802570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 2803570a335bSHugh Dickins unsigned char *map; 2804570a335bSHugh Dickins 2805570a335bSHugh Dickins /* 2806570a335bSHugh Dickins * If the previous map said no continuation, but we've found 2807570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 2808570a335bSHugh Dickins */ 2809570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 2810570a335bSHugh Dickins goto out; 2811570a335bSHugh Dickins 28129b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 2813570a335bSHugh Dickins count = *map; 28149b04c5feSCong Wang kunmap_atomic(map); 2815570a335bSHugh Dickins 2816570a335bSHugh Dickins /* 2817570a335bSHugh Dickins * If this continuation count now has some space in it, 2818570a335bSHugh Dickins * free our allocation and use this one. 2819570a335bSHugh Dickins */ 2820570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 2821570a335bSHugh Dickins goto out; 2822570a335bSHugh Dickins } 2823570a335bSHugh Dickins 2824570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 2825570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 2826570a335bSHugh Dickins out: 2827ec8acf20SShaohua Li spin_unlock(&si->lock); 2828570a335bSHugh Dickins outer: 2829570a335bSHugh Dickins if (page) 2830570a335bSHugh Dickins __free_page(page); 2831570a335bSHugh Dickins return 0; 2832570a335bSHugh Dickins } 2833570a335bSHugh Dickins 2834570a335bSHugh Dickins /* 2835570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 2836570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 2837570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 2838570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 2839570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 2840570a335bSHugh Dickins * Called while __swap_duplicate() or swap_entry_free() holds swap_lock. 2841570a335bSHugh Dickins */ 2842570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 2843570a335bSHugh Dickins pgoff_t offset, unsigned char count) 2844570a335bSHugh Dickins { 2845570a335bSHugh Dickins struct page *head; 2846570a335bSHugh Dickins struct page *page; 2847570a335bSHugh Dickins unsigned char *map; 2848570a335bSHugh Dickins 2849570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2850570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 2851570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 2852570a335bSHugh Dickins return false; /* need to add count continuation */ 2853570a335bSHugh Dickins } 2854570a335bSHugh Dickins 2855570a335bSHugh Dickins offset &= ~PAGE_MASK; 2856570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 28579b04c5feSCong Wang map = kmap_atomic(page) + offset; 2858570a335bSHugh Dickins 2859570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 2860570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 2861570a335bSHugh Dickins 2862570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 2863570a335bSHugh Dickins /* 2864570a335bSHugh Dickins * Think of how you add 1 to 999 2865570a335bSHugh Dickins */ 2866570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 28679b04c5feSCong Wang kunmap_atomic(map); 2868570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2869570a335bSHugh Dickins BUG_ON(page == head); 28709b04c5feSCong Wang map = kmap_atomic(page) + offset; 2871570a335bSHugh Dickins } 2872570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 28739b04c5feSCong Wang kunmap_atomic(map); 2874570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2875570a335bSHugh Dickins if (page == head) 2876570a335bSHugh Dickins return false; /* add count continuation */ 28779b04c5feSCong Wang map = kmap_atomic(page) + offset; 2878570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 2879570a335bSHugh Dickins } 2880570a335bSHugh Dickins *map += 1; 28819b04c5feSCong Wang kunmap_atomic(map); 2882570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2883570a335bSHugh Dickins while (page != head) { 28849b04c5feSCong Wang map = kmap_atomic(page) + offset; 2885570a335bSHugh Dickins *map = COUNT_CONTINUED; 28869b04c5feSCong Wang kunmap_atomic(map); 2887570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2888570a335bSHugh Dickins } 2889570a335bSHugh Dickins return true; /* incremented */ 2890570a335bSHugh Dickins 2891570a335bSHugh Dickins } else { /* decrementing */ 2892570a335bSHugh Dickins /* 2893570a335bSHugh Dickins * Think of how you subtract 1 from 1000 2894570a335bSHugh Dickins */ 2895570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 2896570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 28979b04c5feSCong Wang kunmap_atomic(map); 2898570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2899570a335bSHugh Dickins BUG_ON(page == head); 29009b04c5feSCong Wang map = kmap_atomic(page) + offset; 2901570a335bSHugh Dickins } 2902570a335bSHugh Dickins BUG_ON(*map == 0); 2903570a335bSHugh Dickins *map -= 1; 2904570a335bSHugh Dickins if (*map == 0) 2905570a335bSHugh Dickins count = 0; 29069b04c5feSCong Wang kunmap_atomic(map); 2907570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2908570a335bSHugh Dickins while (page != head) { 29099b04c5feSCong Wang map = kmap_atomic(page) + offset; 2910570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 2911570a335bSHugh Dickins count = COUNT_CONTINUED; 29129b04c5feSCong Wang kunmap_atomic(map); 2913570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2914570a335bSHugh Dickins } 2915570a335bSHugh Dickins return count == COUNT_CONTINUED; 2916570a335bSHugh Dickins } 2917570a335bSHugh Dickins } 2918570a335bSHugh Dickins 2919570a335bSHugh Dickins /* 2920570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 2921570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 2922570a335bSHugh Dickins */ 2923570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 2924570a335bSHugh Dickins { 2925570a335bSHugh Dickins pgoff_t offset; 2926570a335bSHugh Dickins 2927570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 2928570a335bSHugh Dickins struct page *head; 2929570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2930570a335bSHugh Dickins if (page_private(head)) { 2931570a335bSHugh Dickins struct list_head *this, *next; 2932570a335bSHugh Dickins list_for_each_safe(this, next, &head->lru) { 2933570a335bSHugh Dickins struct page *page; 2934570a335bSHugh Dickins page = list_entry(this, struct page, lru); 2935570a335bSHugh Dickins list_del(this); 2936570a335bSHugh Dickins __free_page(page); 2937570a335bSHugh Dickins } 2938570a335bSHugh Dickins } 2939570a335bSHugh Dickins } 2940570a335bSHugh Dickins } 2941