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