Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/swapfile.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 5 | * Swap reorganised 29.12.95, Stephen Tweedie |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/mm.h> |
| 9 | #include <linux/hugetlb.h> |
| 10 | #include <linux/mman.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/kernel_stat.h> |
| 13 | #include <linux/swap.h> |
| 14 | #include <linux/vmalloc.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/namei.h> |
| 17 | #include <linux/shm.h> |
| 18 | #include <linux/blkdev.h> |
Hugh Dickins | 20137a4 | 2009-01-06 14:39:54 -0800 | [diff] [blame] | 19 | #include <linux/random.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/writeback.h> |
| 21 | #include <linux/proc_fs.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/rmap.h> |
| 26 | #include <linux/security.h> |
| 27 | #include <linux/backing-dev.h> |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 29 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/syscalls.h> |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 31 | #include <linux/memcontrol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/tlbflush.h> |
| 35 | #include <linux/swapops.h> |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 36 | #include <linux/page_cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 38 | static bool swap_count_continued(struct swap_info_struct *, pgoff_t, |
| 39 | unsigned char); |
| 40 | static void free_swap_count_continuations(struct swap_info_struct *); |
| 41 | |
Adrian Bunk | 7c363b8 | 2008-07-25 19:46:24 -0700 | [diff] [blame] | 42 | static DEFINE_SPINLOCK(swap_lock); |
| 43 | static unsigned int nr_swapfiles; |
Hugh Dickins | b962716 | 2009-01-06 14:39:41 -0800 | [diff] [blame] | 44 | long nr_swap_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | long total_swap_pages; |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 46 | static int least_priority; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static const char Bad_file[] = "Bad swap file entry "; |
| 49 | static const char Unused_file[] = "Unused swap file entry "; |
| 50 | static const char Bad_offset[] = "Bad swap offset entry "; |
| 51 | static const char Unused_offset[] = "Unused swap offset entry "; |
| 52 | |
Adrian Bunk | 7c363b8 | 2008-07-25 19:46:24 -0700 | [diff] [blame] | 53 | static struct swap_list_t swap_list = {-1, -1}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 55 | static struct swap_info_struct *swap_info[MAX_SWAPFILES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 57 | static DEFINE_MUTEX(swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 59 | static inline unsigned char swap_count(unsigned char ent) |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 60 | { |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 61 | return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */ |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 64 | /* returns 1 if swap entry is freed */ |
KAMEZAWA Hiroyuki | c9e4441 | 2009-06-16 15:32:54 -0700 | [diff] [blame] | 65 | static int |
| 66 | __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset) |
| 67 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 68 | swp_entry_t entry = swp_entry(si->type, offset); |
KAMEZAWA Hiroyuki | c9e4441 | 2009-06-16 15:32:54 -0700 | [diff] [blame] | 69 | struct page *page; |
| 70 | int ret = 0; |
| 71 | |
| 72 | page = find_get_page(&swapper_space, entry.val); |
| 73 | if (!page) |
| 74 | return 0; |
| 75 | /* |
| 76 | * This function is called from scan_swap_map() and it's called |
| 77 | * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here. |
| 78 | * We have to use trylock for avoiding deadlock. This is a special |
| 79 | * case and you should use try_to_free_swap() with explicit lock_page() |
| 80 | * in usual operations. |
| 81 | */ |
| 82 | if (trylock_page(page)) { |
| 83 | ret = try_to_free_swap(page); |
| 84 | unlock_page(page); |
| 85 | } |
| 86 | page_cache_release(page); |
| 87 | return ret; |
| 88 | } |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | /* |
| 91 | * We need this because the bdev->unplug_fn can sleep and we cannot |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 92 | * hold swap_lock while calling the unplug_fn. And swap_lock |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 93 | * cannot be turned into a mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | */ |
| 95 | static DECLARE_RWSEM(swap_unplug_sem); |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page) |
| 98 | { |
| 99 | swp_entry_t entry; |
| 100 | |
| 101 | down_read(&swap_unplug_sem); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 102 | entry.val = page_private(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (PageSwapCache(page)) { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 104 | struct block_device *bdev = swap_info[swp_type(entry)]->bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | struct backing_dev_info *bdi; |
| 106 | |
| 107 | /* |
| 108 | * If the page is removed from swapcache from under us (with a |
| 109 | * racy try_to_unuse/swapoff) we need an additional reference |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 110 | * count to avoid reading garbage from page_private(page) above. |
| 111 | * If the WARN_ON triggers during a swapoff it maybe the race |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | * condition and it's harmless. However if it triggers without |
| 113 | * swapoff it signals a problem. |
| 114 | */ |
| 115 | WARN_ON(page_count(page) <= 1); |
| 116 | |
| 117 | bdi = bdev->bd_inode->i_mapping->backing_dev_info; |
McMullan, Jason | ba32311 | 2005-05-16 21:53:40 -0700 | [diff] [blame] | 118 | blk_run_backing_dev(bdi, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | up_read(&swap_unplug_sem); |
| 121 | } |
| 122 | |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 123 | /* |
| 124 | * swapon tell device that all the old swap contents can be discarded, |
| 125 | * to allow the swap device to optimize its wear-levelling. |
| 126 | */ |
| 127 | static int discard_swap(struct swap_info_struct *si) |
| 128 | { |
| 129 | struct swap_extent *se; |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 130 | sector_t start_block; |
| 131 | sector_t nr_blocks; |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 132 | int err = 0; |
| 133 | |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 134 | /* Do not discard the swap header page! */ |
| 135 | se = &si->first_swap_extent; |
| 136 | start_block = (se->start_block + 1) << (PAGE_SHIFT - 9); |
| 137 | nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9); |
| 138 | if (nr_blocks) { |
| 139 | err = blkdev_issue_discard(si->bdev, start_block, |
| 140 | nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER); |
| 141 | if (err) |
| 142 | return err; |
| 143 | cond_resched(); |
| 144 | } |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 145 | |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 146 | list_for_each_entry(se, &si->first_swap_extent.list, list) { |
| 147 | start_block = se->start_block << (PAGE_SHIFT - 9); |
| 148 | nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9); |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 149 | |
| 150 | err = blkdev_issue_discard(si->bdev, start_block, |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 151 | nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER); |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 152 | if (err) |
| 153 | break; |
| 154 | |
| 155 | cond_resched(); |
| 156 | } |
| 157 | return err; /* That will often be -EOPNOTSUPP */ |
| 158 | } |
| 159 | |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 160 | /* |
| 161 | * swap allocation tell device that a cluster of swap can now be discarded, |
| 162 | * to allow the swap device to optimize its wear-levelling. |
| 163 | */ |
| 164 | static void discard_swap_cluster(struct swap_info_struct *si, |
| 165 | pgoff_t start_page, pgoff_t nr_pages) |
| 166 | { |
| 167 | struct swap_extent *se = si->curr_swap_extent; |
| 168 | int found_extent = 0; |
| 169 | |
| 170 | while (nr_pages) { |
| 171 | struct list_head *lh; |
| 172 | |
| 173 | if (se->start_page <= start_page && |
| 174 | start_page < se->start_page + se->nr_pages) { |
| 175 | pgoff_t offset = start_page - se->start_page; |
| 176 | sector_t start_block = se->start_block + offset; |
Hugh Dickins | 858a2990 | 2009-01-06 14:39:56 -0800 | [diff] [blame] | 177 | sector_t nr_blocks = se->nr_pages - offset; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 178 | |
| 179 | if (nr_blocks > nr_pages) |
| 180 | nr_blocks = nr_pages; |
| 181 | start_page += nr_blocks; |
| 182 | nr_pages -= nr_blocks; |
| 183 | |
| 184 | if (!found_extent++) |
| 185 | si->curr_swap_extent = se; |
| 186 | |
| 187 | start_block <<= PAGE_SHIFT - 9; |
| 188 | nr_blocks <<= PAGE_SHIFT - 9; |
| 189 | if (blkdev_issue_discard(si->bdev, start_block, |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 190 | nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER)) |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | |
| 194 | lh = se->list.next; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 195 | se = list_entry(lh, struct swap_extent, list); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static int wait_for_discard(void *word) |
| 200 | { |
| 201 | schedule(); |
| 202 | return 0; |
| 203 | } |
| 204 | |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 205 | #define SWAPFILE_CLUSTER 256 |
| 206 | #define LATENCY_LIMIT 256 |
| 207 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 208 | static inline unsigned long scan_swap_map(struct swap_info_struct *si, |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 209 | unsigned char usage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 211 | unsigned long offset; |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 212 | unsigned long scan_base; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 213 | unsigned long last_in_cluster = 0; |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 214 | int latency_ration = LATENCY_LIMIT; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 215 | int found_free_cluster = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 217 | /* |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 218 | * We try to cluster swap pages by allocating them sequentially |
| 219 | * in swap. Once we've allocated SWAPFILE_CLUSTER pages this |
| 220 | * way, however, we resort to first-free allocation, starting |
| 221 | * a new cluster. This prevents us from scattering swap pages |
| 222 | * all over the entire swap partition, so that we reduce |
| 223 | * overall disk seek times between swap pages. -- sct |
| 224 | * But we do now try to find an empty cluster. -Andrea |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 225 | * And we let swap pages go all over an SSD partition. Hugh |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 226 | */ |
| 227 | |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 228 | si->flags += SWP_SCANNING; |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 229 | scan_base = offset = si->cluster_next; |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 230 | |
| 231 | if (unlikely(!si->cluster_nr--)) { |
| 232 | if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) { |
| 233 | si->cluster_nr = SWAPFILE_CLUSTER - 1; |
| 234 | goto checks; |
| 235 | } |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 236 | if (si->flags & SWP_DISCARDABLE) { |
| 237 | /* |
| 238 | * Start range check on racing allocations, in case |
| 239 | * they overlap the cluster we eventually decide on |
| 240 | * (we scan without swap_lock to allow preemption). |
| 241 | * It's hardly conceivable that cluster_nr could be |
| 242 | * wrapped during our scan, but don't depend on it. |
| 243 | */ |
| 244 | if (si->lowest_alloc) |
| 245 | goto checks; |
| 246 | si->lowest_alloc = si->max; |
| 247 | si->highest_alloc = 0; |
| 248 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 249 | spin_unlock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 250 | |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 251 | /* |
| 252 | * If seek is expensive, start searching for new cluster from |
| 253 | * start of partition, to minimize the span of allocated swap. |
| 254 | * But if seek is cheap, search from our current position, so |
| 255 | * that swap is allocated from all over the partition: if the |
| 256 | * Flash Translation Layer only remaps within limited zones, |
| 257 | * we don't want to wear out the first zone too quickly. |
| 258 | */ |
| 259 | if (!(si->flags & SWP_SOLIDSTATE)) |
| 260 | scan_base = offset = si->lowest_bit; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 261 | last_in_cluster = offset + SWAPFILE_CLUSTER - 1; |
| 262 | |
| 263 | /* Locate the first empty (unaligned) cluster */ |
| 264 | for (; last_in_cluster <= si->highest_bit; offset++) { |
| 265 | if (si->swap_map[offset]) |
| 266 | last_in_cluster = offset + SWAPFILE_CLUSTER; |
| 267 | else if (offset == last_in_cluster) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 268 | spin_lock(&swap_lock); |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 269 | offset -= SWAPFILE_CLUSTER - 1; |
| 270 | si->cluster_next = offset; |
| 271 | si->cluster_nr = SWAPFILE_CLUSTER - 1; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 272 | found_free_cluster = 1; |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 273 | goto checks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 275 | if (unlikely(--latency_ration < 0)) { |
| 276 | cond_resched(); |
| 277 | latency_ration = LATENCY_LIMIT; |
| 278 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 279 | } |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 280 | |
| 281 | offset = si->lowest_bit; |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 282 | last_in_cluster = offset + SWAPFILE_CLUSTER - 1; |
| 283 | |
| 284 | /* Locate the first empty (unaligned) cluster */ |
| 285 | for (; last_in_cluster < scan_base; offset++) { |
| 286 | if (si->swap_map[offset]) |
| 287 | last_in_cluster = offset + SWAPFILE_CLUSTER; |
| 288 | else if (offset == last_in_cluster) { |
| 289 | spin_lock(&swap_lock); |
| 290 | offset -= SWAPFILE_CLUSTER - 1; |
| 291 | si->cluster_next = offset; |
| 292 | si->cluster_nr = SWAPFILE_CLUSTER - 1; |
| 293 | found_free_cluster = 1; |
| 294 | goto checks; |
| 295 | } |
| 296 | if (unlikely(--latency_ration < 0)) { |
| 297 | cond_resched(); |
| 298 | latency_ration = LATENCY_LIMIT; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | offset = scan_base; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 303 | spin_lock(&swap_lock); |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 304 | si->cluster_nr = SWAPFILE_CLUSTER - 1; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 305 | si->lowest_alloc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 307 | |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 308 | checks: |
| 309 | if (!(si->flags & SWP_WRITEOK)) |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 310 | goto no_page; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 311 | if (!si->highest_bit) |
| 312 | goto no_page; |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 313 | if (offset > si->highest_bit) |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 314 | scan_base = offset = si->lowest_bit; |
KAMEZAWA Hiroyuki | c9e4441 | 2009-06-16 15:32:54 -0700 | [diff] [blame] | 315 | |
| 316 | /* reuse swap entry of cache-only swap if not busy. */ |
| 317 | if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { |
| 318 | int swap_was_freed; |
| 319 | spin_unlock(&swap_lock); |
| 320 | swap_was_freed = __try_to_reclaim_swap(si, offset); |
| 321 | spin_lock(&swap_lock); |
| 322 | /* entry was freed successfully, try to use this again */ |
| 323 | if (swap_was_freed) |
| 324 | goto checks; |
| 325 | goto scan; /* check next one */ |
| 326 | } |
| 327 | |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 328 | if (si->swap_map[offset]) |
| 329 | goto scan; |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 330 | |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 331 | if (offset == si->lowest_bit) |
| 332 | si->lowest_bit++; |
| 333 | if (offset == si->highest_bit) |
| 334 | si->highest_bit--; |
| 335 | si->inuse_pages++; |
| 336 | if (si->inuse_pages == si->pages) { |
| 337 | si->lowest_bit = si->max; |
| 338 | si->highest_bit = 0; |
| 339 | } |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 340 | si->swap_map[offset] = usage; |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 341 | si->cluster_next = offset + 1; |
| 342 | si->flags -= SWP_SCANNING; |
Hugh Dickins | 7992fde | 2009-01-06 14:39:53 -0800 | [diff] [blame] | 343 | |
| 344 | if (si->lowest_alloc) { |
| 345 | /* |
| 346 | * Only set when SWP_DISCARDABLE, and there's a scan |
| 347 | * for a free cluster in progress or just completed. |
| 348 | */ |
| 349 | if (found_free_cluster) { |
| 350 | /* |
| 351 | * To optimize wear-levelling, discard the |
| 352 | * old data of the cluster, taking care not to |
| 353 | * discard any of its pages that have already |
| 354 | * been allocated by racing tasks (offset has |
| 355 | * already stepped over any at the beginning). |
| 356 | */ |
| 357 | if (offset < si->highest_alloc && |
| 358 | si->lowest_alloc <= last_in_cluster) |
| 359 | last_in_cluster = si->lowest_alloc - 1; |
| 360 | si->flags |= SWP_DISCARDING; |
| 361 | spin_unlock(&swap_lock); |
| 362 | |
| 363 | if (offset < last_in_cluster) |
| 364 | discard_swap_cluster(si, offset, |
| 365 | last_in_cluster - offset + 1); |
| 366 | |
| 367 | spin_lock(&swap_lock); |
| 368 | si->lowest_alloc = 0; |
| 369 | si->flags &= ~SWP_DISCARDING; |
| 370 | |
| 371 | smp_mb(); /* wake_up_bit advises this */ |
| 372 | wake_up_bit(&si->flags, ilog2(SWP_DISCARDING)); |
| 373 | |
| 374 | } else if (si->flags & SWP_DISCARDING) { |
| 375 | /* |
| 376 | * Delay using pages allocated by racing tasks |
| 377 | * until the whole discard has been issued. We |
| 378 | * could defer that delay until swap_writepage, |
| 379 | * but it's easier to keep this self-contained. |
| 380 | */ |
| 381 | spin_unlock(&swap_lock); |
| 382 | wait_on_bit(&si->flags, ilog2(SWP_DISCARDING), |
| 383 | wait_for_discard, TASK_UNINTERRUPTIBLE); |
| 384 | spin_lock(&swap_lock); |
| 385 | } else { |
| 386 | /* |
| 387 | * Note pages allocated by racing tasks while |
| 388 | * scan for a free cluster is in progress, so |
| 389 | * that its final discard can exclude them. |
| 390 | */ |
| 391 | if (offset < si->lowest_alloc) |
| 392 | si->lowest_alloc = offset; |
| 393 | if (offset > si->highest_alloc) |
| 394 | si->highest_alloc = offset; |
| 395 | } |
| 396 | } |
Hugh Dickins | ebebbbe | 2009-01-06 14:39:50 -0800 | [diff] [blame] | 397 | return offset; |
| 398 | |
| 399 | scan: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 400 | spin_unlock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 401 | while (++offset <= si->highest_bit) { |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 402 | if (!si->swap_map[offset]) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 403 | spin_lock(&swap_lock); |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 404 | goto checks; |
| 405 | } |
KAMEZAWA Hiroyuki | c9e4441 | 2009-06-16 15:32:54 -0700 | [diff] [blame] | 406 | if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { |
| 407 | spin_lock(&swap_lock); |
| 408 | goto checks; |
| 409 | } |
Hugh Dickins | 048c27f | 2005-09-03 15:54:40 -0700 | [diff] [blame] | 410 | if (unlikely(--latency_ration < 0)) { |
| 411 | cond_resched(); |
| 412 | latency_ration = LATENCY_LIMIT; |
| 413 | } |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 414 | } |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 415 | offset = si->lowest_bit; |
| 416 | while (++offset < scan_base) { |
| 417 | if (!si->swap_map[offset]) { |
| 418 | spin_lock(&swap_lock); |
| 419 | goto checks; |
| 420 | } |
KAMEZAWA Hiroyuki | c9e4441 | 2009-06-16 15:32:54 -0700 | [diff] [blame] | 421 | if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) { |
| 422 | spin_lock(&swap_lock); |
| 423 | goto checks; |
| 424 | } |
Hugh Dickins | c60aa17 | 2009-01-06 14:39:55 -0800 | [diff] [blame] | 425 | if (unlikely(--latency_ration < 0)) { |
| 426 | cond_resched(); |
| 427 | latency_ration = LATENCY_LIMIT; |
| 428 | } |
| 429 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 430 | spin_lock(&swap_lock); |
Hugh Dickins | 7dfad41 | 2005-09-03 15:54:38 -0700 | [diff] [blame] | 431 | |
| 432 | no_page: |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 433 | si->flags -= SWP_SCANNING; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | swp_entry_t get_swap_page(void) |
| 438 | { |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 439 | struct swap_info_struct *si; |
| 440 | pgoff_t offset; |
| 441 | int type, next; |
| 442 | int wrapped = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 444 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (nr_swap_pages <= 0) |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 446 | goto noswap; |
| 447 | nr_swap_pages--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 449 | for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 450 | si = swap_info[type]; |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 451 | next = si->next; |
| 452 | if (next < 0 || |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 453 | (!wrapped && si->prio != swap_info[next]->prio)) { |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 454 | next = swap_list.head; |
| 455 | wrapped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 457 | |
| 458 | if (!si->highest_bit) |
| 459 | continue; |
| 460 | if (!(si->flags & SWP_WRITEOK)) |
| 461 | continue; |
| 462 | |
| 463 | swap_list.next = next; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 464 | /* This is called for allocating swap entry for cache */ |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 465 | offset = scan_swap_map(si, SWAP_HAS_CACHE); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 466 | if (offset) { |
| 467 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 468 | return swp_entry(type, offset); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 469 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 470 | next = swap_list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 472 | |
| 473 | nr_swap_pages++; |
| 474 | noswap: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 475 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 476 | return (swp_entry_t) {0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 479 | /* The only caller of this function is now susupend routine */ |
Rafael J. Wysocki | 3a291a2 | 2006-01-06 00:16:37 -0800 | [diff] [blame] | 480 | swp_entry_t get_swap_page_of_type(int type) |
| 481 | { |
| 482 | struct swap_info_struct *si; |
| 483 | pgoff_t offset; |
| 484 | |
| 485 | spin_lock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 486 | si = swap_info[type]; |
| 487 | if (si && (si->flags & SWP_WRITEOK)) { |
Rafael J. Wysocki | 3a291a2 | 2006-01-06 00:16:37 -0800 | [diff] [blame] | 488 | nr_swap_pages--; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 489 | /* This is called for allocating swap entry, not cache */ |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 490 | offset = scan_swap_map(si, 1); |
Rafael J. Wysocki | 3a291a2 | 2006-01-06 00:16:37 -0800 | [diff] [blame] | 491 | if (offset) { |
| 492 | spin_unlock(&swap_lock); |
| 493 | return swp_entry(type, offset); |
| 494 | } |
| 495 | nr_swap_pages++; |
| 496 | } |
| 497 | spin_unlock(&swap_lock); |
| 498 | return (swp_entry_t) {0}; |
| 499 | } |
| 500 | |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 501 | static struct swap_info_struct *swap_info_get(swp_entry_t entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 503 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | unsigned long offset, type; |
| 505 | |
| 506 | if (!entry.val) |
| 507 | goto out; |
| 508 | type = swp_type(entry); |
| 509 | if (type >= nr_swapfiles) |
| 510 | goto bad_nofile; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 511 | p = swap_info[type]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (!(p->flags & SWP_USED)) |
| 513 | goto bad_device; |
| 514 | offset = swp_offset(entry); |
| 515 | if (offset >= p->max) |
| 516 | goto bad_offset; |
| 517 | if (!p->swap_map[offset]) |
| 518 | goto bad_free; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 519 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return p; |
| 521 | |
| 522 | bad_free: |
| 523 | printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val); |
| 524 | goto out; |
| 525 | bad_offset: |
| 526 | printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val); |
| 527 | goto out; |
| 528 | bad_device: |
| 529 | printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val); |
| 530 | goto out; |
| 531 | bad_nofile: |
| 532 | printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val); |
| 533 | out: |
| 534 | return NULL; |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 537 | static unsigned char swap_entry_free(struct swap_info_struct *p, |
| 538 | swp_entry_t entry, unsigned char usage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 540 | unsigned long offset = swp_offset(entry); |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 541 | unsigned char count; |
| 542 | unsigned char has_cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 544 | count = p->swap_map[offset]; |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 545 | has_cache = count & SWAP_HAS_CACHE; |
| 546 | count &= ~SWAP_HAS_CACHE; |
| 547 | |
| 548 | if (usage == SWAP_HAS_CACHE) { |
| 549 | VM_BUG_ON(!has_cache); |
| 550 | has_cache = 0; |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 551 | } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) { |
| 552 | if (count == COUNT_CONTINUED) { |
| 553 | if (swap_count_continued(p, offset, count)) |
| 554 | count = SWAP_MAP_MAX | COUNT_CONTINUED; |
| 555 | else |
| 556 | count = SWAP_MAP_MAX; |
| 557 | } else |
| 558 | count--; |
| 559 | } |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 560 | |
| 561 | if (!count) |
| 562 | mem_cgroup_uncharge_swap(entry); |
| 563 | |
| 564 | usage = count | has_cache; |
| 565 | p->swap_map[offset] = usage; |
| 566 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 567 | /* free if no reference */ |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 568 | if (!usage) { |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 569 | if (offset < p->lowest_bit) |
| 570 | p->lowest_bit = offset; |
| 571 | if (offset > p->highest_bit) |
| 572 | p->highest_bit = offset; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 573 | if (swap_list.next >= 0 && |
| 574 | p->prio > swap_info[swap_list.next]->prio) |
| 575 | swap_list.next = p->type; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 576 | nr_swap_pages++; |
| 577 | p->inuse_pages--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | } |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 579 | |
| 580 | return usage; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Caller has made sure that the swapdevice corresponding to entry |
| 585 | * is still around or has not been recycled. |
| 586 | */ |
| 587 | void swap_free(swp_entry_t entry) |
| 588 | { |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 589 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | |
| 591 | p = swap_info_get(entry); |
| 592 | if (p) { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 593 | swap_entry_free(p, entry, 1); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 594 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
| 598 | /* |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 599 | * Called after dropping swapcache to decrease refcnt to swap entries. |
| 600 | */ |
| 601 | void swapcache_free(swp_entry_t entry, struct page *page) |
| 602 | { |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 603 | struct swap_info_struct *p; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 604 | unsigned char count; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 605 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 606 | p = swap_info_get(entry); |
| 607 | if (p) { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 608 | count = swap_entry_free(p, entry, SWAP_HAS_CACHE); |
| 609 | if (page) |
| 610 | mem_cgroup_uncharge_swapcache(page, entry, count != 0); |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 611 | spin_unlock(&swap_lock); |
| 612 | } |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 616 | * How many references to page are currently swapped out? |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 617 | * This does not give an exact answer when swap count is continued, |
| 618 | * but does include the high COUNT_CONTINUED flag to allow for that. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | */ |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 620 | static inline int page_swapcount(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 622 | int count = 0; |
| 623 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | swp_entry_t entry; |
| 625 | |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 626 | entry.val = page_private(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | p = swap_info_get(entry); |
| 628 | if (p) { |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 629 | count = swap_count(p->swap_map[swp_offset(entry)]); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 630 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 632 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* |
Hugh Dickins | 7b1fe59 | 2009-01-06 14:39:34 -0800 | [diff] [blame] | 636 | * We can write to an anon page without COW if there are no other references |
| 637 | * to it. And as a side-effect, free up its swap: because the old content |
| 638 | * on disk will never be read, and seeking back there to write new content |
| 639 | * later would only waste time away from clustering. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | */ |
Hugh Dickins | 7b1fe59 | 2009-01-06 14:39:34 -0800 | [diff] [blame] | 641 | int reuse_swap_page(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 643 | int count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 645 | VM_BUG_ON(!PageLocked(page)); |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 646 | count = page_mapcount(page); |
Hugh Dickins | 7b1fe59 | 2009-01-06 14:39:34 -0800 | [diff] [blame] | 647 | if (count <= 1 && PageSwapCache(page)) { |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 648 | count += page_swapcount(page); |
Hugh Dickins | 7b1fe59 | 2009-01-06 14:39:34 -0800 | [diff] [blame] | 649 | if (count == 1 && !PageWriteback(page)) { |
| 650 | delete_from_swap_cache(page); |
| 651 | SetPageDirty(page); |
| 652 | } |
| 653 | } |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 654 | return count == 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 658 | * If swap is getting full, or if there are no more mappings of this page, |
| 659 | * then try_to_free_swap is called to free its swap space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | */ |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 661 | int try_to_free_swap(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
Hugh Dickins | 51726b1 | 2009-01-06 14:39:25 -0800 | [diff] [blame] | 663 | VM_BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
| 665 | if (!PageSwapCache(page)) |
| 666 | return 0; |
| 667 | if (PageWriteback(page)) |
| 668 | return 0; |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 669 | if (page_swapcount(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 672 | delete_from_swap_cache(page); |
| 673 | SetPageDirty(page); |
| 674 | return 1; |
Rik van Riel | 68a22394 | 2008-10-18 20:26:23 -0700 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | * Free the swap entry like above, but also try to |
| 679 | * free the page cache entry if it is the last user. |
| 680 | */ |
Hugh Dickins | 2509ef2 | 2009-01-06 14:40:10 -0800 | [diff] [blame] | 681 | int free_swap_and_cache(swp_entry_t entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
Hugh Dickins | 2509ef2 | 2009-01-06 14:40:10 -0800 | [diff] [blame] | 683 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | struct page *page = NULL; |
| 685 | |
Andi Kleen | a7420aa | 2009-09-16 11:50:05 +0200 | [diff] [blame] | 686 | if (non_swap_entry(entry)) |
Hugh Dickins | 2509ef2 | 2009-01-06 14:40:10 -0800 | [diff] [blame] | 687 | return 1; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | p = swap_info_get(entry); |
| 690 | if (p) { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 691 | if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) { |
Nick Piggin | 93fac70 | 2006-03-31 02:29:56 -0800 | [diff] [blame] | 692 | page = find_get_page(&swapper_space, entry.val); |
Nick Piggin | 8413ac9 | 2008-10-18 20:26:59 -0700 | [diff] [blame] | 693 | if (page && !trylock_page(page)) { |
Nick Piggin | 93fac70 | 2006-03-31 02:29:56 -0800 | [diff] [blame] | 694 | page_cache_release(page); |
| 695 | page = NULL; |
| 696 | } |
| 697 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 698 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | } |
| 700 | if (page) { |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 701 | /* |
| 702 | * Not mapped elsewhere, or swap space full? Free it! |
| 703 | * Also recheck PageSwapCache now page is locked (above). |
| 704 | */ |
Nick Piggin | 93fac70 | 2006-03-31 02:29:56 -0800 | [diff] [blame] | 705 | if (PageSwapCache(page) && !PageWriteback(page) && |
Hugh Dickins | a2c43ee | 2009-01-06 14:39:36 -0800 | [diff] [blame] | 706 | (!page_mapped(page) || vm_swap_full())) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | delete_from_swap_cache(page); |
| 708 | SetPageDirty(page); |
| 709 | } |
| 710 | unlock_page(page); |
| 711 | page_cache_release(page); |
| 712 | } |
Hugh Dickins | 2509ef2 | 2009-01-06 14:40:10 -0800 | [diff] [blame] | 713 | return p != NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 716 | #ifdef CONFIG_HIBERNATION |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 717 | /* |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 718 | * Find the swap type that corresponds to given device (if any). |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 719 | * |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 720 | * @offset - number of the PAGE_SIZE-sized block of the device, starting |
| 721 | * from 0, in which the swap header is expected to be located. |
| 722 | * |
| 723 | * This is needed for the suspend to disk (aka swsusp). |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 724 | */ |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 725 | int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 726 | { |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 727 | struct block_device *bdev = NULL; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 728 | int type; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 729 | |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 730 | if (device) |
| 731 | bdev = bdget(device); |
| 732 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 733 | spin_lock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 734 | for (type = 0; type < nr_swapfiles; type++) { |
| 735 | struct swap_info_struct *sis = swap_info[type]; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 736 | |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 737 | if (!(sis->flags & SWP_WRITEOK)) |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 738 | continue; |
Rafael J. Wysocki | b6b5bce | 2006-08-27 01:23:25 -0700 | [diff] [blame] | 739 | |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 740 | if (!bdev) { |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 741 | if (bdev_p) |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 742 | *bdev_p = bdgrab(sis->bdev); |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 743 | |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 744 | spin_unlock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 745 | return type; |
Rafael J. Wysocki | 6e1819d | 2006-03-23 03:00:03 -0800 | [diff] [blame] | 746 | } |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 747 | if (bdev == sis->bdev) { |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 748 | struct swap_extent *se = &sis->first_swap_extent; |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 749 | |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 750 | if (se->start_block == offset) { |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 751 | if (bdev_p) |
Alan Jenkins | dddac6a | 2009-07-29 21:07:55 +0200 | [diff] [blame] | 752 | *bdev_p = bdgrab(sis->bdev); |
Rafael J. Wysocki | 7bf2368 | 2007-01-05 16:36:28 -0800 | [diff] [blame] | 753 | |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 754 | spin_unlock(&swap_lock); |
| 755 | bdput(bdev); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 756 | return type; |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 757 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | spin_unlock(&swap_lock); |
Rafael J. Wysocki | 915bae9 | 2006-12-06 20:34:07 -0800 | [diff] [blame] | 761 | if (bdev) |
| 762 | bdput(bdev); |
| 763 | |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 764 | return -ENODEV; |
| 765 | } |
| 766 | |
| 767 | /* |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 768 | * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev |
| 769 | * corresponding to given index in swap_info (swap type). |
| 770 | */ |
| 771 | sector_t swapdev_block(int type, pgoff_t offset) |
| 772 | { |
| 773 | struct block_device *bdev; |
| 774 | |
| 775 | if ((unsigned int)type >= nr_swapfiles) |
| 776 | return 0; |
| 777 | if (!(swap_info[type]->flags & SWP_WRITEOK)) |
| 778 | return 0; |
| 779 | return map_swap_page(swp_entry(type, offset), &bdev); |
| 780 | } |
| 781 | |
| 782 | /* |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 783 | * Return either the total number of swap pages of given type, or the number |
| 784 | * of free pages of that type (depending on @free) |
| 785 | * |
| 786 | * This is needed for software suspend |
| 787 | */ |
| 788 | unsigned int count_swap_pages(int type, int free) |
| 789 | { |
| 790 | unsigned int n = 0; |
| 791 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 792 | spin_lock(&swap_lock); |
| 793 | if ((unsigned int)type < nr_swapfiles) { |
| 794 | struct swap_info_struct *sis = swap_info[type]; |
| 795 | |
| 796 | if (sis->flags & SWP_WRITEOK) { |
| 797 | n = sis->pages; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 798 | if (free) |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 799 | n -= sis->inuse_pages; |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 800 | } |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 801 | } |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 802 | spin_unlock(&swap_lock); |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 803 | return n; |
| 804 | } |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 805 | #endif /* CONFIG_HIBERNATION */ |
Rafael J. Wysocki | f577eb3 | 2006-03-23 02:59:59 -0800 | [diff] [blame] | 806 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | /* |
Hugh Dickins | 72866f6 | 2005-10-29 18:15:55 -0700 | [diff] [blame] | 808 | * No need to decide whether this PTE shares the swap entry with others, |
| 809 | * just let do_wp_page work it out if a write is requested later - to |
| 810 | * force COW, vm_page_prot omits write permission from any private vma. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | */ |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 812 | static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | unsigned long addr, swp_entry_t entry, struct page *page) |
| 814 | { |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 815 | struct mem_cgroup *ptr = NULL; |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 816 | spinlock_t *ptl; |
| 817 | pte_t *pte; |
| 818 | int ret = 1; |
| 819 | |
KAMEZAWA Hiroyuki | 85d9fc8 | 2009-01-29 14:25:13 -0800 | [diff] [blame] | 820 | if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) { |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 821 | ret = -ENOMEM; |
KAMEZAWA Hiroyuki | 85d9fc8 | 2009-01-29 14:25:13 -0800 | [diff] [blame] | 822 | goto out_nolock; |
| 823 | } |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 824 | |
| 825 | pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl); |
| 826 | if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) { |
| 827 | if (ret > 0) |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 828 | mem_cgroup_cancel_charge_swapin(ptr); |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 829 | ret = 0; |
| 830 | goto out; |
| 831 | } |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 832 | |
Hugh Dickins | 4294621 | 2005-10-29 18:16:05 -0700 | [diff] [blame] | 833 | inc_mm_counter(vma->vm_mm, anon_rss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | get_page(page); |
| 835 | set_pte_at(vma->vm_mm, addr, pte, |
| 836 | pte_mkold(mk_pte(page, vma->vm_page_prot))); |
| 837 | page_add_anon_rmap(page, vma, addr); |
KAMEZAWA Hiroyuki | 7a81b88 | 2009-01-07 18:07:48 -0800 | [diff] [blame] | 838 | mem_cgroup_commit_charge_swapin(page, ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | swap_free(entry); |
| 840 | /* |
| 841 | * Move the page to the active list so it is not |
| 842 | * immediately swapped out again after swapon. |
| 843 | */ |
| 844 | activate_page(page); |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 845 | out: |
| 846 | pte_unmap_unlock(pte, ptl); |
KAMEZAWA Hiroyuki | 85d9fc8 | 2009-01-29 14:25:13 -0800 | [diff] [blame] | 847 | out_nolock: |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 848 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, |
| 852 | unsigned long addr, unsigned long end, |
| 853 | swp_entry_t entry, struct page *page) |
| 854 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | pte_t swp_pte = swp_entry_to_pte(entry); |
Hugh Dickins | 705e87c | 2005-10-29 18:16:27 -0700 | [diff] [blame] | 856 | pte_t *pte; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 857 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 859 | /* |
| 860 | * We don't actually need pte lock while scanning for swp_pte: since |
| 861 | * we hold page lock and mmap_sem, swp_pte cannot be inserted into the |
| 862 | * page table while we're scanning; though it could get zapped, and on |
| 863 | * some architectures (e.g. x86_32 with PAE) we might catch a glimpse |
| 864 | * of unmatched parts which look like swp_pte, so unuse_pte must |
| 865 | * recheck under pte lock. Scanning without pte lock lets it be |
| 866 | * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE. |
| 867 | */ |
| 868 | pte = pte_offset_map(pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | do { |
| 870 | /* |
| 871 | * swapoff spends a _lot_ of time in this loop! |
| 872 | * Test inline before going to call unuse_pte. |
| 873 | */ |
| 874 | if (unlikely(pte_same(*pte, swp_pte))) { |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 875 | pte_unmap(pte); |
| 876 | ret = unuse_pte(vma, pmd, addr, entry, page); |
| 877 | if (ret) |
| 878 | goto out; |
| 879 | pte = pte_offset_map(pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
| 881 | } while (pte++, addr += PAGE_SIZE, addr != end); |
Hugh Dickins | 044d66c | 2008-02-07 00:14:04 -0800 | [diff] [blame] | 882 | pte_unmap(pte - 1); |
| 883 | out: |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 884 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud, |
| 888 | unsigned long addr, unsigned long end, |
| 889 | swp_entry_t entry, struct page *page) |
| 890 | { |
| 891 | pmd_t *pmd; |
| 892 | unsigned long next; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 893 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | pmd = pmd_offset(pud, addr); |
| 896 | do { |
| 897 | next = pmd_addr_end(addr, end); |
| 898 | if (pmd_none_or_clear_bad(pmd)) |
| 899 | continue; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 900 | ret = unuse_pte_range(vma, pmd, addr, next, entry, page); |
| 901 | if (ret) |
| 902 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } while (pmd++, addr = next, addr != end); |
| 904 | return 0; |
| 905 | } |
| 906 | |
| 907 | static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd, |
| 908 | unsigned long addr, unsigned long end, |
| 909 | swp_entry_t entry, struct page *page) |
| 910 | { |
| 911 | pud_t *pud; |
| 912 | unsigned long next; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 913 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | pud = pud_offset(pgd, addr); |
| 916 | do { |
| 917 | next = pud_addr_end(addr, end); |
| 918 | if (pud_none_or_clear_bad(pud)) |
| 919 | continue; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 920 | ret = unuse_pmd_range(vma, pud, addr, next, entry, page); |
| 921 | if (ret) |
| 922 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } while (pud++, addr = next, addr != end); |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static int unuse_vma(struct vm_area_struct *vma, |
| 928 | swp_entry_t entry, struct page *page) |
| 929 | { |
| 930 | pgd_t *pgd; |
| 931 | unsigned long addr, end, next; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 932 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | |
| 934 | if (page->mapping) { |
| 935 | addr = page_address_in_vma(page, vma); |
| 936 | if (addr == -EFAULT) |
| 937 | return 0; |
| 938 | else |
| 939 | end = addr + PAGE_SIZE; |
| 940 | } else { |
| 941 | addr = vma->vm_start; |
| 942 | end = vma->vm_end; |
| 943 | } |
| 944 | |
| 945 | pgd = pgd_offset(vma->vm_mm, addr); |
| 946 | do { |
| 947 | next = pgd_addr_end(addr, end); |
| 948 | if (pgd_none_or_clear_bad(pgd)) |
| 949 | continue; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 950 | ret = unuse_pud_range(vma, pgd, addr, next, entry, page); |
| 951 | if (ret) |
| 952 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } while (pgd++, addr = next, addr != end); |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | static int unuse_mm(struct mm_struct *mm, |
| 958 | swp_entry_t entry, struct page *page) |
| 959 | { |
| 960 | struct vm_area_struct *vma; |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 961 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
| 963 | if (!down_read_trylock(&mm->mmap_sem)) { |
| 964 | /* |
Fernando Luis Vazquez Cao | 7d03431 | 2008-07-29 22:33:41 -0700 | [diff] [blame] | 965 | * Activate page so shrink_inactive_list is unlikely to unmap |
| 966 | * its ptes while lock is dropped, so swapoff can make progress. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
Hugh Dickins | c475a8a | 2005-06-21 17:15:12 -0700 | [diff] [blame] | 968 | activate_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | unlock_page(page); |
| 970 | down_read(&mm->mmap_sem); |
| 971 | lock_page(page); |
| 972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 974 | if (vma->anon_vma && (ret = unuse_vma(vma, entry, page))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | break; |
| 976 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | up_read(&mm->mmap_sem); |
Balbir Singh | 8a9f3cc | 2008-02-07 00:13:53 -0800 | [diff] [blame] | 978 | return (ret < 0)? ret: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /* |
| 982 | * Scan swap_map from current position to next entry still in use. |
| 983 | * Recycle to start on reaching the end, returning 0 when empty. |
| 984 | */ |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 985 | static unsigned int find_next_to_unuse(struct swap_info_struct *si, |
| 986 | unsigned int prev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 988 | unsigned int max = si->max; |
| 989 | unsigned int i = prev; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 990 | unsigned char count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
| 992 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 993 | * No need for swap_lock here: we're just looking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | * for whether an entry is in use, not modifying it; false |
| 995 | * hits are okay, and sys_swapoff() has already prevented new |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 996 | * allocations from this area (while holding swap_lock). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | */ |
| 998 | for (;;) { |
| 999 | if (++i >= max) { |
| 1000 | if (!prev) { |
| 1001 | i = 0; |
| 1002 | break; |
| 1003 | } |
| 1004 | /* |
| 1005 | * No entries in use at top of swap_map, |
| 1006 | * loop back to start and recheck there. |
| 1007 | */ |
| 1008 | max = prev + 1; |
| 1009 | prev = 0; |
| 1010 | i = 1; |
| 1011 | } |
| 1012 | count = si->swap_map[i]; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1013 | if (count && swap_count(count) != SWAP_MAP_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | break; |
| 1015 | } |
| 1016 | return i; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * We completely avoid races by reading each swap page in advance, |
| 1021 | * and then search for the process using it. All the necessary |
| 1022 | * page table adjustments can then be made atomically. |
| 1023 | */ |
| 1024 | static int try_to_unuse(unsigned int type) |
| 1025 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1026 | struct swap_info_struct *si = swap_info[type]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | struct mm_struct *start_mm; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 1028 | unsigned char *swap_map; |
| 1029 | unsigned char swcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | struct page *page; |
| 1031 | swp_entry_t entry; |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1032 | unsigned int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | int shmem; |
| 1035 | |
| 1036 | /* |
| 1037 | * When searching mms for an entry, a good strategy is to |
| 1038 | * start at the first mm we freed the previous entry from |
| 1039 | * (though actually we don't notice whether we or coincidence |
| 1040 | * freed the entry). Initialize this start_mm with a hold. |
| 1041 | * |
| 1042 | * A simpler strategy would be to start at the last mm we |
| 1043 | * freed the previous entry from; but that would take less |
| 1044 | * advantage of mmlist ordering, which clusters forked mms |
| 1045 | * together, child after parent. If we race with dup_mmap(), we |
| 1046 | * prefer to resolve parent before child, lest we miss entries |
| 1047 | * duplicated after we scanned child: using last mm would invert |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 1048 | * that. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | */ |
| 1050 | start_mm = &init_mm; |
| 1051 | atomic_inc(&init_mm.mm_users); |
| 1052 | |
| 1053 | /* |
| 1054 | * Keep on scanning until all entries have gone. Usually, |
| 1055 | * one pass through swap_map is enough, but not necessarily: |
| 1056 | * there are races when an instance of an entry might be missed. |
| 1057 | */ |
| 1058 | while ((i = find_next_to_unuse(si, i)) != 0) { |
| 1059 | if (signal_pending(current)) { |
| 1060 | retval = -EINTR; |
| 1061 | break; |
| 1062 | } |
| 1063 | |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1064 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | * Get a page for the entry, using the existing swap |
| 1066 | * cache page if there is one. Otherwise, get a clean |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1067 | * page and read the swap into it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | */ |
| 1069 | swap_map = &si->swap_map[i]; |
| 1070 | entry = swp_entry(type, i); |
Hugh Dickins | 02098fe | 2008-02-04 22:28:42 -0800 | [diff] [blame] | 1071 | page = read_swap_cache_async(entry, |
| 1072 | GFP_HIGHUSER_MOVABLE, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | if (!page) { |
| 1074 | /* |
| 1075 | * Either swap_duplicate() failed because entry |
| 1076 | * has been freed independently, and will not be |
| 1077 | * reused since sys_swapoff() already disabled |
| 1078 | * allocation from here, or alloc_page() failed. |
| 1079 | */ |
| 1080 | if (!*swap_map) |
| 1081 | continue; |
| 1082 | retval = -ENOMEM; |
| 1083 | break; |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Don't hold on to start_mm if it looks like exiting. |
| 1088 | */ |
| 1089 | if (atomic_read(&start_mm->mm_users) == 1) { |
| 1090 | mmput(start_mm); |
| 1091 | start_mm = &init_mm; |
| 1092 | atomic_inc(&init_mm.mm_users); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Wait for and lock page. When do_swap_page races with |
| 1097 | * try_to_unuse, do_swap_page can handle the fault much |
| 1098 | * faster than try_to_unuse can locate the entry. This |
| 1099 | * apparently redundant "wait_on_page_locked" lets try_to_unuse |
| 1100 | * defer to do_swap_page in such a case - in some tests, |
| 1101 | * do_swap_page and try_to_unuse repeatedly compete. |
| 1102 | */ |
| 1103 | wait_on_page_locked(page); |
| 1104 | wait_on_page_writeback(page); |
| 1105 | lock_page(page); |
| 1106 | wait_on_page_writeback(page); |
| 1107 | |
| 1108 | /* |
| 1109 | * Remove all references to entry. |
| 1110 | * Whenever we reach init_mm, there's no address space |
| 1111 | * to search, but use it as a reminder to search shmem. |
| 1112 | */ |
| 1113 | shmem = 0; |
| 1114 | swcount = *swap_map; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1115 | if (swap_count(swcount)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | if (start_mm == &init_mm) |
| 1117 | shmem = shmem_unuse(entry, page); |
| 1118 | else |
| 1119 | retval = unuse_mm(start_mm, entry, page); |
| 1120 | } |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1121 | if (swap_count(*swap_map)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | int set_start_mm = (*swap_map >= swcount); |
| 1123 | struct list_head *p = &start_mm->mmlist; |
| 1124 | struct mm_struct *new_start_mm = start_mm; |
| 1125 | struct mm_struct *prev_mm = start_mm; |
| 1126 | struct mm_struct *mm; |
| 1127 | |
| 1128 | atomic_inc(&new_start_mm->mm_users); |
| 1129 | atomic_inc(&prev_mm->mm_users); |
| 1130 | spin_lock(&mmlist_lock); |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1131 | while (swap_count(*swap_map) && !retval && !shmem && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | (p = p->next) != &start_mm->mmlist) { |
| 1133 | mm = list_entry(p, struct mm_struct, mmlist); |
Hugh Dickins | 70af7c5 | 2006-06-23 02:03:44 -0700 | [diff] [blame] | 1134 | if (!atomic_inc_not_zero(&mm->mm_users)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | spin_unlock(&mmlist_lock); |
| 1137 | mmput(prev_mm); |
| 1138 | prev_mm = mm; |
| 1139 | |
| 1140 | cond_resched(); |
| 1141 | |
| 1142 | swcount = *swap_map; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1143 | if (!swap_count(swcount)) /* any usage ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | ; |
| 1145 | else if (mm == &init_mm) { |
| 1146 | set_start_mm = 1; |
| 1147 | shmem = shmem_unuse(entry, page); |
| 1148 | } else |
| 1149 | retval = unuse_mm(mm, entry, page); |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1150 | |
Bo Liu | 32c5fc1 | 2009-11-02 16:50:33 +0000 | [diff] [blame] | 1151 | if (set_start_mm && *swap_map < swcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | mmput(new_start_mm); |
| 1153 | atomic_inc(&mm->mm_users); |
| 1154 | new_start_mm = mm; |
| 1155 | set_start_mm = 0; |
| 1156 | } |
| 1157 | spin_lock(&mmlist_lock); |
| 1158 | } |
| 1159 | spin_unlock(&mmlist_lock); |
| 1160 | mmput(prev_mm); |
| 1161 | mmput(start_mm); |
| 1162 | start_mm = new_start_mm; |
| 1163 | } |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1164 | if (shmem) { |
| 1165 | /* page has already been unlocked and released */ |
| 1166 | if (shmem > 0) |
| 1167 | continue; |
| 1168 | retval = shmem; |
| 1169 | break; |
| 1170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | if (retval) { |
| 1172 | unlock_page(page); |
| 1173 | page_cache_release(page); |
| 1174 | break; |
| 1175 | } |
| 1176 | |
| 1177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | * If a reference remains (rare), we would like to leave |
| 1179 | * the page in the swap cache; but try_to_unmap could |
| 1180 | * then re-duplicate the entry once we drop page lock, |
| 1181 | * so we might loop indefinitely; also, that page could |
| 1182 | * not be swapped out to other storage meanwhile. So: |
| 1183 | * delete from cache even if there's another reference, |
| 1184 | * after ensuring that the data has been saved to disk - |
| 1185 | * since if the reference remains (rarer), it will be |
| 1186 | * read from disk into another page. Splitting into two |
| 1187 | * pages would be incorrect if swap supported "shared |
| 1188 | * private" pages, but they are handled by tmpfs files. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | */ |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 1190 | if (swap_count(*swap_map) && |
| 1191 | PageDirty(page) && PageSwapCache(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | struct writeback_control wbc = { |
| 1193 | .sync_mode = WB_SYNC_NONE, |
| 1194 | }; |
| 1195 | |
| 1196 | swap_writepage(page, &wbc); |
| 1197 | lock_page(page); |
| 1198 | wait_on_page_writeback(page); |
| 1199 | } |
Hugh Dickins | 68bdc8d | 2009-01-06 14:39:37 -0800 | [diff] [blame] | 1200 | |
| 1201 | /* |
| 1202 | * It is conceivable that a racing task removed this page from |
| 1203 | * swap cache just before we acquired the page lock at the top, |
| 1204 | * or while we dropped it in unuse_mm(). The page might even |
| 1205 | * be back in swap cache on another swap area: that we must not |
| 1206 | * delete, since it may not have been written out to swap yet. |
| 1207 | */ |
| 1208 | if (PageSwapCache(page) && |
| 1209 | likely(page_private(page) == entry.val)) |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1210 | delete_from_swap_cache(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | |
| 1212 | /* |
| 1213 | * So we could skip searching mms once swap count went |
| 1214 | * to 1, we did not mark any present ptes as dirty: must |
Anderson Briglia | 2706a1b | 2007-07-15 23:38:09 -0700 | [diff] [blame] | 1215 | * mark page dirty so shrink_page_list will preserve it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | */ |
| 1217 | SetPageDirty(page); |
| 1218 | unlock_page(page); |
| 1219 | page_cache_release(page); |
| 1220 | |
| 1221 | /* |
| 1222 | * Make sure that we aren't completely killing |
| 1223 | * interactive performance. |
| 1224 | */ |
| 1225 | cond_resched(); |
| 1226 | } |
| 1227 | |
| 1228 | mmput(start_mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | return retval; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1233 | * After a successful try_to_unuse, if no swap is now in use, we know |
| 1234 | * we can empty the mmlist. swap_lock must be held on entry and exit. |
| 1235 | * Note that mmlist_lock nests inside swap_lock, and an mm must be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | * added to the mmlist just after page_duplicate - before would be racy. |
| 1237 | */ |
| 1238 | static void drain_mmlist(void) |
| 1239 | { |
| 1240 | struct list_head *p, *next; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1241 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1243 | for (type = 0; type < nr_swapfiles; type++) |
| 1244 | if (swap_info[type]->inuse_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | return; |
| 1246 | spin_lock(&mmlist_lock); |
| 1247 | list_for_each_safe(p, next, &init_mm.mmlist) |
| 1248 | list_del_init(p); |
| 1249 | spin_unlock(&mmlist_lock); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * Use this swapdev's extent info to locate the (PAGE_SIZE) block which |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 1254 | * corresponds to page offset `offset'. Note that the type of this function |
| 1255 | * is sector_t, but it returns page offset into the bdev, not sector offset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | */ |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 1257 | sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | { |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 1259 | struct swap_info_struct *sis; |
| 1260 | struct swap_extent *start_se; |
| 1261 | struct swap_extent *se; |
| 1262 | pgoff_t offset; |
| 1263 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1264 | sis = swap_info[swp_type(entry)]; |
Hugh Dickins | f29ad6a | 2009-12-14 17:58:40 -0800 | [diff] [blame] | 1265 | *bdev = sis->bdev; |
| 1266 | |
| 1267 | offset = swp_offset(entry); |
| 1268 | start_se = sis->curr_swap_extent; |
| 1269 | se = start_se; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | for ( ; ; ) { |
| 1272 | struct list_head *lh; |
| 1273 | |
| 1274 | if (se->start_page <= offset && |
| 1275 | offset < (se->start_page + se->nr_pages)) { |
| 1276 | return se->start_block + (offset - se->start_page); |
| 1277 | } |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1278 | lh = se->list.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | se = list_entry(lh, struct swap_extent, list); |
| 1280 | sis->curr_swap_extent = se; |
| 1281 | BUG_ON(se == start_se); /* It *must* be present */ |
| 1282 | } |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * Free all of a swapdev's extent information |
| 1287 | */ |
| 1288 | static void destroy_swap_extents(struct swap_info_struct *sis) |
| 1289 | { |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1290 | while (!list_empty(&sis->first_swap_extent.list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | struct swap_extent *se; |
| 1292 | |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1293 | se = list_entry(sis->first_swap_extent.list.next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | struct swap_extent, list); |
| 1295 | list_del(&se->list); |
| 1296 | kfree(se); |
| 1297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | * Add a block range (and the corresponding page range) into this swapdev's |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1302 | * extent list. The extent list is kept sorted in page order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | * |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1304 | * This function rather assumes that it is called in ascending page order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | */ |
| 1306 | static int |
| 1307 | add_swap_extent(struct swap_info_struct *sis, unsigned long start_page, |
| 1308 | unsigned long nr_pages, sector_t start_block) |
| 1309 | { |
| 1310 | struct swap_extent *se; |
| 1311 | struct swap_extent *new_se; |
| 1312 | struct list_head *lh; |
| 1313 | |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1314 | if (start_page == 0) { |
| 1315 | se = &sis->first_swap_extent; |
| 1316 | sis->curr_swap_extent = se; |
| 1317 | se->start_page = 0; |
| 1318 | se->nr_pages = nr_pages; |
| 1319 | se->start_block = start_block; |
| 1320 | return 1; |
| 1321 | } else { |
| 1322 | lh = sis->first_swap_extent.list.prev; /* Highest extent */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | se = list_entry(lh, struct swap_extent, list); |
Hugh Dickins | 11d3188 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1324 | BUG_ON(se->start_page + se->nr_pages != start_page); |
| 1325 | if (se->start_block + se->nr_pages == start_block) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | /* Merge it */ |
| 1327 | se->nr_pages += nr_pages; |
| 1328 | return 0; |
| 1329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /* |
| 1333 | * No merge. Insert a new extent, preserving ordering. |
| 1334 | */ |
| 1335 | new_se = kmalloc(sizeof(*se), GFP_KERNEL); |
| 1336 | if (new_se == NULL) |
| 1337 | return -ENOMEM; |
| 1338 | new_se->start_page = start_page; |
| 1339 | new_se->nr_pages = nr_pages; |
| 1340 | new_se->start_block = start_block; |
| 1341 | |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1342 | list_add_tail(&new_se->list, &sis->first_swap_extent.list); |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1343 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /* |
| 1347 | * A `swap extent' is a simple thing which maps a contiguous range of pages |
| 1348 | * onto a contiguous range of disk blocks. An ordered list of swap extents |
| 1349 | * is built at swapon time and is then used at swap_writepage/swap_readpage |
| 1350 | * time for locating where on disk a page belongs. |
| 1351 | * |
| 1352 | * If the swapfile is an S_ISBLK block device, a single extent is installed. |
| 1353 | * This is done so that the main operating code can treat S_ISBLK and S_ISREG |
| 1354 | * swap files identically. |
| 1355 | * |
| 1356 | * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap |
| 1357 | * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK |
| 1358 | * swapfiles are handled *identically* after swapon time. |
| 1359 | * |
| 1360 | * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks |
| 1361 | * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If |
| 1362 | * some stray blocks are found which do not fall within the PAGE_SIZE alignment |
| 1363 | * requirements, they are simply tossed out - we will never use those blocks |
| 1364 | * for swapping. |
| 1365 | * |
Hugh Dickins | b0d9bcd | 2005-09-03 15:54:31 -0700 | [diff] [blame] | 1366 | * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | * prevents root from shooting her foot off by ftruncating an in-use swapfile, |
| 1368 | * which will scribble on the fs. |
| 1369 | * |
| 1370 | * The amount of disk space which a single swap extent represents varies. |
| 1371 | * Typically it is in the 1-4 megabyte range. So we can have hundreds of |
| 1372 | * extents in the list. To avoid much list walking, we cache the previous |
| 1373 | * search location in `curr_swap_extent', and start new searches from there. |
| 1374 | * This is extremely effective. The average number of iterations in |
| 1375 | * map_swap_page() has been measured at about 0.3 per page. - akpm. |
| 1376 | */ |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1377 | static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | { |
| 1379 | struct inode *inode; |
| 1380 | unsigned blocks_per_page; |
| 1381 | unsigned long page_no; |
| 1382 | unsigned blkbits; |
| 1383 | sector_t probe_block; |
| 1384 | sector_t last_block; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1385 | sector_t lowest_block = -1; |
| 1386 | sector_t highest_block = 0; |
| 1387 | int nr_extents = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | int ret; |
| 1389 | |
| 1390 | inode = sis->swap_file->f_mapping->host; |
| 1391 | if (S_ISBLK(inode->i_mode)) { |
| 1392 | ret = add_swap_extent(sis, 0, sis->max, 0); |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1393 | *span = sis->pages; |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1394 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | blkbits = inode->i_blkbits; |
| 1398 | blocks_per_page = PAGE_SIZE >> blkbits; |
| 1399 | |
| 1400 | /* |
| 1401 | * Map all the blocks into the extent list. This code doesn't try |
| 1402 | * to be very smart. |
| 1403 | */ |
| 1404 | probe_block = 0; |
| 1405 | page_no = 0; |
| 1406 | last_block = i_size_read(inode) >> blkbits; |
| 1407 | while ((probe_block + blocks_per_page) <= last_block && |
| 1408 | page_no < sis->max) { |
| 1409 | unsigned block_in_page; |
| 1410 | sector_t first_block; |
| 1411 | |
| 1412 | first_block = bmap(inode, probe_block); |
| 1413 | if (first_block == 0) |
| 1414 | goto bad_bmap; |
| 1415 | |
| 1416 | /* |
| 1417 | * It must be PAGE_SIZE aligned on-disk |
| 1418 | */ |
| 1419 | if (first_block & (blocks_per_page - 1)) { |
| 1420 | probe_block++; |
| 1421 | goto reprobe; |
| 1422 | } |
| 1423 | |
| 1424 | for (block_in_page = 1; block_in_page < blocks_per_page; |
| 1425 | block_in_page++) { |
| 1426 | sector_t block; |
| 1427 | |
| 1428 | block = bmap(inode, probe_block + block_in_page); |
| 1429 | if (block == 0) |
| 1430 | goto bad_bmap; |
| 1431 | if (block != first_block + block_in_page) { |
| 1432 | /* Discontiguity */ |
| 1433 | probe_block++; |
| 1434 | goto reprobe; |
| 1435 | } |
| 1436 | } |
| 1437 | |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1438 | first_block >>= (PAGE_SHIFT - blkbits); |
| 1439 | if (page_no) { /* exclude the header page */ |
| 1440 | if (first_block < lowest_block) |
| 1441 | lowest_block = first_block; |
| 1442 | if (first_block > highest_block) |
| 1443 | highest_block = first_block; |
| 1444 | } |
| 1445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | /* |
| 1447 | * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks |
| 1448 | */ |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1449 | ret = add_swap_extent(sis, page_no, 1, first_block); |
| 1450 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | goto out; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1452 | nr_extents += ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | page_no++; |
| 1454 | probe_block += blocks_per_page; |
| 1455 | reprobe: |
| 1456 | continue; |
| 1457 | } |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1458 | ret = nr_extents; |
| 1459 | *span = 1 + highest_block - lowest_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | if (page_no == 0) |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1461 | page_no = 1; /* force Empty message */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | sis->max = page_no; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1463 | sis->pages = page_no - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | sis->highest_bit = page_no - 1; |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1465 | out: |
| 1466 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | bad_bmap: |
| 1468 | printk(KERN_ERR "swapon: swapfile has holes\n"); |
| 1469 | ret = -EINVAL; |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1470 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1473 | SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 1475 | struct swap_info_struct *p = NULL; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 1476 | unsigned char *swap_map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | struct file *swap_file, *victim; |
| 1478 | struct address_space *mapping; |
| 1479 | struct inode *inode; |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 1480 | char *pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | int i, type, prev; |
| 1482 | int err; |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | if (!capable(CAP_SYS_ADMIN)) |
| 1485 | return -EPERM; |
| 1486 | |
| 1487 | pathname = getname(specialfile); |
| 1488 | err = PTR_ERR(pathname); |
| 1489 | if (IS_ERR(pathname)) |
| 1490 | goto out; |
| 1491 | |
| 1492 | victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0); |
| 1493 | putname(pathname); |
| 1494 | err = PTR_ERR(victim); |
| 1495 | if (IS_ERR(victim)) |
| 1496 | goto out; |
| 1497 | |
| 1498 | mapping = victim->f_mapping; |
| 1499 | prev = -1; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1500 | spin_lock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1501 | for (type = swap_list.head; type >= 0; type = swap_info[type]->next) { |
| 1502 | p = swap_info[type]; |
Hugh Dickins | 22c6f8f | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1503 | if (p->flags & SWP_WRITEOK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | if (p->swap_file->f_mapping == mapping) |
| 1505 | break; |
| 1506 | } |
| 1507 | prev = type; |
| 1508 | } |
| 1509 | if (type < 0) { |
| 1510 | err = -EINVAL; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1511 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | goto out_dput; |
| 1513 | } |
| 1514 | if (!security_vm_enough_memory(p->pages)) |
| 1515 | vm_unacct_memory(p->pages); |
| 1516 | else { |
| 1517 | err = -ENOMEM; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1518 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | goto out_dput; |
| 1520 | } |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1521 | if (prev < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | swap_list.head = p->next; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1523 | else |
| 1524 | swap_info[prev]->next = p->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | if (type == swap_list.next) { |
| 1526 | /* just pick something that's safe... */ |
| 1527 | swap_list.next = swap_list.head; |
| 1528 | } |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1529 | if (p->prio < 0) { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1530 | for (i = p->next; i >= 0; i = swap_info[i]->next) |
| 1531 | swap_info[i]->prio = p->prio--; |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1532 | least_priority++; |
| 1533 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | nr_swap_pages -= p->pages; |
| 1535 | total_swap_pages -= p->pages; |
| 1536 | p->flags &= ~SWP_WRITEOK; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1537 | spin_unlock(&swap_lock); |
Hugh Dickins | fb4f88d | 2005-09-03 15:54:37 -0700 | [diff] [blame] | 1538 | |
Hugh Dickins | 35451be | 2009-09-21 17:02:27 -0700 | [diff] [blame] | 1539 | current->flags |= PF_OOM_ORIGIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | err = try_to_unuse(type); |
Hugh Dickins | 35451be | 2009-09-21 17:02:27 -0700 | [diff] [blame] | 1541 | current->flags &= ~PF_OOM_ORIGIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | if (err) { |
| 1544 | /* re-insert swap space back into swap_list */ |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1545 | spin_lock(&swap_lock); |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1546 | if (p->prio < 0) |
| 1547 | p->prio = --least_priority; |
| 1548 | prev = -1; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1549 | for (i = swap_list.head; i >= 0; i = swap_info[i]->next) { |
| 1550 | if (p->prio >= swap_info[i]->prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | break; |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1552 | prev = i; |
| 1553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | p->next = i; |
| 1555 | if (prev < 0) |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1556 | swap_list.head = swap_list.next = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | else |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1558 | swap_info[prev]->next = type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | nr_swap_pages += p->pages; |
| 1560 | total_swap_pages += p->pages; |
| 1561 | p->flags |= SWP_WRITEOK; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1562 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | goto out_dput; |
| 1564 | } |
Hugh Dickins | 52b7efdb | 2005-09-03 15:54:39 -0700 | [diff] [blame] | 1565 | |
| 1566 | /* wait for any unplug function to finish */ |
| 1567 | down_write(&swap_unplug_sem); |
| 1568 | up_write(&swap_unplug_sem); |
| 1569 | |
Hugh Dickins | 4cd3bb1 | 2005-09-03 15:54:33 -0700 | [diff] [blame] | 1570 | destroy_swap_extents(p); |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 1571 | if (p->flags & SWP_CONTINUED) |
| 1572 | free_swap_count_continuations(p); |
| 1573 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1574 | mutex_lock(&swapon_mutex); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1575 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | drain_mmlist(); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1577 | |
| 1578 | /* wait for anyone still in scan_swap_map */ |
| 1579 | p->highest_bit = 0; /* cuts scans short */ |
| 1580 | while (p->flags >= SWP_SCANNING) { |
| 1581 | spin_unlock(&swap_lock); |
Nishanth Aravamudan | 13e4b57 | 2005-09-10 00:27:25 -0700 | [diff] [blame] | 1582 | schedule_timeout_uninterruptible(1); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1583 | spin_lock(&swap_lock); |
| 1584 | } |
| 1585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | swap_file = p->swap_file; |
| 1587 | p->swap_file = NULL; |
| 1588 | p->max = 0; |
| 1589 | swap_map = p->swap_map; |
| 1590 | p->swap_map = NULL; |
| 1591 | p->flags = 0; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1592 | spin_unlock(&swap_lock); |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1593 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | vfree(swap_map); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 1595 | /* Destroy swap account informatin */ |
| 1596 | swap_cgroup_swapoff(type); |
| 1597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | inode = mapping->host; |
| 1599 | if (S_ISBLK(inode->i_mode)) { |
| 1600 | struct block_device *bdev = I_BDEV(inode); |
| 1601 | set_blocksize(bdev, p->old_block_size); |
| 1602 | bd_release(bdev); |
| 1603 | } else { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1604 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | inode->i_flags &= ~S_SWAPFILE; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1606 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | } |
| 1608 | filp_close(swap_file, NULL); |
| 1609 | err = 0; |
| 1610 | |
| 1611 | out_dput: |
| 1612 | filp_close(victim, NULL); |
| 1613 | out: |
| 1614 | return err; |
| 1615 | } |
| 1616 | |
| 1617 | #ifdef CONFIG_PROC_FS |
| 1618 | /* iterator */ |
| 1619 | static void *swap_start(struct seq_file *swap, loff_t *pos) |
| 1620 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1621 | struct swap_info_struct *si; |
| 1622 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | loff_t l = *pos; |
| 1624 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1625 | mutex_lock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Suleiman Souhlal | 881e4aa | 2006-12-06 20:32:28 -0800 | [diff] [blame] | 1627 | if (!l) |
| 1628 | return SEQ_START_TOKEN; |
| 1629 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1630 | for (type = 0; type < nr_swapfiles; type++) { |
| 1631 | smp_rmb(); /* read nr_swapfiles before swap_info[type] */ |
| 1632 | si = swap_info[type]; |
| 1633 | if (!(si->flags & SWP_USED) || !si->swap_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | continue; |
Suleiman Souhlal | 881e4aa | 2006-12-06 20:32:28 -0800 | [diff] [blame] | 1635 | if (!--l) |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1636 | return si; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | return NULL; |
| 1640 | } |
| 1641 | |
| 1642 | static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) |
| 1643 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1644 | struct swap_info_struct *si = v; |
| 1645 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | |
Suleiman Souhlal | 881e4aa | 2006-12-06 20:32:28 -0800 | [diff] [blame] | 1647 | if (v == SEQ_START_TOKEN) |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1648 | type = 0; |
| 1649 | else |
| 1650 | type = si->type + 1; |
Suleiman Souhlal | 881e4aa | 2006-12-06 20:32:28 -0800 | [diff] [blame] | 1651 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1652 | for (; type < nr_swapfiles; type++) { |
| 1653 | smp_rmb(); /* read nr_swapfiles before swap_info[type] */ |
| 1654 | si = swap_info[type]; |
| 1655 | if (!(si->flags & SWP_USED) || !si->swap_map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | continue; |
| 1657 | ++*pos; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1658 | return si; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | return NULL; |
| 1662 | } |
| 1663 | |
| 1664 | static void swap_stop(struct seq_file *swap, void *v) |
| 1665 | { |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1666 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | static int swap_show(struct seq_file *swap, void *v) |
| 1670 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1671 | struct swap_info_struct *si = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | struct file *file; |
| 1673 | int len; |
| 1674 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1675 | if (si == SEQ_START_TOKEN) { |
Suleiman Souhlal | 881e4aa | 2006-12-06 20:32:28 -0800 | [diff] [blame] | 1676 | seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n"); |
| 1677 | return 0; |
| 1678 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1680 | file = si->swap_file; |
Jan Blunck | c32c2f6 | 2008-02-14 19:38:43 -0800 | [diff] [blame] | 1681 | len = seq_path(swap, &file->f_path, " \t\n\\"); |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1682 | seq_printf(swap, "%*s%s\t%u\t%u\t%d\n", |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1683 | len < 40 ? 40 - len : 1, " ", |
| 1684 | S_ISBLK(file->f_path.dentry->d_inode->i_mode) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | "partition" : "file\t", |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1686 | si->pages << (PAGE_SHIFT - 10), |
| 1687 | si->inuse_pages << (PAGE_SHIFT - 10), |
| 1688 | si->prio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 1692 | static const struct seq_operations swaps_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | .start = swap_start, |
| 1694 | .next = swap_next, |
| 1695 | .stop = swap_stop, |
| 1696 | .show = swap_show |
| 1697 | }; |
| 1698 | |
| 1699 | static int swaps_open(struct inode *inode, struct file *file) |
| 1700 | { |
| 1701 | return seq_open(file, &swaps_op); |
| 1702 | } |
| 1703 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 1704 | static const struct file_operations proc_swaps_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | .open = swaps_open, |
| 1706 | .read = seq_read, |
| 1707 | .llseek = seq_lseek, |
| 1708 | .release = seq_release, |
| 1709 | }; |
| 1710 | |
| 1711 | static int __init procswaps_init(void) |
| 1712 | { |
Denis V. Lunev | 3d71f86 | 2008-04-29 01:02:13 -0700 | [diff] [blame] | 1713 | proc_create("swaps", 0, NULL, &proc_swaps_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | __initcall(procswaps_init); |
| 1717 | #endif /* CONFIG_PROC_FS */ |
| 1718 | |
Jan Beulich | 1796316 | 2008-12-16 11:35:24 +0000 | [diff] [blame] | 1719 | #ifdef MAX_SWAPFILES_CHECK |
| 1720 | static int __init max_swapfiles_check(void) |
| 1721 | { |
| 1722 | MAX_SWAPFILES_CHECK(); |
| 1723 | return 0; |
| 1724 | } |
| 1725 | late_initcall(max_swapfiles_check); |
| 1726 | #endif |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | /* |
| 1729 | * Written 01/25/92 by Simmule Turner, heavily changed by Linus. |
| 1730 | * |
| 1731 | * The swapon system call |
| 1732 | */ |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1733 | SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 1735 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | char *name = NULL; |
| 1737 | struct block_device *bdev = NULL; |
| 1738 | struct file *swap_file = NULL; |
| 1739 | struct address_space *mapping; |
| 1740 | unsigned int type; |
| 1741 | int i, prev; |
| 1742 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | union swap_header *swap_header = NULL; |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1744 | unsigned int nr_good_pages = 0; |
| 1745 | int nr_extents = 0; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1746 | sector_t span; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | unsigned long maxpages = 1; |
Hugh Dickins | 73fd874 | 2009-01-06 14:39:47 -0800 | [diff] [blame] | 1748 | unsigned long swapfilepages; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 1749 | unsigned char *swap_map = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | struct page *page = NULL; |
| 1751 | struct inode *inode = NULL; |
| 1752 | int did_down = 0; |
| 1753 | |
| 1754 | if (!capable(CAP_SYS_ADMIN)) |
| 1755 | return -EPERM; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1756 | |
| 1757 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 1758 | if (!p) |
| 1759 | return -ENOMEM; |
| 1760 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1761 | spin_lock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1762 | for (type = 0; type < nr_swapfiles; type++) { |
| 1763 | if (!(swap_info[type]->flags & SWP_USED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | break; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | error = -EPERM; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 1767 | if (type >= MAX_SWAPFILES) { |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1768 | spin_unlock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1769 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | goto out; |
| 1771 | } |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1772 | if (type >= nr_swapfiles) { |
| 1773 | p->type = type; |
| 1774 | swap_info[type] = p; |
| 1775 | /* |
| 1776 | * Write swap_info[type] before nr_swapfiles, in case a |
| 1777 | * racing procfs swap_start() or swap_next() is reading them. |
| 1778 | * (We never shrink nr_swapfiles, we never free this entry.) |
| 1779 | */ |
| 1780 | smp_wmb(); |
| 1781 | nr_swapfiles++; |
| 1782 | } else { |
| 1783 | kfree(p); |
| 1784 | p = swap_info[type]; |
| 1785 | /* |
| 1786 | * Do not memset this entry: a racing procfs swap_next() |
| 1787 | * would be relying on p->type to remain valid. |
| 1788 | */ |
| 1789 | } |
Hugh Dickins | 9625a5f | 2009-12-14 17:58:42 -0800 | [diff] [blame] | 1790 | INIT_LIST_HEAD(&p->first_swap_extent.list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | p->flags = SWP_USED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | p->next = -1; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1793 | spin_unlock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | name = getname(specialfile); |
| 1796 | error = PTR_ERR(name); |
| 1797 | if (IS_ERR(name)) { |
| 1798 | name = NULL; |
| 1799 | goto bad_swap_2; |
| 1800 | } |
| 1801 | swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0); |
| 1802 | error = PTR_ERR(swap_file); |
| 1803 | if (IS_ERR(swap_file)) { |
| 1804 | swap_file = NULL; |
| 1805 | goto bad_swap_2; |
| 1806 | } |
| 1807 | |
| 1808 | p->swap_file = swap_file; |
| 1809 | mapping = swap_file->f_mapping; |
| 1810 | inode = mapping->host; |
| 1811 | |
| 1812 | error = -EBUSY; |
| 1813 | for (i = 0; i < nr_swapfiles; i++) { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1814 | struct swap_info_struct *q = swap_info[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | |
| 1816 | if (i == type || !q->swap_file) |
| 1817 | continue; |
| 1818 | if (mapping == q->swap_file->f_mapping) |
| 1819 | goto bad_swap; |
| 1820 | } |
| 1821 | |
| 1822 | error = -EINVAL; |
| 1823 | if (S_ISBLK(inode->i_mode)) { |
| 1824 | bdev = I_BDEV(inode); |
| 1825 | error = bd_claim(bdev, sys_swapon); |
| 1826 | if (error < 0) { |
| 1827 | bdev = NULL; |
Rob Landley | f7b3a43 | 2005-09-22 21:44:27 -0700 | [diff] [blame] | 1828 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | goto bad_swap; |
| 1830 | } |
| 1831 | p->old_block_size = block_size(bdev); |
| 1832 | error = set_blocksize(bdev, PAGE_SIZE); |
| 1833 | if (error < 0) |
| 1834 | goto bad_swap; |
| 1835 | p->bdev = bdev; |
| 1836 | } else if (S_ISREG(inode->i_mode)) { |
| 1837 | p->bdev = inode->i_sb->s_bdev; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1838 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | did_down = 1; |
| 1840 | if (IS_SWAPFILE(inode)) { |
| 1841 | error = -EBUSY; |
| 1842 | goto bad_swap; |
| 1843 | } |
| 1844 | } else { |
| 1845 | goto bad_swap; |
| 1846 | } |
| 1847 | |
Hugh Dickins | 73fd874 | 2009-01-06 14:39:47 -0800 | [diff] [blame] | 1848 | swapfilepages = i_size_read(inode) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | |
| 1850 | /* |
| 1851 | * Read the swap header. |
| 1852 | */ |
| 1853 | if (!mapping->a_ops->readpage) { |
| 1854 | error = -EINVAL; |
| 1855 | goto bad_swap; |
| 1856 | } |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 1857 | page = read_mapping_page(mapping, 0, swap_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | if (IS_ERR(page)) { |
| 1859 | error = PTR_ERR(page); |
| 1860 | goto bad_swap; |
| 1861 | } |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1862 | swap_header = kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1864 | if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) { |
Jesper Juhl | e97a311 | 2006-01-11 01:50:28 +0100 | [diff] [blame] | 1865 | printk(KERN_ERR "Unable to find swap-space signature\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | error = -EINVAL; |
| 1867 | goto bad_swap; |
| 1868 | } |
Hugh Dickins | 886bb7e | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1869 | |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1870 | /* swap partition endianess hack... */ |
| 1871 | if (swab32(swap_header->info.version) == 1) { |
| 1872 | swab32s(&swap_header->info.version); |
| 1873 | swab32s(&swap_header->info.last_page); |
| 1874 | swab32s(&swap_header->info.nr_badpages); |
| 1875 | for (i = 0; i < swap_header->info.nr_badpages; i++) |
| 1876 | swab32s(&swap_header->info.badpages[i]); |
| 1877 | } |
| 1878 | /* Check the swap header's sub-version */ |
| 1879 | if (swap_header->info.version != 1) { |
| 1880 | printk(KERN_WARNING |
| 1881 | "Unable to handle swap header version %d\n", |
| 1882 | swap_header->info.version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | error = -EINVAL; |
| 1884 | goto bad_swap; |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | p->lowest_bit = 1; |
| 1888 | p->cluster_next = 1; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1889 | p->cluster_nr = 0; |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1890 | |
| 1891 | /* |
| 1892 | * Find out how many pages are allowed for a single swap |
| 1893 | * device. There are two limiting factors: 1) the number of |
| 1894 | * bits for the swap offset in the swp_entry_t type and |
| 1895 | * 2) the number of bits in the a swap pte as defined by |
| 1896 | * the different architectures. In order to find the |
| 1897 | * largest possible bit mask a swap entry with swap type 0 |
| 1898 | * and swap offset ~0UL is created, encoded to a swap pte, |
| 1899 | * decoded to a swp_entry_t again and finally the swap |
| 1900 | * offset is extracted. This will mask all the bits from |
| 1901 | * the initial ~0UL mask that can't be encoded in either |
| 1902 | * the swp_entry_t or the architecture definition of a |
| 1903 | * swap pte. |
| 1904 | */ |
| 1905 | maxpages = swp_offset(pte_to_swp_entry( |
| 1906 | swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1; |
| 1907 | if (maxpages > swap_header->info.last_page) |
| 1908 | maxpages = swap_header->info.last_page; |
| 1909 | p->highest_bit = maxpages - 1; |
| 1910 | |
| 1911 | error = -EINVAL; |
| 1912 | if (!maxpages) |
| 1913 | goto bad_swap; |
| 1914 | if (swapfilepages && maxpages > swapfilepages) { |
| 1915 | printk(KERN_WARNING |
| 1916 | "Swap area shorter than signature indicates\n"); |
| 1917 | goto bad_swap; |
| 1918 | } |
| 1919 | if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode)) |
| 1920 | goto bad_swap; |
| 1921 | if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES) |
| 1922 | goto bad_swap; |
| 1923 | |
| 1924 | /* OK, set up the swap map and apply the bad block list */ |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 1925 | swap_map = vmalloc(maxpages); |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1926 | if (!swap_map) { |
| 1927 | error = -ENOMEM; |
| 1928 | goto bad_swap; |
| 1929 | } |
| 1930 | |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 1931 | memset(swap_map, 0, maxpages); |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1932 | for (i = 0; i < swap_header->info.nr_badpages; i++) { |
| 1933 | int page_nr = swap_header->info.badpages[i]; |
| 1934 | if (page_nr <= 0 || page_nr >= swap_header->info.last_page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | error = -EINVAL; |
| 1936 | goto bad_swap; |
| 1937 | } |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1938 | swap_map[page_nr] = SWAP_MAP_BAD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | } |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 1940 | |
| 1941 | error = swap_cgroup_swapon(type, maxpages); |
| 1942 | if (error) |
| 1943 | goto bad_swap; |
| 1944 | |
Hugh Dickins | 81e3397 | 2009-01-06 14:39:49 -0800 | [diff] [blame] | 1945 | nr_good_pages = swap_header->info.last_page - |
| 1946 | swap_header->info.nr_badpages - |
| 1947 | 1 /* header page */; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1948 | |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1949 | if (nr_good_pages) { |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1950 | swap_map[0] = SWAP_MAP_BAD; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1951 | p->max = maxpages; |
| 1952 | p->pages = nr_good_pages; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1953 | nr_extents = setup_swap_extents(p, &span); |
| 1954 | if (nr_extents < 0) { |
| 1955 | error = nr_extents; |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1956 | goto bad_swap; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1957 | } |
Hugh Dickins | e2244ec | 2005-09-03 15:54:32 -0700 | [diff] [blame] | 1958 | nr_good_pages = p->pages; |
| 1959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | if (!nr_good_pages) { |
| 1961 | printk(KERN_WARNING "Empty swap-file\n"); |
| 1962 | error = -EINVAL; |
| 1963 | goto bad_swap; |
| 1964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
Suresh Jayaraman | 3bd0f0c | 2009-09-30 10:53:48 +0200 | [diff] [blame] | 1966 | if (p->bdev) { |
| 1967 | if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { |
| 1968 | p->flags |= SWP_SOLIDSTATE; |
| 1969 | p->cluster_next = 1 + (random32() % p->highest_bit); |
| 1970 | } |
| 1971 | if (discard_swap(p) == 0) |
| 1972 | p->flags |= SWP_DISCARDABLE; |
Hugh Dickins | 20137a4 | 2009-01-06 14:39:54 -0800 | [diff] [blame] | 1973 | } |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 1974 | |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 1975 | mutex_lock(&swapon_mutex); |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 1976 | spin_lock(&swap_lock); |
Hugh Dickins | 78ecba0 | 2008-07-23 21:28:23 -0700 | [diff] [blame] | 1977 | if (swap_flags & SWAP_FLAG_PREFER) |
| 1978 | p->prio = |
| 1979 | (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; |
| 1980 | else |
| 1981 | p->prio = --least_priority; |
| 1982 | p->swap_map = swap_map; |
Hugh Dickins | 22c6f8f | 2009-01-06 14:39:48 -0800 | [diff] [blame] | 1983 | p->flags |= SWP_WRITEOK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | nr_swap_pages += nr_good_pages; |
| 1985 | total_swap_pages += nr_good_pages; |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1986 | |
Hugh Dickins | 6eb396d | 2005-09-03 15:54:35 -0700 | [diff] [blame] | 1987 | printk(KERN_INFO "Adding %uk swap on %s. " |
Hugh Dickins | 20137a4 | 2009-01-06 14:39:54 -0800 | [diff] [blame] | 1988 | "Priority:%d extents:%d across:%lluk %s%s\n", |
Hugh Dickins | 53092a7 | 2005-09-03 15:54:34 -0700 | [diff] [blame] | 1989 | nr_good_pages<<(PAGE_SHIFT-10), name, p->prio, |
Hugh Dickins | 6a6ba83 | 2009-01-06 14:39:51 -0800 | [diff] [blame] | 1990 | nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), |
Hugh Dickins | 20137a4 | 2009-01-06 14:39:54 -0800 | [diff] [blame] | 1991 | (p->flags & SWP_SOLIDSTATE) ? "SS" : "", |
| 1992 | (p->flags & SWP_DISCARDABLE) ? "D" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | |
| 1994 | /* insert swap space into swap_list: */ |
| 1995 | prev = -1; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 1996 | for (i = swap_list.head; i >= 0; i = swap_info[i]->next) { |
| 1997 | if (p->prio >= swap_info[i]->prio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | prev = i; |
| 2000 | } |
| 2001 | p->next = i; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 2002 | if (prev < 0) |
| 2003 | swap_list.head = swap_list.next = type; |
| 2004 | else |
| 2005 | swap_info[prev]->next = type; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2006 | spin_unlock(&swap_lock); |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 2007 | mutex_unlock(&swapon_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | error = 0; |
| 2009 | goto out; |
| 2010 | bad_swap: |
| 2011 | if (bdev) { |
| 2012 | set_blocksize(bdev, p->old_block_size); |
| 2013 | bd_release(bdev); |
| 2014 | } |
Hugh Dickins | 4cd3bb1 | 2005-09-03 15:54:33 -0700 | [diff] [blame] | 2015 | destroy_swap_extents(p); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 2016 | swap_cgroup_swapoff(type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | bad_swap_2: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2018 | spin_lock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | p->swap_file = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | p->flags = 0; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2021 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | vfree(swap_map); |
| 2023 | if (swap_file) |
| 2024 | filp_close(swap_file, NULL); |
| 2025 | out: |
| 2026 | if (page && !IS_ERR(page)) { |
| 2027 | kunmap(page); |
| 2028 | page_cache_release(page); |
| 2029 | } |
| 2030 | if (name) |
| 2031 | putname(name); |
| 2032 | if (did_down) { |
| 2033 | if (!error) |
| 2034 | inode->i_flags |= S_SWAPFILE; |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2035 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | } |
| 2037 | return error; |
| 2038 | } |
| 2039 | |
| 2040 | void si_swapinfo(struct sysinfo *val) |
| 2041 | { |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 2042 | unsigned int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | unsigned long nr_to_be_unused = 0; |
| 2044 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2045 | spin_lock(&swap_lock); |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 2046 | for (type = 0; type < nr_swapfiles; type++) { |
| 2047 | struct swap_info_struct *si = swap_info[type]; |
| 2048 | |
| 2049 | if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK)) |
| 2050 | nr_to_be_unused += si->inuse_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | } |
| 2052 | val->freeswap = nr_swap_pages + nr_to_be_unused; |
| 2053 | val->totalswap = total_swap_pages + nr_to_be_unused; |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2054 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * Verify that a swap entry is valid and increment its swap map count. |
| 2059 | * |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2060 | * Returns error code in following case. |
| 2061 | * - success -> 0 |
| 2062 | * - swp_entry is invalid -> EINVAL |
| 2063 | * - swp_entry is migration entry -> EINVAL |
| 2064 | * - swap-cache reference is requested but there is already one. -> EEXIST |
| 2065 | * - swap-cache reference is requested but the entry is not used. -> ENOENT |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2066 | * - swap-mapped reference requested but needs continued swap count. -> ENOMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | */ |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 2068 | static int __swap_duplicate(swp_entry_t entry, unsigned char usage) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | { |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 2070 | struct swap_info_struct *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | unsigned long offset, type; |
Hugh Dickins | 8d69aae | 2009-12-14 17:58:45 -0800 | [diff] [blame] | 2072 | unsigned char count; |
| 2073 | unsigned char has_cache; |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2074 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | |
Andi Kleen | a7420aa | 2009-09-16 11:50:05 +0200 | [diff] [blame] | 2076 | if (non_swap_entry(entry)) |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2077 | goto out; |
Christoph Lameter | 0697212 | 2006-06-23 02:03:35 -0700 | [diff] [blame] | 2078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | type = swp_type(entry); |
| 2080 | if (type >= nr_swapfiles) |
| 2081 | goto bad_file; |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 2082 | p = swap_info[type]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | offset = swp_offset(entry); |
| 2084 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2085 | spin_lock(&swap_lock); |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2086 | if (unlikely(offset >= p->max)) |
| 2087 | goto unlock_out; |
| 2088 | |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2089 | count = p->swap_map[offset]; |
| 2090 | has_cache = count & SWAP_HAS_CACHE; |
| 2091 | count &= ~SWAP_HAS_CACHE; |
| 2092 | err = 0; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2093 | |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2094 | if (usage == SWAP_HAS_CACHE) { |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2095 | |
| 2096 | /* set SWAP_HAS_CACHE if there is no cache and entry is used */ |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2097 | if (!has_cache && count) |
| 2098 | has_cache = SWAP_HAS_CACHE; |
| 2099 | else if (has_cache) /* someone else added cache */ |
| 2100 | err = -EEXIST; |
| 2101 | else /* no users remaining */ |
| 2102 | err = -ENOENT; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2103 | |
| 2104 | } else if (count || has_cache) { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2105 | |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2106 | if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX) |
| 2107 | count += usage; |
| 2108 | else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX) |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2109 | err = -EINVAL; |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2110 | else if (swap_count_continued(p, offset, count)) |
| 2111 | count = COUNT_CONTINUED; |
| 2112 | else |
| 2113 | err = -ENOMEM; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2114 | } else |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2115 | err = -ENOENT; /* unused swap entry */ |
| 2116 | |
| 2117 | p->swap_map[offset] = count | has_cache; |
| 2118 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2119 | unlock_out: |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2120 | spin_unlock(&swap_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | out: |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2122 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | |
| 2124 | bad_file: |
| 2125 | printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val); |
| 2126 | goto out; |
| 2127 | } |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2128 | |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2129 | /* |
| 2130 | * increase reference count of swap entry by 1. |
| 2131 | */ |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2132 | int swap_duplicate(swp_entry_t entry) |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2133 | { |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2134 | int err = 0; |
| 2135 | |
| 2136 | while (!err && __swap_duplicate(entry, 1) == -ENOMEM) |
| 2137 | err = add_swap_count_continuation(entry, GFP_ATOMIC); |
| 2138 | return err; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 2141 | /* |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2142 | * @entry: swap entry for which we allocate swap cache. |
| 2143 | * |
Hugh Dickins | 73c34b6 | 2009-12-14 17:58:43 -0800 | [diff] [blame] | 2144 | * Called when allocating swap cache for existing swap entry, |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2145 | * This can return error codes. Returns 0 at success. |
| 2146 | * -EBUSY means there is a swap cache. |
| 2147 | * Note: return code is different from swap_duplicate(). |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 2148 | */ |
| 2149 | int swapcache_prepare(swp_entry_t entry) |
| 2150 | { |
Hugh Dickins | 253d553 | 2009-12-14 17:58:44 -0800 | [diff] [blame] | 2151 | return __swap_duplicate(entry, SWAP_HAS_CACHE); |
KAMEZAWA Hiroyuki | cb4b86b | 2009-06-16 15:32:52 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2154 | /* |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2155 | * swap_lock prevents swap_map being freed. Don't grab an extra |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | * reference on the swaphandle, it doesn't matter if it becomes unused. |
| 2157 | */ |
| 2158 | int valid_swaphandles(swp_entry_t entry, unsigned long *offset) |
| 2159 | { |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2160 | struct swap_info_struct *si; |
Hugh Dickins | 3f9e794 | 2006-09-29 02:01:26 -0700 | [diff] [blame] | 2161 | int our_page_cluster = page_cluster; |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2162 | pgoff_t target, toff; |
| 2163 | pgoff_t base, end; |
| 2164 | int nr_pages = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | |
Hugh Dickins | 3f9e794 | 2006-09-29 02:01:26 -0700 | [diff] [blame] | 2166 | if (!our_page_cluster) /* no readahead */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | return 0; |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2168 | |
Hugh Dickins | efa90a9 | 2009-12-14 17:58:41 -0800 | [diff] [blame] | 2169 | si = swap_info[swp_type(entry)]; |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2170 | target = swp_offset(entry); |
| 2171 | base = (target >> our_page_cluster) << our_page_cluster; |
| 2172 | end = base + (1 << our_page_cluster); |
| 2173 | if (!base) /* first page is swap header */ |
| 2174 | base++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2176 | spin_lock(&swap_lock); |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2177 | if (end > si->max) /* don't go beyond end of map */ |
| 2178 | end = si->max; |
| 2179 | |
| 2180 | /* Count contiguous allocated slots above our target */ |
| 2181 | for (toff = target; ++toff < end; nr_pages++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | /* Don't read in free or bad pages */ |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2183 | if (!si->swap_map[toff]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | break; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2185 | if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | break; |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2187 | } |
| 2188 | /* Count contiguous allocated slots below our target */ |
| 2189 | for (toff = target; --toff >= base; nr_pages++) { |
| 2190 | /* Don't read in free or bad pages */ |
| 2191 | if (!si->swap_map[toff]) |
| 2192 | break; |
KAMEZAWA Hiroyuki | 355cfa7 | 2009-06-16 15:32:53 -0700 | [diff] [blame] | 2193 | if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD) |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2194 | break; |
| 2195 | } |
Hugh Dickins | 5d337b9 | 2005-09-03 15:54:41 -0700 | [diff] [blame] | 2196 | spin_unlock(&swap_lock); |
Hugh Dickins | 8952898 | 2008-02-04 22:28:45 -0800 | [diff] [blame] | 2197 | |
| 2198 | /* |
| 2199 | * Indicate starting offset, and return number of pages to get: |
| 2200 | * if only 1, say 0, since there's then no readahead to be done. |
| 2201 | */ |
| 2202 | *offset = ++toff; |
| 2203 | return nr_pages? ++nr_pages: 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | } |
Hugh Dickins | 570a335b | 2009-12-14 17:58:46 -0800 | [diff] [blame^] | 2205 | |
| 2206 | /* |
| 2207 | * add_swap_count_continuation - called when a swap count is duplicated |
| 2208 | * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's |
| 2209 | * page of the original vmalloc'ed swap_map, to hold the continuation count |
| 2210 | * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called |
| 2211 | * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc. |
| 2212 | * |
| 2213 | * These continuation pages are seldom referenced: the common paths all work |
| 2214 | * on the original swap_map, only referring to a continuation page when the |
| 2215 | * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX. |
| 2216 | * |
| 2217 | * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding |
| 2218 | * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL) |
| 2219 | * can be called after dropping locks. |
| 2220 | */ |
| 2221 | int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask) |
| 2222 | { |
| 2223 | struct swap_info_struct *si; |
| 2224 | struct page *head; |
| 2225 | struct page *page; |
| 2226 | struct page *list_page; |
| 2227 | pgoff_t offset; |
| 2228 | unsigned char count; |
| 2229 | |
| 2230 | /* |
| 2231 | * When debugging, it's easier to use __GFP_ZERO here; but it's better |
| 2232 | * for latency not to zero a page while GFP_ATOMIC and holding locks. |
| 2233 | */ |
| 2234 | page = alloc_page(gfp_mask | __GFP_HIGHMEM); |
| 2235 | |
| 2236 | si = swap_info_get(entry); |
| 2237 | if (!si) { |
| 2238 | /* |
| 2239 | * An acceptable race has occurred since the failing |
| 2240 | * __swap_duplicate(): the swap entry has been freed, |
| 2241 | * perhaps even the whole swap_map cleared for swapoff. |
| 2242 | */ |
| 2243 | goto outer; |
| 2244 | } |
| 2245 | |
| 2246 | offset = swp_offset(entry); |
| 2247 | count = si->swap_map[offset] & ~SWAP_HAS_CACHE; |
| 2248 | |
| 2249 | if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) { |
| 2250 | /* |
| 2251 | * The higher the swap count, the more likely it is that tasks |
| 2252 | * will race to add swap count continuation: we need to avoid |
| 2253 | * over-provisioning. |
| 2254 | */ |
| 2255 | goto out; |
| 2256 | } |
| 2257 | |
| 2258 | if (!page) { |
| 2259 | spin_unlock(&swap_lock); |
| 2260 | return -ENOMEM; |
| 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * We are fortunate that although vmalloc_to_page uses pte_offset_map, |
| 2265 | * no architecture is using highmem pages for kernel pagetables: so it |
| 2266 | * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps. |
| 2267 | */ |
| 2268 | head = vmalloc_to_page(si->swap_map + offset); |
| 2269 | offset &= ~PAGE_MASK; |
| 2270 | |
| 2271 | /* |
| 2272 | * Page allocation does not initialize the page's lru field, |
| 2273 | * but it does always reset its private field. |
| 2274 | */ |
| 2275 | if (!page_private(head)) { |
| 2276 | BUG_ON(count & COUNT_CONTINUED); |
| 2277 | INIT_LIST_HEAD(&head->lru); |
| 2278 | set_page_private(head, SWP_CONTINUED); |
| 2279 | si->flags |= SWP_CONTINUED; |
| 2280 | } |
| 2281 | |
| 2282 | list_for_each_entry(list_page, &head->lru, lru) { |
| 2283 | unsigned char *map; |
| 2284 | |
| 2285 | /* |
| 2286 | * If the previous map said no continuation, but we've found |
| 2287 | * a continuation page, free our allocation and use this one. |
| 2288 | */ |
| 2289 | if (!(count & COUNT_CONTINUED)) |
| 2290 | goto out; |
| 2291 | |
| 2292 | map = kmap_atomic(list_page, KM_USER0) + offset; |
| 2293 | count = *map; |
| 2294 | kunmap_atomic(map, KM_USER0); |
| 2295 | |
| 2296 | /* |
| 2297 | * If this continuation count now has some space in it, |
| 2298 | * free our allocation and use this one. |
| 2299 | */ |
| 2300 | if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX) |
| 2301 | goto out; |
| 2302 | } |
| 2303 | |
| 2304 | list_add_tail(&page->lru, &head->lru); |
| 2305 | page = NULL; /* now it's attached, don't free it */ |
| 2306 | out: |
| 2307 | spin_unlock(&swap_lock); |
| 2308 | outer: |
| 2309 | if (page) |
| 2310 | __free_page(page); |
| 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | /* |
| 2315 | * swap_count_continued - when the original swap_map count is incremented |
| 2316 | * from SWAP_MAP_MAX, check if there is already a continuation page to carry |
| 2317 | * into, carry if so, or else fail until a new continuation page is allocated; |
| 2318 | * when the original swap_map count is decremented from 0 with continuation, |
| 2319 | * borrow from the continuation and report whether it still holds more. |
| 2320 | * Called while __swap_duplicate() or swap_entry_free() holds swap_lock. |
| 2321 | */ |
| 2322 | static bool swap_count_continued(struct swap_info_struct *si, |
| 2323 | pgoff_t offset, unsigned char count) |
| 2324 | { |
| 2325 | struct page *head; |
| 2326 | struct page *page; |
| 2327 | unsigned char *map; |
| 2328 | |
| 2329 | head = vmalloc_to_page(si->swap_map + offset); |
| 2330 | if (page_private(head) != SWP_CONTINUED) { |
| 2331 | BUG_ON(count & COUNT_CONTINUED); |
| 2332 | return false; /* need to add count continuation */ |
| 2333 | } |
| 2334 | |
| 2335 | offset &= ~PAGE_MASK; |
| 2336 | page = list_entry(head->lru.next, struct page, lru); |
| 2337 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2338 | |
| 2339 | if (count == SWAP_MAP_MAX) /* initial increment from swap_map */ |
| 2340 | goto init_map; /* jump over SWAP_CONT_MAX checks */ |
| 2341 | |
| 2342 | if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */ |
| 2343 | /* |
| 2344 | * Think of how you add 1 to 999 |
| 2345 | */ |
| 2346 | while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) { |
| 2347 | kunmap_atomic(map, KM_USER0); |
| 2348 | page = list_entry(page->lru.next, struct page, lru); |
| 2349 | BUG_ON(page == head); |
| 2350 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2351 | } |
| 2352 | if (*map == SWAP_CONT_MAX) { |
| 2353 | kunmap_atomic(map, KM_USER0); |
| 2354 | page = list_entry(page->lru.next, struct page, lru); |
| 2355 | if (page == head) |
| 2356 | return false; /* add count continuation */ |
| 2357 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2358 | init_map: *map = 0; /* we didn't zero the page */ |
| 2359 | } |
| 2360 | *map += 1; |
| 2361 | kunmap_atomic(map, KM_USER0); |
| 2362 | page = list_entry(page->lru.prev, struct page, lru); |
| 2363 | while (page != head) { |
| 2364 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2365 | *map = COUNT_CONTINUED; |
| 2366 | kunmap_atomic(map, KM_USER0); |
| 2367 | page = list_entry(page->lru.prev, struct page, lru); |
| 2368 | } |
| 2369 | return true; /* incremented */ |
| 2370 | |
| 2371 | } else { /* decrementing */ |
| 2372 | /* |
| 2373 | * Think of how you subtract 1 from 1000 |
| 2374 | */ |
| 2375 | BUG_ON(count != COUNT_CONTINUED); |
| 2376 | while (*map == COUNT_CONTINUED) { |
| 2377 | kunmap_atomic(map, KM_USER0); |
| 2378 | page = list_entry(page->lru.next, struct page, lru); |
| 2379 | BUG_ON(page == head); |
| 2380 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2381 | } |
| 2382 | BUG_ON(*map == 0); |
| 2383 | *map -= 1; |
| 2384 | if (*map == 0) |
| 2385 | count = 0; |
| 2386 | kunmap_atomic(map, KM_USER0); |
| 2387 | page = list_entry(page->lru.prev, struct page, lru); |
| 2388 | while (page != head) { |
| 2389 | map = kmap_atomic(page, KM_USER0) + offset; |
| 2390 | *map = SWAP_CONT_MAX | count; |
| 2391 | count = COUNT_CONTINUED; |
| 2392 | kunmap_atomic(map, KM_USER0); |
| 2393 | page = list_entry(page->lru.prev, struct page, lru); |
| 2394 | } |
| 2395 | return count == COUNT_CONTINUED; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | * free_swap_count_continuations - swapoff free all the continuation pages |
| 2401 | * appended to the swap_map, after swap_map is quiesced, before vfree'ing it. |
| 2402 | */ |
| 2403 | static void free_swap_count_continuations(struct swap_info_struct *si) |
| 2404 | { |
| 2405 | pgoff_t offset; |
| 2406 | |
| 2407 | for (offset = 0; offset < si->max; offset += PAGE_SIZE) { |
| 2408 | struct page *head; |
| 2409 | head = vmalloc_to_page(si->swap_map + offset); |
| 2410 | if (page_private(head)) { |
| 2411 | struct list_head *this, *next; |
| 2412 | list_for_each_safe(this, next, &head->lru) { |
| 2413 | struct page *page; |
| 2414 | page = list_entry(this, struct page, lru); |
| 2415 | list_del(this); |
| 2416 | __free_page(page); |
| 2417 | } |
| 2418 | } |
| 2419 | } |
| 2420 | } |