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> 171da177e4SLinus Torvalds #include <linux/shm.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> 241da177e4SLinus Torvalds #include <linux/module.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> 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #include <asm/pgtable.h> 341da177e4SLinus Torvalds #include <asm/tlbflush.h> 351da177e4SLinus Torvalds #include <linux/swapops.h> 3627a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h> 371da177e4SLinus Torvalds 387c363b8cSAdrian Bunk static DEFINE_SPINLOCK(swap_lock); 397c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 40b962716bSHugh Dickins long nr_swap_pages; 411da177e4SLinus Torvalds long total_swap_pages; 421da177e4SLinus Torvalds static int swap_overflow; 4378ecba08SHugh Dickins static int least_priority; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 461da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 471da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 481da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 491da177e4SLinus Torvalds 507c363b8cSAdrian Bunk static struct swap_list_t swap_list = {-1, -1}; 511da177e4SLinus Torvalds 52efa90a98SHugh Dickins static struct swap_info_struct *swap_info[MAX_SWAPFILES]; 531da177e4SLinus Torvalds 54fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 551da177e4SLinus Torvalds 56*8d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 57355cfa73SKAMEZAWA Hiroyuki { 58253d553bSHugh Dickins return ent & ~SWAP_HAS_CACHE; 59355cfa73SKAMEZAWA Hiroyuki } 60355cfa73SKAMEZAWA Hiroyuki 61efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 62c9e44410SKAMEZAWA Hiroyuki static int 63c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 64c9e44410SKAMEZAWA Hiroyuki { 65efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 66c9e44410SKAMEZAWA Hiroyuki struct page *page; 67c9e44410SKAMEZAWA Hiroyuki int ret = 0; 68c9e44410SKAMEZAWA Hiroyuki 69c9e44410SKAMEZAWA Hiroyuki page = find_get_page(&swapper_space, entry.val); 70c9e44410SKAMEZAWA Hiroyuki if (!page) 71c9e44410SKAMEZAWA Hiroyuki return 0; 72c9e44410SKAMEZAWA Hiroyuki /* 73c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 74c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 75c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 76c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 77c9e44410SKAMEZAWA Hiroyuki * in usual operations. 78c9e44410SKAMEZAWA Hiroyuki */ 79c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 80c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 81c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 82c9e44410SKAMEZAWA Hiroyuki } 83c9e44410SKAMEZAWA Hiroyuki page_cache_release(page); 84c9e44410SKAMEZAWA Hiroyuki return ret; 85c9e44410SKAMEZAWA Hiroyuki } 86355cfa73SKAMEZAWA Hiroyuki 871da177e4SLinus Torvalds /* 881da177e4SLinus Torvalds * We need this because the bdev->unplug_fn can sleep and we cannot 895d337b91SHugh Dickins * hold swap_lock while calling the unplug_fn. And swap_lock 90fc0abb14SIngo Molnar * cannot be turned into a mutex. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem); 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds swp_entry_t entry; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds down_read(&swap_unplug_sem); 994c21e2f2SHugh Dickins entry.val = page_private(page); 1001da177e4SLinus Torvalds if (PageSwapCache(page)) { 101efa90a98SHugh Dickins struct block_device *bdev = swap_info[swp_type(entry)]->bdev; 1021da177e4SLinus Torvalds struct backing_dev_info *bdi; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * If the page is removed from swapcache from under us (with a 1061da177e4SLinus Torvalds * racy try_to_unuse/swapoff) we need an additional reference 1074c21e2f2SHugh Dickins * count to avoid reading garbage from page_private(page) above. 1084c21e2f2SHugh Dickins * If the WARN_ON triggers during a swapoff it maybe the race 1091da177e4SLinus Torvalds * condition and it's harmless. However if it triggers without 1101da177e4SLinus Torvalds * swapoff it signals a problem. 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds WARN_ON(page_count(page) <= 1); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds bdi = bdev->bd_inode->i_mapping->backing_dev_info; 115ba32311eSMcMullan, Jason blk_run_backing_dev(bdi, page); 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds up_read(&swap_unplug_sem); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1206a6ba831SHugh Dickins /* 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, 1379625a5f2SHugh Dickins nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER); 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, 1489625a5f2SHugh Dickins nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER); 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, 1879625a5f2SHugh Dickins nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER)) 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 1967992fde7SHugh Dickins static int wait_for_discard(void *word) 1977992fde7SHugh Dickins { 1987992fde7SHugh Dickins schedule(); 1997992fde7SHugh Dickins return 0; 2007992fde7SHugh Dickins } 2017992fde7SHugh Dickins 202048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 203048c27fdSHugh Dickins #define LATENCY_LIMIT 256 204048c27fdSHugh Dickins 205355cfa73SKAMEZAWA Hiroyuki static inline unsigned long scan_swap_map(struct swap_info_struct *si, 206*8d69aaeeSHugh Dickins unsigned char usage) 2071da177e4SLinus Torvalds { 208ebebbbe9SHugh Dickins unsigned long offset; 209c60aa176SHugh Dickins unsigned long scan_base; 2107992fde7SHugh Dickins unsigned long last_in_cluster = 0; 211048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 2127992fde7SHugh Dickins int found_free_cluster = 0; 2131da177e4SLinus Torvalds 2147dfad418SHugh Dickins /* 2157dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 2167dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 2177dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 2187dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 2197dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 2207dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 2217dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 222c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 2231da177e4SLinus Torvalds */ 2247dfad418SHugh Dickins 22552b7efdbSHugh Dickins si->flags += SWP_SCANNING; 226c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 227ebebbbe9SHugh Dickins 228ebebbbe9SHugh Dickins if (unlikely(!si->cluster_nr--)) { 229ebebbbe9SHugh Dickins if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 2307dfad418SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 231ebebbbe9SHugh Dickins goto checks; 232ebebbbe9SHugh Dickins } 2337992fde7SHugh Dickins if (si->flags & SWP_DISCARDABLE) { 2347992fde7SHugh Dickins /* 2357992fde7SHugh Dickins * Start range check on racing allocations, in case 2367992fde7SHugh Dickins * they overlap the cluster we eventually decide on 2377992fde7SHugh Dickins * (we scan without swap_lock to allow preemption). 2387992fde7SHugh Dickins * It's hardly conceivable that cluster_nr could be 2397992fde7SHugh Dickins * wrapped during our scan, but don't depend on it. 2407992fde7SHugh Dickins */ 2417992fde7SHugh Dickins if (si->lowest_alloc) 2427992fde7SHugh Dickins goto checks; 2437992fde7SHugh Dickins si->lowest_alloc = si->max; 2447992fde7SHugh Dickins si->highest_alloc = 0; 2457992fde7SHugh Dickins } 2465d337b91SHugh Dickins spin_unlock(&swap_lock); 2477dfad418SHugh Dickins 248c60aa176SHugh Dickins /* 249c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 250c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 251c60aa176SHugh Dickins * But if seek is cheap, search from our current position, so 252c60aa176SHugh Dickins * that swap is allocated from all over the partition: if the 253c60aa176SHugh Dickins * Flash Translation Layer only remaps within limited zones, 254c60aa176SHugh Dickins * we don't want to wear out the first zone too quickly. 255c60aa176SHugh Dickins */ 256c60aa176SHugh Dickins if (!(si->flags & SWP_SOLIDSTATE)) 257c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 2587dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 2597dfad418SHugh Dickins 2607dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 2617dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 2621da177e4SLinus Torvalds if (si->swap_map[offset]) 2637dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 2647dfad418SHugh Dickins else if (offset == last_in_cluster) { 2655d337b91SHugh Dickins spin_lock(&swap_lock); 266ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 267ebebbbe9SHugh Dickins si->cluster_next = offset; 268ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 2697992fde7SHugh Dickins found_free_cluster = 1; 270ebebbbe9SHugh Dickins goto checks; 2717dfad418SHugh Dickins } 272048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 273048c27fdSHugh Dickins cond_resched(); 274048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 275048c27fdSHugh Dickins } 2767dfad418SHugh Dickins } 277ebebbbe9SHugh Dickins 278ebebbbe9SHugh Dickins offset = si->lowest_bit; 279c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 280c60aa176SHugh Dickins 281c60aa176SHugh Dickins /* Locate the first empty (unaligned) cluster */ 282c60aa176SHugh Dickins for (; last_in_cluster < scan_base; offset++) { 283c60aa176SHugh Dickins if (si->swap_map[offset]) 284c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 285c60aa176SHugh Dickins else if (offset == last_in_cluster) { 286c60aa176SHugh Dickins spin_lock(&swap_lock); 287c60aa176SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 288c60aa176SHugh Dickins si->cluster_next = offset; 289c60aa176SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 290c60aa176SHugh Dickins found_free_cluster = 1; 291c60aa176SHugh Dickins goto checks; 292c60aa176SHugh Dickins } 293c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 294c60aa176SHugh Dickins cond_resched(); 295c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 296c60aa176SHugh Dickins } 297c60aa176SHugh Dickins } 298c60aa176SHugh Dickins 299c60aa176SHugh Dickins offset = scan_base; 3005d337b91SHugh Dickins spin_lock(&swap_lock); 301ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 3027992fde7SHugh Dickins si->lowest_alloc = 0; 3037dfad418SHugh Dickins } 3047dfad418SHugh Dickins 305ebebbbe9SHugh Dickins checks: 306ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 30752b7efdbSHugh Dickins goto no_page; 3087dfad418SHugh Dickins if (!si->highest_bit) 3097dfad418SHugh Dickins goto no_page; 310ebebbbe9SHugh Dickins if (offset > si->highest_bit) 311c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 312c9e44410SKAMEZAWA Hiroyuki 313c9e44410SKAMEZAWA Hiroyuki /* reuse swap entry of cache-only swap if not busy. */ 314c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 315c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 316c9e44410SKAMEZAWA Hiroyuki spin_unlock(&swap_lock); 317c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 318c9e44410SKAMEZAWA Hiroyuki spin_lock(&swap_lock); 319c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 320c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 321c9e44410SKAMEZAWA Hiroyuki goto checks; 322c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 323c9e44410SKAMEZAWA Hiroyuki } 324c9e44410SKAMEZAWA Hiroyuki 325ebebbbe9SHugh Dickins if (si->swap_map[offset]) 326ebebbbe9SHugh Dickins goto scan; 327ebebbbe9SHugh Dickins 32852b7efdbSHugh Dickins if (offset == si->lowest_bit) 3291da177e4SLinus Torvalds si->lowest_bit++; 3301da177e4SLinus Torvalds if (offset == si->highest_bit) 3311da177e4SLinus Torvalds si->highest_bit--; 3327dfad418SHugh Dickins si->inuse_pages++; 3337dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 3341da177e4SLinus Torvalds si->lowest_bit = si->max; 3351da177e4SLinus Torvalds si->highest_bit = 0; 3361da177e4SLinus Torvalds } 337253d553bSHugh Dickins si->swap_map[offset] = usage; 3381da177e4SLinus Torvalds si->cluster_next = offset + 1; 33952b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 3407992fde7SHugh Dickins 3417992fde7SHugh Dickins if (si->lowest_alloc) { 3427992fde7SHugh Dickins /* 3437992fde7SHugh Dickins * Only set when SWP_DISCARDABLE, and there's a scan 3447992fde7SHugh Dickins * for a free cluster in progress or just completed. 3457992fde7SHugh Dickins */ 3467992fde7SHugh Dickins if (found_free_cluster) { 3477992fde7SHugh Dickins /* 3487992fde7SHugh Dickins * To optimize wear-levelling, discard the 3497992fde7SHugh Dickins * old data of the cluster, taking care not to 3507992fde7SHugh Dickins * discard any of its pages that have already 3517992fde7SHugh Dickins * been allocated by racing tasks (offset has 3527992fde7SHugh Dickins * already stepped over any at the beginning). 3537992fde7SHugh Dickins */ 3547992fde7SHugh Dickins if (offset < si->highest_alloc && 3557992fde7SHugh Dickins si->lowest_alloc <= last_in_cluster) 3567992fde7SHugh Dickins last_in_cluster = si->lowest_alloc - 1; 3577992fde7SHugh Dickins si->flags |= SWP_DISCARDING; 3587992fde7SHugh Dickins spin_unlock(&swap_lock); 3597992fde7SHugh Dickins 3607992fde7SHugh Dickins if (offset < last_in_cluster) 3617992fde7SHugh Dickins discard_swap_cluster(si, offset, 3627992fde7SHugh Dickins last_in_cluster - offset + 1); 3637992fde7SHugh Dickins 3647992fde7SHugh Dickins spin_lock(&swap_lock); 3657992fde7SHugh Dickins si->lowest_alloc = 0; 3667992fde7SHugh Dickins si->flags &= ~SWP_DISCARDING; 3677992fde7SHugh Dickins 3687992fde7SHugh Dickins smp_mb(); /* wake_up_bit advises this */ 3697992fde7SHugh Dickins wake_up_bit(&si->flags, ilog2(SWP_DISCARDING)); 3707992fde7SHugh Dickins 3717992fde7SHugh Dickins } else if (si->flags & SWP_DISCARDING) { 3727992fde7SHugh Dickins /* 3737992fde7SHugh Dickins * Delay using pages allocated by racing tasks 3747992fde7SHugh Dickins * until the whole discard has been issued. We 3757992fde7SHugh Dickins * could defer that delay until swap_writepage, 3767992fde7SHugh Dickins * but it's easier to keep this self-contained. 3777992fde7SHugh Dickins */ 3787992fde7SHugh Dickins spin_unlock(&swap_lock); 3797992fde7SHugh Dickins wait_on_bit(&si->flags, ilog2(SWP_DISCARDING), 3807992fde7SHugh Dickins wait_for_discard, TASK_UNINTERRUPTIBLE); 3817992fde7SHugh Dickins spin_lock(&swap_lock); 3827992fde7SHugh Dickins } else { 3837992fde7SHugh Dickins /* 3847992fde7SHugh Dickins * Note pages allocated by racing tasks while 3857992fde7SHugh Dickins * scan for a free cluster is in progress, so 3867992fde7SHugh Dickins * that its final discard can exclude them. 3877992fde7SHugh Dickins */ 3887992fde7SHugh Dickins if (offset < si->lowest_alloc) 3897992fde7SHugh Dickins si->lowest_alloc = offset; 3907992fde7SHugh Dickins if (offset > si->highest_alloc) 3917992fde7SHugh Dickins si->highest_alloc = offset; 3927992fde7SHugh Dickins } 3937992fde7SHugh Dickins } 3941da177e4SLinus Torvalds return offset; 3957dfad418SHugh Dickins 396ebebbbe9SHugh Dickins scan: 3975d337b91SHugh Dickins spin_unlock(&swap_lock); 3987dfad418SHugh Dickins while (++offset <= si->highest_bit) { 39952b7efdbSHugh Dickins if (!si->swap_map[offset]) { 4005d337b91SHugh Dickins spin_lock(&swap_lock); 40152b7efdbSHugh Dickins goto checks; 4027dfad418SHugh Dickins } 403c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 404c9e44410SKAMEZAWA Hiroyuki spin_lock(&swap_lock); 405c9e44410SKAMEZAWA Hiroyuki goto checks; 406c9e44410SKAMEZAWA Hiroyuki } 407048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 408048c27fdSHugh Dickins cond_resched(); 409048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 410048c27fdSHugh Dickins } 41152b7efdbSHugh Dickins } 412c60aa176SHugh Dickins offset = si->lowest_bit; 413c60aa176SHugh Dickins while (++offset < scan_base) { 414c60aa176SHugh Dickins if (!si->swap_map[offset]) { 4155d337b91SHugh Dickins spin_lock(&swap_lock); 416ebebbbe9SHugh Dickins goto checks; 417c60aa176SHugh Dickins } 418c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 419c9e44410SKAMEZAWA Hiroyuki spin_lock(&swap_lock); 420c9e44410SKAMEZAWA Hiroyuki goto checks; 421c9e44410SKAMEZAWA Hiroyuki } 422c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 423c60aa176SHugh Dickins cond_resched(); 424c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 425c60aa176SHugh Dickins } 426c60aa176SHugh Dickins } 427c60aa176SHugh Dickins spin_lock(&swap_lock); 4287dfad418SHugh Dickins 4297dfad418SHugh Dickins no_page: 43052b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 4311da177e4SLinus Torvalds return 0; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 4351da177e4SLinus Torvalds { 436fb4f88dcSHugh Dickins struct swap_info_struct *si; 437fb4f88dcSHugh Dickins pgoff_t offset; 438fb4f88dcSHugh Dickins int type, next; 439fb4f88dcSHugh Dickins int wrapped = 0; 4401da177e4SLinus Torvalds 4415d337b91SHugh Dickins spin_lock(&swap_lock); 4421da177e4SLinus Torvalds if (nr_swap_pages <= 0) 443fb4f88dcSHugh Dickins goto noswap; 444fb4f88dcSHugh Dickins nr_swap_pages--; 4451da177e4SLinus Torvalds 446fb4f88dcSHugh Dickins for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { 447efa90a98SHugh Dickins si = swap_info[type]; 448fb4f88dcSHugh Dickins next = si->next; 449fb4f88dcSHugh Dickins if (next < 0 || 450efa90a98SHugh Dickins (!wrapped && si->prio != swap_info[next]->prio)) { 451fb4f88dcSHugh Dickins next = swap_list.head; 452fb4f88dcSHugh Dickins wrapped++; 4531da177e4SLinus Torvalds } 454fb4f88dcSHugh Dickins 455fb4f88dcSHugh Dickins if (!si->highest_bit) 456fb4f88dcSHugh Dickins continue; 457fb4f88dcSHugh Dickins if (!(si->flags & SWP_WRITEOK)) 458fb4f88dcSHugh Dickins continue; 459fb4f88dcSHugh Dickins 460fb4f88dcSHugh Dickins swap_list.next = next; 461355cfa73SKAMEZAWA Hiroyuki /* This is called for allocating swap entry for cache */ 462253d553bSHugh Dickins offset = scan_swap_map(si, SWAP_HAS_CACHE); 4635d337b91SHugh Dickins if (offset) { 4645d337b91SHugh Dickins spin_unlock(&swap_lock); 465fb4f88dcSHugh Dickins return swp_entry(type, offset); 4665d337b91SHugh Dickins } 467fb4f88dcSHugh Dickins next = swap_list.next; 468fb4f88dcSHugh Dickins } 469fb4f88dcSHugh Dickins 470fb4f88dcSHugh Dickins nr_swap_pages++; 471fb4f88dcSHugh Dickins noswap: 4725d337b91SHugh Dickins spin_unlock(&swap_lock); 473fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 4741da177e4SLinus Torvalds } 4751da177e4SLinus Torvalds 476355cfa73SKAMEZAWA Hiroyuki /* The only caller of this function is now susupend routine */ 4773a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type) 4783a291a20SRafael J. Wysocki { 4793a291a20SRafael J. Wysocki struct swap_info_struct *si; 4803a291a20SRafael J. Wysocki pgoff_t offset; 4813a291a20SRafael J. Wysocki 4823a291a20SRafael J. Wysocki spin_lock(&swap_lock); 483efa90a98SHugh Dickins si = swap_info[type]; 484efa90a98SHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 4853a291a20SRafael J. Wysocki nr_swap_pages--; 486355cfa73SKAMEZAWA Hiroyuki /* This is called for allocating swap entry, not cache */ 487253d553bSHugh Dickins offset = scan_swap_map(si, 1); 4883a291a20SRafael J. Wysocki if (offset) { 4893a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 4903a291a20SRafael J. Wysocki return swp_entry(type, offset); 4913a291a20SRafael J. Wysocki } 4923a291a20SRafael J. Wysocki nr_swap_pages++; 4933a291a20SRafael J. Wysocki } 4943a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 4953a291a20SRafael J. Wysocki return (swp_entry_t) {0}; 4963a291a20SRafael J. Wysocki } 4973a291a20SRafael J. Wysocki 4981da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct swap_info_struct *p; 5011da177e4SLinus Torvalds unsigned long offset, type; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds if (!entry.val) 5041da177e4SLinus Torvalds goto out; 5051da177e4SLinus Torvalds type = swp_type(entry); 5061da177e4SLinus Torvalds if (type >= nr_swapfiles) 5071da177e4SLinus Torvalds goto bad_nofile; 508efa90a98SHugh Dickins p = swap_info[type]; 5091da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 5101da177e4SLinus Torvalds goto bad_device; 5111da177e4SLinus Torvalds offset = swp_offset(entry); 5121da177e4SLinus Torvalds if (offset >= p->max) 5131da177e4SLinus Torvalds goto bad_offset; 5141da177e4SLinus Torvalds if (!p->swap_map[offset]) 5151da177e4SLinus Torvalds goto bad_free; 5165d337b91SHugh Dickins spin_lock(&swap_lock); 5171da177e4SLinus Torvalds return p; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds bad_free: 5201da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); 5211da177e4SLinus Torvalds goto out; 5221da177e4SLinus Torvalds bad_offset: 5231da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); 5241da177e4SLinus Torvalds goto out; 5251da177e4SLinus Torvalds bad_device: 5261da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); 5271da177e4SLinus Torvalds goto out; 5281da177e4SLinus Torvalds bad_nofile: 5291da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); 5301da177e4SLinus Torvalds out: 5311da177e4SLinus Torvalds return NULL; 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 534*8d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p, 535*8d69aaeeSHugh Dickins swp_entry_t entry, unsigned char usage) 5361da177e4SLinus Torvalds { 537253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 538*8d69aaeeSHugh Dickins unsigned char count; 539*8d69aaeeSHugh Dickins unsigned char has_cache; 5401da177e4SLinus Torvalds 541355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 542253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 543253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 544253d553bSHugh Dickins 545253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 546253d553bSHugh Dickins VM_BUG_ON(!has_cache); 547253d553bSHugh Dickins has_cache = 0; 548253d553bSHugh Dickins } else if (count < SWAP_MAP_MAX) 549253d553bSHugh Dickins count--; 550253d553bSHugh Dickins 551253d553bSHugh Dickins if (!count) 552253d553bSHugh Dickins mem_cgroup_uncharge_swap(entry); 553253d553bSHugh Dickins 554253d553bSHugh Dickins usage = count | has_cache; 555253d553bSHugh Dickins p->swap_map[offset] = usage; 556253d553bSHugh Dickins 557355cfa73SKAMEZAWA Hiroyuki /* free if no reference */ 558253d553bSHugh Dickins if (!usage) { 5591da177e4SLinus Torvalds if (offset < p->lowest_bit) 5601da177e4SLinus Torvalds p->lowest_bit = offset; 5611da177e4SLinus Torvalds if (offset > p->highest_bit) 5621da177e4SLinus Torvalds p->highest_bit = offset; 563efa90a98SHugh Dickins if (swap_list.next >= 0 && 564efa90a98SHugh Dickins p->prio > swap_info[swap_list.next]->prio) 565efa90a98SHugh Dickins swap_list.next = p->type; 5661da177e4SLinus Torvalds nr_swap_pages++; 5671da177e4SLinus Torvalds p->inuse_pages--; 5681da177e4SLinus Torvalds } 569253d553bSHugh Dickins 570253d553bSHugh Dickins return usage; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /* 5741da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 5751da177e4SLinus Torvalds * is still around or has not been recycled. 5761da177e4SLinus Torvalds */ 5771da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 5781da177e4SLinus Torvalds { 5791da177e4SLinus Torvalds struct swap_info_struct *p; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds p = swap_info_get(entry); 5821da177e4SLinus Torvalds if (p) { 583253d553bSHugh Dickins swap_entry_free(p, entry, 1); 5845d337b91SHugh Dickins spin_unlock(&swap_lock); 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /* 589cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 590cb4b86baSKAMEZAWA Hiroyuki */ 591cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page) 592cb4b86baSKAMEZAWA Hiroyuki { 593355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 594*8d69aaeeSHugh Dickins unsigned char count; 595355cfa73SKAMEZAWA Hiroyuki 596355cfa73SKAMEZAWA Hiroyuki p = swap_info_get(entry); 597355cfa73SKAMEZAWA Hiroyuki if (p) { 598253d553bSHugh Dickins count = swap_entry_free(p, entry, SWAP_HAS_CACHE); 599253d553bSHugh Dickins if (page) 600253d553bSHugh Dickins mem_cgroup_uncharge_swapcache(page, entry, count != 0); 601355cfa73SKAMEZAWA Hiroyuki spin_unlock(&swap_lock); 602355cfa73SKAMEZAWA Hiroyuki } 603cb4b86baSKAMEZAWA Hiroyuki } 604cb4b86baSKAMEZAWA Hiroyuki 605cb4b86baSKAMEZAWA Hiroyuki /* 606c475a8abSHugh Dickins * How many references to page are currently swapped out? 6071da177e4SLinus Torvalds */ 608c475a8abSHugh Dickins static inline int page_swapcount(struct page *page) 6091da177e4SLinus Torvalds { 610c475a8abSHugh Dickins int count = 0; 6111da177e4SLinus Torvalds struct swap_info_struct *p; 6121da177e4SLinus Torvalds swp_entry_t entry; 6131da177e4SLinus Torvalds 6144c21e2f2SHugh Dickins entry.val = page_private(page); 6151da177e4SLinus Torvalds p = swap_info_get(entry); 6161da177e4SLinus Torvalds if (p) { 617355cfa73SKAMEZAWA Hiroyuki count = swap_count(p->swap_map[swp_offset(entry)]); 6185d337b91SHugh Dickins spin_unlock(&swap_lock); 6191da177e4SLinus Torvalds } 620c475a8abSHugh Dickins return count; 6211da177e4SLinus Torvalds } 6221da177e4SLinus Torvalds 6231da177e4SLinus Torvalds /* 6247b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 6257b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 6267b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 6277b1fe597SHugh Dickins * later would only waste time away from clustering. 6281da177e4SLinus Torvalds */ 6297b1fe597SHugh Dickins int reuse_swap_page(struct page *page) 6301da177e4SLinus Torvalds { 631c475a8abSHugh Dickins int count; 6321da177e4SLinus Torvalds 63351726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 634c475a8abSHugh Dickins count = page_mapcount(page); 6357b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 636c475a8abSHugh Dickins count += page_swapcount(page); 6377b1fe597SHugh Dickins if (count == 1 && !PageWriteback(page)) { 6387b1fe597SHugh Dickins delete_from_swap_cache(page); 6397b1fe597SHugh Dickins SetPageDirty(page); 6407b1fe597SHugh Dickins } 6417b1fe597SHugh Dickins } 642c475a8abSHugh Dickins return count == 1; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* 646a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 647a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 6481da177e4SLinus Torvalds */ 649a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 6501da177e4SLinus Torvalds { 65151726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 6521da177e4SLinus Torvalds 6531da177e4SLinus Torvalds if (!PageSwapCache(page)) 6541da177e4SLinus Torvalds return 0; 6551da177e4SLinus Torvalds if (PageWriteback(page)) 6561da177e4SLinus Torvalds return 0; 657a2c43eedSHugh Dickins if (page_swapcount(page)) 6581da177e4SLinus Torvalds return 0; 6591da177e4SLinus Torvalds 660a2c43eedSHugh Dickins delete_from_swap_cache(page); 6611da177e4SLinus Torvalds SetPageDirty(page); 662a2c43eedSHugh Dickins return 1; 66368a22394SRik van Riel } 66468a22394SRik van Riel 66568a22394SRik van Riel /* 6661da177e4SLinus Torvalds * Free the swap entry like above, but also try to 6671da177e4SLinus Torvalds * free the page cache entry if it is the last user. 6681da177e4SLinus Torvalds */ 6692509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 6701da177e4SLinus Torvalds { 6711da177e4SLinus Torvalds struct swap_info_struct *p; 6721da177e4SLinus Torvalds struct page *page = NULL; 6731da177e4SLinus Torvalds 674a7420aa5SAndi Kleen if (non_swap_entry(entry)) 6752509ef26SHugh Dickins return 1; 6760697212aSChristoph Lameter 6771da177e4SLinus Torvalds p = swap_info_get(entry); 6781da177e4SLinus Torvalds if (p) { 679253d553bSHugh Dickins if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) { 68093fac704SNick Piggin page = find_get_page(&swapper_space, entry.val); 6818413ac9dSNick Piggin if (page && !trylock_page(page)) { 68293fac704SNick Piggin page_cache_release(page); 68393fac704SNick Piggin page = NULL; 68493fac704SNick Piggin } 68593fac704SNick Piggin } 6865d337b91SHugh Dickins spin_unlock(&swap_lock); 6871da177e4SLinus Torvalds } 6881da177e4SLinus Torvalds if (page) { 689a2c43eedSHugh Dickins /* 690a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 691a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 692a2c43eedSHugh Dickins */ 69393fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 694a2c43eedSHugh Dickins (!page_mapped(page) || vm_swap_full())) { 6951da177e4SLinus Torvalds delete_from_swap_cache(page); 6961da177e4SLinus Torvalds SetPageDirty(page); 6971da177e4SLinus Torvalds } 6981da177e4SLinus Torvalds unlock_page(page); 6991da177e4SLinus Torvalds page_cache_release(page); 7001da177e4SLinus Torvalds } 7012509ef26SHugh Dickins return p != NULL; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 704b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 705f577eb30SRafael J. Wysocki /* 706915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 707f577eb30SRafael J. Wysocki * 708915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 709915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 710915bae9eSRafael J. Wysocki * 711915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 712f577eb30SRafael J. Wysocki */ 7137bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 714f577eb30SRafael J. Wysocki { 715915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 716efa90a98SHugh Dickins int type; 717f577eb30SRafael J. Wysocki 718915bae9eSRafael J. Wysocki if (device) 719915bae9eSRafael J. Wysocki bdev = bdget(device); 720915bae9eSRafael J. Wysocki 721f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 722efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 723efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 724f577eb30SRafael J. Wysocki 725915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 726f577eb30SRafael J. Wysocki continue; 727b6b5bce3SRafael J. Wysocki 728915bae9eSRafael J. Wysocki if (!bdev) { 7297bf23687SRafael J. Wysocki if (bdev_p) 730dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 7317bf23687SRafael J. Wysocki 7326e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 733efa90a98SHugh Dickins return type; 7346e1819d6SRafael J. Wysocki } 735915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 7369625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 737915bae9eSRafael J. Wysocki 738915bae9eSRafael J. Wysocki if (se->start_block == offset) { 7397bf23687SRafael J. Wysocki if (bdev_p) 740dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 7417bf23687SRafael J. Wysocki 742f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 743915bae9eSRafael J. Wysocki bdput(bdev); 744efa90a98SHugh Dickins return type; 745f577eb30SRafael J. Wysocki } 746f577eb30SRafael J. Wysocki } 747915bae9eSRafael J. Wysocki } 748f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 749915bae9eSRafael J. Wysocki if (bdev) 750915bae9eSRafael J. Wysocki bdput(bdev); 751915bae9eSRafael J. Wysocki 752f577eb30SRafael J. Wysocki return -ENODEV; 753f577eb30SRafael J. Wysocki } 754f577eb30SRafael J. Wysocki 755f577eb30SRafael J. Wysocki /* 75673c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 75773c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 75873c34b6aSHugh Dickins */ 75973c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 76073c34b6aSHugh Dickins { 76173c34b6aSHugh Dickins struct block_device *bdev; 76273c34b6aSHugh Dickins 76373c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 76473c34b6aSHugh Dickins return 0; 76573c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 76673c34b6aSHugh Dickins return 0; 76773c34b6aSHugh Dickins return map_swap_page(swp_entry(type, offset), &bdev); 76873c34b6aSHugh Dickins } 76973c34b6aSHugh Dickins 77073c34b6aSHugh Dickins /* 771f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 772f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 773f577eb30SRafael J. Wysocki * 774f577eb30SRafael J. Wysocki * This is needed for software suspend 775f577eb30SRafael J. Wysocki */ 776f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 777f577eb30SRafael J. Wysocki { 778f577eb30SRafael J. Wysocki unsigned int n = 0; 779f577eb30SRafael J. Wysocki 780f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 781efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 782efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 783efa90a98SHugh Dickins 784efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 785efa90a98SHugh Dickins n = sis->pages; 786f577eb30SRafael J. Wysocki if (free) 787efa90a98SHugh Dickins n -= sis->inuse_pages; 788efa90a98SHugh Dickins } 789f577eb30SRafael J. Wysocki } 790f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 791f577eb30SRafael J. Wysocki return n; 792f577eb30SRafael J. Wysocki } 79373c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 794f577eb30SRafael J. Wysocki 7951da177e4SLinus Torvalds /* 79672866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 79772866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 79872866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 7991da177e4SLinus Torvalds */ 800044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 8011da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 8021da177e4SLinus Torvalds { 8037a81b88cSKAMEZAWA Hiroyuki struct mem_cgroup *ptr = NULL; 804044d66c1SHugh Dickins spinlock_t *ptl; 805044d66c1SHugh Dickins pte_t *pte; 806044d66c1SHugh Dickins int ret = 1; 807044d66c1SHugh Dickins 80885d9fc89SKAMEZAWA Hiroyuki if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) { 809044d66c1SHugh Dickins ret = -ENOMEM; 81085d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 81185d9fc89SKAMEZAWA Hiroyuki } 812044d66c1SHugh Dickins 813044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 814044d66c1SHugh Dickins if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) { 815044d66c1SHugh Dickins if (ret > 0) 8167a81b88cSKAMEZAWA Hiroyuki mem_cgroup_cancel_charge_swapin(ptr); 817044d66c1SHugh Dickins ret = 0; 818044d66c1SHugh Dickins goto out; 819044d66c1SHugh Dickins } 8208a9f3ccdSBalbir Singh 8214294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 8221da177e4SLinus Torvalds get_page(page); 8231da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 8241da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 8251da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 8267a81b88cSKAMEZAWA Hiroyuki mem_cgroup_commit_charge_swapin(page, ptr); 8271da177e4SLinus Torvalds swap_free(entry); 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * Move the page to the active list so it is not 8301da177e4SLinus Torvalds * immediately swapped out again after swapon. 8311da177e4SLinus Torvalds */ 8321da177e4SLinus Torvalds activate_page(page); 833044d66c1SHugh Dickins out: 834044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 83585d9fc89SKAMEZAWA Hiroyuki out_nolock: 836044d66c1SHugh Dickins return ret; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 8401da177e4SLinus Torvalds unsigned long addr, unsigned long end, 8411da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 8421da177e4SLinus Torvalds { 8431da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 844705e87c0SHugh Dickins pte_t *pte; 8458a9f3ccdSBalbir Singh int ret = 0; 8461da177e4SLinus Torvalds 847044d66c1SHugh Dickins /* 848044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 849044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 850044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 851044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 852044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 853044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 854044d66c1SHugh Dickins * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 855044d66c1SHugh Dickins */ 856044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 8571da177e4SLinus Torvalds do { 8581da177e4SLinus Torvalds /* 8591da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 8601da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 8611da177e4SLinus Torvalds */ 8621da177e4SLinus Torvalds if (unlikely(pte_same(*pte, swp_pte))) { 863044d66c1SHugh Dickins pte_unmap(pte); 864044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 865044d66c1SHugh Dickins if (ret) 866044d66c1SHugh Dickins goto out; 867044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 870044d66c1SHugh Dickins pte_unmap(pte - 1); 871044d66c1SHugh Dickins out: 8728a9f3ccdSBalbir Singh return ret; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 8761da177e4SLinus Torvalds unsigned long addr, unsigned long end, 8771da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds pmd_t *pmd; 8801da177e4SLinus Torvalds unsigned long next; 8818a9f3ccdSBalbir Singh int ret; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 8841da177e4SLinus Torvalds do { 8851da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 8861da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 8871da177e4SLinus Torvalds continue; 8888a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 8898a9f3ccdSBalbir Singh if (ret) 8908a9f3ccdSBalbir Singh return ret; 8911da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 8921da177e4SLinus Torvalds return 0; 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 8961da177e4SLinus Torvalds unsigned long addr, unsigned long end, 8971da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds pud_t *pud; 9001da177e4SLinus Torvalds unsigned long next; 9018a9f3ccdSBalbir Singh int ret; 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 9041da177e4SLinus Torvalds do { 9051da177e4SLinus Torvalds next = pud_addr_end(addr, end); 9061da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 9071da177e4SLinus Torvalds continue; 9088a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 9098a9f3ccdSBalbir Singh if (ret) 9108a9f3ccdSBalbir Singh return ret; 9111da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 9121da177e4SLinus Torvalds return 0; 9131da177e4SLinus Torvalds } 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 9161da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 9171da177e4SLinus Torvalds { 9181da177e4SLinus Torvalds pgd_t *pgd; 9191da177e4SLinus Torvalds unsigned long addr, end, next; 9208a9f3ccdSBalbir Singh int ret; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (page->mapping) { 9231da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 9241da177e4SLinus Torvalds if (addr == -EFAULT) 9251da177e4SLinus Torvalds return 0; 9261da177e4SLinus Torvalds else 9271da177e4SLinus Torvalds end = addr + PAGE_SIZE; 9281da177e4SLinus Torvalds } else { 9291da177e4SLinus Torvalds addr = vma->vm_start; 9301da177e4SLinus Torvalds end = vma->vm_end; 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 9341da177e4SLinus Torvalds do { 9351da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 9361da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 9371da177e4SLinus Torvalds continue; 9388a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 9398a9f3ccdSBalbir Singh if (ret) 9408a9f3ccdSBalbir Singh return ret; 9411da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 9421da177e4SLinus Torvalds return 0; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 9461da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 9471da177e4SLinus Torvalds { 9481da177e4SLinus Torvalds struct vm_area_struct *vma; 9498a9f3ccdSBalbir Singh int ret = 0; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 9521da177e4SLinus Torvalds /* 9537d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 9547d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 9551da177e4SLinus Torvalds */ 956c475a8abSHugh Dickins activate_page(page); 9571da177e4SLinus Torvalds unlock_page(page); 9581da177e4SLinus Torvalds down_read(&mm->mmap_sem); 9591da177e4SLinus Torvalds lock_page(page); 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 9628a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 9631da177e4SLinus Torvalds break; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds up_read(&mm->mmap_sem); 9668a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 9711da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 9721da177e4SLinus Torvalds */ 9736eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 9746eb396dcSHugh Dickins unsigned int prev) 9751da177e4SLinus Torvalds { 9766eb396dcSHugh Dickins unsigned int max = si->max; 9776eb396dcSHugh Dickins unsigned int i = prev; 978*8d69aaeeSHugh Dickins unsigned char count; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* 9815d337b91SHugh Dickins * No need for swap_lock here: we're just looking 9821da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 9831da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 9845d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 9851da177e4SLinus Torvalds */ 9861da177e4SLinus Torvalds for (;;) { 9871da177e4SLinus Torvalds if (++i >= max) { 9881da177e4SLinus Torvalds if (!prev) { 9891da177e4SLinus Torvalds i = 0; 9901da177e4SLinus Torvalds break; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds /* 9931da177e4SLinus Torvalds * No entries in use at top of swap_map, 9941da177e4SLinus Torvalds * loop back to start and recheck there. 9951da177e4SLinus Torvalds */ 9961da177e4SLinus Torvalds max = prev + 1; 9971da177e4SLinus Torvalds prev = 0; 9981da177e4SLinus Torvalds i = 1; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds count = si->swap_map[i]; 1001355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 10021da177e4SLinus Torvalds break; 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds return i; 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds /* 10081da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 10091da177e4SLinus Torvalds * and then search for the process using it. All the necessary 10101da177e4SLinus Torvalds * page table adjustments can then be made atomically. 10111da177e4SLinus Torvalds */ 10121da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 10131da177e4SLinus Torvalds { 1014efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 10151da177e4SLinus Torvalds struct mm_struct *start_mm; 1016*8d69aaeeSHugh Dickins unsigned char *swap_map; 1017*8d69aaeeSHugh Dickins unsigned char swcount; 10181da177e4SLinus Torvalds struct page *page; 10191da177e4SLinus Torvalds swp_entry_t entry; 10206eb396dcSHugh Dickins unsigned int i = 0; 10211da177e4SLinus Torvalds int retval = 0; 10221da177e4SLinus Torvalds int reset_overflow = 0; 10231da177e4SLinus Torvalds int shmem; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* 10261da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 10271da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 10281da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 10291da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 10301da177e4SLinus Torvalds * 10311da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 10321da177e4SLinus Torvalds * freed the previous entry from; but that would take less 10331da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 10341da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 10351da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 10361da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 10371da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 10381da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 10391da177e4SLinus Torvalds */ 10401da177e4SLinus Torvalds start_mm = &init_mm; 10411da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /* 10441da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 10451da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 10461da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 10471da177e4SLinus Torvalds */ 10481da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 10491da177e4SLinus Torvalds if (signal_pending(current)) { 10501da177e4SLinus Torvalds retval = -EINTR; 10511da177e4SLinus Torvalds break; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /* 10551da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 10561da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 10571da177e4SLinus Torvalds * page and read the swap into it. 10581da177e4SLinus Torvalds */ 10591da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 10601da177e4SLinus Torvalds entry = swp_entry(type, i); 106102098feaSHugh Dickins page = read_swap_cache_async(entry, 106202098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 10631da177e4SLinus Torvalds if (!page) { 10641da177e4SLinus Torvalds /* 10651da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 10661da177e4SLinus Torvalds * has been freed independently, and will not be 10671da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 10681da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 10691da177e4SLinus Torvalds */ 10701da177e4SLinus Torvalds if (!*swap_map) 10711da177e4SLinus Torvalds continue; 10721da177e4SLinus Torvalds retval = -ENOMEM; 10731da177e4SLinus Torvalds break; 10741da177e4SLinus Torvalds } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 10771da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 10781da177e4SLinus Torvalds */ 10791da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 10801da177e4SLinus Torvalds mmput(start_mm); 10811da177e4SLinus Torvalds start_mm = &init_mm; 10821da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds /* 10861da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 10871da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 10881da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 10891da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 10901da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 10911da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 10921da177e4SLinus Torvalds */ 10931da177e4SLinus Torvalds wait_on_page_locked(page); 10941da177e4SLinus Torvalds wait_on_page_writeback(page); 10951da177e4SLinus Torvalds lock_page(page); 10961da177e4SLinus Torvalds wait_on_page_writeback(page); 10971da177e4SLinus Torvalds 10981da177e4SLinus Torvalds /* 10991da177e4SLinus Torvalds * Remove all references to entry. 11001da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 11011da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds shmem = 0; 11041da177e4SLinus Torvalds swcount = *swap_map; 1105355cfa73SKAMEZAWA Hiroyuki if (swap_count(swcount)) { 11061da177e4SLinus Torvalds if (start_mm == &init_mm) 11071da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 11081da177e4SLinus Torvalds else 11091da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 11101da177e4SLinus Torvalds } 1111355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 11121da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 11131da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 11141da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 11151da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 11161da177e4SLinus Torvalds struct mm_struct *mm; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 11191da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 11201da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1121355cfa73SKAMEZAWA Hiroyuki while (swap_count(*swap_map) && !retval && !shmem && 11221da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 11231da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 112470af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 11251da177e4SLinus Torvalds continue; 11261da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 11271da177e4SLinus Torvalds mmput(prev_mm); 11281da177e4SLinus Torvalds prev_mm = mm; 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds cond_resched(); 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds swcount = *swap_map; 1133355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 11341da177e4SLinus Torvalds ; 11351da177e4SLinus Torvalds else if (mm == &init_mm) { 11361da177e4SLinus Torvalds set_start_mm = 1; 11371da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 11381da177e4SLinus Torvalds } else 11391da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1140355cfa73SKAMEZAWA Hiroyuki 114132c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 11421da177e4SLinus Torvalds mmput(new_start_mm); 11431da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 11441da177e4SLinus Torvalds new_start_mm = mm; 11451da177e4SLinus Torvalds set_start_mm = 0; 11461da177e4SLinus Torvalds } 11471da177e4SLinus Torvalds spin_lock(&mmlist_lock); 11481da177e4SLinus Torvalds } 11491da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 11501da177e4SLinus Torvalds mmput(prev_mm); 11511da177e4SLinus Torvalds mmput(start_mm); 11521da177e4SLinus Torvalds start_mm = new_start_mm; 11531da177e4SLinus Torvalds } 11542e0e26c7SHugh Dickins if (shmem) { 11552e0e26c7SHugh Dickins /* page has already been unlocked and released */ 11562e0e26c7SHugh Dickins if (shmem > 0) 11572e0e26c7SHugh Dickins continue; 11582e0e26c7SHugh Dickins retval = shmem; 11592e0e26c7SHugh Dickins break; 11602e0e26c7SHugh Dickins } 11611da177e4SLinus Torvalds if (retval) { 11621da177e4SLinus Torvalds unlock_page(page); 11631da177e4SLinus Torvalds page_cache_release(page); 11641da177e4SLinus Torvalds break; 11651da177e4SLinus Torvalds } 11661da177e4SLinus Torvalds 11671da177e4SLinus Torvalds /* 1168355cfa73SKAMEZAWA Hiroyuki * How could swap count reach 0x7ffe ? 1169355cfa73SKAMEZAWA Hiroyuki * There's no way to repeat a swap page within an mm 1170355cfa73SKAMEZAWA Hiroyuki * (except in shmem, where it's the shared object which takes 1171355cfa73SKAMEZAWA Hiroyuki * the reference count)? 1172355cfa73SKAMEZAWA Hiroyuki * We believe SWAP_MAP_MAX cannot occur.(if occur, unsigned 1173355cfa73SKAMEZAWA Hiroyuki * short is too small....) 11741da177e4SLinus Torvalds * If that's wrong, then we should worry more about 11751da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 11761da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 1177*8d69aaeeSHugh Dickins * 1178*8d69aaeeSHugh Dickins * Yes, that's wrong: though very unlikely, swap count 0x7ffe 1179*8d69aaeeSHugh Dickins * could surely occur if pid_max raised from PID_MAX_DEFAULT; 1180*8d69aaeeSHugh Dickins * and we are now lowering SWAP_MAP_MAX to 0x7e, making it 1181*8d69aaeeSHugh Dickins * much easier to reach. But the next patch will fix that. 1182*8d69aaeeSHugh Dickins * 11831da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 11841da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 11851da177e4SLinus Torvalds */ 1186355cfa73SKAMEZAWA Hiroyuki /* We might release the lock_page() in unuse_mm(). */ 1187355cfa73SKAMEZAWA Hiroyuki if (!PageSwapCache(page) || page_private(page) != entry.val) 1188355cfa73SKAMEZAWA Hiroyuki goto retry; 1189355cfa73SKAMEZAWA Hiroyuki 1190355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) == SWAP_MAP_MAX) { 11915d337b91SHugh Dickins spin_lock(&swap_lock); 1192253d553bSHugh Dickins *swap_map = SWAP_HAS_CACHE; 11935d337b91SHugh Dickins spin_unlock(&swap_lock); 11941da177e4SLinus Torvalds reset_overflow = 1; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 11991da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 12001da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 12011da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 12021da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 12031da177e4SLinus Torvalds * delete from cache even if there's another reference, 12041da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 12051da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 12061da177e4SLinus Torvalds * read from disk into another page. Splitting into two 12071da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 12081da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 12091da177e4SLinus Torvalds */ 1210355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 1211355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 12121da177e4SLinus Torvalds struct writeback_control wbc = { 12131da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 12141da177e4SLinus Torvalds }; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds swap_writepage(page, &wbc); 12171da177e4SLinus Torvalds lock_page(page); 12181da177e4SLinus Torvalds wait_on_page_writeback(page); 12191da177e4SLinus Torvalds } 122068bdc8d6SHugh Dickins 122168bdc8d6SHugh Dickins /* 122268bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 122368bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 122468bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 122568bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 122668bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 122768bdc8d6SHugh Dickins */ 122868bdc8d6SHugh Dickins if (PageSwapCache(page) && 122968bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 12301da177e4SLinus Torvalds delete_from_swap_cache(page); 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds /* 12331da177e4SLinus Torvalds * So we could skip searching mms once swap count went 12341da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 12352706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 12361da177e4SLinus Torvalds */ 12371da177e4SLinus Torvalds SetPageDirty(page); 1238355cfa73SKAMEZAWA Hiroyuki retry: 12391da177e4SLinus Torvalds unlock_page(page); 12401da177e4SLinus Torvalds page_cache_release(page); 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* 12431da177e4SLinus Torvalds * Make sure that we aren't completely killing 12441da177e4SLinus Torvalds * interactive performance. 12451da177e4SLinus Torvalds */ 12461da177e4SLinus Torvalds cond_resched(); 12471da177e4SLinus Torvalds } 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds mmput(start_mm); 12501da177e4SLinus Torvalds if (reset_overflow) { 12511da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 12521da177e4SLinus Torvalds swap_overflow = 0; 12531da177e4SLinus Torvalds } 12541da177e4SLinus Torvalds return retval; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* 12585d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 12595d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 12605d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 12611da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 12621da177e4SLinus Torvalds */ 12631da177e4SLinus Torvalds static void drain_mmlist(void) 12641da177e4SLinus Torvalds { 12651da177e4SLinus Torvalds struct list_head *p, *next; 1266efa90a98SHugh Dickins unsigned int type; 12671da177e4SLinus Torvalds 1268efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 1269efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 12701da177e4SLinus Torvalds return; 12711da177e4SLinus Torvalds spin_lock(&mmlist_lock); 12721da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 12731da177e4SLinus Torvalds list_del_init(p); 12741da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 1279f29ad6a9SHugh Dickins * corresponds to page offset `offset'. Note that the type of this function 1280f29ad6a9SHugh Dickins * is sector_t, but it returns page offset into the bdev, not sector offset. 12811da177e4SLinus Torvalds */ 1282f29ad6a9SHugh Dickins sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev) 12831da177e4SLinus Torvalds { 1284f29ad6a9SHugh Dickins struct swap_info_struct *sis; 1285f29ad6a9SHugh Dickins struct swap_extent *start_se; 1286f29ad6a9SHugh Dickins struct swap_extent *se; 1287f29ad6a9SHugh Dickins pgoff_t offset; 1288f29ad6a9SHugh Dickins 1289efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 1290f29ad6a9SHugh Dickins *bdev = sis->bdev; 1291f29ad6a9SHugh Dickins 1292f29ad6a9SHugh Dickins offset = swp_offset(entry); 1293f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 1294f29ad6a9SHugh Dickins se = start_se; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds for ( ; ; ) { 12971da177e4SLinus Torvalds struct list_head *lh; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds if (se->start_page <= offset && 13001da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 13011da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 13021da177e4SLinus Torvalds } 130311d31886SHugh Dickins lh = se->list.next; 13041da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 13051da177e4SLinus Torvalds sis->curr_swap_extent = se; 13061da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* 13111da177e4SLinus Torvalds * Free all of a swapdev's extent information 13121da177e4SLinus Torvalds */ 13131da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 13141da177e4SLinus Torvalds { 13159625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 13161da177e4SLinus Torvalds struct swap_extent *se; 13171da177e4SLinus Torvalds 13189625a5f2SHugh Dickins se = list_entry(sis->first_swap_extent.list.next, 13191da177e4SLinus Torvalds struct swap_extent, list); 13201da177e4SLinus Torvalds list_del(&se->list); 13211da177e4SLinus Torvalds kfree(se); 13221da177e4SLinus Torvalds } 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /* 13261da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 132711d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 13281da177e4SLinus Torvalds * 132911d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 13301da177e4SLinus Torvalds */ 13311da177e4SLinus Torvalds static int 13321da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 13331da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 13341da177e4SLinus Torvalds { 13351da177e4SLinus Torvalds struct swap_extent *se; 13361da177e4SLinus Torvalds struct swap_extent *new_se; 13371da177e4SLinus Torvalds struct list_head *lh; 13381da177e4SLinus Torvalds 13399625a5f2SHugh Dickins if (start_page == 0) { 13409625a5f2SHugh Dickins se = &sis->first_swap_extent; 13419625a5f2SHugh Dickins sis->curr_swap_extent = se; 13429625a5f2SHugh Dickins se->start_page = 0; 13439625a5f2SHugh Dickins se->nr_pages = nr_pages; 13449625a5f2SHugh Dickins se->start_block = start_block; 13459625a5f2SHugh Dickins return 1; 13469625a5f2SHugh Dickins } else { 13479625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 13481da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 134911d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 135011d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 13511da177e4SLinus Torvalds /* Merge it */ 13521da177e4SLinus Torvalds se->nr_pages += nr_pages; 13531da177e4SLinus Torvalds return 0; 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 13611da177e4SLinus Torvalds if (new_se == NULL) 13621da177e4SLinus Torvalds return -ENOMEM; 13631da177e4SLinus Torvalds new_se->start_page = start_page; 13641da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 13651da177e4SLinus Torvalds new_se->start_block = start_block; 13661da177e4SLinus Torvalds 13679625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 136853092a74SHugh Dickins return 1; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds /* 13721da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 13731da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 13741da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 13751da177e4SLinus Torvalds * time for locating where on disk a page belongs. 13761da177e4SLinus Torvalds * 13771da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 13781da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 13791da177e4SLinus Torvalds * swap files identically. 13801da177e4SLinus Torvalds * 13811da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 13821da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 13831da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 13841da177e4SLinus Torvalds * 13851da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 13861da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 13871da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 13881da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 13891da177e4SLinus Torvalds * for swapping. 13901da177e4SLinus Torvalds * 1391b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 13921da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 13931da177e4SLinus Torvalds * which will scribble on the fs. 13941da177e4SLinus Torvalds * 13951da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 13961da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 13971da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 13981da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 13991da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 14001da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 14011da177e4SLinus Torvalds */ 140253092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 14031da177e4SLinus Torvalds { 14041da177e4SLinus Torvalds struct inode *inode; 14051da177e4SLinus Torvalds unsigned blocks_per_page; 14061da177e4SLinus Torvalds unsigned long page_no; 14071da177e4SLinus Torvalds unsigned blkbits; 14081da177e4SLinus Torvalds sector_t probe_block; 14091da177e4SLinus Torvalds sector_t last_block; 141053092a74SHugh Dickins sector_t lowest_block = -1; 141153092a74SHugh Dickins sector_t highest_block = 0; 141253092a74SHugh Dickins int nr_extents = 0; 14131da177e4SLinus Torvalds int ret; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 14161da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 14171da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 141853092a74SHugh Dickins *span = sis->pages; 14199625a5f2SHugh Dickins goto out; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds blkbits = inode->i_blkbits; 14231da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* 14261da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 14271da177e4SLinus Torvalds * to be very smart. 14281da177e4SLinus Torvalds */ 14291da177e4SLinus Torvalds probe_block = 0; 14301da177e4SLinus Torvalds page_no = 0; 14311da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 14321da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 14331da177e4SLinus Torvalds page_no < sis->max) { 14341da177e4SLinus Torvalds unsigned block_in_page; 14351da177e4SLinus Torvalds sector_t first_block; 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 14381da177e4SLinus Torvalds if (first_block == 0) 14391da177e4SLinus Torvalds goto bad_bmap; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds /* 14421da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 14431da177e4SLinus Torvalds */ 14441da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 14451da177e4SLinus Torvalds probe_block++; 14461da177e4SLinus Torvalds goto reprobe; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 14501da177e4SLinus Torvalds block_in_page++) { 14511da177e4SLinus Torvalds sector_t block; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 14541da177e4SLinus Torvalds if (block == 0) 14551da177e4SLinus Torvalds goto bad_bmap; 14561da177e4SLinus Torvalds if (block != first_block + block_in_page) { 14571da177e4SLinus Torvalds /* Discontiguity */ 14581da177e4SLinus Torvalds probe_block++; 14591da177e4SLinus Torvalds goto reprobe; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 146353092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 146453092a74SHugh Dickins if (page_no) { /* exclude the header page */ 146553092a74SHugh Dickins if (first_block < lowest_block) 146653092a74SHugh Dickins lowest_block = first_block; 146753092a74SHugh Dickins if (first_block > highest_block) 146853092a74SHugh Dickins highest_block = first_block; 146953092a74SHugh Dickins } 147053092a74SHugh Dickins 14711da177e4SLinus Torvalds /* 14721da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 14731da177e4SLinus Torvalds */ 147453092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 147553092a74SHugh Dickins if (ret < 0) 14761da177e4SLinus Torvalds goto out; 147753092a74SHugh Dickins nr_extents += ret; 14781da177e4SLinus Torvalds page_no++; 14791da177e4SLinus Torvalds probe_block += blocks_per_page; 14801da177e4SLinus Torvalds reprobe: 14811da177e4SLinus Torvalds continue; 14821da177e4SLinus Torvalds } 148353092a74SHugh Dickins ret = nr_extents; 148453092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 14851da177e4SLinus Torvalds if (page_no == 0) 1486e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 14871da177e4SLinus Torvalds sis->max = page_no; 1488e2244ec2SHugh Dickins sis->pages = page_no - 1; 14891da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 14909625a5f2SHugh Dickins out: 14919625a5f2SHugh Dickins return ret; 14921da177e4SLinus Torvalds bad_bmap: 14931da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 14941da177e4SLinus Torvalds ret = -EINVAL; 14959625a5f2SHugh Dickins goto out; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 1498c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 14991da177e4SLinus Torvalds { 15001da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 1501*8d69aaeeSHugh Dickins unsigned char *swap_map; 15021da177e4SLinus Torvalds struct file *swap_file, *victim; 15031da177e4SLinus Torvalds struct address_space *mapping; 15041da177e4SLinus Torvalds struct inode *inode; 15051da177e4SLinus Torvalds char *pathname; 15061da177e4SLinus Torvalds int i, type, prev; 15071da177e4SLinus Torvalds int err; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 15101da177e4SLinus Torvalds return -EPERM; 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds pathname = getname(specialfile); 15131da177e4SLinus Torvalds err = PTR_ERR(pathname); 15141da177e4SLinus Torvalds if (IS_ERR(pathname)) 15151da177e4SLinus Torvalds goto out; 15161da177e4SLinus Torvalds 15171da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 15181da177e4SLinus Torvalds putname(pathname); 15191da177e4SLinus Torvalds err = PTR_ERR(victim); 15201da177e4SLinus Torvalds if (IS_ERR(victim)) 15211da177e4SLinus Torvalds goto out; 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds mapping = victim->f_mapping; 15241da177e4SLinus Torvalds prev = -1; 15255d337b91SHugh Dickins spin_lock(&swap_lock); 1526efa90a98SHugh Dickins for (type = swap_list.head; type >= 0; type = swap_info[type]->next) { 1527efa90a98SHugh Dickins p = swap_info[type]; 152822c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 15291da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 15301da177e4SLinus Torvalds break; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds prev = type; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds if (type < 0) { 15351da177e4SLinus Torvalds err = -EINVAL; 15365d337b91SHugh Dickins spin_unlock(&swap_lock); 15371da177e4SLinus Torvalds goto out_dput; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 15401da177e4SLinus Torvalds vm_unacct_memory(p->pages); 15411da177e4SLinus Torvalds else { 15421da177e4SLinus Torvalds err = -ENOMEM; 15435d337b91SHugh Dickins spin_unlock(&swap_lock); 15441da177e4SLinus Torvalds goto out_dput; 15451da177e4SLinus Torvalds } 1546efa90a98SHugh Dickins if (prev < 0) 15471da177e4SLinus Torvalds swap_list.head = p->next; 1548efa90a98SHugh Dickins else 1549efa90a98SHugh Dickins swap_info[prev]->next = p->next; 15501da177e4SLinus Torvalds if (type == swap_list.next) { 15511da177e4SLinus Torvalds /* just pick something that's safe... */ 15521da177e4SLinus Torvalds swap_list.next = swap_list.head; 15531da177e4SLinus Torvalds } 155478ecba08SHugh Dickins if (p->prio < 0) { 1555efa90a98SHugh Dickins for (i = p->next; i >= 0; i = swap_info[i]->next) 1556efa90a98SHugh Dickins swap_info[i]->prio = p->prio--; 155778ecba08SHugh Dickins least_priority++; 155878ecba08SHugh Dickins } 15591da177e4SLinus Torvalds nr_swap_pages -= p->pages; 15601da177e4SLinus Torvalds total_swap_pages -= p->pages; 15611da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 15625d337b91SHugh Dickins spin_unlock(&swap_lock); 1563fb4f88dcSHugh Dickins 156435451beeSHugh Dickins current->flags |= PF_OOM_ORIGIN; 15651da177e4SLinus Torvalds err = try_to_unuse(type); 156635451beeSHugh Dickins current->flags &= ~PF_OOM_ORIGIN; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds if (err) { 15691da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 15705d337b91SHugh Dickins spin_lock(&swap_lock); 157178ecba08SHugh Dickins if (p->prio < 0) 157278ecba08SHugh Dickins p->prio = --least_priority; 157378ecba08SHugh Dickins prev = -1; 1574efa90a98SHugh Dickins for (i = swap_list.head; i >= 0; i = swap_info[i]->next) { 1575efa90a98SHugh Dickins if (p->prio >= swap_info[i]->prio) 15761da177e4SLinus Torvalds break; 157778ecba08SHugh Dickins prev = i; 157878ecba08SHugh Dickins } 15791da177e4SLinus Torvalds p->next = i; 15801da177e4SLinus Torvalds if (prev < 0) 1581efa90a98SHugh Dickins swap_list.head = swap_list.next = type; 15821da177e4SLinus Torvalds else 1583efa90a98SHugh Dickins swap_info[prev]->next = type; 15841da177e4SLinus Torvalds nr_swap_pages += p->pages; 15851da177e4SLinus Torvalds total_swap_pages += p->pages; 15861da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 15875d337b91SHugh Dickins spin_unlock(&swap_lock); 15881da177e4SLinus Torvalds goto out_dput; 15891da177e4SLinus Torvalds } 159052b7efdbSHugh Dickins 159152b7efdbSHugh Dickins /* wait for any unplug function to finish */ 159252b7efdbSHugh Dickins down_write(&swap_unplug_sem); 159352b7efdbSHugh Dickins up_write(&swap_unplug_sem); 159452b7efdbSHugh Dickins 15954cd3bb10SHugh Dickins destroy_swap_extents(p); 1596fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 15975d337b91SHugh Dickins spin_lock(&swap_lock); 15981da177e4SLinus Torvalds drain_mmlist(); 15995d337b91SHugh Dickins 16005d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 16015d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 16025d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 16035d337b91SHugh Dickins spin_unlock(&swap_lock); 160413e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 16055d337b91SHugh Dickins spin_lock(&swap_lock); 16065d337b91SHugh Dickins } 16075d337b91SHugh Dickins 16081da177e4SLinus Torvalds swap_file = p->swap_file; 16091da177e4SLinus Torvalds p->swap_file = NULL; 16101da177e4SLinus Torvalds p->max = 0; 16111da177e4SLinus Torvalds swap_map = p->swap_map; 16121da177e4SLinus Torvalds p->swap_map = NULL; 16131da177e4SLinus Torvalds p->flags = 0; 16145d337b91SHugh Dickins spin_unlock(&swap_lock); 1615fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 16161da177e4SLinus Torvalds vfree(swap_map); 161727a7faa0SKAMEZAWA Hiroyuki /* Destroy swap account informatin */ 161827a7faa0SKAMEZAWA Hiroyuki swap_cgroup_swapoff(type); 161927a7faa0SKAMEZAWA Hiroyuki 16201da177e4SLinus Torvalds inode = mapping->host; 16211da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 16221da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 16231da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 16241da177e4SLinus Torvalds bd_release(bdev); 16251da177e4SLinus Torvalds } else { 16261b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 16271da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 16281b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 16291da177e4SLinus Torvalds } 16301da177e4SLinus Torvalds filp_close(swap_file, NULL); 16311da177e4SLinus Torvalds err = 0; 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds out_dput: 16341da177e4SLinus Torvalds filp_close(victim, NULL); 16351da177e4SLinus Torvalds out: 16361da177e4SLinus Torvalds return err; 16371da177e4SLinus Torvalds } 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 16401da177e4SLinus Torvalds /* iterator */ 16411da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 16421da177e4SLinus Torvalds { 1643efa90a98SHugh Dickins struct swap_info_struct *si; 1644efa90a98SHugh Dickins int type; 16451da177e4SLinus Torvalds loff_t l = *pos; 16461da177e4SLinus Torvalds 1647fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 16481da177e4SLinus Torvalds 1649881e4aabSSuleiman Souhlal if (!l) 1650881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1651881e4aabSSuleiman Souhlal 1652efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1653efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 1654efa90a98SHugh Dickins si = swap_info[type]; 1655efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 16561da177e4SLinus Torvalds continue; 1657881e4aabSSuleiman Souhlal if (!--l) 1658efa90a98SHugh Dickins return si; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds return NULL; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 16651da177e4SLinus Torvalds { 1666efa90a98SHugh Dickins struct swap_info_struct *si = v; 1667efa90a98SHugh Dickins int type; 16681da177e4SLinus Torvalds 1669881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 1670efa90a98SHugh Dickins type = 0; 1671efa90a98SHugh Dickins else 1672efa90a98SHugh Dickins type = si->type + 1; 1673881e4aabSSuleiman Souhlal 1674efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 1675efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 1676efa90a98SHugh Dickins si = swap_info[type]; 1677efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 16781da177e4SLinus Torvalds continue; 16791da177e4SLinus Torvalds ++*pos; 1680efa90a98SHugh Dickins return si; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds return NULL; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 16871da177e4SLinus Torvalds { 1688fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 16921da177e4SLinus Torvalds { 1693efa90a98SHugh Dickins struct swap_info_struct *si = v; 16941da177e4SLinus Torvalds struct file *file; 16951da177e4SLinus Torvalds int len; 16961da177e4SLinus Torvalds 1697efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 16981da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 1699881e4aabSSuleiman Souhlal return 0; 1700881e4aabSSuleiman Souhlal } 17011da177e4SLinus Torvalds 1702efa90a98SHugh Dickins file = si->swap_file; 1703c32c2f63SJan Blunck len = seq_path(swap, &file->f_path, " \t\n\\"); 17046eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 17051da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 1706d3ac7f89SJosef "Jeff" Sipek S_ISBLK(file->f_path.dentry->d_inode->i_mode) ? 17071da177e4SLinus Torvalds "partition" : "file\t", 1708efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 1709efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 1710efa90a98SHugh Dickins si->prio); 17111da177e4SLinus Torvalds return 0; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 171415ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 17151da177e4SLinus Torvalds .start = swap_start, 17161da177e4SLinus Torvalds .next = swap_next, 17171da177e4SLinus Torvalds .stop = swap_stop, 17181da177e4SLinus Torvalds .show = swap_show 17191da177e4SLinus Torvalds }; 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 17221da177e4SLinus Torvalds { 17231da177e4SLinus Torvalds return seq_open(file, &swaps_op); 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 172615ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 17271da177e4SLinus Torvalds .open = swaps_open, 17281da177e4SLinus Torvalds .read = seq_read, 17291da177e4SLinus Torvalds .llseek = seq_lseek, 17301da177e4SLinus Torvalds .release = seq_release, 17311da177e4SLinus Torvalds }; 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds static int __init procswaps_init(void) 17341da177e4SLinus Torvalds { 17353d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 17361da177e4SLinus Torvalds return 0; 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds __initcall(procswaps_init); 17391da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 17401da177e4SLinus Torvalds 17411796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 17421796316aSJan Beulich static int __init max_swapfiles_check(void) 17431796316aSJan Beulich { 17441796316aSJan Beulich MAX_SWAPFILES_CHECK(); 17451796316aSJan Beulich return 0; 17461796316aSJan Beulich } 17471796316aSJan Beulich late_initcall(max_swapfiles_check); 17481796316aSJan Beulich #endif 17491796316aSJan Beulich 17501da177e4SLinus Torvalds /* 17511da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 17521da177e4SLinus Torvalds * 17531da177e4SLinus Torvalds * The swapon system call 17541da177e4SLinus Torvalds */ 1755c4ea37c2SHeiko Carstens SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds struct swap_info_struct *p; 17581da177e4SLinus Torvalds char *name = NULL; 17591da177e4SLinus Torvalds struct block_device *bdev = NULL; 17601da177e4SLinus Torvalds struct file *swap_file = NULL; 17611da177e4SLinus Torvalds struct address_space *mapping; 17621da177e4SLinus Torvalds unsigned int type; 17631da177e4SLinus Torvalds int i, prev; 17641da177e4SLinus Torvalds int error; 17651da177e4SLinus Torvalds union swap_header *swap_header = NULL; 17666eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 17676eb396dcSHugh Dickins int nr_extents = 0; 176853092a74SHugh Dickins sector_t span; 17691da177e4SLinus Torvalds unsigned long maxpages = 1; 177073fd8748SHugh Dickins unsigned long swapfilepages; 1771*8d69aaeeSHugh Dickins unsigned char *swap_map = NULL; 17721da177e4SLinus Torvalds struct page *page = NULL; 17731da177e4SLinus Torvalds struct inode *inode = NULL; 17741da177e4SLinus Torvalds int did_down = 0; 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17771da177e4SLinus Torvalds return -EPERM; 1778efa90a98SHugh Dickins 1779efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 1780efa90a98SHugh Dickins if (!p) 1781efa90a98SHugh Dickins return -ENOMEM; 1782efa90a98SHugh Dickins 17835d337b91SHugh Dickins spin_lock(&swap_lock); 1784efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1785efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 17861da177e4SLinus Torvalds break; 1787efa90a98SHugh Dickins } 17881da177e4SLinus Torvalds error = -EPERM; 17890697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 17905d337b91SHugh Dickins spin_unlock(&swap_lock); 1791efa90a98SHugh Dickins kfree(p); 17921da177e4SLinus Torvalds goto out; 17931da177e4SLinus Torvalds } 1794efa90a98SHugh Dickins if (type >= nr_swapfiles) { 1795efa90a98SHugh Dickins p->type = type; 1796efa90a98SHugh Dickins swap_info[type] = p; 1797efa90a98SHugh Dickins /* 1798efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 1799efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 1800efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 1801efa90a98SHugh Dickins */ 1802efa90a98SHugh Dickins smp_wmb(); 1803efa90a98SHugh Dickins nr_swapfiles++; 1804efa90a98SHugh Dickins } else { 1805efa90a98SHugh Dickins kfree(p); 1806efa90a98SHugh Dickins p = swap_info[type]; 1807efa90a98SHugh Dickins /* 1808efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 1809efa90a98SHugh Dickins * would be relying on p->type to remain valid. 1810efa90a98SHugh Dickins */ 1811efa90a98SHugh Dickins } 18129625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 18131da177e4SLinus Torvalds p->flags = SWP_USED; 18141da177e4SLinus Torvalds p->next = -1; 18155d337b91SHugh Dickins spin_unlock(&swap_lock); 1816efa90a98SHugh Dickins 18171da177e4SLinus Torvalds name = getname(specialfile); 18181da177e4SLinus Torvalds error = PTR_ERR(name); 18191da177e4SLinus Torvalds if (IS_ERR(name)) { 18201da177e4SLinus Torvalds name = NULL; 18211da177e4SLinus Torvalds goto bad_swap_2; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 18241da177e4SLinus Torvalds error = PTR_ERR(swap_file); 18251da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 18261da177e4SLinus Torvalds swap_file = NULL; 18271da177e4SLinus Torvalds goto bad_swap_2; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds p->swap_file = swap_file; 18311da177e4SLinus Torvalds mapping = swap_file->f_mapping; 18321da177e4SLinus Torvalds inode = mapping->host; 18331da177e4SLinus Torvalds 18341da177e4SLinus Torvalds error = -EBUSY; 18351da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 1836efa90a98SHugh Dickins struct swap_info_struct *q = swap_info[i]; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds if (i == type || !q->swap_file) 18391da177e4SLinus Torvalds continue; 18401da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 18411da177e4SLinus Torvalds goto bad_swap; 18421da177e4SLinus Torvalds } 18431da177e4SLinus Torvalds 18441da177e4SLinus Torvalds error = -EINVAL; 18451da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 18461da177e4SLinus Torvalds bdev = I_BDEV(inode); 18471da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 18481da177e4SLinus Torvalds if (error < 0) { 18491da177e4SLinus Torvalds bdev = NULL; 1850f7b3a435SRob Landley error = -EINVAL; 18511da177e4SLinus Torvalds goto bad_swap; 18521da177e4SLinus Torvalds } 18531da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 18541da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 18551da177e4SLinus Torvalds if (error < 0) 18561da177e4SLinus Torvalds goto bad_swap; 18571da177e4SLinus Torvalds p->bdev = bdev; 18581da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 18591da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 18601b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 18611da177e4SLinus Torvalds did_down = 1; 18621da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 18631da177e4SLinus Torvalds error = -EBUSY; 18641da177e4SLinus Torvalds goto bad_swap; 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds } else { 18671da177e4SLinus Torvalds goto bad_swap; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 187073fd8748SHugh Dickins swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds /* 18731da177e4SLinus Torvalds * Read the swap header. 18741da177e4SLinus Torvalds */ 18751da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 18761da177e4SLinus Torvalds error = -EINVAL; 18771da177e4SLinus Torvalds goto bad_swap; 18781da177e4SLinus Torvalds } 1879090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 18801da177e4SLinus Torvalds if (IS_ERR(page)) { 18811da177e4SLinus Torvalds error = PTR_ERR(page); 18821da177e4SLinus Torvalds goto bad_swap; 18831da177e4SLinus Torvalds } 188481e33971SHugh Dickins swap_header = kmap(page); 18851da177e4SLinus Torvalds 188681e33971SHugh Dickins if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 1887e97a3111SJesper Juhl printk(KERN_ERR "Unable to find swap-space signature\n"); 18881da177e4SLinus Torvalds error = -EINVAL; 18891da177e4SLinus Torvalds goto bad_swap; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 1892797df574SChris Dearman /* swap partition endianess hack... */ 1893797df574SChris Dearman if (swab32(swap_header->info.version) == 1) { 1894797df574SChris Dearman swab32s(&swap_header->info.version); 1895797df574SChris Dearman swab32s(&swap_header->info.last_page); 1896797df574SChris Dearman swab32s(&swap_header->info.nr_badpages); 1897797df574SChris Dearman for (i = 0; i < swap_header->info.nr_badpages; i++) 1898797df574SChris Dearman swab32s(&swap_header->info.badpages[i]); 1899797df574SChris Dearman } 190081e33971SHugh Dickins /* Check the swap header's sub-version */ 19011da177e4SLinus Torvalds if (swap_header->info.version != 1) { 19021da177e4SLinus Torvalds printk(KERN_WARNING 19031da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 19041da177e4SLinus Torvalds swap_header->info.version); 19051da177e4SLinus Torvalds error = -EINVAL; 19061da177e4SLinus Torvalds goto bad_swap; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds p->lowest_bit = 1; 191052b7efdbSHugh Dickins p->cluster_next = 1; 1911efa90a98SHugh Dickins p->cluster_nr = 0; 191252b7efdbSHugh Dickins 19131da177e4SLinus Torvalds /* 19141da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 19151da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 19161da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 19171da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 19181da177e4SLinus Torvalds * the different architectures. In order to find the 19191da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 19201da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 19211da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 19221da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 19231da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 19241da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 19251da177e4SLinus Torvalds * swap pte. 19261da177e4SLinus Torvalds */ 192781e33971SHugh Dickins maxpages = swp_offset(pte_to_swp_entry( 192881e33971SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1; 19291da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 19301da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 19311da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 19321da177e4SLinus Torvalds 19331da177e4SLinus Torvalds error = -EINVAL; 1934e2244ec2SHugh Dickins if (!maxpages) 1935e2244ec2SHugh Dickins goto bad_swap; 193673fd8748SHugh Dickins if (swapfilepages && maxpages > swapfilepages) { 19375d1854e1SEric Sandeen printk(KERN_WARNING 19385d1854e1SEric Sandeen "Swap area shorter than signature indicates\n"); 19395d1854e1SEric Sandeen goto bad_swap; 19405d1854e1SEric Sandeen } 1941e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1942e2244ec2SHugh Dickins goto bad_swap; 19431da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 19441da177e4SLinus Torvalds goto bad_swap; 19451da177e4SLinus Torvalds 19461da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 1947*8d69aaeeSHugh Dickins swap_map = vmalloc(maxpages); 194878ecba08SHugh Dickins if (!swap_map) { 19491da177e4SLinus Torvalds error = -ENOMEM; 19501da177e4SLinus Torvalds goto bad_swap; 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds 1953*8d69aaeeSHugh Dickins memset(swap_map, 0, maxpages); 19541da177e4SLinus Torvalds for (i = 0; i < swap_header->info.nr_badpages; i++) { 1955cd105df4STobias Klauser int page_nr = swap_header->info.badpages[i]; 195681e33971SHugh Dickins if (page_nr <= 0 || page_nr >= swap_header->info.last_page) { 19571da177e4SLinus Torvalds error = -EINVAL; 195881e33971SHugh Dickins goto bad_swap; 195981e33971SHugh Dickins } 196078ecba08SHugh Dickins swap_map[page_nr] = SWAP_MAP_BAD; 19611da177e4SLinus Torvalds } 196227a7faa0SKAMEZAWA Hiroyuki 196327a7faa0SKAMEZAWA Hiroyuki error = swap_cgroup_swapon(type, maxpages); 196427a7faa0SKAMEZAWA Hiroyuki if (error) 196527a7faa0SKAMEZAWA Hiroyuki goto bad_swap; 196627a7faa0SKAMEZAWA Hiroyuki 19671da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 19681da177e4SLinus Torvalds swap_header->info.nr_badpages - 19691da177e4SLinus Torvalds 1 /* header page */; 19701da177e4SLinus Torvalds 1971e2244ec2SHugh Dickins if (nr_good_pages) { 197278ecba08SHugh Dickins swap_map[0] = SWAP_MAP_BAD; 1973e2244ec2SHugh Dickins p->max = maxpages; 1974e2244ec2SHugh Dickins p->pages = nr_good_pages; 197553092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 197653092a74SHugh Dickins if (nr_extents < 0) { 197753092a74SHugh Dickins error = nr_extents; 1978e2244ec2SHugh Dickins goto bad_swap; 197953092a74SHugh Dickins } 1980e2244ec2SHugh Dickins nr_good_pages = p->pages; 1981e2244ec2SHugh Dickins } 19821da177e4SLinus Torvalds if (!nr_good_pages) { 19831da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 19841da177e4SLinus Torvalds error = -EINVAL; 19851da177e4SLinus Torvalds goto bad_swap; 19861da177e4SLinus Torvalds } 19871da177e4SLinus Torvalds 19883bd0f0c7SSuresh Jayaraman if (p->bdev) { 198920137a49SHugh Dickins if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { 199020137a49SHugh Dickins p->flags |= SWP_SOLIDSTATE; 199120137a49SHugh Dickins p->cluster_next = 1 + (random32() % p->highest_bit); 199220137a49SHugh Dickins } 19936a6ba831SHugh Dickins if (discard_swap(p) == 0) 19946a6ba831SHugh Dickins p->flags |= SWP_DISCARDABLE; 19953bd0f0c7SSuresh Jayaraman } 19966a6ba831SHugh Dickins 1997fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 19985d337b91SHugh Dickins spin_lock(&swap_lock); 199978ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 200078ecba08SHugh Dickins p->prio = 200178ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 200278ecba08SHugh Dickins else 200378ecba08SHugh Dickins p->prio = --least_priority; 200478ecba08SHugh Dickins p->swap_map = swap_map; 200522c6f8fdSHugh Dickins p->flags |= SWP_WRITEOK; 20061da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 20071da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 200853092a74SHugh Dickins 20096eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 201020137a49SHugh Dickins "Priority:%d extents:%d across:%lluk %s%s\n", 201153092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 20126a6ba831SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 201320137a49SHugh Dickins (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 20146a6ba831SHugh Dickins (p->flags & SWP_DISCARDABLE) ? "D" : ""); 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds /* insert swap space into swap_list: */ 20171da177e4SLinus Torvalds prev = -1; 2018efa90a98SHugh Dickins for (i = swap_list.head; i >= 0; i = swap_info[i]->next) { 2019efa90a98SHugh Dickins if (p->prio >= swap_info[i]->prio) 20201da177e4SLinus Torvalds break; 20211da177e4SLinus Torvalds prev = i; 20221da177e4SLinus Torvalds } 20231da177e4SLinus Torvalds p->next = i; 2024efa90a98SHugh Dickins if (prev < 0) 2025efa90a98SHugh Dickins swap_list.head = swap_list.next = type; 2026efa90a98SHugh Dickins else 2027efa90a98SHugh Dickins swap_info[prev]->next = type; 20285d337b91SHugh Dickins spin_unlock(&swap_lock); 2029fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 20301da177e4SLinus Torvalds error = 0; 20311da177e4SLinus Torvalds goto out; 20321da177e4SLinus Torvalds bad_swap: 20331da177e4SLinus Torvalds if (bdev) { 20341da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 20351da177e4SLinus Torvalds bd_release(bdev); 20361da177e4SLinus Torvalds } 20374cd3bb10SHugh Dickins destroy_swap_extents(p); 203827a7faa0SKAMEZAWA Hiroyuki swap_cgroup_swapoff(type); 20391da177e4SLinus Torvalds bad_swap_2: 20405d337b91SHugh Dickins spin_lock(&swap_lock); 20411da177e4SLinus Torvalds p->swap_file = NULL; 20421da177e4SLinus Torvalds p->flags = 0; 20435d337b91SHugh Dickins spin_unlock(&swap_lock); 20441da177e4SLinus Torvalds vfree(swap_map); 20451da177e4SLinus Torvalds if (swap_file) 20461da177e4SLinus Torvalds filp_close(swap_file, NULL); 20471da177e4SLinus Torvalds out: 20481da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 20491da177e4SLinus Torvalds kunmap(page); 20501da177e4SLinus Torvalds page_cache_release(page); 20511da177e4SLinus Torvalds } 20521da177e4SLinus Torvalds if (name) 20531da177e4SLinus Torvalds putname(name); 20541da177e4SLinus Torvalds if (did_down) { 20551da177e4SLinus Torvalds if (!error) 20561da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 20571b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 20581da177e4SLinus Torvalds } 20591da177e4SLinus Torvalds return error; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 20631da177e4SLinus Torvalds { 2064efa90a98SHugh Dickins unsigned int type; 20651da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 20661da177e4SLinus Torvalds 20675d337b91SHugh Dickins spin_lock(&swap_lock); 2068efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2069efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 2070efa90a98SHugh Dickins 2071efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 2072efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 20751da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 20765d337b91SHugh Dickins spin_unlock(&swap_lock); 20771da177e4SLinus Torvalds } 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds /* 20801da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 20811da177e4SLinus Torvalds * 20821da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 20831da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 2084355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 2085355cfa73SKAMEZAWA Hiroyuki * - success -> 0 2086355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 2087355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 2088355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 2089355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 20901da177e4SLinus Torvalds */ 2091*8d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 20921da177e4SLinus Torvalds { 20931da177e4SLinus Torvalds struct swap_info_struct *p; 20941da177e4SLinus Torvalds unsigned long offset, type; 2095*8d69aaeeSHugh Dickins unsigned char count; 2096*8d69aaeeSHugh Dickins unsigned char has_cache; 2097253d553bSHugh Dickins int err = -EINVAL; 20981da177e4SLinus Torvalds 2099a7420aa5SAndi Kleen if (non_swap_entry(entry)) 2100253d553bSHugh Dickins goto out; 21010697212aSChristoph Lameter 21021da177e4SLinus Torvalds type = swp_type(entry); 21031da177e4SLinus Torvalds if (type >= nr_swapfiles) 21041da177e4SLinus Torvalds goto bad_file; 2105efa90a98SHugh Dickins p = swap_info[type]; 21061da177e4SLinus Torvalds offset = swp_offset(entry); 21071da177e4SLinus Torvalds 21085d337b91SHugh Dickins spin_lock(&swap_lock); 2109355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 2110355cfa73SKAMEZAWA Hiroyuki goto unlock_out; 2111355cfa73SKAMEZAWA Hiroyuki 2112253d553bSHugh Dickins count = p->swap_map[offset]; 2113253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 2114253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 2115253d553bSHugh Dickins err = 0; 2116355cfa73SKAMEZAWA Hiroyuki 2117253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 2118355cfa73SKAMEZAWA Hiroyuki 2119355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 2120253d553bSHugh Dickins if (!has_cache && count) 2121253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 2122253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 2123253d553bSHugh Dickins err = -EEXIST; 2124253d553bSHugh Dickins else /* no users remaining */ 2125253d553bSHugh Dickins err = -ENOENT; 2126355cfa73SKAMEZAWA Hiroyuki 2127355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 2128253d553bSHugh Dickins 2129253d553bSHugh Dickins if (count < SWAP_MAP_MAX - 1) 2130253d553bSHugh Dickins count++; 2131253d553bSHugh Dickins else if (count <= SWAP_MAP_MAX) { 21321da177e4SLinus Torvalds if (swap_overflow++ < 5) 2133355cfa73SKAMEZAWA Hiroyuki printk(KERN_WARNING 2134355cfa73SKAMEZAWA Hiroyuki "swap_dup: swap entry overflow\n"); 2135253d553bSHugh Dickins count = SWAP_MAP_MAX; 2136355cfa73SKAMEZAWA Hiroyuki } else 2137253d553bSHugh Dickins err = -EINVAL; 2138253d553bSHugh Dickins } else 2139253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 2140253d553bSHugh Dickins 2141253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 2142253d553bSHugh Dickins 2143355cfa73SKAMEZAWA Hiroyuki unlock_out: 21445d337b91SHugh Dickins spin_unlock(&swap_lock); 21451da177e4SLinus Torvalds out: 2146253d553bSHugh Dickins return err; 21471da177e4SLinus Torvalds 21481da177e4SLinus Torvalds bad_file: 21491da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 21501da177e4SLinus Torvalds goto out; 21511da177e4SLinus Torvalds } 2152253d553bSHugh Dickins 2153355cfa73SKAMEZAWA Hiroyuki /* 2154355cfa73SKAMEZAWA Hiroyuki * increase reference count of swap entry by 1. 2155355cfa73SKAMEZAWA Hiroyuki */ 2156355cfa73SKAMEZAWA Hiroyuki void swap_duplicate(swp_entry_t entry) 2157355cfa73SKAMEZAWA Hiroyuki { 2158253d553bSHugh Dickins __swap_duplicate(entry, 1); 2159355cfa73SKAMEZAWA Hiroyuki } 21601da177e4SLinus Torvalds 2161cb4b86baSKAMEZAWA Hiroyuki /* 2162355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 2163355cfa73SKAMEZAWA Hiroyuki * 216473c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 2165355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 2166355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 2167355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 2168cb4b86baSKAMEZAWA Hiroyuki */ 2169cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 2170cb4b86baSKAMEZAWA Hiroyuki { 2171253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 2172cb4b86baSKAMEZAWA Hiroyuki } 2173cb4b86baSKAMEZAWA Hiroyuki 21741da177e4SLinus Torvalds /* 21755d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 21761da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 21771da177e4SLinus Torvalds */ 21781da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 21791da177e4SLinus Torvalds { 21808952898bSHugh Dickins struct swap_info_struct *si; 21813f9e7949SHugh Dickins int our_page_cluster = page_cluster; 21828952898bSHugh Dickins pgoff_t target, toff; 21838952898bSHugh Dickins pgoff_t base, end; 21848952898bSHugh Dickins int nr_pages = 0; 21851da177e4SLinus Torvalds 21863f9e7949SHugh Dickins if (!our_page_cluster) /* no readahead */ 21871da177e4SLinus Torvalds return 0; 21888952898bSHugh Dickins 2189efa90a98SHugh Dickins si = swap_info[swp_type(entry)]; 21908952898bSHugh Dickins target = swp_offset(entry); 21918952898bSHugh Dickins base = (target >> our_page_cluster) << our_page_cluster; 21928952898bSHugh Dickins end = base + (1 << our_page_cluster); 21938952898bSHugh Dickins if (!base) /* first page is swap header */ 21948952898bSHugh Dickins base++; 21951da177e4SLinus Torvalds 21965d337b91SHugh Dickins spin_lock(&swap_lock); 21978952898bSHugh Dickins if (end > si->max) /* don't go beyond end of map */ 21988952898bSHugh Dickins end = si->max; 21998952898bSHugh Dickins 22008952898bSHugh Dickins /* Count contiguous allocated slots above our target */ 22018952898bSHugh Dickins for (toff = target; ++toff < end; nr_pages++) { 22021da177e4SLinus Torvalds /* Don't read in free or bad pages */ 22038952898bSHugh Dickins if (!si->swap_map[toff]) 22041da177e4SLinus Torvalds break; 2205355cfa73SKAMEZAWA Hiroyuki if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD) 22061da177e4SLinus Torvalds break; 22078952898bSHugh Dickins } 22088952898bSHugh Dickins /* Count contiguous allocated slots below our target */ 22098952898bSHugh Dickins for (toff = target; --toff >= base; nr_pages++) { 22108952898bSHugh Dickins /* Don't read in free or bad pages */ 22118952898bSHugh Dickins if (!si->swap_map[toff]) 22128952898bSHugh Dickins break; 2213355cfa73SKAMEZAWA Hiroyuki if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD) 22148952898bSHugh Dickins break; 22158952898bSHugh Dickins } 22165d337b91SHugh Dickins spin_unlock(&swap_lock); 22178952898bSHugh Dickins 22188952898bSHugh Dickins /* 22198952898bSHugh Dickins * Indicate starting offset, and return number of pages to get: 22208952898bSHugh Dickins * if only 1, say 0, since there's then no readahead to be done. 22218952898bSHugh Dickins */ 22228952898bSHugh Dickins *offset = ++toff; 22238952898bSHugh Dickins return nr_pages? ++nr_pages: 0; 22241da177e4SLinus Torvalds } 2225