blob: 58bec66001673dbd6dd58dd4d7bf8fa1fc53b135 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -07008#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 Dickins20137a42009-01-06 14:39:54 -080019#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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 Molnarfc0abb12006-01-18 17:42:33 -080028#include <linux/mutex.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080029#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/syscalls.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/pgtable.h>
34#include <asm/tlbflush.h>
35#include <linux/swapops.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080036#include <linux/page_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Hugh Dickins570a3352009-12-14 17:58:46 -080038static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
39 unsigned char);
40static void free_swap_count_continuations(struct swap_info_struct *);
41
Adrian Bunk7c363b82008-07-25 19:46:24 -070042static DEFINE_SPINLOCK(swap_lock);
43static unsigned int nr_swapfiles;
Hugh Dickinsb9627162009-01-06 14:39:41 -080044long nr_swap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045long total_swap_pages;
Hugh Dickins78ecba02008-07-23 21:28:23 -070046static int least_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static const char Bad_file[] = "Bad swap file entry ";
49static const char Unused_file[] = "Unused swap file entry ";
50static const char Bad_offset[] = "Bad swap offset entry ";
51static const char Unused_offset[] = "Unused swap offset entry ";
52
Adrian Bunk7c363b82008-07-25 19:46:24 -070053static struct swap_list_t swap_list = {-1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Hugh Dickinsefa90a92009-12-14 17:58:41 -080055static struct swap_info_struct *swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ingo Molnarfc0abb12006-01-18 17:42:33 -080057static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Hugh Dickins8d69aae2009-12-14 17:58:45 -080059static inline unsigned char swap_count(unsigned char ent)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070060{
Hugh Dickins570a3352009-12-14 17:58:46 -080061 return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070062}
63
Hugh Dickinsefa90a92009-12-14 17:58:41 -080064/* returns 1 if swap entry is freed */
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070065static int
66__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
67{
Hugh Dickinsefa90a92009-12-14 17:58:41 -080068 swp_entry_t entry = swp_entry(si->type, offset);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070069 struct page *page;
70 int ret = 0;
71
72 page = find_get_page(&swapper_space, entry.val);
73 if (!page)
74 return 0;
75 /*
76 * This function is called from scan_swap_map() and it's called
77 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
78 * We have to use trylock for avoiding deadlock. This is a special
79 * case and you should use try_to_free_swap() with explicit lock_page()
80 * in usual operations.
81 */
82 if (trylock_page(page)) {
83 ret = try_to_free_swap(page);
84 unlock_page(page);
85 }
86 page_cache_release(page);
87 return ret;
88}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * We need this because the bdev->unplug_fn can sleep and we cannot
Hugh Dickins5d337b92005-09-03 15:54:41 -070092 * hold swap_lock while calling the unplug_fn. And swap_lock
Ingo Molnarfc0abb12006-01-18 17:42:33 -080093 * cannot be turned into a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95static DECLARE_RWSEM(swap_unplug_sem);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
98{
99 swp_entry_t entry;
100
101 down_read(&swap_unplug_sem);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700102 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (PageSwapCache(page)) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800104 struct block_device *bdev = swap_info[swp_type(entry)]->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct backing_dev_info *bdi;
106
107 /*
108 * If the page is removed from swapcache from under us (with a
109 * racy try_to_unuse/swapoff) we need an additional reference
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700110 * count to avoid reading garbage from page_private(page) above.
111 * If the WARN_ON triggers during a swapoff it maybe the race
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * condition and it's harmless. However if it triggers without
113 * swapoff it signals a problem.
114 */
115 WARN_ON(page_count(page) <= 1);
116
117 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
McMullan, Jasonba323112005-05-16 21:53:40 -0700118 blk_run_backing_dev(bdi, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 up_read(&swap_unplug_sem);
121}
122
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800123/*
124 * swapon tell device that all the old swap contents can be discarded,
125 * to allow the swap device to optimize its wear-levelling.
126 */
127static int discard_swap(struct swap_info_struct *si)
128{
129 struct swap_extent *se;
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800130 sector_t start_block;
131 sector_t nr_blocks;
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800132 int err = 0;
133
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800134 /* Do not discard the swap header page! */
135 se = &si->first_swap_extent;
136 start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
137 nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
138 if (nr_blocks) {
139 err = blkdev_issue_discard(si->bdev, start_block,
140 nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
141 if (err)
142 return err;
143 cond_resched();
144 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800145
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800146 list_for_each_entry(se, &si->first_swap_extent.list, list) {
147 start_block = se->start_block << (PAGE_SHIFT - 9);
148 nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800149
150 err = blkdev_issue_discard(si->bdev, start_block,
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800151 nr_blocks, GFP_KERNEL, DISCARD_FL_BARRIER);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800152 if (err)
153 break;
154
155 cond_resched();
156 }
157 return err; /* That will often be -EOPNOTSUPP */
158}
159
Hugh Dickins7992fde2009-01-06 14:39:53 -0800160/*
161 * swap allocation tell device that a cluster of swap can now be discarded,
162 * to allow the swap device to optimize its wear-levelling.
163 */
164static void discard_swap_cluster(struct swap_info_struct *si,
165 pgoff_t start_page, pgoff_t nr_pages)
166{
167 struct swap_extent *se = si->curr_swap_extent;
168 int found_extent = 0;
169
170 while (nr_pages) {
171 struct list_head *lh;
172
173 if (se->start_page <= start_page &&
174 start_page < se->start_page + se->nr_pages) {
175 pgoff_t offset = start_page - se->start_page;
176 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800177 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800178
179 if (nr_blocks > nr_pages)
180 nr_blocks = nr_pages;
181 start_page += nr_blocks;
182 nr_pages -= nr_blocks;
183
184 if (!found_extent++)
185 si->curr_swap_extent = se;
186
187 start_block <<= PAGE_SHIFT - 9;
188 nr_blocks <<= PAGE_SHIFT - 9;
189 if (blkdev_issue_discard(si->bdev, start_block,
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800190 nr_blocks, GFP_NOIO, DISCARD_FL_BARRIER))
Hugh Dickins7992fde2009-01-06 14:39:53 -0800191 break;
192 }
193
194 lh = se->list.next;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800195 se = list_entry(lh, struct swap_extent, list);
196 }
197}
198
199static int wait_for_discard(void *word)
200{
201 schedule();
202 return 0;
203}
204
Hugh Dickins048c27f2005-09-03 15:54:40 -0700205#define SWAPFILE_CLUSTER 256
206#define LATENCY_LIMIT 256
207
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700208static inline unsigned long scan_swap_map(struct swap_info_struct *si,
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800209 unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800211 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800212 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800213 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700214 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800215 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800217 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700218 * We try to cluster swap pages by allocating them sequentially
219 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
220 * way, however, we resort to first-free allocation, starting
221 * a new cluster. This prevents us from scattering swap pages
222 * all over the entire swap partition, so that we reduce
223 * overall disk seek times between swap pages. -- sct
224 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800225 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700226 */
227
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700228 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800229 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800230
231 if (unlikely(!si->cluster_nr--)) {
232 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
233 si->cluster_nr = SWAPFILE_CLUSTER - 1;
234 goto checks;
235 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800236 if (si->flags & SWP_DISCARDABLE) {
237 /*
238 * Start range check on racing allocations, in case
239 * they overlap the cluster we eventually decide on
240 * (we scan without swap_lock to allow preemption).
241 * It's hardly conceivable that cluster_nr could be
242 * wrapped during our scan, but don't depend on it.
243 */
244 if (si->lowest_alloc)
245 goto checks;
246 si->lowest_alloc = si->max;
247 si->highest_alloc = 0;
248 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700249 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700250
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800251 /*
252 * If seek is expensive, start searching for new cluster from
253 * start of partition, to minimize the span of allocated swap.
254 * But if seek is cheap, search from our current position, so
255 * that swap is allocated from all over the partition: if the
256 * Flash Translation Layer only remaps within limited zones,
257 * we don't want to wear out the first zone too quickly.
258 */
259 if (!(si->flags & SWP_SOLIDSTATE))
260 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700261 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
262
263 /* Locate the first empty (unaligned) cluster */
264 for (; last_in_cluster <= si->highest_bit; offset++) {
265 if (si->swap_map[offset])
266 last_in_cluster = offset + SWAPFILE_CLUSTER;
267 else if (offset == last_in_cluster) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700268 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800269 offset -= SWAPFILE_CLUSTER - 1;
270 si->cluster_next = offset;
271 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800272 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800273 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700275 if (unlikely(--latency_ration < 0)) {
276 cond_resched();
277 latency_ration = LATENCY_LIMIT;
278 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700279 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800280
281 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800282 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
283
284 /* Locate the first empty (unaligned) cluster */
285 for (; last_in_cluster < scan_base; offset++) {
286 if (si->swap_map[offset])
287 last_in_cluster = offset + SWAPFILE_CLUSTER;
288 else if (offset == last_in_cluster) {
289 spin_lock(&swap_lock);
290 offset -= SWAPFILE_CLUSTER - 1;
291 si->cluster_next = offset;
292 si->cluster_nr = SWAPFILE_CLUSTER - 1;
293 found_free_cluster = 1;
294 goto checks;
295 }
296 if (unlikely(--latency_ration < 0)) {
297 cond_resched();
298 latency_ration = LATENCY_LIMIT;
299 }
300 }
301
302 offset = scan_base;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700303 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800304 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800305 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700307
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800308checks:
309 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700310 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700311 if (!si->highest_bit)
312 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800313 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800314 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700315
316 /* reuse swap entry of cache-only swap if not busy. */
317 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
318 int swap_was_freed;
319 spin_unlock(&swap_lock);
320 swap_was_freed = __try_to_reclaim_swap(si, offset);
321 spin_lock(&swap_lock);
322 /* entry was freed successfully, try to use this again */
323 if (swap_was_freed)
324 goto checks;
325 goto scan; /* check next one */
326 }
327
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800328 if (si->swap_map[offset])
329 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700330
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800331 if (offset == si->lowest_bit)
332 si->lowest_bit++;
333 if (offset == si->highest_bit)
334 si->highest_bit--;
335 si->inuse_pages++;
336 if (si->inuse_pages == si->pages) {
337 si->lowest_bit = si->max;
338 si->highest_bit = 0;
339 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800340 si->swap_map[offset] = usage;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800341 si->cluster_next = offset + 1;
342 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800343
344 if (si->lowest_alloc) {
345 /*
346 * Only set when SWP_DISCARDABLE, and there's a scan
347 * for a free cluster in progress or just completed.
348 */
349 if (found_free_cluster) {
350 /*
351 * To optimize wear-levelling, discard the
352 * old data of the cluster, taking care not to
353 * discard any of its pages that have already
354 * been allocated by racing tasks (offset has
355 * already stepped over any at the beginning).
356 */
357 if (offset < si->highest_alloc &&
358 si->lowest_alloc <= last_in_cluster)
359 last_in_cluster = si->lowest_alloc - 1;
360 si->flags |= SWP_DISCARDING;
361 spin_unlock(&swap_lock);
362
363 if (offset < last_in_cluster)
364 discard_swap_cluster(si, offset,
365 last_in_cluster - offset + 1);
366
367 spin_lock(&swap_lock);
368 si->lowest_alloc = 0;
369 si->flags &= ~SWP_DISCARDING;
370
371 smp_mb(); /* wake_up_bit advises this */
372 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
373
374 } else if (si->flags & SWP_DISCARDING) {
375 /*
376 * Delay using pages allocated by racing tasks
377 * until the whole discard has been issued. We
378 * could defer that delay until swap_writepage,
379 * but it's easier to keep this self-contained.
380 */
381 spin_unlock(&swap_lock);
382 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
383 wait_for_discard, TASK_UNINTERRUPTIBLE);
384 spin_lock(&swap_lock);
385 } else {
386 /*
387 * Note pages allocated by racing tasks while
388 * scan for a free cluster is in progress, so
389 * that its final discard can exclude them.
390 */
391 if (offset < si->lowest_alloc)
392 si->lowest_alloc = offset;
393 if (offset > si->highest_alloc)
394 si->highest_alloc = offset;
395 }
396 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800397 return offset;
398
399scan:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700400 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700401 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700402 if (!si->swap_map[offset]) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700403 spin_lock(&swap_lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700404 goto checks;
405 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700406 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
407 spin_lock(&swap_lock);
408 goto checks;
409 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700410 if (unlikely(--latency_ration < 0)) {
411 cond_resched();
412 latency_ration = LATENCY_LIMIT;
413 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700414 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800415 offset = si->lowest_bit;
416 while (++offset < scan_base) {
417 if (!si->swap_map[offset]) {
418 spin_lock(&swap_lock);
419 goto checks;
420 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700421 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
422 spin_lock(&swap_lock);
423 goto checks;
424 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800425 if (unlikely(--latency_ration < 0)) {
426 cond_resched();
427 latency_ration = LATENCY_LIMIT;
428 }
429 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700430 spin_lock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700431
432no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700433 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435}
436
437swp_entry_t get_swap_page(void)
438{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700439 struct swap_info_struct *si;
440 pgoff_t offset;
441 int type, next;
442 int wrapped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Hugh Dickins5d337b92005-09-03 15:54:41 -0700444 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (nr_swap_pages <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700446 goto noswap;
447 nr_swap_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700449 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800450 si = swap_info[type];
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700451 next = si->next;
452 if (next < 0 ||
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800453 (!wrapped && si->prio != swap_info[next]->prio)) {
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700454 next = swap_list.head;
455 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700457
458 if (!si->highest_bit)
459 continue;
460 if (!(si->flags & SWP_WRITEOK))
461 continue;
462
463 swap_list.next = next;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700464 /* This is called for allocating swap entry for cache */
Hugh Dickins253d5532009-12-14 17:58:44 -0800465 offset = scan_swap_map(si, SWAP_HAS_CACHE);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700466 if (offset) {
467 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700468 return swp_entry(type, offset);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700469 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700470 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700472
473 nr_swap_pages++;
474noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700475 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700476 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700479/* The only caller of this function is now susupend routine */
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800480swp_entry_t get_swap_page_of_type(int type)
481{
482 struct swap_info_struct *si;
483 pgoff_t offset;
484
485 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800486 si = swap_info[type];
487 if (si && (si->flags & SWP_WRITEOK)) {
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800488 nr_swap_pages--;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700489 /* This is called for allocating swap entry, not cache */
Hugh Dickins253d5532009-12-14 17:58:44 -0800490 offset = scan_swap_map(si, 1);
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800491 if (offset) {
492 spin_unlock(&swap_lock);
493 return swp_entry(type, offset);
494 }
495 nr_swap_pages++;
496 }
497 spin_unlock(&swap_lock);
498 return (swp_entry_t) {0};
499}
500
Hugh Dickins73c34b62009-12-14 17:58:43 -0800501static struct swap_info_struct *swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800503 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned long offset, type;
505
506 if (!entry.val)
507 goto out;
508 type = swp_type(entry);
509 if (type >= nr_swapfiles)
510 goto bad_nofile;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800511 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!(p->flags & SWP_USED))
513 goto bad_device;
514 offset = swp_offset(entry);
515 if (offset >= p->max)
516 goto bad_offset;
517 if (!p->swap_map[offset])
518 goto bad_free;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700519 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return p;
521
522bad_free:
523 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
524 goto out;
525bad_offset:
526 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
527 goto out;
528bad_device:
529 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
530 goto out;
531bad_nofile:
532 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
533out:
534 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800537static unsigned char swap_entry_free(struct swap_info_struct *p,
538 swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Hugh Dickins253d5532009-12-14 17:58:44 -0800540 unsigned long offset = swp_offset(entry);
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800541 unsigned char count;
542 unsigned char has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700544 count = p->swap_map[offset];
Hugh Dickins253d5532009-12-14 17:58:44 -0800545 has_cache = count & SWAP_HAS_CACHE;
546 count &= ~SWAP_HAS_CACHE;
547
548 if (usage == SWAP_HAS_CACHE) {
549 VM_BUG_ON(!has_cache);
550 has_cache = 0;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800551 } else if (count == SWAP_MAP_SHMEM) {
552 /*
553 * Or we could insist on shmem.c using a special
554 * swap_shmem_free() and free_shmem_swap_and_cache()...
555 */
556 count = 0;
Hugh Dickins570a3352009-12-14 17:58:46 -0800557 } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
558 if (count == COUNT_CONTINUED) {
559 if (swap_count_continued(p, offset, count))
560 count = SWAP_MAP_MAX | COUNT_CONTINUED;
561 else
562 count = SWAP_MAP_MAX;
563 } else
564 count--;
565 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800566
567 if (!count)
568 mem_cgroup_uncharge_swap(entry);
569
570 usage = count | has_cache;
571 p->swap_map[offset] = usage;
572
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700573 /* free if no reference */
Hugh Dickins253d5532009-12-14 17:58:44 -0800574 if (!usage) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700575 if (offset < p->lowest_bit)
576 p->lowest_bit = offset;
577 if (offset > p->highest_bit)
578 p->highest_bit = offset;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800579 if (swap_list.next >= 0 &&
580 p->prio > swap_info[swap_list.next]->prio)
581 swap_list.next = p->type;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700582 nr_swap_pages++;
583 p->inuse_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800585
586 return usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Caller has made sure that the swapdevice corresponding to entry
591 * is still around or has not been recycled.
592 */
593void swap_free(swp_entry_t entry)
594{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800595 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 p = swap_info_get(entry);
598 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800599 swap_entry_free(p, entry, 1);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700600 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602}
603
604/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700605 * Called after dropping swapcache to decrease refcnt to swap entries.
606 */
607void swapcache_free(swp_entry_t entry, struct page *page)
608{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700609 struct swap_info_struct *p;
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800610 unsigned char count;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700611
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700612 p = swap_info_get(entry);
613 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800614 count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
615 if (page)
616 mem_cgroup_uncharge_swapcache(page, entry, count != 0);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700617 spin_unlock(&swap_lock);
618 }
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700619}
620
621/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700622 * How many references to page are currently swapped out?
Hugh Dickins570a3352009-12-14 17:58:46 -0800623 * This does not give an exact answer when swap count is continued,
624 * but does include the high COUNT_CONTINUED flag to allow for that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700626static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700628 int count = 0;
629 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 swp_entry_t entry;
631
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700632 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 p = swap_info_get(entry);
634 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700635 count = swap_count(p->swap_map[swp_offset(entry)]);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700636 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700638 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800642 * We can write to an anon page without COW if there are no other references
643 * to it. And as a side-effect, free up its swap: because the old content
644 * on disk will never be read, and seeking back there to write new content
645 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800647int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700649 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Hugh Dickins51726b12009-01-06 14:39:25 -0800651 VM_BUG_ON(!PageLocked(page));
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700652 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800653 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700654 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800655 if (count == 1 && !PageWriteback(page)) {
656 delete_from_swap_cache(page);
657 SetPageDirty(page);
658 }
659 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700660 return count == 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800664 * If swap is getting full, or if there are no more mappings of this page,
665 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800667int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Hugh Dickins51726b12009-01-06 14:39:25 -0800669 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 if (!PageSwapCache(page))
672 return 0;
673 if (PageWriteback(page))
674 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800675 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return 0;
677
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800678 delete_from_swap_cache(page);
679 SetPageDirty(page);
680 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700681}
682
683/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * Free the swap entry like above, but also try to
685 * free the page cache entry if it is the last user.
686 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800687int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800689 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct page *page = NULL;
691
Andi Kleena7420aa2009-09-16 11:50:05 +0200692 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800693 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 p = swap_info_get(entry);
696 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800697 if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
Nick Piggin93fac702006-03-31 02:29:56 -0800698 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700699 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800700 page_cache_release(page);
701 page = NULL;
702 }
703 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700704 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800707 /*
708 * Not mapped elsewhere, or swap space full? Free it!
709 * Also recheck PageSwapCache now page is locked (above).
710 */
Nick Piggin93fac702006-03-31 02:29:56 -0800711 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800712 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 delete_from_swap_cache(page);
714 SetPageDirty(page);
715 }
716 unlock_page(page);
717 page_cache_release(page);
718 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800719 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200722#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800723/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800724 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800725 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800726 * @offset - number of the PAGE_SIZE-sized block of the device, starting
727 * from 0, in which the swap header is expected to be located.
728 *
729 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800730 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800731int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800732{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800733 struct block_device *bdev = NULL;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800734 int type;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800735
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800736 if (device)
737 bdev = bdget(device);
738
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800739 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800740 for (type = 0; type < nr_swapfiles; type++) {
741 struct swap_info_struct *sis = swap_info[type];
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800742
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800743 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800744 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700745
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800746 if (!bdev) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800747 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200748 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800749
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800750 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800751 return type;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800752 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800753 if (bdev == sis->bdev) {
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800754 struct swap_extent *se = &sis->first_swap_extent;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800755
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800756 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800757 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200758 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800759
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800760 spin_unlock(&swap_lock);
761 bdput(bdev);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800762 return type;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800763 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800764 }
765 }
766 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800767 if (bdev)
768 bdput(bdev);
769
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800770 return -ENODEV;
771}
772
773/*
Hugh Dickins73c34b62009-12-14 17:58:43 -0800774 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
775 * corresponding to given index in swap_info (swap type).
776 */
777sector_t swapdev_block(int type, pgoff_t offset)
778{
779 struct block_device *bdev;
780
781 if ((unsigned int)type >= nr_swapfiles)
782 return 0;
783 if (!(swap_info[type]->flags & SWP_WRITEOK))
784 return 0;
785 return map_swap_page(swp_entry(type, offset), &bdev);
786}
787
788/*
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800789 * Return either the total number of swap pages of given type, or the number
790 * of free pages of that type (depending on @free)
791 *
792 * This is needed for software suspend
793 */
794unsigned int count_swap_pages(int type, int free)
795{
796 unsigned int n = 0;
797
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800798 spin_lock(&swap_lock);
799 if ((unsigned int)type < nr_swapfiles) {
800 struct swap_info_struct *sis = swap_info[type];
801
802 if (sis->flags & SWP_WRITEOK) {
803 n = sis->pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800804 if (free)
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800805 n -= sis->inuse_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800806 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800807 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800808 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800809 return n;
810}
Hugh Dickins73c34b62009-12-14 17:58:43 -0800811#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700814 * No need to decide whether this PTE shares the swap entry with others,
815 * just let do_wp_page work it out if a write is requested later - to
816 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800818static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 unsigned long addr, swp_entry_t entry, struct page *page)
820{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800821 struct mem_cgroup *ptr = NULL;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800822 spinlock_t *ptl;
823 pte_t *pte;
824 int ret = 1;
825
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800826 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800827 ret = -ENOMEM;
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800828 goto out_nolock;
829 }
Hugh Dickins044d66c2008-02-07 00:14:04 -0800830
831 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
832 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
833 if (ret > 0)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800834 mem_cgroup_cancel_charge_swapin(ptr);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800835 ret = 0;
836 goto out;
837 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800838
Hugh Dickins42946212005-10-29 18:16:05 -0700839 inc_mm_counter(vma->vm_mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 get_page(page);
841 set_pte_at(vma->vm_mm, addr, pte,
842 pte_mkold(mk_pte(page, vma->vm_page_prot)));
843 page_add_anon_rmap(page, vma, addr);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800844 mem_cgroup_commit_charge_swapin(page, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 swap_free(entry);
846 /*
847 * Move the page to the active list so it is not
848 * immediately swapped out again after swapon.
849 */
850 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800851out:
852 pte_unmap_unlock(pte, ptl);
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800853out_nolock:
Hugh Dickins044d66c2008-02-07 00:14:04 -0800854 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
858 unsigned long addr, unsigned long end,
859 swp_entry_t entry, struct page *page)
860{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700862 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800863 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Hugh Dickins044d66c2008-02-07 00:14:04 -0800865 /*
866 * We don't actually need pte lock while scanning for swp_pte: since
867 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
868 * page table while we're scanning; though it could get zapped, and on
869 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
870 * of unmatched parts which look like swp_pte, so unuse_pte must
871 * recheck under pte lock. Scanning without pte lock lets it be
872 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
873 */
874 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 do {
876 /*
877 * swapoff spends a _lot_ of time in this loop!
878 * Test inline before going to call unuse_pte.
879 */
880 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800881 pte_unmap(pte);
882 ret = unuse_pte(vma, pmd, addr, entry, page);
883 if (ret)
884 goto out;
885 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800888 pte_unmap(pte - 1);
889out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
894 unsigned long addr, unsigned long end,
895 swp_entry_t entry, struct page *page)
896{
897 pmd_t *pmd;
898 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800899 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 pmd = pmd_offset(pud, addr);
902 do {
903 next = pmd_addr_end(addr, end);
904 if (pmd_none_or_clear_bad(pmd))
905 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800906 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
907 if (ret)
908 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } while (pmd++, addr = next, addr != end);
910 return 0;
911}
912
913static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
914 unsigned long addr, unsigned long end,
915 swp_entry_t entry, struct page *page)
916{
917 pud_t *pud;
918 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800919 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 pud = pud_offset(pgd, addr);
922 do {
923 next = pud_addr_end(addr, end);
924 if (pud_none_or_clear_bad(pud))
925 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800926 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
927 if (ret)
928 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } while (pud++, addr = next, addr != end);
930 return 0;
931}
932
933static int unuse_vma(struct vm_area_struct *vma,
934 swp_entry_t entry, struct page *page)
935{
936 pgd_t *pgd;
937 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800938 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 if (page->mapping) {
941 addr = page_address_in_vma(page, vma);
942 if (addr == -EFAULT)
943 return 0;
944 else
945 end = addr + PAGE_SIZE;
946 } else {
947 addr = vma->vm_start;
948 end = vma->vm_end;
949 }
950
951 pgd = pgd_offset(vma->vm_mm, addr);
952 do {
953 next = pgd_addr_end(addr, end);
954 if (pgd_none_or_clear_bad(pgd))
955 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800956 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
957 if (ret)
958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 } while (pgd++, addr = next, addr != end);
960 return 0;
961}
962
963static int unuse_mm(struct mm_struct *mm,
964 swp_entry_t entry, struct page *page)
965{
966 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800967 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (!down_read_trylock(&mm->mmap_sem)) {
970 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -0700971 * Activate page so shrink_inactive_list is unlikely to unmap
972 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700974 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 unlock_page(page);
976 down_read(&mm->mmap_sem);
977 lock_page(page);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800980 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800984 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987/*
988 * Scan swap_map from current position to next entry still in use.
989 * Recycle to start on reaching the end, returning 0 when empty.
990 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700991static unsigned int find_next_to_unuse(struct swap_info_struct *si,
992 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700994 unsigned int max = si->max;
995 unsigned int i = prev;
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800996 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -0700999 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * for whether an entry is in use, not modifying it; false
1001 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -07001002 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
1004 for (;;) {
1005 if (++i >= max) {
1006 if (!prev) {
1007 i = 0;
1008 break;
1009 }
1010 /*
1011 * No entries in use at top of swap_map,
1012 * loop back to start and recheck there.
1013 */
1014 max = prev + 1;
1015 prev = 0;
1016 i = 1;
1017 }
1018 count = si->swap_map[i];
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001019 if (count && swap_count(count) != SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021 }
1022 return i;
1023}
1024
1025/*
1026 * We completely avoid races by reading each swap page in advance,
1027 * and then search for the process using it. All the necessary
1028 * page table adjustments can then be made atomically.
1029 */
1030static int try_to_unuse(unsigned int type)
1031{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001032 struct swap_info_struct *si = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct mm_struct *start_mm;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001034 unsigned char *swap_map;
1035 unsigned char swcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct page *page;
1037 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001038 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
1042 * When searching mms for an entry, a good strategy is to
1043 * start at the first mm we freed the previous entry from
1044 * (though actually we don't notice whether we or coincidence
1045 * freed the entry). Initialize this start_mm with a hold.
1046 *
1047 * A simpler strategy would be to start at the last mm we
1048 * freed the previous entry from; but that would take less
1049 * advantage of mmlist ordering, which clusters forked mms
1050 * together, child after parent. If we race with dup_mmap(), we
1051 * prefer to resolve parent before child, lest we miss entries
1052 * duplicated after we scanned child: using last mm would invert
Hugh Dickins570a3352009-12-14 17:58:46 -08001053 * that.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
1055 start_mm = &init_mm;
1056 atomic_inc(&init_mm.mm_users);
1057
1058 /*
1059 * Keep on scanning until all entries have gone. Usually,
1060 * one pass through swap_map is enough, but not necessarily:
1061 * there are races when an instance of an entry might be missed.
1062 */
1063 while ((i = find_next_to_unuse(si, i)) != 0) {
1064 if (signal_pending(current)) {
1065 retval = -EINTR;
1066 break;
1067 }
1068
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001069 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * Get a page for the entry, using the existing swap
1071 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001072 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 */
1074 swap_map = &si->swap_map[i];
1075 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -08001076 page = read_swap_cache_async(entry,
1077 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!page) {
1079 /*
1080 * Either swap_duplicate() failed because entry
1081 * has been freed independently, and will not be
1082 * reused since sys_swapoff() already disabled
1083 * allocation from here, or alloc_page() failed.
1084 */
1085 if (!*swap_map)
1086 continue;
1087 retval = -ENOMEM;
1088 break;
1089 }
1090
1091 /*
1092 * Don't hold on to start_mm if it looks like exiting.
1093 */
1094 if (atomic_read(&start_mm->mm_users) == 1) {
1095 mmput(start_mm);
1096 start_mm = &init_mm;
1097 atomic_inc(&init_mm.mm_users);
1098 }
1099
1100 /*
1101 * Wait for and lock page. When do_swap_page races with
1102 * try_to_unuse, do_swap_page can handle the fault much
1103 * faster than try_to_unuse can locate the entry. This
1104 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1105 * defer to do_swap_page in such a case - in some tests,
1106 * do_swap_page and try_to_unuse repeatedly compete.
1107 */
1108 wait_on_page_locked(page);
1109 wait_on_page_writeback(page);
1110 lock_page(page);
1111 wait_on_page_writeback(page);
1112
1113 /*
1114 * Remove all references to entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 swcount = *swap_map;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001117 if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1118 retval = shmem_unuse(entry, page);
1119 /* page has already been unlocked and released */
1120 if (retval < 0)
1121 break;
1122 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001124 if (swap_count(swcount) && start_mm != &init_mm)
1125 retval = unuse_mm(start_mm, entry, page);
1126
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001127 if (swap_count(*swap_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int set_start_mm = (*swap_map >= swcount);
1129 struct list_head *p = &start_mm->mmlist;
1130 struct mm_struct *new_start_mm = start_mm;
1131 struct mm_struct *prev_mm = start_mm;
1132 struct mm_struct *mm;
1133
1134 atomic_inc(&new_start_mm->mm_users);
1135 atomic_inc(&prev_mm->mm_users);
1136 spin_lock(&mmlist_lock);
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001137 while (swap_count(*swap_map) && !retval &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 (p = p->next) != &start_mm->mmlist) {
1139 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001140 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 spin_unlock(&mmlist_lock);
1143 mmput(prev_mm);
1144 prev_mm = mm;
1145
1146 cond_resched();
1147
1148 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001149 if (!swap_count(swcount)) /* any usage ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001151 else if (mm == &init_mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 set_start_mm = 1;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001153 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 retval = unuse_mm(mm, entry, page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001155
Bo Liu32c5fc12009-11-02 16:50:33 +00001156 if (set_start_mm && *swap_map < swcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 mmput(new_start_mm);
1158 atomic_inc(&mm->mm_users);
1159 new_start_mm = mm;
1160 set_start_mm = 0;
1161 }
1162 spin_lock(&mmlist_lock);
1163 }
1164 spin_unlock(&mmlist_lock);
1165 mmput(prev_mm);
1166 mmput(start_mm);
1167 start_mm = new_start_mm;
1168 }
1169 if (retval) {
1170 unlock_page(page);
1171 page_cache_release(page);
1172 break;
1173 }
1174
1175 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 * If a reference remains (rare), we would like to leave
1177 * the page in the swap cache; but try_to_unmap could
1178 * then re-duplicate the entry once we drop page lock,
1179 * so we might loop indefinitely; also, that page could
1180 * not be swapped out to other storage meanwhile. So:
1181 * delete from cache even if there's another reference,
1182 * after ensuring that the data has been saved to disk -
1183 * since if the reference remains (rarer), it will be
1184 * read from disk into another page. Splitting into two
1185 * pages would be incorrect if swap supported "shared
1186 * private" pages, but they are handled by tmpfs files.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001188 if (swap_count(*swap_map) &&
1189 PageDirty(page) && PageSwapCache(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct writeback_control wbc = {
1191 .sync_mode = WB_SYNC_NONE,
1192 };
1193
1194 swap_writepage(page, &wbc);
1195 lock_page(page);
1196 wait_on_page_writeback(page);
1197 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001198
1199 /*
1200 * It is conceivable that a racing task removed this page from
1201 * swap cache just before we acquired the page lock at the top,
1202 * or while we dropped it in unuse_mm(). The page might even
1203 * be back in swap cache on another swap area: that we must not
1204 * delete, since it may not have been written out to swap yet.
1205 */
1206 if (PageSwapCache(page) &&
1207 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001208 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /*
1211 * So we could skip searching mms once swap count went
1212 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001213 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
1215 SetPageDirty(page);
1216 unlock_page(page);
1217 page_cache_release(page);
1218
1219 /*
1220 * Make sure that we aren't completely killing
1221 * interactive performance.
1222 */
1223 cond_resched();
1224 }
1225
1226 mmput(start_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return retval;
1228}
1229
1230/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001231 * After a successful try_to_unuse, if no swap is now in use, we know
1232 * we can empty the mmlist. swap_lock must be held on entry and exit.
1233 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * added to the mmlist just after page_duplicate - before would be racy.
1235 */
1236static void drain_mmlist(void)
1237{
1238 struct list_head *p, *next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001239 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001241 for (type = 0; type < nr_swapfiles; type++)
1242 if (swap_info[type]->inuse_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
1244 spin_lock(&mmlist_lock);
1245 list_for_each_safe(p, next, &init_mm.mmlist)
1246 list_del_init(p);
1247 spin_unlock(&mmlist_lock);
1248}
1249
1250/*
1251 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001252 * corresponds to page offset `offset'. Note that the type of this function
1253 * is sector_t, but it returns page offset into the bdev, not sector offset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001255sector_t map_swap_page(swp_entry_t entry, struct block_device **bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001257 struct swap_info_struct *sis;
1258 struct swap_extent *start_se;
1259 struct swap_extent *se;
1260 pgoff_t offset;
1261
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001262 sis = swap_info[swp_type(entry)];
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001263 *bdev = sis->bdev;
1264
1265 offset = swp_offset(entry);
1266 start_se = sis->curr_swap_extent;
1267 se = start_se;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 for ( ; ; ) {
1270 struct list_head *lh;
1271
1272 if (se->start_page <= offset &&
1273 offset < (se->start_page + se->nr_pages)) {
1274 return se->start_block + (offset - se->start_page);
1275 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001276 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 se = list_entry(lh, struct swap_extent, list);
1278 sis->curr_swap_extent = se;
1279 BUG_ON(se == start_se); /* It *must* be present */
1280 }
1281}
1282
1283/*
1284 * Free all of a swapdev's extent information
1285 */
1286static void destroy_swap_extents(struct swap_info_struct *sis)
1287{
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001288 while (!list_empty(&sis->first_swap_extent.list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct swap_extent *se;
1290
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001291 se = list_entry(sis->first_swap_extent.list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct swap_extent, list);
1293 list_del(&se->list);
1294 kfree(se);
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
1298/*
1299 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001300 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001302 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
1304static int
1305add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1306 unsigned long nr_pages, sector_t start_block)
1307{
1308 struct swap_extent *se;
1309 struct swap_extent *new_se;
1310 struct list_head *lh;
1311
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001312 if (start_page == 0) {
1313 se = &sis->first_swap_extent;
1314 sis->curr_swap_extent = se;
1315 se->start_page = 0;
1316 se->nr_pages = nr_pages;
1317 se->start_block = start_block;
1318 return 1;
1319 } else {
1320 lh = sis->first_swap_extent.list.prev; /* Highest extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001322 BUG_ON(se->start_page + se->nr_pages != start_page);
1323 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* Merge it */
1325 se->nr_pages += nr_pages;
1326 return 0;
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
1330 /*
1331 * No merge. Insert a new extent, preserving ordering.
1332 */
1333 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1334 if (new_se == NULL)
1335 return -ENOMEM;
1336 new_se->start_page = start_page;
1337 new_se->nr_pages = nr_pages;
1338 new_se->start_block = start_block;
1339
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001340 list_add_tail(&new_se->list, &sis->first_swap_extent.list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001341 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344/*
1345 * A `swap extent' is a simple thing which maps a contiguous range of pages
1346 * onto a contiguous range of disk blocks. An ordered list of swap extents
1347 * is built at swapon time and is then used at swap_writepage/swap_readpage
1348 * time for locating where on disk a page belongs.
1349 *
1350 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1351 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1352 * swap files identically.
1353 *
1354 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1355 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1356 * swapfiles are handled *identically* after swapon time.
1357 *
1358 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1359 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1360 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1361 * requirements, they are simply tossed out - we will never use those blocks
1362 * for swapping.
1363 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001364 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1366 * which will scribble on the fs.
1367 *
1368 * The amount of disk space which a single swap extent represents varies.
1369 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1370 * extents in the list. To avoid much list walking, we cache the previous
1371 * search location in `curr_swap_extent', and start new searches from there.
1372 * This is extremely effective. The average number of iterations in
1373 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1374 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001375static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 struct inode *inode;
1378 unsigned blocks_per_page;
1379 unsigned long page_no;
1380 unsigned blkbits;
1381 sector_t probe_block;
1382 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001383 sector_t lowest_block = -1;
1384 sector_t highest_block = 0;
1385 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int ret;
1387
1388 inode = sis->swap_file->f_mapping->host;
1389 if (S_ISBLK(inode->i_mode)) {
1390 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001391 *span = sis->pages;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001392 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
1395 blkbits = inode->i_blkbits;
1396 blocks_per_page = PAGE_SIZE >> blkbits;
1397
1398 /*
1399 * Map all the blocks into the extent list. This code doesn't try
1400 * to be very smart.
1401 */
1402 probe_block = 0;
1403 page_no = 0;
1404 last_block = i_size_read(inode) >> blkbits;
1405 while ((probe_block + blocks_per_page) <= last_block &&
1406 page_no < sis->max) {
1407 unsigned block_in_page;
1408 sector_t first_block;
1409
1410 first_block = bmap(inode, probe_block);
1411 if (first_block == 0)
1412 goto bad_bmap;
1413
1414 /*
1415 * It must be PAGE_SIZE aligned on-disk
1416 */
1417 if (first_block & (blocks_per_page - 1)) {
1418 probe_block++;
1419 goto reprobe;
1420 }
1421
1422 for (block_in_page = 1; block_in_page < blocks_per_page;
1423 block_in_page++) {
1424 sector_t block;
1425
1426 block = bmap(inode, probe_block + block_in_page);
1427 if (block == 0)
1428 goto bad_bmap;
1429 if (block != first_block + block_in_page) {
1430 /* Discontiguity */
1431 probe_block++;
1432 goto reprobe;
1433 }
1434 }
1435
Hugh Dickins53092a72005-09-03 15:54:34 -07001436 first_block >>= (PAGE_SHIFT - blkbits);
1437 if (page_no) { /* exclude the header page */
1438 if (first_block < lowest_block)
1439 lowest_block = first_block;
1440 if (first_block > highest_block)
1441 highest_block = first_block;
1442 }
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /*
1445 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1446 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001447 ret = add_swap_extent(sis, page_no, 1, first_block);
1448 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001450 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 page_no++;
1452 probe_block += blocks_per_page;
1453reprobe:
1454 continue;
1455 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001456 ret = nr_extents;
1457 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001459 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001461 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 sis->highest_bit = page_no - 1;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001463out:
1464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465bad_bmap:
1466 printk(KERN_ERR "swapon: swapfile has holes\n");
1467 ret = -EINVAL;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001471SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001473 struct swap_info_struct *p = NULL;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001474 unsigned char *swap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct file *swap_file, *victim;
1476 struct address_space *mapping;
1477 struct inode *inode;
Hugh Dickins73c34b62009-12-14 17:58:43 -08001478 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 int i, type, prev;
1480 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (!capable(CAP_SYS_ADMIN))
1483 return -EPERM;
1484
1485 pathname = getname(specialfile);
1486 err = PTR_ERR(pathname);
1487 if (IS_ERR(pathname))
1488 goto out;
1489
1490 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1491 putname(pathname);
1492 err = PTR_ERR(victim);
1493 if (IS_ERR(victim))
1494 goto out;
1495
1496 mapping = victim->f_mapping;
1497 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001498 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001499 for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1500 p = swap_info[type];
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001501 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (p->swap_file->f_mapping == mapping)
1503 break;
1504 }
1505 prev = type;
1506 }
1507 if (type < 0) {
1508 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001509 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 goto out_dput;
1511 }
1512 if (!security_vm_enough_memory(p->pages))
1513 vm_unacct_memory(p->pages);
1514 else {
1515 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001516 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto out_dput;
1518 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001519 if (prev < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 swap_list.head = p->next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001521 else
1522 swap_info[prev]->next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (type == swap_list.next) {
1524 /* just pick something that's safe... */
1525 swap_list.next = swap_list.head;
1526 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07001527 if (p->prio < 0) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001528 for (i = p->next; i >= 0; i = swap_info[i]->next)
1529 swap_info[i]->prio = p->prio--;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001530 least_priority++;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 nr_swap_pages -= p->pages;
1533 total_swap_pages -= p->pages;
1534 p->flags &= ~SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001535 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001536
Hugh Dickins35451be2009-09-21 17:02:27 -07001537 current->flags |= PF_OOM_ORIGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 err = try_to_unuse(type);
Hugh Dickins35451be2009-09-21 17:02:27 -07001539 current->flags &= ~PF_OOM_ORIGIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (err) {
1542 /* re-insert swap space back into swap_list */
Hugh Dickins5d337b92005-09-03 15:54:41 -07001543 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001544 if (p->prio < 0)
1545 p->prio = --least_priority;
1546 prev = -1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001547 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1548 if (p->prio >= swap_info[i]->prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 break;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001550 prev = i;
1551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 p->next = i;
1553 if (prev < 0)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001554 swap_list.head = swap_list.next = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 else
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001556 swap_info[prev]->next = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 nr_swap_pages += p->pages;
1558 total_swap_pages += p->pages;
1559 p->flags |= SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001560 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 goto out_dput;
1562 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001563
1564 /* wait for any unplug function to finish */
1565 down_write(&swap_unplug_sem);
1566 up_write(&swap_unplug_sem);
1567
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001568 destroy_swap_extents(p);
Hugh Dickins570a3352009-12-14 17:58:46 -08001569 if (p->flags & SWP_CONTINUED)
1570 free_swap_count_continuations(p);
1571
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001572 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001573 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001575
1576 /* wait for anyone still in scan_swap_map */
1577 p->highest_bit = 0; /* cuts scans short */
1578 while (p->flags >= SWP_SCANNING) {
1579 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001580 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001581 spin_lock(&swap_lock);
1582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 swap_file = p->swap_file;
1585 p->swap_file = NULL;
1586 p->max = 0;
1587 swap_map = p->swap_map;
1588 p->swap_map = NULL;
1589 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001590 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001591 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001593 /* Destroy swap account informatin */
1594 swap_cgroup_swapoff(type);
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 inode = mapping->host;
1597 if (S_ISBLK(inode->i_mode)) {
1598 struct block_device *bdev = I_BDEV(inode);
1599 set_blocksize(bdev, p->old_block_size);
1600 bd_release(bdev);
1601 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001602 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001604 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 }
1606 filp_close(swap_file, NULL);
1607 err = 0;
1608
1609out_dput:
1610 filp_close(victim, NULL);
1611out:
1612 return err;
1613}
1614
1615#ifdef CONFIG_PROC_FS
1616/* iterator */
1617static void *swap_start(struct seq_file *swap, loff_t *pos)
1618{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001619 struct swap_info_struct *si;
1620 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 loff_t l = *pos;
1622
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001623 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001625 if (!l)
1626 return SEQ_START_TOKEN;
1627
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001628 for (type = 0; type < nr_swapfiles; type++) {
1629 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1630 si = swap_info[type];
1631 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001633 if (!--l)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001634 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 return NULL;
1638}
1639
1640static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1641{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001642 struct swap_info_struct *si = v;
1643 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001645 if (v == SEQ_START_TOKEN)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001646 type = 0;
1647 else
1648 type = si->type + 1;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001649
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001650 for (; type < nr_swapfiles; type++) {
1651 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1652 si = swap_info[type];
1653 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 continue;
1655 ++*pos;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001656 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 }
1658
1659 return NULL;
1660}
1661
1662static void swap_stop(struct seq_file *swap, void *v)
1663{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001664 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
1667static int swap_show(struct seq_file *swap, void *v)
1668{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001669 struct swap_info_struct *si = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct file *file;
1671 int len;
1672
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001673 if (si == SEQ_START_TOKEN) {
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001674 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1675 return 0;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001678 file = si->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001679 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001680 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001681 len < 40 ? 40 - len : 1, " ",
1682 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 "partition" : "file\t",
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001684 si->pages << (PAGE_SHIFT - 10),
1685 si->inuse_pages << (PAGE_SHIFT - 10),
1686 si->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688}
1689
Helge Deller15ad7cd2006-12-06 20:40:36 -08001690static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 .start = swap_start,
1692 .next = swap_next,
1693 .stop = swap_stop,
1694 .show = swap_show
1695};
1696
1697static int swaps_open(struct inode *inode, struct file *file)
1698{
1699 return seq_open(file, &swaps_op);
1700}
1701
Helge Deller15ad7cd2006-12-06 20:40:36 -08001702static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 .open = swaps_open,
1704 .read = seq_read,
1705 .llseek = seq_lseek,
1706 .release = seq_release,
1707};
1708
1709static int __init procswaps_init(void)
1710{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001711 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
1713}
1714__initcall(procswaps_init);
1715#endif /* CONFIG_PROC_FS */
1716
Jan Beulich17963162008-12-16 11:35:24 +00001717#ifdef MAX_SWAPFILES_CHECK
1718static int __init max_swapfiles_check(void)
1719{
1720 MAX_SWAPFILES_CHECK();
1721 return 0;
1722}
1723late_initcall(max_swapfiles_check);
1724#endif
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*
1727 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1728 *
1729 * The swapon system call
1730 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001731SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001733 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 char *name = NULL;
1735 struct block_device *bdev = NULL;
1736 struct file *swap_file = NULL;
1737 struct address_space *mapping;
1738 unsigned int type;
1739 int i, prev;
1740 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 union swap_header *swap_header = NULL;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001742 unsigned int nr_good_pages = 0;
1743 int nr_extents = 0;
Hugh Dickins53092a72005-09-03 15:54:34 -07001744 sector_t span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 unsigned long maxpages = 1;
Hugh Dickins73fd8742009-01-06 14:39:47 -08001746 unsigned long swapfilepages;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001747 unsigned char *swap_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 struct page *page = NULL;
1749 struct inode *inode = NULL;
1750 int did_down = 0;
1751
1752 if (!capable(CAP_SYS_ADMIN))
1753 return -EPERM;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001754
1755 p = kzalloc(sizeof(*p), GFP_KERNEL);
1756 if (!p)
1757 return -ENOMEM;
1758
Hugh Dickins5d337b92005-09-03 15:54:41 -07001759 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001760 for (type = 0; type < nr_swapfiles; type++) {
1761 if (!(swap_info[type]->flags & SWP_USED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 break;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 error = -EPERM;
Christoph Lameter06972122006-06-23 02:03:35 -07001765 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001766 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001767 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto out;
1769 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001770 if (type >= nr_swapfiles) {
1771 p->type = type;
1772 swap_info[type] = p;
1773 /*
1774 * Write swap_info[type] before nr_swapfiles, in case a
1775 * racing procfs swap_start() or swap_next() is reading them.
1776 * (We never shrink nr_swapfiles, we never free this entry.)
1777 */
1778 smp_wmb();
1779 nr_swapfiles++;
1780 } else {
1781 kfree(p);
1782 p = swap_info[type];
1783 /*
1784 * Do not memset this entry: a racing procfs swap_next()
1785 * would be relying on p->type to remain valid.
1786 */
1787 }
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001788 INIT_LIST_HEAD(&p->first_swap_extent.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001791 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 name = getname(specialfile);
1794 error = PTR_ERR(name);
1795 if (IS_ERR(name)) {
1796 name = NULL;
1797 goto bad_swap_2;
1798 }
1799 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1800 error = PTR_ERR(swap_file);
1801 if (IS_ERR(swap_file)) {
1802 swap_file = NULL;
1803 goto bad_swap_2;
1804 }
1805
1806 p->swap_file = swap_file;
1807 mapping = swap_file->f_mapping;
1808 inode = mapping->host;
1809
1810 error = -EBUSY;
1811 for (i = 0; i < nr_swapfiles; i++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001812 struct swap_info_struct *q = swap_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 if (i == type || !q->swap_file)
1815 continue;
1816 if (mapping == q->swap_file->f_mapping)
1817 goto bad_swap;
1818 }
1819
1820 error = -EINVAL;
1821 if (S_ISBLK(inode->i_mode)) {
1822 bdev = I_BDEV(inode);
1823 error = bd_claim(bdev, sys_swapon);
1824 if (error < 0) {
1825 bdev = NULL;
Rob Landleyf7b3a432005-09-22 21:44:27 -07001826 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 goto bad_swap;
1828 }
1829 p->old_block_size = block_size(bdev);
1830 error = set_blocksize(bdev, PAGE_SIZE);
1831 if (error < 0)
1832 goto bad_swap;
1833 p->bdev = bdev;
1834 } else if (S_ISREG(inode->i_mode)) {
1835 p->bdev = inode->i_sb->s_bdev;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001836 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 did_down = 1;
1838 if (IS_SWAPFILE(inode)) {
1839 error = -EBUSY;
1840 goto bad_swap;
1841 }
1842 } else {
1843 goto bad_swap;
1844 }
1845
Hugh Dickins73fd8742009-01-06 14:39:47 -08001846 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /*
1849 * Read the swap header.
1850 */
1851 if (!mapping->a_ops->readpage) {
1852 error = -EINVAL;
1853 goto bad_swap;
1854 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07001855 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (IS_ERR(page)) {
1857 error = PTR_ERR(page);
1858 goto bad_swap;
1859 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001860 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Hugh Dickins81e33972009-01-06 14:39:49 -08001862 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Jesper Juhle97a3112006-01-11 01:50:28 +01001863 printk(KERN_ERR "Unable to find swap-space signature\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 error = -EINVAL;
1865 goto bad_swap;
1866 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001867
Hugh Dickins81e33972009-01-06 14:39:49 -08001868 /* swap partition endianess hack... */
1869 if (swab32(swap_header->info.version) == 1) {
1870 swab32s(&swap_header->info.version);
1871 swab32s(&swap_header->info.last_page);
1872 swab32s(&swap_header->info.nr_badpages);
1873 for (i = 0; i < swap_header->info.nr_badpages; i++)
1874 swab32s(&swap_header->info.badpages[i]);
1875 }
1876 /* Check the swap header's sub-version */
1877 if (swap_header->info.version != 1) {
1878 printk(KERN_WARNING
1879 "Unable to handle swap header version %d\n",
1880 swap_header->info.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 error = -EINVAL;
1882 goto bad_swap;
Hugh Dickins81e33972009-01-06 14:39:49 -08001883 }
1884
1885 p->lowest_bit = 1;
1886 p->cluster_next = 1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001887 p->cluster_nr = 0;
Hugh Dickins81e33972009-01-06 14:39:49 -08001888
1889 /*
1890 * Find out how many pages are allowed for a single swap
1891 * device. There are two limiting factors: 1) the number of
1892 * bits for the swap offset in the swp_entry_t type and
1893 * 2) the number of bits in the a swap pte as defined by
1894 * the different architectures. In order to find the
1895 * largest possible bit mask a swap entry with swap type 0
1896 * and swap offset ~0UL is created, encoded to a swap pte,
1897 * decoded to a swp_entry_t again and finally the swap
1898 * offset is extracted. This will mask all the bits from
1899 * the initial ~0UL mask that can't be encoded in either
1900 * the swp_entry_t or the architecture definition of a
1901 * swap pte.
1902 */
1903 maxpages = swp_offset(pte_to_swp_entry(
1904 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1905 if (maxpages > swap_header->info.last_page)
1906 maxpages = swap_header->info.last_page;
1907 p->highest_bit = maxpages - 1;
1908
1909 error = -EINVAL;
1910 if (!maxpages)
1911 goto bad_swap;
1912 if (swapfilepages && maxpages > swapfilepages) {
1913 printk(KERN_WARNING
1914 "Swap area shorter than signature indicates\n");
1915 goto bad_swap;
1916 }
1917 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1918 goto bad_swap;
1919 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1920 goto bad_swap;
1921
1922 /* OK, set up the swap map and apply the bad block list */
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001923 swap_map = vmalloc(maxpages);
Hugh Dickins81e33972009-01-06 14:39:49 -08001924 if (!swap_map) {
1925 error = -ENOMEM;
1926 goto bad_swap;
1927 }
1928
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001929 memset(swap_map, 0, maxpages);
Hugh Dickins81e33972009-01-06 14:39:49 -08001930 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1931 int page_nr = swap_header->info.badpages[i];
1932 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 error = -EINVAL;
1934 goto bad_swap;
1935 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001936 swap_map[page_nr] = SWAP_MAP_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001938
1939 error = swap_cgroup_swapon(type, maxpages);
1940 if (error)
1941 goto bad_swap;
1942
Hugh Dickins81e33972009-01-06 14:39:49 -08001943 nr_good_pages = swap_header->info.last_page -
1944 swap_header->info.nr_badpages -
1945 1 /* header page */;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001946
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001947 if (nr_good_pages) {
Hugh Dickins78ecba02008-07-23 21:28:23 -07001948 swap_map[0] = SWAP_MAP_BAD;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001949 p->max = maxpages;
1950 p->pages = nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001951 nr_extents = setup_swap_extents(p, &span);
1952 if (nr_extents < 0) {
1953 error = nr_extents;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001954 goto bad_swap;
Hugh Dickins53092a72005-09-03 15:54:34 -07001955 }
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001956 nr_good_pages = p->pages;
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (!nr_good_pages) {
1959 printk(KERN_WARNING "Empty swap-file\n");
1960 error = -EINVAL;
1961 goto bad_swap;
1962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Suresh Jayaraman3bd0f0c2009-09-30 10:53:48 +02001964 if (p->bdev) {
1965 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1966 p->flags |= SWP_SOLIDSTATE;
1967 p->cluster_next = 1 + (random32() % p->highest_bit);
1968 }
1969 if (discard_swap(p) == 0)
1970 p->flags |= SWP_DISCARDABLE;
Hugh Dickins20137a42009-01-06 14:39:54 -08001971 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001972
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001973 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001974 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001975 if (swap_flags & SWAP_FLAG_PREFER)
1976 p->prio =
1977 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1978 else
1979 p->prio = --least_priority;
1980 p->swap_map = swap_map;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001981 p->flags |= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 nr_swap_pages += nr_good_pages;
1983 total_swap_pages += nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001984
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001985 printk(KERN_INFO "Adding %uk swap on %s. "
Hugh Dickins20137a42009-01-06 14:39:54 -08001986 "Priority:%d extents:%d across:%lluk %s%s\n",
Hugh Dickins53092a72005-09-03 15:54:34 -07001987 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001988 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
Hugh Dickins20137a42009-01-06 14:39:54 -08001989 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
1990 (p->flags & SWP_DISCARDABLE) ? "D" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* insert swap space into swap_list: */
1993 prev = -1;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001994 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1995 if (p->prio >= swap_info[i]->prio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 prev = i;
1998 }
1999 p->next = i;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002000 if (prev < 0)
2001 swap_list.head = swap_list.next = type;
2002 else
2003 swap_info[prev]->next = type;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002004 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002005 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 error = 0;
2007 goto out;
2008bad_swap:
2009 if (bdev) {
2010 set_blocksize(bdev, p->old_block_size);
2011 bd_release(bdev);
2012 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002013 destroy_swap_extents(p);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002014 swap_cgroup_swapoff(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015bad_swap_2:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002016 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002019 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 vfree(swap_map);
2021 if (swap_file)
2022 filp_close(swap_file, NULL);
2023out:
2024 if (page && !IS_ERR(page)) {
2025 kunmap(page);
2026 page_cache_release(page);
2027 }
2028 if (name)
2029 putname(name);
2030 if (did_down) {
2031 if (!error)
2032 inode->i_flags |= S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002033 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 return error;
2036}
2037
2038void si_swapinfo(struct sysinfo *val)
2039{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002040 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 unsigned long nr_to_be_unused = 0;
2042
Hugh Dickins5d337b92005-09-03 15:54:41 -07002043 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002044 for (type = 0; type < nr_swapfiles; type++) {
2045 struct swap_info_struct *si = swap_info[type];
2046
2047 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2048 nr_to_be_unused += si->inuse_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 val->freeswap = nr_swap_pages + nr_to_be_unused;
2051 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002052 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
2055/*
2056 * Verify that a swap entry is valid and increment its swap map count.
2057 *
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002058 * Returns error code in following case.
2059 * - success -> 0
2060 * - swp_entry is invalid -> EINVAL
2061 * - swp_entry is migration entry -> EINVAL
2062 * - swap-cache reference is requested but there is already one. -> EEXIST
2063 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Hugh Dickins570a3352009-12-14 17:58:46 -08002064 * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 */
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002066static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002068 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 unsigned long offset, type;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002070 unsigned char count;
2071 unsigned char has_cache;
Hugh Dickins253d5532009-12-14 17:58:44 -08002072 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Andi Kleena7420aa2009-09-16 11:50:05 +02002074 if (non_swap_entry(entry))
Hugh Dickins253d5532009-12-14 17:58:44 -08002075 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -07002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 type = swp_type(entry);
2078 if (type >= nr_swapfiles)
2079 goto bad_file;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002080 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 offset = swp_offset(entry);
2082
Hugh Dickins5d337b92005-09-03 15:54:41 -07002083 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002084 if (unlikely(offset >= p->max))
2085 goto unlock_out;
2086
Hugh Dickins253d5532009-12-14 17:58:44 -08002087 count = p->swap_map[offset];
2088 has_cache = count & SWAP_HAS_CACHE;
2089 count &= ~SWAP_HAS_CACHE;
2090 err = 0;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002091
Hugh Dickins253d5532009-12-14 17:58:44 -08002092 if (usage == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002093
2094 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
Hugh Dickins253d5532009-12-14 17:58:44 -08002095 if (!has_cache && count)
2096 has_cache = SWAP_HAS_CACHE;
2097 else if (has_cache) /* someone else added cache */
2098 err = -EEXIST;
2099 else /* no users remaining */
2100 err = -ENOENT;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002101
2102 } else if (count || has_cache) {
Hugh Dickins253d5532009-12-14 17:58:44 -08002103
Hugh Dickins570a3352009-12-14 17:58:46 -08002104 if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2105 count += usage;
2106 else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
Hugh Dickins253d5532009-12-14 17:58:44 -08002107 err = -EINVAL;
Hugh Dickins570a3352009-12-14 17:58:46 -08002108 else if (swap_count_continued(p, offset, count))
2109 count = COUNT_CONTINUED;
2110 else
2111 err = -ENOMEM;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002112 } else
Hugh Dickins253d5532009-12-14 17:58:44 -08002113 err = -ENOENT; /* unused swap entry */
2114
2115 p->swap_map[offset] = count | has_cache;
2116
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002117unlock_out:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002118 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119out:
Hugh Dickins253d5532009-12-14 17:58:44 -08002120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122bad_file:
2123 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2124 goto out;
2125}
Hugh Dickins253d5532009-12-14 17:58:44 -08002126
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002127/*
Hugh Dickinsaaa46862009-12-14 17:58:47 -08002128 * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2129 * (in which case its reference count is never incremented).
2130 */
2131void swap_shmem_alloc(swp_entry_t entry)
2132{
2133 __swap_duplicate(entry, SWAP_MAP_SHMEM);
2134}
2135
2136/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002137 * increase reference count of swap entry by 1.
2138 */
Hugh Dickins570a3352009-12-14 17:58:46 -08002139int swap_duplicate(swp_entry_t entry)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002140{
Hugh Dickins570a3352009-12-14 17:58:46 -08002141 int err = 0;
2142
2143 while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2144 err = add_swap_count_continuation(entry, GFP_ATOMIC);
2145 return err;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002148/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002149 * @entry: swap entry for which we allocate swap cache.
2150 *
Hugh Dickins73c34b62009-12-14 17:58:43 -08002151 * Called when allocating swap cache for existing swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002152 * This can return error codes. Returns 0 at success.
2153 * -EBUSY means there is a swap cache.
2154 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002155 */
2156int swapcache_prepare(swp_entry_t entry)
2157{
Hugh Dickins253d5532009-12-14 17:58:44 -08002158 return __swap_duplicate(entry, SWAP_HAS_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002159}
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002162 * swap_lock prevents swap_map being freed. Don't grab an extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * reference on the swaphandle, it doesn't matter if it becomes unused.
2164 */
2165int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
2166{
Hugh Dickins89528982008-02-04 22:28:45 -08002167 struct swap_info_struct *si;
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002168 int our_page_cluster = page_cluster;
Hugh Dickins89528982008-02-04 22:28:45 -08002169 pgoff_t target, toff;
2170 pgoff_t base, end;
2171 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002173 if (!our_page_cluster) /* no readahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return 0;
Hugh Dickins89528982008-02-04 22:28:45 -08002175
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002176 si = swap_info[swp_type(entry)];
Hugh Dickins89528982008-02-04 22:28:45 -08002177 target = swp_offset(entry);
2178 base = (target >> our_page_cluster) << our_page_cluster;
2179 end = base + (1 << our_page_cluster);
2180 if (!base) /* first page is swap header */
2181 base++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Hugh Dickins5d337b92005-09-03 15:54:41 -07002183 spin_lock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002184 if (end > si->max) /* don't go beyond end of map */
2185 end = si->max;
2186
2187 /* Count contiguous allocated slots above our target */
2188 for (toff = target; ++toff < end; nr_pages++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /* Don't read in free or bad pages */
Hugh Dickins89528982008-02-04 22:28:45 -08002190 if (!si->swap_map[toff])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002192 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002194 }
2195 /* Count contiguous allocated slots below our target */
2196 for (toff = target; --toff >= base; nr_pages++) {
2197 /* Don't read in free or bad pages */
2198 if (!si->swap_map[toff])
2199 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002200 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Hugh Dickins89528982008-02-04 22:28:45 -08002201 break;
2202 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002203 spin_unlock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002204
2205 /*
2206 * Indicate starting offset, and return number of pages to get:
2207 * if only 1, say 0, since there's then no readahead to be done.
2208 */
2209 *offset = ++toff;
2210 return nr_pages? ++nr_pages: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
Hugh Dickins570a3352009-12-14 17:58:46 -08002212
2213/*
2214 * add_swap_count_continuation - called when a swap count is duplicated
2215 * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2216 * page of the original vmalloc'ed swap_map, to hold the continuation count
2217 * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
2218 * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2219 *
2220 * These continuation pages are seldom referenced: the common paths all work
2221 * on the original swap_map, only referring to a continuation page when the
2222 * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2223 *
2224 * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2225 * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2226 * can be called after dropping locks.
2227 */
2228int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2229{
2230 struct swap_info_struct *si;
2231 struct page *head;
2232 struct page *page;
2233 struct page *list_page;
2234 pgoff_t offset;
2235 unsigned char count;
2236
2237 /*
2238 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2239 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2240 */
2241 page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2242
2243 si = swap_info_get(entry);
2244 if (!si) {
2245 /*
2246 * An acceptable race has occurred since the failing
2247 * __swap_duplicate(): the swap entry has been freed,
2248 * perhaps even the whole swap_map cleared for swapoff.
2249 */
2250 goto outer;
2251 }
2252
2253 offset = swp_offset(entry);
2254 count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2255
2256 if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2257 /*
2258 * The higher the swap count, the more likely it is that tasks
2259 * will race to add swap count continuation: we need to avoid
2260 * over-provisioning.
2261 */
2262 goto out;
2263 }
2264
2265 if (!page) {
2266 spin_unlock(&swap_lock);
2267 return -ENOMEM;
2268 }
2269
2270 /*
2271 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2272 * no architecture is using highmem pages for kernel pagetables: so it
2273 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2274 */
2275 head = vmalloc_to_page(si->swap_map + offset);
2276 offset &= ~PAGE_MASK;
2277
2278 /*
2279 * Page allocation does not initialize the page's lru field,
2280 * but it does always reset its private field.
2281 */
2282 if (!page_private(head)) {
2283 BUG_ON(count & COUNT_CONTINUED);
2284 INIT_LIST_HEAD(&head->lru);
2285 set_page_private(head, SWP_CONTINUED);
2286 si->flags |= SWP_CONTINUED;
2287 }
2288
2289 list_for_each_entry(list_page, &head->lru, lru) {
2290 unsigned char *map;
2291
2292 /*
2293 * If the previous map said no continuation, but we've found
2294 * a continuation page, free our allocation and use this one.
2295 */
2296 if (!(count & COUNT_CONTINUED))
2297 goto out;
2298
2299 map = kmap_atomic(list_page, KM_USER0) + offset;
2300 count = *map;
2301 kunmap_atomic(map, KM_USER0);
2302
2303 /*
2304 * If this continuation count now has some space in it,
2305 * free our allocation and use this one.
2306 */
2307 if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2308 goto out;
2309 }
2310
2311 list_add_tail(&page->lru, &head->lru);
2312 page = NULL; /* now it's attached, don't free it */
2313out:
2314 spin_unlock(&swap_lock);
2315outer:
2316 if (page)
2317 __free_page(page);
2318 return 0;
2319}
2320
2321/*
2322 * swap_count_continued - when the original swap_map count is incremented
2323 * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2324 * into, carry if so, or else fail until a new continuation page is allocated;
2325 * when the original swap_map count is decremented from 0 with continuation,
2326 * borrow from the continuation and report whether it still holds more.
2327 * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2328 */
2329static bool swap_count_continued(struct swap_info_struct *si,
2330 pgoff_t offset, unsigned char count)
2331{
2332 struct page *head;
2333 struct page *page;
2334 unsigned char *map;
2335
2336 head = vmalloc_to_page(si->swap_map + offset);
2337 if (page_private(head) != SWP_CONTINUED) {
2338 BUG_ON(count & COUNT_CONTINUED);
2339 return false; /* need to add count continuation */
2340 }
2341
2342 offset &= ~PAGE_MASK;
2343 page = list_entry(head->lru.next, struct page, lru);
2344 map = kmap_atomic(page, KM_USER0) + offset;
2345
2346 if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
2347 goto init_map; /* jump over SWAP_CONT_MAX checks */
2348
2349 if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2350 /*
2351 * Think of how you add 1 to 999
2352 */
2353 while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
2354 kunmap_atomic(map, KM_USER0);
2355 page = list_entry(page->lru.next, struct page, lru);
2356 BUG_ON(page == head);
2357 map = kmap_atomic(page, KM_USER0) + offset;
2358 }
2359 if (*map == SWAP_CONT_MAX) {
2360 kunmap_atomic(map, KM_USER0);
2361 page = list_entry(page->lru.next, struct page, lru);
2362 if (page == head)
2363 return false; /* add count continuation */
2364 map = kmap_atomic(page, KM_USER0) + offset;
2365init_map: *map = 0; /* we didn't zero the page */
2366 }
2367 *map += 1;
2368 kunmap_atomic(map, KM_USER0);
2369 page = list_entry(page->lru.prev, struct page, lru);
2370 while (page != head) {
2371 map = kmap_atomic(page, KM_USER0) + offset;
2372 *map = COUNT_CONTINUED;
2373 kunmap_atomic(map, KM_USER0);
2374 page = list_entry(page->lru.prev, struct page, lru);
2375 }
2376 return true; /* incremented */
2377
2378 } else { /* decrementing */
2379 /*
2380 * Think of how you subtract 1 from 1000
2381 */
2382 BUG_ON(count != COUNT_CONTINUED);
2383 while (*map == COUNT_CONTINUED) {
2384 kunmap_atomic(map, KM_USER0);
2385 page = list_entry(page->lru.next, struct page, lru);
2386 BUG_ON(page == head);
2387 map = kmap_atomic(page, KM_USER0) + offset;
2388 }
2389 BUG_ON(*map == 0);
2390 *map -= 1;
2391 if (*map == 0)
2392 count = 0;
2393 kunmap_atomic(map, KM_USER0);
2394 page = list_entry(page->lru.prev, struct page, lru);
2395 while (page != head) {
2396 map = kmap_atomic(page, KM_USER0) + offset;
2397 *map = SWAP_CONT_MAX | count;
2398 count = COUNT_CONTINUED;
2399 kunmap_atomic(map, KM_USER0);
2400 page = list_entry(page->lru.prev, struct page, lru);
2401 }
2402 return count == COUNT_CONTINUED;
2403 }
2404}
2405
2406/*
2407 * free_swap_count_continuations - swapoff free all the continuation pages
2408 * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2409 */
2410static void free_swap_count_continuations(struct swap_info_struct *si)
2411{
2412 pgoff_t offset;
2413
2414 for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2415 struct page *head;
2416 head = vmalloc_to_page(si->swap_map + offset);
2417 if (page_private(head)) {
2418 struct list_head *this, *next;
2419 list_for_each_safe(this, next, &head->lru) {
2420 struct page *page;
2421 page = list_entry(this, struct page, lru);
2422 list_del(this);
2423 __free_page(page);
2424 }
2425 }
2426 }
2427}