blob: 5f62c5fbd2b0c56cb50e26b765a9ae391f339b7d [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>
Hugh Dickins072441e2011-06-27 16:18:02 -070017#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#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>
Hugh Dickins5ad64682009-12-14 17:59:24 -080024#include <linux/ksm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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>
Kay Sievers66d7dd52010-10-26 14:22:06 -070032#include <linux/poll.h>
David Rientjes72788c32011-05-24 17:11:40 -070033#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/pgtable.h>
36#include <asm/tlbflush.h>
37#include <linux/swapops.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080038#include <linux/page_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Hugh Dickins570a3352009-12-14 17:58:46 -080040static bool swap_count_continued(struct swap_info_struct *, pgoff_t,
41 unsigned char);
42static void free_swap_count_continuations(struct swap_info_struct *);
Lee Schermerhornd4906e12009-12-14 17:58:49 -080043static sector_t map_swap_entry(swp_entry_t, struct block_device**);
Hugh Dickins570a3352009-12-14 17:58:46 -080044
Adrian Bunk7c363b82008-07-25 19:46:24 -070045static DEFINE_SPINLOCK(swap_lock);
46static unsigned int nr_swapfiles;
Shaohua Li1abbca72013-02-22 16:34:38 -080047atomic_long_t nr_swap_pages;
48/* protected with swap_lock. reading in vm_swap_full() doesn't need lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049long total_swap_pages;
Hugh Dickins78ecba02008-07-23 21:28:23 -070050static int least_priority;
Shaohua Li1abbca72013-02-22 16:34:38 -080051static atomic_t highest_priority_index = ATOMIC_INIT(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static const char Bad_file[] = "Bad swap file entry ";
54static const char Unused_file[] = "Unused swap file entry ";
55static const char Bad_offset[] = "Bad swap offset entry ";
56static const char Unused_offset[] = "Unused swap offset entry ";
57
Adrian Bunk7c363b82008-07-25 19:46:24 -070058static struct swap_list_t swap_list = {-1, -1};
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Hugh Dickinsefa90a92009-12-14 17:58:41 -080060static struct swap_info_struct *swap_info[MAX_SWAPFILES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Ingo Molnarfc0abb12006-01-18 17:42:33 -080062static DEFINE_MUTEX(swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Kay Sievers66d7dd52010-10-26 14:22:06 -070064static DECLARE_WAIT_QUEUE_HEAD(proc_poll_wait);
65/* Activity counter to indicate that a swapon or swapoff has occurred */
66static atomic_t proc_poll_event = ATOMIC_INIT(0);
67
Hugh Dickins8d69aae2009-12-14 17:58:45 -080068static inline unsigned char swap_count(unsigned char ent)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070069{
Hugh Dickins570a3352009-12-14 17:58:46 -080070 return ent & ~SWAP_HAS_CACHE; /* may include SWAP_HAS_CONT flag */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070071}
72
Hugh Dickinsefa90a92009-12-14 17:58:41 -080073/* returns 1 if swap entry is freed */
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070074static int
75__try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset)
76{
Hugh Dickinsefa90a92009-12-14 17:58:41 -080077 swp_entry_t entry = swp_entry(si->type, offset);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -070078 struct page *page;
79 int ret = 0;
80
81 page = find_get_page(&swapper_space, entry.val);
82 if (!page)
83 return 0;
84 /*
85 * This function is called from scan_swap_map() and it's called
86 * by vmscan.c at reclaiming pages. So, we hold a lock on a page, here.
87 * We have to use trylock for avoiding deadlock. This is a special
88 * case and you should use try_to_free_swap() with explicit lock_page()
89 * in usual operations.
90 */
91 if (trylock_page(page)) {
92 ret = try_to_free_swap(page);
93 unlock_page(page);
94 }
95 page_cache_release(page);
96 return ret;
97}
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800100 * swapon tell device that all the old swap contents can be discarded,
101 * to allow the swap device to optimize its wear-levelling.
102 */
103static int discard_swap(struct swap_info_struct *si)
104{
105 struct swap_extent *se;
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800106 sector_t start_block;
107 sector_t nr_blocks;
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800108 int err = 0;
109
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800110 /* Do not discard the swap header page! */
111 se = &si->first_swap_extent;
112 start_block = (se->start_block + 1) << (PAGE_SHIFT - 9);
113 nr_blocks = ((sector_t)se->nr_pages - 1) << (PAGE_SHIFT - 9);
114 if (nr_blocks) {
115 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200116 nr_blocks, GFP_KERNEL, 0);
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800117 if (err)
118 return err;
119 cond_resched();
120 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800121
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800122 list_for_each_entry(se, &si->first_swap_extent.list, list) {
123 start_block = se->start_block << (PAGE_SHIFT - 9);
124 nr_blocks = (sector_t)se->nr_pages << (PAGE_SHIFT - 9);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800125
126 err = blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200127 nr_blocks, GFP_KERNEL, 0);
Hugh Dickins6a6ba832009-01-06 14:39:51 -0800128 if (err)
129 break;
130
131 cond_resched();
132 }
133 return err; /* That will often be -EOPNOTSUPP */
134}
135
Hugh Dickins7992fde2009-01-06 14:39:53 -0800136/*
137 * swap allocation tell device that a cluster of swap can now be discarded,
138 * to allow the swap device to optimize its wear-levelling.
139 */
140static void discard_swap_cluster(struct swap_info_struct *si,
141 pgoff_t start_page, pgoff_t nr_pages)
142{
143 struct swap_extent *se = si->curr_swap_extent;
144 int found_extent = 0;
145
146 while (nr_pages) {
147 struct list_head *lh;
148
149 if (se->start_page <= start_page &&
150 start_page < se->start_page + se->nr_pages) {
151 pgoff_t offset = start_page - se->start_page;
152 sector_t start_block = se->start_block + offset;
Hugh Dickins858a29902009-01-06 14:39:56 -0800153 sector_t nr_blocks = se->nr_pages - offset;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800154
155 if (nr_blocks > nr_pages)
156 nr_blocks = nr_pages;
157 start_page += nr_blocks;
158 nr_pages -= nr_blocks;
159
160 if (!found_extent++)
161 si->curr_swap_extent = se;
162
163 start_block <<= PAGE_SHIFT - 9;
164 nr_blocks <<= PAGE_SHIFT - 9;
165 if (blkdev_issue_discard(si->bdev, start_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200166 nr_blocks, GFP_NOIO, 0))
Hugh Dickins7992fde2009-01-06 14:39:53 -0800167 break;
168 }
169
170 lh = se->list.next;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800171 se = list_entry(lh, struct swap_extent, list);
172 }
173}
174
175static int wait_for_discard(void *word)
176{
177 schedule();
178 return 0;
179}
180
Hugh Dickins048c27f2005-09-03 15:54:40 -0700181#define SWAPFILE_CLUSTER 256
182#define LATENCY_LIMIT 256
183
Cesar Eduardo Barros24b8ff72011-03-22 16:33:38 -0700184static unsigned long scan_swap_map(struct swap_info_struct *si,
185 unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800187 unsigned long offset;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800188 unsigned long scan_base;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800189 unsigned long last_in_cluster = 0;
Hugh Dickins048c27f2005-09-03 15:54:40 -0700190 int latency_ration = LATENCY_LIMIT;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800191 int found_free_cluster = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800193 /*
Hugh Dickins7dfad412005-09-03 15:54:38 -0700194 * We try to cluster swap pages by allocating them sequentially
195 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
196 * way, however, we resort to first-free allocation, starting
197 * a new cluster. This prevents us from scattering swap pages
198 * all over the entire swap partition, so that we reduce
199 * overall disk seek times between swap pages. -- sct
200 * But we do now try to find an empty cluster. -Andrea
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800201 * And we let swap pages go all over an SSD partition. Hugh
Hugh Dickins7dfad412005-09-03 15:54:38 -0700202 */
203
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700204 si->flags += SWP_SCANNING;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800205 scan_base = offset = si->cluster_next;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800206
207 if (unlikely(!si->cluster_nr--)) {
208 if (si->pages - si->inuse_pages < SWAPFILE_CLUSTER) {
209 si->cluster_nr = SWAPFILE_CLUSTER - 1;
210 goto checks;
211 }
Hugh Dickins7992fde2009-01-06 14:39:53 -0800212 if (si->flags & SWP_DISCARDABLE) {
213 /*
214 * Start range check on racing allocations, in case
215 * they overlap the cluster we eventually decide on
216 * (we scan without swap_lock to allow preemption).
217 * It's hardly conceivable that cluster_nr could be
218 * wrapped during our scan, but don't depend on it.
219 */
220 if (si->lowest_alloc)
221 goto checks;
222 si->lowest_alloc = si->max;
223 si->highest_alloc = 0;
224 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800225 spin_unlock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700226
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800227 /*
228 * If seek is expensive, start searching for new cluster from
229 * start of partition, to minimize the span of allocated swap.
230 * But if seek is cheap, search from our current position, so
231 * that swap is allocated from all over the partition: if the
232 * Flash Translation Layer only remaps within limited zones,
233 * we don't want to wear out the first zone too quickly.
234 */
235 if (!(si->flags & SWP_SOLIDSTATE))
236 scan_base = offset = si->lowest_bit;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700237 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
238
239 /* Locate the first empty (unaligned) cluster */
240 for (; last_in_cluster <= si->highest_bit; offset++) {
241 if (si->swap_map[offset])
242 last_in_cluster = offset + SWAPFILE_CLUSTER;
243 else if (offset == last_in_cluster) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800244 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800245 offset -= SWAPFILE_CLUSTER - 1;
246 si->cluster_next = offset;
247 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800248 found_free_cluster = 1;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800249 goto checks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700251 if (unlikely(--latency_ration < 0)) {
252 cond_resched();
253 latency_ration = LATENCY_LIMIT;
254 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700255 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800256
257 offset = si->lowest_bit;
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800258 last_in_cluster = offset + SWAPFILE_CLUSTER - 1;
259
260 /* Locate the first empty (unaligned) cluster */
261 for (; last_in_cluster < scan_base; offset++) {
262 if (si->swap_map[offset])
263 last_in_cluster = offset + SWAPFILE_CLUSTER;
264 else if (offset == last_in_cluster) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800265 spin_lock(&si->lock);
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800266 offset -= SWAPFILE_CLUSTER - 1;
267 si->cluster_next = offset;
268 si->cluster_nr = SWAPFILE_CLUSTER - 1;
269 found_free_cluster = 1;
270 goto checks;
271 }
272 if (unlikely(--latency_ration < 0)) {
273 cond_resched();
274 latency_ration = LATENCY_LIMIT;
275 }
276 }
277
278 offset = scan_base;
Shaohua Li1abbca72013-02-22 16:34:38 -0800279 spin_lock(&si->lock);
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800280 si->cluster_nr = SWAPFILE_CLUSTER - 1;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800281 si->lowest_alloc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700283
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800284checks:
285 if (!(si->flags & SWP_WRITEOK))
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700286 goto no_page;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700287 if (!si->highest_bit)
288 goto no_page;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800289 if (offset > si->highest_bit)
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800290 scan_base = offset = si->lowest_bit;
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700291
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700292 /* reuse swap entry of cache-only swap if not busy. */
293 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700294 int swap_was_freed;
Shaohua Li1abbca72013-02-22 16:34:38 -0800295 spin_unlock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700296 swap_was_freed = __try_to_reclaim_swap(si, offset);
Shaohua Li1abbca72013-02-22 16:34:38 -0800297 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700298 /* entry was freed successfully, try to use this again */
299 if (swap_was_freed)
300 goto checks;
301 goto scan; /* check next one */
302 }
303
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800304 if (si->swap_map[offset])
305 goto scan;
Hugh Dickins7dfad412005-09-03 15:54:38 -0700306
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800307 if (offset == si->lowest_bit)
308 si->lowest_bit++;
309 if (offset == si->highest_bit)
310 si->highest_bit--;
311 si->inuse_pages++;
312 if (si->inuse_pages == si->pages) {
313 si->lowest_bit = si->max;
314 si->highest_bit = 0;
315 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800316 si->swap_map[offset] = usage;
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800317 si->cluster_next = offset + 1;
318 si->flags -= SWP_SCANNING;
Hugh Dickins7992fde2009-01-06 14:39:53 -0800319
320 if (si->lowest_alloc) {
321 /*
322 * Only set when SWP_DISCARDABLE, and there's a scan
323 * for a free cluster in progress or just completed.
324 */
325 if (found_free_cluster) {
326 /*
327 * To optimize wear-levelling, discard the
328 * old data of the cluster, taking care not to
329 * discard any of its pages that have already
330 * been allocated by racing tasks (offset has
331 * already stepped over any at the beginning).
332 */
333 if (offset < si->highest_alloc &&
334 si->lowest_alloc <= last_in_cluster)
335 last_in_cluster = si->lowest_alloc - 1;
336 si->flags |= SWP_DISCARDING;
Shaohua Li1abbca72013-02-22 16:34:38 -0800337 spin_unlock(&si->lock);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800338
339 if (offset < last_in_cluster)
340 discard_swap_cluster(si, offset,
341 last_in_cluster - offset + 1);
342
Shaohua Li1abbca72013-02-22 16:34:38 -0800343 spin_lock(&si->lock);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800344 si->lowest_alloc = 0;
345 si->flags &= ~SWP_DISCARDING;
346
347 smp_mb(); /* wake_up_bit advises this */
348 wake_up_bit(&si->flags, ilog2(SWP_DISCARDING));
349
350 } else if (si->flags & SWP_DISCARDING) {
351 /*
352 * Delay using pages allocated by racing tasks
353 * until the whole discard has been issued. We
354 * could defer that delay until swap_writepage,
355 * but it's easier to keep this self-contained.
356 */
Shaohua Li1abbca72013-02-22 16:34:38 -0800357 spin_unlock(&si->lock);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800358 wait_on_bit(&si->flags, ilog2(SWP_DISCARDING),
359 wait_for_discard, TASK_UNINTERRUPTIBLE);
Shaohua Li1abbca72013-02-22 16:34:38 -0800360 spin_lock(&si->lock);
Hugh Dickins7992fde2009-01-06 14:39:53 -0800361 } else {
362 /*
363 * Note pages allocated by racing tasks while
364 * scan for a free cluster is in progress, so
365 * that its final discard can exclude them.
366 */
367 if (offset < si->lowest_alloc)
368 si->lowest_alloc = offset;
369 if (offset > si->highest_alloc)
370 si->highest_alloc = offset;
371 }
372 }
Hugh Dickinsebebbbe2009-01-06 14:39:50 -0800373 return offset;
374
375scan:
Shaohua Li1abbca72013-02-22 16:34:38 -0800376 spin_unlock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700377 while (++offset <= si->highest_bit) {
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700378 if (!si->swap_map[offset]) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800379 spin_lock(&si->lock);
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700380 goto checks;
381 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700382 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800383 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700384 goto checks;
385 }
Hugh Dickins048c27f2005-09-03 15:54:40 -0700386 if (unlikely(--latency_ration < 0)) {
387 cond_resched();
388 latency_ration = LATENCY_LIMIT;
389 }
Hugh Dickins7dfad412005-09-03 15:54:38 -0700390 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800391 offset = si->lowest_bit;
392 while (++offset < scan_base) {
393 if (!si->swap_map[offset]) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800394 spin_lock(&si->lock);
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800395 goto checks;
396 }
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700397 if (vm_swap_full() && si->swap_map[offset] == SWAP_HAS_CACHE) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800398 spin_lock(&si->lock);
KAMEZAWA Hiroyukic9e44412009-06-16 15:32:54 -0700399 goto checks;
400 }
Hugh Dickinsc60aa172009-01-06 14:39:55 -0800401 if (unlikely(--latency_ration < 0)) {
402 cond_resched();
403 latency_ration = LATENCY_LIMIT;
404 }
405 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800406 spin_lock(&si->lock);
Hugh Dickins7dfad412005-09-03 15:54:38 -0700407
408no_page:
Hugh Dickins52b7efdb2005-09-03 15:54:39 -0700409 si->flags -= SWP_SCANNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413swp_entry_t get_swap_page(void)
414{
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700415 struct swap_info_struct *si;
416 pgoff_t offset;
417 int type, next;
418 int wrapped = 0;
Shaohua Li1abbca72013-02-22 16:34:38 -0800419 int hp_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Hugh Dickins5d337b92005-09-03 15:54:41 -0700421 spin_lock(&swap_lock);
Shaohua Li1abbca72013-02-22 16:34:38 -0800422 if (atomic_long_read(&nr_swap_pages) <= 0)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700423 goto noswap;
Shaohua Li1abbca72013-02-22 16:34:38 -0800424 atomic_long_dec(&nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700426 for (type = swap_list.next; type >= 0 && wrapped < 2; type = next) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800427 hp_index = atomic_xchg(&highest_priority_index, -1);
428 /*
429 * highest_priority_index records current highest priority swap
430 * type which just frees swap entries. If its priority is
431 * higher than that of swap_list.next swap type, we use it. It
432 * isn't protected by swap_lock, so it can be an invalid value
433 * if the corresponding swap type is swapoff. We double check
434 * the flags here. It's even possible the swap type is swapoff
435 * and swapon again and its priority is changed. In such rare
436 * case, low prority swap type might be used, but eventually
437 * high priority swap will be used after several rounds of
438 * swap.
439 */
440 if (hp_index != -1 && hp_index != type &&
441 swap_info[type]->prio < swap_info[hp_index]->prio &&
442 (swap_info[hp_index]->flags & SWP_WRITEOK)) {
443 type = hp_index;
444 swap_list.next = type;
445 }
446
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800447 si = swap_info[type];
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700448 next = si->next;
449 if (next < 0 ||
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800450 (!wrapped && si->prio != swap_info[next]->prio)) {
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700451 next = swap_list.head;
452 wrapped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700454
Shaohua Li1abbca72013-02-22 16:34:38 -0800455 spin_lock(&si->lock);
456 if (!si->highest_bit) {
457 spin_unlock(&si->lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700458 continue;
Shaohua Li1abbca72013-02-22 16:34:38 -0800459 }
460 if (!(si->flags & SWP_WRITEOK)) {
461 spin_unlock(&si->lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700462 continue;
Shaohua Li1abbca72013-02-22 16:34:38 -0800463 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700464
465 swap_list.next = next;
Shaohua Li1abbca72013-02-22 16:34:38 -0800466
467 spin_unlock(&swap_lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700468 /* This is called for allocating swap entry for cache */
Hugh Dickins253d5532009-12-14 17:58:44 -0800469 offset = scan_swap_map(si, SWAP_HAS_CACHE);
Shaohua Li1abbca72013-02-22 16:34:38 -0800470 spin_unlock(&si->lock);
471 if (offset)
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700472 return swp_entry(type, offset);
Shaohua Li1abbca72013-02-22 16:34:38 -0800473 spin_lock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700474 next = swap_list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700476
Shaohua Li1abbca72013-02-22 16:34:38 -0800477 atomic_long_inc(&nr_swap_pages);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700478noswap:
Hugh Dickins5d337b92005-09-03 15:54:41 -0700479 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -0700480 return (swp_entry_t) {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Hugh Dickins910321e2010-09-09 16:38:07 -0700483/* The only caller of this function is now susupend routine */
484swp_entry_t get_swap_page_of_type(int type)
485{
486 struct swap_info_struct *si;
487 pgoff_t offset;
488
Hugh Dickins910321e2010-09-09 16:38:07 -0700489 si = swap_info[type];
Shaohua Li1abbca72013-02-22 16:34:38 -0800490 spin_lock(&si->lock);
Hugh Dickins910321e2010-09-09 16:38:07 -0700491 if (si && (si->flags & SWP_WRITEOK)) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800492 atomic_long_dec(&nr_swap_pages);
Hugh Dickins910321e2010-09-09 16:38:07 -0700493 /* This is called for allocating swap entry, not cache */
494 offset = scan_swap_map(si, 1);
495 if (offset) {
Shaohua Li1abbca72013-02-22 16:34:38 -0800496 spin_unlock(&si->lock);
Hugh Dickins910321e2010-09-09 16:38:07 -0700497 return swp_entry(type, offset);
498 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800499 atomic_long_inc(&nr_swap_pages);
Hugh Dickins910321e2010-09-09 16:38:07 -0700500 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800501 spin_unlock(&si->lock);
Hugh Dickins910321e2010-09-09 16:38:07 -0700502 return (swp_entry_t) {0};
503}
504
Hugh Dickins73c34b62009-12-14 17:58:43 -0800505static struct swap_info_struct *swap_info_get(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800507 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 unsigned long offset, type;
509
510 if (!entry.val)
511 goto out;
512 type = swp_type(entry);
513 if (type >= nr_swapfiles)
514 goto bad_nofile;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800515 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (!(p->flags & SWP_USED))
517 goto bad_device;
518 offset = swp_offset(entry);
519 if (offset >= p->max)
520 goto bad_offset;
521 if (!p->swap_map[offset])
522 goto bad_free;
Shaohua Li1abbca72013-02-22 16:34:38 -0800523 spin_lock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return p;
525
526bad_free:
527 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_offset, entry.val);
528 goto out;
529bad_offset:
530 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_offset, entry.val);
531 goto out;
532bad_device:
533 printk(KERN_ERR "swap_free: %s%08lx\n", Unused_file, entry.val);
534 goto out;
535bad_nofile:
536 printk(KERN_ERR "swap_free: %s%08lx\n", Bad_file, entry.val);
537out:
538 return NULL;
Hugh Dickins886bb7e2009-01-06 14:39:48 -0800539}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Shaohua Li1abbca72013-02-22 16:34:38 -0800541/*
542 * This swap type frees swap entry, check if it is the highest priority swap
543 * type which just frees swap entry. get_swap_page() uses
544 * highest_priority_index to search highest priority swap type. The
545 * swap_info_struct.lock can't protect us if there are multiple swap types
546 * active, so we use atomic_cmpxchg.
547 */
548static void set_highest_priority_index(int type)
549{
550 int old_hp_index, new_hp_index;
551
552 do {
553 old_hp_index = atomic_read(&highest_priority_index);
554 if (old_hp_index != -1 &&
555 swap_info[old_hp_index]->prio >= swap_info[type]->prio)
556 break;
557 new_hp_index = type;
558 } while (atomic_cmpxchg(&highest_priority_index,
559 old_hp_index, new_hp_index) != old_hp_index);
560}
561
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800562static unsigned char swap_entry_free(struct swap_info_struct *p,
563 swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Hugh Dickins253d5532009-12-14 17:58:44 -0800565 unsigned long offset = swp_offset(entry);
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800566 unsigned char count;
567 unsigned char has_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700569 count = p->swap_map[offset];
Hugh Dickins253d5532009-12-14 17:58:44 -0800570 has_cache = count & SWAP_HAS_CACHE;
571 count &= ~SWAP_HAS_CACHE;
572
573 if (usage == SWAP_HAS_CACHE) {
574 VM_BUG_ON(!has_cache);
575 has_cache = 0;
Hugh Dickinsaaa46862009-12-14 17:58:47 -0800576 } else if (count == SWAP_MAP_SHMEM) {
577 /*
578 * Or we could insist on shmem.c using a special
579 * swap_shmem_free() and free_shmem_swap_and_cache()...
580 */
581 count = 0;
Hugh Dickins570a3352009-12-14 17:58:46 -0800582 } else if ((count & ~COUNT_CONTINUED) <= SWAP_MAP_MAX) {
583 if (count == COUNT_CONTINUED) {
584 if (swap_count_continued(p, offset, count))
585 count = SWAP_MAP_MAX | COUNT_CONTINUED;
586 else
587 count = SWAP_MAP_MAX;
588 } else
589 count--;
590 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800591
592 if (!count)
593 mem_cgroup_uncharge_swap(entry);
594
595 usage = count | has_cache;
596 p->swap_map[offset] = usage;
597
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700598 /* free if no reference */
Hugh Dickins253d5532009-12-14 17:58:44 -0800599 if (!usage) {
Nitin Guptab3a27d02010-05-17 11:02:43 +0530600 struct gendisk *disk = p->bdev->bd_disk;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700601 if (offset < p->lowest_bit)
602 p->lowest_bit = offset;
603 if (offset > p->highest_bit)
604 p->highest_bit = offset;
Shaohua Li1abbca72013-02-22 16:34:38 -0800605 set_highest_priority_index(p->type);
606 atomic_long_inc(&nr_swap_pages);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700607 p->inuse_pages--;
Nitin Guptab3a27d02010-05-17 11:02:43 +0530608 if ((p->flags & SWP_BLKDEV) &&
609 disk->fops->swap_slot_free_notify)
610 disk->fops->swap_slot_free_notify(p->bdev, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
Hugh Dickins253d5532009-12-14 17:58:44 -0800612
613 return usage;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * Caller has made sure that the swapdevice corresponding to entry
618 * is still around or has not been recycled.
619 */
620void swap_free(swp_entry_t entry)
621{
Hugh Dickins73c34b62009-12-14 17:58:43 -0800622 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 p = swap_info_get(entry);
625 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800626 swap_entry_free(p, entry, 1);
Shaohua Li1abbca72013-02-22 16:34:38 -0800627 spin_unlock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629}
630
631/*
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700632 * Called after dropping swapcache to decrease refcnt to swap entries.
633 */
634void swapcache_free(swp_entry_t entry, struct page *page)
635{
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700636 struct swap_info_struct *p;
Hugh Dickins8d69aae2009-12-14 17:58:45 -0800637 unsigned char count;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700638
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700639 p = swap_info_get(entry);
640 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800641 count = swap_entry_free(p, entry, SWAP_HAS_CACHE);
642 if (page)
643 mem_cgroup_uncharge_swapcache(page, entry, count != 0);
Shaohua Li1abbca72013-02-22 16:34:38 -0800644 spin_unlock(&p->lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700645 }
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -0700646}
647
648/*
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700649 * How many references to page are currently swapped out?
Hugh Dickins570a3352009-12-14 17:58:46 -0800650 * This does not give an exact answer when swap count is continued,
651 * but does include the high COUNT_CONTINUED flag to allow for that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700653static inline int page_swapcount(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700655 int count = 0;
656 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 swp_entry_t entry;
658
Hugh Dickins4c21e2f2005-10-29 18:16:40 -0700659 entry.val = page_private(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 p = swap_info_get(entry);
661 if (p) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -0700662 count = swap_count(p->swap_map[swp_offset(entry)]);
Shaohua Li1abbca72013-02-22 16:34:38 -0800663 spin_unlock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700665 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/*
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800669 * We can write to an anon page without COW if there are no other references
670 * to it. And as a side-effect, free up its swap: because the old content
671 * on disk will never be read, and seeking back there to write new content
672 * later would only waste time away from clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800674int reuse_swap_page(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700676 int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Hugh Dickins51726b12009-01-06 14:39:25 -0800678 VM_BUG_ON(!PageLocked(page));
Hugh Dickins5ad64682009-12-14 17:59:24 -0800679 if (unlikely(PageKsm(page)))
680 return 0;
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700681 count = page_mapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800682 if (count <= 1 && PageSwapCache(page)) {
Hugh Dickinsc475a8a2005-06-21 17:15:12 -0700683 count += page_swapcount(page);
Hugh Dickins7b1fe592009-01-06 14:39:34 -0800684 if (count == 1 && !PageWriteback(page)) {
685 delete_from_swap_cache(page);
686 SetPageDirty(page);
687 }
688 }
Hugh Dickins5ad64682009-12-14 17:59:24 -0800689 return count <= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
692/*
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800693 * If swap is getting full, or if there are no more mappings of this page,
694 * then try_to_free_swap is called to free its swap space.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800696int try_to_free_swap(struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Hugh Dickins51726b12009-01-06 14:39:25 -0800698 VM_BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (!PageSwapCache(page))
701 return 0;
702 if (PageWriteback(page))
703 return 0;
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800704 if (page_swapcount(page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
706
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700707 /*
708 * Once hibernation has begun to create its image of memory,
709 * there's a danger that one of the calls to try_to_free_swap()
710 * - most probably a call from __try_to_reclaim_swap() while
711 * hibernation is allocating its own swap pages for the image,
712 * but conceivably even a call from memory reclaim - will free
713 * the swap from a page which has already been recorded in the
714 * image as a clean swapcache page, and then reuse its swap for
715 * another page of the image. On waking from hibernation, the
716 * original page might be freed under memory pressure, then
717 * later read back in from swap, now with the wrong data.
718 *
Mel Gormanf90ac392012-01-10 15:07:15 -0800719 * Hibration suspends storage while it is writing the image
720 * to disk so check that here.
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700721 */
Mel Gormanf90ac392012-01-10 15:07:15 -0800722 if (pm_suspended_storage())
Hugh Dickinsb73d7fc2010-09-09 16:38:09 -0700723 return 0;
724
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800725 delete_from_swap_cache(page);
726 SetPageDirty(page);
727 return 1;
Rik van Riel68a223942008-10-18 20:26:23 -0700728}
729
730/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * Free the swap entry like above, but also try to
732 * free the page cache entry if it is the last user.
733 */
Hugh Dickins2509ef22009-01-06 14:40:10 -0800734int free_swap_and_cache(swp_entry_t entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Hugh Dickins2509ef22009-01-06 14:40:10 -0800736 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct page *page = NULL;
738
Andi Kleena7420aa2009-09-16 11:50:05 +0200739 if (non_swap_entry(entry))
Hugh Dickins2509ef22009-01-06 14:40:10 -0800740 return 1;
Christoph Lameter06972122006-06-23 02:03:35 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 p = swap_info_get(entry);
743 if (p) {
Hugh Dickins253d5532009-12-14 17:58:44 -0800744 if (swap_entry_free(p, entry, 1) == SWAP_HAS_CACHE) {
Nick Piggin93fac702006-03-31 02:29:56 -0800745 page = find_get_page(&swapper_space, entry.val);
Nick Piggin8413ac92008-10-18 20:26:59 -0700746 if (page && !trylock_page(page)) {
Nick Piggin93fac702006-03-31 02:29:56 -0800747 page_cache_release(page);
748 page = NULL;
749 }
750 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800751 spin_unlock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 if (page) {
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800754 /*
755 * Not mapped elsewhere, or swap space full? Free it!
756 * Also recheck PageSwapCache now page is locked (above).
757 */
Nick Piggin93fac702006-03-31 02:29:56 -0800758 if (PageSwapCache(page) && !PageWriteback(page) &&
Hugh Dickinsa2c43ee2009-01-06 14:39:36 -0800759 (!page_mapped(page) || vm_swap_full())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 delete_from_swap_cache(page);
761 SetPageDirty(page);
762 }
763 unlock_page(page);
764 page_cache_release(page);
765 }
Hugh Dickins2509ef22009-01-06 14:40:10 -0800766 return p != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Daisuke Nishimura02491442010-03-10 15:22:17 -0800769#ifdef CONFIG_CGROUP_MEM_RES_CTLR
770/**
771 * mem_cgroup_count_swap_user - count the user of a swap entry
772 * @ent: the swap entry to be checked
773 * @pagep: the pointer for the swap cache page of the entry to be stored
774 *
775 * Returns the number of the user of the swap entry. The number is valid only
776 * for swaps of anonymous pages.
777 * If the entry is found on swap cache, the page is stored to pagep with
778 * refcount of it being incremented.
779 */
780int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep)
781{
782 struct page *page;
783 struct swap_info_struct *p;
784 int count = 0;
785
786 page = find_get_page(&swapper_space, ent.val);
787 if (page)
788 count += page_mapcount(page);
789 p = swap_info_get(ent);
790 if (p) {
791 count += swap_count(p->swap_map[swp_offset(ent)]);
792 spin_unlock(&swap_lock);
793 }
794
795 *pagep = page;
796 return count;
797}
798#endif
799
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200800#ifdef CONFIG_HIBERNATION
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800801/*
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800802 * Find the swap type that corresponds to given device (if any).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800803 *
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800804 * @offset - number of the PAGE_SIZE-sized block of the device, starting
805 * from 0, in which the swap header is expected to be located.
806 *
807 * This is needed for the suspend to disk (aka swsusp).
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800808 */
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800809int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800810{
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800811 struct block_device *bdev = NULL;
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800812 int type;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800813
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800814 if (device)
815 bdev = bdget(device);
816
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800817 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800818 for (type = 0; type < nr_swapfiles; type++) {
819 struct swap_info_struct *sis = swap_info[type];
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800820
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800821 if (!(sis->flags & SWP_WRITEOK))
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800822 continue;
Rafael J. Wysockib6b5bce2006-08-27 01:23:25 -0700823
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800824 if (!bdev) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800825 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200826 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800827
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800828 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800829 return type;
Rafael J. Wysocki6e1819d2006-03-23 03:00:03 -0800830 }
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800831 if (bdev == sis->bdev) {
Hugh Dickins9625a5f2009-12-14 17:58:42 -0800832 struct swap_extent *se = &sis->first_swap_extent;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800833
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800834 if (se->start_block == offset) {
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800835 if (bdev_p)
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200836 *bdev_p = bdgrab(sis->bdev);
Rafael J. Wysocki7bf23682007-01-05 16:36:28 -0800837
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800838 spin_unlock(&swap_lock);
839 bdput(bdev);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800840 return type;
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800841 }
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800842 }
843 }
844 spin_unlock(&swap_lock);
Rafael J. Wysocki915bae92006-12-06 20:34:07 -0800845 if (bdev)
846 bdput(bdev);
847
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800848 return -ENODEV;
849}
850
851/*
Hugh Dickins73c34b62009-12-14 17:58:43 -0800852 * Get the (PAGE_SIZE) block corresponding to given offset on the swapdev
853 * corresponding to given index in swap_info (swap type).
854 */
855sector_t swapdev_block(int type, pgoff_t offset)
856{
857 struct block_device *bdev;
858
859 if ((unsigned int)type >= nr_swapfiles)
860 return 0;
861 if (!(swap_info[type]->flags & SWP_WRITEOK))
862 return 0;
Lee Schermerhornd4906e12009-12-14 17:58:49 -0800863 return map_swap_entry(swp_entry(type, offset), &bdev);
Hugh Dickins73c34b62009-12-14 17:58:43 -0800864}
865
866/*
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800867 * Return either the total number of swap pages of given type, or the number
868 * of free pages of that type (depending on @free)
869 *
870 * This is needed for software suspend
871 */
872unsigned int count_swap_pages(int type, int free)
873{
874 unsigned int n = 0;
875
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800876 spin_lock(&swap_lock);
877 if ((unsigned int)type < nr_swapfiles) {
878 struct swap_info_struct *sis = swap_info[type];
879
Shaohua Li1abbca72013-02-22 16:34:38 -0800880 spin_lock(&sis->lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800881 if (sis->flags & SWP_WRITEOK) {
882 n = sis->pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800883 if (free)
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800884 n -= sis->inuse_pages;
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800885 }
Shaohua Li1abbca72013-02-22 16:34:38 -0800886 spin_unlock(&sis->lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800887 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -0800888 spin_unlock(&swap_lock);
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800889 return n;
890}
Hugh Dickins73c34b62009-12-14 17:58:43 -0800891#endif /* CONFIG_HIBERNATION */
Rafael J. Wysockif577eb32006-03-23 02:59:59 -0800892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
Hugh Dickins72866f62005-10-29 18:15:55 -0700894 * No need to decide whether this PTE shares the swap entry with others,
895 * just let do_wp_page work it out if a write is requested later - to
896 * force COW, vm_page_prot omits write permission from any private vma.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Hugh Dickins044d66c2008-02-07 00:14:04 -0800898static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned long addr, swp_entry_t entry, struct page *page)
900{
Johannes Weiner72835c82012-01-12 17:18:32 -0800901 struct mem_cgroup *memcg;
Hugh Dickins044d66c2008-02-07 00:14:04 -0800902 spinlock_t *ptl;
903 pte_t *pte;
904 int ret = 1;
905
Johannes Weiner72835c82012-01-12 17:18:32 -0800906 if (mem_cgroup_try_charge_swapin(vma->vm_mm, page,
907 GFP_KERNEL, &memcg)) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800908 ret = -ENOMEM;
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800909 goto out_nolock;
910 }
Hugh Dickins044d66c2008-02-07 00:14:04 -0800911
912 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
913 if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
914 if (ret > 0)
Johannes Weiner72835c82012-01-12 17:18:32 -0800915 mem_cgroup_cancel_charge_swapin(memcg);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800916 ret = 0;
917 goto out;
918 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800919
KAMEZAWA Hiroyukib084d432010-03-05 13:41:42 -0800920 dec_mm_counter(vma->vm_mm, MM_SWAPENTS);
KAMEZAWA Hiroyukid559db02010-03-05 13:41:39 -0800921 inc_mm_counter(vma->vm_mm, MM_ANONPAGES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 get_page(page);
923 set_pte_at(vma->vm_mm, addr, pte,
924 pte_mkold(mk_pte(page, vma->vm_page_prot)));
925 page_add_anon_rmap(page, vma, addr);
Johannes Weiner72835c82012-01-12 17:18:32 -0800926 mem_cgroup_commit_charge_swapin(page, memcg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 swap_free(entry);
928 /*
929 * Move the page to the active list so it is not
930 * immediately swapped out again after swapon.
931 */
932 activate_page(page);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800933out:
934 pte_unmap_unlock(pte, ptl);
KAMEZAWA Hiroyuki85d9fc82009-01-29 14:25:13 -0800935out_nolock:
Hugh Dickins044d66c2008-02-07 00:14:04 -0800936 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
940 unsigned long addr, unsigned long end,
941 swp_entry_t entry, struct page *page)
942{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 pte_t swp_pte = swp_entry_to_pte(entry);
Hugh Dickins705e87c2005-10-29 18:16:27 -0700944 pte_t *pte;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800945 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Hugh Dickins044d66c2008-02-07 00:14:04 -0800947 /*
948 * We don't actually need pte lock while scanning for swp_pte: since
949 * we hold page lock and mmap_sem, swp_pte cannot be inserted into the
950 * page table while we're scanning; though it could get zapped, and on
951 * some architectures (e.g. x86_32 with PAE) we might catch a glimpse
952 * of unmatched parts which look like swp_pte, so unuse_pte must
953 * recheck under pte lock. Scanning without pte lock lets it be
954 * preemptible whenever CONFIG_PREEMPT but not CONFIG_HIGHPTE.
955 */
956 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 do {
958 /*
959 * swapoff spends a _lot_ of time in this loop!
960 * Test inline before going to call unuse_pte.
961 */
962 if (unlikely(pte_same(*pte, swp_pte))) {
Hugh Dickins044d66c2008-02-07 00:14:04 -0800963 pte_unmap(pte);
964 ret = unuse_pte(vma, pmd, addr, entry, page);
965 if (ret)
966 goto out;
967 pte = pte_offset_map(pmd, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
969 } while (pte++, addr += PAGE_SIZE, addr != end);
Hugh Dickins044d66c2008-02-07 00:14:04 -0800970 pte_unmap(pte - 1);
971out:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800972 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
975static inline int unuse_pmd_range(struct vm_area_struct *vma, pud_t *pud,
976 unsigned long addr, unsigned long end,
977 swp_entry_t entry, struct page *page)
978{
979 pmd_t *pmd;
980 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800981 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 pmd = pmd_offset(pud, addr);
984 do {
985 next = pmd_addr_end(addr, end);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -0700986 if (pmd_none_or_trans_huge_or_clear_bad(pmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800988 ret = unuse_pte_range(vma, pmd, addr, next, entry, page);
989 if (ret)
990 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } while (pmd++, addr = next, addr != end);
992 return 0;
993}
994
995static inline int unuse_pud_range(struct vm_area_struct *vma, pgd_t *pgd,
996 unsigned long addr, unsigned long end,
997 swp_entry_t entry, struct page *page)
998{
999 pud_t *pud;
1000 unsigned long next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001001 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 pud = pud_offset(pgd, addr);
1004 do {
1005 next = pud_addr_end(addr, end);
1006 if (pud_none_or_clear_bad(pud))
1007 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001008 ret = unuse_pmd_range(vma, pud, addr, next, entry, page);
1009 if (ret)
1010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 } while (pud++, addr = next, addr != end);
1012 return 0;
1013}
1014
1015static int unuse_vma(struct vm_area_struct *vma,
1016 swp_entry_t entry, struct page *page)
1017{
1018 pgd_t *pgd;
1019 unsigned long addr, end, next;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001020 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Hugh Dickins3ca7b3c2009-12-14 17:58:57 -08001022 if (page_anon_vma(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 addr = page_address_in_vma(page, vma);
1024 if (addr == -EFAULT)
1025 return 0;
1026 else
1027 end = addr + PAGE_SIZE;
1028 } else {
1029 addr = vma->vm_start;
1030 end = vma->vm_end;
1031 }
1032
1033 pgd = pgd_offset(vma->vm_mm, addr);
1034 do {
1035 next = pgd_addr_end(addr, end);
1036 if (pgd_none_or_clear_bad(pgd))
1037 continue;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001038 ret = unuse_pud_range(vma, pgd, addr, next, entry, page);
1039 if (ret)
1040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 } while (pgd++, addr = next, addr != end);
1042 return 0;
1043}
1044
1045static int unuse_mm(struct mm_struct *mm,
1046 swp_entry_t entry, struct page *page)
1047{
1048 struct vm_area_struct *vma;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001049 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 if (!down_read_trylock(&mm->mmap_sem)) {
1052 /*
Fernando Luis Vazquez Cao7d034312008-07-29 22:33:41 -07001053 * Activate page so shrink_inactive_list is unlikely to unmap
1054 * its ptes while lock is dropped, so swapoff can make progress.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Hugh Dickinsc475a8a2005-06-21 17:15:12 -07001056 activate_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 unlock_page(page);
1058 down_read(&mm->mmap_sem);
1059 lock_page(page);
1060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001062 if (vma->anon_vma && (ret = unuse_vma(vma, entry, page)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 up_read(&mm->mmap_sem);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001066 return (ret < 0)? ret: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
1070 * Scan swap_map from current position to next entry still in use.
1071 * Recycle to start on reaching the end, returning 0 when empty.
1072 */
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001073static unsigned int find_next_to_unuse(struct swap_info_struct *si,
1074 unsigned int prev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001076 unsigned int max = si->max;
1077 unsigned int i = prev;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001078 unsigned char count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001081 * No need for swap_lock here: we're just looking
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 * for whether an entry is in use, not modifying it; false
1083 * hits are okay, and sys_swapoff() has already prevented new
Hugh Dickins5d337b92005-09-03 15:54:41 -07001084 * allocations from this area (while holding swap_lock).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086 for (;;) {
1087 if (++i >= max) {
1088 if (!prev) {
1089 i = 0;
1090 break;
1091 }
1092 /*
1093 * No entries in use at top of swap_map,
1094 * loop back to start and recheck there.
1095 */
1096 max = prev + 1;
1097 prev = 0;
1098 i = 1;
1099 }
1100 count = si->swap_map[i];
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001101 if (count && swap_count(count) != SWAP_MAP_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 break;
1103 }
1104 return i;
1105}
1106
1107/*
1108 * We completely avoid races by reading each swap page in advance,
1109 * and then search for the process using it. All the necessary
1110 * page table adjustments can then be made atomically.
1111 */
1112static int try_to_unuse(unsigned int type)
1113{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001114 struct swap_info_struct *si = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 struct mm_struct *start_mm;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001116 unsigned char *swap_map;
1117 unsigned char swcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct page *page;
1119 swp_entry_t entry;
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001120 unsigned int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * When searching mms for an entry, a good strategy is to
1125 * start at the first mm we freed the previous entry from
1126 * (though actually we don't notice whether we or coincidence
1127 * freed the entry). Initialize this start_mm with a hold.
1128 *
1129 * A simpler strategy would be to start at the last mm we
1130 * freed the previous entry from; but that would take less
1131 * advantage of mmlist ordering, which clusters forked mms
1132 * together, child after parent. If we race with dup_mmap(), we
1133 * prefer to resolve parent before child, lest we miss entries
1134 * duplicated after we scanned child: using last mm would invert
Hugh Dickins570a3352009-12-14 17:58:46 -08001135 * that.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
1137 start_mm = &init_mm;
1138 atomic_inc(&init_mm.mm_users);
1139
1140 /*
1141 * Keep on scanning until all entries have gone. Usually,
1142 * one pass through swap_map is enough, but not necessarily:
1143 * there are races when an instance of an entry might be missed.
1144 */
1145 while ((i = find_next_to_unuse(si, i)) != 0) {
1146 if (signal_pending(current)) {
1147 retval = -EINTR;
1148 break;
1149 }
1150
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001151 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 * Get a page for the entry, using the existing swap
1153 * cache page if there is one. Otherwise, get a clean
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001154 * page and read the swap into it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156 swap_map = &si->swap_map[i];
1157 entry = swp_entry(type, i);
Hugh Dickins02098fe2008-02-04 22:28:42 -08001158 page = read_swap_cache_async(entry,
1159 GFP_HIGHUSER_MOVABLE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!page) {
1161 /*
1162 * Either swap_duplicate() failed because entry
1163 * has been freed independently, and will not be
1164 * reused since sys_swapoff() already disabled
1165 * allocation from here, or alloc_page() failed.
1166 */
1167 if (!*swap_map)
1168 continue;
1169 retval = -ENOMEM;
1170 break;
1171 }
1172
1173 /*
1174 * Don't hold on to start_mm if it looks like exiting.
1175 */
1176 if (atomic_read(&start_mm->mm_users) == 1) {
1177 mmput(start_mm);
1178 start_mm = &init_mm;
1179 atomic_inc(&init_mm.mm_users);
1180 }
1181
1182 /*
1183 * Wait for and lock page. When do_swap_page races with
1184 * try_to_unuse, do_swap_page can handle the fault much
1185 * faster than try_to_unuse can locate the entry. This
1186 * apparently redundant "wait_on_page_locked" lets try_to_unuse
1187 * defer to do_swap_page in such a case - in some tests,
1188 * do_swap_page and try_to_unuse repeatedly compete.
1189 */
1190 wait_on_page_locked(page);
1191 wait_on_page_writeback(page);
1192 lock_page(page);
1193 wait_on_page_writeback(page);
1194
1195 /*
1196 * Remove all references to entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 swcount = *swap_map;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001199 if (swap_count(swcount) == SWAP_MAP_SHMEM) {
1200 retval = shmem_unuse(entry, page);
1201 /* page has already been unlocked and released */
1202 if (retval < 0)
1203 break;
1204 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001206 if (swap_count(swcount) && start_mm != &init_mm)
1207 retval = unuse_mm(start_mm, entry, page);
1208
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001209 if (swap_count(*swap_map)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int set_start_mm = (*swap_map >= swcount);
1211 struct list_head *p = &start_mm->mmlist;
1212 struct mm_struct *new_start_mm = start_mm;
1213 struct mm_struct *prev_mm = start_mm;
1214 struct mm_struct *mm;
1215
1216 atomic_inc(&new_start_mm->mm_users);
1217 atomic_inc(&prev_mm->mm_users);
1218 spin_lock(&mmlist_lock);
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001219 while (swap_count(*swap_map) && !retval &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 (p = p->next) != &start_mm->mmlist) {
1221 mm = list_entry(p, struct mm_struct, mmlist);
Hugh Dickins70af7c52006-06-23 02:03:44 -07001222 if (!atomic_inc_not_zero(&mm->mm_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 spin_unlock(&mmlist_lock);
1225 mmput(prev_mm);
1226 prev_mm = mm;
1227
1228 cond_resched();
1229
1230 swcount = *swap_map;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001231 if (!swap_count(swcount)) /* any usage ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001233 else if (mm == &init_mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 set_start_mm = 1;
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001235 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 retval = unuse_mm(mm, entry, page);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001237
Bo Liu32c5fc12009-11-02 16:50:33 +00001238 if (set_start_mm && *swap_map < swcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 mmput(new_start_mm);
1240 atomic_inc(&mm->mm_users);
1241 new_start_mm = mm;
1242 set_start_mm = 0;
1243 }
1244 spin_lock(&mmlist_lock);
1245 }
1246 spin_unlock(&mmlist_lock);
1247 mmput(prev_mm);
1248 mmput(start_mm);
1249 start_mm = new_start_mm;
1250 }
1251 if (retval) {
1252 unlock_page(page);
1253 page_cache_release(page);
1254 break;
1255 }
1256
1257 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * If a reference remains (rare), we would like to leave
1259 * the page in the swap cache; but try_to_unmap could
1260 * then re-duplicate the entry once we drop page lock,
1261 * so we might loop indefinitely; also, that page could
1262 * not be swapped out to other storage meanwhile. So:
1263 * delete from cache even if there's another reference,
1264 * after ensuring that the data has been saved to disk -
1265 * since if the reference remains (rarer), it will be
1266 * read from disk into another page. Splitting into two
1267 * pages would be incorrect if swap supported "shared
1268 * private" pages, but they are handled by tmpfs files.
Hugh Dickins5ad64682009-12-14 17:59:24 -08001269 *
1270 * Given how unuse_vma() targets one particular offset
1271 * in an anon_vma, once the anon_vma has been determined,
1272 * this splitting happens to be just what is needed to
1273 * handle where KSM pages have been swapped out: re-reading
1274 * is unnecessarily slow, but we can fix that later on.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07001276 if (swap_count(*swap_map) &&
1277 PageDirty(page) && PageSwapCache(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct writeback_control wbc = {
1279 .sync_mode = WB_SYNC_NONE,
1280 };
1281
1282 swap_writepage(page, &wbc);
1283 lock_page(page);
1284 wait_on_page_writeback(page);
1285 }
Hugh Dickins68bdc8d2009-01-06 14:39:37 -08001286
1287 /*
1288 * It is conceivable that a racing task removed this page from
1289 * swap cache just before we acquired the page lock at the top,
1290 * or while we dropped it in unuse_mm(). The page might even
1291 * be back in swap cache on another swap area: that we must not
1292 * delete, since it may not have been written out to swap yet.
1293 */
1294 if (PageSwapCache(page) &&
1295 likely(page_private(page) == entry.val))
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001296 delete_from_swap_cache(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /*
1299 * So we could skip searching mms once swap count went
1300 * to 1, we did not mark any present ptes as dirty: must
Anderson Briglia2706a1b2007-07-15 23:38:09 -07001301 * mark page dirty so shrink_page_list will preserve it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 */
1303 SetPageDirty(page);
1304 unlock_page(page);
1305 page_cache_release(page);
1306
1307 /*
1308 * Make sure that we aren't completely killing
1309 * interactive performance.
1310 */
1311 cond_resched();
1312 }
1313
1314 mmput(start_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return retval;
1316}
1317
1318/*
Hugh Dickins5d337b92005-09-03 15:54:41 -07001319 * After a successful try_to_unuse, if no swap is now in use, we know
1320 * we can empty the mmlist. swap_lock must be held on entry and exit.
1321 * Note that mmlist_lock nests inside swap_lock, and an mm must be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 * added to the mmlist just after page_duplicate - before would be racy.
1323 */
1324static void drain_mmlist(void)
1325{
1326 struct list_head *p, *next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001327 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001329 for (type = 0; type < nr_swapfiles; type++)
1330 if (swap_info[type]->inuse_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return;
1332 spin_lock(&mmlist_lock);
1333 list_for_each_safe(p, next, &init_mm.mmlist)
1334 list_del_init(p);
1335 spin_unlock(&mmlist_lock);
1336}
1337
1338/*
1339 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
Lee Schermerhornd4906e12009-12-14 17:58:49 -08001340 * corresponds to page offset for the specified swap entry.
1341 * Note that the type of this function is sector_t, but it returns page offset
1342 * into the bdev, not sector offset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Lee Schermerhornd4906e12009-12-14 17:58:49 -08001344static sector_t map_swap_entry(swp_entry_t entry, struct block_device **bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001346 struct swap_info_struct *sis;
1347 struct swap_extent *start_se;
1348 struct swap_extent *se;
1349 pgoff_t offset;
1350
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001351 sis = swap_info[swp_type(entry)];
Hugh Dickinsf29ad6a2009-12-14 17:58:40 -08001352 *bdev = sis->bdev;
1353
1354 offset = swp_offset(entry);
1355 start_se = sis->curr_swap_extent;
1356 se = start_se;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 for ( ; ; ) {
1359 struct list_head *lh;
1360
1361 if (se->start_page <= offset &&
1362 offset < (se->start_page + se->nr_pages)) {
1363 return se->start_block + (offset - se->start_page);
1364 }
Hugh Dickins11d31882005-09-03 15:54:34 -07001365 lh = se->list.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 se = list_entry(lh, struct swap_extent, list);
1367 sis->curr_swap_extent = se;
1368 BUG_ON(se == start_se); /* It *must* be present */
1369 }
1370}
1371
1372/*
Lee Schermerhornd4906e12009-12-14 17:58:49 -08001373 * Returns the page offset into bdev for the specified page's swap entry.
1374 */
1375sector_t map_swap_page(struct page *page, struct block_device **bdev)
1376{
1377 swp_entry_t entry;
1378 entry.val = page_private(page);
1379 return map_swap_entry(entry, bdev);
1380}
1381
1382/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * Free all of a swapdev's extent information
1384 */
1385static void destroy_swap_extents(struct swap_info_struct *sis)
1386{
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001387 while (!list_empty(&sis->first_swap_extent.list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct swap_extent *se;
1389
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001390 se = list_entry(sis->first_swap_extent.list.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct swap_extent, list);
1392 list_del(&se->list);
1393 kfree(se);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*
1398 * Add a block range (and the corresponding page range) into this swapdev's
Hugh Dickins11d31882005-09-03 15:54:34 -07001399 * extent list. The extent list is kept sorted in page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
Hugh Dickins11d31882005-09-03 15:54:34 -07001401 * This function rather assumes that it is called in ascending page order.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
1403static int
1404add_swap_extent(struct swap_info_struct *sis, unsigned long start_page,
1405 unsigned long nr_pages, sector_t start_block)
1406{
1407 struct swap_extent *se;
1408 struct swap_extent *new_se;
1409 struct list_head *lh;
1410
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001411 if (start_page == 0) {
1412 se = &sis->first_swap_extent;
1413 sis->curr_swap_extent = se;
1414 se->start_page = 0;
1415 se->nr_pages = nr_pages;
1416 se->start_block = start_block;
1417 return 1;
1418 } else {
1419 lh = sis->first_swap_extent.list.prev; /* Highest extent */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 se = list_entry(lh, struct swap_extent, list);
Hugh Dickins11d31882005-09-03 15:54:34 -07001421 BUG_ON(se->start_page + se->nr_pages != start_page);
1422 if (se->start_block + se->nr_pages == start_block) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 /* Merge it */
1424 se->nr_pages += nr_pages;
1425 return 0;
1426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
1429 /*
1430 * No merge. Insert a new extent, preserving ordering.
1431 */
1432 new_se = kmalloc(sizeof(*se), GFP_KERNEL);
1433 if (new_se == NULL)
1434 return -ENOMEM;
1435 new_se->start_page = start_page;
1436 new_se->nr_pages = nr_pages;
1437 new_se->start_block = start_block;
1438
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001439 list_add_tail(&new_se->list, &sis->first_swap_extent.list);
Hugh Dickins53092a72005-09-03 15:54:34 -07001440 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443/*
1444 * A `swap extent' is a simple thing which maps a contiguous range of pages
1445 * onto a contiguous range of disk blocks. An ordered list of swap extents
1446 * is built at swapon time and is then used at swap_writepage/swap_readpage
1447 * time for locating where on disk a page belongs.
1448 *
1449 * If the swapfile is an S_ISBLK block device, a single extent is installed.
1450 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1451 * swap files identically.
1452 *
1453 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1454 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1455 * swapfiles are handled *identically* after swapon time.
1456 *
1457 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1458 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1459 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1460 * requirements, they are simply tossed out - we will never use those blocks
1461 * for swapping.
1462 *
Hugh Dickinsb0d9bcd2005-09-03 15:54:31 -07001463 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1465 * which will scribble on the fs.
1466 *
1467 * The amount of disk space which a single swap extent represents varies.
1468 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1469 * extents in the list. To avoid much list walking, we cache the previous
1470 * search location in `curr_swap_extent', and start new searches from there.
1471 * This is extremely effective. The average number of iterations in
1472 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1473 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001474static int setup_swap_extents(struct swap_info_struct *sis, sector_t *span)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct inode *inode;
1477 unsigned blocks_per_page;
1478 unsigned long page_no;
1479 unsigned blkbits;
1480 sector_t probe_block;
1481 sector_t last_block;
Hugh Dickins53092a72005-09-03 15:54:34 -07001482 sector_t lowest_block = -1;
1483 sector_t highest_block = 0;
1484 int nr_extents = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 int ret;
1486
1487 inode = sis->swap_file->f_mapping->host;
1488 if (S_ISBLK(inode->i_mode)) {
1489 ret = add_swap_extent(sis, 0, sis->max, 0);
Hugh Dickins53092a72005-09-03 15:54:34 -07001490 *span = sis->pages;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 }
1493
1494 blkbits = inode->i_blkbits;
1495 blocks_per_page = PAGE_SIZE >> blkbits;
1496
1497 /*
1498 * Map all the blocks into the extent list. This code doesn't try
1499 * to be very smart.
1500 */
1501 probe_block = 0;
1502 page_no = 0;
1503 last_block = i_size_read(inode) >> blkbits;
1504 while ((probe_block + blocks_per_page) <= last_block &&
1505 page_no < sis->max) {
1506 unsigned block_in_page;
1507 sector_t first_block;
1508
1509 first_block = bmap(inode, probe_block);
1510 if (first_block == 0)
1511 goto bad_bmap;
1512
1513 /*
1514 * It must be PAGE_SIZE aligned on-disk
1515 */
1516 if (first_block & (blocks_per_page - 1)) {
1517 probe_block++;
1518 goto reprobe;
1519 }
1520
1521 for (block_in_page = 1; block_in_page < blocks_per_page;
1522 block_in_page++) {
1523 sector_t block;
1524
1525 block = bmap(inode, probe_block + block_in_page);
1526 if (block == 0)
1527 goto bad_bmap;
1528 if (block != first_block + block_in_page) {
1529 /* Discontiguity */
1530 probe_block++;
1531 goto reprobe;
1532 }
1533 }
1534
Hugh Dickins53092a72005-09-03 15:54:34 -07001535 first_block >>= (PAGE_SHIFT - blkbits);
1536 if (page_no) { /* exclude the header page */
1537 if (first_block < lowest_block)
1538 lowest_block = first_block;
1539 if (first_block > highest_block)
1540 highest_block = first_block;
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 /*
1544 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1545 */
Hugh Dickins53092a72005-09-03 15:54:34 -07001546 ret = add_swap_extent(sis, page_no, 1, first_block);
1547 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 goto out;
Hugh Dickins53092a72005-09-03 15:54:34 -07001549 nr_extents += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 page_no++;
1551 probe_block += blocks_per_page;
1552reprobe:
1553 continue;
1554 }
Hugh Dickins53092a72005-09-03 15:54:34 -07001555 ret = nr_extents;
1556 *span = 1 + highest_block - lowest_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (page_no == 0)
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001558 page_no = 1; /* force Empty message */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 sis->max = page_no;
Hugh Dickinse2244ec2005-09-03 15:54:32 -07001560 sis->pages = page_no - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 sis->highest_bit = page_no - 1;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001562out:
1563 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564bad_bmap:
1565 printk(KERN_ERR "swapon: swapfile has holes\n");
1566 ret = -EINVAL;
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07001570static void enable_swap_info(struct swap_info_struct *p, int prio,
1571 unsigned char *swap_map)
1572{
1573 int i, prev;
1574
1575 spin_lock(&swap_lock);
1576 if (prio >= 0)
1577 p->prio = prio;
1578 else
1579 p->prio = --least_priority;
1580 p->swap_map = swap_map;
1581 p->flags |= SWP_WRITEOK;
Shaohua Li1abbca72013-02-22 16:34:38 -08001582 atomic_long_add(p->pages, &nr_swap_pages);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07001583 total_swap_pages += p->pages;
1584
1585 /* insert swap space into swap_list: */
1586 prev = -1;
1587 for (i = swap_list.head; i >= 0; i = swap_info[i]->next) {
1588 if (p->prio >= swap_info[i]->prio)
1589 break;
1590 prev = i;
1591 }
1592 p->next = i;
1593 if (prev < 0)
1594 swap_list.head = swap_list.next = p->type;
1595 else
1596 swap_info[prev]->next = p->type;
1597 spin_unlock(&swap_lock);
1598}
1599
Heiko Carstensc4ea37c2009-01-14 14:14:28 +01001600SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001602 struct swap_info_struct *p = NULL;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08001603 unsigned char *swap_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct file *swap_file, *victim;
1605 struct address_space *mapping;
1606 struct inode *inode;
Hugh Dickins73c34b62009-12-14 17:58:43 -08001607 char *pathname;
David Rientjes72788c32011-05-24 17:11:40 -07001608 int oom_score_adj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 int i, type, prev;
1610 int err;
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (!capable(CAP_SYS_ADMIN))
1613 return -EPERM;
1614
Al Viro191c5422012-02-13 03:58:52 +00001615 BUG_ON(!current->mm);
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 pathname = getname(specialfile);
1618 err = PTR_ERR(pathname);
1619 if (IS_ERR(pathname))
1620 goto out;
1621
1622 victim = filp_open(pathname, O_RDWR|O_LARGEFILE, 0);
1623 putname(pathname);
1624 err = PTR_ERR(victim);
1625 if (IS_ERR(victim))
1626 goto out;
1627
1628 mapping = victim->f_mapping;
1629 prev = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001630 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001631 for (type = swap_list.head; type >= 0; type = swap_info[type]->next) {
1632 p = swap_info[type];
Hugh Dickins22c6f8f2009-01-06 14:39:48 -08001633 if (p->flags & SWP_WRITEOK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (p->swap_file->f_mapping == mapping)
1635 break;
1636 }
1637 prev = type;
1638 }
1639 if (type < 0) {
1640 err = -EINVAL;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001641 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 goto out_dput;
1643 }
Al Viro191c5422012-02-13 03:58:52 +00001644 if (!security_vm_enough_memory_mm(current->mm, p->pages))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 vm_unacct_memory(p->pages);
1646 else {
1647 err = -ENOMEM;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001648 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto out_dput;
1650 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001651 if (prev < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 swap_list.head = p->next;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001653 else
1654 swap_info[prev]->next = p->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (type == swap_list.next) {
1656 /* just pick something that's safe... */
1657 swap_list.next = swap_list.head;
1658 }
Shaohua Li1abbca72013-02-22 16:34:38 -08001659 spin_lock(&p->lock);
Hugh Dickins78ecba02008-07-23 21:28:23 -07001660 if (p->prio < 0) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001661 for (i = p->next; i >= 0; i = swap_info[i]->next)
1662 swap_info[i]->prio = p->prio--;
Hugh Dickins78ecba02008-07-23 21:28:23 -07001663 least_priority++;
1664 }
Shaohua Li1abbca72013-02-22 16:34:38 -08001665 atomic_long_sub(p->pages, &nr_swap_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 total_swap_pages -= p->pages;
1667 p->flags &= ~SWP_WRITEOK;
Shaohua Li1abbca72013-02-22 16:34:38 -08001668 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001669 spin_unlock(&swap_lock);
Hugh Dickinsfb4f88d2005-09-03 15:54:37 -07001670
David Rientjes72788c32011-05-24 17:11:40 -07001671 oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 err = try_to_unuse(type);
David Rientjes43362a42011-10-31 17:07:18 -07001673 compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (err) {
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07001676 /*
1677 * reading p->prio and p->swap_map outside the lock is
1678 * safe here because only sys_swapon and sys_swapoff
1679 * change them, and there can be no other sys_swapon or
1680 * sys_swapoff for this swap_info_struct at this point.
1681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /* re-insert swap space back into swap_list */
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07001683 enable_swap_info(p, p->prio, p->swap_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 goto out_dput;
1685 }
Hugh Dickins52b7efdb2005-09-03 15:54:39 -07001686
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07001687 destroy_swap_extents(p);
Hugh Dickins570a3352009-12-14 17:58:46 -08001688 if (p->flags & SWP_CONTINUED)
1689 free_swap_count_continuations(p);
1690
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001691 mutex_lock(&swapon_mutex);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001692 spin_lock(&swap_lock);
Shaohua Li1abbca72013-02-22 16:34:38 -08001693 spin_lock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 drain_mmlist();
Hugh Dickins5d337b92005-09-03 15:54:41 -07001695
1696 /* wait for anyone still in scan_swap_map */
1697 p->highest_bit = 0; /* cuts scans short */
1698 while (p->flags >= SWP_SCANNING) {
Shaohua Li1abbca72013-02-22 16:34:38 -08001699 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001700 spin_unlock(&swap_lock);
Nishanth Aravamudan13e4b572005-09-10 00:27:25 -07001701 schedule_timeout_uninterruptible(1);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001702 spin_lock(&swap_lock);
Shaohua Li1abbca72013-02-22 16:34:38 -08001703 spin_lock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001704 }
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 swap_file = p->swap_file;
1707 p->swap_file = NULL;
1708 p->max = 0;
1709 swap_map = p->swap_map;
1710 p->swap_map = NULL;
1711 p->flags = 0;
Shaohua Li1abbca72013-02-22 16:34:38 -08001712 spin_unlock(&p->lock);
Hugh Dickins5d337b92005-09-03 15:54:41 -07001713 spin_unlock(&swap_lock);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001714 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 vfree(swap_map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08001716 /* Destroy swap account informatin */
1717 swap_cgroup_swapoff(type);
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 inode = mapping->host;
1720 if (S_ISBLK(inode->i_mode)) {
1721 struct block_device *bdev = I_BDEV(inode);
1722 set_blocksize(bdev, p->old_block_size);
Tejun Heoe525fd82010-11-13 11:55:17 +01001723 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001725 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 inode->i_flags &= ~S_SWAPFILE;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001727 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 filp_close(swap_file, NULL);
1730 err = 0;
Kay Sievers66d7dd52010-10-26 14:22:06 -07001731 atomic_inc(&proc_poll_event);
1732 wake_up_interruptible(&proc_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734out_dput:
1735 filp_close(victim, NULL);
1736out:
1737 return err;
1738}
1739
1740#ifdef CONFIG_PROC_FS
Kay Sievers66d7dd52010-10-26 14:22:06 -07001741static unsigned swaps_poll(struct file *file, poll_table *wait)
1742{
Kay Sieversf1514632011-07-12 20:48:39 +02001743 struct seq_file *seq = file->private_data;
Kay Sievers66d7dd52010-10-26 14:22:06 -07001744
1745 poll_wait(file, &proc_poll_wait, wait);
1746
Kay Sieversf1514632011-07-12 20:48:39 +02001747 if (seq->poll_event != atomic_read(&proc_poll_event)) {
1748 seq->poll_event = atomic_read(&proc_poll_event);
Kay Sievers66d7dd52010-10-26 14:22:06 -07001749 return POLLIN | POLLRDNORM | POLLERR | POLLPRI;
1750 }
1751
1752 return POLLIN | POLLRDNORM;
1753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/* iterator */
1756static void *swap_start(struct seq_file *swap, loff_t *pos)
1757{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001758 struct swap_info_struct *si;
1759 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 loff_t l = *pos;
1761
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001762 mutex_lock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001764 if (!l)
1765 return SEQ_START_TOKEN;
1766
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001767 for (type = 0; type < nr_swapfiles; type++) {
1768 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1769 si = swap_info[type];
1770 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 continue;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001772 if (!--l)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001773 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
1776 return NULL;
1777}
1778
1779static void *swap_next(struct seq_file *swap, void *v, loff_t *pos)
1780{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001781 struct swap_info_struct *si = v;
1782 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001784 if (v == SEQ_START_TOKEN)
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001785 type = 0;
1786 else
1787 type = si->type + 1;
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001788
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001789 for (; type < nr_swapfiles; type++) {
1790 smp_rmb(); /* read nr_swapfiles before swap_info[type] */
1791 si = swap_info[type];
1792 if (!(si->flags & SWP_USED) || !si->swap_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 continue;
1794 ++*pos;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001795 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
1798 return NULL;
1799}
1800
1801static void swap_stop(struct seq_file *swap, void *v)
1802{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001803 mutex_unlock(&swapon_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806static int swap_show(struct seq_file *swap, void *v)
1807{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001808 struct swap_info_struct *si = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct file *file;
1810 int len;
1811
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001812 if (si == SEQ_START_TOKEN) {
Suleiman Souhlal881e4aa2006-12-06 20:32:28 -08001813 seq_puts(swap,"Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1814 return 0;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001817 file = si->swap_file;
Jan Blunckc32c2f62008-02-14 19:38:43 -08001818 len = seq_path(swap, &file->f_path, " \t\n\\");
Hugh Dickins6eb396d2005-09-03 15:54:35 -07001819 seq_printf(swap, "%*s%s\t%u\t%u\t%d\n",
Hugh Dickins886bb7e2009-01-06 14:39:48 -08001820 len < 40 ? 40 - len : 1, " ",
1821 S_ISBLK(file->f_path.dentry->d_inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 "partition" : "file\t",
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001823 si->pages << (PAGE_SHIFT - 10),
1824 si->inuse_pages << (PAGE_SHIFT - 10),
1825 si->prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
1827}
1828
Helge Deller15ad7cd2006-12-06 20:40:36 -08001829static const struct seq_operations swaps_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 .start = swap_start,
1831 .next = swap_next,
1832 .stop = swap_stop,
1833 .show = swap_show
1834};
1835
1836static int swaps_open(struct inode *inode, struct file *file)
1837{
Kay Sieversf1514632011-07-12 20:48:39 +02001838 struct seq_file *seq;
Kay Sievers66d7dd52010-10-26 14:22:06 -07001839 int ret;
1840
Kay Sievers66d7dd52010-10-26 14:22:06 -07001841 ret = seq_open(file, &swaps_op);
Kay Sieversf1514632011-07-12 20:48:39 +02001842 if (ret)
Kay Sievers66d7dd52010-10-26 14:22:06 -07001843 return ret;
Kay Sievers66d7dd52010-10-26 14:22:06 -07001844
Kay Sieversf1514632011-07-12 20:48:39 +02001845 seq = file->private_data;
1846 seq->poll_event = atomic_read(&proc_poll_event);
1847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
Helge Deller15ad7cd2006-12-06 20:40:36 -08001850static const struct file_operations proc_swaps_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 .open = swaps_open,
1852 .read = seq_read,
1853 .llseek = seq_lseek,
1854 .release = seq_release,
Kay Sievers66d7dd52010-10-26 14:22:06 -07001855 .poll = swaps_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856};
1857
1858static int __init procswaps_init(void)
1859{
Denis V. Lunev3d71f862008-04-29 01:02:13 -07001860 proc_create("swaps", 0, NULL, &proc_swaps_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
1863__initcall(procswaps_init);
1864#endif /* CONFIG_PROC_FS */
1865
Jan Beulich17963162008-12-16 11:35:24 +00001866#ifdef MAX_SWAPFILES_CHECK
1867static int __init max_swapfiles_check(void)
1868{
1869 MAX_SWAPFILES_CHECK();
1870 return 0;
1871}
1872late_initcall(max_swapfiles_check);
1873#endif
1874
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07001875static struct swap_info_struct *alloc_swap_info(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Hugh Dickins73c34b62009-12-14 17:58:43 -08001877 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 unsigned int type;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001879
1880 p = kzalloc(sizeof(*p), GFP_KERNEL);
1881 if (!p)
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07001882 return ERR_PTR(-ENOMEM);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001883
Hugh Dickins5d337b92005-09-03 15:54:41 -07001884 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001885 for (type = 0; type < nr_swapfiles; type++) {
1886 if (!(swap_info[type]->flags & SWP_USED))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001888 }
Christoph Lameter06972122006-06-23 02:03:35 -07001889 if (type >= MAX_SWAPFILES) {
Hugh Dickins5d337b92005-09-03 15:54:41 -07001890 spin_unlock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001891 kfree(p);
Cesar Eduardo Barros730c0582011-03-22 16:33:19 -07001892 return ERR_PTR(-EPERM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001894 if (type >= nr_swapfiles) {
1895 p->type = type;
1896 swap_info[type] = p;
1897 /*
1898 * Write swap_info[type] before nr_swapfiles, in case a
1899 * racing procfs swap_start() or swap_next() is reading them.
1900 * (We never shrink nr_swapfiles, we never free this entry.)
1901 */
1902 smp_wmb();
1903 nr_swapfiles++;
1904 } else {
1905 kfree(p);
1906 p = swap_info[type];
1907 /*
1908 * Do not memset this entry: a racing procfs swap_next()
1909 * would be relying on p->type to remain valid.
1910 */
1911 }
Hugh Dickins9625a5f2009-12-14 17:58:42 -08001912 INIT_LIST_HEAD(&p->first_swap_extent.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 p->flags = SWP_USED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 p->next = -1;
Hugh Dickins5d337b92005-09-03 15:54:41 -07001915 spin_unlock(&swap_lock);
Shaohua Li1abbca72013-02-22 16:34:38 -08001916 spin_lock_init(&p->lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08001917
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07001918 return p;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07001919}
1920
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07001921static int claim_swapfile(struct swap_info_struct *p, struct inode *inode)
1922{
1923 int error;
1924
1925 if (S_ISBLK(inode->i_mode)) {
1926 p->bdev = bdgrab(I_BDEV(inode));
1927 error = blkdev_get(p->bdev,
1928 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1929 sys_swapon);
1930 if (error < 0) {
1931 p->bdev = NULL;
Cesar Eduardo Barros87ade722011-03-22 16:33:27 -07001932 return -EINVAL;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07001933 }
1934 p->old_block_size = block_size(p->bdev);
1935 error = set_blocksize(p->bdev, PAGE_SIZE);
1936 if (error < 0)
Cesar Eduardo Barros87ade722011-03-22 16:33:27 -07001937 return error;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07001938 p->flags |= SWP_BLKDEV;
1939 } else if (S_ISREG(inode->i_mode)) {
1940 p->bdev = inode->i_sb->s_bdev;
1941 mutex_lock(&inode->i_mutex);
Cesar Eduardo Barros87ade722011-03-22 16:33:27 -07001942 if (IS_SWAPFILE(inode))
1943 return -EBUSY;
1944 } else
1945 return -EINVAL;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07001946
1947 return 0;
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07001948}
1949
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001950static unsigned long read_swap_header(struct swap_info_struct *p,
1951 union swap_header *swap_header,
1952 struct inode *inode)
1953{
1954 int i;
1955 unsigned long maxpages;
1956 unsigned long swapfilepages;
1957
1958 if (memcmp("SWAPSPACE2", swap_header->magic.magic, 10)) {
1959 printk(KERN_ERR "Unable to find swap-space signature\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07001960 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001961 }
1962
1963 /* swap partition endianess hack... */
1964 if (swab32(swap_header->info.version) == 1) {
1965 swab32s(&swap_header->info.version);
1966 swab32s(&swap_header->info.last_page);
1967 swab32s(&swap_header->info.nr_badpages);
1968 for (i = 0; i < swap_header->info.nr_badpages; i++)
1969 swab32s(&swap_header->info.badpages[i]);
1970 }
1971 /* Check the swap header's sub-version */
1972 if (swap_header->info.version != 1) {
1973 printk(KERN_WARNING
1974 "Unable to handle swap header version %d\n",
1975 swap_header->info.version);
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07001976 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001977 }
1978
1979 p->lowest_bit = 1;
1980 p->cluster_next = 1;
1981 p->cluster_nr = 0;
1982
1983 /*
1984 * Find out how many pages are allowed for a single swap
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001985 * device. There are three limiting factors: 1) the number
1986 * of bits for the swap offset in the swp_entry_t type, and
1987 * 2) the number of bits in the swap pte as defined by the
1988 * the different architectures, and 3) the number of free bits
1989 * in an exceptional radix_tree entry. In order to find the
1990 * largest possible bit mask, a swap entry with swap type 0
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001991 * and swap offset ~0UL is created, encoded to a swap pte,
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001992 * decoded to a swp_entry_t again, and finally the swap
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001993 * offset is extracted. This will mask all the bits from
1994 * the initial ~0UL mask that can't be encoded in either
1995 * the swp_entry_t or the architecture definition of a
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001996 * swap pte. Then the same is done for a radix_tree entry.
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07001997 */
1998 maxpages = swp_offset(pte_to_swp_entry(
Hugh Dickinsa2c16d62011-08-03 16:21:19 -07001999 swp_entry_to_pte(swp_entry(0, ~0UL))));
2000 maxpages = swp_offset(radix_to_swp_entry(
2001 swp_to_radix_entry(swp_entry(0, maxpages)))) + 1;
2002
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002003 if (maxpages > swap_header->info.last_page) {
2004 maxpages = swap_header->info.last_page + 1;
2005 /* p->max is an unsigned int: don't overflow it */
2006 if ((unsigned int)maxpages == 0)
2007 maxpages = UINT_MAX;
2008 }
2009 p->highest_bit = maxpages - 1;
2010
2011 if (!maxpages)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002012 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002013 swapfilepages = i_size_read(inode) >> PAGE_SHIFT;
2014 if (swapfilepages && maxpages > swapfilepages) {
2015 printk(KERN_WARNING
2016 "Swap area shorter than signature indicates\n");
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002017 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002018 }
2019 if (swap_header->info.nr_badpages && S_ISREG(inode->i_mode))
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002020 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002021 if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
Cesar Eduardo Barros38719022011-03-22 16:33:30 -07002022 return 0;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002023
2024 return maxpages;
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002025}
2026
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002027static int setup_swap_map_and_extents(struct swap_info_struct *p,
2028 union swap_header *swap_header,
2029 unsigned char *swap_map,
2030 unsigned long maxpages,
2031 sector_t *span)
2032{
2033 int i;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002034 unsigned int nr_good_pages;
2035 int nr_extents;
2036
2037 nr_good_pages = maxpages - 1; /* omit header page */
2038
2039 for (i = 0; i < swap_header->info.nr_badpages; i++) {
2040 unsigned int page_nr = swap_header->info.badpages[i];
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002041 if (page_nr == 0 || page_nr > swap_header->info.last_page)
2042 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002043 if (page_nr < maxpages) {
2044 swap_map[page_nr] = SWAP_MAP_BAD;
2045 nr_good_pages--;
2046 }
2047 }
2048
2049 if (nr_good_pages) {
2050 swap_map[0] = SWAP_MAP_BAD;
2051 p->max = maxpages;
2052 p->pages = nr_good_pages;
2053 nr_extents = setup_swap_extents(p, span);
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002054 if (nr_extents < 0)
2055 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002056 nr_good_pages = p->pages;
2057 }
2058 if (!nr_good_pages) {
2059 printk(KERN_WARNING "Empty swap-file\n");
Cesar Eduardo Barrosbdb8e3f2011-03-22 16:33:33 -07002060 return -EINVAL;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002061 }
2062
2063 return nr_extents;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002064}
2065
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002066SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
2067{
2068 struct swap_info_struct *p;
Cesar Eduardo Barros28b36bd2011-03-22 16:33:20 -07002069 char *name;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002070 struct file *swap_file = NULL;
2071 struct address_space *mapping;
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002072 int i;
2073 int prio;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002074 int error;
2075 union swap_header *swap_header;
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002076 int nr_extents;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002077 sector_t span;
2078 unsigned long maxpages;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002079 unsigned char *swap_map = NULL;
2080 struct page *page = NULL;
2081 struct inode *inode = NULL;
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002082
Hugh Dickinsd15cab92012-03-28 14:42:42 -07002083 if (swap_flags & ~SWAP_FLAGS_VALID)
2084 return -EINVAL;
2085
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002086 if (!capable(CAP_SYS_ADMIN))
2087 return -EPERM;
2088
2089 p = alloc_swap_info();
Cesar Eduardo Barros2542e5132011-03-22 16:33:18 -07002090 if (IS_ERR(p))
2091 return PTR_ERR(p);
Cesar Eduardo Barros53cbb242011-03-22 16:33:17 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 name = getname(specialfile);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (IS_ERR(name)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07002095 error = PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 name = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07002097 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099 swap_file = filp_open(name, O_RDWR|O_LARGEFILE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (IS_ERR(swap_file)) {
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07002101 error = PTR_ERR(swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 swap_file = NULL;
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07002103 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
2105
2106 p->swap_file = swap_file;
2107 mapping = swap_file->f_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 for (i = 0; i < nr_swapfiles; i++) {
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002110 struct swap_info_struct *q = swap_info[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Cesar Eduardo Barrose8e6c2e2011-03-22 16:33:16 -07002112 if (q == p || !q->swap_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 continue;
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07002114 if (mapping == q->swap_file->f_mapping) {
2115 error = -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto bad_swap;
Cesar Eduardo Barros7de7fb62011-03-22 16:33:22 -07002117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
2119
Cesar Eduardo Barros21307812011-03-22 23:03:13 -03002120 inode = mapping->host;
2121 /* If S_ISREG(inode->i_mode) will do mutex_lock(&inode->i_mutex); */
Cesar Eduardo Barros4d0e1e12011-03-22 16:33:26 -07002122 error = claim_swapfile(p, inode);
2123 if (unlikely(error))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 goto bad_swap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /*
2127 * Read the swap header.
2128 */
2129 if (!mapping->a_ops->readpage) {
2130 error = -EINVAL;
2131 goto bad_swap;
2132 }
Pekka Enberg090d2b12006-06-23 02:05:08 -07002133 page = read_mapping_page(mapping, 0, swap_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (IS_ERR(page)) {
2135 error = PTR_ERR(page);
2136 goto bad_swap;
2137 }
Hugh Dickins81e33972009-01-06 14:39:49 -08002138 swap_header = kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Cesar Eduardo Barrosca8bd382011-03-22 16:33:29 -07002140 maxpages = read_swap_header(p, swap_header, inode);
2141 if (unlikely(!maxpages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 error = -EINVAL;
2143 goto bad_swap;
2144 }
Hugh Dickins886bb7e2009-01-06 14:39:48 -08002145
Hugh Dickins81e33972009-01-06 14:39:49 -08002146 /* OK, set up the swap map and apply the bad block list */
Cesar Eduardo Barros803d0c832011-03-22 16:33:14 -07002147 swap_map = vzalloc(maxpages);
Hugh Dickins81e33972009-01-06 14:39:49 -08002148 if (!swap_map) {
2149 error = -ENOMEM;
2150 goto bad_swap;
2151 }
2152
Cesar Eduardo Barros1421ef32011-03-22 16:33:31 -07002153 error = swap_cgroup_swapon(p->type, maxpages);
2154 if (error)
2155 goto bad_swap;
2156
Cesar Eduardo Barros915d4d72011-03-22 16:33:32 -07002157 nr_extents = setup_swap_map_and_extents(p, swap_header, swap_map,
2158 maxpages, &span);
2159 if (unlikely(nr_extents < 0)) {
2160 error = nr_extents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto bad_swap;
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Suresh Jayaraman3bd0f0c2009-09-30 10:53:48 +02002164 if (p->bdev) {
2165 if (blk_queue_nonrot(bdev_get_queue(p->bdev))) {
2166 p->flags |= SWP_SOLIDSTATE;
2167 p->cluster_next = 1 + (random32() % p->highest_bit);
2168 }
Shaohua Li052b1982012-03-21 16:34:17 -07002169 if ((swap_flags & SWAP_FLAG_DISCARD) && discard_swap(p) == 0)
Suresh Jayaraman3bd0f0c2009-09-30 10:53:48 +02002170 p->flags |= SWP_DISCARDABLE;
Hugh Dickins20137a42009-01-06 14:39:54 -08002171 }
Hugh Dickins6a6ba832009-01-06 14:39:51 -08002172
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002173 mutex_lock(&swapon_mutex);
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002174 prio = -1;
Hugh Dickins78ecba02008-07-23 21:28:23 -07002175 if (swap_flags & SWAP_FLAG_PREFER)
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002176 prio =
Hugh Dickins78ecba02008-07-23 21:28:23 -07002177 (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT;
Cesar Eduardo Barros40531542011-03-22 16:33:37 -07002178 enable_swap_info(p, prio, swap_map);
Cesar Eduardo Barrosc69dbfb2011-03-22 16:33:35 -07002179
2180 printk(KERN_INFO "Adding %uk swap on %s. "
2181 "Priority:%d extents:%d across:%lluk %s%s\n",
2182 p->pages<<(PAGE_SHIFT-10), name, p->prio,
2183 nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10),
2184 (p->flags & SWP_SOLIDSTATE) ? "SS" : "",
2185 (p->flags & SWP_DISCARDABLE) ? "D" : "");
2186
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002187 mutex_unlock(&swapon_mutex);
Kay Sievers66d7dd52010-10-26 14:22:06 -07002188 atomic_inc(&proc_poll_event);
2189 wake_up_interruptible(&proc_poll_wait);
2190
Cesar Eduardo Barros9b01c352011-03-22 16:33:24 -07002191 if (S_ISREG(inode->i_mode))
2192 inode->i_flags |= S_SWAPFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 error = 0;
2194 goto out;
2195bad_swap:
Cesar Eduardo Barrosbd690102011-03-22 16:33:25 -07002196 if (inode && S_ISBLK(inode->i_mode) && p->bdev) {
Cesar Eduardo Barrosf2090d22011-03-22 16:33:23 -07002197 set_blocksize(p->bdev, p->old_block_size);
2198 blkdev_put(p->bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Hugh Dickins4cd3bb12005-09-03 15:54:33 -07002200 destroy_swap_extents(p);
Cesar Eduardo Barrose8e6c2e2011-03-22 16:33:16 -07002201 swap_cgroup_swapoff(p->type);
Hugh Dickins5d337b92005-09-03 15:54:41 -07002202 spin_lock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 p->swap_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 p->flags = 0;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002205 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 vfree(swap_map);
Mel Gorman52c50562011-03-22 16:30:08 -07002207 if (swap_file) {
Cesar Eduardo Barros21307812011-03-22 23:03:13 -03002208 if (inode && S_ISREG(inode->i_mode)) {
Mel Gorman52c50562011-03-22 16:30:08 -07002209 mutex_unlock(&inode->i_mutex);
Cesar Eduardo Barros21307812011-03-22 23:03:13 -03002210 inode = NULL;
2211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 filp_close(swap_file, NULL);
Mel Gorman52c50562011-03-22 16:30:08 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214out:
2215 if (page && !IS_ERR(page)) {
2216 kunmap(page);
2217 page_cache_release(page);
2218 }
2219 if (name)
2220 putname(name);
Cesar Eduardo Barros9b01c352011-03-22 16:33:24 -07002221 if (inode && S_ISREG(inode->i_mode))
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002222 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return error;
2224}
2225
2226void si_swapinfo(struct sysinfo *val)
2227{
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002228 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 unsigned long nr_to_be_unused = 0;
2230
Hugh Dickins5d337b92005-09-03 15:54:41 -07002231 spin_lock(&swap_lock);
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002232 for (type = 0; type < nr_swapfiles; type++) {
2233 struct swap_info_struct *si = swap_info[type];
2234
2235 if ((si->flags & SWP_USED) && !(si->flags & SWP_WRITEOK))
2236 nr_to_be_unused += si->inuse_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Shaohua Li1abbca72013-02-22 16:34:38 -08002238 val->freeswap = atomic_long_read(&nr_swap_pages) + nr_to_be_unused;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 val->totalswap = total_swap_pages + nr_to_be_unused;
Hugh Dickins5d337b92005-09-03 15:54:41 -07002240 spin_unlock(&swap_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242
2243/*
2244 * Verify that a swap entry is valid and increment its swap map count.
2245 *
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002246 * Returns error code in following case.
2247 * - success -> 0
2248 * - swp_entry is invalid -> EINVAL
2249 * - swp_entry is migration entry -> EINVAL
2250 * - swap-cache reference is requested but there is already one. -> EEXIST
2251 * - swap-cache reference is requested but the entry is not used. -> ENOENT
Hugh Dickins570a3352009-12-14 17:58:46 -08002252 * - swap-mapped reference requested but needs continued swap count. -> ENOMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 */
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002254static int __swap_duplicate(swp_entry_t entry, unsigned char usage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
Hugh Dickins73c34b62009-12-14 17:58:43 -08002256 struct swap_info_struct *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 unsigned long offset, type;
Hugh Dickins8d69aae2009-12-14 17:58:45 -08002258 unsigned char count;
2259 unsigned char has_cache;
Hugh Dickins253d5532009-12-14 17:58:44 -08002260 int err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Andi Kleena7420aa2009-09-16 11:50:05 +02002262 if (non_swap_entry(entry))
Hugh Dickins253d5532009-12-14 17:58:44 -08002263 goto out;
Christoph Lameter06972122006-06-23 02:03:35 -07002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 type = swp_type(entry);
2266 if (type >= nr_swapfiles)
2267 goto bad_file;
Hugh Dickinsefa90a92009-12-14 17:58:41 -08002268 p = swap_info[type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 offset = swp_offset(entry);
2270
Shaohua Li1abbca72013-02-22 16:34:38 -08002271 spin_lock(&p->lock);
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002272 if (unlikely(offset >= p->max))
2273 goto unlock_out;
2274
Hugh Dickins253d5532009-12-14 17:58:44 -08002275 count = p->swap_map[offset];
2276 has_cache = count & SWAP_HAS_CACHE;
2277 count &= ~SWAP_HAS_CACHE;
2278 err = 0;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002279
Hugh Dickins253d5532009-12-14 17:58:44 -08002280 if (usage == SWAP_HAS_CACHE) {
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002281
2282 /* set SWAP_HAS_CACHE if there is no cache and entry is used */
Hugh Dickins253d5532009-12-14 17:58:44 -08002283 if (!has_cache && count)
2284 has_cache = SWAP_HAS_CACHE;
2285 else if (has_cache) /* someone else added cache */
2286 err = -EEXIST;
2287 else /* no users remaining */
2288 err = -ENOENT;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002289
2290 } else if (count || has_cache) {
Hugh Dickins253d5532009-12-14 17:58:44 -08002291
Hugh Dickins570a3352009-12-14 17:58:46 -08002292 if ((count & ~COUNT_CONTINUED) < SWAP_MAP_MAX)
2293 count += usage;
2294 else if ((count & ~COUNT_CONTINUED) > SWAP_MAP_MAX)
Hugh Dickins253d5532009-12-14 17:58:44 -08002295 err = -EINVAL;
Hugh Dickins570a3352009-12-14 17:58:46 -08002296 else if (swap_count_continued(p, offset, count))
2297 count = COUNT_CONTINUED;
2298 else
2299 err = -ENOMEM;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002300 } else
Hugh Dickins253d5532009-12-14 17:58:44 -08002301 err = -ENOENT; /* unused swap entry */
2302
2303 p->swap_map[offset] = count | has_cache;
2304
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002305unlock_out:
Shaohua Li1abbca72013-02-22 16:34:38 -08002306 spin_unlock(&p->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307out:
Hugh Dickins253d5532009-12-14 17:58:44 -08002308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310bad_file:
2311 printk(KERN_ERR "swap_dup: %s%08lx\n", Bad_file, entry.val);
2312 goto out;
2313}
Hugh Dickins253d5532009-12-14 17:58:44 -08002314
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002315/*
Hugh Dickinsaaa46862009-12-14 17:58:47 -08002316 * Help swapoff by noting that swap entry belongs to shmem/tmpfs
2317 * (in which case its reference count is never incremented).
2318 */
2319void swap_shmem_alloc(swp_entry_t entry)
2320{
2321 __swap_duplicate(entry, SWAP_MAP_SHMEM);
2322}
2323
2324/*
Hugh Dickins08259d52010-03-05 13:42:25 -08002325 * Increase reference count of swap entry by 1.
2326 * Returns 0 for success, or -ENOMEM if a swap_count_continuation is required
2327 * but could not be atomically allocated. Returns 0, just as if it succeeded,
2328 * if __swap_duplicate() fails for another reason (-EINVAL or -ENOENT), which
2329 * might occur if a page table entry has got corrupted.
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002330 */
Hugh Dickins570a3352009-12-14 17:58:46 -08002331int swap_duplicate(swp_entry_t entry)
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002332{
Hugh Dickins570a3352009-12-14 17:58:46 -08002333 int err = 0;
2334
2335 while (!err && __swap_duplicate(entry, 1) == -ENOMEM)
2336 err = add_swap_count_continuation(entry, GFP_ATOMIC);
2337 return err;
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002338}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002340/*
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002341 * @entry: swap entry for which we allocate swap cache.
2342 *
Hugh Dickins73c34b62009-12-14 17:58:43 -08002343 * Called when allocating swap cache for existing swap entry,
KAMEZAWA Hiroyuki355cfa72009-06-16 15:32:53 -07002344 * This can return error codes. Returns 0 at success.
2345 * -EBUSY means there is a swap cache.
2346 * Note: return code is different from swap_duplicate().
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002347 */
2348int swapcache_prepare(swp_entry_t entry)
2349{
Hugh Dickins253d5532009-12-14 17:58:44 -08002350 return __swap_duplicate(entry, SWAP_HAS_CACHE);
KAMEZAWA Hiroyukicb4b86b2009-06-16 15:32:52 -07002351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/*
Hugh Dickins570a3352009-12-14 17:58:46 -08002354 * add_swap_count_continuation - called when a swap count is duplicated
2355 * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
2356 * page of the original vmalloc'ed swap_map, to hold the continuation count
2357 * (for that entry and for its neighbouring PAGE_SIZE swap entries). Called
2358 * again when count is duplicated beyond SWAP_MAP_MAX * SWAP_CONT_MAX, etc.
2359 *
2360 * These continuation pages are seldom referenced: the common paths all work
2361 * on the original swap_map, only referring to a continuation page when the
2362 * low "digit" of a count is incremented or decremented through SWAP_MAP_MAX.
2363 *
2364 * add_swap_count_continuation(, GFP_ATOMIC) can be called while holding
2365 * page table locks; if it fails, add_swap_count_continuation(, GFP_KERNEL)
2366 * can be called after dropping locks.
2367 */
2368int add_swap_count_continuation(swp_entry_t entry, gfp_t gfp_mask)
2369{
2370 struct swap_info_struct *si;
2371 struct page *head;
2372 struct page *page;
2373 struct page *list_page;
2374 pgoff_t offset;
2375 unsigned char count;
2376
2377 /*
2378 * When debugging, it's easier to use __GFP_ZERO here; but it's better
2379 * for latency not to zero a page while GFP_ATOMIC and holding locks.
2380 */
2381 page = alloc_page(gfp_mask | __GFP_HIGHMEM);
2382
2383 si = swap_info_get(entry);
2384 if (!si) {
2385 /*
2386 * An acceptable race has occurred since the failing
2387 * __swap_duplicate(): the swap entry has been freed,
2388 * perhaps even the whole swap_map cleared for swapoff.
2389 */
2390 goto outer;
2391 }
2392
2393 offset = swp_offset(entry);
2394 count = si->swap_map[offset] & ~SWAP_HAS_CACHE;
2395
2396 if ((count & ~COUNT_CONTINUED) != SWAP_MAP_MAX) {
2397 /*
2398 * The higher the swap count, the more likely it is that tasks
2399 * will race to add swap count continuation: we need to avoid
2400 * over-provisioning.
2401 */
2402 goto out;
2403 }
2404
2405 if (!page) {
Shaohua Li1abbca72013-02-22 16:34:38 -08002406 spin_unlock(&si->lock);
Hugh Dickins570a3352009-12-14 17:58:46 -08002407 return -ENOMEM;
2408 }
2409
2410 /*
2411 * We are fortunate that although vmalloc_to_page uses pte_offset_map,
2412 * no architecture is using highmem pages for kernel pagetables: so it
2413 * will not corrupt the GFP_ATOMIC caller's atomic pagetable kmaps.
2414 */
2415 head = vmalloc_to_page(si->swap_map + offset);
2416 offset &= ~PAGE_MASK;
2417
2418 /*
2419 * Page allocation does not initialize the page's lru field,
2420 * but it does always reset its private field.
2421 */
2422 if (!page_private(head)) {
2423 BUG_ON(count & COUNT_CONTINUED);
2424 INIT_LIST_HEAD(&head->lru);
2425 set_page_private(head, SWP_CONTINUED);
2426 si->flags |= SWP_CONTINUED;
2427 }
2428
2429 list_for_each_entry(list_page, &head->lru, lru) {
2430 unsigned char *map;
2431
2432 /*
2433 * If the previous map said no continuation, but we've found
2434 * a continuation page, free our allocation and use this one.
2435 */
2436 if (!(count & COUNT_CONTINUED))
2437 goto out;
2438
Cong Wang9b04c5f2011-11-25 23:14:39 +08002439 map = kmap_atomic(list_page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002440 count = *map;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002441 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002442
2443 /*
2444 * If this continuation count now has some space in it,
2445 * free our allocation and use this one.
2446 */
2447 if ((count & ~COUNT_CONTINUED) != SWAP_CONT_MAX)
2448 goto out;
2449 }
2450
2451 list_add_tail(&page->lru, &head->lru);
2452 page = NULL; /* now it's attached, don't free it */
2453out:
Shaohua Li1abbca72013-02-22 16:34:38 -08002454 spin_unlock(&si->lock);
Hugh Dickins570a3352009-12-14 17:58:46 -08002455outer:
2456 if (page)
2457 __free_page(page);
2458 return 0;
2459}
2460
2461/*
2462 * swap_count_continued - when the original swap_map count is incremented
2463 * from SWAP_MAP_MAX, check if there is already a continuation page to carry
2464 * into, carry if so, or else fail until a new continuation page is allocated;
2465 * when the original swap_map count is decremented from 0 with continuation,
2466 * borrow from the continuation and report whether it still holds more.
2467 * Called while __swap_duplicate() or swap_entry_free() holds swap_lock.
2468 */
2469static bool swap_count_continued(struct swap_info_struct *si,
2470 pgoff_t offset, unsigned char count)
2471{
2472 struct page *head;
2473 struct page *page;
2474 unsigned char *map;
2475
2476 head = vmalloc_to_page(si->swap_map + offset);
2477 if (page_private(head) != SWP_CONTINUED) {
2478 BUG_ON(count & COUNT_CONTINUED);
2479 return false; /* need to add count continuation */
2480 }
2481
2482 offset &= ~PAGE_MASK;
2483 page = list_entry(head->lru.next, struct page, lru);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002484 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002485
2486 if (count == SWAP_MAP_MAX) /* initial increment from swap_map */
2487 goto init_map; /* jump over SWAP_CONT_MAX checks */
2488
2489 if (count == (SWAP_MAP_MAX | COUNT_CONTINUED)) { /* incrementing */
2490 /*
2491 * Think of how you add 1 to 999
2492 */
2493 while (*map == (SWAP_CONT_MAX | COUNT_CONTINUED)) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002494 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002495 page = list_entry(page->lru.next, struct page, lru);
2496 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002497 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002498 }
2499 if (*map == SWAP_CONT_MAX) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002500 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002501 page = list_entry(page->lru.next, struct page, lru);
2502 if (page == head)
2503 return false; /* add count continuation */
Cong Wang9b04c5f2011-11-25 23:14:39 +08002504 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002505init_map: *map = 0; /* we didn't zero the page */
2506 }
2507 *map += 1;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002508 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002509 page = list_entry(page->lru.prev, struct page, lru);
2510 while (page != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002511 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002512 *map = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002513 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002514 page = list_entry(page->lru.prev, struct page, lru);
2515 }
2516 return true; /* incremented */
2517
2518 } else { /* decrementing */
2519 /*
2520 * Think of how you subtract 1 from 1000
2521 */
2522 BUG_ON(count != COUNT_CONTINUED);
2523 while (*map == COUNT_CONTINUED) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002524 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002525 page = list_entry(page->lru.next, struct page, lru);
2526 BUG_ON(page == head);
Cong Wang9b04c5f2011-11-25 23:14:39 +08002527 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002528 }
2529 BUG_ON(*map == 0);
2530 *map -= 1;
2531 if (*map == 0)
2532 count = 0;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002533 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002534 page = list_entry(page->lru.prev, struct page, lru);
2535 while (page != head) {
Cong Wang9b04c5f2011-11-25 23:14:39 +08002536 map = kmap_atomic(page) + offset;
Hugh Dickins570a3352009-12-14 17:58:46 -08002537 *map = SWAP_CONT_MAX | count;
2538 count = COUNT_CONTINUED;
Cong Wang9b04c5f2011-11-25 23:14:39 +08002539 kunmap_atomic(map);
Hugh Dickins570a3352009-12-14 17:58:46 -08002540 page = list_entry(page->lru.prev, struct page, lru);
2541 }
2542 return count == COUNT_CONTINUED;
2543 }
2544}
2545
2546/*
2547 * free_swap_count_continuations - swapoff free all the continuation pages
2548 * appended to the swap_map, after swap_map is quiesced, before vfree'ing it.
2549 */
2550static void free_swap_count_continuations(struct swap_info_struct *si)
2551{
2552 pgoff_t offset;
2553
2554 for (offset = 0; offset < si->max; offset += PAGE_SIZE) {
2555 struct page *head;
2556 head = vmalloc_to_page(si->swap_map + offset);
2557 if (page_private(head)) {
2558 struct list_head *this, *next;
2559 list_for_each_safe(this, next, &head->lru) {
2560 struct page *page;
2561 page = list_entry(this, struct page, lru);
2562 list_del(this);
2563 __free_page(page);
2564 }
2565 }
2566 }
2567}