blob: b1cf73bc3b12969f00740031ac60f28840ac212d [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
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080081 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080082 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
83 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080084
85/* default scan 8*512 pte (or vmas) every 30 second */
86static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
87static unsigned int khugepaged_pages_collapsed;
88static unsigned int khugepaged_full_scans;
89static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
90/* during fragmentation poll the hugepage allocator once every minute */
91static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
92static struct task_struct *khugepaged_thread __read_mostly;
93static DEFINE_MUTEX(khugepaged_mutex);
94static DEFINE_SPINLOCK(khugepaged_mm_lock);
95static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
96/*
97 * default collapse hugepages if there is at least one pte mapped like
98 * it would have happened if the vma was large enough during page
99 * fault.
100 */
101static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
102
103static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800104static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700105static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800106
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800107#define MM_SLOTS_HASH_BITS 10
108static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
109
Andrea Arcangeliba761492011-01-13 15:46:58 -0800110static struct kmem_cache *mm_slot_cache __read_mostly;
111
112/**
113 * struct mm_slot - hash lookup from mm to mm_slot
114 * @hash: hash collision list
115 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
116 * @mm: the mm that this information is valid for
117 */
118struct mm_slot {
119 struct hlist_node hash;
120 struct list_head mm_node;
121 struct mm_struct *mm;
122};
123
124/**
125 * struct khugepaged_scan - cursor for scanning
126 * @mm_head: the head of the mm list to scan
127 * @mm_slot: the current mm_slot we are scanning
128 * @address: the next address inside that to be scanned
129 *
130 * There is only the one khugepaged_scan instance of this cursor structure.
131 */
132struct khugepaged_scan {
133 struct list_head mm_head;
134 struct mm_slot *mm_slot;
135 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700136};
137static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800138 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
139};
140
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800141static DEFINE_SPINLOCK(split_queue_lock);
142static LIST_HEAD(split_queue);
143static unsigned long split_queue_len;
144static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700146static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800147{
148 struct zone *zone;
149 int nr_zones = 0;
150 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800151
Andrea Arcangelif0005652011-01-13 15:47:04 -0800152 for_each_populated_zone(zone)
153 nr_zones++;
154
Mel Gorman974a7862015-11-06 16:28:34 -0800155 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800156 recommended_min = pageblock_nr_pages * nr_zones * 2;
157
158 /*
159 * Make sure that on average at least two pageblocks are almost free
160 * of another type, one for a migratetype to fall back to and a
161 * second to avoid subsequent fallbacks of other types There are 3
162 * MIGRATE_TYPES we care about.
163 */
164 recommended_min += pageblock_nr_pages * nr_zones *
165 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
166
167 /* don't ever allow to reserve more than 5% of the lowmem */
168 recommended_min = min(recommended_min,
169 (unsigned long) nr_free_buffer_pages() / 20);
170 recommended_min <<= (PAGE_SHIFT-10);
171
Han Pingtian42aa83c2014-01-23 15:53:28 -0800172 if (recommended_min > min_free_kbytes) {
173 if (user_min_free_kbytes >= 0)
174 pr_info("raising min_free_kbytes from %d to %lu "
175 "to help transparent hugepage allocations\n",
176 min_free_kbytes, recommended_min);
177
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800179 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800180 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800181}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800182
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700183static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800184{
185 int err = 0;
186 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800187 if (!khugepaged_thread)
188 khugepaged_thread = kthread_run(khugepaged, NULL,
189 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530190 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700191 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800192 err = PTR_ERR(khugepaged_thread);
193 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700194 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800195 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700196
197 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800198 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800199
200 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700201 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700202 kthread_stop(khugepaged_thread);
203 khugepaged_thread = NULL;
204 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700205fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800206 return err;
207}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800208
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800209static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800210struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800211
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700212struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800213{
214 struct page *zero_page;
215retry:
216 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700217 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800218
219 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
220 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800221 if (!zero_page) {
222 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700223 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800224 }
225 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800226 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700227 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800228 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700229 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800230 goto retry;
231 }
232
233 /* We take additional reference here. It will be put back by shrinker */
234 atomic_set(&huge_zero_refcount, 2);
235 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700236 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800237}
238
239static void put_huge_zero_page(void)
240{
241 /*
242 * Counter should never go to zero here. Only shrinker can put
243 * last reference.
244 */
245 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
246}
247
Glauber Costa48896462013-08-28 10:18:15 +1000248static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
249 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800250{
Glauber Costa48896462013-08-28 10:18:15 +1000251 /* we can free zero page only if last reference remains */
252 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
253}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800254
Glauber Costa48896462013-08-28 10:18:15 +1000255static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
256 struct shrink_control *sc)
257{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800258 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700259 struct page *zero_page = xchg(&huge_zero_page, NULL);
260 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700261 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000262 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800263 }
264
265 return 0;
266}
267
268static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000269 .count_objects = shrink_huge_zero_page_count,
270 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800271 .seeks = DEFAULT_SEEKS,
272};
273
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800274#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800275
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800276static ssize_t double_flag_show(struct kobject *kobj,
277 struct kobj_attribute *attr, char *buf,
278 enum transparent_hugepage_flag enabled,
279 enum transparent_hugepage_flag req_madv)
280{
281 if (test_bit(enabled, &transparent_hugepage_flags)) {
282 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
283 return sprintf(buf, "[always] madvise never\n");
284 } else if (test_bit(req_madv, &transparent_hugepage_flags))
285 return sprintf(buf, "always [madvise] never\n");
286 else
287 return sprintf(buf, "always madvise [never]\n");
288}
289static ssize_t double_flag_store(struct kobject *kobj,
290 struct kobj_attribute *attr,
291 const char *buf, size_t count,
292 enum transparent_hugepage_flag enabled,
293 enum transparent_hugepage_flag req_madv)
294{
295 if (!memcmp("always", buf,
296 min(sizeof("always")-1, count))) {
297 set_bit(enabled, &transparent_hugepage_flags);
298 clear_bit(req_madv, &transparent_hugepage_flags);
299 } else if (!memcmp("madvise", buf,
300 min(sizeof("madvise")-1, count))) {
301 clear_bit(enabled, &transparent_hugepage_flags);
302 set_bit(req_madv, &transparent_hugepage_flags);
303 } else if (!memcmp("never", buf,
304 min(sizeof("never")-1, count))) {
305 clear_bit(enabled, &transparent_hugepage_flags);
306 clear_bit(req_madv, &transparent_hugepage_flags);
307 } else
308 return -EINVAL;
309
310 return count;
311}
312
313static ssize_t enabled_show(struct kobject *kobj,
314 struct kobj_attribute *attr, char *buf)
315{
316 return double_flag_show(kobj, attr, buf,
317 TRANSPARENT_HUGEPAGE_FLAG,
318 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
319}
320static ssize_t enabled_store(struct kobject *kobj,
321 struct kobj_attribute *attr,
322 const char *buf, size_t count)
323{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800324 ssize_t ret;
325
326 ret = double_flag_store(kobj, attr, buf, count,
327 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{
384 return double_flag_show(kobj, attr, buf,
385 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
386 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
387}
388static ssize_t defrag_store(struct kobject *kobj,
389 struct kobj_attribute *attr,
390 const char *buf, size_t count)
391{
392 return double_flag_store(kobj, attr, buf, count,
393 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
394 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
395}
396static struct kobj_attribute defrag_attr =
397 __ATTR(defrag, 0644, defrag_show, defrag_store);
398
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800399static ssize_t use_zero_page_show(struct kobject *kobj,
400 struct kobj_attribute *attr, char *buf)
401{
402 return single_flag_show(kobj, attr, buf,
403 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
404}
405static ssize_t use_zero_page_store(struct kobject *kobj,
406 struct kobj_attribute *attr, const char *buf, size_t count)
407{
408 return single_flag_store(kobj, attr, buf, count,
409 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
410}
411static struct kobj_attribute use_zero_page_attr =
412 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800413#ifdef CONFIG_DEBUG_VM
414static ssize_t debug_cow_show(struct kobject *kobj,
415 struct kobj_attribute *attr, char *buf)
416{
417 return single_flag_show(kobj, attr, buf,
418 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
419}
420static ssize_t debug_cow_store(struct kobject *kobj,
421 struct kobj_attribute *attr,
422 const char *buf, size_t count)
423{
424 return single_flag_store(kobj, attr, buf, count,
425 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
426}
427static struct kobj_attribute debug_cow_attr =
428 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
429#endif /* CONFIG_DEBUG_VM */
430
431static struct attribute *hugepage_attr[] = {
432 &enabled_attr.attr,
433 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800434 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800435#ifdef CONFIG_DEBUG_VM
436 &debug_cow_attr.attr,
437#endif
438 NULL,
439};
440
441static struct attribute_group hugepage_attr_group = {
442 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800443};
444
445static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
446 struct kobj_attribute *attr,
447 char *buf)
448{
449 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
450}
451
452static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
453 struct kobj_attribute *attr,
454 const char *buf, size_t count)
455{
456 unsigned long msecs;
457 int err;
458
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700459 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800460 if (err || msecs > UINT_MAX)
461 return -EINVAL;
462
463 khugepaged_scan_sleep_millisecs = msecs;
464 wake_up_interruptible(&khugepaged_wait);
465
466 return count;
467}
468static struct kobj_attribute scan_sleep_millisecs_attr =
469 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
470 scan_sleep_millisecs_store);
471
472static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
473 struct kobj_attribute *attr,
474 char *buf)
475{
476 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
477}
478
479static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
480 struct kobj_attribute *attr,
481 const char *buf, size_t count)
482{
483 unsigned long msecs;
484 int err;
485
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700486 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800487 if (err || msecs > UINT_MAX)
488 return -EINVAL;
489
490 khugepaged_alloc_sleep_millisecs = msecs;
491 wake_up_interruptible(&khugepaged_wait);
492
493 return count;
494}
495static struct kobj_attribute alloc_sleep_millisecs_attr =
496 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
497 alloc_sleep_millisecs_store);
498
499static ssize_t pages_to_scan_show(struct kobject *kobj,
500 struct kobj_attribute *attr,
501 char *buf)
502{
503 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
504}
505static ssize_t pages_to_scan_store(struct kobject *kobj,
506 struct kobj_attribute *attr,
507 const char *buf, size_t count)
508{
509 int err;
510 unsigned long pages;
511
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700512 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800513 if (err || !pages || pages > UINT_MAX)
514 return -EINVAL;
515
516 khugepaged_pages_to_scan = pages;
517
518 return count;
519}
520static struct kobj_attribute pages_to_scan_attr =
521 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
522 pages_to_scan_store);
523
524static ssize_t pages_collapsed_show(struct kobject *kobj,
525 struct kobj_attribute *attr,
526 char *buf)
527{
528 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
529}
530static struct kobj_attribute pages_collapsed_attr =
531 __ATTR_RO(pages_collapsed);
532
533static ssize_t full_scans_show(struct kobject *kobj,
534 struct kobj_attribute *attr,
535 char *buf)
536{
537 return sprintf(buf, "%u\n", khugepaged_full_scans);
538}
539static struct kobj_attribute full_scans_attr =
540 __ATTR_RO(full_scans);
541
542static ssize_t khugepaged_defrag_show(struct kobject *kobj,
543 struct kobj_attribute *attr, char *buf)
544{
545 return single_flag_show(kobj, attr, buf,
546 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
547}
548static ssize_t khugepaged_defrag_store(struct kobject *kobj,
549 struct kobj_attribute *attr,
550 const char *buf, size_t count)
551{
552 return single_flag_store(kobj, attr, buf, count,
553 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
554}
555static struct kobj_attribute khugepaged_defrag_attr =
556 __ATTR(defrag, 0644, khugepaged_defrag_show,
557 khugepaged_defrag_store);
558
559/*
560 * max_ptes_none controls if khugepaged should collapse hugepages over
561 * any unmapped ptes in turn potentially increasing the memory
562 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
563 * reduce the available free memory in the system as it
564 * runs. Increasing max_ptes_none will instead potentially reduce the
565 * free memory in the system during the khugepaged scan.
566 */
567static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
568 struct kobj_attribute *attr,
569 char *buf)
570{
571 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
572}
573static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
574 struct kobj_attribute *attr,
575 const char *buf, size_t count)
576{
577 int err;
578 unsigned long max_ptes_none;
579
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700580 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800581 if (err || max_ptes_none > HPAGE_PMD_NR-1)
582 return -EINVAL;
583
584 khugepaged_max_ptes_none = max_ptes_none;
585
586 return count;
587}
588static struct kobj_attribute khugepaged_max_ptes_none_attr =
589 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
590 khugepaged_max_ptes_none_store);
591
592static struct attribute *khugepaged_attr[] = {
593 &khugepaged_defrag_attr.attr,
594 &khugepaged_max_ptes_none_attr.attr,
595 &pages_to_scan_attr.attr,
596 &pages_collapsed_attr.attr,
597 &full_scans_attr.attr,
598 &scan_sleep_millisecs_attr.attr,
599 &alloc_sleep_millisecs_attr.attr,
600 NULL,
601};
602
603static struct attribute_group khugepaged_attr_group = {
604 .attrs = khugepaged_attr,
605 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800606};
Shaohua Li569e5592012-01-12 17:19:11 -0800607
608static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
609{
610 int err;
611
612 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
613 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700614 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800615 return -ENOMEM;
616 }
617
618 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
619 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700620 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800621 goto delete_obj;
622 }
623
624 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
625 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700626 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800627 goto remove_hp_group;
628 }
629
630 return 0;
631
632remove_hp_group:
633 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
634delete_obj:
635 kobject_put(*hugepage_kobj);
636 return err;
637}
638
639static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
640{
641 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
642 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
643 kobject_put(hugepage_kobj);
644}
645#else
646static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
647{
648 return 0;
649}
650
651static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
652{
653}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800654#endif /* CONFIG_SYSFS */
655
656static int __init hugepage_init(void)
657{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800658 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800659 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800660
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800661 if (!has_transparent_hugepage()) {
662 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800663 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800664 }
665
Shaohua Li569e5592012-01-12 17:19:11 -0800666 err = hugepage_init_sysfs(&hugepage_kobj);
667 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700668 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800669
670 err = khugepaged_slab_init();
671 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700672 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800673
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700674 err = register_shrinker(&huge_zero_page_shrinker);
675 if (err)
676 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800677 err = register_shrinker(&deferred_split_shrinker);
678 if (err)
679 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800680
Rik van Riel97562cd2011-01-13 15:47:12 -0800681 /*
682 * By default disable transparent hugepages on smaller systems,
683 * where the extra memory used could hurt more than TLB overhead
684 * is likely to save. The admin can still enable it through /sys.
685 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700686 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800687 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700688 return 0;
689 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800690
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700691 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700692 if (err)
693 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800694
Shaohua Li569e5592012-01-12 17:19:11 -0800695 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700696err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800697 unregister_shrinker(&deferred_split_shrinker);
698err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700699 unregister_shrinker(&huge_zero_page_shrinker);
700err_hzp_shrinker:
701 khugepaged_slab_exit();
702err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800703 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700704err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800705 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800706}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800707subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800708
709static int __init setup_transparent_hugepage(char *str)
710{
711 int ret = 0;
712 if (!str)
713 goto out;
714 if (!strcmp(str, "always")) {
715 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
716 &transparent_hugepage_flags);
717 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
718 &transparent_hugepage_flags);
719 ret = 1;
720 } else if (!strcmp(str, "madvise")) {
721 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
722 &transparent_hugepage_flags);
723 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
724 &transparent_hugepage_flags);
725 ret = 1;
726 } else if (!strcmp(str, "never")) {
727 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
728 &transparent_hugepage_flags);
729 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
730 &transparent_hugepage_flags);
731 ret = 1;
732 }
733out:
734 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700735 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800736 return ret;
737}
738__setup("transparent_hugepage=", setup_transparent_hugepage);
739
Mel Gormanb32967f2012-11-19 12:35:47 +0000740pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800741{
742 if (likely(vma->vm_flags & VM_WRITE))
743 pmd = pmd_mkwrite(pmd);
744 return pmd;
745}
746
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700747static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800748{
749 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700750 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800751 entry = pmd_mkhuge(entry);
752 return entry;
753}
754
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800755static inline struct list_head *page_deferred_list(struct page *page)
756{
757 /*
758 * ->lru in the tail pages is occupied by compound_head.
759 * Let's use ->mapping + ->index in the second tail page as list_head.
760 */
761 return (struct list_head *)&page[2].mapping;
762}
763
764void prep_transhuge_page(struct page *page)
765{
766 /*
767 * we use page->mapping and page->indexlru in second tail page
768 * as list_head: assuming THP order >= 2
769 */
770 BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
771
772 INIT_LIST_HEAD(page_deferred_list(page));
773 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
774}
775
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800776static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
777 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700778 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700779 struct page *page, gfp_t gfp,
780 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800781{
Johannes Weiner00501b52014-08-08 14:19:20 -0700782 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800783 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800784 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700785 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800786
Sasha Levin309381fea2014-01-23 15:52:54 -0800787 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700788
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800789 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700790 put_page(page);
791 count_vm_event(THP_FAULT_FALLBACK);
792 return VM_FAULT_FALLBACK;
793 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800794
Johannes Weiner00501b52014-08-08 14:19:20 -0700795 pgtable = pte_alloc_one(mm, haddr);
796 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800797 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700798 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700799 return VM_FAULT_OOM;
800 }
801
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800802 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700803 /*
804 * The memory barrier inside __SetPageUptodate makes sure that
805 * clear_huge_page writes become visible before the set_pmd_at()
806 * write.
807 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800808 __SetPageUptodate(page);
809
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800810 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800811 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800812 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800813 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800814 put_page(page);
815 pte_free(mm, pgtable);
816 } else {
817 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700818
819 /* Deliver the page fault to userland */
820 if (userfaultfd_missing(vma)) {
821 int ret;
822
823 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800824 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700825 put_page(page);
826 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700827 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700828 VM_UFFD_MISSING);
829 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
830 return ret;
831 }
832
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700833 entry = mk_huge_pmd(page, vma->vm_page_prot);
834 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800835 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800836 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700837 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700838 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800839 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800840 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800841 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800842 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700843 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800844 }
845
David Rientjesaa2e8782012-05-29 15:06:17 -0700846 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800847}
848
Andi Kleencc5d4622011-03-22 16:33:13 -0700849static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800850{
Mel Gorman71baba42015-11-06 16:28:28 -0800851 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800852}
853
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800854/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700855static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800856 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700857 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800858{
859 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700860 if (!pmd_none(*pmd))
861 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700862 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800863 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700864 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800865 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800866 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700867 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800868}
869
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800870int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
871 unsigned long address, pmd_t *pmd,
872 unsigned int flags)
873{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800874 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800875 struct page *page;
876 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800877
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700878 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700879 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700880 if (unlikely(anon_vma_prepare(vma)))
881 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700882 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700883 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200884 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700885 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800886 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700887 pgtable_t pgtable;
888 struct page *zero_page;
889 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700890 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700891 pgtable = pte_alloc_one(mm, haddr);
892 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800893 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700894 zero_page = get_huge_zero_page();
895 if (unlikely(!zero_page)) {
896 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700897 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700898 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700899 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800900 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700901 ret = 0;
902 set = false;
903 if (pmd_none(*pmd)) {
904 if (userfaultfd_missing(vma)) {
905 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700906 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700907 VM_UFFD_MISSING);
908 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
909 } else {
910 set_huge_zero_page(pgtable, mm, vma,
911 haddr, pmd,
912 zero_page);
913 spin_unlock(ptl);
914 set = true;
915 }
916 } else
917 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700918 if (!set) {
919 pte_free(mm, pgtable);
920 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800921 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700922 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800923 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800924 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
925 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700926 if (unlikely(!page)) {
927 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700928 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700929 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800930 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700931 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
932 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800933}
934
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700935static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800936 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700937{
938 struct mm_struct *mm = vma->vm_mm;
939 pmd_t entry;
940 spinlock_t *ptl;
941
942 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -0800943 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
944 if (pfn_t_devmap(pfn))
945 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -0800946 if (write) {
947 entry = pmd_mkyoung(pmd_mkdirty(entry));
948 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700949 }
Ross Zwisler01871e52016-01-15 16:56:02 -0800950 set_pmd_at(mm, addr, pmd, entry);
951 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700952 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700953}
954
955int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800956 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700957{
958 pgprot_t pgprot = vma->vm_page_prot;
959 /*
960 * If we had pmd_special, we could avoid all these restrictions,
961 * but we need to be consistent with PTEs and architectures that
962 * can't support a 'special' bit.
963 */
964 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
965 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
966 (VM_PFNMAP|VM_MIXEDMAP));
967 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -0800968 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700969
970 if (addr < vma->vm_start || addr >= vma->vm_end)
971 return VM_FAULT_SIGBUS;
972 if (track_pfn_insert(vma, &pgprot, pfn))
973 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700974 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
975 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700976}
977
Dan Williams3565fce2016-01-15 16:56:55 -0800978static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
979 pmd_t *pmd)
980{
981 pmd_t _pmd;
982
983 /*
984 * We should set the dirty bit only for FOLL_WRITE but for now
985 * the dirty bit in the pmd is meaningless. And if the dirty
986 * bit will become meaningful and we'll only set it with
987 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
988 * set the young bit, instead of the current set_pmd_at.
989 */
990 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
991 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
992 pmd, _pmd, 1))
993 update_mmu_cache_pmd(vma, addr, pmd);
994}
995
996struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
997 pmd_t *pmd, int flags)
998{
999 unsigned long pfn = pmd_pfn(*pmd);
1000 struct mm_struct *mm = vma->vm_mm;
1001 struct dev_pagemap *pgmap;
1002 struct page *page;
1003
1004 assert_spin_locked(pmd_lockptr(mm, pmd));
1005
1006 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1007 return NULL;
1008
1009 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1010 /* pass */;
1011 else
1012 return NULL;
1013
1014 if (flags & FOLL_TOUCH)
1015 touch_pmd(vma, addr, pmd);
1016
1017 /*
1018 * device mapped pages can only be returned if the
1019 * caller will manage the page reference count.
1020 */
1021 if (!(flags & FOLL_GET))
1022 return ERR_PTR(-EEXIST);
1023
1024 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1025 pgmap = get_dev_pagemap(pfn, NULL);
1026 if (!pgmap)
1027 return ERR_PTR(-EFAULT);
1028 page = pfn_to_page(pfn);
1029 get_page(page);
1030 put_dev_pagemap(pgmap);
1031
1032 return page;
1033}
1034
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001035int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1036 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1037 struct vm_area_struct *vma)
1038{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001039 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001040 struct page *src_page;
1041 pmd_t pmd;
1042 pgtable_t pgtable;
1043 int ret;
1044
1045 ret = -ENOMEM;
1046 pgtable = pte_alloc_one(dst_mm, addr);
1047 if (unlikely(!pgtable))
1048 goto out;
1049
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001050 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1051 src_ptl = pmd_lockptr(src_mm, src_pmd);
1052 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001053
1054 ret = -EAGAIN;
1055 pmd = *src_pmd;
Dan Williams5c7fb562016-01-15 16:56:52 -08001056 if (unlikely(!pmd_trans_huge(pmd) && !pmd_devmap(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001057 pte_free(dst_mm, pgtable);
1058 goto out_unlock;
1059 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001060 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001061 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001062 * under splitting since we don't split the page itself, only pmd to
1063 * a page table.
1064 */
1065 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001066 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001067 /*
1068 * get_huge_zero_page() will never allocate a new page here,
1069 * since we already have a zero page to copy. It just takes a
1070 * reference.
1071 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001072 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001073 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001074 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001075 ret = 0;
1076 goto out_unlock;
1077 }
Mel Gormande466bd2013-12-18 17:08:42 -08001078
Dan Williams5c7fb562016-01-15 16:56:52 -08001079 if (pmd_trans_huge(pmd)) {
1080 /* thp accounting separate from pmd_devmap accounting */
1081 src_page = pmd_page(pmd);
1082 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1083 get_page(src_page);
1084 page_dup_rmap(src_page, true);
1085 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1086 atomic_long_inc(&dst_mm->nr_ptes);
1087 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
1088 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001089
1090 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1091 pmd = pmd_mkold(pmd_wrprotect(pmd));
1092 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001093
1094 ret = 0;
1095out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001096 spin_unlock(src_ptl);
1097 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098out:
1099 return ret;
1100}
1101
Will Deacona1dd4502012-12-11 16:01:27 -08001102void huge_pmd_set_accessed(struct mm_struct *mm,
1103 struct vm_area_struct *vma,
1104 unsigned long address,
1105 pmd_t *pmd, pmd_t orig_pmd,
1106 int dirty)
1107{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001108 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001109 pmd_t entry;
1110 unsigned long haddr;
1111
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001112 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001113 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1114 goto unlock;
1115
1116 entry = pmd_mkyoung(orig_pmd);
1117 haddr = address & HPAGE_PMD_MASK;
1118 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1119 update_mmu_cache_pmd(vma, address, pmd);
1120
1121unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001122 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001123}
1124
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001125static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1126 struct vm_area_struct *vma,
1127 unsigned long address,
1128 pmd_t *pmd, pmd_t orig_pmd,
1129 struct page *page,
1130 unsigned long haddr)
1131{
Johannes Weiner00501b52014-08-08 14:19:20 -07001132 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001133 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001134 pgtable_t pgtable;
1135 pmd_t _pmd;
1136 int ret = 0, i;
1137 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001138 unsigned long mmun_start; /* For mmu_notifiers */
1139 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001140
1141 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1142 GFP_KERNEL);
1143 if (unlikely(!pages)) {
1144 ret |= VM_FAULT_OOM;
1145 goto out;
1146 }
1147
1148 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001149 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1150 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001151 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001152 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001153 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001154 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001155 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001156 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001157 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001158 memcg = (void *)page_private(pages[i]);
1159 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001160 mem_cgroup_cancel_charge(pages[i], memcg,
1161 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001162 put_page(pages[i]);
1163 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001164 kfree(pages);
1165 ret |= VM_FAULT_OOM;
1166 goto out;
1167 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001168 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001169 }
1170
1171 for (i = 0; i < HPAGE_PMD_NR; i++) {
1172 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001173 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001174 __SetPageUptodate(pages[i]);
1175 cond_resched();
1176 }
1177
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001178 mmun_start = haddr;
1179 mmun_end = haddr + HPAGE_PMD_SIZE;
1180 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1181
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001182 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001183 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1184 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001185 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001186
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001187 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001188 /* leave pmd empty until pte is filled */
1189
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001190 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001191 pmd_populate(mm, &_pmd, pgtable);
1192
1193 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1194 pte_t *pte, entry;
1195 entry = mk_pte(pages[i], vma->vm_page_prot);
1196 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001197 memcg = (void *)page_private(pages[i]);
1198 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001199 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001200 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001201 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001202 pte = pte_offset_map(&_pmd, haddr);
1203 VM_BUG_ON(!pte_none(*pte));
1204 set_pte_at(mm, haddr, pte, entry);
1205 pte_unmap(pte);
1206 }
1207 kfree(pages);
1208
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001209 smp_wmb(); /* make pte visible before pmd */
1210 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001211 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001212 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001213
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001214 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1215
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001216 ret |= VM_FAULT_WRITE;
1217 put_page(page);
1218
1219out:
1220 return ret;
1221
1222out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001223 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001224 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001225 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001226 memcg = (void *)page_private(pages[i]);
1227 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001228 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001229 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001230 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001231 kfree(pages);
1232 goto out;
1233}
1234
1235int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1236 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1237{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001238 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001240 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001241 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001242 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001243 unsigned long mmun_start; /* For mmu_notifiers */
1244 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001245 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001246
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001247 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001248 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001249 haddr = address & HPAGE_PMD_MASK;
1250 if (is_huge_zero_pmd(orig_pmd))
1251 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001252 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001253 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1254 goto out_unlock;
1255
1256 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001257 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001258 /*
1259 * We can only reuse the page if nobody else maps the huge page or it's
1260 * part. We can do it by checking page_mapcount() on each sub-page, but
1261 * it's expensive.
1262 * The cheaper way is to check page_count() to be equal 1: every
1263 * mapcount takes page reference reference, so this way we can
1264 * guarantee, that the PMD is the only mapping.
1265 * This can give false negative if somebody pinned the page, but that's
1266 * fine.
1267 */
1268 if (page_mapcount(page) == 1 && page_count(page) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001269 pmd_t entry;
1270 entry = pmd_mkyoung(orig_pmd);
1271 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1272 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001273 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001274 ret |= VM_FAULT_WRITE;
1275 goto out_unlock;
1276 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001277 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001278 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001279alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001281 !transparent_hugepage_debug_cow()) {
Michal Hocko3b363692015-04-15 16:13:29 -07001282 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1283 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001284 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001285 new_page = NULL;
1286
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001287 if (likely(new_page)) {
1288 prep_transhuge_page(new_page);
1289 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001290 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001291 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001292 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001293 } else {
1294 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1295 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001296 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001297 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001298 ret |= VM_FAULT_FALLBACK;
1299 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001300 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001301 }
David Rientjes17766dd2013-09-12 15:14:06 -07001302 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001303 goto out;
1304 }
1305
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001306 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1307 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001308 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001309 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001310 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001311 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001312 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001313 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001314 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001315 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001316 goto out;
1317 }
1318
David Rientjes17766dd2013-09-12 15:14:06 -07001319 count_vm_event(THP_FAULT_ALLOC);
1320
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001321 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001322 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1323 else
1324 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001325 __SetPageUptodate(new_page);
1326
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001327 mmun_start = haddr;
1328 mmun_end = haddr + HPAGE_PMD_SIZE;
1329 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1330
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001331 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001332 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001333 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001334 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001335 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001336 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001337 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001338 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001339 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001340 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001341 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1342 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001343 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001344 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001345 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001346 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001347 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001348 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001349 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001350 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001351 put_huge_zero_page();
1352 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001353 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001354 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001355 put_page(page);
1356 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001357 ret |= VM_FAULT_WRITE;
1358 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001359 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001360out_mn:
1361 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1362out:
1363 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001364out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001365 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001366 return ret;
1367}
1368
David Rientjesb676b292012-10-08 16:34:03 -07001369struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001370 unsigned long addr,
1371 pmd_t *pmd,
1372 unsigned int flags)
1373{
David Rientjesb676b292012-10-08 16:34:03 -07001374 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001375 struct page *page = NULL;
1376
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001377 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001378
1379 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1380 goto out;
1381
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001382 /* Avoid dumping huge zero page */
1383 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1384 return ERR_PTR(-EFAULT);
1385
Mel Gorman2b4847e2013-12-18 17:08:32 -08001386 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001387 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001388 goto out;
1389
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001390 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001391 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001392 if (flags & FOLL_TOUCH)
1393 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001394 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001395 /*
1396 * We don't mlock() pte-mapped THPs. This way we can avoid
1397 * leaking mlocked pages into non-VM_LOCKED VMAs.
1398 *
1399 * In most cases the pmd is the only mapping of the page as we
1400 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1401 * writable private mappings in populate_vma_page_range().
1402 *
1403 * The only scenario when we have the page shared here is if we
1404 * mlocking read-only mapping shared over fork(). We skip
1405 * mlocking such pages.
1406 */
1407 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1408 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001409 lru_add_drain();
1410 if (page->mapping)
1411 mlock_vma_page(page);
1412 unlock_page(page);
1413 }
1414 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001415 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001416 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001417 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001418 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001419
1420out:
1421 return page;
1422}
1423
Mel Gormand10e63f2012-10-25 14:16:31 +02001424/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001425int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1426 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001427{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001428 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001429 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001430 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001431 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001432 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001433 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001434 bool page_locked;
1435 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001436 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001437 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001438
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001439 /* A PROT_NONE fault should not end up here */
1440 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1441
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001442 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001443 if (unlikely(!pmd_same(pmd, *pmdp)))
1444 goto out_unlock;
1445
Mel Gormande466bd2013-12-18 17:08:42 -08001446 /*
1447 * If there are potential migrations, wait for completion and retry
1448 * without disrupting NUMA hinting information. Do not relock and
1449 * check_same as the page may no longer be mapped.
1450 */
1451 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001452 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001453 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001454 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001455 goto out;
1456 }
1457
Mel Gormand10e63f2012-10-25 14:16:31 +02001458 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001459 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001460 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001461 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001462 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001463 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001464 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001465 flags |= TNF_FAULT_LOCAL;
1466 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001467
Mel Gormanbea66fb2015-03-25 15:55:37 -07001468 /* See similar comment in do_numa_page for explanation */
1469 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001470 flags |= TNF_NO_GROUP;
1471
1472 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001473 * Acquire the page lock to serialise THP migrations but avoid dropping
1474 * page_table_lock if at all possible
1475 */
Mel Gormanb8916632013-10-07 11:28:44 +01001476 page_locked = trylock_page(page);
1477 target_nid = mpol_misplaced(page, vma, haddr);
1478 if (target_nid == -1) {
1479 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001480 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001481 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001482 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001483
Mel Gormande466bd2013-12-18 17:08:42 -08001484 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001485 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001486 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001487 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001488 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001489 goto out;
1490 }
1491
Mel Gorman2b4847e2013-12-18 17:08:32 -08001492 /*
1493 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1494 * to serialises splits
1495 */
Mel Gormanb8916632013-10-07 11:28:44 +01001496 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001497 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001498 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001499
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001500 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001501 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001502 if (unlikely(!pmd_same(pmd, *pmdp))) {
1503 unlock_page(page);
1504 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001505 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001506 goto out_unlock;
1507 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001508
Mel Gormanc3a489c2013-12-18 17:08:38 -08001509 /* Bail if we fail to protect against THP splits for any reason */
1510 if (unlikely(!anon_vma)) {
1511 put_page(page);
1512 page_nid = -1;
1513 goto clear_pmdnuma;
1514 }
1515
Mel Gormana54a4072013-10-07 11:28:46 +01001516 /*
1517 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001518 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001519 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001520 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001521 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001522 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001523 if (migrated) {
1524 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001525 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001526 } else
1527 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001528
Mel Gorman8191acb2013-10-07 11:28:45 +01001529 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001530clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001531 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001532 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001533 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001534 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001535 if (was_writable)
1536 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001537 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001538 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001539 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001540out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001541 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001542
1543out:
1544 if (anon_vma)
1545 page_unlock_anon_vma_read(anon_vma);
1546
Mel Gorman8191acb2013-10-07 11:28:45 +01001547 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001548 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001549
Mel Gormand10e63f2012-10-25 14:16:31 +02001550 return 0;
1551}
1552
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001553int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1554 pmd_t *pmd, unsigned long addr, unsigned long next)
1555
1556{
1557 spinlock_t *ptl;
1558 pmd_t orig_pmd;
1559 struct page *page;
1560 struct mm_struct *mm = tlb->mm;
1561 int ret = 0;
1562
1563 if (!pmd_trans_huge_lock(pmd, vma, &ptl))
Linus Torvalds25eedab2016-01-17 18:33:15 -08001564 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001565
1566 orig_pmd = *pmd;
1567 if (is_huge_zero_pmd(orig_pmd)) {
1568 ret = 1;
1569 goto out;
1570 }
1571
1572 page = pmd_page(orig_pmd);
1573 /*
1574 * If other processes are mapping this page, we couldn't discard
1575 * the page unless they all do MADV_FREE so let's skip the page.
1576 */
1577 if (page_mapcount(page) != 1)
1578 goto out;
1579
1580 if (!trylock_page(page))
1581 goto out;
1582
1583 /*
1584 * If user want to discard part-pages of THP, split it so MADV_FREE
1585 * will deactivate only them.
1586 */
1587 if (next - addr != HPAGE_PMD_SIZE) {
1588 get_page(page);
1589 spin_unlock(ptl);
1590 if (split_huge_page(page)) {
1591 put_page(page);
1592 unlock_page(page);
1593 goto out_unlocked;
1594 }
1595 put_page(page);
1596 unlock_page(page);
1597 ret = 1;
1598 goto out_unlocked;
1599 }
1600
1601 if (PageDirty(page))
1602 ClearPageDirty(page);
1603 unlock_page(page);
1604
1605 if (PageActive(page))
1606 deactivate_page(page);
1607
1608 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1609 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1610 tlb->fullmm);
1611 orig_pmd = pmd_mkold(orig_pmd);
1612 orig_pmd = pmd_mkclean(orig_pmd);
1613
1614 set_pmd_at(mm, addr, pmd, orig_pmd);
1615 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1616 }
1617 ret = 1;
1618out:
1619 spin_unlock(ptl);
1620out_unlocked:
1621 return ret;
1622}
1623
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001624int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001625 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001626{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001627 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001628 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001629
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001630 if (!__pmd_trans_huge_lock(pmd, vma, &ptl))
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001631 return 0;
1632 /*
1633 * For architectures like ppc64 we look at deposited pgtable
1634 * when calling pmdp_huge_get_and_clear. So do the
1635 * pgtable_trans_huge_withdraw after finishing pmdp related
1636 * operations.
1637 */
1638 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1639 tlb->fullmm);
1640 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1641 if (vma_is_dax(vma)) {
1642 spin_unlock(ptl);
1643 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001644 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001645 } else if (is_huge_zero_pmd(orig_pmd)) {
1646 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1647 atomic_long_dec(&tlb->mm->nr_ptes);
1648 spin_unlock(ptl);
1649 put_huge_zero_page();
1650 } else {
1651 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001652 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001653 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1654 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1655 VM_BUG_ON_PAGE(!PageHead(page), page);
1656 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1657 atomic_long_dec(&tlb->mm->nr_ptes);
1658 spin_unlock(ptl);
1659 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001660 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001661 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001662}
1663
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001664bool move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001665 unsigned long old_addr,
1666 unsigned long new_addr, unsigned long old_end,
1667 pmd_t *old_pmd, pmd_t *new_pmd)
1668{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001669 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001670 pmd_t pmd;
1671
1672 struct mm_struct *mm = vma->vm_mm;
1673
1674 if ((old_addr & ~HPAGE_PMD_MASK) ||
1675 (new_addr & ~HPAGE_PMD_MASK) ||
1676 old_end - old_addr < HPAGE_PMD_SIZE ||
1677 (new_vma->vm_flags & VM_NOHUGEPAGE))
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001678 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001679
1680 /*
1681 * The destination pmd shouldn't be established, free_pgtables()
1682 * should have release it.
1683 */
1684 if (WARN_ON(!pmd_none(*new_pmd))) {
1685 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001686 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001687 }
1688
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001689 /*
1690 * We don't have to worry about the ordering of src and dst
1691 * ptlocks because exclusive mmap_sem prevents deadlock.
1692 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001693 if (__pmd_trans_huge_lock(old_pmd, vma, &old_ptl)) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001694 new_ptl = pmd_lockptr(mm, new_pmd);
1695 if (new_ptl != old_ptl)
1696 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001697 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001698 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001699
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301700 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1701 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001702 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1703 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001704 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301705 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1706 if (new_ptl != old_ptl)
1707 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001708 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001709 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001710 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001711 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001712}
1713
Mel Gormanf123d742013-10-07 11:28:49 +01001714/*
1715 * Returns
1716 * - 0 if PMD could not be locked
1717 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1718 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1719 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001720int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001721 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001722{
1723 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001724 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001725 int ret = 0;
1726
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001727 if (__pmd_trans_huge_lock(pmd, vma, &ptl)) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001728 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001729 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001730 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001731
1732 /*
1733 * Avoid trapping faults against the zero page. The read-only
1734 * data is likely to be read-cached on the local CPU and
1735 * local/remote hits to the zero page are not interesting.
1736 */
1737 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1738 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001739 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001740 }
1741
Mel Gorman10c10452015-02-12 14:58:44 -08001742 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001743 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001744 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001745 if (preserve_write)
1746 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001747 ret = HPAGE_PMD_NR;
1748 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001749 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001750 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001751 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001752 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001753
1754 return ret;
1755}
1756
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001757/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001758 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001759 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001760 * Note that if it returns true, this routine returns without unlocking page
1761 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001762 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001763bool __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001764 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001765{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001766 *ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001767 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001768 return true;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001769 spin_unlock(*ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001770 return false;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001771}
1772
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001773#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001774
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001775int hugepage_madvise(struct vm_area_struct *vma,
1776 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001777{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001778 switch (advice) {
1779 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001780#ifdef CONFIG_S390
1781 /*
1782 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1783 * can't handle this properly after s390_enable_sie, so we simply
1784 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1785 */
1786 if (mm_has_pgste(vma->vm_mm))
1787 return 0;
1788#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001789 /*
1790 * Be somewhat over-protective like KSM for now!
1791 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001792 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001793 return -EINVAL;
1794 *vm_flags &= ~VM_NOHUGEPAGE;
1795 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001796 /*
1797 * If the vma become good for khugepaged to scan,
1798 * register it here without waiting a page fault that
1799 * may not happen any time soon.
1800 */
David Rientjes6d50e602014-10-29 14:50:31 -07001801 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001802 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001803 break;
1804 case MADV_NOHUGEPAGE:
1805 /*
1806 * Be somewhat over-protective like KSM for now!
1807 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001808 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001809 return -EINVAL;
1810 *vm_flags &= ~VM_HUGEPAGE;
1811 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001812 /*
1813 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1814 * this vma even if we leave the mm registered in khugepaged if
1815 * it got registered before VM_NOHUGEPAGE was set.
1816 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001817 break;
1818 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001819
1820 return 0;
1821}
1822
Andrea Arcangeliba761492011-01-13 15:46:58 -08001823static int __init khugepaged_slab_init(void)
1824{
1825 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1826 sizeof(struct mm_slot),
1827 __alignof__(struct mm_slot), 0, NULL);
1828 if (!mm_slot_cache)
1829 return -ENOMEM;
1830
1831 return 0;
1832}
1833
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001834static void __init khugepaged_slab_exit(void)
1835{
1836 kmem_cache_destroy(mm_slot_cache);
1837}
1838
Andrea Arcangeliba761492011-01-13 15:46:58 -08001839static inline struct mm_slot *alloc_mm_slot(void)
1840{
1841 if (!mm_slot_cache) /* initialization failed */
1842 return NULL;
1843 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1844}
1845
1846static inline void free_mm_slot(struct mm_slot *mm_slot)
1847{
1848 kmem_cache_free(mm_slot_cache, mm_slot);
1849}
1850
Andrea Arcangeliba761492011-01-13 15:46:58 -08001851static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1852{
1853 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001854
Sasha Levinb67bfe02013-02-27 17:06:00 -08001855 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001856 if (mm == mm_slot->mm)
1857 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001858
Andrea Arcangeliba761492011-01-13 15:46:58 -08001859 return NULL;
1860}
1861
1862static void insert_to_mm_slots_hash(struct mm_struct *mm,
1863 struct mm_slot *mm_slot)
1864{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001865 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001866 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001867}
1868
1869static inline int khugepaged_test_exit(struct mm_struct *mm)
1870{
1871 return atomic_read(&mm->mm_users) == 0;
1872}
1873
1874int __khugepaged_enter(struct mm_struct *mm)
1875{
1876 struct mm_slot *mm_slot;
1877 int wakeup;
1878
1879 mm_slot = alloc_mm_slot();
1880 if (!mm_slot)
1881 return -ENOMEM;
1882
1883 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001884 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001885 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1886 free_mm_slot(mm_slot);
1887 return 0;
1888 }
1889
1890 spin_lock(&khugepaged_mm_lock);
1891 insert_to_mm_slots_hash(mm, mm_slot);
1892 /*
1893 * Insert just behind the scanning cursor, to let the area settle
1894 * down a little.
1895 */
1896 wakeup = list_empty(&khugepaged_scan.mm_head);
1897 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1898 spin_unlock(&khugepaged_mm_lock);
1899
1900 atomic_inc(&mm->mm_count);
1901 if (wakeup)
1902 wake_up_interruptible(&khugepaged_wait);
1903
1904 return 0;
1905}
1906
David Rientjes6d50e602014-10-29 14:50:31 -07001907int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1908 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001909{
1910 unsigned long hstart, hend;
1911 if (!vma->anon_vma)
1912 /*
1913 * Not yet faulted in so we will register later in the
1914 * page fault if needed.
1915 */
1916 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001917 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001918 /* khugepaged not yet working on file or special mappings */
1919 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07001920 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001921 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1922 hend = vma->vm_end & HPAGE_PMD_MASK;
1923 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001924 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001925 return 0;
1926}
1927
1928void __khugepaged_exit(struct mm_struct *mm)
1929{
1930 struct mm_slot *mm_slot;
1931 int free = 0;
1932
1933 spin_lock(&khugepaged_mm_lock);
1934 mm_slot = get_mm_slot(mm);
1935 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001936 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001937 list_del(&mm_slot->mm_node);
1938 free = 1;
1939 }
Chris Wrightd788e802011-07-25 17:12:14 -07001940 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001941
1942 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001943 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1944 free_mm_slot(mm_slot);
1945 mmdrop(mm);
1946 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001947 /*
1948 * This is required to serialize against
1949 * khugepaged_test_exit() (which is guaranteed to run
1950 * under mmap sem read mode). Stop here (after we
1951 * return all pagetables will be destroyed) until
1952 * khugepaged has finished working on the pagetables
1953 * under the mmap_sem.
1954 */
1955 down_write(&mm->mmap_sem);
1956 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001957 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001958}
1959
1960static void release_pte_page(struct page *page)
1961{
1962 /* 0 stands for page_is_file_cache(page) == false */
1963 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1964 unlock_page(page);
1965 putback_lru_page(page);
1966}
1967
1968static void release_pte_pages(pte_t *pte, pte_t *_pte)
1969{
1970 while (--_pte >= pte) {
1971 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001972 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001973 release_pte_page(pte_page(pteval));
1974 }
1975}
1976
Andrea Arcangeliba761492011-01-13 15:46:58 -08001977static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1978 unsigned long address,
1979 pte_t *pte)
1980{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001981 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001982 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001983 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001984 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001985
Andrea Arcangeliba761492011-01-13 15:46:58 -08001986 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1987 _pte++, address += PAGE_SIZE) {
1988 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07001989 if (pte_none(pteval) || (pte_present(pteval) &&
1990 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07001991 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001992 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001993 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001994 } else {
1995 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001996 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001997 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001998 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001999 if (!pte_present(pteval)) {
2000 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002001 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002002 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002003 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002004 if (unlikely(!page)) {
2005 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002006 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002007 }
Bob Liu344aa352012-12-11 16:00:34 -08002008
Sasha Levin309381fea2014-01-23 15:52:54 -08002009 VM_BUG_ON_PAGE(PageCompound(page), page);
2010 VM_BUG_ON_PAGE(!PageAnon(page), page);
2011 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002012
Andrea Arcangeliba761492011-01-13 15:46:58 -08002013 /*
2014 * We can do it before isolate_lru_page because the
2015 * page can't be freed from under us. NOTE: PG_lock
2016 * is needed to serialize against split_huge_page
2017 * when invoked from the VM.
2018 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002019 if (!trylock_page(page)) {
2020 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002021 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002022 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002023
2024 /*
2025 * cannot use mapcount: can't collapse if there's a gup pin.
2026 * The page must only be referenced by the scanned process
2027 * and page swap cache.
2028 */
2029 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2030 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002031 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002032 goto out;
2033 }
2034 if (pte_write(pteval)) {
2035 writable = true;
2036 } else {
2037 if (PageSwapCache(page) && !reuse_swap_page(page)) {
2038 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002039 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002040 goto out;
2041 }
2042 /*
2043 * Page is not in the swap cache. It can be collapsed
2044 * into a THP.
2045 */
2046 }
2047
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 /*
2049 * Isolate the page to avoid collapsing an hugepage
2050 * currently in use by the VM.
2051 */
2052 if (isolate_lru_page(page)) {
2053 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002054 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002055 goto out;
2056 }
2057 /* 0 stands for page_is_file_cache(page) == false */
2058 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002059 VM_BUG_ON_PAGE(!PageLocked(page), page);
2060 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002061
2062 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002063 if (pte_young(pteval) ||
2064 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002065 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002066 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002067 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002068 if (likely(writable)) {
2069 if (likely(referenced)) {
2070 result = SCAN_SUCCEED;
2071 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
2072 referenced, writable, result);
2073 return 1;
2074 }
2075 } else {
2076 result = SCAN_PAGE_RO;
2077 }
2078
Andrea Arcangeliba761492011-01-13 15:46:58 -08002079out:
Bob Liu344aa352012-12-11 16:00:34 -08002080 release_pte_pages(pte, _pte);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002081 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
2082 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002083 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002084}
2085
2086static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2087 struct vm_area_struct *vma,
2088 unsigned long address,
2089 spinlock_t *ptl)
2090{
2091 pte_t *_pte;
2092 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2093 pte_t pteval = *_pte;
2094 struct page *src_page;
2095
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002096 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002097 clear_user_highpage(page, address);
2098 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002099 if (is_zero_pfn(pte_pfn(pteval))) {
2100 /*
2101 * ptl mostly unnecessary.
2102 */
2103 spin_lock(ptl);
2104 /*
2105 * paravirt calls inside pte_clear here are
2106 * superfluous.
2107 */
2108 pte_clear(vma->vm_mm, address, _pte);
2109 spin_unlock(ptl);
2110 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002111 } else {
2112 src_page = pte_page(pteval);
2113 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002114 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002115 release_pte_page(src_page);
2116 /*
2117 * ptl mostly unnecessary, but preempt has to
2118 * be disabled to update the per-cpu stats
2119 * inside page_remove_rmap().
2120 */
2121 spin_lock(ptl);
2122 /*
2123 * paravirt calls inside pte_clear here are
2124 * superfluous.
2125 */
2126 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002127 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002128 spin_unlock(ptl);
2129 free_page_and_swap_cache(src_page);
2130 }
2131
2132 address += PAGE_SIZE;
2133 page++;
2134 }
2135}
2136
Xiao Guangrong26234f32012-10-08 16:29:51 -07002137static void khugepaged_alloc_sleep(void)
2138{
Petr Mladekbde43c62015-09-08 15:04:05 -07002139 DEFINE_WAIT(wait);
2140
2141 add_wait_queue(&khugepaged_wait, &wait);
2142 freezable_schedule_timeout_interruptible(
2143 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2144 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002145}
2146
Bob Liu9f1b8682013-11-12 15:07:37 -08002147static int khugepaged_node_load[MAX_NUMNODES];
2148
David Rientjes14a4e212014-08-06 16:07:29 -07002149static bool khugepaged_scan_abort(int nid)
2150{
2151 int i;
2152
2153 /*
2154 * If zone_reclaim_mode is disabled, then no extra effort is made to
2155 * allocate memory locally.
2156 */
2157 if (!zone_reclaim_mode)
2158 return false;
2159
2160 /* If there is a count for this node already, it must be acceptable */
2161 if (khugepaged_node_load[nid])
2162 return false;
2163
2164 for (i = 0; i < MAX_NUMNODES; i++) {
2165 if (!khugepaged_node_load[i])
2166 continue;
2167 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2168 return true;
2169 }
2170 return false;
2171}
2172
Xiao Guangrong26234f32012-10-08 16:29:51 -07002173#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002174static int khugepaged_find_target_node(void)
2175{
2176 static int last_khugepaged_target_node = NUMA_NO_NODE;
2177 int nid, target_node = 0, max_value = 0;
2178
2179 /* find first node with max normal pages hit */
2180 for (nid = 0; nid < MAX_NUMNODES; nid++)
2181 if (khugepaged_node_load[nid] > max_value) {
2182 max_value = khugepaged_node_load[nid];
2183 target_node = nid;
2184 }
2185
2186 /* do some balance if several nodes have the same hit record */
2187 if (target_node <= last_khugepaged_target_node)
2188 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2189 nid++)
2190 if (max_value == khugepaged_node_load[nid]) {
2191 target_node = nid;
2192 break;
2193 }
2194
2195 last_khugepaged_target_node = target_node;
2196 return target_node;
2197}
2198
Xiao Guangrong26234f32012-10-08 16:29:51 -07002199static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2200{
2201 if (IS_ERR(*hpage)) {
2202 if (!*wait)
2203 return false;
2204
2205 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002206 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002207 khugepaged_alloc_sleep();
2208 } else if (*hpage) {
2209 put_page(*hpage);
2210 *hpage = NULL;
2211 }
2212
2213 return true;
2214}
2215
Michal Hocko3b363692015-04-15 16:13:29 -07002216static struct page *
2217khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002218 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002219{
Sasha Levin309381fea2014-01-23 15:52:54 -08002220 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002221
Xiao Guangrong26234f32012-10-08 16:29:51 -07002222 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002223 * Before allocating the hugepage, release the mmap_sem read lock.
2224 * The allocation can take potentially a long time if it involves
2225 * sync compaction, and we do not need to hold the mmap_sem during
2226 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002227 */
2228 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002229
Vlastimil Babka96db8002015-09-08 15:03:50 -07002230 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002231 if (unlikely(!*hpage)) {
2232 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2233 *hpage = ERR_PTR(-ENOMEM);
2234 return NULL;
2235 }
2236
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002237 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002238 count_vm_event(THP_COLLAPSE_ALLOC);
2239 return *hpage;
2240}
2241#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002242static int khugepaged_find_target_node(void)
2243{
2244 return 0;
2245}
2246
Bob Liu10dc4152013-11-12 15:07:35 -08002247static inline struct page *alloc_hugepage(int defrag)
2248{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002249 struct page *page;
2250
2251 page = alloc_pages(alloc_hugepage_gfpmask(defrag, 0), HPAGE_PMD_ORDER);
2252 if (page)
2253 prep_transhuge_page(page);
2254 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002255}
2256
Xiao Guangrong26234f32012-10-08 16:29:51 -07002257static struct page *khugepaged_alloc_hugepage(bool *wait)
2258{
2259 struct page *hpage;
2260
2261 do {
2262 hpage = alloc_hugepage(khugepaged_defrag());
2263 if (!hpage) {
2264 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2265 if (!*wait)
2266 return NULL;
2267
2268 *wait = false;
2269 khugepaged_alloc_sleep();
2270 } else
2271 count_vm_event(THP_COLLAPSE_ALLOC);
2272 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2273
2274 return hpage;
2275}
2276
2277static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2278{
2279 if (!*hpage)
2280 *hpage = khugepaged_alloc_hugepage(wait);
2281
2282 if (unlikely(!*hpage))
2283 return false;
2284
2285 return true;
2286}
2287
Michal Hocko3b363692015-04-15 16:13:29 -07002288static struct page *
2289khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002290 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002291{
2292 up_read(&mm->mmap_sem);
2293 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002294
Xiao Guangrong26234f32012-10-08 16:29:51 -07002295 return *hpage;
2296}
2297#endif
2298
Bob Liufa475e52012-12-11 16:00:39 -08002299static bool hugepage_vma_check(struct vm_area_struct *vma)
2300{
2301 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2302 (vma->vm_flags & VM_NOHUGEPAGE))
2303 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002304 if (!vma->anon_vma || vma->vm_ops)
2305 return false;
2306 if (is_vma_temporary_stack(vma))
2307 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002308 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002309 return true;
2310}
2311
Andrea Arcangeliba761492011-01-13 15:46:58 -08002312static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002313 unsigned long address,
2314 struct page **hpage,
2315 struct vm_area_struct *vma,
2316 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002317{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002318 pmd_t *pmd, _pmd;
2319 pte_t *pte;
2320 pgtable_t pgtable;
2321 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002322 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002323 int isolated = 0, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002324 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002325 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002326 unsigned long mmun_start; /* For mmu_notifiers */
2327 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002328 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002329
2330 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002331
Michal Hocko3b363692015-04-15 16:13:29 -07002332 /* Only allocate from the target node */
2333 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2334 __GFP_THISNODE;
2335
Xiao Guangrong26234f32012-10-08 16:29:51 -07002336 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002337 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002338 if (!new_page) {
2339 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2340 goto out_nolock;
2341 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002342
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002343 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002344 result = SCAN_CGROUP_CHARGE_FAIL;
2345 goto out_nolock;
2346 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002347
2348 /*
2349 * Prevent all access to pagetables with the exception of
2350 * gup_fast later hanlded by the ptep_clear_flush and the VM
2351 * handled by the anon_vma lock + PG_lock.
2352 */
2353 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002354 if (unlikely(khugepaged_test_exit(mm))) {
2355 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002356 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002357 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002358
2359 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002360 if (!vma) {
2361 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002362 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002363 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002364 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2365 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002366 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2367 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002368 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002369 }
2370 if (!hugepage_vma_check(vma)) {
2371 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002372 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002373 }
Bob Liu62190492012-12-11 16:00:37 -08002374 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002375 if (!pmd) {
2376 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002377 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002378 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002379
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002380 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002381
2382 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002383 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002384
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002385 mmun_start = address;
2386 mmun_end = address + HPAGE_PMD_SIZE;
2387 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002388 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002389 /*
2390 * After this gup_fast can't run anymore. This also removes
2391 * any huge TLB entry from the CPU so we won't allow
2392 * huge and small TLB entries for the same virtual address
2393 * to avoid the risk of CPU bugs in that area.
2394 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002395 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002396 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002397 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002398
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002399 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002400 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002401 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002402
2403 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002404 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002405 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002406 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002407 /*
2408 * We can only use set_pmd_at when establishing
2409 * hugepmds and never for establishing regular pmds that
2410 * points to regular pagetables. Use pmd_populate for that
2411 */
2412 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002413 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002414 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002415 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002416 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002417 }
2418
2419 /*
2420 * All pages are isolated and locked so anon_vma rmap
2421 * can't run anymore.
2422 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002423 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002424
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002425 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002426 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002427 __SetPageUptodate(new_page);
2428 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002429
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002430 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2431 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002432
2433 /*
2434 * spin_lock() below is not the equivalent of smp_wmb(), so
2435 * this is needed to avoid the copy_huge_page writes to become
2436 * visible after the set_pmd_at() write.
2437 */
2438 smp_wmb();
2439
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002440 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002441 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002442 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002443 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002444 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002445 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002446 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002447 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002448 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002449
2450 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002451
Andrea Arcangeliba761492011-01-13 15:46:58 -08002452 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002453 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002454out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002455 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002456 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002457 return;
2458
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002459out_nolock:
2460 trace_mm_collapse_huge_page(mm, isolated, result);
2461 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002462out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002463 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002464 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002465}
2466
2467static int khugepaged_scan_pmd(struct mm_struct *mm,
2468 struct vm_area_struct *vma,
2469 unsigned long address,
2470 struct page **hpage)
2471{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002472 pmd_t *pmd;
2473 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002474 int ret = 0, none_or_zero = 0, result = 0;
2475 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002476 unsigned long _address;
2477 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002478 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002479 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002480
2481 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2482
Bob Liu62190492012-12-11 16:00:37 -08002483 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002484 if (!pmd) {
2485 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002486 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002487 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002488
Bob Liu9f1b8682013-11-12 15:07:37 -08002489 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002490 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2491 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2492 _pte++, _address += PAGE_SIZE) {
2493 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002494 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002495 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002496 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002497 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002498 } else {
2499 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002500 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002501 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002502 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002503 if (!pte_present(pteval)) {
2504 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002505 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002506 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002507 if (pte_write(pteval))
2508 writable = true;
2509
Andrea Arcangeliba761492011-01-13 15:46:58 -08002510 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002511 if (unlikely(!page)) {
2512 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002513 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002514 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002515
2516 /* TODO: teach khugepaged to collapse THP mapped with pte */
2517 if (PageCompound(page)) {
2518 result = SCAN_PAGE_COMPOUND;
2519 goto out_unmap;
2520 }
2521
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002522 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002523 * Record which node the original page is from and save this
2524 * information to khugepaged_node_load[].
2525 * Khupaged will allocate hugepage from the node has the max
2526 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002527 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002528 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002529 if (khugepaged_scan_abort(node)) {
2530 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002531 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002532 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002533 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002534 if (!PageLRU(page)) {
2535 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002536 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002537 }
2538 if (PageLocked(page)) {
2539 result = SCAN_PAGE_LOCK;
2540 goto out_unmap;
2541 }
2542 if (!PageAnon(page)) {
2543 result = SCAN_PAGE_ANON;
2544 goto out_unmap;
2545 }
2546
Ebru Akagunduz10359212015-02-11 15:28:28 -08002547 /*
2548 * cannot use mapcount: can't collapse if there's a gup pin.
2549 * The page must only be referenced by the scanned process
2550 * and page swap cache.
2551 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002552 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2553 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002554 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002555 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002556 if (pte_young(pteval) ||
2557 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002558 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002559 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002560 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002561 if (writable) {
2562 if (referenced) {
2563 result = SCAN_SUCCEED;
2564 ret = 1;
2565 } else {
2566 result = SCAN_NO_REFERENCED_PAGE;
2567 }
2568 } else {
2569 result = SCAN_PAGE_RO;
2570 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571out_unmap:
2572 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002573 if (ret) {
2574 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002575 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002576 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002577 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002578out:
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002579 trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2580 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002581 return ret;
2582}
2583
2584static void collect_mm_slot(struct mm_slot *mm_slot)
2585{
2586 struct mm_struct *mm = mm_slot->mm;
2587
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002588 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002589
2590 if (khugepaged_test_exit(mm)) {
2591 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002592 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002593 list_del(&mm_slot->mm_node);
2594
2595 /*
2596 * Not strictly needed because the mm exited already.
2597 *
2598 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2599 */
2600
2601 /* khugepaged_mm_lock actually not necessary for the below */
2602 free_mm_slot(mm_slot);
2603 mmdrop(mm);
2604 }
2605}
2606
2607static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2608 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002609 __releases(&khugepaged_mm_lock)
2610 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002611{
2612 struct mm_slot *mm_slot;
2613 struct mm_struct *mm;
2614 struct vm_area_struct *vma;
2615 int progress = 0;
2616
2617 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002618 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002619
2620 if (khugepaged_scan.mm_slot)
2621 mm_slot = khugepaged_scan.mm_slot;
2622 else {
2623 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2624 struct mm_slot, mm_node);
2625 khugepaged_scan.address = 0;
2626 khugepaged_scan.mm_slot = mm_slot;
2627 }
2628 spin_unlock(&khugepaged_mm_lock);
2629
2630 mm = mm_slot->mm;
2631 down_read(&mm->mmap_sem);
2632 if (unlikely(khugepaged_test_exit(mm)))
2633 vma = NULL;
2634 else
2635 vma = find_vma(mm, khugepaged_scan.address);
2636
2637 progress++;
2638 for (; vma; vma = vma->vm_next) {
2639 unsigned long hstart, hend;
2640
2641 cond_resched();
2642 if (unlikely(khugepaged_test_exit(mm))) {
2643 progress++;
2644 break;
2645 }
Bob Liufa475e52012-12-11 16:00:39 -08002646 if (!hugepage_vma_check(vma)) {
2647skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002648 progress++;
2649 continue;
2650 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002651 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2652 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002653 if (hstart >= hend)
2654 goto skip;
2655 if (khugepaged_scan.address > hend)
2656 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002657 if (khugepaged_scan.address < hstart)
2658 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002659 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002660
2661 while (khugepaged_scan.address < hend) {
2662 int ret;
2663 cond_resched();
2664 if (unlikely(khugepaged_test_exit(mm)))
2665 goto breakouterloop;
2666
2667 VM_BUG_ON(khugepaged_scan.address < hstart ||
2668 khugepaged_scan.address + HPAGE_PMD_SIZE >
2669 hend);
2670 ret = khugepaged_scan_pmd(mm, vma,
2671 khugepaged_scan.address,
2672 hpage);
2673 /* move to next address */
2674 khugepaged_scan.address += HPAGE_PMD_SIZE;
2675 progress += HPAGE_PMD_NR;
2676 if (ret)
2677 /* we released mmap_sem so break loop */
2678 goto breakouterloop_mmap_sem;
2679 if (progress >= pages)
2680 goto breakouterloop;
2681 }
2682 }
2683breakouterloop:
2684 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2685breakouterloop_mmap_sem:
2686
2687 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002688 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002689 /*
2690 * Release the current mm_slot if this mm is about to die, or
2691 * if we scanned all vmas of this mm.
2692 */
2693 if (khugepaged_test_exit(mm) || !vma) {
2694 /*
2695 * Make sure that if mm_users is reaching zero while
2696 * khugepaged runs here, khugepaged_exit will find
2697 * mm_slot not pointing to the exiting mm.
2698 */
2699 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2700 khugepaged_scan.mm_slot = list_entry(
2701 mm_slot->mm_node.next,
2702 struct mm_slot, mm_node);
2703 khugepaged_scan.address = 0;
2704 } else {
2705 khugepaged_scan.mm_slot = NULL;
2706 khugepaged_full_scans++;
2707 }
2708
2709 collect_mm_slot(mm_slot);
2710 }
2711
2712 return progress;
2713}
2714
2715static int khugepaged_has_work(void)
2716{
2717 return !list_empty(&khugepaged_scan.mm_head) &&
2718 khugepaged_enabled();
2719}
2720
2721static int khugepaged_wait_event(void)
2722{
2723 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002724 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002725}
2726
Xiao Guangrongd5169042012-10-08 16:29:48 -07002727static void khugepaged_do_scan(void)
2728{
2729 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002730 unsigned int progress = 0, pass_through_head = 0;
2731 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002732 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002733
2734 barrier(); /* write khugepaged_pages_to_scan to local stack */
2735
2736 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002737 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002738 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002739
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002740 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002741
Jiri Kosinacd092412015-06-24 16:56:07 -07002742 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002743 break;
2744
Andrea Arcangeliba761492011-01-13 15:46:58 -08002745 spin_lock(&khugepaged_mm_lock);
2746 if (!khugepaged_scan.mm_slot)
2747 pass_through_head++;
2748 if (khugepaged_has_work() &&
2749 pass_through_head < 2)
2750 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002751 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002752 else
2753 progress = pages;
2754 spin_unlock(&khugepaged_mm_lock);
2755 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002756
Xiao Guangrongd5169042012-10-08 16:29:48 -07002757 if (!IS_ERR_OR_NULL(hpage))
2758 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002759}
2760
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002761static void khugepaged_wait_work(void)
2762{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002763 if (khugepaged_has_work()) {
2764 if (!khugepaged_scan_sleep_millisecs)
2765 return;
2766
2767 wait_event_freezable_timeout(khugepaged_wait,
2768 kthread_should_stop(),
2769 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2770 return;
2771 }
2772
2773 if (khugepaged_enabled())
2774 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2775}
2776
Andrea Arcangeliba761492011-01-13 15:46:58 -08002777static int khugepaged(void *none)
2778{
2779 struct mm_slot *mm_slot;
2780
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002781 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002782 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002783
Xiao Guangrongb7231782012-10-08 16:29:54 -07002784 while (!kthread_should_stop()) {
2785 khugepaged_do_scan();
2786 khugepaged_wait_work();
2787 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002788
2789 spin_lock(&khugepaged_mm_lock);
2790 mm_slot = khugepaged_scan.mm_slot;
2791 khugepaged_scan.mm_slot = NULL;
2792 if (mm_slot)
2793 collect_mm_slot(mm_slot);
2794 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002795 return 0;
2796}
2797
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002798static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2799 unsigned long haddr, pmd_t *pmd)
2800{
2801 struct mm_struct *mm = vma->vm_mm;
2802 pgtable_t pgtable;
2803 pmd_t _pmd;
2804 int i;
2805
2806 /* leave pmd empty until pte is filled */
2807 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2808
2809 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2810 pmd_populate(mm, &_pmd, pgtable);
2811
2812 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2813 pte_t *pte, entry;
2814 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2815 entry = pte_mkspecial(entry);
2816 pte = pte_offset_map(&_pmd, haddr);
2817 VM_BUG_ON(!pte_none(*pte));
2818 set_pte_at(mm, haddr, pte, entry);
2819 pte_unmap(pte);
2820 }
2821 smp_wmb(); /* make pte visible before pmd */
2822 pmd_populate(mm, pmd, pgtable);
2823 put_huge_zero_page();
2824}
2825
2826static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002827 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002828{
2829 struct mm_struct *mm = vma->vm_mm;
2830 struct page *page;
2831 pgtable_t pgtable;
2832 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002833 bool young, write, dirty;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002834 int i;
2835
2836 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2837 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2838 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002839 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002840
2841 count_vm_event(THP_SPLIT_PMD);
2842
2843 if (vma_is_dax(vma)) {
2844 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2845 if (is_huge_zero_pmd(_pmd))
2846 put_huge_zero_page();
2847 return;
2848 } else if (is_huge_zero_pmd(*pmd)) {
2849 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2850 }
2851
2852 page = pmd_page(*pmd);
2853 VM_BUG_ON_PAGE(!page_count(page), page);
2854 atomic_add(HPAGE_PMD_NR - 1, &page->_count);
2855 write = pmd_write(*pmd);
2856 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002857 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002858
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002859 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2860 pmd_populate(mm, &_pmd, pgtable);
2861
2862 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2863 pte_t entry, *pte;
2864 /*
2865 * Note that NUMA hinting access restrictions are not
2866 * transferred to avoid any possibility of altering
2867 * permissions across VMAs.
2868 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002869 if (freeze) {
2870 swp_entry_t swp_entry;
2871 swp_entry = make_migration_entry(page + i, write);
2872 entry = swp_entry_to_pte(swp_entry);
2873 } else {
2874 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002875 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002876 if (!write)
2877 entry = pte_wrprotect(entry);
2878 if (!young)
2879 entry = pte_mkold(entry);
2880 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002881 if (dirty)
2882 SetPageDirty(page + i);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002883 pte = pte_offset_map(&_pmd, haddr);
2884 BUG_ON(!pte_none(*pte));
2885 set_pte_at(mm, haddr, pte, entry);
2886 atomic_inc(&page[i]._mapcount);
2887 pte_unmap(pte);
2888 }
2889
2890 /*
2891 * Set PG_double_map before dropping compound_mapcount to avoid
2892 * false-negative page_mapped().
2893 */
2894 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2895 for (i = 0; i < HPAGE_PMD_NR; i++)
2896 atomic_inc(&page[i]._mapcount);
2897 }
2898
2899 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2900 /* Last compound_mapcount is gone. */
2901 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2902 if (TestClearPageDoubleMap(page)) {
2903 /* No need in mapcount reference anymore */
2904 for (i = 0; i < HPAGE_PMD_NR; i++)
2905 atomic_dec(&page[i]._mapcount);
2906 }
2907 }
2908
2909 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002910 /*
2911 * Up to this point the pmd is present and huge and userland has the
2912 * whole access to the hugepage during the split (which happens in
2913 * place). If we overwrite the pmd with the not-huge version pointing
2914 * to the pte here (which of course we could if all CPUs were bug
2915 * free), userland could trigger a small page size TLB miss on the
2916 * small sized TLB while the hugepage TLB entry is still established in
2917 * the huge TLB. Some CPU doesn't like that.
2918 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2919 * 383 on page 93. Intel should be safe but is also warns that it's
2920 * only safe if the permission and cache attributes of the two entries
2921 * loaded in the two TLB is identical (which should be the case here).
2922 * But it is generally safer to never allow small and huge TLB entries
2923 * for the same virtual address to be loaded simultaneously. So instead
2924 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2925 * current pmd notpresent (atomically because here the pmd_trans_huge
2926 * and pmd_trans_splitting must remain set at all times on the pmd
2927 * until the split is complete for this pmd), then we flush the SMP TLB
2928 * and finally we write the non-huge version of the pmd entry with
2929 * pmd_populate.
2930 */
2931 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002932 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002933
2934 if (freeze) {
2935 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2936 page_remove_rmap(page + i, false);
2937 put_page(page + i);
2938 }
2939 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002940}
2941
2942void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
2943 unsigned long address)
2944{
2945 spinlock_t *ptl;
2946 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002947 struct page *page = NULL;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002948 unsigned long haddr = address & HPAGE_PMD_MASK;
2949
2950 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2951 ptl = pmd_lock(mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08002952 if (pmd_trans_huge(*pmd)) {
2953 page = pmd_page(*pmd);
2954 if (PageMlocked(page))
2955 get_page(page);
2956 else
2957 page = NULL;
2958 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002959 goto out;
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002960 __split_huge_pmd_locked(vma, pmd, haddr, false);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002961out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002962 spin_unlock(ptl);
2963 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002964 if (page) {
2965 lock_page(page);
2966 munlock_vma_page(page);
2967 unlock_page(page);
2968 put_page(page);
2969 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002970}
2971
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002972static void split_huge_pmd_address(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002973 unsigned long address)
2974{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002975 pgd_t *pgd;
2976 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002977 pmd_t *pmd;
2978
2979 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2980
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002981 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002982 if (!pgd_present(*pgd))
2983 return;
2984
2985 pud = pud_offset(pgd, address);
2986 if (!pud_present(*pud))
2987 return;
2988
2989 pmd = pmd_offset(pud, address);
Dan Williams5c7fb562016-01-15 16:56:52 -08002990 if (!pmd_present(*pmd) || (!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002991 return;
2992 /*
2993 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2994 * materialize from under us.
2995 */
Kirill A. Shutemovad0bed22016-01-15 16:52:53 -08002996 split_huge_pmd(vma, pmd, address);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002997}
2998
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002999void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003000 unsigned long start,
3001 unsigned long end,
3002 long adjust_next)
3003{
3004 /*
3005 * If the new start address isn't hpage aligned and it could
3006 * previously contain an hugepage: check if we need to split
3007 * an huge pmd.
3008 */
3009 if (start & ~HPAGE_PMD_MASK &&
3010 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3011 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003012 split_huge_pmd_address(vma, start);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003013
3014 /*
3015 * If the new end address isn't hpage aligned and it could
3016 * previously contain an hugepage: check if we need to split
3017 * an huge pmd.
3018 */
3019 if (end & ~HPAGE_PMD_MASK &&
3020 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3021 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003022 split_huge_pmd_address(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003023
3024 /*
3025 * If we're also updating the vma->vm_next->vm_start, if the new
3026 * vm_next->vm_start isn't page aligned and it could previously
3027 * contain an hugepage: check if we need to split an huge pmd.
3028 */
3029 if (adjust_next > 0) {
3030 struct vm_area_struct *next = vma->vm_next;
3031 unsigned long nstart = next->vm_start;
3032 nstart += adjust_next << PAGE_SHIFT;
3033 if (nstart & ~HPAGE_PMD_MASK &&
3034 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3035 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003036 split_huge_pmd_address(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003037 }
3038}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003039
3040static void freeze_page_vma(struct vm_area_struct *vma, struct page *page,
3041 unsigned long address)
3042{
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003043 unsigned long haddr = address & HPAGE_PMD_MASK;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003044 spinlock_t *ptl;
3045 pgd_t *pgd;
3046 pud_t *pud;
3047 pmd_t *pmd;
3048 pte_t *pte;
3049 int i, nr = HPAGE_PMD_NR;
3050
3051 /* Skip pages which doesn't belong to the VMA */
3052 if (address < vma->vm_start) {
3053 int off = (vma->vm_start - address) >> PAGE_SHIFT;
3054 page += off;
3055 nr -= off;
3056 address = vma->vm_start;
3057 }
3058
3059 pgd = pgd_offset(vma->vm_mm, address);
3060 if (!pgd_present(*pgd))
3061 return;
3062 pud = pud_offset(pgd, address);
3063 if (!pud_present(*pud))
3064 return;
3065 pmd = pmd_offset(pud, address);
3066 ptl = pmd_lock(vma->vm_mm, pmd);
3067 if (!pmd_present(*pmd)) {
3068 spin_unlock(ptl);
3069 return;
3070 }
3071 if (pmd_trans_huge(*pmd)) {
3072 if (page == pmd_page(*pmd))
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003073 __split_huge_pmd_locked(vma, pmd, haddr, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003074 spin_unlock(ptl);
3075 return;
3076 }
3077 spin_unlock(ptl);
3078
3079 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003080 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++, pte++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003081 pte_t entry, swp_pte;
3082 swp_entry_t swp_entry;
3083
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003084 /*
3085 * We've just crossed page table boundary: need to map next one.
3086 * It can happen if THP was mremaped to non PMD-aligned address.
3087 */
3088 if (unlikely(address == haddr + HPAGE_PMD_SIZE)) {
3089 pte_unmap_unlock(pte - 1, ptl);
3090 pmd = mm_find_pmd(vma->vm_mm, address);
3091 if (!pmd)
3092 return;
3093 pte = pte_offset_map_lock(vma->vm_mm, pmd,
3094 address, &ptl);
3095 }
3096
3097 if (!pte_present(*pte))
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003098 continue;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003099 if (page_to_pfn(page) != pte_pfn(*pte))
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003100 continue;
3101 flush_cache_page(vma, address, page_to_pfn(page));
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003102 entry = ptep_clear_flush(vma, address, pte);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003103 if (pte_dirty(entry))
3104 SetPageDirty(page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003105 swp_entry = make_migration_entry(page, pte_write(entry));
3106 swp_pte = swp_entry_to_pte(swp_entry);
3107 if (pte_soft_dirty(entry))
3108 swp_pte = pte_swp_mksoft_dirty(swp_pte);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003109 set_pte_at(vma->vm_mm, address, pte, swp_pte);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003110 page_remove_rmap(page, false);
3111 put_page(page);
3112 }
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003113 pte_unmap_unlock(pte - 1, ptl);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003114}
3115
3116static void freeze_page(struct anon_vma *anon_vma, struct page *page)
3117{
3118 struct anon_vma_chain *avc;
3119 pgoff_t pgoff = page_to_pgoff(page);
3120
3121 VM_BUG_ON_PAGE(!PageHead(page), page);
3122
3123 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff,
3124 pgoff + HPAGE_PMD_NR - 1) {
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003125 unsigned long address = __vma_address(page, avc->vma);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003126
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003127 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003128 address, address + HPAGE_PMD_SIZE);
3129 freeze_page_vma(avc->vma, page, address);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003130 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003131 address, address + HPAGE_PMD_SIZE);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003132 }
3133}
3134
3135static void unfreeze_page_vma(struct vm_area_struct *vma, struct page *page,
3136 unsigned long address)
3137{
3138 spinlock_t *ptl;
3139 pmd_t *pmd;
3140 pte_t *pte, entry;
3141 swp_entry_t swp_entry;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003142 unsigned long haddr = address & HPAGE_PMD_MASK;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003143 int i, nr = HPAGE_PMD_NR;
3144
3145 /* Skip pages which doesn't belong to the VMA */
3146 if (address < vma->vm_start) {
3147 int off = (vma->vm_start - address) >> PAGE_SHIFT;
3148 page += off;
3149 nr -= off;
3150 address = vma->vm_start;
3151 }
3152
3153 pmd = mm_find_pmd(vma->vm_mm, address);
3154 if (!pmd)
3155 return;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003156
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003157 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003158 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++, pte++) {
3159 /*
3160 * We've just crossed page table boundary: need to map next one.
3161 * It can happen if THP was mremaped to non-PMD aligned address.
3162 */
3163 if (unlikely(address == haddr + HPAGE_PMD_SIZE)) {
3164 pte_unmap_unlock(pte - 1, ptl);
3165 pmd = mm_find_pmd(vma->vm_mm, address);
3166 if (!pmd)
3167 return;
3168 pte = pte_offset_map_lock(vma->vm_mm, pmd,
3169 address, &ptl);
3170 }
3171
3172 if (!is_swap_pte(*pte))
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003173 continue;
3174
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003175 swp_entry = pte_to_swp_entry(*pte);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003176 if (!is_migration_entry(swp_entry))
3177 continue;
3178 if (migration_entry_to_page(swp_entry) != page)
3179 continue;
3180
3181 get_page(page);
3182 page_add_anon_rmap(page, vma, address, false);
3183
3184 entry = pte_mkold(mk_pte(page, vma->vm_page_prot));
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003185 if (PageDirty(page))
3186 entry = pte_mkdirty(entry);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003187 if (is_write_migration_entry(swp_entry))
3188 entry = maybe_mkwrite(entry, vma);
3189
3190 flush_dcache_page(page);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003191 set_pte_at(vma->vm_mm, address, pte, entry);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003192
3193 /* No need to invalidate - it was non-present before */
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003194 update_mmu_cache(vma, address, pte);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003195 }
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003196 pte_unmap_unlock(pte - 1, ptl);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003197}
3198
3199static void unfreeze_page(struct anon_vma *anon_vma, struct page *page)
3200{
3201 struct anon_vma_chain *avc;
3202 pgoff_t pgoff = page_to_pgoff(page);
3203
3204 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
3205 pgoff, pgoff + HPAGE_PMD_NR - 1) {
3206 unsigned long address = __vma_address(page, avc->vma);
3207
3208 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
3209 address, address + HPAGE_PMD_SIZE);
3210 unfreeze_page_vma(avc->vma, page, address);
3211 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
3212 address, address + HPAGE_PMD_SIZE);
3213 }
3214}
3215
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003216static int __split_huge_page_tail(struct page *head, int tail,
3217 struct lruvec *lruvec, struct list_head *list)
3218{
3219 int mapcount;
3220 struct page *page_tail = head + tail;
3221
3222 mapcount = atomic_read(&page_tail->_mapcount) + 1;
3223 VM_BUG_ON_PAGE(atomic_read(&page_tail->_count) != 0, page_tail);
3224
3225 /*
3226 * tail_page->_count is zero and not changing from under us. But
3227 * get_page_unless_zero() may be running from under us on the
3228 * tail_page. If we used atomic_set() below instead of atomic_add(), we
3229 * would then run atomic_set() concurrently with
3230 * get_page_unless_zero(), and atomic_set() is implemented in C not
3231 * using locked ops. spin_unlock on x86 sometime uses locked ops
3232 * because of PPro errata 66, 92, so unless somebody can guarantee
3233 * atomic_set() here would be safe on all archs (and not only on x86),
3234 * it's safer to use atomic_add().
3235 */
3236 atomic_add(mapcount + 1, &page_tail->_count);
3237
3238
3239 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3240 page_tail->flags |= (head->flags &
3241 ((1L << PG_referenced) |
3242 (1L << PG_swapbacked) |
3243 (1L << PG_mlocked) |
3244 (1L << PG_uptodate) |
3245 (1L << PG_active) |
3246 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003247 (1L << PG_unevictable) |
3248 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003249
3250 /*
3251 * After clearing PageTail the gup refcount can be released.
3252 * Page flags also must be visible before we make the page non-compound.
3253 */
3254 smp_wmb();
3255
3256 clear_compound_head(page_tail);
3257
3258 if (page_is_young(head))
3259 set_page_young(page_tail);
3260 if (page_is_idle(head))
3261 set_page_idle(page_tail);
3262
3263 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003264 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003265 page_tail);
3266 page_tail->mapping = head->mapping;
3267
3268 page_tail->index = head->index + tail;
3269 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3270 lru_add_page_tail(head, page_tail, lruvec, list);
3271
3272 return mapcount;
3273}
3274
3275static void __split_huge_page(struct page *page, struct list_head *list)
3276{
3277 struct page *head = compound_head(page);
3278 struct zone *zone = page_zone(head);
3279 struct lruvec *lruvec;
3280 int i, tail_mapcount;
3281
3282 /* prevent PageLRU to go away from under us, and freeze lru stats */
3283 spin_lock_irq(&zone->lru_lock);
3284 lruvec = mem_cgroup_page_lruvec(head, zone);
3285
3286 /* complete memcg works before add pages to LRU */
3287 mem_cgroup_split_huge_fixup(head);
3288
3289 tail_mapcount = 0;
3290 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
3291 tail_mapcount += __split_huge_page_tail(head, i, lruvec, list);
3292 atomic_sub(tail_mapcount, &head->_count);
3293
3294 ClearPageCompound(head);
3295 spin_unlock_irq(&zone->lru_lock);
3296
3297 unfreeze_page(page_anon_vma(head), head);
3298
3299 for (i = 0; i < HPAGE_PMD_NR; i++) {
3300 struct page *subpage = head + i;
3301 if (subpage == page)
3302 continue;
3303 unlock_page(subpage);
3304
3305 /*
3306 * Subpages may be freed if there wasn't any mapping
3307 * like if add_to_swap() is running on a lru page that
3308 * had its mapping zapped. And freeing these pages
3309 * requires taking the lru_lock so we do the put_page
3310 * of the tail pages after the split is complete.
3311 */
3312 put_page(subpage);
3313 }
3314}
3315
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003316int total_mapcount(struct page *page)
3317{
3318 int i, ret;
3319
3320 VM_BUG_ON_PAGE(PageTail(page), page);
3321
3322 if (likely(!PageCompound(page)))
3323 return atomic_read(&page->_mapcount) + 1;
3324
3325 ret = compound_mapcount(page);
3326 if (PageHuge(page))
3327 return ret;
3328 for (i = 0; i < HPAGE_PMD_NR; i++)
3329 ret += atomic_read(&page[i]._mapcount) + 1;
3330 if (PageDoubleMap(page))
3331 ret -= HPAGE_PMD_NR;
3332 return ret;
3333}
3334
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003335/*
3336 * This function splits huge page into normal pages. @page can point to any
3337 * subpage of huge page to split. Split doesn't change the position of @page.
3338 *
3339 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3340 * The huge page must be locked.
3341 *
3342 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3343 *
3344 * Both head page and tail pages will inherit mapping, flags, and so on from
3345 * the hugepage.
3346 *
3347 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3348 * they are not mapped.
3349 *
3350 * Returns 0 if the hugepage is split successfully.
3351 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3352 * us.
3353 */
3354int split_huge_page_to_list(struct page *page, struct list_head *list)
3355{
3356 struct page *head = compound_head(page);
3357 struct anon_vma *anon_vma;
3358 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003359 bool mlocked;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003360
3361 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3362 VM_BUG_ON_PAGE(!PageAnon(page), page);
3363 VM_BUG_ON_PAGE(!PageLocked(page), page);
3364 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3365 VM_BUG_ON_PAGE(!PageCompound(page), page);
3366
3367 /*
3368 * The caller does not necessarily hold an mmap_sem that would prevent
3369 * the anon_vma disappearing so we first we take a reference to it
3370 * and then lock the anon_vma for write. This is similar to
3371 * page_lock_anon_vma_read except the write lock is taken to serialise
3372 * against parallel split or collapse operations.
3373 */
3374 anon_vma = page_get_anon_vma(head);
3375 if (!anon_vma) {
3376 ret = -EBUSY;
3377 goto out;
3378 }
3379 anon_vma_lock_write(anon_vma);
3380
3381 /*
3382 * Racy check if we can split the page, before freeze_page() will
3383 * split PMDs
3384 */
3385 if (total_mapcount(head) != page_count(head) - 1) {
3386 ret = -EBUSY;
3387 goto out_unlock;
3388 }
3389
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003390 mlocked = PageMlocked(page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003391 freeze_page(anon_vma, head);
3392 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3393
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003394 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3395 if (mlocked)
3396 lru_add_drain();
3397
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003398 /* Prevent deferred_split_scan() touching ->_count */
3399 spin_lock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003400 count = page_count(head);
3401 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003402 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003403 if (!list_empty(page_deferred_list(head))) {
3404 split_queue_len--;
3405 list_del(page_deferred_list(head));
3406 }
3407 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003408 __split_huge_page(page, list);
3409 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003410 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003411 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003412 pr_alert("total_mapcount: %u, page_count(): %u\n",
3413 mapcount, count);
3414 if (PageTail(page))
3415 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003416 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003417 BUG();
3418 } else {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003419 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003420 unfreeze_page(anon_vma, head);
3421 ret = -EBUSY;
3422 }
3423
3424out_unlock:
3425 anon_vma_unlock_write(anon_vma);
3426 put_anon_vma(anon_vma);
3427out:
3428 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3429 return ret;
3430}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003431
3432void free_transhuge_page(struct page *page)
3433{
3434 unsigned long flags;
3435
3436 spin_lock_irqsave(&split_queue_lock, flags);
3437 if (!list_empty(page_deferred_list(page))) {
3438 split_queue_len--;
3439 list_del(page_deferred_list(page));
3440 }
3441 spin_unlock_irqrestore(&split_queue_lock, flags);
3442 free_compound_page(page);
3443}
3444
3445void deferred_split_huge_page(struct page *page)
3446{
3447 unsigned long flags;
3448
3449 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3450
3451 spin_lock_irqsave(&split_queue_lock, flags);
3452 if (list_empty(page_deferred_list(page))) {
3453 list_add_tail(page_deferred_list(page), &split_queue);
3454 split_queue_len++;
3455 }
3456 spin_unlock_irqrestore(&split_queue_lock, flags);
3457}
3458
3459static unsigned long deferred_split_count(struct shrinker *shrink,
3460 struct shrink_control *sc)
3461{
3462 /*
3463 * Split a page from split_queue will free up at least one page,
3464 * at most HPAGE_PMD_NR - 1. We don't track exact number.
3465 * Let's use HPAGE_PMD_NR / 2 as ballpark.
3466 */
3467 return ACCESS_ONCE(split_queue_len) * HPAGE_PMD_NR / 2;
3468}
3469
3470static unsigned long deferred_split_scan(struct shrinker *shrink,
3471 struct shrink_control *sc)
3472{
3473 unsigned long flags;
3474 LIST_HEAD(list), *pos, *next;
3475 struct page *page;
3476 int split = 0;
3477
3478 spin_lock_irqsave(&split_queue_lock, flags);
3479 list_splice_init(&split_queue, &list);
3480
3481 /* Take pin on all head pages to avoid freeing them under us */
3482 list_for_each_safe(pos, next, &list) {
3483 page = list_entry((void *)pos, struct page, mapping);
3484 page = compound_head(page);
3485 /* race with put_compound_page() */
3486 if (!get_page_unless_zero(page)) {
3487 list_del_init(page_deferred_list(page));
3488 split_queue_len--;
3489 }
3490 }
3491 spin_unlock_irqrestore(&split_queue_lock, flags);
3492
3493 list_for_each_safe(pos, next, &list) {
3494 page = list_entry((void *)pos, struct page, mapping);
3495 lock_page(page);
3496 /* split_huge_page() removes page from list on success */
3497 if (!split_huge_page(page))
3498 split++;
3499 unlock_page(page);
3500 put_page(page);
3501 }
3502
3503 spin_lock_irqsave(&split_queue_lock, flags);
3504 list_splice_tail(&list, &split_queue);
3505 spin_unlock_irqrestore(&split_queue_lock, flags);
3506
3507 return split * HPAGE_PMD_NR / 2;
3508}
3509
3510static struct shrinker deferred_split_shrinker = {
3511 .count_objects = deferred_split_count,
3512 .scan_objects = deferred_split_scan,
3513 .seeks = DEFAULT_SEEKS,
3514};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003515
3516#ifdef CONFIG_DEBUG_FS
3517static int split_huge_pages_set(void *data, u64 val)
3518{
3519 struct zone *zone;
3520 struct page *page;
3521 unsigned long pfn, max_zone_pfn;
3522 unsigned long total = 0, split = 0;
3523
3524 if (val != 1)
3525 return -EINVAL;
3526
3527 for_each_populated_zone(zone) {
3528 max_zone_pfn = zone_end_pfn(zone);
3529 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3530 if (!pfn_valid(pfn))
3531 continue;
3532
3533 page = pfn_to_page(pfn);
3534 if (!get_page_unless_zero(page))
3535 continue;
3536
3537 if (zone != page_zone(page))
3538 goto next;
3539
3540 if (!PageHead(page) || !PageAnon(page) ||
3541 PageHuge(page))
3542 goto next;
3543
3544 total++;
3545 lock_page(page);
3546 if (!split_huge_page(page))
3547 split++;
3548 unlock_page(page);
3549next:
3550 put_page(page);
3551 }
3552 }
3553
3554 pr_info("%lu of %lu THP split", split, total);
3555
3556 return 0;
3557}
3558DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3559 "%llu\n");
3560
3561static int __init split_huge_pages_debugfs(void)
3562{
3563 void *ret;
3564
3565 ret = debugfs_create_file("split_huge_pages", 0644, NULL, NULL,
3566 &split_huge_pages_fops);
3567 if (!ret)
3568 pr_warn("Failed to create split_huge_pages in debugfs");
3569 return 0;
3570}
3571late_initcall(split_huge_pages_debugfs);
3572#endif