blob: 021db178187278ba4319adf03bfbb57467890d9b [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,
60 SCAN_CGROUP_CHARGE_FAIL
61};
62
63#define CREATE_TRACE_POINTS
64#include <trace/events/huge_memory.h>
65
Andrea Arcangeliba761492011-01-13 15:46:58 -080066/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080067 * By default transparent hugepage support is disabled in order that avoid
68 * to risk increase the memory footprint of applications without a guaranteed
69 * benefit. When transparent hugepage support is enabled, is for all mappings,
70 * and khugepaged scans all mappings.
71 * Defrag is invoked by khugepaged hugepage allocations and by page faults
72 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080073 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080074unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080075#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080076 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080077#endif
78#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
79 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
80#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070081 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080082 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
83 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080084
85/* default scan 8*512 pte (or vmas) every 30 second */
86static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
87static unsigned int khugepaged_pages_collapsed;
88static unsigned int khugepaged_full_scans;
89static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
90/* during fragmentation poll the hugepage allocator once every minute */
91static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
92static struct task_struct *khugepaged_thread __read_mostly;
93static DEFINE_MUTEX(khugepaged_mutex);
94static DEFINE_SPINLOCK(khugepaged_mm_lock);
95static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
96/*
97 * default collapse hugepages if there is at least one pte mapped like
98 * it would have happened if the vma was large enough during page
99 * fault.
100 */
101static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
102
103static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800104static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700105static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800106
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800107#define MM_SLOTS_HASH_BITS 10
108static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
109
Andrea Arcangeliba761492011-01-13 15:46:58 -0800110static struct kmem_cache *mm_slot_cache __read_mostly;
111
112/**
113 * struct mm_slot - hash lookup from mm to mm_slot
114 * @hash: hash collision list
115 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
116 * @mm: the mm that this information is valid for
117 */
118struct mm_slot {
119 struct hlist_node hash;
120 struct list_head mm_node;
121 struct mm_struct *mm;
122};
123
124/**
125 * struct khugepaged_scan - cursor for scanning
126 * @mm_head: the head of the mm list to scan
127 * @mm_slot: the current mm_slot we are scanning
128 * @address: the next address inside that to be scanned
129 *
130 * There is only the one khugepaged_scan instance of this cursor structure.
131 */
132struct khugepaged_scan {
133 struct list_head mm_head;
134 struct mm_slot *mm_slot;
135 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700136};
137static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800138 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
139};
140
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800141static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800142
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700143static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800144{
145 struct zone *zone;
146 int nr_zones = 0;
147 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800148
Andrea Arcangelif0005652011-01-13 15:47:04 -0800149 for_each_populated_zone(zone)
150 nr_zones++;
151
Mel Gorman974a7862015-11-06 16:28:34 -0800152 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800153 recommended_min = pageblock_nr_pages * nr_zones * 2;
154
155 /*
156 * Make sure that on average at least two pageblocks are almost free
157 * of another type, one for a migratetype to fall back to and a
158 * second to avoid subsequent fallbacks of other types There are 3
159 * MIGRATE_TYPES we care about.
160 */
161 recommended_min += pageblock_nr_pages * nr_zones *
162 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
163
164 /* don't ever allow to reserve more than 5% of the lowmem */
165 recommended_min = min(recommended_min,
166 (unsigned long) nr_free_buffer_pages() / 20);
167 recommended_min <<= (PAGE_SHIFT-10);
168
Han Pingtian42aa83c2014-01-23 15:53:28 -0800169 if (recommended_min > min_free_kbytes) {
170 if (user_min_free_kbytes >= 0)
Joe Perches756a0252016-03-17 14:19:47 -0700171 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
Han Pingtian42aa83c2014-01-23 15:53:28 -0800172 min_free_kbytes, recommended_min);
173
Andrea Arcangelif0005652011-01-13 15:47:04 -0800174 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800175 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800176 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800177}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700179static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800180{
181 int err = 0;
182 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800183 if (!khugepaged_thread)
184 khugepaged_thread = kthread_run(khugepaged, NULL,
185 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530186 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700187 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800188 err = PTR_ERR(khugepaged_thread);
189 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700190 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800191 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700192
193 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800194 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800195
196 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700197 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700198 kthread_stop(khugepaged_thread);
199 khugepaged_thread = NULL;
200 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700201fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800202 return err;
203}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800204
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800205static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800206struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800207
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700208struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800209{
210 struct page *zero_page;
211retry:
212 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700213 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800214
215 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
216 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800217 if (!zero_page) {
218 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700219 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800220 }
221 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800222 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700223 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800224 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700225 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800226 goto retry;
227 }
228
229 /* We take additional reference here. It will be put back by shrinker */
230 atomic_set(&huge_zero_refcount, 2);
231 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700232 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800233}
234
235static void put_huge_zero_page(void)
236{
237 /*
238 * Counter should never go to zero here. Only shrinker can put
239 * last reference.
240 */
241 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
242}
243
Glauber Costa48896462013-08-28 10:18:15 +1000244static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
245 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800246{
Glauber Costa48896462013-08-28 10:18:15 +1000247 /* we can free zero page only if last reference remains */
248 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
249}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800250
Glauber Costa48896462013-08-28 10:18:15 +1000251static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
252 struct shrink_control *sc)
253{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800254 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700255 struct page *zero_page = xchg(&huge_zero_page, NULL);
256 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700257 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000258 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800259 }
260
261 return 0;
262}
263
264static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000265 .count_objects = shrink_huge_zero_page_count,
266 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800267 .seeks = DEFAULT_SEEKS,
268};
269
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800270#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800271
Mel Gorman444eb2a42016-03-17 14:19:23 -0700272static ssize_t triple_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800273 struct kobj_attribute *attr,
274 const char *buf, size_t count,
275 enum transparent_hugepage_flag enabled,
Mel Gorman444eb2a42016-03-17 14:19:23 -0700276 enum transparent_hugepage_flag deferred,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800277 enum transparent_hugepage_flag req_madv)
278{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700279 if (!memcmp("defer", buf,
280 min(sizeof("defer")-1, count))) {
281 if (enabled == deferred)
282 return -EINVAL;
283 clear_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800284 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700285 set_bit(deferred, &transparent_hugepage_flags);
286 } else if (!memcmp("always", buf,
287 min(sizeof("always")-1, count))) {
288 clear_bit(deferred, &transparent_hugepage_flags);
289 clear_bit(req_madv, &transparent_hugepage_flags);
290 set_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800291 } else if (!memcmp("madvise", buf,
292 min(sizeof("madvise")-1, count))) {
293 clear_bit(enabled, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700294 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800295 set_bit(req_madv, &transparent_hugepage_flags);
296 } else if (!memcmp("never", buf,
297 min(sizeof("never")-1, count))) {
298 clear_bit(enabled, &transparent_hugepage_flags);
299 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700300 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800301 } else
302 return -EINVAL;
303
304 return count;
305}
306
307static ssize_t enabled_show(struct kobject *kobj,
308 struct kobj_attribute *attr, char *buf)
309{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700310 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
311 return sprintf(buf, "[always] madvise never\n");
312 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
313 return sprintf(buf, "always [madvise] never\n");
314 else
315 return sprintf(buf, "always madvise [never]\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800316}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700317
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800318static ssize_t enabled_store(struct kobject *kobj,
319 struct kobj_attribute *attr,
320 const char *buf, size_t count)
321{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800322 ssize_t ret;
323
Mel Gorman444eb2a42016-03-17 14:19:23 -0700324 ret = triple_flag_store(kobj, attr, buf, count,
325 TRANSPARENT_HUGEPAGE_FLAG,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800326 TRANSPARENT_HUGEPAGE_FLAG,
327 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
328
329 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700330 int err;
331
332 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700333 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700334 mutex_unlock(&khugepaged_mutex);
335
Andrea Arcangeliba761492011-01-13 15:46:58 -0800336 if (err)
337 ret = err;
338 }
339
340 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800341}
342static struct kobj_attribute enabled_attr =
343 __ATTR(enabled, 0644, enabled_show, enabled_store);
344
345static ssize_t single_flag_show(struct kobject *kobj,
346 struct kobj_attribute *attr, char *buf,
347 enum transparent_hugepage_flag flag)
348{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700349 return sprintf(buf, "%d\n",
350 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800351}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700352
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800353static ssize_t single_flag_store(struct kobject *kobj,
354 struct kobj_attribute *attr,
355 const char *buf, size_t count,
356 enum transparent_hugepage_flag flag)
357{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700358 unsigned long value;
359 int ret;
360
361 ret = kstrtoul(buf, 10, &value);
362 if (ret < 0)
363 return ret;
364 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800365 return -EINVAL;
366
Ben Hutchingse27e6152011-04-14 15:22:21 -0700367 if (value)
368 set_bit(flag, &transparent_hugepage_flags);
369 else
370 clear_bit(flag, &transparent_hugepage_flags);
371
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800372 return count;
373}
374
375/*
376 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
377 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
378 * memory just to allocate one more hugepage.
379 */
380static ssize_t defrag_show(struct kobject *kobj,
381 struct kobj_attribute *attr, char *buf)
382{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700383 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
384 return sprintf(buf, "[always] defer madvise never\n");
385 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
386 return sprintf(buf, "always [defer] madvise never\n");
387 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
388 return sprintf(buf, "always defer [madvise] never\n");
389 else
390 return sprintf(buf, "always defer madvise [never]\n");
391
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800392}
393static ssize_t defrag_store(struct kobject *kobj,
394 struct kobj_attribute *attr,
395 const char *buf, size_t count)
396{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700397 return triple_flag_store(kobj, attr, buf, count,
398 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
399 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800400 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
401}
402static struct kobj_attribute defrag_attr =
403 __ATTR(defrag, 0644, defrag_show, defrag_store);
404
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800405static ssize_t use_zero_page_show(struct kobject *kobj,
406 struct kobj_attribute *attr, char *buf)
407{
408 return single_flag_show(kobj, attr, buf,
409 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
410}
411static ssize_t use_zero_page_store(struct kobject *kobj,
412 struct kobj_attribute *attr, const char *buf, size_t count)
413{
414 return single_flag_store(kobj, attr, buf, count,
415 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
416}
417static struct kobj_attribute use_zero_page_attr =
418 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800419#ifdef CONFIG_DEBUG_VM
420static ssize_t debug_cow_show(struct kobject *kobj,
421 struct kobj_attribute *attr, char *buf)
422{
423 return single_flag_show(kobj, attr, buf,
424 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
425}
426static ssize_t debug_cow_store(struct kobject *kobj,
427 struct kobj_attribute *attr,
428 const char *buf, size_t count)
429{
430 return single_flag_store(kobj, attr, buf, count,
431 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
432}
433static struct kobj_attribute debug_cow_attr =
434 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
435#endif /* CONFIG_DEBUG_VM */
436
437static struct attribute *hugepage_attr[] = {
438 &enabled_attr.attr,
439 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800440 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800441#ifdef CONFIG_DEBUG_VM
442 &debug_cow_attr.attr,
443#endif
444 NULL,
445};
446
447static struct attribute_group hugepage_attr_group = {
448 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800449};
450
451static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
452 struct kobj_attribute *attr,
453 char *buf)
454{
455 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
456}
457
458static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
459 struct kobj_attribute *attr,
460 const char *buf, size_t count)
461{
462 unsigned long msecs;
463 int err;
464
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700465 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800466 if (err || msecs > UINT_MAX)
467 return -EINVAL;
468
469 khugepaged_scan_sleep_millisecs = msecs;
470 wake_up_interruptible(&khugepaged_wait);
471
472 return count;
473}
474static struct kobj_attribute scan_sleep_millisecs_attr =
475 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
476 scan_sleep_millisecs_store);
477
478static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
479 struct kobj_attribute *attr,
480 char *buf)
481{
482 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
483}
484
485static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
486 struct kobj_attribute *attr,
487 const char *buf, size_t count)
488{
489 unsigned long msecs;
490 int err;
491
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700492 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800493 if (err || msecs > UINT_MAX)
494 return -EINVAL;
495
496 khugepaged_alloc_sleep_millisecs = msecs;
497 wake_up_interruptible(&khugepaged_wait);
498
499 return count;
500}
501static struct kobj_attribute alloc_sleep_millisecs_attr =
502 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
503 alloc_sleep_millisecs_store);
504
505static ssize_t pages_to_scan_show(struct kobject *kobj,
506 struct kobj_attribute *attr,
507 char *buf)
508{
509 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
510}
511static ssize_t pages_to_scan_store(struct kobject *kobj,
512 struct kobj_attribute *attr,
513 const char *buf, size_t count)
514{
515 int err;
516 unsigned long pages;
517
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700518 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800519 if (err || !pages || pages > UINT_MAX)
520 return -EINVAL;
521
522 khugepaged_pages_to_scan = pages;
523
524 return count;
525}
526static struct kobj_attribute pages_to_scan_attr =
527 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
528 pages_to_scan_store);
529
530static ssize_t pages_collapsed_show(struct kobject *kobj,
531 struct kobj_attribute *attr,
532 char *buf)
533{
534 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
535}
536static struct kobj_attribute pages_collapsed_attr =
537 __ATTR_RO(pages_collapsed);
538
539static ssize_t full_scans_show(struct kobject *kobj,
540 struct kobj_attribute *attr,
541 char *buf)
542{
543 return sprintf(buf, "%u\n", khugepaged_full_scans);
544}
545static struct kobj_attribute full_scans_attr =
546 __ATTR_RO(full_scans);
547
548static ssize_t khugepaged_defrag_show(struct kobject *kobj,
549 struct kobj_attribute *attr, char *buf)
550{
551 return single_flag_show(kobj, attr, buf,
552 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
553}
554static ssize_t khugepaged_defrag_store(struct kobject *kobj,
555 struct kobj_attribute *attr,
556 const char *buf, size_t count)
557{
558 return single_flag_store(kobj, attr, buf, count,
559 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
560}
561static struct kobj_attribute khugepaged_defrag_attr =
562 __ATTR(defrag, 0644, khugepaged_defrag_show,
563 khugepaged_defrag_store);
564
565/*
566 * max_ptes_none controls if khugepaged should collapse hugepages over
567 * any unmapped ptes in turn potentially increasing the memory
568 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
569 * reduce the available free memory in the system as it
570 * runs. Increasing max_ptes_none will instead potentially reduce the
571 * free memory in the system during the khugepaged scan.
572 */
573static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
574 struct kobj_attribute *attr,
575 char *buf)
576{
577 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
578}
579static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
580 struct kobj_attribute *attr,
581 const char *buf, size_t count)
582{
583 int err;
584 unsigned long max_ptes_none;
585
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700586 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800587 if (err || max_ptes_none > HPAGE_PMD_NR-1)
588 return -EINVAL;
589
590 khugepaged_max_ptes_none = max_ptes_none;
591
592 return count;
593}
594static struct kobj_attribute khugepaged_max_ptes_none_attr =
595 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
596 khugepaged_max_ptes_none_store);
597
598static struct attribute *khugepaged_attr[] = {
599 &khugepaged_defrag_attr.attr,
600 &khugepaged_max_ptes_none_attr.attr,
601 &pages_to_scan_attr.attr,
602 &pages_collapsed_attr.attr,
603 &full_scans_attr.attr,
604 &scan_sleep_millisecs_attr.attr,
605 &alloc_sleep_millisecs_attr.attr,
606 NULL,
607};
608
609static struct attribute_group khugepaged_attr_group = {
610 .attrs = khugepaged_attr,
611 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800612};
Shaohua Li569e5592012-01-12 17:19:11 -0800613
614static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
615{
616 int err;
617
618 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
619 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700620 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800621 return -ENOMEM;
622 }
623
624 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
625 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700626 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800627 goto delete_obj;
628 }
629
630 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
631 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700632 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800633 goto remove_hp_group;
634 }
635
636 return 0;
637
638remove_hp_group:
639 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
640delete_obj:
641 kobject_put(*hugepage_kobj);
642 return err;
643}
644
645static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
646{
647 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
648 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
649 kobject_put(hugepage_kobj);
650}
651#else
652static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
653{
654 return 0;
655}
656
657static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
658{
659}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800660#endif /* CONFIG_SYSFS */
661
662static int __init hugepage_init(void)
663{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800664 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800665 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800666
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800667 if (!has_transparent_hugepage()) {
668 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800669 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800670 }
671
Shaohua Li569e5592012-01-12 17:19:11 -0800672 err = hugepage_init_sysfs(&hugepage_kobj);
673 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700674 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800675
676 err = khugepaged_slab_init();
677 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700678 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800679
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700680 err = register_shrinker(&huge_zero_page_shrinker);
681 if (err)
682 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800683 err = register_shrinker(&deferred_split_shrinker);
684 if (err)
685 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800686
Rik van Riel97562cd2011-01-13 15:47:12 -0800687 /*
688 * By default disable transparent hugepages on smaller systems,
689 * where the extra memory used could hurt more than TLB overhead
690 * is likely to save. The admin can still enable it through /sys.
691 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700692 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800693 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700694 return 0;
695 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800696
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700697 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700698 if (err)
699 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800700
Shaohua Li569e5592012-01-12 17:19:11 -0800701 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700702err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800703 unregister_shrinker(&deferred_split_shrinker);
704err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700705 unregister_shrinker(&huge_zero_page_shrinker);
706err_hzp_shrinker:
707 khugepaged_slab_exit();
708err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800709 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700710err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800711 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800712}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800713subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800714
715static int __init setup_transparent_hugepage(char *str)
716{
717 int ret = 0;
718 if (!str)
719 goto out;
720 if (!strcmp(str, "always")) {
721 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
722 &transparent_hugepage_flags);
723 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
724 &transparent_hugepage_flags);
725 ret = 1;
726 } else if (!strcmp(str, "madvise")) {
727 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
728 &transparent_hugepage_flags);
729 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
730 &transparent_hugepage_flags);
731 ret = 1;
732 } else if (!strcmp(str, "never")) {
733 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
734 &transparent_hugepage_flags);
735 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
736 &transparent_hugepage_flags);
737 ret = 1;
738 }
739out:
740 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700741 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800742 return ret;
743}
744__setup("transparent_hugepage=", setup_transparent_hugepage);
745
Mel Gormanb32967f2012-11-19 12:35:47 +0000746pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800747{
748 if (likely(vma->vm_flags & VM_WRITE))
749 pmd = pmd_mkwrite(pmd);
750 return pmd;
751}
752
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700753static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800754{
755 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700756 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800757 entry = pmd_mkhuge(entry);
758 return entry;
759}
760
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800761static inline struct list_head *page_deferred_list(struct page *page)
762{
763 /*
764 * ->lru in the tail pages is occupied by compound_head.
765 * Let's use ->mapping + ->index in the second tail page as list_head.
766 */
767 return (struct list_head *)&page[2].mapping;
768}
769
770void prep_transhuge_page(struct page *page)
771{
772 /*
773 * we use page->mapping and page->indexlru in second tail page
774 * as list_head: assuming THP order >= 2
775 */
776 BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
777
778 INIT_LIST_HEAD(page_deferred_list(page));
779 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
780}
781
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800782static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
783 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700784 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700785 struct page *page, gfp_t gfp,
786 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800787{
Johannes Weiner00501b52014-08-08 14:19:20 -0700788 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800789 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800790 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700791 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800792
Sasha Levin309381fea2014-01-23 15:52:54 -0800793 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700794
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800795 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700796 put_page(page);
797 count_vm_event(THP_FAULT_FALLBACK);
798 return VM_FAULT_FALLBACK;
799 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800800
Johannes Weiner00501b52014-08-08 14:19:20 -0700801 pgtable = pte_alloc_one(mm, haddr);
802 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800803 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700804 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700805 return VM_FAULT_OOM;
806 }
807
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800808 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700809 /*
810 * The memory barrier inside __SetPageUptodate makes sure that
811 * clear_huge_page writes become visible before the set_pmd_at()
812 * write.
813 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800814 __SetPageUptodate(page);
815
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800816 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800817 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800818 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800819 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800820 put_page(page);
821 pte_free(mm, pgtable);
822 } else {
823 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700824
825 /* Deliver the page fault to userland */
826 if (userfaultfd_missing(vma)) {
827 int ret;
828
829 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800830 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700831 put_page(page);
832 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700833 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700834 VM_UFFD_MISSING);
835 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
836 return ret;
837 }
838
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700839 entry = mk_huge_pmd(page, vma->vm_page_prot);
840 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800841 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800842 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700843 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700844 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800845 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800846 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800847 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800848 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700849 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800850 }
851
David Rientjesaa2e8782012-05-29 15:06:17 -0700852 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800853}
854
Mel Gorman444eb2a42016-03-17 14:19:23 -0700855/*
856 * If THP is set to always then directly reclaim/compact as necessary
857 * If set to defer then do no reclaim and defer to khugepaged
858 * If set to madvise and the VMA is flagged then directly reclaim/compact
859 */
860static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800861{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700862 gfp_t reclaim_flags = 0;
863
864 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
865 (vma->vm_flags & VM_HUGEPAGE))
866 reclaim_flags = __GFP_DIRECT_RECLAIM;
867 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
868 reclaim_flags = __GFP_KSWAPD_RECLAIM;
869 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
870 reclaim_flags = __GFP_DIRECT_RECLAIM;
871
872 return GFP_TRANSHUGE | reclaim_flags;
873}
874
875/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
876static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
877{
878 return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800879}
880
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800881/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700882static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800883 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700884 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800885{
886 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700887 if (!pmd_none(*pmd))
888 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700889 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800890 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800891 if (pgtable)
892 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800893 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800894 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700895 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800896}
897
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800898int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
899 unsigned long address, pmd_t *pmd,
900 unsigned int flags)
901{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800902 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800903 struct page *page;
904 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800905
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700906 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700907 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700908 if (unlikely(anon_vma_prepare(vma)))
909 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700910 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700911 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200912 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700913 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800914 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700915 pgtable_t pgtable;
916 struct page *zero_page;
917 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700918 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700919 pgtable = pte_alloc_one(mm, haddr);
920 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800921 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700922 zero_page = get_huge_zero_page();
923 if (unlikely(!zero_page)) {
924 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700925 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700926 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700927 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800928 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700929 ret = 0;
930 set = false;
931 if (pmd_none(*pmd)) {
932 if (userfaultfd_missing(vma)) {
933 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700934 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700935 VM_UFFD_MISSING);
936 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
937 } else {
938 set_huge_zero_page(pgtable, mm, vma,
939 haddr, pmd,
940 zero_page);
941 spin_unlock(ptl);
942 set = true;
943 }
944 } else
945 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700946 if (!set) {
947 pte_free(mm, pgtable);
948 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800949 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700950 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800951 }
Mel Gorman444eb2a42016-03-17 14:19:23 -0700952 gfp = alloc_hugepage_direct_gfpmask(vma);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800953 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700954 if (unlikely(!page)) {
955 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700956 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700957 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800958 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700959 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
960 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800961}
962
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700963static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800964 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700965{
966 struct mm_struct *mm = vma->vm_mm;
967 pmd_t entry;
968 spinlock_t *ptl;
969
970 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -0800971 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
972 if (pfn_t_devmap(pfn))
973 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -0800974 if (write) {
975 entry = pmd_mkyoung(pmd_mkdirty(entry));
976 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700977 }
Ross Zwisler01871e52016-01-15 16:56:02 -0800978 set_pmd_at(mm, addr, pmd, entry);
979 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700980 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700981}
982
983int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800984 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700985{
986 pgprot_t pgprot = vma->vm_page_prot;
987 /*
988 * If we had pmd_special, we could avoid all these restrictions,
989 * but we need to be consistent with PTEs and architectures that
990 * can't support a 'special' bit.
991 */
992 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
993 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
994 (VM_PFNMAP|VM_MIXEDMAP));
995 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -0800996 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700997
998 if (addr < vma->vm_start || addr >= vma->vm_end)
999 return VM_FAULT_SIGBUS;
1000 if (track_pfn_insert(vma, &pgprot, pfn))
1001 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001002 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
1003 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001004}
1005
Dan Williams3565fce2016-01-15 16:56:55 -08001006static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1007 pmd_t *pmd)
1008{
1009 pmd_t _pmd;
1010
1011 /*
1012 * We should set the dirty bit only for FOLL_WRITE but for now
1013 * the dirty bit in the pmd is meaningless. And if the dirty
1014 * bit will become meaningful and we'll only set it with
1015 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1016 * set the young bit, instead of the current set_pmd_at.
1017 */
1018 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1019 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1020 pmd, _pmd, 1))
1021 update_mmu_cache_pmd(vma, addr, pmd);
1022}
1023
1024struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1025 pmd_t *pmd, int flags)
1026{
1027 unsigned long pfn = pmd_pfn(*pmd);
1028 struct mm_struct *mm = vma->vm_mm;
1029 struct dev_pagemap *pgmap;
1030 struct page *page;
1031
1032 assert_spin_locked(pmd_lockptr(mm, pmd));
1033
1034 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1035 return NULL;
1036
1037 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1038 /* pass */;
1039 else
1040 return NULL;
1041
1042 if (flags & FOLL_TOUCH)
1043 touch_pmd(vma, addr, pmd);
1044
1045 /*
1046 * device mapped pages can only be returned if the
1047 * caller will manage the page reference count.
1048 */
1049 if (!(flags & FOLL_GET))
1050 return ERR_PTR(-EEXIST);
1051
1052 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1053 pgmap = get_dev_pagemap(pfn, NULL);
1054 if (!pgmap)
1055 return ERR_PTR(-EFAULT);
1056 page = pfn_to_page(pfn);
1057 get_page(page);
1058 put_dev_pagemap(pgmap);
1059
1060 return page;
1061}
1062
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001063int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1064 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1065 struct vm_area_struct *vma)
1066{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001067 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001068 struct page *src_page;
1069 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001070 pgtable_t pgtable = NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001071 int ret;
1072
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001073 if (!vma_is_dax(vma)) {
1074 ret = -ENOMEM;
1075 pgtable = pte_alloc_one(dst_mm, addr);
1076 if (unlikely(!pgtable))
1077 goto out;
1078 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001079
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001080 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1081 src_ptl = pmd_lockptr(src_mm, src_pmd);
1082 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001083
1084 ret = -EAGAIN;
1085 pmd = *src_pmd;
Dan Williams5c7fb562016-01-15 16:56:52 -08001086 if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001087 pte_free(dst_mm, pgtable);
1088 goto out_unlock;
1089 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001090 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001091 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001092 * under splitting since we don't split the page itself, only pmd to
1093 * a page table.
1094 */
1095 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001096 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001097 /*
1098 * get_huge_zero_page() will never allocate a new page here,
1099 * since we already have a zero page to copy. It just takes a
1100 * reference.
1101 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001102 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001103 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001104 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001105 ret = 0;
1106 goto out_unlock;
1107 }
Mel Gormande466bd2013-12-18 17:08:42 -08001108
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001109 if (!vma_is_dax(vma)) {
Dan Williams5c7fb562016-01-15 16:56:52 -08001110 /* thp accounting separate from pmd_devmap accounting */
1111 src_page = pmd_page(pmd);
1112 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1113 get_page(src_page);
1114 page_dup_rmap(src_page, true);
1115 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1116 atomic_long_inc(&dst_mm->nr_ptes);
1117 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1118 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001119
1120 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1121 pmd = pmd_mkold(pmd_wrprotect(pmd));
1122 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001123
1124 ret = 0;
1125out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001126 spin_unlock(src_ptl);
1127 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128out:
1129 return ret;
1130}
1131
Will Deacona1dd4502012-12-11 16:01:27 -08001132void huge_pmd_set_accessed(struct mm_struct *mm,
1133 struct vm_area_struct *vma,
1134 unsigned long address,
1135 pmd_t *pmd, pmd_t orig_pmd,
1136 int dirty)
1137{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001138 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001139 pmd_t entry;
1140 unsigned long haddr;
1141
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001142 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001143 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1144 goto unlock;
1145
1146 entry = pmd_mkyoung(orig_pmd);
1147 haddr = address & HPAGE_PMD_MASK;
1148 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1149 update_mmu_cache_pmd(vma, address, pmd);
1150
1151unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001152 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001153}
1154
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001155static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1156 struct vm_area_struct *vma,
1157 unsigned long address,
1158 pmd_t *pmd, pmd_t orig_pmd,
1159 struct page *page,
1160 unsigned long haddr)
1161{
Johannes Weiner00501b52014-08-08 14:19:20 -07001162 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001163 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001164 pgtable_t pgtable;
1165 pmd_t _pmd;
1166 int ret = 0, i;
1167 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001168 unsigned long mmun_start; /* For mmu_notifiers */
1169 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001170
1171 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1172 GFP_KERNEL);
1173 if (unlikely(!pages)) {
1174 ret |= VM_FAULT_OOM;
1175 goto out;
1176 }
1177
1178 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001179 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1180 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001181 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001182 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001183 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001184 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001185 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001186 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001187 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001188 memcg = (void *)page_private(pages[i]);
1189 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001190 mem_cgroup_cancel_charge(pages[i], memcg,
1191 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001192 put_page(pages[i]);
1193 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001194 kfree(pages);
1195 ret |= VM_FAULT_OOM;
1196 goto out;
1197 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001198 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001199 }
1200
1201 for (i = 0; i < HPAGE_PMD_NR; i++) {
1202 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001203 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001204 __SetPageUptodate(pages[i]);
1205 cond_resched();
1206 }
1207
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001208 mmun_start = haddr;
1209 mmun_end = haddr + HPAGE_PMD_SIZE;
1210 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1211
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001212 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001213 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1214 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001215 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001216
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001217 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001218 /* leave pmd empty until pte is filled */
1219
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001220 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001221 pmd_populate(mm, &_pmd, pgtable);
1222
1223 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1224 pte_t *pte, entry;
1225 entry = mk_pte(pages[i], vma->vm_page_prot);
1226 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001227 memcg = (void *)page_private(pages[i]);
1228 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001229 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001230 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001231 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001232 pte = pte_offset_map(&_pmd, haddr);
1233 VM_BUG_ON(!pte_none(*pte));
1234 set_pte_at(mm, haddr, pte, entry);
1235 pte_unmap(pte);
1236 }
1237 kfree(pages);
1238
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 smp_wmb(); /* make pte visible before pmd */
1240 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001241 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001242 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001243
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001244 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1245
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001246 ret |= VM_FAULT_WRITE;
1247 put_page(page);
1248
1249out:
1250 return ret;
1251
1252out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001253 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001254 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001255 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001256 memcg = (void *)page_private(pages[i]);
1257 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001258 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001259 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001260 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001261 kfree(pages);
1262 goto out;
1263}
1264
1265int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1266 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1267{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001268 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001269 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001270 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001271 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001272 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001273 unsigned long mmun_start; /* For mmu_notifiers */
1274 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001275 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001276
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001277 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001278 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001279 haddr = address & HPAGE_PMD_MASK;
1280 if (is_huge_zero_pmd(orig_pmd))
1281 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001282 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001283 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1284 goto out_unlock;
1285
1286 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001287 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001288 /*
1289 * We can only reuse the page if nobody else maps the huge page or it's
1290 * part. We can do it by checking page_mapcount() on each sub-page, but
1291 * it's expensive.
1292 * The cheaper way is to check page_count() to be equal 1: every
1293 * mapcount takes page reference reference, so this way we can
1294 * guarantee, that the PMD is the only mapping.
1295 * This can give false negative if somebody pinned the page, but that's
1296 * fine.
1297 */
1298 if (page_mapcount(page) == 1 && page_count(page) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001299 pmd_t entry;
1300 entry = pmd_mkyoung(orig_pmd);
1301 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1302 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001303 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001304 ret |= VM_FAULT_WRITE;
1305 goto out_unlock;
1306 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001307 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001308 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001309alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001310 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001311 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001312 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b363692015-04-15 16:13:29 -07001313 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001314 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001315 new_page = NULL;
1316
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001317 if (likely(new_page)) {
1318 prep_transhuge_page(new_page);
1319 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001320 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001321 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001322 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001323 } else {
1324 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1325 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001326 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001327 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001328 ret |= VM_FAULT_FALLBACK;
1329 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001330 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001331 }
David Rientjes17766dd2013-09-12 15:14:06 -07001332 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001333 goto out;
1334 }
1335
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001336 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1337 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001338 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001339 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001340 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001341 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001342 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001343 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001344 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001345 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001346 goto out;
1347 }
1348
David Rientjes17766dd2013-09-12 15:14:06 -07001349 count_vm_event(THP_FAULT_ALLOC);
1350
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001351 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001352 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1353 else
1354 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001355 __SetPageUptodate(new_page);
1356
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001357 mmun_start = haddr;
1358 mmun_end = haddr + HPAGE_PMD_SIZE;
1359 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1360
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001361 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001362 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001363 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001364 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001365 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001366 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001367 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001368 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001369 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001370 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001371 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1372 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001373 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001374 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001375 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001376 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001377 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001378 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001379 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001380 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001381 put_huge_zero_page();
1382 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001383 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001384 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001385 put_page(page);
1386 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001387 ret |= VM_FAULT_WRITE;
1388 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001389 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001390out_mn:
1391 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1392out:
1393 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001394out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001395 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001396 return ret;
1397}
1398
David Rientjesb676b292012-10-08 16:34:03 -07001399struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001400 unsigned long addr,
1401 pmd_t *pmd,
1402 unsigned int flags)
1403{
David Rientjesb676b292012-10-08 16:34:03 -07001404 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001405 struct page *page = NULL;
1406
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001407 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001408
1409 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1410 goto out;
1411
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001412 /* Avoid dumping huge zero page */
1413 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1414 return ERR_PTR(-EFAULT);
1415
Mel Gorman2b4847e2013-12-18 17:08:32 -08001416 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001417 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001418 goto out;
1419
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001420 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001421 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001422 if (flags & FOLL_TOUCH)
1423 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001424 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001425 /*
1426 * We don't mlock() pte-mapped THPs. This way we can avoid
1427 * leaking mlocked pages into non-VM_LOCKED VMAs.
1428 *
1429 * In most cases the pmd is the only mapping of the page as we
1430 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1431 * writable private mappings in populate_vma_page_range().
1432 *
1433 * The only scenario when we have the page shared here is if we
1434 * mlocking read-only mapping shared over fork(). We skip
1435 * mlocking such pages.
1436 */
1437 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1438 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001439 lru_add_drain();
1440 if (page->mapping)
1441 mlock_vma_page(page);
1442 unlock_page(page);
1443 }
1444 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001445 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001446 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001447 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001448 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001449
1450out:
1451 return page;
1452}
1453
Mel Gormand10e63f2012-10-25 14:16:31 +02001454/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001455int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1456 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001457{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001458 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001459 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001460 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001461 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001462 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001463 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001464 bool page_locked;
1465 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001466 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001467 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001468
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001469 /* A PROT_NONE fault should not end up here */
1470 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1471
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001472 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001473 if (unlikely(!pmd_same(pmd, *pmdp)))
1474 goto out_unlock;
1475
Mel Gormande466bd2013-12-18 17:08:42 -08001476 /*
1477 * If there are potential migrations, wait for completion and retry
1478 * without disrupting NUMA hinting information. Do not relock and
1479 * check_same as the page may no longer be mapped.
1480 */
1481 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001482 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001483 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001484 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001485 goto out;
1486 }
1487
Mel Gormand10e63f2012-10-25 14:16:31 +02001488 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001489 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001490 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001491 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001492 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001493 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001494 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001495 flags |= TNF_FAULT_LOCAL;
1496 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001497
Mel Gormanbea66fb2015-03-25 15:55:37 -07001498 /* See similar comment in do_numa_page for explanation */
1499 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001500 flags |= TNF_NO_GROUP;
1501
1502 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001503 * Acquire the page lock to serialise THP migrations but avoid dropping
1504 * page_table_lock if at all possible
1505 */
Mel Gormanb8916632013-10-07 11:28:44 +01001506 page_locked = trylock_page(page);
1507 target_nid = mpol_misplaced(page, vma, haddr);
1508 if (target_nid == -1) {
1509 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001510 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001511 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001512 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001513
Mel Gormande466bd2013-12-18 17:08:42 -08001514 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001515 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001516 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001517 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001518 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001519 goto out;
1520 }
1521
Mel Gorman2b4847e2013-12-18 17:08:32 -08001522 /*
1523 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1524 * to serialises splits
1525 */
Mel Gormanb8916632013-10-07 11:28:44 +01001526 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001527 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001528 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001529
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001530 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001531 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001532 if (unlikely(!pmd_same(pmd, *pmdp))) {
1533 unlock_page(page);
1534 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001535 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001536 goto out_unlock;
1537 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001538
Mel Gormanc3a489c2013-12-18 17:08:38 -08001539 /* Bail if we fail to protect against THP splits for any reason */
1540 if (unlikely(!anon_vma)) {
1541 put_page(page);
1542 page_nid = -1;
1543 goto clear_pmdnuma;
1544 }
1545
Mel Gormana54a4072013-10-07 11:28:46 +01001546 /*
1547 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001548 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001549 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001550 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001551 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001552 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001553 if (migrated) {
1554 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001555 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001556 } else
1557 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001558
Mel Gorman8191acb2013-10-07 11:28:45 +01001559 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001560clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001561 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001562 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001563 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001564 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001565 if (was_writable)
1566 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001567 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001568 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001569 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001570out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001571 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001572
1573out:
1574 if (anon_vma)
1575 page_unlock_anon_vma_read(anon_vma);
1576
Mel Gorman8191acb2013-10-07 11:28:45 +01001577 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001578 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001579
Mel Gormand10e63f2012-10-25 14:16:31 +02001580 return 0;
1581}
1582
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001583int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1584 pmd_t *pmd, unsigned long addr, unsigned long next)
1585
1586{
1587 spinlock_t *ptl;
1588 pmd_t orig_pmd;
1589 struct page *page;
1590 struct mm_struct *mm = tlb->mm;
1591 int ret = 0;
1592
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001593 ptl = pmd_trans_huge_lock(pmd, vma);
1594 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001595 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001596
1597 orig_pmd = *pmd;
1598 if (is_huge_zero_pmd(orig_pmd)) {
1599 ret = 1;
1600 goto out;
1601 }
1602
1603 page = pmd_page(orig_pmd);
1604 /*
1605 * If other processes are mapping this page, we couldn't discard
1606 * the page unless they all do MADV_FREE so let's skip the page.
1607 */
1608 if (page_mapcount(page) != 1)
1609 goto out;
1610
1611 if (!trylock_page(page))
1612 goto out;
1613
1614 /*
1615 * If user want to discard part-pages of THP, split it so MADV_FREE
1616 * will deactivate only them.
1617 */
1618 if (next - addr != HPAGE_PMD_SIZE) {
1619 get_page(page);
1620 spin_unlock(ptl);
1621 if (split_huge_page(page)) {
1622 put_page(page);
1623 unlock_page(page);
1624 goto out_unlocked;
1625 }
1626 put_page(page);
1627 unlock_page(page);
1628 ret = 1;
1629 goto out_unlocked;
1630 }
1631
1632 if (PageDirty(page))
1633 ClearPageDirty(page);
1634 unlock_page(page);
1635
1636 if (PageActive(page))
1637 deactivate_page(page);
1638
1639 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1640 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1641 tlb->fullmm);
1642 orig_pmd = pmd_mkold(orig_pmd);
1643 orig_pmd = pmd_mkclean(orig_pmd);
1644
1645 set_pmd_at(mm, addr, pmd, orig_pmd);
1646 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1647 }
1648 ret = 1;
1649out:
1650 spin_unlock(ptl);
1651out_unlocked:
1652 return ret;
1653}
1654
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001655int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001656 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001657{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001658 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001659 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001660
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001661 ptl = __pmd_trans_huge_lock(pmd, vma);
1662 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001663 return 0;
1664 /*
1665 * For architectures like ppc64 we look at deposited pgtable
1666 * when calling pmdp_huge_get_and_clear. So do the
1667 * pgtable_trans_huge_withdraw after finishing pmdp related
1668 * operations.
1669 */
1670 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1671 tlb->fullmm);
1672 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1673 if (vma_is_dax(vma)) {
1674 spin_unlock(ptl);
1675 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001676 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001677 } else if (is_huge_zero_pmd(orig_pmd)) {
1678 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1679 atomic_long_dec(&tlb->mm->nr_ptes);
1680 spin_unlock(ptl);
1681 put_huge_zero_page();
1682 } else {
1683 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001684 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001685 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1686 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1687 VM_BUG_ON_PAGE(!PageHead(page), page);
1688 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1689 atomic_long_dec(&tlb->mm->nr_ptes);
1690 spin_unlock(ptl);
1691 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001692 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001693 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001694}
1695
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001696bool move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001697 unsigned long old_addr,
1698 unsigned long new_addr, unsigned long old_end,
1699 pmd_t *old_pmd, pmd_t *new_pmd)
1700{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001701 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001702 pmd_t pmd;
1703
1704 struct mm_struct *mm = vma->vm_mm;
1705
1706 if ((old_addr & ~HPAGE_PMD_MASK) ||
1707 (new_addr & ~HPAGE_PMD_MASK) ||
1708 old_end - old_addr < HPAGE_PMD_SIZE ||
1709 (new_vma->vm_flags & VM_NOHUGEPAGE))
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001710 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001711
1712 /*
1713 * The destination pmd shouldn't be established, free_pgtables()
1714 * should have release it.
1715 */
1716 if (WARN_ON(!pmd_none(*new_pmd))) {
1717 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001718 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001719 }
1720
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001721 /*
1722 * We don't have to worry about the ordering of src and dst
1723 * ptlocks because exclusive mmap_sem prevents deadlock.
1724 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001725 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1726 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001727 new_ptl = pmd_lockptr(mm, new_pmd);
1728 if (new_ptl != old_ptl)
1729 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001730 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001731 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001732
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001733 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1734 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301735 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001736 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1737 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001738 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301739 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1740 if (new_ptl != old_ptl)
1741 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001742 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001743 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001744 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001745 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001746}
1747
Mel Gormanf123d742013-10-07 11:28:49 +01001748/*
1749 * Returns
1750 * - 0 if PMD could not be locked
1751 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1752 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1753 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001754int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001755 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001756{
1757 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001758 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001759 int ret = 0;
1760
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001761 ptl = __pmd_trans_huge_lock(pmd, vma);
1762 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001763 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001764 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001765 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001766
1767 /*
1768 * Avoid trapping faults against the zero page. The read-only
1769 * data is likely to be read-cached on the local CPU and
1770 * local/remote hits to the zero page are not interesting.
1771 */
1772 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1773 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001774 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001775 }
1776
Mel Gorman10c10452015-02-12 14:58:44 -08001777 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001778 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001779 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001780 if (preserve_write)
1781 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001782 ret = HPAGE_PMD_NR;
1783 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001784 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001785 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001786 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001787 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001788
1789 return ret;
1790}
1791
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001792/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001793 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001794 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001795 * Note that if it returns true, this routine returns without unlocking page
1796 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001797 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001798spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001799{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001800 spinlock_t *ptl;
1801 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001802 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001803 return ptl;
1804 spin_unlock(ptl);
1805 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001806}
1807
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001808#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001809
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001810int hugepage_madvise(struct vm_area_struct *vma,
1811 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001812{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001813 switch (advice) {
1814 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001815#ifdef CONFIG_S390
1816 /*
1817 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1818 * can't handle this properly after s390_enable_sie, so we simply
1819 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1820 */
1821 if (mm_has_pgste(vma->vm_mm))
1822 return 0;
1823#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001824 /*
1825 * Be somewhat over-protective like KSM for now!
1826 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001827 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001828 return -EINVAL;
1829 *vm_flags &= ~VM_NOHUGEPAGE;
1830 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001831 /*
1832 * If the vma become good for khugepaged to scan,
1833 * register it here without waiting a page fault that
1834 * may not happen any time soon.
1835 */
David Rientjes6d50e602014-10-29 14:50:31 -07001836 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001837 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001838 break;
1839 case MADV_NOHUGEPAGE:
1840 /*
1841 * Be somewhat over-protective like KSM for now!
1842 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001843 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001844 return -EINVAL;
1845 *vm_flags &= ~VM_HUGEPAGE;
1846 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001847 /*
1848 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1849 * this vma even if we leave the mm registered in khugepaged if
1850 * it got registered before VM_NOHUGEPAGE was set.
1851 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001852 break;
1853 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001854
1855 return 0;
1856}
1857
Andrea Arcangeliba761492011-01-13 15:46:58 -08001858static int __init khugepaged_slab_init(void)
1859{
1860 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1861 sizeof(struct mm_slot),
1862 __alignof__(struct mm_slot), 0, NULL);
1863 if (!mm_slot_cache)
1864 return -ENOMEM;
1865
1866 return 0;
1867}
1868
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001869static void __init khugepaged_slab_exit(void)
1870{
1871 kmem_cache_destroy(mm_slot_cache);
1872}
1873
Andrea Arcangeliba761492011-01-13 15:46:58 -08001874static inline struct mm_slot *alloc_mm_slot(void)
1875{
1876 if (!mm_slot_cache) /* initialization failed */
1877 return NULL;
1878 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1879}
1880
1881static inline void free_mm_slot(struct mm_slot *mm_slot)
1882{
1883 kmem_cache_free(mm_slot_cache, mm_slot);
1884}
1885
Andrea Arcangeliba761492011-01-13 15:46:58 -08001886static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1887{
1888 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001889
Sasha Levinb67bfe02013-02-27 17:06:00 -08001890 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001891 if (mm == mm_slot->mm)
1892 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001893
Andrea Arcangeliba761492011-01-13 15:46:58 -08001894 return NULL;
1895}
1896
1897static void insert_to_mm_slots_hash(struct mm_struct *mm,
1898 struct mm_slot *mm_slot)
1899{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001900 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001901 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001902}
1903
1904static inline int khugepaged_test_exit(struct mm_struct *mm)
1905{
1906 return atomic_read(&mm->mm_users) == 0;
1907}
1908
1909int __khugepaged_enter(struct mm_struct *mm)
1910{
1911 struct mm_slot *mm_slot;
1912 int wakeup;
1913
1914 mm_slot = alloc_mm_slot();
1915 if (!mm_slot)
1916 return -ENOMEM;
1917
1918 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001919 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001920 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1921 free_mm_slot(mm_slot);
1922 return 0;
1923 }
1924
1925 spin_lock(&khugepaged_mm_lock);
1926 insert_to_mm_slots_hash(mm, mm_slot);
1927 /*
1928 * Insert just behind the scanning cursor, to let the area settle
1929 * down a little.
1930 */
1931 wakeup = list_empty(&khugepaged_scan.mm_head);
1932 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1933 spin_unlock(&khugepaged_mm_lock);
1934
1935 atomic_inc(&mm->mm_count);
1936 if (wakeup)
1937 wake_up_interruptible(&khugepaged_wait);
1938
1939 return 0;
1940}
1941
David Rientjes6d50e602014-10-29 14:50:31 -07001942int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1943 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001944{
1945 unsigned long hstart, hend;
1946 if (!vma->anon_vma)
1947 /*
1948 * Not yet faulted in so we will register later in the
1949 * page fault if needed.
1950 */
1951 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001952 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001953 /* khugepaged not yet working on file or special mappings */
1954 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07001955 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001956 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1957 hend = vma->vm_end & HPAGE_PMD_MASK;
1958 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001959 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001960 return 0;
1961}
1962
1963void __khugepaged_exit(struct mm_struct *mm)
1964{
1965 struct mm_slot *mm_slot;
1966 int free = 0;
1967
1968 spin_lock(&khugepaged_mm_lock);
1969 mm_slot = get_mm_slot(mm);
1970 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001971 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001972 list_del(&mm_slot->mm_node);
1973 free = 1;
1974 }
Chris Wrightd788e802011-07-25 17:12:14 -07001975 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001976
1977 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001978 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1979 free_mm_slot(mm_slot);
1980 mmdrop(mm);
1981 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001982 /*
1983 * This is required to serialize against
1984 * khugepaged_test_exit() (which is guaranteed to run
1985 * under mmap sem read mode). Stop here (after we
1986 * return all pagetables will be destroyed) until
1987 * khugepaged has finished working on the pagetables
1988 * under the mmap_sem.
1989 */
1990 down_write(&mm->mmap_sem);
1991 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001992 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001993}
1994
1995static void release_pte_page(struct page *page)
1996{
1997 /* 0 stands for page_is_file_cache(page) == false */
1998 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1999 unlock_page(page);
2000 putback_lru_page(page);
2001}
2002
2003static void release_pte_pages(pte_t *pte, pte_t *_pte)
2004{
2005 while (--_pte >= pte) {
2006 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002007 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002008 release_pte_page(pte_page(pteval));
2009 }
2010}
2011
Andrea Arcangeliba761492011-01-13 15:46:58 -08002012static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2013 unsigned long address,
2014 pte_t *pte)
2015{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002016 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002017 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002018 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002019 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002020
Andrea Arcangeliba761492011-01-13 15:46:58 -08002021 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2022 _pte++, address += PAGE_SIZE) {
2023 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002024 if (pte_none(pteval) || (pte_present(pteval) &&
2025 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002026 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002027 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002028 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002029 } else {
2030 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002031 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002032 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002033 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002034 if (!pte_present(pteval)) {
2035 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002036 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002037 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002038 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002039 if (unlikely(!page)) {
2040 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002041 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002042 }
Bob Liu344aa352012-12-11 16:00:34 -08002043
Sasha Levin309381fea2014-01-23 15:52:54 -08002044 VM_BUG_ON_PAGE(PageCompound(page), page);
2045 VM_BUG_ON_PAGE(!PageAnon(page), page);
2046 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002047
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 /*
2049 * We can do it before isolate_lru_page because the
2050 * page can't be freed from under us. NOTE: PG_lock
2051 * is needed to serialize against split_huge_page
2052 * when invoked from the VM.
2053 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002054 if (!trylock_page(page)) {
2055 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002056 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002057 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002058
2059 /*
2060 * cannot use mapcount: can't collapse if there's a gup pin.
2061 * The page must only be referenced by the scanned process
2062 * and page swap cache.
2063 */
2064 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2065 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002066 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002067 goto out;
2068 }
2069 if (pte_write(pteval)) {
2070 writable = true;
2071 } else {
2072 if (PageSwapCache(page) && !reuse_swap_page(page)) {
2073 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002074 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002075 goto out;
2076 }
2077 /*
2078 * Page is not in the swap cache. It can be collapsed
2079 * into a THP.
2080 */
2081 }
2082
Andrea Arcangeliba761492011-01-13 15:46:58 -08002083 /*
2084 * Isolate the page to avoid collapsing an hugepage
2085 * currently in use by the VM.
2086 */
2087 if (isolate_lru_page(page)) {
2088 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002089 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002090 goto out;
2091 }
2092 /* 0 stands for page_is_file_cache(page) == false */
2093 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002094 VM_BUG_ON_PAGE(!PageLocked(page), page);
2095 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002096
2097 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002098 if (pte_young(pteval) ||
2099 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002100 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002101 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002102 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002103 if (likely(writable)) {
2104 if (likely(referenced)) {
2105 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002106 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002107 referenced, writable, result);
2108 return 1;
2109 }
2110 } else {
2111 result = SCAN_PAGE_RO;
2112 }
2113
Andrea Arcangeliba761492011-01-13 15:46:58 -08002114out:
Bob Liu344aa352012-12-11 16:00:34 -08002115 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002116 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002117 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002118 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002119}
2120
2121static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2122 struct vm_area_struct *vma,
2123 unsigned long address,
2124 spinlock_t *ptl)
2125{
2126 pte_t *_pte;
2127 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2128 pte_t pteval = *_pte;
2129 struct page *src_page;
2130
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002131 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002132 clear_user_highpage(page, address);
2133 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002134 if (is_zero_pfn(pte_pfn(pteval))) {
2135 /*
2136 * ptl mostly unnecessary.
2137 */
2138 spin_lock(ptl);
2139 /*
2140 * paravirt calls inside pte_clear here are
2141 * superfluous.
2142 */
2143 pte_clear(vma->vm_mm, address, _pte);
2144 spin_unlock(ptl);
2145 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002146 } else {
2147 src_page = pte_page(pteval);
2148 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002149 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002150 release_pte_page(src_page);
2151 /*
2152 * ptl mostly unnecessary, but preempt has to
2153 * be disabled to update the per-cpu stats
2154 * inside page_remove_rmap().
2155 */
2156 spin_lock(ptl);
2157 /*
2158 * paravirt calls inside pte_clear here are
2159 * superfluous.
2160 */
2161 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002162 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002163 spin_unlock(ptl);
2164 free_page_and_swap_cache(src_page);
2165 }
2166
2167 address += PAGE_SIZE;
2168 page++;
2169 }
2170}
2171
Xiao Guangrong26234f32012-10-08 16:29:51 -07002172static void khugepaged_alloc_sleep(void)
2173{
Petr Mladekbde43c62015-09-08 15:04:05 -07002174 DEFINE_WAIT(wait);
2175
2176 add_wait_queue(&khugepaged_wait, &wait);
2177 freezable_schedule_timeout_interruptible(
2178 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2179 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002180}
2181
Bob Liu9f1b8682013-11-12 15:07:37 -08002182static int khugepaged_node_load[MAX_NUMNODES];
2183
David Rientjes14a4e212014-08-06 16:07:29 -07002184static bool khugepaged_scan_abort(int nid)
2185{
2186 int i;
2187
2188 /*
2189 * If zone_reclaim_mode is disabled, then no extra effort is made to
2190 * allocate memory locally.
2191 */
2192 if (!zone_reclaim_mode)
2193 return false;
2194
2195 /* If there is a count for this node already, it must be acceptable */
2196 if (khugepaged_node_load[nid])
2197 return false;
2198
2199 for (i = 0; i < MAX_NUMNODES; i++) {
2200 if (!khugepaged_node_load[i])
2201 continue;
2202 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2203 return true;
2204 }
2205 return false;
2206}
2207
Xiao Guangrong26234f32012-10-08 16:29:51 -07002208#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002209static int khugepaged_find_target_node(void)
2210{
2211 static int last_khugepaged_target_node = NUMA_NO_NODE;
2212 int nid, target_node = 0, max_value = 0;
2213
2214 /* find first node with max normal pages hit */
2215 for (nid = 0; nid < MAX_NUMNODES; nid++)
2216 if (khugepaged_node_load[nid] > max_value) {
2217 max_value = khugepaged_node_load[nid];
2218 target_node = nid;
2219 }
2220
2221 /* do some balance if several nodes have the same hit record */
2222 if (target_node <= last_khugepaged_target_node)
2223 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2224 nid++)
2225 if (max_value == khugepaged_node_load[nid]) {
2226 target_node = nid;
2227 break;
2228 }
2229
2230 last_khugepaged_target_node = target_node;
2231 return target_node;
2232}
2233
Xiao Guangrong26234f32012-10-08 16:29:51 -07002234static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2235{
2236 if (IS_ERR(*hpage)) {
2237 if (!*wait)
2238 return false;
2239
2240 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002241 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002242 khugepaged_alloc_sleep();
2243 } else if (*hpage) {
2244 put_page(*hpage);
2245 *hpage = NULL;
2246 }
2247
2248 return true;
2249}
2250
Michal Hocko3b363692015-04-15 16:13:29 -07002251static struct page *
2252khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002253 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002254{
Sasha Levin309381fea2014-01-23 15:52:54 -08002255 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002256
Xiao Guangrong26234f32012-10-08 16:29:51 -07002257 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002258 * Before allocating the hugepage, release the mmap_sem read lock.
2259 * The allocation can take potentially a long time if it involves
2260 * sync compaction, and we do not need to hold the mmap_sem during
2261 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002262 */
2263 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002264
Vlastimil Babka96db8002015-09-08 15:03:50 -07002265 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002266 if (unlikely(!*hpage)) {
2267 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2268 *hpage = ERR_PTR(-ENOMEM);
2269 return NULL;
2270 }
2271
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002272 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002273 count_vm_event(THP_COLLAPSE_ALLOC);
2274 return *hpage;
2275}
2276#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002277static int khugepaged_find_target_node(void)
2278{
2279 return 0;
2280}
2281
Mel Gorman444eb2a42016-03-17 14:19:23 -07002282static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002283{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002284 struct page *page;
2285
Mel Gorman444eb2a42016-03-17 14:19:23 -07002286 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2287 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002288 if (page)
2289 prep_transhuge_page(page);
2290 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002291}
2292
Xiao Guangrong26234f32012-10-08 16:29:51 -07002293static struct page *khugepaged_alloc_hugepage(bool *wait)
2294{
2295 struct page *hpage;
2296
2297 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002298 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002299 if (!hpage) {
2300 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2301 if (!*wait)
2302 return NULL;
2303
2304 *wait = false;
2305 khugepaged_alloc_sleep();
2306 } else
2307 count_vm_event(THP_COLLAPSE_ALLOC);
2308 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2309
2310 return hpage;
2311}
2312
2313static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2314{
2315 if (!*hpage)
2316 *hpage = khugepaged_alloc_hugepage(wait);
2317
2318 if (unlikely(!*hpage))
2319 return false;
2320
2321 return true;
2322}
2323
Michal Hocko3b363692015-04-15 16:13:29 -07002324static struct page *
2325khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002326 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002327{
2328 up_read(&mm->mmap_sem);
2329 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002330
Xiao Guangrong26234f32012-10-08 16:29:51 -07002331 return *hpage;
2332}
2333#endif
2334
Bob Liufa475e52012-12-11 16:00:39 -08002335static bool hugepage_vma_check(struct vm_area_struct *vma)
2336{
2337 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2338 (vma->vm_flags & VM_NOHUGEPAGE))
2339 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002340 if (!vma->anon_vma || vma->vm_ops)
2341 return false;
2342 if (is_vma_temporary_stack(vma))
2343 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002344 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002345 return true;
2346}
2347
Andrea Arcangeliba761492011-01-13 15:46:58 -08002348static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002349 unsigned long address,
2350 struct page **hpage,
2351 struct vm_area_struct *vma,
2352 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002353{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002354 pmd_t *pmd, _pmd;
2355 pte_t *pte;
2356 pgtable_t pgtable;
2357 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002358 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002359 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002360 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002361 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002362 unsigned long mmun_start; /* For mmu_notifiers */
2363 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002364 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002365
2366 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002367
Michal Hocko3b363692015-04-15 16:13:29 -07002368 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002369 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b363692015-04-15 16:13:29 -07002370
Xiao Guangrong26234f32012-10-08 16:29:51 -07002371 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002372 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002373 if (!new_page) {
2374 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2375 goto out_nolock;
2376 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002377
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002378 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002379 result = SCAN_CGROUP_CHARGE_FAIL;
2380 goto out_nolock;
2381 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002382
2383 /*
2384 * Prevent all access to pagetables with the exception of
2385 * gup_fast later hanlded by the ptep_clear_flush and the VM
2386 * handled by the anon_vma lock + PG_lock.
2387 */
2388 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002389 if (unlikely(khugepaged_test_exit(mm))) {
2390 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002391 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002392 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393
2394 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002395 if (!vma) {
2396 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002397 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002398 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002399 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2400 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002401 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2402 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002403 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002404 }
2405 if (!hugepage_vma_check(vma)) {
2406 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002407 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002408 }
Bob Liu62190492012-12-11 16:00:37 -08002409 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002410 if (!pmd) {
2411 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002412 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002413 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002414
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002415 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416
2417 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002418 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002419
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002420 mmun_start = address;
2421 mmun_end = address + HPAGE_PMD_SIZE;
2422 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002423 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002424 /*
2425 * After this gup_fast can't run anymore. This also removes
2426 * any huge TLB entry from the CPU so we won't allow
2427 * huge and small TLB entries for the same virtual address
2428 * to avoid the risk of CPU bugs in that area.
2429 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002430 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002431 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002432 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002433
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002434 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002435 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002436 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002437
2438 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002439 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002440 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002441 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002442 /*
2443 * We can only use set_pmd_at when establishing
2444 * hugepmds and never for establishing regular pmds that
2445 * points to regular pagetables. Use pmd_populate for that
2446 */
2447 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002448 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002449 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002450 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002451 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002452 }
2453
2454 /*
2455 * All pages are isolated and locked so anon_vma rmap
2456 * can't run anymore.
2457 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002458 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002459
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002460 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002461 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002462 __SetPageUptodate(new_page);
2463 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002464
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002465 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2466 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002467
2468 /*
2469 * spin_lock() below is not the equivalent of smp_wmb(), so
2470 * this is needed to avoid the copy_huge_page writes to become
2471 * visible after the set_pmd_at() write.
2472 */
2473 smp_wmb();
2474
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002475 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002476 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002477 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002478 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002479 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002480 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002481 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002482 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002483 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484
2485 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002486
Andrea Arcangeliba761492011-01-13 15:46:58 -08002487 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002488 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002489out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002490 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002491 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002492 return;
2493
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002494out_nolock:
2495 trace_mm_collapse_huge_page(mm, isolated, result);
2496 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002497out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002498 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002499 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002500}
2501
2502static int khugepaged_scan_pmd(struct mm_struct *mm,
2503 struct vm_area_struct *vma,
2504 unsigned long address,
2505 struct page **hpage)
2506{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002507 pmd_t *pmd;
2508 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002509 int ret = 0, none_or_zero = 0, result = 0;
2510 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002511 unsigned long _address;
2512 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002513 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002514 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002515
2516 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2517
Bob Liu62190492012-12-11 16:00:37 -08002518 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002519 if (!pmd) {
2520 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002521 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002522 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002523
Bob Liu9f1b8682013-11-12 15:07:37 -08002524 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002525 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2526 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2527 _pte++, _address += PAGE_SIZE) {
2528 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002529 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002530 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002531 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002532 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002533 } else {
2534 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002535 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002536 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002537 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002538 if (!pte_present(pteval)) {
2539 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002540 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002541 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002542 if (pte_write(pteval))
2543 writable = true;
2544
Andrea Arcangeliba761492011-01-13 15:46:58 -08002545 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002546 if (unlikely(!page)) {
2547 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002548 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002549 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002550
2551 /* TODO: teach khugepaged to collapse THP mapped with pte */
2552 if (PageCompound(page)) {
2553 result = SCAN_PAGE_COMPOUND;
2554 goto out_unmap;
2555 }
2556
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002557 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002558 * Record which node the original page is from and save this
2559 * information to khugepaged_node_load[].
2560 * Khupaged will allocate hugepage from the node has the max
2561 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002562 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002563 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002564 if (khugepaged_scan_abort(node)) {
2565 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002566 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002567 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002568 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002569 if (!PageLRU(page)) {
2570 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002572 }
2573 if (PageLocked(page)) {
2574 result = SCAN_PAGE_LOCK;
2575 goto out_unmap;
2576 }
2577 if (!PageAnon(page)) {
2578 result = SCAN_PAGE_ANON;
2579 goto out_unmap;
2580 }
2581
Ebru Akagunduz10359212015-02-11 15:28:28 -08002582 /*
2583 * cannot use mapcount: can't collapse if there's a gup pin.
2584 * The page must only be referenced by the scanned process
2585 * and page swap cache.
2586 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002587 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2588 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002589 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002590 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002591 if (pte_young(pteval) ||
2592 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002593 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002594 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002595 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002596 if (writable) {
2597 if (referenced) {
2598 result = SCAN_SUCCEED;
2599 ret = 1;
2600 } else {
2601 result = SCAN_NO_REFERENCED_PAGE;
2602 }
2603 } else {
2604 result = SCAN_PAGE_RO;
2605 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002606out_unmap:
2607 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002608 if (ret) {
2609 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002610 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002611 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002612 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002613out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002614 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002615 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002616 return ret;
2617}
2618
2619static void collect_mm_slot(struct mm_slot *mm_slot)
2620{
2621 struct mm_struct *mm = mm_slot->mm;
2622
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002623 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002624
2625 if (khugepaged_test_exit(mm)) {
2626 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002627 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002628 list_del(&mm_slot->mm_node);
2629
2630 /*
2631 * Not strictly needed because the mm exited already.
2632 *
2633 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2634 */
2635
2636 /* khugepaged_mm_lock actually not necessary for the below */
2637 free_mm_slot(mm_slot);
2638 mmdrop(mm);
2639 }
2640}
2641
2642static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2643 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002644 __releases(&khugepaged_mm_lock)
2645 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002646{
2647 struct mm_slot *mm_slot;
2648 struct mm_struct *mm;
2649 struct vm_area_struct *vma;
2650 int progress = 0;
2651
2652 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002653 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002654
2655 if (khugepaged_scan.mm_slot)
2656 mm_slot = khugepaged_scan.mm_slot;
2657 else {
2658 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2659 struct mm_slot, mm_node);
2660 khugepaged_scan.address = 0;
2661 khugepaged_scan.mm_slot = mm_slot;
2662 }
2663 spin_unlock(&khugepaged_mm_lock);
2664
2665 mm = mm_slot->mm;
2666 down_read(&mm->mmap_sem);
2667 if (unlikely(khugepaged_test_exit(mm)))
2668 vma = NULL;
2669 else
2670 vma = find_vma(mm, khugepaged_scan.address);
2671
2672 progress++;
2673 for (; vma; vma = vma->vm_next) {
2674 unsigned long hstart, hend;
2675
2676 cond_resched();
2677 if (unlikely(khugepaged_test_exit(mm))) {
2678 progress++;
2679 break;
2680 }
Bob Liufa475e52012-12-11 16:00:39 -08002681 if (!hugepage_vma_check(vma)) {
2682skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002683 progress++;
2684 continue;
2685 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002686 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2687 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002688 if (hstart >= hend)
2689 goto skip;
2690 if (khugepaged_scan.address > hend)
2691 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002692 if (khugepaged_scan.address < hstart)
2693 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002694 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002695
2696 while (khugepaged_scan.address < hend) {
2697 int ret;
2698 cond_resched();
2699 if (unlikely(khugepaged_test_exit(mm)))
2700 goto breakouterloop;
2701
2702 VM_BUG_ON(khugepaged_scan.address < hstart ||
2703 khugepaged_scan.address + HPAGE_PMD_SIZE >
2704 hend);
2705 ret = khugepaged_scan_pmd(mm, vma,
2706 khugepaged_scan.address,
2707 hpage);
2708 /* move to next address */
2709 khugepaged_scan.address += HPAGE_PMD_SIZE;
2710 progress += HPAGE_PMD_NR;
2711 if (ret)
2712 /* we released mmap_sem so break loop */
2713 goto breakouterloop_mmap_sem;
2714 if (progress >= pages)
2715 goto breakouterloop;
2716 }
2717 }
2718breakouterloop:
2719 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2720breakouterloop_mmap_sem:
2721
2722 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002723 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002724 /*
2725 * Release the current mm_slot if this mm is about to die, or
2726 * if we scanned all vmas of this mm.
2727 */
2728 if (khugepaged_test_exit(mm) || !vma) {
2729 /*
2730 * Make sure that if mm_users is reaching zero while
2731 * khugepaged runs here, khugepaged_exit will find
2732 * mm_slot not pointing to the exiting mm.
2733 */
2734 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2735 khugepaged_scan.mm_slot = list_entry(
2736 mm_slot->mm_node.next,
2737 struct mm_slot, mm_node);
2738 khugepaged_scan.address = 0;
2739 } else {
2740 khugepaged_scan.mm_slot = NULL;
2741 khugepaged_full_scans++;
2742 }
2743
2744 collect_mm_slot(mm_slot);
2745 }
2746
2747 return progress;
2748}
2749
2750static int khugepaged_has_work(void)
2751{
2752 return !list_empty(&khugepaged_scan.mm_head) &&
2753 khugepaged_enabled();
2754}
2755
2756static int khugepaged_wait_event(void)
2757{
2758 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002759 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002760}
2761
Xiao Guangrongd5169042012-10-08 16:29:48 -07002762static void khugepaged_do_scan(void)
2763{
2764 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002765 unsigned int progress = 0, pass_through_head = 0;
2766 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002767 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002768
2769 barrier(); /* write khugepaged_pages_to_scan to local stack */
2770
2771 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002772 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002773 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002774
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002775 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002776
Jiri Kosinacd092412015-06-24 16:56:07 -07002777 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002778 break;
2779
Andrea Arcangeliba761492011-01-13 15:46:58 -08002780 spin_lock(&khugepaged_mm_lock);
2781 if (!khugepaged_scan.mm_slot)
2782 pass_through_head++;
2783 if (khugepaged_has_work() &&
2784 pass_through_head < 2)
2785 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002786 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002787 else
2788 progress = pages;
2789 spin_unlock(&khugepaged_mm_lock);
2790 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002791
Xiao Guangrongd5169042012-10-08 16:29:48 -07002792 if (!IS_ERR_OR_NULL(hpage))
2793 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002794}
2795
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002796static void khugepaged_wait_work(void)
2797{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002798 if (khugepaged_has_work()) {
2799 if (!khugepaged_scan_sleep_millisecs)
2800 return;
2801
2802 wait_event_freezable_timeout(khugepaged_wait,
2803 kthread_should_stop(),
2804 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2805 return;
2806 }
2807
2808 if (khugepaged_enabled())
2809 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2810}
2811
Andrea Arcangeliba761492011-01-13 15:46:58 -08002812static int khugepaged(void *none)
2813{
2814 struct mm_slot *mm_slot;
2815
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002816 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002817 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002818
Xiao Guangrongb7231782012-10-08 16:29:54 -07002819 while (!kthread_should_stop()) {
2820 khugepaged_do_scan();
2821 khugepaged_wait_work();
2822 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002823
2824 spin_lock(&khugepaged_mm_lock);
2825 mm_slot = khugepaged_scan.mm_slot;
2826 khugepaged_scan.mm_slot = NULL;
2827 if (mm_slot)
2828 collect_mm_slot(mm_slot);
2829 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002830 return 0;
2831}
2832
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002833static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2834 unsigned long haddr, pmd_t *pmd)
2835{
2836 struct mm_struct *mm = vma->vm_mm;
2837 pgtable_t pgtable;
2838 pmd_t _pmd;
2839 int i;
2840
2841 /* leave pmd empty until pte is filled */
2842 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2843
2844 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2845 pmd_populate(mm, &_pmd, pgtable);
2846
2847 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2848 pte_t *pte, entry;
2849 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2850 entry = pte_mkspecial(entry);
2851 pte = pte_offset_map(&_pmd, haddr);
2852 VM_BUG_ON(!pte_none(*pte));
2853 set_pte_at(mm, haddr, pte, entry);
2854 pte_unmap(pte);
2855 }
2856 smp_wmb(); /* make pte visible before pmd */
2857 pmd_populate(mm, pmd, pgtable);
2858 put_huge_zero_page();
2859}
2860
2861static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002862 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002863{
2864 struct mm_struct *mm = vma->vm_mm;
2865 struct page *page;
2866 pgtable_t pgtable;
2867 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002868 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002869 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002870 int i;
2871
2872 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2873 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2874 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002875 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002876
2877 count_vm_event(THP_SPLIT_PMD);
2878
2879 if (vma_is_dax(vma)) {
2880 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2881 if (is_huge_zero_pmd(_pmd))
2882 put_huge_zero_page();
2883 return;
2884 } else if (is_huge_zero_pmd(*pmd)) {
2885 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2886 }
2887
2888 page = pmd_page(*pmd);
2889 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002890 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002891 write = pmd_write(*pmd);
2892 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002893 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002894
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05302895 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002896 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2897 pmd_populate(mm, &_pmd, pgtable);
2898
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002899 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002900 pte_t entry, *pte;
2901 /*
2902 * Note that NUMA hinting access restrictions are not
2903 * transferred to avoid any possibility of altering
2904 * permissions across VMAs.
2905 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002906 if (freeze) {
2907 swp_entry_t swp_entry;
2908 swp_entry = make_migration_entry(page + i, write);
2909 entry = swp_entry_to_pte(swp_entry);
2910 } else {
2911 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002912 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002913 if (!write)
2914 entry = pte_wrprotect(entry);
2915 if (!young)
2916 entry = pte_mkold(entry);
2917 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002918 if (dirty)
2919 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002920 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002921 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002922 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002923 atomic_inc(&page[i]._mapcount);
2924 pte_unmap(pte);
2925 }
2926
2927 /*
2928 * Set PG_double_map before dropping compound_mapcount to avoid
2929 * false-negative page_mapped().
2930 */
2931 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2932 for (i = 0; i < HPAGE_PMD_NR; i++)
2933 atomic_inc(&page[i]._mapcount);
2934 }
2935
2936 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2937 /* Last compound_mapcount is gone. */
2938 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2939 if (TestClearPageDoubleMap(page)) {
2940 /* No need in mapcount reference anymore */
2941 for (i = 0; i < HPAGE_PMD_NR; i++)
2942 atomic_dec(&page[i]._mapcount);
2943 }
2944 }
2945
2946 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002947 /*
2948 * Up to this point the pmd is present and huge and userland has the
2949 * whole access to the hugepage during the split (which happens in
2950 * place). If we overwrite the pmd with the not-huge version pointing
2951 * to the pte here (which of course we could if all CPUs were bug
2952 * free), userland could trigger a small page size TLB miss on the
2953 * small sized TLB while the hugepage TLB entry is still established in
2954 * the huge TLB. Some CPU doesn't like that.
2955 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2956 * 383 on page 93. Intel should be safe but is also warns that it's
2957 * only safe if the permission and cache attributes of the two entries
2958 * loaded in the two TLB is identical (which should be the case here).
2959 * But it is generally safer to never allow small and huge TLB entries
2960 * for the same virtual address to be loaded simultaneously. So instead
2961 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2962 * current pmd notpresent (atomically because here the pmd_trans_huge
2963 * and pmd_trans_splitting must remain set at all times on the pmd
2964 * until the split is complete for this pmd), then we flush the SMP TLB
2965 * and finally we write the non-huge version of the pmd entry with
2966 * pmd_populate.
2967 */
2968 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002969 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002970
2971 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002972 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002973 page_remove_rmap(page + i, false);
2974 put_page(page + i);
2975 }
2976 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002977}
2978
2979void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002980 unsigned long address, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002981{
2982 spinlock_t *ptl;
2983 struct mm_struct *mm = vma->vm_mm;
2984 unsigned long haddr = address & HPAGE_PMD_MASK;
2985
2986 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2987 ptl = pmd_lock(mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08002988 if (pmd_trans_huge(*pmd)) {
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07002989 struct page *page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08002990 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07002991 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08002992 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002993 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002994 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002995out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002996 spin_unlock(ptl);
2997 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
2998}
2999
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003000void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3001 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003002{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003003 pgd_t *pgd;
3004 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003005 pmd_t *pmd;
3006
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003007 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003008 if (!pgd_present(*pgd))
3009 return;
3010
3011 pud = pud_offset(pgd, address);
3012 if (!pud_present(*pud))
3013 return;
3014
3015 pmd = pmd_offset(pud, address);
Dan Williams5c7fb562016-01-15 16:56:52 -08003016 if (!pmd_present(*pmd) || (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003017 return;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003018
3019 /*
3020 * If caller asks to setup a migration entries, we need a page to check
3021 * pmd against. Otherwise we can end up replacing wrong page.
3022 */
3023 VM_BUG_ON(freeze && !page);
3024 if (page && page != pmd_page(*pmd))
3025 return;
3026
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003027 /*
3028 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3029 * materialize from under us.
3030 */
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003031 __split_huge_pmd(vma, pmd, address, freeze);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003032}
3033
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003034void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003035 unsigned long start,
3036 unsigned long end,
3037 long adjust_next)
3038{
3039 /*
3040 * If the new start address isn't hpage aligned and it could
3041 * previously contain an hugepage: check if we need to split
3042 * an huge pmd.
3043 */
3044 if (start & ~HPAGE_PMD_MASK &&
3045 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3046 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003047 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003048
3049 /*
3050 * If the new end address isn't hpage aligned and it could
3051 * previously contain an hugepage: check if we need to split
3052 * an huge pmd.
3053 */
3054 if (end & ~HPAGE_PMD_MASK &&
3055 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3056 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003057 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003058
3059 /*
3060 * If we're also updating the vma->vm_next->vm_start, if the new
3061 * vm_next->vm_start isn't page aligned and it could previously
3062 * contain an hugepage: check if we need to split an huge pmd.
3063 */
3064 if (adjust_next > 0) {
3065 struct vm_area_struct *next = vma->vm_next;
3066 unsigned long nstart = next->vm_start;
3067 nstart += adjust_next << PAGE_SHIFT;
3068 if (nstart & ~HPAGE_PMD_MASK &&
3069 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3070 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003071 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003072 }
3073}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003074
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003075static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003076{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003077 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3078 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3079 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003080
3081 VM_BUG_ON_PAGE(!PageHead(page), page);
3082
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003083 /* We only need TTU_SPLIT_HUGE_PMD once */
3084 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3085 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3086 /* Cut short if the page is unmapped */
3087 if (page_count(page) == 1)
3088 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003089
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003090 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003091 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003092 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003093}
3094
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003095static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003096{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003097 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003098
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003099 for (i = 0; i < HPAGE_PMD_NR; i++)
3100 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003101}
3102
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003103static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003104 struct lruvec *lruvec, struct list_head *list)
3105{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003106 struct page *page_tail = head + tail;
3107
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003108 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003109 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003110
3111 /*
3112 * tail_page->_count is zero and not changing from under us. But
3113 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003114 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003115 * would then run atomic_set() concurrently with
3116 * get_page_unless_zero(), and atomic_set() is implemented in C not
3117 * using locked ops. spin_unlock on x86 sometime uses locked ops
3118 * because of PPro errata 66, 92, so unless somebody can guarantee
3119 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003120 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003121 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003122 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003123
3124 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3125 page_tail->flags |= (head->flags &
3126 ((1L << PG_referenced) |
3127 (1L << PG_swapbacked) |
3128 (1L << PG_mlocked) |
3129 (1L << PG_uptodate) |
3130 (1L << PG_active) |
3131 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003132 (1L << PG_unevictable) |
3133 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003134
3135 /*
3136 * After clearing PageTail the gup refcount can be released.
3137 * Page flags also must be visible before we make the page non-compound.
3138 */
3139 smp_wmb();
3140
3141 clear_compound_head(page_tail);
3142
3143 if (page_is_young(head))
3144 set_page_young(page_tail);
3145 if (page_is_idle(head))
3146 set_page_idle(page_tail);
3147
3148 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003149 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003150 page_tail);
3151 page_tail->mapping = head->mapping;
3152
3153 page_tail->index = head->index + tail;
3154 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3155 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003156}
3157
3158static void __split_huge_page(struct page *page, struct list_head *list)
3159{
3160 struct page *head = compound_head(page);
3161 struct zone *zone = page_zone(head);
3162 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003163 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003164
3165 /* prevent PageLRU to go away from under us, and freeze lru stats */
3166 spin_lock_irq(&zone->lru_lock);
3167 lruvec = mem_cgroup_page_lruvec(head, zone);
3168
3169 /* complete memcg works before add pages to LRU */
3170 mem_cgroup_split_huge_fixup(head);
3171
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003172 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003173 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003174
3175 ClearPageCompound(head);
3176 spin_unlock_irq(&zone->lru_lock);
3177
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003178 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003179
3180 for (i = 0; i < HPAGE_PMD_NR; i++) {
3181 struct page *subpage = head + i;
3182 if (subpage == page)
3183 continue;
3184 unlock_page(subpage);
3185
3186 /*
3187 * Subpages may be freed if there wasn't any mapping
3188 * like if add_to_swap() is running on a lru page that
3189 * had its mapping zapped. And freeing these pages
3190 * requires taking the lru_lock so we do the put_page
3191 * of the tail pages after the split is complete.
3192 */
3193 put_page(subpage);
3194 }
3195}
3196
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003197int total_mapcount(struct page *page)
3198{
3199 int i, ret;
3200
3201 VM_BUG_ON_PAGE(PageTail(page), page);
3202
3203 if (likely(!PageCompound(page)))
3204 return atomic_read(&page->_mapcount) + 1;
3205
3206 ret = compound_mapcount(page);
3207 if (PageHuge(page))
3208 return ret;
3209 for (i = 0; i < HPAGE_PMD_NR; i++)
3210 ret += atomic_read(&page[i]._mapcount) + 1;
3211 if (PageDoubleMap(page))
3212 ret -= HPAGE_PMD_NR;
3213 return ret;
3214}
3215
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003216/*
3217 * This function splits huge page into normal pages. @page can point to any
3218 * subpage of huge page to split. Split doesn't change the position of @page.
3219 *
3220 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3221 * The huge page must be locked.
3222 *
3223 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3224 *
3225 * Both head page and tail pages will inherit mapping, flags, and so on from
3226 * the hugepage.
3227 *
3228 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3229 * they are not mapped.
3230 *
3231 * Returns 0 if the hugepage is split successfully.
3232 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3233 * us.
3234 */
3235int split_huge_page_to_list(struct page *page, struct list_head *list)
3236{
3237 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003238 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003239 struct anon_vma *anon_vma;
3240 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003241 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003242 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003243
3244 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3245 VM_BUG_ON_PAGE(!PageAnon(page), page);
3246 VM_BUG_ON_PAGE(!PageLocked(page), page);
3247 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3248 VM_BUG_ON_PAGE(!PageCompound(page), page);
3249
3250 /*
3251 * The caller does not necessarily hold an mmap_sem that would prevent
3252 * the anon_vma disappearing so we first we take a reference to it
3253 * and then lock the anon_vma for write. This is similar to
3254 * page_lock_anon_vma_read except the write lock is taken to serialise
3255 * against parallel split or collapse operations.
3256 */
3257 anon_vma = page_get_anon_vma(head);
3258 if (!anon_vma) {
3259 ret = -EBUSY;
3260 goto out;
3261 }
3262 anon_vma_lock_write(anon_vma);
3263
3264 /*
3265 * Racy check if we can split the page, before freeze_page() will
3266 * split PMDs
3267 */
3268 if (total_mapcount(head) != page_count(head) - 1) {
3269 ret = -EBUSY;
3270 goto out_unlock;
3271 }
3272
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003273 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003274 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003275 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3276
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003277 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3278 if (mlocked)
3279 lru_add_drain();
3280
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003281 /* Prevent deferred_split_scan() touching ->_count */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003282 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003283 count = page_count(head);
3284 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003285 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003286 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003287 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003288 list_del(page_deferred_list(head));
3289 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003290 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003291 __split_huge_page(page, list);
3292 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003293 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003294 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003295 pr_alert("total_mapcount: %u, page_count(): %u\n",
3296 mapcount, count);
3297 if (PageTail(page))
3298 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003299 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003300 BUG();
3301 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003302 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003303 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003304 ret = -EBUSY;
3305 }
3306
3307out_unlock:
3308 anon_vma_unlock_write(anon_vma);
3309 put_anon_vma(anon_vma);
3310out:
3311 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3312 return ret;
3313}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003314
3315void free_transhuge_page(struct page *page)
3316{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003317 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003318 unsigned long flags;
3319
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003320 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003321 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003322 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003323 list_del(page_deferred_list(page));
3324 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003325 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003326 free_compound_page(page);
3327}
3328
3329void deferred_split_huge_page(struct page *page)
3330{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003331 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003332 unsigned long flags;
3333
3334 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3335
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003336 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003337 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003338 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003339 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3340 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003341 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003342 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003343}
3344
3345static unsigned long deferred_split_count(struct shrinker *shrink,
3346 struct shrink_control *sc)
3347{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003348 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003349 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003350}
3351
3352static unsigned long deferred_split_scan(struct shrinker *shrink,
3353 struct shrink_control *sc)
3354{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003355 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003356 unsigned long flags;
3357 LIST_HEAD(list), *pos, *next;
3358 struct page *page;
3359 int split = 0;
3360
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003361 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003362 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003363 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003364 page = list_entry((void *)pos, struct page, mapping);
3365 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003366 if (get_page_unless_zero(page)) {
3367 list_move(page_deferred_list(page), &list);
3368 } else {
3369 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003370 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003371 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003372 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003373 if (!--sc->nr_to_scan)
3374 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003375 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003376 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003377
3378 list_for_each_safe(pos, next, &list) {
3379 page = list_entry((void *)pos, struct page, mapping);
3380 lock_page(page);
3381 /* split_huge_page() removes page from list on success */
3382 if (!split_huge_page(page))
3383 split++;
3384 unlock_page(page);
3385 put_page(page);
3386 }
3387
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003388 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3389 list_splice_tail(&list, &pgdata->split_queue);
3390 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003391
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003392 /*
3393 * Stop shrinker if we didn't split any page, but the queue is empty.
3394 * This can happen if pages were freed under us.
3395 */
3396 if (!split && list_empty(&pgdata->split_queue))
3397 return SHRINK_STOP;
3398 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003399}
3400
3401static struct shrinker deferred_split_shrinker = {
3402 .count_objects = deferred_split_count,
3403 .scan_objects = deferred_split_scan,
3404 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003405 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003406};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003407
3408#ifdef CONFIG_DEBUG_FS
3409static int split_huge_pages_set(void *data, u64 val)
3410{
3411 struct zone *zone;
3412 struct page *page;
3413 unsigned long pfn, max_zone_pfn;
3414 unsigned long total = 0, split = 0;
3415
3416 if (val != 1)
3417 return -EINVAL;
3418
3419 for_each_populated_zone(zone) {
3420 max_zone_pfn = zone_end_pfn(zone);
3421 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3422 if (!pfn_valid(pfn))
3423 continue;
3424
3425 page = pfn_to_page(pfn);
3426 if (!get_page_unless_zero(page))
3427 continue;
3428
3429 if (zone != page_zone(page))
3430 goto next;
3431
3432 if (!PageHead(page) || !PageAnon(page) ||
3433 PageHuge(page))
3434 goto next;
3435
3436 total++;
3437 lock_page(page);
3438 if (!split_huge_page(page))
3439 split++;
3440 unlock_page(page);
3441next:
3442 put_page(page);
3443 }
3444 }
3445
3446 pr_info("%lu of %lu THP split", split, total);
3447
3448 return 0;
3449}
3450DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3451 "%llu\n");
3452
3453static int __init split_huge_pages_debugfs(void)
3454{
3455 void *ret;
3456
3457 ret = debugfs_create_file("split_huge_pages", 0644, NULL, NULL,
3458 &split_huge_pages_fops);
3459 if (!ret)
3460 pr_warn("Failed to create split_huge_pages in debugfs");
3461 return 0;
3462}
3463late_initcall(split_huge_pages_debugfs);
3464#endif