4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
8 #include <linux/config.h>
10 #include <linux/hugetlb.h>
11 #include <linux/mman.h>
12 #include <linux/slab.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/swap.h>
15 #include <linux/vmalloc.h>
16 #include <linux/pagemap.h>
17 #include <linux/namei.h>
18 #include <linux/shm.h>
19 #include <linux/blkdev.h>
20 #include <linux/writeback.h>
21 #include <linux/proc_fs.h>
22 #include <linux/seq_file.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/rmap.h>
26 #include <linux/security.h>
27 #include <linux/backing-dev.h>
28 #include <linux/syscalls.h>
30 #include <asm/pgtable.h>
31 #include <asm/tlbflush.h>
32 #include <linux/swapops.h>
34 DEFINE_SPINLOCK(swaplock
);
35 unsigned int nr_swapfiles
;
36 long total_swap_pages
;
37 static int swap_overflow
;
39 EXPORT_SYMBOL(total_swap_pages
);
41 static const char Bad_file
[] = "Bad swap file entry ";
42 static const char Unused_file
[] = "Unused swap file entry ";
43 static const char Bad_offset
[] = "Bad swap offset entry ";
44 static const char Unused_offset
[] = "Unused swap offset entry ";
46 struct swap_list_t swap_list
= {-1, -1};
48 struct swap_info_struct swap_info
[MAX_SWAPFILES
];
50 static DECLARE_MUTEX(swapon_sem
);
53 * We need this because the bdev->unplug_fn can sleep and we cannot
54 * hold swap_list_lock while calling the unplug_fn. And swap_list_lock
55 * cannot be turned into a semaphore.
57 static DECLARE_RWSEM(swap_unplug_sem
);
59 #define SWAPFILE_CLUSTER 256
61 void swap_unplug_io_fn(struct backing_dev_info
*unused_bdi
, struct page
*page
)
65 down_read(&swap_unplug_sem
);
66 entry
.val
= page
->private;
67 if (PageSwapCache(page
)) {
68 struct block_device
*bdev
= swap_info
[swp_type(entry
)].bdev
;
69 struct backing_dev_info
*bdi
;
72 * If the page is removed from swapcache from under us (with a
73 * racy try_to_unuse/swapoff) we need an additional reference
74 * count to avoid reading garbage from page->private above. If
75 * the WARN_ON triggers during a swapoff it maybe the race
76 * condition and it's harmless. However if it triggers without
77 * swapoff it signals a problem.
79 WARN_ON(page_count(page
) <= 1);
81 bdi
= bdev
->bd_inode
->i_mapping
->backing_dev_info
;
82 blk_run_backing_dev(bdi
, page
);
84 up_read(&swap_unplug_sem
);
87 static inline unsigned long scan_swap_map(struct swap_info_struct
*si
)
91 * We try to cluster swap pages by allocating them
92 * sequentially in swap. Once we've allocated
93 * SWAPFILE_CLUSTER pages this way, however, we resort to
94 * first-free allocation, starting a new cluster. This
95 * prevents us from scattering swap pages all over the entire
96 * swap partition, so that we reduce overall disk seek times
97 * between swap pages. -- sct */
99 while (si
->cluster_next
<= si
->highest_bit
) {
100 offset
= si
->cluster_next
++;
101 if (si
->swap_map
[offset
])
107 si
->cluster_nr
= SWAPFILE_CLUSTER
;
109 /* try to find an empty (even not aligned) cluster. */
110 offset
= si
->lowest_bit
;
112 if (offset
+SWAPFILE_CLUSTER
-1 <= si
->highest_bit
)
115 for (nr
= offset
; nr
< offset
+SWAPFILE_CLUSTER
; nr
++)
116 if (si
->swap_map
[nr
])
119 goto check_next_cluster
;
121 /* We found a completly empty cluster, so start
126 /* No luck, so now go finegrined as usual. -Andrea */
127 for (offset
= si
->lowest_bit
; offset
<= si
->highest_bit
; offset
++) {
128 if (si
->swap_map
[offset
])
130 si
->lowest_bit
= offset
+1;
132 if (offset
== si
->lowest_bit
)
134 if (offset
== si
->highest_bit
)
136 if (si
->lowest_bit
> si
->highest_bit
) {
137 si
->lowest_bit
= si
->max
;
140 si
->swap_map
[offset
] = 1;
142 si
->cluster_next
= offset
+1;
145 si
->lowest_bit
= si
->max
;
150 swp_entry_t
get_swap_page(void)
152 struct swap_info_struct
*si
;
158 if (nr_swap_pages
<= 0)
162 for (type
= swap_list
.next
; type
>= 0 && wrapped
< 2; type
= next
) {
163 si
= swap_info
+ type
;
166 (!wrapped
&& si
->prio
!= swap_info
[next
].prio
)) {
167 next
= swap_list
.head
;
171 if (!si
->highest_bit
)
173 if (!(si
->flags
& SWP_WRITEOK
))
176 swap_list
.next
= next
;
177 swap_device_lock(si
);
179 offset
= scan_swap_map(si
);
180 swap_device_unlock(si
);
182 return swp_entry(type
, offset
);
184 next
= swap_list
.next
;
190 return (swp_entry_t
) {0};
193 static struct swap_info_struct
* swap_info_get(swp_entry_t entry
)
195 struct swap_info_struct
* p
;
196 unsigned long offset
, type
;
200 type
= swp_type(entry
);
201 if (type
>= nr_swapfiles
)
203 p
= & swap_info
[type
];
204 if (!(p
->flags
& SWP_USED
))
206 offset
= swp_offset(entry
);
207 if (offset
>= p
->max
)
209 if (!p
->swap_map
[offset
])
216 printk(KERN_ERR
"swap_free: %s%08lx\n", Unused_offset
, entry
.val
);
219 printk(KERN_ERR
"swap_free: %s%08lx\n", Bad_offset
, entry
.val
);
222 printk(KERN_ERR
"swap_free: %s%08lx\n", Unused_file
, entry
.val
);
225 printk(KERN_ERR
"swap_free: %s%08lx\n", Bad_file
, entry
.val
);
230 static void swap_info_put(struct swap_info_struct
* p
)
232 swap_device_unlock(p
);
236 static int swap_entry_free(struct swap_info_struct
*p
, unsigned long offset
)
238 int count
= p
->swap_map
[offset
];
240 if (count
< SWAP_MAP_MAX
) {
242 p
->swap_map
[offset
] = count
;
244 if (offset
< p
->lowest_bit
)
245 p
->lowest_bit
= offset
;
246 if (offset
> p
->highest_bit
)
247 p
->highest_bit
= offset
;
248 if (p
->prio
> swap_info
[swap_list
.next
].prio
)
249 swap_list
.next
= p
- swap_info
;
258 * Caller has made sure that the swapdevice corresponding to entry
259 * is still around or has not been recycled.
261 void swap_free(swp_entry_t entry
)
263 struct swap_info_struct
* p
;
265 p
= swap_info_get(entry
);
267 swap_entry_free(p
, swp_offset(entry
));
273 * How many references to page are currently swapped out?
275 static inline int page_swapcount(struct page
*page
)
278 struct swap_info_struct
*p
;
281 entry
.val
= page
->private;
282 p
= swap_info_get(entry
);
284 /* Subtract the 1 for the swap cache itself */
285 count
= p
->swap_map
[swp_offset(entry
)] - 1;
292 * We can use this swap cache entry directly
293 * if there are no other references to it.
295 int can_share_swap_page(struct page
*page
)
299 BUG_ON(!PageLocked(page
));
300 count
= page_mapcount(page
);
301 if (count
<= 1 && PageSwapCache(page
))
302 count
+= page_swapcount(page
);
307 * Work out if there are any other processes sharing this
308 * swap cache page. Free it if you can. Return success.
310 int remove_exclusive_swap_page(struct page
*page
)
313 struct swap_info_struct
* p
;
316 BUG_ON(PagePrivate(page
));
317 BUG_ON(!PageLocked(page
));
319 if (!PageSwapCache(page
))
321 if (PageWriteback(page
))
323 if (page_count(page
) != 2) /* 2: us + cache */
326 entry
.val
= page
->private;
327 p
= swap_info_get(entry
);
331 /* Is the only swap cache user the cache itself? */
333 if (p
->swap_map
[swp_offset(entry
)] == 1) {
334 /* Recheck the page count with the swapcache lock held.. */
335 write_lock_irq(&swapper_space
.tree_lock
);
336 if ((page_count(page
) == 2) && !PageWriteback(page
)) {
337 __delete_from_swap_cache(page
);
341 write_unlock_irq(&swapper_space
.tree_lock
);
347 page_cache_release(page
);
354 * Free the swap entry like above, but also try to
355 * free the page cache entry if it is the last user.
357 void free_swap_and_cache(swp_entry_t entry
)
359 struct swap_info_struct
* p
;
360 struct page
*page
= NULL
;
362 p
= swap_info_get(entry
);
364 if (swap_entry_free(p
, swp_offset(entry
)) == 1)
365 page
= find_trylock_page(&swapper_space
, entry
.val
);
371 BUG_ON(PagePrivate(page
));
372 page_cache_get(page
);
373 one_user
= (page_count(page
) == 2);
374 /* Only cache user (+us), or swap space full? Free it! */
375 if (!PageWriteback(page
) && (one_user
|| vm_swap_full())) {
376 delete_from_swap_cache(page
);
380 page_cache_release(page
);
385 * Always set the resulting pte to be nowrite (the same as COW pages
386 * after one process has exited). We don't know just how many PTEs will
387 * share this swap entry, so be cautious and let do_wp_page work out
388 * what to do if a write is requested later.
390 * vma->vm_mm->page_table_lock is held.
392 static void unuse_pte(struct vm_area_struct
*vma
, pte_t
*pte
,
393 unsigned long addr
, swp_entry_t entry
, struct page
*page
)
395 inc_mm_counter(vma
->vm_mm
, rss
);
397 set_pte_at(vma
->vm_mm
, addr
, pte
,
398 pte_mkold(mk_pte(page
, vma
->vm_page_prot
)));
399 page_add_anon_rmap(page
, vma
, addr
);
402 * Move the page to the active list so it is not
403 * immediately swapped out again after swapon.
408 static int unuse_pte_range(struct vm_area_struct
*vma
, pmd_t
*pmd
,
409 unsigned long addr
, unsigned long end
,
410 swp_entry_t entry
, struct page
*page
)
413 pte_t swp_pte
= swp_entry_to_pte(entry
);
415 pte
= pte_offset_map(pmd
, addr
);
418 * swapoff spends a _lot_ of time in this loop!
419 * Test inline before going to call unuse_pte.
421 if (unlikely(pte_same(*pte
, swp_pte
))) {
422 unuse_pte(vma
, pte
, addr
, entry
, page
);
426 } while (pte
++, addr
+= PAGE_SIZE
, addr
!= end
);
431 static inline int unuse_pmd_range(struct vm_area_struct
*vma
, pud_t
*pud
,
432 unsigned long addr
, unsigned long end
,
433 swp_entry_t entry
, struct page
*page
)
438 pmd
= pmd_offset(pud
, addr
);
440 next
= pmd_addr_end(addr
, end
);
441 if (pmd_none_or_clear_bad(pmd
))
443 if (unuse_pte_range(vma
, pmd
, addr
, next
, entry
, page
))
445 } while (pmd
++, addr
= next
, addr
!= end
);
449 static inline int unuse_pud_range(struct vm_area_struct
*vma
, pgd_t
*pgd
,
450 unsigned long addr
, unsigned long end
,
451 swp_entry_t entry
, struct page
*page
)
456 pud
= pud_offset(pgd
, addr
);
458 next
= pud_addr_end(addr
, end
);
459 if (pud_none_or_clear_bad(pud
))
461 if (unuse_pmd_range(vma
, pud
, addr
, next
, entry
, page
))
463 } while (pud
++, addr
= next
, addr
!= end
);
467 static int unuse_vma(struct vm_area_struct
*vma
,
468 swp_entry_t entry
, struct page
*page
)
471 unsigned long addr
, end
, next
;
474 addr
= page_address_in_vma(page
, vma
);
478 end
= addr
+ PAGE_SIZE
;
480 addr
= vma
->vm_start
;
484 pgd
= pgd_offset(vma
->vm_mm
, addr
);
486 next
= pgd_addr_end(addr
, end
);
487 if (pgd_none_or_clear_bad(pgd
))
489 if (unuse_pud_range(vma
, pgd
, addr
, next
, entry
, page
))
491 } while (pgd
++, addr
= next
, addr
!= end
);
495 static int unuse_mm(struct mm_struct
*mm
,
496 swp_entry_t entry
, struct page
*page
)
498 struct vm_area_struct
*vma
;
500 if (!down_read_trylock(&mm
->mmap_sem
)) {
502 * Activate page so shrink_cache is unlikely to unmap its
503 * ptes while lock is dropped, so swapoff can make progress.
507 down_read(&mm
->mmap_sem
);
510 spin_lock(&mm
->page_table_lock
);
511 for (vma
= mm
->mmap
; vma
; vma
= vma
->vm_next
) {
512 if (vma
->anon_vma
&& unuse_vma(vma
, entry
, page
))
515 spin_unlock(&mm
->page_table_lock
);
516 up_read(&mm
->mmap_sem
);
518 * Currently unuse_mm cannot fail, but leave error handling
519 * at call sites for now, since we change it from time to time.
525 * Scan swap_map from current position to next entry still in use.
526 * Recycle to start on reaching the end, returning 0 when empty.
528 static unsigned int find_next_to_unuse(struct swap_info_struct
*si
,
531 unsigned int max
= si
->max
;
532 unsigned int i
= prev
;
536 * No need for swap_device_lock(si) here: we're just looking
537 * for whether an entry is in use, not modifying it; false
538 * hits are okay, and sys_swapoff() has already prevented new
539 * allocations from this area (while holding swap_list_lock()).
548 * No entries in use at top of swap_map,
549 * loop back to start and recheck there.
555 count
= si
->swap_map
[i
];
556 if (count
&& count
!= SWAP_MAP_BAD
)
563 * We completely avoid races by reading each swap page in advance,
564 * and then search for the process using it. All the necessary
565 * page table adjustments can then be made atomically.
567 static int try_to_unuse(unsigned int type
)
569 struct swap_info_struct
* si
= &swap_info
[type
];
570 struct mm_struct
*start_mm
;
571 unsigned short *swap_map
;
572 unsigned short swcount
;
577 int reset_overflow
= 0;
581 * When searching mms for an entry, a good strategy is to
582 * start at the first mm we freed the previous entry from
583 * (though actually we don't notice whether we or coincidence
584 * freed the entry). Initialize this start_mm with a hold.
586 * A simpler strategy would be to start at the last mm we
587 * freed the previous entry from; but that would take less
588 * advantage of mmlist ordering, which clusters forked mms
589 * together, child after parent. If we race with dup_mmap(), we
590 * prefer to resolve parent before child, lest we miss entries
591 * duplicated after we scanned child: using last mm would invert
592 * that. Though it's only a serious concern when an overflowed
593 * swap count is reset from SWAP_MAP_MAX, preventing a rescan.
596 atomic_inc(&init_mm
.mm_users
);
599 * Keep on scanning until all entries have gone. Usually,
600 * one pass through swap_map is enough, but not necessarily:
601 * there are races when an instance of an entry might be missed.
603 while ((i
= find_next_to_unuse(si
, i
)) != 0) {
604 if (signal_pending(current
)) {
610 * Get a page for the entry, using the existing swap
611 * cache page if there is one. Otherwise, get a clean
612 * page and read the swap into it.
614 swap_map
= &si
->swap_map
[i
];
615 entry
= swp_entry(type
, i
);
616 page
= read_swap_cache_async(entry
, NULL
, 0);
619 * Either swap_duplicate() failed because entry
620 * has been freed independently, and will not be
621 * reused since sys_swapoff() already disabled
622 * allocation from here, or alloc_page() failed.
631 * Don't hold on to start_mm if it looks like exiting.
633 if (atomic_read(&start_mm
->mm_users
) == 1) {
636 atomic_inc(&init_mm
.mm_users
);
640 * Wait for and lock page. When do_swap_page races with
641 * try_to_unuse, do_swap_page can handle the fault much
642 * faster than try_to_unuse can locate the entry. This
643 * apparently redundant "wait_on_page_locked" lets try_to_unuse
644 * defer to do_swap_page in such a case - in some tests,
645 * do_swap_page and try_to_unuse repeatedly compete.
647 wait_on_page_locked(page
);
648 wait_on_page_writeback(page
);
650 wait_on_page_writeback(page
);
653 * Remove all references to entry.
654 * Whenever we reach init_mm, there's no address space
655 * to search, but use it as a reminder to search shmem.
660 if (start_mm
== &init_mm
)
661 shmem
= shmem_unuse(entry
, page
);
663 retval
= unuse_mm(start_mm
, entry
, page
);
666 int set_start_mm
= (*swap_map
>= swcount
);
667 struct list_head
*p
= &start_mm
->mmlist
;
668 struct mm_struct
*new_start_mm
= start_mm
;
669 struct mm_struct
*prev_mm
= start_mm
;
670 struct mm_struct
*mm
;
672 atomic_inc(&new_start_mm
->mm_users
);
673 atomic_inc(&prev_mm
->mm_users
);
674 spin_lock(&mmlist_lock
);
675 while (*swap_map
> 1 && !retval
&&
676 (p
= p
->next
) != &start_mm
->mmlist
) {
677 mm
= list_entry(p
, struct mm_struct
, mmlist
);
678 if (atomic_inc_return(&mm
->mm_users
) == 1) {
679 atomic_dec(&mm
->mm_users
);
682 spin_unlock(&mmlist_lock
);
691 else if (mm
== &init_mm
) {
693 shmem
= shmem_unuse(entry
, page
);
695 retval
= unuse_mm(mm
, entry
, page
);
696 if (set_start_mm
&& *swap_map
< swcount
) {
698 atomic_inc(&mm
->mm_users
);
702 spin_lock(&mmlist_lock
);
704 spin_unlock(&mmlist_lock
);
707 start_mm
= new_start_mm
;
711 page_cache_release(page
);
716 * How could swap count reach 0x7fff when the maximum
717 * pid is 0x7fff, and there's no way to repeat a swap
718 * page within an mm (except in shmem, where it's the
719 * shared object which takes the reference count)?
720 * We believe SWAP_MAP_MAX cannot occur in Linux 2.4.
722 * If that's wrong, then we should worry more about
723 * exit_mmap() and do_munmap() cases described above:
724 * we might be resetting SWAP_MAP_MAX too early here.
725 * We know "Undead"s can happen, they're okay, so don't
726 * report them; but do report if we reset SWAP_MAP_MAX.
728 if (*swap_map
== SWAP_MAP_MAX
) {
729 swap_device_lock(si
);
731 swap_device_unlock(si
);
736 * If a reference remains (rare), we would like to leave
737 * the page in the swap cache; but try_to_unmap could
738 * then re-duplicate the entry once we drop page lock,
739 * so we might loop indefinitely; also, that page could
740 * not be swapped out to other storage meanwhile. So:
741 * delete from cache even if there's another reference,
742 * after ensuring that the data has been saved to disk -
743 * since if the reference remains (rarer), it will be
744 * read from disk into another page. Splitting into two
745 * pages would be incorrect if swap supported "shared
746 * private" pages, but they are handled by tmpfs files.
748 * Note shmem_unuse already deleted a swappage from
749 * the swap cache, unless the move to filepage failed:
750 * in which case it left swappage in cache, lowered its
751 * swap count to pass quickly through the loops above,
752 * and now we must reincrement count to try again later.
754 if ((*swap_map
> 1) && PageDirty(page
) && PageSwapCache(page
)) {
755 struct writeback_control wbc
= {
756 .sync_mode
= WB_SYNC_NONE
,
759 swap_writepage(page
, &wbc
);
761 wait_on_page_writeback(page
);
763 if (PageSwapCache(page
)) {
765 swap_duplicate(entry
);
767 delete_from_swap_cache(page
);
771 * So we could skip searching mms once swap count went
772 * to 1, we did not mark any present ptes as dirty: must
773 * mark page dirty so shrink_list will preserve it.
777 page_cache_release(page
);
780 * Make sure that we aren't completely killing
781 * interactive performance.
787 if (reset_overflow
) {
788 printk(KERN_WARNING
"swapoff: cleared swap entry overflow\n");
795 * After a successful try_to_unuse, if no swap is now in use, we know we
796 * can empty the mmlist. swap_list_lock must be held on entry and exit.
797 * Note that mmlist_lock nests inside swap_list_lock, and an mm must be
798 * added to the mmlist just after page_duplicate - before would be racy.
800 static void drain_mmlist(void)
802 struct list_head
*p
, *next
;
805 for (i
= 0; i
< nr_swapfiles
; i
++)
806 if (swap_info
[i
].inuse_pages
)
808 spin_lock(&mmlist_lock
);
809 list_for_each_safe(p
, next
, &init_mm
.mmlist
)
811 spin_unlock(&mmlist_lock
);
815 * Use this swapdev's extent info to locate the (PAGE_SIZE) block which
816 * corresponds to page offset `offset'.
818 sector_t
map_swap_page(struct swap_info_struct
*sis
, pgoff_t offset
)
820 struct swap_extent
*se
= sis
->curr_swap_extent
;
821 struct swap_extent
*start_se
= se
;
824 struct list_head
*lh
;
826 if (se
->start_page
<= offset
&&
827 offset
< (se
->start_page
+ se
->nr_pages
)) {
828 return se
->start_block
+ (offset
- se
->start_page
);
831 if (lh
== &sis
->extent_list
)
833 se
= list_entry(lh
, struct swap_extent
, list
);
834 sis
->curr_swap_extent
= se
;
835 BUG_ON(se
== start_se
); /* It *must* be present */
840 * Free all of a swapdev's extent information
842 static void destroy_swap_extents(struct swap_info_struct
*sis
)
844 while (!list_empty(&sis
->extent_list
)) {
845 struct swap_extent
*se
;
847 se
= list_entry(sis
->extent_list
.next
,
848 struct swap_extent
, list
);
855 * Add a block range (and the corresponding page range) into this swapdev's
856 * extent list. The extent list is kept sorted in page order.
858 * This function rather assumes that it is called in ascending page order.
861 add_swap_extent(struct swap_info_struct
*sis
, unsigned long start_page
,
862 unsigned long nr_pages
, sector_t start_block
)
864 struct swap_extent
*se
;
865 struct swap_extent
*new_se
;
866 struct list_head
*lh
;
868 lh
= sis
->extent_list
.prev
; /* The highest page extent */
869 if (lh
!= &sis
->extent_list
) {
870 se
= list_entry(lh
, struct swap_extent
, list
);
871 BUG_ON(se
->start_page
+ se
->nr_pages
!= start_page
);
872 if (se
->start_block
+ se
->nr_pages
== start_block
) {
874 se
->nr_pages
+= nr_pages
;
880 * No merge. Insert a new extent, preserving ordering.
882 new_se
= kmalloc(sizeof(*se
), GFP_KERNEL
);
885 new_se
->start_page
= start_page
;
886 new_se
->nr_pages
= nr_pages
;
887 new_se
->start_block
= start_block
;
889 list_add_tail(&new_se
->list
, &sis
->extent_list
);
894 * A `swap extent' is a simple thing which maps a contiguous range of pages
895 * onto a contiguous range of disk blocks. An ordered list of swap extents
896 * is built at swapon time and is then used at swap_writepage/swap_readpage
897 * time for locating where on disk a page belongs.
899 * If the swapfile is an S_ISBLK block device, a single extent is installed.
900 * This is done so that the main operating code can treat S_ISBLK and S_ISREG
901 * swap files identically.
903 * Whether the swapdev is an S_ISREG file or an S_ISBLK blockdev, the swap
904 * extent list operates in PAGE_SIZE disk blocks. Both S_ISREG and S_ISBLK
905 * swapfiles are handled *identically* after swapon time.
907 * For S_ISREG swapfiles, setup_swap_extents() will walk all the file's blocks
908 * and will parse them into an ordered extent list, in PAGE_SIZE chunks. If
909 * some stray blocks are found which do not fall within the PAGE_SIZE alignment
910 * requirements, they are simply tossed out - we will never use those blocks
913 * For S_ISREG swapfiles we set S_SWAPFILE across the life of the swapon. This
914 * prevents root from shooting her foot off by ftruncating an in-use swapfile,
915 * which will scribble on the fs.
917 * The amount of disk space which a single swap extent represents varies.
918 * Typically it is in the 1-4 megabyte range. So we can have hundreds of
919 * extents in the list. To avoid much list walking, we cache the previous
920 * search location in `curr_swap_extent', and start new searches from there.
921 * This is extremely effective. The average number of iterations in
922 * map_swap_page() has been measured at about 0.3 per page. - akpm.
924 static int setup_swap_extents(struct swap_info_struct
*sis
, sector_t
*span
)
927 unsigned blocks_per_page
;
928 unsigned long page_no
;
930 sector_t probe_block
;
932 sector_t lowest_block
= -1;
933 sector_t highest_block
= 0;
937 inode
= sis
->swap_file
->f_mapping
->host
;
938 if (S_ISBLK(inode
->i_mode
)) {
939 ret
= add_swap_extent(sis
, 0, sis
->max
, 0);
944 blkbits
= inode
->i_blkbits
;
945 blocks_per_page
= PAGE_SIZE
>> blkbits
;
948 * Map all the blocks into the extent list. This code doesn't try
953 last_block
= i_size_read(inode
) >> blkbits
;
954 while ((probe_block
+ blocks_per_page
) <= last_block
&&
955 page_no
< sis
->max
) {
956 unsigned block_in_page
;
957 sector_t first_block
;
959 first_block
= bmap(inode
, probe_block
);
960 if (first_block
== 0)
964 * It must be PAGE_SIZE aligned on-disk
966 if (first_block
& (blocks_per_page
- 1)) {
971 for (block_in_page
= 1; block_in_page
< blocks_per_page
;
975 block
= bmap(inode
, probe_block
+ block_in_page
);
978 if (block
!= first_block
+ block_in_page
) {
985 first_block
>>= (PAGE_SHIFT
- blkbits
);
986 if (page_no
) { /* exclude the header page */
987 if (first_block
< lowest_block
)
988 lowest_block
= first_block
;
989 if (first_block
> highest_block
)
990 highest_block
= first_block
;
994 * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks
996 ret
= add_swap_extent(sis
, page_no
, 1, first_block
);
1001 probe_block
+= blocks_per_page
;
1006 *span
= 1 + highest_block
- lowest_block
;
1008 page_no
= 1; /* force Empty message */
1010 sis
->pages
= page_no
- 1;
1011 sis
->highest_bit
= page_no
- 1;
1013 sis
->curr_swap_extent
= list_entry(sis
->extent_list
.prev
,
1014 struct swap_extent
, list
);
1017 printk(KERN_ERR
"swapon: swapfile has holes\n");
1023 #if 0 /* We don't need this yet */
1024 #include <linux/backing-dev.h>
1025 int page_queue_congested(struct page
*page
)
1027 struct backing_dev_info
*bdi
;
1029 BUG_ON(!PageLocked(page
)); /* It pins the swap_info_struct */
1031 if (PageSwapCache(page
)) {
1032 swp_entry_t entry
= { .val
= page
->private };
1033 struct swap_info_struct
*sis
;
1035 sis
= get_swap_info_struct(swp_type(entry
));
1036 bdi
= sis
->bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1038 bdi
= page
->mapping
->backing_dev_info
;
1039 return bdi_write_congested(bdi
);
1043 asmlinkage
long sys_swapoff(const char __user
* specialfile
)
1045 struct swap_info_struct
* p
= NULL
;
1046 unsigned short *swap_map
;
1047 struct file
*swap_file
, *victim
;
1048 struct address_space
*mapping
;
1049 struct inode
*inode
;
1054 if (!capable(CAP_SYS_ADMIN
))
1057 pathname
= getname(specialfile
);
1058 err
= PTR_ERR(pathname
);
1059 if (IS_ERR(pathname
))
1062 victim
= filp_open(pathname
, O_RDWR
|O_LARGEFILE
, 0);
1064 err
= PTR_ERR(victim
);
1068 mapping
= victim
->f_mapping
;
1071 for (type
= swap_list
.head
; type
>= 0; type
= swap_info
[type
].next
) {
1072 p
= swap_info
+ type
;
1073 if ((p
->flags
& SWP_ACTIVE
) == SWP_ACTIVE
) {
1074 if (p
->swap_file
->f_mapping
== mapping
)
1084 if (!security_vm_enough_memory(p
->pages
))
1085 vm_unacct_memory(p
->pages
);
1092 swap_list
.head
= p
->next
;
1094 swap_info
[prev
].next
= p
->next
;
1096 if (type
== swap_list
.next
) {
1097 /* just pick something that's safe... */
1098 swap_list
.next
= swap_list
.head
;
1100 nr_swap_pages
-= p
->pages
;
1101 total_swap_pages
-= p
->pages
;
1102 swap_device_lock(p
);
1103 p
->flags
&= ~SWP_WRITEOK
;
1104 swap_device_unlock(p
);
1107 current
->flags
|= PF_SWAPOFF
;
1108 err
= try_to_unuse(type
);
1109 current
->flags
&= ~PF_SWAPOFF
;
1111 /* wait for any unplug function to finish */
1112 down_write(&swap_unplug_sem
);
1113 up_write(&swap_unplug_sem
);
1116 /* re-insert swap space back into swap_list */
1118 for (prev
= -1, i
= swap_list
.head
; i
>= 0; prev
= i
, i
= swap_info
[i
].next
)
1119 if (p
->prio
>= swap_info
[i
].prio
)
1123 swap_list
.head
= swap_list
.next
= p
- swap_info
;
1125 swap_info
[prev
].next
= p
- swap_info
;
1126 nr_swap_pages
+= p
->pages
;
1127 total_swap_pages
+= p
->pages
;
1128 p
->flags
|= SWP_WRITEOK
;
1132 destroy_swap_extents(p
);
1136 swap_device_lock(p
);
1137 swap_file
= p
->swap_file
;
1138 p
->swap_file
= NULL
;
1140 swap_map
= p
->swap_map
;
1143 swap_device_unlock(p
);
1147 inode
= mapping
->host
;
1148 if (S_ISBLK(inode
->i_mode
)) {
1149 struct block_device
*bdev
= I_BDEV(inode
);
1150 set_blocksize(bdev
, p
->old_block_size
);
1153 down(&inode
->i_sem
);
1154 inode
->i_flags
&= ~S_SWAPFILE
;
1157 filp_close(swap_file
, NULL
);
1161 filp_close(victim
, NULL
);
1166 #ifdef CONFIG_PROC_FS
1168 static void *swap_start(struct seq_file
*swap
, loff_t
*pos
)
1170 struct swap_info_struct
*ptr
= swap_info
;
1176 for (i
= 0; i
< nr_swapfiles
; i
++, ptr
++) {
1177 if (!(ptr
->flags
& SWP_USED
) || !ptr
->swap_map
)
1186 static void *swap_next(struct seq_file
*swap
, void *v
, loff_t
*pos
)
1188 struct swap_info_struct
*ptr
= v
;
1189 struct swap_info_struct
*endptr
= swap_info
+ nr_swapfiles
;
1191 for (++ptr
; ptr
< endptr
; ptr
++) {
1192 if (!(ptr
->flags
& SWP_USED
) || !ptr
->swap_map
)
1201 static void swap_stop(struct seq_file
*swap
, void *v
)
1206 static int swap_show(struct seq_file
*swap
, void *v
)
1208 struct swap_info_struct
*ptr
= v
;
1213 seq_puts(swap
, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
1215 file
= ptr
->swap_file
;
1216 len
= seq_path(swap
, file
->f_vfsmnt
, file
->f_dentry
, " \t\n\\");
1217 seq_printf(swap
, "%*s%s\t%u\t%u\t%d\n",
1218 len
< 40 ? 40 - len
: 1, " ",
1219 S_ISBLK(file
->f_dentry
->d_inode
->i_mode
) ?
1220 "partition" : "file\t",
1221 ptr
->pages
<< (PAGE_SHIFT
- 10),
1222 ptr
->inuse_pages
<< (PAGE_SHIFT
- 10),
1227 static struct seq_operations swaps_op
= {
1228 .start
= swap_start
,
1234 static int swaps_open(struct inode
*inode
, struct file
*file
)
1236 return seq_open(file
, &swaps_op
);
1239 static struct file_operations proc_swaps_operations
= {
1242 .llseek
= seq_lseek
,
1243 .release
= seq_release
,
1246 static int __init
procswaps_init(void)
1248 struct proc_dir_entry
*entry
;
1250 entry
= create_proc_entry("swaps", 0, NULL
);
1252 entry
->proc_fops
= &proc_swaps_operations
;
1255 __initcall(procswaps_init
);
1256 #endif /* CONFIG_PROC_FS */
1259 * Written 01/25/92 by Simmule Turner, heavily changed by Linus.
1261 * The swapon system call
1263 asmlinkage
long sys_swapon(const char __user
* specialfile
, int swap_flags
)
1265 struct swap_info_struct
* p
;
1267 struct block_device
*bdev
= NULL
;
1268 struct file
*swap_file
= NULL
;
1269 struct address_space
*mapping
;
1273 static int least_priority
;
1274 union swap_header
*swap_header
= NULL
;
1275 int swap_header_version
;
1276 unsigned int nr_good_pages
= 0;
1279 unsigned long maxpages
= 1;
1281 unsigned short *swap_map
;
1282 struct page
*page
= NULL
;
1283 struct inode
*inode
= NULL
;
1286 if (!capable(CAP_SYS_ADMIN
))
1290 for (type
= 0 ; type
< nr_swapfiles
; type
++,p
++)
1291 if (!(p
->flags
& SWP_USED
))
1295 * Test if adding another swap device is possible. There are
1296 * two limiting factors: 1) the number of bits for the swap
1297 * type swp_entry_t definition and 2) the number of bits for
1298 * the swap type in the swap ptes as defined by the different
1299 * architectures. To honor both limitations a swap entry
1300 * with swap offset 0 and swap type ~0UL is created, encoded
1301 * to a swap pte, decoded to a swp_entry_t again and finally
1302 * the swap type part is extracted. This will mask all bits
1303 * from the initial ~0UL that can't be encoded in either the
1304 * swp_entry_t or the architecture definition of a swap pte.
1306 if (type
> swp_type(pte_to_swp_entry(swp_entry_to_pte(swp_entry(~0UL,0))))) {
1310 if (type
>= nr_swapfiles
)
1311 nr_swapfiles
= type
+1;
1312 INIT_LIST_HEAD(&p
->extent_list
);
1313 p
->flags
= SWP_USED
;
1314 p
->swap_file
= NULL
;
1315 p
->old_block_size
= 0;
1321 spin_lock_init(&p
->sdev_lock
);
1323 if (swap_flags
& SWAP_FLAG_PREFER
) {
1325 (swap_flags
& SWAP_FLAG_PRIO_MASK
)>>SWAP_FLAG_PRIO_SHIFT
;
1327 p
->prio
= --least_priority
;
1330 name
= getname(specialfile
);
1331 error
= PTR_ERR(name
);
1336 swap_file
= filp_open(name
, O_RDWR
|O_LARGEFILE
, 0);
1337 error
= PTR_ERR(swap_file
);
1338 if (IS_ERR(swap_file
)) {
1343 p
->swap_file
= swap_file
;
1344 mapping
= swap_file
->f_mapping
;
1345 inode
= mapping
->host
;
1348 for (i
= 0; i
< nr_swapfiles
; i
++) {
1349 struct swap_info_struct
*q
= &swap_info
[i
];
1351 if (i
== type
|| !q
->swap_file
)
1353 if (mapping
== q
->swap_file
->f_mapping
)
1358 if (S_ISBLK(inode
->i_mode
)) {
1359 bdev
= I_BDEV(inode
);
1360 error
= bd_claim(bdev
, sys_swapon
);
1365 p
->old_block_size
= block_size(bdev
);
1366 error
= set_blocksize(bdev
, PAGE_SIZE
);
1370 } else if (S_ISREG(inode
->i_mode
)) {
1371 p
->bdev
= inode
->i_sb
->s_bdev
;
1372 down(&inode
->i_sem
);
1374 if (IS_SWAPFILE(inode
)) {
1382 swapfilesize
= i_size_read(inode
) >> PAGE_SHIFT
;
1385 * Read the swap header.
1387 if (!mapping
->a_ops
->readpage
) {
1391 page
= read_cache_page(mapping
, 0,
1392 (filler_t
*)mapping
->a_ops
->readpage
, swap_file
);
1394 error
= PTR_ERR(page
);
1397 wait_on_page_locked(page
);
1398 if (!PageUptodate(page
))
1401 swap_header
= page_address(page
);
1403 if (!memcmp("SWAP-SPACE",swap_header
->magic
.magic
,10))
1404 swap_header_version
= 1;
1405 else if (!memcmp("SWAPSPACE2",swap_header
->magic
.magic
,10))
1406 swap_header_version
= 2;
1408 printk("Unable to find swap-space signature\n");
1413 switch (swap_header_version
) {
1415 printk(KERN_ERR
"version 0 swap is no longer supported. "
1416 "Use mkswap -v1 %s\n", name
);
1420 /* Check the swap header's sub-version and the size of
1421 the swap file and bad block lists */
1422 if (swap_header
->info
.version
!= 1) {
1424 "Unable to handle swap header version %d\n",
1425 swap_header
->info
.version
);
1432 * Find out how many pages are allowed for a single swap
1433 * device. There are two limiting factors: 1) the number of
1434 * bits for the swap offset in the swp_entry_t type and
1435 * 2) the number of bits in the a swap pte as defined by
1436 * the different architectures. In order to find the
1437 * largest possible bit mask a swap entry with swap type 0
1438 * and swap offset ~0UL is created, encoded to a swap pte,
1439 * decoded to a swp_entry_t again and finally the swap
1440 * offset is extracted. This will mask all the bits from
1441 * the initial ~0UL mask that can't be encoded in either
1442 * the swp_entry_t or the architecture definition of a
1445 maxpages
= swp_offset(pte_to_swp_entry(swp_entry_to_pte(swp_entry(0,~0UL)))) - 1;
1446 if (maxpages
> swap_header
->info
.last_page
)
1447 maxpages
= swap_header
->info
.last_page
;
1448 p
->highest_bit
= maxpages
- 1;
1453 if (swap_header
->info
.nr_badpages
&& S_ISREG(inode
->i_mode
))
1455 if (swap_header
->info
.nr_badpages
> MAX_SWAP_BADPAGES
)
1458 /* OK, set up the swap map and apply the bad block list */
1459 if (!(p
->swap_map
= vmalloc(maxpages
* sizeof(short)))) {
1465 memset(p
->swap_map
, 0, maxpages
* sizeof(short));
1466 for (i
=0; i
<swap_header
->info
.nr_badpages
; i
++) {
1467 int page
= swap_header
->info
.badpages
[i
];
1468 if (page
<= 0 || page
>= swap_header
->info
.last_page
)
1471 p
->swap_map
[page
] = SWAP_MAP_BAD
;
1473 nr_good_pages
= swap_header
->info
.last_page
-
1474 swap_header
->info
.nr_badpages
-
1475 1 /* header page */;
1480 if (swapfilesize
&& maxpages
> swapfilesize
) {
1482 "Swap area shorter than signature indicates\n");
1486 if (nr_good_pages
) {
1487 p
->swap_map
[0] = SWAP_MAP_BAD
;
1489 p
->pages
= nr_good_pages
;
1490 nr_extents
= setup_swap_extents(p
, &span
);
1491 if (nr_extents
< 0) {
1495 nr_good_pages
= p
->pages
;
1497 if (!nr_good_pages
) {
1498 printk(KERN_WARNING
"Empty swap-file\n");
1505 swap_device_lock(p
);
1506 p
->flags
= SWP_ACTIVE
;
1507 nr_swap_pages
+= nr_good_pages
;
1508 total_swap_pages
+= nr_good_pages
;
1510 printk(KERN_INFO
"Adding %uk swap on %s. "
1511 "Priority:%d extents:%d across:%lluk\n",
1512 nr_good_pages
<<(PAGE_SHIFT
-10), name
, p
->prio
,
1513 nr_extents
, (unsigned long long)span
<<(PAGE_SHIFT
-10));
1515 /* insert swap space into swap_list: */
1517 for (i
= swap_list
.head
; i
>= 0; i
= swap_info
[i
].next
) {
1518 if (p
->prio
>= swap_info
[i
].prio
) {
1525 swap_list
.head
= swap_list
.next
= p
- swap_info
;
1527 swap_info
[prev
].next
= p
- swap_info
;
1529 swap_device_unlock(p
);
1536 set_blocksize(bdev
, p
->old_block_size
);
1539 destroy_swap_extents(p
);
1542 swap_map
= p
->swap_map
;
1543 p
->swap_file
= NULL
;
1546 if (!(swap_flags
& SWAP_FLAG_PREFER
))
1551 filp_close(swap_file
, NULL
);
1553 if (page
&& !IS_ERR(page
)) {
1555 page_cache_release(page
);
1561 inode
->i_flags
|= S_SWAPFILE
;
1567 void si_swapinfo(struct sysinfo
*val
)
1570 unsigned long nr_to_be_unused
= 0;
1573 for (i
= 0; i
< nr_swapfiles
; i
++) {
1574 if (!(swap_info
[i
].flags
& SWP_USED
) ||
1575 (swap_info
[i
].flags
& SWP_WRITEOK
))
1577 nr_to_be_unused
+= swap_info
[i
].inuse_pages
;
1579 val
->freeswap
= nr_swap_pages
+ nr_to_be_unused
;
1580 val
->totalswap
= total_swap_pages
+ nr_to_be_unused
;
1585 * Verify that a swap entry is valid and increment its swap map count.
1587 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
1588 * "permanent", but will be reclaimed by the next swapoff.
1590 int swap_duplicate(swp_entry_t entry
)
1592 struct swap_info_struct
* p
;
1593 unsigned long offset
, type
;
1596 type
= swp_type(entry
);
1597 if (type
>= nr_swapfiles
)
1599 p
= type
+ swap_info
;
1600 offset
= swp_offset(entry
);
1602 swap_device_lock(p
);
1603 if (offset
< p
->max
&& p
->swap_map
[offset
]) {
1604 if (p
->swap_map
[offset
] < SWAP_MAP_MAX
- 1) {
1605 p
->swap_map
[offset
]++;
1607 } else if (p
->swap_map
[offset
] <= SWAP_MAP_MAX
) {
1608 if (swap_overflow
++ < 5)
1609 printk(KERN_WARNING
"swap_dup: swap entry overflow\n");
1610 p
->swap_map
[offset
] = SWAP_MAP_MAX
;
1614 swap_device_unlock(p
);
1619 printk(KERN_ERR
"swap_dup: %s%08lx\n", Bad_file
, entry
.val
);
1623 struct swap_info_struct
*
1624 get_swap_info_struct(unsigned type
)
1626 return &swap_info
[type
];
1630 * swap_device_lock prevents swap_map being freed. Don't grab an extra
1631 * reference on the swaphandle, it doesn't matter if it becomes unused.
1633 int valid_swaphandles(swp_entry_t entry
, unsigned long *offset
)
1635 int ret
= 0, i
= 1 << page_cluster
;
1637 struct swap_info_struct
*swapdev
= swp_type(entry
) + swap_info
;
1639 if (!page_cluster
) /* no readahead */
1641 toff
= (swp_offset(entry
) >> page_cluster
) << page_cluster
;
1642 if (!toff
) /* first page is swap header */
1646 swap_device_lock(swapdev
);
1648 /* Don't read-ahead past the end of the swap area */
1649 if (toff
>= swapdev
->max
)
1651 /* Don't read in free or bad pages */
1652 if (!swapdev
->swap_map
[toff
])
1654 if (swapdev
->swap_map
[toff
] == SWAP_MAP_BAD
)
1659 swap_device_unlock(swapdev
);