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> 191da177e4SLinus Torvalds #include <linux/writeback.h> 201da177e4SLinus Torvalds #include <linux/proc_fs.h> 211da177e4SLinus Torvalds #include <linux/seq_file.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/module.h> 241da177e4SLinus Torvalds #include <linux/rmap.h> 251da177e4SLinus Torvalds #include <linux/security.h> 261da177e4SLinus Torvalds #include <linux/backing-dev.h> 27fc0abb14SIngo Molnar #include <linux/mutex.h> 28c59ede7bSRandy.Dunlap #include <linux/capability.h> 291da177e4SLinus Torvalds #include <linux/syscalls.h> 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds #include <asm/pgtable.h> 321da177e4SLinus Torvalds #include <asm/tlbflush.h> 331da177e4SLinus Torvalds #include <linux/swapops.h> 341da177e4SLinus Torvalds 355d337b91SHugh Dickins DEFINE_SPINLOCK(swap_lock); 361da177e4SLinus Torvalds unsigned int nr_swapfiles; 371da177e4SLinus Torvalds long total_swap_pages; 381da177e4SLinus Torvalds static int swap_overflow; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 411da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 421da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 431da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds struct swap_list_t swap_list = {-1, -1}; 461da177e4SLinus Torvalds 47f577eb30SRafael J. Wysocki static struct swap_info_struct swap_info[MAX_SWAPFILES]; 481da177e4SLinus Torvalds 49fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * We need this because the bdev->unplug_fn can sleep and we cannot 535d337b91SHugh Dickins * hold swap_lock while calling the unplug_fn. And swap_lock 54fc0abb14SIngo Molnar * cannot be turned into a mutex. 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds swp_entry_t entry; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds down_read(&swap_unplug_sem); 634c21e2f2SHugh Dickins entry.val = page_private(page); 641da177e4SLinus Torvalds if (PageSwapCache(page)) { 651da177e4SLinus Torvalds struct block_device *bdev = swap_info[swp_type(entry)].bdev; 661da177e4SLinus Torvalds struct backing_dev_info *bdi; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /* 691da177e4SLinus Torvalds * If the page is removed from swapcache from under us (with a 701da177e4SLinus Torvalds * racy try_to_unuse/swapoff) we need an additional reference 714c21e2f2SHugh Dickins * count to avoid reading garbage from page_private(page) above. 724c21e2f2SHugh Dickins * If the WARN_ON triggers during a swapoff it maybe the race 731da177e4SLinus Torvalds * condition and it's harmless. However if it triggers without 741da177e4SLinus Torvalds * swapoff it signals a problem. 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds WARN_ON(page_count(page) <= 1); 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds bdi = bdev->bd_inode->i_mapping->backing_dev_info; 79ba32311eSMcMullan, Jason blk_run_backing_dev(bdi, page); 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds up_read(&swap_unplug_sem); 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 84048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 85048c27fdSHugh Dickins #define LATENCY_LIMIT 256 86048c27fdSHugh Dickins 876eb396dcSHugh Dickins static inline unsigned long scan_swap_map(struct swap_info_struct *si) 881da177e4SLinus Torvalds { 897dfad418SHugh Dickins unsigned long offset, last_in_cluster; 90048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 911da177e4SLinus Torvalds 927dfad418SHugh Dickins /* 937dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 947dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 957dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 967dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 977dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 987dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 997dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 1001da177e4SLinus Torvalds */ 1017dfad418SHugh Dickins 10252b7efdbSHugh Dickins si->flags += SWP_SCANNING; 1037dfad418SHugh Dickins if (unlikely(!si->cluster_nr)) { 1047dfad418SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 1057dfad418SHugh Dickins if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) 1067dfad418SHugh Dickins goto lowest; 1075d337b91SHugh Dickins spin_unlock(&swap_lock); 1087dfad418SHugh Dickins 1097dfad418SHugh Dickins offset = si->lowest_bit; 1107dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 1117dfad418SHugh Dickins 1127dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 1137dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 1141da177e4SLinus Torvalds if (si->swap_map[offset]) 1157dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 1167dfad418SHugh Dickins else if (offset == last_in_cluster) { 1175d337b91SHugh Dickins spin_lock(&swap_lock); 1189b65ef59SAkinobu Mita si->cluster_next = offset-SWAPFILE_CLUSTER+1; 1197dfad418SHugh Dickins goto cluster; 1207dfad418SHugh Dickins } 121048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 122048c27fdSHugh Dickins cond_resched(); 123048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 124048c27fdSHugh Dickins } 1257dfad418SHugh Dickins } 1265d337b91SHugh Dickins spin_lock(&swap_lock); 1277dfad418SHugh Dickins goto lowest; 1287dfad418SHugh Dickins } 1297dfad418SHugh Dickins 1307dfad418SHugh Dickins si->cluster_nr--; 1317dfad418SHugh Dickins cluster: 1327dfad418SHugh Dickins offset = si->cluster_next; 1337dfad418SHugh Dickins if (offset > si->highest_bit) 1347dfad418SHugh Dickins lowest: offset = si->lowest_bit; 13552b7efdbSHugh Dickins checks: if (!(si->flags & SWP_WRITEOK)) 13652b7efdbSHugh Dickins goto no_page; 1377dfad418SHugh Dickins if (!si->highest_bit) 1387dfad418SHugh Dickins goto no_page; 1397dfad418SHugh Dickins if (!si->swap_map[offset]) { 14052b7efdbSHugh Dickins if (offset == si->lowest_bit) 1411da177e4SLinus Torvalds si->lowest_bit++; 1421da177e4SLinus Torvalds if (offset == si->highest_bit) 1431da177e4SLinus Torvalds si->highest_bit--; 1447dfad418SHugh Dickins si->inuse_pages++; 1457dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 1461da177e4SLinus Torvalds si->lowest_bit = si->max; 1471da177e4SLinus Torvalds si->highest_bit = 0; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds si->swap_map[offset] = 1; 1501da177e4SLinus Torvalds si->cluster_next = offset + 1; 15152b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 1521da177e4SLinus Torvalds return offset; 1531da177e4SLinus Torvalds } 1547dfad418SHugh Dickins 1555d337b91SHugh Dickins spin_unlock(&swap_lock); 1567dfad418SHugh Dickins while (++offset <= si->highest_bit) { 15752b7efdbSHugh Dickins if (!si->swap_map[offset]) { 1585d337b91SHugh Dickins spin_lock(&swap_lock); 15952b7efdbSHugh Dickins goto checks; 1607dfad418SHugh Dickins } 161048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 162048c27fdSHugh Dickins cond_resched(); 163048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 164048c27fdSHugh Dickins } 16552b7efdbSHugh Dickins } 1665d337b91SHugh Dickins spin_lock(&swap_lock); 1677dfad418SHugh Dickins goto lowest; 1687dfad418SHugh Dickins 1697dfad418SHugh Dickins no_page: 17052b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 1711da177e4SLinus Torvalds return 0; 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 1751da177e4SLinus Torvalds { 176fb4f88dcSHugh Dickins struct swap_info_struct *si; 177fb4f88dcSHugh Dickins pgoff_t offset; 178fb4f88dcSHugh Dickins int type, next; 179fb4f88dcSHugh Dickins int wrapped = 0; 1801da177e4SLinus Torvalds 1815d337b91SHugh Dickins spin_lock(&swap_lock); 1821da177e4SLinus Torvalds if (nr_swap_pages <= 0) 183fb4f88dcSHugh Dickins goto noswap; 184fb4f88dcSHugh Dickins nr_swap_pages--; 1851da177e4SLinus Torvalds 186fb4f88dcSHugh Dickins for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { 187fb4f88dcSHugh Dickins si = swap_info + type; 188fb4f88dcSHugh Dickins next = si->next; 189fb4f88dcSHugh Dickins if (next < 0 || 190fb4f88dcSHugh Dickins (!wrapped && si->prio != swap_info[next].prio)) { 191fb4f88dcSHugh Dickins next = swap_list.head; 192fb4f88dcSHugh Dickins wrapped++; 1931da177e4SLinus Torvalds } 194fb4f88dcSHugh Dickins 195fb4f88dcSHugh Dickins if (!si->highest_bit) 196fb4f88dcSHugh Dickins continue; 197fb4f88dcSHugh Dickins if (!(si->flags & SWP_WRITEOK)) 198fb4f88dcSHugh Dickins continue; 199fb4f88dcSHugh Dickins 200fb4f88dcSHugh Dickins swap_list.next = next; 201fb4f88dcSHugh Dickins offset = scan_swap_map(si); 2025d337b91SHugh Dickins if (offset) { 2035d337b91SHugh Dickins spin_unlock(&swap_lock); 204fb4f88dcSHugh Dickins return swp_entry(type, offset); 2055d337b91SHugh Dickins } 206fb4f88dcSHugh Dickins next = swap_list.next; 207fb4f88dcSHugh Dickins } 208fb4f88dcSHugh Dickins 209fb4f88dcSHugh Dickins nr_swap_pages++; 210fb4f88dcSHugh Dickins noswap: 2115d337b91SHugh Dickins spin_unlock(&swap_lock); 212fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2153a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type) 2163a291a20SRafael J. Wysocki { 2173a291a20SRafael J. Wysocki struct swap_info_struct *si; 2183a291a20SRafael J. Wysocki pgoff_t offset; 2193a291a20SRafael J. Wysocki 2203a291a20SRafael J. Wysocki spin_lock(&swap_lock); 2213a291a20SRafael J. Wysocki si = swap_info + type; 2223a291a20SRafael J. Wysocki if (si->flags & SWP_WRITEOK) { 2233a291a20SRafael J. Wysocki nr_swap_pages--; 2243a291a20SRafael J. Wysocki offset = scan_swap_map(si); 2253a291a20SRafael J. Wysocki if (offset) { 2263a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 2273a291a20SRafael J. Wysocki return swp_entry(type, offset); 2283a291a20SRafael J. Wysocki } 2293a291a20SRafael J. Wysocki nr_swap_pages++; 2303a291a20SRafael J. Wysocki } 2313a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 2323a291a20SRafael J. Wysocki return (swp_entry_t) {0}; 2333a291a20SRafael J. Wysocki } 2343a291a20SRafael J. Wysocki 2351da177e4SLinus Torvalds static struct swap_info_struct * swap_info_get(swp_entry_t entry) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds struct swap_info_struct * p; 2381da177e4SLinus Torvalds unsigned long offset, type; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds if (!entry.val) 2411da177e4SLinus Torvalds goto out; 2421da177e4SLinus Torvalds type = swp_type(entry); 2431da177e4SLinus Torvalds if (type >= nr_swapfiles) 2441da177e4SLinus Torvalds goto bad_nofile; 2451da177e4SLinus Torvalds p = & swap_info[type]; 2461da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 2471da177e4SLinus Torvalds goto bad_device; 2481da177e4SLinus Torvalds offset = swp_offset(entry); 2491da177e4SLinus Torvalds if (offset >= p->max) 2501da177e4SLinus Torvalds goto bad_offset; 2511da177e4SLinus Torvalds if (!p->swap_map[offset]) 2521da177e4SLinus Torvalds goto bad_free; 2535d337b91SHugh Dickins spin_lock(&swap_lock); 2541da177e4SLinus Torvalds return p; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds bad_free: 2571da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); 2581da177e4SLinus Torvalds goto out; 2591da177e4SLinus Torvalds bad_offset: 2601da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); 2611da177e4SLinus Torvalds goto out; 2621da177e4SLinus Torvalds bad_device: 2631da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); 2641da177e4SLinus Torvalds goto out; 2651da177e4SLinus Torvalds bad_nofile: 2661da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); 2671da177e4SLinus Torvalds out: 2681da177e4SLinus Torvalds return NULL; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static int swap_entry_free(struct swap_info_struct *p, unsigned long offset) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds int count = p->swap_map[offset]; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds if (count < SWAP_MAP_MAX) { 2761da177e4SLinus Torvalds count--; 2771da177e4SLinus Torvalds p->swap_map[offset] = count; 2781da177e4SLinus Torvalds if (!count) { 2791da177e4SLinus Torvalds if (offset < p->lowest_bit) 2801da177e4SLinus Torvalds p->lowest_bit = offset; 2811da177e4SLinus Torvalds if (offset > p->highest_bit) 2821da177e4SLinus Torvalds p->highest_bit = offset; 28389d09a2cSHugh Dickins if (p->prio > swap_info[swap_list.next].prio) 28489d09a2cSHugh Dickins swap_list.next = p - swap_info; 2851da177e4SLinus Torvalds nr_swap_pages++; 2861da177e4SLinus Torvalds p->inuse_pages--; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds return count; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* 2931da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 2941da177e4SLinus Torvalds * is still around or has not been recycled. 2951da177e4SLinus Torvalds */ 2961da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds struct swap_info_struct * p; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds p = swap_info_get(entry); 3011da177e4SLinus Torvalds if (p) { 3021da177e4SLinus Torvalds swap_entry_free(p, swp_offset(entry)); 3035d337b91SHugh Dickins spin_unlock(&swap_lock); 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 308c475a8abSHugh Dickins * How many references to page are currently swapped out? 3091da177e4SLinus Torvalds */ 310c475a8abSHugh Dickins static inline int page_swapcount(struct page *page) 3111da177e4SLinus Torvalds { 312c475a8abSHugh Dickins int count = 0; 3131da177e4SLinus Torvalds struct swap_info_struct *p; 3141da177e4SLinus Torvalds swp_entry_t entry; 3151da177e4SLinus Torvalds 3164c21e2f2SHugh Dickins entry.val = page_private(page); 3171da177e4SLinus Torvalds p = swap_info_get(entry); 3181da177e4SLinus Torvalds if (p) { 319c475a8abSHugh Dickins /* Subtract the 1 for the swap cache itself */ 320c475a8abSHugh Dickins count = p->swap_map[swp_offset(entry)] - 1; 3215d337b91SHugh Dickins spin_unlock(&swap_lock); 3221da177e4SLinus Torvalds } 323c475a8abSHugh Dickins return count; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* 3271da177e4SLinus Torvalds * We can use this swap cache entry directly 3281da177e4SLinus Torvalds * if there are no other references to it. 3291da177e4SLinus Torvalds */ 3301da177e4SLinus Torvalds int can_share_swap_page(struct page *page) 3311da177e4SLinus Torvalds { 332c475a8abSHugh Dickins int count; 3331da177e4SLinus Torvalds 334c475a8abSHugh Dickins BUG_ON(!PageLocked(page)); 335c475a8abSHugh Dickins count = page_mapcount(page); 336c475a8abSHugh Dickins if (count <= 1 && PageSwapCache(page)) 337c475a8abSHugh Dickins count += page_swapcount(page); 338c475a8abSHugh Dickins return count == 1; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds /* 3421da177e4SLinus Torvalds * Work out if there are any other processes sharing this 3431da177e4SLinus Torvalds * swap cache page. Free it if you can. Return success. 3441da177e4SLinus Torvalds */ 3451da177e4SLinus Torvalds int remove_exclusive_swap_page(struct page *page) 3461da177e4SLinus Torvalds { 3471da177e4SLinus Torvalds int retval; 3481da177e4SLinus Torvalds struct swap_info_struct * p; 3491da177e4SLinus Torvalds swp_entry_t entry; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 3521da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds if (!PageSwapCache(page)) 3551da177e4SLinus Torvalds return 0; 3561da177e4SLinus Torvalds if (PageWriteback(page)) 3571da177e4SLinus Torvalds return 0; 3581da177e4SLinus Torvalds if (page_count(page) != 2) /* 2: us + cache */ 3591da177e4SLinus Torvalds return 0; 3601da177e4SLinus Torvalds 3614c21e2f2SHugh Dickins entry.val = page_private(page); 3621da177e4SLinus Torvalds p = swap_info_get(entry); 3631da177e4SLinus Torvalds if (!p) 3641da177e4SLinus Torvalds return 0; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /* Is the only swap cache user the cache itself? */ 3671da177e4SLinus Torvalds retval = 0; 3681da177e4SLinus Torvalds if (p->swap_map[swp_offset(entry)] == 1) { 3691da177e4SLinus Torvalds /* Recheck the page count with the swapcache lock held.. */ 3701da177e4SLinus Torvalds write_lock_irq(&swapper_space.tree_lock); 3711da177e4SLinus Torvalds if ((page_count(page) == 2) && !PageWriteback(page)) { 3721da177e4SLinus Torvalds __delete_from_swap_cache(page); 3731da177e4SLinus Torvalds SetPageDirty(page); 3741da177e4SLinus Torvalds retval = 1; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds write_unlock_irq(&swapper_space.tree_lock); 3771da177e4SLinus Torvalds } 3785d337b91SHugh Dickins spin_unlock(&swap_lock); 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds if (retval) { 3811da177e4SLinus Torvalds swap_free(entry); 3821da177e4SLinus Torvalds page_cache_release(page); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds return retval; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * Free the swap entry like above, but also try to 3901da177e4SLinus Torvalds * free the page cache entry if it is the last user. 3911da177e4SLinus Torvalds */ 3921da177e4SLinus Torvalds void free_swap_and_cache(swp_entry_t entry) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds struct swap_info_struct * p; 3951da177e4SLinus Torvalds struct page *page = NULL; 3961da177e4SLinus Torvalds 3970697212aSChristoph Lameter if (is_migration_entry(entry)) 3980697212aSChristoph Lameter return; 3990697212aSChristoph Lameter 4001da177e4SLinus Torvalds p = swap_info_get(entry); 4011da177e4SLinus Torvalds if (p) { 40293fac704SNick Piggin if (swap_entry_free(p, swp_offset(entry)) == 1) { 40393fac704SNick Piggin page = find_get_page(&swapper_space, entry.val); 40493fac704SNick Piggin if (page && unlikely(TestSetPageLocked(page))) { 40593fac704SNick Piggin page_cache_release(page); 40693fac704SNick Piggin page = NULL; 40793fac704SNick Piggin } 40893fac704SNick Piggin } 4095d337b91SHugh Dickins spin_unlock(&swap_lock); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds if (page) { 4121da177e4SLinus Torvalds int one_user; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 4151da177e4SLinus Torvalds one_user = (page_count(page) == 2); 4161da177e4SLinus Torvalds /* Only cache user (+us), or swap space full? Free it! */ 41793fac704SNick Piggin /* Also recheck PageSwapCache after page is locked (above) */ 41893fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 41993fac704SNick Piggin (one_user || vm_swap_full())) { 4201da177e4SLinus Torvalds delete_from_swap_cache(page); 4211da177e4SLinus Torvalds SetPageDirty(page); 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds unlock_page(page); 4241da177e4SLinus Torvalds page_cache_release(page); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds 428f577eb30SRafael J. Wysocki #ifdef CONFIG_SOFTWARE_SUSPEND 429f577eb30SRafael J. Wysocki /* 430915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 431f577eb30SRafael J. Wysocki * 432915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 433915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 434915bae9eSRafael J. Wysocki * 435915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 436f577eb30SRafael J. Wysocki */ 437915bae9eSRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset) 438f577eb30SRafael J. Wysocki { 439915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 440f577eb30SRafael J. Wysocki int i; 441f577eb30SRafael J. Wysocki 442915bae9eSRafael J. Wysocki if (device) 443915bae9eSRafael J. Wysocki bdev = bdget(device); 444915bae9eSRafael J. Wysocki 445f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 446f577eb30SRafael J. Wysocki for (i = 0; i < nr_swapfiles; i++) { 447915bae9eSRafael J. Wysocki struct swap_info_struct *sis = swap_info + i; 448f577eb30SRafael J. Wysocki 449915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 450f577eb30SRafael J. Wysocki continue; 451b6b5bce3SRafael J. Wysocki 452915bae9eSRafael J. Wysocki if (!bdev) { 4536e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 4546e1819d6SRafael J. Wysocki return i; 4556e1819d6SRafael J. Wysocki } 456915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 457915bae9eSRafael J. Wysocki struct swap_extent *se; 458915bae9eSRafael J. Wysocki 459915bae9eSRafael J. Wysocki se = list_entry(sis->extent_list.next, 460915bae9eSRafael J. Wysocki struct swap_extent, list); 461915bae9eSRafael J. Wysocki if (se->start_block == offset) { 462f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 463915bae9eSRafael J. Wysocki bdput(bdev); 464f577eb30SRafael J. Wysocki return i; 465f577eb30SRafael J. Wysocki } 466f577eb30SRafael J. Wysocki } 467915bae9eSRafael J. Wysocki } 468f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 469915bae9eSRafael J. Wysocki if (bdev) 470915bae9eSRafael J. Wysocki bdput(bdev); 471915bae9eSRafael J. Wysocki 472f577eb30SRafael J. Wysocki return -ENODEV; 473f577eb30SRafael J. Wysocki } 474f577eb30SRafael J. Wysocki 475f577eb30SRafael J. Wysocki /* 476f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 477f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 478f577eb30SRafael J. Wysocki * 479f577eb30SRafael J. Wysocki * This is needed for software suspend 480f577eb30SRafael J. Wysocki */ 481f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 482f577eb30SRafael J. Wysocki { 483f577eb30SRafael J. Wysocki unsigned int n = 0; 484f577eb30SRafael J. Wysocki 485f577eb30SRafael J. Wysocki if (type < nr_swapfiles) { 486f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 487f577eb30SRafael J. Wysocki if (swap_info[type].flags & SWP_WRITEOK) { 488f577eb30SRafael J. Wysocki n = swap_info[type].pages; 489f577eb30SRafael J. Wysocki if (free) 490f577eb30SRafael J. Wysocki n -= swap_info[type].inuse_pages; 491f577eb30SRafael J. Wysocki } 492f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 493f577eb30SRafael J. Wysocki } 494f577eb30SRafael J. Wysocki return n; 495f577eb30SRafael J. Wysocki } 496f577eb30SRafael J. Wysocki #endif 497f577eb30SRafael J. Wysocki 4981da177e4SLinus Torvalds /* 49972866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 50072866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 50172866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 5021da177e4SLinus Torvalds */ 5031da177e4SLinus Torvalds static void unuse_pte(struct vm_area_struct *vma, pte_t *pte, 5041da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 5051da177e4SLinus Torvalds { 5064294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 5071da177e4SLinus Torvalds get_page(page); 5081da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 5091da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 5101da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 5111da177e4SLinus Torvalds swap_free(entry); 5121da177e4SLinus Torvalds /* 5131da177e4SLinus Torvalds * Move the page to the active list so it is not 5141da177e4SLinus Torvalds * immediately swapped out again after swapon. 5151da177e4SLinus Torvalds */ 5161da177e4SLinus Torvalds activate_page(page); 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 5201da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5211da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 524705e87c0SHugh Dickins pte_t *pte; 525705e87c0SHugh Dickins spinlock_t *ptl; 526705e87c0SHugh Dickins int found = 0; 5271da177e4SLinus Torvalds 528705e87c0SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 5291da177e4SLinus Torvalds do { 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 5321da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds if (unlikely(pte_same(*pte, swp_pte))) { 535705e87c0SHugh Dickins unuse_pte(vma, pte++, addr, entry, page); 536705e87c0SHugh Dickins found = 1; 537705e87c0SHugh Dickins break; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 540705e87c0SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 541705e87c0SHugh Dickins return found; 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 5451da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5461da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5471da177e4SLinus Torvalds { 5481da177e4SLinus Torvalds pmd_t *pmd; 5491da177e4SLinus Torvalds unsigned long next; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 5521da177e4SLinus Torvalds do { 5531da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5541da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 5551da177e4SLinus Torvalds continue; 5561da177e4SLinus Torvalds if (unuse_pte_range(vma, pmd, addr, next, entry, page)) 5571da177e4SLinus Torvalds return 1; 5581da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 5591da177e4SLinus Torvalds return 0; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 5631da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5641da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5651da177e4SLinus Torvalds { 5661da177e4SLinus Torvalds pud_t *pud; 5671da177e4SLinus Torvalds unsigned long next; 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 5701da177e4SLinus Torvalds do { 5711da177e4SLinus Torvalds next = pud_addr_end(addr, end); 5721da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 5731da177e4SLinus Torvalds continue; 5741da177e4SLinus Torvalds if (unuse_pmd_range(vma, pud, addr, next, entry, page)) 5751da177e4SLinus Torvalds return 1; 5761da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 5771da177e4SLinus Torvalds return 0; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 5811da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5821da177e4SLinus Torvalds { 5831da177e4SLinus Torvalds pgd_t *pgd; 5841da177e4SLinus Torvalds unsigned long addr, end, next; 5851da177e4SLinus Torvalds 5861da177e4SLinus Torvalds if (page->mapping) { 5871da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 5881da177e4SLinus Torvalds if (addr == -EFAULT) 5891da177e4SLinus Torvalds return 0; 5901da177e4SLinus Torvalds else 5911da177e4SLinus Torvalds end = addr + PAGE_SIZE; 5921da177e4SLinus Torvalds } else { 5931da177e4SLinus Torvalds addr = vma->vm_start; 5941da177e4SLinus Torvalds end = vma->vm_end; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 5981da177e4SLinus Torvalds do { 5991da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 6001da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 6011da177e4SLinus Torvalds continue; 6021da177e4SLinus Torvalds if (unuse_pud_range(vma, pgd, addr, next, entry, page)) 6031da177e4SLinus Torvalds return 1; 6041da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 6051da177e4SLinus Torvalds return 0; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 6091da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 6101da177e4SLinus Torvalds { 6111da177e4SLinus Torvalds struct vm_area_struct *vma; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 6141da177e4SLinus Torvalds /* 615c475a8abSHugh Dickins * Activate page so shrink_cache is unlikely to unmap its 616c475a8abSHugh Dickins * ptes while lock is dropped, so swapoff can make progress. 6171da177e4SLinus Torvalds */ 618c475a8abSHugh Dickins activate_page(page); 6191da177e4SLinus Torvalds unlock_page(page); 6201da177e4SLinus Torvalds down_read(&mm->mmap_sem); 6211da177e4SLinus Torvalds lock_page(page); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 6241da177e4SLinus Torvalds if (vma->anon_vma && unuse_vma(vma, entry, page)) 6251da177e4SLinus Torvalds break; 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds up_read(&mm->mmap_sem); 6281da177e4SLinus Torvalds /* 6291da177e4SLinus Torvalds * Currently unuse_mm cannot fail, but leave error handling 6301da177e4SLinus Torvalds * at call sites for now, since we change it from time to time. 6311da177e4SLinus Torvalds */ 6321da177e4SLinus Torvalds return 0; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds /* 6361da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 6371da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 6381da177e4SLinus Torvalds */ 6396eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 6406eb396dcSHugh Dickins unsigned int prev) 6411da177e4SLinus Torvalds { 6426eb396dcSHugh Dickins unsigned int max = si->max; 6436eb396dcSHugh Dickins unsigned int i = prev; 6441da177e4SLinus Torvalds int count; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds /* 6475d337b91SHugh Dickins * No need for swap_lock here: we're just looking 6481da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 6491da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 6505d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 6511da177e4SLinus Torvalds */ 6521da177e4SLinus Torvalds for (;;) { 6531da177e4SLinus Torvalds if (++i >= max) { 6541da177e4SLinus Torvalds if (!prev) { 6551da177e4SLinus Torvalds i = 0; 6561da177e4SLinus Torvalds break; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds /* 6591da177e4SLinus Torvalds * No entries in use at top of swap_map, 6601da177e4SLinus Torvalds * loop back to start and recheck there. 6611da177e4SLinus Torvalds */ 6621da177e4SLinus Torvalds max = prev + 1; 6631da177e4SLinus Torvalds prev = 0; 6641da177e4SLinus Torvalds i = 1; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds count = si->swap_map[i]; 6671da177e4SLinus Torvalds if (count && count != SWAP_MAP_BAD) 6681da177e4SLinus Torvalds break; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds return i; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds /* 6741da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 6751da177e4SLinus Torvalds * and then search for the process using it. All the necessary 6761da177e4SLinus Torvalds * page table adjustments can then be made atomically. 6771da177e4SLinus Torvalds */ 6781da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds struct swap_info_struct * si = &swap_info[type]; 6811da177e4SLinus Torvalds struct mm_struct *start_mm; 6821da177e4SLinus Torvalds unsigned short *swap_map; 6831da177e4SLinus Torvalds unsigned short swcount; 6841da177e4SLinus Torvalds struct page *page; 6851da177e4SLinus Torvalds swp_entry_t entry; 6866eb396dcSHugh Dickins unsigned int i = 0; 6871da177e4SLinus Torvalds int retval = 0; 6881da177e4SLinus Torvalds int reset_overflow = 0; 6891da177e4SLinus Torvalds int shmem; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* 6921da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 6931da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 6941da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 6951da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 6961da177e4SLinus Torvalds * 6971da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 6981da177e4SLinus Torvalds * freed the previous entry from; but that would take less 6991da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 7001da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 7011da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 7021da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 7031da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 7041da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 7051da177e4SLinus Torvalds */ 7061da177e4SLinus Torvalds start_mm = &init_mm; 7071da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 7081da177e4SLinus Torvalds 7091da177e4SLinus Torvalds /* 7101da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 7111da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 7121da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 7131da177e4SLinus Torvalds */ 7141da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 7151da177e4SLinus Torvalds if (signal_pending(current)) { 7161da177e4SLinus Torvalds retval = -EINTR; 7171da177e4SLinus Torvalds break; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 7211da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 7221da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 7231da177e4SLinus Torvalds * page and read the swap into it. 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 7261da177e4SLinus Torvalds entry = swp_entry(type, i); 7271da177e4SLinus Torvalds page = read_swap_cache_async(entry, NULL, 0); 7281da177e4SLinus Torvalds if (!page) { 7291da177e4SLinus Torvalds /* 7301da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 7311da177e4SLinus Torvalds * has been freed independently, and will not be 7321da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 7331da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 7341da177e4SLinus Torvalds */ 7351da177e4SLinus Torvalds if (!*swap_map) 7361da177e4SLinus Torvalds continue; 7371da177e4SLinus Torvalds retval = -ENOMEM; 7381da177e4SLinus Torvalds break; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /* 7421da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 7431da177e4SLinus Torvalds */ 7441da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 7451da177e4SLinus Torvalds mmput(start_mm); 7461da177e4SLinus Torvalds start_mm = &init_mm; 7471da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* 7511da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 7521da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 7531da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 7541da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 7551da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 7561da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds wait_on_page_locked(page); 7591da177e4SLinus Torvalds wait_on_page_writeback(page); 7601da177e4SLinus Torvalds lock_page(page); 7611da177e4SLinus Torvalds wait_on_page_writeback(page); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * Remove all references to entry. 7651da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 7661da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 7671da177e4SLinus Torvalds */ 7681da177e4SLinus Torvalds shmem = 0; 7691da177e4SLinus Torvalds swcount = *swap_map; 7701da177e4SLinus Torvalds if (swcount > 1) { 7711da177e4SLinus Torvalds if (start_mm == &init_mm) 7721da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 7731da177e4SLinus Torvalds else 7741da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds if (*swap_map > 1) { 7771da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 7781da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 7791da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 7801da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 7811da177e4SLinus Torvalds struct mm_struct *mm; 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 7841da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 7851da177e4SLinus Torvalds spin_lock(&mmlist_lock); 7861da177e4SLinus Torvalds while (*swap_map > 1 && !retval && 7871da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 7881da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 78970af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 7901da177e4SLinus Torvalds continue; 7911da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7921da177e4SLinus Torvalds mmput(prev_mm); 7931da177e4SLinus Torvalds prev_mm = mm; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds cond_resched(); 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds swcount = *swap_map; 7981da177e4SLinus Torvalds if (swcount <= 1) 7991da177e4SLinus Torvalds ; 8001da177e4SLinus Torvalds else if (mm == &init_mm) { 8011da177e4SLinus Torvalds set_start_mm = 1; 8021da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 8031da177e4SLinus Torvalds } else 8041da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 8051da177e4SLinus Torvalds if (set_start_mm && *swap_map < swcount) { 8061da177e4SLinus Torvalds mmput(new_start_mm); 8071da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 8081da177e4SLinus Torvalds new_start_mm = mm; 8091da177e4SLinus Torvalds set_start_mm = 0; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8141da177e4SLinus Torvalds mmput(prev_mm); 8151da177e4SLinus Torvalds mmput(start_mm); 8161da177e4SLinus Torvalds start_mm = new_start_mm; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds if (retval) { 8191da177e4SLinus Torvalds unlock_page(page); 8201da177e4SLinus Torvalds page_cache_release(page); 8211da177e4SLinus Torvalds break; 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds /* 8251da177e4SLinus Torvalds * How could swap count reach 0x7fff when the maximum 8261da177e4SLinus Torvalds * pid is 0x7fff, and there's no way to repeat a swap 8271da177e4SLinus Torvalds * page within an mm (except in shmem, where it's the 8281da177e4SLinus Torvalds * shared object which takes the reference count)? 8291da177e4SLinus Torvalds * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. 8301da177e4SLinus Torvalds * 8311da177e4SLinus Torvalds * If that's wrong, then we should worry more about 8321da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 8331da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 8341da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 8351da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 8361da177e4SLinus Torvalds */ 8371da177e4SLinus Torvalds if (*swap_map == SWAP_MAP_MAX) { 8385d337b91SHugh Dickins spin_lock(&swap_lock); 8391da177e4SLinus Torvalds *swap_map = 1; 8405d337b91SHugh Dickins spin_unlock(&swap_lock); 8411da177e4SLinus Torvalds reset_overflow = 1; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* 8451da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 8461da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 8471da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 8481da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 8491da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 8501da177e4SLinus Torvalds * delete from cache even if there's another reference, 8511da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 8521da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 8531da177e4SLinus Torvalds * read from disk into another page. Splitting into two 8541da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 8551da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 8561da177e4SLinus Torvalds * 8571da177e4SLinus Torvalds * Note shmem_unuse already deleted a swappage from 8581da177e4SLinus Torvalds * the swap cache, unless the move to filepage failed: 8591da177e4SLinus Torvalds * in which case it left swappage in cache, lowered its 8601da177e4SLinus Torvalds * swap count to pass quickly through the loops above, 8611da177e4SLinus Torvalds * and now we must reincrement count to try again later. 8621da177e4SLinus Torvalds */ 8631da177e4SLinus Torvalds if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { 8641da177e4SLinus Torvalds struct writeback_control wbc = { 8651da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8661da177e4SLinus Torvalds }; 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds swap_writepage(page, &wbc); 8691da177e4SLinus Torvalds lock_page(page); 8701da177e4SLinus Torvalds wait_on_page_writeback(page); 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds if (PageSwapCache(page)) { 8731da177e4SLinus Torvalds if (shmem) 8741da177e4SLinus Torvalds swap_duplicate(entry); 8751da177e4SLinus Torvalds else 8761da177e4SLinus Torvalds delete_from_swap_cache(page); 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * So we could skip searching mms once swap count went 8811da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 8821da177e4SLinus Torvalds * mark page dirty so shrink_list will preserve it. 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds SetPageDirty(page); 8851da177e4SLinus Torvalds unlock_page(page); 8861da177e4SLinus Torvalds page_cache_release(page); 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds /* 8891da177e4SLinus Torvalds * Make sure that we aren't completely killing 8901da177e4SLinus Torvalds * interactive performance. 8911da177e4SLinus Torvalds */ 8921da177e4SLinus Torvalds cond_resched(); 8931da177e4SLinus Torvalds } 8941da177e4SLinus Torvalds 8951da177e4SLinus Torvalds mmput(start_mm); 8961da177e4SLinus Torvalds if (reset_overflow) { 8971da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 8981da177e4SLinus Torvalds swap_overflow = 0; 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds return retval; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds /* 9045d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 9055d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 9065d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 9071da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 9081da177e4SLinus Torvalds */ 9091da177e4SLinus Torvalds static void drain_mmlist(void) 9101da177e4SLinus Torvalds { 9111da177e4SLinus Torvalds struct list_head *p, *next; 9121da177e4SLinus Torvalds unsigned int i; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) 9151da177e4SLinus Torvalds if (swap_info[i].inuse_pages) 9161da177e4SLinus Torvalds return; 9171da177e4SLinus Torvalds spin_lock(&mmlist_lock); 9181da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 9191da177e4SLinus Torvalds list_del_init(p); 9201da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 9211da177e4SLinus Torvalds } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds /* 9241da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 9251da177e4SLinus Torvalds * corresponds to page offset `offset'. 9261da177e4SLinus Torvalds */ 9271da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) 9281da177e4SLinus Torvalds { 9291da177e4SLinus Torvalds struct swap_extent *se = sis->curr_swap_extent; 9301da177e4SLinus Torvalds struct swap_extent *start_se = se; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds for ( ; ; ) { 9331da177e4SLinus Torvalds struct list_head *lh; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds if (se->start_page <= offset && 9361da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 9371da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 9381da177e4SLinus Torvalds } 93911d31886SHugh Dickins lh = se->list.next; 9401da177e4SLinus Torvalds if (lh == &sis->extent_list) 94111d31886SHugh Dickins lh = lh->next; 9421da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 9431da177e4SLinus Torvalds sis->curr_swap_extent = se; 9441da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 9451da177e4SLinus Torvalds } 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9483aef83e0SRafael J. Wysocki #ifdef CONFIG_SOFTWARE_SUSPEND 9493aef83e0SRafael J. Wysocki /* 9503aef83e0SRafael J. Wysocki * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 9513aef83e0SRafael J. Wysocki * corresponding to given index in swap_info (swap type). 9523aef83e0SRafael J. Wysocki */ 9533aef83e0SRafael J. Wysocki sector_t swapdev_block(int swap_type, pgoff_t offset) 9543aef83e0SRafael J. Wysocki { 9553aef83e0SRafael J. Wysocki struct swap_info_struct *sis; 9563aef83e0SRafael J. Wysocki 9573aef83e0SRafael J. Wysocki if (swap_type >= nr_swapfiles) 9583aef83e0SRafael J. Wysocki return 0; 9593aef83e0SRafael J. Wysocki 9603aef83e0SRafael J. Wysocki sis = swap_info + swap_type; 9613aef83e0SRafael J. Wysocki return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0; 9623aef83e0SRafael J. Wysocki } 9633aef83e0SRafael J. Wysocki #endif /* CONFIG_SOFTWARE_SUSPEND */ 9643aef83e0SRafael J. Wysocki 9651da177e4SLinus Torvalds /* 9661da177e4SLinus Torvalds * Free all of a swapdev's extent information 9671da177e4SLinus Torvalds */ 9681da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 9691da177e4SLinus Torvalds { 9701da177e4SLinus Torvalds while (!list_empty(&sis->extent_list)) { 9711da177e4SLinus Torvalds struct swap_extent *se; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds se = list_entry(sis->extent_list.next, 9741da177e4SLinus Torvalds struct swap_extent, list); 9751da177e4SLinus Torvalds list_del(&se->list); 9761da177e4SLinus Torvalds kfree(se); 9771da177e4SLinus Torvalds } 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* 9811da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 98211d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 9831da177e4SLinus Torvalds * 98411d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 9851da177e4SLinus Torvalds */ 9861da177e4SLinus Torvalds static int 9871da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 9881da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds struct swap_extent *se; 9911da177e4SLinus Torvalds struct swap_extent *new_se; 9921da177e4SLinus Torvalds struct list_head *lh; 9931da177e4SLinus Torvalds 99411d31886SHugh Dickins lh = sis->extent_list.prev; /* The highest page extent */ 99511d31886SHugh Dickins if (lh != &sis->extent_list) { 9961da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 99711d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 99811d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 9991da177e4SLinus Torvalds /* Merge it */ 10001da177e4SLinus Torvalds se->nr_pages += nr_pages; 10011da177e4SLinus Torvalds return 0; 10021da177e4SLinus Torvalds } 10031da177e4SLinus Torvalds } 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds /* 10061da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 10071da177e4SLinus Torvalds */ 10081da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 10091da177e4SLinus Torvalds if (new_se == NULL) 10101da177e4SLinus Torvalds return -ENOMEM; 10111da177e4SLinus Torvalds new_se->start_page = start_page; 10121da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 10131da177e4SLinus Torvalds new_se->start_block = start_block; 10141da177e4SLinus Torvalds 101511d31886SHugh Dickins list_add_tail(&new_se->list, &sis->extent_list); 101653092a74SHugh Dickins return 1; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds /* 10201da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 10211da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 10221da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 10231da177e4SLinus Torvalds * time for locating where on disk a page belongs. 10241da177e4SLinus Torvalds * 10251da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 10261da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 10271da177e4SLinus Torvalds * swap files identically. 10281da177e4SLinus Torvalds * 10291da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 10301da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 10311da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 10321da177e4SLinus Torvalds * 10331da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 10341da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 10351da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 10361da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 10371da177e4SLinus Torvalds * for swapping. 10381da177e4SLinus Torvalds * 1039b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 10401da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 10411da177e4SLinus Torvalds * which will scribble on the fs. 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 10441da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 10451da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 10461da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 10471da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 10481da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 10491da177e4SLinus Torvalds */ 105053092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 10511da177e4SLinus Torvalds { 10521da177e4SLinus Torvalds struct inode *inode; 10531da177e4SLinus Torvalds unsigned blocks_per_page; 10541da177e4SLinus Torvalds unsigned long page_no; 10551da177e4SLinus Torvalds unsigned blkbits; 10561da177e4SLinus Torvalds sector_t probe_block; 10571da177e4SLinus Torvalds sector_t last_block; 105853092a74SHugh Dickins sector_t lowest_block = -1; 105953092a74SHugh Dickins sector_t highest_block = 0; 106053092a74SHugh Dickins int nr_extents = 0; 10611da177e4SLinus Torvalds int ret; 10621da177e4SLinus Torvalds 10631da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 10641da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 10651da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 106653092a74SHugh Dickins *span = sis->pages; 10671da177e4SLinus Torvalds goto done; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds blkbits = inode->i_blkbits; 10711da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /* 10741da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 10751da177e4SLinus Torvalds * to be very smart. 10761da177e4SLinus Torvalds */ 10771da177e4SLinus Torvalds probe_block = 0; 10781da177e4SLinus Torvalds page_no = 0; 10791da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 10801da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 10811da177e4SLinus Torvalds page_no < sis->max) { 10821da177e4SLinus Torvalds unsigned block_in_page; 10831da177e4SLinus Torvalds sector_t first_block; 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 10861da177e4SLinus Torvalds if (first_block == 0) 10871da177e4SLinus Torvalds goto bad_bmap; 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 10931da177e4SLinus Torvalds probe_block++; 10941da177e4SLinus Torvalds goto reprobe; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 10981da177e4SLinus Torvalds block_in_page++) { 10991da177e4SLinus Torvalds sector_t block; 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 11021da177e4SLinus Torvalds if (block == 0) 11031da177e4SLinus Torvalds goto bad_bmap; 11041da177e4SLinus Torvalds if (block != first_block + block_in_page) { 11051da177e4SLinus Torvalds /* Discontiguity */ 11061da177e4SLinus Torvalds probe_block++; 11071da177e4SLinus Torvalds goto reprobe; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 111153092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 111253092a74SHugh Dickins if (page_no) { /* exclude the header page */ 111353092a74SHugh Dickins if (first_block < lowest_block) 111453092a74SHugh Dickins lowest_block = first_block; 111553092a74SHugh Dickins if (first_block > highest_block) 111653092a74SHugh Dickins highest_block = first_block; 111753092a74SHugh Dickins } 111853092a74SHugh Dickins 11191da177e4SLinus Torvalds /* 11201da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 11211da177e4SLinus Torvalds */ 112253092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 112353092a74SHugh Dickins if (ret < 0) 11241da177e4SLinus Torvalds goto out; 112553092a74SHugh Dickins nr_extents += ret; 11261da177e4SLinus Torvalds page_no++; 11271da177e4SLinus Torvalds probe_block += blocks_per_page; 11281da177e4SLinus Torvalds reprobe: 11291da177e4SLinus Torvalds continue; 11301da177e4SLinus Torvalds } 113153092a74SHugh Dickins ret = nr_extents; 113253092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 11331da177e4SLinus Torvalds if (page_no == 0) 1134e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 11351da177e4SLinus Torvalds sis->max = page_no; 1136e2244ec2SHugh Dickins sis->pages = page_no - 1; 11371da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 11381da177e4SLinus Torvalds done: 11391da177e4SLinus Torvalds sis->curr_swap_extent = list_entry(sis->extent_list.prev, 11401da177e4SLinus Torvalds struct swap_extent, list); 11411da177e4SLinus Torvalds goto out; 11421da177e4SLinus Torvalds bad_bmap: 11431da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 11441da177e4SLinus Torvalds ret = -EINVAL; 11451da177e4SLinus Torvalds out: 11461da177e4SLinus Torvalds return ret; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds #if 0 /* We don't need this yet */ 11501da177e4SLinus Torvalds #include <linux/backing-dev.h> 11511da177e4SLinus Torvalds int page_queue_congested(struct page *page) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds struct backing_dev_info *bdi; 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if (PageSwapCache(page)) { 11584c21e2f2SHugh Dickins swp_entry_t entry = { .val = page_private(page) }; 11591da177e4SLinus Torvalds struct swap_info_struct *sis; 11601da177e4SLinus Torvalds 11611da177e4SLinus Torvalds sis = get_swap_info_struct(swp_type(entry)); 11621da177e4SLinus Torvalds bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; 11631da177e4SLinus Torvalds } else 11641da177e4SLinus Torvalds bdi = page->mapping->backing_dev_info; 11651da177e4SLinus Torvalds return bdi_write_congested(bdi); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds #endif 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile) 11701da177e4SLinus Torvalds { 11711da177e4SLinus Torvalds struct swap_info_struct * p = NULL; 11721da177e4SLinus Torvalds unsigned short *swap_map; 11731da177e4SLinus Torvalds struct file *swap_file, *victim; 11741da177e4SLinus Torvalds struct address_space *mapping; 11751da177e4SLinus Torvalds struct inode *inode; 11761da177e4SLinus Torvalds char * pathname; 11771da177e4SLinus Torvalds int i, type, prev; 11781da177e4SLinus Torvalds int err; 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11811da177e4SLinus Torvalds return -EPERM; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds pathname = getname(specialfile); 11841da177e4SLinus Torvalds err = PTR_ERR(pathname); 11851da177e4SLinus Torvalds if (IS_ERR(pathname)) 11861da177e4SLinus Torvalds goto out; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 11891da177e4SLinus Torvalds putname(pathname); 11901da177e4SLinus Torvalds err = PTR_ERR(victim); 11911da177e4SLinus Torvalds if (IS_ERR(victim)) 11921da177e4SLinus Torvalds goto out; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds mapping = victim->f_mapping; 11951da177e4SLinus Torvalds prev = -1; 11965d337b91SHugh Dickins spin_lock(&swap_lock); 11971da177e4SLinus Torvalds for (type = swap_list.head; type >= 0; type = swap_info[type].next) { 11981da177e4SLinus Torvalds p = swap_info + type; 11991da177e4SLinus Torvalds if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) { 12001da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 12011da177e4SLinus Torvalds break; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds prev = type; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds if (type < 0) { 12061da177e4SLinus Torvalds err = -EINVAL; 12075d337b91SHugh Dickins spin_unlock(&swap_lock); 12081da177e4SLinus Torvalds goto out_dput; 12091da177e4SLinus Torvalds } 12101da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 12111da177e4SLinus Torvalds vm_unacct_memory(p->pages); 12121da177e4SLinus Torvalds else { 12131da177e4SLinus Torvalds err = -ENOMEM; 12145d337b91SHugh Dickins spin_unlock(&swap_lock); 12151da177e4SLinus Torvalds goto out_dput; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds if (prev < 0) { 12181da177e4SLinus Torvalds swap_list.head = p->next; 12191da177e4SLinus Torvalds } else { 12201da177e4SLinus Torvalds swap_info[prev].next = p->next; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds if (type == swap_list.next) { 12231da177e4SLinus Torvalds /* just pick something that's safe... */ 12241da177e4SLinus Torvalds swap_list.next = swap_list.head; 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds nr_swap_pages -= p->pages; 12271da177e4SLinus Torvalds total_swap_pages -= p->pages; 12281da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 12295d337b91SHugh Dickins spin_unlock(&swap_lock); 1230fb4f88dcSHugh Dickins 12311da177e4SLinus Torvalds current->flags |= PF_SWAPOFF; 12321da177e4SLinus Torvalds err = try_to_unuse(type); 12331da177e4SLinus Torvalds current->flags &= ~PF_SWAPOFF; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds if (err) { 12361da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 12375d337b91SHugh Dickins spin_lock(&swap_lock); 12381da177e4SLinus Torvalds for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next) 12391da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) 12401da177e4SLinus Torvalds break; 12411da177e4SLinus Torvalds p->next = i; 12421da177e4SLinus Torvalds if (prev < 0) 12431da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 12441da177e4SLinus Torvalds else 12451da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 12461da177e4SLinus Torvalds nr_swap_pages += p->pages; 12471da177e4SLinus Torvalds total_swap_pages += p->pages; 12481da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 12495d337b91SHugh Dickins spin_unlock(&swap_lock); 12501da177e4SLinus Torvalds goto out_dput; 12511da177e4SLinus Torvalds } 125252b7efdbSHugh Dickins 125352b7efdbSHugh Dickins /* wait for any unplug function to finish */ 125452b7efdbSHugh Dickins down_write(&swap_unplug_sem); 125552b7efdbSHugh Dickins up_write(&swap_unplug_sem); 125652b7efdbSHugh Dickins 12574cd3bb10SHugh Dickins destroy_swap_extents(p); 1258fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 12595d337b91SHugh Dickins spin_lock(&swap_lock); 12601da177e4SLinus Torvalds drain_mmlist(); 12615d337b91SHugh Dickins 12625d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 12635d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 12645d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 12655d337b91SHugh Dickins spin_unlock(&swap_lock); 126613e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 12675d337b91SHugh Dickins spin_lock(&swap_lock); 12685d337b91SHugh Dickins } 12695d337b91SHugh Dickins 12701da177e4SLinus Torvalds swap_file = p->swap_file; 12711da177e4SLinus Torvalds p->swap_file = NULL; 12721da177e4SLinus Torvalds p->max = 0; 12731da177e4SLinus Torvalds swap_map = p->swap_map; 12741da177e4SLinus Torvalds p->swap_map = NULL; 12751da177e4SLinus Torvalds p->flags = 0; 12765d337b91SHugh Dickins spin_unlock(&swap_lock); 1277fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 12781da177e4SLinus Torvalds vfree(swap_map); 12791da177e4SLinus Torvalds inode = mapping->host; 12801da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 12811da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 12821da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 12831da177e4SLinus Torvalds bd_release(bdev); 12841da177e4SLinus Torvalds } else { 12851b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 12861da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 12871b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 12881da177e4SLinus Torvalds } 12891da177e4SLinus Torvalds filp_close(swap_file, NULL); 12901da177e4SLinus Torvalds err = 0; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds out_dput: 12931da177e4SLinus Torvalds filp_close(victim, NULL); 12941da177e4SLinus Torvalds out: 12951da177e4SLinus Torvalds return err; 12961da177e4SLinus Torvalds } 12971da177e4SLinus Torvalds 12981da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 12991da177e4SLinus Torvalds /* iterator */ 13001da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 13011da177e4SLinus Torvalds { 13021da177e4SLinus Torvalds struct swap_info_struct *ptr = swap_info; 13031da177e4SLinus Torvalds int i; 13041da177e4SLinus Torvalds loff_t l = *pos; 13051da177e4SLinus Torvalds 1306fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 13071da177e4SLinus Torvalds 1308881e4aabSSuleiman Souhlal if (!l) 1309881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1310881e4aabSSuleiman Souhlal 13111da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++, ptr++) { 13121da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 13131da177e4SLinus Torvalds continue; 1314881e4aabSSuleiman Souhlal if (!--l) 13151da177e4SLinus Torvalds return ptr; 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds return NULL; 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 13221da177e4SLinus Torvalds { 1323881e4aabSSuleiman Souhlal struct swap_info_struct *ptr; 13241da177e4SLinus Torvalds struct swap_info_struct *endptr = swap_info + nr_swapfiles; 13251da177e4SLinus Torvalds 1326881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 1327881e4aabSSuleiman Souhlal ptr = swap_info; 1328881e4aabSSuleiman Souhlal else { 1329881e4aabSSuleiman Souhlal ptr = v; 1330881e4aabSSuleiman Souhlal ptr++; 1331881e4aabSSuleiman Souhlal } 1332881e4aabSSuleiman Souhlal 1333881e4aabSSuleiman Souhlal for (; ptr < endptr; ptr++) { 13341da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 13351da177e4SLinus Torvalds continue; 13361da177e4SLinus Torvalds ++*pos; 13371da177e4SLinus Torvalds return ptr; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds return NULL; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 13441da177e4SLinus Torvalds { 1345fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 13491da177e4SLinus Torvalds { 13501da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 13511da177e4SLinus Torvalds struct file *file; 13521da177e4SLinus Torvalds int len; 13531da177e4SLinus Torvalds 1354881e4aabSSuleiman Souhlal if (ptr == SEQ_START_TOKEN) { 13551da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 1356881e4aabSSuleiman Souhlal return 0; 1357881e4aabSSuleiman Souhlal } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds file = ptr->swap_file; 13601da177e4SLinus Torvalds len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\"); 13616eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 13621da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 13631da177e4SLinus Torvalds S_ISBLK(file->f_dentry->d_inode->i_mode) ? 13641da177e4SLinus Torvalds "partition" : "file\t", 13651da177e4SLinus Torvalds ptr->pages << (PAGE_SHIFT - 10), 13661da177e4SLinus Torvalds ptr->inuse_pages << (PAGE_SHIFT - 10), 13671da177e4SLinus Torvalds ptr->prio); 13681da177e4SLinus Torvalds return 0; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds 1371*15ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 13721da177e4SLinus Torvalds .start = swap_start, 13731da177e4SLinus Torvalds .next = swap_next, 13741da177e4SLinus Torvalds .stop = swap_stop, 13751da177e4SLinus Torvalds .show = swap_show 13761da177e4SLinus Torvalds }; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 13791da177e4SLinus Torvalds { 13801da177e4SLinus Torvalds return seq_open(file, &swaps_op); 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 1383*15ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 13841da177e4SLinus Torvalds .open = swaps_open, 13851da177e4SLinus Torvalds .read = seq_read, 13861da177e4SLinus Torvalds .llseek = seq_lseek, 13871da177e4SLinus Torvalds .release = seq_release, 13881da177e4SLinus Torvalds }; 13891da177e4SLinus Torvalds 13901da177e4SLinus Torvalds static int __init procswaps_init(void) 13911da177e4SLinus Torvalds { 13921da177e4SLinus Torvalds struct proc_dir_entry *entry; 13931da177e4SLinus Torvalds 13941da177e4SLinus Torvalds entry = create_proc_entry("swaps", 0, NULL); 13951da177e4SLinus Torvalds if (entry) 13961da177e4SLinus Torvalds entry->proc_fops = &proc_swaps_operations; 13971da177e4SLinus Torvalds return 0; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds __initcall(procswaps_init); 14001da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds /* 14031da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 14041da177e4SLinus Torvalds * 14051da177e4SLinus Torvalds * The swapon system call 14061da177e4SLinus Torvalds */ 14071da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) 14081da177e4SLinus Torvalds { 14091da177e4SLinus Torvalds struct swap_info_struct * p; 14101da177e4SLinus Torvalds char *name = NULL; 14111da177e4SLinus Torvalds struct block_device *bdev = NULL; 14121da177e4SLinus Torvalds struct file *swap_file = NULL; 14131da177e4SLinus Torvalds struct address_space *mapping; 14141da177e4SLinus Torvalds unsigned int type; 14151da177e4SLinus Torvalds int i, prev; 14161da177e4SLinus Torvalds int error; 14171da177e4SLinus Torvalds static int least_priority; 14181da177e4SLinus Torvalds union swap_header *swap_header = NULL; 14191da177e4SLinus Torvalds int swap_header_version; 14206eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 14216eb396dcSHugh Dickins int nr_extents = 0; 142253092a74SHugh Dickins sector_t span; 14231da177e4SLinus Torvalds unsigned long maxpages = 1; 14241da177e4SLinus Torvalds int swapfilesize; 14251da177e4SLinus Torvalds unsigned short *swap_map; 14261da177e4SLinus Torvalds struct page *page = NULL; 14271da177e4SLinus Torvalds struct inode *inode = NULL; 14281da177e4SLinus Torvalds int did_down = 0; 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14311da177e4SLinus Torvalds return -EPERM; 14325d337b91SHugh Dickins spin_lock(&swap_lock); 14331da177e4SLinus Torvalds p = swap_info; 14341da177e4SLinus Torvalds for (type = 0 ; type < nr_swapfiles ; type++,p++) 14351da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 14361da177e4SLinus Torvalds break; 14371da177e4SLinus Torvalds error = -EPERM; 14380697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 14395d337b91SHugh Dickins spin_unlock(&swap_lock); 14401da177e4SLinus Torvalds goto out; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds if (type >= nr_swapfiles) 14431da177e4SLinus Torvalds nr_swapfiles = type+1; 14441da177e4SLinus Torvalds INIT_LIST_HEAD(&p->extent_list); 14451da177e4SLinus Torvalds p->flags = SWP_USED; 14461da177e4SLinus Torvalds p->swap_file = NULL; 14471da177e4SLinus Torvalds p->old_block_size = 0; 14481da177e4SLinus Torvalds p->swap_map = NULL; 14491da177e4SLinus Torvalds p->lowest_bit = 0; 14501da177e4SLinus Torvalds p->highest_bit = 0; 14511da177e4SLinus Torvalds p->cluster_nr = 0; 14521da177e4SLinus Torvalds p->inuse_pages = 0; 14531da177e4SLinus Torvalds p->next = -1; 14541da177e4SLinus Torvalds if (swap_flags & SWAP_FLAG_PREFER) { 14551da177e4SLinus Torvalds p->prio = 14561da177e4SLinus Torvalds (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT; 14571da177e4SLinus Torvalds } else { 14581da177e4SLinus Torvalds p->prio = --least_priority; 14591da177e4SLinus Torvalds } 14605d337b91SHugh Dickins spin_unlock(&swap_lock); 14611da177e4SLinus Torvalds name = getname(specialfile); 14621da177e4SLinus Torvalds error = PTR_ERR(name); 14631da177e4SLinus Torvalds if (IS_ERR(name)) { 14641da177e4SLinus Torvalds name = NULL; 14651da177e4SLinus Torvalds goto bad_swap_2; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 14681da177e4SLinus Torvalds error = PTR_ERR(swap_file); 14691da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 14701da177e4SLinus Torvalds swap_file = NULL; 14711da177e4SLinus Torvalds goto bad_swap_2; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds p->swap_file = swap_file; 14751da177e4SLinus Torvalds mapping = swap_file->f_mapping; 14761da177e4SLinus Torvalds inode = mapping->host; 14771da177e4SLinus Torvalds 14781da177e4SLinus Torvalds error = -EBUSY; 14791da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 14801da177e4SLinus Torvalds struct swap_info_struct *q = &swap_info[i]; 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds if (i == type || !q->swap_file) 14831da177e4SLinus Torvalds continue; 14841da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 14851da177e4SLinus Torvalds goto bad_swap; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds error = -EINVAL; 14891da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 14901da177e4SLinus Torvalds bdev = I_BDEV(inode); 14911da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 14921da177e4SLinus Torvalds if (error < 0) { 14931da177e4SLinus Torvalds bdev = NULL; 1494f7b3a435SRob Landley error = -EINVAL; 14951da177e4SLinus Torvalds goto bad_swap; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 14981da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 14991da177e4SLinus Torvalds if (error < 0) 15001da177e4SLinus Torvalds goto bad_swap; 15011da177e4SLinus Torvalds p->bdev = bdev; 15021da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 15031da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 15041b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 15051da177e4SLinus Torvalds did_down = 1; 15061da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 15071da177e4SLinus Torvalds error = -EBUSY; 15081da177e4SLinus Torvalds goto bad_swap; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds } else { 15111da177e4SLinus Torvalds goto bad_swap; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds swapfilesize = i_size_read(inode) >> PAGE_SHIFT; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /* 15171da177e4SLinus Torvalds * Read the swap header. 15181da177e4SLinus Torvalds */ 15191da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 15201da177e4SLinus Torvalds error = -EINVAL; 15211da177e4SLinus Torvalds goto bad_swap; 15221da177e4SLinus Torvalds } 1523090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 15241da177e4SLinus Torvalds if (IS_ERR(page)) { 15251da177e4SLinus Torvalds error = PTR_ERR(page); 15261da177e4SLinus Torvalds goto bad_swap; 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds wait_on_page_locked(page); 15291da177e4SLinus Torvalds if (!PageUptodate(page)) 15301da177e4SLinus Torvalds goto bad_swap; 15311da177e4SLinus Torvalds kmap(page); 15321da177e4SLinus Torvalds swap_header = page_address(page); 15331da177e4SLinus Torvalds 15341da177e4SLinus Torvalds if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10)) 15351da177e4SLinus Torvalds swap_header_version = 1; 15361da177e4SLinus Torvalds else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10)) 15371da177e4SLinus Torvalds swap_header_version = 2; 15381da177e4SLinus Torvalds else { 1539e97a3111SJesper Juhl printk(KERN_ERR "Unable to find swap-space signature\n"); 15401da177e4SLinus Torvalds error = -EINVAL; 15411da177e4SLinus Torvalds goto bad_swap; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds switch (swap_header_version) { 15451da177e4SLinus Torvalds case 1: 15461da177e4SLinus Torvalds printk(KERN_ERR "version 0 swap is no longer supported. " 15471da177e4SLinus Torvalds "Use mkswap -v1 %s\n", name); 15481da177e4SLinus Torvalds error = -EINVAL; 15491da177e4SLinus Torvalds goto bad_swap; 15501da177e4SLinus Torvalds case 2: 15511da177e4SLinus Torvalds /* Check the swap header's sub-version and the size of 15521da177e4SLinus Torvalds the swap file and bad block lists */ 15531da177e4SLinus Torvalds if (swap_header->info.version != 1) { 15541da177e4SLinus Torvalds printk(KERN_WARNING 15551da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 15561da177e4SLinus Torvalds swap_header->info.version); 15571da177e4SLinus Torvalds error = -EINVAL; 15581da177e4SLinus Torvalds goto bad_swap; 15591da177e4SLinus Torvalds } 15601da177e4SLinus Torvalds 15611da177e4SLinus Torvalds p->lowest_bit = 1; 156252b7efdbSHugh Dickins p->cluster_next = 1; 156352b7efdbSHugh Dickins 15641da177e4SLinus Torvalds /* 15651da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 15661da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 15671da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 15681da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 15691da177e4SLinus Torvalds * the different architectures. In order to find the 15701da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 15711da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 15721da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 15731da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 15741da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 15751da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 15761da177e4SLinus Torvalds * swap pte. 15771da177e4SLinus Torvalds */ 15781da177e4SLinus Torvalds maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1; 15791da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 15801da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 15811da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 15821da177e4SLinus Torvalds 15831da177e4SLinus Torvalds error = -EINVAL; 1584e2244ec2SHugh Dickins if (!maxpages) 1585e2244ec2SHugh Dickins goto bad_swap; 15865d1854e1SEric Sandeen if (swapfilesize && maxpages > swapfilesize) { 15875d1854e1SEric Sandeen printk(KERN_WARNING 15885d1854e1SEric Sandeen "Swap area shorter than signature indicates\n"); 15895d1854e1SEric Sandeen goto bad_swap; 15905d1854e1SEric Sandeen } 1591e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1592e2244ec2SHugh Dickins goto bad_swap; 15931da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 15941da177e4SLinus Torvalds goto bad_swap; 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 15971da177e4SLinus Torvalds if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) { 15981da177e4SLinus Torvalds error = -ENOMEM; 15991da177e4SLinus Torvalds goto bad_swap; 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds error = 0; 16031da177e4SLinus Torvalds memset(p->swap_map, 0, maxpages * sizeof(short)); 16041da177e4SLinus Torvalds for (i = 0; i < swap_header->info.nr_badpages; i++) { 1605cd105df4STobias Klauser int page_nr = swap_header->info.badpages[i]; 1606cd105df4STobias Klauser if (page_nr <= 0 || page_nr >= swap_header->info.last_page) 16071da177e4SLinus Torvalds error = -EINVAL; 16081da177e4SLinus Torvalds else 1609cd105df4STobias Klauser p->swap_map[page_nr] = SWAP_MAP_BAD; 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 16121da177e4SLinus Torvalds swap_header->info.nr_badpages - 16131da177e4SLinus Torvalds 1 /* header page */; 16141da177e4SLinus Torvalds if (error) 16151da177e4SLinus Torvalds goto bad_swap; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 1618e2244ec2SHugh Dickins if (nr_good_pages) { 1619e2244ec2SHugh Dickins p->swap_map[0] = SWAP_MAP_BAD; 1620e2244ec2SHugh Dickins p->max = maxpages; 1621e2244ec2SHugh Dickins p->pages = nr_good_pages; 162253092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 162353092a74SHugh Dickins if (nr_extents < 0) { 162453092a74SHugh Dickins error = nr_extents; 1625e2244ec2SHugh Dickins goto bad_swap; 162653092a74SHugh Dickins } 1627e2244ec2SHugh Dickins nr_good_pages = p->pages; 1628e2244ec2SHugh Dickins } 16291da177e4SLinus Torvalds if (!nr_good_pages) { 16301da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 16311da177e4SLinus Torvalds error = -EINVAL; 16321da177e4SLinus Torvalds goto bad_swap; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds 1635fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 16365d337b91SHugh Dickins spin_lock(&swap_lock); 16371da177e4SLinus Torvalds p->flags = SWP_ACTIVE; 16381da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 16391da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 164053092a74SHugh Dickins 16416eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 164253092a74SHugh Dickins "Priority:%d extents:%d across:%lluk\n", 164353092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 164453092a74SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10)); 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* insert swap space into swap_list: */ 16471da177e4SLinus Torvalds prev = -1; 16481da177e4SLinus Torvalds for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 16491da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) { 16501da177e4SLinus Torvalds break; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds prev = i; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds p->next = i; 16551da177e4SLinus Torvalds if (prev < 0) { 16561da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 16571da177e4SLinus Torvalds } else { 16581da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 16591da177e4SLinus Torvalds } 16605d337b91SHugh Dickins spin_unlock(&swap_lock); 1661fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 16621da177e4SLinus Torvalds error = 0; 16631da177e4SLinus Torvalds goto out; 16641da177e4SLinus Torvalds bad_swap: 16651da177e4SLinus Torvalds if (bdev) { 16661da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 16671da177e4SLinus Torvalds bd_release(bdev); 16681da177e4SLinus Torvalds } 16694cd3bb10SHugh Dickins destroy_swap_extents(p); 16701da177e4SLinus Torvalds bad_swap_2: 16715d337b91SHugh Dickins spin_lock(&swap_lock); 16721da177e4SLinus Torvalds swap_map = p->swap_map; 16731da177e4SLinus Torvalds p->swap_file = NULL; 16741da177e4SLinus Torvalds p->swap_map = NULL; 16751da177e4SLinus Torvalds p->flags = 0; 16761da177e4SLinus Torvalds if (!(swap_flags & SWAP_FLAG_PREFER)) 16771da177e4SLinus Torvalds ++least_priority; 16785d337b91SHugh Dickins spin_unlock(&swap_lock); 16791da177e4SLinus Torvalds vfree(swap_map); 16801da177e4SLinus Torvalds if (swap_file) 16811da177e4SLinus Torvalds filp_close(swap_file, NULL); 16821da177e4SLinus Torvalds out: 16831da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 16841da177e4SLinus Torvalds kunmap(page); 16851da177e4SLinus Torvalds page_cache_release(page); 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds if (name) 16881da177e4SLinus Torvalds putname(name); 16891da177e4SLinus Torvalds if (did_down) { 16901da177e4SLinus Torvalds if (!error) 16911da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 16921b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 16931da177e4SLinus Torvalds } 16941da177e4SLinus Torvalds return error; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 16981da177e4SLinus Torvalds { 16991da177e4SLinus Torvalds unsigned int i; 17001da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 17011da177e4SLinus Torvalds 17025d337b91SHugh Dickins spin_lock(&swap_lock); 17031da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 17041da177e4SLinus Torvalds if (!(swap_info[i].flags & SWP_USED) || 17051da177e4SLinus Torvalds (swap_info[i].flags & SWP_WRITEOK)) 17061da177e4SLinus Torvalds continue; 17071da177e4SLinus Torvalds nr_to_be_unused += swap_info[i].inuse_pages; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 17101da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 17115d337b91SHugh Dickins spin_unlock(&swap_lock); 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* 17151da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 17161da177e4SLinus Torvalds * 17171da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 17181da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 17191da177e4SLinus Torvalds */ 17201da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry) 17211da177e4SLinus Torvalds { 17221da177e4SLinus Torvalds struct swap_info_struct * p; 17231da177e4SLinus Torvalds unsigned long offset, type; 17241da177e4SLinus Torvalds int result = 0; 17251da177e4SLinus Torvalds 17260697212aSChristoph Lameter if (is_migration_entry(entry)) 17270697212aSChristoph Lameter return 1; 17280697212aSChristoph Lameter 17291da177e4SLinus Torvalds type = swp_type(entry); 17301da177e4SLinus Torvalds if (type >= nr_swapfiles) 17311da177e4SLinus Torvalds goto bad_file; 17321da177e4SLinus Torvalds p = type + swap_info; 17331da177e4SLinus Torvalds offset = swp_offset(entry); 17341da177e4SLinus Torvalds 17355d337b91SHugh Dickins spin_lock(&swap_lock); 17361da177e4SLinus Torvalds if (offset < p->max && p->swap_map[offset]) { 17371da177e4SLinus Torvalds if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { 17381da177e4SLinus Torvalds p->swap_map[offset]++; 17391da177e4SLinus Torvalds result = 1; 17401da177e4SLinus Torvalds } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { 17411da177e4SLinus Torvalds if (swap_overflow++ < 5) 17421da177e4SLinus Torvalds printk(KERN_WARNING "swap_dup: swap entry overflow\n"); 17431da177e4SLinus Torvalds p->swap_map[offset] = SWAP_MAP_MAX; 17441da177e4SLinus Torvalds result = 1; 17451da177e4SLinus Torvalds } 17461da177e4SLinus Torvalds } 17475d337b91SHugh Dickins spin_unlock(&swap_lock); 17481da177e4SLinus Torvalds out: 17491da177e4SLinus Torvalds return result; 17501da177e4SLinus Torvalds 17511da177e4SLinus Torvalds bad_file: 17521da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 17531da177e4SLinus Torvalds goto out; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds struct swap_info_struct * 17571da177e4SLinus Torvalds get_swap_info_struct(unsigned type) 17581da177e4SLinus Torvalds { 17591da177e4SLinus Torvalds return &swap_info[type]; 17601da177e4SLinus Torvalds } 17611da177e4SLinus Torvalds 17621da177e4SLinus Torvalds /* 17635d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 17641da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 17651da177e4SLinus Torvalds */ 17661da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 17671da177e4SLinus Torvalds { 17683f9e7949SHugh Dickins int our_page_cluster = page_cluster; 17693f9e7949SHugh Dickins int ret = 0, i = 1 << our_page_cluster; 17701da177e4SLinus Torvalds unsigned long toff; 17711da177e4SLinus Torvalds struct swap_info_struct *swapdev = swp_type(entry) + swap_info; 17721da177e4SLinus Torvalds 17733f9e7949SHugh Dickins if (!our_page_cluster) /* no readahead */ 17741da177e4SLinus Torvalds return 0; 17753f9e7949SHugh Dickins toff = (swp_offset(entry) >> our_page_cluster) << our_page_cluster; 17761da177e4SLinus Torvalds if (!toff) /* first page is swap header */ 17771da177e4SLinus Torvalds toff++, i--; 17781da177e4SLinus Torvalds *offset = toff; 17791da177e4SLinus Torvalds 17805d337b91SHugh Dickins spin_lock(&swap_lock); 17811da177e4SLinus Torvalds do { 17821da177e4SLinus Torvalds /* Don't read-ahead past the end of the swap area */ 17831da177e4SLinus Torvalds if (toff >= swapdev->max) 17841da177e4SLinus Torvalds break; 17851da177e4SLinus Torvalds /* Don't read in free or bad pages */ 17861da177e4SLinus Torvalds if (!swapdev->swap_map[toff]) 17871da177e4SLinus Torvalds break; 17881da177e4SLinus Torvalds if (swapdev->swap_map[toff] == SWAP_MAP_BAD) 17891da177e4SLinus Torvalds break; 17901da177e4SLinus Torvalds toff++; 17911da177e4SLinus Torvalds ret++; 17921da177e4SLinus Torvalds } while (--i); 17935d337b91SHugh Dickins spin_unlock(&swap_lock); 17941da177e4SLinus Torvalds return ret; 17951da177e4SLinus Torvalds } 1796