diff options
Diffstat (limited to 'mm/swapfile.c')
-rw-r--r-- | mm/swapfile.c | 99 |
1 files changed, 46 insertions, 53 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c index fafc26d1b1dc..71373d03fcee 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c | |||
@@ -31,6 +31,8 @@ | |||
31 | #include <linux/memcontrol.h> | 31 | #include <linux/memcontrol.h> |
32 | #include <linux/poll.h> | 32 | #include <linux/poll.h> |
33 | #include <linux/oom.h> | 33 | #include <linux/oom.h> |
34 | #include <linux/frontswap.h> | ||
35 | #include <linux/swapfile.h> | ||
34 | 36 | ||
35 | #include <asm/pgtable.h> | 37 | #include <asm/pgtable.h> |
36 | #include <asm/tlbflush.h> | 38 | #include <asm/tlbflush.h> |
@@ -42,7 +44,7 @@ static bool swap_count_continued(struct swap_info_struct *, pgoff_t, | |||
42 | static void free_swap_count_continuations(struct swap_info_struct *); | 44 | static void free_swap_count_continuations(struct swap_info_struct *); |
43 | static sector_t map_swap_entry(swp_entry_t, struct block_device**); | 45 | static sector_t map_swap_entry(swp_entry_t, struct block_device**); |
44 | 46 | ||
45 | static DEFINE_SPINLOCK(swap_lock); | 47 | DEFINE_SPINLOCK(swap_lock); |
46 | static unsigned int nr_swapfiles; | 48 | static unsigned int nr_swapfiles; |
47 | long nr_swap_pages; | 49 | long nr_swap_pages; |
48 | long total_swap_pages; | 50 | long total_swap_pages; |
@@ -53,9 +55,9 @@ static const char Unused_file[] = "Unused swap file entry "; | |||
53 | static const char Bad_offset[] = "Bad swap offset entry "; | 55 | static const char Bad_offset[] = "Bad swap offset entry "; |
54 | static const char Unused_offset[] = "Unused swap offset entry "; | 56 | static const char Unused_offset[] = "Unused swap offset entry "; |
55 | 57 | ||
56 | static struct swap_list_t swap_list = {-1, -1}; | 58 | struct swap_list_t swap_list = {-1, -1}; |
57 | 59 | ||
58 | static struct swap_info_struct *swap_info[MAX_SWAPFILES]; | 60 | struct swap_info_struct *swap_info[MAX_SWAPFILES]; |
59 | 61 | ||
60 | static DEFINE_MUTEX(swapon_mutex); | 62 | static DEFINE_MUTEX(swapon_mutex); |
61 | 63 | ||
@@ -556,6 +558,7 @@ static unsigned char swap_entry_free(struct swap_info_struct *p, | |||
556 | swap_list.next = p->type; | 558 | swap_list.next = p->type; |
557 | nr_swap_pages++; | 559 | nr_swap_pages++; |
558 | p->inuse_pages--; | 560 | p->inuse_pages--; |
561 | frontswap_invalidate_page(p->type, offset); | ||
559 | if ((p->flags & SWP_BLKDEV) && | 562 | if ((p->flags & SWP_BLKDEV) && |
560 | disk->fops->swap_slot_free_notify) | 563 | disk->fops->swap_slot_free_notify) |
561 | disk->fops->swap_slot_free_notify(p->bdev, offset); | 564 | disk->fops->swap_slot_free_notify(p->bdev, offset); |
@@ -601,7 +604,7 @@ void swapcache_free(swp_entry_t entry, struct page *page) | |||
601 | * This does not give an exact answer when swap count is continued, | 604 | * This does not give an exact answer when swap count is continued, |
602 | * but does include the high COUNT_CONTINUED flag to allow for that. | 605 | * but does include the high COUNT_CONTINUED flag to allow for that. |
603 | */ | 606 | */ |
604 | static inline int page_swapcount(struct page *page) | 607 | int page_swapcount(struct page *page) |
605 | { | 608 | { |
606 | int count = 0; | 609 | int count = 0; |
607 | struct swap_info_struct *p; | 610 | struct swap_info_struct *p; |
@@ -717,37 +720,6 @@ int free_swap_and_cache(swp_entry_t entry) | |||
717 | return p != NULL; | 720 | return p != NULL; |
718 | } | 721 | } |
719 | 722 | ||
720 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR | ||
721 | /** | ||
722 | * mem_cgroup_count_swap_user - count the user of a swap entry | ||
723 | * @ent: the swap entry to be checked | ||
724 | * @pagep: the pointer for the swap cache page of the entry to be stored | ||
725 | * | ||
726 | * Returns the number of the user of the swap entry. The number is valid only | ||
727 | * for swaps of anonymous pages. | ||
728 | * If the entry is found on swap cache, the page is stored to pagep with | ||
729 | * refcount of it being incremented. | ||
730 | */ | ||
731 | int mem_cgroup_count_swap_user(swp_entry_t ent, struct page **pagep) | ||
732 | { | ||
733 | struct page *page; | ||
734 | struct swap_info_struct *p; | ||
735 | int count = 0; | ||
736 | |||
737 | page = find_get_page(&swapper_space, ent.val); | ||
738 | if (page) | ||
739 | count += page_mapcount(page); | ||
740 | p = swap_info_get(ent); | ||
741 | if (p) { | ||
742 | count += swap_count(p->swap_map[swp_offset(ent)]); | ||
743 | spin_unlock(&swap_lock); | ||
744 | } | ||
745 | |||
746 | *pagep = page; | ||
747 | return count; | ||
748 | } | ||
749 | #endif | ||
750 | |||
751 | #ifdef CONFIG_HIBERNATION | 723 | #ifdef CONFIG_HIBERNATION |
752 | /* | 724 | /* |
753 | * Find the swap type that corresponds to given device (if any). | 725 | * Find the swap type that corresponds to given device (if any). |
@@ -1016,11 +988,12 @@ static int unuse_mm(struct mm_struct *mm, | |||
1016 | } | 988 | } |
1017 | 989 | ||
1018 | /* | 990 | /* |
1019 | * Scan swap_map from current position to next entry still in use. | 991 | * Scan swap_map (or frontswap_map if frontswap parameter is true) |
992 | * from current position to next entry still in use. | ||
1020 | * Recycle to start on reaching the end, returning 0 when empty. | 993 | * Recycle to start on reaching the end, returning 0 when empty. |
1021 | */ | 994 | */ |
1022 | static unsigned int find_next_to_unuse(struct swap_info_struct *si, | 995 | static unsigned int find_next_to_unuse(struct swap_info_struct *si, |
1023 | unsigned int prev) | 996 | unsigned int prev, bool frontswap) |
1024 | { | 997 | { |
1025 | unsigned int max = si->max; | 998 | unsigned int max = si->max; |
1026 | unsigned int i = prev; | 999 | unsigned int i = prev; |
@@ -1046,6 +1019,12 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si, | |||
1046 | prev = 0; | 1019 | prev = 0; |
1047 | i = 1; | 1020 | i = 1; |
1048 | } | 1021 | } |
1022 | if (frontswap) { | ||
1023 | if (frontswap_test(si, i)) | ||
1024 | break; | ||
1025 | else | ||
1026 | continue; | ||
1027 | } | ||
1049 | count = si->swap_map[i]; | 1028 | count = si->swap_map[i]; |
1050 | if (count && swap_count(count) != SWAP_MAP_BAD) | 1029 | if (count && swap_count(count) != SWAP_MAP_BAD) |
1051 | break; | 1030 | break; |
@@ -1057,8 +1036,12 @@ static unsigned int find_next_to_unuse(struct swap_info_struct *si, | |||
1057 | * We completely avoid races by reading each swap page in advance, | 1036 | * We completely avoid races by reading each swap page in advance, |
1058 | * and then search for the process using it. All the necessary | 1037 | * and then search for the process using it. All the necessary |
1059 | * page table adjustments can then be made atomically. | 1038 | * page table adjustments can then be made atomically. |
1039 | * | ||
1040 | * if the boolean frontswap is true, only unuse pages_to_unuse pages; | ||
1041 | * pages_to_unuse==0 means all pages; ignored if frontswap is false | ||
1060 | */ | 1042 | */ |
1061 | static int try_to_unuse(unsigned int type) | 1043 | int try_to_unuse(unsigned int type, bool frontswap, |
1044 | unsigned long pages_to_unuse) | ||
1062 | { | 1045 | { |
1063 | struct swap_info_struct *si = swap_info[type]; | 1046 | struct swap_info_struct *si = swap_info[type]; |
1064 | struct mm_struct *start_mm; | 1047 | struct mm_struct *start_mm; |
@@ -1091,7 +1074,7 @@ static int try_to_unuse(unsigned int type) | |||
1091 | * one pass through swap_map is enough, but not necessarily: | 1074 | * one pass through swap_map is enough, but not necessarily: |
1092 | * there are races when an instance of an entry might be missed. | 1075 | * there are races when an instance of an entry might be missed. |
1093 | */ | 1076 | */ |
1094 | while ((i = find_next_to_unuse(si, i)) != 0) { | 1077 | while ((i = find_next_to_unuse(si, i, frontswap)) != 0) { |
1095 | if (signal_pending(current)) { | 1078 | if (signal_pending(current)) { |
1096 | retval = -EINTR; | 1079 | retval = -EINTR; |
1097 | break; | 1080 | break; |
@@ -1258,6 +1241,10 @@ static int try_to_unuse(unsigned int type) | |||
1258 | * interactive performance. | 1241 | * interactive performance. |
1259 | */ | 1242 | */ |
1260 | cond_resched(); | 1243 | cond_resched(); |
1244 | if (frontswap && pages_to_unuse > 0) { | ||
1245 | if (!--pages_to_unuse) | ||
1246 | break; | ||
1247 | } | ||
1261 | } | 1248 | } |
1262 | 1249 | ||
1263 | mmput(start_mm); | 1250 | mmput(start_mm); |
@@ -1517,7 +1504,8 @@ bad_bmap: | |||
1517 | } | 1504 | } |
1518 | 1505 | ||
1519 | static void enable_swap_info(struct swap_info_struct *p, int prio, | 1506 | static void enable_swap_info(struct swap_info_struct *p, int prio, |
1520 | unsigned char *swap_map) | 1507 | unsigned char *swap_map, |
1508 | unsigned long *frontswap_map) | ||
1521 | { | 1509 | { |
1522 | int i, prev; | 1510 | int i, prev; |
1523 | 1511 | ||
@@ -1527,6 +1515,7 @@ static void enable_swap_info(struct swap_info_struct *p, int prio, | |||
1527 | else | 1515 | else |
1528 | p->prio = --least_priority; | 1516 | p->prio = --least_priority; |
1529 | p->swap_map = swap_map; | 1517 | p->swap_map = swap_map; |
1518 | frontswap_map_set(p, frontswap_map); | ||
1530 | p->flags |= SWP_WRITEOK; | 1519 | p->flags |= SWP_WRITEOK; |
1531 | nr_swap_pages += p->pages; | 1520 | nr_swap_pages += p->pages; |
1532 | total_swap_pages += p->pages; | 1521 | total_swap_pages += p->pages; |
@@ -1543,6 +1532,7 @@ static void enable_swap_info(struct swap_info_struct *p, int prio, | |||
1543 | swap_list.head = swap_list.next = p->type; | 1532 | swap_list.head = swap_list.next = p->type; |
1544 | else | 1533 | else |
1545 | swap_info[prev]->next = p->type; | 1534 | swap_info[prev]->next = p->type; |
1535 | frontswap_init(p->type); | ||
1546 | spin_unlock(&swap_lock); | 1536 | spin_unlock(&swap_lock); |
1547 | } | 1537 | } |
1548 | 1538 | ||
@@ -1616,7 +1606,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1616 | spin_unlock(&swap_lock); | 1606 | spin_unlock(&swap_lock); |
1617 | 1607 | ||
1618 | oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX); | 1608 | oom_score_adj = test_set_oom_score_adj(OOM_SCORE_ADJ_MAX); |
1619 | err = try_to_unuse(type); | 1609 | err = try_to_unuse(type, false, 0); /* force all pages to be unused */ |
1620 | compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj); | 1610 | compare_swap_oom_score_adj(OOM_SCORE_ADJ_MAX, oom_score_adj); |
1621 | 1611 | ||
1622 | if (err) { | 1612 | if (err) { |
@@ -1627,7 +1617,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1627 | * sys_swapoff for this swap_info_struct at this point. | 1617 | * sys_swapoff for this swap_info_struct at this point. |
1628 | */ | 1618 | */ |
1629 | /* re-insert swap space back into swap_list */ | 1619 | /* re-insert swap space back into swap_list */ |
1630 | enable_swap_info(p, p->prio, p->swap_map); | 1620 | enable_swap_info(p, p->prio, p->swap_map, frontswap_map_get(p)); |
1631 | goto out_dput; | 1621 | goto out_dput; |
1632 | } | 1622 | } |
1633 | 1623 | ||
@@ -1653,9 +1643,11 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) | |||
1653 | swap_map = p->swap_map; | 1643 | swap_map = p->swap_map; |
1654 | p->swap_map = NULL; | 1644 | p->swap_map = NULL; |
1655 | p->flags = 0; | 1645 | p->flags = 0; |
1646 | frontswap_invalidate_area(type); | ||
1656 | spin_unlock(&swap_lock); | 1647 | spin_unlock(&swap_lock); |
1657 | mutex_unlock(&swapon_mutex); | 1648 | mutex_unlock(&swapon_mutex); |
1658 | vfree(swap_map); | 1649 | vfree(swap_map); |
1650 | vfree(frontswap_map_get(p)); | ||
1659 | /* Destroy swap account informatin */ | 1651 | /* Destroy swap account informatin */ |
1660 | swap_cgroup_swapoff(type); | 1652 | swap_cgroup_swapoff(type); |
1661 | 1653 | ||
@@ -1924,24 +1916,20 @@ static unsigned long read_swap_header(struct swap_info_struct *p, | |||
1924 | 1916 | ||
1925 | /* | 1917 | /* |
1926 | * Find out how many pages are allowed for a single swap | 1918 | * Find out how many pages are allowed for a single swap |
1927 | * device. There are three limiting factors: 1) the number | 1919 | * device. There are two limiting factors: 1) the number |
1928 | * of bits for the swap offset in the swp_entry_t type, and | 1920 | * of bits for the swap offset in the swp_entry_t type, and |
1929 | * 2) the number of bits in the swap pte as defined by the | 1921 | * 2) the number of bits in the swap pte as defined by the |
1930 | * the different architectures, and 3) the number of free bits | 1922 | * different architectures. In order to find the |
1931 | * in an exceptional radix_tree entry. In order to find the | ||
1932 | * largest possible bit mask, a swap entry with swap type 0 | 1923 | * largest possible bit mask, a swap entry with swap type 0 |
1933 | * and swap offset ~0UL is created, encoded to a swap pte, | 1924 | * and swap offset ~0UL is created, encoded to a swap pte, |
1934 | * decoded to a swp_entry_t again, and finally the swap | 1925 | * decoded to a swp_entry_t again, and finally the swap |
1935 | * offset is extracted. This will mask all the bits from | 1926 | * offset is extracted. This will mask all the bits from |
1936 | * the initial ~0UL mask that can't be encoded in either | 1927 | * the initial ~0UL mask that can't be encoded in either |
1937 | * the swp_entry_t or the architecture definition of a | 1928 | * the swp_entry_t or the architecture definition of a |
1938 | * swap pte. Then the same is done for a radix_tree entry. | 1929 | * swap pte. |
1939 | */ | 1930 | */ |
1940 | maxpages = swp_offset(pte_to_swp_entry( | 1931 | maxpages = swp_offset(pte_to_swp_entry( |
1941 | swp_entry_to_pte(swp_entry(0, ~0UL)))); | 1932 | swp_entry_to_pte(swp_entry(0, ~0UL)))) + 1; |
1942 | maxpages = swp_offset(radix_to_swp_entry( | ||
1943 | swp_to_radix_entry(swp_entry(0, maxpages)))) + 1; | ||
1944 | |||
1945 | if (maxpages > swap_header->info.last_page) { | 1933 | if (maxpages > swap_header->info.last_page) { |
1946 | maxpages = swap_header->info.last_page + 1; | 1934 | maxpages = swap_header->info.last_page + 1; |
1947 | /* p->max is an unsigned int: don't overflow it */ | 1935 | /* p->max is an unsigned int: don't overflow it */ |
@@ -2019,6 +2007,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) | |||
2019 | sector_t span; | 2007 | sector_t span; |
2020 | unsigned long maxpages; | 2008 | unsigned long maxpages; |
2021 | unsigned char *swap_map = NULL; | 2009 | unsigned char *swap_map = NULL; |
2010 | unsigned long *frontswap_map = NULL; | ||
2022 | struct page *page = NULL; | 2011 | struct page *page = NULL; |
2023 | struct inode *inode = NULL; | 2012 | struct inode *inode = NULL; |
2024 | 2013 | ||
@@ -2102,6 +2091,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) | |||
2102 | error = nr_extents; | 2091 | error = nr_extents; |
2103 | goto bad_swap; | 2092 | goto bad_swap; |
2104 | } | 2093 | } |
2094 | /* frontswap enabled? set up bit-per-page map for frontswap */ | ||
2095 | if (frontswap_enabled) | ||
2096 | frontswap_map = vzalloc(maxpages / sizeof(long)); | ||
2105 | 2097 | ||
2106 | if (p->bdev) { | 2098 | if (p->bdev) { |
2107 | if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { | 2099 | if (blk_queue_nonrot(bdev_get_queue(p->bdev))) { |
@@ -2117,14 +2109,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) | |||
2117 | if (swap_flags & SWAP_FLAG_PREFER) | 2109 | if (swap_flags & SWAP_FLAG_PREFER) |
2118 | prio = | 2110 | prio = |
2119 | (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; | 2111 | (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; |
2120 | enable_swap_info(p, prio, swap_map); | 2112 | enable_swap_info(p, prio, swap_map, frontswap_map); |
2121 | 2113 | ||
2122 | printk(KERN_INFO "Adding %uk swap on %s. " | 2114 | printk(KERN_INFO "Adding %uk swap on %s. " |
2123 | "Priority:%d extents:%d across:%lluk %s%s\n", | 2115 | "Priority:%d extents:%d across:%lluk %s%s%s\n", |
2124 | p->pages<<(PAGE_SHIFT-10), name, p->prio, | 2116 | p->pages<<(PAGE_SHIFT-10), name, p->prio, |
2125 | nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), | 2117 | nr_extents, (unsigned long long)span<<(PAGE_SHIFT-10), |
2126 | (p->flags & SWP_SOLIDSTATE) ? "SS" : "", | 2118 | (p->flags & SWP_SOLIDSTATE) ? "SS" : "", |
2127 | (p->flags & SWP_DISCARDABLE) ? "D" : ""); | 2119 | (p->flags & SWP_DISCARDABLE) ? "D" : "", |
2120 | (frontswap_map) ? "FS" : ""); | ||
2128 | 2121 | ||
2129 | mutex_unlock(&swapon_mutex); | 2122 | mutex_unlock(&swapon_mutex); |
2130 | atomic_inc(&proc_poll_event); | 2123 | atomic_inc(&proc_poll_event); |