blob: f31f02472396e7671132e965a93664a62238fca5 [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 Wilcox (Oracle)fb5c2022022-06-28 20:15:29 -040021#include <linux/backing-dev.h>
Matthew Wilcox4897c762015-09-08 14:58:45 -070022#include <linux/dax.h>
Andrea Arcangeliba761492011-01-13 15:46:58 -080023#include <linux/khugepaged.h>
Andrea Arcangeli878aee72011-01-13 15:47:10 -080024#include <linux/freezer.h>
Dan Williamsf25748e32016-01-15 16:56:43 -080025#include <linux/pfn_t.h>
Andrea Arcangelia664b2d2011-01-13 15:47:17 -080026#include <linux/mman.h>
Dan Williams3565fce2016-01-15 16:56:55 -080027#include <linux/memremap.h>
Ralf Baechle325adeb2012-10-15 13:44:56 +020028#include <linux/pagemap.h>
Kirill A. Shutemov49071d42016-01-15 16:54:40 -080029#include <linux/debugfs.h>
Mel Gorman4daae3b2012-11-02 11:33:45 +000030#include <linux/migrate.h>
Sasha Levin43b5fbb2013-02-22 16:32:27 -080031#include <linux/hashtable.h>
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -070032#include <linux/userfaultfd_k.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070033#include <linux/page_idle.h>
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -070034#include <linux/shmem_fs.h>
Michal Hocko6b31d592017-08-18 15:16:15 -070035#include <linux/oom.h>
Anshuman Khandual98fa15f2019-03-05 15:42:58 -080036#include <linux/numa.h>
Vlastimil Babkaf7da6772019-08-24 17:54:59 -070037#include <linux/page_owner.h>
Huang Yinga1a3a2f2022-03-22 14:46:27 -070038#include <linux/sched/sysctl.h>
Aneesh Kumar K.V467b1712022-08-18 18:40:41 +053039#include <linux/memory-tiers.h>
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080040
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080041#include <asm/tlb.h>
42#include <asm/pgalloc.h>
43#include "internal.h"
NeilBrown014bb1d2022-05-09 18:20:47 -070044#include "swap.h"
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080045
Anshuman Khandual283fd6f2022-03-24 18:09:58 -070046#define CREATE_TRACE_POINTS
47#include <trace/events/thp.h>
48
Andrea Arcangeliba761492011-01-13 15:46:58 -080049/*
Michael DeGuzisb14d5952017-05-17 15:19:21 -040050 * By default, transparent hugepage support is disabled in order to avoid
51 * risking an increased memory footprint for applications that are not
52 * guaranteed to benefit from it. When transparent hugepage support is
53 * enabled, it is for all mappings, and khugepaged scans all mappings.
Jianguo Wu8bfa3f92013-11-12 15:07:16 -080054 * Defrag is invoked by khugepaged hugepage allocations and by page faults
55 * for all hugepage allocations.
Andrea Arcangeliba761492011-01-13 15:46:58 -080056 */
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -080057unsigned long transparent_hugepage_flags __read_mostly =
Andrea Arcangeli13ece882011-01-13 15:47:07 -080058#ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS
Andrea Arcangeliba761492011-01-13 15:46:58 -080059 (1<<TRANSPARENT_HUGEPAGE_FLAG)|
Andrea Arcangeli13ece882011-01-13 15:47:07 -080060#endif
61#ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE
62 (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)|
63#endif
Mel Gorman444eb2a42016-03-17 14:19:23 -070064 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)|
Kirill A. Shutemov79da5402012-12-12 13:51:12 -080065 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)|
66 (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Andrea Arcangeliba761492011-01-13 15:46:58 -080067
Qi Zheng54d91722023-09-11 17:44:16 +080068static struct shrinker *deferred_split_shrinker;
69static unsigned long deferred_split_count(struct shrinker *shrink,
70 struct shrink_control *sc);
71static unsigned long deferred_split_scan(struct shrinker *shrink,
72 struct shrink_control *sc);
Andrea Arcangelif0005652011-01-13 15:47:04 -080073
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -080074static atomic_t huge_zero_refcount;
Wang, Yalin56873f42015-02-11 15:24:51 -080075struct page *huge_zero_page __read_mostly;
Hugh Dickins3b77e8c2021-06-15 18:23:49 -070076unsigned long huge_zero_pfn __read_mostly = ~0UL;
Kirill A. Shutemov4a6c1292012-12-12 13:50:47 -080077
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -070078bool hugepage_vma_check(struct vm_area_struct *vma, unsigned long vm_flags,
79 bool smaps, bool in_pf, bool enforce_sysfs)
Michal Hocko7635d9c2018-12-28 00:38:21 -080080{
Yang Shi9fec5162022-06-16 10:48:37 -070081 if (!vma->vm_mm) /* vdso */
Yang Shic0630662019-07-18 15:57:27 -070082 return false;
Yang Shi9fec5162022-06-16 10:48:37 -070083
Yang Shi7da4e2c2022-06-16 10:48:38 -070084 /*
85 * Explicitly disabled through madvise or prctl, or some
86 * architectures may disable THP for some mappings, for
87 * example, s390 kvm.
88 * */
89 if ((vm_flags & VM_NOHUGEPAGE) ||
90 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
91 return false;
92 /*
93 * If the hardware/firmware marked hugepage support disabled.
94 */
Peter Xu3c556d22023-03-15 13:16:42 -040095 if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED))
Yang Shi9fec5162022-06-16 10:48:37 -070096 return false;
97
Yang Shi7da4e2c2022-06-16 10:48:38 -070098 /* khugepaged doesn't collapse DAX vma, but page fault is fine. */
99 if (vma_is_dax(vma))
100 return in_pf;
101
102 /*
Zach O'Keefe7a817512023-09-25 13:01:10 -0700103 * khugepaged special VMA and hugetlb VMA.
Yang Shi7da4e2c2022-06-16 10:48:38 -0700104 * Must be checked after dax since some dax mappings may have
105 * VM_MIXEDMAP set.
106 */
Zach O'Keefe7a817512023-09-25 13:01:10 -0700107 if (!in_pf && !smaps && (vm_flags & VM_NO_KHUGEPAGED))
Yang Shi9fec5162022-06-16 10:48:37 -0700108 return false;
109
Yang Shi7da4e2c2022-06-16 10:48:38 -0700110 /*
111 * Check alignment for file vma and size for both file and anon vma.
112 *
113 * Skip the check for page fault. Huge fault does the check in fault
114 * handlers. And this check is not suitable for huge PUD fault.
115 */
116 if (!in_pf &&
117 !transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE)))
Yang Shi9fec5162022-06-16 10:48:37 -0700118 return false;
119
Yang Shi7da4e2c2022-06-16 10:48:38 -0700120 /*
121 * Enabled via shmem mount options or sysfs settings.
122 * Must be done before hugepage flags check since shmem has its
123 * own flags.
124 */
125 if (!in_pf && shmem_file(vma->vm_file))
David Stevens2cf13382023-01-13 11:30:11 +0900126 return shmem_is_huge(file_inode(vma->vm_file), vma->vm_pgoff,
127 !enforce_sysfs, vma->vm_mm, vm_flags);
Yang Shi9fec5162022-06-16 10:48:37 -0700128
Zach O'Keefea7f4e6e2022-07-06 16:59:25 -0700129 /* Enforce sysfs THP requirements as necessary */
130 if (enforce_sysfs &&
131 (!hugepage_flags_enabled() || (!(vm_flags & VM_HUGEPAGE) &&
132 !hugepage_flags_always())))
Yang Shi9fec5162022-06-16 10:48:37 -0700133 return false;
134
Zach O'Keefe7a817512023-09-25 13:01:10 -0700135 if (!vma_is_anonymous(vma)) {
136 /*
137 * Trust that ->huge_fault() handlers know what they are doing
138 * in fault path.
139 */
140 if (((in_pf || smaps)) && vma->vm_ops->huge_fault)
141 return true;
142 /* Only regular file is valid in collapse path */
143 if (((!in_pf || smaps)) && file_thp_enabled(vma))
144 return true;
Yang Shi9fec5162022-06-16 10:48:37 -0700145 return false;
Zach O'Keefe7a817512023-09-25 13:01:10 -0700146 }
Yang Shi9fec5162022-06-16 10:48:37 -0700147
148 if (vma_is_temporary_stack(vma))
149 return false;
150
151 /*
152 * THPeligible bit of smaps should show 1 for proper VMAs even
153 * though anon_vma is not initialized yet.
Yang Shi7da4e2c2022-06-16 10:48:38 -0700154 *
155 * Allow page fault since anon_vma may be not initialized until
156 * the first page fault.
Yang Shi9fec5162022-06-16 10:48:37 -0700157 */
158 if (!vma->anon_vma)
Yang Shi7da4e2c2022-06-16 10:48:38 -0700159 return (smaps || in_pf);
Yang Shi9fec5162022-06-16 10:48:37 -0700160
161 return true;
Michal Hocko7635d9c2018-12-28 00:38:21 -0800162}
163
Miaohe Linaaa97052021-05-04 18:33:55 -0700164static bool get_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800165{
166 struct page *zero_page;
167retry:
168 if (likely(atomic_inc_not_zero(&huge_zero_refcount)))
Miaohe Linaaa97052021-05-04 18:33:55 -0700169 return true;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800170
171 zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,
172 HPAGE_PMD_ORDER);
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800173 if (!zero_page) {
174 count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED);
Miaohe Linaaa97052021-05-04 18:33:55 -0700175 return false;
Kirill A. Shutemovd8a8e1f2012-12-12 13:51:09 -0800176 }
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800177 preempt_disable();
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700178 if (cmpxchg(&huge_zero_page, NULL, zero_page)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800179 preempt_enable();
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700180 __free_pages(zero_page, compound_order(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800181 goto retry;
182 }
Hugh Dickins3b77e8c2021-06-15 18:23:49 -0700183 WRITE_ONCE(huge_zero_pfn, page_to_pfn(zero_page));
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800184
185 /* We take additional reference here. It will be put back by shrinker */
186 atomic_set(&huge_zero_refcount, 2);
187 preempt_enable();
Liu Shixinf4981502022-09-09 10:16:53 +0800188 count_vm_event(THP_ZERO_PAGE_ALLOC);
Miaohe Linaaa97052021-05-04 18:33:55 -0700189 return true;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800190}
191
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700192static void put_huge_zero_page(void)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800193{
194 /*
195 * Counter should never go to zero here. Only shrinker can put
196 * last reference.
197 */
198 BUG_ON(atomic_dec_and_test(&huge_zero_refcount));
199}
200
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700201struct page *mm_get_huge_zero_page(struct mm_struct *mm)
202{
203 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
204 return READ_ONCE(huge_zero_page);
205
206 if (!get_huge_zero_page())
207 return NULL;
208
209 if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
210 put_huge_zero_page();
211
212 return READ_ONCE(huge_zero_page);
213}
214
215void mm_put_huge_zero_page(struct mm_struct *mm)
216{
217 if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags))
218 put_huge_zero_page();
219}
220
Glauber Costa48896462013-08-28 10:18:15 +1000221static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink,
222 struct shrink_control *sc)
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800223{
Glauber Costa48896462013-08-28 10:18:15 +1000224 /* we can free zero page only if last reference remains */
225 return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0;
226}
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800227
Glauber Costa48896462013-08-28 10:18:15 +1000228static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink,
229 struct shrink_control *sc)
230{
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800231 if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) {
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700232 struct page *zero_page = xchg(&huge_zero_page, NULL);
233 BUG_ON(zero_page == NULL);
Hugh Dickins3b77e8c2021-06-15 18:23:49 -0700234 WRITE_ONCE(huge_zero_pfn, ~0UL);
Yu Zhao5ddacbe2014-10-29 14:50:26 -0700235 __free_pages(zero_page, compound_order(zero_page));
Glauber Costa48896462013-08-28 10:18:15 +1000236 return HPAGE_PMD_NR;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800237 }
238
239 return 0;
240}
241
Qi Zheng54d91722023-09-11 17:44:16 +0800242static struct shrinker *huge_zero_page_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800243
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800244#ifdef CONFIG_SYSFS
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800245static ssize_t enabled_show(struct kobject *kobj,
246 struct kobj_attribute *attr, char *buf)
247{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800248 const char *output;
249
Mel Gorman444eb2a42016-03-17 14:19:23 -0700250 if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags))
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800251 output = "[always] madvise never";
252 else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
253 &transparent_hugepage_flags))
254 output = "always [madvise] never";
Mel Gorman444eb2a42016-03-17 14:19:23 -0700255 else
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800256 output = "always madvise [never]";
257
258 return sysfs_emit(buf, "%s\n", output);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800259}
Mel Gorman444eb2a42016-03-17 14:19:23 -0700260
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800261static ssize_t enabled_store(struct kobject *kobj,
262 struct kobj_attribute *attr,
263 const char *buf, size_t count)
264{
David Rientjes21440d72017-02-22 15:45:49 -0800265 ssize_t ret = count;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800266
David Rientjesf42f2552020-01-30 22:14:48 -0800267 if (sysfs_streq(buf, "always")) {
David Rientjes21440d72017-02-22 15:45:49 -0800268 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
269 set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800270 } else if (sysfs_streq(buf, "madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800271 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
272 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800273 } else if (sysfs_streq(buf, "never")) {
David Rientjes21440d72017-02-22 15:45:49 -0800274 clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags);
275 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags);
276 } else
277 ret = -EINVAL;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800278
279 if (ret > 0) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700280 int err = start_stop_khugepaged();
Andrea Arcangeliba761492011-01-13 15:46:58 -0800281 if (err)
282 ret = err;
283 }
Andrea Arcangeliba761492011-01-13 15:46:58 -0800284 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800285}
Miaohe Lin37139bb2022-07-04 21:21:53 +0800286
287static struct kobj_attribute enabled_attr = __ATTR_RW(enabled);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800288
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700289ssize_t single_hugepage_flag_show(struct kobject *kobj,
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800290 struct kobj_attribute *attr, char *buf,
291 enum transparent_hugepage_flag flag)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800292{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800293 return sysfs_emit(buf, "%d\n",
294 !!test_bit(flag, &transparent_hugepage_flags));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800295}
Ben Hutchingse27e6152011-04-14 15:22:21 -0700296
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700297ssize_t single_hugepage_flag_store(struct kobject *kobj,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800298 struct kobj_attribute *attr,
299 const char *buf, size_t count,
300 enum transparent_hugepage_flag flag)
301{
Ben Hutchingse27e6152011-04-14 15:22:21 -0700302 unsigned long value;
303 int ret;
304
305 ret = kstrtoul(buf, 10, &value);
306 if (ret < 0)
307 return ret;
308 if (value > 1)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800309 return -EINVAL;
310
Ben Hutchingse27e6152011-04-14 15:22:21 -0700311 if (value)
312 set_bit(flag, &transparent_hugepage_flags);
313 else
314 clear_bit(flag, &transparent_hugepage_flags);
315
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800316 return count;
317}
318
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800319static ssize_t defrag_show(struct kobject *kobj,
320 struct kobj_attribute *attr, char *buf)
321{
Joe Perchesbfb0ffe2020-12-14 19:14:46 -0800322 const char *output;
323
324 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG,
325 &transparent_hugepage_flags))
326 output = "[always] defer defer+madvise madvise never";
327 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG,
328 &transparent_hugepage_flags))
329 output = "always [defer] defer+madvise madvise never";
330 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG,
331 &transparent_hugepage_flags))
332 output = "always defer [defer+madvise] madvise never";
333 else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG,
334 &transparent_hugepage_flags))
335 output = "always defer defer+madvise [madvise] never";
336 else
337 output = "always defer defer+madvise madvise [never]";
338
339 return sysfs_emit(buf, "%s\n", output);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800340}
David Rientjes21440d72017-02-22 15:45:49 -0800341
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800342static ssize_t defrag_store(struct kobject *kobj,
343 struct kobj_attribute *attr,
344 const char *buf, size_t count)
345{
David Rientjesf42f2552020-01-30 22:14:48 -0800346 if (sysfs_streq(buf, "always")) {
David Rientjes21440d72017-02-22 15:45:49 -0800347 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
348 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
349 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
350 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800351 } else if (sysfs_streq(buf, "defer+madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800352 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
353 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
354 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
355 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800356 } else if (sysfs_streq(buf, "defer")) {
David Rientjes4fad7fb2017-04-07 16:04:54 -0700357 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
358 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
359 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
360 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800361 } else if (sysfs_streq(buf, "madvise")) {
David Rientjes21440d72017-02-22 15:45:49 -0800362 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
363 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
364 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
365 set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
David Rientjesf42f2552020-01-30 22:14:48 -0800366 } else if (sysfs_streq(buf, "never")) {
David Rientjes21440d72017-02-22 15:45:49 -0800367 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags);
368 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags);
369 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags);
370 clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags);
371 } else
372 return -EINVAL;
373
374 return count;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800375}
Miaohe Lin37139bb2022-07-04 21:21:53 +0800376static struct kobj_attribute defrag_attr = __ATTR_RW(defrag);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800377
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800378static ssize_t use_zero_page_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -0800379 struct kobj_attribute *attr, char *buf)
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800380{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700381 return single_hugepage_flag_show(kobj, attr, buf,
Joe Perchesae7a9272020-12-14 19:14:42 -0800382 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800383}
384static ssize_t use_zero_page_store(struct kobject *kobj,
385 struct kobj_attribute *attr, const char *buf, size_t count)
386{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700387 return single_hugepage_flag_store(kobj, attr, buf, count,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800388 TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG);
389}
Miaohe Lin37139bb2022-07-04 21:21:53 +0800390static struct kobj_attribute use_zero_page_attr = __ATTR_RW(use_zero_page);
Hugh Dickins49920d22016-12-12 16:44:50 -0800391
392static ssize_t hpage_pmd_size_show(struct kobject *kobj,
Joe Perchesae7a9272020-12-14 19:14:42 -0800393 struct kobj_attribute *attr, char *buf)
Hugh Dickins49920d22016-12-12 16:44:50 -0800394{
Joe Perchesae7a9272020-12-14 19:14:42 -0800395 return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE);
Hugh Dickins49920d22016-12-12 16:44:50 -0800396}
397static struct kobj_attribute hpage_pmd_size_attr =
398 __ATTR_RO(hpage_pmd_size);
399
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800400static struct attribute *hugepage_attr[] = {
401 &enabled_attr.attr,
402 &defrag_attr.attr,
Kirill A. Shutemov79da5402012-12-12 13:51:12 -0800403 &use_zero_page_attr.attr,
Hugh Dickins49920d22016-12-12 16:44:50 -0800404 &hpage_pmd_size_attr.attr,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700405#ifdef CONFIG_SHMEM
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700406 &shmem_enabled_attr.attr,
407#endif
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800408 NULL,
409};
410
Arvind Yadav8aa95a22017-09-06 16:22:03 -0700411static const struct attribute_group hugepage_attr_group = {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800412 .attrs = hugepage_attr,
Andrea Arcangeliba761492011-01-13 15:46:58 -0800413};
414
Shaohua Li569e5592012-01-12 17:19:11 -0800415static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj)
416{
417 int err;
418
419 *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj);
420 if (unlikely(!*hugepage_kobj)) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700421 pr_err("failed to create transparent hugepage kobject\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800422 return -ENOMEM;
423 }
424
425 err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group);
426 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700427 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800428 goto delete_obj;
429 }
430
431 err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group);
432 if (err) {
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700433 pr_err("failed to register transparent hugepage group\n");
Shaohua Li569e5592012-01-12 17:19:11 -0800434 goto remove_hp_group;
435 }
436
437 return 0;
438
439remove_hp_group:
440 sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group);
441delete_obj:
442 kobject_put(*hugepage_kobj);
443 return err;
444}
445
446static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj)
447{
448 sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group);
449 sysfs_remove_group(hugepage_kobj, &hugepage_attr_group);
450 kobject_put(hugepage_kobj);
451}
452#else
453static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj)
454{
455 return 0;
456}
457
458static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj)
459{
460}
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800461#endif /* CONFIG_SYSFS */
462
Qi Zheng54d91722023-09-11 17:44:16 +0800463static int __init thp_shrinker_init(void)
464{
465 huge_zero_page_shrinker = shrinker_alloc(0, "thp-zero");
466 if (!huge_zero_page_shrinker)
467 return -ENOMEM;
468
469 deferred_split_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE |
470 SHRINKER_MEMCG_AWARE |
471 SHRINKER_NONSLAB,
472 "thp-deferred_split");
473 if (!deferred_split_shrinker) {
474 shrinker_free(huge_zero_page_shrinker);
475 return -ENOMEM;
476 }
477
478 huge_zero_page_shrinker->count_objects = shrink_huge_zero_page_count;
479 huge_zero_page_shrinker->scan_objects = shrink_huge_zero_page_scan;
480 shrinker_register(huge_zero_page_shrinker);
481
482 deferred_split_shrinker->count_objects = deferred_split_count;
483 deferred_split_shrinker->scan_objects = deferred_split_scan;
484 shrinker_register(deferred_split_shrinker);
485
486 return 0;
487}
488
489static void __init thp_shrinker_exit(void)
490{
491 shrinker_free(huge_zero_page_shrinker);
492 shrinker_free(deferred_split_shrinker);
493}
494
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800495static int __init hugepage_init(void)
496{
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800497 int err;
Shaohua Li569e5592012-01-12 17:19:11 -0800498 struct kobject *hugepage_kobj;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800499
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800500 if (!has_transparent_hugepage()) {
Peter Xu3c556d22023-03-15 13:16:42 -0400501 transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_UNSUPPORTED;
Shaohua Li569e5592012-01-12 17:19:11 -0800502 return -EINVAL;
Andrea Arcangeli4b7167b2011-01-13 15:47:09 -0800503 }
504
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530505 /*
506 * hugepages can't be allocated by the buddy allocator
507 */
Kirill A. Shutemov23baf832023-03-15 14:31:33 +0300508 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER > MAX_ORDER);
Kirill A. Shutemovff20c2e2016-03-01 09:45:14 +0530509 /*
510 * we use page->mapping and page->index in second tail page
511 * as list_head: assuming THP order >= 2
512 */
513 MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2);
514
Shaohua Li569e5592012-01-12 17:19:11 -0800515 err = hugepage_init_sysfs(&hugepage_kobj);
516 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700517 goto err_sysfs;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800518
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700519 err = khugepaged_init();
Andrea Arcangeliba761492011-01-13 15:46:58 -0800520 if (err)
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700521 goto err_slab;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800522
Qi Zheng54d91722023-09-11 17:44:16 +0800523 err = thp_shrinker_init();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700524 if (err)
Qi Zheng54d91722023-09-11 17:44:16 +0800525 goto err_shrinker;
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800526
Rik van Riel97562cd2011-01-13 15:47:12 -0800527 /*
528 * By default disable transparent hugepages on smaller systems,
529 * where the extra memory used could hurt more than TLB overhead
530 * is likely to save. The admin can still enable it through /sys.
531 */
Arun KSca79b0c2018-12-28 00:34:29 -0800532 if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) {
Rik van Riel97562cd2011-01-13 15:47:12 -0800533 transparent_hugepage_flags = 0;
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700534 return 0;
535 }
Rik van Riel97562cd2011-01-13 15:47:12 -0800536
Kirill A. Shutemov79553da2932015-04-15 16:14:56 -0700537 err = start_stop_khugepaged();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700538 if (err)
539 goto err_khugepaged;
Andrea Arcangeliba761492011-01-13 15:46:58 -0800540
Shaohua Li569e5592012-01-12 17:19:11 -0800541 return 0;
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700542err_khugepaged:
Qi Zheng54d91722023-09-11 17:44:16 +0800543 thp_shrinker_exit();
544err_shrinker:
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700545 khugepaged_destroy();
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700546err_slab:
Shaohua Li569e5592012-01-12 17:19:11 -0800547 hugepage_exit_sysfs(hugepage_kobj);
Kirill A. Shutemov65ebb642015-04-15 16:14:20 -0700548err_sysfs:
Andrea Arcangeliba761492011-01-13 15:46:58 -0800549 return err;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800550}
Paul Gortmakera64fb3c2014-01-23 15:53:30 -0800551subsys_initcall(hugepage_init);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800552
553static int __init setup_transparent_hugepage(char *str)
554{
555 int ret = 0;
556 if (!str)
557 goto out;
558 if (!strcmp(str, "always")) {
559 set_bit(TRANSPARENT_HUGEPAGE_FLAG,
560 &transparent_hugepage_flags);
561 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
562 &transparent_hugepage_flags);
563 ret = 1;
564 } else if (!strcmp(str, "madvise")) {
565 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
566 &transparent_hugepage_flags);
567 set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
568 &transparent_hugepage_flags);
569 ret = 1;
570 } else if (!strcmp(str, "never")) {
571 clear_bit(TRANSPARENT_HUGEPAGE_FLAG,
572 &transparent_hugepage_flags);
573 clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG,
574 &transparent_hugepage_flags);
575 ret = 1;
576 }
577out:
578 if (!ret)
Andrew Mortonae3a8c12014-06-04 16:06:58 -0700579 pr_warn("transparent_hugepage= cannot parse, ignored\n");
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800580 return ret;
581}
582__setup("transparent_hugepage=", setup_transparent_hugepage);
583
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800584pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800585{
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800586 if (likely(vma->vm_flags & VM_WRITE))
Rick Edgecombe161e3932023-06-12 17:10:29 -0700587 pmd = pmd_mkwrite(pmd, vma);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800588 return pmd;
589}
590
Yang Shi87eaceb2019-09-23 15:38:15 -0700591#ifdef CONFIG_MEMCG
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +0000592static inline
593struct deferred_split *get_deferred_split_queue(struct folio *folio)
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800594{
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +0000595 struct mem_cgroup *memcg = folio_memcg(folio);
596 struct pglist_data *pgdat = NODE_DATA(folio_nid(folio));
Yang Shi87eaceb2019-09-23 15:38:15 -0700597
598 if (memcg)
599 return &memcg->deferred_split_queue;
600 else
601 return &pgdat->deferred_split_queue;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800602}
Yang Shi87eaceb2019-09-23 15:38:15 -0700603#else
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +0000604static inline
605struct deferred_split *get_deferred_split_queue(struct folio *folio)
Yang Shi87eaceb2019-09-23 15:38:15 -0700606{
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +0000607 struct pglist_data *pgdat = NODE_DATA(folio_nid(folio));
Yang Shi87eaceb2019-09-23 15:38:15 -0700608
609 return &pgdat->deferred_split_queue;
610}
611#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800612
Matthew Wilcox (Oracle)da6e7bf2023-08-16 16:11:53 +0100613void folio_prep_large_rmappable(struct folio *folio)
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800614{
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +0000615 VM_BUG_ON_FOLIO(folio_order(folio) < 2, folio);
616 INIT_LIST_HEAD(&folio->_deferred_list);
Matthew Wilcox (Oracle)de53c052023-08-16 16:11:56 +0100617 folio_set_large_rmappable(folio);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -0800618}
619
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +0100620static inline bool is_transparent_hugepage(struct folio *folio)
Sean Christopherson005ba372020-01-08 12:24:36 -0800621{
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +0100622 if (!folio_test_large(folio))
Zou Weifa1f68c2020-06-04 16:49:46 -0700623 return false;
Sean Christopherson005ba372020-01-08 12:24:36 -0800624
Matthew Wilcox (Oracle)f04029f2023-01-11 14:29:05 +0000625 return is_huge_zero_page(&folio->page) ||
Matthew Wilcox (Oracle)de53c052023-08-16 16:11:56 +0100626 folio_test_large_rmappable(folio);
Sean Christopherson005ba372020-01-08 12:24:36 -0800627}
Sean Christopherson005ba372020-01-08 12:24:36 -0800628
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800629static unsigned long __thp_get_unmapped_area(struct file *filp,
630 unsigned long addr, unsigned long len,
Toshi Kani74d2fad2016-10-07 16:59:56 -0700631 loff_t off, unsigned long flags, unsigned long size)
632{
Toshi Kani74d2fad2016-10-07 16:59:56 -0700633 loff_t off_end = off + len;
634 loff_t off_align = round_up(off, size);
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800635 unsigned long len_pad, ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700636
637 if (off_end <= off_align || (off_end - off_align) < size)
638 return 0;
639
640 len_pad = len + size;
641 if (len_pad < len || (off + len_pad) < off)
642 return 0;
643
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800644 ret = current->mm->get_unmapped_area(filp, addr, len_pad,
Toshi Kani74d2fad2016-10-07 16:59:56 -0700645 off >> PAGE_SHIFT, flags);
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800646
647 /*
648 * The failure might be due to length padding. The caller will retry
649 * without the padding.
650 */
651 if (IS_ERR_VALUE(ret))
Toshi Kani74d2fad2016-10-07 16:59:56 -0700652 return 0;
653
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800654 /*
655 * Do not try to align to THP boundary if allocation at the address
656 * hint succeeds.
657 */
658 if (ret == addr)
659 return addr;
660
661 ret += (off - ret) & (size - 1);
662 return ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700663}
664
665unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr,
666 unsigned long len, unsigned long pgoff, unsigned long flags)
667{
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800668 unsigned long ret;
Toshi Kani74d2fad2016-10-07 16:59:56 -0700669 loff_t off = (loff_t)pgoff << PAGE_SHIFT;
670
Kirill A. Shutemov97d3d0f2020-01-13 16:29:10 -0800671 ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE);
672 if (ret)
673 return ret;
William Kucharski1854bc62019-09-22 08:43:15 -0400674
Toshi Kani74d2fad2016-10-07 16:59:56 -0700675 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
676}
677EXPORT_SYMBOL_GPL(thp_get_unmapped_area);
678
Souptick Joarder2b740302018-08-23 17:01:36 -0700679static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf,
680 struct page *page, gfp_t gfp)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800681{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800682 struct vm_area_struct *vma = vmf->vma;
Kefeng Wangcfe32362023-03-02 19:58:29 +0800683 struct folio *folio = page_folio(page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800684 pgtable_t pgtable;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800685 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Souptick Joarder2b740302018-08-23 17:01:36 -0700686 vm_fault_t ret = 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800687
Kefeng Wangcfe32362023-03-02 19:58:29 +0800688 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
Johannes Weiner00501b52014-08-08 14:19:20 -0700689
Kefeng Wangcfe32362023-03-02 19:58:29 +0800690 if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) {
691 folio_put(folio);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700692 count_vm_event(THP_FAULT_FALLBACK);
David Rientjes85b9f462020-04-06 20:04:28 -0700693 count_vm_event(THP_FAULT_FALLBACK_CHARGE);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700694 return VM_FAULT_FALLBACK;
695 }
Kefeng Wangcfe32362023-03-02 19:58:29 +0800696 folio_throttle_swaprate(folio, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800697
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800698 pgtable = pte_alloc_one(vma->vm_mm);
Johannes Weiner00501b52014-08-08 14:19:20 -0700699 if (unlikely(!pgtable)) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700700 ret = VM_FAULT_OOM;
701 goto release;
Johannes Weiner00501b52014-08-08 14:19:20 -0700702 }
703
Huang Yingc79b57e2017-09-06 16:25:04 -0700704 clear_huge_page(page, vmf->address, HPAGE_PMD_NR);
Minchan Kim52f37622013-04-29 15:08:15 -0700705 /*
Kefeng Wangcfe32362023-03-02 19:58:29 +0800706 * The memory barrier inside __folio_mark_uptodate makes sure that
Minchan Kim52f37622013-04-29 15:08:15 -0700707 * clear_huge_page writes become visible before the set_pmd_at()
708 * write.
709 */
Kefeng Wangcfe32362023-03-02 19:58:29 +0800710 __folio_mark_uptodate(folio);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800711
Jan Kara82b0f8c2016-12-14 15:06:58 -0800712 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
713 if (unlikely(!pmd_none(*vmf->pmd))) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700714 goto unlock_release;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800715 } else {
716 pmd_t entry;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700717
Michal Hocko6b31d592017-08-18 15:16:15 -0700718 ret = check_stable_address_space(vma->vm_mm);
719 if (ret)
720 goto unlock_release;
721
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700722 /* Deliver the page fault to userland */
723 if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800724 spin_unlock(vmf->ptl);
Kefeng Wangcfe32362023-03-02 19:58:29 +0800725 folio_put(folio);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700726 pte_free(vma->vm_mm, pgtable);
Miaohe Lin8fd5eda2021-05-04 18:33:49 -0700727 ret = handle_userfault(vmf, VM_UFFD_MISSING);
728 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
729 return ret;
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700730 }
731
Kirill A. Shutemov31223592013-09-12 15:14:01 -0700732 entry = mk_huge_pmd(page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800733 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kefeng Wangcfe32362023-03-02 19:58:29 +0800734 folio_add_new_anon_rmap(folio, vma, haddr);
735 folio_add_lru_vma(folio, vma);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800736 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
737 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry);
Bibo Maofca40572021-02-24 12:06:42 -0800738 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700739 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800740 mm_inc_nr_ptes(vma->vm_mm);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800741 spin_unlock(vmf->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700742 count_vm_event(THP_FAULT_ALLOC);
Johannes Weiner9d82c692020-06-03 16:02:04 -0700743 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800744 }
745
David Rientjesaa2e8782012-05-29 15:06:17 -0700746 return 0;
Michal Hocko6b31d592017-08-18 15:16:15 -0700747unlock_release:
748 spin_unlock(vmf->ptl);
749release:
750 if (pgtable)
751 pte_free(vma->vm_mm, pgtable);
Kefeng Wangcfe32362023-03-02 19:58:29 +0800752 folio_put(folio);
Michal Hocko6b31d592017-08-18 15:16:15 -0700753 return ret;
754
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800755}
756
Mel Gorman444eb2a42016-03-17 14:19:23 -0700757/*
David Rientjes21440d72017-02-22 15:45:49 -0800758 * always: directly stall for all thp allocations
759 * defer: wake kswapd and fail if not immediately available
760 * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise
761 * fail if not immediately available
762 * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately
763 * available
764 * never: never stall for any thp allocation
Mel Gorman444eb2a42016-03-17 14:19:23 -0700765 */
Rik van Riel164cc4f2021-02-25 17:16:18 -0800766gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma)
Andrea Arcangeli0bbbc0b2011-01-13 15:47:05 -0800767{
Rik van Riel164cc4f2021-02-25 17:16:18 -0800768 const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE);
Michal Hocko89c83fb2018-11-02 15:48:31 -0700769
David Rientjesac79f782019-09-04 12:54:18 -0700770 /* Always do synchronous compaction */
David Rientjes21440d72017-02-22 15:45:49 -0800771 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags))
Andrea Arcangelia8282602019-08-13 15:37:53 -0700772 return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY);
David Rientjesac79f782019-09-04 12:54:18 -0700773
774 /* Kick kcompactd and fail quickly */
David Rientjes21440d72017-02-22 15:45:49 -0800775 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700776 return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM;
David Rientjesac79f782019-09-04 12:54:18 -0700777
778 /* Synchronous compaction if madvised, otherwise kick kcompactd */
David Rientjes21440d72017-02-22 15:45:49 -0800779 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700780 return GFP_TRANSHUGE_LIGHT |
781 (vma_madvised ? __GFP_DIRECT_RECLAIM :
782 __GFP_KSWAPD_RECLAIM);
David Rientjesac79f782019-09-04 12:54:18 -0700783
784 /* Only do synchronous compaction if madvised */
David Rientjes21440d72017-02-22 15:45:49 -0800785 if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags))
David Rientjes19deb762019-09-04 12:54:20 -0700786 return GFP_TRANSHUGE_LIGHT |
787 (vma_madvised ? __GFP_DIRECT_RECLAIM : 0);
David Rientjesac79f782019-09-04 12:54:18 -0700788
David Rientjes19deb762019-09-04 12:54:20 -0700789 return GFP_TRANSHUGE_LIGHT;
Mel Gorman444eb2a42016-03-17 14:19:23 -0700790}
791
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -0800792/* Caller must hold page table lock. */
Miaohe Lin2efeb8d2021-02-24 12:07:29 -0800793static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm,
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -0800794 struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd,
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700795 struct page *zero_page)
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800796{
797 pmd_t entry;
Andrew Morton7c414162015-09-08 14:58:43 -0700798 if (!pmd_none(*pmd))
Miaohe Lin2efeb8d2021-02-24 12:07:29 -0800799 return;
Kirill A. Shutemov5918d102013-04-29 15:08:44 -0700800 entry = mk_pmd(zero_page, vma->vm_page_prot);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800801 entry = pmd_mkhuge(entry);
Qi Zhengc8bb4162022-08-18 16:27:48 +0800802 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800803 set_pmd_at(mm, haddr, pmd, entry);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800804 mm_inc_nr_ptes(mm);
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -0800805}
806
Souptick Joarder2b740302018-08-23 17:01:36 -0700807vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800808{
Jan Kara82b0f8c2016-12-14 15:06:58 -0800809 struct vm_area_struct *vma = vmf->vma;
Aneesh Kumar K.V077fcf12015-02-11 15:27:12 -0800810 gfp_t gfp;
Matthew Wilcox (Oracle)cb196ee2022-05-12 20:23:01 -0700811 struct folio *folio;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800812 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800813
Yang Shi43675e62019-07-18 15:57:24 -0700814 if (!transhuge_vma_suitable(vma, haddr))
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700815 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700816 if (unlikely(anon_vma_prepare(vma)))
817 return VM_FAULT_OOM;
Yang Shi4fa68932022-06-16 10:48:35 -0700818 khugepaged_enter_vma(vma, vma->vm_flags);
Yang Shid2081b22022-05-19 14:08:49 -0700819
Jan Kara82b0f8c2016-12-14 15:06:58 -0800820 if (!(vmf->flags & FAULT_FLAG_WRITE) &&
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700821 !mm_forbids_zeropage(vma->vm_mm) &&
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700822 transparent_hugepage_use_zero_page()) {
823 pgtable_t pgtable;
824 struct page *zero_page;
Souptick Joarder2b740302018-08-23 17:01:36 -0700825 vm_fault_t ret;
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800826 pgtable = pte_alloc_one(vma->vm_mm);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700827 if (unlikely(!pgtable))
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800828 return VM_FAULT_OOM;
Aaron Lu6fcb52a2016-10-07 17:00:08 -0700829 zero_page = mm_get_huge_zero_page(vma->vm_mm);
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700830 if (unlikely(!zero_page)) {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700831 pte_free(vma->vm_mm, pgtable);
Andi Kleen81ab4202011-04-14 15:22:06 -0700832 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700833 return VM_FAULT_FALLBACK;
Andi Kleen81ab4202011-04-14 15:22:06 -0700834 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800835 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700836 ret = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800837 if (pmd_none(*vmf->pmd)) {
Michal Hocko6b31d592017-08-18 15:16:15 -0700838 ret = check_stable_address_space(vma->vm_mm);
839 if (ret) {
840 spin_unlock(vmf->ptl);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800841 pte_free(vma->vm_mm, pgtable);
Michal Hocko6b31d592017-08-18 15:16:15 -0700842 } else if (userfaultfd_missing(vma)) {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800843 spin_unlock(vmf->ptl);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800844 pte_free(vma->vm_mm, pgtable);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800845 ret = handle_userfault(vmf, VM_UFFD_MISSING);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700846 VM_BUG_ON(ret & VM_FAULT_FALLBACK);
847 } else {
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700848 set_huge_zero_page(pgtable, vma->vm_mm, vma,
Jan Kara82b0f8c2016-12-14 15:06:58 -0800849 haddr, vmf->pmd, zero_page);
Bibo Maofca40572021-02-24 12:06:42 -0800850 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800851 spin_unlock(vmf->ptl);
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700852 }
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800853 } else {
Jan Kara82b0f8c2016-12-14 15:06:58 -0800854 spin_unlock(vmf->ptl);
Kirill A. Shutemovbae473a2016-07-26 15:25:20 -0700855 pte_free(vma->vm_mm, pgtable);
Gerald Schaeferbfe8cc12020-11-21 22:17:15 -0800856 }
Andrea Arcangeli6b251fc2015-09-04 15:46:20 -0700857 return ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800858 }
Rik van Riel164cc4f2021-02-25 17:16:18 -0800859 gfp = vma_thp_gfp_mask(vma);
Matthew Wilcox (Oracle)cb196ee2022-05-12 20:23:01 -0700860 folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, vma, haddr, true);
861 if (unlikely(!folio)) {
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700862 count_vm_event(THP_FAULT_FALLBACK);
Kirill A. Shutemovc0292552013-09-12 15:14:05 -0700863 return VM_FAULT_FALLBACK;
Kirill A. Shutemov128ec032013-09-12 15:14:03 -0700864 }
Matthew Wilcox (Oracle)cb196ee2022-05-12 20:23:01 -0700865 return __do_huge_pmd_anonymous_page(vmf, &folio->page, gfp);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -0800866}
867
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700868static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr,
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700869 pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write,
870 pgtable_t pgtable)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700871{
872 struct mm_struct *mm = vma->vm_mm;
873 pmd_t entry;
874 spinlock_t *ptl;
875
876 ptl = pmd_lock(mm, pmd);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700877 if (!pmd_none(*pmd)) {
878 if (write) {
879 if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) {
880 WARN_ON_ONCE(!is_huge_zero_pmd(*pmd));
881 goto out_unlock;
882 }
883 entry = pmd_mkyoung(*pmd);
884 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
885 if (pmdp_set_access_flags(vma, addr, pmd, entry, 1))
886 update_mmu_cache_pmd(vma, addr, pmd);
887 }
888
889 goto out_unlock;
890 }
891
Dan Williamsf25748e32016-01-15 16:56:43 -0800892 entry = pmd_mkhuge(pfn_t_pmd(pfn, prot));
893 if (pfn_t_devmap(pfn))
894 entry = pmd_mkdevmap(entry);
Ross Zwisler01871e52016-01-15 16:56:02 -0800895 if (write) {
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800896 entry = pmd_mkyoung(pmd_mkdirty(entry));
897 entry = maybe_pmd_mkwrite(entry, vma);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700898 }
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700899
900 if (pgtable) {
901 pgtable_trans_huge_deposit(mm, pmd, pgtable);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -0800902 mm_inc_nr_ptes(mm);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700903 pgtable = NULL;
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700904 }
905
Ross Zwisler01871e52016-01-15 16:56:02 -0800906 set_pmd_at(mm, addr, pmd, entry);
907 update_mmu_cache_pmd(vma, addr, pmd);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700908
909out_unlock:
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700910 spin_unlock(ptl);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700911 if (pgtable)
912 pte_free(mm, pgtable);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700913}
914
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100915/**
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000916 * vmf_insert_pfn_pmd - insert a pmd size pfn
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100917 * @vmf: Structure describing the fault
918 * @pfn: pfn to insert
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100919 * @write: whether it's a write fault
920 *
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000921 * Insert a pmd size pfn. See vmf_insert_pfn() for additional info.
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +0100922 *
923 * Return: vm_fault_t value.
924 */
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000925vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, pfn_t pfn, bool write)
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700926{
Dan Williamsfce86ff2019-05-13 17:15:33 -0700927 unsigned long addr = vmf->address & PMD_MASK;
928 struct vm_area_struct *vma = vmf->vma;
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000929 pgprot_t pgprot = vma->vm_page_prot;
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700930 pgtable_t pgtable = NULL;
Dan Williamsfce86ff2019-05-13 17:15:33 -0700931
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700932 /*
933 * If we had pmd_special, we could avoid all these restrictions,
934 * but we need to be consistent with PTEs and architectures that
935 * can't support a 'special' bit.
936 */
Dave Jiange1fb4a02018-08-17 15:43:40 -0700937 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
938 !pfn_t_devmap(pfn));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700939 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
940 (VM_PFNMAP|VM_MIXEDMAP));
941 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700942
943 if (addr < vma->vm_start || addr >= vma->vm_end)
944 return VM_FAULT_SIGBUS;
Borislav Petkov308a0472016-10-26 19:43:43 +0200945
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700946 if (arch_needs_pgtable_deposit()) {
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -0800947 pgtable = pte_alloc_one(vma->vm_mm);
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -0700948 if (!pgtable)
949 return VM_FAULT_OOM;
950 }
951
Borislav Petkov308a0472016-10-26 19:43:43 +0200952 track_pfn_insert(vma, &pgprot, pfn);
953
Dan Williamsfce86ff2019-05-13 17:15:33 -0700954 insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable);
Matthew Wilcoxae18d6d2015-09-08 14:59:14 -0700955 return VM_FAULT_NOPAGE;
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700956}
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000957EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd);
Matthew Wilcox5cad4652015-09-08 14:58:54 -0700958
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800959#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800960static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800961{
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800962 if (likely(vma->vm_flags & VM_WRITE))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800963 pud = pud_mkwrite(pud);
964 return pud;
965}
966
967static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr,
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000968 pud_t *pud, pfn_t pfn, bool write)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800969{
970 struct mm_struct *mm = vma->vm_mm;
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +0000971 pgprot_t prot = vma->vm_page_prot;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800972 pud_t entry;
973 spinlock_t *ptl;
974
975 ptl = pud_lock(mm, pud);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700976 if (!pud_none(*pud)) {
977 if (write) {
978 if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) {
979 WARN_ON_ONCE(!is_huge_zero_pud(*pud));
980 goto out_unlock;
981 }
982 entry = pud_mkyoung(*pud);
983 entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma);
984 if (pudp_set_access_flags(vma, addr, pud, entry, 1))
985 update_mmu_cache_pud(vma, addr, pud);
986 }
987 goto out_unlock;
988 }
989
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800990 entry = pud_mkhuge(pfn_t_pud(pfn, prot));
991 if (pfn_t_devmap(pfn))
992 entry = pud_mkdevmap(entry);
993 if (write) {
Linus Torvaldsf55e1012017-11-29 09:01:01 -0800994 entry = pud_mkyoung(pud_mkdirty(entry));
995 entry = maybe_pud_mkwrite(entry, vma);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -0800996 }
997 set_pud_at(mm, addr, pud, entry);
998 update_mmu_cache_pud(vma, addr, pud);
Aneesh Kumar K.Vc6f3c5e2019-04-05 18:39:10 -0700999
1000out_unlock:
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001001 spin_unlock(ptl);
1002}
1003
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +01001004/**
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001005 * vmf_insert_pfn_pud - insert a pud size pfn
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +01001006 * @vmf: Structure describing the fault
1007 * @pfn: pfn to insert
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +01001008 * @write: whether it's a write fault
1009 *
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001010 * Insert a pud size pfn. See vmf_insert_pfn() for additional info.
Thomas Hellstrom (VMware)9a9731b2020-03-24 18:48:09 +01001011 *
1012 * Return: vm_fault_t value.
1013 */
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001014vm_fault_t vmf_insert_pfn_pud(struct vm_fault *vmf, pfn_t pfn, bool write)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001015{
Dan Williamsfce86ff2019-05-13 17:15:33 -07001016 unsigned long addr = vmf->address & PUD_MASK;
1017 struct vm_area_struct *vma = vmf->vma;
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001018 pgprot_t pgprot = vma->vm_page_prot;
Dan Williamsfce86ff2019-05-13 17:15:33 -07001019
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001020 /*
1021 * If we had pud_special, we could avoid all these restrictions,
1022 * but we need to be consistent with PTEs and architectures that
1023 * can't support a 'special' bit.
1024 */
Dave Jiang62ec0d82018-09-04 15:46:16 -07001025 BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) &&
1026 !pfn_t_devmap(pfn));
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001027 BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
1028 (VM_PFNMAP|VM_MIXEDMAP));
1029 BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001030
1031 if (addr < vma->vm_start || addr >= vma->vm_end)
1032 return VM_FAULT_SIGBUS;
1033
1034 track_pfn_insert(vma, &pgprot, pfn);
1035
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001036 insert_pfn_pud(vma, addr, vmf->pud, pfn, write);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001037 return VM_FAULT_NOPAGE;
1038}
Lorenzo Stoakes7b806d22023-03-12 23:40:14 +00001039EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001040#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1041
Dan Williams3565fce2016-01-15 16:56:55 -08001042static void touch_pmd(struct vm_area_struct *vma, unsigned long addr,
Miaohe Lina69e4712022-07-04 21:21:50 +08001043 pmd_t *pmd, bool write)
Dan Williams3565fce2016-01-15 16:56:55 -08001044{
1045 pmd_t _pmd;
1046
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001047 _pmd = pmd_mkyoung(*pmd);
Miaohe Lina69e4712022-07-04 21:21:50 +08001048 if (write)
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001049 _pmd = pmd_mkdirty(_pmd);
Dan Williams3565fce2016-01-15 16:56:55 -08001050 if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK,
Miaohe Lina69e4712022-07-04 21:21:50 +08001051 pmd, _pmd, write))
Dan Williams3565fce2016-01-15 16:56:55 -08001052 update_mmu_cache_pmd(vma, addr, pmd);
1053}
1054
1055struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr,
Keith Buschdf06b372018-10-26 15:10:28 -07001056 pmd_t *pmd, int flags, struct dev_pagemap **pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -08001057{
1058 unsigned long pfn = pmd_pfn(*pmd);
1059 struct mm_struct *mm = vma->vm_mm;
Dan Williams3565fce2016-01-15 16:56:55 -08001060 struct page *page;
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001061 int ret;
Dan Williams3565fce2016-01-15 16:56:55 -08001062
1063 assert_spin_locked(pmd_lockptr(mm, pmd));
1064
Linus Torvaldsf6f37322017-12-15 18:53:22 -08001065 if (flags & FOLL_WRITE && !pmd_write(*pmd))
Dan Williams3565fce2016-01-15 16:56:55 -08001066 return NULL;
1067
1068 if (pmd_present(*pmd) && pmd_devmap(*pmd))
1069 /* pass */;
1070 else
1071 return NULL;
1072
1073 if (flags & FOLL_TOUCH)
Miaohe Lina69e4712022-07-04 21:21:50 +08001074 touch_pmd(vma, addr, pmd, flags & FOLL_WRITE);
Dan Williams3565fce2016-01-15 16:56:55 -08001075
1076 /*
1077 * device mapped pages can only be returned if the
1078 * caller will manage the page reference count.
1079 */
John Hubbard3faa52c2020-04-01 21:05:29 -07001080 if (!(flags & (FOLL_GET | FOLL_PIN)))
Dan Williams3565fce2016-01-15 16:56:55 -08001081 return ERR_PTR(-EEXIST);
1082
1083 pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT;
Keith Buschdf06b372018-10-26 15:10:28 -07001084 *pgmap = get_dev_pagemap(pfn, *pgmap);
1085 if (!*pgmap)
Dan Williams3565fce2016-01-15 16:56:55 -08001086 return ERR_PTR(-EFAULT);
1087 page = pfn_to_page(pfn);
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001088 ret = try_grab_page(page, flags);
1089 if (ret)
1090 page = ERR_PTR(ret);
Dan Williams3565fce2016-01-15 16:56:55 -08001091
1092 return page;
1093}
1094
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001095int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1096 pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr,
Peter Xu8f34f1e2021-06-30 18:49:02 -07001097 struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001098{
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001099 spinlock_t *dst_ptl, *src_ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001100 struct page *src_page;
1101 pmd_t pmd;
Matthew Wilcox12c9d702016-02-02 16:57:57 -08001102 pgtable_t pgtable = NULL;
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001103 int ret = -ENOMEM;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001104
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001105 /* Skip if can be re-fill on fault */
Peter Xu8f34f1e2021-06-30 18:49:02 -07001106 if (!vma_is_anonymous(dst_vma))
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001107 return 0;
1108
Joel Fernandes (Google)4cf58922019-01-03 15:28:34 -08001109 pgtable = pte_alloc_one(dst_mm);
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001110 if (unlikely(!pgtable))
1111 goto out;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001112
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001113 dst_ptl = pmd_lock(dst_mm, dst_pmd);
1114 src_ptl = pmd_lockptr(src_mm, src_pmd);
1115 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001116
1117 ret = -EAGAIN;
1118 pmd = *src_pmd;
Zi Yan84c3fc42017-09-08 16:11:01 -07001119
1120#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1121 if (unlikely(is_swap_pmd(pmd))) {
1122 swp_entry_t entry = pmd_to_swp_entry(pmd);
1123
1124 VM_BUG_ON(!is_pmd_migration_entry(pmd));
David Hildenbrand6c287602022-05-09 18:20:44 -07001125 if (!is_readable_migration_entry(entry)) {
Alistair Popple4dd845b2021-06-30 18:54:09 -07001126 entry = make_readable_migration_entry(
1127 swp_offset(entry));
Zi Yan84c3fc42017-09-08 16:11:01 -07001128 pmd = swp_entry_to_pmd(entry);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001129 if (pmd_swp_soft_dirty(*src_pmd))
1130 pmd = pmd_swp_mksoft_dirty(pmd);
Peter Xu8f34f1e2021-06-30 18:49:02 -07001131 if (pmd_swp_uffd_wp(*src_pmd))
1132 pmd = pmd_swp_mkuffd_wp(pmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001133 set_pmd_at(src_mm, addr, src_pmd, pmd);
1134 }
Zi Yandd8a67f2017-11-02 15:59:47 -07001135 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Kirill A. Shutemovaf5b0f62017-11-15 17:35:40 -08001136 mm_inc_nr_ptes(dst_mm);
Zi Yandd8a67f2017-11-02 15:59:47 -07001137 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Peter Xu8f34f1e2021-06-30 18:49:02 -07001138 if (!userfaultfd_wp(dst_vma))
1139 pmd = pmd_swp_clear_uffd_wp(pmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001140 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
1141 ret = 0;
1142 goto out_unlock;
1143 }
1144#endif
1145
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001146 if (unlikely(!pmd_trans_huge(pmd))) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001147 pte_free(dst_mm, pgtable);
1148 goto out_unlock;
1149 }
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001150 /*
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001151 * When page table lock is held, the huge zero pmd should not be
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001152 * under splitting since we don't split the page itself, only pmd to
1153 * a page table.
1154 */
1155 if (is_huge_zero_pmd(pmd)) {
Kirill A. Shutemov97ae1742012-12-12 13:51:06 -08001156 /*
1157 * get_huge_zero_page() will never allocate a new page here,
1158 * since we already have a zero page to copy. It just takes a
1159 * reference.
1160 */
Peter Xu5fc7a5f2021-06-30 18:48:59 -07001161 mm_get_huge_zero_page(dst_mm);
1162 goto out_zero_page;
Kirill A. Shutemovfc9fe822012-12-12 13:50:51 -08001163 }
Mel Gormande466bd2013-12-18 17:08:42 -08001164
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001165 src_page = pmd_page(pmd);
1166 VM_BUG_ON_PAGE(!PageHead(src_page), src_page);
Peter Xud0420352020-09-25 18:26:00 -04001167
David Hildenbrandfb3d8242022-05-09 18:20:43 -07001168 get_page(src_page);
1169 if (unlikely(page_try_dup_anon_rmap(src_page, true, src_vma))) {
1170 /* Page maybe pinned: split and retry the fault on PTEs. */
1171 put_page(src_page);
Peter Xud0420352020-09-25 18:26:00 -04001172 pte_free(dst_mm, pgtable);
1173 spin_unlock(src_ptl);
1174 spin_unlock(dst_ptl);
Peter Xu8f34f1e2021-06-30 18:49:02 -07001175 __split_huge_pmd(src_vma, src_pmd, addr, false, NULL);
Peter Xud0420352020-09-25 18:26:00 -04001176 return -EAGAIN;
1177 }
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001178 add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR);
Peter Xu5fc7a5f2021-06-30 18:48:59 -07001179out_zero_page:
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08001180 mm_inc_nr_ptes(dst_mm);
Kirill A. Shutemov628d47c2016-07-26 15:25:42 -07001181 pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001182 pmdp_set_wrprotect(src_mm, addr, src_pmd);
Peter Xu8f34f1e2021-06-30 18:49:02 -07001183 if (!userfaultfd_wp(dst_vma))
1184 pmd = pmd_clear_uffd_wp(pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001185 pmd = pmd_mkold(pmd_wrprotect(pmd));
1186 set_pmd_at(dst_mm, addr, dst_pmd, pmd);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001187
1188 ret = 0;
1189out_unlock:
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001190 spin_unlock(src_ptl);
1191 spin_unlock(dst_ptl);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001192out:
1193 return ret;
1194}
1195
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001196#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
1197static void touch_pud(struct vm_area_struct *vma, unsigned long addr,
Miaohe Lin5fe653e2022-07-04 21:21:49 +08001198 pud_t *pud, bool write)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001199{
1200 pud_t _pud;
1201
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001202 _pud = pud_mkyoung(*pud);
Miaohe Lin5fe653e2022-07-04 21:21:49 +08001203 if (write)
Kirill A. Shutemova8f97362017-11-27 06:21:25 +03001204 _pud = pud_mkdirty(_pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001205 if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK,
Miaohe Lin5fe653e2022-07-04 21:21:49 +08001206 pud, _pud, write))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001207 update_mmu_cache_pud(vma, addr, pud);
1208}
1209
1210struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr,
Keith Buschdf06b372018-10-26 15:10:28 -07001211 pud_t *pud, int flags, struct dev_pagemap **pgmap)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001212{
1213 unsigned long pfn = pud_pfn(*pud);
1214 struct mm_struct *mm = vma->vm_mm;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001215 struct page *page;
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001216 int ret;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001217
1218 assert_spin_locked(pud_lockptr(mm, pud));
1219
Linus Torvaldsf6f37322017-12-15 18:53:22 -08001220 if (flags & FOLL_WRITE && !pud_write(*pud))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001221 return NULL;
1222
1223 if (pud_present(*pud) && pud_devmap(*pud))
1224 /* pass */;
1225 else
1226 return NULL;
1227
1228 if (flags & FOLL_TOUCH)
Miaohe Lin5fe653e2022-07-04 21:21:49 +08001229 touch_pud(vma, addr, pud, flags & FOLL_WRITE);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001230
1231 /*
1232 * device mapped pages can only be returned if the
1233 * caller will manage the page reference count.
John Hubbard3faa52c2020-04-01 21:05:29 -07001234 *
1235 * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here:
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001236 */
John Hubbard3faa52c2020-04-01 21:05:29 -07001237 if (!(flags & (FOLL_GET | FOLL_PIN)))
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001238 return ERR_PTR(-EEXIST);
1239
1240 pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT;
Keith Buschdf06b372018-10-26 15:10:28 -07001241 *pgmap = get_dev_pagemap(pfn, *pgmap);
1242 if (!*pgmap)
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001243 return ERR_PTR(-EFAULT);
1244 page = pfn_to_page(pfn);
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001245
1246 ret = try_grab_page(page, flags);
1247 if (ret)
1248 page = ERR_PTR(ret);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001249
1250 return page;
1251}
1252
1253int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm,
1254 pud_t *dst_pud, pud_t *src_pud, unsigned long addr,
1255 struct vm_area_struct *vma)
1256{
1257 spinlock_t *dst_ptl, *src_ptl;
1258 pud_t pud;
1259 int ret;
1260
1261 dst_ptl = pud_lock(dst_mm, dst_pud);
1262 src_ptl = pud_lockptr(src_mm, src_pud);
1263 spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
1264
1265 ret = -EAGAIN;
1266 pud = *src_pud;
1267 if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud)))
1268 goto out_unlock;
1269
1270 /*
1271 * When page table lock is held, the huge zero pud should not be
1272 * under splitting since we don't split the page itself, only pud to
1273 * a page table.
1274 */
1275 if (is_huge_zero_pud(pud)) {
1276 /* No huge zero pud yet */
1277 }
1278
David Hildenbrandfb3d8242022-05-09 18:20:43 -07001279 /*
1280 * TODO: once we support anonymous pages, use page_try_dup_anon_rmap()
1281 * and split if duplicating fails.
1282 */
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001283 pudp_set_wrprotect(src_mm, addr, src_pud);
1284 pud = pud_mkold(pud_wrprotect(pud));
1285 set_pud_at(dst_mm, addr, dst_pud, pud);
1286
1287 ret = 0;
1288out_unlock:
1289 spin_unlock(src_ptl);
1290 spin_unlock(dst_ptl);
1291 return ret;
1292}
1293
1294void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud)
1295{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001296 bool write = vmf->flags & FAULT_FLAG_WRITE;
1297
1298 vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud);
1299 if (unlikely(!pud_same(*vmf->pud, orig_pud)))
1300 goto unlock;
1301
Miaohe Lin5fe653e2022-07-04 21:21:49 +08001302 touch_pud(vmf->vma, vmf->address, vmf->pud, write);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001303unlock:
1304 spin_unlock(vmf->ptl);
1305}
1306#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
1307
Yang Shi5db4f152021-06-30 18:51:35 -07001308void huge_pmd_set_accessed(struct vm_fault *vmf)
Will Deacona1dd4502012-12-11 16:01:27 -08001309{
Minchan Kim20f664a2017-01-10 16:57:51 -08001310 bool write = vmf->flags & FAULT_FLAG_WRITE;
Will Deacona1dd4502012-12-11 16:01:27 -08001311
Jan Kara82b0f8c2016-12-14 15:06:58 -08001312 vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
Miaohe Lina69e4712022-07-04 21:21:50 +08001313 if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd)))
Will Deacona1dd4502012-12-11 16:01:27 -08001314 goto unlock;
1315
Miaohe Lina69e4712022-07-04 21:21:50 +08001316 touch_pmd(vmf->vma, vmf->address, vmf->pmd, write);
Will Deacona1dd4502012-12-11 16:01:27 -08001317
1318unlock:
Jan Kara82b0f8c2016-12-14 15:06:58 -08001319 spin_unlock(vmf->ptl);
Will Deacona1dd4502012-12-11 16:01:27 -08001320}
1321
Yang Shi5db4f152021-06-30 18:51:35 -07001322vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001323{
David Hildenbrandc89357e2022-05-09 18:20:45 -07001324 const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001325 struct vm_area_struct *vma = vmf->vma;
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001326 struct folio *folio;
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001327 struct page *page;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001328 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Yang Shi5db4f152021-06-30 18:51:35 -07001329 pmd_t orig_pmd = vmf->orig_pmd;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001330
Jan Kara82b0f8c2016-12-14 15:06:58 -08001331 vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd);
Sasha Levin81d1b092014-10-09 15:28:10 -07001332 VM_BUG_ON_VMA(!vma->anon_vma, vma);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001333
Kirill A. Shutemov93b47962012-12-12 13:50:54 -08001334 if (is_huge_zero_pmd(orig_pmd))
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001335 goto fallback;
1336
Jan Kara82b0f8c2016-12-14 15:06:58 -08001337 spin_lock(vmf->ptl);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001338
1339 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
1340 spin_unlock(vmf->ptl);
1341 return 0;
1342 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001343
1344 page = pmd_page(orig_pmd);
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001345 folio = page_folio(page);
Miaohe Linf6004e72021-05-04 18:34:02 -07001346 VM_BUG_ON_PAGE(!PageHead(page), page);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001347
David Hildenbrand6c287602022-05-09 18:20:44 -07001348 /* Early check when only holding the PT lock. */
1349 if (PageAnonExclusive(page))
1350 goto reuse;
1351
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001352 if (!folio_trylock(folio)) {
1353 folio_get(folio);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001354 spin_unlock(vmf->ptl);
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001355 folio_lock(folio);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001356 spin_lock(vmf->ptl);
1357 if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) {
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001358 spin_unlock(vmf->ptl);
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001359 folio_unlock(folio);
1360 folio_put(folio);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001361 return 0;
Huang Yingba3c4ce2017-09-06 16:22:19 -07001362 }
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001363 folio_put(folio);
Huang Yingba3c4ce2017-09-06 16:22:19 -07001364 }
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001365
David Hildenbrand6c287602022-05-09 18:20:44 -07001366 /* Recheck after temporarily dropping the PT lock. */
1367 if (PageAnonExclusive(page)) {
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001368 folio_unlock(folio);
David Hildenbrand6c287602022-05-09 18:20:44 -07001369 goto reuse;
1370 }
1371
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001372 /*
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001373 * See do_wp_page(): we can only reuse the folio exclusively if
1374 * there are no additional references. Note that we always drain
Matthew Wilcox (Oracle)1fec68902023-06-21 17:45:56 +01001375 * the LRU cache immediately after adding a THP.
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001376 */
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001377 if (folio_ref_count(folio) >
1378 1 + folio_test_swapcache(folio) * folio_nr_pages(folio))
David Hildenbrand3bff7e32022-03-24 18:13:43 -07001379 goto unlock_fallback;
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001380 if (folio_test_swapcache(folio))
1381 folio_free_swap(folio);
1382 if (folio_ref_count(folio) == 1) {
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001383 pmd_t entry;
David Hildenbrand6c54dc62022-05-09 18:20:43 -07001384
David Hildenbrand06968622023-10-02 16:29:48 +02001385 folio_move_anon_rmap(folio, vma);
David Hildenbrand5ca43282023-10-02 16:29:47 +02001386 SetPageAnonExclusive(page);
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001387 folio_unlock(folio);
David Hildenbrand6c287602022-05-09 18:20:44 -07001388reuse:
David Hildenbrandc89357e2022-05-09 18:20:45 -07001389 if (unlikely(unshare)) {
1390 spin_unlock(vmf->ptl);
1391 return 0;
1392 }
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001393 entry = pmd_mkyoung(orig_pmd);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001394 entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001395 if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1))
Jan Kara82b0f8c2016-12-14 15:06:58 -08001396 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001397 spin_unlock(vmf->ptl);
David Hildenbrandcb8d8632022-10-21 12:11:35 +02001398 return 0;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001399 }
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001400
David Hildenbrand3bff7e32022-03-24 18:13:43 -07001401unlock_fallback:
Matthew Wilcox (Oracle)2fad3d12022-09-02 20:46:38 +01001402 folio_unlock(folio);
Jan Kara82b0f8c2016-12-14 15:06:58 -08001403 spin_unlock(vmf->ptl);
Kirill A. Shutemov3917c802020-06-03 16:00:27 -07001404fallback:
1405 __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL);
1406 return VM_FAULT_FALLBACK;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001407}
1408
David Hildenbrandc27f4792022-11-08 18:46:48 +01001409static inline bool can_change_pmd_writable(struct vm_area_struct *vma,
1410 unsigned long addr, pmd_t pmd)
1411{
1412 struct page *page;
1413
1414 if (WARN_ON_ONCE(!(vma->vm_flags & VM_WRITE)))
1415 return false;
1416
1417 /* Don't touch entries that are not even readable (NUMA hinting). */
1418 if (pmd_protnone(pmd))
1419 return false;
1420
1421 /* Do we need write faults for softdirty tracking? */
1422 if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
1423 return false;
1424
1425 /* Do we need write faults for uffd-wp tracking? */
1426 if (userfaultfd_huge_pmd_wp(vma, pmd))
1427 return false;
1428
1429 if (!(vma->vm_flags & VM_SHARED)) {
1430 /* See can_change_pte_writable(). */
1431 page = vm_normal_page_pmd(vma, addr, pmd);
1432 return page && PageAnon(page) && PageAnonExclusive(page);
1433 }
1434
1435 /* See can_change_pte_writable(). */
1436 return pmd_dirty(pmd);
1437}
1438
David Hildenbrand5535be32022-08-09 22:56:40 +02001439/* FOLL_FORCE can write to even unwritable PMDs in COW mappings. */
1440static inline bool can_follow_write_pmd(pmd_t pmd, struct page *page,
1441 struct vm_area_struct *vma,
1442 unsigned int flags)
Keno Fischer8310d482017-01-24 15:17:48 -08001443{
David Hildenbrand5535be32022-08-09 22:56:40 +02001444 /* If the pmd is writable, we can write to the page. */
1445 if (pmd_write(pmd))
1446 return true;
1447
1448 /* Maybe FOLL_FORCE is set to override it? */
1449 if (!(flags & FOLL_FORCE))
1450 return false;
1451
1452 /* But FOLL_FORCE has no effect on shared mappings */
1453 if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
1454 return false;
1455
1456 /* ... or read-only private ones */
1457 if (!(vma->vm_flags & VM_MAYWRITE))
1458 return false;
1459
1460 /* ... or already writable ones that just need to take a write fault */
1461 if (vma->vm_flags & VM_WRITE)
1462 return false;
1463
1464 /*
1465 * See can_change_pte_writable(): we broke COW and could map the page
1466 * writable if we have an exclusive anonymous page ...
1467 */
1468 if (!page || !PageAnon(page) || !PageAnonExclusive(page))
1469 return false;
1470
1471 /* ... and a write-fault isn't required for other reasons. */
1472 if (vma_soft_dirty_enabled(vma) && !pmd_soft_dirty(pmd))
1473 return false;
1474 return !userfaultfd_huge_pmd_wp(vma, pmd);
Keno Fischer8310d482017-01-24 15:17:48 -08001475}
1476
David Rientjesb676b292012-10-08 16:34:03 -07001477struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001478 unsigned long addr,
1479 pmd_t *pmd,
1480 unsigned int flags)
1481{
David Rientjesb676b292012-10-08 16:34:03 -07001482 struct mm_struct *mm = vma->vm_mm;
David Hildenbrand5535be32022-08-09 22:56:40 +02001483 struct page *page;
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001484 int ret;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001485
Kirill A. Shutemovc4088eb2013-11-14 14:31:04 -08001486 assert_spin_locked(pmd_lockptr(mm, pmd));
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001487
David Hildenbrand5535be32022-08-09 22:56:40 +02001488 page = pmd_page(*pmd);
1489 VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page);
1490
1491 if ((flags & FOLL_WRITE) &&
1492 !can_follow_write_pmd(*pmd, page, vma, flags))
1493 return NULL;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001494
Kirill A. Shutemov85facf22013-02-04 14:28:42 -08001495 /* Avoid dumping huge zero page */
1496 if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd))
1497 return ERR_PTR(-EFAULT);
1498
David Hildenbrandd74943a2023-08-03 16:32:02 +02001499 if (pmd_protnone(*pmd) && !gup_can_follow_protnone(vma, flags))
David Hildenbrand5535be32022-08-09 22:56:40 +02001500 return NULL;
John Hubbard3faa52c2020-04-01 21:05:29 -07001501
David Hildenbrand84209e82022-11-16 11:26:48 +01001502 if (!pmd_write(*pmd) && gup_must_unshare(vma, flags, page))
David Hildenbranda7f22662022-05-09 18:20:45 -07001503 return ERR_PTR(-EMLINK);
1504
David Hildenbrandb6a26192022-05-09 18:20:45 -07001505 VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) &&
1506 !PageAnonExclusive(page), page);
1507
Logan Gunthorpe0f089232022-10-21 11:41:08 -06001508 ret = try_grab_page(page, flags);
1509 if (ret)
1510 return ERR_PTR(ret);
John Hubbard3faa52c2020-04-01 21:05:29 -07001511
Dan Williams3565fce2016-01-15 16:56:55 -08001512 if (flags & FOLL_TOUCH)
Miaohe Lina69e4712022-07-04 21:21:50 +08001513 touch_pmd(vma, addr, pmd, flags & FOLL_WRITE);
John Hubbard3faa52c2020-04-01 21:05:29 -07001514
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001515 page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT;
Dan Williamsca120cf2016-09-03 10:38:03 -07001516 VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page);
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001517
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001518 return page;
1519}
1520
Mel Gormand10e63f2012-10-25 14:16:31 +02001521/* NUMA hinting page fault entry point for trans huge pmds */
Yang Shi5db4f152021-06-30 18:51:35 -07001522vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)
Mel Gormand10e63f2012-10-25 14:16:31 +02001523{
Jan Kara82b0f8c2016-12-14 15:06:58 -08001524 struct vm_area_struct *vma = vmf->vma;
Yang Shic5b5a3d2021-06-30 18:51:42 -07001525 pmd_t oldpmd = vmf->orig_pmd;
1526 pmd_t pmd;
Kefeng Wang667ffc32023-09-21 15:44:13 +08001527 struct folio *folio;
Jan Kara82b0f8c2016-12-14 15:06:58 -08001528 unsigned long haddr = vmf->address & HPAGE_PMD_MASK;
Kefeng Wang667ffc32023-09-21 15:44:13 +08001529 int nid = NUMA_NO_NODE;
Huang Ying33024532022-07-13 16:39:51 +08001530 int target_nid, last_cpupid = (-1 & LAST_CPUPID_MASK);
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001531 bool migrated = false, writable = false;
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001532 int flags = 0;
Mel Gormand10e63f2012-10-25 14:16:31 +02001533
Jan Kara82b0f8c2016-12-14 15:06:58 -08001534 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
Yang Shic5b5a3d2021-06-30 18:51:42 -07001535 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
Jan Kara82b0f8c2016-12-14 15:06:58 -08001536 spin_unlock(vmf->ptl);
Mel Gormande466bd2013-12-18 17:08:42 -08001537 goto out;
1538 }
1539
Yang Shic5b5a3d2021-06-30 18:51:42 -07001540 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001541
1542 /*
1543 * Detect now whether the PMD could be writable; this information
1544 * is only valid while holding the PT lock.
1545 */
1546 writable = pmd_write(pmd);
1547 if (!writable && vma_wants_manual_pte_write_upgrade(vma) &&
1548 can_change_pmd_writable(vma, vmf->address, pmd))
1549 writable = true;
1550
Kefeng Wang667ffc32023-09-21 15:44:13 +08001551 folio = vm_normal_folio_pmd(vma, haddr, pmd);
1552 if (!folio)
Yang Shic5b5a3d2021-06-30 18:51:42 -07001553 goto out_map;
1554
1555 /* See similar comment in do_numa_page for explanation */
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001556 if (!writable)
Yang Shic5b5a3d2021-06-30 18:51:42 -07001557 flags |= TNF_NO_GROUP;
1558
Kefeng Wang667ffc32023-09-21 15:44:13 +08001559 nid = folio_nid(folio);
Huang Ying33024532022-07-13 16:39:51 +08001560 /*
1561 * For memory tiering mode, cpupid of slow memory page is used
1562 * to record page access time. So use default value.
1563 */
Kefeng Wang667ffc32023-09-21 15:44:13 +08001564 if (node_is_toptier(nid))
Kefeng Wangc4a8d2f2023-10-18 22:07:51 +08001565 last_cpupid = folio_last_cpupid(folio);
Kefeng Wangcda6d932023-09-21 15:44:15 +08001566 target_nid = numa_migrate_prep(folio, vma, haddr, nid, &flags);
Yang Shic5b5a3d2021-06-30 18:51:42 -07001567 if (target_nid == NUMA_NO_NODE) {
Kefeng Wang667ffc32023-09-21 15:44:13 +08001568 folio_put(folio);
Yang Shic5b5a3d2021-06-30 18:51:42 -07001569 goto out_map;
1570 }
1571
Jan Kara82b0f8c2016-12-14 15:06:58 -08001572 spin_unlock(vmf->ptl);
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001573 writable = false;
Peter Zijlstra8b1b4362017-06-07 18:05:07 +02001574
Kefeng Wang667ffc32023-09-21 15:44:13 +08001575 migrated = migrate_misplaced_folio(folio, vma, target_nid);
Peter Zijlstra6688cc02013-10-07 11:29:24 +01001576 if (migrated) {
1577 flags |= TNF_MIGRATED;
Kefeng Wang667ffc32023-09-21 15:44:13 +08001578 nid = target_nid;
Yang Shic5b5a3d2021-06-30 18:51:42 -07001579 } else {
Mel Gorman074c2382015-03-25 15:55:42 -07001580 flags |= TNF_MIGRATE_FAIL;
Yang Shic5b5a3d2021-06-30 18:51:42 -07001581 vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd);
1582 if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) {
1583 spin_unlock(vmf->ptl);
1584 goto out;
1585 }
1586 goto out_map;
1587 }
Mel Gormanb8916632013-10-07 11:28:44 +01001588
1589out:
Kefeng Wang667ffc32023-09-21 15:44:13 +08001590 if (nid != NUMA_NO_NODE)
1591 task_numa_fault(last_cpupid, nid, HPAGE_PMD_NR, flags);
Mel Gorman8191acb2013-10-07 11:28:45 +01001592
Mel Gormand10e63f2012-10-25 14:16:31 +02001593 return 0;
Yang Shic5b5a3d2021-06-30 18:51:42 -07001594
1595out_map:
1596 /* Restore the PMD */
1597 pmd = pmd_modify(oldpmd, vma->vm_page_prot);
1598 pmd = pmd_mkyoung(pmd);
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001599 if (writable)
Rick Edgecombe161e3932023-06-12 17:10:29 -07001600 pmd = pmd_mkwrite(pmd, vma);
Yang Shic5b5a3d2021-06-30 18:51:42 -07001601 set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd);
1602 update_mmu_cache_pmd(vma, vmf->address, vmf->pmd);
1603 spin_unlock(vmf->ptl);
1604 goto out;
Mel Gormand10e63f2012-10-25 14:16:31 +02001605}
1606
Huang Ying319904a2016-07-28 15:48:03 -07001607/*
1608 * Return true if we do MADV_FREE successfully on entire pmd page.
1609 * Otherwise, return false.
1610 */
1611bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001612 pmd_t *pmd, unsigned long addr, unsigned long next)
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001613{
1614 spinlock_t *ptl;
1615 pmd_t orig_pmd;
Kefeng Wangfc986a32022-12-07 10:34:30 +08001616 struct folio *folio;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001617 struct mm_struct *mm = tlb->mm;
Huang Ying319904a2016-07-28 15:48:03 -07001618 bool ret = false;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001619
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001620 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001621
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001622 ptl = pmd_trans_huge_lock(pmd, vma);
1623 if (!ptl)
Linus Torvalds25eedab2016-01-17 18:33:15 -08001624 goto out_unlocked;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001625
1626 orig_pmd = *pmd;
Huang Ying319904a2016-07-28 15:48:03 -07001627 if (is_huge_zero_pmd(orig_pmd))
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001628 goto out;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001629
Zi Yan84c3fc42017-09-08 16:11:01 -07001630 if (unlikely(!pmd_present(orig_pmd))) {
1631 VM_BUG_ON(thp_migration_supported() &&
1632 !is_pmd_migration_entry(orig_pmd));
1633 goto out;
1634 }
1635
Kefeng Wangfc986a32022-12-07 10:34:30 +08001636 folio = pfn_folio(pmd_pfn(orig_pmd));
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001637 /*
Kefeng Wangfc986a32022-12-07 10:34:30 +08001638 * If other processes are mapping this folio, we couldn't discard
1639 * the folio unless they all do MADV_FREE so let's skip the folio.
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001640 */
Yin Fengwei20b18aa2023-08-08 10:09:16 +08001641 if (folio_estimated_sharers(folio) != 1)
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001642 goto out;
1643
Kefeng Wangfc986a32022-12-07 10:34:30 +08001644 if (!folio_trylock(folio))
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001645 goto out;
1646
1647 /*
1648 * If user want to discard part-pages of THP, split it so MADV_FREE
1649 * will deactivate only them.
1650 */
1651 if (next - addr != HPAGE_PMD_SIZE) {
Kefeng Wangfc986a32022-12-07 10:34:30 +08001652 folio_get(folio);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001653 spin_unlock(ptl);
Kefeng Wangfc986a32022-12-07 10:34:30 +08001654 split_folio(folio);
1655 folio_unlock(folio);
1656 folio_put(folio);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001657 goto out_unlocked;
1658 }
1659
Kefeng Wangfc986a32022-12-07 10:34:30 +08001660 if (folio_test_dirty(folio))
1661 folio_clear_dirty(folio);
1662 folio_unlock(folio);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001663
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001664 if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) {
Kirill A. Shutemov58ceeb62017-04-13 14:56:26 -07001665 pmdp_invalidate(vma, addr, pmd);
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001666 orig_pmd = pmd_mkold(orig_pmd);
1667 orig_pmd = pmd_mkclean(orig_pmd);
1668
1669 set_pmd_at(mm, addr, pmd, orig_pmd);
1670 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
1671 }
Shaohua Li802a3a92017-05-03 14:52:32 -07001672
Kefeng Wang6a6fe9e2022-12-09 10:06:18 +08001673 folio_mark_lazyfree(folio);
Huang Ying319904a2016-07-28 15:48:03 -07001674 ret = true;
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08001675out:
1676 spin_unlock(ptl);
1677out_unlocked:
1678 return ret;
1679}
1680
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001681static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd)
1682{
1683 pgtable_t pgtable;
1684
1685 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
1686 pte_free(mm, pgtable);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08001687 mm_dec_nr_ptes(mm);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001688}
1689
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001690int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
Shaohua Lif21760b2012-01-12 17:19:16 -08001691 pmd_t *pmd, unsigned long addr)
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001692{
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001693 pmd_t orig_pmd;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001694 spinlock_t *ptl;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001695
Peter Zijlstraed6a7932018-08-31 14:46:08 +02001696 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
Aneesh Kumar K.V07e32662016-12-12 16:42:40 -08001697
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001698 ptl = __pmd_trans_huge_lock(pmd, vma);
1699 if (!ptl)
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001700 return 0;
1701 /*
1702 * For architectures like ppc64 we look at deposited pgtable
1703 * when calling pmdp_huge_get_and_clear. So do the
1704 * pgtable_trans_huge_withdraw after finishing pmdp related
1705 * operations.
1706 */
Aneesh Kumar K.V93a98692020-05-05 12:47:28 +05301707 orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd,
1708 tlb->fullmm);
Rick Edgecombee5136e82023-06-12 17:10:43 -07001709 arch_check_zapped_pmd(vma, orig_pmd);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001710 tlb_remove_pmd_tlb_entry(tlb, pmd, addr);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01001711 if (vma_is_special_huge(vma)) {
Oliver O'Halloran3b6521f2017-05-08 15:59:43 -07001712 if (arch_needs_pgtable_deposit())
1713 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001714 spin_unlock(ptl);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001715 } else if (is_huge_zero_pmd(orig_pmd)) {
Oliver O'Halloranc14a6eb2017-05-08 15:59:40 -07001716 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001717 spin_unlock(ptl);
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001718 } else {
Zi Yan616b8372017-09-08 16:10:57 -07001719 struct page *page = NULL;
1720 int flush_needed = 1;
1721
1722 if (pmd_present(orig_pmd)) {
1723 page = pmd_page(orig_pmd);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08001724 page_remove_rmap(page, vma, true);
Zi Yan616b8372017-09-08 16:10:57 -07001725 VM_BUG_ON_PAGE(page_mapcount(page) < 0, page);
1726 VM_BUG_ON_PAGE(!PageHead(page), page);
1727 } else if (thp_migration_supported()) {
1728 swp_entry_t entry;
1729
1730 VM_BUG_ON(!is_pmd_migration_entry(orig_pmd));
1731 entry = pmd_to_swp_entry(orig_pmd);
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07001732 page = pfn_swap_entry_to_page(entry);
Zi Yan616b8372017-09-08 16:10:57 -07001733 flush_needed = 0;
1734 } else
1735 WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!");
1736
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001737 if (PageAnon(page)) {
Oliver O'Halloranc14a6eb2017-05-08 15:59:40 -07001738 zap_deposited_table(tlb->mm, pmd);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001739 add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR);
1740 } else {
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08001741 if (arch_needs_pgtable_deposit())
1742 zap_deposited_table(tlb->mm, pmd);
Yang Shifadae292018-08-17 15:44:55 -07001743 add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR);
Kirill A. Shutemovb5072382016-07-26 15:25:34 -07001744 }
Zi Yan616b8372017-09-08 16:10:57 -07001745
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001746 spin_unlock(ptl);
Zi Yan616b8372017-09-08 16:10:57 -07001747 if (flush_needed)
1748 tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE);
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001749 }
Kirill A. Shutemovda146762015-09-08 14:59:31 -07001750 return 1;
Andrea Arcangeli71e3aac2011-01-13 15:46:52 -08001751}
1752
Aneesh Kumar K.V1dd38b62016-12-12 16:44:29 -08001753#ifndef pmd_move_must_withdraw
1754static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl,
1755 spinlock_t *old_pmd_ptl,
1756 struct vm_area_struct *vma)
1757{
1758 /*
1759 * With split pmd lock we also need to move preallocated
1760 * PTE page table if new_pmd is on different PMD page table.
1761 *
1762 * We also don't deposit and withdraw tables for file pages.
1763 */
1764 return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma);
1765}
1766#endif
1767
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001768static pmd_t move_soft_dirty_pmd(pmd_t pmd)
1769{
1770#ifdef CONFIG_MEM_SOFT_DIRTY
1771 if (unlikely(is_pmd_migration_entry(pmd)))
1772 pmd = pmd_swp_mksoft_dirty(pmd);
1773 else if (pmd_present(pmd))
1774 pmd = pmd_mksoft_dirty(pmd);
1775#endif
1776 return pmd;
1777}
1778
Hugh Dickinsbf8616d2016-05-19 17:12:54 -07001779bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
Wei Yangb8aa9d92020-08-06 23:23:40 -07001780 unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001781{
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001782 spinlock_t *old_ptl, *new_ptl;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001783 pmd_t pmd;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001784 struct mm_struct *mm = vma->vm_mm;
Aaron Lu5d190422016-11-10 17:16:33 +08001785 bool force_flush = false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001786
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001787 /*
1788 * The destination pmd shouldn't be established, free_pgtables()
Hugh Dickinsa5be6212023-06-08 18:32:47 -07001789 * should have released it; but move_page_tables() might have already
1790 * inserted a page table, if racing against shmem/file collapse.
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001791 */
Hugh Dickinsa5be6212023-06-08 18:32:47 -07001792 if (!pmd_none(*new_pmd)) {
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001793 VM_BUG_ON(pmd_trans_huge(*new_pmd));
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001794 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001795 }
1796
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001797 /*
1798 * We don't have to worry about the ordering of src and dst
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001799 * ptlocks because exclusive mmap_lock prevents deadlock.
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001800 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001801 old_ptl = __pmd_trans_huge_lock(old_pmd, vma);
1802 if (old_ptl) {
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001803 new_ptl = pmd_lockptr(mm, new_pmd);
1804 if (new_ptl != old_ptl)
1805 spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
Aneesh Kumar K.V8809aa22015-06-24 16:57:44 -07001806 pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd);
Linus Torvaldseb66ae02018-10-12 15:22:59 -07001807 if (pmd_present(pmd))
Aaron Lua2ce2662016-11-29 13:27:31 +08001808 force_flush = true;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001809 VM_BUG_ON(!pmd_none(*new_pmd));
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001810
Aneesh Kumar K.V1dd38b62016-12-12 16:44:29 -08001811 if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) {
Aneesh Kumar K.Vb3084f42014-01-13 11:34:24 +05301812 pgtable_t pgtable;
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001813 pgtable = pgtable_trans_huge_withdraw(mm, old_pmd);
1814 pgtable_trans_huge_deposit(mm, new_pmd, pgtable);
Kirill A. Shutemov35928062013-12-12 17:12:33 -08001815 }
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001816 pmd = move_soft_dirty_pmd(pmd);
1817 set_pmd_at(mm, new_addr, new_pmd, pmd);
Aaron Lu5d190422016-11-10 17:16:33 +08001818 if (force_flush)
Miaohe Lin7c38f182022-07-04 21:21:46 +08001819 flush_pmd_tlb_range(vma, old_addr, old_addr + PMD_SIZE);
Linus Torvaldseb66ae02018-10-12 15:22:59 -07001820 if (new_ptl != old_ptl)
1821 spin_unlock(new_ptl);
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001822 spin_unlock(old_ptl);
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001823 return true;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001824 }
Kirill A. Shutemov4b471e82016-01-15 16:53:39 -08001825 return false;
Andrea Arcangeli37a1c492011-10-31 17:08:30 -07001826}
1827
Mel Gormanf123d742013-10-07 11:28:49 +01001828/*
1829 * Returns
1830 * - 0 if PMD could not be locked
Ingo Molnarf0953a12021-05-06 18:06:47 -07001831 * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary
Yang Shie346e662021-06-30 18:51:55 -07001832 * or if prot_numa but THP migration is not supported
Ingo Molnarf0953a12021-05-06 18:06:47 -07001833 * - HPAGE_PMD_NR if protections changed and TLB flush necessary
Mel Gormanf123d742013-10-07 11:28:49 +01001834 */
Nadav Amit4a184192022-05-09 18:20:50 -07001835int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,
1836 pmd_t *pmd, unsigned long addr, pgprot_t newprot,
1837 unsigned long cp_flags)
Johannes Weinercd7548a2011-01-13 15:47:04 -08001838{
1839 struct mm_struct *mm = vma->vm_mm;
Kirill A. Shutemovbf929152013-11-14 14:30:54 -08001840 spinlock_t *ptl;
Nadav Amitc9fe6652022-05-09 18:20:50 -07001841 pmd_t oldpmd, entry;
Peter Xu58705442020-04-06 20:05:45 -07001842 bool prot_numa = cp_flags & MM_CP_PROT_NUMA;
Peter Xu292924b2020-04-06 20:05:49 -07001843 bool uffd_wp = cp_flags & MM_CP_UFFD_WP;
1844 bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE;
David Hildenbrand6a56ccb2022-11-08 18:46:50 +01001845 int ret = 1;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001846
Nadav Amit4a184192022-05-09 18:20:50 -07001847 tlb_change_page_size(tlb, HPAGE_PMD_SIZE);
1848
Yang Shie346e662021-06-30 18:51:55 -07001849 if (prot_numa && !thp_migration_supported())
1850 return 1;
1851
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001852 ptl = __pmd_trans_huge_lock(pmd, vma);
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001853 if (!ptl)
1854 return 0;
Mel Gormane944fd62015-02-12 14:58:35 -08001855
Zi Yan84c3fc42017-09-08 16:11:01 -07001856#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
1857 if (is_swap_pmd(*pmd)) {
1858 swp_entry_t entry = pmd_to_swp_entry(*pmd);
Kefeng Wangd986ba22023-10-18 22:07:57 +08001859 struct folio *folio = page_folio(pfn_swap_entry_to_page(entry));
David Hildenbrand24bf08c2023-04-05 18:02:35 +02001860 pmd_t newpmd;
Zi Yan84c3fc42017-09-08 16:11:01 -07001861
1862 VM_BUG_ON(!is_pmd_migration_entry(*pmd));
Alistair Popple4dd845b2021-06-30 18:54:09 -07001863 if (is_writable_migration_entry(entry)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07001864 /*
1865 * A protection check is difficult so
1866 * just be safe and disable write
1867 */
Kefeng Wangd986ba22023-10-18 22:07:57 +08001868 if (folio_test_anon(folio))
David Hildenbrand6c287602022-05-09 18:20:44 -07001869 entry = make_readable_exclusive_migration_entry(swp_offset(entry));
1870 else
1871 entry = make_readable_migration_entry(swp_offset(entry));
Zi Yan84c3fc42017-09-08 16:11:01 -07001872 newpmd = swp_entry_to_pmd(entry);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07001873 if (pmd_swp_soft_dirty(*pmd))
1874 newpmd = pmd_swp_mksoft_dirty(newpmd);
David Hildenbrand24bf08c2023-04-05 18:02:35 +02001875 } else {
1876 newpmd = *pmd;
Zi Yan84c3fc42017-09-08 16:11:01 -07001877 }
David Hildenbrand24bf08c2023-04-05 18:02:35 +02001878
1879 if (uffd_wp)
1880 newpmd = pmd_swp_mkuffd_wp(newpmd);
1881 else if (uffd_wp_resolve)
1882 newpmd = pmd_swp_clear_uffd_wp(newpmd);
1883 if (!pmd_same(*pmd, newpmd))
1884 set_pmd_at(mm, addr, pmd, newpmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001885 goto unlock;
1886 }
1887#endif
1888
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001889 if (prot_numa) {
Kefeng Wangd986ba22023-10-18 22:07:57 +08001890 struct folio *folio;
Huang Ying33024532022-07-13 16:39:51 +08001891 bool toptier;
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001892 /*
1893 * Avoid trapping faults against the zero page. The read-only
1894 * data is likely to be read-cached on the local CPU and
1895 * local/remote hits to the zero page are not interesting.
1896 */
1897 if (is_huge_zero_pmd(*pmd))
1898 goto unlock;
Johannes Weinercd7548a2011-01-13 15:47:04 -08001899
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001900 if (pmd_protnone(*pmd))
1901 goto unlock;
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001902
Kefeng Wangd986ba22023-10-18 22:07:57 +08001903 folio = page_folio(pmd_page(*pmd));
1904 toptier = node_is_toptier(folio_nid(folio));
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001905 /*
1906 * Skip scanning top tier node if normal numa
1907 * balancing is disabled
1908 */
1909 if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) &&
Huang Ying33024532022-07-13 16:39:51 +08001910 toptier)
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001911 goto unlock;
Huang Ying33024532022-07-13 16:39:51 +08001912
1913 if (sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING &&
1914 !toptier)
Kefeng Wangd986ba22023-10-18 22:07:57 +08001915 folio_xchg_access_time(folio,
1916 jiffies_to_msecs(jiffies));
Huang Yinga1a3a2f2022-03-22 14:46:27 -07001917 }
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001918 /*
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001919 * In case prot_numa, we are under mmap_read_lock(mm). It's critical
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001920 * to not clear pmd intermittently to avoid race with MADV_DONTNEED
Michel Lespinasse3e4e28c2020-06-08 21:33:51 -07001921 * which is also under mmap_read_lock(mm):
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001922 *
1923 * CPU0: CPU1:
1924 * change_huge_pmd(prot_numa=1)
1925 * pmdp_huge_get_and_clear_notify()
1926 * madvise_dontneed()
1927 * zap_pmd_range()
1928 * pmd_trans_huge(*pmd) == 0 (without ptl)
1929 * // skip the pmd
1930 * set_pmd_at();
1931 * // pmd is re-established
1932 *
1933 * The race makes MADV_DONTNEED miss the huge pmd and don't clear it
1934 * which may break userspace.
1935 *
Nadav Amit4f831452022-05-09 18:20:50 -07001936 * pmdp_invalidate_ad() is required to make sure we don't miss
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001937 * dirty/young flags set by hardware.
1938 */
Nadav Amit4f831452022-05-09 18:20:50 -07001939 oldpmd = pmdp_invalidate_ad(vma, addr, pmd);
Kirill A. Shutemovced10802017-04-13 14:56:20 -07001940
Nadav Amitc9fe6652022-05-09 18:20:50 -07001941 entry = pmd_modify(oldpmd, newprot);
Peter Xuf1eb1ba2022-12-14 15:15:33 -05001942 if (uffd_wp)
Peter Xu292924b2020-04-06 20:05:49 -07001943 entry = pmd_mkuffd_wp(entry);
Peter Xuf1eb1ba2022-12-14 15:15:33 -05001944 else if (uffd_wp_resolve)
Peter Xu292924b2020-04-06 20:05:49 -07001945 /*
1946 * Leave the write bit to be handled by PF interrupt
1947 * handler, then things like COW could be properly
1948 * handled.
1949 */
1950 entry = pmd_clear_uffd_wp(entry);
David Hildenbrandc27f4792022-11-08 18:46:48 +01001951
1952 /* See change_pte_range(). */
1953 if ((cp_flags & MM_CP_TRY_CHANGE_WRITABLE) && !pmd_write(entry) &&
1954 can_change_pmd_writable(vma, addr, entry))
Rick Edgecombe161e3932023-06-12 17:10:29 -07001955 entry = pmd_mkwrite(entry, vma);
David Hildenbrandc27f4792022-11-08 18:46:48 +01001956
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001957 ret = HPAGE_PMD_NR;
1958 set_pmd_at(mm, addr, pmd, entry);
Nadav Amit4a184192022-05-09 18:20:50 -07001959
Nadav Amitc9fe6652022-05-09 18:20:50 -07001960 if (huge_pmd_needs_flush(oldpmd, entry))
1961 tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE);
Kirill A. Shutemov0a85e51d2017-04-13 14:56:17 -07001962unlock:
1963 spin_unlock(ptl);
Johannes Weinercd7548a2011-01-13 15:47:04 -08001964 return ret;
1965}
1966
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001967/*
Huang Ying8f19b0c2016-07-26 15:27:04 -07001968 * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001969 *
Huang Ying8f19b0c2016-07-26 15:27:04 -07001970 * Note that if it returns page table lock pointer, this routine returns without
1971 * unlocking page table lock. So callers must unlock it.
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001972 */
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001973spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma)
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001974{
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001975 spinlock_t *ptl;
1976 ptl = pmd_lock(vma->vm_mm, pmd);
Zi Yan84c3fc42017-09-08 16:11:01 -07001977 if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) ||
1978 pmd_devmap(*pmd)))
Kirill A. Shutemovb6ec57f2016-01-21 16:40:25 -08001979 return ptl;
1980 spin_unlock(ptl);
1981 return NULL;
Naoya Horiguchi025c5b22012-03-21 16:33:57 -07001982}
1983
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001984/*
Miaohe Lind965e392022-07-04 21:21:48 +08001985 * Returns page table lock pointer if a given pud maps a thp, NULL otherwise.
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001986 *
Miaohe Lind965e392022-07-04 21:21:48 +08001987 * Note that if it returns page table lock pointer, this routine returns without
1988 * unlocking page table lock. So callers must unlock it.
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08001989 */
1990spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma)
1991{
1992 spinlock_t *ptl;
1993
1994 ptl = pud_lock(vma->vm_mm, pud);
1995 if (likely(pud_trans_huge(*pud) || pud_devmap(*pud)))
1996 return ptl;
1997 spin_unlock(ptl);
1998 return NULL;
1999}
2000
2001#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
2002int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,
2003 pud_t *pud, unsigned long addr)
2004{
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002005 spinlock_t *ptl;
2006
2007 ptl = __pud_trans_huge_lock(pud, vma);
2008 if (!ptl)
2009 return 0;
Miaohe Lin749290792022-07-04 21:21:54 +08002010
Aneesh Kumar K.Vf32928a2023-07-25 00:37:48 +05302011 pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002012 tlb_remove_pud_tlb_entry(tlb, pud, addr);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01002013 if (vma_is_special_huge(vma)) {
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002014 spin_unlock(ptl);
2015 /* No zero page support yet */
2016 } else {
2017 /* No support for anonymous PUD pages yet */
2018 BUG();
2019 }
2020 return 1;
2021}
2022
2023static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud,
2024 unsigned long haddr)
2025{
2026 VM_BUG_ON(haddr & ~HPAGE_PUD_MASK);
2027 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2028 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma);
2029 VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud));
2030
Yisheng Xiece9311c2017-03-09 16:17:00 -08002031 count_vm_event(THP_SPLIT_PUD);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002032
Alistair Poppleec8832d2023-07-25 23:42:06 +10002033 pudp_huge_clear_flush(vma, haddr, pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002034}
2035
2036void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud,
2037 unsigned long address)
2038{
2039 spinlock_t *ptl;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002040 struct mmu_notifier_range range;
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002041
Alistair Popple7d4a8be2023-01-10 13:57:22 +11002042 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07002043 address & HPAGE_PUD_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002044 (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE);
2045 mmu_notifier_invalidate_range_start(&range);
2046 ptl = pud_lock(vma->vm_mm, pud);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002047 if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud)))
2048 goto out;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002049 __split_huge_pud_locked(vma, pud, range.start);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002050
2051out:
2052 spin_unlock(ptl);
Alistair Poppleec8832d2023-07-25 23:42:06 +10002053 mmu_notifier_invalidate_range_end(&range);
Matthew Wilcoxa00cc7d2017-02-24 14:57:02 -08002054}
2055#endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
2056
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002057static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,
2058 unsigned long haddr, pmd_t *pmd)
2059{
2060 struct mm_struct *mm = vma->vm_mm;
2061 pgtable_t pgtable;
David Hildenbrand42b2af22023-03-02 18:54:23 +01002062 pmd_t _pmd, old_pmd;
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002063 unsigned long addr;
2064 pte_t *pte;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002065 int i;
2066
Jérôme Glisse0f108512017-11-15 17:34:07 -08002067 /*
2068 * Leave pmd empty until pte is filled note that it is fine to delay
2069 * notification until mmu_notifier_invalidate_range_end() as we are
2070 * replacing a zero pmd write protected page with a zero pte write
2071 * protected page.
2072 *
Mike Rapoportee657282022-06-27 09:00:26 +03002073 * See Documentation/mm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -08002074 */
David Hildenbrand42b2af22023-03-02 18:54:23 +01002075 old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002076
2077 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2078 pmd_populate(mm, &_pmd, pgtable);
2079
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002080 pte = pte_offset_map(&_pmd, haddr);
2081 VM_BUG_ON(!pte);
2082 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
2083 pte_t entry;
2084
2085 entry = pfn_pte(my_zero_pfn(addr), vma->vm_page_prot);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002086 entry = pte_mkspecial(entry);
David Hildenbrand42b2af22023-03-02 18:54:23 +01002087 if (pmd_uffd_wp(old_pmd))
2088 entry = pte_mkuffd_wp(entry);
Ryan Robertsc33c7942023-06-12 16:15:45 +01002089 VM_BUG_ON(!pte_none(ptep_get(pte)));
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002090 set_pte_at(mm, addr, pte, entry);
2091 pte++;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002092 }
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002093 pte_unmap(pte - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002094 smp_wmb(); /* make pte visible before pmd */
2095 pmd_populate(mm, pmd, pgtable);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002096}
2097
2098static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002099 unsigned long haddr, bool freeze)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002100{
2101 struct mm_struct *mm = vma->vm_mm;
2102 struct page *page;
2103 pgtable_t pgtable;
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002104 pmd_t old_pmd, _pmd;
Peter Xu292924b2020-04-06 20:05:49 -07002105 bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false;
Peter Xu0ccf7f12022-08-11 12:13:28 -04002106 bool anon_exclusive = false, dirty = false;
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002107 unsigned long addr;
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002108 pte_t *pte;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002109 int i;
2110
2111 VM_BUG_ON(haddr & ~HPAGE_PMD_MASK);
2112 VM_BUG_ON_VMA(vma->vm_start > haddr, vma);
2113 VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma);
Zi Yan84c3fc42017-09-08 16:11:01 -07002114 VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd)
2115 && !pmd_devmap(*pmd));
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002116
2117 count_vm_event(THP_SPLIT_PMD);
2118
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002119 if (!vma_is_anonymous(vma)) {
Alistair Poppleec8832d2023-07-25 23:42:06 +10002120 old_pmd = pmdp_huge_clear_flush(vma, haddr, pmd);
Aneesh Kumar K.V953c66c2016-12-12 16:44:32 -08002121 /*
2122 * We are going to unmap this huge page. So
2123 * just go ahead and zap it
2124 */
2125 if (arch_needs_pgtable_deposit())
2126 zap_deposited_table(mm, pmd);
Thomas Hellstrom (VMware)2484ca92020-03-24 18:47:17 +01002127 if (vma_is_special_huge(vma))
Kirill A. Shutemovd21b9e52016-07-26 15:25:37 -07002128 return;
Hugh Dickins99fa8a42021-06-15 18:23:45 -07002129 if (unlikely(is_pmd_migration_entry(old_pmd))) {
2130 swp_entry_t entry;
2131
2132 entry = pmd_to_swp_entry(old_pmd);
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002133 page = pfn_swap_entry_to_page(entry);
Hugh Dickins99fa8a42021-06-15 18:23:45 -07002134 } else {
2135 page = pmd_page(old_pmd);
2136 if (!PageDirty(page) && pmd_dirty(old_pmd))
2137 set_page_dirty(page);
2138 if (!PageReferenced(page) && pmd_young(old_pmd))
2139 SetPageReferenced(page);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08002140 page_remove_rmap(page, vma, true);
Hugh Dickins99fa8a42021-06-15 18:23:45 -07002141 put_page(page);
2142 }
Yang Shifadae292018-08-17 15:44:55 -07002143 add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002144 return;
Hugh Dickins99fa8a42021-06-15 18:23:45 -07002145 }
2146
Hugh Dickins3b77e8c2021-06-15 18:23:49 -07002147 if (is_huge_zero_pmd(*pmd)) {
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002148 /*
2149 * FIXME: Do we want to invalidate secondary mmu by calling
Alistair Popple1af5a812023-07-25 23:42:07 +10002150 * mmu_notifier_arch_invalidate_secondary_tlbs() see comments below
2151 * inside __split_huge_pmd() ?
Jérôme Glisse4645b9f2017-11-15 17:34:11 -08002152 *
2153 * We are going from a zero huge page write protected to zero
2154 * small page also write protected so it does not seems useful
2155 * to invalidate secondary mmu at this time.
2156 */
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002157 return __split_huge_zero_page_pmd(vma, haddr, pmd);
2158 }
2159
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002160 /*
2161 * Up to this point the pmd is present and huge and userland has the
2162 * whole access to the hugepage during the split (which happens in
2163 * place). If we overwrite the pmd with the not-huge version pointing
2164 * to the pte here (which of course we could if all CPUs were bug
2165 * free), userland could trigger a small page size TLB miss on the
2166 * small sized TLB while the hugepage TLB entry is still established in
2167 * the huge TLB. Some CPU doesn't like that.
Alexander A. Klimov42742d92020-08-06 23:26:08 -07002168 * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum
2169 * 383 on page 105. Intel should be safe but is also warns that it's
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002170 * only safe if the permission and cache attributes of the two entries
2171 * loaded in the two TLB is identical (which should be the case here).
2172 * But it is generally safer to never allow small and huge TLB entries
2173 * for the same virtual address to be loaded simultaneously. So instead
2174 * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the
2175 * current pmd notpresent (atomically because here the pmd_trans_huge
2176 * must remain set at all times on the pmd until the split is complete
2177 * for this pmd), then we flush the SMP TLB and finally we write the
2178 * non-huge version of the pmd entry with pmd_populate.
2179 */
2180 old_pmd = pmdp_invalidate(vma, haddr, pmd);
2181
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002182 pmd_migration = is_pmd_migration_entry(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002183 if (unlikely(pmd_migration)) {
Zi Yan84c3fc42017-09-08 16:11:01 -07002184 swp_entry_t entry;
2185
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002186 entry = pmd_to_swp_entry(old_pmd);
Alistair Poppleaf5cdaf2021-06-30 18:54:06 -07002187 page = pfn_swap_entry_to_page(entry);
Alistair Popple4dd845b2021-06-30 18:54:09 -07002188 write = is_writable_migration_entry(entry);
David Hildenbrand6c287602022-05-09 18:20:44 -07002189 if (PageAnon(page))
2190 anon_exclusive = is_readable_exclusive_migration_entry(entry);
Peter Xu2e346872022-08-11 12:13:29 -04002191 young = is_migration_entry_young(entry);
2192 dirty = is_migration_entry_dirty(entry);
Peter Xu2e83ee12018-12-21 14:30:50 -08002193 soft_dirty = pmd_swp_soft_dirty(old_pmd);
Peter Xuf45ec5f2020-04-06 20:06:01 -07002194 uffd_wp = pmd_swp_uffd_wp(old_pmd);
Peter Xu2e83ee12018-12-21 14:30:50 -08002195 } else {
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002196 page = pmd_page(old_pmd);
Peter Xu0ccf7f12022-08-11 12:13:28 -04002197 if (pmd_dirty(old_pmd)) {
2198 dirty = true;
Peter Xu2e83ee12018-12-21 14:30:50 -08002199 SetPageDirty(page);
Peter Xu0ccf7f12022-08-11 12:13:28 -04002200 }
Peter Xu2e83ee12018-12-21 14:30:50 -08002201 write = pmd_write(old_pmd);
2202 young = pmd_young(old_pmd);
2203 soft_dirty = pmd_soft_dirty(old_pmd);
Peter Xu292924b2020-04-06 20:05:49 -07002204 uffd_wp = pmd_uffd_wp(old_pmd);
David Hildenbrand6c287602022-05-09 18:20:44 -07002205
Hugh Dickins9d846042022-03-22 14:46:11 -07002206 VM_BUG_ON_PAGE(!page_count(page), page);
David Hildenbrand6c287602022-05-09 18:20:44 -07002207
2208 /*
2209 * Without "freeze", we'll simply split the PMD, propagating the
2210 * PageAnonExclusive() flag for each PTE by setting it for
2211 * each subpage -- no need to (temporarily) clear.
2212 *
2213 * With "freeze" we want to replace mapped pages by
2214 * migration entries right away. This is only possible if we
2215 * managed to clear PageAnonExclusive() -- see
2216 * set_pmd_migration_entry().
2217 *
2218 * In case we cannot clear PageAnonExclusive(), split the PMD
2219 * only and let try_to_migrate_one() fail later.
David Hildenbrand088b8aa2022-09-01 10:35:59 +02002220 *
2221 * See page_try_share_anon_rmap(): invalidate PMD first.
David Hildenbrand6c287602022-05-09 18:20:44 -07002222 */
2223 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
2224 if (freeze && anon_exclusive && page_try_share_anon_rmap(page))
2225 freeze = false;
Hugh Dickins96d82de2022-11-22 01:51:50 -08002226 if (!freeze)
2227 page_ref_add(page, HPAGE_PMD_NR - 1);
Peter Xu2e83ee12018-12-21 14:30:50 -08002228 }
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002229
Aneesh Kumar K.V423ac9a2018-01-31 16:18:24 -08002230 /*
2231 * Withdraw the table only after we mark the pmd entry invalid.
2232 * This's critical for some architectures (Power).
2233 */
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002234 pgtable = pgtable_trans_huge_withdraw(mm, pmd);
2235 pmd_populate(mm, &_pmd, pgtable);
2236
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002237 pte = pte_offset_map(&_pmd, haddr);
2238 VM_BUG_ON(!pte);
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002239 for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) {
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002240 pte_t entry;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002241 /*
2242 * Note that NUMA hinting access restrictions are not
2243 * transferred to avoid any possibility of altering
2244 * permissions across VMAs.
2245 */
Zi Yan84c3fc42017-09-08 16:11:01 -07002246 if (freeze || pmd_migration) {
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002247 swp_entry_t swp_entry;
Alistair Popple4dd845b2021-06-30 18:54:09 -07002248 if (write)
2249 swp_entry = make_writable_migration_entry(
2250 page_to_pfn(page + i));
David Hildenbrand6c287602022-05-09 18:20:44 -07002251 else if (anon_exclusive)
2252 swp_entry = make_readable_exclusive_migration_entry(
2253 page_to_pfn(page + i));
Alistair Popple4dd845b2021-06-30 18:54:09 -07002254 else
2255 swp_entry = make_readable_migration_entry(
2256 page_to_pfn(page + i));
Peter Xu2e346872022-08-11 12:13:29 -04002257 if (young)
2258 swp_entry = make_migration_entry_young(swp_entry);
2259 if (dirty)
2260 swp_entry = make_migration_entry_dirty(swp_entry);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002261 entry = swp_entry_to_pte(swp_entry);
Andrea Arcangeli804dd152016-08-25 15:16:57 -07002262 if (soft_dirty)
2263 entry = pte_swp_mksoft_dirty(entry);
Peter Xuf45ec5f2020-04-06 20:06:01 -07002264 if (uffd_wp)
2265 entry = pte_swp_mkuffd_wp(entry);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002266 } else {
Andrea Arcangeli6d2329f2016-10-07 17:01:22 -07002267 entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot));
David Hildenbrand1462c522023-04-11 16:25:12 +02002268 if (write)
Rick Edgecombe161e3932023-06-12 17:10:29 -07002269 entry = pte_mkwrite(entry, vma);
David Hildenbrand6c287602022-05-09 18:20:44 -07002270 if (anon_exclusive)
2271 SetPageAnonExclusive(page + i);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002272 if (!young)
2273 entry = pte_mkold(entry);
Peter Xue833bc52022-11-25 13:58:57 -05002274 /* NOTE: this may set soft-dirty too on some archs */
2275 if (dirty)
2276 entry = pte_mkdirty(entry);
Andrea Arcangeli804dd152016-08-25 15:16:57 -07002277 if (soft_dirty)
2278 entry = pte_mksoft_dirty(entry);
Peter Xu292924b2020-04-06 20:05:49 -07002279 if (uffd_wp)
2280 entry = pte_mkuffd_wp(entry);
Miaohe Lin5ba72b42023-07-13 20:05:57 +08002281 page_add_anon_rmap(page + i, vma, addr, RMAP_NONE);
Kirill A. Shutemovba988282016-01-15 16:53:56 -08002282 }
Ryan Robertsc33c7942023-06-12 16:15:45 +01002283 VM_BUG_ON(!pte_none(ptep_get(pte)));
Kirill A. Shutemov2ac015e2016-02-24 18:58:03 +03002284 set_pte_at(mm, addr, pte, entry);
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002285 pte++;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002286 }
Hugh Dickinsc9c1ee22023-06-08 18:41:31 -07002287 pte_unmap(pte - 1);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002288
Hugh Dickinscb67f422022-11-02 18:51:38 -07002289 if (!pmd_migration)
2290 page_remove_rmap(page, vma, true);
Hugh Dickins96d82de2022-11-22 01:51:50 -08002291 if (freeze)
2292 put_page(page);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002293
2294 smp_wmb(); /* make pte visible before pmd */
2295 pmd_populate(mm, pmd, pgtable);
2296}
2297
2298void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
Matthew Wilcox (Oracle)af28a982022-01-21 10:44:52 -05002299 unsigned long address, bool freeze, struct folio *folio)
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002300{
2301 spinlock_t *ptl;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002302 struct mmu_notifier_range range;
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002303
Alistair Popple7d4a8be2023-01-10 13:57:22 +11002304 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07002305 address & HPAGE_PMD_MASK,
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08002306 (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE);
2307 mmu_notifier_invalidate_range_start(&range);
2308 ptl = pmd_lock(vma->vm_mm, pmd);
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002309
2310 /*
Matthew Wilcox (Oracle)af28a982022-01-21 10:44:52 -05002311 * If caller asks to setup a migration entry, we need a folio to check
2312 * pmd against. Otherwise we can end up replacing wrong folio.
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002313 */
Matthew Wilcox (Oracle)af28a982022-01-21 10:44:52 -05002314 VM_BUG_ON(freeze && !folio);
Matthew Wilcox (Oracle)83a84412022-04-06 08:41:39 -04002315 VM_WARN_ON_ONCE(folio && !folio_test_locked(folio));
Naoya Horiguchi33f47512016-07-14 12:07:32 -07002316
David Hildenbrand7f760912022-03-24 18:13:56 -07002317 if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) ||
Matthew Wilcox (Oracle)83a84412022-04-06 08:41:39 -04002318 is_pmd_migration_entry(*pmd)) {
Miaohe Lincea33322022-07-04 21:22:00 +08002319 /*
2320 * It's safe to call pmd_page when folio is set because it's
2321 * guaranteed that pmd is present.
2322 */
Matthew Wilcox (Oracle)83a84412022-04-06 08:41:39 -04002323 if (folio && folio != page_folio(pmd_page(*pmd)))
2324 goto out;
David Hildenbrand7f760912022-03-24 18:13:56 -07002325 __split_huge_pmd_locked(vma, pmd, range.start, freeze);
Matthew Wilcox (Oracle)83a84412022-04-06 08:41:39 -04002326 }
David Hildenbrand7f760912022-03-24 18:13:56 -07002327
Kirill A. Shutemove90309c2016-01-15 16:54:33 -08002328out:
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002329 spin_unlock(ptl);
Alistair Poppleec8832d2023-07-25 23:42:06 +10002330 mmu_notifier_invalidate_range_end(&range);
Kirill A. Shutemoveef1b3b2016-01-15 16:53:53 -08002331}
2332
Kirill A. Shutemovfec89c12016-03-17 14:20:10 -07002333void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,
Matthew Wilcox (Oracle)af28a982022-01-21 10:44:52 -05002334 bool freeze, struct folio *folio)
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002335{
Zach O'Keefe50722802022-07-06 16:59:26 -07002336 pmd_t *pmd = mm_find_pmd(vma->vm_mm, address);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002337
Zach O'Keefe50722802022-07-06 16:59:26 -07002338 if (!pmd)
Hugh Dickinsf72e7dc2014-06-23 13:22:05 -07002339 return;
2340
Matthew Wilcox (Oracle)af28a982022-01-21 10:44:52 -05002341 __split_huge_pmd(vma, pmd, address, freeze, folio);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002342}
2343
Miaohe Lin71f9e582021-05-04 18:33:52 -07002344static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address)
2345{
2346 /*
2347 * If the new address isn't hpage aligned and it could previously
2348 * contain an hugepage: check if we need to split an huge pmd.
2349 */
2350 if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) &&
2351 range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE),
2352 ALIGN(address, HPAGE_PMD_SIZE)))
2353 split_huge_pmd_address(vma, address, false, NULL);
2354}
2355
Kirill A. Shutemove1b99962015-09-08 14:58:37 -07002356void vma_adjust_trans_huge(struct vm_area_struct *vma,
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002357 unsigned long start,
2358 unsigned long end,
2359 long adjust_next)
2360{
Miaohe Lin71f9e582021-05-04 18:33:52 -07002361 /* Check if we need to split start first. */
2362 split_huge_pmd_if_needed(vma, start);
2363
2364 /* Check if we need to split end next. */
2365 split_huge_pmd_if_needed(vma, end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002366
2367 /*
Matthew Wilcox (Oracle)68540502022-09-06 19:49:00 +00002368 * If we're also updating the next vma vm_start,
Miaohe Lin71f9e582021-05-04 18:33:52 -07002369 * check if we need to split it.
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002370 */
2371 if (adjust_next > 0) {
Matthew Wilcox (Oracle)68540502022-09-06 19:49:00 +00002372 struct vm_area_struct *next = find_vma(vma->vm_mm, vma->vm_end);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002373 unsigned long nstart = next->vm_start;
Wei Yangf9d86a62020-10-13 16:53:57 -07002374 nstart += adjust_next;
Miaohe Lin71f9e582021-05-04 18:33:52 -07002375 split_huge_pmd_if_needed(next, nstart);
Andrea Arcangeli94fcc582011-01-13 15:47:08 -08002376 }
2377}
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002378
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002379static void unmap_folio(struct folio *folio)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002380{
Alistair Popplea98a2f02021-06-30 18:54:16 -07002381 enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD |
2382 TTU_SYNC;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002383
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002384 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002385
Alistair Popplea98a2f02021-06-30 18:54:16 -07002386 /*
2387 * Anon pages need migration entries to preserve them, but file
2388 * pages can simply be left unmapped, then faulted back on demand.
2389 * If that is ever changed (perhaps for mlock), update remap_page().
2390 */
Matthew Wilcox (Oracle)4b8554c2022-01-28 14:29:43 -05002391 if (folio_test_anon(folio))
2392 try_to_migrate(folio, ttu_flags);
Alistair Popplea98a2f02021-06-30 18:54:16 -07002393 else
Matthew Wilcox (Oracle)869f7ee2022-02-15 09:28:49 -05002394 try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002395}
2396
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002397static void remap_page(struct folio *folio, unsigned long nr)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002398{
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002399 int i = 0;
Hugh Dickinsab02c252021-06-30 18:52:04 -07002400
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002401 /* If unmap_folio() uses try_to_migrate() on file, remove this check */
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002402 if (!folio_test_anon(folio))
Hugh Dickinsab02c252021-06-30 18:52:04 -07002403 return;
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002404 for (;;) {
2405 remove_migration_ptes(folio, folio, true);
2406 i += folio_nr_pages(folio);
2407 if (i >= nr)
2408 break;
2409 folio = folio_next(folio);
Kirill A. Shutemovace71a12017-02-24 14:57:45 -08002410 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002411}
2412
Alex Shi94866632020-12-15 12:33:24 -08002413static void lru_add_page_tail(struct page *head, struct page *tail,
Alex Shi88dcb9a2020-12-15 12:33:20 -08002414 struct lruvec *lruvec, struct list_head *list)
2415{
Alex Shi94866632020-12-15 12:33:24 -08002416 VM_BUG_ON_PAGE(!PageHead(head), head);
2417 VM_BUG_ON_PAGE(PageCompound(tail), head);
2418 VM_BUG_ON_PAGE(PageLRU(tail), head);
Alex Shi6168d0d2020-12-15 12:34:29 -08002419 lockdep_assert_held(&lruvec->lru_lock);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002420
Alex Shi6dbb5742020-12-15 12:33:29 -08002421 if (list) {
Alex Shi88dcb9a2020-12-15 12:33:20 -08002422 /* page reclaim is reclaiming a huge page */
Alex Shi6dbb5742020-12-15 12:33:29 -08002423 VM_WARN_ON(PageLRU(head));
Alex Shi94866632020-12-15 12:33:24 -08002424 get_page(tail);
2425 list_add_tail(&tail->lru, list);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002426 } else {
Alex Shi6dbb5742020-12-15 12:33:29 -08002427 /* head is still on lru (and we have it frozen) */
2428 VM_WARN_ON(!PageLRU(head));
Hugh Dickins07ca7602022-02-14 18:29:54 -08002429 if (PageUnevictable(tail))
2430 tail->mlock_count = 0;
2431 else
2432 list_add_tail(&tail->lru, &head->lru);
Alex Shi6dbb5742020-12-15 12:33:29 -08002433 SetPageLRU(tail);
Alex Shi88dcb9a2020-12-15 12:33:20 -08002434 }
2435}
2436
David Hildenbrand07e09c42023-08-21 18:08:49 +02002437static void __split_huge_page_tail(struct folio *folio, int tail,
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002438 struct lruvec *lruvec, struct list_head *list)
2439{
David Hildenbrand07e09c42023-08-21 18:08:49 +02002440 struct page *head = &folio->page;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002441 struct page *page_tail = head + tail;
David Hildenbrand07e09c42023-08-21 18:08:49 +02002442 /*
2443 * Careful: new_folio is not a "real" folio before we cleared PageTail.
2444 * Don't pass it around before clear_compound_head().
2445 */
2446 struct folio *new_folio = (struct folio *)page_tail;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002447
Kirill A. Shutemov8df651c2016-03-15 14:57:30 -07002448 VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002449
2450 /*
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002451 * Clone page flags before unfreezing refcount.
2452 *
2453 * After successful get_page_unless_zero() might follow flags change,
Haitao Shi8958b242020-12-15 20:47:26 -08002454 * for example lock_page() which set PG_waiters.
David Hildenbrand6c287602022-05-09 18:20:44 -07002455 *
2456 * Note that for mapped sub-pages of an anonymous THP,
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002457 * PG_anon_exclusive has been cleared in unmap_folio() and is stored in
David Hildenbrand6c287602022-05-09 18:20:44 -07002458 * the migration entry instead from where remap_page() will restore it.
2459 * We can still have PG_anon_exclusive set on effectively unmapped and
2460 * unreferenced sub-pages of an anonymous THP: we can simply drop
2461 * PG_anon_exclusive (-> PG_mappedtodisk) for these here.
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002462 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002463 page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
2464 page_tail->flags |= (head->flags &
2465 ((1L << PG_referenced) |
2466 (1L << PG_swapbacked) |
Huang Ying38d8b4e2017-07-06 15:37:18 -07002467 (1L << PG_swapcache) |
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002468 (1L << PG_mlocked) |
2469 (1L << PG_uptodate) |
2470 (1L << PG_active) |
Johannes Weiner1899ad12018-10-26 15:06:04 -07002471 (1L << PG_workingset) |
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002472 (1L << PG_locked) |
Minchan Kimb8d3c4c2016-01-15 16:55:42 -08002473 (1L << PG_unevictable) |
Catalin Marinasb0284cd2022-11-03 18:10:34 -07002474#ifdef CONFIG_ARCH_USES_PG_ARCH_X
Catalin Marinas72e6afa2020-07-02 10:19:30 +01002475 (1L << PG_arch_2) |
Peter Collingbourneef6458b2022-11-03 18:10:37 -07002476 (1L << PG_arch_3) |
Catalin Marinas72e6afa2020-07-02 10:19:30 +01002477#endif
Yu Zhaoec1c86b2022-09-18 02:00:02 -06002478 (1L << PG_dirty) |
2479 LRU_GEN_MASK | LRU_REFS_MASK));
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002480
Hugh Dickinscb67f422022-11-02 18:51:38 -07002481 /* ->mapping in first and second tail page is replaced by other uses */
Hugh Dickins173d9d92018-11-30 14:10:16 -08002482 VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING,
2483 page_tail);
2484 page_tail->mapping = head->mapping;
2485 page_tail->index = head->index + tail;
Mel Gorman71e2d662022-10-19 14:41:56 +01002486
2487 /*
David Hildenbrandcfeed8f2023-08-21 18:08:46 +02002488 * page->private should not be set in tail pages. Fix up and warn once
2489 * if private is unexpectedly set.
Mel Gorman71e2d662022-10-19 14:41:56 +01002490 */
David Hildenbrandcfeed8f2023-08-21 18:08:46 +02002491 if (unlikely(page_tail->private)) {
2492 VM_WARN_ON_ONCE_PAGE(true, page_tail);
Mel Gorman71e2d662022-10-19 14:41:56 +01002493 page_tail->private = 0;
2494 }
David Hildenbrand07e09c42023-08-21 18:08:49 +02002495 if (folio_test_swapcache(folio))
2496 new_folio->swap.val = folio->swap.val + tail;
Hugh Dickins173d9d92018-11-30 14:10:16 -08002497
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002498 /* Page flags must be visible before we make the page non-compound. */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002499 smp_wmb();
2500
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002501 /*
2502 * Clear PageTail before unfreezing page refcount.
2503 *
2504 * After successful get_page_unless_zero() might follow put_page()
2505 * which needs correct compound_head().
2506 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002507 clear_compound_head(page_tail);
2508
Konstantin Khlebnikov605ca5e2018-04-05 16:23:28 -07002509 /* Finally unfreeze refcount. Additional reference from page cache. */
2510 page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) ||
2511 PageSwapCache(head)));
2512
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002513 if (page_is_young(head))
2514 set_page_young(page_tail);
2515 if (page_is_idle(head))
2516 set_page_idle(page_tail);
2517
Kefeng Wangc8253012023-10-18 22:08:02 +08002518 folio_xchg_last_cpupid(new_folio, folio_last_cpupid(folio));
Michal Hocko94723aa2018-04-10 16:30:07 -07002519
2520 /*
2521 * always add to the tail because some iterators expect new
2522 * pages to show after the currently processed elements - e.g.
2523 * migrate_pages
2524 */
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002525 lru_add_page_tail(head, page_tail, lruvec, list);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002526}
2527
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002528static void __split_huge_page(struct page *page, struct list_head *list,
Alex Shib6769832020-12-15 12:33:33 -08002529 pgoff_t end)
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002530{
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -04002531 struct folio *folio = page_folio(page);
2532 struct page *head = &folio->page;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002533 struct lruvec *lruvec;
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002534 struct address_space *swap_cache = NULL;
2535 unsigned long offset = 0;
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002536 unsigned int nr = thp_nr_pages(head);
Hugh Dickins509f0062023-07-25 16:45:10 +02002537 int i, nr_dropped = 0;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002538
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002539 /* complete memcg works before add pages to LRU */
Zhou Guanghuibe6c8982021-03-12 21:08:30 -08002540 split_page_memcg(head, nr);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002541
David Hildenbrand07e09c42023-08-21 18:08:49 +02002542 if (folio_test_anon(folio) && folio_test_swapcache(folio)) {
2543 offset = swp_offset(folio->swap);
2544 swap_cache = swap_address_space(folio->swap);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002545 xa_lock(&swap_cache->i_pages);
2546 }
2547
Ingo Molnarf0953a12021-05-06 18:06:47 -07002548 /* lock lru list/PageCompound, ref frozen by page_ref_freeze */
Matthew Wilcox (Oracle)e809c3f2021-06-28 21:59:47 -04002549 lruvec = folio_lruvec_lock(folio);
Alex Shib6769832020-12-15 12:33:33 -08002550
Yang Shieac96c32021-10-28 14:36:11 -07002551 ClearPageHasHWPoisoned(head);
2552
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002553 for (i = nr - 1; i >= 1; i--) {
David Hildenbrand07e09c42023-08-21 18:08:49 +02002554 __split_huge_page_tail(folio, i, lruvec, list);
Hugh Dickinsd144bf62021-09-02 14:54:21 -07002555 /* Some pages can be beyond EOF: drop them from page cache */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002556 if (head[i].index >= end) {
Matthew Wilcox (Oracle)fb5c2022022-06-28 20:15:29 -04002557 struct folio *tail = page_folio(head + i);
2558
Hugh Dickinsd144bf62021-09-02 14:54:21 -07002559 if (shmem_mapping(head->mapping))
Hugh Dickins509f0062023-07-25 16:45:10 +02002560 nr_dropped++;
Matthew Wilcox (Oracle)fb5c2022022-06-28 20:15:29 -04002561 else if (folio_test_clear_dirty(tail))
2562 folio_account_cleaned(tail,
2563 inode_to_wb(folio->mapping->host));
2564 __filemap_remove_folio(tail, NULL);
2565 folio_put(tail);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002566 } else if (!PageAnon(page)) {
2567 __xa_store(&head->mapping->i_pages, head[i].index,
2568 head + i, 0);
2569 } else if (swap_cache) {
2570 __xa_store(&swap_cache->i_pages, offset + i,
2571 head + i, 0);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002572 }
2573 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002574
2575 ClearPageCompound(head);
Alex Shi6168d0d2020-12-15 12:34:29 -08002576 unlock_page_lruvec(lruvec);
Alex Shib6769832020-12-15 12:33:33 -08002577 /* Caller disabled irqs, so they are still disabled here */
Vlastimil Babkaf7da6772019-08-24 17:54:59 -07002578
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002579 split_page_owner(head, nr);
Vlastimil Babkaf7da6772019-08-24 17:54:59 -07002580
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002581 /* See comment in __split_huge_page_tail() */
2582 if (PageAnon(head)) {
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002583 /* Additional pin to swap cache */
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002584 if (PageSwapCache(head)) {
Huang Ying38d8b4e2017-07-06 15:37:18 -07002585 page_ref_add(head, 2);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002586 xa_unlock(&swap_cache->i_pages);
2587 } else {
Huang Ying38d8b4e2017-07-06 15:37:18 -07002588 page_ref_inc(head);
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07002589 }
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002590 } else {
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002591 /* Additional pin to page cache */
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002592 page_ref_add(head, 2);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07002593 xa_unlock(&head->mapping->i_pages);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002594 }
Alex Shib6769832020-12-15 12:33:33 -08002595 local_irq_enable();
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002596
Hugh Dickins509f0062023-07-25 16:45:10 +02002597 if (nr_dropped)
2598 shmem_uncharge(head->mapping->host, nr_dropped);
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002599 remap_page(folio, nr);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002600
David Hildenbrand07e09c42023-08-21 18:08:49 +02002601 if (folio_test_swapcache(folio))
2602 split_swap_cluster(folio->swap);
Huang Yingc4f9c702020-10-15 20:06:07 -07002603
Kirill A. Shutemov8cce5472020-10-15 20:05:36 -07002604 for (i = 0; i < nr; i++) {
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002605 struct page *subpage = head + i;
2606 if (subpage == page)
2607 continue;
2608 unlock_page(subpage);
2609
2610 /*
2611 * Subpages may be freed if there wasn't any mapping
2612 * like if add_to_swap() is running on a lru page that
2613 * had its mapping zapped. And freeing these pages
2614 * requires taking the lru_lock so we do the put_page
2615 * of the tail pages after the split is complete.
2616 */
Miaohe Lin0b175462022-07-04 21:21:56 +08002617 free_page_and_swap_cache(subpage);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002618 }
2619}
2620
Huang Yingb8f593c2017-07-06 15:37:28 -07002621/* Racy check whether the huge page can be split */
Matthew Wilcox (Oracle)d4b40842022-02-04 14:13:31 -05002622bool can_split_folio(struct folio *folio, int *pextra_pins)
Huang Yingb8f593c2017-07-06 15:37:28 -07002623{
2624 int extra_pins;
2625
Matthew Wilcoxaa5dc072017-12-04 10:16:10 -05002626 /* Additional pins from page cache */
Matthew Wilcox (Oracle)d4b40842022-02-04 14:13:31 -05002627 if (folio_test_anon(folio))
2628 extra_pins = folio_test_swapcache(folio) ?
2629 folio_nr_pages(folio) : 0;
Huang Yingb8f593c2017-07-06 15:37:28 -07002630 else
Matthew Wilcox (Oracle)d4b40842022-02-04 14:13:31 -05002631 extra_pins = folio_nr_pages(folio);
Huang Yingb8f593c2017-07-06 15:37:28 -07002632 if (pextra_pins)
2633 *pextra_pins = extra_pins;
Matthew Wilcox (Oracle)d4b40842022-02-04 14:13:31 -05002634 return folio_mapcount(folio) == folio_ref_count(folio) - extra_pins - 1;
Huang Yingb8f593c2017-07-06 15:37:28 -07002635}
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{
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002658 struct folio *folio = page_folio(page);
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +00002659 struct deferred_split *ds_queue = get_deferred_split_queue(folio);
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002660 XA_STATE(xas, &folio->mapping->i_pages, folio->index);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002661 struct anon_vma *anon_vma = NULL;
2662 struct address_space *mapping = NULL;
Yang Shi504e0702021-06-15 18:24:07 -07002663 int extra_pins, ret;
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002664 pgoff_t end;
Xu Yu478d1342022-04-28 23:14:43 -07002665 bool is_hzp;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002666
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002667 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio);
2668 VM_BUG_ON_FOLIO(!folio_test_large(folio), folio);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002669
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002670 is_hzp = is_huge_zero_page(&folio->page);
Naoya Horiguchi4737edb2023-04-06 17:20:04 +09002671 if (is_hzp) {
2672 pr_warn_ratelimited("Called split_huge_page for huge zero page\n");
Xu Yu478d1342022-04-28 23:14:43 -07002673 return -EBUSY;
Naoya Horiguchi4737edb2023-04-06 17:20:04 +09002674 }
Xu Yu478d1342022-04-28 23:14:43 -07002675
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002676 if (folio_test_writeback(folio))
Huang Ying59807682017-09-06 16:22:34 -07002677 return -EBUSY;
2678
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002679 if (folio_test_anon(folio)) {
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002680 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002681 * The caller does not necessarily hold an mmap_lock that would
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002682 * prevent the anon_vma disappearing so we first we take a
2683 * reference to it and then lock the anon_vma for write. This
Matthew Wilcox (Oracle)2f031c62022-01-29 16:06:53 -05002684 * is similar to folio_lock_anon_vma_read except the write lock
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002685 * is taken to serialise against parallel split or collapse
2686 * operations.
2687 */
Matthew Wilcox (Oracle)29eea9b2022-09-02 20:46:50 +01002688 anon_vma = folio_get_anon_vma(folio);
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002689 if (!anon_vma) {
2690 ret = -EBUSY;
2691 goto out;
2692 }
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002693 end = -1;
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002694 mapping = NULL;
2695 anon_vma_lock_write(anon_vma);
2696 } else {
Yin Fengwei6a3edd292022-08-10 14:49:07 +08002697 gfp_t gfp;
2698
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002699 mapping = folio->mapping;
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002700
2701 /* Truncated ? */
2702 if (!mapping) {
2703 ret = -EBUSY;
2704 goto out;
2705 }
2706
Yin Fengwei6a3edd292022-08-10 14:49:07 +08002707 gfp = current_gfp_context(mapping_gfp_mask(mapping) &
2708 GFP_RECLAIM_MASK);
2709
David Howells0201ebf2023-06-28 11:48:51 +01002710 if (!filemap_release_folio(folio, gfp)) {
Yin Fengwei6a3edd292022-08-10 14:49:07 +08002711 ret = -EBUSY;
2712 goto out;
2713 }
2714
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002715 xas_split_alloc(&xas, folio, folio_order(folio), gfp);
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002716 if (xas_error(&xas)) {
2717 ret = xas_error(&xas);
2718 goto out;
2719 }
2720
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002721 anon_vma = NULL;
2722 i_mmap_lock_read(mapping);
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002723
2724 /*
2725 *__split_huge_page() may need to trim off pages beyond EOF:
2726 * but on 32-bit, i_size_read() takes an irq-unsafe seqlock,
2727 * which cannot be nested inside the page tree lock. So note
2728 * end now: i_size itself may be changed at any moment, but
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002729 * folio lock is good enough to serialize the trimming.
Hugh Dickins006d3ff2018-11-30 14:10:21 -08002730 */
2731 end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE);
Hugh Dickinsd144bf62021-09-02 14:54:21 -07002732 if (shmem_mapping(mapping))
2733 end = shmem_fallocend(mapping->host, end);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002734 }
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002735
2736 /*
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002737 * Racy check if we can split the page, before unmap_folio() will
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002738 * split PMDs
2739 */
Matthew Wilcox (Oracle)d4b40842022-02-04 14:13:31 -05002740 if (!can_split_folio(folio, &extra_pins)) {
Baolin Wangfd4a7ac2022-10-24 16:34:22 +08002741 ret = -EAGAIN;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002742 goto out_unlock;
2743 }
2744
Matthew Wilcox (Oracle)684555a2022-09-02 20:46:49 +01002745 unmap_folio(folio);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002746
Alex Shib6769832020-12-15 12:33:33 -08002747 /* block interrupt reentry in xa_lock and spinlock */
2748 local_irq_disable();
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002749 if (mapping) {
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002750 /*
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002751 * Check if the folio is present in page cache.
2752 * We assume all tail are present too, if folio is there.
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002753 */
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002754 xas_lock(&xas);
2755 xas_reset(&xas);
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002756 if (xas_load(&xas) != folio)
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002757 goto fail;
2758 }
2759
Joonsoo Kim0139aa72016-05-19 17:10:49 -07002760 /* Prevent deferred_split_scan() touching ->_refcount */
Yang Shi364c1ee2019-09-23 15:38:06 -07002761 spin_lock(&ds_queue->split_queue_lock);
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002762 if (folio_ref_freeze(folio, 1 + extra_pins)) {
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002763 if (!list_empty(&folio->_deferred_list)) {
Yang Shi364c1ee2019-09-23 15:38:06 -07002764 ds_queue->split_queue_len--;
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002765 list_del(&folio->_deferred_list);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002766 }
Wei Yangafb97172020-01-30 22:14:35 -08002767 spin_unlock(&ds_queue->split_queue_lock);
Kirill A. Shutemov06d3eff2019-10-18 20:20:30 -07002768 if (mapping) {
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002769 int nr = folio_nr_pages(folio);
Muchun Songbf9ecea2021-02-24 12:03:27 -08002770
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002771 xas_split(&xas, folio, folio_order(folio));
2772 if (folio_test_swapbacked(folio)) {
2773 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS,
Muchun Song57b28472021-02-24 12:03:31 -08002774 -nr);
Marek Szyprowski1ca75542021-10-18 15:16:19 -07002775 } else {
Matthew Wilcox (Oracle)3e9a13d2022-09-02 20:46:48 +01002776 __lruvec_stat_mod_folio(folio, NR_FILE_THPS,
Muchun Songbf9ecea2021-02-24 12:03:27 -08002777 -nr);
Marek Szyprowski1ca75542021-10-18 15:16:19 -07002778 filemap_nr_thps_dec(mapping);
2779 }
Kirill A. Shutemov06d3eff2019-10-18 20:20:30 -07002780 }
2781
Alex Shib6769832020-12-15 12:33:33 -08002782 __split_huge_page(page, list, end);
Huang Yingc4f9c702020-10-15 20:06:07 -07002783 ret = 0;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002784 } else {
Yang Shi364c1ee2019-09-23 15:38:06 -07002785 spin_unlock(&ds_queue->split_queue_lock);
Yang Shi504e0702021-06-15 18:24:07 -07002786fail:
2787 if (mapping)
Matthew Wilcox (Oracle)6b24ca42020-06-27 22:19:08 -04002788 xas_unlock(&xas);
Alex Shib6769832020-12-15 12:33:33 -08002789 local_irq_enable();
Matthew Wilcox (Oracle)4eecb8b2022-01-28 23:32:59 -05002790 remap_page(folio, folio_nr_pages(folio));
Baolin Wangfd4a7ac2022-10-24 16:34:22 +08002791 ret = -EAGAIN;
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002792 }
2793
2794out_unlock:
Kirill A. Shutemovbaa355f2016-07-26 15:25:51 -07002795 if (anon_vma) {
2796 anon_vma_unlock_write(anon_vma);
2797 put_anon_vma(anon_vma);
2798 }
2799 if (mapping)
2800 i_mmap_unlock_read(mapping);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002801out:
Matthew Wilcox (Oracle)69a37a82022-06-08 15:18:34 -04002802 xas_destroy(&xas);
Kirill A. Shutemove9b61f12016-01-15 16:54:10 -08002803 count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED);
2804 return ret;
2805}
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002806
Matthew Wilcox (Oracle)8dc4a8f2023-08-16 16:11:52 +01002807void folio_undo_large_rmappable(struct folio *folio)
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002808{
Matthew Wilcox (Oracle)8dc4a8f2023-08-16 16:11:52 +01002809 struct deferred_split *ds_queue;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002810 unsigned long flags;
2811
Yin Fengweideedad82023-04-29 16:27:58 +08002812 /*
2813 * At this point, there is no one trying to add the folio to
2814 * deferred_list. If folio is not in deferred_list, it's safe
2815 * to check without acquiring the split_queue_lock.
2816 */
Matthew Wilcox (Oracle)8dc4a8f2023-08-16 16:11:52 +01002817 if (data_race(list_empty(&folio->_deferred_list)))
2818 return;
2819
2820 ds_queue = get_deferred_split_queue(folio);
2821 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2822 if (!list_empty(&folio->_deferred_list)) {
2823 ds_queue->split_queue_len--;
2824 list_del(&folio->_deferred_list);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002825 }
Matthew Wilcox (Oracle)8dc4a8f2023-08-16 16:11:52 +01002826 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002827}
2828
Matthew Wilcox (Oracle)f158ed62023-01-11 14:29:13 +00002829void deferred_split_folio(struct folio *folio)
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002830{
Matthew Wilcox (Oracle)f8baa6b2023-01-11 14:29:12 +00002831 struct deferred_split *ds_queue = get_deferred_split_queue(folio);
Yang Shi87eaceb2019-09-23 15:38:15 -07002832#ifdef CONFIG_MEMCG
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002833 struct mem_cgroup *memcg = folio_memcg(folio);
Yang Shi87eaceb2019-09-23 15:38:15 -07002834#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002835 unsigned long flags;
2836
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002837 VM_BUG_ON_FOLIO(folio_order(folio) < 2, folio);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002838
Yang Shi87eaceb2019-09-23 15:38:15 -07002839 /*
2840 * The try_to_unmap() in page reclaim path might reach here too,
2841 * this may cause a race condition to corrupt deferred split queue.
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002842 * And, if page reclaim is already handling the same folio, it is
Yang Shi87eaceb2019-09-23 15:38:15 -07002843 * unnecessary to handle it again in shrinker.
2844 *
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002845 * Check the swapcache flag to determine if the folio is being
2846 * handled by page reclaim since THP swap would add the folio into
Yang Shi87eaceb2019-09-23 15:38:15 -07002847 * swap cache before calling try_to_unmap().
2848 */
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002849 if (folio_test_swapcache(folio))
Yang Shi87eaceb2019-09-23 15:38:15 -07002850 return;
2851
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002852 if (!list_empty(&folio->_deferred_list))
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002853 return;
2854
Yang Shi364c1ee2019-09-23 15:38:06 -07002855 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002856 if (list_empty(&folio->_deferred_list)) {
Kirill A. Shutemovf9719a02016-03-17 14:18:45 -07002857 count_vm_event(THP_DEFERRED_SPLIT_PAGE);
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002858 list_add_tail(&folio->_deferred_list, &ds_queue->split_queue);
Yang Shi364c1ee2019-09-23 15:38:06 -07002859 ds_queue->split_queue_len++;
Yang Shi87eaceb2019-09-23 15:38:15 -07002860#ifdef CONFIG_MEMCG
2861 if (memcg)
Matthew Wilcox (Oracle)8991de92023-01-11 14:29:11 +00002862 set_shrinker_bit(memcg, folio_nid(folio),
Qi Zheng54d91722023-09-11 17:44:16 +08002863 deferred_split_shrinker->id);
Yang Shi87eaceb2019-09-23 15:38:15 -07002864#endif
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002865 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002866 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002867}
2868
2869static unsigned long deferred_split_count(struct shrinker *shrink,
2870 struct shrink_control *sc)
2871{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08002872 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Yang Shi364c1ee2019-09-23 15:38:06 -07002873 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
Yang Shi87eaceb2019-09-23 15:38:15 -07002874
2875#ifdef CONFIG_MEMCG
2876 if (sc->memcg)
2877 ds_queue = &sc->memcg->deferred_split_queue;
2878#endif
Yang Shi364c1ee2019-09-23 15:38:06 -07002879 return READ_ONCE(ds_queue->split_queue_len);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002880}
2881
2882static unsigned long deferred_split_scan(struct shrinker *shrink,
2883 struct shrink_control *sc)
2884{
Kirill A. Shutemova3d0a9182016-02-02 16:57:08 -08002885 struct pglist_data *pgdata = NODE_DATA(sc->nid);
Yang Shi364c1ee2019-09-23 15:38:06 -07002886 struct deferred_split *ds_queue = &pgdata->deferred_split_queue;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002887 unsigned long flags;
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002888 LIST_HEAD(list);
2889 struct folio *folio, *next;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002890 int split = 0;
2891
Yang Shi87eaceb2019-09-23 15:38:15 -07002892#ifdef CONFIG_MEMCG
2893 if (sc->memcg)
2894 ds_queue = &sc->memcg->deferred_split_queue;
2895#endif
2896
Yang Shi364c1ee2019-09-23 15:38:06 -07002897 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002898 /* Take pin on all head pages to avoid freeing them under us */
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002899 list_for_each_entry_safe(folio, next, &ds_queue->split_queue,
2900 _deferred_list) {
2901 if (folio_try_get(folio)) {
2902 list_move(&folio->_deferred_list, &list);
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08002903 } else {
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002904 /* We lost race with folio_put() */
2905 list_del_init(&folio->_deferred_list);
Yang Shi364c1ee2019-09-23 15:38:06 -07002906 ds_queue->split_queue_len--;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002907 }
Kirill A. Shutemove3ae1952016-02-02 16:57:15 -08002908 if (!--sc->nr_to_scan)
2909 break;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002910 }
Yang Shi364c1ee2019-09-23 15:38:06 -07002911 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002912
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002913 list_for_each_entry_safe(folio, next, &list, _deferred_list) {
2914 if (!folio_trylock(folio))
Kirill A. Shutemovfa41b902018-03-22 16:17:31 -07002915 goto next;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002916 /* split_huge_page() removes page from list on success */
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002917 if (!split_folio(folio))
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002918 split++;
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002919 folio_unlock(folio);
Kirill A. Shutemovfa41b902018-03-22 16:17:31 -07002920next:
Matthew Wilcox (Oracle)4375a552023-01-11 14:29:10 +00002921 folio_put(folio);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002922 }
2923
Yang Shi364c1ee2019-09-23 15:38:06 -07002924 spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
2925 list_splice_tail(&list, &ds_queue->split_queue);
2926 spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags);
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002927
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08002928 /*
2929 * Stop shrinker if we didn't split any page, but the queue is empty.
2930 * This can happen if pages were freed under us.
2931 */
Yang Shi364c1ee2019-09-23 15:38:06 -07002932 if (!split && list_empty(&ds_queue->split_queue))
Kirill A. Shutemovcb8d68e2016-02-02 16:57:12 -08002933 return SHRINK_STOP;
2934 return split;
Kirill A. Shutemov9a982252016-01-15 16:54:17 -08002935}
2936
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002937#ifdef CONFIG_DEBUG_FS
Zi Yanfa6c0232021-05-04 18:34:23 -07002938static void split_huge_pages_all(void)
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002939{
2940 struct zone *zone;
2941 struct page *page;
Kefeng Wang630e7c52022-12-29 20:25:03 +08002942 struct folio *folio;
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002943 unsigned long pfn, max_zone_pfn;
2944 unsigned long total = 0, split = 0;
2945
Zi Yanfa6c0232021-05-04 18:34:23 -07002946 pr_debug("Split all THPs\n");
Miaohe Lina17206d2022-07-04 21:21:57 +08002947 for_each_zone(zone) {
2948 if (!managed_zone(zone))
2949 continue;
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002950 max_zone_pfn = zone_end_pfn(zone);
2951 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) {
Miaohe Lina17206d2022-07-04 21:21:57 +08002952 int nr_pages;
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002953
Naoya Horiguchi2b7aa912022-09-08 13:11:50 +09002954 page = pfn_to_online_page(pfn);
Kefeng Wang630e7c52022-12-29 20:25:03 +08002955 if (!page || PageTail(page))
2956 continue;
2957 folio = page_folio(page);
2958 if (!folio_try_get(folio))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002959 continue;
2960
Kefeng Wang630e7c52022-12-29 20:25:03 +08002961 if (unlikely(page_folio(page) != folio))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002962 goto next;
2963
Kefeng Wang630e7c52022-12-29 20:25:03 +08002964 if (zone != folio_zone(folio))
2965 goto next;
2966
2967 if (!folio_test_large(folio)
2968 || folio_test_hugetlb(folio)
2969 || !folio_test_lru(folio))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002970 goto next;
2971
2972 total++;
Kefeng Wang630e7c52022-12-29 20:25:03 +08002973 folio_lock(folio);
2974 nr_pages = folio_nr_pages(folio);
2975 if (!split_folio(folio))
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002976 split++;
Miaohe Lina17206d2022-07-04 21:21:57 +08002977 pfn += nr_pages - 1;
Kefeng Wang630e7c52022-12-29 20:25:03 +08002978 folio_unlock(folio);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002979next:
Kefeng Wang630e7c52022-12-29 20:25:03 +08002980 folio_put(folio);
Zi Yanfa6c0232021-05-04 18:34:23 -07002981 cond_resched();
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002982 }
2983 }
2984
Zi Yanfa6c0232021-05-04 18:34:23 -07002985 pr_debug("%lu of %lu THP split\n", split, total);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08002986}
Zi Yanfa6c0232021-05-04 18:34:23 -07002987
2988static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma)
2989{
2990 return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) ||
2991 is_vm_hugetlb_page(vma);
2992}
2993
2994static int split_huge_pages_pid(int pid, unsigned long vaddr_start,
2995 unsigned long vaddr_end)
2996{
2997 int ret = 0;
2998 struct task_struct *task;
2999 struct mm_struct *mm;
3000 unsigned long total = 0, split = 0;
3001 unsigned long addr;
3002
3003 vaddr_start &= PAGE_MASK;
3004 vaddr_end &= PAGE_MASK;
3005
3006 /* Find the task_struct from pid */
3007 rcu_read_lock();
3008 task = find_task_by_vpid(pid);
3009 if (!task) {
3010 rcu_read_unlock();
3011 ret = -ESRCH;
3012 goto out;
3013 }
3014 get_task_struct(task);
3015 rcu_read_unlock();
3016
3017 /* Find the mm_struct */
3018 mm = get_task_mm(task);
3019 put_task_struct(task);
3020
3021 if (!mm) {
3022 ret = -EINVAL;
3023 goto out;
3024 }
3025
3026 pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n",
3027 pid, vaddr_start, vaddr_end);
3028
3029 mmap_read_lock(mm);
3030 /*
3031 * always increase addr by PAGE_SIZE, since we could have a PTE page
3032 * table filled with PTE-mapped THPs, each of which is distinct.
3033 */
3034 for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) {
Miaohe Lin74ba2b32022-07-04 21:21:52 +08003035 struct vm_area_struct *vma = vma_lookup(mm, addr);
Zi Yanfa6c0232021-05-04 18:34:23 -07003036 struct page *page;
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003037 struct folio *folio;
Zi Yanfa6c0232021-05-04 18:34:23 -07003038
Miaohe Lin74ba2b32022-07-04 21:21:52 +08003039 if (!vma)
Zi Yanfa6c0232021-05-04 18:34:23 -07003040 break;
3041
3042 /* skip special VMA and hugetlb VMA */
3043 if (vma_not_suitable_for_thp_split(vma)) {
3044 addr = vma->vm_end;
3045 continue;
3046 }
3047
3048 /* FOLL_DUMP to ignore special (like zero) pages */
Miaohe Lin87d27622022-03-22 14:45:29 -07003049 page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP);
Zi Yanfa6c0232021-05-04 18:34:23 -07003050
Haiyue Wangf7091ed2022-08-23 21:58:41 +08003051 if (IS_ERR_OR_NULL(page))
Zi Yanfa6c0232021-05-04 18:34:23 -07003052 continue;
3053
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003054 folio = page_folio(page);
3055 if (!is_transparent_hugepage(folio))
Zi Yanfa6c0232021-05-04 18:34:23 -07003056 goto next;
3057
3058 total++;
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003059 if (!can_split_folio(folio, NULL))
Zi Yanfa6c0232021-05-04 18:34:23 -07003060 goto next;
3061
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003062 if (!folio_trylock(folio))
Zi Yanfa6c0232021-05-04 18:34:23 -07003063 goto next;
3064
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003065 if (!split_folio(folio))
Zi Yanfa6c0232021-05-04 18:34:23 -07003066 split++;
3067
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003068 folio_unlock(folio);
Zi Yanfa6c0232021-05-04 18:34:23 -07003069next:
Matthew Wilcox (Oracle)a644b0a2023-08-16 16:12:01 +01003070 folio_put(folio);
Zi Yanfa6c0232021-05-04 18:34:23 -07003071 cond_resched();
3072 }
3073 mmap_read_unlock(mm);
3074 mmput(mm);
3075
3076 pr_debug("%lu of %lu THP split\n", split, total);
3077
3078out:
3079 return ret;
3080}
3081
Zi Yanfbe37502021-05-04 18:34:26 -07003082static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start,
3083 pgoff_t off_end)
3084{
3085 struct filename *file;
3086 struct file *candidate;
3087 struct address_space *mapping;
3088 int ret = -EINVAL;
3089 pgoff_t index;
3090 int nr_pages = 1;
3091 unsigned long total = 0, split = 0;
3092
3093 file = getname_kernel(file_path);
3094 if (IS_ERR(file))
3095 return ret;
3096
3097 candidate = file_open_name(file, O_RDONLY, 0);
3098 if (IS_ERR(candidate))
3099 goto out;
3100
3101 pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n",
3102 file_path, off_start, off_end);
3103
3104 mapping = candidate->f_mapping;
3105
3106 for (index = off_start; index < off_end; index += nr_pages) {
Christoph Hellwig1fb130b2023-03-07 15:34:04 +01003107 struct folio *folio = filemap_get_folio(mapping, index);
Zi Yanfbe37502021-05-04 18:34:26 -07003108
3109 nr_pages = 1;
Christoph Hellwig66dabbb2023-03-07 15:34:10 +01003110 if (IS_ERR(folio))
Zi Yanfbe37502021-05-04 18:34:26 -07003111 continue;
3112
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003113 if (!folio_test_large(folio))
Zi Yanfbe37502021-05-04 18:34:26 -07003114 goto next;
3115
3116 total++;
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003117 nr_pages = folio_nr_pages(folio);
Zi Yanfbe37502021-05-04 18:34:26 -07003118
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003119 if (!folio_trylock(folio))
Zi Yanfbe37502021-05-04 18:34:26 -07003120 goto next;
3121
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003122 if (!split_folio(folio))
Zi Yanfbe37502021-05-04 18:34:26 -07003123 split++;
3124
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003125 folio_unlock(folio);
Zi Yanfbe37502021-05-04 18:34:26 -07003126next:
Matthew Wilcox (Oracle)9ee2c082022-10-19 19:33:29 +01003127 folio_put(folio);
Zi Yanfbe37502021-05-04 18:34:26 -07003128 cond_resched();
3129 }
3130
3131 filp_close(candidate, NULL);
3132 ret = 0;
3133
3134 pr_debug("%lu of %lu file-backed THP split\n", split, total);
3135out:
3136 putname(file);
3137 return ret;
3138}
3139
Zi Yanfa6c0232021-05-04 18:34:23 -07003140#define MAX_INPUT_BUF_SZ 255
3141
3142static ssize_t split_huge_pages_write(struct file *file, const char __user *buf,
3143 size_t count, loff_t *ppops)
3144{
3145 static DEFINE_MUTEX(split_debug_mutex);
3146 ssize_t ret;
Zi Yanfbe37502021-05-04 18:34:26 -07003147 /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */
3148 char input_buf[MAX_INPUT_BUF_SZ];
Zi Yanfa6c0232021-05-04 18:34:23 -07003149 int pid;
3150 unsigned long vaddr_start, vaddr_end;
3151
3152 ret = mutex_lock_interruptible(&split_debug_mutex);
3153 if (ret)
3154 return ret;
3155
3156 ret = -EFAULT;
3157
3158 memset(input_buf, 0, MAX_INPUT_BUF_SZ);
3159 if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ)))
3160 goto out;
3161
3162 input_buf[MAX_INPUT_BUF_SZ - 1] = '\0';
Zi Yanfbe37502021-05-04 18:34:26 -07003163
3164 if (input_buf[0] == '/') {
3165 char *tok;
3166 char *buf = input_buf;
3167 char file_path[MAX_INPUT_BUF_SZ];
3168 pgoff_t off_start = 0, off_end = 0;
3169 size_t input_len = strlen(input_buf);
3170
3171 tok = strsep(&buf, ",");
3172 if (tok) {
Matthew Wilcox (Oracle)1212e002021-06-30 18:52:11 -07003173 strcpy(file_path, tok);
Zi Yanfbe37502021-05-04 18:34:26 -07003174 } else {
3175 ret = -EINVAL;
3176 goto out;
3177 }
3178
3179 ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end);
3180 if (ret != 2) {
3181 ret = -EINVAL;
3182 goto out;
3183 }
3184 ret = split_huge_pages_in_file(file_path, off_start, off_end);
3185 if (!ret)
3186 ret = input_len;
3187
3188 goto out;
3189 }
3190
Zi Yanfa6c0232021-05-04 18:34:23 -07003191 ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end);
3192 if (ret == 1 && pid == 1) {
3193 split_huge_pages_all();
3194 ret = strlen(input_buf);
3195 goto out;
3196 } else if (ret != 3) {
3197 ret = -EINVAL;
3198 goto out;
3199 }
3200
3201 ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end);
3202 if (!ret)
3203 ret = strlen(input_buf);
3204out:
3205 mutex_unlock(&split_debug_mutex);
3206 return ret;
3207
3208}
3209
3210static const struct file_operations split_huge_pages_fops = {
3211 .owner = THIS_MODULE,
3212 .write = split_huge_pages_write,
3213 .llseek = no_llseek,
3214};
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003215
3216static int __init split_huge_pages_debugfs(void)
3217{
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -08003218 debugfs_create_file("split_huge_pages", 0200, NULL, NULL,
3219 &split_huge_pages_fops);
Kirill A. Shutemov49071d42016-01-15 16:54:40 -08003220 return 0;
3221}
3222late_initcall(split_huge_pages_debugfs);
3223#endif
Zi Yan616b8372017-09-08 16:10:57 -07003224
3225#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
David Hildenbrand7f5abe62022-05-09 18:20:44 -07003226int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,
Zi Yan616b8372017-09-08 16:10:57 -07003227 struct page *page)
3228{
3229 struct vm_area_struct *vma = pvmw->vma;
3230 struct mm_struct *mm = vma->vm_mm;
3231 unsigned long address = pvmw->address;
David Hildenbrand6c287602022-05-09 18:20:44 -07003232 bool anon_exclusive;
Zi Yan616b8372017-09-08 16:10:57 -07003233 pmd_t pmdval;
3234 swp_entry_t entry;
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003235 pmd_t pmdswp;
Zi Yan616b8372017-09-08 16:10:57 -07003236
3237 if (!(pvmw->pmd && !pvmw->pte))
David Hildenbrand7f5abe62022-05-09 18:20:44 -07003238 return 0;
Zi Yan616b8372017-09-08 16:10:57 -07003239
Zi Yan616b8372017-09-08 16:10:57 -07003240 flush_cache_range(vma, address, address + HPAGE_PMD_SIZE);
Huang Ying8a8683a2020-03-05 22:28:29 -08003241 pmdval = pmdp_invalidate(vma, address, pvmw->pmd);
David Hildenbrand6c287602022-05-09 18:20:44 -07003242
David Hildenbrand088b8aa2022-09-01 10:35:59 +02003243 /* See page_try_share_anon_rmap(): invalidate PMD first. */
David Hildenbrand6c287602022-05-09 18:20:44 -07003244 anon_exclusive = PageAnon(page) && PageAnonExclusive(page);
3245 if (anon_exclusive && page_try_share_anon_rmap(page)) {
3246 set_pmd_at(mm, address, pvmw->pmd, pmdval);
David Hildenbrand7f5abe62022-05-09 18:20:44 -07003247 return -EBUSY;
David Hildenbrand6c287602022-05-09 18:20:44 -07003248 }
3249
Zi Yan616b8372017-09-08 16:10:57 -07003250 if (pmd_dirty(pmdval))
3251 set_page_dirty(page);
Alistair Popple4dd845b2021-06-30 18:54:09 -07003252 if (pmd_write(pmdval))
3253 entry = make_writable_migration_entry(page_to_pfn(page));
David Hildenbrand6c287602022-05-09 18:20:44 -07003254 else if (anon_exclusive)
3255 entry = make_readable_exclusive_migration_entry(page_to_pfn(page));
Alistair Popple4dd845b2021-06-30 18:54:09 -07003256 else
3257 entry = make_readable_migration_entry(page_to_pfn(page));
Peter Xu2e346872022-08-11 12:13:29 -04003258 if (pmd_young(pmdval))
3259 entry = make_migration_entry_young(entry);
3260 if (pmd_dirty(pmdval))
3261 entry = make_migration_entry_dirty(entry);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003262 pmdswp = swp_entry_to_pmd(entry);
3263 if (pmd_soft_dirty(pmdval))
3264 pmdswp = pmd_swp_mksoft_dirty(pmdswp);
David Hildenbrand24bf08c2023-04-05 18:02:35 +02003265 if (pmd_uffd_wp(pmdval))
3266 pmdswp = pmd_swp_mkuffd_wp(pmdswp);
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003267 set_pmd_at(mm, address, pvmw->pmd, pmdswp);
Hugh Dickinscea86fe2022-02-14 18:26:39 -08003268 page_remove_rmap(page, vma, true);
Zi Yan616b8372017-09-08 16:10:57 -07003269 put_page(page);
Anshuman Khandual283fd6f2022-03-24 18:09:58 -07003270 trace_set_migration_pmd(address, pmd_val(pmdswp));
David Hildenbrand7f5abe62022-05-09 18:20:44 -07003271
3272 return 0;
Zi Yan616b8372017-09-08 16:10:57 -07003273}
3274
3275void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new)
3276{
3277 struct vm_area_struct *vma = pvmw->vma;
3278 struct mm_struct *mm = vma->vm_mm;
3279 unsigned long address = pvmw->address;
Miaohe Lin4fba8f22022-07-04 21:21:51 +08003280 unsigned long haddr = address & HPAGE_PMD_MASK;
Zi Yan616b8372017-09-08 16:10:57 -07003281 pmd_t pmde;
3282 swp_entry_t entry;
3283
3284 if (!(pvmw->pmd && !pvmw->pte))
3285 return;
3286
3287 entry = pmd_to_swp_entry(*pvmw->pmd);
3288 get_page(new);
Peter Xu2e346872022-08-11 12:13:29 -04003289 pmde = mk_huge_pmd(new, READ_ONCE(vma->vm_page_prot));
Naoya Horiguchiab6e3d02017-09-08 16:11:04 -07003290 if (pmd_swp_soft_dirty(*pvmw->pmd))
3291 pmde = pmd_mksoft_dirty(pmde);
David Hildenbrand3c811f72023-04-11 16:25:10 +02003292 if (is_writable_migration_entry(entry))
Rick Edgecombe161e3932023-06-12 17:10:29 -07003293 pmde = pmd_mkwrite(pmde, vma);
Peter Xu8f34f1e2021-06-30 18:49:02 -07003294 if (pmd_swp_uffd_wp(*pvmw->pmd))
Peter Xuf1eb1ba2022-12-14 15:15:33 -05003295 pmde = pmd_mkuffd_wp(pmde);
Peter Xu2e346872022-08-11 12:13:29 -04003296 if (!is_migration_entry_young(entry))
3297 pmde = pmd_mkold(pmde);
3298 /* NOTE: this may contain setting soft-dirty on some archs */
3299 if (PageDirty(new) && is_migration_entry_dirty(entry))
3300 pmde = pmd_mkdirty(pmde);
Zi Yan616b8372017-09-08 16:10:57 -07003301
David Hildenbrand6c287602022-05-09 18:20:44 -07003302 if (PageAnon(new)) {
3303 rmap_t rmap_flags = RMAP_COMPOUND;
3304
3305 if (!is_readable_migration_entry(entry))
3306 rmap_flags |= RMAP_EXCLUSIVE;
3307
Miaohe Lin4fba8f22022-07-04 21:21:51 +08003308 page_add_anon_rmap(new, vma, haddr, rmap_flags);
David Hildenbrand6c287602022-05-09 18:20:44 -07003309 } else {
Hugh Dickinscea86fe2022-02-14 18:26:39 -08003310 page_add_file_rmap(new, vma, true);
David Hildenbrand6c287602022-05-09 18:20:44 -07003311 }
3312 VM_BUG_ON(pmd_write(pmde) && PageAnon(new) && !PageAnonExclusive(new));
Miaohe Lin4fba8f22022-07-04 21:21:51 +08003313 set_pmd_at(mm, haddr, pvmw->pmd, pmde);
Muchun Song5cbcf222022-03-22 14:41:53 -07003314
3315 /* No need to invalidate - it was non-present before */
Zi Yan616b8372017-09-08 16:10:57 -07003316 update_mmu_cache_pmd(vma, address, pvmw->pmd);
Anshuman Khandual283fd6f2022-03-24 18:09:58 -07003317 trace_remove_migration_pmd(address, pmd_val(pmde));
Zi Yan616b8372017-09-08 16:10:57 -07003318}
3319#endif