Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Red Hat, Inc. |
| 3 | * |
| 4 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 5 | * the COPYING file in the top-level directory. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/highmem.h> |
| 11 | #include <linux/hugetlb.h> |
| 12 | #include <linux/mmu_notifier.h> |
| 13 | #include <linux/rmap.h> |
| 14 | #include <linux/swap.h> |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 15 | #include <linux/shrinker.h> |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 16 | #include <linux/mm_inline.h> |
| 17 | #include <linux/kthread.h> |
| 18 | #include <linux/khugepaged.h> |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 19 | #include <linux/freezer.h> |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 20 | #include <linux/mman.h> |
Ralf Baechle | 325adeb | 2012-10-15 13:44:56 +0200 | [diff] [blame] | 21 | #include <linux/pagemap.h> |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 22 | #include <linux/migrate.h> |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 23 | #include <linux/hashtable.h> |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 24 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 25 | #include <asm/tlb.h> |
| 26 | #include <asm/pgalloc.h> |
| 27 | #include "internal.h" |
| 28 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 29 | /* |
| 30 | * By default transparent hugepage support is enabled for all mappings |
| 31 | * and khugepaged scans all mappings. Defrag is only invoked by |
| 32 | * khugepaged hugepage allocations and by page faults inside |
| 33 | * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived |
| 34 | * allocations. |
| 35 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 36 | unsigned long transparent_hugepage_flags __read_mostly = |
Andrea Arcangeli | 13ece88 | 2011-01-13 15:47:07 -0800 | [diff] [blame] | 37 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 38 | (1<<TRANSPARENT_HUGEPAGE_FLAG)| |
Andrea Arcangeli | 13ece88 | 2011-01-13 15:47:07 -0800 | [diff] [blame] | 39 | #endif |
| 40 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE |
| 41 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)| |
| 42 | #endif |
Andrea Arcangeli | d39d33c | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 43 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)| |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 44 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)| |
| 45 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 46 | |
| 47 | /* default scan 8*512 pte (or vmas) every 30 second */ |
| 48 | static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8; |
| 49 | static unsigned int khugepaged_pages_collapsed; |
| 50 | static unsigned int khugepaged_full_scans; |
| 51 | static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000; |
| 52 | /* during fragmentation poll the hugepage allocator once every minute */ |
| 53 | static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000; |
| 54 | static struct task_struct *khugepaged_thread __read_mostly; |
| 55 | static DEFINE_MUTEX(khugepaged_mutex); |
| 56 | static DEFINE_SPINLOCK(khugepaged_mm_lock); |
| 57 | static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait); |
| 58 | /* |
| 59 | * default collapse hugepages if there is at least one pte mapped like |
| 60 | * it would have happened if the vma was large enough during page |
| 61 | * fault. |
| 62 | */ |
| 63 | static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1; |
| 64 | |
| 65 | static int khugepaged(void *none); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 66 | static int khugepaged_slab_init(void); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 67 | |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 68 | #define MM_SLOTS_HASH_BITS 10 |
| 69 | static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); |
| 70 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 71 | static struct kmem_cache *mm_slot_cache __read_mostly; |
| 72 | |
| 73 | /** |
| 74 | * struct mm_slot - hash lookup from mm to mm_slot |
| 75 | * @hash: hash collision list |
| 76 | * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head |
| 77 | * @mm: the mm that this information is valid for |
| 78 | */ |
| 79 | struct mm_slot { |
| 80 | struct hlist_node hash; |
| 81 | struct list_head mm_node; |
| 82 | struct mm_struct *mm; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * struct khugepaged_scan - cursor for scanning |
| 87 | * @mm_head: the head of the mm list to scan |
| 88 | * @mm_slot: the current mm_slot we are scanning |
| 89 | * @address: the next address inside that to be scanned |
| 90 | * |
| 91 | * There is only the one khugepaged_scan instance of this cursor structure. |
| 92 | */ |
| 93 | struct khugepaged_scan { |
| 94 | struct list_head mm_head; |
| 95 | struct mm_slot *mm_slot; |
| 96 | unsigned long address; |
H Hartley Sweeten | 2f1da64 | 2011-10-31 17:09:25 -0700 | [diff] [blame] | 97 | }; |
| 98 | static struct khugepaged_scan khugepaged_scan = { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 99 | .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), |
| 100 | }; |
| 101 | |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 102 | |
| 103 | static int set_recommended_min_free_kbytes(void) |
| 104 | { |
| 105 | struct zone *zone; |
| 106 | int nr_zones = 0; |
| 107 | unsigned long recommended_min; |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 108 | |
Xiao Guangrong | 17c230a | 2012-10-08 16:29:56 -0700 | [diff] [blame] | 109 | if (!khugepaged_enabled()) |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | |
| 112 | for_each_populated_zone(zone) |
| 113 | nr_zones++; |
| 114 | |
| 115 | /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */ |
| 116 | recommended_min = pageblock_nr_pages * nr_zones * 2; |
| 117 | |
| 118 | /* |
| 119 | * Make sure that on average at least two pageblocks are almost free |
| 120 | * of another type, one for a migratetype to fall back to and a |
| 121 | * second to avoid subsequent fallbacks of other types There are 3 |
| 122 | * MIGRATE_TYPES we care about. |
| 123 | */ |
| 124 | recommended_min += pageblock_nr_pages * nr_zones * |
| 125 | MIGRATE_PCPTYPES * MIGRATE_PCPTYPES; |
| 126 | |
| 127 | /* don't ever allow to reserve more than 5% of the lowmem */ |
| 128 | recommended_min = min(recommended_min, |
| 129 | (unsigned long) nr_free_buffer_pages() / 20); |
| 130 | recommended_min <<= (PAGE_SHIFT-10); |
| 131 | |
| 132 | if (recommended_min > min_free_kbytes) |
| 133 | min_free_kbytes = recommended_min; |
| 134 | setup_per_zone_wmarks(); |
| 135 | return 0; |
| 136 | } |
| 137 | late_initcall(set_recommended_min_free_kbytes); |
| 138 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 139 | static int start_khugepaged(void) |
| 140 | { |
| 141 | int err = 0; |
| 142 | if (khugepaged_enabled()) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 143 | if (!khugepaged_thread) |
| 144 | khugepaged_thread = kthread_run(khugepaged, NULL, |
| 145 | "khugepaged"); |
| 146 | if (unlikely(IS_ERR(khugepaged_thread))) { |
| 147 | printk(KERN_ERR |
| 148 | "khugepaged: kthread_run(khugepaged) failed\n"); |
| 149 | err = PTR_ERR(khugepaged_thread); |
| 150 | khugepaged_thread = NULL; |
| 151 | } |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 152 | |
| 153 | if (!list_empty(&khugepaged_scan.mm_head)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 154 | wake_up_interruptible(&khugepaged_wait); |
Andrea Arcangeli | f000565 | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 155 | |
| 156 | set_recommended_min_free_kbytes(); |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 157 | } else if (khugepaged_thread) { |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 158 | kthread_stop(khugepaged_thread); |
| 159 | khugepaged_thread = NULL; |
| 160 | } |
Xiao Guangrong | 637e3a2 | 2012-10-08 16:29:38 -0700 | [diff] [blame] | 161 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 162 | return err; |
| 163 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 164 | |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 165 | static atomic_t huge_zero_refcount; |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 166 | static struct page *huge_zero_page __read_mostly; |
Kirill A. Shutemov | 4a6c129 | 2012-12-12 13:50:47 -0800 | [diff] [blame] | 167 | |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 168 | static inline bool is_huge_zero_page(struct page *page) |
Kirill A. Shutemov | 4a6c129 | 2012-12-12 13:50:47 -0800 | [diff] [blame] | 169 | { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 170 | return ACCESS_ONCE(huge_zero_page) == page; |
Kirill A. Shutemov | 4a6c129 | 2012-12-12 13:50:47 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static inline bool is_huge_zero_pmd(pmd_t pmd) |
| 174 | { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 175 | return is_huge_zero_page(pmd_page(pmd)); |
Kirill A. Shutemov | 4a6c129 | 2012-12-12 13:50:47 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 178 | static struct page *get_huge_zero_page(void) |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 179 | { |
| 180 | struct page *zero_page; |
| 181 | retry: |
| 182 | if (likely(atomic_inc_not_zero(&huge_zero_refcount))) |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 183 | return ACCESS_ONCE(huge_zero_page); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 184 | |
| 185 | zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE, |
| 186 | HPAGE_PMD_ORDER); |
Kirill A. Shutemov | d8a8e1f | 2012-12-12 13:51:09 -0800 | [diff] [blame] | 187 | if (!zero_page) { |
| 188 | count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED); |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 189 | return NULL; |
Kirill A. Shutemov | d8a8e1f | 2012-12-12 13:51:09 -0800 | [diff] [blame] | 190 | } |
| 191 | count_vm_event(THP_ZERO_PAGE_ALLOC); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 192 | preempt_disable(); |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 193 | if (cmpxchg(&huge_zero_page, NULL, zero_page)) { |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 194 | preempt_enable(); |
| 195 | __free_page(zero_page); |
| 196 | goto retry; |
| 197 | } |
| 198 | |
| 199 | /* We take additional reference here. It will be put back by shrinker */ |
| 200 | atomic_set(&huge_zero_refcount, 2); |
| 201 | preempt_enable(); |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 202 | return ACCESS_ONCE(huge_zero_page); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static void put_huge_zero_page(void) |
| 206 | { |
| 207 | /* |
| 208 | * Counter should never go to zero here. Only shrinker can put |
| 209 | * last reference. |
| 210 | */ |
| 211 | BUG_ON(atomic_dec_and_test(&huge_zero_refcount)); |
| 212 | } |
| 213 | |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 214 | static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink, |
| 215 | struct shrink_control *sc) |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 216 | { |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 217 | /* we can free zero page only if last reference remains */ |
| 218 | return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0; |
| 219 | } |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 220 | |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 221 | static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, |
| 222 | struct shrink_control *sc) |
| 223 | { |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 224 | if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 225 | struct page *zero_page = xchg(&huge_zero_page, NULL); |
| 226 | BUG_ON(zero_page == NULL); |
| 227 | __free_page(zero_page); |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 228 | return HPAGE_PMD_NR; |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static struct shrinker huge_zero_page_shrinker = { |
Glauber Costa | 4889646 | 2013-08-28 10:18:15 +1000 | [diff] [blame] | 235 | .count_objects = shrink_huge_zero_page_count, |
| 236 | .scan_objects = shrink_huge_zero_page_scan, |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 237 | .seeks = DEFAULT_SEEKS, |
| 238 | }; |
| 239 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 240 | #ifdef CONFIG_SYSFS |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 241 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 242 | static ssize_t double_flag_show(struct kobject *kobj, |
| 243 | struct kobj_attribute *attr, char *buf, |
| 244 | enum transparent_hugepage_flag enabled, |
| 245 | enum transparent_hugepage_flag req_madv) |
| 246 | { |
| 247 | if (test_bit(enabled, &transparent_hugepage_flags)) { |
| 248 | VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags)); |
| 249 | return sprintf(buf, "[always] madvise never\n"); |
| 250 | } else if (test_bit(req_madv, &transparent_hugepage_flags)) |
| 251 | return sprintf(buf, "always [madvise] never\n"); |
| 252 | else |
| 253 | return sprintf(buf, "always madvise [never]\n"); |
| 254 | } |
| 255 | static ssize_t double_flag_store(struct kobject *kobj, |
| 256 | struct kobj_attribute *attr, |
| 257 | const char *buf, size_t count, |
| 258 | enum transparent_hugepage_flag enabled, |
| 259 | enum transparent_hugepage_flag req_madv) |
| 260 | { |
| 261 | if (!memcmp("always", buf, |
| 262 | min(sizeof("always")-1, count))) { |
| 263 | set_bit(enabled, &transparent_hugepage_flags); |
| 264 | clear_bit(req_madv, &transparent_hugepage_flags); |
| 265 | } else if (!memcmp("madvise", buf, |
| 266 | min(sizeof("madvise")-1, count))) { |
| 267 | clear_bit(enabled, &transparent_hugepage_flags); |
| 268 | set_bit(req_madv, &transparent_hugepage_flags); |
| 269 | } else if (!memcmp("never", buf, |
| 270 | min(sizeof("never")-1, count))) { |
| 271 | clear_bit(enabled, &transparent_hugepage_flags); |
| 272 | clear_bit(req_madv, &transparent_hugepage_flags); |
| 273 | } else |
| 274 | return -EINVAL; |
| 275 | |
| 276 | return count; |
| 277 | } |
| 278 | |
| 279 | static ssize_t enabled_show(struct kobject *kobj, |
| 280 | struct kobj_attribute *attr, char *buf) |
| 281 | { |
| 282 | return double_flag_show(kobj, attr, buf, |
| 283 | TRANSPARENT_HUGEPAGE_FLAG, |
| 284 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG); |
| 285 | } |
| 286 | static ssize_t enabled_store(struct kobject *kobj, |
| 287 | struct kobj_attribute *attr, |
| 288 | const char *buf, size_t count) |
| 289 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 290 | ssize_t ret; |
| 291 | |
| 292 | ret = double_flag_store(kobj, attr, buf, count, |
| 293 | TRANSPARENT_HUGEPAGE_FLAG, |
| 294 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG); |
| 295 | |
| 296 | if (ret > 0) { |
Xiao Guangrong | 911891a | 2012-10-08 16:29:41 -0700 | [diff] [blame] | 297 | int err; |
| 298 | |
| 299 | mutex_lock(&khugepaged_mutex); |
| 300 | err = start_khugepaged(); |
| 301 | mutex_unlock(&khugepaged_mutex); |
| 302 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 303 | if (err) |
| 304 | ret = err; |
| 305 | } |
| 306 | |
| 307 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 308 | } |
| 309 | static struct kobj_attribute enabled_attr = |
| 310 | __ATTR(enabled, 0644, enabled_show, enabled_store); |
| 311 | |
| 312 | static ssize_t single_flag_show(struct kobject *kobj, |
| 313 | struct kobj_attribute *attr, char *buf, |
| 314 | enum transparent_hugepage_flag flag) |
| 315 | { |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 316 | return sprintf(buf, "%d\n", |
| 317 | !!test_bit(flag, &transparent_hugepage_flags)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 318 | } |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 319 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 320 | static ssize_t single_flag_store(struct kobject *kobj, |
| 321 | struct kobj_attribute *attr, |
| 322 | const char *buf, size_t count, |
| 323 | enum transparent_hugepage_flag flag) |
| 324 | { |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 325 | unsigned long value; |
| 326 | int ret; |
| 327 | |
| 328 | ret = kstrtoul(buf, 10, &value); |
| 329 | if (ret < 0) |
| 330 | return ret; |
| 331 | if (value > 1) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 332 | return -EINVAL; |
| 333 | |
Ben Hutchings | e27e615 | 2011-04-14 15:22:21 -0700 | [diff] [blame] | 334 | if (value) |
| 335 | set_bit(flag, &transparent_hugepage_flags); |
| 336 | else |
| 337 | clear_bit(flag, &transparent_hugepage_flags); |
| 338 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 339 | return count; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Currently defrag only disables __GFP_NOWAIT for allocation. A blind |
| 344 | * __GFP_REPEAT is too aggressive, it's never worth swapping tons of |
| 345 | * memory just to allocate one more hugepage. |
| 346 | */ |
| 347 | static ssize_t defrag_show(struct kobject *kobj, |
| 348 | struct kobj_attribute *attr, char *buf) |
| 349 | { |
| 350 | return double_flag_show(kobj, attr, buf, |
| 351 | TRANSPARENT_HUGEPAGE_DEFRAG_FLAG, |
| 352 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG); |
| 353 | } |
| 354 | static ssize_t defrag_store(struct kobject *kobj, |
| 355 | struct kobj_attribute *attr, |
| 356 | const char *buf, size_t count) |
| 357 | { |
| 358 | return double_flag_store(kobj, attr, buf, count, |
| 359 | TRANSPARENT_HUGEPAGE_DEFRAG_FLAG, |
| 360 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG); |
| 361 | } |
| 362 | static struct kobj_attribute defrag_attr = |
| 363 | __ATTR(defrag, 0644, defrag_show, defrag_store); |
| 364 | |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 365 | static ssize_t use_zero_page_show(struct kobject *kobj, |
| 366 | struct kobj_attribute *attr, char *buf) |
| 367 | { |
| 368 | return single_flag_show(kobj, attr, buf, |
| 369 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
| 370 | } |
| 371 | static ssize_t use_zero_page_store(struct kobject *kobj, |
| 372 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 373 | { |
| 374 | return single_flag_store(kobj, attr, buf, count, |
| 375 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
| 376 | } |
| 377 | static struct kobj_attribute use_zero_page_attr = |
| 378 | __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 379 | #ifdef CONFIG_DEBUG_VM |
| 380 | static ssize_t debug_cow_show(struct kobject *kobj, |
| 381 | struct kobj_attribute *attr, char *buf) |
| 382 | { |
| 383 | return single_flag_show(kobj, attr, buf, |
| 384 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); |
| 385 | } |
| 386 | static ssize_t debug_cow_store(struct kobject *kobj, |
| 387 | struct kobj_attribute *attr, |
| 388 | const char *buf, size_t count) |
| 389 | { |
| 390 | return single_flag_store(kobj, attr, buf, count, |
| 391 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); |
| 392 | } |
| 393 | static struct kobj_attribute debug_cow_attr = |
| 394 | __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store); |
| 395 | #endif /* CONFIG_DEBUG_VM */ |
| 396 | |
| 397 | static struct attribute *hugepage_attr[] = { |
| 398 | &enabled_attr.attr, |
| 399 | &defrag_attr.attr, |
Kirill A. Shutemov | 79da540 | 2012-12-12 13:51:12 -0800 | [diff] [blame] | 400 | &use_zero_page_attr.attr, |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 401 | #ifdef CONFIG_DEBUG_VM |
| 402 | &debug_cow_attr.attr, |
| 403 | #endif |
| 404 | NULL, |
| 405 | }; |
| 406 | |
| 407 | static struct attribute_group hugepage_attr_group = { |
| 408 | .attrs = hugepage_attr, |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, |
| 412 | struct kobj_attribute *attr, |
| 413 | char *buf) |
| 414 | { |
| 415 | return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs); |
| 416 | } |
| 417 | |
| 418 | static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, |
| 419 | struct kobj_attribute *attr, |
| 420 | const char *buf, size_t count) |
| 421 | { |
| 422 | unsigned long msecs; |
| 423 | int err; |
| 424 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 425 | err = kstrtoul(buf, 10, &msecs); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 426 | if (err || msecs > UINT_MAX) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | khugepaged_scan_sleep_millisecs = msecs; |
| 430 | wake_up_interruptible(&khugepaged_wait); |
| 431 | |
| 432 | return count; |
| 433 | } |
| 434 | static struct kobj_attribute scan_sleep_millisecs_attr = |
| 435 | __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show, |
| 436 | scan_sleep_millisecs_store); |
| 437 | |
| 438 | static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj, |
| 439 | struct kobj_attribute *attr, |
| 440 | char *buf) |
| 441 | { |
| 442 | return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs); |
| 443 | } |
| 444 | |
| 445 | static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, |
| 446 | struct kobj_attribute *attr, |
| 447 | const char *buf, size_t count) |
| 448 | { |
| 449 | unsigned long msecs; |
| 450 | int err; |
| 451 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 452 | err = kstrtoul(buf, 10, &msecs); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 453 | if (err || msecs > UINT_MAX) |
| 454 | return -EINVAL; |
| 455 | |
| 456 | khugepaged_alloc_sleep_millisecs = msecs; |
| 457 | wake_up_interruptible(&khugepaged_wait); |
| 458 | |
| 459 | return count; |
| 460 | } |
| 461 | static struct kobj_attribute alloc_sleep_millisecs_attr = |
| 462 | __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show, |
| 463 | alloc_sleep_millisecs_store); |
| 464 | |
| 465 | static ssize_t pages_to_scan_show(struct kobject *kobj, |
| 466 | struct kobj_attribute *attr, |
| 467 | char *buf) |
| 468 | { |
| 469 | return sprintf(buf, "%u\n", khugepaged_pages_to_scan); |
| 470 | } |
| 471 | static ssize_t pages_to_scan_store(struct kobject *kobj, |
| 472 | struct kobj_attribute *attr, |
| 473 | const char *buf, size_t count) |
| 474 | { |
| 475 | int err; |
| 476 | unsigned long pages; |
| 477 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 478 | err = kstrtoul(buf, 10, &pages); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 479 | if (err || !pages || pages > UINT_MAX) |
| 480 | return -EINVAL; |
| 481 | |
| 482 | khugepaged_pages_to_scan = pages; |
| 483 | |
| 484 | return count; |
| 485 | } |
| 486 | static struct kobj_attribute pages_to_scan_attr = |
| 487 | __ATTR(pages_to_scan, 0644, pages_to_scan_show, |
| 488 | pages_to_scan_store); |
| 489 | |
| 490 | static ssize_t pages_collapsed_show(struct kobject *kobj, |
| 491 | struct kobj_attribute *attr, |
| 492 | char *buf) |
| 493 | { |
| 494 | return sprintf(buf, "%u\n", khugepaged_pages_collapsed); |
| 495 | } |
| 496 | static struct kobj_attribute pages_collapsed_attr = |
| 497 | __ATTR_RO(pages_collapsed); |
| 498 | |
| 499 | static ssize_t full_scans_show(struct kobject *kobj, |
| 500 | struct kobj_attribute *attr, |
| 501 | char *buf) |
| 502 | { |
| 503 | return sprintf(buf, "%u\n", khugepaged_full_scans); |
| 504 | } |
| 505 | static struct kobj_attribute full_scans_attr = |
| 506 | __ATTR_RO(full_scans); |
| 507 | |
| 508 | static ssize_t khugepaged_defrag_show(struct kobject *kobj, |
| 509 | struct kobj_attribute *attr, char *buf) |
| 510 | { |
| 511 | return single_flag_show(kobj, attr, buf, |
| 512 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
| 513 | } |
| 514 | static ssize_t khugepaged_defrag_store(struct kobject *kobj, |
| 515 | struct kobj_attribute *attr, |
| 516 | const char *buf, size_t count) |
| 517 | { |
| 518 | return single_flag_store(kobj, attr, buf, count, |
| 519 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); |
| 520 | } |
| 521 | static struct kobj_attribute khugepaged_defrag_attr = |
| 522 | __ATTR(defrag, 0644, khugepaged_defrag_show, |
| 523 | khugepaged_defrag_store); |
| 524 | |
| 525 | /* |
| 526 | * max_ptes_none controls if khugepaged should collapse hugepages over |
| 527 | * any unmapped ptes in turn potentially increasing the memory |
| 528 | * footprint of the vmas. When max_ptes_none is 0 khugepaged will not |
| 529 | * reduce the available free memory in the system as it |
| 530 | * runs. Increasing max_ptes_none will instead potentially reduce the |
| 531 | * free memory in the system during the khugepaged scan. |
| 532 | */ |
| 533 | static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj, |
| 534 | struct kobj_attribute *attr, |
| 535 | char *buf) |
| 536 | { |
| 537 | return sprintf(buf, "%u\n", khugepaged_max_ptes_none); |
| 538 | } |
| 539 | static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj, |
| 540 | struct kobj_attribute *attr, |
| 541 | const char *buf, size_t count) |
| 542 | { |
| 543 | int err; |
| 544 | unsigned long max_ptes_none; |
| 545 | |
Jingoo Han | 3dbb95f | 2013-09-11 14:20:25 -0700 | [diff] [blame] | 546 | err = kstrtoul(buf, 10, &max_ptes_none); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 547 | if (err || max_ptes_none > HPAGE_PMD_NR-1) |
| 548 | return -EINVAL; |
| 549 | |
| 550 | khugepaged_max_ptes_none = max_ptes_none; |
| 551 | |
| 552 | return count; |
| 553 | } |
| 554 | static struct kobj_attribute khugepaged_max_ptes_none_attr = |
| 555 | __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show, |
| 556 | khugepaged_max_ptes_none_store); |
| 557 | |
| 558 | static struct attribute *khugepaged_attr[] = { |
| 559 | &khugepaged_defrag_attr.attr, |
| 560 | &khugepaged_max_ptes_none_attr.attr, |
| 561 | &pages_to_scan_attr.attr, |
| 562 | &pages_collapsed_attr.attr, |
| 563 | &full_scans_attr.attr, |
| 564 | &scan_sleep_millisecs_attr.attr, |
| 565 | &alloc_sleep_millisecs_attr.attr, |
| 566 | NULL, |
| 567 | }; |
| 568 | |
| 569 | static struct attribute_group khugepaged_attr_group = { |
| 570 | .attrs = khugepaged_attr, |
| 571 | .name = "khugepaged", |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 572 | }; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 573 | |
| 574 | static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) |
| 575 | { |
| 576 | int err; |
| 577 | |
| 578 | *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); |
| 579 | if (unlikely(!*hugepage_kobj)) { |
Jeremy Eder | 2c79737 | 2012-12-20 15:05:32 -0800 | [diff] [blame] | 580 | printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 581 | return -ENOMEM; |
| 582 | } |
| 583 | |
| 584 | err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group); |
| 585 | if (err) { |
Jeremy Eder | 2c79737 | 2012-12-20 15:05:32 -0800 | [diff] [blame] | 586 | printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 587 | goto delete_obj; |
| 588 | } |
| 589 | |
| 590 | err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group); |
| 591 | if (err) { |
Jeremy Eder | 2c79737 | 2012-12-20 15:05:32 -0800 | [diff] [blame] | 592 | printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n"); |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 593 | goto remove_hp_group; |
| 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | remove_hp_group: |
| 599 | sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group); |
| 600 | delete_obj: |
| 601 | kobject_put(*hugepage_kobj); |
| 602 | return err; |
| 603 | } |
| 604 | |
| 605 | static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj) |
| 606 | { |
| 607 | sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group); |
| 608 | sysfs_remove_group(hugepage_kobj, &hugepage_attr_group); |
| 609 | kobject_put(hugepage_kobj); |
| 610 | } |
| 611 | #else |
| 612 | static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj) |
| 613 | { |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) |
| 618 | { |
| 619 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 620 | #endif /* CONFIG_SYSFS */ |
| 621 | |
| 622 | static int __init hugepage_init(void) |
| 623 | { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 624 | int err; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 625 | struct kobject *hugepage_kobj; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 626 | |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 627 | if (!has_transparent_hugepage()) { |
| 628 | transparent_hugepage_flags = 0; |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 629 | return -EINVAL; |
Andrea Arcangeli | 4b7167b | 2011-01-13 15:47:09 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 632 | err = hugepage_init_sysfs(&hugepage_kobj); |
| 633 | if (err) |
| 634 | return err; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 635 | |
| 636 | err = khugepaged_slab_init(); |
| 637 | if (err) |
| 638 | goto out; |
| 639 | |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 640 | register_shrinker(&huge_zero_page_shrinker); |
| 641 | |
Rik van Riel | 97562cd | 2011-01-13 15:47:12 -0800 | [diff] [blame] | 642 | /* |
| 643 | * By default disable transparent hugepages on smaller systems, |
| 644 | * where the extra memory used could hurt more than TLB overhead |
| 645 | * is likely to save. The admin can still enable it through /sys. |
| 646 | */ |
| 647 | if (totalram_pages < (512 << (20 - PAGE_SHIFT))) |
| 648 | transparent_hugepage_flags = 0; |
| 649 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 650 | start_khugepaged(); |
| 651 | |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 652 | return 0; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 653 | out: |
Shaohua Li | 569e559 | 2012-01-12 17:19:11 -0800 | [diff] [blame] | 654 | hugepage_exit_sysfs(hugepage_kobj); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 655 | return err; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 656 | } |
| 657 | module_init(hugepage_init) |
| 658 | |
| 659 | static int __init setup_transparent_hugepage(char *str) |
| 660 | { |
| 661 | int ret = 0; |
| 662 | if (!str) |
| 663 | goto out; |
| 664 | if (!strcmp(str, "always")) { |
| 665 | set_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 666 | &transparent_hugepage_flags); |
| 667 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 668 | &transparent_hugepage_flags); |
| 669 | ret = 1; |
| 670 | } else if (!strcmp(str, "madvise")) { |
| 671 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 672 | &transparent_hugepage_flags); |
| 673 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 674 | &transparent_hugepage_flags); |
| 675 | ret = 1; |
| 676 | } else if (!strcmp(str, "never")) { |
| 677 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, |
| 678 | &transparent_hugepage_flags); |
| 679 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, |
| 680 | &transparent_hugepage_flags); |
| 681 | ret = 1; |
| 682 | } |
| 683 | out: |
| 684 | if (!ret) |
| 685 | printk(KERN_WARNING |
| 686 | "transparent_hugepage= cannot parse, ignored\n"); |
| 687 | return ret; |
| 688 | } |
| 689 | __setup("transparent_hugepage=", setup_transparent_hugepage); |
| 690 | |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 691 | pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 692 | { |
| 693 | if (likely(vma->vm_flags & VM_WRITE)) |
| 694 | pmd = pmd_mkwrite(pmd); |
| 695 | return pmd; |
| 696 | } |
| 697 | |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 698 | static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot) |
Bob Liu | b3092b3 | 2012-12-11 16:00:41 -0800 | [diff] [blame] | 699 | { |
| 700 | pmd_t entry; |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 701 | entry = mk_pmd(page, prot); |
Bob Liu | b3092b3 | 2012-12-11 16:00:41 -0800 | [diff] [blame] | 702 | entry = pmd_mkhuge(entry); |
| 703 | return entry; |
| 704 | } |
| 705 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 706 | static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, |
| 707 | struct vm_area_struct *vma, |
| 708 | unsigned long haddr, pmd_t *pmd, |
| 709 | struct page *page) |
| 710 | { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 711 | pgtable_t pgtable; |
| 712 | |
| 713 | VM_BUG_ON(!PageCompound(page)); |
| 714 | pgtable = pte_alloc_one(mm, haddr); |
David Rientjes | edad9d2 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 715 | if (unlikely(!pgtable)) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 716 | return VM_FAULT_OOM; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 717 | |
| 718 | clear_huge_page(page, haddr, HPAGE_PMD_NR); |
Minchan Kim | 52f3762 | 2013-04-29 15:08:15 -0700 | [diff] [blame] | 719 | /* |
| 720 | * The memory barrier inside __SetPageUptodate makes sure that |
| 721 | * clear_huge_page writes become visible before the set_pmd_at() |
| 722 | * write. |
| 723 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 724 | __SetPageUptodate(page); |
| 725 | |
| 726 | spin_lock(&mm->page_table_lock); |
| 727 | if (unlikely(!pmd_none(*pmd))) { |
| 728 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 729 | mem_cgroup_uncharge_page(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 730 | put_page(page); |
| 731 | pte_free(mm, pgtable); |
| 732 | } else { |
| 733 | pmd_t entry; |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 734 | entry = mk_huge_pmd(page, vma->vm_page_prot); |
| 735 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 736 | page_add_new_anon_rmap(page, vma, haddr); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 737 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 738 | set_pmd_at(mm, haddr, pmd, entry); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 739 | add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR); |
Andrea Arcangeli | 1c641e8 | 2012-03-05 14:59:20 -0800 | [diff] [blame] | 740 | mm->nr_ptes++; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 741 | spin_unlock(&mm->page_table_lock); |
| 742 | } |
| 743 | |
David Rientjes | aa2e878 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 744 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 747 | static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 748 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 749 | return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp; |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static inline struct page *alloc_hugepage_vma(int defrag, |
| 753 | struct vm_area_struct *vma, |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 754 | unsigned long haddr, int nd, |
| 755 | gfp_t extra_gfp) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 756 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 757 | return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp), |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 758 | HPAGE_PMD_ORDER, vma, haddr, nd); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | #ifndef CONFIG_NUMA |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 762 | static inline struct page *alloc_hugepage(int defrag) |
| 763 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 764 | return alloc_pages(alloc_hugepage_gfpmask(defrag, 0), |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 765 | HPAGE_PMD_ORDER); |
| 766 | } |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 767 | #endif |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 768 | |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 769 | static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 770 | struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 771 | struct page *zero_page) |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 772 | { |
| 773 | pmd_t entry; |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 774 | if (!pmd_none(*pmd)) |
| 775 | return false; |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 776 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 777 | entry = pmd_wrprotect(entry); |
| 778 | entry = pmd_mkhuge(entry); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 779 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 780 | set_pmd_at(mm, haddr, pmd, entry); |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 781 | mm->nr_ptes++; |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 782 | return true; |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 783 | } |
| 784 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 785 | int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 786 | unsigned long address, pmd_t *pmd, |
| 787 | unsigned int flags) |
| 788 | { |
| 789 | struct page *page; |
| 790 | unsigned long haddr = address & HPAGE_PMD_MASK; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 791 | |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 792 | if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 793 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 794 | if (unlikely(anon_vma_prepare(vma))) |
| 795 | return VM_FAULT_OOM; |
| 796 | if (unlikely(khugepaged_enter(vma))) |
| 797 | return VM_FAULT_OOM; |
| 798 | if (!(flags & FAULT_FLAG_WRITE) && |
| 799 | transparent_hugepage_use_zero_page()) { |
| 800 | pgtable_t pgtable; |
| 801 | struct page *zero_page; |
| 802 | bool set; |
| 803 | pgtable = pte_alloc_one(mm, haddr); |
| 804 | if (unlikely(!pgtable)) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 805 | return VM_FAULT_OOM; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 806 | zero_page = get_huge_zero_page(); |
| 807 | if (unlikely(!zero_page)) { |
| 808 | pte_free(mm, pgtable); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 809 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 810 | return VM_FAULT_FALLBACK; |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 811 | } |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 812 | spin_lock(&mm->page_table_lock); |
| 813 | set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd, |
| 814 | zero_page); |
| 815 | spin_unlock(&mm->page_table_lock); |
| 816 | if (!set) { |
| 817 | pte_free(mm, pgtable); |
| 818 | put_huge_zero_page(); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 819 | } |
David Rientjes | edad9d2 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 820 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 821 | } |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 822 | page = alloc_hugepage_vma(transparent_hugepage_defrag(vma), |
| 823 | vma, haddr, numa_node_id(), 0); |
| 824 | if (unlikely(!page)) { |
| 825 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 826 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 827 | } |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 828 | if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) { |
| 829 | put_page(page); |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 830 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 831 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 832 | } |
| 833 | if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page))) { |
| 834 | mem_cgroup_uncharge_page(page); |
| 835 | put_page(page); |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 836 | count_vm_event(THP_FAULT_FALLBACK); |
Kirill A. Shutemov | c029255 | 2013-09-12 15:14:05 -0700 | [diff] [blame] | 837 | return VM_FAULT_FALLBACK; |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 838 | } |
| 839 | |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 840 | count_vm_event(THP_FAULT_ALLOC); |
Kirill A. Shutemov | 128ec03 | 2013-09-12 15:14:03 -0700 | [diff] [blame] | 841 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
| 845 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, |
| 846 | struct vm_area_struct *vma) |
| 847 | { |
| 848 | struct page *src_page; |
| 849 | pmd_t pmd; |
| 850 | pgtable_t pgtable; |
| 851 | int ret; |
| 852 | |
| 853 | ret = -ENOMEM; |
| 854 | pgtable = pte_alloc_one(dst_mm, addr); |
| 855 | if (unlikely(!pgtable)) |
| 856 | goto out; |
| 857 | |
| 858 | spin_lock(&dst_mm->page_table_lock); |
| 859 | spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING); |
| 860 | |
| 861 | ret = -EAGAIN; |
| 862 | pmd = *src_pmd; |
| 863 | if (unlikely(!pmd_trans_huge(pmd))) { |
| 864 | pte_free(dst_mm, pgtable); |
| 865 | goto out_unlock; |
| 866 | } |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 867 | /* |
| 868 | * mm->page_table_lock is enough to be sure that huge zero pmd is not |
| 869 | * under splitting since we don't split the page itself, only pmd to |
| 870 | * a page table. |
| 871 | */ |
| 872 | if (is_huge_zero_pmd(pmd)) { |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 873 | struct page *zero_page; |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 874 | bool set; |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 875 | /* |
| 876 | * get_huge_zero_page() will never allocate a new page here, |
| 877 | * since we already have a zero page to copy. It just takes a |
| 878 | * reference. |
| 879 | */ |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 880 | zero_page = get_huge_zero_page(); |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 881 | set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd, |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 882 | zero_page); |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 883 | BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */ |
Kirill A. Shutemov | fc9fe82 | 2012-12-12 13:50:51 -0800 | [diff] [blame] | 884 | ret = 0; |
| 885 | goto out_unlock; |
| 886 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 887 | if (unlikely(pmd_trans_splitting(pmd))) { |
| 888 | /* split huge page running from under us */ |
| 889 | spin_unlock(&src_mm->page_table_lock); |
| 890 | spin_unlock(&dst_mm->page_table_lock); |
| 891 | pte_free(dst_mm, pgtable); |
| 892 | |
| 893 | wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */ |
| 894 | goto out; |
| 895 | } |
| 896 | src_page = pmd_page(pmd); |
| 897 | VM_BUG_ON(!PageHead(src_page)); |
| 898 | get_page(src_page); |
| 899 | page_dup_rmap(src_page); |
| 900 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
| 901 | |
| 902 | pmdp_set_wrprotect(src_mm, addr, src_pmd); |
| 903 | pmd = pmd_mkold(pmd_wrprotect(pmd)); |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 904 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 905 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); |
Andrea Arcangeli | 1c641e8 | 2012-03-05 14:59:20 -0800 | [diff] [blame] | 906 | dst_mm->nr_ptes++; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 907 | |
| 908 | ret = 0; |
| 909 | out_unlock: |
| 910 | spin_unlock(&src_mm->page_table_lock); |
| 911 | spin_unlock(&dst_mm->page_table_lock); |
| 912 | out: |
| 913 | return ret; |
| 914 | } |
| 915 | |
Will Deacon | a1dd450 | 2012-12-11 16:01:27 -0800 | [diff] [blame] | 916 | void huge_pmd_set_accessed(struct mm_struct *mm, |
| 917 | struct vm_area_struct *vma, |
| 918 | unsigned long address, |
| 919 | pmd_t *pmd, pmd_t orig_pmd, |
| 920 | int dirty) |
| 921 | { |
| 922 | pmd_t entry; |
| 923 | unsigned long haddr; |
| 924 | |
| 925 | spin_lock(&mm->page_table_lock); |
| 926 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 927 | goto unlock; |
| 928 | |
| 929 | entry = pmd_mkyoung(orig_pmd); |
| 930 | haddr = address & HPAGE_PMD_MASK; |
| 931 | if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty)) |
| 932 | update_mmu_cache_pmd(vma, address, pmd); |
| 933 | |
| 934 | unlock: |
| 935 | spin_unlock(&mm->page_table_lock); |
| 936 | } |
| 937 | |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 938 | static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm, |
| 939 | struct vm_area_struct *vma, unsigned long address, |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 940 | pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr) |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 941 | { |
| 942 | pgtable_t pgtable; |
| 943 | pmd_t _pmd; |
| 944 | struct page *page; |
| 945 | int i, ret = 0; |
| 946 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 947 | unsigned long mmun_end; /* For mmu_notifiers */ |
| 948 | |
| 949 | page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address); |
| 950 | if (!page) { |
| 951 | ret |= VM_FAULT_OOM; |
| 952 | goto out; |
| 953 | } |
| 954 | |
| 955 | if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) { |
| 956 | put_page(page); |
| 957 | ret |= VM_FAULT_OOM; |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | clear_user_highpage(page, address); |
| 962 | __SetPageUptodate(page); |
| 963 | |
| 964 | mmun_start = haddr; |
| 965 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 966 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 967 | |
| 968 | spin_lock(&mm->page_table_lock); |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 969 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 970 | goto out_free_page; |
| 971 | |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 972 | pmdp_clear_flush(vma, haddr, pmd); |
| 973 | /* leave pmd empty until pte is filled */ |
| 974 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 975 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 976 | pmd_populate(mm, &_pmd, pgtable); |
| 977 | |
| 978 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 979 | pte_t *pte, entry; |
| 980 | if (haddr == (address & PAGE_MASK)) { |
| 981 | entry = mk_pte(page, vma->vm_page_prot); |
| 982 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 983 | page_add_new_anon_rmap(page, vma, haddr); |
| 984 | } else { |
| 985 | entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot); |
| 986 | entry = pte_mkspecial(entry); |
| 987 | } |
| 988 | pte = pte_offset_map(&_pmd, haddr); |
| 989 | VM_BUG_ON(!pte_none(*pte)); |
| 990 | set_pte_at(mm, haddr, pte, entry); |
| 991 | pte_unmap(pte); |
| 992 | } |
| 993 | smp_wmb(); /* make pte visible before pmd */ |
| 994 | pmd_populate(mm, pmd, pgtable); |
| 995 | spin_unlock(&mm->page_table_lock); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 996 | put_huge_zero_page(); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 997 | inc_mm_counter(mm, MM_ANONPAGES); |
| 998 | |
| 999 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1000 | |
| 1001 | ret |= VM_FAULT_WRITE; |
| 1002 | out: |
| 1003 | return ret; |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 1004 | out_free_page: |
| 1005 | spin_unlock(&mm->page_table_lock); |
| 1006 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1007 | mem_cgroup_uncharge_page(page); |
| 1008 | put_page(page); |
| 1009 | goto out; |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1012 | static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, |
| 1013 | struct vm_area_struct *vma, |
| 1014 | unsigned long address, |
| 1015 | pmd_t *pmd, pmd_t orig_pmd, |
| 1016 | struct page *page, |
| 1017 | unsigned long haddr) |
| 1018 | { |
| 1019 | pgtable_t pgtable; |
| 1020 | pmd_t _pmd; |
| 1021 | int ret = 0, i; |
| 1022 | struct page **pages; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1023 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 1024 | unsigned long mmun_end; /* For mmu_notifiers */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1025 | |
| 1026 | pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR, |
| 1027 | GFP_KERNEL); |
| 1028 | if (unlikely(!pages)) { |
| 1029 | ret |= VM_FAULT_OOM; |
| 1030 | goto out; |
| 1031 | } |
| 1032 | |
| 1033 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 1034 | pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE | |
| 1035 | __GFP_OTHER_NODE, |
Andi Kleen | 19ee151 | 2011-03-04 17:36:31 -0800 | [diff] [blame] | 1036 | vma, address, page_to_nid(page)); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1037 | if (unlikely(!pages[i] || |
| 1038 | mem_cgroup_newpage_charge(pages[i], mm, |
| 1039 | GFP_KERNEL))) { |
| 1040 | if (pages[i]) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1041 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1042 | mem_cgroup_uncharge_start(); |
| 1043 | while (--i >= 0) { |
| 1044 | mem_cgroup_uncharge_page(pages[i]); |
| 1045 | put_page(pages[i]); |
| 1046 | } |
| 1047 | mem_cgroup_uncharge_end(); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1048 | kfree(pages); |
| 1049 | ret |= VM_FAULT_OOM; |
| 1050 | goto out; |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 1055 | copy_user_highpage(pages[i], page + i, |
Hillf Danton | 0089e48 | 2011-10-31 17:09:38 -0700 | [diff] [blame] | 1056 | haddr + PAGE_SIZE * i, vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1057 | __SetPageUptodate(pages[i]); |
| 1058 | cond_resched(); |
| 1059 | } |
| 1060 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1061 | mmun_start = haddr; |
| 1062 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 1063 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 1064 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1065 | spin_lock(&mm->page_table_lock); |
| 1066 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 1067 | goto out_free_pages; |
| 1068 | VM_BUG_ON(!PageHead(page)); |
| 1069 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1070 | pmdp_clear_flush(vma, haddr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1071 | /* leave pmd empty until pte is filled */ |
| 1072 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1073 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1074 | pmd_populate(mm, &_pmd, pgtable); |
| 1075 | |
| 1076 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 1077 | pte_t *pte, entry; |
| 1078 | entry = mk_pte(pages[i], vma->vm_page_prot); |
| 1079 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1080 | page_add_new_anon_rmap(pages[i], vma, haddr); |
| 1081 | pte = pte_offset_map(&_pmd, haddr); |
| 1082 | VM_BUG_ON(!pte_none(*pte)); |
| 1083 | set_pte_at(mm, haddr, pte, entry); |
| 1084 | pte_unmap(pte); |
| 1085 | } |
| 1086 | kfree(pages); |
| 1087 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1088 | smp_wmb(); /* make pte visible before pmd */ |
| 1089 | pmd_populate(mm, pmd, pgtable); |
| 1090 | page_remove_rmap(page); |
| 1091 | spin_unlock(&mm->page_table_lock); |
| 1092 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1093 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1094 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1095 | ret |= VM_FAULT_WRITE; |
| 1096 | put_page(page); |
| 1097 | |
| 1098 | out: |
| 1099 | return ret; |
| 1100 | |
| 1101 | out_free_pages: |
| 1102 | spin_unlock(&mm->page_table_lock); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1103 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1104 | mem_cgroup_uncharge_start(); |
| 1105 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 1106 | mem_cgroup_uncharge_page(pages[i]); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1107 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1108 | } |
| 1109 | mem_cgroup_uncharge_end(); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1110 | kfree(pages); |
| 1111 | goto out; |
| 1112 | } |
| 1113 | |
| 1114 | int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1115 | unsigned long address, pmd_t *pmd, pmd_t orig_pmd) |
| 1116 | { |
| 1117 | int ret = 0; |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1118 | struct page *page = NULL, *new_page; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1119 | unsigned long haddr; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1120 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 1121 | unsigned long mmun_end; /* For mmu_notifiers */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1122 | |
| 1123 | VM_BUG_ON(!vma->anon_vma); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1124 | haddr = address & HPAGE_PMD_MASK; |
| 1125 | if (is_huge_zero_pmd(orig_pmd)) |
| 1126 | goto alloc; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1127 | spin_lock(&mm->page_table_lock); |
| 1128 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 1129 | goto out_unlock; |
| 1130 | |
| 1131 | page = pmd_page(orig_pmd); |
| 1132 | VM_BUG_ON(!PageCompound(page) || !PageHead(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1133 | if (page_mapcount(page) == 1) { |
| 1134 | pmd_t entry; |
| 1135 | entry = pmd_mkyoung(orig_pmd); |
| 1136 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
| 1137 | if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1)) |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 1138 | update_mmu_cache_pmd(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1139 | ret |= VM_FAULT_WRITE; |
| 1140 | goto out_unlock; |
| 1141 | } |
| 1142 | get_page(page); |
| 1143 | spin_unlock(&mm->page_table_lock); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1144 | alloc: |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1145 | if (transparent_hugepage_enabled(vma) && |
| 1146 | !transparent_hugepage_debug_cow()) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 1147 | new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma), |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 1148 | vma, haddr, numa_node_id(), 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1149 | else |
| 1150 | new_page = NULL; |
| 1151 | |
| 1152 | if (unlikely(!new_page)) { |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1153 | if (is_huge_zero_pmd(orig_pmd)) { |
| 1154 | ret = do_huge_pmd_wp_zero_page_fallback(mm, vma, |
Kirill A. Shutemov | 3ea41e6 | 2012-12-12 13:51:14 -0800 | [diff] [blame] | 1155 | address, pmd, orig_pmd, haddr); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1156 | } else { |
| 1157 | ret = do_huge_pmd_wp_page_fallback(mm, vma, address, |
| 1158 | pmd, orig_pmd, page, haddr); |
| 1159 | if (ret & VM_FAULT_OOM) |
| 1160 | split_huge_page(page); |
| 1161 | put_page(page); |
| 1162 | } |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1163 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1164 | goto out; |
| 1165 | } |
| 1166 | |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1167 | if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) { |
| 1168 | put_page(new_page); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1169 | if (page) { |
| 1170 | split_huge_page(page); |
| 1171 | put_page(page); |
| 1172 | } |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1173 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1174 | ret |= VM_FAULT_OOM; |
| 1175 | goto out; |
| 1176 | } |
| 1177 | |
David Rientjes | 17766dd | 2013-09-12 15:14:06 -0700 | [diff] [blame] | 1178 | count_vm_event(THP_FAULT_ALLOC); |
| 1179 | |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1180 | if (is_huge_zero_pmd(orig_pmd)) |
| 1181 | clear_huge_page(new_page, haddr, HPAGE_PMD_NR); |
| 1182 | else |
| 1183 | copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1184 | __SetPageUptodate(new_page); |
| 1185 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1186 | mmun_start = haddr; |
| 1187 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 1188 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
| 1189 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1190 | spin_lock(&mm->page_table_lock); |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1191 | if (page) |
| 1192 | put_page(page); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1193 | if (unlikely(!pmd_same(*pmd, orig_pmd))) { |
David Rientjes | 6f60b69 | 2012-05-29 15:06:26 -0700 | [diff] [blame] | 1194 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1195 | mem_cgroup_uncharge_page(new_page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1196 | put_page(new_page); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1197 | goto out_mn; |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 1198 | } else { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1199 | pmd_t entry; |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 1200 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
| 1201 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1202 | pmdp_clear_flush(vma, haddr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1203 | page_add_new_anon_rmap(new_page, vma, haddr); |
| 1204 | set_pmd_at(mm, haddr, pmd, entry); |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 1205 | update_mmu_cache_pmd(vma, address, pmd); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 1206 | if (is_huge_zero_pmd(orig_pmd)) { |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1207 | add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 1208 | put_huge_zero_page(); |
| 1209 | } else { |
Kirill A. Shutemov | 93b4796 | 2012-12-12 13:50:54 -0800 | [diff] [blame] | 1210 | VM_BUG_ON(!PageHead(page)); |
| 1211 | page_remove_rmap(page); |
| 1212 | put_page(page); |
| 1213 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1214 | ret |= VM_FAULT_WRITE; |
| 1215 | } |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1216 | spin_unlock(&mm->page_table_lock); |
| 1217 | out_mn: |
| 1218 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 1219 | out: |
| 1220 | return ret; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1221 | out_unlock: |
| 1222 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1223 | return ret; |
| 1224 | } |
| 1225 | |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1226 | struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1227 | unsigned long addr, |
| 1228 | pmd_t *pmd, |
| 1229 | unsigned int flags) |
| 1230 | { |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1231 | struct mm_struct *mm = vma->vm_mm; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1232 | struct page *page = NULL; |
| 1233 | |
| 1234 | assert_spin_locked(&mm->page_table_lock); |
| 1235 | |
| 1236 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) |
| 1237 | goto out; |
| 1238 | |
Kirill A. Shutemov | 85facf2 | 2013-02-04 14:28:42 -0800 | [diff] [blame] | 1239 | /* Avoid dumping huge zero page */ |
| 1240 | if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) |
| 1241 | return ERR_PTR(-EFAULT); |
| 1242 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1243 | page = pmd_page(*pmd); |
| 1244 | VM_BUG_ON(!PageHead(page)); |
| 1245 | if (flags & FOLL_TOUCH) { |
| 1246 | pmd_t _pmd; |
| 1247 | /* |
| 1248 | * We should set the dirty bit only for FOLL_WRITE but |
| 1249 | * for now the dirty bit in the pmd is meaningless. |
| 1250 | * And if the dirty bit will become meaningful and |
| 1251 | * we'll only set it with FOLL_WRITE, an atomic |
| 1252 | * set_bit will be required on the pmd to set the |
| 1253 | * young bit, instead of the current set_pmd_at. |
| 1254 | */ |
| 1255 | _pmd = pmd_mkyoung(pmd_mkdirty(*pmd)); |
Aneesh Kumar K.V | 8663890a | 2013-06-06 00:20:34 -0700 | [diff] [blame] | 1256 | if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK, |
| 1257 | pmd, _pmd, 1)) |
| 1258 | update_mmu_cache_pmd(vma, addr, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1259 | } |
David Rientjes | b676b29 | 2012-10-08 16:34:03 -0700 | [diff] [blame] | 1260 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
| 1261 | if (page->mapping && trylock_page(page)) { |
| 1262 | lru_add_drain(); |
| 1263 | if (page->mapping) |
| 1264 | mlock_vma_page(page); |
| 1265 | unlock_page(page); |
| 1266 | } |
| 1267 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1268 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; |
| 1269 | VM_BUG_ON(!PageCompound(page)); |
| 1270 | if (flags & FOLL_GET) |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1271 | get_page_foll(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1272 | |
| 1273 | out: |
| 1274 | return page; |
| 1275 | } |
| 1276 | |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1277 | /* NUMA hinting page fault entry point for trans huge pmds */ |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1278 | int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 1279 | unsigned long addr, pmd_t pmd, pmd_t *pmdp) |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1280 | { |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1281 | struct anon_vma *anon_vma = NULL; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1282 | struct page *page; |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1283 | unsigned long haddr = addr & HPAGE_PMD_MASK; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1284 | int page_nid = -1, this_nid = numa_node_id(); |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1285 | int target_nid, last_cpupid = -1; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1286 | bool page_locked; |
| 1287 | bool migrated = false; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1288 | int flags = 0; |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1289 | |
| 1290 | spin_lock(&mm->page_table_lock); |
| 1291 | if (unlikely(!pmd_same(pmd, *pmdp))) |
| 1292 | goto out_unlock; |
| 1293 | |
| 1294 | page = pmd_page(pmd); |
Mel Gorman | a1a4618 | 2013-10-07 11:28:50 +0100 | [diff] [blame] | 1295 | BUG_ON(is_huge_zero_page(page)); |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1296 | page_nid = page_to_nid(page); |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1297 | last_cpupid = page_cpupid_last(page); |
Mel Gorman | 03c5a6e | 2012-11-02 14:52:48 +0000 | [diff] [blame] | 1298 | count_vm_numa_event(NUMA_HINT_FAULTS); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame^] | 1299 | if (page_nid == this_nid) { |
Mel Gorman | 03c5a6e | 2012-11-02 14:52:48 +0000 | [diff] [blame] | 1300 | count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); |
Rik van Riel | 04bb2f9 | 2013-10-07 11:29:36 +0100 | [diff] [blame^] | 1301 | flags |= TNF_FAULT_LOCAL; |
| 1302 | } |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1303 | |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1304 | /* |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1305 | * Avoid grouping on DSO/COW pages in specific and RO pages |
| 1306 | * in general, RO pages shouldn't hurt as much anyway since |
| 1307 | * they can be in shared cache state. |
| 1308 | */ |
| 1309 | if (!pmd_write(pmd)) |
| 1310 | flags |= TNF_NO_GROUP; |
| 1311 | |
| 1312 | /* |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1313 | * Acquire the page lock to serialise THP migrations but avoid dropping |
| 1314 | * page_table_lock if at all possible |
| 1315 | */ |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1316 | page_locked = trylock_page(page); |
| 1317 | target_nid = mpol_misplaced(page, vma, haddr); |
| 1318 | if (target_nid == -1) { |
| 1319 | /* If the page was locked, there are no parallel migrations */ |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1320 | if (page_locked) |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1321 | goto clear_pmdnuma; |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1322 | |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1323 | /* |
| 1324 | * Otherwise wait for potential migrations and retry. We do |
| 1325 | * relock and check_same as the page may no longer be mapped. |
| 1326 | * As the fault is being retried, do not account for it. |
| 1327 | */ |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1328 | spin_unlock(&mm->page_table_lock); |
| 1329 | wait_on_page_locked(page); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1330 | page_nid = -1; |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | /* Page is misplaced, serialise migrations and parallel THP splits */ |
| 1335 | get_page(page); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1336 | spin_unlock(&mm->page_table_lock); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1337 | if (!page_locked) |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1338 | lock_page(page); |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1339 | anon_vma = page_lock_anon_vma_read(page); |
Peter Zijlstra | cbee9f8 | 2012-10-25 14:16:43 +0200 | [diff] [blame] | 1340 | |
Peter Zijlstra | c69307d | 2013-10-07 11:28:41 +0100 | [diff] [blame] | 1341 | /* Confirm the PMD did not change while page_table_lock was released */ |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1342 | spin_lock(&mm->page_table_lock); |
| 1343 | if (unlikely(!pmd_same(pmd, *pmdp))) { |
| 1344 | unlock_page(page); |
| 1345 | put_page(page); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1346 | page_nid = -1; |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1347 | goto out_unlock; |
| 1348 | } |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1349 | |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1350 | /* |
| 1351 | * Migrate the THP to the requested node, returns with page unlocked |
| 1352 | * and pmd_numa cleared. |
| 1353 | */ |
Mel Gorman | ff9042b | 2013-10-07 11:28:43 +0100 | [diff] [blame] | 1354 | spin_unlock(&mm->page_table_lock); |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1355 | migrated = migrate_misplaced_transhuge_page(mm, vma, |
Hugh Dickins | 340ef39 | 2013-02-22 16:34:33 -0800 | [diff] [blame] | 1356 | pmdp, pmd, addr, page, target_nid); |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1357 | if (migrated) { |
| 1358 | flags |= TNF_MIGRATED; |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1359 | page_nid = target_nid; |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1360 | } |
Mel Gorman | b32967f | 2012-11-19 12:35:47 +0000 | [diff] [blame] | 1361 | |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1362 | goto out; |
Mel Gorman | 4daae3b | 2012-11-02 11:33:45 +0000 | [diff] [blame] | 1363 | clear_pmdnuma: |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1364 | BUG_ON(!PageLocked(page)); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1365 | pmd = pmd_mknonnuma(pmd); |
| 1366 | set_pmd_at(mm, haddr, pmdp, pmd); |
| 1367 | VM_BUG_ON(pmd_numa(*pmdp)); |
| 1368 | update_mmu_cache_pmd(vma, addr, pmdp); |
Mel Gorman | a54a407 | 2013-10-07 11:28:46 +0100 | [diff] [blame] | 1369 | unlock_page(page); |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1370 | out_unlock: |
| 1371 | spin_unlock(&mm->page_table_lock); |
Mel Gorman | b891663 | 2013-10-07 11:28:44 +0100 | [diff] [blame] | 1372 | |
| 1373 | out: |
| 1374 | if (anon_vma) |
| 1375 | page_unlock_anon_vma_read(anon_vma); |
| 1376 | |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1377 | if (page_nid != -1) |
Peter Zijlstra | 6688cc0 | 2013-10-07 11:29:24 +0100 | [diff] [blame] | 1378 | task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags); |
Mel Gorman | 8191acb | 2013-10-07 11:28:45 +0100 | [diff] [blame] | 1379 | |
Mel Gorman | d10e63f | 2012-10-25 14:16:31 +0200 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1383 | int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
Shaohua Li | f21760b | 2012-01-12 17:19:16 -0800 | [diff] [blame] | 1384 | pmd_t *pmd, unsigned long addr) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1385 | { |
| 1386 | int ret = 0; |
| 1387 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1388 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 1389 | struct page *page; |
| 1390 | pgtable_t pgtable; |
David Miller | f5c8ad4 | 2012-10-08 16:34:26 -0700 | [diff] [blame] | 1391 | pmd_t orig_pmd; |
Aneesh Kumar K.V | a6bf2bb | 2013-06-05 17:14:04 -0700 | [diff] [blame] | 1392 | /* |
| 1393 | * For architectures like ppc64 we look at deposited pgtable |
| 1394 | * when calling pmdp_get_and_clear. So do the |
| 1395 | * pgtable_trans_huge_withdraw after finishing pmdp related |
| 1396 | * operations. |
| 1397 | */ |
David Miller | f5c8ad4 | 2012-10-08 16:34:26 -0700 | [diff] [blame] | 1398 | orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1399 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); |
Aneesh Kumar K.V | a6bf2bb | 2013-06-05 17:14:04 -0700 | [diff] [blame] | 1400 | pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1401 | if (is_huge_zero_pmd(orig_pmd)) { |
| 1402 | tlb->mm->nr_ptes--; |
| 1403 | spin_unlock(&tlb->mm->page_table_lock); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 1404 | put_huge_zero_page(); |
Kirill A. Shutemov | 479f0ab | 2012-12-12 13:50:50 -0800 | [diff] [blame] | 1405 | } else { |
| 1406 | page = pmd_page(orig_pmd); |
| 1407 | page_remove_rmap(page); |
| 1408 | VM_BUG_ON(page_mapcount(page) < 0); |
| 1409 | add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); |
| 1410 | VM_BUG_ON(!PageHead(page)); |
| 1411 | tlb->mm->nr_ptes--; |
| 1412 | spin_unlock(&tlb->mm->page_table_lock); |
| 1413 | tlb_remove_page(tlb, page); |
| 1414 | } |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1415 | pte_free(tlb->mm, pgtable); |
| 1416 | ret = 1; |
| 1417 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1418 | return ret; |
| 1419 | } |
| 1420 | |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1421 | int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
| 1422 | unsigned long addr, unsigned long end, |
| 1423 | unsigned char *vec) |
| 1424 | { |
| 1425 | int ret = 0; |
| 1426 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1427 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 1428 | /* |
| 1429 | * All logical pages in the range are present |
| 1430 | * if backed by a huge page. |
| 1431 | */ |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1432 | spin_unlock(&vma->vm_mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1433 | memset(vec, 1, (end - addr) >> PAGE_SHIFT); |
| 1434 | ret = 1; |
| 1435 | } |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1436 | |
| 1437 | return ret; |
| 1438 | } |
| 1439 | |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1440 | int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma, |
| 1441 | unsigned long old_addr, |
| 1442 | unsigned long new_addr, unsigned long old_end, |
| 1443 | pmd_t *old_pmd, pmd_t *new_pmd) |
| 1444 | { |
| 1445 | int ret = 0; |
| 1446 | pmd_t pmd; |
| 1447 | |
| 1448 | struct mm_struct *mm = vma->vm_mm; |
| 1449 | |
| 1450 | if ((old_addr & ~HPAGE_PMD_MASK) || |
| 1451 | (new_addr & ~HPAGE_PMD_MASK) || |
| 1452 | old_end - old_addr < HPAGE_PMD_SIZE || |
| 1453 | (new_vma->vm_flags & VM_NOHUGEPAGE)) |
| 1454 | goto out; |
| 1455 | |
| 1456 | /* |
| 1457 | * The destination pmd shouldn't be established, free_pgtables() |
| 1458 | * should have release it. |
| 1459 | */ |
| 1460 | if (WARN_ON(!pmd_none(*new_pmd))) { |
| 1461 | VM_BUG_ON(pmd_trans_huge(*new_pmd)); |
| 1462 | goto out; |
| 1463 | } |
| 1464 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1465 | ret = __pmd_trans_huge_lock(old_pmd, vma); |
| 1466 | if (ret == 1) { |
| 1467 | pmd = pmdp_get_and_clear(mm, old_addr, old_pmd); |
| 1468 | VM_BUG_ON(!pmd_none(*new_pmd)); |
Pavel Emelyanov | 0f8975e | 2013-07-03 15:01:20 -0700 | [diff] [blame] | 1469 | set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd)); |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1470 | spin_unlock(&mm->page_table_lock); |
| 1471 | } |
| 1472 | out: |
| 1473 | return ret; |
| 1474 | } |
| 1475 | |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1476 | /* |
| 1477 | * Returns |
| 1478 | * - 0 if PMD could not be locked |
| 1479 | * - 1 if PMD was locked but protections unchange and TLB flush unnecessary |
| 1480 | * - HPAGE_PMD_NR is protections changed and TLB flush necessary |
| 1481 | */ |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1482 | int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1483 | unsigned long addr, pgprot_t newprot, int prot_numa) |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1484 | { |
| 1485 | struct mm_struct *mm = vma->vm_mm; |
| 1486 | int ret = 0; |
| 1487 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1488 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 1489 | pmd_t entry; |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1490 | ret = 1; |
Hugh Dickins | a4f1de1 | 2012-12-16 18:56:58 -0800 | [diff] [blame] | 1491 | if (!prot_numa) { |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1492 | entry = pmdp_get_and_clear(mm, addr, pmd); |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1493 | entry = pmd_modify(entry, newprot); |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1494 | ret = HPAGE_PMD_NR; |
Hugh Dickins | a4f1de1 | 2012-12-16 18:56:58 -0800 | [diff] [blame] | 1495 | BUG_ON(pmd_write(entry)); |
| 1496 | } else { |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1497 | struct page *page = pmd_page(*pmd); |
| 1498 | |
Mel Gorman | a1a4618 | 2013-10-07 11:28:50 +0100 | [diff] [blame] | 1499 | /* |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1500 | * Do not trap faults against the zero page. The |
| 1501 | * read-only data is likely to be read-cached on the |
| 1502 | * local CPU cache and it is less useful to know about |
| 1503 | * local vs remote hits on the zero page. |
Mel Gorman | a1a4618 | 2013-10-07 11:28:50 +0100 | [diff] [blame] | 1504 | */ |
Mel Gorman | 1bc115d | 2013-10-07 11:29:05 +0100 | [diff] [blame] | 1505 | if (!is_huge_zero_page(page) && |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1506 | !pmd_numa(*pmd)) { |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1507 | entry = pmdp_get_and_clear(mm, addr, pmd); |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1508 | entry = pmd_mknuma(entry); |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1509 | ret = HPAGE_PMD_NR; |
Mel Gorman | 4b10e7d | 2012-10-25 14:16:32 +0200 | [diff] [blame] | 1510 | } |
| 1511 | } |
Mel Gorman | f123d74 | 2013-10-07 11:28:49 +0100 | [diff] [blame] | 1512 | |
| 1513 | /* Set PMD if cleared earlier */ |
| 1514 | if (ret == HPAGE_PMD_NR) |
| 1515 | set_pmd_at(mm, addr, pmd, entry); |
| 1516 | |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1517 | spin_unlock(&vma->vm_mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1518 | } |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1519 | |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1523 | /* |
| 1524 | * Returns 1 if a given pmd maps a stable (not under splitting) thp. |
| 1525 | * Returns -1 if it maps a thp under splitting. Returns 0 otherwise. |
| 1526 | * |
| 1527 | * Note that if it returns 1, this routine returns without unlocking page |
| 1528 | * table locks. So callers must unlock them. |
| 1529 | */ |
| 1530 | int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) |
| 1531 | { |
| 1532 | spin_lock(&vma->vm_mm->page_table_lock); |
| 1533 | if (likely(pmd_trans_huge(*pmd))) { |
| 1534 | if (unlikely(pmd_trans_splitting(*pmd))) { |
| 1535 | spin_unlock(&vma->vm_mm->page_table_lock); |
| 1536 | wait_split_huge_page(vma->anon_vma, pmd); |
| 1537 | return -1; |
| 1538 | } else { |
| 1539 | /* Thp mapped by 'pmd' is stable, so we can |
| 1540 | * handle it as it is. */ |
| 1541 | return 1; |
| 1542 | } |
| 1543 | } |
| 1544 | spin_unlock(&vma->vm_mm->page_table_lock); |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1548 | pmd_t *page_check_address_pmd(struct page *page, |
| 1549 | struct mm_struct *mm, |
| 1550 | unsigned long address, |
| 1551 | enum page_check_address_pmd_flag flag) |
| 1552 | { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1553 | pmd_t *pmd, *ret = NULL; |
| 1554 | |
| 1555 | if (address & ~HPAGE_PMD_MASK) |
| 1556 | goto out; |
| 1557 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 1558 | pmd = mm_find_pmd(mm, address); |
| 1559 | if (!pmd) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1560 | goto out; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1561 | if (pmd_none(*pmd)) |
| 1562 | goto out; |
| 1563 | if (pmd_page(*pmd) != page) |
| 1564 | goto out; |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 1565 | /* |
| 1566 | * split_vma() may create temporary aliased mappings. There is |
| 1567 | * no risk as long as all huge pmd are found and have their |
| 1568 | * splitting bit set before __split_huge_page_refcount |
| 1569 | * runs. Finding the same huge pmd more than once during the |
| 1570 | * same rmap walk is not a problem. |
| 1571 | */ |
| 1572 | if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG && |
| 1573 | pmd_trans_splitting(*pmd)) |
| 1574 | goto out; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1575 | if (pmd_trans_huge(*pmd)) { |
| 1576 | VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG && |
| 1577 | !pmd_trans_splitting(*pmd)); |
| 1578 | ret = pmd; |
| 1579 | } |
| 1580 | out: |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
| 1584 | static int __split_huge_page_splitting(struct page *page, |
| 1585 | struct vm_area_struct *vma, |
| 1586 | unsigned long address) |
| 1587 | { |
| 1588 | struct mm_struct *mm = vma->vm_mm; |
| 1589 | pmd_t *pmd; |
| 1590 | int ret = 0; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1591 | /* For mmu_notifiers */ |
| 1592 | const unsigned long mmun_start = address; |
| 1593 | const unsigned long mmun_end = address + HPAGE_PMD_SIZE; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1594 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1595 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1596 | spin_lock(&mm->page_table_lock); |
| 1597 | pmd = page_check_address_pmd(page, mm, address, |
| 1598 | PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG); |
| 1599 | if (pmd) { |
| 1600 | /* |
| 1601 | * We can't temporarily set the pmd to null in order |
| 1602 | * to split it, the pmd must remain marked huge at all |
| 1603 | * times or the VM won't take the pmd_trans_huge paths |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 1604 | * and it won't wait on the anon_vma->root->rwsem to |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1605 | * serialize against split_huge_page*. |
| 1606 | */ |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1607 | pmdp_splitting_flush(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1608 | ret = 1; |
| 1609 | } |
| 1610 | spin_unlock(&mm->page_table_lock); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 1611 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1612 | |
| 1613 | return ret; |
| 1614 | } |
| 1615 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1616 | static void __split_huge_page_refcount(struct page *page, |
| 1617 | struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1618 | { |
| 1619 | int i; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1620 | struct zone *zone = page_zone(page); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1621 | struct lruvec *lruvec; |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1622 | int tail_count = 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1623 | |
| 1624 | /* prevent PageLRU to go away from under us, and freeze lru stats */ |
| 1625 | spin_lock_irq(&zone->lru_lock); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1626 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 1627 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1628 | compound_lock(page); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 1629 | /* complete memcg works before add pages to LRU */ |
| 1630 | mem_cgroup_split_huge_fixup(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1631 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1632 | for (i = HPAGE_PMD_NR - 1; i >= 1; i--) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1633 | struct page *page_tail = page + i; |
| 1634 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1635 | /* tail_page->_mapcount cannot change */ |
| 1636 | BUG_ON(page_mapcount(page_tail) < 0); |
| 1637 | tail_count += page_mapcount(page_tail); |
| 1638 | /* check for overflow */ |
| 1639 | BUG_ON(tail_count < 0); |
| 1640 | BUG_ON(atomic_read(&page_tail->_count) != 0); |
| 1641 | /* |
| 1642 | * tail_page->_count is zero and not changing from |
| 1643 | * under us. But get_page_unless_zero() may be running |
| 1644 | * from under us on the tail_page. If we used |
| 1645 | * atomic_set() below instead of atomic_add(), we |
| 1646 | * would then run atomic_set() concurrently with |
| 1647 | * get_page_unless_zero(), and atomic_set() is |
| 1648 | * implemented in C not using locked ops. spin_unlock |
| 1649 | * on x86 sometime uses locked ops because of PPro |
| 1650 | * errata 66, 92, so unless somebody can guarantee |
| 1651 | * atomic_set() here would be safe on all archs (and |
| 1652 | * not only on x86), it's safer to use atomic_add(). |
| 1653 | */ |
| 1654 | atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1, |
| 1655 | &page_tail->_count); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1656 | |
| 1657 | /* after clearing PageTail the gup refcount can be released */ |
| 1658 | smp_mb(); |
| 1659 | |
Jin Dongming | a6d30dd | 2011-02-01 15:52:40 -0800 | [diff] [blame] | 1660 | /* |
| 1661 | * retain hwpoison flag of the poisoned tail page: |
| 1662 | * fix for the unsuitable process killed on Guest Machine(KVM) |
| 1663 | * by the memory-failure. |
| 1664 | */ |
| 1665 | page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1666 | page_tail->flags |= (page->flags & |
| 1667 | ((1L << PG_referenced) | |
| 1668 | (1L << PG_swapbacked) | |
| 1669 | (1L << PG_mlocked) | |
Kirill A. Shutemov | e180cf8 | 2013-07-31 13:53:39 -0700 | [diff] [blame] | 1670 | (1L << PG_uptodate) | |
| 1671 | (1L << PG_active) | |
| 1672 | (1L << PG_unevictable))); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1673 | page_tail->flags |= (1L << PG_dirty); |
| 1674 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1675 | /* clear PageTail before overwriting first_page */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1676 | smp_wmb(); |
| 1677 | |
| 1678 | /* |
| 1679 | * __split_huge_page_splitting() already set the |
| 1680 | * splitting bit in all pmd that could map this |
| 1681 | * hugepage, that will ensure no CPU can alter the |
| 1682 | * mapcount on the head page. The mapcount is only |
| 1683 | * accounted in the head page and it has to be |
| 1684 | * transferred to all tail pages in the below code. So |
| 1685 | * for this code to be safe, the split the mapcount |
| 1686 | * can't change. But that doesn't mean userland can't |
| 1687 | * keep changing and reading the page contents while |
| 1688 | * we transfer the mapcount, so the pmd splitting |
| 1689 | * status is achieved setting a reserved bit in the |
| 1690 | * pmd, not by clearing the present bit. |
| 1691 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1692 | page_tail->_mapcount = page->_mapcount; |
| 1693 | |
| 1694 | BUG_ON(page_tail->mapping); |
| 1695 | page_tail->mapping = page->mapping; |
| 1696 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1697 | page_tail->index = page->index + i; |
Peter Zijlstra | 9057289 | 2013-10-07 11:29:20 +0100 | [diff] [blame] | 1698 | page_cpupid_xchg_last(page_tail, page_cpupid_last(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1699 | |
| 1700 | BUG_ON(!PageAnon(page_tail)); |
| 1701 | BUG_ON(!PageUptodate(page_tail)); |
| 1702 | BUG_ON(!PageDirty(page_tail)); |
| 1703 | BUG_ON(!PageSwapBacked(page_tail)); |
| 1704 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1705 | lru_add_page_tail(page, page_tail, lruvec, list); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1706 | } |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1707 | atomic_sub(tail_count, &page->_count); |
| 1708 | BUG_ON(atomic_read(&page->_count) <= 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1709 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1710 | __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1711 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1712 | ClearPageCompound(page); |
| 1713 | compound_unlock(page); |
| 1714 | spin_unlock_irq(&zone->lru_lock); |
| 1715 | |
| 1716 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
| 1717 | struct page *page_tail = page + i; |
| 1718 | BUG_ON(page_count(page_tail) <= 0); |
| 1719 | /* |
| 1720 | * Tail pages may be freed if there wasn't any mapping |
| 1721 | * like if add_to_swap() is running on a lru page that |
| 1722 | * had its mapping zapped. And freeing these pages |
| 1723 | * requires taking the lru_lock so we do the put_page |
| 1724 | * of the tail pages after the split is complete. |
| 1725 | */ |
| 1726 | put_page(page_tail); |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * Only the head page (now become a regular page) is required |
| 1731 | * to be pinned by the caller. |
| 1732 | */ |
| 1733 | BUG_ON(page_count(page) <= 0); |
| 1734 | } |
| 1735 | |
| 1736 | static int __split_huge_page_map(struct page *page, |
| 1737 | struct vm_area_struct *vma, |
| 1738 | unsigned long address) |
| 1739 | { |
| 1740 | struct mm_struct *mm = vma->vm_mm; |
| 1741 | pmd_t *pmd, _pmd; |
| 1742 | int ret = 0, i; |
| 1743 | pgtable_t pgtable; |
| 1744 | unsigned long haddr; |
| 1745 | |
| 1746 | spin_lock(&mm->page_table_lock); |
| 1747 | pmd = page_check_address_pmd(page, mm, address, |
| 1748 | PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG); |
| 1749 | if (pmd) { |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 1750 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1751 | pmd_populate(mm, &_pmd, pgtable); |
| 1752 | |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 1753 | haddr = address; |
| 1754 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1755 | pte_t *pte, entry; |
| 1756 | BUG_ON(PageCompound(page+i)); |
| 1757 | entry = mk_pte(page + i, vma->vm_page_prot); |
| 1758 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1759 | if (!pmd_write(*pmd)) |
| 1760 | entry = pte_wrprotect(entry); |
| 1761 | else |
| 1762 | BUG_ON(page_mapcount(page) != 1); |
| 1763 | if (!pmd_young(*pmd)) |
| 1764 | entry = pte_mkold(entry); |
Andrea Arcangeli | 1ba6e0b | 2012-10-04 01:51:06 +0200 | [diff] [blame] | 1765 | if (pmd_numa(*pmd)) |
| 1766 | entry = pte_mknuma(entry); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1767 | pte = pte_offset_map(&_pmd, haddr); |
| 1768 | BUG_ON(!pte_none(*pte)); |
| 1769 | set_pte_at(mm, haddr, pte, entry); |
| 1770 | pte_unmap(pte); |
| 1771 | } |
| 1772 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1773 | smp_wmb(); /* make pte visible before pmd */ |
| 1774 | /* |
| 1775 | * Up to this point the pmd is present and huge and |
| 1776 | * userland has the whole access to the hugepage |
| 1777 | * during the split (which happens in place). If we |
| 1778 | * overwrite the pmd with the not-huge version |
| 1779 | * pointing to the pte here (which of course we could |
| 1780 | * if all CPUs were bug free), userland could trigger |
| 1781 | * a small page size TLB miss on the small sized TLB |
| 1782 | * while the hugepage TLB entry is still established |
| 1783 | * in the huge TLB. Some CPU doesn't like that. See |
| 1784 | * http://support.amd.com/us/Processor_TechDocs/41322.pdf, |
| 1785 | * Erratum 383 on page 93. Intel should be safe but is |
| 1786 | * also warns that it's only safe if the permission |
| 1787 | * and cache attributes of the two entries loaded in |
| 1788 | * the two TLB is identical (which should be the case |
| 1789 | * here). But it is generally safer to never allow |
| 1790 | * small and huge TLB entries for the same virtual |
| 1791 | * address to be loaded simultaneously. So instead of |
| 1792 | * doing "pmd_populate(); flush_tlb_range();" we first |
| 1793 | * mark the current pmd notpresent (atomically because |
| 1794 | * here the pmd_trans_huge and pmd_trans_splitting |
| 1795 | * must remain set at all times on the pmd until the |
| 1796 | * split is complete for this pmd), then we flush the |
| 1797 | * SMP TLB and finally we write the non-huge version |
| 1798 | * of the pmd entry with pmd_populate. |
| 1799 | */ |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame] | 1800 | pmdp_invalidate(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1801 | pmd_populate(mm, pmd, pgtable); |
| 1802 | ret = 1; |
| 1803 | } |
| 1804 | spin_unlock(&mm->page_table_lock); |
| 1805 | |
| 1806 | return ret; |
| 1807 | } |
| 1808 | |
Ingo Molnar | 5a50508 | 2012-12-02 19:56:46 +0000 | [diff] [blame] | 1809 | /* must be called with anon_vma->root->rwsem held */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1810 | static void __split_huge_page(struct page *page, |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1811 | struct anon_vma *anon_vma, |
| 1812 | struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1813 | { |
| 1814 | int mapcount, mapcount2; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1815 | pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1816 | struct anon_vma_chain *avc; |
| 1817 | |
| 1818 | BUG_ON(!PageHead(page)); |
| 1819 | BUG_ON(PageTail(page)); |
| 1820 | |
| 1821 | mapcount = 0; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1822 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1823 | struct vm_area_struct *vma = avc->vma; |
| 1824 | unsigned long addr = vma_address(page, vma); |
| 1825 | BUG_ON(is_vma_temporary_stack(vma)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1826 | mapcount += __split_huge_page_splitting(page, vma, addr); |
| 1827 | } |
Andrea Arcangeli | 05759d3 | 2011-01-13 15:46:53 -0800 | [diff] [blame] | 1828 | /* |
| 1829 | * It is critical that new vmas are added to the tail of the |
| 1830 | * anon_vma list. This guarantes that if copy_huge_pmd() runs |
| 1831 | * and establishes a child pmd before |
| 1832 | * __split_huge_page_splitting() freezes the parent pmd (so if |
| 1833 | * we fail to prevent copy_huge_pmd() from running until the |
| 1834 | * whole __split_huge_page() is complete), we will still see |
| 1835 | * the newly established pmd of the child later during the |
| 1836 | * walk, to be able to set it as pmd_trans_splitting too. |
| 1837 | */ |
| 1838 | if (mapcount != page_mapcount(page)) |
| 1839 | printk(KERN_ERR "mapcount %d page_mapcount %d\n", |
| 1840 | mapcount, page_mapcount(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1841 | BUG_ON(mapcount != page_mapcount(page)); |
| 1842 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1843 | __split_huge_page_refcount(page, list); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1844 | |
| 1845 | mapcount2 = 0; |
Michel Lespinasse | bf181b9 | 2012-10-08 16:31:39 -0700 | [diff] [blame] | 1846 | anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1847 | struct vm_area_struct *vma = avc->vma; |
| 1848 | unsigned long addr = vma_address(page, vma); |
| 1849 | BUG_ON(is_vma_temporary_stack(vma)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1850 | mapcount2 += __split_huge_page_map(page, vma, addr); |
| 1851 | } |
Andrea Arcangeli | 05759d3 | 2011-01-13 15:46:53 -0800 | [diff] [blame] | 1852 | if (mapcount != mapcount2) |
| 1853 | printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n", |
| 1854 | mapcount, mapcount2, page_mapcount(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1855 | BUG_ON(mapcount != mapcount2); |
| 1856 | } |
| 1857 | |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1858 | /* |
| 1859 | * Split a hugepage into normal pages. This doesn't change the position of head |
| 1860 | * page. If @list is null, tail pages will be added to LRU list, otherwise, to |
| 1861 | * @list. Both head page and tail pages will inherit mapping, flags, and so on |
| 1862 | * from the hugepage. |
| 1863 | * Return 0 if the hugepage is split successfully otherwise return 1. |
| 1864 | */ |
| 1865 | int split_huge_page_to_list(struct page *page, struct list_head *list) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1866 | { |
| 1867 | struct anon_vma *anon_vma; |
| 1868 | int ret = 1; |
| 1869 | |
Kirill A. Shutemov | 5918d10 | 2013-04-29 15:08:44 -0700 | [diff] [blame] | 1870 | BUG_ON(is_huge_zero_page(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1871 | BUG_ON(!PageAnon(page)); |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1872 | |
| 1873 | /* |
| 1874 | * The caller does not necessarily hold an mmap_sem that would prevent |
| 1875 | * the anon_vma disappearing so we first we take a reference to it |
| 1876 | * and then lock the anon_vma for write. This is similar to |
| 1877 | * page_lock_anon_vma_read except the write lock is taken to serialise |
| 1878 | * against parallel split or collapse operations. |
| 1879 | */ |
| 1880 | anon_vma = page_get_anon_vma(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1881 | if (!anon_vma) |
| 1882 | goto out; |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1883 | anon_vma_lock_write(anon_vma); |
| 1884 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1885 | ret = 0; |
| 1886 | if (!PageCompound(page)) |
| 1887 | goto out_unlock; |
| 1888 | |
| 1889 | BUG_ON(!PageSwapBacked(page)); |
Shaohua Li | 5bc7b8a | 2013-04-29 15:08:36 -0700 | [diff] [blame] | 1890 | __split_huge_page(page, anon_vma, list); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 1891 | count_vm_event(THP_SPLIT); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1892 | |
| 1893 | BUG_ON(PageCompound(page)); |
| 1894 | out_unlock: |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 1895 | anon_vma_unlock_write(anon_vma); |
Mel Gorman | 062f1af | 2013-01-11 14:32:02 -0800 | [diff] [blame] | 1896 | put_anon_vma(anon_vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1897 | out: |
| 1898 | return ret; |
| 1899 | } |
| 1900 | |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1901 | #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE) |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1902 | |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1903 | int hugepage_madvise(struct vm_area_struct *vma, |
| 1904 | unsigned long *vm_flags, int advice) |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1905 | { |
Gerald Schaefer | 8e72033 | 2012-10-08 16:30:12 -0700 | [diff] [blame] | 1906 | struct mm_struct *mm = vma->vm_mm; |
| 1907 | |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1908 | switch (advice) { |
| 1909 | case MADV_HUGEPAGE: |
| 1910 | /* |
| 1911 | * Be somewhat over-protective like KSM for now! |
| 1912 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1913 | if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1914 | return -EINVAL; |
Gerald Schaefer | 8e72033 | 2012-10-08 16:30:12 -0700 | [diff] [blame] | 1915 | if (mm->def_flags & VM_NOHUGEPAGE) |
| 1916 | return -EINVAL; |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1917 | *vm_flags &= ~VM_NOHUGEPAGE; |
| 1918 | *vm_flags |= VM_HUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1919 | /* |
| 1920 | * If the vma become good for khugepaged to scan, |
| 1921 | * register it here without waiting a page fault that |
| 1922 | * may not happen any time soon. |
| 1923 | */ |
| 1924 | if (unlikely(khugepaged_enter_vma_merge(vma))) |
| 1925 | return -ENOMEM; |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1926 | break; |
| 1927 | case MADV_NOHUGEPAGE: |
| 1928 | /* |
| 1929 | * Be somewhat over-protective like KSM for now! |
| 1930 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1931 | if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1932 | return -EINVAL; |
| 1933 | *vm_flags &= ~VM_HUGEPAGE; |
| 1934 | *vm_flags |= VM_NOHUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1935 | /* |
| 1936 | * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning |
| 1937 | * this vma even if we leave the mm registered in khugepaged if |
| 1938 | * it got registered before VM_NOHUGEPAGE was set. |
| 1939 | */ |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1940 | break; |
| 1941 | } |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1942 | |
| 1943 | return 0; |
| 1944 | } |
| 1945 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1946 | static int __init khugepaged_slab_init(void) |
| 1947 | { |
| 1948 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", |
| 1949 | sizeof(struct mm_slot), |
| 1950 | __alignof__(struct mm_slot), 0, NULL); |
| 1951 | if (!mm_slot_cache) |
| 1952 | return -ENOMEM; |
| 1953 | |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1957 | static inline struct mm_slot *alloc_mm_slot(void) |
| 1958 | { |
| 1959 | if (!mm_slot_cache) /* initialization failed */ |
| 1960 | return NULL; |
| 1961 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); |
| 1962 | } |
| 1963 | |
| 1964 | static inline void free_mm_slot(struct mm_slot *mm_slot) |
| 1965 | { |
| 1966 | kmem_cache_free(mm_slot_cache, mm_slot); |
| 1967 | } |
| 1968 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1969 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
| 1970 | { |
| 1971 | struct mm_slot *mm_slot; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1972 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1973 | hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1974 | if (mm == mm_slot->mm) |
| 1975 | return mm_slot; |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 1976 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1977 | return NULL; |
| 1978 | } |
| 1979 | |
| 1980 | static void insert_to_mm_slots_hash(struct mm_struct *mm, |
| 1981 | struct mm_slot *mm_slot) |
| 1982 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1983 | mm_slot->mm = mm; |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 1984 | hash_add(mm_slots_hash, &mm_slot->hash, (long)mm); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | static inline int khugepaged_test_exit(struct mm_struct *mm) |
| 1988 | { |
| 1989 | return atomic_read(&mm->mm_users) == 0; |
| 1990 | } |
| 1991 | |
| 1992 | int __khugepaged_enter(struct mm_struct *mm) |
| 1993 | { |
| 1994 | struct mm_slot *mm_slot; |
| 1995 | int wakeup; |
| 1996 | |
| 1997 | mm_slot = alloc_mm_slot(); |
| 1998 | if (!mm_slot) |
| 1999 | return -ENOMEM; |
| 2000 | |
| 2001 | /* __khugepaged_exit() must not run from under us */ |
| 2002 | VM_BUG_ON(khugepaged_test_exit(mm)); |
| 2003 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { |
| 2004 | free_mm_slot(mm_slot); |
| 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | spin_lock(&khugepaged_mm_lock); |
| 2009 | insert_to_mm_slots_hash(mm, mm_slot); |
| 2010 | /* |
| 2011 | * Insert just behind the scanning cursor, to let the area settle |
| 2012 | * down a little. |
| 2013 | */ |
| 2014 | wakeup = list_empty(&khugepaged_scan.mm_head); |
| 2015 | list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head); |
| 2016 | spin_unlock(&khugepaged_mm_lock); |
| 2017 | |
| 2018 | atomic_inc(&mm->mm_count); |
| 2019 | if (wakeup) |
| 2020 | wake_up_interruptible(&khugepaged_wait); |
| 2021 | |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
| 2025 | int khugepaged_enter_vma_merge(struct vm_area_struct *vma) |
| 2026 | { |
| 2027 | unsigned long hstart, hend; |
| 2028 | if (!vma->anon_vma) |
| 2029 | /* |
| 2030 | * Not yet faulted in so we will register later in the |
| 2031 | * page fault if needed. |
| 2032 | */ |
| 2033 | return 0; |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 2034 | if (vma->vm_ops) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2035 | /* khugepaged not yet working on file or special mappings */ |
| 2036 | return 0; |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2037 | VM_BUG_ON(vma->vm_flags & VM_NO_THP); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2038 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2039 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 2040 | if (hstart < hend) |
| 2041 | return khugepaged_enter(vma); |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | void __khugepaged_exit(struct mm_struct *mm) |
| 2046 | { |
| 2047 | struct mm_slot *mm_slot; |
| 2048 | int free = 0; |
| 2049 | |
| 2050 | spin_lock(&khugepaged_mm_lock); |
| 2051 | mm_slot = get_mm_slot(mm); |
| 2052 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2053 | hash_del(&mm_slot->hash); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2054 | list_del(&mm_slot->mm_node); |
| 2055 | free = 1; |
| 2056 | } |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 2057 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2058 | |
| 2059 | if (free) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2060 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 2061 | free_mm_slot(mm_slot); |
| 2062 | mmdrop(mm); |
| 2063 | } else if (mm_slot) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2064 | /* |
| 2065 | * This is required to serialize against |
| 2066 | * khugepaged_test_exit() (which is guaranteed to run |
| 2067 | * under mmap sem read mode). Stop here (after we |
| 2068 | * return all pagetables will be destroyed) until |
| 2069 | * khugepaged has finished working on the pagetables |
| 2070 | * under the mmap_sem. |
| 2071 | */ |
| 2072 | down_write(&mm->mmap_sem); |
| 2073 | up_write(&mm->mmap_sem); |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 2074 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | static void release_pte_page(struct page *page) |
| 2078 | { |
| 2079 | /* 0 stands for page_is_file_cache(page) == false */ |
| 2080 | dec_zone_page_state(page, NR_ISOLATED_ANON + 0); |
| 2081 | unlock_page(page); |
| 2082 | putback_lru_page(page); |
| 2083 | } |
| 2084 | |
| 2085 | static void release_pte_pages(pte_t *pte, pte_t *_pte) |
| 2086 | { |
| 2087 | while (--_pte >= pte) { |
| 2088 | pte_t pteval = *_pte; |
| 2089 | if (!pte_none(pteval)) |
| 2090 | release_pte_page(pte_page(pteval)); |
| 2091 | } |
| 2092 | } |
| 2093 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2094 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, |
| 2095 | unsigned long address, |
| 2096 | pte_t *pte) |
| 2097 | { |
| 2098 | struct page *page; |
| 2099 | pte_t *_pte; |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2100 | int referenced = 0, none = 0; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2101 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 2102 | _pte++, address += PAGE_SIZE) { |
| 2103 | pte_t pteval = *_pte; |
| 2104 | if (pte_none(pteval)) { |
| 2105 | if (++none <= khugepaged_max_ptes_none) |
| 2106 | continue; |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2107 | else |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2108 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2109 | } |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2110 | if (!pte_present(pteval) || !pte_write(pteval)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2111 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2112 | page = vm_normal_page(vma, address, pteval); |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2113 | if (unlikely(!page)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2114 | goto out; |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2115 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2116 | VM_BUG_ON(PageCompound(page)); |
| 2117 | BUG_ON(!PageAnon(page)); |
| 2118 | VM_BUG_ON(!PageSwapBacked(page)); |
| 2119 | |
| 2120 | /* cannot use mapcount: can't collapse if there's a gup pin */ |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2121 | if (page_count(page) != 1) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2122 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2123 | /* |
| 2124 | * We can do it before isolate_lru_page because the |
| 2125 | * page can't be freed from under us. NOTE: PG_lock |
| 2126 | * is needed to serialize against split_huge_page |
| 2127 | * when invoked from the VM. |
| 2128 | */ |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2129 | if (!trylock_page(page)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2130 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2131 | /* |
| 2132 | * Isolate the page to avoid collapsing an hugepage |
| 2133 | * currently in use by the VM. |
| 2134 | */ |
| 2135 | if (isolate_lru_page(page)) { |
| 2136 | unlock_page(page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2137 | goto out; |
| 2138 | } |
| 2139 | /* 0 stands for page_is_file_cache(page) == false */ |
| 2140 | inc_zone_page_state(page, NR_ISOLATED_ANON + 0); |
| 2141 | VM_BUG_ON(!PageLocked(page)); |
| 2142 | VM_BUG_ON(PageLRU(page)); |
| 2143 | |
| 2144 | /* If there is no mapped pte young don't collapse the page */ |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2145 | if (pte_young(pteval) || PageReferenced(page) || |
| 2146 | mmu_notifier_test_young(vma->vm_mm, address)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2147 | referenced = 1; |
| 2148 | } |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2149 | if (likely(referenced)) |
| 2150 | return 1; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2151 | out: |
Bob Liu | 344aa35 | 2012-12-11 16:00:34 -0800 | [diff] [blame] | 2152 | release_pte_pages(pte, _pte); |
| 2153 | return 0; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, |
| 2157 | struct vm_area_struct *vma, |
| 2158 | unsigned long address, |
| 2159 | spinlock_t *ptl) |
| 2160 | { |
| 2161 | pte_t *_pte; |
| 2162 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) { |
| 2163 | pte_t pteval = *_pte; |
| 2164 | struct page *src_page; |
| 2165 | |
| 2166 | if (pte_none(pteval)) { |
| 2167 | clear_user_highpage(page, address); |
| 2168 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); |
| 2169 | } else { |
| 2170 | src_page = pte_page(pteval); |
| 2171 | copy_user_highpage(page, src_page, address, vma); |
| 2172 | VM_BUG_ON(page_mapcount(src_page) != 1); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2173 | release_pte_page(src_page); |
| 2174 | /* |
| 2175 | * ptl mostly unnecessary, but preempt has to |
| 2176 | * be disabled to update the per-cpu stats |
| 2177 | * inside page_remove_rmap(). |
| 2178 | */ |
| 2179 | spin_lock(ptl); |
| 2180 | /* |
| 2181 | * paravirt calls inside pte_clear here are |
| 2182 | * superfluous. |
| 2183 | */ |
| 2184 | pte_clear(vma->vm_mm, address, _pte); |
| 2185 | page_remove_rmap(src_page); |
| 2186 | spin_unlock(ptl); |
| 2187 | free_page_and_swap_cache(src_page); |
| 2188 | } |
| 2189 | |
| 2190 | address += PAGE_SIZE; |
| 2191 | page++; |
| 2192 | } |
| 2193 | } |
| 2194 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2195 | static void khugepaged_alloc_sleep(void) |
| 2196 | { |
| 2197 | wait_event_freezable_timeout(khugepaged_wait, false, |
| 2198 | msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); |
| 2199 | } |
| 2200 | |
| 2201 | #ifdef CONFIG_NUMA |
| 2202 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 2203 | { |
| 2204 | if (IS_ERR(*hpage)) { |
| 2205 | if (!*wait) |
| 2206 | return false; |
| 2207 | |
| 2208 | *wait = false; |
Xiao Guangrong | e3b4126 | 2012-10-08 16:32:57 -0700 | [diff] [blame] | 2209 | *hpage = NULL; |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2210 | khugepaged_alloc_sleep(); |
| 2211 | } else if (*hpage) { |
| 2212 | put_page(*hpage); |
| 2213 | *hpage = NULL; |
| 2214 | } |
| 2215 | |
| 2216 | return true; |
| 2217 | } |
| 2218 | |
| 2219 | static struct page |
| 2220 | *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm, |
| 2221 | struct vm_area_struct *vma, unsigned long address, |
| 2222 | int node) |
| 2223 | { |
| 2224 | VM_BUG_ON(*hpage); |
| 2225 | /* |
| 2226 | * Allocate the page while the vma is still valid and under |
| 2227 | * the mmap_sem read mode so there is no memory allocation |
| 2228 | * later when we take the mmap_sem in write mode. This is more |
| 2229 | * friendly behavior (OTOH it may actually hide bugs) to |
| 2230 | * filesystems in userland with daemons allocating memory in |
| 2231 | * the userland I/O paths. Allocating memory with the |
| 2232 | * mmap_sem in read mode is good idea also to allow greater |
| 2233 | * scalability. |
| 2234 | */ |
| 2235 | *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address, |
| 2236 | node, __GFP_OTHER_NODE); |
| 2237 | |
| 2238 | /* |
| 2239 | * After allocating the hugepage, release the mmap_sem read lock in |
| 2240 | * preparation for taking it in write mode. |
| 2241 | */ |
| 2242 | up_read(&mm->mmap_sem); |
| 2243 | if (unlikely(!*hpage)) { |
| 2244 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 2245 | *hpage = ERR_PTR(-ENOMEM); |
| 2246 | return NULL; |
| 2247 | } |
| 2248 | |
| 2249 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 2250 | return *hpage; |
| 2251 | } |
| 2252 | #else |
| 2253 | static struct page *khugepaged_alloc_hugepage(bool *wait) |
| 2254 | { |
| 2255 | struct page *hpage; |
| 2256 | |
| 2257 | do { |
| 2258 | hpage = alloc_hugepage(khugepaged_defrag()); |
| 2259 | if (!hpage) { |
| 2260 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 2261 | if (!*wait) |
| 2262 | return NULL; |
| 2263 | |
| 2264 | *wait = false; |
| 2265 | khugepaged_alloc_sleep(); |
| 2266 | } else |
| 2267 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 2268 | } while (unlikely(!hpage) && likely(khugepaged_enabled())); |
| 2269 | |
| 2270 | return hpage; |
| 2271 | } |
| 2272 | |
| 2273 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 2274 | { |
| 2275 | if (!*hpage) |
| 2276 | *hpage = khugepaged_alloc_hugepage(wait); |
| 2277 | |
| 2278 | if (unlikely(!*hpage)) |
| 2279 | return false; |
| 2280 | |
| 2281 | return true; |
| 2282 | } |
| 2283 | |
| 2284 | static struct page |
| 2285 | *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm, |
| 2286 | struct vm_area_struct *vma, unsigned long address, |
| 2287 | int node) |
| 2288 | { |
| 2289 | up_read(&mm->mmap_sem); |
| 2290 | VM_BUG_ON(!*hpage); |
| 2291 | return *hpage; |
| 2292 | } |
| 2293 | #endif |
| 2294 | |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2295 | static bool hugepage_vma_check(struct vm_area_struct *vma) |
| 2296 | { |
| 2297 | if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || |
| 2298 | (vma->vm_flags & VM_NOHUGEPAGE)) |
| 2299 | return false; |
| 2300 | |
| 2301 | if (!vma->anon_vma || vma->vm_ops) |
| 2302 | return false; |
| 2303 | if (is_vma_temporary_stack(vma)) |
| 2304 | return false; |
| 2305 | VM_BUG_ON(vma->vm_flags & VM_NO_THP); |
| 2306 | return true; |
| 2307 | } |
| 2308 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2309 | static void collapse_huge_page(struct mm_struct *mm, |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2310 | unsigned long address, |
| 2311 | struct page **hpage, |
| 2312 | struct vm_area_struct *vma, |
| 2313 | int node) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2314 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2315 | pmd_t *pmd, _pmd; |
| 2316 | pte_t *pte; |
| 2317 | pgtable_t pgtable; |
| 2318 | struct page *new_page; |
| 2319 | spinlock_t *ptl; |
| 2320 | int isolated; |
| 2321 | unsigned long hstart, hend; |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2322 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 2323 | unsigned long mmun_end; /* For mmu_notifiers */ |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2324 | |
| 2325 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 2326 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2327 | /* release the mmap_sem read lock. */ |
| 2328 | new_page = khugepaged_alloc_page(hpage, mm, vma, address, node); |
| 2329 | if (!new_page) |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2330 | return; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2331 | |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2332 | if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 2333 | return; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2334 | |
| 2335 | /* |
| 2336 | * Prevent all access to pagetables with the exception of |
| 2337 | * gup_fast later hanlded by the ptep_clear_flush and the VM |
| 2338 | * handled by the anon_vma lock + PG_lock. |
| 2339 | */ |
| 2340 | down_write(&mm->mmap_sem); |
| 2341 | if (unlikely(khugepaged_test_exit(mm))) |
| 2342 | goto out; |
| 2343 | |
| 2344 | vma = find_vma(mm, address); |
Libin | a8f531eb | 2013-09-11 14:20:38 -0700 | [diff] [blame] | 2345 | if (!vma) |
| 2346 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2347 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2348 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 2349 | if (address < hstart || address + HPAGE_PMD_SIZE > hend) |
| 2350 | goto out; |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2351 | if (!hugepage_vma_check(vma)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2352 | goto out; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2353 | pmd = mm_find_pmd(mm, address); |
| 2354 | if (!pmd) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2355 | goto out; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2356 | if (pmd_trans_huge(*pmd)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2357 | goto out; |
| 2358 | |
Ingo Molnar | 4fc3f1d | 2012-12-02 19:56:50 +0000 | [diff] [blame] | 2359 | anon_vma_lock_write(vma->anon_vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2360 | |
| 2361 | pte = pte_offset_map(pmd, address); |
| 2362 | ptl = pte_lockptr(mm, pmd); |
| 2363 | |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2364 | mmun_start = address; |
| 2365 | mmun_end = address + HPAGE_PMD_SIZE; |
| 2366 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2367 | spin_lock(&mm->page_table_lock); /* probably unnecessary */ |
| 2368 | /* |
| 2369 | * After this gup_fast can't run anymore. This also removes |
| 2370 | * any huge TLB entry from the CPU so we won't allow |
| 2371 | * huge and small TLB entries for the same virtual address |
| 2372 | * to avoid the risk of CPU bugs in that area. |
| 2373 | */ |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2374 | _pmd = pmdp_clear_flush(vma, address, pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2375 | spin_unlock(&mm->page_table_lock); |
Sagi Grimberg | 2ec74c3 | 2012-10-08 16:33:33 -0700 | [diff] [blame] | 2376 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2377 | |
| 2378 | spin_lock(ptl); |
| 2379 | isolated = __collapse_huge_page_isolate(vma, address, pte); |
| 2380 | spin_unlock(ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2381 | |
| 2382 | if (unlikely(!isolated)) { |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 2383 | pte_unmap(pte); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2384 | spin_lock(&mm->page_table_lock); |
| 2385 | BUG_ON(!pmd_none(*pmd)); |
Aneesh Kumar K.V | 7c34251 | 2013-05-24 15:55:21 -0700 | [diff] [blame] | 2386 | /* |
| 2387 | * We can only use set_pmd_at when establishing |
| 2388 | * hugepmds and never for establishing regular pmds that |
| 2389 | * points to regular pagetables. Use pmd_populate for that |
| 2390 | */ |
| 2391 | pmd_populate(mm, pmd, pmd_pgtable(_pmd)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2392 | spin_unlock(&mm->page_table_lock); |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 2393 | anon_vma_unlock_write(vma->anon_vma); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2394 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2395 | } |
| 2396 | |
| 2397 | /* |
| 2398 | * All pages are isolated and locked so anon_vma rmap |
| 2399 | * can't run anymore. |
| 2400 | */ |
Konstantin Khlebnikov | 08b5270 | 2013-02-22 16:34:40 -0800 | [diff] [blame] | 2401 | anon_vma_unlock_write(vma->anon_vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2402 | |
| 2403 | __collapse_huge_page_copy(pte, new_page, vma, address, ptl); |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 2404 | pte_unmap(pte); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2405 | __SetPageUptodate(new_page); |
| 2406 | pgtable = pmd_pgtable(_pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2407 | |
Kirill A. Shutemov | 3122359 | 2013-09-12 15:14:01 -0700 | [diff] [blame] | 2408 | _pmd = mk_huge_pmd(new_page, vma->vm_page_prot); |
| 2409 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2410 | |
| 2411 | /* |
| 2412 | * spin_lock() below is not the equivalent of smp_wmb(), so |
| 2413 | * this is needed to avoid the copy_huge_page writes to become |
| 2414 | * visible after the set_pmd_at() write. |
| 2415 | */ |
| 2416 | smp_wmb(); |
| 2417 | |
| 2418 | spin_lock(&mm->page_table_lock); |
| 2419 | BUG_ON(!pmd_none(*pmd)); |
| 2420 | page_add_new_anon_rmap(new_page, vma, address); |
Aneesh Kumar K.V | fce144b | 2013-06-05 17:14:06 -0700 | [diff] [blame] | 2421 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2422 | set_pmd_at(mm, address, pmd, _pmd); |
David Miller | b113da6 | 2012-10-08 16:34:25 -0700 | [diff] [blame] | 2423 | update_mmu_cache_pmd(vma, address, pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2424 | spin_unlock(&mm->page_table_lock); |
| 2425 | |
| 2426 | *hpage = NULL; |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2427 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2428 | khugepaged_pages_collapsed++; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2429 | out_up_write: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2430 | up_write(&mm->mmap_sem); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2431 | return; |
| 2432 | |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2433 | out: |
KAMEZAWA Hiroyuki | 678ff89 | 2011-02-10 15:01:36 -0800 | [diff] [blame] | 2434 | mem_cgroup_uncharge_page(new_page); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2435 | goto out_up_write; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | static int khugepaged_scan_pmd(struct mm_struct *mm, |
| 2439 | struct vm_area_struct *vma, |
| 2440 | unsigned long address, |
| 2441 | struct page **hpage) |
| 2442 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2443 | pmd_t *pmd; |
| 2444 | pte_t *pte, *_pte; |
| 2445 | int ret = 0, referenced = 0, none = 0; |
| 2446 | struct page *page; |
| 2447 | unsigned long _address; |
| 2448 | spinlock_t *ptl; |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2449 | int node = NUMA_NO_NODE; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2450 | |
| 2451 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 2452 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2453 | pmd = mm_find_pmd(mm, address); |
| 2454 | if (!pmd) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2455 | goto out; |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2456 | if (pmd_trans_huge(*pmd)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2457 | goto out; |
| 2458 | |
| 2459 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 2460 | for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 2461 | _pte++, _address += PAGE_SIZE) { |
| 2462 | pte_t pteval = *_pte; |
| 2463 | if (pte_none(pteval)) { |
| 2464 | if (++none <= khugepaged_max_ptes_none) |
| 2465 | continue; |
| 2466 | else |
| 2467 | goto out_unmap; |
| 2468 | } |
| 2469 | if (!pte_present(pteval) || !pte_write(pteval)) |
| 2470 | goto out_unmap; |
| 2471 | page = vm_normal_page(vma, _address, pteval); |
| 2472 | if (unlikely(!page)) |
| 2473 | goto out_unmap; |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2474 | /* |
| 2475 | * Chose the node of the first page. This could |
| 2476 | * be more sophisticated and look at more pages, |
| 2477 | * but isn't for now. |
| 2478 | */ |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 2479 | if (node == NUMA_NO_NODE) |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2480 | node = page_to_nid(page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2481 | VM_BUG_ON(PageCompound(page)); |
| 2482 | if (!PageLRU(page) || PageLocked(page) || !PageAnon(page)) |
| 2483 | goto out_unmap; |
| 2484 | /* cannot use mapcount: can't collapse if there's a gup pin */ |
| 2485 | if (page_count(page) != 1) |
| 2486 | goto out_unmap; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2487 | if (pte_young(pteval) || PageReferenced(page) || |
| 2488 | mmu_notifier_test_young(vma->vm_mm, address)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2489 | referenced = 1; |
| 2490 | } |
| 2491 | if (referenced) |
| 2492 | ret = 1; |
| 2493 | out_unmap: |
| 2494 | pte_unmap_unlock(pte, ptl); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2495 | if (ret) |
| 2496 | /* collapse_huge_page will return with the mmap_sem released */ |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2497 | collapse_huge_page(mm, address, hpage, vma, node); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2498 | out: |
| 2499 | return ret; |
| 2500 | } |
| 2501 | |
| 2502 | static void collect_mm_slot(struct mm_slot *mm_slot) |
| 2503 | { |
| 2504 | struct mm_struct *mm = mm_slot->mm; |
| 2505 | |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2506 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2507 | |
| 2508 | if (khugepaged_test_exit(mm)) { |
| 2509 | /* free mm_slot */ |
Sasha Levin | 43b5fbb | 2013-02-22 16:32:27 -0800 | [diff] [blame] | 2510 | hash_del(&mm_slot->hash); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2511 | list_del(&mm_slot->mm_node); |
| 2512 | |
| 2513 | /* |
| 2514 | * Not strictly needed because the mm exited already. |
| 2515 | * |
| 2516 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 2517 | */ |
| 2518 | |
| 2519 | /* khugepaged_mm_lock actually not necessary for the below */ |
| 2520 | free_mm_slot(mm_slot); |
| 2521 | mmdrop(mm); |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, |
| 2526 | struct page **hpage) |
H Hartley Sweeten | 2f1da64 | 2011-10-31 17:09:25 -0700 | [diff] [blame] | 2527 | __releases(&khugepaged_mm_lock) |
| 2528 | __acquires(&khugepaged_mm_lock) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2529 | { |
| 2530 | struct mm_slot *mm_slot; |
| 2531 | struct mm_struct *mm; |
| 2532 | struct vm_area_struct *vma; |
| 2533 | int progress = 0; |
| 2534 | |
| 2535 | VM_BUG_ON(!pages); |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2536 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2537 | |
| 2538 | if (khugepaged_scan.mm_slot) |
| 2539 | mm_slot = khugepaged_scan.mm_slot; |
| 2540 | else { |
| 2541 | mm_slot = list_entry(khugepaged_scan.mm_head.next, |
| 2542 | struct mm_slot, mm_node); |
| 2543 | khugepaged_scan.address = 0; |
| 2544 | khugepaged_scan.mm_slot = mm_slot; |
| 2545 | } |
| 2546 | spin_unlock(&khugepaged_mm_lock); |
| 2547 | |
| 2548 | mm = mm_slot->mm; |
| 2549 | down_read(&mm->mmap_sem); |
| 2550 | if (unlikely(khugepaged_test_exit(mm))) |
| 2551 | vma = NULL; |
| 2552 | else |
| 2553 | vma = find_vma(mm, khugepaged_scan.address); |
| 2554 | |
| 2555 | progress++; |
| 2556 | for (; vma; vma = vma->vm_next) { |
| 2557 | unsigned long hstart, hend; |
| 2558 | |
| 2559 | cond_resched(); |
| 2560 | if (unlikely(khugepaged_test_exit(mm))) { |
| 2561 | progress++; |
| 2562 | break; |
| 2563 | } |
Bob Liu | fa475e5 | 2012-12-11 16:00:39 -0800 | [diff] [blame] | 2564 | if (!hugepage_vma_check(vma)) { |
| 2565 | skip: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2566 | progress++; |
| 2567 | continue; |
| 2568 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2569 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2570 | hend = vma->vm_end & HPAGE_PMD_MASK; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2571 | if (hstart >= hend) |
| 2572 | goto skip; |
| 2573 | if (khugepaged_scan.address > hend) |
| 2574 | goto skip; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2575 | if (khugepaged_scan.address < hstart) |
| 2576 | khugepaged_scan.address = hstart; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2577 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2578 | |
| 2579 | while (khugepaged_scan.address < hend) { |
| 2580 | int ret; |
| 2581 | cond_resched(); |
| 2582 | if (unlikely(khugepaged_test_exit(mm))) |
| 2583 | goto breakouterloop; |
| 2584 | |
| 2585 | VM_BUG_ON(khugepaged_scan.address < hstart || |
| 2586 | khugepaged_scan.address + HPAGE_PMD_SIZE > |
| 2587 | hend); |
| 2588 | ret = khugepaged_scan_pmd(mm, vma, |
| 2589 | khugepaged_scan.address, |
| 2590 | hpage); |
| 2591 | /* move to next address */ |
| 2592 | khugepaged_scan.address += HPAGE_PMD_SIZE; |
| 2593 | progress += HPAGE_PMD_NR; |
| 2594 | if (ret) |
| 2595 | /* we released mmap_sem so break loop */ |
| 2596 | goto breakouterloop_mmap_sem; |
| 2597 | if (progress >= pages) |
| 2598 | goto breakouterloop; |
| 2599 | } |
| 2600 | } |
| 2601 | breakouterloop: |
| 2602 | up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */ |
| 2603 | breakouterloop_mmap_sem: |
| 2604 | |
| 2605 | spin_lock(&khugepaged_mm_lock); |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2606 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2607 | /* |
| 2608 | * Release the current mm_slot if this mm is about to die, or |
| 2609 | * if we scanned all vmas of this mm. |
| 2610 | */ |
| 2611 | if (khugepaged_test_exit(mm) || !vma) { |
| 2612 | /* |
| 2613 | * Make sure that if mm_users is reaching zero while |
| 2614 | * khugepaged runs here, khugepaged_exit will find |
| 2615 | * mm_slot not pointing to the exiting mm. |
| 2616 | */ |
| 2617 | if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) { |
| 2618 | khugepaged_scan.mm_slot = list_entry( |
| 2619 | mm_slot->mm_node.next, |
| 2620 | struct mm_slot, mm_node); |
| 2621 | khugepaged_scan.address = 0; |
| 2622 | } else { |
| 2623 | khugepaged_scan.mm_slot = NULL; |
| 2624 | khugepaged_full_scans++; |
| 2625 | } |
| 2626 | |
| 2627 | collect_mm_slot(mm_slot); |
| 2628 | } |
| 2629 | |
| 2630 | return progress; |
| 2631 | } |
| 2632 | |
| 2633 | static int khugepaged_has_work(void) |
| 2634 | { |
| 2635 | return !list_empty(&khugepaged_scan.mm_head) && |
| 2636 | khugepaged_enabled(); |
| 2637 | } |
| 2638 | |
| 2639 | static int khugepaged_wait_event(void) |
| 2640 | { |
| 2641 | return !list_empty(&khugepaged_scan.mm_head) || |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2642 | kthread_should_stop(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2645 | static void khugepaged_do_scan(void) |
| 2646 | { |
| 2647 | struct page *hpage = NULL; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2648 | unsigned int progress = 0, pass_through_head = 0; |
| 2649 | unsigned int pages = khugepaged_pages_to_scan; |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2650 | bool wait = true; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2651 | |
| 2652 | barrier(); /* write khugepaged_pages_to_scan to local stack */ |
| 2653 | |
| 2654 | while (progress < pages) { |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2655 | if (!khugepaged_prealloc_page(&hpage, &wait)) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2656 | break; |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2657 | |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2658 | cond_resched(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2659 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2660 | if (unlikely(kthread_should_stop() || freezing(current))) |
| 2661 | break; |
| 2662 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2663 | spin_lock(&khugepaged_mm_lock); |
| 2664 | if (!khugepaged_scan.mm_slot) |
| 2665 | pass_through_head++; |
| 2666 | if (khugepaged_has_work() && |
| 2667 | pass_through_head < 2) |
| 2668 | progress += khugepaged_scan_mm_slot(pages - progress, |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2669 | &hpage); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2670 | else |
| 2671 | progress = pages; |
| 2672 | spin_unlock(&khugepaged_mm_lock); |
| 2673 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2674 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2675 | if (!IS_ERR_OR_NULL(hpage)) |
| 2676 | put_page(hpage); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2677 | } |
| 2678 | |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2679 | static void khugepaged_wait_work(void) |
| 2680 | { |
| 2681 | try_to_freeze(); |
| 2682 | |
| 2683 | if (khugepaged_has_work()) { |
| 2684 | if (!khugepaged_scan_sleep_millisecs) |
| 2685 | return; |
| 2686 | |
| 2687 | wait_event_freezable_timeout(khugepaged_wait, |
| 2688 | kthread_should_stop(), |
| 2689 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs)); |
| 2690 | return; |
| 2691 | } |
| 2692 | |
| 2693 | if (khugepaged_enabled()) |
| 2694 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); |
| 2695 | } |
| 2696 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2697 | static int khugepaged(void *none) |
| 2698 | { |
| 2699 | struct mm_slot *mm_slot; |
| 2700 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2701 | set_freezable(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2702 | set_user_nice(current, 19); |
| 2703 | |
Xiao Guangrong | b723178 | 2012-10-08 16:29:54 -0700 | [diff] [blame] | 2704 | while (!kthread_should_stop()) { |
| 2705 | khugepaged_do_scan(); |
| 2706 | khugepaged_wait_work(); |
| 2707 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2708 | |
| 2709 | spin_lock(&khugepaged_mm_lock); |
| 2710 | mm_slot = khugepaged_scan.mm_slot; |
| 2711 | khugepaged_scan.mm_slot = NULL; |
| 2712 | if (mm_slot) |
| 2713 | collect_mm_slot(mm_slot); |
| 2714 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2715 | return 0; |
| 2716 | } |
| 2717 | |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2718 | static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, |
| 2719 | unsigned long haddr, pmd_t *pmd) |
| 2720 | { |
| 2721 | struct mm_struct *mm = vma->vm_mm; |
| 2722 | pgtable_t pgtable; |
| 2723 | pmd_t _pmd; |
| 2724 | int i; |
| 2725 | |
| 2726 | pmdp_clear_flush(vma, haddr, pmd); |
| 2727 | /* leave pmd empty until pte is filled */ |
| 2728 | |
Aneesh Kumar K.V | 6b0b50b | 2013-06-05 17:14:02 -0700 | [diff] [blame] | 2729 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2730 | pmd_populate(mm, &_pmd, pgtable); |
| 2731 | |
| 2732 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 2733 | pte_t *pte, entry; |
| 2734 | entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot); |
| 2735 | entry = pte_mkspecial(entry); |
| 2736 | pte = pte_offset_map(&_pmd, haddr); |
| 2737 | VM_BUG_ON(!pte_none(*pte)); |
| 2738 | set_pte_at(mm, haddr, pte, entry); |
| 2739 | pte_unmap(pte); |
| 2740 | } |
| 2741 | smp_wmb(); /* make pte visible before pmd */ |
| 2742 | pmd_populate(mm, pmd, pgtable); |
Kirill A. Shutemov | 97ae174 | 2012-12-12 13:51:06 -0800 | [diff] [blame] | 2743 | put_huge_zero_page(); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2746 | void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address, |
| 2747 | pmd_t *pmd) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2748 | { |
| 2749 | struct page *page; |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2750 | struct mm_struct *mm = vma->vm_mm; |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2751 | unsigned long haddr = address & HPAGE_PMD_MASK; |
| 2752 | unsigned long mmun_start; /* For mmu_notifiers */ |
| 2753 | unsigned long mmun_end; /* For mmu_notifiers */ |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2754 | |
| 2755 | BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2756 | |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2757 | mmun_start = haddr; |
| 2758 | mmun_end = haddr + HPAGE_PMD_SIZE; |
| 2759 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2760 | spin_lock(&mm->page_table_lock); |
| 2761 | if (unlikely(!pmd_trans_huge(*pmd))) { |
| 2762 | spin_unlock(&mm->page_table_lock); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2763 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
| 2764 | return; |
| 2765 | } |
| 2766 | if (is_huge_zero_pmd(*pmd)) { |
| 2767 | __split_huge_zero_page_pmd(vma, haddr, pmd); |
| 2768 | spin_unlock(&mm->page_table_lock); |
| 2769 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2770 | return; |
| 2771 | } |
| 2772 | page = pmd_page(*pmd); |
| 2773 | VM_BUG_ON(!page_count(page)); |
| 2774 | get_page(page); |
| 2775 | spin_unlock(&mm->page_table_lock); |
Kirill A. Shutemov | c5a647d | 2012-12-12 13:51:00 -0800 | [diff] [blame] | 2776 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2777 | |
| 2778 | split_huge_page(page); |
| 2779 | |
| 2780 | put_page(page); |
| 2781 | BUG_ON(pmd_trans_huge(*pmd)); |
| 2782 | } |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2783 | |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2784 | void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address, |
| 2785 | pmd_t *pmd) |
| 2786 | { |
| 2787 | struct vm_area_struct *vma; |
| 2788 | |
| 2789 | vma = find_vma(mm, address); |
| 2790 | BUG_ON(vma == NULL); |
| 2791 | split_huge_page_pmd(vma, address, pmd); |
| 2792 | } |
| 2793 | |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2794 | static void split_huge_page_address(struct mm_struct *mm, |
| 2795 | unsigned long address) |
| 2796 | { |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2797 | pmd_t *pmd; |
| 2798 | |
| 2799 | VM_BUG_ON(!(address & ~HPAGE_PMD_MASK)); |
| 2800 | |
Bob Liu | 6219049 | 2012-12-11 16:00:37 -0800 | [diff] [blame] | 2801 | pmd = mm_find_pmd(mm, address); |
| 2802 | if (!pmd) |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2803 | return; |
| 2804 | /* |
| 2805 | * Caller holds the mmap_sem write mode, so a huge pmd cannot |
| 2806 | * materialize from under us. |
| 2807 | */ |
Kirill A. Shutemov | e180377 | 2012-12-12 13:50:59 -0800 | [diff] [blame] | 2808 | split_huge_page_pmd_mm(mm, address, pmd); |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | void __vma_adjust_trans_huge(struct vm_area_struct *vma, |
| 2812 | unsigned long start, |
| 2813 | unsigned long end, |
| 2814 | long adjust_next) |
| 2815 | { |
| 2816 | /* |
| 2817 | * If the new start address isn't hpage aligned and it could |
| 2818 | * previously contain an hugepage: check if we need to split |
| 2819 | * an huge pmd. |
| 2820 | */ |
| 2821 | if (start & ~HPAGE_PMD_MASK && |
| 2822 | (start & HPAGE_PMD_MASK) >= vma->vm_start && |
| 2823 | (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 2824 | split_huge_page_address(vma->vm_mm, start); |
| 2825 | |
| 2826 | /* |
| 2827 | * If the new end address isn't hpage aligned and it could |
| 2828 | * previously contain an hugepage: check if we need to split |
| 2829 | * an huge pmd. |
| 2830 | */ |
| 2831 | if (end & ~HPAGE_PMD_MASK && |
| 2832 | (end & HPAGE_PMD_MASK) >= vma->vm_start && |
| 2833 | (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 2834 | split_huge_page_address(vma->vm_mm, end); |
| 2835 | |
| 2836 | /* |
| 2837 | * If we're also updating the vma->vm_next->vm_start, if the new |
| 2838 | * vm_next->vm_start isn't page aligned and it could previously |
| 2839 | * contain an hugepage: check if we need to split an huge pmd. |
| 2840 | */ |
| 2841 | if (adjust_next > 0) { |
| 2842 | struct vm_area_struct *next = vma->vm_next; |
| 2843 | unsigned long nstart = next->vm_start; |
| 2844 | nstart += adjust_next << PAGE_SHIFT; |
| 2845 | if (nstart & ~HPAGE_PMD_MASK && |
| 2846 | (nstart & HPAGE_PMD_MASK) >= next->vm_start && |
| 2847 | (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end) |
| 2848 | split_huge_page_address(next->vm_mm, nstart); |
| 2849 | } |
| 2850 | } |