blob: c426a89e025c726259604b16c99a74ce080a638d [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
Andrew Mortonae3a8c12014-06-04 16:06:58 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080010#include <linux/mm.h>
11#include <linux/sched.h>
12#include <linux/highmem.h>
13#include <linux/hugetlb.h>
14#include <linux/mmu_notifier.h>
15#include <linux/rmap.h>
16#include <linux/swap.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080017#include <linux/shrinker.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080018#include <linux/mm_inline.h>
Matthew Wilcox4897c762015-09-08 14:58:45 -070019#include <linux/dax.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080020#include <linux/kthread.h>
21#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080022#include <linux/freezer.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080023#include <linux/mman.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020024#include <linux/pagemap.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000025#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080026#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070027#include <linux/userfaultfd_k.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080028
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080029#include <asm/tlb.h>
30#include <asm/pgalloc.h>
31#include "internal.h"
32
Andrea Arcangeliba761492011-01-13 15:46:58 -080033/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080034 * By default transparent hugepage support is disabled in order that avoid
35 * to risk increase the memory footprint of applications without a guaranteed
36 * benefit. When transparent hugepage support is enabled, is for all mappings,
37 * and khugepaged scans all mappings.
38 * Defrag is invoked by khugepaged hugepage allocations and by page faults
39 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080040 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080041unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080042#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080043 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080044#endif
45#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
46 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
47#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080048 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080049 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
50 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080051
52/* default scan 8*512 pte (or vmas) every 30 second */
53static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
54static unsigned int khugepaged_pages_collapsed;
55static unsigned int khugepaged_full_scans;
56static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
57/* during fragmentation poll the hugepage allocator once every minute */
58static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
59static struct task_struct *khugepaged_thread __read_mostly;
60static DEFINE_MUTEX(khugepaged_mutex);
61static DEFINE_SPINLOCK(khugepaged_mm_lock);
62static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
63/*
64 * default collapse hugepages if there is at least one pte mapped like
65 * it would have happened if the vma was large enough during page
66 * fault.
67 */
68static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
69
70static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -080071static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -070072static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -080073
Sasha Levin43b5fbb2013-02-22 16:32:27 -080074#define MM_SLOTS_HASH_BITS 10
75static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
76
Andrea Arcangeliba761492011-01-13 15:46:58 -080077static struct kmem_cache *mm_slot_cache __read_mostly;
78
79/**
80 * struct mm_slot - hash lookup from mm to mm_slot
81 * @hash: hash collision list
82 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
83 * @mm: the mm that this information is valid for
84 */
85struct mm_slot {
86 struct hlist_node hash;
87 struct list_head mm_node;
88 struct mm_struct *mm;
89};
90
91/**
92 * struct khugepaged_scan - cursor for scanning
93 * @mm_head: the head of the mm list to scan
94 * @mm_slot: the current mm_slot we are scanning
95 * @address: the next address inside that to be scanned
96 *
97 * There is only the one khugepaged_scan instance of this cursor structure.
98 */
99struct khugepaged_scan {
100 struct list_head mm_head;
101 struct mm_slot *mm_slot;
102 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700103};
104static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800105 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
106};
107
Andrea Arcangelif0005652011-01-13 15:47:04 -0800108
109static int set_recommended_min_free_kbytes(void)
110{
111 struct zone *zone;
112 int nr_zones = 0;
113 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800114
Andrea Arcangelif0005652011-01-13 15:47:04 -0800115 for_each_populated_zone(zone)
116 nr_zones++;
117
118 /* Make sure at least 2 hugepages are free for MIGRATE_RESERVE */
119 recommended_min = pageblock_nr_pages * nr_zones * 2;
120
121 /*
122 * Make sure that on average at least two pageblocks are almost free
123 * of another type, one for a migratetype to fall back to and a
124 * second to avoid subsequent fallbacks of other types There are 3
125 * MIGRATE_TYPES we care about.
126 */
127 recommended_min += pageblock_nr_pages * nr_zones *
128 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
129
130 /* don't ever allow to reserve more than 5% of the lowmem */
131 recommended_min = min(recommended_min,
132 (unsigned long) nr_free_buffer_pages() / 20);
133 recommended_min <<= (PAGE_SHIFT-10);
134
Han Pingtian42aa83c2014-01-23 15:53:28 -0800135 if (recommended_min > min_free_kbytes) {
136 if (user_min_free_kbytes >= 0)
137 pr_info("raising min_free_kbytes from %d to %lu "
138 "to help transparent hugepage allocations\n",
139 min_free_kbytes, recommended_min);
140
Andrea Arcangelif0005652011-01-13 15:47:04 -0800141 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800142 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800143 setup_per_zone_wmarks();
144 return 0;
145}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800146
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700147static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800148{
149 int err = 0;
150 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800151 if (!khugepaged_thread)
152 khugepaged_thread = kthread_run(khugepaged, NULL,
153 "khugepaged");
154 if (unlikely(IS_ERR(khugepaged_thread))) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700155 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800156 err = PTR_ERR(khugepaged_thread);
157 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700158 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800159 }
Xiao Guangrong911891af2012-10-08 16:29:41 -0700160
161 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800162 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800163
164 set_recommended_min_free_kbytes();
Xiao Guangrong911891af2012-10-08 16:29:41 -0700165 } else if (khugepaged_thread) {
Xiao Guangrong911891af2012-10-08 16:29:41 -0700166 kthread_stop(khugepaged_thread);
167 khugepaged_thread = NULL;
168 }
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700169fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800170 return err;
171}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800172
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800173static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800174struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800175
Matthew Wilcoxfc4370442015-09-08 14:58:51 -0700176struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800177{
178 struct page *zero_page;
179retry:
180 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700181 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800182
183 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
184 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800185 if (!zero_page) {
186 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700187 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800188 }
189 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800190 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700191 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800192 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700193 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800194 goto retry;
195 }
196
197 /* We take additional reference here. It will be put back by shrinker */
198 atomic_set(&huge_zero_refcount, 2);
199 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700200 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800201}
202
203static void put_huge_zero_page(void)
204{
205 /*
206 * Counter should never go to zero here. Only shrinker can put
207 * last reference.
208 */
209 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
210}
211
Glauber Costa48896462013-08-28 10:18:15 +1000212static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
213 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800214{
Glauber Costa48896462013-08-28 10:18:15 +1000215 /* we can free zero page only if last reference remains */
216 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
217}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800218
Glauber Costa48896462013-08-28 10:18:15 +1000219static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
220 struct shrink_control *sc)
221{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800222 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700223 struct page *zero_page = xchg(&huge_zero_page, NULL);
224 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700225 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000226 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227 }
228
229 return 0;
230}
231
232static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000233 .count_objects = shrink_huge_zero_page_count,
234 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800235 .seeks = DEFAULT_SEEKS,
236};
237
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800238#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800239
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800240static ssize_t double_flag_show(struct kobject *kobj,
241 struct kobj_attribute *attr, char *buf,
242 enum transparent_hugepage_flag enabled,
243 enum transparent_hugepage_flag req_madv)
244{
245 if (test_bit(enabled, &transparent_hugepage_flags)) {
246 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
247 return sprintf(buf, "[always] madvise never\n");
248 } else if (test_bit(req_madv, &transparent_hugepage_flags))
249 return sprintf(buf, "always [madvise] never\n");
250 else
251 return sprintf(buf, "always madvise [never]\n");
252}
253static ssize_t double_flag_store(struct kobject *kobj,
254 struct kobj_attribute *attr,
255 const char *buf, size_t count,
256 enum transparent_hugepage_flag enabled,
257 enum transparent_hugepage_flag req_madv)
258{
259 if (!memcmp("always", buf,
260 min(sizeof("always")-1, count))) {
261 set_bit(enabled, &transparent_hugepage_flags);
262 clear_bit(req_madv, &transparent_hugepage_flags);
263 } else if (!memcmp("madvise", buf,
264 min(sizeof("madvise")-1, count))) {
265 clear_bit(enabled, &transparent_hugepage_flags);
266 set_bit(req_madv, &transparent_hugepage_flags);
267 } else if (!memcmp("never", buf,
268 min(sizeof("never")-1, count))) {
269 clear_bit(enabled, &transparent_hugepage_flags);
270 clear_bit(req_madv, &transparent_hugepage_flags);
271 } else
272 return -EINVAL;
273
274 return count;
275}
276
277static ssize_t enabled_show(struct kobject *kobj,
278 struct kobj_attribute *attr, char *buf)
279{
280 return double_flag_show(kobj, attr, buf,
281 TRANSPARENT_HUGEPAGE_FLAG,
282 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
283}
284static ssize_t enabled_store(struct kobject *kobj,
285 struct kobj_attribute *attr,
286 const char *buf, size_t count)
287{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800288 ssize_t ret;
289
290 ret = double_flag_store(kobj, attr, buf, count,
291 TRANSPARENT_HUGEPAGE_FLAG,
292 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
293
294 if (ret > 0) {
Xiao Guangrong911891af2012-10-08 16:29:41 -0700295 int err;
296
297 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700298 err = start_stop_khugepaged();
Xiao Guangrong911891af2012-10-08 16:29:41 -0700299 mutex_unlock(&khugepaged_mutex);
300
Andrea Arcangeliba761492011-01-13 15:46:58 -0800301 if (err)
302 ret = err;
303 }
304
305 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800306}
307static struct kobj_attribute enabled_attr =
308 __ATTR(enabled, 0644, enabled_show, enabled_store);
309
310static ssize_t single_flag_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *buf,
312 enum transparent_hugepage_flag flag)
313{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700314 return sprintf(buf, "%d\n",
315 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800316}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700317
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800318static ssize_t single_flag_store(struct kobject *kobj,
319 struct kobj_attribute *attr,
320 const char *buf, size_t count,
321 enum transparent_hugepage_flag flag)
322{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700323 unsigned long value;
324 int ret;
325
326 ret = kstrtoul(buf, 10, &value);
327 if (ret < 0)
328 return ret;
329 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800330 return -EINVAL;
331
Ben Hutchingse27e6152011-04-14 15:22:21 -0700332 if (value)
333 set_bit(flag, &transparent_hugepage_flags);
334 else
335 clear_bit(flag, &transparent_hugepage_flags);
336
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800337 return count;
338}
339
340/*
341 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
342 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
343 * memory just to allocate one more hugepage.
344 */
345static ssize_t defrag_show(struct kobject *kobj,
346 struct kobj_attribute *attr, char *buf)
347{
348 return double_flag_show(kobj, attr, buf,
349 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
350 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
351}
352static ssize_t defrag_store(struct kobject *kobj,
353 struct kobj_attribute *attr,
354 const char *buf, size_t count)
355{
356 return double_flag_store(kobj, attr, buf, count,
357 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
358 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
359}
360static struct kobj_attribute defrag_attr =
361 __ATTR(defrag, 0644, defrag_show, defrag_store);
362
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800363static ssize_t use_zero_page_show(struct kobject *kobj,
364 struct kobj_attribute *attr, char *buf)
365{
366 return single_flag_show(kobj, attr, buf,
367 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
368}
369static ssize_t use_zero_page_store(struct kobject *kobj,
370 struct kobj_attribute *attr, const char *buf, size_t count)
371{
372 return single_flag_store(kobj, attr, buf, count,
373 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
374}
375static struct kobj_attribute use_zero_page_attr =
376 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800377#ifdef CONFIG_DEBUG_VM
378static ssize_t debug_cow_show(struct kobject *kobj,
379 struct kobj_attribute *attr, char *buf)
380{
381 return single_flag_show(kobj, attr, buf,
382 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
383}
384static ssize_t debug_cow_store(struct kobject *kobj,
385 struct kobj_attribute *attr,
386 const char *buf, size_t count)
387{
388 return single_flag_store(kobj, attr, buf, count,
389 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
390}
391static struct kobj_attribute debug_cow_attr =
392 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
393#endif /* CONFIG_DEBUG_VM */
394
395static struct attribute *hugepage_attr[] = {
396 &enabled_attr.attr,
397 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800398 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800399#ifdef CONFIG_DEBUG_VM
400 &debug_cow_attr.attr,
401#endif
402 NULL,
403};
404
405static struct attribute_group hugepage_attr_group = {
406 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800407};
408
409static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
410 struct kobj_attribute *attr,
411 char *buf)
412{
413 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
414}
415
416static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
417 struct kobj_attribute *attr,
418 const char *buf, size_t count)
419{
420 unsigned long msecs;
421 int err;
422
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700423 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800424 if (err || msecs > UINT_MAX)
425 return -EINVAL;
426
427 khugepaged_scan_sleep_millisecs = msecs;
428 wake_up_interruptible(&khugepaged_wait);
429
430 return count;
431}
432static struct kobj_attribute scan_sleep_millisecs_attr =
433 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
434 scan_sleep_millisecs_store);
435
436static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
437 struct kobj_attribute *attr,
438 char *buf)
439{
440 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
441}
442
443static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
444 struct kobj_attribute *attr,
445 const char *buf, size_t count)
446{
447 unsigned long msecs;
448 int err;
449
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700450 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800451 if (err || msecs > UINT_MAX)
452 return -EINVAL;
453
454 khugepaged_alloc_sleep_millisecs = msecs;
455 wake_up_interruptible(&khugepaged_wait);
456
457 return count;
458}
459static struct kobj_attribute alloc_sleep_millisecs_attr =
460 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
461 alloc_sleep_millisecs_store);
462
463static ssize_t pages_to_scan_show(struct kobject *kobj,
464 struct kobj_attribute *attr,
465 char *buf)
466{
467 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
468}
469static ssize_t pages_to_scan_store(struct kobject *kobj,
470 struct kobj_attribute *attr,
471 const char *buf, size_t count)
472{
473 int err;
474 unsigned long pages;
475
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700476 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800477 if (err || !pages || pages > UINT_MAX)
478 return -EINVAL;
479
480 khugepaged_pages_to_scan = pages;
481
482 return count;
483}
484static struct kobj_attribute pages_to_scan_attr =
485 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
486 pages_to_scan_store);
487
488static ssize_t pages_collapsed_show(struct kobject *kobj,
489 struct kobj_attribute *attr,
490 char *buf)
491{
492 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
493}
494static struct kobj_attribute pages_collapsed_attr =
495 __ATTR_RO(pages_collapsed);
496
497static ssize_t full_scans_show(struct kobject *kobj,
498 struct kobj_attribute *attr,
499 char *buf)
500{
501 return sprintf(buf, "%u\n", khugepaged_full_scans);
502}
503static struct kobj_attribute full_scans_attr =
504 __ATTR_RO(full_scans);
505
506static ssize_t khugepaged_defrag_show(struct kobject *kobj,
507 struct kobj_attribute *attr, char *buf)
508{
509 return single_flag_show(kobj, attr, buf,
510 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
511}
512static ssize_t khugepaged_defrag_store(struct kobject *kobj,
513 struct kobj_attribute *attr,
514 const char *buf, size_t count)
515{
516 return single_flag_store(kobj, attr, buf, count,
517 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
518}
519static struct kobj_attribute khugepaged_defrag_attr =
520 __ATTR(defrag, 0644, khugepaged_defrag_show,
521 khugepaged_defrag_store);
522
523/*
524 * max_ptes_none controls if khugepaged should collapse hugepages over
525 * any unmapped ptes in turn potentially increasing the memory
526 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
527 * reduce the available free memory in the system as it
528 * runs. Increasing max_ptes_none will instead potentially reduce the
529 * free memory in the system during the khugepaged scan.
530 */
531static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
532 struct kobj_attribute *attr,
533 char *buf)
534{
535 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
536}
537static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
538 struct kobj_attribute *attr,
539 const char *buf, size_t count)
540{
541 int err;
542 unsigned long max_ptes_none;
543
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700544 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800545 if (err || max_ptes_none > HPAGE_PMD_NR-1)
546 return -EINVAL;
547
548 khugepaged_max_ptes_none = max_ptes_none;
549
550 return count;
551}
552static struct kobj_attribute khugepaged_max_ptes_none_attr =
553 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
554 khugepaged_max_ptes_none_store);
555
556static struct attribute *khugepaged_attr[] = {
557 &khugepaged_defrag_attr.attr,
558 &khugepaged_max_ptes_none_attr.attr,
559 &pages_to_scan_attr.attr,
560 &pages_collapsed_attr.attr,
561 &full_scans_attr.attr,
562 &scan_sleep_millisecs_attr.attr,
563 &alloc_sleep_millisecs_attr.attr,
564 NULL,
565};
566
567static struct attribute_group khugepaged_attr_group = {
568 .attrs = khugepaged_attr,
569 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800570};
Shaohua Li569e5592012-01-12 17:19:11 -0800571
572static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
573{
574 int err;
575
576 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
577 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700578 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800579 return -ENOMEM;
580 }
581
582 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
583 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700584 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800585 goto delete_obj;
586 }
587
588 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
589 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700590 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800591 goto remove_hp_group;
592 }
593
594 return 0;
595
596remove_hp_group:
597 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
598delete_obj:
599 kobject_put(*hugepage_kobj);
600 return err;
601}
602
603static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
604{
605 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
606 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
607 kobject_put(hugepage_kobj);
608}
609#else
610static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
611{
612 return 0;
613}
614
615static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
616{
617}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800618#endif /* CONFIG_SYSFS */
619
620static int __init hugepage_init(void)
621{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800622 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800623 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800624
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800625 if (!has_transparent_hugepage()) {
626 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800627 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800628 }
629
Shaohua Li569e5592012-01-12 17:19:11 -0800630 err = hugepage_init_sysfs(&hugepage_kobj);
631 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700632 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800633
634 err = khugepaged_slab_init();
635 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700636 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800637
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700638 err = register_shrinker(&huge_zero_page_shrinker);
639 if (err)
640 goto err_hzp_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800641
Rik van Riel97562cd2011-01-13 15:47:12 -0800642 /*
643 * By default disable transparent hugepages on smaller systems,
644 * where the extra memory used could hurt more than TLB overhead
645 * is likely to save. The admin can still enable it through /sys.
646 */
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700647 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800648 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700649 return 0;
650 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800651
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700652 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700653 if (err)
654 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800655
Shaohua Li569e5592012-01-12 17:19:11 -0800656 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700657err_khugepaged:
658 unregister_shrinker(&huge_zero_page_shrinker);
659err_hzp_shrinker:
660 khugepaged_slab_exit();
661err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800662 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700663err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800664 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800665}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800666subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800667
668static int __init setup_transparent_hugepage(char *str)
669{
670 int ret = 0;
671 if (!str)
672 goto out;
673 if (!strcmp(str, "always")) {
674 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
675 &transparent_hugepage_flags);
676 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
677 &transparent_hugepage_flags);
678 ret = 1;
679 } else if (!strcmp(str, "madvise")) {
680 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
681 &transparent_hugepage_flags);
682 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
683 &transparent_hugepage_flags);
684 ret = 1;
685 } else if (!strcmp(str, "never")) {
686 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
687 &transparent_hugepage_flags);
688 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
689 &transparent_hugepage_flags);
690 ret = 1;
691 }
692out:
693 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700694 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800695 return ret;
696}
697__setup("transparent_hugepage=", setup_transparent_hugepage);
698
Mel Gormanb32967f2012-11-19 12:35:47 +0000699pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800700{
701 if (likely(vma->vm_flags & VM_WRITE))
702 pmd = pmd_mkwrite(pmd);
703 return pmd;
704}
705
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700706static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800707{
708 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700709 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800710 entry = pmd_mkhuge(entry);
711 return entry;
712}
713
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800714static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
715 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700716 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700717 struct page *page, gfp_t gfp,
718 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800719{
Johannes Weiner00501b52014-08-08 14:19:20 -0700720 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800721 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800722 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700723 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800724
Sasha Levin309381fea2014-01-23 15:52:54 -0800725 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700726
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700727 if (mem_cgroup_try_charge(page, mm, gfp, &memcg)) {
728 put_page(page);
729 count_vm_event(THP_FAULT_FALLBACK);
730 return VM_FAULT_FALLBACK;
731 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800732
Johannes Weiner00501b52014-08-08 14:19:20 -0700733 pgtable = pte_alloc_one(mm, haddr);
734 if (unlikely(!pgtable)) {
735 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700736 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700737 return VM_FAULT_OOM;
738 }
739
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800740 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700741 /*
742 * The memory barrier inside __SetPageUptodate makes sure that
743 * clear_huge_page writes become visible before the set_pmd_at()
744 * write.
745 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800746 __SetPageUptodate(page);
747
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800748 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800749 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800750 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -0700751 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800752 put_page(page);
753 pte_free(mm, pgtable);
754 } else {
755 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700756
757 /* Deliver the page fault to userland */
758 if (userfaultfd_missing(vma)) {
759 int ret;
760
761 spin_unlock(ptl);
762 mem_cgroup_cancel_charge(page, memcg);
763 put_page(page);
764 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700765 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700766 VM_UFFD_MISSING);
767 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
768 return ret;
769 }
770
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700771 entry = mk_huge_pmd(page, vma->vm_page_prot);
772 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800773 page_add_new_anon_rmap(page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -0700774 mem_cgroup_commit_charge(page, memcg, false);
775 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700776 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800777 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800778 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800779 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800780 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700781 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800782 }
783
David Rientjesaa2e8782012-05-29 15:06:17 -0700784 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800785}
786
Andi Kleencc5d4622011-03-22 16:33:13 -0700787static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800788{
Andi Kleencc5d4622011-03-22 16:33:13 -0700789 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800790}
791
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800792/* Caller must hold page table lock. */
Matthew Wilcox4897c762015-09-08 14:58:45 -0700793bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800794 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700795 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800796{
797 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700798 if (!pmd_none(*pmd))
799 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700800 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800801 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700802 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800803 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800804 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700805 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800806}
807
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800808int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
809 unsigned long address, pmd_t *pmd,
810 unsigned int flags)
811{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800812 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800813 struct page *page;
814 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800815
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700816 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700817 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700818 if (unlikely(anon_vma_prepare(vma)))
819 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700820 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700821 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200822 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700823 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800824 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700825 pgtable_t pgtable;
826 struct page *zero_page;
827 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700828 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700829 pgtable = pte_alloc_one(mm, haddr);
830 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800831 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700832 zero_page = get_huge_zero_page();
833 if (unlikely(!zero_page)) {
834 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700835 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700836 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700837 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800838 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700839 ret = 0;
840 set = false;
841 if (pmd_none(*pmd)) {
842 if (userfaultfd_missing(vma)) {
843 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700844 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700845 VM_UFFD_MISSING);
846 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
847 } else {
848 set_huge_zero_page(pgtable, mm, vma,
849 haddr, pmd,
850 zero_page);
851 spin_unlock(ptl);
852 set = true;
853 }
854 } else
855 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700856 if (!set) {
857 pte_free(mm, pgtable);
858 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800859 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700860 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800861 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800862 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
863 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700864 if (unlikely(!page)) {
865 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700866 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700867 }
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700868 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
869 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800870}
871
872int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
873 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
874 struct vm_area_struct *vma)
875{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800876 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800877 struct page *src_page;
878 pmd_t pmd;
879 pgtable_t pgtable;
880 int ret;
881
882 ret = -ENOMEM;
883 pgtable = pte_alloc_one(dst_mm, addr);
884 if (unlikely(!pgtable))
885 goto out;
886
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800887 dst_ptl = pmd_lock(dst_mm, dst_pmd);
888 src_ptl = pmd_lockptr(src_mm, src_pmd);
889 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800890
891 ret = -EAGAIN;
892 pmd = *src_pmd;
893 if (unlikely(!pmd_trans_huge(pmd))) {
894 pte_free(dst_mm, pgtable);
895 goto out_unlock;
896 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800897 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800898 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800899 * under splitting since we don't split the page itself, only pmd to
900 * a page table.
901 */
902 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700903 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800904 /*
905 * get_huge_zero_page() will never allocate a new page here,
906 * since we already have a zero page to copy. It just takes a
907 * reference.
908 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700909 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700910 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700911 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800912 ret = 0;
913 goto out_unlock;
914 }
Mel Gormande466bd2013-12-18 17:08:42 -0800915
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800916 if (unlikely(pmd_trans_splitting(pmd))) {
917 /* split huge page running from under us */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800918 spin_unlock(src_ptl);
919 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800920 pte_free(dst_mm, pgtable);
921
922 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
923 goto out;
924 }
925 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -0800926 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800927 get_page(src_page);
928 page_dup_rmap(src_page);
929 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
930
931 pmdp_set_wrprotect(src_mm, addr, src_pmd);
932 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700933 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800934 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800935 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800936
937 ret = 0;
938out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800939 spin_unlock(src_ptl);
940 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800941out:
942 return ret;
943}
944
Will Deacona1dd4502012-12-11 16:01:27 -0800945void huge_pmd_set_accessed(struct mm_struct *mm,
946 struct vm_area_struct *vma,
947 unsigned long address,
948 pmd_t *pmd, pmd_t orig_pmd,
949 int dirty)
950{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800951 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -0800952 pmd_t entry;
953 unsigned long haddr;
954
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800955 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -0800956 if (unlikely(!pmd_same(*pmd, orig_pmd)))
957 goto unlock;
958
959 entry = pmd_mkyoung(orig_pmd);
960 haddr = address & HPAGE_PMD_MASK;
961 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
962 update_mmu_cache_pmd(vma, address, pmd);
963
964unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800965 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -0800966}
967
Hugh Dickins5338a932014-06-23 13:22:05 -0700968/*
969 * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
970 * during copy_user_huge_page()'s copy_page_rep(): in the case when
971 * the source page gets split and a tail freed before copy completes.
972 * Called under pmd_lock of checked pmd, so safe from splitting itself.
973 */
974static void get_user_huge_page(struct page *page)
975{
976 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
977 struct page *endpage = page + HPAGE_PMD_NR;
978
979 atomic_add(HPAGE_PMD_NR, &page->_count);
980 while (++page < endpage)
981 get_huge_page_tail(page);
982 } else {
983 get_page(page);
984 }
985}
986
987static void put_user_huge_page(struct page *page)
988{
989 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
990 struct page *endpage = page + HPAGE_PMD_NR;
991
992 while (page < endpage)
993 put_page(page++);
994 } else {
995 put_page(page);
996 }
997}
998
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800999static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1000 struct vm_area_struct *vma,
1001 unsigned long address,
1002 pmd_t *pmd, pmd_t orig_pmd,
1003 struct page *page,
1004 unsigned long haddr)
1005{
Johannes Weiner00501b52014-08-08 14:19:20 -07001006 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001007 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001008 pgtable_t pgtable;
1009 pmd_t _pmd;
1010 int ret = 0, i;
1011 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001012 unsigned long mmun_start; /* For mmu_notifiers */
1013 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001014
1015 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1016 GFP_KERNEL);
1017 if (unlikely(!pages)) {
1018 ret |= VM_FAULT_OOM;
1019 goto out;
1020 }
1021
1022 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001023 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1024 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001025 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001026 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001027 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
1028 &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001029 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001030 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001031 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001032 memcg = (void *)page_private(pages[i]);
1033 set_page_private(pages[i], 0);
1034 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001035 put_page(pages[i]);
1036 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001037 kfree(pages);
1038 ret |= VM_FAULT_OOM;
1039 goto out;
1040 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001041 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001042 }
1043
1044 for (i = 0; i < HPAGE_PMD_NR; i++) {
1045 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001046 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001047 __SetPageUptodate(pages[i]);
1048 cond_resched();
1049 }
1050
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001051 mmun_start = haddr;
1052 mmun_end = haddr + HPAGE_PMD_SIZE;
1053 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1054
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001055 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001056 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1057 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001058 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001059
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001060 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001061 /* leave pmd empty until pte is filled */
1062
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001063 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001064 pmd_populate(mm, &_pmd, pgtable);
1065
1066 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1067 pte_t *pte, entry;
1068 entry = mk_pte(pages[i], vma->vm_page_prot);
1069 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001070 memcg = (void *)page_private(pages[i]);
1071 set_page_private(pages[i], 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001072 page_add_new_anon_rmap(pages[i], vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001073 mem_cgroup_commit_charge(pages[i], memcg, false);
1074 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001075 pte = pte_offset_map(&_pmd, haddr);
1076 VM_BUG_ON(!pte_none(*pte));
1077 set_pte_at(mm, haddr, pte, entry);
1078 pte_unmap(pte);
1079 }
1080 kfree(pages);
1081
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001082 smp_wmb(); /* make pte visible before pmd */
1083 pmd_populate(mm, pmd, pgtable);
1084 page_remove_rmap(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001085 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001086
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001087 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1088
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001089 ret |= VM_FAULT_WRITE;
1090 put_page(page);
1091
1092out:
1093 return ret;
1094
1095out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001096 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001097 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001098 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001099 memcg = (void *)page_private(pages[i]);
1100 set_page_private(pages[i], 0);
1101 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001103 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001104 kfree(pages);
1105 goto out;
1106}
1107
1108int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1109 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1110{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001111 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001112 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001113 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001114 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001115 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001116 unsigned long mmun_start; /* For mmu_notifiers */
1117 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07001118 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001119
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001120 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001121 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001122 haddr = address & HPAGE_PMD_MASK;
1123 if (is_huge_zero_pmd(orig_pmd))
1124 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001125 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001126 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1127 goto out_unlock;
1128
1129 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001130 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001131 if (page_mapcount(page) == 1) {
1132 pmd_t entry;
1133 entry = pmd_mkyoung(orig_pmd);
1134 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1135 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001136 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001137 ret |= VM_FAULT_WRITE;
1138 goto out_unlock;
1139 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001140 get_user_huge_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001141 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001142alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001143 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001144 !transparent_hugepage_debug_cow()) {
Michal Hocko3b3636922015-04-15 16:13:29 -07001145 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1146 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001147 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001148 new_page = NULL;
1149
1150 if (unlikely(!new_page)) {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001151 if (!page) {
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001152 split_huge_page_pmd(vma, address, pmd);
1153 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001154 } else {
1155 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1156 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001157 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001158 split_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001159 ret |= VM_FAULT_FALLBACK;
1160 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001161 put_user_huge_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001162 }
David Rientjes17766dd2013-09-12 15:14:06 -07001163 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001164 goto out;
1165 }
1166
Michal Hocko3b3636922015-04-15 16:13:29 -07001167 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001168 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001169 if (page) {
1170 split_huge_page(page);
Hugh Dickins5338a932014-06-23 13:22:05 -07001171 put_user_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001172 } else
1173 split_huge_page_pmd(vma, address, pmd);
1174 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001175 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001176 goto out;
1177 }
1178
David Rientjes17766dd2013-09-12 15:14:06 -07001179 count_vm_event(THP_FAULT_ALLOC);
1180
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001181 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001182 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1183 else
1184 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 __SetPageUptodate(new_page);
1186
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001187 mmun_start = haddr;
1188 mmun_end = haddr + HPAGE_PMD_SIZE;
1189 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1190
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001191 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001192 if (page)
Hugh Dickins5338a932014-06-23 13:22:05 -07001193 put_user_huge_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001194 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001195 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -07001196 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001197 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001198 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001199 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001200 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001201 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1202 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001203 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001204 page_add_new_anon_rmap(new_page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001205 mem_cgroup_commit_charge(new_page, memcg, false);
1206 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001208 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001209 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001210 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001211 put_huge_zero_page();
1212 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001213 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001214 page_remove_rmap(page);
1215 put_page(page);
1216 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001217 ret |= VM_FAULT_WRITE;
1218 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001219 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001220out_mn:
1221 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1222out:
1223 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001224out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001225 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001226 return ret;
1227}
1228
David Rientjesb676b292012-10-08 16:34:03 -07001229struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001230 unsigned long addr,
1231 pmd_t *pmd,
1232 unsigned int flags)
1233{
David Rientjesb676b292012-10-08 16:34:03 -07001234 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001235 struct page *page = NULL;
1236
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001237 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001238
1239 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1240 goto out;
1241
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001242 /* Avoid dumping huge zero page */
1243 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1244 return ERR_PTR(-EFAULT);
1245
Mel Gorman2b4847e2013-12-18 17:08:32 -08001246 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001247 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001248 goto out;
1249
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001250 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001251 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001252 if (flags & FOLL_TOUCH) {
1253 pmd_t _pmd;
1254 /*
1255 * We should set the dirty bit only for FOLL_WRITE but
1256 * for now the dirty bit in the pmd is meaningless.
1257 * And if the dirty bit will become meaningful and
1258 * we'll only set it with FOLL_WRITE, an atomic
1259 * set_bit will be required on the pmd to set the
1260 * young bit, instead of the current set_pmd_at.
1261 */
1262 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001263 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1264 pmd, _pmd, 1))
1265 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001266 }
Kirill A. Shutemov84d33df2015-04-14 15:44:37 -07001267 if ((flags & FOLL_POPULATE) && (vma->vm_flags & VM_LOCKED)) {
David Rientjesb676b292012-10-08 16:34:03 -07001268 if (page->mapping && trylock_page(page)) {
1269 lru_add_drain();
1270 if (page->mapping)
1271 mlock_vma_page(page);
1272 unlock_page(page);
1273 }
1274 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001275 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001276 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001277 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001278 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001279
1280out:
1281 return page;
1282}
1283
Mel Gormand10e63f2012-10-25 14:16:31 +02001284/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001285int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1286 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001287{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001288 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001289 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001290 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001291 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001292 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001293 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001294 bool page_locked;
1295 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001296 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001297 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001298
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001299 /* A PROT_NONE fault should not end up here */
1300 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1301
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001302 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001303 if (unlikely(!pmd_same(pmd, *pmdp)))
1304 goto out_unlock;
1305
Mel Gormande466bd2013-12-18 17:08:42 -08001306 /*
1307 * If there are potential migrations, wait for completion and retry
1308 * without disrupting NUMA hinting information. Do not relock and
1309 * check_same as the page may no longer be mapped.
1310 */
1311 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001312 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001313 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001314 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001315 goto out;
1316 }
1317
Mel Gormand10e63f2012-10-25 14:16:31 +02001318 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001319 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001320 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001321 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001322 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001323 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001324 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001325 flags |= TNF_FAULT_LOCAL;
1326 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001327
Mel Gormanbea66fb2015-03-25 15:55:37 -07001328 /* See similar comment in do_numa_page for explanation */
1329 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001330 flags |= TNF_NO_GROUP;
1331
1332 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001333 * Acquire the page lock to serialise THP migrations but avoid dropping
1334 * page_table_lock if at all possible
1335 */
Mel Gormanb8916632013-10-07 11:28:44 +01001336 page_locked = trylock_page(page);
1337 target_nid = mpol_misplaced(page, vma, haddr);
1338 if (target_nid == -1) {
1339 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001340 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001341 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001342 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001343
Mel Gormande466bd2013-12-18 17:08:42 -08001344 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001345 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001346 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001347 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001348 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001349 goto out;
1350 }
1351
Mel Gorman2b4847e2013-12-18 17:08:32 -08001352 /*
1353 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1354 * to serialises splits
1355 */
Mel Gormanb8916632013-10-07 11:28:44 +01001356 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001357 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001358 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001359
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001360 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001361 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001362 if (unlikely(!pmd_same(pmd, *pmdp))) {
1363 unlock_page(page);
1364 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001365 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001366 goto out_unlock;
1367 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001368
Mel Gormanc3a489c2013-12-18 17:08:38 -08001369 /* Bail if we fail to protect against THP splits for any reason */
1370 if (unlikely(!anon_vma)) {
1371 put_page(page);
1372 page_nid = -1;
1373 goto clear_pmdnuma;
1374 }
1375
Mel Gormana54a4072013-10-07 11:28:46 +01001376 /*
1377 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001378 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001379 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001380 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001381 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001382 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001383 if (migrated) {
1384 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001385 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001386 } else
1387 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001388
Mel Gorman8191acb2013-10-07 11:28:45 +01001389 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001390clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001391 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001392 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001393 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001394 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001395 if (was_writable)
1396 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001397 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001398 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001399 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001400out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001401 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001402
1403out:
1404 if (anon_vma)
1405 page_unlock_anon_vma_read(anon_vma);
1406
Mel Gorman8191acb2013-10-07 11:28:45 +01001407 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001408 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001409
Mel Gormand10e63f2012-10-25 14:16:31 +02001410 return 0;
1411}
1412
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001413int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001414 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001415{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001416 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001417 int ret = 0;
1418
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001419 if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001420 pgtable_t pgtable;
David Millerf5c8ad42012-10-08 16:34:26 -07001421 pmd_t orig_pmd;
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001422 /*
1423 * For architectures like ppc64 we look at deposited pgtable
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001424 * when calling pmdp_huge_get_and_clear. So do the
Aneesh Kumar K.Va6bf2bb2013-06-05 17:14:04 -07001425 * pgtable_trans_huge_withdraw after finishing pmdp related
1426 * operations.
1427 */
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001428 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1429 tlb->fullmm);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001430 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Matthew Wilcox4897c762015-09-08 14:58:45 -07001431 if (vma_is_dax(vma)) {
1432 if (is_huge_zero_pmd(orig_pmd)) {
1433 pgtable = NULL;
1434 } else {
1435 spin_unlock(ptl);
1436 return 1;
1437 }
1438 } else {
1439 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
1440 }
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001441 if (is_huge_zero_pmd(orig_pmd)) {
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001442 atomic_long_dec(&tlb->mm->nr_ptes);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001443 spin_unlock(ptl);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001444 put_huge_zero_page();
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001445 } else {
Matthew Wilcox4897c762015-09-08 14:58:45 -07001446 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001447 page_remove_rmap(page);
Sasha Levin309381fea2014-01-23 15:52:54 -08001448 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001449 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
Sasha Levin309381fea2014-01-23 15:52:54 -08001450 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001451 atomic_long_dec(&tlb->mm->nr_ptes);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001452 spin_unlock(ptl);
Kirill A. Shutemov479f0ab2012-12-12 13:50:50 -08001453 tlb_remove_page(tlb, page);
1454 }
Matthew Wilcox4897c762015-09-08 14:58:45 -07001455 if (pgtable)
1456 pte_free(tlb->mm, pgtable);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001457 ret = 1;
1458 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001459 return ret;
1460}
1461
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001462int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1463 unsigned long old_addr,
1464 unsigned long new_addr, unsigned long old_end,
1465 pmd_t *old_pmd, pmd_t *new_pmd)
1466{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001467 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001468 int ret = 0;
1469 pmd_t pmd;
1470
1471 struct mm_struct *mm = vma->vm_mm;
1472
1473 if ((old_addr & ~HPAGE_PMD_MASK) ||
1474 (new_addr & ~HPAGE_PMD_MASK) ||
1475 old_end - old_addr < HPAGE_PMD_SIZE ||
1476 (new_vma->vm_flags & VM_NOHUGEPAGE))
1477 goto out;
1478
1479 /*
1480 * The destination pmd shouldn't be established, free_pgtables()
1481 * should have release it.
1482 */
1483 if (WARN_ON(!pmd_none(*new_pmd))) {
1484 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1485 goto out;
1486 }
1487
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001488 /*
1489 * We don't have to worry about the ordering of src and dst
1490 * ptlocks because exclusive mmap_sem prevents deadlock.
1491 */
1492 ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001493 if (ret == 1) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001494 new_ptl = pmd_lockptr(mm, new_pmd);
1495 if (new_ptl != old_ptl)
1496 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001497 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001498 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001499
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301500 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1501 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001502 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1503 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001504 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301505 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1506 if (new_ptl != old_ptl)
1507 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001508 spin_unlock(old_ptl);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001509 }
1510out:
1511 return ret;
1512}
1513
Mel Gormanf123d742013-10-07 11:28:49 +01001514/*
1515 * Returns
1516 * - 0 if PMD could not be locked
1517 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1518 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1519 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001520int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001521 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001522{
1523 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001524 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001525 int ret = 0;
1526
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001527 if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001528 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001529 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc012015-03-07 15:20:48 +00001530 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001531
1532 /*
1533 * Avoid trapping faults against the zero page. The read-only
1534 * data is likely to be read-cached on the local CPU and
1535 * local/remote hits to the zero page are not interesting.
1536 */
1537 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1538 spin_unlock(ptl);
Mel Gormanba68bc012015-03-07 15:20:48 +00001539 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001540 }
1541
Mel Gorman10c10452015-02-12 14:58:44 -08001542 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001543 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001544 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001545 if (preserve_write)
1546 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001547 ret = HPAGE_PMD_NR;
1548 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001549 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001550 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001551 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001552 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001553
1554 return ret;
1555}
1556
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001557/*
1558 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1559 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1560 *
1561 * Note that if it returns 1, this routine returns without unlocking page
1562 * table locks. So callers must unlock them.
1563 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001564int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
1565 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001566{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001567 *ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001568 if (likely(pmd_trans_huge(*pmd))) {
1569 if (unlikely(pmd_trans_splitting(*pmd))) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001570 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001571 wait_split_huge_page(vma->anon_vma, pmd);
1572 return -1;
1573 } else {
1574 /* Thp mapped by 'pmd' is stable, so we can
1575 * handle it as it is. */
1576 return 1;
1577 }
1578 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001579 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001580 return 0;
1581}
1582
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001583/*
1584 * This function returns whether a given @page is mapped onto the @address
1585 * in the virtual space of @mm.
1586 *
1587 * When it's true, this function returns *pmd with holding the page table lock
1588 * and passing it back to the caller via @ptl.
1589 * If it's false, returns NULL without holding the page table lock.
1590 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001591pmd_t *page_check_address_pmd(struct page *page,
1592 struct mm_struct *mm,
1593 unsigned long address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001594 enum page_check_address_pmd_flag flag,
1595 spinlock_t **ptl)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001596{
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001597 pgd_t *pgd;
1598 pud_t *pud;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001599 pmd_t *pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001600
1601 if (address & ~HPAGE_PMD_MASK)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001602 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001603
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001604 pgd = pgd_offset(mm, address);
1605 if (!pgd_present(*pgd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001606 return NULL;
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001607 pud = pud_offset(pgd, address);
1608 if (!pud_present(*pud))
1609 return NULL;
1610 pmd = pmd_offset(pud, address);
1611
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001612 *ptl = pmd_lock(mm, pmd);
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001613 if (!pmd_present(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001614 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001615 if (pmd_page(*pmd) != page)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001616 goto unlock;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001617 /*
1618 * split_vma() may create temporary aliased mappings. There is
1619 * no risk as long as all huge pmd are found and have their
1620 * splitting bit set before __split_huge_page_refcount
1621 * runs. Finding the same huge pmd more than once during the
1622 * same rmap walk is not a problem.
1623 */
1624 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1625 pmd_trans_splitting(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001626 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001627 if (pmd_trans_huge(*pmd)) {
1628 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1629 !pmd_trans_splitting(*pmd));
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001630 return pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001631 }
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001632unlock:
1633 spin_unlock(*ptl);
1634 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001635}
1636
1637static int __split_huge_page_splitting(struct page *page,
1638 struct vm_area_struct *vma,
1639 unsigned long address)
1640{
1641 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001642 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001643 pmd_t *pmd;
1644 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001645 /* For mmu_notifiers */
1646 const unsigned long mmun_start = address;
1647 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001648
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001649 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001650 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001651 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001652 if (pmd) {
1653 /*
1654 * We can't temporarily set the pmd to null in order
1655 * to split it, the pmd must remain marked huge at all
1656 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001657 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001658 * serialize against split_huge_page*.
1659 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001660 pmdp_splitting_flush(vma, address, pmd);
Joerg Roedel34ee6452014-11-13 13:46:09 +11001661
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001662 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001663 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001665 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001666
1667 return ret;
1668}
1669
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001670static void __split_huge_page_refcount(struct page *page,
1671 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001672{
1673 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001674 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001675 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001676 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001677
1678 /* prevent PageLRU to go away from under us, and freeze lru stats */
1679 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001680 lruvec = mem_cgroup_page_lruvec(page, zone);
1681
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001682 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001683 /* complete memcg works before add pages to LRU */
1684 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001685
Shaohua Li45676882012-01-12 17:19:18 -08001686 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001687 struct page *page_tail = page + i;
1688
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001689 /* tail_page->_mapcount cannot change */
1690 BUG_ON(page_mapcount(page_tail) < 0);
1691 tail_count += page_mapcount(page_tail);
1692 /* check for overflow */
1693 BUG_ON(tail_count < 0);
1694 BUG_ON(atomic_read(&page_tail->_count) != 0);
1695 /*
1696 * tail_page->_count is zero and not changing from
1697 * under us. But get_page_unless_zero() may be running
1698 * from under us on the tail_page. If we used
1699 * atomic_set() below instead of atomic_add(), we
1700 * would then run atomic_set() concurrently with
1701 * get_page_unless_zero(), and atomic_set() is
1702 * implemented in C not using locked ops. spin_unlock
1703 * on x86 sometime uses locked ops because of PPro
1704 * errata 66, 92, so unless somebody can guarantee
1705 * atomic_set() here would be safe on all archs (and
1706 * not only on x86), it's safer to use atomic_add().
1707 */
1708 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1709 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001710
1711 /* after clearing PageTail the gup refcount can be released */
Waiman Long3a79d522014-08-06 16:05:38 -07001712 smp_mb__after_atomic();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001713
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001714 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001715 page_tail->flags |= (page->flags &
1716 ((1L << PG_referenced) |
1717 (1L << PG_swapbacked) |
1718 (1L << PG_mlocked) |
Kirill A. Shutemove180cf82013-07-31 13:53:39 -07001719 (1L << PG_uptodate) |
1720 (1L << PG_active) |
1721 (1L << PG_unevictable)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001722 page_tail->flags |= (1L << PG_dirty);
1723
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001724 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001725 smp_wmb();
1726
1727 /*
1728 * __split_huge_page_splitting() already set the
1729 * splitting bit in all pmd that could map this
1730 * hugepage, that will ensure no CPU can alter the
1731 * mapcount on the head page. The mapcount is only
1732 * accounted in the head page and it has to be
1733 * transferred to all tail pages in the below code. So
1734 * for this code to be safe, the split the mapcount
1735 * can't change. But that doesn't mean userland can't
1736 * keep changing and reading the page contents while
1737 * we transfer the mapcount, so the pmd splitting
1738 * status is achieved setting a reserved bit in the
1739 * pmd, not by clearing the present bit.
1740 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001741 page_tail->_mapcount = page->_mapcount;
1742
1743 BUG_ON(page_tail->mapping);
1744 page_tail->mapping = page->mapping;
1745
Shaohua Li45676882012-01-12 17:19:18 -08001746 page_tail->index = page->index + i;
Peter Zijlstra90572892013-10-07 11:29:20 +01001747 page_cpupid_xchg_last(page_tail, page_cpupid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001748
1749 BUG_ON(!PageAnon(page_tail));
1750 BUG_ON(!PageUptodate(page_tail));
1751 BUG_ON(!PageDirty(page_tail));
1752 BUG_ON(!PageSwapBacked(page_tail));
1753
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001754 lru_add_page_tail(page, page_tail, lruvec, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001755 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001756 atomic_sub(tail_count, &page->_count);
1757 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001758
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001759 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001760
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001761 ClearPageCompound(page);
1762 compound_unlock(page);
1763 spin_unlock_irq(&zone->lru_lock);
1764
1765 for (i = 1; i < HPAGE_PMD_NR; i++) {
1766 struct page *page_tail = page + i;
1767 BUG_ON(page_count(page_tail) <= 0);
1768 /*
1769 * Tail pages may be freed if there wasn't any mapping
1770 * like if add_to_swap() is running on a lru page that
1771 * had its mapping zapped. And freeing these pages
1772 * requires taking the lru_lock so we do the put_page
1773 * of the tail pages after the split is complete.
1774 */
1775 put_page(page_tail);
1776 }
1777
1778 /*
1779 * Only the head page (now become a regular page) is required
1780 * to be pinned by the caller.
1781 */
1782 BUG_ON(page_count(page) <= 0);
1783}
1784
1785static int __split_huge_page_map(struct page *page,
1786 struct vm_area_struct *vma,
1787 unsigned long address)
1788{
1789 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001790 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001791 pmd_t *pmd, _pmd;
1792 int ret = 0, i;
1793 pgtable_t pgtable;
1794 unsigned long haddr;
1795
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001796 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001797 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001798 if (pmd) {
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001799 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001800 pmd_populate(mm, &_pmd, pgtable);
Waiman Longf8303c22014-08-06 16:05:36 -07001801 if (pmd_write(*pmd))
1802 BUG_ON(page_mapcount(page) != 1);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001803
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001804 haddr = address;
1805 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001806 pte_t *pte, entry;
1807 BUG_ON(PageCompound(page+i));
Mel Gormanabc40bd2014-10-02 19:47:42 +01001808 /*
Mel Gorman8a0516e2015-02-12 14:58:22 -08001809 * Note that NUMA hinting access restrictions are not
1810 * transferred to avoid any possibility of altering
1811 * permissions across VMAs.
Mel Gormanabc40bd2014-10-02 19:47:42 +01001812 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001813 entry = mk_pte(page + i, vma->vm_page_prot);
1814 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1815 if (!pmd_write(*pmd))
1816 entry = pte_wrprotect(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001817 if (!pmd_young(*pmd))
1818 entry = pte_mkold(entry);
1819 pte = pte_offset_map(&_pmd, haddr);
1820 BUG_ON(!pte_none(*pte));
1821 set_pte_at(mm, haddr, pte, entry);
1822 pte_unmap(pte);
1823 }
1824
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001825 smp_wmb(); /* make pte visible before pmd */
1826 /*
1827 * Up to this point the pmd is present and huge and
1828 * userland has the whole access to the hugepage
1829 * during the split (which happens in place). If we
1830 * overwrite the pmd with the not-huge version
1831 * pointing to the pte here (which of course we could
1832 * if all CPUs were bug free), userland could trigger
1833 * a small page size TLB miss on the small sized TLB
1834 * while the hugepage TLB entry is still established
1835 * in the huge TLB. Some CPU doesn't like that. See
1836 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1837 * Erratum 383 on page 93. Intel should be safe but is
1838 * also warns that it's only safe if the permission
1839 * and cache attributes of the two entries loaded in
1840 * the two TLB is identical (which should be the case
1841 * here). But it is generally safer to never allow
1842 * small and huge TLB entries for the same virtual
1843 * address to be loaded simultaneously. So instead of
1844 * doing "pmd_populate(); flush_tlb_range();" we first
1845 * mark the current pmd notpresent (atomically because
1846 * here the pmd_trans_huge and pmd_trans_splitting
1847 * must remain set at all times on the pmd until the
1848 * split is complete for this pmd), then we flush the
1849 * SMP TLB and finally we write the non-huge version
1850 * of the pmd entry with pmd_populate.
1851 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001852 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001853 pmd_populate(mm, pmd, pgtable);
1854 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001855 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001856 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001857
1858 return ret;
1859}
1860
Ingo Molnar5a505082012-12-02 19:56:46 +00001861/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001862static void __split_huge_page(struct page *page,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001863 struct anon_vma *anon_vma,
1864 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001865{
1866 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001867 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001868 struct anon_vma_chain *avc;
1869
1870 BUG_ON(!PageHead(page));
1871 BUG_ON(PageTail(page));
1872
1873 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001874 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001875 struct vm_area_struct *vma = avc->vma;
1876 unsigned long addr = vma_address(page, vma);
1877 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001878 mapcount += __split_huge_page_splitting(page, vma, addr);
1879 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001880 /*
1881 * It is critical that new vmas are added to the tail of the
1882 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1883 * and establishes a child pmd before
1884 * __split_huge_page_splitting() freezes the parent pmd (so if
1885 * we fail to prevent copy_huge_pmd() from running until the
1886 * whole __split_huge_page() is complete), we will still see
1887 * the newly established pmd of the child later during the
1888 * walk, to be able to set it as pmd_trans_splitting too.
1889 */
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001890 if (mapcount != page_mapcount(page)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001891 pr_err("mapcount %d page_mapcount %d\n",
1892 mapcount, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001893 BUG();
1894 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001895
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001896 __split_huge_page_refcount(page, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001897
1898 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001899 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001900 struct vm_area_struct *vma = avc->vma;
1901 unsigned long addr = vma_address(page, vma);
1902 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001903 mapcount2 += __split_huge_page_map(page, vma, addr);
1904 }
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001905 if (mapcount != mapcount2) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001906 pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
1907 mapcount, mapcount2, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001908 BUG();
1909 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001910}
1911
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001912/*
1913 * Split a hugepage into normal pages. This doesn't change the position of head
1914 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1915 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1916 * from the hugepage.
1917 * Return 0 if the hugepage is split successfully otherwise return 1.
1918 */
1919int split_huge_page_to_list(struct page *page, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001920{
1921 struct anon_vma *anon_vma;
1922 int ret = 1;
1923
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001924 BUG_ON(is_huge_zero_page(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001925 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001926
1927 /*
1928 * The caller does not necessarily hold an mmap_sem that would prevent
1929 * the anon_vma disappearing so we first we take a reference to it
1930 * and then lock the anon_vma for write. This is similar to
1931 * page_lock_anon_vma_read except the write lock is taken to serialise
1932 * against parallel split or collapse operations.
1933 */
1934 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001935 if (!anon_vma)
1936 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001937 anon_vma_lock_write(anon_vma);
1938
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001939 ret = 0;
1940 if (!PageCompound(page))
1941 goto out_unlock;
1942
1943 BUG_ON(!PageSwapBacked(page));
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001944 __split_huge_page(page, anon_vma, list);
Andi Kleen81ab4202011-04-14 15:22:06 -07001945 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001946
1947 BUG_ON(PageCompound(page));
1948out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08001949 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08001950 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001951out:
1952 return ret;
1953}
1954
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001955#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001956
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001957int hugepage_madvise(struct vm_area_struct *vma,
1958 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001959{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001960 switch (advice) {
1961 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001962#ifdef CONFIG_S390
1963 /*
1964 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1965 * can't handle this properly after s390_enable_sie, so we simply
1966 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1967 */
1968 if (mm_has_pgste(vma->vm_mm))
1969 return 0;
1970#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001971 /*
1972 * Be somewhat over-protective like KSM for now!
1973 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001974 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001975 return -EINVAL;
1976 *vm_flags &= ~VM_NOHUGEPAGE;
1977 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001978 /*
1979 * If the vma become good for khugepaged to scan,
1980 * register it here without waiting a page fault that
1981 * may not happen any time soon.
1982 */
David Rientjes6d50e602014-10-29 14:50:31 -07001983 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001984 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001985 break;
1986 case MADV_NOHUGEPAGE:
1987 /*
1988 * Be somewhat over-protective like KSM for now!
1989 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001990 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001991 return -EINVAL;
1992 *vm_flags &= ~VM_HUGEPAGE;
1993 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001994 /*
1995 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1996 * this vma even if we leave the mm registered in khugepaged if
1997 * it got registered before VM_NOHUGEPAGE was set.
1998 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001999 break;
2000 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08002001
2002 return 0;
2003}
2004
Andrea Arcangeliba761492011-01-13 15:46:58 -08002005static int __init khugepaged_slab_init(void)
2006{
2007 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
2008 sizeof(struct mm_slot),
2009 __alignof__(struct mm_slot), 0, NULL);
2010 if (!mm_slot_cache)
2011 return -ENOMEM;
2012
2013 return 0;
2014}
2015
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07002016static void __init khugepaged_slab_exit(void)
2017{
2018 kmem_cache_destroy(mm_slot_cache);
2019}
2020
Andrea Arcangeliba761492011-01-13 15:46:58 -08002021static inline struct mm_slot *alloc_mm_slot(void)
2022{
2023 if (!mm_slot_cache) /* initialization failed */
2024 return NULL;
2025 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
2026}
2027
2028static inline void free_mm_slot(struct mm_slot *mm_slot)
2029{
2030 kmem_cache_free(mm_slot_cache, mm_slot);
2031}
2032
Andrea Arcangeliba761492011-01-13 15:46:58 -08002033static struct mm_slot *get_mm_slot(struct mm_struct *mm)
2034{
2035 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002036
Sasha Levinb67bfe02013-02-27 17:06:00 -08002037 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002038 if (mm == mm_slot->mm)
2039 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002040
Andrea Arcangeliba761492011-01-13 15:46:58 -08002041 return NULL;
2042}
2043
2044static void insert_to_mm_slots_hash(struct mm_struct *mm,
2045 struct mm_slot *mm_slot)
2046{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002047 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002048 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002049}
2050
2051static inline int khugepaged_test_exit(struct mm_struct *mm)
2052{
2053 return atomic_read(&mm->mm_users) == 0;
2054}
2055
2056int __khugepaged_enter(struct mm_struct *mm)
2057{
2058 struct mm_slot *mm_slot;
2059 int wakeup;
2060
2061 mm_slot = alloc_mm_slot();
2062 if (!mm_slot)
2063 return -ENOMEM;
2064
2065 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07002066 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002067 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
2068 free_mm_slot(mm_slot);
2069 return 0;
2070 }
2071
2072 spin_lock(&khugepaged_mm_lock);
2073 insert_to_mm_slots_hash(mm, mm_slot);
2074 /*
2075 * Insert just behind the scanning cursor, to let the area settle
2076 * down a little.
2077 */
2078 wakeup = list_empty(&khugepaged_scan.mm_head);
2079 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
2080 spin_unlock(&khugepaged_mm_lock);
2081
2082 atomic_inc(&mm->mm_count);
2083 if (wakeup)
2084 wake_up_interruptible(&khugepaged_wait);
2085
2086 return 0;
2087}
2088
David Rientjes6d50e602014-10-29 14:50:31 -07002089int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
2090 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002091{
2092 unsigned long hstart, hend;
2093 if (!vma->anon_vma)
2094 /*
2095 * Not yet faulted in so we will register later in the
2096 * page fault if needed.
2097 */
2098 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002099 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002100 /* khugepaged not yet working on file or special mappings */
2101 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07002102 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002103 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2104 hend = vma->vm_end & HPAGE_PMD_MASK;
2105 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07002106 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002107 return 0;
2108}
2109
2110void __khugepaged_exit(struct mm_struct *mm)
2111{
2112 struct mm_slot *mm_slot;
2113 int free = 0;
2114
2115 spin_lock(&khugepaged_mm_lock);
2116 mm_slot = get_mm_slot(mm);
2117 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002118 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002119 list_del(&mm_slot->mm_node);
2120 free = 1;
2121 }
Chris Wrightd788e802011-07-25 17:12:14 -07002122 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002123
2124 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002125 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2126 free_mm_slot(mm_slot);
2127 mmdrop(mm);
2128 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002129 /*
2130 * This is required to serialize against
2131 * khugepaged_test_exit() (which is guaranteed to run
2132 * under mmap sem read mode). Stop here (after we
2133 * return all pagetables will be destroyed) until
2134 * khugepaged has finished working on the pagetables
2135 * under the mmap_sem.
2136 */
2137 down_write(&mm->mmap_sem);
2138 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002139 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002140}
2141
2142static void release_pte_page(struct page *page)
2143{
2144 /* 0 stands for page_is_file_cache(page) == false */
2145 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2146 unlock_page(page);
2147 putback_lru_page(page);
2148}
2149
2150static void release_pte_pages(pte_t *pte, pte_t *_pte)
2151{
2152 while (--_pte >= pte) {
2153 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002154 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002155 release_pte_page(pte_page(pteval));
2156 }
2157}
2158
Andrea Arcangeliba761492011-01-13 15:46:58 -08002159static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2160 unsigned long address,
2161 pte_t *pte)
2162{
2163 struct page *page;
2164 pte_t *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002165 int none_or_zero = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002166 bool referenced = false, writable = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002167 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2168 _pte++, address += PAGE_SIZE) {
2169 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002170 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002171 if (!userfaultfd_armed(vma) &&
2172 ++none_or_zero <= khugepaged_max_ptes_none)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002173 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002174 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002175 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002176 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002177 if (!pte_present(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002178 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002179 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002180 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002181 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002182
Sasha Levin309381fea2014-01-23 15:52:54 -08002183 VM_BUG_ON_PAGE(PageCompound(page), page);
2184 VM_BUG_ON_PAGE(!PageAnon(page), page);
2185 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002186
Andrea Arcangeliba761492011-01-13 15:46:58 -08002187 /*
2188 * We can do it before isolate_lru_page because the
2189 * page can't be freed from under us. NOTE: PG_lock
2190 * is needed to serialize against split_huge_page
2191 * when invoked from the VM.
2192 */
Bob Liu344aa352012-12-11 16:00:34 -08002193 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002194 goto out;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002195
2196 /*
2197 * cannot use mapcount: can't collapse if there's a gup pin.
2198 * The page must only be referenced by the scanned process
2199 * and page swap cache.
2200 */
2201 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2202 unlock_page(page);
2203 goto out;
2204 }
2205 if (pte_write(pteval)) {
2206 writable = true;
2207 } else {
2208 if (PageSwapCache(page) && !reuse_swap_page(page)) {
2209 unlock_page(page);
2210 goto out;
2211 }
2212 /*
2213 * Page is not in the swap cache. It can be collapsed
2214 * into a THP.
2215 */
2216 }
2217
Andrea Arcangeliba761492011-01-13 15:46:58 -08002218 /*
2219 * Isolate the page to avoid collapsing an hugepage
2220 * currently in use by the VM.
2221 */
2222 if (isolate_lru_page(page)) {
2223 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002224 goto out;
2225 }
2226 /* 0 stands for page_is_file_cache(page) == false */
2227 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002228 VM_BUG_ON_PAGE(!PageLocked(page), page);
2229 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002230
2231 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002232 if (pte_young(pteval) || PageReferenced(page) ||
2233 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002234 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002235 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002236 if (likely(referenced && writable))
Bob Liu344aa352012-12-11 16:00:34 -08002237 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002238out:
Bob Liu344aa352012-12-11 16:00:34 -08002239 release_pte_pages(pte, _pte);
2240 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002241}
2242
2243static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2244 struct vm_area_struct *vma,
2245 unsigned long address,
2246 spinlock_t *ptl)
2247{
2248 pte_t *_pte;
2249 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2250 pte_t pteval = *_pte;
2251 struct page *src_page;
2252
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002253 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002254 clear_user_highpage(page, address);
2255 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002256 if (is_zero_pfn(pte_pfn(pteval))) {
2257 /*
2258 * ptl mostly unnecessary.
2259 */
2260 spin_lock(ptl);
2261 /*
2262 * paravirt calls inside pte_clear here are
2263 * superfluous.
2264 */
2265 pte_clear(vma->vm_mm, address, _pte);
2266 spin_unlock(ptl);
2267 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002268 } else {
2269 src_page = pte_page(pteval);
2270 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002271 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002272 release_pte_page(src_page);
2273 /*
2274 * ptl mostly unnecessary, but preempt has to
2275 * be disabled to update the per-cpu stats
2276 * inside page_remove_rmap().
2277 */
2278 spin_lock(ptl);
2279 /*
2280 * paravirt calls inside pte_clear here are
2281 * superfluous.
2282 */
2283 pte_clear(vma->vm_mm, address, _pte);
2284 page_remove_rmap(src_page);
2285 spin_unlock(ptl);
2286 free_page_and_swap_cache(src_page);
2287 }
2288
2289 address += PAGE_SIZE;
2290 page++;
2291 }
2292}
2293
Xiao Guangrong26234f32012-10-08 16:29:51 -07002294static void khugepaged_alloc_sleep(void)
2295{
2296 wait_event_freezable_timeout(khugepaged_wait, false,
2297 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2298}
2299
Bob Liu9f1b8682013-11-12 15:07:37 -08002300static int khugepaged_node_load[MAX_NUMNODES];
2301
David Rientjes14a4e212014-08-06 16:07:29 -07002302static bool khugepaged_scan_abort(int nid)
2303{
2304 int i;
2305
2306 /*
2307 * If zone_reclaim_mode is disabled, then no extra effort is made to
2308 * allocate memory locally.
2309 */
2310 if (!zone_reclaim_mode)
2311 return false;
2312
2313 /* If there is a count for this node already, it must be acceptable */
2314 if (khugepaged_node_load[nid])
2315 return false;
2316
2317 for (i = 0; i < MAX_NUMNODES; i++) {
2318 if (!khugepaged_node_load[i])
2319 continue;
2320 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2321 return true;
2322 }
2323 return false;
2324}
2325
Xiao Guangrong26234f32012-10-08 16:29:51 -07002326#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002327static int khugepaged_find_target_node(void)
2328{
2329 static int last_khugepaged_target_node = NUMA_NO_NODE;
2330 int nid, target_node = 0, max_value = 0;
2331
2332 /* find first node with max normal pages hit */
2333 for (nid = 0; nid < MAX_NUMNODES; nid++)
2334 if (khugepaged_node_load[nid] > max_value) {
2335 max_value = khugepaged_node_load[nid];
2336 target_node = nid;
2337 }
2338
2339 /* do some balance if several nodes have the same hit record */
2340 if (target_node <= last_khugepaged_target_node)
2341 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2342 nid++)
2343 if (max_value == khugepaged_node_load[nid]) {
2344 target_node = nid;
2345 break;
2346 }
2347
2348 last_khugepaged_target_node = target_node;
2349 return target_node;
2350}
2351
Xiao Guangrong26234f32012-10-08 16:29:51 -07002352static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2353{
2354 if (IS_ERR(*hpage)) {
2355 if (!*wait)
2356 return false;
2357
2358 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002359 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002360 khugepaged_alloc_sleep();
2361 } else if (*hpage) {
2362 put_page(*hpage);
2363 *hpage = NULL;
2364 }
2365
2366 return true;
2367}
2368
Michal Hocko3b3636922015-04-15 16:13:29 -07002369static struct page *
2370khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002371 struct vm_area_struct *vma, unsigned long address,
2372 int node)
2373{
Sasha Levin309381fea2014-01-23 15:52:54 -08002374 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002375
Xiao Guangrong26234f32012-10-08 16:29:51 -07002376 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002377 * Before allocating the hugepage, release the mmap_sem read lock.
2378 * The allocation can take potentially a long time if it involves
2379 * sync compaction, and we do not need to hold the mmap_sem during
2380 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002381 */
2382 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002383
Michal Hocko3b3636922015-04-15 16:13:29 -07002384 *hpage = alloc_pages_exact_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002385 if (unlikely(!*hpage)) {
2386 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2387 *hpage = ERR_PTR(-ENOMEM);
2388 return NULL;
2389 }
2390
2391 count_vm_event(THP_COLLAPSE_ALLOC);
2392 return *hpage;
2393}
2394#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002395static int khugepaged_find_target_node(void)
2396{
2397 return 0;
2398}
2399
Bob Liu10dc4152013-11-12 15:07:35 -08002400static inline struct page *alloc_hugepage(int defrag)
2401{
2402 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
2403 HPAGE_PMD_ORDER);
2404}
2405
Xiao Guangrong26234f32012-10-08 16:29:51 -07002406static struct page *khugepaged_alloc_hugepage(bool *wait)
2407{
2408 struct page *hpage;
2409
2410 do {
2411 hpage = alloc_hugepage(khugepaged_defrag());
2412 if (!hpage) {
2413 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2414 if (!*wait)
2415 return NULL;
2416
2417 *wait = false;
2418 khugepaged_alloc_sleep();
2419 } else
2420 count_vm_event(THP_COLLAPSE_ALLOC);
2421 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2422
2423 return hpage;
2424}
2425
2426static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2427{
2428 if (!*hpage)
2429 *hpage = khugepaged_alloc_hugepage(wait);
2430
2431 if (unlikely(!*hpage))
2432 return false;
2433
2434 return true;
2435}
2436
Michal Hocko3b3636922015-04-15 16:13:29 -07002437static struct page *
2438khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002439 struct vm_area_struct *vma, unsigned long address,
2440 int node)
2441{
2442 up_read(&mm->mmap_sem);
2443 VM_BUG_ON(!*hpage);
Michal Hocko3b3636922015-04-15 16:13:29 -07002444
Xiao Guangrong26234f32012-10-08 16:29:51 -07002445 return *hpage;
2446}
2447#endif
2448
Bob Liufa475e52012-12-11 16:00:39 -08002449static bool hugepage_vma_check(struct vm_area_struct *vma)
2450{
2451 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2452 (vma->vm_flags & VM_NOHUGEPAGE))
2453 return false;
2454
2455 if (!vma->anon_vma || vma->vm_ops)
2456 return false;
2457 if (is_vma_temporary_stack(vma))
2458 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002459 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002460 return true;
2461}
2462
Andrea Arcangeliba761492011-01-13 15:46:58 -08002463static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002464 unsigned long address,
2465 struct page **hpage,
2466 struct vm_area_struct *vma,
2467 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002468{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002469 pmd_t *pmd, _pmd;
2470 pte_t *pte;
2471 pgtable_t pgtable;
2472 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002473 spinlock_t *pmd_ptl, *pte_ptl;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002474 int isolated;
2475 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002476 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002477 unsigned long mmun_start; /* For mmu_notifiers */
2478 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07002479 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002480
2481 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002482
Michal Hocko3b3636922015-04-15 16:13:29 -07002483 /* Only allocate from the target node */
2484 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2485 __GFP_THISNODE;
2486
Xiao Guangrong26234f32012-10-08 16:29:51 -07002487 /* release the mmap_sem read lock. */
Michal Hocko3b3636922015-04-15 16:13:29 -07002488 new_page = khugepaged_alloc_page(hpage, gfp, mm, vma, address, node);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002489 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002490 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002491
Johannes Weiner00501b52014-08-08 14:19:20 -07002492 if (unlikely(mem_cgroup_try_charge(new_page, mm,
Michal Hocko3b3636922015-04-15 16:13:29 -07002493 gfp, &memcg)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002494 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002495
2496 /*
2497 * Prevent all access to pagetables with the exception of
2498 * gup_fast later hanlded by the ptep_clear_flush and the VM
2499 * handled by the anon_vma lock + PG_lock.
2500 */
2501 down_write(&mm->mmap_sem);
2502 if (unlikely(khugepaged_test_exit(mm)))
2503 goto out;
2504
2505 vma = find_vma(mm, address);
Libina8f531eb2013-09-11 14:20:38 -07002506 if (!vma)
2507 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002508 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2509 hend = vma->vm_end & HPAGE_PMD_MASK;
2510 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2511 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002512 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002513 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002514 pmd = mm_find_pmd(mm, address);
2515 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002516 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002517
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002518 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002519
2520 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002521 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002522
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002523 mmun_start = address;
2524 mmun_end = address + HPAGE_PMD_SIZE;
2525 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002526 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002527 /*
2528 * After this gup_fast can't run anymore. This also removes
2529 * any huge TLB entry from the CPU so we won't allow
2530 * huge and small TLB entries for the same virtual address
2531 * to avoid the risk of CPU bugs in that area.
2532 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002533 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002534 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002535 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002536
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002537 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002538 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002539 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002540
2541 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002542 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002543 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002544 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002545 /*
2546 * We can only use set_pmd_at when establishing
2547 * hugepmds and never for establishing regular pmds that
2548 * points to regular pagetables. Use pmd_populate for that
2549 */
2550 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002551 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002552 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002553 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002554 }
2555
2556 /*
2557 * All pages are isolated and locked so anon_vma rmap
2558 * can't run anymore.
2559 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002560 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002561
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002562 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002563 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002564 __SetPageUptodate(new_page);
2565 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002566
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002567 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2568 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002569
2570 /*
2571 * spin_lock() below is not the equivalent of smp_wmb(), so
2572 * this is needed to avoid the copy_huge_page writes to become
2573 * visible after the set_pmd_at() write.
2574 */
2575 smp_wmb();
2576
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002577 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002578 BUG_ON(!pmd_none(*pmd));
2579 page_add_new_anon_rmap(new_page, vma, address);
Johannes Weiner00501b52014-08-08 14:19:20 -07002580 mem_cgroup_commit_charge(new_page, memcg, false);
2581 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002582 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002583 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002584 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002585 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002586
2587 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002588
Andrea Arcangeliba761492011-01-13 15:46:58 -08002589 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002590out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002591 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002592 return;
2593
Andrea Arcangelice83d212011-01-13 15:47:06 -08002594out:
Johannes Weiner00501b52014-08-08 14:19:20 -07002595 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002596 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002597}
2598
2599static int khugepaged_scan_pmd(struct mm_struct *mm,
2600 struct vm_area_struct *vma,
2601 unsigned long address,
2602 struct page **hpage)
2603{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002604 pmd_t *pmd;
2605 pte_t *pte, *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002606 int ret = 0, none_or_zero = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607 struct page *page;
2608 unsigned long _address;
2609 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002610 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002611 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002612
2613 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2614
Bob Liu62190492012-12-11 16:00:37 -08002615 pmd = mm_find_pmd(mm, address);
2616 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002617 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002618
Bob Liu9f1b8682013-11-12 15:07:37 -08002619 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002620 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2621 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2622 _pte++, _address += PAGE_SIZE) {
2623 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002624 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002625 if (!userfaultfd_armed(vma) &&
2626 ++none_or_zero <= khugepaged_max_ptes_none)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002627 continue;
2628 else
2629 goto out_unmap;
2630 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002631 if (!pte_present(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002632 goto out_unmap;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002633 if (pte_write(pteval))
2634 writable = true;
2635
Andrea Arcangeliba761492011-01-13 15:46:58 -08002636 page = vm_normal_page(vma, _address, pteval);
2637 if (unlikely(!page))
2638 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002639 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002640 * Record which node the original page is from and save this
2641 * information to khugepaged_node_load[].
2642 * Khupaged will allocate hugepage from the node has the max
2643 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002644 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002645 node = page_to_nid(page);
David Rientjes14a4e212014-08-06 16:07:29 -07002646 if (khugepaged_scan_abort(node))
2647 goto out_unmap;
Bob Liu9f1b8682013-11-12 15:07:37 -08002648 khugepaged_node_load[node]++;
Sasha Levin309381fea2014-01-23 15:52:54 -08002649 VM_BUG_ON_PAGE(PageCompound(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002650 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2651 goto out_unmap;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002652 /*
2653 * cannot use mapcount: can't collapse if there's a gup pin.
2654 * The page must only be referenced by the scanned process
2655 * and page swap cache.
2656 */
2657 if (page_count(page) != 1 + !!PageSwapCache(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002658 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002659 if (pte_young(pteval) || PageReferenced(page) ||
2660 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002661 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002662 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002663 if (referenced && writable)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002664 ret = 1;
2665out_unmap:
2666 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002667 if (ret) {
2668 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002669 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002670 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002671 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002672out:
2673 return ret;
2674}
2675
2676static void collect_mm_slot(struct mm_slot *mm_slot)
2677{
2678 struct mm_struct *mm = mm_slot->mm;
2679
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002680 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002681
2682 if (khugepaged_test_exit(mm)) {
2683 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002684 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002685 list_del(&mm_slot->mm_node);
2686
2687 /*
2688 * Not strictly needed because the mm exited already.
2689 *
2690 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2691 */
2692
2693 /* khugepaged_mm_lock actually not necessary for the below */
2694 free_mm_slot(mm_slot);
2695 mmdrop(mm);
2696 }
2697}
2698
2699static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2700 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002701 __releases(&khugepaged_mm_lock)
2702 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002703{
2704 struct mm_slot *mm_slot;
2705 struct mm_struct *mm;
2706 struct vm_area_struct *vma;
2707 int progress = 0;
2708
2709 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002710 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002711
2712 if (khugepaged_scan.mm_slot)
2713 mm_slot = khugepaged_scan.mm_slot;
2714 else {
2715 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2716 struct mm_slot, mm_node);
2717 khugepaged_scan.address = 0;
2718 khugepaged_scan.mm_slot = mm_slot;
2719 }
2720 spin_unlock(&khugepaged_mm_lock);
2721
2722 mm = mm_slot->mm;
2723 down_read(&mm->mmap_sem);
2724 if (unlikely(khugepaged_test_exit(mm)))
2725 vma = NULL;
2726 else
2727 vma = find_vma(mm, khugepaged_scan.address);
2728
2729 progress++;
2730 for (; vma; vma = vma->vm_next) {
2731 unsigned long hstart, hend;
2732
2733 cond_resched();
2734 if (unlikely(khugepaged_test_exit(mm))) {
2735 progress++;
2736 break;
2737 }
Bob Liufa475e52012-12-11 16:00:39 -08002738 if (!hugepage_vma_check(vma)) {
2739skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002740 progress++;
2741 continue;
2742 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002743 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2744 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002745 if (hstart >= hend)
2746 goto skip;
2747 if (khugepaged_scan.address > hend)
2748 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002749 if (khugepaged_scan.address < hstart)
2750 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002751 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002752
2753 while (khugepaged_scan.address < hend) {
2754 int ret;
2755 cond_resched();
2756 if (unlikely(khugepaged_test_exit(mm)))
2757 goto breakouterloop;
2758
2759 VM_BUG_ON(khugepaged_scan.address < hstart ||
2760 khugepaged_scan.address + HPAGE_PMD_SIZE >
2761 hend);
2762 ret = khugepaged_scan_pmd(mm, vma,
2763 khugepaged_scan.address,
2764 hpage);
2765 /* move to next address */
2766 khugepaged_scan.address += HPAGE_PMD_SIZE;
2767 progress += HPAGE_PMD_NR;
2768 if (ret)
2769 /* we released mmap_sem so break loop */
2770 goto breakouterloop_mmap_sem;
2771 if (progress >= pages)
2772 goto breakouterloop;
2773 }
2774 }
2775breakouterloop:
2776 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2777breakouterloop_mmap_sem:
2778
2779 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002780 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002781 /*
2782 * Release the current mm_slot if this mm is about to die, or
2783 * if we scanned all vmas of this mm.
2784 */
2785 if (khugepaged_test_exit(mm) || !vma) {
2786 /*
2787 * Make sure that if mm_users is reaching zero while
2788 * khugepaged runs here, khugepaged_exit will find
2789 * mm_slot not pointing to the exiting mm.
2790 */
2791 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2792 khugepaged_scan.mm_slot = list_entry(
2793 mm_slot->mm_node.next,
2794 struct mm_slot, mm_node);
2795 khugepaged_scan.address = 0;
2796 } else {
2797 khugepaged_scan.mm_slot = NULL;
2798 khugepaged_full_scans++;
2799 }
2800
2801 collect_mm_slot(mm_slot);
2802 }
2803
2804 return progress;
2805}
2806
2807static int khugepaged_has_work(void)
2808{
2809 return !list_empty(&khugepaged_scan.mm_head) &&
2810 khugepaged_enabled();
2811}
2812
2813static int khugepaged_wait_event(void)
2814{
2815 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002816 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002817}
2818
Xiao Guangrongd5169042012-10-08 16:29:48 -07002819static void khugepaged_do_scan(void)
2820{
2821 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002822 unsigned int progress = 0, pass_through_head = 0;
2823 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002824 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002825
2826 barrier(); /* write khugepaged_pages_to_scan to local stack */
2827
2828 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002829 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002830 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002831
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002832 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002833
Jiri Kosinacd092412015-06-24 16:56:07 -07002834 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002835 break;
2836
Andrea Arcangeliba761492011-01-13 15:46:58 -08002837 spin_lock(&khugepaged_mm_lock);
2838 if (!khugepaged_scan.mm_slot)
2839 pass_through_head++;
2840 if (khugepaged_has_work() &&
2841 pass_through_head < 2)
2842 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002843 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002844 else
2845 progress = pages;
2846 spin_unlock(&khugepaged_mm_lock);
2847 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002848
Xiao Guangrongd5169042012-10-08 16:29:48 -07002849 if (!IS_ERR_OR_NULL(hpage))
2850 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002851}
2852
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002853static void khugepaged_wait_work(void)
2854{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002855 if (khugepaged_has_work()) {
2856 if (!khugepaged_scan_sleep_millisecs)
2857 return;
2858
2859 wait_event_freezable_timeout(khugepaged_wait,
2860 kthread_should_stop(),
2861 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2862 return;
2863 }
2864
2865 if (khugepaged_enabled())
2866 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2867}
2868
Andrea Arcangeliba761492011-01-13 15:46:58 -08002869static int khugepaged(void *none)
2870{
2871 struct mm_slot *mm_slot;
2872
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002873 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002874 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002875
Xiao Guangrongb7231782012-10-08 16:29:54 -07002876 while (!kthread_should_stop()) {
2877 khugepaged_do_scan();
2878 khugepaged_wait_work();
2879 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002880
2881 spin_lock(&khugepaged_mm_lock);
2882 mm_slot = khugepaged_scan.mm_slot;
2883 khugepaged_scan.mm_slot = NULL;
2884 if (mm_slot)
2885 collect_mm_slot(mm_slot);
2886 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002887 return 0;
2888}
2889
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002890static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2891 unsigned long haddr, pmd_t *pmd)
2892{
2893 struct mm_struct *mm = vma->vm_mm;
2894 pgtable_t pgtable;
2895 pmd_t _pmd;
2896 int i;
2897
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07002898 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002899 /* leave pmd empty until pte is filled */
2900
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07002901 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002902 pmd_populate(mm, &_pmd, pgtable);
2903
2904 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2905 pte_t *pte, entry;
2906 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2907 entry = pte_mkspecial(entry);
2908 pte = pte_offset_map(&_pmd, haddr);
2909 VM_BUG_ON(!pte_none(*pte));
2910 set_pte_at(mm, haddr, pte, entry);
2911 pte_unmap(pte);
2912 }
2913 smp_wmb(); /* make pte visible before pmd */
2914 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002915 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002916}
2917
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002918void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2919 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002920{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002921 spinlock_t *ptl;
Matthew Wilcox4897c762015-09-08 14:58:45 -07002922 struct page *page = NULL;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002923 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002924 unsigned long haddr = address & HPAGE_PMD_MASK;
2925 unsigned long mmun_start; /* For mmu_notifiers */
2926 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002927
2928 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002929
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002930 mmun_start = haddr;
2931 mmun_end = haddr + HPAGE_PMD_SIZE;
Hugh Dickins750e8162013-10-16 13:47:08 -07002932again:
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002933 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002934 ptl = pmd_lock(mm, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07002935 if (unlikely(!pmd_trans_huge(*pmd)))
2936 goto unlock;
2937 if (vma_is_dax(vma)) {
2938 pmdp_huge_clear_flush(vma, haddr, pmd);
2939 } else if (is_huge_zero_pmd(*pmd)) {
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002940 __split_huge_zero_page_pmd(vma, haddr, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07002941 } else {
2942 page = pmd_page(*pmd);
2943 VM_BUG_ON_PAGE(!page_count(page), page);
2944 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002945 }
Matthew Wilcox4897c762015-09-08 14:58:45 -07002946 unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002947 spin_unlock(ptl);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002948 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002949
Matthew Wilcox4897c762015-09-08 14:58:45 -07002950 if (!page)
2951 return;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002952
Matthew Wilcox4897c762015-09-08 14:58:45 -07002953 split_huge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002954 put_page(page);
Hugh Dickins750e8162013-10-16 13:47:08 -07002955
2956 /*
2957 * We don't always have down_write of mmap_sem here: a racing
2958 * do_huge_pmd_wp_page() might have copied-on-write to another
2959 * huge page before our split_huge_page() got the anon_vma lock.
2960 */
2961 if (unlikely(pmd_trans_huge(*pmd)))
2962 goto again;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002963}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002964
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002965void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
2966 pmd_t *pmd)
2967{
2968 struct vm_area_struct *vma;
2969
2970 vma = find_vma(mm, address);
2971 BUG_ON(vma == NULL);
2972 split_huge_page_pmd(vma, address, pmd);
2973}
2974
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002975static void split_huge_page_address(struct mm_struct *mm,
2976 unsigned long address)
2977{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002978 pgd_t *pgd;
2979 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002980 pmd_t *pmd;
2981
2982 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2983
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002984 pgd = pgd_offset(mm, address);
2985 if (!pgd_present(*pgd))
2986 return;
2987
2988 pud = pud_offset(pgd, address);
2989 if (!pud_present(*pud))
2990 return;
2991
2992 pmd = pmd_offset(pud, address);
2993 if (!pmd_present(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002994 return;
2995 /*
2996 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2997 * materialize from under us.
2998 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002999 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003000}
3001
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003002void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003003 unsigned long start,
3004 unsigned long end,
3005 long adjust_next)
3006{
3007 /*
3008 * If the new start address isn't hpage aligned and it could
3009 * previously contain an hugepage: check if we need to split
3010 * an huge pmd.
3011 */
3012 if (start & ~HPAGE_PMD_MASK &&
3013 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3014 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3015 split_huge_page_address(vma->vm_mm, start);
3016
3017 /*
3018 * If the new end address isn't hpage aligned and it could
3019 * previously contain an hugepage: check if we need to split
3020 * an huge pmd.
3021 */
3022 if (end & ~HPAGE_PMD_MASK &&
3023 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3024 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3025 split_huge_page_address(vma->vm_mm, end);
3026
3027 /*
3028 * If we're also updating the vma->vm_next->vm_start, if the new
3029 * vm_next->vm_start isn't page aligned and it could previously
3030 * contain an hugepage: check if we need to split an huge pmd.
3031 */
3032 if (adjust_next > 0) {
3033 struct vm_area_struct *next = vma->vm_next;
3034 unsigned long nstart = next->vm_start;
3035 nstart += adjust_next << PAGE_SHIFT;
3036 if (nstart & ~HPAGE_PMD_MASK &&
3037 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3038 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
3039 split_huge_page_address(next->vm_mm, nstart);
3040 }
3041}