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 * vma->vm_mm->page_table_lock is held. 4061da177e4SLinus Torvalds */ 4071da177e4SLinus Torvalds static void unuse_pte(struct vm_area_struct *vma, pte_t *pte, 4081da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 4091da177e4SLinus Torvalds { 410*4294621fSHugh Dickins inc_mm_counter(vma->vm_mm, anon_rss); 4111da177e4SLinus Torvalds get_page(page); 4121da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 4131da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 4141da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 4151da177e4SLinus Torvalds swap_free(entry); 4161da177e4SLinus Torvalds /* 4171da177e4SLinus Torvalds * Move the page to the active list so it is not 4181da177e4SLinus Torvalds * immediately swapped out again after swapon. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds activate_page(page); 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 4241da177e4SLinus Torvalds unsigned long addr, unsigned long end, 4251da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds pte_t *pte; 4281da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds pte = pte_offset_map(pmd, addr); 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))) { 4371da177e4SLinus Torvalds unuse_pte(vma, pte, addr, entry, page); 4381da177e4SLinus Torvalds pte_unmap(pte); 4391da177e4SLinus Torvalds return 1; 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 4421da177e4SLinus Torvalds pte_unmap(pte - 1); 4431da177e4SLinus Torvalds return 0; 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 spin_lock(&mm->page_table_lock); 5261da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 5271da177e4SLinus Torvalds if (vma->anon_vma && unuse_vma(vma, entry, page)) 5281da177e4SLinus Torvalds break; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds spin_unlock(&mm->page_table_lock); 5311da177e4SLinus Torvalds up_read(&mm->mmap_sem); 5321da177e4SLinus Torvalds /* 5331da177e4SLinus Torvalds * Currently unuse_mm cannot fail, but leave error handling 5341da177e4SLinus Torvalds * at call sites for now, since we change it from time to time. 5351da177e4SLinus Torvalds */ 5361da177e4SLinus Torvalds return 0; 5371da177e4SLinus Torvalds } 5381da177e4SLinus Torvalds 5391da177e4SLinus Torvalds /* 5401da177e4SLinus Torvalds * Scan swap_map from current position to next entry still in use. 5411da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 5421da177e4SLinus Torvalds */ 5436eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 5446eb396dcSHugh Dickins unsigned int prev) 5451da177e4SLinus Torvalds { 5466eb396dcSHugh Dickins unsigned int max = si->max; 5476eb396dcSHugh Dickins unsigned int i = prev; 5481da177e4SLinus Torvalds int count; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /* 5515d337b91SHugh Dickins * No need for swap_lock here: we're just looking 5521da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 5531da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 5545d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds for (;;) { 5571da177e4SLinus Torvalds if (++i >= max) { 5581da177e4SLinus Torvalds if (!prev) { 5591da177e4SLinus Torvalds i = 0; 5601da177e4SLinus Torvalds break; 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds /* 5631da177e4SLinus Torvalds * No entries in use at top of swap_map, 5641da177e4SLinus Torvalds * loop back to start and recheck there. 5651da177e4SLinus Torvalds */ 5661da177e4SLinus Torvalds max = prev + 1; 5671da177e4SLinus Torvalds prev = 0; 5681da177e4SLinus Torvalds i = 1; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds count = si->swap_map[i]; 5711da177e4SLinus Torvalds if (count && count != SWAP_MAP_BAD) 5721da177e4SLinus Torvalds break; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds return i; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds /* 5781da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 5791da177e4SLinus Torvalds * and then search for the process using it. All the necessary 5801da177e4SLinus Torvalds * page table adjustments can then be made atomically. 5811da177e4SLinus Torvalds */ 5821da177e4SLinus Torvalds static int try_to_unuse(unsigned int type) 5831da177e4SLinus Torvalds { 5841da177e4SLinus Torvalds struct swap_info_struct * si = &swap_info[type]; 5851da177e4SLinus Torvalds struct mm_struct *start_mm; 5861da177e4SLinus Torvalds unsigned short *swap_map; 5871da177e4SLinus Torvalds unsigned short swcount; 5881da177e4SLinus Torvalds struct page *page; 5891da177e4SLinus Torvalds swp_entry_t entry; 5906eb396dcSHugh Dickins unsigned int i = 0; 5911da177e4SLinus Torvalds int retval = 0; 5921da177e4SLinus Torvalds int reset_overflow = 0; 5931da177e4SLinus Torvalds int shmem; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /* 5961da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 5971da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 5981da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 5991da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 6001da177e4SLinus Torvalds * 6011da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 6021da177e4SLinus Torvalds * freed the previous entry from; but that would take less 6031da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 6041da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 6051da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 6061da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 6071da177e4SLinus Torvalds * that. Though it's only a serious concern when an overflowed 6081da177e4SLinus Torvalds * swap count is reset from SWAP_MAP_MAX, preventing a rescan. 6091da177e4SLinus Torvalds */ 6101da177e4SLinus Torvalds start_mm = &init_mm; 6111da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds /* 6141da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 6151da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 6161da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 6171da177e4SLinus Torvalds */ 6181da177e4SLinus Torvalds while ((i = find_next_to_unuse(si, i)) != 0) { 6191da177e4SLinus Torvalds if (signal_pending(current)) { 6201da177e4SLinus Torvalds retval = -EINTR; 6211da177e4SLinus Torvalds break; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds /* 6251da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 6261da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 6271da177e4SLinus Torvalds * page and read the swap into it. 6281da177e4SLinus Torvalds */ 6291da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 6301da177e4SLinus Torvalds entry = swp_entry(type, i); 6311da177e4SLinus Torvalds page = read_swap_cache_async(entry, NULL, 0); 6321da177e4SLinus Torvalds if (!page) { 6331da177e4SLinus Torvalds /* 6341da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 6351da177e4SLinus Torvalds * has been freed independently, and will not be 6361da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 6371da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 6381da177e4SLinus Torvalds */ 6391da177e4SLinus Torvalds if (!*swap_map) 6401da177e4SLinus Torvalds continue; 6411da177e4SLinus Torvalds retval = -ENOMEM; 6421da177e4SLinus Torvalds break; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* 6461da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 6471da177e4SLinus Torvalds */ 6481da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 6491da177e4SLinus Torvalds mmput(start_mm); 6501da177e4SLinus Torvalds start_mm = &init_mm; 6511da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds /* 6551da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 6561da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 6571da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 6581da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 6591da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 6601da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 6611da177e4SLinus Torvalds */ 6621da177e4SLinus Torvalds wait_on_page_locked(page); 6631da177e4SLinus Torvalds wait_on_page_writeback(page); 6641da177e4SLinus Torvalds lock_page(page); 6651da177e4SLinus Torvalds wait_on_page_writeback(page); 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds /* 6681da177e4SLinus Torvalds * Remove all references to entry. 6691da177e4SLinus Torvalds * Whenever we reach init_mm, there's no address space 6701da177e4SLinus Torvalds * to search, but use it as a reminder to search shmem. 6711da177e4SLinus Torvalds */ 6721da177e4SLinus Torvalds shmem = 0; 6731da177e4SLinus Torvalds swcount = *swap_map; 6741da177e4SLinus Torvalds if (swcount > 1) { 6751da177e4SLinus Torvalds if (start_mm == &init_mm) 6761da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 6771da177e4SLinus Torvalds else 6781da177e4SLinus Torvalds retval = unuse_mm(start_mm, entry, page); 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds if (*swap_map > 1) { 6811da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 6821da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 6831da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 6841da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 6851da177e4SLinus Torvalds struct mm_struct *mm; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 6881da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 6891da177e4SLinus Torvalds spin_lock(&mmlist_lock); 6901da177e4SLinus Torvalds while (*swap_map > 1 && !retval && 6911da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 6921da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 6931da177e4SLinus Torvalds if (atomic_inc_return(&mm->mm_users) == 1) { 6941da177e4SLinus Torvalds atomic_dec(&mm->mm_users); 6951da177e4SLinus Torvalds continue; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 6981da177e4SLinus Torvalds mmput(prev_mm); 6991da177e4SLinus Torvalds prev_mm = mm; 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds cond_resched(); 7021da177e4SLinus Torvalds 7031da177e4SLinus Torvalds swcount = *swap_map; 7041da177e4SLinus Torvalds if (swcount <= 1) 7051da177e4SLinus Torvalds ; 7061da177e4SLinus Torvalds else if (mm == &init_mm) { 7071da177e4SLinus Torvalds set_start_mm = 1; 7081da177e4SLinus Torvalds shmem = shmem_unuse(entry, page); 7091da177e4SLinus Torvalds } else 7101da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 7111da177e4SLinus Torvalds if (set_start_mm && *swap_map < swcount) { 7121da177e4SLinus Torvalds mmput(new_start_mm); 7131da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 7141da177e4SLinus Torvalds new_start_mm = mm; 7151da177e4SLinus Torvalds set_start_mm = 0; 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds spin_lock(&mmlist_lock); 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 7201da177e4SLinus Torvalds mmput(prev_mm); 7211da177e4SLinus Torvalds mmput(start_mm); 7221da177e4SLinus Torvalds start_mm = new_start_mm; 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds if (retval) { 7251da177e4SLinus Torvalds unlock_page(page); 7261da177e4SLinus Torvalds page_cache_release(page); 7271da177e4SLinus Torvalds break; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /* 7311da177e4SLinus Torvalds * How could swap count reach 0x7fff when the maximum 7321da177e4SLinus Torvalds * pid is 0x7fff, and there's no way to repeat a swap 7331da177e4SLinus Torvalds * page within an mm (except in shmem, where it's the 7341da177e4SLinus Torvalds * shared object which takes the reference count)? 7351da177e4SLinus Torvalds * We believe SWAP_MAP_MAX cannot occur in Linux 2.4. 7361da177e4SLinus Torvalds * 7371da177e4SLinus Torvalds * If that's wrong, then we should worry more about 7381da177e4SLinus Torvalds * exit_mmap() and do_munmap() cases described above: 7391da177e4SLinus Torvalds * we might be resetting SWAP_MAP_MAX too early here. 7401da177e4SLinus Torvalds * We know "Undead"s can happen, they're okay, so don't 7411da177e4SLinus Torvalds * report them; but do report if we reset SWAP_MAP_MAX. 7421da177e4SLinus Torvalds */ 7431da177e4SLinus Torvalds if (*swap_map == SWAP_MAP_MAX) { 7445d337b91SHugh Dickins spin_lock(&swap_lock); 7451da177e4SLinus Torvalds *swap_map = 1; 7465d337b91SHugh Dickins spin_unlock(&swap_lock); 7471da177e4SLinus Torvalds reset_overflow = 1; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* 7511da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 7521da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 7531da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 7541da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 7551da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 7561da177e4SLinus Torvalds * delete from cache even if there's another reference, 7571da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 7581da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 7591da177e4SLinus Torvalds * read from disk into another page. Splitting into two 7601da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 7611da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 7621da177e4SLinus Torvalds * 7631da177e4SLinus Torvalds * Note shmem_unuse already deleted a swappage from 7641da177e4SLinus Torvalds * the swap cache, unless the move to filepage failed: 7651da177e4SLinus Torvalds * in which case it left swappage in cache, lowered its 7661da177e4SLinus Torvalds * swap count to pass quickly through the loops above, 7671da177e4SLinus Torvalds * and now we must reincrement count to try again later. 7681da177e4SLinus Torvalds */ 7691da177e4SLinus Torvalds if ((*swap_map > 1) && PageDirty(page) && PageSwapCache(page)) { 7701da177e4SLinus Torvalds struct writeback_control wbc = { 7711da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 7721da177e4SLinus Torvalds }; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds swap_writepage(page, &wbc); 7751da177e4SLinus Torvalds lock_page(page); 7761da177e4SLinus Torvalds wait_on_page_writeback(page); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds if (PageSwapCache(page)) { 7791da177e4SLinus Torvalds if (shmem) 7801da177e4SLinus Torvalds swap_duplicate(entry); 7811da177e4SLinus Torvalds else 7821da177e4SLinus Torvalds delete_from_swap_cache(page); 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds /* 7861da177e4SLinus Torvalds * So we could skip searching mms once swap count went 7871da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 7881da177e4SLinus Torvalds * mark page dirty so shrink_list will preserve it. 7891da177e4SLinus Torvalds */ 7901da177e4SLinus Torvalds SetPageDirty(page); 7911da177e4SLinus Torvalds unlock_page(page); 7921da177e4SLinus Torvalds page_cache_release(page); 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds /* 7951da177e4SLinus Torvalds * Make sure that we aren't completely killing 7961da177e4SLinus Torvalds * interactive performance. 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds cond_resched(); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds mmput(start_mm); 8021da177e4SLinus Torvalds if (reset_overflow) { 8031da177e4SLinus Torvalds printk(KERN_WARNING "swapoff: cleared swap entry overflow\n"); 8041da177e4SLinus Torvalds swap_overflow = 0; 8051da177e4SLinus Torvalds } 8061da177e4SLinus Torvalds return retval; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds /* 8105d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 8115d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 8125d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 8131da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds static void drain_mmlist(void) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds struct list_head *p, *next; 8181da177e4SLinus Torvalds unsigned int i; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) 8211da177e4SLinus Torvalds if (swap_info[i].inuse_pages) 8221da177e4SLinus Torvalds return; 8231da177e4SLinus Torvalds spin_lock(&mmlist_lock); 8241da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 8251da177e4SLinus Torvalds list_del_init(p); 8261da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /* 8301da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 8311da177e4SLinus Torvalds * corresponds to page offset `offset'. 8321da177e4SLinus Torvalds */ 8331da177e4SLinus Torvalds sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset) 8341da177e4SLinus Torvalds { 8351da177e4SLinus Torvalds struct swap_extent *se = sis->curr_swap_extent; 8361da177e4SLinus Torvalds struct swap_extent *start_se = se; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds for ( ; ; ) { 8391da177e4SLinus Torvalds struct list_head *lh; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds if (se->start_page <= offset && 8421da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 8431da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 8441da177e4SLinus Torvalds } 84511d31886SHugh Dickins lh = se->list.next; 8461da177e4SLinus Torvalds if (lh == &sis->extent_list) 84711d31886SHugh Dickins lh = lh->next; 8481da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 8491da177e4SLinus Torvalds sis->curr_swap_extent = se; 8501da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 8511da177e4SLinus Torvalds } 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds /* 8551da177e4SLinus Torvalds * Free all of a swapdev's extent information 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 8581da177e4SLinus Torvalds { 8591da177e4SLinus Torvalds while (!list_empty(&sis->extent_list)) { 8601da177e4SLinus Torvalds struct swap_extent *se; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds se = list_entry(sis->extent_list.next, 8631da177e4SLinus Torvalds struct swap_extent, list); 8641da177e4SLinus Torvalds list_del(&se->list); 8651da177e4SLinus Torvalds kfree(se); 8661da177e4SLinus Torvalds } 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds /* 8701da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 87111d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 8721da177e4SLinus Torvalds * 87311d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 8741da177e4SLinus Torvalds */ 8751da177e4SLinus Torvalds static int 8761da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 8771da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 8781da177e4SLinus Torvalds { 8791da177e4SLinus Torvalds struct swap_extent *se; 8801da177e4SLinus Torvalds struct swap_extent *new_se; 8811da177e4SLinus Torvalds struct list_head *lh; 8821da177e4SLinus Torvalds 88311d31886SHugh Dickins lh = sis->extent_list.prev; /* The highest page extent */ 88411d31886SHugh Dickins if (lh != &sis->extent_list) { 8851da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 88611d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 88711d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 8881da177e4SLinus Torvalds /* Merge it */ 8891da177e4SLinus Torvalds se->nr_pages += nr_pages; 8901da177e4SLinus Torvalds return 0; 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds /* 8951da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 8961da177e4SLinus Torvalds */ 8971da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 8981da177e4SLinus Torvalds if (new_se == NULL) 8991da177e4SLinus Torvalds return -ENOMEM; 9001da177e4SLinus Torvalds new_se->start_page = start_page; 9011da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 9021da177e4SLinus Torvalds new_se->start_block = start_block; 9031da177e4SLinus Torvalds 90411d31886SHugh Dickins list_add_tail(&new_se->list, &sis->extent_list); 90553092a74SHugh Dickins return 1; 9061da177e4SLinus Torvalds } 9071da177e4SLinus Torvalds 9081da177e4SLinus Torvalds /* 9091da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 9101da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 9111da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 9121da177e4SLinus Torvalds * time for locating where on disk a page belongs. 9131da177e4SLinus Torvalds * 9141da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 9151da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 9161da177e4SLinus Torvalds * swap files identically. 9171da177e4SLinus Torvalds * 9181da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 9191da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 9201da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 9211da177e4SLinus Torvalds * 9221da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 9231da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 9241da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 9251da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 9261da177e4SLinus Torvalds * for swapping. 9271da177e4SLinus Torvalds * 928b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 9291da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 9301da177e4SLinus Torvalds * which will scribble on the fs. 9311da177e4SLinus Torvalds * 9321da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 9331da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 9341da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 9351da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 9361da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 9371da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 9381da177e4SLinus Torvalds */ 93953092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 9401da177e4SLinus Torvalds { 9411da177e4SLinus Torvalds struct inode *inode; 9421da177e4SLinus Torvalds unsigned blocks_per_page; 9431da177e4SLinus Torvalds unsigned long page_no; 9441da177e4SLinus Torvalds unsigned blkbits; 9451da177e4SLinus Torvalds sector_t probe_block; 9461da177e4SLinus Torvalds sector_t last_block; 94753092a74SHugh Dickins sector_t lowest_block = -1; 94853092a74SHugh Dickins sector_t highest_block = 0; 94953092a74SHugh Dickins int nr_extents = 0; 9501da177e4SLinus Torvalds int ret; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds inode = sis->swap_file->f_mapping->host; 9531da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 9541da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 95553092a74SHugh Dickins *span = sis->pages; 9561da177e4SLinus Torvalds goto done; 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds blkbits = inode->i_blkbits; 9601da177e4SLinus Torvalds blocks_per_page = PAGE_SIZE >> blkbits; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* 9631da177e4SLinus Torvalds * Map all the blocks into the extent list. This code doesn't try 9641da177e4SLinus Torvalds * to be very smart. 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds probe_block = 0; 9671da177e4SLinus Torvalds page_no = 0; 9681da177e4SLinus Torvalds last_block = i_size_read(inode) >> blkbits; 9691da177e4SLinus Torvalds while ((probe_block + blocks_per_page) <= last_block && 9701da177e4SLinus Torvalds page_no < sis->max) { 9711da177e4SLinus Torvalds unsigned block_in_page; 9721da177e4SLinus Torvalds sector_t first_block; 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds first_block = bmap(inode, probe_block); 9751da177e4SLinus Torvalds if (first_block == 0) 9761da177e4SLinus Torvalds goto bad_bmap; 9771da177e4SLinus Torvalds 9781da177e4SLinus Torvalds /* 9791da177e4SLinus Torvalds * It must be PAGE_SIZE aligned on-disk 9801da177e4SLinus Torvalds */ 9811da177e4SLinus Torvalds if (first_block & (blocks_per_page - 1)) { 9821da177e4SLinus Torvalds probe_block++; 9831da177e4SLinus Torvalds goto reprobe; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds for (block_in_page = 1; block_in_page < blocks_per_page; 9871da177e4SLinus Torvalds block_in_page++) { 9881da177e4SLinus Torvalds sector_t block; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds block = bmap(inode, probe_block + block_in_page); 9911da177e4SLinus Torvalds if (block == 0) 9921da177e4SLinus Torvalds goto bad_bmap; 9931da177e4SLinus Torvalds if (block != first_block + block_in_page) { 9941da177e4SLinus Torvalds /* Discontiguity */ 9951da177e4SLinus Torvalds probe_block++; 9961da177e4SLinus Torvalds goto reprobe; 9971da177e4SLinus Torvalds } 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 100053092a74SHugh Dickins first_block >>= (PAGE_SHIFT - blkbits); 100153092a74SHugh Dickins if (page_no) { /* exclude the header page */ 100253092a74SHugh Dickins if (first_block < lowest_block) 100353092a74SHugh Dickins lowest_block = first_block; 100453092a74SHugh Dickins if (first_block > highest_block) 100553092a74SHugh Dickins highest_block = first_block; 100653092a74SHugh Dickins } 100753092a74SHugh Dickins 10081da177e4SLinus Torvalds /* 10091da177e4SLinus Torvalds * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks 10101da177e4SLinus Torvalds */ 101153092a74SHugh Dickins ret = add_swap_extent(sis, page_no, 1, first_block); 101253092a74SHugh Dickins if (ret < 0) 10131da177e4SLinus Torvalds goto out; 101453092a74SHugh Dickins nr_extents += ret; 10151da177e4SLinus Torvalds page_no++; 10161da177e4SLinus Torvalds probe_block += blocks_per_page; 10171da177e4SLinus Torvalds reprobe: 10181da177e4SLinus Torvalds continue; 10191da177e4SLinus Torvalds } 102053092a74SHugh Dickins ret = nr_extents; 102153092a74SHugh Dickins *span = 1 + highest_block - lowest_block; 10221da177e4SLinus Torvalds if (page_no == 0) 1023e2244ec2SHugh Dickins page_no = 1; /* force Empty message */ 10241da177e4SLinus Torvalds sis->max = page_no; 1025e2244ec2SHugh Dickins sis->pages = page_no - 1; 10261da177e4SLinus Torvalds sis->highest_bit = page_no - 1; 10271da177e4SLinus Torvalds done: 10281da177e4SLinus Torvalds sis->curr_swap_extent = list_entry(sis->extent_list.prev, 10291da177e4SLinus Torvalds struct swap_extent, list); 10301da177e4SLinus Torvalds goto out; 10311da177e4SLinus Torvalds bad_bmap: 10321da177e4SLinus Torvalds printk(KERN_ERR "swapon: swapfile has holes\n"); 10331da177e4SLinus Torvalds ret = -EINVAL; 10341da177e4SLinus Torvalds out: 10351da177e4SLinus Torvalds return ret; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds #if 0 /* We don't need this yet */ 10391da177e4SLinus Torvalds #include <linux/backing-dev.h> 10401da177e4SLinus Torvalds int page_queue_congested(struct page *page) 10411da177e4SLinus Torvalds { 10421da177e4SLinus Torvalds struct backing_dev_info *bdi; 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); /* It pins the swap_info_struct */ 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (PageSwapCache(page)) { 10471da177e4SLinus Torvalds swp_entry_t entry = { .val = page->private }; 10481da177e4SLinus Torvalds struct swap_info_struct *sis; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds sis = get_swap_info_struct(swp_type(entry)); 10511da177e4SLinus Torvalds bdi = sis->bdev->bd_inode->i_mapping->backing_dev_info; 10521da177e4SLinus Torvalds } else 10531da177e4SLinus Torvalds bdi = page->mapping->backing_dev_info; 10541da177e4SLinus Torvalds return bdi_write_congested(bdi); 10551da177e4SLinus Torvalds } 10561da177e4SLinus Torvalds #endif 10571da177e4SLinus Torvalds 10581da177e4SLinus Torvalds asmlinkage long sys_swapoff(const char __user * specialfile) 10591da177e4SLinus Torvalds { 10601da177e4SLinus Torvalds struct swap_info_struct * p = NULL; 10611da177e4SLinus Torvalds unsigned short *swap_map; 10621da177e4SLinus Torvalds struct file *swap_file, *victim; 10631da177e4SLinus Torvalds struct address_space *mapping; 10641da177e4SLinus Torvalds struct inode *inode; 10651da177e4SLinus Torvalds char * pathname; 10661da177e4SLinus Torvalds int i, type, prev; 10671da177e4SLinus Torvalds int err; 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 10701da177e4SLinus Torvalds return -EPERM; 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds pathname = getname(specialfile); 10731da177e4SLinus Torvalds err = PTR_ERR(pathname); 10741da177e4SLinus Torvalds if (IS_ERR(pathname)) 10751da177e4SLinus Torvalds goto out; 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); 10781da177e4SLinus Torvalds putname(pathname); 10791da177e4SLinus Torvalds err = PTR_ERR(victim); 10801da177e4SLinus Torvalds if (IS_ERR(victim)) 10811da177e4SLinus Torvalds goto out; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds mapping = victim->f_mapping; 10841da177e4SLinus Torvalds prev = -1; 10855d337b91SHugh Dickins spin_lock(&swap_lock); 10861da177e4SLinus Torvalds for (type = swap_list.head; type >= 0; type = swap_info[type].next) { 10871da177e4SLinus Torvalds p = swap_info + type; 10881da177e4SLinus Torvalds if ((p->flags & SWP_ACTIVE) == SWP_ACTIVE) { 10891da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 10901da177e4SLinus Torvalds break; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds prev = type; 10931da177e4SLinus Torvalds } 10941da177e4SLinus Torvalds if (type < 0) { 10951da177e4SLinus Torvalds err = -EINVAL; 10965d337b91SHugh Dickins spin_unlock(&swap_lock); 10971da177e4SLinus Torvalds goto out_dput; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds if (!security_vm_enough_memory(p->pages)) 11001da177e4SLinus Torvalds vm_unacct_memory(p->pages); 11011da177e4SLinus Torvalds else { 11021da177e4SLinus Torvalds err = -ENOMEM; 11035d337b91SHugh Dickins spin_unlock(&swap_lock); 11041da177e4SLinus Torvalds goto out_dput; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds if (prev < 0) { 11071da177e4SLinus Torvalds swap_list.head = p->next; 11081da177e4SLinus Torvalds } else { 11091da177e4SLinus Torvalds swap_info[prev].next = p->next; 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds if (type == swap_list.next) { 11121da177e4SLinus Torvalds /* just pick something that's safe... */ 11131da177e4SLinus Torvalds swap_list.next = swap_list.head; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds nr_swap_pages -= p->pages; 11161da177e4SLinus Torvalds total_swap_pages -= p->pages; 11171da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 11185d337b91SHugh Dickins spin_unlock(&swap_lock); 1119fb4f88dcSHugh Dickins 11201da177e4SLinus Torvalds current->flags |= PF_SWAPOFF; 11211da177e4SLinus Torvalds err = try_to_unuse(type); 11221da177e4SLinus Torvalds current->flags &= ~PF_SWAPOFF; 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds if (err) { 11251da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 11265d337b91SHugh Dickins spin_lock(&swap_lock); 11271da177e4SLinus Torvalds for (prev = -1, i = swap_list.head; i >= 0; prev = i, i = swap_info[i].next) 11281da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) 11291da177e4SLinus Torvalds break; 11301da177e4SLinus Torvalds p->next = i; 11311da177e4SLinus Torvalds if (prev < 0) 11321da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 11331da177e4SLinus Torvalds else 11341da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 11351da177e4SLinus Torvalds nr_swap_pages += p->pages; 11361da177e4SLinus Torvalds total_swap_pages += p->pages; 11371da177e4SLinus Torvalds p->flags |= SWP_WRITEOK; 11385d337b91SHugh Dickins spin_unlock(&swap_lock); 11391da177e4SLinus Torvalds goto out_dput; 11401da177e4SLinus Torvalds } 114152b7efdbSHugh Dickins 114252b7efdbSHugh Dickins /* wait for any unplug function to finish */ 114352b7efdbSHugh Dickins down_write(&swap_unplug_sem); 114452b7efdbSHugh Dickins up_write(&swap_unplug_sem); 114552b7efdbSHugh Dickins 11464cd3bb10SHugh Dickins destroy_swap_extents(p); 11471da177e4SLinus Torvalds down(&swapon_sem); 11485d337b91SHugh Dickins spin_lock(&swap_lock); 11491da177e4SLinus Torvalds drain_mmlist(); 11505d337b91SHugh Dickins 11515d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 11525d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 11535d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 11545d337b91SHugh Dickins spin_unlock(&swap_lock); 115513e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 11565d337b91SHugh Dickins spin_lock(&swap_lock); 11575d337b91SHugh Dickins } 11585d337b91SHugh Dickins 11591da177e4SLinus Torvalds swap_file = p->swap_file; 11601da177e4SLinus Torvalds p->swap_file = NULL; 11611da177e4SLinus Torvalds p->max = 0; 11621da177e4SLinus Torvalds swap_map = p->swap_map; 11631da177e4SLinus Torvalds p->swap_map = NULL; 11641da177e4SLinus Torvalds p->flags = 0; 11655d337b91SHugh Dickins spin_unlock(&swap_lock); 11661da177e4SLinus Torvalds up(&swapon_sem); 11671da177e4SLinus Torvalds vfree(swap_map); 11681da177e4SLinus Torvalds inode = mapping->host; 11691da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 11701da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 11711da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 11721da177e4SLinus Torvalds bd_release(bdev); 11731da177e4SLinus Torvalds } else { 11741da177e4SLinus Torvalds down(&inode->i_sem); 11751da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 11761da177e4SLinus Torvalds up(&inode->i_sem); 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds filp_close(swap_file, NULL); 11791da177e4SLinus Torvalds err = 0; 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds out_dput: 11821da177e4SLinus Torvalds filp_close(victim, NULL); 11831da177e4SLinus Torvalds out: 11841da177e4SLinus Torvalds return err; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 11881da177e4SLinus Torvalds /* iterator */ 11891da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 11901da177e4SLinus Torvalds { 11911da177e4SLinus Torvalds struct swap_info_struct *ptr = swap_info; 11921da177e4SLinus Torvalds int i; 11931da177e4SLinus Torvalds loff_t l = *pos; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds down(&swapon_sem); 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++, ptr++) { 11981da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 11991da177e4SLinus Torvalds continue; 12001da177e4SLinus Torvalds if (!l--) 12011da177e4SLinus Torvalds return ptr; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds return NULL; 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 12081da177e4SLinus Torvalds { 12091da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 12101da177e4SLinus Torvalds struct swap_info_struct *endptr = swap_info + nr_swapfiles; 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds for (++ptr; ptr < endptr; ptr++) { 12131da177e4SLinus Torvalds if (!(ptr->flags & SWP_USED) || !ptr->swap_map) 12141da177e4SLinus Torvalds continue; 12151da177e4SLinus Torvalds ++*pos; 12161da177e4SLinus Torvalds return ptr; 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds return NULL; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 12231da177e4SLinus Torvalds { 12241da177e4SLinus Torvalds up(&swapon_sem); 12251da177e4SLinus Torvalds } 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 12281da177e4SLinus Torvalds { 12291da177e4SLinus Torvalds struct swap_info_struct *ptr = v; 12301da177e4SLinus Torvalds struct file *file; 12311da177e4SLinus Torvalds int len; 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds if (v == swap_info) 12341da177e4SLinus Torvalds seq_puts(swap, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds file = ptr->swap_file; 12371da177e4SLinus Torvalds len = seq_path(swap, file->f_vfsmnt, file->f_dentry, " \t\n\\"); 12386eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 12391da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 12401da177e4SLinus Torvalds S_ISBLK(file->f_dentry->d_inode->i_mode) ? 12411da177e4SLinus Torvalds "partition" : "file\t", 12421da177e4SLinus Torvalds ptr->pages << (PAGE_SHIFT - 10), 12431da177e4SLinus Torvalds ptr->inuse_pages << (PAGE_SHIFT - 10), 12441da177e4SLinus Torvalds ptr->prio); 12451da177e4SLinus Torvalds return 0; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds static struct seq_operations swaps_op = { 12491da177e4SLinus Torvalds .start = swap_start, 12501da177e4SLinus Torvalds .next = swap_next, 12511da177e4SLinus Torvalds .stop = swap_stop, 12521da177e4SLinus Torvalds .show = swap_show 12531da177e4SLinus Torvalds }; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 12561da177e4SLinus Torvalds { 12571da177e4SLinus Torvalds return seq_open(file, &swaps_op); 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds static struct file_operations proc_swaps_operations = { 12611da177e4SLinus Torvalds .open = swaps_open, 12621da177e4SLinus Torvalds .read = seq_read, 12631da177e4SLinus Torvalds .llseek = seq_lseek, 12641da177e4SLinus Torvalds .release = seq_release, 12651da177e4SLinus Torvalds }; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds static int __init procswaps_init(void) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds struct proc_dir_entry *entry; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds entry = create_proc_entry("swaps", 0, NULL); 12721da177e4SLinus Torvalds if (entry) 12731da177e4SLinus Torvalds entry->proc_fops = &proc_swaps_operations; 12741da177e4SLinus Torvalds return 0; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds __initcall(procswaps_init); 12771da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /* 12801da177e4SLinus Torvalds * Written 01/25/92 by Simmule Turner, heavily changed by Linus. 12811da177e4SLinus Torvalds * 12821da177e4SLinus Torvalds * The swapon system call 12831da177e4SLinus Torvalds */ 12841da177e4SLinus Torvalds asmlinkage long sys_swapon(const char __user * specialfile, int swap_flags) 12851da177e4SLinus Torvalds { 12861da177e4SLinus Torvalds struct swap_info_struct * p; 12871da177e4SLinus Torvalds char *name = NULL; 12881da177e4SLinus Torvalds struct block_device *bdev = NULL; 12891da177e4SLinus Torvalds struct file *swap_file = NULL; 12901da177e4SLinus Torvalds struct address_space *mapping; 12911da177e4SLinus Torvalds unsigned int type; 12921da177e4SLinus Torvalds int i, prev; 12931da177e4SLinus Torvalds int error; 12941da177e4SLinus Torvalds static int least_priority; 12951da177e4SLinus Torvalds union swap_header *swap_header = NULL; 12961da177e4SLinus Torvalds int swap_header_version; 12976eb396dcSHugh Dickins unsigned int nr_good_pages = 0; 12986eb396dcSHugh Dickins int nr_extents = 0; 129953092a74SHugh Dickins sector_t span; 13001da177e4SLinus Torvalds unsigned long maxpages = 1; 13011da177e4SLinus Torvalds int swapfilesize; 13021da177e4SLinus Torvalds unsigned short *swap_map; 13031da177e4SLinus Torvalds struct page *page = NULL; 13041da177e4SLinus Torvalds struct inode *inode = NULL; 13051da177e4SLinus Torvalds int did_down = 0; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 13081da177e4SLinus Torvalds return -EPERM; 13095d337b91SHugh Dickins spin_lock(&swap_lock); 13101da177e4SLinus Torvalds p = swap_info; 13111da177e4SLinus Torvalds for (type = 0 ; type < nr_swapfiles ; type++,p++) 13121da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 13131da177e4SLinus Torvalds break; 13141da177e4SLinus Torvalds error = -EPERM; 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * Test if adding another swap device is possible. There are 13171da177e4SLinus Torvalds * two limiting factors: 1) the number of bits for the swap 13181da177e4SLinus Torvalds * type swp_entry_t definition and 2) the number of bits for 13191da177e4SLinus Torvalds * the swap type in the swap ptes as defined by the different 13201da177e4SLinus Torvalds * architectures. To honor both limitations a swap entry 13211da177e4SLinus Torvalds * with swap offset 0 and swap type ~0UL is created, encoded 13221da177e4SLinus Torvalds * to a swap pte, decoded to a swp_entry_t again and finally 13231da177e4SLinus Torvalds * the swap type part is extracted. This will mask all bits 13241da177e4SLinus Torvalds * from the initial ~0UL that can't be encoded in either the 13251da177e4SLinus Torvalds * swp_entry_t or the architecture definition of a swap pte. 13261da177e4SLinus Torvalds */ 13271da177e4SLinus Torvalds if (type > swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) { 13285d337b91SHugh Dickins spin_unlock(&swap_lock); 13291da177e4SLinus Torvalds goto out; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds if (type >= nr_swapfiles) 13321da177e4SLinus Torvalds nr_swapfiles = type+1; 13331da177e4SLinus Torvalds INIT_LIST_HEAD(&p->extent_list); 13341da177e4SLinus Torvalds p->flags = SWP_USED; 13351da177e4SLinus Torvalds p->swap_file = NULL; 13361da177e4SLinus Torvalds p->old_block_size = 0; 13371da177e4SLinus Torvalds p->swap_map = NULL; 13381da177e4SLinus Torvalds p->lowest_bit = 0; 13391da177e4SLinus Torvalds p->highest_bit = 0; 13401da177e4SLinus Torvalds p->cluster_nr = 0; 13411da177e4SLinus Torvalds p->inuse_pages = 0; 13421da177e4SLinus Torvalds p->next = -1; 13431da177e4SLinus Torvalds if (swap_flags & SWAP_FLAG_PREFER) { 13441da177e4SLinus Torvalds p->prio = 13451da177e4SLinus Torvalds (swap_flags & SWAP_FLAG_PRIO_MASK)>>SWAP_FLAG_PRIO_SHIFT; 13461da177e4SLinus Torvalds } else { 13471da177e4SLinus Torvalds p->prio = --least_priority; 13481da177e4SLinus Torvalds } 13495d337b91SHugh Dickins spin_unlock(&swap_lock); 13501da177e4SLinus Torvalds name = getname(specialfile); 13511da177e4SLinus Torvalds error = PTR_ERR(name); 13521da177e4SLinus Torvalds if (IS_ERR(name)) { 13531da177e4SLinus Torvalds name = NULL; 13541da177e4SLinus Torvalds goto bad_swap_2; 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); 13571da177e4SLinus Torvalds error = PTR_ERR(swap_file); 13581da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 13591da177e4SLinus Torvalds swap_file = NULL; 13601da177e4SLinus Torvalds goto bad_swap_2; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds p->swap_file = swap_file; 13641da177e4SLinus Torvalds mapping = swap_file->f_mapping; 13651da177e4SLinus Torvalds inode = mapping->host; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds error = -EBUSY; 13681da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 13691da177e4SLinus Torvalds struct swap_info_struct *q = &swap_info[i]; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds if (i == type || !q->swap_file) 13721da177e4SLinus Torvalds continue; 13731da177e4SLinus Torvalds if (mapping == q->swap_file->f_mapping) 13741da177e4SLinus Torvalds goto bad_swap; 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds error = -EINVAL; 13781da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 13791da177e4SLinus Torvalds bdev = I_BDEV(inode); 13801da177e4SLinus Torvalds error = bd_claim(bdev, sys_swapon); 13811da177e4SLinus Torvalds if (error < 0) { 13821da177e4SLinus Torvalds bdev = NULL; 1383f7b3a435SRob Landley error = -EINVAL; 13841da177e4SLinus Torvalds goto bad_swap; 13851da177e4SLinus Torvalds } 13861da177e4SLinus Torvalds p->old_block_size = block_size(bdev); 13871da177e4SLinus Torvalds error = set_blocksize(bdev, PAGE_SIZE); 13881da177e4SLinus Torvalds if (error < 0) 13891da177e4SLinus Torvalds goto bad_swap; 13901da177e4SLinus Torvalds p->bdev = bdev; 13911da177e4SLinus Torvalds } else if (S_ISREG(inode->i_mode)) { 13921da177e4SLinus Torvalds p->bdev = inode->i_sb->s_bdev; 13931da177e4SLinus Torvalds down(&inode->i_sem); 13941da177e4SLinus Torvalds did_down = 1; 13951da177e4SLinus Torvalds if (IS_SWAPFILE(inode)) { 13961da177e4SLinus Torvalds error = -EBUSY; 13971da177e4SLinus Torvalds goto bad_swap; 13981da177e4SLinus Torvalds } 13991da177e4SLinus Torvalds } else { 14001da177e4SLinus Torvalds goto bad_swap; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds swapfilesize = i_size_read(inode) >> PAGE_SHIFT; 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds /* 14061da177e4SLinus Torvalds * Read the swap header. 14071da177e4SLinus Torvalds */ 14081da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 14091da177e4SLinus Torvalds error = -EINVAL; 14101da177e4SLinus Torvalds goto bad_swap; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds page = read_cache_page(mapping, 0, 14131da177e4SLinus Torvalds (filler_t *)mapping->a_ops->readpage, swap_file); 14141da177e4SLinus Torvalds if (IS_ERR(page)) { 14151da177e4SLinus Torvalds error = PTR_ERR(page); 14161da177e4SLinus Torvalds goto bad_swap; 14171da177e4SLinus Torvalds } 14181da177e4SLinus Torvalds wait_on_page_locked(page); 14191da177e4SLinus Torvalds if (!PageUptodate(page)) 14201da177e4SLinus Torvalds goto bad_swap; 14211da177e4SLinus Torvalds kmap(page); 14221da177e4SLinus Torvalds swap_header = page_address(page); 14231da177e4SLinus Torvalds 14241da177e4SLinus Torvalds if (!memcmp("SWAP-SPACE",swap_header->magic.magic,10)) 14251da177e4SLinus Torvalds swap_header_version = 1; 14261da177e4SLinus Torvalds else if (!memcmp("SWAPSPACE2",swap_header->magic.magic,10)) 14271da177e4SLinus Torvalds swap_header_version = 2; 14281da177e4SLinus Torvalds else { 14291da177e4SLinus Torvalds printk("Unable to find swap-space signature\n"); 14301da177e4SLinus Torvalds error = -EINVAL; 14311da177e4SLinus Torvalds goto bad_swap; 14321da177e4SLinus Torvalds } 14331da177e4SLinus Torvalds 14341da177e4SLinus Torvalds switch (swap_header_version) { 14351da177e4SLinus Torvalds case 1: 14361da177e4SLinus Torvalds printk(KERN_ERR "version 0 swap is no longer supported. " 14371da177e4SLinus Torvalds "Use mkswap -v1 %s\n", name); 14381da177e4SLinus Torvalds error = -EINVAL; 14391da177e4SLinus Torvalds goto bad_swap; 14401da177e4SLinus Torvalds case 2: 14411da177e4SLinus Torvalds /* Check the swap header's sub-version and the size of 14421da177e4SLinus Torvalds the swap file and bad block lists */ 14431da177e4SLinus Torvalds if (swap_header->info.version != 1) { 14441da177e4SLinus Torvalds printk(KERN_WARNING 14451da177e4SLinus Torvalds "Unable to handle swap header version %d\n", 14461da177e4SLinus Torvalds swap_header->info.version); 14471da177e4SLinus Torvalds error = -EINVAL; 14481da177e4SLinus Torvalds goto bad_swap; 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds 14511da177e4SLinus Torvalds p->lowest_bit = 1; 145252b7efdbSHugh Dickins p->cluster_next = 1; 145352b7efdbSHugh Dickins 14541da177e4SLinus Torvalds /* 14551da177e4SLinus Torvalds * Find out how many pages are allowed for a single swap 14561da177e4SLinus Torvalds * device. There are two limiting factors: 1) the number of 14571da177e4SLinus Torvalds * bits for the swap offset in the swp_entry_t type and 14581da177e4SLinus Torvalds * 2) the number of bits in the a swap pte as defined by 14591da177e4SLinus Torvalds * the different architectures. In order to find the 14601da177e4SLinus Torvalds * largest possible bit mask a swap entry with swap type 0 14611da177e4SLinus Torvalds * and swap offset ~0UL is created, encoded to a swap pte, 14621da177e4SLinus Torvalds * decoded to a swp_entry_t again and finally the swap 14631da177e4SLinus Torvalds * offset is extracted. This will mask all the bits from 14641da177e4SLinus Torvalds * the initial ~0UL mask that can't be encoded in either 14651da177e4SLinus Torvalds * the swp_entry_t or the architecture definition of a 14661da177e4SLinus Torvalds * swap pte. 14671da177e4SLinus Torvalds */ 14681da177e4SLinus Torvalds maxpages = swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1; 14691da177e4SLinus Torvalds if (maxpages > swap_header->info.last_page) 14701da177e4SLinus Torvalds maxpages = swap_header->info.last_page; 14711da177e4SLinus Torvalds p->highest_bit = maxpages - 1; 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds error = -EINVAL; 1474e2244ec2SHugh Dickins if (!maxpages) 1475e2244ec2SHugh Dickins goto bad_swap; 1476e2244ec2SHugh Dickins if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 1477e2244ec2SHugh Dickins goto bad_swap; 14781da177e4SLinus Torvalds if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 14791da177e4SLinus Torvalds goto bad_swap; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 14821da177e4SLinus Torvalds if (!(p->swap_map = vmalloc(maxpages * sizeof(short)))) { 14831da177e4SLinus Torvalds error = -ENOMEM; 14841da177e4SLinus Torvalds goto bad_swap; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds error = 0; 14881da177e4SLinus Torvalds memset(p->swap_map, 0, maxpages * sizeof(short)); 14891da177e4SLinus Torvalds for (i=0; i<swap_header->info.nr_badpages; i++) { 14901da177e4SLinus Torvalds int page = swap_header->info.badpages[i]; 14911da177e4SLinus Torvalds if (page <= 0 || page >= swap_header->info.last_page) 14921da177e4SLinus Torvalds error = -EINVAL; 14931da177e4SLinus Torvalds else 14941da177e4SLinus Torvalds p->swap_map[page] = SWAP_MAP_BAD; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds nr_good_pages = swap_header->info.last_page - 14971da177e4SLinus Torvalds swap_header->info.nr_badpages - 14981da177e4SLinus Torvalds 1 /* header page */; 14991da177e4SLinus Torvalds if (error) 15001da177e4SLinus Torvalds goto bad_swap; 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds if (swapfilesize && maxpages > swapfilesize) { 15041da177e4SLinus Torvalds printk(KERN_WARNING 15051da177e4SLinus Torvalds "Swap area shorter than signature indicates\n"); 15061da177e4SLinus Torvalds error = -EINVAL; 15071da177e4SLinus Torvalds goto bad_swap; 15081da177e4SLinus Torvalds } 1509e2244ec2SHugh Dickins if (nr_good_pages) { 1510e2244ec2SHugh Dickins p->swap_map[0] = SWAP_MAP_BAD; 1511e2244ec2SHugh Dickins p->max = maxpages; 1512e2244ec2SHugh Dickins p->pages = nr_good_pages; 151353092a74SHugh Dickins nr_extents = setup_swap_extents(p, &span); 151453092a74SHugh Dickins if (nr_extents < 0) { 151553092a74SHugh Dickins error = nr_extents; 1516e2244ec2SHugh Dickins goto bad_swap; 151753092a74SHugh Dickins } 1518e2244ec2SHugh Dickins nr_good_pages = p->pages; 1519e2244ec2SHugh Dickins } 15201da177e4SLinus Torvalds if (!nr_good_pages) { 15211da177e4SLinus Torvalds printk(KERN_WARNING "Empty swap-file\n"); 15221da177e4SLinus Torvalds error = -EINVAL; 15231da177e4SLinus Torvalds goto bad_swap; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds down(&swapon_sem); 15275d337b91SHugh Dickins spin_lock(&swap_lock); 15281da177e4SLinus Torvalds p->flags = SWP_ACTIVE; 15291da177e4SLinus Torvalds nr_swap_pages += nr_good_pages; 15301da177e4SLinus Torvalds total_swap_pages += nr_good_pages; 153153092a74SHugh Dickins 15326eb396dcSHugh Dickins printk(KERN_INFO "Adding %uk swap on %s. " 153353092a74SHugh Dickins "Priority:%d extents:%d across:%lluk\n", 153453092a74SHugh Dickins nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, 153553092a74SHugh Dickins nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10)); 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds /* insert swap space into swap_list: */ 15381da177e4SLinus Torvalds prev = -1; 15391da177e4SLinus Torvalds for (i = swap_list.head; i >= 0; i = swap_info[i].next) { 15401da177e4SLinus Torvalds if (p->prio >= swap_info[i].prio) { 15411da177e4SLinus Torvalds break; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds prev = i; 15441da177e4SLinus Torvalds } 15451da177e4SLinus Torvalds p->next = i; 15461da177e4SLinus Torvalds if (prev < 0) { 15471da177e4SLinus Torvalds swap_list.head = swap_list.next = p - swap_info; 15481da177e4SLinus Torvalds } else { 15491da177e4SLinus Torvalds swap_info[prev].next = p - swap_info; 15501da177e4SLinus Torvalds } 15515d337b91SHugh Dickins spin_unlock(&swap_lock); 15521da177e4SLinus Torvalds up(&swapon_sem); 15531da177e4SLinus Torvalds error = 0; 15541da177e4SLinus Torvalds goto out; 15551da177e4SLinus Torvalds bad_swap: 15561da177e4SLinus Torvalds if (bdev) { 15571da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 15581da177e4SLinus Torvalds bd_release(bdev); 15591da177e4SLinus Torvalds } 15604cd3bb10SHugh Dickins destroy_swap_extents(p); 15611da177e4SLinus Torvalds bad_swap_2: 15625d337b91SHugh Dickins spin_lock(&swap_lock); 15631da177e4SLinus Torvalds swap_map = p->swap_map; 15641da177e4SLinus Torvalds p->swap_file = NULL; 15651da177e4SLinus Torvalds p->swap_map = NULL; 15661da177e4SLinus Torvalds p->flags = 0; 15671da177e4SLinus Torvalds if (!(swap_flags & SWAP_FLAG_PREFER)) 15681da177e4SLinus Torvalds ++least_priority; 15695d337b91SHugh Dickins spin_unlock(&swap_lock); 15701da177e4SLinus Torvalds vfree(swap_map); 15711da177e4SLinus Torvalds if (swap_file) 15721da177e4SLinus Torvalds filp_close(swap_file, NULL); 15731da177e4SLinus Torvalds out: 15741da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 15751da177e4SLinus Torvalds kunmap(page); 15761da177e4SLinus Torvalds page_cache_release(page); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds if (name) 15791da177e4SLinus Torvalds putname(name); 15801da177e4SLinus Torvalds if (did_down) { 15811da177e4SLinus Torvalds if (!error) 15821da177e4SLinus Torvalds inode->i_flags |= S_SWAPFILE; 15831da177e4SLinus Torvalds up(&inode->i_sem); 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds return error; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 15891da177e4SLinus Torvalds { 15901da177e4SLinus Torvalds unsigned int i; 15911da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 15921da177e4SLinus Torvalds 15935d337b91SHugh Dickins spin_lock(&swap_lock); 15941da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 15951da177e4SLinus Torvalds if (!(swap_info[i].flags & SWP_USED) || 15961da177e4SLinus Torvalds (swap_info[i].flags & SWP_WRITEOK)) 15971da177e4SLinus Torvalds continue; 15981da177e4SLinus Torvalds nr_to_be_unused += swap_info[i].inuse_pages; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds val->freeswap = nr_swap_pages + nr_to_be_unused; 16011da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 16025d337b91SHugh Dickins spin_unlock(&swap_lock); 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds /* 16061da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 16071da177e4SLinus Torvalds * 16081da177e4SLinus Torvalds * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as 16091da177e4SLinus Torvalds * "permanent", but will be reclaimed by the next swapoff. 16101da177e4SLinus Torvalds */ 16111da177e4SLinus Torvalds int swap_duplicate(swp_entry_t entry) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds struct swap_info_struct * p; 16141da177e4SLinus Torvalds unsigned long offset, type; 16151da177e4SLinus Torvalds int result = 0; 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds type = swp_type(entry); 16181da177e4SLinus Torvalds if (type >= nr_swapfiles) 16191da177e4SLinus Torvalds goto bad_file; 16201da177e4SLinus Torvalds p = type + swap_info; 16211da177e4SLinus Torvalds offset = swp_offset(entry); 16221da177e4SLinus Torvalds 16235d337b91SHugh Dickins spin_lock(&swap_lock); 16241da177e4SLinus Torvalds if (offset < p->max && p->swap_map[offset]) { 16251da177e4SLinus Torvalds if (p->swap_map[offset] < SWAP_MAP_MAX - 1) { 16261da177e4SLinus Torvalds p->swap_map[offset]++; 16271da177e4SLinus Torvalds result = 1; 16281da177e4SLinus Torvalds } else if (p->swap_map[offset] <= SWAP_MAP_MAX) { 16291da177e4SLinus Torvalds if (swap_overflow++ < 5) 16301da177e4SLinus Torvalds printk(KERN_WARNING "swap_dup: swap entry overflow\n"); 16311da177e4SLinus Torvalds p->swap_map[offset] = SWAP_MAP_MAX; 16321da177e4SLinus Torvalds result = 1; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds } 16355d337b91SHugh Dickins spin_unlock(&swap_lock); 16361da177e4SLinus Torvalds out: 16371da177e4SLinus Torvalds return result; 16381da177e4SLinus Torvalds 16391da177e4SLinus Torvalds bad_file: 16401da177e4SLinus Torvalds printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); 16411da177e4SLinus Torvalds goto out; 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds struct swap_info_struct * 16451da177e4SLinus Torvalds get_swap_info_struct(unsigned type) 16461da177e4SLinus Torvalds { 16471da177e4SLinus Torvalds return &swap_info[type]; 16481da177e4SLinus Torvalds } 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds /* 16515d337b91SHugh Dickins * swap_lock prevents swap_map being freed. Don't grab an extra 16521da177e4SLinus Torvalds * reference on the swaphandle, it doesn't matter if it becomes unused. 16531da177e4SLinus Torvalds */ 16541da177e4SLinus Torvalds int valid_swaphandles(swp_entry_t entry, unsigned long *offset) 16551da177e4SLinus Torvalds { 16561da177e4SLinus Torvalds int ret = 0, i = 1 << page_cluster; 16571da177e4SLinus Torvalds unsigned long toff; 16581da177e4SLinus Torvalds struct swap_info_struct *swapdev = swp_type(entry) + swap_info; 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds if (!page_cluster) /* no readahead */ 16611da177e4SLinus Torvalds return 0; 16621da177e4SLinus Torvalds toff = (swp_offset(entry) >> page_cluster) << page_cluster; 16631da177e4SLinus Torvalds if (!toff) /* first page is swap header */ 16641da177e4SLinus Torvalds toff++, i--; 16651da177e4SLinus Torvalds *offset = toff; 16661da177e4SLinus Torvalds 16675d337b91SHugh Dickins spin_lock(&swap_lock); 16681da177e4SLinus Torvalds do { 16691da177e4SLinus Torvalds /* Don't read-ahead past the end of the swap area */ 16701da177e4SLinus Torvalds if (toff >= swapdev->max) 16711da177e4SLinus Torvalds break; 16721da177e4SLinus Torvalds /* Don't read in free or bad pages */ 16731da177e4SLinus Torvalds if (!swapdev->swap_map[toff]) 16741da177e4SLinus Torvalds break; 16751da177e4SLinus Torvalds if (swapdev->swap_map[toff] == SWAP_MAP_BAD) 16761da177e4SLinus Torvalds break; 16771da177e4SLinus Torvalds toff++; 16781da177e4SLinus Torvalds ret++; 16791da177e4SLinus Torvalds } while (--i); 16805d337b91SHugh Dickins spin_unlock(&swap_lock); 16811da177e4SLinus Torvalds return ret; 16821da177e4SLinus Torvalds } 1683