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