blob: 9ed58530f6957bef1e2e0fa166ed87085dcae523 [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;
David Rientjesf0508972016-05-20 16:58:56 -070092static unsigned long khugepaged_sleep_expire;
Andrea Arcangeliba761492011-01-13 15:46:58 -080093static struct task_struct *khugepaged_thread __read_mostly;
94static DEFINE_MUTEX(khugepaged_mutex);
95static DEFINE_SPINLOCK(khugepaged_mm_lock);
96static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
97/*
98 * default collapse hugepages if there is at least one pte mapped like
99 * it would have happened if the vma was large enough during page
100 * fault.
101 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530102static unsigned int khugepaged_max_ptes_none __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800103
104static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800105static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700106static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800107
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800108#define MM_SLOTS_HASH_BITS 10
109static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
110
Andrea Arcangeliba761492011-01-13 15:46:58 -0800111static struct kmem_cache *mm_slot_cache __read_mostly;
112
113/**
114 * struct mm_slot - hash lookup from mm to mm_slot
115 * @hash: hash collision list
116 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
117 * @mm: the mm that this information is valid for
118 */
119struct mm_slot {
120 struct hlist_node hash;
121 struct list_head mm_node;
122 struct mm_struct *mm;
123};
124
125/**
126 * struct khugepaged_scan - cursor for scanning
127 * @mm_head: the head of the mm list to scan
128 * @mm_slot: the current mm_slot we are scanning
129 * @address: the next address inside that to be scanned
130 *
131 * There is only the one khugepaged_scan instance of this cursor structure.
132 */
133struct khugepaged_scan {
134 struct list_head mm_head;
135 struct mm_slot *mm_slot;
136 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700137};
138static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800139 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
140};
141
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800142static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800143
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700144static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145{
146 struct zone *zone;
147 int nr_zones = 0;
148 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800149
Andrea Arcangelif0005652011-01-13 15:47:04 -0800150 for_each_populated_zone(zone)
151 nr_zones++;
152
Mel Gorman974a7862015-11-06 16:28:34 -0800153 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800154 recommended_min = pageblock_nr_pages * nr_zones * 2;
155
156 /*
157 * Make sure that on average at least two pageblocks are almost free
158 * of another type, one for a migratetype to fall back to and a
159 * second to avoid subsequent fallbacks of other types There are 3
160 * MIGRATE_TYPES we care about.
161 */
162 recommended_min += pageblock_nr_pages * nr_zones *
163 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
164
165 /* don't ever allow to reserve more than 5% of the lowmem */
166 recommended_min = min(recommended_min,
167 (unsigned long) nr_free_buffer_pages() / 20);
168 recommended_min <<= (PAGE_SHIFT-10);
169
Han Pingtian42aa83c2014-01-23 15:53:28 -0800170 if (recommended_min > min_free_kbytes) {
171 if (user_min_free_kbytes >= 0)
Joe Perches756a0252016-03-17 14:19:47 -0700172 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
Han Pingtian42aa83c2014-01-23 15:53:28 -0800173 min_free_kbytes, recommended_min);
174
Andrea Arcangelif0005652011-01-13 15:47:04 -0800175 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800176 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800177 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800179
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700180static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800181{
182 int err = 0;
183 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800184 if (!khugepaged_thread)
185 khugepaged_thread = kthread_run(khugepaged, NULL,
186 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530187 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700188 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800189 err = PTR_ERR(khugepaged_thread);
190 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700191 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800192 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700193
194 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800195 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800196
197 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700198 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700199 kthread_stop(khugepaged_thread);
200 khugepaged_thread = NULL;
201 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700202fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800203 return err;
204}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800205
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800206static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800207struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800208
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700209struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800210{
211 struct page *zero_page;
212retry:
213 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700214 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800215
216 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
217 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800218 if (!zero_page) {
219 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700220 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800221 }
222 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800223 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700224 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800225 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700226 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227 goto retry;
228 }
229
230 /* We take additional reference here. It will be put back by shrinker */
231 atomic_set(&huge_zero_refcount, 2);
232 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700233 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800234}
235
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700236void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800237{
238 /*
239 * Counter should never go to zero here. Only shrinker can put
240 * last reference.
241 */
242 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
243}
244
Glauber Costa48896462013-08-28 10:18:15 +1000245static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
246 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800247{
Glauber Costa48896462013-08-28 10:18:15 +1000248 /* we can free zero page only if last reference remains */
249 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
250}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800251
Glauber Costa48896462013-08-28 10:18:15 +1000252static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
253 struct shrink_control *sc)
254{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800255 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700256 struct page *zero_page = xchg(&huge_zero_page, NULL);
257 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700258 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000259 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800260 }
261
262 return 0;
263}
264
265static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000266 .count_objects = shrink_huge_zero_page_count,
267 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800268 .seeks = DEFAULT_SEEKS,
269};
270
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800271#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800272
Mel Gorman444eb2a42016-03-17 14:19:23 -0700273static ssize_t triple_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800274 struct kobj_attribute *attr,
275 const char *buf, size_t count,
276 enum transparent_hugepage_flag enabled,
Mel Gorman444eb2a42016-03-17 14:19:23 -0700277 enum transparent_hugepage_flag deferred,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800278 enum transparent_hugepage_flag req_madv)
279{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700280 if (!memcmp("defer", buf,
281 min(sizeof("defer")-1, count))) {
282 if (enabled == deferred)
283 return -EINVAL;
284 clear_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800285 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700286 set_bit(deferred, &transparent_hugepage_flags);
287 } else if (!memcmp("always", buf,
288 min(sizeof("always")-1, count))) {
289 clear_bit(deferred, &transparent_hugepage_flags);
290 clear_bit(req_madv, &transparent_hugepage_flags);
291 set_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800292 } else if (!memcmp("madvise", buf,
293 min(sizeof("madvise")-1, count))) {
294 clear_bit(enabled, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700295 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800296 set_bit(req_madv, &transparent_hugepage_flags);
297 } else if (!memcmp("never", buf,
298 min(sizeof("never")-1, count))) {
299 clear_bit(enabled, &transparent_hugepage_flags);
300 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700301 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800302 } else
303 return -EINVAL;
304
305 return count;
306}
307
308static ssize_t enabled_show(struct kobject *kobj,
309 struct kobj_attribute *attr, char *buf)
310{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700311 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
312 return sprintf(buf, "[always] madvise never\n");
313 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
314 return sprintf(buf, "always [madvise] never\n");
315 else
316 return sprintf(buf, "always madvise [never]\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800317}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700318
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800319static ssize_t enabled_store(struct kobject *kobj,
320 struct kobj_attribute *attr,
321 const char *buf, size_t count)
322{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800323 ssize_t ret;
324
Mel Gorman444eb2a42016-03-17 14:19:23 -0700325 ret = triple_flag_store(kobj, attr, buf, count,
326 TRANSPARENT_HUGEPAGE_FLAG,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800327 TRANSPARENT_HUGEPAGE_FLAG,
328 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
329
330 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700331 int err;
332
333 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700334 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700335 mutex_unlock(&khugepaged_mutex);
336
Andrea Arcangeliba761492011-01-13 15:46:58 -0800337 if (err)
338 ret = err;
339 }
340
341 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800342}
343static struct kobj_attribute enabled_attr =
344 __ATTR(enabled, 0644, enabled_show, enabled_store);
345
346static ssize_t single_flag_show(struct kobject *kobj,
347 struct kobj_attribute *attr, char *buf,
348 enum transparent_hugepage_flag flag)
349{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700350 return sprintf(buf, "%d\n",
351 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800352}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700353
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800354static ssize_t single_flag_store(struct kobject *kobj,
355 struct kobj_attribute *attr,
356 const char *buf, size_t count,
357 enum transparent_hugepage_flag flag)
358{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700359 unsigned long value;
360 int ret;
361
362 ret = kstrtoul(buf, 10, &value);
363 if (ret < 0)
364 return ret;
365 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800366 return -EINVAL;
367
Ben Hutchingse27e6152011-04-14 15:22:21 -0700368 if (value)
369 set_bit(flag, &transparent_hugepage_flags);
370 else
371 clear_bit(flag, &transparent_hugepage_flags);
372
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800373 return count;
374}
375
376/*
377 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
378 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
379 * memory just to allocate one more hugepage.
380 */
381static ssize_t defrag_show(struct kobject *kobj,
382 struct kobj_attribute *attr, char *buf)
383{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700384 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
385 return sprintf(buf, "[always] defer madvise never\n");
386 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
387 return sprintf(buf, "always [defer] madvise never\n");
388 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
389 return sprintf(buf, "always defer [madvise] never\n");
390 else
391 return sprintf(buf, "always defer madvise [never]\n");
392
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800393}
394static ssize_t defrag_store(struct kobject *kobj,
395 struct kobj_attribute *attr,
396 const char *buf, size_t count)
397{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700398 return triple_flag_store(kobj, attr, buf, count,
399 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
400 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800401 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
402}
403static struct kobj_attribute defrag_attr =
404 __ATTR(defrag, 0644, defrag_show, defrag_store);
405
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800406static ssize_t use_zero_page_show(struct kobject *kobj,
407 struct kobj_attribute *attr, char *buf)
408{
409 return single_flag_show(kobj, attr, buf,
410 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
411}
412static ssize_t use_zero_page_store(struct kobject *kobj,
413 struct kobj_attribute *attr, const char *buf, size_t count)
414{
415 return single_flag_store(kobj, attr, buf, count,
416 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
417}
418static struct kobj_attribute use_zero_page_attr =
419 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800420#ifdef CONFIG_DEBUG_VM
421static ssize_t debug_cow_show(struct kobject *kobj,
422 struct kobj_attribute *attr, char *buf)
423{
424 return single_flag_show(kobj, attr, buf,
425 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
426}
427static ssize_t debug_cow_store(struct kobject *kobj,
428 struct kobj_attribute *attr,
429 const char *buf, size_t count)
430{
431 return single_flag_store(kobj, attr, buf, count,
432 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
433}
434static struct kobj_attribute debug_cow_attr =
435 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
436#endif /* CONFIG_DEBUG_VM */
437
438static struct attribute *hugepage_attr[] = {
439 &enabled_attr.attr,
440 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800441 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800442#ifdef CONFIG_DEBUG_VM
443 &debug_cow_attr.attr,
444#endif
445 NULL,
446};
447
448static struct attribute_group hugepage_attr_group = {
449 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800450};
451
452static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
453 struct kobj_attribute *attr,
454 char *buf)
455{
456 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
457}
458
459static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
460 struct kobj_attribute *attr,
461 const char *buf, size_t count)
462{
463 unsigned long msecs;
464 int err;
465
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700466 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800467 if (err || msecs > UINT_MAX)
468 return -EINVAL;
469
470 khugepaged_scan_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700471 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800472 wake_up_interruptible(&khugepaged_wait);
473
474 return count;
475}
476static struct kobj_attribute scan_sleep_millisecs_attr =
477 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
478 scan_sleep_millisecs_store);
479
480static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
481 struct kobj_attribute *attr,
482 char *buf)
483{
484 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
485}
486
487static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
488 struct kobj_attribute *attr,
489 const char *buf, size_t count)
490{
491 unsigned long msecs;
492 int err;
493
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700494 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800495 if (err || msecs > UINT_MAX)
496 return -EINVAL;
497
498 khugepaged_alloc_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700499 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800500 wake_up_interruptible(&khugepaged_wait);
501
502 return count;
503}
504static struct kobj_attribute alloc_sleep_millisecs_attr =
505 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
506 alloc_sleep_millisecs_store);
507
508static ssize_t pages_to_scan_show(struct kobject *kobj,
509 struct kobj_attribute *attr,
510 char *buf)
511{
512 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
513}
514static ssize_t pages_to_scan_store(struct kobject *kobj,
515 struct kobj_attribute *attr,
516 const char *buf, size_t count)
517{
518 int err;
519 unsigned long pages;
520
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700521 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800522 if (err || !pages || pages > UINT_MAX)
523 return -EINVAL;
524
525 khugepaged_pages_to_scan = pages;
526
527 return count;
528}
529static struct kobj_attribute pages_to_scan_attr =
530 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
531 pages_to_scan_store);
532
533static ssize_t pages_collapsed_show(struct kobject *kobj,
534 struct kobj_attribute *attr,
535 char *buf)
536{
537 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
538}
539static struct kobj_attribute pages_collapsed_attr =
540 __ATTR_RO(pages_collapsed);
541
542static ssize_t full_scans_show(struct kobject *kobj,
543 struct kobj_attribute *attr,
544 char *buf)
545{
546 return sprintf(buf, "%u\n", khugepaged_full_scans);
547}
548static struct kobj_attribute full_scans_attr =
549 __ATTR_RO(full_scans);
550
551static ssize_t khugepaged_defrag_show(struct kobject *kobj,
552 struct kobj_attribute *attr, char *buf)
553{
554 return single_flag_show(kobj, attr, buf,
555 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
556}
557static ssize_t khugepaged_defrag_store(struct kobject *kobj,
558 struct kobj_attribute *attr,
559 const char *buf, size_t count)
560{
561 return single_flag_store(kobj, attr, buf, count,
562 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
563}
564static struct kobj_attribute khugepaged_defrag_attr =
565 __ATTR(defrag, 0644, khugepaged_defrag_show,
566 khugepaged_defrag_store);
567
568/*
569 * max_ptes_none controls if khugepaged should collapse hugepages over
570 * any unmapped ptes in turn potentially increasing the memory
571 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
572 * reduce the available free memory in the system as it
573 * runs. Increasing max_ptes_none will instead potentially reduce the
574 * free memory in the system during the khugepaged scan.
575 */
576static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
577 struct kobj_attribute *attr,
578 char *buf)
579{
580 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
581}
582static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
583 struct kobj_attribute *attr,
584 const char *buf, size_t count)
585{
586 int err;
587 unsigned long max_ptes_none;
588
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700589 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800590 if (err || max_ptes_none > HPAGE_PMD_NR-1)
591 return -EINVAL;
592
593 khugepaged_max_ptes_none = max_ptes_none;
594
595 return count;
596}
597static struct kobj_attribute khugepaged_max_ptes_none_attr =
598 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
599 khugepaged_max_ptes_none_store);
600
601static struct attribute *khugepaged_attr[] = {
602 &khugepaged_defrag_attr.attr,
603 &khugepaged_max_ptes_none_attr.attr,
604 &pages_to_scan_attr.attr,
605 &pages_collapsed_attr.attr,
606 &full_scans_attr.attr,
607 &scan_sleep_millisecs_attr.attr,
608 &alloc_sleep_millisecs_attr.attr,
609 NULL,
610};
611
612static struct attribute_group khugepaged_attr_group = {
613 .attrs = khugepaged_attr,
614 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800615};
Shaohua Li569e5592012-01-12 17:19:11 -0800616
617static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
618{
619 int err;
620
621 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
622 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700623 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800624 return -ENOMEM;
625 }
626
627 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
628 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700629 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800630 goto delete_obj;
631 }
632
633 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
634 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700635 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800636 goto remove_hp_group;
637 }
638
639 return 0;
640
641remove_hp_group:
642 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
643delete_obj:
644 kobject_put(*hugepage_kobj);
645 return err;
646}
647
648static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
649{
650 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
651 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
652 kobject_put(hugepage_kobj);
653}
654#else
655static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
656{
657 return 0;
658}
659
660static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
661{
662}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800663#endif /* CONFIG_SYSFS */
664
665static int __init hugepage_init(void)
666{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800667 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800668 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800669
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800670 if (!has_transparent_hugepage()) {
671 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800672 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800673 }
674
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530675 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
676 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
677 /*
678 * hugepages can't be allocated by the buddy allocator
679 */
680 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
681 /*
682 * we use page->mapping and page->index in second tail page
683 * as list_head: assuming THP order >= 2
684 */
685 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
686
Shaohua Li569e5592012-01-12 17:19:11 -0800687 err = hugepage_init_sysfs(&hugepage_kobj);
688 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700689 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800690
691 err = khugepaged_slab_init();
692 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700693 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800694
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700695 err = register_shrinker(&huge_zero_page_shrinker);
696 if (err)
697 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800698 err = register_shrinker(&deferred_split_shrinker);
699 if (err)
700 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800701
Rik van Riel97562cd2011-01-13 15:47:12 -0800702 /*
703 * By default disable transparent hugepages on smaller systems,
704 * where the extra memory used could hurt more than TLB overhead
705 * is likely to save. The admin can still enable it through /sys.
706 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700707 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800708 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700709 return 0;
710 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800711
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700712 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700713 if (err)
714 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800715
Shaohua Li569e5592012-01-12 17:19:11 -0800716 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700717err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800718 unregister_shrinker(&deferred_split_shrinker);
719err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700720 unregister_shrinker(&huge_zero_page_shrinker);
721err_hzp_shrinker:
722 khugepaged_slab_exit();
723err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800724 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700725err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800726 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800727}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800728subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800729
730static int __init setup_transparent_hugepage(char *str)
731{
732 int ret = 0;
733 if (!str)
734 goto out;
735 if (!strcmp(str, "always")) {
736 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
737 &transparent_hugepage_flags);
738 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
739 &transparent_hugepage_flags);
740 ret = 1;
741 } else if (!strcmp(str, "madvise")) {
742 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
743 &transparent_hugepage_flags);
744 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
745 &transparent_hugepage_flags);
746 ret = 1;
747 } else if (!strcmp(str, "never")) {
748 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
749 &transparent_hugepage_flags);
750 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
751 &transparent_hugepage_flags);
752 ret = 1;
753 }
754out:
755 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700756 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800757 return ret;
758}
759__setup("transparent_hugepage=", setup_transparent_hugepage);
760
Mel Gormanb32967f2012-11-19 12:35:47 +0000761pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800762{
763 if (likely(vma->vm_flags & VM_WRITE))
764 pmd = pmd_mkwrite(pmd);
765 return pmd;
766}
767
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700768static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800769{
Yang Shi340a43b2016-05-20 16:57:30 -0700770 return pmd_mkhuge(mk_pmd(page, prot));
Bob Liub3092b32012-12-11 16:00:41 -0800771}
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}
Dan Williamsdee41072016-05-14 12:20:44 -07001016EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001017
Dan Williams3565fce2016-01-15 16:56:55 -08001018static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1019 pmd_t *pmd)
1020{
1021 pmd_t _pmd;
1022
1023 /*
1024 * We should set the dirty bit only for FOLL_WRITE but for now
1025 * the dirty bit in the pmd is meaningless. And if the dirty
1026 * bit will become meaningful and we'll only set it with
1027 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1028 * set the young bit, instead of the current set_pmd_at.
1029 */
1030 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1031 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1032 pmd, _pmd, 1))
1033 update_mmu_cache_pmd(vma, addr, pmd);
1034}
1035
1036struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1037 pmd_t *pmd, int flags)
1038{
1039 unsigned long pfn = pmd_pfn(*pmd);
1040 struct mm_struct *mm = vma->vm_mm;
1041 struct dev_pagemap *pgmap;
1042 struct page *page;
1043
1044 assert_spin_locked(pmd_lockptr(mm, pmd));
1045
1046 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1047 return NULL;
1048
1049 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1050 /* pass */;
1051 else
1052 return NULL;
1053
1054 if (flags & FOLL_TOUCH)
1055 touch_pmd(vma, addr, pmd);
1056
1057 /*
1058 * device mapped pages can only be returned if the
1059 * caller will manage the page reference count.
1060 */
1061 if (!(flags & FOLL_GET))
1062 return ERR_PTR(-EEXIST);
1063
1064 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1065 pgmap = get_dev_pagemap(pfn, NULL);
1066 if (!pgmap)
1067 return ERR_PTR(-EFAULT);
1068 page = pfn_to_page(pfn);
1069 get_page(page);
1070 put_dev_pagemap(pgmap);
1071
1072 return page;
1073}
1074
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001075int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1076 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1077 struct vm_area_struct *vma)
1078{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001079 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001080 struct page *src_page;
1081 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001082 pgtable_t pgtable = NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001083 int ret;
1084
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001085 if (!vma_is_dax(vma)) {
1086 ret = -ENOMEM;
1087 pgtable = pte_alloc_one(dst_mm, addr);
1088 if (unlikely(!pgtable))
1089 goto out;
1090 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001091
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001092 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1093 src_ptl = pmd_lockptr(src_mm, src_pmd);
1094 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001095
1096 ret = -EAGAIN;
1097 pmd = *src_pmd;
Dan Williams5c7fb562016-01-15 16:56:52 -08001098 if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001099 pte_free(dst_mm, pgtable);
1100 goto out_unlock;
1101 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001102 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001103 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001104 * under splitting since we don't split the page itself, only pmd to
1105 * a page table.
1106 */
1107 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001108 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001109 /*
1110 * get_huge_zero_page() will never allocate a new page here,
1111 * since we already have a zero page to copy. It just takes a
1112 * reference.
1113 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001114 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001115 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001116 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001117 ret = 0;
1118 goto out_unlock;
1119 }
Mel Gormande466bd2013-12-18 17:08:42 -08001120
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001121 if (!vma_is_dax(vma)) {
Dan Williams5c7fb562016-01-15 16:56:52 -08001122 /* thp accounting separate from pmd_devmap accounting */
1123 src_page = pmd_page(pmd);
1124 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1125 get_page(src_page);
1126 page_dup_rmap(src_page, true);
1127 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1128 atomic_long_inc(&dst_mm->nr_ptes);
1129 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1130 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001131
1132 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1133 pmd = pmd_mkold(pmd_wrprotect(pmd));
1134 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001135
1136 ret = 0;
1137out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001138 spin_unlock(src_ptl);
1139 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001140out:
1141 return ret;
1142}
1143
Will Deacona1dd4502012-12-11 16:01:27 -08001144void huge_pmd_set_accessed(struct mm_struct *mm,
1145 struct vm_area_struct *vma,
1146 unsigned long address,
1147 pmd_t *pmd, pmd_t orig_pmd,
1148 int dirty)
1149{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001150 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001151 pmd_t entry;
1152 unsigned long haddr;
1153
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001154 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001155 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1156 goto unlock;
1157
1158 entry = pmd_mkyoung(orig_pmd);
1159 haddr = address & HPAGE_PMD_MASK;
1160 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1161 update_mmu_cache_pmd(vma, address, pmd);
1162
1163unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001164 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001165}
1166
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001167static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1168 struct vm_area_struct *vma,
1169 unsigned long address,
1170 pmd_t *pmd, pmd_t orig_pmd,
1171 struct page *page,
1172 unsigned long haddr)
1173{
Johannes Weiner00501b52014-08-08 14:19:20 -07001174 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001175 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001176 pgtable_t pgtable;
1177 pmd_t _pmd;
1178 int ret = 0, i;
1179 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001180 unsigned long mmun_start; /* For mmu_notifiers */
1181 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001182
1183 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1184 GFP_KERNEL);
1185 if (unlikely(!pages)) {
1186 ret |= VM_FAULT_OOM;
1187 goto out;
1188 }
1189
1190 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001191 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1192 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001193 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001194 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001195 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001196 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001197 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001198 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001199 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001200 memcg = (void *)page_private(pages[i]);
1201 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001202 mem_cgroup_cancel_charge(pages[i], memcg,
1203 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001204 put_page(pages[i]);
1205 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001206 kfree(pages);
1207 ret |= VM_FAULT_OOM;
1208 goto out;
1209 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001210 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001211 }
1212
1213 for (i = 0; i < HPAGE_PMD_NR; i++) {
1214 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001215 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001216 __SetPageUptodate(pages[i]);
1217 cond_resched();
1218 }
1219
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001220 mmun_start = haddr;
1221 mmun_end = haddr + HPAGE_PMD_SIZE;
1222 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1223
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001224 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001225 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1226 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001227 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001228
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001229 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001230 /* leave pmd empty until pte is filled */
1231
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001232 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001233 pmd_populate(mm, &_pmd, pgtable);
1234
1235 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1236 pte_t *pte, entry;
1237 entry = mk_pte(pages[i], vma->vm_page_prot);
1238 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001239 memcg = (void *)page_private(pages[i]);
1240 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001241 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001242 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001243 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001244 pte = pte_offset_map(&_pmd, haddr);
1245 VM_BUG_ON(!pte_none(*pte));
1246 set_pte_at(mm, haddr, pte, entry);
1247 pte_unmap(pte);
1248 }
1249 kfree(pages);
1250
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001251 smp_wmb(); /* make pte visible before pmd */
1252 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001253 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001254 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001255
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001256 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1257
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001258 ret |= VM_FAULT_WRITE;
1259 put_page(page);
1260
1261out:
1262 return ret;
1263
1264out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001265 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001266 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001267 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001268 memcg = (void *)page_private(pages[i]);
1269 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001270 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001271 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001272 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001273 kfree(pages);
1274 goto out;
1275}
1276
1277int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1278 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1279{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001280 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001281 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001282 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001283 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001284 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001285 unsigned long mmun_start; /* For mmu_notifiers */
1286 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001287 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001288
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001289 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001290 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001291 haddr = address & HPAGE_PMD_MASK;
1292 if (is_huge_zero_pmd(orig_pmd))
1293 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001294 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001295 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1296 goto out_unlock;
1297
1298 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001299 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001300 /*
1301 * We can only reuse the page if nobody else maps the huge page or it's
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001302 * part.
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001303 */
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001304 if (page_trans_huge_mapcount(page, NULL) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001305 pmd_t entry;
1306 entry = pmd_mkyoung(orig_pmd);
1307 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1308 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001309 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001310 ret |= VM_FAULT_WRITE;
1311 goto out_unlock;
1312 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001313 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001314 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001315alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001316 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001317 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001318 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b363692015-04-15 16:13:29 -07001319 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001320 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001321 new_page = NULL;
1322
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001323 if (likely(new_page)) {
1324 prep_transhuge_page(new_page);
1325 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001326 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001327 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001328 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001329 } else {
1330 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1331 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001332 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001333 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001334 ret |= VM_FAULT_FALLBACK;
1335 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001336 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001337 }
David Rientjes17766dd2013-09-12 15:14:06 -07001338 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001339 goto out;
1340 }
1341
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001342 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1343 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001344 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001345 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001346 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001347 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001348 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001349 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001350 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001351 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001352 goto out;
1353 }
1354
David Rientjes17766dd2013-09-12 15:14:06 -07001355 count_vm_event(THP_FAULT_ALLOC);
1356
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001357 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001358 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1359 else
1360 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001361 __SetPageUptodate(new_page);
1362
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001363 mmun_start = haddr;
1364 mmun_end = haddr + HPAGE_PMD_SIZE;
1365 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1366
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001367 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001368 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001369 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001370 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001371 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001372 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001373 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001374 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001375 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001376 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001377 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1378 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001379 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001380 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001381 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001382 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001383 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001384 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001385 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001386 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001387 put_huge_zero_page();
1388 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001389 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001390 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001391 put_page(page);
1392 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001393 ret |= VM_FAULT_WRITE;
1394 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001395 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001396out_mn:
1397 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1398out:
1399 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001400out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001401 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001402 return ret;
1403}
1404
David Rientjesb676b292012-10-08 16:34:03 -07001405struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001406 unsigned long addr,
1407 pmd_t *pmd,
1408 unsigned int flags)
1409{
David Rientjesb676b292012-10-08 16:34:03 -07001410 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001411 struct page *page = NULL;
1412
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001413 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001414
1415 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1416 goto out;
1417
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001418 /* Avoid dumping huge zero page */
1419 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1420 return ERR_PTR(-EFAULT);
1421
Mel Gorman2b4847e2013-12-18 17:08:32 -08001422 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001423 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001424 goto out;
1425
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001426 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001427 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001428 if (flags & FOLL_TOUCH)
1429 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001430 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001431 /*
1432 * We don't mlock() pte-mapped THPs. This way we can avoid
1433 * leaking mlocked pages into non-VM_LOCKED VMAs.
1434 *
1435 * In most cases the pmd is the only mapping of the page as we
1436 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1437 * writable private mappings in populate_vma_page_range().
1438 *
1439 * The only scenario when we have the page shared here is if we
1440 * mlocking read-only mapping shared over fork(). We skip
1441 * mlocking such pages.
1442 */
1443 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1444 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001445 lru_add_drain();
1446 if (page->mapping)
1447 mlock_vma_page(page);
1448 unlock_page(page);
1449 }
1450 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001451 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001452 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001453 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001454 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001455
1456out:
1457 return page;
1458}
1459
Mel Gormand10e63f2012-10-25 14:16:31 +02001460/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001461int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1462 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001463{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001464 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001465 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001466 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001467 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001468 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001469 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001470 bool page_locked;
1471 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001472 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001473 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001474
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001475 /* A PROT_NONE fault should not end up here */
1476 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1477
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001478 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001479 if (unlikely(!pmd_same(pmd, *pmdp)))
1480 goto out_unlock;
1481
Mel Gormande466bd2013-12-18 17:08:42 -08001482 /*
1483 * If there are potential migrations, wait for completion and retry
1484 * without disrupting NUMA hinting information. Do not relock and
1485 * check_same as the page may no longer be mapped.
1486 */
1487 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001488 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001489 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001490 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001491 goto out;
1492 }
1493
Mel Gormand10e63f2012-10-25 14:16:31 +02001494 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001495 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001496 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001497 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001498 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001499 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001500 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001501 flags |= TNF_FAULT_LOCAL;
1502 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001503
Mel Gormanbea66fb2015-03-25 15:55:37 -07001504 /* See similar comment in do_numa_page for explanation */
1505 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001506 flags |= TNF_NO_GROUP;
1507
1508 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001509 * Acquire the page lock to serialise THP migrations but avoid dropping
1510 * page_table_lock if at all possible
1511 */
Mel Gormanb8916632013-10-07 11:28:44 +01001512 page_locked = trylock_page(page);
1513 target_nid = mpol_misplaced(page, vma, haddr);
1514 if (target_nid == -1) {
1515 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001516 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001517 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001518 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001519
Mel Gormande466bd2013-12-18 17:08:42 -08001520 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001521 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001522 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001523 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001524 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001525 goto out;
1526 }
1527
Mel Gorman2b4847e2013-12-18 17:08:32 -08001528 /*
1529 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1530 * to serialises splits
1531 */
Mel Gormanb8916632013-10-07 11:28:44 +01001532 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001533 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001534 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001535
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001536 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001537 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001538 if (unlikely(!pmd_same(pmd, *pmdp))) {
1539 unlock_page(page);
1540 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001541 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001542 goto out_unlock;
1543 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001544
Mel Gormanc3a489c2013-12-18 17:08:38 -08001545 /* Bail if we fail to protect against THP splits for any reason */
1546 if (unlikely(!anon_vma)) {
1547 put_page(page);
1548 page_nid = -1;
1549 goto clear_pmdnuma;
1550 }
1551
Mel Gormana54a4072013-10-07 11:28:46 +01001552 /*
1553 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001554 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001555 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001556 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001557 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001558 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001559 if (migrated) {
1560 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001561 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001562 } else
1563 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001564
Mel Gorman8191acb2013-10-07 11:28:45 +01001565 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001566clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001567 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001568 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001569 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001570 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001571 if (was_writable)
1572 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001573 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001574 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001575 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001576out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001577 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001578
1579out:
1580 if (anon_vma)
1581 page_unlock_anon_vma_read(anon_vma);
1582
Mel Gorman8191acb2013-10-07 11:28:45 +01001583 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001584 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001585
Mel Gormand10e63f2012-10-25 14:16:31 +02001586 return 0;
1587}
1588
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001589int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1590 pmd_t *pmd, unsigned long addr, unsigned long next)
1591
1592{
1593 spinlock_t *ptl;
1594 pmd_t orig_pmd;
1595 struct page *page;
1596 struct mm_struct *mm = tlb->mm;
1597 int ret = 0;
1598
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001599 ptl = pmd_trans_huge_lock(pmd, vma);
1600 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001601 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001602
1603 orig_pmd = *pmd;
1604 if (is_huge_zero_pmd(orig_pmd)) {
1605 ret = 1;
1606 goto out;
1607 }
1608
1609 page = pmd_page(orig_pmd);
1610 /*
1611 * If other processes are mapping this page, we couldn't discard
1612 * the page unless they all do MADV_FREE so let's skip the page.
1613 */
1614 if (page_mapcount(page) != 1)
1615 goto out;
1616
1617 if (!trylock_page(page))
1618 goto out;
1619
1620 /*
1621 * If user want to discard part-pages of THP, split it so MADV_FREE
1622 * will deactivate only them.
1623 */
1624 if (next - addr != HPAGE_PMD_SIZE) {
1625 get_page(page);
1626 spin_unlock(ptl);
1627 if (split_huge_page(page)) {
1628 put_page(page);
1629 unlock_page(page);
1630 goto out_unlocked;
1631 }
1632 put_page(page);
1633 unlock_page(page);
1634 ret = 1;
1635 goto out_unlocked;
1636 }
1637
1638 if (PageDirty(page))
1639 ClearPageDirty(page);
1640 unlock_page(page);
1641
1642 if (PageActive(page))
1643 deactivate_page(page);
1644
1645 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1646 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1647 tlb->fullmm);
1648 orig_pmd = pmd_mkold(orig_pmd);
1649 orig_pmd = pmd_mkclean(orig_pmd);
1650
1651 set_pmd_at(mm, addr, pmd, orig_pmd);
1652 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1653 }
1654 ret = 1;
1655out:
1656 spin_unlock(ptl);
1657out_unlocked:
1658 return ret;
1659}
1660
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001661int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001662 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001663{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001664 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001665 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001666
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001667 ptl = __pmd_trans_huge_lock(pmd, vma);
1668 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001669 return 0;
1670 /*
1671 * For architectures like ppc64 we look at deposited pgtable
1672 * when calling pmdp_huge_get_and_clear. So do the
1673 * pgtable_trans_huge_withdraw after finishing pmdp related
1674 * operations.
1675 */
1676 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1677 tlb->fullmm);
1678 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1679 if (vma_is_dax(vma)) {
1680 spin_unlock(ptl);
1681 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001682 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001683 } else if (is_huge_zero_pmd(orig_pmd)) {
1684 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1685 atomic_long_dec(&tlb->mm->nr_ptes);
1686 spin_unlock(ptl);
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001687 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001688 } else {
1689 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001690 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001691 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1692 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1693 VM_BUG_ON_PAGE(!PageHead(page), page);
1694 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1695 atomic_long_dec(&tlb->mm->nr_ptes);
1696 spin_unlock(ptl);
1697 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001698 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001699 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001700}
1701
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001702bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001703 unsigned long new_addr, unsigned long old_end,
1704 pmd_t *old_pmd, pmd_t *new_pmd)
1705{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001706 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001707 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001708 struct mm_struct *mm = vma->vm_mm;
1709
1710 if ((old_addr & ~HPAGE_PMD_MASK) ||
1711 (new_addr & ~HPAGE_PMD_MASK) ||
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001712 old_end - old_addr < HPAGE_PMD_SIZE)
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001713 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001714
1715 /*
1716 * The destination pmd shouldn't be established, free_pgtables()
1717 * should have release it.
1718 */
1719 if (WARN_ON(!pmd_none(*new_pmd))) {
1720 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001721 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001722 }
1723
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001724 /*
1725 * We don't have to worry about the ordering of src and dst
1726 * ptlocks because exclusive mmap_sem prevents deadlock.
1727 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001728 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1729 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001730 new_ptl = pmd_lockptr(mm, new_pmd);
1731 if (new_ptl != old_ptl)
1732 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001733 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001734 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001735
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001736 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1737 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301738 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001739 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1740 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001741 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301742 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1743 if (new_ptl != old_ptl)
1744 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001745 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001746 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001747 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001748 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001749}
1750
Mel Gormanf123d742013-10-07 11:28:49 +01001751/*
1752 * Returns
1753 * - 0 if PMD could not be locked
1754 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1755 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1756 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001757int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001758 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001759{
1760 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001761 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001762 int ret = 0;
1763
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001764 ptl = __pmd_trans_huge_lock(pmd, vma);
1765 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001766 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001767 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001768 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001769
1770 /*
1771 * Avoid trapping faults against the zero page. The read-only
1772 * data is likely to be read-cached on the local CPU and
1773 * local/remote hits to the zero page are not interesting.
1774 */
1775 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1776 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001777 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001778 }
1779
Mel Gorman10c10452015-02-12 14:58:44 -08001780 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001781 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001782 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001783 if (preserve_write)
1784 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001785 ret = HPAGE_PMD_NR;
1786 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001787 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001788 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001789 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001790 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001791
1792 return ret;
1793}
1794
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001795/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001796 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001797 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001798 * Note that if it returns true, this routine returns without unlocking page
1799 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001800 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001801spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001802{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001803 spinlock_t *ptl;
1804 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001805 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001806 return ptl;
1807 spin_unlock(ptl);
1808 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001809}
1810
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001811#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001812
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001813int hugepage_madvise(struct vm_area_struct *vma,
1814 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001815{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001816 switch (advice) {
1817 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001818#ifdef CONFIG_S390
1819 /*
1820 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1821 * can't handle this properly after s390_enable_sie, so we simply
1822 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1823 */
1824 if (mm_has_pgste(vma->vm_mm))
1825 return 0;
1826#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001827 /*
1828 * Be somewhat over-protective like KSM for now!
1829 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001830 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001831 return -EINVAL;
1832 *vm_flags &= ~VM_NOHUGEPAGE;
1833 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001834 /*
1835 * If the vma become good for khugepaged to scan,
1836 * register it here without waiting a page fault that
1837 * may not happen any time soon.
1838 */
David Rientjes6d50e602014-10-29 14:50:31 -07001839 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001840 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001841 break;
1842 case MADV_NOHUGEPAGE:
1843 /*
1844 * Be somewhat over-protective like KSM for now!
1845 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001846 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001847 return -EINVAL;
1848 *vm_flags &= ~VM_HUGEPAGE;
1849 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001850 /*
1851 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1852 * this vma even if we leave the mm registered in khugepaged if
1853 * it got registered before VM_NOHUGEPAGE was set.
1854 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001855 break;
1856 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001857
1858 return 0;
1859}
1860
Andrea Arcangeliba761492011-01-13 15:46:58 -08001861static int __init khugepaged_slab_init(void)
1862{
1863 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1864 sizeof(struct mm_slot),
1865 __alignof__(struct mm_slot), 0, NULL);
1866 if (!mm_slot_cache)
1867 return -ENOMEM;
1868
1869 return 0;
1870}
1871
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001872static void __init khugepaged_slab_exit(void)
1873{
1874 kmem_cache_destroy(mm_slot_cache);
1875}
1876
Andrea Arcangeliba761492011-01-13 15:46:58 -08001877static inline struct mm_slot *alloc_mm_slot(void)
1878{
1879 if (!mm_slot_cache) /* initialization failed */
1880 return NULL;
1881 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1882}
1883
1884static inline void free_mm_slot(struct mm_slot *mm_slot)
1885{
1886 kmem_cache_free(mm_slot_cache, mm_slot);
1887}
1888
Andrea Arcangeliba761492011-01-13 15:46:58 -08001889static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1890{
1891 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001892
Sasha Levinb67bfe02013-02-27 17:06:00 -08001893 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001894 if (mm == mm_slot->mm)
1895 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001896
Andrea Arcangeliba761492011-01-13 15:46:58 -08001897 return NULL;
1898}
1899
1900static void insert_to_mm_slots_hash(struct mm_struct *mm,
1901 struct mm_slot *mm_slot)
1902{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001903 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001904 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905}
1906
1907static inline int khugepaged_test_exit(struct mm_struct *mm)
1908{
1909 return atomic_read(&mm->mm_users) == 0;
1910}
1911
1912int __khugepaged_enter(struct mm_struct *mm)
1913{
1914 struct mm_slot *mm_slot;
1915 int wakeup;
1916
1917 mm_slot = alloc_mm_slot();
1918 if (!mm_slot)
1919 return -ENOMEM;
1920
1921 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001922 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001923 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1924 free_mm_slot(mm_slot);
1925 return 0;
1926 }
1927
1928 spin_lock(&khugepaged_mm_lock);
1929 insert_to_mm_slots_hash(mm, mm_slot);
1930 /*
1931 * Insert just behind the scanning cursor, to let the area settle
1932 * down a little.
1933 */
1934 wakeup = list_empty(&khugepaged_scan.mm_head);
1935 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1936 spin_unlock(&khugepaged_mm_lock);
1937
1938 atomic_inc(&mm->mm_count);
1939 if (wakeup)
1940 wake_up_interruptible(&khugepaged_wait);
1941
1942 return 0;
1943}
1944
David Rientjes6d50e602014-10-29 14:50:31 -07001945int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1946 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001947{
1948 unsigned long hstart, hend;
1949 if (!vma->anon_vma)
1950 /*
1951 * Not yet faulted in so we will register later in the
1952 * page fault if needed.
1953 */
1954 return 0;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07001955 if (vma->vm_ops || (vm_flags & VM_NO_THP))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001956 /* khugepaged not yet working on file or special mappings */
1957 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001958 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1959 hend = vma->vm_end & HPAGE_PMD_MASK;
1960 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001961 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001962 return 0;
1963}
1964
1965void __khugepaged_exit(struct mm_struct *mm)
1966{
1967 struct mm_slot *mm_slot;
1968 int free = 0;
1969
1970 spin_lock(&khugepaged_mm_lock);
1971 mm_slot = get_mm_slot(mm);
1972 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001973 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001974 list_del(&mm_slot->mm_node);
1975 free = 1;
1976 }
Chris Wrightd788e802011-07-25 17:12:14 -07001977 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001978
1979 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001980 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1981 free_mm_slot(mm_slot);
1982 mmdrop(mm);
1983 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001984 /*
1985 * This is required to serialize against
1986 * khugepaged_test_exit() (which is guaranteed to run
1987 * under mmap sem read mode). Stop here (after we
1988 * return all pagetables will be destroyed) until
1989 * khugepaged has finished working on the pagetables
1990 * under the mmap_sem.
1991 */
1992 down_write(&mm->mmap_sem);
1993 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001994 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001995}
1996
1997static void release_pte_page(struct page *page)
1998{
1999 /* 0 stands for page_is_file_cache(page) == false */
2000 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2001 unlock_page(page);
2002 putback_lru_page(page);
2003}
2004
2005static void release_pte_pages(pte_t *pte, pte_t *_pte)
2006{
2007 while (--_pte >= pte) {
2008 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002009 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002010 release_pte_page(pte_page(pteval));
2011 }
2012}
2013
Andrea Arcangeliba761492011-01-13 15:46:58 -08002014static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2015 unsigned long address,
2016 pte_t *pte)
2017{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002018 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002019 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002020 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002021 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002022
Andrea Arcangeliba761492011-01-13 15:46:58 -08002023 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2024 _pte++, address += PAGE_SIZE) {
2025 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002026 if (pte_none(pteval) || (pte_present(pteval) &&
2027 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002028 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002029 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002030 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002031 } else {
2032 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002033 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002034 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002035 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002036 if (!pte_present(pteval)) {
2037 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002038 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002039 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002040 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002041 if (unlikely(!page)) {
2042 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002043 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002044 }
Bob Liu344aa352012-12-11 16:00:34 -08002045
Sasha Levin309381fea2014-01-23 15:52:54 -08002046 VM_BUG_ON_PAGE(PageCompound(page), page);
2047 VM_BUG_ON_PAGE(!PageAnon(page), page);
2048 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002049
Andrea Arcangeliba761492011-01-13 15:46:58 -08002050 /*
2051 * We can do it before isolate_lru_page because the
2052 * page can't be freed from under us. NOTE: PG_lock
2053 * is needed to serialize against split_huge_page
2054 * when invoked from the VM.
2055 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002056 if (!trylock_page(page)) {
2057 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002058 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002059 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002060
2061 /*
2062 * cannot use mapcount: can't collapse if there's a gup pin.
2063 * The page must only be referenced by the scanned process
2064 * and page swap cache.
2065 */
2066 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2067 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002068 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002069 goto out;
2070 }
2071 if (pte_write(pteval)) {
2072 writable = true;
2073 } else {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002074 if (PageSwapCache(page) &&
2075 !reuse_swap_page(page, NULL)) {
Ebru Akagunduz10359212015-02-11 15:28:28 -08002076 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002077 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002078 goto out;
2079 }
2080 /*
2081 * Page is not in the swap cache. It can be collapsed
2082 * into a THP.
2083 */
2084 }
2085
Andrea Arcangeliba761492011-01-13 15:46:58 -08002086 /*
2087 * Isolate the page to avoid collapsing an hugepage
2088 * currently in use by the VM.
2089 */
2090 if (isolate_lru_page(page)) {
2091 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002092 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002093 goto out;
2094 }
2095 /* 0 stands for page_is_file_cache(page) == false */
2096 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002097 VM_BUG_ON_PAGE(!PageLocked(page), page);
2098 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002099
2100 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002101 if (pte_young(pteval) ||
2102 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002103 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002104 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002105 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002106 if (likely(writable)) {
2107 if (likely(referenced)) {
2108 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002109 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002110 referenced, writable, result);
2111 return 1;
2112 }
2113 } else {
2114 result = SCAN_PAGE_RO;
2115 }
2116
Andrea Arcangeliba761492011-01-13 15:46:58 -08002117out:
Bob Liu344aa352012-12-11 16:00:34 -08002118 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002119 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002120 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002121 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002122}
2123
2124static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2125 struct vm_area_struct *vma,
2126 unsigned long address,
2127 spinlock_t *ptl)
2128{
2129 pte_t *_pte;
2130 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2131 pte_t pteval = *_pte;
2132 struct page *src_page;
2133
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002134 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002135 clear_user_highpage(page, address);
2136 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002137 if (is_zero_pfn(pte_pfn(pteval))) {
2138 /*
2139 * ptl mostly unnecessary.
2140 */
2141 spin_lock(ptl);
2142 /*
2143 * paravirt calls inside pte_clear here are
2144 * superfluous.
2145 */
2146 pte_clear(vma->vm_mm, address, _pte);
2147 spin_unlock(ptl);
2148 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002149 } else {
2150 src_page = pte_page(pteval);
2151 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002152 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002153 release_pte_page(src_page);
2154 /*
2155 * ptl mostly unnecessary, but preempt has to
2156 * be disabled to update the per-cpu stats
2157 * inside page_remove_rmap().
2158 */
2159 spin_lock(ptl);
2160 /*
2161 * paravirt calls inside pte_clear here are
2162 * superfluous.
2163 */
2164 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002165 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002166 spin_unlock(ptl);
2167 free_page_and_swap_cache(src_page);
2168 }
2169
2170 address += PAGE_SIZE;
2171 page++;
2172 }
2173}
2174
Xiao Guangrong26234f32012-10-08 16:29:51 -07002175static void khugepaged_alloc_sleep(void)
2176{
Petr Mladekbde43c62015-09-08 15:04:05 -07002177 DEFINE_WAIT(wait);
2178
2179 add_wait_queue(&khugepaged_wait, &wait);
2180 freezable_schedule_timeout_interruptible(
2181 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2182 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002183}
2184
Bob Liu9f1b8682013-11-12 15:07:37 -08002185static int khugepaged_node_load[MAX_NUMNODES];
2186
David Rientjes14a4e212014-08-06 16:07:29 -07002187static bool khugepaged_scan_abort(int nid)
2188{
2189 int i;
2190
2191 /*
2192 * If zone_reclaim_mode is disabled, then no extra effort is made to
2193 * allocate memory locally.
2194 */
2195 if (!zone_reclaim_mode)
2196 return false;
2197
2198 /* If there is a count for this node already, it must be acceptable */
2199 if (khugepaged_node_load[nid])
2200 return false;
2201
2202 for (i = 0; i < MAX_NUMNODES; i++) {
2203 if (!khugepaged_node_load[i])
2204 continue;
2205 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2206 return true;
2207 }
2208 return false;
2209}
2210
Xiao Guangrong26234f32012-10-08 16:29:51 -07002211#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002212static int khugepaged_find_target_node(void)
2213{
2214 static int last_khugepaged_target_node = NUMA_NO_NODE;
2215 int nid, target_node = 0, max_value = 0;
2216
2217 /* find first node with max normal pages hit */
2218 for (nid = 0; nid < MAX_NUMNODES; nid++)
2219 if (khugepaged_node_load[nid] > max_value) {
2220 max_value = khugepaged_node_load[nid];
2221 target_node = nid;
2222 }
2223
2224 /* do some balance if several nodes have the same hit record */
2225 if (target_node <= last_khugepaged_target_node)
2226 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2227 nid++)
2228 if (max_value == khugepaged_node_load[nid]) {
2229 target_node = nid;
2230 break;
2231 }
2232
2233 last_khugepaged_target_node = target_node;
2234 return target_node;
2235}
2236
Xiao Guangrong26234f32012-10-08 16:29:51 -07002237static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2238{
2239 if (IS_ERR(*hpage)) {
2240 if (!*wait)
2241 return false;
2242
2243 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002244 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002245 khugepaged_alloc_sleep();
2246 } else if (*hpage) {
2247 put_page(*hpage);
2248 *hpage = NULL;
2249 }
2250
2251 return true;
2252}
2253
Michal Hocko3b363692015-04-15 16:13:29 -07002254static struct page *
2255khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002256 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002257{
Sasha Levin309381fea2014-01-23 15:52:54 -08002258 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002259
Xiao Guangrong26234f32012-10-08 16:29:51 -07002260 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002261 * Before allocating the hugepage, release the mmap_sem read lock.
2262 * The allocation can take potentially a long time if it involves
2263 * sync compaction, and we do not need to hold the mmap_sem during
2264 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002265 */
2266 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002267
Vlastimil Babka96db8002015-09-08 15:03:50 -07002268 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002269 if (unlikely(!*hpage)) {
2270 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2271 *hpage = ERR_PTR(-ENOMEM);
2272 return NULL;
2273 }
2274
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002275 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002276 count_vm_event(THP_COLLAPSE_ALLOC);
2277 return *hpage;
2278}
2279#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002280static int khugepaged_find_target_node(void)
2281{
2282 return 0;
2283}
2284
Mel Gorman444eb2a42016-03-17 14:19:23 -07002285static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002286{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002287 struct page *page;
2288
Mel Gorman444eb2a42016-03-17 14:19:23 -07002289 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2290 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002291 if (page)
2292 prep_transhuge_page(page);
2293 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002294}
2295
Xiao Guangrong26234f32012-10-08 16:29:51 -07002296static struct page *khugepaged_alloc_hugepage(bool *wait)
2297{
2298 struct page *hpage;
2299
2300 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002301 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002302 if (!hpage) {
2303 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2304 if (!*wait)
2305 return NULL;
2306
2307 *wait = false;
2308 khugepaged_alloc_sleep();
2309 } else
2310 count_vm_event(THP_COLLAPSE_ALLOC);
2311 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2312
2313 return hpage;
2314}
2315
2316static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2317{
2318 if (!*hpage)
2319 *hpage = khugepaged_alloc_hugepage(wait);
2320
2321 if (unlikely(!*hpage))
2322 return false;
2323
2324 return true;
2325}
2326
Michal Hocko3b363692015-04-15 16:13:29 -07002327static struct page *
2328khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002329 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002330{
2331 up_read(&mm->mmap_sem);
2332 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002333
Xiao Guangrong26234f32012-10-08 16:29:51 -07002334 return *hpage;
2335}
2336#endif
2337
Bob Liufa475e52012-12-11 16:00:39 -08002338static bool hugepage_vma_check(struct vm_area_struct *vma)
2339{
2340 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2341 (vma->vm_flags & VM_NOHUGEPAGE))
2342 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002343 if (!vma->anon_vma || vma->vm_ops)
2344 return false;
2345 if (is_vma_temporary_stack(vma))
2346 return false;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07002347 return !(vma->vm_flags & VM_NO_THP);
Bob Liufa475e52012-12-11 16:00:39 -08002348}
2349
Andrea Arcangeliba761492011-01-13 15:46:58 -08002350static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002351 unsigned long address,
2352 struct page **hpage,
2353 struct vm_area_struct *vma,
2354 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002355{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356 pmd_t *pmd, _pmd;
2357 pte_t *pte;
2358 pgtable_t pgtable;
2359 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002360 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002361 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002362 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002363 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002364 unsigned long mmun_start; /* For mmu_notifiers */
2365 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002366 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002367
2368 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002369
Michal Hocko3b363692015-04-15 16:13:29 -07002370 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002371 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b363692015-04-15 16:13:29 -07002372
Xiao Guangrong26234f32012-10-08 16:29:51 -07002373 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002374 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002375 if (!new_page) {
2376 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2377 goto out_nolock;
2378 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002379
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002380 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002381 result = SCAN_CGROUP_CHARGE_FAIL;
2382 goto out_nolock;
2383 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002384
2385 /*
2386 * Prevent all access to pagetables with the exception of
2387 * gup_fast later hanlded by the ptep_clear_flush and the VM
2388 * handled by the anon_vma lock + PG_lock.
2389 */
2390 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002391 if (unlikely(khugepaged_test_exit(mm))) {
2392 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002393 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002394 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002395
2396 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002397 if (!vma) {
2398 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002399 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002400 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002401 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2402 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002403 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2404 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002405 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002406 }
2407 if (!hugepage_vma_check(vma)) {
2408 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002409 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002410 }
Bob Liu62190492012-12-11 16:00:37 -08002411 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002412 if (!pmd) {
2413 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002414 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002415 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002417 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002418
2419 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002420 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002421
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002422 mmun_start = address;
2423 mmun_end = address + HPAGE_PMD_SIZE;
2424 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002425 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002426 /*
2427 * After this gup_fast can't run anymore. This also removes
2428 * any huge TLB entry from the CPU so we won't allow
2429 * huge and small TLB entries for the same virtual address
2430 * to avoid the risk of CPU bugs in that area.
2431 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002432 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002433 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002434 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002435
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002436 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002437 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002438 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002439
2440 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002441 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002442 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002443 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002444 /*
2445 * We can only use set_pmd_at when establishing
2446 * hugepmds and never for establishing regular pmds that
2447 * points to regular pagetables. Use pmd_populate for that
2448 */
2449 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002450 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002451 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002452 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002453 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002454 }
2455
2456 /*
2457 * All pages are isolated and locked so anon_vma rmap
2458 * can't run anymore.
2459 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002460 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002461
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002462 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002463 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002464 __SetPageUptodate(new_page);
2465 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002466
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002467 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2468 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002469
2470 /*
2471 * spin_lock() below is not the equivalent of smp_wmb(), so
2472 * this is needed to avoid the copy_huge_page writes to become
2473 * visible after the set_pmd_at() write.
2474 */
2475 smp_wmb();
2476
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002477 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002478 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002479 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002480 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002481 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002482 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002483 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002484 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002485 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002486
2487 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002488
Andrea Arcangeliba761492011-01-13 15:46:58 -08002489 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002490 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002491out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002492 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002493 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002494 return;
2495
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002496out_nolock:
2497 trace_mm_collapse_huge_page(mm, isolated, result);
2498 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002499out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002500 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002501 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002502}
2503
2504static int khugepaged_scan_pmd(struct mm_struct *mm,
2505 struct vm_area_struct *vma,
2506 unsigned long address,
2507 struct page **hpage)
2508{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002509 pmd_t *pmd;
2510 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002511 int ret = 0, none_or_zero = 0, result = 0;
2512 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002513 unsigned long _address;
2514 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002515 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002516 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002517
2518 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2519
Bob Liu62190492012-12-11 16:00:37 -08002520 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002521 if (!pmd) {
2522 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002523 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002524 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002525
Bob Liu9f1b8682013-11-12 15:07:37 -08002526 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002527 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2528 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2529 _pte++, _address += PAGE_SIZE) {
2530 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002531 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002532 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002533 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002534 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002535 } else {
2536 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002537 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002538 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002539 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002540 if (!pte_present(pteval)) {
2541 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002542 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002543 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002544 if (pte_write(pteval))
2545 writable = true;
2546
Andrea Arcangeliba761492011-01-13 15:46:58 -08002547 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002548 if (unlikely(!page)) {
2549 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002550 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002551 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002552
2553 /* TODO: teach khugepaged to collapse THP mapped with pte */
2554 if (PageCompound(page)) {
2555 result = SCAN_PAGE_COMPOUND;
2556 goto out_unmap;
2557 }
2558
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002559 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002560 * Record which node the original page is from and save this
2561 * information to khugepaged_node_load[].
2562 * Khupaged will allocate hugepage from the node has the max
2563 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002564 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002565 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002566 if (khugepaged_scan_abort(node)) {
2567 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002568 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002569 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002570 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002571 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002572 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002573 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002574 }
2575 if (PageLocked(page)) {
2576 result = SCAN_PAGE_LOCK;
2577 goto out_unmap;
2578 }
2579 if (!PageAnon(page)) {
2580 result = SCAN_PAGE_ANON;
2581 goto out_unmap;
2582 }
2583
Ebru Akagunduz10359212015-02-11 15:28:28 -08002584 /*
2585 * cannot use mapcount: can't collapse if there's a gup pin.
2586 * The page must only be referenced by the scanned process
2587 * and page swap cache.
2588 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002589 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2590 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002591 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002592 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002593 if (pte_young(pteval) ||
2594 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002595 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002596 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002597 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002598 if (writable) {
2599 if (referenced) {
2600 result = SCAN_SUCCEED;
2601 ret = 1;
2602 } else {
2603 result = SCAN_NO_REFERENCED_PAGE;
2604 }
2605 } else {
2606 result = SCAN_PAGE_RO;
2607 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002608out_unmap:
2609 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002610 if (ret) {
2611 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002612 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002613 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002614 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002615out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002616 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002617 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002618 return ret;
2619}
2620
2621static void collect_mm_slot(struct mm_slot *mm_slot)
2622{
2623 struct mm_struct *mm = mm_slot->mm;
2624
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002625 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002626
2627 if (khugepaged_test_exit(mm)) {
2628 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002629 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002630 list_del(&mm_slot->mm_node);
2631
2632 /*
2633 * Not strictly needed because the mm exited already.
2634 *
2635 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2636 */
2637
2638 /* khugepaged_mm_lock actually not necessary for the below */
2639 free_mm_slot(mm_slot);
2640 mmdrop(mm);
2641 }
2642}
2643
2644static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2645 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002646 __releases(&khugepaged_mm_lock)
2647 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002648{
2649 struct mm_slot *mm_slot;
2650 struct mm_struct *mm;
2651 struct vm_area_struct *vma;
2652 int progress = 0;
2653
2654 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002655 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002656
2657 if (khugepaged_scan.mm_slot)
2658 mm_slot = khugepaged_scan.mm_slot;
2659 else {
2660 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2661 struct mm_slot, mm_node);
2662 khugepaged_scan.address = 0;
2663 khugepaged_scan.mm_slot = mm_slot;
2664 }
2665 spin_unlock(&khugepaged_mm_lock);
2666
2667 mm = mm_slot->mm;
2668 down_read(&mm->mmap_sem);
2669 if (unlikely(khugepaged_test_exit(mm)))
2670 vma = NULL;
2671 else
2672 vma = find_vma(mm, khugepaged_scan.address);
2673
2674 progress++;
2675 for (; vma; vma = vma->vm_next) {
2676 unsigned long hstart, hend;
2677
2678 cond_resched();
2679 if (unlikely(khugepaged_test_exit(mm))) {
2680 progress++;
2681 break;
2682 }
Bob Liufa475e52012-12-11 16:00:39 -08002683 if (!hugepage_vma_check(vma)) {
2684skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002685 progress++;
2686 continue;
2687 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002688 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2689 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002690 if (hstart >= hend)
2691 goto skip;
2692 if (khugepaged_scan.address > hend)
2693 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002694 if (khugepaged_scan.address < hstart)
2695 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002696 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002697
2698 while (khugepaged_scan.address < hend) {
2699 int ret;
2700 cond_resched();
2701 if (unlikely(khugepaged_test_exit(mm)))
2702 goto breakouterloop;
2703
2704 VM_BUG_ON(khugepaged_scan.address < hstart ||
2705 khugepaged_scan.address + HPAGE_PMD_SIZE >
2706 hend);
2707 ret = khugepaged_scan_pmd(mm, vma,
2708 khugepaged_scan.address,
2709 hpage);
2710 /* move to next address */
2711 khugepaged_scan.address += HPAGE_PMD_SIZE;
2712 progress += HPAGE_PMD_NR;
2713 if (ret)
2714 /* we released mmap_sem so break loop */
2715 goto breakouterloop_mmap_sem;
2716 if (progress >= pages)
2717 goto breakouterloop;
2718 }
2719 }
2720breakouterloop:
2721 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2722breakouterloop_mmap_sem:
2723
2724 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002725 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002726 /*
2727 * Release the current mm_slot if this mm is about to die, or
2728 * if we scanned all vmas of this mm.
2729 */
2730 if (khugepaged_test_exit(mm) || !vma) {
2731 /*
2732 * Make sure that if mm_users is reaching zero while
2733 * khugepaged runs here, khugepaged_exit will find
2734 * mm_slot not pointing to the exiting mm.
2735 */
2736 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2737 khugepaged_scan.mm_slot = list_entry(
2738 mm_slot->mm_node.next,
2739 struct mm_slot, mm_node);
2740 khugepaged_scan.address = 0;
2741 } else {
2742 khugepaged_scan.mm_slot = NULL;
2743 khugepaged_full_scans++;
2744 }
2745
2746 collect_mm_slot(mm_slot);
2747 }
2748
2749 return progress;
2750}
2751
2752static int khugepaged_has_work(void)
2753{
2754 return !list_empty(&khugepaged_scan.mm_head) &&
2755 khugepaged_enabled();
2756}
2757
2758static int khugepaged_wait_event(void)
2759{
2760 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002761 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002762}
2763
Xiao Guangrongd5169042012-10-08 16:29:48 -07002764static void khugepaged_do_scan(void)
2765{
2766 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002767 unsigned int progress = 0, pass_through_head = 0;
2768 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002769 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002770
2771 barrier(); /* write khugepaged_pages_to_scan to local stack */
2772
2773 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002774 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002775 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002776
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002777 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002778
Jiri Kosinacd092412015-06-24 16:56:07 -07002779 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002780 break;
2781
Andrea Arcangeliba761492011-01-13 15:46:58 -08002782 spin_lock(&khugepaged_mm_lock);
2783 if (!khugepaged_scan.mm_slot)
2784 pass_through_head++;
2785 if (khugepaged_has_work() &&
2786 pass_through_head < 2)
2787 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002788 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002789 else
2790 progress = pages;
2791 spin_unlock(&khugepaged_mm_lock);
2792 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002793
Xiao Guangrongd5169042012-10-08 16:29:48 -07002794 if (!IS_ERR_OR_NULL(hpage))
2795 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002796}
2797
David Rientjesf0508972016-05-20 16:58:56 -07002798static bool khugepaged_should_wakeup(void)
2799{
2800 return kthread_should_stop() ||
2801 time_after_eq(jiffies, khugepaged_sleep_expire);
2802}
2803
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002804static void khugepaged_wait_work(void)
2805{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002806 if (khugepaged_has_work()) {
David Rientjesf0508972016-05-20 16:58:56 -07002807 const unsigned long scan_sleep_jiffies =
2808 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2809
2810 if (!scan_sleep_jiffies)
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002811 return;
2812
David Rientjesf0508972016-05-20 16:58:56 -07002813 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002814 wait_event_freezable_timeout(khugepaged_wait,
David Rientjesf0508972016-05-20 16:58:56 -07002815 khugepaged_should_wakeup(),
2816 scan_sleep_jiffies);
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002817 return;
2818 }
2819
2820 if (khugepaged_enabled())
2821 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2822}
2823
Andrea Arcangeliba761492011-01-13 15:46:58 -08002824static int khugepaged(void *none)
2825{
2826 struct mm_slot *mm_slot;
2827
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002828 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002829 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002830
Xiao Guangrongb7231782012-10-08 16:29:54 -07002831 while (!kthread_should_stop()) {
2832 khugepaged_do_scan();
2833 khugepaged_wait_work();
2834 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002835
2836 spin_lock(&khugepaged_mm_lock);
2837 mm_slot = khugepaged_scan.mm_slot;
2838 khugepaged_scan.mm_slot = NULL;
2839 if (mm_slot)
2840 collect_mm_slot(mm_slot);
2841 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002842 return 0;
2843}
2844
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002845static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2846 unsigned long haddr, pmd_t *pmd)
2847{
2848 struct mm_struct *mm = vma->vm_mm;
2849 pgtable_t pgtable;
2850 pmd_t _pmd;
2851 int i;
2852
2853 /* leave pmd empty until pte is filled */
2854 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2855
2856 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2857 pmd_populate(mm, &_pmd, pgtable);
2858
2859 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2860 pte_t *pte, entry;
2861 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2862 entry = pte_mkspecial(entry);
2863 pte = pte_offset_map(&_pmd, haddr);
2864 VM_BUG_ON(!pte_none(*pte));
2865 set_pte_at(mm, haddr, pte, entry);
2866 pte_unmap(pte);
2867 }
2868 smp_wmb(); /* make pte visible before pmd */
2869 pmd_populate(mm, pmd, pgtable);
2870 put_huge_zero_page();
2871}
2872
2873static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002874 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002875{
2876 struct mm_struct *mm = vma->vm_mm;
2877 struct page *page;
2878 pgtable_t pgtable;
2879 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002880 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002881 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002882 int i;
2883
2884 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2885 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2886 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002887 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002888
2889 count_vm_event(THP_SPLIT_PMD);
2890
2891 if (vma_is_dax(vma)) {
2892 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2893 if (is_huge_zero_pmd(_pmd))
2894 put_huge_zero_page();
2895 return;
2896 } else if (is_huge_zero_pmd(*pmd)) {
2897 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2898 }
2899
2900 page = pmd_page(*pmd);
2901 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002902 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002903 write = pmd_write(*pmd);
2904 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002905 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002906
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05302907 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002908 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2909 pmd_populate(mm, &_pmd, pgtable);
2910
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002911 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002912 pte_t entry, *pte;
2913 /*
2914 * Note that NUMA hinting access restrictions are not
2915 * transferred to avoid any possibility of altering
2916 * permissions across VMAs.
2917 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002918 if (freeze) {
2919 swp_entry_t swp_entry;
2920 swp_entry = make_migration_entry(page + i, write);
2921 entry = swp_entry_to_pte(swp_entry);
2922 } else {
2923 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002924 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002925 if (!write)
2926 entry = pte_wrprotect(entry);
2927 if (!young)
2928 entry = pte_mkold(entry);
2929 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002930 if (dirty)
2931 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002932 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002933 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002934 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002935 atomic_inc(&page[i]._mapcount);
2936 pte_unmap(pte);
2937 }
2938
2939 /*
2940 * Set PG_double_map before dropping compound_mapcount to avoid
2941 * false-negative page_mapped().
2942 */
2943 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2944 for (i = 0; i < HPAGE_PMD_NR; i++)
2945 atomic_inc(&page[i]._mapcount);
2946 }
2947
2948 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2949 /* Last compound_mapcount is gone. */
2950 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2951 if (TestClearPageDoubleMap(page)) {
2952 /* No need in mapcount reference anymore */
2953 for (i = 0; i < HPAGE_PMD_NR; i++)
2954 atomic_dec(&page[i]._mapcount);
2955 }
2956 }
2957
2958 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002959 /*
2960 * Up to this point the pmd is present and huge and userland has the
2961 * whole access to the hugepage during the split (which happens in
2962 * place). If we overwrite the pmd with the not-huge version pointing
2963 * to the pte here (which of course we could if all CPUs were bug
2964 * free), userland could trigger a small page size TLB miss on the
2965 * small sized TLB while the hugepage TLB entry is still established in
2966 * the huge TLB. Some CPU doesn't like that.
2967 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2968 * 383 on page 93. Intel should be safe but is also warns that it's
2969 * only safe if the permission and cache attributes of the two entries
2970 * loaded in the two TLB is identical (which should be the case here).
2971 * But it is generally safer to never allow small and huge TLB entries
2972 * for the same virtual address to be loaded simultaneously. So instead
2973 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2974 * current pmd notpresent (atomically because here the pmd_trans_huge
2975 * and pmd_trans_splitting must remain set at all times on the pmd
2976 * until the split is complete for this pmd), then we flush the SMP TLB
2977 * and finally we write the non-huge version of the pmd entry with
2978 * pmd_populate.
2979 */
2980 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002981 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002982
2983 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002984 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002985 page_remove_rmap(page + i, false);
2986 put_page(page + i);
2987 }
2988 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002989}
2990
2991void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002992 unsigned long address, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002993{
2994 spinlock_t *ptl;
2995 struct mm_struct *mm = vma->vm_mm;
2996 unsigned long haddr = address & HPAGE_PMD_MASK;
2997
2998 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2999 ptl = pmd_lock(mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003000 if (pmd_trans_huge(*pmd)) {
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003001 struct page *page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003002 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003003 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08003004 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003005 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003006 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003007out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003008 spin_unlock(ptl);
3009 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
3010}
3011
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003012void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3013 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003014{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003015 pgd_t *pgd;
3016 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003017 pmd_t *pmd;
3018
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003019 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003020 if (!pgd_present(*pgd))
3021 return;
3022
3023 pud = pud_offset(pgd, address);
3024 if (!pud_present(*pud))
3025 return;
3026
3027 pmd = pmd_offset(pud, address);
Dan Williams5c7fb562016-01-15 16:56:52 -08003028 if (!pmd_present(*pmd) || (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003029 return;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003030
3031 /*
3032 * If caller asks to setup a migration entries, we need a page to check
3033 * pmd against. Otherwise we can end up replacing wrong page.
3034 */
3035 VM_BUG_ON(freeze && !page);
3036 if (page && page != pmd_page(*pmd))
3037 return;
3038
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003039 /*
Andrea Arcangelid5ee7c32016-05-20 16:58:27 -07003040 * Caller holds the mmap_sem write mode or the anon_vma lock,
3041 * so a huge pmd cannot materialize from under us (khugepaged
3042 * holds both the mmap_sem write mode and the anon_vma lock
3043 * write mode).
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003044 */
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003045 __split_huge_pmd(vma, pmd, address, freeze);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003046}
3047
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003048void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003049 unsigned long start,
3050 unsigned long end,
3051 long adjust_next)
3052{
3053 /*
3054 * If the new start address isn't hpage aligned and it could
3055 * previously contain an hugepage: check if we need to split
3056 * an huge pmd.
3057 */
3058 if (start & ~HPAGE_PMD_MASK &&
3059 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3060 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003061 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003062
3063 /*
3064 * If the new end address isn't hpage aligned and it could
3065 * previously contain an hugepage: check if we need to split
3066 * an huge pmd.
3067 */
3068 if (end & ~HPAGE_PMD_MASK &&
3069 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3070 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003071 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003072
3073 /*
3074 * If we're also updating the vma->vm_next->vm_start, if the new
3075 * vm_next->vm_start isn't page aligned and it could previously
3076 * contain an hugepage: check if we need to split an huge pmd.
3077 */
3078 if (adjust_next > 0) {
3079 struct vm_area_struct *next = vma->vm_next;
3080 unsigned long nstart = next->vm_start;
3081 nstart += adjust_next << PAGE_SHIFT;
3082 if (nstart & ~HPAGE_PMD_MASK &&
3083 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3084 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003085 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003086 }
3087}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003088
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003089static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003090{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003091 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3092 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3093 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003094
3095 VM_BUG_ON_PAGE(!PageHead(page), page);
3096
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003097 /* We only need TTU_SPLIT_HUGE_PMD once */
3098 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3099 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3100 /* Cut short if the page is unmapped */
3101 if (page_count(page) == 1)
3102 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003103
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003104 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003105 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003106 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003107}
3108
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003109static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003110{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003111 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003112
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003113 for (i = 0; i < HPAGE_PMD_NR; i++)
3114 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003115}
3116
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003117static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003118 struct lruvec *lruvec, struct list_head *list)
3119{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003120 struct page *page_tail = head + tail;
3121
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003122 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003123 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003124
3125 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003126 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003127 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003128 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003129 * would then run atomic_set() concurrently with
3130 * get_page_unless_zero(), and atomic_set() is implemented in C not
3131 * using locked ops. spin_unlock on x86 sometime uses locked ops
3132 * because of PPro errata 66, 92, so unless somebody can guarantee
3133 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003134 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003135 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003136 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003137
3138 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3139 page_tail->flags |= (head->flags &
3140 ((1L << PG_referenced) |
3141 (1L << PG_swapbacked) |
3142 (1L << PG_mlocked) |
3143 (1L << PG_uptodate) |
3144 (1L << PG_active) |
3145 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003146 (1L << PG_unevictable) |
3147 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003148
3149 /*
3150 * After clearing PageTail the gup refcount can be released.
3151 * Page flags also must be visible before we make the page non-compound.
3152 */
3153 smp_wmb();
3154
3155 clear_compound_head(page_tail);
3156
3157 if (page_is_young(head))
3158 set_page_young(page_tail);
3159 if (page_is_idle(head))
3160 set_page_idle(page_tail);
3161
3162 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003163 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003164 page_tail);
3165 page_tail->mapping = head->mapping;
3166
3167 page_tail->index = head->index + tail;
3168 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3169 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003170}
3171
3172static void __split_huge_page(struct page *page, struct list_head *list)
3173{
3174 struct page *head = compound_head(page);
3175 struct zone *zone = page_zone(head);
3176 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003177 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003178
3179 /* prevent PageLRU to go away from under us, and freeze lru stats */
3180 spin_lock_irq(&zone->lru_lock);
3181 lruvec = mem_cgroup_page_lruvec(head, zone);
3182
3183 /* complete memcg works before add pages to LRU */
3184 mem_cgroup_split_huge_fixup(head);
3185
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003186 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003187 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003188
3189 ClearPageCompound(head);
3190 spin_unlock_irq(&zone->lru_lock);
3191
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003192 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003193
3194 for (i = 0; i < HPAGE_PMD_NR; i++) {
3195 struct page *subpage = head + i;
3196 if (subpage == page)
3197 continue;
3198 unlock_page(subpage);
3199
3200 /*
3201 * Subpages may be freed if there wasn't any mapping
3202 * like if add_to_swap() is running on a lru page that
3203 * had its mapping zapped. And freeing these pages
3204 * requires taking the lru_lock so we do the put_page
3205 * of the tail pages after the split is complete.
3206 */
3207 put_page(subpage);
3208 }
3209}
3210
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003211int total_mapcount(struct page *page)
3212{
3213 int i, ret;
3214
3215 VM_BUG_ON_PAGE(PageTail(page), page);
3216
3217 if (likely(!PageCompound(page)))
3218 return atomic_read(&page->_mapcount) + 1;
3219
3220 ret = compound_mapcount(page);
3221 if (PageHuge(page))
3222 return ret;
3223 for (i = 0; i < HPAGE_PMD_NR; i++)
3224 ret += atomic_read(&page[i]._mapcount) + 1;
3225 if (PageDoubleMap(page))
3226 ret -= HPAGE_PMD_NR;
3227 return ret;
3228}
3229
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003230/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003231 * This calculates accurately how many mappings a transparent hugepage
3232 * has (unlike page_mapcount() which isn't fully accurate). This full
3233 * accuracy is primarily needed to know if copy-on-write faults can
3234 * reuse the page and change the mapping to read-write instead of
3235 * copying them. At the same time this returns the total_mapcount too.
3236 *
3237 * The function returns the highest mapcount any one of the subpages
3238 * has. If the return value is one, even if different processes are
3239 * mapping different subpages of the transparent hugepage, they can
3240 * all reuse it, because each process is reusing a different subpage.
3241 *
3242 * The total_mapcount is instead counting all virtual mappings of the
3243 * subpages. If the total_mapcount is equal to "one", it tells the
3244 * caller all mappings belong to the same "mm" and in turn the
3245 * anon_vma of the transparent hugepage can become the vma->anon_vma
3246 * local one as no other process may be mapping any of the subpages.
3247 *
3248 * It would be more accurate to replace page_mapcount() with
3249 * page_trans_huge_mapcount(), however we only use
3250 * page_trans_huge_mapcount() in the copy-on-write faults where we
3251 * need full accuracy to avoid breaking page pinning, because
3252 * page_trans_huge_mapcount() is slower than page_mapcount().
3253 */
3254int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3255{
3256 int i, ret, _total_mapcount, mapcount;
3257
3258 /* hugetlbfs shouldn't call it */
3259 VM_BUG_ON_PAGE(PageHuge(page), page);
3260
3261 if (likely(!PageTransCompound(page))) {
3262 mapcount = atomic_read(&page->_mapcount) + 1;
3263 if (total_mapcount)
3264 *total_mapcount = mapcount;
3265 return mapcount;
3266 }
3267
3268 page = compound_head(page);
3269
3270 _total_mapcount = ret = 0;
3271 for (i = 0; i < HPAGE_PMD_NR; i++) {
3272 mapcount = atomic_read(&page[i]._mapcount) + 1;
3273 ret = max(ret, mapcount);
3274 _total_mapcount += mapcount;
3275 }
3276 if (PageDoubleMap(page)) {
3277 ret -= 1;
3278 _total_mapcount -= HPAGE_PMD_NR;
3279 }
3280 mapcount = compound_mapcount(page);
3281 ret += mapcount;
3282 _total_mapcount += mapcount;
3283 if (total_mapcount)
3284 *total_mapcount = _total_mapcount;
3285 return ret;
3286}
3287
3288/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003289 * This function splits huge page into normal pages. @page can point to any
3290 * subpage of huge page to split. Split doesn't change the position of @page.
3291 *
3292 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3293 * The huge page must be locked.
3294 *
3295 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3296 *
3297 * Both head page and tail pages will inherit mapping, flags, and so on from
3298 * the hugepage.
3299 *
3300 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3301 * they are not mapped.
3302 *
3303 * Returns 0 if the hugepage is split successfully.
3304 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3305 * us.
3306 */
3307int split_huge_page_to_list(struct page *page, struct list_head *list)
3308{
3309 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003310 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003311 struct anon_vma *anon_vma;
3312 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003313 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003314 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003315
3316 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3317 VM_BUG_ON_PAGE(!PageAnon(page), page);
3318 VM_BUG_ON_PAGE(!PageLocked(page), page);
3319 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3320 VM_BUG_ON_PAGE(!PageCompound(page), page);
3321
3322 /*
3323 * The caller does not necessarily hold an mmap_sem that would prevent
3324 * the anon_vma disappearing so we first we take a reference to it
3325 * and then lock the anon_vma for write. This is similar to
3326 * page_lock_anon_vma_read except the write lock is taken to serialise
3327 * against parallel split or collapse operations.
3328 */
3329 anon_vma = page_get_anon_vma(head);
3330 if (!anon_vma) {
3331 ret = -EBUSY;
3332 goto out;
3333 }
3334 anon_vma_lock_write(anon_vma);
3335
3336 /*
3337 * Racy check if we can split the page, before freeze_page() will
3338 * split PMDs
3339 */
3340 if (total_mapcount(head) != page_count(head) - 1) {
3341 ret = -EBUSY;
3342 goto out_unlock;
3343 }
3344
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003345 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003346 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003347 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3348
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003349 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3350 if (mlocked)
3351 lru_add_drain();
3352
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003353 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003354 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003355 count = page_count(head);
3356 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003357 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003358 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003359 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003360 list_del(page_deferred_list(head));
3361 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003362 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003363 __split_huge_page(page, list);
3364 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003365 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003366 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003367 pr_alert("total_mapcount: %u, page_count(): %u\n",
3368 mapcount, count);
3369 if (PageTail(page))
3370 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003371 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003372 BUG();
3373 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003374 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003375 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003376 ret = -EBUSY;
3377 }
3378
3379out_unlock:
3380 anon_vma_unlock_write(anon_vma);
3381 put_anon_vma(anon_vma);
3382out:
3383 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3384 return ret;
3385}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003386
3387void free_transhuge_page(struct page *page)
3388{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003389 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003390 unsigned long flags;
3391
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003392 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003393 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003394 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003395 list_del(page_deferred_list(page));
3396 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003397 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003398 free_compound_page(page);
3399}
3400
3401void deferred_split_huge_page(struct page *page)
3402{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003403 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003404 unsigned long flags;
3405
3406 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3407
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003408 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003409 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003410 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003411 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3412 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003413 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003414 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003415}
3416
3417static unsigned long deferred_split_count(struct shrinker *shrink,
3418 struct shrink_control *sc)
3419{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003420 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003421 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003422}
3423
3424static unsigned long deferred_split_scan(struct shrinker *shrink,
3425 struct shrink_control *sc)
3426{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003427 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003428 unsigned long flags;
3429 LIST_HEAD(list), *pos, *next;
3430 struct page *page;
3431 int split = 0;
3432
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003433 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003434 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003435 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003436 page = list_entry((void *)pos, struct page, mapping);
3437 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003438 if (get_page_unless_zero(page)) {
3439 list_move(page_deferred_list(page), &list);
3440 } else {
3441 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003442 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003443 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003444 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003445 if (!--sc->nr_to_scan)
3446 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003447 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003448 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003449
3450 list_for_each_safe(pos, next, &list) {
3451 page = list_entry((void *)pos, struct page, mapping);
3452 lock_page(page);
3453 /* split_huge_page() removes page from list on success */
3454 if (!split_huge_page(page))
3455 split++;
3456 unlock_page(page);
3457 put_page(page);
3458 }
3459
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003460 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3461 list_splice_tail(&list, &pgdata->split_queue);
3462 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003463
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003464 /*
3465 * Stop shrinker if we didn't split any page, but the queue is empty.
3466 * This can happen if pages were freed under us.
3467 */
3468 if (!split && list_empty(&pgdata->split_queue))
3469 return SHRINK_STOP;
3470 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003471}
3472
3473static struct shrinker deferred_split_shrinker = {
3474 .count_objects = deferred_split_count,
3475 .scan_objects = deferred_split_scan,
3476 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003477 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003478};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003479
3480#ifdef CONFIG_DEBUG_FS
3481static int split_huge_pages_set(void *data, u64 val)
3482{
3483 struct zone *zone;
3484 struct page *page;
3485 unsigned long pfn, max_zone_pfn;
3486 unsigned long total = 0, split = 0;
3487
3488 if (val != 1)
3489 return -EINVAL;
3490
3491 for_each_populated_zone(zone) {
3492 max_zone_pfn = zone_end_pfn(zone);
3493 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3494 if (!pfn_valid(pfn))
3495 continue;
3496
3497 page = pfn_to_page(pfn);
3498 if (!get_page_unless_zero(page))
3499 continue;
3500
3501 if (zone != page_zone(page))
3502 goto next;
3503
3504 if (!PageHead(page) || !PageAnon(page) ||
3505 PageHuge(page))
3506 goto next;
3507
3508 total++;
3509 lock_page(page);
3510 if (!split_huge_page(page))
3511 split++;
3512 unlock_page(page);
3513next:
3514 put_page(page);
3515 }
3516 }
3517
Yang Shi145bdaa2016-05-05 16:22:00 -07003518 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003519
3520 return 0;
3521}
3522DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3523 "%llu\n");
3524
3525static int __init split_huge_pages_debugfs(void)
3526{
3527 void *ret;
3528
Yang Shi145bdaa2016-05-05 16:22:00 -07003529 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003530 &split_huge_pages_fops);
3531 if (!ret)
3532 pr_warn("Failed to create split_huge_pages in debugfs");
3533 return 0;
3534}
3535late_initcall(split_huge_pages_debugfs);
3536#endif