4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
9 #include <linux/hugetlb.h>
10 #include <linux/mman.h>
11 #include <linux/slab.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/swap.h>
14 #include <linux/vmalloc.h>
15 #include <linux/pagemap.h>
16 #include <linux/namei.h>
17 #include <linux/shm.h>
18 #include <linux/blkdev.h>
19 #include <linux/writeback.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/rmap.h>
25 #include <linux/security.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mutex.h>
28 #include <linux/capability.h>
29 #include <linux/syscalls.h>
31 #include <asm/pgtable.h>
32 #include <asm/tlbflush.h>
33 #include <linux/swapops.h>
35 #if 0 // mask by Victor Yu. 03-15-2007
36 DEFINE_SPINLOCK(swap_lock
);
38 spinlock_t swap_lock
=SPIN_LOCK_UNLOCKED
;
40 unsigned int nr_swapfiles
;
41 long total_swap_pages
;
42 static int swap_overflow
;
44 static const char Bad_file
[] = "Bad swap file entry ";
45 static const char Unused_file
[] = "Unused swap file entry ";
46 static const char Bad_offset
[] = "Bad swap offset entry ";
47 static const char Unused_offset
[] = "Unused swap offset entry ";
49 struct swap_list_t swap_list
= {-1, -1};
51 static struct swap_info_struct swap_info
[MAX_SWAPFILES
];
53 static DEFINE_MUTEX(swapon_mutex
);
56 * We need this because the bdev->unplug_fn can sleep and we cannot
57 * hold swap_lock while calling the unplug_fn. And swap_lock
58 * cannot be turned into a mutex.
60 static DECLARE_RWSEM(swap_unplug_sem
);
62 void swap_unplug_io_fn(struct backing_dev_info
*unused_bdi
, struct page
*page
)
66 down_read(&swap_unplug_sem
);
67 entry
.val
= page_private(page
);
68 if (PageSwapCache(page
)) {
69 struct block_device
*bdev
= swap_info
[swp_type(entry
)].bdev
;
70 struct backing_dev_info
*bdi
;
73 * If the page is removed from swapcache from under us (with a
74 * racy try_to_unuse/swapoff) we need an additional reference
75 * count to avoid reading garbage from page_private(page) above.
76 * If the WARN_ON triggers during a swapoff it maybe the race
77 * condition and it's harmless. However if it triggers without
78 * swapoff it signals a problem.
80 WARN_ON(page_count(page
) <= 1);
82 bdi
= bdev
->bd_inode
->i_mapping
->backing_dev_info
;
83 blk_run_backing_dev(bdi
, page
);
85 up_read(&swap_unplug_sem
);
88 #define SWAPFILE_CLUSTER 256
89 #define LATENCY_LIMIT 256
91 static inline unsigned long scan_swap_map(struct swap_info_struct
*si
)
93 unsigned long offset
, last_in_cluster
;
94 int latency_ration
= LATENCY_LIMIT
;
97 * We try to cluster swap pages by allocating them sequentially
98 * in swap. Once we've allocated SWAPFILE_CLUSTER pages this
99 * way, however, we resort to first-free allocation, starting
100 * a new cluster. This prevents us from scattering swap pages
101 * all over the entire swap partition, so that we reduce
102 * overall disk seek times between swap pages. -- sct
103 * But we do now try to find an empty cluster. -Andrea
106 si
->flags
+= SWP_SCANNING
;
107 if (unlikely(!si
->cluster_nr
)) {
108 si
->cluster_nr
= SWAPFILE_CLUSTER
- 1;
109 if (si
->pages
- si
->inuse_pages
< SWAPFILE_CLUSTER
)
111 spin_unlock(&swap_lock
);
113 offset
= si
->lowest_bit
;
114 last_in_cluster
= offset
+ SWAPFILE_CLUSTER
- 1;
116 /* Locate the first empty (unaligned) cluster */
117 for (; last_in_cluster
<= si
->highest_bit
; offset
++) {
118 if (si
->swap_map
[offset
])
119 last_in_cluster
= offset
+ SWAPFILE_CLUSTER
;
120 else if (offset
== last_in_cluster
) {
121 spin_lock(&swap_lock
);
122 si
->cluster_next
= offset
-SWAPFILE_CLUSTER
+1;
125 if (unlikely(--latency_ration
< 0)) {
127 latency_ration
= LATENCY_LIMIT
;
130 spin_lock(&swap_lock
);
136 offset
= si
->cluster_next
;
137 if (offset
> si
->highest_bit
)
138 lowest
: offset
= si
->lowest_bit
;
139 checks
: if (!(si
->flags
& SWP_WRITEOK
))
141 if (!si
->highest_bit
)
143 if (!si
->swap_map
[offset
]) {
144 if (offset
== si
->lowest_bit
)
146 if (offset
== si
->highest_bit
)
149 if (si
->inuse_pages
== si
->pages
) {
150 si
->lowest_bit
= si
->max
;
153 si
->swap_map
[offset
] = 1;
154 si
->cluster_next
= offset
+ 1;
155 si
->flags
-= SWP_SCANNING
;
159 spin_unlock(&swap_lock
);
160 while (++offset
<= si
->highest_bit
) {
161 if (!si
->swap_map
[offset
]) {
162 spin_lock(&swap_lock
);
165 if (unlikely(--latency_ration
< 0)) {
167 latency_ration
= LATENCY_LIMIT
;
170 spin_lock(&swap_lock
);
174 si
->flags
-= SWP_SCANNING
;
178 swp_entry_t
get_swap_page(void)
180 struct swap_info_struct
*si
;
185 spin_lock(&swap_lock
);
186 if (nr_swap_pages
<= 0)
190 for (type
= swap_list
.next
; type
>= 0 && wrapped
< 2; type
= next
) {
191 si
= swap_info
+ type
;
194 (!wrapped
&& si
->prio
!= swap_info
[next
].prio
)) {
195 next
= swap_list
.head
;
199 if (!si
->highest_bit
)
201 if (!(si
->flags
& SWP_WRITEOK
))
204 swap_list
.next
= next
;
205 offset
= scan_swap_map(si
);
207 spin_unlock(&swap_lock
);
208 return swp_entry(type
, offset
);
210 next
= swap_list
.next
;
215 spin_unlock(&swap_lock
);
216 return (swp_entry_t
) {0};
219 swp_entry_t
get_swap_page_of_type(int type
)
221 struct swap_info_struct
*si
;
224 spin_lock(&swap_lock
);
225 si
= swap_info
+ type
;
226 if (si
->flags
& SWP_WRITEOK
) {
228 offset
= scan_swap_map(si
);
230 spin_unlock(&swap_lock
);
231 return swp_entry(type
, offset
);
235 spin_unlock(&swap_lock
);
236 return (swp_entry_t
) {0};
239 static struct swap_info_struct
* swap_info_get(swp_entry_t entry
)
241 struct swap_info_struct
* p
;
242 unsigned long offset
, type
;
246 type
= swp_type(entry
);
247 if (type
>= nr_swapfiles
)
249 p
= & swap_info
[type
];
250 if (!(p
->flags
& SWP_USED
))
252 offset
= swp_offset(entry
);
253 if (offset
>= p
->max
)
255 if (!p
->swap_map
[offset
])
257 spin_lock(&swap_lock
);
261 printk(KERN_ERR
"swap_free: %s%08lx\n", Unused_offset
, entry
.val
);
264 printk(KERN_ERR
"swap_free: %s%08lx\n", Bad_offset
, entry
.val
);
267 printk(KERN_ERR
"swap_free: %s%08lx\n", Unused_file
, entry
.val
);
270 printk(KERN_ERR
"swap_free: %s%08lx\n", Bad_file
, entry
.val
);
275 static int swap_entry_free(struct swap_info_struct
*p
, unsigned long offset
)
277 int count
= p
->swap_map
[offset
];
279 if (count
< SWAP_MAP_MAX
) {
281 p
->swap_map
[offset
] = count
;
283 if (offset
< p
->lowest_bit
)
284 p
->lowest_bit
= offset
;
285 if (offset
> p
->highest_bit
)
286 p
->highest_bit
= offset
;
287 if (p
->prio
> swap_info
[swap_list
.next
].prio
)
288 swap_list
.next
= p
- swap_info
;
297 * Caller has made sure that the swapdevice corresponding to entry
298 * is still around or has not been recycled.
300 void swap_free(swp_entry_t entry
)
302 struct swap_info_struct
* p
;
304 p
= swap_info_get(entry
);
306 swap_entry_free(p
, swp_offset(entry
));
307 spin_unlock(&swap_lock
);
312 * How many references to page are currently swapped out?
314 static inline int page_swapcount(struct page
*page
)
317 struct swap_info_struct
*p
;
320 entry
.val
= page_private(page
);
321 p
= swap_info_get(entry
);
323 /* Subtract the 1 for the swap cache itself */
324 count
= p
->swap_map
[swp_offset(entry
)] - 1;
325 spin_unlock(&swap_lock
);
331 * We can use this swap cache entry directly
332 * if there are no other references to it.
334 int can_share_swap_page(struct page
*page
)
338 BUG_ON(!PageLocked(page
));
339 count
= page_mapcount(page
);
340 if (count
<= 1 && PageSwapCache(page
))
341 count
+= page_swapcount(page
);
346 * Work out if there are any other processes sharing this
347 * swap cache page. Free it if you can. Return success.
349 int remove_exclusive_swap_page(struct page
*page
)
352 struct swap_info_struct
* p
;
355 BUG_ON(PagePrivate(page
));
356 BUG_ON(!PageLocked(page
));
358 if (!PageSwapCache(page
))
360 if (PageWriteback(page
))
362 if (page_count(page
) != 2) /* 2: us + cache */
365 entry
.val
= page_private(page
);
366 p
= swap_info_get(entry
);
370 /* Is the only swap cache user the cache itself? */
372 if (p
->swap_map
[swp_offset(entry
)] == 1) {
373 /* Recheck the page count with the swapcache lock held.. */
374 write_lock_irq(&swapper_space
.tree_lock
);
375 if ((page_count(page
) == 2) && !PageWriteback(page
)) {
376 __delete_from_swap_cache(page
);
380 write_unlock_irq(&swapper_space
.tree_lock
);
382 spin_unlock(&swap_lock
);
386 page_cache_release(page
);
393 * Free the swap entry like above, but also try to
394 * free the page cache entry if it is the last user.
396 void free_swap_and_cache(swp_entry_t entry
)
398 struct swap_info_struct
* p
;
399 struct page
*page
= NULL
;
401 if (is_migration_entry(entry
))
404 p
= swap_info_get(entry
);
406 if (swap_entry_free(p
, swp_offset(entry
)) == 1) {
407 page
= find_get_page(&swapper_space
, entry
.val
);
408 if (page
&& unlikely(TestSetPageLocked(page
))) {
409 page_cache_release(page
);
413 spin_unlock(&swap_lock
);
418 BUG_ON(PagePrivate(page
));
419 one_user
= (page_count(page
) == 2);
420 /* Only cache user (+us), or swap space full? Free it! */
421 /* Also recheck PageSwapCache after page is locked (above) */
422 if (PageSwapCache(page
) && !PageWriteback(page
) &&
423 (one_user
|| vm_swap_full())) {
424 delete_from_swap_cache(page
);
428 page_cache_release(page
);
432 #ifdef CONFIG_SOFTWARE_SUSPEND
434 * Find the swap type that corresponds to given device (if any)
436 * This is needed for software suspend and is done in such a way that inode
437 * aliasing is allowed.
439 int swap_type_of(dev_t device
)
443 spin_lock(&swap_lock
);
444 for (i
= 0; i
< nr_swapfiles
; i
++) {
447 if (!(swap_info
[i
].flags
& SWP_WRITEOK
))
451 spin_unlock(&swap_lock
);
454 inode
= swap_info
[i
].swap_file
->f_dentry
->d_inode
;
455 if (S_ISBLK(inode
->i_mode
) &&
456 device
== MKDEV(imajor(inode
), iminor(inode
))) {
457 spin_unlock(&swap_lock
);
461 spin_unlock(&swap_lock
);
466 * Return either the total number of swap pages of given type, or the number
467 * of free pages of that type (depending on @free)
469 * This is needed for software suspend
471 unsigned int count_swap_pages(int type
, int free
)
475 if (type
< nr_swapfiles
) {
476 spin_lock(&swap_lock
);
477 if (swap_info
[type
].flags
& SWP_WRITEOK
) {
478 n
= swap_info
[type
].pages
;
480 n
-= swap_info
[type
].inuse_pages
;
482 spin_unlock(&swap_lock
);
489 * No need to decide whether this PTE shares the swap entry with others,
490 * just let do_wp_page work it out if a write is requested later - to
491 * force COW, vm_page_prot omits write permission from any private vma.
493 static void unuse_pte(struct vm_area_struct
*vma
, pte_t
*pte
,
494 unsigned long addr
, swp_entry_t entry
, struct page
*page
)
496 inc_mm_counter(vma
->vm_mm
, anon_rss
);
498 set_pte_at(vma
->vm_mm
, addr
, pte
,
499 pte_mkold(mk_pte(page
, vma
->vm_page_prot
)));
500 page_add_anon_rmap(page
, vma
, addr
);
503 * Move the page to the active list so it is not
504 * immediately swapped out again after swapon.
509 static int unuse_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
510 unsigned long addr
, unsigned long end
,
511 swp_entry_t entry
, struct page
*page
)
513 pte_t swp_pte
= swp_entry_to_pte(entry
);
518 pte
= pte_offset_map_lock(vma
->vm_mm
, pmd
, addr
, &ptl
);
521 * swapoff spends a _lot_ of time in this loop!
522 * Test inline before going to call unuse_pte.
524 if (unlikely(pte_same(*pte
, swp_pte
))) {
525 unuse_pte(vma
, pte
++, addr
, entry
, page
);
529 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
530 pte_unmap_unlock(pte
- 1, ptl
);
534 static inline int unuse_pmd_range(struct vm_area_struct
*vma
, pud_t
*pud
,
535 unsigned long addr
, unsigned long end
,
536 swp_entry_t entry
, struct page
*page
)
541 pmd
= pmd_offset(pud
, addr
);
543 next
= pmd_addr_end(addr
, end
);
544 if (pmd_none_or_clear_bad(pmd
))
546 if (unuse_pte_range(vma
, pmd
, addr
, next
, entry
, page
))
548 } while (pmd
++, addr
= next
, addr
!= end
);
552 static inline int unuse_pud_range(struct vm_area_struct
*vma
, pgd_t
*pgd
,
553 unsigned long addr
, unsigned long end
,
554 swp_entry_t entry
, struct page
*page
)
559 pud
= pud_offset(pgd
, addr
);
561 next
= pud_addr_end(addr
, end
);
562 if (pud_none_or_clear_bad(pud
))
564 if (unuse_pmd_range(vma
, pud
, addr
, next
, entry
, page
))
566 } while (pud
++, addr
= next
, addr
!= end
);
570 static int unuse_vma(struct vm_area_struct
*vma
,
571 swp_entry_t entry
, struct page
*page
)
574 unsigned long addr
, end
, next
;
577 addr
= page_address_in_vma(page
, vma
);
581 end
= addr
+ PAGE_SIZE
;
583 addr
= vma
->vm_start
;
587 pgd
= pgd_offset(vma
->vm_mm
, addr
);
589 next
= pgd_addr_end(addr
, end
);
590 if (pgd_none_or_clear_bad(pgd
))
592 if (unuse_pud_range(vma
, pgd
, addr
, next
, entry
, page
))
594 } while (pgd
++, addr
= next
, addr
!= end
);
598 static int unuse_mm(struct mm_struct
*mm
,
599 swp_entry_t entry
, struct page
*page
)
601 struct vm_area_struct
*vma
;
603 if (!down_read_trylock(&mm
->mmap_sem
)) {
605 * Activate page so shrink_cache is unlikely to unmap its
606 * ptes while lock is dropped, so swapoff can make progress.
610 down_read(&mm
->mmap_sem
);
613 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
614 if (vma
->anon_vma
&& unuse_vma(vma
, entry
, page
))
617 up_read(&mm
->mmap_sem
);
619 * Currently unuse_mm cannot fail, but leave error handling
620 * at call sites for now, since we change it from time to time.
626 * Scan swap_map from current position to next entry still in use.
627 * Recycle to start on reaching the end, returning 0 when empty.
629 static unsigned int find_next_to_unuse(struct swap_info_struct
*si
,
632 unsigned int max
= si
->max
;
633 unsigned int i
= prev
;
637 * No need for swap_lock here: we're just looking
638 * for whether an entry is in use, not modifying it; false
639 * hits are okay, and sys_swapoff() has already prevented new
640 * allocations from this area (while holding swap_lock).
649 * No entries in use at top of swap_map,
650 * loop back to start and recheck there.
656 count
= si
->swap_map
[i
];
657 if (count
&& count
!= SWAP_MAP_BAD
)
664 * We completely avoid races by reading each swap page in advance,
665 * and then search for the process using it. All the necessary
666 * page table adjustments can then be made atomically.
668 static int try_to_unuse(unsigned int type
)
670 struct swap_info_struct
* si
= &swap_info
[type
];
671 struct mm_struct
*start_mm
;
672 unsigned short *swap_map
;
673 unsigned short swcount
;
678 int reset_overflow
= 0;
682 * When searching mms for an entry, a good strategy is to
683 * start at the first mm we freed the previous entry from
684 * (though actually we don't notice whether we or coincidence
685 * freed the entry). Initialize this start_mm with a hold.
687 * A simpler strategy would be to start at the last mm we
688 * freed the previous entry from; but that would take less
689 * advantage of mmlist ordering, which clusters forked mms
690 * together, child after parent. If we race with dup_mmap(), we
691 * prefer to resolve parent before child, lest we miss entries
692 * duplicated after we scanned child: using last mm would invert
693 * that. Though it's only a serious concern when an overflowed
694 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
697 atomic_inc(&init_mm
.mm_users
);
700 * Keep on scanning until all entries have gone. Usually,
701 * one pass through swap_map is enough, but not necessarily:
702 * there are races when an instance of an entry might be missed.
704 while ((i
= find_next_to_unuse(si
, i
)) != 0) {
705 if (signal_pending(current
)) {
711 * Get a page for the entry, using the existing swap
712 * cache page if there is one. Otherwise, get a clean
713 * page and read the swap into it.
715 swap_map
= &si
->swap_map
[i
];
716 entry
= swp_entry(type
, i
);
717 page
= read_swap_cache_async(entry
, NULL
, 0);
720 * Either swap_duplicate() failed because entry
721 * has been freed independently, and will not be
722 * reused since sys_swapoff() already disabled
723 * allocation from here, or alloc_page() failed.
732 * Don't hold on to start_mm if it looks like exiting.
734 if (atomic_read(&start_mm
->mm_users
) == 1) {
737 atomic_inc(&init_mm
.mm_users
);
741 * Wait for and lock page. When do_swap_page races with
742 * try_to_unuse, do_swap_page can handle the fault much
743 * faster than try_to_unuse can locate the entry. This
744 * apparently redundant "wait_on_page_locked" lets try_to_unuse
745 * defer to do_swap_page in such a case - in some tests,
746 * do_swap_page and try_to_unuse repeatedly compete.
748 wait_on_page_locked(page
);
749 wait_on_page_writeback(page
);
751 wait_on_page_writeback(page
);
754 * Remove all references to entry.
755 * Whenever we reach init_mm, there's no address space
756 * to search, but use it as a reminder to search shmem.
761 if (start_mm
== &init_mm
)
762 shmem
= shmem_unuse(entry
, page
);
764 retval
= unuse_mm(start_mm
, entry
, page
);
767 int set_start_mm
= (*swap_map
>= swcount
);
768 struct list_head
*p
= &start_mm
->mmlist
;
769 struct mm_struct
*new_start_mm
= start_mm
;
770 struct mm_struct
*prev_mm
= start_mm
;
771 struct mm_struct
*mm
;
773 atomic_inc(&new_start_mm
->mm_users
);
774 atomic_inc(&prev_mm
->mm_users
);
775 spin_lock(&mmlist_lock
);
776 while (*swap_map
> 1 && !retval
&&
777 (p
= p
->next
) != &start_mm
->mmlist
) {
778 mm
= list_entry(p
, struct mm_struct
, mmlist
);
779 if (!atomic_inc_not_zero(&mm
->mm_users
))
781 spin_unlock(&mmlist_lock
);
790 else if (mm
== &init_mm
) {
792 shmem
= shmem_unuse(entry
, page
);
794 retval
= unuse_mm(mm
, entry
, page
);
795 if (set_start_mm
&& *swap_map
< swcount
) {
797 atomic_inc(&mm
->mm_users
);
801 spin_lock(&mmlist_lock
);
803 spin_unlock(&mmlist_lock
);
806 start_mm
= new_start_mm
;
810 page_cache_release(page
);
815 * How could swap count reach 0x7fff when the maximum
816 * pid is 0x7fff, and there's no way to repeat a swap
817 * page within an mm (except in shmem, where it's the
818 * shared object which takes the reference count)?
819 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
821 * If that's wrong, then we should worry more about
822 * exit_mmap() and do_munmap() cases described above:
823 * we might be resetting SWAP_MAP_MAX too early here.
824 * We know "Undead"s can happen, they're okay, so don't
825 * report them; but do report if we reset SWAP_MAP_MAX.
827 if (*swap_map
== SWAP_MAP_MAX
) {
828 spin_lock(&swap_lock
);
830 spin_unlock(&swap_lock
);
835 * If a reference remains (rare), we would like to leave
836 * the page in the swap cache; but try_to_unmap could
837 * then re-duplicate the entry once we drop page lock,
838 * so we might loop indefinitely; also, that page could
839 * not be swapped out to other storage meanwhile. So:
840 * delete from cache even if there's another reference,
841 * after ensuring that the data has been saved to disk -
842 * since if the reference remains (rarer), it will be
843 * read from disk into another page. Splitting into two
844 * pages would be incorrect if swap supported "shared
845 * private" pages, but they are handled by tmpfs files.
847 * Note shmem_unuse already deleted a swappage from
848 * the swap cache, unless the move to filepage failed:
849 * in which case it left swappage in cache, lowered its
850 * swap count to pass quickly through the loops above,
851 * and now we must reincrement count to try again later.
853 if ((*swap_map
> 1) && PageDirty(page
) && PageSwapCache(page
)) {
854 struct writeback_control wbc
= {
855 .sync_mode
= WB_SYNC_NONE
,
858 swap_writepage(page
, &wbc
);
860 wait_on_page_writeback(page
);
862 if (PageSwapCache(page
)) {
864 swap_duplicate(entry
);
866 delete_from_swap_cache(page
);
870 * So we could skip searching mms once swap count went
871 * to 1, we did not mark any present ptes as dirty: must
872 * mark page dirty so shrink_list will preserve it.
876 page_cache_release(page
);
879 * Make sure that we aren't completely killing
880 * interactive performance.
886 if (reset_overflow
) {
887 printk(KERN_WARNING
"swapoff: cleared swap entry overflow\n");
894 * After a successful try_to_unuse, if no swap is now in use, we know
895 * we can empty the mmlist. swap_lock must be held on entry and exit.
896 * Note that mmlist_lock nests inside swap_lock, and an mm must be
897 * added to the mmlist just after page_duplicate - before would be racy.
899 static void drain_mmlist(void)
901 struct list_head
*p
, *next
;
904 for (i
= 0; i
< nr_swapfiles
; i
++)
905 if (swap_info
[i
].inuse_pages
)
907 spin_lock(&mmlist_lock
);
908 list_for_each_safe(p
, next
, &init_mm
.mmlist
)
910 spin_unlock(&mmlist_lock
);
914 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
915 * corresponds to page offset `offset'.
917 sector_t
map_swap_page(struct swap_info_struct
*sis
, pgoff_t offset
)
919 struct swap_extent
*se
= sis
->curr_swap_extent
;
920 struct swap_extent
*start_se
= se
;
923 struct list_head
*lh
;
925 if (se
->start_page
<= offset
&&
926 offset
< (se
->start_page
+ se
->nr_pages
)) {
927 return se
->start_block
+ (offset
- se
->start_page
);
930 if (lh
== &sis
->extent_list
)
932 se
= list_entry(lh
, struct swap_extent
, list
);
933 sis
->curr_swap_extent
= se
;
934 BUG_ON(se
== start_se
); /* It *must* be present */
939 * Free all of a swapdev's extent information
941 static void destroy_swap_extents(struct swap_info_struct
*sis
)
943 while (!list_empty(&sis
->extent_list
)) {
944 struct swap_extent
*se
;
946 se
= list_entry(sis
->extent_list
.next
,
947 struct swap_extent
, list
);
954 * Add a block range (and the corresponding page range) into this swapdev's
955 * extent list. The extent list is kept sorted in page order.
957 * This function rather assumes that it is called in ascending page order.
960 add_swap_extent(struct swap_info_struct
*sis
, unsigned long start_page
,
961 unsigned long nr_pages
, sector_t start_block
)
963 struct swap_extent
*se
;
964 struct swap_extent
*new_se
;
965 struct list_head
*lh
;
967 lh
= sis
->extent_list
.prev
; /* The highest page extent */
968 if (lh
!= &sis
->extent_list
) {
969 se
= list_entry(lh
, struct swap_extent
, list
);
970 BUG_ON(se
->start_page
+ se
->nr_pages
!= start_page
);
971 if (se
->start_block
+ se
->nr_pages
== start_block
) {
973 se
->nr_pages
+= nr_pages
;
979 * No merge. Insert a new extent, preserving ordering.
981 new_se
= kmalloc(sizeof(*se
), GFP_KERNEL
);
984 new_se
->start_page
= start_page
;
985 new_se
->nr_pages
= nr_pages
;
986 new_se
->start_block
= start_block
;
988 list_add_tail(&new_se
->list
, &sis
->extent_list
);
993 * A `swap extent' is a simple thing which maps a contiguous range of pages
994 * onto a contiguous range of disk blocks. An ordered list of swap extents
995 * is built at swapon time and is then used at swap_writepage/swap_readpage
996 * time for locating where on disk a page belongs.
998 * If the swapfile is an S_ISBLK block device, a single extent is installed.
999 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
1000 * swap files identically.
1002 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
1003 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
1004 * swapfiles are handled *identically* after swapon time.
1006 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
1007 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
1008 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
1009 * requirements, they are simply tossed out - we will never use those blocks
1012 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
1013 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
1014 * which will scribble on the fs.
1016 * The amount of disk space which a single swap extent represents varies.
1017 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
1018 * extents in the list. To avoid much list walking, we cache the previous
1019 * search location in `curr_swap_extent', and start new searches from there.
1020 * This is extremely effective. The average number of iterations in
1021 * map_swap_page() has been measured at about 0.3 per page. - akpm.
1023 static int setup_swap_extents(struct swap_info_struct
*sis
, sector_t
*span
)
1025 struct inode
*inode
;
1026 unsigned blocks_per_page
;
1027 unsigned long page_no
;
1029 sector_t probe_block
;
1030 sector_t last_block
;
1031 sector_t lowest_block
= -1;
1032 sector_t highest_block
= 0;
1036 inode
= sis
->swap_file
->f_mapping
->host
;
1037 if (S_ISBLK(inode
->i_mode
)) {
1038 ret
= add_swap_extent(sis
, 0, sis
->max
, 0);
1043 blkbits
= inode
->i_blkbits
;
1044 blocks_per_page
= PAGE_SIZE
>> blkbits
;
1047 * Map all the blocks into the extent list. This code doesn't try
1052 last_block
= i_size_read(inode
) >> blkbits
;
1053 while ((probe_block
+ blocks_per_page
) <= last_block
&&
1054 page_no
< sis
->max
) {
1055 unsigned block_in_page
;
1056 sector_t first_block
;
1058 first_block
= bmap(inode
, probe_block
);
1059 if (first_block
== 0)
1063 * It must be PAGE_SIZE aligned on-disk
1065 if (first_block
& (blocks_per_page
- 1)) {
1070 for (block_in_page
= 1; block_in_page
< blocks_per_page
;
1074 block
= bmap(inode
, probe_block
+ block_in_page
);
1077 if (block
!= first_block
+ block_in_page
) {
1084 first_block
>>= (PAGE_SHIFT
- blkbits
);
1085 if (page_no
) { /* exclude the header page */
1086 if (first_block
< lowest_block
)
1087 lowest_block
= first_block
;
1088 if (first_block
> highest_block
)
1089 highest_block
= first_block
;
1093 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
1095 ret
= add_swap_extent(sis
, page_no
, 1, first_block
);
1100 probe_block
+= blocks_per_page
;
1105 *span
= 1 + highest_block
- lowest_block
;
1107 page_no
= 1; /* force Empty message */
1109 sis
->pages
= page_no
- 1;
1110 sis
->highest_bit
= page_no
- 1;
1112 sis
->curr_swap_extent
= list_entry(sis
->extent_list
.prev
,
1113 struct swap_extent
, list
);
1116 printk(KERN_ERR
"swapon: swapfile has holes\n");
1122 #if 0 /* We don't need this yet */
1123 #include <linux/backing-dev.h>
1124 int page_queue_congested(struct page
*page
)
1126 struct backing_dev_info
*bdi
;
1128 BUG_ON(!PageLocked(page
)); /* It pins the swap_info_struct */
1130 if (PageSwapCache(page
)) {
1131 swp_entry_t entry
= { .val
= page_private(page
) };
1132 struct swap_info_struct
*sis
;
1134 sis
= get_swap_info_struct(swp_type(entry
));
1135 bdi
= sis
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1137 bdi
= page
->mapping
->backing_dev_info
;
1138 return bdi_write_congested(bdi
);
1142 asmlinkage
long sys_swapoff(const char __user
* specialfile
)
1144 struct swap_info_struct
* p
= NULL
;
1145 unsigned short *swap_map
;
1146 struct file
*swap_file
, *victim
;
1147 struct address_space
*mapping
;
1148 struct inode
*inode
;
1153 if (!capable(CAP_SYS_ADMIN
))
1156 pathname
= getname(specialfile
);
1157 err
= PTR_ERR(pathname
);
1158 if (IS_ERR(pathname
))
1161 victim
= filp_open(pathname
, O_RDWR
|O_LARGEFILE
, 0);
1163 err
= PTR_ERR(victim
);
1167 mapping
= victim
->f_mapping
;
1169 spin_lock(&swap_lock
);
1170 for (type
= swap_list
.head
; type
>= 0; type
= swap_info
[type
].next
) {
1171 p
= swap_info
+ type
;
1172 if ((p
->flags
& SWP_ACTIVE
) == SWP_ACTIVE
) {
1173 if (p
->swap_file
->f_mapping
== mapping
)
1180 spin_unlock(&swap_lock
);
1183 if (!security_vm_enough_memory(p
->pages
))
1184 vm_unacct_memory(p
->pages
);
1187 spin_unlock(&swap_lock
);
1191 swap_list
.head
= p
->next
;
1193 swap_info
[prev
].next
= p
->next
;
1195 if (type
== swap_list
.next
) {
1196 /* just pick something that's safe... */
1197 swap_list
.next
= swap_list
.head
;
1199 nr_swap_pages
-= p
->pages
;
1200 total_swap_pages
-= p
->pages
;
1201 p
->flags
&= ~SWP_WRITEOK
;
1202 spin_unlock(&swap_lock
);
1204 current
->flags
|= PF_SWAPOFF
;
1205 err
= try_to_unuse(type
);
1206 current
->flags
&= ~PF_SWAPOFF
;
1209 /* re-insert swap space back into swap_list */
1210 spin_lock(&swap_lock
);
1211 for (prev
= -1, i
= swap_list
.head
; i
>= 0; prev
= i
, i
= swap_info
[i
].next
)
1212 if (p
->prio
>= swap_info
[i
].prio
)
1216 swap_list
.head
= swap_list
.next
= p
- swap_info
;
1218 swap_info
[prev
].next
= p
- swap_info
;
1219 nr_swap_pages
+= p
->pages
;
1220 total_swap_pages
+= p
->pages
;
1221 p
->flags
|= SWP_WRITEOK
;
1222 spin_unlock(&swap_lock
);
1226 /* wait for any unplug function to finish */
1227 down_write(&swap_unplug_sem
);
1228 up_write(&swap_unplug_sem
);
1230 destroy_swap_extents(p
);
1231 mutex_lock(&swapon_mutex
);
1232 spin_lock(&swap_lock
);
1235 /* wait for anyone still in scan_swap_map */
1236 p
->highest_bit
= 0; /* cuts scans short */
1237 while (p
->flags
>= SWP_SCANNING
) {
1238 spin_unlock(&swap_lock
);
1239 schedule_timeout_uninterruptible(1);
1240 spin_lock(&swap_lock
);
1243 swap_file
= p
->swap_file
;
1244 p
->swap_file
= NULL
;
1246 swap_map
= p
->swap_map
;
1249 spin_unlock(&swap_lock
);
1250 mutex_unlock(&swapon_mutex
);
1252 inode
= mapping
->host
;
1253 if (S_ISBLK(inode
->i_mode
)) {
1254 struct block_device
*bdev
= I_BDEV(inode
);
1255 set_blocksize(bdev
, p
->old_block_size
);
1258 mutex_lock(&inode
->i_mutex
);
1259 inode
->i_flags
&= ~S_SWAPFILE
;
1260 mutex_unlock(&inode
->i_mutex
);
1262 filp_close(swap_file
, NULL
);
1266 filp_close(victim
, NULL
);
1271 #ifdef CONFIG_PROC_FS
1273 static void *swap_start(struct seq_file
*swap
, loff_t
*pos
)
1275 struct swap_info_struct
*ptr
= swap_info
;
1279 mutex_lock(&swapon_mutex
);
1281 for (i
= 0; i
< nr_swapfiles
; i
++, ptr
++) {
1282 if (!(ptr
->flags
& SWP_USED
) || !ptr
->swap_map
)
1291 static void *swap_next(struct seq_file
*swap
, void *v
, loff_t
*pos
)
1293 struct swap_info_struct
*ptr
= v
;
1294 struct swap_info_struct
*endptr
= swap_info
+ nr_swapfiles
;
1296 for (++ptr
; ptr
< endptr
; ptr
++) {
1297 if (!(ptr
->flags
& SWP_USED
) || !ptr
->swap_map
)
1306 static void swap_stop(struct seq_file
*swap
, void *v
)
1308 mutex_unlock(&swapon_mutex
);
1311 static int swap_show(struct seq_file
*swap
, void *v
)
1313 struct swap_info_struct
*ptr
= v
;
1318 seq_puts(swap
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1320 file
= ptr
->swap_file
;
1321 len
= seq_path(swap
, file
->f_vfsmnt
, file
->f_dentry
, " \t\n\\");
1322 seq_printf(swap
, "%*s%s\t%u\t%u\t%d\n",
1323 len
< 40 ? 40 - len
: 1, " ",
1324 S_ISBLK(file
->f_dentry
->d_inode
->i_mode
) ?
1325 "partition" : "file\t",
1326 ptr
->pages
<< (PAGE_SHIFT
- 10),
1327 ptr
->inuse_pages
<< (PAGE_SHIFT
- 10),
1332 static struct seq_operations swaps_op
= {
1333 .start
= swap_start
,
1339 static int swaps_open(struct inode
*inode
, struct file
*file
)
1341 return seq_open(file
, &swaps_op
);
1344 static struct file_operations proc_swaps_operations
= {
1347 .llseek
= seq_lseek
,
1348 .release
= seq_release
,
1351 static int __init
procswaps_init(void)
1353 struct proc_dir_entry
*entry
;
1355 entry
= create_proc_entry("swaps", 0, NULL
);
1357 entry
->proc_fops
= &proc_swaps_operations
;
1360 __initcall(procswaps_init
);
1361 #endif /* CONFIG_PROC_FS */
1364 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1366 * The swapon system call
1368 asmlinkage
long sys_swapon(const char __user
* specialfile
, int swap_flags
)
1370 struct swap_info_struct
* p
;
1372 struct block_device
*bdev
= NULL
;
1373 struct file
*swap_file
= NULL
;
1374 struct address_space
*mapping
;
1378 static int least_priority
;
1379 union swap_header
*swap_header
= NULL
;
1380 int swap_header_version
;
1381 unsigned int nr_good_pages
= 0;
1384 unsigned long maxpages
= 1;
1386 unsigned short *swap_map
;
1387 struct page
*page
= NULL
;
1388 struct inode
*inode
= NULL
;
1391 if (!capable(CAP_SYS_ADMIN
))
1393 spin_lock(&swap_lock
);
1395 for (type
= 0 ; type
< nr_swapfiles
; type
++,p
++)
1396 if (!(p
->flags
& SWP_USED
))
1399 if (type
>= MAX_SWAPFILES
) {
1400 spin_unlock(&swap_lock
);
1403 if (type
>= nr_swapfiles
)
1404 nr_swapfiles
= type
+1;
1405 INIT_LIST_HEAD(&p
->extent_list
);
1406 p
->flags
= SWP_USED
;
1407 p
->swap_file
= NULL
;
1408 p
->old_block_size
= 0;
1415 if (swap_flags
& SWAP_FLAG_PREFER
) {
1417 (swap_flags
& SWAP_FLAG_PRIO_MASK
)>>SWAP_FLAG_PRIO_SHIFT
;
1419 p
->prio
= --least_priority
;
1421 spin_unlock(&swap_lock
);
1422 name
= getname(specialfile
);
1423 error
= PTR_ERR(name
);
1428 swap_file
= filp_open(name
, O_RDWR
|O_LARGEFILE
, 0);
1429 error
= PTR_ERR(swap_file
);
1430 if (IS_ERR(swap_file
)) {
1435 p
->swap_file
= swap_file
;
1436 mapping
= swap_file
->f_mapping
;
1437 inode
= mapping
->host
;
1440 for (i
= 0; i
< nr_swapfiles
; i
++) {
1441 struct swap_info_struct
*q
= &swap_info
[i
];
1443 if (i
== type
|| !q
->swap_file
)
1445 if (mapping
== q
->swap_file
->f_mapping
)
1450 if (S_ISBLK(inode
->i_mode
)) {
1451 bdev
= I_BDEV(inode
);
1452 error
= bd_claim(bdev
, sys_swapon
);
1458 p
->old_block_size
= block_size(bdev
);
1459 error
= set_blocksize(bdev
, PAGE_SIZE
);
1463 } else if (S_ISREG(inode
->i_mode
)) {
1464 p
->bdev
= inode
->i_sb
->s_bdev
;
1465 mutex_lock(&inode
->i_mutex
);
1467 if (IS_SWAPFILE(inode
)) {
1475 swapfilesize
= i_size_read(inode
) >> PAGE_SHIFT
;
1478 * Read the swap header.
1480 if (!mapping
->a_ops
->readpage
) {
1484 page
= read_mapping_page(mapping
, 0, swap_file
);
1486 error
= PTR_ERR(page
);
1489 wait_on_page_locked(page
);
1490 if (!PageUptodate(page
))
1493 swap_header
= page_address(page
);
1495 if (!memcmp("SWAP-SPACE",swap_header
->magic
.magic
,10))
1496 swap_header_version
= 1;
1497 else if (!memcmp("SWAPSPACE2",swap_header
->magic
.magic
,10))
1498 swap_header_version
= 2;
1500 printk(KERN_ERR
"Unable to find swap-space signature\n");
1505 switch (swap_header_version
) {
1507 printk(KERN_ERR
"version 0 swap is no longer supported. "
1508 "Use mkswap -v1 %s\n", name
);
1512 /* Check the swap header's sub-version and the size of
1513 the swap file and bad block lists */
1514 if (swap_header
->info
.version
!= 1) {
1516 "Unable to handle swap header version %d\n",
1517 swap_header
->info
.version
);
1523 p
->cluster_next
= 1;
1526 * Find out how many pages are allowed for a single swap
1527 * device. There are two limiting factors: 1) the number of
1528 * bits for the swap offset in the swp_entry_t type and
1529 * 2) the number of bits in the a swap pte as defined by
1530 * the different architectures. In order to find the
1531 * largest possible bit mask a swap entry with swap type 0
1532 * and swap offset ~0UL is created, encoded to a swap pte,
1533 * decoded to a swp_entry_t again and finally the swap
1534 * offset is extracted. This will mask all the bits from
1535 * the initial ~0UL mask that can't be encoded in either
1536 * the swp_entry_t or the architecture definition of a
1539 maxpages
= swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1540 if (maxpages
> swap_header
->info
.last_page
)
1541 maxpages
= swap_header
->info
.last_page
;
1542 p
->highest_bit
= maxpages
- 1;
1547 if (swap_header
->info
.nr_badpages
&& S_ISREG(inode
->i_mode
))
1549 if (swap_header
->info
.nr_badpages
> MAX_SWAP_BADPAGES
)
1552 /* OK, set up the swap map and apply the bad block list */
1553 if (!(p
->swap_map
= vmalloc(maxpages
* sizeof(short)))) {
1559 memset(p
->swap_map
, 0, maxpages
* sizeof(short));
1560 for (i
= 0; i
< swap_header
->info
.nr_badpages
; i
++) {
1561 int page_nr
= swap_header
->info
.badpages
[i
];
1562 if (page_nr
<= 0 || page_nr
>= swap_header
->info
.last_page
)
1565 p
->swap_map
[page_nr
] = SWAP_MAP_BAD
;
1567 nr_good_pages
= swap_header
->info
.last_page
-
1568 swap_header
->info
.nr_badpages
-
1569 1 /* header page */;
1574 if (swapfilesize
&& maxpages
> swapfilesize
) {
1576 "Swap area shorter than signature indicates\n");
1580 if (nr_good_pages
) {
1581 p
->swap_map
[0] = SWAP_MAP_BAD
;
1583 p
->pages
= nr_good_pages
;
1584 nr_extents
= setup_swap_extents(p
, &span
);
1585 if (nr_extents
< 0) {
1589 nr_good_pages
= p
->pages
;
1591 if (!nr_good_pages
) {
1592 printk(KERN_WARNING
"Empty swap-file\n");
1597 mutex_lock(&swapon_mutex
);
1598 spin_lock(&swap_lock
);
1599 p
->flags
= SWP_ACTIVE
;
1600 nr_swap_pages
+= nr_good_pages
;
1601 total_swap_pages
+= nr_good_pages
;
1603 printk(KERN_INFO
"Adding %uk swap on %s. "
1604 "Priority:%d extents:%d across:%lluk\n",
1605 nr_good_pages
<<(PAGE_SHIFT
-10), name
, p
->prio
,
1606 nr_extents
, (unsigned long long)span
<<(PAGE_SHIFT
-10));
1608 /* insert swap space into swap_list: */
1610 for (i
= swap_list
.head
; i
>= 0; i
= swap_info
[i
].next
) {
1611 if (p
->prio
>= swap_info
[i
].prio
) {
1618 swap_list
.head
= swap_list
.next
= p
- swap_info
;
1620 swap_info
[prev
].next
= p
- swap_info
;
1622 spin_unlock(&swap_lock
);
1623 mutex_unlock(&swapon_mutex
);
1628 set_blocksize(bdev
, p
->old_block_size
);
1631 destroy_swap_extents(p
);
1633 spin_lock(&swap_lock
);
1634 swap_map
= p
->swap_map
;
1635 p
->swap_file
= NULL
;
1638 if (!(swap_flags
& SWAP_FLAG_PREFER
))
1640 spin_unlock(&swap_lock
);
1643 filp_close(swap_file
, NULL
);
1645 if (page
&& !IS_ERR(page
)) {
1647 page_cache_release(page
);
1653 inode
->i_flags
|= S_SWAPFILE
;
1654 mutex_unlock(&inode
->i_mutex
);
1659 void si_swapinfo(struct sysinfo
*val
)
1662 unsigned long nr_to_be_unused
= 0;
1664 spin_lock(&swap_lock
);
1665 for (i
= 0; i
< nr_swapfiles
; i
++) {
1666 if (!(swap_info
[i
].flags
& SWP_USED
) ||
1667 (swap_info
[i
].flags
& SWP_WRITEOK
))
1669 nr_to_be_unused
+= swap_info
[i
].inuse_pages
;
1671 val
->freeswap
= nr_swap_pages
+ nr_to_be_unused
;
1672 val
->totalswap
= total_swap_pages
+ nr_to_be_unused
;
1673 spin_unlock(&swap_lock
);
1677 * Verify that a swap entry is valid and increment its swap map count.
1679 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1680 * "permanent", but will be reclaimed by the next swapoff.
1682 int swap_duplicate(swp_entry_t entry
)
1684 struct swap_info_struct
* p
;
1685 unsigned long offset
, type
;
1688 if (is_migration_entry(entry
))
1691 type
= swp_type(entry
);
1692 if (type
>= nr_swapfiles
)
1694 p
= type
+ swap_info
;
1695 offset
= swp_offset(entry
);
1697 spin_lock(&swap_lock
);
1698 if (offset
< p
->max
&& p
->swap_map
[offset
]) {
1699 if (p
->swap_map
[offset
] < SWAP_MAP_MAX
- 1) {
1700 p
->swap_map
[offset
]++;
1702 } else if (p
->swap_map
[offset
] <= SWAP_MAP_MAX
) {
1703 if (swap_overflow
++ < 5)
1704 printk(KERN_WARNING
"swap_dup: swap entry overflow\n");
1705 p
->swap_map
[offset
] = SWAP_MAP_MAX
;
1709 spin_unlock(&swap_lock
);
1714 printk(KERN_ERR
"swap_dup: %s%08lx\n", Bad_file
, entry
.val
);
1718 struct swap_info_struct
*
1719 get_swap_info_struct(unsigned type
)
1721 return &swap_info
[type
];
1725 * swap_lock prevents swap_map being freed. Don't grab an extra
1726 * reference on the swaphandle, it doesn't matter if it becomes unused.
1728 int valid_swaphandles(swp_entry_t entry
, unsigned long *offset
)
1730 int our_page_cluster
= page_cluster
;
1731 int ret
= 0, i
= 1 << our_page_cluster
;
1733 struct swap_info_struct
*swapdev
= swp_type(entry
) + swap_info
;
1735 if (!our_page_cluster
) /* no readahead */
1737 toff
= (swp_offset(entry
) >> our_page_cluster
) << our_page_cluster
;
1738 if (!toff
) /* first page is swap header */
1742 spin_lock(&swap_lock
);
1744 /* Don't read-ahead past the end of the swap area */
1745 if (toff
>= swapdev
->max
)
1747 /* Don't read in free or bad pages */
1748 if (!swapdev
->swap_map
[toff
])
1750 if (swapdev
->swap_map
[toff
] == SWAP_MAP_BAD
)
1755 spin_unlock(&swap_lock
);