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