blob: 7ab4e32afe12b4798d625c97ec22d14067b89991 [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
Glauber Costa48896462013-08-28 10:18:15 +1000214static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
215 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800216{
Glauber Costa48896462013-08-28 10:18:15 +1000217 /* we can free zero page only if last reference remains */
218 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
219}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800220
Glauber Costa48896462013-08-28 10:18:15 +1000221static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
222 struct shrink_control *sc)
223{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800224 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700225 struct page *zero_page = xchg(&huge_zero_page, NULL);
226 BUG_ON(zero_page == NULL);
227 __free_page(zero_page);
Glauber Costa48896462013-08-28 10:18:15 +1000228 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800229 }
230
231 return 0;
232}
233
234static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000235 .count_objects = shrink_huge_zero_page_count,
236 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800237 .seeks = DEFAULT_SEEKS,
238};
239
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800240#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800241
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800242static ssize_t double_flag_show(struct kobject *kobj,
243 struct kobj_attribute *attr, char *buf,
244 enum transparent_hugepage_flag enabled,
245 enum transparent_hugepage_flag req_madv)
246{
247 if (test_bit(enabled, &transparent_hugepage_flags)) {
248 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
249 return sprintf(buf, "[always] madvise never\n");
250 } else if (test_bit(req_madv, &transparent_hugepage_flags))
251 return sprintf(buf, "always [madvise] never\n");
252 else
253 return sprintf(buf, "always madvise [never]\n");
254}
255static ssize_t double_flag_store(struct kobject *kobj,
256 struct kobj_attribute *attr,
257 const char *buf, size_t count,
258 enum transparent_hugepage_flag enabled,
259 enum transparent_hugepage_flag req_madv)
260{
261 if (!memcmp("always", buf,
262 min(sizeof("always")-1, count))) {
263 set_bit(enabled, &transparent_hugepage_flags);
264 clear_bit(req_madv, &transparent_hugepage_flags);
265 } else if (!memcmp("madvise", buf,
266 min(sizeof("madvise")-1, count))) {
267 clear_bit(enabled, &transparent_hugepage_flags);
268 set_bit(req_madv, &transparent_hugepage_flags);
269 } else if (!memcmp("never", buf,
270 min(sizeof("never")-1, count))) {
271 clear_bit(enabled, &transparent_hugepage_flags);
272 clear_bit(req_madv, &transparent_hugepage_flags);
273 } else
274 return -EINVAL;
275
276 return count;
277}
278
279static ssize_t enabled_show(struct kobject *kobj,
280 struct kobj_attribute *attr, char *buf)
281{
282 return double_flag_show(kobj, attr, buf,
283 TRANSPARENT_HUGEPAGE_FLAG,
284 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
285}
286static ssize_t enabled_store(struct kobject *kobj,
287 struct kobj_attribute *attr,
288 const char *buf, size_t count)
289{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800290 ssize_t ret;
291
292 ret = double_flag_store(kobj, attr, buf, count,
293 TRANSPARENT_HUGEPAGE_FLAG,
294 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
295
296 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700297 int err;
298
299 mutex_lock(&khugepaged_mutex);
300 err = start_khugepaged();
301 mutex_unlock(&khugepaged_mutex);
302
Andrea Arcangeliba761492011-01-13 15:46:58 -0800303 if (err)
304 ret = err;
305 }
306
307 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800308}
309static struct kobj_attribute enabled_attr =
310 __ATTR(enabled, 0644, enabled_show, enabled_store);
311
312static ssize_t single_flag_show(struct kobject *kobj,
313 struct kobj_attribute *attr, char *buf,
314 enum transparent_hugepage_flag flag)
315{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700316 return sprintf(buf, "%d\n",
317 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800318}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700319
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800320static ssize_t single_flag_store(struct kobject *kobj,
321 struct kobj_attribute *attr,
322 const char *buf, size_t count,
323 enum transparent_hugepage_flag flag)
324{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700325 unsigned long value;
326 int ret;
327
328 ret = kstrtoul(buf, 10, &value);
329 if (ret < 0)
330 return ret;
331 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800332 return -EINVAL;
333
Ben Hutchingse27e6152011-04-14 15:22:21 -0700334 if (value)
335 set_bit(flag, &transparent_hugepage_flags);
336 else
337 clear_bit(flag, &transparent_hugepage_flags);
338
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800339 return count;
340}
341
342/*
343 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
344 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
345 * memory just to allocate one more hugepage.
346 */
347static ssize_t defrag_show(struct kobject *kobj,
348 struct kobj_attribute *attr, char *buf)
349{
350 return double_flag_show(kobj, attr, buf,
351 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
352 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
353}
354static ssize_t defrag_store(struct kobject *kobj,
355 struct kobj_attribute *attr,
356 const char *buf, size_t count)
357{
358 return double_flag_store(kobj, attr, buf, count,
359 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
360 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
361}
362static struct kobj_attribute defrag_attr =
363 __ATTR(defrag, 0644, defrag_show, defrag_store);
364
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800365static ssize_t use_zero_page_show(struct kobject *kobj,
366 struct kobj_attribute *attr, char *buf)
367{
368 return single_flag_show(kobj, attr, buf,
369 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
370}
371static ssize_t use_zero_page_store(struct kobject *kobj,
372 struct kobj_attribute *attr, const char *buf, size_t count)
373{
374 return single_flag_store(kobj, attr, buf, count,
375 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
376}
377static struct kobj_attribute use_zero_page_attr =
378 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800379#ifdef CONFIG_DEBUG_VM
380static ssize_t debug_cow_show(struct kobject *kobj,
381 struct kobj_attribute *attr, char *buf)
382{
383 return single_flag_show(kobj, attr, buf,
384 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
385}
386static ssize_t debug_cow_store(struct kobject *kobj,
387 struct kobj_attribute *attr,
388 const char *buf, size_t count)
389{
390 return single_flag_store(kobj, attr, buf, count,
391 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
392}
393static struct kobj_attribute debug_cow_attr =
394 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
395#endif /* CONFIG_DEBUG_VM */
396
397static struct attribute *hugepage_attr[] = {
398 &enabled_attr.attr,
399 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800400 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800401#ifdef CONFIG_DEBUG_VM
402 &debug_cow_attr.attr,
403#endif
404 NULL,
405};
406
407static struct attribute_group hugepage_attr_group = {
408 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800409};
410
411static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
412 struct kobj_attribute *attr,
413 char *buf)
414{
415 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
416}
417
418static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
419 struct kobj_attribute *attr,
420 const char *buf, size_t count)
421{
422 unsigned long msecs;
423 int err;
424
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700425 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800426 if (err || msecs > UINT_MAX)
427 return -EINVAL;
428
429 khugepaged_scan_sleep_millisecs = msecs;
430 wake_up_interruptible(&khugepaged_wait);
431
432 return count;
433}
434static struct kobj_attribute scan_sleep_millisecs_attr =
435 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
436 scan_sleep_millisecs_store);
437
438static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
439 struct kobj_attribute *attr,
440 char *buf)
441{
442 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
443}
444
445static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
446 struct kobj_attribute *attr,
447 const char *buf, size_t count)
448{
449 unsigned long msecs;
450 int err;
451
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700452 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800453 if (err || msecs > UINT_MAX)
454 return -EINVAL;
455
456 khugepaged_alloc_sleep_millisecs = msecs;
457 wake_up_interruptible(&khugepaged_wait);
458
459 return count;
460}
461static struct kobj_attribute alloc_sleep_millisecs_attr =
462 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
463 alloc_sleep_millisecs_store);
464
465static ssize_t pages_to_scan_show(struct kobject *kobj,
466 struct kobj_attribute *attr,
467 char *buf)
468{
469 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
470}
471static ssize_t pages_to_scan_store(struct kobject *kobj,
472 struct kobj_attribute *attr,
473 const char *buf, size_t count)
474{
475 int err;
476 unsigned long pages;
477
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700478 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800479 if (err || !pages || pages > UINT_MAX)
480 return -EINVAL;
481
482 khugepaged_pages_to_scan = pages;
483
484 return count;
485}
486static struct kobj_attribute pages_to_scan_attr =
487 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
488 pages_to_scan_store);
489
490static ssize_t pages_collapsed_show(struct kobject *kobj,
491 struct kobj_attribute *attr,
492 char *buf)
493{
494 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
495}
496static struct kobj_attribute pages_collapsed_attr =
497 __ATTR_RO(pages_collapsed);
498
499static ssize_t full_scans_show(struct kobject *kobj,
500 struct kobj_attribute *attr,
501 char *buf)
502{
503 return sprintf(buf, "%u\n", khugepaged_full_scans);
504}
505static struct kobj_attribute full_scans_attr =
506 __ATTR_RO(full_scans);
507
508static ssize_t khugepaged_defrag_show(struct kobject *kobj,
509 struct kobj_attribute *attr, char *buf)
510{
511 return single_flag_show(kobj, attr, buf,
512 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
513}
514static ssize_t khugepaged_defrag_store(struct kobject *kobj,
515 struct kobj_attribute *attr,
516 const char *buf, size_t count)
517{
518 return single_flag_store(kobj, attr, buf, count,
519 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
520}
521static struct kobj_attribute khugepaged_defrag_attr =
522 __ATTR(defrag, 0644, khugepaged_defrag_show,
523 khugepaged_defrag_store);
524
525/*
526 * max_ptes_none controls if khugepaged should collapse hugepages over
527 * any unmapped ptes in turn potentially increasing the memory
528 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
529 * reduce the available free memory in the system as it
530 * runs. Increasing max_ptes_none will instead potentially reduce the
531 * free memory in the system during the khugepaged scan.
532 */
533static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
534 struct kobj_attribute *attr,
535 char *buf)
536{
537 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
538}
539static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
540 struct kobj_attribute *attr,
541 const char *buf, size_t count)
542{
543 int err;
544 unsigned long max_ptes_none;
545
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700546 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800547 if (err || max_ptes_none > HPAGE_PMD_NR-1)
548 return -EINVAL;
549
550 khugepaged_max_ptes_none = max_ptes_none;
551
552 return count;
553}
554static struct kobj_attribute khugepaged_max_ptes_none_attr =
555 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
556 khugepaged_max_ptes_none_store);
557
558static struct attribute *khugepaged_attr[] = {
559 &khugepaged_defrag_attr.attr,
560 &khugepaged_max_ptes_none_attr.attr,
561 &pages_to_scan_attr.attr,
562 &pages_collapsed_attr.attr,
563 &full_scans_attr.attr,
564 &scan_sleep_millisecs_attr.attr,
565 &alloc_sleep_millisecs_attr.attr,
566 NULL,
567};
568
569static struct attribute_group khugepaged_attr_group = {
570 .attrs = khugepaged_attr,
571 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800572};
Shaohua Li569e5592012-01-12 17:19:11 -0800573
574static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
575{
576 int err;
577
578 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
579 if (unlikely(!*hugepage_kobj)) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800580 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800581 return -ENOMEM;
582 }
583
584 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
585 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800586 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800587 goto delete_obj;
588 }
589
590 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
591 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800592 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800593 goto remove_hp_group;
594 }
595
596 return 0;
597
598remove_hp_group:
599 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
600delete_obj:
601 kobject_put(*hugepage_kobj);
602 return err;
603}
604
605static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
606{
607 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
608 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
609 kobject_put(hugepage_kobj);
610}
611#else
612static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
613{
614 return 0;
615}
616
617static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
618{
619}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800620#endif /* CONFIG_SYSFS */
621
622static int __init hugepage_init(void)
623{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800624 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800625 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800626
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800627 if (!has_transparent_hugepage()) {
628 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800629 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800630 }
631
Shaohua Li569e5592012-01-12 17:19:11 -0800632 err = hugepage_init_sysfs(&hugepage_kobj);
633 if (err)
634 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800635
636 err = khugepaged_slab_init();
637 if (err)
638 goto out;
639
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800640 register_shrinker(&huge_zero_page_shrinker);
641
Rik van Riel97562cd2011-01-13 15:47:12 -0800642 /*
643 * By default disable transparent hugepages on smaller systems,
644 * where the extra memory used could hurt more than TLB overhead
645 * is likely to save. The admin can still enable it through /sys.
646 */
647 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
648 transparent_hugepage_flags = 0;
649
Andrea Arcangeliba761492011-01-13 15:46:58 -0800650 start_khugepaged();
651
Shaohua Li569e5592012-01-12 17:19:11 -0800652 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800653out:
Shaohua Li569e5592012-01-12 17:19:11 -0800654 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800655 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800656}
657module_init(hugepage_init)
658
659static int __init setup_transparent_hugepage(char *str)
660{
661 int ret = 0;
662 if (!str)
663 goto out;
664 if (!strcmp(str, "always")) {
665 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
666 &transparent_hugepage_flags);
667 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
668 &transparent_hugepage_flags);
669 ret = 1;
670 } else if (!strcmp(str, "madvise")) {
671 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
672 &transparent_hugepage_flags);
673 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
674 &transparent_hugepage_flags);
675 ret = 1;
676 } else if (!strcmp(str, "never")) {
677 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
678 &transparent_hugepage_flags);
679 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
680 &transparent_hugepage_flags);
681 ret = 1;
682 }
683out:
684 if (!ret)
685 printk(KERN_WARNING
686 "transparent_hugepage= cannot parse, ignored\n");
687 return ret;
688}
689__setup("transparent_hugepage=", setup_transparent_hugepage);
690
Mel Gormanb32967f2012-11-19 12:35:47 +0000691pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800692{
693 if (likely(vma->vm_flags & VM_WRITE))
694 pmd = pmd_mkwrite(pmd);
695 return pmd;
696}
697
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700698static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800699{
700 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700701 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800702 entry = pmd_mkhuge(entry);
703 return entry;
704}
705
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800706static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
707 struct vm_area_struct *vma,
708 unsigned long haddr, pmd_t *pmd,
709 struct page *page)
710{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800711 pgtable_t pgtable;
712
713 VM_BUG_ON(!PageCompound(page));
714 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700715 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800716 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800717
718 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700719 /*
720 * The memory barrier inside __SetPageUptodate makes sure that
721 * clear_huge_page writes become visible before the set_pmd_at()
722 * write.
723 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800724 __SetPageUptodate(page);
725
726 spin_lock(&mm->page_table_lock);
727 if (unlikely(!pmd_none(*pmd))) {
728 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800729 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800730 put_page(page);
731 pte_free(mm, pgtable);
732 } else {
733 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700734 entry = mk_huge_pmd(page, vma->vm_page_prot);
735 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800736 page_add_new_anon_rmap(page, vma, haddr);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700737 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800738 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800739 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800740 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800741 spin_unlock(&mm->page_table_lock);
742 }
743
David Rientjesaa2e8782012-05-29 15:06:17 -0700744 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800745}
746
Andi Kleencc5d4622011-03-22 16:33:13 -0700747static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800748{
Andi Kleencc5d4622011-03-22 16:33:13 -0700749 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800750}
751
752static inline struct page *alloc_hugepage_vma(int defrag,
753 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700754 unsigned long haddr, int nd,
755 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800756{
Andi Kleencc5d4622011-03-22 16:33:13 -0700757 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800758 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800759}
760
761#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800762static inline struct page *alloc_hugepage(int defrag)
763{
Andi Kleencc5d4622011-03-22 16:33:13 -0700764 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800765 HPAGE_PMD_ORDER);
766}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800767#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800768
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800769static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800770 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700771 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800772{
773 pmd_t entry;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800774 if (!pmd_none(*pmd))
775 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700776 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800777 entry = pmd_wrprotect(entry);
778 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700779 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800780 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800781 mm->nr_ptes++;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800782 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800783}
784
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800785int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
786 unsigned long address, pmd_t *pmd,
787 unsigned int flags)
788{
789 struct page *page;
790 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800791
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700792 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700793 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700794 if (unlikely(anon_vma_prepare(vma)))
795 return VM_FAULT_OOM;
796 if (unlikely(khugepaged_enter(vma)))
797 return VM_FAULT_OOM;
798 if (!(flags & FAULT_FLAG_WRITE) &&
799 transparent_hugepage_use_zero_page()) {
800 pgtable_t pgtable;
801 struct page *zero_page;
802 bool set;
803 pgtable = pte_alloc_one(mm, haddr);
804 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800805 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700806 zero_page = get_huge_zero_page();
807 if (unlikely(!zero_page)) {
808 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700809 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700810 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700811 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700812 spin_lock(&mm->page_table_lock);
813 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
814 zero_page);
815 spin_unlock(&mm->page_table_lock);
816 if (!set) {
817 pte_free(mm, pgtable);
818 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800819 }
David Rientjesedad9d22012-05-29 15:06:17 -0700820 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800821 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700822 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
823 vma, haddr, numa_node_id(), 0);
824 if (unlikely(!page)) {
825 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 }
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700828 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
829 put_page(page);
David Rientjes17766dd2013-09-12 15:14:06 -0700830 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700831 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700832 }
833 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd, page))) {
834 mem_cgroup_uncharge_page(page);
835 put_page(page);
David Rientjes17766dd2013-09-12 15:14:06 -0700836 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700837 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700838 }
839
David Rientjes17766dd2013-09-12 15:14:06 -0700840 count_vm_event(THP_FAULT_ALLOC);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700841 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800842}
843
844int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
845 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
846 struct vm_area_struct *vma)
847{
848 struct page *src_page;
849 pmd_t pmd;
850 pgtable_t pgtable;
851 int ret;
852
853 ret = -ENOMEM;
854 pgtable = pte_alloc_one(dst_mm, addr);
855 if (unlikely(!pgtable))
856 goto out;
857
858 spin_lock(&dst_mm->page_table_lock);
859 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
860
861 ret = -EAGAIN;
862 pmd = *src_pmd;
863 if (unlikely(!pmd_trans_huge(pmd))) {
864 pte_free(dst_mm, pgtable);
865 goto out_unlock;
866 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800867 /*
868 * mm->page_table_lock is enough to be sure that huge zero pmd is not
869 * under splitting since we don't split the page itself, only pmd to
870 * a page table.
871 */
872 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700873 struct page *zero_page;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800874 bool set;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800875 /*
876 * get_huge_zero_page() will never allocate a new page here,
877 * since we already have a zero page to copy. It just takes a
878 * reference.
879 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700880 zero_page = get_huge_zero_page();
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800881 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700882 zero_page);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800883 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800884 ret = 0;
885 goto out_unlock;
886 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800887 if (unlikely(pmd_trans_splitting(pmd))) {
888 /* split huge page running from under us */
889 spin_unlock(&src_mm->page_table_lock);
890 spin_unlock(&dst_mm->page_table_lock);
891 pte_free(dst_mm, pgtable);
892
893 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
894 goto out;
895 }
896 src_page = pmd_page(pmd);
897 VM_BUG_ON(!PageHead(src_page));
898 get_page(src_page);
899 page_dup_rmap(src_page);
900 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
901
902 pmdp_set_wrprotect(src_mm, addr, src_pmd);
903 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700904 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800905 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800906 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800907
908 ret = 0;
909out_unlock:
910 spin_unlock(&src_mm->page_table_lock);
911 spin_unlock(&dst_mm->page_table_lock);
912out:
913 return ret;
914}
915
Will Deacona1dd4502012-12-11 16:01:27 -0800916void huge_pmd_set_accessed(struct mm_struct *mm,
917 struct vm_area_struct *vma,
918 unsigned long address,
919 pmd_t *pmd, pmd_t orig_pmd,
920 int dirty)
921{
922 pmd_t entry;
923 unsigned long haddr;
924
925 spin_lock(&mm->page_table_lock);
926 if (unlikely(!pmd_same(*pmd, orig_pmd)))
927 goto unlock;
928
929 entry = pmd_mkyoung(orig_pmd);
930 haddr = address & HPAGE_PMD_MASK;
931 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
932 update_mmu_cache_pmd(vma, address, pmd);
933
934unlock:
935 spin_unlock(&mm->page_table_lock);
936}
937
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800938static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
939 struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800940 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800941{
942 pgtable_t pgtable;
943 pmd_t _pmd;
944 struct page *page;
945 int i, ret = 0;
946 unsigned long mmun_start; /* For mmu_notifiers */
947 unsigned long mmun_end; /* For mmu_notifiers */
948
949 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
950 if (!page) {
951 ret |= VM_FAULT_OOM;
952 goto out;
953 }
954
955 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
956 put_page(page);
957 ret |= VM_FAULT_OOM;
958 goto out;
959 }
960
961 clear_user_highpage(page, address);
962 __SetPageUptodate(page);
963
964 mmun_start = haddr;
965 mmun_end = haddr + HPAGE_PMD_SIZE;
966 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
967
968 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800969 if (unlikely(!pmd_same(*pmd, orig_pmd)))
970 goto out_free_page;
971
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800972 pmdp_clear_flush(vma, haddr, pmd);
973 /* leave pmd empty until pte is filled */
974
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700975 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800976 pmd_populate(mm, &_pmd, pgtable);
977
978 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
979 pte_t *pte, entry;
980 if (haddr == (address & PAGE_MASK)) {
981 entry = mk_pte(page, vma->vm_page_prot);
982 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
983 page_add_new_anon_rmap(page, vma, haddr);
984 } else {
985 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
986 entry = pte_mkspecial(entry);
987 }
988 pte = pte_offset_map(&_pmd, haddr);
989 VM_BUG_ON(!pte_none(*pte));
990 set_pte_at(mm, haddr, pte, entry);
991 pte_unmap(pte);
992 }
993 smp_wmb(); /* make pte visible before pmd */
994 pmd_populate(mm, pmd, pgtable);
995 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800996 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800997 inc_mm_counter(mm, MM_ANONPAGES);
998
999 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1000
1001 ret |= VM_FAULT_WRITE;
1002out:
1003 return ret;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001004out_free_page:
1005 spin_unlock(&mm->page_table_lock);
1006 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1007 mem_cgroup_uncharge_page(page);
1008 put_page(page);
1009 goto out;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001010}
1011
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001012static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1013 struct vm_area_struct *vma,
1014 unsigned long address,
1015 pmd_t *pmd, pmd_t orig_pmd,
1016 struct page *page,
1017 unsigned long haddr)
1018{
1019 pgtable_t pgtable;
1020 pmd_t _pmd;
1021 int ret = 0, i;
1022 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001023 unsigned long mmun_start; /* For mmu_notifiers */
1024 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001025
1026 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1027 GFP_KERNEL);
1028 if (unlikely(!pages)) {
1029 ret |= VM_FAULT_OOM;
1030 goto out;
1031 }
1032
1033 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001034 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1035 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001036 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001037 if (unlikely(!pages[i] ||
1038 mem_cgroup_newpage_charge(pages[i], mm,
1039 GFP_KERNEL))) {
1040 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001041 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001042 mem_cgroup_uncharge_start();
1043 while (--i >= 0) {
1044 mem_cgroup_uncharge_page(pages[i]);
1045 put_page(pages[i]);
1046 }
1047 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001048 kfree(pages);
1049 ret |= VM_FAULT_OOM;
1050 goto out;
1051 }
1052 }
1053
1054 for (i = 0; i < HPAGE_PMD_NR; i++) {
1055 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001056 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001057 __SetPageUptodate(pages[i]);
1058 cond_resched();
1059 }
1060
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001061 mmun_start = haddr;
1062 mmun_end = haddr + HPAGE_PMD_SIZE;
1063 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1064
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001065 spin_lock(&mm->page_table_lock);
1066 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1067 goto out_free_pages;
1068 VM_BUG_ON(!PageHead(page));
1069
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001070 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001071 /* leave pmd empty until pte is filled */
1072
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001073 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001074 pmd_populate(mm, &_pmd, pgtable);
1075
1076 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1077 pte_t *pte, entry;
1078 entry = mk_pte(pages[i], vma->vm_page_prot);
1079 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1080 page_add_new_anon_rmap(pages[i], vma, haddr);
1081 pte = pte_offset_map(&_pmd, haddr);
1082 VM_BUG_ON(!pte_none(*pte));
1083 set_pte_at(mm, haddr, pte, entry);
1084 pte_unmap(pte);
1085 }
1086 kfree(pages);
1087
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001088 smp_wmb(); /* make pte visible before pmd */
1089 pmd_populate(mm, pmd, pgtable);
1090 page_remove_rmap(page);
1091 spin_unlock(&mm->page_table_lock);
1092
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001093 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1094
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001095 ret |= VM_FAULT_WRITE;
1096 put_page(page);
1097
1098out:
1099 return ret;
1100
1101out_free_pages:
1102 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001103 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001104 mem_cgroup_uncharge_start();
1105 for (i = 0; i < HPAGE_PMD_NR; i++) {
1106 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001107 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001108 }
1109 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001110 kfree(pages);
1111 goto out;
1112}
1113
1114int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1115 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1116{
1117 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001118 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001119 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001120 unsigned long mmun_start; /* For mmu_notifiers */
1121 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001122
1123 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001124 haddr = address & HPAGE_PMD_MASK;
1125 if (is_huge_zero_pmd(orig_pmd))
1126 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001127 spin_lock(&mm->page_table_lock);
1128 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1129 goto out_unlock;
1130
1131 page = pmd_page(orig_pmd);
1132 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001133 if (page_mapcount(page) == 1) {
1134 pmd_t entry;
1135 entry = pmd_mkyoung(orig_pmd);
1136 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1137 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001138 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001139 ret |= VM_FAULT_WRITE;
1140 goto out_unlock;
1141 }
1142 get_page(page);
1143 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001144alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001145 if (transparent_hugepage_enabled(vma) &&
1146 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001147 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001148 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001149 else
1150 new_page = NULL;
1151
1152 if (unlikely(!new_page)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001153 if (is_huge_zero_pmd(orig_pmd)) {
1154 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001155 address, pmd, orig_pmd, haddr);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001156 } else {
1157 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1158 pmd, orig_pmd, page, haddr);
1159 if (ret & VM_FAULT_OOM)
1160 split_huge_page(page);
1161 put_page(page);
1162 }
David Rientjes17766dd2013-09-12 15:14:06 -07001163 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001164 goto out;
1165 }
1166
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001167 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1168 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001169 if (page) {
1170 split_huge_page(page);
1171 put_page(page);
1172 }
David Rientjes17766dd2013-09-12 15:14:06 -07001173 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001174 ret |= VM_FAULT_OOM;
1175 goto out;
1176 }
1177
David Rientjes17766dd2013-09-12 15:14:06 -07001178 count_vm_event(THP_FAULT_ALLOC);
1179
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001180 if (is_huge_zero_pmd(orig_pmd))
1181 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1182 else
1183 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001184 __SetPageUptodate(new_page);
1185
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001186 mmun_start = haddr;
1187 mmun_end = haddr + HPAGE_PMD_SIZE;
1188 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1189
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001190 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001191 if (page)
1192 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001193 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001194 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001195 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001196 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001197 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001198 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001199 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001200 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1201 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001202 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001203 page_add_new_anon_rmap(new_page, vma, haddr);
1204 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001205 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001206 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001207 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001208 put_huge_zero_page();
1209 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001210 VM_BUG_ON(!PageHead(page));
1211 page_remove_rmap(page);
1212 put_page(page);
1213 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001214 ret |= VM_FAULT_WRITE;
1215 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001216 spin_unlock(&mm->page_table_lock);
1217out_mn:
1218 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1219out:
1220 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001221out_unlock:
1222 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001223 return ret;
1224}
1225
David Rientjesb676b292012-10-08 16:34:03 -07001226struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001227 unsigned long addr,
1228 pmd_t *pmd,
1229 unsigned int flags)
1230{
David Rientjesb676b292012-10-08 16:34:03 -07001231 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001232 struct page *page = NULL;
1233
1234 assert_spin_locked(&mm->page_table_lock);
1235
1236 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1237 goto out;
1238
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001239 /* Avoid dumping huge zero page */
1240 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1241 return ERR_PTR(-EFAULT);
1242
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001243 page = pmd_page(*pmd);
1244 VM_BUG_ON(!PageHead(page));
1245 if (flags & FOLL_TOUCH) {
1246 pmd_t _pmd;
1247 /*
1248 * We should set the dirty bit only for FOLL_WRITE but
1249 * for now the dirty bit in the pmd is meaningless.
1250 * And if the dirty bit will become meaningful and
1251 * we'll only set it with FOLL_WRITE, an atomic
1252 * set_bit will be required on the pmd to set the
1253 * young bit, instead of the current set_pmd_at.
1254 */
1255 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001256 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1257 pmd, _pmd, 1))
1258 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001259 }
David Rientjesb676b292012-10-08 16:34:03 -07001260 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1261 if (page->mapping && trylock_page(page)) {
1262 lru_add_drain();
1263 if (page->mapping)
1264 mlock_vma_page(page);
1265 unlock_page(page);
1266 }
1267 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001268 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1269 VM_BUG_ON(!PageCompound(page));
1270 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001271 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001272
1273out:
1274 return page;
1275}
1276
Mel Gormand10e63f2012-10-25 14:16:31 +02001277/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001278int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1279 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001280{
Mel Gormanb8916632013-10-07 11:28:44 +01001281 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001282 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001283 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001284 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001285 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001286 bool page_locked;
1287 bool migrated = false;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001288 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001289
1290 spin_lock(&mm->page_table_lock);
1291 if (unlikely(!pmd_same(pmd, *pmdp)))
1292 goto out_unlock;
1293
1294 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001295 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001296 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001297 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001298 count_vm_numa_event(NUMA_HINT_FAULTS);
Mel Gorman8191acb2013-10-07 11:28:45 +01001299 if (page_nid == this_nid)
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001300 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001301
Mel Gormanff9042b2013-10-07 11:28:43 +01001302 /*
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001303 * Avoid grouping on DSO/COW pages in specific and RO pages
1304 * in general, RO pages shouldn't hurt as much anyway since
1305 * they can be in shared cache state.
1306 */
1307 if (!pmd_write(pmd))
1308 flags |= TNF_NO_GROUP;
1309
1310 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001311 * Acquire the page lock to serialise THP migrations but avoid dropping
1312 * page_table_lock if at all possible
1313 */
Mel Gormanb8916632013-10-07 11:28:44 +01001314 page_locked = trylock_page(page);
1315 target_nid = mpol_misplaced(page, vma, haddr);
1316 if (target_nid == -1) {
1317 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001318 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001319 goto clear_pmdnuma;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001320
Mel Gormana54a4072013-10-07 11:28:46 +01001321 /*
1322 * Otherwise wait for potential migrations and retry. We do
1323 * relock and check_same as the page may no longer be mapped.
1324 * As the fault is being retried, do not account for it.
1325 */
Mel Gormanb8916632013-10-07 11:28:44 +01001326 spin_unlock(&mm->page_table_lock);
1327 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001328 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001329 goto out;
1330 }
1331
1332 /* Page is misplaced, serialise migrations and parallel THP splits */
1333 get_page(page);
Mel Gormanb32967f2012-11-19 12:35:47 +00001334 spin_unlock(&mm->page_table_lock);
Mel Gormana54a4072013-10-07 11:28:46 +01001335 if (!page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001336 lock_page(page);
Mel Gormanb8916632013-10-07 11:28:44 +01001337 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001338
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001339 /* Confirm the PMD did not change while page_table_lock was released */
Mel Gormanb32967f2012-11-19 12:35:47 +00001340 spin_lock(&mm->page_table_lock);
1341 if (unlikely(!pmd_same(pmd, *pmdp))) {
1342 unlock_page(page);
1343 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001344 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001345 goto out_unlock;
1346 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001347
Mel Gormana54a4072013-10-07 11:28:46 +01001348 /*
1349 * Migrate the THP to the requested node, returns with page unlocked
1350 * and pmd_numa cleared.
1351 */
Mel Gormanff9042b2013-10-07 11:28:43 +01001352 spin_unlock(&mm->page_table_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001353 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001354 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001355 if (migrated) {
1356 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001357 page_nid = target_nid;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001358 }
Mel Gormanb32967f2012-11-19 12:35:47 +00001359
Mel Gorman8191acb2013-10-07 11:28:45 +01001360 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001361clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001362 BUG_ON(!PageLocked(page));
Mel Gormand10e63f2012-10-25 14:16:31 +02001363 pmd = pmd_mknonnuma(pmd);
1364 set_pmd_at(mm, haddr, pmdp, pmd);
1365 VM_BUG_ON(pmd_numa(*pmdp));
1366 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001367 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001368out_unlock:
1369 spin_unlock(&mm->page_table_lock);
Mel Gormanb8916632013-10-07 11:28:44 +01001370
1371out:
1372 if (anon_vma)
1373 page_unlock_anon_vma_read(anon_vma);
1374
Mel Gorman8191acb2013-10-07 11:28:45 +01001375 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001376 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001377
Mel Gormand10e63f2012-10-25 14:16:31 +02001378 return 0;
1379}
1380
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001381int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001382 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001383{
1384 int ret = 0;
1385
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001386 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1387 struct page *page;
1388 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001389 pmd_t orig_pmd;
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001390 /*
1391 * For architectures like ppc64 we look at deposited pgtable
1392 * when calling pmdp_get_and_clear. So do the
1393 * pgtable_trans_huge_withdraw after finishing pmdp related
1394 * operations.
1395 */
David Millerf5c8ad42012-10-08 16:34:26 -07001396 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001397 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001398 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001399 if (is_huge_zero_pmd(orig_pmd)) {
1400 tlb->mm->nr_ptes--;
1401 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001402 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001403 } else {
1404 page = pmd_page(orig_pmd);
1405 page_remove_rmap(page);
1406 VM_BUG_ON(page_mapcount(page) < 0);
1407 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1408 VM_BUG_ON(!PageHead(page));
1409 tlb->mm->nr_ptes--;
1410 spin_unlock(&tlb->mm->page_table_lock);
1411 tlb_remove_page(tlb, page);
1412 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001413 pte_free(tlb->mm, pgtable);
1414 ret = 1;
1415 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001416 return ret;
1417}
1418
Johannes Weiner0ca16342011-01-13 15:47:02 -08001419int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1420 unsigned long addr, unsigned long end,
1421 unsigned char *vec)
1422{
1423 int ret = 0;
1424
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001425 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1426 /*
1427 * All logical pages in the range are present
1428 * if backed by a huge page.
1429 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001430 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001431 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1432 ret = 1;
1433 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001434
1435 return ret;
1436}
1437
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001438int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1439 unsigned long old_addr,
1440 unsigned long new_addr, unsigned long old_end,
1441 pmd_t *old_pmd, pmd_t *new_pmd)
1442{
1443 int ret = 0;
1444 pmd_t pmd;
1445
1446 struct mm_struct *mm = vma->vm_mm;
1447
1448 if ((old_addr & ~HPAGE_PMD_MASK) ||
1449 (new_addr & ~HPAGE_PMD_MASK) ||
1450 old_end - old_addr < HPAGE_PMD_SIZE ||
1451 (new_vma->vm_flags & VM_NOHUGEPAGE))
1452 goto out;
1453
1454 /*
1455 * The destination pmd shouldn't be established, free_pgtables()
1456 * should have release it.
1457 */
1458 if (WARN_ON(!pmd_none(*new_pmd))) {
1459 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1460 goto out;
1461 }
1462
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001463 ret = __pmd_trans_huge_lock(old_pmd, vma);
1464 if (ret == 1) {
1465 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1466 VM_BUG_ON(!pmd_none(*new_pmd));
Pavel Emelyanov0f8975e2013-07-03 15:01:20 -07001467 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001468 spin_unlock(&mm->page_table_lock);
1469 }
1470out:
1471 return ret;
1472}
1473
Mel Gormanf123d742013-10-07 11:28:49 +01001474/*
1475 * Returns
1476 * - 0 if PMD could not be locked
1477 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1478 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1479 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001480int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001481 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001482{
1483 struct mm_struct *mm = vma->vm_mm;
1484 int ret = 0;
1485
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001486 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1487 pmd_t entry;
Mel Gormanf123d742013-10-07 11:28:49 +01001488 ret = 1;
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001489 if (!prot_numa) {
Mel Gormanf123d742013-10-07 11:28:49 +01001490 entry = pmdp_get_and_clear(mm, addr, pmd);
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001491 entry = pmd_modify(entry, newprot);
Mel Gormanf123d742013-10-07 11:28:49 +01001492 ret = HPAGE_PMD_NR;
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001493 BUG_ON(pmd_write(entry));
1494 } else {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001495 struct page *page = pmd_page(*pmd);
1496
Mel Gormana1a46182013-10-07 11:28:50 +01001497 /*
Mel Gorman1bc115d2013-10-07 11:29:05 +01001498 * Do not trap faults against the zero page. The
1499 * read-only data is likely to be read-cached on the
1500 * local CPU cache and it is less useful to know about
1501 * local vs remote hits on the zero page.
Mel Gormana1a46182013-10-07 11:28:50 +01001502 */
Mel Gorman1bc115d2013-10-07 11:29:05 +01001503 if (!is_huge_zero_page(page) &&
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001504 !pmd_numa(*pmd)) {
Mel Gormanf123d742013-10-07 11:28:49 +01001505 entry = pmdp_get_and_clear(mm, addr, pmd);
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001506 entry = pmd_mknuma(entry);
Mel Gormanf123d742013-10-07 11:28:49 +01001507 ret = HPAGE_PMD_NR;
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001508 }
1509 }
Mel Gormanf123d742013-10-07 11:28:49 +01001510
1511 /* Set PMD if cleared earlier */
1512 if (ret == HPAGE_PMD_NR)
1513 set_pmd_at(mm, addr, pmd, entry);
1514
Johannes Weinercd7548a2011-01-13 15:47:04 -08001515 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001516 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001517
1518 return ret;
1519}
1520
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001521/*
1522 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1523 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1524 *
1525 * Note that if it returns 1, this routine returns without unlocking page
1526 * table locks. So callers must unlock them.
1527 */
1528int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1529{
1530 spin_lock(&vma->vm_mm->page_table_lock);
1531 if (likely(pmd_trans_huge(*pmd))) {
1532 if (unlikely(pmd_trans_splitting(*pmd))) {
1533 spin_unlock(&vma->vm_mm->page_table_lock);
1534 wait_split_huge_page(vma->anon_vma, pmd);
1535 return -1;
1536 } else {
1537 /* Thp mapped by 'pmd' is stable, so we can
1538 * handle it as it is. */
1539 return 1;
1540 }
1541 }
1542 spin_unlock(&vma->vm_mm->page_table_lock);
1543 return 0;
1544}
1545
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001546pmd_t *page_check_address_pmd(struct page *page,
1547 struct mm_struct *mm,
1548 unsigned long address,
1549 enum page_check_address_pmd_flag flag)
1550{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001551 pmd_t *pmd, *ret = NULL;
1552
1553 if (address & ~HPAGE_PMD_MASK)
1554 goto out;
1555
Bob Liu62190492012-12-11 16:00:37 -08001556 pmd = mm_find_pmd(mm, address);
1557 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001558 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001559 if (pmd_none(*pmd))
1560 goto out;
1561 if (pmd_page(*pmd) != page)
1562 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001563 /*
1564 * split_vma() may create temporary aliased mappings. There is
1565 * no risk as long as all huge pmd are found and have their
1566 * splitting bit set before __split_huge_page_refcount
1567 * runs. Finding the same huge pmd more than once during the
1568 * same rmap walk is not a problem.
1569 */
1570 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1571 pmd_trans_splitting(*pmd))
1572 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001573 if (pmd_trans_huge(*pmd)) {
1574 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1575 !pmd_trans_splitting(*pmd));
1576 ret = pmd;
1577 }
1578out:
1579 return ret;
1580}
1581
1582static int __split_huge_page_splitting(struct page *page,
1583 struct vm_area_struct *vma,
1584 unsigned long address)
1585{
1586 struct mm_struct *mm = vma->vm_mm;
1587 pmd_t *pmd;
1588 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001589 /* For mmu_notifiers */
1590 const unsigned long mmun_start = address;
1591 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001592
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001593 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001594 spin_lock(&mm->page_table_lock);
1595 pmd = page_check_address_pmd(page, mm, address,
1596 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1597 if (pmd) {
1598 /*
1599 * We can't temporarily set the pmd to null in order
1600 * to split it, the pmd must remain marked huge at all
1601 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001602 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001603 * serialize against split_huge_page*.
1604 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001605 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001606 ret = 1;
1607 }
1608 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001609 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001610
1611 return ret;
1612}
1613
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001614static void __split_huge_page_refcount(struct page *page,
1615 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001616{
1617 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001618 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001619 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001620 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001621
1622 /* prevent PageLRU to go away from under us, and freeze lru stats */
1623 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001624 lruvec = mem_cgroup_page_lruvec(page, zone);
1625
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001626 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001627 /* complete memcg works before add pages to LRU */
1628 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001629
Shaohua Li45676882012-01-12 17:19:18 -08001630 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001631 struct page *page_tail = page + i;
1632
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001633 /* tail_page->_mapcount cannot change */
1634 BUG_ON(page_mapcount(page_tail) < 0);
1635 tail_count += page_mapcount(page_tail);
1636 /* check for overflow */
1637 BUG_ON(tail_count < 0);
1638 BUG_ON(atomic_read(&page_tail->_count) != 0);
1639 /*
1640 * tail_page->_count is zero and not changing from
1641 * under us. But get_page_unless_zero() may be running
1642 * from under us on the tail_page. If we used
1643 * atomic_set() below instead of atomic_add(), we
1644 * would then run atomic_set() concurrently with
1645 * get_page_unless_zero(), and atomic_set() is
1646 * implemented in C not using locked ops. spin_unlock
1647 * on x86 sometime uses locked ops because of PPro
1648 * errata 66, 92, so unless somebody can guarantee
1649 * atomic_set() here would be safe on all archs (and
1650 * not only on x86), it's safer to use atomic_add().
1651 */
1652 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1653 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001654
1655 /* after clearing PageTail the gup refcount can be released */
1656 smp_mb();
1657
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001658 /*
1659 * retain hwpoison flag of the poisoned tail page:
1660 * fix for the unsuitable process killed on Guest Machine(KVM)
1661 * by the memory-failure.
1662 */
1663 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664 page_tail->flags |= (page->flags &
1665 ((1L << PG_referenced) |
1666 (1L << PG_swapbacked) |
1667 (1L << PG_mlocked) |
Kirill A. Shutemove180cf82013-07-31 13:53:39 -07001668 (1L << PG_uptodate) |
1669 (1L << PG_active) |
1670 (1L << PG_unevictable)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001671 page_tail->flags |= (1L << PG_dirty);
1672
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001673 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001674 smp_wmb();
1675
1676 /*
1677 * __split_huge_page_splitting() already set the
1678 * splitting bit in all pmd that could map this
1679 * hugepage, that will ensure no CPU can alter the
1680 * mapcount on the head page. The mapcount is only
1681 * accounted in the head page and it has to be
1682 * transferred to all tail pages in the below code. So
1683 * for this code to be safe, the split the mapcount
1684 * can't change. But that doesn't mean userland can't
1685 * keep changing and reading the page contents while
1686 * we transfer the mapcount, so the pmd splitting
1687 * status is achieved setting a reserved bit in the
1688 * pmd, not by clearing the present bit.
1689 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001690 page_tail->_mapcount = page->_mapcount;
1691
1692 BUG_ON(page_tail->mapping);
1693 page_tail->mapping = page->mapping;
1694
Shaohua Li45676882012-01-12 17:19:18 -08001695 page_tail->index = page->index + i;
Peter Zijlstra90572892013-10-07 11:29:20 +01001696 page_cpupid_xchg_last(page_tail, page_cpupid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001697
1698 BUG_ON(!PageAnon(page_tail));
1699 BUG_ON(!PageUptodate(page_tail));
1700 BUG_ON(!PageDirty(page_tail));
1701 BUG_ON(!PageSwapBacked(page_tail));
1702
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001703 lru_add_page_tail(page, page_tail, lruvec, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001704 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001705 atomic_sub(tail_count, &page->_count);
1706 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001707
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001708 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001709
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001710 ClearPageCompound(page);
1711 compound_unlock(page);
1712 spin_unlock_irq(&zone->lru_lock);
1713
1714 for (i = 1; i < HPAGE_PMD_NR; i++) {
1715 struct page *page_tail = page + i;
1716 BUG_ON(page_count(page_tail) <= 0);
1717 /*
1718 * Tail pages may be freed if there wasn't any mapping
1719 * like if add_to_swap() is running on a lru page that
1720 * had its mapping zapped. And freeing these pages
1721 * requires taking the lru_lock so we do the put_page
1722 * of the tail pages after the split is complete.
1723 */
1724 put_page(page_tail);
1725 }
1726
1727 /*
1728 * Only the head page (now become a regular page) is required
1729 * to be pinned by the caller.
1730 */
1731 BUG_ON(page_count(page) <= 0);
1732}
1733
1734static int __split_huge_page_map(struct page *page,
1735 struct vm_area_struct *vma,
1736 unsigned long address)
1737{
1738 struct mm_struct *mm = vma->vm_mm;
1739 pmd_t *pmd, _pmd;
1740 int ret = 0, i;
1741 pgtable_t pgtable;
1742 unsigned long haddr;
1743
1744 spin_lock(&mm->page_table_lock);
1745 pmd = page_check_address_pmd(page, mm, address,
1746 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1747 if (pmd) {
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001748 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001749 pmd_populate(mm, &_pmd, pgtable);
1750
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001751 haddr = address;
1752 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001753 pte_t *pte, entry;
1754 BUG_ON(PageCompound(page+i));
1755 entry = mk_pte(page + i, vma->vm_page_prot);
1756 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1757 if (!pmd_write(*pmd))
1758 entry = pte_wrprotect(entry);
1759 else
1760 BUG_ON(page_mapcount(page) != 1);
1761 if (!pmd_young(*pmd))
1762 entry = pte_mkold(entry);
Andrea Arcangeli1ba6e0b2012-10-04 01:51:06 +02001763 if (pmd_numa(*pmd))
1764 entry = pte_mknuma(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001765 pte = pte_offset_map(&_pmd, haddr);
1766 BUG_ON(!pte_none(*pte));
1767 set_pte_at(mm, haddr, pte, entry);
1768 pte_unmap(pte);
1769 }
1770
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001771 smp_wmb(); /* make pte visible before pmd */
1772 /*
1773 * Up to this point the pmd is present and huge and
1774 * userland has the whole access to the hugepage
1775 * during the split (which happens in place). If we
1776 * overwrite the pmd with the not-huge version
1777 * pointing to the pte here (which of course we could
1778 * if all CPUs were bug free), userland could trigger
1779 * a small page size TLB miss on the small sized TLB
1780 * while the hugepage TLB entry is still established
1781 * in the huge TLB. Some CPU doesn't like that. See
1782 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1783 * Erratum 383 on page 93. Intel should be safe but is
1784 * also warns that it's only safe if the permission
1785 * and cache attributes of the two entries loaded in
1786 * the two TLB is identical (which should be the case
1787 * here). But it is generally safer to never allow
1788 * small and huge TLB entries for the same virtual
1789 * address to be loaded simultaneously. So instead of
1790 * doing "pmd_populate(); flush_tlb_range();" we first
1791 * mark the current pmd notpresent (atomically because
1792 * here the pmd_trans_huge and pmd_trans_splitting
1793 * must remain set at all times on the pmd until the
1794 * split is complete for this pmd), then we flush the
1795 * SMP TLB and finally we write the non-huge version
1796 * of the pmd entry with pmd_populate.
1797 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001798 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001799 pmd_populate(mm, pmd, pgtable);
1800 ret = 1;
1801 }
1802 spin_unlock(&mm->page_table_lock);
1803
1804 return ret;
1805}
1806
Ingo Molnar5a505082012-12-02 19:56:46 +00001807/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001808static void __split_huge_page(struct page *page,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001809 struct anon_vma *anon_vma,
1810 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001811{
1812 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001813 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001814 struct anon_vma_chain *avc;
1815
1816 BUG_ON(!PageHead(page));
1817 BUG_ON(PageTail(page));
1818
1819 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001820 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001821 struct vm_area_struct *vma = avc->vma;
1822 unsigned long addr = vma_address(page, vma);
1823 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001824 mapcount += __split_huge_page_splitting(page, vma, addr);
1825 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001826 /*
1827 * It is critical that new vmas are added to the tail of the
1828 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1829 * and establishes a child pmd before
1830 * __split_huge_page_splitting() freezes the parent pmd (so if
1831 * we fail to prevent copy_huge_pmd() from running until the
1832 * whole __split_huge_page() is complete), we will still see
1833 * the newly established pmd of the child later during the
1834 * walk, to be able to set it as pmd_trans_splitting too.
1835 */
1836 if (mapcount != page_mapcount(page))
1837 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1838 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001839 BUG_ON(mapcount != page_mapcount(page));
1840
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001841 __split_huge_page_refcount(page, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001842
1843 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001844 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001845 struct vm_area_struct *vma = avc->vma;
1846 unsigned long addr = vma_address(page, vma);
1847 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001848 mapcount2 += __split_huge_page_map(page, vma, addr);
1849 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001850 if (mapcount != mapcount2)
1851 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1852 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001853 BUG_ON(mapcount != mapcount2);
1854}
1855
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001856/*
1857 * Split a hugepage into normal pages. This doesn't change the position of head
1858 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1859 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1860 * from the hugepage.
1861 * Return 0 if the hugepage is split successfully otherwise return 1.
1862 */
1863int split_huge_page_to_list(struct page *page, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001864{
1865 struct anon_vma *anon_vma;
1866 int ret = 1;
1867
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001868 BUG_ON(is_huge_zero_page(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001869 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001870
1871 /*
1872 * The caller does not necessarily hold an mmap_sem that would prevent
1873 * the anon_vma disappearing so we first we take a reference to it
1874 * and then lock the anon_vma for write. This is similar to
1875 * page_lock_anon_vma_read except the write lock is taken to serialise
1876 * against parallel split or collapse operations.
1877 */
1878 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001879 if (!anon_vma)
1880 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001881 anon_vma_lock_write(anon_vma);
1882
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001883 ret = 0;
1884 if (!PageCompound(page))
1885 goto out_unlock;
1886
1887 BUG_ON(!PageSwapBacked(page));
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001888 __split_huge_page(page, anon_vma, list);
Andi Kleen81ab4202011-04-14 15:22:06 -07001889 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001890
1891 BUG_ON(PageCompound(page));
1892out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08001893 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08001894 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001895out:
1896 return ret;
1897}
1898
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001899#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001900
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001901int hugepage_madvise(struct vm_area_struct *vma,
1902 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001903{
Gerald Schaefer8e720332012-10-08 16:30:12 -07001904 struct mm_struct *mm = vma->vm_mm;
1905
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001906 switch (advice) {
1907 case MADV_HUGEPAGE:
1908 /*
1909 * Be somewhat over-protective like KSM for now!
1910 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001911 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001912 return -EINVAL;
Gerald Schaefer8e720332012-10-08 16:30:12 -07001913 if (mm->def_flags & VM_NOHUGEPAGE)
1914 return -EINVAL;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001915 *vm_flags &= ~VM_NOHUGEPAGE;
1916 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001917 /*
1918 * If the vma become good for khugepaged to scan,
1919 * register it here without waiting a page fault that
1920 * may not happen any time soon.
1921 */
1922 if (unlikely(khugepaged_enter_vma_merge(vma)))
1923 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001924 break;
1925 case MADV_NOHUGEPAGE:
1926 /*
1927 * Be somewhat over-protective like KSM for now!
1928 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001929 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001930 return -EINVAL;
1931 *vm_flags &= ~VM_HUGEPAGE;
1932 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001933 /*
1934 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1935 * this vma even if we leave the mm registered in khugepaged if
1936 * it got registered before VM_NOHUGEPAGE was set.
1937 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001938 break;
1939 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001940
1941 return 0;
1942}
1943
Andrea Arcangeliba761492011-01-13 15:46:58 -08001944static int __init khugepaged_slab_init(void)
1945{
1946 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1947 sizeof(struct mm_slot),
1948 __alignof__(struct mm_slot), 0, NULL);
1949 if (!mm_slot_cache)
1950 return -ENOMEM;
1951
1952 return 0;
1953}
1954
Andrea Arcangeliba761492011-01-13 15:46:58 -08001955static inline struct mm_slot *alloc_mm_slot(void)
1956{
1957 if (!mm_slot_cache) /* initialization failed */
1958 return NULL;
1959 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1960}
1961
1962static inline void free_mm_slot(struct mm_slot *mm_slot)
1963{
1964 kmem_cache_free(mm_slot_cache, mm_slot);
1965}
1966
Andrea Arcangeliba761492011-01-13 15:46:58 -08001967static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1968{
1969 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001970
Sasha Levinb67bfe02013-02-27 17:06:00 -08001971 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001972 if (mm == mm_slot->mm)
1973 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001974
Andrea Arcangeliba761492011-01-13 15:46:58 -08001975 return NULL;
1976}
1977
1978static void insert_to_mm_slots_hash(struct mm_struct *mm,
1979 struct mm_slot *mm_slot)
1980{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001981 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001982 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001983}
1984
1985static inline int khugepaged_test_exit(struct mm_struct *mm)
1986{
1987 return atomic_read(&mm->mm_users) == 0;
1988}
1989
1990int __khugepaged_enter(struct mm_struct *mm)
1991{
1992 struct mm_slot *mm_slot;
1993 int wakeup;
1994
1995 mm_slot = alloc_mm_slot();
1996 if (!mm_slot)
1997 return -ENOMEM;
1998
1999 /* __khugepaged_exit() must not run from under us */
2000 VM_BUG_ON(khugepaged_test_exit(mm));
2001 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
2002 free_mm_slot(mm_slot);
2003 return 0;
2004 }
2005
2006 spin_lock(&khugepaged_mm_lock);
2007 insert_to_mm_slots_hash(mm, mm_slot);
2008 /*
2009 * Insert just behind the scanning cursor, to let the area settle
2010 * down a little.
2011 */
2012 wakeup = list_empty(&khugepaged_scan.mm_head);
2013 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
2014 spin_unlock(&khugepaged_mm_lock);
2015
2016 atomic_inc(&mm->mm_count);
2017 if (wakeup)
2018 wake_up_interruptible(&khugepaged_wait);
2019
2020 return 0;
2021}
2022
2023int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
2024{
2025 unsigned long hstart, hend;
2026 if (!vma->anon_vma)
2027 /*
2028 * Not yet faulted in so we will register later in the
2029 * page fault if needed.
2030 */
2031 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002032 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002033 /* khugepaged not yet working on file or special mappings */
2034 return 0;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002035 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002036 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2037 hend = vma->vm_end & HPAGE_PMD_MASK;
2038 if (hstart < hend)
2039 return khugepaged_enter(vma);
2040 return 0;
2041}
2042
2043void __khugepaged_exit(struct mm_struct *mm)
2044{
2045 struct mm_slot *mm_slot;
2046 int free = 0;
2047
2048 spin_lock(&khugepaged_mm_lock);
2049 mm_slot = get_mm_slot(mm);
2050 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002051 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002052 list_del(&mm_slot->mm_node);
2053 free = 1;
2054 }
Chris Wrightd788e802011-07-25 17:12:14 -07002055 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002056
2057 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002058 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2059 free_mm_slot(mm_slot);
2060 mmdrop(mm);
2061 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002062 /*
2063 * This is required to serialize against
2064 * khugepaged_test_exit() (which is guaranteed to run
2065 * under mmap sem read mode). Stop here (after we
2066 * return all pagetables will be destroyed) until
2067 * khugepaged has finished working on the pagetables
2068 * under the mmap_sem.
2069 */
2070 down_write(&mm->mmap_sem);
2071 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002072 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002073}
2074
2075static void release_pte_page(struct page *page)
2076{
2077 /* 0 stands for page_is_file_cache(page) == false */
2078 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2079 unlock_page(page);
2080 putback_lru_page(page);
2081}
2082
2083static void release_pte_pages(pte_t *pte, pte_t *_pte)
2084{
2085 while (--_pte >= pte) {
2086 pte_t pteval = *_pte;
2087 if (!pte_none(pteval))
2088 release_pte_page(pte_page(pteval));
2089 }
2090}
2091
Andrea Arcangeliba761492011-01-13 15:46:58 -08002092static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2093 unsigned long address,
2094 pte_t *pte)
2095{
2096 struct page *page;
2097 pte_t *_pte;
Bob Liu344aa352012-12-11 16:00:34 -08002098 int referenced = 0, none = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002099 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2100 _pte++, address += PAGE_SIZE) {
2101 pte_t pteval = *_pte;
2102 if (pte_none(pteval)) {
2103 if (++none <= khugepaged_max_ptes_none)
2104 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002105 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002106 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002107 }
Bob Liu344aa352012-12-11 16:00:34 -08002108 if (!pte_present(pteval) || !pte_write(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002109 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002110 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002111 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002113
Andrea Arcangeliba761492011-01-13 15:46:58 -08002114 VM_BUG_ON(PageCompound(page));
2115 BUG_ON(!PageAnon(page));
2116 VM_BUG_ON(!PageSwapBacked(page));
2117
2118 /* cannot use mapcount: can't collapse if there's a gup pin */
Bob Liu344aa352012-12-11 16:00:34 -08002119 if (page_count(page) != 1)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002120 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002121 /*
2122 * We can do it before isolate_lru_page because the
2123 * page can't be freed from under us. NOTE: PG_lock
2124 * is needed to serialize against split_huge_page
2125 * when invoked from the VM.
2126 */
Bob Liu344aa352012-12-11 16:00:34 -08002127 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002128 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002129 /*
2130 * Isolate the page to avoid collapsing an hugepage
2131 * currently in use by the VM.
2132 */
2133 if (isolate_lru_page(page)) {
2134 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002135 goto out;
2136 }
2137 /* 0 stands for page_is_file_cache(page) == false */
2138 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2139 VM_BUG_ON(!PageLocked(page));
2140 VM_BUG_ON(PageLRU(page));
2141
2142 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002143 if (pte_young(pteval) || PageReferenced(page) ||
2144 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002145 referenced = 1;
2146 }
Bob Liu344aa352012-12-11 16:00:34 -08002147 if (likely(referenced))
2148 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002149out:
Bob Liu344aa352012-12-11 16:00:34 -08002150 release_pte_pages(pte, _pte);
2151 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002152}
2153
2154static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2155 struct vm_area_struct *vma,
2156 unsigned long address,
2157 spinlock_t *ptl)
2158{
2159 pte_t *_pte;
2160 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2161 pte_t pteval = *_pte;
2162 struct page *src_page;
2163
2164 if (pte_none(pteval)) {
2165 clear_user_highpage(page, address);
2166 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2167 } else {
2168 src_page = pte_page(pteval);
2169 copy_user_highpage(page, src_page, address, vma);
2170 VM_BUG_ON(page_mapcount(src_page) != 1);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002171 release_pte_page(src_page);
2172 /*
2173 * ptl mostly unnecessary, but preempt has to
2174 * be disabled to update the per-cpu stats
2175 * inside page_remove_rmap().
2176 */
2177 spin_lock(ptl);
2178 /*
2179 * paravirt calls inside pte_clear here are
2180 * superfluous.
2181 */
2182 pte_clear(vma->vm_mm, address, _pte);
2183 page_remove_rmap(src_page);
2184 spin_unlock(ptl);
2185 free_page_and_swap_cache(src_page);
2186 }
2187
2188 address += PAGE_SIZE;
2189 page++;
2190 }
2191}
2192
Xiao Guangrong26234f32012-10-08 16:29:51 -07002193static void khugepaged_alloc_sleep(void)
2194{
2195 wait_event_freezable_timeout(khugepaged_wait, false,
2196 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2197}
2198
2199#ifdef CONFIG_NUMA
2200static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2201{
2202 if (IS_ERR(*hpage)) {
2203 if (!*wait)
2204 return false;
2205
2206 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002207 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002208 khugepaged_alloc_sleep();
2209 } else if (*hpage) {
2210 put_page(*hpage);
2211 *hpage = NULL;
2212 }
2213
2214 return true;
2215}
2216
2217static struct page
2218*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2219 struct vm_area_struct *vma, unsigned long address,
2220 int node)
2221{
2222 VM_BUG_ON(*hpage);
2223 /*
2224 * Allocate the page while the vma is still valid and under
2225 * the mmap_sem read mode so there is no memory allocation
2226 * later when we take the mmap_sem in write mode. This is more
2227 * friendly behavior (OTOH it may actually hide bugs) to
2228 * filesystems in userland with daemons allocating memory in
2229 * the userland I/O paths. Allocating memory with the
2230 * mmap_sem in read mode is good idea also to allow greater
2231 * scalability.
2232 */
2233 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2234 node, __GFP_OTHER_NODE);
2235
2236 /*
2237 * After allocating the hugepage, release the mmap_sem read lock in
2238 * preparation for taking it in write mode.
2239 */
2240 up_read(&mm->mmap_sem);
2241 if (unlikely(!*hpage)) {
2242 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2243 *hpage = ERR_PTR(-ENOMEM);
2244 return NULL;
2245 }
2246
2247 count_vm_event(THP_COLLAPSE_ALLOC);
2248 return *hpage;
2249}
2250#else
2251static struct page *khugepaged_alloc_hugepage(bool *wait)
2252{
2253 struct page *hpage;
2254
2255 do {
2256 hpage = alloc_hugepage(khugepaged_defrag());
2257 if (!hpage) {
2258 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2259 if (!*wait)
2260 return NULL;
2261
2262 *wait = false;
2263 khugepaged_alloc_sleep();
2264 } else
2265 count_vm_event(THP_COLLAPSE_ALLOC);
2266 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2267
2268 return hpage;
2269}
2270
2271static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2272{
2273 if (!*hpage)
2274 *hpage = khugepaged_alloc_hugepage(wait);
2275
2276 if (unlikely(!*hpage))
2277 return false;
2278
2279 return true;
2280}
2281
2282static struct page
2283*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2284 struct vm_area_struct *vma, unsigned long address,
2285 int node)
2286{
2287 up_read(&mm->mmap_sem);
2288 VM_BUG_ON(!*hpage);
2289 return *hpage;
2290}
2291#endif
2292
Bob Liufa475e52012-12-11 16:00:39 -08002293static bool hugepage_vma_check(struct vm_area_struct *vma)
2294{
2295 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2296 (vma->vm_flags & VM_NOHUGEPAGE))
2297 return false;
2298
2299 if (!vma->anon_vma || vma->vm_ops)
2300 return false;
2301 if (is_vma_temporary_stack(vma))
2302 return false;
2303 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2304 return true;
2305}
2306
Andrea Arcangeliba761492011-01-13 15:46:58 -08002307static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002308 unsigned long address,
2309 struct page **hpage,
2310 struct vm_area_struct *vma,
2311 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002313 pmd_t *pmd, _pmd;
2314 pte_t *pte;
2315 pgtable_t pgtable;
2316 struct page *new_page;
2317 spinlock_t *ptl;
2318 int isolated;
2319 unsigned long hstart, hend;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002320 unsigned long mmun_start; /* For mmu_notifiers */
2321 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002322
2323 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002324
Xiao Guangrong26234f32012-10-08 16:29:51 -07002325 /* release the mmap_sem read lock. */
2326 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2327 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002328 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002329
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002330 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002331 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002332
2333 /*
2334 * Prevent all access to pagetables with the exception of
2335 * gup_fast later hanlded by the ptep_clear_flush and the VM
2336 * handled by the anon_vma lock + PG_lock.
2337 */
2338 down_write(&mm->mmap_sem);
2339 if (unlikely(khugepaged_test_exit(mm)))
2340 goto out;
2341
2342 vma = find_vma(mm, address);
Libina8f531eb2013-09-11 14:20:38 -07002343 if (!vma)
2344 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002345 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2346 hend = vma->vm_end & HPAGE_PMD_MASK;
2347 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2348 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002349 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002350 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002351 pmd = mm_find_pmd(mm, address);
2352 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002353 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002354 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002355 goto out;
2356
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002357 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358
2359 pte = pte_offset_map(pmd, address);
2360 ptl = pte_lockptr(mm, pmd);
2361
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002362 mmun_start = address;
2363 mmun_end = address + HPAGE_PMD_SIZE;
2364 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002365 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2366 /*
2367 * After this gup_fast can't run anymore. This also removes
2368 * any huge TLB entry from the CPU so we won't allow
2369 * huge and small TLB entries for the same virtual address
2370 * to avoid the risk of CPU bugs in that area.
2371 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002372 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002373 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002374 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002375
2376 spin_lock(ptl);
2377 isolated = __collapse_huge_page_isolate(vma, address, pte);
2378 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002379
2380 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002381 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002382 spin_lock(&mm->page_table_lock);
2383 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002384 /*
2385 * We can only use set_pmd_at when establishing
2386 * hugepmds and never for establishing regular pmds that
2387 * points to regular pagetables. Use pmd_populate for that
2388 */
2389 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002390 spin_unlock(&mm->page_table_lock);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002391 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002392 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393 }
2394
2395 /*
2396 * All pages are isolated and locked so anon_vma rmap
2397 * can't run anymore.
2398 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002399 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002400
2401 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002402 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002403 __SetPageUptodate(new_page);
2404 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002405
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002406 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2407 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002408
2409 /*
2410 * spin_lock() below is not the equivalent of smp_wmb(), so
2411 * this is needed to avoid the copy_huge_page writes to become
2412 * visible after the set_pmd_at() write.
2413 */
2414 smp_wmb();
2415
2416 spin_lock(&mm->page_table_lock);
2417 BUG_ON(!pmd_none(*pmd));
2418 page_add_new_anon_rmap(new_page, vma, address);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002419 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002420 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002421 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002422 spin_unlock(&mm->page_table_lock);
2423
2424 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002425
Andrea Arcangeliba761492011-01-13 15:46:58 -08002426 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002427out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002428 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002429 return;
2430
Andrea Arcangelice83d212011-01-13 15:47:06 -08002431out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002432 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002433 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002434}
2435
2436static int khugepaged_scan_pmd(struct mm_struct *mm,
2437 struct vm_area_struct *vma,
2438 unsigned long address,
2439 struct page **hpage)
2440{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002441 pmd_t *pmd;
2442 pte_t *pte, *_pte;
2443 int ret = 0, referenced = 0, none = 0;
2444 struct page *page;
2445 unsigned long _address;
2446 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002447 int node = NUMA_NO_NODE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002448
2449 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2450
Bob Liu62190492012-12-11 16:00:37 -08002451 pmd = mm_find_pmd(mm, address);
2452 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002453 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002454 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002455 goto out;
2456
2457 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2458 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2459 _pte++, _address += PAGE_SIZE) {
2460 pte_t pteval = *_pte;
2461 if (pte_none(pteval)) {
2462 if (++none <= khugepaged_max_ptes_none)
2463 continue;
2464 else
2465 goto out_unmap;
2466 }
2467 if (!pte_present(pteval) || !pte_write(pteval))
2468 goto out_unmap;
2469 page = vm_normal_page(vma, _address, pteval);
2470 if (unlikely(!page))
2471 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002472 /*
2473 * Chose the node of the first page. This could
2474 * be more sophisticated and look at more pages,
2475 * but isn't for now.
2476 */
David Rientjes00ef2d22013-02-22 16:35:36 -08002477 if (node == NUMA_NO_NODE)
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002478 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002479 VM_BUG_ON(PageCompound(page));
2480 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2481 goto out_unmap;
2482 /* cannot use mapcount: can't collapse if there's a gup pin */
2483 if (page_count(page) != 1)
2484 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002485 if (pte_young(pteval) || PageReferenced(page) ||
2486 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002487 referenced = 1;
2488 }
2489 if (referenced)
2490 ret = 1;
2491out_unmap:
2492 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002493 if (ret)
2494 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002495 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002496out:
2497 return ret;
2498}
2499
2500static void collect_mm_slot(struct mm_slot *mm_slot)
2501{
2502 struct mm_struct *mm = mm_slot->mm;
2503
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002504 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002505
2506 if (khugepaged_test_exit(mm)) {
2507 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002508 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002509 list_del(&mm_slot->mm_node);
2510
2511 /*
2512 * Not strictly needed because the mm exited already.
2513 *
2514 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2515 */
2516
2517 /* khugepaged_mm_lock actually not necessary for the below */
2518 free_mm_slot(mm_slot);
2519 mmdrop(mm);
2520 }
2521}
2522
2523static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2524 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002525 __releases(&khugepaged_mm_lock)
2526 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002527{
2528 struct mm_slot *mm_slot;
2529 struct mm_struct *mm;
2530 struct vm_area_struct *vma;
2531 int progress = 0;
2532
2533 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002534 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535
2536 if (khugepaged_scan.mm_slot)
2537 mm_slot = khugepaged_scan.mm_slot;
2538 else {
2539 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2540 struct mm_slot, mm_node);
2541 khugepaged_scan.address = 0;
2542 khugepaged_scan.mm_slot = mm_slot;
2543 }
2544 spin_unlock(&khugepaged_mm_lock);
2545
2546 mm = mm_slot->mm;
2547 down_read(&mm->mmap_sem);
2548 if (unlikely(khugepaged_test_exit(mm)))
2549 vma = NULL;
2550 else
2551 vma = find_vma(mm, khugepaged_scan.address);
2552
2553 progress++;
2554 for (; vma; vma = vma->vm_next) {
2555 unsigned long hstart, hend;
2556
2557 cond_resched();
2558 if (unlikely(khugepaged_test_exit(mm))) {
2559 progress++;
2560 break;
2561 }
Bob Liufa475e52012-12-11 16:00:39 -08002562 if (!hugepage_vma_check(vma)) {
2563skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002564 progress++;
2565 continue;
2566 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002567 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2568 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002569 if (hstart >= hend)
2570 goto skip;
2571 if (khugepaged_scan.address > hend)
2572 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002573 if (khugepaged_scan.address < hstart)
2574 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002575 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576
2577 while (khugepaged_scan.address < hend) {
2578 int ret;
2579 cond_resched();
2580 if (unlikely(khugepaged_test_exit(mm)))
2581 goto breakouterloop;
2582
2583 VM_BUG_ON(khugepaged_scan.address < hstart ||
2584 khugepaged_scan.address + HPAGE_PMD_SIZE >
2585 hend);
2586 ret = khugepaged_scan_pmd(mm, vma,
2587 khugepaged_scan.address,
2588 hpage);
2589 /* move to next address */
2590 khugepaged_scan.address += HPAGE_PMD_SIZE;
2591 progress += HPAGE_PMD_NR;
2592 if (ret)
2593 /* we released mmap_sem so break loop */
2594 goto breakouterloop_mmap_sem;
2595 if (progress >= pages)
2596 goto breakouterloop;
2597 }
2598 }
2599breakouterloop:
2600 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2601breakouterloop_mmap_sem:
2602
2603 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002604 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002605 /*
2606 * Release the current mm_slot if this mm is about to die, or
2607 * if we scanned all vmas of this mm.
2608 */
2609 if (khugepaged_test_exit(mm) || !vma) {
2610 /*
2611 * Make sure that if mm_users is reaching zero while
2612 * khugepaged runs here, khugepaged_exit will find
2613 * mm_slot not pointing to the exiting mm.
2614 */
2615 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2616 khugepaged_scan.mm_slot = list_entry(
2617 mm_slot->mm_node.next,
2618 struct mm_slot, mm_node);
2619 khugepaged_scan.address = 0;
2620 } else {
2621 khugepaged_scan.mm_slot = NULL;
2622 khugepaged_full_scans++;
2623 }
2624
2625 collect_mm_slot(mm_slot);
2626 }
2627
2628 return progress;
2629}
2630
2631static int khugepaged_has_work(void)
2632{
2633 return !list_empty(&khugepaged_scan.mm_head) &&
2634 khugepaged_enabled();
2635}
2636
2637static int khugepaged_wait_event(void)
2638{
2639 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002640 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002641}
2642
Xiao Guangrongd5169042012-10-08 16:29:48 -07002643static void khugepaged_do_scan(void)
2644{
2645 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002646 unsigned int progress = 0, pass_through_head = 0;
2647 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002648 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002649
2650 barrier(); /* write khugepaged_pages_to_scan to local stack */
2651
2652 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002653 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002654 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002655
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002656 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002657
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002658 if (unlikely(kthread_should_stop() || freezing(current)))
2659 break;
2660
Andrea Arcangeliba761492011-01-13 15:46:58 -08002661 spin_lock(&khugepaged_mm_lock);
2662 if (!khugepaged_scan.mm_slot)
2663 pass_through_head++;
2664 if (khugepaged_has_work() &&
2665 pass_through_head < 2)
2666 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002667 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002668 else
2669 progress = pages;
2670 spin_unlock(&khugepaged_mm_lock);
2671 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002672
Xiao Guangrongd5169042012-10-08 16:29:48 -07002673 if (!IS_ERR_OR_NULL(hpage))
2674 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002675}
2676
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002677static void khugepaged_wait_work(void)
2678{
2679 try_to_freeze();
2680
2681 if (khugepaged_has_work()) {
2682 if (!khugepaged_scan_sleep_millisecs)
2683 return;
2684
2685 wait_event_freezable_timeout(khugepaged_wait,
2686 kthread_should_stop(),
2687 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2688 return;
2689 }
2690
2691 if (khugepaged_enabled())
2692 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2693}
2694
Andrea Arcangeliba761492011-01-13 15:46:58 -08002695static int khugepaged(void *none)
2696{
2697 struct mm_slot *mm_slot;
2698
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002699 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002700 set_user_nice(current, 19);
2701
Xiao Guangrongb7231782012-10-08 16:29:54 -07002702 while (!kthread_should_stop()) {
2703 khugepaged_do_scan();
2704 khugepaged_wait_work();
2705 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002706
2707 spin_lock(&khugepaged_mm_lock);
2708 mm_slot = khugepaged_scan.mm_slot;
2709 khugepaged_scan.mm_slot = NULL;
2710 if (mm_slot)
2711 collect_mm_slot(mm_slot);
2712 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002713 return 0;
2714}
2715
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002716static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2717 unsigned long haddr, pmd_t *pmd)
2718{
2719 struct mm_struct *mm = vma->vm_mm;
2720 pgtable_t pgtable;
2721 pmd_t _pmd;
2722 int i;
2723
2724 pmdp_clear_flush(vma, haddr, pmd);
2725 /* leave pmd empty until pte is filled */
2726
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07002727 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002728 pmd_populate(mm, &_pmd, pgtable);
2729
2730 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2731 pte_t *pte, entry;
2732 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2733 entry = pte_mkspecial(entry);
2734 pte = pte_offset_map(&_pmd, haddr);
2735 VM_BUG_ON(!pte_none(*pte));
2736 set_pte_at(mm, haddr, pte, entry);
2737 pte_unmap(pte);
2738 }
2739 smp_wmb(); /* make pte visible before pmd */
2740 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002741 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002742}
2743
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002744void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2745 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002746{
2747 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002748 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002749 unsigned long haddr = address & HPAGE_PMD_MASK;
2750 unsigned long mmun_start; /* For mmu_notifiers */
2751 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002752
2753 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002754
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002755 mmun_start = haddr;
2756 mmun_end = haddr + HPAGE_PMD_SIZE;
2757 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002758 spin_lock(&mm->page_table_lock);
2759 if (unlikely(!pmd_trans_huge(*pmd))) {
2760 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002761 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2762 return;
2763 }
2764 if (is_huge_zero_pmd(*pmd)) {
2765 __split_huge_zero_page_pmd(vma, haddr, pmd);
2766 spin_unlock(&mm->page_table_lock);
2767 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002768 return;
2769 }
2770 page = pmd_page(*pmd);
2771 VM_BUG_ON(!page_count(page));
2772 get_page(page);
2773 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002774 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002775
2776 split_huge_page(page);
2777
2778 put_page(page);
2779 BUG_ON(pmd_trans_huge(*pmd));
2780}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002781
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002782void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2783 pmd_t *pmd)
2784{
2785 struct vm_area_struct *vma;
2786
2787 vma = find_vma(mm, address);
2788 BUG_ON(vma == NULL);
2789 split_huge_page_pmd(vma, address, pmd);
2790}
2791
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002792static void split_huge_page_address(struct mm_struct *mm,
2793 unsigned long address)
2794{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002795 pmd_t *pmd;
2796
2797 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2798
Bob Liu62190492012-12-11 16:00:37 -08002799 pmd = mm_find_pmd(mm, address);
2800 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002801 return;
2802 /*
2803 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2804 * materialize from under us.
2805 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002806 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002807}
2808
2809void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2810 unsigned long start,
2811 unsigned long end,
2812 long adjust_next)
2813{
2814 /*
2815 * If the new start address isn't hpage aligned and it could
2816 * previously contain an hugepage: check if we need to split
2817 * an huge pmd.
2818 */
2819 if (start & ~HPAGE_PMD_MASK &&
2820 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2821 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2822 split_huge_page_address(vma->vm_mm, start);
2823
2824 /*
2825 * If the new end address isn't hpage aligned and it could
2826 * previously contain an hugepage: check if we need to split
2827 * an huge pmd.
2828 */
2829 if (end & ~HPAGE_PMD_MASK &&
2830 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2831 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2832 split_huge_page_address(vma->vm_mm, end);
2833
2834 /*
2835 * If we're also updating the vma->vm_next->vm_start, if the new
2836 * vm_next->vm_start isn't page aligned and it could previously
2837 * contain an hugepage: check if we need to split an huge pmd.
2838 */
2839 if (adjust_next > 0) {
2840 struct vm_area_struct *next = vma->vm_next;
2841 unsigned long nstart = next->vm_start;
2842 nstart += adjust_next << PAGE_SHIFT;
2843 if (nstart & ~HPAGE_PMD_MASK &&
2844 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2845 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2846 split_huge_page_address(next->vm_mm, nstart);
2847 }
2848}