blob: 66675eed67be6d76ea45294faf9bfe00568a2cd2 [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 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053086static unsigned int khugepaged_pages_to_scan __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -080087static 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 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530101static unsigned int khugepaged_max_ptes_none __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800102
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 Perches756a025f02016-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
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700235void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800236{
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
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530672 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
673 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
674 /*
675 * hugepages can't be allocated by the buddy allocator
676 */
677 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
678 /*
679 * we use page->mapping and page->index in second tail page
680 * as list_head: assuming THP order >= 2
681 */
682 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
683
Shaohua Li569e5592012-01-12 17:19:11 -0800684 err = hugepage_init_sysfs(&hugepage_kobj);
685 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700686 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800687
688 err = khugepaged_slab_init();
689 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700690 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800691
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700692 err = register_shrinker(&huge_zero_page_shrinker);
693 if (err)
694 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800695 err = register_shrinker(&deferred_split_shrinker);
696 if (err)
697 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800698
Rik van Riel97562cd2011-01-13 15:47:12 -0800699 /*
700 * By default disable transparent hugepages on smaller systems,
701 * where the extra memory used could hurt more than TLB overhead
702 * is likely to save. The admin can still enable it through /sys.
703 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700704 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800705 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700706 return 0;
707 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800708
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700709 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700710 if (err)
711 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800712
Shaohua Li569e5592012-01-12 17:19:11 -0800713 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700714err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800715 unregister_shrinker(&deferred_split_shrinker);
716err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700717 unregister_shrinker(&huge_zero_page_shrinker);
718err_hzp_shrinker:
719 khugepaged_slab_exit();
720err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800721 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700722err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800723 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800724}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800725subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800726
727static int __init setup_transparent_hugepage(char *str)
728{
729 int ret = 0;
730 if (!str)
731 goto out;
732 if (!strcmp(str, "always")) {
733 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
734 &transparent_hugepage_flags);
735 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
736 &transparent_hugepage_flags);
737 ret = 1;
738 } else if (!strcmp(str, "madvise")) {
739 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
740 &transparent_hugepage_flags);
741 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
742 &transparent_hugepage_flags);
743 ret = 1;
744 } else if (!strcmp(str, "never")) {
745 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
746 &transparent_hugepage_flags);
747 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
748 &transparent_hugepage_flags);
749 ret = 1;
750 }
751out:
752 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700753 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800754 return ret;
755}
756__setup("transparent_hugepage=", setup_transparent_hugepage);
757
Mel Gormanb32967f2012-11-19 12:35:47 +0000758pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800759{
760 if (likely(vma->vm_flags & VM_WRITE))
761 pmd = pmd_mkwrite(pmd);
762 return pmd;
763}
764
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700765static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800766{
767 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700768 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800769 entry = pmd_mkhuge(entry);
770 return entry;
771}
772
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800773static inline struct list_head *page_deferred_list(struct page *page)
774{
775 /*
776 * ->lru in the tail pages is occupied by compound_head.
777 * Let's use ->mapping + ->index in the second tail page as list_head.
778 */
779 return (struct list_head *)&page[2].mapping;
780}
781
782void prep_transhuge_page(struct page *page)
783{
784 /*
785 * we use page->mapping and page->indexlru in second tail page
786 * as list_head: assuming THP order >= 2
787 */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800788
789 INIT_LIST_HEAD(page_deferred_list(page));
790 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
791}
792
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800793static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
794 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700795 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700796 struct page *page, gfp_t gfp,
797 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800798{
Johannes Weiner00501b52014-08-08 14:19:20 -0700799 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800800 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800801 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700802 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800803
Sasha Levin309381fea2014-01-23 15:52:54 -0800804 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700805
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800806 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700807 put_page(page);
808 count_vm_event(THP_FAULT_FALLBACK);
809 return VM_FAULT_FALLBACK;
810 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800811
Johannes Weiner00501b52014-08-08 14:19:20 -0700812 pgtable = pte_alloc_one(mm, haddr);
813 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800814 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700815 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700816 return VM_FAULT_OOM;
817 }
818
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800819 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700820 /*
821 * The memory barrier inside __SetPageUptodate makes sure that
822 * clear_huge_page writes become visible before the set_pmd_at()
823 * write.
824 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800825 __SetPageUptodate(page);
826
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800827 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800828 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800829 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800830 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800831 put_page(page);
832 pte_free(mm, pgtable);
833 } else {
834 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700835
836 /* Deliver the page fault to userland */
837 if (userfaultfd_missing(vma)) {
838 int ret;
839
840 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800841 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700842 put_page(page);
843 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700844 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700845 VM_UFFD_MISSING);
846 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
847 return ret;
848 }
849
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700850 entry = mk_huge_pmd(page, vma->vm_page_prot);
851 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800852 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800853 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700854 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700855 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800856 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800857 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800858 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800859 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700860 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800861 }
862
David Rientjesaa2e8782012-05-29 15:06:17 -0700863 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800864}
865
Mel Gorman444eb2a42016-03-17 14:19:23 -0700866/*
867 * If THP is set to always then directly reclaim/compact as necessary
868 * If set to defer then do no reclaim and defer to khugepaged
869 * If set to madvise and the VMA is flagged then directly reclaim/compact
870 */
871static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800872{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700873 gfp_t reclaim_flags = 0;
874
875 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
876 (vma->vm_flags & VM_HUGEPAGE))
877 reclaim_flags = __GFP_DIRECT_RECLAIM;
878 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
879 reclaim_flags = __GFP_KSWAPD_RECLAIM;
880 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
881 reclaim_flags = __GFP_DIRECT_RECLAIM;
882
883 return GFP_TRANSHUGE | reclaim_flags;
884}
885
886/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
887static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
888{
889 return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800890}
891
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800892/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700893static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800894 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700895 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800896{
897 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700898 if (!pmd_none(*pmd))
899 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700900 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800901 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800902 if (pgtable)
903 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800904 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800905 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700906 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800907}
908
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800909int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
910 unsigned long address, pmd_t *pmd,
911 unsigned int flags)
912{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800913 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800914 struct page *page;
915 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800916
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700917 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700918 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700919 if (unlikely(anon_vma_prepare(vma)))
920 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700921 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700922 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200923 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700924 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800925 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700926 pgtable_t pgtable;
927 struct page *zero_page;
928 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700929 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700930 pgtable = pte_alloc_one(mm, haddr);
931 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800932 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700933 zero_page = get_huge_zero_page();
934 if (unlikely(!zero_page)) {
935 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700936 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700937 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700938 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800939 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700940 ret = 0;
941 set = false;
942 if (pmd_none(*pmd)) {
943 if (userfaultfd_missing(vma)) {
944 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700945 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700946 VM_UFFD_MISSING);
947 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
948 } else {
949 set_huge_zero_page(pgtable, mm, vma,
950 haddr, pmd,
951 zero_page);
952 spin_unlock(ptl);
953 set = true;
954 }
955 } else
956 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700957 if (!set) {
958 pte_free(mm, pgtable);
959 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800960 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700961 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800962 }
Mel Gorman444eb2a42016-03-17 14:19:23 -0700963 gfp = alloc_hugepage_direct_gfpmask(vma);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800964 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700965 if (unlikely(!page)) {
966 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700967 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700968 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800969 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700970 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
971 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800972}
973
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700974static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800975 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700976{
977 struct mm_struct *mm = vma->vm_mm;
978 pmd_t entry;
979 spinlock_t *ptl;
980
981 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -0800982 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
983 if (pfn_t_devmap(pfn))
984 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -0800985 if (write) {
986 entry = pmd_mkyoung(pmd_mkdirty(entry));
987 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700988 }
Ross Zwisler01871e52016-01-15 16:56:02 -0800989 set_pmd_at(mm, addr, pmd, entry);
990 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700991 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700992}
993
994int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800995 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700996{
997 pgprot_t pgprot = vma->vm_page_prot;
998 /*
999 * If we had pmd_special, we could avoid all these restrictions,
1000 * but we need to be consistent with PTEs and architectures that
1001 * can't support a 'special' bit.
1002 */
1003 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1004 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1005 (VM_PFNMAP|VM_MIXEDMAP));
1006 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -08001007 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001008
1009 if (addr < vma->vm_start || addr >= vma->vm_end)
1010 return VM_FAULT_SIGBUS;
1011 if (track_pfn_insert(vma, &pgprot, pfn))
1012 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001013 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
1014 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001015}
1016
Dan Williams3565fce2016-01-15 16:56:55 -08001017static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1018 pmd_t *pmd)
1019{
1020 pmd_t _pmd;
1021
1022 /*
1023 * We should set the dirty bit only for FOLL_WRITE but for now
1024 * the dirty bit in the pmd is meaningless. And if the dirty
1025 * bit will become meaningful and we'll only set it with
1026 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1027 * set the young bit, instead of the current set_pmd_at.
1028 */
1029 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1030 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1031 pmd, _pmd, 1))
1032 update_mmu_cache_pmd(vma, addr, pmd);
1033}
1034
1035struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1036 pmd_t *pmd, int flags)
1037{
1038 unsigned long pfn = pmd_pfn(*pmd);
1039 struct mm_struct *mm = vma->vm_mm;
1040 struct dev_pagemap *pgmap;
1041 struct page *page;
1042
1043 assert_spin_locked(pmd_lockptr(mm, pmd));
1044
1045 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1046 return NULL;
1047
1048 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1049 /* pass */;
1050 else
1051 return NULL;
1052
1053 if (flags & FOLL_TOUCH)
1054 touch_pmd(vma, addr, pmd);
1055
1056 /*
1057 * device mapped pages can only be returned if the
1058 * caller will manage the page reference count.
1059 */
1060 if (!(flags & FOLL_GET))
1061 return ERR_PTR(-EEXIST);
1062
1063 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1064 pgmap = get_dev_pagemap(pfn, NULL);
1065 if (!pgmap)
1066 return ERR_PTR(-EFAULT);
1067 page = pfn_to_page(pfn);
1068 get_page(page);
1069 put_dev_pagemap(pgmap);
1070
1071 return page;
1072}
1073
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001074int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1075 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1076 struct vm_area_struct *vma)
1077{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001078 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001079 struct page *src_page;
1080 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001081 pgtable_t pgtable = NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001082 int ret;
1083
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001084 if (!vma_is_dax(vma)) {
1085 ret = -ENOMEM;
1086 pgtable = pte_alloc_one(dst_mm, addr);
1087 if (unlikely(!pgtable))
1088 goto out;
1089 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001090
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001091 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1092 src_ptl = pmd_lockptr(src_mm, src_pmd);
1093 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001094
1095 ret = -EAGAIN;
1096 pmd = *src_pmd;
Dan Williams5c7fb562016-01-15 16:56:52 -08001097 if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098 pte_free(dst_mm, pgtable);
1099 goto out_unlock;
1100 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001101 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001102 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001103 * under splitting since we don't split the page itself, only pmd to
1104 * a page table.
1105 */
1106 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001107 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001108 /*
1109 * get_huge_zero_page() will never allocate a new page here,
1110 * since we already have a zero page to copy. It just takes a
1111 * reference.
1112 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001113 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001114 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001115 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001116 ret = 0;
1117 goto out_unlock;
1118 }
Mel Gormande466bd2013-12-18 17:08:42 -08001119
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001120 if (!vma_is_dax(vma)) {
Dan Williams5c7fb562016-01-15 16:56:52 -08001121 /* thp accounting separate from pmd_devmap accounting */
1122 src_page = pmd_page(pmd);
1123 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1124 get_page(src_page);
1125 page_dup_rmap(src_page, true);
1126 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1127 atomic_long_inc(&dst_mm->nr_ptes);
1128 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1129 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001130
1131 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1132 pmd = pmd_mkold(pmd_wrprotect(pmd));
1133 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001134
1135 ret = 0;
1136out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001137 spin_unlock(src_ptl);
1138 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001139out:
1140 return ret;
1141}
1142
Will Deacona1dd4502012-12-11 16:01:27 -08001143void huge_pmd_set_accessed(struct mm_struct *mm,
1144 struct vm_area_struct *vma,
1145 unsigned long address,
1146 pmd_t *pmd, pmd_t orig_pmd,
1147 int dirty)
1148{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001149 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001150 pmd_t entry;
1151 unsigned long haddr;
1152
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001153 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001154 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1155 goto unlock;
1156
1157 entry = pmd_mkyoung(orig_pmd);
1158 haddr = address & HPAGE_PMD_MASK;
1159 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1160 update_mmu_cache_pmd(vma, address, pmd);
1161
1162unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001163 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001164}
1165
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001166static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1167 struct vm_area_struct *vma,
1168 unsigned long address,
1169 pmd_t *pmd, pmd_t orig_pmd,
1170 struct page *page,
1171 unsigned long haddr)
1172{
Johannes Weiner00501b52014-08-08 14:19:20 -07001173 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001174 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001175 pgtable_t pgtable;
1176 pmd_t _pmd;
1177 int ret = 0, i;
1178 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001179 unsigned long mmun_start; /* For mmu_notifiers */
1180 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001181
1182 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1183 GFP_KERNEL);
1184 if (unlikely(!pages)) {
1185 ret |= VM_FAULT_OOM;
1186 goto out;
1187 }
1188
1189 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001190 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1191 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001192 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001193 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001194 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001195 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001196 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001197 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001198 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001199 memcg = (void *)page_private(pages[i]);
1200 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001201 mem_cgroup_cancel_charge(pages[i], memcg,
1202 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001203 put_page(pages[i]);
1204 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001205 kfree(pages);
1206 ret |= VM_FAULT_OOM;
1207 goto out;
1208 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001209 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001210 }
1211
1212 for (i = 0; i < HPAGE_PMD_NR; i++) {
1213 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001214 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001215 __SetPageUptodate(pages[i]);
1216 cond_resched();
1217 }
1218
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001219 mmun_start = haddr;
1220 mmun_end = haddr + HPAGE_PMD_SIZE;
1221 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1222
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001223 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001224 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1225 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001226 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001227
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001228 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001229 /* leave pmd empty until pte is filled */
1230
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001231 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001232 pmd_populate(mm, &_pmd, pgtable);
1233
1234 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1235 pte_t *pte, entry;
1236 entry = mk_pte(pages[i], vma->vm_page_prot);
1237 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001238 memcg = (void *)page_private(pages[i]);
1239 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001240 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001241 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001242 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001243 pte = pte_offset_map(&_pmd, haddr);
1244 VM_BUG_ON(!pte_none(*pte));
1245 set_pte_at(mm, haddr, pte, entry);
1246 pte_unmap(pte);
1247 }
1248 kfree(pages);
1249
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001250 smp_wmb(); /* make pte visible before pmd */
1251 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001252 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001253 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001254
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001255 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1256
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001257 ret |= VM_FAULT_WRITE;
1258 put_page(page);
1259
1260out:
1261 return ret;
1262
1263out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001264 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001265 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001266 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001267 memcg = (void *)page_private(pages[i]);
1268 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001269 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001270 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001271 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001272 kfree(pages);
1273 goto out;
1274}
1275
1276int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1277 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1278{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001279 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001281 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001282 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001283 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001284 unsigned long mmun_start; /* For mmu_notifiers */
1285 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001286 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001287
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001288 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001289 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001290 haddr = address & HPAGE_PMD_MASK;
1291 if (is_huge_zero_pmd(orig_pmd))
1292 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001293 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001294 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1295 goto out_unlock;
1296
1297 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001298 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001299 /*
1300 * We can only reuse the page if nobody else maps the huge page or it's
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001301 * part.
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001302 */
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001303 if (page_trans_huge_mapcount(page, NULL) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001304 pmd_t entry;
1305 entry = pmd_mkyoung(orig_pmd);
1306 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1307 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001308 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001309 ret |= VM_FAULT_WRITE;
1310 goto out_unlock;
1311 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001312 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001313 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001314alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001315 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001316 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001317 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b363692015-04-15 16:13:29 -07001318 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001319 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001320 new_page = NULL;
1321
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001322 if (likely(new_page)) {
1323 prep_transhuge_page(new_page);
1324 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001325 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001326 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001327 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001328 } else {
1329 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1330 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001331 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001332 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001333 ret |= VM_FAULT_FALLBACK;
1334 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001335 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001336 }
David Rientjes17766dd2013-09-12 15:14:06 -07001337 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001338 goto out;
1339 }
1340
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001341 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1342 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001343 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001344 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001345 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001346 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001347 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001348 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001349 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001350 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001351 goto out;
1352 }
1353
David Rientjes17766dd2013-09-12 15:14:06 -07001354 count_vm_event(THP_FAULT_ALLOC);
1355
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001356 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001357 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1358 else
1359 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001360 __SetPageUptodate(new_page);
1361
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001362 mmun_start = haddr;
1363 mmun_end = haddr + HPAGE_PMD_SIZE;
1364 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1365
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001366 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001367 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001368 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001369 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001370 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001371 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001372 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001373 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001374 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001375 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001376 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1377 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001378 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001379 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001380 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001381 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001382 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001383 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001384 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001385 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001386 put_huge_zero_page();
1387 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001388 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001389 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001390 put_page(page);
1391 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001392 ret |= VM_FAULT_WRITE;
1393 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001394 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001395out_mn:
1396 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1397out:
1398 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001399out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001400 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001401 return ret;
1402}
1403
David Rientjesb676b292012-10-08 16:34:03 -07001404struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001405 unsigned long addr,
1406 pmd_t *pmd,
1407 unsigned int flags)
1408{
David Rientjesb676b292012-10-08 16:34:03 -07001409 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001410 struct page *page = NULL;
1411
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001412 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001413
1414 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1415 goto out;
1416
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001417 /* Avoid dumping huge zero page */
1418 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1419 return ERR_PTR(-EFAULT);
1420
Mel Gorman2b4847e2013-12-18 17:08:32 -08001421 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001422 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001423 goto out;
1424
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001425 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001426 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001427 if (flags & FOLL_TOUCH)
1428 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001429 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001430 /*
1431 * We don't mlock() pte-mapped THPs. This way we can avoid
1432 * leaking mlocked pages into non-VM_LOCKED VMAs.
1433 *
1434 * In most cases the pmd is the only mapping of the page as we
1435 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1436 * writable private mappings in populate_vma_page_range().
1437 *
1438 * The only scenario when we have the page shared here is if we
1439 * mlocking read-only mapping shared over fork(). We skip
1440 * mlocking such pages.
1441 */
1442 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1443 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001444 lru_add_drain();
1445 if (page->mapping)
1446 mlock_vma_page(page);
1447 unlock_page(page);
1448 }
1449 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001450 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001451 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001452 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001453 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001454
1455out:
1456 return page;
1457}
1458
Mel Gormand10e63f2012-10-25 14:16:31 +02001459/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001460int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1461 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001462{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001463 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001464 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001465 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001466 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001467 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001468 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001469 bool page_locked;
1470 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001471 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001472 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001473
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001474 /* A PROT_NONE fault should not end up here */
1475 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1476
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001477 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001478 if (unlikely(!pmd_same(pmd, *pmdp)))
1479 goto out_unlock;
1480
Mel Gormande466bd2013-12-18 17:08:42 -08001481 /*
1482 * If there are potential migrations, wait for completion and retry
1483 * without disrupting NUMA hinting information. Do not relock and
1484 * check_same as the page may no longer be mapped.
1485 */
1486 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001487 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001488 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001489 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001490 goto out;
1491 }
1492
Mel Gormand10e63f2012-10-25 14:16:31 +02001493 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001494 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001495 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001496 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001497 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001498 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001499 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001500 flags |= TNF_FAULT_LOCAL;
1501 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001502
Mel Gormanbea66fb2015-03-25 15:55:37 -07001503 /* See similar comment in do_numa_page for explanation */
1504 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001505 flags |= TNF_NO_GROUP;
1506
1507 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001508 * Acquire the page lock to serialise THP migrations but avoid dropping
1509 * page_table_lock if at all possible
1510 */
Mel Gormanb8916632013-10-07 11:28:44 +01001511 page_locked = trylock_page(page);
1512 target_nid = mpol_misplaced(page, vma, haddr);
1513 if (target_nid == -1) {
1514 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001515 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001516 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001517 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001518
Mel Gormande466bd2013-12-18 17:08:42 -08001519 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001520 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001521 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001522 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001523 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001524 goto out;
1525 }
1526
Mel Gorman2b4847e2013-12-18 17:08:32 -08001527 /*
1528 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1529 * to serialises splits
1530 */
Mel Gormanb8916632013-10-07 11:28:44 +01001531 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001532 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001533 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001534
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001535 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001536 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001537 if (unlikely(!pmd_same(pmd, *pmdp))) {
1538 unlock_page(page);
1539 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001540 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001541 goto out_unlock;
1542 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001543
Mel Gormanc3a489c2013-12-18 17:08:38 -08001544 /* Bail if we fail to protect against THP splits for any reason */
1545 if (unlikely(!anon_vma)) {
1546 put_page(page);
1547 page_nid = -1;
1548 goto clear_pmdnuma;
1549 }
1550
Mel Gormana54a4072013-10-07 11:28:46 +01001551 /*
1552 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001553 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001554 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001555 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001556 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001557 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001558 if (migrated) {
1559 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001560 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001561 } else
1562 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001563
Mel Gorman8191acb2013-10-07 11:28:45 +01001564 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001565clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001566 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001567 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001568 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001569 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001570 if (was_writable)
1571 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001572 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001573 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001574 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001575out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001576 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001577
1578out:
1579 if (anon_vma)
1580 page_unlock_anon_vma_read(anon_vma);
1581
Mel Gorman8191acb2013-10-07 11:28:45 +01001582 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001583 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001584
Mel Gormand10e63f2012-10-25 14:16:31 +02001585 return 0;
1586}
1587
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001588int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1589 pmd_t *pmd, unsigned long addr, unsigned long next)
1590
1591{
1592 spinlock_t *ptl;
1593 pmd_t orig_pmd;
1594 struct page *page;
1595 struct mm_struct *mm = tlb->mm;
1596 int ret = 0;
1597
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001598 ptl = pmd_trans_huge_lock(pmd, vma);
1599 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001600 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001601
1602 orig_pmd = *pmd;
1603 if (is_huge_zero_pmd(orig_pmd)) {
1604 ret = 1;
1605 goto out;
1606 }
1607
1608 page = pmd_page(orig_pmd);
1609 /*
1610 * If other processes are mapping this page, we couldn't discard
1611 * the page unless they all do MADV_FREE so let's skip the page.
1612 */
1613 if (page_mapcount(page) != 1)
1614 goto out;
1615
1616 if (!trylock_page(page))
1617 goto out;
1618
1619 /*
1620 * If user want to discard part-pages of THP, split it so MADV_FREE
1621 * will deactivate only them.
1622 */
1623 if (next - addr != HPAGE_PMD_SIZE) {
1624 get_page(page);
1625 spin_unlock(ptl);
1626 if (split_huge_page(page)) {
1627 put_page(page);
1628 unlock_page(page);
1629 goto out_unlocked;
1630 }
1631 put_page(page);
1632 unlock_page(page);
1633 ret = 1;
1634 goto out_unlocked;
1635 }
1636
1637 if (PageDirty(page))
1638 ClearPageDirty(page);
1639 unlock_page(page);
1640
1641 if (PageActive(page))
1642 deactivate_page(page);
1643
1644 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1645 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1646 tlb->fullmm);
1647 orig_pmd = pmd_mkold(orig_pmd);
1648 orig_pmd = pmd_mkclean(orig_pmd);
1649
1650 set_pmd_at(mm, addr, pmd, orig_pmd);
1651 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1652 }
1653 ret = 1;
1654out:
1655 spin_unlock(ptl);
1656out_unlocked:
1657 return ret;
1658}
1659
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001660int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001661 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001662{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001663 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001664 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001665
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001666 ptl = __pmd_trans_huge_lock(pmd, vma);
1667 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001668 return 0;
1669 /*
1670 * For architectures like ppc64 we look at deposited pgtable
1671 * when calling pmdp_huge_get_and_clear. So do the
1672 * pgtable_trans_huge_withdraw after finishing pmdp related
1673 * operations.
1674 */
1675 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1676 tlb->fullmm);
1677 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1678 if (vma_is_dax(vma)) {
1679 spin_unlock(ptl);
1680 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001681 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001682 } else if (is_huge_zero_pmd(orig_pmd)) {
1683 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1684 atomic_long_dec(&tlb->mm->nr_ptes);
1685 spin_unlock(ptl);
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001686 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001687 } else {
1688 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001689 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001690 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1691 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1692 VM_BUG_ON_PAGE(!PageHead(page), page);
1693 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1694 atomic_long_dec(&tlb->mm->nr_ptes);
1695 spin_unlock(ptl);
1696 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001697 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001698 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001699}
1700
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001701bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001702 unsigned long new_addr, unsigned long old_end,
1703 pmd_t *old_pmd, pmd_t *new_pmd)
1704{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001705 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001706 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001707 struct mm_struct *mm = vma->vm_mm;
1708
1709 if ((old_addr & ~HPAGE_PMD_MASK) ||
1710 (new_addr & ~HPAGE_PMD_MASK) ||
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001711 old_end - old_addr < HPAGE_PMD_SIZE)
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001712 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001713
1714 /*
1715 * The destination pmd shouldn't be established, free_pgtables()
1716 * should have release it.
1717 */
1718 if (WARN_ON(!pmd_none(*new_pmd))) {
1719 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001720 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001721 }
1722
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001723 /*
1724 * We don't have to worry about the ordering of src and dst
1725 * ptlocks because exclusive mmap_sem prevents deadlock.
1726 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001727 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1728 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001729 new_ptl = pmd_lockptr(mm, new_pmd);
1730 if (new_ptl != old_ptl)
1731 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001732 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001733 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001734
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001735 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1736 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301737 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001738 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1739 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001740 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301741 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1742 if (new_ptl != old_ptl)
1743 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001744 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001745 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001746 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001747 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001748}
1749
Mel Gormanf123d742013-10-07 11:28:49 +01001750/*
1751 * Returns
1752 * - 0 if PMD could not be locked
1753 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1754 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1755 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001756int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001757 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001758{
1759 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001760 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001761 int ret = 0;
1762
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001763 ptl = __pmd_trans_huge_lock(pmd, vma);
1764 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001765 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001766 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001767 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001768
1769 /*
1770 * Avoid trapping faults against the zero page. The read-only
1771 * data is likely to be read-cached on the local CPU and
1772 * local/remote hits to the zero page are not interesting.
1773 */
1774 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1775 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001776 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001777 }
1778
Mel Gorman10c10452015-02-12 14:58:44 -08001779 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001780 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001781 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001782 if (preserve_write)
1783 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001784 ret = HPAGE_PMD_NR;
1785 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001786 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001787 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001788 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001789 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001790
1791 return ret;
1792}
1793
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001794/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001795 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001796 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001797 * Note that if it returns true, this routine returns without unlocking page
1798 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001799 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001800spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001801{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001802 spinlock_t *ptl;
1803 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001804 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001805 return ptl;
1806 spin_unlock(ptl);
1807 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001808}
1809
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001810#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001811
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001812int hugepage_madvise(struct vm_area_struct *vma,
1813 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001814{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001815 switch (advice) {
1816 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001817#ifdef CONFIG_S390
1818 /*
1819 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1820 * can't handle this properly after s390_enable_sie, so we simply
1821 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1822 */
1823 if (mm_has_pgste(vma->vm_mm))
1824 return 0;
1825#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001826 /*
1827 * Be somewhat over-protective like KSM for now!
1828 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001829 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001830 return -EINVAL;
1831 *vm_flags &= ~VM_NOHUGEPAGE;
1832 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001833 /*
1834 * If the vma become good for khugepaged to scan,
1835 * register it here without waiting a page fault that
1836 * may not happen any time soon.
1837 */
David Rientjes6d50e602014-10-29 14:50:31 -07001838 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001839 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001840 break;
1841 case MADV_NOHUGEPAGE:
1842 /*
1843 * Be somewhat over-protective like KSM for now!
1844 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001845 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001846 return -EINVAL;
1847 *vm_flags &= ~VM_HUGEPAGE;
1848 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001849 /*
1850 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1851 * this vma even if we leave the mm registered in khugepaged if
1852 * it got registered before VM_NOHUGEPAGE was set.
1853 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001854 break;
1855 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001856
1857 return 0;
1858}
1859
Andrea Arcangeliba761492011-01-13 15:46:58 -08001860static int __init khugepaged_slab_init(void)
1861{
1862 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1863 sizeof(struct mm_slot),
1864 __alignof__(struct mm_slot), 0, NULL);
1865 if (!mm_slot_cache)
1866 return -ENOMEM;
1867
1868 return 0;
1869}
1870
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001871static void __init khugepaged_slab_exit(void)
1872{
1873 kmem_cache_destroy(mm_slot_cache);
1874}
1875
Andrea Arcangeliba761492011-01-13 15:46:58 -08001876static inline struct mm_slot *alloc_mm_slot(void)
1877{
1878 if (!mm_slot_cache) /* initialization failed */
1879 return NULL;
1880 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1881}
1882
1883static inline void free_mm_slot(struct mm_slot *mm_slot)
1884{
1885 kmem_cache_free(mm_slot_cache, mm_slot);
1886}
1887
Andrea Arcangeliba761492011-01-13 15:46:58 -08001888static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1889{
1890 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001891
Sasha Levinb67bfe02013-02-27 17:06:00 -08001892 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001893 if (mm == mm_slot->mm)
1894 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001895
Andrea Arcangeliba761492011-01-13 15:46:58 -08001896 return NULL;
1897}
1898
1899static void insert_to_mm_slots_hash(struct mm_struct *mm,
1900 struct mm_slot *mm_slot)
1901{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001902 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001903 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001904}
1905
1906static inline int khugepaged_test_exit(struct mm_struct *mm)
1907{
1908 return atomic_read(&mm->mm_users) == 0;
1909}
1910
1911int __khugepaged_enter(struct mm_struct *mm)
1912{
1913 struct mm_slot *mm_slot;
1914 int wakeup;
1915
1916 mm_slot = alloc_mm_slot();
1917 if (!mm_slot)
1918 return -ENOMEM;
1919
1920 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001921 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001922 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1923 free_mm_slot(mm_slot);
1924 return 0;
1925 }
1926
1927 spin_lock(&khugepaged_mm_lock);
1928 insert_to_mm_slots_hash(mm, mm_slot);
1929 /*
1930 * Insert just behind the scanning cursor, to let the area settle
1931 * down a little.
1932 */
1933 wakeup = list_empty(&khugepaged_scan.mm_head);
1934 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1935 spin_unlock(&khugepaged_mm_lock);
1936
1937 atomic_inc(&mm->mm_count);
1938 if (wakeup)
1939 wake_up_interruptible(&khugepaged_wait);
1940
1941 return 0;
1942}
1943
David Rientjes6d50e602014-10-29 14:50:31 -07001944int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1945 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001946{
1947 unsigned long hstart, hend;
1948 if (!vma->anon_vma)
1949 /*
1950 * Not yet faulted in so we will register later in the
1951 * page fault if needed.
1952 */
1953 return 0;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07001954 if (vma->vm_ops || (vm_flags & VM_NO_THP))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001955 /* khugepaged not yet working on file or special mappings */
1956 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001957 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1958 hend = vma->vm_end & HPAGE_PMD_MASK;
1959 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001960 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001961 return 0;
1962}
1963
1964void __khugepaged_exit(struct mm_struct *mm)
1965{
1966 struct mm_slot *mm_slot;
1967 int free = 0;
1968
1969 spin_lock(&khugepaged_mm_lock);
1970 mm_slot = get_mm_slot(mm);
1971 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001972 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001973 list_del(&mm_slot->mm_node);
1974 free = 1;
1975 }
Chris Wrightd788e802011-07-25 17:12:14 -07001976 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001977
1978 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001979 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1980 free_mm_slot(mm_slot);
1981 mmdrop(mm);
1982 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001983 /*
1984 * This is required to serialize against
1985 * khugepaged_test_exit() (which is guaranteed to run
1986 * under mmap sem read mode). Stop here (after we
1987 * return all pagetables will be destroyed) until
1988 * khugepaged has finished working on the pagetables
1989 * under the mmap_sem.
1990 */
1991 down_write(&mm->mmap_sem);
1992 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001993 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001994}
1995
1996static void release_pte_page(struct page *page)
1997{
1998 /* 0 stands for page_is_file_cache(page) == false */
1999 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2000 unlock_page(page);
2001 putback_lru_page(page);
2002}
2003
2004static void release_pte_pages(pte_t *pte, pte_t *_pte)
2005{
2006 while (--_pte >= pte) {
2007 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002008 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002009 release_pte_page(pte_page(pteval));
2010 }
2011}
2012
Andrea Arcangeliba761492011-01-13 15:46:58 -08002013static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2014 unsigned long address,
2015 pte_t *pte)
2016{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002017 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002018 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002019 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002020 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002021
Andrea Arcangeliba761492011-01-13 15:46:58 -08002022 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2023 _pte++, address += PAGE_SIZE) {
2024 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002025 if (pte_none(pteval) || (pte_present(pteval) &&
2026 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002027 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002028 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002029 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002030 } else {
2031 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002032 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002033 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002034 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002035 if (!pte_present(pteval)) {
2036 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002037 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002038 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002039 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002040 if (unlikely(!page)) {
2041 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002042 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002043 }
Bob Liu344aa352012-12-11 16:00:34 -08002044
Sasha Levin309381fea2014-01-23 15:52:54 -08002045 VM_BUG_ON_PAGE(PageCompound(page), page);
2046 VM_BUG_ON_PAGE(!PageAnon(page), page);
2047 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048
Andrea Arcangeliba761492011-01-13 15:46:58 -08002049 /*
2050 * We can do it before isolate_lru_page because the
2051 * page can't be freed from under us. NOTE: PG_lock
2052 * is needed to serialize against split_huge_page
2053 * when invoked from the VM.
2054 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002055 if (!trylock_page(page)) {
2056 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002057 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002058 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002059
2060 /*
2061 * cannot use mapcount: can't collapse if there's a gup pin.
2062 * The page must only be referenced by the scanned process
2063 * and page swap cache.
2064 */
2065 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2066 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002067 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002068 goto out;
2069 }
2070 if (pte_write(pteval)) {
2071 writable = true;
2072 } else {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002073 if (PageSwapCache(page) &&
2074 !reuse_swap_page(page, NULL)) {
Ebru Akagunduz10359212015-02-11 15:28:28 -08002075 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002076 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002077 goto out;
2078 }
2079 /*
2080 * Page is not in the swap cache. It can be collapsed
2081 * into a THP.
2082 */
2083 }
2084
Andrea Arcangeliba761492011-01-13 15:46:58 -08002085 /*
2086 * Isolate the page to avoid collapsing an hugepage
2087 * currently in use by the VM.
2088 */
2089 if (isolate_lru_page(page)) {
2090 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002091 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002092 goto out;
2093 }
2094 /* 0 stands for page_is_file_cache(page) == false */
2095 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002096 VM_BUG_ON_PAGE(!PageLocked(page), page);
2097 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002098
2099 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002100 if (pte_young(pteval) ||
2101 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002102 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002103 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002104 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002105 if (likely(writable)) {
2106 if (likely(referenced)) {
2107 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002108 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002109 referenced, writable, result);
2110 return 1;
2111 }
2112 } else {
2113 result = SCAN_PAGE_RO;
2114 }
2115
Andrea Arcangeliba761492011-01-13 15:46:58 -08002116out:
Bob Liu344aa352012-12-11 16:00:34 -08002117 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002118 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002119 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002120 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002121}
2122
2123static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2124 struct vm_area_struct *vma,
2125 unsigned long address,
2126 spinlock_t *ptl)
2127{
2128 pte_t *_pte;
2129 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2130 pte_t pteval = *_pte;
2131 struct page *src_page;
2132
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002133 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002134 clear_user_highpage(page, address);
2135 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002136 if (is_zero_pfn(pte_pfn(pteval))) {
2137 /*
2138 * ptl mostly unnecessary.
2139 */
2140 spin_lock(ptl);
2141 /*
2142 * paravirt calls inside pte_clear here are
2143 * superfluous.
2144 */
2145 pte_clear(vma->vm_mm, address, _pte);
2146 spin_unlock(ptl);
2147 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002148 } else {
2149 src_page = pte_page(pteval);
2150 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002151 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002152 release_pte_page(src_page);
2153 /*
2154 * ptl mostly unnecessary, but preempt has to
2155 * be disabled to update the per-cpu stats
2156 * inside page_remove_rmap().
2157 */
2158 spin_lock(ptl);
2159 /*
2160 * paravirt calls inside pte_clear here are
2161 * superfluous.
2162 */
2163 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002164 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002165 spin_unlock(ptl);
2166 free_page_and_swap_cache(src_page);
2167 }
2168
2169 address += PAGE_SIZE;
2170 page++;
2171 }
2172}
2173
Xiao Guangrong26234f32012-10-08 16:29:51 -07002174static void khugepaged_alloc_sleep(void)
2175{
Petr Mladekbde43c62015-09-08 15:04:05 -07002176 DEFINE_WAIT(wait);
2177
2178 add_wait_queue(&khugepaged_wait, &wait);
2179 freezable_schedule_timeout_interruptible(
2180 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2181 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002182}
2183
Bob Liu9f1b8682013-11-12 15:07:37 -08002184static int khugepaged_node_load[MAX_NUMNODES];
2185
David Rientjes14a4e212014-08-06 16:07:29 -07002186static bool khugepaged_scan_abort(int nid)
2187{
2188 int i;
2189
2190 /*
2191 * If zone_reclaim_mode is disabled, then no extra effort is made to
2192 * allocate memory locally.
2193 */
2194 if (!zone_reclaim_mode)
2195 return false;
2196
2197 /* If there is a count for this node already, it must be acceptable */
2198 if (khugepaged_node_load[nid])
2199 return false;
2200
2201 for (i = 0; i < MAX_NUMNODES; i++) {
2202 if (!khugepaged_node_load[i])
2203 continue;
2204 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2205 return true;
2206 }
2207 return false;
2208}
2209
Xiao Guangrong26234f32012-10-08 16:29:51 -07002210#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002211static int khugepaged_find_target_node(void)
2212{
2213 static int last_khugepaged_target_node = NUMA_NO_NODE;
2214 int nid, target_node = 0, max_value = 0;
2215
2216 /* find first node with max normal pages hit */
2217 for (nid = 0; nid < MAX_NUMNODES; nid++)
2218 if (khugepaged_node_load[nid] > max_value) {
2219 max_value = khugepaged_node_load[nid];
2220 target_node = nid;
2221 }
2222
2223 /* do some balance if several nodes have the same hit record */
2224 if (target_node <= last_khugepaged_target_node)
2225 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2226 nid++)
2227 if (max_value == khugepaged_node_load[nid]) {
2228 target_node = nid;
2229 break;
2230 }
2231
2232 last_khugepaged_target_node = target_node;
2233 return target_node;
2234}
2235
Xiao Guangrong26234f32012-10-08 16:29:51 -07002236static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2237{
2238 if (IS_ERR(*hpage)) {
2239 if (!*wait)
2240 return false;
2241
2242 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002243 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002244 khugepaged_alloc_sleep();
2245 } else if (*hpage) {
2246 put_page(*hpage);
2247 *hpage = NULL;
2248 }
2249
2250 return true;
2251}
2252
Michal Hocko3b363692015-04-15 16:13:29 -07002253static struct page *
2254khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002255 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002256{
Sasha Levin309381fea2014-01-23 15:52:54 -08002257 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002258
Xiao Guangrong26234f32012-10-08 16:29:51 -07002259 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002260 * Before allocating the hugepage, release the mmap_sem read lock.
2261 * The allocation can take potentially a long time if it involves
2262 * sync compaction, and we do not need to hold the mmap_sem during
2263 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002264 */
2265 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002266
Vlastimil Babka96db8002015-09-08 15:03:50 -07002267 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002268 if (unlikely(!*hpage)) {
2269 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2270 *hpage = ERR_PTR(-ENOMEM);
2271 return NULL;
2272 }
2273
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002274 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002275 count_vm_event(THP_COLLAPSE_ALLOC);
2276 return *hpage;
2277}
2278#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002279static int khugepaged_find_target_node(void)
2280{
2281 return 0;
2282}
2283
Mel Gorman444eb2a42016-03-17 14:19:23 -07002284static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002285{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002286 struct page *page;
2287
Mel Gorman444eb2a42016-03-17 14:19:23 -07002288 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2289 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002290 if (page)
2291 prep_transhuge_page(page);
2292 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002293}
2294
Xiao Guangrong26234f32012-10-08 16:29:51 -07002295static struct page *khugepaged_alloc_hugepage(bool *wait)
2296{
2297 struct page *hpage;
2298
2299 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002300 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002301 if (!hpage) {
2302 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2303 if (!*wait)
2304 return NULL;
2305
2306 *wait = false;
2307 khugepaged_alloc_sleep();
2308 } else
2309 count_vm_event(THP_COLLAPSE_ALLOC);
2310 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2311
2312 return hpage;
2313}
2314
2315static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2316{
2317 if (!*hpage)
2318 *hpage = khugepaged_alloc_hugepage(wait);
2319
2320 if (unlikely(!*hpage))
2321 return false;
2322
2323 return true;
2324}
2325
Michal Hocko3b363692015-04-15 16:13:29 -07002326static struct page *
2327khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002328 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002329{
2330 up_read(&mm->mmap_sem);
2331 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002332
Xiao Guangrong26234f32012-10-08 16:29:51 -07002333 return *hpage;
2334}
2335#endif
2336
Bob Liufa475e52012-12-11 16:00:39 -08002337static bool hugepage_vma_check(struct vm_area_struct *vma)
2338{
2339 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2340 (vma->vm_flags & VM_NOHUGEPAGE))
2341 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002342 if (!vma->anon_vma || vma->vm_ops)
2343 return false;
2344 if (is_vma_temporary_stack(vma))
2345 return false;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07002346 return !(vma->vm_flags & VM_NO_THP);
Bob Liufa475e52012-12-11 16:00:39 -08002347}
2348
Andrea Arcangeliba761492011-01-13 15:46:58 -08002349static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002350 unsigned long address,
2351 struct page **hpage,
2352 struct vm_area_struct *vma,
2353 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002354{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002355 pmd_t *pmd, _pmd;
2356 pte_t *pte;
2357 pgtable_t pgtable;
2358 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002359 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002360 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002361 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002362 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002363 unsigned long mmun_start; /* For mmu_notifiers */
2364 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002365 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002366
2367 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002368
Michal Hocko3b363692015-04-15 16:13:29 -07002369 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002370 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b363692015-04-15 16:13:29 -07002371
Xiao Guangrong26234f32012-10-08 16:29:51 -07002372 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002373 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002374 if (!new_page) {
2375 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2376 goto out_nolock;
2377 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002378
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002379 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002380 result = SCAN_CGROUP_CHARGE_FAIL;
2381 goto out_nolock;
2382 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002383
2384 /*
2385 * Prevent all access to pagetables with the exception of
2386 * gup_fast later hanlded by the ptep_clear_flush and the VM
2387 * handled by the anon_vma lock + PG_lock.
2388 */
2389 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002390 if (unlikely(khugepaged_test_exit(mm))) {
2391 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002392 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002393 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002394
2395 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002396 if (!vma) {
2397 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002398 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002399 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002400 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2401 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002402 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2403 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002404 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002405 }
2406 if (!hugepage_vma_check(vma)) {
2407 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002408 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002409 }
Bob Liu62190492012-12-11 16:00:37 -08002410 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002411 if (!pmd) {
2412 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002413 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002414 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002415
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002416 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002417
2418 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002419 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002420
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002421 mmun_start = address;
2422 mmun_end = address + HPAGE_PMD_SIZE;
2423 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002424 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002425 /*
2426 * After this gup_fast can't run anymore. This also removes
2427 * any huge TLB entry from the CPU so we won't allow
2428 * huge and small TLB entries for the same virtual address
2429 * to avoid the risk of CPU bugs in that area.
2430 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002431 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002432 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002433 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002434
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002435 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002436 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002437 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002438
2439 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002440 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002441 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002442 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002443 /*
2444 * We can only use set_pmd_at when establishing
2445 * hugepmds and never for establishing regular pmds that
2446 * points to regular pagetables. Use pmd_populate for that
2447 */
2448 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002449 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002450 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002451 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002452 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002453 }
2454
2455 /*
2456 * All pages are isolated and locked so anon_vma rmap
2457 * can't run anymore.
2458 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002459 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002460
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002461 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002462 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002463 __SetPageUptodate(new_page);
2464 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002466 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2467 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002468
2469 /*
2470 * spin_lock() below is not the equivalent of smp_wmb(), so
2471 * this is needed to avoid the copy_huge_page writes to become
2472 * visible after the set_pmd_at() write.
2473 */
2474 smp_wmb();
2475
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002476 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002477 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002478 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002479 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002480 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002481 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002482 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002483 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002484 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002485
2486 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002487
Andrea Arcangeliba761492011-01-13 15:46:58 -08002488 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002489 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002490out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002491 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002492 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002493 return;
2494
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002495out_nolock:
2496 trace_mm_collapse_huge_page(mm, isolated, result);
2497 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002498out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002499 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002500 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002501}
2502
2503static int khugepaged_scan_pmd(struct mm_struct *mm,
2504 struct vm_area_struct *vma,
2505 unsigned long address,
2506 struct page **hpage)
2507{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002508 pmd_t *pmd;
2509 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002510 int ret = 0, none_or_zero = 0, result = 0;
2511 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002512 unsigned long _address;
2513 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002514 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002515 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002516
2517 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2518
Bob Liu62190492012-12-11 16:00:37 -08002519 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002520 if (!pmd) {
2521 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002522 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002523 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002524
Bob Liu9f1b8682013-11-12 15:07:37 -08002525 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002526 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2527 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2528 _pte++, _address += PAGE_SIZE) {
2529 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002530 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002531 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002532 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002533 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002534 } else {
2535 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002536 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002537 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002538 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002539 if (!pte_present(pteval)) {
2540 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002541 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002542 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002543 if (pte_write(pteval))
2544 writable = true;
2545
Andrea Arcangeliba761492011-01-13 15:46:58 -08002546 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002547 if (unlikely(!page)) {
2548 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002549 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002550 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002551
2552 /* TODO: teach khugepaged to collapse THP mapped with pte */
2553 if (PageCompound(page)) {
2554 result = SCAN_PAGE_COMPOUND;
2555 goto out_unmap;
2556 }
2557
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002558 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002559 * Record which node the original page is from and save this
2560 * information to khugepaged_node_load[].
2561 * Khupaged will allocate hugepage from the node has the max
2562 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002563 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002564 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002565 if (khugepaged_scan_abort(node)) {
2566 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002567 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002568 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002569 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002570 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002571 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002572 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002573 }
2574 if (PageLocked(page)) {
2575 result = SCAN_PAGE_LOCK;
2576 goto out_unmap;
2577 }
2578 if (!PageAnon(page)) {
2579 result = SCAN_PAGE_ANON;
2580 goto out_unmap;
2581 }
2582
Ebru Akagunduz10359212015-02-11 15:28:28 -08002583 /*
2584 * cannot use mapcount: can't collapse if there's a gup pin.
2585 * The page must only be referenced by the scanned process
2586 * and page swap cache.
2587 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002588 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2589 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002590 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002591 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002592 if (pte_young(pteval) ||
2593 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002594 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002595 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002596 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002597 if (writable) {
2598 if (referenced) {
2599 result = SCAN_SUCCEED;
2600 ret = 1;
2601 } else {
2602 result = SCAN_NO_REFERENCED_PAGE;
2603 }
2604 } else {
2605 result = SCAN_PAGE_RO;
2606 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607out_unmap:
2608 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002609 if (ret) {
2610 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002611 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002612 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002613 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002614out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002615 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002616 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002617 return ret;
2618}
2619
2620static void collect_mm_slot(struct mm_slot *mm_slot)
2621{
2622 struct mm_struct *mm = mm_slot->mm;
2623
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002624 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002625
2626 if (khugepaged_test_exit(mm)) {
2627 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002628 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002629 list_del(&mm_slot->mm_node);
2630
2631 /*
2632 * Not strictly needed because the mm exited already.
2633 *
2634 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2635 */
2636
2637 /* khugepaged_mm_lock actually not necessary for the below */
2638 free_mm_slot(mm_slot);
2639 mmdrop(mm);
2640 }
2641}
2642
2643static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2644 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002645 __releases(&khugepaged_mm_lock)
2646 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002647{
2648 struct mm_slot *mm_slot;
2649 struct mm_struct *mm;
2650 struct vm_area_struct *vma;
2651 int progress = 0;
2652
2653 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002654 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002655
2656 if (khugepaged_scan.mm_slot)
2657 mm_slot = khugepaged_scan.mm_slot;
2658 else {
2659 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2660 struct mm_slot, mm_node);
2661 khugepaged_scan.address = 0;
2662 khugepaged_scan.mm_slot = mm_slot;
2663 }
2664 spin_unlock(&khugepaged_mm_lock);
2665
2666 mm = mm_slot->mm;
2667 down_read(&mm->mmap_sem);
2668 if (unlikely(khugepaged_test_exit(mm)))
2669 vma = NULL;
2670 else
2671 vma = find_vma(mm, khugepaged_scan.address);
2672
2673 progress++;
2674 for (; vma; vma = vma->vm_next) {
2675 unsigned long hstart, hend;
2676
2677 cond_resched();
2678 if (unlikely(khugepaged_test_exit(mm))) {
2679 progress++;
2680 break;
2681 }
Bob Liufa475e52012-12-11 16:00:39 -08002682 if (!hugepage_vma_check(vma)) {
2683skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002684 progress++;
2685 continue;
2686 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002687 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2688 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002689 if (hstart >= hend)
2690 goto skip;
2691 if (khugepaged_scan.address > hend)
2692 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002693 if (khugepaged_scan.address < hstart)
2694 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002695 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002696
2697 while (khugepaged_scan.address < hend) {
2698 int ret;
2699 cond_resched();
2700 if (unlikely(khugepaged_test_exit(mm)))
2701 goto breakouterloop;
2702
2703 VM_BUG_ON(khugepaged_scan.address < hstart ||
2704 khugepaged_scan.address + HPAGE_PMD_SIZE >
2705 hend);
2706 ret = khugepaged_scan_pmd(mm, vma,
2707 khugepaged_scan.address,
2708 hpage);
2709 /* move to next address */
2710 khugepaged_scan.address += HPAGE_PMD_SIZE;
2711 progress += HPAGE_PMD_NR;
2712 if (ret)
2713 /* we released mmap_sem so break loop */
2714 goto breakouterloop_mmap_sem;
2715 if (progress >= pages)
2716 goto breakouterloop;
2717 }
2718 }
2719breakouterloop:
2720 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2721breakouterloop_mmap_sem:
2722
2723 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002724 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002725 /*
2726 * Release the current mm_slot if this mm is about to die, or
2727 * if we scanned all vmas of this mm.
2728 */
2729 if (khugepaged_test_exit(mm) || !vma) {
2730 /*
2731 * Make sure that if mm_users is reaching zero while
2732 * khugepaged runs here, khugepaged_exit will find
2733 * mm_slot not pointing to the exiting mm.
2734 */
2735 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2736 khugepaged_scan.mm_slot = list_entry(
2737 mm_slot->mm_node.next,
2738 struct mm_slot, mm_node);
2739 khugepaged_scan.address = 0;
2740 } else {
2741 khugepaged_scan.mm_slot = NULL;
2742 khugepaged_full_scans++;
2743 }
2744
2745 collect_mm_slot(mm_slot);
2746 }
2747
2748 return progress;
2749}
2750
2751static int khugepaged_has_work(void)
2752{
2753 return !list_empty(&khugepaged_scan.mm_head) &&
2754 khugepaged_enabled();
2755}
2756
2757static int khugepaged_wait_event(void)
2758{
2759 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002760 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002761}
2762
Xiao Guangrongd5169042012-10-08 16:29:48 -07002763static void khugepaged_do_scan(void)
2764{
2765 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002766 unsigned int progress = 0, pass_through_head = 0;
2767 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002768 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002769
2770 barrier(); /* write khugepaged_pages_to_scan to local stack */
2771
2772 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002773 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002774 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002775
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002776 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002777
Jiri Kosinacd092412015-06-24 16:56:07 -07002778 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002779 break;
2780
Andrea Arcangeliba761492011-01-13 15:46:58 -08002781 spin_lock(&khugepaged_mm_lock);
2782 if (!khugepaged_scan.mm_slot)
2783 pass_through_head++;
2784 if (khugepaged_has_work() &&
2785 pass_through_head < 2)
2786 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002787 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002788 else
2789 progress = pages;
2790 spin_unlock(&khugepaged_mm_lock);
2791 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002792
Xiao Guangrongd5169042012-10-08 16:29:48 -07002793 if (!IS_ERR_OR_NULL(hpage))
2794 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002795}
2796
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002797static void khugepaged_wait_work(void)
2798{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002799 if (khugepaged_has_work()) {
2800 if (!khugepaged_scan_sleep_millisecs)
2801 return;
2802
2803 wait_event_freezable_timeout(khugepaged_wait,
2804 kthread_should_stop(),
2805 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2806 return;
2807 }
2808
2809 if (khugepaged_enabled())
2810 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2811}
2812
Andrea Arcangeliba761492011-01-13 15:46:58 -08002813static int khugepaged(void *none)
2814{
2815 struct mm_slot *mm_slot;
2816
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002817 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002818 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002819
Xiao Guangrongb7231782012-10-08 16:29:54 -07002820 while (!kthread_should_stop()) {
2821 khugepaged_do_scan();
2822 khugepaged_wait_work();
2823 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002824
2825 spin_lock(&khugepaged_mm_lock);
2826 mm_slot = khugepaged_scan.mm_slot;
2827 khugepaged_scan.mm_slot = NULL;
2828 if (mm_slot)
2829 collect_mm_slot(mm_slot);
2830 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002831 return 0;
2832}
2833
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002834static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2835 unsigned long haddr, pmd_t *pmd)
2836{
2837 struct mm_struct *mm = vma->vm_mm;
2838 pgtable_t pgtable;
2839 pmd_t _pmd;
2840 int i;
2841
2842 /* leave pmd empty until pte is filled */
2843 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2844
2845 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2846 pmd_populate(mm, &_pmd, pgtable);
2847
2848 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2849 pte_t *pte, entry;
2850 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2851 entry = pte_mkspecial(entry);
2852 pte = pte_offset_map(&_pmd, haddr);
2853 VM_BUG_ON(!pte_none(*pte));
2854 set_pte_at(mm, haddr, pte, entry);
2855 pte_unmap(pte);
2856 }
2857 smp_wmb(); /* make pte visible before pmd */
2858 pmd_populate(mm, pmd, pgtable);
2859 put_huge_zero_page();
2860}
2861
2862static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002863 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002864{
2865 struct mm_struct *mm = vma->vm_mm;
2866 struct page *page;
2867 pgtable_t pgtable;
2868 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002869 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002870 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002871 int i;
2872
2873 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2874 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2875 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002876 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002877
2878 count_vm_event(THP_SPLIT_PMD);
2879
2880 if (vma_is_dax(vma)) {
2881 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2882 if (is_huge_zero_pmd(_pmd))
2883 put_huge_zero_page();
2884 return;
2885 } else if (is_huge_zero_pmd(*pmd)) {
2886 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2887 }
2888
2889 page = pmd_page(*pmd);
2890 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002891 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002892 write = pmd_write(*pmd);
2893 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002894 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002895
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05302896 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002897 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2898 pmd_populate(mm, &_pmd, pgtable);
2899
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002900 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002901 pte_t entry, *pte;
2902 /*
2903 * Note that NUMA hinting access restrictions are not
2904 * transferred to avoid any possibility of altering
2905 * permissions across VMAs.
2906 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002907 if (freeze) {
2908 swp_entry_t swp_entry;
2909 swp_entry = make_migration_entry(page + i, write);
2910 entry = swp_entry_to_pte(swp_entry);
2911 } else {
2912 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002913 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002914 if (!write)
2915 entry = pte_wrprotect(entry);
2916 if (!young)
2917 entry = pte_mkold(entry);
2918 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002919 if (dirty)
2920 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002921 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002922 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002923 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002924 atomic_inc(&page[i]._mapcount);
2925 pte_unmap(pte);
2926 }
2927
2928 /*
2929 * Set PG_double_map before dropping compound_mapcount to avoid
2930 * false-negative page_mapped().
2931 */
2932 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2933 for (i = 0; i < HPAGE_PMD_NR; i++)
2934 atomic_inc(&page[i]._mapcount);
2935 }
2936
2937 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2938 /* Last compound_mapcount is gone. */
2939 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2940 if (TestClearPageDoubleMap(page)) {
2941 /* No need in mapcount reference anymore */
2942 for (i = 0; i < HPAGE_PMD_NR; i++)
2943 atomic_dec(&page[i]._mapcount);
2944 }
2945 }
2946
2947 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002948 /*
2949 * Up to this point the pmd is present and huge and userland has the
2950 * whole access to the hugepage during the split (which happens in
2951 * place). If we overwrite the pmd with the not-huge version pointing
2952 * to the pte here (which of course we could if all CPUs were bug
2953 * free), userland could trigger a small page size TLB miss on the
2954 * small sized TLB while the hugepage TLB entry is still established in
2955 * the huge TLB. Some CPU doesn't like that.
2956 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2957 * 383 on page 93. Intel should be safe but is also warns that it's
2958 * only safe if the permission and cache attributes of the two entries
2959 * loaded in the two TLB is identical (which should be the case here).
2960 * But it is generally safer to never allow small and huge TLB entries
2961 * for the same virtual address to be loaded simultaneously. So instead
2962 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2963 * current pmd notpresent (atomically because here the pmd_trans_huge
2964 * and pmd_trans_splitting must remain set at all times on the pmd
2965 * until the split is complete for this pmd), then we flush the SMP TLB
2966 * and finally we write the non-huge version of the pmd entry with
2967 * pmd_populate.
2968 */
2969 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002970 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002971
2972 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002973 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002974 page_remove_rmap(page + i, false);
2975 put_page(page + i);
2976 }
2977 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002978}
2979
2980void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002981 unsigned long address, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002982{
2983 spinlock_t *ptl;
2984 struct mm_struct *mm = vma->vm_mm;
2985 unsigned long haddr = address & HPAGE_PMD_MASK;
2986
2987 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2988 ptl = pmd_lock(mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08002989 if (pmd_trans_huge(*pmd)) {
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07002990 struct page *page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08002991 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07002992 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08002993 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002994 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002995 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002996out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002997 spin_unlock(ptl);
2998 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
2999}
3000
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003001void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3002 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003003{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003004 pgd_t *pgd;
3005 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003006 pmd_t *pmd;
3007
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003008 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003009 if (!pgd_present(*pgd))
3010 return;
3011
3012 pud = pud_offset(pgd, address);
3013 if (!pud_present(*pud))
3014 return;
3015
3016 pmd = pmd_offset(pud, address);
Dan Williams5c7fb562016-01-15 16:56:52 -08003017 if (!pmd_present(*pmd) || (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003018 return;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003019
3020 /*
3021 * If caller asks to setup a migration entries, we need a page to check
3022 * pmd against. Otherwise we can end up replacing wrong page.
3023 */
3024 VM_BUG_ON(freeze && !page);
3025 if (page && page != pmd_page(*pmd))
3026 return;
3027
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003028 /*
3029 * Caller holds the mmap_sem write mode, so a huge pmd cannot
3030 * materialize from under us.
3031 */
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003032 __split_huge_pmd(vma, pmd, address, freeze);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003033}
3034
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003035void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003036 unsigned long start,
3037 unsigned long end,
3038 long adjust_next)
3039{
3040 /*
3041 * If the new start address isn't hpage aligned and it could
3042 * previously contain an hugepage: check if we need to split
3043 * an huge pmd.
3044 */
3045 if (start & ~HPAGE_PMD_MASK &&
3046 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3047 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003048 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003049
3050 /*
3051 * If the new end address isn't hpage aligned and it could
3052 * previously contain an hugepage: check if we need to split
3053 * an huge pmd.
3054 */
3055 if (end & ~HPAGE_PMD_MASK &&
3056 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3057 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003058 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003059
3060 /*
3061 * If we're also updating the vma->vm_next->vm_start, if the new
3062 * vm_next->vm_start isn't page aligned and it could previously
3063 * contain an hugepage: check if we need to split an huge pmd.
3064 */
3065 if (adjust_next > 0) {
3066 struct vm_area_struct *next = vma->vm_next;
3067 unsigned long nstart = next->vm_start;
3068 nstart += adjust_next << PAGE_SHIFT;
3069 if (nstart & ~HPAGE_PMD_MASK &&
3070 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3071 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003072 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003073 }
3074}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003075
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003076static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003077{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003078 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3079 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3080 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003081
3082 VM_BUG_ON_PAGE(!PageHead(page), page);
3083
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003084 /* We only need TTU_SPLIT_HUGE_PMD once */
3085 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3086 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3087 /* Cut short if the page is unmapped */
3088 if (page_count(page) == 1)
3089 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003090
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003091 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003092 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003093 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003094}
3095
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003096static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003097{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003098 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003099
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003100 for (i = 0; i < HPAGE_PMD_NR; i++)
3101 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003102}
3103
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003104static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003105 struct lruvec *lruvec, struct list_head *list)
3106{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003107 struct page *page_tail = head + tail;
3108
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003109 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003110 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003111
3112 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003113 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003114 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003115 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003116 * would then run atomic_set() concurrently with
3117 * get_page_unless_zero(), and atomic_set() is implemented in C not
3118 * using locked ops. spin_unlock on x86 sometime uses locked ops
3119 * because of PPro errata 66, 92, so unless somebody can guarantee
3120 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003121 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003122 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003123 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003124
3125 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3126 page_tail->flags |= (head->flags &
3127 ((1L << PG_referenced) |
3128 (1L << PG_swapbacked) |
3129 (1L << PG_mlocked) |
3130 (1L << PG_uptodate) |
3131 (1L << PG_active) |
3132 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003133 (1L << PG_unevictable) |
3134 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003135
3136 /*
3137 * After clearing PageTail the gup refcount can be released.
3138 * Page flags also must be visible before we make the page non-compound.
3139 */
3140 smp_wmb();
3141
3142 clear_compound_head(page_tail);
3143
3144 if (page_is_young(head))
3145 set_page_young(page_tail);
3146 if (page_is_idle(head))
3147 set_page_idle(page_tail);
3148
3149 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003150 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003151 page_tail);
3152 page_tail->mapping = head->mapping;
3153
3154 page_tail->index = head->index + tail;
3155 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3156 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003157}
3158
3159static void __split_huge_page(struct page *page, struct list_head *list)
3160{
3161 struct page *head = compound_head(page);
3162 struct zone *zone = page_zone(head);
3163 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003164 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003165
3166 /* prevent PageLRU to go away from under us, and freeze lru stats */
3167 spin_lock_irq(&zone->lru_lock);
3168 lruvec = mem_cgroup_page_lruvec(head, zone);
3169
3170 /* complete memcg works before add pages to LRU */
3171 mem_cgroup_split_huge_fixup(head);
3172
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003173 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003174 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003175
3176 ClearPageCompound(head);
3177 spin_unlock_irq(&zone->lru_lock);
3178
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003179 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003180
3181 for (i = 0; i < HPAGE_PMD_NR; i++) {
3182 struct page *subpage = head + i;
3183 if (subpage == page)
3184 continue;
3185 unlock_page(subpage);
3186
3187 /*
3188 * Subpages may be freed if there wasn't any mapping
3189 * like if add_to_swap() is running on a lru page that
3190 * had its mapping zapped. And freeing these pages
3191 * requires taking the lru_lock so we do the put_page
3192 * of the tail pages after the split is complete.
3193 */
3194 put_page(subpage);
3195 }
3196}
3197
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003198int total_mapcount(struct page *page)
3199{
3200 int i, ret;
3201
3202 VM_BUG_ON_PAGE(PageTail(page), page);
3203
3204 if (likely(!PageCompound(page)))
3205 return atomic_read(&page->_mapcount) + 1;
3206
3207 ret = compound_mapcount(page);
3208 if (PageHuge(page))
3209 return ret;
3210 for (i = 0; i < HPAGE_PMD_NR; i++)
3211 ret += atomic_read(&page[i]._mapcount) + 1;
3212 if (PageDoubleMap(page))
3213 ret -= HPAGE_PMD_NR;
3214 return ret;
3215}
3216
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003217/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003218 * This calculates accurately how many mappings a transparent hugepage
3219 * has (unlike page_mapcount() which isn't fully accurate). This full
3220 * accuracy is primarily needed to know if copy-on-write faults can
3221 * reuse the page and change the mapping to read-write instead of
3222 * copying them. At the same time this returns the total_mapcount too.
3223 *
3224 * The function returns the highest mapcount any one of the subpages
3225 * has. If the return value is one, even if different processes are
3226 * mapping different subpages of the transparent hugepage, they can
3227 * all reuse it, because each process is reusing a different subpage.
3228 *
3229 * The total_mapcount is instead counting all virtual mappings of the
3230 * subpages. If the total_mapcount is equal to "one", it tells the
3231 * caller all mappings belong to the same "mm" and in turn the
3232 * anon_vma of the transparent hugepage can become the vma->anon_vma
3233 * local one as no other process may be mapping any of the subpages.
3234 *
3235 * It would be more accurate to replace page_mapcount() with
3236 * page_trans_huge_mapcount(), however we only use
3237 * page_trans_huge_mapcount() in the copy-on-write faults where we
3238 * need full accuracy to avoid breaking page pinning, because
3239 * page_trans_huge_mapcount() is slower than page_mapcount().
3240 */
3241int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3242{
3243 int i, ret, _total_mapcount, mapcount;
3244
3245 /* hugetlbfs shouldn't call it */
3246 VM_BUG_ON_PAGE(PageHuge(page), page);
3247
3248 if (likely(!PageTransCompound(page))) {
3249 mapcount = atomic_read(&page->_mapcount) + 1;
3250 if (total_mapcount)
3251 *total_mapcount = mapcount;
3252 return mapcount;
3253 }
3254
3255 page = compound_head(page);
3256
3257 _total_mapcount = ret = 0;
3258 for (i = 0; i < HPAGE_PMD_NR; i++) {
3259 mapcount = atomic_read(&page[i]._mapcount) + 1;
3260 ret = max(ret, mapcount);
3261 _total_mapcount += mapcount;
3262 }
3263 if (PageDoubleMap(page)) {
3264 ret -= 1;
3265 _total_mapcount -= HPAGE_PMD_NR;
3266 }
3267 mapcount = compound_mapcount(page);
3268 ret += mapcount;
3269 _total_mapcount += mapcount;
3270 if (total_mapcount)
3271 *total_mapcount = _total_mapcount;
3272 return ret;
3273}
3274
3275/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003276 * This function splits huge page into normal pages. @page can point to any
3277 * subpage of huge page to split. Split doesn't change the position of @page.
3278 *
3279 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3280 * The huge page must be locked.
3281 *
3282 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3283 *
3284 * Both head page and tail pages will inherit mapping, flags, and so on from
3285 * the hugepage.
3286 *
3287 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3288 * they are not mapped.
3289 *
3290 * Returns 0 if the hugepage is split successfully.
3291 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3292 * us.
3293 */
3294int split_huge_page_to_list(struct page *page, struct list_head *list)
3295{
3296 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003297 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003298 struct anon_vma *anon_vma;
3299 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003300 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003301 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003302
3303 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3304 VM_BUG_ON_PAGE(!PageAnon(page), page);
3305 VM_BUG_ON_PAGE(!PageLocked(page), page);
3306 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3307 VM_BUG_ON_PAGE(!PageCompound(page), page);
3308
3309 /*
3310 * The caller does not necessarily hold an mmap_sem that would prevent
3311 * the anon_vma disappearing so we first we take a reference to it
3312 * and then lock the anon_vma for write. This is similar to
3313 * page_lock_anon_vma_read except the write lock is taken to serialise
3314 * against parallel split or collapse operations.
3315 */
3316 anon_vma = page_get_anon_vma(head);
3317 if (!anon_vma) {
3318 ret = -EBUSY;
3319 goto out;
3320 }
3321 anon_vma_lock_write(anon_vma);
3322
3323 /*
3324 * Racy check if we can split the page, before freeze_page() will
3325 * split PMDs
3326 */
3327 if (total_mapcount(head) != page_count(head) - 1) {
3328 ret = -EBUSY;
3329 goto out_unlock;
3330 }
3331
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003332 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003333 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003334 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3335
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003336 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3337 if (mlocked)
3338 lru_add_drain();
3339
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003340 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003341 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003342 count = page_count(head);
3343 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003344 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003345 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003346 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003347 list_del(page_deferred_list(head));
3348 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003349 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003350 __split_huge_page(page, list);
3351 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003352 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003353 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003354 pr_alert("total_mapcount: %u, page_count(): %u\n",
3355 mapcount, count);
3356 if (PageTail(page))
3357 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003358 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003359 BUG();
3360 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003361 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003362 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003363 ret = -EBUSY;
3364 }
3365
3366out_unlock:
3367 anon_vma_unlock_write(anon_vma);
3368 put_anon_vma(anon_vma);
3369out:
3370 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3371 return ret;
3372}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003373
3374void free_transhuge_page(struct page *page)
3375{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003376 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003377 unsigned long flags;
3378
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003379 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003380 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003381 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003382 list_del(page_deferred_list(page));
3383 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003384 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003385 free_compound_page(page);
3386}
3387
3388void deferred_split_huge_page(struct page *page)
3389{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003390 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003391 unsigned long flags;
3392
3393 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3394
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003395 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003396 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003397 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003398 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3399 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003400 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003401 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003402}
3403
3404static unsigned long deferred_split_count(struct shrinker *shrink,
3405 struct shrink_control *sc)
3406{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003407 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003408 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003409}
3410
3411static unsigned long deferred_split_scan(struct shrinker *shrink,
3412 struct shrink_control *sc)
3413{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003414 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003415 unsigned long flags;
3416 LIST_HEAD(list), *pos, *next;
3417 struct page *page;
3418 int split = 0;
3419
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003420 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003421 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003422 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003423 page = list_entry((void *)pos, struct page, mapping);
3424 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003425 if (get_page_unless_zero(page)) {
3426 list_move(page_deferred_list(page), &list);
3427 } else {
3428 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003429 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003430 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003431 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003432 if (!--sc->nr_to_scan)
3433 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003434 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003435 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003436
3437 list_for_each_safe(pos, next, &list) {
3438 page = list_entry((void *)pos, struct page, mapping);
3439 lock_page(page);
3440 /* split_huge_page() removes page from list on success */
3441 if (!split_huge_page(page))
3442 split++;
3443 unlock_page(page);
3444 put_page(page);
3445 }
3446
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003447 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3448 list_splice_tail(&list, &pgdata->split_queue);
3449 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003450
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003451 /*
3452 * Stop shrinker if we didn't split any page, but the queue is empty.
3453 * This can happen if pages were freed under us.
3454 */
3455 if (!split && list_empty(&pgdata->split_queue))
3456 return SHRINK_STOP;
3457 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003458}
3459
3460static struct shrinker deferred_split_shrinker = {
3461 .count_objects = deferred_split_count,
3462 .scan_objects = deferred_split_scan,
3463 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003464 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003465};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003466
3467#ifdef CONFIG_DEBUG_FS
3468static int split_huge_pages_set(void *data, u64 val)
3469{
3470 struct zone *zone;
3471 struct page *page;
3472 unsigned long pfn, max_zone_pfn;
3473 unsigned long total = 0, split = 0;
3474
3475 if (val != 1)
3476 return -EINVAL;
3477
3478 for_each_populated_zone(zone) {
3479 max_zone_pfn = zone_end_pfn(zone);
3480 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3481 if (!pfn_valid(pfn))
3482 continue;
3483
3484 page = pfn_to_page(pfn);
3485 if (!get_page_unless_zero(page))
3486 continue;
3487
3488 if (zone != page_zone(page))
3489 goto next;
3490
3491 if (!PageHead(page) || !PageAnon(page) ||
3492 PageHuge(page))
3493 goto next;
3494
3495 total++;
3496 lock_page(page);
3497 if (!split_huge_page(page))
3498 split++;
3499 unlock_page(page);
3500next:
3501 put_page(page);
3502 }
3503 }
3504
Yang Shi145bdaa2016-05-05 16:22:00 -07003505 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003506
3507 return 0;
3508}
3509DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3510 "%llu\n");
3511
3512static int __init split_huge_pages_debugfs(void)
3513{
3514 void *ret;
3515
Yang Shi145bdaa2016-05-05 16:22:00 -07003516 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003517 &split_huge_pages_fops);
3518 if (!ret)
3519 pr_warn("Failed to create split_huge_pages in debugfs");
3520 return 0;
3521}
3522late_initcall(split_huge_pages_debugfs);
3523#endif