blob: 5eba97874ad5e879af054e2295cd1e1328dd4c0f [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. Shutemovbaa355f2016-07-26 15:25:51 -070033#include <linux/shmem_fs.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080034
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080035#include <asm/tlb.h>
36#include <asm/pgalloc.h>
37#include "internal.h"
38
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080039enum scan_result {
40 SCAN_FAIL,
41 SCAN_SUCCEED,
42 SCAN_PMD_NULL,
43 SCAN_EXCEED_NONE_PTE,
44 SCAN_PTE_NON_PRESENT,
45 SCAN_PAGE_RO,
46 SCAN_NO_REFERENCED_PAGE,
47 SCAN_PAGE_NULL,
48 SCAN_SCAN_ABORT,
49 SCAN_PAGE_COUNT,
50 SCAN_PAGE_LRU,
51 SCAN_PAGE_LOCK,
52 SCAN_PAGE_ANON,
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -080053 SCAN_PAGE_COMPOUND,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080054 SCAN_ANY_PROCESS,
55 SCAN_VMA_NULL,
56 SCAN_VMA_CHECK,
57 SCAN_ADDRESS_RANGE,
58 SCAN_SWAP_CACHE_PAGE,
59 SCAN_DEL_PAGE_LRU,
60 SCAN_ALLOC_HUGE_PAGE_FAIL,
Ebru Akagunduz70652f62016-07-26 15:24:59 -070061 SCAN_CGROUP_CHARGE_FAIL,
62 SCAN_EXCEED_SWAP_PTE
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -080063};
64
65#define CREATE_TRACE_POINTS
66#include <trace/events/huge_memory.h>
67
Andrea Arcangeliba761492011-01-13 15:46:58 -080068/*
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080069 * By default transparent hugepage support is disabled in order that avoid
70 * to risk increase the memory footprint of applications without a guaranteed
71 * benefit. When transparent hugepage support is enabled, is for all mappings,
72 * and khugepaged scans all mappings.
73 * Defrag is invoked by khugepaged hugepage allocations and by page faults
74 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080075 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080076unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080077#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080078 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080079#endif
80#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
81 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
82#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070083 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080084 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
85 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080086
87/* default scan 8*512 pte (or vmas) every 30 second */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +053088static unsigned int khugepaged_pages_to_scan __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -080089static unsigned int khugepaged_pages_collapsed;
90static unsigned int khugepaged_full_scans;
91static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
92/* during fragmentation poll the hugepage allocator once every minute */
93static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
David Rientjesf0508972016-05-20 16:58:56 -070094static unsigned long khugepaged_sleep_expire;
Andrea Arcangeliba761492011-01-13 15:46:58 -080095static struct task_struct *khugepaged_thread __read_mostly;
96static DEFINE_MUTEX(khugepaged_mutex);
97static DEFINE_SPINLOCK(khugepaged_mm_lock);
98static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
99/*
100 * default collapse hugepages if there is at least one pte mapped like
101 * it would have happened if the vma was large enough during page
102 * fault.
103 */
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530104static unsigned int khugepaged_max_ptes_none __read_mostly;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700105static unsigned int khugepaged_max_ptes_swap __read_mostly;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800106
107static int khugepaged(void *none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800108static int khugepaged_slab_init(void);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700109static void khugepaged_slab_exit(void);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800110
Sasha Levin43b5fbb2013-02-22 16:32:27 -0800111#define MM_SLOTS_HASH_BITS 10
112static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
113
Andrea Arcangeliba761492011-01-13 15:46:58 -0800114static struct kmem_cache *mm_slot_cache __read_mostly;
115
116/**
117 * struct mm_slot - hash lookup from mm to mm_slot
118 * @hash: hash collision list
119 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
120 * @mm: the mm that this information is valid for
121 */
122struct mm_slot {
123 struct hlist_node hash;
124 struct list_head mm_node;
125 struct mm_struct *mm;
126};
127
128/**
129 * struct khugepaged_scan - cursor for scanning
130 * @mm_head: the head of the mm list to scan
131 * @mm_slot: the current mm_slot we are scanning
132 * @address: the next address inside that to be scanned
133 *
134 * There is only the one khugepaged_scan instance of this cursor structure.
135 */
136struct khugepaged_scan {
137 struct list_head mm_head;
138 struct mm_slot *mm_slot;
139 unsigned long address;
H Hartley Sweeten2f1da642011-10-31 17:09:25 -0700140};
141static struct khugepaged_scan khugepaged_scan = {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800142 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
143};
144
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800145static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800146
Nicholas Krause2c0b80d2015-09-08 15:00:33 -0700147static void set_recommended_min_free_kbytes(void)
Andrea Arcangelif0005652011-01-13 15:47:04 -0800148{
149 struct zone *zone;
150 int nr_zones = 0;
151 unsigned long recommended_min;
Andrea Arcangelif0005652011-01-13 15:47:04 -0800152
Andrea Arcangelif0005652011-01-13 15:47:04 -0800153 for_each_populated_zone(zone)
154 nr_zones++;
155
Mel Gorman974a7862015-11-06 16:28:34 -0800156 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
Andrea Arcangelif0005652011-01-13 15:47:04 -0800157 recommended_min = pageblock_nr_pages * nr_zones * 2;
158
159 /*
160 * Make sure that on average at least two pageblocks are almost free
161 * of another type, one for a migratetype to fall back to and a
162 * second to avoid subsequent fallbacks of other types There are 3
163 * MIGRATE_TYPES we care about.
164 */
165 recommended_min += pageblock_nr_pages * nr_zones *
166 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
167
168 /* don't ever allow to reserve more than 5% of the lowmem */
169 recommended_min = min(recommended_min,
170 (unsigned long) nr_free_buffer_pages() / 20);
171 recommended_min <<= (PAGE_SHIFT-10);
172
Han Pingtian42aa83c2014-01-23 15:53:28 -0800173 if (recommended_min > min_free_kbytes) {
174 if (user_min_free_kbytes >= 0)
Joe Perches756a025f02016-03-17 14:19:47 -0700175 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
Han Pingtian42aa83c2014-01-23 15:53:28 -0800176 min_free_kbytes, recommended_min);
177
Andrea Arcangelif0005652011-01-13 15:47:04 -0800178 min_free_kbytes = recommended_min;
Han Pingtian42aa83c2014-01-23 15:53:28 -0800179 }
Andrea Arcangelif0005652011-01-13 15:47:04 -0800180 setup_per_zone_wmarks();
Andrea Arcangelif0005652011-01-13 15:47:04 -0800181}
Andrea Arcangelif0005652011-01-13 15:47:04 -0800182
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700183static int start_stop_khugepaged(void)
Andrea Arcangeliba761492011-01-13 15:46:58 -0800184{
185 int err = 0;
186 if (khugepaged_enabled()) {
Andrea Arcangeliba761492011-01-13 15:46:58 -0800187 if (!khugepaged_thread)
188 khugepaged_thread = kthread_run(khugepaged, NULL,
189 "khugepaged");
Viresh Kumar18e8e5c2015-08-12 15:59:46 +0530190 if (IS_ERR(khugepaged_thread)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700191 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
Andrea Arcangeliba761492011-01-13 15:46:58 -0800192 err = PTR_ERR(khugepaged_thread);
193 khugepaged_thread = NULL;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700194 goto fail;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800195 }
Xiao Guangrong911891a2012-10-08 16:29:41 -0700196
197 if (!list_empty(&khugepaged_scan.mm_head))
Andrea Arcangeliba761492011-01-13 15:46:58 -0800198 wake_up_interruptible(&khugepaged_wait);
Andrea Arcangelif0005652011-01-13 15:47:04 -0800199
200 set_recommended_min_free_kbytes();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700201 } else if (khugepaged_thread) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700202 kthread_stop(khugepaged_thread);
203 khugepaged_thread = NULL;
204 }
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700205fail:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800206 return err;
207}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800208
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800209static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -0800210struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -0800211
Matthew Wilcoxfc437042015-09-08 14:58:51 -0700212struct page *get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800213{
214 struct page *zero_page;
215retry:
216 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Jason Low4db0c3c2015-04-15 16:14:08 -0700217 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800218
219 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
220 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800221 if (!zero_page) {
222 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700223 return NULL;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800224 }
225 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800226 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700227 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800228 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700229 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800230 goto retry;
231 }
232
233 /* We take additional reference here. It will be put back by shrinker */
234 atomic_set(&huge_zero_refcount, 2);
235 preempt_enable();
Jason Low4db0c3c2015-04-15 16:14:08 -0700236 return READ_ONCE(huge_zero_page);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800237}
238
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -0700239void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800240{
241 /*
242 * Counter should never go to zero here. Only shrinker can put
243 * last reference.
244 */
245 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
246}
247
Glauber Costa48896462013-08-28 10:18:15 +1000248static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
249 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800250{
Glauber Costa48896462013-08-28 10:18:15 +1000251 /* we can free zero page only if last reference remains */
252 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
253}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800254
Glauber Costa48896462013-08-28 10:18:15 +1000255static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
256 struct shrink_control *sc)
257{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800258 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700259 struct page *zero_page = xchg(&huge_zero_page, NULL);
260 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700261 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000262 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800263 }
264
265 return 0;
266}
267
268static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000269 .count_objects = shrink_huge_zero_page_count,
270 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800271 .seeks = DEFAULT_SEEKS,
272};
273
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800274#ifdef CONFIG_SYSFS
Andrea Arcangeliba761492011-01-13 15:46:58 -0800275
Mel Gorman444eb2a42016-03-17 14:19:23 -0700276static ssize_t triple_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800277 struct kobj_attribute *attr,
278 const char *buf, size_t count,
279 enum transparent_hugepage_flag enabled,
Mel Gorman444eb2a42016-03-17 14:19:23 -0700280 enum transparent_hugepage_flag deferred,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800281 enum transparent_hugepage_flag req_madv)
282{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700283 if (!memcmp("defer", buf,
284 min(sizeof("defer")-1, count))) {
285 if (enabled == deferred)
286 return -EINVAL;
287 clear_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800288 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700289 set_bit(deferred, &transparent_hugepage_flags);
290 } else if (!memcmp("always", buf,
291 min(sizeof("always")-1, count))) {
292 clear_bit(deferred, &transparent_hugepage_flags);
293 clear_bit(req_madv, &transparent_hugepage_flags);
294 set_bit(enabled, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800295 } else if (!memcmp("madvise", buf,
296 min(sizeof("madvise")-1, count))) {
297 clear_bit(enabled, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700298 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800299 set_bit(req_madv, &transparent_hugepage_flags);
300 } else if (!memcmp("never", buf,
301 min(sizeof("never")-1, count))) {
302 clear_bit(enabled, &transparent_hugepage_flags);
303 clear_bit(req_madv, &transparent_hugepage_flags);
Mel Gorman444eb2a42016-03-17 14:19:23 -0700304 clear_bit(deferred, &transparent_hugepage_flags);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800305 } else
306 return -EINVAL;
307
308 return count;
309}
310
311static ssize_t enabled_show(struct kobject *kobj,
312 struct kobj_attribute *attr, char *buf)
313{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700314 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
315 return sprintf(buf, "[always] madvise never\n");
316 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags))
317 return sprintf(buf, "always [madvise] never\n");
318 else
319 return sprintf(buf, "always madvise [never]\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800320}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700321
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800322static ssize_t enabled_store(struct kobject *kobj,
323 struct kobj_attribute *attr,
324 const char *buf, size_t count)
325{
Andrea Arcangeliba761492011-01-13 15:46:58 -0800326 ssize_t ret;
327
Mel Gorman444eb2a42016-03-17 14:19:23 -0700328 ret = triple_flag_store(kobj, attr, buf, count,
329 TRANSPARENT_HUGEPAGE_FLAG,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800330 TRANSPARENT_HUGEPAGE_FLAG,
331 TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG);
332
333 if (ret > 0) {
Xiao Guangrong911891a2012-10-08 16:29:41 -0700334 int err;
335
336 mutex_lock(&khugepaged_mutex);
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700337 err = start_stop_khugepaged();
Xiao Guangrong911891a2012-10-08 16:29:41 -0700338 mutex_unlock(&khugepaged_mutex);
339
Andrea Arcangeliba761492011-01-13 15:46:58 -0800340 if (err)
341 ret = err;
342 }
343
344 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800345}
346static struct kobj_attribute enabled_attr =
347 __ATTR(enabled, 0644, enabled_show, enabled_store);
348
349static ssize_t single_flag_show(struct kobject *kobj,
350 struct kobj_attribute *attr, char *buf,
351 enum transparent_hugepage_flag flag)
352{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700353 return sprintf(buf, "%d\n",
354 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800355}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700356
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800357static ssize_t single_flag_store(struct kobject *kobj,
358 struct kobj_attribute *attr,
359 const char *buf, size_t count,
360 enum transparent_hugepage_flag flag)
361{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700362 unsigned long value;
363 int ret;
364
365 ret = kstrtoul(buf, 10, &value);
366 if (ret < 0)
367 return ret;
368 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800369 return -EINVAL;
370
Ben Hutchingse27e6152011-04-14 15:22:21 -0700371 if (value)
372 set_bit(flag, &transparent_hugepage_flags);
373 else
374 clear_bit(flag, &transparent_hugepage_flags);
375
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800376 return count;
377}
378
379/*
380 * Currently defrag only disables __GFP_NOWAIT for allocation. A blind
381 * __GFP_REPEAT is too aggressive, it's never worth swapping tons of
382 * memory just to allocate one more hugepage.
383 */
384static ssize_t defrag_show(struct kobject *kobj,
385 struct kobj_attribute *attr, char *buf)
386{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700387 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
388 return sprintf(buf, "[always] defer madvise never\n");
389 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
390 return sprintf(buf, "always [defer] madvise never\n");
391 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
392 return sprintf(buf, "always defer [madvise] never\n");
393 else
394 return sprintf(buf, "always defer madvise [never]\n");
395
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800396}
397static ssize_t defrag_store(struct kobject *kobj,
398 struct kobj_attribute *attr,
399 const char *buf, size_t count)
400{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700401 return triple_flag_store(kobj, attr, buf, count,
402 TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
403 TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800404 TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG);
405}
406static struct kobj_attribute defrag_attr =
407 __ATTR(defrag, 0644, defrag_show, defrag_store);
408
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800409static ssize_t use_zero_page_show(struct kobject *kobj,
410 struct kobj_attribute *attr, char *buf)
411{
412 return single_flag_show(kobj, attr, buf,
413 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
414}
415static ssize_t use_zero_page_store(struct kobject *kobj,
416 struct kobj_attribute *attr, const char *buf, size_t count)
417{
418 return single_flag_store(kobj, attr, buf, count,
419 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
420}
421static struct kobj_attribute use_zero_page_attr =
422 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800423#ifdef CONFIG_DEBUG_VM
424static ssize_t debug_cow_show(struct kobject *kobj,
425 struct kobj_attribute *attr, char *buf)
426{
427 return single_flag_show(kobj, attr, buf,
428 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
429}
430static ssize_t debug_cow_store(struct kobject *kobj,
431 struct kobj_attribute *attr,
432 const char *buf, size_t count)
433{
434 return single_flag_store(kobj, attr, buf, count,
435 TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG);
436}
437static struct kobj_attribute debug_cow_attr =
438 __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store);
439#endif /* CONFIG_DEBUG_VM */
440
441static struct attribute *hugepage_attr[] = {
442 &enabled_attr.attr,
443 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800444 &use_zero_page_attr.attr,
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700445#ifdef CONFIG_SHMEM
446 &shmem_enabled_attr.attr,
447#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800448#ifdef CONFIG_DEBUG_VM
449 &debug_cow_attr.attr,
450#endif
451 NULL,
452};
453
454static struct attribute_group hugepage_attr_group = {
455 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800456};
457
458static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
459 struct kobj_attribute *attr,
460 char *buf)
461{
462 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
463}
464
465static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
466 struct kobj_attribute *attr,
467 const char *buf, size_t count)
468{
469 unsigned long msecs;
470 int err;
471
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700472 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800473 if (err || msecs > UINT_MAX)
474 return -EINVAL;
475
476 khugepaged_scan_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700477 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800478 wake_up_interruptible(&khugepaged_wait);
479
480 return count;
481}
482static struct kobj_attribute scan_sleep_millisecs_attr =
483 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
484 scan_sleep_millisecs_store);
485
486static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
487 struct kobj_attribute *attr,
488 char *buf)
489{
490 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
491}
492
493static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
494 struct kobj_attribute *attr,
495 const char *buf, size_t count)
496{
497 unsigned long msecs;
498 int err;
499
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700500 err = kstrtoul(buf, 10, &msecs);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800501 if (err || msecs > UINT_MAX)
502 return -EINVAL;
503
504 khugepaged_alloc_sleep_millisecs = msecs;
David Rientjesf0508972016-05-20 16:58:56 -0700505 khugepaged_sleep_expire = 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800506 wake_up_interruptible(&khugepaged_wait);
507
508 return count;
509}
510static struct kobj_attribute alloc_sleep_millisecs_attr =
511 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
512 alloc_sleep_millisecs_store);
513
514static ssize_t pages_to_scan_show(struct kobject *kobj,
515 struct kobj_attribute *attr,
516 char *buf)
517{
518 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
519}
520static ssize_t pages_to_scan_store(struct kobject *kobj,
521 struct kobj_attribute *attr,
522 const char *buf, size_t count)
523{
524 int err;
525 unsigned long pages;
526
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700527 err = kstrtoul(buf, 10, &pages);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800528 if (err || !pages || pages > UINT_MAX)
529 return -EINVAL;
530
531 khugepaged_pages_to_scan = pages;
532
533 return count;
534}
535static struct kobj_attribute pages_to_scan_attr =
536 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
537 pages_to_scan_store);
538
539static ssize_t pages_collapsed_show(struct kobject *kobj,
540 struct kobj_attribute *attr,
541 char *buf)
542{
543 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
544}
545static struct kobj_attribute pages_collapsed_attr =
546 __ATTR_RO(pages_collapsed);
547
548static ssize_t full_scans_show(struct kobject *kobj,
549 struct kobj_attribute *attr,
550 char *buf)
551{
552 return sprintf(buf, "%u\n", khugepaged_full_scans);
553}
554static struct kobj_attribute full_scans_attr =
555 __ATTR_RO(full_scans);
556
557static ssize_t khugepaged_defrag_show(struct kobject *kobj,
558 struct kobj_attribute *attr, char *buf)
559{
560 return single_flag_show(kobj, attr, buf,
561 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
562}
563static ssize_t khugepaged_defrag_store(struct kobject *kobj,
564 struct kobj_attribute *attr,
565 const char *buf, size_t count)
566{
567 return single_flag_store(kobj, attr, buf, count,
568 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
569}
570static struct kobj_attribute khugepaged_defrag_attr =
571 __ATTR(defrag, 0644, khugepaged_defrag_show,
572 khugepaged_defrag_store);
573
574/*
575 * max_ptes_none controls if khugepaged should collapse hugepages over
576 * any unmapped ptes in turn potentially increasing the memory
577 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
578 * reduce the available free memory in the system as it
579 * runs. Increasing max_ptes_none will instead potentially reduce the
580 * free memory in the system during the khugepaged scan.
581 */
582static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
583 struct kobj_attribute *attr,
584 char *buf)
585{
586 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
587}
588static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
589 struct kobj_attribute *attr,
590 const char *buf, size_t count)
591{
592 int err;
593 unsigned long max_ptes_none;
594
Jingoo Han3dbb95f2013-09-11 14:20:25 -0700595 err = kstrtoul(buf, 10, &max_ptes_none);
Andrea Arcangeliba761492011-01-13 15:46:58 -0800596 if (err || max_ptes_none > HPAGE_PMD_NR-1)
597 return -EINVAL;
598
599 khugepaged_max_ptes_none = max_ptes_none;
600
601 return count;
602}
603static struct kobj_attribute khugepaged_max_ptes_none_attr =
604 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
605 khugepaged_max_ptes_none_store);
606
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700607static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
608 struct kobj_attribute *attr,
609 char *buf)
610{
611 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
612}
613
614static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
615 struct kobj_attribute *attr,
616 const char *buf, size_t count)
617{
618 int err;
619 unsigned long max_ptes_swap;
620
621 err = kstrtoul(buf, 10, &max_ptes_swap);
622 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
623 return -EINVAL;
624
625 khugepaged_max_ptes_swap = max_ptes_swap;
626
627 return count;
628}
629
630static struct kobj_attribute khugepaged_max_ptes_swap_attr =
631 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
632 khugepaged_max_ptes_swap_store);
633
Andrea Arcangeliba761492011-01-13 15:46:58 -0800634static struct attribute *khugepaged_attr[] = {
635 &khugepaged_defrag_attr.attr,
636 &khugepaged_max_ptes_none_attr.attr,
637 &pages_to_scan_attr.attr,
638 &pages_collapsed_attr.attr,
639 &full_scans_attr.attr,
640 &scan_sleep_millisecs_attr.attr,
641 &alloc_sleep_millisecs_attr.attr,
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700642 &khugepaged_max_ptes_swap_attr.attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800643 NULL,
644};
645
646static struct attribute_group khugepaged_attr_group = {
647 .attrs = khugepaged_attr,
648 .name = "khugepaged",
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800649};
Shaohua Li569e5592012-01-12 17:19:11 -0800650
651static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
652{
653 int err;
654
655 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
656 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700657 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800658 return -ENOMEM;
659 }
660
661 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
662 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700663 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800664 goto delete_obj;
665 }
666
667 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
668 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700669 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800670 goto remove_hp_group;
671 }
672
673 return 0;
674
675remove_hp_group:
676 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
677delete_obj:
678 kobject_put(*hugepage_kobj);
679 return err;
680}
681
682static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
683{
684 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
685 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
686 kobject_put(hugepage_kobj);
687}
688#else
689static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
690{
691 return 0;
692}
693
694static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
695{
696}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800697#endif /* CONFIG_SYSFS */
698
699static int __init hugepage_init(void)
700{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800701 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800702 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800703
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800704 if (!has_transparent_hugepage()) {
705 transparent_hugepage_flags = 0;
Shaohua Li569e5592012-01-12 17:19:11 -0800706 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800707 }
708
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530709 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
710 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
Ebru Akagunduz70652f62016-07-26 15:24:59 -0700711 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530712 /*
713 * hugepages can't be allocated by the buddy allocator
714 */
715 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
716 /*
717 * we use page->mapping and page->index in second tail page
718 * as list_head: assuming THP order >= 2
719 */
720 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
721
Shaohua Li569e5592012-01-12 17:19:11 -0800722 err = hugepage_init_sysfs(&hugepage_kobj);
723 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700724 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800725
726 err = khugepaged_slab_init();
727 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700728 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800729
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700730 err = register_shrinker(&huge_zero_page_shrinker);
731 if (err)
732 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800733 err = register_shrinker(&deferred_split_shrinker);
734 if (err)
735 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800736
Rik van Riel97562cd2011-01-13 15:47:12 -0800737 /*
738 * By default disable transparent hugepages on smaller systems,
739 * where the extra memory used could hurt more than TLB overhead
740 * is likely to save. The admin can still enable it through /sys.
741 */
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700742 if (totalram_pages < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800743 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700744 return 0;
745 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800746
Kirill A. Shutemov79553da2015-04-15 16:14:56 -0700747 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700748 if (err)
749 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800750
Shaohua Li569e5592012-01-12 17:19:11 -0800751 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700752err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800753 unregister_shrinker(&deferred_split_shrinker);
754err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700755 unregister_shrinker(&huge_zero_page_shrinker);
756err_hzp_shrinker:
757 khugepaged_slab_exit();
758err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800759 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700760err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800761 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800762}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800763subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800764
765static int __init setup_transparent_hugepage(char *str)
766{
767 int ret = 0;
768 if (!str)
769 goto out;
770 if (!strcmp(str, "always")) {
771 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
772 &transparent_hugepage_flags);
773 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
774 &transparent_hugepage_flags);
775 ret = 1;
776 } else if (!strcmp(str, "madvise")) {
777 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
778 &transparent_hugepage_flags);
779 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
780 &transparent_hugepage_flags);
781 ret = 1;
782 } else if (!strcmp(str, "never")) {
783 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
784 &transparent_hugepage_flags);
785 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
786 &transparent_hugepage_flags);
787 ret = 1;
788 }
789out:
790 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700791 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800792 return ret;
793}
794__setup("transparent_hugepage=", setup_transparent_hugepage);
795
Mel Gormanb32967f2012-11-19 12:35:47 +0000796pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800797{
798 if (likely(vma->vm_flags & VM_WRITE))
799 pmd = pmd_mkwrite(pmd);
800 return pmd;
801}
802
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800803static inline struct list_head *page_deferred_list(struct page *page)
804{
805 /*
806 * ->lru in the tail pages is occupied by compound_head.
807 * Let's use ->mapping + ->index in the second tail page as list_head.
808 */
809 return (struct list_head *)&page[2].mapping;
810}
811
812void prep_transhuge_page(struct page *page)
813{
814 /*
815 * we use page->mapping and page->indexlru in second tail page
816 * as list_head: assuming THP order >= 2
817 */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800818
819 INIT_LIST_HEAD(page_deferred_list(page));
820 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
821}
822
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700823static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page,
824 gfp_t gfp)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800825{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700826 struct vm_area_struct *vma = fe->vma;
Johannes Weiner00501b52014-08-08 14:19:20 -0700827 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800828 pgtable_t pgtable;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700829 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800830
Sasha Levin309381fea2014-01-23 15:52:54 -0800831 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700832
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700833 if (mem_cgroup_try_charge(page, vma->vm_mm, gfp, &memcg, true)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700834 put_page(page);
835 count_vm_event(THP_FAULT_FALLBACK);
836 return VM_FAULT_FALLBACK;
837 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800838
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700839 pgtable = pte_alloc_one(vma->vm_mm, haddr);
Johannes Weiner00501b52014-08-08 14:19:20 -0700840 if (unlikely(!pgtable)) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800841 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700842 put_page(page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700843 return VM_FAULT_OOM;
844 }
845
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800846 clear_huge_page(page, haddr, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700847 /*
848 * The memory barrier inside __SetPageUptodate makes sure that
849 * clear_huge_page writes become visible before the set_pmd_at()
850 * write.
851 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800852 __SetPageUptodate(page);
853
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700854 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
855 if (unlikely(!pmd_none(*fe->pmd))) {
856 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800857 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800858 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700859 pte_free(vma->vm_mm, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800860 } else {
861 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700862
863 /* Deliver the page fault to userland */
864 if (userfaultfd_missing(vma)) {
865 int ret;
866
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700867 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800868 mem_cgroup_cancel_charge(page, memcg, true);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700869 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700870 pte_free(vma->vm_mm, pgtable);
871 ret = handle_userfault(fe, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700872 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
873 return ret;
874 }
875
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700876 entry = mk_huge_pmd(page, vma->vm_page_prot);
877 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800878 page_add_new_anon_rmap(page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -0800879 mem_cgroup_commit_charge(page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -0700880 lru_cache_add_active_or_unevictable(page, vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700881 pgtable_trans_huge_deposit(vma->vm_mm, fe->pmd, pgtable);
882 set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
883 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
884 atomic_long_inc(&vma->vm_mm->nr_ptes);
885 spin_unlock(fe->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700886 count_vm_event(THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800887 }
888
David Rientjesaa2e8782012-05-29 15:06:17 -0700889 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800890}
891
Mel Gorman444eb2a42016-03-17 14:19:23 -0700892/*
893 * If THP is set to always then directly reclaim/compact as necessary
894 * If set to defer then do no reclaim and defer to khugepaged
895 * If set to madvise and the VMA is flagged then directly reclaim/compact
896 */
897static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800898{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700899 gfp_t reclaim_flags = 0;
900
901 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) &&
902 (vma->vm_flags & VM_HUGEPAGE))
903 reclaim_flags = __GFP_DIRECT_RECLAIM;
904 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
905 reclaim_flags = __GFP_KSWAPD_RECLAIM;
906 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
907 reclaim_flags = __GFP_DIRECT_RECLAIM;
908
909 return GFP_TRANSHUGE | reclaim_flags;
910}
911
912/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
913static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
914{
915 return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800916}
917
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800918/* Caller must hold page table lock. */
Kirill A. Shutemovd295e342015-09-08 14:59:34 -0700919static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800920 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700921 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800922{
923 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700924 if (!pmd_none(*pmd))
925 return false;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700926 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800927 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800928 if (pgtable)
929 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800930 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemove1f56c82013-11-14 14:30:48 -0800931 atomic_long_inc(&mm->nr_ptes);
Andrew Morton7c414162015-09-08 14:58:43 -0700932 return true;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800933}
934
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700935int do_huge_pmd_anonymous_page(struct fault_env *fe)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800936{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700937 struct vm_area_struct *vma = fe->vma;
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800938 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800939 struct page *page;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700940 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800941
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700942 if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700943 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700944 if (unlikely(anon_vma_prepare(vma)))
945 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700946 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700947 return VM_FAULT_OOM;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700948 if (!(fe->flags & FAULT_FLAG_WRITE) &&
949 !mm_forbids_zeropage(vma->vm_mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700950 transparent_hugepage_use_zero_page()) {
951 pgtable_t pgtable;
952 struct page *zero_page;
953 bool set;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700954 int ret;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700955 pgtable = pte_alloc_one(vma->vm_mm, haddr);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700956 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800957 return VM_FAULT_OOM;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700958 zero_page = get_huge_zero_page();
959 if (unlikely(!zero_page)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700960 pte_free(vma->vm_mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700961 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700962 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700963 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700964 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700965 ret = 0;
966 set = false;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700967 if (pmd_none(*fe->pmd)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700968 if (userfaultfd_missing(vma)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700969 spin_unlock(fe->ptl);
970 ret = handle_userfault(fe, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700971 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
972 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700973 set_huge_zero_page(pgtable, vma->vm_mm, vma,
974 haddr, fe->pmd, zero_page);
975 spin_unlock(fe->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700976 set = true;
977 }
978 } else
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700979 spin_unlock(fe->ptl);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700980 if (!set) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700981 pte_free(vma->vm_mm, pgtable);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700982 put_huge_zero_page();
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -0800983 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700984 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800985 }
Mel Gorman444eb2a42016-03-17 14:19:23 -0700986 gfp = alloc_hugepage_direct_gfpmask(vma);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800987 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700988 if (unlikely(!page)) {
989 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700990 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700991 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800992 prep_transhuge_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700993 return __do_huge_pmd_anonymous_page(fe, page, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800994}
995
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700996static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -0800997 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700998{
999 struct mm_struct *mm = vma->vm_mm;
1000 pmd_t entry;
1001 spinlock_t *ptl;
1002
1003 ptl = pmd_lock(mm, pmd);
Dan Williamsf25748e32016-01-15 16:56:43 -08001004 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
1005 if (pfn_t_devmap(pfn))
1006 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -08001007 if (write) {
1008 entry = pmd_mkyoung(pmd_mkdirty(entry));
1009 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001010 }
Ross Zwisler01871e52016-01-15 16:56:02 -08001011 set_pmd_at(mm, addr, pmd, entry);
1012 update_mmu_cache_pmd(vma, addr, pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001013 spin_unlock(ptl);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001014}
1015
1016int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Dan Williamsf25748e32016-01-15 16:56:43 -08001017 pmd_t *pmd, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001018{
1019 pgprot_t pgprot = vma->vm_page_prot;
1020 /*
1021 * If we had pmd_special, we could avoid all these restrictions,
1022 * but we need to be consistent with PTEs and architectures that
1023 * can't support a 'special' bit.
1024 */
1025 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
1026 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1027 (VM_PFNMAP|VM_MIXEDMAP));
1028 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Dan Williamsf25748e32016-01-15 16:56:43 -08001029 BUG_ON(!pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001030
1031 if (addr < vma->vm_start || addr >= vma->vm_end)
1032 return VM_FAULT_SIGBUS;
1033 if (track_pfn_insert(vma, &pgprot, pfn))
1034 return VM_FAULT_SIGBUS;
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -07001035 insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write);
1036 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001037}
Dan Williamsdee41072016-05-14 12:20:44 -07001038EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -07001039
Dan Williams3565fce2016-01-15 16:56:55 -08001040static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
1041 pmd_t *pmd)
1042{
1043 pmd_t _pmd;
1044
1045 /*
1046 * We should set the dirty bit only for FOLL_WRITE but for now
1047 * the dirty bit in the pmd is meaningless. And if the dirty
1048 * bit will become meaningful and we'll only set it with
1049 * FOLL_WRITE, an atomic set_bit will be required on the pmd to
1050 * set the young bit, instead of the current set_pmd_at.
1051 */
1052 _pmd = pmd_mkyoung(pmd_mkdirty(*pmd));
1053 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
1054 pmd, _pmd, 1))
1055 update_mmu_cache_pmd(vma, addr, pmd);
1056}
1057
1058struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
1059 pmd_t *pmd, int flags)
1060{
1061 unsigned long pfn = pmd_pfn(*pmd);
1062 struct mm_struct *mm = vma->vm_mm;
1063 struct dev_pagemap *pgmap;
1064 struct page *page;
1065
1066 assert_spin_locked(pmd_lockptr(mm, pmd));
1067
1068 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1069 return NULL;
1070
1071 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1072 /* pass */;
1073 else
1074 return NULL;
1075
1076 if (flags & FOLL_TOUCH)
1077 touch_pmd(vma, addr, pmd);
1078
1079 /*
1080 * device mapped pages can only be returned if the
1081 * caller will manage the page reference count.
1082 */
1083 if (!(flags & FOLL_GET))
1084 return ERR_PTR(-EEXIST);
1085
1086 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
1087 pgmap = get_dev_pagemap(pfn, NULL);
1088 if (!pgmap)
1089 return ERR_PTR(-EFAULT);
1090 page = pfn_to_page(pfn);
1091 get_page(page);
1092 put_dev_pagemap(pgmap);
1093
1094 return page;
1095}
1096
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001097int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1098 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1099 struct vm_area_struct *vma)
1100{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001101 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001102 struct page *src_page;
1103 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001104 pgtable_t pgtable = NULL;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001105 int ret = -ENOMEM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001106
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001107 /* Skip if can be re-fill on fault */
1108 if (!vma_is_anonymous(vma))
1109 return 0;
1110
1111 pgtable = pte_alloc_one(dst_mm, addr);
1112 if (unlikely(!pgtable))
1113 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001114
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001115 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1116 src_ptl = pmd_lockptr(src_mm, src_pmd);
1117 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001118
1119 ret = -EAGAIN;
1120 pmd = *src_pmd;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001121 if (unlikely(!pmd_trans_huge(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001122 pte_free(dst_mm, pgtable);
1123 goto out_unlock;
1124 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001125 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001126 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001127 * under splitting since we don't split the page itself, only pmd to
1128 * a page table.
1129 */
1130 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001131 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001132 /*
1133 * get_huge_zero_page() will never allocate a new page here,
1134 * since we already have a zero page to copy. It just takes a
1135 * reference.
1136 */
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001137 zero_page = get_huge_zero_page();
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001138 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001139 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001140 ret = 0;
1141 goto out_unlock;
1142 }
Mel Gormande466bd2013-12-18 17:08:42 -08001143
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001144 src_page = pmd_page(pmd);
1145 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
1146 get_page(src_page);
1147 page_dup_rmap(src_page, true);
1148 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
1149 atomic_long_inc(&dst_mm->nr_ptes);
1150 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001151
1152 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1153 pmd = pmd_mkold(pmd_wrprotect(pmd));
1154 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001155
1156 ret = 0;
1157out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001158 spin_unlock(src_ptl);
1159 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001160out:
1161 return ret;
1162}
1163
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001164void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd)
Will Deacona1dd4502012-12-11 16:01:27 -08001165{
1166 pmd_t entry;
1167 unsigned long haddr;
1168
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001169 fe->ptl = pmd_lock(fe->vma->vm_mm, fe->pmd);
1170 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Will Deacona1dd4502012-12-11 16:01:27 -08001171 goto unlock;
1172
1173 entry = pmd_mkyoung(orig_pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001174 haddr = fe->address & HPAGE_PMD_MASK;
1175 if (pmdp_set_access_flags(fe->vma, haddr, fe->pmd, entry,
1176 fe->flags & FAULT_FLAG_WRITE))
1177 update_mmu_cache_pmd(fe->vma, fe->address, fe->pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001178
1179unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001180 spin_unlock(fe->ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001181}
1182
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001183static int do_huge_pmd_wp_page_fallback(struct fault_env *fe, pmd_t orig_pmd,
1184 struct page *page)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001186 struct vm_area_struct *vma = fe->vma;
1187 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Johannes Weiner00501b52014-08-08 14:19:20 -07001188 struct mem_cgroup *memcg;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001189 pgtable_t pgtable;
1190 pmd_t _pmd;
1191 int ret = 0, i;
1192 struct page **pages;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001193 unsigned long mmun_start; /* For mmu_notifiers */
1194 unsigned long mmun_end; /* For mmu_notifiers */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001195
1196 pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR,
1197 GFP_KERNEL);
1198 if (unlikely(!pages)) {
1199 ret |= VM_FAULT_OOM;
1200 goto out;
1201 }
1202
1203 for (i = 0; i < HPAGE_PMD_NR; i++) {
Andi Kleencc5d4622011-03-22 16:33:13 -07001204 pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE |
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001205 __GFP_OTHER_NODE, vma,
1206 fe->address, page_to_nid(page));
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001207 if (unlikely(!pages[i] ||
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001208 mem_cgroup_try_charge(pages[i], vma->vm_mm,
1209 GFP_KERNEL, &memcg, false))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001210 if (pages[i])
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001211 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001212 while (--i >= 0) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001213 memcg = (void *)page_private(pages[i]);
1214 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001215 mem_cgroup_cancel_charge(pages[i], memcg,
1216 false);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001217 put_page(pages[i]);
1218 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001219 kfree(pages);
1220 ret |= VM_FAULT_OOM;
1221 goto out;
1222 }
Johannes Weiner00501b52014-08-08 14:19:20 -07001223 set_page_private(pages[i], (unsigned long)memcg);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001224 }
1225
1226 for (i = 0; i < HPAGE_PMD_NR; i++) {
1227 copy_user_highpage(pages[i], page + i,
Hillf Danton0089e482011-10-31 17:09:38 -07001228 haddr + PAGE_SIZE * i, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001229 __SetPageUptodate(pages[i]);
1230 cond_resched();
1231 }
1232
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001233 mmun_start = haddr;
1234 mmun_end = haddr + HPAGE_PMD_SIZE;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001235 mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001236
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001237 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
1238 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001239 goto out_free_pages;
Sasha Levin309381fea2014-01-23 15:52:54 -08001240 VM_BUG_ON_PAGE(!PageHead(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001241
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001242 pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001243 /* leave pmd empty until pte is filled */
1244
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001245 pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, fe->pmd);
1246 pmd_populate(vma->vm_mm, &_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001247
1248 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001249 pte_t entry;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001250 entry = mk_pte(pages[i], vma->vm_page_prot);
1251 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
Johannes Weiner00501b52014-08-08 14:19:20 -07001252 memcg = (void *)page_private(pages[i]);
1253 set_page_private(pages[i], 0);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001254 page_add_new_anon_rmap(pages[i], fe->vma, haddr, false);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001255 mem_cgroup_commit_charge(pages[i], memcg, false, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001256 lru_cache_add_active_or_unevictable(pages[i], vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001257 fe->pte = pte_offset_map(&_pmd, haddr);
1258 VM_BUG_ON(!pte_none(*fe->pte));
1259 set_pte_at(vma->vm_mm, haddr, fe->pte, entry);
1260 pte_unmap(fe->pte);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001261 }
1262 kfree(pages);
1263
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001264 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001265 pmd_populate(vma->vm_mm, fe->pmd, pgtable);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001266 page_remove_rmap(page, true);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001267 spin_unlock(fe->ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001268
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001269 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001270
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001271 ret |= VM_FAULT_WRITE;
1272 put_page(page);
1273
1274out:
1275 return ret;
1276
1277out_free_pages:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001278 spin_unlock(fe->ptl);
1279 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001280 for (i = 0; i < HPAGE_PMD_NR; i++) {
Johannes Weiner00501b52014-08-08 14:19:20 -07001281 memcg = (void *)page_private(pages[i]);
1282 set_page_private(pages[i], 0);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001283 mem_cgroup_cancel_charge(pages[i], memcg, false);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001284 put_page(pages[i]);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001285 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001286 kfree(pages);
1287 goto out;
1288}
1289
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001290int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001291{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001292 struct vm_area_struct *vma = fe->vma;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001293 struct page *page = NULL, *new_page;
Johannes Weiner00501b52014-08-08 14:19:20 -07001294 struct mem_cgroup *memcg;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001295 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001296 unsigned long mmun_start; /* For mmu_notifiers */
1297 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07001298 gfp_t huge_gfp; /* for allocation and charge */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001299 int ret = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001300
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001301 fe->ptl = pmd_lockptr(vma->vm_mm, fe->pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001302 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001303 if (is_huge_zero_pmd(orig_pmd))
1304 goto alloc;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001305 spin_lock(fe->ptl);
1306 if (unlikely(!pmd_same(*fe->pmd, orig_pmd)))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001307 goto out_unlock;
1308
1309 page = pmd_page(orig_pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001310 VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page);
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001311 /*
1312 * We can only reuse the page if nobody else maps the huge page or it's
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001313 * part.
Kirill A. Shutemov1f25fe22016-01-15 16:52:24 -08001314 */
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07001315 if (page_trans_huge_mapcount(page, NULL) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001316 pmd_t entry;
1317 entry = pmd_mkyoung(orig_pmd);
1318 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001319 if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1))
1320 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001321 ret |= VM_FAULT_WRITE;
1322 goto out_unlock;
1323 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001324 get_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001325 spin_unlock(fe->ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001326alloc:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001327 if (transparent_hugepage_enabled(vma) &&
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001328 !transparent_hugepage_debug_cow()) {
Mel Gorman444eb2a42016-03-17 14:19:23 -07001329 huge_gfp = alloc_hugepage_direct_gfpmask(vma);
Michal Hocko3b363692015-04-15 16:13:29 -07001330 new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER);
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -08001331 } else
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001332 new_page = NULL;
1333
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08001334 if (likely(new_page)) {
1335 prep_transhuge_page(new_page);
1336 } else {
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001337 if (!page) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001338 split_huge_pmd(vma, fe->pmd, fe->address);
Kirill A. Shutemove9b71ca2014-04-03 14:48:17 -07001339 ret |= VM_FAULT_FALLBACK;
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001340 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001341 ret = do_huge_pmd_wp_page_fallback(fe, orig_pmd, page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001342 if (ret & VM_FAULT_OOM) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001343 split_huge_pmd(vma, fe->pmd, fe->address);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001344 ret |= VM_FAULT_FALLBACK;
1345 }
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001346 put_page(page);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001347 }
David Rientjes17766dd2013-09-12 15:14:06 -07001348 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001349 goto out;
1350 }
1351
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001352 if (unlikely(mem_cgroup_try_charge(new_page, vma->vm_mm,
1353 huge_gfp, &memcg, true))) {
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001354 put_page(new_page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001355 split_huge_pmd(vma, fe->pmd, fe->address);
1356 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001357 put_page(page);
Kirill A. Shutemov9845cbb2014-02-25 15:01:42 -08001358 ret |= VM_FAULT_FALLBACK;
David Rientjes17766dd2013-09-12 15:14:06 -07001359 count_vm_event(THP_FAULT_FALLBACK);
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001360 goto out;
1361 }
1362
David Rientjes17766dd2013-09-12 15:14:06 -07001363 count_vm_event(THP_FAULT_ALLOC);
1364
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001365 if (!page)
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001366 clear_huge_page(new_page, haddr, HPAGE_PMD_NR);
1367 else
1368 copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001369 __SetPageUptodate(new_page);
1370
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001371 mmun_start = haddr;
1372 mmun_end = haddr + HPAGE_PMD_SIZE;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001373 mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001374
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001375 spin_lock(fe->ptl);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001376 if (page)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001377 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001378 if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) {
1379 spin_unlock(fe->ptl);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001380 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001381 put_page(new_page);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001382 goto out_mn;
Andrea Arcangelib9bbfbe2011-01-13 15:46:57 -08001383 } else {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001384 pmd_t entry;
Kirill A. Shutemov31223592013-09-12 15:14:01 -07001385 entry = mk_huge_pmd(new_page, vma->vm_page_prot);
1386 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001387 pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001388 page_add_new_anon_rmap(new_page, vma, haddr, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001389 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07001390 lru_cache_add_active_or_unevictable(new_page, vma);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001391 set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry);
1392 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Hugh Dickinseecc1e42014-01-12 01:25:21 -08001393 if (!page) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001394 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001395 put_huge_zero_page();
1396 } else {
Sasha Levin309381fea2014-01-23 15:52:54 -08001397 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001398 page_remove_rmap(page, true);
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001399 put_page(page);
1400 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001401 ret |= VM_FAULT_WRITE;
1402 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001403 spin_unlock(fe->ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001404out_mn:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001405 mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07001406out:
1407 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001408out_unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001409 spin_unlock(fe->ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001410 return ret;
1411}
1412
David Rientjesb676b292012-10-08 16:34:03 -07001413struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001414 unsigned long addr,
1415 pmd_t *pmd,
1416 unsigned int flags)
1417{
David Rientjesb676b292012-10-08 16:34:03 -07001418 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001419 struct page *page = NULL;
1420
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001421 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001422
1423 if (flags & FOLL_WRITE && !pmd_write(*pmd))
1424 goto out;
1425
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001426 /* Avoid dumping huge zero page */
1427 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1428 return ERR_PTR(-EFAULT);
1429
Mel Gorman2b4847e2013-12-18 17:08:32 -08001430 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001431 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001432 goto out;
1433
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001434 page = pmd_page(*pmd);
Sasha Levin309381fea2014-01-23 15:52:54 -08001435 VM_BUG_ON_PAGE(!PageHead(page), page);
Dan Williams3565fce2016-01-15 16:56:55 -08001436 if (flags & FOLL_TOUCH)
1437 touch_pmd(vma, addr, pmd);
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001438 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001439 /*
1440 * We don't mlock() pte-mapped THPs. This way we can avoid
1441 * leaking mlocked pages into non-VM_LOCKED VMAs.
1442 *
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001443 * For anon THP:
1444 *
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001445 * In most cases the pmd is the only mapping of the page as we
1446 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1447 * writable private mappings in populate_vma_page_range().
1448 *
1449 * The only scenario when we have the page shared here is if we
1450 * mlocking read-only mapping shared over fork(). We skip
1451 * mlocking such pages.
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001452 *
1453 * For file THP:
1454 *
1455 * We can expect PageDoubleMap() to be stable under page lock:
1456 * for file pages we set it in page_add_file_rmap(), which
1457 * requires page to be locked.
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001458 */
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001459
1460 if (PageAnon(page) && compound_mapcount(page) != 1)
1461 goto skip_mlock;
1462 if (PageDoubleMap(page) || !page->mapping)
1463 goto skip_mlock;
1464 if (!trylock_page(page))
1465 goto skip_mlock;
1466 lru_add_drain();
1467 if (page->mapping && !PageDoubleMap(page))
1468 mlock_vma_page(page);
1469 unlock_page(page);
David Rientjesb676b292012-10-08 16:34:03 -07001470 }
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001471skip_mlock:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001472 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Sasha Levin309381fea2014-01-23 15:52:54 -08001473 VM_BUG_ON_PAGE(!PageCompound(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001474 if (flags & FOLL_GET)
Kirill A. Shutemovddc58f22016-01-15 16:52:56 -08001475 get_page(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001476
1477out:
1478 return page;
1479}
1480
Mel Gormand10e63f2012-10-25 14:16:31 +02001481/* NUMA hinting page fault entry point for trans huge pmds */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001482int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t pmd)
Mel Gormand10e63f2012-10-25 14:16:31 +02001483{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001484 struct vm_area_struct *vma = fe->vma;
Mel Gormanb8916632013-10-07 11:28:44 +01001485 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001486 struct page *page;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001487 unsigned long haddr = fe->address & HPAGE_PMD_MASK;
Mel Gorman8191acb2013-10-07 11:28:45 +01001488 int page_nid = -1, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001489 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001490 bool page_locked;
1491 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001492 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001493 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001494
Mel Gormanc0e7cad2015-02-12 14:58:41 -08001495 /* A PROT_NONE fault should not end up here */
1496 BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)));
1497
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001498 fe->ptl = pmd_lock(vma->vm_mm, fe->pmd);
1499 if (unlikely(!pmd_same(pmd, *fe->pmd)))
Mel Gormand10e63f2012-10-25 14:16:31 +02001500 goto out_unlock;
1501
Mel Gormande466bd2013-12-18 17:08:42 -08001502 /*
1503 * If there are potential migrations, wait for completion and retry
1504 * without disrupting NUMA hinting information. Do not relock and
1505 * check_same as the page may no longer be mapped.
1506 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001507 if (unlikely(pmd_trans_migrating(*fe->pmd))) {
1508 page = pmd_page(*fe->pmd);
1509 spin_unlock(fe->ptl);
Mel Gorman5d833062015-02-12 14:58:16 -08001510 wait_on_page_locked(page);
Mel Gormande466bd2013-12-18 17:08:42 -08001511 goto out;
1512 }
1513
Mel Gormand10e63f2012-10-25 14:16:31 +02001514 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001515 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001516 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001517 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001518 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001519 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001520 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001521 flags |= TNF_FAULT_LOCAL;
1522 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001523
Mel Gormanbea66fb2015-03-25 15:55:37 -07001524 /* See similar comment in do_numa_page for explanation */
1525 if (!(vma->vm_flags & VM_WRITE))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001526 flags |= TNF_NO_GROUP;
1527
1528 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001529 * Acquire the page lock to serialise THP migrations but avoid dropping
1530 * page_table_lock if at all possible
1531 */
Mel Gormanb8916632013-10-07 11:28:44 +01001532 page_locked = trylock_page(page);
1533 target_nid = mpol_misplaced(page, vma, haddr);
1534 if (target_nid == -1) {
1535 /* If the page was locked, there are no parallel migrations */
Mel Gormana54a4072013-10-07 11:28:46 +01001536 if (page_locked)
Mel Gormanb8916632013-10-07 11:28:44 +01001537 goto clear_pmdnuma;
Mel Gorman2b4847e2013-12-18 17:08:32 -08001538 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001539
Mel Gormande466bd2013-12-18 17:08:42 -08001540 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001541 if (!page_locked) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001542 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001543 wait_on_page_locked(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001544 page_nid = -1;
Mel Gormanb8916632013-10-07 11:28:44 +01001545 goto out;
1546 }
1547
Mel Gorman2b4847e2013-12-18 17:08:32 -08001548 /*
1549 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1550 * to serialises splits
1551 */
Mel Gormanb8916632013-10-07 11:28:44 +01001552 get_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001553 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001554 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001555
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001556 /* Confirm the PMD did not change while page_table_lock was released */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001557 spin_lock(fe->ptl);
1558 if (unlikely(!pmd_same(pmd, *fe->pmd))) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001559 unlock_page(page);
1560 put_page(page);
Mel Gormana54a4072013-10-07 11:28:46 +01001561 page_nid = -1;
Mel Gormanb32967f2012-11-19 12:35:47 +00001562 goto out_unlock;
1563 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001564
Mel Gormanc3a489c2013-12-18 17:08:38 -08001565 /* Bail if we fail to protect against THP splits for any reason */
1566 if (unlikely(!anon_vma)) {
1567 put_page(page);
1568 page_nid = -1;
1569 goto clear_pmdnuma;
1570 }
1571
Mel Gormana54a4072013-10-07 11:28:46 +01001572 /*
1573 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001574 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001575 */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001576 spin_unlock(fe->ptl);
1577 migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
1578 fe->pmd, pmd, fe->address, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001579 if (migrated) {
1580 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001581 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001582 } else
1583 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001584
Mel Gorman8191acb2013-10-07 11:28:45 +01001585 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001586clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001587 BUG_ON(!PageLocked(page));
Mel Gormanb191f9b2015-03-25 15:55:40 -07001588 was_writable = pmd_write(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001589 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001590 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001591 if (was_writable)
1592 pmd = pmd_mkwrite(pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001593 set_pmd_at(vma->vm_mm, haddr, fe->pmd, pmd);
1594 update_mmu_cache_pmd(vma, fe->address, fe->pmd);
Mel Gormana54a4072013-10-07 11:28:46 +01001595 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001596out_unlock:
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001597 spin_unlock(fe->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001598
1599out:
1600 if (anon_vma)
1601 page_unlock_anon_vma_read(anon_vma);
1602
Mel Gorman8191acb2013-10-07 11:28:45 +01001603 if (page_nid != -1)
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001604 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, fe->flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001605
Mel Gormand10e63f2012-10-25 14:16:31 +02001606 return 0;
1607}
1608
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001609int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1610 pmd_t *pmd, unsigned long addr, unsigned long next)
1611
1612{
1613 spinlock_t *ptl;
1614 pmd_t orig_pmd;
1615 struct page *page;
1616 struct mm_struct *mm = tlb->mm;
1617 int ret = 0;
1618
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001619 ptl = pmd_trans_huge_lock(pmd, vma);
1620 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001621 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001622
1623 orig_pmd = *pmd;
1624 if (is_huge_zero_pmd(orig_pmd)) {
1625 ret = 1;
1626 goto out;
1627 }
1628
1629 page = pmd_page(orig_pmd);
1630 /*
1631 * If other processes are mapping this page, we couldn't discard
1632 * the page unless they all do MADV_FREE so let's skip the page.
1633 */
1634 if (page_mapcount(page) != 1)
1635 goto out;
1636
1637 if (!trylock_page(page))
1638 goto out;
1639
1640 /*
1641 * If user want to discard part-pages of THP, split it so MADV_FREE
1642 * will deactivate only them.
1643 */
1644 if (next - addr != HPAGE_PMD_SIZE) {
1645 get_page(page);
1646 spin_unlock(ptl);
Huang Ying9818b8c2016-07-14 12:07:12 -07001647 split_huge_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001648 put_page(page);
1649 unlock_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001650 goto out_unlocked;
1651 }
1652
1653 if (PageDirty(page))
1654 ClearPageDirty(page);
1655 unlock_page(page);
1656
1657 if (PageActive(page))
1658 deactivate_page(page);
1659
1660 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
1661 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1662 tlb->fullmm);
1663 orig_pmd = pmd_mkold(orig_pmd);
1664 orig_pmd = pmd_mkclean(orig_pmd);
1665
1666 set_pmd_at(mm, addr, pmd, orig_pmd);
1667 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1668 }
1669 ret = 1;
1670out:
1671 spin_unlock(ptl);
1672out_unlocked:
1673 return ret;
1674}
1675
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001676int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001677 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001678{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001679 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001680 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001681
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001682 ptl = __pmd_trans_huge_lock(pmd, vma);
1683 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001684 return 0;
1685 /*
1686 * For architectures like ppc64 we look at deposited pgtable
1687 * when calling pmdp_huge_get_and_clear. So do the
1688 * pgtable_trans_huge_withdraw after finishing pmdp related
1689 * operations.
1690 */
1691 orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd,
1692 tlb->fullmm);
1693 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1694 if (vma_is_dax(vma)) {
1695 spin_unlock(ptl);
1696 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001697 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001698 } else if (is_huge_zero_pmd(orig_pmd)) {
1699 pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd));
1700 atomic_long_dec(&tlb->mm->nr_ptes);
1701 spin_unlock(ptl);
Kirill A. Shutemovaa88b682016-04-28 16:18:27 -07001702 tlb_remove_page(tlb, pmd_page(orig_pmd));
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001703 } else {
1704 struct page *page = pmd_page(orig_pmd);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08001705 page_remove_rmap(page, true);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001706 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001707 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001708 if (PageAnon(page)) {
1709 pgtable_t pgtable;
1710 pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd);
1711 pte_free(tlb->mm, pgtable);
1712 atomic_long_dec(&tlb->mm->nr_ptes);
1713 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1714 } else {
1715 add_mm_counter(tlb->mm, MM_FILEPAGES, -HPAGE_PMD_NR);
1716 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001717 spin_unlock(ptl);
Aneesh Kumar K.Ve77b0852016-07-26 15:24:12 -07001718 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001719 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001720 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001721}
1722
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001723bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001724 unsigned long new_addr, unsigned long old_end,
1725 pmd_t *old_pmd, pmd_t *new_pmd)
1726{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001727 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001728 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001729 struct mm_struct *mm = vma->vm_mm;
1730
1731 if ((old_addr & ~HPAGE_PMD_MASK) ||
1732 (new_addr & ~HPAGE_PMD_MASK) ||
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001733 old_end - old_addr < HPAGE_PMD_SIZE)
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001734 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001735
1736 /*
1737 * The destination pmd shouldn't be established, free_pgtables()
1738 * should have release it.
1739 */
1740 if (WARN_ON(!pmd_none(*new_pmd))) {
1741 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001742 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001743 }
1744
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001745 /*
1746 * We don't have to worry about the ordering of src and dst
1747 * ptlocks because exclusive mmap_sem prevents deadlock.
1748 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001749 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1750 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001751 new_ptl = pmd_lockptr(mm, new_pmd);
1752 if (new_ptl != old_ptl)
1753 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001754 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001755 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001756
Kirill A. Shutemov69a8ec22016-02-17 13:11:12 -08001757 if (pmd_move_must_withdraw(new_ptl, old_ptl) &&
1758 vma_is_anonymous(vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301759 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001760 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1761 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001762 }
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301763 set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd));
1764 if (new_ptl != old_ptl)
1765 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001766 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001767 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001768 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001769 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001770}
1771
Mel Gormanf123d742013-10-07 11:28:49 +01001772/*
1773 * Returns
1774 * - 0 if PMD could not be locked
1775 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1776 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1777 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001778int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Mel Gormane944fd62015-02-12 14:58:35 -08001779 unsigned long addr, pgprot_t newprot, int prot_numa)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001780{
1781 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001782 spinlock_t *ptl;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001783 int ret = 0;
1784
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001785 ptl = __pmd_trans_huge_lock(pmd, vma);
1786 if (ptl) {
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001787 pmd_t entry;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001788 bool preserve_write = prot_numa && pmd_write(*pmd);
Mel Gormanba68bc02015-03-07 15:20:48 +00001789 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001790
1791 /*
1792 * Avoid trapping faults against the zero page. The read-only
1793 * data is likely to be read-cached on the local CPU and
1794 * local/remote hits to the zero page are not interesting.
1795 */
1796 if (prot_numa && is_huge_zero_pmd(*pmd)) {
1797 spin_unlock(ptl);
Mel Gormanba68bc02015-03-07 15:20:48 +00001798 return ret;
Mel Gormane944fd62015-02-12 14:58:35 -08001799 }
1800
Mel Gorman10c10452015-02-12 14:58:44 -08001801 if (!prot_numa || !pmd_protnone(*pmd)) {
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001802 entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd);
Mel Gorman10c10452015-02-12 14:58:44 -08001803 entry = pmd_modify(entry, newprot);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001804 if (preserve_write)
1805 entry = pmd_mkwrite(entry);
Mel Gorman10c10452015-02-12 14:58:44 -08001806 ret = HPAGE_PMD_NR;
1807 set_pmd_at(mm, addr, pmd, entry);
Kirill A. Shutemovb237ade2016-07-26 15:25:45 -07001808 BUG_ON(vma_is_anonymous(vma) && !preserve_write &&
1809 pmd_write(entry));
Mel Gorman10c10452015-02-12 14:58:44 -08001810 }
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001811 spin_unlock(ptl);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001812 }
Johannes Weinercd7548a2011-01-13 15:47:04 -08001813
1814 return ret;
1815}
1816
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001817/*
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001818 * Returns true if a given pmd maps a thp, false otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001819 *
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001820 * Note that if it returns true, this routine returns without unlocking page
1821 * table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001822 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001823spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001824{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001825 spinlock_t *ptl;
1826 ptl = pmd_lock(vma->vm_mm, pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08001827 if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001828 return ptl;
1829 spin_unlock(ptl);
1830 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001831}
1832
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001833#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE)
Andrea Arcangeli78f11a22011-04-27 15:26:45 -07001834
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001835int hugepage_madvise(struct vm_area_struct *vma,
1836 unsigned long *vm_flags, int advice)
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001837{
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001838 switch (advice) {
1839 case MADV_HUGEPAGE:
Alex Thorlton1e1836e2014-04-07 15:37:09 -07001840#ifdef CONFIG_S390
1841 /*
1842 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
1843 * can't handle this properly after s390_enable_sie, so we simply
1844 * ignore the madvise to prevent qemu from causing a SIGSEGV.
1845 */
1846 if (mm_has_pgste(vma->vm_mm))
1847 return 0;
1848#endif
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001849 *vm_flags &= ~VM_NOHUGEPAGE;
1850 *vm_flags |= VM_HUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001851 /*
1852 * If the vma become good for khugepaged to scan,
1853 * register it here without waiting a page fault that
1854 * may not happen any time soon.
1855 */
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001856 if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
1857 khugepaged_enter_vma_merge(vma, *vm_flags))
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001858 return -ENOMEM;
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001859 break;
1860 case MADV_NOHUGEPAGE:
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001861 *vm_flags &= ~VM_HUGEPAGE;
1862 *vm_flags |= VM_NOHUGEPAGE;
Andrea Arcangeli60ab3242011-01-13 15:47:18 -08001863 /*
1864 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
1865 * this vma even if we leave the mm registered in khugepaged if
1866 * it got registered before VM_NOHUGEPAGE was set.
1867 */
Andrea Arcangelia664b2d2011-01-13 15:47:17 -08001868 break;
1869 }
Andrea Arcangeli0af4e982011-01-13 15:46:55 -08001870
1871 return 0;
1872}
1873
Andrea Arcangeliba761492011-01-13 15:46:58 -08001874static int __init khugepaged_slab_init(void)
1875{
1876 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
1877 sizeof(struct mm_slot),
1878 __alignof__(struct mm_slot), 0, NULL);
1879 if (!mm_slot_cache)
1880 return -ENOMEM;
1881
1882 return 0;
1883}
1884
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -07001885static void __init khugepaged_slab_exit(void)
1886{
1887 kmem_cache_destroy(mm_slot_cache);
1888}
1889
Andrea Arcangeliba761492011-01-13 15:46:58 -08001890static inline struct mm_slot *alloc_mm_slot(void)
1891{
1892 if (!mm_slot_cache) /* initialization failed */
1893 return NULL;
1894 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
1895}
1896
1897static inline void free_mm_slot(struct mm_slot *mm_slot)
1898{
1899 kmem_cache_free(mm_slot_cache, mm_slot);
1900}
1901
Andrea Arcangeliba761492011-01-13 15:46:58 -08001902static struct mm_slot *get_mm_slot(struct mm_struct *mm)
1903{
1904 struct mm_slot *mm_slot;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001905
Sasha Levinb67bfe02013-02-27 17:06:00 -08001906 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001907 if (mm == mm_slot->mm)
1908 return mm_slot;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001909
Andrea Arcangeliba761492011-01-13 15:46:58 -08001910 return NULL;
1911}
1912
1913static void insert_to_mm_slots_hash(struct mm_struct *mm,
1914 struct mm_slot *mm_slot)
1915{
Andrea Arcangeliba761492011-01-13 15:46:58 -08001916 mm_slot->mm = mm;
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001917 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001918}
1919
1920static inline int khugepaged_test_exit(struct mm_struct *mm)
1921{
1922 return atomic_read(&mm->mm_users) == 0;
1923}
1924
1925int __khugepaged_enter(struct mm_struct *mm)
1926{
1927 struct mm_slot *mm_slot;
1928 int wakeup;
1929
1930 mm_slot = alloc_mm_slot();
1931 if (!mm_slot)
1932 return -ENOMEM;
1933
1934 /* __khugepaged_exit() must not run from under us */
Sasha Levin96dad672014-10-09 15:28:39 -07001935 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001936 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
1937 free_mm_slot(mm_slot);
1938 return 0;
1939 }
1940
1941 spin_lock(&khugepaged_mm_lock);
1942 insert_to_mm_slots_hash(mm, mm_slot);
1943 /*
1944 * Insert just behind the scanning cursor, to let the area settle
1945 * down a little.
1946 */
1947 wakeup = list_empty(&khugepaged_scan.mm_head);
1948 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
1949 spin_unlock(&khugepaged_mm_lock);
1950
1951 atomic_inc(&mm->mm_count);
1952 if (wakeup)
1953 wake_up_interruptible(&khugepaged_wait);
1954
1955 return 0;
1956}
1957
David Rientjes6d50e602014-10-29 14:50:31 -07001958int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
1959 unsigned long vm_flags)
Andrea Arcangeliba761492011-01-13 15:46:58 -08001960{
1961 unsigned long hstart, hend;
1962 if (!vma->anon_vma)
1963 /*
1964 * Not yet faulted in so we will register later in the
1965 * page fault if needed.
1966 */
1967 return 0;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001968 if (vma->vm_ops || (vm_flags & VM_NO_KHUGEPAGED))
Andrea Arcangeliba761492011-01-13 15:46:58 -08001969 /* khugepaged not yet working on file or special mappings */
1970 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08001971 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1972 hend = vma->vm_end & HPAGE_PMD_MASK;
1973 if (hstart < hend)
David Rientjes6d50e602014-10-29 14:50:31 -07001974 return khugepaged_enter(vma, vm_flags);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001975 return 0;
1976}
1977
1978void __khugepaged_exit(struct mm_struct *mm)
1979{
1980 struct mm_slot *mm_slot;
1981 int free = 0;
1982
1983 spin_lock(&khugepaged_mm_lock);
1984 mm_slot = get_mm_slot(mm);
1985 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
Sasha Levin43b5fbb2013-02-22 16:32:27 -08001986 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001987 list_del(&mm_slot->mm_node);
1988 free = 1;
1989 }
Chris Wrightd788e802011-07-25 17:12:14 -07001990 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08001991
1992 if (free) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001993 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1994 free_mm_slot(mm_slot);
1995 mmdrop(mm);
1996 } else if (mm_slot) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08001997 /*
1998 * This is required to serialize against
1999 * khugepaged_test_exit() (which is guaranteed to run
2000 * under mmap sem read mode). Stop here (after we
2001 * return all pagetables will be destroyed) until
2002 * khugepaged has finished working on the pagetables
2003 * under the mmap_sem.
2004 */
2005 down_write(&mm->mmap_sem);
2006 up_write(&mm->mmap_sem);
Chris Wrightd788e802011-07-25 17:12:14 -07002007 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002008}
2009
2010static void release_pte_page(struct page *page)
2011{
2012 /* 0 stands for page_is_file_cache(page) == false */
2013 dec_zone_page_state(page, NR_ISOLATED_ANON + 0);
2014 unlock_page(page);
2015 putback_lru_page(page);
2016}
2017
2018static void release_pte_pages(pte_t *pte, pte_t *_pte)
2019{
2020 while (--_pte >= pte) {
2021 pte_t pteval = *_pte;
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002022 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
Andrea Arcangeliba761492011-01-13 15:46:58 -08002023 release_pte_page(pte_page(pteval));
2024 }
2025}
2026
Andrea Arcangeliba761492011-01-13 15:46:58 -08002027static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
2028 unsigned long address,
2029 pte_t *pte)
2030{
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002031 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002032 pte_t *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002033 int none_or_zero = 0, result = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002034 bool referenced = false, writable = false;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002035
Andrea Arcangeliba761492011-01-13 15:46:58 -08002036 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
2037 _pte++, address += PAGE_SIZE) {
2038 pte_t pteval = *_pte;
Minchan Kim47aee4d2015-10-22 13:32:19 -07002039 if (pte_none(pteval) || (pte_present(pteval) &&
2040 is_zero_pfn(pte_pfn(pteval)))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002041 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002042 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002043 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002044 } else {
2045 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002046 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002047 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002048 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002049 if (!pte_present(pteval)) {
2050 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002051 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002052 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002053 page = vm_normal_page(vma, address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002054 if (unlikely(!page)) {
2055 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002056 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002057 }
Bob Liu344aa352012-12-11 16:00:34 -08002058
Sasha Levin309381fea2014-01-23 15:52:54 -08002059 VM_BUG_ON_PAGE(PageCompound(page), page);
2060 VM_BUG_ON_PAGE(!PageAnon(page), page);
2061 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002062
Andrea Arcangeliba761492011-01-13 15:46:58 -08002063 /*
2064 * We can do it before isolate_lru_page because the
2065 * page can't be freed from under us. NOTE: PG_lock
2066 * is needed to serialize against split_huge_page
2067 * when invoked from the VM.
2068 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002069 if (!trylock_page(page)) {
2070 result = SCAN_PAGE_LOCK;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002071 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002072 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002073
2074 /*
2075 * cannot use mapcount: can't collapse if there's a gup pin.
2076 * The page must only be referenced by the scanned process
2077 * and page swap cache.
2078 */
2079 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2080 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002081 result = SCAN_PAGE_COUNT;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002082 goto out;
2083 }
2084 if (pte_write(pteval)) {
2085 writable = true;
2086 } else {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002087 if (PageSwapCache(page) &&
2088 !reuse_swap_page(page, NULL)) {
Ebru Akagunduz10359212015-02-11 15:28:28 -08002089 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002090 result = SCAN_SWAP_CACHE_PAGE;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002091 goto out;
2092 }
2093 /*
2094 * Page is not in the swap cache. It can be collapsed
2095 * into a THP.
2096 */
2097 }
2098
Andrea Arcangeliba761492011-01-13 15:46:58 -08002099 /*
2100 * Isolate the page to avoid collapsing an hugepage
2101 * currently in use by the VM.
2102 */
2103 if (isolate_lru_page(page)) {
2104 unlock_page(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002105 result = SCAN_DEL_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002106 goto out;
2107 }
2108 /* 0 stands for page_is_file_cache(page) == false */
2109 inc_zone_page_state(page, NR_ISOLATED_ANON + 0);
Sasha Levin309381fea2014-01-23 15:52:54 -08002110 VM_BUG_ON_PAGE(!PageLocked(page), page);
2111 VM_BUG_ON_PAGE(PageLRU(page), page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002112
2113 /* If there is no mapped pte young don't collapse the page */
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002114 if (pte_young(pteval) ||
2115 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002116 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002117 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002118 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002119 if (likely(writable)) {
2120 if (likely(referenced)) {
2121 result = SCAN_SUCCEED;
yalin wang16fd0fe2016-01-21 16:40:30 -08002122 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002123 referenced, writable, result);
2124 return 1;
2125 }
2126 } else {
2127 result = SCAN_PAGE_RO;
2128 }
2129
Andrea Arcangeliba761492011-01-13 15:46:58 -08002130out:
Bob Liu344aa352012-12-11 16:00:34 -08002131 release_pte_pages(pte, _pte);
yalin wang16fd0fe2016-01-21 16:40:30 -08002132 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002133 referenced, writable, result);
Bob Liu344aa352012-12-11 16:00:34 -08002134 return 0;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002135}
2136
2137static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
2138 struct vm_area_struct *vma,
2139 unsigned long address,
2140 spinlock_t *ptl)
2141{
2142 pte_t *_pte;
2143 for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) {
2144 pte_t pteval = *_pte;
2145 struct page *src_page;
2146
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002147 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002148 clear_user_highpage(page, address);
2149 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002150 if (is_zero_pfn(pte_pfn(pteval))) {
2151 /*
2152 * ptl mostly unnecessary.
2153 */
2154 spin_lock(ptl);
2155 /*
2156 * paravirt calls inside pte_clear here are
2157 * superfluous.
2158 */
2159 pte_clear(vma->vm_mm, address, _pte);
2160 spin_unlock(ptl);
2161 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002162 } else {
2163 src_page = pte_page(pteval);
2164 copy_user_highpage(page, src_page, address, vma);
Sasha Levin309381fea2014-01-23 15:52:54 -08002165 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002166 release_pte_page(src_page);
2167 /*
2168 * ptl mostly unnecessary, but preempt has to
2169 * be disabled to update the per-cpu stats
2170 * inside page_remove_rmap().
2171 */
2172 spin_lock(ptl);
2173 /*
2174 * paravirt calls inside pte_clear here are
2175 * superfluous.
2176 */
2177 pte_clear(vma->vm_mm, address, _pte);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002178 page_remove_rmap(src_page, false);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002179 spin_unlock(ptl);
2180 free_page_and_swap_cache(src_page);
2181 }
2182
2183 address += PAGE_SIZE;
2184 page++;
2185 }
2186}
2187
Xiao Guangrong26234f32012-10-08 16:29:51 -07002188static void khugepaged_alloc_sleep(void)
2189{
Petr Mladekbde43c62015-09-08 15:04:05 -07002190 DEFINE_WAIT(wait);
2191
2192 add_wait_queue(&khugepaged_wait, &wait);
2193 freezable_schedule_timeout_interruptible(
2194 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
2195 remove_wait_queue(&khugepaged_wait, &wait);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002196}
2197
Bob Liu9f1b8682013-11-12 15:07:37 -08002198static int khugepaged_node_load[MAX_NUMNODES];
2199
David Rientjes14a4e212014-08-06 16:07:29 -07002200static bool khugepaged_scan_abort(int nid)
2201{
2202 int i;
2203
2204 /*
2205 * If zone_reclaim_mode is disabled, then no extra effort is made to
2206 * allocate memory locally.
2207 */
2208 if (!zone_reclaim_mode)
2209 return false;
2210
2211 /* If there is a count for this node already, it must be acceptable */
2212 if (khugepaged_node_load[nid])
2213 return false;
2214
2215 for (i = 0; i < MAX_NUMNODES; i++) {
2216 if (!khugepaged_node_load[i])
2217 continue;
2218 if (node_distance(nid, i) > RECLAIM_DISTANCE)
2219 return true;
2220 }
2221 return false;
2222}
2223
Xiao Guangrong26234f32012-10-08 16:29:51 -07002224#ifdef CONFIG_NUMA
Bob Liu9f1b8682013-11-12 15:07:37 -08002225static int khugepaged_find_target_node(void)
2226{
2227 static int last_khugepaged_target_node = NUMA_NO_NODE;
2228 int nid, target_node = 0, max_value = 0;
2229
2230 /* find first node with max normal pages hit */
2231 for (nid = 0; nid < MAX_NUMNODES; nid++)
2232 if (khugepaged_node_load[nid] > max_value) {
2233 max_value = khugepaged_node_load[nid];
2234 target_node = nid;
2235 }
2236
2237 /* do some balance if several nodes have the same hit record */
2238 if (target_node <= last_khugepaged_target_node)
2239 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
2240 nid++)
2241 if (max_value == khugepaged_node_load[nid]) {
2242 target_node = nid;
2243 break;
2244 }
2245
2246 last_khugepaged_target_node = target_node;
2247 return target_node;
2248}
2249
Xiao Guangrong26234f32012-10-08 16:29:51 -07002250static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2251{
2252 if (IS_ERR(*hpage)) {
2253 if (!*wait)
2254 return false;
2255
2256 *wait = false;
Xiao Guangronge3b41262012-10-08 16:32:57 -07002257 *hpage = NULL;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002258 khugepaged_alloc_sleep();
2259 } else if (*hpage) {
2260 put_page(*hpage);
2261 *hpage = NULL;
2262 }
2263
2264 return true;
2265}
2266
Michal Hocko3b363692015-04-15 16:13:29 -07002267static struct page *
2268khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002269 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002270{
Sasha Levin309381fea2014-01-23 15:52:54 -08002271 VM_BUG_ON_PAGE(*hpage, *hpage);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002272
Xiao Guangrong26234f32012-10-08 16:29:51 -07002273 /*
Vlastimil Babka8b164562014-10-09 15:27:00 -07002274 * Before allocating the hugepage, release the mmap_sem read lock.
2275 * The allocation can take potentially a long time if it involves
2276 * sync compaction, and we do not need to hold the mmap_sem during
2277 * that. We will recheck the vma after taking it again in write mode.
Xiao Guangrong26234f32012-10-08 16:29:51 -07002278 */
2279 up_read(&mm->mmap_sem);
Vlastimil Babka8b164562014-10-09 15:27:00 -07002280
Vlastimil Babka96db8002015-09-08 15:03:50 -07002281 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002282 if (unlikely(!*hpage)) {
2283 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2284 *hpage = ERR_PTR(-ENOMEM);
2285 return NULL;
2286 }
2287
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002288 prep_transhuge_page(*hpage);
Xiao Guangrong26234f32012-10-08 16:29:51 -07002289 count_vm_event(THP_COLLAPSE_ALLOC);
2290 return *hpage;
2291}
2292#else
Bob Liu9f1b8682013-11-12 15:07:37 -08002293static int khugepaged_find_target_node(void)
2294{
2295 return 0;
2296}
2297
Mel Gorman444eb2a42016-03-17 14:19:23 -07002298static inline struct page *alloc_khugepaged_hugepage(void)
Bob Liu10dc4152013-11-12 15:07:35 -08002299{
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002300 struct page *page;
2301
Mel Gorman444eb2a42016-03-17 14:19:23 -07002302 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
2303 HPAGE_PMD_ORDER);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002304 if (page)
2305 prep_transhuge_page(page);
2306 return page;
Bob Liu10dc4152013-11-12 15:07:35 -08002307}
2308
Xiao Guangrong26234f32012-10-08 16:29:51 -07002309static struct page *khugepaged_alloc_hugepage(bool *wait)
2310{
2311 struct page *hpage;
2312
2313 do {
Mel Gorman444eb2a42016-03-17 14:19:23 -07002314 hpage = alloc_khugepaged_hugepage();
Xiao Guangrong26234f32012-10-08 16:29:51 -07002315 if (!hpage) {
2316 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
2317 if (!*wait)
2318 return NULL;
2319
2320 *wait = false;
2321 khugepaged_alloc_sleep();
2322 } else
2323 count_vm_event(THP_COLLAPSE_ALLOC);
2324 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
2325
2326 return hpage;
2327}
2328
2329static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
2330{
2331 if (!*hpage)
2332 *hpage = khugepaged_alloc_hugepage(wait);
2333
2334 if (unlikely(!*hpage))
2335 return false;
2336
2337 return true;
2338}
2339
Michal Hocko3b363692015-04-15 16:13:29 -07002340static struct page *
2341khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm,
Aaron Tomlind6669d62015-11-06 16:28:52 -08002342 unsigned long address, int node)
Xiao Guangrong26234f32012-10-08 16:29:51 -07002343{
2344 up_read(&mm->mmap_sem);
2345 VM_BUG_ON(!*hpage);
Michal Hocko3b363692015-04-15 16:13:29 -07002346
Xiao Guangrong26234f32012-10-08 16:29:51 -07002347 return *hpage;
2348}
2349#endif
2350
Bob Liufa475e52012-12-11 16:00:39 -08002351static bool hugepage_vma_check(struct vm_area_struct *vma)
2352{
2353 if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
2354 (vma->vm_flags & VM_NOHUGEPAGE))
2355 return false;
Bob Liufa475e52012-12-11 16:00:39 -08002356 if (!vma->anon_vma || vma->vm_ops)
2357 return false;
2358 if (is_vma_temporary_stack(vma))
2359 return false;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002360 return !(vma->vm_flags & VM_NO_KHUGEPAGED);
Bob Liufa475e52012-12-11 16:00:39 -08002361}
2362
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002363/*
Ebru Akagunduz72695862016-07-26 15:25:06 -07002364 * If mmap_sem temporarily dropped, revalidate vma
2365 * before taking mmap_sem.
2366 * Return 0 if succeeds, otherwise return none-zero
2367 * value (scan code).
2368 */
2369
2370static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address)
2371{
2372 struct vm_area_struct *vma;
2373 unsigned long hstart, hend;
2374
2375 if (unlikely(khugepaged_test_exit(mm)))
2376 return SCAN_ANY_PROCESS;
2377
2378 vma = find_vma(mm, address);
2379 if (!vma)
2380 return SCAN_VMA_NULL;
2381
2382 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2383 hend = vma->vm_end & HPAGE_PMD_MASK;
2384 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
2385 return SCAN_ADDRESS_RANGE;
2386 if (!hugepage_vma_check(vma))
2387 return SCAN_VMA_CHECK;
2388 return 0;
2389}
2390
2391/*
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002392 * Bring missing pages in from swap, to complete THP collapse.
2393 * Only done if khugepaged_scan_pmd believes it is worthwhile.
2394 *
2395 * Called and returns without pte mapped or spinlocks held,
2396 * but with mmap_sem held to protect against vma changes.
2397 */
2398
Ebru Akagunduz72695862016-07-26 15:25:06 -07002399static bool __collapse_huge_page_swapin(struct mm_struct *mm,
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002400 struct vm_area_struct *vma,
2401 unsigned long address, pmd_t *pmd)
2402{
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002403 pte_t pteval;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002404 int swapped_in = 0, ret = 0;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002405 struct fault_env fe = {
2406 .vma = vma,
2407 .address = address,
2408 .flags = FAULT_FLAG_ALLOW_RETRY,
2409 .pmd = pmd,
2410 };
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002411
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002412 fe.pte = pte_offset_map(pmd, address);
2413 for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE;
2414 fe.pte++, fe.address += PAGE_SIZE) {
2415 pteval = *fe.pte;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002416 if (!is_swap_pte(pteval))
2417 continue;
2418 swapped_in++;
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002419 ret = do_swap_page(&fe, pteval);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002420 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
2421 if (ret & VM_FAULT_RETRY) {
2422 down_read(&mm->mmap_sem);
2423 /* vma is no longer available, don't continue to swapin */
2424 if (hugepage_vma_revalidate(mm, address))
2425 return false;
Kirill A. Shutemov1f52e672016-07-26 15:25:12 -07002426 /* check if the pmd is still valid */
2427 if (mm_find_pmd(mm, address) != pmd)
2428 return false;
Ebru Akagunduz72695862016-07-26 15:25:06 -07002429 }
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002430 if (ret & VM_FAULT_ERROR) {
2431 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 0);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002432 return false;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002433 }
2434 /* pte is unmapped now, we need to map it */
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002435 fe.pte = pte_offset_map(pmd, fe.address);
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002436 }
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07002437 fe.pte--;
2438 pte_unmap(fe.pte);
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002439 trace_mm_collapse_huge_page_swapin(mm, swapped_in, 1);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002440 return true;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002441}
2442
Andrea Arcangeliba761492011-01-13 15:46:58 -08002443static void collapse_huge_page(struct mm_struct *mm,
Xiao Guangrong26234f32012-10-08 16:29:51 -07002444 unsigned long address,
2445 struct page **hpage,
2446 struct vm_area_struct *vma,
2447 int node)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002448{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002449 pmd_t *pmd, _pmd;
2450 pte_t *pte;
2451 pgtable_t pgtable;
2452 struct page *new_page;
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002453 spinlock_t *pmd_ptl, *pte_ptl;
Arnd Bergmann629d9d12016-01-18 21:50:26 +01002454 int isolated = 0, result = 0;
Johannes Weiner00501b52014-08-08 14:19:20 -07002455 struct mem_cgroup *memcg;
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002456 unsigned long mmun_start; /* For mmu_notifiers */
2457 unsigned long mmun_end; /* For mmu_notifiers */
Michal Hocko3b363692015-04-15 16:13:29 -07002458 gfp_t gfp;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002459
2460 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
Andrea Arcangeli692e0b32011-05-24 17:12:14 -07002461
Michal Hocko3b363692015-04-15 16:13:29 -07002462 /* Only allocate from the target node */
Mel Gorman444eb2a42016-03-17 14:19:23 -07002463 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE;
Michal Hocko3b363692015-04-15 16:13:29 -07002464
Xiao Guangrong26234f32012-10-08 16:29:51 -07002465 /* release the mmap_sem read lock. */
Aaron Tomlind6669d62015-11-06 16:28:52 -08002466 new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002467 if (!new_page) {
2468 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
2469 goto out_nolock;
2470 }
Andrea Arcangelice83d212011-01-13 15:47:06 -08002471
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002472 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002473 result = SCAN_CGROUP_CHARGE_FAIL;
2474 goto out_nolock;
2475 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002476
Ebru Akagunduz72695862016-07-26 15:25:06 -07002477 down_read(&mm->mmap_sem);
2478 result = hugepage_vma_revalidate(mm, address);
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002479 if (result) {
2480 mem_cgroup_cancel_charge(new_page, memcg, true);
2481 up_read(&mm->mmap_sem);
2482 goto out_nolock;
2483 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002484
Bob Liu62190492012-12-11 16:00:37 -08002485 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002486 if (!pmd) {
2487 result = SCAN_PMD_NULL;
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002488 mem_cgroup_cancel_charge(new_page, memcg, true);
2489 up_read(&mm->mmap_sem);
2490 goto out_nolock;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002491 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002492
Ebru Akagunduz72695862016-07-26 15:25:06 -07002493 /*
2494 * __collapse_huge_page_swapin always returns with mmap_sem locked.
2495 * If it fails, release mmap_sem and jump directly out.
2496 * Continuing to collapse causes inconsistency.
2497 */
2498 if (!__collapse_huge_page_swapin(mm, vma, address, pmd)) {
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002499 mem_cgroup_cancel_charge(new_page, memcg, true);
Ebru Akagunduz72695862016-07-26 15:25:06 -07002500 up_read(&mm->mmap_sem);
Ebru Akagunduz8024ee22016-07-26 15:25:09 -07002501 goto out_nolock;
Ebru Akagunduz72695862016-07-26 15:25:06 -07002502 }
2503
2504 up_read(&mm->mmap_sem);
2505 /*
2506 * Prevent all access to pagetables with the exception of
2507 * gup_fast later handled by the ptep_clear_flush and the VM
2508 * handled by the anon_vma lock + PG_lock.
2509 */
2510 down_write(&mm->mmap_sem);
2511 result = hugepage_vma_revalidate(mm, address);
2512 if (result)
2513 goto out;
Kirill A. Shutemov1f52e672016-07-26 15:25:12 -07002514 /* check if the pmd is still valid */
2515 if (mm_find_pmd(mm, address) != pmd)
2516 goto out;
Ebru Akagunduz8a966ed2016-07-26 15:25:03 -07002517
Ingo Molnar4fc3f1d2012-12-02 19:56:50 +00002518 anon_vma_lock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002519
2520 pte = pte_offset_map(pmd, address);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002521 pte_ptl = pte_lockptr(mm, pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002522
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002523 mmun_start = address;
2524 mmun_end = address + HPAGE_PMD_SIZE;
2525 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002526 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
Andrea Arcangeliba761492011-01-13 15:46:58 -08002527 /*
2528 * After this gup_fast can't run anymore. This also removes
2529 * any huge TLB entry from the CPU so we won't allow
2530 * huge and small TLB entries for the same virtual address
2531 * to avoid the risk of CPU bugs in that area.
2532 */
Aneesh Kumar K.V15a25b22015-06-24 16:57:39 -07002533 _pmd = pmdp_collapse_flush(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002534 spin_unlock(pmd_ptl);
Sagi Grimberg2ec74c32012-10-08 16:33:33 -07002535 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002536
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002537 spin_lock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002538 isolated = __collapse_huge_page_isolate(vma, address, pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002539 spin_unlock(pte_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002540
2541 if (unlikely(!isolated)) {
Johannes Weiner453c7192011-01-20 14:44:18 -08002542 pte_unmap(pte);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002543 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002544 BUG_ON(!pmd_none(*pmd));
Aneesh Kumar K.V7c342512013-05-24 15:55:21 -07002545 /*
2546 * We can only use set_pmd_at when establishing
2547 * hugepmds and never for establishing regular pmds that
2548 * points to regular pagetables. Use pmd_populate for that
2549 */
2550 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002551 spin_unlock(pmd_ptl);
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002552 anon_vma_unlock_write(vma->anon_vma);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002553 result = SCAN_FAIL;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002554 goto out;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002555 }
2556
2557 /*
2558 * All pages are isolated and locked so anon_vma rmap
2559 * can't run anymore.
2560 */
Konstantin Khlebnikov08b52702013-02-22 16:34:40 -08002561 anon_vma_unlock_write(vma->anon_vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002562
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002563 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
Johannes Weiner453c7192011-01-20 14:44:18 -08002564 pte_unmap(pte);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002565 __SetPageUptodate(new_page);
2566 pgtable = pmd_pgtable(_pmd);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002567
Kirill A. Shutemov31223592013-09-12 15:14:01 -07002568 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
2569 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002570
2571 /*
2572 * spin_lock() below is not the equivalent of smp_wmb(), so
2573 * this is needed to avoid the copy_huge_page writes to become
2574 * visible after the set_pmd_at() write.
2575 */
2576 smp_wmb();
2577
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002578 spin_lock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002579 BUG_ON(!pmd_none(*pmd));
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -08002580 page_add_new_anon_rmap(new_page, vma, address, true);
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002581 mem_cgroup_commit_charge(new_page, memcg, false, true);
Johannes Weiner00501b52014-08-08 14:19:20 -07002582 lru_cache_add_active_or_unevictable(new_page, vma);
Aneesh Kumar K.Vfce144b2013-06-05 17:14:06 -07002583 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002584 set_pmd_at(mm, address, pmd, _pmd);
David Millerb113da62012-10-08 16:34:25 -07002585 update_mmu_cache_pmd(vma, address, pmd);
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08002586 spin_unlock(pmd_ptl);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002587
2588 *hpage = NULL;
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002589
Andrea Arcangeliba761492011-01-13 15:46:58 -08002590 khugepaged_pages_collapsed++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002591 result = SCAN_SUCCEED;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002592out_up_write:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002593 up_write(&mm->mmap_sem);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002594out_nolock:
2595 trace_mm_collapse_huge_page(mm, isolated, result);
2596 return;
Andrea Arcangelice83d212011-01-13 15:47:06 -08002597out:
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08002598 mem_cgroup_cancel_charge(new_page, memcg, true);
Andrea Arcangelice83d212011-01-13 15:47:06 -08002599 goto out_up_write;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002600}
2601
2602static int khugepaged_scan_pmd(struct mm_struct *mm,
2603 struct vm_area_struct *vma,
2604 unsigned long address,
2605 struct page **hpage)
2606{
Andrea Arcangeliba761492011-01-13 15:46:58 -08002607 pmd_t *pmd;
2608 pte_t *pte, *_pte;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002609 int ret = 0, none_or_zero = 0, result = 0;
2610 struct page *page = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002611 unsigned long _address;
2612 spinlock_t *ptl;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002613 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz10359212015-02-11 15:28:28 -08002614 bool writable = false, referenced = false;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002615
2616 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
2617
Bob Liu62190492012-12-11 16:00:37 -08002618 pmd = mm_find_pmd(mm, address);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002619 if (!pmd) {
2620 result = SCAN_PMD_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002621 goto out;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002622 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002623
Bob Liu9f1b8682013-11-12 15:07:37 -08002624 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002625 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
2626 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
2627 _pte++, _address += PAGE_SIZE) {
2628 pte_t pteval = *_pte;
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002629 if (is_swap_pte(pteval)) {
2630 if (++unmapped <= khugepaged_max_ptes_swap) {
2631 continue;
2632 } else {
2633 result = SCAN_EXCEED_SWAP_PTE;
2634 goto out_unmap;
2635 }
2636 }
Ebru Akagunduzca0984c2015-04-14 15:45:24 -07002637 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
Andrea Arcangelic1294d02015-09-04 15:46:27 -07002638 if (!userfaultfd_armed(vma) &&
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002639 ++none_or_zero <= khugepaged_max_ptes_none) {
Andrea Arcangeliba761492011-01-13 15:46:58 -08002640 continue;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002641 } else {
2642 result = SCAN_EXCEED_NONE_PTE;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002643 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002644 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002645 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002646 if (!pte_present(pteval)) {
2647 result = SCAN_PTE_NON_PRESENT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002648 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002649 }
Ebru Akagunduz10359212015-02-11 15:28:28 -08002650 if (pte_write(pteval))
2651 writable = true;
2652
Andrea Arcangeliba761492011-01-13 15:46:58 -08002653 page = vm_normal_page(vma, _address, pteval);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002654 if (unlikely(!page)) {
2655 result = SCAN_PAGE_NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002656 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002657 }
Kirill A. Shutemovb1caa952016-01-15 16:52:39 -08002658
2659 /* TODO: teach khugepaged to collapse THP mapped with pte */
2660 if (PageCompound(page)) {
2661 result = SCAN_PAGE_COMPOUND;
2662 goto out_unmap;
2663 }
2664
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002665 /*
Bob Liu9f1b8682013-11-12 15:07:37 -08002666 * Record which node the original page is from and save this
2667 * information to khugepaged_node_load[].
2668 * Khupaged will allocate hugepage from the node has the max
2669 * hit record.
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002670 */
Bob Liu9f1b8682013-11-12 15:07:37 -08002671 node = page_to_nid(page);
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002672 if (khugepaged_scan_abort(node)) {
2673 result = SCAN_SCAN_ABORT;
David Rientjes14a4e212014-08-06 16:07:29 -07002674 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002675 }
Bob Liu9f1b8682013-11-12 15:07:37 -08002676 khugepaged_node_load[node]++;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002677 if (!PageLRU(page)) {
Kirill A. Shutemov0fda2782016-03-25 14:22:20 -07002678 result = SCAN_PAGE_LRU;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002679 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002680 }
2681 if (PageLocked(page)) {
2682 result = SCAN_PAGE_LOCK;
2683 goto out_unmap;
2684 }
2685 if (!PageAnon(page)) {
2686 result = SCAN_PAGE_ANON;
2687 goto out_unmap;
2688 }
2689
Ebru Akagunduz10359212015-02-11 15:28:28 -08002690 /*
2691 * cannot use mapcount: can't collapse if there's a gup pin.
2692 * The page must only be referenced by the scanned process
2693 * and page swap cache.
2694 */
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002695 if (page_count(page) != 1 + !!PageSwapCache(page)) {
2696 result = SCAN_PAGE_COUNT;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002697 goto out_unmap;
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002698 }
Vladimir Davydov33c3fc72015-09-09 15:35:45 -07002699 if (pte_young(pteval) ||
2700 page_is_young(page) || PageReferenced(page) ||
Andrea Arcangeli8ee53822011-01-13 15:47:10 -08002701 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz10359212015-02-11 15:28:28 -08002702 referenced = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002703 }
Ebru Akagunduz7d2eba02016-01-14 15:22:19 -08002704 if (writable) {
2705 if (referenced) {
2706 result = SCAN_SUCCEED;
2707 ret = 1;
2708 } else {
2709 result = SCAN_NO_REFERENCED_PAGE;
2710 }
2711 } else {
2712 result = SCAN_PAGE_RO;
2713 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002714out_unmap:
2715 pte_unmap_unlock(pte, ptl);
Bob Liu9f1b8682013-11-12 15:07:37 -08002716 if (ret) {
2717 node = khugepaged_find_target_node();
Andrea Arcangelice83d212011-01-13 15:47:06 -08002718 /* collapse_huge_page will return with the mmap_sem released */
Andi Kleen5c4b4be2011-03-04 17:36:32 -08002719 collapse_huge_page(mm, address, hpage, vma, node);
Bob Liu9f1b8682013-11-12 15:07:37 -08002720 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002721out:
yalin wang16fd0fe2016-01-21 16:40:30 -08002722 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
Ebru Akagunduz70652f62016-07-26 15:24:59 -07002723 none_or_zero, result, unmapped);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002724 return ret;
2725}
2726
2727static void collect_mm_slot(struct mm_slot *mm_slot)
2728{
2729 struct mm_struct *mm = mm_slot->mm;
2730
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002731 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002732
2733 if (khugepaged_test_exit(mm)) {
2734 /* free mm_slot */
Sasha Levin43b5fbb2013-02-22 16:32:27 -08002735 hash_del(&mm_slot->hash);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002736 list_del(&mm_slot->mm_node);
2737
2738 /*
2739 * Not strictly needed because the mm exited already.
2740 *
2741 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
2742 */
2743
2744 /* khugepaged_mm_lock actually not necessary for the below */
2745 free_mm_slot(mm_slot);
2746 mmdrop(mm);
2747 }
2748}
2749
2750static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
2751 struct page **hpage)
H Hartley Sweeten2f1da642011-10-31 17:09:25 -07002752 __releases(&khugepaged_mm_lock)
2753 __acquires(&khugepaged_mm_lock)
Andrea Arcangeliba761492011-01-13 15:46:58 -08002754{
2755 struct mm_slot *mm_slot;
2756 struct mm_struct *mm;
2757 struct vm_area_struct *vma;
2758 int progress = 0;
2759
2760 VM_BUG_ON(!pages);
Hugh Dickinsb9980cd2012-02-08 17:13:40 -08002761 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
Andrea Arcangeliba761492011-01-13 15:46:58 -08002762
2763 if (khugepaged_scan.mm_slot)
2764 mm_slot = khugepaged_scan.mm_slot;
2765 else {
2766 mm_slot = list_entry(khugepaged_scan.mm_head.next,
2767 struct mm_slot, mm_node);
2768 khugepaged_scan.address = 0;
2769 khugepaged_scan.mm_slot = mm_slot;
2770 }
2771 spin_unlock(&khugepaged_mm_lock);
2772
2773 mm = mm_slot->mm;
2774 down_read(&mm->mmap_sem);
2775 if (unlikely(khugepaged_test_exit(mm)))
2776 vma = NULL;
2777 else
2778 vma = find_vma(mm, khugepaged_scan.address);
2779
2780 progress++;
2781 for (; vma; vma = vma->vm_next) {
2782 unsigned long hstart, hend;
2783
2784 cond_resched();
2785 if (unlikely(khugepaged_test_exit(mm))) {
2786 progress++;
2787 break;
2788 }
Bob Liufa475e52012-12-11 16:00:39 -08002789 if (!hugepage_vma_check(vma)) {
2790skip:
Andrea Arcangeliba761492011-01-13 15:46:58 -08002791 progress++;
2792 continue;
2793 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002794 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2795 hend = vma->vm_end & HPAGE_PMD_MASK;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002796 if (hstart >= hend)
2797 goto skip;
2798 if (khugepaged_scan.address > hend)
2799 goto skip;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002800 if (khugepaged_scan.address < hstart)
2801 khugepaged_scan.address = hstart;
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002802 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002803
2804 while (khugepaged_scan.address < hend) {
2805 int ret;
2806 cond_resched();
2807 if (unlikely(khugepaged_test_exit(mm)))
2808 goto breakouterloop;
2809
2810 VM_BUG_ON(khugepaged_scan.address < hstart ||
2811 khugepaged_scan.address + HPAGE_PMD_SIZE >
2812 hend);
2813 ret = khugepaged_scan_pmd(mm, vma,
2814 khugepaged_scan.address,
2815 hpage);
2816 /* move to next address */
2817 khugepaged_scan.address += HPAGE_PMD_SIZE;
2818 progress += HPAGE_PMD_NR;
2819 if (ret)
2820 /* we released mmap_sem so break loop */
2821 goto breakouterloop_mmap_sem;
2822 if (progress >= pages)
2823 goto breakouterloop;
2824 }
2825 }
2826breakouterloop:
2827 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2828breakouterloop_mmap_sem:
2829
2830 spin_lock(&khugepaged_mm_lock);
Andrea Arcangelia7d6e4e2011-02-15 19:02:45 +01002831 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002832 /*
2833 * Release the current mm_slot if this mm is about to die, or
2834 * if we scanned all vmas of this mm.
2835 */
2836 if (khugepaged_test_exit(mm) || !vma) {
2837 /*
2838 * Make sure that if mm_users is reaching zero while
2839 * khugepaged runs here, khugepaged_exit will find
2840 * mm_slot not pointing to the exiting mm.
2841 */
2842 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2843 khugepaged_scan.mm_slot = list_entry(
2844 mm_slot->mm_node.next,
2845 struct mm_slot, mm_node);
2846 khugepaged_scan.address = 0;
2847 } else {
2848 khugepaged_scan.mm_slot = NULL;
2849 khugepaged_full_scans++;
2850 }
2851
2852 collect_mm_slot(mm_slot);
2853 }
2854
2855 return progress;
2856}
2857
2858static int khugepaged_has_work(void)
2859{
2860 return !list_empty(&khugepaged_scan.mm_head) &&
2861 khugepaged_enabled();
2862}
2863
2864static int khugepaged_wait_event(void)
2865{
2866 return !list_empty(&khugepaged_scan.mm_head) ||
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002867 kthread_should_stop();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002868}
2869
Xiao Guangrongd5169042012-10-08 16:29:48 -07002870static void khugepaged_do_scan(void)
2871{
2872 struct page *hpage = NULL;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002873 unsigned int progress = 0, pass_through_head = 0;
2874 unsigned int pages = khugepaged_pages_to_scan;
Xiao Guangrongd5169042012-10-08 16:29:48 -07002875 bool wait = true;
Andrea Arcangeliba761492011-01-13 15:46:58 -08002876
2877 barrier(); /* write khugepaged_pages_to_scan to local stack */
2878
2879 while (progress < pages) {
Xiao Guangrong26234f32012-10-08 16:29:51 -07002880 if (!khugepaged_prealloc_page(&hpage, &wait))
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002881 break;
Xiao Guangrong26234f32012-10-08 16:29:51 -07002882
Xiao Guangrong420256ef2012-10-08 16:29:49 -07002883 cond_resched();
Andrea Arcangeliba761492011-01-13 15:46:58 -08002884
Jiri Kosinacd092412015-06-24 16:56:07 -07002885 if (unlikely(kthread_should_stop() || try_to_freeze()))
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002886 break;
2887
Andrea Arcangeliba761492011-01-13 15:46:58 -08002888 spin_lock(&khugepaged_mm_lock);
2889 if (!khugepaged_scan.mm_slot)
2890 pass_through_head++;
2891 if (khugepaged_has_work() &&
2892 pass_through_head < 2)
2893 progress += khugepaged_scan_mm_slot(pages - progress,
Xiao Guangrongd5169042012-10-08 16:29:48 -07002894 &hpage);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002895 else
2896 progress = pages;
2897 spin_unlock(&khugepaged_mm_lock);
2898 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002899
Xiao Guangrongd5169042012-10-08 16:29:48 -07002900 if (!IS_ERR_OR_NULL(hpage))
2901 put_page(hpage);
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -08002902}
2903
David Rientjesf0508972016-05-20 16:58:56 -07002904static bool khugepaged_should_wakeup(void)
2905{
2906 return kthread_should_stop() ||
2907 time_after_eq(jiffies, khugepaged_sleep_expire);
2908}
2909
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002910static void khugepaged_wait_work(void)
2911{
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002912 if (khugepaged_has_work()) {
David Rientjesf0508972016-05-20 16:58:56 -07002913 const unsigned long scan_sleep_jiffies =
2914 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2915
2916 if (!scan_sleep_jiffies)
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002917 return;
2918
David Rientjesf0508972016-05-20 16:58:56 -07002919 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002920 wait_event_freezable_timeout(khugepaged_wait,
David Rientjesf0508972016-05-20 16:58:56 -07002921 khugepaged_should_wakeup(),
2922 scan_sleep_jiffies);
Xiao Guangrong2017c0b2012-10-08 16:29:44 -07002923 return;
2924 }
2925
2926 if (khugepaged_enabled())
2927 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2928}
2929
Andrea Arcangeliba761492011-01-13 15:46:58 -08002930static int khugepaged(void *none)
2931{
2932 struct mm_slot *mm_slot;
2933
Andrea Arcangeli878aee72011-01-13 15:47:10 -08002934 set_freezable();
Dongsheng Yang8698a742014-03-11 18:09:12 +08002935 set_user_nice(current, MAX_NICE);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002936
Xiao Guangrongb7231782012-10-08 16:29:54 -07002937 while (!kthread_should_stop()) {
2938 khugepaged_do_scan();
2939 khugepaged_wait_work();
2940 }
Andrea Arcangeliba761492011-01-13 15:46:58 -08002941
2942 spin_lock(&khugepaged_mm_lock);
2943 mm_slot = khugepaged_scan.mm_slot;
2944 khugepaged_scan.mm_slot = NULL;
2945 if (mm_slot)
2946 collect_mm_slot(mm_slot);
2947 spin_unlock(&khugepaged_mm_lock);
Andrea Arcangeliba761492011-01-13 15:46:58 -08002948 return 0;
2949}
2950
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002951static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2952 unsigned long haddr, pmd_t *pmd)
2953{
2954 struct mm_struct *mm = vma->vm_mm;
2955 pgtable_t pgtable;
2956 pmd_t _pmd;
2957 int i;
2958
2959 /* leave pmd empty until pte is filled */
2960 pmdp_huge_clear_flush_notify(vma, haddr, pmd);
2961
2962 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2963 pmd_populate(mm, &_pmd, pgtable);
2964
2965 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2966 pte_t *pte, entry;
2967 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2968 entry = pte_mkspecial(entry);
2969 pte = pte_offset_map(&_pmd, haddr);
2970 VM_BUG_ON(!pte_none(*pte));
2971 set_pte_at(mm, haddr, pte, entry);
2972 pte_unmap(pte);
2973 }
2974 smp_wmb(); /* make pte visible before pmd */
2975 pmd_populate(mm, pmd, pgtable);
2976 put_huge_zero_page();
2977}
2978
2979static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002980 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002981{
2982 struct mm_struct *mm = vma->vm_mm;
2983 struct page *page;
2984 pgtable_t pgtable;
2985 pmd_t _pmd;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002986 bool young, write, dirty;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002987 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002988 int i;
2989
2990 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2991 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2992 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Dan Williams5c7fb562016-01-15 16:56:52 -08002993 VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002994
2995 count_vm_event(THP_SPLIT_PMD);
2996
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002997 if (!vma_is_anonymous(vma)) {
2998 _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002999 if (is_huge_zero_pmd(_pmd))
3000 put_huge_zero_page();
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07003001 if (vma_is_dax(vma))
3002 return;
3003 page = pmd_page(_pmd);
3004 if (!PageReferenced(page) && pmd_young(_pmd))
3005 SetPageReferenced(page);
3006 page_remove_rmap(page, true);
3007 put_page(page);
3008 add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003009 return;
3010 } else if (is_huge_zero_pmd(*pmd)) {
3011 return __split_huge_zero_page_pmd(vma, haddr, pmd);
3012 }
3013
3014 page = pmd_page(*pmd);
3015 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003016 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003017 write = pmd_write(*pmd);
3018 young = pmd_young(*pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003019 dirty = pmd_dirty(*pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003020
Aneesh Kumar K.Vc777e2a2016-02-09 06:50:31 +05303021 pmdp_huge_split_prepare(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003022 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
3023 pmd_populate(mm, &_pmd, pgtable);
3024
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003025 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003026 pte_t entry, *pte;
3027 /*
3028 * Note that NUMA hinting access restrictions are not
3029 * transferred to avoid any possibility of altering
3030 * permissions across VMAs.
3031 */
Kirill A. Shutemovba988282016-01-15 16:53:56 -08003032 if (freeze) {
3033 swp_entry_t swp_entry;
3034 swp_entry = make_migration_entry(page + i, write);
3035 entry = swp_entry_to_pte(swp_entry);
3036 } else {
3037 entry = mk_pte(page + i, vma->vm_page_prot);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003038 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08003039 if (!write)
3040 entry = pte_wrprotect(entry);
3041 if (!young)
3042 entry = pte_mkold(entry);
3043 }
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003044 if (dirty)
3045 SetPageDirty(page + i);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003046 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003047 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003048 set_pte_at(mm, addr, pte, entry);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003049 atomic_inc(&page[i]._mapcount);
3050 pte_unmap(pte);
3051 }
3052
3053 /*
3054 * Set PG_double_map before dropping compound_mapcount to avoid
3055 * false-negative page_mapped().
3056 */
3057 if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) {
3058 for (i = 0; i < HPAGE_PMD_NR; i++)
3059 atomic_inc(&page[i]._mapcount);
3060 }
3061
3062 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
3063 /* Last compound_mapcount is gone. */
Kirill A. Shutemov65c45372016-07-26 15:26:10 -07003064 __dec_zone_page_state(page, NR_ANON_THPS);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003065 if (TestClearPageDoubleMap(page)) {
3066 /* No need in mapcount reference anymore */
3067 for (i = 0; i < HPAGE_PMD_NR; i++)
3068 atomic_dec(&page[i]._mapcount);
3069 }
3070 }
3071
3072 smp_wmb(); /* make pte visible before pmd */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003073 /*
3074 * Up to this point the pmd is present and huge and userland has the
3075 * whole access to the hugepage during the split (which happens in
3076 * place). If we overwrite the pmd with the not-huge version pointing
3077 * to the pte here (which of course we could if all CPUs were bug
3078 * free), userland could trigger a small page size TLB miss on the
3079 * small sized TLB while the hugepage TLB entry is still established in
3080 * the huge TLB. Some CPU doesn't like that.
3081 * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum
3082 * 383 on page 93. Intel should be safe but is also warns that it's
3083 * only safe if the permission and cache attributes of the two entries
3084 * loaded in the two TLB is identical (which should be the case here).
3085 * But it is generally safer to never allow small and huge TLB entries
3086 * for the same virtual address to be loaded simultaneously. So instead
3087 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
3088 * current pmd notpresent (atomically because here the pmd_trans_huge
3089 * and pmd_trans_splitting must remain set at all times on the pmd
3090 * until the split is complete for this pmd), then we flush the SMP TLB
3091 * and finally we write the non-huge version of the pmd entry with
3092 * pmd_populate.
3093 */
3094 pmdp_invalidate(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003095 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003096
3097 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03003098 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003099 page_remove_rmap(page + i, false);
3100 put_page(page + i);
3101 }
3102 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003103}
3104
3105void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003106 unsigned long address, bool freeze, struct page *page)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003107{
3108 spinlock_t *ptl;
3109 struct mm_struct *mm = vma->vm_mm;
3110 unsigned long haddr = address & HPAGE_PMD_MASK;
3111
3112 mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE);
3113 ptl = pmd_lock(mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003114
3115 /*
3116 * If caller asks to setup a migration entries, we need a page to check
3117 * pmd against. Otherwise we can end up replacing wrong page.
3118 */
3119 VM_BUG_ON(freeze && !page);
3120 if (page && page != pmd_page(*pmd))
3121 goto out;
3122
Dan Williams5c7fb562016-01-15 16:56:52 -08003123 if (pmd_trans_huge(*pmd)) {
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003124 page = pmd_page(*pmd);
Dan Williams5c7fb562016-01-15 16:56:52 -08003125 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07003126 clear_page_mlock(page);
Dan Williams5c7fb562016-01-15 16:56:52 -08003127 } else if (!pmd_devmap(*pmd))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003128 goto out;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003129 __split_huge_pmd_locked(vma, pmd, haddr, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08003130out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08003131 spin_unlock(ptl);
3132 mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE);
3133}
3134
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003135void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
3136 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003137{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003138 pgd_t *pgd;
3139 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003140 pmd_t *pmd;
3141
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08003142 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07003143 if (!pgd_present(*pgd))
3144 return;
3145
3146 pud = pud_offset(pgd, address);
3147 if (!pud_present(*pud))
3148 return;
3149
3150 pmd = pmd_offset(pud, address);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003151
Naoya Horiguchi33f47512016-07-14 12:07:32 -07003152 __split_huge_pmd(vma, pmd, address, freeze, page);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003153}
3154
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07003155void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003156 unsigned long start,
3157 unsigned long end,
3158 long adjust_next)
3159{
3160 /*
3161 * If the new start address isn't hpage aligned and it could
3162 * previously contain an hugepage: check if we need to split
3163 * an huge pmd.
3164 */
3165 if (start & ~HPAGE_PMD_MASK &&
3166 (start & HPAGE_PMD_MASK) >= vma->vm_start &&
3167 (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003168 split_huge_pmd_address(vma, start, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003169
3170 /*
3171 * If the new end address isn't hpage aligned and it could
3172 * previously contain an hugepage: check if we need to split
3173 * an huge pmd.
3174 */
3175 if (end & ~HPAGE_PMD_MASK &&
3176 (end & HPAGE_PMD_MASK) >= vma->vm_start &&
3177 (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003178 split_huge_pmd_address(vma, end, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003179
3180 /*
3181 * If we're also updating the vma->vm_next->vm_start, if the new
3182 * vm_next->vm_start isn't page aligned and it could previously
3183 * contain an hugepage: check if we need to split an huge pmd.
3184 */
3185 if (adjust_next > 0) {
3186 struct vm_area_struct *next = vma->vm_next;
3187 unsigned long nstart = next->vm_start;
3188 nstart += adjust_next << PAGE_SHIFT;
3189 if (nstart & ~HPAGE_PMD_MASK &&
3190 (nstart & HPAGE_PMD_MASK) >= next->vm_start &&
3191 (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end)
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003192 split_huge_pmd_address(next, nstart, false, NULL);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08003193 }
3194}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003195
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003196static void freeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003197{
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003198 enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS |
3199 TTU_RMAP_LOCKED;
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003200 int i, ret;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003201
3202 VM_BUG_ON_PAGE(!PageHead(page), page);
3203
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003204 if (PageAnon(page))
3205 ttu_flags |= TTU_MIGRATION;
3206
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003207 /* We only need TTU_SPLIT_HUGE_PMD once */
3208 ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD);
3209 for (i = 1; !ret && i < HPAGE_PMD_NR; i++) {
3210 /* Cut short if the page is unmapped */
3211 if (page_count(page) == 1)
3212 return;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003213
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003214 ret = try_to_unmap(page + i, ttu_flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003215 }
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003216 VM_BUG_ON_PAGE(ret, page + i - 1);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003217}
3218
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003219static void unfreeze_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003220{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003221 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003222
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003223 for (i = 0; i < HPAGE_PMD_NR; i++)
3224 remove_migration_ptes(page + i, page + i, true);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003225}
3226
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003227static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003228 struct lruvec *lruvec, struct list_head *list)
3229{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003230 struct page *page_tail = head + tail;
3231
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003232 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07003233 VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003234
3235 /*
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003236 * tail_page->_refcount is zero and not changing from under us. But
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003237 * get_page_unless_zero() may be running from under us on the
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003238 * tail_page. If we used atomic_set() below instead of atomic_inc() or
3239 * atomic_add(), we would then run atomic_set() concurrently with
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003240 * get_page_unless_zero(), and atomic_set() is implemented in C not
3241 * using locked ops. spin_unlock on x86 sometime uses locked ops
3242 * because of PPro errata 66, 92, so unless somebody can guarantee
3243 * atomic_set() here would be safe on all archs (and not only on x86),
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003244 * it's safer to use atomic_inc()/atomic_add().
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003245 */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003246 if (PageAnon(head)) {
3247 page_ref_inc(page_tail);
3248 } else {
3249 /* Additional pin to radix tree */
3250 page_ref_add(page_tail, 2);
3251 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003252
3253 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
3254 page_tail->flags |= (head->flags &
3255 ((1L << PG_referenced) |
3256 (1L << PG_swapbacked) |
3257 (1L << PG_mlocked) |
3258 (1L << PG_uptodate) |
3259 (1L << PG_active) |
3260 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08003261 (1L << PG_unevictable) |
3262 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003263
3264 /*
3265 * After clearing PageTail the gup refcount can be released.
3266 * Page flags also must be visible before we make the page non-compound.
3267 */
3268 smp_wmb();
3269
3270 clear_compound_head(page_tail);
3271
3272 if (page_is_young(head))
3273 set_page_young(page_tail);
3274 if (page_is_idle(head))
3275 set_page_idle(page_tail);
3276
3277 /* ->mapping in first tail page is compound_mapcount */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003278 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003279 page_tail);
3280 page_tail->mapping = head->mapping;
3281
3282 page_tail->index = head->index + tail;
3283 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
3284 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003285}
3286
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003287static void __split_huge_page(struct page *page, struct list_head *list,
3288 unsigned long flags)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003289{
3290 struct page *head = compound_head(page);
3291 struct zone *zone = page_zone(head);
3292 struct lruvec *lruvec;
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003293 pgoff_t end = -1;
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003294 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003295
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003296 lruvec = mem_cgroup_page_lruvec(head, zone);
3297
3298 /* complete memcg works before add pages to LRU */
3299 mem_cgroup_split_huge_fixup(head);
3300
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003301 if (!PageAnon(page))
3302 end = DIV_ROUND_UP(i_size_read(head->mapping->host), PAGE_SIZE);
3303
3304 for (i = HPAGE_PMD_NR - 1; i >= 1; i--) {
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07003305 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003306 /* Some pages can be beyond i_size: drop them from page cache */
3307 if (head[i].index >= end) {
3308 __ClearPageDirty(head + i);
3309 __delete_from_page_cache(head + i, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07003310 if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
3311 shmem_uncharge(head->mapping->host, 1);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003312 put_page(head + i);
3313 }
3314 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003315
3316 ClearPageCompound(head);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003317 /* See comment in __split_huge_page_tail() */
3318 if (PageAnon(head)) {
3319 page_ref_inc(head);
3320 } else {
3321 /* Additional pin to radix tree */
3322 page_ref_add(head, 2);
3323 spin_unlock(&head->mapping->tree_lock);
3324 }
3325
3326 spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003327
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003328 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003329
3330 for (i = 0; i < HPAGE_PMD_NR; i++) {
3331 struct page *subpage = head + i;
3332 if (subpage == page)
3333 continue;
3334 unlock_page(subpage);
3335
3336 /*
3337 * Subpages may be freed if there wasn't any mapping
3338 * like if add_to_swap() is running on a lru page that
3339 * had its mapping zapped. And freeing these pages
3340 * requires taking the lru_lock so we do the put_page
3341 * of the tail pages after the split is complete.
3342 */
3343 put_page(subpage);
3344 }
3345}
3346
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003347int total_mapcount(struct page *page)
3348{
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003349 int i, compound, ret;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003350
3351 VM_BUG_ON_PAGE(PageTail(page), page);
3352
3353 if (likely(!PageCompound(page)))
3354 return atomic_read(&page->_mapcount) + 1;
3355
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003356 compound = compound_mapcount(page);
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003357 if (PageHuge(page))
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003358 return compound;
3359 ret = compound;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003360 for (i = 0; i < HPAGE_PMD_NR; i++)
3361 ret += atomic_read(&page[i]._mapcount) + 1;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07003362 /* File pages has compound_mapcount included in _mapcount */
3363 if (!PageAnon(page))
3364 return ret - compound * HPAGE_PMD_NR;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08003365 if (PageDoubleMap(page))
3366 ret -= HPAGE_PMD_NR;
3367 return ret;
3368}
3369
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003370/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07003371 * This calculates accurately how many mappings a transparent hugepage
3372 * has (unlike page_mapcount() which isn't fully accurate). This full
3373 * accuracy is primarily needed to know if copy-on-write faults can
3374 * reuse the page and change the mapping to read-write instead of
3375 * copying them. At the same time this returns the total_mapcount too.
3376 *
3377 * The function returns the highest mapcount any one of the subpages
3378 * has. If the return value is one, even if different processes are
3379 * mapping different subpages of the transparent hugepage, they can
3380 * all reuse it, because each process is reusing a different subpage.
3381 *
3382 * The total_mapcount is instead counting all virtual mappings of the
3383 * subpages. If the total_mapcount is equal to "one", it tells the
3384 * caller all mappings belong to the same "mm" and in turn the
3385 * anon_vma of the transparent hugepage can become the vma->anon_vma
3386 * local one as no other process may be mapping any of the subpages.
3387 *
3388 * It would be more accurate to replace page_mapcount() with
3389 * page_trans_huge_mapcount(), however we only use
3390 * page_trans_huge_mapcount() in the copy-on-write faults where we
3391 * need full accuracy to avoid breaking page pinning, because
3392 * page_trans_huge_mapcount() is slower than page_mapcount().
3393 */
3394int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
3395{
3396 int i, ret, _total_mapcount, mapcount;
3397
3398 /* hugetlbfs shouldn't call it */
3399 VM_BUG_ON_PAGE(PageHuge(page), page);
3400
3401 if (likely(!PageTransCompound(page))) {
3402 mapcount = atomic_read(&page->_mapcount) + 1;
3403 if (total_mapcount)
3404 *total_mapcount = mapcount;
3405 return mapcount;
3406 }
3407
3408 page = compound_head(page);
3409
3410 _total_mapcount = ret = 0;
3411 for (i = 0; i < HPAGE_PMD_NR; i++) {
3412 mapcount = atomic_read(&page[i]._mapcount) + 1;
3413 ret = max(ret, mapcount);
3414 _total_mapcount += mapcount;
3415 }
3416 if (PageDoubleMap(page)) {
3417 ret -= 1;
3418 _total_mapcount -= HPAGE_PMD_NR;
3419 }
3420 mapcount = compound_mapcount(page);
3421 ret += mapcount;
3422 _total_mapcount += mapcount;
3423 if (total_mapcount)
3424 *total_mapcount = _total_mapcount;
3425 return ret;
3426}
3427
3428/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003429 * This function splits huge page into normal pages. @page can point to any
3430 * subpage of huge page to split. Split doesn't change the position of @page.
3431 *
3432 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
3433 * The huge page must be locked.
3434 *
3435 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
3436 *
3437 * Both head page and tail pages will inherit mapping, flags, and so on from
3438 * the hugepage.
3439 *
3440 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
3441 * they are not mapped.
3442 *
3443 * Returns 0 if the hugepage is split successfully.
3444 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
3445 * us.
3446 */
3447int split_huge_page_to_list(struct page *page, struct list_head *list)
3448{
3449 struct page *head = compound_head(page);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003450 struct pglist_data *pgdata = NODE_DATA(page_to_nid(head));
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003451 struct anon_vma *anon_vma = NULL;
3452 struct address_space *mapping = NULL;
3453 int count, mapcount, extra_pins, ret;
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003454 bool mlocked;
Kirill A. Shutemov0b9b6ff2016-01-20 14:58:09 -08003455 unsigned long flags;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003456
3457 VM_BUG_ON_PAGE(is_huge_zero_page(page), page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003458 VM_BUG_ON_PAGE(!PageLocked(page), page);
3459 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
3460 VM_BUG_ON_PAGE(!PageCompound(page), page);
3461
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003462 if (PageAnon(head)) {
3463 /*
3464 * The caller does not necessarily hold an mmap_sem that would
3465 * prevent the anon_vma disappearing so we first we take a
3466 * reference to it and then lock the anon_vma for write. This
3467 * is similar to page_lock_anon_vma_read except the write lock
3468 * is taken to serialise against parallel split or collapse
3469 * operations.
3470 */
3471 anon_vma = page_get_anon_vma(head);
3472 if (!anon_vma) {
3473 ret = -EBUSY;
3474 goto out;
3475 }
3476 extra_pins = 0;
3477 mapping = NULL;
3478 anon_vma_lock_write(anon_vma);
3479 } else {
3480 mapping = head->mapping;
3481
3482 /* Truncated ? */
3483 if (!mapping) {
3484 ret = -EBUSY;
3485 goto out;
3486 }
3487
3488 /* Addidional pins from radix tree */
3489 extra_pins = HPAGE_PMD_NR;
3490 anon_vma = NULL;
3491 i_mmap_lock_read(mapping);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003492 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003493
3494 /*
3495 * Racy check if we can split the page, before freeze_page() will
3496 * split PMDs
3497 */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003498 if (total_mapcount(head) != page_count(head) - extra_pins - 1) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003499 ret = -EBUSY;
3500 goto out_unlock;
3501 }
3502
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003503 mlocked = PageMlocked(page);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003504 freeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003505 VM_BUG_ON_PAGE(compound_mapcount(head), head);
3506
Kirill A. Shutemovd9654322016-01-15 16:54:43 -08003507 /* Make sure the page is not on per-CPU pagevec as it takes pin */
3508 if (mlocked)
3509 lru_add_drain();
3510
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003511 /* prevent PageLRU to go away from under us, and freeze lru stats */
3512 spin_lock_irqsave(&page_zone(head)->lru_lock, flags);
3513
3514 if (mapping) {
3515 void **pslot;
3516
3517 spin_lock(&mapping->tree_lock);
3518 pslot = radix_tree_lookup_slot(&mapping->page_tree,
3519 page_index(head));
3520 /*
3521 * Check if the head page is present in radix tree.
3522 * We assume all tail are present too, if head is there.
3523 */
3524 if (radix_tree_deref_slot_protected(pslot,
3525 &mapping->tree_lock) != head)
3526 goto fail;
3527 }
3528
Joonsoo Kim0139aa72016-05-19 17:10:49 -07003529 /* Prevent deferred_split_scan() touching ->_refcount */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003530 spin_lock(&pgdata->split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003531 count = page_count(head);
3532 mapcount = total_mapcount(head);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003533 if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003534 if (!list_empty(page_deferred_list(head))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003535 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003536 list_del(page_deferred_list(head));
3537 }
Kirill A. Shutemov65c45372016-07-26 15:26:10 -07003538 if (mapping)
3539 __dec_zone_page_state(page, NR_SHMEM_THPS);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003540 spin_unlock(&pgdata->split_queue_lock);
3541 __split_huge_page(page, list, flags);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003542 ret = 0;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003543 } else {
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003544 if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
3545 pr_alert("total_mapcount: %u, page_count(): %u\n",
3546 mapcount, count);
3547 if (PageTail(page))
3548 dump_page(head, NULL);
3549 dump_page(page, "total_mapcount(head) > 0");
3550 BUG();
3551 }
3552 spin_unlock(&pgdata->split_queue_lock);
3553fail: if (mapping)
3554 spin_unlock(&mapping->tree_lock);
3555 spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07003556 unfreeze_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003557 ret = -EBUSY;
3558 }
3559
3560out_unlock:
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003561 if (anon_vma) {
3562 anon_vma_unlock_write(anon_vma);
3563 put_anon_vma(anon_vma);
3564 }
3565 if (mapping)
3566 i_mmap_unlock_read(mapping);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08003567out:
3568 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
3569 return ret;
3570}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003571
3572void free_transhuge_page(struct page *page)
3573{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003574 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003575 unsigned long flags;
3576
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003577 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003578 if (!list_empty(page_deferred_list(page))) {
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003579 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003580 list_del(page_deferred_list(page));
3581 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003582 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003583 free_compound_page(page);
3584}
3585
3586void deferred_split_huge_page(struct page *page)
3587{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003588 struct pglist_data *pgdata = NODE_DATA(page_to_nid(page));
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003589 unsigned long flags;
3590
3591 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
3592
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003593 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003594 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07003595 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003596 list_add_tail(page_deferred_list(page), &pgdata->split_queue);
3597 pgdata->split_queue_len++;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003598 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003599 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003600}
3601
3602static unsigned long deferred_split_count(struct shrinker *shrink,
3603 struct shrink_control *sc)
3604{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003605 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003606 return ACCESS_ONCE(pgdata->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003607}
3608
3609static unsigned long deferred_split_scan(struct shrinker *shrink,
3610 struct shrink_control *sc)
3611{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003612 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003613 unsigned long flags;
3614 LIST_HEAD(list), *pos, *next;
3615 struct page *page;
3616 int split = 0;
3617
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003618 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003619 /* Take pin on all head pages to avoid freeing them under us */
Kirill A. Shutemovae026202016-02-05 15:36:53 -08003620 list_for_each_safe(pos, next, &pgdata->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003621 page = list_entry((void *)pos, struct page, mapping);
3622 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003623 if (get_page_unless_zero(page)) {
3624 list_move(page_deferred_list(page), &list);
3625 } else {
3626 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003627 list_del_init(page_deferred_list(page));
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003628 pgdata->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003629 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08003630 if (!--sc->nr_to_scan)
3631 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003632 }
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003633 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003634
3635 list_for_each_safe(pos, next, &list) {
3636 page = list_entry((void *)pos, struct page, mapping);
3637 lock_page(page);
3638 /* split_huge_page() removes page from list on success */
3639 if (!split_huge_page(page))
3640 split++;
3641 unlock_page(page);
3642 put_page(page);
3643 }
3644
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003645 spin_lock_irqsave(&pgdata->split_queue_lock, flags);
3646 list_splice_tail(&list, &pgdata->split_queue);
3647 spin_unlock_irqrestore(&pgdata->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003648
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08003649 /*
3650 * Stop shrinker if we didn't split any page, but the queue is empty.
3651 * This can happen if pages were freed under us.
3652 */
3653 if (!split && list_empty(&pgdata->split_queue))
3654 return SHRINK_STOP;
3655 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003656}
3657
3658static struct shrinker deferred_split_shrinker = {
3659 .count_objects = deferred_split_count,
3660 .scan_objects = deferred_split_scan,
3661 .seeks = DEFAULT_SEEKS,
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08003662 .flags = SHRINKER_NUMA_AWARE,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08003663};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003664
3665#ifdef CONFIG_DEBUG_FS
3666static int split_huge_pages_set(void *data, u64 val)
3667{
3668 struct zone *zone;
3669 struct page *page;
3670 unsigned long pfn, max_zone_pfn;
3671 unsigned long total = 0, split = 0;
3672
3673 if (val != 1)
3674 return -EINVAL;
3675
3676 for_each_populated_zone(zone) {
3677 max_zone_pfn = zone_end_pfn(zone);
3678 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
3679 if (!pfn_valid(pfn))
3680 continue;
3681
3682 page = pfn_to_page(pfn);
3683 if (!get_page_unless_zero(page))
3684 continue;
3685
3686 if (zone != page_zone(page))
3687 goto next;
3688
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07003689 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003690 goto next;
3691
3692 total++;
3693 lock_page(page);
3694 if (!split_huge_page(page))
3695 split++;
3696 unlock_page(page);
3697next:
3698 put_page(page);
3699 }
3700 }
3701
Yang Shi145bdaa2016-05-05 16:22:00 -07003702 pr_info("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003703
3704 return 0;
3705}
3706DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set,
3707 "%llu\n");
3708
3709static int __init split_huge_pages_debugfs(void)
3710{
3711 void *ret;
3712
Yang Shi145bdaa2016-05-05 16:22:00 -07003713 ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003714 &split_huge_pages_fops);
3715 if (!ret)
3716 pr_warn("Failed to create split_huge_pages in debugfs");
3717 return 0;
3718}
3719late_initcall(split_huge_pages_debugfs);
3720#endif