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