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 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 601 | static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
| 602 | { |
| 603 | if (likely(vma->vm_flags & VM_WRITE)) |
| 604 | pmd = pmd_mkwrite(pmd); |
| 605 | return pmd; |
| 606 | } |
| 607 | |
| 608 | static int __do_huge_pmd_anonymous_page(struct mm_struct *mm, |
| 609 | struct vm_area_struct *vma, |
| 610 | unsigned long haddr, pmd_t *pmd, |
| 611 | struct page *page) |
| 612 | { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 613 | pgtable_t pgtable; |
| 614 | |
| 615 | VM_BUG_ON(!PageCompound(page)); |
| 616 | pgtable = pte_alloc_one(mm, haddr); |
David Rientjes | edad9d2 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 617 | if (unlikely(!pgtable)) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 618 | return VM_FAULT_OOM; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 619 | |
| 620 | clear_huge_page(page, haddr, HPAGE_PMD_NR); |
| 621 | __SetPageUptodate(page); |
| 622 | |
| 623 | spin_lock(&mm->page_table_lock); |
| 624 | if (unlikely(!pmd_none(*pmd))) { |
| 625 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 626 | mem_cgroup_uncharge_page(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 627 | put_page(page); |
| 628 | pte_free(mm, pgtable); |
| 629 | } else { |
| 630 | pmd_t entry; |
| 631 | entry = mk_pmd(page, vma->vm_page_prot); |
| 632 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
| 633 | entry = pmd_mkhuge(entry); |
| 634 | /* |
| 635 | * The spinlocking to take the lru_lock inside |
| 636 | * page_add_new_anon_rmap() acts as a full memory |
| 637 | * barrier to be sure clear_huge_page writes become |
| 638 | * visible after the set_pmd_at() write. |
| 639 | */ |
| 640 | page_add_new_anon_rmap(page, vma, haddr); |
| 641 | set_pmd_at(mm, haddr, pmd, entry); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 642 | pgtable_trans_huge_deposit(mm, pgtable); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 643 | add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR); |
Andrea Arcangeli | 1c641e8 | 2012-03-05 14:59:20 -0800 | [diff] [blame] | 644 | mm->nr_ptes++; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 645 | spin_unlock(&mm->page_table_lock); |
| 646 | } |
| 647 | |
David Rientjes | aa2e878 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 648 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 651 | 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] | 652 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 653 | return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp; |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static inline struct page *alloc_hugepage_vma(int defrag, |
| 657 | struct vm_area_struct *vma, |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 658 | unsigned long haddr, int nd, |
| 659 | gfp_t extra_gfp) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 660 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 661 | return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp), |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 662 | HPAGE_PMD_ORDER, vma, haddr, nd); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | #ifndef CONFIG_NUMA |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 666 | static inline struct page *alloc_hugepage(int defrag) |
| 667 | { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 668 | return alloc_pages(alloc_hugepage_gfpmask(defrag, 0), |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 669 | HPAGE_PMD_ORDER); |
| 670 | } |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 671 | #endif |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 672 | |
| 673 | int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 674 | unsigned long address, pmd_t *pmd, |
| 675 | unsigned int flags) |
| 676 | { |
| 677 | struct page *page; |
| 678 | unsigned long haddr = address & HPAGE_PMD_MASK; |
| 679 | pte_t *pte; |
| 680 | |
| 681 | if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) { |
| 682 | if (unlikely(anon_vma_prepare(vma))) |
| 683 | return VM_FAULT_OOM; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 684 | if (unlikely(khugepaged_enter(vma))) |
| 685 | return VM_FAULT_OOM; |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 686 | page = alloc_hugepage_vma(transparent_hugepage_defrag(vma), |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 687 | vma, haddr, numa_node_id(), 0); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 688 | if (unlikely(!page)) { |
| 689 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 690 | goto out; |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 691 | } |
| 692 | count_vm_event(THP_FAULT_ALLOC); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 693 | if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) { |
| 694 | put_page(page); |
| 695 | goto out; |
| 696 | } |
David Rientjes | edad9d2 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 697 | if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, |
| 698 | page))) { |
| 699 | mem_cgroup_uncharge_page(page); |
| 700 | put_page(page); |
| 701 | goto out; |
| 702 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 703 | |
David Rientjes | edad9d2 | 2012-05-29 15:06:17 -0700 | [diff] [blame] | 704 | return 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 705 | } |
| 706 | out: |
| 707 | /* |
| 708 | * Use __pte_alloc instead of pte_alloc_map, because we can't |
| 709 | * run pte_offset_map on the pmd, if an huge pmd could |
| 710 | * materialize from under us from a different thread. |
| 711 | */ |
| 712 | if (unlikely(__pte_alloc(mm, vma, pmd, address))) |
| 713 | return VM_FAULT_OOM; |
| 714 | /* if an huge pmd materialized from under us just retry later */ |
| 715 | if (unlikely(pmd_trans_huge(*pmd))) |
| 716 | return 0; |
| 717 | /* |
| 718 | * A regular pmd is established and it can't morph into a huge pmd |
| 719 | * from under us anymore at this point because we hold the mmap_sem |
| 720 | * read mode and khugepaged takes it in write mode. So now it's |
| 721 | * safe to run pte_offset_map(). |
| 722 | */ |
| 723 | pte = pte_offset_map(pmd, address); |
| 724 | return handle_pte_fault(mm, vma, address, pte, pmd, flags); |
| 725 | } |
| 726 | |
| 727 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
| 728 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, |
| 729 | struct vm_area_struct *vma) |
| 730 | { |
| 731 | struct page *src_page; |
| 732 | pmd_t pmd; |
| 733 | pgtable_t pgtable; |
| 734 | int ret; |
| 735 | |
| 736 | ret = -ENOMEM; |
| 737 | pgtable = pte_alloc_one(dst_mm, addr); |
| 738 | if (unlikely(!pgtable)) |
| 739 | goto out; |
| 740 | |
| 741 | spin_lock(&dst_mm->page_table_lock); |
| 742 | spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING); |
| 743 | |
| 744 | ret = -EAGAIN; |
| 745 | pmd = *src_pmd; |
| 746 | if (unlikely(!pmd_trans_huge(pmd))) { |
| 747 | pte_free(dst_mm, pgtable); |
| 748 | goto out_unlock; |
| 749 | } |
| 750 | if (unlikely(pmd_trans_splitting(pmd))) { |
| 751 | /* split huge page running from under us */ |
| 752 | spin_unlock(&src_mm->page_table_lock); |
| 753 | spin_unlock(&dst_mm->page_table_lock); |
| 754 | pte_free(dst_mm, pgtable); |
| 755 | |
| 756 | wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */ |
| 757 | goto out; |
| 758 | } |
| 759 | src_page = pmd_page(pmd); |
| 760 | VM_BUG_ON(!PageHead(src_page)); |
| 761 | get_page(src_page); |
| 762 | page_dup_rmap(src_page); |
| 763 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
| 764 | |
| 765 | pmdp_set_wrprotect(src_mm, addr, src_pmd); |
| 766 | pmd = pmd_mkold(pmd_wrprotect(pmd)); |
| 767 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 768 | pgtable_trans_huge_deposit(dst_mm, pgtable); |
Andrea Arcangeli | 1c641e8 | 2012-03-05 14:59:20 -0800 | [diff] [blame] | 769 | dst_mm->nr_ptes++; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 770 | |
| 771 | ret = 0; |
| 772 | out_unlock: |
| 773 | spin_unlock(&src_mm->page_table_lock); |
| 774 | spin_unlock(&dst_mm->page_table_lock); |
| 775 | out: |
| 776 | return ret; |
| 777 | } |
| 778 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 779 | static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm, |
| 780 | struct vm_area_struct *vma, |
| 781 | unsigned long address, |
| 782 | pmd_t *pmd, pmd_t orig_pmd, |
| 783 | struct page *page, |
| 784 | unsigned long haddr) |
| 785 | { |
| 786 | pgtable_t pgtable; |
| 787 | pmd_t _pmd; |
| 788 | int ret = 0, i; |
| 789 | struct page **pages; |
| 790 | |
| 791 | pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR, |
| 792 | GFP_KERNEL); |
| 793 | if (unlikely(!pages)) { |
| 794 | ret |= VM_FAULT_OOM; |
| 795 | goto out; |
| 796 | } |
| 797 | |
| 798 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 799 | pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE | |
| 800 | __GFP_OTHER_NODE, |
Andi Kleen | 19ee151 | 2011-03-04 17:36:31 -0800 | [diff] [blame] | 801 | vma, address, page_to_nid(page)); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 802 | if (unlikely(!pages[i] || |
| 803 | mem_cgroup_newpage_charge(pages[i], mm, |
| 804 | GFP_KERNEL))) { |
| 805 | if (pages[i]) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 806 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 807 | mem_cgroup_uncharge_start(); |
| 808 | while (--i >= 0) { |
| 809 | mem_cgroup_uncharge_page(pages[i]); |
| 810 | put_page(pages[i]); |
| 811 | } |
| 812 | mem_cgroup_uncharge_end(); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 813 | kfree(pages); |
| 814 | ret |= VM_FAULT_OOM; |
| 815 | goto out; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 820 | copy_user_highpage(pages[i], page + i, |
Hillf Danton | 0089e48 | 2011-10-31 17:09:38 -0700 | [diff] [blame] | 821 | haddr + PAGE_SIZE * i, vma); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 822 | __SetPageUptodate(pages[i]); |
| 823 | cond_resched(); |
| 824 | } |
| 825 | |
| 826 | spin_lock(&mm->page_table_lock); |
| 827 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 828 | goto out_free_pages; |
| 829 | VM_BUG_ON(!PageHead(page)); |
| 830 | |
| 831 | pmdp_clear_flush_notify(vma, haddr, pmd); |
| 832 | /* leave pmd empty until pte is filled */ |
| 833 | |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 834 | pgtable = pgtable_trans_huge_withdraw(mm); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 835 | pmd_populate(mm, &_pmd, pgtable); |
| 836 | |
| 837 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
| 838 | pte_t *pte, entry; |
| 839 | entry = mk_pte(pages[i], vma->vm_page_prot); |
| 840 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 841 | page_add_new_anon_rmap(pages[i], vma, haddr); |
| 842 | pte = pte_offset_map(&_pmd, haddr); |
| 843 | VM_BUG_ON(!pte_none(*pte)); |
| 844 | set_pte_at(mm, haddr, pte, entry); |
| 845 | pte_unmap(pte); |
| 846 | } |
| 847 | kfree(pages); |
| 848 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 849 | smp_wmb(); /* make pte visible before pmd */ |
| 850 | pmd_populate(mm, pmd, pgtable); |
| 851 | page_remove_rmap(page); |
| 852 | spin_unlock(&mm->page_table_lock); |
| 853 | |
| 854 | ret |= VM_FAULT_WRITE; |
| 855 | put_page(page); |
| 856 | |
| 857 | out: |
| 858 | return ret; |
| 859 | |
| 860 | out_free_pages: |
| 861 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 862 | mem_cgroup_uncharge_start(); |
| 863 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 864 | mem_cgroup_uncharge_page(pages[i]); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 865 | put_page(pages[i]); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 866 | } |
| 867 | mem_cgroup_uncharge_end(); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 868 | kfree(pages); |
| 869 | goto out; |
| 870 | } |
| 871 | |
| 872 | int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma, |
| 873 | unsigned long address, pmd_t *pmd, pmd_t orig_pmd) |
| 874 | { |
| 875 | int ret = 0; |
| 876 | struct page *page, *new_page; |
| 877 | unsigned long haddr; |
| 878 | |
| 879 | VM_BUG_ON(!vma->anon_vma); |
| 880 | spin_lock(&mm->page_table_lock); |
| 881 | if (unlikely(!pmd_same(*pmd, orig_pmd))) |
| 882 | goto out_unlock; |
| 883 | |
| 884 | page = pmd_page(orig_pmd); |
| 885 | VM_BUG_ON(!PageCompound(page) || !PageHead(page)); |
| 886 | haddr = address & HPAGE_PMD_MASK; |
| 887 | if (page_mapcount(page) == 1) { |
| 888 | pmd_t entry; |
| 889 | entry = pmd_mkyoung(orig_pmd); |
| 890 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
| 891 | if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1)) |
| 892 | update_mmu_cache(vma, address, entry); |
| 893 | ret |= VM_FAULT_WRITE; |
| 894 | goto out_unlock; |
| 895 | } |
| 896 | get_page(page); |
| 897 | spin_unlock(&mm->page_table_lock); |
| 898 | |
| 899 | if (transparent_hugepage_enabled(vma) && |
| 900 | !transparent_hugepage_debug_cow()) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 901 | new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma), |
Andi Kleen | cc5d462 | 2011-03-22 16:33:13 -0700 | [diff] [blame] | 902 | vma, haddr, numa_node_id(), 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 903 | else |
| 904 | new_page = NULL; |
| 905 | |
| 906 | if (unlikely(!new_page)) { |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 907 | count_vm_event(THP_FAULT_FALLBACK); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 908 | ret = do_huge_pmd_wp_page_fallback(mm, vma, address, |
| 909 | pmd, orig_pmd, page, haddr); |
David Rientjes | 1f1d06c | 2012-05-29 15:06:23 -0700 | [diff] [blame] | 910 | if (ret & VM_FAULT_OOM) |
| 911 | split_huge_page(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 912 | put_page(page); |
| 913 | goto out; |
| 914 | } |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 915 | count_vm_event(THP_FAULT_ALLOC); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 916 | |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 917 | if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) { |
| 918 | put_page(new_page); |
David Rientjes | 1f1d06c | 2012-05-29 15:06:23 -0700 | [diff] [blame] | 919 | split_huge_page(page); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 920 | put_page(page); |
| 921 | ret |= VM_FAULT_OOM; |
| 922 | goto out; |
| 923 | } |
| 924 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 925 | copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR); |
| 926 | __SetPageUptodate(new_page); |
| 927 | |
| 928 | spin_lock(&mm->page_table_lock); |
| 929 | put_page(page); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 930 | if (unlikely(!pmd_same(*pmd, orig_pmd))) { |
David Rientjes | 6f60b69 | 2012-05-29 15:06:26 -0700 | [diff] [blame] | 931 | spin_unlock(&mm->page_table_lock); |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 932 | mem_cgroup_uncharge_page(new_page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 933 | put_page(new_page); |
David Rientjes | 6f60b69 | 2012-05-29 15:06:26 -0700 | [diff] [blame] | 934 | goto out; |
Andrea Arcangeli | b9bbfbe | 2011-01-13 15:46:57 -0800 | [diff] [blame] | 935 | } else { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 936 | pmd_t entry; |
| 937 | VM_BUG_ON(!PageHead(page)); |
| 938 | entry = mk_pmd(new_page, vma->vm_page_prot); |
| 939 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
| 940 | entry = pmd_mkhuge(entry); |
| 941 | pmdp_clear_flush_notify(vma, haddr, pmd); |
| 942 | page_add_new_anon_rmap(new_page, vma, haddr); |
| 943 | set_pmd_at(mm, haddr, pmd, entry); |
| 944 | update_mmu_cache(vma, address, entry); |
| 945 | page_remove_rmap(page); |
| 946 | put_page(page); |
| 947 | ret |= VM_FAULT_WRITE; |
| 948 | } |
| 949 | out_unlock: |
| 950 | spin_unlock(&mm->page_table_lock); |
| 951 | out: |
| 952 | return ret; |
| 953 | } |
| 954 | |
| 955 | struct page *follow_trans_huge_pmd(struct mm_struct *mm, |
| 956 | unsigned long addr, |
| 957 | pmd_t *pmd, |
| 958 | unsigned int flags) |
| 959 | { |
| 960 | struct page *page = NULL; |
| 961 | |
| 962 | assert_spin_locked(&mm->page_table_lock); |
| 963 | |
| 964 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) |
| 965 | goto out; |
| 966 | |
| 967 | page = pmd_page(*pmd); |
| 968 | VM_BUG_ON(!PageHead(page)); |
| 969 | if (flags & FOLL_TOUCH) { |
| 970 | pmd_t _pmd; |
| 971 | /* |
| 972 | * We should set the dirty bit only for FOLL_WRITE but |
| 973 | * for now the dirty bit in the pmd is meaningless. |
| 974 | * And if the dirty bit will become meaningful and |
| 975 | * we'll only set it with FOLL_WRITE, an atomic |
| 976 | * set_bit will be required on the pmd to set the |
| 977 | * young bit, instead of the current set_pmd_at. |
| 978 | */ |
| 979 | _pmd = pmd_mkyoung(pmd_mkdirty(*pmd)); |
| 980 | set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd); |
| 981 | } |
| 982 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; |
| 983 | VM_BUG_ON(!PageCompound(page)); |
| 984 | if (flags & FOLL_GET) |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 985 | get_page_foll(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 986 | |
| 987 | out: |
| 988 | return page; |
| 989 | } |
| 990 | |
| 991 | 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] | 992 | pmd_t *pmd, unsigned long addr) |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 993 | { |
| 994 | int ret = 0; |
| 995 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 996 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 997 | struct page *page; |
| 998 | pgtable_t pgtable; |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 999 | pgtable = pgtable_trans_huge_withdraw(tlb->mm); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1000 | page = pmd_page(*pmd); |
| 1001 | pmd_clear(pmd); |
| 1002 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); |
| 1003 | page_remove_rmap(page); |
| 1004 | VM_BUG_ON(page_mapcount(page) < 0); |
| 1005 | add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); |
| 1006 | VM_BUG_ON(!PageHead(page)); |
| 1007 | tlb->mm->nr_ptes--; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1008 | spin_unlock(&tlb->mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1009 | tlb_remove_page(tlb, page); |
| 1010 | pte_free(tlb->mm, pgtable); |
| 1011 | ret = 1; |
| 1012 | } |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1013 | return ret; |
| 1014 | } |
| 1015 | |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1016 | int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
| 1017 | unsigned long addr, unsigned long end, |
| 1018 | unsigned char *vec) |
| 1019 | { |
| 1020 | int ret = 0; |
| 1021 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1022 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 1023 | /* |
| 1024 | * All logical pages in the range are present |
| 1025 | * if backed by a huge page. |
| 1026 | */ |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1027 | spin_unlock(&vma->vm_mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1028 | memset(vec, 1, (end - addr) >> PAGE_SHIFT); |
| 1029 | ret = 1; |
| 1030 | } |
Johannes Weiner | 0ca1634 | 2011-01-13 15:47:02 -0800 | [diff] [blame] | 1031 | |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1035 | int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma, |
| 1036 | unsigned long old_addr, |
| 1037 | unsigned long new_addr, unsigned long old_end, |
| 1038 | pmd_t *old_pmd, pmd_t *new_pmd) |
| 1039 | { |
| 1040 | int ret = 0; |
| 1041 | pmd_t pmd; |
| 1042 | |
| 1043 | struct mm_struct *mm = vma->vm_mm; |
| 1044 | |
| 1045 | if ((old_addr & ~HPAGE_PMD_MASK) || |
| 1046 | (new_addr & ~HPAGE_PMD_MASK) || |
| 1047 | old_end - old_addr < HPAGE_PMD_SIZE || |
| 1048 | (new_vma->vm_flags & VM_NOHUGEPAGE)) |
| 1049 | goto out; |
| 1050 | |
| 1051 | /* |
| 1052 | * The destination pmd shouldn't be established, free_pgtables() |
| 1053 | * should have release it. |
| 1054 | */ |
| 1055 | if (WARN_ON(!pmd_none(*new_pmd))) { |
| 1056 | VM_BUG_ON(pmd_trans_huge(*new_pmd)); |
| 1057 | goto out; |
| 1058 | } |
| 1059 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1060 | ret = __pmd_trans_huge_lock(old_pmd, vma); |
| 1061 | if (ret == 1) { |
| 1062 | pmd = pmdp_get_and_clear(mm, old_addr, old_pmd); |
| 1063 | VM_BUG_ON(!pmd_none(*new_pmd)); |
| 1064 | set_pmd_at(mm, new_addr, new_pmd, pmd); |
Andrea Arcangeli | 37a1c49 | 2011-10-31 17:08:30 -0700 | [diff] [blame] | 1065 | spin_unlock(&mm->page_table_lock); |
| 1066 | } |
| 1067 | out: |
| 1068 | return ret; |
| 1069 | } |
| 1070 | |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1071 | int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
| 1072 | unsigned long addr, pgprot_t newprot) |
| 1073 | { |
| 1074 | struct mm_struct *mm = vma->vm_mm; |
| 1075 | int ret = 0; |
| 1076 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1077 | if (__pmd_trans_huge_lock(pmd, vma) == 1) { |
| 1078 | pmd_t entry; |
| 1079 | entry = pmdp_get_and_clear(mm, addr, pmd); |
| 1080 | entry = pmd_modify(entry, newprot); |
| 1081 | set_pmd_at(mm, addr, pmd, entry); |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1082 | spin_unlock(&vma->vm_mm->page_table_lock); |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1083 | ret = 1; |
| 1084 | } |
Johannes Weiner | cd7548a | 2011-01-13 15:47:04 -0800 | [diff] [blame] | 1085 | |
| 1086 | return ret; |
| 1087 | } |
| 1088 | |
Naoya Horiguchi | 025c5b2 | 2012-03-21 16:33:57 -0700 | [diff] [blame] | 1089 | /* |
| 1090 | * Returns 1 if a given pmd maps a stable (not under splitting) thp. |
| 1091 | * Returns -1 if it maps a thp under splitting. Returns 0 otherwise. |
| 1092 | * |
| 1093 | * Note that if it returns 1, this routine returns without unlocking page |
| 1094 | * table locks. So callers must unlock them. |
| 1095 | */ |
| 1096 | int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) |
| 1097 | { |
| 1098 | spin_lock(&vma->vm_mm->page_table_lock); |
| 1099 | if (likely(pmd_trans_huge(*pmd))) { |
| 1100 | if (unlikely(pmd_trans_splitting(*pmd))) { |
| 1101 | spin_unlock(&vma->vm_mm->page_table_lock); |
| 1102 | wait_split_huge_page(vma->anon_vma, pmd); |
| 1103 | return -1; |
| 1104 | } else { |
| 1105 | /* Thp mapped by 'pmd' is stable, so we can |
| 1106 | * handle it as it is. */ |
| 1107 | return 1; |
| 1108 | } |
| 1109 | } |
| 1110 | spin_unlock(&vma->vm_mm->page_table_lock); |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1114 | pmd_t *page_check_address_pmd(struct page *page, |
| 1115 | struct mm_struct *mm, |
| 1116 | unsigned long address, |
| 1117 | enum page_check_address_pmd_flag flag) |
| 1118 | { |
| 1119 | pgd_t *pgd; |
| 1120 | pud_t *pud; |
| 1121 | pmd_t *pmd, *ret = NULL; |
| 1122 | |
| 1123 | if (address & ~HPAGE_PMD_MASK) |
| 1124 | goto out; |
| 1125 | |
| 1126 | pgd = pgd_offset(mm, address); |
| 1127 | if (!pgd_present(*pgd)) |
| 1128 | goto out; |
| 1129 | |
| 1130 | pud = pud_offset(pgd, address); |
| 1131 | if (!pud_present(*pud)) |
| 1132 | goto out; |
| 1133 | |
| 1134 | pmd = pmd_offset(pud, address); |
| 1135 | if (pmd_none(*pmd)) |
| 1136 | goto out; |
| 1137 | if (pmd_page(*pmd) != page) |
| 1138 | goto out; |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 1139 | /* |
| 1140 | * split_vma() may create temporary aliased mappings. There is |
| 1141 | * no risk as long as all huge pmd are found and have their |
| 1142 | * splitting bit set before __split_huge_page_refcount |
| 1143 | * runs. Finding the same huge pmd more than once during the |
| 1144 | * same rmap walk is not a problem. |
| 1145 | */ |
| 1146 | if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG && |
| 1147 | pmd_trans_splitting(*pmd)) |
| 1148 | goto out; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1149 | if (pmd_trans_huge(*pmd)) { |
| 1150 | VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG && |
| 1151 | !pmd_trans_splitting(*pmd)); |
| 1152 | ret = pmd; |
| 1153 | } |
| 1154 | out: |
| 1155 | return ret; |
| 1156 | } |
| 1157 | |
| 1158 | static int __split_huge_page_splitting(struct page *page, |
| 1159 | struct vm_area_struct *vma, |
| 1160 | unsigned long address) |
| 1161 | { |
| 1162 | struct mm_struct *mm = vma->vm_mm; |
| 1163 | pmd_t *pmd; |
| 1164 | int ret = 0; |
| 1165 | |
| 1166 | spin_lock(&mm->page_table_lock); |
| 1167 | pmd = page_check_address_pmd(page, mm, address, |
| 1168 | PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG); |
| 1169 | if (pmd) { |
| 1170 | /* |
| 1171 | * We can't temporarily set the pmd to null in order |
| 1172 | * to split it, the pmd must remain marked huge at all |
| 1173 | * times or the VM won't take the pmd_trans_huge paths |
Peter Zijlstra | 2b575eb | 2011-05-24 17:12:11 -0700 | [diff] [blame] | 1174 | * and it won't wait on the anon_vma->root->mutex to |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1175 | * serialize against split_huge_page*. |
| 1176 | */ |
| 1177 | pmdp_splitting_flush_notify(vma, address, pmd); |
| 1178 | ret = 1; |
| 1179 | } |
| 1180 | spin_unlock(&mm->page_table_lock); |
| 1181 | |
| 1182 | return ret; |
| 1183 | } |
| 1184 | |
| 1185 | static void __split_huge_page_refcount(struct page *page) |
| 1186 | { |
| 1187 | int i; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1188 | struct zone *zone = page_zone(page); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1189 | struct lruvec *lruvec; |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1190 | int tail_count = 0; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1191 | |
| 1192 | /* prevent PageLRU to go away from under us, and freeze lru stats */ |
| 1193 | spin_lock_irq(&zone->lru_lock); |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1194 | lruvec = mem_cgroup_page_lruvec(page, zone); |
| 1195 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1196 | compound_lock(page); |
KAMEZAWA Hiroyuki | e94c8a9 | 2012-01-12 17:18:20 -0800 | [diff] [blame] | 1197 | /* complete memcg works before add pages to LRU */ |
| 1198 | mem_cgroup_split_huge_fixup(page); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1199 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1200 | for (i = HPAGE_PMD_NR - 1; i >= 1; i--) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1201 | struct page *page_tail = page + i; |
| 1202 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1203 | /* tail_page->_mapcount cannot change */ |
| 1204 | BUG_ON(page_mapcount(page_tail) < 0); |
| 1205 | tail_count += page_mapcount(page_tail); |
| 1206 | /* check for overflow */ |
| 1207 | BUG_ON(tail_count < 0); |
| 1208 | BUG_ON(atomic_read(&page_tail->_count) != 0); |
| 1209 | /* |
| 1210 | * tail_page->_count is zero and not changing from |
| 1211 | * under us. But get_page_unless_zero() may be running |
| 1212 | * from under us on the tail_page. If we used |
| 1213 | * atomic_set() below instead of atomic_add(), we |
| 1214 | * would then run atomic_set() concurrently with |
| 1215 | * get_page_unless_zero(), and atomic_set() is |
| 1216 | * implemented in C not using locked ops. spin_unlock |
| 1217 | * on x86 sometime uses locked ops because of PPro |
| 1218 | * errata 66, 92, so unless somebody can guarantee |
| 1219 | * atomic_set() here would be safe on all archs (and |
| 1220 | * not only on x86), it's safer to use atomic_add(). |
| 1221 | */ |
| 1222 | atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1, |
| 1223 | &page_tail->_count); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1224 | |
| 1225 | /* after clearing PageTail the gup refcount can be released */ |
| 1226 | smp_mb(); |
| 1227 | |
Jin Dongming | a6d30dd | 2011-02-01 15:52:40 -0800 | [diff] [blame] | 1228 | /* |
| 1229 | * retain hwpoison flag of the poisoned tail page: |
| 1230 | * fix for the unsuitable process killed on Guest Machine(KVM) |
| 1231 | * by the memory-failure. |
| 1232 | */ |
| 1233 | page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1234 | page_tail->flags |= (page->flags & |
| 1235 | ((1L << PG_referenced) | |
| 1236 | (1L << PG_swapbacked) | |
| 1237 | (1L << PG_mlocked) | |
| 1238 | (1L << PG_uptodate))); |
| 1239 | page_tail->flags |= (1L << PG_dirty); |
| 1240 | |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1241 | /* clear PageTail before overwriting first_page */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1242 | smp_wmb(); |
| 1243 | |
| 1244 | /* |
| 1245 | * __split_huge_page_splitting() already set the |
| 1246 | * splitting bit in all pmd that could map this |
| 1247 | * hugepage, that will ensure no CPU can alter the |
| 1248 | * mapcount on the head page. The mapcount is only |
| 1249 | * accounted in the head page and it has to be |
| 1250 | * transferred to all tail pages in the below code. So |
| 1251 | * for this code to be safe, the split the mapcount |
| 1252 | * can't change. But that doesn't mean userland can't |
| 1253 | * keep changing and reading the page contents while |
| 1254 | * we transfer the mapcount, so the pmd splitting |
| 1255 | * status is achieved setting a reserved bit in the |
| 1256 | * pmd, not by clearing the present bit. |
| 1257 | */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1258 | page_tail->_mapcount = page->_mapcount; |
| 1259 | |
| 1260 | BUG_ON(page_tail->mapping); |
| 1261 | page_tail->mapping = page->mapping; |
| 1262 | |
Shaohua Li | 4567688 | 2012-01-12 17:19:18 -0800 | [diff] [blame] | 1263 | page_tail->index = page->index + i; |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1264 | |
| 1265 | BUG_ON(!PageAnon(page_tail)); |
| 1266 | BUG_ON(!PageUptodate(page_tail)); |
| 1267 | BUG_ON(!PageDirty(page_tail)); |
| 1268 | BUG_ON(!PageSwapBacked(page_tail)); |
| 1269 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1270 | lru_add_page_tail(page, page_tail, lruvec); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1271 | } |
Andrea Arcangeli | 70b50f9 | 2011-11-02 13:36:59 -0700 | [diff] [blame] | 1272 | atomic_sub(tail_count, &page->_count); |
| 1273 | BUG_ON(atomic_read(&page->_count) <= 0); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1274 | |
Hugh Dickins | fa9add6 | 2012-05-29 15:07:09 -0700 | [diff] [blame] | 1275 | __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1); |
Andrea Arcangeli | 7913417 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1276 | __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR); |
| 1277 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1278 | ClearPageCompound(page); |
| 1279 | compound_unlock(page); |
| 1280 | spin_unlock_irq(&zone->lru_lock); |
| 1281 | |
| 1282 | for (i = 1; i < HPAGE_PMD_NR; i++) { |
| 1283 | struct page *page_tail = page + i; |
| 1284 | BUG_ON(page_count(page_tail) <= 0); |
| 1285 | /* |
| 1286 | * Tail pages may be freed if there wasn't any mapping |
| 1287 | * like if add_to_swap() is running on a lru page that |
| 1288 | * had its mapping zapped. And freeing these pages |
| 1289 | * requires taking the lru_lock so we do the put_page |
| 1290 | * of the tail pages after the split is complete. |
| 1291 | */ |
| 1292 | put_page(page_tail); |
| 1293 | } |
| 1294 | |
| 1295 | /* |
| 1296 | * Only the head page (now become a regular page) is required |
| 1297 | * to be pinned by the caller. |
| 1298 | */ |
| 1299 | BUG_ON(page_count(page) <= 0); |
| 1300 | } |
| 1301 | |
| 1302 | static int __split_huge_page_map(struct page *page, |
| 1303 | struct vm_area_struct *vma, |
| 1304 | unsigned long address) |
| 1305 | { |
| 1306 | struct mm_struct *mm = vma->vm_mm; |
| 1307 | pmd_t *pmd, _pmd; |
| 1308 | int ret = 0, i; |
| 1309 | pgtable_t pgtable; |
| 1310 | unsigned long haddr; |
| 1311 | |
| 1312 | spin_lock(&mm->page_table_lock); |
| 1313 | pmd = page_check_address_pmd(page, mm, address, |
| 1314 | PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG); |
| 1315 | if (pmd) { |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 1316 | pgtable = pgtable_trans_huge_withdraw(mm); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1317 | pmd_populate(mm, &_pmd, pgtable); |
| 1318 | |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 1319 | haddr = address; |
| 1320 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1321 | pte_t *pte, entry; |
| 1322 | BUG_ON(PageCompound(page+i)); |
| 1323 | entry = mk_pte(page + i, vma->vm_page_prot); |
| 1324 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); |
| 1325 | if (!pmd_write(*pmd)) |
| 1326 | entry = pte_wrprotect(entry); |
| 1327 | else |
| 1328 | BUG_ON(page_mapcount(page) != 1); |
| 1329 | if (!pmd_young(*pmd)) |
| 1330 | entry = pte_mkold(entry); |
| 1331 | pte = pte_offset_map(&_pmd, haddr); |
| 1332 | BUG_ON(!pte_none(*pte)); |
| 1333 | set_pte_at(mm, haddr, pte, entry); |
| 1334 | pte_unmap(pte); |
| 1335 | } |
| 1336 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1337 | smp_wmb(); /* make pte visible before pmd */ |
| 1338 | /* |
| 1339 | * Up to this point the pmd is present and huge and |
| 1340 | * userland has the whole access to the hugepage |
| 1341 | * during the split (which happens in place). If we |
| 1342 | * overwrite the pmd with the not-huge version |
| 1343 | * pointing to the pte here (which of course we could |
| 1344 | * if all CPUs were bug free), userland could trigger |
| 1345 | * a small page size TLB miss on the small sized TLB |
| 1346 | * while the hugepage TLB entry is still established |
| 1347 | * in the huge TLB. Some CPU doesn't like that. See |
| 1348 | * http://support.amd.com/us/Processor_TechDocs/41322.pdf, |
| 1349 | * Erratum 383 on page 93. Intel should be safe but is |
| 1350 | * also warns that it's only safe if the permission |
| 1351 | * and cache attributes of the two entries loaded in |
| 1352 | * the two TLB is identical (which should be the case |
| 1353 | * here). But it is generally safer to never allow |
| 1354 | * small and huge TLB entries for the same virtual |
| 1355 | * address to be loaded simultaneously. So instead of |
| 1356 | * doing "pmd_populate(); flush_tlb_range();" we first |
| 1357 | * mark the current pmd notpresent (atomically because |
| 1358 | * here the pmd_trans_huge and pmd_trans_splitting |
| 1359 | * must remain set at all times on the pmd until the |
| 1360 | * split is complete for this pmd), then we flush the |
| 1361 | * SMP TLB and finally we write the non-huge version |
| 1362 | * of the pmd entry with pmd_populate. |
| 1363 | */ |
Gerald Schaefer | 46dcde7 | 2012-10-08 16:30:09 -0700 | [diff] [blame^] | 1364 | pmdp_invalidate(vma, address, pmd); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1365 | pmd_populate(mm, pmd, pgtable); |
| 1366 | ret = 1; |
| 1367 | } |
| 1368 | spin_unlock(&mm->page_table_lock); |
| 1369 | |
| 1370 | return ret; |
| 1371 | } |
| 1372 | |
Peter Zijlstra | 2b575eb | 2011-05-24 17:12:11 -0700 | [diff] [blame] | 1373 | /* must be called with anon_vma->root->mutex hold */ |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1374 | static void __split_huge_page(struct page *page, |
| 1375 | struct anon_vma *anon_vma) |
| 1376 | { |
| 1377 | int mapcount, mapcount2; |
| 1378 | struct anon_vma_chain *avc; |
| 1379 | |
| 1380 | BUG_ON(!PageHead(page)); |
| 1381 | BUG_ON(PageTail(page)); |
| 1382 | |
| 1383 | mapcount = 0; |
| 1384 | list_for_each_entry(avc, &anon_vma->head, same_anon_vma) { |
| 1385 | struct vm_area_struct *vma = avc->vma; |
| 1386 | unsigned long addr = vma_address(page, vma); |
| 1387 | BUG_ON(is_vma_temporary_stack(vma)); |
| 1388 | if (addr == -EFAULT) |
| 1389 | continue; |
| 1390 | mapcount += __split_huge_page_splitting(page, vma, addr); |
| 1391 | } |
Andrea Arcangeli | 05759d3 | 2011-01-13 15:46:53 -0800 | [diff] [blame] | 1392 | /* |
| 1393 | * It is critical that new vmas are added to the tail of the |
| 1394 | * anon_vma list. This guarantes that if copy_huge_pmd() runs |
| 1395 | * and establishes a child pmd before |
| 1396 | * __split_huge_page_splitting() freezes the parent pmd (so if |
| 1397 | * we fail to prevent copy_huge_pmd() from running until the |
| 1398 | * whole __split_huge_page() is complete), we will still see |
| 1399 | * the newly established pmd of the child later during the |
| 1400 | * walk, to be able to set it as pmd_trans_splitting too. |
| 1401 | */ |
| 1402 | if (mapcount != page_mapcount(page)) |
| 1403 | printk(KERN_ERR "mapcount %d page_mapcount %d\n", |
| 1404 | mapcount, page_mapcount(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1405 | BUG_ON(mapcount != page_mapcount(page)); |
| 1406 | |
| 1407 | __split_huge_page_refcount(page); |
| 1408 | |
| 1409 | mapcount2 = 0; |
| 1410 | list_for_each_entry(avc, &anon_vma->head, same_anon_vma) { |
| 1411 | struct vm_area_struct *vma = avc->vma; |
| 1412 | unsigned long addr = vma_address(page, vma); |
| 1413 | BUG_ON(is_vma_temporary_stack(vma)); |
| 1414 | if (addr == -EFAULT) |
| 1415 | continue; |
| 1416 | mapcount2 += __split_huge_page_map(page, vma, addr); |
| 1417 | } |
Andrea Arcangeli | 05759d3 | 2011-01-13 15:46:53 -0800 | [diff] [blame] | 1418 | if (mapcount != mapcount2) |
| 1419 | printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n", |
| 1420 | mapcount, mapcount2, page_mapcount(page)); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1421 | BUG_ON(mapcount != mapcount2); |
| 1422 | } |
| 1423 | |
| 1424 | int split_huge_page(struct page *page) |
| 1425 | { |
| 1426 | struct anon_vma *anon_vma; |
| 1427 | int ret = 1; |
| 1428 | |
| 1429 | BUG_ON(!PageAnon(page)); |
| 1430 | anon_vma = page_lock_anon_vma(page); |
| 1431 | if (!anon_vma) |
| 1432 | goto out; |
| 1433 | ret = 0; |
| 1434 | if (!PageCompound(page)) |
| 1435 | goto out_unlock; |
| 1436 | |
| 1437 | BUG_ON(!PageSwapBacked(page)); |
| 1438 | __split_huge_page(page, anon_vma); |
Andi Kleen | 81ab420 | 2011-04-14 15:22:06 -0700 | [diff] [blame] | 1439 | count_vm_event(THP_SPLIT); |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 1440 | |
| 1441 | BUG_ON(PageCompound(page)); |
| 1442 | out_unlock: |
| 1443 | page_unlock_anon_vma(anon_vma); |
| 1444 | out: |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
Konstantin Khlebnikov | 4b6e1e3 | 2012-10-08 16:28:40 -0700 | [diff] [blame] | 1448 | #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] | 1449 | |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1450 | int hugepage_madvise(struct vm_area_struct *vma, |
| 1451 | unsigned long *vm_flags, int advice) |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1452 | { |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1453 | switch (advice) { |
| 1454 | case MADV_HUGEPAGE: |
| 1455 | /* |
| 1456 | * Be somewhat over-protective like KSM for now! |
| 1457 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1458 | if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1459 | return -EINVAL; |
| 1460 | *vm_flags &= ~VM_NOHUGEPAGE; |
| 1461 | *vm_flags |= VM_HUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1462 | /* |
| 1463 | * If the vma become good for khugepaged to scan, |
| 1464 | * register it here without waiting a page fault that |
| 1465 | * may not happen any time soon. |
| 1466 | */ |
| 1467 | if (unlikely(khugepaged_enter_vma_merge(vma))) |
| 1468 | return -ENOMEM; |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1469 | break; |
| 1470 | case MADV_NOHUGEPAGE: |
| 1471 | /* |
| 1472 | * Be somewhat over-protective like KSM for now! |
| 1473 | */ |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1474 | if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP)) |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1475 | return -EINVAL; |
| 1476 | *vm_flags &= ~VM_HUGEPAGE; |
| 1477 | *vm_flags |= VM_NOHUGEPAGE; |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1478 | /* |
| 1479 | * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning |
| 1480 | * this vma even if we leave the mm registered in khugepaged if |
| 1481 | * it got registered before VM_NOHUGEPAGE was set. |
| 1482 | */ |
Andrea Arcangeli | a664b2d | 2011-01-13 15:47:17 -0800 | [diff] [blame] | 1483 | break; |
| 1484 | } |
Andrea Arcangeli | 0af4e98 | 2011-01-13 15:46:55 -0800 | [diff] [blame] | 1485 | |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1489 | static int __init khugepaged_slab_init(void) |
| 1490 | { |
| 1491 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", |
| 1492 | sizeof(struct mm_slot), |
| 1493 | __alignof__(struct mm_slot), 0, NULL); |
| 1494 | if (!mm_slot_cache) |
| 1495 | return -ENOMEM; |
| 1496 | |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | static void __init khugepaged_slab_free(void) |
| 1501 | { |
| 1502 | kmem_cache_destroy(mm_slot_cache); |
| 1503 | mm_slot_cache = NULL; |
| 1504 | } |
| 1505 | |
| 1506 | static inline struct mm_slot *alloc_mm_slot(void) |
| 1507 | { |
| 1508 | if (!mm_slot_cache) /* initialization failed */ |
| 1509 | return NULL; |
| 1510 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); |
| 1511 | } |
| 1512 | |
| 1513 | static inline void free_mm_slot(struct mm_slot *mm_slot) |
| 1514 | { |
| 1515 | kmem_cache_free(mm_slot_cache, mm_slot); |
| 1516 | } |
| 1517 | |
| 1518 | static int __init mm_slots_hash_init(void) |
| 1519 | { |
| 1520 | mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head), |
| 1521 | GFP_KERNEL); |
| 1522 | if (!mm_slots_hash) |
| 1523 | return -ENOMEM; |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | #if 0 |
| 1528 | static void __init mm_slots_hash_free(void) |
| 1529 | { |
| 1530 | kfree(mm_slots_hash); |
| 1531 | mm_slots_hash = NULL; |
| 1532 | } |
| 1533 | #endif |
| 1534 | |
| 1535 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
| 1536 | { |
| 1537 | struct mm_slot *mm_slot; |
| 1538 | struct hlist_head *bucket; |
| 1539 | struct hlist_node *node; |
| 1540 | |
| 1541 | bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct)) |
| 1542 | % MM_SLOTS_HASH_HEADS]; |
| 1543 | hlist_for_each_entry(mm_slot, node, bucket, hash) { |
| 1544 | if (mm == mm_slot->mm) |
| 1545 | return mm_slot; |
| 1546 | } |
| 1547 | return NULL; |
| 1548 | } |
| 1549 | |
| 1550 | static void insert_to_mm_slots_hash(struct mm_struct *mm, |
| 1551 | struct mm_slot *mm_slot) |
| 1552 | { |
| 1553 | struct hlist_head *bucket; |
| 1554 | |
| 1555 | bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct)) |
| 1556 | % MM_SLOTS_HASH_HEADS]; |
| 1557 | mm_slot->mm = mm; |
| 1558 | hlist_add_head(&mm_slot->hash, bucket); |
| 1559 | } |
| 1560 | |
| 1561 | static inline int khugepaged_test_exit(struct mm_struct *mm) |
| 1562 | { |
| 1563 | return atomic_read(&mm->mm_users) == 0; |
| 1564 | } |
| 1565 | |
| 1566 | int __khugepaged_enter(struct mm_struct *mm) |
| 1567 | { |
| 1568 | struct mm_slot *mm_slot; |
| 1569 | int wakeup; |
| 1570 | |
| 1571 | mm_slot = alloc_mm_slot(); |
| 1572 | if (!mm_slot) |
| 1573 | return -ENOMEM; |
| 1574 | |
| 1575 | /* __khugepaged_exit() must not run from under us */ |
| 1576 | VM_BUG_ON(khugepaged_test_exit(mm)); |
| 1577 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { |
| 1578 | free_mm_slot(mm_slot); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | spin_lock(&khugepaged_mm_lock); |
| 1583 | insert_to_mm_slots_hash(mm, mm_slot); |
| 1584 | /* |
| 1585 | * Insert just behind the scanning cursor, to let the area settle |
| 1586 | * down a little. |
| 1587 | */ |
| 1588 | wakeup = list_empty(&khugepaged_scan.mm_head); |
| 1589 | list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head); |
| 1590 | spin_unlock(&khugepaged_mm_lock); |
| 1591 | |
| 1592 | atomic_inc(&mm->mm_count); |
| 1593 | if (wakeup) |
| 1594 | wake_up_interruptible(&khugepaged_wait); |
| 1595 | |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
| 1599 | int khugepaged_enter_vma_merge(struct vm_area_struct *vma) |
| 1600 | { |
| 1601 | unsigned long hstart, hend; |
| 1602 | if (!vma->anon_vma) |
| 1603 | /* |
| 1604 | * Not yet faulted in so we will register later in the |
| 1605 | * page fault if needed. |
| 1606 | */ |
| 1607 | return 0; |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1608 | if (vma->vm_ops) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1609 | /* khugepaged not yet working on file or special mappings */ |
| 1610 | return 0; |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 1611 | VM_BUG_ON(vma->vm_flags & VM_NO_THP); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1612 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 1613 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 1614 | if (hstart < hend) |
| 1615 | return khugepaged_enter(vma); |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | void __khugepaged_exit(struct mm_struct *mm) |
| 1620 | { |
| 1621 | struct mm_slot *mm_slot; |
| 1622 | int free = 0; |
| 1623 | |
| 1624 | spin_lock(&khugepaged_mm_lock); |
| 1625 | mm_slot = get_mm_slot(mm); |
| 1626 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { |
| 1627 | hlist_del(&mm_slot->hash); |
| 1628 | list_del(&mm_slot->mm_node); |
| 1629 | free = 1; |
| 1630 | } |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 1631 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1632 | |
| 1633 | if (free) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1634 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 1635 | free_mm_slot(mm_slot); |
| 1636 | mmdrop(mm); |
| 1637 | } else if (mm_slot) { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1638 | /* |
| 1639 | * This is required to serialize against |
| 1640 | * khugepaged_test_exit() (which is guaranteed to run |
| 1641 | * under mmap sem read mode). Stop here (after we |
| 1642 | * return all pagetables will be destroyed) until |
| 1643 | * khugepaged has finished working on the pagetables |
| 1644 | * under the mmap_sem. |
| 1645 | */ |
| 1646 | down_write(&mm->mmap_sem); |
| 1647 | up_write(&mm->mmap_sem); |
Chris Wright | d788e80 | 2011-07-25 17:12:14 -0700 | [diff] [blame] | 1648 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | static void release_pte_page(struct page *page) |
| 1652 | { |
| 1653 | /* 0 stands for page_is_file_cache(page) == false */ |
| 1654 | dec_zone_page_state(page, NR_ISOLATED_ANON + 0); |
| 1655 | unlock_page(page); |
| 1656 | putback_lru_page(page); |
| 1657 | } |
| 1658 | |
| 1659 | static void release_pte_pages(pte_t *pte, pte_t *_pte) |
| 1660 | { |
| 1661 | while (--_pte >= pte) { |
| 1662 | pte_t pteval = *_pte; |
| 1663 | if (!pte_none(pteval)) |
| 1664 | release_pte_page(pte_page(pteval)); |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | static void release_all_pte_pages(pte_t *pte) |
| 1669 | { |
| 1670 | release_pte_pages(pte, pte + HPAGE_PMD_NR); |
| 1671 | } |
| 1672 | |
| 1673 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, |
| 1674 | unsigned long address, |
| 1675 | pte_t *pte) |
| 1676 | { |
| 1677 | struct page *page; |
| 1678 | pte_t *_pte; |
| 1679 | int referenced = 0, isolated = 0, none = 0; |
| 1680 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 1681 | _pte++, address += PAGE_SIZE) { |
| 1682 | pte_t pteval = *_pte; |
| 1683 | if (pte_none(pteval)) { |
| 1684 | if (++none <= khugepaged_max_ptes_none) |
| 1685 | continue; |
| 1686 | else { |
| 1687 | release_pte_pages(pte, _pte); |
| 1688 | goto out; |
| 1689 | } |
| 1690 | } |
| 1691 | if (!pte_present(pteval) || !pte_write(pteval)) { |
| 1692 | release_pte_pages(pte, _pte); |
| 1693 | goto out; |
| 1694 | } |
| 1695 | page = vm_normal_page(vma, address, pteval); |
| 1696 | if (unlikely(!page)) { |
| 1697 | release_pte_pages(pte, _pte); |
| 1698 | goto out; |
| 1699 | } |
| 1700 | VM_BUG_ON(PageCompound(page)); |
| 1701 | BUG_ON(!PageAnon(page)); |
| 1702 | VM_BUG_ON(!PageSwapBacked(page)); |
| 1703 | |
| 1704 | /* cannot use mapcount: can't collapse if there's a gup pin */ |
| 1705 | if (page_count(page) != 1) { |
| 1706 | release_pte_pages(pte, _pte); |
| 1707 | goto out; |
| 1708 | } |
| 1709 | /* |
| 1710 | * We can do it before isolate_lru_page because the |
| 1711 | * page can't be freed from under us. NOTE: PG_lock |
| 1712 | * is needed to serialize against split_huge_page |
| 1713 | * when invoked from the VM. |
| 1714 | */ |
| 1715 | if (!trylock_page(page)) { |
| 1716 | release_pte_pages(pte, _pte); |
| 1717 | goto out; |
| 1718 | } |
| 1719 | /* |
| 1720 | * Isolate the page to avoid collapsing an hugepage |
| 1721 | * currently in use by the VM. |
| 1722 | */ |
| 1723 | if (isolate_lru_page(page)) { |
| 1724 | unlock_page(page); |
| 1725 | release_pte_pages(pte, _pte); |
| 1726 | goto out; |
| 1727 | } |
| 1728 | /* 0 stands for page_is_file_cache(page) == false */ |
| 1729 | inc_zone_page_state(page, NR_ISOLATED_ANON + 0); |
| 1730 | VM_BUG_ON(!PageLocked(page)); |
| 1731 | VM_BUG_ON(PageLRU(page)); |
| 1732 | |
| 1733 | /* If there is no mapped pte young don't collapse the page */ |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 1734 | if (pte_young(pteval) || PageReferenced(page) || |
| 1735 | mmu_notifier_test_young(vma->vm_mm, address)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1736 | referenced = 1; |
| 1737 | } |
| 1738 | if (unlikely(!referenced)) |
| 1739 | release_all_pte_pages(pte); |
| 1740 | else |
| 1741 | isolated = 1; |
| 1742 | out: |
| 1743 | return isolated; |
| 1744 | } |
| 1745 | |
| 1746 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, |
| 1747 | struct vm_area_struct *vma, |
| 1748 | unsigned long address, |
| 1749 | spinlock_t *ptl) |
| 1750 | { |
| 1751 | pte_t *_pte; |
| 1752 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) { |
| 1753 | pte_t pteval = *_pte; |
| 1754 | struct page *src_page; |
| 1755 | |
| 1756 | if (pte_none(pteval)) { |
| 1757 | clear_user_highpage(page, address); |
| 1758 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); |
| 1759 | } else { |
| 1760 | src_page = pte_page(pteval); |
| 1761 | copy_user_highpage(page, src_page, address, vma); |
| 1762 | VM_BUG_ON(page_mapcount(src_page) != 1); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1763 | release_pte_page(src_page); |
| 1764 | /* |
| 1765 | * ptl mostly unnecessary, but preempt has to |
| 1766 | * be disabled to update the per-cpu stats |
| 1767 | * inside page_remove_rmap(). |
| 1768 | */ |
| 1769 | spin_lock(ptl); |
| 1770 | /* |
| 1771 | * paravirt calls inside pte_clear here are |
| 1772 | * superfluous. |
| 1773 | */ |
| 1774 | pte_clear(vma->vm_mm, address, _pte); |
| 1775 | page_remove_rmap(src_page); |
| 1776 | spin_unlock(ptl); |
| 1777 | free_page_and_swap_cache(src_page); |
| 1778 | } |
| 1779 | |
| 1780 | address += PAGE_SIZE; |
| 1781 | page++; |
| 1782 | } |
| 1783 | } |
| 1784 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 1785 | static void khugepaged_alloc_sleep(void) |
| 1786 | { |
| 1787 | wait_event_freezable_timeout(khugepaged_wait, false, |
| 1788 | msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); |
| 1789 | } |
| 1790 | |
| 1791 | #ifdef CONFIG_NUMA |
| 1792 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 1793 | { |
| 1794 | if (IS_ERR(*hpage)) { |
| 1795 | if (!*wait) |
| 1796 | return false; |
| 1797 | |
| 1798 | *wait = false; |
| 1799 | khugepaged_alloc_sleep(); |
| 1800 | } else if (*hpage) { |
| 1801 | put_page(*hpage); |
| 1802 | *hpage = NULL; |
| 1803 | } |
| 1804 | |
| 1805 | return true; |
| 1806 | } |
| 1807 | |
| 1808 | static struct page |
| 1809 | *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm, |
| 1810 | struct vm_area_struct *vma, unsigned long address, |
| 1811 | int node) |
| 1812 | { |
| 1813 | VM_BUG_ON(*hpage); |
| 1814 | /* |
| 1815 | * Allocate the page while the vma is still valid and under |
| 1816 | * the mmap_sem read mode so there is no memory allocation |
| 1817 | * later when we take the mmap_sem in write mode. This is more |
| 1818 | * friendly behavior (OTOH it may actually hide bugs) to |
| 1819 | * filesystems in userland with daemons allocating memory in |
| 1820 | * the userland I/O paths. Allocating memory with the |
| 1821 | * mmap_sem in read mode is good idea also to allow greater |
| 1822 | * scalability. |
| 1823 | */ |
| 1824 | *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address, |
| 1825 | node, __GFP_OTHER_NODE); |
| 1826 | |
| 1827 | /* |
| 1828 | * After allocating the hugepage, release the mmap_sem read lock in |
| 1829 | * preparation for taking it in write mode. |
| 1830 | */ |
| 1831 | up_read(&mm->mmap_sem); |
| 1832 | if (unlikely(!*hpage)) { |
| 1833 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 1834 | *hpage = ERR_PTR(-ENOMEM); |
| 1835 | return NULL; |
| 1836 | } |
| 1837 | |
| 1838 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 1839 | return *hpage; |
| 1840 | } |
| 1841 | #else |
| 1842 | static struct page *khugepaged_alloc_hugepage(bool *wait) |
| 1843 | { |
| 1844 | struct page *hpage; |
| 1845 | |
| 1846 | do { |
| 1847 | hpage = alloc_hugepage(khugepaged_defrag()); |
| 1848 | if (!hpage) { |
| 1849 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
| 1850 | if (!*wait) |
| 1851 | return NULL; |
| 1852 | |
| 1853 | *wait = false; |
| 1854 | khugepaged_alloc_sleep(); |
| 1855 | } else |
| 1856 | count_vm_event(THP_COLLAPSE_ALLOC); |
| 1857 | } while (unlikely(!hpage) && likely(khugepaged_enabled())); |
| 1858 | |
| 1859 | return hpage; |
| 1860 | } |
| 1861 | |
| 1862 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
| 1863 | { |
| 1864 | if (!*hpage) |
| 1865 | *hpage = khugepaged_alloc_hugepage(wait); |
| 1866 | |
| 1867 | if (unlikely(!*hpage)) |
| 1868 | return false; |
| 1869 | |
| 1870 | return true; |
| 1871 | } |
| 1872 | |
| 1873 | static struct page |
| 1874 | *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm, |
| 1875 | struct vm_area_struct *vma, unsigned long address, |
| 1876 | int node) |
| 1877 | { |
| 1878 | up_read(&mm->mmap_sem); |
| 1879 | VM_BUG_ON(!*hpage); |
| 1880 | return *hpage; |
| 1881 | } |
| 1882 | #endif |
| 1883 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1884 | static void collapse_huge_page(struct mm_struct *mm, |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 1885 | unsigned long address, |
| 1886 | struct page **hpage, |
| 1887 | struct vm_area_struct *vma, |
| 1888 | int node) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1889 | { |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1890 | pgd_t *pgd; |
| 1891 | pud_t *pud; |
| 1892 | pmd_t *pmd, _pmd; |
| 1893 | pte_t *pte; |
| 1894 | pgtable_t pgtable; |
| 1895 | struct page *new_page; |
| 1896 | spinlock_t *ptl; |
| 1897 | int isolated; |
| 1898 | unsigned long hstart, hend; |
| 1899 | |
| 1900 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 1901 | |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 1902 | /* release the mmap_sem read lock. */ |
| 1903 | new_page = khugepaged_alloc_page(hpage, mm, vma, address, node); |
| 1904 | if (!new_page) |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 1905 | return; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 1906 | |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 1907 | if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) |
Andrea Arcangeli | 692e0b3 | 2011-05-24 17:12:14 -0700 | [diff] [blame] | 1908 | return; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1909 | |
| 1910 | /* |
| 1911 | * Prevent all access to pagetables with the exception of |
| 1912 | * gup_fast later hanlded by the ptep_clear_flush and the VM |
| 1913 | * handled by the anon_vma lock + PG_lock. |
| 1914 | */ |
| 1915 | down_write(&mm->mmap_sem); |
| 1916 | if (unlikely(khugepaged_test_exit(mm))) |
| 1917 | goto out; |
| 1918 | |
| 1919 | vma = find_vma(mm, address); |
| 1920 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 1921 | hend = vma->vm_end & HPAGE_PMD_MASK; |
| 1922 | if (address < hstart || address + HPAGE_PMD_SIZE > hend) |
| 1923 | goto out; |
| 1924 | |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 1925 | if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || |
| 1926 | (vma->vm_flags & VM_NOHUGEPAGE)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1927 | goto out; |
| 1928 | |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 1929 | if (!vma->anon_vma || vma->vm_ops) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1930 | goto out; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 1931 | if (is_vma_temporary_stack(vma)) |
| 1932 | goto out; |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 1933 | VM_BUG_ON(vma->vm_flags & VM_NO_THP); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1934 | |
| 1935 | pgd = pgd_offset(mm, address); |
| 1936 | if (!pgd_present(*pgd)) |
| 1937 | goto out; |
| 1938 | |
| 1939 | pud = pud_offset(pgd, address); |
| 1940 | if (!pud_present(*pud)) |
| 1941 | goto out; |
| 1942 | |
| 1943 | pmd = pmd_offset(pud, address); |
| 1944 | /* pmd can't go away or become huge under us */ |
| 1945 | if (!pmd_present(*pmd) || pmd_trans_huge(*pmd)) |
| 1946 | goto out; |
| 1947 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1948 | anon_vma_lock(vma->anon_vma); |
| 1949 | |
| 1950 | pte = pte_offset_map(pmd, address); |
| 1951 | ptl = pte_lockptr(mm, pmd); |
| 1952 | |
| 1953 | spin_lock(&mm->page_table_lock); /* probably unnecessary */ |
| 1954 | /* |
| 1955 | * After this gup_fast can't run anymore. This also removes |
| 1956 | * any huge TLB entry from the CPU so we won't allow |
| 1957 | * huge and small TLB entries for the same virtual address |
| 1958 | * to avoid the risk of CPU bugs in that area. |
| 1959 | */ |
| 1960 | _pmd = pmdp_clear_flush_notify(vma, address, pmd); |
| 1961 | spin_unlock(&mm->page_table_lock); |
| 1962 | |
| 1963 | spin_lock(ptl); |
| 1964 | isolated = __collapse_huge_page_isolate(vma, address, pte); |
| 1965 | spin_unlock(ptl); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1966 | |
| 1967 | if (unlikely(!isolated)) { |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 1968 | pte_unmap(pte); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1969 | spin_lock(&mm->page_table_lock); |
| 1970 | BUG_ON(!pmd_none(*pmd)); |
| 1971 | set_pmd_at(mm, address, pmd, _pmd); |
| 1972 | spin_unlock(&mm->page_table_lock); |
| 1973 | anon_vma_unlock(vma->anon_vma); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 1974 | goto out; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* |
| 1978 | * All pages are isolated and locked so anon_vma rmap |
| 1979 | * can't run anymore. |
| 1980 | */ |
| 1981 | anon_vma_unlock(vma->anon_vma); |
| 1982 | |
| 1983 | __collapse_huge_page_copy(pte, new_page, vma, address, ptl); |
Johannes Weiner | 453c719 | 2011-01-20 14:44:18 -0800 | [diff] [blame] | 1984 | pte_unmap(pte); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1985 | __SetPageUptodate(new_page); |
| 1986 | pgtable = pmd_pgtable(_pmd); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 1987 | |
| 1988 | _pmd = mk_pmd(new_page, vma->vm_page_prot); |
| 1989 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); |
| 1990 | _pmd = pmd_mkhuge(_pmd); |
| 1991 | |
| 1992 | /* |
| 1993 | * spin_lock() below is not the equivalent of smp_wmb(), so |
| 1994 | * this is needed to avoid the copy_huge_page writes to become |
| 1995 | * visible after the set_pmd_at() write. |
| 1996 | */ |
| 1997 | smp_wmb(); |
| 1998 | |
| 1999 | spin_lock(&mm->page_table_lock); |
| 2000 | BUG_ON(!pmd_none(*pmd)); |
| 2001 | page_add_new_anon_rmap(new_page, vma, address); |
| 2002 | set_pmd_at(mm, address, pmd, _pmd); |
Hillf Danton | 35d8c7a | 2011-10-31 17:09:40 -0700 | [diff] [blame] | 2003 | update_mmu_cache(vma, address, _pmd); |
Gerald Schaefer | e3ebcf64 | 2012-10-08 16:30:07 -0700 | [diff] [blame] | 2004 | pgtable_trans_huge_deposit(mm, pgtable); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2005 | spin_unlock(&mm->page_table_lock); |
| 2006 | |
| 2007 | *hpage = NULL; |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2008 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2009 | khugepaged_pages_collapsed++; |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2010 | out_up_write: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2011 | up_write(&mm->mmap_sem); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2012 | return; |
| 2013 | |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2014 | out: |
KAMEZAWA Hiroyuki | 678ff89 | 2011-02-10 15:01:36 -0800 | [diff] [blame] | 2015 | mem_cgroup_uncharge_page(new_page); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2016 | goto out_up_write; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | static int khugepaged_scan_pmd(struct mm_struct *mm, |
| 2020 | struct vm_area_struct *vma, |
| 2021 | unsigned long address, |
| 2022 | struct page **hpage) |
| 2023 | { |
| 2024 | pgd_t *pgd; |
| 2025 | pud_t *pud; |
| 2026 | pmd_t *pmd; |
| 2027 | pte_t *pte, *_pte; |
| 2028 | int ret = 0, referenced = 0, none = 0; |
| 2029 | struct page *page; |
| 2030 | unsigned long _address; |
| 2031 | spinlock_t *ptl; |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2032 | int node = -1; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2033 | |
| 2034 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); |
| 2035 | |
| 2036 | pgd = pgd_offset(mm, address); |
| 2037 | if (!pgd_present(*pgd)) |
| 2038 | goto out; |
| 2039 | |
| 2040 | pud = pud_offset(pgd, address); |
| 2041 | if (!pud_present(*pud)) |
| 2042 | goto out; |
| 2043 | |
| 2044 | pmd = pmd_offset(pud, address); |
| 2045 | if (!pmd_present(*pmd) || pmd_trans_huge(*pmd)) |
| 2046 | goto out; |
| 2047 | |
| 2048 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
| 2049 | for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR; |
| 2050 | _pte++, _address += PAGE_SIZE) { |
| 2051 | pte_t pteval = *_pte; |
| 2052 | if (pte_none(pteval)) { |
| 2053 | if (++none <= khugepaged_max_ptes_none) |
| 2054 | continue; |
| 2055 | else |
| 2056 | goto out_unmap; |
| 2057 | } |
| 2058 | if (!pte_present(pteval) || !pte_write(pteval)) |
| 2059 | goto out_unmap; |
| 2060 | page = vm_normal_page(vma, _address, pteval); |
| 2061 | if (unlikely(!page)) |
| 2062 | goto out_unmap; |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2063 | /* |
| 2064 | * Chose the node of the first page. This could |
| 2065 | * be more sophisticated and look at more pages, |
| 2066 | * but isn't for now. |
| 2067 | */ |
| 2068 | if (node == -1) |
| 2069 | node = page_to_nid(page); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2070 | VM_BUG_ON(PageCompound(page)); |
| 2071 | if (!PageLRU(page) || PageLocked(page) || !PageAnon(page)) |
| 2072 | goto out_unmap; |
| 2073 | /* cannot use mapcount: can't collapse if there's a gup pin */ |
| 2074 | if (page_count(page) != 1) |
| 2075 | goto out_unmap; |
Andrea Arcangeli | 8ee5382 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2076 | if (pte_young(pteval) || PageReferenced(page) || |
| 2077 | mmu_notifier_test_young(vma->vm_mm, address)) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2078 | referenced = 1; |
| 2079 | } |
| 2080 | if (referenced) |
| 2081 | ret = 1; |
| 2082 | out_unmap: |
| 2083 | pte_unmap_unlock(pte, ptl); |
Andrea Arcangeli | ce83d21 | 2011-01-13 15:47:06 -0800 | [diff] [blame] | 2084 | if (ret) |
| 2085 | /* collapse_huge_page will return with the mmap_sem released */ |
Andi Kleen | 5c4b4be | 2011-03-04 17:36:32 -0800 | [diff] [blame] | 2086 | collapse_huge_page(mm, address, hpage, vma, node); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2087 | out: |
| 2088 | return ret; |
| 2089 | } |
| 2090 | |
| 2091 | static void collect_mm_slot(struct mm_slot *mm_slot) |
| 2092 | { |
| 2093 | struct mm_struct *mm = mm_slot->mm; |
| 2094 | |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2095 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2096 | |
| 2097 | if (khugepaged_test_exit(mm)) { |
| 2098 | /* free mm_slot */ |
| 2099 | hlist_del(&mm_slot->hash); |
| 2100 | list_del(&mm_slot->mm_node); |
| 2101 | |
| 2102 | /* |
| 2103 | * Not strictly needed because the mm exited already. |
| 2104 | * |
| 2105 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
| 2106 | */ |
| 2107 | |
| 2108 | /* khugepaged_mm_lock actually not necessary for the below */ |
| 2109 | free_mm_slot(mm_slot); |
| 2110 | mmdrop(mm); |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, |
| 2115 | struct page **hpage) |
H Hartley Sweeten | 2f1da64 | 2011-10-31 17:09:25 -0700 | [diff] [blame] | 2116 | __releases(&khugepaged_mm_lock) |
| 2117 | __acquires(&khugepaged_mm_lock) |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2118 | { |
| 2119 | struct mm_slot *mm_slot; |
| 2120 | struct mm_struct *mm; |
| 2121 | struct vm_area_struct *vma; |
| 2122 | int progress = 0; |
| 2123 | |
| 2124 | VM_BUG_ON(!pages); |
Hugh Dickins | b9980cd | 2012-02-08 17:13:40 -0800 | [diff] [blame] | 2125 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2126 | |
| 2127 | if (khugepaged_scan.mm_slot) |
| 2128 | mm_slot = khugepaged_scan.mm_slot; |
| 2129 | else { |
| 2130 | mm_slot = list_entry(khugepaged_scan.mm_head.next, |
| 2131 | struct mm_slot, mm_node); |
| 2132 | khugepaged_scan.address = 0; |
| 2133 | khugepaged_scan.mm_slot = mm_slot; |
| 2134 | } |
| 2135 | spin_unlock(&khugepaged_mm_lock); |
| 2136 | |
| 2137 | mm = mm_slot->mm; |
| 2138 | down_read(&mm->mmap_sem); |
| 2139 | if (unlikely(khugepaged_test_exit(mm))) |
| 2140 | vma = NULL; |
| 2141 | else |
| 2142 | vma = find_vma(mm, khugepaged_scan.address); |
| 2143 | |
| 2144 | progress++; |
| 2145 | for (; vma; vma = vma->vm_next) { |
| 2146 | unsigned long hstart, hend; |
| 2147 | |
| 2148 | cond_resched(); |
| 2149 | if (unlikely(khugepaged_test_exit(mm))) { |
| 2150 | progress++; |
| 2151 | break; |
| 2152 | } |
| 2153 | |
Andrea Arcangeli | 60ab324 | 2011-01-13 15:47:18 -0800 | [diff] [blame] | 2154 | if ((!(vma->vm_flags & VM_HUGEPAGE) && |
| 2155 | !khugepaged_always()) || |
| 2156 | (vma->vm_flags & VM_NOHUGEPAGE)) { |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2157 | skip: |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2158 | progress++; |
| 2159 | continue; |
| 2160 | } |
Andrea Arcangeli | 78f11a2 | 2011-04-27 15:26:45 -0700 | [diff] [blame] | 2161 | if (!vma->anon_vma || vma->vm_ops) |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2162 | goto skip; |
| 2163 | if (is_vma_temporary_stack(vma)) |
| 2164 | goto skip; |
Konstantin Khlebnikov | b3b9c29 | 2012-10-08 16:28:34 -0700 | [diff] [blame] | 2165 | VM_BUG_ON(vma->vm_flags & VM_NO_THP); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2166 | |
| 2167 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
| 2168 | hend = vma->vm_end & HPAGE_PMD_MASK; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2169 | if (hstart >= hend) |
| 2170 | goto skip; |
| 2171 | if (khugepaged_scan.address > hend) |
| 2172 | goto skip; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2173 | if (khugepaged_scan.address < hstart) |
| 2174 | khugepaged_scan.address = hstart; |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2175 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2176 | |
| 2177 | while (khugepaged_scan.address < hend) { |
| 2178 | int ret; |
| 2179 | cond_resched(); |
| 2180 | if (unlikely(khugepaged_test_exit(mm))) |
| 2181 | goto breakouterloop; |
| 2182 | |
| 2183 | VM_BUG_ON(khugepaged_scan.address < hstart || |
| 2184 | khugepaged_scan.address + HPAGE_PMD_SIZE > |
| 2185 | hend); |
| 2186 | ret = khugepaged_scan_pmd(mm, vma, |
| 2187 | khugepaged_scan.address, |
| 2188 | hpage); |
| 2189 | /* move to next address */ |
| 2190 | khugepaged_scan.address += HPAGE_PMD_SIZE; |
| 2191 | progress += HPAGE_PMD_NR; |
| 2192 | if (ret) |
| 2193 | /* we released mmap_sem so break loop */ |
| 2194 | goto breakouterloop_mmap_sem; |
| 2195 | if (progress >= pages) |
| 2196 | goto breakouterloop; |
| 2197 | } |
| 2198 | } |
| 2199 | breakouterloop: |
| 2200 | up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */ |
| 2201 | breakouterloop_mmap_sem: |
| 2202 | |
| 2203 | spin_lock(&khugepaged_mm_lock); |
Andrea Arcangeli | a7d6e4e | 2011-02-15 19:02:45 +0100 | [diff] [blame] | 2204 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2205 | /* |
| 2206 | * Release the current mm_slot if this mm is about to die, or |
| 2207 | * if we scanned all vmas of this mm. |
| 2208 | */ |
| 2209 | if (khugepaged_test_exit(mm) || !vma) { |
| 2210 | /* |
| 2211 | * Make sure that if mm_users is reaching zero while |
| 2212 | * khugepaged runs here, khugepaged_exit will find |
| 2213 | * mm_slot not pointing to the exiting mm. |
| 2214 | */ |
| 2215 | if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) { |
| 2216 | khugepaged_scan.mm_slot = list_entry( |
| 2217 | mm_slot->mm_node.next, |
| 2218 | struct mm_slot, mm_node); |
| 2219 | khugepaged_scan.address = 0; |
| 2220 | } else { |
| 2221 | khugepaged_scan.mm_slot = NULL; |
| 2222 | khugepaged_full_scans++; |
| 2223 | } |
| 2224 | |
| 2225 | collect_mm_slot(mm_slot); |
| 2226 | } |
| 2227 | |
| 2228 | return progress; |
| 2229 | } |
| 2230 | |
| 2231 | static int khugepaged_has_work(void) |
| 2232 | { |
| 2233 | return !list_empty(&khugepaged_scan.mm_head) && |
| 2234 | khugepaged_enabled(); |
| 2235 | } |
| 2236 | |
| 2237 | static int khugepaged_wait_event(void) |
| 2238 | { |
| 2239 | return !list_empty(&khugepaged_scan.mm_head) || |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2240 | kthread_should_stop(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2241 | } |
| 2242 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2243 | static void khugepaged_do_scan(void) |
| 2244 | { |
| 2245 | struct page *hpage = NULL; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2246 | unsigned int progress = 0, pass_through_head = 0; |
| 2247 | unsigned int pages = khugepaged_pages_to_scan; |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2248 | bool wait = true; |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2249 | |
| 2250 | barrier(); /* write khugepaged_pages_to_scan to local stack */ |
| 2251 | |
| 2252 | while (progress < pages) { |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2253 | if (!khugepaged_prealloc_page(&hpage, &wait)) |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2254 | break; |
Xiao Guangrong | 26234f3 | 2012-10-08 16:29:51 -0700 | [diff] [blame] | 2255 | |
Xiao Guangrong | 420256ef | 2012-10-08 16:29:49 -0700 | [diff] [blame] | 2256 | cond_resched(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2257 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2258 | if (unlikely(kthread_should_stop() || freezing(current))) |
| 2259 | break; |
| 2260 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2261 | spin_lock(&khugepaged_mm_lock); |
| 2262 | if (!khugepaged_scan.mm_slot) |
| 2263 | pass_through_head++; |
| 2264 | if (khugepaged_has_work() && |
| 2265 | pass_through_head < 2) |
| 2266 | progress += khugepaged_scan_mm_slot(pages - progress, |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2267 | &hpage); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2268 | else |
| 2269 | progress = pages; |
| 2270 | spin_unlock(&khugepaged_mm_lock); |
| 2271 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2272 | |
Xiao Guangrong | d516904 | 2012-10-08 16:29:48 -0700 | [diff] [blame] | 2273 | if (!IS_ERR_OR_NULL(hpage)) |
| 2274 | put_page(hpage); |
Andrea Arcangeli | 0bbbc0b | 2011-01-13 15:47:05 -0800 | [diff] [blame] | 2275 | } |
| 2276 | |
Xiao Guangrong | 2017c0b | 2012-10-08 16:29:44 -0700 | [diff] [blame] | 2277 | static void khugepaged_wait_work(void) |
| 2278 | { |
| 2279 | try_to_freeze(); |
| 2280 | |
| 2281 | if (khugepaged_has_work()) { |
| 2282 | if (!khugepaged_scan_sleep_millisecs) |
| 2283 | return; |
| 2284 | |
| 2285 | wait_event_freezable_timeout(khugepaged_wait, |
| 2286 | kthread_should_stop(), |
| 2287 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs)); |
| 2288 | return; |
| 2289 | } |
| 2290 | |
| 2291 | if (khugepaged_enabled()) |
| 2292 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); |
| 2293 | } |
| 2294 | |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2295 | static int khugepaged(void *none) |
| 2296 | { |
| 2297 | struct mm_slot *mm_slot; |
| 2298 | |
Andrea Arcangeli | 878aee7 | 2011-01-13 15:47:10 -0800 | [diff] [blame] | 2299 | set_freezable(); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2300 | set_user_nice(current, 19); |
| 2301 | |
Xiao Guangrong | b723178 | 2012-10-08 16:29:54 -0700 | [diff] [blame] | 2302 | while (!kthread_should_stop()) { |
| 2303 | khugepaged_do_scan(); |
| 2304 | khugepaged_wait_work(); |
| 2305 | } |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2306 | |
| 2307 | spin_lock(&khugepaged_mm_lock); |
| 2308 | mm_slot = khugepaged_scan.mm_slot; |
| 2309 | khugepaged_scan.mm_slot = NULL; |
| 2310 | if (mm_slot) |
| 2311 | collect_mm_slot(mm_slot); |
| 2312 | spin_unlock(&khugepaged_mm_lock); |
Andrea Arcangeli | ba76149 | 2011-01-13 15:46:58 -0800 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
| 2315 | |
Andrea Arcangeli | 71e3aac | 2011-01-13 15:46:52 -0800 | [diff] [blame] | 2316 | void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd) |
| 2317 | { |
| 2318 | struct page *page; |
| 2319 | |
| 2320 | spin_lock(&mm->page_table_lock); |
| 2321 | if (unlikely(!pmd_trans_huge(*pmd))) { |
| 2322 | spin_unlock(&mm->page_table_lock); |
| 2323 | return; |
| 2324 | } |
| 2325 | page = pmd_page(*pmd); |
| 2326 | VM_BUG_ON(!page_count(page)); |
| 2327 | get_page(page); |
| 2328 | spin_unlock(&mm->page_table_lock); |
| 2329 | |
| 2330 | split_huge_page(page); |
| 2331 | |
| 2332 | put_page(page); |
| 2333 | BUG_ON(pmd_trans_huge(*pmd)); |
| 2334 | } |
Andrea Arcangeli | 94fcc58 | 2011-01-13 15:47:08 -0800 | [diff] [blame] | 2335 | |
| 2336 | static void split_huge_page_address(struct mm_struct *mm, |
| 2337 | unsigned long address) |
| 2338 | { |
| 2339 | pgd_t *pgd; |
| 2340 | pud_t *pud; |
| 2341 | pmd_t *pmd; |
| 2342 | |
| 2343 | VM_BUG_ON(!(address & ~HPAGE_PMD_MASK)); |
| 2344 | |
| 2345 | pgd = pgd_offset(mm, address); |
| 2346 | if (!pgd_present(*pgd)) |
| 2347 | return; |
| 2348 | |
| 2349 | pud = pud_offset(pgd, address); |
| 2350 | if (!pud_present(*pud)) |
| 2351 | return; |
| 2352 | |
| 2353 | pmd = pmd_offset(pud, address); |
| 2354 | if (!pmd_present(*pmd)) |
| 2355 | return; |
| 2356 | /* |
| 2357 | * Caller holds the mmap_sem write mode, so a huge pmd cannot |
| 2358 | * materialize from under us. |
| 2359 | */ |
| 2360 | split_huge_page_pmd(mm, pmd); |
| 2361 | } |
| 2362 | |
| 2363 | void __vma_adjust_trans_huge(struct vm_area_struct *vma, |
| 2364 | unsigned long start, |
| 2365 | unsigned long end, |
| 2366 | long adjust_next) |
| 2367 | { |
| 2368 | /* |
| 2369 | * If the new start address isn't hpage aligned and it could |
| 2370 | * previously contain an hugepage: check if we need to split |
| 2371 | * an huge pmd. |
| 2372 | */ |
| 2373 | if (start & ~HPAGE_PMD_MASK && |
| 2374 | (start & HPAGE_PMD_MASK) >= vma->vm_start && |
| 2375 | (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 2376 | split_huge_page_address(vma->vm_mm, start); |
| 2377 | |
| 2378 | /* |
| 2379 | * If the new end address isn't hpage aligned and it could |
| 2380 | * previously contain an hugepage: check if we need to split |
| 2381 | * an huge pmd. |
| 2382 | */ |
| 2383 | if (end & ~HPAGE_PMD_MASK && |
| 2384 | (end & HPAGE_PMD_MASK) >= vma->vm_start && |
| 2385 | (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) |
| 2386 | split_huge_page_address(vma->vm_mm, end); |
| 2387 | |
| 2388 | /* |
| 2389 | * If we're also updating the vma->vm_next->vm_start, if the new |
| 2390 | * vm_next->vm_start isn't page aligned and it could previously |
| 2391 | * contain an hugepage: check if we need to split an huge pmd. |
| 2392 | */ |
| 2393 | if (adjust_next > 0) { |
| 2394 | struct vm_area_struct *next = vma->vm_next; |
| 2395 | unsigned long nstart = next->vm_start; |
| 2396 | nstart += adjust_next << PAGE_SHIFT; |
| 2397 | if (nstart & ~HPAGE_PMD_MASK && |
| 2398 | (nstart & HPAGE_PMD_MASK) >= next->vm_start && |
| 2399 | (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end) |
| 2400 | split_huge_page_address(next->vm_mm, nstart); |
| 2401 | } |
| 2402 | } |