blob: 3a20f11248a13d5e759ee1f99e97b8faed179150 [file] [log] [blame]
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001/*
2 * Copyright (C) 2009 Red Hat, Inc.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 */
7
Andrew Mortonae3a8c12014-06-04 16:06:58 -07008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080010#include <linux/mm.h>
11#include <linux/sched.h>
12#include <linux/highmem.h>
13#include <linux/hugetlb.h>
14#include <linux/mmu_notifier.h>
15#include <linux/rmap.h>
16#include <linux/swap.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080017#include <linux/shrinker.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080018#include <linux/mm_inline.h>
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -080019#include <linux/swapops.h>
Matthew Wilcox4897c762015-09-08 14:58:45 -070020#include <linux/dax.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080021#include <linux/kthread.h>
22#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080023#include <linux/freezer.h>
Dan Williamsf25748e32016-01-15 16:56:43 -080024#include <linux/pfn_t.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080025#include <linux/mman.h>
Dan Williams3565fce2016-01-15 16:56:55 -080026#include <linux/memremap.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020027#include <linux/pagemap.h>
Kirill A. Shutemov49071d42016-01-15 16:54:40 -080028#include <linux/debugfs.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000029#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080030#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070031#include <linux/userfaultfd_k.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070032#include <linux/page_idle.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080033
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080034#include <asm/tlb.h>
35#include <asm/pgalloc.h>
36#include "internal.h"
37
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080038enum scan_result {
39 SCAN_FAIL,
40 SCAN_SUCCEED,
41 SCAN_PMD_NULL,
42 SCAN_EXCEED_NONE_PTE,
43 SCAN_PTE_NON_PRESENT,
44 SCAN_PAGE_RO,
45 SCAN_NO_REFERENCED_PAGE,
46 SCAN_PAGE_NULL,
47 SCAN_SCAN_ABORT,
48 SCAN_PAGE_COUNT,
49 SCAN_PAGE_LRU,
50 SCAN_PAGE_LOCK,
51 SCAN_PAGE_ANON,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080052 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080053 SCAN_ANY_PROCESS,
54 SCAN_VMA_NULL,
55 SCAN_VMA_CHECK,
56 SCAN_ADDRESS_RANGE,
57 SCAN_SWAP_CACHE_PAGE,
58 SCAN_DEL_PAGE_LRU,
59 SCAN_ALLOC_HUGE_PAGE_FAIL,
Ebru Akagunduz70652f62016-07-26 15:24:59 -070060 SCAN_CGROUP_CHARGE_FAIL,
61 SCAN_EXCEED_SWAP_PTE
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080062};
63
64#define CREATE_TRACE_POINTS
65#include <trace/events/huge_memory.h>
66
Andrea Arcangeliba761492011-01-13 15:46:58 -080067/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080068 * By default transparent hugepage support is disabled in order that avoid
69 * to risk increase the memory footprint of applications without a guaranteed
70 * benefit. When transparent hugepage support is enabled, is for all mappings,
71 * and khugepaged scans all mappings.
72 * Defrag is invoked by khugepaged hugepage allocations and by page faults
73 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080074 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080075unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080076#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080077 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080078#endif
79#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
80 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
81#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070082 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080083 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
84 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080085
86/* default scan 8*512 pte (or vmas) every 30 second */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053087static unsigned int khugepaged_pages_to_scan __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -080088static unsigned int khugepaged_pages_collapsed;
89static unsigned int khugepaged_full_scans;
90static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
91/* during fragmentation poll the hugepage allocator once every minute */
92static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
David Rientjesf0508972016-05-20 16:58:56 -070093static unsigned long khugepaged_sleep_expire;
Andrea Arcangeliba761492011-01-13 15:46:58 -080094static struct task_struct *khugepaged_thread __read_mostly;
95static DEFINE_MUTEX(khugepaged_mutex);
96static DEFINE_SPINLOCK(khugepaged_mm_lock);
97static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
98/*
99 * default collapse hugepages if there is at least one pte mapped like
100 * it would have happened if the vma was large enough during page
101 * fault.
102 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530103static unsigned int khugepaged_max_ptes_none __read_mostly;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700104static unsigned int khugepaged_max_ptes_swap __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800105
106static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800107static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700108static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800109
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800110#define MM_SLOTS_HASH_BITS 10
111static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
112
Andrea Arcangeliba761492011-01-13 15:46:58 -0800113static struct kmem_cache *mm_slot_cache __read_mostly;
114
115/**
116 * struct mm_slot - hash lookup from mm to mm_slot
117 * @hash: hash collision list
118 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
119 * @mm: the mm that this information is valid for
120 */
121struct mm_slot {
122 struct hlist_node hash;
123 struct list_head mm_node;
124 struct mm_struct *mm;
125};
126
127/**
128 * struct khugepaged_scan - cursor for scanning
129 * @mm_head: the head of the mm list to scan
130 * @mm_slot: the current mm_slot we are scanning
131 * @address: the next address inside that to be scanned
132 *
133 * There is only the one khugepaged_scan instance of this cursor structure.
134 */
135struct khugepaged_scan {
136 struct list_head mm_head;
137 struct mm_slot *mm_slot;
138 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700139};
140static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800141 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
142};
143
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800144static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800145
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700146static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800147{
148 struct zone *zone;
149 int nr_zones = 0;
150 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800151
Andrea Arcangelif0005652011-01-13 15:47:04 -0800152 for_each_populated_zone(zone)
153 nr_zones++;
154
Mel Gorman974a7862015-11-06 16:28:34 -0800155 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800156 recommended_min = pageblock_nr_pages * nr_zones * 2;
157
158 /*
159 * Make sure that on average at least two pageblocks are almost free
160 * of another type, one for a migratetype to fall back to and a
161 * second to avoid subsequent fallbacks of other types There are 3
162 * MIGRATE_TYPES we care about.
163 */
164 recommended_min += pageblock_nr_pages * nr_zones *
165 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
166
167 /* don't ever allow to reserve more than 5% of the lowmem */
168 recommended_min = min(recommended_min,
169 (unsigned long) nr_free_buffer_pages() / 20);
170 recommended_min <<= (PAGE_SHIFT-10);
171
Han Pingtian42aa83c2014-01-23 15:53:28 -0800172 if (recommended_min > min_free_kbytes) {
173 if (user_min_free_kbytes >= 0)
Joe Perches756a0252016-03-17 14:19:47 -0700174 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
Han Pingtian42aa83c2014-01-23 15:53:28 -0800175 min_free_kbytes, recommended_min);
176
Andrea Arcangelif0005652011-01-13 15:47:04 -0800177 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800178 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800179 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800180}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800181
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700182static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800183{
184 int err = 0;
185 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800186 if (!khugepaged_thread)
187 khugepaged_thread = kthread_run(khugepaged, NULL,
188 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530189 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700190 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800191 err = PTR_ERR(khugepaged_thread);
192 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700193 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800194 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700195
196 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800197 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800198
199 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700200 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700201 kthread_stop(khugepaged_thread);
202 khugepaged_thread = NULL;
203 }
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700204fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800205 return err;
206}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800207
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800208static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800209struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800210
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700211struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800212{
213 struct page *zero_page;
214retry:
215 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700216 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800217
218 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
219 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800220 if (!zero_page) {
221 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700222 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800223 }
224 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800225 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700226 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700228 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800229 goto retry;
230 }
231
232 /* We take additional reference here. It will be put back by shrinker */
233 atomic_set(&huge_zero_refcount, 2);
234 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700235 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800236}
237
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700238void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800239{
240 /*
241 * Counter should never go to zero here. Only shrinker can put
242 * last reference.
243 */
244 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
245}
246
Glauber Costa48896462013-08-28 10:18:15 +1000247static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
248 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800249{
Glauber Costa48896462013-08-28 10:18:15 +1000250 /* we can free zero page only if last reference remains */
251 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
252}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800253
Glauber Costa48896462013-08-28 10:18:15 +1000254static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
255 struct shrink_control *sc)
256{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800257 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700258 struct page *zero_page = xchg(&huge_zero_page, NULL);
259 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700260 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000261 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800262 }
263
264 return 0;
265}
266
267static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000268 .count_objects = shrink_huge_zero_page_count,
269 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800270 .seeks = DEFAULT_SEEKS,
271};
272
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800273#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800274
Mel Gorman444eb2a42016-03-17 14:19:23 -0700275static ssize_t triple_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800276 struct kobj_attribute *attr,
277 const char *buf, size_t count,
278 enum transparent_hugepage_flag enabled,
Mel Gorman444eb2a42016-03-17 14:19:23 -0700279 enum transparent_hugepage_flag deferred,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800280 enum transparent_hugepage_flag req_madv)
281{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700282 if (!memcmp("defer", buf,
283 min(sizeof("defer")-1, count))) {
284 if (enabled == deferred)
285 return -EINVAL;
286 clear_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800287 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700288 set_bit(deferred, &transparent_hugepage_flags);
289 } else if (!memcmp("always", buf,
290 min(sizeof("always")-1, count))) {
291 clear_bit(deferred, &transparent_hugepage_flags);
292 clear_bit(req_madv, &transparent_hugepage_flags);
293 set_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800294 } else if (!memcmp("madvise", buf,
295 min(sizeof("madvise")-1, count))) {
296 clear_bit(enabled, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700297 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800298 set_bit(req_madv, &transparent_hugepage_flags);
299 } else if (!memcmp("never", buf,
300 min(sizeof("never")-1, count))) {
301 clear_bit(enabled, &transparent_hugepage_flags);
302 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700303 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800304 } 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{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700313 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
314 return sprintf(buf, "[always] madvise never\n");
315 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
316 return sprintf(buf, "always [madvise] never\n");
317 else
318 return sprintf(buf, "always madvise [never]\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800319}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700320
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800321static ssize_t enabled_store(struct kobject *kobj,
322 struct kobj_attribute *attr,
323 const char *buf, size_t count)
324{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800325 ssize_t ret;
326
Mel Gorman444eb2a42016-03-17 14:19:23 -0700327 ret = triple_flag_store(kobj, attr, buf, count,
328 TRANSPARENT_HUGEPAGE_FLAG,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800329 TRANSPARENT_HUGEPAGE_FLAG,
330 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
331
332 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700333 int err;
334
335 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700336 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700337 mutex_unlock(&khugepaged_mutex);
338
Andrea Arcangeliba761492011-01-13 15:46:58 -0800339 if (err)
340 ret = err;
341 }
342
343 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800344}
345static struct kobj_attribute enabled_attr =
346 __ATTR(enabled, 0644, enabled_show, enabled_store);
347
348static ssize_t single_flag_show(struct kobject *kobj,
349 struct kobj_attribute *attr, char *buf,
350 enum transparent_hugepage_flag flag)
351{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700352 return sprintf(buf, "%d\n",
353 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800354}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700355
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800356static ssize_t single_flag_store(struct kobject *kobj,
357 struct kobj_attribute *attr,
358 const char *buf, size_t count,
359 enum transparent_hugepage_flag flag)
360{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700361 unsigned long value;
362 int ret;
363
364 ret = kstrtoul(buf, 10, &value);
365 if (ret < 0)
366 return ret;
367 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800368 return -EINVAL;
369
Ben Hutchingse27e6152011-04-14 15:22:21 -0700370 if (value)
371 set_bit(flag, &transparent_hugepage_flags);
372 else
373 clear_bit(flag, &transparent_hugepage_flags);
374
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800375 return count;
376}
377
378/*
379 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
380 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
381 * memory just to allocate one more hugepage.
382 */
383static ssize_t defrag_show(struct kobject *kobj,
384 struct kobj_attribute *attr, char *buf)
385{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700386 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
387 return sprintf(buf, "[always] defer madvise never\n");
388 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
389 return sprintf(buf, "always [defer] madvise never\n");
390 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
391 return sprintf(buf, "always defer [madvise] never\n");
392 else
393 return sprintf(buf, "always defer madvise [never]\n");
394
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800395}
396static ssize_t defrag_store(struct kobject *kobj,
397 struct kobj_attribute *attr,
398 const char *buf, size_t count)
399{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700400 return triple_flag_store(kobj, attr, buf, count,
401 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
402 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800403 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
404}
405static struct kobj_attribute defrag_attr =
406 __ATTR(defrag, 0644, defrag_show, defrag_store);
407
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800408static ssize_t use_zero_page_show(struct kobject *kobj,
409 struct kobj_attribute *attr, char *buf)
410{
411 return single_flag_show(kobj, attr, buf,
412 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
413}
414static ssize_t use_zero_page_store(struct kobject *kobj,
415 struct kobj_attribute *attr, const char *buf, size_t count)
416{
417 return single_flag_store(kobj, attr, buf, count,
418 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
419}
420static struct kobj_attribute use_zero_page_attr =
421 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800422#ifdef CONFIG_DEBUG_VM
423static ssize_t debug_cow_show(struct kobject *kobj,
424 struct kobj_attribute *attr, char *buf)
425{
426 return single_flag_show(kobj, attr, buf,
427 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
428}
429static ssize_t debug_cow_store(struct kobject *kobj,
430 struct kobj_attribute *attr,
431 const char *buf, size_t count)
432{
433 return single_flag_store(kobj, attr, buf, count,
434 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
435}
436static struct kobj_attribute debug_cow_attr =
437 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
438#endif /* CONFIG_DEBUG_VM */
439
440static struct attribute *hugepage_attr[] = {
441 &enabled_attr.attr,
442 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800443 &use_zero_page_attr.attr,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800444#ifdef CONFIG_DEBUG_VM
445 &debug_cow_attr.attr,
446#endif
447 NULL,
448};
449
450static struct attribute_group hugepage_attr_group = {
451 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800452};
453
454static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
455 struct kobj_attribute *attr,
456 char *buf)
457{
458 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
459}
460
461static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
462 struct kobj_attribute *attr,
463 const char *buf, size_t count)
464{
465 unsigned long msecs;
466 int err;
467
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700468 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800469 if (err || msecs > UINT_MAX)
470 return -EINVAL;
471
472 khugepaged_scan_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700473 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800474 wake_up_interruptible(&khugepaged_wait);
475
476 return count;
477}
478static struct kobj_attribute scan_sleep_millisecs_attr =
479 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
480 scan_sleep_millisecs_store);
481
482static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
483 struct kobj_attribute *attr,
484 char *buf)
485{
486 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
487}
488
489static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
490 struct kobj_attribute *attr,
491 const char *buf, size_t count)
492{
493 unsigned long msecs;
494 int err;
495
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700496 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800497 if (err || msecs > UINT_MAX)
498 return -EINVAL;
499
500 khugepaged_alloc_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700501 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800502 wake_up_interruptible(&khugepaged_wait);
503
504 return count;
505}
506static struct kobj_attribute alloc_sleep_millisecs_attr =
507 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
508 alloc_sleep_millisecs_store);
509
510static ssize_t pages_to_scan_show(struct kobject *kobj,
511 struct kobj_attribute *attr,
512 char *buf)
513{
514 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
515}
516static ssize_t pages_to_scan_store(struct kobject *kobj,
517 struct kobj_attribute *attr,
518 const char *buf, size_t count)
519{
520 int err;
521 unsigned long pages;
522
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700523 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800524 if (err || !pages || pages > UINT_MAX)
525 return -EINVAL;
526
527 khugepaged_pages_to_scan = pages;
528
529 return count;
530}
531static struct kobj_attribute pages_to_scan_attr =
532 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
533 pages_to_scan_store);
534
535static ssize_t pages_collapsed_show(struct kobject *kobj,
536 struct kobj_attribute *attr,
537 char *buf)
538{
539 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
540}
541static struct kobj_attribute pages_collapsed_attr =
542 __ATTR_RO(pages_collapsed);
543
544static ssize_t full_scans_show(struct kobject *kobj,
545 struct kobj_attribute *attr,
546 char *buf)
547{
548 return sprintf(buf, "%u\n", khugepaged_full_scans);
549}
550static struct kobj_attribute full_scans_attr =
551 __ATTR_RO(full_scans);
552
553static ssize_t khugepaged_defrag_show(struct kobject *kobj,
554 struct kobj_attribute *attr, char *buf)
555{
556 return single_flag_show(kobj, attr, buf,
557 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
558}
559static ssize_t khugepaged_defrag_store(struct kobject *kobj,
560 struct kobj_attribute *attr,
561 const char *buf, size_t count)
562{
563 return single_flag_store(kobj, attr, buf, count,
564 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
565}
566static struct kobj_attribute khugepaged_defrag_attr =
567 __ATTR(defrag, 0644, khugepaged_defrag_show,
568 khugepaged_defrag_store);
569
570/*
571 * max_ptes_none controls if khugepaged should collapse hugepages over
572 * any unmapped ptes in turn potentially increasing the memory
573 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
574 * reduce the available free memory in the system as it
575 * runs. Increasing max_ptes_none will instead potentially reduce the
576 * free memory in the system during the khugepaged scan.
577 */
578static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
579 struct kobj_attribute *attr,
580 char *buf)
581{
582 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
583}
584static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
585 struct kobj_attribute *attr,
586 const char *buf, size_t count)
587{
588 int err;
589 unsigned long max_ptes_none;
590
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700591 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800592 if (err || max_ptes_none > HPAGE_PMD_NR-1)
593 return -EINVAL;
594
595 khugepaged_max_ptes_none = max_ptes_none;
596
597 return count;
598}
599static struct kobj_attribute khugepaged_max_ptes_none_attr =
600 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
601 khugepaged_max_ptes_none_store);
602
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700603static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
604 struct kobj_attribute *attr,
605 char *buf)
606{
607 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
608}
609
610static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
611 struct kobj_attribute *attr,
612 const char *buf, size_t count)
613{
614 int err;
615 unsigned long max_ptes_swap;
616
617 err = kstrtoul(buf, 10, &max_ptes_swap);
618 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
619 return -EINVAL;
620
621 khugepaged_max_ptes_swap = max_ptes_swap;
622
623 return count;
624}
625
626static struct kobj_attribute khugepaged_max_ptes_swap_attr =
627 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
628 khugepaged_max_ptes_swap_store);
629
Andrea Arcangeliba761492011-01-13 15:46:58 -0800630static struct attribute *khugepaged_attr[] = {
631 &khugepaged_defrag_attr.attr,
632 &khugepaged_max_ptes_none_attr.attr,
633 &pages_to_scan_attr.attr,
634 &pages_collapsed_attr.attr,
635 &full_scans_attr.attr,
636 &scan_sleep_millisecs_attr.attr,
637 &alloc_sleep_millisecs_attr.attr,
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700638 &khugepaged_max_ptes_swap_attr.attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800639 NULL,
640};
641
642static struct attribute_group khugepaged_attr_group = {
643 .attrs = khugepaged_attr,
644 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800645};
Shaohua Li569e5592012-01-12 17:19:11 -0800646
647static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
648{
649 int err;
650
651 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
652 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700653 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800654 return -ENOMEM;
655 }
656
657 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
658 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700659 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800660 goto delete_obj;
661 }
662
663 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
664 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700665 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800666 goto remove_hp_group;
667 }
668
669 return 0;
670
671remove_hp_group:
672 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
673delete_obj:
674 kobject_put(*hugepage_kobj);
675 return err;
676}
677
678static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
679{
680 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
681 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
682 kobject_put(hugepage_kobj);
683}
684#else
685static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
686{
687 return 0;
688}
689
690static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
691{
692}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800693#endif /* CONFIG_SYSFS */
694
695static int __init hugepage_init(void)
696{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800697 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800698 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800699
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800700 if (!has_transparent_hugepage()) {
701 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800702 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800703 }
704
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530705 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
706 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700707 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530708 /*
709 * hugepages can't be allocated by the buddy allocator
710 */
711 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
712 /*
713 * we use page->mapping and page->index in second tail page
714 * as list_head: assuming THP order >= 2
715 */
716 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
717
Shaohua Li569e5592012-01-12 17:19:11 -0800718 err = hugepage_init_sysfs(&hugepage_kobj);
719 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700720 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800721
722 err = khugepaged_slab_init();
723 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700724 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800725
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700726 err = register_shrinker(&huge_zero_page_shrinker);
727 if (err)
728 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800729 err = register_shrinker(&deferred_split_shrinker);
730 if (err)
731 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800732
Rik van Riel97562cd2011-01-13 15:47:12 -0800733 /*
734 * By default disable transparent hugepages on smaller systems,
735 * where the extra memory used could hurt more than TLB overhead
736 * is likely to save. The admin can still enable it through /sys.
737 */
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700738 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800739 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700740 return 0;
741 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800742
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700743 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700744 if (err)
745 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800746
Shaohua Li569e5592012-01-12 17:19:11 -0800747 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700748err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800749 unregister_shrinker(&deferred_split_shrinker);
750err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700751 unregister_shrinker(&huge_zero_page_shrinker);
752err_hzp_shrinker:
753 khugepaged_slab_exit();
754err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800755 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700756err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800757 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800758}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800759subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800760
761static int __init setup_transparent_hugepage(char *str)
762{
763 int ret = 0;
764 if (!str)
765 goto out;
766 if (!strcmp(str, "always")) {
767 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
768 &transparent_hugepage_flags);
769 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
770 &transparent_hugepage_flags);
771 ret = 1;
772 } else if (!strcmp(str, "madvise")) {
773 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
774 &transparent_hugepage_flags);
775 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
776 &transparent_hugepage_flags);
777 ret = 1;
778 } else if (!strcmp(str, "never")) {
779 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
780 &transparent_hugepage_flags);
781 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
782 &transparent_hugepage_flags);
783 ret = 1;
784 }
785out:
786 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700787 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800788 return ret;
789}
790__setup("transparent_hugepage=", setup_transparent_hugepage);
791
Mel Gormanb32967f2012-11-19 12:35:47 +0000792pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800793{
794 if (likely(vma->vm_flags & VM_WRITE))
795 pmd = pmd_mkwrite(pmd);
796 return pmd;
797}
798
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800799static inline struct list_head *page_deferred_list(struct page *page)
800{
801 /*
802 * ->lru in the tail pages is occupied by compound_head.
803 * Let's use ->mapping + ->index in the second tail page as list_head.
804 */
805 return (struct list_head *)&page[2].mapping;
806}
807
808void prep_transhuge_page(struct page *page)
809{
810 /*
811 * we use page->mapping and page->indexlru in second tail page
812 * as list_head: assuming THP order >= 2
813 */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800814
815 INIT_LIST_HEAD(page_deferred_list(page));
816 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
817}
818
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700819static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page,
820 gfp_t gfp)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800821{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700822 struct vm_area_struct *vma = fe->vma;
Johannes Weiner00501b52014-08-08 14:19:20 -0700823 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800824 pgtable_t pgtable;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700825 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800826
Sasha Levin309381fea2014-01-23 15:52:54 -0800827 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700828
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700829 if (mem_cgroup_try_charge(page, vma->vm_mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700830 put_page(page);
831 count_vm_event(THP_FAULT_FALLBACK);
832 return VM_FAULT_FALLBACK;
833 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800834
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700835 pgtable = pte_alloc_one(vma->vm_mm, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -0700836 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800837 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700838 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700839 return VM_FAULT_OOM;
840 }
841
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800842 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700843 /*
844 * The memory barrier inside __SetPageUptodate makes sure that
845 * clear_huge_page writes become visible before the set_pmd_at()
846 * write.
847 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800848 __SetPageUptodate(page);
849
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700850 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
851 if (unlikely(!pmd_none(*fe->pmd))) {
852 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800853 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800854 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700855 pte_free(vma->vm_mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800856 } else {
857 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700858
859 /* Deliver the page fault to userland */
860 if (userfaultfd_missing(vma)) {
861 int ret;
862
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700863 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800864 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700865 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700866 pte_free(vma->vm_mm, pgtable);
867 ret = handle_userfault(fe, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700868 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
869 return ret;
870 }
871
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700872 entry = mk_huge_pmd(page, vma->vm_page_prot);
873 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800874 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800875 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700876 lru_cache_add_active_or_unevictable(page, vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700877 pgtable_trans_huge_deposit(vma->vm_mm, fe->pmd, pgtable);
878 set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
879 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
880 atomic_long_inc(&vma->vm_mm->nr_ptes);
881 spin_unlock(fe->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700882 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800883 }
884
David Rientjesaa2e8782012-05-29 15:06:17 -0700885 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800886}
887
Mel Gorman444eb2a42016-03-17 14:19:23 -0700888/*
889 * If THP is set to always then directly reclaim/compact as necessary
890 * If set to defer then do no reclaim and defer to khugepaged
891 * If set to madvise and the VMA is flagged then directly reclaim/compact
892 */
893static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800894{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700895 gfp_t reclaim_flags = 0;
896
897 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
898 (vma->vm_flags & VM_HUGEPAGE))
899 reclaim_flags = __GFP_DIRECT_RECLAIM;
900 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
901 reclaim_flags = __GFP_KSWAPD_RECLAIM;
902 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
903 reclaim_flags = __GFP_DIRECT_RECLAIM;
904
905 return GFP_TRANSHUGE | reclaim_flags;
906}
907
908/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
909static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
910{
911 return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800912}
913
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800914/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700915static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800916 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700917 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800918{
919 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700920 if (!pmd_none(*pmd))
921 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700922 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800923 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800924 if (pgtable)
925 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800926 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800927 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700928 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800929}
930
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700931int do_huge_pmd_anonymous_page(struct fault_env *fe)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800932{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700933 struct vm_area_struct *vma = fe->vma;
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800934 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800935 struct page *page;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700936 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800937
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700938 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700939 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700940 if (unlikely(anon_vma_prepare(vma)))
941 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700942 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700943 return VM_FAULT_OOM;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700944 if (!(fe->flags & FAULT_FLAG_WRITE) &&
945 !mm_forbids_zeropage(vma->vm_mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700946 transparent_hugepage_use_zero_page()) {
947 pgtable_t pgtable;
948 struct page *zero_page;
949 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700950 int ret;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700951 pgtable = pte_alloc_one(vma->vm_mm, haddr);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700952 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800953 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700954 zero_page = get_huge_zero_page();
955 if (unlikely(!zero_page)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700956 pte_free(vma->vm_mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700957 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700958 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700959 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700960 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700961 ret = 0;
962 set = false;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700963 if (pmd_none(*fe->pmd)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700964 if (userfaultfd_missing(vma)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700965 spin_unlock(fe->ptl);
966 ret = handle_userfault(fe, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700967 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
968 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700969 set_huge_zero_page(pgtable, vma->vm_mm, vma,
970 haddr, fe->pmd, zero_page);
971 spin_unlock(fe->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700972 set = true;
973 }
974 } else
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700975 spin_unlock(fe->ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700976 if (!set) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700977 pte_free(vma->vm_mm, pgtable);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700978 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800979 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700980 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800981 }
Mel Gorman444eb2a42016-03-17 14:19:23 -0700982 gfp = alloc_hugepage_direct_gfpmask(vma);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800983 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700984 if (unlikely(!page)) {
985 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700986 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700987 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800988 prep_transhuge_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700989 return __do_huge_pmd_anonymous_page(fe, page, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800990}
991
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700992static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800993 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700994{
995 struct mm_struct *mm = vma->vm_mm;
996 pmd_t entry;
997 spinlock_t *ptl;
998
999 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -08001000 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
1001 if (pfn_t_devmap(pfn))
1002 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -08001003 if (write) {
1004 entry = pmd_mkyoung(pmd_mkdirty(entry));
1005 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001006 }
Ross Zwisler01871e52016-01-15 16:56:02 -08001007 set_pmd_at(mm, addr, pmd, entry);
1008 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001009 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001010}
1011
1012int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -08001013 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001014{
1015 pgprot_t pgprot = vma->vm_page_prot;
1016 /*
1017 * If we had pmd_special, we could avoid all these restrictions,
1018 * but we need to be consistent with PTEs and architectures that
1019 * can't support a 'special' bit.
1020 */
1021 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1022 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1023 (VM_PFNMAP|VM_MIXEDMAP));
1024 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -08001025 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001026
1027 if (addr < vma->vm_start || addr >= vma->vm_end)
1028 return VM_FAULT_SIGBUS;
1029 if (track_pfn_insert(vma, &pgprot, pfn))
1030 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001031 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
1032 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001033}
Dan Williamsdee41072016-05-14 12:20:44 -07001034EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001035
Dan Williams3565fce2016-01-15 16:56:55 -08001036static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1037 pmd_t *pmd)
1038{
1039 pmd_t _pmd;
1040
1041 /*
1042 * We should set the dirty bit only for FOLL_WRITE but for now
1043 * the dirty bit in the pmd is meaningless. And if the dirty
1044 * bit will become meaningful and we'll only set it with
1045 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1046 * set the young bit, instead of the current set_pmd_at.
1047 */
1048 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1049 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1050 pmd, _pmd, 1))
1051 update_mmu_cache_pmd(vma, addr, pmd);
1052}
1053
1054struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1055 pmd_t *pmd, int flags)
1056{
1057 unsigned long pfn = pmd_pfn(*pmd);
1058 struct mm_struct *mm = vma->vm_mm;
1059 struct dev_pagemap *pgmap;
1060 struct page *page;
1061
1062 assert_spin_locked(pmd_lockptr(mm, pmd));
1063
1064 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1065 return NULL;
1066
1067 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1068 /* pass */;
1069 else
1070 return NULL;
1071
1072 if (flags & FOLL_TOUCH)
1073 touch_pmd(vma, addr, pmd);
1074
1075 /*
1076 * device mapped pages can only be returned if the
1077 * caller will manage the page reference count.
1078 */
1079 if (!(flags & FOLL_GET))
1080 return ERR_PTR(-EEXIST);
1081
1082 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1083 pgmap = get_dev_pagemap(pfn, NULL);
1084 if (!pgmap)
1085 return ERR_PTR(-EFAULT);
1086 page = pfn_to_page(pfn);
1087 get_page(page);
1088 put_dev_pagemap(pgmap);
1089
1090 return page;
1091}
1092
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001093int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1094 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1095 struct vm_area_struct *vma)
1096{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001097 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098 struct page *src_page;
1099 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001100 pgtable_t pgtable = NULL;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001101 int ret = -ENOMEM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001103 /* Skip if can be re-fill on fault */
1104 if (!vma_is_anonymous(vma))
1105 return 0;
1106
1107 pgtable = pte_alloc_one(dst_mm, addr);
1108 if (unlikely(!pgtable))
1109 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001110
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001111 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1112 src_ptl = pmd_lockptr(src_mm, src_pmd);
1113 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001114
1115 ret = -EAGAIN;
1116 pmd = *src_pmd;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001117 if (unlikely(!pmd_trans_huge(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001118 pte_free(dst_mm, pgtable);
1119 goto out_unlock;
1120 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001121 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001122 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001123 * under splitting since we don't split the page itself, only pmd to
1124 * a page table.
1125 */
1126 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001127 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001128 /*
1129 * get_huge_zero_page() will never allocate a new page here,
1130 * since we already have a zero page to copy. It just takes a
1131 * reference.
1132 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001133 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001134 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001135 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001136 ret = 0;
1137 goto out_unlock;
1138 }
Mel Gormande466bd2013-12-18 17:08:42 -08001139
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001140 src_page = pmd_page(pmd);
1141 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1142 get_page(src_page);
1143 page_dup_rmap(src_page, true);
1144 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1145 atomic_long_inc(&dst_mm->nr_ptes);
1146 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147
1148 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1149 pmd = pmd_mkold(pmd_wrprotect(pmd));
1150 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001151
1152 ret = 0;
1153out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001154 spin_unlock(src_ptl);
1155 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001156out:
1157 return ret;
1158}
1159
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001160void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd)
Will Deacona1dd4502012-12-11 16:01:27 -08001161{
1162 pmd_t entry;
1163 unsigned long haddr;
1164
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001165 fe->ptl = pmd_lock(fe->vma->vm_mm, fe->pmd);
1166 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Will Deacona1dd4502012-12-11 16:01:27 -08001167 goto unlock;
1168
1169 entry = pmd_mkyoung(orig_pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001170 haddr = fe->address & HPAGE_PMD_MASK;
1171 if (pmdp_set_access_flags(fe->vma, haddr, fe->pmd, entry,
1172 fe->flags & FAULT_FLAG_WRITE))
1173 update_mmu_cache_pmd(fe->vma, fe->address, fe->pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001174
1175unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001176 spin_unlock(fe->ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001177}
1178
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001179static int do_huge_pmd_wp_page_fallback(struct fault_env *fe, pmd_t orig_pmd,
1180 struct page *page)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001181{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001182 struct vm_area_struct *vma = fe->vma;
1183 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Johannes Weiner00501b52014-08-08 14:19:20 -07001184 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 pgtable_t pgtable;
1186 pmd_t _pmd;
1187 int ret = 0, i;
1188 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001189 unsigned long mmun_start; /* For mmu_notifiers */
1190 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001191
1192 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1193 GFP_KERNEL);
1194 if (unlikely(!pages)) {
1195 ret |= VM_FAULT_OOM;
1196 goto out;
1197 }
1198
1199 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001200 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001201 __GFP_OTHER_NODE, vma,
1202 fe->address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001203 if (unlikely(!pages[i] ||
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001204 mem_cgroup_try_charge(pages[i], vma->vm_mm,
1205 GFP_KERNEL, &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001206 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001207 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001208 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001209 memcg = (void *)page_private(pages[i]);
1210 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001211 mem_cgroup_cancel_charge(pages[i], memcg,
1212 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001213 put_page(pages[i]);
1214 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001215 kfree(pages);
1216 ret |= VM_FAULT_OOM;
1217 goto out;
1218 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001219 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001220 }
1221
1222 for (i = 0; i < HPAGE_PMD_NR; i++) {
1223 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001224 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001225 __SetPageUptodate(pages[i]);
1226 cond_resched();
1227 }
1228
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001229 mmun_start = haddr;
1230 mmun_end = haddr + HPAGE_PMD_SIZE;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001231 mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001232
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001233 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
1234 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001235 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001236 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001237
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001238 pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 /* leave pmd empty until pte is filled */
1240
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001241 pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, fe->pmd);
1242 pmd_populate(vma->vm_mm, &_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001243
1244 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001245 pte_t entry;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001246 entry = mk_pte(pages[i], vma->vm_page_prot);
1247 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001248 memcg = (void *)page_private(pages[i]);
1249 set_page_private(pages[i], 0);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001250 page_add_new_anon_rmap(pages[i], fe->vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001251 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001252 lru_cache_add_active_or_unevictable(pages[i], vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001253 fe->pte = pte_offset_map(&_pmd, haddr);
1254 VM_BUG_ON(!pte_none(*fe->pte));
1255 set_pte_at(vma->vm_mm, haddr, fe->pte, entry);
1256 pte_unmap(fe->pte);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001257 }
1258 kfree(pages);
1259
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001260 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001261 pmd_populate(vma->vm_mm, fe->pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001262 page_remove_rmap(page, true);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001263 spin_unlock(fe->ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001264
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001265 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001266
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001267 ret |= VM_FAULT_WRITE;
1268 put_page(page);
1269
1270out:
1271 return ret;
1272
1273out_free_pages:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001274 spin_unlock(fe->ptl);
1275 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001276 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001277 memcg = (void *)page_private(pages[i]);
1278 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001279 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001281 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001282 kfree(pages);
1283 goto out;
1284}
1285
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001286int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001287{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001288 struct vm_area_struct *vma = fe->vma;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001289 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001290 struct mem_cgroup *memcg;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001291 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001292 unsigned long mmun_start; /* For mmu_notifiers */
1293 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07001294 gfp_t huge_gfp; /* for allocation and charge */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001295 int ret = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001296
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001297 fe->ptl = pmd_lockptr(vma->vm_mm, fe->pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001298 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001299 if (is_huge_zero_pmd(orig_pmd))
1300 goto alloc;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001301 spin_lock(fe->ptl);
1302 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001303 goto out_unlock;
1304
1305 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001306 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001307 /*
1308 * We can only reuse the page if nobody else maps the huge page or it's
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001309 * part.
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001310 */
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001311 if (page_trans_huge_mapcount(page, NULL) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001312 pmd_t entry;
1313 entry = pmd_mkyoung(orig_pmd);
1314 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001315 if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1))
1316 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001317 ret |= VM_FAULT_WRITE;
1318 goto out_unlock;
1319 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001320 get_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001321 spin_unlock(fe->ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001322alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001323 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001324 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001325 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b3636922015-04-15 16:13:29 -07001326 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001327 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001328 new_page = NULL;
1329
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001330 if (likely(new_page)) {
1331 prep_transhuge_page(new_page);
1332 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001333 if (!page) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001334 split_huge_pmd(vma, fe->pmd, fe->address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001335 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001336 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001337 ret = do_huge_pmd_wp_page_fallback(fe, orig_pmd, page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001338 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001339 split_huge_pmd(vma, fe->pmd, fe->address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001340 ret |= VM_FAULT_FALLBACK;
1341 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001342 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001343 }
David Rientjes17766dd2013-09-12 15:14:06 -07001344 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001345 goto out;
1346 }
1347
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001348 if (unlikely(mem_cgroup_try_charge(new_page, vma->vm_mm,
1349 huge_gfp, &memcg, true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001350 put_page(new_page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001351 split_huge_pmd(vma, fe->pmd, fe->address);
1352 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001353 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001354 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001355 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001356 goto out;
1357 }
1358
David Rientjes17766dd2013-09-12 15:14:06 -07001359 count_vm_event(THP_FAULT_ALLOC);
1360
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001361 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001362 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1363 else
1364 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001365 __SetPageUptodate(new_page);
1366
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001367 mmun_start = haddr;
1368 mmun_end = haddr + HPAGE_PMD_SIZE;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001369 mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001370
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001371 spin_lock(fe->ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001372 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001373 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001374 if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) {
1375 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001376 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001377 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001378 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001379 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001380 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001381 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1382 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001383 pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001384 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001385 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001386 lru_cache_add_active_or_unevictable(new_page, vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001387 set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
1388 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001389 if (!page) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001390 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001391 put_huge_zero_page();
1392 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001393 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001394 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001395 put_page(page);
1396 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001397 ret |= VM_FAULT_WRITE;
1398 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001399 spin_unlock(fe->ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001400out_mn:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001401 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001402out:
1403 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001404out_unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001405 spin_unlock(fe->ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001406 return ret;
1407}
1408
David Rientjesb676b292012-10-08 16:34:03 -07001409struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001410 unsigned long addr,
1411 pmd_t *pmd,
1412 unsigned int flags)
1413{
David Rientjesb676b292012-10-08 16:34:03 -07001414 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001415 struct page *page = NULL;
1416
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001417 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001418
1419 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1420 goto out;
1421
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001422 /* Avoid dumping huge zero page */
1423 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1424 return ERR_PTR(-EFAULT);
1425
Mel Gorman2b4847e2013-12-18 17:08:32 -08001426 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001427 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001428 goto out;
1429
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001430 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001431 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001432 if (flags & FOLL_TOUCH)
1433 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001434 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001435 /*
1436 * We don't mlock() pte-mapped THPs. This way we can avoid
1437 * leaking mlocked pages into non-VM_LOCKED VMAs.
1438 *
1439 * In most cases the pmd is the only mapping of the page as we
1440 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1441 * writable private mappings in populate_vma_page_range().
1442 *
1443 * The only scenario when we have the page shared here is if we
1444 * mlocking read-only mapping shared over fork(). We skip
1445 * mlocking such pages.
1446 */
1447 if (compound_mapcount(page) == 1 && !PageDoubleMap(page) &&
1448 page->mapping && trylock_page(page)) {
David Rientjesb676b292012-10-08 16:34:03 -07001449 lru_add_drain();
1450 if (page->mapping)
1451 mlock_vma_page(page);
1452 unlock_page(page);
1453 }
1454 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001455 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001456 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001457 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001458 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001459
1460out:
1461 return page;
1462}
1463
Mel Gormand10e63f2012-10-25 14:16:31 +02001464/* NUMA hinting page fault entry point for trans huge pmds */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001465int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t pmd)
Mel Gormand10e63f2012-10-25 14:16:31 +02001466{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001467 struct vm_area_struct *vma = fe->vma;
Mel Gormanb8916632013-10-07 11:28:44 +01001468 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001469 struct page *page;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001470 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001471 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001472 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001473 bool page_locked;
1474 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001475 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001476 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001477
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001478 /* A PROT_NONE fault should not end up here */
1479 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1480
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001481 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
1482 if (unlikely(!pmd_same(pmd, *fe->pmd)))
Mel Gormand10e63f2012-10-25 14:16:31 +02001483 goto out_unlock;
1484
Mel Gormande466bd2013-12-18 17:08:42 -08001485 /*
1486 * If there are potential migrations, wait for completion and retry
1487 * without disrupting NUMA hinting information. Do not relock and
1488 * check_same as the page may no longer be mapped.
1489 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001490 if (unlikely(pmd_trans_migrating(*fe->pmd))) {
1491 page = pmd_page(*fe->pmd);
1492 spin_unlock(fe->ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001493 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001494 goto out;
1495 }
1496
Mel Gormand10e63f2012-10-25 14:16:31 +02001497 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001498 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001499 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001500 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001501 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001502 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001503 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001504 flags |= TNF_FAULT_LOCAL;
1505 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001506
Mel Gormanbea66fb2015-03-25 15:55:37 -07001507 /* See similar comment in do_numa_page for explanation */
1508 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001509 flags |= TNF_NO_GROUP;
1510
1511 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001512 * Acquire the page lock to serialise THP migrations but avoid dropping
1513 * page_table_lock if at all possible
1514 */
Mel Gormanb8916632013-10-07 11:28:44 +01001515 page_locked = trylock_page(page);
1516 target_nid = mpol_misplaced(page, vma, haddr);
1517 if (target_nid == -1) {
1518 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001519 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001520 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001521 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001522
Mel Gormande466bd2013-12-18 17:08:42 -08001523 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001524 if (!page_locked) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001525 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001526 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001527 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001528 goto out;
1529 }
1530
Mel Gorman2b4847e2013-12-18 17:08:32 -08001531 /*
1532 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1533 * to serialises splits
1534 */
Mel Gormanb8916632013-10-07 11:28:44 +01001535 get_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001536 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001537 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001538
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001539 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001540 spin_lock(fe->ptl);
1541 if (unlikely(!pmd_same(pmd, *fe->pmd))) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001542 unlock_page(page);
1543 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001544 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001545 goto out_unlock;
1546 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001547
Mel Gormanc3a489c2013-12-18 17:08:38 -08001548 /* Bail if we fail to protect against THP splits for any reason */
1549 if (unlikely(!anon_vma)) {
1550 put_page(page);
1551 page_nid = -1;
1552 goto clear_pmdnuma;
1553 }
1554
Mel Gormana54a4072013-10-07 11:28:46 +01001555 /*
1556 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001557 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001558 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001559 spin_unlock(fe->ptl);
1560 migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
1561 fe->pmd, pmd, fe->address, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001562 if (migrated) {
1563 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001564 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001565 } else
1566 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001567
Mel Gorman8191acb2013-10-07 11:28:45 +01001568 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001569clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001570 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001571 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001572 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001573 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001574 if (was_writable)
1575 pmd = pmd_mkwrite(pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001576 set_pmd_at(vma->vm_mm, haddr, fe->pmd, pmd);
1577 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Mel Gormana54a4072013-10-07 11:28:46 +01001578 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001579out_unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001580 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001581
1582out:
1583 if (anon_vma)
1584 page_unlock_anon_vma_read(anon_vma);
1585
Mel Gorman8191acb2013-10-07 11:28:45 +01001586 if (page_nid != -1)
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001587 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, fe->flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001588
Mel Gormand10e63f2012-10-25 14:16:31 +02001589 return 0;
1590}
1591
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001592int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1593 pmd_t *pmd, unsigned long addr, unsigned long next)
1594
1595{
1596 spinlock_t *ptl;
1597 pmd_t orig_pmd;
1598 struct page *page;
1599 struct mm_struct *mm = tlb->mm;
1600 int ret = 0;
1601
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001602 ptl = pmd_trans_huge_lock(pmd, vma);
1603 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001604 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001605
1606 orig_pmd = *pmd;
1607 if (is_huge_zero_pmd(orig_pmd)) {
1608 ret = 1;
1609 goto out;
1610 }
1611
1612 page = pmd_page(orig_pmd);
1613 /*
1614 * If other processes are mapping this page, we couldn't discard
1615 * the page unless they all do MADV_FREE so let's skip the page.
1616 */
1617 if (page_mapcount(page) != 1)
1618 goto out;
1619
1620 if (!trylock_page(page))
1621 goto out;
1622
1623 /*
1624 * If user want to discard part-pages of THP, split it so MADV_FREE
1625 * will deactivate only them.
1626 */
1627 if (next - addr != HPAGE_PMD_SIZE) {
1628 get_page(page);
1629 spin_unlock(ptl);
Huang Ying9818b8c2016-07-14 12:07:12 -07001630 split_huge_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001631 put_page(page);
1632 unlock_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001633 goto out_unlocked;
1634 }
1635
1636 if (PageDirty(page))
1637 ClearPageDirty(page);
1638 unlock_page(page);
1639
1640 if (PageActive(page))
1641 deactivate_page(page);
1642
1643 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1644 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1645 tlb->fullmm);
1646 orig_pmd = pmd_mkold(orig_pmd);
1647 orig_pmd = pmd_mkclean(orig_pmd);
1648
1649 set_pmd_at(mm, addr, pmd, orig_pmd);
1650 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1651 }
1652 ret = 1;
1653out:
1654 spin_unlock(ptl);
1655out_unlocked:
1656 return ret;
1657}
1658
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001659int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001660 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001661{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001662 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001663 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001664
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001665 ptl = __pmd_trans_huge_lock(pmd, vma);
1666 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001667 return 0;
1668 /*
1669 * For architectures like ppc64 we look at deposited pgtable
1670 * when calling pmdp_huge_get_and_clear. So do the
1671 * pgtable_trans_huge_withdraw after finishing pmdp related
1672 * operations.
1673 */
1674 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1675 tlb->fullmm);
1676 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1677 if (vma_is_dax(vma)) {
1678 spin_unlock(ptl);
1679 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001680 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001681 } else if (is_huge_zero_pmd(orig_pmd)) {
1682 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1683 atomic_long_dec(&tlb->mm->nr_ptes);
1684 spin_unlock(ptl);
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001685 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001686 } else {
1687 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001688 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001689 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001690 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001691 if (PageAnon(page)) {
1692 pgtable_t pgtable;
1693 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
1694 pte_free(tlb->mm, pgtable);
1695 atomic_long_dec(&tlb->mm->nr_ptes);
1696 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1697 } else {
1698 add_mm_counter(tlb->mm, MM_FILEPAGES, -HPAGE_PMD_NR);
1699 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001700 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07001701 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001702 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001703 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001704}
1705
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001706bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001707 unsigned long new_addr, unsigned long old_end,
1708 pmd_t *old_pmd, pmd_t *new_pmd)
1709{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001710 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001711 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001712 struct mm_struct *mm = vma->vm_mm;
1713
1714 if ((old_addr & ~HPAGE_PMD_MASK) ||
1715 (new_addr & ~HPAGE_PMD_MASK) ||
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001716 old_end - old_addr < HPAGE_PMD_SIZE)
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001717 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001718
1719 /*
1720 * The destination pmd shouldn't be established, free_pgtables()
1721 * should have release it.
1722 */
1723 if (WARN_ON(!pmd_none(*new_pmd))) {
1724 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001725 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001726 }
1727
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001728 /*
1729 * We don't have to worry about the ordering of src and dst
1730 * ptlocks because exclusive mmap_sem prevents deadlock.
1731 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001732 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1733 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001734 new_ptl = pmd_lockptr(mm, new_pmd);
1735 if (new_ptl != old_ptl)
1736 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001737 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001738 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001739
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001740 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1741 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301742 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001743 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1744 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001745 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301746 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1747 if (new_ptl != old_ptl)
1748 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001749 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001750 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001751 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001752 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001753}
1754
Mel Gormanf123d742013-10-07 11:28:49 +01001755/*
1756 * Returns
1757 * - 0 if PMD could not be locked
1758 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1759 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1760 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001761int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001762 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001763{
1764 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001765 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001766 int ret = 0;
1767
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001768 ptl = __pmd_trans_huge_lock(pmd, vma);
1769 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001770 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001771 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc012015-03-07 15:20:48 +00001772 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001773
1774 /*
1775 * Avoid trapping faults against the zero page. The read-only
1776 * data is likely to be read-cached on the local CPU and
1777 * local/remote hits to the zero page are not interesting.
1778 */
1779 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1780 spin_unlock(ptl);
Mel Gormanba68bc012015-03-07 15:20:48 +00001781 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001782 }
1783
Mel Gorman10c10452015-02-12 14:58:44 -08001784 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001785 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001786 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001787 if (preserve_write)
1788 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001789 ret = HPAGE_PMD_NR;
1790 set_pmd_at(mm, addr, pmd, entry);
Kirill A. Shutemovb237ade2016-07-26 15:25:45 -07001791 BUG_ON(vma_is_anonymous(vma) && !preserve_write &&
1792 pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001793 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001794 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001795 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001796
1797 return ret;
1798}
1799
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001800/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001801 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001802 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001803 * Note that if it returns true, this routine returns without unlocking page
1804 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001805 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001806spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001807{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001808 spinlock_t *ptl;
1809 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001810 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001811 return ptl;
1812 spin_unlock(ptl);
1813 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001814}
1815
Vlastimil Babka9050d7e2014-03-03 15:38:27 -08001816#define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001817
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001818int hugepage_madvise(struct vm_area_struct *vma,
1819 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001820{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001821 switch (advice) {
1822 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001823#ifdef CONFIG_S390
1824 /*
1825 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1826 * can't handle this properly after s390_enable_sie, so we simply
1827 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1828 */
1829 if (mm_has_pgste(vma->vm_mm))
1830 return 0;
1831#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001832 /*
1833 * Be somewhat over-protective like KSM for now!
1834 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001835 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001836 return -EINVAL;
1837 *vm_flags &= ~VM_NOHUGEPAGE;
1838 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001839 /*
1840 * If the vma become good for khugepaged to scan,
1841 * register it here without waiting a page fault that
1842 * may not happen any time soon.
1843 */
David Rientjes6d50e602014-10-29 14:50:31 -07001844 if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags)))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001845 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001846 break;
1847 case MADV_NOHUGEPAGE:
1848 /*
1849 * Be somewhat over-protective like KSM for now!
1850 */
Jason J. Herne1a763612015-11-20 15:57:04 -08001851 if (*vm_flags & VM_NO_THP)
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001852 return -EINVAL;
1853 *vm_flags &= ~VM_HUGEPAGE;
1854 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001855 /*
1856 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1857 * this vma even if we leave the mm registered in khugepaged if
1858 * it got registered before VM_NOHUGEPAGE was set.
1859 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001860 break;
1861 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001862
1863 return 0;
1864}
1865
Andrea Arcangeliba761492011-01-13 15:46:58 -08001866static int __init khugepaged_slab_init(void)
1867{
1868 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1869 sizeof(struct mm_slot),
1870 __alignof__(struct mm_slot), 0, NULL);
1871 if (!mm_slot_cache)
1872 return -ENOMEM;
1873
1874 return 0;
1875}
1876
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001877static void __init khugepaged_slab_exit(void)
1878{
1879 kmem_cache_destroy(mm_slot_cache);
1880}
1881
Andrea Arcangeliba761492011-01-13 15:46:58 -08001882static inline struct mm_slot *alloc_mm_slot(void)
1883{
1884 if (!mm_slot_cache) /* initialization failed */
1885 return NULL;
1886 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1887}
1888
1889static inline void free_mm_slot(struct mm_slot *mm_slot)
1890{
1891 kmem_cache_free(mm_slot_cache, mm_slot);
1892}
1893
Andrea Arcangeliba761492011-01-13 15:46:58 -08001894static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1895{
1896 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001897
Sasha Levinb67bfe02013-02-27 17:06:00 -08001898 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001899 if (mm == mm_slot->mm)
1900 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001901
Andrea Arcangeliba761492011-01-13 15:46:58 -08001902 return NULL;
1903}
1904
1905static void insert_to_mm_slots_hash(struct mm_struct *mm,
1906 struct mm_slot *mm_slot)
1907{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001908 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001909 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001910}
1911
1912static inline int khugepaged_test_exit(struct mm_struct *mm)
1913{
1914 return atomic_read(&mm->mm_users) == 0;
1915}
1916
1917int __khugepaged_enter(struct mm_struct *mm)
1918{
1919 struct mm_slot *mm_slot;
1920 int wakeup;
1921
1922 mm_slot = alloc_mm_slot();
1923 if (!mm_slot)
1924 return -ENOMEM;
1925
1926 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001927 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001928 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1929 free_mm_slot(mm_slot);
1930 return 0;
1931 }
1932
1933 spin_lock(&khugepaged_mm_lock);
1934 insert_to_mm_slots_hash(mm, mm_slot);
1935 /*
1936 * Insert just behind the scanning cursor, to let the area settle
1937 * down a little.
1938 */
1939 wakeup = list_empty(&khugepaged_scan.mm_head);
1940 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1941 spin_unlock(&khugepaged_mm_lock);
1942
1943 atomic_inc(&mm->mm_count);
1944 if (wakeup)
1945 wake_up_interruptible(&khugepaged_wait);
1946
1947 return 0;
1948}
1949
David Rientjes6d50e602014-10-29 14:50:31 -07001950int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1951 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001952{
1953 unsigned long hstart, hend;
1954 if (!vma->anon_vma)
1955 /*
1956 * Not yet faulted in so we will register later in the
1957 * page fault if needed.
1958 */
1959 return 0;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07001960 if (vma->vm_ops || (vm_flags & VM_NO_THP))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001961 /* khugepaged not yet working on file or special mappings */
1962 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001963 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1964 hend = vma->vm_end & HPAGE_PMD_MASK;
1965 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001966 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001967 return 0;
1968}
1969
1970void __khugepaged_exit(struct mm_struct *mm)
1971{
1972 struct mm_slot *mm_slot;
1973 int free = 0;
1974
1975 spin_lock(&khugepaged_mm_lock);
1976 mm_slot = get_mm_slot(mm);
1977 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001978 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001979 list_del(&mm_slot->mm_node);
1980 free = 1;
1981 }
Chris Wrightd788e802011-07-25 17:12:14 -07001982 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001983
1984 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001985 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1986 free_mm_slot(mm_slot);
1987 mmdrop(mm);
1988 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001989 /*
1990 * This is required to serialize against
1991 * khugepaged_test_exit() (which is guaranteed to run
1992 * under mmap sem read mode). Stop here (after we
1993 * return all pagetables will be destroyed) until
1994 * khugepaged has finished working on the pagetables
1995 * under the mmap_sem.
1996 */
1997 down_write(&mm->mmap_sem);
1998 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07001999 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002000}
2001
2002static void release_pte_page(struct page *page)
2003{
2004 /* 0 stands for page_is_file_cache(page) == false */
2005 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2006 unlock_page(page);
2007 putback_lru_page(page);
2008}
2009
2010static void release_pte_pages(pte_t *pte, pte_t *_pte)
2011{
2012 while (--_pte >= pte) {
2013 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002014 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002015 release_pte_page(pte_page(pteval));
2016 }
2017}
2018
Andrea Arcangeliba761492011-01-13 15:46:58 -08002019static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2020 unsigned long address,
2021 pte_t *pte)
2022{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002023 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002024 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002025 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002026 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002027
Andrea Arcangeliba761492011-01-13 15:46:58 -08002028 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2029 _pte++, address += PAGE_SIZE) {
2030 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002031 if (pte_none(pteval) || (pte_present(pteval) &&
2032 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002033 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002034 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002035 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002036 } else {
2037 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002038 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002039 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002040 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002041 if (!pte_present(pteval)) {
2042 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002043 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002044 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002045 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002046 if (unlikely(!page)) {
2047 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002049 }
Bob Liu344aa352012-12-11 16:00:34 -08002050
Sasha Levin309381fea2014-01-23 15:52:54 -08002051 VM_BUG_ON_PAGE(PageCompound(page), page);
2052 VM_BUG_ON_PAGE(!PageAnon(page), page);
2053 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002054
Andrea Arcangeliba761492011-01-13 15:46:58 -08002055 /*
2056 * We can do it before isolate_lru_page because the
2057 * page can't be freed from under us. NOTE: PG_lock
2058 * is needed to serialize against split_huge_page
2059 * when invoked from the VM.
2060 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002061 if (!trylock_page(page)) {
2062 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002063 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002064 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002065
2066 /*
2067 * cannot use mapcount: can't collapse if there's a gup pin.
2068 * The page must only be referenced by the scanned process
2069 * and page swap cache.
2070 */
2071 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2072 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002073 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002074 goto out;
2075 }
2076 if (pte_write(pteval)) {
2077 writable = true;
2078 } else {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002079 if (PageSwapCache(page) &&
2080 !reuse_swap_page(page, NULL)) {
Ebru Akagunduz10359212015-02-11 15:28:28 -08002081 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002082 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002083 goto out;
2084 }
2085 /*
2086 * Page is not in the swap cache. It can be collapsed
2087 * into a THP.
2088 */
2089 }
2090
Andrea Arcangeliba761492011-01-13 15:46:58 -08002091 /*
2092 * Isolate the page to avoid collapsing an hugepage
2093 * currently in use by the VM.
2094 */
2095 if (isolate_lru_page(page)) {
2096 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002097 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002098 goto out;
2099 }
2100 /* 0 stands for page_is_file_cache(page) == false */
2101 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002102 VM_BUG_ON_PAGE(!PageLocked(page), page);
2103 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002104
2105 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002106 if (pte_young(pteval) ||
2107 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002108 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002109 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002110 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002111 if (likely(writable)) {
2112 if (likely(referenced)) {
2113 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002114 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002115 referenced, writable, result);
2116 return 1;
2117 }
2118 } else {
2119 result = SCAN_PAGE_RO;
2120 }
2121
Andrea Arcangeliba761492011-01-13 15:46:58 -08002122out:
Bob Liu344aa352012-12-11 16:00:34 -08002123 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002124 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002125 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002126 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002127}
2128
2129static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2130 struct vm_area_struct *vma,
2131 unsigned long address,
2132 spinlock_t *ptl)
2133{
2134 pte_t *_pte;
2135 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2136 pte_t pteval = *_pte;
2137 struct page *src_page;
2138
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002139 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002140 clear_user_highpage(page, address);
2141 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002142 if (is_zero_pfn(pte_pfn(pteval))) {
2143 /*
2144 * ptl mostly unnecessary.
2145 */
2146 spin_lock(ptl);
2147 /*
2148 * paravirt calls inside pte_clear here are
2149 * superfluous.
2150 */
2151 pte_clear(vma->vm_mm, address, _pte);
2152 spin_unlock(ptl);
2153 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002154 } else {
2155 src_page = pte_page(pteval);
2156 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002157 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002158 release_pte_page(src_page);
2159 /*
2160 * ptl mostly unnecessary, but preempt has to
2161 * be disabled to update the per-cpu stats
2162 * inside page_remove_rmap().
2163 */
2164 spin_lock(ptl);
2165 /*
2166 * paravirt calls inside pte_clear here are
2167 * superfluous.
2168 */
2169 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002170 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002171 spin_unlock(ptl);
2172 free_page_and_swap_cache(src_page);
2173 }
2174
2175 address += PAGE_SIZE;
2176 page++;
2177 }
2178}
2179
Xiao Guangrong26234f32012-10-08 16:29:51 -07002180static void khugepaged_alloc_sleep(void)
2181{
Petr Mladekbde43c62015-09-08 15:04:05 -07002182 DEFINE_WAIT(wait);
2183
2184 add_wait_queue(&khugepaged_wait, &wait);
2185 freezable_schedule_timeout_interruptible(
2186 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2187 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002188}
2189
Bob Liu9f1b8682013-11-12 15:07:37 -08002190static int khugepaged_node_load[MAX_NUMNODES];
2191
David Rientjes14a4e212014-08-06 16:07:29 -07002192static bool khugepaged_scan_abort(int nid)
2193{
2194 int i;
2195
2196 /*
2197 * If zone_reclaim_mode is disabled, then no extra effort is made to
2198 * allocate memory locally.
2199 */
2200 if (!zone_reclaim_mode)
2201 return false;
2202
2203 /* If there is a count for this node already, it must be acceptable */
2204 if (khugepaged_node_load[nid])
2205 return false;
2206
2207 for (i = 0; i < MAX_NUMNODES; i++) {
2208 if (!khugepaged_node_load[i])
2209 continue;
2210 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2211 return true;
2212 }
2213 return false;
2214}
2215
Xiao Guangrong26234f32012-10-08 16:29:51 -07002216#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002217static int khugepaged_find_target_node(void)
2218{
2219 static int last_khugepaged_target_node = NUMA_NO_NODE;
2220 int nid, target_node = 0, max_value = 0;
2221
2222 /* find first node with max normal pages hit */
2223 for (nid = 0; nid < MAX_NUMNODES; nid++)
2224 if (khugepaged_node_load[nid] > max_value) {
2225 max_value = khugepaged_node_load[nid];
2226 target_node = nid;
2227 }
2228
2229 /* do some balance if several nodes have the same hit record */
2230 if (target_node <= last_khugepaged_target_node)
2231 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2232 nid++)
2233 if (max_value == khugepaged_node_load[nid]) {
2234 target_node = nid;
2235 break;
2236 }
2237
2238 last_khugepaged_target_node = target_node;
2239 return target_node;
2240}
2241
Xiao Guangrong26234f32012-10-08 16:29:51 -07002242static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2243{
2244 if (IS_ERR(*hpage)) {
2245 if (!*wait)
2246 return false;
2247
2248 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002249 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002250 khugepaged_alloc_sleep();
2251 } else if (*hpage) {
2252 put_page(*hpage);
2253 *hpage = NULL;
2254 }
2255
2256 return true;
2257}
2258
Michal Hocko3b3636922015-04-15 16:13:29 -07002259static struct page *
2260khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002261 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002262{
Sasha Levin309381fea2014-01-23 15:52:54 -08002263 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002264
Xiao Guangrong26234f32012-10-08 16:29:51 -07002265 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002266 * Before allocating the hugepage, release the mmap_sem read lock.
2267 * The allocation can take potentially a long time if it involves
2268 * sync compaction, and we do not need to hold the mmap_sem during
2269 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002270 */
2271 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002272
Vlastimil Babka96db8002015-09-08 15:03:50 -07002273 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002274 if (unlikely(!*hpage)) {
2275 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2276 *hpage = ERR_PTR(-ENOMEM);
2277 return NULL;
2278 }
2279
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002280 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002281 count_vm_event(THP_COLLAPSE_ALLOC);
2282 return *hpage;
2283}
2284#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002285static int khugepaged_find_target_node(void)
2286{
2287 return 0;
2288}
2289
Mel Gorman444eb2a42016-03-17 14:19:23 -07002290static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002291{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002292 struct page *page;
2293
Mel Gorman444eb2a42016-03-17 14:19:23 -07002294 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2295 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002296 if (page)
2297 prep_transhuge_page(page);
2298 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002299}
2300
Xiao Guangrong26234f32012-10-08 16:29:51 -07002301static struct page *khugepaged_alloc_hugepage(bool *wait)
2302{
2303 struct page *hpage;
2304
2305 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002306 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002307 if (!hpage) {
2308 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2309 if (!*wait)
2310 return NULL;
2311
2312 *wait = false;
2313 khugepaged_alloc_sleep();
2314 } else
2315 count_vm_event(THP_COLLAPSE_ALLOC);
2316 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2317
2318 return hpage;
2319}
2320
2321static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2322{
2323 if (!*hpage)
2324 *hpage = khugepaged_alloc_hugepage(wait);
2325
2326 if (unlikely(!*hpage))
2327 return false;
2328
2329 return true;
2330}
2331
Michal Hocko3b3636922015-04-15 16:13:29 -07002332static struct page *
2333khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002334 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002335{
2336 up_read(&mm->mmap_sem);
2337 VM_BUG_ON(!*hpage);
Michal Hocko3b3636922015-04-15 16:13:29 -07002338
Xiao Guangrong26234f32012-10-08 16:29:51 -07002339 return *hpage;
2340}
2341#endif
2342
Bob Liufa475e52012-12-11 16:00:39 -08002343static bool hugepage_vma_check(struct vm_area_struct *vma)
2344{
2345 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2346 (vma->vm_flags & VM_NOHUGEPAGE))
2347 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002348 if (!vma->anon_vma || vma->vm_ops)
2349 return false;
2350 if (is_vma_temporary_stack(vma))
2351 return false;
Konstantin Khlebnikov3486b852016-04-28 16:18:32 -07002352 return !(vma->vm_flags & VM_NO_THP);
Bob Liufa475e52012-12-11 16:00:39 -08002353}
2354
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002355/*
Ebru Akagunduz72695862016-07-26 15:25:06 -07002356 * If mmap_sem temporarily dropped, revalidate vma
2357 * before taking mmap_sem.
2358 * Return 0 if succeeds, otherwise return none-zero
2359 * value (scan code).
2360 */
2361
2362static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address)
2363{
2364 struct vm_area_struct *vma;
2365 unsigned long hstart, hend;
2366
2367 if (unlikely(khugepaged_test_exit(mm)))
2368 return SCAN_ANY_PROCESS;
2369
2370 vma = find_vma(mm, address);
2371 if (!vma)
2372 return SCAN_VMA_NULL;
2373
2374 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2375 hend = vma->vm_end & HPAGE_PMD_MASK;
2376 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2377 return SCAN_ADDRESS_RANGE;
2378 if (!hugepage_vma_check(vma))
2379 return SCAN_VMA_CHECK;
2380 return 0;
2381}
2382
2383/*
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002384 * Bring missing pages in from swap, to complete THP collapse.
2385 * Only done if khugepaged_scan_pmd believes it is worthwhile.
2386 *
2387 * Called and returns without pte mapped or spinlocks held,
2388 * but with mmap_sem held to protect against vma changes.
2389 */
2390
Ebru Akagunduz72695862016-07-26 15:25:06 -07002391static bool __collapse_huge_page_swapin(struct mm_struct *mm,
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002392 struct vm_area_struct *vma,
2393 unsigned long address, pmd_t *pmd)
2394{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002395 pte_t pteval;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002396 int swapped_in = 0, ret = 0;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002397 struct fault_env fe = {
2398 .vma = vma,
2399 .address = address,
2400 .flags = FAULT_FLAG_ALLOW_RETRY,
2401 .pmd = pmd,
2402 };
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002403
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002404 fe.pte = pte_offset_map(pmd, address);
2405 for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE;
2406 fe.pte++, fe.address += PAGE_SIZE) {
2407 pteval = *fe.pte;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002408 if (!is_swap_pte(pteval))
2409 continue;
2410 swapped_in++;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002411 ret = do_swap_page(&fe, pteval);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002412 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
2413 if (ret & VM_FAULT_RETRY) {
2414 down_read(&mm->mmap_sem);
2415 /* vma is no longer available, don't continue to swapin */
2416 if (hugepage_vma_revalidate(mm, address))
2417 return false;
Kirill A. Shutemov1f52e672016-07-26 15:25:12 -07002418 /* check if the pmd is still valid */
2419 if (mm_find_pmd(mm, address) != pmd)
2420 return false;
Ebru Akagunduz72695862016-07-26 15:25:06 -07002421 }
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002422 if (ret & VM_FAULT_ERROR) {
2423 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 0);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002424 return false;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002425 }
2426 /* pte is unmapped now, we need to map it */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002427 fe.pte = pte_offset_map(pmd, fe.address);
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002428 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002429 fe.pte--;
2430 pte_unmap(fe.pte);
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002431 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 1);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002432 return true;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002433}
2434
Andrea Arcangeliba761492011-01-13 15:46:58 -08002435static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002436 unsigned long address,
2437 struct page **hpage,
2438 struct vm_area_struct *vma,
2439 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002440{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002441 pmd_t *pmd, _pmd;
2442 pte_t *pte;
2443 pgtable_t pgtable;
2444 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002445 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002446 int isolated = 0, result = 0;
Johannes Weiner00501b52014-08-08 14:19:20 -07002447 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002448 unsigned long mmun_start; /* For mmu_notifiers */
2449 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b3636922015-04-15 16:13:29 -07002450 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002451
2452 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002453
Michal Hocko3b3636922015-04-15 16:13:29 -07002454 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002455 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b3636922015-04-15 16:13:29 -07002456
Xiao Guangrong26234f32012-10-08 16:29:51 -07002457 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002458 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002459 if (!new_page) {
2460 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2461 goto out_nolock;
2462 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002463
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002464 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002465 result = SCAN_CGROUP_CHARGE_FAIL;
2466 goto out_nolock;
2467 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002468
Ebru Akagunduz72695862016-07-26 15:25:06 -07002469 down_read(&mm->mmap_sem);
2470 result = hugepage_vma_revalidate(mm, address);
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002471 if (result) {
2472 mem_cgroup_cancel_charge(new_page, memcg, true);
2473 up_read(&mm->mmap_sem);
2474 goto out_nolock;
2475 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002476
Bob Liu62190492012-12-11 16:00:37 -08002477 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002478 if (!pmd) {
2479 result = SCAN_PMD_NULL;
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002480 mem_cgroup_cancel_charge(new_page, memcg, true);
2481 up_read(&mm->mmap_sem);
2482 goto out_nolock;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002483 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484
Ebru Akagunduz72695862016-07-26 15:25:06 -07002485 /*
2486 * __collapse_huge_page_swapin always returns with mmap_sem locked.
2487 * If it fails, release mmap_sem and jump directly out.
2488 * Continuing to collapse causes inconsistency.
2489 */
2490 if (!__collapse_huge_page_swapin(mm, vma, address, pmd)) {
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002491 mem_cgroup_cancel_charge(new_page, memcg, true);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002492 up_read(&mm->mmap_sem);
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002493 goto out_nolock;
Ebru Akagunduz72695862016-07-26 15:25:06 -07002494 }
2495
2496 up_read(&mm->mmap_sem);
2497 /*
2498 * Prevent all access to pagetables with the exception of
2499 * gup_fast later handled by the ptep_clear_flush and the VM
2500 * handled by the anon_vma lock + PG_lock.
2501 */
2502 down_write(&mm->mmap_sem);
2503 result = hugepage_vma_revalidate(mm, address);
2504 if (result)
2505 goto out;
Kirill A. Shutemov1f52e672016-07-26 15:25:12 -07002506 /* check if the pmd is still valid */
2507 if (mm_find_pmd(mm, address) != pmd)
2508 goto out;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002509
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002510 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002511
2512 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002513 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002514
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002515 mmun_start = address;
2516 mmun_end = address + HPAGE_PMD_SIZE;
2517 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002518 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002519 /*
2520 * After this gup_fast can't run anymore. This also removes
2521 * any huge TLB entry from the CPU so we won't allow
2522 * huge and small TLB entries for the same virtual address
2523 * to avoid the risk of CPU bugs in that area.
2524 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002525 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002526 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002527 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002528
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002529 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002530 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002531 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002532
2533 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002534 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002535 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002536 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002537 /*
2538 * We can only use set_pmd_at when establishing
2539 * hugepmds and never for establishing regular pmds that
2540 * points to regular pagetables. Use pmd_populate for that
2541 */
2542 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002543 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002544 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002545 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002546 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002547 }
2548
2549 /*
2550 * All pages are isolated and locked so anon_vma rmap
2551 * can't run anymore.
2552 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002553 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002554
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002555 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002556 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002557 __SetPageUptodate(new_page);
2558 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002559
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002560 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2561 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002562
2563 /*
2564 * spin_lock() below is not the equivalent of smp_wmb(), so
2565 * this is needed to avoid the copy_huge_page writes to become
2566 * visible after the set_pmd_at() write.
2567 */
2568 smp_wmb();
2569
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002570 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002571 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002572 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002573 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002574 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002575 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002576 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002577 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002578 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579
2580 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002581
Andrea Arcangeliba761492011-01-13 15:46:58 -08002582 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002583 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002584out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002585 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002586out_nolock:
2587 trace_mm_collapse_huge_page(mm, isolated, result);
2588 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002589out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002590 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002591 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002592}
2593
2594static int khugepaged_scan_pmd(struct mm_struct *mm,
2595 struct vm_area_struct *vma,
2596 unsigned long address,
2597 struct page **hpage)
2598{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002599 pmd_t *pmd;
2600 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002601 int ret = 0, none_or_zero = 0, result = 0;
2602 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002603 unsigned long _address;
2604 spinlock_t *ptl;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002605 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002606 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607
2608 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2609
Bob Liu62190492012-12-11 16:00:37 -08002610 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002611 if (!pmd) {
2612 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002613 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002614 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002615
Bob Liu9f1b8682013-11-12 15:07:37 -08002616 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002617 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2618 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2619 _pte++, _address += PAGE_SIZE) {
2620 pte_t pteval = *_pte;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002621 if (is_swap_pte(pteval)) {
2622 if (++unmapped <= khugepaged_max_ptes_swap) {
2623 continue;
2624 } else {
2625 result = SCAN_EXCEED_SWAP_PTE;
2626 goto out_unmap;
2627 }
2628 }
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002629 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002630 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002631 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002632 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002633 } else {
2634 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002635 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002636 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002637 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002638 if (!pte_present(pteval)) {
2639 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002640 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002641 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002642 if (pte_write(pteval))
2643 writable = true;
2644
Andrea Arcangeliba761492011-01-13 15:46:58 -08002645 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002646 if (unlikely(!page)) {
2647 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002648 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002649 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002650
2651 /* TODO: teach khugepaged to collapse THP mapped with pte */
2652 if (PageCompound(page)) {
2653 result = SCAN_PAGE_COMPOUND;
2654 goto out_unmap;
2655 }
2656
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002657 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002658 * Record which node the original page is from and save this
2659 * information to khugepaged_node_load[].
2660 * Khupaged will allocate hugepage from the node has the max
2661 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002662 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002663 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002664 if (khugepaged_scan_abort(node)) {
2665 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002666 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002667 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002668 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002669 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002670 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002671 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002672 }
2673 if (PageLocked(page)) {
2674 result = SCAN_PAGE_LOCK;
2675 goto out_unmap;
2676 }
2677 if (!PageAnon(page)) {
2678 result = SCAN_PAGE_ANON;
2679 goto out_unmap;
2680 }
2681
Ebru Akagunduz10359212015-02-11 15:28:28 -08002682 /*
2683 * cannot use mapcount: can't collapse if there's a gup pin.
2684 * The page must only be referenced by the scanned process
2685 * and page swap cache.
2686 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002687 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2688 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002689 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002690 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002691 if (pte_young(pteval) ||
2692 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002693 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002694 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002695 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002696 if (writable) {
2697 if (referenced) {
2698 result = SCAN_SUCCEED;
2699 ret = 1;
2700 } else {
2701 result = SCAN_NO_REFERENCED_PAGE;
2702 }
2703 } else {
2704 result = SCAN_PAGE_RO;
2705 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002706out_unmap:
2707 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002708 if (ret) {
2709 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002710 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002711 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002712 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002713out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002714 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002715 none_or_zero, result, unmapped);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002716 return ret;
2717}
2718
2719static void collect_mm_slot(struct mm_slot *mm_slot)
2720{
2721 struct mm_struct *mm = mm_slot->mm;
2722
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002723 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002724
2725 if (khugepaged_test_exit(mm)) {
2726 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002727 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002728 list_del(&mm_slot->mm_node);
2729
2730 /*
2731 * Not strictly needed because the mm exited already.
2732 *
2733 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2734 */
2735
2736 /* khugepaged_mm_lock actually not necessary for the below */
2737 free_mm_slot(mm_slot);
2738 mmdrop(mm);
2739 }
2740}
2741
2742static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2743 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002744 __releases(&khugepaged_mm_lock)
2745 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002746{
2747 struct mm_slot *mm_slot;
2748 struct mm_struct *mm;
2749 struct vm_area_struct *vma;
2750 int progress = 0;
2751
2752 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002753 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002754
2755 if (khugepaged_scan.mm_slot)
2756 mm_slot = khugepaged_scan.mm_slot;
2757 else {
2758 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2759 struct mm_slot, mm_node);
2760 khugepaged_scan.address = 0;
2761 khugepaged_scan.mm_slot = mm_slot;
2762 }
2763 spin_unlock(&khugepaged_mm_lock);
2764
2765 mm = mm_slot->mm;
2766 down_read(&mm->mmap_sem);
2767 if (unlikely(khugepaged_test_exit(mm)))
2768 vma = NULL;
2769 else
2770 vma = find_vma(mm, khugepaged_scan.address);
2771
2772 progress++;
2773 for (; vma; vma = vma->vm_next) {
2774 unsigned long hstart, hend;
2775
2776 cond_resched();
2777 if (unlikely(khugepaged_test_exit(mm))) {
2778 progress++;
2779 break;
2780 }
Bob Liufa475e52012-12-11 16:00:39 -08002781 if (!hugepage_vma_check(vma)) {
2782skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002783 progress++;
2784 continue;
2785 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002786 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2787 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002788 if (hstart >= hend)
2789 goto skip;
2790 if (khugepaged_scan.address > hend)
2791 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002792 if (khugepaged_scan.address < hstart)
2793 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002794 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002795
2796 while (khugepaged_scan.address < hend) {
2797 int ret;
2798 cond_resched();
2799 if (unlikely(khugepaged_test_exit(mm)))
2800 goto breakouterloop;
2801
2802 VM_BUG_ON(khugepaged_scan.address < hstart ||
2803 khugepaged_scan.address + HPAGE_PMD_SIZE >
2804 hend);
2805 ret = khugepaged_scan_pmd(mm, vma,
2806 khugepaged_scan.address,
2807 hpage);
2808 /* move to next address */
2809 khugepaged_scan.address += HPAGE_PMD_SIZE;
2810 progress += HPAGE_PMD_NR;
2811 if (ret)
2812 /* we released mmap_sem so break loop */
2813 goto breakouterloop_mmap_sem;
2814 if (progress >= pages)
2815 goto breakouterloop;
2816 }
2817 }
2818breakouterloop:
2819 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2820breakouterloop_mmap_sem:
2821
2822 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002823 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002824 /*
2825 * Release the current mm_slot if this mm is about to die, or
2826 * if we scanned all vmas of this mm.
2827 */
2828 if (khugepaged_test_exit(mm) || !vma) {
2829 /*
2830 * Make sure that if mm_users is reaching zero while
2831 * khugepaged runs here, khugepaged_exit will find
2832 * mm_slot not pointing to the exiting mm.
2833 */
2834 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2835 khugepaged_scan.mm_slot = list_entry(
2836 mm_slot->mm_node.next,
2837 struct mm_slot, mm_node);
2838 khugepaged_scan.address = 0;
2839 } else {
2840 khugepaged_scan.mm_slot = NULL;
2841 khugepaged_full_scans++;
2842 }
2843
2844 collect_mm_slot(mm_slot);
2845 }
2846
2847 return progress;
2848}
2849
2850static int khugepaged_has_work(void)
2851{
2852 return !list_empty(&khugepaged_scan.mm_head) &&
2853 khugepaged_enabled();
2854}
2855
2856static int khugepaged_wait_event(void)
2857{
2858 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002859 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002860}
2861
Xiao Guangrongd5169042012-10-08 16:29:48 -07002862static void khugepaged_do_scan(void)
2863{
2864 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002865 unsigned int progress = 0, pass_through_head = 0;
2866 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002867 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002868
2869 barrier(); /* write khugepaged_pages_to_scan to local stack */
2870
2871 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002872 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002873 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002874
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002875 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002876
Jiri Kosinacd092412015-06-24 16:56:07 -07002877 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002878 break;
2879
Andrea Arcangeliba761492011-01-13 15:46:58 -08002880 spin_lock(&khugepaged_mm_lock);
2881 if (!khugepaged_scan.mm_slot)
2882 pass_through_head++;
2883 if (khugepaged_has_work() &&
2884 pass_through_head < 2)
2885 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002886 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002887 else
2888 progress = pages;
2889 spin_unlock(&khugepaged_mm_lock);
2890 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002891
Xiao Guangrongd5169042012-10-08 16:29:48 -07002892 if (!IS_ERR_OR_NULL(hpage))
2893 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002894}
2895
David Rientjesf0508972016-05-20 16:58:56 -07002896static bool khugepaged_should_wakeup(void)
2897{
2898 return kthread_should_stop() ||
2899 time_after_eq(jiffies, khugepaged_sleep_expire);
2900}
2901
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002902static void khugepaged_wait_work(void)
2903{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002904 if (khugepaged_has_work()) {
David Rientjesf0508972016-05-20 16:58:56 -07002905 const unsigned long scan_sleep_jiffies =
2906 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2907
2908 if (!scan_sleep_jiffies)
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002909 return;
2910
David Rientjesf0508972016-05-20 16:58:56 -07002911 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002912 wait_event_freezable_timeout(khugepaged_wait,
David Rientjesf0508972016-05-20 16:58:56 -07002913 khugepaged_should_wakeup(),
2914 scan_sleep_jiffies);
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002915 return;
2916 }
2917
2918 if (khugepaged_enabled())
2919 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2920}
2921
Andrea Arcangeliba761492011-01-13 15:46:58 -08002922static int khugepaged(void *none)
2923{
2924 struct mm_slot *mm_slot;
2925
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002926 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002927 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002928
Xiao Guangrongb7231782012-10-08 16:29:54 -07002929 while (!kthread_should_stop()) {
2930 khugepaged_do_scan();
2931 khugepaged_wait_work();
2932 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002933
2934 spin_lock(&khugepaged_mm_lock);
2935 mm_slot = khugepaged_scan.mm_slot;
2936 khugepaged_scan.mm_slot = NULL;
2937 if (mm_slot)
2938 collect_mm_slot(mm_slot);
2939 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002940 return 0;
2941}
2942
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002943static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2944 unsigned long haddr, pmd_t *pmd)
2945{
2946 struct mm_struct *mm = vma->vm_mm;
2947 pgtable_t pgtable;
2948 pmd_t _pmd;
2949 int i;
2950
2951 /* leave pmd empty until pte is filled */
2952 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2953
2954 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2955 pmd_populate(mm, &_pmd, pgtable);
2956
2957 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2958 pte_t *pte, entry;
2959 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2960 entry = pte_mkspecial(entry);
2961 pte = pte_offset_map(&_pmd, haddr);
2962 VM_BUG_ON(!pte_none(*pte));
2963 set_pte_at(mm, haddr, pte, entry);
2964 pte_unmap(pte);
2965 }
2966 smp_wmb(); /* make pte visible before pmd */
2967 pmd_populate(mm, pmd, pgtable);
2968 put_huge_zero_page();
2969}
2970
2971static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002972 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002973{
2974 struct mm_struct *mm = vma->vm_mm;
2975 struct page *page;
2976 pgtable_t pgtable;
2977 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002978 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002979 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002980 int i;
2981
2982 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2983 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2984 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002985 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002986
2987 count_vm_event(THP_SPLIT_PMD);
2988
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002989 if (!vma_is_anonymous(vma)) {
2990 _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002991 if (is_huge_zero_pmd(_pmd))
2992 put_huge_zero_page();
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002993 if (vma_is_dax(vma))
2994 return;
2995 page = pmd_page(_pmd);
2996 if (!PageReferenced(page) && pmd_young(_pmd))
2997 SetPageReferenced(page);
2998 page_remove_rmap(page, true);
2999 put_page(page);
3000 add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003001 return;
3002 } else if (is_huge_zero_pmd(*pmd)) {
3003 return __split_huge_zero_page_pmd(vma, haddr, pmd);
3004 }
3005
3006 page = pmd_page(*pmd);
3007 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003008 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003009 write = pmd_write(*pmd);
3010 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003011 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003012
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05303013 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003014 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
3015 pmd_populate(mm, &_pmd, pgtable);
3016
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003017 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003018 pte_t entry, *pte;
3019 /*
3020 * Note that NUMA hinting access restrictions are not
3021 * transferred to avoid any possibility of altering
3022 * permissions across VMAs.
3023 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08003024 if (freeze) {
3025 swp_entry_t swp_entry;
3026 swp_entry = make_migration_entry(page + i, write);
3027 entry = swp_entry_to_pte(swp_entry);
3028 } else {
3029 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003030 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08003031 if (!write)
3032 entry = pte_wrprotect(entry);
3033 if (!young)
3034 entry = pte_mkold(entry);
3035 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003036 if (dirty)
3037 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003038 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003039 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003040 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003041 atomic_inc(&page[i]._mapcount);
3042 pte_unmap(pte);
3043 }
3044
3045 /*
3046 * Set PG_double_map before dropping compound_mapcount to avoid
3047 * false-negative page_mapped().
3048 */
3049 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
3050 for (i = 0; i < HPAGE_PMD_NR; i++)
3051 atomic_inc(&page[i]._mapcount);
3052 }
3053
3054 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
3055 /* Last compound_mapcount is gone. */
3056 __dec_zone_page_state(page, NR_ANON_TRANSPARENT_HUGEPAGES);
3057 if (TestClearPageDoubleMap(page)) {
3058 /* No need in mapcount reference anymore */
3059 for (i = 0; i < HPAGE_PMD_NR; i++)
3060 atomic_dec(&page[i]._mapcount);
3061 }
3062 }
3063
3064 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003065 /*
3066 * Up to this point the pmd is present and huge and userland has the
3067 * whole access to the hugepage during the split (which happens in
3068 * place). If we overwrite the pmd with the not-huge version pointing
3069 * to the pte here (which of course we could if all CPUs were bug
3070 * free), userland could trigger a small page size TLB miss on the
3071 * small sized TLB while the hugepage TLB entry is still established in
3072 * the huge TLB. Some CPU doesn't like that.
3073 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
3074 * 383 on page 93. Intel should be safe but is also warns that it's
3075 * only safe if the permission and cache attributes of the two entries
3076 * loaded in the two TLB is identical (which should be the case here).
3077 * But it is generally safer to never allow small and huge TLB entries
3078 * for the same virtual address to be loaded simultaneously. So instead
3079 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
3080 * current pmd notpresent (atomically because here the pmd_trans_huge
3081 * and pmd_trans_splitting must remain set at all times on the pmd
3082 * until the split is complete for this pmd), then we flush the SMP TLB
3083 * and finally we write the non-huge version of the pmd entry with
3084 * pmd_populate.
3085 */
3086 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003087 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003088
3089 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003090 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003091 page_remove_rmap(page + i, false);
3092 put_page(page + i);
3093 }
3094 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003095}
3096
3097void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003098 unsigned long address, bool freeze, struct page *page)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003099{
3100 spinlock_t *ptl;
3101 struct mm_struct *mm = vma->vm_mm;
3102 unsigned long haddr = address & HPAGE_PMD_MASK;
3103
3104 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
3105 ptl = pmd_lock(mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003106
3107 /*
3108 * If caller asks to setup a migration entries, we need a page to check
3109 * pmd against. Otherwise we can end up replacing wrong page.
3110 */
3111 VM_BUG_ON(freeze && !page);
3112 if (page && page != pmd_page(*pmd))
3113 goto out;
3114
Dan Williams5c7fb562016-01-15 16:56:52 -08003115 if (pmd_trans_huge(*pmd)) {
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003116 page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003117 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003118 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08003119 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003120 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003121 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003122out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003123 spin_unlock(ptl);
3124 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
3125}
3126
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003127void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3128 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003129{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003130 pgd_t *pgd;
3131 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003132 pmd_t *pmd;
3133
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003134 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003135 if (!pgd_present(*pgd))
3136 return;
3137
3138 pud = pud_offset(pgd, address);
3139 if (!pud_present(*pud))
3140 return;
3141
3142 pmd = pmd_offset(pud, address);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003143
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003144 __split_huge_pmd(vma, pmd, address, freeze, page);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003145}
3146
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003147void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003148 unsigned long start,
3149 unsigned long end,
3150 long adjust_next)
3151{
3152 /*
3153 * If the new start address isn't hpage aligned and it could
3154 * previously contain an hugepage: check if we need to split
3155 * an huge pmd.
3156 */
3157 if (start & ~HPAGE_PMD_MASK &&
3158 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3159 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003160 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003161
3162 /*
3163 * If the new end address isn't hpage aligned and it could
3164 * previously contain an hugepage: check if we need to split
3165 * an huge pmd.
3166 */
3167 if (end & ~HPAGE_PMD_MASK &&
3168 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3169 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003170 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003171
3172 /*
3173 * If we're also updating the vma->vm_next->vm_start, if the new
3174 * vm_next->vm_start isn't page aligned and it could previously
3175 * contain an hugepage: check if we need to split an huge pmd.
3176 */
3177 if (adjust_next > 0) {
3178 struct vm_area_struct *next = vma->vm_next;
3179 unsigned long nstart = next->vm_start;
3180 nstart += adjust_next << PAGE_SHIFT;
3181 if (nstart & ~HPAGE_PMD_MASK &&
3182 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3183 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003184 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003185 }
3186}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003187
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003188static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003189{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003190 enum ttu_flags ttu_flags = TTU_MIGRATION | TTU_IGNORE_MLOCK |
3191 TTU_IGNORE_ACCESS | TTU_RMAP_LOCKED;
3192 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003193
3194 VM_BUG_ON_PAGE(!PageHead(page), page);
3195
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003196 /* We only need TTU_SPLIT_HUGE_PMD once */
3197 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3198 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3199 /* Cut short if the page is unmapped */
3200 if (page_count(page) == 1)
3201 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003202
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003203 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003204 }
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003205 VM_BUG_ON(ret);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003206}
3207
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003208static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003209{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003210 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003211
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003212 for (i = 0; i < HPAGE_PMD_NR; i++)
3213 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003214}
3215
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003216static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003217 struct lruvec *lruvec, struct list_head *list)
3218{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003219 struct page *page_tail = head + tail;
3220
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003221 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003222 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003223
3224 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003225 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003226 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003227 * tail_page. If we used atomic_set() below instead of atomic_inc(), we
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003228 * would then run atomic_set() concurrently with
3229 * get_page_unless_zero(), and atomic_set() is implemented in C not
3230 * using locked ops. spin_unlock on x86 sometime uses locked ops
3231 * because of PPro errata 66, 92, so unless somebody can guarantee
3232 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003233 * it's safer to use atomic_inc().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003234 */
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003235 page_ref_inc(page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003236
3237 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3238 page_tail->flags |= (head->flags &
3239 ((1L << PG_referenced) |
3240 (1L << PG_swapbacked) |
3241 (1L << PG_mlocked) |
3242 (1L << PG_uptodate) |
3243 (1L << PG_active) |
3244 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003245 (1L << PG_unevictable) |
3246 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003247
3248 /*
3249 * After clearing PageTail the gup refcount can be released.
3250 * Page flags also must be visible before we make the page non-compound.
3251 */
3252 smp_wmb();
3253
3254 clear_compound_head(page_tail);
3255
3256 if (page_is_young(head))
3257 set_page_young(page_tail);
3258 if (page_is_idle(head))
3259 set_page_idle(page_tail);
3260
3261 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003262 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003263 page_tail);
3264 page_tail->mapping = head->mapping;
3265
3266 page_tail->index = head->index + tail;
3267 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3268 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003269}
3270
3271static void __split_huge_page(struct page *page, struct list_head *list)
3272{
3273 struct page *head = compound_head(page);
3274 struct zone *zone = page_zone(head);
3275 struct lruvec *lruvec;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003276 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003277
3278 /* prevent PageLRU to go away from under us, and freeze lru stats */
3279 spin_lock_irq(&zone->lru_lock);
3280 lruvec = mem_cgroup_page_lruvec(head, zone);
3281
3282 /* complete memcg works before add pages to LRU */
3283 mem_cgroup_split_huge_fixup(head);
3284
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003285 for (i = HPAGE_PMD_NR - 1; i >= 1; i--)
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003286 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003287
3288 ClearPageCompound(head);
3289 spin_unlock_irq(&zone->lru_lock);
3290
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003291 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003292
3293 for (i = 0; i < HPAGE_PMD_NR; i++) {
3294 struct page *subpage = head + i;
3295 if (subpage == page)
3296 continue;
3297 unlock_page(subpage);
3298
3299 /*
3300 * Subpages may be freed if there wasn't any mapping
3301 * like if add_to_swap() is running on a lru page that
3302 * had its mapping zapped. And freeing these pages
3303 * requires taking the lru_lock so we do the put_page
3304 * of the tail pages after the split is complete.
3305 */
3306 put_page(subpage);
3307 }
3308}
3309
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003310int total_mapcount(struct page *page)
3311{
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003312 int i, compound, ret;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003313
3314 VM_BUG_ON_PAGE(PageTail(page), page);
3315
3316 if (likely(!PageCompound(page)))
3317 return atomic_read(&page->_mapcount) + 1;
3318
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003319 compound = compound_mapcount(page);
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003320 if (PageHuge(page))
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003321 return compound;
3322 ret = compound;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003323 for (i = 0; i < HPAGE_PMD_NR; i++)
3324 ret += atomic_read(&page[i]._mapcount) + 1;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003325 /* File pages has compound_mapcount included in _mapcount */
3326 if (!PageAnon(page))
3327 return ret - compound * HPAGE_PMD_NR;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003328 if (PageDoubleMap(page))
3329 ret -= HPAGE_PMD_NR;
3330 return ret;
3331}
3332
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003333/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003334 * This calculates accurately how many mappings a transparent hugepage
3335 * has (unlike page_mapcount() which isn't fully accurate). This full
3336 * accuracy is primarily needed to know if copy-on-write faults can
3337 * reuse the page and change the mapping to read-write instead of
3338 * copying them. At the same time this returns the total_mapcount too.
3339 *
3340 * The function returns the highest mapcount any one of the subpages
3341 * has. If the return value is one, even if different processes are
3342 * mapping different subpages of the transparent hugepage, they can
3343 * all reuse it, because each process is reusing a different subpage.
3344 *
3345 * The total_mapcount is instead counting all virtual mappings of the
3346 * subpages. If the total_mapcount is equal to "one", it tells the
3347 * caller all mappings belong to the same "mm" and in turn the
3348 * anon_vma of the transparent hugepage can become the vma->anon_vma
3349 * local one as no other process may be mapping any of the subpages.
3350 *
3351 * It would be more accurate to replace page_mapcount() with
3352 * page_trans_huge_mapcount(), however we only use
3353 * page_trans_huge_mapcount() in the copy-on-write faults where we
3354 * need full accuracy to avoid breaking page pinning, because
3355 * page_trans_huge_mapcount() is slower than page_mapcount().
3356 */
3357int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3358{
3359 int i, ret, _total_mapcount, mapcount;
3360
3361 /* hugetlbfs shouldn't call it */
3362 VM_BUG_ON_PAGE(PageHuge(page), page);
3363
3364 if (likely(!PageTransCompound(page))) {
3365 mapcount = atomic_read(&page->_mapcount) + 1;
3366 if (total_mapcount)
3367 *total_mapcount = mapcount;
3368 return mapcount;
3369 }
3370
3371 page = compound_head(page);
3372
3373 _total_mapcount = ret = 0;
3374 for (i = 0; i < HPAGE_PMD_NR; i++) {
3375 mapcount = atomic_read(&page[i]._mapcount) + 1;
3376 ret = max(ret, mapcount);
3377 _total_mapcount += mapcount;
3378 }
3379 if (PageDoubleMap(page)) {
3380 ret -= 1;
3381 _total_mapcount -= HPAGE_PMD_NR;
3382 }
3383 mapcount = compound_mapcount(page);
3384 ret += mapcount;
3385 _total_mapcount += mapcount;
3386 if (total_mapcount)
3387 *total_mapcount = _total_mapcount;
3388 return ret;
3389}
3390
3391/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003392 * This function splits huge page into normal pages. @page can point to any
3393 * subpage of huge page to split. Split doesn't change the position of @page.
3394 *
3395 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3396 * The huge page must be locked.
3397 *
3398 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3399 *
3400 * Both head page and tail pages will inherit mapping, flags, and so on from
3401 * the hugepage.
3402 *
3403 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3404 * they are not mapped.
3405 *
3406 * Returns 0 if the hugepage is split successfully.
3407 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3408 * us.
3409 */
3410int split_huge_page_to_list(struct page *page, struct list_head *list)
3411{
3412 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003413 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003414 struct anon_vma *anon_vma;
3415 int count, mapcount, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003416 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003417 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003418
3419 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
3420 VM_BUG_ON_PAGE(!PageAnon(page), page);
3421 VM_BUG_ON_PAGE(!PageLocked(page), page);
3422 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3423 VM_BUG_ON_PAGE(!PageCompound(page), page);
3424
3425 /*
3426 * The caller does not necessarily hold an mmap_sem that would prevent
3427 * the anon_vma disappearing so we first we take a reference to it
3428 * and then lock the anon_vma for write. This is similar to
3429 * page_lock_anon_vma_read except the write lock is taken to serialise
3430 * against parallel split or collapse operations.
3431 */
3432 anon_vma = page_get_anon_vma(head);
3433 if (!anon_vma) {
3434 ret = -EBUSY;
3435 goto out;
3436 }
3437 anon_vma_lock_write(anon_vma);
3438
3439 /*
3440 * Racy check if we can split the page, before freeze_page() will
3441 * split PMDs
3442 */
3443 if (total_mapcount(head) != page_count(head) - 1) {
3444 ret = -EBUSY;
3445 goto out_unlock;
3446 }
3447
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003448 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003449 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003450 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3451
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003452 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3453 if (mlocked)
3454 lru_add_drain();
3455
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003456 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003457 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003458 count = page_count(head);
3459 mapcount = total_mapcount(head);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003460 if (!mapcount && count == 1) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003461 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003462 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003463 list_del(page_deferred_list(head));
3464 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003465 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003466 __split_huge_page(page, list);
3467 ret = 0;
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003468 } else if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003469 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003470 pr_alert("total_mapcount: %u, page_count(): %u\n",
3471 mapcount, count);
3472 if (PageTail(page))
3473 dump_page(head, NULL);
Kirill A. Shutemovbd560862016-01-15 16:55:46 -08003474 dump_page(page, "total_mapcount(head) > 0");
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003475 BUG();
3476 } else {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003477 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003478 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003479 ret = -EBUSY;
3480 }
3481
3482out_unlock:
3483 anon_vma_unlock_write(anon_vma);
3484 put_anon_vma(anon_vma);
3485out:
3486 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3487 return ret;
3488}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003489
3490void free_transhuge_page(struct page *page)
3491{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003492 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003493 unsigned long flags;
3494
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003495 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003496 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003497 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003498 list_del(page_deferred_list(page));
3499 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003500 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003501 free_compound_page(page);
3502}
3503
3504void deferred_split_huge_page(struct page *page)
3505{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003506 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003507 unsigned long flags;
3508
3509 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3510
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003511 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003512 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003513 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003514 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3515 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003516 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003517 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003518}
3519
3520static unsigned long deferred_split_count(struct shrinker *shrink,
3521 struct shrink_control *sc)
3522{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003523 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003524 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003525}
3526
3527static unsigned long deferred_split_scan(struct shrinker *shrink,
3528 struct shrink_control *sc)
3529{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003530 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003531 unsigned long flags;
3532 LIST_HEAD(list), *pos, *next;
3533 struct page *page;
3534 int split = 0;
3535
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003536 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003537 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003538 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003539 page = list_entry((void *)pos, struct page, mapping);
3540 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003541 if (get_page_unless_zero(page)) {
3542 list_move(page_deferred_list(page), &list);
3543 } else {
3544 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003545 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003546 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003547 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003548 if (!--sc->nr_to_scan)
3549 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003550 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003551 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003552
3553 list_for_each_safe(pos, next, &list) {
3554 page = list_entry((void *)pos, struct page, mapping);
3555 lock_page(page);
3556 /* split_huge_page() removes page from list on success */
3557 if (!split_huge_page(page))
3558 split++;
3559 unlock_page(page);
3560 put_page(page);
3561 }
3562
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003563 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3564 list_splice_tail(&list, &pgdata->split_queue);
3565 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003566
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003567 /*
3568 * Stop shrinker if we didn't split any page, but the queue is empty.
3569 * This can happen if pages were freed under us.
3570 */
3571 if (!split && list_empty(&pgdata->split_queue))
3572 return SHRINK_STOP;
3573 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003574}
3575
3576static struct shrinker deferred_split_shrinker = {
3577 .count_objects = deferred_split_count,
3578 .scan_objects = deferred_split_scan,
3579 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003580 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003581};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003582
3583#ifdef CONFIG_DEBUG_FS
3584static int split_huge_pages_set(void *data, u64 val)
3585{
3586 struct zone *zone;
3587 struct page *page;
3588 unsigned long pfn, max_zone_pfn;
3589 unsigned long total = 0, split = 0;
3590
3591 if (val != 1)
3592 return -EINVAL;
3593
3594 for_each_populated_zone(zone) {
3595 max_zone_pfn = zone_end_pfn(zone);
3596 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3597 if (!pfn_valid(pfn))
3598 continue;
3599
3600 page = pfn_to_page(pfn);
3601 if (!get_page_unless_zero(page))
3602 continue;
3603
3604 if (zone != page_zone(page))
3605 goto next;
3606
3607 if (!PageHead(page) || !PageAnon(page) ||
3608 PageHuge(page))
3609 goto next;
3610
3611 total++;
3612 lock_page(page);
3613 if (!split_huge_page(page))
3614 split++;
3615 unlock_page(page);
3616next:
3617 put_page(page);
3618 }
3619 }
3620
Yang Shi145bdaa2016-05-05 16:22:00 -07003621 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003622
3623 return 0;
3624}
3625DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3626 "%llu\n");
3627
3628static int __init split_huge_pages_debugfs(void)
3629{
3630 void *ret;
3631
Yang Shi145bdaa2016-05-05 16:22:00 -07003632 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003633 &split_huge_pages_fops);
3634 if (!ret)
3635 pr_warn("Failed to create split_huge_pages in debugfs");
3636 return 0;
3637}
3638late_initcall(split_huge_pages_debugfs);
3639#endif