blob: 6001ee6347a9694f4a9b31ef9060913ff30440bf [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>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080023
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080024#include <asm/tlb.h>
25#include <asm/pgalloc.h>
26#include "internal.h"
27
Andrea Arcangeliba761492011-01-13 15:46:58 -080028/*
29 * By default transparent hugepage support is enabled for all mappings
30 * and khugepaged scans all mappings. Defrag is only invoked by
31 * khugepaged hugepage allocations and by page faults inside
32 * MADV_HUGEPAGE regions to avoid the risk of slowing down short lived
33 * allocations.
34 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080035unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080036#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080037 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080038#endif
39#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
40 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
41#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080042 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080043 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
44 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080045
46/* default scan 8*512 pte (or vmas) every 30 second */
47static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
48static unsigned int khugepaged_pages_collapsed;
49static unsigned int khugepaged_full_scans;
50static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
51/* during fragmentation poll the hugepage allocator once every minute */
52static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
53static struct task_struct *khugepaged_thread __read_mostly;
54static DEFINE_MUTEX(khugepaged_mutex);
55static DEFINE_SPINLOCK(khugepaged_mm_lock);
56static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
57/*
58 * default collapse hugepages if there is at least one pte mapped like
59 * it would have happened if the vma was large enough during page
60 * fault.
61 */
62static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
63
64static int khugepaged(void *none);
65static int mm_slots_hash_init(void);
66static int khugepaged_slab_init(void);
67static void khugepaged_slab_free(void);
68
69#define MM_SLOTS_HASH_HEADS 1024
70static struct hlist_head *mm_slots_hash __read_mostly;
71static 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
637 err = mm_slots_hash_init();
638 if (err) {
639 khugepaged_slab_free();
640 goto out;
641 }
642
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800643 register_shrinker(&huge_zero_page_shrinker);
644
Rik van Riel97562cd2011-01-13 15:47:12 -0800645 /*
646 * By default disable transparent hugepages on smaller systems,
647 * where the extra memory used could hurt more than TLB overhead
648 * is likely to save. The admin can still enable it through /sys.
649 */
650 if (totalram_pages < (512 << (20 - PAGE_SHIFT)))
651 transparent_hugepage_flags = 0;
652
Andrea Arcangeliba761492011-01-13 15:46:58 -0800653 start_khugepaged();
654
Shaohua Li569e5592012-01-12 17:19:11 -0800655 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800656out:
Shaohua Li569e5592012-01-12 17:19:11 -0800657 hugepage_exit_sysfs(hugepage_kobj);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800658 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800659}
660module_init(hugepage_init)
661
662static int __init setup_transparent_hugepage(char *str)
663{
664 int ret = 0;
665 if (!str)
666 goto out;
667 if (!strcmp(str, "always")) {
668 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
669 &transparent_hugepage_flags);
670 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
671 &transparent_hugepage_flags);
672 ret = 1;
673 } else if (!strcmp(str, "madvise")) {
674 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
675 &transparent_hugepage_flags);
676 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
677 &transparent_hugepage_flags);
678 ret = 1;
679 } else if (!strcmp(str, "never")) {
680 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
681 &transparent_hugepage_flags);
682 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
683 &transparent_hugepage_flags);
684 ret = 1;
685 }
686out:
687 if (!ret)
688 printk(KERN_WARNING
689 "transparent_hugepage= cannot parse, ignored\n");
690 return ret;
691}
692__setup("transparent_hugepage=", setup_transparent_hugepage);
693
Mel Gormanb32967f2012-11-19 12:35:47 +0000694pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800695{
696 if (likely(vma->vm_flags & VM_WRITE))
697 pmd = pmd_mkwrite(pmd);
698 return pmd;
699}
700
Bob Liub3092b32012-12-11 16:00:41 -0800701static inline pmd_t mk_huge_pmd(struct page *page, struct vm_area_struct *vma)
702{
703 pmd_t entry;
704 entry = mk_pmd(page, vma->vm_page_prot);
705 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
706 entry = pmd_mkhuge(entry);
707 return entry;
708}
709
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800710static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
711 struct vm_area_struct *vma,
712 unsigned long haddr, pmd_t *pmd,
713 struct page *page)
714{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800715 pgtable_t pgtable;
716
717 VM_BUG_ON(!PageCompound(page));
718 pgtable = pte_alloc_one(mm, haddr);
David Rientjesedad9d22012-05-29 15:06:17 -0700719 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800720 return VM_FAULT_OOM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800721
722 clear_huge_page(page, haddr, HPAGE_PMD_NR);
723 __SetPageUptodate(page);
724
725 spin_lock(&mm->page_table_lock);
726 if (unlikely(!pmd_none(*pmd))) {
727 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800728 mem_cgroup_uncharge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800729 put_page(page);
730 pte_free(mm, pgtable);
731 } else {
732 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -0800733 entry = mk_huge_pmd(page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800734 /*
735 * The spinlocking to take the lru_lock inside
736 * page_add_new_anon_rmap() acts as a full memory
737 * barrier to be sure clear_huge_page writes become
738 * visible after the set_pmd_at() write.
739 */
740 page_add_new_anon_rmap(page, vma, haddr);
741 set_pmd_at(mm, haddr, pmd, entry);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700742 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800743 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800744 mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800745 spin_unlock(&mm->page_table_lock);
746 }
747
David Rientjesaa2e8782012-05-29 15:06:17 -0700748 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800749}
750
Andi Kleencc5d4622011-03-22 16:33:13 -0700751static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800752{
Andi Kleencc5d4622011-03-22 16:33:13 -0700753 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800754}
755
756static inline struct page *alloc_hugepage_vma(int defrag,
757 struct vm_area_struct *vma,
Andi Kleencc5d4622011-03-22 16:33:13 -0700758 unsigned long haddr, int nd,
759 gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800760{
Andi Kleencc5d4622011-03-22 16:33:13 -0700761 return alloc_pages_vma(alloc_hugepage_gfpmask(defrag, extra_gfp),
Andi Kleen5c4b4be2011-03-04 17:36:32 -0800762 HPAGE_PMD_ORDER, vma, haddr, nd);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800763}
764
765#ifndef CONFIG_NUMA
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800766static inline struct page *alloc_hugepage(int defrag)
767{
Andi Kleencc5d4622011-03-22 16:33:13 -0700768 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800769 HPAGE_PMD_ORDER);
770}
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800771#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800772
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800773static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800774 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
775 unsigned long zero_pfn)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800776{
777 pmd_t entry;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800778 if (!pmd_none(*pmd))
779 return false;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800780 entry = pfn_pmd(zero_pfn, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800781 entry = pmd_wrprotect(entry);
782 entry = pmd_mkhuge(entry);
783 set_pmd_at(mm, haddr, pmd, entry);
784 pgtable_trans_huge_deposit(mm, pgtable);
785 mm->nr_ptes++;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800786 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800787}
788
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800789int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
790 unsigned long address, pmd_t *pmd,
791 unsigned int flags)
792{
793 struct page *page;
794 unsigned long haddr = address & HPAGE_PMD_MASK;
795 pte_t *pte;
796
797 if (haddr >= vma->vm_start && haddr + HPAGE_PMD_SIZE <= vma->vm_end) {
798 if (unlikely(anon_vma_prepare(vma)))
799 return VM_FAULT_OOM;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800800 if (unlikely(khugepaged_enter(vma)))
801 return VM_FAULT_OOM;
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800802 if (!(flags & FAULT_FLAG_WRITE) &&
803 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800804 pgtable_t pgtable;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800805 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800806 bool set;
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800807 pgtable = pte_alloc_one(mm, haddr);
808 if (unlikely(!pgtable))
809 return VM_FAULT_OOM;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800810 zero_pfn = get_huge_zero_page();
811 if (unlikely(!zero_pfn)) {
812 pte_free(mm, pgtable);
813 count_vm_event(THP_FAULT_FALLBACK);
814 goto out;
815 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800816 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800817 set = set_huge_zero_page(pgtable, mm, vma, haddr, pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800818 zero_pfn);
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800819 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800820 if (!set) {
821 pte_free(mm, pgtable);
822 put_huge_zero_page();
823 }
Kirill A. Shutemov80371952012-12-12 13:51:02 -0800824 return 0;
825 }
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800826 page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -0700827 vma, haddr, numa_node_id(), 0);
Andi Kleen81ab4202011-04-14 15:22:06 -0700828 if (unlikely(!page)) {
829 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800830 goto out;
Andi Kleen81ab4202011-04-14 15:22:06 -0700831 }
832 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800833 if (unlikely(mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))) {
834 put_page(page);
835 goto out;
836 }
David Rientjesedad9d22012-05-29 15:06:17 -0700837 if (unlikely(__do_huge_pmd_anonymous_page(mm, vma, haddr, pmd,
838 page))) {
839 mem_cgroup_uncharge_page(page);
840 put_page(page);
841 goto out;
842 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800843
David Rientjesedad9d22012-05-29 15:06:17 -0700844 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800845 }
846out:
847 /*
848 * Use __pte_alloc instead of pte_alloc_map, because we can't
849 * run pte_offset_map on the pmd, if an huge pmd could
850 * materialize from under us from a different thread.
851 */
Mel Gorman4fd01772011-10-12 21:06:51 +0200852 if (unlikely(pmd_none(*pmd)) &&
853 unlikely(__pte_alloc(mm, vma, pmd, address)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800854 return VM_FAULT_OOM;
855 /* if an huge pmd materialized from under us just retry later */
856 if (unlikely(pmd_trans_huge(*pmd)))
857 return 0;
858 /*
859 * A regular pmd is established and it can't morph into a huge pmd
860 * from under us anymore at this point because we hold the mmap_sem
861 * read mode and khugepaged takes it in write mode. So now it's
862 * safe to run pte_offset_map().
863 */
864 pte = pte_offset_map(pmd, address);
865 return handle_pte_fault(mm, vma, address, pte, pmd, flags);
866}
867
868int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
869 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
870 struct vm_area_struct *vma)
871{
872 struct page *src_page;
873 pmd_t pmd;
874 pgtable_t pgtable;
875 int ret;
876
877 ret = -ENOMEM;
878 pgtable = pte_alloc_one(dst_mm, addr);
879 if (unlikely(!pgtable))
880 goto out;
881
882 spin_lock(&dst_mm->page_table_lock);
883 spin_lock_nested(&src_mm->page_table_lock, SINGLE_DEPTH_NESTING);
884
885 ret = -EAGAIN;
886 pmd = *src_pmd;
887 if (unlikely(!pmd_trans_huge(pmd))) {
888 pte_free(dst_mm, pgtable);
889 goto out_unlock;
890 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800891 /*
892 * mm->page_table_lock is enough to be sure that huge zero pmd is not
893 * under splitting since we don't split the page itself, only pmd to
894 * a page table.
895 */
896 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800897 unsigned long zero_pfn;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800898 bool set;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800899 /*
900 * get_huge_zero_page() will never allocate a new page here,
901 * since we already have a zero page to copy. It just takes a
902 * reference.
903 */
904 zero_pfn = get_huge_zero_page();
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800905 set = set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800906 zero_pfn);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800907 BUG_ON(!set); /* unexpected !pmd_none(dst_pmd) */
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800908 ret = 0;
909 goto out_unlock;
910 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800911 if (unlikely(pmd_trans_splitting(pmd))) {
912 /* split huge page running from under us */
913 spin_unlock(&src_mm->page_table_lock);
914 spin_unlock(&dst_mm->page_table_lock);
915 pte_free(dst_mm, pgtable);
916
917 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
918 goto out;
919 }
920 src_page = pmd_page(pmd);
921 VM_BUG_ON(!PageHead(src_page));
922 get_page(src_page);
923 page_dup_rmap(src_page);
924 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
925
926 pmdp_set_wrprotect(src_mm, addr, src_pmd);
927 pmd = pmd_mkold(pmd_wrprotect(pmd));
928 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -0700929 pgtable_trans_huge_deposit(dst_mm, pgtable);
Andrea Arcangeli1c641e82012-03-05 14:59:20 -0800930 dst_mm->nr_ptes++;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800931
932 ret = 0;
933out_unlock:
934 spin_unlock(&src_mm->page_table_lock);
935 spin_unlock(&dst_mm->page_table_lock);
936out:
937 return ret;
938}
939
Will Deacona1dd4502012-12-11 16:01:27 -0800940void huge_pmd_set_accessed(struct mm_struct *mm,
941 struct vm_area_struct *vma,
942 unsigned long address,
943 pmd_t *pmd, pmd_t orig_pmd,
944 int dirty)
945{
946 pmd_t entry;
947 unsigned long haddr;
948
949 spin_lock(&mm->page_table_lock);
950 if (unlikely(!pmd_same(*pmd, orig_pmd)))
951 goto unlock;
952
953 entry = pmd_mkyoung(orig_pmd);
954 haddr = address & HPAGE_PMD_MASK;
955 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
956 update_mmu_cache_pmd(vma, address, pmd);
957
958unlock:
959 spin_unlock(&mm->page_table_lock);
960}
961
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800962static int do_huge_pmd_wp_zero_page_fallback(struct mm_struct *mm,
963 struct vm_area_struct *vma, unsigned long address,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800964 pmd_t *pmd, pmd_t orig_pmd, unsigned long haddr)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800965{
966 pgtable_t pgtable;
967 pmd_t _pmd;
968 struct page *page;
969 int i, ret = 0;
970 unsigned long mmun_start; /* For mmu_notifiers */
971 unsigned long mmun_end; /* For mmu_notifiers */
972
973 page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
974 if (!page) {
975 ret |= VM_FAULT_OOM;
976 goto out;
977 }
978
979 if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL)) {
980 put_page(page);
981 ret |= VM_FAULT_OOM;
982 goto out;
983 }
984
985 clear_user_highpage(page, address);
986 __SetPageUptodate(page);
987
988 mmun_start = haddr;
989 mmun_end = haddr + HPAGE_PMD_SIZE;
990 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
991
992 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -0800993 if (unlikely(!pmd_same(*pmd, orig_pmd)))
994 goto out_free_page;
995
Kirill A. Shutemov93b47962012-12-12 13:50:54 -0800996 pmdp_clear_flush(vma, haddr, pmd);
997 /* leave pmd empty until pte is filled */
998
999 pgtable = pgtable_trans_huge_withdraw(mm);
1000 pmd_populate(mm, &_pmd, pgtable);
1001
1002 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1003 pte_t *pte, entry;
1004 if (haddr == (address & PAGE_MASK)) {
1005 entry = mk_pte(page, vma->vm_page_prot);
1006 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1007 page_add_new_anon_rmap(page, vma, haddr);
1008 } else {
1009 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
1010 entry = pte_mkspecial(entry);
1011 }
1012 pte = pte_offset_map(&_pmd, haddr);
1013 VM_BUG_ON(!pte_none(*pte));
1014 set_pte_at(mm, haddr, pte, entry);
1015 pte_unmap(pte);
1016 }
1017 smp_wmb(); /* make pte visible before pmd */
1018 pmd_populate(mm, pmd, pgtable);
1019 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001020 put_huge_zero_page();
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001021 inc_mm_counter(mm, MM_ANONPAGES);
1022
1023 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1024
1025 ret |= VM_FAULT_WRITE;
1026out:
1027 return ret;
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001028out_free_page:
1029 spin_unlock(&mm->page_table_lock);
1030 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1031 mem_cgroup_uncharge_page(page);
1032 put_page(page);
1033 goto out;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001034}
1035
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001036static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1037 struct vm_area_struct *vma,
1038 unsigned long address,
1039 pmd_t *pmd, pmd_t orig_pmd,
1040 struct page *page,
1041 unsigned long haddr)
1042{
1043 pgtable_t pgtable;
1044 pmd_t _pmd;
1045 int ret = 0, i;
1046 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001047 unsigned long mmun_start; /* For mmu_notifiers */
1048 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001049
1050 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1051 GFP_KERNEL);
1052 if (unlikely(!pages)) {
1053 ret |= VM_FAULT_OOM;
1054 goto out;
1055 }
1056
1057 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001058 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1059 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001060 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001061 if (unlikely(!pages[i] ||
1062 mem_cgroup_newpage_charge(pages[i], mm,
1063 GFP_KERNEL))) {
1064 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001065 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001066 mem_cgroup_uncharge_start();
1067 while (--i >= 0) {
1068 mem_cgroup_uncharge_page(pages[i]);
1069 put_page(pages[i]);
1070 }
1071 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001072 kfree(pages);
1073 ret |= VM_FAULT_OOM;
1074 goto out;
1075 }
1076 }
1077
1078 for (i = 0; i < HPAGE_PMD_NR; i++) {
1079 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001080 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001081 __SetPageUptodate(pages[i]);
1082 cond_resched();
1083 }
1084
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001085 mmun_start = haddr;
1086 mmun_end = haddr + HPAGE_PMD_SIZE;
1087 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1088
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001089 spin_lock(&mm->page_table_lock);
1090 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1091 goto out_free_pages;
1092 VM_BUG_ON(!PageHead(page));
1093
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001094 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001095 /* leave pmd empty until pte is filled */
1096
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001097 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098 pmd_populate(mm, &_pmd, pgtable);
1099
1100 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1101 pte_t *pte, entry;
1102 entry = mk_pte(pages[i], vma->vm_page_prot);
1103 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1104 page_add_new_anon_rmap(pages[i], vma, haddr);
1105 pte = pte_offset_map(&_pmd, haddr);
1106 VM_BUG_ON(!pte_none(*pte));
1107 set_pte_at(mm, haddr, pte, entry);
1108 pte_unmap(pte);
1109 }
1110 kfree(pages);
1111
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001112 smp_wmb(); /* make pte visible before pmd */
1113 pmd_populate(mm, pmd, pgtable);
1114 page_remove_rmap(page);
1115 spin_unlock(&mm->page_table_lock);
1116
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001117 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1118
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001119 ret |= VM_FAULT_WRITE;
1120 put_page(page);
1121
1122out:
1123 return ret;
1124
1125out_free_pages:
1126 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001127 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001128 mem_cgroup_uncharge_start();
1129 for (i = 0; i < HPAGE_PMD_NR; i++) {
1130 mem_cgroup_uncharge_page(pages[i]);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001131 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001132 }
1133 mem_cgroup_uncharge_end();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001134 kfree(pages);
1135 goto out;
1136}
1137
1138int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1139 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1140{
1141 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001142 struct page *page = NULL, *new_page;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001143 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001144 unsigned long mmun_start; /* For mmu_notifiers */
1145 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001146
1147 VM_BUG_ON(!vma->anon_vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001148 haddr = address & HPAGE_PMD_MASK;
1149 if (is_huge_zero_pmd(orig_pmd))
1150 goto alloc;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001151 spin_lock(&mm->page_table_lock);
1152 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1153 goto out_unlock;
1154
1155 page = pmd_page(orig_pmd);
1156 VM_BUG_ON(!PageCompound(page) || !PageHead(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001157 if (page_mapcount(page) == 1) {
1158 pmd_t entry;
1159 entry = pmd_mkyoung(orig_pmd);
1160 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1161 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001162 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001163 ret |= VM_FAULT_WRITE;
1164 goto out_unlock;
1165 }
1166 get_page(page);
1167 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001168alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001169 if (transparent_hugepage_enabled(vma) &&
1170 !transparent_hugepage_debug_cow())
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08001171 new_page = alloc_hugepage_vma(transparent_hugepage_defrag(vma),
Andi Kleencc5d4622011-03-22 16:33:13 -07001172 vma, haddr, numa_node_id(), 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001173 else
1174 new_page = NULL;
1175
1176 if (unlikely(!new_page)) {
Andi Kleen81ab4202011-04-14 15:22:06 -07001177 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001178 if (is_huge_zero_pmd(orig_pmd)) {
1179 ret = do_huge_pmd_wp_zero_page_fallback(mm, vma,
Kirill A. Shutemov3ea41e62012-12-12 13:51:14 -08001180 address, pmd, orig_pmd, haddr);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001181 } else {
1182 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1183 pmd, orig_pmd, page, haddr);
1184 if (ret & VM_FAULT_OOM)
1185 split_huge_page(page);
1186 put_page(page);
1187 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001188 goto out;
1189 }
Andi Kleen81ab4202011-04-14 15:22:06 -07001190 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001191
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001192 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))) {
1193 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001194 if (page) {
1195 split_huge_page(page);
1196 put_page(page);
1197 }
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001198 ret |= VM_FAULT_OOM;
1199 goto out;
1200 }
1201
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001202 if (is_huge_zero_pmd(orig_pmd))
1203 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1204 else
1205 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001206 __SetPageUptodate(new_page);
1207
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001208 mmun_start = haddr;
1209 mmun_end = haddr + HPAGE_PMD_SIZE;
1210 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1211
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001212 spin_lock(&mm->page_table_lock);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001213 if (page)
1214 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001215 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
David Rientjes6f60b692012-05-29 15:06:26 -07001216 spin_unlock(&mm->page_table_lock);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001217 mem_cgroup_uncharge_page(new_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001219 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001220 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001221 pmd_t entry;
Bob Liub3092b32012-12-11 16:00:41 -08001222 entry = mk_huge_pmd(new_page, vma);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001223 pmdp_clear_flush(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001224 page_add_new_anon_rmap(new_page, vma, haddr);
1225 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001226 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001227 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001228 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001229 put_huge_zero_page();
1230 } else {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001231 VM_BUG_ON(!PageHead(page));
1232 page_remove_rmap(page);
1233 put_page(page);
1234 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001235 ret |= VM_FAULT_WRITE;
1236 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001237 spin_unlock(&mm->page_table_lock);
1238out_mn:
1239 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1240out:
1241 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242out_unlock:
1243 spin_unlock(&mm->page_table_lock);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001244 return ret;
1245}
1246
David Rientjesb676b292012-10-08 16:34:03 -07001247struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001248 unsigned long addr,
1249 pmd_t *pmd,
1250 unsigned int flags)
1251{
David Rientjesb676b292012-10-08 16:34:03 -07001252 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001253 struct page *page = NULL;
1254
1255 assert_spin_locked(&mm->page_table_lock);
1256
1257 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1258 goto out;
1259
1260 page = pmd_page(*pmd);
1261 VM_BUG_ON(!PageHead(page));
1262 if (flags & FOLL_TOUCH) {
1263 pmd_t _pmd;
1264 /*
1265 * We should set the dirty bit only for FOLL_WRITE but
1266 * for now the dirty bit in the pmd is meaningless.
1267 * And if the dirty bit will become meaningful and
1268 * we'll only set it with FOLL_WRITE, an atomic
1269 * set_bit will be required on the pmd to set the
1270 * young bit, instead of the current set_pmd_at.
1271 */
1272 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1273 set_pmd_at(mm, addr & HPAGE_PMD_MASK, pmd, _pmd);
1274 }
David Rientjesb676b292012-10-08 16:34:03 -07001275 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
1276 if (page->mapping && trylock_page(page)) {
1277 lru_add_drain();
1278 if (page->mapping)
1279 mlock_vma_page(page);
1280 unlock_page(page);
1281 }
1282 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001283 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
1284 VM_BUG_ON(!PageCompound(page));
1285 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001286 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001287
1288out:
1289 return page;
1290}
1291
Mel Gormand10e63f2012-10-25 14:16:31 +02001292/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001293int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1294 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001295{
Mel Gormanb32967f2012-11-19 12:35:47 +00001296 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001297 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001298 int target_nid;
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001299 int current_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001300 bool migrated;
1301 bool page_locked = false;
Mel Gormand10e63f2012-10-25 14:16:31 +02001302
1303 spin_lock(&mm->page_table_lock);
1304 if (unlikely(!pmd_same(pmd, *pmdp)))
1305 goto out_unlock;
1306
1307 page = pmd_page(pmd);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001308 get_page(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001309 current_nid = page_to_nid(page);
1310 count_vm_numa_event(NUMA_HINT_FAULTS);
1311 if (current_nid == numa_node_id())
1312 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001313
1314 target_nid = mpol_misplaced(page, vma, haddr);
Mel Gormanb32967f2012-11-19 12:35:47 +00001315 if (target_nid == -1) {
1316 put_page(page);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001317 goto clear_pmdnuma;
Mel Gormanb32967f2012-11-19 12:35:47 +00001318 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001319
Mel Gormanb32967f2012-11-19 12:35:47 +00001320 /* Acquire the page lock to serialise THP migrations */
1321 spin_unlock(&mm->page_table_lock);
1322 lock_page(page);
1323 page_locked = true;
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001324
Mel Gormanb32967f2012-11-19 12:35:47 +00001325 /* Confirm the PTE did not while locked */
1326 spin_lock(&mm->page_table_lock);
1327 if (unlikely(!pmd_same(pmd, *pmdp))) {
1328 unlock_page(page);
1329 put_page(page);
1330 goto out_unlock;
1331 }
1332 spin_unlock(&mm->page_table_lock);
1333
1334 /* Migrate the THP to the requested node */
1335 migrated = migrate_misplaced_transhuge_page(mm, vma,
1336 pmdp, pmd, addr,
1337 page, target_nid);
1338 if (migrated)
1339 current_nid = target_nid;
1340 else {
1341 spin_lock(&mm->page_table_lock);
1342 if (unlikely(!pmd_same(pmd, *pmdp))) {
1343 unlock_page(page);
1344 goto out_unlock;
1345 }
1346 goto clear_pmdnuma;
1347 }
1348
1349 task_numa_fault(current_nid, HPAGE_PMD_NR, migrated);
Mel Gorman4daae3b2012-11-02 11:33:45 +00001350 return 0;
1351
1352clear_pmdnuma:
Mel Gormand10e63f2012-10-25 14:16:31 +02001353 pmd = pmd_mknonnuma(pmd);
1354 set_pmd_at(mm, haddr, pmdp, pmd);
1355 VM_BUG_ON(pmd_numa(*pmdp));
1356 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormanb32967f2012-11-19 12:35:47 +00001357 if (page_locked)
1358 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001359
1360out_unlock:
1361 spin_unlock(&mm->page_table_lock);
Mel Gormanb32967f2012-11-19 12:35:47 +00001362 if (current_nid != -1)
1363 task_numa_fault(current_nid, HPAGE_PMD_NR, migrated);
Mel Gormand10e63f2012-10-25 14:16:31 +02001364 return 0;
1365}
1366
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001367int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001368 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001369{
1370 int ret = 0;
1371
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001372 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1373 struct page *page;
1374 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001375 pmd_t orig_pmd;
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001376 pgtable = pgtable_trans_huge_withdraw(tlb->mm);
David Millerf5c8ad42012-10-08 16:34:26 -07001377 orig_pmd = pmdp_get_and_clear(tlb->mm, addr, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001378 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001379 if (is_huge_zero_pmd(orig_pmd)) {
1380 tlb->mm->nr_ptes--;
1381 spin_unlock(&tlb->mm->page_table_lock);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001382 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001383 } else {
1384 page = pmd_page(orig_pmd);
1385 page_remove_rmap(page);
1386 VM_BUG_ON(page_mapcount(page) < 0);
1387 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1388 VM_BUG_ON(!PageHead(page));
1389 tlb->mm->nr_ptes--;
1390 spin_unlock(&tlb->mm->page_table_lock);
1391 tlb_remove_page(tlb, page);
1392 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001393 pte_free(tlb->mm, pgtable);
1394 ret = 1;
1395 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001396 return ret;
1397}
1398
Johannes Weiner0ca16342011-01-13 15:47:02 -08001399int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
1400 unsigned long addr, unsigned long end,
1401 unsigned char *vec)
1402{
1403 int ret = 0;
1404
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001405 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1406 /*
1407 * All logical pages in the range are present
1408 * if backed by a huge page.
1409 */
Johannes Weiner0ca16342011-01-13 15:47:02 -08001410 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001411 memset(vec, 1, (end - addr) >> PAGE_SHIFT);
1412 ret = 1;
1413 }
Johannes Weiner0ca16342011-01-13 15:47:02 -08001414
1415 return ret;
1416}
1417
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001418int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1419 unsigned long old_addr,
1420 unsigned long new_addr, unsigned long old_end,
1421 pmd_t *old_pmd, pmd_t *new_pmd)
1422{
1423 int ret = 0;
1424 pmd_t pmd;
1425
1426 struct mm_struct *mm = vma->vm_mm;
1427
1428 if ((old_addr & ~HPAGE_PMD_MASK) ||
1429 (new_addr & ~HPAGE_PMD_MASK) ||
1430 old_end - old_addr < HPAGE_PMD_SIZE ||
1431 (new_vma->vm_flags & VM_NOHUGEPAGE))
1432 goto out;
1433
1434 /*
1435 * The destination pmd shouldn't be established, free_pgtables()
1436 * should have release it.
1437 */
1438 if (WARN_ON(!pmd_none(*new_pmd))) {
1439 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1440 goto out;
1441 }
1442
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001443 ret = __pmd_trans_huge_lock(old_pmd, vma);
1444 if (ret == 1) {
1445 pmd = pmdp_get_and_clear(mm, old_addr, old_pmd);
1446 VM_BUG_ON(!pmd_none(*new_pmd));
1447 set_pmd_at(mm, new_addr, new_pmd, pmd);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001448 spin_unlock(&mm->page_table_lock);
1449 }
1450out:
1451 return ret;
1452}
1453
Johannes Weinercd7548a2011-01-13 15:47:04 -08001454int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001455 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001456{
1457 struct mm_struct *mm = vma->vm_mm;
1458 int ret = 0;
1459
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001460 if (__pmd_trans_huge_lock(pmd, vma) == 1) {
1461 pmd_t entry;
1462 entry = pmdp_get_and_clear(mm, addr, pmd);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001463 if (!prot_numa) {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001464 entry = pmd_modify(entry, newprot);
Hugh Dickinsa4f1de12012-12-16 18:56:58 -08001465 BUG_ON(pmd_write(entry));
1466 } else {
Mel Gorman4b10e7d2012-10-25 14:16:32 +02001467 struct page *page = pmd_page(*pmd);
1468
1469 /* only check non-shared pages */
1470 if (page_mapcount(page) == 1 &&
1471 !pmd_numa(*pmd)) {
1472 entry = pmd_mknuma(entry);
1473 }
1474 }
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001475 set_pmd_at(mm, addr, pmd, entry);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001476 spin_unlock(&vma->vm_mm->page_table_lock);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001477 ret = 1;
1478 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001479
1480 return ret;
1481}
1482
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001483/*
1484 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1485 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1486 *
1487 * Note that if it returns 1, this routine returns without unlocking page
1488 * table locks. So callers must unlock them.
1489 */
1490int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
1491{
1492 spin_lock(&vma->vm_mm->page_table_lock);
1493 if (likely(pmd_trans_huge(*pmd))) {
1494 if (unlikely(pmd_trans_splitting(*pmd))) {
1495 spin_unlock(&vma->vm_mm->page_table_lock);
1496 wait_split_huge_page(vma->anon_vma, pmd);
1497 return -1;
1498 } else {
1499 /* Thp mapped by 'pmd' is stable, so we can
1500 * handle it as it is. */
1501 return 1;
1502 }
1503 }
1504 spin_unlock(&vma->vm_mm->page_table_lock);
1505 return 0;
1506}
1507
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001508pmd_t *page_check_address_pmd(struct page *page,
1509 struct mm_struct *mm,
1510 unsigned long address,
1511 enum page_check_address_pmd_flag flag)
1512{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001513 pmd_t *pmd, *ret = NULL;
1514
1515 if (address & ~HPAGE_PMD_MASK)
1516 goto out;
1517
Bob Liu62190492012-12-11 16:00:37 -08001518 pmd = mm_find_pmd(mm, address);
1519 if (!pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001520 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001521 if (pmd_none(*pmd))
1522 goto out;
1523 if (pmd_page(*pmd) != page)
1524 goto out;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001525 /*
1526 * split_vma() may create temporary aliased mappings. There is
1527 * no risk as long as all huge pmd are found and have their
1528 * splitting bit set before __split_huge_page_refcount
1529 * runs. Finding the same huge pmd more than once during the
1530 * same rmap walk is not a problem.
1531 */
1532 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1533 pmd_trans_splitting(*pmd))
1534 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001535 if (pmd_trans_huge(*pmd)) {
1536 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1537 !pmd_trans_splitting(*pmd));
1538 ret = pmd;
1539 }
1540out:
1541 return ret;
1542}
1543
1544static int __split_huge_page_splitting(struct page *page,
1545 struct vm_area_struct *vma,
1546 unsigned long address)
1547{
1548 struct mm_struct *mm = vma->vm_mm;
1549 pmd_t *pmd;
1550 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001551 /* For mmu_notifiers */
1552 const unsigned long mmun_start = address;
1553 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001554
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001555 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001556 spin_lock(&mm->page_table_lock);
1557 pmd = page_check_address_pmd(page, mm, address,
1558 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG);
1559 if (pmd) {
1560 /*
1561 * We can't temporarily set the pmd to null in order
1562 * to split it, the pmd must remain marked huge at all
1563 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001564 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001565 * serialize against split_huge_page*.
1566 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001567 pmdp_splitting_flush(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001568 ret = 1;
1569 }
1570 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001571 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001572
1573 return ret;
1574}
1575
1576static void __split_huge_page_refcount(struct page *page)
1577{
1578 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001579 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001580 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001581 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001582
1583 /* prevent PageLRU to go away from under us, and freeze lru stats */
1584 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001585 lruvec = mem_cgroup_page_lruvec(page, zone);
1586
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001587 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001588 /* complete memcg works before add pages to LRU */
1589 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001590
Shaohua Li45676882012-01-12 17:19:18 -08001591 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001592 struct page *page_tail = page + i;
1593
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001594 /* tail_page->_mapcount cannot change */
1595 BUG_ON(page_mapcount(page_tail) < 0);
1596 tail_count += page_mapcount(page_tail);
1597 /* check for overflow */
1598 BUG_ON(tail_count < 0);
1599 BUG_ON(atomic_read(&page_tail->_count) != 0);
1600 /*
1601 * tail_page->_count is zero and not changing from
1602 * under us. But get_page_unless_zero() may be running
1603 * from under us on the tail_page. If we used
1604 * atomic_set() below instead of atomic_add(), we
1605 * would then run atomic_set() concurrently with
1606 * get_page_unless_zero(), and atomic_set() is
1607 * implemented in C not using locked ops. spin_unlock
1608 * on x86 sometime uses locked ops because of PPro
1609 * errata 66, 92, so unless somebody can guarantee
1610 * atomic_set() here would be safe on all archs (and
1611 * not only on x86), it's safer to use atomic_add().
1612 */
1613 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1614 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001615
1616 /* after clearing PageTail the gup refcount can be released */
1617 smp_mb();
1618
Jin Dongminga6d30dd2011-02-01 15:52:40 -08001619 /*
1620 * retain hwpoison flag of the poisoned tail page:
1621 * fix for the unsuitable process killed on Guest Machine(KVM)
1622 * by the memory-failure.
1623 */
1624 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP | __PG_HWPOISON;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001625 page_tail->flags |= (page->flags &
1626 ((1L << PG_referenced) |
1627 (1L << PG_swapbacked) |
1628 (1L << PG_mlocked) |
1629 (1L << PG_uptodate)));
1630 page_tail->flags |= (1L << PG_dirty);
1631
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001632 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001633 smp_wmb();
1634
1635 /*
1636 * __split_huge_page_splitting() already set the
1637 * splitting bit in all pmd that could map this
1638 * hugepage, that will ensure no CPU can alter the
1639 * mapcount on the head page. The mapcount is only
1640 * accounted in the head page and it has to be
1641 * transferred to all tail pages in the below code. So
1642 * for this code to be safe, the split the mapcount
1643 * can't change. But that doesn't mean userland can't
1644 * keep changing and reading the page contents while
1645 * we transfer the mapcount, so the pmd splitting
1646 * status is achieved setting a reserved bit in the
1647 * pmd, not by clearing the present bit.
1648 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001649 page_tail->_mapcount = page->_mapcount;
1650
1651 BUG_ON(page_tail->mapping);
1652 page_tail->mapping = page->mapping;
1653
Shaohua Li45676882012-01-12 17:19:18 -08001654 page_tail->index = page->index + i;
Hillf Danton5aa80372012-11-27 14:40:32 +00001655 page_xchg_last_nid(page_tail, page_last_nid(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001656
1657 BUG_ON(!PageAnon(page_tail));
1658 BUG_ON(!PageUptodate(page_tail));
1659 BUG_ON(!PageDirty(page_tail));
1660 BUG_ON(!PageSwapBacked(page_tail));
1661
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001662 lru_add_page_tail(page, page_tail, lruvec);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001663 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001664 atomic_sub(tail_count, &page->_count);
1665 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001666
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001667 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001668 __mod_zone_page_state(zone, NR_ANON_PAGES, HPAGE_PMD_NR);
1669
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001670 ClearPageCompound(page);
1671 compound_unlock(page);
1672 spin_unlock_irq(&zone->lru_lock);
1673
1674 for (i = 1; i < HPAGE_PMD_NR; i++) {
1675 struct page *page_tail = page + i;
1676 BUG_ON(page_count(page_tail) <= 0);
1677 /*
1678 * Tail pages may be freed if there wasn't any mapping
1679 * like if add_to_swap() is running on a lru page that
1680 * had its mapping zapped. And freeing these pages
1681 * requires taking the lru_lock so we do the put_page
1682 * of the tail pages after the split is complete.
1683 */
1684 put_page(page_tail);
1685 }
1686
1687 /*
1688 * Only the head page (now become a regular page) is required
1689 * to be pinned by the caller.
1690 */
1691 BUG_ON(page_count(page) <= 0);
1692}
1693
1694static int __split_huge_page_map(struct page *page,
1695 struct vm_area_struct *vma,
1696 unsigned long address)
1697{
1698 struct mm_struct *mm = vma->vm_mm;
1699 pmd_t *pmd, _pmd;
1700 int ret = 0, i;
1701 pgtable_t pgtable;
1702 unsigned long haddr;
1703
1704 spin_lock(&mm->page_table_lock);
1705 pmd = page_check_address_pmd(page, mm, address,
1706 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG);
1707 if (pmd) {
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001708 pgtable = pgtable_trans_huge_withdraw(mm);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001709 pmd_populate(mm, &_pmd, pgtable);
1710
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001711 haddr = address;
1712 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001713 pte_t *pte, entry;
1714 BUG_ON(PageCompound(page+i));
1715 entry = mk_pte(page + i, vma->vm_page_prot);
1716 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1717 if (!pmd_write(*pmd))
1718 entry = pte_wrprotect(entry);
1719 else
1720 BUG_ON(page_mapcount(page) != 1);
1721 if (!pmd_young(*pmd))
1722 entry = pte_mkold(entry);
Andrea Arcangeli1ba6e0b2012-10-04 01:51:06 +02001723 if (pmd_numa(*pmd))
1724 entry = pte_mknuma(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001725 pte = pte_offset_map(&_pmd, haddr);
1726 BUG_ON(!pte_none(*pte));
1727 set_pte_at(mm, haddr, pte, entry);
1728 pte_unmap(pte);
1729 }
1730
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001731 smp_wmb(); /* make pte visible before pmd */
1732 /*
1733 * Up to this point the pmd is present and huge and
1734 * userland has the whole access to the hugepage
1735 * during the split (which happens in place). If we
1736 * overwrite the pmd with the not-huge version
1737 * pointing to the pte here (which of course we could
1738 * if all CPUs were bug free), userland could trigger
1739 * a small page size TLB miss on the small sized TLB
1740 * while the hugepage TLB entry is still established
1741 * in the huge TLB. Some CPU doesn't like that. See
1742 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1743 * Erratum 383 on page 93. Intel should be safe but is
1744 * also warns that it's only safe if the permission
1745 * and cache attributes of the two entries loaded in
1746 * the two TLB is identical (which should be the case
1747 * here). But it is generally safer to never allow
1748 * small and huge TLB entries for the same virtual
1749 * address to be loaded simultaneously. So instead of
1750 * doing "pmd_populate(); flush_tlb_range();" we first
1751 * mark the current pmd notpresent (atomically because
1752 * here the pmd_trans_huge and pmd_trans_splitting
1753 * must remain set at all times on the pmd until the
1754 * split is complete for this pmd), then we flush the
1755 * SMP TLB and finally we write the non-huge version
1756 * of the pmd entry with pmd_populate.
1757 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001758 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001759 pmd_populate(mm, pmd, pgtable);
1760 ret = 1;
1761 }
1762 spin_unlock(&mm->page_table_lock);
1763
1764 return ret;
1765}
1766
Ingo Molnar5a505082012-12-02 19:56:46 +00001767/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001768static void __split_huge_page(struct page *page,
1769 struct anon_vma *anon_vma)
1770{
1771 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001772 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001773 struct anon_vma_chain *avc;
1774
1775 BUG_ON(!PageHead(page));
1776 BUG_ON(PageTail(page));
1777
1778 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001779 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001780 struct vm_area_struct *vma = avc->vma;
1781 unsigned long addr = vma_address(page, vma);
1782 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001783 mapcount += __split_huge_page_splitting(page, vma, addr);
1784 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001785 /*
1786 * It is critical that new vmas are added to the tail of the
1787 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1788 * and establishes a child pmd before
1789 * __split_huge_page_splitting() freezes the parent pmd (so if
1790 * we fail to prevent copy_huge_pmd() from running until the
1791 * whole __split_huge_page() is complete), we will still see
1792 * the newly established pmd of the child later during the
1793 * walk, to be able to set it as pmd_trans_splitting too.
1794 */
1795 if (mapcount != page_mapcount(page))
1796 printk(KERN_ERR "mapcount %d page_mapcount %d\n",
1797 mapcount, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001798 BUG_ON(mapcount != page_mapcount(page));
1799
1800 __split_huge_page_refcount(page);
1801
1802 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001803 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001804 struct vm_area_struct *vma = avc->vma;
1805 unsigned long addr = vma_address(page, vma);
1806 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001807 mapcount2 += __split_huge_page_map(page, vma, addr);
1808 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001809 if (mapcount != mapcount2)
1810 printk(KERN_ERR "mapcount %d mapcount2 %d page_mapcount %d\n",
1811 mapcount, mapcount2, page_mapcount(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001812 BUG_ON(mapcount != mapcount2);
1813}
1814
1815int split_huge_page(struct page *page)
1816{
1817 struct anon_vma *anon_vma;
1818 int ret = 1;
1819
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08001820 BUG_ON(is_huge_zero_pfn(page_to_pfn(page)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001821 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001822
1823 /*
1824 * The caller does not necessarily hold an mmap_sem that would prevent
1825 * the anon_vma disappearing so we first we take a reference to it
1826 * and then lock the anon_vma for write. This is similar to
1827 * page_lock_anon_vma_read except the write lock is taken to serialise
1828 * against parallel split or collapse operations.
1829 */
1830 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001831 if (!anon_vma)
1832 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001833 anon_vma_lock_write(anon_vma);
1834
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001835 ret = 0;
1836 if (!PageCompound(page))
1837 goto out_unlock;
1838
1839 BUG_ON(!PageSwapBacked(page));
1840 __split_huge_page(page, anon_vma);
Andi Kleen81ab4202011-04-14 15:22:06 -07001841 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001842
1843 BUG_ON(PageCompound(page));
1844out_unlock:
Mel Gorman062f1af2013-01-11 14:32:02 -08001845 anon_vma_unlock(anon_vma);
1846 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001847out:
1848 return ret;
1849}
1850
Konstantin Khlebnikov4b6e1e32012-10-08 16:28:40 -07001851#define VM_NO_THP (VM_SPECIAL|VM_MIXEDMAP|VM_HUGETLB|VM_SHARED|VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001852
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001853int hugepage_madvise(struct vm_area_struct *vma,
1854 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001855{
Gerald Schaefer8e720332012-10-08 16:30:12 -07001856 struct mm_struct *mm = vma->vm_mm;
1857
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001858 switch (advice) {
1859 case MADV_HUGEPAGE:
1860 /*
1861 * Be somewhat over-protective like KSM for now!
1862 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001863 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001864 return -EINVAL;
Gerald Schaefer8e720332012-10-08 16:30:12 -07001865 if (mm->def_flags & VM_NOHUGEPAGE)
1866 return -EINVAL;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001867 *vm_flags &= ~VM_NOHUGEPAGE;
1868 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001869 /*
1870 * If the vma become good for khugepaged to scan,
1871 * register it here without waiting a page fault that
1872 * may not happen any time soon.
1873 */
1874 if (unlikely(khugepaged_enter_vma_merge(vma)))
1875 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001876 break;
1877 case MADV_NOHUGEPAGE:
1878 /*
1879 * Be somewhat over-protective like KSM for now!
1880 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001881 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001882 return -EINVAL;
1883 *vm_flags &= ~VM_HUGEPAGE;
1884 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001885 /*
1886 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1887 * this vma even if we leave the mm registered in khugepaged if
1888 * it got registered before VM_NOHUGEPAGE was set.
1889 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001890 break;
1891 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001892
1893 return 0;
1894}
1895
Andrea Arcangeliba761492011-01-13 15:46:58 -08001896static int __init khugepaged_slab_init(void)
1897{
1898 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1899 sizeof(struct mm_slot),
1900 __alignof__(struct mm_slot), 0, NULL);
1901 if (!mm_slot_cache)
1902 return -ENOMEM;
1903
1904 return 0;
1905}
1906
1907static void __init khugepaged_slab_free(void)
1908{
1909 kmem_cache_destroy(mm_slot_cache);
1910 mm_slot_cache = NULL;
1911}
1912
1913static inline struct mm_slot *alloc_mm_slot(void)
1914{
1915 if (!mm_slot_cache) /* initialization failed */
1916 return NULL;
1917 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1918}
1919
1920static inline void free_mm_slot(struct mm_slot *mm_slot)
1921{
1922 kmem_cache_free(mm_slot_cache, mm_slot);
1923}
1924
1925static int __init mm_slots_hash_init(void)
1926{
1927 mm_slots_hash = kzalloc(MM_SLOTS_HASH_HEADS * sizeof(struct hlist_head),
1928 GFP_KERNEL);
1929 if (!mm_slots_hash)
1930 return -ENOMEM;
1931 return 0;
1932}
1933
1934#if 0
1935static void __init mm_slots_hash_free(void)
1936{
1937 kfree(mm_slots_hash);
1938 mm_slots_hash = NULL;
1939}
1940#endif
1941
1942static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1943{
1944 struct mm_slot *mm_slot;
1945 struct hlist_head *bucket;
1946 struct hlist_node *node;
1947
1948 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1949 % MM_SLOTS_HASH_HEADS];
1950 hlist_for_each_entry(mm_slot, node, bucket, hash) {
1951 if (mm == mm_slot->mm)
1952 return mm_slot;
1953 }
1954 return NULL;
1955}
1956
1957static void insert_to_mm_slots_hash(struct mm_struct *mm,
1958 struct mm_slot *mm_slot)
1959{
1960 struct hlist_head *bucket;
1961
1962 bucket = &mm_slots_hash[((unsigned long)mm / sizeof(struct mm_struct))
1963 % MM_SLOTS_HASH_HEADS];
1964 mm_slot->mm = mm;
1965 hlist_add_head(&mm_slot->hash, bucket);
1966}
1967
1968static inline int khugepaged_test_exit(struct mm_struct *mm)
1969{
1970 return atomic_read(&mm->mm_users) == 0;
1971}
1972
1973int __khugepaged_enter(struct mm_struct *mm)
1974{
1975 struct mm_slot *mm_slot;
1976 int wakeup;
1977
1978 mm_slot = alloc_mm_slot();
1979 if (!mm_slot)
1980 return -ENOMEM;
1981
1982 /* __khugepaged_exit() must not run from under us */
1983 VM_BUG_ON(khugepaged_test_exit(mm));
1984 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1985 free_mm_slot(mm_slot);
1986 return 0;
1987 }
1988
1989 spin_lock(&khugepaged_mm_lock);
1990 insert_to_mm_slots_hash(mm, mm_slot);
1991 /*
1992 * Insert just behind the scanning cursor, to let the area settle
1993 * down a little.
1994 */
1995 wakeup = list_empty(&khugepaged_scan.mm_head);
1996 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1997 spin_unlock(&khugepaged_mm_lock);
1998
1999 atomic_inc(&mm->mm_count);
2000 if (wakeup)
2001 wake_up_interruptible(&khugepaged_wait);
2002
2003 return 0;
2004}
2005
2006int khugepaged_enter_vma_merge(struct vm_area_struct *vma)
2007{
2008 unsigned long hstart, hend;
2009 if (!vma->anon_vma)
2010 /*
2011 * Not yet faulted in so we will register later in the
2012 * page fault if needed.
2013 */
2014 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002015 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002016 /* khugepaged not yet working on file or special mappings */
2017 return 0;
Konstantin Khlebnikovb3b9c292012-10-08 16:28:34 -07002018 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002019 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2020 hend = vma->vm_end & HPAGE_PMD_MASK;
2021 if (hstart < hend)
2022 return khugepaged_enter(vma);
2023 return 0;
2024}
2025
2026void __khugepaged_exit(struct mm_struct *mm)
2027{
2028 struct mm_slot *mm_slot;
2029 int free = 0;
2030
2031 spin_lock(&khugepaged_mm_lock);
2032 mm_slot = get_mm_slot(mm);
2033 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
2034 hlist_del(&mm_slot->hash);
2035 list_del(&mm_slot->mm_node);
2036 free = 1;
2037 }
Chris Wrightd788e802011-07-25 17:12:14 -07002038 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002039
2040 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002041 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2042 free_mm_slot(mm_slot);
2043 mmdrop(mm);
2044 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002045 /*
2046 * This is required to serialize against
2047 * khugepaged_test_exit() (which is guaranteed to run
2048 * under mmap sem read mode). Stop here (after we
2049 * return all pagetables will be destroyed) until
2050 * khugepaged has finished working on the pagetables
2051 * under the mmap_sem.
2052 */
2053 down_write(&mm->mmap_sem);
2054 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002055 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002056}
2057
2058static void release_pte_page(struct page *page)
2059{
2060 /* 0 stands for page_is_file_cache(page) == false */
2061 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2062 unlock_page(page);
2063 putback_lru_page(page);
2064}
2065
2066static void release_pte_pages(pte_t *pte, pte_t *_pte)
2067{
2068 while (--_pte >= pte) {
2069 pte_t pteval = *_pte;
2070 if (!pte_none(pteval))
2071 release_pte_page(pte_page(pteval));
2072 }
2073}
2074
Andrea Arcangeliba761492011-01-13 15:46:58 -08002075static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2076 unsigned long address,
2077 pte_t *pte)
2078{
2079 struct page *page;
2080 pte_t *_pte;
Bob Liu344aa352012-12-11 16:00:34 -08002081 int referenced = 0, none = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002082 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2083 _pte++, address += PAGE_SIZE) {
2084 pte_t pteval = *_pte;
2085 if (pte_none(pteval)) {
2086 if (++none <= khugepaged_max_ptes_none)
2087 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002088 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002089 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002090 }
Bob Liu344aa352012-12-11 16:00:34 -08002091 if (!pte_present(pteval) || !pte_write(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002092 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002093 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002094 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002095 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002096
Andrea Arcangeliba761492011-01-13 15:46:58 -08002097 VM_BUG_ON(PageCompound(page));
2098 BUG_ON(!PageAnon(page));
2099 VM_BUG_ON(!PageSwapBacked(page));
2100
2101 /* cannot use mapcount: can't collapse if there's a gup pin */
Bob Liu344aa352012-12-11 16:00:34 -08002102 if (page_count(page) != 1)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002103 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002104 /*
2105 * We can do it before isolate_lru_page because the
2106 * page can't be freed from under us. NOTE: PG_lock
2107 * is needed to serialize against split_huge_page
2108 * when invoked from the VM.
2109 */
Bob Liu344aa352012-12-11 16:00:34 -08002110 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002111 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112 /*
2113 * Isolate the page to avoid collapsing an hugepage
2114 * currently in use by the VM.
2115 */
2116 if (isolate_lru_page(page)) {
2117 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002118 goto out;
2119 }
2120 /* 0 stands for page_is_file_cache(page) == false */
2121 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
2122 VM_BUG_ON(!PageLocked(page));
2123 VM_BUG_ON(PageLRU(page));
2124
2125 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002126 if (pte_young(pteval) || PageReferenced(page) ||
2127 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002128 referenced = 1;
2129 }
Bob Liu344aa352012-12-11 16:00:34 -08002130 if (likely(referenced))
2131 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002132out:
Bob Liu344aa352012-12-11 16:00:34 -08002133 release_pte_pages(pte, _pte);
2134 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002135}
2136
2137static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2138 struct vm_area_struct *vma,
2139 unsigned long address,
2140 spinlock_t *ptl)
2141{
2142 pte_t *_pte;
2143 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2144 pte_t pteval = *_pte;
2145 struct page *src_page;
2146
2147 if (pte_none(pteval)) {
2148 clear_user_highpage(page, address);
2149 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
2150 } else {
2151 src_page = pte_page(pteval);
2152 copy_user_highpage(page, src_page, address, vma);
2153 VM_BUG_ON(page_mapcount(src_page) != 1);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002154 release_pte_page(src_page);
2155 /*
2156 * ptl mostly unnecessary, but preempt has to
2157 * be disabled to update the per-cpu stats
2158 * inside page_remove_rmap().
2159 */
2160 spin_lock(ptl);
2161 /*
2162 * paravirt calls inside pte_clear here are
2163 * superfluous.
2164 */
2165 pte_clear(vma->vm_mm, address, _pte);
2166 page_remove_rmap(src_page);
2167 spin_unlock(ptl);
2168 free_page_and_swap_cache(src_page);
2169 }
2170
2171 address += PAGE_SIZE;
2172 page++;
2173 }
2174}
2175
Xiao Guangrong26234f32012-10-08 16:29:51 -07002176static void khugepaged_alloc_sleep(void)
2177{
2178 wait_event_freezable_timeout(khugepaged_wait, false,
2179 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2180}
2181
2182#ifdef CONFIG_NUMA
2183static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2184{
2185 if (IS_ERR(*hpage)) {
2186 if (!*wait)
2187 return false;
2188
2189 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002190 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002191 khugepaged_alloc_sleep();
2192 } else if (*hpage) {
2193 put_page(*hpage);
2194 *hpage = NULL;
2195 }
2196
2197 return true;
2198}
2199
2200static struct page
2201*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2202 struct vm_area_struct *vma, unsigned long address,
2203 int node)
2204{
2205 VM_BUG_ON(*hpage);
2206 /*
2207 * Allocate the page while the vma is still valid and under
2208 * the mmap_sem read mode so there is no memory allocation
2209 * later when we take the mmap_sem in write mode. This is more
2210 * friendly behavior (OTOH it may actually hide bugs) to
2211 * filesystems in userland with daemons allocating memory in
2212 * the userland I/O paths. Allocating memory with the
2213 * mmap_sem in read mode is good idea also to allow greater
2214 * scalability.
2215 */
2216 *hpage = alloc_hugepage_vma(khugepaged_defrag(), vma, address,
2217 node, __GFP_OTHER_NODE);
2218
2219 /*
2220 * After allocating the hugepage, release the mmap_sem read lock in
2221 * preparation for taking it in write mode.
2222 */
2223 up_read(&mm->mmap_sem);
2224 if (unlikely(!*hpage)) {
2225 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2226 *hpage = ERR_PTR(-ENOMEM);
2227 return NULL;
2228 }
2229
2230 count_vm_event(THP_COLLAPSE_ALLOC);
2231 return *hpage;
2232}
2233#else
2234static struct page *khugepaged_alloc_hugepage(bool *wait)
2235{
2236 struct page *hpage;
2237
2238 do {
2239 hpage = alloc_hugepage(khugepaged_defrag());
2240 if (!hpage) {
2241 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2242 if (!*wait)
2243 return NULL;
2244
2245 *wait = false;
2246 khugepaged_alloc_sleep();
2247 } else
2248 count_vm_event(THP_COLLAPSE_ALLOC);
2249 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2250
2251 return hpage;
2252}
2253
2254static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2255{
2256 if (!*hpage)
2257 *hpage = khugepaged_alloc_hugepage(wait);
2258
2259 if (unlikely(!*hpage))
2260 return false;
2261
2262 return true;
2263}
2264
2265static struct page
2266*khugepaged_alloc_page(struct page **hpage, struct mm_struct *mm,
2267 struct vm_area_struct *vma, unsigned long address,
2268 int node)
2269{
2270 up_read(&mm->mmap_sem);
2271 VM_BUG_ON(!*hpage);
2272 return *hpage;
2273}
2274#endif
2275
Bob Liufa475e52012-12-11 16:00:39 -08002276static bool hugepage_vma_check(struct vm_area_struct *vma)
2277{
2278 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2279 (vma->vm_flags & VM_NOHUGEPAGE))
2280 return false;
2281
2282 if (!vma->anon_vma || vma->vm_ops)
2283 return false;
2284 if (is_vma_temporary_stack(vma))
2285 return false;
2286 VM_BUG_ON(vma->vm_flags & VM_NO_THP);
2287 return true;
2288}
2289
Andrea Arcangeliba761492011-01-13 15:46:58 -08002290static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002291 unsigned long address,
2292 struct page **hpage,
2293 struct vm_area_struct *vma,
2294 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002295{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002296 pmd_t *pmd, _pmd;
2297 pte_t *pte;
2298 pgtable_t pgtable;
2299 struct page *new_page;
2300 spinlock_t *ptl;
2301 int isolated;
2302 unsigned long hstart, hend;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002303 unsigned long mmun_start; /* For mmu_notifiers */
2304 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002305
2306 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002307
Xiao Guangrong26234f32012-10-08 16:29:51 -07002308 /* release the mmap_sem read lock. */
2309 new_page = khugepaged_alloc_page(hpage, mm, vma, address, node);
2310 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002311 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002312
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002313 if (unlikely(mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002314 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002315
2316 /*
2317 * Prevent all access to pagetables with the exception of
2318 * gup_fast later hanlded by the ptep_clear_flush and the VM
2319 * handled by the anon_vma lock + PG_lock.
2320 */
2321 down_write(&mm->mmap_sem);
2322 if (unlikely(khugepaged_test_exit(mm)))
2323 goto out;
2324
2325 vma = find_vma(mm, address);
2326 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2327 hend = vma->vm_end & HPAGE_PMD_MASK;
2328 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2329 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002330 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002332 pmd = mm_find_pmd(mm, address);
2333 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002334 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002335 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002336 goto out;
2337
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002338 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002339
2340 pte = pte_offset_map(pmd, address);
2341 ptl = pte_lockptr(mm, pmd);
2342
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002343 mmun_start = address;
2344 mmun_end = address + HPAGE_PMD_SIZE;
2345 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002346 spin_lock(&mm->page_table_lock); /* probably unnecessary */
2347 /*
2348 * After this gup_fast can't run anymore. This also removes
2349 * any huge TLB entry from the CPU so we won't allow
2350 * huge and small TLB entries for the same virtual address
2351 * to avoid the risk of CPU bugs in that area.
2352 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002353 _pmd = pmdp_clear_flush(vma, address, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002354 spin_unlock(&mm->page_table_lock);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002355 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356
2357 spin_lock(ptl);
2358 isolated = __collapse_huge_page_isolate(vma, address, pte);
2359 spin_unlock(ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002360
2361 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002362 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002363 spin_lock(&mm->page_table_lock);
2364 BUG_ON(!pmd_none(*pmd));
2365 set_pmd_at(mm, address, pmd, _pmd);
2366 spin_unlock(&mm->page_table_lock);
2367 anon_vma_unlock(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002368 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002369 }
2370
2371 /*
2372 * All pages are isolated and locked so anon_vma rmap
2373 * can't run anymore.
2374 */
2375 anon_vma_unlock(vma->anon_vma);
2376
2377 __collapse_huge_page_copy(pte, new_page, vma, address, ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002378 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002379 __SetPageUptodate(new_page);
2380 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002381
Bob Liub3092b32012-12-11 16:00:41 -08002382 _pmd = mk_huge_pmd(new_page, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002383
2384 /*
2385 * spin_lock() below is not the equivalent of smp_wmb(), so
2386 * this is needed to avoid the copy_huge_page writes to become
2387 * visible after the set_pmd_at() write.
2388 */
2389 smp_wmb();
2390
2391 spin_lock(&mm->page_table_lock);
2392 BUG_ON(!pmd_none(*pmd));
2393 page_add_new_anon_rmap(new_page, vma, address);
2394 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002395 update_mmu_cache_pmd(vma, address, pmd);
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07002396 pgtable_trans_huge_deposit(mm, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002397 spin_unlock(&mm->page_table_lock);
2398
2399 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002400
Andrea Arcangeliba761492011-01-13 15:46:58 -08002401 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002402out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002403 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002404 return;
2405
Andrea Arcangelice83d212011-01-13 15:47:06 -08002406out:
KAMEZAWA Hiroyuki678ff892011-02-10 15:01:36 -08002407 mem_cgroup_uncharge_page(new_page);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002408 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002409}
2410
2411static int khugepaged_scan_pmd(struct mm_struct *mm,
2412 struct vm_area_struct *vma,
2413 unsigned long address,
2414 struct page **hpage)
2415{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416 pmd_t *pmd;
2417 pte_t *pte, *_pte;
2418 int ret = 0, referenced = 0, none = 0;
2419 struct page *page;
2420 unsigned long _address;
2421 spinlock_t *ptl;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002422 int node = -1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002423
2424 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2425
Bob Liu62190492012-12-11 16:00:37 -08002426 pmd = mm_find_pmd(mm, address);
2427 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002428 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002429 if (pmd_trans_huge(*pmd))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002430 goto out;
2431
2432 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2433 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2434 _pte++, _address += PAGE_SIZE) {
2435 pte_t pteval = *_pte;
2436 if (pte_none(pteval)) {
2437 if (++none <= khugepaged_max_ptes_none)
2438 continue;
2439 else
2440 goto out_unmap;
2441 }
2442 if (!pte_present(pteval) || !pte_write(pteval))
2443 goto out_unmap;
2444 page = vm_normal_page(vma, _address, pteval);
2445 if (unlikely(!page))
2446 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002447 /*
2448 * Chose the node of the first page. This could
2449 * be more sophisticated and look at more pages,
2450 * but isn't for now.
2451 */
2452 if (node == -1)
2453 node = page_to_nid(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002454 VM_BUG_ON(PageCompound(page));
2455 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2456 goto out_unmap;
2457 /* cannot use mapcount: can't collapse if there's a gup pin */
2458 if (page_count(page) != 1)
2459 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002460 if (pte_young(pteval) || PageReferenced(page) ||
2461 mmu_notifier_test_young(vma->vm_mm, address))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002462 referenced = 1;
2463 }
2464 if (referenced)
2465 ret = 1;
2466out_unmap:
2467 pte_unmap_unlock(pte, ptl);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002468 if (ret)
2469 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002470 collapse_huge_page(mm, address, hpage, vma, node);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002471out:
2472 return ret;
2473}
2474
2475static void collect_mm_slot(struct mm_slot *mm_slot)
2476{
2477 struct mm_struct *mm = mm_slot->mm;
2478
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002479 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002480
2481 if (khugepaged_test_exit(mm)) {
2482 /* free mm_slot */
2483 hlist_del(&mm_slot->hash);
2484 list_del(&mm_slot->mm_node);
2485
2486 /*
2487 * Not strictly needed because the mm exited already.
2488 *
2489 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2490 */
2491
2492 /* khugepaged_mm_lock actually not necessary for the below */
2493 free_mm_slot(mm_slot);
2494 mmdrop(mm);
2495 }
2496}
2497
2498static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2499 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002500 __releases(&khugepaged_mm_lock)
2501 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002502{
2503 struct mm_slot *mm_slot;
2504 struct mm_struct *mm;
2505 struct vm_area_struct *vma;
2506 int progress = 0;
2507
2508 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002509 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002510
2511 if (khugepaged_scan.mm_slot)
2512 mm_slot = khugepaged_scan.mm_slot;
2513 else {
2514 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2515 struct mm_slot, mm_node);
2516 khugepaged_scan.address = 0;
2517 khugepaged_scan.mm_slot = mm_slot;
2518 }
2519 spin_unlock(&khugepaged_mm_lock);
2520
2521 mm = mm_slot->mm;
2522 down_read(&mm->mmap_sem);
2523 if (unlikely(khugepaged_test_exit(mm)))
2524 vma = NULL;
2525 else
2526 vma = find_vma(mm, khugepaged_scan.address);
2527
2528 progress++;
2529 for (; vma; vma = vma->vm_next) {
2530 unsigned long hstart, hend;
2531
2532 cond_resched();
2533 if (unlikely(khugepaged_test_exit(mm))) {
2534 progress++;
2535 break;
2536 }
Bob Liufa475e52012-12-11 16:00:39 -08002537 if (!hugepage_vma_check(vma)) {
2538skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002539 progress++;
2540 continue;
2541 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002542 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2543 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002544 if (hstart >= hend)
2545 goto skip;
2546 if (khugepaged_scan.address > hend)
2547 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002548 if (khugepaged_scan.address < hstart)
2549 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002550 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002551
2552 while (khugepaged_scan.address < hend) {
2553 int ret;
2554 cond_resched();
2555 if (unlikely(khugepaged_test_exit(mm)))
2556 goto breakouterloop;
2557
2558 VM_BUG_ON(khugepaged_scan.address < hstart ||
2559 khugepaged_scan.address + HPAGE_PMD_SIZE >
2560 hend);
2561 ret = khugepaged_scan_pmd(mm, vma,
2562 khugepaged_scan.address,
2563 hpage);
2564 /* move to next address */
2565 khugepaged_scan.address += HPAGE_PMD_SIZE;
2566 progress += HPAGE_PMD_NR;
2567 if (ret)
2568 /* we released mmap_sem so break loop */
2569 goto breakouterloop_mmap_sem;
2570 if (progress >= pages)
2571 goto breakouterloop;
2572 }
2573 }
2574breakouterloop:
2575 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2576breakouterloop_mmap_sem:
2577
2578 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002579 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002580 /*
2581 * Release the current mm_slot if this mm is about to die, or
2582 * if we scanned all vmas of this mm.
2583 */
2584 if (khugepaged_test_exit(mm) || !vma) {
2585 /*
2586 * Make sure that if mm_users is reaching zero while
2587 * khugepaged runs here, khugepaged_exit will find
2588 * mm_slot not pointing to the exiting mm.
2589 */
2590 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2591 khugepaged_scan.mm_slot = list_entry(
2592 mm_slot->mm_node.next,
2593 struct mm_slot, mm_node);
2594 khugepaged_scan.address = 0;
2595 } else {
2596 khugepaged_scan.mm_slot = NULL;
2597 khugepaged_full_scans++;
2598 }
2599
2600 collect_mm_slot(mm_slot);
2601 }
2602
2603 return progress;
2604}
2605
2606static int khugepaged_has_work(void)
2607{
2608 return !list_empty(&khugepaged_scan.mm_head) &&
2609 khugepaged_enabled();
2610}
2611
2612static int khugepaged_wait_event(void)
2613{
2614 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002615 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002616}
2617
Xiao Guangrongd5169042012-10-08 16:29:48 -07002618static void khugepaged_do_scan(void)
2619{
2620 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002621 unsigned int progress = 0, pass_through_head = 0;
2622 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002623 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002624
2625 barrier(); /* write khugepaged_pages_to_scan to local stack */
2626
2627 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002628 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002629 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002630
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002631 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002632
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002633 if (unlikely(kthread_should_stop() || freezing(current)))
2634 break;
2635
Andrea Arcangeliba761492011-01-13 15:46:58 -08002636 spin_lock(&khugepaged_mm_lock);
2637 if (!khugepaged_scan.mm_slot)
2638 pass_through_head++;
2639 if (khugepaged_has_work() &&
2640 pass_through_head < 2)
2641 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002642 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002643 else
2644 progress = pages;
2645 spin_unlock(&khugepaged_mm_lock);
2646 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002647
Xiao Guangrongd5169042012-10-08 16:29:48 -07002648 if (!IS_ERR_OR_NULL(hpage))
2649 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002650}
2651
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002652static void khugepaged_wait_work(void)
2653{
2654 try_to_freeze();
2655
2656 if (khugepaged_has_work()) {
2657 if (!khugepaged_scan_sleep_millisecs)
2658 return;
2659
2660 wait_event_freezable_timeout(khugepaged_wait,
2661 kthread_should_stop(),
2662 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2663 return;
2664 }
2665
2666 if (khugepaged_enabled())
2667 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2668}
2669
Andrea Arcangeliba761492011-01-13 15:46:58 -08002670static int khugepaged(void *none)
2671{
2672 struct mm_slot *mm_slot;
2673
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002674 set_freezable();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002675 set_user_nice(current, 19);
2676
Xiao Guangrongb7231782012-10-08 16:29:54 -07002677 while (!kthread_should_stop()) {
2678 khugepaged_do_scan();
2679 khugepaged_wait_work();
2680 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002681
2682 spin_lock(&khugepaged_mm_lock);
2683 mm_slot = khugepaged_scan.mm_slot;
2684 khugepaged_scan.mm_slot = NULL;
2685 if (mm_slot)
2686 collect_mm_slot(mm_slot);
2687 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002688 return 0;
2689}
2690
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002691static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2692 unsigned long haddr, pmd_t *pmd)
2693{
2694 struct mm_struct *mm = vma->vm_mm;
2695 pgtable_t pgtable;
2696 pmd_t _pmd;
2697 int i;
2698
2699 pmdp_clear_flush(vma, haddr, pmd);
2700 /* leave pmd empty until pte is filled */
2701
2702 pgtable = pgtable_trans_huge_withdraw(mm);
2703 pmd_populate(mm, &_pmd, pgtable);
2704
2705 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2706 pte_t *pte, entry;
2707 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2708 entry = pte_mkspecial(entry);
2709 pte = pte_offset_map(&_pmd, haddr);
2710 VM_BUG_ON(!pte_none(*pte));
2711 set_pte_at(mm, haddr, pte, entry);
2712 pte_unmap(pte);
2713 }
2714 smp_wmb(); /* make pte visible before pmd */
2715 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002716 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002717}
2718
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002719void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2720 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002721{
2722 struct page *page;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002723 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002724 unsigned long haddr = address & HPAGE_PMD_MASK;
2725 unsigned long mmun_start; /* For mmu_notifiers */
2726 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002727
2728 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002729
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002730 mmun_start = haddr;
2731 mmun_end = haddr + HPAGE_PMD_SIZE;
2732 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002733 spin_lock(&mm->page_table_lock);
2734 if (unlikely(!pmd_trans_huge(*pmd))) {
2735 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002736 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
2737 return;
2738 }
2739 if (is_huge_zero_pmd(*pmd)) {
2740 __split_huge_zero_page_pmd(vma, haddr, pmd);
2741 spin_unlock(&mm->page_table_lock);
2742 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002743 return;
2744 }
2745 page = pmd_page(*pmd);
2746 VM_BUG_ON(!page_count(page));
2747 get_page(page);
2748 spin_unlock(&mm->page_table_lock);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002749 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002750
2751 split_huge_page(page);
2752
2753 put_page(page);
2754 BUG_ON(pmd_trans_huge(*pmd));
2755}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002756
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002757void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2758 pmd_t *pmd)
2759{
2760 struct vm_area_struct *vma;
2761
2762 vma = find_vma(mm, address);
2763 BUG_ON(vma == NULL);
2764 split_huge_page_pmd(vma, address, pmd);
2765}
2766
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002767static void split_huge_page_address(struct mm_struct *mm,
2768 unsigned long address)
2769{
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002770 pmd_t *pmd;
2771
2772 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2773
Bob Liu62190492012-12-11 16:00:37 -08002774 pmd = mm_find_pmd(mm, address);
2775 if (!pmd)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002776 return;
2777 /*
2778 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2779 * materialize from under us.
2780 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002781 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002782}
2783
2784void __vma_adjust_trans_huge(struct vm_area_struct *vma,
2785 unsigned long start,
2786 unsigned long end,
2787 long adjust_next)
2788{
2789 /*
2790 * If the new start address isn't hpage aligned and it could
2791 * previously contain an hugepage: check if we need to split
2792 * an huge pmd.
2793 */
2794 if (start & ~HPAGE_PMD_MASK &&
2795 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2796 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2797 split_huge_page_address(vma->vm_mm, start);
2798
2799 /*
2800 * If the new end address isn't hpage aligned and it could
2801 * previously contain an hugepage: check if we need to split
2802 * an huge pmd.
2803 */
2804 if (end & ~HPAGE_PMD_MASK &&
2805 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2806 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
2807 split_huge_page_address(vma->vm_mm, end);
2808
2809 /*
2810 * If we're also updating the vma->vm_next->vm_start, if the new
2811 * vm_next->vm_start isn't page aligned and it could previously
2812 * contain an hugepage: check if we need to split an huge pmd.
2813 */
2814 if (adjust_next > 0) {
2815 struct vm_area_struct *next = vma->vm_next;
2816 unsigned long nstart = next->vm_start;
2817 nstart += adjust_next << PAGE_SHIFT;
2818 if (nstart & ~HPAGE_PMD_MASK &&
2819 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2820 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
2821 split_huge_page_address(next->vm_mm, nstart);
2822 }
2823}