blob: f283cb7c480e15f6ca37ab7b8d1f602ede7edffc [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>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080024#include <linux/mman.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020025#include <linux/pagemap.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000026#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080027#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070028#include <linux/userfaultfd_k.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070029#include <linux/page_idle.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080030
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080031#include <asm/tlb.h>
32#include <asm/pgalloc.h>
33#include "internal.h"
34
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080035enum scan_result {
36 SCAN_FAIL,
37 SCAN_SUCCEED,
38 SCAN_PMD_NULL,
39 SCAN_EXCEED_NONE_PTE,
40 SCAN_PTE_NON_PRESENT,
41 SCAN_PAGE_RO,
42 SCAN_NO_REFERENCED_PAGE,
43 SCAN_PAGE_NULL,
44 SCAN_SCAN_ABORT,
45 SCAN_PAGE_COUNT,
46 SCAN_PAGE_LRU,
47 SCAN_PAGE_LOCK,
48 SCAN_PAGE_ANON,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080049 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080050 SCAN_ANY_PROCESS,
51 SCAN_VMA_NULL,
52 SCAN_VMA_CHECK,
53 SCAN_ADDRESS_RANGE,
54 SCAN_SWAP_CACHE_PAGE,
55 SCAN_DEL_PAGE_LRU,
56 SCAN_ALLOC_HUGE_PAGE_FAIL,
57 SCAN_CGROUP_CHARGE_FAIL
58};
59
60#define CREATE_TRACE_POINTS
61#include <trace/events/huge_memory.h>
62
Andrea Arcangeliba761492011-01-13 15:46:58 -080063/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080064 * By default transparent hugepage support is disabled in order that avoid
65 * to risk increase the memory footprint of applications without a guaranteed
66 * benefit. When transparent hugepage support is enabled, is for all mappings,
67 * and khugepaged scans all mappings.
68 * Defrag is invoked by khugepaged hugepage allocations and by page faults
69 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080070 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080071unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080072#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080073 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080074#endif
75#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
76 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
77#endif
Andrea Arcangelid39d33c2011-01-13 15:47:05 -080078 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080079 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
80 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080081
82/* default scan 8*512 pte (or vmas) every 30 second */
83static unsigned int khugepaged_pages_to_scan __read_mostly = HPAGE_PMD_NR*8;
84static unsigned int khugepaged_pages_collapsed;
85static unsigned int khugepaged_full_scans;
86static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
87/* during fragmentation poll the hugepage allocator once every minute */
88static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
89static struct task_struct *khugepaged_thread __read_mostly;
90static DEFINE_MUTEX(khugepaged_mutex);
91static DEFINE_SPINLOCK(khugepaged_mm_lock);
92static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
93/*
94 * default collapse hugepages if there is at least one pte mapped like
95 * it would have happened if the vma was large enough during page
96 * fault.
97 */
98static unsigned int khugepaged_max_ptes_none __read_mostly = HPAGE_PMD_NR-1;
99
100static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800101static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700102static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800103
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800104#define MM_SLOTS_HASH_BITS 10
105static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
106
Andrea Arcangeliba761492011-01-13 15:46:58 -0800107static struct kmem_cache *mm_slot_cache __read_mostly;
108
109/**
110 * struct mm_slot - hash lookup from mm to mm_slot
111 * @hash: hash collision list
112 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
113 * @mm: the mm that this information is valid for
114 */
115struct mm_slot {
116 struct hlist_node hash;
117 struct list_head mm_node;
118 struct mm_struct *mm;
119};
120
121/**
122 * struct khugepaged_scan - cursor for scanning
123 * @mm_head: the head of the mm list to scan
124 * @mm_slot: the current mm_slot we are scanning
125 * @address: the next address inside that to be scanned
126 *
127 * There is only the one khugepaged_scan instance of this cursor structure.
128 */
129struct khugepaged_scan {
130 struct list_head mm_head;
131 struct mm_slot *mm_slot;
132 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700133};
134static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800135 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
136};
137
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800138static DEFINE_SPINLOCK(split_queue_lock);
139static LIST_HEAD(split_queue);
140static unsigned long split_queue_len;
141static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800142
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700143static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800144{
145 struct zone *zone;
146 int nr_zones = 0;
147 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800148
Andrea Arcangelif0005652011-01-13 15:47:04 -0800149 for_each_populated_zone(zone)
150 nr_zones++;
151
Mel Gorman974a7862015-11-06 16:28:34 -0800152 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800153 recommended_min = pageblock_nr_pages * nr_zones * 2;
154
155 /*
156 * Make sure that on average at least two pageblocks are almost free
157 * of another type, one for a migratetype to fall back to and a
158 * second to avoid subsequent fallbacks of other types There are 3
159 * MIGRATE_TYPES we care about.
160 */
161 recommended_min += pageblock_nr_pages * nr_zones *
162 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
163
164 /* don't ever allow to reserve more than 5% of the lowmem */
165 recommended_min = min(recommended_min,
166 (unsigned long) nr_free_buffer_pages() / 20);
167 recommended_min <<= (PAGE_SHIFT-10);
168
Han Pingtian42aa83c2014-01-23 15:53:28 -0800169 if (recommended_min > min_free_kbytes) {
170 if (user_min_free_kbytes >= 0)
171 pr_info("raising min_free_kbytes from %d to %lu "
172 "to help transparent hugepage allocations\n",
173 min_free_kbytes, recommended_min);
174
Andrea Arcangelif0005652011-01-13 15:47:04 -0800175 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800176 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800177 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800179
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700180static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800181{
182 int err = 0;
183 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800184 if (!khugepaged_thread)
185 khugepaged_thread = kthread_run(khugepaged, NULL,
186 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530187 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700188 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800189 err = PTR_ERR(khugepaged_thread);
190 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700191 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800192 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700193
194 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800195 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800196
197 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700198 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700199 kthread_stop(khugepaged_thread);
200 khugepaged_thread = NULL;
201 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700202fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800203 return err;
204}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800205
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800206static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800207struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800208
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700209struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800210{
211 struct page *zero_page;
212retry:
213 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700214 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800215
216 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
217 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800218 if (!zero_page) {
219 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700220 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800221 }
222 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800223 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700224 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800225 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700226 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227 goto retry;
228 }
229
230 /* We take additional reference here. It will be put back by shrinker */
231 atomic_set(&huge_zero_refcount, 2);
232 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700233 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800234}
235
236static void put_huge_zero_page(void)
237{
238 /*
239 * Counter should never go to zero here. Only shrinker can put
240 * last reference.
241 */
242 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
243}
244
Glauber Costa48896462013-08-28 10:18:15 +1000245static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
246 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800247{
Glauber Costa48896462013-08-28 10:18:15 +1000248 /* we can free zero page only if last reference remains */
249 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
250}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800251
Glauber Costa48896462013-08-28 10:18:15 +1000252static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
253 struct shrink_control *sc)
254{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800255 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700256 struct page *zero_page = xchg(&huge_zero_page, NULL);
257 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700258 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000259 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800260 }
261
262 return 0;
263}
264
265static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000266 .count_objects = shrink_huge_zero_page_count,
267 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800268 .seeks = DEFAULT_SEEKS,
269};
270
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800271#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800272
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800273static ssize_t double_flag_show(struct kobject *kobj,
274 struct kobj_attribute *attr, char *buf,
275 enum transparent_hugepage_flag enabled,
276 enum transparent_hugepage_flag req_madv)
277{
278 if (test_bit(enabled, &transparent_hugepage_flags)) {
279 VM_BUG_ON(test_bit(req_madv, &transparent_hugepage_flags));
280 return sprintf(buf, "[always] madvise never\n");
281 } else if (test_bit(req_madv, &transparent_hugepage_flags))
282 return sprintf(buf, "always [madvise] never\n");
283 else
284 return sprintf(buf, "always madvise [never]\n");
285}
286static ssize_t double_flag_store(struct kobject *kobj,
287 struct kobj_attribute *attr,
288 const char *buf, size_t count,
289 enum transparent_hugepage_flag enabled,
290 enum transparent_hugepage_flag req_madv)
291{
292 if (!memcmp("always", buf,
293 min(sizeof("always")-1, count))) {
294 set_bit(enabled, &transparent_hugepage_flags);
295 clear_bit(req_madv, &transparent_hugepage_flags);
296 } else if (!memcmp("madvise", buf,
297 min(sizeof("madvise")-1, count))) {
298 clear_bit(enabled, &transparent_hugepage_flags);
299 set_bit(req_madv, &transparent_hugepage_flags);
300 } else if (!memcmp("never", buf,
301 min(sizeof("never")-1, count))) {
302 clear_bit(enabled, &transparent_hugepage_flags);
303 clear_bit(req_madv, &transparent_hugepage_flags);
304 } else
305 return -EINVAL;
306
307 return count;
308}
309
310static ssize_t enabled_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *buf)
312{
313 return double_flag_show(kobj, attr, buf,
314 TRANSPARENT_HUGEPAGE_FLAG,
315 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
316}
317static ssize_t enabled_store(struct kobject *kobj,
318 struct kobj_attribute *attr,
319 const char *buf, size_t count)
320{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800321 ssize_t ret;
322
323 ret = double_flag_store(kobj, attr, buf, count,
324 TRANSPARENT_HUGEPAGE_FLAG,
325 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
326
327 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700328 int err;
329
330 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700331 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700332 mutex_unlock(&khugepaged_mutex);
333
Andrea Arcangeliba761492011-01-13 15:46:58 -0800334 if (err)
335 ret = err;
336 }
337
338 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800339}
340static struct kobj_attribute enabled_attr =
341 __ATTR(enabled, 0644, enabled_show, enabled_store);
342
343static ssize_t single_flag_show(struct kobject *kobj,
344 struct kobj_attribute *attr, char *buf,
345 enum transparent_hugepage_flag flag)
346{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700347 return sprintf(buf, "%d\n",
348 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800349}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700350
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800351static ssize_t single_flag_store(struct kobject *kobj,
352 struct kobj_attribute *attr,
353 const char *buf, size_t count,
354 enum transparent_hugepage_flag flag)
355{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700356 unsigned long value;
357 int ret;
358
359 ret = kstrtoul(buf, 10, &value);
360 if (ret < 0)
361 return ret;
362 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800363 return -EINVAL;
364
Ben Hutchingse27e6152011-04-14 15:22:21 -0700365 if (value)
366 set_bit(flag, &transparent_hugepage_flags);
367 else
368 clear_bit(flag, &transparent_hugepage_flags);
369
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800370 return count;
371}
372
373/*
374 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
375 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
376 * memory just to allocate one more hugepage.
377 */
378static ssize_t defrag_show(struct kobject *kobj,
379 struct kobj_attribute *attr, char *buf)
380{
381 return double_flag_show(kobj, attr, buf,
382 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
383 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
384}
385static ssize_t defrag_store(struct kobject *kobj,
386 struct kobj_attribute *attr,
387 const char *buf, size_t count)
388{
389 return double_flag_store(kobj, attr, buf, count,
390 TRANSPARENT_HUGEPAGE_DEFRAG_FLAG,
391 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
392}
393static struct kobj_attribute defrag_attr =
394 __ATTR(defrag, 0644, defrag_show, defrag_store);
395
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800396static ssize_t use_zero_page_show(struct kobject *kobj,
397 struct kobj_attribute *attr, char *buf)
398{
399 return single_flag_show(kobj, attr, buf,
400 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
401}
402static ssize_t use_zero_page_store(struct kobject *kobj,
403 struct kobj_attribute *attr, const char *buf, size_t count)
404{
405 return single_flag_store(kobj, attr, buf, count,
406 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
407}
408static struct kobj_attribute use_zero_page_attr =
409 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800410#ifdef CONFIG_DEBUG_VM
411static ssize_t debug_cow_show(struct kobject *kobj,
412 struct kobj_attribute *attr, char *buf)
413{
414 return single_flag_show(kobj, attr, buf,
415 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
416}
417static ssize_t debug_cow_store(struct kobject *kobj,
418 struct kobj_attribute *attr,
419 const char *buf, size_t count)
420{
421 return single_flag_store(kobj, attr, buf, count,
422 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
423}
424static struct kobj_attribute debug_cow_attr =
425 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
426#endif /* CONFIG_DEBUG_VM */
427
428static struct attribute *hugepage_attr[] = {
429 &enabled_attr.attr,
430 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800431 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800432#ifdef CONFIG_DEBUG_VM
433 &debug_cow_attr.attr,
434#endif
435 NULL,
436};
437
438static struct attribute_group hugepage_attr_group = {
439 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800440};
441
442static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
443 struct kobj_attribute *attr,
444 char *buf)
445{
446 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
447}
448
449static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
450 struct kobj_attribute *attr,
451 const char *buf, size_t count)
452{
453 unsigned long msecs;
454 int err;
455
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700456 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800457 if (err || msecs > UINT_MAX)
458 return -EINVAL;
459
460 khugepaged_scan_sleep_millisecs = msecs;
461 wake_up_interruptible(&khugepaged_wait);
462
463 return count;
464}
465static struct kobj_attribute scan_sleep_millisecs_attr =
466 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
467 scan_sleep_millisecs_store);
468
469static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
470 struct kobj_attribute *attr,
471 char *buf)
472{
473 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
474}
475
476static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
477 struct kobj_attribute *attr,
478 const char *buf, size_t count)
479{
480 unsigned long msecs;
481 int err;
482
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700483 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800484 if (err || msecs > UINT_MAX)
485 return -EINVAL;
486
487 khugepaged_alloc_sleep_millisecs = msecs;
488 wake_up_interruptible(&khugepaged_wait);
489
490 return count;
491}
492static struct kobj_attribute alloc_sleep_millisecs_attr =
493 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
494 alloc_sleep_millisecs_store);
495
496static ssize_t pages_to_scan_show(struct kobject *kobj,
497 struct kobj_attribute *attr,
498 char *buf)
499{
500 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
501}
502static ssize_t pages_to_scan_store(struct kobject *kobj,
503 struct kobj_attribute *attr,
504 const char *buf, size_t count)
505{
506 int err;
507 unsigned long pages;
508
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700509 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800510 if (err || !pages || pages > UINT_MAX)
511 return -EINVAL;
512
513 khugepaged_pages_to_scan = pages;
514
515 return count;
516}
517static struct kobj_attribute pages_to_scan_attr =
518 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
519 pages_to_scan_store);
520
521static ssize_t pages_collapsed_show(struct kobject *kobj,
522 struct kobj_attribute *attr,
523 char *buf)
524{
525 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
526}
527static struct kobj_attribute pages_collapsed_attr =
528 __ATTR_RO(pages_collapsed);
529
530static ssize_t full_scans_show(struct kobject *kobj,
531 struct kobj_attribute *attr,
532 char *buf)
533{
534 return sprintf(buf, "%u\n", khugepaged_full_scans);
535}
536static struct kobj_attribute full_scans_attr =
537 __ATTR_RO(full_scans);
538
539static ssize_t khugepaged_defrag_show(struct kobject *kobj,
540 struct kobj_attribute *attr, char *buf)
541{
542 return single_flag_show(kobj, attr, buf,
543 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
544}
545static ssize_t khugepaged_defrag_store(struct kobject *kobj,
546 struct kobj_attribute *attr,
547 const char *buf, size_t count)
548{
549 return single_flag_store(kobj, attr, buf, count,
550 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
551}
552static struct kobj_attribute khugepaged_defrag_attr =
553 __ATTR(defrag, 0644, khugepaged_defrag_show,
554 khugepaged_defrag_store);
555
556/*
557 * max_ptes_none controls if khugepaged should collapse hugepages over
558 * any unmapped ptes in turn potentially increasing the memory
559 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
560 * reduce the available free memory in the system as it
561 * runs. Increasing max_ptes_none will instead potentially reduce the
562 * free memory in the system during the khugepaged scan.
563 */
564static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
565 struct kobj_attribute *attr,
566 char *buf)
567{
568 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
569}
570static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
571 struct kobj_attribute *attr,
572 const char *buf, size_t count)
573{
574 int err;
575 unsigned long max_ptes_none;
576
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700577 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800578 if (err || max_ptes_none > HPAGE_PMD_NR-1)
579 return -EINVAL;
580
581 khugepaged_max_ptes_none = max_ptes_none;
582
583 return count;
584}
585static struct kobj_attribute khugepaged_max_ptes_none_attr =
586 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
587 khugepaged_max_ptes_none_store);
588
589static struct attribute *khugepaged_attr[] = {
590 &khugepaged_defrag_attr.attr,
591 &khugepaged_max_ptes_none_attr.attr,
592 &pages_to_scan_attr.attr,
593 &pages_collapsed_attr.attr,
594 &full_scans_attr.attr,
595 &scan_sleep_millisecs_attr.attr,
596 &alloc_sleep_millisecs_attr.attr,
597 NULL,
598};
599
600static struct attribute_group khugepaged_attr_group = {
601 .attrs = khugepaged_attr,
602 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800603};
Shaohua Li569e5592012-01-12 17:19:11 -0800604
605static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
606{
607 int err;
608
609 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
610 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700611 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800612 return -ENOMEM;
613 }
614
615 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
616 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700617 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800618 goto delete_obj;
619 }
620
621 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
622 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700623 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800624 goto remove_hp_group;
625 }
626
627 return 0;
628
629remove_hp_group:
630 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
631delete_obj:
632 kobject_put(*hugepage_kobj);
633 return err;
634}
635
636static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
637{
638 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
639 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
640 kobject_put(hugepage_kobj);
641}
642#else
643static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
644{
645 return 0;
646}
647
648static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
649{
650}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800651#endif /* CONFIG_SYSFS */
652
653static int __init hugepage_init(void)
654{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800655 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800656 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800657
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800658 if (!has_transparent_hugepage()) {
659 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800660 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800661 }
662
Shaohua Li569e5592012-01-12 17:19:11 -0800663 err = hugepage_init_sysfs(&hugepage_kobj);
664 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700665 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800666
667 err = khugepaged_slab_init();
668 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700669 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800670
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700671 err = register_shrinker(&huge_zero_page_shrinker);
672 if (err)
673 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800674 err = register_shrinker(&deferred_split_shrinker);
675 if (err)
676 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800677
Rik van Riel97562cd2011-01-13 15:47:12 -0800678 /*
679 * By default disable transparent hugepages on smaller systems,
680 * where the extra memory used could hurt more than TLB overhead
681 * is likely to save. The admin can still enable it through /sys.
682 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700683 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800684 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700685 return 0;
686 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800687
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700688 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700689 if (err)
690 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800691
Shaohua Li569e5592012-01-12 17:19:11 -0800692 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700693err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800694 unregister_shrinker(&deferred_split_shrinker);
695err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700696 unregister_shrinker(&huge_zero_page_shrinker);
697err_hzp_shrinker:
698 khugepaged_slab_exit();
699err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800700 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700701err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800702 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800703}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800704subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800705
706static int __init setup_transparent_hugepage(char *str)
707{
708 int ret = 0;
709 if (!str)
710 goto out;
711 if (!strcmp(str, "always")) {
712 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
713 &transparent_hugepage_flags);
714 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
715 &transparent_hugepage_flags);
716 ret = 1;
717 } else if (!strcmp(str, "madvise")) {
718 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
719 &transparent_hugepage_flags);
720 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
721 &transparent_hugepage_flags);
722 ret = 1;
723 } else if (!strcmp(str, "never")) {
724 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
725 &transparent_hugepage_flags);
726 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
727 &transparent_hugepage_flags);
728 ret = 1;
729 }
730out:
731 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700732 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800733 return ret;
734}
735__setup("transparent_hugepage=", setup_transparent_hugepage);
736
Mel Gormanb32967f2012-11-19 12:35:47 +0000737pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800738{
739 if (likely(vma->vm_flags & VM_WRITE))
740 pmd = pmd_mkwrite(pmd);
741 return pmd;
742}
743
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700744static inline pmd_t mk_huge_pmd(struct page *page, pgprot_t prot)
Bob Liub3092b32012-12-11 16:00:41 -0800745{
746 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700747 entry = mk_pmd(page, prot);
Bob Liub3092b32012-12-11 16:00:41 -0800748 entry = pmd_mkhuge(entry);
749 return entry;
750}
751
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800752static inline struct list_head *page_deferred_list(struct page *page)
753{
754 /*
755 * ->lru in the tail pages is occupied by compound_head.
756 * Let's use ->mapping + ->index in the second tail page as list_head.
757 */
758 return (struct list_head *)&page[2].mapping;
759}
760
761void prep_transhuge_page(struct page *page)
762{
763 /*
764 * we use page->mapping and page->indexlru in second tail page
765 * as list_head: assuming THP order >= 2
766 */
767 BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
768
769 INIT_LIST_HEAD(page_deferred_list(page));
770 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
771}
772
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800773static int __do_huge_pmd_anonymous_page(struct mm_struct *mm,
774 struct vm_area_struct *vma,
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700775 unsigned long address, pmd_t *pmd,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700776 struct page *page, gfp_t gfp,
777 unsigned int flags)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800778{
Johannes Weiner00501b52014-08-08 14:19:20 -0700779 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800780 pgtable_t pgtable;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800781 spinlock_t *ptl;
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700782 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800783
Sasha Levin309381fea2014-01-23 15:52:54 -0800784 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700785
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800786 if (mem_cgroup_try_charge(page, mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700787 put_page(page);
788 count_vm_event(THP_FAULT_FALLBACK);
789 return VM_FAULT_FALLBACK;
790 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800791
Johannes Weiner00501b52014-08-08 14:19:20 -0700792 pgtable = pte_alloc_one(mm, haddr);
793 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800794 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700795 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700796 return VM_FAULT_OOM;
797 }
798
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800799 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700800 /*
801 * The memory barrier inside __SetPageUptodate makes sure that
802 * clear_huge_page writes become visible before the set_pmd_at()
803 * write.
804 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800805 __SetPageUptodate(page);
806
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800807 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800808 if (unlikely(!pmd_none(*pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800809 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800810 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800811 put_page(page);
812 pte_free(mm, pgtable);
813 } else {
814 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700815
816 /* Deliver the page fault to userland */
817 if (userfaultfd_missing(vma)) {
818 int ret;
819
820 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800821 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700822 put_page(page);
823 pte_free(mm, pgtable);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700824 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700825 VM_UFFD_MISSING);
826 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
827 return ret;
828 }
829
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700830 entry = mk_huge_pmd(page, vma->vm_page_prot);
831 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800832 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800833 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700834 lru_cache_add_active_or_unevictable(page, vma);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700835 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800836 set_pmd_at(mm, haddr, pmd, entry);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800837 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800838 atomic_long_inc(&mm->nr_ptes);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800839 spin_unlock(ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700840 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800841 }
842
David Rientjesaa2e8782012-05-29 15:06:17 -0700843 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800844}
845
Andi Kleencc5d4622011-03-22 16:33:13 -0700846static inline gfp_t alloc_hugepage_gfpmask(int defrag, gfp_t extra_gfp)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800847{
Mel Gorman71baba42015-11-06 16:28:28 -0800848 return (GFP_TRANSHUGE & ~(defrag ? 0 : __GFP_RECLAIM)) | extra_gfp;
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800849}
850
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800851/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700852static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800853 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700854 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800855{
856 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700857 if (!pmd_none(*pmd))
858 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700859 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800860 entry = pmd_mkhuge(entry);
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -0700861 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800862 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800863 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700864 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800865}
866
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800867int do_huge_pmd_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
868 unsigned long address, pmd_t *pmd,
869 unsigned int flags)
870{
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800871 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800872 struct page *page;
873 unsigned long haddr = address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800874
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700875 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700876 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700877 if (unlikely(anon_vma_prepare(vma)))
878 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700879 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700880 return VM_FAULT_OOM;
Dominik Dingel593befa2014-10-23 12:07:44 +0200881 if (!(flags & FAULT_FLAG_WRITE) && !mm_forbids_zeropage(mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700882 transparent_hugepage_use_zero_page()) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800883 spinlock_t *ptl;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700884 pgtable_t pgtable;
885 struct page *zero_page;
886 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700887 int ret;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700888 pgtable = pte_alloc_one(mm, haddr);
889 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800890 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700891 zero_page = get_huge_zero_page();
892 if (unlikely(!zero_page)) {
893 pte_free(mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700894 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700895 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700896 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800897 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700898 ret = 0;
899 set = false;
900 if (pmd_none(*pmd)) {
901 if (userfaultfd_missing(vma)) {
902 spin_unlock(ptl);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700903 ret = handle_userfault(vma, address, flags,
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700904 VM_UFFD_MISSING);
905 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
906 } else {
907 set_huge_zero_page(pgtable, mm, vma,
908 haddr, pmd,
909 zero_page);
910 spin_unlock(ptl);
911 set = true;
912 }
913 } else
914 spin_unlock(ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700915 if (!set) {
916 pte_free(mm, pgtable);
917 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800918 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700919 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800920 }
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800921 gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
922 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700923 if (unlikely(!page)) {
924 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700925 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700926 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800927 prep_transhuge_page(page);
Andrea Arcangeli230c92a2015-09-04 15:47:20 -0700928 return __do_huge_pmd_anonymous_page(mm, vma, address, pmd, page, gfp,
929 flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800930}
931
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700932static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700933 pmd_t *pmd, unsigned long pfn, pgprot_t prot, bool write)
934{
935 struct mm_struct *mm = vma->vm_mm;
936 pmd_t entry;
937 spinlock_t *ptl;
938
939 ptl = pmd_lock(mm, pmd);
940 if (pmd_none(*pmd)) {
941 entry = pmd_mkhuge(pfn_pmd(pfn, prot));
942 if (write) {
943 entry = pmd_mkyoung(pmd_mkdirty(entry));
944 entry = maybe_pmd_mkwrite(entry, vma);
945 }
946 set_pmd_at(mm, addr, pmd, entry);
947 update_mmu_cache_pmd(vma, addr, pmd);
948 }
949 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700950}
951
952int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
953 pmd_t *pmd, unsigned long pfn, bool write)
954{
955 pgprot_t pgprot = vma->vm_page_prot;
956 /*
957 * If we had pmd_special, we could avoid all these restrictions,
958 * but we need to be consistent with PTEs and architectures that
959 * can't support a 'special' bit.
960 */
961 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
962 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
963 (VM_PFNMAP|VM_MIXEDMAP));
964 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
965 BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
966
967 if (addr < vma->vm_start || addr >= vma->vm_end)
968 return VM_FAULT_SIGBUS;
969 if (track_pfn_insert(vma, &pgprot, pfn))
970 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700971 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
972 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700973}
974
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800975int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
976 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
977 struct vm_area_struct *vma)
978{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800979 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800980 struct page *src_page;
981 pmd_t pmd;
982 pgtable_t pgtable;
983 int ret;
984
985 ret = -ENOMEM;
986 pgtable = pte_alloc_one(dst_mm, addr);
987 if (unlikely(!pgtable))
988 goto out;
989
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800990 dst_ptl = pmd_lock(dst_mm, dst_pmd);
991 src_ptl = pmd_lockptr(src_mm, src_pmd);
992 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800993
994 ret = -EAGAIN;
995 pmd = *src_pmd;
996 if (unlikely(!pmd_trans_huge(pmd))) {
997 pte_free(dst_mm, pgtable);
998 goto out_unlock;
999 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001000 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001001 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001002 * under splitting since we don't split the page itself, only pmd to
1003 * a page table.
1004 */
1005 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001006 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001007 /*
1008 * get_huge_zero_page() will never allocate a new page here,
1009 * since we already have a zero page to copy. It just takes a
1010 * reference.
1011 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001012 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001013 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001014 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001015 ret = 0;
1016 goto out_unlock;
1017 }
Mel Gormande466bd2013-12-18 17:08:42 -08001018
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001019 src_page = pmd_page(pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001020 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001021 get_page(src_page);
Kirill A. Shutemov53f92632016-01-15 16:53:42 -08001022 page_dup_rmap(src_page, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001023 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1024
1025 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1026 pmd = pmd_mkold(pmd_wrprotect(pmd));
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001027 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001028 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -08001029 atomic_long_inc(&dst_mm->nr_ptes);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001030
1031 ret = 0;
1032out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001033 spin_unlock(src_ptl);
1034 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001035out:
1036 return ret;
1037}
1038
Will Deacona1dd4502012-12-11 16:01:27 -08001039void huge_pmd_set_accessed(struct mm_struct *mm,
1040 struct vm_area_struct *vma,
1041 unsigned long address,
1042 pmd_t *pmd, pmd_t orig_pmd,
1043 int dirty)
1044{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001045 spinlock_t *ptl;
Will Deacona1dd4502012-12-11 16:01:27 -08001046 pmd_t entry;
1047 unsigned long haddr;
1048
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001049 ptl = pmd_lock(mm, pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001050 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1051 goto unlock;
1052
1053 entry = pmd_mkyoung(orig_pmd);
1054 haddr = address & HPAGE_PMD_MASK;
1055 if (pmdp_set_access_flags(vma, haddr, pmd, entry, dirty))
1056 update_mmu_cache_pmd(vma, address, pmd);
1057
1058unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001059 spin_unlock(ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001060}
1061
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001062static int do_huge_pmd_wp_page_fallback(struct mm_struct *mm,
1063 struct vm_area_struct *vma,
1064 unsigned long address,
1065 pmd_t *pmd, pmd_t orig_pmd,
1066 struct page *page,
1067 unsigned long haddr)
1068{
Johannes Weiner00501b52014-08-08 14:19:20 -07001069 struct mem_cgroup *memcg;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001070 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001071 pgtable_t pgtable;
1072 pmd_t _pmd;
1073 int ret = 0, i;
1074 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001075 unsigned long mmun_start; /* For mmu_notifiers */
1076 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001077
1078 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1079 GFP_KERNEL);
1080 if (unlikely(!pages)) {
1081 ret |= VM_FAULT_OOM;
1082 goto out;
1083 }
1084
1085 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001086 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
1087 __GFP_OTHER_NODE,
Andi Kleen19ee1512011-03-04 17:36:31 -08001088 vma, address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001089 if (unlikely(!pages[i] ||
Johannes Weiner00501b52014-08-08 14:19:20 -07001090 mem_cgroup_try_charge(pages[i], mm, GFP_KERNEL,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001091 &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001092 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001093 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001094 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001095 memcg = (void *)page_private(pages[i]);
1096 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001097 mem_cgroup_cancel_charge(pages[i], memcg,
1098 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001099 put_page(pages[i]);
1100 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001101 kfree(pages);
1102 ret |= VM_FAULT_OOM;
1103 goto out;
1104 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001105 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001106 }
1107
1108 for (i = 0; i < HPAGE_PMD_NR; i++) {
1109 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001110 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001111 __SetPageUptodate(pages[i]);
1112 cond_resched();
1113 }
1114
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001115 mmun_start = haddr;
1116 mmun_end = haddr + HPAGE_PMD_SIZE;
1117 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1118
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001119 ptl = pmd_lock(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001120 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1121 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001122 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001123
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001124 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001125 /* leave pmd empty until pte is filled */
1126
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001127 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001128 pmd_populate(mm, &_pmd, pgtable);
1129
1130 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
1131 pte_t *pte, entry;
1132 entry = mk_pte(pages[i], vma->vm_page_prot);
1133 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001134 memcg = (void *)page_private(pages[i]);
1135 set_page_private(pages[i], 0);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001136 page_add_new_anon_rmap(pages[i], vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001137 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001138 lru_cache_add_active_or_unevictable(pages[i], vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001139 pte = pte_offset_map(&_pmd, haddr);
1140 VM_BUG_ON(!pte_none(*pte));
1141 set_pte_at(mm, haddr, pte, entry);
1142 pte_unmap(pte);
1143 }
1144 kfree(pages);
1145
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001146 smp_wmb(); /* make pte visible before pmd */
1147 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001148 page_remove_rmap(page, true);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001149 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001150
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001151 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1152
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001153 ret |= VM_FAULT_WRITE;
1154 put_page(page);
1155
1156out:
1157 return ret;
1158
1159out_free_pages:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001160 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001161 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001162 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001163 memcg = (void *)page_private(pages[i]);
1164 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001165 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001166 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001167 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001168 kfree(pages);
1169 goto out;
1170}
1171
1172int do_huge_pmd_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1173 unsigned long address, pmd_t *pmd, pmd_t orig_pmd)
1174{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001175 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001176 int ret = 0;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001177 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001178 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001179 unsigned long haddr;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001180 unsigned long mmun_start; /* For mmu_notifiers */
1181 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001182 gfp_t huge_gfp; /* for allocation and charge */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001183
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001184 ptl = pmd_lockptr(mm, pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001185 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001186 haddr = address & HPAGE_PMD_MASK;
1187 if (is_huge_zero_pmd(orig_pmd))
1188 goto alloc;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001189 spin_lock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001190 if (unlikely(!pmd_same(*pmd, orig_pmd)))
1191 goto out_unlock;
1192
1193 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001194 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001195 /*
1196 * We can only reuse the page if nobody else maps the huge page or it's
1197 * part. We can do it by checking page_mapcount() on each sub-page, but
1198 * it's expensive.
1199 * The cheaper way is to check page_count() to be equal 1: every
1200 * mapcount takes page reference reference, so this way we can
1201 * guarantee, that the PMD is the only mapping.
1202 * This can give false negative if somebody pinned the page, but that's
1203 * fine.
1204 */
1205 if (page_mapcount(page) == 1 && page_count(page) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001206 pmd_t entry;
1207 entry = pmd_mkyoung(orig_pmd);
1208 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
1209 if (pmdp_set_access_flags(vma, haddr, pmd, entry, 1))
David Millerb113da62012-10-08 16:34:25 -07001210 update_mmu_cache_pmd(vma, address, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001211 ret |= VM_FAULT_WRITE;
1212 goto out_unlock;
1213 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001214 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001215 spin_unlock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001216alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001217 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001218 !transparent_hugepage_debug_cow()) {
Michal Hocko3b363692015-04-15 16:13:29 -07001219 huge_gfp = alloc_hugepage_gfpmask(transparent_hugepage_defrag(vma), 0);
1220 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001221 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001222 new_page = NULL;
1223
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001224 if (likely(new_page)) {
1225 prep_transhuge_page(new_page);
1226 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001227 if (!page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001228 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001229 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001230 } else {
1231 ret = do_huge_pmd_wp_page_fallback(mm, vma, address,
1232 pmd, orig_pmd, page, haddr);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001233 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001234 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001235 ret |= VM_FAULT_FALLBACK;
1236 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001237 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001238 }
David Rientjes17766dd2013-09-12 15:14:06 -07001239 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001240 goto out;
1241 }
1242
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001243 if (unlikely(mem_cgroup_try_charge(new_page, mm, huge_gfp, &memcg,
1244 true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001245 put_page(new_page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001246 if (page) {
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001247 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001248 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001249 } else
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08001250 split_huge_pmd(vma, pmd, address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001251 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001252 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001253 goto out;
1254 }
1255
David Rientjes17766dd2013-09-12 15:14:06 -07001256 count_vm_event(THP_FAULT_ALLOC);
1257
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001258 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001259 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1260 else
1261 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001262 __SetPageUptodate(new_page);
1263
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001264 mmun_start = haddr;
1265 mmun_end = haddr + HPAGE_PMD_SIZE;
1266 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1267
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001268 spin_lock(ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001269 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001270 put_page(page);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001271 if (unlikely(!pmd_same(*pmd, orig_pmd))) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001272 spin_unlock(ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001273 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001274 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001275 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001276 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001277 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001278 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1279 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001280 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001281 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001282 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001283 lru_cache_add_active_or_unevictable(new_page, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001284 set_pmd_at(mm, haddr, pmd, entry);
David Millerb113da62012-10-08 16:34:25 -07001285 update_mmu_cache_pmd(vma, address, pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001286 if (!page) {
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001287 add_mm_counter(mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001288 put_huge_zero_page();
1289 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001290 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001291 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001292 put_page(page);
1293 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001294 ret |= VM_FAULT_WRITE;
1295 }
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001296 spin_unlock(ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001297out_mn:
1298 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1299out:
1300 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001301out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001302 spin_unlock(ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001303 return ret;
1304}
1305
David Rientjesb676b292012-10-08 16:34:03 -07001306struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001307 unsigned long addr,
1308 pmd_t *pmd,
1309 unsigned int flags)
1310{
David Rientjesb676b292012-10-08 16:34:03 -07001311 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001312 struct page *page = NULL;
1313
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001314 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001315
1316 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1317 goto out;
1318
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001319 /* Avoid dumping huge zero page */
1320 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1321 return ERR_PTR(-EFAULT);
1322
Mel Gorman2b4847e2013-12-18 17:08:32 -08001323 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001324 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001325 goto out;
1326
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001327 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001328 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001329 if (flags & FOLL_TOUCH) {
1330 pmd_t _pmd;
1331 /*
1332 * We should set the dirty bit only for FOLL_WRITE but
1333 * for now the dirty bit in the pmd is meaningless.
1334 * And if the dirty bit will become meaningful and
1335 * we'll only set it with FOLL_WRITE, an atomic
1336 * set_bit will be required on the pmd to set the
1337 * young bit, instead of the current set_pmd_at.
1338 */
1339 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
Aneesh Kumar K.V8663890a2013-06-06 00:20:34 -07001340 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1341 pmd, _pmd, 1))
1342 update_mmu_cache_pmd(vma, addr, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001343 }
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001344 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001345 /*
1346 * We don't mlock() pte-mapped THPs. This way we can avoid
1347 * leaking mlocked pages into non-VM_LOCKED VMAs.
1348 *
1349 * In most cases the pmd is the only mapping of the page as we
1350 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1351 * writable private mappings in populate_vma_page_range().
1352 *
1353 * The only scenario when we have the page shared here is if we
1354 * mlocking read-only mapping shared over fork(). We skip
1355 * mlocking such pages.
1356 */
1357 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1358 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001359 lru_add_drain();
1360 if (page->mapping)
1361 mlock_vma_page(page);
1362 unlock_page(page);
1363 }
1364 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001365 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001366 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001367 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001368 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001369
1370out:
1371 return page;
1372}
1373
Mel Gormand10e63f2012-10-25 14:16:31 +02001374/* NUMA hinting page fault entry point for trans huge pmds */
Mel Gorman4daae3b2012-11-02 11:33:45 +00001375int do_huge_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
1376 unsigned long addr, pmd_t pmd, pmd_t *pmdp)
Mel Gormand10e63f2012-10-25 14:16:31 +02001377{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001378 spinlock_t *ptl;
Mel Gormanb8916632013-10-07 11:28:44 +01001379 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001380 struct page *page;
Mel Gormand10e63f2012-10-25 14:16:31 +02001381 unsigned long haddr = addr & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001382 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001383 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001384 bool page_locked;
1385 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001386 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001387 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001388
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001389 /* A PROT_NONE fault should not end up here */
1390 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1391
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001392 ptl = pmd_lock(mm, pmdp);
Mel Gormand10e63f2012-10-25 14:16:31 +02001393 if (unlikely(!pmd_same(pmd, *pmdp)))
1394 goto out_unlock;
1395
Mel Gormande466bd2013-12-18 17:08:42 -08001396 /*
1397 * If there are potential migrations, wait for completion and retry
1398 * without disrupting NUMA hinting information. Do not relock and
1399 * check_same as the page may no longer be mapped.
1400 */
1401 if (unlikely(pmd_trans_migrating(*pmdp))) {
Mel Gorman5d833062015-02-12 14:58:16 -08001402 page = pmd_page(*pmdp);
Mel Gormande466bd2013-12-18 17:08:42 -08001403 spin_unlock(ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001404 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001405 goto out;
1406 }
1407
Mel Gormand10e63f2012-10-25 14:16:31 +02001408 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001409 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001410 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001411 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001412 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001413 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001414 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001415 flags |= TNF_FAULT_LOCAL;
1416 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001417
Mel Gormanbea66fb2015-03-25 15:55:37 -07001418 /* See similar comment in do_numa_page for explanation */
1419 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001420 flags |= TNF_NO_GROUP;
1421
1422 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001423 * Acquire the page lock to serialise THP migrations but avoid dropping
1424 * page_table_lock if at all possible
1425 */
Mel Gormanb8916632013-10-07 11:28:44 +01001426 page_locked = trylock_page(page);
1427 target_nid = mpol_misplaced(page, vma, haddr);
1428 if (target_nid == -1) {
1429 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001430 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001431 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001432 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001433
Mel Gormande466bd2013-12-18 17:08:42 -08001434 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001435 if (!page_locked) {
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001436 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001437 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001438 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001439 goto out;
1440 }
1441
Mel Gorman2b4847e2013-12-18 17:08:32 -08001442 /*
1443 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1444 * to serialises splits
1445 */
Mel Gormanb8916632013-10-07 11:28:44 +01001446 get_page(page);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001447 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001448 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001449
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001450 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001451 spin_lock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001452 if (unlikely(!pmd_same(pmd, *pmdp))) {
1453 unlock_page(page);
1454 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001455 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001456 goto out_unlock;
1457 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001458
Mel Gormanc3a489c2013-12-18 17:08:38 -08001459 /* Bail if we fail to protect against THP splits for any reason */
1460 if (unlikely(!anon_vma)) {
1461 put_page(page);
1462 page_nid = -1;
1463 goto clear_pmdnuma;
1464 }
1465
Mel Gormana54a4072013-10-07 11:28:46 +01001466 /*
1467 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001468 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001469 */
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001470 spin_unlock(ptl);
Mel Gormanb32967f2012-11-19 12:35:47 +00001471 migrated = migrate_misplaced_transhuge_page(mm, vma,
Hugh Dickins340ef392013-02-22 16:34:33 -08001472 pmdp, pmd, addr, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001473 if (migrated) {
1474 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001475 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001476 } else
1477 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001478
Mel Gorman8191acb2013-10-07 11:28:45 +01001479 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001480clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001481 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001482 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001483 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001484 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001485 if (was_writable)
1486 pmd = pmd_mkwrite(pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001487 set_pmd_at(mm, haddr, pmdp, pmd);
Mel Gormand10e63f2012-10-25 14:16:31 +02001488 update_mmu_cache_pmd(vma, addr, pmdp);
Mel Gormana54a4072013-10-07 11:28:46 +01001489 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001490out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001491 spin_unlock(ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001492
1493out:
1494 if (anon_vma)
1495 page_unlock_anon_vma_read(anon_vma);
1496
Mel Gorman8191acb2013-10-07 11:28:45 +01001497 if (page_nid != -1)
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001498 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001499
Mel Gormand10e63f2012-10-25 14:16:31 +02001500 return 0;
1501}
1502
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001503int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001504 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001505{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001506 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001507 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001508
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001509 if (!__pmd_trans_huge_lock(pmd, vma, &ptl))
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001510 return 0;
1511 /*
1512 * For architectures like ppc64 we look at deposited pgtable
1513 * when calling pmdp_huge_get_and_clear. So do the
1514 * pgtable_trans_huge_withdraw after finishing pmdp related
1515 * operations.
1516 */
1517 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1518 tlb->fullmm);
1519 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1520 if (vma_is_dax(vma)) {
1521 spin_unlock(ptl);
1522 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001523 put_huge_zero_page();
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001524 } else if (is_huge_zero_pmd(orig_pmd)) {
1525 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1526 atomic_long_dec(&tlb->mm->nr_ptes);
1527 spin_unlock(ptl);
1528 put_huge_zero_page();
1529 } else {
1530 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001531 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001532 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1533 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1534 VM_BUG_ON_PAGE(!PageHead(page), page);
1535 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1536 atomic_long_dec(&tlb->mm->nr_ptes);
1537 spin_unlock(ptl);
1538 tlb_remove_page(tlb, page);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001539 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001540 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001541}
1542
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001543bool move_huge_pmd(struct vm_area_struct *vma, struct vm_area_struct *new_vma,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001544 unsigned long old_addr,
1545 unsigned long new_addr, unsigned long old_end,
1546 pmd_t *old_pmd, pmd_t *new_pmd)
1547{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001548 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001549 pmd_t pmd;
1550
1551 struct mm_struct *mm = vma->vm_mm;
1552
1553 if ((old_addr & ~HPAGE_PMD_MASK) ||
1554 (new_addr & ~HPAGE_PMD_MASK) ||
1555 old_end - old_addr < HPAGE_PMD_SIZE ||
1556 (new_vma->vm_flags & VM_NOHUGEPAGE))
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001557 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001558
1559 /*
1560 * The destination pmd shouldn't be established, free_pgtables()
1561 * should have release it.
1562 */
1563 if (WARN_ON(!pmd_none(*new_pmd))) {
1564 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001565 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001566 }
1567
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001568 /*
1569 * We don't have to worry about the ordering of src and dst
1570 * ptlocks because exclusive mmap_sem prevents deadlock.
1571 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001572 if (__pmd_trans_huge_lock(old_pmd, vma, &old_ptl)) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001573 new_ptl = pmd_lockptr(mm, new_pmd);
1574 if (new_ptl != old_ptl)
1575 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001576 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001577 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001578
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301579 if (pmd_move_must_withdraw(new_ptl, old_ptl)) {
1580 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001581 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1582 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001583 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301584 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1585 if (new_ptl != old_ptl)
1586 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001587 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001588 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001589 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001590 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001591}
1592
Mel Gormanf123d742013-10-07 11:28:49 +01001593/*
1594 * Returns
1595 * - 0 if PMD could not be locked
1596 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1597 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1598 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001599int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001600 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001601{
1602 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001603 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001604 int ret = 0;
1605
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001606 if (__pmd_trans_huge_lock(pmd, vma, &ptl)) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001607 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001608 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001609 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001610
1611 /*
1612 * Avoid trapping faults against the zero page. The read-only
1613 * data is likely to be read-cached on the local CPU and
1614 * local/remote hits to the zero page are not interesting.
1615 */
1616 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1617 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001618 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001619 }
1620
Mel Gorman10c10452015-02-12 14:58:44 -08001621 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001622 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001623 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001624 if (preserve_write)
1625 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001626 ret = HPAGE_PMD_NR;
1627 set_pmd_at(mm, addr, pmd, entry);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001628 BUG_ON(!preserve_write && pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001629 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001630 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001631 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001632
1633 return ret;
1634}
1635
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001636/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001637 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001638 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001639 * Note that if it returns true, this routine returns without unlocking page
1640 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001641 */
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001642bool __pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma,
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001643 spinlock_t **ptl)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001644{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001645 *ptl = pmd_lock(vma->vm_mm, pmd);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001646 if (likely(pmd_trans_huge(*pmd)))
1647 return true;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001648 spin_unlock(*ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001649 return false;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001650}
1651
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001652/*
1653 * This function returns whether a given @page is mapped onto the @address
1654 * in the virtual space of @mm.
1655 *
1656 * When it's true, this function returns *pmd with holding the page table lock
1657 * and passing it back to the caller via @ptl.
1658 * If it's false, returns NULL without holding the page table lock.
1659 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001660pmd_t *page_check_address_pmd(struct page *page,
1661 struct mm_struct *mm,
1662 unsigned long address,
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001663 spinlock_t **ptl)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664{
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001665 pgd_t *pgd;
1666 pud_t *pud;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001667 pmd_t *pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001668
1669 if (address & ~HPAGE_PMD_MASK)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001670 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001671
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001672 pgd = pgd_offset(mm, address);
1673 if (!pgd_present(*pgd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001674 return NULL;
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001675 pud = pud_offset(pgd, address);
1676 if (!pud_present(*pud))
1677 return NULL;
1678 pmd = pmd_offset(pud, address);
1679
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001680 *ptl = pmd_lock(mm, pmd);
Kirill A. Shutemovb5a8cad2014-04-18 15:07:25 -07001681 if (!pmd_present(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001682 goto unlock;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001683 if (pmd_page(*pmd) != page)
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001684 goto unlock;
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001685 if (pmd_trans_huge(*pmd))
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001686 return pmd;
Kirill A. Shutemov117b0792013-11-14 14:30:56 -08001687unlock:
1688 spin_unlock(*ptl);
1689 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001690}
1691
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001692#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001693
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001694int hugepage_madvise(struct vm_area_struct *vma,
1695 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001696{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001697 switch (advice) {
1698 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001699#ifdef CONFIG_S390
1700 /*
1701 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1702 * can't handle this properly after s390_enable_sie, so we simply
1703 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1704 */
1705 if (mm_has_pgste(vma->vm_mm))
1706 return 0;
1707#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001708 /*
1709 * Be somewhat over-protective like KSM for now!
1710 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001711 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001712 return -EINVAL;
1713 *vm_flags &= ~VM_NOHUGEPAGE;
1714 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001715 /*
1716 * If the vma become good for khugepaged to scan,
1717 * register it here without waiting a page fault that
1718 * may not happen any time soon.
1719 */
David Rientjes6d50e602014-10-29 14:50:31 -07001720 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001721 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001722 break;
1723 case MADV_NOHUGEPAGE:
1724 /*
1725 * Be somewhat over-protective like KSM for now!
1726 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001727 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001728 return -EINVAL;
1729 *vm_flags &= ~VM_HUGEPAGE;
1730 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001731 /*
1732 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1733 * this vma even if we leave the mm registered in khugepaged if
1734 * it got registered before VM_NOHUGEPAGE was set.
1735 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001736 break;
1737 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001738
1739 return 0;
1740}
1741
Andrea Arcangeliba761492011-01-13 15:46:58 -08001742static int __init khugepaged_slab_init(void)
1743{
1744 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1745 sizeof(struct mm_slot),
1746 __alignof__(struct mm_slot), 0, NULL);
1747 if (!mm_slot_cache)
1748 return -ENOMEM;
1749
1750 return 0;
1751}
1752
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001753static void __init khugepaged_slab_exit(void)
1754{
1755 kmem_cache_destroy(mm_slot_cache);
1756}
1757
Andrea Arcangeliba761492011-01-13 15:46:58 -08001758static inline struct mm_slot *alloc_mm_slot(void)
1759{
1760 if (!mm_slot_cache) /* initialization failed */
1761 return NULL;
1762 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1763}
1764
1765static inline void free_mm_slot(struct mm_slot *mm_slot)
1766{
1767 kmem_cache_free(mm_slot_cache, mm_slot);
1768}
1769
Andrea Arcangeliba761492011-01-13 15:46:58 -08001770static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1771{
1772 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001773
Sasha Levinb67bfe02013-02-27 17:06:00 -08001774 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001775 if (mm == mm_slot->mm)
1776 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001777
Andrea Arcangeliba761492011-01-13 15:46:58 -08001778 return NULL;
1779}
1780
1781static void insert_to_mm_slots_hash(struct mm_struct *mm,
1782 struct mm_slot *mm_slot)
1783{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001784 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001785 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001786}
1787
1788static inline int khugepaged_test_exit(struct mm_struct *mm)
1789{
1790 return atomic_read(&mm->mm_users) == 0;
1791}
1792
1793int __khugepaged_enter(struct mm_struct *mm)
1794{
1795 struct mm_slot *mm_slot;
1796 int wakeup;
1797
1798 mm_slot = alloc_mm_slot();
1799 if (!mm_slot)
1800 return -ENOMEM;
1801
1802 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001803 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001804 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1805 free_mm_slot(mm_slot);
1806 return 0;
1807 }
1808
1809 spin_lock(&khugepaged_mm_lock);
1810 insert_to_mm_slots_hash(mm, mm_slot);
1811 /*
1812 * Insert just behind the scanning cursor, to let the area settle
1813 * down a little.
1814 */
1815 wakeup = list_empty(&khugepaged_scan.mm_head);
1816 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1817 spin_unlock(&khugepaged_mm_lock);
1818
1819 atomic_inc(&mm->mm_count);
1820 if (wakeup)
1821 wake_up_interruptible(&khugepaged_wait);
1822
1823 return 0;
1824}
1825
David Rientjes6d50e602014-10-29 14:50:31 -07001826int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1827 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001828{
1829 unsigned long hstart, hend;
1830 if (!vma->anon_vma)
1831 /*
1832 * Not yet faulted in so we will register later in the
1833 * page fault if needed.
1834 */
1835 return 0;
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001836 if (vma->vm_ops)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001837 /* khugepaged not yet working on file or special mappings */
1838 return 0;
David Rientjes6d50e602014-10-29 14:50:31 -07001839 VM_BUG_ON_VMA(vm_flags & VM_NO_THP, vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001840 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1841 hend = vma->vm_end & HPAGE_PMD_MASK;
1842 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001843 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001844 return 0;
1845}
1846
1847void __khugepaged_exit(struct mm_struct *mm)
1848{
1849 struct mm_slot *mm_slot;
1850 int free = 0;
1851
1852 spin_lock(&khugepaged_mm_lock);
1853 mm_slot = get_mm_slot(mm);
1854 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001855 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001856 list_del(&mm_slot->mm_node);
1857 free = 1;
1858 }
Chris Wrightd788e802011-07-25 17:12:14 -07001859 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001860
1861 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001862 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1863 free_mm_slot(mm_slot);
1864 mmdrop(mm);
1865 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001866 /*
1867 * This is required to serialize against
1868 * khugepaged_test_exit() (which is guaranteed to run
1869 * under mmap sem read mode). Stop here (after we
1870 * return all pagetables will be destroyed) until
1871 * khugepaged has finished working on the pagetables
1872 * under the mmap_sem.
1873 */
1874 down_write(&mm->mmap_sem);
1875 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001876 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001877}
1878
1879static void release_pte_page(struct page *page)
1880{
1881 /* 0 stands for page_is_file_cache(page) == false */
1882 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
1883 unlock_page(page);
1884 putback_lru_page(page);
1885}
1886
1887static void release_pte_pages(pte_t *pte, pte_t *_pte)
1888{
1889 while (--_pte >= pte) {
1890 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07001891 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001892 release_pte_page(pte_page(pteval));
1893 }
1894}
1895
Andrea Arcangeliba761492011-01-13 15:46:58 -08001896static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
1897 unsigned long address,
1898 pte_t *pte)
1899{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001900 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001901 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001902 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001903 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001904
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
1906 _pte++, address += PAGE_SIZE) {
1907 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07001908 if (pte_none(pteval) || (pte_present(pteval) &&
1909 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07001910 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001911 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001912 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001913 } else {
1914 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001915 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001916 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001917 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001918 if (!pte_present(pteval)) {
1919 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001920 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001921 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08001922 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001923 if (unlikely(!page)) {
1924 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001925 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001926 }
Bob Liu344aa352012-12-11 16:00:34 -08001927
Sasha Levin309381fea2014-01-23 15:52:54 -08001928 VM_BUG_ON_PAGE(PageCompound(page), page);
1929 VM_BUG_ON_PAGE(!PageAnon(page), page);
1930 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001931
Andrea Arcangeliba761492011-01-13 15:46:58 -08001932 /*
1933 * We can do it before isolate_lru_page because the
1934 * page can't be freed from under us. NOTE: PG_lock
1935 * is needed to serialize against split_huge_page
1936 * when invoked from the VM.
1937 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001938 if (!trylock_page(page)) {
1939 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001940 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001941 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08001942
1943 /*
1944 * cannot use mapcount: can't collapse if there's a gup pin.
1945 * The page must only be referenced by the scanned process
1946 * and page swap cache.
1947 */
1948 if (page_count(page) != 1 + !!PageSwapCache(page)) {
1949 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001950 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001951 goto out;
1952 }
1953 if (pte_write(pteval)) {
1954 writable = true;
1955 } else {
1956 if (PageSwapCache(page) && !reuse_swap_page(page)) {
1957 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001958 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08001959 goto out;
1960 }
1961 /*
1962 * Page is not in the swap cache. It can be collapsed
1963 * into a THP.
1964 */
1965 }
1966
Andrea Arcangeliba761492011-01-13 15:46:58 -08001967 /*
1968 * Isolate the page to avoid collapsing an hugepage
1969 * currently in use by the VM.
1970 */
1971 if (isolate_lru_page(page)) {
1972 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001973 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001974 goto out;
1975 }
1976 /* 0 stands for page_is_file_cache(page) == false */
1977 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08001978 VM_BUG_ON_PAGE(!PageLocked(page), page);
1979 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001980
1981 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07001982 if (pte_young(pteval) ||
1983 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08001984 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08001985 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001986 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08001987 if (likely(writable)) {
1988 if (likely(referenced)) {
1989 result = SCAN_SUCCEED;
1990 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
1991 referenced, writable, result);
1992 return 1;
1993 }
1994 } else {
1995 result = SCAN_PAGE_RO;
1996 }
1997
Andrea Arcangeliba761492011-01-13 15:46:58 -08001998out:
Bob Liu344aa352012-12-11 16:00:34 -08001999 release_pte_pages(pte, _pte);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002000 trace_mm_collapse_huge_page_isolate(page_to_pfn(page), none_or_zero,
2001 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002002 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002003}
2004
2005static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2006 struct vm_area_struct *vma,
2007 unsigned long address,
2008 spinlock_t *ptl)
2009{
2010 pte_t *_pte;
2011 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2012 pte_t pteval = *_pte;
2013 struct page *src_page;
2014
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002015 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002016 clear_user_highpage(page, address);
2017 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002018 if (is_zero_pfn(pte_pfn(pteval))) {
2019 /*
2020 * ptl mostly unnecessary.
2021 */
2022 spin_lock(ptl);
2023 /*
2024 * paravirt calls inside pte_clear here are
2025 * superfluous.
2026 */
2027 pte_clear(vma->vm_mm, address, _pte);
2028 spin_unlock(ptl);
2029 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002030 } else {
2031 src_page = pte_page(pteval);
2032 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002033 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002034 release_pte_page(src_page);
2035 /*
2036 * ptl mostly unnecessary, but preempt has to
2037 * be disabled to update the per-cpu stats
2038 * inside page_remove_rmap().
2039 */
2040 spin_lock(ptl);
2041 /*
2042 * paravirt calls inside pte_clear here are
2043 * superfluous.
2044 */
2045 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002046 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002047 spin_unlock(ptl);
2048 free_page_and_swap_cache(src_page);
2049 }
2050
2051 address += PAGE_SIZE;
2052 page++;
2053 }
2054}
2055
Xiao Guangrong26234f32012-10-08 16:29:51 -07002056static void khugepaged_alloc_sleep(void)
2057{
Petr Mladekbde43c62015-09-08 15:04:05 -07002058 DEFINE_WAIT(wait);
2059
2060 add_wait_queue(&khugepaged_wait, &wait);
2061 freezable_schedule_timeout_interruptible(
2062 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2063 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002064}
2065
Bob Liu9f1b8682013-11-12 15:07:37 -08002066static int khugepaged_node_load[MAX_NUMNODES];
2067
David Rientjes14a4e212014-08-06 16:07:29 -07002068static bool khugepaged_scan_abort(int nid)
2069{
2070 int i;
2071
2072 /*
2073 * If zone_reclaim_mode is disabled, then no extra effort is made to
2074 * allocate memory locally.
2075 */
2076 if (!zone_reclaim_mode)
2077 return false;
2078
2079 /* If there is a count for this node already, it must be acceptable */
2080 if (khugepaged_node_load[nid])
2081 return false;
2082
2083 for (i = 0; i < MAX_NUMNODES; i++) {
2084 if (!khugepaged_node_load[i])
2085 continue;
2086 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2087 return true;
2088 }
2089 return false;
2090}
2091
Xiao Guangrong26234f32012-10-08 16:29:51 -07002092#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002093static int khugepaged_find_target_node(void)
2094{
2095 static int last_khugepaged_target_node = NUMA_NO_NODE;
2096 int nid, target_node = 0, max_value = 0;
2097
2098 /* find first node with max normal pages hit */
2099 for (nid = 0; nid < MAX_NUMNODES; nid++)
2100 if (khugepaged_node_load[nid] > max_value) {
2101 max_value = khugepaged_node_load[nid];
2102 target_node = nid;
2103 }
2104
2105 /* do some balance if several nodes have the same hit record */
2106 if (target_node <= last_khugepaged_target_node)
2107 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2108 nid++)
2109 if (max_value == khugepaged_node_load[nid]) {
2110 target_node = nid;
2111 break;
2112 }
2113
2114 last_khugepaged_target_node = target_node;
2115 return target_node;
2116}
2117
Xiao Guangrong26234f32012-10-08 16:29:51 -07002118static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2119{
2120 if (IS_ERR(*hpage)) {
2121 if (!*wait)
2122 return false;
2123
2124 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002125 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002126 khugepaged_alloc_sleep();
2127 } else if (*hpage) {
2128 put_page(*hpage);
2129 *hpage = NULL;
2130 }
2131
2132 return true;
2133}
2134
Michal Hocko3b363692015-04-15 16:13:29 -07002135static struct page *
2136khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002137 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002138{
Sasha Levin309381fea2014-01-23 15:52:54 -08002139 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002140
Xiao Guangrong26234f32012-10-08 16:29:51 -07002141 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002142 * Before allocating the hugepage, release the mmap_sem read lock.
2143 * The allocation can take potentially a long time if it involves
2144 * sync compaction, and we do not need to hold the mmap_sem during
2145 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002146 */
2147 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002148
Vlastimil Babka96db8002015-09-08 15:03:50 -07002149 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002150 if (unlikely(!*hpage)) {
2151 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2152 *hpage = ERR_PTR(-ENOMEM);
2153 return NULL;
2154 }
2155
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002156 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002157 count_vm_event(THP_COLLAPSE_ALLOC);
2158 return *hpage;
2159}
2160#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002161static int khugepaged_find_target_node(void)
2162{
2163 return 0;
2164}
2165
Bob Liu10dc4152013-11-12 15:07:35 -08002166static inline struct page *alloc_hugepage(int defrag)
2167{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002168 struct page *page;
2169
2170 page = alloc_pages(alloc_hugepage_gfpmask(defrag, 0), HPAGE_PMD_ORDER);
2171 if (page)
2172 prep_transhuge_page(page);
2173 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002174}
2175
Xiao Guangrong26234f32012-10-08 16:29:51 -07002176static struct page *khugepaged_alloc_hugepage(bool *wait)
2177{
2178 struct page *hpage;
2179
2180 do {
2181 hpage = alloc_hugepage(khugepaged_defrag());
2182 if (!hpage) {
2183 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2184 if (!*wait)
2185 return NULL;
2186
2187 *wait = false;
2188 khugepaged_alloc_sleep();
2189 } else
2190 count_vm_event(THP_COLLAPSE_ALLOC);
2191 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2192
2193 return hpage;
2194}
2195
2196static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2197{
2198 if (!*hpage)
2199 *hpage = khugepaged_alloc_hugepage(wait);
2200
2201 if (unlikely(!*hpage))
2202 return false;
2203
2204 return true;
2205}
2206
Michal Hocko3b363692015-04-15 16:13:29 -07002207static struct page *
2208khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002209 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002210{
2211 up_read(&mm->mmap_sem);
2212 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002213
Xiao Guangrong26234f32012-10-08 16:29:51 -07002214 return *hpage;
2215}
2216#endif
2217
Bob Liufa475e52012-12-11 16:00:39 -08002218static bool hugepage_vma_check(struct vm_area_struct *vma)
2219{
2220 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2221 (vma->vm_flags & VM_NOHUGEPAGE))
2222 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002223 if (!vma->anon_vma || vma->vm_ops)
2224 return false;
2225 if (is_vma_temporary_stack(vma))
2226 return false;
Sasha Levin81d1b092014-10-09 15:28:10 -07002227 VM_BUG_ON_VMA(vma->vm_flags & VM_NO_THP, vma);
Bob Liufa475e52012-12-11 16:00:39 -08002228 return true;
2229}
2230
Andrea Arcangeliba761492011-01-13 15:46:58 -08002231static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002232 unsigned long address,
2233 struct page **hpage,
2234 struct vm_area_struct *vma,
2235 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002236{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002237 pmd_t *pmd, _pmd;
2238 pte_t *pte;
2239 pgtable_t pgtable;
2240 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002241 spinlock_t *pmd_ptl, *pte_ptl;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002242 int isolated, result = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002243 unsigned long hstart, hend;
Johannes Weiner00501b52014-08-08 14:19:20 -07002244 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002245 unsigned long mmun_start; /* For mmu_notifiers */
2246 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002247 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002248
2249 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002250
Michal Hocko3b363692015-04-15 16:13:29 -07002251 /* Only allocate from the target node */
2252 gfp = alloc_hugepage_gfpmask(khugepaged_defrag(), __GFP_OTHER_NODE) |
2253 __GFP_THISNODE;
2254
Xiao Guangrong26234f32012-10-08 16:29:51 -07002255 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002256 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002257 if (!new_page) {
2258 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2259 goto out_nolock;
2260 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002261
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002262 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002263 result = SCAN_CGROUP_CHARGE_FAIL;
2264 goto out_nolock;
2265 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002266
2267 /*
2268 * Prevent all access to pagetables with the exception of
2269 * gup_fast later hanlded by the ptep_clear_flush and the VM
2270 * handled by the anon_vma lock + PG_lock.
2271 */
2272 down_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002273 if (unlikely(khugepaged_test_exit(mm))) {
2274 result = SCAN_ANY_PROCESS;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002275 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002276 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002277
2278 vma = find_vma(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002279 if (!vma) {
2280 result = SCAN_VMA_NULL;
Libina8f531eb2013-09-11 14:20:38 -07002281 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002282 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002283 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2284 hend = vma->vm_end & HPAGE_PMD_MASK;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002285 if (address < hstart || address + HPAGE_PMD_SIZE > hend) {
2286 result = SCAN_ADDRESS_RANGE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002287 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002288 }
2289 if (!hugepage_vma_check(vma)) {
2290 result = SCAN_VMA_CHECK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002291 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002292 }
Bob Liu62190492012-12-11 16:00:37 -08002293 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002294 if (!pmd) {
2295 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002296 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002297 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002298
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002299 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002300
2301 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002302 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002303
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002304 mmun_start = address;
2305 mmun_end = address + HPAGE_PMD_SIZE;
2306 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002307 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002308 /*
2309 * After this gup_fast can't run anymore. This also removes
2310 * any huge TLB entry from the CPU so we won't allow
2311 * huge and small TLB entries for the same virtual address
2312 * to avoid the risk of CPU bugs in that area.
2313 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002314 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002315 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002316 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002317
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002318 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002319 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002320 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002321
2322 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002323 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002324 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002325 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002326 /*
2327 * We can only use set_pmd_at when establishing
2328 * hugepmds and never for establishing regular pmds that
2329 * points to regular pagetables. Use pmd_populate for that
2330 */
2331 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002332 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002333 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002334 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002335 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002336 }
2337
2338 /*
2339 * All pages are isolated and locked so anon_vma rmap
2340 * can't run anymore.
2341 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002342 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002343
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002344 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002345 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002346 __SetPageUptodate(new_page);
2347 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002348
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002349 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2350 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002351
2352 /*
2353 * spin_lock() below is not the equivalent of smp_wmb(), so
2354 * this is needed to avoid the copy_huge_page writes to become
2355 * visible after the set_pmd_at() write.
2356 */
2357 smp_wmb();
2358
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002359 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002360 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002361 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002362 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002363 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002364 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002365 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002366 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002367 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002368
2369 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002370
Andrea Arcangeliba761492011-01-13 15:46:58 -08002371 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002372 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002373out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002374 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002375 trace_mm_collapse_huge_page(mm, isolated, result);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002376 return;
2377
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002378out_nolock:
2379 trace_mm_collapse_huge_page(mm, isolated, result);
2380 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002381out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002382 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002383 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002384}
2385
2386static int khugepaged_scan_pmd(struct mm_struct *mm,
2387 struct vm_area_struct *vma,
2388 unsigned long address,
2389 struct page **hpage)
2390{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002391 pmd_t *pmd;
2392 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002393 int ret = 0, none_or_zero = 0, result = 0;
2394 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002395 unsigned long _address;
2396 spinlock_t *ptl;
David Rientjes00ef2d22013-02-22 16:35:36 -08002397 int node = NUMA_NO_NODE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002398 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002399
2400 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2401
Bob Liu62190492012-12-11 16:00:37 -08002402 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002403 if (!pmd) {
2404 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002405 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002406 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002407
Bob Liu9f1b8682013-11-12 15:07:37 -08002408 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002409 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2410 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2411 _pte++, _address += PAGE_SIZE) {
2412 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002413 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002414 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002415 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002416 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002417 } else {
2418 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002419 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002420 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002421 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002422 if (!pte_present(pteval)) {
2423 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002424 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002425 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002426 if (pte_write(pteval))
2427 writable = true;
2428
Andrea Arcangeliba761492011-01-13 15:46:58 -08002429 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002430 if (unlikely(!page)) {
2431 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002432 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002433 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002434
2435 /* TODO: teach khugepaged to collapse THP mapped with pte */
2436 if (PageCompound(page)) {
2437 result = SCAN_PAGE_COMPOUND;
2438 goto out_unmap;
2439 }
2440
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002441 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002442 * Record which node the original page is from and save this
2443 * information to khugepaged_node_load[].
2444 * Khupaged will allocate hugepage from the node has the max
2445 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002446 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002447 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002448 if (khugepaged_scan_abort(node)) {
2449 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002450 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002451 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002452 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002453 if (!PageLRU(page)) {
2454 result = SCAN_SCAN_ABORT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002455 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002456 }
2457 if (PageLocked(page)) {
2458 result = SCAN_PAGE_LOCK;
2459 goto out_unmap;
2460 }
2461 if (!PageAnon(page)) {
2462 result = SCAN_PAGE_ANON;
2463 goto out_unmap;
2464 }
2465
Ebru Akagunduz10359212015-02-11 15:28:28 -08002466 /*
2467 * cannot use mapcount: can't collapse if there's a gup pin.
2468 * The page must only be referenced by the scanned process
2469 * and page swap cache.
2470 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002471 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2472 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002473 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002474 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002475 if (pte_young(pteval) ||
2476 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002477 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002478 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002479 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002480 if (writable) {
2481 if (referenced) {
2482 result = SCAN_SUCCEED;
2483 ret = 1;
2484 } else {
2485 result = SCAN_NO_REFERENCED_PAGE;
2486 }
2487 } else {
2488 result = SCAN_PAGE_RO;
2489 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002490out_unmap:
2491 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002492 if (ret) {
2493 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002494 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002495 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002496 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002497out:
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002498 trace_mm_khugepaged_scan_pmd(mm, page_to_pfn(page), writable, referenced,
2499 none_or_zero, result);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002500 return ret;
2501}
2502
2503static void collect_mm_slot(struct mm_slot *mm_slot)
2504{
2505 struct mm_struct *mm = mm_slot->mm;
2506
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002507 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002508
2509 if (khugepaged_test_exit(mm)) {
2510 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002511 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002512 list_del(&mm_slot->mm_node);
2513
2514 /*
2515 * Not strictly needed because the mm exited already.
2516 *
2517 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2518 */
2519
2520 /* khugepaged_mm_lock actually not necessary for the below */
2521 free_mm_slot(mm_slot);
2522 mmdrop(mm);
2523 }
2524}
2525
2526static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2527 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002528 __releases(&khugepaged_mm_lock)
2529 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002530{
2531 struct mm_slot *mm_slot;
2532 struct mm_struct *mm;
2533 struct vm_area_struct *vma;
2534 int progress = 0;
2535
2536 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002537 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002538
2539 if (khugepaged_scan.mm_slot)
2540 mm_slot = khugepaged_scan.mm_slot;
2541 else {
2542 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2543 struct mm_slot, mm_node);
2544 khugepaged_scan.address = 0;
2545 khugepaged_scan.mm_slot = mm_slot;
2546 }
2547 spin_unlock(&khugepaged_mm_lock);
2548
2549 mm = mm_slot->mm;
2550 down_read(&mm->mmap_sem);
2551 if (unlikely(khugepaged_test_exit(mm)))
2552 vma = NULL;
2553 else
2554 vma = find_vma(mm, khugepaged_scan.address);
2555
2556 progress++;
2557 for (; vma; vma = vma->vm_next) {
2558 unsigned long hstart, hend;
2559
2560 cond_resched();
2561 if (unlikely(khugepaged_test_exit(mm))) {
2562 progress++;
2563 break;
2564 }
Bob Liufa475e52012-12-11 16:00:39 -08002565 if (!hugepage_vma_check(vma)) {
2566skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002567 progress++;
2568 continue;
2569 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002570 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2571 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002572 if (hstart >= hend)
2573 goto skip;
2574 if (khugepaged_scan.address > hend)
2575 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576 if (khugepaged_scan.address < hstart)
2577 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002578 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579
2580 while (khugepaged_scan.address < hend) {
2581 int ret;
2582 cond_resched();
2583 if (unlikely(khugepaged_test_exit(mm)))
2584 goto breakouterloop;
2585
2586 VM_BUG_ON(khugepaged_scan.address < hstart ||
2587 khugepaged_scan.address + HPAGE_PMD_SIZE >
2588 hend);
2589 ret = khugepaged_scan_pmd(mm, vma,
2590 khugepaged_scan.address,
2591 hpage);
2592 /* move to next address */
2593 khugepaged_scan.address += HPAGE_PMD_SIZE;
2594 progress += HPAGE_PMD_NR;
2595 if (ret)
2596 /* we released mmap_sem so break loop */
2597 goto breakouterloop_mmap_sem;
2598 if (progress >= pages)
2599 goto breakouterloop;
2600 }
2601 }
2602breakouterloop:
2603 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2604breakouterloop_mmap_sem:
2605
2606 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002607 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002608 /*
2609 * Release the current mm_slot if this mm is about to die, or
2610 * if we scanned all vmas of this mm.
2611 */
2612 if (khugepaged_test_exit(mm) || !vma) {
2613 /*
2614 * Make sure that if mm_users is reaching zero while
2615 * khugepaged runs here, khugepaged_exit will find
2616 * mm_slot not pointing to the exiting mm.
2617 */
2618 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2619 khugepaged_scan.mm_slot = list_entry(
2620 mm_slot->mm_node.next,
2621 struct mm_slot, mm_node);
2622 khugepaged_scan.address = 0;
2623 } else {
2624 khugepaged_scan.mm_slot = NULL;
2625 khugepaged_full_scans++;
2626 }
2627
2628 collect_mm_slot(mm_slot);
2629 }
2630
2631 return progress;
2632}
2633
2634static int khugepaged_has_work(void)
2635{
2636 return !list_empty(&khugepaged_scan.mm_head) &&
2637 khugepaged_enabled();
2638}
2639
2640static int khugepaged_wait_event(void)
2641{
2642 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002643 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002644}
2645
Xiao Guangrongd5169042012-10-08 16:29:48 -07002646static void khugepaged_do_scan(void)
2647{
2648 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002649 unsigned int progress = 0, pass_through_head = 0;
2650 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002651 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002652
2653 barrier(); /* write khugepaged_pages_to_scan to local stack */
2654
2655 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002656 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002657 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002658
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002659 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002660
Jiri Kosinacd092412015-06-24 16:56:07 -07002661 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002662 break;
2663
Andrea Arcangeliba761492011-01-13 15:46:58 -08002664 spin_lock(&khugepaged_mm_lock);
2665 if (!khugepaged_scan.mm_slot)
2666 pass_through_head++;
2667 if (khugepaged_has_work() &&
2668 pass_through_head < 2)
2669 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002670 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002671 else
2672 progress = pages;
2673 spin_unlock(&khugepaged_mm_lock);
2674 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002675
Xiao Guangrongd5169042012-10-08 16:29:48 -07002676 if (!IS_ERR_OR_NULL(hpage))
2677 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002678}
2679
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002680static void khugepaged_wait_work(void)
2681{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002682 if (khugepaged_has_work()) {
2683 if (!khugepaged_scan_sleep_millisecs)
2684 return;
2685
2686 wait_event_freezable_timeout(khugepaged_wait,
2687 kthread_should_stop(),
2688 msecs_to_jiffies(khugepaged_scan_sleep_millisecs));
2689 return;
2690 }
2691
2692 if (khugepaged_enabled())
2693 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2694}
2695
Andrea Arcangeliba761492011-01-13 15:46:58 -08002696static int khugepaged(void *none)
2697{
2698 struct mm_slot *mm_slot;
2699
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002700 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002701 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002702
Xiao Guangrongb7231782012-10-08 16:29:54 -07002703 while (!kthread_should_stop()) {
2704 khugepaged_do_scan();
2705 khugepaged_wait_work();
2706 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002707
2708 spin_lock(&khugepaged_mm_lock);
2709 mm_slot = khugepaged_scan.mm_slot;
2710 khugepaged_scan.mm_slot = NULL;
2711 if (mm_slot)
2712 collect_mm_slot(mm_slot);
2713 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002714 return 0;
2715}
2716
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002717static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2718 unsigned long haddr, pmd_t *pmd)
2719{
2720 struct mm_struct *mm = vma->vm_mm;
2721 pgtable_t pgtable;
2722 pmd_t _pmd;
2723 int i;
2724
2725 /* leave pmd empty until pte is filled */
2726 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2727
2728 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2729 pmd_populate(mm, &_pmd, pgtable);
2730
2731 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2732 pte_t *pte, entry;
2733 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2734 entry = pte_mkspecial(entry);
2735 pte = pte_offset_map(&_pmd, haddr);
2736 VM_BUG_ON(!pte_none(*pte));
2737 set_pte_at(mm, haddr, pte, entry);
2738 pte_unmap(pte);
2739 }
2740 smp_wmb(); /* make pte visible before pmd */
2741 pmd_populate(mm, pmd, pgtable);
2742 put_huge_zero_page();
2743}
2744
2745static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002746 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002747{
2748 struct mm_struct *mm = vma->vm_mm;
2749 struct page *page;
2750 pgtable_t pgtable;
2751 pmd_t _pmd;
2752 bool young, write;
2753 int i;
2754
2755 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2756 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2757 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
2758 VM_BUG_ON(!pmd_trans_huge(*pmd));
2759
2760 count_vm_event(THP_SPLIT_PMD);
2761
2762 if (vma_is_dax(vma)) {
2763 pmd_t _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2764 if (is_huge_zero_pmd(_pmd))
2765 put_huge_zero_page();
2766 return;
2767 } else if (is_huge_zero_pmd(*pmd)) {
2768 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2769 }
2770
2771 page = pmd_page(*pmd);
2772 VM_BUG_ON_PAGE(!page_count(page), page);
2773 atomic_add(HPAGE_PMD_NR - 1, &page->_count);
2774 write = pmd_write(*pmd);
2775 young = pmd_young(*pmd);
2776
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002777 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2778 pmd_populate(mm, &_pmd, pgtable);
2779
2780 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2781 pte_t entry, *pte;
2782 /*
2783 * Note that NUMA hinting access restrictions are not
2784 * transferred to avoid any possibility of altering
2785 * permissions across VMAs.
2786 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002787 if (freeze) {
2788 swp_entry_t swp_entry;
2789 swp_entry = make_migration_entry(page + i, write);
2790 entry = swp_entry_to_pte(swp_entry);
2791 } else {
2792 entry = mk_pte(page + i, vma->vm_page_prot);
2793 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
2794 if (!write)
2795 entry = pte_wrprotect(entry);
2796 if (!young)
2797 entry = pte_mkold(entry);
2798 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002799 pte = pte_offset_map(&_pmd, haddr);
2800 BUG_ON(!pte_none(*pte));
2801 set_pte_at(mm, haddr, pte, entry);
2802 atomic_inc(&page[i]._mapcount);
2803 pte_unmap(pte);
2804 }
2805
2806 /*
2807 * Set PG_double_map before dropping compound_mapcount to avoid
2808 * false-negative page_mapped().
2809 */
2810 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
2811 for (i = 0; i < HPAGE_PMD_NR; i++)
2812 atomic_inc(&page[i]._mapcount);
2813 }
2814
2815 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2816 /* Last compound_mapcount is gone. */
2817 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
2818 if (TestClearPageDoubleMap(page)) {
2819 /* No need in mapcount reference anymore */
2820 for (i = 0; i < HPAGE_PMD_NR; i++)
2821 atomic_dec(&page[i]._mapcount);
2822 }
2823 }
2824
2825 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002826 /*
2827 * Up to this point the pmd is present and huge and userland has the
2828 * whole access to the hugepage during the split (which happens in
2829 * place). If we overwrite the pmd with the not-huge version pointing
2830 * to the pte here (which of course we could if all CPUs were bug
2831 * free), userland could trigger a small page size TLB miss on the
2832 * small sized TLB while the hugepage TLB entry is still established in
2833 * the huge TLB. Some CPU doesn't like that.
2834 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
2835 * 383 on page 93. Intel should be safe but is also warns that it's
2836 * only safe if the permission and cache attributes of the two entries
2837 * loaded in the two TLB is identical (which should be the case here).
2838 * But it is generally safer to never allow small and huge TLB entries
2839 * for the same virtual address to be loaded simultaneously. So instead
2840 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2841 * current pmd notpresent (atomically because here the pmd_trans_huge
2842 * and pmd_trans_splitting must remain set at all times on the pmd
2843 * until the split is complete for this pmd), then we flush the SMP TLB
2844 * and finally we write the non-huge version of the pmd entry with
2845 * pmd_populate.
2846 */
2847 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002848 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002849
2850 if (freeze) {
2851 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2852 page_remove_rmap(page + i, false);
2853 put_page(page + i);
2854 }
2855 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002856}
2857
2858void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
2859 unsigned long address)
2860{
2861 spinlock_t *ptl;
2862 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002863 struct page *page = NULL;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002864 unsigned long haddr = address & HPAGE_PMD_MASK;
2865
2866 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
2867 ptl = pmd_lock(mm, pmd);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002868 if (unlikely(!pmd_trans_huge(*pmd)))
2869 goto out;
2870 page = pmd_page(*pmd);
2871 __split_huge_pmd_locked(vma, pmd, haddr, false);
2872 if (PageMlocked(page))
2873 get_page(page);
2874 else
2875 page = NULL;
2876out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002877 spin_unlock(ptl);
2878 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002879 if (page) {
2880 lock_page(page);
2881 munlock_vma_page(page);
2882 unlock_page(page);
2883 put_page(page);
2884 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002885}
2886
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002887static void split_huge_pmd_address(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002888 unsigned long address)
2889{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002890 pgd_t *pgd;
2891 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002892 pmd_t *pmd;
2893
2894 VM_BUG_ON(!(address & ~HPAGE_PMD_MASK));
2895
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002896 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002897 if (!pgd_present(*pgd))
2898 return;
2899
2900 pud = pud_offset(pgd, address);
2901 if (!pud_present(*pud))
2902 return;
2903
2904 pmd = pmd_offset(pud, address);
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002905 if (!pmd_present(*pmd) || !pmd_trans_huge(*pmd))
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002906 return;
2907 /*
2908 * Caller holds the mmap_sem write mode, so a huge pmd cannot
2909 * materialize from under us.
2910 */
Kirill A. Shutemovad0bed22016-01-15 16:52:53 -08002911 split_huge_pmd(vma, pmd, address);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002912}
2913
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002914void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002915 unsigned long start,
2916 unsigned long end,
2917 long adjust_next)
2918{
2919 /*
2920 * If the new start address isn't hpage aligned and it could
2921 * previously contain an hugepage: check if we need to split
2922 * an huge pmd.
2923 */
2924 if (start & ~HPAGE_PMD_MASK &&
2925 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
2926 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002927 split_huge_pmd_address(vma, start);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002928
2929 /*
2930 * If the new end address isn't hpage aligned and it could
2931 * previously contain an hugepage: check if we need to split
2932 * an huge pmd.
2933 */
2934 if (end & ~HPAGE_PMD_MASK &&
2935 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
2936 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002937 split_huge_pmd_address(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002938
2939 /*
2940 * If we're also updating the vma->vm_next->vm_start, if the new
2941 * vm_next->vm_start isn't page aligned and it could previously
2942 * contain an hugepage: check if we need to split an huge pmd.
2943 */
2944 if (adjust_next > 0) {
2945 struct vm_area_struct *next = vma->vm_next;
2946 unsigned long nstart = next->vm_start;
2947 nstart += adjust_next << PAGE_SHIFT;
2948 if (nstart & ~HPAGE_PMD_MASK &&
2949 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
2950 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002951 split_huge_pmd_address(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002952 }
2953}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002954
2955static void freeze_page_vma(struct vm_area_struct *vma, struct page *page,
2956 unsigned long address)
2957{
2958 spinlock_t *ptl;
2959 pgd_t *pgd;
2960 pud_t *pud;
2961 pmd_t *pmd;
2962 pte_t *pte;
2963 int i, nr = HPAGE_PMD_NR;
2964
2965 /* Skip pages which doesn't belong to the VMA */
2966 if (address < vma->vm_start) {
2967 int off = (vma->vm_start - address) >> PAGE_SHIFT;
2968 page += off;
2969 nr -= off;
2970 address = vma->vm_start;
2971 }
2972
2973 pgd = pgd_offset(vma->vm_mm, address);
2974 if (!pgd_present(*pgd))
2975 return;
2976 pud = pud_offset(pgd, address);
2977 if (!pud_present(*pud))
2978 return;
2979 pmd = pmd_offset(pud, address);
2980 ptl = pmd_lock(vma->vm_mm, pmd);
2981 if (!pmd_present(*pmd)) {
2982 spin_unlock(ptl);
2983 return;
2984 }
2985 if (pmd_trans_huge(*pmd)) {
2986 if (page == pmd_page(*pmd))
2987 __split_huge_pmd_locked(vma, pmd, address, true);
2988 spin_unlock(ptl);
2989 return;
2990 }
2991 spin_unlock(ptl);
2992
2993 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
2994 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++) {
2995 pte_t entry, swp_pte;
2996 swp_entry_t swp_entry;
2997
2998 if (!pte_present(pte[i]))
2999 continue;
3000 if (page_to_pfn(page) != pte_pfn(pte[i]))
3001 continue;
3002 flush_cache_page(vma, address, page_to_pfn(page));
3003 entry = ptep_clear_flush(vma, address, pte + i);
3004 swp_entry = make_migration_entry(page, pte_write(entry));
3005 swp_pte = swp_entry_to_pte(swp_entry);
3006 if (pte_soft_dirty(entry))
3007 swp_pte = pte_swp_mksoft_dirty(swp_pte);
3008 set_pte_at(vma->vm_mm, address, pte + i, swp_pte);
3009 page_remove_rmap(page, false);
3010 put_page(page);
3011 }
3012 pte_unmap_unlock(pte, ptl);
3013}
3014
3015static void freeze_page(struct anon_vma *anon_vma, struct page *page)
3016{
3017 struct anon_vma_chain *avc;
3018 pgoff_t pgoff = page_to_pgoff(page);
3019
3020 VM_BUG_ON_PAGE(!PageHead(page), page);
3021
3022 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff,
3023 pgoff + HPAGE_PMD_NR - 1) {
3024 unsigned long haddr;
3025
3026 haddr = __vma_address(page, avc->vma) & HPAGE_PMD_MASK;
3027 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
3028 haddr, haddr + HPAGE_PMD_SIZE);
3029 freeze_page_vma(avc->vma, page, haddr);
3030 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
3031 haddr, haddr + HPAGE_PMD_SIZE);
3032 }
3033}
3034
3035static void unfreeze_page_vma(struct vm_area_struct *vma, struct page *page,
3036 unsigned long address)
3037{
3038 spinlock_t *ptl;
3039 pmd_t *pmd;
3040 pte_t *pte, entry;
3041 swp_entry_t swp_entry;
3042 int i, nr = HPAGE_PMD_NR;
3043
3044 /* Skip pages which doesn't belong to the VMA */
3045 if (address < vma->vm_start) {
3046 int off = (vma->vm_start - address) >> PAGE_SHIFT;
3047 page += off;
3048 nr -= off;
3049 address = vma->vm_start;
3050 }
3051
3052 pmd = mm_find_pmd(vma->vm_mm, address);
3053 if (!pmd)
3054 return;
3055 pte = pte_offset_map_lock(vma->vm_mm, pmd, address, &ptl);
3056 for (i = 0; i < nr; i++, address += PAGE_SIZE, page++) {
3057 if (!is_swap_pte(pte[i]))
3058 continue;
3059
3060 swp_entry = pte_to_swp_entry(pte[i]);
3061 if (!is_migration_entry(swp_entry))
3062 continue;
3063 if (migration_entry_to_page(swp_entry) != page)
3064 continue;
3065
3066 get_page(page);
3067 page_add_anon_rmap(page, vma, address, false);
3068
3069 entry = pte_mkold(mk_pte(page, vma->vm_page_prot));
3070 entry = pte_mkdirty(entry);
3071 if (is_write_migration_entry(swp_entry))
3072 entry = maybe_mkwrite(entry, vma);
3073
3074 flush_dcache_page(page);
3075 set_pte_at(vma->vm_mm, address, pte + i, entry);
3076
3077 /* No need to invalidate - it was non-present before */
3078 update_mmu_cache(vma, address, pte + i);
3079 }
3080 pte_unmap_unlock(pte, ptl);
3081}
3082
3083static void unfreeze_page(struct anon_vma *anon_vma, struct page *page)
3084{
3085 struct anon_vma_chain *avc;
3086 pgoff_t pgoff = page_to_pgoff(page);
3087
3088 anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root,
3089 pgoff, pgoff + HPAGE_PMD_NR - 1) {
3090 unsigned long address = __vma_address(page, avc->vma);
3091
3092 mmu_notifier_invalidate_range_start(avc->vma->vm_mm,
3093 address, address + HPAGE_PMD_SIZE);
3094 unfreeze_page_vma(avc->vma, page, address);
3095 mmu_notifier_invalidate_range_end(avc->vma->vm_mm,
3096 address, address + HPAGE_PMD_SIZE);
3097 }
3098}
3099
3100static int total_mapcount(struct page *page)
3101{
3102 int i, ret;
3103
3104 ret = compound_mapcount(page);
3105 for (i = 0; i < HPAGE_PMD_NR; i++)
3106 ret += atomic_read(&page[i]._mapcount) + 1;
3107
3108 if (PageDoubleMap(page))
3109 ret -= HPAGE_PMD_NR;
3110
3111 return ret;
3112}
3113
3114static int __split_huge_page_tail(struct page *head, int tail,
3115 struct lruvec *lruvec, struct list_head *list)
3116{
3117 int mapcount;
3118 struct page *page_tail = head + tail;
3119
3120 mapcount = atomic_read(&page_tail->_mapcount) + 1;
3121 VM_BUG_ON_PAGE(atomic_read(&page_tail->_count) != 0, page_tail);
3122
3123 /*
3124 * tail_page->_count is zero and not changing from under us. But
3125 * get_page_unless_zero() may be running from under us on the
3126 * tail_page. If we used atomic_set() below instead of atomic_add(), we
3127 * would then run atomic_set() concurrently with
3128 * get_page_unless_zero(), and atomic_set() is implemented in C not
3129 * using locked ops. spin_unlock on x86 sometime uses locked ops
3130 * because of PPro errata 66, 92, so unless somebody can guarantee
3131 * atomic_set() here would be safe on all archs (and not only on x86),
3132 * it's safer to use atomic_add().
3133 */
3134 atomic_add(mapcount + 1, &page_tail->_count);
3135
3136
3137 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3138 page_tail->flags |= (head->flags &
3139 ((1L << PG_referenced) |
3140 (1L << PG_swapbacked) |
3141 (1L << PG_mlocked) |
3142 (1L << PG_uptodate) |
3143 (1L << PG_active) |
3144 (1L << PG_locked) |
3145 (1L << PG_unevictable)));
3146 page_tail->flags |= (1L << PG_dirty);
3147
3148 /*
3149 * After clearing PageTail the gup refcount can be released.
3150 * Page flags also must be visible before we make the page non-compound.
3151 */
3152 smp_wmb();
3153
3154 clear_compound_head(page_tail);
3155
3156 if (page_is_young(head))
3157 set_page_young(page_tail);
3158 if (page_is_idle(head))
3159 set_page_idle(page_tail);
3160
3161 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003162 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003163 page_tail);
3164 page_tail->mapping = head->mapping;
3165
3166 page_tail->index = head->index + tail;
3167 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3168 lru_add_page_tail(head, page_tail, lruvec, list);
3169
3170 return mapcount;
3171}
3172
3173static void __split_huge_page(struct page *page, struct list_head *list)
3174{
3175 struct page *head = compound_head(page);
3176 struct zone *zone = page_zone(head);
3177 struct lruvec *lruvec;
3178 int i, tail_mapcount;
3179
3180 /* prevent PageLRU to go away from under us, and freeze lru stats */
3181 spin_lock_irq(&zone->lru_lock);
3182 lruvec = mem_cgroup_page_lruvec(head, zone);
3183
3184 /* complete memcg works before add pages to LRU */
3185 mem_cgroup_split_huge_fixup(head);
3186
3187 tail_mapcount = 0;
3188 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
3189 tail_mapcount += __split_huge_page_tail(head, i, lruvec, list);
3190 atomic_sub(tail_mapcount, &head->_count);
3191
3192 ClearPageCompound(head);
3193 spin_unlock_irq(&zone->lru_lock);
3194
3195 unfreeze_page(page_anon_vma(head), head);
3196
3197 for (i = 0; i < HPAGE_PMD_NR; i++) {
3198 struct page *subpage = head + i;
3199 if (subpage == page)
3200 continue;
3201 unlock_page(subpage);
3202
3203 /*
3204 * Subpages may be freed if there wasn't any mapping
3205 * like if add_to_swap() is running on a lru page that
3206 * had its mapping zapped. And freeing these pages
3207 * requires taking the lru_lock so we do the put_page
3208 * of the tail pages after the split is complete.
3209 */
3210 put_page(subpage);
3211 }
3212}
3213
3214/*
3215 * This function splits huge page into normal pages. @page can point to any
3216 * subpage of huge page to split. Split doesn't change the position of @page.
3217 *
3218 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3219 * The huge page must be locked.
3220 *
3221 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3222 *
3223 * Both head page and tail pages will inherit mapping, flags, and so on from
3224 * the hugepage.
3225 *
3226 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3227 * they are not mapped.
3228 *
3229 * Returns 0 if the hugepage is split successfully.
3230 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3231 * us.
3232 */
3233int split_huge_page_to_list(struct page *page, struct list_head *list)
3234{
3235 struct page *head = compound_head(page);
3236 struct anon_vma *anon_vma;
3237 int count, mapcount, ret;
3238
3239 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3240 VM_BUG_ON_PAGE(!PageAnon(page), page);
3241 VM_BUG_ON_PAGE(!PageLocked(page), page);
3242 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3243 VM_BUG_ON_PAGE(!PageCompound(page), page);
3244
3245 /*
3246 * The caller does not necessarily hold an mmap_sem that would prevent
3247 * the anon_vma disappearing so we first we take a reference to it
3248 * and then lock the anon_vma for write. This is similar to
3249 * page_lock_anon_vma_read except the write lock is taken to serialise
3250 * against parallel split or collapse operations.
3251 */
3252 anon_vma = page_get_anon_vma(head);
3253 if (!anon_vma) {
3254 ret = -EBUSY;
3255 goto out;
3256 }
3257 anon_vma_lock_write(anon_vma);
3258
3259 /*
3260 * Racy check if we can split the page, before freeze_page() will
3261 * split PMDs
3262 */
3263 if (total_mapcount(head) != page_count(head) - 1) {
3264 ret = -EBUSY;
3265 goto out_unlock;
3266 }
3267
3268 freeze_page(anon_vma, head);
3269 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3270
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003271 /* Prevent deferred_split_scan() touching ->_count */
3272 spin_lock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003273 count = page_count(head);
3274 mapcount = total_mapcount(head);
3275 if (mapcount == count - 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003276 if (!list_empty(page_deferred_list(head))) {
3277 split_queue_len--;
3278 list_del(page_deferred_list(head));
3279 }
3280 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003281 __split_huge_page(page, list);
3282 ret = 0;
3283 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount > count - 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003284 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003285 pr_alert("total_mapcount: %u, page_count(): %u\n",
3286 mapcount, count);
3287 if (PageTail(page))
3288 dump_page(head, NULL);
3289 dump_page(page, "total_mapcount(head) > page_count(head) - 1");
3290 BUG();
3291 } else {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003292 spin_unlock(&split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003293 unfreeze_page(anon_vma, head);
3294 ret = -EBUSY;
3295 }
3296
3297out_unlock:
3298 anon_vma_unlock_write(anon_vma);
3299 put_anon_vma(anon_vma);
3300out:
3301 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3302 return ret;
3303}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003304
3305void free_transhuge_page(struct page *page)
3306{
3307 unsigned long flags;
3308
3309 spin_lock_irqsave(&split_queue_lock, flags);
3310 if (!list_empty(page_deferred_list(page))) {
3311 split_queue_len--;
3312 list_del(page_deferred_list(page));
3313 }
3314 spin_unlock_irqrestore(&split_queue_lock, flags);
3315 free_compound_page(page);
3316}
3317
3318void deferred_split_huge_page(struct page *page)
3319{
3320 unsigned long flags;
3321
3322 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3323
3324 spin_lock_irqsave(&split_queue_lock, flags);
3325 if (list_empty(page_deferred_list(page))) {
3326 list_add_tail(page_deferred_list(page), &split_queue);
3327 split_queue_len++;
3328 }
3329 spin_unlock_irqrestore(&split_queue_lock, flags);
3330}
3331
3332static unsigned long deferred_split_count(struct shrinker *shrink,
3333 struct shrink_control *sc)
3334{
3335 /*
3336 * Split a page from split_queue will free up at least one page,
3337 * at most HPAGE_PMD_NR - 1. We don't track exact number.
3338 * Let's use HPAGE_PMD_NR / 2 as ballpark.
3339 */
3340 return ACCESS_ONCE(split_queue_len) * HPAGE_PMD_NR / 2;
3341}
3342
3343static unsigned long deferred_split_scan(struct shrinker *shrink,
3344 struct shrink_control *sc)
3345{
3346 unsigned long flags;
3347 LIST_HEAD(list), *pos, *next;
3348 struct page *page;
3349 int split = 0;
3350
3351 spin_lock_irqsave(&split_queue_lock, flags);
3352 list_splice_init(&split_queue, &list);
3353
3354 /* Take pin on all head pages to avoid freeing them under us */
3355 list_for_each_safe(pos, next, &list) {
3356 page = list_entry((void *)pos, struct page, mapping);
3357 page = compound_head(page);
3358 /* race with put_compound_page() */
3359 if (!get_page_unless_zero(page)) {
3360 list_del_init(page_deferred_list(page));
3361 split_queue_len--;
3362 }
3363 }
3364 spin_unlock_irqrestore(&split_queue_lock, flags);
3365
3366 list_for_each_safe(pos, next, &list) {
3367 page = list_entry((void *)pos, struct page, mapping);
3368 lock_page(page);
3369 /* split_huge_page() removes page from list on success */
3370 if (!split_huge_page(page))
3371 split++;
3372 unlock_page(page);
3373 put_page(page);
3374 }
3375
3376 spin_lock_irqsave(&split_queue_lock, flags);
3377 list_splice_tail(&list, &split_queue);
3378 spin_unlock_irqrestore(&split_queue_lock, flags);
3379
3380 return split * HPAGE_PMD_NR / 2;
3381}
3382
3383static struct shrinker deferred_split_shrinker = {
3384 .count_objects = deferred_split_count,
3385 .scan_objects = deferred_split_scan,
3386 .seeks = DEFAULT_SEEKS,
3387};