blob: 883f613ada7e2a8ba114300a1aa93d846133fba6 [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
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700109static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800110{
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();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800144}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700146static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800147{
148 int err = 0;
149 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800150 if (!khugepaged_thread)
151 khugepaged_thread = kthread_run(khugepaged, NULL,
152 "khugepaged");
153 if (unlikely(IS_ERR(khugepaged_thread))) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700154 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800155 err = PTR_ERR(khugepaged_thread);
156 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700157 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800158 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700159
160 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800161 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800162
163 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700164 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700165 kthread_stop(khugepaged_thread);
166 khugepaged_thread = NULL;
167 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700168fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800169 return err;
170}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800171
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800172static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800173struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800174
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700175struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800176{
177 struct page *zero_page;
178retry:
179 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700180 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800181
182 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
183 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800184 if (!zero_page) {
185 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700186 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800187 }
188 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800189 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700190 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800191 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700192 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800193 goto retry;
194 }
195
196 /* We take additional reference here. It will be put back by shrinker */
197 atomic_set(&huge_zero_refcount, 2);
198 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700199 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800200}
201
202static void put_huge_zero_page(void)
203{
204 /*
205 * Counter should never go to zero here. Only shrinker can put
206 * last reference.
207 */
208 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
209}
210
Glauber Costa48896462013-08-28 10:18:15 +1000211static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
212 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800213{
Glauber Costa48896462013-08-28 10:18:15 +1000214 /* we can free zero page only if last reference remains */
215 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
216}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800217
Glauber Costa48896462013-08-28 10:18:15 +1000218static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
219 struct shrink_control *sc)
220{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800221 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700222 struct page *zero_page = xchg(&huge_zero_page, NULL);
223 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700224 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000225 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800226 }
227
228 return 0;
229}
230
231static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000232 .count_objects = shrink_huge_zero_page_count,
233 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800234 .seeks = DEFAULT_SEEKS,
235};
236
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800237#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800238
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800239static ssize_t double_flag_show(struct kobject *kobj,
240 struct kobj_attribute *attr, char *buf,
241 enum transparent_hugepage_flag enabled,
242 enum transparent_hugepage_flag req_madv)
243{
244 if (test_bit(enabled, &transparent_hugepage_flags)) {
245 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
246 return sprintf(buf, "[always] madvise never\n");
247 } else if (test_bit(req_madv, &transparent_hugepage_flags))
248 return sprintf(buf, "always [madvise] never\n");
249 else
250 return sprintf(buf, "always madvise [never]\n");
251}
252static ssize_t double_flag_store(struct kobject *kobj,
253 struct kobj_attribute *attr,
254 const char *buf, size_t count,
255 enum transparent_hugepage_flag enabled,
256 enum transparent_hugepage_flag req_madv)
257{
258 if (!memcmp("always", buf,
259 min(sizeof("always")-1, count))) {
260 set_bit(enabled, &transparent_hugepage_flags);
261 clear_bit(req_madv, &transparent_hugepage_flags);
262 } else if (!memcmp("madvise", buf,
263 min(sizeof("madvise")-1, count))) {
264 clear_bit(enabled, &transparent_hugepage_flags);
265 set_bit(req_madv, &transparent_hugepage_flags);
266 } else if (!memcmp("never", buf,
267 min(sizeof("never")-1, count))) {
268 clear_bit(enabled, &transparent_hugepage_flags);
269 clear_bit(req_madv, &transparent_hugepage_flags);
270 } else
271 return -EINVAL;
272
273 return count;
274}
275
276static ssize_t enabled_show(struct kobject *kobj,
277 struct kobj_attribute *attr, char *buf)
278{
279 return double_flag_show(kobj, attr, buf,
280 TRANSPARENT_HUGEPAGE_FLAG,
281 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
282}
283static ssize_t enabled_store(struct kobject *kobj,
284 struct kobj_attribute *attr,
285 const char *buf, size_t count)
286{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800287 ssize_t ret;
288
289 ret = double_flag_store(kobj, attr, buf, count,
290 TRANSPARENT_HUGEPAGE_FLAG,
291 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
292
293 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700294 int err;
295
296 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700297 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700298 mutex_unlock(&khugepaged_mutex);
299
Andrea Arcangeliba761492011-01-13 15:46:58 -0800300 if (err)
301 ret = err;
302 }
303
304 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800305}
306static struct kobj_attribute enabled_attr =
307 __ATTR(enabled, 0644, enabled_show, enabled_store);
308
309static ssize_t single_flag_show(struct kobject *kobj,
310 struct kobj_attribute *attr, char *buf,
311 enum transparent_hugepage_flag flag)
312{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700313 return sprintf(buf, "%d\n",
314 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800315}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700316
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800317static ssize_t single_flag_store(struct kobject *kobj,
318 struct kobj_attribute *attr,
319 const char *buf, size_t count,
320 enum transparent_hugepage_flag flag)
321{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700322 unsigned long value;
323 int ret;
324
325 ret = kstrtoul(buf, 10, &value);
326 if (ret < 0)
327 return ret;
328 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800329 return -EINVAL;
330
Ben Hutchingse27e6152011-04-14 15:22:21 -0700331 if (value)
332 set_bit(flag, &transparent_hugepage_flags);
333 else
334 clear_bit(flag, &transparent_hugepage_flags);
335
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800336 return count;
337}
338
339/*
340 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
341 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
342 * memory just to allocate one more hugepage.
343 */
344static ssize_t defrag_show(struct kobject *kobj,
345 struct kobj_attribute *attr, char *buf)
346{
347 return double_flag_show(kobj, attr, buf,
348 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
349 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
350}
351static ssize_t defrag_store(struct kobject *kobj,
352 struct kobj_attribute *attr,
353 const char *buf, size_t count)
354{
355 return double_flag_store(kobj, attr, buf, count,
356 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
357 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
358}
359static struct kobj_attribute defrag_attr =
360 __ATTR(defrag, 0644, defrag_show, defrag_store);
361
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800362static ssize_t use_zero_page_show(struct kobject *kobj,
363 struct kobj_attribute *attr, char *buf)
364{
365 return single_flag_show(kobj, attr, buf,
366 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
367}
368static ssize_t use_zero_page_store(struct kobject *kobj,
369 struct kobj_attribute *attr, const char *buf, size_t count)
370{
371 return single_flag_store(kobj, attr, buf, count,
372 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
373}
374static struct kobj_attribute use_zero_page_attr =
375 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800376#ifdef CONFIG_DEBUG_VM
377static ssize_t debug_cow_show(struct kobject *kobj,
378 struct kobj_attribute *attr, char *buf)
379{
380 return single_flag_show(kobj, attr, buf,
381 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
382}
383static ssize_t debug_cow_store(struct kobject *kobj,
384 struct kobj_attribute *attr,
385 const char *buf, size_t count)
386{
387 return single_flag_store(kobj, attr, buf, count,
388 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
389}
390static struct kobj_attribute debug_cow_attr =
391 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
392#endif /* CONFIG_DEBUG_VM */
393
394static struct attribute *hugepage_attr[] = {
395 &enabled_attr.attr,
396 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800397 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800398#ifdef CONFIG_DEBUG_VM
399 &debug_cow_attr.attr,
400#endif
401 NULL,
402};
403
404static struct attribute_group hugepage_attr_group = {
405 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800406};
407
408static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
409 struct kobj_attribute *attr,
410 char *buf)
411{
412 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
413}
414
415static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
416 struct kobj_attribute *attr,
417 const char *buf, size_t count)
418{
419 unsigned long msecs;
420 int err;
421
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700422 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800423 if (err || msecs > UINT_MAX)
424 return -EINVAL;
425
426 khugepaged_scan_sleep_millisecs = msecs;
427 wake_up_interruptible(&khugepaged_wait);
428
429 return count;
430}
431static struct kobj_attribute scan_sleep_millisecs_attr =
432 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
433 scan_sleep_millisecs_store);
434
435static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
436 struct kobj_attribute *attr,
437 char *buf)
438{
439 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
440}
441
442static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 const char *buf, size_t count)
445{
446 unsigned long msecs;
447 int err;
448
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700449 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800450 if (err || msecs > UINT_MAX)
451 return -EINVAL;
452
453 khugepaged_alloc_sleep_millisecs = msecs;
454 wake_up_interruptible(&khugepaged_wait);
455
456 return count;
457}
458static struct kobj_attribute alloc_sleep_millisecs_attr =
459 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
460 alloc_sleep_millisecs_store);
461
462static ssize_t pages_to_scan_show(struct kobject *kobj,
463 struct kobj_attribute *attr,
464 char *buf)
465{
466 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
467}
468static ssize_t pages_to_scan_store(struct kobject *kobj,
469 struct kobj_attribute *attr,
470 const char *buf, size_t count)
471{
472 int err;
473 unsigned long pages;
474
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700475 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800476 if (err || !pages || pages > UINT_MAX)
477 return -EINVAL;
478
479 khugepaged_pages_to_scan = pages;
480
481 return count;
482}
483static struct kobj_attribute pages_to_scan_attr =
484 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
485 pages_to_scan_store);
486
487static ssize_t pages_collapsed_show(struct kobject *kobj,
488 struct kobj_attribute *attr,
489 char *buf)
490{
491 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
492}
493static struct kobj_attribute pages_collapsed_attr =
494 __ATTR_RO(pages_collapsed);
495
496static ssize_t full_scans_show(struct kobject *kobj,
497 struct kobj_attribute *attr,
498 char *buf)
499{
500 return sprintf(buf, "%u\n", khugepaged_full_scans);
501}
502static struct kobj_attribute full_scans_attr =
503 __ATTR_RO(full_scans);
504
505static ssize_t khugepaged_defrag_show(struct kobject *kobj,
506 struct kobj_attribute *attr, char *buf)
507{
508 return single_flag_show(kobj, attr, buf,
509 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
510}
511static ssize_t khugepaged_defrag_store(struct kobject *kobj,
512 struct kobj_attribute *attr,
513 const char *buf, size_t count)
514{
515 return single_flag_store(kobj, attr, buf, count,
516 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
517}
518static struct kobj_attribute khugepaged_defrag_attr =
519 __ATTR(defrag, 0644, khugepaged_defrag_show,
520 khugepaged_defrag_store);
521
522/*
523 * max_ptes_none controls if khugepaged should collapse hugepages over
524 * any unmapped ptes in turn potentially increasing the memory
525 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
526 * reduce the available free memory in the system as it
527 * runs. Increasing max_ptes_none will instead potentially reduce the
528 * free memory in the system during the khugepaged scan.
529 */
530static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
531 struct kobj_attribute *attr,
532 char *buf)
533{
534 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
535}
536static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
537 struct kobj_attribute *attr,
538 const char *buf, size_t count)
539{
540 int err;
541 unsigned long max_ptes_none;
542
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700543 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800544 if (err || max_ptes_none > HPAGE_PMD_NR-1)
545 return -EINVAL;
546
547 khugepaged_max_ptes_none = max_ptes_none;
548
549 return count;
550}
551static struct kobj_attribute khugepaged_max_ptes_none_attr =
552 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
553 khugepaged_max_ptes_none_store);
554
555static struct attribute *khugepaged_attr[] = {
556 &khugepaged_defrag_attr.attr,
557 &khugepaged_max_ptes_none_attr.attr,
558 &pages_to_scan_attr.attr,
559 &pages_collapsed_attr.attr,
560 &full_scans_attr.attr,
561 &scan_sleep_millisecs_attr.attr,
562 &alloc_sleep_millisecs_attr.attr,
563 NULL,
564};
565
566static struct attribute_group khugepaged_attr_group = {
567 .attrs = khugepaged_attr,
568 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800569};
Shaohua Li569e5592012-01-12 17:19:11 -0800570
571static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
572{
573 int err;
574
575 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
576 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700577 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800578 return -ENOMEM;
579 }
580
581 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
582 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700583 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800584 goto delete_obj;
585 }
586
587 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
588 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700589 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800590 goto remove_hp_group;
591 }
592
593 return 0;
594
595remove_hp_group:
596 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
597delete_obj:
598 kobject_put(*hugepage_kobj);
599 return err;
600}
601
602static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
603{
604 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
605 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
606 kobject_put(hugepage_kobj);
607}
608#else
609static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
610{
611 return 0;
612}
613
614static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
615{
616}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800617#endif /* CONFIG_SYSFS */
618
619static int __init hugepage_init(void)
620{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800621 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800622 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800623
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800624 if (!has_transparent_hugepage()) {
625 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800626 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800627 }
628
Shaohua Li569e5592012-01-12 17:19:11 -0800629 err = hugepage_init_sysfs(&hugepage_kobj);
630 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700631 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800632
633 err = khugepaged_slab_init();
634 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700635 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800636
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700637 err = register_shrinker(&huge_zero_page_shrinker);
638 if (err)
639 goto err_hzp_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800640
Rik van Riel97562cd2011-01-13 15:47:12 -0800641 /*
642 * By default disable transparent hugepages on smaller systems,
643 * where the extra memory used could hurt more than TLB overhead
644 * is likely to save. The admin can still enable it through /sys.
645 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700646 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800647 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700648 return 0;
649 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800650
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700651 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700652 if (err)
653 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800654
Shaohua Li569e5592012-01-12 17:19:11 -0800655 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700656err_khugepaged:
657 unregister_shrinker(&huge_zero_page_shrinker);
658err_hzp_shrinker:
659 khugepaged_slab_exit();
660err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800661 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700662err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800663 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800664}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800665subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800666
667static int __init setup_transparent_hugepage(char *str)
668{
669 int ret = 0;
670 if (!str)
671 goto out;
672 if (!strcmp(str, "always")) {
673 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
674 &transparent_hugepage_flags);
675 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
676 &transparent_hugepage_flags);
677 ret = 1;
678 } else if (!strcmp(str, "madvise")) {
679 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
680 &transparent_hugepage_flags);
681 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
682 &transparent_hugepage_flags);
683 ret = 1;
684 } else if (!strcmp(str, "never")) {
685 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
686 &transparent_hugepage_flags);
687 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
688 &transparent_hugepage_flags);
689 ret = 1;
690 }
691out:
692 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700693 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800694 return ret;
695}
696__setup("transparent_hugepage=", setup_transparent_hugepage);
697
Mel Gormanb32967f2012-11-19 12:35:47 +0000698pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800699{
700 if (likely(vma->vm_flags & VM_WRITE))
701 pmd = pmd_mkwrite(pmd);
702 return pmd;
703}
704
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700705static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800706{
707 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700708 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800709 entry = pmd_mkhuge(entry);
710 return entry;
711}
712
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800713static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
714 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700715 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700716 struct page *page, gfp_t gfp,
717 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800718{
Johannes Weiner00501b52014-08-08 14:19:20 -0700719 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800720 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800721 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700722 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800723
Sasha Levin309381fea2014-01-23 15:52:54 -0800724 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700725
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700726 if (mem_cgroup_try_charge(page, mm, gfp, &memcg)) {
727 put_page(page);
728 count_vm_event(THP_FAULT_FALLBACK);
729 return VM_FAULT_FALLBACK;
730 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800731
Johannes Weiner00501b52014-08-08 14:19:20 -0700732 pgtable = pte_alloc_one(mm, haddr);
733 if (unlikely(!pgtable)) {
734 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700735 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700736 return VM_FAULT_OOM;
737 }
738
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800739 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700740 /*
741 * The memory barrier inside __SetPageUptodate makes sure that
742 * clear_huge_page writes become visible before the set_pmd_at()
743 * write.
744 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800745 __SetPageUptodate(page);
746
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800747 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800748 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800749 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -0700750 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800751 put_page(page);
752 pte_free(mm, pgtable);
753 } else {
754 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700755
756 /* Deliver the page fault to userland */
757 if (userfaultfd_missing(vma)) {
758 int ret;
759
760 spin_unlock(ptl);
761 mem_cgroup_cancel_charge(page, memcg);
762 put_page(page);
763 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700764 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700765 VM_UFFD_MISSING);
766 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
767 return ret;
768 }
769
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700770 entry = mk_huge_pmd(page, vma->vm_page_prot);
771 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800772 page_add_new_anon_rmap(page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -0700773 mem_cgroup_commit_charge(page, memcg, false);
774 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700775 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800776 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800777 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800778 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800779 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700780 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800781 }
782
David Rientjesaa2e8782012-05-29 15:06:17 -0700783 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800784}
785
Andi Kleencc5d4622011-03-22 16:33:13 -0700786static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800787{
Andi Kleencc5d4622011-03-22 16:33:13 -0700788 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_WAIT)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800789}
790
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800791/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700792static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800793 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700794 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800795{
796 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700797 if (!pmd_none(*pmd))
798 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700799 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800800 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700801 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800802 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800803 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700804 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800805}
806
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800807int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
808 unsigned long address, pmd_t *pmd,
809 unsigned int flags)
810{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800811 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800812 struct page *page;
813 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800814
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700815 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700816 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700817 if (unlikely(anon_vma_prepare(vma)))
818 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700819 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700820 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200821 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700822 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800823 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700824 pgtable_t pgtable;
825 struct page *zero_page;
826 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700827 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700828 pgtable = pte_alloc_one(mm, haddr);
829 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800830 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700831 zero_page = get_huge_zero_page();
832 if (unlikely(!zero_page)) {
833 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700834 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700835 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700836 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800837 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700838 ret = 0;
839 set = false;
840 if (pmd_none(*pmd)) {
841 if (userfaultfd_missing(vma)) {
842 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700843 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700844 VM_UFFD_MISSING);
845 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
846 } else {
847 set_huge_zero_page(pgtable, mm, vma,
848 haddr, pmd,
849 zero_page);
850 spin_unlock(ptl);
851 set = true;
852 }
853 } else
854 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700855 if (!set) {
856 pte_free(mm, pgtable);
857 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800858 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700859 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800860 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800861 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
862 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700863 if (unlikely(!page)) {
864 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700865 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700866 }
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700867 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
868 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800869}
870
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700871static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700872 pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
873{
874 struct mm_struct *mm = vma->vm_mm;
875 pmd_t entry;
876 spinlock_t *ptl;
877
878 ptl = pmd_lock(mm, pmd);
879 if (pmd_none(*pmd)) {
880 entry = pmd_mkhuge(pfn_pmd(pfn, prot));
881 if (write) {
882 entry = pmd_mkyoung(pmd_mkdirty(entry));
883 entry = maybe_pmd_mkwrite(entry, vma);
884 }
885 set_pmd_at(mm, addr, pmd, entry);
886 update_mmu_cache_pmd(vma, addr, pmd);
887 }
888 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700889}
890
891int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
892 pmd_t *pmd, unsigned long pfn, bool write)
893{
894 pgprot_t pgprot = vma->vm_page_prot;
895 /*
896 * If we had pmd_special, we could avoid all these restrictions,
897 * but we need to be consistent with PTEs and architectures that
898 * can't support a 'special' bit.
899 */
900 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
901 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
902 (VM_PFNMAP|VM_MIXEDMAP));
903 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
904 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
905
906 if (addr < vma->vm_start || addr >= vma->vm_end)
907 return VM_FAULT_SIGBUS;
908 if (track_pfn_insert(vma, &pgprot, pfn))
909 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700910 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
911 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700912}
913
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800914int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
915 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
916 struct vm_area_struct *vma)
917{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800918 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800919 struct page *src_page;
920 pmd_t pmd;
921 pgtable_t pgtable;
922 int ret;
923
924 ret = -ENOMEM;
925 pgtable = pte_alloc_one(dst_mm, addr);
926 if (unlikely(!pgtable))
927 goto out;
928
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800929 dst_ptl = pmd_lock(dst_mm, dst_pmd);
930 src_ptl = pmd_lockptr(src_mm, src_pmd);
931 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800932
933 ret = -EAGAIN;
934 pmd = *src_pmd;
935 if (unlikely(!pmd_trans_huge(pmd))) {
936 pte_free(dst_mm, pgtable);
937 goto out_unlock;
938 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800939 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800940 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800941 * under splitting since we don't split the page itself, only pmd to
942 * a page table.
943 */
944 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700945 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800946 /*
947 * get_huge_zero_page() will never allocate a new page here,
948 * since we already have a zero page to copy. It just takes a
949 * reference.
950 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700951 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700952 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700953 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800954 ret = 0;
955 goto out_unlock;
956 }
Mel Gormande466bd2013-12-18 17:08:42 -0800957
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800958 if (unlikely(pmd_trans_splitting(pmd))) {
959 /* split huge page running from under us */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800960 spin_unlock(src_ptl);
961 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800962 pte_free(dst_mm, pgtable);
963
964 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
965 goto out;
966 }
967 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -0800968 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800969 get_page(src_page);
970 page_dup_rmap(src_page);
971 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
972
973 pmdp_set_wrprotect(src_mm, addr, src_pmd);
974 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700975 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800976 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800977 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800978
979 ret = 0;
980out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800981 spin_unlock(src_ptl);
982 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800983out:
984 return ret;
985}
986
Will Deacona1dd4502012-12-11 16:01:27 -0800987void huge_pmd_set_accessed(struct mm_struct *mm,
988 struct vm_area_struct *vma,
989 unsigned long address,
990 pmd_t *pmd, pmd_t orig_pmd,
991 int dirty)
992{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800993 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -0800994 pmd_t entry;
995 unsigned long haddr;
996
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800997 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -0800998 if (unlikely(!pmd_same(*pmd, orig_pmd)))
999 goto unlock;
1000
1001 entry = pmd_mkyoung(orig_pmd);
1002 haddr = address & HPAGE_PMD_MASK;
1003 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1004 update_mmu_cache_pmd(vma, address, pmd);
1005
1006unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001007 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001008}
1009
Hugh Dickins5338a932014-06-23 13:22:05 -07001010/*
1011 * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
1012 * during copy_user_huge_page()'s copy_page_rep(): in the case when
1013 * the source page gets split and a tail freed before copy completes.
1014 * Called under pmd_lock of checked pmd, so safe from splitting itself.
1015 */
1016static void get_user_huge_page(struct page *page)
1017{
1018 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
1019 struct page *endpage = page + HPAGE_PMD_NR;
1020
1021 atomic_add(HPAGE_PMD_NR, &page->_count);
1022 while (++page < endpage)
1023 get_huge_page_tail(page);
1024 } else {
1025 get_page(page);
1026 }
1027}
1028
1029static void put_user_huge_page(struct page *page)
1030{
1031 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
1032 struct page *endpage = page + HPAGE_PMD_NR;
1033
1034 while (page < endpage)
1035 put_page(page++);
1036 } else {
1037 put_page(page);
1038 }
1039}
1040
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001041static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1042 struct vm_area_struct *vma,
1043 unsigned long address,
1044 pmd_t *pmd, pmd_t orig_pmd,
1045 struct page *page,
1046 unsigned long haddr)
1047{
Johannes Weiner00501b52014-08-08 14:19:20 -07001048 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001049 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001050 pgtable_t pgtable;
1051 pmd_t _pmd;
1052 int ret = 0, i;
1053 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001054 unsigned long mmun_start; /* For mmu_notifiers */
1055 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001056
1057 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1058 GFP_KERNEL);
1059 if (unlikely(!pages)) {
1060 ret |= VM_FAULT_OOM;
1061 goto out;
1062 }
1063
1064 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001065 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1066 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001067 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001068 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001069 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
1070 &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001071 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001072 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001073 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001074 memcg = (void *)page_private(pages[i]);
1075 set_page_private(pages[i], 0);
1076 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001077 put_page(pages[i]);
1078 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001079 kfree(pages);
1080 ret |= VM_FAULT_OOM;
1081 goto out;
1082 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001083 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001084 }
1085
1086 for (i = 0; i < HPAGE_PMD_NR; i++) {
1087 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001088 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001089 __SetPageUptodate(pages[i]);
1090 cond_resched();
1091 }
1092
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001093 mmun_start = haddr;
1094 mmun_end = haddr + HPAGE_PMD_SIZE;
1095 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1096
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001097 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1099 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001100 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001101
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001102 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001103 /* leave pmd empty until pte is filled */
1104
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001105 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001106 pmd_populate(mm, &_pmd, pgtable);
1107
1108 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1109 pte_t *pte, entry;
1110 entry = mk_pte(pages[i], vma->vm_page_prot);
1111 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001112 memcg = (void *)page_private(pages[i]);
1113 set_page_private(pages[i], 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001114 page_add_new_anon_rmap(pages[i], vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001115 mem_cgroup_commit_charge(pages[i], memcg, false);
1116 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001117 pte = pte_offset_map(&_pmd, haddr);
1118 VM_BUG_ON(!pte_none(*pte));
1119 set_pte_at(mm, haddr, pte, entry);
1120 pte_unmap(pte);
1121 }
1122 kfree(pages);
1123
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001124 smp_wmb(); /* make pte visible before pmd */
1125 pmd_populate(mm, pmd, pgtable);
1126 page_remove_rmap(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001127 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001129 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1130
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001131 ret |= VM_FAULT_WRITE;
1132 put_page(page);
1133
1134out:
1135 return ret;
1136
1137out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001138 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001139 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001140 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001141 memcg = (void *)page_private(pages[i]);
1142 set_page_private(pages[i], 0);
1143 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001144 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001145 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001146 kfree(pages);
1147 goto out;
1148}
1149
1150int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1151 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1152{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001153 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001154 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001155 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001156 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001157 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001158 unsigned long mmun_start; /* For mmu_notifiers */
1159 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001160 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001161
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001162 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001163 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001164 haddr = address & HPAGE_PMD_MASK;
1165 if (is_huge_zero_pmd(orig_pmd))
1166 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001167 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001168 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1169 goto out_unlock;
1170
1171 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001172 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001173 if (page_mapcount(page) == 1) {
1174 pmd_t entry;
1175 entry = pmd_mkyoung(orig_pmd);
1176 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1177 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001178 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001179 ret |= VM_FAULT_WRITE;
1180 goto out_unlock;
1181 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001182 get_user_huge_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001183 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001184alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001186 !transparent_hugepage_debug_cow()) {
Michal Hocko3b363692015-04-15 16:13:29 -07001187 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1188 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001189 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001190 new_page = NULL;
1191
1192 if (unlikely(!new_page)) {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001193 if (!page) {
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001194 split_huge_page_pmd(vma, address, pmd);
1195 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001196 } else {
1197 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1198 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001199 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001200 split_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001201 ret |= VM_FAULT_FALLBACK;
1202 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001203 put_user_huge_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001204 }
David Rientjes17766dd2013-09-12 15:14:06 -07001205 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001206 goto out;
1207 }
1208
Michal Hocko3b363692015-04-15 16:13:29 -07001209 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001210 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001211 if (page) {
1212 split_huge_page(page);
Hugh Dickins5338a932014-06-23 13:22:05 -07001213 put_user_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001214 } else
1215 split_huge_page_pmd(vma, address, pmd);
1216 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001217 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001218 goto out;
1219 }
1220
David Rientjes17766dd2013-09-12 15:14:06 -07001221 count_vm_event(THP_FAULT_ALLOC);
1222
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001223 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001224 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1225 else
1226 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001227 __SetPageUptodate(new_page);
1228
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001229 mmun_start = haddr;
1230 mmun_end = haddr + HPAGE_PMD_SIZE;
1231 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1232
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001233 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001234 if (page)
Hugh Dickins5338a932014-06-23 13:22:05 -07001235 put_user_huge_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001236 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001237 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -07001238 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001240 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001241 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001243 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1244 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001245 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001246 page_add_new_anon_rmap(new_page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001247 mem_cgroup_commit_charge(new_page, memcg, false);
1248 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001249 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001250 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001251 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001252 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001253 put_huge_zero_page();
1254 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001255 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001256 page_remove_rmap(page);
1257 put_page(page);
1258 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001259 ret |= VM_FAULT_WRITE;
1260 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001261 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001262out_mn:
1263 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1264out:
1265 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001266out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001267 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001268 return ret;
1269}
1270
David Rientjesb676b292012-10-08 16:34:03 -07001271struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001272 unsigned long addr,
1273 pmd_t *pmd,
1274 unsigned int flags)
1275{
David Rientjesb676b292012-10-08 16:34:03 -07001276 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001277 struct page *page = NULL;
1278
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001279 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280
1281 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1282 goto out;
1283
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001284 /* Avoid dumping huge zero page */
1285 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1286 return ERR_PTR(-EFAULT);
1287
Mel Gorman2b4847e2013-12-18 17:08:32 -08001288 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001289 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001290 goto out;
1291
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001292 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001293 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001294 if (flags & FOLL_TOUCH) {
1295 pmd_t _pmd;
1296 /*
1297 * We should set the dirty bit only for FOLL_WRITE but
1298 * for now the dirty bit in the pmd is meaningless.
1299 * And if the dirty bit will become meaningful and
1300 * we'll only set it with FOLL_WRITE, an atomic
1301 * set_bit will be required on the pmd to set the
1302 * young bit, instead of the current set_pmd_at.
1303 */
1304 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001305 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1306 pmd, _pmd, 1))
1307 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001308 }
Kirill A. Shutemov84d33df2015-04-14 15:44:37 -07001309 if ((flags & FOLL_POPULATE) && (vma->vm_flags & VM_LOCKED)) {
David Rientjesb676b292012-10-08 16:34:03 -07001310 if (page->mapping && trylock_page(page)) {
1311 lru_add_drain();
1312 if (page->mapping)
1313 mlock_vma_page(page);
1314 unlock_page(page);
1315 }
1316 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001317 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001318 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001319 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001320 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001321
1322out:
1323 return page;
1324}
1325
Mel Gormand10e63f2012-10-25 14:16:31 +02001326/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001327int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1328 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001329{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001330 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001331 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001332 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001333 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001334 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001335 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001336 bool page_locked;
1337 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001338 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001339 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001340
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001341 /* A PROT_NONE fault should not end up here */
1342 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1343
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001344 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001345 if (unlikely(!pmd_same(pmd, *pmdp)))
1346 goto out_unlock;
1347
Mel Gormande466bd2013-12-18 17:08:42 -08001348 /*
1349 * If there are potential migrations, wait for completion and retry
1350 * without disrupting NUMA hinting information. Do not relock and
1351 * check_same as the page may no longer be mapped.
1352 */
1353 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001354 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001355 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001356 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001357 goto out;
1358 }
1359
Mel Gormand10e63f2012-10-25 14:16:31 +02001360 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001361 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001362 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001363 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001364 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001365 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001366 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001367 flags |= TNF_FAULT_LOCAL;
1368 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001369
Mel Gormanbea66fb2015-03-25 15:55:37 -07001370 /* See similar comment in do_numa_page for explanation */
1371 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001372 flags |= TNF_NO_GROUP;
1373
1374 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001375 * Acquire the page lock to serialise THP migrations but avoid dropping
1376 * page_table_lock if at all possible
1377 */
Mel Gormanb8916632013-10-07 11:28:44 +01001378 page_locked = trylock_page(page);
1379 target_nid = mpol_misplaced(page, vma, haddr);
1380 if (target_nid == -1) {
1381 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001382 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001383 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001384 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001385
Mel Gormande466bd2013-12-18 17:08:42 -08001386 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001387 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001388 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001389 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001390 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001391 goto out;
1392 }
1393
Mel Gorman2b4847e2013-12-18 17:08:32 -08001394 /*
1395 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1396 * to serialises splits
1397 */
Mel Gormanb8916632013-10-07 11:28:44 +01001398 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001399 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001400 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001401
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001402 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001403 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001404 if (unlikely(!pmd_same(pmd, *pmdp))) {
1405 unlock_page(page);
1406 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001407 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001408 goto out_unlock;
1409 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001410
Mel Gormanc3a489c2013-12-18 17:08:38 -08001411 /* Bail if we fail to protect against THP splits for any reason */
1412 if (unlikely(!anon_vma)) {
1413 put_page(page);
1414 page_nid = -1;
1415 goto clear_pmdnuma;
1416 }
1417
Mel Gormana54a4072013-10-07 11:28:46 +01001418 /*
1419 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001420 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001421 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001422 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001423 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001424 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001425 if (migrated) {
1426 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001427 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001428 } else
1429 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001430
Mel Gorman8191acb2013-10-07 11:28:45 +01001431 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001432clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001433 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001434 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001435 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001436 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001437 if (was_writable)
1438 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001439 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001440 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001441 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001442out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001443 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001444
1445out:
1446 if (anon_vma)
1447 page_unlock_anon_vma_read(anon_vma);
1448
Mel Gorman8191acb2013-10-07 11:28:45 +01001449 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001450 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001451
Mel Gormand10e63f2012-10-25 14:16:31 +02001452 return 0;
1453}
1454
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001455int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001456 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001457{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001458 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001459 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001460
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001461 if (__pmd_trans_huge_lock(pmd, vma, &ptl) != 1)
1462 return 0;
1463 /*
1464 * For architectures like ppc64 we look at deposited pgtable
1465 * when calling pmdp_huge_get_and_clear. So do the
1466 * pgtable_trans_huge_withdraw after finishing pmdp related
1467 * operations.
1468 */
1469 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1470 tlb->fullmm);
1471 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1472 if (vma_is_dax(vma)) {
1473 spin_unlock(ptl);
1474 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001475 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001476 } else if (is_huge_zero_pmd(orig_pmd)) {
1477 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1478 atomic_long_dec(&tlb->mm->nr_ptes);
1479 spin_unlock(ptl);
1480 put_huge_zero_page();
1481 } else {
1482 struct page *page = pmd_page(orig_pmd);
1483 page_remove_rmap(page);
1484 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1485 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1486 VM_BUG_ON_PAGE(!PageHead(page), page);
1487 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1488 atomic_long_dec(&tlb->mm->nr_ptes);
1489 spin_unlock(ptl);
1490 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001491 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001492 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001493}
1494
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001495int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1496 unsigned long old_addr,
1497 unsigned long new_addr, unsigned long old_end,
1498 pmd_t *old_pmd, pmd_t *new_pmd)
1499{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001500 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001501 int ret = 0;
1502 pmd_t pmd;
1503
1504 struct mm_struct *mm = vma->vm_mm;
1505
1506 if ((old_addr & ~HPAGE_PMD_MASK) ||
1507 (new_addr & ~HPAGE_PMD_MASK) ||
1508 old_end - old_addr < HPAGE_PMD_SIZE ||
1509 (new_vma->vm_flags & VM_NOHUGEPAGE))
1510 goto out;
1511
1512 /*
1513 * The destination pmd shouldn't be established, free_pgtables()
1514 * should have release it.
1515 */
1516 if (WARN_ON(!pmd_none(*new_pmd))) {
1517 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1518 goto out;
1519 }
1520
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001521 /*
1522 * We don't have to worry about the ordering of src and dst
1523 * ptlocks because exclusive mmap_sem prevents deadlock.
1524 */
1525 ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001526 if (ret == 1) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001527 new_ptl = pmd_lockptr(mm, new_pmd);
1528 if (new_ptl != old_ptl)
1529 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001530 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001531 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001532
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301533 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1534 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001535 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1536 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001537 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301538 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1539 if (new_ptl != old_ptl)
1540 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001541 spin_unlock(old_ptl);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001542 }
1543out:
1544 return ret;
1545}
1546
Mel Gormanf123d742013-10-07 11:28:49 +01001547/*
1548 * Returns
1549 * - 0 if PMD could not be locked
1550 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1551 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1552 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001553int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001554 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001555{
1556 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001557 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001558 int ret = 0;
1559
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001560 if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001561 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001562 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001563 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001564
1565 /*
1566 * Avoid trapping faults against the zero page. The read-only
1567 * data is likely to be read-cached on the local CPU and
1568 * local/remote hits to the zero page are not interesting.
1569 */
1570 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1571 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001572 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001573 }
1574
Mel Gorman10c10452015-02-12 14:58:44 -08001575 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001576 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001577 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001578 if (preserve_write)
1579 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001580 ret = HPAGE_PMD_NR;
1581 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001582 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001583 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001584 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001585 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001586
1587 return ret;
1588}
1589
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001590/*
1591 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1592 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1593 *
1594 * Note that if it returns 1, this routine returns without unlocking page
1595 * table locks. So callers must unlock them.
1596 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001597int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
1598 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001599{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001600 *ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001601 if (likely(pmd_trans_huge(*pmd))) {
1602 if (unlikely(pmd_trans_splitting(*pmd))) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001603 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001604 wait_split_huge_page(vma->anon_vma, pmd);
1605 return -1;
1606 } else {
1607 /* Thp mapped by 'pmd' is stable, so we can
1608 * handle it as it is. */
1609 return 1;
1610 }
1611 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001612 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001613 return 0;
1614}
1615
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001616/*
1617 * This function returns whether a given @page is mapped onto the @address
1618 * in the virtual space of @mm.
1619 *
1620 * When it's true, this function returns *pmd with holding the page table lock
1621 * and passing it back to the caller via @ptl.
1622 * If it's false, returns NULL without holding the page table lock.
1623 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001624pmd_t *page_check_address_pmd(struct page *page,
1625 struct mm_struct *mm,
1626 unsigned long address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001627 enum page_check_address_pmd_flag flag,
1628 spinlock_t **ptl)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001629{
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001630 pgd_t *pgd;
1631 pud_t *pud;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001632 pmd_t *pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001633
1634 if (address & ~HPAGE_PMD_MASK)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001635 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001636
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001637 pgd = pgd_offset(mm, address);
1638 if (!pgd_present(*pgd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001639 return NULL;
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001640 pud = pud_offset(pgd, address);
1641 if (!pud_present(*pud))
1642 return NULL;
1643 pmd = pmd_offset(pud, address);
1644
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001645 *ptl = pmd_lock(mm, pmd);
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001646 if (!pmd_present(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001647 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001648 if (pmd_page(*pmd) != page)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001649 goto unlock;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001650 /*
1651 * split_vma() may create temporary aliased mappings. There is
1652 * no risk as long as all huge pmd are found and have their
1653 * splitting bit set before __split_huge_page_refcount
1654 * runs. Finding the same huge pmd more than once during the
1655 * same rmap walk is not a problem.
1656 */
1657 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1658 pmd_trans_splitting(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001659 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001660 if (pmd_trans_huge(*pmd)) {
1661 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1662 !pmd_trans_splitting(*pmd));
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001663 return pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664 }
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001665unlock:
1666 spin_unlock(*ptl);
1667 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001668}
1669
1670static int __split_huge_page_splitting(struct page *page,
1671 struct vm_area_struct *vma,
1672 unsigned long address)
1673{
1674 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001675 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001676 pmd_t *pmd;
1677 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001678 /* For mmu_notifiers */
1679 const unsigned long mmun_start = address;
1680 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001681
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001682 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001683 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001684 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001685 if (pmd) {
1686 /*
1687 * We can't temporarily set the pmd to null in order
1688 * to split it, the pmd must remain marked huge at all
1689 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001690 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001691 * serialize against split_huge_page*.
1692 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001693 pmdp_splitting_flush(vma, address, pmd);
Joerg Roedel34ee6452014-11-13 13:46:09 +11001694
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001695 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001696 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001697 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001698 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001699
1700 return ret;
1701}
1702
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001703static void __split_huge_page_refcount(struct page *page,
1704 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001705{
1706 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001707 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001708 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001709 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001710
1711 /* prevent PageLRU to go away from under us, and freeze lru stats */
1712 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001713 lruvec = mem_cgroup_page_lruvec(page, zone);
1714
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001715 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001716 /* complete memcg works before add pages to LRU */
1717 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001718
Shaohua Li45676882012-01-12 17:19:18 -08001719 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001720 struct page *page_tail = page + i;
1721
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001722 /* tail_page->_mapcount cannot change */
1723 BUG_ON(page_mapcount(page_tail) < 0);
1724 tail_count += page_mapcount(page_tail);
1725 /* check for overflow */
1726 BUG_ON(tail_count < 0);
1727 BUG_ON(atomic_read(&page_tail->_count) != 0);
1728 /*
1729 * tail_page->_count is zero and not changing from
1730 * under us. But get_page_unless_zero() may be running
1731 * from under us on the tail_page. If we used
1732 * atomic_set() below instead of atomic_add(), we
1733 * would then run atomic_set() concurrently with
1734 * get_page_unless_zero(), and atomic_set() is
1735 * implemented in C not using locked ops. spin_unlock
1736 * on x86 sometime uses locked ops because of PPro
1737 * errata 66, 92, so unless somebody can guarantee
1738 * atomic_set() here would be safe on all archs (and
1739 * not only on x86), it's safer to use atomic_add().
1740 */
1741 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1742 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001743
1744 /* after clearing PageTail the gup refcount can be released */
Waiman Long3a79d522014-08-06 16:05:38 -07001745 smp_mb__after_atomic();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001746
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001747 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001748 page_tail->flags |= (page->flags &
1749 ((1L << PG_referenced) |
1750 (1L << PG_swapbacked) |
1751 (1L << PG_mlocked) |
Kirill A. Shutemove180cf82013-07-31 13:53:39 -07001752 (1L << PG_uptodate) |
1753 (1L << PG_active) |
1754 (1L << PG_unevictable)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001755 page_tail->flags |= (1L << PG_dirty);
1756
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001757 /* clear PageTail before overwriting first_page */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001758 smp_wmb();
1759
1760 /*
1761 * __split_huge_page_splitting() already set the
1762 * splitting bit in all pmd that could map this
1763 * hugepage, that will ensure no CPU can alter the
1764 * mapcount on the head page. The mapcount is only
1765 * accounted in the head page and it has to be
1766 * transferred to all tail pages in the below code. So
1767 * for this code to be safe, the split the mapcount
1768 * can't change. But that doesn't mean userland can't
1769 * keep changing and reading the page contents while
1770 * we transfer the mapcount, so the pmd splitting
1771 * status is achieved setting a reserved bit in the
1772 * pmd, not by clearing the present bit.
1773 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001774 page_tail->_mapcount = page->_mapcount;
1775
1776 BUG_ON(page_tail->mapping);
1777 page_tail->mapping = page->mapping;
1778
Shaohua Li45676882012-01-12 17:19:18 -08001779 page_tail->index = page->index + i;
Peter Zijlstra90572892013-10-07 11:29:20 +01001780 page_cpupid_xchg_last(page_tail, page_cpupid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001781
1782 BUG_ON(!PageAnon(page_tail));
1783 BUG_ON(!PageUptodate(page_tail));
1784 BUG_ON(!PageDirty(page_tail));
1785 BUG_ON(!PageSwapBacked(page_tail));
1786
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001787 lru_add_page_tail(page, page_tail, lruvec, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001788 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001789 atomic_sub(tail_count, &page->_count);
1790 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001791
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001792 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001793
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001794 ClearPageCompound(page);
1795 compound_unlock(page);
1796 spin_unlock_irq(&zone->lru_lock);
1797
1798 for (i = 1; i < HPAGE_PMD_NR; i++) {
1799 struct page *page_tail = page + i;
1800 BUG_ON(page_count(page_tail) <= 0);
1801 /*
1802 * Tail pages may be freed if there wasn't any mapping
1803 * like if add_to_swap() is running on a lru page that
1804 * had its mapping zapped. And freeing these pages
1805 * requires taking the lru_lock so we do the put_page
1806 * of the tail pages after the split is complete.
1807 */
1808 put_page(page_tail);
1809 }
1810
1811 /*
1812 * Only the head page (now become a regular page) is required
1813 * to be pinned by the caller.
1814 */
1815 BUG_ON(page_count(page) <= 0);
1816}
1817
1818static int __split_huge_page_map(struct page *page,
1819 struct vm_area_struct *vma,
1820 unsigned long address)
1821{
1822 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001823 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001824 pmd_t *pmd, _pmd;
1825 int ret = 0, i;
1826 pgtable_t pgtable;
1827 unsigned long haddr;
1828
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001829 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001830 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001831 if (pmd) {
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001832 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001833 pmd_populate(mm, &_pmd, pgtable);
Waiman Longf8303c22014-08-06 16:05:36 -07001834 if (pmd_write(*pmd))
1835 BUG_ON(page_mapcount(page) != 1);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001836
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001837 haddr = address;
1838 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001839 pte_t *pte, entry;
1840 BUG_ON(PageCompound(page+i));
Mel Gormanabc40bd2014-10-02 19:47:42 +01001841 /*
Mel Gorman8a0516e2015-02-12 14:58:22 -08001842 * Note that NUMA hinting access restrictions are not
1843 * transferred to avoid any possibility of altering
1844 * permissions across VMAs.
Mel Gormanabc40bd2014-10-02 19:47:42 +01001845 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001846 entry = mk_pte(page + i, vma->vm_page_prot);
1847 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1848 if (!pmd_write(*pmd))
1849 entry = pte_wrprotect(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001850 if (!pmd_young(*pmd))
1851 entry = pte_mkold(entry);
1852 pte = pte_offset_map(&_pmd, haddr);
1853 BUG_ON(!pte_none(*pte));
1854 set_pte_at(mm, haddr, pte, entry);
1855 pte_unmap(pte);
1856 }
1857
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001858 smp_wmb(); /* make pte visible before pmd */
1859 /*
1860 * Up to this point the pmd is present and huge and
1861 * userland has the whole access to the hugepage
1862 * during the split (which happens in place). If we
1863 * overwrite the pmd with the not-huge version
1864 * pointing to the pte here (which of course we could
1865 * if all CPUs were bug free), userland could trigger
1866 * a small page size TLB miss on the small sized TLB
1867 * while the hugepage TLB entry is still established
1868 * in the huge TLB. Some CPU doesn't like that. See
1869 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1870 * Erratum 383 on page 93. Intel should be safe but is
1871 * also warns that it's only safe if the permission
1872 * and cache attributes of the two entries loaded in
1873 * the two TLB is identical (which should be the case
1874 * here). But it is generally safer to never allow
1875 * small and huge TLB entries for the same virtual
1876 * address to be loaded simultaneously. So instead of
1877 * doing "pmd_populate(); flush_tlb_range();" we first
1878 * mark the current pmd notpresent (atomically because
1879 * here the pmd_trans_huge and pmd_trans_splitting
1880 * must remain set at all times on the pmd until the
1881 * split is complete for this pmd), then we flush the
1882 * SMP TLB and finally we write the non-huge version
1883 * of the pmd entry with pmd_populate.
1884 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001885 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001886 pmd_populate(mm, pmd, pgtable);
1887 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001888 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001889 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001890
1891 return ret;
1892}
1893
Ingo Molnar5a505082012-12-02 19:56:46 +00001894/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001895static void __split_huge_page(struct page *page,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001896 struct anon_vma *anon_vma,
1897 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001898{
1899 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001900 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001901 struct anon_vma_chain *avc;
1902
1903 BUG_ON(!PageHead(page));
1904 BUG_ON(PageTail(page));
1905
1906 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001907 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001908 struct vm_area_struct *vma = avc->vma;
1909 unsigned long addr = vma_address(page, vma);
1910 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001911 mapcount += __split_huge_page_splitting(page, vma, addr);
1912 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001913 /*
1914 * It is critical that new vmas are added to the tail of the
1915 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1916 * and establishes a child pmd before
1917 * __split_huge_page_splitting() freezes the parent pmd (so if
1918 * we fail to prevent copy_huge_pmd() from running until the
1919 * whole __split_huge_page() is complete), we will still see
1920 * the newly established pmd of the child later during the
1921 * walk, to be able to set it as pmd_trans_splitting too.
1922 */
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001923 if (mapcount != page_mapcount(page)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001924 pr_err("mapcount %d page_mapcount %d\n",
1925 mapcount, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001926 BUG();
1927 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001928
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001929 __split_huge_page_refcount(page, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001930
1931 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001932 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001933 struct vm_area_struct *vma = avc->vma;
1934 unsigned long addr = vma_address(page, vma);
1935 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001936 mapcount2 += __split_huge_page_map(page, vma, addr);
1937 }
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001938 if (mapcount != mapcount2) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001939 pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
1940 mapcount, mapcount2, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001941 BUG();
1942 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001943}
1944
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001945/*
1946 * Split a hugepage into normal pages. This doesn't change the position of head
1947 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1948 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1949 * from the hugepage.
1950 * Return 0 if the hugepage is split successfully otherwise return 1.
1951 */
1952int split_huge_page_to_list(struct page *page, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001953{
1954 struct anon_vma *anon_vma;
1955 int ret = 1;
1956
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001957 BUG_ON(is_huge_zero_page(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001958 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001959
1960 /*
1961 * The caller does not necessarily hold an mmap_sem that would prevent
1962 * the anon_vma disappearing so we first we take a reference to it
1963 * and then lock the anon_vma for write. This is similar to
1964 * page_lock_anon_vma_read except the write lock is taken to serialise
1965 * against parallel split or collapse operations.
1966 */
1967 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001968 if (!anon_vma)
1969 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08001970 anon_vma_lock_write(anon_vma);
1971
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001972 ret = 0;
1973 if (!PageCompound(page))
1974 goto out_unlock;
1975
1976 BUG_ON(!PageSwapBacked(page));
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001977 __split_huge_page(page, anon_vma, list);
Andi Kleen81ab4202011-04-14 15:22:06 -07001978 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001979
1980 BUG_ON(PageCompound(page));
1981out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08001982 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08001983 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001984out:
1985 return ret;
1986}
1987
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001988#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001989
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001990int hugepage_madvise(struct vm_area_struct *vma,
1991 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001992{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001993 switch (advice) {
1994 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001995#ifdef CONFIG_S390
1996 /*
1997 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1998 * can't handle this properly after s390_enable_sie, so we simply
1999 * ignore the madvise to prevent qemu from causing a SIGSEGV.
2000 */
2001 if (mm_has_pgste(vma->vm_mm))
2002 return 0;
2003#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002004 /*
2005 * Be somewhat over-protective like KSM for now!
2006 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002007 if (*vm_flags & (VM_HUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002008 return -EINVAL;
2009 *vm_flags &= ~VM_NOHUGEPAGE;
2010 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002011 /*
2012 * If the vma become good for khugepaged to scan,
2013 * register it here without waiting a page fault that
2014 * may not happen any time soon.
2015 */
David Rientjes6d50e602014-10-29 14:50:31 -07002016 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002017 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002018 break;
2019 case MADV_NOHUGEPAGE:
2020 /*
2021 * Be somewhat over-protective like KSM for now!
2022 */
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002023 if (*vm_flags & (VM_NOHUGEPAGE | VM_NO_THP))
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002024 return -EINVAL;
2025 *vm_flags &= ~VM_HUGEPAGE;
2026 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002027 /*
2028 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
2029 * this vma even if we leave the mm registered in khugepaged if
2030 * it got registered before VM_NOHUGEPAGE was set.
2031 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002032 break;
2033 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08002034
2035 return 0;
2036}
2037
Andrea Arcangeliba761492011-01-13 15:46:58 -08002038static int __init khugepaged_slab_init(void)
2039{
2040 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
2041 sizeof(struct mm_slot),
2042 __alignof__(struct mm_slot), 0, NULL);
2043 if (!mm_slot_cache)
2044 return -ENOMEM;
2045
2046 return 0;
2047}
2048
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07002049static void __init khugepaged_slab_exit(void)
2050{
2051 kmem_cache_destroy(mm_slot_cache);
2052}
2053
Andrea Arcangeliba761492011-01-13 15:46:58 -08002054static inline struct mm_slot *alloc_mm_slot(void)
2055{
2056 if (!mm_slot_cache) /* initialization failed */
2057 return NULL;
2058 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
2059}
2060
2061static inline void free_mm_slot(struct mm_slot *mm_slot)
2062{
2063 kmem_cache_free(mm_slot_cache, mm_slot);
2064}
2065
Andrea Arcangeliba761492011-01-13 15:46:58 -08002066static struct mm_slot *get_mm_slot(struct mm_struct *mm)
2067{
2068 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002069
Sasha Levinb67bfe02013-02-27 17:06:00 -08002070 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002071 if (mm == mm_slot->mm)
2072 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002073
Andrea Arcangeliba761492011-01-13 15:46:58 -08002074 return NULL;
2075}
2076
2077static void insert_to_mm_slots_hash(struct mm_struct *mm,
2078 struct mm_slot *mm_slot)
2079{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002080 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002081 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002082}
2083
2084static inline int khugepaged_test_exit(struct mm_struct *mm)
2085{
2086 return atomic_read(&mm->mm_users) == 0;
2087}
2088
2089int __khugepaged_enter(struct mm_struct *mm)
2090{
2091 struct mm_slot *mm_slot;
2092 int wakeup;
2093
2094 mm_slot = alloc_mm_slot();
2095 if (!mm_slot)
2096 return -ENOMEM;
2097
2098 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07002099 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002100 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
2101 free_mm_slot(mm_slot);
2102 return 0;
2103 }
2104
2105 spin_lock(&khugepaged_mm_lock);
2106 insert_to_mm_slots_hash(mm, mm_slot);
2107 /*
2108 * Insert just behind the scanning cursor, to let the area settle
2109 * down a little.
2110 */
2111 wakeup = list_empty(&khugepaged_scan.mm_head);
2112 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
2113 spin_unlock(&khugepaged_mm_lock);
2114
2115 atomic_inc(&mm->mm_count);
2116 if (wakeup)
2117 wake_up_interruptible(&khugepaged_wait);
2118
2119 return 0;
2120}
2121
David Rientjes6d50e602014-10-29 14:50:31 -07002122int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
2123 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002124{
2125 unsigned long hstart, hend;
2126 if (!vma->anon_vma)
2127 /*
2128 * Not yet faulted in so we will register later in the
2129 * page fault if needed.
2130 */
2131 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002132 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002133 /* khugepaged not yet working on file or special mappings */
2134 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07002135 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002136 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2137 hend = vma->vm_end & HPAGE_PMD_MASK;
2138 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07002139 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002140 return 0;
2141}
2142
2143void __khugepaged_exit(struct mm_struct *mm)
2144{
2145 struct mm_slot *mm_slot;
2146 int free = 0;
2147
2148 spin_lock(&khugepaged_mm_lock);
2149 mm_slot = get_mm_slot(mm);
2150 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002151 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002152 list_del(&mm_slot->mm_node);
2153 free = 1;
2154 }
Chris Wrightd788e802011-07-25 17:12:14 -07002155 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002156
2157 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002158 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2159 free_mm_slot(mm_slot);
2160 mmdrop(mm);
2161 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002162 /*
2163 * This is required to serialize against
2164 * khugepaged_test_exit() (which is guaranteed to run
2165 * under mmap sem read mode). Stop here (after we
2166 * return all pagetables will be destroyed) until
2167 * khugepaged has finished working on the pagetables
2168 * under the mmap_sem.
2169 */
2170 down_write(&mm->mmap_sem);
2171 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002172 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002173}
2174
2175static void release_pte_page(struct page *page)
2176{
2177 /* 0 stands for page_is_file_cache(page) == false */
2178 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2179 unlock_page(page);
2180 putback_lru_page(page);
2181}
2182
2183static void release_pte_pages(pte_t *pte, pte_t *_pte)
2184{
2185 while (--_pte >= pte) {
2186 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002187 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002188 release_pte_page(pte_page(pteval));
2189 }
2190}
2191
Andrea Arcangeliba761492011-01-13 15:46:58 -08002192static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2193 unsigned long address,
2194 pte_t *pte)
2195{
2196 struct page *page;
2197 pte_t *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002198 int none_or_zero = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002199 bool referenced = false, writable = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002200 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2201 _pte++, address += PAGE_SIZE) {
2202 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002203 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002204 if (!userfaultfd_armed(vma) &&
2205 ++none_or_zero <= khugepaged_max_ptes_none)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002206 continue;
Bob Liu344aa352012-12-11 16:00:34 -08002207 else
Andrea Arcangeliba761492011-01-13 15:46:58 -08002208 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002209 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002210 if (!pte_present(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002211 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002212 page = vm_normal_page(vma, address, pteval);
Bob Liu344aa352012-12-11 16:00:34 -08002213 if (unlikely(!page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002214 goto out;
Bob Liu344aa352012-12-11 16:00:34 -08002215
Sasha Levin309381fea2014-01-23 15:52:54 -08002216 VM_BUG_ON_PAGE(PageCompound(page), page);
2217 VM_BUG_ON_PAGE(!PageAnon(page), page);
2218 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002219
Andrea Arcangeliba761492011-01-13 15:46:58 -08002220 /*
2221 * We can do it before isolate_lru_page because the
2222 * page can't be freed from under us. NOTE: PG_lock
2223 * is needed to serialize against split_huge_page
2224 * when invoked from the VM.
2225 */
Bob Liu344aa352012-12-11 16:00:34 -08002226 if (!trylock_page(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002227 goto out;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002228
2229 /*
2230 * cannot use mapcount: can't collapse if there's a gup pin.
2231 * The page must only be referenced by the scanned process
2232 * and page swap cache.
2233 */
2234 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2235 unlock_page(page);
2236 goto out;
2237 }
2238 if (pte_write(pteval)) {
2239 writable = true;
2240 } else {
2241 if (PageSwapCache(page) && !reuse_swap_page(page)) {
2242 unlock_page(page);
2243 goto out;
2244 }
2245 /*
2246 * Page is not in the swap cache. It can be collapsed
2247 * into a THP.
2248 */
2249 }
2250
Andrea Arcangeliba761492011-01-13 15:46:58 -08002251 /*
2252 * Isolate the page to avoid collapsing an hugepage
2253 * currently in use by the VM.
2254 */
2255 if (isolate_lru_page(page)) {
2256 unlock_page(page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002257 goto out;
2258 }
2259 /* 0 stands for page_is_file_cache(page) == false */
2260 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002261 VM_BUG_ON_PAGE(!PageLocked(page), page);
2262 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002263
2264 /* If there is no mapped pte young don't collapse the page */
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002265 if (pte_young(pteval) || PageReferenced(page) ||
2266 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002267 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002268 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002269 if (likely(referenced && writable))
Bob Liu344aa352012-12-11 16:00:34 -08002270 return 1;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002271out:
Bob Liu344aa352012-12-11 16:00:34 -08002272 release_pte_pages(pte, _pte);
2273 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002274}
2275
2276static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2277 struct vm_area_struct *vma,
2278 unsigned long address,
2279 spinlock_t *ptl)
2280{
2281 pte_t *_pte;
2282 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2283 pte_t pteval = *_pte;
2284 struct page *src_page;
2285
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002286 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002287 clear_user_highpage(page, address);
2288 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002289 if (is_zero_pfn(pte_pfn(pteval))) {
2290 /*
2291 * ptl mostly unnecessary.
2292 */
2293 spin_lock(ptl);
2294 /*
2295 * paravirt calls inside pte_clear here are
2296 * superfluous.
2297 */
2298 pte_clear(vma->vm_mm, address, _pte);
2299 spin_unlock(ptl);
2300 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002301 } else {
2302 src_page = pte_page(pteval);
2303 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002304 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002305 release_pte_page(src_page);
2306 /*
2307 * ptl mostly unnecessary, but preempt has to
2308 * be disabled to update the per-cpu stats
2309 * inside page_remove_rmap().
2310 */
2311 spin_lock(ptl);
2312 /*
2313 * paravirt calls inside pte_clear here are
2314 * superfluous.
2315 */
2316 pte_clear(vma->vm_mm, address, _pte);
2317 page_remove_rmap(src_page);
2318 spin_unlock(ptl);
2319 free_page_and_swap_cache(src_page);
2320 }
2321
2322 address += PAGE_SIZE;
2323 page++;
2324 }
2325}
2326
Xiao Guangrong26234f32012-10-08 16:29:51 -07002327static void khugepaged_alloc_sleep(void)
2328{
2329 wait_event_freezable_timeout(khugepaged_wait, false,
2330 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2331}
2332
Bob Liu9f1b8682013-11-12 15:07:37 -08002333static int khugepaged_node_load[MAX_NUMNODES];
2334
David Rientjes14a4e212014-08-06 16:07:29 -07002335static bool khugepaged_scan_abort(int nid)
2336{
2337 int i;
2338
2339 /*
2340 * If zone_reclaim_mode is disabled, then no extra effort is made to
2341 * allocate memory locally.
2342 */
2343 if (!zone_reclaim_mode)
2344 return false;
2345
2346 /* If there is a count for this node already, it must be acceptable */
2347 if (khugepaged_node_load[nid])
2348 return false;
2349
2350 for (i = 0; i < MAX_NUMNODES; i++) {
2351 if (!khugepaged_node_load[i])
2352 continue;
2353 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2354 return true;
2355 }
2356 return false;
2357}
2358
Xiao Guangrong26234f32012-10-08 16:29:51 -07002359#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002360static int khugepaged_find_target_node(void)
2361{
2362 static int last_khugepaged_target_node = NUMA_NO_NODE;
2363 int nid, target_node = 0, max_value = 0;
2364
2365 /* find first node with max normal pages hit */
2366 for (nid = 0; nid < MAX_NUMNODES; nid++)
2367 if (khugepaged_node_load[nid] > max_value) {
2368 max_value = khugepaged_node_load[nid];
2369 target_node = nid;
2370 }
2371
2372 /* do some balance if several nodes have the same hit record */
2373 if (target_node <= last_khugepaged_target_node)
2374 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2375 nid++)
2376 if (max_value == khugepaged_node_load[nid]) {
2377 target_node = nid;
2378 break;
2379 }
2380
2381 last_khugepaged_target_node = target_node;
2382 return target_node;
2383}
2384
Xiao Guangrong26234f32012-10-08 16:29:51 -07002385static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2386{
2387 if (IS_ERR(*hpage)) {
2388 if (!*wait)
2389 return false;
2390
2391 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002392 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002393 khugepaged_alloc_sleep();
2394 } else if (*hpage) {
2395 put_page(*hpage);
2396 *hpage = NULL;
2397 }
2398
2399 return true;
2400}
2401
Michal Hocko3b363692015-04-15 16:13:29 -07002402static struct page *
2403khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002404 struct vm_area_struct *vma, unsigned long address,
2405 int node)
2406{
Sasha Levin309381fea2014-01-23 15:52:54 -08002407 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002408
Xiao Guangrong26234f32012-10-08 16:29:51 -07002409 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002410 * Before allocating the hugepage, release the mmap_sem read lock.
2411 * The allocation can take potentially a long time if it involves
2412 * sync compaction, and we do not need to hold the mmap_sem during
2413 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002414 */
2415 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002416
Vlastimil Babka96db8002015-09-08 15:03:50 -07002417 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002418 if (unlikely(!*hpage)) {
2419 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2420 *hpage = ERR_PTR(-ENOMEM);
2421 return NULL;
2422 }
2423
2424 count_vm_event(THP_COLLAPSE_ALLOC);
2425 return *hpage;
2426}
2427#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002428static int khugepaged_find_target_node(void)
2429{
2430 return 0;
2431}
2432
Bob Liu10dc4152013-11-12 15:07:35 -08002433static inline struct page *alloc_hugepage(int defrag)
2434{
2435 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
2436 HPAGE_PMD_ORDER);
2437}
2438
Xiao Guangrong26234f32012-10-08 16:29:51 -07002439static struct page *khugepaged_alloc_hugepage(bool *wait)
2440{
2441 struct page *hpage;
2442
2443 do {
2444 hpage = alloc_hugepage(khugepaged_defrag());
2445 if (!hpage) {
2446 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2447 if (!*wait)
2448 return NULL;
2449
2450 *wait = false;
2451 khugepaged_alloc_sleep();
2452 } else
2453 count_vm_event(THP_COLLAPSE_ALLOC);
2454 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2455
2456 return hpage;
2457}
2458
2459static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2460{
2461 if (!*hpage)
2462 *hpage = khugepaged_alloc_hugepage(wait);
2463
2464 if (unlikely(!*hpage))
2465 return false;
2466
2467 return true;
2468}
2469
Michal Hocko3b363692015-04-15 16:13:29 -07002470static struct page *
2471khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002472 struct vm_area_struct *vma, unsigned long address,
2473 int node)
2474{
2475 up_read(&mm->mmap_sem);
2476 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002477
Xiao Guangrong26234f32012-10-08 16:29:51 -07002478 return *hpage;
2479}
2480#endif
2481
Bob Liufa475e52012-12-11 16:00:39 -08002482static bool hugepage_vma_check(struct vm_area_struct *vma)
2483{
2484 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2485 (vma->vm_flags & VM_NOHUGEPAGE))
2486 return false;
2487
2488 if (!vma->anon_vma || vma->vm_ops)
2489 return false;
2490 if (is_vma_temporary_stack(vma))
2491 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002492 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002493 return true;
2494}
2495
Andrea Arcangeliba761492011-01-13 15:46:58 -08002496static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002497 unsigned long address,
2498 struct page **hpage,
2499 struct vm_area_struct *vma,
2500 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002501{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002502 pmd_t *pmd, _pmd;
2503 pte_t *pte;
2504 pgtable_t pgtable;
2505 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002506 spinlock_t *pmd_ptl, *pte_ptl;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002507 int isolated;
2508 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002509 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002510 unsigned long mmun_start; /* For mmu_notifiers */
2511 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002512 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002513
2514 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002515
Michal Hocko3b363692015-04-15 16:13:29 -07002516 /* Only allocate from the target node */
2517 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2518 __GFP_THISNODE;
2519
Xiao Guangrong26234f32012-10-08 16:29:51 -07002520 /* release the mmap_sem read lock. */
Michal Hocko3b363692015-04-15 16:13:29 -07002521 new_page = khugepaged_alloc_page(hpage, gfp, mm, vma, address, node);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002522 if (!new_page)
Andrea Arcangelice83d212011-01-13 15:47:06 -08002523 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002524
Johannes Weiner00501b52014-08-08 14:19:20 -07002525 if (unlikely(mem_cgroup_try_charge(new_page, mm,
Michal Hocko3b363692015-04-15 16:13:29 -07002526 gfp, &memcg)))
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002527 return;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002528
2529 /*
2530 * Prevent all access to pagetables with the exception of
2531 * gup_fast later hanlded by the ptep_clear_flush and the VM
2532 * handled by the anon_vma lock + PG_lock.
2533 */
2534 down_write(&mm->mmap_sem);
2535 if (unlikely(khugepaged_test_exit(mm)))
2536 goto out;
2537
2538 vma = find_vma(mm, address);
Libina8f531eb2013-09-11 14:20:38 -07002539 if (!vma)
2540 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002541 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2542 hend = vma->vm_end & HPAGE_PMD_MASK;
2543 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2544 goto out;
Bob Liufa475e52012-12-11 16:00:39 -08002545 if (!hugepage_vma_check(vma))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002546 goto out;
Bob Liu62190492012-12-11 16:00:37 -08002547 pmd = mm_find_pmd(mm, address);
2548 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002549 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002550
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002551 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002552
2553 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002554 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002555
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002556 mmun_start = address;
2557 mmun_end = address + HPAGE_PMD_SIZE;
2558 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002559 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002560 /*
2561 * After this gup_fast can't run anymore. This also removes
2562 * any huge TLB entry from the CPU so we won't allow
2563 * huge and small TLB entries for the same virtual address
2564 * to avoid the risk of CPU bugs in that area.
2565 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002566 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002567 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002568 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002569
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002570 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002572 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002573
2574 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002575 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002576 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002577 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002578 /*
2579 * We can only use set_pmd_at when establishing
2580 * hugepmds and never for establishing regular pmds that
2581 * points to regular pagetables. Use pmd_populate for that
2582 */
2583 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002584 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002585 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002586 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002587 }
2588
2589 /*
2590 * All pages are isolated and locked so anon_vma rmap
2591 * can't run anymore.
2592 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002593 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002594
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002595 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002596 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002597 __SetPageUptodate(new_page);
2598 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002599
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002600 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2601 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002602
2603 /*
2604 * spin_lock() below is not the equivalent of smp_wmb(), so
2605 * this is needed to avoid the copy_huge_page writes to become
2606 * visible after the set_pmd_at() write.
2607 */
2608 smp_wmb();
2609
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002610 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002611 BUG_ON(!pmd_none(*pmd));
2612 page_add_new_anon_rmap(new_page, vma, address);
Johannes Weiner00501b52014-08-08 14:19:20 -07002613 mem_cgroup_commit_charge(new_page, memcg, false);
2614 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002615 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002616 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002617 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002618 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002619
2620 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002621
Andrea Arcangeliba761492011-01-13 15:46:58 -08002622 khugepaged_pages_collapsed++;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002623out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002624 up_write(&mm->mmap_sem);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002625 return;
2626
Andrea Arcangelice83d212011-01-13 15:47:06 -08002627out:
Johannes Weiner00501b52014-08-08 14:19:20 -07002628 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002629 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002630}
2631
2632static int khugepaged_scan_pmd(struct mm_struct *mm,
2633 struct vm_area_struct *vma,
2634 unsigned long address,
2635 struct page **hpage)
2636{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002637 pmd_t *pmd;
2638 pte_t *pte, *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002639 int ret = 0, none_or_zero = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002640 struct page *page;
2641 unsigned long _address;
2642 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002643 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002644 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002645
2646 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2647
Bob Liu62190492012-12-11 16:00:37 -08002648 pmd = mm_find_pmd(mm, address);
2649 if (!pmd)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002650 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002651
Bob Liu9f1b8682013-11-12 15:07:37 -08002652 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002653 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2654 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2655 _pte++, _address += PAGE_SIZE) {
2656 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002657 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002658 if (!userfaultfd_armed(vma) &&
2659 ++none_or_zero <= khugepaged_max_ptes_none)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002660 continue;
2661 else
2662 goto out_unmap;
2663 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002664 if (!pte_present(pteval))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002665 goto out_unmap;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002666 if (pte_write(pteval))
2667 writable = true;
2668
Andrea Arcangeliba761492011-01-13 15:46:58 -08002669 page = vm_normal_page(vma, _address, pteval);
2670 if (unlikely(!page))
2671 goto out_unmap;
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002672 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002673 * Record which node the original page is from and save this
2674 * information to khugepaged_node_load[].
2675 * Khupaged will allocate hugepage from the node has the max
2676 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002677 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002678 node = page_to_nid(page);
David Rientjes14a4e212014-08-06 16:07:29 -07002679 if (khugepaged_scan_abort(node))
2680 goto out_unmap;
Bob Liu9f1b8682013-11-12 15:07:37 -08002681 khugepaged_node_load[node]++;
Sasha Levin309381fea2014-01-23 15:52:54 -08002682 VM_BUG_ON_PAGE(PageCompound(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002683 if (!PageLRU(page) || PageLocked(page) || !PageAnon(page))
2684 goto out_unmap;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002685 /*
2686 * cannot use mapcount: can't collapse if there's a gup pin.
2687 * The page must only be referenced by the scanned process
2688 * and page swap cache.
2689 */
2690 if (page_count(page) != 1 + !!PageSwapCache(page))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002691 goto out_unmap;
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002692 if (pte_young(pteval) || PageReferenced(page) ||
2693 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002694 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002695 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002696 if (referenced && writable)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002697 ret = 1;
2698out_unmap:
2699 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002700 if (ret) {
2701 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002702 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002703 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002704 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002705out:
2706 return ret;
2707}
2708
2709static void collect_mm_slot(struct mm_slot *mm_slot)
2710{
2711 struct mm_struct *mm = mm_slot->mm;
2712
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002713 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002714
2715 if (khugepaged_test_exit(mm)) {
2716 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002717 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002718 list_del(&mm_slot->mm_node);
2719
2720 /*
2721 * Not strictly needed because the mm exited already.
2722 *
2723 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2724 */
2725
2726 /* khugepaged_mm_lock actually not necessary for the below */
2727 free_mm_slot(mm_slot);
2728 mmdrop(mm);
2729 }
2730}
2731
2732static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2733 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002734 __releases(&khugepaged_mm_lock)
2735 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002736{
2737 struct mm_slot *mm_slot;
2738 struct mm_struct *mm;
2739 struct vm_area_struct *vma;
2740 int progress = 0;
2741
2742 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002743 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002744
2745 if (khugepaged_scan.mm_slot)
2746 mm_slot = khugepaged_scan.mm_slot;
2747 else {
2748 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2749 struct mm_slot, mm_node);
2750 khugepaged_scan.address = 0;
2751 khugepaged_scan.mm_slot = mm_slot;
2752 }
2753 spin_unlock(&khugepaged_mm_lock);
2754
2755 mm = mm_slot->mm;
2756 down_read(&mm->mmap_sem);
2757 if (unlikely(khugepaged_test_exit(mm)))
2758 vma = NULL;
2759 else
2760 vma = find_vma(mm, khugepaged_scan.address);
2761
2762 progress++;
2763 for (; vma; vma = vma->vm_next) {
2764 unsigned long hstart, hend;
2765
2766 cond_resched();
2767 if (unlikely(khugepaged_test_exit(mm))) {
2768 progress++;
2769 break;
2770 }
Bob Liufa475e52012-12-11 16:00:39 -08002771 if (!hugepage_vma_check(vma)) {
2772skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002773 progress++;
2774 continue;
2775 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002776 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2777 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002778 if (hstart >= hend)
2779 goto skip;
2780 if (khugepaged_scan.address > hend)
2781 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002782 if (khugepaged_scan.address < hstart)
2783 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002784 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002785
2786 while (khugepaged_scan.address < hend) {
2787 int ret;
2788 cond_resched();
2789 if (unlikely(khugepaged_test_exit(mm)))
2790 goto breakouterloop;
2791
2792 VM_BUG_ON(khugepaged_scan.address < hstart ||
2793 khugepaged_scan.address + HPAGE_PMD_SIZE >
2794 hend);
2795 ret = khugepaged_scan_pmd(mm, vma,
2796 khugepaged_scan.address,
2797 hpage);
2798 /* move to next address */
2799 khugepaged_scan.address += HPAGE_PMD_SIZE;
2800 progress += HPAGE_PMD_NR;
2801 if (ret)
2802 /* we released mmap_sem so break loop */
2803 goto breakouterloop_mmap_sem;
2804 if (progress >= pages)
2805 goto breakouterloop;
2806 }
2807 }
2808breakouterloop:
2809 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2810breakouterloop_mmap_sem:
2811
2812 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002813 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002814 /*
2815 * Release the current mm_slot if this mm is about to die, or
2816 * if we scanned all vmas of this mm.
2817 */
2818 if (khugepaged_test_exit(mm) || !vma) {
2819 /*
2820 * Make sure that if mm_users is reaching zero while
2821 * khugepaged runs here, khugepaged_exit will find
2822 * mm_slot not pointing to the exiting mm.
2823 */
2824 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2825 khugepaged_scan.mm_slot = list_entry(
2826 mm_slot->mm_node.next,
2827 struct mm_slot, mm_node);
2828 khugepaged_scan.address = 0;
2829 } else {
2830 khugepaged_scan.mm_slot = NULL;
2831 khugepaged_full_scans++;
2832 }
2833
2834 collect_mm_slot(mm_slot);
2835 }
2836
2837 return progress;
2838}
2839
2840static int khugepaged_has_work(void)
2841{
2842 return !list_empty(&khugepaged_scan.mm_head) &&
2843 khugepaged_enabled();
2844}
2845
2846static int khugepaged_wait_event(void)
2847{
2848 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002849 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002850}
2851
Xiao Guangrongd5169042012-10-08 16:29:48 -07002852static void khugepaged_do_scan(void)
2853{
2854 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002855 unsigned int progress = 0, pass_through_head = 0;
2856 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002857 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002858
2859 barrier(); /* write khugepaged_pages_to_scan to local stack */
2860
2861 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002862 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002863 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002864
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002865 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002866
Jiri Kosinacd092412015-06-24 16:56:07 -07002867 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002868 break;
2869
Andrea Arcangeliba761492011-01-13 15:46:58 -08002870 spin_lock(&khugepaged_mm_lock);
2871 if (!khugepaged_scan.mm_slot)
2872 pass_through_head++;
2873 if (khugepaged_has_work() &&
2874 pass_through_head < 2)
2875 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002876 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002877 else
2878 progress = pages;
2879 spin_unlock(&khugepaged_mm_lock);
2880 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002881
Xiao Guangrongd5169042012-10-08 16:29:48 -07002882 if (!IS_ERR_OR_NULL(hpage))
2883 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002884}
2885
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002886static void khugepaged_wait_work(void)
2887{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002888 if (khugepaged_has_work()) {
2889 if (!khugepaged_scan_sleep_millisecs)
2890 return;
2891
2892 wait_event_freezable_timeout(khugepaged_wait,
2893 kthread_should_stop(),
2894 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2895 return;
2896 }
2897
2898 if (khugepaged_enabled())
2899 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2900}
2901
Andrea Arcangeliba761492011-01-13 15:46:58 -08002902static int khugepaged(void *none)
2903{
2904 struct mm_slot *mm_slot;
2905
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002906 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002907 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002908
Xiao Guangrongb7231782012-10-08 16:29:54 -07002909 while (!kthread_should_stop()) {
2910 khugepaged_do_scan();
2911 khugepaged_wait_work();
2912 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002913
2914 spin_lock(&khugepaged_mm_lock);
2915 mm_slot = khugepaged_scan.mm_slot;
2916 khugepaged_scan.mm_slot = NULL;
2917 if (mm_slot)
2918 collect_mm_slot(mm_slot);
2919 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002920 return 0;
2921}
2922
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002923static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2924 unsigned long haddr, pmd_t *pmd)
2925{
2926 struct mm_struct *mm = vma->vm_mm;
2927 pgtable_t pgtable;
2928 pmd_t _pmd;
2929 int i;
2930
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07002931 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002932 /* leave pmd empty until pte is filled */
2933
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07002934 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002935 pmd_populate(mm, &_pmd, pgtable);
2936
2937 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2938 pte_t *pte, entry;
2939 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2940 entry = pte_mkspecial(entry);
2941 pte = pte_offset_map(&_pmd, haddr);
2942 VM_BUG_ON(!pte_none(*pte));
2943 set_pte_at(mm, haddr, pte, entry);
2944 pte_unmap(pte);
2945 }
2946 smp_wmb(); /* make pte visible before pmd */
2947 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08002948 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002949}
2950
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002951void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
2952 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002953{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002954 spinlock_t *ptl;
Matthew Wilcox4897c762015-09-08 14:58:45 -07002955 struct page *page = NULL;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002956 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002957 unsigned long haddr = address & HPAGE_PMD_MASK;
2958 unsigned long mmun_start; /* For mmu_notifiers */
2959 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08002960
2961 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002962
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002963 mmun_start = haddr;
2964 mmun_end = haddr + HPAGE_PMD_SIZE;
Hugh Dickins750e8162013-10-16 13:47:08 -07002965again:
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002966 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002967 ptl = pmd_lock(mm, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07002968 if (unlikely(!pmd_trans_huge(*pmd)))
2969 goto unlock;
2970 if (vma_is_dax(vma)) {
Kirill A. Shutemov5b701b82015-09-08 14:59:28 -07002971 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2972 if (is_huge_zero_pmd(_pmd))
2973 put_huge_zero_page();
Matthew Wilcox4897c762015-09-08 14:58:45 -07002974 } else if (is_huge_zero_pmd(*pmd)) {
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002975 __split_huge_zero_page_pmd(vma, haddr, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07002976 } else {
2977 page = pmd_page(*pmd);
2978 VM_BUG_ON_PAGE(!page_count(page), page);
2979 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002980 }
Matthew Wilcox4897c762015-09-08 14:58:45 -07002981 unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002982 spin_unlock(ptl);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08002983 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002984
Matthew Wilcox4897c762015-09-08 14:58:45 -07002985 if (!page)
2986 return;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002987
Matthew Wilcox4897c762015-09-08 14:58:45 -07002988 split_huge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002989 put_page(page);
Hugh Dickins750e8162013-10-16 13:47:08 -07002990
2991 /*
2992 * We don't always have down_write of mmap_sem here: a racing
2993 * do_huge_pmd_wp_page() might have copied-on-write to another
2994 * huge page before our split_huge_page() got the anon_vma lock.
2995 */
2996 if (unlikely(pmd_trans_huge(*pmd)))
2997 goto again;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002998}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002999
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003000void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
3001 pmd_t *pmd)
3002{
3003 struct vm_area_struct *vma;
3004
3005 vma = find_vma(mm, address);
3006 BUG_ON(vma == NULL);
3007 split_huge_page_pmd(vma, address, pmd);
3008}
3009
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003010static void split_huge_page_address(struct mm_struct *mm,
3011 unsigned long address)
3012{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003013 pgd_t *pgd;
3014 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003015 pmd_t *pmd;
3016
3017 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
3018
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003019 pgd = pgd_offset(mm, address);
3020 if (!pgd_present(*pgd))
3021 return;
3022
3023 pud = pud_offset(pgd, address);
3024 if (!pud_present(*pud))
3025 return;
3026
3027 pmd = pmd_offset(pud, address);
3028 if (!pmd_present(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003029 return;
3030 /*
3031 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3032 * materialize from under us.
3033 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003034 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003035}
3036
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003037void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003038 unsigned long start,
3039 unsigned long end,
3040 long adjust_next)
3041{
3042 /*
3043 * If the new start address isn't hpage aligned and it could
3044 * previously contain an hugepage: check if we need to split
3045 * an huge pmd.
3046 */
3047 if (start & ~HPAGE_PMD_MASK &&
3048 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3049 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3050 split_huge_page_address(vma->vm_mm, start);
3051
3052 /*
3053 * If the new end address isn't hpage aligned and it could
3054 * previously contain an hugepage: check if we need to split
3055 * an huge pmd.
3056 */
3057 if (end & ~HPAGE_PMD_MASK &&
3058 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3059 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3060 split_huge_page_address(vma->vm_mm, end);
3061
3062 /*
3063 * If we're also updating the vma->vm_next->vm_start, if the new
3064 * vm_next->vm_start isn't page aligned and it could previously
3065 * contain an hugepage: check if we need to split an huge pmd.
3066 */
3067 if (adjust_next > 0) {
3068 struct vm_area_struct *next = vma->vm_next;
3069 unsigned long nstart = next->vm_start;
3070 nstart += adjust_next << PAGE_SHIFT;
3071 if (nstart & ~HPAGE_PMD_MASK &&
3072 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3073 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
3074 split_huge_page_address(next->vm_mm, nstart);
3075 }
3076}