blob: 76cc3f7dd4f057cd44540edf3dfd5d479e8e31ca [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;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -070092};
93static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -080094 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
95};
96
Andrea Arcangelif0005652011-01-13 15:47:04 -080097
98static int set_recommended_min_free_kbytes(void)
99{
100 struct zone *zone;
101 int nr_zones = 0;
102 unsigned long recommended_min;
103 extern int min_free_kbytes;
104
105 if (!test_bit(TRANSPARENT_HUGEPAGE_FLAG,
106 &transparent_hugepage_flags) &&
107 !test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
108 &transparent_hugepage_flags))
109 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()) {
142 int wakeup;
143 if (unlikely(!mm_slot_cache || !mm_slots_hash)) {
144 err = -ENOMEM;
145 goto out;
146 }
147 mutex_lock(&khugepaged_mutex);
148 if (!khugepaged_thread)
149 khugepaged_thread = kthread_run(khugepaged, NULL,
150 "khugepaged");
151 if (unlikely(IS_ERR(khugepaged_thread))) {
152 printk(KERN_ERR
153 "khugepaged: kthread_run(khugepaged) failed\n");
154 err = PTR_ERR(khugepaged_thread);
155 khugepaged_thread = NULL;
156 }
157 wakeup = !list_empty(&khugepaged_scan.mm_head);
158 mutex_unlock(&khugepaged_mutex);
159 if (wakeup)
160 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800161
162 set_recommended_min_free_kbytes();
Andrea Arcangeliba761492011-01-13 15:46:58 -0800163 } else
164 /* wakeup to exit */
165 wake_up_interruptible(&khugepaged_wait);
166out:
167 return err;
168}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800169
170#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800171
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800172static ssize_t double_flag_show(struct kobject *kobj,
173 struct kobj_attribute *attr, char *buf,
174 enum transparent_hugepage_flag enabled,
175 enum transparent_hugepage_flag req_madv)
176{
177 if (test_bit(enabled, &transparent_hugepage_flags)) {
178 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
179 return sprintf(buf, "[always] madvise never\n");
180 } else if (test_bit(req_madv, &transparent_hugepage_flags))
181 return sprintf(buf, "always [madvise] never\n");
182 else
183 return sprintf(buf, "always madvise [never]\n");
184}
185static ssize_t double_flag_store(struct kobject *kobj,
186 struct kobj_attribute *attr,
187 const char *buf, size_t count,
188 enum transparent_hugepage_flag enabled,
189 enum transparent_hugepage_flag req_madv)
190{
191 if (!memcmp("always", buf,
192 min(sizeof("always")-1, count))) {
193 set_bit(enabled, &transparent_hugepage_flags);
194 clear_bit(req_madv, &transparent_hugepage_flags);
195 } else if (!memcmp("madvise", buf,
196 min(sizeof("madvise")-1, count))) {
197 clear_bit(enabled, &transparent_hugepage_flags);
198 set_bit(req_madv, &transparent_hugepage_flags);
199 } else if (!memcmp("never", buf,
200 min(sizeof("never")-1, count))) {
201 clear_bit(enabled, &transparent_hugepage_flags);
202 clear_bit(req_madv, &transparent_hugepage_flags);
203 } else
204 return -EINVAL;
205
206 return count;
207}
208
209static ssize_t enabled_show(struct kobject *kobj,
210 struct kobj_attribute *attr, char *buf)
211{
212 return double_flag_show(kobj, attr, buf,
213 TRANSPARENT_HUGEPAGE_FLAG,
214 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
215}
216static ssize_t enabled_store(struct kobject *kobj,
217 struct kobj_attribute *attr,
218 const char *buf, size_t count)
219{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800220 ssize_t ret;
221
222 ret = double_flag_store(kobj, attr, buf, count,
223 TRANSPARENT_HUGEPAGE_FLAG,
224 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
225
226 if (ret > 0) {
227 int err = start_khugepaged();
228 if (err)
229 ret = err;
230 }
231
Andrea Arcangelif0005652011-01-13 15:47:04 -0800232 if (ret > 0 &&
233 (test_bit(TRANSPARENT_HUGEPAGE_FLAG,
234 &transparent_hugepage_flags) ||
235 test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
236 &transparent_hugepage_flags)))
237 set_recommended_min_free_kbytes();
238
Andrea Arcangeliba761492011-01-13 15:46:58 -0800239 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800240}
241static struct kobj_attribute enabled_attr =
242 __ATTR(enabled, 0644, enabled_show, enabled_store);
243
244static ssize_t single_flag_show(struct kobject *kobj,
245 struct kobj_attribute *attr, char *buf,
246 enum transparent_hugepage_flag flag)
247{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700248 return sprintf(buf, "%d\n",
249 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800250}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700251
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800252static 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{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700257 unsigned long value;
258 int ret;
259
260 ret = kstrtoul(buf, 10, &value);
261 if (ret < 0)
262 return ret;
263 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800264 return -EINVAL;
265
Ben Hutchingse27e6152011-04-14 15:22:21 -0700266 if (value)
267 set_bit(flag, &transparent_hugepage_flags);
268 else
269 clear_bit(flag, &transparent_hugepage_flags);
270
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800271 return count;
272}
273
274/*
275 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
276 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
277 * memory just to allocate one more hugepage.
278 */
279static ssize_t defrag_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf)
281{
282 return double_flag_show(kobj, attr, buf,
283 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
284 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
285}
286static ssize_t defrag_store(struct kobject *kobj,
287 struct kobj_attribute *attr,
288 const char *buf, size_t count)
289{
290 return double_flag_store(kobj, attr, buf, count,
291 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
292 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
293}
294static struct kobj_attribute defrag_attr =
295 __ATTR(defrag, 0644, defrag_show, defrag_store);
296
297#ifdef CONFIG_DEBUG_VM
298static ssize_t debug_cow_show(struct kobject *kobj,
299 struct kobj_attribute *attr, char *buf)
300{
301 return single_flag_show(kobj, attr, buf,
302 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
303}
304static ssize_t debug_cow_store(struct kobject *kobj,
305 struct kobj_attribute *attr,
306 const char *buf, size_t count)
307{
308 return single_flag_store(kobj, attr, buf, count,
309 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
310}
311static struct kobj_attribute debug_cow_attr =
312 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
313#endif /* CONFIG_DEBUG_VM */
314
315static struct attribute *hugepage_attr[] = {
316 &enabled_attr.attr,
317 &defrag_attr.attr,
318#ifdef CONFIG_DEBUG_VM
319 &debug_cow_attr.attr,
320#endif
321 NULL,
322};
323
324static struct attribute_group hugepage_attr_group = {
325 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800326};
327
328static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
329 struct kobj_attribute *attr,
330 char *buf)
331{
332 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
333}
334
335static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
336 struct kobj_attribute *attr,
337 const char *buf, size_t count)
338{
339 unsigned long msecs;
340 int err;
341
342 err = strict_strtoul(buf, 10, &msecs);
343 if (err || msecs > UINT_MAX)
344 return -EINVAL;
345
346 khugepaged_scan_sleep_millisecs = msecs;
347 wake_up_interruptible(&khugepaged_wait);
348
349 return count;
350}
351static struct kobj_attribute scan_sleep_millisecs_attr =
352 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
353 scan_sleep_millisecs_store);
354
355static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
356 struct kobj_attribute *attr,
357 char *buf)
358{
359 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
360}
361
362static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
363 struct kobj_attribute *attr,
364 const char *buf, size_t count)
365{
366 unsigned long msecs;
367 int err;
368
369 err = strict_strtoul(buf, 10, &msecs);
370 if (err || msecs > UINT_MAX)
371 return -EINVAL;
372
373 khugepaged_alloc_sleep_millisecs = msecs;
374 wake_up_interruptible(&khugepaged_wait);
375
376 return count;
377}
378static struct kobj_attribute alloc_sleep_millisecs_attr =
379 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
380 alloc_sleep_millisecs_store);
381
382static ssize_t pages_to_scan_show(struct kobject *kobj,
383 struct kobj_attribute *attr,
384 char *buf)
385{
386 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
387}
388static ssize_t pages_to_scan_store(struct kobject *kobj,
389 struct kobj_attribute *attr,
390 const char *buf, size_t count)
391{
392 int err;
393 unsigned long pages;
394
395 err = strict_strtoul(buf, 10, &pages);
396 if (err || !pages || pages > UINT_MAX)
397 return -EINVAL;
398
399 khugepaged_pages_to_scan = pages;
400
401 return count;
402}
403static struct kobj_attribute pages_to_scan_attr =
404 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
405 pages_to_scan_store);
406
407static ssize_t pages_collapsed_show(struct kobject *kobj,
408 struct kobj_attribute *attr,
409 char *buf)
410{
411 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
412}
413static struct kobj_attribute pages_collapsed_attr =
414 __ATTR_RO(pages_collapsed);
415
416static ssize_t full_scans_show(struct kobject *kobj,
417 struct kobj_attribute *attr,
418 char *buf)
419{
420 return sprintf(buf, "%u\n", khugepaged_full_scans);
421}
422static struct kobj_attribute full_scans_attr =
423 __ATTR_RO(full_scans);
424
425static ssize_t khugepaged_defrag_show(struct kobject *kobj,
426 struct kobj_attribute *attr, char *buf)
427{
428 return single_flag_show(kobj, attr, buf,
429 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
430}
431static ssize_t khugepaged_defrag_store(struct kobject *kobj,
432 struct kobj_attribute *attr,
433 const char *buf, size_t count)
434{
435 return single_flag_store(kobj, attr, buf, count,
436 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
437}
438static struct kobj_attribute khugepaged_defrag_attr =
439 __ATTR(defrag, 0644, khugepaged_defrag_show,
440 khugepaged_defrag_store);
441
442/*
443 * max_ptes_none controls if khugepaged should collapse hugepages over
444 * any unmapped ptes in turn potentially increasing the memory
445 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
446 * reduce the available free memory in the system as it
447 * runs. Increasing max_ptes_none will instead potentially reduce the
448 * free memory in the system during the khugepaged scan.
449 */
450static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
451 struct kobj_attribute *attr,
452 char *buf)
453{
454 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
455}
456static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
457 struct kobj_attribute *attr,
458 const char *buf, size_t count)
459{
460 int err;
461 unsigned long max_ptes_none;
462
463 err = strict_strtoul(buf, 10, &max_ptes_none);
464 if (err || max_ptes_none > HPAGE_PMD_NR-1)
465 return -EINVAL;
466
467 khugepaged_max_ptes_none = max_ptes_none;
468
469 return count;
470}
471static struct kobj_attribute khugepaged_max_ptes_none_attr =
472 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
473 khugepaged_max_ptes_none_store);
474
475static struct attribute *khugepaged_attr[] = {
476 &khugepaged_defrag_attr.attr,
477 &khugepaged_max_ptes_none_attr.attr,
478 &pages_to_scan_attr.attr,
479 &pages_collapsed_attr.attr,
480 &full_scans_attr.attr,
481 &scan_sleep_millisecs_attr.attr,
482 &alloc_sleep_millisecs_attr.attr,
483 NULL,
484};
485
486static struct attribute_group khugepaged_attr_group = {
487 .attrs = khugepaged_attr,
488 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800489};
Shaohua Li569e5592012-01-12 17:19:11 -0800490
491static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
492{
493 int err;
494
495 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
496 if (unlikely(!*hugepage_kobj)) {
497 printk(KERN_ERR "hugepage: failed kobject create\n");
498 return -ENOMEM;
499 }
500
501 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
502 if (err) {
503 printk(KERN_ERR "hugepage: failed register hugeage group\n");
504 goto delete_obj;
505 }
506
507 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
508 if (err) {
509 printk(KERN_ERR "hugepage: failed register hugeage group\n");
510 goto remove_hp_group;
511 }
512
513 return 0;
514
515remove_hp_group:
516 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
517delete_obj:
518 kobject_put(*hugepage_kobj);
519 return err;
520}
521
522static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
523{
524 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
525 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
526 kobject_put(hugepage_kobj);
527}
528#else
529static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
530{
531 return 0;
532}
533
534static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
535{
536}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800537#endif /* CONFIG_SYSFS */
538
539static int __init hugepage_init(void)
540{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800541 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800542 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800543
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800544 if (!has_transparent_hugepage()) {
545 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800546 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800547 }
548
Shaohua Li569e5592012-01-12 17:19:11 -0800549 err = hugepage_init_sysfs(&hugepage_kobj);
550 if (err)
551 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800552
553 err = khugepaged_slab_init();
554 if (err)
555 goto out;
556
557 err = mm_slots_hash_init();
558 if (err) {
559 khugepaged_slab_free();
560 goto out;
561 }
562
Rik van Riel97562cd2011-01-13 15:47:12 -0800563 /*
564 * By default disable transparent hugepages on smaller systems,
565 * where the extra memory used could hurt more than TLB overhead
566 * is likely to save. The admin can still enable it through /sys.
567 */
568 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
569 transparent_hugepage_flags = 0;
570
Andrea Arcangeliba761492011-01-13 15:46:58 -0800571 start_khugepaged();
572
Andrea Arcangelif0005652011-01-13 15:47:04 -0800573 set_recommended_min_free_kbytes();
574
Shaohua Li569e5592012-01-12 17:19:11 -0800575 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800576out:
Shaohua Li569e5592012-01-12 17:19:11 -0800577 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800578 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800579}
580module_init(hugepage_init)
581
582static int __init setup_transparent_hugepage(char *str)
583{
584 int ret = 0;
585 if (!str)
586 goto out;
587 if (!strcmp(str, "always")) {
588 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
589 &transparent_hugepage_flags);
590 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
591 &transparent_hugepage_flags);
592 ret = 1;
593 } else if (!strcmp(str, "madvise")) {
594 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
595 &transparent_hugepage_flags);
596 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
597 &transparent_hugepage_flags);
598 ret = 1;
599 } else if (!strcmp(str, "never")) {
600 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
601 &transparent_hugepage_flags);
602 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
603 &transparent_hugepage_flags);
604 ret = 1;
605 }
606out:
607 if (!ret)
608 printk(KERN_WARNING
609 "transparent_hugepage= cannot parse, ignored\n");
610 return ret;
611}
612__setup("transparent_hugepage=", setup_transparent_hugepage);
613
614static void prepare_pmd_huge_pte(pgtable_t pgtable,
615 struct mm_struct *mm)
616{
617 assert_spin_locked(&mm->page_table_lock);
618
619 /* FIFO */
620 if (!mm->pmd_huge_pte)
621 INIT_LIST_HEAD(&pgtable->lru);
622 else
623 list_add(&pgtable->lru, &mm->pmd_huge_pte->lru);
624 mm->pmd_huge_pte = pgtable;
625}
626
627static inline pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
628{
629 if (likely(vma->vm_flags & VM_WRITE))
630 pmd = pmd_mkwrite(pmd);
631 return pmd;
632}
633
634static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
635 struct vm_area_struct *vma,
636 unsigned long haddr, pmd_t *pmd,
637 struct page *page)
638{
639 int ret = 0;
640 pgtable_t pgtable;
641
642 VM_BUG_ON(!PageCompound(page));
643 pgtable = pte_alloc_one(mm, haddr);
644 if (unlikely(!pgtable)) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800645 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800646 put_page(page);
647 return VM_FAULT_OOM;
648 }
649
650 clear_huge_page(page, haddr, HPAGE_PMD_NR);
651 __SetPageUptodate(page);
652
653 spin_lock(&mm->page_table_lock);
654 if (unlikely(!pmd_none(*pmd))) {
655 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800656 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800657 put_page(page);
658 pte_free(mm, pgtable);
659 } else {
660 pmd_t entry;
661 entry = mk_pmd(page, vma->vm_page_prot);
662 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
663 entry = pmd_mkhuge(entry);
664 /*
665 * The spinlocking to take the lru_lock inside
666 * page_add_new_anon_rmap() acts as a full memory
667 * barrier to be sure clear_huge_page writes become
668 * visible after the set_pmd_at() write.
669 */
670 page_add_new_anon_rmap(page, vma, haddr);
671 set_pmd_at(mm, haddr, pmd, entry);
672 prepare_pmd_huge_pte(pgtable, mm);
673 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
674 spin_unlock(&mm->page_table_lock);
675 }
676
677 return ret;
678}
679
Andi Kleencc5d4622011-03-22 16:33:13 -0700680static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800681{
Andi Kleencc5d4622011-03-22 16:33:13 -0700682 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800683}
684
685static inline struct page *alloc_hugepage_vma(int defrag,
686 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700687 unsigned long haddr, int nd,
688 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800689{
Andi Kleencc5d4622011-03-22 16:33:13 -0700690 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800691 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800692}
693
694#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800695static inline struct page *alloc_hugepage(int defrag)
696{
Andi Kleencc5d4622011-03-22 16:33:13 -0700697 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800698 HPAGE_PMD_ORDER);
699}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800700#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800701
702int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
703 unsigned long address, pmd_t *pmd,
704 unsigned int flags)
705{
706 struct page *page;
707 unsigned long haddr = address & HPAGE_PMD_MASK;
708 pte_t *pte;
709
710 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
711 if (unlikely(anon_vma_prepare(vma)))
712 return VM_FAULT_OOM;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800713 if (unlikely(khugepaged_enter(vma)))
714 return VM_FAULT_OOM;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800715 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700716 vma, haddr, numa_node_id(), 0);
Andi Kleen81ab4202011-04-14 15:22:06 -0700717 if (unlikely(!page)) {
718 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800719 goto out;
Andi Kleen81ab4202011-04-14 15:22:06 -0700720 }
721 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800722 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
723 put_page(page);
724 goto out;
725 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800726
727 return __do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page);
728 }
729out:
730 /*
731 * Use __pte_alloc instead of pte_alloc_map, because we can't
732 * run pte_offset_map on the pmd, if an huge pmd could
733 * materialize from under us from a different thread.
734 */
735 if (unlikely(__pte_alloc(mm, vma, pmd, address)))
736 return VM_FAULT_OOM;
737 /* if an huge pmd materialized from under us just retry later */
738 if (unlikely(pmd_trans_huge(*pmd)))
739 return 0;
740 /*
741 * A regular pmd is established and it can't morph into a huge pmd
742 * from under us anymore at this point because we hold the mmap_sem
743 * read mode and khugepaged takes it in write mode. So now it's
744 * safe to run pte_offset_map().
745 */
746 pte = pte_offset_map(pmd, address);
747 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
748}
749
750int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
751 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
752 struct vm_area_struct *vma)
753{
754 struct page *src_page;
755 pmd_t pmd;
756 pgtable_t pgtable;
757 int ret;
758
759 ret = -ENOMEM;
760 pgtable = pte_alloc_one(dst_mm, addr);
761 if (unlikely(!pgtable))
762 goto out;
763
764 spin_lock(&dst_mm->page_table_lock);
765 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
766
767 ret = -EAGAIN;
768 pmd = *src_pmd;
769 if (unlikely(!pmd_trans_huge(pmd))) {
770 pte_free(dst_mm, pgtable);
771 goto out_unlock;
772 }
773 if (unlikely(pmd_trans_splitting(pmd))) {
774 /* split huge page running from under us */
775 spin_unlock(&src_mm->page_table_lock);
776 spin_unlock(&dst_mm->page_table_lock);
777 pte_free(dst_mm, pgtable);
778
779 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
780 goto out;
781 }
782 src_page = pmd_page(pmd);
783 VM_BUG_ON(!PageHead(src_page));
784 get_page(src_page);
785 page_dup_rmap(src_page);
786 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
787
788 pmdp_set_wrprotect(src_mm, addr, src_pmd);
789 pmd = pmd_mkold(pmd_wrprotect(pmd));
790 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
791 prepare_pmd_huge_pte(pgtable, dst_mm);
792
793 ret = 0;
794out_unlock:
795 spin_unlock(&src_mm->page_table_lock);
796 spin_unlock(&dst_mm->page_table_lock);
797out:
798 return ret;
799}
800
801/* no "address" argument so destroys page coloring of some arch */
802pgtable_t get_pmd_huge_pte(struct mm_struct *mm)
803{
804 pgtable_t pgtable;
805
806 assert_spin_locked(&mm->page_table_lock);
807
808 /* FIFO */
809 pgtable = mm->pmd_huge_pte;
810 if (list_empty(&pgtable->lru))
811 mm->pmd_huge_pte = NULL;
812 else {
813 mm->pmd_huge_pte = list_entry(pgtable->lru.next,
814 struct page, lru);
815 list_del(&pgtable->lru);
816 }
817 return pgtable;
818}
819
820static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
821 struct vm_area_struct *vma,
822 unsigned long address,
823 pmd_t *pmd, pmd_t orig_pmd,
824 struct page *page,
825 unsigned long haddr)
826{
827 pgtable_t pgtable;
828 pmd_t _pmd;
829 int ret = 0, i;
830 struct page **pages;
831
832 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
833 GFP_KERNEL);
834 if (unlikely(!pages)) {
835 ret |= VM_FAULT_OOM;
836 goto out;
837 }
838
839 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -0700840 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
841 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -0800842 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800843 if (unlikely(!pages[i] ||
844 mem_cgroup_newpage_charge(pages[i], mm,
845 GFP_KERNEL))) {
846 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800847 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800848 mem_cgroup_uncharge_start();
849 while (--i >= 0) {
850 mem_cgroup_uncharge_page(pages[i]);
851 put_page(pages[i]);
852 }
853 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800854 kfree(pages);
855 ret |= VM_FAULT_OOM;
856 goto out;
857 }
858 }
859
860 for (i = 0; i < HPAGE_PMD_NR; i++) {
861 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -0700862 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800863 __SetPageUptodate(pages[i]);
864 cond_resched();
865 }
866
867 spin_lock(&mm->page_table_lock);
868 if (unlikely(!pmd_same(*pmd, orig_pmd)))
869 goto out_free_pages;
870 VM_BUG_ON(!PageHead(page));
871
872 pmdp_clear_flush_notify(vma, haddr, pmd);
873 /* leave pmd empty until pte is filled */
874
875 pgtable = get_pmd_huge_pte(mm);
876 pmd_populate(mm, &_pmd, pgtable);
877
878 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
879 pte_t *pte, entry;
880 entry = mk_pte(pages[i], vma->vm_page_prot);
881 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
882 page_add_new_anon_rmap(pages[i], vma, haddr);
883 pte = pte_offset_map(&_pmd, haddr);
884 VM_BUG_ON(!pte_none(*pte));
885 set_pte_at(mm, haddr, pte, entry);
886 pte_unmap(pte);
887 }
888 kfree(pages);
889
890 mm->nr_ptes++;
891 smp_wmb(); /* make pte visible before pmd */
892 pmd_populate(mm, pmd, pgtable);
893 page_remove_rmap(page);
894 spin_unlock(&mm->page_table_lock);
895
896 ret |= VM_FAULT_WRITE;
897 put_page(page);
898
899out:
900 return ret;
901
902out_free_pages:
903 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800904 mem_cgroup_uncharge_start();
905 for (i = 0; i < HPAGE_PMD_NR; i++) {
906 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800907 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800908 }
909 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800910 kfree(pages);
911 goto out;
912}
913
914int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
915 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
916{
917 int ret = 0;
918 struct page *page, *new_page;
919 unsigned long haddr;
920
921 VM_BUG_ON(!vma->anon_vma);
922 spin_lock(&mm->page_table_lock);
923 if (unlikely(!pmd_same(*pmd, orig_pmd)))
924 goto out_unlock;
925
926 page = pmd_page(orig_pmd);
927 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
928 haddr = address & HPAGE_PMD_MASK;
929 if (page_mapcount(page) == 1) {
930 pmd_t entry;
931 entry = pmd_mkyoung(orig_pmd);
932 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
933 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
934 update_mmu_cache(vma, address, entry);
935 ret |= VM_FAULT_WRITE;
936 goto out_unlock;
937 }
938 get_page(page);
939 spin_unlock(&mm->page_table_lock);
940
941 if (transparent_hugepage_enabled(vma) &&
942 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800943 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700944 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800945 else
946 new_page = NULL;
947
948 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -0700949 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800950 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
951 pmd, orig_pmd, page, haddr);
952 put_page(page);
953 goto out;
954 }
Andi Kleen81ab4202011-04-14 15:22:06 -0700955 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800956
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800957 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
958 put_page(new_page);
959 put_page(page);
960 ret |= VM_FAULT_OOM;
961 goto out;
962 }
963
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800964 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
965 __SetPageUptodate(new_page);
966
967 spin_lock(&mm->page_table_lock);
968 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800969 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
970 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800971 put_page(new_page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800972 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800973 pmd_t entry;
974 VM_BUG_ON(!PageHead(page));
975 entry = mk_pmd(new_page, vma->vm_page_prot);
976 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
977 entry = pmd_mkhuge(entry);
978 pmdp_clear_flush_notify(vma, haddr, pmd);
979 page_add_new_anon_rmap(new_page, vma, haddr);
980 set_pmd_at(mm, haddr, pmd, entry);
981 update_mmu_cache(vma, address, entry);
982 page_remove_rmap(page);
983 put_page(page);
984 ret |= VM_FAULT_WRITE;
985 }
986out_unlock:
987 spin_unlock(&mm->page_table_lock);
988out:
989 return ret;
990}
991
992struct page *follow_trans_huge_pmd(struct mm_struct *mm,
993 unsigned long addr,
994 pmd_t *pmd,
995 unsigned int flags)
996{
997 struct page *page = NULL;
998
999 assert_spin_locked(&mm->page_table_lock);
1000
1001 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1002 goto out;
1003
1004 page = pmd_page(*pmd);
1005 VM_BUG_ON(!PageHead(page));
1006 if (flags & FOLL_TOUCH) {
1007 pmd_t _pmd;
1008 /*
1009 * We should set the dirty bit only for FOLL_WRITE but
1010 * for now the dirty bit in the pmd is meaningless.
1011 * And if the dirty bit will become meaningful and
1012 * we'll only set it with FOLL_WRITE, an atomic
1013 * set_bit will be required on the pmd to set the
1014 * young bit, instead of the current set_pmd_at.
1015 */
1016 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1017 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1018 }
1019 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1020 VM_BUG_ON(!PageCompound(page));
1021 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001022 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001023
1024out:
1025 return page;
1026}
1027
1028int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001029 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001030{
1031 int ret = 0;
1032
1033 spin_lock(&tlb->mm->page_table_lock);
1034 if (likely(pmd_trans_huge(*pmd))) {
1035 if (unlikely(pmd_trans_splitting(*pmd))) {
1036 spin_unlock(&tlb->mm->page_table_lock);
1037 wait_split_huge_page(vma->anon_vma,
1038 pmd);
1039 } else {
1040 struct page *page;
1041 pgtable_t pgtable;
1042 pgtable = get_pmd_huge_pte(tlb->mm);
1043 page = pmd_page(*pmd);
1044 pmd_clear(pmd);
Shaohua Lif21760b2012-01-12 17:19:16 -08001045 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001046 page_remove_rmap(page);
1047 VM_BUG_ON(page_mapcount(page) < 0);
1048 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1049 VM_BUG_ON(!PageHead(page));
1050 spin_unlock(&tlb->mm->page_table_lock);
1051 tlb_remove_page(tlb, page);
1052 pte_free(tlb->mm, pgtable);
1053 ret = 1;
1054 }
1055 } else
1056 spin_unlock(&tlb->mm->page_table_lock);
1057
1058 return ret;
1059}
1060
Johannes Weiner0ca16342011-01-13 15:47:02 -08001061int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1062 unsigned long addr, unsigned long end,
1063 unsigned char *vec)
1064{
1065 int ret = 0;
1066
1067 spin_lock(&vma->vm_mm->page_table_lock);
1068 if (likely(pmd_trans_huge(*pmd))) {
1069 ret = !pmd_trans_splitting(*pmd);
1070 spin_unlock(&vma->vm_mm->page_table_lock);
1071 if (unlikely(!ret))
1072 wait_split_huge_page(vma->anon_vma, pmd);
1073 else {
1074 /*
1075 * All logical pages in the range are present
1076 * if backed by a huge page.
1077 */
1078 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1079 }
1080 } else
1081 spin_unlock(&vma->vm_mm->page_table_lock);
1082
1083 return ret;
1084}
1085
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001086int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1087 unsigned long old_addr,
1088 unsigned long new_addr, unsigned long old_end,
1089 pmd_t *old_pmd, pmd_t *new_pmd)
1090{
1091 int ret = 0;
1092 pmd_t pmd;
1093
1094 struct mm_struct *mm = vma->vm_mm;
1095
1096 if ((old_addr & ~HPAGE_PMD_MASK) ||
1097 (new_addr & ~HPAGE_PMD_MASK) ||
1098 old_end - old_addr < HPAGE_PMD_SIZE ||
1099 (new_vma->vm_flags & VM_NOHUGEPAGE))
1100 goto out;
1101
1102 /*
1103 * The destination pmd shouldn't be established, free_pgtables()
1104 * should have release it.
1105 */
1106 if (WARN_ON(!pmd_none(*new_pmd))) {
1107 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1108 goto out;
1109 }
1110
1111 spin_lock(&mm->page_table_lock);
1112 if (likely(pmd_trans_huge(*old_pmd))) {
1113 if (pmd_trans_splitting(*old_pmd)) {
1114 spin_unlock(&mm->page_table_lock);
1115 wait_split_huge_page(vma->anon_vma, old_pmd);
1116 ret = -1;
1117 } else {
1118 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1119 VM_BUG_ON(!pmd_none(*new_pmd));
1120 set_pmd_at(mm, new_addr, new_pmd, pmd);
1121 spin_unlock(&mm->page_table_lock);
1122 ret = 1;
1123 }
1124 } else {
1125 spin_unlock(&mm->page_table_lock);
1126 }
1127out:
1128 return ret;
1129}
1130
Johannes Weinercd7548a2011-01-13 15:47:04 -08001131int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1132 unsigned long addr, pgprot_t newprot)
1133{
1134 struct mm_struct *mm = vma->vm_mm;
1135 int ret = 0;
1136
1137 spin_lock(&mm->page_table_lock);
1138 if (likely(pmd_trans_huge(*pmd))) {
1139 if (unlikely(pmd_trans_splitting(*pmd))) {
1140 spin_unlock(&mm->page_table_lock);
1141 wait_split_huge_page(vma->anon_vma, pmd);
1142 } else {
1143 pmd_t entry;
1144
1145 entry = pmdp_get_and_clear(mm, addr, pmd);
1146 entry = pmd_modify(entry, newprot);
1147 set_pmd_at(mm, addr, pmd, entry);
1148 spin_unlock(&vma->vm_mm->page_table_lock);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001149 ret = 1;
1150 }
1151 } else
1152 spin_unlock(&vma->vm_mm->page_table_lock);
1153
1154 return ret;
1155}
1156
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001157pmd_t *page_check_address_pmd(struct page *page,
1158 struct mm_struct *mm,
1159 unsigned long address,
1160 enum page_check_address_pmd_flag flag)
1161{
1162 pgd_t *pgd;
1163 pud_t *pud;
1164 pmd_t *pmd, *ret = NULL;
1165
1166 if (address & ~HPAGE_PMD_MASK)
1167 goto out;
1168
1169 pgd = pgd_offset(mm, address);
1170 if (!pgd_present(*pgd))
1171 goto out;
1172
1173 pud = pud_offset(pgd, address);
1174 if (!pud_present(*pud))
1175 goto out;
1176
1177 pmd = pmd_offset(pud, address);
1178 if (pmd_none(*pmd))
1179 goto out;
1180 if (pmd_page(*pmd) != page)
1181 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001182 /*
1183 * split_vma() may create temporary aliased mappings. There is
1184 * no risk as long as all huge pmd are found and have their
1185 * splitting bit set before __split_huge_page_refcount
1186 * runs. Finding the same huge pmd more than once during the
1187 * same rmap walk is not a problem.
1188 */
1189 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1190 pmd_trans_splitting(*pmd))
1191 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001192 if (pmd_trans_huge(*pmd)) {
1193 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1194 !pmd_trans_splitting(*pmd));
1195 ret = pmd;
1196 }
1197out:
1198 return ret;
1199}
1200
1201static int __split_huge_page_splitting(struct page *page,
1202 struct vm_area_struct *vma,
1203 unsigned long address)
1204{
1205 struct mm_struct *mm = vma->vm_mm;
1206 pmd_t *pmd;
1207 int ret = 0;
1208
1209 spin_lock(&mm->page_table_lock);
1210 pmd = page_check_address_pmd(page, mm, address,
1211 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1212 if (pmd) {
1213 /*
1214 * We can't temporarily set the pmd to null in order
1215 * to split it, the pmd must remain marked huge at all
1216 * times or the VM won't take the pmd_trans_huge paths
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07001217 * and it won't wait on the anon_vma->root->mutex to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 * serialize against split_huge_page*.
1219 */
1220 pmdp_splitting_flush_notify(vma, address, pmd);
1221 ret = 1;
1222 }
1223 spin_unlock(&mm->page_table_lock);
1224
1225 return ret;
1226}
1227
1228static void __split_huge_page_refcount(struct page *page)
1229{
1230 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001231 struct zone *zone = page_zone(page);
Rik van Riel2c888cf2011-01-13 15:47:13 -08001232 int zonestat;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001233 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001234
1235 /* prevent PageLRU to go away from under us, and freeze lru stats */
1236 spin_lock_irq(&zone->lru_lock);
1237 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001238 /* complete memcg works before add pages to LRU */
1239 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001240
Shaohua Li45676882012-01-12 17:19:18 -08001241 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 struct page *page_tail = page + i;
1243
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001244 /* tail_page->_mapcount cannot change */
1245 BUG_ON(page_mapcount(page_tail) < 0);
1246 tail_count += page_mapcount(page_tail);
1247 /* check for overflow */
1248 BUG_ON(tail_count < 0);
1249 BUG_ON(atomic_read(&page_tail->_count) != 0);
1250 /*
1251 * tail_page->_count is zero and not changing from
1252 * under us. But get_page_unless_zero() may be running
1253 * from under us on the tail_page. If we used
1254 * atomic_set() below instead of atomic_add(), we
1255 * would then run atomic_set() concurrently with
1256 * get_page_unless_zero(), and atomic_set() is
1257 * implemented in C not using locked ops. spin_unlock
1258 * on x86 sometime uses locked ops because of PPro
1259 * errata 66, 92, so unless somebody can guarantee
1260 * atomic_set() here would be safe on all archs (and
1261 * not only on x86), it's safer to use atomic_add().
1262 */
1263 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1264 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001265
1266 /* after clearing PageTail the gup refcount can be released */
1267 smp_mb();
1268
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001269 /*
1270 * retain hwpoison flag of the poisoned tail page:
1271 * fix for the unsuitable process killed on Guest Machine(KVM)
1272 * by the memory-failure.
1273 */
1274 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001275 page_tail->flags |= (page->flags &
1276 ((1L << PG_referenced) |
1277 (1L << PG_swapbacked) |
1278 (1L << PG_mlocked) |
1279 (1L << PG_uptodate)));
1280 page_tail->flags |= (1L << PG_dirty);
1281
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001282 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001283 smp_wmb();
1284
1285 /*
1286 * __split_huge_page_splitting() already set the
1287 * splitting bit in all pmd that could map this
1288 * hugepage, that will ensure no CPU can alter the
1289 * mapcount on the head page. The mapcount is only
1290 * accounted in the head page and it has to be
1291 * transferred to all tail pages in the below code. So
1292 * for this code to be safe, the split the mapcount
1293 * can't change. But that doesn't mean userland can't
1294 * keep changing and reading the page contents while
1295 * we transfer the mapcount, so the pmd splitting
1296 * status is achieved setting a reserved bit in the
1297 * pmd, not by clearing the present bit.
1298 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001299 page_tail->_mapcount = page->_mapcount;
1300
1301 BUG_ON(page_tail->mapping);
1302 page_tail->mapping = page->mapping;
1303
Shaohua Li45676882012-01-12 17:19:18 -08001304 page_tail->index = page->index + i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001305
1306 BUG_ON(!PageAnon(page_tail));
1307 BUG_ON(!PageUptodate(page_tail));
1308 BUG_ON(!PageDirty(page_tail));
1309 BUG_ON(!PageSwapBacked(page_tail));
1310
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08001311
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001312 lru_add_page_tail(zone, page, page_tail);
1313 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001314 atomic_sub(tail_count, &page->_count);
1315 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001316
Andrea Arcangeli79134172011-01-13 15:46:58 -08001317 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
1318 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1319
Rik van Riel2c888cf2011-01-13 15:47:13 -08001320 /*
1321 * A hugepage counts for HPAGE_PMD_NR pages on the LRU statistics,
1322 * so adjust those appropriately if this page is on the LRU.
1323 */
1324 if (PageLRU(page)) {
1325 zonestat = NR_LRU_BASE + page_lru(page);
1326 __mod_zone_page_state(zone, zonestat, -(HPAGE_PMD_NR-1));
1327 }
1328
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001329 ClearPageCompound(page);
1330 compound_unlock(page);
1331 spin_unlock_irq(&zone->lru_lock);
1332
1333 for (i = 1; i < HPAGE_PMD_NR; i++) {
1334 struct page *page_tail = page + i;
1335 BUG_ON(page_count(page_tail) <= 0);
1336 /*
1337 * Tail pages may be freed if there wasn't any mapping
1338 * like if add_to_swap() is running on a lru page that
1339 * had its mapping zapped. And freeing these pages
1340 * requires taking the lru_lock so we do the put_page
1341 * of the tail pages after the split is complete.
1342 */
1343 put_page(page_tail);
1344 }
1345
1346 /*
1347 * Only the head page (now become a regular page) is required
1348 * to be pinned by the caller.
1349 */
1350 BUG_ON(page_count(page) <= 0);
1351}
1352
1353static int __split_huge_page_map(struct page *page,
1354 struct vm_area_struct *vma,
1355 unsigned long address)
1356{
1357 struct mm_struct *mm = vma->vm_mm;
1358 pmd_t *pmd, _pmd;
1359 int ret = 0, i;
1360 pgtable_t pgtable;
1361 unsigned long haddr;
1362
1363 spin_lock(&mm->page_table_lock);
1364 pmd = page_check_address_pmd(page, mm, address,
1365 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1366 if (pmd) {
1367 pgtable = get_pmd_huge_pte(mm);
1368 pmd_populate(mm, &_pmd, pgtable);
1369
1370 for (i = 0, haddr = address; i < HPAGE_PMD_NR;
1371 i++, haddr += PAGE_SIZE) {
1372 pte_t *pte, entry;
1373 BUG_ON(PageCompound(page+i));
1374 entry = mk_pte(page + i, vma->vm_page_prot);
1375 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1376 if (!pmd_write(*pmd))
1377 entry = pte_wrprotect(entry);
1378 else
1379 BUG_ON(page_mapcount(page) != 1);
1380 if (!pmd_young(*pmd))
1381 entry = pte_mkold(entry);
1382 pte = pte_offset_map(&_pmd, haddr);
1383 BUG_ON(!pte_none(*pte));
1384 set_pte_at(mm, haddr, pte, entry);
1385 pte_unmap(pte);
1386 }
1387
1388 mm->nr_ptes++;
1389 smp_wmb(); /* make pte visible before pmd */
1390 /*
1391 * Up to this point the pmd is present and huge and
1392 * userland has the whole access to the hugepage
1393 * during the split (which happens in place). If we
1394 * overwrite the pmd with the not-huge version
1395 * pointing to the pte here (which of course we could
1396 * if all CPUs were bug free), userland could trigger
1397 * a small page size TLB miss on the small sized TLB
1398 * while the hugepage TLB entry is still established
1399 * in the huge TLB. Some CPU doesn't like that. See
1400 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1401 * Erratum 383 on page 93. Intel should be safe but is
1402 * also warns that it's only safe if the permission
1403 * and cache attributes of the two entries loaded in
1404 * the two TLB is identical (which should be the case
1405 * here). But it is generally safer to never allow
1406 * small and huge TLB entries for the same virtual
1407 * address to be loaded simultaneously. So instead of
1408 * doing "pmd_populate(); flush_tlb_range();" we first
1409 * mark the current pmd notpresent (atomically because
1410 * here the pmd_trans_huge and pmd_trans_splitting
1411 * must remain set at all times on the pmd until the
1412 * split is complete for this pmd), then we flush the
1413 * SMP TLB and finally we write the non-huge version
1414 * of the pmd entry with pmd_populate.
1415 */
1416 set_pmd_at(mm, address, pmd, pmd_mknotpresent(*pmd));
1417 flush_tlb_range(vma, address, address + HPAGE_PMD_SIZE);
1418 pmd_populate(mm, pmd, pgtable);
1419 ret = 1;
1420 }
1421 spin_unlock(&mm->page_table_lock);
1422
1423 return ret;
1424}
1425
Peter Zijlstra2b575eb2011-05-24 17:12:11 -07001426/* must be called with anon_vma->root->mutex hold */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001427static void __split_huge_page(struct page *page,
1428 struct anon_vma *anon_vma)
1429{
1430 int mapcount, mapcount2;
1431 struct anon_vma_chain *avc;
1432
1433 BUG_ON(!PageHead(page));
1434 BUG_ON(PageTail(page));
1435
1436 mapcount = 0;
1437 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1438 struct vm_area_struct *vma = avc->vma;
1439 unsigned long addr = vma_address(page, vma);
1440 BUG_ON(is_vma_temporary_stack(vma));
1441 if (addr == -EFAULT)
1442 continue;
1443 mapcount += __split_huge_page_splitting(page, vma, addr);
1444 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001445 /*
1446 * It is critical that new vmas are added to the tail of the
1447 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1448 * and establishes a child pmd before
1449 * __split_huge_page_splitting() freezes the parent pmd (so if
1450 * we fail to prevent copy_huge_pmd() from running until the
1451 * whole __split_huge_page() is complete), we will still see
1452 * the newly established pmd of the child later during the
1453 * walk, to be able to set it as pmd_trans_splitting too.
1454 */
1455 if (mapcount != page_mapcount(page))
1456 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1457 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001458 BUG_ON(mapcount != page_mapcount(page));
1459
1460 __split_huge_page_refcount(page);
1461
1462 mapcount2 = 0;
1463 list_for_each_entry(avc, &anon_vma->head, same_anon_vma) {
1464 struct vm_area_struct *vma = avc->vma;
1465 unsigned long addr = vma_address(page, vma);
1466 BUG_ON(is_vma_temporary_stack(vma));
1467 if (addr == -EFAULT)
1468 continue;
1469 mapcount2 += __split_huge_page_map(page, vma, addr);
1470 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001471 if (mapcount != mapcount2)
1472 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1473 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001474 BUG_ON(mapcount != mapcount2);
1475}
1476
1477int split_huge_page(struct page *page)
1478{
1479 struct anon_vma *anon_vma;
1480 int ret = 1;
1481
1482 BUG_ON(!PageAnon(page));
1483 anon_vma = page_lock_anon_vma(page);
1484 if (!anon_vma)
1485 goto out;
1486 ret = 0;
1487 if (!PageCompound(page))
1488 goto out_unlock;
1489
1490 BUG_ON(!PageSwapBacked(page));
1491 __split_huge_page(page, anon_vma);
Andi Kleen81ab4202011-04-14 15:22:06 -07001492 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001493
1494 BUG_ON(PageCompound(page));
1495out_unlock:
1496 page_unlock_anon_vma(anon_vma);
1497out:
1498 return ret;
1499}
1500
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001501#define VM_NO_THP (VM_SPECIAL|VM_INSERTPAGE|VM_MIXEDMAP|VM_SAO| \
1502 VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
1503
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001504int hugepage_madvise(struct vm_area_struct *vma,
1505 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001506{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001507 switch (advice) {
1508 case MADV_HUGEPAGE:
1509 /*
1510 * Be somewhat over-protective like KSM for now!
1511 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001512 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001513 return -EINVAL;
1514 *vm_flags &= ~VM_NOHUGEPAGE;
1515 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001516 /*
1517 * If the vma become good for khugepaged to scan,
1518 * register it here without waiting a page fault that
1519 * may not happen any time soon.
1520 */
1521 if (unlikely(khugepaged_enter_vma_merge(vma)))
1522 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001523 break;
1524 case MADV_NOHUGEPAGE:
1525 /*
1526 * Be somewhat over-protective like KSM for now!
1527 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001528 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001529 return -EINVAL;
1530 *vm_flags &= ~VM_HUGEPAGE;
1531 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001532 /*
1533 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1534 * this vma even if we leave the mm registered in khugepaged if
1535 * it got registered before VM_NOHUGEPAGE was set.
1536 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001537 break;
1538 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001539
1540 return 0;
1541}
1542
Andrea Arcangeliba761492011-01-13 15:46:58 -08001543static int __init khugepaged_slab_init(void)
1544{
1545 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1546 sizeof(struct mm_slot),
1547 __alignof__(struct mm_slot), 0, NULL);
1548 if (!mm_slot_cache)
1549 return -ENOMEM;
1550
1551 return 0;
1552}
1553
1554static void __init khugepaged_slab_free(void)
1555{
1556 kmem_cache_destroy(mm_slot_cache);
1557 mm_slot_cache = NULL;
1558}
1559
1560static inline struct mm_slot *alloc_mm_slot(void)
1561{
1562 if (!mm_slot_cache) /* initialization failed */
1563 return NULL;
1564 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1565}
1566
1567static inline void free_mm_slot(struct mm_slot *mm_slot)
1568{
1569 kmem_cache_free(mm_slot_cache, mm_slot);
1570}
1571
1572static int __init mm_slots_hash_init(void)
1573{
1574 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1575 GFP_KERNEL);
1576 if (!mm_slots_hash)
1577 return -ENOMEM;
1578 return 0;
1579}
1580
1581#if 0
1582static void __init mm_slots_hash_free(void)
1583{
1584 kfree(mm_slots_hash);
1585 mm_slots_hash = NULL;
1586}
1587#endif
1588
1589static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1590{
1591 struct mm_slot *mm_slot;
1592 struct hlist_head *bucket;
1593 struct hlist_node *node;
1594
1595 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1596 % MM_SLOTS_HASH_HEADS];
1597 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1598 if (mm == mm_slot->mm)
1599 return mm_slot;
1600 }
1601 return NULL;
1602}
1603
1604static void insert_to_mm_slots_hash(struct mm_struct *mm,
1605 struct mm_slot *mm_slot)
1606{
1607 struct hlist_head *bucket;
1608
1609 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1610 % MM_SLOTS_HASH_HEADS];
1611 mm_slot->mm = mm;
1612 hlist_add_head(&mm_slot->hash, bucket);
1613}
1614
1615static inline int khugepaged_test_exit(struct mm_struct *mm)
1616{
1617 return atomic_read(&mm->mm_users) == 0;
1618}
1619
1620int __khugepaged_enter(struct mm_struct *mm)
1621{
1622 struct mm_slot *mm_slot;
1623 int wakeup;
1624
1625 mm_slot = alloc_mm_slot();
1626 if (!mm_slot)
1627 return -ENOMEM;
1628
1629 /* __khugepaged_exit() must not run from under us */
1630 VM_BUG_ON(khugepaged_test_exit(mm));
1631 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1632 free_mm_slot(mm_slot);
1633 return 0;
1634 }
1635
1636 spin_lock(&khugepaged_mm_lock);
1637 insert_to_mm_slots_hash(mm, mm_slot);
1638 /*
1639 * Insert just behind the scanning cursor, to let the area settle
1640 * down a little.
1641 */
1642 wakeup = list_empty(&khugepaged_scan.mm_head);
1643 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1644 spin_unlock(&khugepaged_mm_lock);
1645
1646 atomic_inc(&mm->mm_count);
1647 if (wakeup)
1648 wake_up_interruptible(&khugepaged_wait);
1649
1650 return 0;
1651}
1652
1653int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1654{
1655 unsigned long hstart, hend;
1656 if (!vma->anon_vma)
1657 /*
1658 * Not yet faulted in so we will register later in the
1659 * page fault if needed.
1660 */
1661 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001662 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001663 /* khugepaged not yet working on file or special mappings */
1664 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001665 /*
1666 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1667 * true too, verify it here.
1668 */
1669 VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001670 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1671 hend = vma->vm_end & HPAGE_PMD_MASK;
1672 if (hstart < hend)
1673 return khugepaged_enter(vma);
1674 return 0;
1675}
1676
1677void __khugepaged_exit(struct mm_struct *mm)
1678{
1679 struct mm_slot *mm_slot;
1680 int free = 0;
1681
1682 spin_lock(&khugepaged_mm_lock);
1683 mm_slot = get_mm_slot(mm);
1684 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
1685 hlist_del(&mm_slot->hash);
1686 list_del(&mm_slot->mm_node);
1687 free = 1;
1688 }
Chris Wrightd788e802011-07-25 17:12:14 -07001689 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001690
1691 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001692 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1693 free_mm_slot(mm_slot);
1694 mmdrop(mm);
1695 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001696 /*
1697 * This is required to serialize against
1698 * khugepaged_test_exit() (which is guaranteed to run
1699 * under mmap sem read mode). Stop here (after we
1700 * return all pagetables will be destroyed) until
1701 * khugepaged has finished working on the pagetables
1702 * under the mmap_sem.
1703 */
1704 down_write(&mm->mmap_sem);
1705 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001706 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001707}
1708
1709static void release_pte_page(struct page *page)
1710{
1711 /* 0 stands for page_is_file_cache(page) == false */
1712 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1713 unlock_page(page);
1714 putback_lru_page(page);
1715}
1716
1717static void release_pte_pages(pte_t *pte, pte_t *_pte)
1718{
1719 while (--_pte >= pte) {
1720 pte_t pteval = *_pte;
1721 if (!pte_none(pteval))
1722 release_pte_page(pte_page(pteval));
1723 }
1724}
1725
1726static void release_all_pte_pages(pte_t *pte)
1727{
1728 release_pte_pages(pte, pte + HPAGE_PMD_NR);
1729}
1730
1731static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1732 unsigned long address,
1733 pte_t *pte)
1734{
1735 struct page *page;
1736 pte_t *_pte;
1737 int referenced = 0, isolated = 0, none = 0;
1738 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1739 _pte++, address += PAGE_SIZE) {
1740 pte_t pteval = *_pte;
1741 if (pte_none(pteval)) {
1742 if (++none <= khugepaged_max_ptes_none)
1743 continue;
1744 else {
1745 release_pte_pages(pte, _pte);
1746 goto out;
1747 }
1748 }
1749 if (!pte_present(pteval) || !pte_write(pteval)) {
1750 release_pte_pages(pte, _pte);
1751 goto out;
1752 }
1753 page = vm_normal_page(vma, address, pteval);
1754 if (unlikely(!page)) {
1755 release_pte_pages(pte, _pte);
1756 goto out;
1757 }
1758 VM_BUG_ON(PageCompound(page));
1759 BUG_ON(!PageAnon(page));
1760 VM_BUG_ON(!PageSwapBacked(page));
1761
1762 /* cannot use mapcount: can't collapse if there's a gup pin */
1763 if (page_count(page) != 1) {
1764 release_pte_pages(pte, _pte);
1765 goto out;
1766 }
1767 /*
1768 * We can do it before isolate_lru_page because the
1769 * page can't be freed from under us. NOTE: PG_lock
1770 * is needed to serialize against split_huge_page
1771 * when invoked from the VM.
1772 */
1773 if (!trylock_page(page)) {
1774 release_pte_pages(pte, _pte);
1775 goto out;
1776 }
1777 /*
1778 * Isolate the page to avoid collapsing an hugepage
1779 * currently in use by the VM.
1780 */
1781 if (isolate_lru_page(page)) {
1782 unlock_page(page);
1783 release_pte_pages(pte, _pte);
1784 goto out;
1785 }
1786 /* 0 stands for page_is_file_cache(page) == false */
1787 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
1788 VM_BUG_ON(!PageLocked(page));
1789 VM_BUG_ON(PageLRU(page));
1790
1791 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001792 if (pte_young(pteval) || PageReferenced(page) ||
1793 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001794 referenced = 1;
1795 }
1796 if (unlikely(!referenced))
1797 release_all_pte_pages(pte);
1798 else
1799 isolated = 1;
1800out:
1801 return isolated;
1802}
1803
1804static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1805 struct vm_area_struct *vma,
1806 unsigned long address,
1807 spinlock_t *ptl)
1808{
1809 pte_t *_pte;
1810 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1811 pte_t pteval = *_pte;
1812 struct page *src_page;
1813
1814 if (pte_none(pteval)) {
1815 clear_user_highpage(page, address);
1816 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
1817 } else {
1818 src_page = pte_page(pteval);
1819 copy_user_highpage(page, src_page, address, vma);
1820 VM_BUG_ON(page_mapcount(src_page) != 1);
1821 VM_BUG_ON(page_count(src_page) != 2);
1822 release_pte_page(src_page);
1823 /*
1824 * ptl mostly unnecessary, but preempt has to
1825 * be disabled to update the per-cpu stats
1826 * inside page_remove_rmap().
1827 */
1828 spin_lock(ptl);
1829 /*
1830 * paravirt calls inside pte_clear here are
1831 * superfluous.
1832 */
1833 pte_clear(vma->vm_mm, address, _pte);
1834 page_remove_rmap(src_page);
1835 spin_unlock(ptl);
1836 free_page_and_swap_cache(src_page);
1837 }
1838
1839 address += PAGE_SIZE;
1840 page++;
1841 }
1842}
1843
1844static void collapse_huge_page(struct mm_struct *mm,
1845 unsigned long address,
Andrea Arcangelice83d212011-01-13 15:47:06 -08001846 struct page **hpage,
Andi Kleen5c4b4be2011-03-04 17:36:32 -08001847 struct vm_area_struct *vma,
1848 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001849{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001850 pgd_t *pgd;
1851 pud_t *pud;
1852 pmd_t *pmd, _pmd;
1853 pte_t *pte;
1854 pgtable_t pgtable;
1855 struct page *new_page;
1856 spinlock_t *ptl;
1857 int isolated;
1858 unsigned long hstart, hend;
1859
1860 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001861#ifndef CONFIG_NUMA
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07001862 up_read(&mm->mmap_sem);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001863 VM_BUG_ON(!*hpage);
Andrea Arcangelice83d212011-01-13 15:47:06 -08001864 new_page = *hpage;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001865#else
1866 VM_BUG_ON(*hpage);
Andrea Arcangelice83d212011-01-13 15:47:06 -08001867 /*
1868 * Allocate the page while the vma is still valid and under
1869 * the mmap_sem read mode so there is no memory allocation
1870 * later when we take the mmap_sem in write mode. This is more
1871 * friendly behavior (OTOH it may actually hide bugs) to
1872 * filesystems in userland with daemons allocating memory in
1873 * the userland I/O paths. Allocating memory with the
1874 * mmap_sem in read mode is good idea also to allow greater
1875 * scalability.
1876 */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08001877 new_page = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
Andi Kleencc5d4622011-03-22 16:33:13 -07001878 node, __GFP_OTHER_NODE);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07001879
1880 /*
1881 * After allocating the hugepage, release the mmap_sem read lock in
1882 * preparation for taking it in write mode.
1883 */
1884 up_read(&mm->mmap_sem);
Andrea Arcangelice83d212011-01-13 15:47:06 -08001885 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -07001886 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
Andrea Arcangelice83d212011-01-13 15:47:06 -08001887 *hpage = ERR_PTR(-ENOMEM);
1888 return;
1889 }
Hugh Dickins2fbfac42011-03-14 01:08:47 -07001890#endif
Andrea Arcangelice83d212011-01-13 15:47:06 -08001891
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07001892 count_vm_event(THP_COLLAPSE_ALLOC);
1893 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1894#ifdef CONFIG_NUMA
1895 put_page(new_page);
1896#endif
1897 return;
1898 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001899
1900 /*
1901 * Prevent all access to pagetables with the exception of
1902 * gup_fast later hanlded by the ptep_clear_flush and the VM
1903 * handled by the anon_vma lock + PG_lock.
1904 */
1905 down_write(&mm->mmap_sem);
1906 if (unlikely(khugepaged_test_exit(mm)))
1907 goto out;
1908
1909 vma = find_vma(mm, address);
1910 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1911 hend = vma->vm_end & HPAGE_PMD_MASK;
1912 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
1913 goto out;
1914
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001915 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
1916 (vma->vm_flags & VM_NOHUGEPAGE))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001917 goto out;
1918
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001919 if (!vma->anon_vma || vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001920 goto out;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01001921 if (is_vma_temporary_stack(vma))
1922 goto out;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001923 /*
1924 * If is_pfn_mapping() is true is_learn_pfn_mapping() must be
1925 * true too, verify it here.
1926 */
1927 VM_BUG_ON(is_linear_pfn_mapping(vma) || vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001928
1929 pgd = pgd_offset(mm, address);
1930 if (!pgd_present(*pgd))
1931 goto out;
1932
1933 pud = pud_offset(pgd, address);
1934 if (!pud_present(*pud))
1935 goto out;
1936
1937 pmd = pmd_offset(pud, address);
1938 /* pmd can't go away or become huge under us */
1939 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
1940 goto out;
1941
Andrea Arcangeliba761492011-01-13 15:46:58 -08001942 anon_vma_lock(vma->anon_vma);
1943
1944 pte = pte_offset_map(pmd, address);
1945 ptl = pte_lockptr(mm, pmd);
1946
1947 spin_lock(&mm->page_table_lock); /* probably unnecessary */
1948 /*
1949 * After this gup_fast can't run anymore. This also removes
1950 * any huge TLB entry from the CPU so we won't allow
1951 * huge and small TLB entries for the same virtual address
1952 * to avoid the risk of CPU bugs in that area.
1953 */
1954 _pmd = pmdp_clear_flush_notify(vma, address, pmd);
1955 spin_unlock(&mm->page_table_lock);
1956
1957 spin_lock(ptl);
1958 isolated = __collapse_huge_page_isolate(vma, address, pte);
1959 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001960
1961 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08001962 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001963 spin_lock(&mm->page_table_lock);
1964 BUG_ON(!pmd_none(*pmd));
1965 set_pmd_at(mm, address, pmd, _pmd);
1966 spin_unlock(&mm->page_table_lock);
1967 anon_vma_unlock(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08001968 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001969 }
1970
1971 /*
1972 * All pages are isolated and locked so anon_vma rmap
1973 * can't run anymore.
1974 */
1975 anon_vma_unlock(vma->anon_vma);
1976
1977 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08001978 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001979 __SetPageUptodate(new_page);
1980 pgtable = pmd_pgtable(_pmd);
1981 VM_BUG_ON(page_count(pgtable) != 1);
1982 VM_BUG_ON(page_mapcount(pgtable) != 0);
1983
1984 _pmd = mk_pmd(new_page, vma->vm_page_prot);
1985 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1986 _pmd = pmd_mkhuge(_pmd);
1987
1988 /*
1989 * spin_lock() below is not the equivalent of smp_wmb(), so
1990 * this is needed to avoid the copy_huge_page writes to become
1991 * visible after the set_pmd_at() write.
1992 */
1993 smp_wmb();
1994
1995 spin_lock(&mm->page_table_lock);
1996 BUG_ON(!pmd_none(*pmd));
1997 page_add_new_anon_rmap(new_page, vma, address);
1998 set_pmd_at(mm, address, pmd, _pmd);
Hillf Danton35d8c7a2011-10-31 17:09:40 -07001999 update_mmu_cache(vma, address, _pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002000 prepare_pmd_huge_pte(pgtable, mm);
2001 mm->nr_ptes--;
2002 spin_unlock(&mm->page_table_lock);
2003
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002004#ifndef CONFIG_NUMA
Andrea Arcangeliba761492011-01-13 15:46:58 -08002005 *hpage = NULL;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002006#endif
Andrea Arcangeliba761492011-01-13 15:46:58 -08002007 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002008out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002009 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002010 return;
2011
Andrea Arcangelice83d212011-01-13 15:47:06 -08002012out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002013 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002014#ifdef CONFIG_NUMA
2015 put_page(new_page);
2016#endif
Andrea Arcangelice83d212011-01-13 15:47:06 -08002017 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002018}
2019
2020static int khugepaged_scan_pmd(struct mm_struct *mm,
2021 struct vm_area_struct *vma,
2022 unsigned long address,
2023 struct page **hpage)
2024{
2025 pgd_t *pgd;
2026 pud_t *pud;
2027 pmd_t *pmd;
2028 pte_t *pte, *_pte;
2029 int ret = 0, referenced = 0, none = 0;
2030 struct page *page;
2031 unsigned long _address;
2032 spinlock_t *ptl;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002033 int node = -1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002034
2035 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2036
2037 pgd = pgd_offset(mm, address);
2038 if (!pgd_present(*pgd))
2039 goto out;
2040
2041 pud = pud_offset(pgd, address);
2042 if (!pud_present(*pud))
2043 goto out;
2044
2045 pmd = pmd_offset(pud, address);
2046 if (!pmd_present(*pmd) || pmd_trans_huge(*pmd))
2047 goto out;
2048
2049 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2050 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2051 _pte++, _address += PAGE_SIZE) {
2052 pte_t pteval = *_pte;
2053 if (pte_none(pteval)) {
2054 if (++none <= khugepaged_max_ptes_none)
2055 continue;
2056 else
2057 goto out_unmap;
2058 }
2059 if (!pte_present(pteval) || !pte_write(pteval))
2060 goto out_unmap;
2061 page = vm_normal_page(vma, _address, pteval);
2062 if (unlikely(!page))
2063 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002064 /*
2065 * Chose the node of the first page. This could
2066 * be more sophisticated and look at more pages,
2067 * but isn't for now.
2068 */
2069 if (node == -1)
2070 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002071 VM_BUG_ON(PageCompound(page));
2072 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2073 goto out_unmap;
2074 /* cannot use mapcount: can't collapse if there's a gup pin */
2075 if (page_count(page) != 1)
2076 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002077 if (pte_young(pteval) || PageReferenced(page) ||
2078 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002079 referenced = 1;
2080 }
2081 if (referenced)
2082 ret = 1;
2083out_unmap:
2084 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002085 if (ret)
2086 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002087 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002088out:
2089 return ret;
2090}
2091
2092static void collect_mm_slot(struct mm_slot *mm_slot)
2093{
2094 struct mm_struct *mm = mm_slot->mm;
2095
2096 VM_BUG_ON(!spin_is_locked(&khugepaged_mm_lock));
2097
2098 if (khugepaged_test_exit(mm)) {
2099 /* free mm_slot */
2100 hlist_del(&mm_slot->hash);
2101 list_del(&mm_slot->mm_node);
2102
2103 /*
2104 * Not strictly needed because the mm exited already.
2105 *
2106 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2107 */
2108
2109 /* khugepaged_mm_lock actually not necessary for the below */
2110 free_mm_slot(mm_slot);
2111 mmdrop(mm);
2112 }
2113}
2114
2115static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2116 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002117 __releases(&khugepaged_mm_lock)
2118 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002119{
2120 struct mm_slot *mm_slot;
2121 struct mm_struct *mm;
2122 struct vm_area_struct *vma;
2123 int progress = 0;
2124
2125 VM_BUG_ON(!pages);
2126 VM_BUG_ON(!spin_is_locked(&khugepaged_mm_lock));
2127
2128 if (khugepaged_scan.mm_slot)
2129 mm_slot = khugepaged_scan.mm_slot;
2130 else {
2131 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2132 struct mm_slot, mm_node);
2133 khugepaged_scan.address = 0;
2134 khugepaged_scan.mm_slot = mm_slot;
2135 }
2136 spin_unlock(&khugepaged_mm_lock);
2137
2138 mm = mm_slot->mm;
2139 down_read(&mm->mmap_sem);
2140 if (unlikely(khugepaged_test_exit(mm)))
2141 vma = NULL;
2142 else
2143 vma = find_vma(mm, khugepaged_scan.address);
2144
2145 progress++;
2146 for (; vma; vma = vma->vm_next) {
2147 unsigned long hstart, hend;
2148
2149 cond_resched();
2150 if (unlikely(khugepaged_test_exit(mm))) {
2151 progress++;
2152 break;
2153 }
2154
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002155 if ((!(vma->vm_flags & VM_HUGEPAGE) &&
2156 !khugepaged_always()) ||
2157 (vma->vm_flags & VM_NOHUGEPAGE)) {
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002158 skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002159 progress++;
2160 continue;
2161 }
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002162 if (!vma->anon_vma || vma->vm_ops)
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002163 goto skip;
2164 if (is_vma_temporary_stack(vma))
2165 goto skip;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002166 /*
2167 * If is_pfn_mapping() is true is_learn_pfn_mapping()
2168 * must be true too, verify it here.
2169 */
2170 VM_BUG_ON(is_linear_pfn_mapping(vma) ||
2171 vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002172
2173 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2174 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002175 if (hstart >= hend)
2176 goto skip;
2177 if (khugepaged_scan.address > hend)
2178 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002179 if (khugepaged_scan.address < hstart)
2180 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002181 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002182
2183 while (khugepaged_scan.address < hend) {
2184 int ret;
2185 cond_resched();
2186 if (unlikely(khugepaged_test_exit(mm)))
2187 goto breakouterloop;
2188
2189 VM_BUG_ON(khugepaged_scan.address < hstart ||
2190 khugepaged_scan.address + HPAGE_PMD_SIZE >
2191 hend);
2192 ret = khugepaged_scan_pmd(mm, vma,
2193 khugepaged_scan.address,
2194 hpage);
2195 /* move to next address */
2196 khugepaged_scan.address += HPAGE_PMD_SIZE;
2197 progress += HPAGE_PMD_NR;
2198 if (ret)
2199 /* we released mmap_sem so break loop */
2200 goto breakouterloop_mmap_sem;
2201 if (progress >= pages)
2202 goto breakouterloop;
2203 }
2204 }
2205breakouterloop:
2206 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2207breakouterloop_mmap_sem:
2208
2209 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002210 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002211 /*
2212 * Release the current mm_slot if this mm is about to die, or
2213 * if we scanned all vmas of this mm.
2214 */
2215 if (khugepaged_test_exit(mm) || !vma) {
2216 /*
2217 * Make sure that if mm_users is reaching zero while
2218 * khugepaged runs here, khugepaged_exit will find
2219 * mm_slot not pointing to the exiting mm.
2220 */
2221 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2222 khugepaged_scan.mm_slot = list_entry(
2223 mm_slot->mm_node.next,
2224 struct mm_slot, mm_node);
2225 khugepaged_scan.address = 0;
2226 } else {
2227 khugepaged_scan.mm_slot = NULL;
2228 khugepaged_full_scans++;
2229 }
2230
2231 collect_mm_slot(mm_slot);
2232 }
2233
2234 return progress;
2235}
2236
2237static int khugepaged_has_work(void)
2238{
2239 return !list_empty(&khugepaged_scan.mm_head) &&
2240 khugepaged_enabled();
2241}
2242
2243static int khugepaged_wait_event(void)
2244{
2245 return !list_empty(&khugepaged_scan.mm_head) ||
2246 !khugepaged_enabled();
2247}
2248
2249static void khugepaged_do_scan(struct page **hpage)
2250{
2251 unsigned int progress = 0, pass_through_head = 0;
2252 unsigned int pages = khugepaged_pages_to_scan;
2253
2254 barrier(); /* write khugepaged_pages_to_scan to local stack */
2255
2256 while (progress < pages) {
2257 cond_resched();
2258
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002259#ifndef CONFIG_NUMA
Andrea Arcangeliba761492011-01-13 15:46:58 -08002260 if (!*hpage) {
2261 *hpage = alloc_hugepage(khugepaged_defrag());
Andi Kleen81ab4202011-04-14 15:22:06 -07002262 if (unlikely(!*hpage)) {
2263 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002264 break;
Andi Kleen81ab4202011-04-14 15:22:06 -07002265 }
2266 count_vm_event(THP_COLLAPSE_ALLOC);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002267 }
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002268#else
2269 if (IS_ERR(*hpage))
2270 break;
2271#endif
Andrea Arcangeliba761492011-01-13 15:46:58 -08002272
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002273 if (unlikely(kthread_should_stop() || freezing(current)))
2274 break;
2275
Andrea Arcangeliba761492011-01-13 15:46:58 -08002276 spin_lock(&khugepaged_mm_lock);
2277 if (!khugepaged_scan.mm_slot)
2278 pass_through_head++;
2279 if (khugepaged_has_work() &&
2280 pass_through_head < 2)
2281 progress += khugepaged_scan_mm_slot(pages - progress,
2282 hpage);
2283 else
2284 progress = pages;
2285 spin_unlock(&khugepaged_mm_lock);
2286 }
2287}
2288
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002289static void khugepaged_alloc_sleep(void)
2290{
Andrea Arcangeli1dfb0592011-12-08 14:33:57 -08002291 wait_event_freezable_timeout(khugepaged_wait, false,
2292 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002293}
2294
2295#ifndef CONFIG_NUMA
Andrea Arcangeliba761492011-01-13 15:46:58 -08002296static struct page *khugepaged_alloc_hugepage(void)
2297{
2298 struct page *hpage;
2299
2300 do {
2301 hpage = alloc_hugepage(khugepaged_defrag());
Andi Kleen81ab4202011-04-14 15:22:06 -07002302 if (!hpage) {
2303 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002304 khugepaged_alloc_sleep();
Andi Kleen81ab4202011-04-14 15:22:06 -07002305 } else
2306 count_vm_event(THP_COLLAPSE_ALLOC);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002307 } while (unlikely(!hpage) &&
2308 likely(khugepaged_enabled()));
2309 return hpage;
2310}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002311#endif
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312
2313static void khugepaged_loop(void)
2314{
2315 struct page *hpage;
2316
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002317#ifdef CONFIG_NUMA
2318 hpage = NULL;
2319#endif
Andrea Arcangeliba761492011-01-13 15:46:58 -08002320 while (likely(khugepaged_enabled())) {
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002321#ifndef CONFIG_NUMA
Andrea Arcangeliba761492011-01-13 15:46:58 -08002322 hpage = khugepaged_alloc_hugepage();
Andrea Arcangelif300ea42011-06-15 15:08:08 -07002323 if (unlikely(!hpage))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002324 break;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002325#else
2326 if (IS_ERR(hpage)) {
2327 khugepaged_alloc_sleep();
2328 hpage = NULL;
2329 }
2330#endif
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331
2332 khugepaged_do_scan(&hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002333#ifndef CONFIG_NUMA
Andrea Arcangeliba761492011-01-13 15:46:58 -08002334 if (hpage)
2335 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002336#endif
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002337 try_to_freeze();
2338 if (unlikely(kthread_should_stop()))
2339 break;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002340 if (khugepaged_has_work()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002341 if (!khugepaged_scan_sleep_millisecs)
2342 continue;
Andrea Arcangeli1dfb0592011-12-08 14:33:57 -08002343 wait_event_freezable_timeout(khugepaged_wait, false,
2344 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002345 } else if (khugepaged_enabled())
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002346 wait_event_freezable(khugepaged_wait,
2347 khugepaged_wait_event());
Andrea Arcangeliba761492011-01-13 15:46:58 -08002348 }
2349}
2350
2351static int khugepaged(void *none)
2352{
2353 struct mm_slot *mm_slot;
2354
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002355 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356 set_user_nice(current, 19);
2357
2358 /* serialize with start_khugepaged() */
2359 mutex_lock(&khugepaged_mutex);
2360
2361 for (;;) {
2362 mutex_unlock(&khugepaged_mutex);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002363 VM_BUG_ON(khugepaged_thread != current);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002364 khugepaged_loop();
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002365 VM_BUG_ON(khugepaged_thread != current);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002366
2367 mutex_lock(&khugepaged_mutex);
2368 if (!khugepaged_enabled())
2369 break;
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002370 if (unlikely(kthread_should_stop()))
2371 break;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002372 }
2373
2374 spin_lock(&khugepaged_mm_lock);
2375 mm_slot = khugepaged_scan.mm_slot;
2376 khugepaged_scan.mm_slot = NULL;
2377 if (mm_slot)
2378 collect_mm_slot(mm_slot);
2379 spin_unlock(&khugepaged_mm_lock);
2380
2381 khugepaged_thread = NULL;
2382 mutex_unlock(&khugepaged_mutex);
2383
2384 return 0;
2385}
2386
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002387void __split_huge_page_pmd(struct mm_struct *mm, pmd_t *pmd)
2388{
2389 struct page *page;
2390
2391 spin_lock(&mm->page_table_lock);
2392 if (unlikely(!pmd_trans_huge(*pmd))) {
2393 spin_unlock(&mm->page_table_lock);
2394 return;
2395 }
2396 page = pmd_page(*pmd);
2397 VM_BUG_ON(!page_count(page));
2398 get_page(page);
2399 spin_unlock(&mm->page_table_lock);
2400
2401 split_huge_page(page);
2402
2403 put_page(page);
2404 BUG_ON(pmd_trans_huge(*pmd));
2405}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002406
2407static void split_huge_page_address(struct mm_struct *mm,
2408 unsigned long address)
2409{
2410 pgd_t *pgd;
2411 pud_t *pud;
2412 pmd_t *pmd;
2413
2414 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2415
2416 pgd = pgd_offset(mm, address);
2417 if (!pgd_present(*pgd))
2418 return;
2419
2420 pud = pud_offset(pgd, address);
2421 if (!pud_present(*pud))
2422 return;
2423
2424 pmd = pmd_offset(pud, address);
2425 if (!pmd_present(*pmd))
2426 return;
2427 /*
2428 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2429 * materialize from under us.
2430 */
2431 split_huge_page_pmd(mm, pmd);
2432}
2433
2434void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2435 unsigned long start,
2436 unsigned long end,
2437 long adjust_next)
2438{
2439 /*
2440 * If the new start address isn't hpage aligned and it could
2441 * previously contain an hugepage: check if we need to split
2442 * an huge pmd.
2443 */
2444 if (start & ~HPAGE_PMD_MASK &&
2445 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2446 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2447 split_huge_page_address(vma->vm_mm, start);
2448
2449 /*
2450 * If the new end address isn't hpage aligned and it could
2451 * previously contain an hugepage: check if we need to split
2452 * an huge pmd.
2453 */
2454 if (end & ~HPAGE_PMD_MASK &&
2455 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2456 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2457 split_huge_page_address(vma->vm_mm, end);
2458
2459 /*
2460 * If we're also updating the vma->vm_next->vm_start, if the new
2461 * vm_next->vm_start isn't page aligned and it could previously
2462 * contain an hugepage: check if we need to split an huge pmd.
2463 */
2464 if (adjust_next > 0) {
2465 struct vm_area_struct *next = vma->vm_next;
2466 unsigned long nstart = next->vm_start;
2467 nstart += adjust_next << PAGE_SHIFT;
2468 if (nstart & ~HPAGE_PMD_MASK &&
2469 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2470 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2471 split_huge_page_address(next->vm_mm, nstart);
2472 }
2473}