blob: 882b04449904a2f819fc51db609de9491dd209a3 [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>
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -080019#include <linux/swapops.h>
Matthew Wilcox4897c762015-09-08 14:58:45 -070020#include <linux/dax.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080021#include <linux/kthread.h>
22#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080023#include <linux/freezer.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080024#include <linux/mman.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020025#include <linux/pagemap.h>
Kirill A. Shutemov49071d42016-01-15 16:54:40 -080026#include <linux/debugfs.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000027#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080028#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070029#include <linux/userfaultfd_k.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070030#include <linux/page_idle.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080031
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080032#include <asm/tlb.h>
33#include <asm/pgalloc.h>
34#include "internal.h"
35
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080036enum scan_result {
37 SCAN_FAIL,
38 SCAN_SUCCEED,
39 SCAN_PMD_NULL,
40 SCAN_EXCEED_NONE_PTE,
41 SCAN_PTE_NON_PRESENT,
42 SCAN_PAGE_RO,
43 SCAN_NO_REFERENCED_PAGE,
44 SCAN_PAGE_NULL,
45 SCAN_SCAN_ABORT,
46 SCAN_PAGE_COUNT,
47 SCAN_PAGE_LRU,
48 SCAN_PAGE_LOCK,
49 SCAN_PAGE_ANON,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080050 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080051 SCAN_ANY_PROCESS,
52 SCAN_VMA_NULL,
53 SCAN_VMA_CHECK,
54 SCAN_ADDRESS_RANGE,
55 SCAN_SWAP_CACHE_PAGE,
56 SCAN_DEL_PAGE_LRU,
57 SCAN_ALLOC_HUGE_PAGE_FAIL,
58 SCAN_CGROUP_CHARGE_FAIL
59};
60
61#define CREATE_TRACE_POINTS
62#include <trace/events/huge_memory.h>
63
Andrea Arcangeliba761492011-01-13 15:46:58 -080064/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080065 * By default transparent hugepage support is disabled in order that avoid
66 * to risk increase the memory footprint of applications without a guaranteed
67 * benefit. When transparent hugepage support is enabled, is for all mappings,
68 * and khugepaged scans all mappings.
69 * Defrag is invoked by khugepaged hugepage allocations and by page faults
70 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080071 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080072unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080073#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080074 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080075#endif
76#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
77 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
78#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080079 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080080 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
81 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080082
83/* default scan 8*512 pte (or vmas) every 30 second */
84static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
85static unsigned int khugepaged_pages_collapsed;
86static unsigned int khugepaged_full_scans;
87static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
88/* during fragmentation poll the hugepage allocator once every minute */
89static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
90static struct task_struct *khugepaged_thread __read_mostly;
91static DEFINE_MUTEX(khugepaged_mutex);
92static DEFINE_SPINLOCK(khugepaged_mm_lock);
93static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
94/*
95 * default collapse hugepages if there is at least one pte mapped like
96 * it would have happened if the vma was large enough during page
97 * fault.
98 */
99static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
100
101static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800102static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700103static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800104
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800105#define MM_SLOTS_HASH_BITS 10
106static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
107
Andrea Arcangeliba761492011-01-13 15:46:58 -0800108static struct kmem_cache *mm_slot_cache __read_mostly;
109
110/**
111 * struct mm_slot - hash lookup from mm to mm_slot
112 * @hash: hash collision list
113 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
114 * @mm: the mm that this information is valid for
115 */
116struct mm_slot {
117 struct hlist_node hash;
118 struct list_head mm_node;
119 struct mm_struct *mm;
120};
121
122/**
123 * struct khugepaged_scan - cursor for scanning
124 * @mm_head: the head of the mm list to scan
125 * @mm_slot: the current mm_slot we are scanning
126 * @address: the next address inside that to be scanned
127 *
128 * There is only the one khugepaged_scan instance of this cursor structure.
129 */
130struct khugepaged_scan {
131 struct list_head mm_head;
132 struct mm_slot *mm_slot;
133 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700134};
135static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800136 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
137};
138
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800139static DEFINE_SPINLOCK(split_queue_lock);
140static LIST_HEAD(split_queue);
141static unsigned long split_queue_len;
142static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800143
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700144static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145{
146 struct zone *zone;
147 int nr_zones = 0;
148 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800149
Andrea Arcangelif0005652011-01-13 15:47:04 -0800150 for_each_populated_zone(zone)
151 nr_zones++;
152
Mel Gorman974a7862015-11-06 16:28:34 -0800153 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800154 recommended_min = pageblock_nr_pages * nr_zones * 2;
155
156 /*
157 * Make sure that on average at least two pageblocks are almost free
158 * of another type, one for a migratetype to fall back to and a
159 * second to avoid subsequent fallbacks of other types There are 3
160 * MIGRATE_TYPES we care about.
161 */
162 recommended_min += pageblock_nr_pages * nr_zones *
163 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
164
165 /* don't ever allow to reserve more than 5% of the lowmem */
166 recommended_min = min(recommended_min,
167 (unsigned long) nr_free_buffer_pages() / 20);
168 recommended_min <<= (PAGE_SHIFT-10);
169
Han Pingtian42aa83c2014-01-23 15:53:28 -0800170 if (recommended_min > min_free_kbytes) {
171 if (user_min_free_kbytes >= 0)
172 pr_info("raising min_free_kbytes from %d to %lu "
173 "to help transparent hugepage allocations\n",
174 min_free_kbytes, recommended_min);
175
Andrea Arcangelif0005652011-01-13 15:47:04 -0800176 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800177 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800179}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800180
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700181static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800182{
183 int err = 0;
184 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800185 if (!khugepaged_thread)
186 khugepaged_thread = kthread_run(khugepaged, NULL,
187 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530188 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700189 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800190 err = PTR_ERR(khugepaged_thread);
191 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700192 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800193 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700194
195 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800196 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800197
198 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700199 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700200 kthread_stop(khugepaged_thread);
201 khugepaged_thread = NULL;
202 }
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700203fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800204 return err;
205}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800206
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800207static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800208struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800209
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700210struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800211{
212 struct page *zero_page;
213retry:
214 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700215 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800216
217 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
218 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800219 if (!zero_page) {
220 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700221 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800222 }
223 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800224 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700225 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800226 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700227 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800228 goto retry;
229 }
230
231 /* We take additional reference here. It will be put back by shrinker */
232 atomic_set(&huge_zero_refcount, 2);
233 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700234 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800235}
236
237static void put_huge_zero_page(void)
238{
239 /*
240 * Counter should never go to zero here. Only shrinker can put
241 * last reference.
242 */
243 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
244}
245
Glauber Costa48896462013-08-28 10:18:15 +1000246static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
247 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800248{
Glauber Costa48896462013-08-28 10:18:15 +1000249 /* we can free zero page only if last reference remains */
250 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
251}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800252
Glauber Costa48896462013-08-28 10:18:15 +1000253static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
254 struct shrink_control *sc)
255{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800256 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700257 struct page *zero_page = xchg(&huge_zero_page, NULL);
258 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700259 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000260 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800261 }
262
263 return 0;
264}
265
266static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000267 .count_objects = shrink_huge_zero_page_count,
268 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800269 .seeks = DEFAULT_SEEKS,
270};
271
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800272#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800273
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800274static ssize_t double_flag_show(struct kobject *kobj,
275 struct kobj_attribute *attr, char *buf,
276 enum transparent_hugepage_flag enabled,
277 enum transparent_hugepage_flag req_madv)
278{
279 if (test_bit(enabled, &transparent_hugepage_flags)) {
280 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
281 return sprintf(buf, "[always] madvise never\n");
282 } else if (test_bit(req_madv, &transparent_hugepage_flags))
283 return sprintf(buf, "always [madvise] never\n");
284 else
285 return sprintf(buf, "always madvise [never]\n");
286}
287static ssize_t double_flag_store(struct kobject *kobj,
288 struct kobj_attribute *attr,
289 const char *buf, size_t count,
290 enum transparent_hugepage_flag enabled,
291 enum transparent_hugepage_flag req_madv)
292{
293 if (!memcmp("always", buf,
294 min(sizeof("always")-1, count))) {
295 set_bit(enabled, &transparent_hugepage_flags);
296 clear_bit(req_madv, &transparent_hugepage_flags);
297 } else if (!memcmp("madvise", buf,
298 min(sizeof("madvise")-1, count))) {
299 clear_bit(enabled, &transparent_hugepage_flags);
300 set_bit(req_madv, &transparent_hugepage_flags);
301 } else if (!memcmp("never", buf,
302 min(sizeof("never")-1, count))) {
303 clear_bit(enabled, &transparent_hugepage_flags);
304 clear_bit(req_madv, &transparent_hugepage_flags);
305 } else
306 return -EINVAL;
307
308 return count;
309}
310
311static ssize_t enabled_show(struct kobject *kobj,
312 struct kobj_attribute *attr, char *buf)
313{
314 return double_flag_show(kobj, attr, buf,
315 TRANSPARENT_HUGEPAGE_FLAG,
316 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
317}
318static ssize_t enabled_store(struct kobject *kobj,
319 struct kobj_attribute *attr,
320 const char *buf, size_t count)
321{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800322 ssize_t ret;
323
324 ret = double_flag_store(kobj, attr, buf, count,
325 TRANSPARENT_HUGEPAGE_FLAG,
326 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
327
328 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700329 int err;
330
331 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700332 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700333 mutex_unlock(&khugepaged_mutex);
334
Andrea Arcangeliba761492011-01-13 15:46:58 -0800335 if (err)
336 ret = err;
337 }
338
339 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800340}
341static struct kobj_attribute enabled_attr =
342 __ATTR(enabled, 0644, enabled_show, enabled_store);
343
344static ssize_t single_flag_show(struct kobject *kobj,
345 struct kobj_attribute *attr, char *buf,
346 enum transparent_hugepage_flag flag)
347{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700348 return sprintf(buf, "%d\n",
349 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800350}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700351
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800352static ssize_t single_flag_store(struct kobject *kobj,
353 struct kobj_attribute *attr,
354 const char *buf, size_t count,
355 enum transparent_hugepage_flag flag)
356{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700357 unsigned long value;
358 int ret;
359
360 ret = kstrtoul(buf, 10, &value);
361 if (ret < 0)
362 return ret;
363 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800364 return -EINVAL;
365
Ben Hutchingse27e6152011-04-14 15:22:21 -0700366 if (value)
367 set_bit(flag, &transparent_hugepage_flags);
368 else
369 clear_bit(flag, &transparent_hugepage_flags);
370
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800371 return count;
372}
373
374/*
375 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
376 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
377 * memory just to allocate one more hugepage.
378 */
379static ssize_t defrag_show(struct kobject *kobj,
380 struct kobj_attribute *attr, char *buf)
381{
382 return double_flag_show(kobj, attr, buf,
383 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
384 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
385}
386static ssize_t defrag_store(struct kobject *kobj,
387 struct kobj_attribute *attr,
388 const char *buf, size_t count)
389{
390 return double_flag_store(kobj, attr, buf, count,
391 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
392 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
393}
394static struct kobj_attribute defrag_attr =
395 __ATTR(defrag, 0644, defrag_show, defrag_store);
396
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800397static ssize_t use_zero_page_show(struct kobject *kobj,
398 struct kobj_attribute *attr, char *buf)
399{
400 return single_flag_show(kobj, attr, buf,
401 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
402}
403static ssize_t use_zero_page_store(struct kobject *kobj,
404 struct kobj_attribute *attr, const char *buf, size_t count)
405{
406 return single_flag_store(kobj, attr, buf, count,
407 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
408}
409static struct kobj_attribute use_zero_page_attr =
410 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800411#ifdef CONFIG_DEBUG_VM
412static ssize_t debug_cow_show(struct kobject *kobj,
413 struct kobj_attribute *attr, char *buf)
414{
415 return single_flag_show(kobj, attr, buf,
416 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
417}
418static ssize_t debug_cow_store(struct kobject *kobj,
419 struct kobj_attribute *attr,
420 const char *buf, size_t count)
421{
422 return single_flag_store(kobj, attr, buf, count,
423 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
424}
425static struct kobj_attribute debug_cow_attr =
426 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
427#endif /* CONFIG_DEBUG_VM */
428
429static struct attribute *hugepage_attr[] = {
430 &enabled_attr.attr,
431 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800432 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800433#ifdef CONFIG_DEBUG_VM
434 &debug_cow_attr.attr,
435#endif
436 NULL,
437};
438
439static struct attribute_group hugepage_attr_group = {
440 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800441};
442
443static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
444 struct kobj_attribute *attr,
445 char *buf)
446{
447 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
448}
449
450static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
451 struct kobj_attribute *attr,
452 const char *buf, size_t count)
453{
454 unsigned long msecs;
455 int err;
456
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700457 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800458 if (err || msecs > UINT_MAX)
459 return -EINVAL;
460
461 khugepaged_scan_sleep_millisecs = msecs;
462 wake_up_interruptible(&khugepaged_wait);
463
464 return count;
465}
466static struct kobj_attribute scan_sleep_millisecs_attr =
467 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
468 scan_sleep_millisecs_store);
469
470static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
471 struct kobj_attribute *attr,
472 char *buf)
473{
474 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
475}
476
477static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
478 struct kobj_attribute *attr,
479 const char *buf, size_t count)
480{
481 unsigned long msecs;
482 int err;
483
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700484 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800485 if (err || msecs > UINT_MAX)
486 return -EINVAL;
487
488 khugepaged_alloc_sleep_millisecs = msecs;
489 wake_up_interruptible(&khugepaged_wait);
490
491 return count;
492}
493static struct kobj_attribute alloc_sleep_millisecs_attr =
494 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
495 alloc_sleep_millisecs_store);
496
497static ssize_t pages_to_scan_show(struct kobject *kobj,
498 struct kobj_attribute *attr,
499 char *buf)
500{
501 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
502}
503static ssize_t pages_to_scan_store(struct kobject *kobj,
504 struct kobj_attribute *attr,
505 const char *buf, size_t count)
506{
507 int err;
508 unsigned long pages;
509
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700510 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800511 if (err || !pages || pages > UINT_MAX)
512 return -EINVAL;
513
514 khugepaged_pages_to_scan = pages;
515
516 return count;
517}
518static struct kobj_attribute pages_to_scan_attr =
519 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
520 pages_to_scan_store);
521
522static ssize_t pages_collapsed_show(struct kobject *kobj,
523 struct kobj_attribute *attr,
524 char *buf)
525{
526 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
527}
528static struct kobj_attribute pages_collapsed_attr =
529 __ATTR_RO(pages_collapsed);
530
531static ssize_t full_scans_show(struct kobject *kobj,
532 struct kobj_attribute *attr,
533 char *buf)
534{
535 return sprintf(buf, "%u\n", khugepaged_full_scans);
536}
537static struct kobj_attribute full_scans_attr =
538 __ATTR_RO(full_scans);
539
540static ssize_t khugepaged_defrag_show(struct kobject *kobj,
541 struct kobj_attribute *attr, char *buf)
542{
543 return single_flag_show(kobj, attr, buf,
544 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
545}
546static ssize_t khugepaged_defrag_store(struct kobject *kobj,
547 struct kobj_attribute *attr,
548 const char *buf, size_t count)
549{
550 return single_flag_store(kobj, attr, buf, count,
551 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
552}
553static struct kobj_attribute khugepaged_defrag_attr =
554 __ATTR(defrag, 0644, khugepaged_defrag_show,
555 khugepaged_defrag_store);
556
557/*
558 * max_ptes_none controls if khugepaged should collapse hugepages over
559 * any unmapped ptes in turn potentially increasing the memory
560 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
561 * reduce the available free memory in the system as it
562 * runs. Increasing max_ptes_none will instead potentially reduce the
563 * free memory in the system during the khugepaged scan.
564 */
565static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
566 struct kobj_attribute *attr,
567 char *buf)
568{
569 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
570}
571static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
572 struct kobj_attribute *attr,
573 const char *buf, size_t count)
574{
575 int err;
576 unsigned long max_ptes_none;
577
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700578 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800579 if (err || max_ptes_none > HPAGE_PMD_NR-1)
580 return -EINVAL;
581
582 khugepaged_max_ptes_none = max_ptes_none;
583
584 return count;
585}
586static struct kobj_attribute khugepaged_max_ptes_none_attr =
587 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
588 khugepaged_max_ptes_none_store);
589
590static struct attribute *khugepaged_attr[] = {
591 &khugepaged_defrag_attr.attr,
592 &khugepaged_max_ptes_none_attr.attr,
593 &pages_to_scan_attr.attr,
594 &pages_collapsed_attr.attr,
595 &full_scans_attr.attr,
596 &scan_sleep_millisecs_attr.attr,
597 &alloc_sleep_millisecs_attr.attr,
598 NULL,
599};
600
601static struct attribute_group khugepaged_attr_group = {
602 .attrs = khugepaged_attr,
603 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800604};
Shaohua Li569e5592012-01-12 17:19:11 -0800605
606static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
607{
608 int err;
609
610 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
611 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700612 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800613 return -ENOMEM;
614 }
615
616 err = sysfs_create_group(*hugepage_kobj, &hugepage_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 delete_obj;
620 }
621
622 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
623 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700624 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800625 goto remove_hp_group;
626 }
627
628 return 0;
629
630remove_hp_group:
631 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
632delete_obj:
633 kobject_put(*hugepage_kobj);
634 return err;
635}
636
637static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
638{
639 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
640 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
641 kobject_put(hugepage_kobj);
642}
643#else
644static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
645{
646 return 0;
647}
648
649static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
650{
651}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800652#endif /* CONFIG_SYSFS */
653
654static int __init hugepage_init(void)
655{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800656 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800657 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800658
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800659 if (!has_transparent_hugepage()) {
660 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800661 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800662 }
663
Shaohua Li569e5592012-01-12 17:19:11 -0800664 err = hugepage_init_sysfs(&hugepage_kobj);
665 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700666 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800667
668 err = khugepaged_slab_init();
669 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700670 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800671
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700672 err = register_shrinker(&huge_zero_page_shrinker);
673 if (err)
674 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800675 err = register_shrinker(&deferred_split_shrinker);
676 if (err)
677 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800678
Rik van Riel97562cd2011-01-13 15:47:12 -0800679 /*
680 * By default disable transparent hugepages on smaller systems,
681 * where the extra memory used could hurt more than TLB overhead
682 * is likely to save. The admin can still enable it through /sys.
683 */
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700684 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800685 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700686 return 0;
687 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800688
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700689 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700690 if (err)
691 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800692
Shaohua Li569e5592012-01-12 17:19:11 -0800693 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700694err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800695 unregister_shrinker(&deferred_split_shrinker);
696err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700697 unregister_shrinker(&huge_zero_page_shrinker);
698err_hzp_shrinker:
699 khugepaged_slab_exit();
700err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800701 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700702err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800703 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800704}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800705subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800706
707static int __init setup_transparent_hugepage(char *str)
708{
709 int ret = 0;
710 if (!str)
711 goto out;
712 if (!strcmp(str, "always")) {
713 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
714 &transparent_hugepage_flags);
715 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
716 &transparent_hugepage_flags);
717 ret = 1;
718 } else if (!strcmp(str, "madvise")) {
719 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
720 &transparent_hugepage_flags);
721 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
722 &transparent_hugepage_flags);
723 ret = 1;
724 } else if (!strcmp(str, "never")) {
725 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
726 &transparent_hugepage_flags);
727 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
728 &transparent_hugepage_flags);
729 ret = 1;
730 }
731out:
732 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700733 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800734 return ret;
735}
736__setup("transparent_hugepage=", setup_transparent_hugepage);
737
Mel Gormanb32967f2012-11-19 12:35:47 +0000738pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800739{
740 if (likely(vma->vm_flags & VM_WRITE))
741 pmd = pmd_mkwrite(pmd);
742 return pmd;
743}
744
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700745static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800746{
747 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700748 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800749 entry = pmd_mkhuge(entry);
750 return entry;
751}
752
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800753static inline struct list_head *page_deferred_list(struct page *page)
754{
755 /*
756 * ->lru in the tail pages is occupied by compound_head.
757 * Let's use ->mapping + ->index in the second tail page as list_head.
758 */
759 return (struct list_head *)&page[2].mapping;
760}
761
762void prep_transhuge_page(struct page *page)
763{
764 /*
765 * we use page->mapping and page->indexlru in second tail page
766 * as list_head: assuming THP order >= 2
767 */
768 BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
769
770 INIT_LIST_HEAD(page_deferred_list(page));
771 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
772}
773
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800774static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
775 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700776 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700777 struct page *page, gfp_t gfp,
778 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800779{
Johannes Weiner00501b52014-08-08 14:19:20 -0700780 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800781 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800782 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700783 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800784
Sasha Levin309381fea2014-01-23 15:52:54 -0800785 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700786
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800787 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700788 put_page(page);
789 count_vm_event(THP_FAULT_FALLBACK);
790 return VM_FAULT_FALLBACK;
791 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800792
Johannes Weiner00501b52014-08-08 14:19:20 -0700793 pgtable = pte_alloc_one(mm, haddr);
794 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800795 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700796 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700797 return VM_FAULT_OOM;
798 }
799
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800800 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700801 /*
802 * The memory barrier inside __SetPageUptodate makes sure that
803 * clear_huge_page writes become visible before the set_pmd_at()
804 * write.
805 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800806 __SetPageUptodate(page);
807
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800808 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800809 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800810 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800811 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800812 put_page(page);
813 pte_free(mm, pgtable);
814 } else {
815 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700816
817 /* Deliver the page fault to userland */
818 if (userfaultfd_missing(vma)) {
819 int ret;
820
821 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800822 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700823 put_page(page);
824 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700825 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700826 VM_UFFD_MISSING);
827 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
828 return ret;
829 }
830
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700831 entry = mk_huge_pmd(page, vma->vm_page_prot);
832 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800833 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800834 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700835 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700836 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800837 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800838 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800839 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800840 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700841 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800842 }
843
David Rientjesaa2e8782012-05-29 15:06:17 -0700844 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800845}
846
Andi Kleencc5d4622011-03-22 16:33:13 -0700847static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800848{
Mel Gorman71baba42015-11-06 16:28:28 -0800849 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800850}
851
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800852/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700853static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800854 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700855 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800856{
857 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700858 if (!pmd_none(*pmd))
859 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700860 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800861 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700862 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800863 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800864 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700865 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800866}
867
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800868int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
869 unsigned long address, pmd_t *pmd,
870 unsigned int flags)
871{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800872 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800873 struct page *page;
874 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800875
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700876 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700877 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700878 if (unlikely(anon_vma_prepare(vma)))
879 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700880 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700881 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200882 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700883 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800884 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700885 pgtable_t pgtable;
886 struct page *zero_page;
887 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700888 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700889 pgtable = pte_alloc_one(mm, haddr);
890 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800891 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700892 zero_page = get_huge_zero_page();
893 if (unlikely(!zero_page)) {
894 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700895 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700896 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700897 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800898 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700899 ret = 0;
900 set = false;
901 if (pmd_none(*pmd)) {
902 if (userfaultfd_missing(vma)) {
903 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700904 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700905 VM_UFFD_MISSING);
906 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
907 } else {
908 set_huge_zero_page(pgtable, mm, vma,
909 haddr, pmd,
910 zero_page);
911 spin_unlock(ptl);
912 set = true;
913 }
914 } else
915 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700916 if (!set) {
917 pte_free(mm, pgtable);
918 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800919 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700920 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800921 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800922 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
923 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700924 if (unlikely(!page)) {
925 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700926 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700927 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800928 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700929 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
930 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800931}
932
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700933static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700934 pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
935{
936 struct mm_struct *mm = vma->vm_mm;
937 pmd_t entry;
938 spinlock_t *ptl;
939
940 ptl = pmd_lock(mm, pmd);
941 if (pmd_none(*pmd)) {
942 entry = pmd_mkhuge(pfn_pmd(pfn, prot));
943 if (write) {
944 entry = pmd_mkyoung(pmd_mkdirty(entry));
945 entry = maybe_pmd_mkwrite(entry, vma);
946 }
947 set_pmd_at(mm, addr, pmd, entry);
948 update_mmu_cache_pmd(vma, addr, pmd);
949 }
950 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700951}
952
953int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
954 pmd_t *pmd, unsigned long pfn, bool write)
955{
956 pgprot_t pgprot = vma->vm_page_prot;
957 /*
958 * If we had pmd_special, we could avoid all these restrictions,
959 * but we need to be consistent with PTEs and architectures that
960 * can't support a 'special' bit.
961 */
962 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
963 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
964 (VM_PFNMAP|VM_MIXEDMAP));
965 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
966 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
967
968 if (addr < vma->vm_start || addr >= vma->vm_end)
969 return VM_FAULT_SIGBUS;
970 if (track_pfn_insert(vma, &pgprot, pfn))
971 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700972 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
973 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700974}
975
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800976int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
977 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
978 struct vm_area_struct *vma)
979{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800980 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800981 struct page *src_page;
982 pmd_t pmd;
983 pgtable_t pgtable;
984 int ret;
985
986 ret = -ENOMEM;
987 pgtable = pte_alloc_one(dst_mm, addr);
988 if (unlikely(!pgtable))
989 goto out;
990
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800991 dst_ptl = pmd_lock(dst_mm, dst_pmd);
992 src_ptl = pmd_lockptr(src_mm, src_pmd);
993 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800994
995 ret = -EAGAIN;
996 pmd = *src_pmd;
997 if (unlikely(!pmd_trans_huge(pmd))) {
998 pte_free(dst_mm, pgtable);
999 goto out_unlock;
1000 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001001 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001002 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001003 * under splitting since we don't split the page itself, only pmd to
1004 * a page table.
1005 */
1006 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001007 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001008 /*
1009 * get_huge_zero_page() will never allocate a new page here,
1010 * since we already have a zero page to copy. It just takes a
1011 * reference.
1012 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001013 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001014 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001015 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001016 ret = 0;
1017 goto out_unlock;
1018 }
Mel Gormande466bd2013-12-18 17:08:42 -08001019
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001020 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001021 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001022 get_page(src_page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001023 page_dup_rmap(src_page, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001024 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1025
1026 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1027 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001028 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001029 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001030 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001031
1032 ret = 0;
1033out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001034 spin_unlock(src_ptl);
1035 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001036out:
1037 return ret;
1038}
1039
Will Deacona1dd4502012-12-11 16:01:27 -08001040void huge_pmd_set_accessed(struct mm_struct *mm,
1041 struct vm_area_struct *vma,
1042 unsigned long address,
1043 pmd_t *pmd, pmd_t orig_pmd,
1044 int dirty)
1045{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001046 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001047 pmd_t entry;
1048 unsigned long haddr;
1049
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001050 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001051 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1052 goto unlock;
1053
1054 entry = pmd_mkyoung(orig_pmd);
1055 haddr = address & HPAGE_PMD_MASK;
1056 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1057 update_mmu_cache_pmd(vma, address, pmd);
1058
1059unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001060 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001061}
1062
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001063static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1064 struct vm_area_struct *vma,
1065 unsigned long address,
1066 pmd_t *pmd, pmd_t orig_pmd,
1067 struct page *page,
1068 unsigned long haddr)
1069{
Johannes Weiner00501b52014-08-08 14:19:20 -07001070 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001071 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001072 pgtable_t pgtable;
1073 pmd_t _pmd;
1074 int ret = 0, i;
1075 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001076 unsigned long mmun_start; /* For mmu_notifiers */
1077 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001078
1079 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1080 GFP_KERNEL);
1081 if (unlikely(!pages)) {
1082 ret |= VM_FAULT_OOM;
1083 goto out;
1084 }
1085
1086 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001087 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1088 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001089 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001090 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001091 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001092 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001093 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001094 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001095 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001096 memcg = (void *)page_private(pages[i]);
1097 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001098 mem_cgroup_cancel_charge(pages[i], memcg,
1099 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001100 put_page(pages[i]);
1101 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102 kfree(pages);
1103 ret |= VM_FAULT_OOM;
1104 goto out;
1105 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001106 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001107 }
1108
1109 for (i = 0; i < HPAGE_PMD_NR; i++) {
1110 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001111 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001112 __SetPageUptodate(pages[i]);
1113 cond_resched();
1114 }
1115
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001116 mmun_start = haddr;
1117 mmun_end = haddr + HPAGE_PMD_SIZE;
1118 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1119
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001120 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001121 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1122 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001123 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001124
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001125 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001126 /* leave pmd empty until pte is filled */
1127
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001128 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001129 pmd_populate(mm, &_pmd, pgtable);
1130
1131 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1132 pte_t *pte, entry;
1133 entry = mk_pte(pages[i], vma->vm_page_prot);
1134 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001135 memcg = (void *)page_private(pages[i]);
1136 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001137 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001138 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001139 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001140 pte = pte_offset_map(&_pmd, haddr);
1141 VM_BUG_ON(!pte_none(*pte));
1142 set_pte_at(mm, haddr, pte, entry);
1143 pte_unmap(pte);
1144 }
1145 kfree(pages);
1146
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147 smp_wmb(); /* make pte visible before pmd */
1148 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001149 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001150 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001151
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001152 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1153
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001154 ret |= VM_FAULT_WRITE;
1155 put_page(page);
1156
1157out:
1158 return ret;
1159
1160out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001161 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001162 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001163 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001164 memcg = (void *)page_private(pages[i]);
1165 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001166 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001167 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001168 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001169 kfree(pages);
1170 goto out;
1171}
1172
1173int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1174 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1175{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001176 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001177 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001178 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001179 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001180 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001181 unsigned long mmun_start; /* For mmu_notifiers */
1182 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07001183 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001184
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001185 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001186 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001187 haddr = address & HPAGE_PMD_MASK;
1188 if (is_huge_zero_pmd(orig_pmd))
1189 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001190 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001191 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1192 goto out_unlock;
1193
1194 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001195 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001196 /*
1197 * We can only reuse the page if nobody else maps the huge page or it's
1198 * part. We can do it by checking page_mapcount() on each sub-page, but
1199 * it's expensive.
1200 * The cheaper way is to check page_count() to be equal 1: every
1201 * mapcount takes page reference reference, so this way we can
1202 * guarantee, that the PMD is the only mapping.
1203 * This can give false negative if somebody pinned the page, but that's
1204 * fine.
1205 */
1206 if (page_mapcount(page) == 1 && page_count(page) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 pmd_t entry;
1208 entry = pmd_mkyoung(orig_pmd);
1209 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1210 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001211 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001212 ret |= VM_FAULT_WRITE;
1213 goto out_unlock;
1214 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001215 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001216 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001217alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001219 !transparent_hugepage_debug_cow()) {
Michal Hocko3b3636922015-04-15 16:13:29 -07001220 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1221 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001222 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001223 new_page = NULL;
1224
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001225 if (likely(new_page)) {
1226 prep_transhuge_page(new_page);
1227 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001228 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001229 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001230 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001231 } else {
1232 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1233 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001234 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001235 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001236 ret |= VM_FAULT_FALLBACK;
1237 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001238 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001239 }
David Rientjes17766dd2013-09-12 15:14:06 -07001240 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001241 goto out;
1242 }
1243
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001244 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1245 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001246 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001247 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001248 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001249 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001250 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001251 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001252 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001253 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001254 goto out;
1255 }
1256
David Rientjes17766dd2013-09-12 15:14:06 -07001257 count_vm_event(THP_FAULT_ALLOC);
1258
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001259 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001260 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1261 else
1262 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001263 __SetPageUptodate(new_page);
1264
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001265 mmun_start = haddr;
1266 mmun_end = haddr + HPAGE_PMD_SIZE;
1267 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1268
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001269 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001270 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001271 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001272 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001273 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001274 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001275 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001276 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001277 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001278 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001279 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1280 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001281 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001282 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001283 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001284 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001285 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001286 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001287 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001288 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001289 put_huge_zero_page();
1290 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001291 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001292 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001293 put_page(page);
1294 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001295 ret |= VM_FAULT_WRITE;
1296 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001297 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001298out_mn:
1299 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1300out:
1301 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001302out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001303 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001304 return ret;
1305}
1306
David Rientjesb676b292012-10-08 16:34:03 -07001307struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001308 unsigned long addr,
1309 pmd_t *pmd,
1310 unsigned int flags)
1311{
David Rientjesb676b292012-10-08 16:34:03 -07001312 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001313 struct page *page = NULL;
1314
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001315 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001316
1317 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1318 goto out;
1319
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001320 /* Avoid dumping huge zero page */
1321 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1322 return ERR_PTR(-EFAULT);
1323
Mel Gorman2b4847e2013-12-18 17:08:32 -08001324 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001325 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001326 goto out;
1327
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001328 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001329 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001330 if (flags & FOLL_TOUCH) {
1331 pmd_t _pmd;
1332 /*
1333 * We should set the dirty bit only for FOLL_WRITE but
1334 * for now the dirty bit in the pmd is meaningless.
1335 * And if the dirty bit will become meaningful and
1336 * we'll only set it with FOLL_WRITE, an atomic
1337 * set_bit will be required on the pmd to set the
1338 * young bit, instead of the current set_pmd_at.
1339 */
1340 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001341 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1342 pmd, _pmd, 1))
1343 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001344 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001345 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001346 /*
1347 * We don't mlock() pte-mapped THPs. This way we can avoid
1348 * leaking mlocked pages into non-VM_LOCKED VMAs.
1349 *
1350 * In most cases the pmd is the only mapping of the page as we
1351 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1352 * writable private mappings in populate_vma_page_range().
1353 *
1354 * The only scenario when we have the page shared here is if we
1355 * mlocking read-only mapping shared over fork(). We skip
1356 * mlocking such pages.
1357 */
1358 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1359 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001360 lru_add_drain();
1361 if (page->mapping)
1362 mlock_vma_page(page);
1363 unlock_page(page);
1364 }
1365 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001366 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001367 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001368 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001369 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001370
1371out:
1372 return page;
1373}
1374
Mel Gormand10e63f2012-10-25 14:16:31 +02001375/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001376int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1377 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001378{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001379 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001380 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001381 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001382 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001383 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001384 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001385 bool page_locked;
1386 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001387 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001388 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001389
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001390 /* A PROT_NONE fault should not end up here */
1391 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1392
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001393 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001394 if (unlikely(!pmd_same(pmd, *pmdp)))
1395 goto out_unlock;
1396
Mel Gormande466bd2013-12-18 17:08:42 -08001397 /*
1398 * If there are potential migrations, wait for completion and retry
1399 * without disrupting NUMA hinting information. Do not relock and
1400 * check_same as the page may no longer be mapped.
1401 */
1402 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001403 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001404 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001405 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001406 goto out;
1407 }
1408
Mel Gormand10e63f2012-10-25 14:16:31 +02001409 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001410 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001411 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001412 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001413 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001414 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001415 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001416 flags |= TNF_FAULT_LOCAL;
1417 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001418
Mel Gormanbea66fb2015-03-25 15:55:37 -07001419 /* See similar comment in do_numa_page for explanation */
1420 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001421 flags |= TNF_NO_GROUP;
1422
1423 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001424 * Acquire the page lock to serialise THP migrations but avoid dropping
1425 * page_table_lock if at all possible
1426 */
Mel Gormanb8916632013-10-07 11:28:44 +01001427 page_locked = trylock_page(page);
1428 target_nid = mpol_misplaced(page, vma, haddr);
1429 if (target_nid == -1) {
1430 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001431 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001432 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001433 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001434
Mel Gormande466bd2013-12-18 17:08:42 -08001435 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001436 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001437 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001438 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001439 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001440 goto out;
1441 }
1442
Mel Gorman2b4847e2013-12-18 17:08:32 -08001443 /*
1444 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1445 * to serialises splits
1446 */
Mel Gormanb8916632013-10-07 11:28:44 +01001447 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001448 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001449 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001450
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001451 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001452 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001453 if (unlikely(!pmd_same(pmd, *pmdp))) {
1454 unlock_page(page);
1455 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001456 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001457 goto out_unlock;
1458 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001459
Mel Gormanc3a489c2013-12-18 17:08:38 -08001460 /* Bail if we fail to protect against THP splits for any reason */
1461 if (unlikely(!anon_vma)) {
1462 put_page(page);
1463 page_nid = -1;
1464 goto clear_pmdnuma;
1465 }
1466
Mel Gormana54a4072013-10-07 11:28:46 +01001467 /*
1468 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001469 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001470 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001471 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001472 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001473 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001474 if (migrated) {
1475 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001476 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001477 } else
1478 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001479
Mel Gorman8191acb2013-10-07 11:28:45 +01001480 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001481clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001482 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001483 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001484 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001485 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001486 if (was_writable)
1487 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001488 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001489 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001490 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001491out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001492 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001493
1494out:
1495 if (anon_vma)
1496 page_unlock_anon_vma_read(anon_vma);
1497
Mel Gorman8191acb2013-10-07 11:28:45 +01001498 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001499 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001500
Mel Gormand10e63f2012-10-25 14:16:31 +02001501 return 0;
1502}
1503
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001504int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001505 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001506{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001507 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001508 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001509
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001510 if (!__pmd_trans_huge_lock(pmd, vma, &ptl))
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001511 return 0;
1512 /*
1513 * For architectures like ppc64 we look at deposited pgtable
1514 * when calling pmdp_huge_get_and_clear. So do the
1515 * pgtable_trans_huge_withdraw after finishing pmdp related
1516 * operations.
1517 */
1518 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1519 tlb->fullmm);
1520 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1521 if (vma_is_dax(vma)) {
1522 spin_unlock(ptl);
1523 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001524 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001525 } else if (is_huge_zero_pmd(orig_pmd)) {
1526 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1527 atomic_long_dec(&tlb->mm->nr_ptes);
1528 spin_unlock(ptl);
1529 put_huge_zero_page();
1530 } else {
1531 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001532 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001533 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1534 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1535 VM_BUG_ON_PAGE(!PageHead(page), page);
1536 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1537 atomic_long_dec(&tlb->mm->nr_ptes);
1538 spin_unlock(ptl);
1539 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001540 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001541 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001542}
1543
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001544bool move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001545 unsigned long old_addr,
1546 unsigned long new_addr, unsigned long old_end,
1547 pmd_t *old_pmd, pmd_t *new_pmd)
1548{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001549 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001550 pmd_t pmd;
1551
1552 struct mm_struct *mm = vma->vm_mm;
1553
1554 if ((old_addr & ~HPAGE_PMD_MASK) ||
1555 (new_addr & ~HPAGE_PMD_MASK) ||
1556 old_end - old_addr < HPAGE_PMD_SIZE ||
1557 (new_vma->vm_flags & VM_NOHUGEPAGE))
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001558 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001559
1560 /*
1561 * The destination pmd shouldn't be established, free_pgtables()
1562 * should have release it.
1563 */
1564 if (WARN_ON(!pmd_none(*new_pmd))) {
1565 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001566 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001567 }
1568
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001569 /*
1570 * We don't have to worry about the ordering of src and dst
1571 * ptlocks because exclusive mmap_sem prevents deadlock.
1572 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001573 if (__pmd_trans_huge_lock(old_pmd, vma, &old_ptl)) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001574 new_ptl = pmd_lockptr(mm, new_pmd);
1575 if (new_ptl != old_ptl)
1576 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001577 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001578 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001579
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301580 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1581 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001582 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1583 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001584 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301585 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1586 if (new_ptl != old_ptl)
1587 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001588 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001589 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001590 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001591 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001592}
1593
Mel Gormanf123d742013-10-07 11:28:49 +01001594/*
1595 * Returns
1596 * - 0 if PMD could not be locked
1597 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1598 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1599 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001600int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001601 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001602{
1603 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001604 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001605 int ret = 0;
1606
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001607 if (__pmd_trans_huge_lock(pmd, vma, &ptl)) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001608 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001609 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc012015-03-07 15:20:48 +00001610 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001611
1612 /*
1613 * Avoid trapping faults against the zero page. The read-only
1614 * data is likely to be read-cached on the local CPU and
1615 * local/remote hits to the zero page are not interesting.
1616 */
1617 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1618 spin_unlock(ptl);
Mel Gormanba68bc012015-03-07 15:20:48 +00001619 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001620 }
1621
Mel Gorman10c10452015-02-12 14:58:44 -08001622 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001623 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001624 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001625 if (preserve_write)
1626 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001627 ret = HPAGE_PMD_NR;
1628 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001629 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001630 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001631 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001632 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001633
1634 return ret;
1635}
1636
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001637/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001638 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001639 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001640 * Note that if it returns true, this routine returns without unlocking page
1641 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001642 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001643bool __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001644 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001645{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001646 *ptl = pmd_lock(vma->vm_mm, pmd);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001647 if (likely(pmd_trans_huge(*pmd)))
1648 return true;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001649 spin_unlock(*ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001650 return false;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001651}
1652
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001653#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001654
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001655int hugepage_madvise(struct vm_area_struct *vma,
1656 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001657{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001658 switch (advice) {
1659 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001660#ifdef CONFIG_S390
1661 /*
1662 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1663 * can't handle this properly after s390_enable_sie, so we simply
1664 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1665 */
1666 if (mm_has_pgste(vma->vm_mm))
1667 return 0;
1668#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001669 /*
1670 * Be somewhat over-protective like KSM for now!
1671 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001672 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001673 return -EINVAL;
1674 *vm_flags &= ~VM_NOHUGEPAGE;
1675 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001676 /*
1677 * If the vma become good for khugepaged to scan,
1678 * register it here without waiting a page fault that
1679 * may not happen any time soon.
1680 */
David Rientjes6d50e602014-10-29 14:50:31 -07001681 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001682 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001683 break;
1684 case MADV_NOHUGEPAGE:
1685 /*
1686 * Be somewhat over-protective like KSM for now!
1687 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001688 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001689 return -EINVAL;
1690 *vm_flags &= ~VM_HUGEPAGE;
1691 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001692 /*
1693 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1694 * this vma even if we leave the mm registered in khugepaged if
1695 * it got registered before VM_NOHUGEPAGE was set.
1696 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001697 break;
1698 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001699
1700 return 0;
1701}
1702
Andrea Arcangeliba761492011-01-13 15:46:58 -08001703static int __init khugepaged_slab_init(void)
1704{
1705 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1706 sizeof(struct mm_slot),
1707 __alignof__(struct mm_slot), 0, NULL);
1708 if (!mm_slot_cache)
1709 return -ENOMEM;
1710
1711 return 0;
1712}
1713
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001714static void __init khugepaged_slab_exit(void)
1715{
1716 kmem_cache_destroy(mm_slot_cache);
1717}
1718
Andrea Arcangeliba761492011-01-13 15:46:58 -08001719static inline struct mm_slot *alloc_mm_slot(void)
1720{
1721 if (!mm_slot_cache) /* initialization failed */
1722 return NULL;
1723 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1724}
1725
1726static inline void free_mm_slot(struct mm_slot *mm_slot)
1727{
1728 kmem_cache_free(mm_slot_cache, mm_slot);
1729}
1730
Andrea Arcangeliba761492011-01-13 15:46:58 -08001731static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1732{
1733 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001734
Sasha Levinb67bfe02013-02-27 17:06:00 -08001735 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001736 if (mm == mm_slot->mm)
1737 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001738
Andrea Arcangeliba761492011-01-13 15:46:58 -08001739 return NULL;
1740}
1741
1742static void insert_to_mm_slots_hash(struct mm_struct *mm,
1743 struct mm_slot *mm_slot)
1744{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001745 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001746 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001747}
1748
1749static inline int khugepaged_test_exit(struct mm_struct *mm)
1750{
1751 return atomic_read(&mm->mm_users) == 0;
1752}
1753
1754int __khugepaged_enter(struct mm_struct *mm)
1755{
1756 struct mm_slot *mm_slot;
1757 int wakeup;
1758
1759 mm_slot = alloc_mm_slot();
1760 if (!mm_slot)
1761 return -ENOMEM;
1762
1763 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001764 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001765 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1766 free_mm_slot(mm_slot);
1767 return 0;
1768 }
1769
1770 spin_lock(&khugepaged_mm_lock);
1771 insert_to_mm_slots_hash(mm, mm_slot);
1772 /*
1773 * Insert just behind the scanning cursor, to let the area settle
1774 * down a little.
1775 */
1776 wakeup = list_empty(&khugepaged_scan.mm_head);
1777 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1778 spin_unlock(&khugepaged_mm_lock);
1779
1780 atomic_inc(&mm->mm_count);
1781 if (wakeup)
1782 wake_up_interruptible(&khugepaged_wait);
1783
1784 return 0;
1785}
1786
David Rientjes6d50e602014-10-29 14:50:31 -07001787int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1788 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001789{
1790 unsigned long hstart, hend;
1791 if (!vma->anon_vma)
1792 /*
1793 * Not yet faulted in so we will register later in the
1794 * page fault if needed.
1795 */
1796 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001797 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001798 /* khugepaged not yet working on file or special mappings */
1799 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07001800 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001801 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1802 hend = vma->vm_end & HPAGE_PMD_MASK;
1803 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001804 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001805 return 0;
1806}
1807
1808void __khugepaged_exit(struct mm_struct *mm)
1809{
1810 struct mm_slot *mm_slot;
1811 int free = 0;
1812
1813 spin_lock(&khugepaged_mm_lock);
1814 mm_slot = get_mm_slot(mm);
1815 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001816 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001817 list_del(&mm_slot->mm_node);
1818 free = 1;
1819 }
Chris Wrightd788e802011-07-25 17:12:14 -07001820 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001821
1822 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001823 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1824 free_mm_slot(mm_slot);
1825 mmdrop(mm);
1826 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001827 /*
1828 * This is required to serialize against
1829 * khugepaged_test_exit() (which is guaranteed to run
1830 * under mmap sem read mode). Stop here (after we
1831 * return all pagetables will be destroyed) until
1832 * khugepaged has finished working on the pagetables
1833 * under the mmap_sem.
1834 */
1835 down_write(&mm->mmap_sem);
1836 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001837 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001838}
1839
1840static void release_pte_page(struct page *page)
1841{
1842 /* 0 stands for page_is_file_cache(page) == false */
1843 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1844 unlock_page(page);
1845 putback_lru_page(page);
1846}
1847
1848static void release_pte_pages(pte_t *pte, pte_t *_pte)
1849{
1850 while (--_pte >= pte) {
1851 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001852 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001853 release_pte_page(pte_page(pteval));
1854 }
1855}
1856
Andrea Arcangeliba761492011-01-13 15:46:58 -08001857static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1858 unsigned long address,
1859 pte_t *pte)
1860{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001861 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001862 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001863 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001864 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001865
Andrea Arcangeliba761492011-01-13 15:46:58 -08001866 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1867 _pte++, address += PAGE_SIZE) {
1868 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07001869 if (pte_none(pteval) || (pte_present(pteval) &&
1870 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07001871 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001872 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001873 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001874 } else {
1875 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001876 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001877 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001878 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001879 if (!pte_present(pteval)) {
1880 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001881 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001882 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001883 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001884 if (unlikely(!page)) {
1885 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001886 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001887 }
Bob Liu344aa352012-12-11 16:00:34 -08001888
Sasha Levin309381fea2014-01-23 15:52:54 -08001889 VM_BUG_ON_PAGE(PageCompound(page), page);
1890 VM_BUG_ON_PAGE(!PageAnon(page), page);
1891 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001892
Andrea Arcangeliba761492011-01-13 15:46:58 -08001893 /*
1894 * We can do it before isolate_lru_page because the
1895 * page can't be freed from under us. NOTE: PG_lock
1896 * is needed to serialize against split_huge_page
1897 * when invoked from the VM.
1898 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001899 if (!trylock_page(page)) {
1900 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001901 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001902 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08001903
1904 /*
1905 * cannot use mapcount: can't collapse if there's a gup pin.
1906 * The page must only be referenced by the scanned process
1907 * and page swap cache.
1908 */
1909 if (page_count(page) != 1 + !!PageSwapCache(page)) {
1910 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001911 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001912 goto out;
1913 }
1914 if (pte_write(pteval)) {
1915 writable = true;
1916 } else {
1917 if (PageSwapCache(page) && !reuse_swap_page(page)) {
1918 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001919 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001920 goto out;
1921 }
1922 /*
1923 * Page is not in the swap cache. It can be collapsed
1924 * into a THP.
1925 */
1926 }
1927
Andrea Arcangeliba761492011-01-13 15:46:58 -08001928 /*
1929 * Isolate the page to avoid collapsing an hugepage
1930 * currently in use by the VM.
1931 */
1932 if (isolate_lru_page(page)) {
1933 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001934 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001935 goto out;
1936 }
1937 /* 0 stands for page_is_file_cache(page) == false */
1938 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08001939 VM_BUG_ON_PAGE(!PageLocked(page), page);
1940 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001941
1942 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001943 if (pte_young(pteval) ||
1944 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001945 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08001946 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001947 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001948 if (likely(writable)) {
1949 if (likely(referenced)) {
1950 result = SCAN_SUCCEED;
1951 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
1952 referenced, writable, result);
1953 return 1;
1954 }
1955 } else {
1956 result = SCAN_PAGE_RO;
1957 }
1958
Andrea Arcangeliba761492011-01-13 15:46:58 -08001959out:
Bob Liu344aa352012-12-11 16:00:34 -08001960 release_pte_pages(pte, _pte);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001961 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
1962 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08001963 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001964}
1965
1966static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
1967 struct vm_area_struct *vma,
1968 unsigned long address,
1969 spinlock_t *ptl)
1970{
1971 pte_t *_pte;
1972 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
1973 pte_t pteval = *_pte;
1974 struct page *src_page;
1975
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001976 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001977 clear_user_highpage(page, address);
1978 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001979 if (is_zero_pfn(pte_pfn(pteval))) {
1980 /*
1981 * ptl mostly unnecessary.
1982 */
1983 spin_lock(ptl);
1984 /*
1985 * paravirt calls inside pte_clear here are
1986 * superfluous.
1987 */
1988 pte_clear(vma->vm_mm, address, _pte);
1989 spin_unlock(ptl);
1990 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001991 } else {
1992 src_page = pte_page(pteval);
1993 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08001994 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001995 release_pte_page(src_page);
1996 /*
1997 * ptl mostly unnecessary, but preempt has to
1998 * be disabled to update the per-cpu stats
1999 * inside page_remove_rmap().
2000 */
2001 spin_lock(ptl);
2002 /*
2003 * paravirt calls inside pte_clear here are
2004 * superfluous.
2005 */
2006 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002007 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002008 spin_unlock(ptl);
2009 free_page_and_swap_cache(src_page);
2010 }
2011
2012 address += PAGE_SIZE;
2013 page++;
2014 }
2015}
2016
Xiao Guangrong26234f32012-10-08 16:29:51 -07002017static void khugepaged_alloc_sleep(void)
2018{
Petr Mladekbde43c62015-09-08 15:04:05 -07002019 DEFINE_WAIT(wait);
2020
2021 add_wait_queue(&khugepaged_wait, &wait);
2022 freezable_schedule_timeout_interruptible(
2023 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2024 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002025}
2026
Bob Liu9f1b8682013-11-12 15:07:37 -08002027static int khugepaged_node_load[MAX_NUMNODES];
2028
David Rientjes14a4e212014-08-06 16:07:29 -07002029static bool khugepaged_scan_abort(int nid)
2030{
2031 int i;
2032
2033 /*
2034 * If zone_reclaim_mode is disabled, then no extra effort is made to
2035 * allocate memory locally.
2036 */
2037 if (!zone_reclaim_mode)
2038 return false;
2039
2040 /* If there is a count for this node already, it must be acceptable */
2041 if (khugepaged_node_load[nid])
2042 return false;
2043
2044 for (i = 0; i < MAX_NUMNODES; i++) {
2045 if (!khugepaged_node_load[i])
2046 continue;
2047 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2048 return true;
2049 }
2050 return false;
2051}
2052
Xiao Guangrong26234f32012-10-08 16:29:51 -07002053#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002054static int khugepaged_find_target_node(void)
2055{
2056 static int last_khugepaged_target_node = NUMA_NO_NODE;
2057 int nid, target_node = 0, max_value = 0;
2058
2059 /* find first node with max normal pages hit */
2060 for (nid = 0; nid < MAX_NUMNODES; nid++)
2061 if (khugepaged_node_load[nid] > max_value) {
2062 max_value = khugepaged_node_load[nid];
2063 target_node = nid;
2064 }
2065
2066 /* do some balance if several nodes have the same hit record */
2067 if (target_node <= last_khugepaged_target_node)
2068 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2069 nid++)
2070 if (max_value == khugepaged_node_load[nid]) {
2071 target_node = nid;
2072 break;
2073 }
2074
2075 last_khugepaged_target_node = target_node;
2076 return target_node;
2077}
2078
Xiao Guangrong26234f32012-10-08 16:29:51 -07002079static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2080{
2081 if (IS_ERR(*hpage)) {
2082 if (!*wait)
2083 return false;
2084
2085 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002086 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002087 khugepaged_alloc_sleep();
2088 } else if (*hpage) {
2089 put_page(*hpage);
2090 *hpage = NULL;
2091 }
2092
2093 return true;
2094}
2095
Michal Hocko3b3636922015-04-15 16:13:29 -07002096static struct page *
2097khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002098 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002099{
Sasha Levin309381fea2014-01-23 15:52:54 -08002100 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002101
Xiao Guangrong26234f32012-10-08 16:29:51 -07002102 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002103 * Before allocating the hugepage, release the mmap_sem read lock.
2104 * The allocation can take potentially a long time if it involves
2105 * sync compaction, and we do not need to hold the mmap_sem during
2106 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002107 */
2108 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002109
Vlastimil Babka96db8002015-09-08 15:03:50 -07002110 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002111 if (unlikely(!*hpage)) {
2112 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2113 *hpage = ERR_PTR(-ENOMEM);
2114 return NULL;
2115 }
2116
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002117 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002118 count_vm_event(THP_COLLAPSE_ALLOC);
2119 return *hpage;
2120}
2121#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002122static int khugepaged_find_target_node(void)
2123{
2124 return 0;
2125}
2126
Bob Liu10dc4152013-11-12 15:07:35 -08002127static inline struct page *alloc_hugepage(int defrag)
2128{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002129 struct page *page;
2130
2131 page = alloc_pages(alloc_hugepage_gfpmask(defrag, 0), HPAGE_PMD_ORDER);
2132 if (page)
2133 prep_transhuge_page(page);
2134 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002135}
2136
Xiao Guangrong26234f32012-10-08 16:29:51 -07002137static struct page *khugepaged_alloc_hugepage(bool *wait)
2138{
2139 struct page *hpage;
2140
2141 do {
2142 hpage = alloc_hugepage(khugepaged_defrag());
2143 if (!hpage) {
2144 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2145 if (!*wait)
2146 return NULL;
2147
2148 *wait = false;
2149 khugepaged_alloc_sleep();
2150 } else
2151 count_vm_event(THP_COLLAPSE_ALLOC);
2152 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2153
2154 return hpage;
2155}
2156
2157static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2158{
2159 if (!*hpage)
2160 *hpage = khugepaged_alloc_hugepage(wait);
2161
2162 if (unlikely(!*hpage))
2163 return false;
2164
2165 return true;
2166}
2167
Michal Hocko3b3636922015-04-15 16:13:29 -07002168static struct page *
2169khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002170 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002171{
2172 up_read(&mm->mmap_sem);
2173 VM_BUG_ON(!*hpage);
Michal Hocko3b3636922015-04-15 16:13:29 -07002174
Xiao Guangrong26234f32012-10-08 16:29:51 -07002175 return *hpage;
2176}
2177#endif
2178
Bob Liufa475e52012-12-11 16:00:39 -08002179static bool hugepage_vma_check(struct vm_area_struct *vma)
2180{
2181 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2182 (vma->vm_flags & VM_NOHUGEPAGE))
2183 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002184 if (!vma->anon_vma || vma->vm_ops)
2185 return false;
2186 if (is_vma_temporary_stack(vma))
2187 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002188 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002189 return true;
2190}
2191
Andrea Arcangeliba761492011-01-13 15:46:58 -08002192static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002193 unsigned long address,
2194 struct page **hpage,
2195 struct vm_area_struct *vma,
2196 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002197{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002198 pmd_t *pmd, _pmd;
2199 pte_t *pte;
2200 pgtable_t pgtable;
2201 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002202 spinlock_t *pmd_ptl, *pte_ptl;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002203 int isolated, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002204 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002205 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002206 unsigned long mmun_start; /* For mmu_notifiers */
2207 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07002208 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002209
2210 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002211
Michal Hocko3b3636922015-04-15 16:13:29 -07002212 /* Only allocate from the target node */
2213 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2214 __GFP_THISNODE;
2215
Xiao Guangrong26234f32012-10-08 16:29:51 -07002216 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002217 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002218 if (!new_page) {
2219 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2220 goto out_nolock;
2221 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002222
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002223 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002224 result = SCAN_CGROUP_CHARGE_FAIL;
2225 goto out_nolock;
2226 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002227
2228 /*
2229 * Prevent all access to pagetables with the exception of
2230 * gup_fast later hanlded by the ptep_clear_flush and the VM
2231 * handled by the anon_vma lock + PG_lock.
2232 */
2233 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002234 if (unlikely(khugepaged_test_exit(mm))) {
2235 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002236 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002237 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002238
2239 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002240 if (!vma) {
2241 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002242 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002243 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002244 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2245 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002246 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2247 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002248 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002249 }
2250 if (!hugepage_vma_check(vma)) {
2251 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002252 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002253 }
Bob Liu62190492012-12-11 16:00:37 -08002254 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002255 if (!pmd) {
2256 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002257 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002258 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002259
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002260 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002261
2262 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002263 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002264
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002265 mmun_start = address;
2266 mmun_end = address + HPAGE_PMD_SIZE;
2267 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002268 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002269 /*
2270 * After this gup_fast can't run anymore. This also removes
2271 * any huge TLB entry from the CPU so we won't allow
2272 * huge and small TLB entries for the same virtual address
2273 * to avoid the risk of CPU bugs in that area.
2274 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002275 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002276 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002277 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002278
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002279 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002280 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002281 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002282
2283 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002284 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002285 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002286 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002287 /*
2288 * We can only use set_pmd_at when establishing
2289 * hugepmds and never for establishing regular pmds that
2290 * points to regular pagetables. Use pmd_populate for that
2291 */
2292 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002293 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002294 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002295 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002296 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002297 }
2298
2299 /*
2300 * All pages are isolated and locked so anon_vma rmap
2301 * can't run anymore.
2302 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002303 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002304
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002305 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002306 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002307 __SetPageUptodate(new_page);
2308 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002309
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002310 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2311 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312
2313 /*
2314 * spin_lock() below is not the equivalent of smp_wmb(), so
2315 * this is needed to avoid the copy_huge_page writes to become
2316 * visible after the set_pmd_at() write.
2317 */
2318 smp_wmb();
2319
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002320 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002321 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002322 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002323 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002324 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002325 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002326 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002327 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002328 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002329
2330 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002331
Andrea Arcangeliba761492011-01-13 15:46:58 -08002332 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002333 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002334out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002335 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002336 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002337 return;
2338
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002339out_nolock:
2340 trace_mm_collapse_huge_page(mm, isolated, result);
2341 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002342out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002343 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002344 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002345}
2346
2347static int khugepaged_scan_pmd(struct mm_struct *mm,
2348 struct vm_area_struct *vma,
2349 unsigned long address,
2350 struct page **hpage)
2351{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002352 pmd_t *pmd;
2353 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002354 int ret = 0, none_or_zero = 0, result = 0;
2355 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356 unsigned long _address;
2357 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002358 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002359 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002360
2361 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2362
Bob Liu62190492012-12-11 16:00:37 -08002363 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002364 if (!pmd) {
2365 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002366 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002367 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002368
Bob Liu9f1b8682013-11-12 15:07:37 -08002369 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002370 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2371 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2372 _pte++, _address += PAGE_SIZE) {
2373 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002374 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002375 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002376 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002377 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002378 } else {
2379 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002380 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002381 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002382 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002383 if (!pte_present(pteval)) {
2384 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002385 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002386 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002387 if (pte_write(pteval))
2388 writable = true;
2389
Andrea Arcangeliba761492011-01-13 15:46:58 -08002390 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002391 if (unlikely(!page)) {
2392 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002394 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002395
2396 /* TODO: teach khugepaged to collapse THP mapped with pte */
2397 if (PageCompound(page)) {
2398 result = SCAN_PAGE_COMPOUND;
2399 goto out_unmap;
2400 }
2401
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002402 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002403 * Record which node the original page is from and save this
2404 * information to khugepaged_node_load[].
2405 * Khupaged will allocate hugepage from the node has the max
2406 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002407 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002408 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002409 if (khugepaged_scan_abort(node)) {
2410 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002411 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002412 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002413 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002414 if (!PageLRU(page)) {
2415 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002417 }
2418 if (PageLocked(page)) {
2419 result = SCAN_PAGE_LOCK;
2420 goto out_unmap;
2421 }
2422 if (!PageAnon(page)) {
2423 result = SCAN_PAGE_ANON;
2424 goto out_unmap;
2425 }
2426
Ebru Akagunduz10359212015-02-11 15:28:28 -08002427 /*
2428 * cannot use mapcount: can't collapse if there's a gup pin.
2429 * The page must only be referenced by the scanned process
2430 * and page swap cache.
2431 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002432 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2433 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002434 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002435 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002436 if (pte_young(pteval) ||
2437 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002438 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002439 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002440 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002441 if (writable) {
2442 if (referenced) {
2443 result = SCAN_SUCCEED;
2444 ret = 1;
2445 } else {
2446 result = SCAN_NO_REFERENCED_PAGE;
2447 }
2448 } else {
2449 result = SCAN_PAGE_RO;
2450 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002451out_unmap:
2452 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002453 if (ret) {
2454 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002455 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002456 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002457 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002458out:
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002459 trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2460 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002461 return ret;
2462}
2463
2464static void collect_mm_slot(struct mm_slot *mm_slot)
2465{
2466 struct mm_struct *mm = mm_slot->mm;
2467
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002468 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002469
2470 if (khugepaged_test_exit(mm)) {
2471 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002472 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002473 list_del(&mm_slot->mm_node);
2474
2475 /*
2476 * Not strictly needed because the mm exited already.
2477 *
2478 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2479 */
2480
2481 /* khugepaged_mm_lock actually not necessary for the below */
2482 free_mm_slot(mm_slot);
2483 mmdrop(mm);
2484 }
2485}
2486
2487static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2488 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002489 __releases(&khugepaged_mm_lock)
2490 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002491{
2492 struct mm_slot *mm_slot;
2493 struct mm_struct *mm;
2494 struct vm_area_struct *vma;
2495 int progress = 0;
2496
2497 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002498 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002499
2500 if (khugepaged_scan.mm_slot)
2501 mm_slot = khugepaged_scan.mm_slot;
2502 else {
2503 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2504 struct mm_slot, mm_node);
2505 khugepaged_scan.address = 0;
2506 khugepaged_scan.mm_slot = mm_slot;
2507 }
2508 spin_unlock(&khugepaged_mm_lock);
2509
2510 mm = mm_slot->mm;
2511 down_read(&mm->mmap_sem);
2512 if (unlikely(khugepaged_test_exit(mm)))
2513 vma = NULL;
2514 else
2515 vma = find_vma(mm, khugepaged_scan.address);
2516
2517 progress++;
2518 for (; vma; vma = vma->vm_next) {
2519 unsigned long hstart, hend;
2520
2521 cond_resched();
2522 if (unlikely(khugepaged_test_exit(mm))) {
2523 progress++;
2524 break;
2525 }
Bob Liufa475e52012-12-11 16:00:39 -08002526 if (!hugepage_vma_check(vma)) {
2527skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002528 progress++;
2529 continue;
2530 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002531 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2532 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002533 if (hstart >= hend)
2534 goto skip;
2535 if (khugepaged_scan.address > hend)
2536 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002537 if (khugepaged_scan.address < hstart)
2538 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002539 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002540
2541 while (khugepaged_scan.address < hend) {
2542 int ret;
2543 cond_resched();
2544 if (unlikely(khugepaged_test_exit(mm)))
2545 goto breakouterloop;
2546
2547 VM_BUG_ON(khugepaged_scan.address < hstart ||
2548 khugepaged_scan.address + HPAGE_PMD_SIZE >
2549 hend);
2550 ret = khugepaged_scan_pmd(mm, vma,
2551 khugepaged_scan.address,
2552 hpage);
2553 /* move to next address */
2554 khugepaged_scan.address += HPAGE_PMD_SIZE;
2555 progress += HPAGE_PMD_NR;
2556 if (ret)
2557 /* we released mmap_sem so break loop */
2558 goto breakouterloop_mmap_sem;
2559 if (progress >= pages)
2560 goto breakouterloop;
2561 }
2562 }
2563breakouterloop:
2564 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2565breakouterloop_mmap_sem:
2566
2567 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002568 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002569 /*
2570 * Release the current mm_slot if this mm is about to die, or
2571 * if we scanned all vmas of this mm.
2572 */
2573 if (khugepaged_test_exit(mm) || !vma) {
2574 /*
2575 * Make sure that if mm_users is reaching zero while
2576 * khugepaged runs here, khugepaged_exit will find
2577 * mm_slot not pointing to the exiting mm.
2578 */
2579 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2580 khugepaged_scan.mm_slot = list_entry(
2581 mm_slot->mm_node.next,
2582 struct mm_slot, mm_node);
2583 khugepaged_scan.address = 0;
2584 } else {
2585 khugepaged_scan.mm_slot = NULL;
2586 khugepaged_full_scans++;
2587 }
2588
2589 collect_mm_slot(mm_slot);
2590 }
2591
2592 return progress;
2593}
2594
2595static int khugepaged_has_work(void)
2596{
2597 return !list_empty(&khugepaged_scan.mm_head) &&
2598 khugepaged_enabled();
2599}
2600
2601static int khugepaged_wait_event(void)
2602{
2603 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002604 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002605}
2606
Xiao Guangrongd5169042012-10-08 16:29:48 -07002607static void khugepaged_do_scan(void)
2608{
2609 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002610 unsigned int progress = 0, pass_through_head = 0;
2611 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002612 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002613
2614 barrier(); /* write khugepaged_pages_to_scan to local stack */
2615
2616 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002617 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002618 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002619
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002620 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002621
Jiri Kosinacd092412015-06-24 16:56:07 -07002622 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002623 break;
2624
Andrea Arcangeliba761492011-01-13 15:46:58 -08002625 spin_lock(&khugepaged_mm_lock);
2626 if (!khugepaged_scan.mm_slot)
2627 pass_through_head++;
2628 if (khugepaged_has_work() &&
2629 pass_through_head < 2)
2630 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002631 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002632 else
2633 progress = pages;
2634 spin_unlock(&khugepaged_mm_lock);
2635 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002636
Xiao Guangrongd5169042012-10-08 16:29:48 -07002637 if (!IS_ERR_OR_NULL(hpage))
2638 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002639}
2640
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002641static void khugepaged_wait_work(void)
2642{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002643 if (khugepaged_has_work()) {
2644 if (!khugepaged_scan_sleep_millisecs)
2645 return;
2646
2647 wait_event_freezable_timeout(khugepaged_wait,
2648 kthread_should_stop(),
2649 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2650 return;
2651 }
2652
2653 if (khugepaged_enabled())
2654 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2655}
2656
Andrea Arcangeliba761492011-01-13 15:46:58 -08002657static int khugepaged(void *none)
2658{
2659 struct mm_slot *mm_slot;
2660
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002661 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002662 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002663
Xiao Guangrongb7231782012-10-08 16:29:54 -07002664 while (!kthread_should_stop()) {
2665 khugepaged_do_scan();
2666 khugepaged_wait_work();
2667 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002668
2669 spin_lock(&khugepaged_mm_lock);
2670 mm_slot = khugepaged_scan.mm_slot;
2671 khugepaged_scan.mm_slot = NULL;
2672 if (mm_slot)
2673 collect_mm_slot(mm_slot);
2674 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002675 return 0;
2676}
2677
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002678static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2679 unsigned long haddr, pmd_t *pmd)
2680{
2681 struct mm_struct *mm = vma->vm_mm;
2682 pgtable_t pgtable;
2683 pmd_t _pmd;
2684 int i;
2685
2686 /* leave pmd empty until pte is filled */
2687 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2688
2689 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2690 pmd_populate(mm, &_pmd, pgtable);
2691
2692 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2693 pte_t *pte, entry;
2694 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2695 entry = pte_mkspecial(entry);
2696 pte = pte_offset_map(&_pmd, haddr);
2697 VM_BUG_ON(!pte_none(*pte));
2698 set_pte_at(mm, haddr, pte, entry);
2699 pte_unmap(pte);
2700 }
2701 smp_wmb(); /* make pte visible before pmd */
2702 pmd_populate(mm, pmd, pgtable);
2703 put_huge_zero_page();
2704}
2705
2706static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002707 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002708{
2709 struct mm_struct *mm = vma->vm_mm;
2710 struct page *page;
2711 pgtable_t pgtable;
2712 pmd_t _pmd;
2713 bool young, write;
2714 int i;
2715
2716 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2717 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2718 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
2719 VM_BUG_ON(!pmd_trans_huge(*pmd));
2720
2721 count_vm_event(THP_SPLIT_PMD);
2722
2723 if (vma_is_dax(vma)) {
2724 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2725 if (is_huge_zero_pmd(_pmd))
2726 put_huge_zero_page();
2727 return;
2728 } else if (is_huge_zero_pmd(*pmd)) {
2729 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2730 }
2731
2732 page = pmd_page(*pmd);
2733 VM_BUG_ON_PAGE(!page_count(page), page);
2734 atomic_add(HPAGE_PMD_NR - 1, &page->_count);
2735 write = pmd_write(*pmd);
2736 young = pmd_young(*pmd);
2737
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002738 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2739 pmd_populate(mm, &_pmd, pgtable);
2740
2741 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2742 pte_t entry, *pte;
2743 /*
2744 * Note that NUMA hinting access restrictions are not
2745 * transferred to avoid any possibility of altering
2746 * permissions across VMAs.
2747 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002748 if (freeze) {
2749 swp_entry_t swp_entry;
2750 swp_entry = make_migration_entry(page + i, write);
2751 entry = swp_entry_to_pte(swp_entry);
2752 } else {
2753 entry = mk_pte(page + i, vma->vm_page_prot);
2754 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2755 if (!write)
2756 entry = pte_wrprotect(entry);
2757 if (!young)
2758 entry = pte_mkold(entry);
2759 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002760 pte = pte_offset_map(&_pmd, haddr);
2761 BUG_ON(!pte_none(*pte));
2762 set_pte_at(mm, haddr, pte, entry);
2763 atomic_inc(&page[i]._mapcount);
2764 pte_unmap(pte);
2765 }
2766
2767 /*
2768 * Set PG_double_map before dropping compound_mapcount to avoid
2769 * false-negative page_mapped().
2770 */
2771 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2772 for (i = 0; i < HPAGE_PMD_NR; i++)
2773 atomic_inc(&page[i]._mapcount);
2774 }
2775
2776 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2777 /* Last compound_mapcount is gone. */
2778 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2779 if (TestClearPageDoubleMap(page)) {
2780 /* No need in mapcount reference anymore */
2781 for (i = 0; i < HPAGE_PMD_NR; i++)
2782 atomic_dec(&page[i]._mapcount);
2783 }
2784 }
2785
2786 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002787 /*
2788 * Up to this point the pmd is present and huge and userland has the
2789 * whole access to the hugepage during the split (which happens in
2790 * place). If we overwrite the pmd with the not-huge version pointing
2791 * to the pte here (which of course we could if all CPUs were bug
2792 * free), userland could trigger a small page size TLB miss on the
2793 * small sized TLB while the hugepage TLB entry is still established in
2794 * the huge TLB. Some CPU doesn't like that.
2795 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2796 * 383 on page 93. Intel should be safe but is also warns that it's
2797 * only safe if the permission and cache attributes of the two entries
2798 * loaded in the two TLB is identical (which should be the case here).
2799 * But it is generally safer to never allow small and huge TLB entries
2800 * for the same virtual address to be loaded simultaneously. So instead
2801 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2802 * current pmd notpresent (atomically because here the pmd_trans_huge
2803 * and pmd_trans_splitting must remain set at all times on the pmd
2804 * until the split is complete for this pmd), then we flush the SMP TLB
2805 * and finally we write the non-huge version of the pmd entry with
2806 * pmd_populate.
2807 */
2808 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002809 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002810
2811 if (freeze) {
2812 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2813 page_remove_rmap(page + i, false);
2814 put_page(page + i);
2815 }
2816 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002817}
2818
2819void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
2820 unsigned long address)
2821{
2822 spinlock_t *ptl;
2823 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002824 struct page *page = NULL;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002825 unsigned long haddr = address & HPAGE_PMD_MASK;
2826
2827 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2828 ptl = pmd_lock(mm, pmd);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002829 if (unlikely(!pmd_trans_huge(*pmd)))
2830 goto out;
2831 page = pmd_page(*pmd);
2832 __split_huge_pmd_locked(vma, pmd, haddr, false);
2833 if (PageMlocked(page))
2834 get_page(page);
2835 else
2836 page = NULL;
2837out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002838 spin_unlock(ptl);
2839 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002840 if (page) {
2841 lock_page(page);
2842 munlock_vma_page(page);
2843 unlock_page(page);
2844 put_page(page);
2845 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002846}
2847
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002848static void split_huge_pmd_address(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002849 unsigned long address)
2850{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002851 pgd_t *pgd;
2852 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002853 pmd_t *pmd;
2854
2855 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2856
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002857 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002858 if (!pgd_present(*pgd))
2859 return;
2860
2861 pud = pud_offset(pgd, address);
2862 if (!pud_present(*pud))
2863 return;
2864
2865 pmd = pmd_offset(pud, address);
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002866 if (!pmd_present(*pmd) || !pmd_trans_huge(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002867 return;
2868 /*
2869 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2870 * materialize from under us.
2871 */
Kirill A. Shutemovad0bed22016-01-15 16:52:53 -08002872 split_huge_pmd(vma, pmd, address);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002873}
2874
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002875void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002876 unsigned long start,
2877 unsigned long end,
2878 long adjust_next)
2879{
2880 /*
2881 * If the new start address isn't hpage aligned and it could
2882 * previously contain an hugepage: check if we need to split
2883 * an huge pmd.
2884 */
2885 if (start & ~HPAGE_PMD_MASK &&
2886 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2887 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002888 split_huge_pmd_address(vma, start);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002889
2890 /*
2891 * If the new end address isn't hpage aligned and it could
2892 * previously contain an hugepage: check if we need to split
2893 * an huge pmd.
2894 */
2895 if (end & ~HPAGE_PMD_MASK &&
2896 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2897 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002898 split_huge_pmd_address(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002899
2900 /*
2901 * If we're also updating the vma->vm_next->vm_start, if the new
2902 * vm_next->vm_start isn't page aligned and it could previously
2903 * contain an hugepage: check if we need to split an huge pmd.
2904 */
2905 if (adjust_next > 0) {
2906 struct vm_area_struct *next = vma->vm_next;
2907 unsigned long nstart = next->vm_start;
2908 nstart += adjust_next << PAGE_SHIFT;
2909 if (nstart & ~HPAGE_PMD_MASK &&
2910 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2911 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002912 split_huge_pmd_address(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002913 }
2914}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002915
2916static void freeze_page_vma(struct vm_area_struct *vma, struct page *page,
2917 unsigned long address)
2918{
2919 spinlock_t *ptl;
2920 pgd_t *pgd;
2921 pud_t *pud;
2922 pmd_t *pmd;
2923 pte_t *pte;
2924 int i, nr = HPAGE_PMD_NR;
2925
2926 /* Skip pages which doesn't belong to the VMA */
2927 if (address < vma->vm_start) {
2928 int off = (vma->vm_start - address) >> PAGE_SHIFT;
2929 page += off;
2930 nr -= off;
2931 address = vma->vm_start;
2932 }
2933
2934 pgd = pgd_offset(vma->vm_mm, address);
2935 if (!pgd_present(*pgd))
2936 return;
2937 pud = pud_offset(pgd, address);
2938 if (!pud_present(*pud))
2939 return;
2940 pmd = pmd_offset(pud, address);
2941 ptl = pmd_lock(vma->vm_mm, pmd);
2942 if (!pmd_present(*pmd)) {
2943 spin_unlock(ptl);
2944 return;
2945 }
2946 if (pmd_trans_huge(*pmd)) {
2947 if (page == pmd_page(*pmd))
2948 __split_huge_pmd_locked(vma, pmd, address, true);
2949 spin_unlock(ptl);
2950 return;
2951 }
2952 spin_unlock(ptl);
2953
2954 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
2955 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++) {
2956 pte_t entry, swp_pte;
2957 swp_entry_t swp_entry;
2958
2959 if (!pte_present(pte[i]))
2960 continue;
2961 if (page_to_pfn(page) != pte_pfn(pte[i]))
2962 continue;
2963 flush_cache_page(vma, address, page_to_pfn(page));
2964 entry = ptep_clear_flush(vma, address, pte + i);
2965 swp_entry = make_migration_entry(page, pte_write(entry));
2966 swp_pte = swp_entry_to_pte(swp_entry);
2967 if (pte_soft_dirty(entry))
2968 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2969 set_pte_at(vma->vm_mm, address, pte + i, swp_pte);
2970 page_remove_rmap(page, false);
2971 put_page(page);
2972 }
2973 pte_unmap_unlock(pte, ptl);
2974}
2975
2976static void freeze_page(struct anon_vma *anon_vma, struct page *page)
2977{
2978 struct anon_vma_chain *avc;
2979 pgoff_t pgoff = page_to_pgoff(page);
2980
2981 VM_BUG_ON_PAGE(!PageHead(page), page);
2982
2983 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff,
2984 pgoff + HPAGE_PMD_NR - 1) {
2985 unsigned long haddr;
2986
2987 haddr = __vma_address(page, avc->vma) & HPAGE_PMD_MASK;
2988 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
2989 haddr, haddr + HPAGE_PMD_SIZE);
2990 freeze_page_vma(avc->vma, page, haddr);
2991 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
2992 haddr, haddr + HPAGE_PMD_SIZE);
2993 }
2994}
2995
2996static void unfreeze_page_vma(struct vm_area_struct *vma, struct page *page,
2997 unsigned long address)
2998{
2999 spinlock_t *ptl;
3000 pmd_t *pmd;
3001 pte_t *pte, entry;
3002 swp_entry_t swp_entry;
3003 int i, nr = HPAGE_PMD_NR;
3004
3005 /* Skip pages which doesn't belong to the VMA */
3006 if (address < vma->vm_start) {
3007 int off = (vma->vm_start - address) >> PAGE_SHIFT;
3008 page += off;
3009 nr -= off;
3010 address = vma->vm_start;
3011 }
3012
3013 pmd = mm_find_pmd(vma->vm_mm, address);
3014 if (!pmd)
3015 return;
3016 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
3017 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++) {
3018 if (!is_swap_pte(pte[i]))
3019 continue;
3020
3021 swp_entry = pte_to_swp_entry(pte[i]);
3022 if (!is_migration_entry(swp_entry))
3023 continue;
3024 if (migration_entry_to_page(swp_entry) != page)
3025 continue;
3026
3027 get_page(page);
3028 page_add_anon_rmap(page, vma, address, false);
3029
3030 entry = pte_mkold(mk_pte(page, vma->vm_page_prot));
3031 entry = pte_mkdirty(entry);
3032 if (is_write_migration_entry(swp_entry))
3033 entry = maybe_mkwrite(entry, vma);
3034
3035 flush_dcache_page(page);
3036 set_pte_at(vma->vm_mm, address, pte + i, entry);
3037
3038 /* No need to invalidate - it was non-present before */
3039 update_mmu_cache(vma, address, pte + i);
3040 }
3041 pte_unmap_unlock(pte, ptl);
3042}
3043
3044static void unfreeze_page(struct anon_vma *anon_vma, struct page *page)
3045{
3046 struct anon_vma_chain *avc;
3047 pgoff_t pgoff = page_to_pgoff(page);
3048
3049 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
3050 pgoff, pgoff + HPAGE_PMD_NR - 1) {
3051 unsigned long address = __vma_address(page, avc->vma);
3052
3053 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
3054 address, address + HPAGE_PMD_SIZE);
3055 unfreeze_page_vma(avc->vma, page, address);
3056 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
3057 address, address + HPAGE_PMD_SIZE);
3058 }
3059}
3060
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003061static int __split_huge_page_tail(struct page *head, int tail,
3062 struct lruvec *lruvec, struct list_head *list)
3063{
3064 int mapcount;
3065 struct page *page_tail = head + tail;
3066
3067 mapcount = atomic_read(&page_tail->_mapcount) + 1;
3068 VM_BUG_ON_PAGE(atomic_read(&page_tail->_count) != 0, page_tail);
3069
3070 /*
3071 * tail_page->_count is zero and not changing from under us. But
3072 * get_page_unless_zero() may be running from under us on the
3073 * tail_page. If we used atomic_set() below instead of atomic_add(), we
3074 * would then run atomic_set() concurrently with
3075 * get_page_unless_zero(), and atomic_set() is implemented in C not
3076 * using locked ops. spin_unlock on x86 sometime uses locked ops
3077 * because of PPro errata 66, 92, so unless somebody can guarantee
3078 * atomic_set() here would be safe on all archs (and not only on x86),
3079 * it's safer to use atomic_add().
3080 */
3081 atomic_add(mapcount + 1, &page_tail->_count);
3082
3083
3084 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3085 page_tail->flags |= (head->flags &
3086 ((1L << PG_referenced) |
3087 (1L << PG_swapbacked) |
3088 (1L << PG_mlocked) |
3089 (1L << PG_uptodate) |
3090 (1L << PG_active) |
3091 (1L << PG_locked) |
3092 (1L << PG_unevictable)));
3093 page_tail->flags |= (1L << PG_dirty);
3094
3095 /*
3096 * After clearing PageTail the gup refcount can be released.
3097 * Page flags also must be visible before we make the page non-compound.
3098 */
3099 smp_wmb();
3100
3101 clear_compound_head(page_tail);
3102
3103 if (page_is_young(head))
3104 set_page_young(page_tail);
3105 if (page_is_idle(head))
3106 set_page_idle(page_tail);
3107
3108 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003109 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003110 page_tail);
3111 page_tail->mapping = head->mapping;
3112
3113 page_tail->index = head->index + tail;
3114 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3115 lru_add_page_tail(head, page_tail, lruvec, list);
3116
3117 return mapcount;
3118}
3119
3120static void __split_huge_page(struct page *page, struct list_head *list)
3121{
3122 struct page *head = compound_head(page);
3123 struct zone *zone = page_zone(head);
3124 struct lruvec *lruvec;
3125 int i, tail_mapcount;
3126
3127 /* prevent PageLRU to go away from under us, and freeze lru stats */
3128 spin_lock_irq(&zone->lru_lock);
3129 lruvec = mem_cgroup_page_lruvec(head, zone);
3130
3131 /* complete memcg works before add pages to LRU */
3132 mem_cgroup_split_huge_fixup(head);
3133
3134 tail_mapcount = 0;
3135 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
3136 tail_mapcount += __split_huge_page_tail(head, i, lruvec, list);
3137 atomic_sub(tail_mapcount, &head->_count);
3138
3139 ClearPageCompound(head);
3140 spin_unlock_irq(&zone->lru_lock);
3141
3142 unfreeze_page(page_anon_vma(head), head);
3143
3144 for (i = 0; i < HPAGE_PMD_NR; i++) {
3145 struct page *subpage = head + i;
3146 if (subpage == page)
3147 continue;
3148 unlock_page(subpage);
3149
3150 /*
3151 * Subpages may be freed if there wasn't any mapping
3152 * like if add_to_swap() is running on a lru page that
3153 * had its mapping zapped. And freeing these pages
3154 * requires taking the lru_lock so we do the put_page
3155 * of the tail pages after the split is complete.
3156 */
3157 put_page(subpage);
3158 }
3159}
3160
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003161int total_mapcount(struct page *page)
3162{
3163 int i, ret;
3164
3165 VM_BUG_ON_PAGE(PageTail(page), page);
3166
3167 if (likely(!PageCompound(page)))
3168 return atomic_read(&page->_mapcount) + 1;
3169
3170 ret = compound_mapcount(page);
3171 if (PageHuge(page))
3172 return ret;
3173 for (i = 0; i < HPAGE_PMD_NR; i++)
3174 ret += atomic_read(&page[i]._mapcount) + 1;
3175 if (PageDoubleMap(page))
3176 ret -= HPAGE_PMD_NR;
3177 return ret;
3178}
3179
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003180/*
3181 * This function splits huge page into normal pages. @page can point to any
3182 * subpage of huge page to split. Split doesn't change the position of @page.
3183 *
3184 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3185 * The huge page must be locked.
3186 *
3187 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3188 *
3189 * Both head page and tail pages will inherit mapping, flags, and so on from
3190 * the hugepage.
3191 *
3192 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3193 * they are not mapped.
3194 *
3195 * Returns 0 if the hugepage is split successfully.
3196 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3197 * us.
3198 */
3199int split_huge_page_to_list(struct page *page, struct list_head *list)
3200{
3201 struct page *head = compound_head(page);
3202 struct anon_vma *anon_vma;
3203 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003204 bool mlocked;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003205
3206 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3207 VM_BUG_ON_PAGE(!PageAnon(page), page);
3208 VM_BUG_ON_PAGE(!PageLocked(page), page);
3209 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3210 VM_BUG_ON_PAGE(!PageCompound(page), page);
3211
3212 /*
3213 * The caller does not necessarily hold an mmap_sem that would prevent
3214 * the anon_vma disappearing so we first we take a reference to it
3215 * and then lock the anon_vma for write. This is similar to
3216 * page_lock_anon_vma_read except the write lock is taken to serialise
3217 * against parallel split or collapse operations.
3218 */
3219 anon_vma = page_get_anon_vma(head);
3220 if (!anon_vma) {
3221 ret = -EBUSY;
3222 goto out;
3223 }
3224 anon_vma_lock_write(anon_vma);
3225
3226 /*
3227 * Racy check if we can split the page, before freeze_page() will
3228 * split PMDs
3229 */
3230 if (total_mapcount(head) != page_count(head) - 1) {
3231 ret = -EBUSY;
3232 goto out_unlock;
3233 }
3234
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003235 mlocked = PageMlocked(page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003236 freeze_page(anon_vma, head);
3237 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3238
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003239 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3240 if (mlocked)
3241 lru_add_drain();
3242
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003243 /* Prevent deferred_split_scan() touching ->_count */
3244 spin_lock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003245 count = page_count(head);
3246 mapcount = total_mapcount(head);
3247 if (mapcount == count - 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003248 if (!list_empty(page_deferred_list(head))) {
3249 split_queue_len--;
3250 list_del(page_deferred_list(head));
3251 }
3252 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003253 __split_huge_page(page, list);
3254 ret = 0;
3255 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount > count - 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003256 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003257 pr_alert("total_mapcount: %u, page_count(): %u\n",
3258 mapcount, count);
3259 if (PageTail(page))
3260 dump_page(head, NULL);
3261 dump_page(page, "total_mapcount(head) > page_count(head) - 1");
3262 BUG();
3263 } else {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003264 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003265 unfreeze_page(anon_vma, head);
3266 ret = -EBUSY;
3267 }
3268
3269out_unlock:
3270 anon_vma_unlock_write(anon_vma);
3271 put_anon_vma(anon_vma);
3272out:
3273 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3274 return ret;
3275}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003276
3277void free_transhuge_page(struct page *page)
3278{
3279 unsigned long flags;
3280
3281 spin_lock_irqsave(&split_queue_lock, flags);
3282 if (!list_empty(page_deferred_list(page))) {
3283 split_queue_len--;
3284 list_del(page_deferred_list(page));
3285 }
3286 spin_unlock_irqrestore(&split_queue_lock, flags);
3287 free_compound_page(page);
3288}
3289
3290void deferred_split_huge_page(struct page *page)
3291{
3292 unsigned long flags;
3293
3294 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3295
3296 spin_lock_irqsave(&split_queue_lock, flags);
3297 if (list_empty(page_deferred_list(page))) {
3298 list_add_tail(page_deferred_list(page), &split_queue);
3299 split_queue_len++;
3300 }
3301 spin_unlock_irqrestore(&split_queue_lock, flags);
3302}
3303
3304static unsigned long deferred_split_count(struct shrinker *shrink,
3305 struct shrink_control *sc)
3306{
3307 /*
3308 * Split a page from split_queue will free up at least one page,
3309 * at most HPAGE_PMD_NR - 1. We don't track exact number.
3310 * Let's use HPAGE_PMD_NR / 2 as ballpark.
3311 */
3312 return ACCESS_ONCE(split_queue_len) * HPAGE_PMD_NR / 2;
3313}
3314
3315static unsigned long deferred_split_scan(struct shrinker *shrink,
3316 struct shrink_control *sc)
3317{
3318 unsigned long flags;
3319 LIST_HEAD(list), *pos, *next;
3320 struct page *page;
3321 int split = 0;
3322
3323 spin_lock_irqsave(&split_queue_lock, flags);
3324 list_splice_init(&split_queue, &list);
3325
3326 /* Take pin on all head pages to avoid freeing them under us */
3327 list_for_each_safe(pos, next, &list) {
3328 page = list_entry((void *)pos, struct page, mapping);
3329 page = compound_head(page);
3330 /* race with put_compound_page() */
3331 if (!get_page_unless_zero(page)) {
3332 list_del_init(page_deferred_list(page));
3333 split_queue_len--;
3334 }
3335 }
3336 spin_unlock_irqrestore(&split_queue_lock, flags);
3337
3338 list_for_each_safe(pos, next, &list) {
3339 page = list_entry((void *)pos, struct page, mapping);
3340 lock_page(page);
3341 /* split_huge_page() removes page from list on success */
3342 if (!split_huge_page(page))
3343 split++;
3344 unlock_page(page);
3345 put_page(page);
3346 }
3347
3348 spin_lock_irqsave(&split_queue_lock, flags);
3349 list_splice_tail(&list, &split_queue);
3350 spin_unlock_irqrestore(&split_queue_lock, flags);
3351
3352 return split * HPAGE_PMD_NR / 2;
3353}
3354
3355static struct shrinker deferred_split_shrinker = {
3356 .count_objects = deferred_split_count,
3357 .scan_objects = deferred_split_scan,
3358 .seeks = DEFAULT_SEEKS,
3359};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003360
3361#ifdef CONFIG_DEBUG_FS
3362static int split_huge_pages_set(void *data, u64 val)
3363{
3364 struct zone *zone;
3365 struct page *page;
3366 unsigned long pfn, max_zone_pfn;
3367 unsigned long total = 0, split = 0;
3368
3369 if (val != 1)
3370 return -EINVAL;
3371
3372 for_each_populated_zone(zone) {
3373 max_zone_pfn = zone_end_pfn(zone);
3374 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3375 if (!pfn_valid(pfn))
3376 continue;
3377
3378 page = pfn_to_page(pfn);
3379 if (!get_page_unless_zero(page))
3380 continue;
3381
3382 if (zone != page_zone(page))
3383 goto next;
3384
3385 if (!PageHead(page) || !PageAnon(page) ||
3386 PageHuge(page))
3387 goto next;
3388
3389 total++;
3390 lock_page(page);
3391 if (!split_huge_page(page))
3392 split++;
3393 unlock_page(page);
3394next:
3395 put_page(page);
3396 }
3397 }
3398
3399 pr_info("%lu of %lu THP split", split, total);
3400
3401 return 0;
3402}
3403DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3404 "%llu\n");
3405
3406static int __init split_huge_pages_debugfs(void)
3407{
3408 void *ret;
3409
3410 ret = debugfs_create_file("split_huge_pages", 0644, NULL, NULL,
3411 &split_huge_pages_fops);
3412 if (!ret)
3413 pr_warn("Failed to create split_huge_pages in debugfs");
3414 return 0;
3415}
3416late_initcall(split_huge_pages_debugfs);
3417#endif