blob: b1cc6591ed8341bbf5183d5c89aafd60d9fd77c1 [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;
108 extern int min_free_kbytes;
109
Xiao Guangrong17c230a2012-10-08 16:29:56 -0700110 if (!khugepaged_enabled())
Andrea Arcangelif0005652011-01-13 15:47:04 -0800111 return 0;
112
113 for_each_populated_zone(zone)
114 nr_zones++;
115
116 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
117 recommended_min = pageblock_nr_pages * nr_zones * 2;
118
119 /*
120 * Make sure that on average at least two pageblocks are almost free
121 * of another type, one for a migratetype to fall back to and a
122 * second to avoid subsequent fallbacks of other types There are 3
123 * MIGRATE_TYPES we care about.
124 */
125 recommended_min += pageblock_nr_pages * nr_zones *
126 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
127
128 /* don't ever allow to reserve more than 5% of the lowmem */
129 recommended_min = min(recommended_min,
130 (unsigned long) nr_free_buffer_pages() / 20);
131 recommended_min <<= (PAGE_SHIFT-10);
132
133 if (recommended_min > min_free_kbytes)
134 min_free_kbytes = recommended_min;
135 setup_per_zone_wmarks();
136 return 0;
137}
138late_initcall(set_recommended_min_free_kbytes);
139
Andrea Arcangeliba761492011-01-13 15:46:58 -0800140static int start_khugepaged(void)
141{
142 int err = 0;
143 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800144 if (!khugepaged_thread)
145 khugepaged_thread = kthread_run(khugepaged, NULL,
146 "khugepaged");
147 if (unlikely(IS_ERR(khugepaged_thread))) {
148 printk(KERN_ERR
149 "khugepaged: kthread_run(khugepaged) failed\n");
150 err = PTR_ERR(khugepaged_thread);
151 khugepaged_thread = NULL;
152 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700153
154 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800155 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800156
157 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700158 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700159 kthread_stop(khugepaged_thread);
160 khugepaged_thread = NULL;
161 }
Xiao Guangrong637e3a22012-10-08 16:29:38 -0700162
Andrea Arcangeliba761492011-01-13 15:46:58 -0800163 return err;
164}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800165
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800166static atomic_t huge_zero_refcount;
167static unsigned long huge_zero_pfn __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800168
169static inline bool is_huge_zero_pfn(unsigned long pfn)
170{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800171 unsigned long zero_pfn = ACCESS_ONCE(huge_zero_pfn);
172 return zero_pfn && pfn == zero_pfn;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800173}
174
175static inline bool is_huge_zero_pmd(pmd_t pmd)
176{
177 return is_huge_zero_pfn(pmd_pfn(pmd));
178}
179
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800180static unsigned long get_huge_zero_page(void)
181{
182 struct page *zero_page;
183retry:
184 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
185 return ACCESS_ONCE(huge_zero_pfn);
186
187 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
188 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800189 if (!zero_page) {
190 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800191 return 0;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800192 }
193 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800194 preempt_disable();
195 if (cmpxchg(&huge_zero_pfn, 0, page_to_pfn(zero_page))) {
196 preempt_enable();
197 __free_page(zero_page);
198 goto retry;
199 }
200
201 /* We take additional reference here. It will be put back by shrinker */
202 atomic_set(&huge_zero_refcount, 2);
203 preempt_enable();
204 return ACCESS_ONCE(huge_zero_pfn);
205}
206
207static void put_huge_zero_page(void)
208{
209 /*
210 * Counter should never go to zero here. Only shrinker can put
211 * last reference.
212 */
213 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
214}
215
216static int shrink_huge_zero_page(struct shrinker *shrink,
217 struct shrink_control *sc)
218{
219 if (!sc->nr_to_scan)
220 /* we can free zero page only if last reference remains */
221 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
222
223 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
224 unsigned long zero_pfn = xchg(&huge_zero_pfn, 0);
225 BUG_ON(zero_pfn == 0);
226 __free_page(__pfn_to_page(zero_pfn));
227 }
228
229 return 0;
230}
231
232static struct shrinker huge_zero_page_shrinker = {
233 .shrink = shrink_huge_zero_page,
234 .seeks = DEFAULT_SEEKS,
235};
236
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800237#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800238
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800239static ssize_t double_flag_show(struct kobject *kobj,
240 struct kobj_attribute *attr, char *buf,
241 enum transparent_hugepage_flag enabled,
242 enum transparent_hugepage_flag req_madv)
243{
244 if (test_bit(enabled, &transparent_hugepage_flags)) {
245 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
246 return sprintf(buf, "[always] madvise never\n");
247 } else if (test_bit(req_madv, &transparent_hugepage_flags))
248 return sprintf(buf, "always [madvise] never\n");
249 else
250 return sprintf(buf, "always madvise [never]\n");
251}
252static ssize_t double_flag_store(struct kobject *kobj,
253 struct kobj_attribute *attr,
254 const char *buf, size_t count,
255 enum transparent_hugepage_flag enabled,
256 enum transparent_hugepage_flag req_madv)
257{
258 if (!memcmp("always", buf,
259 min(sizeof("always")-1, count))) {
260 set_bit(enabled, &transparent_hugepage_flags);
261 clear_bit(req_madv, &transparent_hugepage_flags);
262 } else if (!memcmp("madvise", buf,
263 min(sizeof("madvise")-1, count))) {
264 clear_bit(enabled, &transparent_hugepage_flags);
265 set_bit(req_madv, &transparent_hugepage_flags);
266 } else if (!memcmp("never", buf,
267 min(sizeof("never")-1, count))) {
268 clear_bit(enabled, &transparent_hugepage_flags);
269 clear_bit(req_madv, &transparent_hugepage_flags);
270 } else
271 return -EINVAL;
272
273 return count;
274}
275
276static ssize_t enabled_show(struct kobject *kobj,
277 struct kobj_attribute *attr, char *buf)
278{
279 return double_flag_show(kobj, attr, buf,
280 TRANSPARENT_HUGEPAGE_FLAG,
281 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
282}
283static ssize_t enabled_store(struct kobject *kobj,
284 struct kobj_attribute *attr,
285 const char *buf, size_t count)
286{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800287 ssize_t ret;
288
289 ret = double_flag_store(kobj, attr, buf, count,
290 TRANSPARENT_HUGEPAGE_FLAG,
291 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
292
293 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700294 int err;
295
296 mutex_lock(&khugepaged_mutex);
297 err = start_khugepaged();
298 mutex_unlock(&khugepaged_mutex);
299
Andrea Arcangeliba761492011-01-13 15:46:58 -0800300 if (err)
301 ret = err;
302 }
303
304 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800305}
306static struct kobj_attribute enabled_attr =
307 __ATTR(enabled, 0644, enabled_show, enabled_store);
308
309static ssize_t single_flag_show(struct kobject *kobj,
310 struct kobj_attribute *attr, char *buf,
311 enum transparent_hugepage_flag flag)
312{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700313 return sprintf(buf, "%d\n",
314 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800315}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700316
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800317static ssize_t single_flag_store(struct kobject *kobj,
318 struct kobj_attribute *attr,
319 const char *buf, size_t count,
320 enum transparent_hugepage_flag flag)
321{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700322 unsigned long value;
323 int ret;
324
325 ret = kstrtoul(buf, 10, &value);
326 if (ret < 0)
327 return ret;
328 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800329 return -EINVAL;
330
Ben Hutchingse27e6152011-04-14 15:22:21 -0700331 if (value)
332 set_bit(flag, &transparent_hugepage_flags);
333 else
334 clear_bit(flag, &transparent_hugepage_flags);
335
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800336 return count;
337}
338
339/*
340 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
341 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
342 * memory just to allocate one more hugepage.
343 */
344static ssize_t defrag_show(struct kobject *kobj,
345 struct kobj_attribute *attr, char *buf)
346{
347 return double_flag_show(kobj, attr, buf,
348 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
349 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
350}
351static ssize_t defrag_store(struct kobject *kobj,
352 struct kobj_attribute *attr,
353 const char *buf, size_t count)
354{
355 return double_flag_store(kobj, attr, buf, count,
356 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
357 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
358}
359static struct kobj_attribute defrag_attr =
360 __ATTR(defrag, 0644, defrag_show, defrag_store);
361
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800362static ssize_t use_zero_page_show(struct kobject *kobj,
363 struct kobj_attribute *attr, char *buf)
364{
365 return single_flag_show(kobj, attr, buf,
366 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
367}
368static ssize_t use_zero_page_store(struct kobject *kobj,
369 struct kobj_attribute *attr, const char *buf, size_t count)
370{
371 return single_flag_store(kobj, attr, buf, count,
372 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
373}
374static struct kobj_attribute use_zero_page_attr =
375 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800376#ifdef CONFIG_DEBUG_VM
377static ssize_t debug_cow_show(struct kobject *kobj,
378 struct kobj_attribute *attr, char *buf)
379{
380 return single_flag_show(kobj, attr, buf,
381 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
382}
383static ssize_t debug_cow_store(struct kobject *kobj,
384 struct kobj_attribute *attr,
385 const char *buf, size_t count)
386{
387 return single_flag_store(kobj, attr, buf, count,
388 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
389}
390static struct kobj_attribute debug_cow_attr =
391 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
392#endif /* CONFIG_DEBUG_VM */
393
394static struct attribute *hugepage_attr[] = {
395 &enabled_attr.attr,
396 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800397 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800398#ifdef CONFIG_DEBUG_VM
399 &debug_cow_attr.attr,
400#endif
401 NULL,
402};
403
404static struct attribute_group hugepage_attr_group = {
405 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800406};
407
408static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
409 struct kobj_attribute *attr,
410 char *buf)
411{
412 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
413}
414
415static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
416 struct kobj_attribute *attr,
417 const char *buf, size_t count)
418{
419 unsigned long msecs;
420 int err;
421
422 err = strict_strtoul(buf, 10, &msecs);
423 if (err || msecs > UINT_MAX)
424 return -EINVAL;
425
426 khugepaged_scan_sleep_millisecs = msecs;
427 wake_up_interruptible(&khugepaged_wait);
428
429 return count;
430}
431static struct kobj_attribute scan_sleep_millisecs_attr =
432 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
433 scan_sleep_millisecs_store);
434
435static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
436 struct kobj_attribute *attr,
437 char *buf)
438{
439 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
440}
441
442static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 const char *buf, size_t count)
445{
446 unsigned long msecs;
447 int err;
448
449 err = strict_strtoul(buf, 10, &msecs);
450 if (err || msecs > UINT_MAX)
451 return -EINVAL;
452
453 khugepaged_alloc_sleep_millisecs = msecs;
454 wake_up_interruptible(&khugepaged_wait);
455
456 return count;
457}
458static struct kobj_attribute alloc_sleep_millisecs_attr =
459 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
460 alloc_sleep_millisecs_store);
461
462static ssize_t pages_to_scan_show(struct kobject *kobj,
463 struct kobj_attribute *attr,
464 char *buf)
465{
466 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
467}
468static ssize_t pages_to_scan_store(struct kobject *kobj,
469 struct kobj_attribute *attr,
470 const char *buf, size_t count)
471{
472 int err;
473 unsigned long pages;
474
475 err = strict_strtoul(buf, 10, &pages);
476 if (err || !pages || pages > UINT_MAX)
477 return -EINVAL;
478
479 khugepaged_pages_to_scan = pages;
480
481 return count;
482}
483static struct kobj_attribute pages_to_scan_attr =
484 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
485 pages_to_scan_store);
486
487static ssize_t pages_collapsed_show(struct kobject *kobj,
488 struct kobj_attribute *attr,
489 char *buf)
490{
491 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
492}
493static struct kobj_attribute pages_collapsed_attr =
494 __ATTR_RO(pages_collapsed);
495
496static ssize_t full_scans_show(struct kobject *kobj,
497 struct kobj_attribute *attr,
498 char *buf)
499{
500 return sprintf(buf, "%u\n", khugepaged_full_scans);
501}
502static struct kobj_attribute full_scans_attr =
503 __ATTR_RO(full_scans);
504
505static ssize_t khugepaged_defrag_show(struct kobject *kobj,
506 struct kobj_attribute *attr, char *buf)
507{
508 return single_flag_show(kobj, attr, buf,
509 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
510}
511static ssize_t khugepaged_defrag_store(struct kobject *kobj,
512 struct kobj_attribute *attr,
513 const char *buf, size_t count)
514{
515 return single_flag_store(kobj, attr, buf, count,
516 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
517}
518static struct kobj_attribute khugepaged_defrag_attr =
519 __ATTR(defrag, 0644, khugepaged_defrag_show,
520 khugepaged_defrag_store);
521
522/*
523 * max_ptes_none controls if khugepaged should collapse hugepages over
524 * any unmapped ptes in turn potentially increasing the memory
525 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
526 * reduce the available free memory in the system as it
527 * runs. Increasing max_ptes_none will instead potentially reduce the
528 * free memory in the system during the khugepaged scan.
529 */
530static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
531 struct kobj_attribute *attr,
532 char *buf)
533{
534 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
535}
536static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
537 struct kobj_attribute *attr,
538 const char *buf, size_t count)
539{
540 int err;
541 unsigned long max_ptes_none;
542
543 err = strict_strtoul(buf, 10, &max_ptes_none);
544 if (err || max_ptes_none > HPAGE_PMD_NR-1)
545 return -EINVAL;
546
547 khugepaged_max_ptes_none = max_ptes_none;
548
549 return count;
550}
551static struct kobj_attribute khugepaged_max_ptes_none_attr =
552 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
553 khugepaged_max_ptes_none_store);
554
555static struct attribute *khugepaged_attr[] = {
556 &khugepaged_defrag_attr.attr,
557 &khugepaged_max_ptes_none_attr.attr,
558 &pages_to_scan_attr.attr,
559 &pages_collapsed_attr.attr,
560 &full_scans_attr.attr,
561 &scan_sleep_millisecs_attr.attr,
562 &alloc_sleep_millisecs_attr.attr,
563 NULL,
564};
565
566static struct attribute_group khugepaged_attr_group = {
567 .attrs = khugepaged_attr,
568 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800569};
Shaohua Li569e5592012-01-12 17:19:11 -0800570
571static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
572{
573 int err;
574
575 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
576 if (unlikely(!*hugepage_kobj)) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800577 printk(KERN_ERR "hugepage: failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800578 return -ENOMEM;
579 }
580
581 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
582 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800583 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800584 goto delete_obj;
585 }
586
587 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
588 if (err) {
Jeremy Eder2c797372012-12-20 15:05:32 -0800589 printk(KERN_ERR "hugepage: failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800590 goto remove_hp_group;
591 }
592
593 return 0;
594
595remove_hp_group:
596 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
597delete_obj:
598 kobject_put(*hugepage_kobj);
599 return err;
600}
601
602static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
603{
604 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
605 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
606 kobject_put(hugepage_kobj);
607}
608#else
609static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
610{
611 return 0;
612}
613
614static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
615{
616}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800617#endif /* CONFIG_SYSFS */
618
619static int __init hugepage_init(void)
620{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800621 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800622 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800623
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800624 if (!has_transparent_hugepage()) {
625 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800626 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800627 }
628
Shaohua Li569e5592012-01-12 17:19:11 -0800629 err = hugepage_init_sysfs(&hugepage_kobj);
630 if (err)
631 return err;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800632
633 err = khugepaged_slab_init();
634 if (err)
635 goto out;
636
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800637 register_shrinker(&huge_zero_page_shrinker);
638
Rik van Riel97562cd2011-01-13 15:47:12 -0800639 /*
640 * By default disable transparent hugepages on smaller systems,
641 * where the extra memory used could hurt more than TLB overhead
642 * is likely to save. The admin can still enable it through /sys.
643 */
644 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
645 transparent_hugepage_flags = 0;
646
Andrea Arcangeliba761492011-01-13 15:46:58 -0800647 start_khugepaged();
648
Shaohua Li569e5592012-01-12 17:19:11 -0800649 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800650out:
Shaohua Li569e5592012-01-12 17:19:11 -0800651 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800652 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800653}
654module_init(hugepage_init)
655
656static int __init setup_transparent_hugepage(char *str)
657{
658 int ret = 0;
659 if (!str)
660 goto out;
661 if (!strcmp(str, "always")) {
662 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
663 &transparent_hugepage_flags);
664 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
665 &transparent_hugepage_flags);
666 ret = 1;
667 } else if (!strcmp(str, "madvise")) {
668 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
669 &transparent_hugepage_flags);
670 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
671 &transparent_hugepage_flags);
672 ret = 1;
673 } else if (!strcmp(str, "never")) {
674 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
675 &transparent_hugepage_flags);
676 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
677 &transparent_hugepage_flags);
678 ret = 1;
679 }
680out:
681 if (!ret)
682 printk(KERN_WARNING
683 "transparent_hugepage= cannot parse, ignored\n");
684 return ret;
685}
686__setup("transparent_hugepage=", setup_transparent_hugepage);
687
Mel Gormanb32967f2012-11-19 12:35:47 +0000688pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800689{
690 if (likely(vma->vm_flags & VM_WRITE))
691 pmd = pmd_mkwrite(pmd);
692 return pmd;
693}
694
Bob Liub3092b32012-12-11 16:00:41 -0800695static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
696{
697 pmd_t entry;
698 entry = mk_pmd(page, vma->vm_page_prot);
699 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
700 entry = pmd_mkhuge(entry);
701 return entry;
702}
703
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800704static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
705 struct vm_area_struct *vma,
706 unsigned long haddr, pmd_t *pmd,
707 struct page *page)
708{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800709 pgtable_t pgtable;
710
711 VM_BUG_ON(!PageCompound(page));
712 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700713 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800714 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800715
716 clear_huge_page(page, haddr, HPAGE_PMD_NR);
717 __SetPageUptodate(page);
718
719 spin_lock(&mm->page_table_lock);
720 if (unlikely(!pmd_none(*pmd))) {
721 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800722 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800723 put_page(page);
724 pte_free(mm, pgtable);
725 } else {
726 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -0800727 entry = mk_huge_pmd(page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800728 /*
729 * The spinlocking to take the lru_lock inside
730 * page_add_new_anon_rmap() acts as a full memory
731 * barrier to be sure clear_huge_page writes become
732 * visible after the set_pmd_at() write.
733 */
734 page_add_new_anon_rmap(page, vma, haddr);
735 set_pmd_at(mm, haddr, pmd, entry);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700736 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800737 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800738 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800739 spin_unlock(&mm->page_table_lock);
740 }
741
David Rientjesaa2e8782012-05-29 15:06:17 -0700742 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800743}
744
Andi Kleencc5d4622011-03-22 16:33:13 -0700745static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800746{
Andi Kleencc5d4622011-03-22 16:33:13 -0700747 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800748}
749
750static inline struct page *alloc_hugepage_vma(int defrag,
751 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700752 unsigned long haddr, int nd,
753 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800754{
Andi Kleencc5d4622011-03-22 16:33:13 -0700755 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800756 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800757}
758
759#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800760static inline struct page *alloc_hugepage(int defrag)
761{
Andi Kleencc5d4622011-03-22 16:33:13 -0700762 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800763 HPAGE_PMD_ORDER);
764}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800765#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800766
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800767static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800768 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
769 unsigned long zero_pfn)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800770{
771 pmd_t entry;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800772 if (!pmd_none(*pmd))
773 return false;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800774 entry = pfn_pmd(zero_pfn, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800775 entry = pmd_wrprotect(entry);
776 entry = pmd_mkhuge(entry);
777 set_pmd_at(mm, haddr, pmd, entry);
778 pgtable_trans_huge_deposit(mm, pgtable);
779 mm->nr_ptes++;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800780 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800781}
782
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800783int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
784 unsigned long address, pmd_t *pmd,
785 unsigned int flags)
786{
787 struct page *page;
788 unsigned long haddr = address & HPAGE_PMD_MASK;
789 pte_t *pte;
790
791 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
792 if (unlikely(anon_vma_prepare(vma)))
793 return VM_FAULT_OOM;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800794 if (unlikely(khugepaged_enter(vma)))
795 return VM_FAULT_OOM;
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800796 if (!(flags & FAULT_FLAG_WRITE) &&
797 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800798 pgtable_t pgtable;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800799 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800800 bool set;
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800801 pgtable = pte_alloc_one(mm, haddr);
802 if (unlikely(!pgtable))
803 return VM_FAULT_OOM;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800804 zero_pfn = get_huge_zero_page();
805 if (unlikely(!zero_pfn)) {
806 pte_free(mm, pgtable);
807 count_vm_event(THP_FAULT_FALLBACK);
808 goto out;
809 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800810 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800811 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800812 zero_pfn);
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800813 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800814 if (!set) {
815 pte_free(mm, pgtable);
816 put_huge_zero_page();
817 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800818 return 0;
819 }
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800820 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700821 vma, haddr, numa_node_id(), 0);
Andi Kleen81ab4202011-04-14 15:22:06 -0700822 if (unlikely(!page)) {
823 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800824 goto out;
Andi Kleen81ab4202011-04-14 15:22:06 -0700825 }
826 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800827 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
828 put_page(page);
829 goto out;
830 }
David Rientjesedad9d22012-05-29 15:06:17 -0700831 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
832 page))) {
833 mem_cgroup_uncharge_page(page);
834 put_page(page);
835 goto out;
836 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800837
David Rientjesedad9d22012-05-29 15:06:17 -0700838 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800839 }
840out:
841 /*
842 * Use __pte_alloc instead of pte_alloc_map, because we can't
843 * run pte_offset_map on the pmd, if an huge pmd could
844 * materialize from under us from a different thread.
845 */
Mel Gorman4fd01772011-10-12 21:06:51 +0200846 if (unlikely(pmd_none(*pmd)) &&
847 unlikely(__pte_alloc(mm, vma, pmd, address)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800848 return VM_FAULT_OOM;
849 /* if an huge pmd materialized from under us just retry later */
850 if (unlikely(pmd_trans_huge(*pmd)))
851 return 0;
852 /*
853 * A regular pmd is established and it can't morph into a huge pmd
854 * from under us anymore at this point because we hold the mmap_sem
855 * read mode and khugepaged takes it in write mode. So now it's
856 * safe to run pte_offset_map().
857 */
858 pte = pte_offset_map(pmd, address);
859 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
860}
861
862int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
863 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
864 struct vm_area_struct *vma)
865{
866 struct page *src_page;
867 pmd_t pmd;
868 pgtable_t pgtable;
869 int ret;
870
871 ret = -ENOMEM;
872 pgtable = pte_alloc_one(dst_mm, addr);
873 if (unlikely(!pgtable))
874 goto out;
875
876 spin_lock(&dst_mm->page_table_lock);
877 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
878
879 ret = -EAGAIN;
880 pmd = *src_pmd;
881 if (unlikely(!pmd_trans_huge(pmd))) {
882 pte_free(dst_mm, pgtable);
883 goto out_unlock;
884 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800885 /*
886 * mm->page_table_lock is enough to be sure that huge zero pmd is not
887 * under splitting since we don't split the page itself, only pmd to
888 * a page table.
889 */
890 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800891 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800892 bool set;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800893 /*
894 * get_huge_zero_page() will never allocate a new page here,
895 * since we already have a zero page to copy. It just takes a
896 * reference.
897 */
898 zero_pfn = get_huge_zero_page();
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800899 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800900 zero_pfn);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800901 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800902 ret = 0;
903 goto out_unlock;
904 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800905 if (unlikely(pmd_trans_splitting(pmd))) {
906 /* split huge page running from under us */
907 spin_unlock(&src_mm->page_table_lock);
908 spin_unlock(&dst_mm->page_table_lock);
909 pte_free(dst_mm, pgtable);
910
911 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
912 goto out;
913 }
914 src_page = pmd_page(pmd);
915 VM_BUG_ON(!PageHead(src_page));
916 get_page(src_page);
917 page_dup_rmap(src_page);
918 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
919
920 pmdp_set_wrprotect(src_mm, addr, src_pmd);
921 pmd = pmd_mkold(pmd_wrprotect(pmd));
922 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700923 pgtable_trans_huge_deposit(dst_mm, pgtable);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800924 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800925
926 ret = 0;
927out_unlock:
928 spin_unlock(&src_mm->page_table_lock);
929 spin_unlock(&dst_mm->page_table_lock);
930out:
931 return ret;
932}
933
Will Deacona1dd4502012-12-11 16:01:27 -0800934void huge_pmd_set_accessed(struct mm_struct *mm,
935 struct vm_area_struct *vma,
936 unsigned long address,
937 pmd_t *pmd, pmd_t orig_pmd,
938 int dirty)
939{
940 pmd_t entry;
941 unsigned long haddr;
942
943 spin_lock(&mm->page_table_lock);
944 if (unlikely(!pmd_same(*pmd, orig_pmd)))
945 goto unlock;
946
947 entry = pmd_mkyoung(orig_pmd);
948 haddr = address & HPAGE_PMD_MASK;
949 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
950 update_mmu_cache_pmd(vma, address, pmd);
951
952unlock:
953 spin_unlock(&mm->page_table_lock);
954}
955
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800956static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
957 struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800958 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800959{
960 pgtable_t pgtable;
961 pmd_t _pmd;
962 struct page *page;
963 int i, ret = 0;
964 unsigned long mmun_start; /* For mmu_notifiers */
965 unsigned long mmun_end; /* For mmu_notifiers */
966
967 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
968 if (!page) {
969 ret |= VM_FAULT_OOM;
970 goto out;
971 }
972
973 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
974 put_page(page);
975 ret |= VM_FAULT_OOM;
976 goto out;
977 }
978
979 clear_user_highpage(page, address);
980 __SetPageUptodate(page);
981
982 mmun_start = haddr;
983 mmun_end = haddr + HPAGE_PMD_SIZE;
984 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
985
986 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800987 if (unlikely(!pmd_same(*pmd, orig_pmd)))
988 goto out_free_page;
989
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800990 pmdp_clear_flush(vma, haddr, pmd);
991 /* leave pmd empty until pte is filled */
992
993 pgtable = pgtable_trans_huge_withdraw(mm);
994 pmd_populate(mm, &_pmd, pgtable);
995
996 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
997 pte_t *pte, entry;
998 if (haddr == (address & PAGE_MASK)) {
999 entry = mk_pte(page, vma->vm_page_prot);
1000 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1001 page_add_new_anon_rmap(page, vma, haddr);
1002 } else {
1003 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1004 entry = pte_mkspecial(entry);
1005 }
1006 pte = pte_offset_map(&_pmd, haddr);
1007 VM_BUG_ON(!pte_none(*pte));
1008 set_pte_at(mm, haddr, pte, entry);
1009 pte_unmap(pte);
1010 }
1011 smp_wmb(); /* make pte visible before pmd */
1012 pmd_populate(mm, pmd, pgtable);
1013 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001014 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001015 inc_mm_counter(mm, MM_ANONPAGES);
1016
1017 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1018
1019 ret |= VM_FAULT_WRITE;
1020out:
1021 return ret;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001022out_free_page:
1023 spin_unlock(&mm->page_table_lock);
1024 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1025 mem_cgroup_uncharge_page(page);
1026 put_page(page);
1027 goto out;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001028}
1029
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001030static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1031 struct vm_area_struct *vma,
1032 unsigned long address,
1033 pmd_t *pmd, pmd_t orig_pmd,
1034 struct page *page,
1035 unsigned long haddr)
1036{
1037 pgtable_t pgtable;
1038 pmd_t _pmd;
1039 int ret = 0, i;
1040 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001041 unsigned long mmun_start; /* For mmu_notifiers */
1042 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001043
1044 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1045 GFP_KERNEL);
1046 if (unlikely(!pages)) {
1047 ret |= VM_FAULT_OOM;
1048 goto out;
1049 }
1050
1051 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001052 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1053 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001054 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001055 if (unlikely(!pages[i] ||
1056 mem_cgroup_newpage_charge(pages[i], mm,
1057 GFP_KERNEL))) {
1058 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001059 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001060 mem_cgroup_uncharge_start();
1061 while (--i >= 0) {
1062 mem_cgroup_uncharge_page(pages[i]);
1063 put_page(pages[i]);
1064 }
1065 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001066 kfree(pages);
1067 ret |= VM_FAULT_OOM;
1068 goto out;
1069 }
1070 }
1071
1072 for (i = 0; i < HPAGE_PMD_NR; i++) {
1073 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001074 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001075 __SetPageUptodate(pages[i]);
1076 cond_resched();
1077 }
1078
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001079 mmun_start = haddr;
1080 mmun_end = haddr + HPAGE_PMD_SIZE;
1081 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1082
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001083 spin_lock(&mm->page_table_lock);
1084 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1085 goto out_free_pages;
1086 VM_BUG_ON(!PageHead(page));
1087
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001088 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001089 /* leave pmd empty until pte is filled */
1090
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001091 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001092 pmd_populate(mm, &_pmd, pgtable);
1093
1094 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1095 pte_t *pte, entry;
1096 entry = mk_pte(pages[i], vma->vm_page_prot);
1097 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1098 page_add_new_anon_rmap(pages[i], vma, haddr);
1099 pte = pte_offset_map(&_pmd, haddr);
1100 VM_BUG_ON(!pte_none(*pte));
1101 set_pte_at(mm, haddr, pte, entry);
1102 pte_unmap(pte);
1103 }
1104 kfree(pages);
1105
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001106 smp_wmb(); /* make pte visible before pmd */
1107 pmd_populate(mm, pmd, pgtable);
1108 page_remove_rmap(page);
1109 spin_unlock(&mm->page_table_lock);
1110
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001111 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1112
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001113 ret |= VM_FAULT_WRITE;
1114 put_page(page);
1115
1116out:
1117 return ret;
1118
1119out_free_pages:
1120 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001121 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001122 mem_cgroup_uncharge_start();
1123 for (i = 0; i < HPAGE_PMD_NR; i++) {
1124 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001125 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001126 }
1127 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128 kfree(pages);
1129 goto out;
1130}
1131
1132int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1133 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1134{
1135 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001136 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001137 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001138 unsigned long mmun_start; /* For mmu_notifiers */
1139 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001140
1141 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001142 haddr = address & HPAGE_PMD_MASK;
1143 if (is_huge_zero_pmd(orig_pmd))
1144 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001145 spin_lock(&mm->page_table_lock);
1146 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1147 goto out_unlock;
1148
1149 page = pmd_page(orig_pmd);
1150 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001151 if (page_mapcount(page) == 1) {
1152 pmd_t entry;
1153 entry = pmd_mkyoung(orig_pmd);
1154 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1155 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001156 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001157 ret |= VM_FAULT_WRITE;
1158 goto out_unlock;
1159 }
1160 get_page(page);
1161 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001162alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001163 if (transparent_hugepage_enabled(vma) &&
1164 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001165 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001166 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001167 else
1168 new_page = NULL;
1169
1170 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -07001171 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001172 if (is_huge_zero_pmd(orig_pmd)) {
1173 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001174 address, pmd, orig_pmd, haddr);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001175 } else {
1176 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1177 pmd, orig_pmd, page, haddr);
1178 if (ret & VM_FAULT_OOM)
1179 split_huge_page(page);
1180 put_page(page);
1181 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001182 goto out;
1183 }
Andi Kleen81ab4202011-04-14 15:22:06 -07001184 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001186 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1187 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001188 if (page) {
1189 split_huge_page(page);
1190 put_page(page);
1191 }
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001192 ret |= VM_FAULT_OOM;
1193 goto out;
1194 }
1195
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001196 if (is_huge_zero_pmd(orig_pmd))
1197 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1198 else
1199 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001200 __SetPageUptodate(new_page);
1201
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001202 mmun_start = haddr;
1203 mmun_end = haddr + HPAGE_PMD_SIZE;
1204 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1205
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001206 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001207 if (page)
1208 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001209 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001210 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001211 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001212 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001213 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001214 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001215 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -08001216 entry = mk_huge_pmd(new_page, vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001217 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 page_add_new_anon_rmap(new_page, vma, haddr);
1219 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001220 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001221 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001222 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001223 put_huge_zero_page();
1224 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001225 VM_BUG_ON(!PageHead(page));
1226 page_remove_rmap(page);
1227 put_page(page);
1228 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001229 ret |= VM_FAULT_WRITE;
1230 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001231 spin_unlock(&mm->page_table_lock);
1232out_mn:
1233 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1234out:
1235 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001236out_unlock:
1237 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001238 return ret;
1239}
1240
David Rientjesb676b292012-10-08 16:34:03 -07001241struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 unsigned long addr,
1243 pmd_t *pmd,
1244 unsigned int flags)
1245{
David Rientjesb676b292012-10-08 16:34:03 -07001246 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001247 struct page *page = NULL;
1248
1249 assert_spin_locked(&mm->page_table_lock);
1250
1251 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1252 goto out;
1253
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001254 /* Avoid dumping huge zero page */
1255 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1256 return ERR_PTR(-EFAULT);
1257
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001258 page = pmd_page(*pmd);
1259 VM_BUG_ON(!PageHead(page));
1260 if (flags & FOLL_TOUCH) {
1261 pmd_t _pmd;
1262 /*
1263 * We should set the dirty bit only for FOLL_WRITE but
1264 * for now the dirty bit in the pmd is meaningless.
1265 * And if the dirty bit will become meaningful and
1266 * we'll only set it with FOLL_WRITE, an atomic
1267 * set_bit will be required on the pmd to set the
1268 * young bit, instead of the current set_pmd_at.
1269 */
1270 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1271 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1272 }
David Rientjesb676b292012-10-08 16:34:03 -07001273 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1274 if (page->mapping && trylock_page(page)) {
1275 lru_add_drain();
1276 if (page->mapping)
1277 mlock_vma_page(page);
1278 unlock_page(page);
1279 }
1280 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001281 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1282 VM_BUG_ON(!PageCompound(page));
1283 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001284 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001285
1286out:
1287 return page;
1288}
1289
Mel Gormand10e63f2012-10-25 14:16:31 +02001290/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001291int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1292 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001293{
Mel Gormanb32967f2012-11-19 12:35:47 +00001294 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001295 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001296 int target_nid;
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001297 int current_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001298 bool migrated;
Mel Gormand10e63f2012-10-25 14:16:31 +02001299
1300 spin_lock(&mm->page_table_lock);
1301 if (unlikely(!pmd_same(pmd, *pmdp)))
1302 goto out_unlock;
1303
1304 page = pmd_page(pmd);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001305 get_page(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001306 current_nid = page_to_nid(page);
1307 count_vm_numa_event(NUMA_HINT_FAULTS);
1308 if (current_nid == numa_node_id())
1309 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001310
1311 target_nid = mpol_misplaced(page, vma, haddr);
Mel Gormanb32967f2012-11-19 12:35:47 +00001312 if (target_nid == -1) {
1313 put_page(page);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001314 goto clear_pmdnuma;
Mel Gormanb32967f2012-11-19 12:35:47 +00001315 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001316
Mel Gormanb32967f2012-11-19 12:35:47 +00001317 /* Acquire the page lock to serialise THP migrations */
1318 spin_unlock(&mm->page_table_lock);
1319 lock_page(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001320
Mel Gormanb32967f2012-11-19 12:35:47 +00001321 /* Confirm the PTE did not while locked */
1322 spin_lock(&mm->page_table_lock);
1323 if (unlikely(!pmd_same(pmd, *pmdp))) {
1324 unlock_page(page);
1325 put_page(page);
1326 goto out_unlock;
1327 }
1328 spin_unlock(&mm->page_table_lock);
1329
1330 /* Migrate the THP to the requested node */
1331 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001332 pmdp, pmd, addr, page, target_nid);
1333 if (!migrated)
1334 goto check_same;
Mel Gormanb32967f2012-11-19 12:35:47 +00001335
Hugh Dickins340ef392013-02-22 16:34:33 -08001336 task_numa_fault(target_nid, HPAGE_PMD_NR, true);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001337 return 0;
1338
Hugh Dickins340ef392013-02-22 16:34:33 -08001339check_same:
1340 spin_lock(&mm->page_table_lock);
1341 if (unlikely(!pmd_same(pmd, *pmdp)))
1342 goto out_unlock;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001343clear_pmdnuma:
Mel Gormand10e63f2012-10-25 14:16:31 +02001344 pmd = pmd_mknonnuma(pmd);
1345 set_pmd_at(mm, haddr, pmdp, pmd);
1346 VM_BUG_ON(pmd_numa(*pmdp));
1347 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001348out_unlock:
1349 spin_unlock(&mm->page_table_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001350 if (current_nid != -1)
Hugh Dickins340ef392013-02-22 16:34:33 -08001351 task_numa_fault(current_nid, HPAGE_PMD_NR, false);
Mel Gormand10e63f2012-10-25 14:16:31 +02001352 return 0;
1353}
1354
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001355int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001356 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001357{
1358 int ret = 0;
1359
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001360 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1361 struct page *page;
1362 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001363 pmd_t orig_pmd;
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001364 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
David Millerf5c8ad42012-10-08 16:34:26 -07001365 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001366 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001367 if (is_huge_zero_pmd(orig_pmd)) {
1368 tlb->mm->nr_ptes--;
1369 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001370 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001371 } else {
1372 page = pmd_page(orig_pmd);
1373 page_remove_rmap(page);
1374 VM_BUG_ON(page_mapcount(page) < 0);
1375 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1376 VM_BUG_ON(!PageHead(page));
1377 tlb->mm->nr_ptes--;
1378 spin_unlock(&tlb->mm->page_table_lock);
1379 tlb_remove_page(tlb, page);
1380 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001381 pte_free(tlb->mm, pgtable);
1382 ret = 1;
1383 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001384 return ret;
1385}
1386
Johannes Weiner0ca16342011-01-13 15:47:02 -08001387int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1388 unsigned long addr, unsigned long end,
1389 unsigned char *vec)
1390{
1391 int ret = 0;
1392
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001393 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1394 /*
1395 * All logical pages in the range are present
1396 * if backed by a huge page.
1397 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001398 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001399 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1400 ret = 1;
1401 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001402
1403 return ret;
1404}
1405
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001406int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1407 unsigned long old_addr,
1408 unsigned long new_addr, unsigned long old_end,
1409 pmd_t *old_pmd, pmd_t *new_pmd)
1410{
1411 int ret = 0;
1412 pmd_t pmd;
1413
1414 struct mm_struct *mm = vma->vm_mm;
1415
1416 if ((old_addr & ~HPAGE_PMD_MASK) ||
1417 (new_addr & ~HPAGE_PMD_MASK) ||
1418 old_end - old_addr < HPAGE_PMD_SIZE ||
1419 (new_vma->vm_flags & VM_NOHUGEPAGE))
1420 goto out;
1421
1422 /*
1423 * The destination pmd shouldn't be established, free_pgtables()
1424 * should have release it.
1425 */
1426 if (WARN_ON(!pmd_none(*new_pmd))) {
1427 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1428 goto out;
1429 }
1430
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001431 ret = __pmd_trans_huge_lock(old_pmd, vma);
1432 if (ret == 1) {
1433 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1434 VM_BUG_ON(!pmd_none(*new_pmd));
1435 set_pmd_at(mm, new_addr, new_pmd, pmd);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001436 spin_unlock(&mm->page_table_lock);
1437 }
1438out:
1439 return ret;
1440}
1441
Johannes Weinercd7548a2011-01-13 15:47:04 -08001442int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001443 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001444{
1445 struct mm_struct *mm = vma->vm_mm;
1446 int ret = 0;
1447
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001448 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1449 pmd_t entry;
1450 entry = pmdp_get_and_clear(mm, addr, pmd);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001451 if (!prot_numa) {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001452 entry = pmd_modify(entry, newprot);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001453 BUG_ON(pmd_write(entry));
1454 } else {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001455 struct page *page = pmd_page(*pmd);
1456
1457 /* only check non-shared pages */
1458 if (page_mapcount(page) == 1 &&
1459 !pmd_numa(*pmd)) {
1460 entry = pmd_mknuma(entry);
1461 }
1462 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001463 set_pmd_at(mm, addr, pmd, entry);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001464 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001465 ret = 1;
1466 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001467
1468 return ret;
1469}
1470
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001471/*
1472 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1473 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1474 *
1475 * Note that if it returns 1, this routine returns without unlocking page
1476 * table locks. So callers must unlock them.
1477 */
1478int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1479{
1480 spin_lock(&vma->vm_mm->page_table_lock);
1481 if (likely(pmd_trans_huge(*pmd))) {
1482 if (unlikely(pmd_trans_splitting(*pmd))) {
1483 spin_unlock(&vma->vm_mm->page_table_lock);
1484 wait_split_huge_page(vma->anon_vma, pmd);
1485 return -1;
1486 } else {
1487 /* Thp mapped by 'pmd' is stable, so we can
1488 * handle it as it is. */
1489 return 1;
1490 }
1491 }
1492 spin_unlock(&vma->vm_mm->page_table_lock);
1493 return 0;
1494}
1495
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001496pmd_t *page_check_address_pmd(struct page *page,
1497 struct mm_struct *mm,
1498 unsigned long address,
1499 enum page_check_address_pmd_flag flag)
1500{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001501 pmd_t *pmd, *ret = NULL;
1502
1503 if (address & ~HPAGE_PMD_MASK)
1504 goto out;
1505
Bob Liu62190492012-12-11 16:00:37 -08001506 pmd = mm_find_pmd(mm, address);
1507 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001508 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001509 if (pmd_none(*pmd))
1510 goto out;
1511 if (pmd_page(*pmd) != page)
1512 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001513 /*
1514 * split_vma() may create temporary aliased mappings. There is
1515 * no risk as long as all huge pmd are found and have their
1516 * splitting bit set before __split_huge_page_refcount
1517 * runs. Finding the same huge pmd more than once during the
1518 * same rmap walk is not a problem.
1519 */
1520 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1521 pmd_trans_splitting(*pmd))
1522 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001523 if (pmd_trans_huge(*pmd)) {
1524 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1525 !pmd_trans_splitting(*pmd));
1526 ret = pmd;
1527 }
1528out:
1529 return ret;
1530}
1531
1532static int __split_huge_page_splitting(struct page *page,
1533 struct vm_area_struct *vma,
1534 unsigned long address)
1535{
1536 struct mm_struct *mm = vma->vm_mm;
1537 pmd_t *pmd;
1538 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001539 /* For mmu_notifiers */
1540 const unsigned long mmun_start = address;
1541 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001542
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001543 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001544 spin_lock(&mm->page_table_lock);
1545 pmd = page_check_address_pmd(page, mm, address,
1546 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1547 if (pmd) {
1548 /*
1549 * We can't temporarily set the pmd to null in order
1550 * to split it, the pmd must remain marked huge at all
1551 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001552 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001553 * serialize against split_huge_page*.
1554 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001555 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001556 ret = 1;
1557 }
1558 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001559 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001560
1561 return ret;
1562}
1563
1564static void __split_huge_page_refcount(struct page *page)
1565{
1566 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001567 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001568 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001569 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001570
1571 /* prevent PageLRU to go away from under us, and freeze lru stats */
1572 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001573 lruvec = mem_cgroup_page_lruvec(page, zone);
1574
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001575 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001576 /* complete memcg works before add pages to LRU */
1577 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001578
Shaohua Li45676882012-01-12 17:19:18 -08001579 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001580 struct page *page_tail = page + i;
1581
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001582 /* tail_page->_mapcount cannot change */
1583 BUG_ON(page_mapcount(page_tail) < 0);
1584 tail_count += page_mapcount(page_tail);
1585 /* check for overflow */
1586 BUG_ON(tail_count < 0);
1587 BUG_ON(atomic_read(&page_tail->_count) != 0);
1588 /*
1589 * tail_page->_count is zero and not changing from
1590 * under us. But get_page_unless_zero() may be running
1591 * from under us on the tail_page. If we used
1592 * atomic_set() below instead of atomic_add(), we
1593 * would then run atomic_set() concurrently with
1594 * get_page_unless_zero(), and atomic_set() is
1595 * implemented in C not using locked ops. spin_unlock
1596 * on x86 sometime uses locked ops because of PPro
1597 * errata 66, 92, so unless somebody can guarantee
1598 * atomic_set() here would be safe on all archs (and
1599 * not only on x86), it's safer to use atomic_add().
1600 */
1601 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1602 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001603
1604 /* after clearing PageTail the gup refcount can be released */
1605 smp_mb();
1606
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001607 /*
1608 * retain hwpoison flag of the poisoned tail page:
1609 * fix for the unsuitable process killed on Guest Machine(KVM)
1610 * by the memory-failure.
1611 */
1612 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001613 page_tail->flags |= (page->flags &
1614 ((1L << PG_referenced) |
1615 (1L << PG_swapbacked) |
1616 (1L << PG_mlocked) |
1617 (1L << PG_uptodate)));
1618 page_tail->flags |= (1L << PG_dirty);
1619
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001620 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001621 smp_wmb();
1622
1623 /*
1624 * __split_huge_page_splitting() already set the
1625 * splitting bit in all pmd that could map this
1626 * hugepage, that will ensure no CPU can alter the
1627 * mapcount on the head page. The mapcount is only
1628 * accounted in the head page and it has to be
1629 * transferred to all tail pages in the below code. So
1630 * for this code to be safe, the split the mapcount
1631 * can't change. But that doesn't mean userland can't
1632 * keep changing and reading the page contents while
1633 * we transfer the mapcount, so the pmd splitting
1634 * status is achieved setting a reserved bit in the
1635 * pmd, not by clearing the present bit.
1636 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001637 page_tail->_mapcount = page->_mapcount;
1638
1639 BUG_ON(page_tail->mapping);
1640 page_tail->mapping = page->mapping;
1641
Shaohua Li45676882012-01-12 17:19:18 -08001642 page_tail->index = page->index + i;
Hillf Danton5aa80372012-11-27 14:40:32 +00001643 page_xchg_last_nid(page_tail, page_last_nid(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001644
1645 BUG_ON(!PageAnon(page_tail));
1646 BUG_ON(!PageUptodate(page_tail));
1647 BUG_ON(!PageDirty(page_tail));
1648 BUG_ON(!PageSwapBacked(page_tail));
1649
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001650 lru_add_page_tail(page, page_tail, lruvec);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001651 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001652 atomic_sub(tail_count, &page->_count);
1653 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001654
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001655 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001656 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1657
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001658 ClearPageCompound(page);
1659 compound_unlock(page);
1660 spin_unlock_irq(&zone->lru_lock);
1661
1662 for (i = 1; i < HPAGE_PMD_NR; i++) {
1663 struct page *page_tail = page + i;
1664 BUG_ON(page_count(page_tail) <= 0);
1665 /*
1666 * Tail pages may be freed if there wasn't any mapping
1667 * like if add_to_swap() is running on a lru page that
1668 * had its mapping zapped. And freeing these pages
1669 * requires taking the lru_lock so we do the put_page
1670 * of the tail pages after the split is complete.
1671 */
1672 put_page(page_tail);
1673 }
1674
1675 /*
1676 * Only the head page (now become a regular page) is required
1677 * to be pinned by the caller.
1678 */
1679 BUG_ON(page_count(page) <= 0);
1680}
1681
1682static int __split_huge_page_map(struct page *page,
1683 struct vm_area_struct *vma,
1684 unsigned long address)
1685{
1686 struct mm_struct *mm = vma->vm_mm;
1687 pmd_t *pmd, _pmd;
1688 int ret = 0, i;
1689 pgtable_t pgtable;
1690 unsigned long haddr;
1691
1692 spin_lock(&mm->page_table_lock);
1693 pmd = page_check_address_pmd(page, mm, address,
1694 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1695 if (pmd) {
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001696 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001697 pmd_populate(mm, &_pmd, pgtable);
1698
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001699 haddr = address;
1700 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001701 pte_t *pte, entry;
1702 BUG_ON(PageCompound(page+i));
1703 entry = mk_pte(page + i, vma->vm_page_prot);
1704 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1705 if (!pmd_write(*pmd))
1706 entry = pte_wrprotect(entry);
1707 else
1708 BUG_ON(page_mapcount(page) != 1);
1709 if (!pmd_young(*pmd))
1710 entry = pte_mkold(entry);
Andrea Arcangeli1ba6e0b2012-10-04 01:51:06 +02001711 if (pmd_numa(*pmd))
1712 entry = pte_mknuma(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001713 pte = pte_offset_map(&_pmd, haddr);
1714 BUG_ON(!pte_none(*pte));
1715 set_pte_at(mm, haddr, pte, entry);
1716 pte_unmap(pte);
1717 }
1718
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001719 smp_wmb(); /* make pte visible before pmd */
1720 /*
1721 * Up to this point the pmd is present and huge and
1722 * userland has the whole access to the hugepage
1723 * during the split (which happens in place). If we
1724 * overwrite the pmd with the not-huge version
1725 * pointing to the pte here (which of course we could
1726 * if all CPUs were bug free), userland could trigger
1727 * a small page size TLB miss on the small sized TLB
1728 * while the hugepage TLB entry is still established
1729 * in the huge TLB. Some CPU doesn't like that. See
1730 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1731 * Erratum 383 on page 93. Intel should be safe but is
1732 * also warns that it's only safe if the permission
1733 * and cache attributes of the two entries loaded in
1734 * the two TLB is identical (which should be the case
1735 * here). But it is generally safer to never allow
1736 * small and huge TLB entries for the same virtual
1737 * address to be loaded simultaneously. So instead of
1738 * doing "pmd_populate(); flush_tlb_range();" we first
1739 * mark the current pmd notpresent (atomically because
1740 * here the pmd_trans_huge and pmd_trans_splitting
1741 * must remain set at all times on the pmd until the
1742 * split is complete for this pmd), then we flush the
1743 * SMP TLB and finally we write the non-huge version
1744 * of the pmd entry with pmd_populate.
1745 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001746 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001747 pmd_populate(mm, pmd, pgtable);
1748 ret = 1;
1749 }
1750 spin_unlock(&mm->page_table_lock);
1751
1752 return ret;
1753}
1754
Ingo Molnar5a505082012-12-02 19:56:46 +00001755/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001756static void __split_huge_page(struct page *page,
1757 struct anon_vma *anon_vma)
1758{
1759 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001760 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001761 struct anon_vma_chain *avc;
1762
1763 BUG_ON(!PageHead(page));
1764 BUG_ON(PageTail(page));
1765
1766 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001767 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001768 struct vm_area_struct *vma = avc->vma;
1769 unsigned long addr = vma_address(page, vma);
1770 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001771 mapcount += __split_huge_page_splitting(page, vma, addr);
1772 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001773 /*
1774 * It is critical that new vmas are added to the tail of the
1775 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1776 * and establishes a child pmd before
1777 * __split_huge_page_splitting() freezes the parent pmd (so if
1778 * we fail to prevent copy_huge_pmd() from running until the
1779 * whole __split_huge_page() is complete), we will still see
1780 * the newly established pmd of the child later during the
1781 * walk, to be able to set it as pmd_trans_splitting too.
1782 */
1783 if (mapcount != page_mapcount(page))
1784 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1785 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001786 BUG_ON(mapcount != page_mapcount(page));
1787
1788 __split_huge_page_refcount(page);
1789
1790 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001791 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001792 struct vm_area_struct *vma = avc->vma;
1793 unsigned long addr = vma_address(page, vma);
1794 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001795 mapcount2 += __split_huge_page_map(page, vma, addr);
1796 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001797 if (mapcount != mapcount2)
1798 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1799 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001800 BUG_ON(mapcount != mapcount2);
1801}
1802
1803int split_huge_page(struct page *page)
1804{
1805 struct anon_vma *anon_vma;
1806 int ret = 1;
1807
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08001808 BUG_ON(is_huge_zero_pfn(page_to_pfn(page)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001809 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001810
1811 /*
1812 * The caller does not necessarily hold an mmap_sem that would prevent
1813 * the anon_vma disappearing so we first we take a reference to it
1814 * and then lock the anon_vma for write. This is similar to
1815 * page_lock_anon_vma_read except the write lock is taken to serialise
1816 * against parallel split or collapse operations.
1817 */
1818 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001819 if (!anon_vma)
1820 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001821 anon_vma_lock_write(anon_vma);
1822
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001823 ret = 0;
1824 if (!PageCompound(page))
1825 goto out_unlock;
1826
1827 BUG_ON(!PageSwapBacked(page));
1828 __split_huge_page(page, anon_vma);
Andi Kleen81ab4202011-04-14 15:22:06 -07001829 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001830
1831 BUG_ON(PageCompound(page));
1832out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08001833 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08001834 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001835out:
1836 return ret;
1837}
1838
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001839#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001840
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001841int hugepage_madvise(struct vm_area_struct *vma,
1842 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001843{
Gerald Schaefer8e720332012-10-08 16:30:12 -07001844 struct mm_struct *mm = vma->vm_mm;
1845
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001846 switch (advice) {
1847 case MADV_HUGEPAGE:
1848 /*
1849 * Be somewhat over-protective like KSM for now!
1850 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001851 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001852 return -EINVAL;
Gerald Schaefer8e720332012-10-08 16:30:12 -07001853 if (mm->def_flags & VM_NOHUGEPAGE)
1854 return -EINVAL;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001855 *vm_flags &= ~VM_NOHUGEPAGE;
1856 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001857 /*
1858 * If the vma become good for khugepaged to scan,
1859 * register it here without waiting a page fault that
1860 * may not happen any time soon.
1861 */
1862 if (unlikely(khugepaged_enter_vma_merge(vma)))
1863 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001864 break;
1865 case MADV_NOHUGEPAGE:
1866 /*
1867 * Be somewhat over-protective like KSM for now!
1868 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001869 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001870 return -EINVAL;
1871 *vm_flags &= ~VM_HUGEPAGE;
1872 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001873 /*
1874 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1875 * this vma even if we leave the mm registered in khugepaged if
1876 * it got registered before VM_NOHUGEPAGE was set.
1877 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001878 break;
1879 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001880
1881 return 0;
1882}
1883
Andrea Arcangeliba761492011-01-13 15:46:58 -08001884static int __init khugepaged_slab_init(void)
1885{
1886 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1887 sizeof(struct mm_slot),
1888 __alignof__(struct mm_slot), 0, NULL);
1889 if (!mm_slot_cache)
1890 return -ENOMEM;
1891
1892 return 0;
1893}
1894
Andrea Arcangeliba761492011-01-13 15:46:58 -08001895static inline struct mm_slot *alloc_mm_slot(void)
1896{
1897 if (!mm_slot_cache) /* initialization failed */
1898 return NULL;
1899 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1900}
1901
1902static inline void free_mm_slot(struct mm_slot *mm_slot)
1903{
1904 kmem_cache_free(mm_slot_cache, mm_slot);
1905}
1906
Andrea Arcangeliba761492011-01-13 15:46:58 -08001907static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1908{
1909 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001910 struct hlist_node *node;
1911
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001912 hash_for_each_possible(mm_slots_hash, mm_slot, node, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001913 if (mm == mm_slot->mm)
1914 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001915
Andrea Arcangeliba761492011-01-13 15:46:58 -08001916 return NULL;
1917}
1918
1919static void insert_to_mm_slots_hash(struct mm_struct *mm,
1920 struct mm_slot *mm_slot)
1921{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001922 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001923 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001924}
1925
1926static inline int khugepaged_test_exit(struct mm_struct *mm)
1927{
1928 return atomic_read(&mm->mm_users) == 0;
1929}
1930
1931int __khugepaged_enter(struct mm_struct *mm)
1932{
1933 struct mm_slot *mm_slot;
1934 int wakeup;
1935
1936 mm_slot = alloc_mm_slot();
1937 if (!mm_slot)
1938 return -ENOMEM;
1939
1940 /* __khugepaged_exit() must not run from under us */
1941 VM_BUG_ON(khugepaged_test_exit(mm));
1942 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1943 free_mm_slot(mm_slot);
1944 return 0;
1945 }
1946
1947 spin_lock(&khugepaged_mm_lock);
1948 insert_to_mm_slots_hash(mm, mm_slot);
1949 /*
1950 * Insert just behind the scanning cursor, to let the area settle
1951 * down a little.
1952 */
1953 wakeup = list_empty(&khugepaged_scan.mm_head);
1954 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1955 spin_unlock(&khugepaged_mm_lock);
1956
1957 atomic_inc(&mm->mm_count);
1958 if (wakeup)
1959 wake_up_interruptible(&khugepaged_wait);
1960
1961 return 0;
1962}
1963
1964int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
1965{
1966 unsigned long hstart, hend;
1967 if (!vma->anon_vma)
1968 /*
1969 * Not yet faulted in so we will register later in the
1970 * page fault if needed.
1971 */
1972 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001973 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001974 /* khugepaged not yet working on file or special mappings */
1975 return 0;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07001976 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001977 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1978 hend = vma->vm_end & HPAGE_PMD_MASK;
1979 if (hstart < hend)
1980 return khugepaged_enter(vma);
1981 return 0;
1982}
1983
1984void __khugepaged_exit(struct mm_struct *mm)
1985{
1986 struct mm_slot *mm_slot;
1987 int free = 0;
1988
1989 spin_lock(&khugepaged_mm_lock);
1990 mm_slot = get_mm_slot(mm);
1991 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001992 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001993 list_del(&mm_slot->mm_node);
1994 free = 1;
1995 }
Chris Wrightd788e802011-07-25 17:12:14 -07001996 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001997
1998 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001999 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2000 free_mm_slot(mm_slot);
2001 mmdrop(mm);
2002 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002003 /*
2004 * This is required to serialize against
2005 * khugepaged_test_exit() (which is guaranteed to run
2006 * under mmap sem read mode). Stop here (after we
2007 * return all pagetables will be destroyed) until
2008 * khugepaged has finished working on the pagetables
2009 * under the mmap_sem.
2010 */
2011 down_write(&mm->mmap_sem);
2012 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002013 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002014}
2015
2016static void release_pte_page(struct page *page)
2017{
2018 /* 0 stands for page_is_file_cache(page) == false */
2019 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2020 unlock_page(page);
2021 putback_lru_page(page);
2022}
2023
2024static void release_pte_pages(pte_t *pte, pte_t *_pte)
2025{
2026 while (--_pte >= pte) {
2027 pte_t pteval = *_pte;
2028 if (!pte_none(pteval))
2029 release_pte_page(pte_page(pteval));
2030 }
2031}
2032
Andrea Arcangeliba761492011-01-13 15:46:58 -08002033static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2034 unsigned long address,
2035 pte_t *pte)
2036{
2037 struct page *page;
2038 pte_t *_pte;
Bob Liu344aa352012-12-11 16:00:34 -08002039 int referenced = 0, none = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002040 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2041 _pte++, address += PAGE_SIZE) {
2042 pte_t pteval = *_pte;
2043 if (pte_none(pteval)) {
2044 if (++none <= khugepaged_max_ptes_none)
2045 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002046 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002047 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 }
Bob Liu344aa352012-12-11 16:00:34 -08002049 if (!pte_present(pteval) || !pte_write(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002050 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002051 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002052 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002053 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002054
Andrea Arcangeliba761492011-01-13 15:46:58 -08002055 VM_BUG_ON(PageCompound(page));
2056 BUG_ON(!PageAnon(page));
2057 VM_BUG_ON(!PageSwapBacked(page));
2058
2059 /* cannot use mapcount: can't collapse if there's a gup pin */
Bob Liu344aa352012-12-11 16:00:34 -08002060 if (page_count(page) != 1)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002061 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002062 /*
2063 * We can do it before isolate_lru_page because the
2064 * page can't be freed from under us. NOTE: PG_lock
2065 * is needed to serialize against split_huge_page
2066 * when invoked from the VM.
2067 */
Bob Liu344aa352012-12-11 16:00:34 -08002068 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002069 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002070 /*
2071 * Isolate the page to avoid collapsing an hugepage
2072 * currently in use by the VM.
2073 */
2074 if (isolate_lru_page(page)) {
2075 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002076 goto out;
2077 }
2078 /* 0 stands for page_is_file_cache(page) == false */
2079 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2080 VM_BUG_ON(!PageLocked(page));
2081 VM_BUG_ON(PageLRU(page));
2082
2083 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002084 if (pte_young(pteval) || PageReferenced(page) ||
2085 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002086 referenced = 1;
2087 }
Bob Liu344aa352012-12-11 16:00:34 -08002088 if (likely(referenced))
2089 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002090out:
Bob Liu344aa352012-12-11 16:00:34 -08002091 release_pte_pages(pte, _pte);
2092 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002093}
2094
2095static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2096 struct vm_area_struct *vma,
2097 unsigned long address,
2098 spinlock_t *ptl)
2099{
2100 pte_t *_pte;
2101 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2102 pte_t pteval = *_pte;
2103 struct page *src_page;
2104
2105 if (pte_none(pteval)) {
2106 clear_user_highpage(page, address);
2107 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2108 } else {
2109 src_page = pte_page(pteval);
2110 copy_user_highpage(page, src_page, address, vma);
2111 VM_BUG_ON(page_mapcount(src_page) != 1);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112 release_pte_page(src_page);
2113 /*
2114 * ptl mostly unnecessary, but preempt has to
2115 * be disabled to update the per-cpu stats
2116 * inside page_remove_rmap().
2117 */
2118 spin_lock(ptl);
2119 /*
2120 * paravirt calls inside pte_clear here are
2121 * superfluous.
2122 */
2123 pte_clear(vma->vm_mm, address, _pte);
2124 page_remove_rmap(src_page);
2125 spin_unlock(ptl);
2126 free_page_and_swap_cache(src_page);
2127 }
2128
2129 address += PAGE_SIZE;
2130 page++;
2131 }
2132}
2133
Xiao Guangrong26234f32012-10-08 16:29:51 -07002134static void khugepaged_alloc_sleep(void)
2135{
2136 wait_event_freezable_timeout(khugepaged_wait, false,
2137 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2138}
2139
2140#ifdef CONFIG_NUMA
2141static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2142{
2143 if (IS_ERR(*hpage)) {
2144 if (!*wait)
2145 return false;
2146
2147 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002148 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002149 khugepaged_alloc_sleep();
2150 } else if (*hpage) {
2151 put_page(*hpage);
2152 *hpage = NULL;
2153 }
2154
2155 return true;
2156}
2157
2158static struct page
2159*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2160 struct vm_area_struct *vma, unsigned long address,
2161 int node)
2162{
2163 VM_BUG_ON(*hpage);
2164 /*
2165 * Allocate the page while the vma is still valid and under
2166 * the mmap_sem read mode so there is no memory allocation
2167 * later when we take the mmap_sem in write mode. This is more
2168 * friendly behavior (OTOH it may actually hide bugs) to
2169 * filesystems in userland with daemons allocating memory in
2170 * the userland I/O paths. Allocating memory with the
2171 * mmap_sem in read mode is good idea also to allow greater
2172 * scalability.
2173 */
2174 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2175 node, __GFP_OTHER_NODE);
2176
2177 /*
2178 * After allocating the hugepage, release the mmap_sem read lock in
2179 * preparation for taking it in write mode.
2180 */
2181 up_read(&mm->mmap_sem);
2182 if (unlikely(!*hpage)) {
2183 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2184 *hpage = ERR_PTR(-ENOMEM);
2185 return NULL;
2186 }
2187
2188 count_vm_event(THP_COLLAPSE_ALLOC);
2189 return *hpage;
2190}
2191#else
2192static struct page *khugepaged_alloc_hugepage(bool *wait)
2193{
2194 struct page *hpage;
2195
2196 do {
2197 hpage = alloc_hugepage(khugepaged_defrag());
2198 if (!hpage) {
2199 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2200 if (!*wait)
2201 return NULL;
2202
2203 *wait = false;
2204 khugepaged_alloc_sleep();
2205 } else
2206 count_vm_event(THP_COLLAPSE_ALLOC);
2207 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2208
2209 return hpage;
2210}
2211
2212static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2213{
2214 if (!*hpage)
2215 *hpage = khugepaged_alloc_hugepage(wait);
2216
2217 if (unlikely(!*hpage))
2218 return false;
2219
2220 return true;
2221}
2222
2223static struct page
2224*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2225 struct vm_area_struct *vma, unsigned long address,
2226 int node)
2227{
2228 up_read(&mm->mmap_sem);
2229 VM_BUG_ON(!*hpage);
2230 return *hpage;
2231}
2232#endif
2233
Bob Liufa475e52012-12-11 16:00:39 -08002234static bool hugepage_vma_check(struct vm_area_struct *vma)
2235{
2236 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2237 (vma->vm_flags & VM_NOHUGEPAGE))
2238 return false;
2239
2240 if (!vma->anon_vma || vma->vm_ops)
2241 return false;
2242 if (is_vma_temporary_stack(vma))
2243 return false;
2244 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2245 return true;
2246}
2247
Andrea Arcangeliba761492011-01-13 15:46:58 -08002248static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002249 unsigned long address,
2250 struct page **hpage,
2251 struct vm_area_struct *vma,
2252 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002253{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002254 pmd_t *pmd, _pmd;
2255 pte_t *pte;
2256 pgtable_t pgtable;
2257 struct page *new_page;
2258 spinlock_t *ptl;
2259 int isolated;
2260 unsigned long hstart, hend;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002261 unsigned long mmun_start; /* For mmu_notifiers */
2262 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002263
2264 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002265
Xiao Guangrong26234f32012-10-08 16:29:51 -07002266 /* release the mmap_sem read lock. */
2267 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2268 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002269 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002270
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002271 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002272 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002273
2274 /*
2275 * Prevent all access to pagetables with the exception of
2276 * gup_fast later hanlded by the ptep_clear_flush and the VM
2277 * handled by the anon_vma lock + PG_lock.
2278 */
2279 down_write(&mm->mmap_sem);
2280 if (unlikely(khugepaged_test_exit(mm)))
2281 goto out;
2282
2283 vma = find_vma(mm, address);
2284 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2285 hend = vma->vm_end & HPAGE_PMD_MASK;
2286 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2287 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002288 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002289 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002290 pmd = mm_find_pmd(mm, address);
2291 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002292 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002293 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002294 goto out;
2295
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002296 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002297
2298 pte = pte_offset_map(pmd, address);
2299 ptl = pte_lockptr(mm, pmd);
2300
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002301 mmun_start = address;
2302 mmun_end = address + HPAGE_PMD_SIZE;
2303 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002304 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2305 /*
2306 * After this gup_fast can't run anymore. This also removes
2307 * any huge TLB entry from the CPU so we won't allow
2308 * huge and small TLB entries for the same virtual address
2309 * to avoid the risk of CPU bugs in that area.
2310 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002311 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002313 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002314
2315 spin_lock(ptl);
2316 isolated = __collapse_huge_page_isolate(vma, address, pte);
2317 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002318
2319 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002320 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002321 spin_lock(&mm->page_table_lock);
2322 BUG_ON(!pmd_none(*pmd));
2323 set_pmd_at(mm, address, pmd, _pmd);
2324 spin_unlock(&mm->page_table_lock);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002325 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002326 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002327 }
2328
2329 /*
2330 * All pages are isolated and locked so anon_vma rmap
2331 * can't run anymore.
2332 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002333 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002334
2335 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002336 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002337 __SetPageUptodate(new_page);
2338 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002339
Bob Liub3092b32012-12-11 16:00:41 -08002340 _pmd = mk_huge_pmd(new_page, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002341
2342 /*
2343 * spin_lock() below is not the equivalent of smp_wmb(), so
2344 * this is needed to avoid the copy_huge_page writes to become
2345 * visible after the set_pmd_at() write.
2346 */
2347 smp_wmb();
2348
2349 spin_lock(&mm->page_table_lock);
2350 BUG_ON(!pmd_none(*pmd));
2351 page_add_new_anon_rmap(new_page, vma, address);
2352 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002353 update_mmu_cache_pmd(vma, address, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07002354 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002355 spin_unlock(&mm->page_table_lock);
2356
2357 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002358
Andrea Arcangeliba761492011-01-13 15:46:58 -08002359 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002360out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002361 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002362 return;
2363
Andrea Arcangelice83d212011-01-13 15:47:06 -08002364out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002365 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002366 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002367}
2368
2369static int khugepaged_scan_pmd(struct mm_struct *mm,
2370 struct vm_area_struct *vma,
2371 unsigned long address,
2372 struct page **hpage)
2373{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002374 pmd_t *pmd;
2375 pte_t *pte, *_pte;
2376 int ret = 0, referenced = 0, none = 0;
2377 struct page *page;
2378 unsigned long _address;
2379 spinlock_t *ptl;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002380 int node = -1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002381
2382 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2383
Bob Liu62190492012-12-11 16:00:37 -08002384 pmd = mm_find_pmd(mm, address);
2385 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002386 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002387 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002388 goto out;
2389
2390 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2391 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2392 _pte++, _address += PAGE_SIZE) {
2393 pte_t pteval = *_pte;
2394 if (pte_none(pteval)) {
2395 if (++none <= khugepaged_max_ptes_none)
2396 continue;
2397 else
2398 goto out_unmap;
2399 }
2400 if (!pte_present(pteval) || !pte_write(pteval))
2401 goto out_unmap;
2402 page = vm_normal_page(vma, _address, pteval);
2403 if (unlikely(!page))
2404 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002405 /*
2406 * Chose the node of the first page. This could
2407 * be more sophisticated and look at more pages,
2408 * but isn't for now.
2409 */
2410 if (node == -1)
2411 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002412 VM_BUG_ON(PageCompound(page));
2413 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2414 goto out_unmap;
2415 /* cannot use mapcount: can't collapse if there's a gup pin */
2416 if (page_count(page) != 1)
2417 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002418 if (pte_young(pteval) || PageReferenced(page) ||
2419 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002420 referenced = 1;
2421 }
2422 if (referenced)
2423 ret = 1;
2424out_unmap:
2425 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002426 if (ret)
2427 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002428 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002429out:
2430 return ret;
2431}
2432
2433static void collect_mm_slot(struct mm_slot *mm_slot)
2434{
2435 struct mm_struct *mm = mm_slot->mm;
2436
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002437 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002438
2439 if (khugepaged_test_exit(mm)) {
2440 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002441 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002442 list_del(&mm_slot->mm_node);
2443
2444 /*
2445 * Not strictly needed because the mm exited already.
2446 *
2447 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2448 */
2449
2450 /* khugepaged_mm_lock actually not necessary for the below */
2451 free_mm_slot(mm_slot);
2452 mmdrop(mm);
2453 }
2454}
2455
2456static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2457 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002458 __releases(&khugepaged_mm_lock)
2459 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002460{
2461 struct mm_slot *mm_slot;
2462 struct mm_struct *mm;
2463 struct vm_area_struct *vma;
2464 int progress = 0;
2465
2466 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002467 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002468
2469 if (khugepaged_scan.mm_slot)
2470 mm_slot = khugepaged_scan.mm_slot;
2471 else {
2472 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2473 struct mm_slot, mm_node);
2474 khugepaged_scan.address = 0;
2475 khugepaged_scan.mm_slot = mm_slot;
2476 }
2477 spin_unlock(&khugepaged_mm_lock);
2478
2479 mm = mm_slot->mm;
2480 down_read(&mm->mmap_sem);
2481 if (unlikely(khugepaged_test_exit(mm)))
2482 vma = NULL;
2483 else
2484 vma = find_vma(mm, khugepaged_scan.address);
2485
2486 progress++;
2487 for (; vma; vma = vma->vm_next) {
2488 unsigned long hstart, hend;
2489
2490 cond_resched();
2491 if (unlikely(khugepaged_test_exit(mm))) {
2492 progress++;
2493 break;
2494 }
Bob Liufa475e52012-12-11 16:00:39 -08002495 if (!hugepage_vma_check(vma)) {
2496skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002497 progress++;
2498 continue;
2499 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002500 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2501 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002502 if (hstart >= hend)
2503 goto skip;
2504 if (khugepaged_scan.address > hend)
2505 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002506 if (khugepaged_scan.address < hstart)
2507 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002508 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002509
2510 while (khugepaged_scan.address < hend) {
2511 int ret;
2512 cond_resched();
2513 if (unlikely(khugepaged_test_exit(mm)))
2514 goto breakouterloop;
2515
2516 VM_BUG_ON(khugepaged_scan.address < hstart ||
2517 khugepaged_scan.address + HPAGE_PMD_SIZE >
2518 hend);
2519 ret = khugepaged_scan_pmd(mm, vma,
2520 khugepaged_scan.address,
2521 hpage);
2522 /* move to next address */
2523 khugepaged_scan.address += HPAGE_PMD_SIZE;
2524 progress += HPAGE_PMD_NR;
2525 if (ret)
2526 /* we released mmap_sem so break loop */
2527 goto breakouterloop_mmap_sem;
2528 if (progress >= pages)
2529 goto breakouterloop;
2530 }
2531 }
2532breakouterloop:
2533 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2534breakouterloop_mmap_sem:
2535
2536 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002537 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002538 /*
2539 * Release the current mm_slot if this mm is about to die, or
2540 * if we scanned all vmas of this mm.
2541 */
2542 if (khugepaged_test_exit(mm) || !vma) {
2543 /*
2544 * Make sure that if mm_users is reaching zero while
2545 * khugepaged runs here, khugepaged_exit will find
2546 * mm_slot not pointing to the exiting mm.
2547 */
2548 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2549 khugepaged_scan.mm_slot = list_entry(
2550 mm_slot->mm_node.next,
2551 struct mm_slot, mm_node);
2552 khugepaged_scan.address = 0;
2553 } else {
2554 khugepaged_scan.mm_slot = NULL;
2555 khugepaged_full_scans++;
2556 }
2557
2558 collect_mm_slot(mm_slot);
2559 }
2560
2561 return progress;
2562}
2563
2564static int khugepaged_has_work(void)
2565{
2566 return !list_empty(&khugepaged_scan.mm_head) &&
2567 khugepaged_enabled();
2568}
2569
2570static int khugepaged_wait_event(void)
2571{
2572 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002573 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002574}
2575
Xiao Guangrongd5169042012-10-08 16:29:48 -07002576static void khugepaged_do_scan(void)
2577{
2578 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579 unsigned int progress = 0, pass_through_head = 0;
2580 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002581 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002582
2583 barrier(); /* write khugepaged_pages_to_scan to local stack */
2584
2585 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002586 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002587 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002588
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002589 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002590
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002591 if (unlikely(kthread_should_stop() || freezing(current)))
2592 break;
2593
Andrea Arcangeliba761492011-01-13 15:46:58 -08002594 spin_lock(&khugepaged_mm_lock);
2595 if (!khugepaged_scan.mm_slot)
2596 pass_through_head++;
2597 if (khugepaged_has_work() &&
2598 pass_through_head < 2)
2599 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002600 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002601 else
2602 progress = pages;
2603 spin_unlock(&khugepaged_mm_lock);
2604 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002605
Xiao Guangrongd5169042012-10-08 16:29:48 -07002606 if (!IS_ERR_OR_NULL(hpage))
2607 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002608}
2609
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002610static void khugepaged_wait_work(void)
2611{
2612 try_to_freeze();
2613
2614 if (khugepaged_has_work()) {
2615 if (!khugepaged_scan_sleep_millisecs)
2616 return;
2617
2618 wait_event_freezable_timeout(khugepaged_wait,
2619 kthread_should_stop(),
2620 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2621 return;
2622 }
2623
2624 if (khugepaged_enabled())
2625 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2626}
2627
Andrea Arcangeliba761492011-01-13 15:46:58 -08002628static int khugepaged(void *none)
2629{
2630 struct mm_slot *mm_slot;
2631
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002632 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002633 set_user_nice(current, 19);
2634
Xiao Guangrongb7231782012-10-08 16:29:54 -07002635 while (!kthread_should_stop()) {
2636 khugepaged_do_scan();
2637 khugepaged_wait_work();
2638 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002639
2640 spin_lock(&khugepaged_mm_lock);
2641 mm_slot = khugepaged_scan.mm_slot;
2642 khugepaged_scan.mm_slot = NULL;
2643 if (mm_slot)
2644 collect_mm_slot(mm_slot);
2645 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002646 return 0;
2647}
2648
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002649static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2650 unsigned long haddr, pmd_t *pmd)
2651{
2652 struct mm_struct *mm = vma->vm_mm;
2653 pgtable_t pgtable;
2654 pmd_t _pmd;
2655 int i;
2656
2657 pmdp_clear_flush(vma, haddr, pmd);
2658 /* leave pmd empty until pte is filled */
2659
2660 pgtable = pgtable_trans_huge_withdraw(mm);
2661 pmd_populate(mm, &_pmd, pgtable);
2662
2663 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2664 pte_t *pte, entry;
2665 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2666 entry = pte_mkspecial(entry);
2667 pte = pte_offset_map(&_pmd, haddr);
2668 VM_BUG_ON(!pte_none(*pte));
2669 set_pte_at(mm, haddr, pte, entry);
2670 pte_unmap(pte);
2671 }
2672 smp_wmb(); /* make pte visible before pmd */
2673 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002674 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002675}
2676
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002677void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2678 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002679{
2680 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002681 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002682 unsigned long haddr = address & HPAGE_PMD_MASK;
2683 unsigned long mmun_start; /* For mmu_notifiers */
2684 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002685
2686 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002687
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002688 mmun_start = haddr;
2689 mmun_end = haddr + HPAGE_PMD_SIZE;
2690 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002691 spin_lock(&mm->page_table_lock);
2692 if (unlikely(!pmd_trans_huge(*pmd))) {
2693 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002694 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2695 return;
2696 }
2697 if (is_huge_zero_pmd(*pmd)) {
2698 __split_huge_zero_page_pmd(vma, haddr, pmd);
2699 spin_unlock(&mm->page_table_lock);
2700 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002701 return;
2702 }
2703 page = pmd_page(*pmd);
2704 VM_BUG_ON(!page_count(page));
2705 get_page(page);
2706 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002707 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002708
2709 split_huge_page(page);
2710
2711 put_page(page);
2712 BUG_ON(pmd_trans_huge(*pmd));
2713}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002714
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002715void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2716 pmd_t *pmd)
2717{
2718 struct vm_area_struct *vma;
2719
2720 vma = find_vma(mm, address);
2721 BUG_ON(vma == NULL);
2722 split_huge_page_pmd(vma, address, pmd);
2723}
2724
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002725static void split_huge_page_address(struct mm_struct *mm,
2726 unsigned long address)
2727{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002728 pmd_t *pmd;
2729
2730 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2731
Bob Liu62190492012-12-11 16:00:37 -08002732 pmd = mm_find_pmd(mm, address);
2733 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002734 return;
2735 /*
2736 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2737 * materialize from under us.
2738 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002739 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002740}
2741
2742void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2743 unsigned long start,
2744 unsigned long end,
2745 long adjust_next)
2746{
2747 /*
2748 * If the new start address isn't hpage aligned and it could
2749 * previously contain an hugepage: check if we need to split
2750 * an huge pmd.
2751 */
2752 if (start & ~HPAGE_PMD_MASK &&
2753 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2754 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2755 split_huge_page_address(vma->vm_mm, start);
2756
2757 /*
2758 * If the new end address isn't hpage aligned and it could
2759 * previously contain an hugepage: check if we need to split
2760 * an huge pmd.
2761 */
2762 if (end & ~HPAGE_PMD_MASK &&
2763 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2764 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2765 split_huge_page_address(vma->vm_mm, end);
2766
2767 /*
2768 * If we're also updating the vma->vm_next->vm_start, if the new
2769 * vm_next->vm_start isn't page aligned and it could previously
2770 * contain an hugepage: check if we need to split an huge pmd.
2771 */
2772 if (adjust_next > 0) {
2773 struct vm_area_struct *next = vma->vm_next;
2774 unsigned long nstart = next->vm_start;
2775 nstart += adjust_next << PAGE_SHIFT;
2776 if (nstart & ~HPAGE_PMD_MASK &&
2777 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2778 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2779 split_huge_page_address(next->vm_mm, nstart);
2780 }
2781}