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