blob: b3788683f7d331d045711a85c78c47ccf899e0fe [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08002/*
3 * Copyright (C) 2009 Red Hat, Inc.
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08004 */
5
Andrew Mortonae3a8c12014-06-04 16:06:58 -07006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08008#include <linux/mm.h>
9#include <linux/sched.h>
Zi Yanfa6c0232021-05-04 18:34:23 -070010#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010011#include <linux/sched/coredump.h>
Ingo Molnar6a3827d2017-02-08 18:51:31 +010012#include <linux/sched/numa_balancing.h>
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080013#include <linux/highmem.h>
14#include <linux/hugetlb.h>
15#include <linux/mmu_notifier.h>
16#include <linux/rmap.h>
17#include <linux/swap.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080018#include <linux/shrinker.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080019#include <linux/mm_inline.h>
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -080020#include <linux/swapops.h>
Matthew Wilcox4897c762015-09-08 14:58:45 -070021#include <linux/dax.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080022#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>
Michal Hocko6b31d592017-08-18 15:16:15 -070034#include <linux/oom.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080035#include <linux/numa.h>
Vlastimil Babkaf7da6772019-08-24 17:54:59 -070036#include <linux/page_owner.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080037
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080038#include <asm/tlb.h>
39#include <asm/pgalloc.h>
40#include "internal.h"
41
Andrea Arcangeliba761492011-01-13 15:46:58 -080042/*
Michael DeGuzisb14d5952017-05-17 15:19:21 -040043 * By default, transparent hugepage support is disabled in order to avoid
44 * risking an increased memory footprint for applications that are not
45 * guaranteed to benefit from it. When transparent hugepage support is
46 * enabled, it is for all mappings, and khugepaged scans all mappings.
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080047 * Defrag is invoked by khugepaged hugepage allocations and by page faults
48 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080049 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080050unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080051#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080052 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080053#endif
54#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
55 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
56#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070057 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080058 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
59 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080060
Kirill A. Shutemov9a982252016-01-15 16:54:17 -080061static struct shrinker deferred_split_shrinker;
Andrea Arcangelif0005652011-01-13 15:47:04 -080062
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080063static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -080064struct page *huge_zero_page __read_mostly;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -080065
Michal Hocko7635d9c2018-12-28 00:38:21 -080066bool transparent_hugepage_enabled(struct vm_area_struct *vma)
67{
Yang Shic0630662019-07-18 15:57:27 -070068 /* The addr is used to check if the vma size fits */
69 unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
70
71 if (!transhuge_vma_suitable(vma, addr))
72 return false;
Michal Hocko7635d9c2018-12-28 00:38:21 -080073 if (vma_is_anonymous(vma))
74 return __transparent_hugepage_enabled(vma);
Yang Shic0630662019-07-18 15:57:27 -070075 if (vma_is_shmem(vma))
76 return shmem_huge_enabled(vma);
Michal Hocko7635d9c2018-12-28 00:38:21 -080077
78 return false;
79}
80
Miaohe Linaaa97052021-05-04 18:33:55 -070081static bool get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080082{
83 struct page *zero_page;
84retry:
85 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Miaohe Linaaa97052021-05-04 18:33:55 -070086 return true;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080087
88 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
89 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -080090 if (!zero_page) {
91 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Miaohe Linaaa97052021-05-04 18:33:55 -070092 return false;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -080093 }
94 count_vm_event(THP_ZERO_PAGE_ALLOC);
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080095 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -070096 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080097 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -070098 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080099 goto retry;
100 }
101
102 /* We take additional reference here. It will be put back by shrinker */
103 atomic_set(&huge_zero_refcount, 2);
104 preempt_enable();
Miaohe Linaaa97052021-05-04 18:33:55 -0700105 return true;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800106}
107
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700108static void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800109{
110 /*
111 * Counter should never go to zero here. Only shrinker can put
112 * last reference.
113 */
114 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
115}
116
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700117struct page *mm_get_huge_zero_page(struct mm_struct *mm)
118{
119 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
120 return READ_ONCE(huge_zero_page);
121
122 if (!get_huge_zero_page())
123 return NULL;
124
125 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
126 put_huge_zero_page();
127
128 return READ_ONCE(huge_zero_page);
129}
130
131void mm_put_huge_zero_page(struct mm_struct *mm)
132{
133 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
134 put_huge_zero_page();
135}
136
Glauber Costa48896462013-08-28 10:18:15 +1000137static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
138 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800139{
Glauber Costa48896462013-08-28 10:18:15 +1000140 /* we can free zero page only if last reference remains */
141 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
142}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800143
Glauber Costa48896462013-08-28 10:18:15 +1000144static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
145 struct shrink_control *sc)
146{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800147 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700148 struct page *zero_page = xchg(&huge_zero_page, NULL);
149 BUG_ON(zero_page == NULL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700150 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000151 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800152 }
153
154 return 0;
155}
156
157static struct shrinker huge_zero_page_shrinker = {
Glauber Costa48896462013-08-28 10:18:15 +1000158 .count_objects = shrink_huge_zero_page_count,
159 .scan_objects = shrink_huge_zero_page_scan,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800160 .seeks = DEFAULT_SEEKS,
161};
162
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800163#ifdef CONFIG_SYSFS
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800164static ssize_t enabled_show(struct kobject *kobj,
165 struct kobj_attribute *attr, char *buf)
166{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800167 const char *output;
168
Mel Gorman444eb2a42016-03-17 14:19:23 -0700169 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800170 output = "[always] madvise never";
171 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
172 &transparent_hugepage_flags))
173 output = "always [madvise] never";
Mel Gorman444eb2a42016-03-17 14:19:23 -0700174 else
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800175 output = "always madvise [never]";
176
177 return sysfs_emit(buf, "%s\n", output);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800178}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700179
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800180static ssize_t enabled_store(struct kobject *kobj,
181 struct kobj_attribute *attr,
182 const char *buf, size_t count)
183{
David Rientjes21440d72017-02-22 15:45:49 -0800184 ssize_t ret = count;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800185
David Rientjesf42f2552020-01-30 22:14:48 -0800186 if (sysfs_streq(buf, "always")) {
David Rientjes21440d72017-02-22 15:45:49 -0800187 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
188 set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800189 } else if (sysfs_streq(buf, "madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800190 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
191 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800192 } else if (sysfs_streq(buf, "never")) {
David Rientjes21440d72017-02-22 15:45:49 -0800193 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
194 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
195 } else
196 ret = -EINVAL;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800197
198 if (ret > 0) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700199 int err = start_stop_khugepaged();
Andrea Arcangeliba761492011-01-13 15:46:58 -0800200 if (err)
201 ret = err;
202 }
Andrea Arcangeliba761492011-01-13 15:46:58 -0800203 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800204}
205static struct kobj_attribute enabled_attr =
206 __ATTR(enabled, 0644, enabled_show, enabled_store);
207
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700208ssize_t single_hugepage_flag_show(struct kobject *kobj,
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800209 struct kobj_attribute *attr, char *buf,
210 enum transparent_hugepage_flag flag)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800211{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800212 return sysfs_emit(buf, "%d\n",
213 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800214}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700215
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700216ssize_t single_hugepage_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800217 struct kobj_attribute *attr,
218 const char *buf, size_t count,
219 enum transparent_hugepage_flag flag)
220{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700221 unsigned long value;
222 int ret;
223
224 ret = kstrtoul(buf, 10, &value);
225 if (ret < 0)
226 return ret;
227 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800228 return -EINVAL;
229
Ben Hutchingse27e6152011-04-14 15:22:21 -0700230 if (value)
231 set_bit(flag, &transparent_hugepage_flags);
232 else
233 clear_bit(flag, &transparent_hugepage_flags);
234
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800235 return count;
236}
237
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800238static ssize_t defrag_show(struct kobject *kobj,
239 struct kobj_attribute *attr, char *buf)
240{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800241 const char *output;
242
243 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
244 &transparent_hugepage_flags))
245 output = "[always] defer defer+madvise madvise never";
246 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
247 &transparent_hugepage_flags))
248 output = "always [defer] defer+madvise madvise never";
249 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
250 &transparent_hugepage_flags))
251 output = "always defer [defer+madvise] madvise never";
252 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
253 &transparent_hugepage_flags))
254 output = "always defer defer+madvise [madvise] never";
255 else
256 output = "always defer defer+madvise madvise [never]";
257
258 return sysfs_emit(buf, "%s\n", output);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800259}
David Rientjes21440d72017-02-22 15:45:49 -0800260
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800261static ssize_t defrag_store(struct kobject *kobj,
262 struct kobj_attribute *attr,
263 const char *buf, size_t count)
264{
David Rientjesf42f2552020-01-30 22:14:48 -0800265 if (sysfs_streq(buf, "always")) {
David Rientjes21440d72017-02-22 15:45:49 -0800266 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
267 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
268 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
269 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800270 } else if (sysfs_streq(buf, "defer+madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800271 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
272 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
273 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
274 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800275 } else if (sysfs_streq(buf, "defer")) {
David Rientjes4fad7fb2017-04-07 16:04:54 -0700276 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
277 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
278 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
279 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800280 } else if (sysfs_streq(buf, "madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800281 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
282 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
283 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
284 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800285 } else if (sysfs_streq(buf, "never")) {
David Rientjes21440d72017-02-22 15:45:49 -0800286 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
287 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
288 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
289 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
290 } else
291 return -EINVAL;
292
293 return count;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800294}
295static struct kobj_attribute defrag_attr =
296 __ATTR(defrag, 0644, defrag_show, defrag_store);
297
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800298static ssize_t use_zero_page_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -0800299 struct kobj_attribute *attr, char *buf)
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800300{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700301 return single_hugepage_flag_show(kobj, attr, buf,
Joe Perchesae7a9272020-12-14 19:14:42 -0800302 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800303}
304static ssize_t use_zero_page_store(struct kobject *kobj,
305 struct kobj_attribute *attr, const char *buf, size_t count)
306{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700307 return single_hugepage_flag_store(kobj, attr, buf, count,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800308 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
309}
310static struct kobj_attribute use_zero_page_attr =
311 __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store);
Hugh Dickins49920d22016-12-12 16:44:50 -0800312
313static ssize_t hpage_pmd_size_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -0800314 struct kobj_attribute *attr, char *buf)
Hugh Dickins49920d22016-12-12 16:44:50 -0800315{
Joe Perchesae7a9272020-12-14 19:14:42 -0800316 return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE);
Hugh Dickins49920d22016-12-12 16:44:50 -0800317}
318static struct kobj_attribute hpage_pmd_size_attr =
319 __ATTR_RO(hpage_pmd_size);
320
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800321static struct attribute *hugepage_attr[] = {
322 &enabled_attr.attr,
323 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800324 &use_zero_page_attr.attr,
Hugh Dickins49920d22016-12-12 16:44:50 -0800325 &hpage_pmd_size_attr.attr,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700326#ifdef CONFIG_SHMEM
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700327 &shmem_enabled_attr.attr,
328#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800329 NULL,
330};
331
Arvind Yadav8aa95a22017-09-06 16:22:03 -0700332static const struct attribute_group hugepage_attr_group = {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800333 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800334};
335
Shaohua Li569e5592012-01-12 17:19:11 -0800336static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
337{
338 int err;
339
340 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
341 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700342 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800343 return -ENOMEM;
344 }
345
346 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
347 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700348 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800349 goto delete_obj;
350 }
351
352 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
353 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700354 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800355 goto remove_hp_group;
356 }
357
358 return 0;
359
360remove_hp_group:
361 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
362delete_obj:
363 kobject_put(*hugepage_kobj);
364 return err;
365}
366
367static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
368{
369 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
370 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
371 kobject_put(hugepage_kobj);
372}
373#else
374static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
375{
376 return 0;
377}
378
379static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
380{
381}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800382#endif /* CONFIG_SYSFS */
383
384static int __init hugepage_init(void)
385{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800386 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800387 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800388
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800389 if (!has_transparent_hugepage()) {
Aneesh Kumar K.Vbae84952021-02-24 12:07:32 -0800390 /*
391 * Hardware doesn't support hugepages, hence disable
392 * DAX PMD support.
393 */
394 transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_NEVER_DAX;
Shaohua Li569e5592012-01-12 17:19:11 -0800395 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800396 }
397
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530398 /*
399 * hugepages can't be allocated by the buddy allocator
400 */
401 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER);
402 /*
403 * we use page->mapping and page->index in second tail page
404 * as list_head: assuming THP order >= 2
405 */
406 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
407
Shaohua Li569e5592012-01-12 17:19:11 -0800408 err = hugepage_init_sysfs(&hugepage_kobj);
409 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700410 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800411
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700412 err = khugepaged_init();
Andrea Arcangeliba761492011-01-13 15:46:58 -0800413 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700414 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800415
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700416 err = register_shrinker(&huge_zero_page_shrinker);
417 if (err)
418 goto err_hzp_shrinker;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800419 err = register_shrinker(&deferred_split_shrinker);
420 if (err)
421 goto err_split_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800422
Rik van Riel97562cd2011-01-13 15:47:12 -0800423 /*
424 * By default disable transparent hugepages on smaller systems,
425 * where the extra memory used could hurt more than TLB overhead
426 * is likely to save. The admin can still enable it through /sys.
427 */
Arun KSca79b0c2018-12-28 00:34:29 -0800428 if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800429 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700430 return 0;
431 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800432
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700433 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700434 if (err)
435 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800436
Shaohua Li569e5592012-01-12 17:19:11 -0800437 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700438err_khugepaged:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800439 unregister_shrinker(&deferred_split_shrinker);
440err_split_shrinker:
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700441 unregister_shrinker(&huge_zero_page_shrinker);
442err_hzp_shrinker:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700443 khugepaged_destroy();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700444err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800445 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700446err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800447 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800448}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800449subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800450
451static int __init setup_transparent_hugepage(char *str)
452{
453 int ret = 0;
454 if (!str)
455 goto out;
456 if (!strcmp(str, "always")) {
457 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
458 &transparent_hugepage_flags);
459 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
460 &transparent_hugepage_flags);
461 ret = 1;
462 } else if (!strcmp(str, "madvise")) {
463 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
464 &transparent_hugepage_flags);
465 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
466 &transparent_hugepage_flags);
467 ret = 1;
468 } else if (!strcmp(str, "never")) {
469 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
470 &transparent_hugepage_flags);
471 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
472 &transparent_hugepage_flags);
473 ret = 1;
474 }
475out:
476 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700477 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800478 return ret;
479}
480__setup("transparent_hugepage=", setup_transparent_hugepage);
481
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800482pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800483{
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800484 if (likely(vma->vm_flags & VM_WRITE))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800485 pmd = pmd_mkwrite(pmd);
486 return pmd;
487}
488
Yang Shi87eaceb2019-09-23 15:38:15 -0700489#ifdef CONFIG_MEMCG
490static inline struct deferred_split *get_deferred_split_queue(struct page *page)
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800491{
Roman Gushchinbcfe06b2020-12-01 13:58:27 -0800492 struct mem_cgroup *memcg = page_memcg(compound_head(page));
Yang Shi87eaceb2019-09-23 15:38:15 -0700493 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
494
495 if (memcg)
496 return &memcg->deferred_split_queue;
497 else
498 return &pgdat->deferred_split_queue;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800499}
Yang Shi87eaceb2019-09-23 15:38:15 -0700500#else
501static inline struct deferred_split *get_deferred_split_queue(struct page *page)
502{
503 struct pglist_data *pgdat = NODE_DATA(page_to_nid(page));
504
505 return &pgdat->deferred_split_queue;
506}
507#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800508
509void prep_transhuge_page(struct page *page)
510{
511 /*
512 * we use page->mapping and page->indexlru in second tail page
513 * as list_head: assuming THP order >= 2
514 */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800515
516 INIT_LIST_HEAD(page_deferred_list(page));
517 set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR);
518}
519
Sean Christopherson005ba372020-01-08 12:24:36 -0800520bool is_transparent_hugepage(struct page *page)
521{
522 if (!PageCompound(page))
Zou Weifa1f68c2020-06-04 16:49:46 -0700523 return false;
Sean Christopherson005ba372020-01-08 12:24:36 -0800524
525 page = compound_head(page);
526 return is_huge_zero_page(page) ||
527 page[1].compound_dtor == TRANSHUGE_PAGE_DTOR;
528}
529EXPORT_SYMBOL_GPL(is_transparent_hugepage);
530
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800531static unsigned long __thp_get_unmapped_area(struct file *filp,
532 unsigned long addr, unsigned long len,
Toshi Kani74d2fad2016-10-07 16:59:56 -0700533 loff_t off, unsigned long flags, unsigned long size)
534{
Toshi Kani74d2fad2016-10-07 16:59:56 -0700535 loff_t off_end = off + len;
536 loff_t off_align = round_up(off, size);
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800537 unsigned long len_pad, ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700538
539 if (off_end <= off_align || (off_end - off_align) < size)
540 return 0;
541
542 len_pad = len + size;
543 if (len_pad < len || (off + len_pad) < off)
544 return 0;
545
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800546 ret = current->mm->get_unmapped_area(filp, addr, len_pad,
Toshi Kani74d2fad2016-10-07 16:59:56 -0700547 off >> PAGE_SHIFT, flags);
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800548
549 /*
550 * The failure might be due to length padding. The caller will retry
551 * without the padding.
552 */
553 if (IS_ERR_VALUE(ret))
Toshi Kani74d2fad2016-10-07 16:59:56 -0700554 return 0;
555
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800556 /*
557 * Do not try to align to THP boundary if allocation at the address
558 * hint succeeds.
559 */
560 if (ret == addr)
561 return addr;
562
563 ret += (off - ret) & (size - 1);
564 return ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700565}
566
567unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
568 unsigned long len, unsigned long pgoff, unsigned long flags)
569{
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800570 unsigned long ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700571 loff_t off = (loff_t)pgoff << PAGE_SHIFT;
572
Toshi Kani74d2fad2016-10-07 16:59:56 -0700573 if (!IS_DAX(filp->f_mapping->host) || !IS_ENABLED(CONFIG_FS_DAX_PMD))
574 goto out;
575
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800576 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
577 if (ret)
578 return ret;
579out:
Toshi Kani74d2fad2016-10-07 16:59:56 -0700580 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
581}
582EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
583
Souptick Joarder2b740302018-08-23 17:01:36 -0700584static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
585 struct page *page, gfp_t gfp)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800586{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800587 struct vm_area_struct *vma = vmf->vma;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800588 pgtable_t pgtable;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800589 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Souptick Joarder2b740302018-08-23 17:01:36 -0700590 vm_fault_t ret = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800591
Sasha Levin309381fea2014-01-23 15:52:54 -0800592 VM_BUG_ON_PAGE(!PageCompound(page), page);
Johannes Weiner00501b52014-08-08 14:19:20 -0700593
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -0700594 if (mem_cgroup_charge(page, vma->vm_mm, gfp)) {
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700595 put_page(page);
596 count_vm_event(THP_FAULT_FALLBACK);
David Rientjes85b9f462020-04-06 20:04:28 -0700597 count_vm_event(THP_FAULT_FALLBACK_CHARGE);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700598 return VM_FAULT_FALLBACK;
599 }
Johannes Weiner9d82c692020-06-03 16:02:04 -0700600 cgroup_throttle_swaprate(page, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800601
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800602 pgtable = pte_alloc_one(vma->vm_mm);
Johannes Weiner00501b52014-08-08 14:19:20 -0700603 if (unlikely(!pgtable)) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700604 ret = VM_FAULT_OOM;
605 goto release;
Johannes Weiner00501b52014-08-08 14:19:20 -0700606 }
607
Huang Yingc79b57e2017-09-06 16:25:04 -0700608 clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700609 /*
610 * The memory barrier inside __SetPageUptodate makes sure that
611 * clear_huge_page writes become visible before the set_pmd_at()
612 * write.
613 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800614 __SetPageUptodate(page);
615
Jan Kara82b0f8c2016-12-14 15:06:58 -0800616 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
617 if (unlikely(!pmd_none(*vmf->pmd))) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700618 goto unlock_release;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800619 } else {
620 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700621
Michal Hocko6b31d592017-08-18 15:16:15 -0700622 ret = check_stable_address_space(vma->vm_mm);
623 if (ret)
624 goto unlock_release;
625
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700626 /* Deliver the page fault to userland */
627 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800628 spin_unlock(vmf->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700629 put_page(page);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700630 pte_free(vma->vm_mm, pgtable);
Miaohe Lin8fd5eda2021-05-04 18:33:49 -0700631 ret = handle_userfault(vmf, VM_UFFD_MISSING);
632 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
633 return ret;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700634 }
635
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700636 entry = mk_huge_pmd(page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800637 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemovd281ee62016-01-15 16:52:16 -0800638 page_add_new_anon_rmap(page, vma, haddr, true);
Joonsoo Kimb5181542020-08-11 18:30:40 -0700639 lru_cache_add_inactive_or_unevictable(page, vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800640 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
641 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Bibo Maofca40572021-02-24 12:06:42 -0800642 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700643 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800644 mm_inc_nr_ptes(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800645 spin_unlock(vmf->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700646 count_vm_event(THP_FAULT_ALLOC);
Johannes Weiner9d82c692020-06-03 16:02:04 -0700647 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800648 }
649
David Rientjesaa2e8782012-05-29 15:06:17 -0700650 return 0;
Michal Hocko6b31d592017-08-18 15:16:15 -0700651unlock_release:
652 spin_unlock(vmf->ptl);
653release:
654 if (pgtable)
655 pte_free(vma->vm_mm, pgtable);
Michal Hocko6b31d592017-08-18 15:16:15 -0700656 put_page(page);
657 return ret;
658
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800659}
660
Mel Gorman444eb2a42016-03-17 14:19:23 -0700661/*
David Rientjes21440d72017-02-22 15:45:49 -0800662 * always: directly stall for all thp allocations
663 * defer: wake kswapd and fail if not immediately available
664 * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
665 * fail if not immediately available
666 * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
667 * available
668 * never: never stall for any thp allocation
Mel Gorman444eb2a42016-03-17 14:19:23 -0700669 */
Rik van Riel164cc4f2021-02-25 17:16:18 -0800670gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800671{
Rik van Riel164cc4f2021-02-25 17:16:18 -0800672 const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
Michal Hocko89c83fb2018-11-02 15:48:31 -0700673
David Rientjesac79f782019-09-04 12:54:18 -0700674 /* Always do synchronous compaction */
David Rientjes21440d72017-02-22 15:45:49 -0800675 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
Andrea Arcangelia8282602019-08-13 15:37:53 -0700676 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
David Rientjesac79f782019-09-04 12:54:18 -0700677
678 /* Kick kcompactd and fail quickly */
David Rientjes21440d72017-02-22 15:45:49 -0800679 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700680 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
David Rientjesac79f782019-09-04 12:54:18 -0700681
682 /* Synchronous compaction if madvised, otherwise kick kcompactd */
David Rientjes21440d72017-02-22 15:45:49 -0800683 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700684 return GFP_TRANSHUGE_LIGHT |
685 (vma_madvised ? __GFP_DIRECT_RECLAIM :
686 __GFP_KSWAPD_RECLAIM);
David Rientjesac79f782019-09-04 12:54:18 -0700687
688 /* Only do synchronous compaction if madvised */
David Rientjes21440d72017-02-22 15:45:49 -0800689 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700690 return GFP_TRANSHUGE_LIGHT |
691 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
David Rientjesac79f782019-09-04 12:54:18 -0700692
David Rientjes19deb762019-09-04 12:54:20 -0700693 return GFP_TRANSHUGE_LIGHT;
Mel Gorman444eb2a42016-03-17 14:19:23 -0700694}
695
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800696/* Caller must hold page table lock. */
Miaohe Lin2efeb8d2021-02-24 12:07:29 -0800697static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800698 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700699 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800700{
701 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700702 if (!pmd_none(*pmd))
Miaohe Lin2efeb8d2021-02-24 12:07:29 -0800703 return;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700704 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800705 entry = pmd_mkhuge(entry);
Matthew Wilcox12c9d702016-02-02 16:57:57 -0800706 if (pgtable)
707 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800708 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800709 mm_inc_nr_ptes(mm);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800710}
711
Souptick Joarder2b740302018-08-23 17:01:36 -0700712vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800713{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800714 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800715 gfp_t gfp;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800716 struct page *page;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800717 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800718
Yang Shi43675e62019-07-18 15:57:24 -0700719 if (!transhuge_vma_suitable(vma, haddr))
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700720 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700721 if (unlikely(anon_vma_prepare(vma)))
722 return VM_FAULT_OOM;
David Rientjes6d50e602014-10-29 14:50:31 -0700723 if (unlikely(khugepaged_enter(vma, vma->vm_flags)))
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700724 return VM_FAULT_OOM;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800725 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700726 !mm_forbids_zeropage(vma->vm_mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700727 transparent_hugepage_use_zero_page()) {
728 pgtable_t pgtable;
729 struct page *zero_page;
Souptick Joarder2b740302018-08-23 17:01:36 -0700730 vm_fault_t ret;
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800731 pgtable = pte_alloc_one(vma->vm_mm);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700732 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800733 return VM_FAULT_OOM;
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700734 zero_page = mm_get_huge_zero_page(vma->vm_mm);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700735 if (unlikely(!zero_page)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700736 pte_free(vma->vm_mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700737 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700738 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700739 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800740 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700741 ret = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800742 if (pmd_none(*vmf->pmd)) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700743 ret = check_stable_address_space(vma->vm_mm);
744 if (ret) {
745 spin_unlock(vmf->ptl);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800746 pte_free(vma->vm_mm, pgtable);
Michal Hocko6b31d592017-08-18 15:16:15 -0700747 } else if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800748 spin_unlock(vmf->ptl);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800749 pte_free(vma->vm_mm, pgtable);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800750 ret = handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700751 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
752 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700753 set_huge_zero_page(pgtable, vma->vm_mm, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -0800754 haddr, vmf->pmd, zero_page);
Bibo Maofca40572021-02-24 12:06:42 -0800755 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800756 spin_unlock(vmf->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700757 }
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800758 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800759 spin_unlock(vmf->ptl);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700760 pte_free(vma->vm_mm, pgtable);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800761 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700762 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800763 }
Rik van Riel164cc4f2021-02-25 17:16:18 -0800764 gfp = vma_thp_gfp_mask(vma);
David Rientjes19deb762019-09-04 12:54:20 -0700765 page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700766 if (unlikely(!page)) {
767 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700768 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700769 }
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800770 prep_transhuge_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800771 return __do_huge_pmd_anonymous_page(vmf, page, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800772}
773
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700774static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700775 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
776 pgtable_t pgtable)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700777{
778 struct mm_struct *mm = vma->vm_mm;
779 pmd_t entry;
780 spinlock_t *ptl;
781
782 ptl = pmd_lock(mm, pmd);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700783 if (!pmd_none(*pmd)) {
784 if (write) {
785 if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
786 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
787 goto out_unlock;
788 }
789 entry = pmd_mkyoung(*pmd);
790 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
791 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
792 update_mmu_cache_pmd(vma, addr, pmd);
793 }
794
795 goto out_unlock;
796 }
797
Dan Williamsf25748e32016-01-15 16:56:43 -0800798 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
799 if (pfn_t_devmap(pfn))
800 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -0800801 if (write) {
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800802 entry = pmd_mkyoung(pmd_mkdirty(entry));
803 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700804 }
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700805
806 if (pgtable) {
807 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800808 mm_inc_nr_ptes(mm);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700809 pgtable = NULL;
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700810 }
811
Ross Zwisler01871e52016-01-15 16:56:02 -0800812 set_pmd_at(mm, addr, pmd, entry);
813 update_mmu_cache_pmd(vma, addr, pmd);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700814
815out_unlock:
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700816 spin_unlock(ptl);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700817 if (pgtable)
818 pte_free(mm, pgtable);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700819}
820
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100821/**
822 * vmf_insert_pfn_pmd_prot - insert a pmd size pfn
823 * @vmf: Structure describing the fault
824 * @pfn: pfn to insert
825 * @pgprot: page protection to use
826 * @write: whether it's a write fault
827 *
828 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info and
829 * also consult the vmf_insert_mixed_prot() documentation when
830 * @pgprot != @vmf->vma->vm_page_prot.
831 *
832 * Return: vm_fault_t value.
833 */
834vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn,
835 pgprot_t pgprot, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700836{
Dan Williamsfce86ff2019-05-13 17:15:33 -0700837 unsigned long addr = vmf->address & PMD_MASK;
838 struct vm_area_struct *vma = vmf->vma;
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700839 pgtable_t pgtable = NULL;
Dan Williamsfce86ff2019-05-13 17:15:33 -0700840
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700841 /*
842 * If we had pmd_special, we could avoid all these restrictions,
843 * but we need to be consistent with PTEs and architectures that
844 * can't support a 'special' bit.
845 */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700846 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
847 !pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700848 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
849 (VM_PFNMAP|VM_MIXEDMAP));
850 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700851
852 if (addr < vma->vm_start || addr >= vma->vm_end)
853 return VM_FAULT_SIGBUS;
Borislav Petkov308a0472016-10-26 19:43:43 +0200854
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700855 if (arch_needs_pgtable_deposit()) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800856 pgtable = pte_alloc_one(vma->vm_mm);
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700857 if (!pgtable)
858 return VM_FAULT_OOM;
859 }
860
Borislav Petkov308a0472016-10-26 19:43:43 +0200861 track_pfn_insert(vma, &pgprot, pfn);
862
Dan Williamsfce86ff2019-05-13 17:15:33 -0700863 insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700864 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700865}
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100866EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd_prot);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700867
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800868#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800869static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800870{
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800871 if (likely(vma->vm_flags & VM_WRITE))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800872 pud = pud_mkwrite(pud);
873 return pud;
874}
875
876static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
877 pud_t *pud, pfn_t pfn, pgprot_t prot, bool write)
878{
879 struct mm_struct *mm = vma->vm_mm;
880 pud_t entry;
881 spinlock_t *ptl;
882
883 ptl = pud_lock(mm, pud);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700884 if (!pud_none(*pud)) {
885 if (write) {
886 if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
887 WARN_ON_ONCE(!is_huge_zero_pud(*pud));
888 goto out_unlock;
889 }
890 entry = pud_mkyoung(*pud);
891 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
892 if (pudp_set_access_flags(vma, addr, pud, entry, 1))
893 update_mmu_cache_pud(vma, addr, pud);
894 }
895 goto out_unlock;
896 }
897
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800898 entry = pud_mkhuge(pfn_t_pud(pfn, prot));
899 if (pfn_t_devmap(pfn))
900 entry = pud_mkdevmap(entry);
901 if (write) {
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800902 entry = pud_mkyoung(pud_mkdirty(entry));
903 entry = maybe_pud_mkwrite(entry, vma);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800904 }
905 set_pud_at(mm, addr, pud, entry);
906 update_mmu_cache_pud(vma, addr, pud);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700907
908out_unlock:
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800909 spin_unlock(ptl);
910}
911
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100912/**
913 * vmf_insert_pfn_pud_prot - insert a pud size pfn
914 * @vmf: Structure describing the fault
915 * @pfn: pfn to insert
916 * @pgprot: page protection to use
917 * @write: whether it's a write fault
918 *
919 * Insert a pud size pfn. See vmf_insert_pfn() for additional info and
920 * also consult the vmf_insert_mixed_prot() documentation when
921 * @pgprot != @vmf->vma->vm_page_prot.
922 *
923 * Return: vm_fault_t value.
924 */
925vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn,
926 pgprot_t pgprot, bool write)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800927{
Dan Williamsfce86ff2019-05-13 17:15:33 -0700928 unsigned long addr = vmf->address & PUD_MASK;
929 struct vm_area_struct *vma = vmf->vma;
Dan Williamsfce86ff2019-05-13 17:15:33 -0700930
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800931 /*
932 * If we had pud_special, we could avoid all these restrictions,
933 * but we need to be consistent with PTEs and architectures that
934 * can't support a 'special' bit.
935 */
Dave Jiang62ec0d82018-09-04 15:46:16 -0700936 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
937 !pfn_t_devmap(pfn));
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800938 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
939 (VM_PFNMAP|VM_MIXEDMAP));
940 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800941
942 if (addr < vma->vm_start || addr >= vma->vm_end)
943 return VM_FAULT_SIGBUS;
944
945 track_pfn_insert(vma, &pgprot, pfn);
946
Dan Williamsfce86ff2019-05-13 17:15:33 -0700947 insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800948 return VM_FAULT_NOPAGE;
949}
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100950EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud_prot);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800951#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
952
Dan Williams3565fce2016-01-15 16:56:55 -0800953static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
Kirill A. Shutemova8f97362017-11-27 06:21:25 +0300954 pmd_t *pmd, int flags)
Dan Williams3565fce2016-01-15 16:56:55 -0800955{
956 pmd_t _pmd;
957
Kirill A. Shutemova8f97362017-11-27 06:21:25 +0300958 _pmd = pmd_mkyoung(*pmd);
959 if (flags & FOLL_WRITE)
960 _pmd = pmd_mkdirty(_pmd);
Dan Williams3565fce2016-01-15 16:56:55 -0800961 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
Kirill A. Shutemova8f97362017-11-27 06:21:25 +0300962 pmd, _pmd, flags & FOLL_WRITE))
Dan Williams3565fce2016-01-15 16:56:55 -0800963 update_mmu_cache_pmd(vma, addr, pmd);
964}
965
966struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
Keith Buschdf06b372018-10-26 15:10:28 -0700967 pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -0800968{
969 unsigned long pfn = pmd_pfn(*pmd);
970 struct mm_struct *mm = vma->vm_mm;
Dan Williams3565fce2016-01-15 16:56:55 -0800971 struct page *page;
972
973 assert_spin_locked(pmd_lockptr(mm, pmd));
974
Keno Fischer8310d482017-01-24 15:17:48 -0800975 /*
976 * When we COW a devmap PMD entry, we split it into PTEs, so we should
977 * not be in this function with `flags & FOLL_COW` set.
978 */
979 WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set");
980
John Hubbard3faa52c2020-04-01 21:05:29 -0700981 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
982 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
983 (FOLL_PIN | FOLL_GET)))
984 return NULL;
985
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800986 if (flags & FOLL_WRITE && !pmd_write(*pmd))
Dan Williams3565fce2016-01-15 16:56:55 -0800987 return NULL;
988
989 if (pmd_present(*pmd) && pmd_devmap(*pmd))
990 /* pass */;
991 else
992 return NULL;
993
994 if (flags & FOLL_TOUCH)
Kirill A. Shutemova8f97362017-11-27 06:21:25 +0300995 touch_pmd(vma, addr, pmd, flags);
Dan Williams3565fce2016-01-15 16:56:55 -0800996
997 /*
998 * device mapped pages can only be returned if the
999 * caller will manage the page reference count.
1000 */
John Hubbard3faa52c2020-04-01 21:05:29 -07001001 if (!(flags & (FOLL_GET | FOLL_PIN)))
Dan Williams3565fce2016-01-15 16:56:55 -08001002 return ERR_PTR(-EEXIST);
1003
1004 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
Keith Buschdf06b372018-10-26 15:10:28 -07001005 *pgmap = get_dev_pagemap(pfn, *pgmap);
1006 if (!*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -08001007 return ERR_PTR(-EFAULT);
1008 page = pfn_to_page(pfn);
John Hubbard3faa52c2020-04-01 21:05:29 -07001009 if (!try_grab_page(page, flags))
1010 page = ERR_PTR(-ENOMEM);
Dan Williams3565fce2016-01-15 16:56:55 -08001011
1012 return page;
1013}
1014
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001015int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1016 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
1017 struct vm_area_struct *vma)
1018{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001019 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001020 struct page *src_page;
1021 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001022 pgtable_t pgtable = NULL;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001023 int ret = -ENOMEM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001024
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001025 /* Skip if can be re-fill on fault */
1026 if (!vma_is_anonymous(vma))
1027 return 0;
1028
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08001029 pgtable = pte_alloc_one(dst_mm);
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001030 if (unlikely(!pgtable))
1031 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001032
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001033 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1034 src_ptl = pmd_lockptr(src_mm, src_pmd);
1035 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001036
1037 ret = -EAGAIN;
1038 pmd = *src_pmd;
Zi Yan84c3fc42017-09-08 16:11:01 -07001039
Peter Xub569a172020-04-06 20:05:53 -07001040 /*
1041 * Make sure the _PAGE_UFFD_WP bit is cleared if the new VMA
1042 * does not have the VM_UFFD_WP, which means that the uffd
1043 * fork event is not enabled.
1044 */
1045 if (!(vma->vm_flags & VM_UFFD_WP))
1046 pmd = pmd_clear_uffd_wp(pmd);
1047
Zi Yan84c3fc42017-09-08 16:11:01 -07001048#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1049 if (unlikely(is_swap_pmd(pmd))) {
1050 swp_entry_t entry = pmd_to_swp_entry(pmd);
1051
1052 VM_BUG_ON(!is_pmd_migration_entry(pmd));
1053 if (is_write_migration_entry(entry)) {
1054 make_migration_entry_read(&entry);
1055 pmd = swp_entry_to_pmd(entry);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001056 if (pmd_swp_soft_dirty(*src_pmd))
1057 pmd = pmd_swp_mksoft_dirty(pmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001058 set_pmd_at(src_mm, addr, src_pmd, pmd);
1059 }
Zi Yandd8a67f2017-11-02 15:59:47 -07001060 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08001061 mm_inc_nr_ptes(dst_mm);
Zi Yandd8a67f2017-11-02 15:59:47 -07001062 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Zi Yan84c3fc42017-09-08 16:11:01 -07001063 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1064 ret = 0;
1065 goto out_unlock;
1066 }
1067#endif
1068
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001069 if (unlikely(!pmd_trans_huge(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001070 pte_free(dst_mm, pgtable);
1071 goto out_unlock;
1072 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001073 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001074 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001075 * under splitting since we don't split the page itself, only pmd to
1076 * a page table.
1077 */
1078 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001079 struct page *zero_page;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001080 /*
1081 * get_huge_zero_page() will never allocate a new page here,
1082 * since we already have a zero page to copy. It just takes a
1083 * reference.
1084 */
Aaron Lu6fcb52a2016-10-07 17:00:08 -07001085 zero_page = mm_get_huge_zero_page(dst_mm);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -07001086 set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -07001087 zero_page);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001088 ret = 0;
1089 goto out_unlock;
1090 }
Mel Gormande466bd2013-12-18 17:08:42 -08001091
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001092 src_page = pmd_page(pmd);
1093 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Peter Xud0420352020-09-25 18:26:00 -04001094
1095 /*
1096 * If this page is a potentially pinned page, split and retry the fault
1097 * with smaller page size. Normally this should not happen because the
1098 * userspace should use MADV_DONTFORK upon pinned regions. This is a
1099 * best effort that the pinned pages won't be replaced by another
1100 * random page during the coming copy-on-write.
1101 */
Peter Xu97a7e472021-03-12 21:07:26 -08001102 if (unlikely(page_needs_cow_for_dma(vma, src_page))) {
Peter Xud0420352020-09-25 18:26:00 -04001103 pte_free(dst_mm, pgtable);
1104 spin_unlock(src_ptl);
1105 spin_unlock(dst_ptl);
1106 __split_huge_pmd(vma, src_pmd, addr, false, NULL);
1107 return -EAGAIN;
1108 }
1109
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001110 get_page(src_page);
1111 page_dup_rmap(src_page, true);
1112 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08001113 mm_inc_nr_ptes(dst_mm);
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001114 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001115
1116 pmdp_set_wrprotect(src_mm, addr, src_pmd);
1117 pmd = pmd_mkold(pmd_wrprotect(pmd));
1118 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001119
1120 ret = 0;
1121out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001122 spin_unlock(src_ptl);
1123 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001124out:
1125 return ret;
1126}
1127
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001128#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1129static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001130 pud_t *pud, int flags)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001131{
1132 pud_t _pud;
1133
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001134 _pud = pud_mkyoung(*pud);
1135 if (flags & FOLL_WRITE)
1136 _pud = pud_mkdirty(_pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001137 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001138 pud, _pud, flags & FOLL_WRITE))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001139 update_mmu_cache_pud(vma, addr, pud);
1140}
1141
1142struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
Keith Buschdf06b372018-10-26 15:10:28 -07001143 pud_t *pud, int flags, struct dev_pagemap **pgmap)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001144{
1145 unsigned long pfn = pud_pfn(*pud);
1146 struct mm_struct *mm = vma->vm_mm;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001147 struct page *page;
1148
1149 assert_spin_locked(pud_lockptr(mm, pud));
1150
Linus Torvaldsf6f37322017-12-15 18:53:22 -08001151 if (flags & FOLL_WRITE && !pud_write(*pud))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001152 return NULL;
1153
John Hubbard3faa52c2020-04-01 21:05:29 -07001154 /* FOLL_GET and FOLL_PIN are mutually exclusive. */
1155 if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) ==
1156 (FOLL_PIN | FOLL_GET)))
1157 return NULL;
1158
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001159 if (pud_present(*pud) && pud_devmap(*pud))
1160 /* pass */;
1161 else
1162 return NULL;
1163
1164 if (flags & FOLL_TOUCH)
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001165 touch_pud(vma, addr, pud, flags);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001166
1167 /*
1168 * device mapped pages can only be returned if the
1169 * caller will manage the page reference count.
John Hubbard3faa52c2020-04-01 21:05:29 -07001170 *
1171 * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here:
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001172 */
John Hubbard3faa52c2020-04-01 21:05:29 -07001173 if (!(flags & (FOLL_GET | FOLL_PIN)))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001174 return ERR_PTR(-EEXIST);
1175
1176 pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
Keith Buschdf06b372018-10-26 15:10:28 -07001177 *pgmap = get_dev_pagemap(pfn, *pgmap);
1178 if (!*pgmap)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001179 return ERR_PTR(-EFAULT);
1180 page = pfn_to_page(pfn);
John Hubbard3faa52c2020-04-01 21:05:29 -07001181 if (!try_grab_page(page, flags))
1182 page = ERR_PTR(-ENOMEM);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001183
1184 return page;
1185}
1186
1187int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1188 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1189 struct vm_area_struct *vma)
1190{
1191 spinlock_t *dst_ptl, *src_ptl;
1192 pud_t pud;
1193 int ret;
1194
1195 dst_ptl = pud_lock(dst_mm, dst_pud);
1196 src_ptl = pud_lockptr(src_mm, src_pud);
1197 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1198
1199 ret = -EAGAIN;
1200 pud = *src_pud;
1201 if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
1202 goto out_unlock;
1203
1204 /*
1205 * When page table lock is held, the huge zero pud should not be
1206 * under splitting since we don't split the page itself, only pud to
1207 * a page table.
1208 */
1209 if (is_huge_zero_pud(pud)) {
1210 /* No huge zero pud yet */
1211 }
1212
Peter Xud0420352020-09-25 18:26:00 -04001213 /* Please refer to comments in copy_huge_pmd() */
Peter Xu97a7e472021-03-12 21:07:26 -08001214 if (unlikely(page_needs_cow_for_dma(vma, pud_page(pud)))) {
Peter Xud0420352020-09-25 18:26:00 -04001215 spin_unlock(src_ptl);
1216 spin_unlock(dst_ptl);
1217 __split_huge_pud(vma, src_pud, addr);
1218 return -EAGAIN;
1219 }
1220
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001221 pudp_set_wrprotect(src_mm, addr, src_pud);
1222 pud = pud_mkold(pud_wrprotect(pud));
1223 set_pud_at(dst_mm, addr, dst_pud, pud);
1224
1225 ret = 0;
1226out_unlock:
1227 spin_unlock(src_ptl);
1228 spin_unlock(dst_ptl);
1229 return ret;
1230}
1231
1232void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
1233{
1234 pud_t entry;
1235 unsigned long haddr;
1236 bool write = vmf->flags & FAULT_FLAG_WRITE;
1237
1238 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1239 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1240 goto unlock;
1241
1242 entry = pud_mkyoung(orig_pud);
1243 if (write)
1244 entry = pud_mkdirty(entry);
1245 haddr = vmf->address & HPAGE_PUD_MASK;
1246 if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write))
1247 update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud);
1248
1249unlock:
1250 spin_unlock(vmf->ptl);
1251}
1252#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1253
Jan Kara82b0f8c2016-12-14 15:06:58 -08001254void huge_pmd_set_accessed(struct vm_fault *vmf, pmd_t orig_pmd)
Will Deacona1dd4502012-12-11 16:01:27 -08001255{
1256 pmd_t entry;
1257 unsigned long haddr;
Minchan Kim20f664a2017-01-10 16:57:51 -08001258 bool write = vmf->flags & FAULT_FLAG_WRITE;
Will Deacona1dd4502012-12-11 16:01:27 -08001259
Jan Kara82b0f8c2016-12-14 15:06:58 -08001260 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1261 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd)))
Will Deacona1dd4502012-12-11 16:01:27 -08001262 goto unlock;
1263
1264 entry = pmd_mkyoung(orig_pmd);
Minchan Kim20f664a2017-01-10 16:57:51 -08001265 if (write)
1266 entry = pmd_mkdirty(entry);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001267 haddr = vmf->address & HPAGE_PMD_MASK;
Minchan Kim20f664a2017-01-10 16:57:51 -08001268 if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write))
Jan Kara82b0f8c2016-12-14 15:06:58 -08001269 update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd);
Will Deacona1dd4502012-12-11 16:01:27 -08001270
1271unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08001272 spin_unlock(vmf->ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001273}
1274
Souptick Joarder2b740302018-08-23 17:01:36 -07001275vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001276{
Jan Kara82b0f8c2016-12-14 15:06:58 -08001277 struct vm_area_struct *vma = vmf->vma;
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001278 struct page *page;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001279 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001280
Jan Kara82b0f8c2016-12-14 15:06:58 -08001281 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001282 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001283
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001284 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001285 goto fallback;
1286
Jan Kara82b0f8c2016-12-14 15:06:58 -08001287 spin_lock(vmf->ptl);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001288
1289 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1290 spin_unlock(vmf->ptl);
1291 return 0;
1292 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001293
1294 page = pmd_page(orig_pmd);
Miaohe Linf6004e72021-05-04 18:34:02 -07001295 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001296
1297 /* Lock page for reuse_swap_page() */
Huang Yingba3c4ce2017-09-06 16:22:19 -07001298 if (!trylock_page(page)) {
1299 get_page(page);
1300 spin_unlock(vmf->ptl);
1301 lock_page(page);
1302 spin_lock(vmf->ptl);
1303 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001304 spin_unlock(vmf->ptl);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001305 unlock_page(page);
1306 put_page(page);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001307 return 0;
Huang Yingba3c4ce2017-09-06 16:22:19 -07001308 }
1309 put_page(page);
1310 }
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001311
1312 /*
1313 * We can only reuse the page if nobody else maps the huge page or it's
1314 * part.
1315 */
Huang Yingba3c4ce2017-09-06 16:22:19 -07001316 if (reuse_swap_page(page, NULL)) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001317 pmd_t entry;
1318 entry = pmd_mkyoung(orig_pmd);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001319 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001320 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
Jan Kara82b0f8c2016-12-14 15:06:58 -08001321 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001322 unlock_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001323 spin_unlock(vmf->ptl);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001324 return VM_FAULT_WRITE;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001325 }
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001326
1327 unlock_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001328 spin_unlock(vmf->ptl);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001329fallback:
1330 __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
1331 return VM_FAULT_FALLBACK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001332}
1333
Keno Fischer8310d482017-01-24 15:17:48 -08001334/*
Peter Xua308c712020-08-21 19:49:57 -04001335 * FOLL_FORCE can write to even unwritable pmd's, but only
1336 * after we've gone through a COW cycle and they are dirty.
Keno Fischer8310d482017-01-24 15:17:48 -08001337 */
1338static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags)
1339{
Peter Xua308c712020-08-21 19:49:57 -04001340 return pmd_write(pmd) ||
1341 ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd));
Keno Fischer8310d482017-01-24 15:17:48 -08001342}
1343
David Rientjesb676b292012-10-08 16:34:03 -07001344struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001345 unsigned long addr,
1346 pmd_t *pmd,
1347 unsigned int flags)
1348{
David Rientjesb676b292012-10-08 16:34:03 -07001349 struct mm_struct *mm = vma->vm_mm;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001350 struct page *page = NULL;
1351
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001352 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001353
Keno Fischer8310d482017-01-24 15:17:48 -08001354 if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001355 goto out;
1356
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001357 /* Avoid dumping huge zero page */
1358 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1359 return ERR_PTR(-EFAULT);
1360
Mel Gorman2b4847e2013-12-18 17:08:32 -08001361 /* Full NUMA hinting faults to serialise migration in fault paths */
Mel Gorman8a0516e2015-02-12 14:58:22 -08001362 if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
Mel Gorman2b4847e2013-12-18 17:08:32 -08001363 goto out;
1364
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001365 page = pmd_page(*pmd);
Dan Williamsca120cf2016-09-03 10:38:03 -07001366 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
John Hubbard3faa52c2020-04-01 21:05:29 -07001367
1368 if (!try_grab_page(page, flags))
1369 return ERR_PTR(-ENOMEM);
1370
Dan Williams3565fce2016-01-15 16:56:55 -08001371 if (flags & FOLL_TOUCH)
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001372 touch_pmd(vma, addr, pmd, flags);
John Hubbard3faa52c2020-04-01 21:05:29 -07001373
Eric B Munsonde60f5f2015-11-05 18:51:36 -08001374 if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001375 /*
1376 * We don't mlock() pte-mapped THPs. This way we can avoid
1377 * leaking mlocked pages into non-VM_LOCKED VMAs.
1378 *
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001379 * For anon THP:
1380 *
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001381 * In most cases the pmd is the only mapping of the page as we
1382 * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for
1383 * writable private mappings in populate_vma_page_range().
1384 *
1385 * The only scenario when we have the page shared here is if we
1386 * mlocking read-only mapping shared over fork(). We skip
1387 * mlocking such pages.
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001388 *
1389 * For file THP:
1390 *
1391 * We can expect PageDoubleMap() to be stable under page lock:
1392 * for file pages we set it in page_add_file_rmap(), which
1393 * requires page to be locked.
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08001394 */
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001395
1396 if (PageAnon(page) && compound_mapcount(page) != 1)
1397 goto skip_mlock;
1398 if (PageDoubleMap(page) || !page->mapping)
1399 goto skip_mlock;
1400 if (!trylock_page(page))
1401 goto skip_mlock;
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001402 if (page->mapping && !PageDoubleMap(page))
1403 mlock_vma_page(page);
1404 unlock_page(page);
David Rientjesb676b292012-10-08 16:34:03 -07001405 }
Kirill A. Shutemov9a73f612016-07-26 15:25:53 -07001406skip_mlock:
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001407 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Dan Williamsca120cf2016-09-03 10:38:03 -07001408 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001409
1410out:
1411 return page;
1412}
1413
Mel Gormand10e63f2012-10-25 14:16:31 +02001414/* NUMA hinting page fault entry point for trans huge pmds */
Souptick Joarder2b740302018-08-23 17:01:36 -07001415vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)
Mel Gormand10e63f2012-10-25 14:16:31 +02001416{
Jan Kara82b0f8c2016-12-14 15:06:58 -08001417 struct vm_area_struct *vma = vmf->vma;
Mel Gormanb8916632013-10-07 11:28:44 +01001418 struct anon_vma *anon_vma = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001419 struct page *page;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001420 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001421 int page_nid = NUMA_NO_NODE, this_nid = numa_node_id();
Peter Zijlstra90572892013-10-07 11:29:20 +01001422 int target_nid, last_cpupid = -1;
Mel Gorman8191acb2013-10-07 11:28:45 +01001423 bool page_locked;
1424 bool migrated = false;
Mel Gormanb191f9b2015-03-25 15:55:40 -07001425 bool was_writable;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001426 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001427
Jan Kara82b0f8c2016-12-14 15:06:58 -08001428 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1429 if (unlikely(!pmd_same(pmd, *vmf->pmd)))
Mel Gormand10e63f2012-10-25 14:16:31 +02001430 goto out_unlock;
1431
Mel Gormande466bd2013-12-18 17:08:42 -08001432 /*
1433 * If there are potential migrations, wait for completion and retry
1434 * without disrupting NUMA hinting information. Do not relock and
1435 * check_same as the page may no longer be mapped.
1436 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08001437 if (unlikely(pmd_trans_migrating(*vmf->pmd))) {
1438 page = pmd_page(*vmf->pmd);
Mark Rutland3c226c62017-06-16 14:02:34 -07001439 if (!get_page_unless_zero(page))
1440 goto out_unlock;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001441 spin_unlock(vmf->ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001442 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Mel Gormande466bd2013-12-18 17:08:42 -08001443 goto out;
1444 }
1445
Mel Gormand10e63f2012-10-25 14:16:31 +02001446 page = pmd_page(pmd);
Mel Gormana1a46182013-10-07 11:28:50 +01001447 BUG_ON(is_huge_zero_page(page));
Mel Gorman8191acb2013-10-07 11:28:45 +01001448 page_nid = page_to_nid(page);
Peter Zijlstra90572892013-10-07 11:29:20 +01001449 last_cpupid = page_cpupid_last(page);
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001450 count_vm_numa_event(NUMA_HINT_FAULTS);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001451 if (page_nid == this_nid) {
Mel Gorman03c5a6e2012-11-02 14:52:48 +00001452 count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
Rik van Riel04bb2f92013-10-07 11:29:36 +01001453 flags |= TNF_FAULT_LOCAL;
1454 }
Mel Gorman4daae3b2012-11-02 11:33:45 +00001455
Mel Gormanbea66fb2015-03-25 15:55:37 -07001456 /* See similar comment in do_numa_page for explanation */
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08001457 if (!pmd_savedwrite(pmd))
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001458 flags |= TNF_NO_GROUP;
1459
1460 /*
Mel Gormanff9042b2013-10-07 11:28:43 +01001461 * Acquire the page lock to serialise THP migrations but avoid dropping
1462 * page_table_lock if at all possible
1463 */
Mel Gormanb8916632013-10-07 11:28:44 +01001464 page_locked = trylock_page(page);
1465 target_nid = mpol_misplaced(page, vma, haddr);
Mel Gormande466bd2013-12-18 17:08:42 -08001466 /* Migration could have started since the pmd_trans_migrating check */
Mel Gorman2b4847e2013-12-18 17:08:32 -08001467 if (!page_locked) {
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001468 page_nid = NUMA_NO_NODE;
Mark Rutland3c226c62017-06-16 14:02:34 -07001469 if (!get_page_unless_zero(page))
1470 goto out_unlock;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001471 spin_unlock(vmf->ptl);
Matthew Wilcox (Oracle)48054622021-02-24 12:02:02 -08001472 put_and_wait_on_page_locked(page, TASK_UNINTERRUPTIBLE);
Mel Gormanb8916632013-10-07 11:28:44 +01001473 goto out;
Miaohe Lin6beb5e82021-05-04 18:33:59 -07001474 } else if (target_nid == NUMA_NO_NODE) {
1475 /* There are no parallel migrations and page is in the right
1476 * node. Clear the numa hinting info in this pmd.
1477 */
1478 goto clear_pmdnuma;
Mel Gormanb8916632013-10-07 11:28:44 +01001479 }
1480
Mel Gorman2b4847e2013-12-18 17:08:32 -08001481 /*
1482 * Page is misplaced. Page lock serialises migrations. Acquire anon_vma
1483 * to serialises splits
1484 */
Mel Gormanb8916632013-10-07 11:28:44 +01001485 get_page(page);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001486 spin_unlock(vmf->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001487 anon_vma = page_lock_anon_vma_read(page);
Peter Zijlstracbee9f82012-10-25 14:16:43 +02001488
Peter Zijlstrac69307d2013-10-07 11:28:41 +01001489 /* Confirm the PMD did not change while page_table_lock was released */
Jan Kara82b0f8c2016-12-14 15:06:58 -08001490 spin_lock(vmf->ptl);
1491 if (unlikely(!pmd_same(pmd, *vmf->pmd))) {
Mel Gormanb32967f2012-11-19 12:35:47 +00001492 unlock_page(page);
1493 put_page(page);
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001494 page_nid = NUMA_NO_NODE;
Mel Gormanb32967f2012-11-19 12:35:47 +00001495 goto out_unlock;
1496 }
Mel Gormanff9042b2013-10-07 11:28:43 +01001497
Mel Gormanc3a489c2013-12-18 17:08:38 -08001498 /* Bail if we fail to protect against THP splits for any reason */
1499 if (unlikely(!anon_vma)) {
1500 put_page(page);
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001501 page_nid = NUMA_NO_NODE;
Mel Gormanc3a489c2013-12-18 17:08:38 -08001502 goto clear_pmdnuma;
1503 }
1504
Mel Gormana54a4072013-10-07 11:28:46 +01001505 /*
Peter Zijlstra8b1b4362017-06-07 18:05:07 +02001506 * Since we took the NUMA fault, we must have observed the !accessible
1507 * bit. Make sure all other CPUs agree with that, to avoid them
1508 * modifying the page we're about to migrate.
1509 *
1510 * Must be done under PTL such that we'll observe the relevant
Peter Zijlstraccde85b2017-08-11 14:29:01 +02001511 * inc_tlb_flush_pending().
1512 *
1513 * We are not sure a pending tlb flush here is for a huge page
1514 * mapping or not. Hence use the tlb range variant
Peter Zijlstra8b1b4362017-06-07 18:05:07 +02001515 */
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07001516 if (mm_tlb_flush_pending(vma->vm_mm)) {
Peter Zijlstraccde85b2017-08-11 14:29:01 +02001517 flush_tlb_range(vma, haddr, haddr + HPAGE_PMD_SIZE);
Andrea Arcangeli7066f0f2018-10-26 15:10:40 -07001518 /*
1519 * change_huge_pmd() released the pmd lock before
1520 * invalidating the secondary MMUs sharing the primary
1521 * MMU pagetables (with ->invalidate_range()). The
1522 * mmu_notifier_invalidate_range_end() (which
1523 * internally calls ->invalidate_range()) in
1524 * change_pmd_range() will run after us, so we can't
1525 * rely on it here and we need an explicit invalidate.
1526 */
1527 mmu_notifier_invalidate_range(vma->vm_mm, haddr,
1528 haddr + HPAGE_PMD_SIZE);
1529 }
Peter Zijlstra8b1b4362017-06-07 18:05:07 +02001530
1531 /*
Mel Gormana54a4072013-10-07 11:28:46 +01001532 * Migrate the THP to the requested node, returns with page unlocked
Mel Gorman8a0516e2015-02-12 14:58:22 -08001533 * and access rights restored.
Mel Gormana54a4072013-10-07 11:28:46 +01001534 */
Jan Kara82b0f8c2016-12-14 15:06:58 -08001535 spin_unlock(vmf->ptl);
Peter Zijlstra8b1b4362017-06-07 18:05:07 +02001536
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -07001537 migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -08001538 vmf->pmd, pmd, vmf->address, page, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001539 if (migrated) {
1540 flags |= TNF_MIGRATED;
Mel Gorman8191acb2013-10-07 11:28:45 +01001541 page_nid = target_nid;
Mel Gorman074c2382015-03-25 15:55:42 -07001542 } else
1543 flags |= TNF_MIGRATE_FAIL;
Mel Gormanb32967f2012-11-19 12:35:47 +00001544
Mel Gorman8191acb2013-10-07 11:28:45 +01001545 goto out;
Mel Gorman4daae3b2012-11-02 11:33:45 +00001546clear_pmdnuma:
Mel Gormana54a4072013-10-07 11:28:46 +01001547 BUG_ON(!PageLocked(page));
Aneesh Kumar K.V288bc542017-02-24 14:59:16 -08001548 was_writable = pmd_savedwrite(pmd);
Mel Gorman4d942462015-02-12 14:58:28 -08001549 pmd = pmd_modify(pmd, vma->vm_page_prot);
Mel Gormanb7b04002015-03-25 15:55:45 -07001550 pmd = pmd_mkyoung(pmd);
Mel Gormanb191f9b2015-03-25 15:55:40 -07001551 if (was_writable)
1552 pmd = pmd_mkwrite(pmd);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001553 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1554 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Mel Gormana54a4072013-10-07 11:28:46 +01001555 unlock_page(page);
Mel Gormand10e63f2012-10-25 14:16:31 +02001556out_unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08001557 spin_unlock(vmf->ptl);
Mel Gormanb8916632013-10-07 11:28:44 +01001558
1559out:
1560 if (anon_vma)
1561 page_unlock_anon_vma_read(anon_vma);
1562
Anshuman Khandual98fa15f2019-03-05 15:42:58 -08001563 if (page_nid != NUMA_NO_NODE)
Jan Kara82b0f8c2016-12-14 15:06:58 -08001564 task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR,
Aneesh Kumar K.V9a8b3002017-02-24 14:59:56 -08001565 flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001566
Mel Gormand10e63f2012-10-25 14:16:31 +02001567 return 0;
1568}
1569
Huang Ying319904a2016-07-28 15:48:03 -07001570/*
1571 * Return true if we do MADV_FREE successfully on entire pmd page.
1572 * Otherwise, return false.
1573 */
1574bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001575 pmd_t *pmd, unsigned long addr, unsigned long next)
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001576{
1577 spinlock_t *ptl;
1578 pmd_t orig_pmd;
1579 struct page *page;
1580 struct mm_struct *mm = tlb->mm;
Huang Ying319904a2016-07-28 15:48:03 -07001581 bool ret = false;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001582
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001583 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001584
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001585 ptl = pmd_trans_huge_lock(pmd, vma);
1586 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001587 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001588
1589 orig_pmd = *pmd;
Huang Ying319904a2016-07-28 15:48:03 -07001590 if (is_huge_zero_pmd(orig_pmd))
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001591 goto out;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001592
Zi Yan84c3fc42017-09-08 16:11:01 -07001593 if (unlikely(!pmd_present(orig_pmd))) {
1594 VM_BUG_ON(thp_migration_supported() &&
1595 !is_pmd_migration_entry(orig_pmd));
1596 goto out;
1597 }
1598
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001599 page = pmd_page(orig_pmd);
1600 /*
1601 * If other processes are mapping this page, we couldn't discard
1602 * the page unless they all do MADV_FREE so let's skip the page.
1603 */
1604 if (page_mapcount(page) != 1)
1605 goto out;
1606
1607 if (!trylock_page(page))
1608 goto out;
1609
1610 /*
1611 * If user want to discard part-pages of THP, split it so MADV_FREE
1612 * will deactivate only them.
1613 */
1614 if (next - addr != HPAGE_PMD_SIZE) {
1615 get_page(page);
1616 spin_unlock(ptl);
Huang Ying9818b8c2016-07-14 12:07:12 -07001617 split_huge_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001618 unlock_page(page);
Kirill A. Shutemovbbf29ff2017-07-06 15:35:28 -07001619 put_page(page);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001620 goto out_unlocked;
1621 }
1622
1623 if (PageDirty(page))
1624 ClearPageDirty(page);
1625 unlock_page(page);
1626
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001627 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
Kirill A. Shutemov58ceeb62017-04-13 14:56:26 -07001628 pmdp_invalidate(vma, addr, pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001629 orig_pmd = pmd_mkold(orig_pmd);
1630 orig_pmd = pmd_mkclean(orig_pmd);
1631
1632 set_pmd_at(mm, addr, pmd, orig_pmd);
1633 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1634 }
Shaohua Li802a3a92017-05-03 14:52:32 -07001635
1636 mark_page_lazyfree(page);
Huang Ying319904a2016-07-28 15:48:03 -07001637 ret = true;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001638out:
1639 spin_unlock(ptl);
1640out_unlocked:
1641 return ret;
1642}
1643
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001644static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1645{
1646 pgtable_t pgtable;
1647
1648 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1649 pte_free(mm, pgtable);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08001650 mm_dec_nr_ptes(mm);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001651}
1652
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001653int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001654 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001655{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001656 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001657 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001658
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001659 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001660
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001661 ptl = __pmd_trans_huge_lock(pmd, vma);
1662 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001663 return 0;
1664 /*
1665 * For architectures like ppc64 we look at deposited pgtable
1666 * when calling pmdp_huge_get_and_clear. So do the
1667 * pgtable_trans_huge_withdraw after finishing pmdp related
1668 * operations.
1669 */
Aneesh Kumar K.V93a98692020-05-05 12:47:28 +05301670 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
1671 tlb->fullmm);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001672 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01001673 if (vma_is_special_huge(vma)) {
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -07001674 if (arch_needs_pgtable_deposit())
1675 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001676 spin_unlock(ptl);
1677 if (is_huge_zero_pmd(orig_pmd))
Aneesh Kumar K.Vc0f2e172016-12-12 16:42:31 -08001678 tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001679 } else if (is_huge_zero_pmd(orig_pmd)) {
Oliver O'Halloranc14a6eb2017-05-08 15:59:40 -07001680 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001681 spin_unlock(ptl);
Aneesh Kumar K.Vc0f2e172016-12-12 16:42:31 -08001682 tlb_remove_page_size(tlb, pmd_page(orig_pmd), HPAGE_PMD_SIZE);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001683 } else {
Zi Yan616b8372017-09-08 16:10:57 -07001684 struct page *page = NULL;
1685 int flush_needed = 1;
1686
1687 if (pmd_present(orig_pmd)) {
1688 page = pmd_page(orig_pmd);
1689 page_remove_rmap(page, true);
1690 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1691 VM_BUG_ON_PAGE(!PageHead(page), page);
1692 } else if (thp_migration_supported()) {
1693 swp_entry_t entry;
1694
1695 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1696 entry = pmd_to_swp_entry(orig_pmd);
Miaohe Lina44f89d2021-05-04 18:34:08 -07001697 page = migration_entry_to_page(entry);
Zi Yan616b8372017-09-08 16:10:57 -07001698 flush_needed = 0;
1699 } else
1700 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1701
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001702 if (PageAnon(page)) {
Oliver O'Halloranc14a6eb2017-05-08 15:59:40 -07001703 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001704 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1705 } else {
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001706 if (arch_needs_pgtable_deposit())
1707 zap_deposited_table(tlb->mm, pmd);
Yang Shifadae292018-08-17 15:44:55 -07001708 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001709 }
Zi Yan616b8372017-09-08 16:10:57 -07001710
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001711 spin_unlock(ptl);
Zi Yan616b8372017-09-08 16:10:57 -07001712 if (flush_needed)
1713 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001714 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001715 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001716}
1717
Aneesh Kumar K.V1dd38b62016-12-12 16:44:29 -08001718#ifndef pmd_move_must_withdraw
1719static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1720 spinlock_t *old_pmd_ptl,
1721 struct vm_area_struct *vma)
1722{
1723 /*
1724 * With split pmd lock we also need to move preallocated
1725 * PTE page table if new_pmd is on different PMD page table.
1726 *
1727 * We also don't deposit and withdraw tables for file pages.
1728 */
1729 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1730}
1731#endif
1732
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001733static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1734{
1735#ifdef CONFIG_MEM_SOFT_DIRTY
1736 if (unlikely(is_pmd_migration_entry(pmd)))
1737 pmd = pmd_swp_mksoft_dirty(pmd);
1738 else if (pmd_present(pmd))
1739 pmd = pmd_mksoft_dirty(pmd);
1740#endif
1741 return pmd;
1742}
1743
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001744bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Wei Yangb8aa9d92020-08-06 23:23:40 -07001745 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001746{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001747 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001748 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001749 struct mm_struct *mm = vma->vm_mm;
Aaron Lu5d190422016-11-10 17:16:33 +08001750 bool force_flush = false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001751
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001752 /*
1753 * The destination pmd shouldn't be established, free_pgtables()
1754 * should have release it.
1755 */
1756 if (WARN_ON(!pmd_none(*new_pmd))) {
1757 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001758 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001759 }
1760
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001761 /*
1762 * We don't have to worry about the ordering of src and dst
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001763 * ptlocks because exclusive mmap_lock prevents deadlock.
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001764 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001765 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1766 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001767 new_ptl = pmd_lockptr(mm, new_pmd);
1768 if (new_ptl != old_ptl)
1769 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001770 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Linus Torvaldseb66ae02018-10-12 15:22:59 -07001771 if (pmd_present(pmd))
Aaron Lua2ce2662016-11-29 13:27:31 +08001772 force_flush = true;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001773 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001774
Aneesh Kumar K.V1dd38b62016-12-12 16:44:29 -08001775 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301776 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001777 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1778 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001779 }
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001780 pmd = move_soft_dirty_pmd(pmd);
1781 set_pmd_at(mm, new_addr, new_pmd, pmd);
Aaron Lu5d190422016-11-10 17:16:33 +08001782 if (force_flush)
1783 flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
Linus Torvaldseb66ae02018-10-12 15:22:59 -07001784 if (new_ptl != old_ptl)
1785 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001786 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001787 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001788 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001789 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001790}
1791
Mel Gormanf123d742013-10-07 11:28:49 +01001792/*
1793 * Returns
1794 * - 0 if PMD could not be locked
1795 * - 1 if PMD was locked but protections unchange and TLB flush unnecessary
1796 * - HPAGE_PMD_NR is protections changed and TLB flush necessary
1797 */
Johannes Weinercd7548a2011-01-13 15:47:04 -08001798int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Peter Xu58705442020-04-06 20:05:45 -07001799 unsigned long addr, pgprot_t newprot, unsigned long cp_flags)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001800{
1801 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001802 spinlock_t *ptl;
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001803 pmd_t entry;
1804 bool preserve_write;
1805 int ret;
Peter Xu58705442020-04-06 20:05:45 -07001806 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
Peter Xu292924b2020-04-06 20:05:49 -07001807 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
1808 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001809
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001810 ptl = __pmd_trans_huge_lock(pmd, vma);
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001811 if (!ptl)
1812 return 0;
Mel Gormane944fd62015-02-12 14:58:35 -08001813
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001814 preserve_write = prot_numa && pmd_write(*pmd);
1815 ret = 1;
Mel Gormane944fd62015-02-12 14:58:35 -08001816
Zi Yan84c3fc42017-09-08 16:11:01 -07001817#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1818 if (is_swap_pmd(*pmd)) {
1819 swp_entry_t entry = pmd_to_swp_entry(*pmd);
1820
1821 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
1822 if (is_write_migration_entry(entry)) {
1823 pmd_t newpmd;
1824 /*
1825 * A protection check is difficult so
1826 * just be safe and disable write
1827 */
1828 make_migration_entry_read(&entry);
1829 newpmd = swp_entry_to_pmd(entry);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001830 if (pmd_swp_soft_dirty(*pmd))
1831 newpmd = pmd_swp_mksoft_dirty(newpmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001832 set_pmd_at(mm, addr, pmd, newpmd);
1833 }
1834 goto unlock;
1835 }
1836#endif
1837
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001838 /*
1839 * Avoid trapping faults against the zero page. The read-only
1840 * data is likely to be read-cached on the local CPU and
1841 * local/remote hits to the zero page are not interesting.
1842 */
1843 if (prot_numa && is_huge_zero_pmd(*pmd))
1844 goto unlock;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001845
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001846 if (prot_numa && pmd_protnone(*pmd))
1847 goto unlock;
1848
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001849 /*
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001850 * In case prot_numa, we are under mmap_read_lock(mm). It's critical
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001851 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001852 * which is also under mmap_read_lock(mm):
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001853 *
1854 * CPU0: CPU1:
1855 * change_huge_pmd(prot_numa=1)
1856 * pmdp_huge_get_and_clear_notify()
1857 * madvise_dontneed()
1858 * zap_pmd_range()
1859 * pmd_trans_huge(*pmd) == 0 (without ptl)
1860 * // skip the pmd
1861 * set_pmd_at();
1862 * // pmd is re-established
1863 *
1864 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1865 * which may break userspace.
1866 *
1867 * pmdp_invalidate() is required to make sure we don't miss
1868 * dirty/young flags set by hardware.
1869 */
Kirill A. Shutemova3cf9882018-01-31 16:18:20 -08001870 entry = pmdp_invalidate(vma, addr, pmd);
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001871
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001872 entry = pmd_modify(entry, newprot);
1873 if (preserve_write)
1874 entry = pmd_mk_savedwrite(entry);
Peter Xu292924b2020-04-06 20:05:49 -07001875 if (uffd_wp) {
1876 entry = pmd_wrprotect(entry);
1877 entry = pmd_mkuffd_wp(entry);
1878 } else if (uffd_wp_resolve) {
1879 /*
1880 * Leave the write bit to be handled by PF interrupt
1881 * handler, then things like COW could be properly
1882 * handled.
1883 */
1884 entry = pmd_clear_uffd_wp(entry);
1885 }
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001886 ret = HPAGE_PMD_NR;
1887 set_pmd_at(mm, addr, pmd, entry);
1888 BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry));
1889unlock:
1890 spin_unlock(ptl);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001891 return ret;
1892}
1893
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001894/*
Huang Ying8f19b0c2016-07-26 15:27:04 -07001895 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001896 *
Huang Ying8f19b0c2016-07-26 15:27:04 -07001897 * Note that if it returns page table lock pointer, this routine returns without
1898 * unlocking page table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001899 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001900spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001901{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001902 spinlock_t *ptl;
1903 ptl = pmd_lock(vma->vm_mm, pmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001904 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
1905 pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001906 return ptl;
1907 spin_unlock(ptl);
1908 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001909}
1910
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001911/*
1912 * Returns true if a given pud maps a thp, false otherwise.
1913 *
1914 * Note that if it returns true, this routine returns without unlocking page
1915 * table lock. So callers must unlock it.
1916 */
1917spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
1918{
1919 spinlock_t *ptl;
1920
1921 ptl = pud_lock(vma->vm_mm, pud);
1922 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
1923 return ptl;
1924 spin_unlock(ptl);
1925 return NULL;
1926}
1927
1928#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1929int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
1930 pud_t *pud, unsigned long addr)
1931{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001932 spinlock_t *ptl;
1933
1934 ptl = __pud_trans_huge_lock(pud, vma);
1935 if (!ptl)
1936 return 0;
1937 /*
1938 * For architectures like ppc64 we look at deposited pgtable
1939 * when calling pudp_huge_get_and_clear. So do the
1940 * pgtable_trans_huge_withdraw after finishing pudp related
1941 * operations.
1942 */
Qian Cai70516b92019-03-05 15:50:00 -08001943 pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001944 tlb_remove_pud_tlb_entry(tlb, pud, addr);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01001945 if (vma_is_special_huge(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001946 spin_unlock(ptl);
1947 /* No zero page support yet */
1948 } else {
1949 /* No support for anonymous PUD pages yet */
1950 BUG();
1951 }
1952 return 1;
1953}
1954
1955static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
1956 unsigned long haddr)
1957{
1958 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
1959 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
1960 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
1961 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
1962
Yisheng Xiece9311c2017-03-09 16:17:00 -08001963 count_vm_event(THP_SPLIT_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001964
1965 pudp_huge_clear_flush_notify(vma, haddr, pud);
1966}
1967
1968void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
1969 unsigned long address)
1970{
1971 spinlock_t *ptl;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001972 struct mmu_notifier_range range;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001973
Jérôme Glisse7269f992019-05-13 17:20:53 -07001974 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001975 address & HPAGE_PUD_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001976 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
1977 mmu_notifier_invalidate_range_start(&range);
1978 ptl = pud_lock(vma->vm_mm, pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001979 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
1980 goto out;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001981 __split_huge_pud_locked(vma, pud, range.start);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001982
1983out:
1984 spin_unlock(ptl);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08001985 /*
1986 * No need to double call mmu_notifier->invalidate_range() callback as
1987 * the above pudp_huge_clear_flush_notify() did already call it.
1988 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001989 mmu_notifier_invalidate_range_only_end(&range);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001990}
1991#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1992
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08001993static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
1994 unsigned long haddr, pmd_t *pmd)
1995{
1996 struct mm_struct *mm = vma->vm_mm;
1997 pgtable_t pgtable;
1998 pmd_t _pmd;
1999 int i;
2000
Jérôme Glisse0f108512017-11-15 17:34:07 -08002001 /*
2002 * Leave pmd empty until pte is filled note that it is fine to delay
2003 * notification until mmu_notifier_invalidate_range_end() as we are
2004 * replacing a zero pmd write protected page with a zero pte write
2005 * protected page.
2006 *
Mike Rapoportad56b732018-03-21 21:22:47 +02002007 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08002008 */
2009 pmdp_huge_clear_flush(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002010
2011 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2012 pmd_populate(mm, &_pmd, pgtable);
2013
2014 for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) {
2015 pte_t *pte, entry;
2016 entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot);
2017 entry = pte_mkspecial(entry);
2018 pte = pte_offset_map(&_pmd, haddr);
2019 VM_BUG_ON(!pte_none(*pte));
2020 set_pte_at(mm, haddr, pte, entry);
2021 pte_unmap(pte);
2022 }
2023 smp_wmb(); /* make pte visible before pmd */
2024 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002025}
2026
2027static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002028 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002029{
2030 struct mm_struct *mm = vma->vm_mm;
2031 struct page *page;
2032 pgtable_t pgtable;
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002033 pmd_t old_pmd, _pmd;
Peter Xu292924b2020-04-06 20:05:49 -07002034 bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002035 unsigned long addr;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002036 int i;
2037
2038 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2039 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2040 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Zi Yan84c3fc42017-09-08 16:11:01 -07002041 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
2042 && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002043
2044 count_vm_event(THP_SPLIT_PMD);
2045
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002046 if (!vma_is_anonymous(vma)) {
2047 _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08002048 /*
2049 * We are going to unmap this huge page. So
2050 * just go ahead and zap it
2051 */
2052 if (arch_needs_pgtable_deposit())
2053 zap_deposited_table(mm, pmd);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01002054 if (vma_is_special_huge(vma))
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002055 return;
2056 page = pmd_page(_pmd);
Hugh Dickinse1f1b152018-07-20 17:53:45 -07002057 if (!PageDirty(page) && pmd_dirty(_pmd))
2058 set_page_dirty(page);
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002059 if (!PageReferenced(page) && pmd_young(_pmd))
2060 SetPageReferenced(page);
2061 page_remove_rmap(page, true);
2062 put_page(page);
Yang Shifadae292018-08-17 15:44:55 -07002063 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002064 return;
Ralph Campbellec0abae2020-09-18 21:20:24 -07002065 } else if (pmd_trans_huge(*pmd) && is_huge_zero_pmd(*pmd)) {
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002066 /*
2067 * FIXME: Do we want to invalidate secondary mmu by calling
2068 * mmu_notifier_invalidate_range() see comments below inside
2069 * __split_huge_pmd() ?
2070 *
2071 * We are going from a zero huge page write protected to zero
2072 * small page also write protected so it does not seems useful
2073 * to invalidate secondary mmu at this time.
2074 */
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002075 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2076 }
2077
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002078 /*
2079 * Up to this point the pmd is present and huge and userland has the
2080 * whole access to the hugepage during the split (which happens in
2081 * place). If we overwrite the pmd with the not-huge version pointing
2082 * to the pte here (which of course we could if all CPUs were bug
2083 * free), userland could trigger a small page size TLB miss on the
2084 * small sized TLB while the hugepage TLB entry is still established in
2085 * the huge TLB. Some CPU doesn't like that.
Alexander A. Klimov42742d92020-08-06 23:26:08 -07002086 * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
2087 * 383 on page 105. Intel should be safe but is also warns that it's
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002088 * only safe if the permission and cache attributes of the two entries
2089 * loaded in the two TLB is identical (which should be the case here).
2090 * But it is generally safer to never allow small and huge TLB entries
2091 * for the same virtual address to be loaded simultaneously. So instead
2092 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2093 * current pmd notpresent (atomically because here the pmd_trans_huge
2094 * must remain set at all times on the pmd until the split is complete
2095 * for this pmd), then we flush the SMP TLB and finally we write the
2096 * non-huge version of the pmd entry with pmd_populate.
2097 */
2098 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2099
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002100 pmd_migration = is_pmd_migration_entry(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002101 if (unlikely(pmd_migration)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07002102 swp_entry_t entry;
2103
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002104 entry = pmd_to_swp_entry(old_pmd);
Miaohe Lina44f89d2021-05-04 18:34:08 -07002105 page = migration_entry_to_page(entry);
Peter Xu2e83ee12018-12-21 14:30:50 -08002106 write = is_write_migration_entry(entry);
2107 young = false;
2108 soft_dirty = pmd_swp_soft_dirty(old_pmd);
Peter Xuf45ec5f2020-04-06 20:06:01 -07002109 uffd_wp = pmd_swp_uffd_wp(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002110 } else {
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002111 page = pmd_page(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002112 if (pmd_dirty(old_pmd))
2113 SetPageDirty(page);
2114 write = pmd_write(old_pmd);
2115 young = pmd_young(old_pmd);
2116 soft_dirty = pmd_soft_dirty(old_pmd);
Peter Xu292924b2020-04-06 20:05:49 -07002117 uffd_wp = pmd_uffd_wp(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002118 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002119 VM_BUG_ON_PAGE(!page_count(page), page);
Joonsoo Kimfe896d12016-03-17 14:19:26 -07002120 page_ref_add(page, HPAGE_PMD_NR - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002121
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002122 /*
2123 * Withdraw the table only after we mark the pmd entry invalid.
2124 * This's critical for some architectures (Power).
2125 */
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002126 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2127 pmd_populate(mm, &_pmd, pgtable);
2128
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002129 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002130 pte_t entry, *pte;
2131 /*
2132 * Note that NUMA hinting access restrictions are not
2133 * transferred to avoid any possibility of altering
2134 * permissions across VMAs.
2135 */
Zi Yan84c3fc42017-09-08 16:11:01 -07002136 if (freeze || pmd_migration) {
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002137 swp_entry_t swp_entry;
2138 swp_entry = make_migration_entry(page + i, write);
2139 entry = swp_entry_to_pte(swp_entry);
Andrea Arcangeli804dd152016-08-25 15:16:57 -07002140 if (soft_dirty)
2141 entry = pte_swp_mksoft_dirty(entry);
Peter Xuf45ec5f2020-04-06 20:06:01 -07002142 if (uffd_wp)
2143 entry = pte_swp_mkuffd_wp(entry);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002144 } else {
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07002145 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002146 entry = maybe_mkwrite(entry, vma);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002147 if (!write)
2148 entry = pte_wrprotect(entry);
2149 if (!young)
2150 entry = pte_mkold(entry);
Andrea Arcangeli804dd152016-08-25 15:16:57 -07002151 if (soft_dirty)
2152 entry = pte_mksoft_dirty(entry);
Peter Xu292924b2020-04-06 20:05:49 -07002153 if (uffd_wp)
2154 entry = pte_mkuffd_wp(entry);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002155 }
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002156 pte = pte_offset_map(&_pmd, addr);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002157 BUG_ON(!pte_none(*pte));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002158 set_pte_at(mm, addr, pte, entry);
Ralph Campbellec0abae2020-09-18 21:20:24 -07002159 if (!pmd_migration)
2160 atomic_inc(&page[i]._mapcount);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002161 pte_unmap(pte);
2162 }
2163
Ralph Campbellec0abae2020-09-18 21:20:24 -07002164 if (!pmd_migration) {
2165 /*
2166 * Set PG_double_map before dropping compound_mapcount to avoid
2167 * false-negative page_mapped().
2168 */
2169 if (compound_mapcount(page) > 1 &&
2170 !TestSetPageDoubleMap(page)) {
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002171 for (i = 0; i < HPAGE_PMD_NR; i++)
Ralph Campbellec0abae2020-09-18 21:20:24 -07002172 atomic_inc(&page[i]._mapcount);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002173 }
Ralph Campbellec0abae2020-09-18 21:20:24 -07002174
2175 lock_page_memcg(page);
2176 if (atomic_add_negative(-1, compound_mapcount_ptr(page))) {
2177 /* Last compound_mapcount is gone. */
Muchun Song69473e52021-02-24 12:03:23 -08002178 __mod_lruvec_page_state(page, NR_ANON_THPS,
2179 -HPAGE_PMD_NR);
Ralph Campbellec0abae2020-09-18 21:20:24 -07002180 if (TestClearPageDoubleMap(page)) {
2181 /* No need in mapcount reference anymore */
2182 for (i = 0; i < HPAGE_PMD_NR; i++)
2183 atomic_dec(&page[i]._mapcount);
2184 }
2185 }
2186 unlock_page_memcg(page);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002187 }
2188
2189 smp_wmb(); /* make pte visible before pmd */
2190 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002191
2192 if (freeze) {
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002193 for (i = 0; i < HPAGE_PMD_NR; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002194 page_remove_rmap(page + i, false);
2195 put_page(page + i);
2196 }
2197 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002198}
2199
2200void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002201 unsigned long address, bool freeze, struct page *page)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002202{
2203 spinlock_t *ptl;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002204 struct mmu_notifier_range range;
Hugh Dickins1c2f6732021-02-04 18:32:31 -08002205 bool do_unlock_page = false;
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002206 pmd_t _pmd;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002207
Jérôme Glisse7269f992019-05-13 17:20:53 -07002208 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07002209 address & HPAGE_PMD_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002210 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2211 mmu_notifier_invalidate_range_start(&range);
2212 ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002213
2214 /*
2215 * If caller asks to setup a migration entries, we need a page to check
2216 * pmd against. Otherwise we can end up replacing wrong page.
2217 */
2218 VM_BUG_ON(freeze && !page);
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002219 if (page) {
2220 VM_WARN_ON_ONCE(!PageLocked(page));
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002221 if (page != pmd_page(*pmd))
2222 goto out;
2223 }
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002224
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002225repeat:
Dan Williams5c7fb562016-01-15 16:56:52 -08002226 if (pmd_trans_huge(*pmd)) {
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002227 if (!page) {
2228 page = pmd_page(*pmd);
Hugh Dickins1c2f6732021-02-04 18:32:31 -08002229 /*
2230 * An anonymous page must be locked, to ensure that a
2231 * concurrent reuse_swap_page() sees stable mapcount;
2232 * but reuse_swap_page() is not used on shmem or file,
2233 * and page lock must not be taken when zap_pmd_range()
2234 * calls __split_huge_pmd() while i_mmap_lock is held.
2235 */
2236 if (PageAnon(page)) {
2237 if (unlikely(!trylock_page(page))) {
2238 get_page(page);
2239 _pmd = *pmd;
2240 spin_unlock(ptl);
2241 lock_page(page);
2242 spin_lock(ptl);
2243 if (unlikely(!pmd_same(*pmd, _pmd))) {
2244 unlock_page(page);
2245 put_page(page);
2246 page = NULL;
2247 goto repeat;
2248 }
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002249 put_page(page);
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002250 }
Hugh Dickins1c2f6732021-02-04 18:32:31 -08002251 do_unlock_page = true;
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002252 }
2253 }
Dan Williams5c7fb562016-01-15 16:56:52 -08002254 if (PageMlocked(page))
Kirill A. Shutemov5f737712016-03-17 14:20:13 -07002255 clear_page_mlock(page);
Zi Yan84c3fc42017-09-08 16:11:01 -07002256 } else if (!(pmd_devmap(*pmd) || is_pmd_migration_entry(*pmd)))
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002257 goto out;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002258 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002259out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002260 spin_unlock(ptl);
Hugh Dickins1c2f6732021-02-04 18:32:31 -08002261 if (do_unlock_page)
Andrea Arcangelic444eb52020-05-27 19:06:24 -04002262 unlock_page(page);
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002263 /*
2264 * No need to double call mmu_notifier->invalidate_range() callback.
2265 * They are 3 cases to consider inside __split_huge_pmd_locked():
2266 * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious
2267 * 2) __split_huge_zero_page_pmd() read only zero page and any write
2268 * fault will trigger a flush_notify before pointing to a new page
2269 * (it is fine if the secondary mmu keeps pointing to the old zero
2270 * page in the meantime)
2271 * 3) Split a huge pmd into pte pointing to the same page. No need
2272 * to invalidate secondary tlb entry they are all still valid.
2273 * any further changes to individual pte will notify. So no need
2274 * to call mmu_notifier->invalidate_range()
2275 */
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002276 mmu_notifier_invalidate_range_only_end(&range);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002277}
2278
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002279void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
2280 bool freeze, struct page *page)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002281{
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002282 pgd_t *pgd;
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002283 p4d_t *p4d;
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002284 pud_t *pud;
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002285 pmd_t *pmd;
2286
Kirill A. Shutemov78ddc532016-01-15 16:52:42 -08002287 pgd = pgd_offset(vma->vm_mm, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002288 if (!pgd_present(*pgd))
2289 return;
2290
Kirill A. Shutemovc2febaf2017-03-09 17:24:07 +03002291 p4d = p4d_offset(pgd, address);
2292 if (!p4d_present(*p4d))
2293 return;
2294
2295 pud = pud_offset(p4d, address);
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002296 if (!pud_present(*pud))
2297 return;
2298
2299 pmd = pmd_offset(pud, address);
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002300
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002301 __split_huge_pmd(vma, pmd, address, freeze, page);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002302}
2303
Miaohe Lin71f9e582021-05-04 18:33:52 -07002304static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
2305{
2306 /*
2307 * If the new address isn't hpage aligned and it could previously
2308 * contain an hugepage: check if we need to split an huge pmd.
2309 */
2310 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
2311 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
2312 ALIGN(address, HPAGE_PMD_SIZE)))
2313 split_huge_pmd_address(vma, address, false, NULL);
2314}
2315
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002316void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002317 unsigned long start,
2318 unsigned long end,
2319 long adjust_next)
2320{
Miaohe Lin71f9e582021-05-04 18:33:52 -07002321 /* Check if we need to split start first. */
2322 split_huge_pmd_if_needed(vma, start);
2323
2324 /* Check if we need to split end next. */
2325 split_huge_pmd_if_needed(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002326
2327 /*
Miaohe Lin71f9e582021-05-04 18:33:52 -07002328 * If we're also updating the vma->vm_next->vm_start,
2329 * check if we need to split it.
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002330 */
2331 if (adjust_next > 0) {
2332 struct vm_area_struct *next = vma->vm_next;
2333 unsigned long nstart = next->vm_start;
Wei Yangf9d86a62020-10-13 16:53:57 -07002334 nstart += adjust_next;
Miaohe Lin71f9e582021-05-04 18:33:52 -07002335 split_huge_pmd_if_needed(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002336 }
2337}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002338
Hugh Dickins906f9cd2018-11-30 14:10:13 -08002339static void unmap_page(struct page *page)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002340{
Shakeel Butt013339d2020-12-14 19:06:39 -08002341 enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK |
Kirill A. Shutemovc7ab0d22017-02-24 14:58:01 -08002342 TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD;
Minchan Kim666e5a42017-05-03 14:54:20 -07002343 bool unmap_success;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002344
2345 VM_BUG_ON_PAGE(!PageHead(page), page);
2346
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002347 if (PageAnon(page))
Naoya Horiguchib5ff8162017-09-08 16:10:49 -07002348 ttu_flags |= TTU_SPLIT_FREEZE;
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002349
Minchan Kim666e5a42017-05-03 14:54:20 -07002350 unmap_success = try_to_unmap(page, ttu_flags);
2351 VM_BUG_ON_PAGE(!unmap_success, page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002352}
2353
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002354static void remap_page(struct page *page, unsigned int nr)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002355{
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002356 int i;
Kirill A. Shutemovace71a12017-02-24 14:57:45 -08002357 if (PageTransHuge(page)) {
2358 remove_migration_ptes(page, page, true);
2359 } else {
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002360 for (i = 0; i < nr; i++)
Kirill A. Shutemovace71a12017-02-24 14:57:45 -08002361 remove_migration_ptes(page + i, page + i, true);
2362 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002363}
2364
Alex Shi94866632020-12-15 12:33:24 -08002365static void lru_add_page_tail(struct page *head, struct page *tail,
Alex Shi88dcb9a2020-12-15 12:33:20 -08002366 struct lruvec *lruvec, struct list_head *list)
2367{
Alex Shi94866632020-12-15 12:33:24 -08002368 VM_BUG_ON_PAGE(!PageHead(head), head);
2369 VM_BUG_ON_PAGE(PageCompound(tail), head);
2370 VM_BUG_ON_PAGE(PageLRU(tail), head);
Alex Shi6168d0d2020-12-15 12:34:29 -08002371 lockdep_assert_held(&lruvec->lru_lock);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002372
Alex Shi6dbb5742020-12-15 12:33:29 -08002373 if (list) {
Alex Shi88dcb9a2020-12-15 12:33:20 -08002374 /* page reclaim is reclaiming a huge page */
Alex Shi6dbb5742020-12-15 12:33:29 -08002375 VM_WARN_ON(PageLRU(head));
Alex Shi94866632020-12-15 12:33:24 -08002376 get_page(tail);
2377 list_add_tail(&tail->lru, list);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002378 } else {
Alex Shi6dbb5742020-12-15 12:33:29 -08002379 /* head is still on lru (and we have it frozen) */
2380 VM_WARN_ON(!PageLRU(head));
2381 SetPageLRU(tail);
2382 list_add_tail(&tail->lru, &head->lru);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002383 }
2384}
2385
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07002386static void __split_huge_page_tail(struct page *head, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002387 struct lruvec *lruvec, struct list_head *list)
2388{
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002389 struct page *page_tail = head + tail;
2390
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07002391 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002392
2393 /*
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002394 * Clone page flags before unfreezing refcount.
2395 *
2396 * After successful get_page_unless_zero() might follow flags change,
Haitao Shi8958b242020-12-15 20:47:26 -08002397 * for example lock_page() which set PG_waiters.
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002398 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002399 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2400 page_tail->flags |= (head->flags &
2401 ((1L << PG_referenced) |
2402 (1L << PG_swapbacked) |
Huang Ying38d8b4e2017-07-06 15:37:18 -07002403 (1L << PG_swapcache) |
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002404 (1L << PG_mlocked) |
2405 (1L << PG_uptodate) |
2406 (1L << PG_active) |
Johannes Weiner1899ad12018-10-26 15:06:04 -07002407 (1L << PG_workingset) |
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002408 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002409 (1L << PG_unevictable) |
Catalin Marinas72e6afa2020-07-02 10:19:30 +01002410#ifdef CONFIG_64BIT
2411 (1L << PG_arch_2) |
2412#endif
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002413 (1L << PG_dirty)));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002414
Hugh Dickins173d9d92018-11-30 14:10:16 -08002415 /* ->mapping in first tail page is compound_mapcount */
2416 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2417 page_tail);
2418 page_tail->mapping = head->mapping;
2419 page_tail->index = head->index + tail;
2420
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002421 /* Page flags must be visible before we make the page non-compound. */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002422 smp_wmb();
2423
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002424 /*
2425 * Clear PageTail before unfreezing page refcount.
2426 *
2427 * After successful get_page_unless_zero() might follow put_page()
2428 * which needs correct compound_head().
2429 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002430 clear_compound_head(page_tail);
2431
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002432 /* Finally unfreeze refcount. Additional reference from page cache. */
2433 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2434 PageSwapCache(head)));
2435
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002436 if (page_is_young(head))
2437 set_page_young(page_tail);
2438 if (page_is_idle(head))
2439 set_page_idle(page_tail);
2440
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002441 page_cpupid_xchg_last(page_tail, page_cpupid_last(head));
Michal Hocko94723aa2018-04-10 16:30:07 -07002442
2443 /*
2444 * always add to the tail because some iterators expect new
2445 * pages to show after the currently processed elements - e.g.
2446 * migrate_pages
2447 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002448 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002449}
2450
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002451static void __split_huge_page(struct page *page, struct list_head *list,
Alex Shib6769832020-12-15 12:33:33 -08002452 pgoff_t end)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002453{
2454 struct page *head = compound_head(page);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002455 struct lruvec *lruvec;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002456 struct address_space *swap_cache = NULL;
2457 unsigned long offset = 0;
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002458 unsigned int nr = thp_nr_pages(head);
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07002459 int i;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002460
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002461 /* complete memcg works before add pages to LRU */
Zhou Guanghuibe6c8982021-03-12 21:08:30 -08002462 split_page_memcg(head, nr);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002463
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002464 if (PageAnon(head) && PageSwapCache(head)) {
2465 swp_entry_t entry = { .val = page_private(head) };
2466
2467 offset = swp_offset(entry);
2468 swap_cache = swap_address_space(entry);
2469 xa_lock(&swap_cache->i_pages);
2470 }
2471
Alex Shi6168d0d2020-12-15 12:34:29 -08002472 /* lock lru list/PageCompound, ref freezed by page_ref_freeze */
2473 lruvec = lock_page_lruvec(head);
Alex Shib6769832020-12-15 12:33:33 -08002474
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002475 for (i = nr - 1; i >= 1; i--) {
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07002476 __split_huge_page_tail(head, i, lruvec, list);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002477 /* Some pages can be beyond i_size: drop them from page cache */
2478 if (head[i].index >= end) {
Hugh Dickins2d077d42018-06-01 16:50:45 -07002479 ClearPageDirty(head + i);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002480 __delete_from_page_cache(head + i, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002481 if (IS_ENABLED(CONFIG_SHMEM) && PageSwapBacked(head))
2482 shmem_uncharge(head->mapping->host, 1);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002483 put_page(head + i);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002484 } else if (!PageAnon(page)) {
2485 __xa_store(&head->mapping->i_pages, head[i].index,
2486 head + i, 0);
2487 } else if (swap_cache) {
2488 __xa_store(&swap_cache->i_pages, offset + i,
2489 head + i, 0);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002490 }
2491 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002492
2493 ClearPageCompound(head);
Alex Shi6168d0d2020-12-15 12:34:29 -08002494 unlock_page_lruvec(lruvec);
Alex Shib6769832020-12-15 12:33:33 -08002495 /* Caller disabled irqs, so they are still disabled here */
Vlastimil Babkaf7da6772019-08-24 17:54:59 -07002496
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002497 split_page_owner(head, nr);
Vlastimil Babkaf7da6772019-08-24 17:54:59 -07002498
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002499 /* See comment in __split_huge_page_tail() */
2500 if (PageAnon(head)) {
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002501 /* Additional pin to swap cache */
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002502 if (PageSwapCache(head)) {
Huang Ying38d8b4e2017-07-06 15:37:18 -07002503 page_ref_add(head, 2);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002504 xa_unlock(&swap_cache->i_pages);
2505 } else {
Huang Ying38d8b4e2017-07-06 15:37:18 -07002506 page_ref_inc(head);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002507 }
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002508 } else {
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002509 /* Additional pin to page cache */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002510 page_ref_add(head, 2);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002511 xa_unlock(&head->mapping->i_pages);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002512 }
Alex Shib6769832020-12-15 12:33:33 -08002513 local_irq_enable();
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002514
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002515 remap_page(head, nr);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002516
Huang Yingc4f9c702020-10-15 20:06:07 -07002517 if (PageSwapCache(head)) {
2518 swp_entry_t entry = { .val = page_private(head) };
2519
2520 split_swap_cluster(entry);
2521 }
2522
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002523 for (i = 0; i < nr; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002524 struct page *subpage = head + i;
2525 if (subpage == page)
2526 continue;
2527 unlock_page(subpage);
2528
2529 /*
2530 * Subpages may be freed if there wasn't any mapping
2531 * like if add_to_swap() is running on a lru page that
2532 * had its mapping zapped. And freeing these pages
2533 * requires taking the lru_lock so we do the put_page
2534 * of the tail pages after the split is complete.
2535 */
2536 put_page(subpage);
2537 }
2538}
2539
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002540int total_mapcount(struct page *page)
2541{
Kirill A. Shutemov86b562b2020-10-15 20:05:33 -07002542 int i, compound, nr, ret;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002543
2544 VM_BUG_ON_PAGE(PageTail(page), page);
2545
2546 if (likely(!PageCompound(page)))
2547 return atomic_read(&page->_mapcount) + 1;
2548
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07002549 compound = compound_mapcount(page);
Kirill A. Shutemov86b562b2020-10-15 20:05:33 -07002550 nr = compound_nr(page);
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002551 if (PageHuge(page))
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07002552 return compound;
2553 ret = compound;
Kirill A. Shutemov86b562b2020-10-15 20:05:33 -07002554 for (i = 0; i < nr; i++)
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002555 ret += atomic_read(&page[i]._mapcount) + 1;
Kirill A. Shutemovdd78fed2016-07-26 15:25:26 -07002556 /* File pages has compound_mapcount included in _mapcount */
2557 if (!PageAnon(page))
Kirill A. Shutemov86b562b2020-10-15 20:05:33 -07002558 return ret - compound * nr;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002559 if (PageDoubleMap(page))
Kirill A. Shutemov86b562b2020-10-15 20:05:33 -07002560 ret -= nr;
Kirill A. Shutemovb20ce5e2016-01-15 16:54:37 -08002561 return ret;
2562}
2563
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002564/*
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002565 * This calculates accurately how many mappings a transparent hugepage
2566 * has (unlike page_mapcount() which isn't fully accurate). This full
2567 * accuracy is primarily needed to know if copy-on-write faults can
2568 * reuse the page and change the mapping to read-write instead of
2569 * copying them. At the same time this returns the total_mapcount too.
2570 *
2571 * The function returns the highest mapcount any one of the subpages
2572 * has. If the return value is one, even if different processes are
2573 * mapping different subpages of the transparent hugepage, they can
2574 * all reuse it, because each process is reusing a different subpage.
2575 *
2576 * The total_mapcount is instead counting all virtual mappings of the
2577 * subpages. If the total_mapcount is equal to "one", it tells the
2578 * caller all mappings belong to the same "mm" and in turn the
2579 * anon_vma of the transparent hugepage can become the vma->anon_vma
2580 * local one as no other process may be mapping any of the subpages.
2581 *
2582 * It would be more accurate to replace page_mapcount() with
2583 * page_trans_huge_mapcount(), however we only use
2584 * page_trans_huge_mapcount() in the copy-on-write faults where we
2585 * need full accuracy to avoid breaking page pinning, because
2586 * page_trans_huge_mapcount() is slower than page_mapcount().
2587 */
2588int page_trans_huge_mapcount(struct page *page, int *total_mapcount)
2589{
2590 int i, ret, _total_mapcount, mapcount;
2591
2592 /* hugetlbfs shouldn't call it */
2593 VM_BUG_ON_PAGE(PageHuge(page), page);
2594
2595 if (likely(!PageTransCompound(page))) {
2596 mapcount = atomic_read(&page->_mapcount) + 1;
2597 if (total_mapcount)
2598 *total_mapcount = mapcount;
2599 return mapcount;
2600 }
2601
2602 page = compound_head(page);
2603
2604 _total_mapcount = ret = 0;
Matthew Wilcox (Oracle)65dfe3c2020-10-15 20:05:39 -07002605 for (i = 0; i < thp_nr_pages(page); i++) {
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002606 mapcount = atomic_read(&page[i]._mapcount) + 1;
2607 ret = max(ret, mapcount);
2608 _total_mapcount += mapcount;
2609 }
2610 if (PageDoubleMap(page)) {
2611 ret -= 1;
Matthew Wilcox (Oracle)65dfe3c2020-10-15 20:05:39 -07002612 _total_mapcount -= thp_nr_pages(page);
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002613 }
2614 mapcount = compound_mapcount(page);
2615 ret += mapcount;
2616 _total_mapcount += mapcount;
2617 if (total_mapcount)
2618 *total_mapcount = _total_mapcount;
2619 return ret;
2620}
2621
Huang Yingb8f593c2017-07-06 15:37:28 -07002622/* Racy check whether the huge page can be split */
2623bool can_split_huge_page(struct page *page, int *pextra_pins)
2624{
2625 int extra_pins;
2626
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002627 /* Additional pins from page cache */
Huang Yingb8f593c2017-07-06 15:37:28 -07002628 if (PageAnon(page))
Matthew Wilcox (Oracle)e2333da2020-10-15 20:05:43 -07002629 extra_pins = PageSwapCache(page) ? thp_nr_pages(page) : 0;
Huang Yingb8f593c2017-07-06 15:37:28 -07002630 else
Matthew Wilcox (Oracle)e2333da2020-10-15 20:05:43 -07002631 extra_pins = thp_nr_pages(page);
Huang Yingb8f593c2017-07-06 15:37:28 -07002632 if (pextra_pins)
2633 *pextra_pins = extra_pins;
2634 return total_mapcount(page) == page_count(page) - extra_pins - 1;
2635}
2636
Andrea Arcangeli6d0a07e2016-05-12 15:42:25 -07002637/*
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002638 * This function splits huge page into normal pages. @page can point to any
2639 * subpage of huge page to split. Split doesn't change the position of @page.
2640 *
2641 * Only caller must hold pin on the @page, otherwise split fails with -EBUSY.
2642 * The huge page must be locked.
2643 *
2644 * If @list is null, tail pages will be added to LRU list, otherwise, to @list.
2645 *
2646 * Both head page and tail pages will inherit mapping, flags, and so on from
2647 * the hugepage.
2648 *
2649 * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if
2650 * they are not mapped.
2651 *
2652 * Returns 0 if the hugepage is split successfully.
2653 * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under
2654 * us.
2655 */
2656int split_huge_page_to_list(struct page *page, struct list_head *list)
2657{
2658 struct page *head = compound_head(page);
Wei Yanga8803e62020-01-30 22:14:32 -08002659 struct deferred_split *ds_queue = get_deferred_split_queue(head);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002660 struct anon_vma *anon_vma = NULL;
2661 struct address_space *mapping = NULL;
2662 int count, mapcount, extra_pins, ret;
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002663 pgoff_t end;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002664
Wei Yangcb829622020-01-30 22:14:29 -08002665 VM_BUG_ON_PAGE(is_huge_zero_page(head), head);
Wei Yanga8803e62020-01-30 22:14:32 -08002666 VM_BUG_ON_PAGE(!PageLocked(head), head);
2667 VM_BUG_ON_PAGE(!PageCompound(head), head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002668
Wei Yanga8803e62020-01-30 22:14:32 -08002669 if (PageWriteback(head))
Huang Ying59807682017-09-06 16:22:34 -07002670 return -EBUSY;
2671
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002672 if (PageAnon(head)) {
2673 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002674 * The caller does not necessarily hold an mmap_lock that would
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002675 * prevent the anon_vma disappearing so we first we take a
2676 * reference to it and then lock the anon_vma for write. This
2677 * is similar to page_lock_anon_vma_read except the write lock
2678 * is taken to serialise against parallel split or collapse
2679 * operations.
2680 */
2681 anon_vma = page_get_anon_vma(head);
2682 if (!anon_vma) {
2683 ret = -EBUSY;
2684 goto out;
2685 }
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002686 end = -1;
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002687 mapping = NULL;
2688 anon_vma_lock_write(anon_vma);
2689 } else {
2690 mapping = head->mapping;
2691
2692 /* Truncated ? */
2693 if (!mapping) {
2694 ret = -EBUSY;
2695 goto out;
2696 }
2697
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002698 anon_vma = NULL;
2699 i_mmap_lock_read(mapping);
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002700
2701 /*
2702 *__split_huge_page() may need to trim off pages beyond EOF:
2703 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2704 * which cannot be nested inside the page tree lock. So note
2705 * end now: i_size itself may be changed at any moment, but
2706 * head page lock is good enough to serialize the trimming.
2707 */
2708 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002709 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002710
2711 /*
Hugh Dickins906f9cd2018-11-30 14:10:13 -08002712 * Racy check if we can split the page, before unmap_page() will
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002713 * split PMDs
2714 */
Huang Yingb8f593c2017-07-06 15:37:28 -07002715 if (!can_split_huge_page(head, &extra_pins)) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002716 ret = -EBUSY;
2717 goto out_unlock;
2718 }
2719
Hugh Dickins906f9cd2018-11-30 14:10:13 -08002720 unmap_page(head);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002721 VM_BUG_ON_PAGE(compound_mapcount(head), head);
2722
Alex Shib6769832020-12-15 12:33:33 -08002723 /* block interrupt reentry in xa_lock and spinlock */
2724 local_irq_disable();
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002725 if (mapping) {
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002726 XA_STATE(xas, &mapping->i_pages, page_index(head));
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002727
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002728 /*
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002729 * Check if the head page is present in page cache.
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002730 * We assume all tail are present too, if head is there.
2731 */
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002732 xa_lock(&mapping->i_pages);
2733 if (xas_load(&xas) != head)
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002734 goto fail;
2735 }
2736
Joonsoo Kim0139aa72016-05-19 17:10:49 -07002737 /* Prevent deferred_split_scan() touching ->_refcount */
Yang Shi364c1ee2019-09-23 15:38:06 -07002738 spin_lock(&ds_queue->split_queue_lock);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002739 count = page_count(head);
2740 mapcount = total_mapcount(head);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002741 if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002742 if (!list_empty(page_deferred_list(head))) {
Yang Shi364c1ee2019-09-23 15:38:06 -07002743 ds_queue->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002744 list_del(page_deferred_list(head));
2745 }
Wei Yangafb97172020-01-30 22:14:35 -08002746 spin_unlock(&ds_queue->split_queue_lock);
Kirill A. Shutemov06d3eff2019-10-18 20:20:30 -07002747 if (mapping) {
Muchun Songbf9ecea2021-02-24 12:03:27 -08002748 int nr = thp_nr_pages(head);
2749
Wei Yanga8803e62020-01-30 22:14:32 -08002750 if (PageSwapBacked(head))
Muchun Song57b28472021-02-24 12:03:31 -08002751 __mod_lruvec_page_state(head, NR_SHMEM_THPS,
2752 -nr);
Kirill A. Shutemov06d3eff2019-10-18 20:20:30 -07002753 else
Muchun Songbf9ecea2021-02-24 12:03:27 -08002754 __mod_lruvec_page_state(head, NR_FILE_THPS,
2755 -nr);
Kirill A. Shutemov06d3eff2019-10-18 20:20:30 -07002756 }
2757
Alex Shib6769832020-12-15 12:33:33 -08002758 __split_huge_page(page, list, end);
Huang Yingc4f9c702020-10-15 20:06:07 -07002759 ret = 0;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002760 } else {
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002761 if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) {
2762 pr_alert("total_mapcount: %u, page_count(): %u\n",
2763 mapcount, count);
2764 if (PageTail(page))
2765 dump_page(head, NULL);
2766 dump_page(page, "total_mapcount(head) > 0");
2767 BUG();
2768 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002769 spin_unlock(&ds_queue->split_queue_lock);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002770fail: if (mapping)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002771 xa_unlock(&mapping->i_pages);
Alex Shib6769832020-12-15 12:33:33 -08002772 local_irq_enable();
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002773 remap_page(head, thp_nr_pages(head));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002774 ret = -EBUSY;
2775 }
2776
2777out_unlock:
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002778 if (anon_vma) {
2779 anon_vma_unlock_write(anon_vma);
2780 put_anon_vma(anon_vma);
2781 }
2782 if (mapping)
2783 i_mmap_unlock_read(mapping);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002784out:
2785 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2786 return ret;
2787}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002788
2789void free_transhuge_page(struct page *page)
2790{
Yang Shi87eaceb2019-09-23 15:38:15 -07002791 struct deferred_split *ds_queue = get_deferred_split_queue(page);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002792 unsigned long flags;
2793
Yang Shi364c1ee2019-09-23 15:38:06 -07002794 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002795 if (!list_empty(page_deferred_list(page))) {
Yang Shi364c1ee2019-09-23 15:38:06 -07002796 ds_queue->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002797 list_del(page_deferred_list(page));
2798 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002799 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002800 free_compound_page(page);
2801}
2802
2803void deferred_split_huge_page(struct page *page)
2804{
Yang Shi87eaceb2019-09-23 15:38:15 -07002805 struct deferred_split *ds_queue = get_deferred_split_queue(page);
2806#ifdef CONFIG_MEMCG
Roman Gushchinbcfe06b2020-12-01 13:58:27 -08002807 struct mem_cgroup *memcg = page_memcg(compound_head(page));
Yang Shi87eaceb2019-09-23 15:38:15 -07002808#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002809 unsigned long flags;
2810
2811 VM_BUG_ON_PAGE(!PageTransHuge(page), page);
2812
Yang Shi87eaceb2019-09-23 15:38:15 -07002813 /*
2814 * The try_to_unmap() in page reclaim path might reach here too,
2815 * this may cause a race condition to corrupt deferred split queue.
2816 * And, if page reclaim is already handling the same page, it is
2817 * unnecessary to handle it again in shrinker.
2818 *
2819 * Check PageSwapCache to determine if the page is being
2820 * handled by page reclaim since THP swap would add the page into
2821 * swap cache before calling try_to_unmap().
2822 */
2823 if (PageSwapCache(page))
2824 return;
2825
Yang Shi364c1ee2019-09-23 15:38:06 -07002826 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002827 if (list_empty(page_deferred_list(page))) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07002828 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Yang Shi364c1ee2019-09-23 15:38:06 -07002829 list_add_tail(page_deferred_list(page), &ds_queue->split_queue);
2830 ds_queue->split_queue_len++;
Yang Shi87eaceb2019-09-23 15:38:15 -07002831#ifdef CONFIG_MEMCG
2832 if (memcg)
2833 memcg_set_shrinker_bit(memcg, page_to_nid(page),
2834 deferred_split_shrinker.id);
2835#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002836 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002837 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002838}
2839
2840static unsigned long deferred_split_count(struct shrinker *shrink,
2841 struct shrink_control *sc)
2842{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08002843 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Yang Shi364c1ee2019-09-23 15:38:06 -07002844 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
Yang Shi87eaceb2019-09-23 15:38:15 -07002845
2846#ifdef CONFIG_MEMCG
2847 if (sc->memcg)
2848 ds_queue = &sc->memcg->deferred_split_queue;
2849#endif
Yang Shi364c1ee2019-09-23 15:38:06 -07002850 return READ_ONCE(ds_queue->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002851}
2852
2853static unsigned long deferred_split_scan(struct shrinker *shrink,
2854 struct shrink_control *sc)
2855{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08002856 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Yang Shi364c1ee2019-09-23 15:38:06 -07002857 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002858 unsigned long flags;
2859 LIST_HEAD(list), *pos, *next;
2860 struct page *page;
2861 int split = 0;
2862
Yang Shi87eaceb2019-09-23 15:38:15 -07002863#ifdef CONFIG_MEMCG
2864 if (sc->memcg)
2865 ds_queue = &sc->memcg->deferred_split_queue;
2866#endif
2867
Yang Shi364c1ee2019-09-23 15:38:06 -07002868 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002869 /* Take pin on all head pages to avoid freeing them under us */
Yang Shi364c1ee2019-09-23 15:38:06 -07002870 list_for_each_safe(pos, next, &ds_queue->split_queue) {
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002871 page = list_entry((void *)pos, struct page, mapping);
2872 page = compound_head(page);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08002873 if (get_page_unless_zero(page)) {
2874 list_move(page_deferred_list(page), &list);
2875 } else {
2876 /* We lost race with put_compound_page() */
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002877 list_del_init(page_deferred_list(page));
Yang Shi364c1ee2019-09-23 15:38:06 -07002878 ds_queue->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002879 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08002880 if (!--sc->nr_to_scan)
2881 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002882 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002883 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002884
2885 list_for_each_safe(pos, next, &list) {
2886 page = list_entry((void *)pos, struct page, mapping);
Kirill A. Shutemovfa41b902018-03-22 16:17:31 -07002887 if (!trylock_page(page))
2888 goto next;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002889 /* split_huge_page() removes page from list on success */
2890 if (!split_huge_page(page))
2891 split++;
2892 unlock_page(page);
Kirill A. Shutemovfa41b902018-03-22 16:17:31 -07002893next:
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002894 put_page(page);
2895 }
2896
Yang Shi364c1ee2019-09-23 15:38:06 -07002897 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2898 list_splice_tail(&list, &ds_queue->split_queue);
2899 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002900
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08002901 /*
2902 * Stop shrinker if we didn't split any page, but the queue is empty.
2903 * This can happen if pages were freed under us.
2904 */
Yang Shi364c1ee2019-09-23 15:38:06 -07002905 if (!split && list_empty(&ds_queue->split_queue))
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08002906 return SHRINK_STOP;
2907 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002908}
2909
2910static struct shrinker deferred_split_shrinker = {
2911 .count_objects = deferred_split_count,
2912 .scan_objects = deferred_split_scan,
2913 .seeks = DEFAULT_SEEKS,
Yang Shi87eaceb2019-09-23 15:38:15 -07002914 .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE |
2915 SHRINKER_NONSLAB,
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002916};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002917
2918#ifdef CONFIG_DEBUG_FS
Zi Yanfa6c0232021-05-04 18:34:23 -07002919static void split_huge_pages_all(void)
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002920{
2921 struct zone *zone;
2922 struct page *page;
2923 unsigned long pfn, max_zone_pfn;
2924 unsigned long total = 0, split = 0;
2925
Zi Yanfa6c0232021-05-04 18:34:23 -07002926 pr_debug("Split all THPs\n");
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002927 for_each_populated_zone(zone) {
2928 max_zone_pfn = zone_end_pfn(zone);
2929 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
2930 if (!pfn_valid(pfn))
2931 continue;
2932
2933 page = pfn_to_page(pfn);
2934 if (!get_page_unless_zero(page))
2935 continue;
2936
2937 if (zone != page_zone(page))
2938 goto next;
2939
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002940 if (!PageHead(page) || PageHuge(page) || !PageLRU(page))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002941 goto next;
2942
2943 total++;
2944 lock_page(page);
2945 if (!split_huge_page(page))
2946 split++;
2947 unlock_page(page);
2948next:
2949 put_page(page);
Zi Yanfa6c0232021-05-04 18:34:23 -07002950 cond_resched();
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002951 }
2952 }
2953
Zi Yanfa6c0232021-05-04 18:34:23 -07002954 pr_debug("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002955}
Zi Yanfa6c0232021-05-04 18:34:23 -07002956
2957static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
2958{
2959 return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) ||
2960 is_vm_hugetlb_page(vma);
2961}
2962
2963static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
2964 unsigned long vaddr_end)
2965{
2966 int ret = 0;
2967 struct task_struct *task;
2968 struct mm_struct *mm;
2969 unsigned long total = 0, split = 0;
2970 unsigned long addr;
2971
2972 vaddr_start &= PAGE_MASK;
2973 vaddr_end &= PAGE_MASK;
2974
2975 /* Find the task_struct from pid */
2976 rcu_read_lock();
2977 task = find_task_by_vpid(pid);
2978 if (!task) {
2979 rcu_read_unlock();
2980 ret = -ESRCH;
2981 goto out;
2982 }
2983 get_task_struct(task);
2984 rcu_read_unlock();
2985
2986 /* Find the mm_struct */
2987 mm = get_task_mm(task);
2988 put_task_struct(task);
2989
2990 if (!mm) {
2991 ret = -EINVAL;
2992 goto out;
2993 }
2994
2995 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n",
2996 pid, vaddr_start, vaddr_end);
2997
2998 mmap_read_lock(mm);
2999 /*
3000 * always increase addr by PAGE_SIZE, since we could have a PTE page
3001 * table filled with PTE-mapped THPs, each of which is distinct.
3002 */
3003 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
3004 struct vm_area_struct *vma = find_vma(mm, addr);
3005 unsigned int follflags;
3006 struct page *page;
3007
3008 if (!vma || addr < vma->vm_start)
3009 break;
3010
3011 /* skip special VMA and hugetlb VMA */
3012 if (vma_not_suitable_for_thp_split(vma)) {
3013 addr = vma->vm_end;
3014 continue;
3015 }
3016
3017 /* FOLL_DUMP to ignore special (like zero) pages */
3018 follflags = FOLL_GET | FOLL_DUMP;
3019 page = follow_page(vma, addr, follflags);
3020
3021 if (IS_ERR(page))
3022 continue;
3023 if (!page)
3024 continue;
3025
3026 if (!is_transparent_hugepage(page))
3027 goto next;
3028
3029 total++;
3030 if (!can_split_huge_page(compound_head(page), NULL))
3031 goto next;
3032
3033 if (!trylock_page(page))
3034 goto next;
3035
3036 if (!split_huge_page(page))
3037 split++;
3038
3039 unlock_page(page);
3040next:
3041 put_page(page);
3042 cond_resched();
3043 }
3044 mmap_read_unlock(mm);
3045 mmput(mm);
3046
3047 pr_debug("%lu of %lu THP split\n", split, total);
3048
3049out:
3050 return ret;
3051}
3052
Zi Yanfbe37502021-05-04 18:34:26 -07003053static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
3054 pgoff_t off_end)
3055{
3056 struct filename *file;
3057 struct file *candidate;
3058 struct address_space *mapping;
3059 int ret = -EINVAL;
3060 pgoff_t index;
3061 int nr_pages = 1;
3062 unsigned long total = 0, split = 0;
3063
3064 file = getname_kernel(file_path);
3065 if (IS_ERR(file))
3066 return ret;
3067
3068 candidate = file_open_name(file, O_RDONLY, 0);
3069 if (IS_ERR(candidate))
3070 goto out;
3071
3072 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n",
3073 file_path, off_start, off_end);
3074
3075 mapping = candidate->f_mapping;
3076
3077 for (index = off_start; index < off_end; index += nr_pages) {
3078 struct page *fpage = pagecache_get_page(mapping, index,
3079 FGP_ENTRY | FGP_HEAD, 0);
3080
3081 nr_pages = 1;
3082 if (xa_is_value(fpage) || !fpage)
3083 continue;
3084
3085 if (!is_transparent_hugepage(fpage))
3086 goto next;
3087
3088 total++;
3089 nr_pages = thp_nr_pages(fpage);
3090
3091 if (!trylock_page(fpage))
3092 goto next;
3093
3094 if (!split_huge_page(fpage))
3095 split++;
3096
3097 unlock_page(fpage);
3098next:
3099 put_page(fpage);
3100 cond_resched();
3101 }
3102
3103 filp_close(candidate, NULL);
3104 ret = 0;
3105
3106 pr_debug("%lu of %lu file-backed THP split\n", split, total);
3107out:
3108 putname(file);
3109 return ret;
3110}
3111
Zi Yanfa6c0232021-05-04 18:34:23 -07003112#define MAX_INPUT_BUF_SZ 255
3113
3114static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
3115 size_t count, loff_t *ppops)
3116{
3117 static DEFINE_MUTEX(split_debug_mutex);
3118 ssize_t ret;
Zi Yanfbe37502021-05-04 18:34:26 -07003119 /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3120 char input_buf[MAX_INPUT_BUF_SZ];
Zi Yanfa6c0232021-05-04 18:34:23 -07003121 int pid;
3122 unsigned long vaddr_start, vaddr_end;
3123
3124 ret = mutex_lock_interruptible(&split_debug_mutex);
3125 if (ret)
3126 return ret;
3127
3128 ret = -EFAULT;
3129
3130 memset(input_buf, 0, MAX_INPUT_BUF_SZ);
3131 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
3132 goto out;
3133
3134 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
Zi Yanfbe37502021-05-04 18:34:26 -07003135
3136 if (input_buf[0] == '/') {
3137 char *tok;
3138 char *buf = input_buf;
3139 char file_path[MAX_INPUT_BUF_SZ];
3140 pgoff_t off_start = 0, off_end = 0;
3141 size_t input_len = strlen(input_buf);
3142
3143 tok = strsep(&buf, ",");
3144 if (tok) {
3145 strncpy(file_path, tok, MAX_INPUT_BUF_SZ);
3146 } else {
3147 ret = -EINVAL;
3148 goto out;
3149 }
3150
3151 ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3152 if (ret != 2) {
3153 ret = -EINVAL;
3154 goto out;
3155 }
3156 ret = split_huge_pages_in_file(file_path, off_start, off_end);
3157 if (!ret)
3158 ret = input_len;
3159
3160 goto out;
3161 }
3162
Zi Yanfa6c0232021-05-04 18:34:23 -07003163 ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3164 if (ret == 1 && pid == 1) {
3165 split_huge_pages_all();
3166 ret = strlen(input_buf);
3167 goto out;
3168 } else if (ret != 3) {
3169 ret = -EINVAL;
3170 goto out;
3171 }
3172
3173 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3174 if (!ret)
3175 ret = strlen(input_buf);
3176out:
3177 mutex_unlock(&split_debug_mutex);
3178 return ret;
3179
3180}
3181
3182static const struct file_operations split_huge_pages_fops = {
3183 .owner = THIS_MODULE,
3184 .write = split_huge_pages_write,
3185 .llseek = no_llseek,
3186};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003187
3188static int __init split_huge_pages_debugfs(void)
3189{
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08003190 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3191 &split_huge_pages_fops);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003192 return 0;
3193}
3194late_initcall(split_huge_pages_debugfs);
3195#endif
Zi Yan616b8372017-09-08 16:10:57 -07003196
3197#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
3198void set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
3199 struct page *page)
3200{
3201 struct vm_area_struct *vma = pvmw->vma;
3202 struct mm_struct *mm = vma->vm_mm;
3203 unsigned long address = pvmw->address;
3204 pmd_t pmdval;
3205 swp_entry_t entry;
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003206 pmd_t pmdswp;
Zi Yan616b8372017-09-08 16:10:57 -07003207
3208 if (!(pvmw->pmd && !pvmw->pte))
3209 return;
3210
Zi Yan616b8372017-09-08 16:10:57 -07003211 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
Huang Ying8a8683a2020-03-05 22:28:29 -08003212 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
Zi Yan616b8372017-09-08 16:10:57 -07003213 if (pmd_dirty(pmdval))
3214 set_page_dirty(page);
3215 entry = make_migration_entry(page, pmd_write(pmdval));
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003216 pmdswp = swp_entry_to_pmd(entry);
3217 if (pmd_soft_dirty(pmdval))
3218 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
3219 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
Zi Yan616b8372017-09-08 16:10:57 -07003220 page_remove_rmap(page, true);
3221 put_page(page);
Zi Yan616b8372017-09-08 16:10:57 -07003222}
3223
3224void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3225{
3226 struct vm_area_struct *vma = pvmw->vma;
3227 struct mm_struct *mm = vma->vm_mm;
3228 unsigned long address = pvmw->address;
3229 unsigned long mmun_start = address & HPAGE_PMD_MASK;
3230 pmd_t pmde;
3231 swp_entry_t entry;
3232
3233 if (!(pvmw->pmd && !pvmw->pte))
3234 return;
3235
3236 entry = pmd_to_swp_entry(*pvmw->pmd);
3237 get_page(new);
3238 pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot));
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003239 if (pmd_swp_soft_dirty(*pvmw->pmd))
3240 pmde = pmd_mksoft_dirty(pmde);
Zi Yan616b8372017-09-08 16:10:57 -07003241 if (is_write_migration_entry(entry))
Linus Torvaldsf55e1012017-11-29 09:01:01 -08003242 pmde = maybe_pmd_mkwrite(pmde, vma);
Zi Yan616b8372017-09-08 16:10:57 -07003243
3244 flush_cache_range(vma, mmun_start, mmun_start + HPAGE_PMD_SIZE);
Naoya Horiguchie71769a2018-04-20 14:55:45 -07003245 if (PageAnon(new))
3246 page_add_anon_rmap(new, vma, mmun_start, true);
3247 else
3248 page_add_file_rmap(new, true);
Zi Yan616b8372017-09-08 16:10:57 -07003249 set_pmd_at(mm, mmun_start, pvmw->pmd, pmde);
Kirill A. Shutemove125fe42018-10-05 15:51:41 -07003250 if ((vma->vm_flags & VM_LOCKED) && !PageDoubleMap(new))
Zi Yan616b8372017-09-08 16:10:57 -07003251 mlock_vma_page(new);
3252 update_mmu_cache_pmd(vma, address, pvmw->pmd);
3253}
3254#endif