blob: ce5dda6d604b503f2284d727f1600a3f12af97eb [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
Adrian Bunk7c363b82008-07-25 19:46:24 -070038static DEFINE_SPINLOCK(swap_lock);
39static unsigned int nr_swapfiles;
Hugh Dickinsb9627162009-01-06 14:39:41 -080040long nr_swap_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041long total_swap_pages;
42static int swap_overflow;
Hugh Dickins78ecba02008-07-23 21:28:23 -070043static int least_priority;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static const char Bad_file[] = "Bad swap file entry ";
46static const char Unused_file[] = "Unused swap file entry ";
47static const char Bad_offset[] = "Bad swap offset entry ";
48static const char Unused_offset[] = "Unused swap offset entry ";
49
Adrian Bunk7c363b82008-07-25 19:46:24 -070050static struct swap_list_t swap_list = {-1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Rafael J. Wysockif577eb32006-03-23 02:59:59 -080052static struct swap_info_struct swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ingo Molnarfc0abb12006-01-18 17:42:33 -080054static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070056/* For reference count accounting in swap_map */
57/* enum for swap_map[] handling. internal use only */
58enum {
59 SWAP_MAP = 0, /* ops for reference from swap users */
60 SWAP_CACHE, /* ops for reference from swap cache */
61};
62
63static inline int swap_count(unsigned short ent)
64{
65 return ent & SWAP_COUNT_MASK;
66}
67
68static inline bool swap_has_cache(unsigned short ent)
69{
70 return !!(ent & SWAP_HAS_CACHE);
71}
72
73static inline unsigned short encode_swapmap(int count, bool has_cache)
74{
75 unsigned short ret = count;
76
77 if (has_cache)
78 return SWAP_HAS_CACHE | ret;
79 return ret;
80}
81
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070082/* returnes 1 if swap entry is freed */
83static int
84__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
85{
86 int type = si - swap_info;
87 swp_entry_t entry = swp_entry(type, offset);
88 struct page *page;
89 int ret = 0;
90
91 page = find_get_page(&swapper_space, entry.val);
92 if (!page)
93 return 0;
94 /*
95 * This function is called from scan_swap_map() and it's called
96 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
97 * We have to use trylock for avoiding deadlock. This is a special
98 * case and you should use try_to_free_swap() with explicit lock_page()
99 * in usual operations.
100 */
101 if (trylock_page(page)) {
102 ret = try_to_free_swap(page);
103 unlock_page(page);
104 }
105 page_cache_release(page);
106 return ret;
107}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * We need this because the bdev->unplug_fn can sleep and we cannot
Hugh Dickins5d337b92005-09-03 15:54:41 -0700111 * hold swap_lock while calling the unplug_fn. And swap_lock
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800112 * cannot be turned into a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114static DECLARE_RWSEM(swap_unplug_sem);
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116void swap_unplug_io_fn(struct backing_dev_info *unused_bdi, struct page *page)
117{
118 swp_entry_t entry;
119
120 down_read(&swap_unplug_sem);
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700121 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (PageSwapCache(page)) {
123 struct block_device *bdev = swap_info[swp_type(entry)].bdev;
124 struct backing_dev_info *bdi;
125
126 /*
127 * If the page is removed from swapcache from under us (with a
128 * racy try_to_unuse/swapoff) we need an additional reference
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700129 * count to avoid reading garbage from page_private(page) above.
130 * If the WARN_ON triggers during a swapoff it maybe the race
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 * condition and it's harmless. However if it triggers without
132 * swapoff it signals a problem.
133 */
134 WARN_ON(page_count(page) <= 1);
135
136 bdi = bdev->bd_inode->i_mapping->backing_dev_info;
McMullan, Jasonba323112005-05-16 21:53:40 -0700137 blk_run_backing_dev(bdi, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 up_read(&swap_unplug_sem);
140}
141
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800142/*
143 * swapon tell device that all the old swap contents can be discarded,
144 * to allow the swap device to optimize its wear-levelling.
145 */
146static int discard_swap(struct swap_info_struct *si)
147{
148 struct swap_extent *se;
149 int err = 0;
150
151 list_for_each_entry(se, &si->extent_list, list) {
152 sector_t start_block = se->start_block << (PAGE_SHIFT - 9);
Hugh Dickins858a29902009-01-06 14:39:56 -0800153 sector_t nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800154
155 if (se->start_page == 0) {
156 /* Do not discard the swap header page! */
157 start_block += 1 << (PAGE_SHIFT - 9);
158 nr_blocks -= 1 << (PAGE_SHIFT - 9);
159 if (!nr_blocks)
160 continue;
161 }
162
163 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwig746cd1e2009-09-12 07:35:43 +0200164 nr_blocks, GFP_KERNEL,
165 DISCARD_FL_BARRIER);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800166 if (err)
167 break;
168
169 cond_resched();
170 }
171 return err; /* That will often be -EOPNOTSUPP */
172}
173
Hugh Dickins7992fde2009-01-06 14:39:53 -0800174/*
175 * swap allocation tell device that a cluster of swap can now be discarded,
176 * to allow the swap device to optimize its wear-levelling.
177 */
178static void discard_swap_cluster(struct swap_info_struct *si,
179 pgoff_t start_page, pgoff_t nr_pages)
180{
181 struct swap_extent *se = si->curr_swap_extent;
182 int found_extent = 0;
183
184 while (nr_pages) {
185 struct list_head *lh;
186
187 if (se->start_page <= start_page &&
188 start_page < se->start_page + se->nr_pages) {
189 pgoff_t offset = start_page - se->start_page;
190 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800191 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800192
193 if (nr_blocks > nr_pages)
194 nr_blocks = nr_pages;
195 start_page += nr_blocks;
196 nr_pages -= nr_blocks;
197
198 if (!found_extent++)
199 si->curr_swap_extent = se;
200
201 start_block <<= PAGE_SHIFT - 9;
202 nr_blocks <<= PAGE_SHIFT - 9;
203 if (blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwig746cd1e2009-09-12 07:35:43 +0200204 nr_blocks, GFP_NOIO,
205 DISCARD_FL_BARRIER))
Hugh Dickins7992fde2009-01-06 14:39:53 -0800206 break;
207 }
208
209 lh = se->list.next;
210 if (lh == &si->extent_list)
211 lh = lh->next;
212 se = list_entry(lh, struct swap_extent, list);
213 }
214}
215
216static int wait_for_discard(void *word)
217{
218 schedule();
219 return 0;
220}
221
Hugh Dickins048c27f2005-09-03 15:54:40 -0700222#define SWAPFILE_CLUSTER 256
223#define LATENCY_LIMIT 256
224
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700225static inline unsigned long scan_swap_map(struct swap_info_struct *si,
226 int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800228 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800229 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800230 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700231 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800232 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800234 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700235 * We try to cluster swap pages by allocating them sequentially
236 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
237 * way, however, we resort to first-free allocation, starting
238 * a new cluster. This prevents us from scattering swap pages
239 * all over the entire swap partition, so that we reduce
240 * overall disk seek times between swap pages. -- sct
241 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800242 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700243 */
244
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700245 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800246 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800247
248 if (unlikely(!si->cluster_nr--)) {
249 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
250 si->cluster_nr = SWAPFILE_CLUSTER - 1;
251 goto checks;
252 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800253 if (si->flags & SWP_DISCARDABLE) {
254 /*
255 * Start range check on racing allocations, in case
256 * they overlap the cluster we eventually decide on
257 * (we scan without swap_lock to allow preemption).
258 * It's hardly conceivable that cluster_nr could be
259 * wrapped during our scan, but don't depend on it.
260 */
261 if (si->lowest_alloc)
262 goto checks;
263 si->lowest_alloc = si->max;
264 si->highest_alloc = 0;
265 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700266 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700267
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800268 /*
269 * If seek is expensive, start searching for new cluster from
270 * start of partition, to minimize the span of allocated swap.
271 * But if seek is cheap, search from our current position, so
272 * that swap is allocated from all over the partition: if the
273 * Flash Translation Layer only remaps within limited zones,
274 * we don't want to wear out the first zone too quickly.
275 */
276 if (!(si->flags & SWP_SOLIDSTATE))
277 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700278 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
279
280 /* Locate the first empty (unaligned) cluster */
281 for (; last_in_cluster <= si->highest_bit; offset++) {
282 if (si->swap_map[offset])
283 last_in_cluster = offset + SWAPFILE_CLUSTER;
284 else if (offset == last_in_cluster) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700285 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800286 offset -= SWAPFILE_CLUSTER - 1;
287 si->cluster_next = offset;
288 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800289 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800290 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700292 if (unlikely(--latency_ration < 0)) {
293 cond_resched();
294 latency_ration = LATENCY_LIMIT;
295 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700296 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800297
298 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800299 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
300
301 /* Locate the first empty (unaligned) cluster */
302 for (; last_in_cluster < scan_base; offset++) {
303 if (si->swap_map[offset])
304 last_in_cluster = offset + SWAPFILE_CLUSTER;
305 else if (offset == last_in_cluster) {
306 spin_lock(&swap_lock);
307 offset -= SWAPFILE_CLUSTER - 1;
308 si->cluster_next = offset;
309 si->cluster_nr = SWAPFILE_CLUSTER - 1;
310 found_free_cluster = 1;
311 goto checks;
312 }
313 if (unlikely(--latency_ration < 0)) {
314 cond_resched();
315 latency_ration = LATENCY_LIMIT;
316 }
317 }
318
319 offset = scan_base;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700320 spin_lock(&swap_lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800321 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800322 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700324
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800325checks:
326 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700327 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700328 if (!si->highest_bit)
329 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800330 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800331 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700332
333 /* reuse swap entry of cache-only swap if not busy. */
334 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
335 int swap_was_freed;
336 spin_unlock(&swap_lock);
337 swap_was_freed = __try_to_reclaim_swap(si, offset);
338 spin_lock(&swap_lock);
339 /* entry was freed successfully, try to use this again */
340 if (swap_was_freed)
341 goto checks;
342 goto scan; /* check next one */
343 }
344
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800345 if (si->swap_map[offset])
346 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700347
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800348 if (offset == si->lowest_bit)
349 si->lowest_bit++;
350 if (offset == si->highest_bit)
351 si->highest_bit--;
352 si->inuse_pages++;
353 if (si->inuse_pages == si->pages) {
354 si->lowest_bit = si->max;
355 si->highest_bit = 0;
356 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700357 if (cache == SWAP_CACHE) /* at usual swap-out via vmscan.c */
358 si->swap_map[offset] = encode_swapmap(0, true);
359 else /* at suspend */
360 si->swap_map[offset] = encode_swapmap(1, false);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800361 si->cluster_next = offset + 1;
362 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800363
364 if (si->lowest_alloc) {
365 /*
366 * Only set when SWP_DISCARDABLE, and there's a scan
367 * for a free cluster in progress or just completed.
368 */
369 if (found_free_cluster) {
370 /*
371 * To optimize wear-levelling, discard the
372 * old data of the cluster, taking care not to
373 * discard any of its pages that have already
374 * been allocated by racing tasks (offset has
375 * already stepped over any at the beginning).
376 */
377 if (offset < si->highest_alloc &&
378 si->lowest_alloc <= last_in_cluster)
379 last_in_cluster = si->lowest_alloc - 1;
380 si->flags |= SWP_DISCARDING;
381 spin_unlock(&swap_lock);
382
383 if (offset < last_in_cluster)
384 discard_swap_cluster(si, offset,
385 last_in_cluster - offset + 1);
386
387 spin_lock(&swap_lock);
388 si->lowest_alloc = 0;
389 si->flags &= ~SWP_DISCARDING;
390
391 smp_mb(); /* wake_up_bit advises this */
392 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
393
394 } else if (si->flags & SWP_DISCARDING) {
395 /*
396 * Delay using pages allocated by racing tasks
397 * until the whole discard has been issued. We
398 * could defer that delay until swap_writepage,
399 * but it's easier to keep this self-contained.
400 */
401 spin_unlock(&swap_lock);
402 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
403 wait_for_discard, TASK_UNINTERRUPTIBLE);
404 spin_lock(&swap_lock);
405 } else {
406 /*
407 * Note pages allocated by racing tasks while
408 * scan for a free cluster is in progress, so
409 * that its final discard can exclude them.
410 */
411 if (offset < si->lowest_alloc)
412 si->lowest_alloc = offset;
413 if (offset > si->highest_alloc)
414 si->highest_alloc = offset;
415 }
416 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800417 return offset;
418
419scan:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700420 spin_unlock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700421 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700422 if (!si->swap_map[offset]) {
Hugh Dickins5d337b92005-09-03 15:54:41 -0700423 spin_lock(&swap_lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700424 goto checks;
425 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700426 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
427 spin_lock(&swap_lock);
428 goto checks;
429 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700430 if (unlikely(--latency_ration < 0)) {
431 cond_resched();
432 latency_ration = LATENCY_LIMIT;
433 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700434 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800435 offset = si->lowest_bit;
436 while (++offset < scan_base) {
437 if (!si->swap_map[offset]) {
438 spin_lock(&swap_lock);
439 goto checks;
440 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700441 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
442 spin_lock(&swap_lock);
443 goto checks;
444 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800445 if (unlikely(--latency_ration < 0)) {
446 cond_resched();
447 latency_ration = LATENCY_LIMIT;
448 }
449 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700450 spin_lock(&swap_lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700451
452no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700453 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
457swp_entry_t get_swap_page(void)
458{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700459 struct swap_info_struct *si;
460 pgoff_t offset;
461 int type, next;
462 int wrapped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Hugh Dickins5d337b92005-09-03 15:54:41 -0700464 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (nr_swap_pages <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700466 goto noswap;
467 nr_swap_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700469 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
470 si = swap_info + type;
471 next = si->next;
472 if (next < 0 ||
473 (!wrapped && si->prio != swap_info[next].prio)) {
474 next = swap_list.head;
475 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700477
478 if (!si->highest_bit)
479 continue;
480 if (!(si->flags & SWP_WRITEOK))
481 continue;
482
483 swap_list.next = next;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700484 /* This is called for allocating swap entry for cache */
485 offset = scan_swap_map(si, SWAP_CACHE);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700486 if (offset) {
487 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700488 return swp_entry(type, offset);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700489 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700490 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700492
493 nr_swap_pages++;
494noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700495 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700496 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700499/* The only caller of this function is now susupend routine */
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800500swp_entry_t get_swap_page_of_type(int type)
501{
502 struct swap_info_struct *si;
503 pgoff_t offset;
504
505 spin_lock(&swap_lock);
506 si = swap_info + type;
507 if (si->flags & SWP_WRITEOK) {
508 nr_swap_pages--;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700509 /* This is called for allocating swap entry, not cache */
510 offset = scan_swap_map(si, SWAP_MAP);
Rafael J. Wysocki3a291a22006-01-06 00:16:37 -0800511 if (offset) {
512 spin_unlock(&swap_lock);
513 return swp_entry(type, offset);
514 }
515 nr_swap_pages++;
516 }
517 spin_unlock(&swap_lock);
518 return (swp_entry_t) {0};
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static struct swap_info_struct * swap_info_get(swp_entry_t entry)
522{
523 struct swap_info_struct * p;
524 unsigned long offset, type;
525
526 if (!entry.val)
527 goto out;
528 type = swp_type(entry);
529 if (type >= nr_swapfiles)
530 goto bad_nofile;
531 p = & swap_info[type];
532 if (!(p->flags & SWP_USED))
533 goto bad_device;
534 offset = swp_offset(entry);
535 if (offset >= p->max)
536 goto bad_offset;
537 if (!p->swap_map[offset])
538 goto bad_free;
Hugh Dickins5d337b92005-09-03 15:54:41 -0700539 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return p;
541
542bad_free:
543 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
544 goto out;
545bad_offset:
546 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
547 goto out;
548bad_device:
549 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
550 goto out;
551bad_nofile:
552 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
553out:
554 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800555}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700557static int swap_entry_free(struct swap_info_struct *p,
558 swp_entry_t ent, int cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800560 unsigned long offset = swp_offset(ent);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700561 int count = swap_count(p->swap_map[offset]);
562 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700564 has_cache = swap_has_cache(p->swap_map[offset]);
565
566 if (cache == SWAP_MAP) { /* dropping usage count of swap */
567 if (count < SWAP_MAP_MAX) {
568 count--;
569 p->swap_map[offset] = encode_swapmap(count, has_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700571 } else { /* dropping swap cache flag */
572 VM_BUG_ON(!has_cache);
573 p->swap_map[offset] = encode_swapmap(count, false);
574
575 }
576 /* return code. */
577 count = p->swap_map[offset];
578 /* free if no reference */
579 if (!count) {
580 if (offset < p->lowest_bit)
581 p->lowest_bit = offset;
582 if (offset > p->highest_bit)
583 p->highest_bit = offset;
584 if (p->prio > swap_info[swap_list.next].prio)
585 swap_list.next = p - swap_info;
586 nr_swap_pages++;
587 p->inuse_pages--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700589 if (!swap_count(count))
590 mem_cgroup_uncharge_swap(ent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return count;
592}
593
594/*
595 * Caller has made sure that the swapdevice corresponding to entry
596 * is still around or has not been recycled.
597 */
598void swap_free(swp_entry_t entry)
599{
600 struct swap_info_struct * p;
601
602 p = swap_info_get(entry);
603 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700604 swap_entry_free(p, entry, SWAP_MAP);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700605 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607}
608
609/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700610 * Called after dropping swapcache to decrease refcnt to swap entries.
611 */
612void swapcache_free(swp_entry_t entry, struct page *page)
613{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700614 struct swap_info_struct *p;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700615 int ret;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700616
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700617 p = swap_info_get(entry);
618 if (p) {
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700619 ret = swap_entry_free(p, entry, SWAP_CACHE);
620 if (page) {
621 bool swapout;
622 if (ret)
623 swapout = true; /* the end of swap out */
624 else
625 swapout = false; /* no more swap users! */
626 mem_cgroup_uncharge_swapcache(page, entry, swapout);
627 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700628 spin_unlock(&swap_lock);
629 }
630 return;
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700631}
632
633/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700634 * How many references to page are currently swapped out?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700636static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700638 int count = 0;
639 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 swp_entry_t entry;
641
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700642 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 p = swap_info_get(entry);
644 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700645 count = swap_count(p->swap_map[swp_offset(entry)]);
Hugh Dickins5d337b92005-09-03 15:54:41 -0700646 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700648 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800652 * We can write to an anon page without COW if there are no other references
653 * to it. And as a side-effect, free up its swap: because the old content
654 * on disk will never be read, and seeking back there to write new content
655 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800657int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700659 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Hugh Dickins51726b12009-01-06 14:39:25 -0800661 VM_BUG_ON(!PageLocked(page));
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700662 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800663 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700664 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800665 if (count == 1 && !PageWriteback(page)) {
666 delete_from_swap_cache(page);
667 SetPageDirty(page);
668 }
669 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700670 return count == 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800674 * If swap is getting full, or if there are no more mappings of this page,
675 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800677int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Hugh Dickins51726b12009-01-06 14:39:25 -0800679 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!PageSwapCache(page))
682 return 0;
683 if (PageWriteback(page))
684 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800685 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800688 delete_from_swap_cache(page);
689 SetPageDirty(page);
690 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700691}
692
693/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 * Free the swap entry like above, but also try to
695 * free the page cache entry if it is the last user.
696 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800697int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800699 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct page *page = NULL;
701
Andi Kleena7420aa2009-09-16 11:50:05 +0200702 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800703 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 p = swap_info_get(entry);
706 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700707 if (swap_entry_free(p, entry, SWAP_MAP) == SWAP_HAS_CACHE) {
Nick Piggin93fac702006-03-31 02:29:56 -0800708 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700709 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800710 page_cache_release(page);
711 page = NULL;
712 }
713 }
Hugh Dickins5d337b92005-09-03 15:54:41 -0700714 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800717 /*
718 * Not mapped elsewhere, or swap space full? Free it!
719 * Also recheck PageSwapCache now page is locked (above).
720 */
Nick Piggin93fac702006-03-31 02:29:56 -0800721 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800722 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 delete_from_swap_cache(page);
724 SetPageDirty(page);
725 }
726 unlock_page(page);
727 page_cache_release(page);
728 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800729 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200732#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800733/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800734 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800735 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800736 * @offset - number of the PAGE_SIZE-sized block of the device, starting
737 * from 0, in which the swap header is expected to be located.
738 *
739 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800740 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800741int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800742{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800743 struct block_device *bdev = NULL;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800744 int i;
745
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800746 if (device)
747 bdev = bdget(device);
748
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800749 spin_lock(&swap_lock);
750 for (i = 0; i < nr_swapfiles; i++) {
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800751 struct swap_info_struct *sis = swap_info + i;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800752
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800753 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800754 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700755
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800756 if (!bdev) {
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. Wysocki6e1819d2006-03-23 03:00:03 -0800760 spin_unlock(&swap_lock);
761 return i;
762 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800763 if (bdev == sis->bdev) {
764 struct swap_extent *se;
765
766 se = list_entry(sis->extent_list.next,
767 struct swap_extent, list);
768 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800769 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200770 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800771
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800772 spin_unlock(&swap_lock);
773 bdput(bdev);
774 return i;
775 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800776 }
777 }
778 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800779 if (bdev)
780 bdput(bdev);
781
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800782 return -ENODEV;
783}
784
785/*
786 * Return either the total number of swap pages of given type, or the number
787 * of free pages of that type (depending on @free)
788 *
789 * This is needed for software suspend
790 */
791unsigned int count_swap_pages(int type, int free)
792{
793 unsigned int n = 0;
794
795 if (type < nr_swapfiles) {
796 spin_lock(&swap_lock);
797 if (swap_info[type].flags & SWP_WRITEOK) {
798 n = swap_info[type].pages;
799 if (free)
800 n -= swap_info[type].inuse_pages;
801 }
802 spin_unlock(&swap_lock);
803 }
804 return n;
805}
806#endif
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700809 * No need to decide whether this PTE shares the swap entry with others,
810 * just let do_wp_page work it out if a write is requested later - to
811 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800813static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned long addr, swp_entry_t entry, struct page *page)
815{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800816 struct mem_cgroup *ptr = NULL;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800817 spinlock_t *ptl;
818 pte_t *pte;
819 int ret = 1;
820
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800821 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page, GFP_KERNEL, &ptr)) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800822 ret = -ENOMEM;
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800823 goto out_nolock;
824 }
Hugh Dickins044d66c2008-02-07 00:14:04 -0800825
826 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
827 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
828 if (ret > 0)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800829 mem_cgroup_cancel_charge_swapin(ptr);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800830 ret = 0;
831 goto out;
832 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800833
Hugh Dickins42946212005-10-29 18:16:05 -0700834 inc_mm_counter(vma->vm_mm, anon_rss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 get_page(page);
836 set_pte_at(vma->vm_mm, addr, pte,
837 pte_mkold(mk_pte(page, vma->vm_page_prot)));
838 page_add_anon_rmap(page, vma, addr);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800839 mem_cgroup_commit_charge_swapin(page, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 swap_free(entry);
841 /*
842 * Move the page to the active list so it is not
843 * immediately swapped out again after swapon.
844 */
845 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800846out:
847 pte_unmap_unlock(pte, ptl);
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800848out_nolock:
Hugh Dickins044d66c2008-02-07 00:14:04 -0800849 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
852static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
853 unsigned long addr, unsigned long end,
854 swp_entry_t entry, struct page *page)
855{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700857 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800858 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Hugh Dickins044d66c2008-02-07 00:14:04 -0800860 /*
861 * We don't actually need pte lock while scanning for swp_pte: since
862 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
863 * page table while we're scanning; though it could get zapped, and on
864 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
865 * of unmatched parts which look like swp_pte, so unuse_pte must
866 * recheck under pte lock. Scanning without pte lock lets it be
867 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
868 */
869 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 do {
871 /*
872 * swapoff spends a _lot_ of time in this loop!
873 * Test inline before going to call unuse_pte.
874 */
875 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800876 pte_unmap(pte);
877 ret = unuse_pte(vma, pmd, addr, entry, page);
878 if (ret)
879 goto out;
880 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800883 pte_unmap(pte - 1);
884out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800885 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
888static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
889 unsigned long addr, unsigned long end,
890 swp_entry_t entry, struct page *page)
891{
892 pmd_t *pmd;
893 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800894 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 pmd = pmd_offset(pud, addr);
897 do {
898 next = pmd_addr_end(addr, end);
899 if (pmd_none_or_clear_bad(pmd))
900 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800901 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
902 if (ret)
903 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 } while (pmd++, addr = next, addr != end);
905 return 0;
906}
907
908static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
909 unsigned long addr, unsigned long end,
910 swp_entry_t entry, struct page *page)
911{
912 pud_t *pud;
913 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800914 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 pud = pud_offset(pgd, addr);
917 do {
918 next = pud_addr_end(addr, end);
919 if (pud_none_or_clear_bad(pud))
920 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800921 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
922 if (ret)
923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 } while (pud++, addr = next, addr != end);
925 return 0;
926}
927
928static int unuse_vma(struct vm_area_struct *vma,
929 swp_entry_t entry, struct page *page)
930{
931 pgd_t *pgd;
932 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800933 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (page->mapping) {
936 addr = page_address_in_vma(page, vma);
937 if (addr == -EFAULT)
938 return 0;
939 else
940 end = addr + PAGE_SIZE;
941 } else {
942 addr = vma->vm_start;
943 end = vma->vm_end;
944 }
945
946 pgd = pgd_offset(vma->vm_mm, addr);
947 do {
948 next = pgd_addr_end(addr, end);
949 if (pgd_none_or_clear_bad(pgd))
950 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800951 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
952 if (ret)
953 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 } while (pgd++, addr = next, addr != end);
955 return 0;
956}
957
958static int unuse_mm(struct mm_struct *mm,
959 swp_entry_t entry, struct page *page)
960{
961 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800962 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (!down_read_trylock(&mm->mmap_sem)) {
965 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -0700966 * Activate page so shrink_inactive_list is unlikely to unmap
967 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700969 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 unlock_page(page);
971 down_read(&mm->mmap_sem);
972 lock_page(page);
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800975 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 break;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800979 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/*
983 * Scan swap_map from current position to next entry still in use.
984 * Recycle to start on reaching the end, returning 0 when empty.
985 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700986static unsigned int find_next_to_unuse(struct swap_info_struct *si,
987 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Hugh Dickins6eb396d2005-09-03 15:54:35 -0700989 unsigned int max = si->max;
990 unsigned int i = prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 int count;
992
993 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -0700994 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 * for whether an entry is in use, not modifying it; false
996 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -0700997 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 */
999 for (;;) {
1000 if (++i >= max) {
1001 if (!prev) {
1002 i = 0;
1003 break;
1004 }
1005 /*
1006 * No entries in use at top of swap_map,
1007 * loop back to start and recheck there.
1008 */
1009 max = prev + 1;
1010 prev = 0;
1011 i = 1;
1012 }
1013 count = si->swap_map[i];
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001014 if (count && swap_count(count) != SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016 }
1017 return i;
1018}
1019
1020/*
1021 * We completely avoid races by reading each swap page in advance,
1022 * and then search for the process using it. All the necessary
1023 * page table adjustments can then be made atomically.
1024 */
1025static int try_to_unuse(unsigned int type)
1026{
1027 struct swap_info_struct * si = &swap_info[type];
1028 struct mm_struct *start_mm;
1029 unsigned short *swap_map;
1030 unsigned short swcount;
1031 struct page *page;
1032 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001033 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 int retval = 0;
1035 int reset_overflow = 0;
1036 int shmem;
1037
1038 /*
1039 * When searching mms for an entry, a good strategy is to
1040 * start at the first mm we freed the previous entry from
1041 * (though actually we don't notice whether we or coincidence
1042 * freed the entry). Initialize this start_mm with a hold.
1043 *
1044 * A simpler strategy would be to start at the last mm we
1045 * freed the previous entry from; but that would take less
1046 * advantage of mmlist ordering, which clusters forked mms
1047 * together, child after parent. If we race with dup_mmap(), we
1048 * prefer to resolve parent before child, lest we miss entries
1049 * duplicated after we scanned child: using last mm would invert
1050 * that. Though it's only a serious concern when an overflowed
1051 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
1052 */
1053 start_mm = &init_mm;
1054 atomic_inc(&init_mm.mm_users);
1055
1056 /*
1057 * Keep on scanning until all entries have gone. Usually,
1058 * one pass through swap_map is enough, but not necessarily:
1059 * there are races when an instance of an entry might be missed.
1060 */
1061 while ((i = find_next_to_unuse(si, i)) != 0) {
1062 if (signal_pending(current)) {
1063 retval = -EINTR;
1064 break;
1065 }
1066
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001067 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * Get a page for the entry, using the existing swap
1069 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001070 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072 swap_map = &si->swap_map[i];
1073 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -08001074 page = read_swap_cache_async(entry,
1075 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!page) {
1077 /*
1078 * Either swap_duplicate() failed because entry
1079 * has been freed independently, and will not be
1080 * reused since sys_swapoff() already disabled
1081 * allocation from here, or alloc_page() failed.
1082 */
1083 if (!*swap_map)
1084 continue;
1085 retval = -ENOMEM;
1086 break;
1087 }
1088
1089 /*
1090 * Don't hold on to start_mm if it looks like exiting.
1091 */
1092 if (atomic_read(&start_mm->mm_users) == 1) {
1093 mmput(start_mm);
1094 start_mm = &init_mm;
1095 atomic_inc(&init_mm.mm_users);
1096 }
1097
1098 /*
1099 * Wait for and lock page. When do_swap_page races with
1100 * try_to_unuse, do_swap_page can handle the fault much
1101 * faster than try_to_unuse can locate the entry. This
1102 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1103 * defer to do_swap_page in such a case - in some tests,
1104 * do_swap_page and try_to_unuse repeatedly compete.
1105 */
1106 wait_on_page_locked(page);
1107 wait_on_page_writeback(page);
1108 lock_page(page);
1109 wait_on_page_writeback(page);
1110
1111 /*
1112 * Remove all references to entry.
1113 * Whenever we reach init_mm, there's no address space
1114 * to search, but use it as a reminder to search shmem.
1115 */
1116 shmem = 0;
1117 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001118 if (swap_count(swcount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (start_mm == &init_mm)
1120 shmem = shmem_unuse(entry, page);
1121 else
1122 retval = unuse_mm(start_mm, entry, page);
1123 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001124 if (swap_count(*swap_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int set_start_mm = (*swap_map >= swcount);
1126 struct list_head *p = &start_mm->mmlist;
1127 struct mm_struct *new_start_mm = start_mm;
1128 struct mm_struct *prev_mm = start_mm;
1129 struct mm_struct *mm;
1130
1131 atomic_inc(&new_start_mm->mm_users);
1132 atomic_inc(&prev_mm->mm_users);
1133 spin_lock(&mmlist_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001134 while (swap_count(*swap_map) && !retval && !shmem &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 (p = p->next) != &start_mm->mmlist) {
1136 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001137 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 spin_unlock(&mmlist_lock);
1140 mmput(prev_mm);
1141 prev_mm = mm;
1142
1143 cond_resched();
1144
1145 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001146 if (!swap_count(swcount)) /* any usage ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ;
1148 else if (mm == &init_mm) {
1149 set_start_mm = 1;
1150 shmem = shmem_unuse(entry, page);
1151 } else
1152 retval = unuse_mm(mm, entry, page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001153
1154 if (set_start_mm &&
1155 swap_count(*swap_map) < swcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 mmput(new_start_mm);
1157 atomic_inc(&mm->mm_users);
1158 new_start_mm = mm;
1159 set_start_mm = 0;
1160 }
1161 spin_lock(&mmlist_lock);
1162 }
1163 spin_unlock(&mmlist_lock);
1164 mmput(prev_mm);
1165 mmput(start_mm);
1166 start_mm = new_start_mm;
1167 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001168 if (shmem) {
1169 /* page has already been unlocked and released */
1170 if (shmem > 0)
1171 continue;
1172 retval = shmem;
1173 break;
1174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (retval) {
1176 unlock_page(page);
1177 page_cache_release(page);
1178 break;
1179 }
1180
1181 /*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001182 * How could swap count reach 0x7ffe ?
1183 * There's no way to repeat a swap page within an mm
1184 * (except in shmem, where it's the shared object which takes
1185 * the reference count)?
1186 * We believe SWAP_MAP_MAX cannot occur.(if occur, unsigned
1187 * short is too small....)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 * If that's wrong, then we should worry more about
1189 * exit_mmap() and do_munmap() cases described above:
1190 * we might be resetting SWAP_MAP_MAX too early here.
1191 * We know "Undead"s can happen, they're okay, so don't
1192 * report them; but do report if we reset SWAP_MAP_MAX.
1193 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001194 /* We might release the lock_page() in unuse_mm(). */
1195 if (!PageSwapCache(page) || page_private(page) != entry.val)
1196 goto retry;
1197
1198 if (swap_count(*swap_map) == SWAP_MAP_MAX) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001199 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001200 *swap_map = encode_swapmap(0, true);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001201 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 reset_overflow = 1;
1203 }
1204
1205 /*
1206 * If a reference remains (rare), we would like to leave
1207 * the page in the swap cache; but try_to_unmap could
1208 * then re-duplicate the entry once we drop page lock,
1209 * so we might loop indefinitely; also, that page could
1210 * not be swapped out to other storage meanwhile. So:
1211 * delete from cache even if there's another reference,
1212 * after ensuring that the data has been saved to disk -
1213 * since if the reference remains (rarer), it will be
1214 * read from disk into another page. Splitting into two
1215 * pages would be incorrect if swap supported "shared
1216 * private" pages, but they are handled by tmpfs files.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001218 if (swap_count(*swap_map) &&
1219 PageDirty(page) && PageSwapCache(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 struct writeback_control wbc = {
1221 .sync_mode = WB_SYNC_NONE,
1222 };
1223
1224 swap_writepage(page, &wbc);
1225 lock_page(page);
1226 wait_on_page_writeback(page);
1227 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001228
1229 /*
1230 * It is conceivable that a racing task removed this page from
1231 * swap cache just before we acquired the page lock at the top,
1232 * or while we dropped it in unuse_mm(). The page might even
1233 * be back in swap cache on another swap area: that we must not
1234 * delete, since it may not have been written out to swap yet.
1235 */
1236 if (PageSwapCache(page) &&
1237 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001238 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * So we could skip searching mms once swap count went
1242 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001243 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 */
1245 SetPageDirty(page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001246retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unlock_page(page);
1248 page_cache_release(page);
1249
1250 /*
1251 * Make sure that we aren't completely killing
1252 * interactive performance.
1253 */
1254 cond_resched();
1255 }
1256
1257 mmput(start_mm);
1258 if (reset_overflow) {
1259 printk(KERN_WARNING "swapoff: cleared swap entry overflow\n");
1260 swap_overflow = 0;
1261 }
1262 return retval;
1263}
1264
1265/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001266 * After a successful try_to_unuse, if no swap is now in use, we know
1267 * we can empty the mmlist. swap_lock must be held on entry and exit.
1268 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * added to the mmlist just after page_duplicate - before would be racy.
1270 */
1271static void drain_mmlist(void)
1272{
1273 struct list_head *p, *next;
1274 unsigned int i;
1275
1276 for (i = 0; i < nr_swapfiles; i++)
1277 if (swap_info[i].inuse_pages)
1278 return;
1279 spin_lock(&mmlist_lock);
1280 list_for_each_safe(p, next, &init_mm.mmlist)
1281 list_del_init(p);
1282 spin_unlock(&mmlist_lock);
1283}
1284
1285/*
1286 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
1287 * corresponds to page offset `offset'.
1288 */
1289sector_t map_swap_page(struct swap_info_struct *sis, pgoff_t offset)
1290{
1291 struct swap_extent *se = sis->curr_swap_extent;
1292 struct swap_extent *start_se = se;
1293
1294 for ( ; ; ) {
1295 struct list_head *lh;
1296
1297 if (se->start_page <= offset &&
1298 offset < (se->start_page + se->nr_pages)) {
1299 return se->start_block + (offset - se->start_page);
1300 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001301 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (lh == &sis->extent_list)
Hugh Dickins11d31882005-09-03 15:54:34 -07001303 lh = lh->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 se = list_entry(lh, struct swap_extent, list);
1305 sis->curr_swap_extent = se;
1306 BUG_ON(se == start_se); /* It *must* be present */
1307 }
1308}
1309
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001310#ifdef CONFIG_HIBERNATION
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001311/*
1312 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
1313 * corresponding to given index in swap_info (swap type).
1314 */
1315sector_t swapdev_block(int swap_type, pgoff_t offset)
1316{
1317 struct swap_info_struct *sis;
1318
1319 if (swap_type >= nr_swapfiles)
1320 return 0;
1321
1322 sis = swap_info + swap_type;
1323 return (sis->flags & SWP_WRITEOK) ? map_swap_page(sis, offset) : 0;
1324}
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +02001325#endif /* CONFIG_HIBERNATION */
Rafael J. Wysocki3aef83e2006-12-06 20:34:10 -08001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327/*
1328 * Free all of a swapdev's extent information
1329 */
1330static void destroy_swap_extents(struct swap_info_struct *sis)
1331{
1332 while (!list_empty(&sis->extent_list)) {
1333 struct swap_extent *se;
1334
1335 se = list_entry(sis->extent_list.next,
1336 struct swap_extent, list);
1337 list_del(&se->list);
1338 kfree(se);
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342/*
1343 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001344 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001346 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
1348static int
1349add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1350 unsigned long nr_pages, sector_t start_block)
1351{
1352 struct swap_extent *se;
1353 struct swap_extent *new_se;
1354 struct list_head *lh;
1355
Hugh Dickins11d31882005-09-03 15:54:34 -07001356 lh = sis->extent_list.prev; /* The highest page extent */
1357 if (lh != &sis->extent_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001359 BUG_ON(se->start_page + se->nr_pages != start_page);
1360 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 /* Merge it */
1362 se->nr_pages += nr_pages;
1363 return 0;
1364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
1366
1367 /*
1368 * No merge. Insert a new extent, preserving ordering.
1369 */
1370 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1371 if (new_se == NULL)
1372 return -ENOMEM;
1373 new_se->start_page = start_page;
1374 new_se->nr_pages = nr_pages;
1375 new_se->start_block = start_block;
1376
Hugh Dickins11d31882005-09-03 15:54:34 -07001377 list_add_tail(&new_se->list, &sis->extent_list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001378 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
1381/*
1382 * A `swap extent' is a simple thing which maps a contiguous range of pages
1383 * onto a contiguous range of disk blocks. An ordered list of swap extents
1384 * is built at swapon time and is then used at swap_writepage/swap_readpage
1385 * time for locating where on disk a page belongs.
1386 *
1387 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1388 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1389 * swap files identically.
1390 *
1391 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1392 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1393 * swapfiles are handled *identically* after swapon time.
1394 *
1395 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1396 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1397 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1398 * requirements, they are simply tossed out - we will never use those blocks
1399 * for swapping.
1400 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001401 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1403 * which will scribble on the fs.
1404 *
1405 * The amount of disk space which a single swap extent represents varies.
1406 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1407 * extents in the list. To avoid much list walking, we cache the previous
1408 * search location in `curr_swap_extent', and start new searches from there.
1409 * This is extremely effective. The average number of iterations in
1410 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1411 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001412static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 struct inode *inode;
1415 unsigned blocks_per_page;
1416 unsigned long page_no;
1417 unsigned blkbits;
1418 sector_t probe_block;
1419 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001420 sector_t lowest_block = -1;
1421 sector_t highest_block = 0;
1422 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int ret;
1424
1425 inode = sis->swap_file->f_mapping->host;
1426 if (S_ISBLK(inode->i_mode)) {
1427 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001428 *span = sis->pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 goto done;
1430 }
1431
1432 blkbits = inode->i_blkbits;
1433 blocks_per_page = PAGE_SIZE >> blkbits;
1434
1435 /*
1436 * Map all the blocks into the extent list. This code doesn't try
1437 * to be very smart.
1438 */
1439 probe_block = 0;
1440 page_no = 0;
1441 last_block = i_size_read(inode) >> blkbits;
1442 while ((probe_block + blocks_per_page) <= last_block &&
1443 page_no < sis->max) {
1444 unsigned block_in_page;
1445 sector_t first_block;
1446
1447 first_block = bmap(inode, probe_block);
1448 if (first_block == 0)
1449 goto bad_bmap;
1450
1451 /*
1452 * It must be PAGE_SIZE aligned on-disk
1453 */
1454 if (first_block & (blocks_per_page - 1)) {
1455 probe_block++;
1456 goto reprobe;
1457 }
1458
1459 for (block_in_page = 1; block_in_page < blocks_per_page;
1460 block_in_page++) {
1461 sector_t block;
1462
1463 block = bmap(inode, probe_block + block_in_page);
1464 if (block == 0)
1465 goto bad_bmap;
1466 if (block != first_block + block_in_page) {
1467 /* Discontiguity */
1468 probe_block++;
1469 goto reprobe;
1470 }
1471 }
1472
Hugh Dickins53092a72005-09-03 15:54:34 -07001473 first_block >>= (PAGE_SHIFT - blkbits);
1474 if (page_no) { /* exclude the header page */
1475 if (first_block < lowest_block)
1476 lowest_block = first_block;
1477 if (first_block > highest_block)
1478 highest_block = first_block;
1479 }
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /*
1482 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1483 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001484 ret = add_swap_extent(sis, page_no, 1, first_block);
1485 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001487 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 page_no++;
1489 probe_block += blocks_per_page;
1490reprobe:
1491 continue;
1492 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001493 ret = nr_extents;
1494 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001496 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001498 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 sis->highest_bit = page_no - 1;
1500done:
1501 sis->curr_swap_extent = list_entry(sis->extent_list.prev,
1502 struct swap_extent, list);
1503 goto out;
1504bad_bmap:
1505 printk(KERN_ERR "swapon: swapfile has holes\n");
1506 ret = -EINVAL;
1507out:
1508 return ret;
1509}
1510
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001511SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct swap_info_struct * p = NULL;
1514 unsigned short *swap_map;
1515 struct file *swap_file, *victim;
1516 struct address_space *mapping;
1517 struct inode *inode;
1518 char * pathname;
1519 int i, type, prev;
1520 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (!capable(CAP_SYS_ADMIN))
1523 return -EPERM;
1524
1525 pathname = getname(specialfile);
1526 err = PTR_ERR(pathname);
1527 if (IS_ERR(pathname))
1528 goto out;
1529
1530 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1531 putname(pathname);
1532 err = PTR_ERR(victim);
1533 if (IS_ERR(victim))
1534 goto out;
1535
1536 mapping = victim->f_mapping;
1537 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001538 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 for (type = swap_list.head; type >= 0; type = swap_info[type].next) {
1540 p = swap_info + type;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001541 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 if (p->swap_file->f_mapping == mapping)
1543 break;
1544 }
1545 prev = type;
1546 }
1547 if (type < 0) {
1548 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001549 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto out_dput;
1551 }
1552 if (!security_vm_enough_memory(p->pages))
1553 vm_unacct_memory(p->pages);
1554 else {
1555 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001556 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto out_dput;
1558 }
1559 if (prev < 0) {
1560 swap_list.head = p->next;
1561 } else {
1562 swap_info[prev].next = p->next;
1563 }
1564 if (type == swap_list.next) {
1565 /* just pick something that's safe... */
1566 swap_list.next = swap_list.head;
1567 }
Hugh Dickins78ecba02008-07-23 21:28:23 -07001568 if (p->prio < 0) {
1569 for (i = p->next; i >= 0; i = swap_info[i].next)
1570 swap_info[i].prio = p->prio--;
1571 least_priority++;
1572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 nr_swap_pages -= p->pages;
1574 total_swap_pages -= p->pages;
1575 p->flags &= ~SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001576 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 current->flags |= PF_SWAPOFF;
1579 err = try_to_unuse(type);
1580 current->flags &= ~PF_SWAPOFF;
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (err) {
1583 /* re-insert swap space back into swap_list */
Hugh Dickins5d337b92005-09-03 15:54:41 -07001584 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001585 if (p->prio < 0)
1586 p->prio = --least_priority;
1587 prev = -1;
1588 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (p->prio >= swap_info[i].prio)
1590 break;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001591 prev = i;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 p->next = i;
1594 if (prev < 0)
1595 swap_list.head = swap_list.next = p - swap_info;
1596 else
1597 swap_info[prev].next = p - swap_info;
1598 nr_swap_pages += p->pages;
1599 total_swap_pages += p->pages;
1600 p->flags |= SWP_WRITEOK;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001601 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto out_dput;
1603 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001604
1605 /* wait for any unplug function to finish */
1606 down_write(&swap_unplug_sem);
1607 up_write(&swap_unplug_sem);
1608
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001609 destroy_swap_extents(p);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001610 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001611 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001613
1614 /* wait for anyone still in scan_swap_map */
1615 p->highest_bit = 0; /* cuts scans short */
1616 while (p->flags >= SWP_SCANNING) {
1617 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001618 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001619 spin_lock(&swap_lock);
1620 }
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 swap_file = p->swap_file;
1623 p->swap_file = NULL;
1624 p->max = 0;
1625 swap_map = p->swap_map;
1626 p->swap_map = NULL;
1627 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001628 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001629 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001631 /* Destroy swap account informatin */
1632 swap_cgroup_swapoff(type);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 inode = mapping->host;
1635 if (S_ISBLK(inode->i_mode)) {
1636 struct block_device *bdev = I_BDEV(inode);
1637 set_blocksize(bdev, p->old_block_size);
1638 bd_release(bdev);
1639 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001640 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001642 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 filp_close(swap_file, NULL);
1645 err = 0;
1646
1647out_dput:
1648 filp_close(victim, NULL);
1649out:
1650 return err;
1651}
1652
1653#ifdef CONFIG_PROC_FS
1654/* iterator */
1655static void *swap_start(struct seq_file *swap, loff_t *pos)
1656{
1657 struct swap_info_struct *ptr = swap_info;
1658 int i;
1659 loff_t l = *pos;
1660
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001661 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001663 if (!l)
1664 return SEQ_START_TOKEN;
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 for (i = 0; i < nr_swapfiles; i++, ptr++) {
1667 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1668 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001669 if (!--l)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return ptr;
1671 }
1672
1673 return NULL;
1674}
1675
1676static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1677{
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001678 struct swap_info_struct *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct swap_info_struct *endptr = swap_info + nr_swapfiles;
1680
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001681 if (v == SEQ_START_TOKEN)
1682 ptr = swap_info;
1683 else {
1684 ptr = v;
1685 ptr++;
1686 }
1687
1688 for (; ptr < endptr; ptr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!(ptr->flags & SWP_USED) || !ptr->swap_map)
1690 continue;
1691 ++*pos;
1692 return ptr;
1693 }
1694
1695 return NULL;
1696}
1697
1698static void swap_stop(struct seq_file *swap, void *v)
1699{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001700 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
1703static int swap_show(struct seq_file *swap, void *v)
1704{
1705 struct swap_info_struct *ptr = v;
1706 struct file *file;
1707 int len;
1708
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001709 if (ptr == SEQ_START_TOKEN) {
1710 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1711 return 0;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 file = ptr->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001715 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001716 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001717 len < 40 ? 40 - len : 1, " ",
1718 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 "partition" : "file\t",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001720 ptr->pages << (PAGE_SHIFT - 10),
1721 ptr->inuse_pages << (PAGE_SHIFT - 10),
1722 ptr->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
Helge Deller15ad7cd2006-12-06 20:40:36 -08001726static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 .start = swap_start,
1728 .next = swap_next,
1729 .stop = swap_stop,
1730 .show = swap_show
1731};
1732
1733static int swaps_open(struct inode *inode, struct file *file)
1734{
1735 return seq_open(file, &swaps_op);
1736}
1737
Helge Deller15ad7cd2006-12-06 20:40:36 -08001738static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .open = swaps_open,
1740 .read = seq_read,
1741 .llseek = seq_lseek,
1742 .release = seq_release,
1743};
1744
1745static int __init procswaps_init(void)
1746{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001747 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return 0;
1749}
1750__initcall(procswaps_init);
1751#endif /* CONFIG_PROC_FS */
1752
Jan Beulich17963162008-12-16 11:35:24 +00001753#ifdef MAX_SWAPFILES_CHECK
1754static int __init max_swapfiles_check(void)
1755{
1756 MAX_SWAPFILES_CHECK();
1757 return 0;
1758}
1759late_initcall(max_swapfiles_check);
1760#endif
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/*
1763 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1764 *
1765 * The swapon system call
1766 */
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001767SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 struct swap_info_struct * p;
1770 char *name = NULL;
1771 struct block_device *bdev = NULL;
1772 struct file *swap_file = NULL;
1773 struct address_space *mapping;
1774 unsigned int type;
1775 int i, prev;
1776 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 union swap_header *swap_header = NULL;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001778 unsigned int nr_good_pages = 0;
1779 int nr_extents = 0;
Hugh Dickins53092a72005-09-03 15:54:34 -07001780 sector_t span;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned long maxpages = 1;
Hugh Dickins73fd8742009-01-06 14:39:47 -08001782 unsigned long swapfilepages;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001783 unsigned short *swap_map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct page *page = NULL;
1785 struct inode *inode = NULL;
1786 int did_down = 0;
1787
1788 if (!capable(CAP_SYS_ADMIN))
1789 return -EPERM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001790 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 p = swap_info;
1792 for (type = 0 ; type < nr_swapfiles ; type++,p++)
1793 if (!(p->flags & SWP_USED))
1794 break;
1795 error = -EPERM;
Christoph Lameter06972122006-06-23 02:03:35 -07001796 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001797 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto out;
1799 }
1800 if (type >= nr_swapfiles)
1801 nr_swapfiles = type+1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001802 memset(p, 0, sizeof(*p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 INIT_LIST_HEAD(&p->extent_list);
1804 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001806 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 name = getname(specialfile);
1808 error = PTR_ERR(name);
1809 if (IS_ERR(name)) {
1810 name = NULL;
1811 goto bad_swap_2;
1812 }
1813 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
1814 error = PTR_ERR(swap_file);
1815 if (IS_ERR(swap_file)) {
1816 swap_file = NULL;
1817 goto bad_swap_2;
1818 }
1819
1820 p->swap_file = swap_file;
1821 mapping = swap_file->f_mapping;
1822 inode = mapping->host;
1823
1824 error = -EBUSY;
1825 for (i = 0; i < nr_swapfiles; i++) {
1826 struct swap_info_struct *q = &swap_info[i];
1827
1828 if (i == type || !q->swap_file)
1829 continue;
1830 if (mapping == q->swap_file->f_mapping)
1831 goto bad_swap;
1832 }
1833
1834 error = -EINVAL;
1835 if (S_ISBLK(inode->i_mode)) {
1836 bdev = I_BDEV(inode);
1837 error = bd_claim(bdev, sys_swapon);
1838 if (error < 0) {
1839 bdev = NULL;
Rob Landleyf7b3a432005-09-22 21:44:27 -07001840 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto bad_swap;
1842 }
1843 p->old_block_size = block_size(bdev);
1844 error = set_blocksize(bdev, PAGE_SIZE);
1845 if (error < 0)
1846 goto bad_swap;
1847 p->bdev = bdev;
1848 } else if (S_ISREG(inode->i_mode)) {
1849 p->bdev = inode->i_sb->s_bdev;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001850 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 did_down = 1;
1852 if (IS_SWAPFILE(inode)) {
1853 error = -EBUSY;
1854 goto bad_swap;
1855 }
1856 } else {
1857 goto bad_swap;
1858 }
1859
Hugh Dickins73fd8742009-01-06 14:39:47 -08001860 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 /*
1863 * Read the swap header.
1864 */
1865 if (!mapping->a_ops->readpage) {
1866 error = -EINVAL;
1867 goto bad_swap;
1868 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07001869 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (IS_ERR(page)) {
1871 error = PTR_ERR(page);
1872 goto bad_swap;
1873 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001874 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Hugh Dickins81e33972009-01-06 14:39:49 -08001876 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
Jesper Juhle97a3112006-01-11 01:50:28 +01001877 printk(KERN_ERR "Unable to find swap-space signature\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 error = -EINVAL;
1879 goto bad_swap;
1880 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001881
Hugh Dickins81e33972009-01-06 14:39:49 -08001882 /* swap partition endianess hack... */
1883 if (swab32(swap_header->info.version) == 1) {
1884 swab32s(&swap_header->info.version);
1885 swab32s(&swap_header->info.last_page);
1886 swab32s(&swap_header->info.nr_badpages);
1887 for (i = 0; i < swap_header->info.nr_badpages; i++)
1888 swab32s(&swap_header->info.badpages[i]);
1889 }
1890 /* Check the swap header's sub-version */
1891 if (swap_header->info.version != 1) {
1892 printk(KERN_WARNING
1893 "Unable to handle swap header version %d\n",
1894 swap_header->info.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 error = -EINVAL;
1896 goto bad_swap;
Hugh Dickins81e33972009-01-06 14:39:49 -08001897 }
1898
1899 p->lowest_bit = 1;
1900 p->cluster_next = 1;
1901
1902 /*
1903 * Find out how many pages are allowed for a single swap
1904 * device. There are two limiting factors: 1) the number of
1905 * bits for the swap offset in the swp_entry_t type and
1906 * 2) the number of bits in the a swap pte as defined by
1907 * the different architectures. In order to find the
1908 * largest possible bit mask a swap entry with swap type 0
1909 * and swap offset ~0UL is created, encoded to a swap pte,
1910 * decoded to a swp_entry_t again and finally the swap
1911 * offset is extracted. This will mask all the bits from
1912 * the initial ~0UL mask that can't be encoded in either
1913 * the swp_entry_t or the architecture definition of a
1914 * swap pte.
1915 */
1916 maxpages = swp_offset(pte_to_swp_entry(
1917 swp_entry_to_pte(swp_entry(0, ~0UL)))) - 1;
1918 if (maxpages > swap_header->info.last_page)
1919 maxpages = swap_header->info.last_page;
1920 p->highest_bit = maxpages - 1;
1921
1922 error = -EINVAL;
1923 if (!maxpages)
1924 goto bad_swap;
1925 if (swapfilepages && maxpages > swapfilepages) {
1926 printk(KERN_WARNING
1927 "Swap area shorter than signature indicates\n");
1928 goto bad_swap;
1929 }
1930 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
1931 goto bad_swap;
1932 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
1933 goto bad_swap;
1934
1935 /* OK, set up the swap map and apply the bad block list */
1936 swap_map = vmalloc(maxpages * sizeof(short));
1937 if (!swap_map) {
1938 error = -ENOMEM;
1939 goto bad_swap;
1940 }
1941
1942 memset(swap_map, 0, maxpages * sizeof(short));
1943 for (i = 0; i < swap_header->info.nr_badpages; i++) {
1944 int page_nr = swap_header->info.badpages[i];
1945 if (page_nr <= 0 || page_nr >= swap_header->info.last_page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 error = -EINVAL;
1947 goto bad_swap;
1948 }
Hugh Dickins81e33972009-01-06 14:39:49 -08001949 swap_map[page_nr] = SWAP_MAP_BAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001951
1952 error = swap_cgroup_swapon(type, maxpages);
1953 if (error)
1954 goto bad_swap;
1955
Hugh Dickins81e33972009-01-06 14:39:49 -08001956 nr_good_pages = swap_header->info.last_page -
1957 swap_header->info.nr_badpages -
1958 1 /* header page */;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001959
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001960 if (nr_good_pages) {
Hugh Dickins78ecba02008-07-23 21:28:23 -07001961 swap_map[0] = SWAP_MAP_BAD;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001962 p->max = maxpages;
1963 p->pages = nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001964 nr_extents = setup_swap_extents(p, &span);
1965 if (nr_extents < 0) {
1966 error = nr_extents;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001967 goto bad_swap;
Hugh Dickins53092a72005-09-03 15:54:34 -07001968 }
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001969 nr_good_pages = p->pages;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (!nr_good_pages) {
1972 printk(KERN_WARNING "Empty swap-file\n");
1973 error = -EINVAL;
1974 goto bad_swap;
1975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Hugh Dickins20137a42009-01-06 14:39:54 -08001977 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
1978 p->flags |= SWP_SOLIDSTATE;
Hugh Dickins20137a42009-01-06 14:39:54 -08001979 p->cluster_next = 1 + (random32() % p->highest_bit);
1980 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001981 if (discard_swap(p) == 0)
1982 p->flags |= SWP_DISCARDABLE;
1983
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001984 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001985 spin_lock(&swap_lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001986 if (swap_flags & SWAP_FLAG_PREFER)
1987 p->prio =
1988 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
1989 else
1990 p->prio = --least_priority;
1991 p->swap_map = swap_map;
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001992 p->flags |= SWP_WRITEOK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 nr_swap_pages += nr_good_pages;
1994 total_swap_pages += nr_good_pages;
Hugh Dickins53092a72005-09-03 15:54:34 -07001995
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001996 printk(KERN_INFO "Adding %uk swap on %s. "
Hugh Dickins20137a42009-01-06 14:39:54 -08001997 "Priority:%d extents:%d across:%lluk %s%s\n",
Hugh Dickins53092a72005-09-03 15:54:34 -07001998 nr_good_pages<<(PAGE_SHIFT-10), name, p->prio,
Hugh Dickins6a6ba832009-01-06 14:39:51 -08001999 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
Hugh Dickins20137a42009-01-06 14:39:54 -08002000 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2001 (p->flags & SWP_DISCARDABLE) ? "D" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /* insert swap space into swap_list: */
2004 prev = -1;
2005 for (i = swap_list.head; i >= 0; i = swap_info[i].next) {
2006 if (p->prio >= swap_info[i].prio) {
2007 break;
2008 }
2009 prev = i;
2010 }
2011 p->next = i;
2012 if (prev < 0) {
2013 swap_list.head = swap_list.next = p - swap_info;
2014 } else {
2015 swap_info[prev].next = p - swap_info;
2016 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002017 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002018 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 error = 0;
2020 goto out;
2021bad_swap:
2022 if (bdev) {
2023 set_blocksize(bdev, p->old_block_size);
2024 bd_release(bdev);
2025 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002026 destroy_swap_extents(p);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08002027 swap_cgroup_swapoff(type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028bad_swap_2:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002029 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002032 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 vfree(swap_map);
2034 if (swap_file)
2035 filp_close(swap_file, NULL);
2036out:
2037 if (page && !IS_ERR(page)) {
2038 kunmap(page);
2039 page_cache_release(page);
2040 }
2041 if (name)
2042 putname(name);
2043 if (did_down) {
2044 if (!error)
2045 inode->i_flags |= S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002046 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048 return error;
2049}
2050
2051void si_swapinfo(struct sysinfo *val)
2052{
2053 unsigned int i;
2054 unsigned long nr_to_be_unused = 0;
2055
Hugh Dickins5d337b92005-09-03 15:54:41 -07002056 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 for (i = 0; i < nr_swapfiles; i++) {
2058 if (!(swap_info[i].flags & SWP_USED) ||
2059 (swap_info[i].flags & SWP_WRITEOK))
2060 continue;
2061 nr_to_be_unused += swap_info[i].inuse_pages;
2062 }
2063 val->freeswap = nr_swap_pages + nr_to_be_unused;
2064 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002065 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
2068/*
2069 * Verify that a swap entry is valid and increment its swap map count.
2070 *
2071 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
2072 * "permanent", but will be reclaimed by the next swapoff.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002073 * Returns error code in following case.
2074 * - success -> 0
2075 * - swp_entry is invalid -> EINVAL
2076 * - swp_entry is migration entry -> EINVAL
2077 * - swap-cache reference is requested but there is already one. -> EEXIST
2078 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002080static int __swap_duplicate(swp_entry_t entry, bool cache)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct swap_info_struct * p;
2083 unsigned long offset, type;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002084 int result = -EINVAL;
2085 int count;
2086 bool has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Andi Kleena7420aa2009-09-16 11:50:05 +02002088 if (non_swap_entry(entry))
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002089 return -EINVAL;
Christoph Lameter06972122006-06-23 02:03:35 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 type = swp_type(entry);
2092 if (type >= nr_swapfiles)
2093 goto bad_file;
2094 p = type + swap_info;
2095 offset = swp_offset(entry);
2096
Hugh Dickins5d337b92005-09-03 15:54:41 -07002097 spin_lock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002098
2099 if (unlikely(offset >= p->max))
2100 goto unlock_out;
2101
2102 count = swap_count(p->swap_map[offset]);
2103 has_cache = swap_has_cache(p->swap_map[offset]);
2104
2105 if (cache == SWAP_CACHE) { /* called for swapcache/swapin-readahead */
2106
2107 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
2108 if (!has_cache && count) {
2109 p->swap_map[offset] = encode_swapmap(count, true);
2110 result = 0;
2111 } else if (has_cache) /* someone added cache */
2112 result = -EEXIST;
2113 else if (!count) /* no users */
2114 result = -ENOENT;
2115
2116 } else if (count || has_cache) {
2117 if (count < SWAP_MAP_MAX - 1) {
2118 p->swap_map[offset] = encode_swapmap(count + 1,
2119 has_cache);
2120 result = 0;
2121 } else if (count <= SWAP_MAP_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (swap_overflow++ < 5)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002123 printk(KERN_WARNING
2124 "swap_dup: swap entry overflow\n");
2125 p->swap_map[offset] = encode_swapmap(SWAP_MAP_MAX,
2126 has_cache);
2127 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002129 } else
2130 result = -ENOENT; /* unused swap entry */
2131unlock_out:
Hugh Dickins5d337b92005-09-03 15:54:41 -07002132 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133out:
2134 return result;
2135
2136bad_file:
2137 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2138 goto out;
2139}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002140/*
2141 * increase reference count of swap entry by 1.
2142 */
2143void swap_duplicate(swp_entry_t entry)
2144{
2145 __swap_duplicate(entry, SWAP_MAP);
2146}
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 *
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002151 * Called when allocating swap cache for exising 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{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002158 return __swap_duplicate(entry, SWAP_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002159}
2160
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162struct swap_info_struct *
2163get_swap_info_struct(unsigned type)
2164{
2165 return &swap_info[type];
2166}
2167
2168/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07002169 * swap_lock prevents swap_map being freed. Don't grab an extra
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * reference on the swaphandle, it doesn't matter if it becomes unused.
2171 */
2172int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
2173{
Hugh Dickins89528982008-02-04 22:28:45 -08002174 struct swap_info_struct *si;
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002175 int our_page_cluster = page_cluster;
Hugh Dickins89528982008-02-04 22:28:45 -08002176 pgoff_t target, toff;
2177 pgoff_t base, end;
2178 int nr_pages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Hugh Dickins3f9e7942006-09-29 02:01:26 -07002180 if (!our_page_cluster) /* no readahead */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
Hugh Dickins89528982008-02-04 22:28:45 -08002182
2183 si = &swap_info[swp_type(entry)];
2184 target = swp_offset(entry);
2185 base = (target >> our_page_cluster) << our_page_cluster;
2186 end = base + (1 << our_page_cluster);
2187 if (!base) /* first page is swap header */
2188 base++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Hugh Dickins5d337b92005-09-03 15:54:41 -07002190 spin_lock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002191 if (end > si->max) /* don't go beyond end of map */
2192 end = si->max;
2193
2194 /* Count contiguous allocated slots above our target */
2195 for (toff = target; ++toff < end; nr_pages++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* Don't read in free or bad pages */
Hugh Dickins89528982008-02-04 22:28:45 -08002197 if (!si->swap_map[toff])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002199 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 break;
Hugh Dickins89528982008-02-04 22:28:45 -08002201 }
2202 /* Count contiguous allocated slots below our target */
2203 for (toff = target; --toff >= base; nr_pages++) {
2204 /* Don't read in free or bad pages */
2205 if (!si->swap_map[toff])
2206 break;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002207 if (swap_count(si->swap_map[toff]) == SWAP_MAP_BAD)
Hugh Dickins89528982008-02-04 22:28:45 -08002208 break;
2209 }
Hugh Dickins5d337b92005-09-03 15:54:41 -07002210 spin_unlock(&swap_lock);
Hugh Dickins89528982008-02-04 22:28:45 -08002211
2212 /*
2213 * Indicate starting offset, and return number of pages to get:
2214 * if only 1, say 0, since there's then no readahead to be done.
2215 */
2216 *offset = ++toff;
2217 return nr_pages? ++nr_pages: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}