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