blob: d89220cb1d9fc69942362ff3871042e469409879 [file] [log] [blame]
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001/*
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>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080015#include <linux/shrinker.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080016#include <linux/mm_inline.h>
17#include <linux/kthread.h>
18#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080019#include <linux/freezer.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080020#include <linux/mman.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020021#include <linux/pagemap.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080022
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080023#include <asm/tlb.h>
24#include <asm/pgalloc.h>
25#include "internal.h"
26
Andrea Arcangeliba761492011-01-13 15:46:58 -080027/*
28 * By default transparent hugepage support is enabled for all mappings
29 * and khugepaged scans all mappings. Defrag is only invoked by
30 * khugepaged hugepage allocations and by page faults inside
31 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
32 * allocations.
33 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080034unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080035#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080036 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080037#endif
38#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
39 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
40#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080041 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Andrea Arcangeliba761492011-01-13 15:46:58 -080042 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
43
44/* default scan 8*512 pte (or vmas) every 30 second */
45static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
46static unsigned int khugepaged_pages_collapsed;
47static unsigned int khugepaged_full_scans;
48static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
49/* during fragmentation poll the hugepage allocator once every minute */
50static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
51static struct task_struct *khugepaged_thread __read_mostly;
52static DEFINE_MUTEX(khugepaged_mutex);
53static DEFINE_SPINLOCK(khugepaged_mm_lock);
54static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
55/*
56 * default collapse hugepages if there is at least one pte mapped like
57 * it would have happened if the vma was large enough during page
58 * fault.
59 */
60static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
61
62static int khugepaged(void *none);
63static int mm_slots_hash_init(void);
64static int khugepaged_slab_init(void);
65static void khugepaged_slab_free(void);
66
67#define MM_SLOTS_HASH_HEADS 1024
68static struct hlist_head *mm_slots_hash __read_mostly;
69static struct kmem_cache *mm_slot_cache __read_mostly;
70
71/**
72 * struct mm_slot - hash lookup from mm to mm_slot
73 * @hash: hash collision list
74 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
75 * @mm: the mm that this information is valid for
76 */
77struct mm_slot {
78 struct hlist_node hash;
79 struct list_head mm_node;
80 struct mm_struct *mm;
81};
82
83/**
84 * struct khugepaged_scan - cursor for scanning
85 * @mm_head: the head of the mm list to scan
86 * @mm_slot: the current mm_slot we are scanning
87 * @address: the next address inside that to be scanned
88 *
89 * There is only the one khugepaged_scan instance of this cursor structure.
90 */
91struct khugepaged_scan {
92 struct list_head mm_head;
93 struct mm_slot *mm_slot;
94 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -070095};
96static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -080097 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
98};
99
Andrea Arcangelif0005652011-01-13 15:47:04 -0800100
101static int set_recommended_min_free_kbytes(void)
102{
103 struct zone *zone;
104 int nr_zones = 0;
105 unsigned long recommended_min;
106 extern int min_free_kbytes;
107
Xiao Guangrong17c230a2012-10-08 16:29:56 -0700108 if (!khugepaged_enabled())
Andrea Arcangelif0005652011-01-13 15:47:04 -0800109 return 0;
110
111 for_each_populated_zone(zone)
112 nr_zones++;
113
114 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
115 recommended_min = pageblock_nr_pages * nr_zones * 2;
116
117 /*
118 * Make sure that on average at least two pageblocks are almost free
119 * of another type, one for a migratetype to fall back to and a
120 * second to avoid subsequent fallbacks of other types There are 3
121 * MIGRATE_TYPES we care about.
122 */
123 recommended_min += pageblock_nr_pages * nr_zones *
124 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
125
126 /* don't ever allow to reserve more than 5% of the lowmem */
127 recommended_min = min(recommended_min,
128 (unsigned long) nr_free_buffer_pages() / 20);
129 recommended_min <<= (PAGE_SHIFT-10);
130
131 if (recommended_min > min_free_kbytes)
132 min_free_kbytes = recommended_min;
133 setup_per_zone_wmarks();
134 return 0;
135}
136late_initcall(set_recommended_min_free_kbytes);
137
Andrea Arcangeliba761492011-01-13 15:46:58 -0800138static int start_khugepaged(void)
139{
140 int err = 0;
141 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800142 if (!khugepaged_thread)
143 khugepaged_thread = kthread_run(khugepaged, NULL,
144 "khugepaged");
145 if (unlikely(IS_ERR(khugepaged_thread))) {
146 printk(KERN_ERR
147 "khugepaged: kthread_run(khugepaged) failed\n");
148 err = PTR_ERR(khugepaged_thread);
149 khugepaged_thread = NULL;
150 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700151
152 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800153 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800154
155 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700156 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700157 kthread_stop(khugepaged_thread);
158 khugepaged_thread = NULL;
159 }
Xiao Guangrong637e3a22012-10-08 16:29:38 -0700160
Andrea Arcangeliba761492011-01-13 15:46:58 -0800161 return err;
162}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800163
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800164static atomic_t huge_zero_refcount;
165static unsigned long huge_zero_pfn __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800166
167static inline bool is_huge_zero_pfn(unsigned long pfn)
168{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800169 unsigned long zero_pfn = ACCESS_ONCE(huge_zero_pfn);
170 return zero_pfn && pfn == zero_pfn;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800171}
172
173static inline bool is_huge_zero_pmd(pmd_t pmd)
174{
175 return is_huge_zero_pfn(pmd_pfn(pmd));
176}
177
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800178static unsigned long get_huge_zero_page(void)
179{
180 struct page *zero_page;
181retry:
182 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
183 return ACCESS_ONCE(huge_zero_pfn);
184
185 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
186 HPAGE_PMD_ORDER);
187 if (!zero_page)
188 return 0;
189 preempt_disable();
190 if (cmpxchg(&huge_zero_pfn, 0, page_to_pfn(zero_page))) {
191 preempt_enable();
192 __free_page(zero_page);
193 goto retry;
194 }
195
196 /* We take additional reference here. It will be put back by shrinker */
197 atomic_set(&huge_zero_refcount, 2);
198 preempt_enable();
199 return ACCESS_ONCE(huge_zero_pfn);
200}
201
202static void put_huge_zero_page(void)
203{
204 /*
205 * Counter should never go to zero here. Only shrinker can put
206 * last reference.
207 */
208 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
209}
210
211static int shrink_huge_zero_page(struct shrinker *shrink,
212 struct shrink_control *sc)
213{
214 if (!sc->nr_to_scan)
215 /* we can free zero page only if last reference remains */
216 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
217
218 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
219 unsigned long zero_pfn = xchg(&huge_zero_pfn, 0);
220 BUG_ON(zero_pfn == 0);
221 __free_page(__pfn_to_page(zero_pfn));
222 }
223
224 return 0;
225}
226
227static struct shrinker huge_zero_page_shrinker = {
228 .shrink = shrink_huge_zero_page,
229 .seeks = DEFAULT_SEEKS,
230};
231
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800232#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800233
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800234static ssize_t double_flag_show(struct kobject *kobj,
235 struct kobj_attribute *attr, char *buf,
236 enum transparent_hugepage_flag enabled,
237 enum transparent_hugepage_flag req_madv)
238{
239 if (test_bit(enabled, &transparent_hugepage_flags)) {
240 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
241 return sprintf(buf, "[always] madvise never\n");
242 } else if (test_bit(req_madv, &transparent_hugepage_flags))
243 return sprintf(buf, "always [madvise] never\n");
244 else
245 return sprintf(buf, "always madvise [never]\n");
246}
247static ssize_t double_flag_store(struct kobject *kobj,
248 struct kobj_attribute *attr,
249 const char *buf, size_t count,
250 enum transparent_hugepage_flag enabled,
251 enum transparent_hugepage_flag req_madv)
252{
253 if (!memcmp("always", buf,
254 min(sizeof("always")-1, count))) {
255 set_bit(enabled, &transparent_hugepage_flags);
256 clear_bit(req_madv, &transparent_hugepage_flags);
257 } else if (!memcmp("madvise", buf,
258 min(sizeof("madvise")-1, count))) {
259 clear_bit(enabled, &transparent_hugepage_flags);
260 set_bit(req_madv, &transparent_hugepage_flags);
261 } else if (!memcmp("never", buf,
262 min(sizeof("never")-1, count))) {
263 clear_bit(enabled, &transparent_hugepage_flags);
264 clear_bit(req_madv, &transparent_hugepage_flags);
265 } else
266 return -EINVAL;
267
268 return count;
269}
270
271static ssize_t enabled_show(struct kobject *kobj,
272 struct kobj_attribute *attr, char *buf)
273{
274 return double_flag_show(kobj, attr, buf,
275 TRANSPARENT_HUGEPAGE_FLAG,
276 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
277}
278static ssize_t enabled_store(struct kobject *kobj,
279 struct kobj_attribute *attr,
280 const char *buf, size_t count)
281{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800282 ssize_t ret;
283
284 ret = double_flag_store(kobj, attr, buf, count,
285 TRANSPARENT_HUGEPAGE_FLAG,
286 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
287
288 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700289 int err;
290
291 mutex_lock(&khugepaged_mutex);
292 err = start_khugepaged();
293 mutex_unlock(&khugepaged_mutex);
294
Andrea Arcangeliba761492011-01-13 15:46:58 -0800295 if (err)
296 ret = err;
297 }
298
299 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800300}
301static struct kobj_attribute enabled_attr =
302 __ATTR(enabled, 0644, enabled_show, enabled_store);
303
304static ssize_t single_flag_show(struct kobject *kobj,
305 struct kobj_attribute *attr, char *buf,
306 enum transparent_hugepage_flag flag)
307{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700308 return sprintf(buf, "%d\n",
309 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800310}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700311
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800312static ssize_t single_flag_store(struct kobject *kobj,
313 struct kobj_attribute *attr,
314 const char *buf, size_t count,
315 enum transparent_hugepage_flag flag)
316{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700317 unsigned long value;
318 int ret;
319
320 ret = kstrtoul(buf, 10, &value);
321 if (ret < 0)
322 return ret;
323 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800324 return -EINVAL;
325
Ben Hutchingse27e6152011-04-14 15:22:21 -0700326 if (value)
327 set_bit(flag, &transparent_hugepage_flags);
328 else
329 clear_bit(flag, &transparent_hugepage_flags);
330
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800331 return count;
332}
333
334/*
335 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
336 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
337 * memory just to allocate one more hugepage.
338 */
339static ssize_t defrag_show(struct kobject *kobj,
340 struct kobj_attribute *attr, char *buf)
341{
342 return double_flag_show(kobj, attr, buf,
343 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
344 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
345}
346static ssize_t defrag_store(struct kobject *kobj,
347 struct kobj_attribute *attr,
348 const char *buf, size_t count)
349{
350 return double_flag_store(kobj, attr, buf, count,
351 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
352 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
353}
354static struct kobj_attribute defrag_attr =
355 __ATTR(defrag, 0644, defrag_show, defrag_store);
356
357#ifdef CONFIG_DEBUG_VM
358static ssize_t debug_cow_show(struct kobject *kobj,
359 struct kobj_attribute *attr, char *buf)
360{
361 return single_flag_show(kobj, attr, buf,
362 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
363}
364static ssize_t debug_cow_store(struct kobject *kobj,
365 struct kobj_attribute *attr,
366 const char *buf, size_t count)
367{
368 return single_flag_store(kobj, attr, buf, count,
369 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
370}
371static struct kobj_attribute debug_cow_attr =
372 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
373#endif /* CONFIG_DEBUG_VM */
374
375static struct attribute *hugepage_attr[] = {
376 &enabled_attr.attr,
377 &defrag_attr.attr,
378#ifdef CONFIG_DEBUG_VM
379 &debug_cow_attr.attr,
380#endif
381 NULL,
382};
383
384static struct attribute_group hugepage_attr_group = {
385 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800386};
387
388static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
389 struct kobj_attribute *attr,
390 char *buf)
391{
392 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
393}
394
395static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
396 struct kobj_attribute *attr,
397 const char *buf, size_t count)
398{
399 unsigned long msecs;
400 int err;
401
402 err = strict_strtoul(buf, 10, &msecs);
403 if (err || msecs > UINT_MAX)
404 return -EINVAL;
405
406 khugepaged_scan_sleep_millisecs = msecs;
407 wake_up_interruptible(&khugepaged_wait);
408
409 return count;
410}
411static struct kobj_attribute scan_sleep_millisecs_attr =
412 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
413 scan_sleep_millisecs_store);
414
415static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
416 struct kobj_attribute *attr,
417 char *buf)
418{
419 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
420}
421
422static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
423 struct kobj_attribute *attr,
424 const char *buf, size_t count)
425{
426 unsigned long msecs;
427 int err;
428
429 err = strict_strtoul(buf, 10, &msecs);
430 if (err || msecs > UINT_MAX)
431 return -EINVAL;
432
433 khugepaged_alloc_sleep_millisecs = msecs;
434 wake_up_interruptible(&khugepaged_wait);
435
436 return count;
437}
438static struct kobj_attribute alloc_sleep_millisecs_attr =
439 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
440 alloc_sleep_millisecs_store);
441
442static ssize_t pages_to_scan_show(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 char *buf)
445{
446 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
447}
448static ssize_t pages_to_scan_store(struct kobject *kobj,
449 struct kobj_attribute *attr,
450 const char *buf, size_t count)
451{
452 int err;
453 unsigned long pages;
454
455 err = strict_strtoul(buf, 10, &pages);
456 if (err || !pages || pages > UINT_MAX)
457 return -EINVAL;
458
459 khugepaged_pages_to_scan = pages;
460
461 return count;
462}
463static struct kobj_attribute pages_to_scan_attr =
464 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
465 pages_to_scan_store);
466
467static ssize_t pages_collapsed_show(struct kobject *kobj,
468 struct kobj_attribute *attr,
469 char *buf)
470{
471 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
472}
473static struct kobj_attribute pages_collapsed_attr =
474 __ATTR_RO(pages_collapsed);
475
476static ssize_t full_scans_show(struct kobject *kobj,
477 struct kobj_attribute *attr,
478 char *buf)
479{
480 return sprintf(buf, "%u\n", khugepaged_full_scans);
481}
482static struct kobj_attribute full_scans_attr =
483 __ATTR_RO(full_scans);
484
485static ssize_t khugepaged_defrag_show(struct kobject *kobj,
486 struct kobj_attribute *attr, char *buf)
487{
488 return single_flag_show(kobj, attr, buf,
489 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
490}
491static ssize_t khugepaged_defrag_store(struct kobject *kobj,
492 struct kobj_attribute *attr,
493 const char *buf, size_t count)
494{
495 return single_flag_store(kobj, attr, buf, count,
496 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
497}
498static struct kobj_attribute khugepaged_defrag_attr =
499 __ATTR(defrag, 0644, khugepaged_defrag_show,
500 khugepaged_defrag_store);
501
502/*
503 * max_ptes_none controls if khugepaged should collapse hugepages over
504 * any unmapped ptes in turn potentially increasing the memory
505 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
506 * reduce the available free memory in the system as it
507 * runs. Increasing max_ptes_none will instead potentially reduce the
508 * free memory in the system during the khugepaged scan.
509 */
510static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 char *buf)
513{
514 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
515}
516static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
517 struct kobj_attribute *attr,
518 const char *buf, size_t count)
519{
520 int err;
521 unsigned long max_ptes_none;
522
523 err = strict_strtoul(buf, 10, &max_ptes_none);
524 if (err || max_ptes_none > HPAGE_PMD_NR-1)
525 return -EINVAL;
526
527 khugepaged_max_ptes_none = max_ptes_none;
528
529 return count;
530}
531static struct kobj_attribute khugepaged_max_ptes_none_attr =
532 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
533 khugepaged_max_ptes_none_store);
534
535static struct attribute *khugepaged_attr[] = {
536 &khugepaged_defrag_attr.attr,
537 &khugepaged_max_ptes_none_attr.attr,
538 &pages_to_scan_attr.attr,
539 &pages_collapsed_attr.attr,
540 &full_scans_attr.attr,
541 &scan_sleep_millisecs_attr.attr,
542 &alloc_sleep_millisecs_attr.attr,
543 NULL,
544};
545
546static struct attribute_group khugepaged_attr_group = {
547 .attrs = khugepaged_attr,
548 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800549};
Shaohua Li569e5592012-01-12 17:19:11 -0800550
551static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
552{
553 int err;
554
555 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
556 if (unlikely(!*hugepage_kobj)) {
557 printk(KERN_ERR "hugepage: failed kobject create\n");
558 return -ENOMEM;
559 }
560
561 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
562 if (err) {
563 printk(KERN_ERR "hugepage: failed register hugeage group\n");
564 goto delete_obj;
565 }
566
567 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
568 if (err) {
569 printk(KERN_ERR "hugepage: failed register hugeage group\n");
570 goto remove_hp_group;
571 }
572
573 return 0;
574
575remove_hp_group:
576 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
577delete_obj:
578 kobject_put(*hugepage_kobj);
579 return err;
580}
581
582static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
583{
584 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
585 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
586 kobject_put(hugepage_kobj);
587}
588#else
589static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
590{
591 return 0;
592}
593
594static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
595{
596}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800597#endif /* CONFIG_SYSFS */
598
599static int __init hugepage_init(void)
600{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800601 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800602 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800603
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800604 if (!has_transparent_hugepage()) {
605 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800606 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800607 }
608
Shaohua Li569e5592012-01-12 17:19:11 -0800609 err = hugepage_init_sysfs(&hugepage_kobj);
610 if (err)
611 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800612
613 err = khugepaged_slab_init();
614 if (err)
615 goto out;
616
617 err = mm_slots_hash_init();
618 if (err) {
619 khugepaged_slab_free();
620 goto out;
621 }
622
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800623 register_shrinker(&huge_zero_page_shrinker);
624
Rik van Riel97562cd2011-01-13 15:47:12 -0800625 /*
626 * By default disable transparent hugepages on smaller systems,
627 * where the extra memory used could hurt more than TLB overhead
628 * is likely to save. The admin can still enable it through /sys.
629 */
630 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
631 transparent_hugepage_flags = 0;
632
Andrea Arcangeliba761492011-01-13 15:46:58 -0800633 start_khugepaged();
634
Shaohua Li569e5592012-01-12 17:19:11 -0800635 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800636out:
Shaohua Li569e5592012-01-12 17:19:11 -0800637 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800638 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800639}
640module_init(hugepage_init)
641
642static int __init setup_transparent_hugepage(char *str)
643{
644 int ret = 0;
645 if (!str)
646 goto out;
647 if (!strcmp(str, "always")) {
648 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
649 &transparent_hugepage_flags);
650 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
651 &transparent_hugepage_flags);
652 ret = 1;
653 } else if (!strcmp(str, "madvise")) {
654 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
655 &transparent_hugepage_flags);
656 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
657 &transparent_hugepage_flags);
658 ret = 1;
659 } else if (!strcmp(str, "never")) {
660 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
661 &transparent_hugepage_flags);
662 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
663 &transparent_hugepage_flags);
664 ret = 1;
665 }
666out:
667 if (!ret)
668 printk(KERN_WARNING
669 "transparent_hugepage= cannot parse, ignored\n");
670 return ret;
671}
672__setup("transparent_hugepage=", setup_transparent_hugepage);
673
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800674static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
675{
676 if (likely(vma->vm_flags & VM_WRITE))
677 pmd = pmd_mkwrite(pmd);
678 return pmd;
679}
680
Bob Liub3092b32012-12-11 16:00:41 -0800681static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
682{
683 pmd_t entry;
684 entry = mk_pmd(page, vma->vm_page_prot);
685 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
686 entry = pmd_mkhuge(entry);
687 return entry;
688}
689
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800690static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
691 struct vm_area_struct *vma,
692 unsigned long haddr, pmd_t *pmd,
693 struct page *page)
694{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800695 pgtable_t pgtable;
696
697 VM_BUG_ON(!PageCompound(page));
698 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700699 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800700 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800701
702 clear_huge_page(page, haddr, HPAGE_PMD_NR);
703 __SetPageUptodate(page);
704
705 spin_lock(&mm->page_table_lock);
706 if (unlikely(!pmd_none(*pmd))) {
707 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800708 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800709 put_page(page);
710 pte_free(mm, pgtable);
711 } else {
712 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -0800713 entry = mk_huge_pmd(page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800714 /*
715 * The spinlocking to take the lru_lock inside
716 * page_add_new_anon_rmap() acts as a full memory
717 * barrier to be sure clear_huge_page writes become
718 * visible after the set_pmd_at() write.
719 */
720 page_add_new_anon_rmap(page, vma, haddr);
721 set_pmd_at(mm, haddr, pmd, entry);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700722 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800723 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800724 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800725 spin_unlock(&mm->page_table_lock);
726 }
727
David Rientjesaa2e8782012-05-29 15:06:17 -0700728 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800729}
730
Andi Kleencc5d4622011-03-22 16:33:13 -0700731static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800732{
Andi Kleencc5d4622011-03-22 16:33:13 -0700733 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800734}
735
736static inline struct page *alloc_hugepage_vma(int defrag,
737 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700738 unsigned long haddr, int nd,
739 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800740{
Andi Kleencc5d4622011-03-22 16:33:13 -0700741 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800742 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800743}
744
745#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800746static inline struct page *alloc_hugepage(int defrag)
747{
Andi Kleencc5d4622011-03-22 16:33:13 -0700748 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800749 HPAGE_PMD_ORDER);
750}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800751#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800752
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800753static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800754 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
755 unsigned long zero_pfn)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800756{
757 pmd_t entry;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800758 entry = pfn_pmd(zero_pfn, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800759 entry = pmd_wrprotect(entry);
760 entry = pmd_mkhuge(entry);
761 set_pmd_at(mm, haddr, pmd, entry);
762 pgtable_trans_huge_deposit(mm, pgtable);
763 mm->nr_ptes++;
764}
765
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800766int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
767 unsigned long address, pmd_t *pmd,
768 unsigned int flags)
769{
770 struct page *page;
771 unsigned long haddr = address & HPAGE_PMD_MASK;
772 pte_t *pte;
773
774 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
775 if (unlikely(anon_vma_prepare(vma)))
776 return VM_FAULT_OOM;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800777 if (unlikely(khugepaged_enter(vma)))
778 return VM_FAULT_OOM;
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800779 if (!(flags & FAULT_FLAG_WRITE)) {
780 pgtable_t pgtable;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800781 unsigned long zero_pfn;
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800782 pgtable = pte_alloc_one(mm, haddr);
783 if (unlikely(!pgtable))
784 return VM_FAULT_OOM;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800785 zero_pfn = get_huge_zero_page();
786 if (unlikely(!zero_pfn)) {
787 pte_free(mm, pgtable);
788 count_vm_event(THP_FAULT_FALLBACK);
789 goto out;
790 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800791 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800792 set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
793 zero_pfn);
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800794 spin_unlock(&mm->page_table_lock);
795 return 0;
796 }
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800797 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700798 vma, haddr, numa_node_id(), 0);
Andi Kleen81ab4202011-04-14 15:22:06 -0700799 if (unlikely(!page)) {
800 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800801 goto out;
Andi Kleen81ab4202011-04-14 15:22:06 -0700802 }
803 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800804 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
805 put_page(page);
806 goto out;
807 }
David Rientjesedad9d22012-05-29 15:06:17 -0700808 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
809 page))) {
810 mem_cgroup_uncharge_page(page);
811 put_page(page);
812 goto out;
813 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800814
David Rientjesedad9d22012-05-29 15:06:17 -0700815 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800816 }
817out:
818 /*
819 * Use __pte_alloc instead of pte_alloc_map, because we can't
820 * run pte_offset_map on the pmd, if an huge pmd could
821 * materialize from under us from a different thread.
822 */
823 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
824 return VM_FAULT_OOM;
825 /* if an huge pmd materialized from under us just retry later */
826 if (unlikely(pmd_trans_huge(*pmd)))
827 return 0;
828 /*
829 * A regular pmd is established and it can't morph into a huge pmd
830 * from under us anymore at this point because we hold the mmap_sem
831 * read mode and khugepaged takes it in write mode. So now it's
832 * safe to run pte_offset_map().
833 */
834 pte = pte_offset_map(pmd, address);
835 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
836}
837
838int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
839 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
840 struct vm_area_struct *vma)
841{
842 struct page *src_page;
843 pmd_t pmd;
844 pgtable_t pgtable;
845 int ret;
846
847 ret = -ENOMEM;
848 pgtable = pte_alloc_one(dst_mm, addr);
849 if (unlikely(!pgtable))
850 goto out;
851
852 spin_lock(&dst_mm->page_table_lock);
853 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
854
855 ret = -EAGAIN;
856 pmd = *src_pmd;
857 if (unlikely(!pmd_trans_huge(pmd))) {
858 pte_free(dst_mm, pgtable);
859 goto out_unlock;
860 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800861 /*
862 * mm->page_table_lock is enough to be sure that huge zero pmd is not
863 * under splitting since we don't split the page itself, only pmd to
864 * a page table.
865 */
866 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800867 unsigned long zero_pfn;
868 /*
869 * get_huge_zero_page() will never allocate a new page here,
870 * since we already have a zero page to copy. It just takes a
871 * reference.
872 */
873 zero_pfn = get_huge_zero_page();
874 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
875 zero_pfn);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800876 ret = 0;
877 goto out_unlock;
878 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800879 if (unlikely(pmd_trans_splitting(pmd))) {
880 /* split huge page running from under us */
881 spin_unlock(&src_mm->page_table_lock);
882 spin_unlock(&dst_mm->page_table_lock);
883 pte_free(dst_mm, pgtable);
884
885 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
886 goto out;
887 }
888 src_page = pmd_page(pmd);
889 VM_BUG_ON(!PageHead(src_page));
890 get_page(src_page);
891 page_dup_rmap(src_page);
892 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
893
894 pmdp_set_wrprotect(src_mm, addr, src_pmd);
895 pmd = pmd_mkold(pmd_wrprotect(pmd));
896 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700897 pgtable_trans_huge_deposit(dst_mm, pgtable);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800898 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800899
900 ret = 0;
901out_unlock:
902 spin_unlock(&src_mm->page_table_lock);
903 spin_unlock(&dst_mm->page_table_lock);
904out:
905 return ret;
906}
907
Will Deacona1dd4502012-12-11 16:01:27 -0800908void huge_pmd_set_accessed(struct mm_struct *mm,
909 struct vm_area_struct *vma,
910 unsigned long address,
911 pmd_t *pmd, pmd_t orig_pmd,
912 int dirty)
913{
914 pmd_t entry;
915 unsigned long haddr;
916
917 spin_lock(&mm->page_table_lock);
918 if (unlikely(!pmd_same(*pmd, orig_pmd)))
919 goto unlock;
920
921 entry = pmd_mkyoung(orig_pmd);
922 haddr = address & HPAGE_PMD_MASK;
923 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
924 update_mmu_cache_pmd(vma, address, pmd);
925
926unlock:
927 spin_unlock(&mm->page_table_lock);
928}
929
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800930static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
931 struct vm_area_struct *vma, unsigned long address,
932 pmd_t *pmd, unsigned long haddr)
933{
934 pgtable_t pgtable;
935 pmd_t _pmd;
936 struct page *page;
937 int i, ret = 0;
938 unsigned long mmun_start; /* For mmu_notifiers */
939 unsigned long mmun_end; /* For mmu_notifiers */
940
941 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
942 if (!page) {
943 ret |= VM_FAULT_OOM;
944 goto out;
945 }
946
947 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
948 put_page(page);
949 ret |= VM_FAULT_OOM;
950 goto out;
951 }
952
953 clear_user_highpage(page, address);
954 __SetPageUptodate(page);
955
956 mmun_start = haddr;
957 mmun_end = haddr + HPAGE_PMD_SIZE;
958 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
959
960 spin_lock(&mm->page_table_lock);
961 pmdp_clear_flush(vma, haddr, pmd);
962 /* leave pmd empty until pte is filled */
963
964 pgtable = pgtable_trans_huge_withdraw(mm);
965 pmd_populate(mm, &_pmd, pgtable);
966
967 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
968 pte_t *pte, entry;
969 if (haddr == (address & PAGE_MASK)) {
970 entry = mk_pte(page, vma->vm_page_prot);
971 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
972 page_add_new_anon_rmap(page, vma, haddr);
973 } else {
974 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
975 entry = pte_mkspecial(entry);
976 }
977 pte = pte_offset_map(&_pmd, haddr);
978 VM_BUG_ON(!pte_none(*pte));
979 set_pte_at(mm, haddr, pte, entry);
980 pte_unmap(pte);
981 }
982 smp_wmb(); /* make pte visible before pmd */
983 pmd_populate(mm, pmd, pgtable);
984 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800985 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800986 inc_mm_counter(mm, MM_ANONPAGES);
987
988 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
989
990 ret |= VM_FAULT_WRITE;
991out:
992 return ret;
993}
994
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800995static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
996 struct vm_area_struct *vma,
997 unsigned long address,
998 pmd_t *pmd, pmd_t orig_pmd,
999 struct page *page,
1000 unsigned long haddr)
1001{
1002 pgtable_t pgtable;
1003 pmd_t _pmd;
1004 int ret = 0, i;
1005 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001006 unsigned long mmun_start; /* For mmu_notifiers */
1007 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001008
1009 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1010 GFP_KERNEL);
1011 if (unlikely(!pages)) {
1012 ret |= VM_FAULT_OOM;
1013 goto out;
1014 }
1015
1016 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001017 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1018 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001019 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001020 if (unlikely(!pages[i] ||
1021 mem_cgroup_newpage_charge(pages[i], mm,
1022 GFP_KERNEL))) {
1023 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001024 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001025 mem_cgroup_uncharge_start();
1026 while (--i >= 0) {
1027 mem_cgroup_uncharge_page(pages[i]);
1028 put_page(pages[i]);
1029 }
1030 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001031 kfree(pages);
1032 ret |= VM_FAULT_OOM;
1033 goto out;
1034 }
1035 }
1036
1037 for (i = 0; i < HPAGE_PMD_NR; i++) {
1038 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001039 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001040 __SetPageUptodate(pages[i]);
1041 cond_resched();
1042 }
1043
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001044 mmun_start = haddr;
1045 mmun_end = haddr + HPAGE_PMD_SIZE;
1046 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1047
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001048 spin_lock(&mm->page_table_lock);
1049 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1050 goto out_free_pages;
1051 VM_BUG_ON(!PageHead(page));
1052
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001053 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001054 /* leave pmd empty until pte is filled */
1055
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001056 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001057 pmd_populate(mm, &_pmd, pgtable);
1058
1059 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1060 pte_t *pte, entry;
1061 entry = mk_pte(pages[i], vma->vm_page_prot);
1062 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1063 page_add_new_anon_rmap(pages[i], vma, haddr);
1064 pte = pte_offset_map(&_pmd, haddr);
1065 VM_BUG_ON(!pte_none(*pte));
1066 set_pte_at(mm, haddr, pte, entry);
1067 pte_unmap(pte);
1068 }
1069 kfree(pages);
1070
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001071 smp_wmb(); /* make pte visible before pmd */
1072 pmd_populate(mm, pmd, pgtable);
1073 page_remove_rmap(page);
1074 spin_unlock(&mm->page_table_lock);
1075
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001076 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1077
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001078 ret |= VM_FAULT_WRITE;
1079 put_page(page);
1080
1081out:
1082 return ret;
1083
1084out_free_pages:
1085 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001086 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001087 mem_cgroup_uncharge_start();
1088 for (i = 0; i < HPAGE_PMD_NR; i++) {
1089 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001090 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001091 }
1092 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001093 kfree(pages);
1094 goto out;
1095}
1096
1097int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1098 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1099{
1100 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001101 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001103 unsigned long mmun_start; /* For mmu_notifiers */
1104 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001105
1106 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001107 haddr = address & HPAGE_PMD_MASK;
1108 if (is_huge_zero_pmd(orig_pmd))
1109 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001110 spin_lock(&mm->page_table_lock);
1111 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1112 goto out_unlock;
1113
1114 page = pmd_page(orig_pmd);
1115 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001116 if (page_mapcount(page) == 1) {
1117 pmd_t entry;
1118 entry = pmd_mkyoung(orig_pmd);
1119 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1120 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001121 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001122 ret |= VM_FAULT_WRITE;
1123 goto out_unlock;
1124 }
1125 get_page(page);
1126 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001127alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128 if (transparent_hugepage_enabled(vma) &&
1129 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001130 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001131 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001132 else
1133 new_page = NULL;
1134
1135 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -07001136 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001137 if (is_huge_zero_pmd(orig_pmd)) {
1138 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
1139 address, pmd, haddr);
1140 } else {
1141 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1142 pmd, orig_pmd, page, haddr);
1143 if (ret & VM_FAULT_OOM)
1144 split_huge_page(page);
1145 put_page(page);
1146 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147 goto out;
1148 }
Andi Kleen81ab4202011-04-14 15:22:06 -07001149 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001150
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001151 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1152 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001153 if (page) {
1154 split_huge_page(page);
1155 put_page(page);
1156 }
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001157 ret |= VM_FAULT_OOM;
1158 goto out;
1159 }
1160
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001161 if (is_huge_zero_pmd(orig_pmd))
1162 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1163 else
1164 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001165 __SetPageUptodate(new_page);
1166
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001167 mmun_start = haddr;
1168 mmun_end = haddr + HPAGE_PMD_SIZE;
1169 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1170
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001171 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001172 if (page)
1173 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001174 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001175 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001176 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001177 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001178 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001179 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001180 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -08001181 entry = mk_huge_pmd(new_page, vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001182 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001183 page_add_new_anon_rmap(new_page, vma, haddr);
1184 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001185 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001186 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001187 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001188 put_huge_zero_page();
1189 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001190 VM_BUG_ON(!PageHead(page));
1191 page_remove_rmap(page);
1192 put_page(page);
1193 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001194 ret |= VM_FAULT_WRITE;
1195 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001196 spin_unlock(&mm->page_table_lock);
1197out_mn:
1198 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1199out:
1200 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001201out_unlock:
1202 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001203 return ret;
1204}
1205
David Rientjesb676b292012-10-08 16:34:03 -07001206struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 unsigned long addr,
1208 pmd_t *pmd,
1209 unsigned int flags)
1210{
David Rientjesb676b292012-10-08 16:34:03 -07001211 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001212 struct page *page = NULL;
1213
1214 assert_spin_locked(&mm->page_table_lock);
1215
1216 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1217 goto out;
1218
1219 page = pmd_page(*pmd);
1220 VM_BUG_ON(!PageHead(page));
1221 if (flags & FOLL_TOUCH) {
1222 pmd_t _pmd;
1223 /*
1224 * We should set the dirty bit only for FOLL_WRITE but
1225 * for now the dirty bit in the pmd is meaningless.
1226 * And if the dirty bit will become meaningful and
1227 * we'll only set it with FOLL_WRITE, an atomic
1228 * set_bit will be required on the pmd to set the
1229 * young bit, instead of the current set_pmd_at.
1230 */
1231 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1232 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1233 }
David Rientjesb676b292012-10-08 16:34:03 -07001234 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1235 if (page->mapping && trylock_page(page)) {
1236 lru_add_drain();
1237 if (page->mapping)
1238 mlock_vma_page(page);
1239 unlock_page(page);
1240 }
1241 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1243 VM_BUG_ON(!PageCompound(page));
1244 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001245 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001246
1247out:
1248 return page;
1249}
1250
1251int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001252 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001253{
1254 int ret = 0;
1255
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001256 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1257 struct page *page;
1258 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001259 pmd_t orig_pmd;
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001260 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
David Millerf5c8ad42012-10-08 16:34:26 -07001261 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001262 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001263 if (is_huge_zero_pmd(orig_pmd)) {
1264 tlb->mm->nr_ptes--;
1265 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001266 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001267 } else {
1268 page = pmd_page(orig_pmd);
1269 page_remove_rmap(page);
1270 VM_BUG_ON(page_mapcount(page) < 0);
1271 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1272 VM_BUG_ON(!PageHead(page));
1273 tlb->mm->nr_ptes--;
1274 spin_unlock(&tlb->mm->page_table_lock);
1275 tlb_remove_page(tlb, page);
1276 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001277 pte_free(tlb->mm, pgtable);
1278 ret = 1;
1279 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280 return ret;
1281}
1282
Johannes Weiner0ca16342011-01-13 15:47:02 -08001283int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1284 unsigned long addr, unsigned long end,
1285 unsigned char *vec)
1286{
1287 int ret = 0;
1288
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001289 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1290 /*
1291 * All logical pages in the range are present
1292 * if backed by a huge page.
1293 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001294 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001295 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1296 ret = 1;
1297 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001298
1299 return ret;
1300}
1301
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001302int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1303 unsigned long old_addr,
1304 unsigned long new_addr, unsigned long old_end,
1305 pmd_t *old_pmd, pmd_t *new_pmd)
1306{
1307 int ret = 0;
1308 pmd_t pmd;
1309
1310 struct mm_struct *mm = vma->vm_mm;
1311
1312 if ((old_addr & ~HPAGE_PMD_MASK) ||
1313 (new_addr & ~HPAGE_PMD_MASK) ||
1314 old_end - old_addr < HPAGE_PMD_SIZE ||
1315 (new_vma->vm_flags & VM_NOHUGEPAGE))
1316 goto out;
1317
1318 /*
1319 * The destination pmd shouldn't be established, free_pgtables()
1320 * should have release it.
1321 */
1322 if (WARN_ON(!pmd_none(*new_pmd))) {
1323 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1324 goto out;
1325 }
1326
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001327 ret = __pmd_trans_huge_lock(old_pmd, vma);
1328 if (ret == 1) {
1329 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1330 VM_BUG_ON(!pmd_none(*new_pmd));
1331 set_pmd_at(mm, new_addr, new_pmd, pmd);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001332 spin_unlock(&mm->page_table_lock);
1333 }
1334out:
1335 return ret;
1336}
1337
Johannes Weinercd7548a2011-01-13 15:47:04 -08001338int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1339 unsigned long addr, pgprot_t newprot)
1340{
1341 struct mm_struct *mm = vma->vm_mm;
1342 int ret = 0;
1343
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001344 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1345 pmd_t entry;
1346 entry = pmdp_get_and_clear(mm, addr, pmd);
1347 entry = pmd_modify(entry, newprot);
Kirill A. Shutemovcad7f612012-12-12 13:50:57 -08001348 BUG_ON(pmd_write(entry));
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001349 set_pmd_at(mm, addr, pmd, entry);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001350 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001351 ret = 1;
1352 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001353
1354 return ret;
1355}
1356
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001357/*
1358 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1359 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1360 *
1361 * Note that if it returns 1, this routine returns without unlocking page
1362 * table locks. So callers must unlock them.
1363 */
1364int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1365{
1366 spin_lock(&vma->vm_mm->page_table_lock);
1367 if (likely(pmd_trans_huge(*pmd))) {
1368 if (unlikely(pmd_trans_splitting(*pmd))) {
1369 spin_unlock(&vma->vm_mm->page_table_lock);
1370 wait_split_huge_page(vma->anon_vma, pmd);
1371 return -1;
1372 } else {
1373 /* Thp mapped by 'pmd' is stable, so we can
1374 * handle it as it is. */
1375 return 1;
1376 }
1377 }
1378 spin_unlock(&vma->vm_mm->page_table_lock);
1379 return 0;
1380}
1381
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001382pmd_t *page_check_address_pmd(struct page *page,
1383 struct mm_struct *mm,
1384 unsigned long address,
1385 enum page_check_address_pmd_flag flag)
1386{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001387 pmd_t *pmd, *ret = NULL;
1388
1389 if (address & ~HPAGE_PMD_MASK)
1390 goto out;
1391
Bob Liu62190492012-12-11 16:00:37 -08001392 pmd = mm_find_pmd(mm, address);
1393 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001394 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001395 if (pmd_none(*pmd))
1396 goto out;
1397 if (pmd_page(*pmd) != page)
1398 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001399 /*
1400 * split_vma() may create temporary aliased mappings. There is
1401 * no risk as long as all huge pmd are found and have their
1402 * splitting bit set before __split_huge_page_refcount
1403 * runs. Finding the same huge pmd more than once during the
1404 * same rmap walk is not a problem.
1405 */
1406 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1407 pmd_trans_splitting(*pmd))
1408 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001409 if (pmd_trans_huge(*pmd)) {
1410 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1411 !pmd_trans_splitting(*pmd));
1412 ret = pmd;
1413 }
1414out:
1415 return ret;
1416}
1417
1418static int __split_huge_page_splitting(struct page *page,
1419 struct vm_area_struct *vma,
1420 unsigned long address)
1421{
1422 struct mm_struct *mm = vma->vm_mm;
1423 pmd_t *pmd;
1424 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001425 /* For mmu_notifiers */
1426 const unsigned long mmun_start = address;
1427 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001428
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001429 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001430 spin_lock(&mm->page_table_lock);
1431 pmd = page_check_address_pmd(page, mm, address,
1432 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1433 if (pmd) {
1434 /*
1435 * We can't temporarily set the pmd to null in order
1436 * to split it, the pmd must remain marked huge at all
1437 * times or the VM won't take the pmd_trans_huge paths
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07001438 * and it won't wait on the anon_vma->root->mutex to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001439 * serialize against split_huge_page*.
1440 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001441 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001442 ret = 1;
1443 }
1444 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001445 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001446
1447 return ret;
1448}
1449
1450static void __split_huge_page_refcount(struct page *page)
1451{
1452 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001453 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001454 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001455 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001456
1457 /* prevent PageLRU to go away from under us, and freeze lru stats */
1458 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001459 lruvec = mem_cgroup_page_lruvec(page, zone);
1460
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001461 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001462 /* complete memcg works before add pages to LRU */
1463 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001464
Shaohua Li45676882012-01-12 17:19:18 -08001465 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001466 struct page *page_tail = page + i;
1467
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001468 /* tail_page->_mapcount cannot change */
1469 BUG_ON(page_mapcount(page_tail) < 0);
1470 tail_count += page_mapcount(page_tail);
1471 /* check for overflow */
1472 BUG_ON(tail_count < 0);
1473 BUG_ON(atomic_read(&page_tail->_count) != 0);
1474 /*
1475 * tail_page->_count is zero and not changing from
1476 * under us. But get_page_unless_zero() may be running
1477 * from under us on the tail_page. If we used
1478 * atomic_set() below instead of atomic_add(), we
1479 * would then run atomic_set() concurrently with
1480 * get_page_unless_zero(), and atomic_set() is
1481 * implemented in C not using locked ops. spin_unlock
1482 * on x86 sometime uses locked ops because of PPro
1483 * errata 66, 92, so unless somebody can guarantee
1484 * atomic_set() here would be safe on all archs (and
1485 * not only on x86), it's safer to use atomic_add().
1486 */
1487 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1488 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001489
1490 /* after clearing PageTail the gup refcount can be released */
1491 smp_mb();
1492
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001493 /*
1494 * retain hwpoison flag of the poisoned tail page:
1495 * fix for the unsuitable process killed on Guest Machine(KVM)
1496 * by the memory-failure.
1497 */
1498 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001499 page_tail->flags |= (page->flags &
1500 ((1L << PG_referenced) |
1501 (1L << PG_swapbacked) |
1502 (1L << PG_mlocked) |
1503 (1L << PG_uptodate)));
1504 page_tail->flags |= (1L << PG_dirty);
1505
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001506 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001507 smp_wmb();
1508
1509 /*
1510 * __split_huge_page_splitting() already set the
1511 * splitting bit in all pmd that could map this
1512 * hugepage, that will ensure no CPU can alter the
1513 * mapcount on the head page. The mapcount is only
1514 * accounted in the head page and it has to be
1515 * transferred to all tail pages in the below code. So
1516 * for this code to be safe, the split the mapcount
1517 * can't change. But that doesn't mean userland can't
1518 * keep changing and reading the page contents while
1519 * we transfer the mapcount, so the pmd splitting
1520 * status is achieved setting a reserved bit in the
1521 * pmd, not by clearing the present bit.
1522 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001523 page_tail->_mapcount = page->_mapcount;
1524
1525 BUG_ON(page_tail->mapping);
1526 page_tail->mapping = page->mapping;
1527
Shaohua Li45676882012-01-12 17:19:18 -08001528 page_tail->index = page->index + i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001529
1530 BUG_ON(!PageAnon(page_tail));
1531 BUG_ON(!PageUptodate(page_tail));
1532 BUG_ON(!PageDirty(page_tail));
1533 BUG_ON(!PageSwapBacked(page_tail));
1534
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001535 lru_add_page_tail(page, page_tail, lruvec);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001536 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001537 atomic_sub(tail_count, &page->_count);
1538 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001539
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001540 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001541 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1542
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001543 ClearPageCompound(page);
1544 compound_unlock(page);
1545 spin_unlock_irq(&zone->lru_lock);
1546
1547 for (i = 1; i < HPAGE_PMD_NR; i++) {
1548 struct page *page_tail = page + i;
1549 BUG_ON(page_count(page_tail) <= 0);
1550 /*
1551 * Tail pages may be freed if there wasn't any mapping
1552 * like if add_to_swap() is running on a lru page that
1553 * had its mapping zapped. And freeing these pages
1554 * requires taking the lru_lock so we do the put_page
1555 * of the tail pages after the split is complete.
1556 */
1557 put_page(page_tail);
1558 }
1559
1560 /*
1561 * Only the head page (now become a regular page) is required
1562 * to be pinned by the caller.
1563 */
1564 BUG_ON(page_count(page) <= 0);
1565}
1566
1567static int __split_huge_page_map(struct page *page,
1568 struct vm_area_struct *vma,
1569 unsigned long address)
1570{
1571 struct mm_struct *mm = vma->vm_mm;
1572 pmd_t *pmd, _pmd;
1573 int ret = 0, i;
1574 pgtable_t pgtable;
1575 unsigned long haddr;
1576
1577 spin_lock(&mm->page_table_lock);
1578 pmd = page_check_address_pmd(page, mm, address,
1579 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1580 if (pmd) {
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001581 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001582 pmd_populate(mm, &_pmd, pgtable);
1583
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001584 haddr = address;
1585 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001586 pte_t *pte, entry;
1587 BUG_ON(PageCompound(page+i));
1588 entry = mk_pte(page + i, vma->vm_page_prot);
1589 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1590 if (!pmd_write(*pmd))
1591 entry = pte_wrprotect(entry);
1592 else
1593 BUG_ON(page_mapcount(page) != 1);
1594 if (!pmd_young(*pmd))
1595 entry = pte_mkold(entry);
1596 pte = pte_offset_map(&_pmd, haddr);
1597 BUG_ON(!pte_none(*pte));
1598 set_pte_at(mm, haddr, pte, entry);
1599 pte_unmap(pte);
1600 }
1601
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001602 smp_wmb(); /* make pte visible before pmd */
1603 /*
1604 * Up to this point the pmd is present and huge and
1605 * userland has the whole access to the hugepage
1606 * during the split (which happens in place). If we
1607 * overwrite the pmd with the not-huge version
1608 * pointing to the pte here (which of course we could
1609 * if all CPUs were bug free), userland could trigger
1610 * a small page size TLB miss on the small sized TLB
1611 * while the hugepage TLB entry is still established
1612 * in the huge TLB. Some CPU doesn't like that. See
1613 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1614 * Erratum 383 on page 93. Intel should be safe but is
1615 * also warns that it's only safe if the permission
1616 * and cache attributes of the two entries loaded in
1617 * the two TLB is identical (which should be the case
1618 * here). But it is generally safer to never allow
1619 * small and huge TLB entries for the same virtual
1620 * address to be loaded simultaneously. So instead of
1621 * doing "pmd_populate(); flush_tlb_range();" we first
1622 * mark the current pmd notpresent (atomically because
1623 * here the pmd_trans_huge and pmd_trans_splitting
1624 * must remain set at all times on the pmd until the
1625 * split is complete for this pmd), then we flush the
1626 * SMP TLB and finally we write the non-huge version
1627 * of the pmd entry with pmd_populate.
1628 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001629 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001630 pmd_populate(mm, pmd, pgtable);
1631 ret = 1;
1632 }
1633 spin_unlock(&mm->page_table_lock);
1634
1635 return ret;
1636}
1637
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07001638/* must be called with anon_vma->root->mutex hold */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001639static void __split_huge_page(struct page *page,
1640 struct anon_vma *anon_vma)
1641{
1642 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001643 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001644 struct anon_vma_chain *avc;
1645
1646 BUG_ON(!PageHead(page));
1647 BUG_ON(PageTail(page));
1648
1649 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001650 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001651 struct vm_area_struct *vma = avc->vma;
1652 unsigned long addr = vma_address(page, vma);
1653 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001654 mapcount += __split_huge_page_splitting(page, vma, addr);
1655 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001656 /*
1657 * It is critical that new vmas are added to the tail of the
1658 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1659 * and establishes a child pmd before
1660 * __split_huge_page_splitting() freezes the parent pmd (so if
1661 * we fail to prevent copy_huge_pmd() from running until the
1662 * whole __split_huge_page() is complete), we will still see
1663 * the newly established pmd of the child later during the
1664 * walk, to be able to set it as pmd_trans_splitting too.
1665 */
1666 if (mapcount != page_mapcount(page))
1667 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1668 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001669 BUG_ON(mapcount != page_mapcount(page));
1670
1671 __split_huge_page_refcount(page);
1672
1673 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001674 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001675 struct vm_area_struct *vma = avc->vma;
1676 unsigned long addr = vma_address(page, vma);
1677 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001678 mapcount2 += __split_huge_page_map(page, vma, addr);
1679 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001680 if (mapcount != mapcount2)
1681 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1682 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001683 BUG_ON(mapcount != mapcount2);
1684}
1685
1686int split_huge_page(struct page *page)
1687{
1688 struct anon_vma *anon_vma;
1689 int ret = 1;
1690
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08001691 BUG_ON(is_huge_zero_pfn(page_to_pfn(page)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001692 BUG_ON(!PageAnon(page));
1693 anon_vma = page_lock_anon_vma(page);
1694 if (!anon_vma)
1695 goto out;
1696 ret = 0;
1697 if (!PageCompound(page))
1698 goto out_unlock;
1699
1700 BUG_ON(!PageSwapBacked(page));
1701 __split_huge_page(page, anon_vma);
Andi Kleen81ab4202011-04-14 15:22:06 -07001702 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001703
1704 BUG_ON(PageCompound(page));
1705out_unlock:
1706 page_unlock_anon_vma(anon_vma);
1707out:
1708 return ret;
1709}
1710
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001711#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001712
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001713int hugepage_madvise(struct vm_area_struct *vma,
1714 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001715{
Gerald Schaefer8e720332012-10-08 16:30:12 -07001716 struct mm_struct *mm = vma->vm_mm;
1717
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001718 switch (advice) {
1719 case MADV_HUGEPAGE:
1720 /*
1721 * Be somewhat over-protective like KSM for now!
1722 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001723 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001724 return -EINVAL;
Gerald Schaefer8e720332012-10-08 16:30:12 -07001725 if (mm->def_flags & VM_NOHUGEPAGE)
1726 return -EINVAL;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001727 *vm_flags &= ~VM_NOHUGEPAGE;
1728 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001729 /*
1730 * If the vma become good for khugepaged to scan,
1731 * register it here without waiting a page fault that
1732 * may not happen any time soon.
1733 */
1734 if (unlikely(khugepaged_enter_vma_merge(vma)))
1735 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001736 break;
1737 case MADV_NOHUGEPAGE:
1738 /*
1739 * Be somewhat over-protective like KSM for now!
1740 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001741 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001742 return -EINVAL;
1743 *vm_flags &= ~VM_HUGEPAGE;
1744 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001745 /*
1746 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1747 * this vma even if we leave the mm registered in khugepaged if
1748 * it got registered before VM_NOHUGEPAGE was set.
1749 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001750 break;
1751 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001752
1753 return 0;
1754}
1755
Andrea Arcangeliba761492011-01-13 15:46:58 -08001756static int __init khugepaged_slab_init(void)
1757{
1758 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1759 sizeof(struct mm_slot),
1760 __alignof__(struct mm_slot), 0, NULL);
1761 if (!mm_slot_cache)
1762 return -ENOMEM;
1763
1764 return 0;
1765}
1766
1767static void __init khugepaged_slab_free(void)
1768{
1769 kmem_cache_destroy(mm_slot_cache);
1770 mm_slot_cache = NULL;
1771}
1772
1773static inline struct mm_slot *alloc_mm_slot(void)
1774{
1775 if (!mm_slot_cache) /* initialization failed */
1776 return NULL;
1777 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1778}
1779
1780static inline void free_mm_slot(struct mm_slot *mm_slot)
1781{
1782 kmem_cache_free(mm_slot_cache, mm_slot);
1783}
1784
1785static int __init mm_slots_hash_init(void)
1786{
1787 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1788 GFP_KERNEL);
1789 if (!mm_slots_hash)
1790 return -ENOMEM;
1791 return 0;
1792}
1793
1794#if 0
1795static void __init mm_slots_hash_free(void)
1796{
1797 kfree(mm_slots_hash);
1798 mm_slots_hash = NULL;
1799}
1800#endif
1801
1802static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1803{
1804 struct mm_slot *mm_slot;
1805 struct hlist_head *bucket;
1806 struct hlist_node *node;
1807
1808 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1809 % MM_SLOTS_HASH_HEADS];
1810 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1811 if (mm == mm_slot->mm)
1812 return mm_slot;
1813 }
1814 return NULL;
1815}
1816
1817static void insert_to_mm_slots_hash(struct mm_struct *mm,
1818 struct mm_slot *mm_slot)
1819{
1820 struct hlist_head *bucket;
1821
1822 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1823 % MM_SLOTS_HASH_HEADS];
1824 mm_slot->mm = mm;
1825 hlist_add_head(&mm_slot->hash, bucket);
1826}
1827
1828static inline int khugepaged_test_exit(struct mm_struct *mm)
1829{
1830 return atomic_read(&mm->mm_users) == 0;
1831}
1832
1833int __khugepaged_enter(struct mm_struct *mm)
1834{
1835 struct mm_slot *mm_slot;
1836 int wakeup;
1837
1838 mm_slot = alloc_mm_slot();
1839 if (!mm_slot)
1840 return -ENOMEM;
1841
1842 /* __khugepaged_exit() must not run from under us */
1843 VM_BUG_ON(khugepaged_test_exit(mm));
1844 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1845 free_mm_slot(mm_slot);
1846 return 0;
1847 }
1848
1849 spin_lock(&khugepaged_mm_lock);
1850 insert_to_mm_slots_hash(mm, mm_slot);
1851 /*
1852 * Insert just behind the scanning cursor, to let the area settle
1853 * down a little.
1854 */
1855 wakeup = list_empty(&khugepaged_scan.mm_head);
1856 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1857 spin_unlock(&khugepaged_mm_lock);
1858
1859 atomic_inc(&mm->mm_count);
1860 if (wakeup)
1861 wake_up_interruptible(&khugepaged_wait);
1862
1863 return 0;
1864}
1865
1866int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1867{
1868 unsigned long hstart, hend;
1869 if (!vma->anon_vma)
1870 /*
1871 * Not yet faulted in so we will register later in the
1872 * page fault if needed.
1873 */
1874 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001875 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001876 /* khugepaged not yet working on file or special mappings */
1877 return 0;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001878 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001879 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1880 hend = vma->vm_end & HPAGE_PMD_MASK;
1881 if (hstart < hend)
1882 return khugepaged_enter(vma);
1883 return 0;
1884}
1885
1886void __khugepaged_exit(struct mm_struct *mm)
1887{
1888 struct mm_slot *mm_slot;
1889 int free = 0;
1890
1891 spin_lock(&khugepaged_mm_lock);
1892 mm_slot = get_mm_slot(mm);
1893 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1894 hlist_del(&mm_slot->hash);
1895 list_del(&mm_slot->mm_node);
1896 free = 1;
1897 }
Chris Wrightd788e802011-07-25 17:12:14 -07001898 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001899
1900 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001901 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1902 free_mm_slot(mm_slot);
1903 mmdrop(mm);
1904 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905 /*
1906 * This is required to serialize against
1907 * khugepaged_test_exit() (which is guaranteed to run
1908 * under mmap sem read mode). Stop here (after we
1909 * return all pagetables will be destroyed) until
1910 * khugepaged has finished working on the pagetables
1911 * under the mmap_sem.
1912 */
1913 down_write(&mm->mmap_sem);
1914 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001915 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001916}
1917
1918static void release_pte_page(struct page *page)
1919{
1920 /* 0 stands for page_is_file_cache(page) == false */
1921 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1922 unlock_page(page);
1923 putback_lru_page(page);
1924}
1925
1926static void release_pte_pages(pte_t *pte, pte_t *_pte)
1927{
1928 while (--_pte >= pte) {
1929 pte_t pteval = *_pte;
1930 if (!pte_none(pteval))
1931 release_pte_page(pte_page(pteval));
1932 }
1933}
1934
Andrea Arcangeliba761492011-01-13 15:46:58 -08001935static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1936 unsigned long address,
1937 pte_t *pte)
1938{
1939 struct page *page;
1940 pte_t *_pte;
Bob Liu344aa352012-12-11 16:00:34 -08001941 int referenced = 0, none = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001942 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1943 _pte++, address += PAGE_SIZE) {
1944 pte_t pteval = *_pte;
1945 if (pte_none(pteval)) {
1946 if (++none <= khugepaged_max_ptes_none)
1947 continue;
Bob Liu344aa352012-12-11 16:00:34 -08001948 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08001949 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001950 }
Bob Liu344aa352012-12-11 16:00:34 -08001951 if (!pte_present(pteval) || !pte_write(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001952 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001953 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08001954 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001955 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08001956
Andrea Arcangeliba761492011-01-13 15:46:58 -08001957 VM_BUG_ON(PageCompound(page));
1958 BUG_ON(!PageAnon(page));
1959 VM_BUG_ON(!PageSwapBacked(page));
1960
1961 /* cannot use mapcount: can't collapse if there's a gup pin */
Bob Liu344aa352012-12-11 16:00:34 -08001962 if (page_count(page) != 1)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001963 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001964 /*
1965 * We can do it before isolate_lru_page because the
1966 * page can't be freed from under us. NOTE: PG_lock
1967 * is needed to serialize against split_huge_page
1968 * when invoked from the VM.
1969 */
Bob Liu344aa352012-12-11 16:00:34 -08001970 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001971 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001972 /*
1973 * Isolate the page to avoid collapsing an hugepage
1974 * currently in use by the VM.
1975 */
1976 if (isolate_lru_page(page)) {
1977 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001978 goto out;
1979 }
1980 /* 0 stands for page_is_file_cache(page) == false */
1981 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1982 VM_BUG_ON(!PageLocked(page));
1983 VM_BUG_ON(PageLRU(page));
1984
1985 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001986 if (pte_young(pteval) || PageReferenced(page) ||
1987 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001988 referenced = 1;
1989 }
Bob Liu344aa352012-12-11 16:00:34 -08001990 if (likely(referenced))
1991 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001992out:
Bob Liu344aa352012-12-11 16:00:34 -08001993 release_pte_pages(pte, _pte);
1994 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001995}
1996
1997static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1998 struct vm_area_struct *vma,
1999 unsigned long address,
2000 spinlock_t *ptl)
2001{
2002 pte_t *_pte;
2003 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2004 pte_t pteval = *_pte;
2005 struct page *src_page;
2006
2007 if (pte_none(pteval)) {
2008 clear_user_highpage(page, address);
2009 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2010 } else {
2011 src_page = pte_page(pteval);
2012 copy_user_highpage(page, src_page, address, vma);
2013 VM_BUG_ON(page_mapcount(src_page) != 1);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002014 release_pte_page(src_page);
2015 /*
2016 * ptl mostly unnecessary, but preempt has to
2017 * be disabled to update the per-cpu stats
2018 * inside page_remove_rmap().
2019 */
2020 spin_lock(ptl);
2021 /*
2022 * paravirt calls inside pte_clear here are
2023 * superfluous.
2024 */
2025 pte_clear(vma->vm_mm, address, _pte);
2026 page_remove_rmap(src_page);
2027 spin_unlock(ptl);
2028 free_page_and_swap_cache(src_page);
2029 }
2030
2031 address += PAGE_SIZE;
2032 page++;
2033 }
2034}
2035
Xiao Guangrong26234f32012-10-08 16:29:51 -07002036static void khugepaged_alloc_sleep(void)
2037{
2038 wait_event_freezable_timeout(khugepaged_wait, false,
2039 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2040}
2041
2042#ifdef CONFIG_NUMA
2043static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2044{
2045 if (IS_ERR(*hpage)) {
2046 if (!*wait)
2047 return false;
2048
2049 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002050 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002051 khugepaged_alloc_sleep();
2052 } else if (*hpage) {
2053 put_page(*hpage);
2054 *hpage = NULL;
2055 }
2056
2057 return true;
2058}
2059
2060static struct page
2061*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2062 struct vm_area_struct *vma, unsigned long address,
2063 int node)
2064{
2065 VM_BUG_ON(*hpage);
2066 /*
2067 * Allocate the page while the vma is still valid and under
2068 * the mmap_sem read mode so there is no memory allocation
2069 * later when we take the mmap_sem in write mode. This is more
2070 * friendly behavior (OTOH it may actually hide bugs) to
2071 * filesystems in userland with daemons allocating memory in
2072 * the userland I/O paths. Allocating memory with the
2073 * mmap_sem in read mode is good idea also to allow greater
2074 * scalability.
2075 */
2076 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2077 node, __GFP_OTHER_NODE);
2078
2079 /*
2080 * After allocating the hugepage, release the mmap_sem read lock in
2081 * preparation for taking it in write mode.
2082 */
2083 up_read(&mm->mmap_sem);
2084 if (unlikely(!*hpage)) {
2085 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2086 *hpage = ERR_PTR(-ENOMEM);
2087 return NULL;
2088 }
2089
2090 count_vm_event(THP_COLLAPSE_ALLOC);
2091 return *hpage;
2092}
2093#else
2094static struct page *khugepaged_alloc_hugepage(bool *wait)
2095{
2096 struct page *hpage;
2097
2098 do {
2099 hpage = alloc_hugepage(khugepaged_defrag());
2100 if (!hpage) {
2101 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2102 if (!*wait)
2103 return NULL;
2104
2105 *wait = false;
2106 khugepaged_alloc_sleep();
2107 } else
2108 count_vm_event(THP_COLLAPSE_ALLOC);
2109 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2110
2111 return hpage;
2112}
2113
2114static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2115{
2116 if (!*hpage)
2117 *hpage = khugepaged_alloc_hugepage(wait);
2118
2119 if (unlikely(!*hpage))
2120 return false;
2121
2122 return true;
2123}
2124
2125static struct page
2126*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2127 struct vm_area_struct *vma, unsigned long address,
2128 int node)
2129{
2130 up_read(&mm->mmap_sem);
2131 VM_BUG_ON(!*hpage);
2132 return *hpage;
2133}
2134#endif
2135
Bob Liufa475e52012-12-11 16:00:39 -08002136static bool hugepage_vma_check(struct vm_area_struct *vma)
2137{
2138 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2139 (vma->vm_flags & VM_NOHUGEPAGE))
2140 return false;
2141
2142 if (!vma->anon_vma || vma->vm_ops)
2143 return false;
2144 if (is_vma_temporary_stack(vma))
2145 return false;
2146 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2147 return true;
2148}
2149
Andrea Arcangeliba761492011-01-13 15:46:58 -08002150static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002151 unsigned long address,
2152 struct page **hpage,
2153 struct vm_area_struct *vma,
2154 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002155{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002156 pmd_t *pmd, _pmd;
2157 pte_t *pte;
2158 pgtable_t pgtable;
2159 struct page *new_page;
2160 spinlock_t *ptl;
2161 int isolated;
2162 unsigned long hstart, hend;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002163 unsigned long mmun_start; /* For mmu_notifiers */
2164 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002165
2166 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002167
Xiao Guangrong26234f32012-10-08 16:29:51 -07002168 /* release the mmap_sem read lock. */
2169 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2170 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002171 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002172
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002173 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002174 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002175
2176 /*
2177 * Prevent all access to pagetables with the exception of
2178 * gup_fast later hanlded by the ptep_clear_flush and the VM
2179 * handled by the anon_vma lock + PG_lock.
2180 */
2181 down_write(&mm->mmap_sem);
2182 if (unlikely(khugepaged_test_exit(mm)))
2183 goto out;
2184
2185 vma = find_vma(mm, address);
2186 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2187 hend = vma->vm_end & HPAGE_PMD_MASK;
2188 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2189 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002190 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002191 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002192 pmd = mm_find_pmd(mm, address);
2193 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002194 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002195 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002196 goto out;
2197
Andrea Arcangeliba761492011-01-13 15:46:58 -08002198 anon_vma_lock(vma->anon_vma);
2199
2200 pte = pte_offset_map(pmd, address);
2201 ptl = pte_lockptr(mm, pmd);
2202
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002203 mmun_start = address;
2204 mmun_end = address + HPAGE_PMD_SIZE;
2205 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002206 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2207 /*
2208 * After this gup_fast can't run anymore. This also removes
2209 * any huge TLB entry from the CPU so we won't allow
2210 * huge and small TLB entries for the same virtual address
2211 * to avoid the risk of CPU bugs in that area.
2212 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002213 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002214 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002215 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002216
2217 spin_lock(ptl);
2218 isolated = __collapse_huge_page_isolate(vma, address, pte);
2219 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002220
2221 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002222 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002223 spin_lock(&mm->page_table_lock);
2224 BUG_ON(!pmd_none(*pmd));
2225 set_pmd_at(mm, address, pmd, _pmd);
2226 spin_unlock(&mm->page_table_lock);
2227 anon_vma_unlock(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002228 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002229 }
2230
2231 /*
2232 * All pages are isolated and locked so anon_vma rmap
2233 * can't run anymore.
2234 */
2235 anon_vma_unlock(vma->anon_vma);
2236
2237 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002238 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002239 __SetPageUptodate(new_page);
2240 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002241
Bob Liub3092b32012-12-11 16:00:41 -08002242 _pmd = mk_huge_pmd(new_page, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002243
2244 /*
2245 * spin_lock() below is not the equivalent of smp_wmb(), so
2246 * this is needed to avoid the copy_huge_page writes to become
2247 * visible after the set_pmd_at() write.
2248 */
2249 smp_wmb();
2250
2251 spin_lock(&mm->page_table_lock);
2252 BUG_ON(!pmd_none(*pmd));
2253 page_add_new_anon_rmap(new_page, vma, address);
2254 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002255 update_mmu_cache_pmd(vma, address, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07002256 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002257 spin_unlock(&mm->page_table_lock);
2258
2259 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002260
Andrea Arcangeliba761492011-01-13 15:46:58 -08002261 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002262out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002263 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002264 return;
2265
Andrea Arcangelice83d212011-01-13 15:47:06 -08002266out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002267 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002268 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002269}
2270
2271static int khugepaged_scan_pmd(struct mm_struct *mm,
2272 struct vm_area_struct *vma,
2273 unsigned long address,
2274 struct page **hpage)
2275{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002276 pmd_t *pmd;
2277 pte_t *pte, *_pte;
2278 int ret = 0, referenced = 0, none = 0;
2279 struct page *page;
2280 unsigned long _address;
2281 spinlock_t *ptl;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002282 int node = -1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002283
2284 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2285
Bob Liu62190492012-12-11 16:00:37 -08002286 pmd = mm_find_pmd(mm, address);
2287 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002288 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002289 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002290 goto out;
2291
2292 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2293 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2294 _pte++, _address += PAGE_SIZE) {
2295 pte_t pteval = *_pte;
2296 if (pte_none(pteval)) {
2297 if (++none <= khugepaged_max_ptes_none)
2298 continue;
2299 else
2300 goto out_unmap;
2301 }
2302 if (!pte_present(pteval) || !pte_write(pteval))
2303 goto out_unmap;
2304 page = vm_normal_page(vma, _address, pteval);
2305 if (unlikely(!page))
2306 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002307 /*
2308 * Chose the node of the first page. This could
2309 * be more sophisticated and look at more pages,
2310 * but isn't for now.
2311 */
2312 if (node == -1)
2313 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002314 VM_BUG_ON(PageCompound(page));
2315 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2316 goto out_unmap;
2317 /* cannot use mapcount: can't collapse if there's a gup pin */
2318 if (page_count(page) != 1)
2319 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002320 if (pte_young(pteval) || PageReferenced(page) ||
2321 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002322 referenced = 1;
2323 }
2324 if (referenced)
2325 ret = 1;
2326out_unmap:
2327 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002328 if (ret)
2329 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002330 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331out:
2332 return ret;
2333}
2334
2335static void collect_mm_slot(struct mm_slot *mm_slot)
2336{
2337 struct mm_struct *mm = mm_slot->mm;
2338
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002339 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002340
2341 if (khugepaged_test_exit(mm)) {
2342 /* free mm_slot */
2343 hlist_del(&mm_slot->hash);
2344 list_del(&mm_slot->mm_node);
2345
2346 /*
2347 * Not strictly needed because the mm exited already.
2348 *
2349 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2350 */
2351
2352 /* khugepaged_mm_lock actually not necessary for the below */
2353 free_mm_slot(mm_slot);
2354 mmdrop(mm);
2355 }
2356}
2357
2358static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2359 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002360 __releases(&khugepaged_mm_lock)
2361 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002362{
2363 struct mm_slot *mm_slot;
2364 struct mm_struct *mm;
2365 struct vm_area_struct *vma;
2366 int progress = 0;
2367
2368 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002369 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002370
2371 if (khugepaged_scan.mm_slot)
2372 mm_slot = khugepaged_scan.mm_slot;
2373 else {
2374 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2375 struct mm_slot, mm_node);
2376 khugepaged_scan.address = 0;
2377 khugepaged_scan.mm_slot = mm_slot;
2378 }
2379 spin_unlock(&khugepaged_mm_lock);
2380
2381 mm = mm_slot->mm;
2382 down_read(&mm->mmap_sem);
2383 if (unlikely(khugepaged_test_exit(mm)))
2384 vma = NULL;
2385 else
2386 vma = find_vma(mm, khugepaged_scan.address);
2387
2388 progress++;
2389 for (; vma; vma = vma->vm_next) {
2390 unsigned long hstart, hend;
2391
2392 cond_resched();
2393 if (unlikely(khugepaged_test_exit(mm))) {
2394 progress++;
2395 break;
2396 }
Bob Liufa475e52012-12-11 16:00:39 -08002397 if (!hugepage_vma_check(vma)) {
2398skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002399 progress++;
2400 continue;
2401 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002402 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2403 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002404 if (hstart >= hend)
2405 goto skip;
2406 if (khugepaged_scan.address > hend)
2407 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002408 if (khugepaged_scan.address < hstart)
2409 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002410 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002411
2412 while (khugepaged_scan.address < hend) {
2413 int ret;
2414 cond_resched();
2415 if (unlikely(khugepaged_test_exit(mm)))
2416 goto breakouterloop;
2417
2418 VM_BUG_ON(khugepaged_scan.address < hstart ||
2419 khugepaged_scan.address + HPAGE_PMD_SIZE >
2420 hend);
2421 ret = khugepaged_scan_pmd(mm, vma,
2422 khugepaged_scan.address,
2423 hpage);
2424 /* move to next address */
2425 khugepaged_scan.address += HPAGE_PMD_SIZE;
2426 progress += HPAGE_PMD_NR;
2427 if (ret)
2428 /* we released mmap_sem so break loop */
2429 goto breakouterloop_mmap_sem;
2430 if (progress >= pages)
2431 goto breakouterloop;
2432 }
2433 }
2434breakouterloop:
2435 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2436breakouterloop_mmap_sem:
2437
2438 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002439 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002440 /*
2441 * Release the current mm_slot if this mm is about to die, or
2442 * if we scanned all vmas of this mm.
2443 */
2444 if (khugepaged_test_exit(mm) || !vma) {
2445 /*
2446 * Make sure that if mm_users is reaching zero while
2447 * khugepaged runs here, khugepaged_exit will find
2448 * mm_slot not pointing to the exiting mm.
2449 */
2450 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2451 khugepaged_scan.mm_slot = list_entry(
2452 mm_slot->mm_node.next,
2453 struct mm_slot, mm_node);
2454 khugepaged_scan.address = 0;
2455 } else {
2456 khugepaged_scan.mm_slot = NULL;
2457 khugepaged_full_scans++;
2458 }
2459
2460 collect_mm_slot(mm_slot);
2461 }
2462
2463 return progress;
2464}
2465
2466static int khugepaged_has_work(void)
2467{
2468 return !list_empty(&khugepaged_scan.mm_head) &&
2469 khugepaged_enabled();
2470}
2471
2472static int khugepaged_wait_event(void)
2473{
2474 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002475 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002476}
2477
Xiao Guangrongd5169042012-10-08 16:29:48 -07002478static void khugepaged_do_scan(void)
2479{
2480 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002481 unsigned int progress = 0, pass_through_head = 0;
2482 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002483 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484
2485 barrier(); /* write khugepaged_pages_to_scan to local stack */
2486
2487 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002488 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002489 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002490
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002491 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002492
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002493 if (unlikely(kthread_should_stop() || freezing(current)))
2494 break;
2495
Andrea Arcangeliba761492011-01-13 15:46:58 -08002496 spin_lock(&khugepaged_mm_lock);
2497 if (!khugepaged_scan.mm_slot)
2498 pass_through_head++;
2499 if (khugepaged_has_work() &&
2500 pass_through_head < 2)
2501 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002502 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002503 else
2504 progress = pages;
2505 spin_unlock(&khugepaged_mm_lock);
2506 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002507
Xiao Guangrongd5169042012-10-08 16:29:48 -07002508 if (!IS_ERR_OR_NULL(hpage))
2509 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002510}
2511
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002512static void khugepaged_wait_work(void)
2513{
2514 try_to_freeze();
2515
2516 if (khugepaged_has_work()) {
2517 if (!khugepaged_scan_sleep_millisecs)
2518 return;
2519
2520 wait_event_freezable_timeout(khugepaged_wait,
2521 kthread_should_stop(),
2522 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2523 return;
2524 }
2525
2526 if (khugepaged_enabled())
2527 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2528}
2529
Andrea Arcangeliba761492011-01-13 15:46:58 -08002530static int khugepaged(void *none)
2531{
2532 struct mm_slot *mm_slot;
2533
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002534 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535 set_user_nice(current, 19);
2536
Xiao Guangrongb7231782012-10-08 16:29:54 -07002537 while (!kthread_should_stop()) {
2538 khugepaged_do_scan();
2539 khugepaged_wait_work();
2540 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002541
2542 spin_lock(&khugepaged_mm_lock);
2543 mm_slot = khugepaged_scan.mm_slot;
2544 khugepaged_scan.mm_slot = NULL;
2545 if (mm_slot)
2546 collect_mm_slot(mm_slot);
2547 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002548 return 0;
2549}
2550
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002551static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2552 unsigned long haddr, pmd_t *pmd)
2553{
2554 struct mm_struct *mm = vma->vm_mm;
2555 pgtable_t pgtable;
2556 pmd_t _pmd;
2557 int i;
2558
2559 pmdp_clear_flush(vma, haddr, pmd);
2560 /* leave pmd empty until pte is filled */
2561
2562 pgtable = pgtable_trans_huge_withdraw(mm);
2563 pmd_populate(mm, &_pmd, pgtable);
2564
2565 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2566 pte_t *pte, entry;
2567 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2568 entry = pte_mkspecial(entry);
2569 pte = pte_offset_map(&_pmd, haddr);
2570 VM_BUG_ON(!pte_none(*pte));
2571 set_pte_at(mm, haddr, pte, entry);
2572 pte_unmap(pte);
2573 }
2574 smp_wmb(); /* make pte visible before pmd */
2575 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002576 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002577}
2578
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002579void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2580 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002581{
2582 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002583 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002584 unsigned long haddr = address & HPAGE_PMD_MASK;
2585 unsigned long mmun_start; /* For mmu_notifiers */
2586 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002587
2588 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002589
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002590 mmun_start = haddr;
2591 mmun_end = haddr + HPAGE_PMD_SIZE;
2592 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002593 spin_lock(&mm->page_table_lock);
2594 if (unlikely(!pmd_trans_huge(*pmd))) {
2595 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002596 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2597 return;
2598 }
2599 if (is_huge_zero_pmd(*pmd)) {
2600 __split_huge_zero_page_pmd(vma, haddr, pmd);
2601 spin_unlock(&mm->page_table_lock);
2602 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002603 return;
2604 }
2605 page = pmd_page(*pmd);
2606 VM_BUG_ON(!page_count(page));
2607 get_page(page);
2608 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002609 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002610
2611 split_huge_page(page);
2612
2613 put_page(page);
2614 BUG_ON(pmd_trans_huge(*pmd));
2615}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002616
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002617void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2618 pmd_t *pmd)
2619{
2620 struct vm_area_struct *vma;
2621
2622 vma = find_vma(mm, address);
2623 BUG_ON(vma == NULL);
2624 split_huge_page_pmd(vma, address, pmd);
2625}
2626
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002627static void split_huge_page_address(struct mm_struct *mm,
2628 unsigned long address)
2629{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002630 pmd_t *pmd;
2631
2632 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2633
Bob Liu62190492012-12-11 16:00:37 -08002634 pmd = mm_find_pmd(mm, address);
2635 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002636 return;
2637 /*
2638 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2639 * materialize from under us.
2640 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002641 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002642}
2643
2644void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2645 unsigned long start,
2646 unsigned long end,
2647 long adjust_next)
2648{
2649 /*
2650 * If the new start address isn't hpage aligned and it could
2651 * previously contain an hugepage: check if we need to split
2652 * an huge pmd.
2653 */
2654 if (start & ~HPAGE_PMD_MASK &&
2655 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2656 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2657 split_huge_page_address(vma->vm_mm, start);
2658
2659 /*
2660 * If the new end address isn't hpage aligned and it could
2661 * previously contain an hugepage: check if we need to split
2662 * an huge pmd.
2663 */
2664 if (end & ~HPAGE_PMD_MASK &&
2665 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2666 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2667 split_huge_page_address(vma->vm_mm, end);
2668
2669 /*
2670 * If we're also updating the vma->vm_next->vm_start, if the new
2671 * vm_next->vm_start isn't page aligned and it could previously
2672 * contain an hugepage: check if we need to split an huge pmd.
2673 */
2674 if (adjust_next > 0) {
2675 struct vm_area_struct *next = vma->vm_next;
2676 unsigned long nstart = next->vm_start;
2677 nstart += adjust_next << PAGE_SHIFT;
2678 if (nstart & ~HPAGE_PMD_MASK &&
2679 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2680 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2681 split_huge_page_address(next->vm_mm, nstart);
2682 }
2683}