blob: f952f055fdcf571ec82893d388a73fa6b23b284b [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>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070028#include <linux/page_idle.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080029
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080030#include <asm/tlb.h>
31#include <asm/pgalloc.h>
32#include "internal.h"
33
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080034enum scan_result {
35 SCAN_FAIL,
36 SCAN_SUCCEED,
37 SCAN_PMD_NULL,
38 SCAN_EXCEED_NONE_PTE,
39 SCAN_PTE_NON_PRESENT,
40 SCAN_PAGE_RO,
41 SCAN_NO_REFERENCED_PAGE,
42 SCAN_PAGE_NULL,
43 SCAN_SCAN_ABORT,
44 SCAN_PAGE_COUNT,
45 SCAN_PAGE_LRU,
46 SCAN_PAGE_LOCK,
47 SCAN_PAGE_ANON,
48 SCAN_ANY_PROCESS,
49 SCAN_VMA_NULL,
50 SCAN_VMA_CHECK,
51 SCAN_ADDRESS_RANGE,
52 SCAN_SWAP_CACHE_PAGE,
53 SCAN_DEL_PAGE_LRU,
54 SCAN_ALLOC_HUGE_PAGE_FAIL,
55 SCAN_CGROUP_CHARGE_FAIL
56};
57
58#define CREATE_TRACE_POINTS
59#include <trace/events/huge_memory.h>
60
Andrea Arcangeliba761492011-01-13 15:46:58 -080061/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080062 * By default transparent hugepage support is disabled in order that avoid
63 * to risk increase the memory footprint of applications without a guaranteed
64 * benefit. When transparent hugepage support is enabled, is for all mappings,
65 * and khugepaged scans all mappings.
66 * Defrag is invoked by khugepaged hugepage allocations and by page faults
67 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080068 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080069unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080070#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080071 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080072#endif
73#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
74 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
75#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080076 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080077 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
78 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080079
80/* default scan 8*512 pte (or vmas) every 30 second */
81static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
82static unsigned int khugepaged_pages_collapsed;
83static unsigned int khugepaged_full_scans;
84static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
85/* during fragmentation poll the hugepage allocator once every minute */
86static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
87static struct task_struct *khugepaged_thread __read_mostly;
88static DEFINE_MUTEX(khugepaged_mutex);
89static DEFINE_SPINLOCK(khugepaged_mm_lock);
90static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
91/*
92 * default collapse hugepages if there is at least one pte mapped like
93 * it would have happened if the vma was large enough during page
94 * fault.
95 */
96static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
97
98static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -080099static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700100static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800101
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800102#define MM_SLOTS_HASH_BITS 10
103static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
104
Andrea Arcangeliba761492011-01-13 15:46:58 -0800105static struct kmem_cache *mm_slot_cache __read_mostly;
106
107/**
108 * struct mm_slot - hash lookup from mm to mm_slot
109 * @hash: hash collision list
110 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
111 * @mm: the mm that this information is valid for
112 */
113struct mm_slot {
114 struct hlist_node hash;
115 struct list_head mm_node;
116 struct mm_struct *mm;
117};
118
119/**
120 * struct khugepaged_scan - cursor for scanning
121 * @mm_head: the head of the mm list to scan
122 * @mm_slot: the current mm_slot we are scanning
123 * @address: the next address inside that to be scanned
124 *
125 * There is only the one khugepaged_scan instance of this cursor structure.
126 */
127struct khugepaged_scan {
128 struct list_head mm_head;
129 struct mm_slot *mm_slot;
130 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700131};
132static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800133 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
134};
135
Andrea Arcangelif0005652011-01-13 15:47:04 -0800136
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700137static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800138{
139 struct zone *zone;
140 int nr_zones = 0;
141 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800142
Andrea Arcangelif0005652011-01-13 15:47:04 -0800143 for_each_populated_zone(zone)
144 nr_zones++;
145
Mel Gorman974a7862015-11-06 16:28:34 -0800146 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800147 recommended_min = pageblock_nr_pages * nr_zones * 2;
148
149 /*
150 * Make sure that on average at least two pageblocks are almost free
151 * of another type, one for a migratetype to fall back to and a
152 * second to avoid subsequent fallbacks of other types There are 3
153 * MIGRATE_TYPES we care about.
154 */
155 recommended_min += pageblock_nr_pages * nr_zones *
156 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
157
158 /* don't ever allow to reserve more than 5% of the lowmem */
159 recommended_min = min(recommended_min,
160 (unsigned long) nr_free_buffer_pages() / 20);
161 recommended_min <<= (PAGE_SHIFT-10);
162
Han Pingtian42aa83c2014-01-23 15:53:28 -0800163 if (recommended_min > min_free_kbytes) {
164 if (user_min_free_kbytes >= 0)
165 pr_info("raising min_free_kbytes from %d to %lu "
166 "to help transparent hugepage allocations\n",
167 min_free_kbytes, recommended_min);
168
Andrea Arcangelif0005652011-01-13 15:47:04 -0800169 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800170 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800171 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800172}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800173
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700174static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800175{
176 int err = 0;
177 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800178 if (!khugepaged_thread)
179 khugepaged_thread = kthread_run(khugepaged, NULL,
180 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530181 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700182 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800183 err = PTR_ERR(khugepaged_thread);
184 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700185 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800186 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700187
188 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800189 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800190
191 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700192 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700193 kthread_stop(khugepaged_thread);
194 khugepaged_thread = NULL;
195 }
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700196fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800197 return err;
198}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800199
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800200static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800201struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800202
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700203struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800204{
205 struct page *zero_page;
206retry:
207 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700208 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800209
210 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
211 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800212 if (!zero_page) {
213 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700214 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800215 }
216 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800217 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700218 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800219 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700220 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800221 goto retry;
222 }
223
224 /* We take additional reference here. It will be put back by shrinker */
225 atomic_set(&huge_zero_refcount, 2);
226 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700227 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800228}
229
230static void put_huge_zero_page(void)
231{
232 /*
233 * Counter should never go to zero here. Only shrinker can put
234 * last reference.
235 */
236 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
237}
238
Glauber Costa48896462013-08-28 10:18:15 +1000239static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
240 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800241{
Glauber Costa48896462013-08-28 10:18:15 +1000242 /* we can free zero page only if last reference remains */
243 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
244}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800245
Glauber Costa48896462013-08-28 10:18:15 +1000246static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
247 struct shrink_control *sc)
248{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800249 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700250 struct page *zero_page = xchg(&huge_zero_page, NULL);
251 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700252 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000253 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800254 }
255
256 return 0;
257}
258
259static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000260 .count_objects = shrink_huge_zero_page_count,
261 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800262 .seeks = DEFAULT_SEEKS,
263};
264
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800265#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800266
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800267static ssize_t double_flag_show(struct kobject *kobj,
268 struct kobj_attribute *attr, char *buf,
269 enum transparent_hugepage_flag enabled,
270 enum transparent_hugepage_flag req_madv)
271{
272 if (test_bit(enabled, &transparent_hugepage_flags)) {
273 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
274 return sprintf(buf, "[always] madvise never\n");
275 } else if (test_bit(req_madv, &transparent_hugepage_flags))
276 return sprintf(buf, "always [madvise] never\n");
277 else
278 return sprintf(buf, "always madvise [never]\n");
279}
280static ssize_t double_flag_store(struct kobject *kobj,
281 struct kobj_attribute *attr,
282 const char *buf, size_t count,
283 enum transparent_hugepage_flag enabled,
284 enum transparent_hugepage_flag req_madv)
285{
286 if (!memcmp("always", buf,
287 min(sizeof("always")-1, count))) {
288 set_bit(enabled, &transparent_hugepage_flags);
289 clear_bit(req_madv, &transparent_hugepage_flags);
290 } else if (!memcmp("madvise", buf,
291 min(sizeof("madvise")-1, count))) {
292 clear_bit(enabled, &transparent_hugepage_flags);
293 set_bit(req_madv, &transparent_hugepage_flags);
294 } else if (!memcmp("never", buf,
295 min(sizeof("never")-1, count))) {
296 clear_bit(enabled, &transparent_hugepage_flags);
297 clear_bit(req_madv, &transparent_hugepage_flags);
298 } else
299 return -EINVAL;
300
301 return count;
302}
303
304static ssize_t enabled_show(struct kobject *kobj,
305 struct kobj_attribute *attr, char *buf)
306{
307 return double_flag_show(kobj, attr, buf,
308 TRANSPARENT_HUGEPAGE_FLAG,
309 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
310}
311static ssize_t enabled_store(struct kobject *kobj,
312 struct kobj_attribute *attr,
313 const char *buf, size_t count)
314{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800315 ssize_t ret;
316
317 ret = double_flag_store(kobj, attr, buf, count,
318 TRANSPARENT_HUGEPAGE_FLAG,
319 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
320
321 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700322 int err;
323
324 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700325 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700326 mutex_unlock(&khugepaged_mutex);
327
Andrea Arcangeliba761492011-01-13 15:46:58 -0800328 if (err)
329 ret = err;
330 }
331
332 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800333}
334static struct kobj_attribute enabled_attr =
335 __ATTR(enabled, 0644, enabled_show, enabled_store);
336
337static ssize_t single_flag_show(struct kobject *kobj,
338 struct kobj_attribute *attr, char *buf,
339 enum transparent_hugepage_flag flag)
340{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700341 return sprintf(buf, "%d\n",
342 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800343}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700344
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800345static ssize_t single_flag_store(struct kobject *kobj,
346 struct kobj_attribute *attr,
347 const char *buf, size_t count,
348 enum transparent_hugepage_flag flag)
349{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700350 unsigned long value;
351 int ret;
352
353 ret = kstrtoul(buf, 10, &value);
354 if (ret < 0)
355 return ret;
356 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800357 return -EINVAL;
358
Ben Hutchingse27e6152011-04-14 15:22:21 -0700359 if (value)
360 set_bit(flag, &transparent_hugepage_flags);
361 else
362 clear_bit(flag, &transparent_hugepage_flags);
363
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800364 return count;
365}
366
367/*
368 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
369 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
370 * memory just to allocate one more hugepage.
371 */
372static ssize_t defrag_show(struct kobject *kobj,
373 struct kobj_attribute *attr, char *buf)
374{
375 return double_flag_show(kobj, attr, buf,
376 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
377 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
378}
379static ssize_t defrag_store(struct kobject *kobj,
380 struct kobj_attribute *attr,
381 const char *buf, size_t count)
382{
383 return double_flag_store(kobj, attr, buf, count,
384 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
385 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
386}
387static struct kobj_attribute defrag_attr =
388 __ATTR(defrag, 0644, defrag_show, defrag_store);
389
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800390static ssize_t use_zero_page_show(struct kobject *kobj,
391 struct kobj_attribute *attr, char *buf)
392{
393 return single_flag_show(kobj, attr, buf,
394 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
395}
396static ssize_t use_zero_page_store(struct kobject *kobj,
397 struct kobj_attribute *attr, const char *buf, size_t count)
398{
399 return single_flag_store(kobj, attr, buf, count,
400 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
401}
402static struct kobj_attribute use_zero_page_attr =
403 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800404#ifdef CONFIG_DEBUG_VM
405static ssize_t debug_cow_show(struct kobject *kobj,
406 struct kobj_attribute *attr, char *buf)
407{
408 return single_flag_show(kobj, attr, buf,
409 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
410}
411static ssize_t debug_cow_store(struct kobject *kobj,
412 struct kobj_attribute *attr,
413 const char *buf, size_t count)
414{
415 return single_flag_store(kobj, attr, buf, count,
416 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
417}
418static struct kobj_attribute debug_cow_attr =
419 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
420#endif /* CONFIG_DEBUG_VM */
421
422static struct attribute *hugepage_attr[] = {
423 &enabled_attr.attr,
424 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800425 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800426#ifdef CONFIG_DEBUG_VM
427 &debug_cow_attr.attr,
428#endif
429 NULL,
430};
431
432static struct attribute_group hugepage_attr_group = {
433 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800434};
435
436static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
437 struct kobj_attribute *attr,
438 char *buf)
439{
440 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
441}
442
443static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
444 struct kobj_attribute *attr,
445 const char *buf, size_t count)
446{
447 unsigned long msecs;
448 int err;
449
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700450 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800451 if (err || msecs > UINT_MAX)
452 return -EINVAL;
453
454 khugepaged_scan_sleep_millisecs = msecs;
455 wake_up_interruptible(&khugepaged_wait);
456
457 return count;
458}
459static struct kobj_attribute scan_sleep_millisecs_attr =
460 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
461 scan_sleep_millisecs_store);
462
463static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
464 struct kobj_attribute *attr,
465 char *buf)
466{
467 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
468}
469
470static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
471 struct kobj_attribute *attr,
472 const char *buf, size_t count)
473{
474 unsigned long msecs;
475 int err;
476
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700477 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800478 if (err || msecs > UINT_MAX)
479 return -EINVAL;
480
481 khugepaged_alloc_sleep_millisecs = msecs;
482 wake_up_interruptible(&khugepaged_wait);
483
484 return count;
485}
486static struct kobj_attribute alloc_sleep_millisecs_attr =
487 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
488 alloc_sleep_millisecs_store);
489
490static ssize_t pages_to_scan_show(struct kobject *kobj,
491 struct kobj_attribute *attr,
492 char *buf)
493{
494 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
495}
496static ssize_t pages_to_scan_store(struct kobject *kobj,
497 struct kobj_attribute *attr,
498 const char *buf, size_t count)
499{
500 int err;
501 unsigned long pages;
502
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700503 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800504 if (err || !pages || pages > UINT_MAX)
505 return -EINVAL;
506
507 khugepaged_pages_to_scan = pages;
508
509 return count;
510}
511static struct kobj_attribute pages_to_scan_attr =
512 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
513 pages_to_scan_store);
514
515static ssize_t pages_collapsed_show(struct kobject *kobj,
516 struct kobj_attribute *attr,
517 char *buf)
518{
519 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
520}
521static struct kobj_attribute pages_collapsed_attr =
522 __ATTR_RO(pages_collapsed);
523
524static ssize_t full_scans_show(struct kobject *kobj,
525 struct kobj_attribute *attr,
526 char *buf)
527{
528 return sprintf(buf, "%u\n", khugepaged_full_scans);
529}
530static struct kobj_attribute full_scans_attr =
531 __ATTR_RO(full_scans);
532
533static ssize_t khugepaged_defrag_show(struct kobject *kobj,
534 struct kobj_attribute *attr, char *buf)
535{
536 return single_flag_show(kobj, attr, buf,
537 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
538}
539static ssize_t khugepaged_defrag_store(struct kobject *kobj,
540 struct kobj_attribute *attr,
541 const char *buf, size_t count)
542{
543 return single_flag_store(kobj, attr, buf, count,
544 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
545}
546static struct kobj_attribute khugepaged_defrag_attr =
547 __ATTR(defrag, 0644, khugepaged_defrag_show,
548 khugepaged_defrag_store);
549
550/*
551 * max_ptes_none controls if khugepaged should collapse hugepages over
552 * any unmapped ptes in turn potentially increasing the memory
553 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
554 * reduce the available free memory in the system as it
555 * runs. Increasing max_ptes_none will instead potentially reduce the
556 * free memory in the system during the khugepaged scan.
557 */
558static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
559 struct kobj_attribute *attr,
560 char *buf)
561{
562 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
563}
564static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
565 struct kobj_attribute *attr,
566 const char *buf, size_t count)
567{
568 int err;
569 unsigned long max_ptes_none;
570
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700571 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800572 if (err || max_ptes_none > HPAGE_PMD_NR-1)
573 return -EINVAL;
574
575 khugepaged_max_ptes_none = max_ptes_none;
576
577 return count;
578}
579static struct kobj_attribute khugepaged_max_ptes_none_attr =
580 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
581 khugepaged_max_ptes_none_store);
582
583static struct attribute *khugepaged_attr[] = {
584 &khugepaged_defrag_attr.attr,
585 &khugepaged_max_ptes_none_attr.attr,
586 &pages_to_scan_attr.attr,
587 &pages_collapsed_attr.attr,
588 &full_scans_attr.attr,
589 &scan_sleep_millisecs_attr.attr,
590 &alloc_sleep_millisecs_attr.attr,
591 NULL,
592};
593
594static struct attribute_group khugepaged_attr_group = {
595 .attrs = khugepaged_attr,
596 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800597};
Shaohua Li569e5592012-01-12 17:19:11 -0800598
599static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
600{
601 int err;
602
603 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
604 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700605 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800606 return -ENOMEM;
607 }
608
609 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
610 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700611 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800612 goto delete_obj;
613 }
614
615 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
616 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700617 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800618 goto remove_hp_group;
619 }
620
621 return 0;
622
623remove_hp_group:
624 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
625delete_obj:
626 kobject_put(*hugepage_kobj);
627 return err;
628}
629
630static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
631{
632 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
633 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
634 kobject_put(hugepage_kobj);
635}
636#else
637static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
638{
639 return 0;
640}
641
642static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
643{
644}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800645#endif /* CONFIG_SYSFS */
646
647static int __init hugepage_init(void)
648{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800649 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800650 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800651
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800652 if (!has_transparent_hugepage()) {
653 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800654 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800655 }
656
Shaohua Li569e5592012-01-12 17:19:11 -0800657 err = hugepage_init_sysfs(&hugepage_kobj);
658 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700659 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800660
661 err = khugepaged_slab_init();
662 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700663 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800664
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700665 err = register_shrinker(&huge_zero_page_shrinker);
666 if (err)
667 goto err_hzp_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800668
Rik van Riel97562cd2011-01-13 15:47:12 -0800669 /*
670 * By default disable transparent hugepages on smaller systems,
671 * where the extra memory used could hurt more than TLB overhead
672 * is likely to save. The admin can still enable it through /sys.
673 */
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700674 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800675 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700676 return 0;
677 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800678
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700679 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700680 if (err)
681 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800682
Shaohua Li569e5592012-01-12 17:19:11 -0800683 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700684err_khugepaged:
685 unregister_shrinker(&huge_zero_page_shrinker);
686err_hzp_shrinker:
687 khugepaged_slab_exit();
688err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800689 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700690err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800691 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800692}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800693subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800694
695static int __init setup_transparent_hugepage(char *str)
696{
697 int ret = 0;
698 if (!str)
699 goto out;
700 if (!strcmp(str, "always")) {
701 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
702 &transparent_hugepage_flags);
703 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
704 &transparent_hugepage_flags);
705 ret = 1;
706 } else if (!strcmp(str, "madvise")) {
707 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
708 &transparent_hugepage_flags);
709 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
710 &transparent_hugepage_flags);
711 ret = 1;
712 } else if (!strcmp(str, "never")) {
713 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
714 &transparent_hugepage_flags);
715 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
716 &transparent_hugepage_flags);
717 ret = 1;
718 }
719out:
720 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700721 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800722 return ret;
723}
724__setup("transparent_hugepage=", setup_transparent_hugepage);
725
Mel Gormanb32967f2012-11-19 12:35:47 +0000726pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800727{
728 if (likely(vma->vm_flags & VM_WRITE))
729 pmd = pmd_mkwrite(pmd);
730 return pmd;
731}
732
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700733static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800734{
735 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700736 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800737 entry = pmd_mkhuge(entry);
738 return entry;
739}
740
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800741static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
742 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700743 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700744 struct page *page, gfp_t gfp,
745 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800746{
Johannes Weiner00501b52014-08-08 14:19:20 -0700747 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800748 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800749 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700750 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800751
Sasha Levin309381fea2014-01-23 15:52:54 -0800752 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700753
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700754 if (mem_cgroup_try_charge(page, mm, gfp, &memcg)) {
755 put_page(page);
756 count_vm_event(THP_FAULT_FALLBACK);
757 return VM_FAULT_FALLBACK;
758 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800759
Johannes Weiner00501b52014-08-08 14:19:20 -0700760 pgtable = pte_alloc_one(mm, haddr);
761 if (unlikely(!pgtable)) {
762 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700763 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700764 return VM_FAULT_OOM;
765 }
766
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800767 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700768 /*
769 * The memory barrier inside __SetPageUptodate makes sure that
770 * clear_huge_page writes become visible before the set_pmd_at()
771 * write.
772 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800773 __SetPageUptodate(page);
774
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800775 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800776 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800777 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -0700778 mem_cgroup_cancel_charge(page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800779 put_page(page);
780 pte_free(mm, pgtable);
781 } else {
782 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700783
784 /* Deliver the page fault to userland */
785 if (userfaultfd_missing(vma)) {
786 int ret;
787
788 spin_unlock(ptl);
789 mem_cgroup_cancel_charge(page, memcg);
790 put_page(page);
791 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700792 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700793 VM_UFFD_MISSING);
794 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
795 return ret;
796 }
797
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700798 entry = mk_huge_pmd(page, vma->vm_page_prot);
799 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800800 page_add_new_anon_rmap(page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -0700801 mem_cgroup_commit_charge(page, memcg, false);
802 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700803 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800804 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800805 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800806 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800807 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700808 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800809 }
810
David Rientjesaa2e8782012-05-29 15:06:17 -0700811 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800812}
813
Andi Kleencc5d4622011-03-22 16:33:13 -0700814static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800815{
Mel Gorman71baba42015-11-06 16:28:28 -0800816 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800817}
818
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800819/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700820static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800821 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700822 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800823{
824 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700825 if (!pmd_none(*pmd))
826 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700827 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800828 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700829 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800830 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800831 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700832 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800833}
834
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800835int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
836 unsigned long address, pmd_t *pmd,
837 unsigned int flags)
838{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800839 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800840 struct page *page;
841 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800842
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700843 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700844 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700845 if (unlikely(anon_vma_prepare(vma)))
846 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700847 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700848 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200849 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700850 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800851 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700852 pgtable_t pgtable;
853 struct page *zero_page;
854 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700855 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700856 pgtable = pte_alloc_one(mm, haddr);
857 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800858 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700859 zero_page = get_huge_zero_page();
860 if (unlikely(!zero_page)) {
861 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700862 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700863 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700864 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800865 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700866 ret = 0;
867 set = false;
868 if (pmd_none(*pmd)) {
869 if (userfaultfd_missing(vma)) {
870 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700871 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700872 VM_UFFD_MISSING);
873 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
874 } else {
875 set_huge_zero_page(pgtable, mm, vma,
876 haddr, pmd,
877 zero_page);
878 spin_unlock(ptl);
879 set = true;
880 }
881 } else
882 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700883 if (!set) {
884 pte_free(mm, pgtable);
885 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800886 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700887 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800888 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800889 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
890 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700891 if (unlikely(!page)) {
892 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700893 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700894 }
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700895 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
896 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800897}
898
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700899static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700900 pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
901{
902 struct mm_struct *mm = vma->vm_mm;
903 pmd_t entry;
904 spinlock_t *ptl;
905
906 ptl = pmd_lock(mm, pmd);
907 if (pmd_none(*pmd)) {
908 entry = pmd_mkhuge(pfn_pmd(pfn, prot));
909 if (write) {
910 entry = pmd_mkyoung(pmd_mkdirty(entry));
911 entry = maybe_pmd_mkwrite(entry, vma);
912 }
913 set_pmd_at(mm, addr, pmd, entry);
914 update_mmu_cache_pmd(vma, addr, pmd);
915 }
916 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700917}
918
919int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
920 pmd_t *pmd, unsigned long pfn, bool write)
921{
922 pgprot_t pgprot = vma->vm_page_prot;
923 /*
924 * If we had pmd_special, we could avoid all these restrictions,
925 * but we need to be consistent with PTEs and architectures that
926 * can't support a 'special' bit.
927 */
928 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
929 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
930 (VM_PFNMAP|VM_MIXEDMAP));
931 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
932 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
933
934 if (addr < vma->vm_start || addr >= vma->vm_end)
935 return VM_FAULT_SIGBUS;
936 if (track_pfn_insert(vma, &pgprot, pfn))
937 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700938 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
939 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700940}
941
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800942int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
943 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
944 struct vm_area_struct *vma)
945{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800946 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800947 struct page *src_page;
948 pmd_t pmd;
949 pgtable_t pgtable;
950 int ret;
951
952 ret = -ENOMEM;
953 pgtable = pte_alloc_one(dst_mm, addr);
954 if (unlikely(!pgtable))
955 goto out;
956
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800957 dst_ptl = pmd_lock(dst_mm, dst_pmd);
958 src_ptl = pmd_lockptr(src_mm, src_pmd);
959 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800960
961 ret = -EAGAIN;
962 pmd = *src_pmd;
963 if (unlikely(!pmd_trans_huge(pmd))) {
964 pte_free(dst_mm, pgtable);
965 goto out_unlock;
966 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800967 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800968 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800969 * under splitting since we don't split the page itself, only pmd to
970 * a page table.
971 */
972 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700973 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800974 /*
975 * get_huge_zero_page() will never allocate a new page here,
976 * since we already have a zero page to copy. It just takes a
977 * reference.
978 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700979 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700980 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700981 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800982 ret = 0;
983 goto out_unlock;
984 }
Mel Gormande466bd2013-12-18 17:08:42 -0800985
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800986 if (unlikely(pmd_trans_splitting(pmd))) {
987 /* split huge page running from under us */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800988 spin_unlock(src_ptl);
989 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800990 pte_free(dst_mm, pgtable);
991
992 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
993 goto out;
994 }
995 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -0800996 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800997 get_page(src_page);
998 page_dup_rmap(src_page);
999 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1000
1001 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1002 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001003 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001004 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001005 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001006
1007 ret = 0;
1008out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001009 spin_unlock(src_ptl);
1010 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001011out:
1012 return ret;
1013}
1014
Will Deacona1dd4502012-12-11 16:01:27 -08001015void huge_pmd_set_accessed(struct mm_struct *mm,
1016 struct vm_area_struct *vma,
1017 unsigned long address,
1018 pmd_t *pmd, pmd_t orig_pmd,
1019 int dirty)
1020{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001021 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001022 pmd_t entry;
1023 unsigned long haddr;
1024
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001025 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001026 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1027 goto unlock;
1028
1029 entry = pmd_mkyoung(orig_pmd);
1030 haddr = address & HPAGE_PMD_MASK;
1031 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1032 update_mmu_cache_pmd(vma, address, pmd);
1033
1034unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001035 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001036}
1037
Hugh Dickins5338a932014-06-23 13:22:05 -07001038/*
1039 * Save CONFIG_DEBUG_PAGEALLOC from faulting falsely on tail pages
1040 * during copy_user_huge_page()'s copy_page_rep(): in the case when
1041 * the source page gets split and a tail freed before copy completes.
1042 * Called under pmd_lock of checked pmd, so safe from splitting itself.
1043 */
1044static void get_user_huge_page(struct page *page)
1045{
1046 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
1047 struct page *endpage = page + HPAGE_PMD_NR;
1048
1049 atomic_add(HPAGE_PMD_NR, &page->_count);
1050 while (++page < endpage)
1051 get_huge_page_tail(page);
1052 } else {
1053 get_page(page);
1054 }
1055}
1056
1057static void put_user_huge_page(struct page *page)
1058{
1059 if (IS_ENABLED(CONFIG_DEBUG_PAGEALLOC)) {
1060 struct page *endpage = page + HPAGE_PMD_NR;
1061
1062 while (page < endpage)
1063 put_page(page++);
1064 } else {
1065 put_page(page);
1066 }
1067}
1068
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001069static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1070 struct vm_area_struct *vma,
1071 unsigned long address,
1072 pmd_t *pmd, pmd_t orig_pmd,
1073 struct page *page,
1074 unsigned long haddr)
1075{
Johannes Weiner00501b52014-08-08 14:19:20 -07001076 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001077 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001078 pgtable_t pgtable;
1079 pmd_t _pmd;
1080 int ret = 0, i;
1081 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001082 unsigned long mmun_start; /* For mmu_notifiers */
1083 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001084
1085 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1086 GFP_KERNEL);
1087 if (unlikely(!pages)) {
1088 ret |= VM_FAULT_OOM;
1089 goto out;
1090 }
1091
1092 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001093 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1094 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001095 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001096 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001097 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
1098 &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001099 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001100 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001101 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001102 memcg = (void *)page_private(pages[i]);
1103 set_page_private(pages[i], 0);
1104 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001105 put_page(pages[i]);
1106 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001107 kfree(pages);
1108 ret |= VM_FAULT_OOM;
1109 goto out;
1110 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001111 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001112 }
1113
1114 for (i = 0; i < HPAGE_PMD_NR; i++) {
1115 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001116 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001117 __SetPageUptodate(pages[i]);
1118 cond_resched();
1119 }
1120
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001121 mmun_start = haddr;
1122 mmun_end = haddr + HPAGE_PMD_SIZE;
1123 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1124
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001125 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001126 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1127 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001128 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001129
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001130 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001131 /* leave pmd empty until pte is filled */
1132
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001133 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001134 pmd_populate(mm, &_pmd, pgtable);
1135
1136 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1137 pte_t *pte, entry;
1138 entry = mk_pte(pages[i], vma->vm_page_prot);
1139 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001140 memcg = (void *)page_private(pages[i]);
1141 set_page_private(pages[i], 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001142 page_add_new_anon_rmap(pages[i], vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001143 mem_cgroup_commit_charge(pages[i], memcg, false);
1144 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001145 pte = pte_offset_map(&_pmd, haddr);
1146 VM_BUG_ON(!pte_none(*pte));
1147 set_pte_at(mm, haddr, pte, entry);
1148 pte_unmap(pte);
1149 }
1150 kfree(pages);
1151
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001152 smp_wmb(); /* make pte visible before pmd */
1153 pmd_populate(mm, pmd, pgtable);
1154 page_remove_rmap(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001155 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001156
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001157 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1158
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001159 ret |= VM_FAULT_WRITE;
1160 put_page(page);
1161
1162out:
1163 return ret;
1164
1165out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001166 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001167 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001168 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001169 memcg = (void *)page_private(pages[i]);
1170 set_page_private(pages[i], 0);
1171 mem_cgroup_cancel_charge(pages[i], memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001172 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001173 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001174 kfree(pages);
1175 goto out;
1176}
1177
1178int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1179 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1180{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001181 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001182 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001183 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001184 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001186 unsigned long mmun_start; /* For mmu_notifiers */
1187 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001188 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001189
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001190 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001191 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001192 haddr = address & HPAGE_PMD_MASK;
1193 if (is_huge_zero_pmd(orig_pmd))
1194 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001195 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001196 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1197 goto out_unlock;
1198
1199 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001200 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001201 if (page_mapcount(page) == 1) {
1202 pmd_t entry;
1203 entry = pmd_mkyoung(orig_pmd);
1204 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1205 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001206 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 ret |= VM_FAULT_WRITE;
1208 goto out_unlock;
1209 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001210 get_user_huge_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001211 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001212alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001213 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001214 !transparent_hugepage_debug_cow()) {
Michal Hocko3b363692015-04-15 16:13:29 -07001215 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1216 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001217 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 new_page = NULL;
1219
1220 if (unlikely(!new_page)) {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001221 if (!page) {
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001222 split_huge_page_pmd(vma, address, pmd);
1223 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001224 } else {
1225 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1226 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001227 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001228 split_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001229 ret |= VM_FAULT_FALLBACK;
1230 }
Hugh Dickins5338a932014-06-23 13:22:05 -07001231 put_user_huge_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001232 }
David Rientjes17766dd2013-09-12 15:14:06 -07001233 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001234 goto out;
1235 }
1236
Michal Hocko3b363692015-04-15 16:13:29 -07001237 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001238 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001239 if (page) {
1240 split_huge_page(page);
Hugh Dickins5338a932014-06-23 13:22:05 -07001241 put_user_huge_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001242 } else
1243 split_huge_page_pmd(vma, address, pmd);
1244 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001245 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001246 goto out;
1247 }
1248
David Rientjes17766dd2013-09-12 15:14:06 -07001249 count_vm_event(THP_FAULT_ALLOC);
1250
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001251 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001252 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1253 else
1254 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001255 __SetPageUptodate(new_page);
1256
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001257 mmun_start = haddr;
1258 mmun_end = haddr + HPAGE_PMD_SIZE;
1259 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1260
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001261 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001262 if (page)
Hugh Dickins5338a932014-06-23 13:22:05 -07001263 put_user_huge_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001264 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001265 spin_unlock(ptl);
Johannes Weiner00501b52014-08-08 14:19:20 -07001266 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001267 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001268 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001269 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001270 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001271 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1272 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001273 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001274 page_add_new_anon_rmap(new_page, vma, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -07001275 mem_cgroup_commit_charge(new_page, memcg, false);
1276 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001277 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001278 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001279 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001280 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001281 put_huge_zero_page();
1282 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001283 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001284 page_remove_rmap(page);
1285 put_page(page);
1286 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001287 ret |= VM_FAULT_WRITE;
1288 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001289 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001290out_mn:
1291 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1292out:
1293 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001294out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001295 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001296 return ret;
1297}
1298
David Rientjesb676b292012-10-08 16:34:03 -07001299struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001300 unsigned long addr,
1301 pmd_t *pmd,
1302 unsigned int flags)
1303{
David Rientjesb676b292012-10-08 16:34:03 -07001304 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001305 struct page *page = NULL;
1306
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001307 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001308
1309 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1310 goto out;
1311
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001312 /* Avoid dumping huge zero page */
1313 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1314 return ERR_PTR(-EFAULT);
1315
Mel Gorman2b4847e2013-12-18 17:08:32 -08001316 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001317 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001318 goto out;
1319
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001320 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001321 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001322 if (flags & FOLL_TOUCH) {
1323 pmd_t _pmd;
1324 /*
1325 * We should set the dirty bit only for FOLL_WRITE but
1326 * for now the dirty bit in the pmd is meaningless.
1327 * And if the dirty bit will become meaningful and
1328 * we'll only set it with FOLL_WRITE, an atomic
1329 * set_bit will be required on the pmd to set the
1330 * young bit, instead of the current set_pmd_at.
1331 */
1332 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001333 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1334 pmd, _pmd, 1))
1335 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001336 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001337 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
David Rientjesb676b292012-10-08 16:34:03 -07001338 if (page->mapping && trylock_page(page)) {
1339 lru_add_drain();
1340 if (page->mapping)
1341 mlock_vma_page(page);
1342 unlock_page(page);
1343 }
1344 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001345 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001346 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001347 if (flags & FOLL_GET)
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001348 get_page_foll(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001349
1350out:
1351 return page;
1352}
1353
Mel Gormand10e63f2012-10-25 14:16:31 +02001354/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001355int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1356 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001357{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001358 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001359 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001360 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001361 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001362 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001363 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001364 bool page_locked;
1365 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001366 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001367 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001368
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001369 /* A PROT_NONE fault should not end up here */
1370 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1371
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001372 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001373 if (unlikely(!pmd_same(pmd, *pmdp)))
1374 goto out_unlock;
1375
Mel Gormande466bd2013-12-18 17:08:42 -08001376 /*
1377 * If there are potential migrations, wait for completion and retry
1378 * without disrupting NUMA hinting information. Do not relock and
1379 * check_same as the page may no longer be mapped.
1380 */
1381 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001382 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001383 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001384 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001385 goto out;
1386 }
1387
Mel Gormand10e63f2012-10-25 14:16:31 +02001388 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001389 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001390 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001391 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001392 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001393 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001394 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001395 flags |= TNF_FAULT_LOCAL;
1396 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001397
Mel Gormanbea66fb2015-03-25 15:55:37 -07001398 /* See similar comment in do_numa_page for explanation */
1399 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001400 flags |= TNF_NO_GROUP;
1401
1402 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001403 * Acquire the page lock to serialise THP migrations but avoid dropping
1404 * page_table_lock if at all possible
1405 */
Mel Gormanb8916632013-10-07 11:28:44 +01001406 page_locked = trylock_page(page);
1407 target_nid = mpol_misplaced(page, vma, haddr);
1408 if (target_nid == -1) {
1409 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001410 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001411 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001412 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001413
Mel Gormande466bd2013-12-18 17:08:42 -08001414 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001415 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001416 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001417 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001418 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001419 goto out;
1420 }
1421
Mel Gorman2b4847e2013-12-18 17:08:32 -08001422 /*
1423 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1424 * to serialises splits
1425 */
Mel Gormanb8916632013-10-07 11:28:44 +01001426 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001427 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001428 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001429
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001430 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001431 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001432 if (unlikely(!pmd_same(pmd, *pmdp))) {
1433 unlock_page(page);
1434 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001435 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001436 goto out_unlock;
1437 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001438
Mel Gormanc3a489c2013-12-18 17:08:38 -08001439 /* Bail if we fail to protect against THP splits for any reason */
1440 if (unlikely(!anon_vma)) {
1441 put_page(page);
1442 page_nid = -1;
1443 goto clear_pmdnuma;
1444 }
1445
Mel Gormana54a4072013-10-07 11:28:46 +01001446 /*
1447 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001448 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001449 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001450 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001451 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001452 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001453 if (migrated) {
1454 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001455 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001456 } else
1457 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001458
Mel Gorman8191acb2013-10-07 11:28:45 +01001459 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001460clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001461 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001462 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001463 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001464 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001465 if (was_writable)
1466 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001467 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001468 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001469 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001470out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001471 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001472
1473out:
1474 if (anon_vma)
1475 page_unlock_anon_vma_read(anon_vma);
1476
Mel Gorman8191acb2013-10-07 11:28:45 +01001477 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001478 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001479
Mel Gormand10e63f2012-10-25 14:16:31 +02001480 return 0;
1481}
1482
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001483int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001484 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001485{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001486 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001487 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001488
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001489 if (__pmd_trans_huge_lock(pmd, vma, &ptl) != 1)
1490 return 0;
1491 /*
1492 * For architectures like ppc64 we look at deposited pgtable
1493 * when calling pmdp_huge_get_and_clear. So do the
1494 * pgtable_trans_huge_withdraw after finishing pmdp related
1495 * operations.
1496 */
1497 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1498 tlb->fullmm);
1499 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1500 if (vma_is_dax(vma)) {
1501 spin_unlock(ptl);
1502 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001503 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001504 } else if (is_huge_zero_pmd(orig_pmd)) {
1505 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1506 atomic_long_dec(&tlb->mm->nr_ptes);
1507 spin_unlock(ptl);
1508 put_huge_zero_page();
1509 } else {
1510 struct page *page = pmd_page(orig_pmd);
1511 page_remove_rmap(page);
1512 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1513 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1514 VM_BUG_ON_PAGE(!PageHead(page), page);
1515 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1516 atomic_long_dec(&tlb->mm->nr_ptes);
1517 spin_unlock(ptl);
1518 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001519 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001520 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001521}
1522
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001523int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1524 unsigned long old_addr,
1525 unsigned long new_addr, unsigned long old_end,
1526 pmd_t *old_pmd, pmd_t *new_pmd)
1527{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001528 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001529 int ret = 0;
1530 pmd_t pmd;
1531
1532 struct mm_struct *mm = vma->vm_mm;
1533
1534 if ((old_addr & ~HPAGE_PMD_MASK) ||
1535 (new_addr & ~HPAGE_PMD_MASK) ||
1536 old_end - old_addr < HPAGE_PMD_SIZE ||
1537 (new_vma->vm_flags & VM_NOHUGEPAGE))
1538 goto out;
1539
1540 /*
1541 * The destination pmd shouldn't be established, free_pgtables()
1542 * should have release it.
1543 */
1544 if (WARN_ON(!pmd_none(*new_pmd))) {
1545 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1546 goto out;
1547 }
1548
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001549 /*
1550 * We don't have to worry about the ordering of src and dst
1551 * ptlocks because exclusive mmap_sem prevents deadlock.
1552 */
1553 ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001554 if (ret == 1) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001555 new_ptl = pmd_lockptr(mm, new_pmd);
1556 if (new_ptl != old_ptl)
1557 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001558 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001559 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001560
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301561 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1562 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001563 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1564 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001565 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301566 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1567 if (new_ptl != old_ptl)
1568 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001569 spin_unlock(old_ptl);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001570 }
1571out:
1572 return ret;
1573}
1574
Mel Gormanf123d742013-10-07 11:28:49 +01001575/*
1576 * Returns
1577 * - 0 if PMD could not be locked
1578 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1579 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1580 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001581int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001582 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001583{
1584 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001585 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001586 int ret = 0;
1587
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001588 if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001589 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001590 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001591 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001592
1593 /*
1594 * Avoid trapping faults against the zero page. The read-only
1595 * data is likely to be read-cached on the local CPU and
1596 * local/remote hits to the zero page are not interesting.
1597 */
1598 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1599 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001600 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001601 }
1602
Mel Gorman10c10452015-02-12 14:58:44 -08001603 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001604 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001605 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001606 if (preserve_write)
1607 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001608 ret = HPAGE_PMD_NR;
1609 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001610 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001611 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001612 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001613 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001614
1615 return ret;
1616}
1617
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001618/*
1619 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1620 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1621 *
1622 * Note that if it returns 1, this routine returns without unlocking page
1623 * table locks. So callers must unlock them.
1624 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001625int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
1626 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001627{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001628 *ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001629 if (likely(pmd_trans_huge(*pmd))) {
1630 if (unlikely(pmd_trans_splitting(*pmd))) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001631 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001632 wait_split_huge_page(vma->anon_vma, pmd);
1633 return -1;
1634 } else {
1635 /* Thp mapped by 'pmd' is stable, so we can
1636 * handle it as it is. */
1637 return 1;
1638 }
1639 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001640 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001641 return 0;
1642}
1643
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001644/*
1645 * This function returns whether a given @page is mapped onto the @address
1646 * in the virtual space of @mm.
1647 *
1648 * When it's true, this function returns *pmd with holding the page table lock
1649 * and passing it back to the caller via @ptl.
1650 * If it's false, returns NULL without holding the page table lock.
1651 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001652pmd_t *page_check_address_pmd(struct page *page,
1653 struct mm_struct *mm,
1654 unsigned long address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001655 enum page_check_address_pmd_flag flag,
1656 spinlock_t **ptl)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001657{
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001658 pgd_t *pgd;
1659 pud_t *pud;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001660 pmd_t *pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001661
1662 if (address & ~HPAGE_PMD_MASK)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001663 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001665 pgd = pgd_offset(mm, address);
1666 if (!pgd_present(*pgd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001667 return NULL;
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001668 pud = pud_offset(pgd, address);
1669 if (!pud_present(*pud))
1670 return NULL;
1671 pmd = pmd_offset(pud, address);
1672
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001673 *ptl = pmd_lock(mm, pmd);
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001674 if (!pmd_present(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001675 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001676 if (pmd_page(*pmd) != page)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001677 goto unlock;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001678 /*
1679 * split_vma() may create temporary aliased mappings. There is
1680 * no risk as long as all huge pmd are found and have their
1681 * splitting bit set before __split_huge_page_refcount
1682 * runs. Finding the same huge pmd more than once during the
1683 * same rmap walk is not a problem.
1684 */
1685 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1686 pmd_trans_splitting(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001687 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001688 if (pmd_trans_huge(*pmd)) {
1689 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1690 !pmd_trans_splitting(*pmd));
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001691 return pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001692 }
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001693unlock:
1694 spin_unlock(*ptl);
1695 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001696}
1697
1698static int __split_huge_page_splitting(struct page *page,
1699 struct vm_area_struct *vma,
1700 unsigned long address)
1701{
1702 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001703 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001704 pmd_t *pmd;
1705 int ret = 0;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001706 /* For mmu_notifiers */
1707 const unsigned long mmun_start = address;
1708 const unsigned long mmun_end = address + HPAGE_PMD_SIZE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001709
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001710 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001711 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001712 PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001713 if (pmd) {
1714 /*
1715 * We can't temporarily set the pmd to null in order
1716 * to split it, the pmd must remain marked huge at all
1717 * times or the VM won't take the pmd_trans_huge paths
Ingo Molnar5a505082012-12-02 19:56:46 +00001718 * and it won't wait on the anon_vma->root->rwsem to
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001719 * serialize against split_huge_page*.
1720 */
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001721 pmdp_splitting_flush(vma, address, pmd);
Joerg Roedel34ee6452014-11-13 13:46:09 +11001722
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001723 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001724 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001725 }
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001726 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001727
1728 return ret;
1729}
1730
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001731static void __split_huge_page_refcount(struct page *page,
1732 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001733{
1734 int i;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001735 struct zone *zone = page_zone(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001736 struct lruvec *lruvec;
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001737 int tail_count = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001738
1739 /* prevent PageLRU to go away from under us, and freeze lru stats */
1740 spin_lock_irq(&zone->lru_lock);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001741 lruvec = mem_cgroup_page_lruvec(page, zone);
1742
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001743 compound_lock(page);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08001744 /* complete memcg works before add pages to LRU */
1745 mem_cgroup_split_huge_fixup(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001746
Shaohua Li45676882012-01-12 17:19:18 -08001747 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001748 struct page *page_tail = page + i;
1749
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001750 /* tail_page->_mapcount cannot change */
1751 BUG_ON(page_mapcount(page_tail) < 0);
1752 tail_count += page_mapcount(page_tail);
1753 /* check for overflow */
1754 BUG_ON(tail_count < 0);
1755 BUG_ON(atomic_read(&page_tail->_count) != 0);
1756 /*
1757 * tail_page->_count is zero and not changing from
1758 * under us. But get_page_unless_zero() may be running
1759 * from under us on the tail_page. If we used
1760 * atomic_set() below instead of atomic_add(), we
1761 * would then run atomic_set() concurrently with
1762 * get_page_unless_zero(), and atomic_set() is
1763 * implemented in C not using locked ops. spin_unlock
1764 * on x86 sometime uses locked ops because of PPro
1765 * errata 66, 92, so unless somebody can guarantee
1766 * atomic_set() here would be safe on all archs (and
1767 * not only on x86), it's safer to use atomic_add().
1768 */
1769 atomic_add(page_mapcount(page) + page_mapcount(page_tail) + 1,
1770 &page_tail->_count);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001771
1772 /* after clearing PageTail the gup refcount can be released */
Waiman Long3a79d522014-08-06 16:05:38 -07001773 smp_mb__after_atomic();
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001774
Naoya Horiguchif4c18e62015-08-06 15:47:08 -07001775 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001776 page_tail->flags |= (page->flags &
1777 ((1L << PG_referenced) |
1778 (1L << PG_swapbacked) |
1779 (1L << PG_mlocked) |
Kirill A. Shutemove180cf82013-07-31 13:53:39 -07001780 (1L << PG_uptodate) |
1781 (1L << PG_active) |
1782 (1L << PG_unevictable)));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001783 page_tail->flags |= (1L << PG_dirty);
1784
Kirill A. Shutemov1d798ca2015-11-06 16:29:54 -08001785 clear_compound_head(page_tail);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001786
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001787 if (page_is_young(page))
1788 set_page_young(page_tail);
1789 if (page_is_idle(page))
1790 set_page_idle(page_tail);
1791
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001792 /*
1793 * __split_huge_page_splitting() already set the
1794 * splitting bit in all pmd that could map this
1795 * hugepage, that will ensure no CPU can alter the
1796 * mapcount on the head page. The mapcount is only
1797 * accounted in the head page and it has to be
1798 * transferred to all tail pages in the below code. So
1799 * for this code to be safe, the split the mapcount
1800 * can't change. But that doesn't mean userland can't
1801 * keep changing and reading the page contents while
1802 * we transfer the mapcount, so the pmd splitting
1803 * status is achieved setting a reserved bit in the
1804 * pmd, not by clearing the present bit.
1805 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001806 page_tail->_mapcount = page->_mapcount;
1807
1808 BUG_ON(page_tail->mapping);
1809 page_tail->mapping = page->mapping;
1810
Shaohua Li45676882012-01-12 17:19:18 -08001811 page_tail->index = page->index + i;
Peter Zijlstra90572892013-10-07 11:29:20 +01001812 page_cpupid_xchg_last(page_tail, page_cpupid_last(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001813
1814 BUG_ON(!PageAnon(page_tail));
1815 BUG_ON(!PageUptodate(page_tail));
1816 BUG_ON(!PageDirty(page_tail));
1817 BUG_ON(!PageSwapBacked(page_tail));
1818
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001819 lru_add_page_tail(page, page_tail, lruvec, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001820 }
Andrea Arcangeli70b50f92011-11-02 13:36:59 -07001821 atomic_sub(tail_count, &page->_count);
1822 BUG_ON(atomic_read(&page->_count) <= 0);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001823
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001824 __mod_zone_page_state(zone, NR_ANON_TRANSPARENT_HUGEPAGES, -1);
Andrea Arcangeli79134172011-01-13 15:46:58 -08001825
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001826 ClearPageCompound(page);
1827 compound_unlock(page);
1828 spin_unlock_irq(&zone->lru_lock);
1829
1830 for (i = 1; i < HPAGE_PMD_NR; i++) {
1831 struct page *page_tail = page + i;
1832 BUG_ON(page_count(page_tail) <= 0);
1833 /*
1834 * Tail pages may be freed if there wasn't any mapping
1835 * like if add_to_swap() is running on a lru page that
1836 * had its mapping zapped. And freeing these pages
1837 * requires taking the lru_lock so we do the put_page
1838 * of the tail pages after the split is complete.
1839 */
1840 put_page(page_tail);
1841 }
1842
1843 /*
1844 * Only the head page (now become a regular page) is required
1845 * to be pinned by the caller.
1846 */
1847 BUG_ON(page_count(page) <= 0);
1848}
1849
1850static int __split_huge_page_map(struct page *page,
1851 struct vm_area_struct *vma,
1852 unsigned long address)
1853{
1854 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001855 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001856 pmd_t *pmd, _pmd;
1857 int ret = 0, i;
1858 pgtable_t pgtable;
1859 unsigned long haddr;
1860
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001861 pmd = page_check_address_pmd(page, mm, address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001862 PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG, &ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001863 if (pmd) {
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001864 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001865 pmd_populate(mm, &_pmd, pgtable);
Waiman Longf8303c22014-08-06 16:05:36 -07001866 if (pmd_write(*pmd))
1867 BUG_ON(page_mapcount(page) != 1);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001868
Gerald Schaefere3ebcf642012-10-08 16:30:07 -07001869 haddr = address;
1870 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001871 pte_t *pte, entry;
1872 BUG_ON(PageCompound(page+i));
Mel Gormanabc40bd2014-10-02 19:47:42 +01001873 /*
Mel Gorman8a0516e2015-02-12 14:58:22 -08001874 * Note that NUMA hinting access restrictions are not
1875 * transferred to avoid any possibility of altering
1876 * permissions across VMAs.
Mel Gormanabc40bd2014-10-02 19:47:42 +01001877 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001878 entry = mk_pte(page + i, vma->vm_page_prot);
1879 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1880 if (!pmd_write(*pmd))
1881 entry = pte_wrprotect(entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001882 if (!pmd_young(*pmd))
1883 entry = pte_mkold(entry);
1884 pte = pte_offset_map(&_pmd, haddr);
1885 BUG_ON(!pte_none(*pte));
1886 set_pte_at(mm, haddr, pte, entry);
1887 pte_unmap(pte);
1888 }
1889
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001890 smp_wmb(); /* make pte visible before pmd */
1891 /*
1892 * Up to this point the pmd is present and huge and
1893 * userland has the whole access to the hugepage
1894 * during the split (which happens in place). If we
1895 * overwrite the pmd with the not-huge version
1896 * pointing to the pte here (which of course we could
1897 * if all CPUs were bug free), userland could trigger
1898 * a small page size TLB miss on the small sized TLB
1899 * while the hugepage TLB entry is still established
1900 * in the huge TLB. Some CPU doesn't like that. See
1901 * http://support.amd.com/us/Processor_TechDocs/41322.pdf,
1902 * Erratum 383 on page 93. Intel should be safe but is
1903 * also warns that it's only safe if the permission
1904 * and cache attributes of the two entries loaded in
1905 * the two TLB is identical (which should be the case
1906 * here). But it is generally safer to never allow
1907 * small and huge TLB entries for the same virtual
1908 * address to be loaded simultaneously. So instead of
Vineet Gupta12ebc152015-02-20 10:36:28 +05301909 * doing "pmd_populate(); flush_pmd_tlb_range();" we first
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001910 * mark the current pmd notpresent (atomically because
1911 * here the pmd_trans_huge and pmd_trans_splitting
1912 * must remain set at all times on the pmd until the
1913 * split is complete for this pmd), then we flush the
1914 * SMP TLB and finally we write the non-huge version
1915 * of the pmd entry with pmd_populate.
1916 */
Gerald Schaefer46dcde72012-10-08 16:30:09 -07001917 pmdp_invalidate(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001918 pmd_populate(mm, pmd, pgtable);
1919 ret = 1;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001920 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001921 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001922
1923 return ret;
1924}
1925
Ingo Molnar5a505082012-12-02 19:56:46 +00001926/* must be called with anon_vma->root->rwsem held */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001927static void __split_huge_page(struct page *page,
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001928 struct anon_vma *anon_vma,
1929 struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001930{
1931 int mapcount, mapcount2;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001932 pgoff_t pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001933 struct anon_vma_chain *avc;
1934
1935 BUG_ON(!PageHead(page));
1936 BUG_ON(PageTail(page));
1937
1938 mapcount = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001939 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001940 struct vm_area_struct *vma = avc->vma;
1941 unsigned long addr = vma_address(page, vma);
1942 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001943 mapcount += __split_huge_page_splitting(page, vma, addr);
1944 }
Andrea Arcangeli05759d32011-01-13 15:46:53 -08001945 /*
1946 * It is critical that new vmas are added to the tail of the
1947 * anon_vma list. This guarantes that if copy_huge_pmd() runs
1948 * and establishes a child pmd before
1949 * __split_huge_page_splitting() freezes the parent pmd (so if
1950 * we fail to prevent copy_huge_pmd() from running until the
1951 * whole __split_huge_page() is complete), we will still see
1952 * the newly established pmd of the child later during the
1953 * walk, to be able to set it as pmd_trans_splitting too.
1954 */
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001955 if (mapcount != page_mapcount(page)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001956 pr_err("mapcount %d page_mapcount %d\n",
1957 mapcount, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001958 BUG();
1959 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001960
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001961 __split_huge_page_refcount(page, list);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001962
1963 mapcount2 = 0;
Michel Lespinassebf181b92012-10-08 16:31:39 -07001964 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001965 struct vm_area_struct *vma = avc->vma;
1966 unsigned long addr = vma_address(page, vma);
1967 BUG_ON(is_vma_temporary_stack(vma));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001968 mapcount2 += __split_huge_page_map(page, vma, addr);
1969 }
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001970 if (mapcount != mapcount2) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -07001971 pr_err("mapcount %d mapcount2 %d page_mapcount %d\n",
1972 mapcount, mapcount2, page_mapcount(page));
Kirill A. Shutemovff9e43e2014-06-04 16:06:57 -07001973 BUG();
1974 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001975}
1976
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07001977/*
1978 * Split a hugepage into normal pages. This doesn't change the position of head
1979 * page. If @list is null, tail pages will be added to LRU list, otherwise, to
1980 * @list. Both head page and tail pages will inherit mapping, flags, and so on
1981 * from the hugepage.
1982 * Return 0 if the hugepage is split successfully otherwise return 1.
1983 */
1984int split_huge_page_to_list(struct page *page, struct list_head *list)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001985{
1986 struct anon_vma *anon_vma;
1987 int ret = 1;
1988
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001989 BUG_ON(is_huge_zero_page(page));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001990 BUG_ON(!PageAnon(page));
Mel Gorman062f1af2013-01-11 14:32:02 -08001991
1992 /*
1993 * The caller does not necessarily hold an mmap_sem that would prevent
1994 * the anon_vma disappearing so we first we take a reference to it
1995 * and then lock the anon_vma for write. This is similar to
1996 * page_lock_anon_vma_read except the write lock is taken to serialise
1997 * against parallel split or collapse operations.
1998 */
1999 anon_vma = page_get_anon_vma(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002000 if (!anon_vma)
2001 goto out;
Mel Gorman062f1af2013-01-11 14:32:02 -08002002 anon_vma_lock_write(anon_vma);
2003
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002004 ret = 0;
2005 if (!PageCompound(page))
2006 goto out_unlock;
2007
2008 BUG_ON(!PageSwapBacked(page));
Shaohua Li5bc7b8a2013-04-29 15:08:36 -07002009 __split_huge_page(page, anon_vma, list);
Andi Kleen81ab4202011-04-14 15:22:06 -07002010 count_vm_event(THP_SPLIT);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002011
2012 BUG_ON(PageCompound(page));
2013out_unlock:
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002014 anon_vma_unlock_write(anon_vma);
Mel Gorman062f1af2013-01-11 14:32:02 -08002015 put_anon_vma(anon_vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002016out:
2017 return ret;
2018}
2019
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08002020#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002021
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002022int hugepage_madvise(struct vm_area_struct *vma,
2023 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08002024{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002025 switch (advice) {
2026 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07002027#ifdef CONFIG_S390
2028 /*
2029 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
2030 * can't handle this properly after s390_enable_sie, so we simply
2031 * ignore the madvise to prevent qemu from causing a SIGSEGV.
2032 */
2033 if (mm_has_pgste(vma->vm_mm))
2034 return 0;
2035#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002036 /*
2037 * Be somewhat over-protective like KSM for now!
2038 */
Jason J. Herne1a763612015-11-20 15:57:04 -08002039 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002040 return -EINVAL;
2041 *vm_flags &= ~VM_NOHUGEPAGE;
2042 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002043 /*
2044 * If the vma become good for khugepaged to scan,
2045 * register it here without waiting a page fault that
2046 * may not happen any time soon.
2047 */
David Rientjes6d50e602014-10-29 14:50:31 -07002048 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002049 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002050 break;
2051 case MADV_NOHUGEPAGE:
2052 /*
2053 * Be somewhat over-protective like KSM for now!
2054 */
Jason J. Herne1a763612015-11-20 15:57:04 -08002055 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002056 return -EINVAL;
2057 *vm_flags &= ~VM_HUGEPAGE;
2058 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08002059 /*
2060 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
2061 * this vma even if we leave the mm registered in khugepaged if
2062 * it got registered before VM_NOHUGEPAGE was set.
2063 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08002064 break;
2065 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08002066
2067 return 0;
2068}
2069
Andrea Arcangeliba761492011-01-13 15:46:58 -08002070static int __init khugepaged_slab_init(void)
2071{
2072 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
2073 sizeof(struct mm_slot),
2074 __alignof__(struct mm_slot), 0, NULL);
2075 if (!mm_slot_cache)
2076 return -ENOMEM;
2077
2078 return 0;
2079}
2080
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07002081static void __init khugepaged_slab_exit(void)
2082{
2083 kmem_cache_destroy(mm_slot_cache);
2084}
2085
Andrea Arcangeliba761492011-01-13 15:46:58 -08002086static inline struct mm_slot *alloc_mm_slot(void)
2087{
2088 if (!mm_slot_cache) /* initialization failed */
2089 return NULL;
2090 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
2091}
2092
2093static inline void free_mm_slot(struct mm_slot *mm_slot)
2094{
2095 kmem_cache_free(mm_slot_cache, mm_slot);
2096}
2097
Andrea Arcangeliba761492011-01-13 15:46:58 -08002098static struct mm_slot *get_mm_slot(struct mm_struct *mm)
2099{
2100 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002101
Sasha Levinb67bfe02013-02-27 17:06:00 -08002102 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002103 if (mm == mm_slot->mm)
2104 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002105
Andrea Arcangeliba761492011-01-13 15:46:58 -08002106 return NULL;
2107}
2108
2109static void insert_to_mm_slots_hash(struct mm_struct *mm,
2110 struct mm_slot *mm_slot)
2111{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002113 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002114}
2115
2116static inline int khugepaged_test_exit(struct mm_struct *mm)
2117{
2118 return atomic_read(&mm->mm_users) == 0;
2119}
2120
2121int __khugepaged_enter(struct mm_struct *mm)
2122{
2123 struct mm_slot *mm_slot;
2124 int wakeup;
2125
2126 mm_slot = alloc_mm_slot();
2127 if (!mm_slot)
2128 return -ENOMEM;
2129
2130 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07002131 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002132 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
2133 free_mm_slot(mm_slot);
2134 return 0;
2135 }
2136
2137 spin_lock(&khugepaged_mm_lock);
2138 insert_to_mm_slots_hash(mm, mm_slot);
2139 /*
2140 * Insert just behind the scanning cursor, to let the area settle
2141 * down a little.
2142 */
2143 wakeup = list_empty(&khugepaged_scan.mm_head);
2144 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
2145 spin_unlock(&khugepaged_mm_lock);
2146
2147 atomic_inc(&mm->mm_count);
2148 if (wakeup)
2149 wake_up_interruptible(&khugepaged_wait);
2150
2151 return 0;
2152}
2153
David Rientjes6d50e602014-10-29 14:50:31 -07002154int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
2155 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002156{
2157 unsigned long hstart, hend;
2158 if (!vma->anon_vma)
2159 /*
2160 * Not yet faulted in so we will register later in the
2161 * page fault if needed.
2162 */
2163 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07002164 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002165 /* khugepaged not yet working on file or special mappings */
2166 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07002167 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002168 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2169 hend = vma->vm_end & HPAGE_PMD_MASK;
2170 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07002171 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002172 return 0;
2173}
2174
2175void __khugepaged_exit(struct mm_struct *mm)
2176{
2177 struct mm_slot *mm_slot;
2178 int free = 0;
2179
2180 spin_lock(&khugepaged_mm_lock);
2181 mm_slot = get_mm_slot(mm);
2182 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002183 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002184 list_del(&mm_slot->mm_node);
2185 free = 1;
2186 }
Chris Wrightd788e802011-07-25 17:12:14 -07002187 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002188
2189 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002190 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2191 free_mm_slot(mm_slot);
2192 mmdrop(mm);
2193 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002194 /*
2195 * This is required to serialize against
2196 * khugepaged_test_exit() (which is guaranteed to run
2197 * under mmap sem read mode). Stop here (after we
2198 * return all pagetables will be destroyed) until
2199 * khugepaged has finished working on the pagetables
2200 * under the mmap_sem.
2201 */
2202 down_write(&mm->mmap_sem);
2203 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002204 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002205}
2206
2207static void release_pte_page(struct page *page)
2208{
2209 /* 0 stands for page_is_file_cache(page) == false */
2210 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2211 unlock_page(page);
2212 putback_lru_page(page);
2213}
2214
2215static void release_pte_pages(pte_t *pte, pte_t *_pte)
2216{
2217 while (--_pte >= pte) {
2218 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002219 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002220 release_pte_page(pte_page(pteval));
2221 }
2222}
2223
Andrea Arcangeliba761492011-01-13 15:46:58 -08002224static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2225 unsigned long address,
2226 pte_t *pte)
2227{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002228 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002229 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002230 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002231 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002232
Andrea Arcangeliba761492011-01-13 15:46:58 -08002233 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2234 _pte++, address += PAGE_SIZE) {
2235 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002236 if (pte_none(pteval) || (pte_present(pteval) &&
2237 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002238 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002239 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002240 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002241 } else {
2242 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002243 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002244 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002245 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002246 if (!pte_present(pteval)) {
2247 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002248 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002249 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002250 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002251 if (unlikely(!page)) {
2252 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002253 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002254 }
Bob Liu344aa352012-12-11 16:00:34 -08002255
Sasha Levin309381fea2014-01-23 15:52:54 -08002256 VM_BUG_ON_PAGE(PageCompound(page), page);
2257 VM_BUG_ON_PAGE(!PageAnon(page), page);
2258 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002259
Andrea Arcangeliba761492011-01-13 15:46:58 -08002260 /*
2261 * We can do it before isolate_lru_page because the
2262 * page can't be freed from under us. NOTE: PG_lock
2263 * is needed to serialize against split_huge_page
2264 * when invoked from the VM.
2265 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002266 if (!trylock_page(page)) {
2267 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002268 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002269 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002270
2271 /*
2272 * cannot use mapcount: can't collapse if there's a gup pin.
2273 * The page must only be referenced by the scanned process
2274 * and page swap cache.
2275 */
2276 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2277 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002278 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002279 goto out;
2280 }
2281 if (pte_write(pteval)) {
2282 writable = true;
2283 } else {
2284 if (PageSwapCache(page) && !reuse_swap_page(page)) {
2285 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002286 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002287 goto out;
2288 }
2289 /*
2290 * Page is not in the swap cache. It can be collapsed
2291 * into a THP.
2292 */
2293 }
2294
Andrea Arcangeliba761492011-01-13 15:46:58 -08002295 /*
2296 * Isolate the page to avoid collapsing an hugepage
2297 * currently in use by the VM.
2298 */
2299 if (isolate_lru_page(page)) {
2300 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002301 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002302 goto out;
2303 }
2304 /* 0 stands for page_is_file_cache(page) == false */
2305 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002306 VM_BUG_ON_PAGE(!PageLocked(page), page);
2307 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002308
2309 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002310 if (pte_young(pteval) ||
2311 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002312 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002313 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002314 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002315 if (likely(writable)) {
2316 if (likely(referenced)) {
2317 result = SCAN_SUCCEED;
2318 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
2319 referenced, writable, result);
2320 return 1;
2321 }
2322 } else {
2323 result = SCAN_PAGE_RO;
2324 }
2325
Andrea Arcangeliba761492011-01-13 15:46:58 -08002326out:
Bob Liu344aa352012-12-11 16:00:34 -08002327 release_pte_pages(pte, _pte);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002328 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
2329 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002330 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002331}
2332
2333static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2334 struct vm_area_struct *vma,
2335 unsigned long address,
2336 spinlock_t *ptl)
2337{
2338 pte_t *_pte;
2339 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2340 pte_t pteval = *_pte;
2341 struct page *src_page;
2342
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002343 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002344 clear_user_highpage(page, address);
2345 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002346 if (is_zero_pfn(pte_pfn(pteval))) {
2347 /*
2348 * ptl mostly unnecessary.
2349 */
2350 spin_lock(ptl);
2351 /*
2352 * paravirt calls inside pte_clear here are
2353 * superfluous.
2354 */
2355 pte_clear(vma->vm_mm, address, _pte);
2356 spin_unlock(ptl);
2357 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358 } else {
2359 src_page = pte_page(pteval);
2360 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002361 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002362 release_pte_page(src_page);
2363 /*
2364 * ptl mostly unnecessary, but preempt has to
2365 * be disabled to update the per-cpu stats
2366 * inside page_remove_rmap().
2367 */
2368 spin_lock(ptl);
2369 /*
2370 * paravirt calls inside pte_clear here are
2371 * superfluous.
2372 */
2373 pte_clear(vma->vm_mm, address, _pte);
2374 page_remove_rmap(src_page);
2375 spin_unlock(ptl);
2376 free_page_and_swap_cache(src_page);
2377 }
2378
2379 address += PAGE_SIZE;
2380 page++;
2381 }
2382}
2383
Xiao Guangrong26234f32012-10-08 16:29:51 -07002384static void khugepaged_alloc_sleep(void)
2385{
Petr Mladekbde43c62015-09-08 15:04:05 -07002386 DEFINE_WAIT(wait);
2387
2388 add_wait_queue(&khugepaged_wait, &wait);
2389 freezable_schedule_timeout_interruptible(
2390 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2391 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002392}
2393
Bob Liu9f1b8682013-11-12 15:07:37 -08002394static int khugepaged_node_load[MAX_NUMNODES];
2395
David Rientjes14a4e212014-08-06 16:07:29 -07002396static bool khugepaged_scan_abort(int nid)
2397{
2398 int i;
2399
2400 /*
2401 * If zone_reclaim_mode is disabled, then no extra effort is made to
2402 * allocate memory locally.
2403 */
2404 if (!zone_reclaim_mode)
2405 return false;
2406
2407 /* If there is a count for this node already, it must be acceptable */
2408 if (khugepaged_node_load[nid])
2409 return false;
2410
2411 for (i = 0; i < MAX_NUMNODES; i++) {
2412 if (!khugepaged_node_load[i])
2413 continue;
2414 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2415 return true;
2416 }
2417 return false;
2418}
2419
Xiao Guangrong26234f32012-10-08 16:29:51 -07002420#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002421static int khugepaged_find_target_node(void)
2422{
2423 static int last_khugepaged_target_node = NUMA_NO_NODE;
2424 int nid, target_node = 0, max_value = 0;
2425
2426 /* find first node with max normal pages hit */
2427 for (nid = 0; nid < MAX_NUMNODES; nid++)
2428 if (khugepaged_node_load[nid] > max_value) {
2429 max_value = khugepaged_node_load[nid];
2430 target_node = nid;
2431 }
2432
2433 /* do some balance if several nodes have the same hit record */
2434 if (target_node <= last_khugepaged_target_node)
2435 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2436 nid++)
2437 if (max_value == khugepaged_node_load[nid]) {
2438 target_node = nid;
2439 break;
2440 }
2441
2442 last_khugepaged_target_node = target_node;
2443 return target_node;
2444}
2445
Xiao Guangrong26234f32012-10-08 16:29:51 -07002446static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2447{
2448 if (IS_ERR(*hpage)) {
2449 if (!*wait)
2450 return false;
2451
2452 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002453 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002454 khugepaged_alloc_sleep();
2455 } else if (*hpage) {
2456 put_page(*hpage);
2457 *hpage = NULL;
2458 }
2459
2460 return true;
2461}
2462
Michal Hocko3b363692015-04-15 16:13:29 -07002463static struct page *
2464khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002465 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002466{
Sasha Levin309381fea2014-01-23 15:52:54 -08002467 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002468
Xiao Guangrong26234f32012-10-08 16:29:51 -07002469 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002470 * Before allocating the hugepage, release the mmap_sem read lock.
2471 * The allocation can take potentially a long time if it involves
2472 * sync compaction, and we do not need to hold the mmap_sem during
2473 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002474 */
2475 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002476
Vlastimil Babka96db8002015-09-08 15:03:50 -07002477 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002478 if (unlikely(!*hpage)) {
2479 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2480 *hpage = ERR_PTR(-ENOMEM);
2481 return NULL;
2482 }
2483
2484 count_vm_event(THP_COLLAPSE_ALLOC);
2485 return *hpage;
2486}
2487#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002488static int khugepaged_find_target_node(void)
2489{
2490 return 0;
2491}
2492
Bob Liu10dc4152013-11-12 15:07:35 -08002493static inline struct page *alloc_hugepage(int defrag)
2494{
2495 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
2496 HPAGE_PMD_ORDER);
2497}
2498
Xiao Guangrong26234f32012-10-08 16:29:51 -07002499static struct page *khugepaged_alloc_hugepage(bool *wait)
2500{
2501 struct page *hpage;
2502
2503 do {
2504 hpage = alloc_hugepage(khugepaged_defrag());
2505 if (!hpage) {
2506 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2507 if (!*wait)
2508 return NULL;
2509
2510 *wait = false;
2511 khugepaged_alloc_sleep();
2512 } else
2513 count_vm_event(THP_COLLAPSE_ALLOC);
2514 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2515
2516 return hpage;
2517}
2518
2519static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2520{
2521 if (!*hpage)
2522 *hpage = khugepaged_alloc_hugepage(wait);
2523
2524 if (unlikely(!*hpage))
2525 return false;
2526
2527 return true;
2528}
2529
Michal Hocko3b363692015-04-15 16:13:29 -07002530static struct page *
2531khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002532 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002533{
2534 up_read(&mm->mmap_sem);
2535 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002536
Xiao Guangrong26234f32012-10-08 16:29:51 -07002537 return *hpage;
2538}
2539#endif
2540
Bob Liufa475e52012-12-11 16:00:39 -08002541static bool hugepage_vma_check(struct vm_area_struct *vma)
2542{
2543 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2544 (vma->vm_flags & VM_NOHUGEPAGE))
2545 return false;
2546
2547 if (!vma->anon_vma || vma->vm_ops)
2548 return false;
2549 if (is_vma_temporary_stack(vma))
2550 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002551 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002552 return true;
2553}
2554
Andrea Arcangeliba761492011-01-13 15:46:58 -08002555static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002556 unsigned long address,
2557 struct page **hpage,
2558 struct vm_area_struct *vma,
2559 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002560{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002561 pmd_t *pmd, _pmd;
2562 pte_t *pte;
2563 pgtable_t pgtable;
2564 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002565 spinlock_t *pmd_ptl, *pte_ptl;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002566 int isolated, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002567 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002568 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002569 unsigned long mmun_start; /* For mmu_notifiers */
2570 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002571 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002572
2573 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002574
Michal Hocko3b363692015-04-15 16:13:29 -07002575 /* Only allocate from the target node */
2576 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2577 __GFP_THISNODE;
2578
Xiao Guangrong26234f32012-10-08 16:29:51 -07002579 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002580 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002581 if (!new_page) {
2582 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2583 goto out_nolock;
2584 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002585
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002586 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg))) {
2587 result = SCAN_CGROUP_CHARGE_FAIL;
2588 goto out_nolock;
2589 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002590
2591 /*
2592 * Prevent all access to pagetables with the exception of
2593 * gup_fast later hanlded by the ptep_clear_flush and the VM
2594 * handled by the anon_vma lock + PG_lock.
2595 */
2596 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002597 if (unlikely(khugepaged_test_exit(mm))) {
2598 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002599 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002600 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002601
2602 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002603 if (!vma) {
2604 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002605 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002606 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2608 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002609 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2610 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002611 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002612 }
2613 if (!hugepage_vma_check(vma)) {
2614 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002615 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002616 }
Bob Liu62190492012-12-11 16:00:37 -08002617 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002618 if (!pmd) {
2619 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002620 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002621 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002622
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002623 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002624
2625 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002626 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002627
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002628 mmun_start = address;
2629 mmun_end = address + HPAGE_PMD_SIZE;
2630 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002631 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002632 /*
2633 * After this gup_fast can't run anymore. This also removes
2634 * any huge TLB entry from the CPU so we won't allow
2635 * huge and small TLB entries for the same virtual address
2636 * to avoid the risk of CPU bugs in that area.
2637 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002638 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002639 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002640 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002641
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002642 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002643 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002644 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002645
2646 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002647 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002648 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002649 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002650 /*
2651 * We can only use set_pmd_at when establishing
2652 * hugepmds and never for establishing regular pmds that
2653 * points to regular pagetables. Use pmd_populate for that
2654 */
2655 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002656 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002657 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002658 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002659 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002660 }
2661
2662 /*
2663 * All pages are isolated and locked so anon_vma rmap
2664 * can't run anymore.
2665 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002666 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002667
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002668 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002669 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002670 __SetPageUptodate(new_page);
2671 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002672
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002673 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2674 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002675
2676 /*
2677 * spin_lock() below is not the equivalent of smp_wmb(), so
2678 * this is needed to avoid the copy_huge_page writes to become
2679 * visible after the set_pmd_at() write.
2680 */
2681 smp_wmb();
2682
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002683 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002684 BUG_ON(!pmd_none(*pmd));
2685 page_add_new_anon_rmap(new_page, vma, address);
Johannes Weiner00501b52014-08-08 14:19:20 -07002686 mem_cgroup_commit_charge(new_page, memcg, false);
2687 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002688 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002689 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002690 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002691 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002692
2693 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002694
Andrea Arcangeliba761492011-01-13 15:46:58 -08002695 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002696 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002697out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002698 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002699 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002700 return;
2701
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002702out_nolock:
2703 trace_mm_collapse_huge_page(mm, isolated, result);
2704 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002705out:
Johannes Weiner00501b52014-08-08 14:19:20 -07002706 mem_cgroup_cancel_charge(new_page, memcg);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002707 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002708}
2709
2710static int khugepaged_scan_pmd(struct mm_struct *mm,
2711 struct vm_area_struct *vma,
2712 unsigned long address,
2713 struct page **hpage)
2714{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002715 pmd_t *pmd;
2716 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002717 int ret = 0, none_or_zero = 0, result = 0;
2718 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002719 unsigned long _address;
2720 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002721 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002722 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002723
2724 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2725
Bob Liu62190492012-12-11 16:00:37 -08002726 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002727 if (!pmd) {
2728 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002729 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002730 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002731
Bob Liu9f1b8682013-11-12 15:07:37 -08002732 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002733 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2734 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2735 _pte++, _address += PAGE_SIZE) {
2736 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002737 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002738 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002739 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002740 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002741 } else {
2742 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002743 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002744 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002745 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002746 if (!pte_present(pteval)) {
2747 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002748 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002749 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002750 if (pte_write(pteval))
2751 writable = true;
2752
Andrea Arcangeliba761492011-01-13 15:46:58 -08002753 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002754 if (unlikely(!page)) {
2755 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002756 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002757 }
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002758 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002759 * Record which node the original page is from and save this
2760 * information to khugepaged_node_load[].
2761 * Khupaged will allocate hugepage from the node has the max
2762 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002763 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002764 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002765 if (khugepaged_scan_abort(node)) {
2766 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002767 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002768 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002769 khugepaged_node_load[node]++;
Sasha Levin309381fea2014-01-23 15:52:54 -08002770 VM_BUG_ON_PAGE(PageCompound(page), page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002771 if (!PageLRU(page)) {
2772 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002773 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002774 }
2775 if (PageLocked(page)) {
2776 result = SCAN_PAGE_LOCK;
2777 goto out_unmap;
2778 }
2779 if (!PageAnon(page)) {
2780 result = SCAN_PAGE_ANON;
2781 goto out_unmap;
2782 }
2783
Ebru Akagunduz10359212015-02-11 15:28:28 -08002784 /*
2785 * cannot use mapcount: can't collapse if there's a gup pin.
2786 * The page must only be referenced by the scanned process
2787 * and page swap cache.
2788 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002789 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2790 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002791 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002792 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002793 if (pte_young(pteval) ||
2794 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002795 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002796 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002797 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002798 if (writable) {
2799 if (referenced) {
2800 result = SCAN_SUCCEED;
2801 ret = 1;
2802 } else {
2803 result = SCAN_NO_REFERENCED_PAGE;
2804 }
2805 } else {
2806 result = SCAN_PAGE_RO;
2807 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002808out_unmap:
2809 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002810 if (ret) {
2811 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002812 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002813 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002814 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002815out:
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002816 trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2817 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002818 return ret;
2819}
2820
2821static void collect_mm_slot(struct mm_slot *mm_slot)
2822{
2823 struct mm_struct *mm = mm_slot->mm;
2824
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002825 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002826
2827 if (khugepaged_test_exit(mm)) {
2828 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002829 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002830 list_del(&mm_slot->mm_node);
2831
2832 /*
2833 * Not strictly needed because the mm exited already.
2834 *
2835 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2836 */
2837
2838 /* khugepaged_mm_lock actually not necessary for the below */
2839 free_mm_slot(mm_slot);
2840 mmdrop(mm);
2841 }
2842}
2843
2844static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2845 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002846 __releases(&khugepaged_mm_lock)
2847 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002848{
2849 struct mm_slot *mm_slot;
2850 struct mm_struct *mm;
2851 struct vm_area_struct *vma;
2852 int progress = 0;
2853
2854 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002855 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002856
2857 if (khugepaged_scan.mm_slot)
2858 mm_slot = khugepaged_scan.mm_slot;
2859 else {
2860 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2861 struct mm_slot, mm_node);
2862 khugepaged_scan.address = 0;
2863 khugepaged_scan.mm_slot = mm_slot;
2864 }
2865 spin_unlock(&khugepaged_mm_lock);
2866
2867 mm = mm_slot->mm;
2868 down_read(&mm->mmap_sem);
2869 if (unlikely(khugepaged_test_exit(mm)))
2870 vma = NULL;
2871 else
2872 vma = find_vma(mm, khugepaged_scan.address);
2873
2874 progress++;
2875 for (; vma; vma = vma->vm_next) {
2876 unsigned long hstart, hend;
2877
2878 cond_resched();
2879 if (unlikely(khugepaged_test_exit(mm))) {
2880 progress++;
2881 break;
2882 }
Bob Liufa475e52012-12-11 16:00:39 -08002883 if (!hugepage_vma_check(vma)) {
2884skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002885 progress++;
2886 continue;
2887 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002888 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2889 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002890 if (hstart >= hend)
2891 goto skip;
2892 if (khugepaged_scan.address > hend)
2893 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002894 if (khugepaged_scan.address < hstart)
2895 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002896 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002897
2898 while (khugepaged_scan.address < hend) {
2899 int ret;
2900 cond_resched();
2901 if (unlikely(khugepaged_test_exit(mm)))
2902 goto breakouterloop;
2903
2904 VM_BUG_ON(khugepaged_scan.address < hstart ||
2905 khugepaged_scan.address + HPAGE_PMD_SIZE >
2906 hend);
2907 ret = khugepaged_scan_pmd(mm, vma,
2908 khugepaged_scan.address,
2909 hpage);
2910 /* move to next address */
2911 khugepaged_scan.address += HPAGE_PMD_SIZE;
2912 progress += HPAGE_PMD_NR;
2913 if (ret)
2914 /* we released mmap_sem so break loop */
2915 goto breakouterloop_mmap_sem;
2916 if (progress >= pages)
2917 goto breakouterloop;
2918 }
2919 }
2920breakouterloop:
2921 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2922breakouterloop_mmap_sem:
2923
2924 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002925 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002926 /*
2927 * Release the current mm_slot if this mm is about to die, or
2928 * if we scanned all vmas of this mm.
2929 */
2930 if (khugepaged_test_exit(mm) || !vma) {
2931 /*
2932 * Make sure that if mm_users is reaching zero while
2933 * khugepaged runs here, khugepaged_exit will find
2934 * mm_slot not pointing to the exiting mm.
2935 */
2936 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2937 khugepaged_scan.mm_slot = list_entry(
2938 mm_slot->mm_node.next,
2939 struct mm_slot, mm_node);
2940 khugepaged_scan.address = 0;
2941 } else {
2942 khugepaged_scan.mm_slot = NULL;
2943 khugepaged_full_scans++;
2944 }
2945
2946 collect_mm_slot(mm_slot);
2947 }
2948
2949 return progress;
2950}
2951
2952static int khugepaged_has_work(void)
2953{
2954 return !list_empty(&khugepaged_scan.mm_head) &&
2955 khugepaged_enabled();
2956}
2957
2958static int khugepaged_wait_event(void)
2959{
2960 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002961 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002962}
2963
Xiao Guangrongd5169042012-10-08 16:29:48 -07002964static void khugepaged_do_scan(void)
2965{
2966 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002967 unsigned int progress = 0, pass_through_head = 0;
2968 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002969 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002970
2971 barrier(); /* write khugepaged_pages_to_scan to local stack */
2972
2973 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002974 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002975 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002976
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002977 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002978
Jiri Kosinacd092412015-06-24 16:56:07 -07002979 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002980 break;
2981
Andrea Arcangeliba761492011-01-13 15:46:58 -08002982 spin_lock(&khugepaged_mm_lock);
2983 if (!khugepaged_scan.mm_slot)
2984 pass_through_head++;
2985 if (khugepaged_has_work() &&
2986 pass_through_head < 2)
2987 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002988 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002989 else
2990 progress = pages;
2991 spin_unlock(&khugepaged_mm_lock);
2992 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002993
Xiao Guangrongd5169042012-10-08 16:29:48 -07002994 if (!IS_ERR_OR_NULL(hpage))
2995 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002996}
2997
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002998static void khugepaged_wait_work(void)
2999{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07003000 if (khugepaged_has_work()) {
3001 if (!khugepaged_scan_sleep_millisecs)
3002 return;
3003
3004 wait_event_freezable_timeout(khugepaged_wait,
3005 kthread_should_stop(),
3006 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
3007 return;
3008 }
3009
3010 if (khugepaged_enabled())
3011 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
3012}
3013
Andrea Arcangeliba761492011-01-13 15:46:58 -08003014static int khugepaged(void *none)
3015{
3016 struct mm_slot *mm_slot;
3017
Andrea Arcangeli878aee72011-01-13 15:47:10 -08003018 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08003019 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08003020
Xiao Guangrongb7231782012-10-08 16:29:54 -07003021 while (!kthread_should_stop()) {
3022 khugepaged_do_scan();
3023 khugepaged_wait_work();
3024 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08003025
3026 spin_lock(&khugepaged_mm_lock);
3027 mm_slot = khugepaged_scan.mm_slot;
3028 khugepaged_scan.mm_slot = NULL;
3029 if (mm_slot)
3030 collect_mm_slot(mm_slot);
3031 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08003032 return 0;
3033}
3034
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003035static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
3036 unsigned long haddr, pmd_t *pmd)
3037{
3038 struct mm_struct *mm = vma->vm_mm;
3039 pgtable_t pgtable;
3040 pmd_t _pmd;
3041 int i;
3042
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07003043 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003044 /* leave pmd empty until pte is filled */
3045
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07003046 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003047 pmd_populate(mm, &_pmd, pgtable);
3048
3049 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
3050 pte_t *pte, entry;
3051 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
3052 entry = pte_mkspecial(entry);
3053 pte = pte_offset_map(&_pmd, haddr);
3054 VM_BUG_ON(!pte_none(*pte));
3055 set_pte_at(mm, haddr, pte, entry);
3056 pte_unmap(pte);
3057 }
3058 smp_wmb(); /* make pte visible before pmd */
3059 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08003060 put_huge_zero_page();
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003061}
3062
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003063void __split_huge_page_pmd(struct vm_area_struct *vma, unsigned long address,
3064 pmd_t *pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003065{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08003066 spinlock_t *ptl;
Matthew Wilcox4897c762015-09-08 14:58:45 -07003067 struct page *page = NULL;
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003068 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003069 unsigned long haddr = address & HPAGE_PMD_MASK;
3070 unsigned long mmun_start; /* For mmu_notifiers */
3071 unsigned long mmun_end; /* For mmu_notifiers */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003072
3073 BUG_ON(vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003074
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003075 mmun_start = haddr;
3076 mmun_end = haddr + HPAGE_PMD_SIZE;
Hugh Dickins750e8162013-10-16 13:47:08 -07003077again:
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003078 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08003079 ptl = pmd_lock(mm, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07003080 if (unlikely(!pmd_trans_huge(*pmd)))
3081 goto unlock;
3082 if (vma_is_dax(vma)) {
Kirill A. Shutemov5b701b82015-09-08 14:59:28 -07003083 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
3084 if (is_huge_zero_pmd(_pmd))
3085 put_huge_zero_page();
Matthew Wilcox4897c762015-09-08 14:58:45 -07003086 } else if (is_huge_zero_pmd(*pmd)) {
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003087 __split_huge_zero_page_pmd(vma, haddr, pmd);
Matthew Wilcox4897c762015-09-08 14:58:45 -07003088 } else {
3089 page = pmd_page(*pmd);
3090 VM_BUG_ON_PAGE(!page_count(page), page);
3091 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003092 }
Matthew Wilcox4897c762015-09-08 14:58:45 -07003093 unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08003094 spin_unlock(ptl);
Kirill A. Shutemovc5a647d2012-12-12 13:51:00 -08003095 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003096
Matthew Wilcox4897c762015-09-08 14:58:45 -07003097 if (!page)
3098 return;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003099
Matthew Wilcox4897c762015-09-08 14:58:45 -07003100 split_huge_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003101 put_page(page);
Hugh Dickins750e8162013-10-16 13:47:08 -07003102
3103 /*
3104 * We don't always have down_write of mmap_sem here: a racing
3105 * do_huge_pmd_wp_page() might have copied-on-write to another
3106 * huge page before our split_huge_page() got the anon_vma lock.
3107 */
3108 if (unlikely(pmd_trans_huge(*pmd)))
3109 goto again;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08003110}
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003111
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003112void split_huge_page_pmd_mm(struct mm_struct *mm, unsigned long address,
3113 pmd_t *pmd)
3114{
3115 struct vm_area_struct *vma;
3116
3117 vma = find_vma(mm, address);
3118 BUG_ON(vma == NULL);
3119 split_huge_page_pmd(vma, address, pmd);
3120}
3121
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003122static void split_huge_page_address(struct mm_struct *mm,
3123 unsigned long address)
3124{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003125 pgd_t *pgd;
3126 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003127 pmd_t *pmd;
3128
3129 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
3130
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003131 pgd = pgd_offset(mm, address);
3132 if (!pgd_present(*pgd))
3133 return;
3134
3135 pud = pud_offset(pgd, address);
3136 if (!pud_present(*pud))
3137 return;
3138
3139 pmd = pmd_offset(pud, address);
3140 if (!pmd_present(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003141 return;
3142 /*
3143 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3144 * materialize from under us.
3145 */
Kirill A. Shutemove1803772012-12-12 13:50:59 -08003146 split_huge_page_pmd_mm(mm, address, pmd);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003147}
3148
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003149void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003150 unsigned long start,
3151 unsigned long end,
3152 long adjust_next)
3153{
3154 /*
3155 * If the new start address isn't hpage aligned and it could
3156 * previously contain an hugepage: check if we need to split
3157 * an huge pmd.
3158 */
3159 if (start & ~HPAGE_PMD_MASK &&
3160 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3161 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3162 split_huge_page_address(vma->vm_mm, start);
3163
3164 /*
3165 * If the new end address isn't hpage aligned and it could
3166 * previously contain an hugepage: check if we need to split
3167 * an huge pmd.
3168 */
3169 if (end & ~HPAGE_PMD_MASK &&
3170 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3171 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
3172 split_huge_page_address(vma->vm_mm, end);
3173
3174 /*
3175 * If we're also updating the vma->vm_next->vm_start, if the new
3176 * vm_next->vm_start isn't page aligned and it could previously
3177 * contain an hugepage: check if we need to split an huge pmd.
3178 */
3179 if (adjust_next > 0) {
3180 struct vm_area_struct *next = vma->vm_next;
3181 unsigned long nstart = next->vm_start;
3182 nstart += adjust_next << PAGE_SHIFT;
3183 if (nstart & ~HPAGE_PMD_MASK &&
3184 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3185 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
3186 split_huge_page_address(next->vm_mm, nstart);
3187 }
3188}