blob: ed474483a620d3cea65a6322a83a80f8a72107b5 [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>
Dan Williamsf25748e32016-01-15 16:56:43 -080024#include <linux/pfn_t.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080025#include <linux/mman.h>
Dan Williams3565fce2016-01-15 16:56:55 -080026#include <linux/memremap.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020027#include <linux/pagemap.h>
Kirill A. Shutemov49071d42016-01-15 16:54:40 -080028#include <linux/debugfs.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000029#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080030#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070031#include <linux/userfaultfd_k.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070032#include <linux/page_idle.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080033
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080034#include <asm/tlb.h>
35#include <asm/pgalloc.h>
36#include "internal.h"
37
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080038enum scan_result {
39 SCAN_FAIL,
40 SCAN_SUCCEED,
41 SCAN_PMD_NULL,
42 SCAN_EXCEED_NONE_PTE,
43 SCAN_PTE_NON_PRESENT,
44 SCAN_PAGE_RO,
45 SCAN_NO_REFERENCED_PAGE,
46 SCAN_PAGE_NULL,
47 SCAN_SCAN_ABORT,
48 SCAN_PAGE_COUNT,
49 SCAN_PAGE_LRU,
50 SCAN_PAGE_LOCK,
51 SCAN_PAGE_ANON,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080052 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080053 SCAN_ANY_PROCESS,
54 SCAN_VMA_NULL,
55 SCAN_VMA_CHECK,
56 SCAN_ADDRESS_RANGE,
57 SCAN_SWAP_CACHE_PAGE,
58 SCAN_DEL_PAGE_LRU,
59 SCAN_ALLOC_HUGE_PAGE_FAIL,
Ebru Akagunduz70652f62016-07-26 15:24:59 -070060 SCAN_CGROUP_CHARGE_FAIL,
61 SCAN_EXCEED_SWAP_PTE
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080062};
63
64#define CREATE_TRACE_POINTS
65#include <trace/events/huge_memory.h>
66
Andrea Arcangeliba761492011-01-13 15:46:58 -080067/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080068 * By default transparent hugepage support is disabled in order that avoid
69 * to risk increase the memory footprint of applications without a guaranteed
70 * benefit. When transparent hugepage support is enabled, is for all mappings,
71 * and khugepaged scans all mappings.
72 * Defrag is invoked by khugepaged hugepage allocations and by page faults
73 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080074 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080075unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080076#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080077 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080078#endif
79#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
80 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
81#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070082 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080083 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
84 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080085
86/* default scan 8*512 pte (or vmas) every 30 second */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053087static unsigned int khugepaged_pages_to_scan __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -080088static unsigned int khugepaged_pages_collapsed;
89static unsigned int khugepaged_full_scans;
90static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
91/* during fragmentation poll the hugepage allocator once every minute */
92static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
David Rientjesf0508972016-05-20 16:58:56 -070093static unsigned long khugepaged_sleep_expire;
Andrea Arcangeliba761492011-01-13 15:46:58 -080094static struct task_struct *khugepaged_thread __read_mostly;
95static DEFINE_MUTEX(khugepaged_mutex);
96static DEFINE_SPINLOCK(khugepaged_mm_lock);
97static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
98/*
99 * default collapse hugepages if there is at least one pte mapped like
100 * it would have happened if the vma was large enough during page
101 * fault.
102 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530103static unsigned int khugepaged_max_ptes_none __read_mostly;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700104static unsigned int khugepaged_max_ptes_swap __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800105
106static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800107static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700108static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800109
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800110#define MM_SLOTS_HASH_BITS 10
111static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
112
Andrea Arcangeliba761492011-01-13 15:46:58 -0800113static struct kmem_cache *mm_slot_cache __read_mostly;
114
115/**
116 * struct mm_slot - hash lookup from mm to mm_slot
117 * @hash: hash collision list
118 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
119 * @mm: the mm that this information is valid for
120 */
121struct mm_slot {
122 struct hlist_node hash;
123 struct list_head mm_node;
124 struct mm_struct *mm;
125};
126
127/**
128 * struct khugepaged_scan - cursor for scanning
129 * @mm_head: the head of the mm list to scan
130 * @mm_slot: the current mm_slot we are scanning
131 * @address: the next address inside that to be scanned
132 *
133 * There is only the one khugepaged_scan instance of this cursor structure.
134 */
135struct khugepaged_scan {
136 struct list_head mm_head;
137 struct mm_slot *mm_slot;
138 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700139};
140static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800141 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
142};
143
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800144static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700146static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800147{
148 struct zone *zone;
149 int nr_zones = 0;
150 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800151
Andrea Arcangelif0005652011-01-13 15:47:04 -0800152 for_each_populated_zone(zone)
153 nr_zones++;
154
Mel Gorman974a7862015-11-06 16:28:34 -0800155 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800156 recommended_min = pageblock_nr_pages * nr_zones * 2;
157
158 /*
159 * Make sure that on average at least two pageblocks are almost free
160 * of another type, one for a migratetype to fall back to and a
161 * second to avoid subsequent fallbacks of other types There are 3
162 * MIGRATE_TYPES we care about.
163 */
164 recommended_min += pageblock_nr_pages * nr_zones *
165 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
166
167 /* don't ever allow to reserve more than 5% of the lowmem */
168 recommended_min = min(recommended_min,
169 (unsigned long) nr_free_buffer_pages() / 20);
170 recommended_min <<= (PAGE_SHIFT-10);
171
Han Pingtian42aa83c2014-01-23 15:53:28 -0800172 if (recommended_min > min_free_kbytes) {
173 if (user_min_free_kbytes >= 0)
Joe Perches756a025f02016-03-17 14:19:47 -0700174 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
Han Pingtian42aa83c2014-01-23 15:53:28 -0800175 min_free_kbytes, recommended_min);
176
Andrea Arcangelif0005652011-01-13 15:47:04 -0800177 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800178 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800179 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800180}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800181
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700182static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800183{
184 int err = 0;
185 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800186 if (!khugepaged_thread)
187 khugepaged_thread = kthread_run(khugepaged, NULL,
188 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530189 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700190 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800191 err = PTR_ERR(khugepaged_thread);
192 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700193 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800194 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700195
196 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800197 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800198
199 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700200 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700201 kthread_stop(khugepaged_thread);
202 khugepaged_thread = NULL;
203 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700204fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800205 return err;
206}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800207
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800208static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800209struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800210
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700211struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800212{
213 struct page *zero_page;
214retry:
215 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700216 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800217
218 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
219 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800220 if (!zero_page) {
221 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700222 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800223 }
224 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800225 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700226 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700228 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800229 goto retry;
230 }
231
232 /* We take additional reference here. It will be put back by shrinker */
233 atomic_set(&huge_zero_refcount, 2);
234 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700235 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800236}
237
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700238void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800239{
240 /*
241 * Counter should never go to zero here. Only shrinker can put
242 * last reference.
243 */
244 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
245}
246
Glauber Costa48896462013-08-28 10:18:15 +1000247static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
248 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800249{
Glauber Costa48896462013-08-28 10:18:15 +1000250 /* we can free zero page only if last reference remains */
251 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
252}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800253
Glauber Costa48896462013-08-28 10:18:15 +1000254static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
255 struct shrink_control *sc)
256{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800257 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700258 struct page *zero_page = xchg(&huge_zero_page, NULL);
259 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700260 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000261 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800262 }
263
264 return 0;
265}
266
267static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000268 .count_objects = shrink_huge_zero_page_count,
269 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800270 .seeks = DEFAULT_SEEKS,
271};
272
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800273#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800274
Mel Gorman444eb2a42016-03-17 14:19:23 -0700275static ssize_t triple_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800276 struct kobj_attribute *attr,
277 const char *buf, size_t count,
278 enum transparent_hugepage_flag enabled,
Mel Gorman444eb2a42016-03-17 14:19:23 -0700279 enum transparent_hugepage_flag deferred,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800280 enum transparent_hugepage_flag req_madv)
281{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700282 if (!memcmp("defer", buf,
283 min(sizeof("defer")-1, count))) {
284 if (enabled == deferred)
285 return -EINVAL;
286 clear_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800287 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700288 set_bit(deferred, &transparent_hugepage_flags);
289 } else if (!memcmp("always", buf,
290 min(sizeof("always")-1, count))) {
291 clear_bit(deferred, &transparent_hugepage_flags);
292 clear_bit(req_madv, &transparent_hugepage_flags);
293 set_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800294 } else if (!memcmp("madvise", buf,
295 min(sizeof("madvise")-1, count))) {
296 clear_bit(enabled, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700297 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800298 set_bit(req_madv, &transparent_hugepage_flags);
299 } else if (!memcmp("never", buf,
300 min(sizeof("never")-1, count))) {
301 clear_bit(enabled, &transparent_hugepage_flags);
302 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700303 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800304 } else
305 return -EINVAL;
306
307 return count;
308}
309
310static ssize_t enabled_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *buf)
312{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700313 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
314 return sprintf(buf, "[always] madvise never\n");
315 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
316 return sprintf(buf, "always [madvise] never\n");
317 else
318 return sprintf(buf, "always madvise [never]\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800319}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700320
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800321static ssize_t enabled_store(struct kobject *kobj,
322 struct kobj_attribute *attr,
323 const char *buf, size_t count)
324{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800325 ssize_t ret;
326
Mel Gorman444eb2a42016-03-17 14:19:23 -0700327 ret = triple_flag_store(kobj, attr, buf, count,
328 TRANSPARENT_HUGEPAGE_FLAG,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800329 TRANSPARENT_HUGEPAGE_FLAG,
330 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
331
332 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700333 int err;
334
335 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700336 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700337 mutex_unlock(&khugepaged_mutex);
338
Andrea Arcangeliba761492011-01-13 15:46:58 -0800339 if (err)
340 ret = err;
341 }
342
343 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800344}
345static struct kobj_attribute enabled_attr =
346 __ATTR(enabled, 0644, enabled_show, enabled_store);
347
348static ssize_t single_flag_show(struct kobject *kobj,
349 struct kobj_attribute *attr, char *buf,
350 enum transparent_hugepage_flag flag)
351{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700352 return sprintf(buf, "%d\n",
353 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800354}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700355
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800356static ssize_t single_flag_store(struct kobject *kobj,
357 struct kobj_attribute *attr,
358 const char *buf, size_t count,
359 enum transparent_hugepage_flag flag)
360{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700361 unsigned long value;
362 int ret;
363
364 ret = kstrtoul(buf, 10, &value);
365 if (ret < 0)
366 return ret;
367 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800368 return -EINVAL;
369
Ben Hutchingse27e6152011-04-14 15:22:21 -0700370 if (value)
371 set_bit(flag, &transparent_hugepage_flags);
372 else
373 clear_bit(flag, &transparent_hugepage_flags);
374
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800375 return count;
376}
377
378/*
379 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
380 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
381 * memory just to allocate one more hugepage.
382 */
383static ssize_t defrag_show(struct kobject *kobj,
384 struct kobj_attribute *attr, char *buf)
385{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700386 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
387 return sprintf(buf, "[always] defer madvise never\n");
388 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
389 return sprintf(buf, "always [defer] madvise never\n");
390 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
391 return sprintf(buf, "always defer [madvise] never\n");
392 else
393 return sprintf(buf, "always defer madvise [never]\n");
394
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800395}
396static ssize_t defrag_store(struct kobject *kobj,
397 struct kobj_attribute *attr,
398 const char *buf, size_t count)
399{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700400 return triple_flag_store(kobj, attr, buf, count,
401 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
402 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800403 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
404}
405static struct kobj_attribute defrag_attr =
406 __ATTR(defrag, 0644, defrag_show, defrag_store);
407
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800408static ssize_t use_zero_page_show(struct kobject *kobj,
409 struct kobj_attribute *attr, char *buf)
410{
411 return single_flag_show(kobj, attr, buf,
412 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
413}
414static ssize_t use_zero_page_store(struct kobject *kobj,
415 struct kobj_attribute *attr, const char *buf, size_t count)
416{
417 return single_flag_store(kobj, attr, buf, count,
418 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
419}
420static struct kobj_attribute use_zero_page_attr =
421 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800422#ifdef CONFIG_DEBUG_VM
423static ssize_t debug_cow_show(struct kobject *kobj,
424 struct kobj_attribute *attr, char *buf)
425{
426 return single_flag_show(kobj, attr, buf,
427 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
428}
429static ssize_t debug_cow_store(struct kobject *kobj,
430 struct kobj_attribute *attr,
431 const char *buf, size_t count)
432{
433 return single_flag_store(kobj, attr, buf, count,
434 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
435}
436static struct kobj_attribute debug_cow_attr =
437 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
438#endif /* CONFIG_DEBUG_VM */
439
440static struct attribute *hugepage_attr[] = {
441 &enabled_attr.attr,
442 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800443 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800444#ifdef CONFIG_DEBUG_VM
445 &debug_cow_attr.attr,
446#endif
447 NULL,
448};
449
450static struct attribute_group hugepage_attr_group = {
451 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800452};
453
454static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
455 struct kobj_attribute *attr,
456 char *buf)
457{
458 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
459}
460
461static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
462 struct kobj_attribute *attr,
463 const char *buf, size_t count)
464{
465 unsigned long msecs;
466 int err;
467
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700468 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800469 if (err || msecs > UINT_MAX)
470 return -EINVAL;
471
472 khugepaged_scan_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700473 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800474 wake_up_interruptible(&khugepaged_wait);
475
476 return count;
477}
478static struct kobj_attribute scan_sleep_millisecs_attr =
479 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
480 scan_sleep_millisecs_store);
481
482static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
483 struct kobj_attribute *attr,
484 char *buf)
485{
486 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
487}
488
489static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
490 struct kobj_attribute *attr,
491 const char *buf, size_t count)
492{
493 unsigned long msecs;
494 int err;
495
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700496 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800497 if (err || msecs > UINT_MAX)
498 return -EINVAL;
499
500 khugepaged_alloc_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700501 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800502 wake_up_interruptible(&khugepaged_wait);
503
504 return count;
505}
506static struct kobj_attribute alloc_sleep_millisecs_attr =
507 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
508 alloc_sleep_millisecs_store);
509
510static ssize_t pages_to_scan_show(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 char *buf)
513{
514 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
515}
516static ssize_t pages_to_scan_store(struct kobject *kobj,
517 struct kobj_attribute *attr,
518 const char *buf, size_t count)
519{
520 int err;
521 unsigned long pages;
522
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700523 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800524 if (err || !pages || pages > UINT_MAX)
525 return -EINVAL;
526
527 khugepaged_pages_to_scan = pages;
528
529 return count;
530}
531static struct kobj_attribute pages_to_scan_attr =
532 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
533 pages_to_scan_store);
534
535static ssize_t pages_collapsed_show(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 char *buf)
538{
539 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
540}
541static struct kobj_attribute pages_collapsed_attr =
542 __ATTR_RO(pages_collapsed);
543
544static ssize_t full_scans_show(struct kobject *kobj,
545 struct kobj_attribute *attr,
546 char *buf)
547{
548 return sprintf(buf, "%u\n", khugepaged_full_scans);
549}
550static struct kobj_attribute full_scans_attr =
551 __ATTR_RO(full_scans);
552
553static ssize_t khugepaged_defrag_show(struct kobject *kobj,
554 struct kobj_attribute *attr, char *buf)
555{
556 return single_flag_show(kobj, attr, buf,
557 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
558}
559static ssize_t khugepaged_defrag_store(struct kobject *kobj,
560 struct kobj_attribute *attr,
561 const char *buf, size_t count)
562{
563 return single_flag_store(kobj, attr, buf, count,
564 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
565}
566static struct kobj_attribute khugepaged_defrag_attr =
567 __ATTR(defrag, 0644, khugepaged_defrag_show,
568 khugepaged_defrag_store);
569
570/*
571 * max_ptes_none controls if khugepaged should collapse hugepages over
572 * any unmapped ptes in turn potentially increasing the memory
573 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
574 * reduce the available free memory in the system as it
575 * runs. Increasing max_ptes_none will instead potentially reduce the
576 * free memory in the system during the khugepaged scan.
577 */
578static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
579 struct kobj_attribute *attr,
580 char *buf)
581{
582 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
583}
584static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
585 struct kobj_attribute *attr,
586 const char *buf, size_t count)
587{
588 int err;
589 unsigned long max_ptes_none;
590
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700591 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800592 if (err || max_ptes_none > HPAGE_PMD_NR-1)
593 return -EINVAL;
594
595 khugepaged_max_ptes_none = max_ptes_none;
596
597 return count;
598}
599static struct kobj_attribute khugepaged_max_ptes_none_attr =
600 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
601 khugepaged_max_ptes_none_store);
602
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700603static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
604 struct kobj_attribute *attr,
605 char *buf)
606{
607 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
608}
609
610static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
611 struct kobj_attribute *attr,
612 const char *buf, size_t count)
613{
614 int err;
615 unsigned long max_ptes_swap;
616
617 err = kstrtoul(buf, 10, &max_ptes_swap);
618 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
619 return -EINVAL;
620
621 khugepaged_max_ptes_swap = max_ptes_swap;
622
623 return count;
624}
625
626static struct kobj_attribute khugepaged_max_ptes_swap_attr =
627 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
628 khugepaged_max_ptes_swap_store);
629
Andrea Arcangeliba761492011-01-13 15:46:58 -0800630static struct attribute *khugepaged_attr[] = {
631 &khugepaged_defrag_attr.attr,
632 &khugepaged_max_ptes_none_attr.attr,
633 &pages_to_scan_attr.attr,
634 &pages_collapsed_attr.attr,
635 &full_scans_attr.attr,
636 &scan_sleep_millisecs_attr.attr,
637 &alloc_sleep_millisecs_attr.attr,
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700638 &khugepaged_max_ptes_swap_attr.attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800639 NULL,
640};
641
642static struct attribute_group khugepaged_attr_group = {
643 .attrs = khugepaged_attr,
644 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800645};
Shaohua Li569e5592012-01-12 17:19:11 -0800646
647static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
648{
649 int err;
650
651 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
652 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700653 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800654 return -ENOMEM;
655 }
656
657 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
658 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700659 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800660 goto delete_obj;
661 }
662
663 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
664 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700665 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800666 goto remove_hp_group;
667 }
668
669 return 0;
670
671remove_hp_group:
672 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
673delete_obj:
674 kobject_put(*hugepage_kobj);
675 return err;
676}
677
678static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
679{
680 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
681 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
682 kobject_put(hugepage_kobj);
683}
684#else
685static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
686{
687 return 0;
688}
689
690static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
691{
692}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800693#endif /* CONFIG_SYSFS */
694
695static int __init hugepage_init(void)
696{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800697 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800698 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800699
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800700 if (!has_transparent_hugepage()) {
701 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800702 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800703 }
704
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530705 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
706 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700707 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530708 /*
709 * hugepages can't be allocated by the buddy allocator
710 */
711 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
712 /*
713 * we use page->mapping and page->index in second tail page
714 * as list_head: assuming THP order >= 2
715 */
716 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
717
Shaohua Li569e5592012-01-12 17:19:11 -0800718 err = hugepage_init_sysfs(&hugepage_kobj);
719 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700720 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800721
722 err = khugepaged_slab_init();
723 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700724 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800725
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700726 err = register_shrinker(&huge_zero_page_shrinker);
727 if (err)
728 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800729 err = register_shrinker(&deferred_split_shrinker);
730 if (err)
731 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800732
Rik van Riel97562cd2011-01-13 15:47:12 -0800733 /*
734 * By default disable transparent hugepages on smaller systems,
735 * where the extra memory used could hurt more than TLB overhead
736 * is likely to save. The admin can still enable it through /sys.
737 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700738 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800739 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700740 return 0;
741 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800742
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700743 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700744 if (err)
745 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800746
Shaohua Li569e5592012-01-12 17:19:11 -0800747 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700748err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800749 unregister_shrinker(&deferred_split_shrinker);
750err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700751 unregister_shrinker(&huge_zero_page_shrinker);
752err_hzp_shrinker:
753 khugepaged_slab_exit();
754err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800755 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700756err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800757 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800758}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800759subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800760
761static int __init setup_transparent_hugepage(char *str)
762{
763 int ret = 0;
764 if (!str)
765 goto out;
766 if (!strcmp(str, "always")) {
767 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
768 &transparent_hugepage_flags);
769 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
770 &transparent_hugepage_flags);
771 ret = 1;
772 } else if (!strcmp(str, "madvise")) {
773 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
774 &transparent_hugepage_flags);
775 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
776 &transparent_hugepage_flags);
777 ret = 1;
778 } else if (!strcmp(str, "never")) {
779 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
780 &transparent_hugepage_flags);
781 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
782 &transparent_hugepage_flags);
783 ret = 1;
784 }
785out:
786 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700787 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800788 return ret;
789}
790__setup("transparent_hugepage=", setup_transparent_hugepage);
791
Mel Gormanb32967f2012-11-19 12:35:47 +0000792pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800793{
794 if (likely(vma->vm_flags & VM_WRITE))
795 pmd = pmd_mkwrite(pmd);
796 return pmd;
797}
798
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700799static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800800{
Yang Shi340a43b2016-05-20 16:57:30 -0700801 return pmd_mkhuge(mk_pmd(page, prot));
Bob Liub3092b32012-12-11 16:00:41 -0800802}
803
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800804static inline struct list_head *page_deferred_list(struct page *page)
805{
806 /*
807 * ->lru in the tail pages is occupied by compound_head.
808 * Let's use ->mapping + ->index in the second tail page as list_head.
809 */
810 return (struct list_head *)&page[2].mapping;
811}
812
813void prep_transhuge_page(struct page *page)
814{
815 /*
816 * we use page->mapping and page->indexlru in second tail page
817 * as list_head: assuming THP order >= 2
818 */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800819
820 INIT_LIST_HEAD(page_deferred_list(page));
821 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
822}
823
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800824static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
825 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700826 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700827 struct page *page, gfp_t gfp,
828 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800829{
Johannes Weiner00501b52014-08-08 14:19:20 -0700830 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800831 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800832 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700833 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800834
Sasha Levin309381fea2014-01-23 15:52:54 -0800835 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700836
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800837 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700838 put_page(page);
839 count_vm_event(THP_FAULT_FALLBACK);
840 return VM_FAULT_FALLBACK;
841 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800842
Johannes Weiner00501b52014-08-08 14:19:20 -0700843 pgtable = pte_alloc_one(mm, haddr);
844 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800845 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700846 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700847 return VM_FAULT_OOM;
848 }
849
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800850 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700851 /*
852 * The memory barrier inside __SetPageUptodate makes sure that
853 * clear_huge_page writes become visible before the set_pmd_at()
854 * write.
855 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800856 __SetPageUptodate(page);
857
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800858 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800859 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800860 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800861 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800862 put_page(page);
863 pte_free(mm, pgtable);
864 } else {
865 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700866
867 /* Deliver the page fault to userland */
868 if (userfaultfd_missing(vma)) {
869 int ret;
870
871 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800872 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700873 put_page(page);
874 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700875 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700876 VM_UFFD_MISSING);
877 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
878 return ret;
879 }
880
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700881 entry = mk_huge_pmd(page, vma->vm_page_prot);
882 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800883 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800884 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700885 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700886 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800887 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800888 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800889 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800890 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700891 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800892 }
893
David Rientjesaa2e8782012-05-29 15:06:17 -0700894 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800895}
896
Mel Gorman444eb2a42016-03-17 14:19:23 -0700897/*
898 * If THP is set to always then directly reclaim/compact as necessary
899 * If set to defer then do no reclaim and defer to khugepaged
900 * If set to madvise and the VMA is flagged then directly reclaim/compact
901 */
902static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800903{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700904 gfp_t reclaim_flags = 0;
905
906 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
907 (vma->vm_flags & VM_HUGEPAGE))
908 reclaim_flags = __GFP_DIRECT_RECLAIM;
909 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
910 reclaim_flags = __GFP_KSWAPD_RECLAIM;
911 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
912 reclaim_flags = __GFP_DIRECT_RECLAIM;
913
914 return GFP_TRANSHUGE | reclaim_flags;
915}
916
917/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
918static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
919{
920 return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800921}
922
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800923/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700924static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800925 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700926 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800927{
928 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700929 if (!pmd_none(*pmd))
930 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700931 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800932 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800933 if (pgtable)
934 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800935 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800936 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700937 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800938}
939
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800940int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
941 unsigned long address, pmd_t *pmd,
942 unsigned int flags)
943{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800944 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800945 struct page *page;
946 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800947
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700948 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700949 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700950 if (unlikely(anon_vma_prepare(vma)))
951 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700952 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700953 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200954 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700955 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800956 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700957 pgtable_t pgtable;
958 struct page *zero_page;
959 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700960 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700961 pgtable = pte_alloc_one(mm, haddr);
962 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800963 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700964 zero_page = get_huge_zero_page();
965 if (unlikely(!zero_page)) {
966 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700967 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700968 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700969 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800970 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700971 ret = 0;
972 set = false;
973 if (pmd_none(*pmd)) {
974 if (userfaultfd_missing(vma)) {
975 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700976 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700977 VM_UFFD_MISSING);
978 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
979 } else {
980 set_huge_zero_page(pgtable, mm, vma,
981 haddr, pmd,
982 zero_page);
983 spin_unlock(ptl);
984 set = true;
985 }
986 } else
987 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700988 if (!set) {
989 pte_free(mm, pgtable);
990 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800991 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700992 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800993 }
Mel Gorman444eb2a42016-03-17 14:19:23 -0700994 gfp = alloc_hugepage_direct_gfpmask(vma);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800995 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700996 if (unlikely(!page)) {
997 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700998 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700999 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001000 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -07001001 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
1002 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001003}
1004
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001005static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -08001006 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001007{
1008 struct mm_struct *mm = vma->vm_mm;
1009 pmd_t entry;
1010 spinlock_t *ptl;
1011
1012 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -08001013 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
1014 if (pfn_t_devmap(pfn))
1015 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -08001016 if (write) {
1017 entry = pmd_mkyoung(pmd_mkdirty(entry));
1018 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001019 }
Ross Zwisler01871e52016-01-15 16:56:02 -08001020 set_pmd_at(mm, addr, pmd, entry);
1021 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001022 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001023}
1024
1025int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -08001026 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001027{
1028 pgprot_t pgprot = vma->vm_page_prot;
1029 /*
1030 * If we had pmd_special, we could avoid all these restrictions,
1031 * but we need to be consistent with PTEs and architectures that
1032 * can't support a 'special' bit.
1033 */
1034 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1035 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1036 (VM_PFNMAP|VM_MIXEDMAP));
1037 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -08001038 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001039
1040 if (addr < vma->vm_start || addr >= vma->vm_end)
1041 return VM_FAULT_SIGBUS;
1042 if (track_pfn_insert(vma, &pgprot, pfn))
1043 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001044 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
1045 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001046}
Dan Williamsdee41072016-05-14 12:20:44 -07001047EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001048
Dan Williams3565fce2016-01-15 16:56:55 -08001049static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1050 pmd_t *pmd)
1051{
1052 pmd_t _pmd;
1053
1054 /*
1055 * We should set the dirty bit only for FOLL_WRITE but for now
1056 * the dirty bit in the pmd is meaningless. And if the dirty
1057 * bit will become meaningful and we'll only set it with
1058 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1059 * set the young bit, instead of the current set_pmd_at.
1060 */
1061 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1062 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1063 pmd, _pmd, 1))
1064 update_mmu_cache_pmd(vma, addr, pmd);
1065}
1066
1067struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1068 pmd_t *pmd, int flags)
1069{
1070 unsigned long pfn = pmd_pfn(*pmd);
1071 struct mm_struct *mm = vma->vm_mm;
1072 struct dev_pagemap *pgmap;
1073 struct page *page;
1074
1075 assert_spin_locked(pmd_lockptr(mm, pmd));
1076
1077 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1078 return NULL;
1079
1080 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1081 /* pass */;
1082 else
1083 return NULL;
1084
1085 if (flags & FOLL_TOUCH)
1086 touch_pmd(vma, addr, pmd);
1087
1088 /*
1089 * device mapped pages can only be returned if the
1090 * caller will manage the page reference count.
1091 */
1092 if (!(flags & FOLL_GET))
1093 return ERR_PTR(-EEXIST);
1094
1095 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1096 pgmap = get_dev_pagemap(pfn, NULL);
1097 if (!pgmap)
1098 return ERR_PTR(-EFAULT);
1099 page = pfn_to_page(pfn);
1100 get_page(page);
1101 put_dev_pagemap(pgmap);
1102
1103 return page;
1104}
1105
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001106int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1107 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1108 struct vm_area_struct *vma)
1109{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001110 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001111 struct page *src_page;
1112 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001113 pgtable_t pgtable = NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001114 int ret;
1115
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001116 if (!vma_is_dax(vma)) {
1117 ret = -ENOMEM;
1118 pgtable = pte_alloc_one(dst_mm, addr);
1119 if (unlikely(!pgtable))
1120 goto out;
1121 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001122
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001123 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1124 src_ptl = pmd_lockptr(src_mm, src_pmd);
1125 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001126
1127 ret = -EAGAIN;
1128 pmd = *src_pmd;
Dan Williams5c7fb562016-01-15 16:56:52 -08001129 if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001130 pte_free(dst_mm, pgtable);
1131 goto out_unlock;
1132 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001133 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001134 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001135 * under splitting since we don't split the page itself, only pmd to
1136 * a page table.
1137 */
1138 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001139 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001140 /*
1141 * get_huge_zero_page() will never allocate a new page here,
1142 * since we already have a zero page to copy. It just takes a
1143 * reference.
1144 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001145 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001146 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001147 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001148 ret = 0;
1149 goto out_unlock;
1150 }
Mel Gormande466bd2013-12-18 17:08:42 -08001151
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001152 if (!vma_is_dax(vma)) {
Dan Williams5c7fb562016-01-15 16:56:52 -08001153 /* thp accounting separate from pmd_devmap accounting */
1154 src_page = pmd_page(pmd);
1155 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1156 get_page(src_page);
1157 page_dup_rmap(src_page, true);
1158 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1159 atomic_long_inc(&dst_mm->nr_ptes);
1160 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1161 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001162
1163 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1164 pmd = pmd_mkold(pmd_wrprotect(pmd));
1165 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001166
1167 ret = 0;
1168out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001169 spin_unlock(src_ptl);
1170 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001171out:
1172 return ret;
1173}
1174
Will Deacona1dd4502012-12-11 16:01:27 -08001175void huge_pmd_set_accessed(struct mm_struct *mm,
1176 struct vm_area_struct *vma,
1177 unsigned long address,
1178 pmd_t *pmd, pmd_t orig_pmd,
1179 int dirty)
1180{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001181 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001182 pmd_t entry;
1183 unsigned long haddr;
1184
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001185 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001186 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1187 goto unlock;
1188
1189 entry = pmd_mkyoung(orig_pmd);
1190 haddr = address & HPAGE_PMD_MASK;
1191 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1192 update_mmu_cache_pmd(vma, address, pmd);
1193
1194unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001195 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001196}
1197
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001198static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1199 struct vm_area_struct *vma,
1200 unsigned long address,
1201 pmd_t *pmd, pmd_t orig_pmd,
1202 struct page *page,
1203 unsigned long haddr)
1204{
Johannes Weiner00501b52014-08-08 14:19:20 -07001205 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001206 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 pgtable_t pgtable;
1208 pmd_t _pmd;
1209 int ret = 0, i;
1210 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001211 unsigned long mmun_start; /* For mmu_notifiers */
1212 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001213
1214 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1215 GFP_KERNEL);
1216 if (unlikely(!pages)) {
1217 ret |= VM_FAULT_OOM;
1218 goto out;
1219 }
1220
1221 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001222 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1223 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001224 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001225 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001226 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001227 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001228 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001229 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001230 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001231 memcg = (void *)page_private(pages[i]);
1232 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001233 mem_cgroup_cancel_charge(pages[i], memcg,
1234 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001235 put_page(pages[i]);
1236 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001237 kfree(pages);
1238 ret |= VM_FAULT_OOM;
1239 goto out;
1240 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001241 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 }
1243
1244 for (i = 0; i < HPAGE_PMD_NR; i++) {
1245 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001246 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001247 __SetPageUptodate(pages[i]);
1248 cond_resched();
1249 }
1250
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001251 mmun_start = haddr;
1252 mmun_end = haddr + HPAGE_PMD_SIZE;
1253 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1254
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001255 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001256 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1257 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001258 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001259
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001260 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001261 /* leave pmd empty until pte is filled */
1262
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001263 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001264 pmd_populate(mm, &_pmd, pgtable);
1265
1266 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1267 pte_t *pte, entry;
1268 entry = mk_pte(pages[i], vma->vm_page_prot);
1269 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001270 memcg = (void *)page_private(pages[i]);
1271 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001272 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001273 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001274 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001275 pte = pte_offset_map(&_pmd, haddr);
1276 VM_BUG_ON(!pte_none(*pte));
1277 set_pte_at(mm, haddr, pte, entry);
1278 pte_unmap(pte);
1279 }
1280 kfree(pages);
1281
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001282 smp_wmb(); /* make pte visible before pmd */
1283 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001284 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001285 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001286
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001287 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1288
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001289 ret |= VM_FAULT_WRITE;
1290 put_page(page);
1291
1292out:
1293 return ret;
1294
1295out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001296 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001297 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001298 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001299 memcg = (void *)page_private(pages[i]);
1300 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001301 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001302 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001303 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001304 kfree(pages);
1305 goto out;
1306}
1307
1308int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1309 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1310{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001311 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001312 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001313 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001314 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001315 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001316 unsigned long mmun_start; /* For mmu_notifiers */
1317 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001318 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001319
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001320 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001321 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001322 haddr = address & HPAGE_PMD_MASK;
1323 if (is_huge_zero_pmd(orig_pmd))
1324 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001325 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001326 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1327 goto out_unlock;
1328
1329 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001330 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001331 /*
1332 * We can only reuse the page if nobody else maps the huge page or it's
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001333 * part.
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001334 */
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001335 if (page_trans_huge_mapcount(page, NULL) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001336 pmd_t entry;
1337 entry = pmd_mkyoung(orig_pmd);
1338 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1339 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001340 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001341 ret |= VM_FAULT_WRITE;
1342 goto out_unlock;
1343 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001344 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001345 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001346alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001347 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001348 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001349 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b363692015-04-15 16:13:29 -07001350 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001351 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001352 new_page = NULL;
1353
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001354 if (likely(new_page)) {
1355 prep_transhuge_page(new_page);
1356 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001357 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001358 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001359 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001360 } else {
1361 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1362 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001363 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001364 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001365 ret |= VM_FAULT_FALLBACK;
1366 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001367 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001368 }
David Rientjes17766dd2013-09-12 15:14:06 -07001369 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001370 goto out;
1371 }
1372
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001373 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1374 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001375 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001376 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001377 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001378 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001379 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001380 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001381 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001382 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001383 goto out;
1384 }
1385
David Rientjes17766dd2013-09-12 15:14:06 -07001386 count_vm_event(THP_FAULT_ALLOC);
1387
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001388 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001389 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1390 else
1391 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001392 __SetPageUptodate(new_page);
1393
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001394 mmun_start = haddr;
1395 mmun_end = haddr + HPAGE_PMD_SIZE;
1396 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1397
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001398 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001399 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001400 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001401 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001402 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001403 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001404 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001405 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001406 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001407 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001408 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1409 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001410 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001411 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001412 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001413 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001414 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001415 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001416 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001417 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001418 put_huge_zero_page();
1419 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001420 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001421 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001422 put_page(page);
1423 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001424 ret |= VM_FAULT_WRITE;
1425 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001426 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001427out_mn:
1428 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1429out:
1430 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001431out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001432 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001433 return ret;
1434}
1435
David Rientjesb676b292012-10-08 16:34:03 -07001436struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001437 unsigned long addr,
1438 pmd_t *pmd,
1439 unsigned int flags)
1440{
David Rientjesb676b292012-10-08 16:34:03 -07001441 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001442 struct page *page = NULL;
1443
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001444 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001445
1446 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1447 goto out;
1448
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001449 /* Avoid dumping huge zero page */
1450 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1451 return ERR_PTR(-EFAULT);
1452
Mel Gorman2b4847e2013-12-18 17:08:32 -08001453 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001454 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001455 goto out;
1456
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001457 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001458 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001459 if (flags & FOLL_TOUCH)
1460 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001461 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001462 /*
1463 * We don't mlock() pte-mapped THPs. This way we can avoid
1464 * leaking mlocked pages into non-VM_LOCKED VMAs.
1465 *
1466 * In most cases the pmd is the only mapping of the page as we
1467 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1468 * writable private mappings in populate_vma_page_range().
1469 *
1470 * The only scenario when we have the page shared here is if we
1471 * mlocking read-only mapping shared over fork(). We skip
1472 * mlocking such pages.
1473 */
1474 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1475 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001476 lru_add_drain();
1477 if (page->mapping)
1478 mlock_vma_page(page);
1479 unlock_page(page);
1480 }
1481 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001482 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001483 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001484 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001485 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001486
1487out:
1488 return page;
1489}
1490
Mel Gormand10e63f2012-10-25 14:16:31 +02001491/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001492int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1493 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001494{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001495 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001496 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001497 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001498 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001499 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001500 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001501 bool page_locked;
1502 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001503 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001504 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001505
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001506 /* A PROT_NONE fault should not end up here */
1507 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1508
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001509 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001510 if (unlikely(!pmd_same(pmd, *pmdp)))
1511 goto out_unlock;
1512
Mel Gormande466bd2013-12-18 17:08:42 -08001513 /*
1514 * If there are potential migrations, wait for completion and retry
1515 * without disrupting NUMA hinting information. Do not relock and
1516 * check_same as the page may no longer be mapped.
1517 */
1518 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001519 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001520 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001521 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001522 goto out;
1523 }
1524
Mel Gormand10e63f2012-10-25 14:16:31 +02001525 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001526 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001527 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001528 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001529 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001530 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001531 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001532 flags |= TNF_FAULT_LOCAL;
1533 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001534
Mel Gormanbea66fb2015-03-25 15:55:37 -07001535 /* See similar comment in do_numa_page for explanation */
1536 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001537 flags |= TNF_NO_GROUP;
1538
1539 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001540 * Acquire the page lock to serialise THP migrations but avoid dropping
1541 * page_table_lock if at all possible
1542 */
Mel Gormanb8916632013-10-07 11:28:44 +01001543 page_locked = trylock_page(page);
1544 target_nid = mpol_misplaced(page, vma, haddr);
1545 if (target_nid == -1) {
1546 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001547 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001548 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001549 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001550
Mel Gormande466bd2013-12-18 17:08:42 -08001551 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001552 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001553 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001554 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001555 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001556 goto out;
1557 }
1558
Mel Gorman2b4847e2013-12-18 17:08:32 -08001559 /*
1560 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1561 * to serialises splits
1562 */
Mel Gormanb8916632013-10-07 11:28:44 +01001563 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001564 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001565 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001566
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001567 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001568 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001569 if (unlikely(!pmd_same(pmd, *pmdp))) {
1570 unlock_page(page);
1571 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001572 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001573 goto out_unlock;
1574 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001575
Mel Gormanc3a489c2013-12-18 17:08:38 -08001576 /* Bail if we fail to protect against THP splits for any reason */
1577 if (unlikely(!anon_vma)) {
1578 put_page(page);
1579 page_nid = -1;
1580 goto clear_pmdnuma;
1581 }
1582
Mel Gormana54a4072013-10-07 11:28:46 +01001583 /*
1584 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001585 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001586 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001587 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001588 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001589 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001590 if (migrated) {
1591 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001592 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001593 } else
1594 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001595
Mel Gorman8191acb2013-10-07 11:28:45 +01001596 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001597clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001598 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001599 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001600 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001601 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001602 if (was_writable)
1603 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001604 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001605 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001606 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001607out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001608 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001609
1610out:
1611 if (anon_vma)
1612 page_unlock_anon_vma_read(anon_vma);
1613
Mel Gorman8191acb2013-10-07 11:28:45 +01001614 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001615 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001616
Mel Gormand10e63f2012-10-25 14:16:31 +02001617 return 0;
1618}
1619
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001620int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1621 pmd_t *pmd, unsigned long addr, unsigned long next)
1622
1623{
1624 spinlock_t *ptl;
1625 pmd_t orig_pmd;
1626 struct page *page;
1627 struct mm_struct *mm = tlb->mm;
1628 int ret = 0;
1629
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001630 ptl = pmd_trans_huge_lock(pmd, vma);
1631 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001632 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001633
1634 orig_pmd = *pmd;
1635 if (is_huge_zero_pmd(orig_pmd)) {
1636 ret = 1;
1637 goto out;
1638 }
1639
1640 page = pmd_page(orig_pmd);
1641 /*
1642 * If other processes are mapping this page, we couldn't discard
1643 * the page unless they all do MADV_FREE so let's skip the page.
1644 */
1645 if (page_mapcount(page) != 1)
1646 goto out;
1647
1648 if (!trylock_page(page))
1649 goto out;
1650
1651 /*
1652 * If user want to discard part-pages of THP, split it so MADV_FREE
1653 * will deactivate only them.
1654 */
1655 if (next - addr != HPAGE_PMD_SIZE) {
1656 get_page(page);
1657 spin_unlock(ptl);
Huang Ying9818b8c2016-07-14 12:07:12 -07001658 split_huge_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001659 put_page(page);
1660 unlock_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001661 goto out_unlocked;
1662 }
1663
1664 if (PageDirty(page))
1665 ClearPageDirty(page);
1666 unlock_page(page);
1667
1668 if (PageActive(page))
1669 deactivate_page(page);
1670
1671 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1672 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1673 tlb->fullmm);
1674 orig_pmd = pmd_mkold(orig_pmd);
1675 orig_pmd = pmd_mkclean(orig_pmd);
1676
1677 set_pmd_at(mm, addr, pmd, orig_pmd);
1678 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1679 }
1680 ret = 1;
1681out:
1682 spin_unlock(ptl);
1683out_unlocked:
1684 return ret;
1685}
1686
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001687int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001688 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001689{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001690 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001691 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001692
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001693 ptl = __pmd_trans_huge_lock(pmd, vma);
1694 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001695 return 0;
1696 /*
1697 * For architectures like ppc64 we look at deposited pgtable
1698 * when calling pmdp_huge_get_and_clear. So do the
1699 * pgtable_trans_huge_withdraw after finishing pmdp related
1700 * operations.
1701 */
1702 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1703 tlb->fullmm);
1704 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1705 if (vma_is_dax(vma)) {
1706 spin_unlock(ptl);
1707 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001708 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001709 } else if (is_huge_zero_pmd(orig_pmd)) {
1710 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1711 atomic_long_dec(&tlb->mm->nr_ptes);
1712 spin_unlock(ptl);
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001713 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001714 } else {
1715 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001716 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001717 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1718 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1719 VM_BUG_ON_PAGE(!PageHead(page), page);
1720 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1721 atomic_long_dec(&tlb->mm->nr_ptes);
1722 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07001723 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001724 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001725 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001726}
1727
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001728bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001729 unsigned long new_addr, unsigned long old_end,
1730 pmd_t *old_pmd, pmd_t *new_pmd)
1731{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001732 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001733 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001734 struct mm_struct *mm = vma->vm_mm;
1735
1736 if ((old_addr & ~HPAGE_PMD_MASK) ||
1737 (new_addr & ~HPAGE_PMD_MASK) ||
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001738 old_end - old_addr < HPAGE_PMD_SIZE)
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001739 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001740
1741 /*
1742 * The destination pmd shouldn't be established, free_pgtables()
1743 * should have release it.
1744 */
1745 if (WARN_ON(!pmd_none(*new_pmd))) {
1746 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001747 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001748 }
1749
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001750 /*
1751 * We don't have to worry about the ordering of src and dst
1752 * ptlocks because exclusive mmap_sem prevents deadlock.
1753 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001754 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1755 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001756 new_ptl = pmd_lockptr(mm, new_pmd);
1757 if (new_ptl != old_ptl)
1758 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001759 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001760 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001761
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001762 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1763 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301764 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001765 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1766 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001767 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301768 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1769 if (new_ptl != old_ptl)
1770 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001771 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001772 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001773 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001774 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001775}
1776
Mel Gormanf123d742013-10-07 11:28:49 +01001777/*
1778 * Returns
1779 * - 0 if PMD could not be locked
1780 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1781 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1782 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001783int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001784 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001785{
1786 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001787 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001788 int ret = 0;
1789
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001790 ptl = __pmd_trans_huge_lock(pmd, vma);
1791 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001792 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001793 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001794 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001795
1796 /*
1797 * Avoid trapping faults against the zero page. The read-only
1798 * data is likely to be read-cached on the local CPU and
1799 * local/remote hits to the zero page are not interesting.
1800 */
1801 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1802 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001803 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001804 }
1805
Mel Gorman10c10452015-02-12 14:58:44 -08001806 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001807 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001808 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001809 if (preserve_write)
1810 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001811 ret = HPAGE_PMD_NR;
1812 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001813 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001814 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001815 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001816 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001817
1818 return ret;
1819}
1820
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001821/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001822 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001823 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001824 * Note that if it returns true, this routine returns without unlocking page
1825 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001826 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001827spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001828{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001829 spinlock_t *ptl;
1830 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001831 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001832 return ptl;
1833 spin_unlock(ptl);
1834 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001835}
1836
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001837#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001838
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001839int hugepage_madvise(struct vm_area_struct *vma,
1840 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001841{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001842 switch (advice) {
1843 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001844#ifdef CONFIG_S390
1845 /*
1846 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1847 * can't handle this properly after s390_enable_sie, so we simply
1848 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1849 */
1850 if (mm_has_pgste(vma->vm_mm))
1851 return 0;
1852#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001853 /*
1854 * Be somewhat over-protective like KSM for now!
1855 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001856 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001857 return -EINVAL;
1858 *vm_flags &= ~VM_NOHUGEPAGE;
1859 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001860 /*
1861 * If the vma become good for khugepaged to scan,
1862 * register it here without waiting a page fault that
1863 * may not happen any time soon.
1864 */
David Rientjes6d50e602014-10-29 14:50:31 -07001865 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001866 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001867 break;
1868 case MADV_NOHUGEPAGE:
1869 /*
1870 * Be somewhat over-protective like KSM for now!
1871 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001872 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001873 return -EINVAL;
1874 *vm_flags &= ~VM_HUGEPAGE;
1875 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001876 /*
1877 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1878 * this vma even if we leave the mm registered in khugepaged if
1879 * it got registered before VM_NOHUGEPAGE was set.
1880 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001881 break;
1882 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001883
1884 return 0;
1885}
1886
Andrea Arcangeliba761492011-01-13 15:46:58 -08001887static int __init khugepaged_slab_init(void)
1888{
1889 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1890 sizeof(struct mm_slot),
1891 __alignof__(struct mm_slot), 0, NULL);
1892 if (!mm_slot_cache)
1893 return -ENOMEM;
1894
1895 return 0;
1896}
1897
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001898static void __init khugepaged_slab_exit(void)
1899{
1900 kmem_cache_destroy(mm_slot_cache);
1901}
1902
Andrea Arcangeliba761492011-01-13 15:46:58 -08001903static inline struct mm_slot *alloc_mm_slot(void)
1904{
1905 if (!mm_slot_cache) /* initialization failed */
1906 return NULL;
1907 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1908}
1909
1910static inline void free_mm_slot(struct mm_slot *mm_slot)
1911{
1912 kmem_cache_free(mm_slot_cache, mm_slot);
1913}
1914
Andrea Arcangeliba761492011-01-13 15:46:58 -08001915static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1916{
1917 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001918
Sasha Levinb67bfe02013-02-27 17:06:00 -08001919 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001920 if (mm == mm_slot->mm)
1921 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001922
Andrea Arcangeliba761492011-01-13 15:46:58 -08001923 return NULL;
1924}
1925
1926static void insert_to_mm_slots_hash(struct mm_struct *mm,
1927 struct mm_slot *mm_slot)
1928{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001929 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001930 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001931}
1932
1933static inline int khugepaged_test_exit(struct mm_struct *mm)
1934{
1935 return atomic_read(&mm->mm_users) == 0;
1936}
1937
1938int __khugepaged_enter(struct mm_struct *mm)
1939{
1940 struct mm_slot *mm_slot;
1941 int wakeup;
1942
1943 mm_slot = alloc_mm_slot();
1944 if (!mm_slot)
1945 return -ENOMEM;
1946
1947 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001948 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001949 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1950 free_mm_slot(mm_slot);
1951 return 0;
1952 }
1953
1954 spin_lock(&khugepaged_mm_lock);
1955 insert_to_mm_slots_hash(mm, mm_slot);
1956 /*
1957 * Insert just behind the scanning cursor, to let the area settle
1958 * down a little.
1959 */
1960 wakeup = list_empty(&khugepaged_scan.mm_head);
1961 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1962 spin_unlock(&khugepaged_mm_lock);
1963
1964 atomic_inc(&mm->mm_count);
1965 if (wakeup)
1966 wake_up_interruptible(&khugepaged_wait);
1967
1968 return 0;
1969}
1970
David Rientjes6d50e602014-10-29 14:50:31 -07001971int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1972 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001973{
1974 unsigned long hstart, hend;
1975 if (!vma->anon_vma)
1976 /*
1977 * Not yet faulted in so we will register later in the
1978 * page fault if needed.
1979 */
1980 return 0;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07001981 if (vma->vm_ops || (vm_flags & VM_NO_THP))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001982 /* khugepaged not yet working on file or special mappings */
1983 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001984 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1985 hend = vma->vm_end & HPAGE_PMD_MASK;
1986 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001987 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001988 return 0;
1989}
1990
1991void __khugepaged_exit(struct mm_struct *mm)
1992{
1993 struct mm_slot *mm_slot;
1994 int free = 0;
1995
1996 spin_lock(&khugepaged_mm_lock);
1997 mm_slot = get_mm_slot(mm);
1998 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001999 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002000 list_del(&mm_slot->mm_node);
2001 free = 1;
2002 }
Chris Wrightd788e802011-07-25 17:12:14 -07002003 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002004
2005 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002006 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2007 free_mm_slot(mm_slot);
2008 mmdrop(mm);
2009 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002010 /*
2011 * This is required to serialize against
2012 * khugepaged_test_exit() (which is guaranteed to run
2013 * under mmap sem read mode). Stop here (after we
2014 * return all pagetables will be destroyed) until
2015 * khugepaged has finished working on the pagetables
2016 * under the mmap_sem.
2017 */
2018 down_write(&mm->mmap_sem);
2019 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002020 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002021}
2022
2023static void release_pte_page(struct page *page)
2024{
2025 /* 0 stands for page_is_file_cache(page) == false */
2026 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2027 unlock_page(page);
2028 putback_lru_page(page);
2029}
2030
2031static void release_pte_pages(pte_t *pte, pte_t *_pte)
2032{
2033 while (--_pte >= pte) {
2034 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002035 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002036 release_pte_page(pte_page(pteval));
2037 }
2038}
2039
Andrea Arcangeliba761492011-01-13 15:46:58 -08002040static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2041 unsigned long address,
2042 pte_t *pte)
2043{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002044 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002045 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002046 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002047 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002048
Andrea Arcangeliba761492011-01-13 15:46:58 -08002049 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2050 _pte++, address += PAGE_SIZE) {
2051 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002052 if (pte_none(pteval) || (pte_present(pteval) &&
2053 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002054 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002055 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002056 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002057 } else {
2058 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002059 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002060 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002061 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002062 if (!pte_present(pteval)) {
2063 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002064 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002065 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002066 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002067 if (unlikely(!page)) {
2068 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002069 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002070 }
Bob Liu344aa352012-12-11 16:00:34 -08002071
Sasha Levin309381fea2014-01-23 15:52:54 -08002072 VM_BUG_ON_PAGE(PageCompound(page), page);
2073 VM_BUG_ON_PAGE(!PageAnon(page), page);
2074 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002075
Andrea Arcangeliba761492011-01-13 15:46:58 -08002076 /*
2077 * We can do it before isolate_lru_page because the
2078 * page can't be freed from under us. NOTE: PG_lock
2079 * is needed to serialize against split_huge_page
2080 * when invoked from the VM.
2081 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002082 if (!trylock_page(page)) {
2083 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002084 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002085 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002086
2087 /*
2088 * cannot use mapcount: can't collapse if there's a gup pin.
2089 * The page must only be referenced by the scanned process
2090 * and page swap cache.
2091 */
2092 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2093 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002094 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002095 goto out;
2096 }
2097 if (pte_write(pteval)) {
2098 writable = true;
2099 } else {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002100 if (PageSwapCache(page) &&
2101 !reuse_swap_page(page, NULL)) {
Ebru Akagunduz10359212015-02-11 15:28:28 -08002102 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002103 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002104 goto out;
2105 }
2106 /*
2107 * Page is not in the swap cache. It can be collapsed
2108 * into a THP.
2109 */
2110 }
2111
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112 /*
2113 * Isolate the page to avoid collapsing an hugepage
2114 * currently in use by the VM.
2115 */
2116 if (isolate_lru_page(page)) {
2117 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002118 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002119 goto out;
2120 }
2121 /* 0 stands for page_is_file_cache(page) == false */
2122 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002123 VM_BUG_ON_PAGE(!PageLocked(page), page);
2124 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002125
2126 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002127 if (pte_young(pteval) ||
2128 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002129 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002130 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002131 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002132 if (likely(writable)) {
2133 if (likely(referenced)) {
2134 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002135 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002136 referenced, writable, result);
2137 return 1;
2138 }
2139 } else {
2140 result = SCAN_PAGE_RO;
2141 }
2142
Andrea Arcangeliba761492011-01-13 15:46:58 -08002143out:
Bob Liu344aa352012-12-11 16:00:34 -08002144 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002145 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002146 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002147 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002148}
2149
2150static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2151 struct vm_area_struct *vma,
2152 unsigned long address,
2153 spinlock_t *ptl)
2154{
2155 pte_t *_pte;
2156 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2157 pte_t pteval = *_pte;
2158 struct page *src_page;
2159
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002160 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002161 clear_user_highpage(page, address);
2162 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002163 if (is_zero_pfn(pte_pfn(pteval))) {
2164 /*
2165 * ptl mostly unnecessary.
2166 */
2167 spin_lock(ptl);
2168 /*
2169 * paravirt calls inside pte_clear here are
2170 * superfluous.
2171 */
2172 pte_clear(vma->vm_mm, address, _pte);
2173 spin_unlock(ptl);
2174 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002175 } else {
2176 src_page = pte_page(pteval);
2177 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002178 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002179 release_pte_page(src_page);
2180 /*
2181 * ptl mostly unnecessary, but preempt has to
2182 * be disabled to update the per-cpu stats
2183 * inside page_remove_rmap().
2184 */
2185 spin_lock(ptl);
2186 /*
2187 * paravirt calls inside pte_clear here are
2188 * superfluous.
2189 */
2190 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002191 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002192 spin_unlock(ptl);
2193 free_page_and_swap_cache(src_page);
2194 }
2195
2196 address += PAGE_SIZE;
2197 page++;
2198 }
2199}
2200
Xiao Guangrong26234f32012-10-08 16:29:51 -07002201static void khugepaged_alloc_sleep(void)
2202{
Petr Mladekbde43c62015-09-08 15:04:05 -07002203 DEFINE_WAIT(wait);
2204
2205 add_wait_queue(&khugepaged_wait, &wait);
2206 freezable_schedule_timeout_interruptible(
2207 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2208 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002209}
2210
Bob Liu9f1b8682013-11-12 15:07:37 -08002211static int khugepaged_node_load[MAX_NUMNODES];
2212
David Rientjes14a4e212014-08-06 16:07:29 -07002213static bool khugepaged_scan_abort(int nid)
2214{
2215 int i;
2216
2217 /*
2218 * If zone_reclaim_mode is disabled, then no extra effort is made to
2219 * allocate memory locally.
2220 */
2221 if (!zone_reclaim_mode)
2222 return false;
2223
2224 /* If there is a count for this node already, it must be acceptable */
2225 if (khugepaged_node_load[nid])
2226 return false;
2227
2228 for (i = 0; i < MAX_NUMNODES; i++) {
2229 if (!khugepaged_node_load[i])
2230 continue;
2231 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2232 return true;
2233 }
2234 return false;
2235}
2236
Xiao Guangrong26234f32012-10-08 16:29:51 -07002237#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002238static int khugepaged_find_target_node(void)
2239{
2240 static int last_khugepaged_target_node = NUMA_NO_NODE;
2241 int nid, target_node = 0, max_value = 0;
2242
2243 /* find first node with max normal pages hit */
2244 for (nid = 0; nid < MAX_NUMNODES; nid++)
2245 if (khugepaged_node_load[nid] > max_value) {
2246 max_value = khugepaged_node_load[nid];
2247 target_node = nid;
2248 }
2249
2250 /* do some balance if several nodes have the same hit record */
2251 if (target_node <= last_khugepaged_target_node)
2252 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2253 nid++)
2254 if (max_value == khugepaged_node_load[nid]) {
2255 target_node = nid;
2256 break;
2257 }
2258
2259 last_khugepaged_target_node = target_node;
2260 return target_node;
2261}
2262
Xiao Guangrong26234f32012-10-08 16:29:51 -07002263static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2264{
2265 if (IS_ERR(*hpage)) {
2266 if (!*wait)
2267 return false;
2268
2269 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002270 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002271 khugepaged_alloc_sleep();
2272 } else if (*hpage) {
2273 put_page(*hpage);
2274 *hpage = NULL;
2275 }
2276
2277 return true;
2278}
2279
Michal Hocko3b363692015-04-15 16:13:29 -07002280static struct page *
2281khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002282 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002283{
Sasha Levin309381fea2014-01-23 15:52:54 -08002284 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002285
Xiao Guangrong26234f32012-10-08 16:29:51 -07002286 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002287 * Before allocating the hugepage, release the mmap_sem read lock.
2288 * The allocation can take potentially a long time if it involves
2289 * sync compaction, and we do not need to hold the mmap_sem during
2290 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002291 */
2292 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002293
Vlastimil Babka96db8002015-09-08 15:03:50 -07002294 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002295 if (unlikely(!*hpage)) {
2296 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2297 *hpage = ERR_PTR(-ENOMEM);
2298 return NULL;
2299 }
2300
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002301 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002302 count_vm_event(THP_COLLAPSE_ALLOC);
2303 return *hpage;
2304}
2305#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002306static int khugepaged_find_target_node(void)
2307{
2308 return 0;
2309}
2310
Mel Gorman444eb2a42016-03-17 14:19:23 -07002311static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002312{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002313 struct page *page;
2314
Mel Gorman444eb2a42016-03-17 14:19:23 -07002315 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2316 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002317 if (page)
2318 prep_transhuge_page(page);
2319 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002320}
2321
Xiao Guangrong26234f32012-10-08 16:29:51 -07002322static struct page *khugepaged_alloc_hugepage(bool *wait)
2323{
2324 struct page *hpage;
2325
2326 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002327 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002328 if (!hpage) {
2329 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2330 if (!*wait)
2331 return NULL;
2332
2333 *wait = false;
2334 khugepaged_alloc_sleep();
2335 } else
2336 count_vm_event(THP_COLLAPSE_ALLOC);
2337 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2338
2339 return hpage;
2340}
2341
2342static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2343{
2344 if (!*hpage)
2345 *hpage = khugepaged_alloc_hugepage(wait);
2346
2347 if (unlikely(!*hpage))
2348 return false;
2349
2350 return true;
2351}
2352
Michal Hocko3b363692015-04-15 16:13:29 -07002353static struct page *
2354khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002355 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002356{
2357 up_read(&mm->mmap_sem);
2358 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002359
Xiao Guangrong26234f32012-10-08 16:29:51 -07002360 return *hpage;
2361}
2362#endif
2363
Bob Liufa475e52012-12-11 16:00:39 -08002364static bool hugepage_vma_check(struct vm_area_struct *vma)
2365{
2366 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2367 (vma->vm_flags & VM_NOHUGEPAGE))
2368 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002369 if (!vma->anon_vma || vma->vm_ops)
2370 return false;
2371 if (is_vma_temporary_stack(vma))
2372 return false;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07002373 return !(vma->vm_flags & VM_NO_THP);
Bob Liufa475e52012-12-11 16:00:39 -08002374}
2375
Andrea Arcangeliba761492011-01-13 15:46:58 -08002376static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002377 unsigned long address,
2378 struct page **hpage,
2379 struct vm_area_struct *vma,
2380 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002381{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002382 pmd_t *pmd, _pmd;
2383 pte_t *pte;
2384 pgtable_t pgtable;
2385 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002386 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002387 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002388 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002389 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002390 unsigned long mmun_start; /* For mmu_notifiers */
2391 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002392 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393
2394 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002395
Michal Hocko3b363692015-04-15 16:13:29 -07002396 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002397 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b363692015-04-15 16:13:29 -07002398
Xiao Guangrong26234f32012-10-08 16:29:51 -07002399 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002400 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002401 if (!new_page) {
2402 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2403 goto out_nolock;
2404 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002405
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002406 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002407 result = SCAN_CGROUP_CHARGE_FAIL;
2408 goto out_nolock;
2409 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002410
2411 /*
2412 * Prevent all access to pagetables with the exception of
2413 * gup_fast later hanlded by the ptep_clear_flush and the VM
2414 * handled by the anon_vma lock + PG_lock.
2415 */
2416 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002417 if (unlikely(khugepaged_test_exit(mm))) {
2418 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002419 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002420 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002421
2422 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002423 if (!vma) {
2424 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002425 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002426 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002427 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2428 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002429 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2430 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002431 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002432 }
2433 if (!hugepage_vma_check(vma)) {
2434 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002435 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002436 }
Bob Liu62190492012-12-11 16:00:37 -08002437 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002438 if (!pmd) {
2439 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002440 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002441 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002442
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002443 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002444
2445 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002446 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002447
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002448 mmun_start = address;
2449 mmun_end = address + HPAGE_PMD_SIZE;
2450 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002451 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002452 /*
2453 * After this gup_fast can't run anymore. This also removes
2454 * any huge TLB entry from the CPU so we won't allow
2455 * huge and small TLB entries for the same virtual address
2456 * to avoid the risk of CPU bugs in that area.
2457 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002458 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002459 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002460 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002461
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002462 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002463 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002464 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465
2466 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002467 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002468 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002469 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002470 /*
2471 * We can only use set_pmd_at when establishing
2472 * hugepmds and never for establishing regular pmds that
2473 * points to regular pagetables. Use pmd_populate for that
2474 */
2475 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002476 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002477 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002478 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002479 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002480 }
2481
2482 /*
2483 * All pages are isolated and locked so anon_vma rmap
2484 * can't run anymore.
2485 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002486 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002487
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002488 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002489 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002490 __SetPageUptodate(new_page);
2491 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002492
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002493 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2494 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002495
2496 /*
2497 * spin_lock() below is not the equivalent of smp_wmb(), so
2498 * this is needed to avoid the copy_huge_page writes to become
2499 * visible after the set_pmd_at() write.
2500 */
2501 smp_wmb();
2502
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002503 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002504 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002505 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002506 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002507 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002508 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002509 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002510 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002511 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002512
2513 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002514
Andrea Arcangeliba761492011-01-13 15:46:58 -08002515 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002516 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002517out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002518 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002519 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002520 return;
2521
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002522out_nolock:
2523 trace_mm_collapse_huge_page(mm, isolated, result);
2524 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002525out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002526 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002527 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002528}
2529
2530static int khugepaged_scan_pmd(struct mm_struct *mm,
2531 struct vm_area_struct *vma,
2532 unsigned long address,
2533 struct page **hpage)
2534{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535 pmd_t *pmd;
2536 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002537 int ret = 0, none_or_zero = 0, result = 0;
2538 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002539 unsigned long _address;
2540 spinlock_t *ptl;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002541 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002542 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002543
2544 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2545
Bob Liu62190492012-12-11 16:00:37 -08002546 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002547 if (!pmd) {
2548 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002549 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002550 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002551
Bob Liu9f1b8682013-11-12 15:07:37 -08002552 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002553 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2554 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2555 _pte++, _address += PAGE_SIZE) {
2556 pte_t pteval = *_pte;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002557 if (is_swap_pte(pteval)) {
2558 if (++unmapped <= khugepaged_max_ptes_swap) {
2559 continue;
2560 } else {
2561 result = SCAN_EXCEED_SWAP_PTE;
2562 goto out_unmap;
2563 }
2564 }
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002565 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002566 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002567 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002568 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002569 } else {
2570 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002572 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002573 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002574 if (!pte_present(pteval)) {
2575 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002577 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002578 if (pte_write(pteval))
2579 writable = true;
2580
Andrea Arcangeliba761492011-01-13 15:46:58 -08002581 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002582 if (unlikely(!page)) {
2583 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002584 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002585 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002586
2587 /* TODO: teach khugepaged to collapse THP mapped with pte */
2588 if (PageCompound(page)) {
2589 result = SCAN_PAGE_COMPOUND;
2590 goto out_unmap;
2591 }
2592
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002593 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002594 * Record which node the original page is from and save this
2595 * information to khugepaged_node_load[].
2596 * Khupaged will allocate hugepage from the node has the max
2597 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002598 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002599 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002600 if (khugepaged_scan_abort(node)) {
2601 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002602 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002603 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002604 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002605 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002606 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002608 }
2609 if (PageLocked(page)) {
2610 result = SCAN_PAGE_LOCK;
2611 goto out_unmap;
2612 }
2613 if (!PageAnon(page)) {
2614 result = SCAN_PAGE_ANON;
2615 goto out_unmap;
2616 }
2617
Ebru Akagunduz10359212015-02-11 15:28:28 -08002618 /*
2619 * cannot use mapcount: can't collapse if there's a gup pin.
2620 * The page must only be referenced by the scanned process
2621 * and page swap cache.
2622 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002623 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2624 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002625 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002626 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002627 if (pte_young(pteval) ||
2628 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002629 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002630 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002631 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002632 if (writable) {
2633 if (referenced) {
2634 result = SCAN_SUCCEED;
2635 ret = 1;
2636 } else {
2637 result = SCAN_NO_REFERENCED_PAGE;
2638 }
2639 } else {
2640 result = SCAN_PAGE_RO;
2641 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002642out_unmap:
2643 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002644 if (ret) {
2645 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002646 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002647 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002648 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002649out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002650 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002651 none_or_zero, result, unmapped);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002652 return ret;
2653}
2654
2655static void collect_mm_slot(struct mm_slot *mm_slot)
2656{
2657 struct mm_struct *mm = mm_slot->mm;
2658
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002659 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002660
2661 if (khugepaged_test_exit(mm)) {
2662 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002663 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002664 list_del(&mm_slot->mm_node);
2665
2666 /*
2667 * Not strictly needed because the mm exited already.
2668 *
2669 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2670 */
2671
2672 /* khugepaged_mm_lock actually not necessary for the below */
2673 free_mm_slot(mm_slot);
2674 mmdrop(mm);
2675 }
2676}
2677
2678static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2679 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002680 __releases(&khugepaged_mm_lock)
2681 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002682{
2683 struct mm_slot *mm_slot;
2684 struct mm_struct *mm;
2685 struct vm_area_struct *vma;
2686 int progress = 0;
2687
2688 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002689 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002690
2691 if (khugepaged_scan.mm_slot)
2692 mm_slot = khugepaged_scan.mm_slot;
2693 else {
2694 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2695 struct mm_slot, mm_node);
2696 khugepaged_scan.address = 0;
2697 khugepaged_scan.mm_slot = mm_slot;
2698 }
2699 spin_unlock(&khugepaged_mm_lock);
2700
2701 mm = mm_slot->mm;
2702 down_read(&mm->mmap_sem);
2703 if (unlikely(khugepaged_test_exit(mm)))
2704 vma = NULL;
2705 else
2706 vma = find_vma(mm, khugepaged_scan.address);
2707
2708 progress++;
2709 for (; vma; vma = vma->vm_next) {
2710 unsigned long hstart, hend;
2711
2712 cond_resched();
2713 if (unlikely(khugepaged_test_exit(mm))) {
2714 progress++;
2715 break;
2716 }
Bob Liufa475e52012-12-11 16:00:39 -08002717 if (!hugepage_vma_check(vma)) {
2718skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002719 progress++;
2720 continue;
2721 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002722 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2723 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002724 if (hstart >= hend)
2725 goto skip;
2726 if (khugepaged_scan.address > hend)
2727 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002728 if (khugepaged_scan.address < hstart)
2729 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002730 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002731
2732 while (khugepaged_scan.address < hend) {
2733 int ret;
2734 cond_resched();
2735 if (unlikely(khugepaged_test_exit(mm)))
2736 goto breakouterloop;
2737
2738 VM_BUG_ON(khugepaged_scan.address < hstart ||
2739 khugepaged_scan.address + HPAGE_PMD_SIZE >
2740 hend);
2741 ret = khugepaged_scan_pmd(mm, vma,
2742 khugepaged_scan.address,
2743 hpage);
2744 /* move to next address */
2745 khugepaged_scan.address += HPAGE_PMD_SIZE;
2746 progress += HPAGE_PMD_NR;
2747 if (ret)
2748 /* we released mmap_sem so break loop */
2749 goto breakouterloop_mmap_sem;
2750 if (progress >= pages)
2751 goto breakouterloop;
2752 }
2753 }
2754breakouterloop:
2755 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2756breakouterloop_mmap_sem:
2757
2758 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002759 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002760 /*
2761 * Release the current mm_slot if this mm is about to die, or
2762 * if we scanned all vmas of this mm.
2763 */
2764 if (khugepaged_test_exit(mm) || !vma) {
2765 /*
2766 * Make sure that if mm_users is reaching zero while
2767 * khugepaged runs here, khugepaged_exit will find
2768 * mm_slot not pointing to the exiting mm.
2769 */
2770 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2771 khugepaged_scan.mm_slot = list_entry(
2772 mm_slot->mm_node.next,
2773 struct mm_slot, mm_node);
2774 khugepaged_scan.address = 0;
2775 } else {
2776 khugepaged_scan.mm_slot = NULL;
2777 khugepaged_full_scans++;
2778 }
2779
2780 collect_mm_slot(mm_slot);
2781 }
2782
2783 return progress;
2784}
2785
2786static int khugepaged_has_work(void)
2787{
2788 return !list_empty(&khugepaged_scan.mm_head) &&
2789 khugepaged_enabled();
2790}
2791
2792static int khugepaged_wait_event(void)
2793{
2794 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002795 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002796}
2797
Xiao Guangrongd5169042012-10-08 16:29:48 -07002798static void khugepaged_do_scan(void)
2799{
2800 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002801 unsigned int progress = 0, pass_through_head = 0;
2802 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002803 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002804
2805 barrier(); /* write khugepaged_pages_to_scan to local stack */
2806
2807 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002808 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002809 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002810
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002811 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002812
Jiri Kosinacd092412015-06-24 16:56:07 -07002813 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002814 break;
2815
Andrea Arcangeliba761492011-01-13 15:46:58 -08002816 spin_lock(&khugepaged_mm_lock);
2817 if (!khugepaged_scan.mm_slot)
2818 pass_through_head++;
2819 if (khugepaged_has_work() &&
2820 pass_through_head < 2)
2821 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002822 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002823 else
2824 progress = pages;
2825 spin_unlock(&khugepaged_mm_lock);
2826 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002827
Xiao Guangrongd5169042012-10-08 16:29:48 -07002828 if (!IS_ERR_OR_NULL(hpage))
2829 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002830}
2831
David Rientjesf0508972016-05-20 16:58:56 -07002832static bool khugepaged_should_wakeup(void)
2833{
2834 return kthread_should_stop() ||
2835 time_after_eq(jiffies, khugepaged_sleep_expire);
2836}
2837
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002838static void khugepaged_wait_work(void)
2839{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002840 if (khugepaged_has_work()) {
David Rientjesf0508972016-05-20 16:58:56 -07002841 const unsigned long scan_sleep_jiffies =
2842 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2843
2844 if (!scan_sleep_jiffies)
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002845 return;
2846
David Rientjesf0508972016-05-20 16:58:56 -07002847 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002848 wait_event_freezable_timeout(khugepaged_wait,
David Rientjesf0508972016-05-20 16:58:56 -07002849 khugepaged_should_wakeup(),
2850 scan_sleep_jiffies);
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002851 return;
2852 }
2853
2854 if (khugepaged_enabled())
2855 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2856}
2857
Andrea Arcangeliba761492011-01-13 15:46:58 -08002858static int khugepaged(void *none)
2859{
2860 struct mm_slot *mm_slot;
2861
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002862 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002863 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002864
Xiao Guangrongb7231782012-10-08 16:29:54 -07002865 while (!kthread_should_stop()) {
2866 khugepaged_do_scan();
2867 khugepaged_wait_work();
2868 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002869
2870 spin_lock(&khugepaged_mm_lock);
2871 mm_slot = khugepaged_scan.mm_slot;
2872 khugepaged_scan.mm_slot = NULL;
2873 if (mm_slot)
2874 collect_mm_slot(mm_slot);
2875 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002876 return 0;
2877}
2878
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002879static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2880 unsigned long haddr, pmd_t *pmd)
2881{
2882 struct mm_struct *mm = vma->vm_mm;
2883 pgtable_t pgtable;
2884 pmd_t _pmd;
2885 int i;
2886
2887 /* leave pmd empty until pte is filled */
2888 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2889
2890 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2891 pmd_populate(mm, &_pmd, pgtable);
2892
2893 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2894 pte_t *pte, entry;
2895 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2896 entry = pte_mkspecial(entry);
2897 pte = pte_offset_map(&_pmd, haddr);
2898 VM_BUG_ON(!pte_none(*pte));
2899 set_pte_at(mm, haddr, pte, entry);
2900 pte_unmap(pte);
2901 }
2902 smp_wmb(); /* make pte visible before pmd */
2903 pmd_populate(mm, pmd, pgtable);
2904 put_huge_zero_page();
2905}
2906
2907static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002908 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002909{
2910 struct mm_struct *mm = vma->vm_mm;
2911 struct page *page;
2912 pgtable_t pgtable;
2913 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002914 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002915 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002916 int i;
2917
2918 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2919 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2920 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002921 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002922
2923 count_vm_event(THP_SPLIT_PMD);
2924
2925 if (vma_is_dax(vma)) {
2926 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2927 if (is_huge_zero_pmd(_pmd))
2928 put_huge_zero_page();
2929 return;
2930 } else if (is_huge_zero_pmd(*pmd)) {
2931 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2932 }
2933
2934 page = pmd_page(*pmd);
2935 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002936 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002937 write = pmd_write(*pmd);
2938 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002939 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002940
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05302941 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002942 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2943 pmd_populate(mm, &_pmd, pgtable);
2944
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002945 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002946 pte_t entry, *pte;
2947 /*
2948 * Note that NUMA hinting access restrictions are not
2949 * transferred to avoid any possibility of altering
2950 * permissions across VMAs.
2951 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002952 if (freeze) {
2953 swp_entry_t swp_entry;
2954 swp_entry = make_migration_entry(page + i, write);
2955 entry = swp_entry_to_pte(swp_entry);
2956 } else {
2957 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002958 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002959 if (!write)
2960 entry = pte_wrprotect(entry);
2961 if (!young)
2962 entry = pte_mkold(entry);
2963 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002964 if (dirty)
2965 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002966 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002967 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002968 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002969 atomic_inc(&page[i]._mapcount);
2970 pte_unmap(pte);
2971 }
2972
2973 /*
2974 * Set PG_double_map before dropping compound_mapcount to avoid
2975 * false-negative page_mapped().
2976 */
2977 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2978 for (i = 0; i < HPAGE_PMD_NR; i++)
2979 atomic_inc(&page[i]._mapcount);
2980 }
2981
2982 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2983 /* Last compound_mapcount is gone. */
2984 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2985 if (TestClearPageDoubleMap(page)) {
2986 /* No need in mapcount reference anymore */
2987 for (i = 0; i < HPAGE_PMD_NR; i++)
2988 atomic_dec(&page[i]._mapcount);
2989 }
2990 }
2991
2992 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002993 /*
2994 * Up to this point the pmd is present and huge and userland has the
2995 * whole access to the hugepage during the split (which happens in
2996 * place). If we overwrite the pmd with the not-huge version pointing
2997 * to the pte here (which of course we could if all CPUs were bug
2998 * free), userland could trigger a small page size TLB miss on the
2999 * small sized TLB while the hugepage TLB entry is still established in
3000 * the huge TLB. Some CPU doesn't like that.
3001 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
3002 * 383 on page 93. Intel should be safe but is also warns that it's
3003 * only safe if the permission and cache attributes of the two entries
3004 * loaded in the two TLB is identical (which should be the case here).
3005 * But it is generally safer to never allow small and huge TLB entries
3006 * for the same virtual address to be loaded simultaneously. So instead
3007 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
3008 * current pmd notpresent (atomically because here the pmd_trans_huge
3009 * and pmd_trans_splitting must remain set at all times on the pmd
3010 * until the split is complete for this pmd), then we flush the SMP TLB
3011 * and finally we write the non-huge version of the pmd entry with
3012 * pmd_populate.
3013 */
3014 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003015 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003016
3017 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003018 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003019 page_remove_rmap(page + i, false);
3020 put_page(page + i);
3021 }
3022 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003023}
3024
3025void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003026 unsigned long address, bool freeze, struct page *page)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003027{
3028 spinlock_t *ptl;
3029 struct mm_struct *mm = vma->vm_mm;
3030 unsigned long haddr = address & HPAGE_PMD_MASK;
3031
3032 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
3033 ptl = pmd_lock(mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003034
3035 /*
3036 * If caller asks to setup a migration entries, we need a page to check
3037 * pmd against. Otherwise we can end up replacing wrong page.
3038 */
3039 VM_BUG_ON(freeze && !page);
3040 if (page && page != pmd_page(*pmd))
3041 goto out;
3042
Dan Williams5c7fb562016-01-15 16:56:52 -08003043 if (pmd_trans_huge(*pmd)) {
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003044 page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003045 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003046 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08003047 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003048 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003049 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003050out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003051 spin_unlock(ptl);
3052 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
3053}
3054
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003055void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3056 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003057{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003058 pgd_t *pgd;
3059 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003060 pmd_t *pmd;
3061
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003062 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003063 if (!pgd_present(*pgd))
3064 return;
3065
3066 pud = pud_offset(pgd, address);
3067 if (!pud_present(*pud))
3068 return;
3069
3070 pmd = pmd_offset(pud, address);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003071
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003072 __split_huge_pmd(vma, pmd, address, freeze, page);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003073}
3074
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003075void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003076 unsigned long start,
3077 unsigned long end,
3078 long adjust_next)
3079{
3080 /*
3081 * If the new start address isn't hpage aligned and it could
3082 * previously contain an hugepage: check if we need to split
3083 * an huge pmd.
3084 */
3085 if (start & ~HPAGE_PMD_MASK &&
3086 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3087 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003088 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003089
3090 /*
3091 * If the new end address isn't hpage aligned and it could
3092 * previously contain an hugepage: check if we need to split
3093 * an huge pmd.
3094 */
3095 if (end & ~HPAGE_PMD_MASK &&
3096 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3097 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003098 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003099
3100 /*
3101 * If we're also updating the vma->vm_next->vm_start, if the new
3102 * vm_next->vm_start isn't page aligned and it could previously
3103 * contain an hugepage: check if we need to split an huge pmd.
3104 */
3105 if (adjust_next > 0) {
3106 struct vm_area_struct *next = vma->vm_next;
3107 unsigned long nstart = next->vm_start;
3108 nstart += adjust_next << PAGE_SHIFT;
3109 if (nstart & ~HPAGE_PMD_MASK &&
3110 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3111 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003112 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003113 }
3114}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003115
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003116static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003117{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003118 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3119 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3120 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003121
3122 VM_BUG_ON_PAGE(!PageHead(page), page);
3123
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003124 /* We only need TTU_SPLIT_HUGE_PMD once */
3125 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3126 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3127 /* Cut short if the page is unmapped */
3128 if (page_count(page) == 1)
3129 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003130
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003131 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003132 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003133 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003134}
3135
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003136static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003137{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003138 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003139
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003140 for (i = 0; i < HPAGE_PMD_NR; i++)
3141 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003142}
3143
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003144static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003145 struct lruvec *lruvec, struct list_head *list)
3146{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003147 struct page *page_tail = head + tail;
3148
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003149 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003150 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003151
3152 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003153 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003154 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003155 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003156 * would then run atomic_set() concurrently with
3157 * get_page_unless_zero(), and atomic_set() is implemented in C not
3158 * using locked ops. spin_unlock on x86 sometime uses locked ops
3159 * because of PPro errata 66, 92, so unless somebody can guarantee
3160 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003161 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003162 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003163 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003164
3165 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3166 page_tail->flags |= (head->flags &
3167 ((1L << PG_referenced) |
3168 (1L << PG_swapbacked) |
3169 (1L << PG_mlocked) |
3170 (1L << PG_uptodate) |
3171 (1L << PG_active) |
3172 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003173 (1L << PG_unevictable) |
3174 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003175
3176 /*
3177 * After clearing PageTail the gup refcount can be released.
3178 * Page flags also must be visible before we make the page non-compound.
3179 */
3180 smp_wmb();
3181
3182 clear_compound_head(page_tail);
3183
3184 if (page_is_young(head))
3185 set_page_young(page_tail);
3186 if (page_is_idle(head))
3187 set_page_idle(page_tail);
3188
3189 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003190 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003191 page_tail);
3192 page_tail->mapping = head->mapping;
3193
3194 page_tail->index = head->index + tail;
3195 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3196 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003197}
3198
3199static void __split_huge_page(struct page *page, struct list_head *list)
3200{
3201 struct page *head = compound_head(page);
3202 struct zone *zone = page_zone(head);
3203 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003204 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003205
3206 /* prevent PageLRU to go away from under us, and freeze lru stats */
3207 spin_lock_irq(&zone->lru_lock);
3208 lruvec = mem_cgroup_page_lruvec(head, zone);
3209
3210 /* complete memcg works before add pages to LRU */
3211 mem_cgroup_split_huge_fixup(head);
3212
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003213 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003214 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003215
3216 ClearPageCompound(head);
3217 spin_unlock_irq(&zone->lru_lock);
3218
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003219 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003220
3221 for (i = 0; i < HPAGE_PMD_NR; i++) {
3222 struct page *subpage = head + i;
3223 if (subpage == page)
3224 continue;
3225 unlock_page(subpage);
3226
3227 /*
3228 * Subpages may be freed if there wasn't any mapping
3229 * like if add_to_swap() is running on a lru page that
3230 * had its mapping zapped. And freeing these pages
3231 * requires taking the lru_lock so we do the put_page
3232 * of the tail pages after the split is complete.
3233 */
3234 put_page(subpage);
3235 }
3236}
3237
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003238int total_mapcount(struct page *page)
3239{
3240 int i, ret;
3241
3242 VM_BUG_ON_PAGE(PageTail(page), page);
3243
3244 if (likely(!PageCompound(page)))
3245 return atomic_read(&page->_mapcount) + 1;
3246
3247 ret = compound_mapcount(page);
3248 if (PageHuge(page))
3249 return ret;
3250 for (i = 0; i < HPAGE_PMD_NR; i++)
3251 ret += atomic_read(&page[i]._mapcount) + 1;
3252 if (PageDoubleMap(page))
3253 ret -= HPAGE_PMD_NR;
3254 return ret;
3255}
3256
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003257/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003258 * This calculates accurately how many mappings a transparent hugepage
3259 * has (unlike page_mapcount() which isn't fully accurate). This full
3260 * accuracy is primarily needed to know if copy-on-write faults can
3261 * reuse the page and change the mapping to read-write instead of
3262 * copying them. At the same time this returns the total_mapcount too.
3263 *
3264 * The function returns the highest mapcount any one of the subpages
3265 * has. If the return value is one, even if different processes are
3266 * mapping different subpages of the transparent hugepage, they can
3267 * all reuse it, because each process is reusing a different subpage.
3268 *
3269 * The total_mapcount is instead counting all virtual mappings of the
3270 * subpages. If the total_mapcount is equal to "one", it tells the
3271 * caller all mappings belong to the same "mm" and in turn the
3272 * anon_vma of the transparent hugepage can become the vma->anon_vma
3273 * local one as no other process may be mapping any of the subpages.
3274 *
3275 * It would be more accurate to replace page_mapcount() with
3276 * page_trans_huge_mapcount(), however we only use
3277 * page_trans_huge_mapcount() in the copy-on-write faults where we
3278 * need full accuracy to avoid breaking page pinning, because
3279 * page_trans_huge_mapcount() is slower than page_mapcount().
3280 */
3281int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3282{
3283 int i, ret, _total_mapcount, mapcount;
3284
3285 /* hugetlbfs shouldn't call it */
3286 VM_BUG_ON_PAGE(PageHuge(page), page);
3287
3288 if (likely(!PageTransCompound(page))) {
3289 mapcount = atomic_read(&page->_mapcount) + 1;
3290 if (total_mapcount)
3291 *total_mapcount = mapcount;
3292 return mapcount;
3293 }
3294
3295 page = compound_head(page);
3296
3297 _total_mapcount = ret = 0;
3298 for (i = 0; i < HPAGE_PMD_NR; i++) {
3299 mapcount = atomic_read(&page[i]._mapcount) + 1;
3300 ret = max(ret, mapcount);
3301 _total_mapcount += mapcount;
3302 }
3303 if (PageDoubleMap(page)) {
3304 ret -= 1;
3305 _total_mapcount -= HPAGE_PMD_NR;
3306 }
3307 mapcount = compound_mapcount(page);
3308 ret += mapcount;
3309 _total_mapcount += mapcount;
3310 if (total_mapcount)
3311 *total_mapcount = _total_mapcount;
3312 return ret;
3313}
3314
3315/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003316 * This function splits huge page into normal pages. @page can point to any
3317 * subpage of huge page to split. Split doesn't change the position of @page.
3318 *
3319 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3320 * The huge page must be locked.
3321 *
3322 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3323 *
3324 * Both head page and tail pages will inherit mapping, flags, and so on from
3325 * the hugepage.
3326 *
3327 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3328 * they are not mapped.
3329 *
3330 * Returns 0 if the hugepage is split successfully.
3331 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3332 * us.
3333 */
3334int split_huge_page_to_list(struct page *page, struct list_head *list)
3335{
3336 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003337 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003338 struct anon_vma *anon_vma;
3339 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003340 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003341 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003342
3343 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3344 VM_BUG_ON_PAGE(!PageAnon(page), page);
3345 VM_BUG_ON_PAGE(!PageLocked(page), page);
3346 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3347 VM_BUG_ON_PAGE(!PageCompound(page), page);
3348
3349 /*
3350 * The caller does not necessarily hold an mmap_sem that would prevent
3351 * the anon_vma disappearing so we first we take a reference to it
3352 * and then lock the anon_vma for write. This is similar to
3353 * page_lock_anon_vma_read except the write lock is taken to serialise
3354 * against parallel split or collapse operations.
3355 */
3356 anon_vma = page_get_anon_vma(head);
3357 if (!anon_vma) {
3358 ret = -EBUSY;
3359 goto out;
3360 }
3361 anon_vma_lock_write(anon_vma);
3362
3363 /*
3364 * Racy check if we can split the page, before freeze_page() will
3365 * split PMDs
3366 */
3367 if (total_mapcount(head) != page_count(head) - 1) {
3368 ret = -EBUSY;
3369 goto out_unlock;
3370 }
3371
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003372 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003373 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003374 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3375
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003376 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3377 if (mlocked)
3378 lru_add_drain();
3379
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003380 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003381 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003382 count = page_count(head);
3383 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003384 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003385 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003386 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003387 list_del(page_deferred_list(head));
3388 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003389 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003390 __split_huge_page(page, list);
3391 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003392 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003393 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003394 pr_alert("total_mapcount: %u, page_count(): %u\n",
3395 mapcount, count);
3396 if (PageTail(page))
3397 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003398 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003399 BUG();
3400 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003401 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003402 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003403 ret = -EBUSY;
3404 }
3405
3406out_unlock:
3407 anon_vma_unlock_write(anon_vma);
3408 put_anon_vma(anon_vma);
3409out:
3410 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3411 return ret;
3412}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003413
3414void free_transhuge_page(struct page *page)
3415{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003416 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003417 unsigned long flags;
3418
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003419 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003420 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003421 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003422 list_del(page_deferred_list(page));
3423 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003424 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003425 free_compound_page(page);
3426}
3427
3428void deferred_split_huge_page(struct page *page)
3429{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003430 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003431 unsigned long flags;
3432
3433 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3434
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003435 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003436 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003437 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003438 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3439 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003440 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003441 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003442}
3443
3444static unsigned long deferred_split_count(struct shrinker *shrink,
3445 struct shrink_control *sc)
3446{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003447 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003448 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003449}
3450
3451static unsigned long deferred_split_scan(struct shrinker *shrink,
3452 struct shrink_control *sc)
3453{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003454 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003455 unsigned long flags;
3456 LIST_HEAD(list), *pos, *next;
3457 struct page *page;
3458 int split = 0;
3459
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003460 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003461 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003462 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003463 page = list_entry((void *)pos, struct page, mapping);
3464 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003465 if (get_page_unless_zero(page)) {
3466 list_move(page_deferred_list(page), &list);
3467 } else {
3468 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003469 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003470 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003471 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003472 if (!--sc->nr_to_scan)
3473 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003474 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003475 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003476
3477 list_for_each_safe(pos, next, &list) {
3478 page = list_entry((void *)pos, struct page, mapping);
3479 lock_page(page);
3480 /* split_huge_page() removes page from list on success */
3481 if (!split_huge_page(page))
3482 split++;
3483 unlock_page(page);
3484 put_page(page);
3485 }
3486
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003487 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3488 list_splice_tail(&list, &pgdata->split_queue);
3489 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003490
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003491 /*
3492 * Stop shrinker if we didn't split any page, but the queue is empty.
3493 * This can happen if pages were freed under us.
3494 */
3495 if (!split && list_empty(&pgdata->split_queue))
3496 return SHRINK_STOP;
3497 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003498}
3499
3500static struct shrinker deferred_split_shrinker = {
3501 .count_objects = deferred_split_count,
3502 .scan_objects = deferred_split_scan,
3503 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003504 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003505};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003506
3507#ifdef CONFIG_DEBUG_FS
3508static int split_huge_pages_set(void *data, u64 val)
3509{
3510 struct zone *zone;
3511 struct page *page;
3512 unsigned long pfn, max_zone_pfn;
3513 unsigned long total = 0, split = 0;
3514
3515 if (val != 1)
3516 return -EINVAL;
3517
3518 for_each_populated_zone(zone) {
3519 max_zone_pfn = zone_end_pfn(zone);
3520 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3521 if (!pfn_valid(pfn))
3522 continue;
3523
3524 page = pfn_to_page(pfn);
3525 if (!get_page_unless_zero(page))
3526 continue;
3527
3528 if (zone != page_zone(page))
3529 goto next;
3530
3531 if (!PageHead(page) || !PageAnon(page) ||
3532 PageHuge(page))
3533 goto next;
3534
3535 total++;
3536 lock_page(page);
3537 if (!split_huge_page(page))
3538 split++;
3539 unlock_page(page);
3540next:
3541 put_page(page);
3542 }
3543 }
3544
Yang Shi145bdaa2016-05-05 16:22:00 -07003545 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003546
3547 return 0;
3548}
3549DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3550 "%llu\n");
3551
3552static int __init split_huge_pages_debugfs(void)
3553{
3554 void *ret;
3555
Yang Shi145bdaa2016-05-05 16:22:00 -07003556 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003557 &split_huge_pages_fops);
3558 if (!ret)
3559 pr_warn("Failed to create split_huge_pages in debugfs");
3560 return 0;
3561}
3562late_initcall(split_huge_pages_debugfs);
3563#endif