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