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