blob: 72fd53fe2b612a8aff79b39eba8c3b9ce74a1b6a [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,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080048 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080049 SCAN_ANY_PROCESS,
50 SCAN_VMA_NULL,
51 SCAN_VMA_CHECK,
52 SCAN_ADDRESS_RANGE,
53 SCAN_SWAP_CACHE_PAGE,
54 SCAN_DEL_PAGE_LRU,
55 SCAN_ALLOC_HUGE_PAGE_FAIL,
56 SCAN_CGROUP_CHARGE_FAIL
57};
58
59#define CREATE_TRACE_POINTS
60#include <trace/events/huge_memory.h>
61
Andrea Arcangeliba761492011-01-13 15:46:58 -080062/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080063 * By default transparent hugepage support is disabled in order that avoid
64 * to risk increase the memory footprint of applications without a guaranteed
65 * benefit. When transparent hugepage support is enabled, is for all mappings,
66 * and khugepaged scans all mappings.
67 * Defrag is invoked by khugepaged hugepage allocations and by page faults
68 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080069 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080070unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080071#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080072 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080073#endif
74#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
75 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
76#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080077 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080078 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
79 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080080
81/* default scan 8*512 pte (or vmas) every 30 second */
82static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
83static unsigned int khugepaged_pages_collapsed;
84static unsigned int khugepaged_full_scans;
85static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
86/* during fragmentation poll the hugepage allocator once every minute */
87static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
88static struct task_struct *khugepaged_thread __read_mostly;
89static DEFINE_MUTEX(khugepaged_mutex);
90static DEFINE_SPINLOCK(khugepaged_mm_lock);
91static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
92/*
93 * default collapse hugepages if there is at least one pte mapped like
94 * it would have happened if the vma was large enough during page
95 * fault.
96 */
97static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
98
99static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800100static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700101static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800102
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800103#define MM_SLOTS_HASH_BITS 10
104static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
105
Andrea Arcangeliba761492011-01-13 15:46:58 -0800106static struct kmem_cache *mm_slot_cache __read_mostly;
107
108/**
109 * struct mm_slot - hash lookup from mm to mm_slot
110 * @hash: hash collision list
111 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
112 * @mm: the mm that this information is valid for
113 */
114struct mm_slot {
115 struct hlist_node hash;
116 struct list_head mm_node;
117 struct mm_struct *mm;
118};
119
120/**
121 * struct khugepaged_scan - cursor for scanning
122 * @mm_head: the head of the mm list to scan
123 * @mm_slot: the current mm_slot we are scanning
124 * @address: the next address inside that to be scanned
125 *
126 * There is only the one khugepaged_scan instance of this cursor structure.
127 */
128struct khugepaged_scan {
129 struct list_head mm_head;
130 struct mm_slot *mm_slot;
131 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700132};
133static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800134 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
135};
136
Andrea Arcangelif0005652011-01-13 15:47:04 -0800137
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700138static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800139{
140 struct zone *zone;
141 int nr_zones = 0;
142 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800143
Andrea Arcangelif0005652011-01-13 15:47:04 -0800144 for_each_populated_zone(zone)
145 nr_zones++;
146
Mel Gorman974a7862015-11-06 16:28:34 -0800147 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800148 recommended_min = pageblock_nr_pages * nr_zones * 2;
149
150 /*
151 * Make sure that on average at least two pageblocks are almost free
152 * of another type, one for a migratetype to fall back to and a
153 * second to avoid subsequent fallbacks of other types There are 3
154 * MIGRATE_TYPES we care about.
155 */
156 recommended_min += pageblock_nr_pages * nr_zones *
157 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
158
159 /* don't ever allow to reserve more than 5% of the lowmem */
160 recommended_min = min(recommended_min,
161 (unsigned long) nr_free_buffer_pages() / 20);
162 recommended_min <<= (PAGE_SHIFT-10);
163
Han Pingtian42aa83c2014-01-23 15:53:28 -0800164 if (recommended_min > min_free_kbytes) {
165 if (user_min_free_kbytes >= 0)
166 pr_info("raising min_free_kbytes from %d to %lu "
167 "to help transparent hugepage allocations\n",
168 min_free_kbytes, recommended_min);
169
Andrea Arcangelif0005652011-01-13 15:47:04 -0800170 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800171 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800172 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800173}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800174
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700175static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800176{
177 int err = 0;
178 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800179 if (!khugepaged_thread)
180 khugepaged_thread = kthread_run(khugepaged, NULL,
181 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530182 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700183 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800184 err = PTR_ERR(khugepaged_thread);
185 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700186 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800187 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700188
189 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800190 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800191
192 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700193 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700194 kthread_stop(khugepaged_thread);
195 khugepaged_thread = NULL;
196 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700197fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800198 return err;
199}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800200
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800201static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800202struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800203
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700204struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800205{
206 struct page *zero_page;
207retry:
208 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700209 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800210
211 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
212 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800213 if (!zero_page) {
214 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700215 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800216 }
217 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800218 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700219 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800220 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700221 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800222 goto retry;
223 }
224
225 /* We take additional reference here. It will be put back by shrinker */
226 atomic_set(&huge_zero_refcount, 2);
227 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700228 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800229}
230
231static void put_huge_zero_page(void)
232{
233 /*
234 * Counter should never go to zero here. Only shrinker can put
235 * last reference.
236 */
237 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
238}
239
Glauber Costa48896462013-08-28 10:18:15 +1000240static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
241 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800242{
Glauber Costa48896462013-08-28 10:18:15 +1000243 /* we can free zero page only if last reference remains */
244 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
245}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800246
Glauber Costa48896462013-08-28 10:18:15 +1000247static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
248 struct shrink_control *sc)
249{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800250 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700251 struct page *zero_page = xchg(&huge_zero_page, NULL);
252 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700253 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000254 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800255 }
256
257 return 0;
258}
259
260static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000261 .count_objects = shrink_huge_zero_page_count,
262 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800263 .seeks = DEFAULT_SEEKS,
264};
265
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800266#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800267
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800268static ssize_t double_flag_show(struct kobject *kobj,
269 struct kobj_attribute *attr, char *buf,
270 enum transparent_hugepage_flag enabled,
271 enum transparent_hugepage_flag req_madv)
272{
273 if (test_bit(enabled, &transparent_hugepage_flags)) {
274 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
275 return sprintf(buf, "[always] madvise never\n");
276 } else if (test_bit(req_madv, &transparent_hugepage_flags))
277 return sprintf(buf, "always [madvise] never\n");
278 else
279 return sprintf(buf, "always madvise [never]\n");
280}
281static ssize_t double_flag_store(struct kobject *kobj,
282 struct kobj_attribute *attr,
283 const char *buf, size_t count,
284 enum transparent_hugepage_flag enabled,
285 enum transparent_hugepage_flag req_madv)
286{
287 if (!memcmp("always", buf,
288 min(sizeof("always")-1, count))) {
289 set_bit(enabled, &transparent_hugepage_flags);
290 clear_bit(req_madv, &transparent_hugepage_flags);
291 } else if (!memcmp("madvise", buf,
292 min(sizeof("madvise")-1, count))) {
293 clear_bit(enabled, &transparent_hugepage_flags);
294 set_bit(req_madv, &transparent_hugepage_flags);
295 } else if (!memcmp("never", buf,
296 min(sizeof("never")-1, count))) {
297 clear_bit(enabled, &transparent_hugepage_flags);
298 clear_bit(req_madv, &transparent_hugepage_flags);
299 } else
300 return -EINVAL;
301
302 return count;
303}
304
305static ssize_t enabled_show(struct kobject *kobj,
306 struct kobj_attribute *attr, char *buf)
307{
308 return double_flag_show(kobj, attr, buf,
309 TRANSPARENT_HUGEPAGE_FLAG,
310 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
311}
312static ssize_t enabled_store(struct kobject *kobj,
313 struct kobj_attribute *attr,
314 const char *buf, size_t count)
315{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800316 ssize_t ret;
317
318 ret = double_flag_store(kobj, attr, buf, count,
319 TRANSPARENT_HUGEPAGE_FLAG,
320 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
321
322 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700323 int err;
324
325 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700326 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700327 mutex_unlock(&khugepaged_mutex);
328
Andrea Arcangeliba761492011-01-13 15:46:58 -0800329 if (err)
330 ret = err;
331 }
332
333 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800334}
335static struct kobj_attribute enabled_attr =
336 __ATTR(enabled, 0644, enabled_show, enabled_store);
337
338static ssize_t single_flag_show(struct kobject *kobj,
339 struct kobj_attribute *attr, char *buf,
340 enum transparent_hugepage_flag flag)
341{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700342 return sprintf(buf, "%d\n",
343 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800344}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700345
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800346static ssize_t single_flag_store(struct kobject *kobj,
347 struct kobj_attribute *attr,
348 const char *buf, size_t count,
349 enum transparent_hugepage_flag flag)
350{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700351 unsigned long value;
352 int ret;
353
354 ret = kstrtoul(buf, 10, &value);
355 if (ret < 0)
356 return ret;
357 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800358 return -EINVAL;
359
Ben Hutchingse27e6152011-04-14 15:22:21 -0700360 if (value)
361 set_bit(flag, &transparent_hugepage_flags);
362 else
363 clear_bit(flag, &transparent_hugepage_flags);
364
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800365 return count;
366}
367
368/*
369 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
370 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
371 * memory just to allocate one more hugepage.
372 */
373static ssize_t defrag_show(struct kobject *kobj,
374 struct kobj_attribute *attr, char *buf)
375{
376 return double_flag_show(kobj, attr, buf,
377 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
378 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
379}
380static ssize_t defrag_store(struct kobject *kobj,
381 struct kobj_attribute *attr,
382 const char *buf, size_t count)
383{
384 return double_flag_store(kobj, attr, buf, count,
385 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
386 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
387}
388static struct kobj_attribute defrag_attr =
389 __ATTR(defrag, 0644, defrag_show, defrag_store);
390
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800391static ssize_t use_zero_page_show(struct kobject *kobj,
392 struct kobj_attribute *attr, char *buf)
393{
394 return single_flag_show(kobj, attr, buf,
395 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
396}
397static ssize_t use_zero_page_store(struct kobject *kobj,
398 struct kobj_attribute *attr, const char *buf, size_t count)
399{
400 return single_flag_store(kobj, attr, buf, count,
401 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
402}
403static struct kobj_attribute use_zero_page_attr =
404 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800405#ifdef CONFIG_DEBUG_VM
406static ssize_t debug_cow_show(struct kobject *kobj,
407 struct kobj_attribute *attr, char *buf)
408{
409 return single_flag_show(kobj, attr, buf,
410 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
411}
412static ssize_t debug_cow_store(struct kobject *kobj,
413 struct kobj_attribute *attr,
414 const char *buf, size_t count)
415{
416 return single_flag_store(kobj, attr, buf, count,
417 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
418}
419static struct kobj_attribute debug_cow_attr =
420 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
421#endif /* CONFIG_DEBUG_VM */
422
423static struct attribute *hugepage_attr[] = {
424 &enabled_attr.attr,
425 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800426 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800427#ifdef CONFIG_DEBUG_VM
428 &debug_cow_attr.attr,
429#endif
430 NULL,
431};
432
433static struct attribute_group hugepage_attr_group = {
434 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800435};
436
437static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
438 struct kobj_attribute *attr,
439 char *buf)
440{
441 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
442}
443
444static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
445 struct kobj_attribute *attr,
446 const char *buf, size_t count)
447{
448 unsigned long msecs;
449 int err;
450
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700451 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800452 if (err || msecs > UINT_MAX)
453 return -EINVAL;
454
455 khugepaged_scan_sleep_millisecs = msecs;
456 wake_up_interruptible(&khugepaged_wait);
457
458 return count;
459}
460static struct kobj_attribute scan_sleep_millisecs_attr =
461 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
462 scan_sleep_millisecs_store);
463
464static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
465 struct kobj_attribute *attr,
466 char *buf)
467{
468 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
469}
470
471static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
472 struct kobj_attribute *attr,
473 const char *buf, size_t count)
474{
475 unsigned long msecs;
476 int err;
477
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700478 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800479 if (err || msecs > UINT_MAX)
480 return -EINVAL;
481
482 khugepaged_alloc_sleep_millisecs = msecs;
483 wake_up_interruptible(&khugepaged_wait);
484
485 return count;
486}
487static struct kobj_attribute alloc_sleep_millisecs_attr =
488 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
489 alloc_sleep_millisecs_store);
490
491static ssize_t pages_to_scan_show(struct kobject *kobj,
492 struct kobj_attribute *attr,
493 char *buf)
494{
495 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
496}
497static ssize_t pages_to_scan_store(struct kobject *kobj,
498 struct kobj_attribute *attr,
499 const char *buf, size_t count)
500{
501 int err;
502 unsigned long pages;
503
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700504 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800505 if (err || !pages || pages > UINT_MAX)
506 return -EINVAL;
507
508 khugepaged_pages_to_scan = pages;
509
510 return count;
511}
512static struct kobj_attribute pages_to_scan_attr =
513 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
514 pages_to_scan_store);
515
516static ssize_t pages_collapsed_show(struct kobject *kobj,
517 struct kobj_attribute *attr,
518 char *buf)
519{
520 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
521}
522static struct kobj_attribute pages_collapsed_attr =
523 __ATTR_RO(pages_collapsed);
524
525static ssize_t full_scans_show(struct kobject *kobj,
526 struct kobj_attribute *attr,
527 char *buf)
528{
529 return sprintf(buf, "%u\n", khugepaged_full_scans);
530}
531static struct kobj_attribute full_scans_attr =
532 __ATTR_RO(full_scans);
533
534static ssize_t khugepaged_defrag_show(struct kobject *kobj,
535 struct kobj_attribute *attr, char *buf)
536{
537 return single_flag_show(kobj, attr, buf,
538 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
539}
540static ssize_t khugepaged_defrag_store(struct kobject *kobj,
541 struct kobj_attribute *attr,
542 const char *buf, size_t count)
543{
544 return single_flag_store(kobj, attr, buf, count,
545 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
546}
547static struct kobj_attribute khugepaged_defrag_attr =
548 __ATTR(defrag, 0644, khugepaged_defrag_show,
549 khugepaged_defrag_store);
550
551/*
552 * max_ptes_none controls if khugepaged should collapse hugepages over
553 * any unmapped ptes in turn potentially increasing the memory
554 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
555 * reduce the available free memory in the system as it
556 * runs. Increasing max_ptes_none will instead potentially reduce the
557 * free memory in the system during the khugepaged scan.
558 */
559static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
560 struct kobj_attribute *attr,
561 char *buf)
562{
563 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
564}
565static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
566 struct kobj_attribute *attr,
567 const char *buf, size_t count)
568{
569 int err;
570 unsigned long max_ptes_none;
571
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700572 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800573 if (err || max_ptes_none > HPAGE_PMD_NR-1)
574 return -EINVAL;
575
576 khugepaged_max_ptes_none = max_ptes_none;
577
578 return count;
579}
580static struct kobj_attribute khugepaged_max_ptes_none_attr =
581 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
582 khugepaged_max_ptes_none_store);
583
584static struct attribute *khugepaged_attr[] = {
585 &khugepaged_defrag_attr.attr,
586 &khugepaged_max_ptes_none_attr.attr,
587 &pages_to_scan_attr.attr,
588 &pages_collapsed_attr.attr,
589 &full_scans_attr.attr,
590 &scan_sleep_millisecs_attr.attr,
591 &alloc_sleep_millisecs_attr.attr,
592 NULL,
593};
594
595static struct attribute_group khugepaged_attr_group = {
596 .attrs = khugepaged_attr,
597 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800598};
Shaohua Li569e5592012-01-12 17:19:11 -0800599
600static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
601{
602 int err;
603
604 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
605 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700606 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800607 return -ENOMEM;
608 }
609
610 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
611 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700612 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800613 goto delete_obj;
614 }
615
616 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
617 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700618 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800619 goto remove_hp_group;
620 }
621
622 return 0;
623
624remove_hp_group:
625 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
626delete_obj:
627 kobject_put(*hugepage_kobj);
628 return err;
629}
630
631static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
632{
633 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
634 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
635 kobject_put(hugepage_kobj);
636}
637#else
638static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
639{
640 return 0;
641}
642
643static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
644{
645}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800646#endif /* CONFIG_SYSFS */
647
648static int __init hugepage_init(void)
649{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800650 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800651 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800652
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800653 if (!has_transparent_hugepage()) {
654 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800655 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800656 }
657
Shaohua Li569e5592012-01-12 17:19:11 -0800658 err = hugepage_init_sysfs(&hugepage_kobj);
659 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700660 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800661
662 err = khugepaged_slab_init();
663 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700664 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800665
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700666 err = register_shrinker(&huge_zero_page_shrinker);
667 if (err)
668 goto err_hzp_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800669
Rik van Riel97562cd2011-01-13 15:47:12 -0800670 /*
671 * By default disable transparent hugepages on smaller systems,
672 * where the extra memory used could hurt more than TLB overhead
673 * is likely to save. The admin can still enable it through /sys.
674 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700675 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800676 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700677 return 0;
678 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800679
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700680 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700681 if (err)
682 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800683
Shaohua Li569e5592012-01-12 17:19:11 -0800684 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700685err_khugepaged:
686 unregister_shrinker(&huge_zero_page_shrinker);
687err_hzp_shrinker:
688 khugepaged_slab_exit();
689err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800690 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700691err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800692 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800693}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800694subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800695
696static int __init setup_transparent_hugepage(char *str)
697{
698 int ret = 0;
699 if (!str)
700 goto out;
701 if (!strcmp(str, "always")) {
702 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
703 &transparent_hugepage_flags);
704 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
705 &transparent_hugepage_flags);
706 ret = 1;
707 } else if (!strcmp(str, "madvise")) {
708 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
709 &transparent_hugepage_flags);
710 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
711 &transparent_hugepage_flags);
712 ret = 1;
713 } else if (!strcmp(str, "never")) {
714 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
715 &transparent_hugepage_flags);
716 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
717 &transparent_hugepage_flags);
718 ret = 1;
719 }
720out:
721 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700722 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800723 return ret;
724}
725__setup("transparent_hugepage=", setup_transparent_hugepage);
726
Mel Gormanb32967f2012-11-19 12:35:47 +0000727pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800728{
729 if (likely(vma->vm_flags & VM_WRITE))
730 pmd = pmd_mkwrite(pmd);
731 return pmd;
732}
733
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700734static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800735{
736 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700737 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800738 entry = pmd_mkhuge(entry);
739 return entry;
740}
741
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800742static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
743 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700744 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700745 struct page *page, gfp_t gfp,
746 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800747{
Johannes Weiner00501b52014-08-08 14:19:20 -0700748 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800749 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800750 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700751 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800752
Sasha Levin309381fea2014-01-23 15:52:54 -0800753 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700754
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800755 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700756 put_page(page);
757 count_vm_event(THP_FAULT_FALLBACK);
758 return VM_FAULT_FALLBACK;
759 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800760
Johannes Weiner00501b52014-08-08 14:19:20 -0700761 pgtable = pte_alloc_one(mm, haddr);
762 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800763 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700764 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700765 return VM_FAULT_OOM;
766 }
767
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800768 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700769 /*
770 * The memory barrier inside __SetPageUptodate makes sure that
771 * clear_huge_page writes become visible before the set_pmd_at()
772 * write.
773 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800774 __SetPageUptodate(page);
775
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800776 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800777 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800778 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800779 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800780 put_page(page);
781 pte_free(mm, pgtable);
782 } else {
783 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700784
785 /* Deliver the page fault to userland */
786 if (userfaultfd_missing(vma)) {
787 int ret;
788
789 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800790 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700791 put_page(page);
792 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700793 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700794 VM_UFFD_MISSING);
795 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
796 return ret;
797 }
798
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700799 entry = mk_huge_pmd(page, vma->vm_page_prot);
800 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800801 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800802 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700803 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700804 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800805 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800806 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800807 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800808 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700809 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800810 }
811
David Rientjesaa2e8782012-05-29 15:06:17 -0700812 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800813}
814
Andi Kleencc5d4622011-03-22 16:33:13 -0700815static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800816{
Mel Gorman71baba42015-11-06 16:28:28 -0800817 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800818}
819
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800820/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700821static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800822 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700823 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800824{
825 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700826 if (!pmd_none(*pmd))
827 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700828 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800829 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700830 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800831 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800832 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700833 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800834}
835
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800836int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
837 unsigned long address, pmd_t *pmd,
838 unsigned int flags)
839{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800840 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800841 struct page *page;
842 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800843
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700844 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700845 return VM_FAULT_FALLBACK;
Kirill A. Shutemov7479df62016-01-15 16:52:35 -0800846 if (vma->vm_flags & VM_LOCKED)
847 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700848 if (unlikely(anon_vma_prepare(vma)))
849 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700850 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700851 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200852 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700853 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800854 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700855 pgtable_t pgtable;
856 struct page *zero_page;
857 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700858 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700859 pgtable = pte_alloc_one(mm, haddr);
860 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800861 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700862 zero_page = get_huge_zero_page();
863 if (unlikely(!zero_page)) {
864 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700865 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700866 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700867 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800868 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700869 ret = 0;
870 set = false;
871 if (pmd_none(*pmd)) {
872 if (userfaultfd_missing(vma)) {
873 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700874 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700875 VM_UFFD_MISSING);
876 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
877 } else {
878 set_huge_zero_page(pgtable, mm, vma,
879 haddr, pmd,
880 zero_page);
881 spin_unlock(ptl);
882 set = true;
883 }
884 } else
885 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700886 if (!set) {
887 pte_free(mm, pgtable);
888 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800889 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700890 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800891 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800892 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
893 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700894 if (unlikely(!page)) {
895 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700896 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700897 }
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700898 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
899 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800900}
901
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700902static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700903 pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
904{
905 struct mm_struct *mm = vma->vm_mm;
906 pmd_t entry;
907 spinlock_t *ptl;
908
909 ptl = pmd_lock(mm, pmd);
910 if (pmd_none(*pmd)) {
911 entry = pmd_mkhuge(pfn_pmd(pfn, prot));
912 if (write) {
913 entry = pmd_mkyoung(pmd_mkdirty(entry));
914 entry = maybe_pmd_mkwrite(entry, vma);
915 }
916 set_pmd_at(mm, addr, pmd, entry);
917 update_mmu_cache_pmd(vma, addr, pmd);
918 }
919 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700920}
921
922int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
923 pmd_t *pmd, unsigned long pfn, bool write)
924{
925 pgprot_t pgprot = vma->vm_page_prot;
926 /*
927 * If we had pmd_special, we could avoid all these restrictions,
928 * but we need to be consistent with PTEs and architectures that
929 * can't support a 'special' bit.
930 */
931 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
932 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
933 (VM_PFNMAP|VM_MIXEDMAP));
934 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
935 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
936
937 if (addr < vma->vm_start || addr >= vma->vm_end)
938 return VM_FAULT_SIGBUS;
939 if (track_pfn_insert(vma, &pgprot, pfn))
940 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700941 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
942 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700943}
944
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800945int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
946 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
947 struct vm_area_struct *vma)
948{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800949 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800950 struct page *src_page;
951 pmd_t pmd;
952 pgtable_t pgtable;
953 int ret;
954
955 ret = -ENOMEM;
956 pgtable = pte_alloc_one(dst_mm, addr);
957 if (unlikely(!pgtable))
958 goto out;
959
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800960 dst_ptl = pmd_lock(dst_mm, dst_pmd);
961 src_ptl = pmd_lockptr(src_mm, src_pmd);
962 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800963
964 ret = -EAGAIN;
965 pmd = *src_pmd;
966 if (unlikely(!pmd_trans_huge(pmd))) {
967 pte_free(dst_mm, pgtable);
968 goto out_unlock;
969 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800970 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800971 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800972 * under splitting since we don't split the page itself, only pmd to
973 * a page table.
974 */
975 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700976 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800977 /*
978 * get_huge_zero_page() will never allocate a new page here,
979 * since we already have a zero page to copy. It just takes a
980 * reference.
981 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700982 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700983 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700984 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800985 ret = 0;
986 goto out_unlock;
987 }
Mel Gormande466bd2013-12-18 17:08:42 -0800988
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800989 if (unlikely(pmd_trans_splitting(pmd))) {
990 /* split huge page running from under us */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800991 spin_unlock(src_ptl);
992 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800993 pte_free(dst_mm, pgtable);
994
995 wait_split_huge_page(vma->anon_vma, src_pmd); /* src_vma */
996 goto out;
997 }
998 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -0800999 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001000 get_page(src_page);
1001 page_dup_rmap(src_page);
1002 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1003
1004 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1005 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001006 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001007 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001008 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001009
1010 ret = 0;
1011out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001012 spin_unlock(src_ptl);
1013 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001014out:
1015 return ret;
1016}
1017
Will Deacona1dd4502012-12-11 16:01:27 -08001018void huge_pmd_set_accessed(struct mm_struct *mm,
1019 struct vm_area_struct *vma,
1020 unsigned long address,
1021 pmd_t *pmd, pmd_t orig_pmd,
1022 int dirty)
1023{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001024 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001025 pmd_t entry;
1026 unsigned long haddr;
1027
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001028 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001029 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1030 goto unlock;
1031
1032 entry = pmd_mkyoung(orig_pmd);
1033 haddr = address & HPAGE_PMD_MASK;
1034 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1035 update_mmu_cache_pmd(vma, address, pmd);
1036
1037unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001038 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001039}
1040
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001041static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1042 struct vm_area_struct *vma,
1043 unsigned long address,
1044 pmd_t *pmd, pmd_t orig_pmd,
1045 struct page *page,
1046 unsigned long haddr)
1047{
Johannes Weiner00501b52014-08-08 14:19:20 -07001048 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001049 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001050 pgtable_t pgtable;
1051 pmd_t _pmd;
1052 int ret = 0, i;
1053 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001054 unsigned long mmun_start; /* For mmu_notifiers */
1055 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001056
1057 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1058 GFP_KERNEL);
1059 if (unlikely(!pages)) {
1060 ret |= VM_FAULT_OOM;
1061 goto out;
1062 }
1063
1064 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001065 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1066 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001067 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001068 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001069 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001070 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001071 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001072 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001073 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001074 memcg = (void *)page_private(pages[i]);
1075 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001076 mem_cgroup_cancel_charge(pages[i], memcg,
1077 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001078 put_page(pages[i]);
1079 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001080 kfree(pages);
1081 ret |= VM_FAULT_OOM;
1082 goto out;
1083 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001084 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001085 }
1086
1087 for (i = 0; i < HPAGE_PMD_NR; i++) {
1088 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001089 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001090 __SetPageUptodate(pages[i]);
1091 cond_resched();
1092 }
1093
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001094 mmun_start = haddr;
1095 mmun_end = haddr + HPAGE_PMD_SIZE;
1096 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1097
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001098 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001099 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1100 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001101 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001103 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001104 /* leave pmd empty until pte is filled */
1105
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001106 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001107 pmd_populate(mm, &_pmd, pgtable);
1108
1109 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1110 pte_t *pte, entry;
1111 entry = mk_pte(pages[i], vma->vm_page_prot);
1112 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001113 memcg = (void *)page_private(pages[i]);
1114 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001115 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001116 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001117 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001118 pte = pte_offset_map(&_pmd, haddr);
1119 VM_BUG_ON(!pte_none(*pte));
1120 set_pte_at(mm, haddr, pte, entry);
1121 pte_unmap(pte);
1122 }
1123 kfree(pages);
1124
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001125 smp_wmb(); /* make pte visible before pmd */
1126 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001127 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001128 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001129
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001130 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1131
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001132 ret |= VM_FAULT_WRITE;
1133 put_page(page);
1134
1135out:
1136 return ret;
1137
1138out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001139 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001140 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001141 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001142 memcg = (void *)page_private(pages[i]);
1143 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001144 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001145 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001146 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147 kfree(pages);
1148 goto out;
1149}
1150
1151int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1152 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1153{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001154 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001155 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001156 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001157 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001158 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001159 unsigned long mmun_start; /* For mmu_notifiers */
1160 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001161 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001162
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001163 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001164 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001165 haddr = address & HPAGE_PMD_MASK;
1166 if (is_huge_zero_pmd(orig_pmd))
1167 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001168 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001169 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1170 goto out_unlock;
1171
1172 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001173 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001174 /*
1175 * We can only reuse the page if nobody else maps the huge page or it's
1176 * part. We can do it by checking page_mapcount() on each sub-page, but
1177 * it's expensive.
1178 * The cheaper way is to check page_count() to be equal 1: every
1179 * mapcount takes page reference reference, so this way we can
1180 * guarantee, that the PMD is the only mapping.
1181 * This can give false negative if somebody pinned the page, but that's
1182 * fine.
1183 */
1184 if (page_mapcount(page) == 1 && page_count(page) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 pmd_t entry;
1186 entry = pmd_mkyoung(orig_pmd);
1187 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1188 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001189 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001190 ret |= VM_FAULT_WRITE;
1191 goto out_unlock;
1192 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001193 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001194 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001195alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001196 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001197 !transparent_hugepage_debug_cow()) {
Michal Hocko3b363692015-04-15 16:13:29 -07001198 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1199 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001200 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001201 new_page = NULL;
1202
1203 if (unlikely(!new_page)) {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001204 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001205 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001206 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001207 } else {
1208 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1209 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001210 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001211 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001212 ret |= VM_FAULT_FALLBACK;
1213 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001214 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001215 }
David Rientjes17766dd2013-09-12 15:14:06 -07001216 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001217 goto out;
1218 }
1219
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001220 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1221 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001222 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001223 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001224 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001225 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001226 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001227 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001228 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001229 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001230 goto out;
1231 }
1232
David Rientjes17766dd2013-09-12 15:14:06 -07001233 count_vm_event(THP_FAULT_ALLOC);
1234
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001235 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001236 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1237 else
1238 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 __SetPageUptodate(new_page);
1240
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001241 mmun_start = haddr;
1242 mmun_end = haddr + HPAGE_PMD_SIZE;
1243 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1244
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001245 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001246 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001247 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001248 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001249 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001250 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001251 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001252 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001253 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001254 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001255 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1256 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001257 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001258 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001259 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001260 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001261 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001262 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001263 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001264 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001265 put_huge_zero_page();
1266 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001267 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001268 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001269 put_page(page);
1270 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001271 ret |= VM_FAULT_WRITE;
1272 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001273 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001274out_mn:
1275 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1276out:
1277 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001278out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001279 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280 return ret;
1281}
1282
David Rientjesb676b292012-10-08 16:34:03 -07001283struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001284 unsigned long addr,
1285 pmd_t *pmd,
1286 unsigned int flags)
1287{
David Rientjesb676b292012-10-08 16:34:03 -07001288 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001289 struct page *page = NULL;
1290
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001291 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001292
1293 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1294 goto out;
1295
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001296 /* Avoid dumping huge zero page */
1297 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1298 return ERR_PTR(-EFAULT);
1299
Mel Gorman2b4847e2013-12-18 17:08:32 -08001300 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001301 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001302 goto out;
1303
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001304 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001305 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001306 if (flags & FOLL_TOUCH) {
1307 pmd_t _pmd;
1308 /*
1309 * We should set the dirty bit only for FOLL_WRITE but
1310 * for now the dirty bit in the pmd is meaningless.
1311 * And if the dirty bit will become meaningful and
1312 * we'll only set it with FOLL_WRITE, an atomic
1313 * set_bit will be required on the pmd to set the
1314 * young bit, instead of the current set_pmd_at.
1315 */
1316 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001317 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1318 pmd, _pmd, 1))
1319 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001320 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001321 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
David Rientjesb676b292012-10-08 16:34:03 -07001322 if (page->mapping && trylock_page(page)) {
1323 lru_add_drain();
1324 if (page->mapping)
1325 mlock_vma_page(page);
1326 unlock_page(page);
1327 }
1328 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001329 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001330 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001331 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001332 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001333
1334out:
1335 return page;
1336}
1337
Mel Gormand10e63f2012-10-25 14:16:31 +02001338/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001339int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1340 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001341{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001342 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001343 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001344 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001345 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001346 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001347 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001348 bool page_locked;
1349 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001350 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001351 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001352
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001353 /* A PROT_NONE fault should not end up here */
1354 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1355
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001356 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001357 if (unlikely(!pmd_same(pmd, *pmdp)))
1358 goto out_unlock;
1359
Mel Gormande466bd2013-12-18 17:08:42 -08001360 /*
1361 * If there are potential migrations, wait for completion and retry
1362 * without disrupting NUMA hinting information. Do not relock and
1363 * check_same as the page may no longer be mapped.
1364 */
1365 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001366 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001367 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001368 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001369 goto out;
1370 }
1371
Mel Gormand10e63f2012-10-25 14:16:31 +02001372 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001373 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001374 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001375 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001376 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001377 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001378 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001379 flags |= TNF_FAULT_LOCAL;
1380 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001381
Mel Gormanbea66fb2015-03-25 15:55:37 -07001382 /* See similar comment in do_numa_page for explanation */
1383 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001384 flags |= TNF_NO_GROUP;
1385
1386 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001387 * Acquire the page lock to serialise THP migrations but avoid dropping
1388 * page_table_lock if at all possible
1389 */
Mel Gormanb8916632013-10-07 11:28:44 +01001390 page_locked = trylock_page(page);
1391 target_nid = mpol_misplaced(page, vma, haddr);
1392 if (target_nid == -1) {
1393 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001394 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001395 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001396 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001397
Mel Gormande466bd2013-12-18 17:08:42 -08001398 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001399 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001400 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001401 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001402 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001403 goto out;
1404 }
1405
Mel Gorman2b4847e2013-12-18 17:08:32 -08001406 /*
1407 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1408 * to serialises splits
1409 */
Mel Gormanb8916632013-10-07 11:28:44 +01001410 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001411 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001412 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001413
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001414 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001415 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001416 if (unlikely(!pmd_same(pmd, *pmdp))) {
1417 unlock_page(page);
1418 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001419 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001420 goto out_unlock;
1421 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001422
Mel Gormanc3a489c2013-12-18 17:08:38 -08001423 /* Bail if we fail to protect against THP splits for any reason */
1424 if (unlikely(!anon_vma)) {
1425 put_page(page);
1426 page_nid = -1;
1427 goto clear_pmdnuma;
1428 }
1429
Mel Gormana54a4072013-10-07 11:28:46 +01001430 /*
1431 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001432 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001433 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001434 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001435 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001436 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001437 if (migrated) {
1438 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001439 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001440 } else
1441 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001442
Mel Gorman8191acb2013-10-07 11:28:45 +01001443 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001444clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001445 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001446 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001447 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001448 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001449 if (was_writable)
1450 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001451 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001452 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001453 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001454out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001455 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001456
1457out:
1458 if (anon_vma)
1459 page_unlock_anon_vma_read(anon_vma);
1460
Mel Gorman8191acb2013-10-07 11:28:45 +01001461 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001462 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001463
Mel Gormand10e63f2012-10-25 14:16:31 +02001464 return 0;
1465}
1466
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001467int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001468 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001469{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001470 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001471 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001472
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001473 if (__pmd_trans_huge_lock(pmd, vma, &ptl) != 1)
1474 return 0;
1475 /*
1476 * For architectures like ppc64 we look at deposited pgtable
1477 * when calling pmdp_huge_get_and_clear. So do the
1478 * pgtable_trans_huge_withdraw after finishing pmdp related
1479 * operations.
1480 */
1481 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1482 tlb->fullmm);
1483 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1484 if (vma_is_dax(vma)) {
1485 spin_unlock(ptl);
1486 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001487 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001488 } else if (is_huge_zero_pmd(orig_pmd)) {
1489 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1490 atomic_long_dec(&tlb->mm->nr_ptes);
1491 spin_unlock(ptl);
1492 put_huge_zero_page();
1493 } else {
1494 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001495 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001496 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1497 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1498 VM_BUG_ON_PAGE(!PageHead(page), page);
1499 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1500 atomic_long_dec(&tlb->mm->nr_ptes);
1501 spin_unlock(ptl);
1502 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001503 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001504 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001505}
1506
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001507int move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
1508 unsigned long old_addr,
1509 unsigned long new_addr, unsigned long old_end,
1510 pmd_t *old_pmd, pmd_t *new_pmd)
1511{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001512 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001513 int ret = 0;
1514 pmd_t pmd;
1515
1516 struct mm_struct *mm = vma->vm_mm;
1517
1518 if ((old_addr & ~HPAGE_PMD_MASK) ||
1519 (new_addr & ~HPAGE_PMD_MASK) ||
1520 old_end - old_addr < HPAGE_PMD_SIZE ||
1521 (new_vma->vm_flags & VM_NOHUGEPAGE))
1522 goto out;
1523
1524 /*
1525 * The destination pmd shouldn't be established, free_pgtables()
1526 * should have release it.
1527 */
1528 if (WARN_ON(!pmd_none(*new_pmd))) {
1529 VM_BUG_ON(pmd_trans_huge(*new_pmd));
1530 goto out;
1531 }
1532
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001533 /*
1534 * We don't have to worry about the ordering of src and dst
1535 * ptlocks because exclusive mmap_sem prevents deadlock.
1536 */
1537 ret = __pmd_trans_huge_lock(old_pmd, vma, &old_ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001538 if (ret == 1) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001539 new_ptl = pmd_lockptr(mm, new_pmd);
1540 if (new_ptl != old_ptl)
1541 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001542 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001543 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001544
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301545 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1546 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001547 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1548 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001549 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301550 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1551 if (new_ptl != old_ptl)
1552 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001553 spin_unlock(old_ptl);
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001554 }
1555out:
1556 return ret;
1557}
1558
Mel Gormanf123d742013-10-07 11:28:49 +01001559/*
1560 * Returns
1561 * - 0 if PMD could not be locked
1562 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1563 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1564 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001565int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001566 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001567{
1568 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001569 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001570 int ret = 0;
1571
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001572 if (__pmd_trans_huge_lock(pmd, vma, &ptl) == 1) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001573 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001574 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001575 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001576
1577 /*
1578 * Avoid trapping faults against the zero page. The read-only
1579 * data is likely to be read-cached on the local CPU and
1580 * local/remote hits to the zero page are not interesting.
1581 */
1582 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1583 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001584 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001585 }
1586
Mel Gorman10c10452015-02-12 14:58:44 -08001587 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001588 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001589 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001590 if (preserve_write)
1591 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001592 ret = HPAGE_PMD_NR;
1593 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001594 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001595 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001596 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001597 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001598
1599 return ret;
1600}
1601
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001602/*
1603 * Returns 1 if a given pmd maps a stable (not under splitting) thp.
1604 * Returns -1 if it maps a thp under splitting. Returns 0 otherwise.
1605 *
1606 * Note that if it returns 1, this routine returns without unlocking page
1607 * table locks. So callers must unlock them.
1608 */
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001609int __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
1610 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001611{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001612 *ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001613 if (likely(pmd_trans_huge(*pmd))) {
1614 if (unlikely(pmd_trans_splitting(*pmd))) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001615 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001616 wait_split_huge_page(vma->anon_vma, pmd);
1617 return -1;
1618 } else {
1619 /* Thp mapped by 'pmd' is stable, so we can
1620 * handle it as it is. */
1621 return 1;
1622 }
1623 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001624 spin_unlock(*ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001625 return 0;
1626}
1627
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001628/*
1629 * This function returns whether a given @page is mapped onto the @address
1630 * in the virtual space of @mm.
1631 *
1632 * When it's true, this function returns *pmd with holding the page table lock
1633 * and passing it back to the caller via @ptl.
1634 * If it's false, returns NULL without holding the page table lock.
1635 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001636pmd_t *page_check_address_pmd(struct page *page,
1637 struct mm_struct *mm,
1638 unsigned long address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001639 enum page_check_address_pmd_flag flag,
1640 spinlock_t **ptl)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001641{
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001642 pgd_t *pgd;
1643 pud_t *pud;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001644 pmd_t *pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001645
1646 if (address & ~HPAGE_PMD_MASK)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001647 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001648
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001649 pgd = pgd_offset(mm, address);
1650 if (!pgd_present(*pgd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001651 return NULL;
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001652 pud = pud_offset(pgd, address);
1653 if (!pud_present(*pud))
1654 return NULL;
1655 pmd = pmd_offset(pud, address);
1656
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001657 *ptl = pmd_lock(mm, pmd);
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001658 if (!pmd_present(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001659 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001660 if (pmd_page(*pmd) != page)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001661 goto unlock;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08001662 /*
1663 * split_vma() may create temporary aliased mappings. There is
1664 * no risk as long as all huge pmd are found and have their
1665 * splitting bit set before __split_huge_page_refcount
1666 * runs. Finding the same huge pmd more than once during the
1667 * same rmap walk is not a problem.
1668 */
1669 if (flag == PAGE_CHECK_ADDRESS_PMD_NOTSPLITTING_FLAG &&
1670 pmd_trans_splitting(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001671 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001672 if (pmd_trans_huge(*pmd)) {
1673 VM_BUG_ON(flag == PAGE_CHECK_ADDRESS_PMD_SPLITTING_FLAG &&
1674 !pmd_trans_splitting(*pmd));
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001675 return pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001676 }
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001677unlock:
1678 spin_unlock(*ptl);
1679 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001680}
1681
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001682#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001683
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001684int hugepage_madvise(struct vm_area_struct *vma,
1685 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001686{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001687 switch (advice) {
1688 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001689#ifdef CONFIG_S390
1690 /*
1691 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1692 * can't handle this properly after s390_enable_sie, so we simply
1693 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1694 */
1695 if (mm_has_pgste(vma->vm_mm))
1696 return 0;
1697#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001698 /*
1699 * Be somewhat over-protective like KSM for now!
1700 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001701 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001702 return -EINVAL;
1703 *vm_flags &= ~VM_NOHUGEPAGE;
1704 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001705 /*
1706 * If the vma become good for khugepaged to scan,
1707 * register it here without waiting a page fault that
1708 * may not happen any time soon.
1709 */
David Rientjes6d50e602014-10-29 14:50:31 -07001710 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001711 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001712 break;
1713 case MADV_NOHUGEPAGE:
1714 /*
1715 * Be somewhat over-protective like KSM for now!
1716 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001717 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001718 return -EINVAL;
1719 *vm_flags &= ~VM_HUGEPAGE;
1720 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001721 /*
1722 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1723 * this vma even if we leave the mm registered in khugepaged if
1724 * it got registered before VM_NOHUGEPAGE was set.
1725 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001726 break;
1727 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001728
1729 return 0;
1730}
1731
Andrea Arcangeliba761492011-01-13 15:46:58 -08001732static int __init khugepaged_slab_init(void)
1733{
1734 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1735 sizeof(struct mm_slot),
1736 __alignof__(struct mm_slot), 0, NULL);
1737 if (!mm_slot_cache)
1738 return -ENOMEM;
1739
1740 return 0;
1741}
1742
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001743static void __init khugepaged_slab_exit(void)
1744{
1745 kmem_cache_destroy(mm_slot_cache);
1746}
1747
Andrea Arcangeliba761492011-01-13 15:46:58 -08001748static inline struct mm_slot *alloc_mm_slot(void)
1749{
1750 if (!mm_slot_cache) /* initialization failed */
1751 return NULL;
1752 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1753}
1754
1755static inline void free_mm_slot(struct mm_slot *mm_slot)
1756{
1757 kmem_cache_free(mm_slot_cache, mm_slot);
1758}
1759
Andrea Arcangeliba761492011-01-13 15:46:58 -08001760static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1761{
1762 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001763
Sasha Levinb67bfe02013-02-27 17:06:00 -08001764 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001765 if (mm == mm_slot->mm)
1766 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001767
Andrea Arcangeliba761492011-01-13 15:46:58 -08001768 return NULL;
1769}
1770
1771static void insert_to_mm_slots_hash(struct mm_struct *mm,
1772 struct mm_slot *mm_slot)
1773{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001774 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001775 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001776}
1777
1778static inline int khugepaged_test_exit(struct mm_struct *mm)
1779{
1780 return atomic_read(&mm->mm_users) == 0;
1781}
1782
1783int __khugepaged_enter(struct mm_struct *mm)
1784{
1785 struct mm_slot *mm_slot;
1786 int wakeup;
1787
1788 mm_slot = alloc_mm_slot();
1789 if (!mm_slot)
1790 return -ENOMEM;
1791
1792 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001793 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001794 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1795 free_mm_slot(mm_slot);
1796 return 0;
1797 }
1798
1799 spin_lock(&khugepaged_mm_lock);
1800 insert_to_mm_slots_hash(mm, mm_slot);
1801 /*
1802 * Insert just behind the scanning cursor, to let the area settle
1803 * down a little.
1804 */
1805 wakeup = list_empty(&khugepaged_scan.mm_head);
1806 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1807 spin_unlock(&khugepaged_mm_lock);
1808
1809 atomic_inc(&mm->mm_count);
1810 if (wakeup)
1811 wake_up_interruptible(&khugepaged_wait);
1812
1813 return 0;
1814}
1815
David Rientjes6d50e602014-10-29 14:50:31 -07001816int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1817 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001818{
1819 unsigned long hstart, hend;
1820 if (!vma->anon_vma)
1821 /*
1822 * Not yet faulted in so we will register later in the
1823 * page fault if needed.
1824 */
1825 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001826 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001827 /* khugepaged not yet working on file or special mappings */
1828 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07001829 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001830 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1831 hend = vma->vm_end & HPAGE_PMD_MASK;
1832 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001833 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001834 return 0;
1835}
1836
1837void __khugepaged_exit(struct mm_struct *mm)
1838{
1839 struct mm_slot *mm_slot;
1840 int free = 0;
1841
1842 spin_lock(&khugepaged_mm_lock);
1843 mm_slot = get_mm_slot(mm);
1844 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001845 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001846 list_del(&mm_slot->mm_node);
1847 free = 1;
1848 }
Chris Wrightd788e802011-07-25 17:12:14 -07001849 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001850
1851 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001852 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1853 free_mm_slot(mm_slot);
1854 mmdrop(mm);
1855 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001856 /*
1857 * This is required to serialize against
1858 * khugepaged_test_exit() (which is guaranteed to run
1859 * under mmap sem read mode). Stop here (after we
1860 * return all pagetables will be destroyed) until
1861 * khugepaged has finished working on the pagetables
1862 * under the mmap_sem.
1863 */
1864 down_write(&mm->mmap_sem);
1865 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001866 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001867}
1868
1869static void release_pte_page(struct page *page)
1870{
1871 /* 0 stands for page_is_file_cache(page) == false */
1872 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1873 unlock_page(page);
1874 putback_lru_page(page);
1875}
1876
1877static void release_pte_pages(pte_t *pte, pte_t *_pte)
1878{
1879 while (--_pte >= pte) {
1880 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001881 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001882 release_pte_page(pte_page(pteval));
1883 }
1884}
1885
Andrea Arcangeliba761492011-01-13 15:46:58 -08001886static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1887 unsigned long address,
1888 pte_t *pte)
1889{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001890 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001891 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001892 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001893 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001894
Andrea Arcangeliba761492011-01-13 15:46:58 -08001895 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1896 _pte++, address += PAGE_SIZE) {
1897 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07001898 if (pte_none(pteval) || (pte_present(pteval) &&
1899 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07001900 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001901 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001902 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001903 } else {
1904 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001906 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001907 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001908 if (!pte_present(pteval)) {
1909 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001910 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001911 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001912 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001913 if (unlikely(!page)) {
1914 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001915 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001916 }
Bob Liu344aa352012-12-11 16:00:34 -08001917
Sasha Levin309381fea2014-01-23 15:52:54 -08001918 VM_BUG_ON_PAGE(PageCompound(page), page);
1919 VM_BUG_ON_PAGE(!PageAnon(page), page);
1920 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001921
Andrea Arcangeliba761492011-01-13 15:46:58 -08001922 /*
1923 * We can do it before isolate_lru_page because the
1924 * page can't be freed from under us. NOTE: PG_lock
1925 * is needed to serialize against split_huge_page
1926 * when invoked from the VM.
1927 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001928 if (!trylock_page(page)) {
1929 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001930 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001931 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08001932
1933 /*
1934 * cannot use mapcount: can't collapse if there's a gup pin.
1935 * The page must only be referenced by the scanned process
1936 * and page swap cache.
1937 */
1938 if (page_count(page) != 1 + !!PageSwapCache(page)) {
1939 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001940 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001941 goto out;
1942 }
1943 if (pte_write(pteval)) {
1944 writable = true;
1945 } else {
1946 if (PageSwapCache(page) && !reuse_swap_page(page)) {
1947 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001948 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001949 goto out;
1950 }
1951 /*
1952 * Page is not in the swap cache. It can be collapsed
1953 * into a THP.
1954 */
1955 }
1956
Andrea Arcangeliba761492011-01-13 15:46:58 -08001957 /*
1958 * Isolate the page to avoid collapsing an hugepage
1959 * currently in use by the VM.
1960 */
1961 if (isolate_lru_page(page)) {
1962 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001963 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001964 goto out;
1965 }
1966 /* 0 stands for page_is_file_cache(page) == false */
1967 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08001968 VM_BUG_ON_PAGE(!PageLocked(page), page);
1969 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001970
1971 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001972 if (pte_young(pteval) ||
1973 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001974 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08001975 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001976 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001977 if (likely(writable)) {
1978 if (likely(referenced)) {
1979 result = SCAN_SUCCEED;
1980 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
1981 referenced, writable, result);
1982 return 1;
1983 }
1984 } else {
1985 result = SCAN_PAGE_RO;
1986 }
1987
Andrea Arcangeliba761492011-01-13 15:46:58 -08001988out:
Bob Liu344aa352012-12-11 16:00:34 -08001989 release_pte_pages(pte, _pte);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001990 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
1991 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08001992 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001993}
1994
1995static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1996 struct vm_area_struct *vma,
1997 unsigned long address,
1998 spinlock_t *ptl)
1999{
2000 pte_t *_pte;
2001 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2002 pte_t pteval = *_pte;
2003 struct page *src_page;
2004
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002005 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002006 clear_user_highpage(page, address);
2007 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002008 if (is_zero_pfn(pte_pfn(pteval))) {
2009 /*
2010 * ptl mostly unnecessary.
2011 */
2012 spin_lock(ptl);
2013 /*
2014 * paravirt calls inside pte_clear here are
2015 * superfluous.
2016 */
2017 pte_clear(vma->vm_mm, address, _pte);
2018 spin_unlock(ptl);
2019 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002020 } else {
2021 src_page = pte_page(pteval);
2022 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002023 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002024 release_pte_page(src_page);
2025 /*
2026 * ptl mostly unnecessary, but preempt has to
2027 * be disabled to update the per-cpu stats
2028 * inside page_remove_rmap().
2029 */
2030 spin_lock(ptl);
2031 /*
2032 * paravirt calls inside pte_clear here are
2033 * superfluous.
2034 */
2035 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002036 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002037 spin_unlock(ptl);
2038 free_page_and_swap_cache(src_page);
2039 }
2040
2041 address += PAGE_SIZE;
2042 page++;
2043 }
2044}
2045
Xiao Guangrong26234f32012-10-08 16:29:51 -07002046static void khugepaged_alloc_sleep(void)
2047{
Petr Mladekbde43c62015-09-08 15:04:05 -07002048 DEFINE_WAIT(wait);
2049
2050 add_wait_queue(&khugepaged_wait, &wait);
2051 freezable_schedule_timeout_interruptible(
2052 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2053 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002054}
2055
Bob Liu9f1b8682013-11-12 15:07:37 -08002056static int khugepaged_node_load[MAX_NUMNODES];
2057
David Rientjes14a4e212014-08-06 16:07:29 -07002058static bool khugepaged_scan_abort(int nid)
2059{
2060 int i;
2061
2062 /*
2063 * If zone_reclaim_mode is disabled, then no extra effort is made to
2064 * allocate memory locally.
2065 */
2066 if (!zone_reclaim_mode)
2067 return false;
2068
2069 /* If there is a count for this node already, it must be acceptable */
2070 if (khugepaged_node_load[nid])
2071 return false;
2072
2073 for (i = 0; i < MAX_NUMNODES; i++) {
2074 if (!khugepaged_node_load[i])
2075 continue;
2076 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2077 return true;
2078 }
2079 return false;
2080}
2081
Xiao Guangrong26234f32012-10-08 16:29:51 -07002082#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002083static int khugepaged_find_target_node(void)
2084{
2085 static int last_khugepaged_target_node = NUMA_NO_NODE;
2086 int nid, target_node = 0, max_value = 0;
2087
2088 /* find first node with max normal pages hit */
2089 for (nid = 0; nid < MAX_NUMNODES; nid++)
2090 if (khugepaged_node_load[nid] > max_value) {
2091 max_value = khugepaged_node_load[nid];
2092 target_node = nid;
2093 }
2094
2095 /* do some balance if several nodes have the same hit record */
2096 if (target_node <= last_khugepaged_target_node)
2097 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2098 nid++)
2099 if (max_value == khugepaged_node_load[nid]) {
2100 target_node = nid;
2101 break;
2102 }
2103
2104 last_khugepaged_target_node = target_node;
2105 return target_node;
2106}
2107
Xiao Guangrong26234f32012-10-08 16:29:51 -07002108static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2109{
2110 if (IS_ERR(*hpage)) {
2111 if (!*wait)
2112 return false;
2113
2114 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002115 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002116 khugepaged_alloc_sleep();
2117 } else if (*hpage) {
2118 put_page(*hpage);
2119 *hpage = NULL;
2120 }
2121
2122 return true;
2123}
2124
Michal Hocko3b363692015-04-15 16:13:29 -07002125static struct page *
2126khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002127 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002128{
Sasha Levin309381fea2014-01-23 15:52:54 -08002129 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002130
Xiao Guangrong26234f32012-10-08 16:29:51 -07002131 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002132 * Before allocating the hugepage, release the mmap_sem read lock.
2133 * The allocation can take potentially a long time if it involves
2134 * sync compaction, and we do not need to hold the mmap_sem during
2135 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002136 */
2137 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002138
Vlastimil Babka96db8002015-09-08 15:03:50 -07002139 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002140 if (unlikely(!*hpage)) {
2141 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2142 *hpage = ERR_PTR(-ENOMEM);
2143 return NULL;
2144 }
2145
2146 count_vm_event(THP_COLLAPSE_ALLOC);
2147 return *hpage;
2148}
2149#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002150static int khugepaged_find_target_node(void)
2151{
2152 return 0;
2153}
2154
Bob Liu10dc4152013-11-12 15:07:35 -08002155static inline struct page *alloc_hugepage(int defrag)
2156{
2157 return alloc_pages(alloc_hugepage_gfpmask(defrag, 0),
2158 HPAGE_PMD_ORDER);
2159}
2160
Xiao Guangrong26234f32012-10-08 16:29:51 -07002161static struct page *khugepaged_alloc_hugepage(bool *wait)
2162{
2163 struct page *hpage;
2164
2165 do {
2166 hpage = alloc_hugepage(khugepaged_defrag());
2167 if (!hpage) {
2168 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2169 if (!*wait)
2170 return NULL;
2171
2172 *wait = false;
2173 khugepaged_alloc_sleep();
2174 } else
2175 count_vm_event(THP_COLLAPSE_ALLOC);
2176 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2177
2178 return hpage;
2179}
2180
2181static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2182{
2183 if (!*hpage)
2184 *hpage = khugepaged_alloc_hugepage(wait);
2185
2186 if (unlikely(!*hpage))
2187 return false;
2188
2189 return true;
2190}
2191
Michal Hocko3b363692015-04-15 16:13:29 -07002192static struct page *
2193khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002194 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002195{
2196 up_read(&mm->mmap_sem);
2197 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002198
Xiao Guangrong26234f32012-10-08 16:29:51 -07002199 return *hpage;
2200}
2201#endif
2202
Bob Liufa475e52012-12-11 16:00:39 -08002203static bool hugepage_vma_check(struct vm_area_struct *vma)
2204{
2205 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2206 (vma->vm_flags & VM_NOHUGEPAGE))
2207 return false;
Kirill A. Shutemov7479df62016-01-15 16:52:35 -08002208 if (vma->vm_flags & VM_LOCKED)
2209 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002210 if (!vma->anon_vma || vma->vm_ops)
2211 return false;
2212 if (is_vma_temporary_stack(vma))
2213 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002214 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002215 return true;
2216}
2217
Andrea Arcangeliba761492011-01-13 15:46:58 -08002218static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002219 unsigned long address,
2220 struct page **hpage,
2221 struct vm_area_struct *vma,
2222 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002223{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002224 pmd_t *pmd, _pmd;
2225 pte_t *pte;
2226 pgtable_t pgtable;
2227 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002228 spinlock_t *pmd_ptl, *pte_ptl;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002229 int isolated, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002230 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002231 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002232 unsigned long mmun_start; /* For mmu_notifiers */
2233 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002234 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002235
2236 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002237
Michal Hocko3b363692015-04-15 16:13:29 -07002238 /* Only allocate from the target node */
2239 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2240 __GFP_THISNODE;
2241
Xiao Guangrong26234f32012-10-08 16:29:51 -07002242 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002243 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002244 if (!new_page) {
2245 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2246 goto out_nolock;
2247 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002248
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002249 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002250 result = SCAN_CGROUP_CHARGE_FAIL;
2251 goto out_nolock;
2252 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002253
2254 /*
2255 * Prevent all access to pagetables with the exception of
2256 * gup_fast later hanlded by the ptep_clear_flush and the VM
2257 * handled by the anon_vma lock + PG_lock.
2258 */
2259 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002260 if (unlikely(khugepaged_test_exit(mm))) {
2261 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002262 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002263 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002264
2265 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002266 if (!vma) {
2267 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002268 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002269 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002270 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2271 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002272 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2273 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002274 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002275 }
2276 if (!hugepage_vma_check(vma)) {
2277 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002278 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002279 }
Bob Liu62190492012-12-11 16:00:37 -08002280 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002281 if (!pmd) {
2282 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002283 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002284 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002285
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002286 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002287
2288 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002289 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002290
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002291 mmun_start = address;
2292 mmun_end = address + HPAGE_PMD_SIZE;
2293 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002294 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002295 /*
2296 * After this gup_fast can't run anymore. This also removes
2297 * any huge TLB entry from the CPU so we won't allow
2298 * huge and small TLB entries for the same virtual address
2299 * to avoid the risk of CPU bugs in that area.
2300 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002301 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002302 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002303 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002304
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002305 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002306 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002307 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002308
2309 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002310 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002311 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002313 /*
2314 * We can only use set_pmd_at when establishing
2315 * hugepmds and never for establishing regular pmds that
2316 * points to regular pagetables. Use pmd_populate for that
2317 */
2318 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002319 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002320 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002321 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002322 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002323 }
2324
2325 /*
2326 * All pages are isolated and locked so anon_vma rmap
2327 * can't run anymore.
2328 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002329 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002330
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002331 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002332 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002333 __SetPageUptodate(new_page);
2334 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002335
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002336 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2337 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002338
2339 /*
2340 * spin_lock() below is not the equivalent of smp_wmb(), so
2341 * this is needed to avoid the copy_huge_page writes to become
2342 * visible after the set_pmd_at() write.
2343 */
2344 smp_wmb();
2345
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002346 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002347 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002348 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002349 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002350 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002351 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002352 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002353 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002354 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002355
2356 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002357
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002359 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002360out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002361 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002362 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002363 return;
2364
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002365out_nolock:
2366 trace_mm_collapse_huge_page(mm, isolated, result);
2367 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002368out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002369 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002370 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002371}
2372
2373static int khugepaged_scan_pmd(struct mm_struct *mm,
2374 struct vm_area_struct *vma,
2375 unsigned long address,
2376 struct page **hpage)
2377{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002378 pmd_t *pmd;
2379 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002380 int ret = 0, none_or_zero = 0, result = 0;
2381 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002382 unsigned long _address;
2383 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002384 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002385 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002386
2387 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2388
Bob Liu62190492012-12-11 16:00:37 -08002389 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002390 if (!pmd) {
2391 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002392 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002393 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002394
Bob Liu9f1b8682013-11-12 15:07:37 -08002395 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002396 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2397 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2398 _pte++, _address += PAGE_SIZE) {
2399 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002400 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002401 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002402 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002403 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002404 } else {
2405 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002406 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002407 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002408 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002409 if (!pte_present(pteval)) {
2410 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002411 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002412 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002413 if (pte_write(pteval))
2414 writable = true;
2415
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002417 if (unlikely(!page)) {
2418 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002419 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002420 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002421
2422 /* TODO: teach khugepaged to collapse THP mapped with pte */
2423 if (PageCompound(page)) {
2424 result = SCAN_PAGE_COMPOUND;
2425 goto out_unmap;
2426 }
2427
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002428 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002429 * Record which node the original page is from and save this
2430 * information to khugepaged_node_load[].
2431 * Khupaged will allocate hugepage from the node has the max
2432 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002433 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002434 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002435 if (khugepaged_scan_abort(node)) {
2436 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002437 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002438 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002439 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002440 if (!PageLRU(page)) {
2441 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002442 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002443 }
2444 if (PageLocked(page)) {
2445 result = SCAN_PAGE_LOCK;
2446 goto out_unmap;
2447 }
2448 if (!PageAnon(page)) {
2449 result = SCAN_PAGE_ANON;
2450 goto out_unmap;
2451 }
2452
Ebru Akagunduz10359212015-02-11 15:28:28 -08002453 /*
2454 * cannot use mapcount: can't collapse if there's a gup pin.
2455 * The page must only be referenced by the scanned process
2456 * and page swap cache.
2457 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002458 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2459 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002460 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002461 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002462 if (pte_young(pteval) ||
2463 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002464 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002465 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002466 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002467 if (writable) {
2468 if (referenced) {
2469 result = SCAN_SUCCEED;
2470 ret = 1;
2471 } else {
2472 result = SCAN_NO_REFERENCED_PAGE;
2473 }
2474 } else {
2475 result = SCAN_PAGE_RO;
2476 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002477out_unmap:
2478 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002479 if (ret) {
2480 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002481 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002482 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002483 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484out:
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002485 trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2486 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002487 return ret;
2488}
2489
2490static void collect_mm_slot(struct mm_slot *mm_slot)
2491{
2492 struct mm_struct *mm = mm_slot->mm;
2493
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002494 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002495
2496 if (khugepaged_test_exit(mm)) {
2497 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002498 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002499 list_del(&mm_slot->mm_node);
2500
2501 /*
2502 * Not strictly needed because the mm exited already.
2503 *
2504 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2505 */
2506
2507 /* khugepaged_mm_lock actually not necessary for the below */
2508 free_mm_slot(mm_slot);
2509 mmdrop(mm);
2510 }
2511}
2512
2513static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2514 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002515 __releases(&khugepaged_mm_lock)
2516 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002517{
2518 struct mm_slot *mm_slot;
2519 struct mm_struct *mm;
2520 struct vm_area_struct *vma;
2521 int progress = 0;
2522
2523 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002524 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002525
2526 if (khugepaged_scan.mm_slot)
2527 mm_slot = khugepaged_scan.mm_slot;
2528 else {
2529 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2530 struct mm_slot, mm_node);
2531 khugepaged_scan.address = 0;
2532 khugepaged_scan.mm_slot = mm_slot;
2533 }
2534 spin_unlock(&khugepaged_mm_lock);
2535
2536 mm = mm_slot->mm;
2537 down_read(&mm->mmap_sem);
2538 if (unlikely(khugepaged_test_exit(mm)))
2539 vma = NULL;
2540 else
2541 vma = find_vma(mm, khugepaged_scan.address);
2542
2543 progress++;
2544 for (; vma; vma = vma->vm_next) {
2545 unsigned long hstart, hend;
2546
2547 cond_resched();
2548 if (unlikely(khugepaged_test_exit(mm))) {
2549 progress++;
2550 break;
2551 }
Bob Liufa475e52012-12-11 16:00:39 -08002552 if (!hugepage_vma_check(vma)) {
2553skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002554 progress++;
2555 continue;
2556 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002557 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2558 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002559 if (hstart >= hend)
2560 goto skip;
2561 if (khugepaged_scan.address > hend)
2562 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002563 if (khugepaged_scan.address < hstart)
2564 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002565 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002566
2567 while (khugepaged_scan.address < hend) {
2568 int ret;
2569 cond_resched();
2570 if (unlikely(khugepaged_test_exit(mm)))
2571 goto breakouterloop;
2572
2573 VM_BUG_ON(khugepaged_scan.address < hstart ||
2574 khugepaged_scan.address + HPAGE_PMD_SIZE >
2575 hend);
2576 ret = khugepaged_scan_pmd(mm, vma,
2577 khugepaged_scan.address,
2578 hpage);
2579 /* move to next address */
2580 khugepaged_scan.address += HPAGE_PMD_SIZE;
2581 progress += HPAGE_PMD_NR;
2582 if (ret)
2583 /* we released mmap_sem so break loop */
2584 goto breakouterloop_mmap_sem;
2585 if (progress >= pages)
2586 goto breakouterloop;
2587 }
2588 }
2589breakouterloop:
2590 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2591breakouterloop_mmap_sem:
2592
2593 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002594 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002595 /*
2596 * Release the current mm_slot if this mm is about to die, or
2597 * if we scanned all vmas of this mm.
2598 */
2599 if (khugepaged_test_exit(mm) || !vma) {
2600 /*
2601 * Make sure that if mm_users is reaching zero while
2602 * khugepaged runs here, khugepaged_exit will find
2603 * mm_slot not pointing to the exiting mm.
2604 */
2605 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2606 khugepaged_scan.mm_slot = list_entry(
2607 mm_slot->mm_node.next,
2608 struct mm_slot, mm_node);
2609 khugepaged_scan.address = 0;
2610 } else {
2611 khugepaged_scan.mm_slot = NULL;
2612 khugepaged_full_scans++;
2613 }
2614
2615 collect_mm_slot(mm_slot);
2616 }
2617
2618 return progress;
2619}
2620
2621static int khugepaged_has_work(void)
2622{
2623 return !list_empty(&khugepaged_scan.mm_head) &&
2624 khugepaged_enabled();
2625}
2626
2627static int khugepaged_wait_event(void)
2628{
2629 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002630 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002631}
2632
Xiao Guangrongd5169042012-10-08 16:29:48 -07002633static void khugepaged_do_scan(void)
2634{
2635 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002636 unsigned int progress = 0, pass_through_head = 0;
2637 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002638 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002639
2640 barrier(); /* write khugepaged_pages_to_scan to local stack */
2641
2642 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002643 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002644 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002645
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002646 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002647
Jiri Kosinacd092412015-06-24 16:56:07 -07002648 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002649 break;
2650
Andrea Arcangeliba761492011-01-13 15:46:58 -08002651 spin_lock(&khugepaged_mm_lock);
2652 if (!khugepaged_scan.mm_slot)
2653 pass_through_head++;
2654 if (khugepaged_has_work() &&
2655 pass_through_head < 2)
2656 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002657 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002658 else
2659 progress = pages;
2660 spin_unlock(&khugepaged_mm_lock);
2661 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002662
Xiao Guangrongd5169042012-10-08 16:29:48 -07002663 if (!IS_ERR_OR_NULL(hpage))
2664 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002665}
2666
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002667static void khugepaged_wait_work(void)
2668{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002669 if (khugepaged_has_work()) {
2670 if (!khugepaged_scan_sleep_millisecs)
2671 return;
2672
2673 wait_event_freezable_timeout(khugepaged_wait,
2674 kthread_should_stop(),
2675 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2676 return;
2677 }
2678
2679 if (khugepaged_enabled())
2680 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2681}
2682
Andrea Arcangeliba761492011-01-13 15:46:58 -08002683static int khugepaged(void *none)
2684{
2685 struct mm_slot *mm_slot;
2686
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002687 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002688 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002689
Xiao Guangrongb7231782012-10-08 16:29:54 -07002690 while (!kthread_should_stop()) {
2691 khugepaged_do_scan();
2692 khugepaged_wait_work();
2693 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002694
2695 spin_lock(&khugepaged_mm_lock);
2696 mm_slot = khugepaged_scan.mm_slot;
2697 khugepaged_scan.mm_slot = NULL;
2698 if (mm_slot)
2699 collect_mm_slot(mm_slot);
2700 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002701 return 0;
2702}
2703
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002704static void split_huge_pmd_address(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002705 unsigned long address)
2706{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002707 pgd_t *pgd;
2708 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002709 pmd_t *pmd;
2710
2711 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2712
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002713 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002714 if (!pgd_present(*pgd))
2715 return;
2716
2717 pud = pud_offset(pgd, address);
2718 if (!pud_present(*pud))
2719 return;
2720
2721 pmd = pmd_offset(pud, address);
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002722 if (!pmd_present(*pmd) || !pmd_trans_huge(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002723 return;
2724 /*
2725 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2726 * materialize from under us.
2727 */
Kirill A. Shutemovad0bed22016-01-15 16:52:53 -08002728 split_huge_pmd(vma, pmd, address);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002729}
2730
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002731void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002732 unsigned long start,
2733 unsigned long end,
2734 long adjust_next)
2735{
2736 /*
2737 * If the new start address isn't hpage aligned and it could
2738 * previously contain an hugepage: check if we need to split
2739 * an huge pmd.
2740 */
2741 if (start & ~HPAGE_PMD_MASK &&
2742 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2743 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002744 split_huge_pmd_address(vma, start);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002745
2746 /*
2747 * If the new end address isn't hpage aligned and it could
2748 * previously contain an hugepage: check if we need to split
2749 * an huge pmd.
2750 */
2751 if (end & ~HPAGE_PMD_MASK &&
2752 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2753 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002754 split_huge_pmd_address(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002755
2756 /*
2757 * If we're also updating the vma->vm_next->vm_start, if the new
2758 * vm_next->vm_start isn't page aligned and it could previously
2759 * contain an hugepage: check if we need to split an huge pmd.
2760 */
2761 if (adjust_next > 0) {
2762 struct vm_area_struct *next = vma->vm_next;
2763 unsigned long nstart = next->vm_start;
2764 nstart += adjust_next << PAGE_SHIFT;
2765 if (nstart & ~HPAGE_PMD_MASK &&
2766 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2767 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002768 split_huge_pmd_address(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002769 }
2770}