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/config.h> 91da177e4SLinus Torvalds #include <linux/mm.h> 101da177e4SLinus Torvalds #include <linux/hugetlb.h> 111da177e4SLinus Torvalds #include <linux/mman.h> 121da177e4SLinus Torvalds #include <linux/slab.h> 131da177e4SLinus Torvalds #include <linux/kernel_stat.h> 141da177e4SLinus Torvalds #include <linux/swap.h> 151da177e4SLinus Torvalds #include <linux/vmalloc.h> 161da177e4SLinus Torvalds #include <linux/pagemap.h> 171da177e4SLinus Torvalds #include <linux/namei.h> 181da177e4SLinus Torvalds #include <linux/shm.h> 191da177e4SLinus Torvalds #include <linux/blkdev.h> 201da177e4SLinus Torvalds #include <linux/writeback.h> 211da177e4SLinus Torvalds #include <linux/proc_fs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 231da177e4SLinus Torvalds #include <linux/init.h> 241da177e4SLinus Torvalds #include <linux/module.h> 251da177e4SLinus Torvalds #include <linux/rmap.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <asm/pgtable.h> 311da177e4SLinus Torvalds #include <asm/tlbflush.h> 321da177e4SLinus Torvalds #include <linux/swapops.h> 331da177e4SLinus Torvalds 345d337b91SHugh Dickins DEFINE_SPINLOCK(swap_lock); 351da177e4SLinus Torvalds unsigned int nr_swapfiles; 361da177e4SLinus Torvalds long total_swap_pages; 371da177e4SLinus Torvalds static int swap_overflow; 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds EXPORT_SYMBOL(total_swap_pages); 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 481da177e4SLinus Torvalds struct swap_info_struct swap_info[MAX_SWAPFILES]; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds static DECLARE_MUTEX(swapon_sem); 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 551da177e4SLinus Torvalds * cannot be turned into a semaphore. 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); 641da177e4SLinus Torvalds entry.val = page->private; 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 721da177e4SLinus Torvalds * count to avoid reading garbage from page->private above. If 731da177e4SLinus Torvalds * 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); 1197dfad418SHugh Dickins 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 2161da177e4SLinus Torvalds static struct swap_info_struct * swap_info_get(swp_entry_t entry) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct swap_info_struct * p; 2191da177e4SLinus Torvalds unsigned long offset, type; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds if (!entry.val) 2221da177e4SLinus Torvalds goto out; 2231da177e4SLinus Torvalds type = swp_type(entry); 2241da177e4SLinus Torvalds if (type >= nr_swapfiles) 2251da177e4SLinus Torvalds goto bad_nofile; 2261da177e4SLinus Torvalds p = & swap_info[type]; 2271da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 2281da177e4SLinus Torvalds goto bad_device; 2291da177e4SLinus Torvalds offset = swp_offset(entry); 2301da177e4SLinus Torvalds if (offset >= p->max) 2311da177e4SLinus Torvalds goto bad_offset; 2321da177e4SLinus Torvalds if (!p->swap_map[offset]) 2331da177e4SLinus Torvalds goto bad_free; 2345d337b91SHugh Dickins spin_lock(&swap_lock); 2351da177e4SLinus Torvalds return p; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds bad_free: 2381da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); 2391da177e4SLinus Torvalds goto out; 2401da177e4SLinus Torvalds bad_offset: 2411da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); 2421da177e4SLinus Torvalds goto out; 2431da177e4SLinus Torvalds bad_device: 2441da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); 2451da177e4SLinus Torvalds goto out; 2461da177e4SLinus Torvalds bad_nofile: 2471da177e4SLinus Torvalds printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); 2481da177e4SLinus Torvalds out: 2491da177e4SLinus Torvalds return NULL; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds static int swap_entry_free(struct swap_info_struct *p, unsigned long offset) 2531da177e4SLinus Torvalds { 2541da177e4SLinus Torvalds int count = p->swap_map[offset]; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds if (count < SWAP_MAP_MAX) { 2571da177e4SLinus Torvalds count--; 2581da177e4SLinus Torvalds p->swap_map[offset] = count; 2591da177e4SLinus Torvalds if (!count) { 2601da177e4SLinus Torvalds if (offset < p->lowest_bit) 2611da177e4SLinus Torvalds p->lowest_bit = offset; 2621da177e4SLinus Torvalds if (offset > p->highest_bit) 2631da177e4SLinus Torvalds p->highest_bit = offset; 26489d09a2cSHugh Dickins if (p->prio > swap_info[swap_list.next].prio) 26589d09a2cSHugh Dickins swap_list.next = p - swap_info; 2661da177e4SLinus Torvalds nr_swap_pages++; 2671da177e4SLinus Torvalds p->inuse_pages--; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds return count; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /* 2741da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 2751da177e4SLinus Torvalds * is still around or has not been recycled. 2761da177e4SLinus Torvalds */ 2771da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds struct swap_info_struct * p; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds p = swap_info_get(entry); 2821da177e4SLinus Torvalds if (p) { 2831da177e4SLinus Torvalds swap_entry_free(p, swp_offset(entry)); 2845d337b91SHugh Dickins spin_unlock(&swap_lock); 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* 289c475a8abSHugh Dickins * How many references to page are currently swapped out? 2901da177e4SLinus Torvalds */ 291c475a8abSHugh Dickins static inline int page_swapcount(struct page *page) 2921da177e4SLinus Torvalds { 293c475a8abSHugh Dickins int count = 0; 2941da177e4SLinus Torvalds struct swap_info_struct *p; 2951da177e4SLinus Torvalds swp_entry_t entry; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds entry.val = page->private; 2981da177e4SLinus Torvalds p = swap_info_get(entry); 2991da177e4SLinus Torvalds if (p) { 300c475a8abSHugh Dickins /* Subtract the 1 for the swap cache itself */ 301c475a8abSHugh Dickins count = p->swap_map[swp_offset(entry)] - 1; 3025d337b91SHugh Dickins spin_unlock(&swap_lock); 3031da177e4SLinus Torvalds } 304c475a8abSHugh Dickins return count; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * We can use this swap cache entry directly 3091da177e4SLinus Torvalds * if there are no other references to it. 3101da177e4SLinus Torvalds */ 3111da177e4SLinus Torvalds int can_share_swap_page(struct page *page) 3121da177e4SLinus Torvalds { 313c475a8abSHugh Dickins int count; 3141da177e4SLinus Torvalds 315c475a8abSHugh Dickins BUG_ON(!PageLocked(page)); 316c475a8abSHugh Dickins count = page_mapcount(page); 317c475a8abSHugh Dickins if (count <= 1 && PageSwapCache(page)) 318c475a8abSHugh Dickins count += page_swapcount(page); 319c475a8abSHugh Dickins return count == 1; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds /* 3231da177e4SLinus Torvalds * Work out if there are any other processes sharing this 3241da177e4SLinus Torvalds * swap cache page. Free it if you can. Return success. 3251da177e4SLinus Torvalds */ 3261da177e4SLinus Torvalds int remove_exclusive_swap_page(struct page *page) 3271da177e4SLinus Torvalds { 3281da177e4SLinus Torvalds int retval; 3291da177e4SLinus Torvalds struct swap_info_struct * p; 3301da177e4SLinus Torvalds swp_entry_t entry; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 3331da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds if (!PageSwapCache(page)) 3361da177e4SLinus Torvalds return 0; 3371da177e4SLinus Torvalds if (PageWriteback(page)) 3381da177e4SLinus Torvalds return 0; 3391da177e4SLinus Torvalds if (page_count(page) != 2) /* 2: us + cache */ 3401da177e4SLinus Torvalds return 0; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds entry.val = page->private; 3431da177e4SLinus Torvalds p = swap_info_get(entry); 3441da177e4SLinus Torvalds if (!p) 3451da177e4SLinus Torvalds return 0; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* Is the only swap cache user the cache itself? */ 3481da177e4SLinus Torvalds retval = 0; 3491da177e4SLinus Torvalds if (p->swap_map[swp_offset(entry)] == 1) { 3501da177e4SLinus Torvalds /* Recheck the page count with the swapcache lock held.. */ 3511da177e4SLinus Torvalds write_lock_irq(&swapper_space.tree_lock); 3521da177e4SLinus Torvalds if ((page_count(page) == 2) && !PageWriteback(page)) { 3531da177e4SLinus Torvalds __delete_from_swap_cache(page); 3541da177e4SLinus Torvalds SetPageDirty(page); 3551da177e4SLinus Torvalds retval = 1; 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds write_unlock_irq(&swapper_space.tree_lock); 3581da177e4SLinus Torvalds } 3595d337b91SHugh Dickins spin_unlock(&swap_lock); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds if (retval) { 3621da177e4SLinus Torvalds swap_free(entry); 3631da177e4SLinus Torvalds page_cache_release(page); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds return retval; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /* 3701da177e4SLinus Torvalds * Free the swap entry like above, but also try to 3711da177e4SLinus Torvalds * free the page cache entry if it is the last user. 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds void free_swap_and_cache(swp_entry_t entry) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds struct swap_info_struct * p; 3761da177e4SLinus Torvalds struct page *page = NULL; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds p = swap_info_get(entry); 3791da177e4SLinus Torvalds if (p) { 3801da177e4SLinus Torvalds if (swap_entry_free(p, swp_offset(entry)) == 1) 3811da177e4SLinus Torvalds page = find_trylock_page(&swapper_space, entry.val); 3825d337b91SHugh Dickins spin_unlock(&swap_lock); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds if (page) { 3851da177e4SLinus Torvalds int one_user; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds BUG_ON(PagePrivate(page)); 3881da177e4SLinus Torvalds page_cache_get(page); 3891da177e4SLinus Torvalds one_user = (page_count(page) == 2); 3901da177e4SLinus Torvalds /* Only cache user (+us), or swap space full? Free it! */ 3911da177e4SLinus Torvalds if (!PageWriteback(page) && (one_user || vm_swap_full())) { 3921da177e4SLinus Torvalds delete_from_swap_cache(page); 3931da177e4SLinus Torvalds SetPageDirty(page); 3941da177e4SLinus Torvalds } 3951da177e4SLinus Torvalds unlock_page(page); 3961da177e4SLinus Torvalds page_cache_release(page); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* 40172866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 40272866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 40372866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 4041da177e4SLinus Torvalds */ 4051da177e4SLinus Torvalds static void unuse_pte(struct vm_area_struct *vma, pte_t *pte, 4061da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 4071da177e4SLinus Torvalds { 4084294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 4091da177e4SLinus Torvalds get_page(page); 4101da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 4111da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 4121da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 4131da177e4SLinus Torvalds swap_free(entry); 4141da177e4SLinus Torvalds /* 4151da177e4SLinus Torvalds * Move the page to the active list so it is not 4161da177e4SLinus Torvalds * immediately swapped out again after swapon. 4171da177e4SLinus Torvalds */ 4181da177e4SLinus Torvalds activate_page(page); 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 4221da177e4SLinus Torvalds unsigned long addr, unsigned long end, 4231da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 4241da177e4SLinus Torvalds { 4251da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 426*705e87c0SHugh Dickins pte_t *pte; 427*705e87c0SHugh Dickins spinlock_t *ptl; 428*705e87c0SHugh Dickins int found = 0; 4291da177e4SLinus Torvalds 430*705e87c0SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 4311da177e4SLinus Torvalds do { 4321da177e4SLinus Torvalds /* 4331da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 4341da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 4351da177e4SLinus Torvalds */ 4361da177e4SLinus Torvalds if (unlikely(pte_same(*pte, swp_pte))) { 437*705e87c0SHugh Dickins unuse_pte(vma, pte++, addr, entry, page); 438*705e87c0SHugh Dickins found = 1; 439*705e87c0SHugh Dickins break; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 442*705e87c0SHugh Dickins pte_unmap_unlock(pte - 1, ptl); 443*705e87c0SHugh Dickins return found; 4441da177e4SLinus Torvalds } 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 4471da177e4SLinus Torvalds unsigned long addr, unsigned long end, 4481da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 4491da177e4SLinus Torvalds { 4501da177e4SLinus Torvalds pmd_t *pmd; 4511da177e4SLinus Torvalds unsigned long next; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 4541da177e4SLinus Torvalds do { 4551da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 4561da177e4SLinus Torvalds if (pmd_none_or_clear_bad(pmd)) 4571da177e4SLinus Torvalds continue; 4581da177e4SLinus Torvalds if (unuse_pte_range(vma, pmd, addr, next, entry, page)) 4591da177e4SLinus Torvalds return 1; 4601da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 4611da177e4SLinus Torvalds return 0; 4621da177e4SLinus Torvalds } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 4651da177e4SLinus Torvalds unsigned long addr, unsigned long end, 4661da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 4671da177e4SLinus Torvalds { 4681da177e4SLinus Torvalds pud_t *pud; 4691da177e4SLinus Torvalds unsigned long next; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 4721da177e4SLinus Torvalds do { 4731da177e4SLinus Torvalds next = pud_addr_end(addr, end); 4741da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 4751da177e4SLinus Torvalds continue; 4761da177e4SLinus Torvalds if (unuse_pmd_range(vma, pud, addr, next, entry, page)) 4771da177e4SLinus Torvalds return 1; 4781da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 4791da177e4SLinus Torvalds return 0; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 4831da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 4841da177e4SLinus Torvalds { 4851da177e4SLinus Torvalds pgd_t *pgd; 4861da177e4SLinus Torvalds unsigned long addr, end, next; 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (page->mapping) { 4891da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 4901da177e4SLinus Torvalds if (addr == -EFAULT) 4911da177e4SLinus Torvalds return 0; 4921da177e4SLinus Torvalds else 4931da177e4SLinus Torvalds end = addr + PAGE_SIZE; 4941da177e4SLinus Torvalds } else { 4951da177e4SLinus Torvalds addr = vma->vm_start; 4961da177e4SLinus Torvalds end = vma->vm_end; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 5001da177e4SLinus Torvalds do { 5011da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 5021da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 5031da177e4SLinus Torvalds continue; 5041da177e4SLinus Torvalds if (unuse_pud_range(vma, pgd, addr, next, entry, page)) 5051da177e4SLinus Torvalds return 1; 5061da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 5071da177e4SLinus Torvalds return 0; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 5111da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds struct vm_area_struct *vma; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 5161da177e4SLinus Torvalds /* 517c475a8abSHugh Dickins * Activate page so shrink_cache is unlikely to unmap its 518c475a8abSHugh Dickins * ptes while lock is dropped, so swapoff can make progress. 5191da177e4SLinus Torvalds */ 520c475a8abSHugh Dickins activate_page(page); 5211da177e4SLinus Torvalds unlock_page(page); 5221da177e4SLinus Torvalds down_read(&mm->mmap_sem); 5231da177e4SLinus Torvalds lock_page(page); 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 5261da177e4SLinus Torvalds if (vma->anon_vma && unuse_vma(vma, entry, page)) 5271da177e4SLinus Torvalds break; 5281da177e4SLinus Torvalds } 5291da177e4SLinus Torvalds up_read(&mm->mmap_sem); 5301da177e4SLinus Torvalds /* 5311da177e4SLinus Torvalds * Currently unuse_mm cannot fail, but leave error handling 5321da177e4SLinus Torvalds * at call sites for now, since we change it from time to time. 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds return 0; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds 5371da177e4SLinus Torvalds /* 5381da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 5391da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 5401da177e4SLinus Torvalds */ 5416eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 5426eb396dcSHugh Dickins unsigned int prev) 5431da177e4SLinus Torvalds { 5446eb396dcSHugh Dickins unsigned int max = si->max; 5456eb396dcSHugh Dickins unsigned int i = prev; 5461da177e4SLinus Torvalds int count; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* 5495d337b91SHugh Dickins * No need for swap_lock here: we're just looking 5501da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 5511da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 5525d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 5531da177e4SLinus Torvalds */ 5541da177e4SLinus Torvalds for (;;) { 5551da177e4SLinus Torvalds if (++i >= max) { 5561da177e4SLinus Torvalds if (!prev) { 5571da177e4SLinus Torvalds i = 0; 5581da177e4SLinus Torvalds break; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds /* 5611da177e4SLinus Torvalds * No entries in use at top of swap_map, 5621da177e4SLinus Torvalds * loop back to start and recheck there. 5631da177e4SLinus Torvalds */ 5641da177e4SLinus Torvalds max = prev + 1; 5651da177e4SLinus Torvalds prev = 0; 5661da177e4SLinus Torvalds i = 1; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds count = si->swap_map[i]; 5691da177e4SLinus Torvalds if (count && count != SWAP_MAP_BAD) 5701da177e4SLinus Torvalds break; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds return i; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 5761da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 5771da177e4SLinus Torvalds * and then search for the process using it. All the necessary 5781da177e4SLinus Torvalds * page table adjustments can then be made atomically. 5791da177e4SLinus Torvalds */ 5801da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct swap_info_struct * si = &swap_info[type]; 5831da177e4SLinus Torvalds struct mm_struct *start_mm; 5841da177e4SLinus Torvalds unsigned short *swap_map; 5851da177e4SLinus Torvalds unsigned short swcount; 5861da177e4SLinus Torvalds struct page *page; 5871da177e4SLinus Torvalds swp_entry_t entry; 5886eb396dcSHugh Dickins unsigned int i = 0; 5891da177e4SLinus Torvalds int retval = 0; 5901da177e4SLinus Torvalds int reset_overflow = 0; 5911da177e4SLinus Torvalds int shmem; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /* 5941da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 5951da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 5961da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 5971da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 5981da177e4SLinus Torvalds * 5991da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 6001da177e4SLinus Torvalds * freed the previous entry from; but that would take less 6011da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 6021da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 6031da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 6041da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 6051da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 6061da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds start_mm = &init_mm; 6091da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 6131da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 6141da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 6171da177e4SLinus Torvalds if (signal_pending(current)) { 6181da177e4SLinus Torvalds retval = -EINTR; 6191da177e4SLinus Torvalds break; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6221da177e4SLinus Torvalds /* 6231da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 6241da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 6251da177e4SLinus Torvalds * page and read the swap into it. 6261da177e4SLinus Torvalds */ 6271da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 6281da177e4SLinus Torvalds entry = swp_entry(type, i); 6291da177e4SLinus Torvalds page = read_swap_cache_async(entry, NULL, 0); 6301da177e4SLinus Torvalds if (!page) { 6311da177e4SLinus Torvalds /* 6321da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 6331da177e4SLinus Torvalds * has been freed independently, and will not be 6341da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 6351da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds if (!*swap_map) 6381da177e4SLinus Torvalds continue; 6391da177e4SLinus Torvalds retval = -ENOMEM; 6401da177e4SLinus Torvalds break; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds /* 6441da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 6451da177e4SLinus Torvalds */ 6461da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 6471da177e4SLinus Torvalds mmput(start_mm); 6481da177e4SLinus Torvalds start_mm = &init_mm; 6491da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 6501da177e4SLinus Torvalds } 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* 6531da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 6541da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 6551da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 6561da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 6571da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 6581da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 6591da177e4SLinus Torvalds */ 6601da177e4SLinus Torvalds wait_on_page_locked(page); 6611da177e4SLinus Torvalds wait_on_page_writeback(page); 6621da177e4SLinus Torvalds lock_page(page); 6631da177e4SLinus Torvalds wait_on_page_writeback(page); 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /* 6661da177e4SLinus Torvalds * Remove all references to entry. 6671da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 6681da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 6691da177e4SLinus Torvalds */ 6701da177e4SLinus Torvalds shmem = 0; 6711da177e4SLinus Torvalds swcount = *swap_map; 6721da177e4SLinus Torvalds if (swcount > 1) { 6731da177e4SLinus Torvalds if (start_mm == &init_mm) 6741da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 6751da177e4SLinus Torvalds else 6761da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds if (*swap_map > 1) { 6791da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 6801da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 6811da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 6821da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 6831da177e4SLinus Torvalds struct mm_struct *mm; 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 6861da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 6871da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6881da177e4SLinus Torvalds while (*swap_map > 1 && !retval && 6891da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 6901da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 6911da177e4SLinus Torvalds if (atomic_inc_return(&mm->mm_users) == 1) { 6921da177e4SLinus Torvalds atomic_dec(&mm->mm_users); 6931da177e4SLinus Torvalds continue; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6961da177e4SLinus Torvalds mmput(prev_mm); 6971da177e4SLinus Torvalds prev_mm = mm; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds cond_resched(); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds swcount = *swap_map; 7021da177e4SLinus Torvalds if (swcount <= 1) 7031da177e4SLinus Torvalds ; 7041da177e4SLinus Torvalds else if (mm == &init_mm) { 7051da177e4SLinus Torvalds set_start_mm = 1; 7061da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 7071da177e4SLinus Torvalds } else 7081da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 7091da177e4SLinus Torvalds if (set_start_mm && *swap_map < swcount) { 7101da177e4SLinus Torvalds mmput(new_start_mm); 7111da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 7121da177e4SLinus Torvalds new_start_mm = mm; 7131da177e4SLinus Torvalds set_start_mm = 0; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds spin_lock(&mmlist_lock); 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7181da177e4SLinus Torvalds mmput(prev_mm); 7191da177e4SLinus Torvalds mmput(start_mm); 7201da177e4SLinus Torvalds start_mm = new_start_mm; 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds if (retval) { 7231da177e4SLinus Torvalds unlock_page(page); 7241da177e4SLinus Torvalds page_cache_release(page); 7251da177e4SLinus Torvalds break; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * How could swap count reach 0x7fff when the maximum 7301da177e4SLinus Torvalds * pid is 0x7fff, and there's no way to repeat a swap 7311da177e4SLinus Torvalds * page within an mm (except in shmem, where it's the 7321da177e4SLinus Torvalds * shared object which takes the reference count)? 7331da177e4SLinus Torvalds * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. 7341da177e4SLinus Torvalds * 7351da177e4SLinus Torvalds * If that's wrong, then we should worry more about 7361da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 7371da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 7381da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 7391da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 7401da177e4SLinus Torvalds */ 7411da177e4SLinus Torvalds if (*swap_map == SWAP_MAP_MAX) { 7425d337b91SHugh Dickins spin_lock(&swap_lock); 7431da177e4SLinus Torvalds *swap_map = 1; 7445d337b91SHugh Dickins spin_unlock(&swap_lock); 7451da177e4SLinus Torvalds reset_overflow = 1; 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds /* 7491da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 7501da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 7511da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 7521da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 7531da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 7541da177e4SLinus Torvalds * delete from cache even if there's another reference, 7551da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 7561da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 7571da177e4SLinus Torvalds * read from disk into another page. Splitting into two 7581da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 7591da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 7601da177e4SLinus Torvalds * 7611da177e4SLinus Torvalds * Note shmem_unuse already deleted a swappage from 7621da177e4SLinus Torvalds * the swap cache, unless the move to filepage failed: 7631da177e4SLinus Torvalds * in which case it left swappage in cache, lowered its 7641da177e4SLinus Torvalds * swap count to pass quickly through the loops above, 7651da177e4SLinus Torvalds * and now we must reincrement count to try again later. 7661da177e4SLinus Torvalds */ 7671da177e4SLinus Torvalds if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { 7681da177e4SLinus Torvalds struct writeback_control wbc = { 7691da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 7701da177e4SLinus Torvalds }; 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds swap_writepage(page, &wbc); 7731da177e4SLinus Torvalds lock_page(page); 7741da177e4SLinus Torvalds wait_on_page_writeback(page); 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds if (PageSwapCache(page)) { 7771da177e4SLinus Torvalds if (shmem) 7781da177e4SLinus Torvalds swap_duplicate(entry); 7791da177e4SLinus Torvalds else 7801da177e4SLinus Torvalds delete_from_swap_cache(page); 7811da177e4SLinus Torvalds } 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds /* 7841da177e4SLinus Torvalds * So we could skip searching mms once swap count went 7851da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 7861da177e4SLinus Torvalds * mark page dirty so shrink_list will preserve it. 7871da177e4SLinus Torvalds */ 7881da177e4SLinus Torvalds SetPageDirty(page); 7891da177e4SLinus Torvalds unlock_page(page); 7901da177e4SLinus Torvalds page_cache_release(page); 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* 7931da177e4SLinus Torvalds * Make sure that we aren't completely killing 7941da177e4SLinus Torvalds * interactive performance. 7951da177e4SLinus Torvalds */ 7961da177e4SLinus Torvalds cond_resched(); 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds mmput(start_mm); 8001da177e4SLinus Torvalds if (reset_overflow) { 8011da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 8021da177e4SLinus Torvalds swap_overflow = 0; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds return retval; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds 8071da177e4SLinus Torvalds /* 8085d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 8095d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 8105d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 8111da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds static void drain_mmlist(void) 8141da177e4SLinus Torvalds { 8151da177e4SLinus Torvalds struct list_head *p, *next; 8161da177e4SLinus Torvalds unsigned int i; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) 8191da177e4SLinus Torvalds if (swap_info[i].inuse_pages) 8201da177e4SLinus Torvalds return; 8211da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8221da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 8231da177e4SLinus Torvalds list_del_init(p); 8241da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds /* 8281da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 8291da177e4SLinus Torvalds * corresponds to page offset `offset'. 8301da177e4SLinus Torvalds */ 8311da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct swap_extent *se = sis->curr_swap_extent; 8341da177e4SLinus Torvalds struct swap_extent *start_se = se; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds for ( ; ; ) { 8371da177e4SLinus Torvalds struct list_head *lh; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds if (se->start_page <= offset && 8401da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 8411da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 8421da177e4SLinus Torvalds } 84311d31886SHugh Dickins lh = se->list.next; 8441da177e4SLinus Torvalds if (lh == &sis->extent_list) 84511d31886SHugh Dickins lh = lh->next; 8461da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 8471da177e4SLinus Torvalds sis->curr_swap_extent = se; 8481da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds /* 8531da177e4SLinus Torvalds * Free all of a swapdev's extent information 8541da177e4SLinus Torvalds */ 8551da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds while (!list_empty(&sis->extent_list)) { 8581da177e4SLinus Torvalds struct swap_extent *se; 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds se = list_entry(sis->extent_list.next, 8611da177e4SLinus Torvalds struct swap_extent, list); 8621da177e4SLinus Torvalds list_del(&se->list); 8631da177e4SLinus Torvalds kfree(se); 8641da177e4SLinus Torvalds } 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds /* 8681da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 86911d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 8701da177e4SLinus Torvalds * 87111d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 8721da177e4SLinus Torvalds */ 8731da177e4SLinus Torvalds static int 8741da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 8751da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 8761da177e4SLinus Torvalds { 8771da177e4SLinus Torvalds struct swap_extent *se; 8781da177e4SLinus Torvalds struct swap_extent *new_se; 8791da177e4SLinus Torvalds struct list_head *lh; 8801da177e4SLinus Torvalds 88111d31886SHugh Dickins lh = sis->extent_list.prev; /* The highest page extent */ 88211d31886SHugh Dickins if (lh != &sis->extent_list) { 8831da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 88411d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 88511d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 8861da177e4SLinus Torvalds /* Merge it */ 8871da177e4SLinus Torvalds se->nr_pages += nr_pages; 8881da177e4SLinus Torvalds return 0; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /* 8931da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 8961da177e4SLinus Torvalds if (new_se == NULL) 8971da177e4SLinus Torvalds return -ENOMEM; 8981da177e4SLinus Torvalds new_se->start_page = start_page; 8991da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 9001da177e4SLinus Torvalds new_se->start_block = start_block; 9011da177e4SLinus Torvalds 90211d31886SHugh Dickins list_add_tail(&new_se->list, &sis->extent_list); 90353092a74SHugh Dickins return 1; 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 9081da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 9091da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 9101da177e4SLinus Torvalds * time for locating where on disk a page belongs. 9111da177e4SLinus Torvalds * 9121da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 9131da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 9141da177e4SLinus Torvalds * swap files identically. 9151da177e4SLinus Torvalds * 9161da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 9171da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 9181da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 9191da177e4SLinus Torvalds * 9201da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 9211da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 9221da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 9231da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 9241da177e4SLinus Torvalds * for swapping. 9251da177e4SLinus Torvalds * 926b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 9271da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 9281da177e4SLinus Torvalds * which will scribble on the fs. 9291da177e4SLinus Torvalds * 9301da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 9311da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 9321da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 9331da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 9341da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 9351da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 9361da177e4SLinus Torvalds */ 93753092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds struct inode *inode; 9401da177e4SLinus Torvalds unsigned blocks_per_page; 9411da177e4SLinus Torvalds unsigned long page_no; 9421da177e4SLinus Torvalds unsigned blkbits; 9431da177e4SLinus Torvalds sector_t probe_block; 9441da177e4SLinus Torvalds sector_t last_block; 94553092a74SHugh Dickins sector_t lowest_block = -1; 94653092a74SHugh Dickins sector_t highest_block = 0; 94753092a74SHugh Dickins int nr_extents = 0; 9481da177e4SLinus Torvalds int ret; 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 9511da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 9521da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 95353092a74SHugh Dickins *span = sis->pages; 9541da177e4SLinus Torvalds goto done; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds blkbits = inode->i_blkbits; 9581da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 9591da177e4SLinus Torvalds 9601da177e4SLinus Torvalds /* 9611da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 9621da177e4SLinus Torvalds * to be very smart. 9631da177e4SLinus Torvalds */ 9641da177e4SLinus Torvalds probe_block = 0; 9651da177e4SLinus Torvalds page_no = 0; 9661da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 9671da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 9681da177e4SLinus Torvalds page_no < sis->max) { 9691da177e4SLinus Torvalds unsigned block_in_page; 9701da177e4SLinus Torvalds sector_t first_block; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 9731da177e4SLinus Torvalds if (first_block == 0) 9741da177e4SLinus Torvalds goto bad_bmap; 9751da177e4SLinus Torvalds 9761da177e4SLinus Torvalds /* 9771da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 9781da177e4SLinus Torvalds */ 9791da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 9801da177e4SLinus Torvalds probe_block++; 9811da177e4SLinus Torvalds goto reprobe; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 9851da177e4SLinus Torvalds block_in_page++) { 9861da177e4SLinus Torvalds sector_t block; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 9891da177e4SLinus Torvalds if (block == 0) 9901da177e4SLinus Torvalds goto bad_bmap; 9911da177e4SLinus Torvalds if (block != first_block + block_in_page) { 9921da177e4SLinus Torvalds /* Discontiguity */ 9931da177e4SLinus Torvalds probe_block++; 9941da177e4SLinus Torvalds goto reprobe; 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 99853092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 99953092a74SHugh Dickins if (page_no) { /* exclude the header page */ 100053092a74SHugh Dickins if (first_block < lowest_block) 100153092a74SHugh Dickins lowest_block = first_block; 100253092a74SHugh Dickins if (first_block > highest_block) 100353092a74SHugh Dickins highest_block = first_block; 100453092a74SHugh Dickins } 100553092a74SHugh Dickins 10061da177e4SLinus Torvalds /* 10071da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 10081da177e4SLinus Torvalds */ 100953092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 101053092a74SHugh Dickins if (ret < 0) 10111da177e4SLinus Torvalds goto out; 101253092a74SHugh Dickins nr_extents += ret; 10131da177e4SLinus Torvalds page_no++; 10141da177e4SLinus Torvalds probe_block += blocks_per_page; 10151da177e4SLinus Torvalds reprobe: 10161da177e4SLinus Torvalds continue; 10171da177e4SLinus Torvalds } 101853092a74SHugh Dickins ret = nr_extents; 101953092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 10201da177e4SLinus Torvalds if (page_no == 0) 1021e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 10221da177e4SLinus Torvalds sis->max = page_no; 1023e2244ec2SHugh Dickins sis->pages = page_no - 1; 10241da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 10251da177e4SLinus Torvalds done: 10261da177e4SLinus Torvalds sis->curr_swap_extent = list_entry(sis->extent_list.prev, 10271da177e4SLinus Torvalds struct swap_extent, list); 10281da177e4SLinus Torvalds goto out; 10291da177e4SLinus Torvalds bad_bmap: 10301da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 10311da177e4SLinus Torvalds ret = -EINVAL; 10321da177e4SLinus Torvalds out: 10331da177e4SLinus Torvalds return ret; 10341da177e4SLinus Torvalds } 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds #if 0 /* We don't need this yet */ 10371da177e4SLinus Torvalds #include <linux/backing-dev.h> 10381da177e4SLinus Torvalds int page_queue_congested(struct page *page) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds struct backing_dev_info *bdi; 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds if (PageSwapCache(page)) { 10451da177e4SLinus Torvalds swp_entry_t entry = { .val = page->private }; 10461da177e4SLinus Torvalds struct swap_info_struct *sis; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds sis = get_swap_info_struct(swp_type(entry)); 10491da177e4SLinus Torvalds bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; 10501da177e4SLinus Torvalds } else 10511da177e4SLinus Torvalds bdi = page->mapping->backing_dev_info; 10521da177e4SLinus Torvalds return bdi_write_congested(bdi); 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds #endif 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile) 10571da177e4SLinus Torvalds { 10581da177e4SLinus Torvalds struct swap_info_struct * p = NULL; 10591da177e4SLinus Torvalds unsigned short *swap_map; 10601da177e4SLinus Torvalds struct file *swap_file, *victim; 10611da177e4SLinus Torvalds struct address_space *mapping; 10621da177e4SLinus Torvalds struct inode *inode; 10631da177e4SLinus Torvalds char * pathname; 10641da177e4SLinus Torvalds int i, type, prev; 10651da177e4SLinus Torvalds int err; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 10681da177e4SLinus Torvalds return -EPERM; 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds pathname = getname(specialfile); 10711da177e4SLinus Torvalds err = PTR_ERR(pathname); 10721da177e4SLinus Torvalds if (IS_ERR(pathname)) 10731da177e4SLinus Torvalds goto out; 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 10761da177e4SLinus Torvalds putname(pathname); 10771da177e4SLinus Torvalds err = PTR_ERR(victim); 10781da177e4SLinus Torvalds if (IS_ERR(victim)) 10791da177e4SLinus Torvalds goto out; 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds mapping = victim->f_mapping; 10821da177e4SLinus Torvalds prev = -1; 10835d337b91SHugh Dickins spin_lock(&swap_lock); 10841da177e4SLinus Torvalds for (type = swap_list.head; type >= 0; type = swap_info[type].next) { 10851da177e4SLinus Torvalds p = swap_info + type; 10861da177e4SLinus Torvalds if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) { 10871da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 10881da177e4SLinus Torvalds break; 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds prev = type; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds if (type < 0) { 10931da177e4SLinus Torvalds err = -EINVAL; 10945d337b91SHugh Dickins spin_unlock(&swap_lock); 10951da177e4SLinus Torvalds goto out_dput; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 10981da177e4SLinus Torvalds vm_unacct_memory(p->pages); 10991da177e4SLinus Torvalds else { 11001da177e4SLinus Torvalds err = -ENOMEM; 11015d337b91SHugh Dickins spin_unlock(&swap_lock); 11021da177e4SLinus Torvalds goto out_dput; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds if (prev < 0) { 11051da177e4SLinus Torvalds swap_list.head = p->next; 11061da177e4SLinus Torvalds } else { 11071da177e4SLinus Torvalds swap_info[prev].next = p->next; 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds if (type == swap_list.next) { 11101da177e4SLinus Torvalds /* just pick something that's safe... */ 11111da177e4SLinus Torvalds swap_list.next = swap_list.head; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds nr_swap_pages -= p->pages; 11141da177e4SLinus Torvalds total_swap_pages -= p->pages; 11151da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 11165d337b91SHugh Dickins spin_unlock(&swap_lock); 1117fb4f88dcSHugh Dickins 11181da177e4SLinus Torvalds current->flags |= PF_SWAPOFF; 11191da177e4SLinus Torvalds err = try_to_unuse(type); 11201da177e4SLinus Torvalds current->flags &= ~PF_SWAPOFF; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds if (err) { 11231da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 11245d337b91SHugh Dickins spin_lock(&swap_lock); 11251da177e4SLinus Torvalds for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next) 11261da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) 11271da177e4SLinus Torvalds break; 11281da177e4SLinus Torvalds p->next = i; 11291da177e4SLinus Torvalds if (prev < 0) 11301da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 11311da177e4SLinus Torvalds else 11321da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 11331da177e4SLinus Torvalds nr_swap_pages += p->pages; 11341da177e4SLinus Torvalds total_swap_pages += p->pages; 11351da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 11365d337b91SHugh Dickins spin_unlock(&swap_lock); 11371da177e4SLinus Torvalds goto out_dput; 11381da177e4SLinus Torvalds } 113952b7efdbSHugh Dickins 114052b7efdbSHugh Dickins /* wait for any unplug function to finish */ 114152b7efdbSHugh Dickins down_write(&swap_unplug_sem); 114252b7efdbSHugh Dickins up_write(&swap_unplug_sem); 114352b7efdbSHugh Dickins 11444cd3bb10SHugh Dickins destroy_swap_extents(p); 11451da177e4SLinus Torvalds down(&swapon_sem); 11465d337b91SHugh Dickins spin_lock(&swap_lock); 11471da177e4SLinus Torvalds drain_mmlist(); 11485d337b91SHugh Dickins 11495d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 11505d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 11515d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 11525d337b91SHugh Dickins spin_unlock(&swap_lock); 115313e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 11545d337b91SHugh Dickins spin_lock(&swap_lock); 11555d337b91SHugh Dickins } 11565d337b91SHugh Dickins 11571da177e4SLinus Torvalds swap_file = p->swap_file; 11581da177e4SLinus Torvalds p->swap_file = NULL; 11591da177e4SLinus Torvalds p->max = 0; 11601da177e4SLinus Torvalds swap_map = p->swap_map; 11611da177e4SLinus Torvalds p->swap_map = NULL; 11621da177e4SLinus Torvalds p->flags = 0; 11635d337b91SHugh Dickins spin_unlock(&swap_lock); 11641da177e4SLinus Torvalds up(&swapon_sem); 11651da177e4SLinus Torvalds vfree(swap_map); 11661da177e4SLinus Torvalds inode = mapping->host; 11671da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 11681da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 11691da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 11701da177e4SLinus Torvalds bd_release(bdev); 11711da177e4SLinus Torvalds } else { 11721da177e4SLinus Torvalds down(&inode->i_sem); 11731da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 11741da177e4SLinus Torvalds up(&inode->i_sem); 11751da177e4SLinus Torvalds } 11761da177e4SLinus Torvalds filp_close(swap_file, NULL); 11771da177e4SLinus Torvalds err = 0; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds out_dput: 11801da177e4SLinus Torvalds filp_close(victim, NULL); 11811da177e4SLinus Torvalds out: 11821da177e4SLinus Torvalds return err; 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds 11851da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11861da177e4SLinus Torvalds /* iterator */ 11871da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 11881da177e4SLinus Torvalds { 11891da177e4SLinus Torvalds struct swap_info_struct *ptr = swap_info; 11901da177e4SLinus Torvalds int i; 11911da177e4SLinus Torvalds loff_t l = *pos; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds down(&swapon_sem); 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++, ptr++) { 11961da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 11971da177e4SLinus Torvalds continue; 11981da177e4SLinus Torvalds if (!l--) 11991da177e4SLinus Torvalds return ptr; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds return NULL; 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 12061da177e4SLinus Torvalds { 12071da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 12081da177e4SLinus Torvalds struct swap_info_struct *endptr = swap_info + nr_swapfiles; 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds for (++ptr; ptr < endptr; ptr++) { 12111da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 12121da177e4SLinus Torvalds continue; 12131da177e4SLinus Torvalds ++*pos; 12141da177e4SLinus Torvalds return ptr; 12151da177e4SLinus Torvalds } 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds return NULL; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 12211da177e4SLinus Torvalds { 12221da177e4SLinus Torvalds up(&swapon_sem); 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 12281da177e4SLinus Torvalds struct file *file; 12291da177e4SLinus Torvalds int len; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds if (v == swap_info) 12321da177e4SLinus Torvalds seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds file = ptr->swap_file; 12351da177e4SLinus Torvalds len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\"); 12366eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 12371da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 12381da177e4SLinus Torvalds S_ISBLK(file->f_dentry->d_inode->i_mode) ? 12391da177e4SLinus Torvalds "partition" : "file\t", 12401da177e4SLinus Torvalds ptr->pages << (PAGE_SHIFT - 10), 12411da177e4SLinus Torvalds ptr->inuse_pages << (PAGE_SHIFT - 10), 12421da177e4SLinus Torvalds ptr->prio); 12431da177e4SLinus Torvalds return 0; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds static struct seq_operations swaps_op = { 12471da177e4SLinus Torvalds .start = swap_start, 12481da177e4SLinus Torvalds .next = swap_next, 12491da177e4SLinus Torvalds .stop = swap_stop, 12501da177e4SLinus Torvalds .show = swap_show 12511da177e4SLinus Torvalds }; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 12541da177e4SLinus Torvalds { 12551da177e4SLinus Torvalds return seq_open(file, &swaps_op); 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12581da177e4SLinus Torvalds static struct file_operations proc_swaps_operations = { 12591da177e4SLinus Torvalds .open = swaps_open, 12601da177e4SLinus Torvalds .read = seq_read, 12611da177e4SLinus Torvalds .llseek = seq_lseek, 12621da177e4SLinus Torvalds .release = seq_release, 12631da177e4SLinus Torvalds }; 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds static int __init procswaps_init(void) 12661da177e4SLinus Torvalds { 12671da177e4SLinus Torvalds struct proc_dir_entry *entry; 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds entry = create_proc_entry("swaps", 0, NULL); 12701da177e4SLinus Torvalds if (entry) 12711da177e4SLinus Torvalds entry->proc_fops = &proc_swaps_operations; 12721da177e4SLinus Torvalds return 0; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds __initcall(procswaps_init); 12751da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 12791da177e4SLinus Torvalds * 12801da177e4SLinus Torvalds * The swapon system call 12811da177e4SLinus Torvalds */ 12821da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) 12831da177e4SLinus Torvalds { 12841da177e4SLinus Torvalds struct swap_info_struct * p; 12851da177e4SLinus Torvalds char *name = NULL; 12861da177e4SLinus Torvalds struct block_device *bdev = NULL; 12871da177e4SLinus Torvalds struct file *swap_file = NULL; 12881da177e4SLinus Torvalds struct address_space *mapping; 12891da177e4SLinus Torvalds unsigned int type; 12901da177e4SLinus Torvalds int i, prev; 12911da177e4SLinus Torvalds int error; 12921da177e4SLinus Torvalds static int least_priority; 12931da177e4SLinus Torvalds union swap_header *swap_header = NULL; 12941da177e4SLinus Torvalds int swap_header_version; 12956eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 12966eb396dcSHugh Dickins int nr_extents = 0; 129753092a74SHugh Dickins sector_t span; 12981da177e4SLinus Torvalds unsigned long maxpages = 1; 12991da177e4SLinus Torvalds int swapfilesize; 13001da177e4SLinus Torvalds unsigned short *swap_map; 13011da177e4SLinus Torvalds struct page *page = NULL; 13021da177e4SLinus Torvalds struct inode *inode = NULL; 13031da177e4SLinus Torvalds int did_down = 0; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13061da177e4SLinus Torvalds return -EPERM; 13075d337b91SHugh Dickins spin_lock(&swap_lock); 13081da177e4SLinus Torvalds p = swap_info; 13091da177e4SLinus Torvalds for (type = 0 ; type < nr_swapfiles ; type++,p++) 13101da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 13111da177e4SLinus Torvalds break; 13121da177e4SLinus Torvalds error = -EPERM; 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Test if adding another swap device is possible. There are 13151da177e4SLinus Torvalds * two limiting factors: 1) the number of bits for the swap 13161da177e4SLinus Torvalds * type swp_entry_t definition and 2) the number of bits for 13171da177e4SLinus Torvalds * the swap type in the swap ptes as defined by the different 13181da177e4SLinus Torvalds * architectures. To honor both limitations a swap entry 13191da177e4SLinus Torvalds * with swap offset 0 and swap type ~0UL is created, encoded 13201da177e4SLinus Torvalds * to a swap pte, decoded to a swp_entry_t again and finally 13211da177e4SLinus Torvalds * the swap type part is extracted. This will mask all bits 13221da177e4SLinus Torvalds * from the initial ~0UL that can't be encoded in either the 13231da177e4SLinus Torvalds * swp_entry_t or the architecture definition of a swap pte. 13241da177e4SLinus Torvalds */ 13251da177e4SLinus Torvalds if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) { 13265d337b91SHugh Dickins spin_unlock(&swap_lock); 13271da177e4SLinus Torvalds goto out; 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds if (type >= nr_swapfiles) 13301da177e4SLinus Torvalds nr_swapfiles = type+1; 13311da177e4SLinus Torvalds INIT_LIST_HEAD(&p->extent_list); 13321da177e4SLinus Torvalds p->flags = SWP_USED; 13331da177e4SLinus Torvalds p->swap_file = NULL; 13341da177e4SLinus Torvalds p->old_block_size = 0; 13351da177e4SLinus Torvalds p->swap_map = NULL; 13361da177e4SLinus Torvalds p->lowest_bit = 0; 13371da177e4SLinus Torvalds p->highest_bit = 0; 13381da177e4SLinus Torvalds p->cluster_nr = 0; 13391da177e4SLinus Torvalds p->inuse_pages = 0; 13401da177e4SLinus Torvalds p->next = -1; 13411da177e4SLinus Torvalds if (swap_flags & SWAP_FLAG_PREFER) { 13421da177e4SLinus Torvalds p->prio = 13431da177e4SLinus Torvalds (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT; 13441da177e4SLinus Torvalds } else { 13451da177e4SLinus Torvalds p->prio = --least_priority; 13461da177e4SLinus Torvalds } 13475d337b91SHugh Dickins spin_unlock(&swap_lock); 13481da177e4SLinus Torvalds name = getname(specialfile); 13491da177e4SLinus Torvalds error = PTR_ERR(name); 13501da177e4SLinus Torvalds if (IS_ERR(name)) { 13511da177e4SLinus Torvalds name = NULL; 13521da177e4SLinus Torvalds goto bad_swap_2; 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 13551da177e4SLinus Torvalds error = PTR_ERR(swap_file); 13561da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 13571da177e4SLinus Torvalds swap_file = NULL; 13581da177e4SLinus Torvalds goto bad_swap_2; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds p->swap_file = swap_file; 13621da177e4SLinus Torvalds mapping = swap_file->f_mapping; 13631da177e4SLinus Torvalds inode = mapping->host; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds error = -EBUSY; 13661da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 13671da177e4SLinus Torvalds struct swap_info_struct *q = &swap_info[i]; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds if (i == type || !q->swap_file) 13701da177e4SLinus Torvalds continue; 13711da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 13721da177e4SLinus Torvalds goto bad_swap; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds error = -EINVAL; 13761da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 13771da177e4SLinus Torvalds bdev = I_BDEV(inode); 13781da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 13791da177e4SLinus Torvalds if (error < 0) { 13801da177e4SLinus Torvalds bdev = NULL; 1381f7b3a435SRob Landley error = -EINVAL; 13821da177e4SLinus Torvalds goto bad_swap; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 13851da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 13861da177e4SLinus Torvalds if (error < 0) 13871da177e4SLinus Torvalds goto bad_swap; 13881da177e4SLinus Torvalds p->bdev = bdev; 13891da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 13901da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 13911da177e4SLinus Torvalds down(&inode->i_sem); 13921da177e4SLinus Torvalds did_down = 1; 13931da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 13941da177e4SLinus Torvalds error = -EBUSY; 13951da177e4SLinus Torvalds goto bad_swap; 13961da177e4SLinus Torvalds } 13971da177e4SLinus Torvalds } else { 13981da177e4SLinus Torvalds goto bad_swap; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds swapfilesize = i_size_read(inode) >> PAGE_SHIFT; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* 14041da177e4SLinus Torvalds * Read the swap header. 14051da177e4SLinus Torvalds */ 14061da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 14071da177e4SLinus Torvalds error = -EINVAL; 14081da177e4SLinus Torvalds goto bad_swap; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds page = read_cache_page(mapping, 0, 14111da177e4SLinus Torvalds (filler_t *)mapping->a_ops->readpage, swap_file); 14121da177e4SLinus Torvalds if (IS_ERR(page)) { 14131da177e4SLinus Torvalds error = PTR_ERR(page); 14141da177e4SLinus Torvalds goto bad_swap; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds wait_on_page_locked(page); 14171da177e4SLinus Torvalds if (!PageUptodate(page)) 14181da177e4SLinus Torvalds goto bad_swap; 14191da177e4SLinus Torvalds kmap(page); 14201da177e4SLinus Torvalds swap_header = page_address(page); 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10)) 14231da177e4SLinus Torvalds swap_header_version = 1; 14241da177e4SLinus Torvalds else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10)) 14251da177e4SLinus Torvalds swap_header_version = 2; 14261da177e4SLinus Torvalds else { 14271da177e4SLinus Torvalds printk("Unable to find swap-space signature\n"); 14281da177e4SLinus Torvalds error = -EINVAL; 14291da177e4SLinus Torvalds goto bad_swap; 14301da177e4SLinus Torvalds } 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds switch (swap_header_version) { 14331da177e4SLinus Torvalds case 1: 14341da177e4SLinus Torvalds printk(KERN_ERR "version 0 swap is no longer supported. " 14351da177e4SLinus Torvalds "Use mkswap -v1 %s\n", name); 14361da177e4SLinus Torvalds error = -EINVAL; 14371da177e4SLinus Torvalds goto bad_swap; 14381da177e4SLinus Torvalds case 2: 14391da177e4SLinus Torvalds /* Check the swap header's sub-version and the size of 14401da177e4SLinus Torvalds the swap file and bad block lists */ 14411da177e4SLinus Torvalds if (swap_header->info.version != 1) { 14421da177e4SLinus Torvalds printk(KERN_WARNING 14431da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 14441da177e4SLinus Torvalds swap_header->info.version); 14451da177e4SLinus Torvalds error = -EINVAL; 14461da177e4SLinus Torvalds goto bad_swap; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds p->lowest_bit = 1; 145052b7efdbSHugh Dickins p->cluster_next = 1; 145152b7efdbSHugh Dickins 14521da177e4SLinus Torvalds /* 14531da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 14541da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 14551da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 14561da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 14571da177e4SLinus Torvalds * the different architectures. In order to find the 14581da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 14591da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 14601da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 14611da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 14621da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 14631da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 14641da177e4SLinus Torvalds * swap pte. 14651da177e4SLinus Torvalds */ 14661da177e4SLinus Torvalds maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1; 14671da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 14681da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 14691da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds error = -EINVAL; 1472e2244ec2SHugh Dickins if (!maxpages) 1473e2244ec2SHugh Dickins goto bad_swap; 1474e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1475e2244ec2SHugh Dickins goto bad_swap; 14761da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 14771da177e4SLinus Torvalds goto bad_swap; 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 14801da177e4SLinus Torvalds if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) { 14811da177e4SLinus Torvalds error = -ENOMEM; 14821da177e4SLinus Torvalds goto bad_swap; 14831da177e4SLinus Torvalds } 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds error = 0; 14861da177e4SLinus Torvalds memset(p->swap_map, 0, maxpages * sizeof(short)); 14871da177e4SLinus Torvalds for (i=0; i<swap_header->info.nr_badpages; i++) { 14881da177e4SLinus Torvalds int page = swap_header->info.badpages[i]; 14891da177e4SLinus Torvalds if (page <= 0 || page >= swap_header->info.last_page) 14901da177e4SLinus Torvalds error = -EINVAL; 14911da177e4SLinus Torvalds else 14921da177e4SLinus Torvalds p->swap_map[page] = SWAP_MAP_BAD; 14931da177e4SLinus Torvalds } 14941da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 14951da177e4SLinus Torvalds swap_header->info.nr_badpages - 14961da177e4SLinus Torvalds 1 /* header page */; 14971da177e4SLinus Torvalds if (error) 14981da177e4SLinus Torvalds goto bad_swap; 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds 15011da177e4SLinus Torvalds if (swapfilesize && maxpages > swapfilesize) { 15021da177e4SLinus Torvalds printk(KERN_WARNING 15031da177e4SLinus Torvalds "Swap area shorter than signature indicates\n"); 15041da177e4SLinus Torvalds error = -EINVAL; 15051da177e4SLinus Torvalds goto bad_swap; 15061da177e4SLinus Torvalds } 1507e2244ec2SHugh Dickins if (nr_good_pages) { 1508e2244ec2SHugh Dickins p->swap_map[0] = SWAP_MAP_BAD; 1509e2244ec2SHugh Dickins p->max = maxpages; 1510e2244ec2SHugh Dickins p->pages = nr_good_pages; 151153092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 151253092a74SHugh Dickins if (nr_extents < 0) { 151353092a74SHugh Dickins error = nr_extents; 1514e2244ec2SHugh Dickins goto bad_swap; 151553092a74SHugh Dickins } 1516e2244ec2SHugh Dickins nr_good_pages = p->pages; 1517e2244ec2SHugh Dickins } 15181da177e4SLinus Torvalds if (!nr_good_pages) { 15191da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 15201da177e4SLinus Torvalds error = -EINVAL; 15211da177e4SLinus Torvalds goto bad_swap; 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds 15241da177e4SLinus Torvalds down(&swapon_sem); 15255d337b91SHugh Dickins spin_lock(&swap_lock); 15261da177e4SLinus Torvalds p->flags = SWP_ACTIVE; 15271da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 15281da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 152953092a74SHugh Dickins 15306eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 153153092a74SHugh Dickins "Priority:%d extents:%d across:%lluk\n", 153253092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 153353092a74SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10)); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds /* insert swap space into swap_list: */ 15361da177e4SLinus Torvalds prev = -1; 15371da177e4SLinus Torvalds for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 15381da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) { 15391da177e4SLinus Torvalds break; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds prev = i; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds p->next = i; 15441da177e4SLinus Torvalds if (prev < 0) { 15451da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 15461da177e4SLinus Torvalds } else { 15471da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 15481da177e4SLinus Torvalds } 15495d337b91SHugh Dickins spin_unlock(&swap_lock); 15501da177e4SLinus Torvalds up(&swapon_sem); 15511da177e4SLinus Torvalds error = 0; 15521da177e4SLinus Torvalds goto out; 15531da177e4SLinus Torvalds bad_swap: 15541da177e4SLinus Torvalds if (bdev) { 15551da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 15561da177e4SLinus Torvalds bd_release(bdev); 15571da177e4SLinus Torvalds } 15584cd3bb10SHugh Dickins destroy_swap_extents(p); 15591da177e4SLinus Torvalds bad_swap_2: 15605d337b91SHugh Dickins spin_lock(&swap_lock); 15611da177e4SLinus Torvalds swap_map = p->swap_map; 15621da177e4SLinus Torvalds p->swap_file = NULL; 15631da177e4SLinus Torvalds p->swap_map = NULL; 15641da177e4SLinus Torvalds p->flags = 0; 15651da177e4SLinus Torvalds if (!(swap_flags & SWAP_FLAG_PREFER)) 15661da177e4SLinus Torvalds ++least_priority; 15675d337b91SHugh Dickins spin_unlock(&swap_lock); 15681da177e4SLinus Torvalds vfree(swap_map); 15691da177e4SLinus Torvalds if (swap_file) 15701da177e4SLinus Torvalds filp_close(swap_file, NULL); 15711da177e4SLinus Torvalds out: 15721da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 15731da177e4SLinus Torvalds kunmap(page); 15741da177e4SLinus Torvalds page_cache_release(page); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds if (name) 15771da177e4SLinus Torvalds putname(name); 15781da177e4SLinus Torvalds if (did_down) { 15791da177e4SLinus Torvalds if (!error) 15801da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 15811da177e4SLinus Torvalds up(&inode->i_sem); 15821da177e4SLinus Torvalds } 15831da177e4SLinus Torvalds return error; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds unsigned int i; 15891da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 15901da177e4SLinus Torvalds 15915d337b91SHugh Dickins spin_lock(&swap_lock); 15921da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 15931da177e4SLinus Torvalds if (!(swap_info[i].flags & SWP_USED) || 15941da177e4SLinus Torvalds (swap_info[i].flags & SWP_WRITEOK)) 15951da177e4SLinus Torvalds continue; 15961da177e4SLinus Torvalds nr_to_be_unused += swap_info[i].inuse_pages; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 15991da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 16005d337b91SHugh Dickins spin_unlock(&swap_lock); 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 16031da177e4SLinus Torvalds /* 16041da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 16051da177e4SLinus Torvalds * 16061da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 16071da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 16081da177e4SLinus Torvalds */ 16091da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry) 16101da177e4SLinus Torvalds { 16111da177e4SLinus Torvalds struct swap_info_struct * p; 16121da177e4SLinus Torvalds unsigned long offset, type; 16131da177e4SLinus Torvalds int result = 0; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds type = swp_type(entry); 16161da177e4SLinus Torvalds if (type >= nr_swapfiles) 16171da177e4SLinus Torvalds goto bad_file; 16181da177e4SLinus Torvalds p = type + swap_info; 16191da177e4SLinus Torvalds offset = swp_offset(entry); 16201da177e4SLinus Torvalds 16215d337b91SHugh Dickins spin_lock(&swap_lock); 16221da177e4SLinus Torvalds if (offset < p->max && p->swap_map[offset]) { 16231da177e4SLinus Torvalds if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { 16241da177e4SLinus Torvalds p->swap_map[offset]++; 16251da177e4SLinus Torvalds result = 1; 16261da177e4SLinus Torvalds } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { 16271da177e4SLinus Torvalds if (swap_overflow++ < 5) 16281da177e4SLinus Torvalds printk(KERN_WARNING "swap_dup: swap entry overflow\n"); 16291da177e4SLinus Torvalds p->swap_map[offset] = SWAP_MAP_MAX; 16301da177e4SLinus Torvalds result = 1; 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds } 16335d337b91SHugh Dickins spin_unlock(&swap_lock); 16341da177e4SLinus Torvalds out: 16351da177e4SLinus Torvalds return result; 16361da177e4SLinus Torvalds 16371da177e4SLinus Torvalds bad_file: 16381da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 16391da177e4SLinus Torvalds goto out; 16401da177e4SLinus Torvalds } 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds struct swap_info_struct * 16431da177e4SLinus Torvalds get_swap_info_struct(unsigned type) 16441da177e4SLinus Torvalds { 16451da177e4SLinus Torvalds return &swap_info[type]; 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* 16495d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 16501da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 16511da177e4SLinus Torvalds */ 16521da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 16531da177e4SLinus Torvalds { 16541da177e4SLinus Torvalds int ret = 0, i = 1 << page_cluster; 16551da177e4SLinus Torvalds unsigned long toff; 16561da177e4SLinus Torvalds struct swap_info_struct *swapdev = swp_type(entry) + swap_info; 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds if (!page_cluster) /* no readahead */ 16591da177e4SLinus Torvalds return 0; 16601da177e4SLinus Torvalds toff = (swp_offset(entry) >> page_cluster) << page_cluster; 16611da177e4SLinus Torvalds if (!toff) /* first page is swap header */ 16621da177e4SLinus Torvalds toff++, i--; 16631da177e4SLinus Torvalds *offset = toff; 16641da177e4SLinus Torvalds 16655d337b91SHugh Dickins spin_lock(&swap_lock); 16661da177e4SLinus Torvalds do { 16671da177e4SLinus Torvalds /* Don't read-ahead past the end of the swap area */ 16681da177e4SLinus Torvalds if (toff >= swapdev->max) 16691da177e4SLinus Torvalds break; 16701da177e4SLinus Torvalds /* Don't read in free or bad pages */ 16711da177e4SLinus Torvalds if (!swapdev->swap_map[toff]) 16721da177e4SLinus Torvalds break; 16731da177e4SLinus Torvalds if (swapdev->swap_map[toff] == SWAP_MAP_BAD) 16741da177e4SLinus Torvalds break; 16751da177e4SLinus Torvalds toff++; 16761da177e4SLinus Torvalds ret++; 16771da177e4SLinus Torvalds } while (--i); 16785d337b91SHugh Dickins spin_unlock(&swap_lock); 16791da177e4SLinus Torvalds return ret; 16801da177e4SLinus Torvalds } 1681