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> 361da177e4SLinus Torvalds 377c363b8cSAdrian Bunk static DEFINE_SPINLOCK(swap_lock); 387c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 39b962716bSHugh Dickins long nr_swap_pages; 401da177e4SLinus Torvalds long total_swap_pages; 411da177e4SLinus Torvalds static int swap_overflow; 4278ecba08SHugh Dickins static int least_priority; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 451da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 461da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 471da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 481da177e4SLinus Torvalds 497c363b8cSAdrian Bunk static struct swap_list_t swap_list = {-1, -1}; 501da177e4SLinus Torvalds 51f577eb30SRafael J. Wysocki static struct swap_info_struct swap_info[MAX_SWAPFILES]; 521da177e4SLinus Torvalds 53fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds * We need this because the bdev->unplug_fn can sleep and we cannot 575d337b91SHugh Dickins * hold swap_lock while calling the unplug_fn. And swap_lock 58fc0abb14SIngo Molnar * cannot be turned into a mutex. 591da177e4SLinus Torvalds */ 601da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem); 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds swp_entry_t entry; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds down_read(&swap_unplug_sem); 674c21e2f2SHugh Dickins entry.val = page_private(page); 681da177e4SLinus Torvalds if (PageSwapCache(page)) { 691da177e4SLinus Torvalds struct block_device *bdev = swap_info[swp_type(entry)].bdev; 701da177e4SLinus Torvalds struct backing_dev_info *bdi; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds /* 731da177e4SLinus Torvalds * If the page is removed from swapcache from under us (with a 741da177e4SLinus Torvalds * racy try_to_unuse/swapoff) we need an additional reference 754c21e2f2SHugh Dickins * count to avoid reading garbage from page_private(page) above. 764c21e2f2SHugh Dickins * If the WARN_ON triggers during a swapoff it maybe the race 771da177e4SLinus Torvalds * condition and it's harmless. However if it triggers without 781da177e4SLinus Torvalds * swapoff it signals a problem. 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds WARN_ON(page_count(page) <= 1); 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds bdi = bdev->bd_inode->i_mapping->backing_dev_info; 83ba32311eSMcMullan, Jason blk_run_backing_dev(bdi, page); 841da177e4SLinus Torvalds } 851da177e4SLinus Torvalds up_read(&swap_unplug_sem); 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 886a6ba831SHugh Dickins /* 896a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 906a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 916a6ba831SHugh Dickins */ 926a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 936a6ba831SHugh Dickins { 946a6ba831SHugh Dickins struct swap_extent *se; 956a6ba831SHugh Dickins int err = 0; 966a6ba831SHugh Dickins 976a6ba831SHugh Dickins list_for_each_entry(se, &si->extent_list, list) { 986a6ba831SHugh Dickins sector_t start_block = se->start_block << (PAGE_SHIFT - 9); 99858a2990SHugh Dickins sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1006a6ba831SHugh Dickins 1016a6ba831SHugh Dickins if (se->start_page == 0) { 1026a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1036a6ba831SHugh Dickins start_block += 1 << (PAGE_SHIFT - 9); 1046a6ba831SHugh Dickins nr_blocks -= 1 << (PAGE_SHIFT - 9); 1056a6ba831SHugh Dickins if (!nr_blocks) 1066a6ba831SHugh Dickins continue; 1076a6ba831SHugh Dickins } 1086a6ba831SHugh Dickins 1096a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 1106a6ba831SHugh Dickins nr_blocks, GFP_KERNEL); 1116a6ba831SHugh Dickins if (err) 1126a6ba831SHugh Dickins break; 1136a6ba831SHugh Dickins 1146a6ba831SHugh Dickins cond_resched(); 1156a6ba831SHugh Dickins } 1166a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1176a6ba831SHugh Dickins } 1186a6ba831SHugh Dickins 1197992fde7SHugh Dickins /* 1207992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1217992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1227992fde7SHugh Dickins */ 1237992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1247992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1257992fde7SHugh Dickins { 1267992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1277992fde7SHugh Dickins int found_extent = 0; 1287992fde7SHugh Dickins 1297992fde7SHugh Dickins while (nr_pages) { 1307992fde7SHugh Dickins struct list_head *lh; 1317992fde7SHugh Dickins 1327992fde7SHugh Dickins if (se->start_page <= start_page && 1337992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1347992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1357992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 136858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1377992fde7SHugh Dickins 1387992fde7SHugh Dickins if (nr_blocks > nr_pages) 1397992fde7SHugh Dickins nr_blocks = nr_pages; 1407992fde7SHugh Dickins start_page += nr_blocks; 1417992fde7SHugh Dickins nr_pages -= nr_blocks; 1427992fde7SHugh Dickins 1437992fde7SHugh Dickins if (!found_extent++) 1447992fde7SHugh Dickins si->curr_swap_extent = se; 1457992fde7SHugh Dickins 1467992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1477992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1487992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 1497992fde7SHugh Dickins nr_blocks, GFP_NOIO)) 1507992fde7SHugh Dickins break; 1517992fde7SHugh Dickins } 1527992fde7SHugh Dickins 1537992fde7SHugh Dickins lh = se->list.next; 1547992fde7SHugh Dickins if (lh == &si->extent_list) 1557992fde7SHugh Dickins lh = lh->next; 1567992fde7SHugh Dickins se = list_entry(lh, struct swap_extent, list); 1577992fde7SHugh Dickins } 1587992fde7SHugh Dickins } 1597992fde7SHugh Dickins 1607992fde7SHugh Dickins static int wait_for_discard(void *word) 1617992fde7SHugh Dickins { 1627992fde7SHugh Dickins schedule(); 1637992fde7SHugh Dickins return 0; 1647992fde7SHugh Dickins } 1657992fde7SHugh Dickins 166048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 167048c27fdSHugh Dickins #define LATENCY_LIMIT 256 168048c27fdSHugh Dickins 1696eb396dcSHugh Dickins static inline unsigned long scan_swap_map(struct swap_info_struct *si) 1701da177e4SLinus Torvalds { 171ebebbbe9SHugh Dickins unsigned long offset; 172c60aa176SHugh Dickins unsigned long scan_base; 1737992fde7SHugh Dickins unsigned long last_in_cluster = 0; 174048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 1757992fde7SHugh Dickins int found_free_cluster = 0; 1761da177e4SLinus Torvalds 1777dfad418SHugh Dickins /* 1787dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 1797dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 1807dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 1817dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 1827dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 1837dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 1847dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 185c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 1861da177e4SLinus Torvalds */ 1877dfad418SHugh Dickins 18852b7efdbSHugh Dickins si->flags += SWP_SCANNING; 189c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 190ebebbbe9SHugh Dickins 191ebebbbe9SHugh Dickins if (unlikely(!si->cluster_nr--)) { 192ebebbbe9SHugh Dickins if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 1937dfad418SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 194ebebbbe9SHugh Dickins goto checks; 195ebebbbe9SHugh Dickins } 1967992fde7SHugh Dickins if (si->flags & SWP_DISCARDABLE) { 1977992fde7SHugh Dickins /* 1987992fde7SHugh Dickins * Start range check on racing allocations, in case 1997992fde7SHugh Dickins * they overlap the cluster we eventually decide on 2007992fde7SHugh Dickins * (we scan without swap_lock to allow preemption). 2017992fde7SHugh Dickins * It's hardly conceivable that cluster_nr could be 2027992fde7SHugh Dickins * wrapped during our scan, but don't depend on it. 2037992fde7SHugh Dickins */ 2047992fde7SHugh Dickins if (si->lowest_alloc) 2057992fde7SHugh Dickins goto checks; 2067992fde7SHugh Dickins si->lowest_alloc = si->max; 2077992fde7SHugh Dickins si->highest_alloc = 0; 2087992fde7SHugh Dickins } 2095d337b91SHugh Dickins spin_unlock(&swap_lock); 2107dfad418SHugh Dickins 211c60aa176SHugh Dickins /* 212c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 213c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 214c60aa176SHugh Dickins * But if seek is cheap, search from our current position, so 215c60aa176SHugh Dickins * that swap is allocated from all over the partition: if the 216c60aa176SHugh Dickins * Flash Translation Layer only remaps within limited zones, 217c60aa176SHugh Dickins * we don't want to wear out the first zone too quickly. 218c60aa176SHugh Dickins */ 219c60aa176SHugh Dickins if (!(si->flags & SWP_SOLIDSTATE)) 220c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 2217dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 2227dfad418SHugh Dickins 2237dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 2247dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 2251da177e4SLinus Torvalds if (si->swap_map[offset]) 2267dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 2277dfad418SHugh Dickins else if (offset == last_in_cluster) { 2285d337b91SHugh Dickins spin_lock(&swap_lock); 229ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 230ebebbbe9SHugh Dickins si->cluster_next = offset; 231ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 2327992fde7SHugh Dickins found_free_cluster = 1; 233ebebbbe9SHugh Dickins goto checks; 2347dfad418SHugh Dickins } 235048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 236048c27fdSHugh Dickins cond_resched(); 237048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 238048c27fdSHugh Dickins } 2397dfad418SHugh Dickins } 240ebebbbe9SHugh Dickins 241ebebbbe9SHugh Dickins offset = si->lowest_bit; 242c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 243c60aa176SHugh Dickins 244c60aa176SHugh Dickins /* Locate the first empty (unaligned) cluster */ 245c60aa176SHugh Dickins for (; last_in_cluster < scan_base; offset++) { 246c60aa176SHugh Dickins if (si->swap_map[offset]) 247c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 248c60aa176SHugh Dickins else if (offset == last_in_cluster) { 249c60aa176SHugh Dickins spin_lock(&swap_lock); 250c60aa176SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 251c60aa176SHugh Dickins si->cluster_next = offset; 252c60aa176SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 253c60aa176SHugh Dickins found_free_cluster = 1; 254c60aa176SHugh Dickins goto checks; 255c60aa176SHugh Dickins } 256c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 257c60aa176SHugh Dickins cond_resched(); 258c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 259c60aa176SHugh Dickins } 260c60aa176SHugh Dickins } 261c60aa176SHugh Dickins 262c60aa176SHugh Dickins offset = scan_base; 2635d337b91SHugh Dickins spin_lock(&swap_lock); 264ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 2657992fde7SHugh Dickins si->lowest_alloc = 0; 2667dfad418SHugh Dickins } 2677dfad418SHugh Dickins 268ebebbbe9SHugh Dickins checks: 269ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 27052b7efdbSHugh Dickins goto no_page; 2717dfad418SHugh Dickins if (!si->highest_bit) 2727dfad418SHugh Dickins goto no_page; 273ebebbbe9SHugh Dickins if (offset > si->highest_bit) 274c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 275ebebbbe9SHugh Dickins if (si->swap_map[offset]) 276ebebbbe9SHugh Dickins goto scan; 277ebebbbe9SHugh Dickins 27852b7efdbSHugh Dickins if (offset == si->lowest_bit) 2791da177e4SLinus Torvalds si->lowest_bit++; 2801da177e4SLinus Torvalds if (offset == si->highest_bit) 2811da177e4SLinus Torvalds si->highest_bit--; 2827dfad418SHugh Dickins si->inuse_pages++; 2837dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 2841da177e4SLinus Torvalds si->lowest_bit = si->max; 2851da177e4SLinus Torvalds si->highest_bit = 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds si->swap_map[offset] = 1; 2881da177e4SLinus Torvalds si->cluster_next = offset + 1; 28952b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 2907992fde7SHugh Dickins 2917992fde7SHugh Dickins if (si->lowest_alloc) { 2927992fde7SHugh Dickins /* 2937992fde7SHugh Dickins * Only set when SWP_DISCARDABLE, and there's a scan 2947992fde7SHugh Dickins * for a free cluster in progress or just completed. 2957992fde7SHugh Dickins */ 2967992fde7SHugh Dickins if (found_free_cluster) { 2977992fde7SHugh Dickins /* 2987992fde7SHugh Dickins * To optimize wear-levelling, discard the 2997992fde7SHugh Dickins * old data of the cluster, taking care not to 3007992fde7SHugh Dickins * discard any of its pages that have already 3017992fde7SHugh Dickins * been allocated by racing tasks (offset has 3027992fde7SHugh Dickins * already stepped over any at the beginning). 3037992fde7SHugh Dickins */ 3047992fde7SHugh Dickins if (offset < si->highest_alloc && 3057992fde7SHugh Dickins si->lowest_alloc <= last_in_cluster) 3067992fde7SHugh Dickins last_in_cluster = si->lowest_alloc - 1; 3077992fde7SHugh Dickins si->flags |= SWP_DISCARDING; 3087992fde7SHugh Dickins spin_unlock(&swap_lock); 3097992fde7SHugh Dickins 3107992fde7SHugh Dickins if (offset < last_in_cluster) 3117992fde7SHugh Dickins discard_swap_cluster(si, offset, 3127992fde7SHugh Dickins last_in_cluster - offset + 1); 3137992fde7SHugh Dickins 3147992fde7SHugh Dickins spin_lock(&swap_lock); 3157992fde7SHugh Dickins si->lowest_alloc = 0; 3167992fde7SHugh Dickins si->flags &= ~SWP_DISCARDING; 3177992fde7SHugh Dickins 3187992fde7SHugh Dickins smp_mb(); /* wake_up_bit advises this */ 3197992fde7SHugh Dickins wake_up_bit(&si->flags, ilog2(SWP_DISCARDING)); 3207992fde7SHugh Dickins 3217992fde7SHugh Dickins } else if (si->flags & SWP_DISCARDING) { 3227992fde7SHugh Dickins /* 3237992fde7SHugh Dickins * Delay using pages allocated by racing tasks 3247992fde7SHugh Dickins * until the whole discard has been issued. We 3257992fde7SHugh Dickins * could defer that delay until swap_writepage, 3267992fde7SHugh Dickins * but it's easier to keep this self-contained. 3277992fde7SHugh Dickins */ 3287992fde7SHugh Dickins spin_unlock(&swap_lock); 3297992fde7SHugh Dickins wait_on_bit(&si->flags, ilog2(SWP_DISCARDING), 3307992fde7SHugh Dickins wait_for_discard, TASK_UNINTERRUPTIBLE); 3317992fde7SHugh Dickins spin_lock(&swap_lock); 3327992fde7SHugh Dickins } else { 3337992fde7SHugh Dickins /* 3347992fde7SHugh Dickins * Note pages allocated by racing tasks while 3357992fde7SHugh Dickins * scan for a free cluster is in progress, so 3367992fde7SHugh Dickins * that its final discard can exclude them. 3377992fde7SHugh Dickins */ 3387992fde7SHugh Dickins if (offset < si->lowest_alloc) 3397992fde7SHugh Dickins si->lowest_alloc = offset; 3407992fde7SHugh Dickins if (offset > si->highest_alloc) 3417992fde7SHugh Dickins si->highest_alloc = offset; 3427992fde7SHugh Dickins } 3437992fde7SHugh Dickins } 3441da177e4SLinus Torvalds return offset; 3457dfad418SHugh Dickins 346ebebbbe9SHugh Dickins scan: 3475d337b91SHugh Dickins spin_unlock(&swap_lock); 3487dfad418SHugh Dickins while (++offset <= si->highest_bit) { 34952b7efdbSHugh Dickins if (!si->swap_map[offset]) { 3505d337b91SHugh Dickins spin_lock(&swap_lock); 35152b7efdbSHugh Dickins goto checks; 3527dfad418SHugh Dickins } 353048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 354048c27fdSHugh Dickins cond_resched(); 355048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 356048c27fdSHugh Dickins } 35752b7efdbSHugh Dickins } 358c60aa176SHugh Dickins offset = si->lowest_bit; 359c60aa176SHugh Dickins while (++offset < scan_base) { 360c60aa176SHugh Dickins if (!si->swap_map[offset]) { 3615d337b91SHugh Dickins spin_lock(&swap_lock); 362ebebbbe9SHugh Dickins goto checks; 363c60aa176SHugh Dickins } 364c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 365c60aa176SHugh Dickins cond_resched(); 366c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 367c60aa176SHugh Dickins } 368c60aa176SHugh Dickins } 369c60aa176SHugh Dickins spin_lock(&swap_lock); 3707dfad418SHugh Dickins 3717dfad418SHugh Dickins no_page: 37252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 3731da177e4SLinus Torvalds return 0; 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 3771da177e4SLinus Torvalds { 378fb4f88dcSHugh Dickins struct swap_info_struct *si; 379fb4f88dcSHugh Dickins pgoff_t offset; 380fb4f88dcSHugh Dickins int type, next; 381fb4f88dcSHugh Dickins int wrapped = 0; 3821da177e4SLinus Torvalds 3835d337b91SHugh Dickins spin_lock(&swap_lock); 3841da177e4SLinus Torvalds if (nr_swap_pages <= 0) 385fb4f88dcSHugh Dickins goto noswap; 386fb4f88dcSHugh Dickins nr_swap_pages--; 3871da177e4SLinus Torvalds 388fb4f88dcSHugh Dickins for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { 389fb4f88dcSHugh Dickins si = swap_info + type; 390fb4f88dcSHugh Dickins next = si->next; 391fb4f88dcSHugh Dickins if (next < 0 || 392fb4f88dcSHugh Dickins (!wrapped && si->prio != swap_info[next].prio)) { 393fb4f88dcSHugh Dickins next = swap_list.head; 394fb4f88dcSHugh Dickins wrapped++; 3951da177e4SLinus Torvalds } 396fb4f88dcSHugh Dickins 397fb4f88dcSHugh Dickins if (!si->highest_bit) 398fb4f88dcSHugh Dickins continue; 399fb4f88dcSHugh Dickins if (!(si->flags & SWP_WRITEOK)) 400fb4f88dcSHugh Dickins continue; 401fb4f88dcSHugh Dickins 402fb4f88dcSHugh Dickins swap_list.next = next; 403fb4f88dcSHugh Dickins offset = scan_swap_map(si); 4045d337b91SHugh Dickins if (offset) { 4055d337b91SHugh Dickins spin_unlock(&swap_lock); 406fb4f88dcSHugh Dickins return swp_entry(type, offset); 4075d337b91SHugh Dickins } 408fb4f88dcSHugh Dickins next = swap_list.next; 409fb4f88dcSHugh Dickins } 410fb4f88dcSHugh Dickins 411fb4f88dcSHugh Dickins nr_swap_pages++; 412fb4f88dcSHugh Dickins noswap: 4135d337b91SHugh Dickins spin_unlock(&swap_lock); 414fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4173a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type) 4183a291a20SRafael J. Wysocki { 4193a291a20SRafael J. Wysocki struct swap_info_struct *si; 4203a291a20SRafael J. Wysocki pgoff_t offset; 4213a291a20SRafael J. Wysocki 4223a291a20SRafael J. Wysocki spin_lock(&swap_lock); 4233a291a20SRafael J. Wysocki si = swap_info + type; 4243a291a20SRafael J. Wysocki if (si->flags & SWP_WRITEOK) { 4253a291a20SRafael J. Wysocki nr_swap_pages--; 4263a291a20SRafael J. Wysocki offset = scan_swap_map(si); 4273a291a20SRafael J. Wysocki if (offset) { 4283a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 4293a291a20SRafael J. Wysocki return swp_entry(type, offset); 4303a291a20SRafael J. Wysocki } 4313a291a20SRafael J. Wysocki nr_swap_pages++; 4323a291a20SRafael J. Wysocki } 4333a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 4343a291a20SRafael J. Wysocki return (swp_entry_t) {0}; 4353a291a20SRafael J. Wysocki } 4363a291a20SRafael J. Wysocki 4371da177e4SLinus Torvalds static struct swap_info_struct * swap_info_get(swp_entry_t entry) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds struct swap_info_struct * p; 4401da177e4SLinus Torvalds unsigned long offset, type; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds if (!entry.val) 4431da177e4SLinus Torvalds goto out; 4441da177e4SLinus Torvalds type = swp_type(entry); 4451da177e4SLinus Torvalds if (type >= nr_swapfiles) 4461da177e4SLinus Torvalds goto bad_nofile; 4471da177e4SLinus Torvalds p = & swap_info[type]; 4481da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 4491da177e4SLinus Torvalds goto bad_device; 4501da177e4SLinus Torvalds offset = swp_offset(entry); 4511da177e4SLinus Torvalds if (offset >= p->max) 4521da177e4SLinus Torvalds goto bad_offset; 4531da177e4SLinus Torvalds if (!p->swap_map[offset]) 4541da177e4SLinus Torvalds goto bad_free; 4555d337b91SHugh Dickins spin_lock(&swap_lock); 4561da177e4SLinus Torvalds return p; 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds bad_free: 4591da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); 4601da177e4SLinus Torvalds goto out; 4611da177e4SLinus Torvalds bad_offset: 4621da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); 4631da177e4SLinus Torvalds goto out; 4641da177e4SLinus Torvalds bad_device: 4651da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); 4661da177e4SLinus Torvalds goto out; 4671da177e4SLinus Torvalds bad_nofile: 4681da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); 4691da177e4SLinus Torvalds out: 4701da177e4SLinus Torvalds return NULL; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds static int swap_entry_free(struct swap_info_struct *p, unsigned long offset) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds int count = p->swap_map[offset]; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds if (count < SWAP_MAP_MAX) { 4781da177e4SLinus Torvalds count--; 4791da177e4SLinus Torvalds p->swap_map[offset] = count; 4801da177e4SLinus Torvalds if (!count) { 4811da177e4SLinus Torvalds if (offset < p->lowest_bit) 4821da177e4SLinus Torvalds p->lowest_bit = offset; 4831da177e4SLinus Torvalds if (offset > p->highest_bit) 4841da177e4SLinus Torvalds p->highest_bit = offset; 48589d09a2cSHugh Dickins if (p->prio > swap_info[swap_list.next].prio) 48689d09a2cSHugh Dickins swap_list.next = p - swap_info; 4871da177e4SLinus Torvalds nr_swap_pages++; 4881da177e4SLinus Torvalds p->inuse_pages--; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds return count; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds /* 4951da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 4961da177e4SLinus Torvalds * is still around or has not been recycled. 4971da177e4SLinus Torvalds */ 4981da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 4991da177e4SLinus Torvalds { 5001da177e4SLinus Torvalds struct swap_info_struct * p; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds p = swap_info_get(entry); 5031da177e4SLinus Torvalds if (p) { 5041da177e4SLinus Torvalds swap_entry_free(p, swp_offset(entry)); 5055d337b91SHugh Dickins spin_unlock(&swap_lock); 5061da177e4SLinus Torvalds } 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* 510c475a8abSHugh Dickins * How many references to page are currently swapped out? 5111da177e4SLinus Torvalds */ 512c475a8abSHugh Dickins static inline int page_swapcount(struct page *page) 5131da177e4SLinus Torvalds { 514c475a8abSHugh Dickins int count = 0; 5151da177e4SLinus Torvalds struct swap_info_struct *p; 5161da177e4SLinus Torvalds swp_entry_t entry; 5171da177e4SLinus Torvalds 5184c21e2f2SHugh Dickins entry.val = page_private(page); 5191da177e4SLinus Torvalds p = swap_info_get(entry); 5201da177e4SLinus Torvalds if (p) { 521c475a8abSHugh Dickins /* Subtract the 1 for the swap cache itself */ 522c475a8abSHugh Dickins count = p->swap_map[swp_offset(entry)] - 1; 5235d337b91SHugh Dickins spin_unlock(&swap_lock); 5241da177e4SLinus Torvalds } 525c475a8abSHugh Dickins return count; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds /* 5297b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 5307b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 5317b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 5327b1fe597SHugh Dickins * later would only waste time away from clustering. 5331da177e4SLinus Torvalds */ 5347b1fe597SHugh Dickins int reuse_swap_page(struct page *page) 5351da177e4SLinus Torvalds { 536c475a8abSHugh Dickins int count; 5371da177e4SLinus Torvalds 53851726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 539c475a8abSHugh Dickins count = page_mapcount(page); 5407b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 541c475a8abSHugh Dickins count += page_swapcount(page); 5427b1fe597SHugh Dickins if (count == 1 && !PageWriteback(page)) { 5437b1fe597SHugh Dickins delete_from_swap_cache(page); 5447b1fe597SHugh Dickins SetPageDirty(page); 5457b1fe597SHugh Dickins } 5467b1fe597SHugh Dickins } 547c475a8abSHugh Dickins return count == 1; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* 551a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 552a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 5531da177e4SLinus Torvalds */ 554a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 5551da177e4SLinus Torvalds { 55651726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds if (!PageSwapCache(page)) 5591da177e4SLinus Torvalds return 0; 5601da177e4SLinus Torvalds if (PageWriteback(page)) 5611da177e4SLinus Torvalds return 0; 562a2c43eedSHugh Dickins if (page_swapcount(page)) 5631da177e4SLinus Torvalds return 0; 5641da177e4SLinus Torvalds 565a2c43eedSHugh Dickins delete_from_swap_cache(page); 5661da177e4SLinus Torvalds SetPageDirty(page); 567a2c43eedSHugh Dickins return 1; 56868a22394SRik van Riel } 56968a22394SRik van Riel 57068a22394SRik van Riel /* 5711da177e4SLinus Torvalds * Free the swap entry like above, but also try to 5721da177e4SLinus Torvalds * free the page cache entry if it is the last user. 5731da177e4SLinus Torvalds */ 574*2509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds struct swap_info_struct *p; 5771da177e4SLinus Torvalds struct page *page = NULL; 5781da177e4SLinus Torvalds 5790697212aSChristoph Lameter if (is_migration_entry(entry)) 580*2509ef26SHugh Dickins return 1; 5810697212aSChristoph Lameter 5821da177e4SLinus Torvalds p = swap_info_get(entry); 5831da177e4SLinus Torvalds if (p) { 58493fac704SNick Piggin if (swap_entry_free(p, swp_offset(entry)) == 1) { 58593fac704SNick Piggin page = find_get_page(&swapper_space, entry.val); 5868413ac9dSNick Piggin if (page && !trylock_page(page)) { 58793fac704SNick Piggin page_cache_release(page); 58893fac704SNick Piggin page = NULL; 58993fac704SNick Piggin } 59093fac704SNick Piggin } 5915d337b91SHugh Dickins spin_unlock(&swap_lock); 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds if (page) { 594a2c43eedSHugh Dickins /* 595a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 596a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 597a2c43eedSHugh Dickins */ 59893fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 599a2c43eedSHugh Dickins (!page_mapped(page) || vm_swap_full())) { 6001da177e4SLinus Torvalds delete_from_swap_cache(page); 6011da177e4SLinus Torvalds SetPageDirty(page); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds unlock_page(page); 6041da177e4SLinus Torvalds page_cache_release(page); 6051da177e4SLinus Torvalds } 606*2509ef26SHugh Dickins return p != NULL; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 609b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 610f577eb30SRafael J. Wysocki /* 611915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 612f577eb30SRafael J. Wysocki * 613915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 614915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 615915bae9eSRafael J. Wysocki * 616915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 617f577eb30SRafael J. Wysocki */ 6187bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 619f577eb30SRafael J. Wysocki { 620915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 621f577eb30SRafael J. Wysocki int i; 622f577eb30SRafael J. Wysocki 623915bae9eSRafael J. Wysocki if (device) 624915bae9eSRafael J. Wysocki bdev = bdget(device); 625915bae9eSRafael J. Wysocki 626f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 627f577eb30SRafael J. Wysocki for (i = 0; i < nr_swapfiles; i++) { 628915bae9eSRafael J. Wysocki struct swap_info_struct *sis = swap_info + i; 629f577eb30SRafael J. Wysocki 630915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 631f577eb30SRafael J. Wysocki continue; 632b6b5bce3SRafael J. Wysocki 633915bae9eSRafael J. Wysocki if (!bdev) { 6347bf23687SRafael J. Wysocki if (bdev_p) 6357bf23687SRafael J. Wysocki *bdev_p = sis->bdev; 6367bf23687SRafael J. Wysocki 6376e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 6386e1819d6SRafael J. Wysocki return i; 6396e1819d6SRafael J. Wysocki } 640915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 641915bae9eSRafael J. Wysocki struct swap_extent *se; 642915bae9eSRafael J. Wysocki 643915bae9eSRafael J. Wysocki se = list_entry(sis->extent_list.next, 644915bae9eSRafael J. Wysocki struct swap_extent, list); 645915bae9eSRafael J. Wysocki if (se->start_block == offset) { 6467bf23687SRafael J. Wysocki if (bdev_p) 6477bf23687SRafael J. Wysocki *bdev_p = sis->bdev; 6487bf23687SRafael J. Wysocki 649f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 650915bae9eSRafael J. Wysocki bdput(bdev); 651f577eb30SRafael J. Wysocki return i; 652f577eb30SRafael J. Wysocki } 653f577eb30SRafael J. Wysocki } 654915bae9eSRafael J. Wysocki } 655f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 656915bae9eSRafael J. Wysocki if (bdev) 657915bae9eSRafael J. Wysocki bdput(bdev); 658915bae9eSRafael J. Wysocki 659f577eb30SRafael J. Wysocki return -ENODEV; 660f577eb30SRafael J. Wysocki } 661f577eb30SRafael J. Wysocki 662f577eb30SRafael J. Wysocki /* 663f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 664f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 665f577eb30SRafael J. Wysocki * 666f577eb30SRafael J. Wysocki * This is needed for software suspend 667f577eb30SRafael J. Wysocki */ 668f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 669f577eb30SRafael J. Wysocki { 670f577eb30SRafael J. Wysocki unsigned int n = 0; 671f577eb30SRafael J. Wysocki 672f577eb30SRafael J. Wysocki if (type < nr_swapfiles) { 673f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 674f577eb30SRafael J. Wysocki if (swap_info[type].flags & SWP_WRITEOK) { 675f577eb30SRafael J. Wysocki n = swap_info[type].pages; 676f577eb30SRafael J. Wysocki if (free) 677f577eb30SRafael J. Wysocki n -= swap_info[type].inuse_pages; 678f577eb30SRafael J. Wysocki } 679f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 680f577eb30SRafael J. Wysocki } 681f577eb30SRafael J. Wysocki return n; 682f577eb30SRafael J. Wysocki } 683f577eb30SRafael J. Wysocki #endif 684f577eb30SRafael J. Wysocki 6851da177e4SLinus Torvalds /* 68672866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 68772866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 68872866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 6891da177e4SLinus Torvalds */ 690044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 6911da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 6921da177e4SLinus Torvalds { 693044d66c1SHugh Dickins spinlock_t *ptl; 694044d66c1SHugh Dickins pte_t *pte; 695044d66c1SHugh Dickins int ret = 1; 696044d66c1SHugh Dickins 697e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) 698044d66c1SHugh Dickins ret = -ENOMEM; 699044d66c1SHugh Dickins 700044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 701044d66c1SHugh Dickins if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) { 702044d66c1SHugh Dickins if (ret > 0) 703044d66c1SHugh Dickins mem_cgroup_uncharge_page(page); 704044d66c1SHugh Dickins ret = 0; 705044d66c1SHugh Dickins goto out; 706044d66c1SHugh Dickins } 7078a9f3ccdSBalbir Singh 7084294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 7091da177e4SLinus Torvalds get_page(page); 7101da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 7111da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 7121da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 7131da177e4SLinus Torvalds swap_free(entry); 7141da177e4SLinus Torvalds /* 7151da177e4SLinus Torvalds * Move the page to the active list so it is not 7161da177e4SLinus Torvalds * immediately swapped out again after swapon. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds activate_page(page); 719044d66c1SHugh Dickins out: 720044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 721044d66c1SHugh Dickins return ret; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 7251da177e4SLinus Torvalds unsigned long addr, unsigned long end, 7261da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 729705e87c0SHugh Dickins pte_t *pte; 7308a9f3ccdSBalbir Singh int ret = 0; 7311da177e4SLinus Torvalds 732044d66c1SHugh Dickins /* 733044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 734044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 735044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 736044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 737044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 738044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 739044d66c1SHugh Dickins * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 740044d66c1SHugh Dickins */ 741044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 7421da177e4SLinus Torvalds do { 7431da177e4SLinus Torvalds /* 7441da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 7451da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 7461da177e4SLinus Torvalds */ 7471da177e4SLinus Torvalds if (unlikely(pte_same(*pte, swp_pte))) { 748044d66c1SHugh Dickins pte_unmap(pte); 749044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 750044d66c1SHugh Dickins if (ret) 751044d66c1SHugh Dickins goto out; 752044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 755044d66c1SHugh Dickins pte_unmap(pte - 1); 756044d66c1SHugh Dickins out: 7578a9f3ccdSBalbir Singh return ret; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 7611da177e4SLinus Torvalds unsigned long addr, unsigned long end, 7621da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds pmd_t *pmd; 7651da177e4SLinus Torvalds unsigned long next; 7668a9f3ccdSBalbir Singh int ret; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 7691da177e4SLinus Torvalds do { 7701da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 7711da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 7721da177e4SLinus Torvalds continue; 7738a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 7748a9f3ccdSBalbir Singh if (ret) 7758a9f3ccdSBalbir Singh return ret; 7761da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 7771da177e4SLinus Torvalds return 0; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 7811da177e4SLinus Torvalds unsigned long addr, unsigned long end, 7821da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds pud_t *pud; 7851da177e4SLinus Torvalds unsigned long next; 7868a9f3ccdSBalbir Singh int ret; 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 7891da177e4SLinus Torvalds do { 7901da177e4SLinus Torvalds next = pud_addr_end(addr, end); 7911da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 7921da177e4SLinus Torvalds continue; 7938a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 7948a9f3ccdSBalbir Singh if (ret) 7958a9f3ccdSBalbir Singh return ret; 7961da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 7971da177e4SLinus Torvalds return 0; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 8011da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 8021da177e4SLinus Torvalds { 8031da177e4SLinus Torvalds pgd_t *pgd; 8041da177e4SLinus Torvalds unsigned long addr, end, next; 8058a9f3ccdSBalbir Singh int ret; 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds if (page->mapping) { 8081da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 8091da177e4SLinus Torvalds if (addr == -EFAULT) 8101da177e4SLinus Torvalds return 0; 8111da177e4SLinus Torvalds else 8121da177e4SLinus Torvalds end = addr + PAGE_SIZE; 8131da177e4SLinus Torvalds } else { 8141da177e4SLinus Torvalds addr = vma->vm_start; 8151da177e4SLinus Torvalds end = vma->vm_end; 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 8191da177e4SLinus Torvalds do { 8201da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 8211da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 8221da177e4SLinus Torvalds continue; 8238a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 8248a9f3ccdSBalbir Singh if (ret) 8258a9f3ccdSBalbir Singh return ret; 8261da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 8271da177e4SLinus Torvalds return 0; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 8311da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct vm_area_struct *vma; 8348a9f3ccdSBalbir Singh int ret = 0; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 8371da177e4SLinus Torvalds /* 8387d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 8397d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 8401da177e4SLinus Torvalds */ 841c475a8abSHugh Dickins activate_page(page); 8421da177e4SLinus Torvalds unlock_page(page); 8431da177e4SLinus Torvalds down_read(&mm->mmap_sem); 8441da177e4SLinus Torvalds lock_page(page); 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 8478a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 8481da177e4SLinus Torvalds break; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds up_read(&mm->mmap_sem); 8518a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds /* 8551da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 8561da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 8571da177e4SLinus Torvalds */ 8586eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 8596eb396dcSHugh Dickins unsigned int prev) 8601da177e4SLinus Torvalds { 8616eb396dcSHugh Dickins unsigned int max = si->max; 8626eb396dcSHugh Dickins unsigned int i = prev; 8631da177e4SLinus Torvalds int count; 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /* 8665d337b91SHugh Dickins * No need for swap_lock here: we're just looking 8671da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 8681da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 8695d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 8701da177e4SLinus Torvalds */ 8711da177e4SLinus Torvalds for (;;) { 8721da177e4SLinus Torvalds if (++i >= max) { 8731da177e4SLinus Torvalds if (!prev) { 8741da177e4SLinus Torvalds i = 0; 8751da177e4SLinus Torvalds break; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds /* 8781da177e4SLinus Torvalds * No entries in use at top of swap_map, 8791da177e4SLinus Torvalds * loop back to start and recheck there. 8801da177e4SLinus Torvalds */ 8811da177e4SLinus Torvalds max = prev + 1; 8821da177e4SLinus Torvalds prev = 0; 8831da177e4SLinus Torvalds i = 1; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds count = si->swap_map[i]; 8861da177e4SLinus Torvalds if (count && count != SWAP_MAP_BAD) 8871da177e4SLinus Torvalds break; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds return i; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /* 8931da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 8941da177e4SLinus Torvalds * and then search for the process using it. All the necessary 8951da177e4SLinus Torvalds * page table adjustments can then be made atomically. 8961da177e4SLinus Torvalds */ 8971da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 8981da177e4SLinus Torvalds { 8991da177e4SLinus Torvalds struct swap_info_struct * si = &swap_info[type]; 9001da177e4SLinus Torvalds struct mm_struct *start_mm; 9011da177e4SLinus Torvalds unsigned short *swap_map; 9021da177e4SLinus Torvalds unsigned short swcount; 9031da177e4SLinus Torvalds struct page *page; 9041da177e4SLinus Torvalds swp_entry_t entry; 9056eb396dcSHugh Dickins unsigned int i = 0; 9061da177e4SLinus Torvalds int retval = 0; 9071da177e4SLinus Torvalds int reset_overflow = 0; 9081da177e4SLinus Torvalds int shmem; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds /* 9111da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 9121da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 9131da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 9141da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 9151da177e4SLinus Torvalds * 9161da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 9171da177e4SLinus Torvalds * freed the previous entry from; but that would take less 9181da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 9191da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 9201da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 9211da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 9221da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 9231da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 9241da177e4SLinus Torvalds */ 9251da177e4SLinus Torvalds start_mm = &init_mm; 9261da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds /* 9291da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 9301da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 9311da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 9321da177e4SLinus Torvalds */ 9331da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 9341da177e4SLinus Torvalds if (signal_pending(current)) { 9351da177e4SLinus Torvalds retval = -EINTR; 9361da177e4SLinus Torvalds break; 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* 9401da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 9411da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 9421da177e4SLinus Torvalds * page and read the swap into it. 9431da177e4SLinus Torvalds */ 9441da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 9451da177e4SLinus Torvalds entry = swp_entry(type, i); 94602098feaSHugh Dickins page = read_swap_cache_async(entry, 94702098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 9481da177e4SLinus Torvalds if (!page) { 9491da177e4SLinus Torvalds /* 9501da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 9511da177e4SLinus Torvalds * has been freed independently, and will not be 9521da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 9531da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 9541da177e4SLinus Torvalds */ 9551da177e4SLinus Torvalds if (!*swap_map) 9561da177e4SLinus Torvalds continue; 9571da177e4SLinus Torvalds retval = -ENOMEM; 9581da177e4SLinus Torvalds break; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds /* 9621da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 9631da177e4SLinus Torvalds */ 9641da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 9651da177e4SLinus Torvalds mmput(start_mm); 9661da177e4SLinus Torvalds start_mm = &init_mm; 9671da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds /* 9711da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 9721da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 9731da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 9741da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 9751da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 9761da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 9771da177e4SLinus Torvalds */ 9781da177e4SLinus Torvalds wait_on_page_locked(page); 9791da177e4SLinus Torvalds wait_on_page_writeback(page); 9801da177e4SLinus Torvalds lock_page(page); 9811da177e4SLinus Torvalds wait_on_page_writeback(page); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* 9841da177e4SLinus Torvalds * Remove all references to entry. 9851da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 9861da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds shmem = 0; 9891da177e4SLinus Torvalds swcount = *swap_map; 9901da177e4SLinus Torvalds if (swcount > 1) { 9911da177e4SLinus Torvalds if (start_mm == &init_mm) 9921da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 9931da177e4SLinus Torvalds else 9941da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds if (*swap_map > 1) { 9971da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 9981da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 9991da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 10001da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 10011da177e4SLinus Torvalds struct mm_struct *mm; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 10041da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 10051da177e4SLinus Torvalds spin_lock(&mmlist_lock); 10062e0e26c7SHugh Dickins while (*swap_map > 1 && !retval && !shmem && 10071da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 10081da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 100970af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 10101da177e4SLinus Torvalds continue; 10111da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 10121da177e4SLinus Torvalds mmput(prev_mm); 10131da177e4SLinus Torvalds prev_mm = mm; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds cond_resched(); 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds swcount = *swap_map; 10181da177e4SLinus Torvalds if (swcount <= 1) 10191da177e4SLinus Torvalds ; 10201da177e4SLinus Torvalds else if (mm == &init_mm) { 10211da177e4SLinus Torvalds set_start_mm = 1; 10221da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 10231da177e4SLinus Torvalds } else 10241da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 10251da177e4SLinus Torvalds if (set_start_mm && *swap_map < swcount) { 10261da177e4SLinus Torvalds mmput(new_start_mm); 10271da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 10281da177e4SLinus Torvalds new_start_mm = mm; 10291da177e4SLinus Torvalds set_start_mm = 0; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds spin_lock(&mmlist_lock); 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 10341da177e4SLinus Torvalds mmput(prev_mm); 10351da177e4SLinus Torvalds mmput(start_mm); 10361da177e4SLinus Torvalds start_mm = new_start_mm; 10371da177e4SLinus Torvalds } 10382e0e26c7SHugh Dickins if (shmem) { 10392e0e26c7SHugh Dickins /* page has already been unlocked and released */ 10402e0e26c7SHugh Dickins if (shmem > 0) 10412e0e26c7SHugh Dickins continue; 10422e0e26c7SHugh Dickins retval = shmem; 10432e0e26c7SHugh Dickins break; 10442e0e26c7SHugh Dickins } 10451da177e4SLinus Torvalds if (retval) { 10461da177e4SLinus Torvalds unlock_page(page); 10471da177e4SLinus Torvalds page_cache_release(page); 10481da177e4SLinus Torvalds break; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* 10521da177e4SLinus Torvalds * How could swap count reach 0x7fff when the maximum 10531da177e4SLinus Torvalds * pid is 0x7fff, and there's no way to repeat a swap 10541da177e4SLinus Torvalds * page within an mm (except in shmem, where it's the 10551da177e4SLinus Torvalds * shared object which takes the reference count)? 10561da177e4SLinus Torvalds * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. 10571da177e4SLinus Torvalds * 10581da177e4SLinus Torvalds * If that's wrong, then we should worry more about 10591da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 10601da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 10611da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 10621da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 10631da177e4SLinus Torvalds */ 10641da177e4SLinus Torvalds if (*swap_map == SWAP_MAP_MAX) { 10655d337b91SHugh Dickins spin_lock(&swap_lock); 10661da177e4SLinus Torvalds *swap_map = 1; 10675d337b91SHugh Dickins spin_unlock(&swap_lock); 10681da177e4SLinus Torvalds reset_overflow = 1; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 10731da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 10741da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 10751da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 10761da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 10771da177e4SLinus Torvalds * delete from cache even if there's another reference, 10781da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 10791da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 10801da177e4SLinus Torvalds * read from disk into another page. Splitting into two 10811da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 10821da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 10831da177e4SLinus Torvalds */ 10841da177e4SLinus Torvalds if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { 10851da177e4SLinus Torvalds struct writeback_control wbc = { 10861da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 10871da177e4SLinus Torvalds }; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds swap_writepage(page, &wbc); 10901da177e4SLinus Torvalds lock_page(page); 10911da177e4SLinus Torvalds wait_on_page_writeback(page); 10921da177e4SLinus Torvalds } 109368bdc8d6SHugh Dickins 109468bdc8d6SHugh Dickins /* 109568bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 109668bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 109768bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 109868bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 109968bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 110068bdc8d6SHugh Dickins */ 110168bdc8d6SHugh Dickins if (PageSwapCache(page) && 110268bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 11031da177e4SLinus Torvalds delete_from_swap_cache(page); 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /* 11061da177e4SLinus Torvalds * So we could skip searching mms once swap count went 11071da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 11082706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 11091da177e4SLinus Torvalds */ 11101da177e4SLinus Torvalds SetPageDirty(page); 11111da177e4SLinus Torvalds unlock_page(page); 11121da177e4SLinus Torvalds page_cache_release(page); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* 11151da177e4SLinus Torvalds * Make sure that we aren't completely killing 11161da177e4SLinus Torvalds * interactive performance. 11171da177e4SLinus Torvalds */ 11181da177e4SLinus Torvalds cond_resched(); 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds mmput(start_mm); 11221da177e4SLinus Torvalds if (reset_overflow) { 11231da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 11241da177e4SLinus Torvalds swap_overflow = 0; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds return retval; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* 11305d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 11315d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 11325d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 11331da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 11341da177e4SLinus Torvalds */ 11351da177e4SLinus Torvalds static void drain_mmlist(void) 11361da177e4SLinus Torvalds { 11371da177e4SLinus Torvalds struct list_head *p, *next; 11381da177e4SLinus Torvalds unsigned int i; 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) 11411da177e4SLinus Torvalds if (swap_info[i].inuse_pages) 11421da177e4SLinus Torvalds return; 11431da177e4SLinus Torvalds spin_lock(&mmlist_lock); 11441da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 11451da177e4SLinus Torvalds list_del_init(p); 11461da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds /* 11501da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 11511da177e4SLinus Torvalds * corresponds to page offset `offset'. 11521da177e4SLinus Torvalds */ 11531da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) 11541da177e4SLinus Torvalds { 11551da177e4SLinus Torvalds struct swap_extent *se = sis->curr_swap_extent; 11561da177e4SLinus Torvalds struct swap_extent *start_se = se; 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds for ( ; ; ) { 11591da177e4SLinus Torvalds struct list_head *lh; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds if (se->start_page <= offset && 11621da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 11631da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 11641da177e4SLinus Torvalds } 116511d31886SHugh Dickins lh = se->list.next; 11661da177e4SLinus Torvalds if (lh == &sis->extent_list) 116711d31886SHugh Dickins lh = lh->next; 11681da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 11691da177e4SLinus Torvalds sis->curr_swap_extent = se; 11701da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 1174b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 11753aef83e0SRafael J. Wysocki /* 11763aef83e0SRafael J. Wysocki * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 11773aef83e0SRafael J. Wysocki * corresponding to given index in swap_info (swap type). 11783aef83e0SRafael J. Wysocki */ 11793aef83e0SRafael J. Wysocki sector_t swapdev_block(int swap_type, pgoff_t offset) 11803aef83e0SRafael J. Wysocki { 11813aef83e0SRafael J. Wysocki struct swap_info_struct *sis; 11823aef83e0SRafael J. Wysocki 11833aef83e0SRafael J. Wysocki if (swap_type >= nr_swapfiles) 11843aef83e0SRafael J. Wysocki return 0; 11853aef83e0SRafael J. Wysocki 11863aef83e0SRafael J. Wysocki sis = swap_info + swap_type; 11873aef83e0SRafael J. Wysocki return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0; 11883aef83e0SRafael J. Wysocki } 1189b0cb1a19SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 11903aef83e0SRafael J. Wysocki 11911da177e4SLinus Torvalds /* 11921da177e4SLinus Torvalds * Free all of a swapdev's extent information 11931da177e4SLinus Torvalds */ 11941da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 11951da177e4SLinus Torvalds { 11961da177e4SLinus Torvalds while (!list_empty(&sis->extent_list)) { 11971da177e4SLinus Torvalds struct swap_extent *se; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds se = list_entry(sis->extent_list.next, 12001da177e4SLinus Torvalds struct swap_extent, list); 12011da177e4SLinus Torvalds list_del(&se->list); 12021da177e4SLinus Torvalds kfree(se); 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /* 12071da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 120811d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 12091da177e4SLinus Torvalds * 121011d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 12111da177e4SLinus Torvalds */ 12121da177e4SLinus Torvalds static int 12131da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 12141da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds struct swap_extent *se; 12171da177e4SLinus Torvalds struct swap_extent *new_se; 12181da177e4SLinus Torvalds struct list_head *lh; 12191da177e4SLinus Torvalds 122011d31886SHugh Dickins lh = sis->extent_list.prev; /* The highest page extent */ 122111d31886SHugh Dickins if (lh != &sis->extent_list) { 12221da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 122311d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 122411d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 12251da177e4SLinus Torvalds /* Merge it */ 12261da177e4SLinus Torvalds se->nr_pages += nr_pages; 12271da177e4SLinus Torvalds return 0; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds } 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds /* 12321da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 12331da177e4SLinus Torvalds */ 12341da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 12351da177e4SLinus Torvalds if (new_se == NULL) 12361da177e4SLinus Torvalds return -ENOMEM; 12371da177e4SLinus Torvalds new_se->start_page = start_page; 12381da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 12391da177e4SLinus Torvalds new_se->start_block = start_block; 12401da177e4SLinus Torvalds 124111d31886SHugh Dickins list_add_tail(&new_se->list, &sis->extent_list); 124253092a74SHugh Dickins return 1; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* 12461da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 12471da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 12481da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 12491da177e4SLinus Torvalds * time for locating where on disk a page belongs. 12501da177e4SLinus Torvalds * 12511da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 12521da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 12531da177e4SLinus Torvalds * swap files identically. 12541da177e4SLinus Torvalds * 12551da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 12561da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 12571da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 12581da177e4SLinus Torvalds * 12591da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 12601da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 12611da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 12621da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 12631da177e4SLinus Torvalds * for swapping. 12641da177e4SLinus Torvalds * 1265b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 12661da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 12671da177e4SLinus Torvalds * which will scribble on the fs. 12681da177e4SLinus Torvalds * 12691da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 12701da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 12711da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 12721da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 12731da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 12741da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 12751da177e4SLinus Torvalds */ 127653092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 12771da177e4SLinus Torvalds { 12781da177e4SLinus Torvalds struct inode *inode; 12791da177e4SLinus Torvalds unsigned blocks_per_page; 12801da177e4SLinus Torvalds unsigned long page_no; 12811da177e4SLinus Torvalds unsigned blkbits; 12821da177e4SLinus Torvalds sector_t probe_block; 12831da177e4SLinus Torvalds sector_t last_block; 128453092a74SHugh Dickins sector_t lowest_block = -1; 128553092a74SHugh Dickins sector_t highest_block = 0; 128653092a74SHugh Dickins int nr_extents = 0; 12871da177e4SLinus Torvalds int ret; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 12901da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 12911da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 129253092a74SHugh Dickins *span = sis->pages; 12931da177e4SLinus Torvalds goto done; 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds blkbits = inode->i_blkbits; 12971da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* 13001da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 13011da177e4SLinus Torvalds * to be very smart. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds probe_block = 0; 13041da177e4SLinus Torvalds page_no = 0; 13051da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 13061da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 13071da177e4SLinus Torvalds page_no < sis->max) { 13081da177e4SLinus Torvalds unsigned block_in_page; 13091da177e4SLinus Torvalds sector_t first_block; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 13121da177e4SLinus Torvalds if (first_block == 0) 13131da177e4SLinus Torvalds goto bad_bmap; 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 13171da177e4SLinus Torvalds */ 13181da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 13191da177e4SLinus Torvalds probe_block++; 13201da177e4SLinus Torvalds goto reprobe; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 13241da177e4SLinus Torvalds block_in_page++) { 13251da177e4SLinus Torvalds sector_t block; 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 13281da177e4SLinus Torvalds if (block == 0) 13291da177e4SLinus Torvalds goto bad_bmap; 13301da177e4SLinus Torvalds if (block != first_block + block_in_page) { 13311da177e4SLinus Torvalds /* Discontiguity */ 13321da177e4SLinus Torvalds probe_block++; 13331da177e4SLinus Torvalds goto reprobe; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 133753092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 133853092a74SHugh Dickins if (page_no) { /* exclude the header page */ 133953092a74SHugh Dickins if (first_block < lowest_block) 134053092a74SHugh Dickins lowest_block = first_block; 134153092a74SHugh Dickins if (first_block > highest_block) 134253092a74SHugh Dickins highest_block = first_block; 134353092a74SHugh Dickins } 134453092a74SHugh Dickins 13451da177e4SLinus Torvalds /* 13461da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 13471da177e4SLinus Torvalds */ 134853092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 134953092a74SHugh Dickins if (ret < 0) 13501da177e4SLinus Torvalds goto out; 135153092a74SHugh Dickins nr_extents += ret; 13521da177e4SLinus Torvalds page_no++; 13531da177e4SLinus Torvalds probe_block += blocks_per_page; 13541da177e4SLinus Torvalds reprobe: 13551da177e4SLinus Torvalds continue; 13561da177e4SLinus Torvalds } 135753092a74SHugh Dickins ret = nr_extents; 135853092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 13591da177e4SLinus Torvalds if (page_no == 0) 1360e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 13611da177e4SLinus Torvalds sis->max = page_no; 1362e2244ec2SHugh Dickins sis->pages = page_no - 1; 13631da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 13641da177e4SLinus Torvalds done: 13651da177e4SLinus Torvalds sis->curr_swap_extent = list_entry(sis->extent_list.prev, 13661da177e4SLinus Torvalds struct swap_extent, list); 13671da177e4SLinus Torvalds goto out; 13681da177e4SLinus Torvalds bad_bmap: 13691da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 13701da177e4SLinus Torvalds ret = -EINVAL; 13711da177e4SLinus Torvalds out: 13721da177e4SLinus Torvalds return ret; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds #if 0 /* We don't need this yet */ 13761da177e4SLinus Torvalds #include <linux/backing-dev.h> 13771da177e4SLinus Torvalds int page_queue_congested(struct page *page) 13781da177e4SLinus Torvalds { 13791da177e4SLinus Torvalds struct backing_dev_info *bdi; 13801da177e4SLinus Torvalds 138151726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds if (PageSwapCache(page)) { 13844c21e2f2SHugh Dickins swp_entry_t entry = { .val = page_private(page) }; 13851da177e4SLinus Torvalds struct swap_info_struct *sis; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds sis = get_swap_info_struct(swp_type(entry)); 13881da177e4SLinus Torvalds bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; 13891da177e4SLinus Torvalds } else 13901da177e4SLinus Torvalds bdi = page->mapping->backing_dev_info; 13911da177e4SLinus Torvalds return bdi_write_congested(bdi); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds #endif 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile) 13961da177e4SLinus Torvalds { 13971da177e4SLinus Torvalds struct swap_info_struct * p = NULL; 13981da177e4SLinus Torvalds unsigned short *swap_map; 13991da177e4SLinus Torvalds struct file *swap_file, *victim; 14001da177e4SLinus Torvalds struct address_space *mapping; 14011da177e4SLinus Torvalds struct inode *inode; 14021da177e4SLinus Torvalds char * pathname; 14031da177e4SLinus Torvalds int i, type, prev; 14041da177e4SLinus Torvalds int err; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14071da177e4SLinus Torvalds return -EPERM; 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds pathname = getname(specialfile); 14101da177e4SLinus Torvalds err = PTR_ERR(pathname); 14111da177e4SLinus Torvalds if (IS_ERR(pathname)) 14121da177e4SLinus Torvalds goto out; 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 14151da177e4SLinus Torvalds putname(pathname); 14161da177e4SLinus Torvalds err = PTR_ERR(victim); 14171da177e4SLinus Torvalds if (IS_ERR(victim)) 14181da177e4SLinus Torvalds goto out; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds mapping = victim->f_mapping; 14211da177e4SLinus Torvalds prev = -1; 14225d337b91SHugh Dickins spin_lock(&swap_lock); 14231da177e4SLinus Torvalds for (type = swap_list.head; type >= 0; type = swap_info[type].next) { 14241da177e4SLinus Torvalds p = swap_info + type; 142522c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 14261da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds prev = type; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds if (type < 0) { 14321da177e4SLinus Torvalds err = -EINVAL; 14335d337b91SHugh Dickins spin_unlock(&swap_lock); 14341da177e4SLinus Torvalds goto out_dput; 14351da177e4SLinus Torvalds } 14361da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 14371da177e4SLinus Torvalds vm_unacct_memory(p->pages); 14381da177e4SLinus Torvalds else { 14391da177e4SLinus Torvalds err = -ENOMEM; 14405d337b91SHugh Dickins spin_unlock(&swap_lock); 14411da177e4SLinus Torvalds goto out_dput; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds if (prev < 0) { 14441da177e4SLinus Torvalds swap_list.head = p->next; 14451da177e4SLinus Torvalds } else { 14461da177e4SLinus Torvalds swap_info[prev].next = p->next; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds if (type == swap_list.next) { 14491da177e4SLinus Torvalds /* just pick something that's safe... */ 14501da177e4SLinus Torvalds swap_list.next = swap_list.head; 14511da177e4SLinus Torvalds } 145278ecba08SHugh Dickins if (p->prio < 0) { 145378ecba08SHugh Dickins for (i = p->next; i >= 0; i = swap_info[i].next) 145478ecba08SHugh Dickins swap_info[i].prio = p->prio--; 145578ecba08SHugh Dickins least_priority++; 145678ecba08SHugh Dickins } 14571da177e4SLinus Torvalds nr_swap_pages -= p->pages; 14581da177e4SLinus Torvalds total_swap_pages -= p->pages; 14591da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 14605d337b91SHugh Dickins spin_unlock(&swap_lock); 1461fb4f88dcSHugh Dickins 14621da177e4SLinus Torvalds current->flags |= PF_SWAPOFF; 14631da177e4SLinus Torvalds err = try_to_unuse(type); 14641da177e4SLinus Torvalds current->flags &= ~PF_SWAPOFF; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds if (err) { 14671da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 14685d337b91SHugh Dickins spin_lock(&swap_lock); 146978ecba08SHugh Dickins if (p->prio < 0) 147078ecba08SHugh Dickins p->prio = --least_priority; 147178ecba08SHugh Dickins prev = -1; 147278ecba08SHugh Dickins for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 14731da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) 14741da177e4SLinus Torvalds break; 147578ecba08SHugh Dickins prev = i; 147678ecba08SHugh Dickins } 14771da177e4SLinus Torvalds p->next = i; 14781da177e4SLinus Torvalds if (prev < 0) 14791da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 14801da177e4SLinus Torvalds else 14811da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 14821da177e4SLinus Torvalds nr_swap_pages += p->pages; 14831da177e4SLinus Torvalds total_swap_pages += p->pages; 14841da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 14855d337b91SHugh Dickins spin_unlock(&swap_lock); 14861da177e4SLinus Torvalds goto out_dput; 14871da177e4SLinus Torvalds } 148852b7efdbSHugh Dickins 148952b7efdbSHugh Dickins /* wait for any unplug function to finish */ 149052b7efdbSHugh Dickins down_write(&swap_unplug_sem); 149152b7efdbSHugh Dickins up_write(&swap_unplug_sem); 149252b7efdbSHugh Dickins 14934cd3bb10SHugh Dickins destroy_swap_extents(p); 1494fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 14955d337b91SHugh Dickins spin_lock(&swap_lock); 14961da177e4SLinus Torvalds drain_mmlist(); 14975d337b91SHugh Dickins 14985d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 14995d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 15005d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 15015d337b91SHugh Dickins spin_unlock(&swap_lock); 150213e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 15035d337b91SHugh Dickins spin_lock(&swap_lock); 15045d337b91SHugh Dickins } 15055d337b91SHugh Dickins 15061da177e4SLinus Torvalds swap_file = p->swap_file; 15071da177e4SLinus Torvalds p->swap_file = NULL; 15081da177e4SLinus Torvalds p->max = 0; 15091da177e4SLinus Torvalds swap_map = p->swap_map; 15101da177e4SLinus Torvalds p->swap_map = NULL; 15111da177e4SLinus Torvalds p->flags = 0; 15125d337b91SHugh Dickins spin_unlock(&swap_lock); 1513fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 15141da177e4SLinus Torvalds vfree(swap_map); 15151da177e4SLinus Torvalds inode = mapping->host; 15161da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 15171da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 15181da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 15191da177e4SLinus Torvalds bd_release(bdev); 15201da177e4SLinus Torvalds } else { 15211b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 15221da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 15231b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds filp_close(swap_file, NULL); 15261da177e4SLinus Torvalds err = 0; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds out_dput: 15291da177e4SLinus Torvalds filp_close(victim, NULL); 15301da177e4SLinus Torvalds out: 15311da177e4SLinus Torvalds return err; 15321da177e4SLinus Torvalds } 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 15351da177e4SLinus Torvalds /* iterator */ 15361da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 15371da177e4SLinus Torvalds { 15381da177e4SLinus Torvalds struct swap_info_struct *ptr = swap_info; 15391da177e4SLinus Torvalds int i; 15401da177e4SLinus Torvalds loff_t l = *pos; 15411da177e4SLinus Torvalds 1542fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 15431da177e4SLinus Torvalds 1544881e4aabSSuleiman Souhlal if (!l) 1545881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1546881e4aabSSuleiman Souhlal 15471da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++, ptr++) { 15481da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 15491da177e4SLinus Torvalds continue; 1550881e4aabSSuleiman Souhlal if (!--l) 15511da177e4SLinus Torvalds return ptr; 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds return NULL; 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 15581da177e4SLinus Torvalds { 1559881e4aabSSuleiman Souhlal struct swap_info_struct *ptr; 15601da177e4SLinus Torvalds struct swap_info_struct *endptr = swap_info + nr_swapfiles; 15611da177e4SLinus Torvalds 1562881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 1563881e4aabSSuleiman Souhlal ptr = swap_info; 1564881e4aabSSuleiman Souhlal else { 1565881e4aabSSuleiman Souhlal ptr = v; 1566881e4aabSSuleiman Souhlal ptr++; 1567881e4aabSSuleiman Souhlal } 1568881e4aabSSuleiman Souhlal 1569881e4aabSSuleiman Souhlal for (; ptr < endptr; ptr++) { 15701da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 15711da177e4SLinus Torvalds continue; 15721da177e4SLinus Torvalds ++*pos; 15731da177e4SLinus Torvalds return ptr; 15741da177e4SLinus Torvalds } 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds return NULL; 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15791da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 15801da177e4SLinus Torvalds { 1581fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds 15841da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 15851da177e4SLinus Torvalds { 15861da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 15871da177e4SLinus Torvalds struct file *file; 15881da177e4SLinus Torvalds int len; 15891da177e4SLinus Torvalds 1590881e4aabSSuleiman Souhlal if (ptr == SEQ_START_TOKEN) { 15911da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 1592881e4aabSSuleiman Souhlal return 0; 1593881e4aabSSuleiman Souhlal } 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds file = ptr->swap_file; 1596c32c2f63SJan Blunck len = seq_path(swap, &file->f_path, " \t\n\\"); 15976eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 15981da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 1599d3ac7f89SJosef "Jeff" Sipek S_ISBLK(file->f_path.dentry->d_inode->i_mode) ? 16001da177e4SLinus Torvalds "partition" : "file\t", 16011da177e4SLinus Torvalds ptr->pages << (PAGE_SHIFT - 10), 16021da177e4SLinus Torvalds ptr->inuse_pages << (PAGE_SHIFT - 10), 16031da177e4SLinus Torvalds ptr->prio); 16041da177e4SLinus Torvalds return 0; 16051da177e4SLinus Torvalds } 16061da177e4SLinus Torvalds 160715ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 16081da177e4SLinus Torvalds .start = swap_start, 16091da177e4SLinus Torvalds .next = swap_next, 16101da177e4SLinus Torvalds .stop = swap_stop, 16111da177e4SLinus Torvalds .show = swap_show 16121da177e4SLinus Torvalds }; 16131da177e4SLinus Torvalds 16141da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 16151da177e4SLinus Torvalds { 16161da177e4SLinus Torvalds return seq_open(file, &swaps_op); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 161915ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 16201da177e4SLinus Torvalds .open = swaps_open, 16211da177e4SLinus Torvalds .read = seq_read, 16221da177e4SLinus Torvalds .llseek = seq_lseek, 16231da177e4SLinus Torvalds .release = seq_release, 16241da177e4SLinus Torvalds }; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds static int __init procswaps_init(void) 16271da177e4SLinus Torvalds { 16283d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 16291da177e4SLinus Torvalds return 0; 16301da177e4SLinus Torvalds } 16311da177e4SLinus Torvalds __initcall(procswaps_init); 16321da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 16331da177e4SLinus Torvalds 16341796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 16351796316aSJan Beulich static int __init max_swapfiles_check(void) 16361796316aSJan Beulich { 16371796316aSJan Beulich MAX_SWAPFILES_CHECK(); 16381796316aSJan Beulich return 0; 16391796316aSJan Beulich } 16401796316aSJan Beulich late_initcall(max_swapfiles_check); 16411796316aSJan Beulich #endif 16421796316aSJan Beulich 16431da177e4SLinus Torvalds /* 16441da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 16451da177e4SLinus Torvalds * 16461da177e4SLinus Torvalds * The swapon system call 16471da177e4SLinus Torvalds */ 16481da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) 16491da177e4SLinus Torvalds { 16501da177e4SLinus Torvalds struct swap_info_struct * p; 16511da177e4SLinus Torvalds char *name = NULL; 16521da177e4SLinus Torvalds struct block_device *bdev = NULL; 16531da177e4SLinus Torvalds struct file *swap_file = NULL; 16541da177e4SLinus Torvalds struct address_space *mapping; 16551da177e4SLinus Torvalds unsigned int type; 16561da177e4SLinus Torvalds int i, prev; 16571da177e4SLinus Torvalds int error; 16581da177e4SLinus Torvalds union swap_header *swap_header = NULL; 16596eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 16606eb396dcSHugh Dickins int nr_extents = 0; 166153092a74SHugh Dickins sector_t span; 16621da177e4SLinus Torvalds unsigned long maxpages = 1; 166373fd8748SHugh Dickins unsigned long swapfilepages; 166478ecba08SHugh Dickins unsigned short *swap_map = NULL; 16651da177e4SLinus Torvalds struct page *page = NULL; 16661da177e4SLinus Torvalds struct inode *inode = NULL; 16671da177e4SLinus Torvalds int did_down = 0; 16681da177e4SLinus Torvalds 16691da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 16701da177e4SLinus Torvalds return -EPERM; 16715d337b91SHugh Dickins spin_lock(&swap_lock); 16721da177e4SLinus Torvalds p = swap_info; 16731da177e4SLinus Torvalds for (type = 0 ; type < nr_swapfiles ; type++,p++) 16741da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 16751da177e4SLinus Torvalds break; 16761da177e4SLinus Torvalds error = -EPERM; 16770697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 16785d337b91SHugh Dickins spin_unlock(&swap_lock); 16791da177e4SLinus Torvalds goto out; 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds if (type >= nr_swapfiles) 16821da177e4SLinus Torvalds nr_swapfiles = type+1; 168378ecba08SHugh Dickins memset(p, 0, sizeof(*p)); 16841da177e4SLinus Torvalds INIT_LIST_HEAD(&p->extent_list); 16851da177e4SLinus Torvalds p->flags = SWP_USED; 16861da177e4SLinus Torvalds p->next = -1; 16875d337b91SHugh Dickins spin_unlock(&swap_lock); 16881da177e4SLinus Torvalds name = getname(specialfile); 16891da177e4SLinus Torvalds error = PTR_ERR(name); 16901da177e4SLinus Torvalds if (IS_ERR(name)) { 16911da177e4SLinus Torvalds name = NULL; 16921da177e4SLinus Torvalds goto bad_swap_2; 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 16951da177e4SLinus Torvalds error = PTR_ERR(swap_file); 16961da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 16971da177e4SLinus Torvalds swap_file = NULL; 16981da177e4SLinus Torvalds goto bad_swap_2; 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds p->swap_file = swap_file; 17021da177e4SLinus Torvalds mapping = swap_file->f_mapping; 17031da177e4SLinus Torvalds inode = mapping->host; 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds error = -EBUSY; 17061da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 17071da177e4SLinus Torvalds struct swap_info_struct *q = &swap_info[i]; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds if (i == type || !q->swap_file) 17101da177e4SLinus Torvalds continue; 17111da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 17121da177e4SLinus Torvalds goto bad_swap; 17131da177e4SLinus Torvalds } 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds error = -EINVAL; 17161da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 17171da177e4SLinus Torvalds bdev = I_BDEV(inode); 17181da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 17191da177e4SLinus Torvalds if (error < 0) { 17201da177e4SLinus Torvalds bdev = NULL; 1721f7b3a435SRob Landley error = -EINVAL; 17221da177e4SLinus Torvalds goto bad_swap; 17231da177e4SLinus Torvalds } 17241da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 17251da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 17261da177e4SLinus Torvalds if (error < 0) 17271da177e4SLinus Torvalds goto bad_swap; 17281da177e4SLinus Torvalds p->bdev = bdev; 17291da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 17301da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 17311b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 17321da177e4SLinus Torvalds did_down = 1; 17331da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 17341da177e4SLinus Torvalds error = -EBUSY; 17351da177e4SLinus Torvalds goto bad_swap; 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds } else { 17381da177e4SLinus Torvalds goto bad_swap; 17391da177e4SLinus Torvalds } 17401da177e4SLinus Torvalds 174173fd8748SHugh Dickins swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* 17441da177e4SLinus Torvalds * Read the swap header. 17451da177e4SLinus Torvalds */ 17461da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 17471da177e4SLinus Torvalds error = -EINVAL; 17481da177e4SLinus Torvalds goto bad_swap; 17491da177e4SLinus Torvalds } 1750090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 17511da177e4SLinus Torvalds if (IS_ERR(page)) { 17521da177e4SLinus Torvalds error = PTR_ERR(page); 17531da177e4SLinus Torvalds goto bad_swap; 17541da177e4SLinus Torvalds } 175581e33971SHugh Dickins swap_header = kmap(page); 17561da177e4SLinus Torvalds 175781e33971SHugh Dickins if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 1758e97a3111SJesper Juhl printk(KERN_ERR "Unable to find swap-space signature\n"); 17591da177e4SLinus Torvalds error = -EINVAL; 17601da177e4SLinus Torvalds goto bad_swap; 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 1763797df574SChris Dearman /* swap partition endianess hack... */ 1764797df574SChris Dearman if (swab32(swap_header->info.version) == 1) { 1765797df574SChris Dearman swab32s(&swap_header->info.version); 1766797df574SChris Dearman swab32s(&swap_header->info.last_page); 1767797df574SChris Dearman swab32s(&swap_header->info.nr_badpages); 1768797df574SChris Dearman for (i = 0; i < swap_header->info.nr_badpages; i++) 1769797df574SChris Dearman swab32s(&swap_header->info.badpages[i]); 1770797df574SChris Dearman } 177181e33971SHugh Dickins /* Check the swap header's sub-version */ 17721da177e4SLinus Torvalds if (swap_header->info.version != 1) { 17731da177e4SLinus Torvalds printk(KERN_WARNING 17741da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 17751da177e4SLinus Torvalds swap_header->info.version); 17761da177e4SLinus Torvalds error = -EINVAL; 17771da177e4SLinus Torvalds goto bad_swap; 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds p->lowest_bit = 1; 178152b7efdbSHugh Dickins p->cluster_next = 1; 178252b7efdbSHugh Dickins 17831da177e4SLinus Torvalds /* 17841da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 17851da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 17861da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 17871da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 17881da177e4SLinus Torvalds * the different architectures. In order to find the 17891da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 17901da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 17911da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 17921da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 17931da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 17941da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 17951da177e4SLinus Torvalds * swap pte. 17961da177e4SLinus Torvalds */ 179781e33971SHugh Dickins maxpages = swp_offset(pte_to_swp_entry( 179881e33971SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1; 17991da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 18001da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 18011da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds error = -EINVAL; 1804e2244ec2SHugh Dickins if (!maxpages) 1805e2244ec2SHugh Dickins goto bad_swap; 180673fd8748SHugh Dickins if (swapfilepages && maxpages > swapfilepages) { 18075d1854e1SEric Sandeen printk(KERN_WARNING 18085d1854e1SEric Sandeen "Swap area shorter than signature indicates\n"); 18095d1854e1SEric Sandeen goto bad_swap; 18105d1854e1SEric Sandeen } 1811e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1812e2244ec2SHugh Dickins goto bad_swap; 18131da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 18141da177e4SLinus Torvalds goto bad_swap; 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 181778ecba08SHugh Dickins swap_map = vmalloc(maxpages * sizeof(short)); 181878ecba08SHugh Dickins if (!swap_map) { 18191da177e4SLinus Torvalds error = -ENOMEM; 18201da177e4SLinus Torvalds goto bad_swap; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 182378ecba08SHugh Dickins memset(swap_map, 0, maxpages * sizeof(short)); 18241da177e4SLinus Torvalds for (i = 0; i < swap_header->info.nr_badpages; i++) { 1825cd105df4STobias Klauser int page_nr = swap_header->info.badpages[i]; 182681e33971SHugh Dickins if (page_nr <= 0 || page_nr >= swap_header->info.last_page) { 18271da177e4SLinus Torvalds error = -EINVAL; 182881e33971SHugh Dickins goto bad_swap; 182981e33971SHugh Dickins } 183078ecba08SHugh Dickins swap_map[page_nr] = SWAP_MAP_BAD; 18311da177e4SLinus Torvalds } 18321da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 18331da177e4SLinus Torvalds swap_header->info.nr_badpages - 18341da177e4SLinus Torvalds 1 /* header page */; 18351da177e4SLinus Torvalds 1836e2244ec2SHugh Dickins if (nr_good_pages) { 183778ecba08SHugh Dickins swap_map[0] = SWAP_MAP_BAD; 1838e2244ec2SHugh Dickins p->max = maxpages; 1839e2244ec2SHugh Dickins p->pages = nr_good_pages; 184053092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 184153092a74SHugh Dickins if (nr_extents < 0) { 184253092a74SHugh Dickins error = nr_extents; 1843e2244ec2SHugh Dickins goto bad_swap; 184453092a74SHugh Dickins } 1845e2244ec2SHugh Dickins nr_good_pages = p->pages; 1846e2244ec2SHugh Dickins } 18471da177e4SLinus Torvalds if (!nr_good_pages) { 18481da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 18491da177e4SLinus Torvalds error = -EINVAL; 18501da177e4SLinus Torvalds goto bad_swap; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 185320137a49SHugh Dickins if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { 185420137a49SHugh Dickins p->flags |= SWP_SOLIDSTATE; 185520137a49SHugh Dickins p->cluster_next = 1 + (random32() % p->highest_bit); 185620137a49SHugh Dickins } 18576a6ba831SHugh Dickins if (discard_swap(p) == 0) 18586a6ba831SHugh Dickins p->flags |= SWP_DISCARDABLE; 18596a6ba831SHugh Dickins 1860fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 18615d337b91SHugh Dickins spin_lock(&swap_lock); 186278ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 186378ecba08SHugh Dickins p->prio = 186478ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 186578ecba08SHugh Dickins else 186678ecba08SHugh Dickins p->prio = --least_priority; 186778ecba08SHugh Dickins p->swap_map = swap_map; 186822c6f8fdSHugh Dickins p->flags |= SWP_WRITEOK; 18691da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 18701da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 187153092a74SHugh Dickins 18726eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 187320137a49SHugh Dickins "Priority:%d extents:%d across:%lluk %s%s\n", 187453092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 18756a6ba831SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 187620137a49SHugh Dickins (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 18776a6ba831SHugh Dickins (p->flags & SWP_DISCARDABLE) ? "D" : ""); 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds /* insert swap space into swap_list: */ 18801da177e4SLinus Torvalds prev = -1; 18811da177e4SLinus Torvalds for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 18821da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) { 18831da177e4SLinus Torvalds break; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds prev = i; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds p->next = i; 18881da177e4SLinus Torvalds if (prev < 0) { 18891da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 18901da177e4SLinus Torvalds } else { 18911da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 18921da177e4SLinus Torvalds } 18935d337b91SHugh Dickins spin_unlock(&swap_lock); 1894fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 18951da177e4SLinus Torvalds error = 0; 18961da177e4SLinus Torvalds goto out; 18971da177e4SLinus Torvalds bad_swap: 18981da177e4SLinus Torvalds if (bdev) { 18991da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 19001da177e4SLinus Torvalds bd_release(bdev); 19011da177e4SLinus Torvalds } 19024cd3bb10SHugh Dickins destroy_swap_extents(p); 19031da177e4SLinus Torvalds bad_swap_2: 19045d337b91SHugh Dickins spin_lock(&swap_lock); 19051da177e4SLinus Torvalds p->swap_file = NULL; 19061da177e4SLinus Torvalds p->flags = 0; 19075d337b91SHugh Dickins spin_unlock(&swap_lock); 19081da177e4SLinus Torvalds vfree(swap_map); 19091da177e4SLinus Torvalds if (swap_file) 19101da177e4SLinus Torvalds filp_close(swap_file, NULL); 19111da177e4SLinus Torvalds out: 19121da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 19131da177e4SLinus Torvalds kunmap(page); 19141da177e4SLinus Torvalds page_cache_release(page); 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds if (name) 19171da177e4SLinus Torvalds putname(name); 19181da177e4SLinus Torvalds if (did_down) { 19191da177e4SLinus Torvalds if (!error) 19201da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 19211b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 19221da177e4SLinus Torvalds } 19231da177e4SLinus Torvalds return error; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds 19261da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds unsigned int i; 19291da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 19301da177e4SLinus Torvalds 19315d337b91SHugh Dickins spin_lock(&swap_lock); 19321da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 19331da177e4SLinus Torvalds if (!(swap_info[i].flags & SWP_USED) || 19341da177e4SLinus Torvalds (swap_info[i].flags & SWP_WRITEOK)) 19351da177e4SLinus Torvalds continue; 19361da177e4SLinus Torvalds nr_to_be_unused += swap_info[i].inuse_pages; 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 19391da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 19405d337b91SHugh Dickins spin_unlock(&swap_lock); 19411da177e4SLinus Torvalds } 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds /* 19441da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 19451da177e4SLinus Torvalds * 19461da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 19471da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 19481da177e4SLinus Torvalds */ 19491da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds struct swap_info_struct * p; 19521da177e4SLinus Torvalds unsigned long offset, type; 19531da177e4SLinus Torvalds int result = 0; 19541da177e4SLinus Torvalds 19550697212aSChristoph Lameter if (is_migration_entry(entry)) 19560697212aSChristoph Lameter return 1; 19570697212aSChristoph Lameter 19581da177e4SLinus Torvalds type = swp_type(entry); 19591da177e4SLinus Torvalds if (type >= nr_swapfiles) 19601da177e4SLinus Torvalds goto bad_file; 19611da177e4SLinus Torvalds p = type + swap_info; 19621da177e4SLinus Torvalds offset = swp_offset(entry); 19631da177e4SLinus Torvalds 19645d337b91SHugh Dickins spin_lock(&swap_lock); 19651da177e4SLinus Torvalds if (offset < p->max && p->swap_map[offset]) { 19661da177e4SLinus Torvalds if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { 19671da177e4SLinus Torvalds p->swap_map[offset]++; 19681da177e4SLinus Torvalds result = 1; 19691da177e4SLinus Torvalds } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { 19701da177e4SLinus Torvalds if (swap_overflow++ < 5) 19711da177e4SLinus Torvalds printk(KERN_WARNING "swap_dup: swap entry overflow\n"); 19721da177e4SLinus Torvalds p->swap_map[offset] = SWAP_MAP_MAX; 19731da177e4SLinus Torvalds result = 1; 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds } 19765d337b91SHugh Dickins spin_unlock(&swap_lock); 19771da177e4SLinus Torvalds out: 19781da177e4SLinus Torvalds return result; 19791da177e4SLinus Torvalds 19801da177e4SLinus Torvalds bad_file: 19811da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 19821da177e4SLinus Torvalds goto out; 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 19851da177e4SLinus Torvalds struct swap_info_struct * 19861da177e4SLinus Torvalds get_swap_info_struct(unsigned type) 19871da177e4SLinus Torvalds { 19881da177e4SLinus Torvalds return &swap_info[type]; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds /* 19925d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 19931da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 19941da177e4SLinus Torvalds */ 19951da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 19961da177e4SLinus Torvalds { 19978952898bSHugh Dickins struct swap_info_struct *si; 19983f9e7949SHugh Dickins int our_page_cluster = page_cluster; 19998952898bSHugh Dickins pgoff_t target, toff; 20008952898bSHugh Dickins pgoff_t base, end; 20018952898bSHugh Dickins int nr_pages = 0; 20021da177e4SLinus Torvalds 20033f9e7949SHugh Dickins if (!our_page_cluster) /* no readahead */ 20041da177e4SLinus Torvalds return 0; 20058952898bSHugh Dickins 20068952898bSHugh Dickins si = &swap_info[swp_type(entry)]; 20078952898bSHugh Dickins target = swp_offset(entry); 20088952898bSHugh Dickins base = (target >> our_page_cluster) << our_page_cluster; 20098952898bSHugh Dickins end = base + (1 << our_page_cluster); 20108952898bSHugh Dickins if (!base) /* first page is swap header */ 20118952898bSHugh Dickins base++; 20121da177e4SLinus Torvalds 20135d337b91SHugh Dickins spin_lock(&swap_lock); 20148952898bSHugh Dickins if (end > si->max) /* don't go beyond end of map */ 20158952898bSHugh Dickins end = si->max; 20168952898bSHugh Dickins 20178952898bSHugh Dickins /* Count contiguous allocated slots above our target */ 20188952898bSHugh Dickins for (toff = target; ++toff < end; nr_pages++) { 20191da177e4SLinus Torvalds /* Don't read in free or bad pages */ 20208952898bSHugh Dickins if (!si->swap_map[toff]) 20211da177e4SLinus Torvalds break; 20228952898bSHugh Dickins if (si->swap_map[toff] == SWAP_MAP_BAD) 20231da177e4SLinus Torvalds break; 20248952898bSHugh Dickins } 20258952898bSHugh Dickins /* Count contiguous allocated slots below our target */ 20268952898bSHugh Dickins for (toff = target; --toff >= base; nr_pages++) { 20278952898bSHugh Dickins /* Don't read in free or bad pages */ 20288952898bSHugh Dickins if (!si->swap_map[toff]) 20298952898bSHugh Dickins break; 20308952898bSHugh Dickins if (si->swap_map[toff] == SWAP_MAP_BAD) 20318952898bSHugh Dickins break; 20328952898bSHugh Dickins } 20335d337b91SHugh Dickins spin_unlock(&swap_lock); 20348952898bSHugh Dickins 20358952898bSHugh Dickins /* 20368952898bSHugh Dickins * Indicate starting offset, and return number of pages to get: 20378952898bSHugh Dickins * if only 1, say 0, since there's then no readahead to be done. 20388952898bSHugh Dickins */ 20398952898bSHugh Dickins *offset = ++toff; 20408952898bSHugh Dickins return nr_pages? ++nr_pages: 0; 20411da177e4SLinus Torvalds } 2042