blob: b11351579e7ae7cbb416b4da177b7ab91bd42229 [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 Perches756a0252016-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. Shutemov79553da2932015-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. Shutemov79553da2932015-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. Shutemov79553da2932015-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. Shutemov79553da2932015-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. Shutemov79553da2932015-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. Shutemov79553da2932015-04-15 16:14:56 -0700740 return 0;
741 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800742
Kirill A. Shutemov79553da2932015-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 Hocko3b3636922015-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 Hocko3b3636922015-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 Gormanba68bc012015-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 Gormanba68bc012015-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 Hocko3b3636922015-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 Hocko3b3636922015-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 Hocko3b3636922015-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
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002376/*
2377 * Bring missing pages in from swap, to complete THP collapse.
2378 * Only done if khugepaged_scan_pmd believes it is worthwhile.
2379 *
2380 * Called and returns without pte mapped or spinlocks held,
2381 * but with mmap_sem held to protect against vma changes.
2382 */
2383
2384static void __collapse_huge_page_swapin(struct mm_struct *mm,
2385 struct vm_area_struct *vma,
2386 unsigned long address, pmd_t *pmd)
2387{
2388 unsigned long _address;
2389 pte_t *pte, pteval;
2390 int swapped_in = 0, ret = 0;
2391
2392 pte = pte_offset_map(pmd, address);
2393 for (_address = address; _address < address + HPAGE_PMD_NR*PAGE_SIZE;
2394 pte++, _address += PAGE_SIZE) {
2395 pteval = *pte;
2396 if (!is_swap_pte(pteval))
2397 continue;
2398 swapped_in++;
2399 ret = do_swap_page(mm, vma, _address, pte, pmd,
2400 FAULT_FLAG_ALLOW_RETRY|FAULT_FLAG_RETRY_NOWAIT,
2401 pteval);
2402 if (ret & VM_FAULT_ERROR) {
2403 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 0);
2404 return;
2405 }
2406 /* pte is unmapped now, we need to map it */
2407 pte = pte_offset_map(pmd, _address);
2408 }
2409 pte--;
2410 pte_unmap(pte);
2411 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 1);
2412}
2413
Andrea Arcangeliba761492011-01-13 15:46:58 -08002414static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002415 unsigned long address,
2416 struct page **hpage,
2417 struct vm_area_struct *vma,
2418 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002419{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002420 pmd_t *pmd, _pmd;
2421 pte_t *pte;
2422 pgtable_t pgtable;
2423 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002424 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002425 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002426 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002427 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002428 unsigned long mmun_start; /* For mmu_notifiers */
2429 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07002430 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002431
2432 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002433
Michal Hocko3b3636922015-04-15 16:13:29 -07002434 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002435 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b3636922015-04-15 16:13:29 -07002436
Xiao Guangrong26234f32012-10-08 16:29:51 -07002437 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002438 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002439 if (!new_page) {
2440 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2441 goto out_nolock;
2442 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002443
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002444 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002445 result = SCAN_CGROUP_CHARGE_FAIL;
2446 goto out_nolock;
2447 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002448
2449 /*
2450 * Prevent all access to pagetables with the exception of
2451 * gup_fast later hanlded by the ptep_clear_flush and the VM
2452 * handled by the anon_vma lock + PG_lock.
2453 */
2454 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002455 if (unlikely(khugepaged_test_exit(mm))) {
2456 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002457 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002458 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002459
2460 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002461 if (!vma) {
2462 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002463 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002464 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2466 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002467 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2468 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002469 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002470 }
2471 if (!hugepage_vma_check(vma)) {
2472 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002473 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002474 }
Bob Liu62190492012-12-11 16:00:37 -08002475 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002476 if (!pmd) {
2477 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002478 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002479 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002480
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002481 __collapse_huge_page_swapin(mm, vma, address, pmd);
2482
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002483 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484
2485 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002486 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002487
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002488 mmun_start = address;
2489 mmun_end = address + HPAGE_PMD_SIZE;
2490 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002491 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002492 /*
2493 * After this gup_fast can't run anymore. This also removes
2494 * any huge TLB entry from the CPU so we won't allow
2495 * huge and small TLB entries for the same virtual address
2496 * to avoid the risk of CPU bugs in that area.
2497 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002498 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002499 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002500 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002501
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002502 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002503 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002504 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002505
2506 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002507 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002508 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002509 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002510 /*
2511 * We can only use set_pmd_at when establishing
2512 * hugepmds and never for establishing regular pmds that
2513 * points to regular pagetables. Use pmd_populate for that
2514 */
2515 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002516 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002517 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002518 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002519 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002520 }
2521
2522 /*
2523 * All pages are isolated and locked so anon_vma rmap
2524 * can't run anymore.
2525 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002526 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002527
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002528 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002529 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002530 __SetPageUptodate(new_page);
2531 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002532
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002533 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2534 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535
2536 /*
2537 * spin_lock() below is not the equivalent of smp_wmb(), so
2538 * this is needed to avoid the copy_huge_page writes to become
2539 * visible after the set_pmd_at() write.
2540 */
2541 smp_wmb();
2542
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002543 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002544 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002545 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002546 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002547 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002548 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002549 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002550 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002551 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002552
2553 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002554
Andrea Arcangeliba761492011-01-13 15:46:58 -08002555 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002556 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002557out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002558 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002559out_nolock:
2560 trace_mm_collapse_huge_page(mm, isolated, result);
2561 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002562out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002563 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002564 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002565}
2566
2567static int khugepaged_scan_pmd(struct mm_struct *mm,
2568 struct vm_area_struct *vma,
2569 unsigned long address,
2570 struct page **hpage)
2571{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002572 pmd_t *pmd;
2573 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002574 int ret = 0, none_or_zero = 0, result = 0;
2575 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576 unsigned long _address;
2577 spinlock_t *ptl;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002578 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002579 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002580
2581 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2582
Bob Liu62190492012-12-11 16:00:37 -08002583 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002584 if (!pmd) {
2585 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002586 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002587 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002588
Bob Liu9f1b8682013-11-12 15:07:37 -08002589 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002590 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2591 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2592 _pte++, _address += PAGE_SIZE) {
2593 pte_t pteval = *_pte;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002594 if (is_swap_pte(pteval)) {
2595 if (++unmapped <= khugepaged_max_ptes_swap) {
2596 continue;
2597 } else {
2598 result = SCAN_EXCEED_SWAP_PTE;
2599 goto out_unmap;
2600 }
2601 }
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002602 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002603 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002604 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002605 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002606 } else {
2607 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002608 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002609 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002610 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002611 if (!pte_present(pteval)) {
2612 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002613 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002614 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002615 if (pte_write(pteval))
2616 writable = true;
2617
Andrea Arcangeliba761492011-01-13 15:46:58 -08002618 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002619 if (unlikely(!page)) {
2620 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002621 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002622 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002623
2624 /* TODO: teach khugepaged to collapse THP mapped with pte */
2625 if (PageCompound(page)) {
2626 result = SCAN_PAGE_COMPOUND;
2627 goto out_unmap;
2628 }
2629
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002630 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002631 * Record which node the original page is from and save this
2632 * information to khugepaged_node_load[].
2633 * Khupaged will allocate hugepage from the node has the max
2634 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002635 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002636 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002637 if (khugepaged_scan_abort(node)) {
2638 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002639 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002640 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002641 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002642 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002643 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002644 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002645 }
2646 if (PageLocked(page)) {
2647 result = SCAN_PAGE_LOCK;
2648 goto out_unmap;
2649 }
2650 if (!PageAnon(page)) {
2651 result = SCAN_PAGE_ANON;
2652 goto out_unmap;
2653 }
2654
Ebru Akagunduz10359212015-02-11 15:28:28 -08002655 /*
2656 * cannot use mapcount: can't collapse if there's a gup pin.
2657 * The page must only be referenced by the scanned process
2658 * and page swap cache.
2659 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002660 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2661 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002662 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002663 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002664 if (pte_young(pteval) ||
2665 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002666 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002667 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002668 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002669 if (writable) {
2670 if (referenced) {
2671 result = SCAN_SUCCEED;
2672 ret = 1;
2673 } else {
2674 result = SCAN_NO_REFERENCED_PAGE;
2675 }
2676 } else {
2677 result = SCAN_PAGE_RO;
2678 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002679out_unmap:
2680 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002681 if (ret) {
2682 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002683 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002684 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002685 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002686out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002687 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002688 none_or_zero, result, unmapped);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002689 return ret;
2690}
2691
2692static void collect_mm_slot(struct mm_slot *mm_slot)
2693{
2694 struct mm_struct *mm = mm_slot->mm;
2695
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002696 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002697
2698 if (khugepaged_test_exit(mm)) {
2699 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002700 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002701 list_del(&mm_slot->mm_node);
2702
2703 /*
2704 * Not strictly needed because the mm exited already.
2705 *
2706 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2707 */
2708
2709 /* khugepaged_mm_lock actually not necessary for the below */
2710 free_mm_slot(mm_slot);
2711 mmdrop(mm);
2712 }
2713}
2714
2715static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2716 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002717 __releases(&khugepaged_mm_lock)
2718 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002719{
2720 struct mm_slot *mm_slot;
2721 struct mm_struct *mm;
2722 struct vm_area_struct *vma;
2723 int progress = 0;
2724
2725 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002726 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002727
2728 if (khugepaged_scan.mm_slot)
2729 mm_slot = khugepaged_scan.mm_slot;
2730 else {
2731 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2732 struct mm_slot, mm_node);
2733 khugepaged_scan.address = 0;
2734 khugepaged_scan.mm_slot = mm_slot;
2735 }
2736 spin_unlock(&khugepaged_mm_lock);
2737
2738 mm = mm_slot->mm;
2739 down_read(&mm->mmap_sem);
2740 if (unlikely(khugepaged_test_exit(mm)))
2741 vma = NULL;
2742 else
2743 vma = find_vma(mm, khugepaged_scan.address);
2744
2745 progress++;
2746 for (; vma; vma = vma->vm_next) {
2747 unsigned long hstart, hend;
2748
2749 cond_resched();
2750 if (unlikely(khugepaged_test_exit(mm))) {
2751 progress++;
2752 break;
2753 }
Bob Liufa475e52012-12-11 16:00:39 -08002754 if (!hugepage_vma_check(vma)) {
2755skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002756 progress++;
2757 continue;
2758 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002759 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2760 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002761 if (hstart >= hend)
2762 goto skip;
2763 if (khugepaged_scan.address > hend)
2764 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002765 if (khugepaged_scan.address < hstart)
2766 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002767 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002768
2769 while (khugepaged_scan.address < hend) {
2770 int ret;
2771 cond_resched();
2772 if (unlikely(khugepaged_test_exit(mm)))
2773 goto breakouterloop;
2774
2775 VM_BUG_ON(khugepaged_scan.address < hstart ||
2776 khugepaged_scan.address + HPAGE_PMD_SIZE >
2777 hend);
2778 ret = khugepaged_scan_pmd(mm, vma,
2779 khugepaged_scan.address,
2780 hpage);
2781 /* move to next address */
2782 khugepaged_scan.address += HPAGE_PMD_SIZE;
2783 progress += HPAGE_PMD_NR;
2784 if (ret)
2785 /* we released mmap_sem so break loop */
2786 goto breakouterloop_mmap_sem;
2787 if (progress >= pages)
2788 goto breakouterloop;
2789 }
2790 }
2791breakouterloop:
2792 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2793breakouterloop_mmap_sem:
2794
2795 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002796 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002797 /*
2798 * Release the current mm_slot if this mm is about to die, or
2799 * if we scanned all vmas of this mm.
2800 */
2801 if (khugepaged_test_exit(mm) || !vma) {
2802 /*
2803 * Make sure that if mm_users is reaching zero while
2804 * khugepaged runs here, khugepaged_exit will find
2805 * mm_slot not pointing to the exiting mm.
2806 */
2807 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2808 khugepaged_scan.mm_slot = list_entry(
2809 mm_slot->mm_node.next,
2810 struct mm_slot, mm_node);
2811 khugepaged_scan.address = 0;
2812 } else {
2813 khugepaged_scan.mm_slot = NULL;
2814 khugepaged_full_scans++;
2815 }
2816
2817 collect_mm_slot(mm_slot);
2818 }
2819
2820 return progress;
2821}
2822
2823static int khugepaged_has_work(void)
2824{
2825 return !list_empty(&khugepaged_scan.mm_head) &&
2826 khugepaged_enabled();
2827}
2828
2829static int khugepaged_wait_event(void)
2830{
2831 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002832 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002833}
2834
Xiao Guangrongd5169042012-10-08 16:29:48 -07002835static void khugepaged_do_scan(void)
2836{
2837 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002838 unsigned int progress = 0, pass_through_head = 0;
2839 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002840 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002841
2842 barrier(); /* write khugepaged_pages_to_scan to local stack */
2843
2844 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002845 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002846 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002847
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002848 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002849
Jiri Kosinacd092412015-06-24 16:56:07 -07002850 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002851 break;
2852
Andrea Arcangeliba761492011-01-13 15:46:58 -08002853 spin_lock(&khugepaged_mm_lock);
2854 if (!khugepaged_scan.mm_slot)
2855 pass_through_head++;
2856 if (khugepaged_has_work() &&
2857 pass_through_head < 2)
2858 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002859 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002860 else
2861 progress = pages;
2862 spin_unlock(&khugepaged_mm_lock);
2863 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002864
Xiao Guangrongd5169042012-10-08 16:29:48 -07002865 if (!IS_ERR_OR_NULL(hpage))
2866 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002867}
2868
David Rientjesf0508972016-05-20 16:58:56 -07002869static bool khugepaged_should_wakeup(void)
2870{
2871 return kthread_should_stop() ||
2872 time_after_eq(jiffies, khugepaged_sleep_expire);
2873}
2874
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002875static void khugepaged_wait_work(void)
2876{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002877 if (khugepaged_has_work()) {
David Rientjesf0508972016-05-20 16:58:56 -07002878 const unsigned long scan_sleep_jiffies =
2879 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2880
2881 if (!scan_sleep_jiffies)
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002882 return;
2883
David Rientjesf0508972016-05-20 16:58:56 -07002884 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002885 wait_event_freezable_timeout(khugepaged_wait,
David Rientjesf0508972016-05-20 16:58:56 -07002886 khugepaged_should_wakeup(),
2887 scan_sleep_jiffies);
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002888 return;
2889 }
2890
2891 if (khugepaged_enabled())
2892 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2893}
2894
Andrea Arcangeliba761492011-01-13 15:46:58 -08002895static int khugepaged(void *none)
2896{
2897 struct mm_slot *mm_slot;
2898
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002899 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002900 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002901
Xiao Guangrongb7231782012-10-08 16:29:54 -07002902 while (!kthread_should_stop()) {
2903 khugepaged_do_scan();
2904 khugepaged_wait_work();
2905 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002906
2907 spin_lock(&khugepaged_mm_lock);
2908 mm_slot = khugepaged_scan.mm_slot;
2909 khugepaged_scan.mm_slot = NULL;
2910 if (mm_slot)
2911 collect_mm_slot(mm_slot);
2912 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002913 return 0;
2914}
2915
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002916static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2917 unsigned long haddr, pmd_t *pmd)
2918{
2919 struct mm_struct *mm = vma->vm_mm;
2920 pgtable_t pgtable;
2921 pmd_t _pmd;
2922 int i;
2923
2924 /* leave pmd empty until pte is filled */
2925 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2926
2927 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2928 pmd_populate(mm, &_pmd, pgtable);
2929
2930 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2931 pte_t *pte, entry;
2932 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2933 entry = pte_mkspecial(entry);
2934 pte = pte_offset_map(&_pmd, haddr);
2935 VM_BUG_ON(!pte_none(*pte));
2936 set_pte_at(mm, haddr, pte, entry);
2937 pte_unmap(pte);
2938 }
2939 smp_wmb(); /* make pte visible before pmd */
2940 pmd_populate(mm, pmd, pgtable);
2941 put_huge_zero_page();
2942}
2943
2944static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002945 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002946{
2947 struct mm_struct *mm = vma->vm_mm;
2948 struct page *page;
2949 pgtable_t pgtable;
2950 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002951 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002952 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002953 int i;
2954
2955 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2956 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2957 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002958 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002959
2960 count_vm_event(THP_SPLIT_PMD);
2961
2962 if (vma_is_dax(vma)) {
2963 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2964 if (is_huge_zero_pmd(_pmd))
2965 put_huge_zero_page();
2966 return;
2967 } else if (is_huge_zero_pmd(*pmd)) {
2968 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2969 }
2970
2971 page = pmd_page(*pmd);
2972 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002973 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002974 write = pmd_write(*pmd);
2975 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002976 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002977
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05302978 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002979 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2980 pmd_populate(mm, &_pmd, pgtable);
2981
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002982 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002983 pte_t entry, *pte;
2984 /*
2985 * Note that NUMA hinting access restrictions are not
2986 * transferred to avoid any possibility of altering
2987 * permissions across VMAs.
2988 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002989 if (freeze) {
2990 swp_entry_t swp_entry;
2991 swp_entry = make_migration_entry(page + i, write);
2992 entry = swp_entry_to_pte(swp_entry);
2993 } else {
2994 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002995 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002996 if (!write)
2997 entry = pte_wrprotect(entry);
2998 if (!young)
2999 entry = pte_mkold(entry);
3000 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003001 if (dirty)
3002 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003003 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003004 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003005 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003006 atomic_inc(&page[i]._mapcount);
3007 pte_unmap(pte);
3008 }
3009
3010 /*
3011 * Set PG_double_map before dropping compound_mapcount to avoid
3012 * false-negative page_mapped().
3013 */
3014 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
3015 for (i = 0; i < HPAGE_PMD_NR; i++)
3016 atomic_inc(&page[i]._mapcount);
3017 }
3018
3019 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
3020 /* Last compound_mapcount is gone. */
3021 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
3022 if (TestClearPageDoubleMap(page)) {
3023 /* No need in mapcount reference anymore */
3024 for (i = 0; i < HPAGE_PMD_NR; i++)
3025 atomic_dec(&page[i]._mapcount);
3026 }
3027 }
3028
3029 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003030 /*
3031 * Up to this point the pmd is present and huge and userland has the
3032 * whole access to the hugepage during the split (which happens in
3033 * place). If we overwrite the pmd with the not-huge version pointing
3034 * to the pte here (which of course we could if all CPUs were bug
3035 * free), userland could trigger a small page size TLB miss on the
3036 * small sized TLB while the hugepage TLB entry is still established in
3037 * the huge TLB. Some CPU doesn't like that.
3038 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
3039 * 383 on page 93. Intel should be safe but is also warns that it's
3040 * only safe if the permission and cache attributes of the two entries
3041 * loaded in the two TLB is identical (which should be the case here).
3042 * But it is generally safer to never allow small and huge TLB entries
3043 * for the same virtual address to be loaded simultaneously. So instead
3044 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
3045 * current pmd notpresent (atomically because here the pmd_trans_huge
3046 * and pmd_trans_splitting must remain set at all times on the pmd
3047 * until the split is complete for this pmd), then we flush the SMP TLB
3048 * and finally we write the non-huge version of the pmd entry with
3049 * pmd_populate.
3050 */
3051 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003052 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003053
3054 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003055 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003056 page_remove_rmap(page + i, false);
3057 put_page(page + i);
3058 }
3059 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003060}
3061
3062void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003063 unsigned long address, bool freeze, struct page *page)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003064{
3065 spinlock_t *ptl;
3066 struct mm_struct *mm = vma->vm_mm;
3067 unsigned long haddr = address & HPAGE_PMD_MASK;
3068
3069 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
3070 ptl = pmd_lock(mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003071
3072 /*
3073 * If caller asks to setup a migration entries, we need a page to check
3074 * pmd against. Otherwise we can end up replacing wrong page.
3075 */
3076 VM_BUG_ON(freeze && !page);
3077 if (page && page != pmd_page(*pmd))
3078 goto out;
3079
Dan Williams5c7fb562016-01-15 16:56:52 -08003080 if (pmd_trans_huge(*pmd)) {
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003081 page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003082 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003083 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08003084 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003085 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003086 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003087out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003088 spin_unlock(ptl);
3089 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
3090}
3091
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003092void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3093 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003094{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003095 pgd_t *pgd;
3096 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003097 pmd_t *pmd;
3098
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003099 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003100 if (!pgd_present(*pgd))
3101 return;
3102
3103 pud = pud_offset(pgd, address);
3104 if (!pud_present(*pud))
3105 return;
3106
3107 pmd = pmd_offset(pud, address);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003108
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003109 __split_huge_pmd(vma, pmd, address, freeze, page);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003110}
3111
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003112void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003113 unsigned long start,
3114 unsigned long end,
3115 long adjust_next)
3116{
3117 /*
3118 * If the new start address isn't hpage aligned and it could
3119 * previously contain an hugepage: check if we need to split
3120 * an huge pmd.
3121 */
3122 if (start & ~HPAGE_PMD_MASK &&
3123 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3124 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003125 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003126
3127 /*
3128 * If the new end address isn't hpage aligned and it could
3129 * previously contain an hugepage: check if we need to split
3130 * an huge pmd.
3131 */
3132 if (end & ~HPAGE_PMD_MASK &&
3133 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3134 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003135 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003136
3137 /*
3138 * If we're also updating the vma->vm_next->vm_start, if the new
3139 * vm_next->vm_start isn't page aligned and it could previously
3140 * contain an hugepage: check if we need to split an huge pmd.
3141 */
3142 if (adjust_next > 0) {
3143 struct vm_area_struct *next = vma->vm_next;
3144 unsigned long nstart = next->vm_start;
3145 nstart += adjust_next << PAGE_SHIFT;
3146 if (nstart & ~HPAGE_PMD_MASK &&
3147 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3148 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003149 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003150 }
3151}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003152
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003153static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003154{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003155 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3156 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3157 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003158
3159 VM_BUG_ON_PAGE(!PageHead(page), page);
3160
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003161 /* We only need TTU_SPLIT_HUGE_PMD once */
3162 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3163 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3164 /* Cut short if the page is unmapped */
3165 if (page_count(page) == 1)
3166 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003167
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003168 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003169 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003170 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003171}
3172
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003173static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003174{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003175 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003176
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003177 for (i = 0; i < HPAGE_PMD_NR; i++)
3178 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003179}
3180
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003181static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003182 struct lruvec *lruvec, struct list_head *list)
3183{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003184 struct page *page_tail = head + tail;
3185
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003186 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003187 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003188
3189 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003190 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003191 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003192 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003193 * would then run atomic_set() concurrently with
3194 * get_page_unless_zero(), and atomic_set() is implemented in C not
3195 * using locked ops. spin_unlock on x86 sometime uses locked ops
3196 * because of PPro errata 66, 92, so unless somebody can guarantee
3197 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003198 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003199 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003200 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003201
3202 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3203 page_tail->flags |= (head->flags &
3204 ((1L << PG_referenced) |
3205 (1L << PG_swapbacked) |
3206 (1L << PG_mlocked) |
3207 (1L << PG_uptodate) |
3208 (1L << PG_active) |
3209 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003210 (1L << PG_unevictable) |
3211 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003212
3213 /*
3214 * After clearing PageTail the gup refcount can be released.
3215 * Page flags also must be visible before we make the page non-compound.
3216 */
3217 smp_wmb();
3218
3219 clear_compound_head(page_tail);
3220
3221 if (page_is_young(head))
3222 set_page_young(page_tail);
3223 if (page_is_idle(head))
3224 set_page_idle(page_tail);
3225
3226 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003227 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003228 page_tail);
3229 page_tail->mapping = head->mapping;
3230
3231 page_tail->index = head->index + tail;
3232 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3233 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003234}
3235
3236static void __split_huge_page(struct page *page, struct list_head *list)
3237{
3238 struct page *head = compound_head(page);
3239 struct zone *zone = page_zone(head);
3240 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003241 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003242
3243 /* prevent PageLRU to go away from under us, and freeze lru stats */
3244 spin_lock_irq(&zone->lru_lock);
3245 lruvec = mem_cgroup_page_lruvec(head, zone);
3246
3247 /* complete memcg works before add pages to LRU */
3248 mem_cgroup_split_huge_fixup(head);
3249
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003250 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003251 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003252
3253 ClearPageCompound(head);
3254 spin_unlock_irq(&zone->lru_lock);
3255
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003256 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003257
3258 for (i = 0; i < HPAGE_PMD_NR; i++) {
3259 struct page *subpage = head + i;
3260 if (subpage == page)
3261 continue;
3262 unlock_page(subpage);
3263
3264 /*
3265 * Subpages may be freed if there wasn't any mapping
3266 * like if add_to_swap() is running on a lru page that
3267 * had its mapping zapped. And freeing these pages
3268 * requires taking the lru_lock so we do the put_page
3269 * of the tail pages after the split is complete.
3270 */
3271 put_page(subpage);
3272 }
3273}
3274
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003275int total_mapcount(struct page *page)
3276{
3277 int i, ret;
3278
3279 VM_BUG_ON_PAGE(PageTail(page), page);
3280
3281 if (likely(!PageCompound(page)))
3282 return atomic_read(&page->_mapcount) + 1;
3283
3284 ret = compound_mapcount(page);
3285 if (PageHuge(page))
3286 return ret;
3287 for (i = 0; i < HPAGE_PMD_NR; i++)
3288 ret += atomic_read(&page[i]._mapcount) + 1;
3289 if (PageDoubleMap(page))
3290 ret -= HPAGE_PMD_NR;
3291 return ret;
3292}
3293
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003294/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003295 * This calculates accurately how many mappings a transparent hugepage
3296 * has (unlike page_mapcount() which isn't fully accurate). This full
3297 * accuracy is primarily needed to know if copy-on-write faults can
3298 * reuse the page and change the mapping to read-write instead of
3299 * copying them. At the same time this returns the total_mapcount too.
3300 *
3301 * The function returns the highest mapcount any one of the subpages
3302 * has. If the return value is one, even if different processes are
3303 * mapping different subpages of the transparent hugepage, they can
3304 * all reuse it, because each process is reusing a different subpage.
3305 *
3306 * The total_mapcount is instead counting all virtual mappings of the
3307 * subpages. If the total_mapcount is equal to "one", it tells the
3308 * caller all mappings belong to the same "mm" and in turn the
3309 * anon_vma of the transparent hugepage can become the vma->anon_vma
3310 * local one as no other process may be mapping any of the subpages.
3311 *
3312 * It would be more accurate to replace page_mapcount() with
3313 * page_trans_huge_mapcount(), however we only use
3314 * page_trans_huge_mapcount() in the copy-on-write faults where we
3315 * need full accuracy to avoid breaking page pinning, because
3316 * page_trans_huge_mapcount() is slower than page_mapcount().
3317 */
3318int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3319{
3320 int i, ret, _total_mapcount, mapcount;
3321
3322 /* hugetlbfs shouldn't call it */
3323 VM_BUG_ON_PAGE(PageHuge(page), page);
3324
3325 if (likely(!PageTransCompound(page))) {
3326 mapcount = atomic_read(&page->_mapcount) + 1;
3327 if (total_mapcount)
3328 *total_mapcount = mapcount;
3329 return mapcount;
3330 }
3331
3332 page = compound_head(page);
3333
3334 _total_mapcount = ret = 0;
3335 for (i = 0; i < HPAGE_PMD_NR; i++) {
3336 mapcount = atomic_read(&page[i]._mapcount) + 1;
3337 ret = max(ret, mapcount);
3338 _total_mapcount += mapcount;
3339 }
3340 if (PageDoubleMap(page)) {
3341 ret -= 1;
3342 _total_mapcount -= HPAGE_PMD_NR;
3343 }
3344 mapcount = compound_mapcount(page);
3345 ret += mapcount;
3346 _total_mapcount += mapcount;
3347 if (total_mapcount)
3348 *total_mapcount = _total_mapcount;
3349 return ret;
3350}
3351
3352/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003353 * This function splits huge page into normal pages. @page can point to any
3354 * subpage of huge page to split. Split doesn't change the position of @page.
3355 *
3356 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3357 * The huge page must be locked.
3358 *
3359 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3360 *
3361 * Both head page and tail pages will inherit mapping, flags, and so on from
3362 * the hugepage.
3363 *
3364 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3365 * they are not mapped.
3366 *
3367 * Returns 0 if the hugepage is split successfully.
3368 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3369 * us.
3370 */
3371int split_huge_page_to_list(struct page *page, struct list_head *list)
3372{
3373 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003374 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003375 struct anon_vma *anon_vma;
3376 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003377 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003378 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003379
3380 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3381 VM_BUG_ON_PAGE(!PageAnon(page), page);
3382 VM_BUG_ON_PAGE(!PageLocked(page), page);
3383 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3384 VM_BUG_ON_PAGE(!PageCompound(page), page);
3385
3386 /*
3387 * The caller does not necessarily hold an mmap_sem that would prevent
3388 * the anon_vma disappearing so we first we take a reference to it
3389 * and then lock the anon_vma for write. This is similar to
3390 * page_lock_anon_vma_read except the write lock is taken to serialise
3391 * against parallel split or collapse operations.
3392 */
3393 anon_vma = page_get_anon_vma(head);
3394 if (!anon_vma) {
3395 ret = -EBUSY;
3396 goto out;
3397 }
3398 anon_vma_lock_write(anon_vma);
3399
3400 /*
3401 * Racy check if we can split the page, before freeze_page() will
3402 * split PMDs
3403 */
3404 if (total_mapcount(head) != page_count(head) - 1) {
3405 ret = -EBUSY;
3406 goto out_unlock;
3407 }
3408
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003409 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003410 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003411 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3412
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003413 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3414 if (mlocked)
3415 lru_add_drain();
3416
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003417 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003418 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003419 count = page_count(head);
3420 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003421 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003422 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003423 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003424 list_del(page_deferred_list(head));
3425 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003426 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003427 __split_huge_page(page, list);
3428 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003429 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003430 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003431 pr_alert("total_mapcount: %u, page_count(): %u\n",
3432 mapcount, count);
3433 if (PageTail(page))
3434 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003435 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003436 BUG();
3437 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003438 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003439 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003440 ret = -EBUSY;
3441 }
3442
3443out_unlock:
3444 anon_vma_unlock_write(anon_vma);
3445 put_anon_vma(anon_vma);
3446out:
3447 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3448 return ret;
3449}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003450
3451void free_transhuge_page(struct page *page)
3452{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003453 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003454 unsigned long flags;
3455
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003456 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003457 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003458 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003459 list_del(page_deferred_list(page));
3460 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003461 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003462 free_compound_page(page);
3463}
3464
3465void deferred_split_huge_page(struct page *page)
3466{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003467 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003468 unsigned long flags;
3469
3470 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3471
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003472 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003473 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003474 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003475 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3476 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003477 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003478 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003479}
3480
3481static unsigned long deferred_split_count(struct shrinker *shrink,
3482 struct shrink_control *sc)
3483{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003484 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003485 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003486}
3487
3488static unsigned long deferred_split_scan(struct shrinker *shrink,
3489 struct shrink_control *sc)
3490{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003491 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003492 unsigned long flags;
3493 LIST_HEAD(list), *pos, *next;
3494 struct page *page;
3495 int split = 0;
3496
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003497 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003498 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003499 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003500 page = list_entry((void *)pos, struct page, mapping);
3501 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003502 if (get_page_unless_zero(page)) {
3503 list_move(page_deferred_list(page), &list);
3504 } else {
3505 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003506 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003507 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003508 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003509 if (!--sc->nr_to_scan)
3510 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003511 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003512 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003513
3514 list_for_each_safe(pos, next, &list) {
3515 page = list_entry((void *)pos, struct page, mapping);
3516 lock_page(page);
3517 /* split_huge_page() removes page from list on success */
3518 if (!split_huge_page(page))
3519 split++;
3520 unlock_page(page);
3521 put_page(page);
3522 }
3523
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003524 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3525 list_splice_tail(&list, &pgdata->split_queue);
3526 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003527
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003528 /*
3529 * Stop shrinker if we didn't split any page, but the queue is empty.
3530 * This can happen if pages were freed under us.
3531 */
3532 if (!split && list_empty(&pgdata->split_queue))
3533 return SHRINK_STOP;
3534 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003535}
3536
3537static struct shrinker deferred_split_shrinker = {
3538 .count_objects = deferred_split_count,
3539 .scan_objects = deferred_split_scan,
3540 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003541 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003542};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003543
3544#ifdef CONFIG_DEBUG_FS
3545static int split_huge_pages_set(void *data, u64 val)
3546{
3547 struct zone *zone;
3548 struct page *page;
3549 unsigned long pfn, max_zone_pfn;
3550 unsigned long total = 0, split = 0;
3551
3552 if (val != 1)
3553 return -EINVAL;
3554
3555 for_each_populated_zone(zone) {
3556 max_zone_pfn = zone_end_pfn(zone);
3557 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3558 if (!pfn_valid(pfn))
3559 continue;
3560
3561 page = pfn_to_page(pfn);
3562 if (!get_page_unless_zero(page))
3563 continue;
3564
3565 if (zone != page_zone(page))
3566 goto next;
3567
3568 if (!PageHead(page) || !PageAnon(page) ||
3569 PageHuge(page))
3570 goto next;
3571
3572 total++;
3573 lock_page(page);
3574 if (!split_huge_page(page))
3575 split++;
3576 unlock_page(page);
3577next:
3578 put_page(page);
3579 }
3580 }
3581
Yang Shi145bdaa2016-05-05 16:22:00 -07003582 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003583
3584 return 0;
3585}
3586DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3587 "%llu\n");
3588
3589static int __init split_huge_pages_debugfs(void)
3590{
3591 void *ret;
3592
Yang Shi145bdaa2016-05-05 16:22:00 -07003593 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003594 &split_huge_pages_fops);
3595 if (!ret)
3596 pr_warn("Failed to create split_huge_pages in debugfs");
3597 return 0;
3598}
3599late_initcall(split_huge_pages_debugfs);
3600#endif