blob: f60c4ebaa30c6147dafb4ba650f388c62a47edc4 [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;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700166static struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800167
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700168static inline bool is_huge_zero_page(struct page *page)
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800169{
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700170 return ACCESS_ONCE(huge_zero_page) == page;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800171}
172
173static inline bool is_huge_zero_pmd(pmd_t pmd)
174{
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700175 return is_huge_zero_page(pmd_page(pmd));
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800176}
177
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700178static struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800179{
180 struct page *zero_page;
181retry:
182 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700183 return ACCESS_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800184
185 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
186 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800187 if (!zero_page) {
188 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700189 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800190 }
191 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800192 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700193 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800194 preempt_enable();
195 __free_page(zero_page);
196 goto retry;
197 }
198
199 /* We take additional reference here. It will be put back by shrinker */
200 atomic_set(&huge_zero_refcount, 2);
201 preempt_enable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700202 return ACCESS_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800203}
204
205static void put_huge_zero_page(void)
206{
207 /*
208 * Counter should never go to zero here. Only shrinker can put
209 * last reference.
210 */
211 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
212}
213
214static int shrink_huge_zero_page(struct shrinker *shrink,
215 struct shrink_control *sc)
216{
217 if (!sc->nr_to_scan)
218 /* we can free zero page only if last reference remains */
219 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
220
221 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700222 struct page *zero_page = xchg(&huge_zero_page, NULL);
223 BUG_ON(zero_page == NULL);
224 __free_page(zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800225 }
226
227 return 0;
228}
229
230static struct shrinker huge_zero_page_shrinker = {
231 .shrink = shrink_huge_zero_page,
232 .seeks = DEFAULT_SEEKS,
233};
234
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800235#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800236
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800237static ssize_t double_flag_show(struct kobject *kobj,
238 struct kobj_attribute *attr, char *buf,
239 enum transparent_hugepage_flag enabled,
240 enum transparent_hugepage_flag req_madv)
241{
242 if (test_bit(enabled, &transparent_hugepage_flags)) {
243 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
244 return sprintf(buf, "[always] madvise never\n");
245 } else if (test_bit(req_madv, &transparent_hugepage_flags))
246 return sprintf(buf, "always [madvise] never\n");
247 else
248 return sprintf(buf, "always madvise [never]\n");
249}
250static ssize_t double_flag_store(struct kobject *kobj,
251 struct kobj_attribute *attr,
252 const char *buf, size_t count,
253 enum transparent_hugepage_flag enabled,
254 enum transparent_hugepage_flag req_madv)
255{
256 if (!memcmp("always", buf,
257 min(sizeof("always")-1, count))) {
258 set_bit(enabled, &transparent_hugepage_flags);
259 clear_bit(req_madv, &transparent_hugepage_flags);
260 } else if (!memcmp("madvise", buf,
261 min(sizeof("madvise")-1, count))) {
262 clear_bit(enabled, &transparent_hugepage_flags);
263 set_bit(req_madv, &transparent_hugepage_flags);
264 } else if (!memcmp("never", buf,
265 min(sizeof("never")-1, count))) {
266 clear_bit(enabled, &transparent_hugepage_flags);
267 clear_bit(req_madv, &transparent_hugepage_flags);
268 } else
269 return -EINVAL;
270
271 return count;
272}
273
274static ssize_t enabled_show(struct kobject *kobj,
275 struct kobj_attribute *attr, char *buf)
276{
277 return double_flag_show(kobj, attr, buf,
278 TRANSPARENT_HUGEPAGE_FLAG,
279 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
280}
281static ssize_t enabled_store(struct kobject *kobj,
282 struct kobj_attribute *attr,
283 const char *buf, size_t count)
284{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800285 ssize_t ret;
286
287 ret = double_flag_store(kobj, attr, buf, count,
288 TRANSPARENT_HUGEPAGE_FLAG,
289 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
290
291 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700292 int err;
293
294 mutex_lock(&khugepaged_mutex);
295 err = start_khugepaged();
296 mutex_unlock(&khugepaged_mutex);
297
Andrea Arcangeliba761492011-01-13 15:46:58 -0800298 if (err)
299 ret = err;
300 }
301
302 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800303}
304static struct kobj_attribute enabled_attr =
305 __ATTR(enabled, 0644, enabled_show, enabled_store);
306
307static ssize_t single_flag_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *buf,
309 enum transparent_hugepage_flag flag)
310{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700311 return sprintf(buf, "%d\n",
312 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800313}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700314
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800315static ssize_t single_flag_store(struct kobject *kobj,
316 struct kobj_attribute *attr,
317 const char *buf, size_t count,
318 enum transparent_hugepage_flag flag)
319{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700320 unsigned long value;
321 int ret;
322
323 ret = kstrtoul(buf, 10, &value);
324 if (ret < 0)
325 return ret;
326 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800327 return -EINVAL;
328
Ben Hutchingse27e6152011-04-14 15:22:21 -0700329 if (value)
330 set_bit(flag, &transparent_hugepage_flags);
331 else
332 clear_bit(flag, &transparent_hugepage_flags);
333
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800334 return count;
335}
336
337/*
338 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
339 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
340 * memory just to allocate one more hugepage.
341 */
342static ssize_t defrag_show(struct kobject *kobj,
343 struct kobj_attribute *attr, char *buf)
344{
345 return double_flag_show(kobj, attr, buf,
346 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
347 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
348}
349static ssize_t defrag_store(struct kobject *kobj,
350 struct kobj_attribute *attr,
351 const char *buf, size_t count)
352{
353 return double_flag_store(kobj, attr, buf, count,
354 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
355 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
356}
357static struct kobj_attribute defrag_attr =
358 __ATTR(defrag, 0644, defrag_show, defrag_store);
359
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800360static ssize_t use_zero_page_show(struct kobject *kobj,
361 struct kobj_attribute *attr, char *buf)
362{
363 return single_flag_show(kobj, attr, buf,
364 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
365}
366static ssize_t use_zero_page_store(struct kobject *kobj,
367 struct kobj_attribute *attr, const char *buf, size_t count)
368{
369 return single_flag_store(kobj, attr, buf, count,
370 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
371}
372static struct kobj_attribute use_zero_page_attr =
373 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800374#ifdef CONFIG_DEBUG_VM
375static ssize_t debug_cow_show(struct kobject *kobj,
376 struct kobj_attribute *attr, char *buf)
377{
378 return single_flag_show(kobj, attr, buf,
379 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
380}
381static ssize_t debug_cow_store(struct kobject *kobj,
382 struct kobj_attribute *attr,
383 const char *buf, size_t count)
384{
385 return single_flag_store(kobj, attr, buf, count,
386 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
387}
388static struct kobj_attribute debug_cow_attr =
389 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
390#endif /* CONFIG_DEBUG_VM */
391
392static struct attribute *hugepage_attr[] = {
393 &enabled_attr.attr,
394 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800395 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800396#ifdef CONFIG_DEBUG_VM
397 &debug_cow_attr.attr,
398#endif
399 NULL,
400};
401
402static struct attribute_group hugepage_attr_group = {
403 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800404};
405
406static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
407 struct kobj_attribute *attr,
408 char *buf)
409{
410 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
411}
412
413static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
414 struct kobj_attribute *attr,
415 const char *buf, size_t count)
416{
417 unsigned long msecs;
418 int err;
419
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700420 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800421 if (err || msecs > UINT_MAX)
422 return -EINVAL;
423
424 khugepaged_scan_sleep_millisecs = msecs;
425 wake_up_interruptible(&khugepaged_wait);
426
427 return count;
428}
429static struct kobj_attribute scan_sleep_millisecs_attr =
430 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
431 scan_sleep_millisecs_store);
432
433static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
434 struct kobj_attribute *attr,
435 char *buf)
436{
437 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
438}
439
440static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
441 struct kobj_attribute *attr,
442 const char *buf, size_t count)
443{
444 unsigned long msecs;
445 int err;
446
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700447 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800448 if (err || msecs > UINT_MAX)
449 return -EINVAL;
450
451 khugepaged_alloc_sleep_millisecs = msecs;
452 wake_up_interruptible(&khugepaged_wait);
453
454 return count;
455}
456static struct kobj_attribute alloc_sleep_millisecs_attr =
457 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
458 alloc_sleep_millisecs_store);
459
460static ssize_t pages_to_scan_show(struct kobject *kobj,
461 struct kobj_attribute *attr,
462 char *buf)
463{
464 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
465}
466static ssize_t pages_to_scan_store(struct kobject *kobj,
467 struct kobj_attribute *attr,
468 const char *buf, size_t count)
469{
470 int err;
471 unsigned long pages;
472
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700473 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800474 if (err || !pages || pages > UINT_MAX)
475 return -EINVAL;
476
477 khugepaged_pages_to_scan = pages;
478
479 return count;
480}
481static struct kobj_attribute pages_to_scan_attr =
482 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
483 pages_to_scan_store);
484
485static ssize_t pages_collapsed_show(struct kobject *kobj,
486 struct kobj_attribute *attr,
487 char *buf)
488{
489 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
490}
491static struct kobj_attribute pages_collapsed_attr =
492 __ATTR_RO(pages_collapsed);
493
494static ssize_t full_scans_show(struct kobject *kobj,
495 struct kobj_attribute *attr,
496 char *buf)
497{
498 return sprintf(buf, "%u\n", khugepaged_full_scans);
499}
500static struct kobj_attribute full_scans_attr =
501 __ATTR_RO(full_scans);
502
503static ssize_t khugepaged_defrag_show(struct kobject *kobj,
504 struct kobj_attribute *attr, char *buf)
505{
506 return single_flag_show(kobj, attr, buf,
507 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
508}
509static ssize_t khugepaged_defrag_store(struct kobject *kobj,
510 struct kobj_attribute *attr,
511 const char *buf, size_t count)
512{
513 return single_flag_store(kobj, attr, buf, count,
514 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
515}
516static struct kobj_attribute khugepaged_defrag_attr =
517 __ATTR(defrag, 0644, khugepaged_defrag_show,
518 khugepaged_defrag_store);
519
520/*
521 * max_ptes_none controls if khugepaged should collapse hugepages over
522 * any unmapped ptes in turn potentially increasing the memory
523 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
524 * reduce the available free memory in the system as it
525 * runs. Increasing max_ptes_none will instead potentially reduce the
526 * free memory in the system during the khugepaged scan.
527 */
528static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
529 struct kobj_attribute *attr,
530 char *buf)
531{
532 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
533}
534static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
535 struct kobj_attribute *attr,
536 const char *buf, size_t count)
537{
538 int err;
539 unsigned long max_ptes_none;
540
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700541 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800542 if (err || max_ptes_none > HPAGE_PMD_NR-1)
543 return -EINVAL;
544
545 khugepaged_max_ptes_none = max_ptes_none;
546
547 return count;
548}
549static struct kobj_attribute khugepaged_max_ptes_none_attr =
550 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
551 khugepaged_max_ptes_none_store);
552
553static struct attribute *khugepaged_attr[] = {
554 &khugepaged_defrag_attr.attr,
555 &khugepaged_max_ptes_none_attr.attr,
556 &pages_to_scan_attr.attr,
557 &pages_collapsed_attr.attr,
558 &full_scans_attr.attr,
559 &scan_sleep_millisecs_attr.attr,
560 &alloc_sleep_millisecs_attr.attr,
561 NULL,
562};
563
564static struct attribute_group khugepaged_attr_group = {
565 .attrs = khugepaged_attr,
566 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800567};
Shaohua Li569e5592012-01-12 17:19:11 -0800568
569static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
570{
571 int err;
572
573 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
574 if (unlikely(!*hugepage_kobj)) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800575 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800576 return -ENOMEM;
577 }
578
579 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
580 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800581 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800582 goto delete_obj;
583 }
584
585 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
586 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800587 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800588 goto remove_hp_group;
589 }
590
591 return 0;
592
593remove_hp_group:
594 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
595delete_obj:
596 kobject_put(*hugepage_kobj);
597 return err;
598}
599
600static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
601{
602 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
603 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
604 kobject_put(hugepage_kobj);
605}
606#else
607static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
608{
609 return 0;
610}
611
612static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
613{
614}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800615#endif /* CONFIG_SYSFS */
616
617static int __init hugepage_init(void)
618{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800619 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800620 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800621
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800622 if (!has_transparent_hugepage()) {
623 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800624 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800625 }
626
Shaohua Li569e5592012-01-12 17:19:11 -0800627 err = hugepage_init_sysfs(&hugepage_kobj);
628 if (err)
629 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800630
631 err = khugepaged_slab_init();
632 if (err)
633 goto out;
634
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800635 register_shrinker(&huge_zero_page_shrinker);
636
Rik van Riel97562cd2011-01-13 15:47:12 -0800637 /*
638 * By default disable transparent hugepages on smaller systems,
639 * where the extra memory used could hurt more than TLB overhead
640 * is likely to save. The admin can still enable it through /sys.
641 */
642 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
643 transparent_hugepage_flags = 0;
644
Andrea Arcangeliba761492011-01-13 15:46:58 -0800645 start_khugepaged();
646
Shaohua Li569e5592012-01-12 17:19:11 -0800647 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800648out:
Shaohua Li569e5592012-01-12 17:19:11 -0800649 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800650 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800651}
652module_init(hugepage_init)
653
654static int __init setup_transparent_hugepage(char *str)
655{
656 int ret = 0;
657 if (!str)
658 goto out;
659 if (!strcmp(str, "always")) {
660 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
661 &transparent_hugepage_flags);
662 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
663 &transparent_hugepage_flags);
664 ret = 1;
665 } else if (!strcmp(str, "madvise")) {
666 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
667 &transparent_hugepage_flags);
668 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
669 &transparent_hugepage_flags);
670 ret = 1;
671 } else if (!strcmp(str, "never")) {
672 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
673 &transparent_hugepage_flags);
674 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
675 &transparent_hugepage_flags);
676 ret = 1;
677 }
678out:
679 if (!ret)
680 printk(KERN_WARNING
681 "transparent_hugepage= cannot parse, ignored\n");
682 return ret;
683}
684__setup("transparent_hugepage=", setup_transparent_hugepage);
685
Mel Gormanb32967f2012-11-19 12:35:47 +0000686pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800687{
688 if (likely(vma->vm_flags & VM_WRITE))
689 pmd = pmd_mkwrite(pmd);
690 return pmd;
691}
692
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700693static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800694{
695 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700696 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800697 entry = pmd_mkhuge(entry);
698 return entry;
699}
700
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800701static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
702 struct vm_area_struct *vma,
703 unsigned long haddr, pmd_t *pmd,
704 struct page *page)
705{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800706 pgtable_t pgtable;
707
708 VM_BUG_ON(!PageCompound(page));
709 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700710 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800711 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800712
713 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700714 /*
715 * The memory barrier inside __SetPageUptodate makes sure that
716 * clear_huge_page writes become visible before the set_pmd_at()
717 * write.
718 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800719 __SetPageUptodate(page);
720
721 spin_lock(&mm->page_table_lock);
722 if (unlikely(!pmd_none(*pmd))) {
723 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800724 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800725 put_page(page);
726 pte_free(mm, pgtable);
727 } else {
728 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700729 entry = mk_huge_pmd(page, vma->vm_page_prot);
730 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800731 page_add_new_anon_rmap(page, vma, haddr);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700732 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800733 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800734 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800735 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800736 spin_unlock(&mm->page_table_lock);
737 }
738
David Rientjesaa2e8782012-05-29 15:06:17 -0700739 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800740}
741
Andi Kleencc5d4622011-03-22 16:33:13 -0700742static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800743{
Andi Kleencc5d4622011-03-22 16:33:13 -0700744 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800745}
746
747static inline struct page *alloc_hugepage_vma(int defrag,
748 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700749 unsigned long haddr, int nd,
750 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800751{
Andi Kleencc5d4622011-03-22 16:33:13 -0700752 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800753 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800754}
755
756#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800757static inline struct page *alloc_hugepage(int defrag)
758{
Andi Kleencc5d4622011-03-22 16:33:13 -0700759 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800760 HPAGE_PMD_ORDER);
761}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800762#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800763
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800764static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800765 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700766 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800767{
768 pmd_t entry;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800769 if (!pmd_none(*pmd))
770 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700771 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800772 entry = pmd_wrprotect(entry);
773 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700774 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800775 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800776 mm->nr_ptes++;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800777 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800778}
779
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800780int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
781 unsigned long address, pmd_t *pmd,
782 unsigned int flags)
783{
784 struct page *page;
785 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800786
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700787 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700788 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700789 if (unlikely(anon_vma_prepare(vma)))
790 return VM_FAULT_OOM;
791 if (unlikely(khugepaged_enter(vma)))
792 return VM_FAULT_OOM;
793 if (!(flags & FAULT_FLAG_WRITE) &&
794 transparent_hugepage_use_zero_page()) {
795 pgtable_t pgtable;
796 struct page *zero_page;
797 bool set;
798 pgtable = pte_alloc_one(mm, haddr);
799 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800800 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700801 zero_page = get_huge_zero_page();
802 if (unlikely(!zero_page)) {
803 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700804 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700805 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700806 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700807 spin_lock(&mm->page_table_lock);
808 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
809 zero_page);
810 spin_unlock(&mm->page_table_lock);
811 if (!set) {
812 pte_free(mm, pgtable);
813 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800814 }
David Rientjesedad9d22012-05-29 15:06:17 -0700815 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800816 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700817 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
818 vma, haddr, numa_node_id(), 0);
819 if (unlikely(!page)) {
820 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700821 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700822 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700823 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
824 put_page(page);
David Rientjes17766dd2013-09-12 15:14:06 -0700825 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700826 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700827 }
828 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page))) {
829 mem_cgroup_uncharge_page(page);
830 put_page(page);
David Rientjes17766dd2013-09-12 15:14:06 -0700831 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700832 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700833 }
834
David Rientjes17766dd2013-09-12 15:14:06 -0700835 count_vm_event(THP_FAULT_ALLOC);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700836 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800837}
838
839int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
840 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
841 struct vm_area_struct *vma)
842{
843 struct page *src_page;
844 pmd_t pmd;
845 pgtable_t pgtable;
846 int ret;
847
848 ret = -ENOMEM;
849 pgtable = pte_alloc_one(dst_mm, addr);
850 if (unlikely(!pgtable))
851 goto out;
852
853 spin_lock(&dst_mm->page_table_lock);
854 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
855
856 ret = -EAGAIN;
857 pmd = *src_pmd;
858 if (unlikely(!pmd_trans_huge(pmd))) {
859 pte_free(dst_mm, pgtable);
860 goto out_unlock;
861 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800862 /*
863 * mm->page_table_lock is enough to be sure that huge zero pmd is not
864 * under splitting since we don't split the page itself, only pmd to
865 * a page table.
866 */
867 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700868 struct page *zero_page;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800869 bool set;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800870 /*
871 * get_huge_zero_page() will never allocate a new page here,
872 * since we already have a zero page to copy. It just takes a
873 * reference.
874 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700875 zero_page = get_huge_zero_page();
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800876 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700877 zero_page);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800878 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800879 ret = 0;
880 goto out_unlock;
881 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800882 if (unlikely(pmd_trans_splitting(pmd))) {
883 /* split huge page running from under us */
884 spin_unlock(&src_mm->page_table_lock);
885 spin_unlock(&dst_mm->page_table_lock);
886 pte_free(dst_mm, pgtable);
887
888 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
889 goto out;
890 }
891 src_page = pmd_page(pmd);
892 VM_BUG_ON(!PageHead(src_page));
893 get_page(src_page);
894 page_dup_rmap(src_page);
895 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
896
897 pmdp_set_wrprotect(src_mm, addr, src_pmd);
898 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700899 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800900 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800901 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800902
903 ret = 0;
904out_unlock:
905 spin_unlock(&src_mm->page_table_lock);
906 spin_unlock(&dst_mm->page_table_lock);
907out:
908 return ret;
909}
910
Will Deacona1dd4502012-12-11 16:01:27 -0800911void huge_pmd_set_accessed(struct mm_struct *mm,
912 struct vm_area_struct *vma,
913 unsigned long address,
914 pmd_t *pmd, pmd_t orig_pmd,
915 int dirty)
916{
917 pmd_t entry;
918 unsigned long haddr;
919
920 spin_lock(&mm->page_table_lock);
921 if (unlikely(!pmd_same(*pmd, orig_pmd)))
922 goto unlock;
923
924 entry = pmd_mkyoung(orig_pmd);
925 haddr = address & HPAGE_PMD_MASK;
926 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
927 update_mmu_cache_pmd(vma, address, pmd);
928
929unlock:
930 spin_unlock(&mm->page_table_lock);
931}
932
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800933static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
934 struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800935 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800936{
937 pgtable_t pgtable;
938 pmd_t _pmd;
939 struct page *page;
940 int i, ret = 0;
941 unsigned long mmun_start; /* For mmu_notifiers */
942 unsigned long mmun_end; /* For mmu_notifiers */
943
944 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
945 if (!page) {
946 ret |= VM_FAULT_OOM;
947 goto out;
948 }
949
950 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
951 put_page(page);
952 ret |= VM_FAULT_OOM;
953 goto out;
954 }
955
956 clear_user_highpage(page, address);
957 __SetPageUptodate(page);
958
959 mmun_start = haddr;
960 mmun_end = haddr + HPAGE_PMD_SIZE;
961 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
962
963 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800964 if (unlikely(!pmd_same(*pmd, orig_pmd)))
965 goto out_free_page;
966
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800967 pmdp_clear_flush(vma, haddr, pmd);
968 /* leave pmd empty until pte is filled */
969
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700970 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800971 pmd_populate(mm, &_pmd, pgtable);
972
973 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
974 pte_t *pte, entry;
975 if (haddr == (address & PAGE_MASK)) {
976 entry = mk_pte(page, vma->vm_page_prot);
977 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
978 page_add_new_anon_rmap(page, vma, haddr);
979 } else {
980 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
981 entry = pte_mkspecial(entry);
982 }
983 pte = pte_offset_map(&_pmd, haddr);
984 VM_BUG_ON(!pte_none(*pte));
985 set_pte_at(mm, haddr, pte, entry);
986 pte_unmap(pte);
987 }
988 smp_wmb(); /* make pte visible before pmd */
989 pmd_populate(mm, pmd, pgtable);
990 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800991 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800992 inc_mm_counter(mm, MM_ANONPAGES);
993
994 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
995
996 ret |= VM_FAULT_WRITE;
997out:
998 return ret;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800999out_free_page:
1000 spin_unlock(&mm->page_table_lock);
1001 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1002 mem_cgroup_uncharge_page(page);
1003 put_page(page);
1004 goto out;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001005}
1006
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001007static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1008 struct vm_area_struct *vma,
1009 unsigned long address,
1010 pmd_t *pmd, pmd_t orig_pmd,
1011 struct page *page,
1012 unsigned long haddr)
1013{
1014 pgtable_t pgtable;
1015 pmd_t _pmd;
1016 int ret = 0, i;
1017 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001018 unsigned long mmun_start; /* For mmu_notifiers */
1019 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001020
1021 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1022 GFP_KERNEL);
1023 if (unlikely(!pages)) {
1024 ret |= VM_FAULT_OOM;
1025 goto out;
1026 }
1027
1028 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001029 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1030 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001031 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001032 if (unlikely(!pages[i] ||
1033 mem_cgroup_newpage_charge(pages[i], mm,
1034 GFP_KERNEL))) {
1035 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001036 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001037 mem_cgroup_uncharge_start();
1038 while (--i >= 0) {
1039 mem_cgroup_uncharge_page(pages[i]);
1040 put_page(pages[i]);
1041 }
1042 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001043 kfree(pages);
1044 ret |= VM_FAULT_OOM;
1045 goto out;
1046 }
1047 }
1048
1049 for (i = 0; i < HPAGE_PMD_NR; i++) {
1050 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001051 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001052 __SetPageUptodate(pages[i]);
1053 cond_resched();
1054 }
1055
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001056 mmun_start = haddr;
1057 mmun_end = haddr + HPAGE_PMD_SIZE;
1058 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1059
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001060 spin_lock(&mm->page_table_lock);
1061 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1062 goto out_free_pages;
1063 VM_BUG_ON(!PageHead(page));
1064
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001065 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001066 /* leave pmd empty until pte is filled */
1067
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001068 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001069 pmd_populate(mm, &_pmd, pgtable);
1070
1071 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1072 pte_t *pte, entry;
1073 entry = mk_pte(pages[i], vma->vm_page_prot);
1074 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1075 page_add_new_anon_rmap(pages[i], vma, haddr);
1076 pte = pte_offset_map(&_pmd, haddr);
1077 VM_BUG_ON(!pte_none(*pte));
1078 set_pte_at(mm, haddr, pte, entry);
1079 pte_unmap(pte);
1080 }
1081 kfree(pages);
1082
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001083 smp_wmb(); /* make pte visible before pmd */
1084 pmd_populate(mm, pmd, pgtable);
1085 page_remove_rmap(page);
1086 spin_unlock(&mm->page_table_lock);
1087
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001088 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1089
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001090 ret |= VM_FAULT_WRITE;
1091 put_page(page);
1092
1093out:
1094 return ret;
1095
1096out_free_pages:
1097 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001098 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001099 mem_cgroup_uncharge_start();
1100 for (i = 0; i < HPAGE_PMD_NR; i++) {
1101 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001103 }
1104 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001105 kfree(pages);
1106 goto out;
1107}
1108
1109int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1110 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1111{
1112 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001113 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001114 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001115 unsigned long mmun_start; /* For mmu_notifiers */
1116 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001117
1118 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001119 haddr = address & HPAGE_PMD_MASK;
1120 if (is_huge_zero_pmd(orig_pmd))
1121 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001122 spin_lock(&mm->page_table_lock);
1123 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1124 goto out_unlock;
1125
1126 page = pmd_page(orig_pmd);
1127 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128 if (page_mapcount(page) == 1) {
1129 pmd_t entry;
1130 entry = pmd_mkyoung(orig_pmd);
1131 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1132 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001133 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001134 ret |= VM_FAULT_WRITE;
1135 goto out_unlock;
1136 }
1137 get_page(page);
1138 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001139alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001140 if (transparent_hugepage_enabled(vma) &&
1141 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001142 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001143 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001144 else
1145 new_page = NULL;
1146
1147 if (unlikely(!new_page)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001148 if (is_huge_zero_pmd(orig_pmd)) {
1149 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001150 address, pmd, orig_pmd, haddr);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001151 } else {
1152 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1153 pmd, orig_pmd, page, haddr);
1154 if (ret & VM_FAULT_OOM)
1155 split_huge_page(page);
1156 put_page(page);
1157 }
David Rientjes17766dd2013-09-12 15:14:06 -07001158 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001159 goto out;
1160 }
1161
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001162 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1163 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001164 if (page) {
1165 split_huge_page(page);
1166 put_page(page);
1167 }
David Rientjes17766dd2013-09-12 15:14:06 -07001168 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001169 ret |= VM_FAULT_OOM;
1170 goto out;
1171 }
1172
David Rientjes17766dd2013-09-12 15:14:06 -07001173 count_vm_event(THP_FAULT_ALLOC);
1174
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001175 if (is_huge_zero_pmd(orig_pmd))
1176 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1177 else
1178 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001179 __SetPageUptodate(new_page);
1180
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001181 mmun_start = haddr;
1182 mmun_end = haddr + HPAGE_PMD_SIZE;
1183 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1184
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001186 if (page)
1187 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001188 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001189 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001190 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001191 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001192 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001193 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001194 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001195 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1196 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001197 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001198 page_add_new_anon_rmap(new_page, vma, haddr);
1199 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001200 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001201 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001202 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001203 put_huge_zero_page();
1204 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001205 VM_BUG_ON(!PageHead(page));
1206 page_remove_rmap(page);
1207 put_page(page);
1208 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001209 ret |= VM_FAULT_WRITE;
1210 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001211 spin_unlock(&mm->page_table_lock);
1212out_mn:
1213 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1214out:
1215 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001216out_unlock:
1217 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 return ret;
1219}
1220
David Rientjesb676b292012-10-08 16:34:03 -07001221struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001222 unsigned long addr,
1223 pmd_t *pmd,
1224 unsigned int flags)
1225{
David Rientjesb676b292012-10-08 16:34:03 -07001226 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001227 struct page *page = NULL;
1228
1229 assert_spin_locked(&mm->page_table_lock);
1230
1231 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1232 goto out;
1233
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001234 /* Avoid dumping huge zero page */
1235 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1236 return ERR_PTR(-EFAULT);
1237
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001238 page = pmd_page(*pmd);
1239 VM_BUG_ON(!PageHead(page));
1240 if (flags & FOLL_TOUCH) {
1241 pmd_t _pmd;
1242 /*
1243 * We should set the dirty bit only for FOLL_WRITE but
1244 * for now the dirty bit in the pmd is meaningless.
1245 * And if the dirty bit will become meaningful and
1246 * we'll only set it with FOLL_WRITE, an atomic
1247 * set_bit will be required on the pmd to set the
1248 * young bit, instead of the current set_pmd_at.
1249 */
1250 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001251 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1252 pmd, _pmd, 1))
1253 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001254 }
David Rientjesb676b292012-10-08 16:34:03 -07001255 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1256 if (page->mapping && trylock_page(page)) {
1257 lru_add_drain();
1258 if (page->mapping)
1259 mlock_vma_page(page);
1260 unlock_page(page);
1261 }
1262 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001263 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1264 VM_BUG_ON(!PageCompound(page));
1265 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001266 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001267
1268out:
1269 return page;
1270}
1271
Mel Gormand10e63f2012-10-25 14:16:31 +02001272/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001273int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1274 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001275{
Mel Gormanb32967f2012-11-19 12:35:47 +00001276 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001277 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001278 int target_nid;
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001279 int current_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001280 bool migrated;
Mel Gormand10e63f2012-10-25 14:16:31 +02001281
1282 spin_lock(&mm->page_table_lock);
1283 if (unlikely(!pmd_same(pmd, *pmdp)))
1284 goto out_unlock;
1285
1286 page = pmd_page(pmd);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001287 get_page(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001288 current_nid = page_to_nid(page);
1289 count_vm_numa_event(NUMA_HINT_FAULTS);
1290 if (current_nid == numa_node_id())
1291 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001292
1293 target_nid = mpol_misplaced(page, vma, haddr);
Mel Gormanb32967f2012-11-19 12:35:47 +00001294 if (target_nid == -1) {
1295 put_page(page);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001296 goto clear_pmdnuma;
Mel Gormanb32967f2012-11-19 12:35:47 +00001297 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001298
Mel Gormanb32967f2012-11-19 12:35:47 +00001299 /* Acquire the page lock to serialise THP migrations */
1300 spin_unlock(&mm->page_table_lock);
1301 lock_page(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001302
Mel Gormanb32967f2012-11-19 12:35:47 +00001303 /* Confirm the PTE did not while locked */
1304 spin_lock(&mm->page_table_lock);
1305 if (unlikely(!pmd_same(pmd, *pmdp))) {
1306 unlock_page(page);
1307 put_page(page);
1308 goto out_unlock;
1309 }
1310 spin_unlock(&mm->page_table_lock);
1311
1312 /* Migrate the THP to the requested node */
1313 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001314 pmdp, pmd, addr, page, target_nid);
1315 if (!migrated)
1316 goto check_same;
Mel Gormanb32967f2012-11-19 12:35:47 +00001317
Hugh Dickins340ef392013-02-22 16:34:33 -08001318 task_numa_fault(target_nid, HPAGE_PMD_NR, true);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001319 return 0;
1320
Hugh Dickins340ef392013-02-22 16:34:33 -08001321check_same:
1322 spin_lock(&mm->page_table_lock);
1323 if (unlikely(!pmd_same(pmd, *pmdp)))
1324 goto out_unlock;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001325clear_pmdnuma:
Mel Gormand10e63f2012-10-25 14:16:31 +02001326 pmd = pmd_mknonnuma(pmd);
1327 set_pmd_at(mm, haddr, pmdp, pmd);
1328 VM_BUG_ON(pmd_numa(*pmdp));
1329 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001330out_unlock:
1331 spin_unlock(&mm->page_table_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001332 if (current_nid != -1)
Hugh Dickins340ef392013-02-22 16:34:33 -08001333 task_numa_fault(current_nid, HPAGE_PMD_NR, false);
Mel Gormand10e63f2012-10-25 14:16:31 +02001334 return 0;
1335}
1336
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001337int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001338 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001339{
1340 int ret = 0;
1341
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001342 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1343 struct page *page;
1344 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001345 pmd_t orig_pmd;
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001346 /*
1347 * For architectures like ppc64 we look at deposited pgtable
1348 * when calling pmdp_get_and_clear. So do the
1349 * pgtable_trans_huge_withdraw after finishing pmdp related
1350 * operations.
1351 */
David Millerf5c8ad42012-10-08 16:34:26 -07001352 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001353 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001354 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001355 if (is_huge_zero_pmd(orig_pmd)) {
1356 tlb->mm->nr_ptes--;
1357 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001358 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001359 } else {
1360 page = pmd_page(orig_pmd);
1361 page_remove_rmap(page);
1362 VM_BUG_ON(page_mapcount(page) < 0);
1363 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1364 VM_BUG_ON(!PageHead(page));
1365 tlb->mm->nr_ptes--;
1366 spin_unlock(&tlb->mm->page_table_lock);
1367 tlb_remove_page(tlb, page);
1368 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001369 pte_free(tlb->mm, pgtable);
1370 ret = 1;
1371 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001372 return ret;
1373}
1374
Johannes Weiner0ca16342011-01-13 15:47:02 -08001375int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1376 unsigned long addr, unsigned long end,
1377 unsigned char *vec)
1378{
1379 int ret = 0;
1380
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001381 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1382 /*
1383 * All logical pages in the range are present
1384 * if backed by a huge page.
1385 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001386 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001387 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1388 ret = 1;
1389 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001390
1391 return ret;
1392}
1393
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001394int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1395 unsigned long old_addr,
1396 unsigned long new_addr, unsigned long old_end,
1397 pmd_t *old_pmd, pmd_t *new_pmd)
1398{
1399 int ret = 0;
1400 pmd_t pmd;
1401
1402 struct mm_struct *mm = vma->vm_mm;
1403
1404 if ((old_addr & ~HPAGE_PMD_MASK) ||
1405 (new_addr & ~HPAGE_PMD_MASK) ||
1406 old_end - old_addr < HPAGE_PMD_SIZE ||
1407 (new_vma->vm_flags & VM_NOHUGEPAGE))
1408 goto out;
1409
1410 /*
1411 * The destination pmd shouldn't be established, free_pgtables()
1412 * should have release it.
1413 */
1414 if (WARN_ON(!pmd_none(*new_pmd))) {
1415 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1416 goto out;
1417 }
1418
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001419 ret = __pmd_trans_huge_lock(old_pmd, vma);
1420 if (ret == 1) {
1421 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1422 VM_BUG_ON(!pmd_none(*new_pmd));
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001423 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001424 spin_unlock(&mm->page_table_lock);
1425 }
1426out:
1427 return ret;
1428}
1429
Johannes Weinercd7548a2011-01-13 15:47:04 -08001430int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001431 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001432{
1433 struct mm_struct *mm = vma->vm_mm;
1434 int ret = 0;
1435
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001436 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1437 pmd_t entry;
1438 entry = pmdp_get_and_clear(mm, addr, pmd);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001439 if (!prot_numa) {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001440 entry = pmd_modify(entry, newprot);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001441 BUG_ON(pmd_write(entry));
1442 } else {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001443 struct page *page = pmd_page(*pmd);
1444
1445 /* only check non-shared pages */
1446 if (page_mapcount(page) == 1 &&
1447 !pmd_numa(*pmd)) {
1448 entry = pmd_mknuma(entry);
1449 }
1450 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001451 set_pmd_at(mm, addr, pmd, entry);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001452 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001453 ret = 1;
1454 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001455
1456 return ret;
1457}
1458
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001459/*
1460 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1461 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1462 *
1463 * Note that if it returns 1, this routine returns without unlocking page
1464 * table locks. So callers must unlock them.
1465 */
1466int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1467{
1468 spin_lock(&vma->vm_mm->page_table_lock);
1469 if (likely(pmd_trans_huge(*pmd))) {
1470 if (unlikely(pmd_trans_splitting(*pmd))) {
1471 spin_unlock(&vma->vm_mm->page_table_lock);
1472 wait_split_huge_page(vma->anon_vma, pmd);
1473 return -1;
1474 } else {
1475 /* Thp mapped by 'pmd' is stable, so we can
1476 * handle it as it is. */
1477 return 1;
1478 }
1479 }
1480 spin_unlock(&vma->vm_mm->page_table_lock);
1481 return 0;
1482}
1483
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001484pmd_t *page_check_address_pmd(struct page *page,
1485 struct mm_struct *mm,
1486 unsigned long address,
1487 enum page_check_address_pmd_flag flag)
1488{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001489 pmd_t *pmd, *ret = NULL;
1490
1491 if (address & ~HPAGE_PMD_MASK)
1492 goto out;
1493
Bob Liu62190492012-12-11 16:00:37 -08001494 pmd = mm_find_pmd(mm, address);
1495 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001496 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001497 if (pmd_none(*pmd))
1498 goto out;
1499 if (pmd_page(*pmd) != page)
1500 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001501 /*
1502 * split_vma() may create temporary aliased mappings. There is
1503 * no risk as long as all huge pmd are found and have their
1504 * splitting bit set before __split_huge_page_refcount
1505 * runs. Finding the same huge pmd more than once during the
1506 * same rmap walk is not a problem.
1507 */
1508 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1509 pmd_trans_splitting(*pmd))
1510 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001511 if (pmd_trans_huge(*pmd)) {
1512 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1513 !pmd_trans_splitting(*pmd));
1514 ret = pmd;
1515 }
1516out:
1517 return ret;
1518}
1519
1520static int __split_huge_page_splitting(struct page *page,
1521 struct vm_area_struct *vma,
1522 unsigned long address)
1523{
1524 struct mm_struct *mm = vma->vm_mm;
1525 pmd_t *pmd;
1526 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001527 /* For mmu_notifiers */
1528 const unsigned long mmun_start = address;
1529 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001530
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001531 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001532 spin_lock(&mm->page_table_lock);
1533 pmd = page_check_address_pmd(page, mm, address,
1534 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1535 if (pmd) {
1536 /*
1537 * We can't temporarily set the pmd to null in order
1538 * to split it, the pmd must remain marked huge at all
1539 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001540 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001541 * serialize against split_huge_page*.
1542 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001543 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001544 ret = 1;
1545 }
1546 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001547 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001548
1549 return ret;
1550}
1551
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001552static void __split_huge_page_refcount(struct page *page,
1553 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001554{
1555 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001556 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001557 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001558 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001559
1560 /* prevent PageLRU to go away from under us, and freeze lru stats */
1561 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001562 lruvec = mem_cgroup_page_lruvec(page, zone);
1563
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001564 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001565 /* complete memcg works before add pages to LRU */
1566 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001567
Shaohua Li45676882012-01-12 17:19:18 -08001568 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001569 struct page *page_tail = page + i;
1570
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001571 /* tail_page->_mapcount cannot change */
1572 BUG_ON(page_mapcount(page_tail) < 0);
1573 tail_count += page_mapcount(page_tail);
1574 /* check for overflow */
1575 BUG_ON(tail_count < 0);
1576 BUG_ON(atomic_read(&page_tail->_count) != 0);
1577 /*
1578 * tail_page->_count is zero and not changing from
1579 * under us. But get_page_unless_zero() may be running
1580 * from under us on the tail_page. If we used
1581 * atomic_set() below instead of atomic_add(), we
1582 * would then run atomic_set() concurrently with
1583 * get_page_unless_zero(), and atomic_set() is
1584 * implemented in C not using locked ops. spin_unlock
1585 * on x86 sometime uses locked ops because of PPro
1586 * errata 66, 92, so unless somebody can guarantee
1587 * atomic_set() here would be safe on all archs (and
1588 * not only on x86), it's safer to use atomic_add().
1589 */
1590 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1591 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001592
1593 /* after clearing PageTail the gup refcount can be released */
1594 smp_mb();
1595
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001596 /*
1597 * retain hwpoison flag of the poisoned tail page:
1598 * fix for the unsuitable process killed on Guest Machine(KVM)
1599 * by the memory-failure.
1600 */
1601 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001602 page_tail->flags |= (page->flags &
1603 ((1L << PG_referenced) |
1604 (1L << PG_swapbacked) |
1605 (1L << PG_mlocked) |
Kirill A. Shutemove180cf82013-07-31 13:53:39 -07001606 (1L << PG_uptodate) |
1607 (1L << PG_active) |
1608 (1L << PG_unevictable)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001609 page_tail->flags |= (1L << PG_dirty);
1610
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001611 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001612 smp_wmb();
1613
1614 /*
1615 * __split_huge_page_splitting() already set the
1616 * splitting bit in all pmd that could map this
1617 * hugepage, that will ensure no CPU can alter the
1618 * mapcount on the head page. The mapcount is only
1619 * accounted in the head page and it has to be
1620 * transferred to all tail pages in the below code. So
1621 * for this code to be safe, the split the mapcount
1622 * can't change. But that doesn't mean userland can't
1623 * keep changing and reading the page contents while
1624 * we transfer the mapcount, so the pmd splitting
1625 * status is achieved setting a reserved bit in the
1626 * pmd, not by clearing the present bit.
1627 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001628 page_tail->_mapcount = page->_mapcount;
1629
1630 BUG_ON(page_tail->mapping);
1631 page_tail->mapping = page->mapping;
1632
Shaohua Li45676882012-01-12 17:19:18 -08001633 page_tail->index = page->index + i;
Mel Gorman22b751c2013-02-22 16:34:59 -08001634 page_nid_xchg_last(page_tail, page_nid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001635
1636 BUG_ON(!PageAnon(page_tail));
1637 BUG_ON(!PageUptodate(page_tail));
1638 BUG_ON(!PageDirty(page_tail));
1639 BUG_ON(!PageSwapBacked(page_tail));
1640
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001641 lru_add_page_tail(page, page_tail, lruvec, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001642 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001643 atomic_sub(tail_count, &page->_count);
1644 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001645
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001646 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001647
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001648 ClearPageCompound(page);
1649 compound_unlock(page);
1650 spin_unlock_irq(&zone->lru_lock);
1651
1652 for (i = 1; i < HPAGE_PMD_NR; i++) {
1653 struct page *page_tail = page + i;
1654 BUG_ON(page_count(page_tail) <= 0);
1655 /*
1656 * Tail pages may be freed if there wasn't any mapping
1657 * like if add_to_swap() is running on a lru page that
1658 * had its mapping zapped. And freeing these pages
1659 * requires taking the lru_lock so we do the put_page
1660 * of the tail pages after the split is complete.
1661 */
1662 put_page(page_tail);
1663 }
1664
1665 /*
1666 * Only the head page (now become a regular page) is required
1667 * to be pinned by the caller.
1668 */
1669 BUG_ON(page_count(page) <= 0);
1670}
1671
1672static int __split_huge_page_map(struct page *page,
1673 struct vm_area_struct *vma,
1674 unsigned long address)
1675{
1676 struct mm_struct *mm = vma->vm_mm;
1677 pmd_t *pmd, _pmd;
1678 int ret = 0, i;
1679 pgtable_t pgtable;
1680 unsigned long haddr;
1681
1682 spin_lock(&mm->page_table_lock);
1683 pmd = page_check_address_pmd(page, mm, address,
1684 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1685 if (pmd) {
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001686 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001687 pmd_populate(mm, &_pmd, pgtable);
1688
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001689 haddr = address;
1690 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001691 pte_t *pte, entry;
1692 BUG_ON(PageCompound(page+i));
1693 entry = mk_pte(page + i, vma->vm_page_prot);
1694 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1695 if (!pmd_write(*pmd))
1696 entry = pte_wrprotect(entry);
1697 else
1698 BUG_ON(page_mapcount(page) != 1);
1699 if (!pmd_young(*pmd))
1700 entry = pte_mkold(entry);
Andrea Arcangeli1ba6e0b2012-10-04 01:51:06 +02001701 if (pmd_numa(*pmd))
1702 entry = pte_mknuma(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001703 pte = pte_offset_map(&_pmd, haddr);
1704 BUG_ON(!pte_none(*pte));
1705 set_pte_at(mm, haddr, pte, entry);
1706 pte_unmap(pte);
1707 }
1708
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001709 smp_wmb(); /* make pte visible before pmd */
1710 /*
1711 * Up to this point the pmd is present and huge and
1712 * userland has the whole access to the hugepage
1713 * during the split (which happens in place). If we
1714 * overwrite the pmd with the not-huge version
1715 * pointing to the pte here (which of course we could
1716 * if all CPUs were bug free), userland could trigger
1717 * a small page size TLB miss on the small sized TLB
1718 * while the hugepage TLB entry is still established
1719 * in the huge TLB. Some CPU doesn't like that. See
1720 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1721 * Erratum 383 on page 93. Intel should be safe but is
1722 * also warns that it's only safe if the permission
1723 * and cache attributes of the two entries loaded in
1724 * the two TLB is identical (which should be the case
1725 * here). But it is generally safer to never allow
1726 * small and huge TLB entries for the same virtual
1727 * address to be loaded simultaneously. So instead of
1728 * doing "pmd_populate(); flush_tlb_range();" we first
1729 * mark the current pmd notpresent (atomically because
1730 * here the pmd_trans_huge and pmd_trans_splitting
1731 * must remain set at all times on the pmd until the
1732 * split is complete for this pmd), then we flush the
1733 * SMP TLB and finally we write the non-huge version
1734 * of the pmd entry with pmd_populate.
1735 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001736 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001737 pmd_populate(mm, pmd, pgtable);
1738 ret = 1;
1739 }
1740 spin_unlock(&mm->page_table_lock);
1741
1742 return ret;
1743}
1744
Ingo Molnar5a505082012-12-02 19:56:46 +00001745/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001746static void __split_huge_page(struct page *page,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001747 struct anon_vma *anon_vma,
1748 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001749{
1750 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001751 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001752 struct anon_vma_chain *avc;
1753
1754 BUG_ON(!PageHead(page));
1755 BUG_ON(PageTail(page));
1756
1757 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001758 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001759 struct vm_area_struct *vma = avc->vma;
1760 unsigned long addr = vma_address(page, vma);
1761 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001762 mapcount += __split_huge_page_splitting(page, vma, addr);
1763 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001764 /*
1765 * It is critical that new vmas are added to the tail of the
1766 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1767 * and establishes a child pmd before
1768 * __split_huge_page_splitting() freezes the parent pmd (so if
1769 * we fail to prevent copy_huge_pmd() from running until the
1770 * whole __split_huge_page() is complete), we will still see
1771 * the newly established pmd of the child later during the
1772 * walk, to be able to set it as pmd_trans_splitting too.
1773 */
1774 if (mapcount != page_mapcount(page))
1775 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1776 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001777 BUG_ON(mapcount != page_mapcount(page));
1778
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001779 __split_huge_page_refcount(page, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001780
1781 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001782 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001783 struct vm_area_struct *vma = avc->vma;
1784 unsigned long addr = vma_address(page, vma);
1785 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001786 mapcount2 += __split_huge_page_map(page, vma, addr);
1787 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001788 if (mapcount != mapcount2)
1789 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1790 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001791 BUG_ON(mapcount != mapcount2);
1792}
1793
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001794/*
1795 * Split a hugepage into normal pages. This doesn't change the position of head
1796 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1797 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1798 * from the hugepage.
1799 * Return 0 if the hugepage is split successfully otherwise return 1.
1800 */
1801int split_huge_page_to_list(struct page *page, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001802{
1803 struct anon_vma *anon_vma;
1804 int ret = 1;
1805
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001806 BUG_ON(is_huge_zero_page(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));
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001826 __split_huge_page(page, anon_vma, list);
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);
Libina8f531eb2013-09-11 14:20:38 -07002281 if (!vma)
2282 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002283 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2284 hend = vma->vm_end & HPAGE_PMD_MASK;
2285 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2286 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002287 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002288 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002289 pmd = mm_find_pmd(mm, address);
2290 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002291 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002292 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002293 goto out;
2294
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002295 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002296
2297 pte = pte_offset_map(pmd, address);
2298 ptl = pte_lockptr(mm, pmd);
2299
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002300 mmun_start = address;
2301 mmun_end = address + HPAGE_PMD_SIZE;
2302 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002303 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2304 /*
2305 * After this gup_fast can't run anymore. This also removes
2306 * any huge TLB entry from the CPU so we won't allow
2307 * huge and small TLB entries for the same virtual address
2308 * to avoid the risk of CPU bugs in that area.
2309 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002310 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002311 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002312 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002313
2314 spin_lock(ptl);
2315 isolated = __collapse_huge_page_isolate(vma, address, pte);
2316 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002317
2318 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002319 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002320 spin_lock(&mm->page_table_lock);
2321 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002322 /*
2323 * We can only use set_pmd_at when establishing
2324 * hugepmds and never for establishing regular pmds that
2325 * points to regular pagetables. Use pmd_populate for that
2326 */
2327 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002328 spin_unlock(&mm->page_table_lock);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002329 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002330 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331 }
2332
2333 /*
2334 * All pages are isolated and locked so anon_vma rmap
2335 * can't run anymore.
2336 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002337 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002338
2339 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002340 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002341 __SetPageUptodate(new_page);
2342 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002343
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002344 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2345 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002346
2347 /*
2348 * spin_lock() below is not the equivalent of smp_wmb(), so
2349 * this is needed to avoid the copy_huge_page writes to become
2350 * visible after the set_pmd_at() write.
2351 */
2352 smp_wmb();
2353
2354 spin_lock(&mm->page_table_lock);
2355 BUG_ON(!pmd_none(*pmd));
2356 page_add_new_anon_rmap(new_page, vma, address);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002357 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002359 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002360 spin_unlock(&mm->page_table_lock);
2361
2362 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002363
Andrea Arcangeliba761492011-01-13 15:46:58 -08002364 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002365out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002366 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002367 return;
2368
Andrea Arcangelice83d212011-01-13 15:47:06 -08002369out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002370 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002371 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002372}
2373
2374static int khugepaged_scan_pmd(struct mm_struct *mm,
2375 struct vm_area_struct *vma,
2376 unsigned long address,
2377 struct page **hpage)
2378{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002379 pmd_t *pmd;
2380 pte_t *pte, *_pte;
2381 int ret = 0, referenced = 0, none = 0;
2382 struct page *page;
2383 unsigned long _address;
2384 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002385 int node = NUMA_NO_NODE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002386
2387 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2388
Bob Liu62190492012-12-11 16:00:37 -08002389 pmd = mm_find_pmd(mm, address);
2390 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002391 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002392 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393 goto out;
2394
2395 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2396 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2397 _pte++, _address += PAGE_SIZE) {
2398 pte_t pteval = *_pte;
2399 if (pte_none(pteval)) {
2400 if (++none <= khugepaged_max_ptes_none)
2401 continue;
2402 else
2403 goto out_unmap;
2404 }
2405 if (!pte_present(pteval) || !pte_write(pteval))
2406 goto out_unmap;
2407 page = vm_normal_page(vma, _address, pteval);
2408 if (unlikely(!page))
2409 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002410 /*
2411 * Chose the node of the first page. This could
2412 * be more sophisticated and look at more pages,
2413 * but isn't for now.
2414 */
David Rientjes00ef2d22013-02-22 16:35:36 -08002415 if (node == NUMA_NO_NODE)
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002416 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002417 VM_BUG_ON(PageCompound(page));
2418 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2419 goto out_unmap;
2420 /* cannot use mapcount: can't collapse if there's a gup pin */
2421 if (page_count(page) != 1)
2422 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002423 if (pte_young(pteval) || PageReferenced(page) ||
2424 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002425 referenced = 1;
2426 }
2427 if (referenced)
2428 ret = 1;
2429out_unmap:
2430 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002431 if (ret)
2432 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002433 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002434out:
2435 return ret;
2436}
2437
2438static void collect_mm_slot(struct mm_slot *mm_slot)
2439{
2440 struct mm_struct *mm = mm_slot->mm;
2441
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002442 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002443
2444 if (khugepaged_test_exit(mm)) {
2445 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002446 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002447 list_del(&mm_slot->mm_node);
2448
2449 /*
2450 * Not strictly needed because the mm exited already.
2451 *
2452 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2453 */
2454
2455 /* khugepaged_mm_lock actually not necessary for the below */
2456 free_mm_slot(mm_slot);
2457 mmdrop(mm);
2458 }
2459}
2460
2461static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2462 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002463 __releases(&khugepaged_mm_lock)
2464 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465{
2466 struct mm_slot *mm_slot;
2467 struct mm_struct *mm;
2468 struct vm_area_struct *vma;
2469 int progress = 0;
2470
2471 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002472 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002473
2474 if (khugepaged_scan.mm_slot)
2475 mm_slot = khugepaged_scan.mm_slot;
2476 else {
2477 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2478 struct mm_slot, mm_node);
2479 khugepaged_scan.address = 0;
2480 khugepaged_scan.mm_slot = mm_slot;
2481 }
2482 spin_unlock(&khugepaged_mm_lock);
2483
2484 mm = mm_slot->mm;
2485 down_read(&mm->mmap_sem);
2486 if (unlikely(khugepaged_test_exit(mm)))
2487 vma = NULL;
2488 else
2489 vma = find_vma(mm, khugepaged_scan.address);
2490
2491 progress++;
2492 for (; vma; vma = vma->vm_next) {
2493 unsigned long hstart, hend;
2494
2495 cond_resched();
2496 if (unlikely(khugepaged_test_exit(mm))) {
2497 progress++;
2498 break;
2499 }
Bob Liufa475e52012-12-11 16:00:39 -08002500 if (!hugepage_vma_check(vma)) {
2501skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002502 progress++;
2503 continue;
2504 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002505 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2506 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002507 if (hstart >= hend)
2508 goto skip;
2509 if (khugepaged_scan.address > hend)
2510 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002511 if (khugepaged_scan.address < hstart)
2512 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002513 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002514
2515 while (khugepaged_scan.address < hend) {
2516 int ret;
2517 cond_resched();
2518 if (unlikely(khugepaged_test_exit(mm)))
2519 goto breakouterloop;
2520
2521 VM_BUG_ON(khugepaged_scan.address < hstart ||
2522 khugepaged_scan.address + HPAGE_PMD_SIZE >
2523 hend);
2524 ret = khugepaged_scan_pmd(mm, vma,
2525 khugepaged_scan.address,
2526 hpage);
2527 /* move to next address */
2528 khugepaged_scan.address += HPAGE_PMD_SIZE;
2529 progress += HPAGE_PMD_NR;
2530 if (ret)
2531 /* we released mmap_sem so break loop */
2532 goto breakouterloop_mmap_sem;
2533 if (progress >= pages)
2534 goto breakouterloop;
2535 }
2536 }
2537breakouterloop:
2538 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2539breakouterloop_mmap_sem:
2540
2541 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002542 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002543 /*
2544 * Release the current mm_slot if this mm is about to die, or
2545 * if we scanned all vmas of this mm.
2546 */
2547 if (khugepaged_test_exit(mm) || !vma) {
2548 /*
2549 * Make sure that if mm_users is reaching zero while
2550 * khugepaged runs here, khugepaged_exit will find
2551 * mm_slot not pointing to the exiting mm.
2552 */
2553 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2554 khugepaged_scan.mm_slot = list_entry(
2555 mm_slot->mm_node.next,
2556 struct mm_slot, mm_node);
2557 khugepaged_scan.address = 0;
2558 } else {
2559 khugepaged_scan.mm_slot = NULL;
2560 khugepaged_full_scans++;
2561 }
2562
2563 collect_mm_slot(mm_slot);
2564 }
2565
2566 return progress;
2567}
2568
2569static int khugepaged_has_work(void)
2570{
2571 return !list_empty(&khugepaged_scan.mm_head) &&
2572 khugepaged_enabled();
2573}
2574
2575static int khugepaged_wait_event(void)
2576{
2577 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002578 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579}
2580
Xiao Guangrongd5169042012-10-08 16:29:48 -07002581static void khugepaged_do_scan(void)
2582{
2583 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002584 unsigned int progress = 0, pass_through_head = 0;
2585 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002586 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002587
2588 barrier(); /* write khugepaged_pages_to_scan to local stack */
2589
2590 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002591 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002592 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002593
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002594 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002595
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002596 if (unlikely(kthread_should_stop() || freezing(current)))
2597 break;
2598
Andrea Arcangeliba761492011-01-13 15:46:58 -08002599 spin_lock(&khugepaged_mm_lock);
2600 if (!khugepaged_scan.mm_slot)
2601 pass_through_head++;
2602 if (khugepaged_has_work() &&
2603 pass_through_head < 2)
2604 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002605 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002606 else
2607 progress = pages;
2608 spin_unlock(&khugepaged_mm_lock);
2609 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002610
Xiao Guangrongd5169042012-10-08 16:29:48 -07002611 if (!IS_ERR_OR_NULL(hpage))
2612 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002613}
2614
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002615static void khugepaged_wait_work(void)
2616{
2617 try_to_freeze();
2618
2619 if (khugepaged_has_work()) {
2620 if (!khugepaged_scan_sleep_millisecs)
2621 return;
2622
2623 wait_event_freezable_timeout(khugepaged_wait,
2624 kthread_should_stop(),
2625 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2626 return;
2627 }
2628
2629 if (khugepaged_enabled())
2630 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2631}
2632
Andrea Arcangeliba761492011-01-13 15:46:58 -08002633static int khugepaged(void *none)
2634{
2635 struct mm_slot *mm_slot;
2636
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002637 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002638 set_user_nice(current, 19);
2639
Xiao Guangrongb7231782012-10-08 16:29:54 -07002640 while (!kthread_should_stop()) {
2641 khugepaged_do_scan();
2642 khugepaged_wait_work();
2643 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002644
2645 spin_lock(&khugepaged_mm_lock);
2646 mm_slot = khugepaged_scan.mm_slot;
2647 khugepaged_scan.mm_slot = NULL;
2648 if (mm_slot)
2649 collect_mm_slot(mm_slot);
2650 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002651 return 0;
2652}
2653
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002654static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2655 unsigned long haddr, pmd_t *pmd)
2656{
2657 struct mm_struct *mm = vma->vm_mm;
2658 pgtable_t pgtable;
2659 pmd_t _pmd;
2660 int i;
2661
2662 pmdp_clear_flush(vma, haddr, pmd);
2663 /* leave pmd empty until pte is filled */
2664
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07002665 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002666 pmd_populate(mm, &_pmd, pgtable);
2667
2668 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2669 pte_t *pte, entry;
2670 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2671 entry = pte_mkspecial(entry);
2672 pte = pte_offset_map(&_pmd, haddr);
2673 VM_BUG_ON(!pte_none(*pte));
2674 set_pte_at(mm, haddr, pte, entry);
2675 pte_unmap(pte);
2676 }
2677 smp_wmb(); /* make pte visible before pmd */
2678 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002679 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002680}
2681
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002682void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2683 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002684{
2685 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002686 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002687 unsigned long haddr = address & HPAGE_PMD_MASK;
2688 unsigned long mmun_start; /* For mmu_notifiers */
2689 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002690
2691 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002692
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002693 mmun_start = haddr;
2694 mmun_end = haddr + HPAGE_PMD_SIZE;
2695 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002696 spin_lock(&mm->page_table_lock);
2697 if (unlikely(!pmd_trans_huge(*pmd))) {
2698 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002699 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2700 return;
2701 }
2702 if (is_huge_zero_pmd(*pmd)) {
2703 __split_huge_zero_page_pmd(vma, haddr, pmd);
2704 spin_unlock(&mm->page_table_lock);
2705 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002706 return;
2707 }
2708 page = pmd_page(*pmd);
2709 VM_BUG_ON(!page_count(page));
2710 get_page(page);
2711 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002712 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002713
2714 split_huge_page(page);
2715
2716 put_page(page);
2717 BUG_ON(pmd_trans_huge(*pmd));
2718}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002719
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002720void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2721 pmd_t *pmd)
2722{
2723 struct vm_area_struct *vma;
2724
2725 vma = find_vma(mm, address);
2726 BUG_ON(vma == NULL);
2727 split_huge_page_pmd(vma, address, pmd);
2728}
2729
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002730static void split_huge_page_address(struct mm_struct *mm,
2731 unsigned long address)
2732{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002733 pmd_t *pmd;
2734
2735 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2736
Bob Liu62190492012-12-11 16:00:37 -08002737 pmd = mm_find_pmd(mm, address);
2738 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002739 return;
2740 /*
2741 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2742 * materialize from under us.
2743 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002744 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002745}
2746
2747void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2748 unsigned long start,
2749 unsigned long end,
2750 long adjust_next)
2751{
2752 /*
2753 * If the new start address isn't hpage aligned and it could
2754 * previously contain an hugepage: check if we need to split
2755 * an huge pmd.
2756 */
2757 if (start & ~HPAGE_PMD_MASK &&
2758 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2759 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2760 split_huge_page_address(vma->vm_mm, start);
2761
2762 /*
2763 * If the new end address isn't hpage aligned and it could
2764 * previously contain an hugepage: check if we need to split
2765 * an huge pmd.
2766 */
2767 if (end & ~HPAGE_PMD_MASK &&
2768 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2769 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2770 split_huge_page_address(vma->vm_mm, end);
2771
2772 /*
2773 * If we're also updating the vma->vm_next->vm_start, if the new
2774 * vm_next->vm_start isn't page aligned and it could previously
2775 * contain an hugepage: check if we need to split an huge pmd.
2776 */
2777 if (adjust_next > 0) {
2778 struct vm_area_struct *next = vma->vm_next;
2779 unsigned long nstart = next->vm_start;
2780 nstart += adjust_next << PAGE_SHIFT;
2781 if (nstart & ~HPAGE_PMD_MASK &&
2782 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2783 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2784 split_huge_page_address(next->vm_mm, nstart);
2785 }
2786}