blob: 45eaae03062805899f559399691b42ca9554d829 [file] [log] [blame]
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001/*
2 * Copyright (C) 2009 Red Hat, Inc.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 */
7
8#include <linux/mm.h>
9#include <linux/sched.h>
10#include <linux/highmem.h>
11#include <linux/hugetlb.h>
12#include <linux/mmu_notifier.h>
13#include <linux/rmap.h>
14#include <linux/swap.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080015#include <linux/shrinker.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080016#include <linux/mm_inline.h>
17#include <linux/kthread.h>
18#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080019#include <linux/freezer.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080020#include <linux/mman.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020021#include <linux/pagemap.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000022#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080023#include <linux/hashtable.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080024
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080025#include <asm/tlb.h>
26#include <asm/pgalloc.h>
27#include "internal.h"
28
Andrea Arcangeliba761492011-01-13 15:46:58 -080029/*
30 * By default transparent hugepage support is enabled for all mappings
31 * and khugepaged scans all mappings. Defrag is only invoked by
32 * khugepaged hugepage allocations and by page faults inside
33 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
34 * allocations.
35 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080036unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080037#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080038 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080039#endif
40#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
41 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
42#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080043 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080044 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
45 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080046
47/* default scan 8*512 pte (or vmas) every 30 second */
48static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
49static unsigned int khugepaged_pages_collapsed;
50static unsigned int khugepaged_full_scans;
51static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
52/* during fragmentation poll the hugepage allocator once every minute */
53static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
54static struct task_struct *khugepaged_thread __read_mostly;
55static DEFINE_MUTEX(khugepaged_mutex);
56static DEFINE_SPINLOCK(khugepaged_mm_lock);
57static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
58/*
59 * default collapse hugepages if there is at least one pte mapped like
60 * it would have happened if the vma was large enough during page
61 * fault.
62 */
63static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
64
65static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -080066static int khugepaged_slab_init(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -080067
Sasha Levin43b5fbb2013-02-22 16:32:27 -080068#define MM_SLOTS_HASH_BITS 10
69static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
70
Andrea Arcangeliba761492011-01-13 15:46:58 -080071static struct kmem_cache *mm_slot_cache __read_mostly;
72
73/**
74 * struct mm_slot - hash lookup from mm to mm_slot
75 * @hash: hash collision list
76 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
77 * @mm: the mm that this information is valid for
78 */
79struct mm_slot {
80 struct hlist_node hash;
81 struct list_head mm_node;
82 struct mm_struct *mm;
83};
84
85/**
86 * struct khugepaged_scan - cursor for scanning
87 * @mm_head: the head of the mm list to scan
88 * @mm_slot: the current mm_slot we are scanning
89 * @address: the next address inside that to be scanned
90 *
91 * There is only the one khugepaged_scan instance of this cursor structure.
92 */
93struct khugepaged_scan {
94 struct list_head mm_head;
95 struct mm_slot *mm_slot;
96 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -070097};
98static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -080099 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
100};
101
Andrea Arcangelif0005652011-01-13 15:47:04 -0800102
103static int set_recommended_min_free_kbytes(void)
104{
105 struct zone *zone;
106 int nr_zones = 0;
107 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800108
Xiao Guangrong17c230a2012-10-08 16:29:56 -0700109 if (!khugepaged_enabled())
Andrea Arcangelif0005652011-01-13 15:47:04 -0800110 return 0;
111
112 for_each_populated_zone(zone)
113 nr_zones++;
114
115 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
116 recommended_min = pageblock_nr_pages * nr_zones * 2;
117
118 /*
119 * Make sure that on average at least two pageblocks are almost free
120 * of another type, one for a migratetype to fall back to and a
121 * second to avoid subsequent fallbacks of other types There are 3
122 * MIGRATE_TYPES we care about.
123 */
124 recommended_min += pageblock_nr_pages * nr_zones *
125 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
126
127 /* don't ever allow to reserve more than 5% of the lowmem */
128 recommended_min = min(recommended_min,
129 (unsigned long) nr_free_buffer_pages() / 20);
130 recommended_min <<= (PAGE_SHIFT-10);
131
132 if (recommended_min > min_free_kbytes)
133 min_free_kbytes = recommended_min;
134 setup_per_zone_wmarks();
135 return 0;
136}
137late_initcall(set_recommended_min_free_kbytes);
138
Andrea Arcangeliba761492011-01-13 15:46:58 -0800139static int start_khugepaged(void)
140{
141 int err = 0;
142 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800143 if (!khugepaged_thread)
144 khugepaged_thread = kthread_run(khugepaged, NULL,
145 "khugepaged");
146 if (unlikely(IS_ERR(khugepaged_thread))) {
147 printk(KERN_ERR
148 "khugepaged: kthread_run(khugepaged) failed\n");
149 err = PTR_ERR(khugepaged_thread);
150 khugepaged_thread = NULL;
151 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700152
153 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800154 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800155
156 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700157 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700158 kthread_stop(khugepaged_thread);
159 khugepaged_thread = NULL;
160 }
Xiao Guangrong637e3a22012-10-08 16:29:38 -0700161
Andrea Arcangeliba761492011-01-13 15:46:58 -0800162 return err;
163}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800164
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800165static atomic_t huge_zero_refcount;
166static unsigned long huge_zero_pfn __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800167
168static inline bool is_huge_zero_pfn(unsigned long pfn)
169{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800170 unsigned long zero_pfn = ACCESS_ONCE(huge_zero_pfn);
171 return zero_pfn && pfn == zero_pfn;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800172}
173
174static inline bool is_huge_zero_pmd(pmd_t pmd)
175{
176 return is_huge_zero_pfn(pmd_pfn(pmd));
177}
178
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800179static unsigned long get_huge_zero_page(void)
180{
181 struct page *zero_page;
182retry:
183 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
184 return ACCESS_ONCE(huge_zero_pfn);
185
186 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
187 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800188 if (!zero_page) {
189 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800190 return 0;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800191 }
192 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800193 preempt_disable();
194 if (cmpxchg(&huge_zero_pfn, 0, page_to_pfn(zero_page))) {
195 preempt_enable();
196 __free_page(zero_page);
197 goto retry;
198 }
199
200 /* We take additional reference here. It will be put back by shrinker */
201 atomic_set(&huge_zero_refcount, 2);
202 preempt_enable();
203 return ACCESS_ONCE(huge_zero_pfn);
204}
205
206static void put_huge_zero_page(void)
207{
208 /*
209 * Counter should never go to zero here. Only shrinker can put
210 * last reference.
211 */
212 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
213}
214
215static int shrink_huge_zero_page(struct shrinker *shrink,
216 struct shrink_control *sc)
217{
218 if (!sc->nr_to_scan)
219 /* we can free zero page only if last reference remains */
220 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
221
222 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
223 unsigned long zero_pfn = xchg(&huge_zero_pfn, 0);
224 BUG_ON(zero_pfn == 0);
225 __free_page(__pfn_to_page(zero_pfn));
226 }
227
228 return 0;
229}
230
231static struct shrinker huge_zero_page_shrinker = {
232 .shrink = shrink_huge_zero_page,
233 .seeks = DEFAULT_SEEKS,
234};
235
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800236#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800237
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800238static ssize_t double_flag_show(struct kobject *kobj,
239 struct kobj_attribute *attr, char *buf,
240 enum transparent_hugepage_flag enabled,
241 enum transparent_hugepage_flag req_madv)
242{
243 if (test_bit(enabled, &transparent_hugepage_flags)) {
244 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
245 return sprintf(buf, "[always] madvise never\n");
246 } else if (test_bit(req_madv, &transparent_hugepage_flags))
247 return sprintf(buf, "always [madvise] never\n");
248 else
249 return sprintf(buf, "always madvise [never]\n");
250}
251static ssize_t double_flag_store(struct kobject *kobj,
252 struct kobj_attribute *attr,
253 const char *buf, size_t count,
254 enum transparent_hugepage_flag enabled,
255 enum transparent_hugepage_flag req_madv)
256{
257 if (!memcmp("always", buf,
258 min(sizeof("always")-1, count))) {
259 set_bit(enabled, &transparent_hugepage_flags);
260 clear_bit(req_madv, &transparent_hugepage_flags);
261 } else if (!memcmp("madvise", buf,
262 min(sizeof("madvise")-1, count))) {
263 clear_bit(enabled, &transparent_hugepage_flags);
264 set_bit(req_madv, &transparent_hugepage_flags);
265 } else if (!memcmp("never", buf,
266 min(sizeof("never")-1, count))) {
267 clear_bit(enabled, &transparent_hugepage_flags);
268 clear_bit(req_madv, &transparent_hugepage_flags);
269 } else
270 return -EINVAL;
271
272 return count;
273}
274
275static ssize_t enabled_show(struct kobject *kobj,
276 struct kobj_attribute *attr, char *buf)
277{
278 return double_flag_show(kobj, attr, buf,
279 TRANSPARENT_HUGEPAGE_FLAG,
280 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
281}
282static ssize_t enabled_store(struct kobject *kobj,
283 struct kobj_attribute *attr,
284 const char *buf, size_t count)
285{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800286 ssize_t ret;
287
288 ret = double_flag_store(kobj, attr, buf, count,
289 TRANSPARENT_HUGEPAGE_FLAG,
290 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
291
292 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700293 int err;
294
295 mutex_lock(&khugepaged_mutex);
296 err = start_khugepaged();
297 mutex_unlock(&khugepaged_mutex);
298
Andrea Arcangeliba761492011-01-13 15:46:58 -0800299 if (err)
300 ret = err;
301 }
302
303 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800304}
305static struct kobj_attribute enabled_attr =
306 __ATTR(enabled, 0644, enabled_show, enabled_store);
307
308static ssize_t single_flag_show(struct kobject *kobj,
309 struct kobj_attribute *attr, char *buf,
310 enum transparent_hugepage_flag flag)
311{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700312 return sprintf(buf, "%d\n",
313 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800314}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700315
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800316static ssize_t single_flag_store(struct kobject *kobj,
317 struct kobj_attribute *attr,
318 const char *buf, size_t count,
319 enum transparent_hugepage_flag flag)
320{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700321 unsigned long value;
322 int ret;
323
324 ret = kstrtoul(buf, 10, &value);
325 if (ret < 0)
326 return ret;
327 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800328 return -EINVAL;
329
Ben Hutchingse27e6152011-04-14 15:22:21 -0700330 if (value)
331 set_bit(flag, &transparent_hugepage_flags);
332 else
333 clear_bit(flag, &transparent_hugepage_flags);
334
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800335 return count;
336}
337
338/*
339 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
340 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
341 * memory just to allocate one more hugepage.
342 */
343static ssize_t defrag_show(struct kobject *kobj,
344 struct kobj_attribute *attr, char *buf)
345{
346 return double_flag_show(kobj, attr, buf,
347 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
348 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
349}
350static ssize_t defrag_store(struct kobject *kobj,
351 struct kobj_attribute *attr,
352 const char *buf, size_t count)
353{
354 return double_flag_store(kobj, attr, buf, count,
355 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
356 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
357}
358static struct kobj_attribute defrag_attr =
359 __ATTR(defrag, 0644, defrag_show, defrag_store);
360
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800361static ssize_t use_zero_page_show(struct kobject *kobj,
362 struct kobj_attribute *attr, char *buf)
363{
364 return single_flag_show(kobj, attr, buf,
365 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
366}
367static ssize_t use_zero_page_store(struct kobject *kobj,
368 struct kobj_attribute *attr, const char *buf, size_t count)
369{
370 return single_flag_store(kobj, attr, buf, count,
371 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
372}
373static struct kobj_attribute use_zero_page_attr =
374 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800375#ifdef CONFIG_DEBUG_VM
376static ssize_t debug_cow_show(struct kobject *kobj,
377 struct kobj_attribute *attr, char *buf)
378{
379 return single_flag_show(kobj, attr, buf,
380 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
381}
382static ssize_t debug_cow_store(struct kobject *kobj,
383 struct kobj_attribute *attr,
384 const char *buf, size_t count)
385{
386 return single_flag_store(kobj, attr, buf, count,
387 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
388}
389static struct kobj_attribute debug_cow_attr =
390 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
391#endif /* CONFIG_DEBUG_VM */
392
393static struct attribute *hugepage_attr[] = {
394 &enabled_attr.attr,
395 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800396 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800397#ifdef CONFIG_DEBUG_VM
398 &debug_cow_attr.attr,
399#endif
400 NULL,
401};
402
403static struct attribute_group hugepage_attr_group = {
404 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800405};
406
407static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
408 struct kobj_attribute *attr,
409 char *buf)
410{
411 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
412}
413
414static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
415 struct kobj_attribute *attr,
416 const char *buf, size_t count)
417{
418 unsigned long msecs;
419 int err;
420
421 err = strict_strtoul(buf, 10, &msecs);
422 if (err || msecs > UINT_MAX)
423 return -EINVAL;
424
425 khugepaged_scan_sleep_millisecs = msecs;
426 wake_up_interruptible(&khugepaged_wait);
427
428 return count;
429}
430static struct kobj_attribute scan_sleep_millisecs_attr =
431 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
432 scan_sleep_millisecs_store);
433
434static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
435 struct kobj_attribute *attr,
436 char *buf)
437{
438 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
439}
440
441static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
442 struct kobj_attribute *attr,
443 const char *buf, size_t count)
444{
445 unsigned long msecs;
446 int err;
447
448 err = strict_strtoul(buf, 10, &msecs);
449 if (err || msecs > UINT_MAX)
450 return -EINVAL;
451
452 khugepaged_alloc_sleep_millisecs = msecs;
453 wake_up_interruptible(&khugepaged_wait);
454
455 return count;
456}
457static struct kobj_attribute alloc_sleep_millisecs_attr =
458 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
459 alloc_sleep_millisecs_store);
460
461static ssize_t pages_to_scan_show(struct kobject *kobj,
462 struct kobj_attribute *attr,
463 char *buf)
464{
465 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
466}
467static ssize_t pages_to_scan_store(struct kobject *kobj,
468 struct kobj_attribute *attr,
469 const char *buf, size_t count)
470{
471 int err;
472 unsigned long pages;
473
474 err = strict_strtoul(buf, 10, &pages);
475 if (err || !pages || pages > UINT_MAX)
476 return -EINVAL;
477
478 khugepaged_pages_to_scan = pages;
479
480 return count;
481}
482static struct kobj_attribute pages_to_scan_attr =
483 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
484 pages_to_scan_store);
485
486static ssize_t pages_collapsed_show(struct kobject *kobj,
487 struct kobj_attribute *attr,
488 char *buf)
489{
490 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
491}
492static struct kobj_attribute pages_collapsed_attr =
493 __ATTR_RO(pages_collapsed);
494
495static ssize_t full_scans_show(struct kobject *kobj,
496 struct kobj_attribute *attr,
497 char *buf)
498{
499 return sprintf(buf, "%u\n", khugepaged_full_scans);
500}
501static struct kobj_attribute full_scans_attr =
502 __ATTR_RO(full_scans);
503
504static ssize_t khugepaged_defrag_show(struct kobject *kobj,
505 struct kobj_attribute *attr, char *buf)
506{
507 return single_flag_show(kobj, attr, buf,
508 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
509}
510static ssize_t khugepaged_defrag_store(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 const char *buf, size_t count)
513{
514 return single_flag_store(kobj, attr, buf, count,
515 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
516}
517static struct kobj_attribute khugepaged_defrag_attr =
518 __ATTR(defrag, 0644, khugepaged_defrag_show,
519 khugepaged_defrag_store);
520
521/*
522 * max_ptes_none controls if khugepaged should collapse hugepages over
523 * any unmapped ptes in turn potentially increasing the memory
524 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
525 * reduce the available free memory in the system as it
526 * runs. Increasing max_ptes_none will instead potentially reduce the
527 * free memory in the system during the khugepaged scan.
528 */
529static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
530 struct kobj_attribute *attr,
531 char *buf)
532{
533 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
534}
535static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 const char *buf, size_t count)
538{
539 int err;
540 unsigned long max_ptes_none;
541
542 err = strict_strtoul(buf, 10, &max_ptes_none);
543 if (err || max_ptes_none > HPAGE_PMD_NR-1)
544 return -EINVAL;
545
546 khugepaged_max_ptes_none = max_ptes_none;
547
548 return count;
549}
550static struct kobj_attribute khugepaged_max_ptes_none_attr =
551 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
552 khugepaged_max_ptes_none_store);
553
554static struct attribute *khugepaged_attr[] = {
555 &khugepaged_defrag_attr.attr,
556 &khugepaged_max_ptes_none_attr.attr,
557 &pages_to_scan_attr.attr,
558 &pages_collapsed_attr.attr,
559 &full_scans_attr.attr,
560 &scan_sleep_millisecs_attr.attr,
561 &alloc_sleep_millisecs_attr.attr,
562 NULL,
563};
564
565static struct attribute_group khugepaged_attr_group = {
566 .attrs = khugepaged_attr,
567 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800568};
Shaohua Li569e5592012-01-12 17:19:11 -0800569
570static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
571{
572 int err;
573
574 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
575 if (unlikely(!*hugepage_kobj)) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800576 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800577 return -ENOMEM;
578 }
579
580 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
581 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800582 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800583 goto delete_obj;
584 }
585
586 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
587 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800588 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800589 goto remove_hp_group;
590 }
591
592 return 0;
593
594remove_hp_group:
595 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
596delete_obj:
597 kobject_put(*hugepage_kobj);
598 return err;
599}
600
601static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
602{
603 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
604 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
605 kobject_put(hugepage_kobj);
606}
607#else
608static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
609{
610 return 0;
611}
612
613static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
614{
615}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800616#endif /* CONFIG_SYSFS */
617
618static int __init hugepage_init(void)
619{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800620 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800621 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800622
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800623 if (!has_transparent_hugepage()) {
624 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800625 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800626 }
627
Shaohua Li569e5592012-01-12 17:19:11 -0800628 err = hugepage_init_sysfs(&hugepage_kobj);
629 if (err)
630 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800631
632 err = khugepaged_slab_init();
633 if (err)
634 goto out;
635
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800636 register_shrinker(&huge_zero_page_shrinker);
637
Rik van Riel97562cd2011-01-13 15:47:12 -0800638 /*
639 * By default disable transparent hugepages on smaller systems,
640 * where the extra memory used could hurt more than TLB overhead
641 * is likely to save. The admin can still enable it through /sys.
642 */
643 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
644 transparent_hugepage_flags = 0;
645
Andrea Arcangeliba761492011-01-13 15:46:58 -0800646 start_khugepaged();
647
Shaohua Li569e5592012-01-12 17:19:11 -0800648 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800649out:
Shaohua Li569e5592012-01-12 17:19:11 -0800650 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800651 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800652}
653module_init(hugepage_init)
654
655static int __init setup_transparent_hugepage(char *str)
656{
657 int ret = 0;
658 if (!str)
659 goto out;
660 if (!strcmp(str, "always")) {
661 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
662 &transparent_hugepage_flags);
663 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
664 &transparent_hugepage_flags);
665 ret = 1;
666 } else if (!strcmp(str, "madvise")) {
667 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
668 &transparent_hugepage_flags);
669 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
670 &transparent_hugepage_flags);
671 ret = 1;
672 } else if (!strcmp(str, "never")) {
673 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
674 &transparent_hugepage_flags);
675 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
676 &transparent_hugepage_flags);
677 ret = 1;
678 }
679out:
680 if (!ret)
681 printk(KERN_WARNING
682 "transparent_hugepage= cannot parse, ignored\n");
683 return ret;
684}
685__setup("transparent_hugepage=", setup_transparent_hugepage);
686
Mel Gormanb32967f2012-11-19 12:35:47 +0000687pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800688{
689 if (likely(vma->vm_flags & VM_WRITE))
690 pmd = pmd_mkwrite(pmd);
691 return pmd;
692}
693
Bob Liub3092b32012-12-11 16:00:41 -0800694static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
695{
696 pmd_t entry;
697 entry = mk_pmd(page, vma->vm_page_prot);
698 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
699 entry = pmd_mkhuge(entry);
700 return entry;
701}
702
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800703static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
704 struct vm_area_struct *vma,
705 unsigned long haddr, pmd_t *pmd,
706 struct page *page)
707{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800708 pgtable_t pgtable;
709
710 VM_BUG_ON(!PageCompound(page));
711 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700712 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800713 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800714
715 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700716 /*
717 * The memory barrier inside __SetPageUptodate makes sure that
718 * clear_huge_page writes become visible before the set_pmd_at()
719 * write.
720 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800721 __SetPageUptodate(page);
722
723 spin_lock(&mm->page_table_lock);
724 if (unlikely(!pmd_none(*pmd))) {
725 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800726 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800727 put_page(page);
728 pte_free(mm, pgtable);
729 } else {
730 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -0800731 entry = mk_huge_pmd(page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800732 page_add_new_anon_rmap(page, vma, haddr);
733 set_pmd_at(mm, haddr, pmd, entry);
Gerald Schaefere3ebcf62012-10-08 16:30:07 -0700734 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800735 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800736 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800737 spin_unlock(&mm->page_table_lock);
738 }
739
David Rientjesaa2e8782012-05-29 15:06:17 -0700740 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800741}
742
Andi Kleencc5d4622011-03-22 16:33:13 -0700743static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800744{
Andi Kleencc5d4622011-03-22 16:33:13 -0700745 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800746}
747
748static inline struct page *alloc_hugepage_vma(int defrag,
749 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700750 unsigned long haddr, int nd,
751 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800752{
Andi Kleencc5d4622011-03-22 16:33:13 -0700753 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800754 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800755}
756
757#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800758static inline struct page *alloc_hugepage(int defrag)
759{
Andi Kleencc5d4622011-03-22 16:33:13 -0700760 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800761 HPAGE_PMD_ORDER);
762}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800763#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800764
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800765static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800766 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
767 unsigned long zero_pfn)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800768{
769 pmd_t entry;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800770 if (!pmd_none(*pmd))
771 return false;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800772 entry = pfn_pmd(zero_pfn, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800773 entry = pmd_wrprotect(entry);
774 entry = pmd_mkhuge(entry);
775 set_pmd_at(mm, haddr, pmd, entry);
776 pgtable_trans_huge_deposit(mm, pgtable);
777 mm->nr_ptes++;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800778 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800779}
780
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800781int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
782 unsigned long address, pmd_t *pmd,
783 unsigned int flags)
784{
785 struct page *page;
786 unsigned long haddr = address & HPAGE_PMD_MASK;
787 pte_t *pte;
788
789 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
790 if (unlikely(anon_vma_prepare(vma)))
791 return VM_FAULT_OOM;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800792 if (unlikely(khugepaged_enter(vma)))
793 return VM_FAULT_OOM;
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800794 if (!(flags & FAULT_FLAG_WRITE) &&
795 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800796 pgtable_t pgtable;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800797 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800798 bool set;
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800799 pgtable = pte_alloc_one(mm, haddr);
800 if (unlikely(!pgtable))
801 return VM_FAULT_OOM;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800802 zero_pfn = get_huge_zero_page();
803 if (unlikely(!zero_pfn)) {
804 pte_free(mm, pgtable);
805 count_vm_event(THP_FAULT_FALLBACK);
806 goto out;
807 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800808 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800809 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800810 zero_pfn);
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800811 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800812 if (!set) {
813 pte_free(mm, pgtable);
814 put_huge_zero_page();
815 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800816 return 0;
817 }
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800818 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700819 vma, haddr, numa_node_id(), 0);
Andi Kleen81ab4202011-04-14 15:22:06 -0700820 if (unlikely(!page)) {
821 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800822 goto out;
Andi Kleen81ab4202011-04-14 15:22:06 -0700823 }
824 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800825 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
826 put_page(page);
827 goto out;
828 }
David Rientjesedad9d22012-05-29 15:06:17 -0700829 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
830 page))) {
831 mem_cgroup_uncharge_page(page);
832 put_page(page);
833 goto out;
834 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800835
David Rientjesedad9d22012-05-29 15:06:17 -0700836 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800837 }
838out:
839 /*
840 * Use __pte_alloc instead of pte_alloc_map, because we can't
841 * run pte_offset_map on the pmd, if an huge pmd could
842 * materialize from under us from a different thread.
843 */
Mel Gorman4fd01772011-10-12 21:06:51 +0200844 if (unlikely(pmd_none(*pmd)) &&
845 unlikely(__pte_alloc(mm, vma, pmd, address)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800846 return VM_FAULT_OOM;
847 /* if an huge pmd materialized from under us just retry later */
848 if (unlikely(pmd_trans_huge(*pmd)))
849 return 0;
850 /*
851 * A regular pmd is established and it can't morph into a huge pmd
852 * from under us anymore at this point because we hold the mmap_sem
853 * read mode and khugepaged takes it in write mode. So now it's
854 * safe to run pte_offset_map().
855 */
856 pte = pte_offset_map(pmd, address);
857 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
858}
859
860int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
861 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
862 struct vm_area_struct *vma)
863{
864 struct page *src_page;
865 pmd_t pmd;
866 pgtable_t pgtable;
867 int ret;
868
869 ret = -ENOMEM;
870 pgtable = pte_alloc_one(dst_mm, addr);
871 if (unlikely(!pgtable))
872 goto out;
873
874 spin_lock(&dst_mm->page_table_lock);
875 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
876
877 ret = -EAGAIN;
878 pmd = *src_pmd;
879 if (unlikely(!pmd_trans_huge(pmd))) {
880 pte_free(dst_mm, pgtable);
881 goto out_unlock;
882 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800883 /*
884 * mm->page_table_lock is enough to be sure that huge zero pmd is not
885 * under splitting since we don't split the page itself, only pmd to
886 * a page table.
887 */
888 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800889 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800890 bool set;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800891 /*
892 * get_huge_zero_page() will never allocate a new page here,
893 * since we already have a zero page to copy. It just takes a
894 * reference.
895 */
896 zero_pfn = get_huge_zero_page();
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800897 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800898 zero_pfn);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800899 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800900 ret = 0;
901 goto out_unlock;
902 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800903 if (unlikely(pmd_trans_splitting(pmd))) {
904 /* split huge page running from under us */
905 spin_unlock(&src_mm->page_table_lock);
906 spin_unlock(&dst_mm->page_table_lock);
907 pte_free(dst_mm, pgtable);
908
909 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
910 goto out;
911 }
912 src_page = pmd_page(pmd);
913 VM_BUG_ON(!PageHead(src_page));
914 get_page(src_page);
915 page_dup_rmap(src_page);
916 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
917
918 pmdp_set_wrprotect(src_mm, addr, src_pmd);
919 pmd = pmd_mkold(pmd_wrprotect(pmd));
920 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Gerald Schaefere3ebcf62012-10-08 16:30:07 -0700921 pgtable_trans_huge_deposit(dst_mm, pgtable);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800922 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800923
924 ret = 0;
925out_unlock:
926 spin_unlock(&src_mm->page_table_lock);
927 spin_unlock(&dst_mm->page_table_lock);
928out:
929 return ret;
930}
931
Will Deacona1dd4502012-12-11 16:01:27 -0800932void huge_pmd_set_accessed(struct mm_struct *mm,
933 struct vm_area_struct *vma,
934 unsigned long address,
935 pmd_t *pmd, pmd_t orig_pmd,
936 int dirty)
937{
938 pmd_t entry;
939 unsigned long haddr;
940
941 spin_lock(&mm->page_table_lock);
942 if (unlikely(!pmd_same(*pmd, orig_pmd)))
943 goto unlock;
944
945 entry = pmd_mkyoung(orig_pmd);
946 haddr = address & HPAGE_PMD_MASK;
947 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
948 update_mmu_cache_pmd(vma, address, pmd);
949
950unlock:
951 spin_unlock(&mm->page_table_lock);
952}
953
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800954static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
955 struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800956 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800957{
958 pgtable_t pgtable;
959 pmd_t _pmd;
960 struct page *page;
961 int i, ret = 0;
962 unsigned long mmun_start; /* For mmu_notifiers */
963 unsigned long mmun_end; /* For mmu_notifiers */
964
965 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
966 if (!page) {
967 ret |= VM_FAULT_OOM;
968 goto out;
969 }
970
971 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
972 put_page(page);
973 ret |= VM_FAULT_OOM;
974 goto out;
975 }
976
977 clear_user_highpage(page, address);
978 __SetPageUptodate(page);
979
980 mmun_start = haddr;
981 mmun_end = haddr + HPAGE_PMD_SIZE;
982 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
983
984 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800985 if (unlikely(!pmd_same(*pmd, orig_pmd)))
986 goto out_free_page;
987
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800988 pmdp_clear_flush(vma, haddr, pmd);
989 /* leave pmd empty until pte is filled */
990
991 pgtable = pgtable_trans_huge_withdraw(mm);
992 pmd_populate(mm, &_pmd, pgtable);
993
994 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
995 pte_t *pte, entry;
996 if (haddr == (address & PAGE_MASK)) {
997 entry = mk_pte(page, vma->vm_page_prot);
998 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
999 page_add_new_anon_rmap(page, vma, haddr);
1000 } else {
1001 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1002 entry = pte_mkspecial(entry);
1003 }
1004 pte = pte_offset_map(&_pmd, haddr);
1005 VM_BUG_ON(!pte_none(*pte));
1006 set_pte_at(mm, haddr, pte, entry);
1007 pte_unmap(pte);
1008 }
1009 smp_wmb(); /* make pte visible before pmd */
1010 pmd_populate(mm, pmd, pgtable);
1011 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001012 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001013 inc_mm_counter(mm, MM_ANONPAGES);
1014
1015 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1016
1017 ret |= VM_FAULT_WRITE;
1018out:
1019 return ret;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001020out_free_page:
1021 spin_unlock(&mm->page_table_lock);
1022 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1023 mem_cgroup_uncharge_page(page);
1024 put_page(page);
1025 goto out;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001026}
1027
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001028static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1029 struct vm_area_struct *vma,
1030 unsigned long address,
1031 pmd_t *pmd, pmd_t orig_pmd,
1032 struct page *page,
1033 unsigned long haddr)
1034{
1035 pgtable_t pgtable;
1036 pmd_t _pmd;
1037 int ret = 0, i;
1038 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001039 unsigned long mmun_start; /* For mmu_notifiers */
1040 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001041
1042 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1043 GFP_KERNEL);
1044 if (unlikely(!pages)) {
1045 ret |= VM_FAULT_OOM;
1046 goto out;
1047 }
1048
1049 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001050 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1051 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001052 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001053 if (unlikely(!pages[i] ||
1054 mem_cgroup_newpage_charge(pages[i], mm,
1055 GFP_KERNEL))) {
1056 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001057 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001058 mem_cgroup_uncharge_start();
1059 while (--i >= 0) {
1060 mem_cgroup_uncharge_page(pages[i]);
1061 put_page(pages[i]);
1062 }
1063 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001064 kfree(pages);
1065 ret |= VM_FAULT_OOM;
1066 goto out;
1067 }
1068 }
1069
1070 for (i = 0; i < HPAGE_PMD_NR; i++) {
1071 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001072 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001073 __SetPageUptodate(pages[i]);
1074 cond_resched();
1075 }
1076
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001077 mmun_start = haddr;
1078 mmun_end = haddr + HPAGE_PMD_SIZE;
1079 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1080
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001081 spin_lock(&mm->page_table_lock);
1082 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1083 goto out_free_pages;
1084 VM_BUG_ON(!PageHead(page));
1085
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001086 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001087 /* leave pmd empty until pte is filled */
1088
Gerald Schaefere3ebcf62012-10-08 16:30:07 -07001089 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001090 pmd_populate(mm, &_pmd, pgtable);
1091
1092 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1093 pte_t *pte, entry;
1094 entry = mk_pte(pages[i], vma->vm_page_prot);
1095 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1096 page_add_new_anon_rmap(pages[i], vma, haddr);
1097 pte = pte_offset_map(&_pmd, haddr);
1098 VM_BUG_ON(!pte_none(*pte));
1099 set_pte_at(mm, haddr, pte, entry);
1100 pte_unmap(pte);
1101 }
1102 kfree(pages);
1103
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001104 smp_wmb(); /* make pte visible before pmd */
1105 pmd_populate(mm, pmd, pgtable);
1106 page_remove_rmap(page);
1107 spin_unlock(&mm->page_table_lock);
1108
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001109 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1110
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001111 ret |= VM_FAULT_WRITE;
1112 put_page(page);
1113
1114out:
1115 return ret;
1116
1117out_free_pages:
1118 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001119 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001120 mem_cgroup_uncharge_start();
1121 for (i = 0; i < HPAGE_PMD_NR; i++) {
1122 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001123 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001124 }
1125 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001126 kfree(pages);
1127 goto out;
1128}
1129
1130int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1131 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1132{
1133 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001134 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001135 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001136 unsigned long mmun_start; /* For mmu_notifiers */
1137 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001138
1139 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001140 haddr = address & HPAGE_PMD_MASK;
1141 if (is_huge_zero_pmd(orig_pmd))
1142 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001143 spin_lock(&mm->page_table_lock);
1144 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1145 goto out_unlock;
1146
1147 page = pmd_page(orig_pmd);
1148 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001149 if (page_mapcount(page) == 1) {
1150 pmd_t entry;
1151 entry = pmd_mkyoung(orig_pmd);
1152 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1153 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001154 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001155 ret |= VM_FAULT_WRITE;
1156 goto out_unlock;
1157 }
1158 get_page(page);
1159 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001160alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001161 if (transparent_hugepage_enabled(vma) &&
1162 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001163 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001164 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001165 else
1166 new_page = NULL;
1167
1168 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -07001169 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001170 if (is_huge_zero_pmd(orig_pmd)) {
1171 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001172 address, pmd, orig_pmd, haddr);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001173 } else {
1174 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1175 pmd, orig_pmd, page, haddr);
1176 if (ret & VM_FAULT_OOM)
1177 split_huge_page(page);
1178 put_page(page);
1179 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001180 goto out;
1181 }
Andi Kleen81ab4202011-04-14 15:22:06 -07001182 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001183
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001184 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1185 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001186 if (page) {
1187 split_huge_page(page);
1188 put_page(page);
1189 }
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001190 ret |= VM_FAULT_OOM;
1191 goto out;
1192 }
1193
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001194 if (is_huge_zero_pmd(orig_pmd))
1195 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1196 else
1197 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001198 __SetPageUptodate(new_page);
1199
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001200 mmun_start = haddr;
1201 mmun_end = haddr + HPAGE_PMD_SIZE;
1202 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1203
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001204 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001205 if (page)
1206 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001207 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001208 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001209 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001210 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001211 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001212 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001213 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -08001214 entry = mk_huge_pmd(new_page, vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001215 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001216 page_add_new_anon_rmap(new_page, vma, haddr);
1217 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001218 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001219 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001220 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001221 put_huge_zero_page();
1222 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001223 VM_BUG_ON(!PageHead(page));
1224 page_remove_rmap(page);
1225 put_page(page);
1226 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001227 ret |= VM_FAULT_WRITE;
1228 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001229 spin_unlock(&mm->page_table_lock);
1230out_mn:
1231 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1232out:
1233 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001234out_unlock:
1235 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001236 return ret;
1237}
1238
David Rientjesb676b292012-10-08 16:34:03 -07001239struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001240 unsigned long addr,
1241 pmd_t *pmd,
1242 unsigned int flags)
1243{
David Rientjesb676b292012-10-08 16:34:03 -07001244 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001245 struct page *page = NULL;
1246
1247 assert_spin_locked(&mm->page_table_lock);
1248
1249 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1250 goto out;
1251
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001252 /* Avoid dumping huge zero page */
1253 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1254 return ERR_PTR(-EFAULT);
1255
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001256 page = pmd_page(*pmd);
1257 VM_BUG_ON(!PageHead(page));
1258 if (flags & FOLL_TOUCH) {
1259 pmd_t _pmd;
1260 /*
1261 * We should set the dirty bit only for FOLL_WRITE but
1262 * for now the dirty bit in the pmd is meaningless.
1263 * And if the dirty bit will become meaningful and
1264 * we'll only set it with FOLL_WRITE, an atomic
1265 * set_bit will be required on the pmd to set the
1266 * young bit, instead of the current set_pmd_at.
1267 */
1268 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1269 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1270 }
David Rientjesb676b292012-10-08 16:34:03 -07001271 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1272 if (page->mapping && trylock_page(page)) {
1273 lru_add_drain();
1274 if (page->mapping)
1275 mlock_vma_page(page);
1276 unlock_page(page);
1277 }
1278 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001279 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1280 VM_BUG_ON(!PageCompound(page));
1281 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001282 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001283
1284out:
1285 return page;
1286}
1287
Mel Gormand10e63f2012-10-25 14:16:31 +02001288/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001289int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1290 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001291{
Mel Gormanb32967f2012-11-19 12:35:47 +00001292 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001293 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001294 int target_nid;
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001295 int current_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001296 bool migrated;
Mel Gormand10e63f2012-10-25 14:16:31 +02001297
1298 spin_lock(&mm->page_table_lock);
1299 if (unlikely(!pmd_same(pmd, *pmdp)))
1300 goto out_unlock;
1301
1302 page = pmd_page(pmd);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001303 get_page(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001304 current_nid = page_to_nid(page);
1305 count_vm_numa_event(NUMA_HINT_FAULTS);
1306 if (current_nid == numa_node_id())
1307 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001308
1309 target_nid = mpol_misplaced(page, vma, haddr);
Mel Gormanb32967f2012-11-19 12:35:47 +00001310 if (target_nid == -1) {
1311 put_page(page);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001312 goto clear_pmdnuma;
Mel Gormanb32967f2012-11-19 12:35:47 +00001313 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001314
Mel Gormanb32967f2012-11-19 12:35:47 +00001315 /* Acquire the page lock to serialise THP migrations */
1316 spin_unlock(&mm->page_table_lock);
1317 lock_page(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001318
Mel Gormanb32967f2012-11-19 12:35:47 +00001319 /* Confirm the PTE did not while locked */
1320 spin_lock(&mm->page_table_lock);
1321 if (unlikely(!pmd_same(pmd, *pmdp))) {
1322 unlock_page(page);
1323 put_page(page);
1324 goto out_unlock;
1325 }
1326 spin_unlock(&mm->page_table_lock);
1327
1328 /* Migrate the THP to the requested node */
1329 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001330 pmdp, pmd, addr, page, target_nid);
1331 if (!migrated)
1332 goto check_same;
Mel Gormanb32967f2012-11-19 12:35:47 +00001333
Hugh Dickins340ef392013-02-22 16:34:33 -08001334 task_numa_fault(target_nid, HPAGE_PMD_NR, true);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001335 return 0;
1336
Hugh Dickins340ef392013-02-22 16:34:33 -08001337check_same:
1338 spin_lock(&mm->page_table_lock);
1339 if (unlikely(!pmd_same(pmd, *pmdp)))
1340 goto out_unlock;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001341clear_pmdnuma:
Mel Gormand10e63f2012-10-25 14:16:31 +02001342 pmd = pmd_mknonnuma(pmd);
1343 set_pmd_at(mm, haddr, pmdp, pmd);
1344 VM_BUG_ON(pmd_numa(*pmdp));
1345 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001346out_unlock:
1347 spin_unlock(&mm->page_table_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001348 if (current_nid != -1)
Hugh Dickins340ef392013-02-22 16:34:33 -08001349 task_numa_fault(current_nid, HPAGE_PMD_NR, false);
Mel Gormand10e63f2012-10-25 14:16:31 +02001350 return 0;
1351}
1352
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001353int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001354 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001355{
1356 int ret = 0;
1357
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001358 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1359 struct page *page;
1360 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001361 pmd_t orig_pmd;
Gerald Schaefere3ebcf62012-10-08 16:30:07 -07001362 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
David Millerf5c8ad42012-10-08 16:34:26 -07001363 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001364 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001365 if (is_huge_zero_pmd(orig_pmd)) {
1366 tlb->mm->nr_ptes--;
1367 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001368 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001369 } else {
1370 page = pmd_page(orig_pmd);
1371 page_remove_rmap(page);
1372 VM_BUG_ON(page_mapcount(page) < 0);
1373 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1374 VM_BUG_ON(!PageHead(page));
1375 tlb->mm->nr_ptes--;
1376 spin_unlock(&tlb->mm->page_table_lock);
1377 tlb_remove_page(tlb, page);
1378 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001379 pte_free(tlb->mm, pgtable);
1380 ret = 1;
1381 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001382 return ret;
1383}
1384
Johannes Weiner0ca16342011-01-13 15:47:02 -08001385int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1386 unsigned long addr, unsigned long end,
1387 unsigned char *vec)
1388{
1389 int ret = 0;
1390
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001391 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1392 /*
1393 * All logical pages in the range are present
1394 * if backed by a huge page.
1395 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001396 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001397 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1398 ret = 1;
1399 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001400
1401 return ret;
1402}
1403
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001404int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1405 unsigned long old_addr,
1406 unsigned long new_addr, unsigned long old_end,
1407 pmd_t *old_pmd, pmd_t *new_pmd)
1408{
1409 int ret = 0;
1410 pmd_t pmd;
1411
1412 struct mm_struct *mm = vma->vm_mm;
1413
1414 if ((old_addr & ~HPAGE_PMD_MASK) ||
1415 (new_addr & ~HPAGE_PMD_MASK) ||
1416 old_end - old_addr < HPAGE_PMD_SIZE ||
1417 (new_vma->vm_flags & VM_NOHUGEPAGE))
1418 goto out;
1419
1420 /*
1421 * The destination pmd shouldn't be established, free_pgtables()
1422 * should have release it.
1423 */
1424 if (WARN_ON(!pmd_none(*new_pmd))) {
1425 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1426 goto out;
1427 }
1428
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001429 ret = __pmd_trans_huge_lock(old_pmd, vma);
1430 if (ret == 1) {
1431 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1432 VM_BUG_ON(!pmd_none(*new_pmd));
1433 set_pmd_at(mm, new_addr, new_pmd, pmd);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001434 spin_unlock(&mm->page_table_lock);
1435 }
1436out:
1437 return ret;
1438}
1439
Johannes Weinercd7548a2011-01-13 15:47:04 -08001440int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001441 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001442{
1443 struct mm_struct *mm = vma->vm_mm;
1444 int ret = 0;
1445
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001446 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1447 pmd_t entry;
1448 entry = pmdp_get_and_clear(mm, addr, pmd);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001449 if (!prot_numa) {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001450 entry = pmd_modify(entry, newprot);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001451 BUG_ON(pmd_write(entry));
1452 } else {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001453 struct page *page = pmd_page(*pmd);
1454
1455 /* only check non-shared pages */
1456 if (page_mapcount(page) == 1 &&
1457 !pmd_numa(*pmd)) {
1458 entry = pmd_mknuma(entry);
1459 }
1460 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001461 set_pmd_at(mm, addr, pmd, entry);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001462 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001463 ret = 1;
1464 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001465
1466 return ret;
1467}
1468
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001469/*
1470 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1471 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1472 *
1473 * Note that if it returns 1, this routine returns without unlocking page
1474 * table locks. So callers must unlock them.
1475 */
1476int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1477{
1478 spin_lock(&vma->vm_mm->page_table_lock);
1479 if (likely(pmd_trans_huge(*pmd))) {
1480 if (unlikely(pmd_trans_splitting(*pmd))) {
1481 spin_unlock(&vma->vm_mm->page_table_lock);
1482 wait_split_huge_page(vma->anon_vma, pmd);
1483 return -1;
1484 } else {
1485 /* Thp mapped by 'pmd' is stable, so we can
1486 * handle it as it is. */
1487 return 1;
1488 }
1489 }
1490 spin_unlock(&vma->vm_mm->page_table_lock);
1491 return 0;
1492}
1493
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001494pmd_t *page_check_address_pmd(struct page *page,
1495 struct mm_struct *mm,
1496 unsigned long address,
1497 enum page_check_address_pmd_flag flag)
1498{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001499 pmd_t *pmd, *ret = NULL;
1500
1501 if (address & ~HPAGE_PMD_MASK)
1502 goto out;
1503
Bob Liu62190492012-12-11 16:00:37 -08001504 pmd = mm_find_pmd(mm, address);
1505 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001506 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001507 if (pmd_none(*pmd))
1508 goto out;
1509 if (pmd_page(*pmd) != page)
1510 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001511 /*
1512 * split_vma() may create temporary aliased mappings. There is
1513 * no risk as long as all huge pmd are found and have their
1514 * splitting bit set before __split_huge_page_refcount
1515 * runs. Finding the same huge pmd more than once during the
1516 * same rmap walk is not a problem.
1517 */
1518 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1519 pmd_trans_splitting(*pmd))
1520 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001521 if (pmd_trans_huge(*pmd)) {
1522 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1523 !pmd_trans_splitting(*pmd));
1524 ret = pmd;
1525 }
1526out:
1527 return ret;
1528}
1529
1530static int __split_huge_page_splitting(struct page *page,
1531 struct vm_area_struct *vma,
1532 unsigned long address)
1533{
1534 struct mm_struct *mm = vma->vm_mm;
1535 pmd_t *pmd;
1536 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001537 /* For mmu_notifiers */
1538 const unsigned long mmun_start = address;
1539 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001540
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001541 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001542 spin_lock(&mm->page_table_lock);
1543 pmd = page_check_address_pmd(page, mm, address,
1544 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1545 if (pmd) {
1546 /*
1547 * We can't temporarily set the pmd to null in order
1548 * to split it, the pmd must remain marked huge at all
1549 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001550 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001551 * serialize against split_huge_page*.
1552 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001553 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001554 ret = 1;
1555 }
1556 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001557 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001558
1559 return ret;
1560}
1561
1562static void __split_huge_page_refcount(struct page *page)
1563{
1564 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001565 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001566 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001567 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001568
1569 /* prevent PageLRU to go away from under us, and freeze lru stats */
1570 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001571 lruvec = mem_cgroup_page_lruvec(page, zone);
1572
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001573 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001574 /* complete memcg works before add pages to LRU */
1575 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001576
Shaohua Li45676882012-01-12 17:19:18 -08001577 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001578 struct page *page_tail = page + i;
1579
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001580 /* tail_page->_mapcount cannot change */
1581 BUG_ON(page_mapcount(page_tail) < 0);
1582 tail_count += page_mapcount(page_tail);
1583 /* check for overflow */
1584 BUG_ON(tail_count < 0);
1585 BUG_ON(atomic_read(&page_tail->_count) != 0);
1586 /*
1587 * tail_page->_count is zero and not changing from
1588 * under us. But get_page_unless_zero() may be running
1589 * from under us on the tail_page. If we used
1590 * atomic_set() below instead of atomic_add(), we
1591 * would then run atomic_set() concurrently with
1592 * get_page_unless_zero(), and atomic_set() is
1593 * implemented in C not using locked ops. spin_unlock
1594 * on x86 sometime uses locked ops because of PPro
1595 * errata 66, 92, so unless somebody can guarantee
1596 * atomic_set() here would be safe on all archs (and
1597 * not only on x86), it's safer to use atomic_add().
1598 */
1599 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1600 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001601
1602 /* after clearing PageTail the gup refcount can be released */
1603 smp_mb();
1604
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001605 /*
1606 * retain hwpoison flag of the poisoned tail page:
1607 * fix for the unsuitable process killed on Guest Machine(KVM)
1608 * by the memory-failure.
1609 */
1610 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001611 page_tail->flags |= (page->flags &
1612 ((1L << PG_referenced) |
1613 (1L << PG_swapbacked) |
1614 (1L << PG_mlocked) |
1615 (1L << PG_uptodate)));
1616 page_tail->flags |= (1L << PG_dirty);
1617
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001618 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001619 smp_wmb();
1620
1621 /*
1622 * __split_huge_page_splitting() already set the
1623 * splitting bit in all pmd that could map this
1624 * hugepage, that will ensure no CPU can alter the
1625 * mapcount on the head page. The mapcount is only
1626 * accounted in the head page and it has to be
1627 * transferred to all tail pages in the below code. So
1628 * for this code to be safe, the split the mapcount
1629 * can't change. But that doesn't mean userland can't
1630 * keep changing and reading the page contents while
1631 * we transfer the mapcount, so the pmd splitting
1632 * status is achieved setting a reserved bit in the
1633 * pmd, not by clearing the present bit.
1634 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001635 page_tail->_mapcount = page->_mapcount;
1636
1637 BUG_ON(page_tail->mapping);
1638 page_tail->mapping = page->mapping;
1639
Shaohua Li45676882012-01-12 17:19:18 -08001640 page_tail->index = page->index + i;
Mel Gorman22b751c2013-02-22 16:34:59 -08001641 page_nid_xchg_last(page_tail, page_nid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001642
1643 BUG_ON(!PageAnon(page_tail));
1644 BUG_ON(!PageUptodate(page_tail));
1645 BUG_ON(!PageDirty(page_tail));
1646 BUG_ON(!PageSwapBacked(page_tail));
1647
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001648 lru_add_page_tail(page, page_tail, lruvec);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001649 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001650 atomic_sub(tail_count, &page->_count);
1651 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001652
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001653 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001654 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1655
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001656 ClearPageCompound(page);
1657 compound_unlock(page);
1658 spin_unlock_irq(&zone->lru_lock);
1659
1660 for (i = 1; i < HPAGE_PMD_NR; i++) {
1661 struct page *page_tail = page + i;
1662 BUG_ON(page_count(page_tail) <= 0);
1663 /*
1664 * Tail pages may be freed if there wasn't any mapping
1665 * like if add_to_swap() is running on a lru page that
1666 * had its mapping zapped. And freeing these pages
1667 * requires taking the lru_lock so we do the put_page
1668 * of the tail pages after the split is complete.
1669 */
1670 put_page(page_tail);
1671 }
1672
1673 /*
1674 * Only the head page (now become a regular page) is required
1675 * to be pinned by the caller.
1676 */
1677 BUG_ON(page_count(page) <= 0);
1678}
1679
1680static int __split_huge_page_map(struct page *page,
1681 struct vm_area_struct *vma,
1682 unsigned long address)
1683{
1684 struct mm_struct *mm = vma->vm_mm;
1685 pmd_t *pmd, _pmd;
1686 int ret = 0, i;
1687 pgtable_t pgtable;
1688 unsigned long haddr;
1689
1690 spin_lock(&mm->page_table_lock);
1691 pmd = page_check_address_pmd(page, mm, address,
1692 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1693 if (pmd) {
Gerald Schaefere3ebcf62012-10-08 16:30:07 -07001694 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001695 pmd_populate(mm, &_pmd, pgtable);
1696
Gerald Schaefere3ebcf62012-10-08 16:30:07 -07001697 haddr = address;
1698 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001699 pte_t *pte, entry;
1700 BUG_ON(PageCompound(page+i));
1701 entry = mk_pte(page + i, vma->vm_page_prot);
1702 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1703 if (!pmd_write(*pmd))
1704 entry = pte_wrprotect(entry);
1705 else
1706 BUG_ON(page_mapcount(page) != 1);
1707 if (!pmd_young(*pmd))
1708 entry = pte_mkold(entry);
Andrea Arcangeli1ba6e0b2012-10-04 01:51:06 +02001709 if (pmd_numa(*pmd))
1710 entry = pte_mknuma(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001711 pte = pte_offset_map(&_pmd, haddr);
1712 BUG_ON(!pte_none(*pte));
1713 set_pte_at(mm, haddr, pte, entry);
1714 pte_unmap(pte);
1715 }
1716
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001717 smp_wmb(); /* make pte visible before pmd */
1718 /*
1719 * Up to this point the pmd is present and huge and
1720 * userland has the whole access to the hugepage
1721 * during the split (which happens in place). If we
1722 * overwrite the pmd with the not-huge version
1723 * pointing to the pte here (which of course we could
1724 * if all CPUs were bug free), userland could trigger
1725 * a small page size TLB miss on the small sized TLB
1726 * while the hugepage TLB entry is still established
1727 * in the huge TLB. Some CPU doesn't like that. See
1728 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1729 * Erratum 383 on page 93. Intel should be safe but is
1730 * also warns that it's only safe if the permission
1731 * and cache attributes of the two entries loaded in
1732 * the two TLB is identical (which should be the case
1733 * here). But it is generally safer to never allow
1734 * small and huge TLB entries for the same virtual
1735 * address to be loaded simultaneously. So instead of
1736 * doing "pmd_populate(); flush_tlb_range();" we first
1737 * mark the current pmd notpresent (atomically because
1738 * here the pmd_trans_huge and pmd_trans_splitting
1739 * must remain set at all times on the pmd until the
1740 * split is complete for this pmd), then we flush the
1741 * SMP TLB and finally we write the non-huge version
1742 * of the pmd entry with pmd_populate.
1743 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001744 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001745 pmd_populate(mm, pmd, pgtable);
1746 ret = 1;
1747 }
1748 spin_unlock(&mm->page_table_lock);
1749
1750 return ret;
1751}
1752
Ingo Molnar5a505082012-12-02 19:56:46 +00001753/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001754static void __split_huge_page(struct page *page,
1755 struct anon_vma *anon_vma)
1756{
1757 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001758 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001759 struct anon_vma_chain *avc;
1760
1761 BUG_ON(!PageHead(page));
1762 BUG_ON(PageTail(page));
1763
1764 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001765 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001766 struct vm_area_struct *vma = avc->vma;
1767 unsigned long addr = vma_address(page, vma);
1768 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001769 mapcount += __split_huge_page_splitting(page, vma, addr);
1770 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001771 /*
1772 * It is critical that new vmas are added to the tail of the
1773 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1774 * and establishes a child pmd before
1775 * __split_huge_page_splitting() freezes the parent pmd (so if
1776 * we fail to prevent copy_huge_pmd() from running until the
1777 * whole __split_huge_page() is complete), we will still see
1778 * the newly established pmd of the child later during the
1779 * walk, to be able to set it as pmd_trans_splitting too.
1780 */
1781 if (mapcount != page_mapcount(page))
1782 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1783 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001784 BUG_ON(mapcount != page_mapcount(page));
1785
1786 __split_huge_page_refcount(page);
1787
1788 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001789 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001790 struct vm_area_struct *vma = avc->vma;
1791 unsigned long addr = vma_address(page, vma);
1792 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001793 mapcount2 += __split_huge_page_map(page, vma, addr);
1794 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001795 if (mapcount != mapcount2)
1796 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1797 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001798 BUG_ON(mapcount != mapcount2);
1799}
1800
1801int split_huge_page(struct page *page)
1802{
1803 struct anon_vma *anon_vma;
1804 int ret = 1;
1805
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08001806 BUG_ON(is_huge_zero_pfn(page_to_pfn(page)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001807 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001808
1809 /*
1810 * The caller does not necessarily hold an mmap_sem that would prevent
1811 * the anon_vma disappearing so we first we take a reference to it
1812 * and then lock the anon_vma for write. This is similar to
1813 * page_lock_anon_vma_read except the write lock is taken to serialise
1814 * against parallel split or collapse operations.
1815 */
1816 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001817 if (!anon_vma)
1818 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001819 anon_vma_lock_write(anon_vma);
1820
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001821 ret = 0;
1822 if (!PageCompound(page))
1823 goto out_unlock;
1824
1825 BUG_ON(!PageSwapBacked(page));
1826 __split_huge_page(page, anon_vma);
Andi Kleen81ab4202011-04-14 15:22:06 -07001827 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001828
1829 BUG_ON(PageCompound(page));
1830out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08001831 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08001832 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001833out:
1834 return ret;
1835}
1836
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001837#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001838
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001839int hugepage_madvise(struct vm_area_struct *vma,
1840 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001841{
Gerald Schaefer8e720332012-10-08 16:30:12 -07001842 struct mm_struct *mm = vma->vm_mm;
1843
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001844 switch (advice) {
1845 case MADV_HUGEPAGE:
1846 /*
1847 * Be somewhat over-protective like KSM for now!
1848 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001849 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001850 return -EINVAL;
Gerald Schaefer8e720332012-10-08 16:30:12 -07001851 if (mm->def_flags & VM_NOHUGEPAGE)
1852 return -EINVAL;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001853 *vm_flags &= ~VM_NOHUGEPAGE;
1854 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001855 /*
1856 * If the vma become good for khugepaged to scan,
1857 * register it here without waiting a page fault that
1858 * may not happen any time soon.
1859 */
1860 if (unlikely(khugepaged_enter_vma_merge(vma)))
1861 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001862 break;
1863 case MADV_NOHUGEPAGE:
1864 /*
1865 * Be somewhat over-protective like KSM for now!
1866 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001867 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001868 return -EINVAL;
1869 *vm_flags &= ~VM_HUGEPAGE;
1870 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001871 /*
1872 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1873 * this vma even if we leave the mm registered in khugepaged if
1874 * it got registered before VM_NOHUGEPAGE was set.
1875 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001876 break;
1877 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001878
1879 return 0;
1880}
1881
Andrea Arcangeliba761492011-01-13 15:46:58 -08001882static int __init khugepaged_slab_init(void)
1883{
1884 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1885 sizeof(struct mm_slot),
1886 __alignof__(struct mm_slot), 0, NULL);
1887 if (!mm_slot_cache)
1888 return -ENOMEM;
1889
1890 return 0;
1891}
1892
Andrea Arcangeliba761492011-01-13 15:46:58 -08001893static inline struct mm_slot *alloc_mm_slot(void)
1894{
1895 if (!mm_slot_cache) /* initialization failed */
1896 return NULL;
1897 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1898}
1899
1900static inline void free_mm_slot(struct mm_slot *mm_slot)
1901{
1902 kmem_cache_free(mm_slot_cache, mm_slot);
1903}
1904
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1906{
1907 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001908
Sasha Levinb67bfe02013-02-27 17:06:00 -08001909 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001910 if (mm == mm_slot->mm)
1911 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001912
Andrea Arcangeliba761492011-01-13 15:46:58 -08001913 return NULL;
1914}
1915
1916static void insert_to_mm_slots_hash(struct mm_struct *mm,
1917 struct mm_slot *mm_slot)
1918{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001919 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001920 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001921}
1922
1923static inline int khugepaged_test_exit(struct mm_struct *mm)
1924{
1925 return atomic_read(&mm->mm_users) == 0;
1926}
1927
1928int __khugepaged_enter(struct mm_struct *mm)
1929{
1930 struct mm_slot *mm_slot;
1931 int wakeup;
1932
1933 mm_slot = alloc_mm_slot();
1934 if (!mm_slot)
1935 return -ENOMEM;
1936
1937 /* __khugepaged_exit() must not run from under us */
1938 VM_BUG_ON(khugepaged_test_exit(mm));
1939 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1940 free_mm_slot(mm_slot);
1941 return 0;
1942 }
1943
1944 spin_lock(&khugepaged_mm_lock);
1945 insert_to_mm_slots_hash(mm, mm_slot);
1946 /*
1947 * Insert just behind the scanning cursor, to let the area settle
1948 * down a little.
1949 */
1950 wakeup = list_empty(&khugepaged_scan.mm_head);
1951 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1952 spin_unlock(&khugepaged_mm_lock);
1953
1954 atomic_inc(&mm->mm_count);
1955 if (wakeup)
1956 wake_up_interruptible(&khugepaged_wait);
1957
1958 return 0;
1959}
1960
1961int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1962{
1963 unsigned long hstart, hend;
1964 if (!vma->anon_vma)
1965 /*
1966 * Not yet faulted in so we will register later in the
1967 * page fault if needed.
1968 */
1969 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001970 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001971 /* khugepaged not yet working on file or special mappings */
1972 return 0;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001973 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001974 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1975 hend = vma->vm_end & HPAGE_PMD_MASK;
1976 if (hstart < hend)
1977 return khugepaged_enter(vma);
1978 return 0;
1979}
1980
1981void __khugepaged_exit(struct mm_struct *mm)
1982{
1983 struct mm_slot *mm_slot;
1984 int free = 0;
1985
1986 spin_lock(&khugepaged_mm_lock);
1987 mm_slot = get_mm_slot(mm);
1988 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001989 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001990 list_del(&mm_slot->mm_node);
1991 free = 1;
1992 }
Chris Wrightd788e802011-07-25 17:12:14 -07001993 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001994
1995 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001996 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1997 free_mm_slot(mm_slot);
1998 mmdrop(mm);
1999 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002000 /*
2001 * This is required to serialize against
2002 * khugepaged_test_exit() (which is guaranteed to run
2003 * under mmap sem read mode). Stop here (after we
2004 * return all pagetables will be destroyed) until
2005 * khugepaged has finished working on the pagetables
2006 * under the mmap_sem.
2007 */
2008 down_write(&mm->mmap_sem);
2009 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002010 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002011}
2012
2013static void release_pte_page(struct page *page)
2014{
2015 /* 0 stands for page_is_file_cache(page) == false */
2016 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2017 unlock_page(page);
2018 putback_lru_page(page);
2019}
2020
2021static void release_pte_pages(pte_t *pte, pte_t *_pte)
2022{
2023 while (--_pte >= pte) {
2024 pte_t pteval = *_pte;
2025 if (!pte_none(pteval))
2026 release_pte_page(pte_page(pteval));
2027 }
2028}
2029
Andrea Arcangeliba761492011-01-13 15:46:58 -08002030static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2031 unsigned long address,
2032 pte_t *pte)
2033{
2034 struct page *page;
2035 pte_t *_pte;
Bob Liu344aa352012-12-11 16:00:34 -08002036 int referenced = 0, none = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002037 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2038 _pte++, address += PAGE_SIZE) {
2039 pte_t pteval = *_pte;
2040 if (pte_none(pteval)) {
2041 if (++none <= khugepaged_max_ptes_none)
2042 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002043 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002044 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002045 }
Bob Liu344aa352012-12-11 16:00:34 -08002046 if (!pte_present(pteval) || !pte_write(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002047 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002049 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002050 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002051
Andrea Arcangeliba761492011-01-13 15:46:58 -08002052 VM_BUG_ON(PageCompound(page));
2053 BUG_ON(!PageAnon(page));
2054 VM_BUG_ON(!PageSwapBacked(page));
2055
2056 /* cannot use mapcount: can't collapse if there's a gup pin */
Bob Liu344aa352012-12-11 16:00:34 -08002057 if (page_count(page) != 1)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002058 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002059 /*
2060 * We can do it before isolate_lru_page because the
2061 * page can't be freed from under us. NOTE: PG_lock
2062 * is needed to serialize against split_huge_page
2063 * when invoked from the VM.
2064 */
Bob Liu344aa352012-12-11 16:00:34 -08002065 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002066 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002067 /*
2068 * Isolate the page to avoid collapsing an hugepage
2069 * currently in use by the VM.
2070 */
2071 if (isolate_lru_page(page)) {
2072 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002073 goto out;
2074 }
2075 /* 0 stands for page_is_file_cache(page) == false */
2076 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2077 VM_BUG_ON(!PageLocked(page));
2078 VM_BUG_ON(PageLRU(page));
2079
2080 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002081 if (pte_young(pteval) || PageReferenced(page) ||
2082 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002083 referenced = 1;
2084 }
Bob Liu344aa352012-12-11 16:00:34 -08002085 if (likely(referenced))
2086 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002087out:
Bob Liu344aa352012-12-11 16:00:34 -08002088 release_pte_pages(pte, _pte);
2089 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002090}
2091
2092static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2093 struct vm_area_struct *vma,
2094 unsigned long address,
2095 spinlock_t *ptl)
2096{
2097 pte_t *_pte;
2098 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2099 pte_t pteval = *_pte;
2100 struct page *src_page;
2101
2102 if (pte_none(pteval)) {
2103 clear_user_highpage(page, address);
2104 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2105 } else {
2106 src_page = pte_page(pteval);
2107 copy_user_highpage(page, src_page, address, vma);
2108 VM_BUG_ON(page_mapcount(src_page) != 1);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002109 release_pte_page(src_page);
2110 /*
2111 * ptl mostly unnecessary, but preempt has to
2112 * be disabled to update the per-cpu stats
2113 * inside page_remove_rmap().
2114 */
2115 spin_lock(ptl);
2116 /*
2117 * paravirt calls inside pte_clear here are
2118 * superfluous.
2119 */
2120 pte_clear(vma->vm_mm, address, _pte);
2121 page_remove_rmap(src_page);
2122 spin_unlock(ptl);
2123 free_page_and_swap_cache(src_page);
2124 }
2125
2126 address += PAGE_SIZE;
2127 page++;
2128 }
2129}
2130
Xiao Guangrong26234f32012-10-08 16:29:51 -07002131static void khugepaged_alloc_sleep(void)
2132{
2133 wait_event_freezable_timeout(khugepaged_wait, false,
2134 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2135}
2136
2137#ifdef CONFIG_NUMA
2138static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2139{
2140 if (IS_ERR(*hpage)) {
2141 if (!*wait)
2142 return false;
2143
2144 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002145 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002146 khugepaged_alloc_sleep();
2147 } else if (*hpage) {
2148 put_page(*hpage);
2149 *hpage = NULL;
2150 }
2151
2152 return true;
2153}
2154
2155static struct page
2156*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2157 struct vm_area_struct *vma, unsigned long address,
2158 int node)
2159{
2160 VM_BUG_ON(*hpage);
2161 /*
2162 * Allocate the page while the vma is still valid and under
2163 * the mmap_sem read mode so there is no memory allocation
2164 * later when we take the mmap_sem in write mode. This is more
2165 * friendly behavior (OTOH it may actually hide bugs) to
2166 * filesystems in userland with daemons allocating memory in
2167 * the userland I/O paths. Allocating memory with the
2168 * mmap_sem in read mode is good idea also to allow greater
2169 * scalability.
2170 */
2171 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2172 node, __GFP_OTHER_NODE);
2173
2174 /*
2175 * After allocating the hugepage, release the mmap_sem read lock in
2176 * preparation for taking it in write mode.
2177 */
2178 up_read(&mm->mmap_sem);
2179 if (unlikely(!*hpage)) {
2180 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2181 *hpage = ERR_PTR(-ENOMEM);
2182 return NULL;
2183 }
2184
2185 count_vm_event(THP_COLLAPSE_ALLOC);
2186 return *hpage;
2187}
2188#else
2189static struct page *khugepaged_alloc_hugepage(bool *wait)
2190{
2191 struct page *hpage;
2192
2193 do {
2194 hpage = alloc_hugepage(khugepaged_defrag());
2195 if (!hpage) {
2196 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2197 if (!*wait)
2198 return NULL;
2199
2200 *wait = false;
2201 khugepaged_alloc_sleep();
2202 } else
2203 count_vm_event(THP_COLLAPSE_ALLOC);
2204 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2205
2206 return hpage;
2207}
2208
2209static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2210{
2211 if (!*hpage)
2212 *hpage = khugepaged_alloc_hugepage(wait);
2213
2214 if (unlikely(!*hpage))
2215 return false;
2216
2217 return true;
2218}
2219
2220static struct page
2221*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2222 struct vm_area_struct *vma, unsigned long address,
2223 int node)
2224{
2225 up_read(&mm->mmap_sem);
2226 VM_BUG_ON(!*hpage);
2227 return *hpage;
2228}
2229#endif
2230
Bob Liufa475e52012-12-11 16:00:39 -08002231static bool hugepage_vma_check(struct vm_area_struct *vma)
2232{
2233 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2234 (vma->vm_flags & VM_NOHUGEPAGE))
2235 return false;
2236
2237 if (!vma->anon_vma || vma->vm_ops)
2238 return false;
2239 if (is_vma_temporary_stack(vma))
2240 return false;
2241 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2242 return true;
2243}
2244
Andrea Arcangeliba761492011-01-13 15:46:58 -08002245static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002246 unsigned long address,
2247 struct page **hpage,
2248 struct vm_area_struct *vma,
2249 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002250{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002251 pmd_t *pmd, _pmd;
2252 pte_t *pte;
2253 pgtable_t pgtable;
2254 struct page *new_page;
2255 spinlock_t *ptl;
2256 int isolated;
2257 unsigned long hstart, hend;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002258 unsigned long mmun_start; /* For mmu_notifiers */
2259 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002260
2261 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002262
Xiao Guangrong26234f32012-10-08 16:29:51 -07002263 /* release the mmap_sem read lock. */
2264 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2265 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002266 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002267
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002268 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002269 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002270
2271 /*
2272 * Prevent all access to pagetables with the exception of
2273 * gup_fast later hanlded by the ptep_clear_flush and the VM
2274 * handled by the anon_vma lock + PG_lock.
2275 */
2276 down_write(&mm->mmap_sem);
2277 if (unlikely(khugepaged_test_exit(mm)))
2278 goto out;
2279
2280 vma = find_vma(mm, address);
2281 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2282 hend = vma->vm_end & HPAGE_PMD_MASK;
2283 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2284 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002285 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002286 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002287 pmd = mm_find_pmd(mm, address);
2288 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002289 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002290 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002291 goto out;
2292
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002293 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002294
2295 pte = pte_offset_map(pmd, address);
2296 ptl = pte_lockptr(mm, pmd);
2297
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002298 mmun_start = address;
2299 mmun_end = address + HPAGE_PMD_SIZE;
2300 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002301 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2302 /*
2303 * After this gup_fast can't run anymore. This also removes
2304 * any huge TLB entry from the CPU so we won't allow
2305 * huge and small TLB entries for the same virtual address
2306 * to avoid the risk of CPU bugs in that area.
2307 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002308 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002309 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002310 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002311
2312 spin_lock(ptl);
2313 isolated = __collapse_huge_page_isolate(vma, address, pte);
2314 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002315
2316 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002317 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002318 spin_lock(&mm->page_table_lock);
2319 BUG_ON(!pmd_none(*pmd));
2320 set_pmd_at(mm, address, pmd, _pmd);
2321 spin_unlock(&mm->page_table_lock);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002322 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002323 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002324 }
2325
2326 /*
2327 * All pages are isolated and locked so anon_vma rmap
2328 * can't run anymore.
2329 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002330 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331
2332 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002333 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002334 __SetPageUptodate(new_page);
2335 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002336
Bob Liub3092b32012-12-11 16:00:41 -08002337 _pmd = mk_huge_pmd(new_page, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002338
2339 /*
2340 * spin_lock() below is not the equivalent of smp_wmb(), so
2341 * this is needed to avoid the copy_huge_page writes to become
2342 * visible after the set_pmd_at() write.
2343 */
2344 smp_wmb();
2345
2346 spin_lock(&mm->page_table_lock);
2347 BUG_ON(!pmd_none(*pmd));
2348 page_add_new_anon_rmap(new_page, vma, address);
2349 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002350 update_mmu_cache_pmd(vma, address, pmd);
Gerald Schaefere3ebcf62012-10-08 16:30:07 -07002351 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002352 spin_unlock(&mm->page_table_lock);
2353
2354 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002355
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002357out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002359 return;
2360
Andrea Arcangelice83d212011-01-13 15:47:06 -08002361out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002362 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002363 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002364}
2365
2366static int khugepaged_scan_pmd(struct mm_struct *mm,
2367 struct vm_area_struct *vma,
2368 unsigned long address,
2369 struct page **hpage)
2370{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002371 pmd_t *pmd;
2372 pte_t *pte, *_pte;
2373 int ret = 0, referenced = 0, none = 0;
2374 struct page *page;
2375 unsigned long _address;
2376 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002377 int node = NUMA_NO_NODE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002378
2379 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2380
Bob Liu62190492012-12-11 16:00:37 -08002381 pmd = mm_find_pmd(mm, address);
2382 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002383 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002384 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002385 goto out;
2386
2387 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2388 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2389 _pte++, _address += PAGE_SIZE) {
2390 pte_t pteval = *_pte;
2391 if (pte_none(pteval)) {
2392 if (++none <= khugepaged_max_ptes_none)
2393 continue;
2394 else
2395 goto out_unmap;
2396 }
2397 if (!pte_present(pteval) || !pte_write(pteval))
2398 goto out_unmap;
2399 page = vm_normal_page(vma, _address, pteval);
2400 if (unlikely(!page))
2401 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002402 /*
2403 * Chose the node of the first page. This could
2404 * be more sophisticated and look at more pages,
2405 * but isn't for now.
2406 */
David Rientjes00ef2d22013-02-22 16:35:36 -08002407 if (node == NUMA_NO_NODE)
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002408 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002409 VM_BUG_ON(PageCompound(page));
2410 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2411 goto out_unmap;
2412 /* cannot use mapcount: can't collapse if there's a gup pin */
2413 if (page_count(page) != 1)
2414 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002415 if (pte_young(pteval) || PageReferenced(page) ||
2416 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002417 referenced = 1;
2418 }
2419 if (referenced)
2420 ret = 1;
2421out_unmap:
2422 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002423 if (ret)
2424 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002425 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002426out:
2427 return ret;
2428}
2429
2430static void collect_mm_slot(struct mm_slot *mm_slot)
2431{
2432 struct mm_struct *mm = mm_slot->mm;
2433
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002434 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002435
2436 if (khugepaged_test_exit(mm)) {
2437 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002438 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002439 list_del(&mm_slot->mm_node);
2440
2441 /*
2442 * Not strictly needed because the mm exited already.
2443 *
2444 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2445 */
2446
2447 /* khugepaged_mm_lock actually not necessary for the below */
2448 free_mm_slot(mm_slot);
2449 mmdrop(mm);
2450 }
2451}
2452
2453static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2454 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002455 __releases(&khugepaged_mm_lock)
2456 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002457{
2458 struct mm_slot *mm_slot;
2459 struct mm_struct *mm;
2460 struct vm_area_struct *vma;
2461 int progress = 0;
2462
2463 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002464 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465
2466 if (khugepaged_scan.mm_slot)
2467 mm_slot = khugepaged_scan.mm_slot;
2468 else {
2469 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2470 struct mm_slot, mm_node);
2471 khugepaged_scan.address = 0;
2472 khugepaged_scan.mm_slot = mm_slot;
2473 }
2474 spin_unlock(&khugepaged_mm_lock);
2475
2476 mm = mm_slot->mm;
2477 down_read(&mm->mmap_sem);
2478 if (unlikely(khugepaged_test_exit(mm)))
2479 vma = NULL;
2480 else
2481 vma = find_vma(mm, khugepaged_scan.address);
2482
2483 progress++;
2484 for (; vma; vma = vma->vm_next) {
2485 unsigned long hstart, hend;
2486
2487 cond_resched();
2488 if (unlikely(khugepaged_test_exit(mm))) {
2489 progress++;
2490 break;
2491 }
Bob Liufa475e52012-12-11 16:00:39 -08002492 if (!hugepage_vma_check(vma)) {
2493skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002494 progress++;
2495 continue;
2496 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002497 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2498 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002499 if (hstart >= hend)
2500 goto skip;
2501 if (khugepaged_scan.address > hend)
2502 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002503 if (khugepaged_scan.address < hstart)
2504 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002505 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002506
2507 while (khugepaged_scan.address < hend) {
2508 int ret;
2509 cond_resched();
2510 if (unlikely(khugepaged_test_exit(mm)))
2511 goto breakouterloop;
2512
2513 VM_BUG_ON(khugepaged_scan.address < hstart ||
2514 khugepaged_scan.address + HPAGE_PMD_SIZE >
2515 hend);
2516 ret = khugepaged_scan_pmd(mm, vma,
2517 khugepaged_scan.address,
2518 hpage);
2519 /* move to next address */
2520 khugepaged_scan.address += HPAGE_PMD_SIZE;
2521 progress += HPAGE_PMD_NR;
2522 if (ret)
2523 /* we released mmap_sem so break loop */
2524 goto breakouterloop_mmap_sem;
2525 if (progress >= pages)
2526 goto breakouterloop;
2527 }
2528 }
2529breakouterloop:
2530 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2531breakouterloop_mmap_sem:
2532
2533 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002534 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535 /*
2536 * Release the current mm_slot if this mm is about to die, or
2537 * if we scanned all vmas of this mm.
2538 */
2539 if (khugepaged_test_exit(mm) || !vma) {
2540 /*
2541 * Make sure that if mm_users is reaching zero while
2542 * khugepaged runs here, khugepaged_exit will find
2543 * mm_slot not pointing to the exiting mm.
2544 */
2545 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2546 khugepaged_scan.mm_slot = list_entry(
2547 mm_slot->mm_node.next,
2548 struct mm_slot, mm_node);
2549 khugepaged_scan.address = 0;
2550 } else {
2551 khugepaged_scan.mm_slot = NULL;
2552 khugepaged_full_scans++;
2553 }
2554
2555 collect_mm_slot(mm_slot);
2556 }
2557
2558 return progress;
2559}
2560
2561static int khugepaged_has_work(void)
2562{
2563 return !list_empty(&khugepaged_scan.mm_head) &&
2564 khugepaged_enabled();
2565}
2566
2567static int khugepaged_wait_event(void)
2568{
2569 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002570 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571}
2572
Xiao Guangrongd5169042012-10-08 16:29:48 -07002573static void khugepaged_do_scan(void)
2574{
2575 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576 unsigned int progress = 0, pass_through_head = 0;
2577 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002578 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579
2580 barrier(); /* write khugepaged_pages_to_scan to local stack */
2581
2582 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002583 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002584 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002585
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002586 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002587
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002588 if (unlikely(kthread_should_stop() || freezing(current)))
2589 break;
2590
Andrea Arcangeliba761492011-01-13 15:46:58 -08002591 spin_lock(&khugepaged_mm_lock);
2592 if (!khugepaged_scan.mm_slot)
2593 pass_through_head++;
2594 if (khugepaged_has_work() &&
2595 pass_through_head < 2)
2596 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002597 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002598 else
2599 progress = pages;
2600 spin_unlock(&khugepaged_mm_lock);
2601 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002602
Xiao Guangrongd5169042012-10-08 16:29:48 -07002603 if (!IS_ERR_OR_NULL(hpage))
2604 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002605}
2606
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002607static void khugepaged_wait_work(void)
2608{
2609 try_to_freeze();
2610
2611 if (khugepaged_has_work()) {
2612 if (!khugepaged_scan_sleep_millisecs)
2613 return;
2614
2615 wait_event_freezable_timeout(khugepaged_wait,
2616 kthread_should_stop(),
2617 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2618 return;
2619 }
2620
2621 if (khugepaged_enabled())
2622 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2623}
2624
Andrea Arcangeliba761492011-01-13 15:46:58 -08002625static int khugepaged(void *none)
2626{
2627 struct mm_slot *mm_slot;
2628
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002629 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002630 set_user_nice(current, 19);
2631
Xiao Guangrongb7231782012-10-08 16:29:54 -07002632 while (!kthread_should_stop()) {
2633 khugepaged_do_scan();
2634 khugepaged_wait_work();
2635 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002636
2637 spin_lock(&khugepaged_mm_lock);
2638 mm_slot = khugepaged_scan.mm_slot;
2639 khugepaged_scan.mm_slot = NULL;
2640 if (mm_slot)
2641 collect_mm_slot(mm_slot);
2642 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002643 return 0;
2644}
2645
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002646static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2647 unsigned long haddr, pmd_t *pmd)
2648{
2649 struct mm_struct *mm = vma->vm_mm;
2650 pgtable_t pgtable;
2651 pmd_t _pmd;
2652 int i;
2653
2654 pmdp_clear_flush(vma, haddr, pmd);
2655 /* leave pmd empty until pte is filled */
2656
2657 pgtable = pgtable_trans_huge_withdraw(mm);
2658 pmd_populate(mm, &_pmd, pgtable);
2659
2660 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2661 pte_t *pte, entry;
2662 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2663 entry = pte_mkspecial(entry);
2664 pte = pte_offset_map(&_pmd, haddr);
2665 VM_BUG_ON(!pte_none(*pte));
2666 set_pte_at(mm, haddr, pte, entry);
2667 pte_unmap(pte);
2668 }
2669 smp_wmb(); /* make pte visible before pmd */
2670 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002671 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002672}
2673
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002674void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2675 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002676{
2677 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002678 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002679 unsigned long haddr = address & HPAGE_PMD_MASK;
2680 unsigned long mmun_start; /* For mmu_notifiers */
2681 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002682
2683 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002684
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002685 mmun_start = haddr;
2686 mmun_end = haddr + HPAGE_PMD_SIZE;
2687 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002688 spin_lock(&mm->page_table_lock);
2689 if (unlikely(!pmd_trans_huge(*pmd))) {
2690 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002691 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2692 return;
2693 }
2694 if (is_huge_zero_pmd(*pmd)) {
2695 __split_huge_zero_page_pmd(vma, haddr, pmd);
2696 spin_unlock(&mm->page_table_lock);
2697 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002698 return;
2699 }
2700 page = pmd_page(*pmd);
2701 VM_BUG_ON(!page_count(page));
2702 get_page(page);
2703 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002704 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002705
2706 split_huge_page(page);
2707
2708 put_page(page);
2709 BUG_ON(pmd_trans_huge(*pmd));
2710}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002711
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002712void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2713 pmd_t *pmd)
2714{
2715 struct vm_area_struct *vma;
2716
2717 vma = find_vma(mm, address);
2718 BUG_ON(vma == NULL);
2719 split_huge_page_pmd(vma, address, pmd);
2720}
2721
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002722static void split_huge_page_address(struct mm_struct *mm,
2723 unsigned long address)
2724{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002725 pmd_t *pmd;
2726
2727 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2728
Bob Liu62190492012-12-11 16:00:37 -08002729 pmd = mm_find_pmd(mm, address);
2730 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002731 return;
2732 /*
2733 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2734 * materialize from under us.
2735 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002736 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002737}
2738
2739void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2740 unsigned long start,
2741 unsigned long end,
2742 long adjust_next)
2743{
2744 /*
2745 * If the new start address isn't hpage aligned and it could
2746 * previously contain an hugepage: check if we need to split
2747 * an huge pmd.
2748 */
2749 if (start & ~HPAGE_PMD_MASK &&
2750 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2751 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2752 split_huge_page_address(vma->vm_mm, start);
2753
2754 /*
2755 * If the new end address isn't hpage aligned and it could
2756 * previously contain an hugepage: check if we need to split
2757 * an huge pmd.
2758 */
2759 if (end & ~HPAGE_PMD_MASK &&
2760 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2761 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2762 split_huge_page_address(vma->vm_mm, end);
2763
2764 /*
2765 * If we're also updating the vma->vm_next->vm_start, if the new
2766 * vm_next->vm_start isn't page aligned and it could previously
2767 * contain an hugepage: check if we need to split an huge pmd.
2768 */
2769 if (adjust_next > 0) {
2770 struct vm_area_struct *next = vma->vm_next;
2771 unsigned long nstart = next->vm_start;
2772 nstart += adjust_next << PAGE_SHIFT;
2773 if (nstart & ~HPAGE_PMD_MASK &&
2774 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2775 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2776 split_huge_page_address(next->vm_mm, nstart);
2777 }
2778}