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