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