11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/mm/swapfile.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 51da177e4SLinus Torvalds * Swap reorganised 29.12.95, Stephen Tweedie 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/mm.h> 91da177e4SLinus Torvalds #include <linux/hugetlb.h> 101da177e4SLinus Torvalds #include <linux/mman.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/kernel_stat.h> 131da177e4SLinus Torvalds #include <linux/swap.h> 141da177e4SLinus Torvalds #include <linux/vmalloc.h> 151da177e4SLinus Torvalds #include <linux/pagemap.h> 161da177e4SLinus Torvalds #include <linux/namei.h> 17072441e2SHugh Dickins #include <linux/shmem_fs.h> 181da177e4SLinus Torvalds #include <linux/blkdev.h> 1920137a49SHugh Dickins #include <linux/random.h> 201da177e4SLinus Torvalds #include <linux/writeback.h> 211da177e4SLinus Torvalds #include <linux/proc_fs.h> 221da177e4SLinus Torvalds #include <linux/seq_file.h> 231da177e4SLinus Torvalds #include <linux/init.h> 245ad64688SHugh Dickins #include <linux/ksm.h> 251da177e4SLinus Torvalds #include <linux/rmap.h> 261da177e4SLinus Torvalds #include <linux/security.h> 271da177e4SLinus Torvalds #include <linux/backing-dev.h> 28fc0abb14SIngo Molnar #include <linux/mutex.h> 29c59ede7bSRandy.Dunlap #include <linux/capability.h> 301da177e4SLinus Torvalds #include <linux/syscalls.h> 318a9f3ccdSBalbir Singh #include <linux/memcontrol.h> 3266d7dd51SKay Sievers #include <linux/poll.h> 3372788c38SDavid Rientjes #include <linux/oom.h> 3438b5faf4SDan Magenheimer #include <linux/frontswap.h> 3538b5faf4SDan Magenheimer #include <linux/swapfile.h> 36f981c595SMel Gorman #include <linux/export.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <asm/pgtable.h> 391da177e4SLinus Torvalds #include <asm/tlbflush.h> 401da177e4SLinus Torvalds #include <linux/swapops.h> 4127a7faa0SKAMEZAWA Hiroyuki #include <linux/page_cgroup.h> 421da177e4SLinus Torvalds 43570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *, pgoff_t, 44570a335bSHugh Dickins unsigned char); 45570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *); 46d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t, struct block_device**); 47570a335bSHugh Dickins 4838b5faf4SDan Magenheimer DEFINE_SPINLOCK(swap_lock); 497c363b8cSAdrian Bunk static unsigned int nr_swapfiles; 50ec8acf20SShaohua Li atomic_long_t nr_swap_pages; 51ec8acf20SShaohua Li /* protected with swap_lock. reading in vm_swap_full() doesn't need lock */ 521da177e4SLinus Torvalds long total_swap_pages; 5378ecba08SHugh Dickins static int least_priority; 54ec8acf20SShaohua Li static atomic_t highest_priority_index = ATOMIC_INIT(-1); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds static const char Bad_file[] = "Bad swap file entry "; 571da177e4SLinus Torvalds static const char Unused_file[] = "Unused swap file entry "; 581da177e4SLinus Torvalds static const char Bad_offset[] = "Bad swap offset entry "; 591da177e4SLinus Torvalds static const char Unused_offset[] = "Unused swap offset entry "; 601da177e4SLinus Torvalds 6138b5faf4SDan Magenheimer struct swap_list_t swap_list = {-1, -1}; 621da177e4SLinus Torvalds 6338b5faf4SDan Magenheimer struct swap_info_struct *swap_info[MAX_SWAPFILES]; 641da177e4SLinus Torvalds 65fc0abb14SIngo Molnar static DEFINE_MUTEX(swapon_mutex); 661da177e4SLinus Torvalds 6766d7dd51SKay Sievers static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait); 6866d7dd51SKay Sievers /* Activity counter to indicate that a swapon or swapoff has occurred */ 6966d7dd51SKay Sievers static atomic_t proc_poll_event = ATOMIC_INIT(0); 7066d7dd51SKay Sievers 718d69aaeeSHugh Dickins static inline unsigned char swap_count(unsigned char ent) 72355cfa73SKAMEZAWA Hiroyuki { 73570a335bSHugh Dickins return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ 74355cfa73SKAMEZAWA Hiroyuki } 75355cfa73SKAMEZAWA Hiroyuki 76efa90a98SHugh Dickins /* returns 1 if swap entry is freed */ 77c9e44410SKAMEZAWA Hiroyuki static int 78c9e44410SKAMEZAWA Hiroyuki __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) 79c9e44410SKAMEZAWA Hiroyuki { 80efa90a98SHugh Dickins swp_entry_t entry = swp_entry(si->type, offset); 81c9e44410SKAMEZAWA Hiroyuki struct page *page; 82c9e44410SKAMEZAWA Hiroyuki int ret = 0; 83c9e44410SKAMEZAWA Hiroyuki 8433806f06SShaohua Li page = find_get_page(swap_address_space(entry), entry.val); 85c9e44410SKAMEZAWA Hiroyuki if (!page) 86c9e44410SKAMEZAWA Hiroyuki return 0; 87c9e44410SKAMEZAWA Hiroyuki /* 88c9e44410SKAMEZAWA Hiroyuki * This function is called from scan_swap_map() and it's called 89c9e44410SKAMEZAWA Hiroyuki * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. 90c9e44410SKAMEZAWA Hiroyuki * We have to use trylock for avoiding deadlock. This is a special 91c9e44410SKAMEZAWA Hiroyuki * case and you should use try_to_free_swap() with explicit lock_page() 92c9e44410SKAMEZAWA Hiroyuki * in usual operations. 93c9e44410SKAMEZAWA Hiroyuki */ 94c9e44410SKAMEZAWA Hiroyuki if (trylock_page(page)) { 95c9e44410SKAMEZAWA Hiroyuki ret = try_to_free_swap(page); 96c9e44410SKAMEZAWA Hiroyuki unlock_page(page); 97c9e44410SKAMEZAWA Hiroyuki } 98c9e44410SKAMEZAWA Hiroyuki page_cache_release(page); 99c9e44410SKAMEZAWA Hiroyuki return ret; 100c9e44410SKAMEZAWA Hiroyuki } 101355cfa73SKAMEZAWA Hiroyuki 1021da177e4SLinus Torvalds /* 1036a6ba831SHugh Dickins * swapon tell device that all the old swap contents can be discarded, 1046a6ba831SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1056a6ba831SHugh Dickins */ 1066a6ba831SHugh Dickins static int discard_swap(struct swap_info_struct *si) 1076a6ba831SHugh Dickins { 1086a6ba831SHugh Dickins struct swap_extent *se; 1099625a5f2SHugh Dickins sector_t start_block; 1109625a5f2SHugh Dickins sector_t nr_blocks; 1116a6ba831SHugh Dickins int err = 0; 1126a6ba831SHugh Dickins 1136a6ba831SHugh Dickins /* Do not discard the swap header page! */ 1149625a5f2SHugh Dickins se = &si->first_swap_extent; 1159625a5f2SHugh Dickins start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); 1169625a5f2SHugh Dickins nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); 1179625a5f2SHugh Dickins if (nr_blocks) { 1189625a5f2SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 119dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1209625a5f2SHugh Dickins if (err) 1219625a5f2SHugh Dickins return err; 1229625a5f2SHugh Dickins cond_resched(); 1236a6ba831SHugh Dickins } 1246a6ba831SHugh Dickins 1259625a5f2SHugh Dickins list_for_each_entry(se, &si->first_swap_extent.list, list) { 1269625a5f2SHugh Dickins start_block = se->start_block << (PAGE_SHIFT - 9); 1279625a5f2SHugh Dickins nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); 1289625a5f2SHugh Dickins 1296a6ba831SHugh Dickins err = blkdev_issue_discard(si->bdev, start_block, 130dd3932edSChristoph Hellwig nr_blocks, GFP_KERNEL, 0); 1316a6ba831SHugh Dickins if (err) 1326a6ba831SHugh Dickins break; 1336a6ba831SHugh Dickins 1346a6ba831SHugh Dickins cond_resched(); 1356a6ba831SHugh Dickins } 1366a6ba831SHugh Dickins return err; /* That will often be -EOPNOTSUPP */ 1376a6ba831SHugh Dickins } 1386a6ba831SHugh Dickins 1397992fde7SHugh Dickins /* 1407992fde7SHugh Dickins * swap allocation tell device that a cluster of swap can now be discarded, 1417992fde7SHugh Dickins * to allow the swap device to optimize its wear-levelling. 1427992fde7SHugh Dickins */ 1437992fde7SHugh Dickins static void discard_swap_cluster(struct swap_info_struct *si, 1447992fde7SHugh Dickins pgoff_t start_page, pgoff_t nr_pages) 1457992fde7SHugh Dickins { 1467992fde7SHugh Dickins struct swap_extent *se = si->curr_swap_extent; 1477992fde7SHugh Dickins int found_extent = 0; 1487992fde7SHugh Dickins 1497992fde7SHugh Dickins while (nr_pages) { 1507992fde7SHugh Dickins struct list_head *lh; 1517992fde7SHugh Dickins 1527992fde7SHugh Dickins if (se->start_page <= start_page && 1537992fde7SHugh Dickins start_page < se->start_page + se->nr_pages) { 1547992fde7SHugh Dickins pgoff_t offset = start_page - se->start_page; 1557992fde7SHugh Dickins sector_t start_block = se->start_block + offset; 156858a2990SHugh Dickins sector_t nr_blocks = se->nr_pages - offset; 1577992fde7SHugh Dickins 1587992fde7SHugh Dickins if (nr_blocks > nr_pages) 1597992fde7SHugh Dickins nr_blocks = nr_pages; 1607992fde7SHugh Dickins start_page += nr_blocks; 1617992fde7SHugh Dickins nr_pages -= nr_blocks; 1627992fde7SHugh Dickins 1637992fde7SHugh Dickins if (!found_extent++) 1647992fde7SHugh Dickins si->curr_swap_extent = se; 1657992fde7SHugh Dickins 1667992fde7SHugh Dickins start_block <<= PAGE_SHIFT - 9; 1677992fde7SHugh Dickins nr_blocks <<= PAGE_SHIFT - 9; 1687992fde7SHugh Dickins if (blkdev_issue_discard(si->bdev, start_block, 169dd3932edSChristoph Hellwig nr_blocks, GFP_NOIO, 0)) 1707992fde7SHugh Dickins break; 1717992fde7SHugh Dickins } 1727992fde7SHugh Dickins 1737992fde7SHugh Dickins lh = se->list.next; 1747992fde7SHugh Dickins se = list_entry(lh, struct swap_extent, list); 1757992fde7SHugh Dickins } 1767992fde7SHugh Dickins } 1777992fde7SHugh Dickins 1787992fde7SHugh Dickins static int wait_for_discard(void *word) 1797992fde7SHugh Dickins { 1807992fde7SHugh Dickins schedule(); 1817992fde7SHugh Dickins return 0; 1827992fde7SHugh Dickins } 1837992fde7SHugh Dickins 184048c27fdSHugh Dickins #define SWAPFILE_CLUSTER 256 185048c27fdSHugh Dickins #define LATENCY_LIMIT 256 186048c27fdSHugh Dickins 187*2a8f9449SShaohua Li static inline void cluster_set_flag(struct swap_cluster_info *info, 188*2a8f9449SShaohua Li unsigned int flag) 189*2a8f9449SShaohua Li { 190*2a8f9449SShaohua Li info->flags = flag; 191*2a8f9449SShaohua Li } 192*2a8f9449SShaohua Li 193*2a8f9449SShaohua Li static inline unsigned int cluster_count(struct swap_cluster_info *info) 194*2a8f9449SShaohua Li { 195*2a8f9449SShaohua Li return info->data; 196*2a8f9449SShaohua Li } 197*2a8f9449SShaohua Li 198*2a8f9449SShaohua Li static inline void cluster_set_count(struct swap_cluster_info *info, 199*2a8f9449SShaohua Li unsigned int c) 200*2a8f9449SShaohua Li { 201*2a8f9449SShaohua Li info->data = c; 202*2a8f9449SShaohua Li } 203*2a8f9449SShaohua Li 204*2a8f9449SShaohua Li static inline void cluster_set_count_flag(struct swap_cluster_info *info, 205*2a8f9449SShaohua Li unsigned int c, unsigned int f) 206*2a8f9449SShaohua Li { 207*2a8f9449SShaohua Li info->flags = f; 208*2a8f9449SShaohua Li info->data = c; 209*2a8f9449SShaohua Li } 210*2a8f9449SShaohua Li 211*2a8f9449SShaohua Li static inline unsigned int cluster_next(struct swap_cluster_info *info) 212*2a8f9449SShaohua Li { 213*2a8f9449SShaohua Li return info->data; 214*2a8f9449SShaohua Li } 215*2a8f9449SShaohua Li 216*2a8f9449SShaohua Li static inline void cluster_set_next(struct swap_cluster_info *info, 217*2a8f9449SShaohua Li unsigned int n) 218*2a8f9449SShaohua Li { 219*2a8f9449SShaohua Li info->data = n; 220*2a8f9449SShaohua Li } 221*2a8f9449SShaohua Li 222*2a8f9449SShaohua Li static inline void cluster_set_next_flag(struct swap_cluster_info *info, 223*2a8f9449SShaohua Li unsigned int n, unsigned int f) 224*2a8f9449SShaohua Li { 225*2a8f9449SShaohua Li info->flags = f; 226*2a8f9449SShaohua Li info->data = n; 227*2a8f9449SShaohua Li } 228*2a8f9449SShaohua Li 229*2a8f9449SShaohua Li static inline bool cluster_is_free(struct swap_cluster_info *info) 230*2a8f9449SShaohua Li { 231*2a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_FREE; 232*2a8f9449SShaohua Li } 233*2a8f9449SShaohua Li 234*2a8f9449SShaohua Li static inline bool cluster_is_null(struct swap_cluster_info *info) 235*2a8f9449SShaohua Li { 236*2a8f9449SShaohua Li return info->flags & CLUSTER_FLAG_NEXT_NULL; 237*2a8f9449SShaohua Li } 238*2a8f9449SShaohua Li 239*2a8f9449SShaohua Li static inline void cluster_set_null(struct swap_cluster_info *info) 240*2a8f9449SShaohua Li { 241*2a8f9449SShaohua Li info->flags = CLUSTER_FLAG_NEXT_NULL; 242*2a8f9449SShaohua Li info->data = 0; 243*2a8f9449SShaohua Li } 244*2a8f9449SShaohua Li 245*2a8f9449SShaohua Li /* 246*2a8f9449SShaohua Li * The cluster corresponding to page_nr will be used. The cluster will be 247*2a8f9449SShaohua Li * removed from free cluster list and its usage counter will be increased. 248*2a8f9449SShaohua Li */ 249*2a8f9449SShaohua Li static void inc_cluster_info_page(struct swap_info_struct *p, 250*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 251*2a8f9449SShaohua Li { 252*2a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 253*2a8f9449SShaohua Li 254*2a8f9449SShaohua Li if (!cluster_info) 255*2a8f9449SShaohua Li return; 256*2a8f9449SShaohua Li if (cluster_is_free(&cluster_info[idx])) { 257*2a8f9449SShaohua Li VM_BUG_ON(cluster_next(&p->free_cluster_head) != idx); 258*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, 259*2a8f9449SShaohua Li cluster_next(&cluster_info[idx]), 0); 260*2a8f9449SShaohua Li if (cluster_next(&p->free_cluster_tail) == idx) { 261*2a8f9449SShaohua Li cluster_set_null(&p->free_cluster_tail); 262*2a8f9449SShaohua Li cluster_set_null(&p->free_cluster_head); 263*2a8f9449SShaohua Li } 264*2a8f9449SShaohua Li cluster_set_count_flag(&cluster_info[idx], 0, 0); 265*2a8f9449SShaohua Li } 266*2a8f9449SShaohua Li 267*2a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) >= SWAPFILE_CLUSTER); 268*2a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 269*2a8f9449SShaohua Li cluster_count(&cluster_info[idx]) + 1); 270*2a8f9449SShaohua Li } 271*2a8f9449SShaohua Li 272*2a8f9449SShaohua Li /* 273*2a8f9449SShaohua Li * The cluster corresponding to page_nr decreases one usage. If the usage 274*2a8f9449SShaohua Li * counter becomes 0, which means no page in the cluster is in using, we can 275*2a8f9449SShaohua Li * optionally discard the cluster and add it to free cluster list. 276*2a8f9449SShaohua Li */ 277*2a8f9449SShaohua Li static void dec_cluster_info_page(struct swap_info_struct *p, 278*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info, unsigned long page_nr) 279*2a8f9449SShaohua Li { 280*2a8f9449SShaohua Li unsigned long idx = page_nr / SWAPFILE_CLUSTER; 281*2a8f9449SShaohua Li 282*2a8f9449SShaohua Li if (!cluster_info) 283*2a8f9449SShaohua Li return; 284*2a8f9449SShaohua Li 285*2a8f9449SShaohua Li VM_BUG_ON(cluster_count(&cluster_info[idx]) == 0); 286*2a8f9449SShaohua Li cluster_set_count(&cluster_info[idx], 287*2a8f9449SShaohua Li cluster_count(&cluster_info[idx]) - 1); 288*2a8f9449SShaohua Li 289*2a8f9449SShaohua Li if (cluster_count(&cluster_info[idx]) == 0) { 290*2a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 291*2a8f9449SShaohua Li if (cluster_is_null(&p->free_cluster_head)) { 292*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, idx, 0); 293*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, idx, 0); 294*2a8f9449SShaohua Li } else { 295*2a8f9449SShaohua Li unsigned int tail = cluster_next(&p->free_cluster_tail); 296*2a8f9449SShaohua Li cluster_set_next(&cluster_info[tail], idx); 297*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, idx, 0); 298*2a8f9449SShaohua Li } 299*2a8f9449SShaohua Li } 300*2a8f9449SShaohua Li } 301*2a8f9449SShaohua Li 302*2a8f9449SShaohua Li /* 303*2a8f9449SShaohua Li * It's possible scan_swap_map() uses a free cluster in the middle of free 304*2a8f9449SShaohua Li * cluster list. Avoiding such abuse to avoid list corruption. 305*2a8f9449SShaohua Li */ 306*2a8f9449SShaohua Li static inline bool scan_swap_map_recheck_cluster(struct swap_info_struct *si, 307*2a8f9449SShaohua Li unsigned long offset) 308*2a8f9449SShaohua Li { 309*2a8f9449SShaohua Li offset /= SWAPFILE_CLUSTER; 310*2a8f9449SShaohua Li return !cluster_is_null(&si->free_cluster_head) && 311*2a8f9449SShaohua Li offset != cluster_next(&si->free_cluster_head) && 312*2a8f9449SShaohua Li cluster_is_free(&si->cluster_info[offset]); 313*2a8f9449SShaohua Li } 314*2a8f9449SShaohua Li 31524b8ff7cSCesar Eduardo Barros static unsigned long scan_swap_map(struct swap_info_struct *si, 3168d69aaeeSHugh Dickins unsigned char usage) 3171da177e4SLinus Torvalds { 318ebebbbe9SHugh Dickins unsigned long offset; 319c60aa176SHugh Dickins unsigned long scan_base; 3207992fde7SHugh Dickins unsigned long last_in_cluster = 0; 321048c27fdSHugh Dickins int latency_ration = LATENCY_LIMIT; 3227992fde7SHugh Dickins int found_free_cluster = 0; 3231da177e4SLinus Torvalds 3247dfad418SHugh Dickins /* 3257dfad418SHugh Dickins * We try to cluster swap pages by allocating them sequentially 3267dfad418SHugh Dickins * in swap. Once we've allocated SWAPFILE_CLUSTER pages this 3277dfad418SHugh Dickins * way, however, we resort to first-free allocation, starting 3287dfad418SHugh Dickins * a new cluster. This prevents us from scattering swap pages 3297dfad418SHugh Dickins * all over the entire swap partition, so that we reduce 3307dfad418SHugh Dickins * overall disk seek times between swap pages. -- sct 3317dfad418SHugh Dickins * But we do now try to find an empty cluster. -Andrea 332c60aa176SHugh Dickins * And we let swap pages go all over an SSD partition. Hugh 3331da177e4SLinus Torvalds */ 3347dfad418SHugh Dickins 33552b7efdbSHugh Dickins si->flags += SWP_SCANNING; 336c60aa176SHugh Dickins scan_base = offset = si->cluster_next; 337ebebbbe9SHugh Dickins 338ebebbbe9SHugh Dickins if (unlikely(!si->cluster_nr--)) { 339ebebbbe9SHugh Dickins if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { 3407dfad418SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 341ebebbbe9SHugh Dickins goto checks; 342ebebbbe9SHugh Dickins } 343dcf6b7ddSRafael Aquini if (si->flags & SWP_PAGE_DISCARD) { 3447992fde7SHugh Dickins /* 3457992fde7SHugh Dickins * Start range check on racing allocations, in case 3467992fde7SHugh Dickins * they overlap the cluster we eventually decide on 3477992fde7SHugh Dickins * (we scan without swap_lock to allow preemption). 3487992fde7SHugh Dickins * It's hardly conceivable that cluster_nr could be 3497992fde7SHugh Dickins * wrapped during our scan, but don't depend on it. 3507992fde7SHugh Dickins */ 3517992fde7SHugh Dickins if (si->lowest_alloc) 3527992fde7SHugh Dickins goto checks; 3537992fde7SHugh Dickins si->lowest_alloc = si->max; 3547992fde7SHugh Dickins si->highest_alloc = 0; 3557992fde7SHugh Dickins } 356*2a8f9449SShaohua Li check_cluster: 357*2a8f9449SShaohua Li if (!cluster_is_null(&si->free_cluster_head)) { 358*2a8f9449SShaohua Li offset = cluster_next(&si->free_cluster_head) * 359*2a8f9449SShaohua Li SWAPFILE_CLUSTER; 360*2a8f9449SShaohua Li last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 361*2a8f9449SShaohua Li si->cluster_next = offset; 362*2a8f9449SShaohua Li si->cluster_nr = SWAPFILE_CLUSTER - 1; 363*2a8f9449SShaohua Li found_free_cluster = 1; 364*2a8f9449SShaohua Li goto checks; 365*2a8f9449SShaohua Li } else if (si->cluster_info) { 366*2a8f9449SShaohua Li /* 367*2a8f9449SShaohua Li * Checking free cluster is fast enough, we can do the 368*2a8f9449SShaohua Li * check every time 369*2a8f9449SShaohua Li */ 370*2a8f9449SShaohua Li si->cluster_nr = 0; 371*2a8f9449SShaohua Li si->lowest_alloc = 0; 372*2a8f9449SShaohua Li goto checks; 373*2a8f9449SShaohua Li } 374*2a8f9449SShaohua Li 375ec8acf20SShaohua Li spin_unlock(&si->lock); 3767dfad418SHugh Dickins 377c60aa176SHugh Dickins /* 378c60aa176SHugh Dickins * If seek is expensive, start searching for new cluster from 379c60aa176SHugh Dickins * start of partition, to minimize the span of allocated swap. 380c60aa176SHugh Dickins * But if seek is cheap, search from our current position, so 381c60aa176SHugh Dickins * that swap is allocated from all over the partition: if the 382c60aa176SHugh Dickins * Flash Translation Layer only remaps within limited zones, 383c60aa176SHugh Dickins * we don't want to wear out the first zone too quickly. 384c60aa176SHugh Dickins */ 385c60aa176SHugh Dickins if (!(si->flags & SWP_SOLIDSTATE)) 386c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 3877dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 3887dfad418SHugh Dickins 3897dfad418SHugh Dickins /* Locate the first empty (unaligned) cluster */ 3907dfad418SHugh Dickins for (; last_in_cluster <= si->highest_bit; offset++) { 3911da177e4SLinus Torvalds if (si->swap_map[offset]) 3927dfad418SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 3937dfad418SHugh Dickins else if (offset == last_in_cluster) { 394ec8acf20SShaohua Li spin_lock(&si->lock); 395ebebbbe9SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 396ebebbbe9SHugh Dickins si->cluster_next = offset; 397ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 3987992fde7SHugh Dickins found_free_cluster = 1; 399ebebbbe9SHugh Dickins goto checks; 4007dfad418SHugh Dickins } 401048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 402048c27fdSHugh Dickins cond_resched(); 403048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 404048c27fdSHugh Dickins } 4057dfad418SHugh Dickins } 406ebebbbe9SHugh Dickins 407ebebbbe9SHugh Dickins offset = si->lowest_bit; 408c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER - 1; 409c60aa176SHugh Dickins 410c60aa176SHugh Dickins /* Locate the first empty (unaligned) cluster */ 411c60aa176SHugh Dickins for (; last_in_cluster < scan_base; offset++) { 412c60aa176SHugh Dickins if (si->swap_map[offset]) 413c60aa176SHugh Dickins last_in_cluster = offset + SWAPFILE_CLUSTER; 414c60aa176SHugh Dickins else if (offset == last_in_cluster) { 415ec8acf20SShaohua Li spin_lock(&si->lock); 416c60aa176SHugh Dickins offset -= SWAPFILE_CLUSTER - 1; 417c60aa176SHugh Dickins si->cluster_next = offset; 418c60aa176SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 419c60aa176SHugh Dickins found_free_cluster = 1; 420c60aa176SHugh Dickins goto checks; 421c60aa176SHugh Dickins } 422c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 423c60aa176SHugh Dickins cond_resched(); 424c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 425c60aa176SHugh Dickins } 426c60aa176SHugh Dickins } 427c60aa176SHugh Dickins 428c60aa176SHugh Dickins offset = scan_base; 429ec8acf20SShaohua Li spin_lock(&si->lock); 430ebebbbe9SHugh Dickins si->cluster_nr = SWAPFILE_CLUSTER - 1; 4317992fde7SHugh Dickins si->lowest_alloc = 0; 4327dfad418SHugh Dickins } 4337dfad418SHugh Dickins 434ebebbbe9SHugh Dickins checks: 435*2a8f9449SShaohua Li if (scan_swap_map_recheck_cluster(si, offset)) 436*2a8f9449SShaohua Li goto check_cluster; 437ebebbbe9SHugh Dickins if (!(si->flags & SWP_WRITEOK)) 43852b7efdbSHugh Dickins goto no_page; 4397dfad418SHugh Dickins if (!si->highest_bit) 4407dfad418SHugh Dickins goto no_page; 441ebebbbe9SHugh Dickins if (offset > si->highest_bit) 442c60aa176SHugh Dickins scan_base = offset = si->lowest_bit; 443c9e44410SKAMEZAWA Hiroyuki 444b73d7fceSHugh Dickins /* reuse swap entry of cache-only swap if not busy. */ 445b73d7fceSHugh Dickins if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 446c9e44410SKAMEZAWA Hiroyuki int swap_was_freed; 447ec8acf20SShaohua Li spin_unlock(&si->lock); 448c9e44410SKAMEZAWA Hiroyuki swap_was_freed = __try_to_reclaim_swap(si, offset); 449ec8acf20SShaohua Li spin_lock(&si->lock); 450c9e44410SKAMEZAWA Hiroyuki /* entry was freed successfully, try to use this again */ 451c9e44410SKAMEZAWA Hiroyuki if (swap_was_freed) 452c9e44410SKAMEZAWA Hiroyuki goto checks; 453c9e44410SKAMEZAWA Hiroyuki goto scan; /* check next one */ 454c9e44410SKAMEZAWA Hiroyuki } 455c9e44410SKAMEZAWA Hiroyuki 456ebebbbe9SHugh Dickins if (si->swap_map[offset]) 457ebebbbe9SHugh Dickins goto scan; 458ebebbbe9SHugh Dickins 45952b7efdbSHugh Dickins if (offset == si->lowest_bit) 4601da177e4SLinus Torvalds si->lowest_bit++; 4611da177e4SLinus Torvalds if (offset == si->highest_bit) 4621da177e4SLinus Torvalds si->highest_bit--; 4637dfad418SHugh Dickins si->inuse_pages++; 4647dfad418SHugh Dickins if (si->inuse_pages == si->pages) { 4651da177e4SLinus Torvalds si->lowest_bit = si->max; 4661da177e4SLinus Torvalds si->highest_bit = 0; 4671da177e4SLinus Torvalds } 468253d553bSHugh Dickins si->swap_map[offset] = usage; 469*2a8f9449SShaohua Li inc_cluster_info_page(si, si->cluster_info, offset); 4701da177e4SLinus Torvalds si->cluster_next = offset + 1; 47152b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 4727992fde7SHugh Dickins 4737992fde7SHugh Dickins if (si->lowest_alloc) { 4747992fde7SHugh Dickins /* 475dcf6b7ddSRafael Aquini * Only set when SWP_PAGE_DISCARD, and there's a scan 4767992fde7SHugh Dickins * for a free cluster in progress or just completed. 4777992fde7SHugh Dickins */ 4787992fde7SHugh Dickins if (found_free_cluster) { 4797992fde7SHugh Dickins /* 4807992fde7SHugh Dickins * To optimize wear-levelling, discard the 4817992fde7SHugh Dickins * old data of the cluster, taking care not to 4827992fde7SHugh Dickins * discard any of its pages that have already 4837992fde7SHugh Dickins * been allocated by racing tasks (offset has 4847992fde7SHugh Dickins * already stepped over any at the beginning). 4857992fde7SHugh Dickins */ 4867992fde7SHugh Dickins if (offset < si->highest_alloc && 4877992fde7SHugh Dickins si->lowest_alloc <= last_in_cluster) 4887992fde7SHugh Dickins last_in_cluster = si->lowest_alloc - 1; 4897992fde7SHugh Dickins si->flags |= SWP_DISCARDING; 490ec8acf20SShaohua Li spin_unlock(&si->lock); 4917992fde7SHugh Dickins 4927992fde7SHugh Dickins if (offset < last_in_cluster) 4937992fde7SHugh Dickins discard_swap_cluster(si, offset, 4947992fde7SHugh Dickins last_in_cluster - offset + 1); 4957992fde7SHugh Dickins 496ec8acf20SShaohua Li spin_lock(&si->lock); 4977992fde7SHugh Dickins si->lowest_alloc = 0; 4987992fde7SHugh Dickins si->flags &= ~SWP_DISCARDING; 4997992fde7SHugh Dickins 5007992fde7SHugh Dickins smp_mb(); /* wake_up_bit advises this */ 5017992fde7SHugh Dickins wake_up_bit(&si->flags, ilog2(SWP_DISCARDING)); 5027992fde7SHugh Dickins 5037992fde7SHugh Dickins } else if (si->flags & SWP_DISCARDING) { 5047992fde7SHugh Dickins /* 5057992fde7SHugh Dickins * Delay using pages allocated by racing tasks 5067992fde7SHugh Dickins * until the whole discard has been issued. We 5077992fde7SHugh Dickins * could defer that delay until swap_writepage, 5087992fde7SHugh Dickins * but it's easier to keep this self-contained. 5097992fde7SHugh Dickins */ 510ec8acf20SShaohua Li spin_unlock(&si->lock); 5117992fde7SHugh Dickins wait_on_bit(&si->flags, ilog2(SWP_DISCARDING), 5127992fde7SHugh Dickins wait_for_discard, TASK_UNINTERRUPTIBLE); 513ec8acf20SShaohua Li spin_lock(&si->lock); 5147992fde7SHugh Dickins } else { 5157992fde7SHugh Dickins /* 5167992fde7SHugh Dickins * Note pages allocated by racing tasks while 5177992fde7SHugh Dickins * scan for a free cluster is in progress, so 5187992fde7SHugh Dickins * that its final discard can exclude them. 5197992fde7SHugh Dickins */ 5207992fde7SHugh Dickins if (offset < si->lowest_alloc) 5217992fde7SHugh Dickins si->lowest_alloc = offset; 5227992fde7SHugh Dickins if (offset > si->highest_alloc) 5237992fde7SHugh Dickins si->highest_alloc = offset; 5247992fde7SHugh Dickins } 5257992fde7SHugh Dickins } 5261da177e4SLinus Torvalds return offset; 5277dfad418SHugh Dickins 528ebebbbe9SHugh Dickins scan: 529ec8acf20SShaohua Li spin_unlock(&si->lock); 5307dfad418SHugh Dickins while (++offset <= si->highest_bit) { 53152b7efdbSHugh Dickins if (!si->swap_map[offset]) { 532ec8acf20SShaohua Li spin_lock(&si->lock); 53352b7efdbSHugh Dickins goto checks; 5347dfad418SHugh Dickins } 535c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 536ec8acf20SShaohua Li spin_lock(&si->lock); 537c9e44410SKAMEZAWA Hiroyuki goto checks; 538c9e44410SKAMEZAWA Hiroyuki } 539048c27fdSHugh Dickins if (unlikely(--latency_ration < 0)) { 540048c27fdSHugh Dickins cond_resched(); 541048c27fdSHugh Dickins latency_ration = LATENCY_LIMIT; 542048c27fdSHugh Dickins } 54352b7efdbSHugh Dickins } 544c60aa176SHugh Dickins offset = si->lowest_bit; 545c60aa176SHugh Dickins while (++offset < scan_base) { 546c60aa176SHugh Dickins if (!si->swap_map[offset]) { 547ec8acf20SShaohua Li spin_lock(&si->lock); 548ebebbbe9SHugh Dickins goto checks; 549c60aa176SHugh Dickins } 550c9e44410SKAMEZAWA Hiroyuki if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { 551ec8acf20SShaohua Li spin_lock(&si->lock); 552c9e44410SKAMEZAWA Hiroyuki goto checks; 553c9e44410SKAMEZAWA Hiroyuki } 554c60aa176SHugh Dickins if (unlikely(--latency_ration < 0)) { 555c60aa176SHugh Dickins cond_resched(); 556c60aa176SHugh Dickins latency_ration = LATENCY_LIMIT; 557c60aa176SHugh Dickins } 558c60aa176SHugh Dickins } 559ec8acf20SShaohua Li spin_lock(&si->lock); 5607dfad418SHugh Dickins 5617dfad418SHugh Dickins no_page: 56252b7efdbSHugh Dickins si->flags -= SWP_SCANNING; 5631da177e4SLinus Torvalds return 0; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds swp_entry_t get_swap_page(void) 5671da177e4SLinus Torvalds { 568fb4f88dcSHugh Dickins struct swap_info_struct *si; 569fb4f88dcSHugh Dickins pgoff_t offset; 570fb4f88dcSHugh Dickins int type, next; 571fb4f88dcSHugh Dickins int wrapped = 0; 572ec8acf20SShaohua Li int hp_index; 5731da177e4SLinus Torvalds 5745d337b91SHugh Dickins spin_lock(&swap_lock); 575ec8acf20SShaohua Li if (atomic_long_read(&nr_swap_pages) <= 0) 576fb4f88dcSHugh Dickins goto noswap; 577ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 5781da177e4SLinus Torvalds 579fb4f88dcSHugh Dickins for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { 580ec8acf20SShaohua Li hp_index = atomic_xchg(&highest_priority_index, -1); 581ec8acf20SShaohua Li /* 582ec8acf20SShaohua Li * highest_priority_index records current highest priority swap 583ec8acf20SShaohua Li * type which just frees swap entries. If its priority is 584ec8acf20SShaohua Li * higher than that of swap_list.next swap type, we use it. It 585ec8acf20SShaohua Li * isn't protected by swap_lock, so it can be an invalid value 586ec8acf20SShaohua Li * if the corresponding swap type is swapoff. We double check 587ec8acf20SShaohua Li * the flags here. It's even possible the swap type is swapoff 588ec8acf20SShaohua Li * and swapon again and its priority is changed. In such rare 589ec8acf20SShaohua Li * case, low prority swap type might be used, but eventually 590ec8acf20SShaohua Li * high priority swap will be used after several rounds of 591ec8acf20SShaohua Li * swap. 592ec8acf20SShaohua Li */ 593ec8acf20SShaohua Li if (hp_index != -1 && hp_index != type && 594ec8acf20SShaohua Li swap_info[type]->prio < swap_info[hp_index]->prio && 595ec8acf20SShaohua Li (swap_info[hp_index]->flags & SWP_WRITEOK)) { 596ec8acf20SShaohua Li type = hp_index; 597ec8acf20SShaohua Li swap_list.next = type; 598ec8acf20SShaohua Li } 599ec8acf20SShaohua Li 600efa90a98SHugh Dickins si = swap_info[type]; 601fb4f88dcSHugh Dickins next = si->next; 602fb4f88dcSHugh Dickins if (next < 0 || 603efa90a98SHugh Dickins (!wrapped && si->prio != swap_info[next]->prio)) { 604fb4f88dcSHugh Dickins next = swap_list.head; 605fb4f88dcSHugh Dickins wrapped++; 6061da177e4SLinus Torvalds } 607fb4f88dcSHugh Dickins 608ec8acf20SShaohua Li spin_lock(&si->lock); 609ec8acf20SShaohua Li if (!si->highest_bit) { 610ec8acf20SShaohua Li spin_unlock(&si->lock); 611fb4f88dcSHugh Dickins continue; 612ec8acf20SShaohua Li } 613ec8acf20SShaohua Li if (!(si->flags & SWP_WRITEOK)) { 614ec8acf20SShaohua Li spin_unlock(&si->lock); 615fb4f88dcSHugh Dickins continue; 616ec8acf20SShaohua Li } 617fb4f88dcSHugh Dickins 618fb4f88dcSHugh Dickins swap_list.next = next; 619ec8acf20SShaohua Li 620ec8acf20SShaohua Li spin_unlock(&swap_lock); 621355cfa73SKAMEZAWA Hiroyuki /* This is called for allocating swap entry for cache */ 622253d553bSHugh Dickins offset = scan_swap_map(si, SWAP_HAS_CACHE); 623ec8acf20SShaohua Li spin_unlock(&si->lock); 624ec8acf20SShaohua Li if (offset) 625fb4f88dcSHugh Dickins return swp_entry(type, offset); 626ec8acf20SShaohua Li spin_lock(&swap_lock); 627fb4f88dcSHugh Dickins next = swap_list.next; 628fb4f88dcSHugh Dickins } 629fb4f88dcSHugh Dickins 630ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 631fb4f88dcSHugh Dickins noswap: 6325d337b91SHugh Dickins spin_unlock(&swap_lock); 633fb4f88dcSHugh Dickins return (swp_entry_t) {0}; 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636910321eaSHugh Dickins /* The only caller of this function is now susupend routine */ 637910321eaSHugh Dickins swp_entry_t get_swap_page_of_type(int type) 638910321eaSHugh Dickins { 639910321eaSHugh Dickins struct swap_info_struct *si; 640910321eaSHugh Dickins pgoff_t offset; 641910321eaSHugh Dickins 642910321eaSHugh Dickins si = swap_info[type]; 643ec8acf20SShaohua Li spin_lock(&si->lock); 644910321eaSHugh Dickins if (si && (si->flags & SWP_WRITEOK)) { 645ec8acf20SShaohua Li atomic_long_dec(&nr_swap_pages); 646910321eaSHugh Dickins /* This is called for allocating swap entry, not cache */ 647910321eaSHugh Dickins offset = scan_swap_map(si, 1); 648910321eaSHugh Dickins if (offset) { 649ec8acf20SShaohua Li spin_unlock(&si->lock); 650910321eaSHugh Dickins return swp_entry(type, offset); 651910321eaSHugh Dickins } 652ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 653910321eaSHugh Dickins } 654ec8acf20SShaohua Li spin_unlock(&si->lock); 655910321eaSHugh Dickins return (swp_entry_t) {0}; 656910321eaSHugh Dickins } 657910321eaSHugh Dickins 6581da177e4SLinus Torvalds static struct swap_info_struct *swap_info_get(swp_entry_t entry) 6591da177e4SLinus Torvalds { 6601da177e4SLinus Torvalds struct swap_info_struct *p; 6611da177e4SLinus Torvalds unsigned long offset, type; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds if (!entry.val) 6641da177e4SLinus Torvalds goto out; 6651da177e4SLinus Torvalds type = swp_type(entry); 6661da177e4SLinus Torvalds if (type >= nr_swapfiles) 6671da177e4SLinus Torvalds goto bad_nofile; 668efa90a98SHugh Dickins p = swap_info[type]; 6691da177e4SLinus Torvalds if (!(p->flags & SWP_USED)) 6701da177e4SLinus Torvalds goto bad_device; 6711da177e4SLinus Torvalds offset = swp_offset(entry); 6721da177e4SLinus Torvalds if (offset >= p->max) 6731da177e4SLinus Torvalds goto bad_offset; 6741da177e4SLinus Torvalds if (!p->swap_map[offset]) 6751da177e4SLinus Torvalds goto bad_free; 676ec8acf20SShaohua Li spin_lock(&p->lock); 6771da177e4SLinus Torvalds return p; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds bad_free: 680465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Unused_offset, entry.val); 6811da177e4SLinus Torvalds goto out; 6821da177e4SLinus Torvalds bad_offset: 683465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Bad_offset, entry.val); 6841da177e4SLinus Torvalds goto out; 6851da177e4SLinus Torvalds bad_device: 686465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Unused_file, entry.val); 6871da177e4SLinus Torvalds goto out; 6881da177e4SLinus Torvalds bad_nofile: 689465c47fdSAndrew Morton pr_err("swap_free: %s%08lx\n", Bad_file, entry.val); 6901da177e4SLinus Torvalds out: 6911da177e4SLinus Torvalds return NULL; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 694ec8acf20SShaohua Li /* 695ec8acf20SShaohua Li * This swap type frees swap entry, check if it is the highest priority swap 696ec8acf20SShaohua Li * type which just frees swap entry. get_swap_page() uses 697ec8acf20SShaohua Li * highest_priority_index to search highest priority swap type. The 698ec8acf20SShaohua Li * swap_info_struct.lock can't protect us if there are multiple swap types 699ec8acf20SShaohua Li * active, so we use atomic_cmpxchg. 700ec8acf20SShaohua Li */ 701ec8acf20SShaohua Li static void set_highest_priority_index(int type) 702ec8acf20SShaohua Li { 703ec8acf20SShaohua Li int old_hp_index, new_hp_index; 704ec8acf20SShaohua Li 705ec8acf20SShaohua Li do { 706ec8acf20SShaohua Li old_hp_index = atomic_read(&highest_priority_index); 707ec8acf20SShaohua Li if (old_hp_index != -1 && 708ec8acf20SShaohua Li swap_info[old_hp_index]->prio >= swap_info[type]->prio) 709ec8acf20SShaohua Li break; 710ec8acf20SShaohua Li new_hp_index = type; 711ec8acf20SShaohua Li } while (atomic_cmpxchg(&highest_priority_index, 712ec8acf20SShaohua Li old_hp_index, new_hp_index) != old_hp_index); 713ec8acf20SShaohua Li } 714ec8acf20SShaohua Li 7158d69aaeeSHugh Dickins static unsigned char swap_entry_free(struct swap_info_struct *p, 7168d69aaeeSHugh Dickins swp_entry_t entry, unsigned char usage) 7171da177e4SLinus Torvalds { 718253d553bSHugh Dickins unsigned long offset = swp_offset(entry); 7198d69aaeeSHugh Dickins unsigned char count; 7208d69aaeeSHugh Dickins unsigned char has_cache; 7211da177e4SLinus Torvalds 722355cfa73SKAMEZAWA Hiroyuki count = p->swap_map[offset]; 723253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 724253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 725253d553bSHugh Dickins 726253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 727253d553bSHugh Dickins VM_BUG_ON(!has_cache); 728253d553bSHugh Dickins has_cache = 0; 729aaa46865SHugh Dickins } else if (count == SWAP_MAP_SHMEM) { 730aaa46865SHugh Dickins /* 731aaa46865SHugh Dickins * Or we could insist on shmem.c using a special 732aaa46865SHugh Dickins * swap_shmem_free() and free_shmem_swap_and_cache()... 733aaa46865SHugh Dickins */ 734aaa46865SHugh Dickins count = 0; 735570a335bSHugh Dickins } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { 736570a335bSHugh Dickins if (count == COUNT_CONTINUED) { 737570a335bSHugh Dickins if (swap_count_continued(p, offset, count)) 738570a335bSHugh Dickins count = SWAP_MAP_MAX | COUNT_CONTINUED; 739570a335bSHugh Dickins else 740570a335bSHugh Dickins count = SWAP_MAP_MAX; 741570a335bSHugh Dickins } else 742253d553bSHugh Dickins count--; 743570a335bSHugh Dickins } 744253d553bSHugh Dickins 745253d553bSHugh Dickins if (!count) 746253d553bSHugh Dickins mem_cgroup_uncharge_swap(entry); 747253d553bSHugh Dickins 748253d553bSHugh Dickins usage = count | has_cache; 749253d553bSHugh Dickins p->swap_map[offset] = usage; 750253d553bSHugh Dickins 751355cfa73SKAMEZAWA Hiroyuki /* free if no reference */ 752253d553bSHugh Dickins if (!usage) { 753*2a8f9449SShaohua Li dec_cluster_info_page(p, p->cluster_info, offset); 7541da177e4SLinus Torvalds if (offset < p->lowest_bit) 7551da177e4SLinus Torvalds p->lowest_bit = offset; 7561da177e4SLinus Torvalds if (offset > p->highest_bit) 7571da177e4SLinus Torvalds p->highest_bit = offset; 758ec8acf20SShaohua Li set_highest_priority_index(p->type); 759ec8acf20SShaohua Li atomic_long_inc(&nr_swap_pages); 7601da177e4SLinus Torvalds p->inuse_pages--; 76138b5faf4SDan Magenheimer frontswap_invalidate_page(p->type, offset); 76273744923SMel Gorman if (p->flags & SWP_BLKDEV) { 76373744923SMel Gorman struct gendisk *disk = p->bdev->bd_disk; 76473744923SMel Gorman if (disk->fops->swap_slot_free_notify) 76573744923SMel Gorman disk->fops->swap_slot_free_notify(p->bdev, 76673744923SMel Gorman offset); 76773744923SMel Gorman } 7681da177e4SLinus Torvalds } 769253d553bSHugh Dickins 770253d553bSHugh Dickins return usage; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /* 7741da177e4SLinus Torvalds * Caller has made sure that the swapdevice corresponding to entry 7751da177e4SLinus Torvalds * is still around or has not been recycled. 7761da177e4SLinus Torvalds */ 7771da177e4SLinus Torvalds void swap_free(swp_entry_t entry) 7781da177e4SLinus Torvalds { 7791da177e4SLinus Torvalds struct swap_info_struct *p; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds p = swap_info_get(entry); 7821da177e4SLinus Torvalds if (p) { 783253d553bSHugh Dickins swap_entry_free(p, entry, 1); 784ec8acf20SShaohua Li spin_unlock(&p->lock); 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds /* 789cb4b86baSKAMEZAWA Hiroyuki * Called after dropping swapcache to decrease refcnt to swap entries. 790cb4b86baSKAMEZAWA Hiroyuki */ 791cb4b86baSKAMEZAWA Hiroyuki void swapcache_free(swp_entry_t entry, struct page *page) 792cb4b86baSKAMEZAWA Hiroyuki { 793355cfa73SKAMEZAWA Hiroyuki struct swap_info_struct *p; 7948d69aaeeSHugh Dickins unsigned char count; 795355cfa73SKAMEZAWA Hiroyuki 796355cfa73SKAMEZAWA Hiroyuki p = swap_info_get(entry); 797355cfa73SKAMEZAWA Hiroyuki if (p) { 798253d553bSHugh Dickins count = swap_entry_free(p, entry, SWAP_HAS_CACHE); 799253d553bSHugh Dickins if (page) 800253d553bSHugh Dickins mem_cgroup_uncharge_swapcache(page, entry, count != 0); 801ec8acf20SShaohua Li spin_unlock(&p->lock); 802355cfa73SKAMEZAWA Hiroyuki } 803cb4b86baSKAMEZAWA Hiroyuki } 804cb4b86baSKAMEZAWA Hiroyuki 805cb4b86baSKAMEZAWA Hiroyuki /* 806c475a8abSHugh Dickins * How many references to page are currently swapped out? 807570a335bSHugh Dickins * This does not give an exact answer when swap count is continued, 808570a335bSHugh Dickins * but does include the high COUNT_CONTINUED flag to allow for that. 8091da177e4SLinus Torvalds */ 810bde05d1cSHugh Dickins int page_swapcount(struct page *page) 8111da177e4SLinus Torvalds { 812c475a8abSHugh Dickins int count = 0; 8131da177e4SLinus Torvalds struct swap_info_struct *p; 8141da177e4SLinus Torvalds swp_entry_t entry; 8151da177e4SLinus Torvalds 8164c21e2f2SHugh Dickins entry.val = page_private(page); 8171da177e4SLinus Torvalds p = swap_info_get(entry); 8181da177e4SLinus Torvalds if (p) { 819355cfa73SKAMEZAWA Hiroyuki count = swap_count(p->swap_map[swp_offset(entry)]); 820ec8acf20SShaohua Li spin_unlock(&p->lock); 8211da177e4SLinus Torvalds } 822c475a8abSHugh Dickins return count; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /* 8267b1fe597SHugh Dickins * We can write to an anon page without COW if there are no other references 8277b1fe597SHugh Dickins * to it. And as a side-effect, free up its swap: because the old content 8287b1fe597SHugh Dickins * on disk will never be read, and seeking back there to write new content 8297b1fe597SHugh Dickins * later would only waste time away from clustering. 8301da177e4SLinus Torvalds */ 8317b1fe597SHugh Dickins int reuse_swap_page(struct page *page) 8321da177e4SLinus Torvalds { 833c475a8abSHugh Dickins int count; 8341da177e4SLinus Torvalds 83551726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 8365ad64688SHugh Dickins if (unlikely(PageKsm(page))) 8375ad64688SHugh Dickins return 0; 838c475a8abSHugh Dickins count = page_mapcount(page); 8397b1fe597SHugh Dickins if (count <= 1 && PageSwapCache(page)) { 840c475a8abSHugh Dickins count += page_swapcount(page); 8417b1fe597SHugh Dickins if (count == 1 && !PageWriteback(page)) { 8427b1fe597SHugh Dickins delete_from_swap_cache(page); 8437b1fe597SHugh Dickins SetPageDirty(page); 8447b1fe597SHugh Dickins } 8457b1fe597SHugh Dickins } 8465ad64688SHugh Dickins return count <= 1; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds /* 850a2c43eedSHugh Dickins * If swap is getting full, or if there are no more mappings of this page, 851a2c43eedSHugh Dickins * then try_to_free_swap is called to free its swap space. 8521da177e4SLinus Torvalds */ 853a2c43eedSHugh Dickins int try_to_free_swap(struct page *page) 8541da177e4SLinus Torvalds { 85551726b12SHugh Dickins VM_BUG_ON(!PageLocked(page)); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds if (!PageSwapCache(page)) 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds if (PageWriteback(page)) 8601da177e4SLinus Torvalds return 0; 861a2c43eedSHugh Dickins if (page_swapcount(page)) 8621da177e4SLinus Torvalds return 0; 8631da177e4SLinus Torvalds 864b73d7fceSHugh Dickins /* 865b73d7fceSHugh Dickins * Once hibernation has begun to create its image of memory, 866b73d7fceSHugh Dickins * there's a danger that one of the calls to try_to_free_swap() 867b73d7fceSHugh Dickins * - most probably a call from __try_to_reclaim_swap() while 868b73d7fceSHugh Dickins * hibernation is allocating its own swap pages for the image, 869b73d7fceSHugh Dickins * but conceivably even a call from memory reclaim - will free 870b73d7fceSHugh Dickins * the swap from a page which has already been recorded in the 871b73d7fceSHugh Dickins * image as a clean swapcache page, and then reuse its swap for 872b73d7fceSHugh Dickins * another page of the image. On waking from hibernation, the 873b73d7fceSHugh Dickins * original page might be freed under memory pressure, then 874b73d7fceSHugh Dickins * later read back in from swap, now with the wrong data. 875b73d7fceSHugh Dickins * 876f90ac398SMel Gorman * Hibration suspends storage while it is writing the image 877f90ac398SMel Gorman * to disk so check that here. 878b73d7fceSHugh Dickins */ 879f90ac398SMel Gorman if (pm_suspended_storage()) 880b73d7fceSHugh Dickins return 0; 881b73d7fceSHugh Dickins 882a2c43eedSHugh Dickins delete_from_swap_cache(page); 8831da177e4SLinus Torvalds SetPageDirty(page); 884a2c43eedSHugh Dickins return 1; 88568a22394SRik van Riel } 88668a22394SRik van Riel 88768a22394SRik van Riel /* 8881da177e4SLinus Torvalds * Free the swap entry like above, but also try to 8891da177e4SLinus Torvalds * free the page cache entry if it is the last user. 8901da177e4SLinus Torvalds */ 8912509ef26SHugh Dickins int free_swap_and_cache(swp_entry_t entry) 8921da177e4SLinus Torvalds { 8931da177e4SLinus Torvalds struct swap_info_struct *p; 8941da177e4SLinus Torvalds struct page *page = NULL; 8951da177e4SLinus Torvalds 896a7420aa5SAndi Kleen if (non_swap_entry(entry)) 8972509ef26SHugh Dickins return 1; 8980697212aSChristoph Lameter 8991da177e4SLinus Torvalds p = swap_info_get(entry); 9001da177e4SLinus Torvalds if (p) { 901253d553bSHugh Dickins if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) { 90233806f06SShaohua Li page = find_get_page(swap_address_space(entry), 90333806f06SShaohua Li entry.val); 9048413ac9dSNick Piggin if (page && !trylock_page(page)) { 90593fac704SNick Piggin page_cache_release(page); 90693fac704SNick Piggin page = NULL; 90793fac704SNick Piggin } 90893fac704SNick Piggin } 909ec8acf20SShaohua Li spin_unlock(&p->lock); 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds if (page) { 912a2c43eedSHugh Dickins /* 913a2c43eedSHugh Dickins * Not mapped elsewhere, or swap space full? Free it! 914a2c43eedSHugh Dickins * Also recheck PageSwapCache now page is locked (above). 915a2c43eedSHugh Dickins */ 91693fac704SNick Piggin if (PageSwapCache(page) && !PageWriteback(page) && 917a2c43eedSHugh Dickins (!page_mapped(page) || vm_swap_full())) { 9181da177e4SLinus Torvalds delete_from_swap_cache(page); 9191da177e4SLinus Torvalds SetPageDirty(page); 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds unlock_page(page); 9221da177e4SLinus Torvalds page_cache_release(page); 9231da177e4SLinus Torvalds } 9242509ef26SHugh Dickins return p != NULL; 9251da177e4SLinus Torvalds } 9261da177e4SLinus Torvalds 927b0cb1a19SRafael J. Wysocki #ifdef CONFIG_HIBERNATION 928f577eb30SRafael J. Wysocki /* 929915bae9eSRafael J. Wysocki * Find the swap type that corresponds to given device (if any). 930f577eb30SRafael J. Wysocki * 931915bae9eSRafael J. Wysocki * @offset - number of the PAGE_SIZE-sized block of the device, starting 932915bae9eSRafael J. Wysocki * from 0, in which the swap header is expected to be located. 933915bae9eSRafael J. Wysocki * 934915bae9eSRafael J. Wysocki * This is needed for the suspend to disk (aka swsusp). 935f577eb30SRafael J. Wysocki */ 9367bf23687SRafael J. Wysocki int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) 937f577eb30SRafael J. Wysocki { 938915bae9eSRafael J. Wysocki struct block_device *bdev = NULL; 939efa90a98SHugh Dickins int type; 940f577eb30SRafael J. Wysocki 941915bae9eSRafael J. Wysocki if (device) 942915bae9eSRafael J. Wysocki bdev = bdget(device); 943915bae9eSRafael J. Wysocki 944f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 945efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 946efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 947f577eb30SRafael J. Wysocki 948915bae9eSRafael J. Wysocki if (!(sis->flags & SWP_WRITEOK)) 949f577eb30SRafael J. Wysocki continue; 950b6b5bce3SRafael J. Wysocki 951915bae9eSRafael J. Wysocki if (!bdev) { 9527bf23687SRafael J. Wysocki if (bdev_p) 953dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 9547bf23687SRafael J. Wysocki 9556e1819d6SRafael J. Wysocki spin_unlock(&swap_lock); 956efa90a98SHugh Dickins return type; 9576e1819d6SRafael J. Wysocki } 958915bae9eSRafael J. Wysocki if (bdev == sis->bdev) { 9599625a5f2SHugh Dickins struct swap_extent *se = &sis->first_swap_extent; 960915bae9eSRafael J. Wysocki 961915bae9eSRafael J. Wysocki if (se->start_block == offset) { 9627bf23687SRafael J. Wysocki if (bdev_p) 963dddac6a7SAlan Jenkins *bdev_p = bdgrab(sis->bdev); 9647bf23687SRafael J. Wysocki 965f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 966915bae9eSRafael J. Wysocki bdput(bdev); 967efa90a98SHugh Dickins return type; 968f577eb30SRafael J. Wysocki } 969f577eb30SRafael J. Wysocki } 970915bae9eSRafael J. Wysocki } 971f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 972915bae9eSRafael J. Wysocki if (bdev) 973915bae9eSRafael J. Wysocki bdput(bdev); 974915bae9eSRafael J. Wysocki 975f577eb30SRafael J. Wysocki return -ENODEV; 976f577eb30SRafael J. Wysocki } 977f577eb30SRafael J. Wysocki 978f577eb30SRafael J. Wysocki /* 97973c34b6aSHugh Dickins * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev 98073c34b6aSHugh Dickins * corresponding to given index in swap_info (swap type). 98173c34b6aSHugh Dickins */ 98273c34b6aSHugh Dickins sector_t swapdev_block(int type, pgoff_t offset) 98373c34b6aSHugh Dickins { 98473c34b6aSHugh Dickins struct block_device *bdev; 98573c34b6aSHugh Dickins 98673c34b6aSHugh Dickins if ((unsigned int)type >= nr_swapfiles) 98773c34b6aSHugh Dickins return 0; 98873c34b6aSHugh Dickins if (!(swap_info[type]->flags & SWP_WRITEOK)) 98973c34b6aSHugh Dickins return 0; 990d4906e1aSLee Schermerhorn return map_swap_entry(swp_entry(type, offset), &bdev); 99173c34b6aSHugh Dickins } 99273c34b6aSHugh Dickins 99373c34b6aSHugh Dickins /* 994f577eb30SRafael J. Wysocki * Return either the total number of swap pages of given type, or the number 995f577eb30SRafael J. Wysocki * of free pages of that type (depending on @free) 996f577eb30SRafael J. Wysocki * 997f577eb30SRafael J. Wysocki * This is needed for software suspend 998f577eb30SRafael J. Wysocki */ 999f577eb30SRafael J. Wysocki unsigned int count_swap_pages(int type, int free) 1000f577eb30SRafael J. Wysocki { 1001f577eb30SRafael J. Wysocki unsigned int n = 0; 1002f577eb30SRafael J. Wysocki 1003f577eb30SRafael J. Wysocki spin_lock(&swap_lock); 1004efa90a98SHugh Dickins if ((unsigned int)type < nr_swapfiles) { 1005efa90a98SHugh Dickins struct swap_info_struct *sis = swap_info[type]; 1006efa90a98SHugh Dickins 1007ec8acf20SShaohua Li spin_lock(&sis->lock); 1008efa90a98SHugh Dickins if (sis->flags & SWP_WRITEOK) { 1009efa90a98SHugh Dickins n = sis->pages; 1010f577eb30SRafael J. Wysocki if (free) 1011efa90a98SHugh Dickins n -= sis->inuse_pages; 1012efa90a98SHugh Dickins } 1013ec8acf20SShaohua Li spin_unlock(&sis->lock); 1014f577eb30SRafael J. Wysocki } 1015f577eb30SRafael J. Wysocki spin_unlock(&swap_lock); 1016f577eb30SRafael J. Wysocki return n; 1017f577eb30SRafael J. Wysocki } 101873c34b6aSHugh Dickins #endif /* CONFIG_HIBERNATION */ 1019f577eb30SRafael J. Wysocki 1020179ef71cSCyrill Gorcunov static inline int maybe_same_pte(pte_t pte, pte_t swp_pte) 1021179ef71cSCyrill Gorcunov { 1022179ef71cSCyrill Gorcunov #ifdef CONFIG_MEM_SOFT_DIRTY 1023179ef71cSCyrill Gorcunov /* 1024179ef71cSCyrill Gorcunov * When pte keeps soft dirty bit the pte generated 1025179ef71cSCyrill Gorcunov * from swap entry does not has it, still it's same 1026179ef71cSCyrill Gorcunov * pte from logical point of view. 1027179ef71cSCyrill Gorcunov */ 1028179ef71cSCyrill Gorcunov pte_t swp_pte_dirty = pte_swp_mksoft_dirty(swp_pte); 1029179ef71cSCyrill Gorcunov return pte_same(pte, swp_pte) || pte_same(pte, swp_pte_dirty); 1030179ef71cSCyrill Gorcunov #else 1031179ef71cSCyrill Gorcunov return pte_same(pte, swp_pte); 1032179ef71cSCyrill Gorcunov #endif 1033179ef71cSCyrill Gorcunov } 1034179ef71cSCyrill Gorcunov 10351da177e4SLinus Torvalds /* 103672866f6fSHugh Dickins * No need to decide whether this PTE shares the swap entry with others, 103772866f6fSHugh Dickins * just let do_wp_page work it out if a write is requested later - to 103872866f6fSHugh Dickins * force COW, vm_page_prot omits write permission from any private vma. 10391da177e4SLinus Torvalds */ 1040044d66c1SHugh Dickins static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, 10411da177e4SLinus Torvalds unsigned long addr, swp_entry_t entry, struct page *page) 10421da177e4SLinus Torvalds { 10439e16b7fbSHugh Dickins struct page *swapcache; 104472835c86SJohannes Weiner struct mem_cgroup *memcg; 1045044d66c1SHugh Dickins spinlock_t *ptl; 1046044d66c1SHugh Dickins pte_t *pte; 1047044d66c1SHugh Dickins int ret = 1; 1048044d66c1SHugh Dickins 10499e16b7fbSHugh Dickins swapcache = page; 10509e16b7fbSHugh Dickins page = ksm_might_need_to_copy(page, vma, addr); 10519e16b7fbSHugh Dickins if (unlikely(!page)) 10529e16b7fbSHugh Dickins return -ENOMEM; 10539e16b7fbSHugh Dickins 105472835c86SJohannes Weiner if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, 105572835c86SJohannes Weiner GFP_KERNEL, &memcg)) { 1056044d66c1SHugh Dickins ret = -ENOMEM; 105785d9fc89SKAMEZAWA Hiroyuki goto out_nolock; 105885d9fc89SKAMEZAWA Hiroyuki } 1059044d66c1SHugh Dickins 1060044d66c1SHugh Dickins pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); 1061179ef71cSCyrill Gorcunov if (unlikely(!maybe_same_pte(*pte, swp_entry_to_pte(entry)))) { 106272835c86SJohannes Weiner mem_cgroup_cancel_charge_swapin(memcg); 1063044d66c1SHugh Dickins ret = 0; 1064044d66c1SHugh Dickins goto out; 1065044d66c1SHugh Dickins } 10668a9f3ccdSBalbir Singh 1067b084d435SKAMEZAWA Hiroyuki dec_mm_counter(vma->vm_mm, MM_SWAPENTS); 1068d559db08SKAMEZAWA Hiroyuki inc_mm_counter(vma->vm_mm, MM_ANONPAGES); 10691da177e4SLinus Torvalds get_page(page); 10701da177e4SLinus Torvalds set_pte_at(vma->vm_mm, addr, pte, 10711da177e4SLinus Torvalds pte_mkold(mk_pte(page, vma->vm_page_prot))); 10729e16b7fbSHugh Dickins if (page == swapcache) 10731da177e4SLinus Torvalds page_add_anon_rmap(page, vma, addr); 10749e16b7fbSHugh Dickins else /* ksm created a completely new copy */ 10759e16b7fbSHugh Dickins page_add_new_anon_rmap(page, vma, addr); 107672835c86SJohannes Weiner mem_cgroup_commit_charge_swapin(page, memcg); 10771da177e4SLinus Torvalds swap_free(entry); 10781da177e4SLinus Torvalds /* 10791da177e4SLinus Torvalds * Move the page to the active list so it is not 10801da177e4SLinus Torvalds * immediately swapped out again after swapon. 10811da177e4SLinus Torvalds */ 10821da177e4SLinus Torvalds activate_page(page); 1083044d66c1SHugh Dickins out: 1084044d66c1SHugh Dickins pte_unmap_unlock(pte, ptl); 108585d9fc89SKAMEZAWA Hiroyuki out_nolock: 10869e16b7fbSHugh Dickins if (page != swapcache) { 10879e16b7fbSHugh Dickins unlock_page(page); 10889e16b7fbSHugh Dickins put_page(page); 10899e16b7fbSHugh Dickins } 1090044d66c1SHugh Dickins return ret; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, 10941da177e4SLinus Torvalds unsigned long addr, unsigned long end, 10951da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 10961da177e4SLinus Torvalds { 10971da177e4SLinus Torvalds pte_t swp_pte = swp_entry_to_pte(entry); 1098705e87c0SHugh Dickins pte_t *pte; 10998a9f3ccdSBalbir Singh int ret = 0; 11001da177e4SLinus Torvalds 1101044d66c1SHugh Dickins /* 1102044d66c1SHugh Dickins * We don't actually need pte lock while scanning for swp_pte: since 1103044d66c1SHugh Dickins * we hold page lock and mmap_sem, swp_pte cannot be inserted into the 1104044d66c1SHugh Dickins * page table while we're scanning; though it could get zapped, and on 1105044d66c1SHugh Dickins * some architectures (e.g. x86_32 with PAE) we might catch a glimpse 1106044d66c1SHugh Dickins * of unmatched parts which look like swp_pte, so unuse_pte must 1107044d66c1SHugh Dickins * recheck under pte lock. Scanning without pte lock lets it be 1108044d66c1SHugh Dickins * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. 1109044d66c1SHugh Dickins */ 1110044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 11111da177e4SLinus Torvalds do { 11121da177e4SLinus Torvalds /* 11131da177e4SLinus Torvalds * swapoff spends a _lot_ of time in this loop! 11141da177e4SLinus Torvalds * Test inline before going to call unuse_pte. 11151da177e4SLinus Torvalds */ 1116179ef71cSCyrill Gorcunov if (unlikely(maybe_same_pte(*pte, swp_pte))) { 1117044d66c1SHugh Dickins pte_unmap(pte); 1118044d66c1SHugh Dickins ret = unuse_pte(vma, pmd, addr, entry, page); 1119044d66c1SHugh Dickins if (ret) 1120044d66c1SHugh Dickins goto out; 1121044d66c1SHugh Dickins pte = pte_offset_map(pmd, addr); 11221da177e4SLinus Torvalds } 11231da177e4SLinus Torvalds } while (pte++, addr += PAGE_SIZE, addr != end); 1124044d66c1SHugh Dickins pte_unmap(pte - 1); 1125044d66c1SHugh Dickins out: 11268a9f3ccdSBalbir Singh return ret; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, 11301da177e4SLinus Torvalds unsigned long addr, unsigned long end, 11311da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 11321da177e4SLinus Torvalds { 11331da177e4SLinus Torvalds pmd_t *pmd; 11341da177e4SLinus Torvalds unsigned long next; 11358a9f3ccdSBalbir Singh int ret; 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds pmd = pmd_offset(pud, addr); 11381da177e4SLinus Torvalds do { 11391da177e4SLinus Torvalds next = pmd_addr_end(addr, end); 11401a5a9906SAndrea Arcangeli if (pmd_none_or_trans_huge_or_clear_bad(pmd)) 11411da177e4SLinus Torvalds continue; 11428a9f3ccdSBalbir Singh ret = unuse_pte_range(vma, pmd, addr, next, entry, page); 11438a9f3ccdSBalbir Singh if (ret) 11448a9f3ccdSBalbir Singh return ret; 11451da177e4SLinus Torvalds } while (pmd++, addr = next, addr != end); 11461da177e4SLinus Torvalds return 0; 11471da177e4SLinus Torvalds } 11481da177e4SLinus Torvalds 11491da177e4SLinus Torvalds static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, 11501da177e4SLinus Torvalds unsigned long addr, unsigned long end, 11511da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds pud_t *pud; 11541da177e4SLinus Torvalds unsigned long next; 11558a9f3ccdSBalbir Singh int ret; 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds pud = pud_offset(pgd, addr); 11581da177e4SLinus Torvalds do { 11591da177e4SLinus Torvalds next = pud_addr_end(addr, end); 11601da177e4SLinus Torvalds if (pud_none_or_clear_bad(pud)) 11611da177e4SLinus Torvalds continue; 11628a9f3ccdSBalbir Singh ret = unuse_pmd_range(vma, pud, addr, next, entry, page); 11638a9f3ccdSBalbir Singh if (ret) 11648a9f3ccdSBalbir Singh return ret; 11651da177e4SLinus Torvalds } while (pud++, addr = next, addr != end); 11661da177e4SLinus Torvalds return 0; 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds static int unuse_vma(struct vm_area_struct *vma, 11701da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 11711da177e4SLinus Torvalds { 11721da177e4SLinus Torvalds pgd_t *pgd; 11731da177e4SLinus Torvalds unsigned long addr, end, next; 11748a9f3ccdSBalbir Singh int ret; 11751da177e4SLinus Torvalds 11763ca7b3c5SHugh Dickins if (page_anon_vma(page)) { 11771da177e4SLinus Torvalds addr = page_address_in_vma(page, vma); 11781da177e4SLinus Torvalds if (addr == -EFAULT) 11791da177e4SLinus Torvalds return 0; 11801da177e4SLinus Torvalds else 11811da177e4SLinus Torvalds end = addr + PAGE_SIZE; 11821da177e4SLinus Torvalds } else { 11831da177e4SLinus Torvalds addr = vma->vm_start; 11841da177e4SLinus Torvalds end = vma->vm_end; 11851da177e4SLinus Torvalds } 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds pgd = pgd_offset(vma->vm_mm, addr); 11881da177e4SLinus Torvalds do { 11891da177e4SLinus Torvalds next = pgd_addr_end(addr, end); 11901da177e4SLinus Torvalds if (pgd_none_or_clear_bad(pgd)) 11911da177e4SLinus Torvalds continue; 11928a9f3ccdSBalbir Singh ret = unuse_pud_range(vma, pgd, addr, next, entry, page); 11938a9f3ccdSBalbir Singh if (ret) 11948a9f3ccdSBalbir Singh return ret; 11951da177e4SLinus Torvalds } while (pgd++, addr = next, addr != end); 11961da177e4SLinus Torvalds return 0; 11971da177e4SLinus Torvalds } 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds static int unuse_mm(struct mm_struct *mm, 12001da177e4SLinus Torvalds swp_entry_t entry, struct page *page) 12011da177e4SLinus Torvalds { 12021da177e4SLinus Torvalds struct vm_area_struct *vma; 12038a9f3ccdSBalbir Singh int ret = 0; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds if (!down_read_trylock(&mm->mmap_sem)) { 12061da177e4SLinus Torvalds /* 12077d03431cSFernando Luis Vazquez Cao * Activate page so shrink_inactive_list is unlikely to unmap 12087d03431cSFernando Luis Vazquez Cao * its ptes while lock is dropped, so swapoff can make progress. 12091da177e4SLinus Torvalds */ 1210c475a8abSHugh Dickins activate_page(page); 12111da177e4SLinus Torvalds unlock_page(page); 12121da177e4SLinus Torvalds down_read(&mm->mmap_sem); 12131da177e4SLinus Torvalds lock_page(page); 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds for (vma = mm->mmap; vma; vma = vma->vm_next) { 12168a9f3ccdSBalbir Singh if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) 12171da177e4SLinus Torvalds break; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds up_read(&mm->mmap_sem); 12208a9f3ccdSBalbir Singh return (ret < 0)? ret: 0; 12211da177e4SLinus Torvalds } 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds /* 122438b5faf4SDan Magenheimer * Scan swap_map (or frontswap_map if frontswap parameter is true) 122538b5faf4SDan Magenheimer * from current position to next entry still in use. 12261da177e4SLinus Torvalds * Recycle to start on reaching the end, returning 0 when empty. 12271da177e4SLinus Torvalds */ 12286eb396dcSHugh Dickins static unsigned int find_next_to_unuse(struct swap_info_struct *si, 122938b5faf4SDan Magenheimer unsigned int prev, bool frontswap) 12301da177e4SLinus Torvalds { 12316eb396dcSHugh Dickins unsigned int max = si->max; 12326eb396dcSHugh Dickins unsigned int i = prev; 12338d69aaeeSHugh Dickins unsigned char count; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds /* 12365d337b91SHugh Dickins * No need for swap_lock here: we're just looking 12371da177e4SLinus Torvalds * for whether an entry is in use, not modifying it; false 12381da177e4SLinus Torvalds * hits are okay, and sys_swapoff() has already prevented new 12395d337b91SHugh Dickins * allocations from this area (while holding swap_lock). 12401da177e4SLinus Torvalds */ 12411da177e4SLinus Torvalds for (;;) { 12421da177e4SLinus Torvalds if (++i >= max) { 12431da177e4SLinus Torvalds if (!prev) { 12441da177e4SLinus Torvalds i = 0; 12451da177e4SLinus Torvalds break; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds /* 12481da177e4SLinus Torvalds * No entries in use at top of swap_map, 12491da177e4SLinus Torvalds * loop back to start and recheck there. 12501da177e4SLinus Torvalds */ 12511da177e4SLinus Torvalds max = prev + 1; 12521da177e4SLinus Torvalds prev = 0; 12531da177e4SLinus Torvalds i = 1; 12541da177e4SLinus Torvalds } 125538b5faf4SDan Magenheimer if (frontswap) { 125638b5faf4SDan Magenheimer if (frontswap_test(si, i)) 125738b5faf4SDan Magenheimer break; 125838b5faf4SDan Magenheimer else 125938b5faf4SDan Magenheimer continue; 126038b5faf4SDan Magenheimer } 12611da177e4SLinus Torvalds count = si->swap_map[i]; 1262355cfa73SKAMEZAWA Hiroyuki if (count && swap_count(count) != SWAP_MAP_BAD) 12631da177e4SLinus Torvalds break; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds return i; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * We completely avoid races by reading each swap page in advance, 12701da177e4SLinus Torvalds * and then search for the process using it. All the necessary 12711da177e4SLinus Torvalds * page table adjustments can then be made atomically. 127238b5faf4SDan Magenheimer * 127338b5faf4SDan Magenheimer * if the boolean frontswap is true, only unuse pages_to_unuse pages; 127438b5faf4SDan Magenheimer * pages_to_unuse==0 means all pages; ignored if frontswap is false 12751da177e4SLinus Torvalds */ 127638b5faf4SDan Magenheimer int try_to_unuse(unsigned int type, bool frontswap, 127738b5faf4SDan Magenheimer unsigned long pages_to_unuse) 12781da177e4SLinus Torvalds { 1279efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 12801da177e4SLinus Torvalds struct mm_struct *start_mm; 12818d69aaeeSHugh Dickins unsigned char *swap_map; 12828d69aaeeSHugh Dickins unsigned char swcount; 12831da177e4SLinus Torvalds struct page *page; 12841da177e4SLinus Torvalds swp_entry_t entry; 12856eb396dcSHugh Dickins unsigned int i = 0; 12861da177e4SLinus Torvalds int retval = 0; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /* 12891da177e4SLinus Torvalds * When searching mms for an entry, a good strategy is to 12901da177e4SLinus Torvalds * start at the first mm we freed the previous entry from 12911da177e4SLinus Torvalds * (though actually we don't notice whether we or coincidence 12921da177e4SLinus Torvalds * freed the entry). Initialize this start_mm with a hold. 12931da177e4SLinus Torvalds * 12941da177e4SLinus Torvalds * A simpler strategy would be to start at the last mm we 12951da177e4SLinus Torvalds * freed the previous entry from; but that would take less 12961da177e4SLinus Torvalds * advantage of mmlist ordering, which clusters forked mms 12971da177e4SLinus Torvalds * together, child after parent. If we race with dup_mmap(), we 12981da177e4SLinus Torvalds * prefer to resolve parent before child, lest we miss entries 12991da177e4SLinus Torvalds * duplicated after we scanned child: using last mm would invert 1300570a335bSHugh Dickins * that. 13011da177e4SLinus Torvalds */ 13021da177e4SLinus Torvalds start_mm = &init_mm; 13031da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* 13061da177e4SLinus Torvalds * Keep on scanning until all entries have gone. Usually, 13071da177e4SLinus Torvalds * one pass through swap_map is enough, but not necessarily: 13081da177e4SLinus Torvalds * there are races when an instance of an entry might be missed. 13091da177e4SLinus Torvalds */ 131038b5faf4SDan Magenheimer while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { 13111da177e4SLinus Torvalds if (signal_pending(current)) { 13121da177e4SLinus Torvalds retval = -EINTR; 13131da177e4SLinus Torvalds break; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds /* 13171da177e4SLinus Torvalds * Get a page for the entry, using the existing swap 13181da177e4SLinus Torvalds * cache page if there is one. Otherwise, get a clean 13191da177e4SLinus Torvalds * page and read the swap into it. 13201da177e4SLinus Torvalds */ 13211da177e4SLinus Torvalds swap_map = &si->swap_map[i]; 13221da177e4SLinus Torvalds entry = swp_entry(type, i); 132302098feaSHugh Dickins page = read_swap_cache_async(entry, 132402098feaSHugh Dickins GFP_HIGHUSER_MOVABLE, NULL, 0); 13251da177e4SLinus Torvalds if (!page) { 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * Either swap_duplicate() failed because entry 13281da177e4SLinus Torvalds * has been freed independently, and will not be 13291da177e4SLinus Torvalds * reused since sys_swapoff() already disabled 13301da177e4SLinus Torvalds * allocation from here, or alloc_page() failed. 13311da177e4SLinus Torvalds */ 13321da177e4SLinus Torvalds if (!*swap_map) 13331da177e4SLinus Torvalds continue; 13341da177e4SLinus Torvalds retval = -ENOMEM; 13351da177e4SLinus Torvalds break; 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds /* 13391da177e4SLinus Torvalds * Don't hold on to start_mm if it looks like exiting. 13401da177e4SLinus Torvalds */ 13411da177e4SLinus Torvalds if (atomic_read(&start_mm->mm_users) == 1) { 13421da177e4SLinus Torvalds mmput(start_mm); 13431da177e4SLinus Torvalds start_mm = &init_mm; 13441da177e4SLinus Torvalds atomic_inc(&init_mm.mm_users); 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds /* 13481da177e4SLinus Torvalds * Wait for and lock page. When do_swap_page races with 13491da177e4SLinus Torvalds * try_to_unuse, do_swap_page can handle the fault much 13501da177e4SLinus Torvalds * faster than try_to_unuse can locate the entry. This 13511da177e4SLinus Torvalds * apparently redundant "wait_on_page_locked" lets try_to_unuse 13521da177e4SLinus Torvalds * defer to do_swap_page in such a case - in some tests, 13531da177e4SLinus Torvalds * do_swap_page and try_to_unuse repeatedly compete. 13541da177e4SLinus Torvalds */ 13551da177e4SLinus Torvalds wait_on_page_locked(page); 13561da177e4SLinus Torvalds wait_on_page_writeback(page); 13571da177e4SLinus Torvalds lock_page(page); 13581da177e4SLinus Torvalds wait_on_page_writeback(page); 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds /* 13611da177e4SLinus Torvalds * Remove all references to entry. 13621da177e4SLinus Torvalds */ 13631da177e4SLinus Torvalds swcount = *swap_map; 1364aaa46865SHugh Dickins if (swap_count(swcount) == SWAP_MAP_SHMEM) { 1365aaa46865SHugh Dickins retval = shmem_unuse(entry, page); 1366aaa46865SHugh Dickins /* page has already been unlocked and released */ 1367aaa46865SHugh Dickins if (retval < 0) 1368aaa46865SHugh Dickins break; 1369aaa46865SHugh Dickins continue; 13701da177e4SLinus Torvalds } 1371aaa46865SHugh Dickins if (swap_count(swcount) && start_mm != &init_mm) 1372aaa46865SHugh Dickins retval = unuse_mm(start_mm, entry, page); 1373aaa46865SHugh Dickins 1374355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map)) { 13751da177e4SLinus Torvalds int set_start_mm = (*swap_map >= swcount); 13761da177e4SLinus Torvalds struct list_head *p = &start_mm->mmlist; 13771da177e4SLinus Torvalds struct mm_struct *new_start_mm = start_mm; 13781da177e4SLinus Torvalds struct mm_struct *prev_mm = start_mm; 13791da177e4SLinus Torvalds struct mm_struct *mm; 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds atomic_inc(&new_start_mm->mm_users); 13821da177e4SLinus Torvalds atomic_inc(&prev_mm->mm_users); 13831da177e4SLinus Torvalds spin_lock(&mmlist_lock); 1384aaa46865SHugh Dickins while (swap_count(*swap_map) && !retval && 13851da177e4SLinus Torvalds (p = p->next) != &start_mm->mmlist) { 13861da177e4SLinus Torvalds mm = list_entry(p, struct mm_struct, mmlist); 138770af7c5cSHugh Dickins if (!atomic_inc_not_zero(&mm->mm_users)) 13881da177e4SLinus Torvalds continue; 13891da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 13901da177e4SLinus Torvalds mmput(prev_mm); 13911da177e4SLinus Torvalds prev_mm = mm; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds cond_resched(); 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds swcount = *swap_map; 1396355cfa73SKAMEZAWA Hiroyuki if (!swap_count(swcount)) /* any usage ? */ 13971da177e4SLinus Torvalds ; 1398aaa46865SHugh Dickins else if (mm == &init_mm) 13991da177e4SLinus Torvalds set_start_mm = 1; 1400aaa46865SHugh Dickins else 14011da177e4SLinus Torvalds retval = unuse_mm(mm, entry, page); 1402355cfa73SKAMEZAWA Hiroyuki 140332c5fc10SBo Liu if (set_start_mm && *swap_map < swcount) { 14041da177e4SLinus Torvalds mmput(new_start_mm); 14051da177e4SLinus Torvalds atomic_inc(&mm->mm_users); 14061da177e4SLinus Torvalds new_start_mm = mm; 14071da177e4SLinus Torvalds set_start_mm = 0; 14081da177e4SLinus Torvalds } 14091da177e4SLinus Torvalds spin_lock(&mmlist_lock); 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 14121da177e4SLinus Torvalds mmput(prev_mm); 14131da177e4SLinus Torvalds mmput(start_mm); 14141da177e4SLinus Torvalds start_mm = new_start_mm; 14151da177e4SLinus Torvalds } 14161da177e4SLinus Torvalds if (retval) { 14171da177e4SLinus Torvalds unlock_page(page); 14181da177e4SLinus Torvalds page_cache_release(page); 14191da177e4SLinus Torvalds break; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds /* 14231da177e4SLinus Torvalds * If a reference remains (rare), we would like to leave 14241da177e4SLinus Torvalds * the page in the swap cache; but try_to_unmap could 14251da177e4SLinus Torvalds * then re-duplicate the entry once we drop page lock, 14261da177e4SLinus Torvalds * so we might loop indefinitely; also, that page could 14271da177e4SLinus Torvalds * not be swapped out to other storage meanwhile. So: 14281da177e4SLinus Torvalds * delete from cache even if there's another reference, 14291da177e4SLinus Torvalds * after ensuring that the data has been saved to disk - 14301da177e4SLinus Torvalds * since if the reference remains (rarer), it will be 14311da177e4SLinus Torvalds * read from disk into another page. Splitting into two 14321da177e4SLinus Torvalds * pages would be incorrect if swap supported "shared 14331da177e4SLinus Torvalds * private" pages, but they are handled by tmpfs files. 14345ad64688SHugh Dickins * 14355ad64688SHugh Dickins * Given how unuse_vma() targets one particular offset 14365ad64688SHugh Dickins * in an anon_vma, once the anon_vma has been determined, 14375ad64688SHugh Dickins * this splitting happens to be just what is needed to 14385ad64688SHugh Dickins * handle where KSM pages have been swapped out: re-reading 14395ad64688SHugh Dickins * is unnecessarily slow, but we can fix that later on. 14401da177e4SLinus Torvalds */ 1441355cfa73SKAMEZAWA Hiroyuki if (swap_count(*swap_map) && 1442355cfa73SKAMEZAWA Hiroyuki PageDirty(page) && PageSwapCache(page)) { 14431da177e4SLinus Torvalds struct writeback_control wbc = { 14441da177e4SLinus Torvalds .sync_mode = WB_SYNC_NONE, 14451da177e4SLinus Torvalds }; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds swap_writepage(page, &wbc); 14481da177e4SLinus Torvalds lock_page(page); 14491da177e4SLinus Torvalds wait_on_page_writeback(page); 14501da177e4SLinus Torvalds } 145168bdc8d6SHugh Dickins 145268bdc8d6SHugh Dickins /* 145368bdc8d6SHugh Dickins * It is conceivable that a racing task removed this page from 145468bdc8d6SHugh Dickins * swap cache just before we acquired the page lock at the top, 145568bdc8d6SHugh Dickins * or while we dropped it in unuse_mm(). The page might even 145668bdc8d6SHugh Dickins * be back in swap cache on another swap area: that we must not 145768bdc8d6SHugh Dickins * delete, since it may not have been written out to swap yet. 145868bdc8d6SHugh Dickins */ 145968bdc8d6SHugh Dickins if (PageSwapCache(page) && 146068bdc8d6SHugh Dickins likely(page_private(page) == entry.val)) 14611da177e4SLinus Torvalds delete_from_swap_cache(page); 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * So we could skip searching mms once swap count went 14651da177e4SLinus Torvalds * to 1, we did not mark any present ptes as dirty: must 14662706a1b8SAnderson Briglia * mark page dirty so shrink_page_list will preserve it. 14671da177e4SLinus Torvalds */ 14681da177e4SLinus Torvalds SetPageDirty(page); 14691da177e4SLinus Torvalds unlock_page(page); 14701da177e4SLinus Torvalds page_cache_release(page); 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds /* 14731da177e4SLinus Torvalds * Make sure that we aren't completely killing 14741da177e4SLinus Torvalds * interactive performance. 14751da177e4SLinus Torvalds */ 14761da177e4SLinus Torvalds cond_resched(); 147738b5faf4SDan Magenheimer if (frontswap && pages_to_unuse > 0) { 147838b5faf4SDan Magenheimer if (!--pages_to_unuse) 147938b5faf4SDan Magenheimer break; 148038b5faf4SDan Magenheimer } 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds mmput(start_mm); 14841da177e4SLinus Torvalds return retval; 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds /* 14885d337b91SHugh Dickins * After a successful try_to_unuse, if no swap is now in use, we know 14895d337b91SHugh Dickins * we can empty the mmlist. swap_lock must be held on entry and exit. 14905d337b91SHugh Dickins * Note that mmlist_lock nests inside swap_lock, and an mm must be 14911da177e4SLinus Torvalds * added to the mmlist just after page_duplicate - before would be racy. 14921da177e4SLinus Torvalds */ 14931da177e4SLinus Torvalds static void drain_mmlist(void) 14941da177e4SLinus Torvalds { 14951da177e4SLinus Torvalds struct list_head *p, *next; 1496efa90a98SHugh Dickins unsigned int type; 14971da177e4SLinus Torvalds 1498efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) 1499efa90a98SHugh Dickins if (swap_info[type]->inuse_pages) 15001da177e4SLinus Torvalds return; 15011da177e4SLinus Torvalds spin_lock(&mmlist_lock); 15021da177e4SLinus Torvalds list_for_each_safe(p, next, &init_mm.mmlist) 15031da177e4SLinus Torvalds list_del_init(p); 15041da177e4SLinus Torvalds spin_unlock(&mmlist_lock); 15051da177e4SLinus Torvalds } 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * Use this swapdev's extent info to locate the (PAGE_SIZE) block which 1509d4906e1aSLee Schermerhorn * corresponds to page offset for the specified swap entry. 1510d4906e1aSLee Schermerhorn * Note that the type of this function is sector_t, but it returns page offset 1511d4906e1aSLee Schermerhorn * into the bdev, not sector offset. 15121da177e4SLinus Torvalds */ 1513d4906e1aSLee Schermerhorn static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev) 15141da177e4SLinus Torvalds { 1515f29ad6a9SHugh Dickins struct swap_info_struct *sis; 1516f29ad6a9SHugh Dickins struct swap_extent *start_se; 1517f29ad6a9SHugh Dickins struct swap_extent *se; 1518f29ad6a9SHugh Dickins pgoff_t offset; 1519f29ad6a9SHugh Dickins 1520efa90a98SHugh Dickins sis = swap_info[swp_type(entry)]; 1521f29ad6a9SHugh Dickins *bdev = sis->bdev; 1522f29ad6a9SHugh Dickins 1523f29ad6a9SHugh Dickins offset = swp_offset(entry); 1524f29ad6a9SHugh Dickins start_se = sis->curr_swap_extent; 1525f29ad6a9SHugh Dickins se = start_se; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds for ( ; ; ) { 15281da177e4SLinus Torvalds struct list_head *lh; 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds if (se->start_page <= offset && 15311da177e4SLinus Torvalds offset < (se->start_page + se->nr_pages)) { 15321da177e4SLinus Torvalds return se->start_block + (offset - se->start_page); 15331da177e4SLinus Torvalds } 153411d31886SHugh Dickins lh = se->list.next; 15351da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 15361da177e4SLinus Torvalds sis->curr_swap_extent = se; 15371da177e4SLinus Torvalds BUG_ON(se == start_se); /* It *must* be present */ 15381da177e4SLinus Torvalds } 15391da177e4SLinus Torvalds } 15401da177e4SLinus Torvalds 15411da177e4SLinus Torvalds /* 1542d4906e1aSLee Schermerhorn * Returns the page offset into bdev for the specified page's swap entry. 1543d4906e1aSLee Schermerhorn */ 1544d4906e1aSLee Schermerhorn sector_t map_swap_page(struct page *page, struct block_device **bdev) 1545d4906e1aSLee Schermerhorn { 1546d4906e1aSLee Schermerhorn swp_entry_t entry; 1547d4906e1aSLee Schermerhorn entry.val = page_private(page); 1548d4906e1aSLee Schermerhorn return map_swap_entry(entry, bdev); 1549d4906e1aSLee Schermerhorn } 1550d4906e1aSLee Schermerhorn 1551d4906e1aSLee Schermerhorn /* 15521da177e4SLinus Torvalds * Free all of a swapdev's extent information 15531da177e4SLinus Torvalds */ 15541da177e4SLinus Torvalds static void destroy_swap_extents(struct swap_info_struct *sis) 15551da177e4SLinus Torvalds { 15569625a5f2SHugh Dickins while (!list_empty(&sis->first_swap_extent.list)) { 15571da177e4SLinus Torvalds struct swap_extent *se; 15581da177e4SLinus Torvalds 15599625a5f2SHugh Dickins se = list_entry(sis->first_swap_extent.list.next, 15601da177e4SLinus Torvalds struct swap_extent, list); 15611da177e4SLinus Torvalds list_del(&se->list); 15621da177e4SLinus Torvalds kfree(se); 15631da177e4SLinus Torvalds } 156462c230bcSMel Gorman 156562c230bcSMel Gorman if (sis->flags & SWP_FILE) { 156662c230bcSMel Gorman struct file *swap_file = sis->swap_file; 156762c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 156862c230bcSMel Gorman 156962c230bcSMel Gorman sis->flags &= ~SWP_FILE; 157062c230bcSMel Gorman mapping->a_ops->swap_deactivate(swap_file); 157162c230bcSMel Gorman } 15721da177e4SLinus Torvalds } 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds /* 15751da177e4SLinus Torvalds * Add a block range (and the corresponding page range) into this swapdev's 157611d31886SHugh Dickins * extent list. The extent list is kept sorted in page order. 15771da177e4SLinus Torvalds * 157811d31886SHugh Dickins * This function rather assumes that it is called in ascending page order. 15791da177e4SLinus Torvalds */ 1580a509bc1aSMel Gorman int 15811da177e4SLinus Torvalds add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, 15821da177e4SLinus Torvalds unsigned long nr_pages, sector_t start_block) 15831da177e4SLinus Torvalds { 15841da177e4SLinus Torvalds struct swap_extent *se; 15851da177e4SLinus Torvalds struct swap_extent *new_se; 15861da177e4SLinus Torvalds struct list_head *lh; 15871da177e4SLinus Torvalds 15889625a5f2SHugh Dickins if (start_page == 0) { 15899625a5f2SHugh Dickins se = &sis->first_swap_extent; 15909625a5f2SHugh Dickins sis->curr_swap_extent = se; 15919625a5f2SHugh Dickins se->start_page = 0; 15929625a5f2SHugh Dickins se->nr_pages = nr_pages; 15939625a5f2SHugh Dickins se->start_block = start_block; 15949625a5f2SHugh Dickins return 1; 15959625a5f2SHugh Dickins } else { 15969625a5f2SHugh Dickins lh = sis->first_swap_extent.list.prev; /* Highest extent */ 15971da177e4SLinus Torvalds se = list_entry(lh, struct swap_extent, list); 159811d31886SHugh Dickins BUG_ON(se->start_page + se->nr_pages != start_page); 159911d31886SHugh Dickins if (se->start_block + se->nr_pages == start_block) { 16001da177e4SLinus Torvalds /* Merge it */ 16011da177e4SLinus Torvalds se->nr_pages += nr_pages; 16021da177e4SLinus Torvalds return 0; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds /* 16071da177e4SLinus Torvalds * No merge. Insert a new extent, preserving ordering. 16081da177e4SLinus Torvalds */ 16091da177e4SLinus Torvalds new_se = kmalloc(sizeof(*se), GFP_KERNEL); 16101da177e4SLinus Torvalds if (new_se == NULL) 16111da177e4SLinus Torvalds return -ENOMEM; 16121da177e4SLinus Torvalds new_se->start_page = start_page; 16131da177e4SLinus Torvalds new_se->nr_pages = nr_pages; 16141da177e4SLinus Torvalds new_se->start_block = start_block; 16151da177e4SLinus Torvalds 16169625a5f2SHugh Dickins list_add_tail(&new_se->list, &sis->first_swap_extent.list); 161753092a74SHugh Dickins return 1; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16201da177e4SLinus Torvalds /* 16211da177e4SLinus Torvalds * A `swap extent' is a simple thing which maps a contiguous range of pages 16221da177e4SLinus Torvalds * onto a contiguous range of disk blocks. An ordered list of swap extents 16231da177e4SLinus Torvalds * is built at swapon time and is then used at swap_writepage/swap_readpage 16241da177e4SLinus Torvalds * time for locating where on disk a page belongs. 16251da177e4SLinus Torvalds * 16261da177e4SLinus Torvalds * If the swapfile is an S_ISBLK block device, a single extent is installed. 16271da177e4SLinus Torvalds * This is done so that the main operating code can treat S_ISBLK and S_ISREG 16281da177e4SLinus Torvalds * swap files identically. 16291da177e4SLinus Torvalds * 16301da177e4SLinus Torvalds * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap 16311da177e4SLinus Torvalds * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK 16321da177e4SLinus Torvalds * swapfiles are handled *identically* after swapon time. 16331da177e4SLinus Torvalds * 16341da177e4SLinus Torvalds * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks 16351da177e4SLinus Torvalds * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If 16361da177e4SLinus Torvalds * some stray blocks are found which do not fall within the PAGE_SIZE alignment 16371da177e4SLinus Torvalds * requirements, they are simply tossed out - we will never use those blocks 16381da177e4SLinus Torvalds * for swapping. 16391da177e4SLinus Torvalds * 1640b0d9bcd4SHugh Dickins * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This 16411da177e4SLinus Torvalds * prevents root from shooting her foot off by ftruncating an in-use swapfile, 16421da177e4SLinus Torvalds * which will scribble on the fs. 16431da177e4SLinus Torvalds * 16441da177e4SLinus Torvalds * The amount of disk space which a single swap extent represents varies. 16451da177e4SLinus Torvalds * Typically it is in the 1-4 megabyte range. So we can have hundreds of 16461da177e4SLinus Torvalds * extents in the list. To avoid much list walking, we cache the previous 16471da177e4SLinus Torvalds * search location in `curr_swap_extent', and start new searches from there. 16481da177e4SLinus Torvalds * This is extremely effective. The average number of iterations in 16491da177e4SLinus Torvalds * map_swap_page() has been measured at about 0.3 per page. - akpm. 16501da177e4SLinus Torvalds */ 165153092a74SHugh Dickins static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) 16521da177e4SLinus Torvalds { 165362c230bcSMel Gorman struct file *swap_file = sis->swap_file; 165462c230bcSMel Gorman struct address_space *mapping = swap_file->f_mapping; 165562c230bcSMel Gorman struct inode *inode = mapping->host; 16561da177e4SLinus Torvalds int ret; 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 16591da177e4SLinus Torvalds ret = add_swap_extent(sis, 0, sis->max, 0); 166053092a74SHugh Dickins *span = sis->pages; 1661a509bc1aSMel Gorman return ret; 16621da177e4SLinus Torvalds } 16631da177e4SLinus Torvalds 166462c230bcSMel Gorman if (mapping->a_ops->swap_activate) { 1665a509bc1aSMel Gorman ret = mapping->a_ops->swap_activate(sis, swap_file, span); 166662c230bcSMel Gorman if (!ret) { 166762c230bcSMel Gorman sis->flags |= SWP_FILE; 166862c230bcSMel Gorman ret = add_swap_extent(sis, 0, sis->max, 0); 166962c230bcSMel Gorman *span = sis->pages; 167062c230bcSMel Gorman } 16719625a5f2SHugh Dickins return ret; 1672a509bc1aSMel Gorman } 1673a509bc1aSMel Gorman 1674a509bc1aSMel Gorman return generic_swapfile_activate(sis, swap_file, span); 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 1677cf0cac0aSCesar Eduardo Barros static void _enable_swap_info(struct swap_info_struct *p, int prio, 1678*2a8f9449SShaohua Li unsigned char *swap_map, 1679*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info) 168040531542SCesar Eduardo Barros { 168140531542SCesar Eduardo Barros int i, prev; 168240531542SCesar Eduardo Barros 168340531542SCesar Eduardo Barros if (prio >= 0) 168440531542SCesar Eduardo Barros p->prio = prio; 168540531542SCesar Eduardo Barros else 168640531542SCesar Eduardo Barros p->prio = --least_priority; 168740531542SCesar Eduardo Barros p->swap_map = swap_map; 1688*2a8f9449SShaohua Li p->cluster_info = cluster_info; 168940531542SCesar Eduardo Barros p->flags |= SWP_WRITEOK; 1690ec8acf20SShaohua Li atomic_long_add(p->pages, &nr_swap_pages); 169140531542SCesar Eduardo Barros total_swap_pages += p->pages; 169240531542SCesar Eduardo Barros 169340531542SCesar Eduardo Barros /* insert swap space into swap_list: */ 169440531542SCesar Eduardo Barros prev = -1; 169540531542SCesar Eduardo Barros for (i = swap_list.head; i >= 0; i = swap_info[i]->next) { 169640531542SCesar Eduardo Barros if (p->prio >= swap_info[i]->prio) 169740531542SCesar Eduardo Barros break; 169840531542SCesar Eduardo Barros prev = i; 169940531542SCesar Eduardo Barros } 170040531542SCesar Eduardo Barros p->next = i; 170140531542SCesar Eduardo Barros if (prev < 0) 170240531542SCesar Eduardo Barros swap_list.head = swap_list.next = p->type; 170340531542SCesar Eduardo Barros else 170440531542SCesar Eduardo Barros swap_info[prev]->next = p->type; 1705cf0cac0aSCesar Eduardo Barros } 1706cf0cac0aSCesar Eduardo Barros 1707cf0cac0aSCesar Eduardo Barros static void enable_swap_info(struct swap_info_struct *p, int prio, 1708cf0cac0aSCesar Eduardo Barros unsigned char *swap_map, 1709*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 1710cf0cac0aSCesar Eduardo Barros unsigned long *frontswap_map) 1711cf0cac0aSCesar Eduardo Barros { 17124f89849dSMinchan Kim frontswap_init(p->type, frontswap_map); 1713cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1714ec8acf20SShaohua Li spin_lock(&p->lock); 1715*2a8f9449SShaohua Li _enable_swap_info(p, prio, swap_map, cluster_info); 1716ec8acf20SShaohua Li spin_unlock(&p->lock); 1717cf0cac0aSCesar Eduardo Barros spin_unlock(&swap_lock); 1718cf0cac0aSCesar Eduardo Barros } 1719cf0cac0aSCesar Eduardo Barros 1720cf0cac0aSCesar Eduardo Barros static void reinsert_swap_info(struct swap_info_struct *p) 1721cf0cac0aSCesar Eduardo Barros { 1722cf0cac0aSCesar Eduardo Barros spin_lock(&swap_lock); 1723ec8acf20SShaohua Li spin_lock(&p->lock); 1724*2a8f9449SShaohua Li _enable_swap_info(p, p->prio, p->swap_map, p->cluster_info); 1725ec8acf20SShaohua Li spin_unlock(&p->lock); 172640531542SCesar Eduardo Barros spin_unlock(&swap_lock); 172740531542SCesar Eduardo Barros } 172840531542SCesar Eduardo Barros 1729c4ea37c2SHeiko Carstens SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) 17301da177e4SLinus Torvalds { 17311da177e4SLinus Torvalds struct swap_info_struct *p = NULL; 17328d69aaeeSHugh Dickins unsigned char *swap_map; 1733*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info; 17344f89849dSMinchan Kim unsigned long *frontswap_map; 17351da177e4SLinus Torvalds struct file *swap_file, *victim; 17361da177e4SLinus Torvalds struct address_space *mapping; 17371da177e4SLinus Torvalds struct inode *inode; 173891a27b2aSJeff Layton struct filename *pathname; 17391da177e4SLinus Torvalds int i, type, prev; 17401da177e4SLinus Torvalds int err; 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds if (!capable(CAP_SYS_ADMIN)) 17431da177e4SLinus Torvalds return -EPERM; 17441da177e4SLinus Torvalds 1745191c5424SAl Viro BUG_ON(!current->mm); 1746191c5424SAl Viro 17471da177e4SLinus Torvalds pathname = getname(specialfile); 17481da177e4SLinus Torvalds if (IS_ERR(pathname)) 1749f58b59c1SXiaotian Feng return PTR_ERR(pathname); 17501da177e4SLinus Torvalds 1751669abf4eSJeff Layton victim = file_open_name(pathname, O_RDWR|O_LARGEFILE, 0); 17521da177e4SLinus Torvalds err = PTR_ERR(victim); 17531da177e4SLinus Torvalds if (IS_ERR(victim)) 17541da177e4SLinus Torvalds goto out; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds mapping = victim->f_mapping; 17571da177e4SLinus Torvalds prev = -1; 17585d337b91SHugh Dickins spin_lock(&swap_lock); 1759efa90a98SHugh Dickins for (type = swap_list.head; type >= 0; type = swap_info[type]->next) { 1760efa90a98SHugh Dickins p = swap_info[type]; 176122c6f8fdSHugh Dickins if (p->flags & SWP_WRITEOK) { 17621da177e4SLinus Torvalds if (p->swap_file->f_mapping == mapping) 17631da177e4SLinus Torvalds break; 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds prev = type; 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds if (type < 0) { 17681da177e4SLinus Torvalds err = -EINVAL; 17695d337b91SHugh Dickins spin_unlock(&swap_lock); 17701da177e4SLinus Torvalds goto out_dput; 17711da177e4SLinus Torvalds } 1772191c5424SAl Viro if (!security_vm_enough_memory_mm(current->mm, p->pages)) 17731da177e4SLinus Torvalds vm_unacct_memory(p->pages); 17741da177e4SLinus Torvalds else { 17751da177e4SLinus Torvalds err = -ENOMEM; 17765d337b91SHugh Dickins spin_unlock(&swap_lock); 17771da177e4SLinus Torvalds goto out_dput; 17781da177e4SLinus Torvalds } 1779efa90a98SHugh Dickins if (prev < 0) 17801da177e4SLinus Torvalds swap_list.head = p->next; 1781efa90a98SHugh Dickins else 1782efa90a98SHugh Dickins swap_info[prev]->next = p->next; 17831da177e4SLinus Torvalds if (type == swap_list.next) { 17841da177e4SLinus Torvalds /* just pick something that's safe... */ 17851da177e4SLinus Torvalds swap_list.next = swap_list.head; 17861da177e4SLinus Torvalds } 1787ec8acf20SShaohua Li spin_lock(&p->lock); 178878ecba08SHugh Dickins if (p->prio < 0) { 1789efa90a98SHugh Dickins for (i = p->next; i >= 0; i = swap_info[i]->next) 1790efa90a98SHugh Dickins swap_info[i]->prio = p->prio--; 179178ecba08SHugh Dickins least_priority++; 179278ecba08SHugh Dickins } 1793ec8acf20SShaohua Li atomic_long_sub(p->pages, &nr_swap_pages); 17941da177e4SLinus Torvalds total_swap_pages -= p->pages; 17951da177e4SLinus Torvalds p->flags &= ~SWP_WRITEOK; 1796ec8acf20SShaohua Li spin_unlock(&p->lock); 17975d337b91SHugh Dickins spin_unlock(&swap_lock); 1798fb4f88dcSHugh Dickins 1799e1e12d2fSDavid Rientjes set_current_oom_origin(); 180038b5faf4SDan Magenheimer err = try_to_unuse(type, false, 0); /* force all pages to be unused */ 1801e1e12d2fSDavid Rientjes clear_current_oom_origin(); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds if (err) { 18041da177e4SLinus Torvalds /* re-insert swap space back into swap_list */ 1805cf0cac0aSCesar Eduardo Barros reinsert_swap_info(p); 18061da177e4SLinus Torvalds goto out_dput; 18071da177e4SLinus Torvalds } 180852b7efdbSHugh Dickins 18094cd3bb10SHugh Dickins destroy_swap_extents(p); 1810570a335bSHugh Dickins if (p->flags & SWP_CONTINUED) 1811570a335bSHugh Dickins free_swap_count_continuations(p); 1812570a335bSHugh Dickins 1813fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 18145d337b91SHugh Dickins spin_lock(&swap_lock); 1815ec8acf20SShaohua Li spin_lock(&p->lock); 18161da177e4SLinus Torvalds drain_mmlist(); 18175d337b91SHugh Dickins 18185d337b91SHugh Dickins /* wait for anyone still in scan_swap_map */ 18195d337b91SHugh Dickins p->highest_bit = 0; /* cuts scans short */ 18205d337b91SHugh Dickins while (p->flags >= SWP_SCANNING) { 1821ec8acf20SShaohua Li spin_unlock(&p->lock); 18225d337b91SHugh Dickins spin_unlock(&swap_lock); 182313e4b57fSNishanth Aravamudan schedule_timeout_uninterruptible(1); 18245d337b91SHugh Dickins spin_lock(&swap_lock); 1825ec8acf20SShaohua Li spin_lock(&p->lock); 18265d337b91SHugh Dickins } 18275d337b91SHugh Dickins 18281da177e4SLinus Torvalds swap_file = p->swap_file; 18291da177e4SLinus Torvalds p->swap_file = NULL; 18301da177e4SLinus Torvalds p->max = 0; 18311da177e4SLinus Torvalds swap_map = p->swap_map; 18321da177e4SLinus Torvalds p->swap_map = NULL; 1833*2a8f9449SShaohua Li cluster_info = p->cluster_info; 1834*2a8f9449SShaohua Li p->cluster_info = NULL; 18351da177e4SLinus Torvalds p->flags = 0; 18364f89849dSMinchan Kim frontswap_map = frontswap_map_get(p); 18374f89849dSMinchan Kim frontswap_map_set(p, NULL); 1838ec8acf20SShaohua Li spin_unlock(&p->lock); 18395d337b91SHugh Dickins spin_unlock(&swap_lock); 18404f89849dSMinchan Kim frontswap_invalidate_area(type); 1841fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 18421da177e4SLinus Torvalds vfree(swap_map); 1843*2a8f9449SShaohua Li vfree(cluster_info); 18444f89849dSMinchan Kim vfree(frontswap_map); 184527a7faa0SKAMEZAWA Hiroyuki /* Destroy swap account informatin */ 184627a7faa0SKAMEZAWA Hiroyuki swap_cgroup_swapoff(type); 184727a7faa0SKAMEZAWA Hiroyuki 18481da177e4SLinus Torvalds inode = mapping->host; 18491da177e4SLinus Torvalds if (S_ISBLK(inode->i_mode)) { 18501da177e4SLinus Torvalds struct block_device *bdev = I_BDEV(inode); 18511da177e4SLinus Torvalds set_blocksize(bdev, p->old_block_size); 1852e525fd89STejun Heo blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 18531da177e4SLinus Torvalds } else { 18541b1dcc1bSJes Sorensen mutex_lock(&inode->i_mutex); 18551da177e4SLinus Torvalds inode->i_flags &= ~S_SWAPFILE; 18561b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds filp_close(swap_file, NULL); 18591da177e4SLinus Torvalds err = 0; 186066d7dd51SKay Sievers atomic_inc(&proc_poll_event); 186166d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds out_dput: 18641da177e4SLinus Torvalds filp_close(victim, NULL); 18651da177e4SLinus Torvalds out: 1866f58b59c1SXiaotian Feng putname(pathname); 18671da177e4SLinus Torvalds return err; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds #ifdef CONFIG_PROC_FS 187166d7dd51SKay Sievers static unsigned swaps_poll(struct file *file, poll_table *wait) 187266d7dd51SKay Sievers { 1873f1514638SKay Sievers struct seq_file *seq = file->private_data; 187466d7dd51SKay Sievers 187566d7dd51SKay Sievers poll_wait(file, &proc_poll_wait, wait); 187666d7dd51SKay Sievers 1877f1514638SKay Sievers if (seq->poll_event != atomic_read(&proc_poll_event)) { 1878f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 187966d7dd51SKay Sievers return POLLIN | POLLRDNORM | POLLERR | POLLPRI; 188066d7dd51SKay Sievers } 188166d7dd51SKay Sievers 188266d7dd51SKay Sievers return POLLIN | POLLRDNORM; 188366d7dd51SKay Sievers } 188466d7dd51SKay Sievers 18851da177e4SLinus Torvalds /* iterator */ 18861da177e4SLinus Torvalds static void *swap_start(struct seq_file *swap, loff_t *pos) 18871da177e4SLinus Torvalds { 1888efa90a98SHugh Dickins struct swap_info_struct *si; 1889efa90a98SHugh Dickins int type; 18901da177e4SLinus Torvalds loff_t l = *pos; 18911da177e4SLinus Torvalds 1892fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 18931da177e4SLinus Torvalds 1894881e4aabSSuleiman Souhlal if (!l) 1895881e4aabSSuleiman Souhlal return SEQ_START_TOKEN; 1896881e4aabSSuleiman Souhlal 1897efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 1898efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 1899efa90a98SHugh Dickins si = swap_info[type]; 1900efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 19011da177e4SLinus Torvalds continue; 1902881e4aabSSuleiman Souhlal if (!--l) 1903efa90a98SHugh Dickins return si; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds return NULL; 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 19091da177e4SLinus Torvalds static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) 19101da177e4SLinus Torvalds { 1911efa90a98SHugh Dickins struct swap_info_struct *si = v; 1912efa90a98SHugh Dickins int type; 19131da177e4SLinus Torvalds 1914881e4aabSSuleiman Souhlal if (v == SEQ_START_TOKEN) 1915efa90a98SHugh Dickins type = 0; 1916efa90a98SHugh Dickins else 1917efa90a98SHugh Dickins type = si->type + 1; 1918881e4aabSSuleiman Souhlal 1919efa90a98SHugh Dickins for (; type < nr_swapfiles; type++) { 1920efa90a98SHugh Dickins smp_rmb(); /* read nr_swapfiles before swap_info[type] */ 1921efa90a98SHugh Dickins si = swap_info[type]; 1922efa90a98SHugh Dickins if (!(si->flags & SWP_USED) || !si->swap_map) 19231da177e4SLinus Torvalds continue; 19241da177e4SLinus Torvalds ++*pos; 1925efa90a98SHugh Dickins return si; 19261da177e4SLinus Torvalds } 19271da177e4SLinus Torvalds 19281da177e4SLinus Torvalds return NULL; 19291da177e4SLinus Torvalds } 19301da177e4SLinus Torvalds 19311da177e4SLinus Torvalds static void swap_stop(struct seq_file *swap, void *v) 19321da177e4SLinus Torvalds { 1933fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds 19361da177e4SLinus Torvalds static int swap_show(struct seq_file *swap, void *v) 19371da177e4SLinus Torvalds { 1938efa90a98SHugh Dickins struct swap_info_struct *si = v; 19391da177e4SLinus Torvalds struct file *file; 19401da177e4SLinus Torvalds int len; 19411da177e4SLinus Torvalds 1942efa90a98SHugh Dickins if (si == SEQ_START_TOKEN) { 19431da177e4SLinus Torvalds seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); 1944881e4aabSSuleiman Souhlal return 0; 1945881e4aabSSuleiman Souhlal } 19461da177e4SLinus Torvalds 1947efa90a98SHugh Dickins file = si->swap_file; 1948c32c2f63SJan Blunck len = seq_path(swap, &file->f_path, " \t\n\\"); 19496eb396dcSHugh Dickins seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", 19501da177e4SLinus Torvalds len < 40 ? 40 - len : 1, " ", 1951496ad9aaSAl Viro S_ISBLK(file_inode(file)->i_mode) ? 19521da177e4SLinus Torvalds "partition" : "file\t", 1953efa90a98SHugh Dickins si->pages << (PAGE_SHIFT - 10), 1954efa90a98SHugh Dickins si->inuse_pages << (PAGE_SHIFT - 10), 1955efa90a98SHugh Dickins si->prio); 19561da177e4SLinus Torvalds return 0; 19571da177e4SLinus Torvalds } 19581da177e4SLinus Torvalds 195915ad7cdcSHelge Deller static const struct seq_operations swaps_op = { 19601da177e4SLinus Torvalds .start = swap_start, 19611da177e4SLinus Torvalds .next = swap_next, 19621da177e4SLinus Torvalds .stop = swap_stop, 19631da177e4SLinus Torvalds .show = swap_show 19641da177e4SLinus Torvalds }; 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds static int swaps_open(struct inode *inode, struct file *file) 19671da177e4SLinus Torvalds { 1968f1514638SKay Sievers struct seq_file *seq; 196966d7dd51SKay Sievers int ret; 197066d7dd51SKay Sievers 197166d7dd51SKay Sievers ret = seq_open(file, &swaps_op); 1972f1514638SKay Sievers if (ret) 197366d7dd51SKay Sievers return ret; 197466d7dd51SKay Sievers 1975f1514638SKay Sievers seq = file->private_data; 1976f1514638SKay Sievers seq->poll_event = atomic_read(&proc_poll_event); 1977f1514638SKay Sievers return 0; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds 198015ad7cdcSHelge Deller static const struct file_operations proc_swaps_operations = { 19811da177e4SLinus Torvalds .open = swaps_open, 19821da177e4SLinus Torvalds .read = seq_read, 19831da177e4SLinus Torvalds .llseek = seq_lseek, 19841da177e4SLinus Torvalds .release = seq_release, 198566d7dd51SKay Sievers .poll = swaps_poll, 19861da177e4SLinus Torvalds }; 19871da177e4SLinus Torvalds 19881da177e4SLinus Torvalds static int __init procswaps_init(void) 19891da177e4SLinus Torvalds { 19903d71f86fSDenis V. Lunev proc_create("swaps", 0, NULL, &proc_swaps_operations); 19911da177e4SLinus Torvalds return 0; 19921da177e4SLinus Torvalds } 19931da177e4SLinus Torvalds __initcall(procswaps_init); 19941da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */ 19951da177e4SLinus Torvalds 19961796316aSJan Beulich #ifdef MAX_SWAPFILES_CHECK 19971796316aSJan Beulich static int __init max_swapfiles_check(void) 19981796316aSJan Beulich { 19991796316aSJan Beulich MAX_SWAPFILES_CHECK(); 20001796316aSJan Beulich return 0; 20011796316aSJan Beulich } 20021796316aSJan Beulich late_initcall(max_swapfiles_check); 20031796316aSJan Beulich #endif 20041796316aSJan Beulich 200553cbb243SCesar Eduardo Barros static struct swap_info_struct *alloc_swap_info(void) 20061da177e4SLinus Torvalds { 20071da177e4SLinus Torvalds struct swap_info_struct *p; 20081da177e4SLinus Torvalds unsigned int type; 2009efa90a98SHugh Dickins 2010efa90a98SHugh Dickins p = kzalloc(sizeof(*p), GFP_KERNEL); 2011efa90a98SHugh Dickins if (!p) 201253cbb243SCesar Eduardo Barros return ERR_PTR(-ENOMEM); 2013efa90a98SHugh Dickins 20145d337b91SHugh Dickins spin_lock(&swap_lock); 2015efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2016efa90a98SHugh Dickins if (!(swap_info[type]->flags & SWP_USED)) 20171da177e4SLinus Torvalds break; 2018efa90a98SHugh Dickins } 20190697212aSChristoph Lameter if (type >= MAX_SWAPFILES) { 20205d337b91SHugh Dickins spin_unlock(&swap_lock); 2021efa90a98SHugh Dickins kfree(p); 2022730c0581SCesar Eduardo Barros return ERR_PTR(-EPERM); 20231da177e4SLinus Torvalds } 2024efa90a98SHugh Dickins if (type >= nr_swapfiles) { 2025efa90a98SHugh Dickins p->type = type; 2026efa90a98SHugh Dickins swap_info[type] = p; 2027efa90a98SHugh Dickins /* 2028efa90a98SHugh Dickins * Write swap_info[type] before nr_swapfiles, in case a 2029efa90a98SHugh Dickins * racing procfs swap_start() or swap_next() is reading them. 2030efa90a98SHugh Dickins * (We never shrink nr_swapfiles, we never free this entry.) 2031efa90a98SHugh Dickins */ 2032efa90a98SHugh Dickins smp_wmb(); 2033efa90a98SHugh Dickins nr_swapfiles++; 2034efa90a98SHugh Dickins } else { 2035efa90a98SHugh Dickins kfree(p); 2036efa90a98SHugh Dickins p = swap_info[type]; 2037efa90a98SHugh Dickins /* 2038efa90a98SHugh Dickins * Do not memset this entry: a racing procfs swap_next() 2039efa90a98SHugh Dickins * would be relying on p->type to remain valid. 2040efa90a98SHugh Dickins */ 2041efa90a98SHugh Dickins } 20429625a5f2SHugh Dickins INIT_LIST_HEAD(&p->first_swap_extent.list); 20431da177e4SLinus Torvalds p->flags = SWP_USED; 20441da177e4SLinus Torvalds p->next = -1; 20455d337b91SHugh Dickins spin_unlock(&swap_lock); 2046ec8acf20SShaohua Li spin_lock_init(&p->lock); 2047efa90a98SHugh Dickins 204853cbb243SCesar Eduardo Barros return p; 204953cbb243SCesar Eduardo Barros } 205053cbb243SCesar Eduardo Barros 20514d0e1e10SCesar Eduardo Barros static int claim_swapfile(struct swap_info_struct *p, struct inode *inode) 20524d0e1e10SCesar Eduardo Barros { 20534d0e1e10SCesar Eduardo Barros int error; 20544d0e1e10SCesar Eduardo Barros 20554d0e1e10SCesar Eduardo Barros if (S_ISBLK(inode->i_mode)) { 20564d0e1e10SCesar Eduardo Barros p->bdev = bdgrab(I_BDEV(inode)); 20574d0e1e10SCesar Eduardo Barros error = blkdev_get(p->bdev, 20584d0e1e10SCesar Eduardo Barros FMODE_READ | FMODE_WRITE | FMODE_EXCL, 20594d0e1e10SCesar Eduardo Barros sys_swapon); 20604d0e1e10SCesar Eduardo Barros if (error < 0) { 20614d0e1e10SCesar Eduardo Barros p->bdev = NULL; 206287ade72aSCesar Eduardo Barros return -EINVAL; 20634d0e1e10SCesar Eduardo Barros } 20644d0e1e10SCesar Eduardo Barros p->old_block_size = block_size(p->bdev); 20654d0e1e10SCesar Eduardo Barros error = set_blocksize(p->bdev, PAGE_SIZE); 20664d0e1e10SCesar Eduardo Barros if (error < 0) 206787ade72aSCesar Eduardo Barros return error; 20684d0e1e10SCesar Eduardo Barros p->flags |= SWP_BLKDEV; 20694d0e1e10SCesar Eduardo Barros } else if (S_ISREG(inode->i_mode)) { 20704d0e1e10SCesar Eduardo Barros p->bdev = inode->i_sb->s_bdev; 20714d0e1e10SCesar Eduardo Barros mutex_lock(&inode->i_mutex); 207287ade72aSCesar Eduardo Barros if (IS_SWAPFILE(inode)) 207387ade72aSCesar Eduardo Barros return -EBUSY; 207487ade72aSCesar Eduardo Barros } else 207587ade72aSCesar Eduardo Barros return -EINVAL; 20764d0e1e10SCesar Eduardo Barros 20774d0e1e10SCesar Eduardo Barros return 0; 20784d0e1e10SCesar Eduardo Barros } 20794d0e1e10SCesar Eduardo Barros 2080ca8bd38bSCesar Eduardo Barros static unsigned long read_swap_header(struct swap_info_struct *p, 2081ca8bd38bSCesar Eduardo Barros union swap_header *swap_header, 2082ca8bd38bSCesar Eduardo Barros struct inode *inode) 2083ca8bd38bSCesar Eduardo Barros { 2084ca8bd38bSCesar Eduardo Barros int i; 2085ca8bd38bSCesar Eduardo Barros unsigned long maxpages; 2086ca8bd38bSCesar Eduardo Barros unsigned long swapfilepages; 2087d6bbbd29SRaymond Jennings unsigned long last_page; 2088ca8bd38bSCesar Eduardo Barros 2089ca8bd38bSCesar Eduardo Barros if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { 2090465c47fdSAndrew Morton pr_err("Unable to find swap-space signature\n"); 209138719025SCesar Eduardo Barros return 0; 2092ca8bd38bSCesar Eduardo Barros } 2093ca8bd38bSCesar Eduardo Barros 2094ca8bd38bSCesar Eduardo Barros /* swap partition endianess hack... */ 2095ca8bd38bSCesar Eduardo Barros if (swab32(swap_header->info.version) == 1) { 2096ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.version); 2097ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.last_page); 2098ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.nr_badpages); 2099ca8bd38bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) 2100ca8bd38bSCesar Eduardo Barros swab32s(&swap_header->info.badpages[i]); 2101ca8bd38bSCesar Eduardo Barros } 2102ca8bd38bSCesar Eduardo Barros /* Check the swap header's sub-version */ 2103ca8bd38bSCesar Eduardo Barros if (swap_header->info.version != 1) { 2104465c47fdSAndrew Morton pr_warn("Unable to handle swap header version %d\n", 2105ca8bd38bSCesar Eduardo Barros swap_header->info.version); 210638719025SCesar Eduardo Barros return 0; 2107ca8bd38bSCesar Eduardo Barros } 2108ca8bd38bSCesar Eduardo Barros 2109ca8bd38bSCesar Eduardo Barros p->lowest_bit = 1; 2110ca8bd38bSCesar Eduardo Barros p->cluster_next = 1; 2111ca8bd38bSCesar Eduardo Barros p->cluster_nr = 0; 2112ca8bd38bSCesar Eduardo Barros 2113ca8bd38bSCesar Eduardo Barros /* 2114ca8bd38bSCesar Eduardo Barros * Find out how many pages are allowed for a single swap 21159b15b817SHugh Dickins * device. There are two limiting factors: 1) the number 2116a2c16d6cSHugh Dickins * of bits for the swap offset in the swp_entry_t type, and 2117a2c16d6cSHugh Dickins * 2) the number of bits in the swap pte as defined by the 21189b15b817SHugh Dickins * different architectures. In order to find the 2119a2c16d6cSHugh Dickins * largest possible bit mask, a swap entry with swap type 0 2120ca8bd38bSCesar Eduardo Barros * and swap offset ~0UL is created, encoded to a swap pte, 2121a2c16d6cSHugh Dickins * decoded to a swp_entry_t again, and finally the swap 2122ca8bd38bSCesar Eduardo Barros * offset is extracted. This will mask all the bits from 2123ca8bd38bSCesar Eduardo Barros * the initial ~0UL mask that can't be encoded in either 2124ca8bd38bSCesar Eduardo Barros * the swp_entry_t or the architecture definition of a 21259b15b817SHugh Dickins * swap pte. 2126ca8bd38bSCesar Eduardo Barros */ 2127ca8bd38bSCesar Eduardo Barros maxpages = swp_offset(pte_to_swp_entry( 21289b15b817SHugh Dickins swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; 2129d6bbbd29SRaymond Jennings last_page = swap_header->info.last_page; 2130d6bbbd29SRaymond Jennings if (last_page > maxpages) { 2131465c47fdSAndrew Morton pr_warn("Truncating oversized swap area, only using %luk out of %luk\n", 2132d6bbbd29SRaymond Jennings maxpages << (PAGE_SHIFT - 10), 2133d6bbbd29SRaymond Jennings last_page << (PAGE_SHIFT - 10)); 2134d6bbbd29SRaymond Jennings } 2135d6bbbd29SRaymond Jennings if (maxpages > last_page) { 2136d6bbbd29SRaymond Jennings maxpages = last_page + 1; 2137ca8bd38bSCesar Eduardo Barros /* p->max is an unsigned int: don't overflow it */ 2138ca8bd38bSCesar Eduardo Barros if ((unsigned int)maxpages == 0) 2139ca8bd38bSCesar Eduardo Barros maxpages = UINT_MAX; 2140ca8bd38bSCesar Eduardo Barros } 2141ca8bd38bSCesar Eduardo Barros p->highest_bit = maxpages - 1; 2142ca8bd38bSCesar Eduardo Barros 2143ca8bd38bSCesar Eduardo Barros if (!maxpages) 214438719025SCesar Eduardo Barros return 0; 2145ca8bd38bSCesar Eduardo Barros swapfilepages = i_size_read(inode) >> PAGE_SHIFT; 2146ca8bd38bSCesar Eduardo Barros if (swapfilepages && maxpages > swapfilepages) { 2147465c47fdSAndrew Morton pr_warn("Swap area shorter than signature indicates\n"); 214838719025SCesar Eduardo Barros return 0; 2149ca8bd38bSCesar Eduardo Barros } 2150ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) 215138719025SCesar Eduardo Barros return 0; 2152ca8bd38bSCesar Eduardo Barros if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) 215338719025SCesar Eduardo Barros return 0; 2154ca8bd38bSCesar Eduardo Barros 2155ca8bd38bSCesar Eduardo Barros return maxpages; 2156ca8bd38bSCesar Eduardo Barros } 2157ca8bd38bSCesar Eduardo Barros 2158915d4d7bSCesar Eduardo Barros static int setup_swap_map_and_extents(struct swap_info_struct *p, 2159915d4d7bSCesar Eduardo Barros union swap_header *swap_header, 2160915d4d7bSCesar Eduardo Barros unsigned char *swap_map, 2161*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info, 2162915d4d7bSCesar Eduardo Barros unsigned long maxpages, 2163915d4d7bSCesar Eduardo Barros sector_t *span) 2164915d4d7bSCesar Eduardo Barros { 2165915d4d7bSCesar Eduardo Barros int i; 2166915d4d7bSCesar Eduardo Barros unsigned int nr_good_pages; 2167915d4d7bSCesar Eduardo Barros int nr_extents; 2168*2a8f9449SShaohua Li unsigned long nr_clusters = DIV_ROUND_UP(maxpages, SWAPFILE_CLUSTER); 2169*2a8f9449SShaohua Li unsigned long idx = p->cluster_next / SWAPFILE_CLUSTER; 2170915d4d7bSCesar Eduardo Barros 2171915d4d7bSCesar Eduardo Barros nr_good_pages = maxpages - 1; /* omit header page */ 2172915d4d7bSCesar Eduardo Barros 2173*2a8f9449SShaohua Li cluster_set_null(&p->free_cluster_head); 2174*2a8f9449SShaohua Li cluster_set_null(&p->free_cluster_tail); 2175*2a8f9449SShaohua Li 2176915d4d7bSCesar Eduardo Barros for (i = 0; i < swap_header->info.nr_badpages; i++) { 2177915d4d7bSCesar Eduardo Barros unsigned int page_nr = swap_header->info.badpages[i]; 2178bdb8e3f6SCesar Eduardo Barros if (page_nr == 0 || page_nr > swap_header->info.last_page) 2179bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2180915d4d7bSCesar Eduardo Barros if (page_nr < maxpages) { 2181915d4d7bSCesar Eduardo Barros swap_map[page_nr] = SWAP_MAP_BAD; 2182915d4d7bSCesar Eduardo Barros nr_good_pages--; 2183*2a8f9449SShaohua Li /* 2184*2a8f9449SShaohua Li * Haven't marked the cluster free yet, no list 2185*2a8f9449SShaohua Li * operation involved 2186*2a8f9449SShaohua Li */ 2187*2a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, page_nr); 2188915d4d7bSCesar Eduardo Barros } 2189915d4d7bSCesar Eduardo Barros } 2190915d4d7bSCesar Eduardo Barros 2191*2a8f9449SShaohua Li /* Haven't marked the cluster free yet, no list operation involved */ 2192*2a8f9449SShaohua Li for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) 2193*2a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, i); 2194*2a8f9449SShaohua Li 2195915d4d7bSCesar Eduardo Barros if (nr_good_pages) { 2196915d4d7bSCesar Eduardo Barros swap_map[0] = SWAP_MAP_BAD; 2197*2a8f9449SShaohua Li /* 2198*2a8f9449SShaohua Li * Not mark the cluster free yet, no list 2199*2a8f9449SShaohua Li * operation involved 2200*2a8f9449SShaohua Li */ 2201*2a8f9449SShaohua Li inc_cluster_info_page(p, cluster_info, 0); 2202915d4d7bSCesar Eduardo Barros p->max = maxpages; 2203915d4d7bSCesar Eduardo Barros p->pages = nr_good_pages; 2204915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_extents(p, span); 2205bdb8e3f6SCesar Eduardo Barros if (nr_extents < 0) 2206bdb8e3f6SCesar Eduardo Barros return nr_extents; 2207915d4d7bSCesar Eduardo Barros nr_good_pages = p->pages; 2208915d4d7bSCesar Eduardo Barros } 2209915d4d7bSCesar Eduardo Barros if (!nr_good_pages) { 2210465c47fdSAndrew Morton pr_warn("Empty swap-file\n"); 2211bdb8e3f6SCesar Eduardo Barros return -EINVAL; 2212915d4d7bSCesar Eduardo Barros } 2213915d4d7bSCesar Eduardo Barros 2214*2a8f9449SShaohua Li if (!cluster_info) 2215*2a8f9449SShaohua Li return nr_extents; 2216*2a8f9449SShaohua Li 2217*2a8f9449SShaohua Li for (i = 0; i < nr_clusters; i++) { 2218*2a8f9449SShaohua Li if (!cluster_count(&cluster_info[idx])) { 2219*2a8f9449SShaohua Li cluster_set_flag(&cluster_info[idx], CLUSTER_FLAG_FREE); 2220*2a8f9449SShaohua Li if (cluster_is_null(&p->free_cluster_head)) { 2221*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_head, 2222*2a8f9449SShaohua Li idx, 0); 2223*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, 2224*2a8f9449SShaohua Li idx, 0); 2225*2a8f9449SShaohua Li } else { 2226*2a8f9449SShaohua Li unsigned int tail; 2227*2a8f9449SShaohua Li 2228*2a8f9449SShaohua Li tail = cluster_next(&p->free_cluster_tail); 2229*2a8f9449SShaohua Li cluster_set_next(&cluster_info[tail], idx); 2230*2a8f9449SShaohua Li cluster_set_next_flag(&p->free_cluster_tail, 2231*2a8f9449SShaohua Li idx, 0); 2232*2a8f9449SShaohua Li } 2233*2a8f9449SShaohua Li } 2234*2a8f9449SShaohua Li idx++; 2235*2a8f9449SShaohua Li if (idx == nr_clusters) 2236*2a8f9449SShaohua Li idx = 0; 2237*2a8f9449SShaohua Li } 2238915d4d7bSCesar Eduardo Barros return nr_extents; 2239915d4d7bSCesar Eduardo Barros } 2240915d4d7bSCesar Eduardo Barros 2241dcf6b7ddSRafael Aquini /* 2242dcf6b7ddSRafael Aquini * Helper to sys_swapon determining if a given swap 2243dcf6b7ddSRafael Aquini * backing device queue supports DISCARD operations. 2244dcf6b7ddSRafael Aquini */ 2245dcf6b7ddSRafael Aquini static bool swap_discardable(struct swap_info_struct *si) 2246dcf6b7ddSRafael Aquini { 2247dcf6b7ddSRafael Aquini struct request_queue *q = bdev_get_queue(si->bdev); 2248dcf6b7ddSRafael Aquini 2249dcf6b7ddSRafael Aquini if (!q || !blk_queue_discard(q)) 2250dcf6b7ddSRafael Aquini return false; 2251dcf6b7ddSRafael Aquini 2252dcf6b7ddSRafael Aquini return true; 2253dcf6b7ddSRafael Aquini } 2254dcf6b7ddSRafael Aquini 225553cbb243SCesar Eduardo Barros SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) 225653cbb243SCesar Eduardo Barros { 225753cbb243SCesar Eduardo Barros struct swap_info_struct *p; 225891a27b2aSJeff Layton struct filename *name; 225953cbb243SCesar Eduardo Barros struct file *swap_file = NULL; 226053cbb243SCesar Eduardo Barros struct address_space *mapping; 226140531542SCesar Eduardo Barros int i; 226240531542SCesar Eduardo Barros int prio; 226353cbb243SCesar Eduardo Barros int error; 226453cbb243SCesar Eduardo Barros union swap_header *swap_header; 2265915d4d7bSCesar Eduardo Barros int nr_extents; 226653cbb243SCesar Eduardo Barros sector_t span; 226753cbb243SCesar Eduardo Barros unsigned long maxpages; 226853cbb243SCesar Eduardo Barros unsigned char *swap_map = NULL; 2269*2a8f9449SShaohua Li struct swap_cluster_info *cluster_info = NULL; 227038b5faf4SDan Magenheimer unsigned long *frontswap_map = NULL; 227153cbb243SCesar Eduardo Barros struct page *page = NULL; 227253cbb243SCesar Eduardo Barros struct inode *inode = NULL; 227353cbb243SCesar Eduardo Barros 2274d15cab97SHugh Dickins if (swap_flags & ~SWAP_FLAGS_VALID) 2275d15cab97SHugh Dickins return -EINVAL; 2276d15cab97SHugh Dickins 227753cbb243SCesar Eduardo Barros if (!capable(CAP_SYS_ADMIN)) 227853cbb243SCesar Eduardo Barros return -EPERM; 227953cbb243SCesar Eduardo Barros 228053cbb243SCesar Eduardo Barros p = alloc_swap_info(); 22812542e513SCesar Eduardo Barros if (IS_ERR(p)) 22822542e513SCesar Eduardo Barros return PTR_ERR(p); 228353cbb243SCesar Eduardo Barros 22841da177e4SLinus Torvalds name = getname(specialfile); 22851da177e4SLinus Torvalds if (IS_ERR(name)) { 22867de7fb6bSCesar Eduardo Barros error = PTR_ERR(name); 22871da177e4SLinus Torvalds name = NULL; 2288bd69010bSCesar Eduardo Barros goto bad_swap; 22891da177e4SLinus Torvalds } 2290669abf4eSJeff Layton swap_file = file_open_name(name, O_RDWR|O_LARGEFILE, 0); 22911da177e4SLinus Torvalds if (IS_ERR(swap_file)) { 22927de7fb6bSCesar Eduardo Barros error = PTR_ERR(swap_file); 22931da177e4SLinus Torvalds swap_file = NULL; 2294bd69010bSCesar Eduardo Barros goto bad_swap; 22951da177e4SLinus Torvalds } 22961da177e4SLinus Torvalds 22971da177e4SLinus Torvalds p->swap_file = swap_file; 22981da177e4SLinus Torvalds mapping = swap_file->f_mapping; 22991da177e4SLinus Torvalds 23001da177e4SLinus Torvalds for (i = 0; i < nr_swapfiles; i++) { 2301efa90a98SHugh Dickins struct swap_info_struct *q = swap_info[i]; 23021da177e4SLinus Torvalds 2303e8e6c2ecSCesar Eduardo Barros if (q == p || !q->swap_file) 23041da177e4SLinus Torvalds continue; 23057de7fb6bSCesar Eduardo Barros if (mapping == q->swap_file->f_mapping) { 23067de7fb6bSCesar Eduardo Barros error = -EBUSY; 23071da177e4SLinus Torvalds goto bad_swap; 23081da177e4SLinus Torvalds } 23097de7fb6bSCesar Eduardo Barros } 23101da177e4SLinus Torvalds 23112130781eSCesar Eduardo Barros inode = mapping->host; 23122130781eSCesar Eduardo Barros /* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */ 23134d0e1e10SCesar Eduardo Barros error = claim_swapfile(p, inode); 23144d0e1e10SCesar Eduardo Barros if (unlikely(error)) 23151da177e4SLinus Torvalds goto bad_swap; 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds /* 23181da177e4SLinus Torvalds * Read the swap header. 23191da177e4SLinus Torvalds */ 23201da177e4SLinus Torvalds if (!mapping->a_ops->readpage) { 23211da177e4SLinus Torvalds error = -EINVAL; 23221da177e4SLinus Torvalds goto bad_swap; 23231da177e4SLinus Torvalds } 2324090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, swap_file); 23251da177e4SLinus Torvalds if (IS_ERR(page)) { 23261da177e4SLinus Torvalds error = PTR_ERR(page); 23271da177e4SLinus Torvalds goto bad_swap; 23281da177e4SLinus Torvalds } 232981e33971SHugh Dickins swap_header = kmap(page); 23301da177e4SLinus Torvalds 2331ca8bd38bSCesar Eduardo Barros maxpages = read_swap_header(p, swap_header, inode); 2332ca8bd38bSCesar Eduardo Barros if (unlikely(!maxpages)) { 23331da177e4SLinus Torvalds error = -EINVAL; 23341da177e4SLinus Torvalds goto bad_swap; 23351da177e4SLinus Torvalds } 23361da177e4SLinus Torvalds 23371da177e4SLinus Torvalds /* OK, set up the swap map and apply the bad block list */ 2338803d0c83SCesar Eduardo Barros swap_map = vzalloc(maxpages); 233978ecba08SHugh Dickins if (!swap_map) { 23401da177e4SLinus Torvalds error = -ENOMEM; 23411da177e4SLinus Torvalds goto bad_swap; 23421da177e4SLinus Torvalds } 2343*2a8f9449SShaohua Li if (p->bdev && blk_queue_nonrot(bdev_get_queue(p->bdev))) { 2344*2a8f9449SShaohua Li p->flags |= SWP_SOLIDSTATE; 2345*2a8f9449SShaohua Li /* 2346*2a8f9449SShaohua Li * select a random position to start with to help wear leveling 2347*2a8f9449SShaohua Li * SSD 2348*2a8f9449SShaohua Li */ 2349*2a8f9449SShaohua Li p->cluster_next = 1 + (prandom_u32() % p->highest_bit); 2350*2a8f9449SShaohua Li 2351*2a8f9449SShaohua Li cluster_info = vzalloc(DIV_ROUND_UP(maxpages, 2352*2a8f9449SShaohua Li SWAPFILE_CLUSTER) * sizeof(*cluster_info)); 2353*2a8f9449SShaohua Li if (!cluster_info) { 2354*2a8f9449SShaohua Li error = -ENOMEM; 2355*2a8f9449SShaohua Li goto bad_swap; 2356*2a8f9449SShaohua Li } 2357*2a8f9449SShaohua Li } 23581da177e4SLinus Torvalds 23591421ef3cSCesar Eduardo Barros error = swap_cgroup_swapon(p->type, maxpages); 23601421ef3cSCesar Eduardo Barros if (error) 23611421ef3cSCesar Eduardo Barros goto bad_swap; 23621421ef3cSCesar Eduardo Barros 2363915d4d7bSCesar Eduardo Barros nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map, 2364*2a8f9449SShaohua Li cluster_info, maxpages, &span); 2365915d4d7bSCesar Eduardo Barros if (unlikely(nr_extents < 0)) { 236653092a74SHugh Dickins error = nr_extents; 2367e2244ec2SHugh Dickins goto bad_swap; 236853092a74SHugh Dickins } 236938b5faf4SDan Magenheimer /* frontswap enabled? set up bit-per-page map for frontswap */ 237038b5faf4SDan Magenheimer if (frontswap_enabled) 23717b57976dSAkinobu Mita frontswap_map = vzalloc(BITS_TO_LONGS(maxpages) * sizeof(long)); 23721da177e4SLinus Torvalds 2373*2a8f9449SShaohua Li if (p->bdev &&(swap_flags & SWAP_FLAG_DISCARD) && swap_discardable(p)) { 2374dcf6b7ddSRafael Aquini /* 2375dcf6b7ddSRafael Aquini * When discard is enabled for swap with no particular 2376dcf6b7ddSRafael Aquini * policy flagged, we set all swap discard flags here in 2377dcf6b7ddSRafael Aquini * order to sustain backward compatibility with older 2378dcf6b7ddSRafael Aquini * swapon(8) releases. 2379dcf6b7ddSRafael Aquini */ 2380dcf6b7ddSRafael Aquini p->flags |= (SWP_DISCARDABLE | SWP_AREA_DISCARD | 2381dcf6b7ddSRafael Aquini SWP_PAGE_DISCARD); 2382dcf6b7ddSRafael Aquini 2383dcf6b7ddSRafael Aquini /* 2384dcf6b7ddSRafael Aquini * By flagging sys_swapon, a sysadmin can tell us to 2385dcf6b7ddSRafael Aquini * either do single-time area discards only, or to just 2386dcf6b7ddSRafael Aquini * perform discards for released swap page-clusters. 2387dcf6b7ddSRafael Aquini * Now it's time to adjust the p->flags accordingly. 2388dcf6b7ddSRafael Aquini */ 2389dcf6b7ddSRafael Aquini if (swap_flags & SWAP_FLAG_DISCARD_ONCE) 2390dcf6b7ddSRafael Aquini p->flags &= ~SWP_PAGE_DISCARD; 2391dcf6b7ddSRafael Aquini else if (swap_flags & SWAP_FLAG_DISCARD_PAGES) 2392dcf6b7ddSRafael Aquini p->flags &= ~SWP_AREA_DISCARD; 2393dcf6b7ddSRafael Aquini 2394dcf6b7ddSRafael Aquini /* issue a swapon-time discard if it's still required */ 2395dcf6b7ddSRafael Aquini if (p->flags & SWP_AREA_DISCARD) { 2396dcf6b7ddSRafael Aquini int err = discard_swap(p); 2397dcf6b7ddSRafael Aquini if (unlikely(err)) 2398465c47fdSAndrew Morton pr_err("swapon: discard_swap(%p): %d\n", 2399dcf6b7ddSRafael Aquini p, err); 2400dcf6b7ddSRafael Aquini } 2401dcf6b7ddSRafael Aquini } 24026a6ba831SHugh Dickins 2403fc0abb14SIngo Molnar mutex_lock(&swapon_mutex); 240440531542SCesar Eduardo Barros prio = -1; 240578ecba08SHugh Dickins if (swap_flags & SWAP_FLAG_PREFER) 240640531542SCesar Eduardo Barros prio = 240778ecba08SHugh Dickins (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; 2408*2a8f9449SShaohua Li enable_swap_info(p, prio, swap_map, cluster_info, frontswap_map); 2409c69dbfb8SCesar Eduardo Barros 2410465c47fdSAndrew Morton pr_info("Adding %uk swap on %s. " 2411dcf6b7ddSRafael Aquini "Priority:%d extents:%d across:%lluk %s%s%s%s%s\n", 241291a27b2aSJeff Layton p->pages<<(PAGE_SHIFT-10), name->name, p->prio, 2413c69dbfb8SCesar Eduardo Barros nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), 2414c69dbfb8SCesar Eduardo Barros (p->flags & SWP_SOLIDSTATE) ? "SS" : "", 241538b5faf4SDan Magenheimer (p->flags & SWP_DISCARDABLE) ? "D" : "", 2416dcf6b7ddSRafael Aquini (p->flags & SWP_AREA_DISCARD) ? "s" : "", 2417dcf6b7ddSRafael Aquini (p->flags & SWP_PAGE_DISCARD) ? "c" : "", 241838b5faf4SDan Magenheimer (frontswap_map) ? "FS" : ""); 2419c69dbfb8SCesar Eduardo Barros 2420fc0abb14SIngo Molnar mutex_unlock(&swapon_mutex); 242166d7dd51SKay Sievers atomic_inc(&proc_poll_event); 242266d7dd51SKay Sievers wake_up_interruptible(&proc_poll_wait); 242366d7dd51SKay Sievers 24249b01c350SCesar Eduardo Barros if (S_ISREG(inode->i_mode)) 24259b01c350SCesar Eduardo Barros inode->i_flags |= S_SWAPFILE; 24261da177e4SLinus Torvalds error = 0; 24271da177e4SLinus Torvalds goto out; 24281da177e4SLinus Torvalds bad_swap: 2429bd69010bSCesar Eduardo Barros if (inode && S_ISBLK(inode->i_mode) && p->bdev) { 2430f2090d2dSCesar Eduardo Barros set_blocksize(p->bdev, p->old_block_size); 2431f2090d2dSCesar Eduardo Barros blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL); 24321da177e4SLinus Torvalds } 24334cd3bb10SHugh Dickins destroy_swap_extents(p); 2434e8e6c2ecSCesar Eduardo Barros swap_cgroup_swapoff(p->type); 24355d337b91SHugh Dickins spin_lock(&swap_lock); 24361da177e4SLinus Torvalds p->swap_file = NULL; 24371da177e4SLinus Torvalds p->flags = 0; 24385d337b91SHugh Dickins spin_unlock(&swap_lock); 24391da177e4SLinus Torvalds vfree(swap_map); 2440*2a8f9449SShaohua Li vfree(cluster_info); 244152c50567SMel Gorman if (swap_file) { 24422130781eSCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) { 244352c50567SMel Gorman mutex_unlock(&inode->i_mutex); 24442130781eSCesar Eduardo Barros inode = NULL; 24452130781eSCesar Eduardo Barros } 24461da177e4SLinus Torvalds filp_close(swap_file, NULL); 244752c50567SMel Gorman } 24481da177e4SLinus Torvalds out: 24491da177e4SLinus Torvalds if (page && !IS_ERR(page)) { 24501da177e4SLinus Torvalds kunmap(page); 24511da177e4SLinus Torvalds page_cache_release(page); 24521da177e4SLinus Torvalds } 24531da177e4SLinus Torvalds if (name) 24541da177e4SLinus Torvalds putname(name); 24559b01c350SCesar Eduardo Barros if (inode && S_ISREG(inode->i_mode)) 24561b1dcc1bSJes Sorensen mutex_unlock(&inode->i_mutex); 24571da177e4SLinus Torvalds return error; 24581da177e4SLinus Torvalds } 24591da177e4SLinus Torvalds 24601da177e4SLinus Torvalds void si_swapinfo(struct sysinfo *val) 24611da177e4SLinus Torvalds { 2462efa90a98SHugh Dickins unsigned int type; 24631da177e4SLinus Torvalds unsigned long nr_to_be_unused = 0; 24641da177e4SLinus Torvalds 24655d337b91SHugh Dickins spin_lock(&swap_lock); 2466efa90a98SHugh Dickins for (type = 0; type < nr_swapfiles; type++) { 2467efa90a98SHugh Dickins struct swap_info_struct *si = swap_info[type]; 2468efa90a98SHugh Dickins 2469efa90a98SHugh Dickins if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) 2470efa90a98SHugh Dickins nr_to_be_unused += si->inuse_pages; 24711da177e4SLinus Torvalds } 2472ec8acf20SShaohua Li val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused; 24731da177e4SLinus Torvalds val->totalswap = total_swap_pages + nr_to_be_unused; 24745d337b91SHugh Dickins spin_unlock(&swap_lock); 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24771da177e4SLinus Torvalds /* 24781da177e4SLinus Torvalds * Verify that a swap entry is valid and increment its swap map count. 24791da177e4SLinus Torvalds * 2480355cfa73SKAMEZAWA Hiroyuki * Returns error code in following case. 2481355cfa73SKAMEZAWA Hiroyuki * - success -> 0 2482355cfa73SKAMEZAWA Hiroyuki * - swp_entry is invalid -> EINVAL 2483355cfa73SKAMEZAWA Hiroyuki * - swp_entry is migration entry -> EINVAL 2484355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but there is already one. -> EEXIST 2485355cfa73SKAMEZAWA Hiroyuki * - swap-cache reference is requested but the entry is not used. -> ENOENT 2486570a335bSHugh Dickins * - swap-mapped reference requested but needs continued swap count. -> ENOMEM 24871da177e4SLinus Torvalds */ 24888d69aaeeSHugh Dickins static int __swap_duplicate(swp_entry_t entry, unsigned char usage) 24891da177e4SLinus Torvalds { 24901da177e4SLinus Torvalds struct swap_info_struct *p; 24911da177e4SLinus Torvalds unsigned long offset, type; 24928d69aaeeSHugh Dickins unsigned char count; 24938d69aaeeSHugh Dickins unsigned char has_cache; 2494253d553bSHugh Dickins int err = -EINVAL; 24951da177e4SLinus Torvalds 2496a7420aa5SAndi Kleen if (non_swap_entry(entry)) 2497253d553bSHugh Dickins goto out; 24980697212aSChristoph Lameter 24991da177e4SLinus Torvalds type = swp_type(entry); 25001da177e4SLinus Torvalds if (type >= nr_swapfiles) 25011da177e4SLinus Torvalds goto bad_file; 2502efa90a98SHugh Dickins p = swap_info[type]; 25031da177e4SLinus Torvalds offset = swp_offset(entry); 25041da177e4SLinus Torvalds 2505ec8acf20SShaohua Li spin_lock(&p->lock); 2506355cfa73SKAMEZAWA Hiroyuki if (unlikely(offset >= p->max)) 2507355cfa73SKAMEZAWA Hiroyuki goto unlock_out; 2508355cfa73SKAMEZAWA Hiroyuki 2509253d553bSHugh Dickins count = p->swap_map[offset]; 2510253d553bSHugh Dickins has_cache = count & SWAP_HAS_CACHE; 2511253d553bSHugh Dickins count &= ~SWAP_HAS_CACHE; 2512253d553bSHugh Dickins err = 0; 2513355cfa73SKAMEZAWA Hiroyuki 2514253d553bSHugh Dickins if (usage == SWAP_HAS_CACHE) { 2515355cfa73SKAMEZAWA Hiroyuki 2516355cfa73SKAMEZAWA Hiroyuki /* set SWAP_HAS_CACHE if there is no cache and entry is used */ 2517253d553bSHugh Dickins if (!has_cache && count) 2518253d553bSHugh Dickins has_cache = SWAP_HAS_CACHE; 2519253d553bSHugh Dickins else if (has_cache) /* someone else added cache */ 2520253d553bSHugh Dickins err = -EEXIST; 2521253d553bSHugh Dickins else /* no users remaining */ 2522253d553bSHugh Dickins err = -ENOENT; 2523355cfa73SKAMEZAWA Hiroyuki 2524355cfa73SKAMEZAWA Hiroyuki } else if (count || has_cache) { 2525253d553bSHugh Dickins 2526570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) 2527570a335bSHugh Dickins count += usage; 2528570a335bSHugh Dickins else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) 2529253d553bSHugh Dickins err = -EINVAL; 2530570a335bSHugh Dickins else if (swap_count_continued(p, offset, count)) 2531570a335bSHugh Dickins count = COUNT_CONTINUED; 2532570a335bSHugh Dickins else 2533570a335bSHugh Dickins err = -ENOMEM; 2534253d553bSHugh Dickins } else 2535253d553bSHugh Dickins err = -ENOENT; /* unused swap entry */ 2536253d553bSHugh Dickins 2537253d553bSHugh Dickins p->swap_map[offset] = count | has_cache; 2538253d553bSHugh Dickins 2539355cfa73SKAMEZAWA Hiroyuki unlock_out: 2540ec8acf20SShaohua Li spin_unlock(&p->lock); 25411da177e4SLinus Torvalds out: 2542253d553bSHugh Dickins return err; 25431da177e4SLinus Torvalds 25441da177e4SLinus Torvalds bad_file: 2545465c47fdSAndrew Morton pr_err("swap_dup: %s%08lx\n", Bad_file, entry.val); 25461da177e4SLinus Torvalds goto out; 25471da177e4SLinus Torvalds } 2548253d553bSHugh Dickins 2549355cfa73SKAMEZAWA Hiroyuki /* 2550aaa46865SHugh Dickins * Help swapoff by noting that swap entry belongs to shmem/tmpfs 2551aaa46865SHugh Dickins * (in which case its reference count is never incremented). 2552aaa46865SHugh Dickins */ 2553aaa46865SHugh Dickins void swap_shmem_alloc(swp_entry_t entry) 2554aaa46865SHugh Dickins { 2555aaa46865SHugh Dickins __swap_duplicate(entry, SWAP_MAP_SHMEM); 2556aaa46865SHugh Dickins } 2557aaa46865SHugh Dickins 2558aaa46865SHugh Dickins /* 255908259d58SHugh Dickins * Increase reference count of swap entry by 1. 256008259d58SHugh Dickins * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required 256108259d58SHugh Dickins * but could not be atomically allocated. Returns 0, just as if it succeeded, 256208259d58SHugh Dickins * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which 256308259d58SHugh Dickins * might occur if a page table entry has got corrupted. 2564355cfa73SKAMEZAWA Hiroyuki */ 2565570a335bSHugh Dickins int swap_duplicate(swp_entry_t entry) 2566355cfa73SKAMEZAWA Hiroyuki { 2567570a335bSHugh Dickins int err = 0; 2568570a335bSHugh Dickins 2569570a335bSHugh Dickins while (!err && __swap_duplicate(entry, 1) == -ENOMEM) 2570570a335bSHugh Dickins err = add_swap_count_continuation(entry, GFP_ATOMIC); 2571570a335bSHugh Dickins return err; 2572355cfa73SKAMEZAWA Hiroyuki } 25731da177e4SLinus Torvalds 2574cb4b86baSKAMEZAWA Hiroyuki /* 2575355cfa73SKAMEZAWA Hiroyuki * @entry: swap entry for which we allocate swap cache. 2576355cfa73SKAMEZAWA Hiroyuki * 257773c34b6aSHugh Dickins * Called when allocating swap cache for existing swap entry, 2578355cfa73SKAMEZAWA Hiroyuki * This can return error codes. Returns 0 at success. 2579355cfa73SKAMEZAWA Hiroyuki * -EBUSY means there is a swap cache. 2580355cfa73SKAMEZAWA Hiroyuki * Note: return code is different from swap_duplicate(). 2581cb4b86baSKAMEZAWA Hiroyuki */ 2582cb4b86baSKAMEZAWA Hiroyuki int swapcache_prepare(swp_entry_t entry) 2583cb4b86baSKAMEZAWA Hiroyuki { 2584253d553bSHugh Dickins return __swap_duplicate(entry, SWAP_HAS_CACHE); 2585cb4b86baSKAMEZAWA Hiroyuki } 2586cb4b86baSKAMEZAWA Hiroyuki 2587f981c595SMel Gorman struct swap_info_struct *page_swap_info(struct page *page) 2588f981c595SMel Gorman { 2589f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2590f981c595SMel Gorman BUG_ON(!PageSwapCache(page)); 2591f981c595SMel Gorman return swap_info[swp_type(swap)]; 2592f981c595SMel Gorman } 2593f981c595SMel Gorman 2594f981c595SMel Gorman /* 2595f981c595SMel Gorman * out-of-line __page_file_ methods to avoid include hell. 2596f981c595SMel Gorman */ 2597f981c595SMel Gorman struct address_space *__page_file_mapping(struct page *page) 2598f981c595SMel Gorman { 2599f981c595SMel Gorman VM_BUG_ON(!PageSwapCache(page)); 2600f981c595SMel Gorman return page_swap_info(page)->swap_file->f_mapping; 2601f981c595SMel Gorman } 2602f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_mapping); 2603f981c595SMel Gorman 2604f981c595SMel Gorman pgoff_t __page_file_index(struct page *page) 2605f981c595SMel Gorman { 2606f981c595SMel Gorman swp_entry_t swap = { .val = page_private(page) }; 2607f981c595SMel Gorman VM_BUG_ON(!PageSwapCache(page)); 2608f981c595SMel Gorman return swp_offset(swap); 2609f981c595SMel Gorman } 2610f981c595SMel Gorman EXPORT_SYMBOL_GPL(__page_file_index); 2611f981c595SMel Gorman 26121da177e4SLinus Torvalds /* 2613570a335bSHugh Dickins * add_swap_count_continuation - called when a swap count is duplicated 2614570a335bSHugh Dickins * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's 2615570a335bSHugh Dickins * page of the original vmalloc'ed swap_map, to hold the continuation count 2616570a335bSHugh Dickins * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called 2617570a335bSHugh Dickins * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. 2618570a335bSHugh Dickins * 2619570a335bSHugh Dickins * These continuation pages are seldom referenced: the common paths all work 2620570a335bSHugh Dickins * on the original swap_map, only referring to a continuation page when the 2621570a335bSHugh Dickins * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. 2622570a335bSHugh Dickins * 2623570a335bSHugh Dickins * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding 2624570a335bSHugh Dickins * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) 2625570a335bSHugh Dickins * can be called after dropping locks. 2626570a335bSHugh Dickins */ 2627570a335bSHugh Dickins int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) 2628570a335bSHugh Dickins { 2629570a335bSHugh Dickins struct swap_info_struct *si; 2630570a335bSHugh Dickins struct page *head; 2631570a335bSHugh Dickins struct page *page; 2632570a335bSHugh Dickins struct page *list_page; 2633570a335bSHugh Dickins pgoff_t offset; 2634570a335bSHugh Dickins unsigned char count; 2635570a335bSHugh Dickins 2636570a335bSHugh Dickins /* 2637570a335bSHugh Dickins * When debugging, it's easier to use __GFP_ZERO here; but it's better 2638570a335bSHugh Dickins * for latency not to zero a page while GFP_ATOMIC and holding locks. 2639570a335bSHugh Dickins */ 2640570a335bSHugh Dickins page = alloc_page(gfp_mask | __GFP_HIGHMEM); 2641570a335bSHugh Dickins 2642570a335bSHugh Dickins si = swap_info_get(entry); 2643570a335bSHugh Dickins if (!si) { 2644570a335bSHugh Dickins /* 2645570a335bSHugh Dickins * An acceptable race has occurred since the failing 2646570a335bSHugh Dickins * __swap_duplicate(): the swap entry has been freed, 2647570a335bSHugh Dickins * perhaps even the whole swap_map cleared for swapoff. 2648570a335bSHugh Dickins */ 2649570a335bSHugh Dickins goto outer; 2650570a335bSHugh Dickins } 2651570a335bSHugh Dickins 2652570a335bSHugh Dickins offset = swp_offset(entry); 2653570a335bSHugh Dickins count = si->swap_map[offset] & ~SWAP_HAS_CACHE; 2654570a335bSHugh Dickins 2655570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { 2656570a335bSHugh Dickins /* 2657570a335bSHugh Dickins * The higher the swap count, the more likely it is that tasks 2658570a335bSHugh Dickins * will race to add swap count continuation: we need to avoid 2659570a335bSHugh Dickins * over-provisioning. 2660570a335bSHugh Dickins */ 2661570a335bSHugh Dickins goto out; 2662570a335bSHugh Dickins } 2663570a335bSHugh Dickins 2664570a335bSHugh Dickins if (!page) { 2665ec8acf20SShaohua Li spin_unlock(&si->lock); 2666570a335bSHugh Dickins return -ENOMEM; 2667570a335bSHugh Dickins } 2668570a335bSHugh Dickins 2669570a335bSHugh Dickins /* 2670570a335bSHugh Dickins * We are fortunate that although vmalloc_to_page uses pte_offset_map, 2671570a335bSHugh Dickins * no architecture is using highmem pages for kernel pagetables: so it 2672570a335bSHugh Dickins * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps. 2673570a335bSHugh Dickins */ 2674570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2675570a335bSHugh Dickins offset &= ~PAGE_MASK; 2676570a335bSHugh Dickins 2677570a335bSHugh Dickins /* 2678570a335bSHugh Dickins * Page allocation does not initialize the page's lru field, 2679570a335bSHugh Dickins * but it does always reset its private field. 2680570a335bSHugh Dickins */ 2681570a335bSHugh Dickins if (!page_private(head)) { 2682570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 2683570a335bSHugh Dickins INIT_LIST_HEAD(&head->lru); 2684570a335bSHugh Dickins set_page_private(head, SWP_CONTINUED); 2685570a335bSHugh Dickins si->flags |= SWP_CONTINUED; 2686570a335bSHugh Dickins } 2687570a335bSHugh Dickins 2688570a335bSHugh Dickins list_for_each_entry(list_page, &head->lru, lru) { 2689570a335bSHugh Dickins unsigned char *map; 2690570a335bSHugh Dickins 2691570a335bSHugh Dickins /* 2692570a335bSHugh Dickins * If the previous map said no continuation, but we've found 2693570a335bSHugh Dickins * a continuation page, free our allocation and use this one. 2694570a335bSHugh Dickins */ 2695570a335bSHugh Dickins if (!(count & COUNT_CONTINUED)) 2696570a335bSHugh Dickins goto out; 2697570a335bSHugh Dickins 26989b04c5feSCong Wang map = kmap_atomic(list_page) + offset; 2699570a335bSHugh Dickins count = *map; 27009b04c5feSCong Wang kunmap_atomic(map); 2701570a335bSHugh Dickins 2702570a335bSHugh Dickins /* 2703570a335bSHugh Dickins * If this continuation count now has some space in it, 2704570a335bSHugh Dickins * free our allocation and use this one. 2705570a335bSHugh Dickins */ 2706570a335bSHugh Dickins if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) 2707570a335bSHugh Dickins goto out; 2708570a335bSHugh Dickins } 2709570a335bSHugh Dickins 2710570a335bSHugh Dickins list_add_tail(&page->lru, &head->lru); 2711570a335bSHugh Dickins page = NULL; /* now it's attached, don't free it */ 2712570a335bSHugh Dickins out: 2713ec8acf20SShaohua Li spin_unlock(&si->lock); 2714570a335bSHugh Dickins outer: 2715570a335bSHugh Dickins if (page) 2716570a335bSHugh Dickins __free_page(page); 2717570a335bSHugh Dickins return 0; 2718570a335bSHugh Dickins } 2719570a335bSHugh Dickins 2720570a335bSHugh Dickins /* 2721570a335bSHugh Dickins * swap_count_continued - when the original swap_map count is incremented 2722570a335bSHugh Dickins * from SWAP_MAP_MAX, check if there is already a continuation page to carry 2723570a335bSHugh Dickins * into, carry if so, or else fail until a new continuation page is allocated; 2724570a335bSHugh Dickins * when the original swap_map count is decremented from 0 with continuation, 2725570a335bSHugh Dickins * borrow from the continuation and report whether it still holds more. 2726570a335bSHugh Dickins * Called while __swap_duplicate() or swap_entry_free() holds swap_lock. 2727570a335bSHugh Dickins */ 2728570a335bSHugh Dickins static bool swap_count_continued(struct swap_info_struct *si, 2729570a335bSHugh Dickins pgoff_t offset, unsigned char count) 2730570a335bSHugh Dickins { 2731570a335bSHugh Dickins struct page *head; 2732570a335bSHugh Dickins struct page *page; 2733570a335bSHugh Dickins unsigned char *map; 2734570a335bSHugh Dickins 2735570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2736570a335bSHugh Dickins if (page_private(head) != SWP_CONTINUED) { 2737570a335bSHugh Dickins BUG_ON(count & COUNT_CONTINUED); 2738570a335bSHugh Dickins return false; /* need to add count continuation */ 2739570a335bSHugh Dickins } 2740570a335bSHugh Dickins 2741570a335bSHugh Dickins offset &= ~PAGE_MASK; 2742570a335bSHugh Dickins page = list_entry(head->lru.next, struct page, lru); 27439b04c5feSCong Wang map = kmap_atomic(page) + offset; 2744570a335bSHugh Dickins 2745570a335bSHugh Dickins if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ 2746570a335bSHugh Dickins goto init_map; /* jump over SWAP_CONT_MAX checks */ 2747570a335bSHugh Dickins 2748570a335bSHugh Dickins if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ 2749570a335bSHugh Dickins /* 2750570a335bSHugh Dickins * Think of how you add 1 to 999 2751570a335bSHugh Dickins */ 2752570a335bSHugh Dickins while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { 27539b04c5feSCong Wang kunmap_atomic(map); 2754570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2755570a335bSHugh Dickins BUG_ON(page == head); 27569b04c5feSCong Wang map = kmap_atomic(page) + offset; 2757570a335bSHugh Dickins } 2758570a335bSHugh Dickins if (*map == SWAP_CONT_MAX) { 27599b04c5feSCong Wang kunmap_atomic(map); 2760570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2761570a335bSHugh Dickins if (page == head) 2762570a335bSHugh Dickins return false; /* add count continuation */ 27639b04c5feSCong Wang map = kmap_atomic(page) + offset; 2764570a335bSHugh Dickins init_map: *map = 0; /* we didn't zero the page */ 2765570a335bSHugh Dickins } 2766570a335bSHugh Dickins *map += 1; 27679b04c5feSCong Wang kunmap_atomic(map); 2768570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2769570a335bSHugh Dickins while (page != head) { 27709b04c5feSCong Wang map = kmap_atomic(page) + offset; 2771570a335bSHugh Dickins *map = COUNT_CONTINUED; 27729b04c5feSCong Wang kunmap_atomic(map); 2773570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2774570a335bSHugh Dickins } 2775570a335bSHugh Dickins return true; /* incremented */ 2776570a335bSHugh Dickins 2777570a335bSHugh Dickins } else { /* decrementing */ 2778570a335bSHugh Dickins /* 2779570a335bSHugh Dickins * Think of how you subtract 1 from 1000 2780570a335bSHugh Dickins */ 2781570a335bSHugh Dickins BUG_ON(count != COUNT_CONTINUED); 2782570a335bSHugh Dickins while (*map == COUNT_CONTINUED) { 27839b04c5feSCong Wang kunmap_atomic(map); 2784570a335bSHugh Dickins page = list_entry(page->lru.next, struct page, lru); 2785570a335bSHugh Dickins BUG_ON(page == head); 27869b04c5feSCong Wang map = kmap_atomic(page) + offset; 2787570a335bSHugh Dickins } 2788570a335bSHugh Dickins BUG_ON(*map == 0); 2789570a335bSHugh Dickins *map -= 1; 2790570a335bSHugh Dickins if (*map == 0) 2791570a335bSHugh Dickins count = 0; 27929b04c5feSCong Wang kunmap_atomic(map); 2793570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2794570a335bSHugh Dickins while (page != head) { 27959b04c5feSCong Wang map = kmap_atomic(page) + offset; 2796570a335bSHugh Dickins *map = SWAP_CONT_MAX | count; 2797570a335bSHugh Dickins count = COUNT_CONTINUED; 27989b04c5feSCong Wang kunmap_atomic(map); 2799570a335bSHugh Dickins page = list_entry(page->lru.prev, struct page, lru); 2800570a335bSHugh Dickins } 2801570a335bSHugh Dickins return count == COUNT_CONTINUED; 2802570a335bSHugh Dickins } 2803570a335bSHugh Dickins } 2804570a335bSHugh Dickins 2805570a335bSHugh Dickins /* 2806570a335bSHugh Dickins * free_swap_count_continuations - swapoff free all the continuation pages 2807570a335bSHugh Dickins * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. 2808570a335bSHugh Dickins */ 2809570a335bSHugh Dickins static void free_swap_count_continuations(struct swap_info_struct *si) 2810570a335bSHugh Dickins { 2811570a335bSHugh Dickins pgoff_t offset; 2812570a335bSHugh Dickins 2813570a335bSHugh Dickins for (offset = 0; offset < si->max; offset += PAGE_SIZE) { 2814570a335bSHugh Dickins struct page *head; 2815570a335bSHugh Dickins head = vmalloc_to_page(si->swap_map + offset); 2816570a335bSHugh Dickins if (page_private(head)) { 2817570a335bSHugh Dickins struct list_head *this, *next; 2818570a335bSHugh Dickins list_for_each_safe(this, next, &head->lru) { 2819570a335bSHugh Dickins struct page *page; 2820570a335bSHugh Dickins page = list_entry(this, struct page, lru); 2821570a335bSHugh Dickins list_del(this); 2822570a335bSHugh Dickins __free_page(page); 2823570a335bSHugh Dickins } 2824570a335bSHugh Dickins } 2825570a335bSHugh Dickins } 2826570a335bSHugh Dickins } 2827