Commit | Line | Data |
---|---|---|
20c8ccb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
71e3aac0 AA |
2 | /* |
3 | * Copyright (C) 2009 Red Hat, Inc. | |
71e3aac0 AA |
4 | */ |
5 | ||
ae3a8c1c AM |
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
7 | ||
71e3aac0 AA |
8 | #include <linux/mm.h> |
9 | #include <linux/sched.h> | |
fa6c0231 | 10 | #include <linux/sched/mm.h> |
f7ccbae4 | 11 | #include <linux/sched/coredump.h> |
6a3827d7 | 12 | #include <linux/sched/numa_balancing.h> |
71e3aac0 AA |
13 | #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> | |
97ae1749 | 18 | #include <linux/shrinker.h> |
ba76149f | 19 | #include <linux/mm_inline.h> |
e9b61f19 | 20 | #include <linux/swapops.h> |
4897c765 | 21 | #include <linux/dax.h> |
ba76149f | 22 | #include <linux/khugepaged.h> |
878aee7d | 23 | #include <linux/freezer.h> |
f25748e3 | 24 | #include <linux/pfn_t.h> |
a664b2d8 | 25 | #include <linux/mman.h> |
3565fce3 | 26 | #include <linux/memremap.h> |
325adeb5 | 27 | #include <linux/pagemap.h> |
49071d43 | 28 | #include <linux/debugfs.h> |
4daae3b4 | 29 | #include <linux/migrate.h> |
43b5fbbd | 30 | #include <linux/hashtable.h> |
6b251fc9 | 31 | #include <linux/userfaultfd_k.h> |
33c3fc71 | 32 | #include <linux/page_idle.h> |
baa355fd | 33 | #include <linux/shmem_fs.h> |
6b31d595 | 34 | #include <linux/oom.h> |
98fa15f3 | 35 | #include <linux/numa.h> |
f7da677b | 36 | #include <linux/page_owner.h> |
a1a3a2fc | 37 | #include <linux/sched/sysctl.h> |
97ae1749 | 38 | |
71e3aac0 AA |
39 | #include <asm/tlb.h> |
40 | #include <asm/pgalloc.h> | |
41 | #include "internal.h" | |
014bb1de | 42 | #include "swap.h" |
71e3aac0 | 43 | |
283fd6fe AK |
44 | #define CREATE_TRACE_POINTS |
45 | #include <trace/events/thp.h> | |
46 | ||
ba76149f | 47 | /* |
b14d595a MD |
48 | * By default, transparent hugepage support is disabled in order to avoid |
49 | * risking an increased memory footprint for applications that are not | |
50 | * guaranteed to benefit from it. When transparent hugepage support is | |
51 | * enabled, it is for all mappings, and khugepaged scans all mappings. | |
8bfa3f9a JW |
52 | * Defrag is invoked by khugepaged hugepage allocations and by page faults |
53 | * for all hugepage allocations. | |
ba76149f | 54 | */ |
71e3aac0 | 55 | unsigned long transparent_hugepage_flags __read_mostly = |
13ece886 | 56 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS |
ba76149f | 57 | (1<<TRANSPARENT_HUGEPAGE_FLAG)| |
13ece886 AA |
58 | #endif |
59 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE | |
60 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)| | |
61 | #endif | |
444eb2a4 | 62 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)| |
79da5407 KS |
63 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)| |
64 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); | |
ba76149f | 65 | |
9a982250 | 66 | static struct shrinker deferred_split_shrinker; |
f000565a | 67 | |
97ae1749 | 68 | static atomic_t huge_zero_refcount; |
56873f43 | 69 | struct page *huge_zero_page __read_mostly; |
3b77e8c8 | 70 | unsigned long huge_zero_pfn __read_mostly = ~0UL; |
4a6c1297 | 71 | |
9fec5168 YS |
72 | bool hugepage_vma_check(struct vm_area_struct *vma, |
73 | unsigned long vm_flags, | |
74 | bool smaps) | |
7635d9cb | 75 | { |
9fec5168 YS |
76 | if (!vma->vm_mm) /* vdso */ |
77 | return false; | |
78 | ||
79 | if (!transhuge_vma_enabled(vma, vm_flags)) | |
80 | return false; | |
c0630669 | 81 | |
9fec5168 | 82 | if (vm_flags & VM_NO_KHUGEPAGED) |
c0630669 | 83 | return false; |
9fec5168 YS |
84 | |
85 | /* Don't run khugepaged against DAX vma */ | |
86 | if (vma_is_dax(vma)) | |
87 | return false; | |
88 | ||
89 | /* Check alignment for file vma and size for both file and anon vma */ | |
90 | if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE))) | |
91 | return false; | |
92 | ||
93 | /* Enabled via shmem mount options or sysfs settings. */ | |
94 | if (shmem_file(vma->vm_file)) | |
c0630669 | 95 | return shmem_huge_enabled(vma); |
9fec5168 YS |
96 | |
97 | if (!khugepaged_enabled()) | |
98 | return false; | |
99 | ||
100 | /* THP settings require madvise. */ | |
101 | if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) | |
102 | return false; | |
103 | ||
104 | /* Only regular file is valid */ | |
105 | if (file_thp_enabled(vma)) | |
78d12c19 | 106 | return true; |
7635d9cb | 107 | |
9fec5168 YS |
108 | if (!vma_is_anonymous(vma)) |
109 | return false; | |
110 | ||
111 | if (vma_is_temporary_stack(vma)) | |
112 | return false; | |
113 | ||
114 | /* | |
115 | * THPeligible bit of smaps should show 1 for proper VMAs even | |
116 | * though anon_vma is not initialized yet. | |
117 | */ | |
118 | if (!vma->anon_vma) | |
119 | return smaps; | |
120 | ||
121 | return true; | |
7635d9cb MH |
122 | } |
123 | ||
aaa9705b | 124 | static bool get_huge_zero_page(void) |
97ae1749 KS |
125 | { |
126 | struct page *zero_page; | |
127 | retry: | |
128 | if (likely(atomic_inc_not_zero(&huge_zero_refcount))) | |
aaa9705b | 129 | return true; |
97ae1749 KS |
130 | |
131 | zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE, | |
4a6c1297 | 132 | HPAGE_PMD_ORDER); |
d8a8e1f0 KS |
133 | if (!zero_page) { |
134 | count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED); | |
aaa9705b | 135 | return false; |
d8a8e1f0 KS |
136 | } |
137 | count_vm_event(THP_ZERO_PAGE_ALLOC); | |
97ae1749 | 138 | preempt_disable(); |
5918d10a | 139 | if (cmpxchg(&huge_zero_page, NULL, zero_page)) { |
97ae1749 | 140 | preempt_enable(); |
5ddacbe9 | 141 | __free_pages(zero_page, compound_order(zero_page)); |
97ae1749 KS |
142 | goto retry; |
143 | } | |
3b77e8c8 | 144 | WRITE_ONCE(huge_zero_pfn, page_to_pfn(zero_page)); |
97ae1749 KS |
145 | |
146 | /* We take additional reference here. It will be put back by shrinker */ | |
147 | atomic_set(&huge_zero_refcount, 2); | |
148 | preempt_enable(); | |
aaa9705b | 149 | return true; |
4a6c1297 KS |
150 | } |
151 | ||
6fcb52a5 | 152 | static void put_huge_zero_page(void) |
4a6c1297 | 153 | { |
97ae1749 KS |
154 | /* |
155 | * Counter should never go to zero here. Only shrinker can put | |
156 | * last reference. | |
157 | */ | |
158 | BUG_ON(atomic_dec_and_test(&huge_zero_refcount)); | |
4a6c1297 KS |
159 | } |
160 | ||
6fcb52a5 AL |
161 | struct page *mm_get_huge_zero_page(struct mm_struct *mm) |
162 | { | |
163 | if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags)) | |
164 | return READ_ONCE(huge_zero_page); | |
165 | ||
166 | if (!get_huge_zero_page()) | |
167 | return NULL; | |
168 | ||
169 | if (test_and_set_bit(MMF_HUGE_ZERO_PAGE, &mm->flags)) | |
170 | put_huge_zero_page(); | |
171 | ||
172 | return READ_ONCE(huge_zero_page); | |
173 | } | |
174 | ||
175 | void mm_put_huge_zero_page(struct mm_struct *mm) | |
176 | { | |
177 | if (test_bit(MMF_HUGE_ZERO_PAGE, &mm->flags)) | |
178 | put_huge_zero_page(); | |
179 | } | |
180 | ||
48896466 GC |
181 | static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink, |
182 | struct shrink_control *sc) | |
4a6c1297 | 183 | { |
48896466 GC |
184 | /* we can free zero page only if last reference remains */ |
185 | return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0; | |
186 | } | |
97ae1749 | 187 | |
48896466 GC |
188 | static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, |
189 | struct shrink_control *sc) | |
190 | { | |
97ae1749 | 191 | if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) { |
5918d10a KS |
192 | struct page *zero_page = xchg(&huge_zero_page, NULL); |
193 | BUG_ON(zero_page == NULL); | |
3b77e8c8 | 194 | WRITE_ONCE(huge_zero_pfn, ~0UL); |
5ddacbe9 | 195 | __free_pages(zero_page, compound_order(zero_page)); |
48896466 | 196 | return HPAGE_PMD_NR; |
97ae1749 KS |
197 | } |
198 | ||
199 | return 0; | |
4a6c1297 KS |
200 | } |
201 | ||
97ae1749 | 202 | static struct shrinker huge_zero_page_shrinker = { |
48896466 GC |
203 | .count_objects = shrink_huge_zero_page_count, |
204 | .scan_objects = shrink_huge_zero_page_scan, | |
97ae1749 KS |
205 | .seeks = DEFAULT_SEEKS, |
206 | }; | |
207 | ||
71e3aac0 | 208 | #ifdef CONFIG_SYSFS |
71e3aac0 AA |
209 | static ssize_t enabled_show(struct kobject *kobj, |
210 | struct kobj_attribute *attr, char *buf) | |
211 | { | |
bfb0ffeb JP |
212 | const char *output; |
213 | ||
444eb2a4 | 214 | if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags)) |
bfb0ffeb JP |
215 | output = "[always] madvise never"; |
216 | else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
217 | &transparent_hugepage_flags)) | |
218 | output = "always [madvise] never"; | |
444eb2a4 | 219 | else |
bfb0ffeb JP |
220 | output = "always madvise [never]"; |
221 | ||
222 | return sysfs_emit(buf, "%s\n", output); | |
71e3aac0 | 223 | } |
444eb2a4 | 224 | |
71e3aac0 AA |
225 | static ssize_t enabled_store(struct kobject *kobj, |
226 | struct kobj_attribute *attr, | |
227 | const char *buf, size_t count) | |
228 | { | |
21440d7e | 229 | ssize_t ret = count; |
ba76149f | 230 | |
f42f2552 | 231 | if (sysfs_streq(buf, "always")) { |
21440d7e DR |
232 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); |
233 | set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 234 | } else if (sysfs_streq(buf, "madvise")) { |
21440d7e DR |
235 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); |
236 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 237 | } else if (sysfs_streq(buf, "never")) { |
21440d7e DR |
238 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); |
239 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
240 | } else | |
241 | ret = -EINVAL; | |
ba76149f AA |
242 | |
243 | if (ret > 0) { | |
b46e756f | 244 | int err = start_stop_khugepaged(); |
ba76149f AA |
245 | if (err) |
246 | ret = err; | |
247 | } | |
ba76149f | 248 | return ret; |
71e3aac0 AA |
249 | } |
250 | static struct kobj_attribute enabled_attr = | |
251 | __ATTR(enabled, 0644, enabled_show, enabled_store); | |
252 | ||
b46e756f | 253 | ssize_t single_hugepage_flag_show(struct kobject *kobj, |
bfb0ffeb JP |
254 | struct kobj_attribute *attr, char *buf, |
255 | enum transparent_hugepage_flag flag) | |
71e3aac0 | 256 | { |
bfb0ffeb JP |
257 | return sysfs_emit(buf, "%d\n", |
258 | !!test_bit(flag, &transparent_hugepage_flags)); | |
71e3aac0 | 259 | } |
e27e6151 | 260 | |
b46e756f | 261 | ssize_t single_hugepage_flag_store(struct kobject *kobj, |
71e3aac0 AA |
262 | struct kobj_attribute *attr, |
263 | const char *buf, size_t count, | |
264 | enum transparent_hugepage_flag flag) | |
265 | { | |
e27e6151 BH |
266 | unsigned long value; |
267 | int ret; | |
268 | ||
269 | ret = kstrtoul(buf, 10, &value); | |
270 | if (ret < 0) | |
271 | return ret; | |
272 | if (value > 1) | |
273 | return -EINVAL; | |
274 | ||
275 | if (value) | |
71e3aac0 | 276 | set_bit(flag, &transparent_hugepage_flags); |
e27e6151 | 277 | else |
71e3aac0 | 278 | clear_bit(flag, &transparent_hugepage_flags); |
71e3aac0 AA |
279 | |
280 | return count; | |
281 | } | |
282 | ||
71e3aac0 AA |
283 | static ssize_t defrag_show(struct kobject *kobj, |
284 | struct kobj_attribute *attr, char *buf) | |
285 | { | |
bfb0ffeb JP |
286 | const char *output; |
287 | ||
288 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, | |
289 | &transparent_hugepage_flags)) | |
290 | output = "[always] defer defer+madvise madvise never"; | |
291 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, | |
292 | &transparent_hugepage_flags)) | |
293 | output = "always [defer] defer+madvise madvise never"; | |
294 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, | |
295 | &transparent_hugepage_flags)) | |
296 | output = "always defer [defer+madvise] madvise never"; | |
297 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, | |
298 | &transparent_hugepage_flags)) | |
299 | output = "always defer defer+madvise [madvise] never"; | |
300 | else | |
301 | output = "always defer defer+madvise madvise [never]"; | |
302 | ||
303 | return sysfs_emit(buf, "%s\n", output); | |
71e3aac0 | 304 | } |
21440d7e | 305 | |
71e3aac0 AA |
306 | static ssize_t defrag_store(struct kobject *kobj, |
307 | struct kobj_attribute *attr, | |
308 | const char *buf, size_t count) | |
309 | { | |
f42f2552 | 310 | if (sysfs_streq(buf, "always")) { |
21440d7e DR |
311 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); |
312 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); | |
313 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
314 | set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 315 | } else if (sysfs_streq(buf, "defer+madvise")) { |
21440d7e DR |
316 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); |
317 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); | |
318 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
319 | set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 320 | } else if (sysfs_streq(buf, "defer")) { |
4fad7fb6 DR |
321 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); |
322 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); | |
323 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
324 | set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 325 | } else if (sysfs_streq(buf, "madvise")) { |
21440d7e DR |
326 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); |
327 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); | |
328 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); | |
329 | set_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
f42f2552 | 330 | } else if (sysfs_streq(buf, "never")) { |
21440d7e DR |
331 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags); |
332 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags); | |
333 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags); | |
334 | clear_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags); | |
335 | } else | |
336 | return -EINVAL; | |
337 | ||
338 | return count; | |
71e3aac0 AA |
339 | } |
340 | static struct kobj_attribute defrag_attr = | |
341 | __ATTR(defrag, 0644, defrag_show, defrag_store); | |
342 | ||
79da5407 | 343 | static ssize_t use_zero_page_show(struct kobject *kobj, |
ae7a927d | 344 | struct kobj_attribute *attr, char *buf) |
79da5407 | 345 | { |
b46e756f | 346 | return single_hugepage_flag_show(kobj, attr, buf, |
ae7a927d | 347 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
79da5407 KS |
348 | } |
349 | static ssize_t use_zero_page_store(struct kobject *kobj, | |
350 | struct kobj_attribute *attr, const char *buf, size_t count) | |
351 | { | |
b46e756f | 352 | return single_hugepage_flag_store(kobj, attr, buf, count, |
79da5407 KS |
353 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); |
354 | } | |
355 | static struct kobj_attribute use_zero_page_attr = | |
356 | __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store); | |
49920d28 HD |
357 | |
358 | static ssize_t hpage_pmd_size_show(struct kobject *kobj, | |
ae7a927d | 359 | struct kobj_attribute *attr, char *buf) |
49920d28 | 360 | { |
ae7a927d | 361 | return sysfs_emit(buf, "%lu\n", HPAGE_PMD_SIZE); |
49920d28 HD |
362 | } |
363 | static struct kobj_attribute hpage_pmd_size_attr = | |
364 | __ATTR_RO(hpage_pmd_size); | |
365 | ||
71e3aac0 AA |
366 | static struct attribute *hugepage_attr[] = { |
367 | &enabled_attr.attr, | |
368 | &defrag_attr.attr, | |
79da5407 | 369 | &use_zero_page_attr.attr, |
49920d28 | 370 | &hpage_pmd_size_attr.attr, |
396bcc52 | 371 | #ifdef CONFIG_SHMEM |
5a6e75f8 | 372 | &shmem_enabled_attr.attr, |
71e3aac0 AA |
373 | #endif |
374 | NULL, | |
375 | }; | |
376 | ||
8aa95a21 | 377 | static const struct attribute_group hugepage_attr_group = { |
71e3aac0 | 378 | .attrs = hugepage_attr, |
ba76149f AA |
379 | }; |
380 | ||
569e5590 | 381 | static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) |
71e3aac0 | 382 | { |
71e3aac0 AA |
383 | int err; |
384 | ||
569e5590 SL |
385 | *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); |
386 | if (unlikely(!*hugepage_kobj)) { | |
ae3a8c1c | 387 | pr_err("failed to create transparent hugepage kobject\n"); |
569e5590 | 388 | return -ENOMEM; |
ba76149f AA |
389 | } |
390 | ||
569e5590 | 391 | err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group); |
ba76149f | 392 | if (err) { |
ae3a8c1c | 393 | pr_err("failed to register transparent hugepage group\n"); |
569e5590 | 394 | goto delete_obj; |
ba76149f AA |
395 | } |
396 | ||
569e5590 | 397 | err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group); |
ba76149f | 398 | if (err) { |
ae3a8c1c | 399 | pr_err("failed to register transparent hugepage group\n"); |
569e5590 | 400 | goto remove_hp_group; |
ba76149f | 401 | } |
569e5590 SL |
402 | |
403 | return 0; | |
404 | ||
405 | remove_hp_group: | |
406 | sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group); | |
407 | delete_obj: | |
408 | kobject_put(*hugepage_kobj); | |
409 | return err; | |
410 | } | |
411 | ||
412 | static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj) | |
413 | { | |
414 | sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group); | |
415 | sysfs_remove_group(hugepage_kobj, &hugepage_attr_group); | |
416 | kobject_put(hugepage_kobj); | |
417 | } | |
418 | #else | |
419 | static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj) | |
420 | { | |
421 | return 0; | |
422 | } | |
423 | ||
424 | static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) | |
425 | { | |
426 | } | |
427 | #endif /* CONFIG_SYSFS */ | |
428 | ||
429 | static int __init hugepage_init(void) | |
430 | { | |
431 | int err; | |
432 | struct kobject *hugepage_kobj; | |
433 | ||
434 | if (!has_transparent_hugepage()) { | |
bae84953 AK |
435 | /* |
436 | * Hardware doesn't support hugepages, hence disable | |
437 | * DAX PMD support. | |
438 | */ | |
439 | transparent_hugepage_flags = 1 << TRANSPARENT_HUGEPAGE_NEVER_DAX; | |
569e5590 SL |
440 | return -EINVAL; |
441 | } | |
442 | ||
ff20c2e0 KS |
443 | /* |
444 | * hugepages can't be allocated by the buddy allocator | |
445 | */ | |
446 | MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER); | |
447 | /* | |
448 | * we use page->mapping and page->index in second tail page | |
449 | * as list_head: assuming THP order >= 2 | |
450 | */ | |
451 | MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2); | |
452 | ||
569e5590 SL |
453 | err = hugepage_init_sysfs(&hugepage_kobj); |
454 | if (err) | |
65ebb64f | 455 | goto err_sysfs; |
ba76149f | 456 | |
b46e756f | 457 | err = khugepaged_init(); |
ba76149f | 458 | if (err) |
65ebb64f | 459 | goto err_slab; |
ba76149f | 460 | |
e33c267a | 461 | err = register_shrinker(&huge_zero_page_shrinker, "thp-zero"); |
65ebb64f KS |
462 | if (err) |
463 | goto err_hzp_shrinker; | |
e33c267a | 464 | err = register_shrinker(&deferred_split_shrinker, "thp-deferred_split"); |
9a982250 KS |
465 | if (err) |
466 | goto err_split_shrinker; | |
97ae1749 | 467 | |
97562cd2 RR |
468 | /* |
469 | * By default disable transparent hugepages on smaller systems, | |
470 | * where the extra memory used could hurt more than TLB overhead | |
471 | * is likely to save. The admin can still enable it through /sys. | |
472 | */ | |
ca79b0c2 | 473 | if (totalram_pages() < (512 << (20 - PAGE_SHIFT))) { |
97562cd2 | 474 | transparent_hugepage_flags = 0; |
79553da2 KS |
475 | return 0; |
476 | } | |
97562cd2 | 477 | |
79553da2 | 478 | err = start_stop_khugepaged(); |
65ebb64f KS |
479 | if (err) |
480 | goto err_khugepaged; | |
ba76149f | 481 | |
569e5590 | 482 | return 0; |
65ebb64f | 483 | err_khugepaged: |
9a982250 KS |
484 | unregister_shrinker(&deferred_split_shrinker); |
485 | err_split_shrinker: | |
65ebb64f KS |
486 | unregister_shrinker(&huge_zero_page_shrinker); |
487 | err_hzp_shrinker: | |
b46e756f | 488 | khugepaged_destroy(); |
65ebb64f | 489 | err_slab: |
569e5590 | 490 | hugepage_exit_sysfs(hugepage_kobj); |
65ebb64f | 491 | err_sysfs: |
ba76149f | 492 | return err; |
71e3aac0 | 493 | } |
a64fb3cd | 494 | subsys_initcall(hugepage_init); |
71e3aac0 AA |
495 | |
496 | static int __init setup_transparent_hugepage(char *str) | |
497 | { | |
498 | int ret = 0; | |
499 | if (!str) | |
500 | goto out; | |
501 | if (!strcmp(str, "always")) { | |
502 | set_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
503 | &transparent_hugepage_flags); | |
504 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
505 | &transparent_hugepage_flags); | |
506 | ret = 1; | |
507 | } else if (!strcmp(str, "madvise")) { | |
508 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
509 | &transparent_hugepage_flags); | |
510 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
511 | &transparent_hugepage_flags); | |
512 | ret = 1; | |
513 | } else if (!strcmp(str, "never")) { | |
514 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
515 | &transparent_hugepage_flags); | |
516 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
517 | &transparent_hugepage_flags); | |
518 | ret = 1; | |
519 | } | |
520 | out: | |
521 | if (!ret) | |
ae3a8c1c | 522 | pr_warn("transparent_hugepage= cannot parse, ignored\n"); |
71e3aac0 AA |
523 | return ret; |
524 | } | |
525 | __setup("transparent_hugepage=", setup_transparent_hugepage); | |
526 | ||
f55e1014 | 527 | pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
71e3aac0 | 528 | { |
f55e1014 | 529 | if (likely(vma->vm_flags & VM_WRITE)) |
71e3aac0 AA |
530 | pmd = pmd_mkwrite(pmd); |
531 | return pmd; | |
532 | } | |
533 | ||
87eaceb3 YS |
534 | #ifdef CONFIG_MEMCG |
535 | static inline struct deferred_split *get_deferred_split_queue(struct page *page) | |
9a982250 | 536 | { |
bcfe06bf | 537 | struct mem_cgroup *memcg = page_memcg(compound_head(page)); |
87eaceb3 YS |
538 | struct pglist_data *pgdat = NODE_DATA(page_to_nid(page)); |
539 | ||
540 | if (memcg) | |
541 | return &memcg->deferred_split_queue; | |
542 | else | |
543 | return &pgdat->deferred_split_queue; | |
9a982250 | 544 | } |
87eaceb3 YS |
545 | #else |
546 | static inline struct deferred_split *get_deferred_split_queue(struct page *page) | |
547 | { | |
548 | struct pglist_data *pgdat = NODE_DATA(page_to_nid(page)); | |
549 | ||
550 | return &pgdat->deferred_split_queue; | |
551 | } | |
552 | #endif | |
9a982250 KS |
553 | |
554 | void prep_transhuge_page(struct page *page) | |
555 | { | |
556 | /* | |
557 | * we use page->mapping and page->indexlru in second tail page | |
558 | * as list_head: assuming THP order >= 2 | |
559 | */ | |
9a982250 KS |
560 | |
561 | INIT_LIST_HEAD(page_deferred_list(page)); | |
562 | set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR); | |
563 | } | |
564 | ||
562beb72 | 565 | static inline bool is_transparent_hugepage(struct page *page) |
005ba37c SC |
566 | { |
567 | if (!PageCompound(page)) | |
fa1f68cc | 568 | return false; |
005ba37c SC |
569 | |
570 | page = compound_head(page); | |
571 | return is_huge_zero_page(page) || | |
572 | page[1].compound_dtor == TRANSHUGE_PAGE_DTOR; | |
573 | } | |
005ba37c | 574 | |
97d3d0f9 KS |
575 | static unsigned long __thp_get_unmapped_area(struct file *filp, |
576 | unsigned long addr, unsigned long len, | |
74d2fad1 TK |
577 | loff_t off, unsigned long flags, unsigned long size) |
578 | { | |
74d2fad1 TK |
579 | loff_t off_end = off + len; |
580 | loff_t off_align = round_up(off, size); | |
97d3d0f9 | 581 | unsigned long len_pad, ret; |
74d2fad1 TK |
582 | |
583 | if (off_end <= off_align || (off_end - off_align) < size) | |
584 | return 0; | |
585 | ||
586 | len_pad = len + size; | |
587 | if (len_pad < len || (off + len_pad) < off) | |
588 | return 0; | |
589 | ||
97d3d0f9 | 590 | ret = current->mm->get_unmapped_area(filp, addr, len_pad, |
74d2fad1 | 591 | off >> PAGE_SHIFT, flags); |
97d3d0f9 KS |
592 | |
593 | /* | |
594 | * The failure might be due to length padding. The caller will retry | |
595 | * without the padding. | |
596 | */ | |
597 | if (IS_ERR_VALUE(ret)) | |
74d2fad1 TK |
598 | return 0; |
599 | ||
97d3d0f9 KS |
600 | /* |
601 | * Do not try to align to THP boundary if allocation at the address | |
602 | * hint succeeds. | |
603 | */ | |
604 | if (ret == addr) | |
605 | return addr; | |
606 | ||
607 | ret += (off - ret) & (size - 1); | |
608 | return ret; | |
74d2fad1 TK |
609 | } |
610 | ||
611 | unsigned long thp_get_unmapped_area(struct file *filp, unsigned long addr, | |
612 | unsigned long len, unsigned long pgoff, unsigned long flags) | |
613 | { | |
97d3d0f9 | 614 | unsigned long ret; |
74d2fad1 TK |
615 | loff_t off = (loff_t)pgoff << PAGE_SHIFT; |
616 | ||
97d3d0f9 KS |
617 | ret = __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE); |
618 | if (ret) | |
619 | return ret; | |
1854bc6e | 620 | |
74d2fad1 TK |
621 | return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); |
622 | } | |
623 | EXPORT_SYMBOL_GPL(thp_get_unmapped_area); | |
624 | ||
2b740303 SJ |
625 | static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf, |
626 | struct page *page, gfp_t gfp) | |
71e3aac0 | 627 | { |
82b0f8c3 | 628 | struct vm_area_struct *vma = vmf->vma; |
71e3aac0 | 629 | pgtable_t pgtable; |
82b0f8c3 | 630 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; |
2b740303 | 631 | vm_fault_t ret = 0; |
71e3aac0 | 632 | |
309381fe | 633 | VM_BUG_ON_PAGE(!PageCompound(page), page); |
00501b53 | 634 | |
8f425e4e | 635 | if (mem_cgroup_charge(page_folio(page), vma->vm_mm, gfp)) { |
6b251fc9 AA |
636 | put_page(page); |
637 | count_vm_event(THP_FAULT_FALLBACK); | |
85b9f46e | 638 | count_vm_event(THP_FAULT_FALLBACK_CHARGE); |
6b251fc9 AA |
639 | return VM_FAULT_FALLBACK; |
640 | } | |
9d82c694 | 641 | cgroup_throttle_swaprate(page, gfp); |
00501b53 | 642 | |
4cf58924 | 643 | pgtable = pte_alloc_one(vma->vm_mm); |
00501b53 | 644 | if (unlikely(!pgtable)) { |
6b31d595 MH |
645 | ret = VM_FAULT_OOM; |
646 | goto release; | |
00501b53 | 647 | } |
71e3aac0 | 648 | |
c79b57e4 | 649 | clear_huge_page(page, vmf->address, HPAGE_PMD_NR); |
52f37629 MK |
650 | /* |
651 | * The memory barrier inside __SetPageUptodate makes sure that | |
652 | * clear_huge_page writes become visible before the set_pmd_at() | |
653 | * write. | |
654 | */ | |
71e3aac0 AA |
655 | __SetPageUptodate(page); |
656 | ||
82b0f8c3 JK |
657 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
658 | if (unlikely(!pmd_none(*vmf->pmd))) { | |
6b31d595 | 659 | goto unlock_release; |
71e3aac0 AA |
660 | } else { |
661 | pmd_t entry; | |
6b251fc9 | 662 | |
6b31d595 MH |
663 | ret = check_stable_address_space(vma->vm_mm); |
664 | if (ret) | |
665 | goto unlock_release; | |
666 | ||
6b251fc9 AA |
667 | /* Deliver the page fault to userland */ |
668 | if (userfaultfd_missing(vma)) { | |
82b0f8c3 | 669 | spin_unlock(vmf->ptl); |
6b251fc9 | 670 | put_page(page); |
bae473a4 | 671 | pte_free(vma->vm_mm, pgtable); |
8fd5eda4 ML |
672 | ret = handle_userfault(vmf, VM_UFFD_MISSING); |
673 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); | |
674 | return ret; | |
6b251fc9 AA |
675 | } |
676 | ||
3122359a | 677 | entry = mk_huge_pmd(page, vma->vm_page_prot); |
f55e1014 | 678 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
40f2bbf7 | 679 | page_add_new_anon_rmap(page, vma, haddr); |
b518154e | 680 | lru_cache_add_inactive_or_unevictable(page, vma); |
82b0f8c3 JK |
681 | pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); |
682 | set_pmd_at(vma->vm_mm, haddr, vmf->pmd, entry); | |
fca40573 | 683 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); |
bae473a4 | 684 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
c4812909 | 685 | mm_inc_nr_ptes(vma->vm_mm); |
82b0f8c3 | 686 | spin_unlock(vmf->ptl); |
6b251fc9 | 687 | count_vm_event(THP_FAULT_ALLOC); |
9d82c694 | 688 | count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC); |
71e3aac0 AA |
689 | } |
690 | ||
aa2e878e | 691 | return 0; |
6b31d595 MH |
692 | unlock_release: |
693 | spin_unlock(vmf->ptl); | |
694 | release: | |
695 | if (pgtable) | |
696 | pte_free(vma->vm_mm, pgtable); | |
6b31d595 MH |
697 | put_page(page); |
698 | return ret; | |
699 | ||
71e3aac0 AA |
700 | } |
701 | ||
444eb2a4 | 702 | /* |
21440d7e DR |
703 | * always: directly stall for all thp allocations |
704 | * defer: wake kswapd and fail if not immediately available | |
705 | * defer+madvise: wake kswapd and directly stall for MADV_HUGEPAGE, otherwise | |
706 | * fail if not immediately available | |
707 | * madvise: directly stall for MADV_HUGEPAGE, otherwise fail if not immediately | |
708 | * available | |
709 | * never: never stall for any thp allocation | |
444eb2a4 | 710 | */ |
164cc4fe | 711 | gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma) |
444eb2a4 | 712 | { |
164cc4fe | 713 | const bool vma_madvised = vma && (vma->vm_flags & VM_HUGEPAGE); |
2f0799a0 | 714 | |
ac79f78d | 715 | /* Always do synchronous compaction */ |
a8282608 AA |
716 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags)) |
717 | return GFP_TRANSHUGE | (vma_madvised ? 0 : __GFP_NORETRY); | |
ac79f78d DR |
718 | |
719 | /* Kick kcompactd and fail quickly */ | |
21440d7e | 720 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags)) |
19deb769 | 721 | return GFP_TRANSHUGE_LIGHT | __GFP_KSWAPD_RECLAIM; |
ac79f78d DR |
722 | |
723 | /* Synchronous compaction if madvised, otherwise kick kcompactd */ | |
21440d7e | 724 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, &transparent_hugepage_flags)) |
19deb769 DR |
725 | return GFP_TRANSHUGE_LIGHT | |
726 | (vma_madvised ? __GFP_DIRECT_RECLAIM : | |
727 | __GFP_KSWAPD_RECLAIM); | |
ac79f78d DR |
728 | |
729 | /* Only do synchronous compaction if madvised */ | |
21440d7e | 730 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags)) |
19deb769 DR |
731 | return GFP_TRANSHUGE_LIGHT | |
732 | (vma_madvised ? __GFP_DIRECT_RECLAIM : 0); | |
ac79f78d | 733 | |
19deb769 | 734 | return GFP_TRANSHUGE_LIGHT; |
444eb2a4 MG |
735 | } |
736 | ||
c4088ebd | 737 | /* Caller must hold page table lock. */ |
2efeb8da | 738 | static void set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, |
97ae1749 | 739 | struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, |
5918d10a | 740 | struct page *zero_page) |
fc9fe822 KS |
741 | { |
742 | pmd_t entry; | |
7c414164 | 743 | if (!pmd_none(*pmd)) |
2efeb8da | 744 | return; |
5918d10a | 745 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
fc9fe822 | 746 | entry = pmd_mkhuge(entry); |
12c9d70b MW |
747 | if (pgtable) |
748 | pgtable_trans_huge_deposit(mm, pmd, pgtable); | |
fc9fe822 | 749 | set_pmd_at(mm, haddr, pmd, entry); |
c4812909 | 750 | mm_inc_nr_ptes(mm); |
fc9fe822 KS |
751 | } |
752 | ||
2b740303 | 753 | vm_fault_t do_huge_pmd_anonymous_page(struct vm_fault *vmf) |
71e3aac0 | 754 | { |
82b0f8c3 | 755 | struct vm_area_struct *vma = vmf->vma; |
077fcf11 | 756 | gfp_t gfp; |
cb196ee1 | 757 | struct folio *folio; |
82b0f8c3 | 758 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; |
71e3aac0 | 759 | |
43675e6f | 760 | if (!transhuge_vma_suitable(vma, haddr)) |
c0292554 | 761 | return VM_FAULT_FALLBACK; |
128ec037 KS |
762 | if (unlikely(anon_vma_prepare(vma))) |
763 | return VM_FAULT_OOM; | |
4fa6893f | 764 | khugepaged_enter_vma(vma, vma->vm_flags); |
d2081b2b | 765 | |
82b0f8c3 | 766 | if (!(vmf->flags & FAULT_FLAG_WRITE) && |
bae473a4 | 767 | !mm_forbids_zeropage(vma->vm_mm) && |
128ec037 KS |
768 | transparent_hugepage_use_zero_page()) { |
769 | pgtable_t pgtable; | |
770 | struct page *zero_page; | |
2b740303 | 771 | vm_fault_t ret; |
4cf58924 | 772 | pgtable = pte_alloc_one(vma->vm_mm); |
128ec037 | 773 | if (unlikely(!pgtable)) |
ba76149f | 774 | return VM_FAULT_OOM; |
6fcb52a5 | 775 | zero_page = mm_get_huge_zero_page(vma->vm_mm); |
128ec037 | 776 | if (unlikely(!zero_page)) { |
bae473a4 | 777 | pte_free(vma->vm_mm, pgtable); |
81ab4201 | 778 | count_vm_event(THP_FAULT_FALLBACK); |
c0292554 | 779 | return VM_FAULT_FALLBACK; |
b9bbfbe3 | 780 | } |
82b0f8c3 | 781 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
6b251fc9 | 782 | ret = 0; |
82b0f8c3 | 783 | if (pmd_none(*vmf->pmd)) { |
6b31d595 MH |
784 | ret = check_stable_address_space(vma->vm_mm); |
785 | if (ret) { | |
786 | spin_unlock(vmf->ptl); | |
bfe8cc1d | 787 | pte_free(vma->vm_mm, pgtable); |
6b31d595 | 788 | } else if (userfaultfd_missing(vma)) { |
82b0f8c3 | 789 | spin_unlock(vmf->ptl); |
bfe8cc1d | 790 | pte_free(vma->vm_mm, pgtable); |
82b0f8c3 | 791 | ret = handle_userfault(vmf, VM_UFFD_MISSING); |
6b251fc9 AA |
792 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); |
793 | } else { | |
bae473a4 | 794 | set_huge_zero_page(pgtable, vma->vm_mm, vma, |
82b0f8c3 | 795 | haddr, vmf->pmd, zero_page); |
fca40573 | 796 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); |
82b0f8c3 | 797 | spin_unlock(vmf->ptl); |
6b251fc9 | 798 | } |
bfe8cc1d | 799 | } else { |
82b0f8c3 | 800 | spin_unlock(vmf->ptl); |
bae473a4 | 801 | pte_free(vma->vm_mm, pgtable); |
bfe8cc1d | 802 | } |
6b251fc9 | 803 | return ret; |
71e3aac0 | 804 | } |
164cc4fe | 805 | gfp = vma_thp_gfp_mask(vma); |
cb196ee1 MWO |
806 | folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, vma, haddr, true); |
807 | if (unlikely(!folio)) { | |
128ec037 | 808 | count_vm_event(THP_FAULT_FALLBACK); |
c0292554 | 809 | return VM_FAULT_FALLBACK; |
128ec037 | 810 | } |
cb196ee1 | 811 | return __do_huge_pmd_anonymous_page(vmf, &folio->page, gfp); |
71e3aac0 AA |
812 | } |
813 | ||
ae18d6dc | 814 | static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, |
3b6521f5 OH |
815 | pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write, |
816 | pgtable_t pgtable) | |
5cad465d MW |
817 | { |
818 | struct mm_struct *mm = vma->vm_mm; | |
819 | pmd_t entry; | |
820 | spinlock_t *ptl; | |
821 | ||
822 | ptl = pmd_lock(mm, pmd); | |
c6f3c5ee AK |
823 | if (!pmd_none(*pmd)) { |
824 | if (write) { | |
825 | if (pmd_pfn(*pmd) != pfn_t_to_pfn(pfn)) { | |
826 | WARN_ON_ONCE(!is_huge_zero_pmd(*pmd)); | |
827 | goto out_unlock; | |
828 | } | |
829 | entry = pmd_mkyoung(*pmd); | |
830 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); | |
831 | if (pmdp_set_access_flags(vma, addr, pmd, entry, 1)) | |
832 | update_mmu_cache_pmd(vma, addr, pmd); | |
833 | } | |
834 | ||
835 | goto out_unlock; | |
836 | } | |
837 | ||
f25748e3 DW |
838 | entry = pmd_mkhuge(pfn_t_pmd(pfn, prot)); |
839 | if (pfn_t_devmap(pfn)) | |
840 | entry = pmd_mkdevmap(entry); | |
01871e59 | 841 | if (write) { |
f55e1014 LT |
842 | entry = pmd_mkyoung(pmd_mkdirty(entry)); |
843 | entry = maybe_pmd_mkwrite(entry, vma); | |
5cad465d | 844 | } |
3b6521f5 OH |
845 | |
846 | if (pgtable) { | |
847 | pgtable_trans_huge_deposit(mm, pmd, pgtable); | |
c4812909 | 848 | mm_inc_nr_ptes(mm); |
c6f3c5ee | 849 | pgtable = NULL; |
3b6521f5 OH |
850 | } |
851 | ||
01871e59 RZ |
852 | set_pmd_at(mm, addr, pmd, entry); |
853 | update_mmu_cache_pmd(vma, addr, pmd); | |
c6f3c5ee AK |
854 | |
855 | out_unlock: | |
5cad465d | 856 | spin_unlock(ptl); |
c6f3c5ee AK |
857 | if (pgtable) |
858 | pte_free(mm, pgtable); | |
5cad465d MW |
859 | } |
860 | ||
9a9731b1 THV |
861 | /** |
862 | * vmf_insert_pfn_pmd_prot - insert a pmd size pfn | |
863 | * @vmf: Structure describing the fault | |
864 | * @pfn: pfn to insert | |
865 | * @pgprot: page protection to use | |
866 | * @write: whether it's a write fault | |
867 | * | |
868 | * Insert a pmd size pfn. See vmf_insert_pfn() for additional info and | |
869 | * also consult the vmf_insert_mixed_prot() documentation when | |
870 | * @pgprot != @vmf->vma->vm_page_prot. | |
871 | * | |
872 | * Return: vm_fault_t value. | |
873 | */ | |
874 | vm_fault_t vmf_insert_pfn_pmd_prot(struct vm_fault *vmf, pfn_t pfn, | |
875 | pgprot_t pgprot, bool write) | |
5cad465d | 876 | { |
fce86ff5 DW |
877 | unsigned long addr = vmf->address & PMD_MASK; |
878 | struct vm_area_struct *vma = vmf->vma; | |
3b6521f5 | 879 | pgtable_t pgtable = NULL; |
fce86ff5 | 880 | |
5cad465d MW |
881 | /* |
882 | * If we had pmd_special, we could avoid all these restrictions, | |
883 | * but we need to be consistent with PTEs and architectures that | |
884 | * can't support a 'special' bit. | |
885 | */ | |
e1fb4a08 DJ |
886 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) && |
887 | !pfn_t_devmap(pfn)); | |
5cad465d MW |
888 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == |
889 | (VM_PFNMAP|VM_MIXEDMAP)); | |
890 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); | |
5cad465d MW |
891 | |
892 | if (addr < vma->vm_start || addr >= vma->vm_end) | |
893 | return VM_FAULT_SIGBUS; | |
308a047c | 894 | |
3b6521f5 | 895 | if (arch_needs_pgtable_deposit()) { |
4cf58924 | 896 | pgtable = pte_alloc_one(vma->vm_mm); |
3b6521f5 OH |
897 | if (!pgtable) |
898 | return VM_FAULT_OOM; | |
899 | } | |
900 | ||
308a047c BP |
901 | track_pfn_insert(vma, &pgprot, pfn); |
902 | ||
fce86ff5 | 903 | insert_pfn_pmd(vma, addr, vmf->pmd, pfn, pgprot, write, pgtable); |
ae18d6dc | 904 | return VM_FAULT_NOPAGE; |
5cad465d | 905 | } |
9a9731b1 | 906 | EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd_prot); |
5cad465d | 907 | |
a00cc7d9 | 908 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
f55e1014 | 909 | static pud_t maybe_pud_mkwrite(pud_t pud, struct vm_area_struct *vma) |
a00cc7d9 | 910 | { |
f55e1014 | 911 | if (likely(vma->vm_flags & VM_WRITE)) |
a00cc7d9 MW |
912 | pud = pud_mkwrite(pud); |
913 | return pud; | |
914 | } | |
915 | ||
916 | static void insert_pfn_pud(struct vm_area_struct *vma, unsigned long addr, | |
917 | pud_t *pud, pfn_t pfn, pgprot_t prot, bool write) | |
918 | { | |
919 | struct mm_struct *mm = vma->vm_mm; | |
920 | pud_t entry; | |
921 | spinlock_t *ptl; | |
922 | ||
923 | ptl = pud_lock(mm, pud); | |
c6f3c5ee AK |
924 | if (!pud_none(*pud)) { |
925 | if (write) { | |
926 | if (pud_pfn(*pud) != pfn_t_to_pfn(pfn)) { | |
927 | WARN_ON_ONCE(!is_huge_zero_pud(*pud)); | |
928 | goto out_unlock; | |
929 | } | |
930 | entry = pud_mkyoung(*pud); | |
931 | entry = maybe_pud_mkwrite(pud_mkdirty(entry), vma); | |
932 | if (pudp_set_access_flags(vma, addr, pud, entry, 1)) | |
933 | update_mmu_cache_pud(vma, addr, pud); | |
934 | } | |
935 | goto out_unlock; | |
936 | } | |
937 | ||
a00cc7d9 MW |
938 | entry = pud_mkhuge(pfn_t_pud(pfn, prot)); |
939 | if (pfn_t_devmap(pfn)) | |
940 | entry = pud_mkdevmap(entry); | |
941 | if (write) { | |
f55e1014 LT |
942 | entry = pud_mkyoung(pud_mkdirty(entry)); |
943 | entry = maybe_pud_mkwrite(entry, vma); | |
a00cc7d9 MW |
944 | } |
945 | set_pud_at(mm, addr, pud, entry); | |
946 | update_mmu_cache_pud(vma, addr, pud); | |
c6f3c5ee AK |
947 | |
948 | out_unlock: | |
a00cc7d9 MW |
949 | spin_unlock(ptl); |
950 | } | |
951 | ||
9a9731b1 THV |
952 | /** |
953 | * vmf_insert_pfn_pud_prot - insert a pud size pfn | |
954 | * @vmf: Structure describing the fault | |
955 | * @pfn: pfn to insert | |
956 | * @pgprot: page protection to use | |
957 | * @write: whether it's a write fault | |
958 | * | |
959 | * Insert a pud size pfn. See vmf_insert_pfn() for additional info and | |
960 | * also consult the vmf_insert_mixed_prot() documentation when | |
961 | * @pgprot != @vmf->vma->vm_page_prot. | |
962 | * | |
963 | * Return: vm_fault_t value. | |
964 | */ | |
965 | vm_fault_t vmf_insert_pfn_pud_prot(struct vm_fault *vmf, pfn_t pfn, | |
966 | pgprot_t pgprot, bool write) | |
a00cc7d9 | 967 | { |
fce86ff5 DW |
968 | unsigned long addr = vmf->address & PUD_MASK; |
969 | struct vm_area_struct *vma = vmf->vma; | |
fce86ff5 | 970 | |
a00cc7d9 MW |
971 | /* |
972 | * If we had pud_special, we could avoid all these restrictions, | |
973 | * but we need to be consistent with PTEs and architectures that | |
974 | * can't support a 'special' bit. | |
975 | */ | |
62ec0d8c DJ |
976 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) && |
977 | !pfn_t_devmap(pfn)); | |
a00cc7d9 MW |
978 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == |
979 | (VM_PFNMAP|VM_MIXEDMAP)); | |
980 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); | |
a00cc7d9 MW |
981 | |
982 | if (addr < vma->vm_start || addr >= vma->vm_end) | |
983 | return VM_FAULT_SIGBUS; | |
984 | ||
985 | track_pfn_insert(vma, &pgprot, pfn); | |
986 | ||
fce86ff5 | 987 | insert_pfn_pud(vma, addr, vmf->pud, pfn, pgprot, write); |
a00cc7d9 MW |
988 | return VM_FAULT_NOPAGE; |
989 | } | |
9a9731b1 | 990 | EXPORT_SYMBOL_GPL(vmf_insert_pfn_pud_prot); |
a00cc7d9 MW |
991 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
992 | ||
3565fce3 | 993 | static void touch_pmd(struct vm_area_struct *vma, unsigned long addr, |
a8f97366 | 994 | pmd_t *pmd, int flags) |
3565fce3 DW |
995 | { |
996 | pmd_t _pmd; | |
997 | ||
a8f97366 KS |
998 | _pmd = pmd_mkyoung(*pmd); |
999 | if (flags & FOLL_WRITE) | |
1000 | _pmd = pmd_mkdirty(_pmd); | |
3565fce3 | 1001 | if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK, |
a8f97366 | 1002 | pmd, _pmd, flags & FOLL_WRITE)) |
3565fce3 DW |
1003 | update_mmu_cache_pmd(vma, addr, pmd); |
1004 | } | |
1005 | ||
1006 | struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr, | |
df06b37f | 1007 | pmd_t *pmd, int flags, struct dev_pagemap **pgmap) |
3565fce3 DW |
1008 | { |
1009 | unsigned long pfn = pmd_pfn(*pmd); | |
1010 | struct mm_struct *mm = vma->vm_mm; | |
3565fce3 DW |
1011 | struct page *page; |
1012 | ||
1013 | assert_spin_locked(pmd_lockptr(mm, pmd)); | |
1014 | ||
8310d48b KF |
1015 | /* |
1016 | * When we COW a devmap PMD entry, we split it into PTEs, so we should | |
1017 | * not be in this function with `flags & FOLL_COW` set. | |
1018 | */ | |
1019 | WARN_ONCE(flags & FOLL_COW, "mm: In follow_devmap_pmd with FOLL_COW set"); | |
1020 | ||
3faa52c0 JH |
1021 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
1022 | if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == | |
1023 | (FOLL_PIN | FOLL_GET))) | |
1024 | return NULL; | |
1025 | ||
f6f37321 | 1026 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) |
3565fce3 DW |
1027 | return NULL; |
1028 | ||
1029 | if (pmd_present(*pmd) && pmd_devmap(*pmd)) | |
1030 | /* pass */; | |
1031 | else | |
1032 | return NULL; | |
1033 | ||
1034 | if (flags & FOLL_TOUCH) | |
a8f97366 | 1035 | touch_pmd(vma, addr, pmd, flags); |
3565fce3 DW |
1036 | |
1037 | /* | |
1038 | * device mapped pages can only be returned if the | |
1039 | * caller will manage the page reference count. | |
1040 | */ | |
3faa52c0 | 1041 | if (!(flags & (FOLL_GET | FOLL_PIN))) |
3565fce3 DW |
1042 | return ERR_PTR(-EEXIST); |
1043 | ||
1044 | pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT; | |
df06b37f KB |
1045 | *pgmap = get_dev_pagemap(pfn, *pgmap); |
1046 | if (!*pgmap) | |
3565fce3 DW |
1047 | return ERR_PTR(-EFAULT); |
1048 | page = pfn_to_page(pfn); | |
3faa52c0 JH |
1049 | if (!try_grab_page(page, flags)) |
1050 | page = ERR_PTR(-ENOMEM); | |
3565fce3 DW |
1051 | |
1052 | return page; | |
1053 | } | |
1054 | ||
71e3aac0 AA |
1055 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
1056 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, | |
8f34f1ea | 1057 | struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) |
71e3aac0 | 1058 | { |
c4088ebd | 1059 | spinlock_t *dst_ptl, *src_ptl; |
71e3aac0 AA |
1060 | struct page *src_page; |
1061 | pmd_t pmd; | |
12c9d70b | 1062 | pgtable_t pgtable = NULL; |
628d47ce | 1063 | int ret = -ENOMEM; |
71e3aac0 | 1064 | |
628d47ce | 1065 | /* Skip if can be re-fill on fault */ |
8f34f1ea | 1066 | if (!vma_is_anonymous(dst_vma)) |
628d47ce KS |
1067 | return 0; |
1068 | ||
4cf58924 | 1069 | pgtable = pte_alloc_one(dst_mm); |
628d47ce KS |
1070 | if (unlikely(!pgtable)) |
1071 | goto out; | |
71e3aac0 | 1072 | |
c4088ebd KS |
1073 | dst_ptl = pmd_lock(dst_mm, dst_pmd); |
1074 | src_ptl = pmd_lockptr(src_mm, src_pmd); | |
1075 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); | |
71e3aac0 AA |
1076 | |
1077 | ret = -EAGAIN; | |
1078 | pmd = *src_pmd; | |
84c3fc4e ZY |
1079 | |
1080 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION | |
1081 | if (unlikely(is_swap_pmd(pmd))) { | |
1082 | swp_entry_t entry = pmd_to_swp_entry(pmd); | |
1083 | ||
1084 | VM_BUG_ON(!is_pmd_migration_entry(pmd)); | |
6c287605 | 1085 | if (!is_readable_migration_entry(entry)) { |
4dd845b5 AP |
1086 | entry = make_readable_migration_entry( |
1087 | swp_offset(entry)); | |
84c3fc4e | 1088 | pmd = swp_entry_to_pmd(entry); |
ab6e3d09 NH |
1089 | if (pmd_swp_soft_dirty(*src_pmd)) |
1090 | pmd = pmd_swp_mksoft_dirty(pmd); | |
8f34f1ea PX |
1091 | if (pmd_swp_uffd_wp(*src_pmd)) |
1092 | pmd = pmd_swp_mkuffd_wp(pmd); | |
84c3fc4e ZY |
1093 | set_pmd_at(src_mm, addr, src_pmd, pmd); |
1094 | } | |
dd8a67f9 | 1095 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
af5b0f6a | 1096 | mm_inc_nr_ptes(dst_mm); |
dd8a67f9 | 1097 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); |
8f34f1ea PX |
1098 | if (!userfaultfd_wp(dst_vma)) |
1099 | pmd = pmd_swp_clear_uffd_wp(pmd); | |
84c3fc4e ZY |
1100 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); |
1101 | ret = 0; | |
1102 | goto out_unlock; | |
1103 | } | |
1104 | #endif | |
1105 | ||
628d47ce | 1106 | if (unlikely(!pmd_trans_huge(pmd))) { |
71e3aac0 AA |
1107 | pte_free(dst_mm, pgtable); |
1108 | goto out_unlock; | |
1109 | } | |
fc9fe822 | 1110 | /* |
c4088ebd | 1111 | * When page table lock is held, the huge zero pmd should not be |
fc9fe822 KS |
1112 | * under splitting since we don't split the page itself, only pmd to |
1113 | * a page table. | |
1114 | */ | |
1115 | if (is_huge_zero_pmd(pmd)) { | |
97ae1749 KS |
1116 | /* |
1117 | * get_huge_zero_page() will never allocate a new page here, | |
1118 | * since we already have a zero page to copy. It just takes a | |
1119 | * reference. | |
1120 | */ | |
5fc7a5f6 PX |
1121 | mm_get_huge_zero_page(dst_mm); |
1122 | goto out_zero_page; | |
fc9fe822 | 1123 | } |
de466bd6 | 1124 | |
628d47ce KS |
1125 | src_page = pmd_page(pmd); |
1126 | VM_BUG_ON_PAGE(!PageHead(src_page), src_page); | |
d042035e | 1127 | |
fb3d824d DH |
1128 | get_page(src_page); |
1129 | if (unlikely(page_try_dup_anon_rmap(src_page, true, src_vma))) { | |
1130 | /* Page maybe pinned: split and retry the fault on PTEs. */ | |
1131 | put_page(src_page); | |
d042035e PX |
1132 | pte_free(dst_mm, pgtable); |
1133 | spin_unlock(src_ptl); | |
1134 | spin_unlock(dst_ptl); | |
8f34f1ea | 1135 | __split_huge_pmd(src_vma, src_pmd, addr, false, NULL); |
d042035e PX |
1136 | return -EAGAIN; |
1137 | } | |
628d47ce | 1138 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
5fc7a5f6 | 1139 | out_zero_page: |
c4812909 | 1140 | mm_inc_nr_ptes(dst_mm); |
628d47ce | 1141 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); |
71e3aac0 | 1142 | pmdp_set_wrprotect(src_mm, addr, src_pmd); |
8f34f1ea PX |
1143 | if (!userfaultfd_wp(dst_vma)) |
1144 | pmd = pmd_clear_uffd_wp(pmd); | |
71e3aac0 AA |
1145 | pmd = pmd_mkold(pmd_wrprotect(pmd)); |
1146 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); | |
71e3aac0 AA |
1147 | |
1148 | ret = 0; | |
1149 | out_unlock: | |
c4088ebd KS |
1150 | spin_unlock(src_ptl); |
1151 | spin_unlock(dst_ptl); | |
71e3aac0 AA |
1152 | out: |
1153 | return ret; | |
1154 | } | |
1155 | ||
a00cc7d9 MW |
1156 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
1157 | static void touch_pud(struct vm_area_struct *vma, unsigned long addr, | |
a8f97366 | 1158 | pud_t *pud, int flags) |
a00cc7d9 MW |
1159 | { |
1160 | pud_t _pud; | |
1161 | ||
a8f97366 KS |
1162 | _pud = pud_mkyoung(*pud); |
1163 | if (flags & FOLL_WRITE) | |
1164 | _pud = pud_mkdirty(_pud); | |
a00cc7d9 | 1165 | if (pudp_set_access_flags(vma, addr & HPAGE_PUD_MASK, |
a8f97366 | 1166 | pud, _pud, flags & FOLL_WRITE)) |
a00cc7d9 MW |
1167 | update_mmu_cache_pud(vma, addr, pud); |
1168 | } | |
1169 | ||
1170 | struct page *follow_devmap_pud(struct vm_area_struct *vma, unsigned long addr, | |
df06b37f | 1171 | pud_t *pud, int flags, struct dev_pagemap **pgmap) |
a00cc7d9 MW |
1172 | { |
1173 | unsigned long pfn = pud_pfn(*pud); | |
1174 | struct mm_struct *mm = vma->vm_mm; | |
a00cc7d9 MW |
1175 | struct page *page; |
1176 | ||
1177 | assert_spin_locked(pud_lockptr(mm, pud)); | |
1178 | ||
f6f37321 | 1179 | if (flags & FOLL_WRITE && !pud_write(*pud)) |
a00cc7d9 MW |
1180 | return NULL; |
1181 | ||
3faa52c0 JH |
1182 | /* FOLL_GET and FOLL_PIN are mutually exclusive. */ |
1183 | if (WARN_ON_ONCE((flags & (FOLL_PIN | FOLL_GET)) == | |
1184 | (FOLL_PIN | FOLL_GET))) | |
1185 | return NULL; | |
1186 | ||
a00cc7d9 MW |
1187 | if (pud_present(*pud) && pud_devmap(*pud)) |
1188 | /* pass */; | |
1189 | else | |
1190 | return NULL; | |
1191 | ||
1192 | if (flags & FOLL_TOUCH) | |
a8f97366 | 1193 | touch_pud(vma, addr, pud, flags); |
a00cc7d9 MW |
1194 | |
1195 | /* | |
1196 | * device mapped pages can only be returned if the | |
1197 | * caller will manage the page reference count. | |
3faa52c0 JH |
1198 | * |
1199 | * At least one of FOLL_GET | FOLL_PIN must be set, so assert that here: | |
a00cc7d9 | 1200 | */ |
3faa52c0 | 1201 | if (!(flags & (FOLL_GET | FOLL_PIN))) |
a00cc7d9 MW |
1202 | return ERR_PTR(-EEXIST); |
1203 | ||
1204 | pfn += (addr & ~PUD_MASK) >> PAGE_SHIFT; | |
df06b37f KB |
1205 | *pgmap = get_dev_pagemap(pfn, *pgmap); |
1206 | if (!*pgmap) | |
a00cc7d9 MW |
1207 | return ERR_PTR(-EFAULT); |
1208 | page = pfn_to_page(pfn); | |
3faa52c0 JH |
1209 | if (!try_grab_page(page, flags)) |
1210 | page = ERR_PTR(-ENOMEM); | |
a00cc7d9 MW |
1211 | |
1212 | return page; | |
1213 | } | |
1214 | ||
1215 | int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, | |
1216 | pud_t *dst_pud, pud_t *src_pud, unsigned long addr, | |
1217 | struct vm_area_struct *vma) | |
1218 | { | |
1219 | spinlock_t *dst_ptl, *src_ptl; | |
1220 | pud_t pud; | |
1221 | int ret; | |
1222 | ||
1223 | dst_ptl = pud_lock(dst_mm, dst_pud); | |
1224 | src_ptl = pud_lockptr(src_mm, src_pud); | |
1225 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); | |
1226 | ||
1227 | ret = -EAGAIN; | |
1228 | pud = *src_pud; | |
1229 | if (unlikely(!pud_trans_huge(pud) && !pud_devmap(pud))) | |
1230 | goto out_unlock; | |
1231 | ||
1232 | /* | |
1233 | * When page table lock is held, the huge zero pud should not be | |
1234 | * under splitting since we don't split the page itself, only pud to | |
1235 | * a page table. | |
1236 | */ | |
1237 | if (is_huge_zero_pud(pud)) { | |
1238 | /* No huge zero pud yet */ | |
1239 | } | |
1240 | ||
fb3d824d DH |
1241 | /* |
1242 | * TODO: once we support anonymous pages, use page_try_dup_anon_rmap() | |
1243 | * and split if duplicating fails. | |
1244 | */ | |
a00cc7d9 MW |
1245 | pudp_set_wrprotect(src_mm, addr, src_pud); |
1246 | pud = pud_mkold(pud_wrprotect(pud)); | |
1247 | set_pud_at(dst_mm, addr, dst_pud, pud); | |
1248 | ||
1249 | ret = 0; | |
1250 | out_unlock: | |
1251 | spin_unlock(src_ptl); | |
1252 | spin_unlock(dst_ptl); | |
1253 | return ret; | |
1254 | } | |
1255 | ||
1256 | void huge_pud_set_accessed(struct vm_fault *vmf, pud_t orig_pud) | |
1257 | { | |
1258 | pud_t entry; | |
1259 | unsigned long haddr; | |
1260 | bool write = vmf->flags & FAULT_FLAG_WRITE; | |
1261 | ||
1262 | vmf->ptl = pud_lock(vmf->vma->vm_mm, vmf->pud); | |
1263 | if (unlikely(!pud_same(*vmf->pud, orig_pud))) | |
1264 | goto unlock; | |
1265 | ||
1266 | entry = pud_mkyoung(orig_pud); | |
1267 | if (write) | |
1268 | entry = pud_mkdirty(entry); | |
1269 | haddr = vmf->address & HPAGE_PUD_MASK; | |
1270 | if (pudp_set_access_flags(vmf->vma, haddr, vmf->pud, entry, write)) | |
1271 | update_mmu_cache_pud(vmf->vma, vmf->address, vmf->pud); | |
1272 | ||
1273 | unlock: | |
1274 | spin_unlock(vmf->ptl); | |
1275 | } | |
1276 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ | |
1277 | ||
5db4f15c | 1278 | void huge_pmd_set_accessed(struct vm_fault *vmf) |
a1dd450b WD |
1279 | { |
1280 | pmd_t entry; | |
1281 | unsigned long haddr; | |
20f664aa | 1282 | bool write = vmf->flags & FAULT_FLAG_WRITE; |
5db4f15c | 1283 | pmd_t orig_pmd = vmf->orig_pmd; |
a1dd450b | 1284 | |
82b0f8c3 JK |
1285 | vmf->ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); |
1286 | if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) | |
a1dd450b WD |
1287 | goto unlock; |
1288 | ||
1289 | entry = pmd_mkyoung(orig_pmd); | |
20f664aa MK |
1290 | if (write) |
1291 | entry = pmd_mkdirty(entry); | |
82b0f8c3 | 1292 | haddr = vmf->address & HPAGE_PMD_MASK; |
20f664aa | 1293 | if (pmdp_set_access_flags(vmf->vma, haddr, vmf->pmd, entry, write)) |
82b0f8c3 | 1294 | update_mmu_cache_pmd(vmf->vma, vmf->address, vmf->pmd); |
a1dd450b WD |
1295 | |
1296 | unlock: | |
82b0f8c3 | 1297 | spin_unlock(vmf->ptl); |
a1dd450b WD |
1298 | } |
1299 | ||
5db4f15c | 1300 | vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf) |
71e3aac0 | 1301 | { |
c89357e2 | 1302 | const bool unshare = vmf->flags & FAULT_FLAG_UNSHARE; |
82b0f8c3 | 1303 | struct vm_area_struct *vma = vmf->vma; |
3917c802 | 1304 | struct page *page; |
82b0f8c3 | 1305 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; |
5db4f15c | 1306 | pmd_t orig_pmd = vmf->orig_pmd; |
71e3aac0 | 1307 | |
82b0f8c3 | 1308 | vmf->ptl = pmd_lockptr(vma->vm_mm, vmf->pmd); |
81d1b09c | 1309 | VM_BUG_ON_VMA(!vma->anon_vma, vma); |
3917c802 | 1310 | |
c89357e2 DH |
1311 | VM_BUG_ON(unshare && (vmf->flags & FAULT_FLAG_WRITE)); |
1312 | VM_BUG_ON(!unshare && !(vmf->flags & FAULT_FLAG_WRITE)); | |
1313 | ||
93b4796d | 1314 | if (is_huge_zero_pmd(orig_pmd)) |
3917c802 KS |
1315 | goto fallback; |
1316 | ||
82b0f8c3 | 1317 | spin_lock(vmf->ptl); |
3917c802 KS |
1318 | |
1319 | if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) { | |
1320 | spin_unlock(vmf->ptl); | |
1321 | return 0; | |
1322 | } | |
71e3aac0 AA |
1323 | |
1324 | page = pmd_page(orig_pmd); | |
f6004e73 | 1325 | VM_BUG_ON_PAGE(!PageHead(page), page); |
3917c802 | 1326 | |
6c287605 DH |
1327 | /* Early check when only holding the PT lock. */ |
1328 | if (PageAnonExclusive(page)) | |
1329 | goto reuse; | |
1330 | ||
ba3c4ce6 HY |
1331 | if (!trylock_page(page)) { |
1332 | get_page(page); | |
1333 | spin_unlock(vmf->ptl); | |
1334 | lock_page(page); | |
1335 | spin_lock(vmf->ptl); | |
1336 | if (unlikely(!pmd_same(*vmf->pmd, orig_pmd))) { | |
3917c802 | 1337 | spin_unlock(vmf->ptl); |
ba3c4ce6 HY |
1338 | unlock_page(page); |
1339 | put_page(page); | |
3917c802 | 1340 | return 0; |
ba3c4ce6 HY |
1341 | } |
1342 | put_page(page); | |
1343 | } | |
3917c802 | 1344 | |
6c287605 DH |
1345 | /* Recheck after temporarily dropping the PT lock. */ |
1346 | if (PageAnonExclusive(page)) { | |
1347 | unlock_page(page); | |
1348 | goto reuse; | |
1349 | } | |
1350 | ||
3917c802 | 1351 | /* |
c89357e2 | 1352 | * See do_wp_page(): we can only reuse the page exclusively if there are |
3bff7e3f DH |
1353 | * no additional references. Note that we always drain the LRU |
1354 | * pagevecs immediately after adding a THP. | |
3917c802 | 1355 | */ |
3bff7e3f DH |
1356 | if (page_count(page) > 1 + PageSwapCache(page) * thp_nr_pages(page)) |
1357 | goto unlock_fallback; | |
1358 | if (PageSwapCache(page)) | |
1359 | try_to_free_swap(page); | |
1360 | if (page_count(page) == 1) { | |
71e3aac0 | 1361 | pmd_t entry; |
6c54dc6c DH |
1362 | |
1363 | page_move_anon_rmap(page, vma); | |
6c287605 DH |
1364 | unlock_page(page); |
1365 | reuse: | |
c89357e2 DH |
1366 | if (unlikely(unshare)) { |
1367 | spin_unlock(vmf->ptl); | |
1368 | return 0; | |
1369 | } | |
71e3aac0 | 1370 | entry = pmd_mkyoung(orig_pmd); |
f55e1014 | 1371 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); |
3917c802 | 1372 | if (pmdp_set_access_flags(vma, haddr, vmf->pmd, entry, 1)) |
82b0f8c3 | 1373 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); |
82b0f8c3 | 1374 | spin_unlock(vmf->ptl); |
3917c802 | 1375 | return VM_FAULT_WRITE; |
71e3aac0 | 1376 | } |
3917c802 | 1377 | |
3bff7e3f | 1378 | unlock_fallback: |
3917c802 | 1379 | unlock_page(page); |
82b0f8c3 | 1380 | spin_unlock(vmf->ptl); |
3917c802 KS |
1381 | fallback: |
1382 | __split_huge_pmd(vma, vmf->pmd, vmf->address, false, NULL); | |
1383 | return VM_FAULT_FALLBACK; | |
71e3aac0 AA |
1384 | } |
1385 | ||
8310d48b | 1386 | /* |
a308c71b PX |
1387 | * FOLL_FORCE can write to even unwritable pmd's, but only |
1388 | * after we've gone through a COW cycle and they are dirty. | |
8310d48b KF |
1389 | */ |
1390 | static inline bool can_follow_write_pmd(pmd_t pmd, unsigned int flags) | |
1391 | { | |
a308c71b PX |
1392 | return pmd_write(pmd) || |
1393 | ((flags & FOLL_FORCE) && (flags & FOLL_COW) && pmd_dirty(pmd)); | |
8310d48b KF |
1394 | } |
1395 | ||
b676b293 | 1396 | struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, |
71e3aac0 AA |
1397 | unsigned long addr, |
1398 | pmd_t *pmd, | |
1399 | unsigned int flags) | |
1400 | { | |
b676b293 | 1401 | struct mm_struct *mm = vma->vm_mm; |
71e3aac0 AA |
1402 | struct page *page = NULL; |
1403 | ||
c4088ebd | 1404 | assert_spin_locked(pmd_lockptr(mm, pmd)); |
71e3aac0 | 1405 | |
8310d48b | 1406 | if (flags & FOLL_WRITE && !can_follow_write_pmd(*pmd, flags)) |
71e3aac0 AA |
1407 | goto out; |
1408 | ||
85facf25 KS |
1409 | /* Avoid dumping huge zero page */ |
1410 | if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) | |
1411 | return ERR_PTR(-EFAULT); | |
1412 | ||
2b4847e7 | 1413 | /* Full NUMA hinting faults to serialise migration in fault paths */ |
8a0516ed | 1414 | if ((flags & FOLL_NUMA) && pmd_protnone(*pmd)) |
2b4847e7 MG |
1415 | goto out; |
1416 | ||
71e3aac0 | 1417 | page = pmd_page(*pmd); |
ca120cf6 | 1418 | VM_BUG_ON_PAGE(!PageHead(page) && !is_zone_device_page(page), page); |
3faa52c0 | 1419 | |
a7f22660 DH |
1420 | if (!pmd_write(*pmd) && gup_must_unshare(flags, page)) |
1421 | return ERR_PTR(-EMLINK); | |
1422 | ||
b6a2619c DH |
1423 | VM_BUG_ON_PAGE((flags & FOLL_PIN) && PageAnon(page) && |
1424 | !PageAnonExclusive(page), page); | |
1425 | ||
3faa52c0 JH |
1426 | if (!try_grab_page(page, flags)) |
1427 | return ERR_PTR(-ENOMEM); | |
1428 | ||
3565fce3 | 1429 | if (flags & FOLL_TOUCH) |
a8f97366 | 1430 | touch_pmd(vma, addr, pmd, flags); |
3faa52c0 | 1431 | |
71e3aac0 | 1432 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; |
ca120cf6 | 1433 | VM_BUG_ON_PAGE(!PageCompound(page) && !is_zone_device_page(page), page); |
71e3aac0 AA |
1434 | |
1435 | out: | |
1436 | return page; | |
1437 | } | |
1438 | ||
d10e63f2 | 1439 | /* NUMA hinting page fault entry point for trans huge pmds */ |
5db4f15c | 1440 | vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) |
d10e63f2 | 1441 | { |
82b0f8c3 | 1442 | struct vm_area_struct *vma = vmf->vma; |
c5b5a3dd YS |
1443 | pmd_t oldpmd = vmf->orig_pmd; |
1444 | pmd_t pmd; | |
b32967ff | 1445 | struct page *page; |
82b0f8c3 | 1446 | unsigned long haddr = vmf->address & HPAGE_PMD_MASK; |
c5b5a3dd | 1447 | int page_nid = NUMA_NO_NODE; |
90572890 | 1448 | int target_nid, last_cpupid = -1; |
8191acbd | 1449 | bool migrated = false; |
c5b5a3dd | 1450 | bool was_writable = pmd_savedwrite(oldpmd); |
6688cc05 | 1451 | int flags = 0; |
d10e63f2 | 1452 | |
82b0f8c3 | 1453 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
c5b5a3dd | 1454 | if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) { |
82b0f8c3 | 1455 | spin_unlock(vmf->ptl); |
de466bd6 MG |
1456 | goto out; |
1457 | } | |
1458 | ||
c5b5a3dd YS |
1459 | pmd = pmd_modify(oldpmd, vma->vm_page_prot); |
1460 | page = vm_normal_page_pmd(vma, haddr, pmd); | |
1461 | if (!page) | |
1462 | goto out_map; | |
1463 | ||
1464 | /* See similar comment in do_numa_page for explanation */ | |
1465 | if (!was_writable) | |
1466 | flags |= TNF_NO_GROUP; | |
1467 | ||
1468 | page_nid = page_to_nid(page); | |
1469 | last_cpupid = page_cpupid_last(page); | |
1470 | target_nid = numa_migrate_prep(page, vma, haddr, page_nid, | |
1471 | &flags); | |
1472 | ||
1473 | if (target_nid == NUMA_NO_NODE) { | |
1474 | put_page(page); | |
1475 | goto out_map; | |
1476 | } | |
1477 | ||
82b0f8c3 | 1478 | spin_unlock(vmf->ptl); |
8b1b436d | 1479 | |
c5b5a3dd | 1480 | migrated = migrate_misplaced_page(page, vma, target_nid); |
6688cc05 PZ |
1481 | if (migrated) { |
1482 | flags |= TNF_MIGRATED; | |
8191acbd | 1483 | page_nid = target_nid; |
c5b5a3dd | 1484 | } else { |
074c2381 | 1485 | flags |= TNF_MIGRATE_FAIL; |
c5b5a3dd YS |
1486 | vmf->ptl = pmd_lock(vma->vm_mm, vmf->pmd); |
1487 | if (unlikely(!pmd_same(oldpmd, *vmf->pmd))) { | |
1488 | spin_unlock(vmf->ptl); | |
1489 | goto out; | |
1490 | } | |
1491 | goto out_map; | |
1492 | } | |
b8916634 MG |
1493 | |
1494 | out: | |
98fa15f3 | 1495 | if (page_nid != NUMA_NO_NODE) |
82b0f8c3 | 1496 | task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, |
9a8b300f | 1497 | flags); |
8191acbd | 1498 | |
d10e63f2 | 1499 | return 0; |
c5b5a3dd YS |
1500 | |
1501 | out_map: | |
1502 | /* Restore the PMD */ | |
1503 | pmd = pmd_modify(oldpmd, vma->vm_page_prot); | |
1504 | pmd = pmd_mkyoung(pmd); | |
1505 | if (was_writable) | |
1506 | pmd = pmd_mkwrite(pmd); | |
1507 | set_pmd_at(vma->vm_mm, haddr, vmf->pmd, pmd); | |
1508 | update_mmu_cache_pmd(vma, vmf->address, vmf->pmd); | |
1509 | spin_unlock(vmf->ptl); | |
1510 | goto out; | |
d10e63f2 MG |
1511 | } |
1512 | ||
319904ad HY |
1513 | /* |
1514 | * Return true if we do MADV_FREE successfully on entire pmd page. | |
1515 | * Otherwise, return false. | |
1516 | */ | |
1517 | bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, | |
b8d3c4c3 | 1518 | pmd_t *pmd, unsigned long addr, unsigned long next) |
b8d3c4c3 MK |
1519 | { |
1520 | spinlock_t *ptl; | |
1521 | pmd_t orig_pmd; | |
1522 | struct page *page; | |
1523 | struct mm_struct *mm = tlb->mm; | |
319904ad | 1524 | bool ret = false; |
b8d3c4c3 | 1525 | |
ed6a7935 | 1526 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE); |
07e32661 | 1527 | |
b6ec57f4 KS |
1528 | ptl = pmd_trans_huge_lock(pmd, vma); |
1529 | if (!ptl) | |
25eedabe | 1530 | goto out_unlocked; |
b8d3c4c3 MK |
1531 | |
1532 | orig_pmd = *pmd; | |
319904ad | 1533 | if (is_huge_zero_pmd(orig_pmd)) |
b8d3c4c3 | 1534 | goto out; |
b8d3c4c3 | 1535 | |
84c3fc4e ZY |
1536 | if (unlikely(!pmd_present(orig_pmd))) { |
1537 | VM_BUG_ON(thp_migration_supported() && | |
1538 | !is_pmd_migration_entry(orig_pmd)); | |
1539 | goto out; | |
1540 | } | |
1541 | ||
b8d3c4c3 MK |
1542 | page = pmd_page(orig_pmd); |
1543 | /* | |
1544 | * If other processes are mapping this page, we couldn't discard | |
1545 | * the page unless they all do MADV_FREE so let's skip the page. | |
1546 | */ | |
babbbdd0 | 1547 | if (total_mapcount(page) != 1) |
b8d3c4c3 MK |
1548 | goto out; |
1549 | ||
1550 | if (!trylock_page(page)) | |
1551 | goto out; | |
1552 | ||
1553 | /* | |
1554 | * If user want to discard part-pages of THP, split it so MADV_FREE | |
1555 | * will deactivate only them. | |
1556 | */ | |
1557 | if (next - addr != HPAGE_PMD_SIZE) { | |
1558 | get_page(page); | |
1559 | spin_unlock(ptl); | |
9818b8cd | 1560 | split_huge_page(page); |
b8d3c4c3 | 1561 | unlock_page(page); |
bbf29ffc | 1562 | put_page(page); |
b8d3c4c3 MK |
1563 | goto out_unlocked; |
1564 | } | |
1565 | ||
1566 | if (PageDirty(page)) | |
1567 | ClearPageDirty(page); | |
1568 | unlock_page(page); | |
1569 | ||
b8d3c4c3 | 1570 | if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) { |
58ceeb6b | 1571 | pmdp_invalidate(vma, addr, pmd); |
b8d3c4c3 MK |
1572 | orig_pmd = pmd_mkold(orig_pmd); |
1573 | orig_pmd = pmd_mkclean(orig_pmd); | |
1574 | ||
1575 | set_pmd_at(mm, addr, pmd, orig_pmd); | |
1576 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); | |
1577 | } | |
802a3a92 SL |
1578 | |
1579 | mark_page_lazyfree(page); | |
319904ad | 1580 | ret = true; |
b8d3c4c3 MK |
1581 | out: |
1582 | spin_unlock(ptl); | |
1583 | out_unlocked: | |
1584 | return ret; | |
1585 | } | |
1586 | ||
953c66c2 AK |
1587 | static inline void zap_deposited_table(struct mm_struct *mm, pmd_t *pmd) |
1588 | { | |
1589 | pgtable_t pgtable; | |
1590 | ||
1591 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); | |
1592 | pte_free(mm, pgtable); | |
c4812909 | 1593 | mm_dec_nr_ptes(mm); |
953c66c2 AK |
1594 | } |
1595 | ||
71e3aac0 | 1596 | int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
f21760b1 | 1597 | pmd_t *pmd, unsigned long addr) |
71e3aac0 | 1598 | { |
da146769 | 1599 | pmd_t orig_pmd; |
bf929152 | 1600 | spinlock_t *ptl; |
71e3aac0 | 1601 | |
ed6a7935 | 1602 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE); |
07e32661 | 1603 | |
b6ec57f4 KS |
1604 | ptl = __pmd_trans_huge_lock(pmd, vma); |
1605 | if (!ptl) | |
da146769 KS |
1606 | return 0; |
1607 | /* | |
1608 | * For architectures like ppc64 we look at deposited pgtable | |
1609 | * when calling pmdp_huge_get_and_clear. So do the | |
1610 | * pgtable_trans_huge_withdraw after finishing pmdp related | |
1611 | * operations. | |
1612 | */ | |
93a98695 AK |
1613 | orig_pmd = pmdp_huge_get_and_clear_full(vma, addr, pmd, |
1614 | tlb->fullmm); | |
da146769 | 1615 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); |
2484ca9b | 1616 | if (vma_is_special_huge(vma)) { |
3b6521f5 OH |
1617 | if (arch_needs_pgtable_deposit()) |
1618 | zap_deposited_table(tlb->mm, pmd); | |
da146769 | 1619 | spin_unlock(ptl); |
da146769 | 1620 | } else if (is_huge_zero_pmd(orig_pmd)) { |
c14a6eb4 | 1621 | zap_deposited_table(tlb->mm, pmd); |
da146769 | 1622 | spin_unlock(ptl); |
da146769 | 1623 | } else { |
616b8371 ZY |
1624 | struct page *page = NULL; |
1625 | int flush_needed = 1; | |
1626 | ||
1627 | if (pmd_present(orig_pmd)) { | |
1628 | page = pmd_page(orig_pmd); | |
cea86fe2 | 1629 | page_remove_rmap(page, vma, true); |
616b8371 ZY |
1630 | VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); |
1631 | VM_BUG_ON_PAGE(!PageHead(page), page); | |
1632 | } else if (thp_migration_supported()) { | |
1633 | swp_entry_t entry; | |
1634 | ||
1635 | VM_BUG_ON(!is_pmd_migration_entry(orig_pmd)); | |
1636 | entry = pmd_to_swp_entry(orig_pmd); | |
af5cdaf8 | 1637 | page = pfn_swap_entry_to_page(entry); |
616b8371 ZY |
1638 | flush_needed = 0; |
1639 | } else | |
1640 | WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); | |
1641 | ||
b5072380 | 1642 | if (PageAnon(page)) { |
c14a6eb4 | 1643 | zap_deposited_table(tlb->mm, pmd); |
b5072380 KS |
1644 | add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); |
1645 | } else { | |
953c66c2 AK |
1646 | if (arch_needs_pgtable_deposit()) |
1647 | zap_deposited_table(tlb->mm, pmd); | |
fadae295 | 1648 | add_mm_counter(tlb->mm, mm_counter_file(page), -HPAGE_PMD_NR); |
b5072380 | 1649 | } |
616b8371 | 1650 | |
da146769 | 1651 | spin_unlock(ptl); |
616b8371 ZY |
1652 | if (flush_needed) |
1653 | tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE); | |
025c5b24 | 1654 | } |
da146769 | 1655 | return 1; |
71e3aac0 AA |
1656 | } |
1657 | ||
1dd38b6c AK |
1658 | #ifndef pmd_move_must_withdraw |
1659 | static inline int pmd_move_must_withdraw(spinlock_t *new_pmd_ptl, | |
1660 | spinlock_t *old_pmd_ptl, | |
1661 | struct vm_area_struct *vma) | |
1662 | { | |
1663 | /* | |
1664 | * With split pmd lock we also need to move preallocated | |
1665 | * PTE page table if new_pmd is on different PMD page table. | |
1666 | * | |
1667 | * We also don't deposit and withdraw tables for file pages. | |
1668 | */ | |
1669 | return (new_pmd_ptl != old_pmd_ptl) && vma_is_anonymous(vma); | |
1670 | } | |
1671 | #endif | |
1672 | ||
ab6e3d09 NH |
1673 | static pmd_t move_soft_dirty_pmd(pmd_t pmd) |
1674 | { | |
1675 | #ifdef CONFIG_MEM_SOFT_DIRTY | |
1676 | if (unlikely(is_pmd_migration_entry(pmd))) | |
1677 | pmd = pmd_swp_mksoft_dirty(pmd); | |
1678 | else if (pmd_present(pmd)) | |
1679 | pmd = pmd_mksoft_dirty(pmd); | |
1680 | #endif | |
1681 | return pmd; | |
1682 | } | |
1683 | ||
bf8616d5 | 1684 | bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, |
b8aa9d9d | 1685 | unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd) |
37a1c49a | 1686 | { |
bf929152 | 1687 | spinlock_t *old_ptl, *new_ptl; |
37a1c49a | 1688 | pmd_t pmd; |
37a1c49a | 1689 | struct mm_struct *mm = vma->vm_mm; |
5d190420 | 1690 | bool force_flush = false; |
37a1c49a | 1691 | |
37a1c49a AA |
1692 | /* |
1693 | * The destination pmd shouldn't be established, free_pgtables() | |
1694 | * should have release it. | |
1695 | */ | |
1696 | if (WARN_ON(!pmd_none(*new_pmd))) { | |
1697 | VM_BUG_ON(pmd_trans_huge(*new_pmd)); | |
4b471e88 | 1698 | return false; |
37a1c49a AA |
1699 | } |
1700 | ||
bf929152 KS |
1701 | /* |
1702 | * We don't have to worry about the ordering of src and dst | |
c1e8d7c6 | 1703 | * ptlocks because exclusive mmap_lock prevents deadlock. |
bf929152 | 1704 | */ |
b6ec57f4 KS |
1705 | old_ptl = __pmd_trans_huge_lock(old_pmd, vma); |
1706 | if (old_ptl) { | |
bf929152 KS |
1707 | new_ptl = pmd_lockptr(mm, new_pmd); |
1708 | if (new_ptl != old_ptl) | |
1709 | spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); | |
8809aa2d | 1710 | pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); |
eb66ae03 | 1711 | if (pmd_present(pmd)) |
a2ce2666 | 1712 | force_flush = true; |
025c5b24 | 1713 | VM_BUG_ON(!pmd_none(*new_pmd)); |
3592806c | 1714 | |
1dd38b6c | 1715 | if (pmd_move_must_withdraw(new_ptl, old_ptl, vma)) { |
b3084f4d | 1716 | pgtable_t pgtable; |
3592806c KS |
1717 | pgtable = pgtable_trans_huge_withdraw(mm, old_pmd); |
1718 | pgtable_trans_huge_deposit(mm, new_pmd, pgtable); | |
3592806c | 1719 | } |
ab6e3d09 NH |
1720 | pmd = move_soft_dirty_pmd(pmd); |
1721 | set_pmd_at(mm, new_addr, new_pmd, pmd); | |
5d190420 AL |
1722 | if (force_flush) |
1723 | flush_tlb_range(vma, old_addr, old_addr + PMD_SIZE); | |
eb66ae03 LT |
1724 | if (new_ptl != old_ptl) |
1725 | spin_unlock(new_ptl); | |
bf929152 | 1726 | spin_unlock(old_ptl); |
4b471e88 | 1727 | return true; |
37a1c49a | 1728 | } |
4b471e88 | 1729 | return false; |
37a1c49a AA |
1730 | } |
1731 | ||
f123d74a MG |
1732 | /* |
1733 | * Returns | |
1734 | * - 0 if PMD could not be locked | |
f0953a1b | 1735 | * - 1 if PMD was locked but protections unchanged and TLB flush unnecessary |
e346e668 | 1736 | * or if prot_numa but THP migration is not supported |
f0953a1b | 1737 | * - HPAGE_PMD_NR if protections changed and TLB flush necessary |
f123d74a | 1738 | */ |
4a18419f NA |
1739 | int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
1740 | pmd_t *pmd, unsigned long addr, pgprot_t newprot, | |
1741 | unsigned long cp_flags) | |
cd7548ab JW |
1742 | { |
1743 | struct mm_struct *mm = vma->vm_mm; | |
bf929152 | 1744 | spinlock_t *ptl; |
c9fe6656 | 1745 | pmd_t oldpmd, entry; |
0a85e51d KS |
1746 | bool preserve_write; |
1747 | int ret; | |
58705444 | 1748 | bool prot_numa = cp_flags & MM_CP_PROT_NUMA; |
292924b2 PX |
1749 | bool uffd_wp = cp_flags & MM_CP_UFFD_WP; |
1750 | bool uffd_wp_resolve = cp_flags & MM_CP_UFFD_WP_RESOLVE; | |
cd7548ab | 1751 | |
4a18419f NA |
1752 | tlb_change_page_size(tlb, HPAGE_PMD_SIZE); |
1753 | ||
e346e668 YS |
1754 | if (prot_numa && !thp_migration_supported()) |
1755 | return 1; | |
1756 | ||
b6ec57f4 | 1757 | ptl = __pmd_trans_huge_lock(pmd, vma); |
0a85e51d KS |
1758 | if (!ptl) |
1759 | return 0; | |
e944fd67 | 1760 | |
0a85e51d KS |
1761 | preserve_write = prot_numa && pmd_write(*pmd); |
1762 | ret = 1; | |
e944fd67 | 1763 | |
84c3fc4e ZY |
1764 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION |
1765 | if (is_swap_pmd(*pmd)) { | |
1766 | swp_entry_t entry = pmd_to_swp_entry(*pmd); | |
6c287605 | 1767 | struct page *page = pfn_swap_entry_to_page(entry); |
84c3fc4e ZY |
1768 | |
1769 | VM_BUG_ON(!is_pmd_migration_entry(*pmd)); | |
4dd845b5 | 1770 | if (is_writable_migration_entry(entry)) { |
84c3fc4e ZY |
1771 | pmd_t newpmd; |
1772 | /* | |
1773 | * A protection check is difficult so | |
1774 | * just be safe and disable write | |
1775 | */ | |
6c287605 DH |
1776 | if (PageAnon(page)) |
1777 | entry = make_readable_exclusive_migration_entry(swp_offset(entry)); | |
1778 | else | |
1779 | entry = make_readable_migration_entry(swp_offset(entry)); | |
84c3fc4e | 1780 | newpmd = swp_entry_to_pmd(entry); |
ab6e3d09 NH |
1781 | if (pmd_swp_soft_dirty(*pmd)) |
1782 | newpmd = pmd_swp_mksoft_dirty(newpmd); | |
8f34f1ea PX |
1783 | if (pmd_swp_uffd_wp(*pmd)) |
1784 | newpmd = pmd_swp_mkuffd_wp(newpmd); | |
84c3fc4e ZY |
1785 | set_pmd_at(mm, addr, pmd, newpmd); |
1786 | } | |
1787 | goto unlock; | |
1788 | } | |
1789 | #endif | |
1790 | ||
a1a3a2fc HY |
1791 | if (prot_numa) { |
1792 | struct page *page; | |
1793 | /* | |
1794 | * Avoid trapping faults against the zero page. The read-only | |
1795 | * data is likely to be read-cached on the local CPU and | |
1796 | * local/remote hits to the zero page are not interesting. | |
1797 | */ | |
1798 | if (is_huge_zero_pmd(*pmd)) | |
1799 | goto unlock; | |
025c5b24 | 1800 | |
a1a3a2fc HY |
1801 | if (pmd_protnone(*pmd)) |
1802 | goto unlock; | |
0a85e51d | 1803 | |
a1a3a2fc HY |
1804 | page = pmd_page(*pmd); |
1805 | /* | |
1806 | * Skip scanning top tier node if normal numa | |
1807 | * balancing is disabled | |
1808 | */ | |
1809 | if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_NORMAL) && | |
1810 | node_is_toptier(page_to_nid(page))) | |
1811 | goto unlock; | |
1812 | } | |
ced10803 | 1813 | /* |
3e4e28c5 | 1814 | * In case prot_numa, we are under mmap_read_lock(mm). It's critical |
ced10803 | 1815 | * to not clear pmd intermittently to avoid race with MADV_DONTNEED |
3e4e28c5 | 1816 | * which is also under mmap_read_lock(mm): |
ced10803 KS |
1817 | * |
1818 | * CPU0: CPU1: | |
1819 | * change_huge_pmd(prot_numa=1) | |
1820 | * pmdp_huge_get_and_clear_notify() | |
1821 | * madvise_dontneed() | |
1822 | * zap_pmd_range() | |
1823 | * pmd_trans_huge(*pmd) == 0 (without ptl) | |
1824 | * // skip the pmd | |
1825 | * set_pmd_at(); | |
1826 | * // pmd is re-established | |
1827 | * | |
1828 | * The race makes MADV_DONTNEED miss the huge pmd and don't clear it | |
1829 | * which may break userspace. | |
1830 | * | |
4f831457 | 1831 | * pmdp_invalidate_ad() is required to make sure we don't miss |
ced10803 KS |
1832 | * dirty/young flags set by hardware. |
1833 | */ | |
4f831457 | 1834 | oldpmd = pmdp_invalidate_ad(vma, addr, pmd); |
ced10803 | 1835 | |
c9fe6656 | 1836 | entry = pmd_modify(oldpmd, newprot); |
0a85e51d KS |
1837 | if (preserve_write) |
1838 | entry = pmd_mk_savedwrite(entry); | |
292924b2 PX |
1839 | if (uffd_wp) { |
1840 | entry = pmd_wrprotect(entry); | |
1841 | entry = pmd_mkuffd_wp(entry); | |
1842 | } else if (uffd_wp_resolve) { | |
1843 | /* | |
1844 | * Leave the write bit to be handled by PF interrupt | |
1845 | * handler, then things like COW could be properly | |
1846 | * handled. | |
1847 | */ | |
1848 | entry = pmd_clear_uffd_wp(entry); | |
1849 | } | |
0a85e51d KS |
1850 | ret = HPAGE_PMD_NR; |
1851 | set_pmd_at(mm, addr, pmd, entry); | |
4a18419f | 1852 | |
c9fe6656 NA |
1853 | if (huge_pmd_needs_flush(oldpmd, entry)) |
1854 | tlb_flush_pmd_range(tlb, addr, HPAGE_PMD_SIZE); | |
4a18419f | 1855 | |
0a85e51d KS |
1856 | BUG_ON(vma_is_anonymous(vma) && !preserve_write && pmd_write(entry)); |
1857 | unlock: | |
1858 | spin_unlock(ptl); | |
025c5b24 NH |
1859 | return ret; |
1860 | } | |
1861 | ||
1862 | /* | |
8f19b0c0 | 1863 | * Returns page table lock pointer if a given pmd maps a thp, NULL otherwise. |
025c5b24 | 1864 | * |
8f19b0c0 HY |
1865 | * Note that if it returns page table lock pointer, this routine returns without |
1866 | * unlocking page table lock. So callers must unlock it. | |
025c5b24 | 1867 | */ |
b6ec57f4 | 1868 | spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) |
025c5b24 | 1869 | { |
b6ec57f4 KS |
1870 | spinlock_t *ptl; |
1871 | ptl = pmd_lock(vma->vm_mm, pmd); | |
84c3fc4e ZY |
1872 | if (likely(is_swap_pmd(*pmd) || pmd_trans_huge(*pmd) || |
1873 | pmd_devmap(*pmd))) | |
b6ec57f4 KS |
1874 | return ptl; |
1875 | spin_unlock(ptl); | |
1876 | return NULL; | |
cd7548ab JW |
1877 | } |
1878 | ||
a00cc7d9 MW |
1879 | /* |
1880 | * Returns true if a given pud maps a thp, false otherwise. | |
1881 | * | |
1882 | * Note that if it returns true, this routine returns without unlocking page | |
1883 | * table lock. So callers must unlock it. | |
1884 | */ | |
1885 | spinlock_t *__pud_trans_huge_lock(pud_t *pud, struct vm_area_struct *vma) | |
1886 | { | |
1887 | spinlock_t *ptl; | |
1888 | ||
1889 | ptl = pud_lock(vma->vm_mm, pud); | |
1890 | if (likely(pud_trans_huge(*pud) || pud_devmap(*pud))) | |
1891 | return ptl; | |
1892 | spin_unlock(ptl); | |
1893 | return NULL; | |
1894 | } | |
1895 | ||
1896 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD | |
1897 | int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, | |
1898 | pud_t *pud, unsigned long addr) | |
1899 | { | |
a00cc7d9 MW |
1900 | spinlock_t *ptl; |
1901 | ||
1902 | ptl = __pud_trans_huge_lock(pud, vma); | |
1903 | if (!ptl) | |
1904 | return 0; | |
1905 | /* | |
1906 | * For architectures like ppc64 we look at deposited pgtable | |
1907 | * when calling pudp_huge_get_and_clear. So do the | |
1908 | * pgtable_trans_huge_withdraw after finishing pudp related | |
1909 | * operations. | |
1910 | */ | |
70516b93 | 1911 | pudp_huge_get_and_clear_full(tlb->mm, addr, pud, tlb->fullmm); |
a00cc7d9 | 1912 | tlb_remove_pud_tlb_entry(tlb, pud, addr); |
2484ca9b | 1913 | if (vma_is_special_huge(vma)) { |
a00cc7d9 MW |
1914 | spin_unlock(ptl); |
1915 | /* No zero page support yet */ | |
1916 | } else { | |
1917 | /* No support for anonymous PUD pages yet */ | |
1918 | BUG(); | |
1919 | } | |
1920 | return 1; | |
1921 | } | |
1922 | ||
1923 | static void __split_huge_pud_locked(struct vm_area_struct *vma, pud_t *pud, | |
1924 | unsigned long haddr) | |
1925 | { | |
1926 | VM_BUG_ON(haddr & ~HPAGE_PUD_MASK); | |
1927 | VM_BUG_ON_VMA(vma->vm_start > haddr, vma); | |
1928 | VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PUD_SIZE, vma); | |
1929 | VM_BUG_ON(!pud_trans_huge(*pud) && !pud_devmap(*pud)); | |
1930 | ||
ce9311cf | 1931 | count_vm_event(THP_SPLIT_PUD); |
a00cc7d9 MW |
1932 | |
1933 | pudp_huge_clear_flush_notify(vma, haddr, pud); | |
1934 | } | |
1935 | ||
1936 | void __split_huge_pud(struct vm_area_struct *vma, pud_t *pud, | |
1937 | unsigned long address) | |
1938 | { | |
1939 | spinlock_t *ptl; | |
ac46d4f3 | 1940 | struct mmu_notifier_range range; |
a00cc7d9 | 1941 | |
7269f999 | 1942 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
6f4f13e8 | 1943 | address & HPAGE_PUD_MASK, |
ac46d4f3 JG |
1944 | (address & HPAGE_PUD_MASK) + HPAGE_PUD_SIZE); |
1945 | mmu_notifier_invalidate_range_start(&range); | |
1946 | ptl = pud_lock(vma->vm_mm, pud); | |
a00cc7d9 MW |
1947 | if (unlikely(!pud_trans_huge(*pud) && !pud_devmap(*pud))) |
1948 | goto out; | |
ac46d4f3 | 1949 | __split_huge_pud_locked(vma, pud, range.start); |
a00cc7d9 MW |
1950 | |
1951 | out: | |
1952 | spin_unlock(ptl); | |
4645b9fe JG |
1953 | /* |
1954 | * No need to double call mmu_notifier->invalidate_range() callback as | |
1955 | * the above pudp_huge_clear_flush_notify() did already call it. | |
1956 | */ | |
ac46d4f3 | 1957 | mmu_notifier_invalidate_range_only_end(&range); |
a00cc7d9 MW |
1958 | } |
1959 | #endif /* CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ | |
1960 | ||
eef1b3ba KS |
1961 | static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, |
1962 | unsigned long haddr, pmd_t *pmd) | |
1963 | { | |
1964 | struct mm_struct *mm = vma->vm_mm; | |
1965 | pgtable_t pgtable; | |
1966 | pmd_t _pmd; | |
1967 | int i; | |
1968 | ||
0f10851e JG |
1969 | /* |
1970 | * Leave pmd empty until pte is filled note that it is fine to delay | |
1971 | * notification until mmu_notifier_invalidate_range_end() as we are | |
1972 | * replacing a zero pmd write protected page with a zero pte write | |
1973 | * protected page. | |
1974 | * | |
ee65728e | 1975 | * See Documentation/mm/mmu_notifier.rst |
0f10851e JG |
1976 | */ |
1977 | pmdp_huge_clear_flush(vma, haddr, pmd); | |
eef1b3ba KS |
1978 | |
1979 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); | |
1980 | pmd_populate(mm, &_pmd, pgtable); | |
1981 | ||
1982 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { | |
1983 | pte_t *pte, entry; | |
1984 | entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot); | |
1985 | entry = pte_mkspecial(entry); | |
1986 | pte = pte_offset_map(&_pmd, haddr); | |
1987 | VM_BUG_ON(!pte_none(*pte)); | |
1988 | set_pte_at(mm, haddr, pte, entry); | |
1989 | pte_unmap(pte); | |
1990 | } | |
1991 | smp_wmb(); /* make pte visible before pmd */ | |
1992 | pmd_populate(mm, pmd, pgtable); | |
eef1b3ba KS |
1993 | } |
1994 | ||
1995 | static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, | |
ba988280 | 1996 | unsigned long haddr, bool freeze) |
eef1b3ba KS |
1997 | { |
1998 | struct mm_struct *mm = vma->vm_mm; | |
1999 | struct page *page; | |
2000 | pgtable_t pgtable; | |
423ac9af | 2001 | pmd_t old_pmd, _pmd; |
292924b2 | 2002 | bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; |
6c287605 | 2003 | bool anon_exclusive = false; |
2ac015e2 | 2004 | unsigned long addr; |
eef1b3ba KS |
2005 | int i; |
2006 | ||
2007 | VM_BUG_ON(haddr & ~HPAGE_PMD_MASK); | |
2008 | VM_BUG_ON_VMA(vma->vm_start > haddr, vma); | |
2009 | VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma); | |
84c3fc4e ZY |
2010 | VM_BUG_ON(!is_pmd_migration_entry(*pmd) && !pmd_trans_huge(*pmd) |
2011 | && !pmd_devmap(*pmd)); | |
eef1b3ba KS |
2012 | |
2013 | count_vm_event(THP_SPLIT_PMD); | |
2014 | ||
d21b9e57 | 2015 | if (!vma_is_anonymous(vma)) { |
99fa8a48 | 2016 | old_pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd); |
953c66c2 AK |
2017 | /* |
2018 | * We are going to unmap this huge page. So | |
2019 | * just go ahead and zap it | |
2020 | */ | |
2021 | if (arch_needs_pgtable_deposit()) | |
2022 | zap_deposited_table(mm, pmd); | |
2484ca9b | 2023 | if (vma_is_special_huge(vma)) |
d21b9e57 | 2024 | return; |
99fa8a48 HD |
2025 | if (unlikely(is_pmd_migration_entry(old_pmd))) { |
2026 | swp_entry_t entry; | |
2027 | ||
2028 | entry = pmd_to_swp_entry(old_pmd); | |
af5cdaf8 | 2029 | page = pfn_swap_entry_to_page(entry); |
99fa8a48 HD |
2030 | } else { |
2031 | page = pmd_page(old_pmd); | |
2032 | if (!PageDirty(page) && pmd_dirty(old_pmd)) | |
2033 | set_page_dirty(page); | |
2034 | if (!PageReferenced(page) && pmd_young(old_pmd)) | |
2035 | SetPageReferenced(page); | |
cea86fe2 | 2036 | page_remove_rmap(page, vma, true); |
99fa8a48 HD |
2037 | put_page(page); |
2038 | } | |
fadae295 | 2039 | add_mm_counter(mm, mm_counter_file(page), -HPAGE_PMD_NR); |
eef1b3ba | 2040 | return; |
99fa8a48 HD |
2041 | } |
2042 | ||
3b77e8c8 | 2043 | if (is_huge_zero_pmd(*pmd)) { |
4645b9fe JG |
2044 | /* |
2045 | * FIXME: Do we want to invalidate secondary mmu by calling | |
2046 | * mmu_notifier_invalidate_range() see comments below inside | |
2047 | * __split_huge_pmd() ? | |
2048 | * | |
2049 | * We are going from a zero huge page write protected to zero | |
2050 | * small page also write protected so it does not seems useful | |
2051 | * to invalidate secondary mmu at this time. | |
2052 | */ | |
eef1b3ba KS |
2053 | return __split_huge_zero_page_pmd(vma, haddr, pmd); |
2054 | } | |
2055 | ||
423ac9af AK |
2056 | /* |
2057 | * Up to this point the pmd is present and huge and userland has the | |
2058 | * whole access to the hugepage during the split (which happens in | |
2059 | * place). If we overwrite the pmd with the not-huge version pointing | |
2060 | * to the pte here (which of course we could if all CPUs were bug | |
2061 | * free), userland could trigger a small page size TLB miss on the | |
2062 | * small sized TLB while the hugepage TLB entry is still established in | |
2063 | * the huge TLB. Some CPU doesn't like that. | |
42742d9b AK |
2064 | * See http://support.amd.com/TechDocs/41322_10h_Rev_Gd.pdf, Erratum |
2065 | * 383 on page 105. Intel should be safe but is also warns that it's | |
423ac9af AK |
2066 | * only safe if the permission and cache attributes of the two entries |
2067 | * loaded in the two TLB is identical (which should be the case here). | |
2068 | * But it is generally safer to never allow small and huge TLB entries | |
2069 | * for the same virtual address to be loaded simultaneously. So instead | |
2070 | * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the | |
2071 | * current pmd notpresent (atomically because here the pmd_trans_huge | |
2072 | * must remain set at all times on the pmd until the split is complete | |
2073 | * for this pmd), then we flush the SMP TLB and finally we write the | |
2074 | * non-huge version of the pmd entry with pmd_populate. | |
2075 | */ | |
2076 | old_pmd = pmdp_invalidate(vma, haddr, pmd); | |
2077 | ||
423ac9af | 2078 | pmd_migration = is_pmd_migration_entry(old_pmd); |
2e83ee1d | 2079 | if (unlikely(pmd_migration)) { |
84c3fc4e ZY |
2080 | swp_entry_t entry; |
2081 | ||
423ac9af | 2082 | entry = pmd_to_swp_entry(old_pmd); |
af5cdaf8 | 2083 | page = pfn_swap_entry_to_page(entry); |
4dd845b5 | 2084 | write = is_writable_migration_entry(entry); |
6c287605 DH |
2085 | if (PageAnon(page)) |
2086 | anon_exclusive = is_readable_exclusive_migration_entry(entry); | |
2e83ee1d PX |
2087 | young = false; |
2088 | soft_dirty = pmd_swp_soft_dirty(old_pmd); | |
f45ec5ff | 2089 | uffd_wp = pmd_swp_uffd_wp(old_pmd); |
2e83ee1d | 2090 | } else { |
423ac9af | 2091 | page = pmd_page(old_pmd); |
2e83ee1d PX |
2092 | if (pmd_dirty(old_pmd)) |
2093 | SetPageDirty(page); | |
2094 | write = pmd_write(old_pmd); | |
2095 | young = pmd_young(old_pmd); | |
2096 | soft_dirty = pmd_soft_dirty(old_pmd); | |
292924b2 | 2097 | uffd_wp = pmd_uffd_wp(old_pmd); |
6c287605 | 2098 | |
9d84604b HD |
2099 | VM_BUG_ON_PAGE(!page_count(page), page); |
2100 | page_ref_add(page, HPAGE_PMD_NR - 1); | |
6c287605 DH |
2101 | |
2102 | /* | |
2103 | * Without "freeze", we'll simply split the PMD, propagating the | |
2104 | * PageAnonExclusive() flag for each PTE by setting it for | |
2105 | * each subpage -- no need to (temporarily) clear. | |
2106 | * | |
2107 | * With "freeze" we want to replace mapped pages by | |
2108 | * migration entries right away. This is only possible if we | |
2109 | * managed to clear PageAnonExclusive() -- see | |
2110 | * set_pmd_migration_entry(). | |
2111 | * | |
2112 | * In case we cannot clear PageAnonExclusive(), split the PMD | |
2113 | * only and let try_to_migrate_one() fail later. | |
2114 | */ | |
2115 | anon_exclusive = PageAnon(page) && PageAnonExclusive(page); | |
2116 | if (freeze && anon_exclusive && page_try_share_anon_rmap(page)) | |
2117 | freeze = false; | |
2e83ee1d | 2118 | } |
eef1b3ba | 2119 | |
423ac9af AK |
2120 | /* |
2121 | * Withdraw the table only after we mark the pmd entry invalid. | |
2122 | * This's critical for some architectures (Power). | |
2123 | */ | |
eef1b3ba KS |
2124 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
2125 | pmd_populate(mm, &_pmd, pgtable); | |
2126 | ||
2ac015e2 | 2127 | for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { |
eef1b3ba KS |
2128 | pte_t entry, *pte; |
2129 | /* | |
2130 | * Note that NUMA hinting access restrictions are not | |
2131 | * transferred to avoid any possibility of altering | |
2132 | * permissions across VMAs. | |
2133 | */ | |
84c3fc4e | 2134 | if (freeze || pmd_migration) { |
ba988280 | 2135 | swp_entry_t swp_entry; |
4dd845b5 AP |
2136 | if (write) |
2137 | swp_entry = make_writable_migration_entry( | |
2138 | page_to_pfn(page + i)); | |
6c287605 DH |
2139 | else if (anon_exclusive) |
2140 | swp_entry = make_readable_exclusive_migration_entry( | |
2141 | page_to_pfn(page + i)); | |
4dd845b5 AP |
2142 | else |
2143 | swp_entry = make_readable_migration_entry( | |
2144 | page_to_pfn(page + i)); | |
ba988280 | 2145 | entry = swp_entry_to_pte(swp_entry); |
804dd150 AA |
2146 | if (soft_dirty) |
2147 | entry = pte_swp_mksoft_dirty(entry); | |
f45ec5ff PX |
2148 | if (uffd_wp) |
2149 | entry = pte_swp_mkuffd_wp(entry); | |
ba988280 | 2150 | } else { |
6d2329f8 | 2151 | entry = mk_pte(page + i, READ_ONCE(vma->vm_page_prot)); |
b8d3c4c3 | 2152 | entry = maybe_mkwrite(entry, vma); |
6c287605 DH |
2153 | if (anon_exclusive) |
2154 | SetPageAnonExclusive(page + i); | |
ba988280 KS |
2155 | if (!write) |
2156 | entry = pte_wrprotect(entry); | |
2157 | if (!young) | |
2158 | entry = pte_mkold(entry); | |
804dd150 AA |
2159 | if (soft_dirty) |
2160 | entry = pte_mksoft_dirty(entry); | |
292924b2 PX |
2161 | if (uffd_wp) |
2162 | entry = pte_mkuffd_wp(entry); | |
ba988280 | 2163 | } |
2ac015e2 | 2164 | pte = pte_offset_map(&_pmd, addr); |
eef1b3ba | 2165 | BUG_ON(!pte_none(*pte)); |
2ac015e2 | 2166 | set_pte_at(mm, addr, pte, entry); |
ec0abae6 | 2167 | if (!pmd_migration) |
eef1b3ba | 2168 | atomic_inc(&page[i]._mapcount); |
ec0abae6 | 2169 | pte_unmap(pte); |
eef1b3ba KS |
2170 | } |
2171 | ||
ec0abae6 RC |
2172 | if (!pmd_migration) { |
2173 | /* | |
2174 | * Set PG_double_map before dropping compound_mapcount to avoid | |
2175 | * false-negative page_mapped(). | |
2176 | */ | |
2177 | if (compound_mapcount(page) > 1 && | |
2178 | !TestSetPageDoubleMap(page)) { | |
eef1b3ba | 2179 | for (i = 0; i < HPAGE_PMD_NR; i++) |
ec0abae6 RC |
2180 | atomic_inc(&page[i]._mapcount); |
2181 | } | |
2182 | ||
2183 | lock_page_memcg(page); | |
2184 | if (atomic_add_negative(-1, compound_mapcount_ptr(page))) { | |
2185 | /* Last compound_mapcount is gone. */ | |
69473e5d MS |
2186 | __mod_lruvec_page_state(page, NR_ANON_THPS, |
2187 | -HPAGE_PMD_NR); | |
ec0abae6 RC |
2188 | if (TestClearPageDoubleMap(page)) { |
2189 | /* No need in mapcount reference anymore */ | |
2190 | for (i = 0; i < HPAGE_PMD_NR; i++) | |
2191 | atomic_dec(&page[i]._mapcount); | |
2192 | } | |
eef1b3ba | 2193 | } |
ec0abae6 | 2194 | unlock_page_memcg(page); |
cea86fe2 HD |
2195 | |
2196 | /* Above is effectively page_remove_rmap(page, vma, true) */ | |
2197 | munlock_vma_page(page, vma, true); | |
eef1b3ba KS |
2198 | } |
2199 | ||
2200 | smp_wmb(); /* make pte visible before pmd */ | |
2201 | pmd_populate(mm, pmd, pgtable); | |
e9b61f19 KS |
2202 | |
2203 | if (freeze) { | |
2ac015e2 | 2204 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
cea86fe2 | 2205 | page_remove_rmap(page + i, vma, false); |
e9b61f19 KS |
2206 | put_page(page + i); |
2207 | } | |
2208 | } | |
eef1b3ba KS |
2209 | } |
2210 | ||
2211 | void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, | |
af28a988 | 2212 | unsigned long address, bool freeze, struct folio *folio) |
eef1b3ba KS |
2213 | { |
2214 | spinlock_t *ptl; | |
ac46d4f3 | 2215 | struct mmu_notifier_range range; |
eef1b3ba | 2216 | |
7269f999 | 2217 | mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma, vma->vm_mm, |
6f4f13e8 | 2218 | address & HPAGE_PMD_MASK, |
ac46d4f3 JG |
2219 | (address & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE); |
2220 | mmu_notifier_invalidate_range_start(&range); | |
2221 | ptl = pmd_lock(vma->vm_mm, pmd); | |
33f4751e NH |
2222 | |
2223 | /* | |
af28a988 MWO |
2224 | * If caller asks to setup a migration entry, we need a folio to check |
2225 | * pmd against. Otherwise we can end up replacing wrong folio. | |
33f4751e | 2226 | */ |
af28a988 | 2227 | VM_BUG_ON(freeze && !folio); |
83a8441f | 2228 | VM_WARN_ON_ONCE(folio && !folio_test_locked(folio)); |
33f4751e | 2229 | |
7f760917 | 2230 | if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd) || |
83a8441f MWO |
2231 | is_pmd_migration_entry(*pmd)) { |
2232 | if (folio && folio != page_folio(pmd_page(*pmd))) | |
2233 | goto out; | |
7f760917 | 2234 | __split_huge_pmd_locked(vma, pmd, range.start, freeze); |
83a8441f | 2235 | } |
7f760917 | 2236 | |
e90309c9 | 2237 | out: |
eef1b3ba | 2238 | spin_unlock(ptl); |
4645b9fe JG |
2239 | /* |
2240 | * No need to double call mmu_notifier->invalidate_range() callback. | |
2241 | * They are 3 cases to consider inside __split_huge_pmd_locked(): | |
2242 | * 1) pmdp_huge_clear_flush_notify() call invalidate_range() obvious | |
2243 | * 2) __split_huge_zero_page_pmd() read only zero page and any write | |
2244 | * fault will trigger a flush_notify before pointing to a new page | |
2245 | * (it is fine if the secondary mmu keeps pointing to the old zero | |
2246 | * page in the meantime) | |
2247 | * 3) Split a huge pmd into pte pointing to the same page. No need | |
2248 | * to invalidate secondary tlb entry they are all still valid. | |
2249 | * any further changes to individual pte will notify. So no need | |
2250 | * to call mmu_notifier->invalidate_range() | |
2251 | */ | |
ac46d4f3 | 2252 | mmu_notifier_invalidate_range_only_end(&range); |
eef1b3ba KS |
2253 | } |
2254 | ||
fec89c10 | 2255 | void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, |
af28a988 | 2256 | bool freeze, struct folio *folio) |
94fcc585 | 2257 | { |
f72e7dcd | 2258 | pgd_t *pgd; |
c2febafc | 2259 | p4d_t *p4d; |
f72e7dcd | 2260 | pud_t *pud; |
94fcc585 AA |
2261 | pmd_t *pmd; |
2262 | ||
78ddc534 | 2263 | pgd = pgd_offset(vma->vm_mm, address); |
f72e7dcd HD |
2264 | if (!pgd_present(*pgd)) |
2265 | return; | |
2266 | ||
c2febafc KS |
2267 | p4d = p4d_offset(pgd, address); |
2268 | if (!p4d_present(*p4d)) | |
2269 | return; | |
2270 | ||
2271 | pud = pud_offset(p4d, address); | |
f72e7dcd HD |
2272 | if (!pud_present(*pud)) |
2273 | return; | |
2274 | ||
2275 | pmd = pmd_offset(pud, address); | |
fec89c10 | 2276 | |
af28a988 | 2277 | __split_huge_pmd(vma, pmd, address, freeze, folio); |
94fcc585 AA |
2278 | } |
2279 | ||
71f9e58e ML |
2280 | static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address) |
2281 | { | |
2282 | /* | |
2283 | * If the new address isn't hpage aligned and it could previously | |
2284 | * contain an hugepage: check if we need to split an huge pmd. | |
2285 | */ | |
2286 | if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) && | |
2287 | range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE), | |
2288 | ALIGN(address, HPAGE_PMD_SIZE))) | |
2289 | split_huge_pmd_address(vma, address, false, NULL); | |
2290 | } | |
2291 | ||
e1b9996b | 2292 | void vma_adjust_trans_huge(struct vm_area_struct *vma, |
94fcc585 AA |
2293 | unsigned long start, |
2294 | unsigned long end, | |
2295 | long adjust_next) | |
2296 | { | |
71f9e58e ML |
2297 | /* Check if we need to split start first. */ |
2298 | split_huge_pmd_if_needed(vma, start); | |
94fcc585 | 2299 | |
71f9e58e ML |
2300 | /* Check if we need to split end next. */ |
2301 | split_huge_pmd_if_needed(vma, end); | |
94fcc585 AA |
2302 | |
2303 | /* | |
71f9e58e ML |
2304 | * If we're also updating the vma->vm_next->vm_start, |
2305 | * check if we need to split it. | |
94fcc585 AA |
2306 | */ |
2307 | if (adjust_next > 0) { | |
2308 | struct vm_area_struct *next = vma->vm_next; | |
2309 | unsigned long nstart = next->vm_start; | |
f9d86a60 | 2310 | nstart += adjust_next; |
71f9e58e | 2311 | split_huge_pmd_if_needed(next, nstart); |
94fcc585 AA |
2312 | } |
2313 | } | |
e9b61f19 | 2314 | |
906f9cdf | 2315 | static void unmap_page(struct page *page) |
e9b61f19 | 2316 | { |
869f7ee6 | 2317 | struct folio *folio = page_folio(page); |
a98a2f0c AP |
2318 | enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | |
2319 | TTU_SYNC; | |
e9b61f19 KS |
2320 | |
2321 | VM_BUG_ON_PAGE(!PageHead(page), page); | |
2322 | ||
a98a2f0c AP |
2323 | /* |
2324 | * Anon pages need migration entries to preserve them, but file | |
2325 | * pages can simply be left unmapped, then faulted back on demand. | |
2326 | * If that is ever changed (perhaps for mlock), update remap_page(). | |
2327 | */ | |
4b8554c5 MWO |
2328 | if (folio_test_anon(folio)) |
2329 | try_to_migrate(folio, ttu_flags); | |
a98a2f0c | 2330 | else |
869f7ee6 | 2331 | try_to_unmap(folio, ttu_flags | TTU_IGNORE_MLOCK); |
e9b61f19 KS |
2332 | } |
2333 | ||
4eecb8b9 | 2334 | static void remap_page(struct folio *folio, unsigned long nr) |
e9b61f19 | 2335 | { |
4eecb8b9 | 2336 | int i = 0; |
ab02c252 | 2337 | |
64b586d1 | 2338 | /* If unmap_page() uses try_to_migrate() on file, remove this check */ |
4eecb8b9 | 2339 | if (!folio_test_anon(folio)) |
ab02c252 | 2340 | return; |
4eecb8b9 MWO |
2341 | for (;;) { |
2342 | remove_migration_ptes(folio, folio, true); | |
2343 | i += folio_nr_pages(folio); | |
2344 | if (i >= nr) | |
2345 | break; | |
2346 | folio = folio_next(folio); | |
ace71a19 | 2347 | } |
e9b61f19 KS |
2348 | } |
2349 | ||
94866635 | 2350 | static void lru_add_page_tail(struct page *head, struct page *tail, |
88dcb9a3 AS |
2351 | struct lruvec *lruvec, struct list_head *list) |
2352 | { | |
94866635 AS |
2353 | VM_BUG_ON_PAGE(!PageHead(head), head); |
2354 | VM_BUG_ON_PAGE(PageCompound(tail), head); | |
2355 | VM_BUG_ON_PAGE(PageLRU(tail), head); | |
6168d0da | 2356 | lockdep_assert_held(&lruvec->lru_lock); |
88dcb9a3 | 2357 | |
6dbb5741 | 2358 | if (list) { |
88dcb9a3 | 2359 | /* page reclaim is reclaiming a huge page */ |
6dbb5741 | 2360 | VM_WARN_ON(PageLRU(head)); |
94866635 AS |
2361 | get_page(tail); |
2362 | list_add_tail(&tail->lru, list); | |
88dcb9a3 | 2363 | } else { |
6dbb5741 AS |
2364 | /* head is still on lru (and we have it frozen) */ |
2365 | VM_WARN_ON(!PageLRU(head)); | |
07ca7606 HD |
2366 | if (PageUnevictable(tail)) |
2367 | tail->mlock_count = 0; | |
2368 | else | |
2369 | list_add_tail(&tail->lru, &head->lru); | |
6dbb5741 | 2370 | SetPageLRU(tail); |
88dcb9a3 AS |
2371 | } |
2372 | } | |
2373 | ||
8df651c7 | 2374 | static void __split_huge_page_tail(struct page *head, int tail, |
e9b61f19 KS |
2375 | struct lruvec *lruvec, struct list_head *list) |
2376 | { | |
e9b61f19 KS |
2377 | struct page *page_tail = head + tail; |
2378 | ||
8df651c7 | 2379 | VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); |
e9b61f19 KS |
2380 | |
2381 | /* | |
605ca5ed KK |
2382 | * Clone page flags before unfreezing refcount. |
2383 | * | |
2384 | * After successful get_page_unless_zero() might follow flags change, | |
8958b249 | 2385 | * for example lock_page() which set PG_waiters. |
6c287605 DH |
2386 | * |
2387 | * Note that for mapped sub-pages of an anonymous THP, | |
2388 | * PG_anon_exclusive has been cleared in unmap_page() and is stored in | |
2389 | * the migration entry instead from where remap_page() will restore it. | |
2390 | * We can still have PG_anon_exclusive set on effectively unmapped and | |
2391 | * unreferenced sub-pages of an anonymous THP: we can simply drop | |
2392 | * PG_anon_exclusive (-> PG_mappedtodisk) for these here. | |
e9b61f19 | 2393 | */ |
e9b61f19 KS |
2394 | page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
2395 | page_tail->flags |= (head->flags & | |
2396 | ((1L << PG_referenced) | | |
2397 | (1L << PG_swapbacked) | | |
38d8b4e6 | 2398 | (1L << PG_swapcache) | |
e9b61f19 KS |
2399 | (1L << PG_mlocked) | |
2400 | (1L << PG_uptodate) | | |
2401 | (1L << PG_active) | | |
1899ad18 | 2402 | (1L << PG_workingset) | |
e9b61f19 | 2403 | (1L << PG_locked) | |
b8d3c4c3 | 2404 | (1L << PG_unevictable) | |
72e6afa0 CM |
2405 | #ifdef CONFIG_64BIT |
2406 | (1L << PG_arch_2) | | |
2407 | #endif | |
b8d3c4c3 | 2408 | (1L << PG_dirty))); |
e9b61f19 | 2409 | |
173d9d9f HD |
2410 | /* ->mapping in first tail page is compound_mapcount */ |
2411 | VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING, | |
2412 | page_tail); | |
2413 | page_tail->mapping = head->mapping; | |
2414 | page_tail->index = head->index + tail; | |
b653db77 | 2415 | page_tail->private = 0; |
173d9d9f | 2416 | |
605ca5ed | 2417 | /* Page flags must be visible before we make the page non-compound. */ |
e9b61f19 KS |
2418 | smp_wmb(); |
2419 | ||
605ca5ed KK |
2420 | /* |
2421 | * Clear PageTail before unfreezing page refcount. | |
2422 | * | |
2423 | * After successful get_page_unless_zero() might follow put_page() | |
2424 | * which needs correct compound_head(). | |
2425 | */ | |
e9b61f19 KS |
2426 | clear_compound_head(page_tail); |
2427 | ||
605ca5ed KK |
2428 | /* Finally unfreeze refcount. Additional reference from page cache. */ |
2429 | page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) || | |
2430 | PageSwapCache(head))); | |
2431 | ||
e9b61f19 KS |
2432 | if (page_is_young(head)) |
2433 | set_page_young(page_tail); | |
2434 | if (page_is_idle(head)) | |
2435 | set_page_idle(page_tail); | |
2436 | ||
e9b61f19 | 2437 | page_cpupid_xchg_last(page_tail, page_cpupid_last(head)); |
94723aaf MH |
2438 | |
2439 | /* | |
2440 | * always add to the tail because some iterators expect new | |
2441 | * pages to show after the currently processed elements - e.g. | |
2442 | * migrate_pages | |
2443 | */ | |
e9b61f19 | 2444 | lru_add_page_tail(head, page_tail, lruvec, list); |
e9b61f19 KS |
2445 | } |
2446 | ||
baa355fd | 2447 | static void __split_huge_page(struct page *page, struct list_head *list, |
b6769834 | 2448 | pgoff_t end) |
e9b61f19 | 2449 | { |
e809c3fe MWO |
2450 | struct folio *folio = page_folio(page); |
2451 | struct page *head = &folio->page; | |
e9b61f19 | 2452 | struct lruvec *lruvec; |
4101196b MWO |
2453 | struct address_space *swap_cache = NULL; |
2454 | unsigned long offset = 0; | |
8cce5475 | 2455 | unsigned int nr = thp_nr_pages(head); |
8df651c7 | 2456 | int i; |
e9b61f19 | 2457 | |
e9b61f19 | 2458 | /* complete memcg works before add pages to LRU */ |
be6c8982 | 2459 | split_page_memcg(head, nr); |
e9b61f19 | 2460 | |
4101196b MWO |
2461 | if (PageAnon(head) && PageSwapCache(head)) { |
2462 | swp_entry_t entry = { .val = page_private(head) }; | |
2463 | ||
2464 | offset = swp_offset(entry); | |
2465 | swap_cache = swap_address_space(entry); | |
2466 | xa_lock(&swap_cache->i_pages); | |
2467 | } | |
2468 | ||
f0953a1b | 2469 | /* lock lru list/PageCompound, ref frozen by page_ref_freeze */ |
e809c3fe | 2470 | lruvec = folio_lruvec_lock(folio); |
b6769834 | 2471 | |
eac96c3e YS |
2472 | ClearPageHasHWPoisoned(head); |
2473 | ||
8cce5475 | 2474 | for (i = nr - 1; i >= 1; i--) { |
8df651c7 | 2475 | __split_huge_page_tail(head, i, lruvec, list); |
d144bf62 | 2476 | /* Some pages can be beyond EOF: drop them from page cache */ |
baa355fd | 2477 | if (head[i].index >= end) { |
2d077d4b | 2478 | ClearPageDirty(head + i); |
baa355fd | 2479 | __delete_from_page_cache(head + i, NULL); |
d144bf62 | 2480 | if (shmem_mapping(head->mapping)) |
800d8c63 | 2481 | shmem_uncharge(head->mapping->host, 1); |
baa355fd | 2482 | put_page(head + i); |
4101196b MWO |
2483 | } else if (!PageAnon(page)) { |
2484 | __xa_store(&head->mapping->i_pages, head[i].index, | |
2485 | head + i, 0); | |
2486 | } else if (swap_cache) { | |
2487 | __xa_store(&swap_cache->i_pages, offset + i, | |
2488 | head + i, 0); | |
baa355fd KS |
2489 | } |
2490 | } | |
e9b61f19 KS |
2491 | |
2492 | ClearPageCompound(head); | |
6168d0da | 2493 | unlock_page_lruvec(lruvec); |
b6769834 | 2494 | /* Caller disabled irqs, so they are still disabled here */ |
f7da677b | 2495 | |
8cce5475 | 2496 | split_page_owner(head, nr); |
f7da677b | 2497 | |
baa355fd KS |
2498 | /* See comment in __split_huge_page_tail() */ |
2499 | if (PageAnon(head)) { | |
aa5dc07f | 2500 | /* Additional pin to swap cache */ |
4101196b | 2501 | if (PageSwapCache(head)) { |
38d8b4e6 | 2502 | page_ref_add(head, 2); |
4101196b MWO |
2503 | xa_unlock(&swap_cache->i_pages); |
2504 | } else { | |
38d8b4e6 | 2505 | page_ref_inc(head); |
4101196b | 2506 | } |
baa355fd | 2507 | } else { |
aa5dc07f | 2508 | /* Additional pin to page cache */ |
baa355fd | 2509 | page_ref_add(head, 2); |
b93b0163 | 2510 | xa_unlock(&head->mapping->i_pages); |
baa355fd | 2511 | } |
b6769834 | 2512 | local_irq_enable(); |
e9b61f19 | 2513 | |
4eecb8b9 | 2514 | remap_page(folio, nr); |
e9b61f19 | 2515 | |
c4f9c701 HY |
2516 | if (PageSwapCache(head)) { |
2517 | swp_entry_t entry = { .val = page_private(head) }; | |
2518 | ||
2519 | split_swap_cluster(entry); | |
2520 | } | |
2521 | ||
8cce5475 | 2522 | for (i = 0; i < nr; i++) { |
e9b61f19 KS |
2523 | struct page *subpage = head + i; |
2524 | if (subpage == page) | |
2525 | continue; | |
2526 | unlock_page(subpage); | |
2527 | ||
2528 | /* | |
2529 | * Subpages may be freed if there wasn't any mapping | |
2530 | * like if add_to_swap() is running on a lru page that | |
2531 | * had its mapping zapped. And freeing these pages | |
2532 | * requires taking the lru_lock so we do the put_page | |
2533 | * of the tail pages after the split is complete. | |
2534 | */ | |
2535 | put_page(subpage); | |
2536 | } | |
2537 | } | |
2538 | ||
b8f593cd | 2539 | /* Racy check whether the huge page can be split */ |
d4b4084a | 2540 | bool can_split_folio(struct folio *folio, int *pextra_pins) |
b8f593cd HY |
2541 | { |
2542 | int extra_pins; | |
2543 | ||
aa5dc07f | 2544 | /* Additional pins from page cache */ |
d4b4084a MWO |
2545 | if (folio_test_anon(folio)) |
2546 | extra_pins = folio_test_swapcache(folio) ? | |
2547 | folio_nr_pages(folio) : 0; | |
b8f593cd | 2548 | else |
d4b4084a | 2549 | extra_pins = folio_nr_pages(folio); |
b8f593cd HY |
2550 | if (pextra_pins) |
2551 | *pextra_pins = extra_pins; | |
d4b4084a | 2552 | return folio_mapcount(folio) == folio_ref_count(folio) - extra_pins - 1; |
b8f593cd HY |
2553 | } |
2554 | ||
e9b61f19 KS |
2555 | /* |
2556 | * This function splits huge page into normal pages. @page can point to any | |
2557 | * subpage of huge page to split. Split doesn't change the position of @page. | |
2558 | * | |
2559 | * Only caller must hold pin on the @page, otherwise split fails with -EBUSY. | |
2560 | * The huge page must be locked. | |
2561 | * | |
2562 | * If @list is null, tail pages will be added to LRU list, otherwise, to @list. | |
2563 | * | |
2564 | * Both head page and tail pages will inherit mapping, flags, and so on from | |
2565 | * the hugepage. | |
2566 | * | |
2567 | * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if | |
2568 | * they are not mapped. | |
2569 | * | |
2570 | * Returns 0 if the hugepage is split successfully. | |
2571 | * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under | |
2572 | * us. | |
2573 | */ | |
2574 | int split_huge_page_to_list(struct page *page, struct list_head *list) | |
2575 | { | |
4eecb8b9 MWO |
2576 | struct folio *folio = page_folio(page); |
2577 | struct page *head = &folio->page; | |
a8803e6c | 2578 | struct deferred_split *ds_queue = get_deferred_split_queue(head); |
6b24ca4a | 2579 | XA_STATE(xas, &head->mapping->i_pages, head->index); |
baa355fd KS |
2580 | struct anon_vma *anon_vma = NULL; |
2581 | struct address_space *mapping = NULL; | |
504e070d | 2582 | int extra_pins, ret; |
006d3ff2 | 2583 | pgoff_t end; |
478d134e | 2584 | bool is_hzp; |
e9b61f19 | 2585 | |
a8803e6c WY |
2586 | VM_BUG_ON_PAGE(!PageLocked(head), head); |
2587 | VM_BUG_ON_PAGE(!PageCompound(head), head); | |
e9b61f19 | 2588 | |
478d134e XY |
2589 | is_hzp = is_huge_zero_page(head); |
2590 | VM_WARN_ON_ONCE_PAGE(is_hzp, head); | |
2591 | if (is_hzp) | |
2592 | return -EBUSY; | |
2593 | ||
a8803e6c | 2594 | if (PageWriteback(head)) |
59807685 HY |
2595 | return -EBUSY; |
2596 | ||
baa355fd KS |
2597 | if (PageAnon(head)) { |
2598 | /* | |
c1e8d7c6 | 2599 | * The caller does not necessarily hold an mmap_lock that would |
baa355fd KS |
2600 | * prevent the anon_vma disappearing so we first we take a |
2601 | * reference to it and then lock the anon_vma for write. This | |
2f031c6f | 2602 | * is similar to folio_lock_anon_vma_read except the write lock |
baa355fd KS |
2603 | * is taken to serialise against parallel split or collapse |
2604 | * operations. | |
2605 | */ | |
2606 | anon_vma = page_get_anon_vma(head); | |
2607 | if (!anon_vma) { | |
2608 | ret = -EBUSY; | |
2609 | goto out; | |
2610 | } | |
006d3ff2 | 2611 | end = -1; |
baa355fd KS |
2612 | mapping = NULL; |
2613 | anon_vma_lock_write(anon_vma); | |
2614 | } else { | |
2615 | mapping = head->mapping; | |
2616 | ||
2617 | /* Truncated ? */ | |
2618 | if (!mapping) { | |
2619 | ret = -EBUSY; | |
2620 | goto out; | |
2621 | } | |
2622 | ||
6b24ca4a MWO |
2623 | xas_split_alloc(&xas, head, compound_order(head), |
2624 | mapping_gfp_mask(mapping) & GFP_RECLAIM_MASK); | |
2625 | if (xas_error(&xas)) { | |
2626 | ret = xas_error(&xas); | |
2627 | goto out; | |
2628 | } | |
2629 | ||
baa355fd KS |
2630 | anon_vma = NULL; |
2631 | i_mmap_lock_read(mapping); | |
006d3ff2 HD |
2632 | |
2633 | /* | |
2634 | *__split_huge_page() may need to trim off pages beyond EOF: | |
2635 | * but on 32-bit, i_size_read() takes an irq-unsafe seqlock, | |
2636 | * which cannot be nested inside the page tree lock. So note | |
2637 | * end now: i_size itself may be changed at any moment, but | |
2638 | * head page lock is good enough to serialize the trimming. | |
2639 | */ | |
2640 | end = DIV_ROUND_UP(i_size_read(mapping->host), PAGE_SIZE); | |
d144bf62 HD |
2641 | if (shmem_mapping(mapping)) |
2642 | end = shmem_fallocend(mapping->host, end); | |
e9b61f19 | 2643 | } |
e9b61f19 KS |
2644 | |
2645 | /* | |
906f9cdf | 2646 | * Racy check if we can split the page, before unmap_page() will |
e9b61f19 KS |
2647 | * split PMDs |
2648 | */ | |
d4b4084a | 2649 | if (!can_split_folio(folio, &extra_pins)) { |
e9b61f19 KS |
2650 | ret = -EBUSY; |
2651 | goto out_unlock; | |
2652 | } | |
2653 | ||
906f9cdf | 2654 | unmap_page(head); |
e9b61f19 | 2655 | |
b6769834 AS |
2656 | /* block interrupt reentry in xa_lock and spinlock */ |
2657 | local_irq_disable(); | |
baa355fd | 2658 | if (mapping) { |
baa355fd | 2659 | /* |
aa5dc07f | 2660 | * Check if the head page is present in page cache. |
baa355fd KS |
2661 | * We assume all tail are present too, if head is there. |
2662 | */ | |
6b24ca4a MWO |
2663 | xas_lock(&xas); |
2664 | xas_reset(&xas); | |
aa5dc07f | 2665 | if (xas_load(&xas) != head) |
baa355fd KS |
2666 | goto fail; |
2667 | } | |
2668 | ||
0139aa7b | 2669 | /* Prevent deferred_split_scan() touching ->_refcount */ |
364c1eeb | 2670 | spin_lock(&ds_queue->split_queue_lock); |
504e070d | 2671 | if (page_ref_freeze(head, 1 + extra_pins)) { |
9a982250 | 2672 | if (!list_empty(page_deferred_list(head))) { |
364c1eeb | 2673 | ds_queue->split_queue_len--; |
9a982250 KS |
2674 | list_del(page_deferred_list(head)); |
2675 | } | |
afb97172 | 2676 | spin_unlock(&ds_queue->split_queue_lock); |
06d3eff6 | 2677 | if (mapping) { |
bf9ecead MS |
2678 | int nr = thp_nr_pages(head); |
2679 | ||
6b24ca4a | 2680 | xas_split(&xas, head, thp_order(head)); |
1ca7554d | 2681 | if (PageSwapBacked(head)) { |
57b2847d MS |
2682 | __mod_lruvec_page_state(head, NR_SHMEM_THPS, |
2683 | -nr); | |
1ca7554d | 2684 | } else { |
bf9ecead MS |
2685 | __mod_lruvec_page_state(head, NR_FILE_THPS, |
2686 | -nr); | |
1ca7554d MS |
2687 | filemap_nr_thps_dec(mapping); |
2688 | } | |
06d3eff6 KS |
2689 | } |
2690 | ||
b6769834 | 2691 | __split_huge_page(page, list, end); |
c4f9c701 | 2692 | ret = 0; |
e9b61f19 | 2693 | } else { |
364c1eeb | 2694 | spin_unlock(&ds_queue->split_queue_lock); |
504e070d YS |
2695 | fail: |
2696 | if (mapping) | |
6b24ca4a | 2697 | xas_unlock(&xas); |
b6769834 | 2698 | local_irq_enable(); |
4eecb8b9 | 2699 | remap_page(folio, folio_nr_pages(folio)); |
e9b61f19 KS |
2700 | ret = -EBUSY; |
2701 | } | |
2702 | ||
2703 | out_unlock: | |
baa355fd KS |
2704 | if (anon_vma) { |
2705 | anon_vma_unlock_write(anon_vma); | |
2706 | put_anon_vma(anon_vma); | |
2707 | } | |
2708 | if (mapping) | |
2709 | i_mmap_unlock_read(mapping); | |
e9b61f19 | 2710 | out: |
69a37a8b | 2711 | xas_destroy(&xas); |
e9b61f19 KS |
2712 | count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED); |
2713 | return ret; | |
2714 | } | |
9a982250 KS |
2715 | |
2716 | void free_transhuge_page(struct page *page) | |
2717 | { | |
87eaceb3 | 2718 | struct deferred_split *ds_queue = get_deferred_split_queue(page); |
9a982250 KS |
2719 | unsigned long flags; |
2720 | ||
364c1eeb | 2721 | spin_lock_irqsave(&ds_queue->split_queue_lock, flags); |
9a982250 | 2722 | if (!list_empty(page_deferred_list(page))) { |
364c1eeb | 2723 | ds_queue->split_queue_len--; |
9a982250 KS |
2724 | list_del(page_deferred_list(page)); |
2725 | } | |
364c1eeb | 2726 | spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); |
9a982250 KS |
2727 | free_compound_page(page); |
2728 | } | |
2729 | ||
2730 | void deferred_split_huge_page(struct page *page) | |
2731 | { | |
87eaceb3 YS |
2732 | struct deferred_split *ds_queue = get_deferred_split_queue(page); |
2733 | #ifdef CONFIG_MEMCG | |
bcfe06bf | 2734 | struct mem_cgroup *memcg = page_memcg(compound_head(page)); |
87eaceb3 | 2735 | #endif |
9a982250 KS |
2736 | unsigned long flags; |
2737 | ||
2738 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); | |
2739 | ||
87eaceb3 YS |
2740 | /* |
2741 | * The try_to_unmap() in page reclaim path might reach here too, | |
2742 | * this may cause a race condition to corrupt deferred split queue. | |
2743 | * And, if page reclaim is already handling the same page, it is | |
2744 | * unnecessary to handle it again in shrinker. | |
2745 | * | |
2746 | * Check PageSwapCache to determine if the page is being | |
2747 | * handled by page reclaim since THP swap would add the page into | |
2748 | * swap cache before calling try_to_unmap(). | |
2749 | */ | |
2750 | if (PageSwapCache(page)) | |
2751 | return; | |
2752 | ||
364c1eeb | 2753 | spin_lock_irqsave(&ds_queue->split_queue_lock, flags); |
9a982250 | 2754 | if (list_empty(page_deferred_list(page))) { |
f9719a03 | 2755 | count_vm_event(THP_DEFERRED_SPLIT_PAGE); |
364c1eeb YS |
2756 | list_add_tail(page_deferred_list(page), &ds_queue->split_queue); |
2757 | ds_queue->split_queue_len++; | |
87eaceb3 YS |
2758 | #ifdef CONFIG_MEMCG |
2759 | if (memcg) | |
2bfd3637 YS |
2760 | set_shrinker_bit(memcg, page_to_nid(page), |
2761 | deferred_split_shrinker.id); | |
87eaceb3 | 2762 | #endif |
9a982250 | 2763 | } |
364c1eeb | 2764 | spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); |
9a982250 KS |
2765 | } |
2766 | ||
2767 | static unsigned long deferred_split_count(struct shrinker *shrink, | |
2768 | struct shrink_control *sc) | |
2769 | { | |
a3d0a918 | 2770 | struct pglist_data *pgdata = NODE_DATA(sc->nid); |
364c1eeb | 2771 | struct deferred_split *ds_queue = &pgdata->deferred_split_queue; |
87eaceb3 YS |
2772 | |
2773 | #ifdef CONFIG_MEMCG | |
2774 | if (sc->memcg) | |
2775 | ds_queue = &sc->memcg->deferred_split_queue; | |
2776 | #endif | |
364c1eeb | 2777 | return READ_ONCE(ds_queue->split_queue_len); |
9a982250 KS |
2778 | } |
2779 | ||
2780 | static unsigned long deferred_split_scan(struct shrinker *shrink, | |
2781 | struct shrink_control *sc) | |
2782 | { | |
a3d0a918 | 2783 | struct pglist_data *pgdata = NODE_DATA(sc->nid); |
364c1eeb | 2784 | struct deferred_split *ds_queue = &pgdata->deferred_split_queue; |
9a982250 KS |
2785 | unsigned long flags; |
2786 | LIST_HEAD(list), *pos, *next; | |
2787 | struct page *page; | |
2788 | int split = 0; | |
2789 | ||
87eaceb3 YS |
2790 | #ifdef CONFIG_MEMCG |
2791 | if (sc->memcg) | |
2792 | ds_queue = &sc->memcg->deferred_split_queue; | |
2793 | #endif | |
2794 | ||
364c1eeb | 2795 | spin_lock_irqsave(&ds_queue->split_queue_lock, flags); |
9a982250 | 2796 | /* Take pin on all head pages to avoid freeing them under us */ |
364c1eeb | 2797 | list_for_each_safe(pos, next, &ds_queue->split_queue) { |
dfe5c51c | 2798 | page = list_entry((void *)pos, struct page, deferred_list); |
9a982250 | 2799 | page = compound_head(page); |
e3ae1953 KS |
2800 | if (get_page_unless_zero(page)) { |
2801 | list_move(page_deferred_list(page), &list); | |
2802 | } else { | |
2803 | /* We lost race with put_compound_page() */ | |
9a982250 | 2804 | list_del_init(page_deferred_list(page)); |
364c1eeb | 2805 | ds_queue->split_queue_len--; |
9a982250 | 2806 | } |
e3ae1953 KS |
2807 | if (!--sc->nr_to_scan) |
2808 | break; | |
9a982250 | 2809 | } |
364c1eeb | 2810 | spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); |
9a982250 KS |
2811 | |
2812 | list_for_each_safe(pos, next, &list) { | |
dfe5c51c | 2813 | page = list_entry((void *)pos, struct page, deferred_list); |
fa41b900 KS |
2814 | if (!trylock_page(page)) |
2815 | goto next; | |
9a982250 KS |
2816 | /* split_huge_page() removes page from list on success */ |
2817 | if (!split_huge_page(page)) | |
2818 | split++; | |
2819 | unlock_page(page); | |
fa41b900 | 2820 | next: |
9a982250 KS |
2821 | put_page(page); |
2822 | } | |
2823 | ||
364c1eeb YS |
2824 | spin_lock_irqsave(&ds_queue->split_queue_lock, flags); |
2825 | list_splice_tail(&list, &ds_queue->split_queue); | |
2826 | spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); | |
9a982250 | 2827 | |
cb8d68ec KS |
2828 | /* |
2829 | * Stop shrinker if we didn't split any page, but the queue is empty. | |
2830 | * This can happen if pages were freed under us. | |
2831 | */ | |
364c1eeb | 2832 | if (!split && list_empty(&ds_queue->split_queue)) |
cb8d68ec KS |
2833 | return SHRINK_STOP; |
2834 | return split; | |
9a982250 KS |
2835 | } |
2836 | ||
2837 | static struct shrinker deferred_split_shrinker = { | |
2838 | .count_objects = deferred_split_count, | |
2839 | .scan_objects = deferred_split_scan, | |
2840 | .seeks = DEFAULT_SEEKS, | |
87eaceb3 YS |
2841 | .flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE | |
2842 | SHRINKER_NONSLAB, | |
9a982250 | 2843 | }; |
49071d43 KS |
2844 | |
2845 | #ifdef CONFIG_DEBUG_FS | |
fa6c0231 | 2846 | static void split_huge_pages_all(void) |
49071d43 KS |
2847 | { |
2848 | struct zone *zone; | |
2849 | struct page *page; | |
2850 | unsigned long pfn, max_zone_pfn; | |
2851 | unsigned long total = 0, split = 0; | |
2852 | ||
fa6c0231 | 2853 | pr_debug("Split all THPs\n"); |
49071d43 KS |
2854 | for_each_populated_zone(zone) { |
2855 | max_zone_pfn = zone_end_pfn(zone); | |
2856 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) { | |
2857 | if (!pfn_valid(pfn)) | |
2858 | continue; | |
2859 | ||
2860 | page = pfn_to_page(pfn); | |
2861 | if (!get_page_unless_zero(page)) | |
2862 | continue; | |
2863 | ||
2864 | if (zone != page_zone(page)) | |
2865 | goto next; | |
2866 | ||
baa355fd | 2867 | if (!PageHead(page) || PageHuge(page) || !PageLRU(page)) |
49071d43 KS |
2868 | goto next; |
2869 | ||
2870 | total++; | |
2871 | lock_page(page); | |
2872 | if (!split_huge_page(page)) | |
2873 | split++; | |
2874 | unlock_page(page); | |
2875 | next: | |
2876 | put_page(page); | |
fa6c0231 | 2877 | cond_resched(); |
49071d43 KS |
2878 | } |
2879 | } | |
2880 | ||
fa6c0231 ZY |
2881 | pr_debug("%lu of %lu THP split\n", split, total); |
2882 | } | |
49071d43 | 2883 | |
fa6c0231 ZY |
2884 | static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma) |
2885 | { | |
2886 | return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) || | |
2887 | is_vm_hugetlb_page(vma); | |
2888 | } | |
2889 | ||
2890 | static int split_huge_pages_pid(int pid, unsigned long vaddr_start, | |
2891 | unsigned long vaddr_end) | |
2892 | { | |
2893 | int ret = 0; | |
2894 | struct task_struct *task; | |
2895 | struct mm_struct *mm; | |
2896 | unsigned long total = 0, split = 0; | |
2897 | unsigned long addr; | |
2898 | ||
2899 | vaddr_start &= PAGE_MASK; | |
2900 | vaddr_end &= PAGE_MASK; | |
2901 | ||
2902 | /* Find the task_struct from pid */ | |
2903 | rcu_read_lock(); | |
2904 | task = find_task_by_vpid(pid); | |
2905 | if (!task) { | |
2906 | rcu_read_unlock(); | |
2907 | ret = -ESRCH; | |
2908 | goto out; | |
2909 | } | |
2910 | get_task_struct(task); | |
2911 | rcu_read_unlock(); | |
2912 | ||
2913 | /* Find the mm_struct */ | |
2914 | mm = get_task_mm(task); | |
2915 | put_task_struct(task); | |
2916 | ||
2917 | if (!mm) { | |
2918 | ret = -EINVAL; | |
2919 | goto out; | |
2920 | } | |
2921 | ||
2922 | pr_debug("Split huge pages in pid: %d, vaddr: [0x%lx - 0x%lx]\n", | |
2923 | pid, vaddr_start, vaddr_end); | |
2924 | ||
2925 | mmap_read_lock(mm); | |
2926 | /* | |
2927 | * always increase addr by PAGE_SIZE, since we could have a PTE page | |
2928 | * table filled with PTE-mapped THPs, each of which is distinct. | |
2929 | */ | |
2930 | for (addr = vaddr_start; addr < vaddr_end; addr += PAGE_SIZE) { | |
2931 | struct vm_area_struct *vma = find_vma(mm, addr); | |
fa6c0231 ZY |
2932 | struct page *page; |
2933 | ||
2934 | if (!vma || addr < vma->vm_start) | |
2935 | break; | |
2936 | ||
2937 | /* skip special VMA and hugetlb VMA */ | |
2938 | if (vma_not_suitable_for_thp_split(vma)) { | |
2939 | addr = vma->vm_end; | |
2940 | continue; | |
2941 | } | |
2942 | ||
2943 | /* FOLL_DUMP to ignore special (like zero) pages */ | |
87d2762e | 2944 | page = follow_page(vma, addr, FOLL_GET | FOLL_DUMP); |
fa6c0231 ZY |
2945 | |
2946 | if (IS_ERR(page)) | |
2947 | continue; | |
3218f871 | 2948 | if (!page || is_zone_device_page(page)) |
fa6c0231 ZY |
2949 | continue; |
2950 | ||
2951 | if (!is_transparent_hugepage(page)) | |
2952 | goto next; | |
2953 | ||
2954 | total++; | |
d4b4084a | 2955 | if (!can_split_folio(page_folio(page), NULL)) |
fa6c0231 ZY |
2956 | goto next; |
2957 | ||
2958 | if (!trylock_page(page)) | |
2959 | goto next; | |
2960 | ||
2961 | if (!split_huge_page(page)) | |
2962 | split++; | |
2963 | ||
2964 | unlock_page(page); | |
2965 | next: | |
2966 | put_page(page); | |
2967 | cond_resched(); | |
2968 | } | |
2969 | mmap_read_unlock(mm); | |
2970 | mmput(mm); | |
2971 | ||
2972 | pr_debug("%lu of %lu THP split\n", split, total); | |
2973 | ||
2974 | out: | |
2975 | return ret; | |
49071d43 | 2976 | } |
fa6c0231 | 2977 | |
fbe37501 ZY |
2978 | static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, |
2979 | pgoff_t off_end) | |
2980 | { | |
2981 | struct filename *file; | |
2982 | struct file *candidate; | |
2983 | struct address_space *mapping; | |
2984 | int ret = -EINVAL; | |
2985 | pgoff_t index; | |
2986 | int nr_pages = 1; | |
2987 | unsigned long total = 0, split = 0; | |
2988 | ||
2989 | file = getname_kernel(file_path); | |
2990 | if (IS_ERR(file)) | |
2991 | return ret; | |
2992 | ||
2993 | candidate = file_open_name(file, O_RDONLY, 0); | |
2994 | if (IS_ERR(candidate)) | |
2995 | goto out; | |
2996 | ||
2997 | pr_debug("split file-backed THPs in file: %s, page offset: [0x%lx - 0x%lx]\n", | |
2998 | file_path, off_start, off_end); | |
2999 | ||
3000 | mapping = candidate->f_mapping; | |
3001 | ||
3002 | for (index = off_start; index < off_end; index += nr_pages) { | |
3003 | struct page *fpage = pagecache_get_page(mapping, index, | |
3004 | FGP_ENTRY | FGP_HEAD, 0); | |
3005 | ||
3006 | nr_pages = 1; | |
3007 | if (xa_is_value(fpage) || !fpage) | |
3008 | continue; | |
3009 | ||
3010 | if (!is_transparent_hugepage(fpage)) | |
3011 | goto next; | |
3012 | ||
3013 | total++; | |
3014 | nr_pages = thp_nr_pages(fpage); | |
3015 | ||
3016 | if (!trylock_page(fpage)) | |
3017 | goto next; | |
3018 | ||
3019 | if (!split_huge_page(fpage)) | |
3020 | split++; | |
3021 | ||
3022 | unlock_page(fpage); | |
3023 | next: | |
3024 | put_page(fpage); | |
3025 | cond_resched(); | |
3026 | } | |
3027 | ||
3028 | filp_close(candidate, NULL); | |
3029 | ret = 0; | |
3030 | ||
3031 | pr_debug("%lu of %lu file-backed THP split\n", split, total); | |
3032 | out: | |
3033 | putname(file); | |
3034 | return ret; | |
3035 | } | |
3036 | ||
fa6c0231 ZY |
3037 | #define MAX_INPUT_BUF_SZ 255 |
3038 | ||
3039 | static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, | |
3040 | size_t count, loff_t *ppops) | |
3041 | { | |
3042 | static DEFINE_MUTEX(split_debug_mutex); | |
3043 | ssize_t ret; | |
fbe37501 ZY |
3044 | /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */ |
3045 | char input_buf[MAX_INPUT_BUF_SZ]; | |
fa6c0231 ZY |
3046 | int pid; |
3047 | unsigned long vaddr_start, vaddr_end; | |
3048 | ||
3049 | ret = mutex_lock_interruptible(&split_debug_mutex); | |
3050 | if (ret) | |
3051 | return ret; | |
3052 | ||
3053 | ret = -EFAULT; | |
3054 | ||
3055 | memset(input_buf, 0, MAX_INPUT_BUF_SZ); | |
3056 | if (copy_from_user(input_buf, buf, min_t(size_t, count, MAX_INPUT_BUF_SZ))) | |
3057 | goto out; | |
3058 | ||
3059 | input_buf[MAX_INPUT_BUF_SZ - 1] = '\0'; | |
fbe37501 ZY |
3060 | |
3061 | if (input_buf[0] == '/') { | |
3062 | char *tok; | |
3063 | char *buf = input_buf; | |
3064 | char file_path[MAX_INPUT_BUF_SZ]; | |
3065 | pgoff_t off_start = 0, off_end = 0; | |
3066 | size_t input_len = strlen(input_buf); | |
3067 | ||
3068 | tok = strsep(&buf, ","); | |
3069 | if (tok) { | |
1212e00c | 3070 | strcpy(file_path, tok); |
fbe37501 ZY |
3071 | } else { |
3072 | ret = -EINVAL; | |
3073 | goto out; | |
3074 | } | |
3075 | ||
3076 | ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end); | |
3077 | if (ret != 2) { | |
3078 | ret = -EINVAL; | |
3079 | goto out; | |
3080 | } | |
3081 | ret = split_huge_pages_in_file(file_path, off_start, off_end); | |
3082 | if (!ret) | |
3083 | ret = input_len; | |
3084 | ||
3085 | goto out; | |
3086 | } | |
3087 | ||
fa6c0231 ZY |
3088 | ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end); |
3089 | if (ret == 1 && pid == 1) { | |
3090 | split_huge_pages_all(); | |
3091 | ret = strlen(input_buf); | |
3092 | goto out; | |
3093 | } else if (ret != 3) { | |
3094 | ret = -EINVAL; | |
3095 | goto out; | |
3096 | } | |
3097 | ||
3098 | ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end); | |
3099 | if (!ret) | |
3100 | ret = strlen(input_buf); | |
3101 | out: | |
3102 | mutex_unlock(&split_debug_mutex); | |
3103 | return ret; | |
3104 | ||
3105 | } | |
3106 | ||
3107 | static const struct file_operations split_huge_pages_fops = { | |
3108 | .owner = THIS_MODULE, | |
3109 | .write = split_huge_pages_write, | |
3110 | .llseek = no_llseek, | |
3111 | }; | |
49071d43 KS |
3112 | |
3113 | static int __init split_huge_pages_debugfs(void) | |
3114 | { | |
d9f7979c GKH |
3115 | debugfs_create_file("split_huge_pages", 0200, NULL, NULL, |
3116 | &split_huge_pages_fops); | |
49071d43 KS |
3117 | return 0; |
3118 | } | |
3119 | late_initcall(split_huge_pages_debugfs); | |
3120 | #endif | |
616b8371 ZY |
3121 | |
3122 | #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION | |
7f5abe60 | 3123 | int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw, |
616b8371 ZY |
3124 | struct page *page) |
3125 | { | |
3126 | struct vm_area_struct *vma = pvmw->vma; | |
3127 | struct mm_struct *mm = vma->vm_mm; | |
3128 | unsigned long address = pvmw->address; | |
6c287605 | 3129 | bool anon_exclusive; |
616b8371 ZY |
3130 | pmd_t pmdval; |
3131 | swp_entry_t entry; | |
ab6e3d09 | 3132 | pmd_t pmdswp; |
616b8371 ZY |
3133 | |
3134 | if (!(pvmw->pmd && !pvmw->pte)) | |
7f5abe60 | 3135 | return 0; |
616b8371 | 3136 | |
616b8371 | 3137 | flush_cache_range(vma, address, address + HPAGE_PMD_SIZE); |
8a8683ad | 3138 | pmdval = pmdp_invalidate(vma, address, pvmw->pmd); |
6c287605 DH |
3139 | |
3140 | anon_exclusive = PageAnon(page) && PageAnonExclusive(page); | |
3141 | if (anon_exclusive && page_try_share_anon_rmap(page)) { | |
3142 | set_pmd_at(mm, address, pvmw->pmd, pmdval); | |
7f5abe60 | 3143 | return -EBUSY; |
6c287605 DH |
3144 | } |
3145 | ||
616b8371 ZY |
3146 | if (pmd_dirty(pmdval)) |
3147 | set_page_dirty(page); | |
4dd845b5 AP |
3148 | if (pmd_write(pmdval)) |
3149 | entry = make_writable_migration_entry(page_to_pfn(page)); | |
6c287605 DH |
3150 | else if (anon_exclusive) |
3151 | entry = make_readable_exclusive_migration_entry(page_to_pfn(page)); | |
4dd845b5 AP |
3152 | else |
3153 | entry = make_readable_migration_entry(page_to_pfn(page)); | |
ab6e3d09 NH |
3154 | pmdswp = swp_entry_to_pmd(entry); |
3155 | if (pmd_soft_dirty(pmdval)) | |
3156 | pmdswp = pmd_swp_mksoft_dirty(pmdswp); | |
3157 | set_pmd_at(mm, address, pvmw->pmd, pmdswp); | |
cea86fe2 | 3158 | page_remove_rmap(page, vma, true); |
616b8371 | 3159 | put_page(page); |
283fd6fe | 3160 | trace_set_migration_pmd(address, pmd_val(pmdswp)); |
7f5abe60 DH |
3161 | |
3162 | return 0; | |
616b8371 ZY |
3163 | } |
3164 | ||
3165 | void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) | |
3166 | { | |
3167 | struct vm_area_struct *vma = pvmw->vma; | |
3168 | struct mm_struct *mm = vma->vm_mm; | |
3169 | unsigned long address = pvmw->address; | |
3170 | unsigned long mmun_start = address & HPAGE_PMD_MASK; | |
3171 | pmd_t pmde; | |
3172 | swp_entry_t entry; | |
3173 | ||
3174 | if (!(pvmw->pmd && !pvmw->pte)) | |
3175 | return; | |
3176 | ||
3177 | entry = pmd_to_swp_entry(*pvmw->pmd); | |
3178 | get_page(new); | |
3179 | pmde = pmd_mkold(mk_huge_pmd(new, vma->vm_page_prot)); | |
ab6e3d09 NH |
3180 | if (pmd_swp_soft_dirty(*pvmw->pmd)) |
3181 | pmde = pmd_mksoft_dirty(pmde); | |
4dd845b5 | 3182 | if (is_writable_migration_entry(entry)) |
f55e1014 | 3183 | pmde = maybe_pmd_mkwrite(pmde, vma); |
8f34f1ea PX |
3184 | if (pmd_swp_uffd_wp(*pvmw->pmd)) |
3185 | pmde = pmd_wrprotect(pmd_mkuffd_wp(pmde)); | |
616b8371 | 3186 | |
6c287605 DH |
3187 | if (PageAnon(new)) { |
3188 | rmap_t rmap_flags = RMAP_COMPOUND; | |
3189 | ||
3190 | if (!is_readable_migration_entry(entry)) | |
3191 | rmap_flags |= RMAP_EXCLUSIVE; | |
3192 | ||
3193 | page_add_anon_rmap(new, vma, mmun_start, rmap_flags); | |
3194 | } else { | |
cea86fe2 | 3195 | page_add_file_rmap(new, vma, true); |
6c287605 DH |
3196 | } |
3197 | VM_BUG_ON(pmd_write(pmde) && PageAnon(new) && !PageAnonExclusive(new)); | |
616b8371 | 3198 | set_pmd_at(mm, mmun_start, pvmw->pmd, pmde); |
5cbcf225 MS |
3199 | |
3200 | /* No need to invalidate - it was non-present before */ | |
616b8371 | 3201 | update_mmu_cache_pmd(vma, address, pvmw->pmd); |
283fd6fe | 3202 | trace_remove_migration_pmd(address, pmd_val(pmde)); |
616b8371 ZY |
3203 | } |
3204 | #endif |