thp: change split_huge_page_pmd() interface
[linux-3.10.git] / mm / huge_memory.c
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>
15 #include <linux/mm_inline.h>
16 #include <linux/kthread.h>
17 #include <linux/khugepaged.h>
18 #include <linux/freezer.h>
19 #include <linux/mman.h>
20 #include <linux/pagemap.h>
21 #include <asm/tlb.h>
22 #include <asm/pgalloc.h>
23 #include "internal.h"
24
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  */
32 unsigned long transparent_hugepage_flags __read_mostly =
33 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
34         (1<<TRANSPARENT_HUGEPAGE_FLAG)|
35 #endif
36 #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
37         (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
38 #endif
39         (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
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;
50 static unsigned long huge_zero_pfn __read_mostly;
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;
94 };
95 static struct khugepaged_scan khugepaged_scan = {
96         .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
97 };
98
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
107         if (!khugepaged_enabled())
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
137 static int start_khugepaged(void)
138 {
139         int err = 0;
140         if (khugepaged_enabled()) {
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                 }
150
151                 if (!list_empty(&khugepaged_scan.mm_head))
152                         wake_up_interruptible(&khugepaged_wait);
153
154                 set_recommended_min_free_kbytes();
155         } else if (khugepaged_thread) {
156                 kthread_stop(khugepaged_thread);
157                 khugepaged_thread = NULL;
158         }
159
160         return err;
161 }
162
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
186 #ifdef CONFIG_SYSFS
187
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 {
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) {
243                 int err;
244
245                 mutex_lock(&khugepaged_mutex);
246                 err = start_khugepaged();
247                 mutex_unlock(&khugepaged_mutex);
248
249                 if (err)
250                         ret = err;
251         }
252
253         return ret;
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 {
262         return sprintf(buf, "%d\n",
263                        !!test_bit(flag, &transparent_hugepage_flags));
264 }
265
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 {
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)
278                 return -EINVAL;
279
280         if (value)
281                 set_bit(flag, &transparent_hugepage_flags);
282         else
283                 clear_bit(flag, &transparent_hugepage_flags);
284
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,
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",
503 };
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 }
551 #endif /* CONFIG_SYSFS */
552
553 static int __init hugepage_init(void)
554 {
555         int err;
556         struct kobject *hugepage_kobj;
557
558         if (!has_transparent_hugepage()) {
559                 transparent_hugepage_flags = 0;
560                 return -EINVAL;
561         }
562
563         err = hugepage_init_sysfs(&hugepage_kobj);
564         if (err)
565                 return err;
566
567         err = init_huge_zero_page();
568         if (err)
569                 goto out;
570
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
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
589         start_khugepaged();
590
591         return 0;
592 out:
593         if (huge_zero_pfn)
594                 __free_page(pfn_to_page(huge_zero_pfn));
595         hugepage_exit_sysfs(hugepage_kobj);
596         return err;
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
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
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
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 {
653         pgtable_t pgtable;
654
655         VM_BUG_ON(!PageCompound(page));
656         pgtable = pte_alloc_one(mm, haddr);
657         if (unlikely(!pgtable))
658                 return VM_FAULT_OOM;
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);
666                 mem_cgroup_uncharge_page(page);
667                 put_page(page);
668                 pte_free(mm, pgtable);
669         } else {
670                 pmd_t entry;
671                 entry = mk_huge_pmd(page, vma);
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);
680                 pgtable_trans_huge_deposit(mm, pgtable);
681                 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
682                 mm->nr_ptes++;
683                 spin_unlock(&mm->page_table_lock);
684         }
685
686         return 0;
687 }
688
689 static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
690 {
691         return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
692 }
693
694 static inline struct page *alloc_hugepage_vma(int defrag,
695                                               struct vm_area_struct *vma,
696                                               unsigned long haddr, int nd,
697                                               gfp_t extra_gfp)
698 {
699         return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
700                                HPAGE_PMD_ORDER, vma, haddr, nd);
701 }
702
703 #ifndef CONFIG_NUMA
704 static inline struct page *alloc_hugepage(int defrag)
705 {
706         return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
707                            HPAGE_PMD_ORDER);
708 }
709 #endif
710
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
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;
734                 if (unlikely(khugepaged_enter(vma)))
735                         return VM_FAULT_OOM;
736                 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
737                                           vma, haddr, numa_node_id(), 0);
738                 if (unlikely(!page)) {
739                         count_vm_event(THP_FAULT_FALLBACK);
740                         goto out;
741                 }
742                 count_vm_event(THP_FAULT_ALLOC);
743                 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
744                         put_page(page);
745                         goto out;
746                 }
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                 }
753
754                 return 0;
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         }
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         }
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);
828         pgtable_trans_huge_deposit(dst_mm, pgtable);
829         dst_mm->nr_ptes++;
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
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
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
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;
936         unsigned long mmun_start;       /* For mmu_notifiers */
937         unsigned long mmun_end;         /* For mmu_notifiers */
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++) {
947                 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
948                                                __GFP_OTHER_NODE,
949                                                vma, address, page_to_nid(page));
950                 if (unlikely(!pages[i] ||
951                              mem_cgroup_newpage_charge(pages[i], mm,
952                                                        GFP_KERNEL))) {
953                         if (pages[i])
954                                 put_page(pages[i]);
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();
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,
969                                    haddr + PAGE_SIZE * i, vma);
970                 __SetPageUptodate(pages[i]);
971                 cond_resched();
972         }
973
974         mmun_start = haddr;
975         mmun_end   = haddr + HPAGE_PMD_SIZE;
976         mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
977
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
983         pmdp_clear_flush(vma, haddr, pmd);
984         /* leave pmd empty until pte is filled */
985
986         pgtable = pgtable_trans_huge_withdraw(mm);
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
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
1006         mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1007
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);
1016         mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1017         mem_cgroup_uncharge_start();
1018         for (i = 0; i < HPAGE_PMD_NR; i++) {
1019                 mem_cgroup_uncharge_page(pages[i]);
1020                 put_page(pages[i]);
1021         }
1022         mem_cgroup_uncharge_end();
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;
1031         struct page *page = NULL, *new_page;
1032         unsigned long haddr;
1033         unsigned long mmun_start;       /* For mmu_notifiers */
1034         unsigned long mmun_end;         /* For mmu_notifiers */
1035
1036         VM_BUG_ON(!vma->anon_vma);
1037         haddr = address & HPAGE_PMD_MASK;
1038         if (is_huge_zero_pmd(orig_pmd))
1039                 goto alloc;
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));
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))
1051                         update_mmu_cache_pmd(vma, address, pmd);
1052                 ret |= VM_FAULT_WRITE;
1053                 goto out_unlock;
1054         }
1055         get_page(page);
1056         spin_unlock(&mm->page_table_lock);
1057 alloc:
1058         if (transparent_hugepage_enabled(vma) &&
1059             !transparent_hugepage_debug_cow())
1060                 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
1061                                               vma, haddr, numa_node_id(), 0);
1062         else
1063                 new_page = NULL;
1064
1065         if (unlikely(!new_page)) {
1066                 count_vm_event(THP_FAULT_FALLBACK);
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                 }
1077                 goto out;
1078         }
1079         count_vm_event(THP_FAULT_ALLOC);
1080
1081         if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1082                 put_page(new_page);
1083                 if (page) {
1084                         split_huge_page(page);
1085                         put_page(page);
1086                 }
1087                 ret |= VM_FAULT_OOM;
1088                 goto out;
1089         }
1090
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);
1095         __SetPageUptodate(new_page);
1096
1097         mmun_start = haddr;
1098         mmun_end   = haddr + HPAGE_PMD_SIZE;
1099         mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1100
1101         spin_lock(&mm->page_table_lock);
1102         if (page)
1103                 put_page(page);
1104         if (unlikely(!pmd_same(*pmd, orig_pmd))) {
1105                 spin_unlock(&mm->page_table_lock);
1106                 mem_cgroup_uncharge_page(new_page);
1107                 put_page(new_page);
1108                 goto out_mn;
1109         } else {
1110                 pmd_t entry;
1111                 entry = mk_huge_pmd(new_page, vma);
1112                 pmdp_clear_flush(vma, haddr, pmd);
1113                 page_add_new_anon_rmap(new_page, vma, haddr);
1114                 set_pmd_at(mm, haddr, pmd, entry);
1115                 update_mmu_cache_pmd(vma, address, pmd);
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                 }
1123                 ret |= VM_FAULT_WRITE;
1124         }
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;
1130 out_unlock:
1131         spin_unlock(&mm->page_table_lock);
1132         return ret;
1133 }
1134
1135 struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
1136                                    unsigned long addr,
1137                                    pmd_t *pmd,
1138                                    unsigned int flags)
1139 {
1140         struct mm_struct *mm = vma->vm_mm;
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         }
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         }
1171         page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1172         VM_BUG_ON(!PageCompound(page));
1173         if (flags & FOLL_GET)
1174                 get_page_foll(page);
1175
1176 out:
1177         return page;
1178 }
1179
1180 int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1181                  pmd_t *pmd, unsigned long addr)
1182 {
1183         int ret = 0;
1184
1185         if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1186                 struct page *page;
1187                 pgtable_t pgtable;
1188                 pmd_t orig_pmd;
1189                 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
1190                 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
1191                 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
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                 }
1205                 pte_free(tlb->mm, pgtable);
1206                 ret = 1;
1207         }
1208         return ret;
1209 }
1210
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
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                  */
1222                 spin_unlock(&vma->vm_mm->page_table_lock);
1223                 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1224                 ret = 1;
1225         }
1226
1227         return ret;
1228 }
1229
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
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);
1260                 spin_unlock(&mm->page_table_lock);
1261         }
1262 out:
1263         return ret;
1264 }
1265
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
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                 BUG_ON(pmd_write(entry));
1277                 set_pmd_at(mm, addr, pmd, entry);
1278                 spin_unlock(&vma->vm_mm->page_table_lock);
1279                 ret = 1;
1280         }
1281
1282         return ret;
1283 }
1284
1285 /*
1286  * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1287  * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1288  *
1289  * Note that if it returns 1, this routine returns without unlocking page
1290  * table locks. So callers must unlock them.
1291  */
1292 int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1293 {
1294         spin_lock(&vma->vm_mm->page_table_lock);
1295         if (likely(pmd_trans_huge(*pmd))) {
1296                 if (unlikely(pmd_trans_splitting(*pmd))) {
1297                         spin_unlock(&vma->vm_mm->page_table_lock);
1298                         wait_split_huge_page(vma->anon_vma, pmd);
1299                         return -1;
1300                 } else {
1301                         /* Thp mapped by 'pmd' is stable, so we can
1302                          * handle it as it is. */
1303                         return 1;
1304                 }
1305         }
1306         spin_unlock(&vma->vm_mm->page_table_lock);
1307         return 0;
1308 }
1309
1310 pmd_t *page_check_address_pmd(struct page *page,
1311                               struct mm_struct *mm,
1312                               unsigned long address,
1313                               enum page_check_address_pmd_flag flag)
1314 {
1315         pmd_t *pmd, *ret = NULL;
1316
1317         if (address & ~HPAGE_PMD_MASK)
1318                 goto out;
1319
1320         pmd = mm_find_pmd(mm, address);
1321         if (!pmd)
1322                 goto out;
1323         if (pmd_none(*pmd))
1324                 goto out;
1325         if (pmd_page(*pmd) != page)
1326                 goto out;
1327         /*
1328          * split_vma() may create temporary aliased mappings. There is
1329          * no risk as long as all huge pmd are found and have their
1330          * splitting bit set before __split_huge_page_refcount
1331          * runs. Finding the same huge pmd more than once during the
1332          * same rmap walk is not a problem.
1333          */
1334         if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1335             pmd_trans_splitting(*pmd))
1336                 goto out;
1337         if (pmd_trans_huge(*pmd)) {
1338                 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1339                           !pmd_trans_splitting(*pmd));
1340                 ret = pmd;
1341         }
1342 out:
1343         return ret;
1344 }
1345
1346 static int __split_huge_page_splitting(struct page *page,
1347                                        struct vm_area_struct *vma,
1348                                        unsigned long address)
1349 {
1350         struct mm_struct *mm = vma->vm_mm;
1351         pmd_t *pmd;
1352         int ret = 0;
1353         /* For mmu_notifiers */
1354         const unsigned long mmun_start = address;
1355         const unsigned long mmun_end   = address + HPAGE_PMD_SIZE;
1356
1357         mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1358         spin_lock(&mm->page_table_lock);
1359         pmd = page_check_address_pmd(page, mm, address,
1360                                      PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1361         if (pmd) {
1362                 /*
1363                  * We can't temporarily set the pmd to null in order
1364                  * to split it, the pmd must remain marked huge at all
1365                  * times or the VM won't take the pmd_trans_huge paths
1366                  * and it won't wait on the anon_vma->root->mutex to
1367                  * serialize against split_huge_page*.
1368                  */
1369                 pmdp_splitting_flush(vma, address, pmd);
1370                 ret = 1;
1371         }
1372         spin_unlock(&mm->page_table_lock);
1373         mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1374
1375         return ret;
1376 }
1377
1378 static void __split_huge_page_refcount(struct page *page)
1379 {
1380         int i;
1381         struct zone *zone = page_zone(page);
1382         struct lruvec *lruvec;
1383         int tail_count = 0;
1384
1385         /* prevent PageLRU to go away from under us, and freeze lru stats */
1386         spin_lock_irq(&zone->lru_lock);
1387         lruvec = mem_cgroup_page_lruvec(page, zone);
1388
1389         compound_lock(page);
1390         /* complete memcg works before add pages to LRU */
1391         mem_cgroup_split_huge_fixup(page);
1392
1393         for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
1394                 struct page *page_tail = page + i;
1395
1396                 /* tail_page->_mapcount cannot change */
1397                 BUG_ON(page_mapcount(page_tail) < 0);
1398                 tail_count += page_mapcount(page_tail);
1399                 /* check for overflow */
1400                 BUG_ON(tail_count < 0);
1401                 BUG_ON(atomic_read(&page_tail->_count) != 0);
1402                 /*
1403                  * tail_page->_count is zero and not changing from
1404                  * under us. But get_page_unless_zero() may be running
1405                  * from under us on the tail_page. If we used
1406                  * atomic_set() below instead of atomic_add(), we
1407                  * would then run atomic_set() concurrently with
1408                  * get_page_unless_zero(), and atomic_set() is
1409                  * implemented in C not using locked ops. spin_unlock
1410                  * on x86 sometime uses locked ops because of PPro
1411                  * errata 66, 92, so unless somebody can guarantee
1412                  * atomic_set() here would be safe on all archs (and
1413                  * not only on x86), it's safer to use atomic_add().
1414                  */
1415                 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1416                            &page_tail->_count);
1417
1418                 /* after clearing PageTail the gup refcount can be released */
1419                 smp_mb();
1420
1421                 /*
1422                  * retain hwpoison flag of the poisoned tail page:
1423                  *   fix for the unsuitable process killed on Guest Machine(KVM)
1424                  *   by the memory-failure.
1425                  */
1426                 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
1427                 page_tail->flags |= (page->flags &
1428                                      ((1L << PG_referenced) |
1429                                       (1L << PG_swapbacked) |
1430                                       (1L << PG_mlocked) |
1431                                       (1L << PG_uptodate)));
1432                 page_tail->flags |= (1L << PG_dirty);
1433
1434                 /* clear PageTail before overwriting first_page */
1435                 smp_wmb();
1436
1437                 /*
1438                  * __split_huge_page_splitting() already set the
1439                  * splitting bit in all pmd that could map this
1440                  * hugepage, that will ensure no CPU can alter the
1441                  * mapcount on the head page. The mapcount is only
1442                  * accounted in the head page and it has to be
1443                  * transferred to all tail pages in the below code. So
1444                  * for this code to be safe, the split the mapcount
1445                  * can't change. But that doesn't mean userland can't
1446                  * keep changing and reading the page contents while
1447                  * we transfer the mapcount, so the pmd splitting
1448                  * status is achieved setting a reserved bit in the
1449                  * pmd, not by clearing the present bit.
1450                 */
1451                 page_tail->_mapcount = page->_mapcount;
1452
1453                 BUG_ON(page_tail->mapping);
1454                 page_tail->mapping = page->mapping;
1455
1456                 page_tail->index = page->index + i;
1457
1458                 BUG_ON(!PageAnon(page_tail));
1459                 BUG_ON(!PageUptodate(page_tail));
1460                 BUG_ON(!PageDirty(page_tail));
1461                 BUG_ON(!PageSwapBacked(page_tail));
1462
1463                 lru_add_page_tail(page, page_tail, lruvec);
1464         }
1465         atomic_sub(tail_count, &page->_count);
1466         BUG_ON(atomic_read(&page->_count) <= 0);
1467
1468         __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
1469         __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1470
1471         ClearPageCompound(page);
1472         compound_unlock(page);
1473         spin_unlock_irq(&zone->lru_lock);
1474
1475         for (i = 1; i < HPAGE_PMD_NR; i++) {
1476                 struct page *page_tail = page + i;
1477                 BUG_ON(page_count(page_tail) <= 0);
1478                 /*
1479                  * Tail pages may be freed if there wasn't any mapping
1480                  * like if add_to_swap() is running on a lru page that
1481                  * had its mapping zapped. And freeing these pages
1482                  * requires taking the lru_lock so we do the put_page
1483                  * of the tail pages after the split is complete.
1484                  */
1485                 put_page(page_tail);
1486         }
1487
1488         /*
1489          * Only the head page (now become a regular page) is required
1490          * to be pinned by the caller.
1491          */
1492         BUG_ON(page_count(page) <= 0);
1493 }
1494
1495 static int __split_huge_page_map(struct page *page,
1496                                  struct vm_area_struct *vma,
1497                                  unsigned long address)
1498 {
1499         struct mm_struct *mm = vma->vm_mm;
1500         pmd_t *pmd, _pmd;
1501         int ret = 0, i;
1502         pgtable_t pgtable;
1503         unsigned long haddr;
1504
1505         spin_lock(&mm->page_table_lock);
1506         pmd = page_check_address_pmd(page, mm, address,
1507                                      PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1508         if (pmd) {
1509                 pgtable = pgtable_trans_huge_withdraw(mm);
1510                 pmd_populate(mm, &_pmd, pgtable);
1511
1512                 haddr = address;
1513                 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1514                         pte_t *pte, entry;
1515                         BUG_ON(PageCompound(page+i));
1516                         entry = mk_pte(page + i, vma->vm_page_prot);
1517                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1518                         if (!pmd_write(*pmd))
1519                                 entry = pte_wrprotect(entry);
1520                         else
1521                                 BUG_ON(page_mapcount(page) != 1);
1522                         if (!pmd_young(*pmd))
1523                                 entry = pte_mkold(entry);
1524                         pte = pte_offset_map(&_pmd, haddr);
1525                         BUG_ON(!pte_none(*pte));
1526                         set_pte_at(mm, haddr, pte, entry);
1527                         pte_unmap(pte);
1528                 }
1529
1530                 smp_wmb(); /* make pte visible before pmd */
1531                 /*
1532                  * Up to this point the pmd is present and huge and
1533                  * userland has the whole access to the hugepage
1534                  * during the split (which happens in place). If we
1535                  * overwrite the pmd with the not-huge version
1536                  * pointing to the pte here (which of course we could
1537                  * if all CPUs were bug free), userland could trigger
1538                  * a small page size TLB miss on the small sized TLB
1539                  * while the hugepage TLB entry is still established
1540                  * in the huge TLB. Some CPU doesn't like that. See
1541                  * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1542                  * Erratum 383 on page 93. Intel should be safe but is
1543                  * also warns that it's only safe if the permission
1544                  * and cache attributes of the two entries loaded in
1545                  * the two TLB is identical (which should be the case
1546                  * here). But it is generally safer to never allow
1547                  * small and huge TLB entries for the same virtual
1548                  * address to be loaded simultaneously. So instead of
1549                  * doing "pmd_populate(); flush_tlb_range();" we first
1550                  * mark the current pmd notpresent (atomically because
1551                  * here the pmd_trans_huge and pmd_trans_splitting
1552                  * must remain set at all times on the pmd until the
1553                  * split is complete for this pmd), then we flush the
1554                  * SMP TLB and finally we write the non-huge version
1555                  * of the pmd entry with pmd_populate.
1556                  */
1557                 pmdp_invalidate(vma, address, pmd);
1558                 pmd_populate(mm, pmd, pgtable);
1559                 ret = 1;
1560         }
1561         spin_unlock(&mm->page_table_lock);
1562
1563         return ret;
1564 }
1565
1566 /* must be called with anon_vma->root->mutex hold */
1567 static void __split_huge_page(struct page *page,
1568                               struct anon_vma *anon_vma)
1569 {
1570         int mapcount, mapcount2;
1571         pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
1572         struct anon_vma_chain *avc;
1573
1574         BUG_ON(!PageHead(page));
1575         BUG_ON(PageTail(page));
1576
1577         mapcount = 0;
1578         anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1579                 struct vm_area_struct *vma = avc->vma;
1580                 unsigned long addr = vma_address(page, vma);
1581                 BUG_ON(is_vma_temporary_stack(vma));
1582                 mapcount += __split_huge_page_splitting(page, vma, addr);
1583         }
1584         /*
1585          * It is critical that new vmas are added to the tail of the
1586          * anon_vma list. This guarantes that if copy_huge_pmd() runs
1587          * and establishes a child pmd before
1588          * __split_huge_page_splitting() freezes the parent pmd (so if
1589          * we fail to prevent copy_huge_pmd() from running until the
1590          * whole __split_huge_page() is complete), we will still see
1591          * the newly established pmd of the child later during the
1592          * walk, to be able to set it as pmd_trans_splitting too.
1593          */
1594         if (mapcount != page_mapcount(page))
1595                 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1596                        mapcount, page_mapcount(page));
1597         BUG_ON(mapcount != page_mapcount(page));
1598
1599         __split_huge_page_refcount(page);
1600
1601         mapcount2 = 0;
1602         anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
1603                 struct vm_area_struct *vma = avc->vma;
1604                 unsigned long addr = vma_address(page, vma);
1605                 BUG_ON(is_vma_temporary_stack(vma));
1606                 mapcount2 += __split_huge_page_map(page, vma, addr);
1607         }
1608         if (mapcount != mapcount2)
1609                 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1610                        mapcount, mapcount2, page_mapcount(page));
1611         BUG_ON(mapcount != mapcount2);
1612 }
1613
1614 int split_huge_page(struct page *page)
1615 {
1616         struct anon_vma *anon_vma;
1617         int ret = 1;
1618
1619         BUG_ON(!PageAnon(page));
1620         anon_vma = page_lock_anon_vma(page);
1621         if (!anon_vma)
1622                 goto out;
1623         ret = 0;
1624         if (!PageCompound(page))
1625                 goto out_unlock;
1626
1627         BUG_ON(!PageSwapBacked(page));
1628         __split_huge_page(page, anon_vma);
1629         count_vm_event(THP_SPLIT);
1630
1631         BUG_ON(PageCompound(page));
1632 out_unlock:
1633         page_unlock_anon_vma(anon_vma);
1634 out:
1635         return ret;
1636 }
1637
1638 #define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1639
1640 int hugepage_madvise(struct vm_area_struct *vma,
1641                      unsigned long *vm_flags, int advice)
1642 {
1643         struct mm_struct *mm = vma->vm_mm;
1644
1645         switch (advice) {
1646         case MADV_HUGEPAGE:
1647                 /*
1648                  * Be somewhat over-protective like KSM for now!
1649                  */
1650                 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
1651                         return -EINVAL;
1652                 if (mm->def_flags & VM_NOHUGEPAGE)
1653                         return -EINVAL;
1654                 *vm_flags &= ~VM_NOHUGEPAGE;
1655                 *vm_flags |= VM_HUGEPAGE;
1656                 /*
1657                  * If the vma become good for khugepaged to scan,
1658                  * register it here without waiting a page fault that
1659                  * may not happen any time soon.
1660                  */
1661                 if (unlikely(khugepaged_enter_vma_merge(vma)))
1662                         return -ENOMEM;
1663                 break;
1664         case MADV_NOHUGEPAGE:
1665                 /*
1666                  * Be somewhat over-protective like KSM for now!
1667                  */
1668                 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
1669                         return -EINVAL;
1670                 *vm_flags &= ~VM_HUGEPAGE;
1671                 *vm_flags |= VM_NOHUGEPAGE;
1672                 /*
1673                  * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1674                  * this vma even if we leave the mm registered in khugepaged if
1675                  * it got registered before VM_NOHUGEPAGE was set.
1676                  */
1677                 break;
1678         }
1679
1680         return 0;
1681 }
1682
1683 static int __init khugepaged_slab_init(void)
1684 {
1685         mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1686                                           sizeof(struct mm_slot),
1687                                           __alignof__(struct mm_slot), 0, NULL);
1688         if (!mm_slot_cache)
1689                 return -ENOMEM;
1690
1691         return 0;
1692 }
1693
1694 static void __init khugepaged_slab_free(void)
1695 {
1696         kmem_cache_destroy(mm_slot_cache);
1697         mm_slot_cache = NULL;
1698 }
1699
1700 static inline struct mm_slot *alloc_mm_slot(void)
1701 {
1702         if (!mm_slot_cache)     /* initialization failed */
1703                 return NULL;
1704         return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1705 }
1706
1707 static inline void free_mm_slot(struct mm_slot *mm_slot)
1708 {
1709         kmem_cache_free(mm_slot_cache, mm_slot);
1710 }
1711
1712 static int __init mm_slots_hash_init(void)
1713 {
1714         mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1715                                 GFP_KERNEL);
1716         if (!mm_slots_hash)
1717                 return -ENOMEM;
1718         return 0;
1719 }
1720
1721 #if 0
1722 static void __init mm_slots_hash_free(void)
1723 {
1724         kfree(mm_slots_hash);
1725         mm_slots_hash = NULL;
1726 }
1727 #endif
1728
1729 static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1730 {
1731         struct mm_slot *mm_slot;
1732         struct hlist_head *bucket;
1733         struct hlist_node *node;
1734
1735         bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1736                                 % MM_SLOTS_HASH_HEADS];
1737         hlist_for_each_entry(mm_slot, node, bucket, hash) {
1738                 if (mm == mm_slot->mm)
1739                         return mm_slot;
1740         }
1741         return NULL;
1742 }
1743
1744 static void insert_to_mm_slots_hash(struct mm_struct *mm,
1745                                     struct mm_slot *mm_slot)
1746 {
1747         struct hlist_head *bucket;
1748
1749         bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1750                                 % MM_SLOTS_HASH_HEADS];
1751         mm_slot->mm = mm;
1752         hlist_add_head(&mm_slot->hash, bucket);
1753 }
1754
1755 static inline int khugepaged_test_exit(struct mm_struct *mm)
1756 {
1757         return atomic_read(&mm->mm_users) == 0;
1758 }
1759
1760 int __khugepaged_enter(struct mm_struct *mm)
1761 {
1762         struct mm_slot *mm_slot;
1763         int wakeup;
1764
1765         mm_slot = alloc_mm_slot();
1766         if (!mm_slot)
1767                 return -ENOMEM;
1768
1769         /* __khugepaged_exit() must not run from under us */
1770         VM_BUG_ON(khugepaged_test_exit(mm));
1771         if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1772                 free_mm_slot(mm_slot);
1773                 return 0;
1774         }
1775
1776         spin_lock(&khugepaged_mm_lock);
1777         insert_to_mm_slots_hash(mm, mm_slot);
1778         /*
1779          * Insert just behind the scanning cursor, to let the area settle
1780          * down a little.
1781          */
1782         wakeup = list_empty(&khugepaged_scan.mm_head);
1783         list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1784         spin_unlock(&khugepaged_mm_lock);
1785
1786         atomic_inc(&mm->mm_count);
1787         if (wakeup)
1788                 wake_up_interruptible(&khugepaged_wait);
1789
1790         return 0;
1791 }
1792
1793 int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1794 {
1795         unsigned long hstart, hend;
1796         if (!vma->anon_vma)
1797                 /*
1798                  * Not yet faulted in so we will register later in the
1799                  * page fault if needed.
1800                  */
1801                 return 0;
1802         if (vma->vm_ops)
1803                 /* khugepaged not yet working on file or special mappings */
1804                 return 0;
1805         VM_BUG_ON(vma->vm_flags & VM_NO_THP);
1806         hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1807         hend = vma->vm_end & HPAGE_PMD_MASK;
1808         if (hstart < hend)
1809                 return khugepaged_enter(vma);
1810         return 0;
1811 }
1812
1813 void __khugepaged_exit(struct mm_struct *mm)
1814 {
1815         struct mm_slot *mm_slot;
1816         int free = 0;
1817
1818         spin_lock(&khugepaged_mm_lock);
1819         mm_slot = get_mm_slot(mm);
1820         if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1821                 hlist_del(&mm_slot->hash);
1822                 list_del(&mm_slot->mm_node);
1823                 free = 1;
1824         }
1825         spin_unlock(&khugepaged_mm_lock);
1826
1827         if (free) {
1828                 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1829                 free_mm_slot(mm_slot);
1830                 mmdrop(mm);
1831         } else if (mm_slot) {
1832                 /*
1833                  * This is required to serialize against
1834                  * khugepaged_test_exit() (which is guaranteed to run
1835                  * under mmap sem read mode). Stop here (after we
1836                  * return all pagetables will be destroyed) until
1837                  * khugepaged has finished working on the pagetables
1838                  * under the mmap_sem.
1839                  */
1840                 down_write(&mm->mmap_sem);
1841                 up_write(&mm->mmap_sem);
1842         }
1843 }
1844
1845 static void release_pte_page(struct page *page)
1846 {
1847         /* 0 stands for page_is_file_cache(page) == false */
1848         dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1849         unlock_page(page);
1850         putback_lru_page(page);
1851 }
1852
1853 static void release_pte_pages(pte_t *pte, pte_t *_pte)
1854 {
1855         while (--_pte >= pte) {
1856                 pte_t pteval = *_pte;
1857                 if (!pte_none(pteval))
1858                         release_pte_page(pte_page(pteval));
1859         }
1860 }
1861
1862 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1863                                         unsigned long address,
1864                                         pte_t *pte)
1865 {
1866         struct page *page;
1867         pte_t *_pte;
1868         int referenced = 0, none = 0;
1869         for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1870              _pte++, address += PAGE_SIZE) {
1871                 pte_t pteval = *_pte;
1872                 if (pte_none(pteval)) {
1873                         if (++none <= khugepaged_max_ptes_none)
1874                                 continue;
1875                         else
1876                                 goto out;
1877                 }
1878                 if (!pte_present(pteval) || !pte_write(pteval))
1879                         goto out;
1880                 page = vm_normal_page(vma, address, pteval);
1881                 if (unlikely(!page))
1882                         goto out;
1883
1884                 VM_BUG_ON(PageCompound(page));
1885                 BUG_ON(!PageAnon(page));
1886                 VM_BUG_ON(!PageSwapBacked(page));
1887
1888                 /* cannot use mapcount: can't collapse if there's a gup pin */
1889                 if (page_count(page) != 1)
1890                         goto out;
1891                 /*
1892                  * We can do it before isolate_lru_page because the
1893                  * page can't be freed from under us. NOTE: PG_lock
1894                  * is needed to serialize against split_huge_page
1895                  * when invoked from the VM.
1896                  */
1897                 if (!trylock_page(page))
1898                         goto out;
1899                 /*
1900                  * Isolate the page to avoid collapsing an hugepage
1901                  * currently in use by the VM.
1902                  */
1903                 if (isolate_lru_page(page)) {
1904                         unlock_page(page);
1905                         goto out;
1906                 }
1907                 /* 0 stands for page_is_file_cache(page) == false */
1908                 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1909                 VM_BUG_ON(!PageLocked(page));
1910                 VM_BUG_ON(PageLRU(page));
1911
1912                 /* If there is no mapped pte young don't collapse the page */
1913                 if (pte_young(pteval) || PageReferenced(page) ||
1914                     mmu_notifier_test_young(vma->vm_mm, address))
1915                         referenced = 1;
1916         }
1917         if (likely(referenced))
1918                 return 1;
1919 out:
1920         release_pte_pages(pte, _pte);
1921         return 0;
1922 }
1923
1924 static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1925                                       struct vm_area_struct *vma,
1926                                       unsigned long address,
1927                                       spinlock_t *ptl)
1928 {
1929         pte_t *_pte;
1930         for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1931                 pte_t pteval = *_pte;
1932                 struct page *src_page;
1933
1934                 if (pte_none(pteval)) {
1935                         clear_user_highpage(page, address);
1936                         add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1937                 } else {
1938                         src_page = pte_page(pteval);
1939                         copy_user_highpage(page, src_page, address, vma);
1940                         VM_BUG_ON(page_mapcount(src_page) != 1);
1941                         release_pte_page(src_page);
1942                         /*
1943                          * ptl mostly unnecessary, but preempt has to
1944                          * be disabled to update the per-cpu stats
1945                          * inside page_remove_rmap().
1946                          */
1947                         spin_lock(ptl);
1948                         /*
1949                          * paravirt calls inside pte_clear here are
1950                          * superfluous.
1951                          */
1952                         pte_clear(vma->vm_mm, address, _pte);
1953                         page_remove_rmap(src_page);
1954                         spin_unlock(ptl);
1955                         free_page_and_swap_cache(src_page);
1956                 }
1957
1958                 address += PAGE_SIZE;
1959                 page++;
1960         }
1961 }
1962
1963 static void khugepaged_alloc_sleep(void)
1964 {
1965         wait_event_freezable_timeout(khugepaged_wait, false,
1966                         msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
1967 }
1968
1969 #ifdef CONFIG_NUMA
1970 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
1971 {
1972         if (IS_ERR(*hpage)) {
1973                 if (!*wait)
1974                         return false;
1975
1976                 *wait = false;
1977                 *hpage = NULL;
1978                 khugepaged_alloc_sleep();
1979         } else if (*hpage) {
1980                 put_page(*hpage);
1981                 *hpage = NULL;
1982         }
1983
1984         return true;
1985 }
1986
1987 static struct page
1988 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
1989                        struct vm_area_struct *vma, unsigned long address,
1990                        int node)
1991 {
1992         VM_BUG_ON(*hpage);
1993         /*
1994          * Allocate the page while the vma is still valid and under
1995          * the mmap_sem read mode so there is no memory allocation
1996          * later when we take the mmap_sem in write mode. This is more
1997          * friendly behavior (OTOH it may actually hide bugs) to
1998          * filesystems in userland with daemons allocating memory in
1999          * the userland I/O paths.  Allocating memory with the
2000          * mmap_sem in read mode is good idea also to allow greater
2001          * scalability.
2002          */
2003         *hpage  = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2004                                       node, __GFP_OTHER_NODE);
2005
2006         /*
2007          * After allocating the hugepage, release the mmap_sem read lock in
2008          * preparation for taking it in write mode.
2009          */
2010         up_read(&mm->mmap_sem);
2011         if (unlikely(!*hpage)) {
2012                 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2013                 *hpage = ERR_PTR(-ENOMEM);
2014                 return NULL;
2015         }
2016
2017         count_vm_event(THP_COLLAPSE_ALLOC);
2018         return *hpage;
2019 }
2020 #else
2021 static struct page *khugepaged_alloc_hugepage(bool *wait)
2022 {
2023         struct page *hpage;
2024
2025         do {
2026                 hpage = alloc_hugepage(khugepaged_defrag());
2027                 if (!hpage) {
2028                         count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2029                         if (!*wait)
2030                                 return NULL;
2031
2032                         *wait = false;
2033                         khugepaged_alloc_sleep();
2034                 } else
2035                         count_vm_event(THP_COLLAPSE_ALLOC);
2036         } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2037
2038         return hpage;
2039 }
2040
2041 static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2042 {
2043         if (!*hpage)
2044                 *hpage = khugepaged_alloc_hugepage(wait);
2045
2046         if (unlikely(!*hpage))
2047                 return false;
2048
2049         return true;
2050 }
2051
2052 static struct page
2053 *khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2054                        struct vm_area_struct *vma, unsigned long address,
2055                        int node)
2056 {
2057         up_read(&mm->mmap_sem);
2058         VM_BUG_ON(!*hpage);
2059         return  *hpage;
2060 }
2061 #endif
2062
2063 static bool hugepage_vma_check(struct vm_area_struct *vma)
2064 {
2065         if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2066             (vma->vm_flags & VM_NOHUGEPAGE))
2067                 return false;
2068
2069         if (!vma->anon_vma || vma->vm_ops)
2070                 return false;
2071         if (is_vma_temporary_stack(vma))
2072                 return false;
2073         VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2074         return true;
2075 }
2076
2077 static void collapse_huge_page(struct mm_struct *mm,
2078                                    unsigned long address,
2079                                    struct page **hpage,
2080                                    struct vm_area_struct *vma,
2081                                    int node)
2082 {
2083         pmd_t *pmd, _pmd;
2084         pte_t *pte;
2085         pgtable_t pgtable;
2086         struct page *new_page;
2087         spinlock_t *ptl;
2088         int isolated;
2089         unsigned long hstart, hend;
2090         unsigned long mmun_start;       /* For mmu_notifiers */
2091         unsigned long mmun_end;         /* For mmu_notifiers */
2092
2093         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2094
2095         /* release the mmap_sem read lock. */
2096         new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2097         if (!new_page)
2098                 return;
2099
2100         if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
2101                 return;
2102
2103         /*
2104          * Prevent all access to pagetables with the exception of
2105          * gup_fast later hanlded by the ptep_clear_flush and the VM
2106          * handled by the anon_vma lock + PG_lock.
2107          */
2108         down_write(&mm->mmap_sem);
2109         if (unlikely(khugepaged_test_exit(mm)))
2110                 goto out;
2111
2112         vma = find_vma(mm, address);
2113         hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2114         hend = vma->vm_end & HPAGE_PMD_MASK;
2115         if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2116                 goto out;
2117         if (!hugepage_vma_check(vma))
2118                 goto out;
2119         pmd = mm_find_pmd(mm, address);
2120         if (!pmd)
2121                 goto out;
2122         if (pmd_trans_huge(*pmd))
2123                 goto out;
2124
2125         anon_vma_lock(vma->anon_vma);
2126
2127         pte = pte_offset_map(pmd, address);
2128         ptl = pte_lockptr(mm, pmd);
2129
2130         mmun_start = address;
2131         mmun_end   = address + HPAGE_PMD_SIZE;
2132         mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
2133         spin_lock(&mm->page_table_lock); /* probably unnecessary */
2134         /*
2135          * After this gup_fast can't run anymore. This also removes
2136          * any huge TLB entry from the CPU so we won't allow
2137          * huge and small TLB entries for the same virtual address
2138          * to avoid the risk of CPU bugs in that area.
2139          */
2140         _pmd = pmdp_clear_flush(vma, address, pmd);
2141         spin_unlock(&mm->page_table_lock);
2142         mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2143
2144         spin_lock(ptl);
2145         isolated = __collapse_huge_page_isolate(vma, address, pte);
2146         spin_unlock(ptl);
2147
2148         if (unlikely(!isolated)) {
2149                 pte_unmap(pte);
2150                 spin_lock(&mm->page_table_lock);
2151                 BUG_ON(!pmd_none(*pmd));
2152                 set_pmd_at(mm, address, pmd, _pmd);
2153                 spin_unlock(&mm->page_table_lock);
2154                 anon_vma_unlock(vma->anon_vma);
2155                 goto out;
2156         }
2157
2158         /*
2159          * All pages are isolated and locked so anon_vma rmap
2160          * can't run anymore.
2161          */
2162         anon_vma_unlock(vma->anon_vma);
2163
2164         __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
2165         pte_unmap(pte);
2166         __SetPageUptodate(new_page);
2167         pgtable = pmd_pgtable(_pmd);
2168
2169         _pmd = mk_huge_pmd(new_page, vma);
2170
2171         /*
2172          * spin_lock() below is not the equivalent of smp_wmb(), so
2173          * this is needed to avoid the copy_huge_page writes to become
2174          * visible after the set_pmd_at() write.
2175          */
2176         smp_wmb();
2177
2178         spin_lock(&mm->page_table_lock);
2179         BUG_ON(!pmd_none(*pmd));
2180         page_add_new_anon_rmap(new_page, vma, address);
2181         set_pmd_at(mm, address, pmd, _pmd);
2182         update_mmu_cache_pmd(vma, address, pmd);
2183         pgtable_trans_huge_deposit(mm, pgtable);
2184         spin_unlock(&mm->page_table_lock);
2185
2186         *hpage = NULL;
2187
2188         khugepaged_pages_collapsed++;
2189 out_up_write:
2190         up_write(&mm->mmap_sem);
2191         return;
2192
2193 out:
2194         mem_cgroup_uncharge_page(new_page);
2195         goto out_up_write;
2196 }
2197
2198 static int khugepaged_scan_pmd(struct mm_struct *mm,
2199                                struct vm_area_struct *vma,
2200                                unsigned long address,
2201                                struct page **hpage)
2202 {
2203         pmd_t *pmd;
2204         pte_t *pte, *_pte;
2205         int ret = 0, referenced = 0, none = 0;
2206         struct page *page;
2207         unsigned long _address;
2208         spinlock_t *ptl;
2209         int node = -1;
2210
2211         VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2212
2213         pmd = mm_find_pmd(mm, address);
2214         if (!pmd)
2215                 goto out;
2216         if (pmd_trans_huge(*pmd))
2217                 goto out;
2218
2219         pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2220         for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2221              _pte++, _address += PAGE_SIZE) {
2222                 pte_t pteval = *_pte;
2223                 if (pte_none(pteval)) {
2224                         if (++none <= khugepaged_max_ptes_none)
2225                                 continue;
2226                         else
2227                                 goto out_unmap;
2228                 }
2229                 if (!pte_present(pteval) || !pte_write(pteval))
2230                         goto out_unmap;
2231                 page = vm_normal_page(vma, _address, pteval);
2232                 if (unlikely(!page))
2233                         goto out_unmap;
2234                 /*
2235                  * Chose the node of the first page. This could
2236                  * be more sophisticated and look at more pages,
2237                  * but isn't for now.
2238                  */
2239                 if (node == -1)
2240                         node = page_to_nid(page);
2241                 VM_BUG_ON(PageCompound(page));
2242                 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2243                         goto out_unmap;
2244                 /* cannot use mapcount: can't collapse if there's a gup pin */
2245                 if (page_count(page) != 1)
2246                         goto out_unmap;
2247                 if (pte_young(pteval) || PageReferenced(page) ||
2248                     mmu_notifier_test_young(vma->vm_mm, address))
2249                         referenced = 1;
2250         }
2251         if (referenced)
2252                 ret = 1;
2253 out_unmap:
2254         pte_unmap_unlock(pte, ptl);
2255         if (ret)
2256                 /* collapse_huge_page will return with the mmap_sem released */
2257                 collapse_huge_page(mm, address, hpage, vma, node);
2258 out:
2259         return ret;
2260 }
2261
2262 static void collect_mm_slot(struct mm_slot *mm_slot)
2263 {
2264         struct mm_struct *mm = mm_slot->mm;
2265
2266         VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2267
2268         if (khugepaged_test_exit(mm)) {
2269                 /* free mm_slot */
2270                 hlist_del(&mm_slot->hash);
2271                 list_del(&mm_slot->mm_node);
2272
2273                 /*
2274                  * Not strictly needed because the mm exited already.
2275                  *
2276                  * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2277                  */
2278
2279                 /* khugepaged_mm_lock actually not necessary for the below */
2280                 free_mm_slot(mm_slot);
2281                 mmdrop(mm);
2282         }
2283 }
2284
2285 static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2286                                             struct page **hpage)
2287         __releases(&khugepaged_mm_lock)
2288         __acquires(&khugepaged_mm_lock)
2289 {
2290         struct mm_slot *mm_slot;
2291         struct mm_struct *mm;
2292         struct vm_area_struct *vma;
2293         int progress = 0;
2294
2295         VM_BUG_ON(!pages);
2296         VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
2297
2298         if (khugepaged_scan.mm_slot)
2299                 mm_slot = khugepaged_scan.mm_slot;
2300         else {
2301                 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2302                                      struct mm_slot, mm_node);
2303                 khugepaged_scan.address = 0;
2304                 khugepaged_scan.mm_slot = mm_slot;
2305         }
2306         spin_unlock(&khugepaged_mm_lock);
2307
2308         mm = mm_slot->mm;
2309         down_read(&mm->mmap_sem);
2310         if (unlikely(khugepaged_test_exit(mm)))
2311                 vma = NULL;
2312         else
2313                 vma = find_vma(mm, khugepaged_scan.address);
2314
2315         progress++;
2316         for (; vma; vma = vma->vm_next) {
2317                 unsigned long hstart, hend;
2318
2319                 cond_resched();
2320                 if (unlikely(khugepaged_test_exit(mm))) {
2321                         progress++;
2322                         break;
2323                 }
2324                 if (!hugepage_vma_check(vma)) {
2325 skip:
2326                         progress++;
2327                         continue;
2328                 }
2329                 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2330                 hend = vma->vm_end & HPAGE_PMD_MASK;
2331                 if (hstart >= hend)
2332                         goto skip;
2333                 if (khugepaged_scan.address > hend)
2334                         goto skip;
2335                 if (khugepaged_scan.address < hstart)
2336                         khugepaged_scan.address = hstart;
2337                 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2338
2339                 while (khugepaged_scan.address < hend) {
2340                         int ret;
2341                         cond_resched();
2342                         if (unlikely(khugepaged_test_exit(mm)))
2343                                 goto breakouterloop;
2344
2345                         VM_BUG_ON(khugepaged_scan.address < hstart ||
2346                                   khugepaged_scan.address + HPAGE_PMD_SIZE >
2347                                   hend);
2348                         ret = khugepaged_scan_pmd(mm, vma,
2349                                                   khugepaged_scan.address,
2350                                                   hpage);
2351                         /* move to next address */
2352                         khugepaged_scan.address += HPAGE_PMD_SIZE;
2353                         progress += HPAGE_PMD_NR;
2354                         if (ret)
2355                                 /* we released mmap_sem so break loop */
2356                                 goto breakouterloop_mmap_sem;
2357                         if (progress >= pages)
2358                                 goto breakouterloop;
2359                 }
2360         }
2361 breakouterloop:
2362         up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2363 breakouterloop_mmap_sem:
2364
2365         spin_lock(&khugepaged_mm_lock);
2366         VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2367         /*
2368          * Release the current mm_slot if this mm is about to die, or
2369          * if we scanned all vmas of this mm.
2370          */
2371         if (khugepaged_test_exit(mm) || !vma) {
2372                 /*
2373                  * Make sure that if mm_users is reaching zero while
2374                  * khugepaged runs here, khugepaged_exit will find
2375                  * mm_slot not pointing to the exiting mm.
2376                  */
2377                 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2378                         khugepaged_scan.mm_slot = list_entry(
2379                                 mm_slot->mm_node.next,
2380                                 struct mm_slot, mm_node);
2381                         khugepaged_scan.address = 0;
2382                 } else {
2383                         khugepaged_scan.mm_slot = NULL;
2384                         khugepaged_full_scans++;
2385                 }
2386
2387                 collect_mm_slot(mm_slot);
2388         }
2389
2390         return progress;
2391 }
2392
2393 static int khugepaged_has_work(void)
2394 {
2395         return !list_empty(&khugepaged_scan.mm_head) &&
2396                 khugepaged_enabled();
2397 }
2398
2399 static int khugepaged_wait_event(void)
2400 {
2401         return !list_empty(&khugepaged_scan.mm_head) ||
2402                 kthread_should_stop();
2403 }
2404
2405 static void khugepaged_do_scan(void)
2406 {
2407         struct page *hpage = NULL;
2408         unsigned int progress = 0, pass_through_head = 0;
2409         unsigned int pages = khugepaged_pages_to_scan;
2410         bool wait = true;
2411
2412         barrier(); /* write khugepaged_pages_to_scan to local stack */
2413
2414         while (progress < pages) {
2415                 if (!khugepaged_prealloc_page(&hpage, &wait))
2416                         break;
2417
2418                 cond_resched();
2419
2420                 if (unlikely(kthread_should_stop() || freezing(current)))
2421                         break;
2422
2423                 spin_lock(&khugepaged_mm_lock);
2424                 if (!khugepaged_scan.mm_slot)
2425                         pass_through_head++;
2426                 if (khugepaged_has_work() &&
2427                     pass_through_head < 2)
2428                         progress += khugepaged_scan_mm_slot(pages - progress,
2429                                                             &hpage);
2430                 else
2431                         progress = pages;
2432                 spin_unlock(&khugepaged_mm_lock);
2433         }
2434
2435         if (!IS_ERR_OR_NULL(hpage))
2436                 put_page(hpage);
2437 }
2438
2439 static void khugepaged_wait_work(void)
2440 {
2441         try_to_freeze();
2442
2443         if (khugepaged_has_work()) {
2444                 if (!khugepaged_scan_sleep_millisecs)
2445                         return;
2446
2447                 wait_event_freezable_timeout(khugepaged_wait,
2448                                              kthread_should_stop(),
2449                         msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2450                 return;
2451         }
2452
2453         if (khugepaged_enabled())
2454                 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2455 }
2456
2457 static int khugepaged(void *none)
2458 {
2459         struct mm_slot *mm_slot;
2460
2461         set_freezable();
2462         set_user_nice(current, 19);
2463
2464         while (!kthread_should_stop()) {
2465                 khugepaged_do_scan();
2466                 khugepaged_wait_work();
2467         }
2468
2469         spin_lock(&khugepaged_mm_lock);
2470         mm_slot = khugepaged_scan.mm_slot;
2471         khugepaged_scan.mm_slot = NULL;
2472         if (mm_slot)
2473                 collect_mm_slot(mm_slot);
2474         spin_unlock(&khugepaged_mm_lock);
2475         return 0;
2476 }
2477
2478 void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2479                 pmd_t *pmd)
2480 {
2481         struct page *page;
2482         unsigned long haddr = address & HPAGE_PMD_MASK;
2483         struct mm_struct *mm = vma->vm_mm;
2484
2485         BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
2486
2487         spin_lock(&mm->page_table_lock);
2488         if (unlikely(!pmd_trans_huge(*pmd))) {
2489                 spin_unlock(&mm->page_table_lock);
2490                 return;
2491         }
2492         page = pmd_page(*pmd);
2493         VM_BUG_ON(!page_count(page));
2494         get_page(page);
2495         spin_unlock(&mm->page_table_lock);
2496
2497         split_huge_page(page);
2498
2499         put_page(page);
2500         BUG_ON(pmd_trans_huge(*pmd));
2501 }
2502
2503 void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2504                 pmd_t *pmd)
2505 {
2506         struct vm_area_struct *vma;
2507
2508         vma = find_vma(mm, address);
2509         BUG_ON(vma == NULL);
2510         split_huge_page_pmd(vma, address, pmd);
2511 }
2512
2513 static void split_huge_page_address(struct mm_struct *mm,
2514                                     unsigned long address)
2515 {
2516         pmd_t *pmd;
2517
2518         VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2519
2520         pmd = mm_find_pmd(mm, address);
2521         if (!pmd)
2522                 return;
2523         /*
2524          * Caller holds the mmap_sem write mode, so a huge pmd cannot
2525          * materialize from under us.
2526          */
2527         split_huge_page_pmd_mm(mm, address, pmd);
2528 }
2529
2530 void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2531                              unsigned long start,
2532                              unsigned long end,
2533                              long adjust_next)
2534 {
2535         /*
2536          * If the new start address isn't hpage aligned and it could
2537          * previously contain an hugepage: check if we need to split
2538          * an huge pmd.
2539          */
2540         if (start & ~HPAGE_PMD_MASK &&
2541             (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2542             (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2543                 split_huge_page_address(vma->vm_mm, start);
2544
2545         /*
2546          * If the new end address isn't hpage aligned and it could
2547          * previously contain an hugepage: check if we need to split
2548          * an huge pmd.
2549          */
2550         if (end & ~HPAGE_PMD_MASK &&
2551             (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2552             (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2553                 split_huge_page_address(vma->vm_mm, end);
2554
2555         /*
2556          * If we're also updating the vma->vm_next->vm_start, if the new
2557          * vm_next->vm_start isn't page aligned and it could previously
2558          * contain an hugepage: check if we need to split an huge pmd.
2559          */
2560         if (adjust_next > 0) {
2561                 struct vm_area_struct *next = vma->vm_next;
2562                 unsigned long nstart = next->vm_start;
2563                 nstart += adjust_next << PAGE_SHIFT;
2564                 if (nstart & ~HPAGE_PMD_MASK &&
2565                     (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2566                     (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2567                         split_huge_page_address(next->vm_mm, nstart);
2568         }
2569 }