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