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> 308a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include <asm/pgtable.h> 331da177e4SLinus Torvalds #include <asm/tlbflush.h> 341da177e4SLinus Torvalds #include <linux/swapops.h> 351da177e4SLinus Torvalds 365d337b91SHugh Dickins DEFINE_SPINLOCK(swap_lock); 371da177e4SLinus Torvalds unsigned int nr_swapfiles; 381da177e4SLinus Torvalds long total_swap_pages; 391da177e4SLinus Torvalds static int swap_overflow; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 421da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 431da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 441da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds struct swap_list_t swap_list = {-1, -1}; 471da177e4SLinus Torvalds 48f577eb30SRafael J. Wysocki static struct swap_info_struct swap_info[MAX_SWAPFILES]; 491da177e4SLinus Torvalds 50fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * We need this because the bdev->unplug_fn can sleep and we cannot 545d337b91SHugh Dickins * hold swap_lock while calling the unplug_fn. And swap_lock 55fc0abb14SIngo Molnar * cannot be turned into a mutex. 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds static DECLARE_RWSEM(swap_unplug_sem); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds swp_entry_t entry; 621da177e4SLinus Torvalds 631da177e4SLinus Torvalds down_read(&swap_unplug_sem); 644c21e2f2SHugh Dickins entry.val = page_private(page); 651da177e4SLinus Torvalds if (PageSwapCache(page)) { 661da177e4SLinus Torvalds struct block_device *bdev = swap_info[swp_type(entry)].bdev; 671da177e4SLinus Torvalds struct backing_dev_info *bdi; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* 701da177e4SLinus Torvalds * If the page is removed from swapcache from under us (with a 711da177e4SLinus Torvalds * racy try_to_unuse/swapoff) we need an additional reference 724c21e2f2SHugh Dickins * count to avoid reading garbage from page_private(page) above. 734c21e2f2SHugh Dickins * If the WARN_ON triggers during a swapoff it maybe the race 741da177e4SLinus Torvalds * condition and it's harmless. However if it triggers without 751da177e4SLinus Torvalds * swapoff it signals a problem. 761da177e4SLinus Torvalds */ 771da177e4SLinus Torvalds WARN_ON(page_count(page) <= 1); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds bdi = bdev->bd_inode->i_mapping->backing_dev_info; 80ba32311eSMcMullan, Jason blk_run_backing_dev(bdi, page); 811da177e4SLinus Torvalds } 821da177e4SLinus Torvalds up_read(&swap_unplug_sem); 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 85048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 86048c27fdSHugh Dickins #define LATENCY_LIMIT 256 87048c27fdSHugh Dickins 886eb396dcSHugh Dickins static inline unsigned long scan_swap_map(struct swap_info_struct *si) 891da177e4SLinus Torvalds { 907dfad418SHugh Dickins unsigned long offset, last_in_cluster; 91048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 921da177e4SLinus Torvalds 937dfad418SHugh Dickins /* 947dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 957dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 967dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 977dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 987dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 997dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 1007dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 1011da177e4SLinus Torvalds */ 1027dfad418SHugh Dickins 10352b7efdbSHugh Dickins si->flags += SWP_SCANNING; 1047dfad418SHugh Dickins if (unlikely(!si->cluster_nr)) { 1057dfad418SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 1067dfad418SHugh Dickins if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) 1077dfad418SHugh Dickins goto lowest; 1085d337b91SHugh Dickins spin_unlock(&swap_lock); 1097dfad418SHugh Dickins 1107dfad418SHugh Dickins offset = si->lowest_bit; 1117dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 1127dfad418SHugh Dickins 1137dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 1147dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 1151da177e4SLinus Torvalds if (si->swap_map[offset]) 1167dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 1177dfad418SHugh Dickins else if (offset == last_in_cluster) { 1185d337b91SHugh Dickins spin_lock(&swap_lock); 1199b65ef59SAkinobu Mita si->cluster_next = offset-SWAPFILE_CLUSTER+1; 1207dfad418SHugh Dickins goto cluster; 1217dfad418SHugh Dickins } 122048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 123048c27fdSHugh Dickins cond_resched(); 124048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 125048c27fdSHugh Dickins } 1267dfad418SHugh Dickins } 1275d337b91SHugh Dickins spin_lock(&swap_lock); 1287dfad418SHugh Dickins goto lowest; 1297dfad418SHugh Dickins } 1307dfad418SHugh Dickins 1317dfad418SHugh Dickins si->cluster_nr--; 1327dfad418SHugh Dickins cluster: 1337dfad418SHugh Dickins offset = si->cluster_next; 1347dfad418SHugh Dickins if (offset > si->highest_bit) 1357dfad418SHugh Dickins lowest: offset = si->lowest_bit; 13652b7efdbSHugh Dickins checks: if (!(si->flags & SWP_WRITEOK)) 13752b7efdbSHugh Dickins goto no_page; 1387dfad418SHugh Dickins if (!si->highest_bit) 1397dfad418SHugh Dickins goto no_page; 1407dfad418SHugh Dickins if (!si->swap_map[offset]) { 14152b7efdbSHugh Dickins if (offset == si->lowest_bit) 1421da177e4SLinus Torvalds si->lowest_bit++; 1431da177e4SLinus Torvalds if (offset == si->highest_bit) 1441da177e4SLinus Torvalds si->highest_bit--; 1457dfad418SHugh Dickins si->inuse_pages++; 1467dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 1471da177e4SLinus Torvalds si->lowest_bit = si->max; 1481da177e4SLinus Torvalds si->highest_bit = 0; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds si->swap_map[offset] = 1; 1511da177e4SLinus Torvalds si->cluster_next = offset + 1; 15252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 1531da177e4SLinus Torvalds return offset; 1541da177e4SLinus Torvalds } 1557dfad418SHugh Dickins 1565d337b91SHugh Dickins spin_unlock(&swap_lock); 1577dfad418SHugh Dickins while (++offset <= si->highest_bit) { 15852b7efdbSHugh Dickins if (!si->swap_map[offset]) { 1595d337b91SHugh Dickins spin_lock(&swap_lock); 16052b7efdbSHugh Dickins goto checks; 1617dfad418SHugh Dickins } 162048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 163048c27fdSHugh Dickins cond_resched(); 164048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 165048c27fdSHugh Dickins } 16652b7efdbSHugh Dickins } 1675d337b91SHugh Dickins spin_lock(&swap_lock); 1687dfad418SHugh Dickins goto lowest; 1697dfad418SHugh Dickins 1707dfad418SHugh Dickins no_page: 17152b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 1721da177e4SLinus Torvalds return 0; 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 1761da177e4SLinus Torvalds { 177fb4f88dcSHugh Dickins struct swap_info_struct *si; 178fb4f88dcSHugh Dickins pgoff_t offset; 179fb4f88dcSHugh Dickins int type, next; 180fb4f88dcSHugh Dickins int wrapped = 0; 1811da177e4SLinus Torvalds 1825d337b91SHugh Dickins spin_lock(&swap_lock); 1831da177e4SLinus Torvalds if (nr_swap_pages <= 0) 184fb4f88dcSHugh Dickins goto noswap; 185fb4f88dcSHugh Dickins nr_swap_pages--; 1861da177e4SLinus Torvalds 187fb4f88dcSHugh Dickins for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { 188fb4f88dcSHugh Dickins si = swap_info + type; 189fb4f88dcSHugh Dickins next = si->next; 190fb4f88dcSHugh Dickins if (next < 0 || 191fb4f88dcSHugh Dickins (!wrapped && si->prio != swap_info[next].prio)) { 192fb4f88dcSHugh Dickins next = swap_list.head; 193fb4f88dcSHugh Dickins wrapped++; 1941da177e4SLinus Torvalds } 195fb4f88dcSHugh Dickins 196fb4f88dcSHugh Dickins if (!si->highest_bit) 197fb4f88dcSHugh Dickins continue; 198fb4f88dcSHugh Dickins if (!(si->flags & SWP_WRITEOK)) 199fb4f88dcSHugh Dickins continue; 200fb4f88dcSHugh Dickins 201fb4f88dcSHugh Dickins swap_list.next = next; 202fb4f88dcSHugh Dickins offset = scan_swap_map(si); 2035d337b91SHugh Dickins if (offset) { 2045d337b91SHugh Dickins spin_unlock(&swap_lock); 205fb4f88dcSHugh Dickins return swp_entry(type, offset); 2065d337b91SHugh Dickins } 207fb4f88dcSHugh Dickins next = swap_list.next; 208fb4f88dcSHugh Dickins } 209fb4f88dcSHugh Dickins 210fb4f88dcSHugh Dickins nr_swap_pages++; 211fb4f88dcSHugh Dickins noswap: 2125d337b91SHugh Dickins spin_unlock(&swap_lock); 213fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2163a291a20SRafael J. Wysocki swp_entry_t get_swap_page_of_type(int type) 2173a291a20SRafael J. Wysocki { 2183a291a20SRafael J. Wysocki struct swap_info_struct *si; 2193a291a20SRafael J. Wysocki pgoff_t offset; 2203a291a20SRafael J. Wysocki 2213a291a20SRafael J. Wysocki spin_lock(&swap_lock); 2223a291a20SRafael J. Wysocki si = swap_info + type; 2233a291a20SRafael J. Wysocki if (si->flags & SWP_WRITEOK) { 2243a291a20SRafael J. Wysocki nr_swap_pages--; 2253a291a20SRafael J. Wysocki offset = scan_swap_map(si); 2263a291a20SRafael J. Wysocki if (offset) { 2273a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 2283a291a20SRafael J. Wysocki return swp_entry(type, offset); 2293a291a20SRafael J. Wysocki } 2303a291a20SRafael J. Wysocki nr_swap_pages++; 2313a291a20SRafael J. Wysocki } 2323a291a20SRafael J. Wysocki spin_unlock(&swap_lock); 2333a291a20SRafael J. Wysocki return (swp_entry_t) {0}; 2343a291a20SRafael J. Wysocki } 2353a291a20SRafael J. Wysocki 2361da177e4SLinus Torvalds static struct swap_info_struct * swap_info_get(swp_entry_t entry) 2371da177e4SLinus Torvalds { 2381da177e4SLinus Torvalds struct swap_info_struct * p; 2391da177e4SLinus Torvalds unsigned long offset, type; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds if (!entry.val) 2421da177e4SLinus Torvalds goto out; 2431da177e4SLinus Torvalds type = swp_type(entry); 2441da177e4SLinus Torvalds if (type >= nr_swapfiles) 2451da177e4SLinus Torvalds goto bad_nofile; 2461da177e4SLinus Torvalds p = & swap_info[type]; 2471da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 2481da177e4SLinus Torvalds goto bad_device; 2491da177e4SLinus Torvalds offset = swp_offset(entry); 2501da177e4SLinus Torvalds if (offset >= p->max) 2511da177e4SLinus Torvalds goto bad_offset; 2521da177e4SLinus Torvalds if (!p->swap_map[offset]) 2531da177e4SLinus Torvalds goto bad_free; 2545d337b91SHugh Dickins spin_lock(&swap_lock); 2551da177e4SLinus Torvalds return p; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds bad_free: 2581da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); 2591da177e4SLinus Torvalds goto out; 2601da177e4SLinus Torvalds bad_offset: 2611da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); 2621da177e4SLinus Torvalds goto out; 2631da177e4SLinus Torvalds bad_device: 2641da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); 2651da177e4SLinus Torvalds goto out; 2661da177e4SLinus Torvalds bad_nofile: 2671da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); 2681da177e4SLinus Torvalds out: 2691da177e4SLinus Torvalds return NULL; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds static int swap_entry_free(struct swap_info_struct *p, unsigned long offset) 2731da177e4SLinus Torvalds { 2741da177e4SLinus Torvalds int count = p->swap_map[offset]; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds if (count < SWAP_MAP_MAX) { 2771da177e4SLinus Torvalds count--; 2781da177e4SLinus Torvalds p->swap_map[offset] = count; 2791da177e4SLinus Torvalds if (!count) { 2801da177e4SLinus Torvalds if (offset < p->lowest_bit) 2811da177e4SLinus Torvalds p->lowest_bit = offset; 2821da177e4SLinus Torvalds if (offset > p->highest_bit) 2831da177e4SLinus Torvalds p->highest_bit = offset; 28489d09a2cSHugh Dickins if (p->prio > swap_info[swap_list.next].prio) 28589d09a2cSHugh Dickins swap_list.next = p - swap_info; 2861da177e4SLinus Torvalds nr_swap_pages++; 2871da177e4SLinus Torvalds p->inuse_pages--; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds return count; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* 2941da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 2951da177e4SLinus Torvalds * is still around or has not been recycled. 2961da177e4SLinus Torvalds */ 2971da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct swap_info_struct * p; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds p = swap_info_get(entry); 3021da177e4SLinus Torvalds if (p) { 3031da177e4SLinus Torvalds swap_entry_free(p, swp_offset(entry)); 3045d337b91SHugh Dickins spin_unlock(&swap_lock); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /* 309c475a8abSHugh Dickins * How many references to page are currently swapped out? 3101da177e4SLinus Torvalds */ 311c475a8abSHugh Dickins static inline int page_swapcount(struct page *page) 3121da177e4SLinus Torvalds { 313c475a8abSHugh Dickins int count = 0; 3141da177e4SLinus Torvalds struct swap_info_struct *p; 3151da177e4SLinus Torvalds swp_entry_t entry; 3161da177e4SLinus Torvalds 3174c21e2f2SHugh Dickins entry.val = page_private(page); 3181da177e4SLinus Torvalds p = swap_info_get(entry); 3191da177e4SLinus Torvalds if (p) { 320c475a8abSHugh Dickins /* Subtract the 1 for the swap cache itself */ 321c475a8abSHugh Dickins count = p->swap_map[swp_offset(entry)] - 1; 3225d337b91SHugh Dickins spin_unlock(&swap_lock); 3231da177e4SLinus Torvalds } 324c475a8abSHugh Dickins return count; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* 3281da177e4SLinus Torvalds * We can use this swap cache entry directly 3291da177e4SLinus Torvalds * if there are no other references to it. 3301da177e4SLinus Torvalds */ 3311da177e4SLinus Torvalds int can_share_swap_page(struct page *page) 3321da177e4SLinus Torvalds { 333c475a8abSHugh Dickins int count; 3341da177e4SLinus Torvalds 335c475a8abSHugh Dickins BUG_ON(!PageLocked(page)); 336c475a8abSHugh Dickins count = page_mapcount(page); 337c475a8abSHugh Dickins if (count <= 1 && PageSwapCache(page)) 338c475a8abSHugh Dickins count += page_swapcount(page); 339c475a8abSHugh Dickins return count == 1; 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * Work out if there are any other processes sharing this 3441da177e4SLinus Torvalds * swap cache page. Free it if you can. Return success. 3451da177e4SLinus Torvalds */ 3461da177e4SLinus Torvalds int remove_exclusive_swap_page(struct page *page) 3471da177e4SLinus Torvalds { 3481da177e4SLinus Torvalds int retval; 3491da177e4SLinus Torvalds struct swap_info_struct * p; 3501da177e4SLinus Torvalds swp_entry_t entry; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 3531da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds if (!PageSwapCache(page)) 3561da177e4SLinus Torvalds return 0; 3571da177e4SLinus Torvalds if (PageWriteback(page)) 3581da177e4SLinus Torvalds return 0; 3591da177e4SLinus Torvalds if (page_count(page) != 2) /* 2: us + cache */ 3601da177e4SLinus Torvalds return 0; 3611da177e4SLinus Torvalds 3624c21e2f2SHugh Dickins entry.val = page_private(page); 3631da177e4SLinus Torvalds p = swap_info_get(entry); 3641da177e4SLinus Torvalds if (!p) 3651da177e4SLinus Torvalds return 0; 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* Is the only swap cache user the cache itself? */ 3681da177e4SLinus Torvalds retval = 0; 3691da177e4SLinus Torvalds if (p->swap_map[swp_offset(entry)] == 1) { 3701da177e4SLinus Torvalds /* Recheck the page count with the swapcache lock held.. */ 3711da177e4SLinus Torvalds write_lock_irq(&swapper_space.tree_lock); 3721da177e4SLinus Torvalds if ((page_count(page) == 2) && !PageWriteback(page)) { 3731da177e4SLinus Torvalds __delete_from_swap_cache(page); 3741da177e4SLinus Torvalds SetPageDirty(page); 3751da177e4SLinus Torvalds retval = 1; 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds write_unlock_irq(&swapper_space.tree_lock); 3781da177e4SLinus Torvalds } 3795d337b91SHugh Dickins spin_unlock(&swap_lock); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds if (retval) { 3821da177e4SLinus Torvalds swap_free(entry); 3831da177e4SLinus Torvalds page_cache_release(page); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds return retval; 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds 3891da177e4SLinus Torvalds /* 3901da177e4SLinus Torvalds * Free the swap entry like above, but also try to 3911da177e4SLinus Torvalds * free the page cache entry if it is the last user. 3921da177e4SLinus Torvalds */ 3931da177e4SLinus Torvalds void free_swap_and_cache(swp_entry_t entry) 3941da177e4SLinus Torvalds { 3951da177e4SLinus Torvalds struct swap_info_struct * p; 3961da177e4SLinus Torvalds struct page *page = NULL; 3971da177e4SLinus Torvalds 3980697212aSChristoph Lameter if (is_migration_entry(entry)) 3990697212aSChristoph Lameter return; 4000697212aSChristoph Lameter 4011da177e4SLinus Torvalds p = swap_info_get(entry); 4021da177e4SLinus Torvalds if (p) { 40393fac704SNick Piggin if (swap_entry_free(p, swp_offset(entry)) == 1) { 40493fac704SNick Piggin page = find_get_page(&swapper_space, entry.val); 40593fac704SNick Piggin if (page && unlikely(TestSetPageLocked(page))) { 40693fac704SNick Piggin page_cache_release(page); 40793fac704SNick Piggin page = NULL; 40893fac704SNick Piggin } 40993fac704SNick Piggin } 4105d337b91SHugh Dickins spin_unlock(&swap_lock); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds if (page) { 4131da177e4SLinus Torvalds int one_user; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 4161da177e4SLinus Torvalds one_user = (page_count(page) == 2); 4171da177e4SLinus Torvalds /* Only cache user (+us), or swap space full? Free it! */ 41893fac704SNick Piggin /* Also recheck PageSwapCache after page is locked (above) */ 41993fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 42093fac704SNick Piggin (one_user || vm_swap_full())) { 4211da177e4SLinus Torvalds delete_from_swap_cache(page); 4221da177e4SLinus Torvalds SetPageDirty(page); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds unlock_page(page); 4251da177e4SLinus Torvalds page_cache_release(page); 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 429b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 430f577eb30SRafael J. Wysocki /* 431915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 432f577eb30SRafael J. Wysocki * 433915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 434915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 435915bae9eSRafael J. Wysocki * 436915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 437f577eb30SRafael J. Wysocki */ 4387bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 439f577eb30SRafael J. Wysocki { 440915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 441f577eb30SRafael J. Wysocki int i; 442f577eb30SRafael J. Wysocki 443915bae9eSRafael J. Wysocki if (device) 444915bae9eSRafael J. Wysocki bdev = bdget(device); 445915bae9eSRafael J. Wysocki 446f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 447f577eb30SRafael J. Wysocki for (i = 0; i < nr_swapfiles; i++) { 448915bae9eSRafael J. Wysocki struct swap_info_struct *sis = swap_info + i; 449f577eb30SRafael J. Wysocki 450915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 451f577eb30SRafael J. Wysocki continue; 452b6b5bce3SRafael J. Wysocki 453915bae9eSRafael J. Wysocki if (!bdev) { 4547bf23687SRafael J. Wysocki if (bdev_p) 4557bf23687SRafael J. Wysocki *bdev_p = sis->bdev; 4567bf23687SRafael J. Wysocki 4576e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 4586e1819d6SRafael J. Wysocki return i; 4596e1819d6SRafael J. Wysocki } 460915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 461915bae9eSRafael J. Wysocki struct swap_extent *se; 462915bae9eSRafael J. Wysocki 463915bae9eSRafael J. Wysocki se = list_entry(sis->extent_list.next, 464915bae9eSRafael J. Wysocki struct swap_extent, list); 465915bae9eSRafael J. Wysocki if (se->start_block == offset) { 4667bf23687SRafael J. Wysocki if (bdev_p) 4677bf23687SRafael J. Wysocki *bdev_p = sis->bdev; 4687bf23687SRafael J. Wysocki 469f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 470915bae9eSRafael J. Wysocki bdput(bdev); 471f577eb30SRafael J. Wysocki return i; 472f577eb30SRafael J. Wysocki } 473f577eb30SRafael J. Wysocki } 474915bae9eSRafael J. Wysocki } 475f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 476915bae9eSRafael J. Wysocki if (bdev) 477915bae9eSRafael J. Wysocki bdput(bdev); 478915bae9eSRafael J. Wysocki 479f577eb30SRafael J. Wysocki return -ENODEV; 480f577eb30SRafael J. Wysocki } 481f577eb30SRafael J. Wysocki 482f577eb30SRafael J. Wysocki /* 483f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 484f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 485f577eb30SRafael J. Wysocki * 486f577eb30SRafael J. Wysocki * This is needed for software suspend 487f577eb30SRafael J. Wysocki */ 488f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 489f577eb30SRafael J. Wysocki { 490f577eb30SRafael J. Wysocki unsigned int n = 0; 491f577eb30SRafael J. Wysocki 492f577eb30SRafael J. Wysocki if (type < nr_swapfiles) { 493f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 494f577eb30SRafael J. Wysocki if (swap_info[type].flags & SWP_WRITEOK) { 495f577eb30SRafael J. Wysocki n = swap_info[type].pages; 496f577eb30SRafael J. Wysocki if (free) 497f577eb30SRafael J. Wysocki n -= swap_info[type].inuse_pages; 498f577eb30SRafael J. Wysocki } 499f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 500f577eb30SRafael J. Wysocki } 501f577eb30SRafael J. Wysocki return n; 502f577eb30SRafael J. Wysocki } 503f577eb30SRafael J. Wysocki #endif 504f577eb30SRafael J. Wysocki 5051da177e4SLinus Torvalds /* 50672866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 50772866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 50872866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 5091da177e4SLinus Torvalds */ 5108a9f3ccdSBalbir Singh static int unuse_pte(struct vm_area_struct *vma, pte_t *pte, 5111da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 5121da177e4SLinus Torvalds { 513*e1a1cd59SBalbir Singh if (mem_cgroup_charge(page, vma->vm_mm, GFP_KERNEL)) 5148a9f3ccdSBalbir Singh return -ENOMEM; 5158a9f3ccdSBalbir Singh 5164294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 5171da177e4SLinus Torvalds get_page(page); 5181da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 5191da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 5201da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 5211da177e4SLinus Torvalds swap_free(entry); 5221da177e4SLinus Torvalds /* 5231da177e4SLinus Torvalds * Move the page to the active list so it is not 5241da177e4SLinus Torvalds * immediately swapped out again after swapon. 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds activate_page(page); 5278a9f3ccdSBalbir Singh return 1; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 5311da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5321da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 535705e87c0SHugh Dickins pte_t *pte; 53659bd2658SHugh Dickins spinlock_t *ptl; 5378a9f3ccdSBalbir Singh int ret = 0; 5381da177e4SLinus Torvalds 53959bd2658SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 5401da177e4SLinus Torvalds do { 5411da177e4SLinus Torvalds /* 5421da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 5431da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 5441da177e4SLinus Torvalds */ 5451da177e4SLinus Torvalds if (unlikely(pte_same(*pte, swp_pte))) { 5468a9f3ccdSBalbir Singh ret = unuse_pte(vma, pte++, addr, entry, page); 54759bd2658SHugh Dickins break; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 55059bd2658SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 5518a9f3ccdSBalbir Singh return ret; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 5551da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5561da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5571da177e4SLinus Torvalds { 5581da177e4SLinus Torvalds pmd_t *pmd; 5591da177e4SLinus Torvalds unsigned long next; 5608a9f3ccdSBalbir Singh int ret; 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 5631da177e4SLinus Torvalds do { 5641da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 5651da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 5661da177e4SLinus Torvalds continue; 5678a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 5688a9f3ccdSBalbir Singh if (ret) 5698a9f3ccdSBalbir Singh return ret; 5701da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 5711da177e4SLinus Torvalds return 0; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 5751da177e4SLinus Torvalds unsigned long addr, unsigned long end, 5761da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds pud_t *pud; 5791da177e4SLinus Torvalds unsigned long next; 5808a9f3ccdSBalbir Singh int ret; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 5831da177e4SLinus Torvalds do { 5841da177e4SLinus Torvalds next = pud_addr_end(addr, end); 5851da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 5861da177e4SLinus Torvalds continue; 5878a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 5888a9f3ccdSBalbir Singh if (ret) 5898a9f3ccdSBalbir Singh return ret; 5901da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 5911da177e4SLinus Torvalds return 0; 5921da177e4SLinus Torvalds } 5931da177e4SLinus Torvalds 5941da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 5951da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5961da177e4SLinus Torvalds { 5971da177e4SLinus Torvalds pgd_t *pgd; 5981da177e4SLinus Torvalds unsigned long addr, end, next; 5998a9f3ccdSBalbir Singh int ret; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (page->mapping) { 6021da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 6031da177e4SLinus Torvalds if (addr == -EFAULT) 6041da177e4SLinus Torvalds return 0; 6051da177e4SLinus Torvalds else 6061da177e4SLinus Torvalds end = addr + PAGE_SIZE; 6071da177e4SLinus Torvalds } else { 6081da177e4SLinus Torvalds addr = vma->vm_start; 6091da177e4SLinus Torvalds end = vma->vm_end; 6101da177e4SLinus Torvalds } 6111da177e4SLinus Torvalds 6121da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 6131da177e4SLinus Torvalds do { 6141da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 6151da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 6161da177e4SLinus Torvalds continue; 6178a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 6188a9f3ccdSBalbir Singh if (ret) 6198a9f3ccdSBalbir Singh return ret; 6201da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 6211da177e4SLinus Torvalds return 0; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 6251da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 6261da177e4SLinus Torvalds { 6271da177e4SLinus Torvalds struct vm_area_struct *vma; 6288a9f3ccdSBalbir Singh int ret = 0; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 6311da177e4SLinus Torvalds /* 632c475a8abSHugh Dickins * Activate page so shrink_cache is unlikely to unmap its 633c475a8abSHugh Dickins * ptes while lock is dropped, so swapoff can make progress. 6341da177e4SLinus Torvalds */ 635c475a8abSHugh Dickins activate_page(page); 6361da177e4SLinus Torvalds unlock_page(page); 6371da177e4SLinus Torvalds down_read(&mm->mmap_sem); 6381da177e4SLinus Torvalds lock_page(page); 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 6418a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 6421da177e4SLinus Torvalds break; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds up_read(&mm->mmap_sem); 6458a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 6461da177e4SLinus Torvalds } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* 6491da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 6501da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 6511da177e4SLinus Torvalds */ 6526eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 6536eb396dcSHugh Dickins unsigned int prev) 6541da177e4SLinus Torvalds { 6556eb396dcSHugh Dickins unsigned int max = si->max; 6566eb396dcSHugh Dickins unsigned int i = prev; 6571da177e4SLinus Torvalds int count; 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds /* 6605d337b91SHugh Dickins * No need for swap_lock here: we're just looking 6611da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 6621da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 6635d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 6641da177e4SLinus Torvalds */ 6651da177e4SLinus Torvalds for (;;) { 6661da177e4SLinus Torvalds if (++i >= max) { 6671da177e4SLinus Torvalds if (!prev) { 6681da177e4SLinus Torvalds i = 0; 6691da177e4SLinus Torvalds break; 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds /* 6721da177e4SLinus Torvalds * No entries in use at top of swap_map, 6731da177e4SLinus Torvalds * loop back to start and recheck there. 6741da177e4SLinus Torvalds */ 6751da177e4SLinus Torvalds max = prev + 1; 6761da177e4SLinus Torvalds prev = 0; 6771da177e4SLinus Torvalds i = 1; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds count = si->swap_map[i]; 6801da177e4SLinus Torvalds if (count && count != SWAP_MAP_BAD) 6811da177e4SLinus Torvalds break; 6821da177e4SLinus Torvalds } 6831da177e4SLinus Torvalds return i; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /* 6871da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 6881da177e4SLinus Torvalds * and then search for the process using it. All the necessary 6891da177e4SLinus Torvalds * page table adjustments can then be made atomically. 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds struct swap_info_struct * si = &swap_info[type]; 6941da177e4SLinus Torvalds struct mm_struct *start_mm; 6951da177e4SLinus Torvalds unsigned short *swap_map; 6961da177e4SLinus Torvalds unsigned short swcount; 6971da177e4SLinus Torvalds struct page *page; 6981da177e4SLinus Torvalds swp_entry_t entry; 6996eb396dcSHugh Dickins unsigned int i = 0; 7001da177e4SLinus Torvalds int retval = 0; 7011da177e4SLinus Torvalds int reset_overflow = 0; 7021da177e4SLinus Torvalds int shmem; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* 7051da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 7061da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 7071da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 7081da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 7091da177e4SLinus Torvalds * 7101da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 7111da177e4SLinus Torvalds * freed the previous entry from; but that would take less 7121da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 7131da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 7141da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 7151da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 7161da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 7171da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds start_mm = &init_mm; 7201da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /* 7231da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 7241da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 7251da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 7261da177e4SLinus Torvalds */ 7271da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 7281da177e4SLinus Torvalds if (signal_pending(current)) { 7291da177e4SLinus Torvalds retval = -EINTR; 7301da177e4SLinus Torvalds break; 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* 7341da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 7351da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 7361da177e4SLinus Torvalds * page and read the swap into it. 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 7391da177e4SLinus Torvalds entry = swp_entry(type, i); 74002098feaSHugh Dickins page = read_swap_cache_async(entry, 74102098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 7421da177e4SLinus Torvalds if (!page) { 7431da177e4SLinus Torvalds /* 7441da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 7451da177e4SLinus Torvalds * has been freed independently, and will not be 7461da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 7471da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 7481da177e4SLinus Torvalds */ 7491da177e4SLinus Torvalds if (!*swap_map) 7501da177e4SLinus Torvalds continue; 7511da177e4SLinus Torvalds retval = -ENOMEM; 7521da177e4SLinus Torvalds break; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds /* 7561da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 7571da177e4SLinus Torvalds */ 7581da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 7591da177e4SLinus Torvalds mmput(start_mm); 7601da177e4SLinus Torvalds start_mm = &init_mm; 7611da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds /* 7651da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 7661da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 7671da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 7681da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 7691da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 7701da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds wait_on_page_locked(page); 7731da177e4SLinus Torvalds wait_on_page_writeback(page); 7741da177e4SLinus Torvalds lock_page(page); 7751da177e4SLinus Torvalds wait_on_page_writeback(page); 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds /* 7781da177e4SLinus Torvalds * Remove all references to entry. 7791da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 7801da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds shmem = 0; 7831da177e4SLinus Torvalds swcount = *swap_map; 7841da177e4SLinus Torvalds if (swcount > 1) { 7851da177e4SLinus Torvalds if (start_mm == &init_mm) 7861da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 7871da177e4SLinus Torvalds else 7881da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 7891da177e4SLinus Torvalds } 7901da177e4SLinus Torvalds if (*swap_map > 1) { 7911da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 7921da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 7931da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 7941da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 7951da177e4SLinus Torvalds struct mm_struct *mm; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 7981da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 7991da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8002e0e26c7SHugh Dickins while (*swap_map > 1 && !retval && !shmem && 8011da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 8021da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 80370af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 8041da177e4SLinus Torvalds continue; 8051da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8061da177e4SLinus Torvalds mmput(prev_mm); 8071da177e4SLinus Torvalds prev_mm = mm; 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds cond_resched(); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds swcount = *swap_map; 8121da177e4SLinus Torvalds if (swcount <= 1) 8131da177e4SLinus Torvalds ; 8141da177e4SLinus Torvalds else if (mm == &init_mm) { 8151da177e4SLinus Torvalds set_start_mm = 1; 8161da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 8171da177e4SLinus Torvalds } else 8181da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 8191da177e4SLinus Torvalds if (set_start_mm && *swap_map < swcount) { 8201da177e4SLinus Torvalds mmput(new_start_mm); 8211da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 8221da177e4SLinus Torvalds new_start_mm = mm; 8231da177e4SLinus Torvalds set_start_mm = 0; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8281da177e4SLinus Torvalds mmput(prev_mm); 8291da177e4SLinus Torvalds mmput(start_mm); 8301da177e4SLinus Torvalds start_mm = new_start_mm; 8311da177e4SLinus Torvalds } 8322e0e26c7SHugh Dickins if (shmem) { 8332e0e26c7SHugh Dickins /* page has already been unlocked and released */ 8342e0e26c7SHugh Dickins if (shmem > 0) 8352e0e26c7SHugh Dickins continue; 8362e0e26c7SHugh Dickins retval = shmem; 8372e0e26c7SHugh Dickins break; 8382e0e26c7SHugh Dickins } 8391da177e4SLinus Torvalds if (retval) { 8401da177e4SLinus Torvalds unlock_page(page); 8411da177e4SLinus Torvalds page_cache_release(page); 8421da177e4SLinus Torvalds break; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds /* 8461da177e4SLinus Torvalds * How could swap count reach 0x7fff when the maximum 8471da177e4SLinus Torvalds * pid is 0x7fff, and there's no way to repeat a swap 8481da177e4SLinus Torvalds * page within an mm (except in shmem, where it's the 8491da177e4SLinus Torvalds * shared object which takes the reference count)? 8501da177e4SLinus Torvalds * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. 8511da177e4SLinus Torvalds * 8521da177e4SLinus Torvalds * If that's wrong, then we should worry more about 8531da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 8541da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 8551da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 8561da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 8571da177e4SLinus Torvalds */ 8581da177e4SLinus Torvalds if (*swap_map == SWAP_MAP_MAX) { 8595d337b91SHugh Dickins spin_lock(&swap_lock); 8601da177e4SLinus Torvalds *swap_map = 1; 8615d337b91SHugh Dickins spin_unlock(&swap_lock); 8621da177e4SLinus Torvalds reset_overflow = 1; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /* 8661da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 8671da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 8681da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 8691da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 8701da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 8711da177e4SLinus Torvalds * delete from cache even if there's another reference, 8721da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 8731da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 8741da177e4SLinus Torvalds * read from disk into another page. Splitting into two 8751da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 8761da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 8771da177e4SLinus Torvalds */ 8781da177e4SLinus Torvalds if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { 8791da177e4SLinus Torvalds struct writeback_control wbc = { 8801da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 8811da177e4SLinus Torvalds }; 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds swap_writepage(page, &wbc); 8841da177e4SLinus Torvalds lock_page(page); 8851da177e4SLinus Torvalds wait_on_page_writeback(page); 8861da177e4SLinus Torvalds } 8872e0e26c7SHugh Dickins if (PageSwapCache(page)) 8881da177e4SLinus Torvalds delete_from_swap_cache(page); 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds /* 8911da177e4SLinus Torvalds * So we could skip searching mms once swap count went 8921da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 8932706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds SetPageDirty(page); 8961da177e4SLinus Torvalds unlock_page(page); 8971da177e4SLinus Torvalds page_cache_release(page); 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* 9001da177e4SLinus Torvalds * Make sure that we aren't completely killing 9011da177e4SLinus Torvalds * interactive performance. 9021da177e4SLinus Torvalds */ 9031da177e4SLinus Torvalds cond_resched(); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds mmput(start_mm); 9071da177e4SLinus Torvalds if (reset_overflow) { 9081da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 9091da177e4SLinus Torvalds swap_overflow = 0; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds return retval; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* 9155d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 9165d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 9175d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 9181da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 9191da177e4SLinus Torvalds */ 9201da177e4SLinus Torvalds static void drain_mmlist(void) 9211da177e4SLinus Torvalds { 9221da177e4SLinus Torvalds struct list_head *p, *next; 9231da177e4SLinus Torvalds unsigned int i; 9241da177e4SLinus Torvalds 9251da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) 9261da177e4SLinus Torvalds if (swap_info[i].inuse_pages) 9271da177e4SLinus Torvalds return; 9281da177e4SLinus Torvalds spin_lock(&mmlist_lock); 9291da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 9301da177e4SLinus Torvalds list_del_init(p); 9311da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 9361da177e4SLinus Torvalds * corresponds to page offset `offset'. 9371da177e4SLinus Torvalds */ 9381da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) 9391da177e4SLinus Torvalds { 9401da177e4SLinus Torvalds struct swap_extent *se = sis->curr_swap_extent; 9411da177e4SLinus Torvalds struct swap_extent *start_se = se; 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds for ( ; ; ) { 9441da177e4SLinus Torvalds struct list_head *lh; 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds if (se->start_page <= offset && 9471da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 9481da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 9491da177e4SLinus Torvalds } 95011d31886SHugh Dickins lh = se->list.next; 9511da177e4SLinus Torvalds if (lh == &sis->extent_list) 95211d31886SHugh Dickins lh = lh->next; 9531da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 9541da177e4SLinus Torvalds sis->curr_swap_extent = se; 9551da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 959b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 9603aef83e0SRafael J. Wysocki /* 9613aef83e0SRafael J. Wysocki * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 9623aef83e0SRafael J. Wysocki * corresponding to given index in swap_info (swap type). 9633aef83e0SRafael J. Wysocki */ 9643aef83e0SRafael J. Wysocki sector_t swapdev_block(int swap_type, pgoff_t offset) 9653aef83e0SRafael J. Wysocki { 9663aef83e0SRafael J. Wysocki struct swap_info_struct *sis; 9673aef83e0SRafael J. Wysocki 9683aef83e0SRafael J. Wysocki if (swap_type >= nr_swapfiles) 9693aef83e0SRafael J. Wysocki return 0; 9703aef83e0SRafael J. Wysocki 9713aef83e0SRafael J. Wysocki sis = swap_info + swap_type; 9723aef83e0SRafael J. Wysocki return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0; 9733aef83e0SRafael J. Wysocki } 974b0cb1a19SRafael J. Wysocki #endif /* CONFIG_HIBERNATION */ 9753aef83e0SRafael J. Wysocki 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * Free all of a swapdev's extent information 9781da177e4SLinus Torvalds */ 9791da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 9801da177e4SLinus Torvalds { 9811da177e4SLinus Torvalds while (!list_empty(&sis->extent_list)) { 9821da177e4SLinus Torvalds struct swap_extent *se; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds se = list_entry(sis->extent_list.next, 9851da177e4SLinus Torvalds struct swap_extent, list); 9861da177e4SLinus Torvalds list_del(&se->list); 9871da177e4SLinus Torvalds kfree(se); 9881da177e4SLinus Torvalds } 9891da177e4SLinus Torvalds } 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds /* 9921da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 99311d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 9941da177e4SLinus Torvalds * 99511d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 9961da177e4SLinus Torvalds */ 9971da177e4SLinus Torvalds static int 9981da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 9991da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 10001da177e4SLinus Torvalds { 10011da177e4SLinus Torvalds struct swap_extent *se; 10021da177e4SLinus Torvalds struct swap_extent *new_se; 10031da177e4SLinus Torvalds struct list_head *lh; 10041da177e4SLinus Torvalds 100511d31886SHugh Dickins lh = sis->extent_list.prev; /* The highest page extent */ 100611d31886SHugh Dickins if (lh != &sis->extent_list) { 10071da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 100811d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 100911d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 10101da177e4SLinus Torvalds /* Merge it */ 10111da177e4SLinus Torvalds se->nr_pages += nr_pages; 10121da177e4SLinus Torvalds return 0; 10131da177e4SLinus Torvalds } 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds /* 10171da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 10181da177e4SLinus Torvalds */ 10191da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 10201da177e4SLinus Torvalds if (new_se == NULL) 10211da177e4SLinus Torvalds return -ENOMEM; 10221da177e4SLinus Torvalds new_se->start_page = start_page; 10231da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 10241da177e4SLinus Torvalds new_se->start_block = start_block; 10251da177e4SLinus Torvalds 102611d31886SHugh Dickins list_add_tail(&new_se->list, &sis->extent_list); 102753092a74SHugh Dickins return 1; 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* 10311da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 10321da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 10331da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 10341da177e4SLinus Torvalds * time for locating where on disk a page belongs. 10351da177e4SLinus Torvalds * 10361da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 10371da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 10381da177e4SLinus Torvalds * swap files identically. 10391da177e4SLinus Torvalds * 10401da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 10411da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 10421da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 10431da177e4SLinus Torvalds * 10441da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 10451da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 10461da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 10471da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 10481da177e4SLinus Torvalds * for swapping. 10491da177e4SLinus Torvalds * 1050b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 10511da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 10521da177e4SLinus Torvalds * which will scribble on the fs. 10531da177e4SLinus Torvalds * 10541da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 10551da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 10561da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 10571da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 10581da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 10591da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 10601da177e4SLinus Torvalds */ 106153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 10621da177e4SLinus Torvalds { 10631da177e4SLinus Torvalds struct inode *inode; 10641da177e4SLinus Torvalds unsigned blocks_per_page; 10651da177e4SLinus Torvalds unsigned long page_no; 10661da177e4SLinus Torvalds unsigned blkbits; 10671da177e4SLinus Torvalds sector_t probe_block; 10681da177e4SLinus Torvalds sector_t last_block; 106953092a74SHugh Dickins sector_t lowest_block = -1; 107053092a74SHugh Dickins sector_t highest_block = 0; 107153092a74SHugh Dickins int nr_extents = 0; 10721da177e4SLinus Torvalds int ret; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 10751da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 10761da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 107753092a74SHugh Dickins *span = sis->pages; 10781da177e4SLinus Torvalds goto done; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds blkbits = inode->i_blkbits; 10821da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds /* 10851da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 10861da177e4SLinus Torvalds * to be very smart. 10871da177e4SLinus Torvalds */ 10881da177e4SLinus Torvalds probe_block = 0; 10891da177e4SLinus Torvalds page_no = 0; 10901da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 10911da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 10921da177e4SLinus Torvalds page_no < sis->max) { 10931da177e4SLinus Torvalds unsigned block_in_page; 10941da177e4SLinus Torvalds sector_t first_block; 10951da177e4SLinus Torvalds 10961da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 10971da177e4SLinus Torvalds if (first_block == 0) 10981da177e4SLinus Torvalds goto bad_bmap; 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds /* 11011da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 11021da177e4SLinus Torvalds */ 11031da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 11041da177e4SLinus Torvalds probe_block++; 11051da177e4SLinus Torvalds goto reprobe; 11061da177e4SLinus Torvalds } 11071da177e4SLinus Torvalds 11081da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 11091da177e4SLinus Torvalds block_in_page++) { 11101da177e4SLinus Torvalds sector_t block; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 11131da177e4SLinus Torvalds if (block == 0) 11141da177e4SLinus Torvalds goto bad_bmap; 11151da177e4SLinus Torvalds if (block != first_block + block_in_page) { 11161da177e4SLinus Torvalds /* Discontiguity */ 11171da177e4SLinus Torvalds probe_block++; 11181da177e4SLinus Torvalds goto reprobe; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 112253092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 112353092a74SHugh Dickins if (page_no) { /* exclude the header page */ 112453092a74SHugh Dickins if (first_block < lowest_block) 112553092a74SHugh Dickins lowest_block = first_block; 112653092a74SHugh Dickins if (first_block > highest_block) 112753092a74SHugh Dickins highest_block = first_block; 112853092a74SHugh Dickins } 112953092a74SHugh Dickins 11301da177e4SLinus Torvalds /* 11311da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 11321da177e4SLinus Torvalds */ 113353092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 113453092a74SHugh Dickins if (ret < 0) 11351da177e4SLinus Torvalds goto out; 113653092a74SHugh Dickins nr_extents += ret; 11371da177e4SLinus Torvalds page_no++; 11381da177e4SLinus Torvalds probe_block += blocks_per_page; 11391da177e4SLinus Torvalds reprobe: 11401da177e4SLinus Torvalds continue; 11411da177e4SLinus Torvalds } 114253092a74SHugh Dickins ret = nr_extents; 114353092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 11441da177e4SLinus Torvalds if (page_no == 0) 1145e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 11461da177e4SLinus Torvalds sis->max = page_no; 1147e2244ec2SHugh Dickins sis->pages = page_no - 1; 11481da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 11491da177e4SLinus Torvalds done: 11501da177e4SLinus Torvalds sis->curr_swap_extent = list_entry(sis->extent_list.prev, 11511da177e4SLinus Torvalds struct swap_extent, list); 11521da177e4SLinus Torvalds goto out; 11531da177e4SLinus Torvalds bad_bmap: 11541da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 11551da177e4SLinus Torvalds ret = -EINVAL; 11561da177e4SLinus Torvalds out: 11571da177e4SLinus Torvalds return ret; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds #if 0 /* We don't need this yet */ 11611da177e4SLinus Torvalds #include <linux/backing-dev.h> 11621da177e4SLinus Torvalds int page_queue_congested(struct page *page) 11631da177e4SLinus Torvalds { 11641da177e4SLinus Torvalds struct backing_dev_info *bdi; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds if (PageSwapCache(page)) { 11694c21e2f2SHugh Dickins swp_entry_t entry = { .val = page_private(page) }; 11701da177e4SLinus Torvalds struct swap_info_struct *sis; 11711da177e4SLinus Torvalds 11721da177e4SLinus Torvalds sis = get_swap_info_struct(swp_type(entry)); 11731da177e4SLinus Torvalds bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; 11741da177e4SLinus Torvalds } else 11751da177e4SLinus Torvalds bdi = page->mapping->backing_dev_info; 11761da177e4SLinus Torvalds return bdi_write_congested(bdi); 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds #endif 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile) 11811da177e4SLinus Torvalds { 11821da177e4SLinus Torvalds struct swap_info_struct * p = NULL; 11831da177e4SLinus Torvalds unsigned short *swap_map; 11841da177e4SLinus Torvalds struct file *swap_file, *victim; 11851da177e4SLinus Torvalds struct address_space *mapping; 11861da177e4SLinus Torvalds struct inode *inode; 11871da177e4SLinus Torvalds char * pathname; 11881da177e4SLinus Torvalds int i, type, prev; 11891da177e4SLinus Torvalds int err; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 11921da177e4SLinus Torvalds return -EPERM; 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds pathname = getname(specialfile); 11951da177e4SLinus Torvalds err = PTR_ERR(pathname); 11961da177e4SLinus Torvalds if (IS_ERR(pathname)) 11971da177e4SLinus Torvalds goto out; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 12001da177e4SLinus Torvalds putname(pathname); 12011da177e4SLinus Torvalds err = PTR_ERR(victim); 12021da177e4SLinus Torvalds if (IS_ERR(victim)) 12031da177e4SLinus Torvalds goto out; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds mapping = victim->f_mapping; 12061da177e4SLinus Torvalds prev = -1; 12075d337b91SHugh Dickins spin_lock(&swap_lock); 12081da177e4SLinus Torvalds for (type = swap_list.head; type >= 0; type = swap_info[type].next) { 12091da177e4SLinus Torvalds p = swap_info + type; 12101da177e4SLinus Torvalds if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) { 12111da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 12121da177e4SLinus Torvalds break; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds prev = type; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds if (type < 0) { 12171da177e4SLinus Torvalds err = -EINVAL; 12185d337b91SHugh Dickins spin_unlock(&swap_lock); 12191da177e4SLinus Torvalds goto out_dput; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 12221da177e4SLinus Torvalds vm_unacct_memory(p->pages); 12231da177e4SLinus Torvalds else { 12241da177e4SLinus Torvalds err = -ENOMEM; 12255d337b91SHugh Dickins spin_unlock(&swap_lock); 12261da177e4SLinus Torvalds goto out_dput; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds if (prev < 0) { 12291da177e4SLinus Torvalds swap_list.head = p->next; 12301da177e4SLinus Torvalds } else { 12311da177e4SLinus Torvalds swap_info[prev].next = p->next; 12321da177e4SLinus Torvalds } 12331da177e4SLinus Torvalds if (type == swap_list.next) { 12341da177e4SLinus Torvalds /* just pick something that's safe... */ 12351da177e4SLinus Torvalds swap_list.next = swap_list.head; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds nr_swap_pages -= p->pages; 12381da177e4SLinus Torvalds total_swap_pages -= p->pages; 12391da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 12405d337b91SHugh Dickins spin_unlock(&swap_lock); 1241fb4f88dcSHugh Dickins 12421da177e4SLinus Torvalds current->flags |= PF_SWAPOFF; 12431da177e4SLinus Torvalds err = try_to_unuse(type); 12441da177e4SLinus Torvalds current->flags &= ~PF_SWAPOFF; 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (err) { 12471da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 12485d337b91SHugh Dickins spin_lock(&swap_lock); 12491da177e4SLinus Torvalds for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next) 12501da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) 12511da177e4SLinus Torvalds break; 12521da177e4SLinus Torvalds p->next = i; 12531da177e4SLinus Torvalds if (prev < 0) 12541da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 12551da177e4SLinus Torvalds else 12561da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 12571da177e4SLinus Torvalds nr_swap_pages += p->pages; 12581da177e4SLinus Torvalds total_swap_pages += p->pages; 12591da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 12605d337b91SHugh Dickins spin_unlock(&swap_lock); 12611da177e4SLinus Torvalds goto out_dput; 12621da177e4SLinus Torvalds } 126352b7efdbSHugh Dickins 126452b7efdbSHugh Dickins /* wait for any unplug function to finish */ 126552b7efdbSHugh Dickins down_write(&swap_unplug_sem); 126652b7efdbSHugh Dickins up_write(&swap_unplug_sem); 126752b7efdbSHugh Dickins 12684cd3bb10SHugh Dickins destroy_swap_extents(p); 1269fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 12705d337b91SHugh Dickins spin_lock(&swap_lock); 12711da177e4SLinus Torvalds drain_mmlist(); 12725d337b91SHugh Dickins 12735d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 12745d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 12755d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 12765d337b91SHugh Dickins spin_unlock(&swap_lock); 127713e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 12785d337b91SHugh Dickins spin_lock(&swap_lock); 12795d337b91SHugh Dickins } 12805d337b91SHugh Dickins 12811da177e4SLinus Torvalds swap_file = p->swap_file; 12821da177e4SLinus Torvalds p->swap_file = NULL; 12831da177e4SLinus Torvalds p->max = 0; 12841da177e4SLinus Torvalds swap_map = p->swap_map; 12851da177e4SLinus Torvalds p->swap_map = NULL; 12861da177e4SLinus Torvalds p->flags = 0; 12875d337b91SHugh Dickins spin_unlock(&swap_lock); 1288fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 12891da177e4SLinus Torvalds vfree(swap_map); 12901da177e4SLinus Torvalds inode = mapping->host; 12911da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 12921da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 12931da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 12941da177e4SLinus Torvalds bd_release(bdev); 12951da177e4SLinus Torvalds } else { 12961b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 12971da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 12981b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds filp_close(swap_file, NULL); 13011da177e4SLinus Torvalds err = 0; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds out_dput: 13041da177e4SLinus Torvalds filp_close(victim, NULL); 13051da177e4SLinus Torvalds out: 13061da177e4SLinus Torvalds return err; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 13101da177e4SLinus Torvalds /* iterator */ 13111da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 13121da177e4SLinus Torvalds { 13131da177e4SLinus Torvalds struct swap_info_struct *ptr = swap_info; 13141da177e4SLinus Torvalds int i; 13151da177e4SLinus Torvalds loff_t l = *pos; 13161da177e4SLinus Torvalds 1317fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 13181da177e4SLinus Torvalds 1319881e4aabSSuleiman Souhlal if (!l) 1320881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1321881e4aabSSuleiman Souhlal 13221da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++, ptr++) { 13231da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 13241da177e4SLinus Torvalds continue; 1325881e4aabSSuleiman Souhlal if (!--l) 13261da177e4SLinus Torvalds return ptr; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds return NULL; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 13331da177e4SLinus Torvalds { 1334881e4aabSSuleiman Souhlal struct swap_info_struct *ptr; 13351da177e4SLinus Torvalds struct swap_info_struct *endptr = swap_info + nr_swapfiles; 13361da177e4SLinus Torvalds 1337881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 1338881e4aabSSuleiman Souhlal ptr = swap_info; 1339881e4aabSSuleiman Souhlal else { 1340881e4aabSSuleiman Souhlal ptr = v; 1341881e4aabSSuleiman Souhlal ptr++; 1342881e4aabSSuleiman Souhlal } 1343881e4aabSSuleiman Souhlal 1344881e4aabSSuleiman Souhlal for (; ptr < endptr; ptr++) { 13451da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 13461da177e4SLinus Torvalds continue; 13471da177e4SLinus Torvalds ++*pos; 13481da177e4SLinus Torvalds return ptr; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds return NULL; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 13551da177e4SLinus Torvalds { 1356fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 13571da177e4SLinus Torvalds } 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 13601da177e4SLinus Torvalds { 13611da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 13621da177e4SLinus Torvalds struct file *file; 13631da177e4SLinus Torvalds int len; 13641da177e4SLinus Torvalds 1365881e4aabSSuleiman Souhlal if (ptr == SEQ_START_TOKEN) { 13661da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 1367881e4aabSSuleiman Souhlal return 0; 1368881e4aabSSuleiman Souhlal } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds file = ptr->swap_file; 1371d3ac7f89SJosef "Jeff" Sipek len = seq_path(swap, file->f_path.mnt, file->f_path.dentry, " \t\n\\"); 13726eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 13731da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 1374d3ac7f89SJosef "Jeff" Sipek S_ISBLK(file->f_path.dentry->d_inode->i_mode) ? 13751da177e4SLinus Torvalds "partition" : "file\t", 13761da177e4SLinus Torvalds ptr->pages << (PAGE_SHIFT - 10), 13771da177e4SLinus Torvalds ptr->inuse_pages << (PAGE_SHIFT - 10), 13781da177e4SLinus Torvalds ptr->prio); 13791da177e4SLinus Torvalds return 0; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 138215ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 13831da177e4SLinus Torvalds .start = swap_start, 13841da177e4SLinus Torvalds .next = swap_next, 13851da177e4SLinus Torvalds .stop = swap_stop, 13861da177e4SLinus Torvalds .show = swap_show 13871da177e4SLinus Torvalds }; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 13901da177e4SLinus Torvalds { 13911da177e4SLinus Torvalds return seq_open(file, &swaps_op); 13921da177e4SLinus Torvalds } 13931da177e4SLinus Torvalds 139415ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 13951da177e4SLinus Torvalds .open = swaps_open, 13961da177e4SLinus Torvalds .read = seq_read, 13971da177e4SLinus Torvalds .llseek = seq_lseek, 13981da177e4SLinus Torvalds .release = seq_release, 13991da177e4SLinus Torvalds }; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds static int __init procswaps_init(void) 14021da177e4SLinus Torvalds { 14031da177e4SLinus Torvalds struct proc_dir_entry *entry; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds entry = create_proc_entry("swaps", 0, NULL); 14061da177e4SLinus Torvalds if (entry) 14071da177e4SLinus Torvalds entry->proc_fops = &proc_swaps_operations; 14081da177e4SLinus Torvalds return 0; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds __initcall(procswaps_init); 14111da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 14151da177e4SLinus Torvalds * 14161da177e4SLinus Torvalds * The swapon system call 14171da177e4SLinus Torvalds */ 14181da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) 14191da177e4SLinus Torvalds { 14201da177e4SLinus Torvalds struct swap_info_struct * p; 14211da177e4SLinus Torvalds char *name = NULL; 14221da177e4SLinus Torvalds struct block_device *bdev = NULL; 14231da177e4SLinus Torvalds struct file *swap_file = NULL; 14241da177e4SLinus Torvalds struct address_space *mapping; 14251da177e4SLinus Torvalds unsigned int type; 14261da177e4SLinus Torvalds int i, prev; 14271da177e4SLinus Torvalds int error; 14281da177e4SLinus Torvalds static int least_priority; 14291da177e4SLinus Torvalds union swap_header *swap_header = NULL; 14301da177e4SLinus Torvalds int swap_header_version; 14316eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 14326eb396dcSHugh Dickins int nr_extents = 0; 143353092a74SHugh Dickins sector_t span; 14341da177e4SLinus Torvalds unsigned long maxpages = 1; 14351da177e4SLinus Torvalds int swapfilesize; 14361da177e4SLinus Torvalds unsigned short *swap_map; 14371da177e4SLinus Torvalds struct page *page = NULL; 14381da177e4SLinus Torvalds struct inode *inode = NULL; 14391da177e4SLinus Torvalds int did_down = 0; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 14421da177e4SLinus Torvalds return -EPERM; 14435d337b91SHugh Dickins spin_lock(&swap_lock); 14441da177e4SLinus Torvalds p = swap_info; 14451da177e4SLinus Torvalds for (type = 0 ; type < nr_swapfiles ; type++,p++) 14461da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 14471da177e4SLinus Torvalds break; 14481da177e4SLinus Torvalds error = -EPERM; 14490697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 14505d337b91SHugh Dickins spin_unlock(&swap_lock); 14511da177e4SLinus Torvalds goto out; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds if (type >= nr_swapfiles) 14541da177e4SLinus Torvalds nr_swapfiles = type+1; 14551da177e4SLinus Torvalds INIT_LIST_HEAD(&p->extent_list); 14561da177e4SLinus Torvalds p->flags = SWP_USED; 14571da177e4SLinus Torvalds p->swap_file = NULL; 14581da177e4SLinus Torvalds p->old_block_size = 0; 14591da177e4SLinus Torvalds p->swap_map = NULL; 14601da177e4SLinus Torvalds p->lowest_bit = 0; 14611da177e4SLinus Torvalds p->highest_bit = 0; 14621da177e4SLinus Torvalds p->cluster_nr = 0; 14631da177e4SLinus Torvalds p->inuse_pages = 0; 14641da177e4SLinus Torvalds p->next = -1; 14651da177e4SLinus Torvalds if (swap_flags & SWAP_FLAG_PREFER) { 14661da177e4SLinus Torvalds p->prio = 14671da177e4SLinus Torvalds (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT; 14681da177e4SLinus Torvalds } else { 14691da177e4SLinus Torvalds p->prio = --least_priority; 14701da177e4SLinus Torvalds } 14715d337b91SHugh Dickins spin_unlock(&swap_lock); 14721da177e4SLinus Torvalds name = getname(specialfile); 14731da177e4SLinus Torvalds error = PTR_ERR(name); 14741da177e4SLinus Torvalds if (IS_ERR(name)) { 14751da177e4SLinus Torvalds name = NULL; 14761da177e4SLinus Torvalds goto bad_swap_2; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 14791da177e4SLinus Torvalds error = PTR_ERR(swap_file); 14801da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 14811da177e4SLinus Torvalds swap_file = NULL; 14821da177e4SLinus Torvalds goto bad_swap_2; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds p->swap_file = swap_file; 14861da177e4SLinus Torvalds mapping = swap_file->f_mapping; 14871da177e4SLinus Torvalds inode = mapping->host; 14881da177e4SLinus Torvalds 14891da177e4SLinus Torvalds error = -EBUSY; 14901da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 14911da177e4SLinus Torvalds struct swap_info_struct *q = &swap_info[i]; 14921da177e4SLinus Torvalds 14931da177e4SLinus Torvalds if (i == type || !q->swap_file) 14941da177e4SLinus Torvalds continue; 14951da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 14961da177e4SLinus Torvalds goto bad_swap; 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds error = -EINVAL; 15001da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 15011da177e4SLinus Torvalds bdev = I_BDEV(inode); 15021da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 15031da177e4SLinus Torvalds if (error < 0) { 15041da177e4SLinus Torvalds bdev = NULL; 1505f7b3a435SRob Landley error = -EINVAL; 15061da177e4SLinus Torvalds goto bad_swap; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 15091da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 15101da177e4SLinus Torvalds if (error < 0) 15111da177e4SLinus Torvalds goto bad_swap; 15121da177e4SLinus Torvalds p->bdev = bdev; 15131da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 15141da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 15151b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 15161da177e4SLinus Torvalds did_down = 1; 15171da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 15181da177e4SLinus Torvalds error = -EBUSY; 15191da177e4SLinus Torvalds goto bad_swap; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds } else { 15221da177e4SLinus Torvalds goto bad_swap; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds swapfilesize = i_size_read(inode) >> PAGE_SHIFT; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /* 15281da177e4SLinus Torvalds * Read the swap header. 15291da177e4SLinus Torvalds */ 15301da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 15311da177e4SLinus Torvalds error = -EINVAL; 15321da177e4SLinus Torvalds goto bad_swap; 15331da177e4SLinus Torvalds } 1534090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 15351da177e4SLinus Torvalds if (IS_ERR(page)) { 15361da177e4SLinus Torvalds error = PTR_ERR(page); 15371da177e4SLinus Torvalds goto bad_swap; 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds kmap(page); 15401da177e4SLinus Torvalds swap_header = page_address(page); 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10)) 15431da177e4SLinus Torvalds swap_header_version = 1; 15441da177e4SLinus Torvalds else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10)) 15451da177e4SLinus Torvalds swap_header_version = 2; 15461da177e4SLinus Torvalds else { 1547e97a3111SJesper Juhl printk(KERN_ERR "Unable to find swap-space signature\n"); 15481da177e4SLinus Torvalds error = -EINVAL; 15491da177e4SLinus Torvalds goto bad_swap; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds switch (swap_header_version) { 15531da177e4SLinus Torvalds case 1: 15541da177e4SLinus Torvalds printk(KERN_ERR "version 0 swap is no longer supported. " 15551da177e4SLinus Torvalds "Use mkswap -v1 %s\n", name); 15561da177e4SLinus Torvalds error = -EINVAL; 15571da177e4SLinus Torvalds goto bad_swap; 15581da177e4SLinus Torvalds case 2: 15591da177e4SLinus Torvalds /* Check the swap header's sub-version and the size of 15601da177e4SLinus Torvalds the swap file and bad block lists */ 15611da177e4SLinus Torvalds if (swap_header->info.version != 1) { 15621da177e4SLinus Torvalds printk(KERN_WARNING 15631da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 15641da177e4SLinus Torvalds swap_header->info.version); 15651da177e4SLinus Torvalds error = -EINVAL; 15661da177e4SLinus Torvalds goto bad_swap; 15671da177e4SLinus Torvalds } 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds p->lowest_bit = 1; 157052b7efdbSHugh Dickins p->cluster_next = 1; 157152b7efdbSHugh Dickins 15721da177e4SLinus Torvalds /* 15731da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 15741da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 15751da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 15761da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 15771da177e4SLinus Torvalds * the different architectures. In order to find the 15781da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 15791da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 15801da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 15811da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 15821da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 15831da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 15841da177e4SLinus Torvalds * swap pte. 15851da177e4SLinus Torvalds */ 15861da177e4SLinus Torvalds maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1; 15871da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 15881da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 15891da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 15901da177e4SLinus Torvalds 15911da177e4SLinus Torvalds error = -EINVAL; 1592e2244ec2SHugh Dickins if (!maxpages) 1593e2244ec2SHugh Dickins goto bad_swap; 15945d1854e1SEric Sandeen if (swapfilesize && maxpages > swapfilesize) { 15955d1854e1SEric Sandeen printk(KERN_WARNING 15965d1854e1SEric Sandeen "Swap area shorter than signature indicates\n"); 15975d1854e1SEric Sandeen goto bad_swap; 15985d1854e1SEric Sandeen } 1599e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1600e2244ec2SHugh Dickins goto bad_swap; 16011da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 16021da177e4SLinus Torvalds goto bad_swap; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 16051da177e4SLinus Torvalds if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) { 16061da177e4SLinus Torvalds error = -ENOMEM; 16071da177e4SLinus Torvalds goto bad_swap; 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds error = 0; 16111da177e4SLinus Torvalds memset(p->swap_map, 0, maxpages * sizeof(short)); 16121da177e4SLinus Torvalds for (i = 0; i < swap_header->info.nr_badpages; i++) { 1613cd105df4STobias Klauser int page_nr = swap_header->info.badpages[i]; 1614cd105df4STobias Klauser if (page_nr <= 0 || page_nr >= swap_header->info.last_page) 16151da177e4SLinus Torvalds error = -EINVAL; 16161da177e4SLinus Torvalds else 1617cd105df4STobias Klauser p->swap_map[page_nr] = SWAP_MAP_BAD; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 16201da177e4SLinus Torvalds swap_header->info.nr_badpages - 16211da177e4SLinus Torvalds 1 /* header page */; 16221da177e4SLinus Torvalds if (error) 16231da177e4SLinus Torvalds goto bad_swap; 16241da177e4SLinus Torvalds } 16251da177e4SLinus Torvalds 1626e2244ec2SHugh Dickins if (nr_good_pages) { 1627e2244ec2SHugh Dickins p->swap_map[0] = SWAP_MAP_BAD; 1628e2244ec2SHugh Dickins p->max = maxpages; 1629e2244ec2SHugh Dickins p->pages = nr_good_pages; 163053092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 163153092a74SHugh Dickins if (nr_extents < 0) { 163253092a74SHugh Dickins error = nr_extents; 1633e2244ec2SHugh Dickins goto bad_swap; 163453092a74SHugh Dickins } 1635e2244ec2SHugh Dickins nr_good_pages = p->pages; 1636e2244ec2SHugh Dickins } 16371da177e4SLinus Torvalds if (!nr_good_pages) { 16381da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 16391da177e4SLinus Torvalds error = -EINVAL; 16401da177e4SLinus Torvalds goto bad_swap; 16411da177e4SLinus Torvalds } 16421da177e4SLinus Torvalds 1643fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 16445d337b91SHugh Dickins spin_lock(&swap_lock); 16451da177e4SLinus Torvalds p->flags = SWP_ACTIVE; 16461da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 16471da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 164853092a74SHugh Dickins 16496eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 165053092a74SHugh Dickins "Priority:%d extents:%d across:%lluk\n", 165153092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 165253092a74SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10)); 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds /* insert swap space into swap_list: */ 16551da177e4SLinus Torvalds prev = -1; 16561da177e4SLinus Torvalds for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 16571da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) { 16581da177e4SLinus Torvalds break; 16591da177e4SLinus Torvalds } 16601da177e4SLinus Torvalds prev = i; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds p->next = i; 16631da177e4SLinus Torvalds if (prev < 0) { 16641da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 16651da177e4SLinus Torvalds } else { 16661da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 16671da177e4SLinus Torvalds } 16685d337b91SHugh Dickins spin_unlock(&swap_lock); 1669fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 16701da177e4SLinus Torvalds error = 0; 16711da177e4SLinus Torvalds goto out; 16721da177e4SLinus Torvalds bad_swap: 16731da177e4SLinus Torvalds if (bdev) { 16741da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 16751da177e4SLinus Torvalds bd_release(bdev); 16761da177e4SLinus Torvalds } 16774cd3bb10SHugh Dickins destroy_swap_extents(p); 16781da177e4SLinus Torvalds bad_swap_2: 16795d337b91SHugh Dickins spin_lock(&swap_lock); 16801da177e4SLinus Torvalds swap_map = p->swap_map; 16811da177e4SLinus Torvalds p->swap_file = NULL; 16821da177e4SLinus Torvalds p->swap_map = NULL; 16831da177e4SLinus Torvalds p->flags = 0; 16841da177e4SLinus Torvalds if (!(swap_flags & SWAP_FLAG_PREFER)) 16851da177e4SLinus Torvalds ++least_priority; 16865d337b91SHugh Dickins spin_unlock(&swap_lock); 16871da177e4SLinus Torvalds vfree(swap_map); 16881da177e4SLinus Torvalds if (swap_file) 16891da177e4SLinus Torvalds filp_close(swap_file, NULL); 16901da177e4SLinus Torvalds out: 16911da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 16921da177e4SLinus Torvalds kunmap(page); 16931da177e4SLinus Torvalds page_cache_release(page); 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds if (name) 16961da177e4SLinus Torvalds putname(name); 16971da177e4SLinus Torvalds if (did_down) { 16981da177e4SLinus Torvalds if (!error) 16991da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 17001b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds return error; 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 17061da177e4SLinus Torvalds { 17071da177e4SLinus Torvalds unsigned int i; 17081da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 17091da177e4SLinus Torvalds 17105d337b91SHugh Dickins spin_lock(&swap_lock); 17111da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 17121da177e4SLinus Torvalds if (!(swap_info[i].flags & SWP_USED) || 17131da177e4SLinus Torvalds (swap_info[i].flags & SWP_WRITEOK)) 17141da177e4SLinus Torvalds continue; 17151da177e4SLinus Torvalds nr_to_be_unused += swap_info[i].inuse_pages; 17161da177e4SLinus Torvalds } 17171da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 17181da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 17195d337b91SHugh Dickins spin_unlock(&swap_lock); 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17221da177e4SLinus Torvalds /* 17231da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 17241da177e4SLinus Torvalds * 17251da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 17261da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 17271da177e4SLinus Torvalds */ 17281da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry) 17291da177e4SLinus Torvalds { 17301da177e4SLinus Torvalds struct swap_info_struct * p; 17311da177e4SLinus Torvalds unsigned long offset, type; 17321da177e4SLinus Torvalds int result = 0; 17331da177e4SLinus Torvalds 17340697212aSChristoph Lameter if (is_migration_entry(entry)) 17350697212aSChristoph Lameter return 1; 17360697212aSChristoph Lameter 17371da177e4SLinus Torvalds type = swp_type(entry); 17381da177e4SLinus Torvalds if (type >= nr_swapfiles) 17391da177e4SLinus Torvalds goto bad_file; 17401da177e4SLinus Torvalds p = type + swap_info; 17411da177e4SLinus Torvalds offset = swp_offset(entry); 17421da177e4SLinus Torvalds 17435d337b91SHugh Dickins spin_lock(&swap_lock); 17441da177e4SLinus Torvalds if (offset < p->max && p->swap_map[offset]) { 17451da177e4SLinus Torvalds if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { 17461da177e4SLinus Torvalds p->swap_map[offset]++; 17471da177e4SLinus Torvalds result = 1; 17481da177e4SLinus Torvalds } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { 17491da177e4SLinus Torvalds if (swap_overflow++ < 5) 17501da177e4SLinus Torvalds printk(KERN_WARNING "swap_dup: swap entry overflow\n"); 17511da177e4SLinus Torvalds p->swap_map[offset] = SWAP_MAP_MAX; 17521da177e4SLinus Torvalds result = 1; 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds } 17555d337b91SHugh Dickins spin_unlock(&swap_lock); 17561da177e4SLinus Torvalds out: 17571da177e4SLinus Torvalds return result; 17581da177e4SLinus Torvalds 17591da177e4SLinus Torvalds bad_file: 17601da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 17611da177e4SLinus Torvalds goto out; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds struct swap_info_struct * 17651da177e4SLinus Torvalds get_swap_info_struct(unsigned type) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds return &swap_info[type]; 17681da177e4SLinus Torvalds } 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds /* 17715d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 17721da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 17731da177e4SLinus Torvalds */ 17741da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 17751da177e4SLinus Torvalds { 17768952898bSHugh Dickins struct swap_info_struct *si; 17773f9e7949SHugh Dickins int our_page_cluster = page_cluster; 17788952898bSHugh Dickins pgoff_t target, toff; 17798952898bSHugh Dickins pgoff_t base, end; 17808952898bSHugh Dickins int nr_pages = 0; 17811da177e4SLinus Torvalds 17823f9e7949SHugh Dickins if (!our_page_cluster) /* no readahead */ 17831da177e4SLinus Torvalds return 0; 17848952898bSHugh Dickins 17858952898bSHugh Dickins si = &swap_info[swp_type(entry)]; 17868952898bSHugh Dickins target = swp_offset(entry); 17878952898bSHugh Dickins base = (target >> our_page_cluster) << our_page_cluster; 17888952898bSHugh Dickins end = base + (1 << our_page_cluster); 17898952898bSHugh Dickins if (!base) /* first page is swap header */ 17908952898bSHugh Dickins base++; 17911da177e4SLinus Torvalds 17925d337b91SHugh Dickins spin_lock(&swap_lock); 17938952898bSHugh Dickins if (end > si->max) /* don't go beyond end of map */ 17948952898bSHugh Dickins end = si->max; 17958952898bSHugh Dickins 17968952898bSHugh Dickins /* Count contiguous allocated slots above our target */ 17978952898bSHugh Dickins for (toff = target; ++toff < end; nr_pages++) { 17981da177e4SLinus Torvalds /* Don't read in free or bad pages */ 17998952898bSHugh Dickins if (!si->swap_map[toff]) 18001da177e4SLinus Torvalds break; 18018952898bSHugh Dickins if (si->swap_map[toff] == SWAP_MAP_BAD) 18021da177e4SLinus Torvalds break; 18038952898bSHugh Dickins } 18048952898bSHugh Dickins /* Count contiguous allocated slots below our target */ 18058952898bSHugh Dickins for (toff = target; --toff >= base; nr_pages++) { 18068952898bSHugh Dickins /* Don't read in free or bad pages */ 18078952898bSHugh Dickins if (!si->swap_map[toff]) 18088952898bSHugh Dickins break; 18098952898bSHugh Dickins if (si->swap_map[toff] == SWAP_MAP_BAD) 18108952898bSHugh Dickins break; 18118952898bSHugh Dickins } 18125d337b91SHugh Dickins spin_unlock(&swap_lock); 18138952898bSHugh Dickins 18148952898bSHugh Dickins /* 18158952898bSHugh Dickins * Indicate starting offset, and return number of pages to get: 18168952898bSHugh Dickins * if only 1, say 0, since there's then no readahead to be done. 18178952898bSHugh Dickins */ 18188952898bSHugh Dickins *offset = ++toff; 18198952898bSHugh Dickins return nr_pages? ++nr_pages: 0; 18201da177e4SLinus Torvalds } 1821