]>
Commit | Line | Data |
---|---|---|
71e3aac0 AA |
1 | /* |
2 | * Copyright (C) 2009 Red Hat, Inc. | |
3 | * | |
4 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
5 | * the COPYING file in the top-level directory. | |
6 | */ | |
7 | ||
ae3a8c1c AM |
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
9 | ||
71e3aac0 AA |
10 | #include <linux/mm.h> |
11 | #include <linux/sched.h> | |
12 | #include <linux/highmem.h> | |
13 | #include <linux/hugetlb.h> | |
14 | #include <linux/mmu_notifier.h> | |
15 | #include <linux/rmap.h> | |
16 | #include <linux/swap.h> | |
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 AA |
21 | #include <linux/kthread.h> |
22 | #include <linux/khugepaged.h> | |
878aee7d | 23 | #include <linux/freezer.h> |
f25748e3 | 24 | #include <linux/pfn_t.h> |
a664b2d8 | 25 | #include <linux/mman.h> |
3565fce3 | 26 | #include <linux/memremap.h> |
325adeb5 | 27 | #include <linux/pagemap.h> |
49071d43 | 28 | #include <linux/debugfs.h> |
4daae3b4 | 29 | #include <linux/migrate.h> |
43b5fbbd | 30 | #include <linux/hashtable.h> |
6b251fc9 | 31 | #include <linux/userfaultfd_k.h> |
33c3fc71 | 32 | #include <linux/page_idle.h> |
baa355fd | 33 | #include <linux/shmem_fs.h> |
97ae1749 | 34 | |
71e3aac0 AA |
35 | #include <asm/tlb.h> |
36 | #include <asm/pgalloc.h> | |
37 | #include "internal.h" | |
38 | ||
7d2eba05 EA |
39 | enum scan_result { |
40 | SCAN_FAIL, | |
41 | SCAN_SUCCEED, | |
42 | SCAN_PMD_NULL, | |
43 | SCAN_EXCEED_NONE_PTE, | |
44 | SCAN_PTE_NON_PRESENT, | |
45 | SCAN_PAGE_RO, | |
46 | SCAN_NO_REFERENCED_PAGE, | |
47 | SCAN_PAGE_NULL, | |
48 | SCAN_SCAN_ABORT, | |
49 | SCAN_PAGE_COUNT, | |
50 | SCAN_PAGE_LRU, | |
51 | SCAN_PAGE_LOCK, | |
52 | SCAN_PAGE_ANON, | |
b1caa957 | 53 | SCAN_PAGE_COMPOUND, |
7d2eba05 EA |
54 | SCAN_ANY_PROCESS, |
55 | SCAN_VMA_NULL, | |
56 | SCAN_VMA_CHECK, | |
57 | SCAN_ADDRESS_RANGE, | |
58 | SCAN_SWAP_CACHE_PAGE, | |
59 | SCAN_DEL_PAGE_LRU, | |
60 | SCAN_ALLOC_HUGE_PAGE_FAIL, | |
70652f6e EA |
61 | SCAN_CGROUP_CHARGE_FAIL, |
62 | SCAN_EXCEED_SWAP_PTE | |
7d2eba05 EA |
63 | }; |
64 | ||
65 | #define CREATE_TRACE_POINTS | |
66 | #include <trace/events/huge_memory.h> | |
67 | ||
ba76149f | 68 | /* |
8bfa3f9a JW |
69 | * By default transparent hugepage support is disabled in order that avoid |
70 | * to risk increase the memory footprint of applications without a guaranteed | |
71 | * benefit. When transparent hugepage support is enabled, is for all mappings, | |
72 | * and khugepaged scans all mappings. | |
73 | * Defrag is invoked by khugepaged hugepage allocations and by page faults | |
74 | * for all hugepage allocations. | |
ba76149f | 75 | */ |
71e3aac0 | 76 | unsigned long transparent_hugepage_flags __read_mostly = |
13ece886 | 77 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_ALWAYS |
ba76149f | 78 | (1<<TRANSPARENT_HUGEPAGE_FLAG)| |
13ece886 AA |
79 | #endif |
80 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE_MADVISE | |
81 | (1<<TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG)| | |
82 | #endif | |
444eb2a4 | 83 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG)| |
79da5407 KS |
84 | (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG)| |
85 | (1<<TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); | |
ba76149f AA |
86 | |
87 | /* default scan 8*512 pte (or vmas) every 30 second */ | |
ff20c2e0 | 88 | static unsigned int khugepaged_pages_to_scan __read_mostly; |
ba76149f AA |
89 | static unsigned int khugepaged_pages_collapsed; |
90 | static unsigned int khugepaged_full_scans; | |
91 | static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000; | |
92 | /* during fragmentation poll the hugepage allocator once every minute */ | |
93 | static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000; | |
f0508977 | 94 | static unsigned long khugepaged_sleep_expire; |
ba76149f AA |
95 | static struct task_struct *khugepaged_thread __read_mostly; |
96 | static DEFINE_MUTEX(khugepaged_mutex); | |
97 | static DEFINE_SPINLOCK(khugepaged_mm_lock); | |
98 | static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait); | |
99 | /* | |
100 | * default collapse hugepages if there is at least one pte mapped like | |
101 | * it would have happened if the vma was large enough during page | |
102 | * fault. | |
103 | */ | |
ff20c2e0 | 104 | static unsigned int khugepaged_max_ptes_none __read_mostly; |
70652f6e | 105 | static unsigned int khugepaged_max_ptes_swap __read_mostly; |
ba76149f AA |
106 | |
107 | static int khugepaged(void *none); | |
ba76149f | 108 | static int khugepaged_slab_init(void); |
65ebb64f | 109 | static void khugepaged_slab_exit(void); |
ba76149f | 110 | |
43b5fbbd SL |
111 | #define MM_SLOTS_HASH_BITS 10 |
112 | static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); | |
113 | ||
ba76149f AA |
114 | static struct kmem_cache *mm_slot_cache __read_mostly; |
115 | ||
116 | /** | |
117 | * struct mm_slot - hash lookup from mm to mm_slot | |
118 | * @hash: hash collision list | |
119 | * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head | |
120 | * @mm: the mm that this information is valid for | |
121 | */ | |
122 | struct mm_slot { | |
123 | struct hlist_node hash; | |
124 | struct list_head mm_node; | |
125 | struct mm_struct *mm; | |
126 | }; | |
127 | ||
128 | /** | |
129 | * struct khugepaged_scan - cursor for scanning | |
130 | * @mm_head: the head of the mm list to scan | |
131 | * @mm_slot: the current mm_slot we are scanning | |
132 | * @address: the next address inside that to be scanned | |
133 | * | |
134 | * There is only the one khugepaged_scan instance of this cursor structure. | |
135 | */ | |
136 | struct khugepaged_scan { | |
137 | struct list_head mm_head; | |
138 | struct mm_slot *mm_slot; | |
139 | unsigned long address; | |
2f1da642 HS |
140 | }; |
141 | static struct khugepaged_scan khugepaged_scan = { | |
ba76149f AA |
142 | .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), |
143 | }; | |
144 | ||
9a982250 | 145 | static struct shrinker deferred_split_shrinker; |
f000565a | 146 | |
2c0b80d4 | 147 | static void set_recommended_min_free_kbytes(void) |
f000565a AA |
148 | { |
149 | struct zone *zone; | |
150 | int nr_zones = 0; | |
151 | unsigned long recommended_min; | |
f000565a | 152 | |
f000565a AA |
153 | for_each_populated_zone(zone) |
154 | nr_zones++; | |
155 | ||
974a786e | 156 | /* Ensure 2 pageblocks are free to assist fragmentation avoidance */ |
f000565a AA |
157 | recommended_min = pageblock_nr_pages * nr_zones * 2; |
158 | ||
159 | /* | |
160 | * Make sure that on average at least two pageblocks are almost free | |
161 | * of another type, one for a migratetype to fall back to and a | |
162 | * second to avoid subsequent fallbacks of other types There are 3 | |
163 | * MIGRATE_TYPES we care about. | |
164 | */ | |
165 | recommended_min += pageblock_nr_pages * nr_zones * | |
166 | MIGRATE_PCPTYPES * MIGRATE_PCPTYPES; | |
167 | ||
168 | /* don't ever allow to reserve more than 5% of the lowmem */ | |
169 | recommended_min = min(recommended_min, | |
170 | (unsigned long) nr_free_buffer_pages() / 20); | |
171 | recommended_min <<= (PAGE_SHIFT-10); | |
172 | ||
42aa83cb HP |
173 | if (recommended_min > min_free_kbytes) { |
174 | if (user_min_free_kbytes >= 0) | |
756a025f | 175 | pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n", |
42aa83cb HP |
176 | min_free_kbytes, recommended_min); |
177 | ||
f000565a | 178 | min_free_kbytes = recommended_min; |
42aa83cb | 179 | } |
f000565a | 180 | setup_per_zone_wmarks(); |
f000565a | 181 | } |
f000565a | 182 | |
79553da2 | 183 | static int start_stop_khugepaged(void) |
ba76149f AA |
184 | { |
185 | int err = 0; | |
186 | if (khugepaged_enabled()) { | |
ba76149f AA |
187 | if (!khugepaged_thread) |
188 | khugepaged_thread = kthread_run(khugepaged, NULL, | |
189 | "khugepaged"); | |
18e8e5c7 | 190 | if (IS_ERR(khugepaged_thread)) { |
ae3a8c1c | 191 | pr_err("khugepaged: kthread_run(khugepaged) failed\n"); |
ba76149f AA |
192 | err = PTR_ERR(khugepaged_thread); |
193 | khugepaged_thread = NULL; | |
79553da2 | 194 | goto fail; |
ba76149f | 195 | } |
911891af XG |
196 | |
197 | if (!list_empty(&khugepaged_scan.mm_head)) | |
ba76149f | 198 | wake_up_interruptible(&khugepaged_wait); |
f000565a AA |
199 | |
200 | set_recommended_min_free_kbytes(); | |
911891af | 201 | } else if (khugepaged_thread) { |
911891af XG |
202 | kthread_stop(khugepaged_thread); |
203 | khugepaged_thread = NULL; | |
204 | } | |
79553da2 | 205 | fail: |
ba76149f AA |
206 | return err; |
207 | } | |
71e3aac0 | 208 | |
97ae1749 | 209 | static atomic_t huge_zero_refcount; |
56873f43 | 210 | struct page *huge_zero_page __read_mostly; |
4a6c1297 | 211 | |
fc437044 | 212 | struct page *get_huge_zero_page(void) |
97ae1749 KS |
213 | { |
214 | struct page *zero_page; | |
215 | retry: | |
216 | if (likely(atomic_inc_not_zero(&huge_zero_refcount))) | |
4db0c3c2 | 217 | return READ_ONCE(huge_zero_page); |
97ae1749 KS |
218 | |
219 | zero_page = alloc_pages((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE, | |
4a6c1297 | 220 | HPAGE_PMD_ORDER); |
d8a8e1f0 KS |
221 | if (!zero_page) { |
222 | count_vm_event(THP_ZERO_PAGE_ALLOC_FAILED); | |
5918d10a | 223 | return NULL; |
d8a8e1f0 KS |
224 | } |
225 | count_vm_event(THP_ZERO_PAGE_ALLOC); | |
97ae1749 | 226 | preempt_disable(); |
5918d10a | 227 | if (cmpxchg(&huge_zero_page, NULL, zero_page)) { |
97ae1749 | 228 | preempt_enable(); |
5ddacbe9 | 229 | __free_pages(zero_page, compound_order(zero_page)); |
97ae1749 KS |
230 | goto retry; |
231 | } | |
232 | ||
233 | /* We take additional reference here. It will be put back by shrinker */ | |
234 | atomic_set(&huge_zero_refcount, 2); | |
235 | preempt_enable(); | |
4db0c3c2 | 236 | return READ_ONCE(huge_zero_page); |
4a6c1297 KS |
237 | } |
238 | ||
aa88b68c | 239 | void put_huge_zero_page(void) |
4a6c1297 | 240 | { |
97ae1749 KS |
241 | /* |
242 | * Counter should never go to zero here. Only shrinker can put | |
243 | * last reference. | |
244 | */ | |
245 | BUG_ON(atomic_dec_and_test(&huge_zero_refcount)); | |
4a6c1297 KS |
246 | } |
247 | ||
48896466 GC |
248 | static unsigned long shrink_huge_zero_page_count(struct shrinker *shrink, |
249 | struct shrink_control *sc) | |
4a6c1297 | 250 | { |
48896466 GC |
251 | /* we can free zero page only if last reference remains */ |
252 | return atomic_read(&huge_zero_refcount) == 1 ? HPAGE_PMD_NR : 0; | |
253 | } | |
97ae1749 | 254 | |
48896466 GC |
255 | static unsigned long shrink_huge_zero_page_scan(struct shrinker *shrink, |
256 | struct shrink_control *sc) | |
257 | { | |
97ae1749 | 258 | if (atomic_cmpxchg(&huge_zero_refcount, 1, 0) == 1) { |
5918d10a KS |
259 | struct page *zero_page = xchg(&huge_zero_page, NULL); |
260 | BUG_ON(zero_page == NULL); | |
5ddacbe9 | 261 | __free_pages(zero_page, compound_order(zero_page)); |
48896466 | 262 | return HPAGE_PMD_NR; |
97ae1749 KS |
263 | } |
264 | ||
265 | return 0; | |
4a6c1297 KS |
266 | } |
267 | ||
97ae1749 | 268 | static struct shrinker huge_zero_page_shrinker = { |
48896466 GC |
269 | .count_objects = shrink_huge_zero_page_count, |
270 | .scan_objects = shrink_huge_zero_page_scan, | |
97ae1749 KS |
271 | .seeks = DEFAULT_SEEKS, |
272 | }; | |
273 | ||
71e3aac0 | 274 | #ifdef CONFIG_SYSFS |
ba76149f | 275 | |
444eb2a4 | 276 | static ssize_t triple_flag_store(struct kobject *kobj, |
71e3aac0 AA |
277 | struct kobj_attribute *attr, |
278 | const char *buf, size_t count, | |
279 | enum transparent_hugepage_flag enabled, | |
444eb2a4 | 280 | enum transparent_hugepage_flag deferred, |
71e3aac0 AA |
281 | enum transparent_hugepage_flag req_madv) |
282 | { | |
444eb2a4 MG |
283 | if (!memcmp("defer", buf, |
284 | min(sizeof("defer")-1, count))) { | |
285 | if (enabled == deferred) | |
286 | return -EINVAL; | |
287 | clear_bit(enabled, &transparent_hugepage_flags); | |
288 | clear_bit(req_madv, &transparent_hugepage_flags); | |
289 | set_bit(deferred, &transparent_hugepage_flags); | |
290 | } else if (!memcmp("always", buf, | |
71e3aac0 | 291 | min(sizeof("always")-1, count))) { |
444eb2a4 | 292 | clear_bit(deferred, &transparent_hugepage_flags); |
71e3aac0 | 293 | clear_bit(req_madv, &transparent_hugepage_flags); |
444eb2a4 | 294 | set_bit(enabled, &transparent_hugepage_flags); |
71e3aac0 AA |
295 | } else if (!memcmp("madvise", buf, |
296 | min(sizeof("madvise")-1, count))) { | |
297 | clear_bit(enabled, &transparent_hugepage_flags); | |
444eb2a4 | 298 | clear_bit(deferred, &transparent_hugepage_flags); |
71e3aac0 AA |
299 | set_bit(req_madv, &transparent_hugepage_flags); |
300 | } else if (!memcmp("never", buf, | |
301 | min(sizeof("never")-1, count))) { | |
302 | clear_bit(enabled, &transparent_hugepage_flags); | |
303 | clear_bit(req_madv, &transparent_hugepage_flags); | |
444eb2a4 | 304 | clear_bit(deferred, &transparent_hugepage_flags); |
71e3aac0 AA |
305 | } else |
306 | return -EINVAL; | |
307 | ||
308 | return count; | |
309 | } | |
310 | ||
311 | static ssize_t enabled_show(struct kobject *kobj, | |
312 | struct kobj_attribute *attr, char *buf) | |
313 | { | |
444eb2a4 MG |
314 | if (test_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags)) |
315 | return sprintf(buf, "[always] madvise never\n"); | |
316 | else if (test_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags)) | |
317 | return sprintf(buf, "always [madvise] never\n"); | |
318 | else | |
319 | return sprintf(buf, "always madvise [never]\n"); | |
71e3aac0 | 320 | } |
444eb2a4 | 321 | |
71e3aac0 AA |
322 | static ssize_t enabled_store(struct kobject *kobj, |
323 | struct kobj_attribute *attr, | |
324 | const char *buf, size_t count) | |
325 | { | |
ba76149f AA |
326 | ssize_t ret; |
327 | ||
444eb2a4 MG |
328 | ret = triple_flag_store(kobj, attr, buf, count, |
329 | TRANSPARENT_HUGEPAGE_FLAG, | |
ba76149f AA |
330 | TRANSPARENT_HUGEPAGE_FLAG, |
331 | TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG); | |
332 | ||
333 | if (ret > 0) { | |
911891af XG |
334 | int err; |
335 | ||
336 | mutex_lock(&khugepaged_mutex); | |
79553da2 | 337 | err = start_stop_khugepaged(); |
911891af XG |
338 | mutex_unlock(&khugepaged_mutex); |
339 | ||
ba76149f AA |
340 | if (err) |
341 | ret = err; | |
342 | } | |
343 | ||
344 | return ret; | |
71e3aac0 AA |
345 | } |
346 | static struct kobj_attribute enabled_attr = | |
347 | __ATTR(enabled, 0644, enabled_show, enabled_store); | |
348 | ||
349 | static ssize_t single_flag_show(struct kobject *kobj, | |
350 | struct kobj_attribute *attr, char *buf, | |
351 | enum transparent_hugepage_flag flag) | |
352 | { | |
e27e6151 BH |
353 | return sprintf(buf, "%d\n", |
354 | !!test_bit(flag, &transparent_hugepage_flags)); | |
71e3aac0 | 355 | } |
e27e6151 | 356 | |
71e3aac0 AA |
357 | static ssize_t single_flag_store(struct kobject *kobj, |
358 | struct kobj_attribute *attr, | |
359 | const char *buf, size_t count, | |
360 | enum transparent_hugepage_flag flag) | |
361 | { | |
e27e6151 BH |
362 | unsigned long value; |
363 | int ret; | |
364 | ||
365 | ret = kstrtoul(buf, 10, &value); | |
366 | if (ret < 0) | |
367 | return ret; | |
368 | if (value > 1) | |
369 | return -EINVAL; | |
370 | ||
371 | if (value) | |
71e3aac0 | 372 | set_bit(flag, &transparent_hugepage_flags); |
e27e6151 | 373 | else |
71e3aac0 | 374 | clear_bit(flag, &transparent_hugepage_flags); |
71e3aac0 AA |
375 | |
376 | return count; | |
377 | } | |
378 | ||
379 | /* | |
380 | * Currently defrag only disables __GFP_NOWAIT for allocation. A blind | |
381 | * __GFP_REPEAT is too aggressive, it's never worth swapping tons of | |
382 | * memory just to allocate one more hugepage. | |
383 | */ | |
384 | static ssize_t defrag_show(struct kobject *kobj, | |
385 | struct kobj_attribute *attr, char *buf) | |
386 | { | |
444eb2a4 MG |
387 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags)) |
388 | return sprintf(buf, "[always] defer madvise never\n"); | |
389 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags)) | |
390 | return sprintf(buf, "always [defer] madvise never\n"); | |
391 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags)) | |
392 | return sprintf(buf, "always defer [madvise] never\n"); | |
393 | else | |
394 | return sprintf(buf, "always defer madvise [never]\n"); | |
395 | ||
71e3aac0 AA |
396 | } |
397 | static ssize_t defrag_store(struct kobject *kobj, | |
398 | struct kobj_attribute *attr, | |
399 | const char *buf, size_t count) | |
400 | { | |
444eb2a4 MG |
401 | return triple_flag_store(kobj, attr, buf, count, |
402 | TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, | |
403 | TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, | |
71e3aac0 AA |
404 | TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG); |
405 | } | |
406 | static struct kobj_attribute defrag_attr = | |
407 | __ATTR(defrag, 0644, defrag_show, defrag_store); | |
408 | ||
79da5407 KS |
409 | static ssize_t use_zero_page_show(struct kobject *kobj, |
410 | struct kobj_attribute *attr, char *buf) | |
411 | { | |
412 | return single_flag_show(kobj, attr, buf, | |
413 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); | |
414 | } | |
415 | static ssize_t use_zero_page_store(struct kobject *kobj, | |
416 | struct kobj_attribute *attr, const char *buf, size_t count) | |
417 | { | |
418 | return single_flag_store(kobj, attr, buf, count, | |
419 | TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG); | |
420 | } | |
421 | static struct kobj_attribute use_zero_page_attr = | |
422 | __ATTR(use_zero_page, 0644, use_zero_page_show, use_zero_page_store); | |
71e3aac0 AA |
423 | #ifdef CONFIG_DEBUG_VM |
424 | static ssize_t debug_cow_show(struct kobject *kobj, | |
425 | struct kobj_attribute *attr, char *buf) | |
426 | { | |
427 | return single_flag_show(kobj, attr, buf, | |
428 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); | |
429 | } | |
430 | static ssize_t debug_cow_store(struct kobject *kobj, | |
431 | struct kobj_attribute *attr, | |
432 | const char *buf, size_t count) | |
433 | { | |
434 | return single_flag_store(kobj, attr, buf, count, | |
435 | TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG); | |
436 | } | |
437 | static struct kobj_attribute debug_cow_attr = | |
438 | __ATTR(debug_cow, 0644, debug_cow_show, debug_cow_store); | |
439 | #endif /* CONFIG_DEBUG_VM */ | |
440 | ||
441 | static struct attribute *hugepage_attr[] = { | |
442 | &enabled_attr.attr, | |
443 | &defrag_attr.attr, | |
79da5407 | 444 | &use_zero_page_attr.attr, |
5a6e75f8 KS |
445 | #ifdef CONFIG_SHMEM |
446 | &shmem_enabled_attr.attr, | |
447 | #endif | |
71e3aac0 AA |
448 | #ifdef CONFIG_DEBUG_VM |
449 | &debug_cow_attr.attr, | |
450 | #endif | |
451 | NULL, | |
452 | }; | |
453 | ||
454 | static struct attribute_group hugepage_attr_group = { | |
455 | .attrs = hugepage_attr, | |
ba76149f AA |
456 | }; |
457 | ||
458 | static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, | |
459 | struct kobj_attribute *attr, | |
460 | char *buf) | |
461 | { | |
462 | return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs); | |
463 | } | |
464 | ||
465 | static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, | |
466 | struct kobj_attribute *attr, | |
467 | const char *buf, size_t count) | |
468 | { | |
469 | unsigned long msecs; | |
470 | int err; | |
471 | ||
3dbb95f7 | 472 | err = kstrtoul(buf, 10, &msecs); |
ba76149f AA |
473 | if (err || msecs > UINT_MAX) |
474 | return -EINVAL; | |
475 | ||
476 | khugepaged_scan_sleep_millisecs = msecs; | |
f0508977 | 477 | khugepaged_sleep_expire = 0; |
ba76149f AA |
478 | wake_up_interruptible(&khugepaged_wait); |
479 | ||
480 | return count; | |
481 | } | |
482 | static struct kobj_attribute scan_sleep_millisecs_attr = | |
483 | __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show, | |
484 | scan_sleep_millisecs_store); | |
485 | ||
486 | static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj, | |
487 | struct kobj_attribute *attr, | |
488 | char *buf) | |
489 | { | |
490 | return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs); | |
491 | } | |
492 | ||
493 | static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, | |
494 | struct kobj_attribute *attr, | |
495 | const char *buf, size_t count) | |
496 | { | |
497 | unsigned long msecs; | |
498 | int err; | |
499 | ||
3dbb95f7 | 500 | err = kstrtoul(buf, 10, &msecs); |
ba76149f AA |
501 | if (err || msecs > UINT_MAX) |
502 | return -EINVAL; | |
503 | ||
504 | khugepaged_alloc_sleep_millisecs = msecs; | |
f0508977 | 505 | khugepaged_sleep_expire = 0; |
ba76149f AA |
506 | wake_up_interruptible(&khugepaged_wait); |
507 | ||
508 | return count; | |
509 | } | |
510 | static struct kobj_attribute alloc_sleep_millisecs_attr = | |
511 | __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show, | |
512 | alloc_sleep_millisecs_store); | |
513 | ||
514 | static ssize_t pages_to_scan_show(struct kobject *kobj, | |
515 | struct kobj_attribute *attr, | |
516 | char *buf) | |
517 | { | |
518 | return sprintf(buf, "%u\n", khugepaged_pages_to_scan); | |
519 | } | |
520 | static ssize_t pages_to_scan_store(struct kobject *kobj, | |
521 | struct kobj_attribute *attr, | |
522 | const char *buf, size_t count) | |
523 | { | |
524 | int err; | |
525 | unsigned long pages; | |
526 | ||
3dbb95f7 | 527 | err = kstrtoul(buf, 10, &pages); |
ba76149f AA |
528 | if (err || !pages || pages > UINT_MAX) |
529 | return -EINVAL; | |
530 | ||
531 | khugepaged_pages_to_scan = pages; | |
532 | ||
533 | return count; | |
534 | } | |
535 | static struct kobj_attribute pages_to_scan_attr = | |
536 | __ATTR(pages_to_scan, 0644, pages_to_scan_show, | |
537 | pages_to_scan_store); | |
538 | ||
539 | static ssize_t pages_collapsed_show(struct kobject *kobj, | |
540 | struct kobj_attribute *attr, | |
541 | char *buf) | |
542 | { | |
543 | return sprintf(buf, "%u\n", khugepaged_pages_collapsed); | |
544 | } | |
545 | static struct kobj_attribute pages_collapsed_attr = | |
546 | __ATTR_RO(pages_collapsed); | |
547 | ||
548 | static ssize_t full_scans_show(struct kobject *kobj, | |
549 | struct kobj_attribute *attr, | |
550 | char *buf) | |
551 | { | |
552 | return sprintf(buf, "%u\n", khugepaged_full_scans); | |
553 | } | |
554 | static struct kobj_attribute full_scans_attr = | |
555 | __ATTR_RO(full_scans); | |
556 | ||
557 | static ssize_t khugepaged_defrag_show(struct kobject *kobj, | |
558 | struct kobj_attribute *attr, char *buf) | |
559 | { | |
560 | return single_flag_show(kobj, attr, buf, | |
561 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); | |
562 | } | |
563 | static ssize_t khugepaged_defrag_store(struct kobject *kobj, | |
564 | struct kobj_attribute *attr, | |
565 | const char *buf, size_t count) | |
566 | { | |
567 | return single_flag_store(kobj, attr, buf, count, | |
568 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); | |
569 | } | |
570 | static struct kobj_attribute khugepaged_defrag_attr = | |
571 | __ATTR(defrag, 0644, khugepaged_defrag_show, | |
572 | khugepaged_defrag_store); | |
573 | ||
574 | /* | |
575 | * max_ptes_none controls if khugepaged should collapse hugepages over | |
576 | * any unmapped ptes in turn potentially increasing the memory | |
577 | * footprint of the vmas. When max_ptes_none is 0 khugepaged will not | |
578 | * reduce the available free memory in the system as it | |
579 | * runs. Increasing max_ptes_none will instead potentially reduce the | |
580 | * free memory in the system during the khugepaged scan. | |
581 | */ | |
582 | static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj, | |
583 | struct kobj_attribute *attr, | |
584 | char *buf) | |
585 | { | |
586 | return sprintf(buf, "%u\n", khugepaged_max_ptes_none); | |
587 | } | |
588 | static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj, | |
589 | struct kobj_attribute *attr, | |
590 | const char *buf, size_t count) | |
591 | { | |
592 | int err; | |
593 | unsigned long max_ptes_none; | |
594 | ||
3dbb95f7 | 595 | err = kstrtoul(buf, 10, &max_ptes_none); |
ba76149f AA |
596 | if (err || max_ptes_none > HPAGE_PMD_NR-1) |
597 | return -EINVAL; | |
598 | ||
599 | khugepaged_max_ptes_none = max_ptes_none; | |
600 | ||
601 | return count; | |
602 | } | |
603 | static struct kobj_attribute khugepaged_max_ptes_none_attr = | |
604 | __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show, | |
605 | khugepaged_max_ptes_none_store); | |
606 | ||
70652f6e EA |
607 | static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj, |
608 | struct kobj_attribute *attr, | |
609 | char *buf) | |
610 | { | |
611 | return sprintf(buf, "%u\n", khugepaged_max_ptes_swap); | |
612 | } | |
613 | ||
614 | static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj, | |
615 | struct kobj_attribute *attr, | |
616 | const char *buf, size_t count) | |
617 | { | |
618 | int err; | |
619 | unsigned long max_ptes_swap; | |
620 | ||
621 | err = kstrtoul(buf, 10, &max_ptes_swap); | |
622 | if (err || max_ptes_swap > HPAGE_PMD_NR-1) | |
623 | return -EINVAL; | |
624 | ||
625 | khugepaged_max_ptes_swap = max_ptes_swap; | |
626 | ||
627 | return count; | |
628 | } | |
629 | ||
630 | static struct kobj_attribute khugepaged_max_ptes_swap_attr = | |
631 | __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show, | |
632 | khugepaged_max_ptes_swap_store); | |
633 | ||
ba76149f AA |
634 | static struct attribute *khugepaged_attr[] = { |
635 | &khugepaged_defrag_attr.attr, | |
636 | &khugepaged_max_ptes_none_attr.attr, | |
637 | &pages_to_scan_attr.attr, | |
638 | &pages_collapsed_attr.attr, | |
639 | &full_scans_attr.attr, | |
640 | &scan_sleep_millisecs_attr.attr, | |
641 | &alloc_sleep_millisecs_attr.attr, | |
70652f6e | 642 | &khugepaged_max_ptes_swap_attr.attr, |
ba76149f AA |
643 | NULL, |
644 | }; | |
645 | ||
646 | static struct attribute_group khugepaged_attr_group = { | |
647 | .attrs = khugepaged_attr, | |
648 | .name = "khugepaged", | |
71e3aac0 | 649 | }; |
71e3aac0 | 650 | |
569e5590 | 651 | static int __init hugepage_init_sysfs(struct kobject **hugepage_kobj) |
71e3aac0 | 652 | { |
71e3aac0 AA |
653 | int err; |
654 | ||
569e5590 SL |
655 | *hugepage_kobj = kobject_create_and_add("transparent_hugepage", mm_kobj); |
656 | if (unlikely(!*hugepage_kobj)) { | |
ae3a8c1c | 657 | pr_err("failed to create transparent hugepage kobject\n"); |
569e5590 | 658 | return -ENOMEM; |
ba76149f AA |
659 | } |
660 | ||
569e5590 | 661 | err = sysfs_create_group(*hugepage_kobj, &hugepage_attr_group); |
ba76149f | 662 | if (err) { |
ae3a8c1c | 663 | pr_err("failed to register transparent hugepage group\n"); |
569e5590 | 664 | goto delete_obj; |
ba76149f AA |
665 | } |
666 | ||
569e5590 | 667 | err = sysfs_create_group(*hugepage_kobj, &khugepaged_attr_group); |
ba76149f | 668 | if (err) { |
ae3a8c1c | 669 | pr_err("failed to register transparent hugepage group\n"); |
569e5590 | 670 | goto remove_hp_group; |
ba76149f | 671 | } |
569e5590 SL |
672 | |
673 | return 0; | |
674 | ||
675 | remove_hp_group: | |
676 | sysfs_remove_group(*hugepage_kobj, &hugepage_attr_group); | |
677 | delete_obj: | |
678 | kobject_put(*hugepage_kobj); | |
679 | return err; | |
680 | } | |
681 | ||
682 | static void __init hugepage_exit_sysfs(struct kobject *hugepage_kobj) | |
683 | { | |
684 | sysfs_remove_group(hugepage_kobj, &khugepaged_attr_group); | |
685 | sysfs_remove_group(hugepage_kobj, &hugepage_attr_group); | |
686 | kobject_put(hugepage_kobj); | |
687 | } | |
688 | #else | |
689 | static inline int hugepage_init_sysfs(struct kobject **hugepage_kobj) | |
690 | { | |
691 | return 0; | |
692 | } | |
693 | ||
694 | static inline void hugepage_exit_sysfs(struct kobject *hugepage_kobj) | |
695 | { | |
696 | } | |
697 | #endif /* CONFIG_SYSFS */ | |
698 | ||
699 | static int __init hugepage_init(void) | |
700 | { | |
701 | int err; | |
702 | struct kobject *hugepage_kobj; | |
703 | ||
704 | if (!has_transparent_hugepage()) { | |
705 | transparent_hugepage_flags = 0; | |
706 | return -EINVAL; | |
707 | } | |
708 | ||
ff20c2e0 KS |
709 | khugepaged_pages_to_scan = HPAGE_PMD_NR * 8; |
710 | khugepaged_max_ptes_none = HPAGE_PMD_NR - 1; | |
70652f6e | 711 | khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8; |
ff20c2e0 KS |
712 | /* |
713 | * hugepages can't be allocated by the buddy allocator | |
714 | */ | |
715 | MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER >= MAX_ORDER); | |
716 | /* | |
717 | * we use page->mapping and page->index in second tail page | |
718 | * as list_head: assuming THP order >= 2 | |
719 | */ | |
720 | MAYBE_BUILD_BUG_ON(HPAGE_PMD_ORDER < 2); | |
721 | ||
569e5590 SL |
722 | err = hugepage_init_sysfs(&hugepage_kobj); |
723 | if (err) | |
65ebb64f | 724 | goto err_sysfs; |
ba76149f AA |
725 | |
726 | err = khugepaged_slab_init(); | |
727 | if (err) | |
65ebb64f | 728 | goto err_slab; |
ba76149f | 729 | |
65ebb64f KS |
730 | err = register_shrinker(&huge_zero_page_shrinker); |
731 | if (err) | |
732 | goto err_hzp_shrinker; | |
9a982250 KS |
733 | err = register_shrinker(&deferred_split_shrinker); |
734 | if (err) | |
735 | goto err_split_shrinker; | |
97ae1749 | 736 | |
97562cd2 RR |
737 | /* |
738 | * By default disable transparent hugepages on smaller systems, | |
739 | * where the extra memory used could hurt more than TLB overhead | |
740 | * is likely to save. The admin can still enable it through /sys. | |
741 | */ | |
79553da2 | 742 | if (totalram_pages < (512 << (20 - PAGE_SHIFT))) { |
97562cd2 | 743 | transparent_hugepage_flags = 0; |
79553da2 KS |
744 | return 0; |
745 | } | |
97562cd2 | 746 | |
79553da2 | 747 | err = start_stop_khugepaged(); |
65ebb64f KS |
748 | if (err) |
749 | goto err_khugepaged; | |
ba76149f | 750 | |
569e5590 | 751 | return 0; |
65ebb64f | 752 | err_khugepaged: |
9a982250 KS |
753 | unregister_shrinker(&deferred_split_shrinker); |
754 | err_split_shrinker: | |
65ebb64f KS |
755 | unregister_shrinker(&huge_zero_page_shrinker); |
756 | err_hzp_shrinker: | |
757 | khugepaged_slab_exit(); | |
758 | err_slab: | |
569e5590 | 759 | hugepage_exit_sysfs(hugepage_kobj); |
65ebb64f | 760 | err_sysfs: |
ba76149f | 761 | return err; |
71e3aac0 | 762 | } |
a64fb3cd | 763 | subsys_initcall(hugepage_init); |
71e3aac0 AA |
764 | |
765 | static int __init setup_transparent_hugepage(char *str) | |
766 | { | |
767 | int ret = 0; | |
768 | if (!str) | |
769 | goto out; | |
770 | if (!strcmp(str, "always")) { | |
771 | set_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
772 | &transparent_hugepage_flags); | |
773 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
774 | &transparent_hugepage_flags); | |
775 | ret = 1; | |
776 | } else if (!strcmp(str, "madvise")) { | |
777 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
778 | &transparent_hugepage_flags); | |
779 | set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
780 | &transparent_hugepage_flags); | |
781 | ret = 1; | |
782 | } else if (!strcmp(str, "never")) { | |
783 | clear_bit(TRANSPARENT_HUGEPAGE_FLAG, | |
784 | &transparent_hugepage_flags); | |
785 | clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, | |
786 | &transparent_hugepage_flags); | |
787 | ret = 1; | |
788 | } | |
789 | out: | |
790 | if (!ret) | |
ae3a8c1c | 791 | pr_warn("transparent_hugepage= cannot parse, ignored\n"); |
71e3aac0 AA |
792 | return ret; |
793 | } | |
794 | __setup("transparent_hugepage=", setup_transparent_hugepage); | |
795 | ||
b32967ff | 796 | pmd_t maybe_pmd_mkwrite(pmd_t pmd, struct vm_area_struct *vma) |
71e3aac0 AA |
797 | { |
798 | if (likely(vma->vm_flags & VM_WRITE)) | |
799 | pmd = pmd_mkwrite(pmd); | |
800 | return pmd; | |
801 | } | |
802 | ||
9a982250 KS |
803 | static inline struct list_head *page_deferred_list(struct page *page) |
804 | { | |
805 | /* | |
806 | * ->lru in the tail pages is occupied by compound_head. | |
807 | * Let's use ->mapping + ->index in the second tail page as list_head. | |
808 | */ | |
809 | return (struct list_head *)&page[2].mapping; | |
810 | } | |
811 | ||
812 | void prep_transhuge_page(struct page *page) | |
813 | { | |
814 | /* | |
815 | * we use page->mapping and page->indexlru in second tail page | |
816 | * as list_head: assuming THP order >= 2 | |
817 | */ | |
9a982250 KS |
818 | |
819 | INIT_LIST_HEAD(page_deferred_list(page)); | |
820 | set_compound_page_dtor(page, TRANSHUGE_PAGE_DTOR); | |
821 | } | |
822 | ||
bae473a4 KS |
823 | static int __do_huge_pmd_anonymous_page(struct fault_env *fe, struct page *page, |
824 | gfp_t gfp) | |
71e3aac0 | 825 | { |
bae473a4 | 826 | struct vm_area_struct *vma = fe->vma; |
00501b53 | 827 | struct mem_cgroup *memcg; |
71e3aac0 | 828 | pgtable_t pgtable; |
bae473a4 | 829 | unsigned long haddr = fe->address & HPAGE_PMD_MASK; |
71e3aac0 | 830 | |
309381fe | 831 | VM_BUG_ON_PAGE(!PageCompound(page), page); |
00501b53 | 832 | |
bae473a4 | 833 | if (mem_cgroup_try_charge(page, vma->vm_mm, gfp, &memcg, true)) { |
6b251fc9 AA |
834 | put_page(page); |
835 | count_vm_event(THP_FAULT_FALLBACK); | |
836 | return VM_FAULT_FALLBACK; | |
837 | } | |
00501b53 | 838 | |
bae473a4 | 839 | pgtable = pte_alloc_one(vma->vm_mm, haddr); |
00501b53 | 840 | if (unlikely(!pgtable)) { |
f627c2f5 | 841 | mem_cgroup_cancel_charge(page, memcg, true); |
6b251fc9 | 842 | put_page(page); |
71e3aac0 | 843 | return VM_FAULT_OOM; |
00501b53 | 844 | } |
71e3aac0 AA |
845 | |
846 | clear_huge_page(page, haddr, HPAGE_PMD_NR); | |
52f37629 MK |
847 | /* |
848 | * The memory barrier inside __SetPageUptodate makes sure that | |
849 | * clear_huge_page writes become visible before the set_pmd_at() | |
850 | * write. | |
851 | */ | |
71e3aac0 AA |
852 | __SetPageUptodate(page); |
853 | ||
bae473a4 KS |
854 | fe->ptl = pmd_lock(vma->vm_mm, fe->pmd); |
855 | if (unlikely(!pmd_none(*fe->pmd))) { | |
856 | spin_unlock(fe->ptl); | |
f627c2f5 | 857 | mem_cgroup_cancel_charge(page, memcg, true); |
71e3aac0 | 858 | put_page(page); |
bae473a4 | 859 | pte_free(vma->vm_mm, pgtable); |
71e3aac0 AA |
860 | } else { |
861 | pmd_t entry; | |
6b251fc9 AA |
862 | |
863 | /* Deliver the page fault to userland */ | |
864 | if (userfaultfd_missing(vma)) { | |
865 | int ret; | |
866 | ||
bae473a4 | 867 | spin_unlock(fe->ptl); |
f627c2f5 | 868 | mem_cgroup_cancel_charge(page, memcg, true); |
6b251fc9 | 869 | put_page(page); |
bae473a4 KS |
870 | pte_free(vma->vm_mm, pgtable); |
871 | ret = handle_userfault(fe, VM_UFFD_MISSING); | |
6b251fc9 AA |
872 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); |
873 | return ret; | |
874 | } | |
875 | ||
3122359a KS |
876 | entry = mk_huge_pmd(page, vma->vm_page_prot); |
877 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); | |
d281ee61 | 878 | page_add_new_anon_rmap(page, vma, haddr, true); |
f627c2f5 | 879 | mem_cgroup_commit_charge(page, memcg, false, true); |
00501b53 | 880 | lru_cache_add_active_or_unevictable(page, vma); |
bae473a4 KS |
881 | pgtable_trans_huge_deposit(vma->vm_mm, fe->pmd, pgtable); |
882 | set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry); | |
883 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR); | |
884 | atomic_long_inc(&vma->vm_mm->nr_ptes); | |
885 | spin_unlock(fe->ptl); | |
6b251fc9 | 886 | count_vm_event(THP_FAULT_ALLOC); |
71e3aac0 AA |
887 | } |
888 | ||
aa2e878e | 889 | return 0; |
71e3aac0 AA |
890 | } |
891 | ||
444eb2a4 MG |
892 | /* |
893 | * If THP is set to always then directly reclaim/compact as necessary | |
894 | * If set to defer then do no reclaim and defer to khugepaged | |
895 | * If set to madvise and the VMA is flagged then directly reclaim/compact | |
896 | */ | |
897 | static inline gfp_t alloc_hugepage_direct_gfpmask(struct vm_area_struct *vma) | |
898 | { | |
899 | gfp_t reclaim_flags = 0; | |
900 | ||
901 | if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG, &transparent_hugepage_flags) && | |
902 | (vma->vm_flags & VM_HUGEPAGE)) | |
903 | reclaim_flags = __GFP_DIRECT_RECLAIM; | |
904 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, &transparent_hugepage_flags)) | |
905 | reclaim_flags = __GFP_KSWAPD_RECLAIM; | |
906 | else if (test_bit(TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, &transparent_hugepage_flags)) | |
907 | reclaim_flags = __GFP_DIRECT_RECLAIM; | |
908 | ||
909 | return GFP_TRANSHUGE | reclaim_flags; | |
910 | } | |
911 | ||
912 | /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */ | |
913 | static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void) | |
0bbbc0b3 | 914 | { |
444eb2a4 | 915 | return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0); |
0bbbc0b3 AA |
916 | } |
917 | ||
c4088ebd | 918 | /* Caller must hold page table lock. */ |
d295e341 | 919 | static bool set_huge_zero_page(pgtable_t pgtable, struct mm_struct *mm, |
97ae1749 | 920 | struct vm_area_struct *vma, unsigned long haddr, pmd_t *pmd, |
5918d10a | 921 | struct page *zero_page) |
fc9fe822 KS |
922 | { |
923 | pmd_t entry; | |
7c414164 AM |
924 | if (!pmd_none(*pmd)) |
925 | return false; | |
5918d10a | 926 | entry = mk_pmd(zero_page, vma->vm_page_prot); |
fc9fe822 | 927 | entry = pmd_mkhuge(entry); |
12c9d70b MW |
928 | if (pgtable) |
929 | pgtable_trans_huge_deposit(mm, pmd, pgtable); | |
fc9fe822 | 930 | set_pmd_at(mm, haddr, pmd, entry); |
e1f56c89 | 931 | atomic_long_inc(&mm->nr_ptes); |
7c414164 | 932 | return true; |
fc9fe822 KS |
933 | } |
934 | ||
bae473a4 | 935 | int do_huge_pmd_anonymous_page(struct fault_env *fe) |
71e3aac0 | 936 | { |
bae473a4 | 937 | struct vm_area_struct *vma = fe->vma; |
077fcf11 | 938 | gfp_t gfp; |
71e3aac0 | 939 | struct page *page; |
bae473a4 | 940 | unsigned long haddr = fe->address & HPAGE_PMD_MASK; |
71e3aac0 | 941 | |
128ec037 | 942 | if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end) |
c0292554 | 943 | return VM_FAULT_FALLBACK; |
128ec037 KS |
944 | if (unlikely(anon_vma_prepare(vma))) |
945 | return VM_FAULT_OOM; | |
6d50e60c | 946 | if (unlikely(khugepaged_enter(vma, vma->vm_flags))) |
128ec037 | 947 | return VM_FAULT_OOM; |
bae473a4 KS |
948 | if (!(fe->flags & FAULT_FLAG_WRITE) && |
949 | !mm_forbids_zeropage(vma->vm_mm) && | |
128ec037 KS |
950 | transparent_hugepage_use_zero_page()) { |
951 | pgtable_t pgtable; | |
952 | struct page *zero_page; | |
953 | bool set; | |
6b251fc9 | 954 | int ret; |
bae473a4 | 955 | pgtable = pte_alloc_one(vma->vm_mm, haddr); |
128ec037 | 956 | if (unlikely(!pgtable)) |
ba76149f | 957 | return VM_FAULT_OOM; |
128ec037 KS |
958 | zero_page = get_huge_zero_page(); |
959 | if (unlikely(!zero_page)) { | |
bae473a4 | 960 | pte_free(vma->vm_mm, pgtable); |
81ab4201 | 961 | count_vm_event(THP_FAULT_FALLBACK); |
c0292554 | 962 | return VM_FAULT_FALLBACK; |
b9bbfbe3 | 963 | } |
bae473a4 | 964 | fe->ptl = pmd_lock(vma->vm_mm, fe->pmd); |
6b251fc9 AA |
965 | ret = 0; |
966 | set = false; | |
bae473a4 | 967 | if (pmd_none(*fe->pmd)) { |
6b251fc9 | 968 | if (userfaultfd_missing(vma)) { |
bae473a4 KS |
969 | spin_unlock(fe->ptl); |
970 | ret = handle_userfault(fe, VM_UFFD_MISSING); | |
6b251fc9 AA |
971 | VM_BUG_ON(ret & VM_FAULT_FALLBACK); |
972 | } else { | |
bae473a4 KS |
973 | set_huge_zero_page(pgtable, vma->vm_mm, vma, |
974 | haddr, fe->pmd, zero_page); | |
975 | spin_unlock(fe->ptl); | |
6b251fc9 AA |
976 | set = true; |
977 | } | |
978 | } else | |
bae473a4 | 979 | spin_unlock(fe->ptl); |
128ec037 | 980 | if (!set) { |
bae473a4 | 981 | pte_free(vma->vm_mm, pgtable); |
128ec037 | 982 | put_huge_zero_page(); |
edad9d2c | 983 | } |
6b251fc9 | 984 | return ret; |
71e3aac0 | 985 | } |
444eb2a4 | 986 | gfp = alloc_hugepage_direct_gfpmask(vma); |
077fcf11 | 987 | page = alloc_hugepage_vma(gfp, vma, haddr, HPAGE_PMD_ORDER); |
128ec037 KS |
988 | if (unlikely(!page)) { |
989 | count_vm_event(THP_FAULT_FALLBACK); | |
c0292554 | 990 | return VM_FAULT_FALLBACK; |
128ec037 | 991 | } |
9a982250 | 992 | prep_transhuge_page(page); |
bae473a4 | 993 | return __do_huge_pmd_anonymous_page(fe, page, gfp); |
71e3aac0 AA |
994 | } |
995 | ||
ae18d6dc | 996 | static void insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, |
f25748e3 | 997 | pmd_t *pmd, pfn_t pfn, pgprot_t prot, bool write) |
5cad465d MW |
998 | { |
999 | struct mm_struct *mm = vma->vm_mm; | |
1000 | pmd_t entry; | |
1001 | spinlock_t *ptl; | |
1002 | ||
1003 | ptl = pmd_lock(mm, pmd); | |
f25748e3 DW |
1004 | entry = pmd_mkhuge(pfn_t_pmd(pfn, prot)); |
1005 | if (pfn_t_devmap(pfn)) | |
1006 | entry = pmd_mkdevmap(entry); | |
01871e59 RZ |
1007 | if (write) { |
1008 | entry = pmd_mkyoung(pmd_mkdirty(entry)); | |
1009 | entry = maybe_pmd_mkwrite(entry, vma); | |
5cad465d | 1010 | } |
01871e59 RZ |
1011 | set_pmd_at(mm, addr, pmd, entry); |
1012 | update_mmu_cache_pmd(vma, addr, pmd); | |
5cad465d | 1013 | spin_unlock(ptl); |
5cad465d MW |
1014 | } |
1015 | ||
1016 | int vmf_insert_pfn_pmd(struct vm_area_struct *vma, unsigned long addr, | |
f25748e3 | 1017 | pmd_t *pmd, pfn_t pfn, bool write) |
5cad465d MW |
1018 | { |
1019 | pgprot_t pgprot = vma->vm_page_prot; | |
1020 | /* | |
1021 | * If we had pmd_special, we could avoid all these restrictions, | |
1022 | * but we need to be consistent with PTEs and architectures that | |
1023 | * can't support a 'special' bit. | |
1024 | */ | |
1025 | BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))); | |
1026 | BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) == | |
1027 | (VM_PFNMAP|VM_MIXEDMAP)); | |
1028 | BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags)); | |
f25748e3 | 1029 | BUG_ON(!pfn_t_devmap(pfn)); |
5cad465d MW |
1030 | |
1031 | if (addr < vma->vm_start || addr >= vma->vm_end) | |
1032 | return VM_FAULT_SIGBUS; | |
1033 | if (track_pfn_insert(vma, &pgprot, pfn)) | |
1034 | return VM_FAULT_SIGBUS; | |
ae18d6dc MW |
1035 | insert_pfn_pmd(vma, addr, pmd, pfn, pgprot, write); |
1036 | return VM_FAULT_NOPAGE; | |
5cad465d | 1037 | } |
dee41079 | 1038 | EXPORT_SYMBOL_GPL(vmf_insert_pfn_pmd); |
5cad465d | 1039 | |
3565fce3 DW |
1040 | static void touch_pmd(struct vm_area_struct *vma, unsigned long addr, |
1041 | pmd_t *pmd) | |
1042 | { | |
1043 | pmd_t _pmd; | |
1044 | ||
1045 | /* | |
1046 | * We should set the dirty bit only for FOLL_WRITE but for now | |
1047 | * the dirty bit in the pmd is meaningless. And if the dirty | |
1048 | * bit will become meaningful and we'll only set it with | |
1049 | * FOLL_WRITE, an atomic set_bit will be required on the pmd to | |
1050 | * set the young bit, instead of the current set_pmd_at. | |
1051 | */ | |
1052 | _pmd = pmd_mkyoung(pmd_mkdirty(*pmd)); | |
1053 | if (pmdp_set_access_flags(vma, addr & HPAGE_PMD_MASK, | |
1054 | pmd, _pmd, 1)) | |
1055 | update_mmu_cache_pmd(vma, addr, pmd); | |
1056 | } | |
1057 | ||
1058 | struct page *follow_devmap_pmd(struct vm_area_struct *vma, unsigned long addr, | |
1059 | pmd_t *pmd, int flags) | |
1060 | { | |
1061 | unsigned long pfn = pmd_pfn(*pmd); | |
1062 | struct mm_struct *mm = vma->vm_mm; | |
1063 | struct dev_pagemap *pgmap; | |
1064 | struct page *page; | |
1065 | ||
1066 | assert_spin_locked(pmd_lockptr(mm, pmd)); | |
1067 | ||
1068 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) | |
1069 | return NULL; | |
1070 | ||
1071 | if (pmd_present(*pmd) && pmd_devmap(*pmd)) | |
1072 | /* pass */; | |
1073 | else | |
1074 | return NULL; | |
1075 | ||
1076 | if (flags & FOLL_TOUCH) | |
1077 | touch_pmd(vma, addr, pmd); | |
1078 | ||
1079 | /* | |
1080 | * device mapped pages can only be returned if the | |
1081 | * caller will manage the page reference count. | |
1082 | */ | |
1083 | if (!(flags & FOLL_GET)) | |
1084 | return ERR_PTR(-EEXIST); | |
1085 | ||
1086 | pfn += (addr & ~PMD_MASK) >> PAGE_SHIFT; | |
1087 | pgmap = get_dev_pagemap(pfn, NULL); | |
1088 | if (!pgmap) | |
1089 | return ERR_PTR(-EFAULT); | |
1090 | page = pfn_to_page(pfn); | |
1091 | get_page(page); | |
1092 | put_dev_pagemap(pgmap); | |
1093 | ||
1094 | return page; | |
1095 | } | |
1096 | ||
71e3aac0 AA |
1097 | int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, |
1098 | pmd_t *dst_pmd, pmd_t *src_pmd, unsigned long addr, | |
1099 | struct vm_area_struct *vma) | |
1100 | { | |
c4088ebd | 1101 | spinlock_t *dst_ptl, *src_ptl; |
71e3aac0 AA |
1102 | struct page *src_page; |
1103 | pmd_t pmd; | |
12c9d70b | 1104 | pgtable_t pgtable = NULL; |
628d47ce | 1105 | int ret = -ENOMEM; |
71e3aac0 | 1106 | |
628d47ce KS |
1107 | /* Skip if can be re-fill on fault */ |
1108 | if (!vma_is_anonymous(vma)) | |
1109 | return 0; | |
1110 | ||
1111 | pgtable = pte_alloc_one(dst_mm, addr); | |
1112 | if (unlikely(!pgtable)) | |
1113 | goto out; | |
71e3aac0 | 1114 | |
c4088ebd KS |
1115 | dst_ptl = pmd_lock(dst_mm, dst_pmd); |
1116 | src_ptl = pmd_lockptr(src_mm, src_pmd); | |
1117 | spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING); | |
71e3aac0 AA |
1118 | |
1119 | ret = -EAGAIN; | |
1120 | pmd = *src_pmd; | |
628d47ce | 1121 | if (unlikely(!pmd_trans_huge(pmd))) { |
71e3aac0 AA |
1122 | pte_free(dst_mm, pgtable); |
1123 | goto out_unlock; | |
1124 | } | |
fc9fe822 | 1125 | /* |
c4088ebd | 1126 | * When page table lock is held, the huge zero pmd should not be |
fc9fe822 KS |
1127 | * under splitting since we don't split the page itself, only pmd to |
1128 | * a page table. | |
1129 | */ | |
1130 | if (is_huge_zero_pmd(pmd)) { | |
5918d10a | 1131 | struct page *zero_page; |
97ae1749 KS |
1132 | /* |
1133 | * get_huge_zero_page() will never allocate a new page here, | |
1134 | * since we already have a zero page to copy. It just takes a | |
1135 | * reference. | |
1136 | */ | |
5918d10a | 1137 | zero_page = get_huge_zero_page(); |
6b251fc9 | 1138 | set_huge_zero_page(pgtable, dst_mm, vma, addr, dst_pmd, |
5918d10a | 1139 | zero_page); |
fc9fe822 KS |
1140 | ret = 0; |
1141 | goto out_unlock; | |
1142 | } | |
de466bd6 | 1143 | |
628d47ce KS |
1144 | src_page = pmd_page(pmd); |
1145 | VM_BUG_ON_PAGE(!PageHead(src_page), src_page); | |
1146 | get_page(src_page); | |
1147 | page_dup_rmap(src_page, true); | |
1148 | add_mm_counter(dst_mm, MM_ANONPAGES, HPAGE_PMD_NR); | |
1149 | atomic_long_inc(&dst_mm->nr_ptes); | |
1150 | pgtable_trans_huge_deposit(dst_mm, dst_pmd, pgtable); | |
71e3aac0 AA |
1151 | |
1152 | pmdp_set_wrprotect(src_mm, addr, src_pmd); | |
1153 | pmd = pmd_mkold(pmd_wrprotect(pmd)); | |
1154 | set_pmd_at(dst_mm, addr, dst_pmd, pmd); | |
71e3aac0 AA |
1155 | |
1156 | ret = 0; | |
1157 | out_unlock: | |
c4088ebd KS |
1158 | spin_unlock(src_ptl); |
1159 | spin_unlock(dst_ptl); | |
71e3aac0 AA |
1160 | out: |
1161 | return ret; | |
1162 | } | |
1163 | ||
bae473a4 | 1164 | void huge_pmd_set_accessed(struct fault_env *fe, pmd_t orig_pmd) |
a1dd450b WD |
1165 | { |
1166 | pmd_t entry; | |
1167 | unsigned long haddr; | |
1168 | ||
bae473a4 KS |
1169 | fe->ptl = pmd_lock(fe->vma->vm_mm, fe->pmd); |
1170 | if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) | |
a1dd450b WD |
1171 | goto unlock; |
1172 | ||
1173 | entry = pmd_mkyoung(orig_pmd); | |
bae473a4 KS |
1174 | haddr = fe->address & HPAGE_PMD_MASK; |
1175 | if (pmdp_set_access_flags(fe->vma, haddr, fe->pmd, entry, | |
1176 | fe->flags & FAULT_FLAG_WRITE)) | |
1177 | update_mmu_cache_pmd(fe->vma, fe->address, fe->pmd); | |
a1dd450b WD |
1178 | |
1179 | unlock: | |
bae473a4 | 1180 | spin_unlock(fe->ptl); |
a1dd450b WD |
1181 | } |
1182 | ||
bae473a4 KS |
1183 | static int do_huge_pmd_wp_page_fallback(struct fault_env *fe, pmd_t orig_pmd, |
1184 | struct page *page) | |
71e3aac0 | 1185 | { |
bae473a4 KS |
1186 | struct vm_area_struct *vma = fe->vma; |
1187 | unsigned long haddr = fe->address & HPAGE_PMD_MASK; | |
00501b53 | 1188 | struct mem_cgroup *memcg; |
71e3aac0 AA |
1189 | pgtable_t pgtable; |
1190 | pmd_t _pmd; | |
1191 | int ret = 0, i; | |
1192 | struct page **pages; | |
2ec74c3e SG |
1193 | unsigned long mmun_start; /* For mmu_notifiers */ |
1194 | unsigned long mmun_end; /* For mmu_notifiers */ | |
71e3aac0 AA |
1195 | |
1196 | pages = kmalloc(sizeof(struct page *) * HPAGE_PMD_NR, | |
1197 | GFP_KERNEL); | |
1198 | if (unlikely(!pages)) { | |
1199 | ret |= VM_FAULT_OOM; | |
1200 | goto out; | |
1201 | } | |
1202 | ||
1203 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
cc5d462f | 1204 | pages[i] = alloc_page_vma_node(GFP_HIGHUSER_MOVABLE | |
bae473a4 KS |
1205 | __GFP_OTHER_NODE, vma, |
1206 | fe->address, page_to_nid(page)); | |
b9bbfbe3 | 1207 | if (unlikely(!pages[i] || |
bae473a4 KS |
1208 | mem_cgroup_try_charge(pages[i], vma->vm_mm, |
1209 | GFP_KERNEL, &memcg, false))) { | |
b9bbfbe3 | 1210 | if (pages[i]) |
71e3aac0 | 1211 | put_page(pages[i]); |
b9bbfbe3 | 1212 | while (--i >= 0) { |
00501b53 JW |
1213 | memcg = (void *)page_private(pages[i]); |
1214 | set_page_private(pages[i], 0); | |
f627c2f5 KS |
1215 | mem_cgroup_cancel_charge(pages[i], memcg, |
1216 | false); | |
b9bbfbe3 AA |
1217 | put_page(pages[i]); |
1218 | } | |
71e3aac0 AA |
1219 | kfree(pages); |
1220 | ret |= VM_FAULT_OOM; | |
1221 | goto out; | |
1222 | } | |
00501b53 | 1223 | set_page_private(pages[i], (unsigned long)memcg); |
71e3aac0 AA |
1224 | } |
1225 | ||
1226 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
1227 | copy_user_highpage(pages[i], page + i, | |
0089e485 | 1228 | haddr + PAGE_SIZE * i, vma); |
71e3aac0 AA |
1229 | __SetPageUptodate(pages[i]); |
1230 | cond_resched(); | |
1231 | } | |
1232 | ||
2ec74c3e SG |
1233 | mmun_start = haddr; |
1234 | mmun_end = haddr + HPAGE_PMD_SIZE; | |
bae473a4 | 1235 | mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end); |
2ec74c3e | 1236 | |
bae473a4 KS |
1237 | fe->ptl = pmd_lock(vma->vm_mm, fe->pmd); |
1238 | if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) | |
71e3aac0 | 1239 | goto out_free_pages; |
309381fe | 1240 | VM_BUG_ON_PAGE(!PageHead(page), page); |
71e3aac0 | 1241 | |
bae473a4 | 1242 | pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd); |
71e3aac0 AA |
1243 | /* leave pmd empty until pte is filled */ |
1244 | ||
bae473a4 KS |
1245 | pgtable = pgtable_trans_huge_withdraw(vma->vm_mm, fe->pmd); |
1246 | pmd_populate(vma->vm_mm, &_pmd, pgtable); | |
71e3aac0 AA |
1247 | |
1248 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { | |
bae473a4 | 1249 | pte_t entry; |
71e3aac0 AA |
1250 | entry = mk_pte(pages[i], vma->vm_page_prot); |
1251 | entry = maybe_mkwrite(pte_mkdirty(entry), vma); | |
00501b53 JW |
1252 | memcg = (void *)page_private(pages[i]); |
1253 | set_page_private(pages[i], 0); | |
bae473a4 | 1254 | page_add_new_anon_rmap(pages[i], fe->vma, haddr, false); |
f627c2f5 | 1255 | mem_cgroup_commit_charge(pages[i], memcg, false, false); |
00501b53 | 1256 | lru_cache_add_active_or_unevictable(pages[i], vma); |
bae473a4 KS |
1257 | fe->pte = pte_offset_map(&_pmd, haddr); |
1258 | VM_BUG_ON(!pte_none(*fe->pte)); | |
1259 | set_pte_at(vma->vm_mm, haddr, fe->pte, entry); | |
1260 | pte_unmap(fe->pte); | |
71e3aac0 AA |
1261 | } |
1262 | kfree(pages); | |
1263 | ||
71e3aac0 | 1264 | smp_wmb(); /* make pte visible before pmd */ |
bae473a4 | 1265 | pmd_populate(vma->vm_mm, fe->pmd, pgtable); |
d281ee61 | 1266 | page_remove_rmap(page, true); |
bae473a4 | 1267 | spin_unlock(fe->ptl); |
71e3aac0 | 1268 | |
bae473a4 | 1269 | mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end); |
2ec74c3e | 1270 | |
71e3aac0 AA |
1271 | ret |= VM_FAULT_WRITE; |
1272 | put_page(page); | |
1273 | ||
1274 | out: | |
1275 | return ret; | |
1276 | ||
1277 | out_free_pages: | |
bae473a4 KS |
1278 | spin_unlock(fe->ptl); |
1279 | mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end); | |
b9bbfbe3 | 1280 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
00501b53 JW |
1281 | memcg = (void *)page_private(pages[i]); |
1282 | set_page_private(pages[i], 0); | |
f627c2f5 | 1283 | mem_cgroup_cancel_charge(pages[i], memcg, false); |
71e3aac0 | 1284 | put_page(pages[i]); |
b9bbfbe3 | 1285 | } |
71e3aac0 AA |
1286 | kfree(pages); |
1287 | goto out; | |
1288 | } | |
1289 | ||
bae473a4 | 1290 | int do_huge_pmd_wp_page(struct fault_env *fe, pmd_t orig_pmd) |
71e3aac0 | 1291 | { |
bae473a4 | 1292 | struct vm_area_struct *vma = fe->vma; |
93b4796d | 1293 | struct page *page = NULL, *new_page; |
00501b53 | 1294 | struct mem_cgroup *memcg; |
bae473a4 | 1295 | unsigned long haddr = fe->address & HPAGE_PMD_MASK; |
2ec74c3e SG |
1296 | unsigned long mmun_start; /* For mmu_notifiers */ |
1297 | unsigned long mmun_end; /* For mmu_notifiers */ | |
3b363692 | 1298 | gfp_t huge_gfp; /* for allocation and charge */ |
bae473a4 | 1299 | int ret = 0; |
71e3aac0 | 1300 | |
bae473a4 | 1301 | fe->ptl = pmd_lockptr(vma->vm_mm, fe->pmd); |
81d1b09c | 1302 | VM_BUG_ON_VMA(!vma->anon_vma, vma); |
93b4796d KS |
1303 | if (is_huge_zero_pmd(orig_pmd)) |
1304 | goto alloc; | |
bae473a4 KS |
1305 | spin_lock(fe->ptl); |
1306 | if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) | |
71e3aac0 AA |
1307 | goto out_unlock; |
1308 | ||
1309 | page = pmd_page(orig_pmd); | |
309381fe | 1310 | VM_BUG_ON_PAGE(!PageCompound(page) || !PageHead(page), page); |
1f25fe20 KS |
1311 | /* |
1312 | * We can only reuse the page if nobody else maps the huge page or it's | |
6d0a07ed | 1313 | * part. |
1f25fe20 | 1314 | */ |
6d0a07ed | 1315 | if (page_trans_huge_mapcount(page, NULL) == 1) { |
71e3aac0 AA |
1316 | pmd_t entry; |
1317 | entry = pmd_mkyoung(orig_pmd); | |
1318 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); | |
bae473a4 KS |
1319 | if (pmdp_set_access_flags(vma, haddr, fe->pmd, entry, 1)) |
1320 | update_mmu_cache_pmd(vma, fe->address, fe->pmd); | |
71e3aac0 AA |
1321 | ret |= VM_FAULT_WRITE; |
1322 | goto out_unlock; | |
1323 | } | |
ddc58f27 | 1324 | get_page(page); |
bae473a4 | 1325 | spin_unlock(fe->ptl); |
93b4796d | 1326 | alloc: |
71e3aac0 | 1327 | if (transparent_hugepage_enabled(vma) && |
077fcf11 | 1328 | !transparent_hugepage_debug_cow()) { |
444eb2a4 | 1329 | huge_gfp = alloc_hugepage_direct_gfpmask(vma); |
3b363692 | 1330 | new_page = alloc_hugepage_vma(huge_gfp, vma, haddr, HPAGE_PMD_ORDER); |
077fcf11 | 1331 | } else |
71e3aac0 AA |
1332 | new_page = NULL; |
1333 | ||
9a982250 KS |
1334 | if (likely(new_page)) { |
1335 | prep_transhuge_page(new_page); | |
1336 | } else { | |
eecc1e42 | 1337 | if (!page) { |
bae473a4 | 1338 | split_huge_pmd(vma, fe->pmd, fe->address); |
e9b71ca9 | 1339 | ret |= VM_FAULT_FALLBACK; |
93b4796d | 1340 | } else { |
bae473a4 | 1341 | ret = do_huge_pmd_wp_page_fallback(fe, orig_pmd, page); |
9845cbbd | 1342 | if (ret & VM_FAULT_OOM) { |
bae473a4 | 1343 | split_huge_pmd(vma, fe->pmd, fe->address); |
9845cbbd KS |
1344 | ret |= VM_FAULT_FALLBACK; |
1345 | } | |
ddc58f27 | 1346 | put_page(page); |
93b4796d | 1347 | } |
17766dde | 1348 | count_vm_event(THP_FAULT_FALLBACK); |
71e3aac0 AA |
1349 | goto out; |
1350 | } | |
1351 | ||
bae473a4 KS |
1352 | if (unlikely(mem_cgroup_try_charge(new_page, vma->vm_mm, |
1353 | huge_gfp, &memcg, true))) { | |
b9bbfbe3 | 1354 | put_page(new_page); |
bae473a4 KS |
1355 | split_huge_pmd(vma, fe->pmd, fe->address); |
1356 | if (page) | |
ddc58f27 | 1357 | put_page(page); |
9845cbbd | 1358 | ret |= VM_FAULT_FALLBACK; |
17766dde | 1359 | count_vm_event(THP_FAULT_FALLBACK); |
b9bbfbe3 AA |
1360 | goto out; |
1361 | } | |
1362 | ||
17766dde DR |
1363 | count_vm_event(THP_FAULT_ALLOC); |
1364 | ||
eecc1e42 | 1365 | if (!page) |
93b4796d KS |
1366 | clear_huge_page(new_page, haddr, HPAGE_PMD_NR); |
1367 | else | |
1368 | copy_user_huge_page(new_page, page, haddr, vma, HPAGE_PMD_NR); | |
71e3aac0 AA |
1369 | __SetPageUptodate(new_page); |
1370 | ||
2ec74c3e SG |
1371 | mmun_start = haddr; |
1372 | mmun_end = haddr + HPAGE_PMD_SIZE; | |
bae473a4 | 1373 | mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end); |
2ec74c3e | 1374 | |
bae473a4 | 1375 | spin_lock(fe->ptl); |
93b4796d | 1376 | if (page) |
ddc58f27 | 1377 | put_page(page); |
bae473a4 KS |
1378 | if (unlikely(!pmd_same(*fe->pmd, orig_pmd))) { |
1379 | spin_unlock(fe->ptl); | |
f627c2f5 | 1380 | mem_cgroup_cancel_charge(new_page, memcg, true); |
71e3aac0 | 1381 | put_page(new_page); |
2ec74c3e | 1382 | goto out_mn; |
b9bbfbe3 | 1383 | } else { |
71e3aac0 | 1384 | pmd_t entry; |
3122359a KS |
1385 | entry = mk_huge_pmd(new_page, vma->vm_page_prot); |
1386 | entry = maybe_pmd_mkwrite(pmd_mkdirty(entry), vma); | |
bae473a4 | 1387 | pmdp_huge_clear_flush_notify(vma, haddr, fe->pmd); |
d281ee61 | 1388 | page_add_new_anon_rmap(new_page, vma, haddr, true); |
f627c2f5 | 1389 | mem_cgroup_commit_charge(new_page, memcg, false, true); |
00501b53 | 1390 | lru_cache_add_active_or_unevictable(new_page, vma); |
bae473a4 KS |
1391 | set_pmd_at(vma->vm_mm, haddr, fe->pmd, entry); |
1392 | update_mmu_cache_pmd(vma, fe->address, fe->pmd); | |
eecc1e42 | 1393 | if (!page) { |
bae473a4 | 1394 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR); |
97ae1749 KS |
1395 | put_huge_zero_page(); |
1396 | } else { | |
309381fe | 1397 | VM_BUG_ON_PAGE(!PageHead(page), page); |
d281ee61 | 1398 | page_remove_rmap(page, true); |
93b4796d KS |
1399 | put_page(page); |
1400 | } | |
71e3aac0 AA |
1401 | ret |= VM_FAULT_WRITE; |
1402 | } | |
bae473a4 | 1403 | spin_unlock(fe->ptl); |
2ec74c3e | 1404 | out_mn: |
bae473a4 | 1405 | mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end); |
71e3aac0 AA |
1406 | out: |
1407 | return ret; | |
2ec74c3e | 1408 | out_unlock: |
bae473a4 | 1409 | spin_unlock(fe->ptl); |
2ec74c3e | 1410 | return ret; |
71e3aac0 AA |
1411 | } |
1412 | ||
b676b293 | 1413 | struct page *follow_trans_huge_pmd(struct vm_area_struct *vma, |
71e3aac0 AA |
1414 | unsigned long addr, |
1415 | pmd_t *pmd, | |
1416 | unsigned int flags) | |
1417 | { | |
b676b293 | 1418 | struct mm_struct *mm = vma->vm_mm; |
71e3aac0 AA |
1419 | struct page *page = NULL; |
1420 | ||
c4088ebd | 1421 | assert_spin_locked(pmd_lockptr(mm, pmd)); |
71e3aac0 AA |
1422 | |
1423 | if (flags & FOLL_WRITE && !pmd_write(*pmd)) | |
1424 | goto out; | |
1425 | ||
85facf25 KS |
1426 | /* Avoid dumping huge zero page */ |
1427 | if ((flags & FOLL_DUMP) && is_huge_zero_pmd(*pmd)) | |
1428 | return ERR_PTR(-EFAULT); | |
1429 | ||
2b4847e7 | 1430 | /* Full NUMA hinting faults to serialise migration in fault paths */ |
8a0516ed | 1431 | if ((flags & FOLL_NUMA) && pmd_protnone(*pmd)) |
2b4847e7 MG |
1432 | goto out; |
1433 | ||
71e3aac0 | 1434 | page = pmd_page(*pmd); |
309381fe | 1435 | VM_BUG_ON_PAGE(!PageHead(page), page); |
3565fce3 DW |
1436 | if (flags & FOLL_TOUCH) |
1437 | touch_pmd(vma, addr, pmd); | |
de60f5f1 | 1438 | if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) { |
e90309c9 KS |
1439 | /* |
1440 | * We don't mlock() pte-mapped THPs. This way we can avoid | |
1441 | * leaking mlocked pages into non-VM_LOCKED VMAs. | |
1442 | * | |
9a73f61b KS |
1443 | * For anon THP: |
1444 | * | |
e90309c9 KS |
1445 | * In most cases the pmd is the only mapping of the page as we |
1446 | * break COW for the mlock() -- see gup_flags |= FOLL_WRITE for | |
1447 | * writable private mappings in populate_vma_page_range(). | |
1448 | * | |
1449 | * The only scenario when we have the page shared here is if we | |
1450 | * mlocking read-only mapping shared over fork(). We skip | |
1451 | * mlocking such pages. | |
9a73f61b KS |
1452 | * |
1453 | * For file THP: | |
1454 | * | |
1455 | * We can expect PageDoubleMap() to be stable under page lock: | |
1456 | * for file pages we set it in page_add_file_rmap(), which | |
1457 | * requires page to be locked. | |
e90309c9 | 1458 | */ |
9a73f61b KS |
1459 | |
1460 | if (PageAnon(page) && compound_mapcount(page) != 1) | |
1461 | goto skip_mlock; | |
1462 | if (PageDoubleMap(page) || !page->mapping) | |
1463 | goto skip_mlock; | |
1464 | if (!trylock_page(page)) | |
1465 | goto skip_mlock; | |
1466 | lru_add_drain(); | |
1467 | if (page->mapping && !PageDoubleMap(page)) | |
1468 | mlock_vma_page(page); | |
1469 | unlock_page(page); | |
b676b293 | 1470 | } |
9a73f61b | 1471 | skip_mlock: |
71e3aac0 | 1472 | page += (addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT; |
309381fe | 1473 | VM_BUG_ON_PAGE(!PageCompound(page), page); |
71e3aac0 | 1474 | if (flags & FOLL_GET) |
ddc58f27 | 1475 | get_page(page); |
71e3aac0 AA |
1476 | |
1477 | out: | |
1478 | return page; | |
1479 | } | |
1480 | ||
d10e63f2 | 1481 | /* NUMA hinting page fault entry point for trans huge pmds */ |
bae473a4 | 1482 | int do_huge_pmd_numa_page(struct fault_env *fe, pmd_t pmd) |
d10e63f2 | 1483 | { |
bae473a4 | 1484 | struct vm_area_struct *vma = fe->vma; |
b8916634 | 1485 | struct anon_vma *anon_vma = NULL; |
b32967ff | 1486 | struct page *page; |
bae473a4 | 1487 | unsigned long haddr = fe->address & HPAGE_PMD_MASK; |
8191acbd | 1488 | int page_nid = -1, this_nid = numa_node_id(); |
90572890 | 1489 | int target_nid, last_cpupid = -1; |
8191acbd MG |
1490 | bool page_locked; |
1491 | bool migrated = false; | |
b191f9b1 | 1492 | bool was_writable; |
6688cc05 | 1493 | int flags = 0; |
d10e63f2 | 1494 | |
c0e7cad9 MG |
1495 | /* A PROT_NONE fault should not end up here */ |
1496 | BUG_ON(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))); | |
1497 | ||
bae473a4 KS |
1498 | fe->ptl = pmd_lock(vma->vm_mm, fe->pmd); |
1499 | if (unlikely(!pmd_same(pmd, *fe->pmd))) | |
d10e63f2 MG |
1500 | goto out_unlock; |
1501 | ||
de466bd6 MG |
1502 | /* |
1503 | * If there are potential migrations, wait for completion and retry | |
1504 | * without disrupting NUMA hinting information. Do not relock and | |
1505 | * check_same as the page may no longer be mapped. | |
1506 | */ | |
bae473a4 KS |
1507 | if (unlikely(pmd_trans_migrating(*fe->pmd))) { |
1508 | page = pmd_page(*fe->pmd); | |
1509 | spin_unlock(fe->ptl); | |
5d833062 | 1510 | wait_on_page_locked(page); |
de466bd6 MG |
1511 | goto out; |
1512 | } | |
1513 | ||
d10e63f2 | 1514 | page = pmd_page(pmd); |
a1a46184 | 1515 | BUG_ON(is_huge_zero_page(page)); |
8191acbd | 1516 | page_nid = page_to_nid(page); |
90572890 | 1517 | last_cpupid = page_cpupid_last(page); |
03c5a6e1 | 1518 | count_vm_numa_event(NUMA_HINT_FAULTS); |
04bb2f94 | 1519 | if (page_nid == this_nid) { |
03c5a6e1 | 1520 | count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL); |
04bb2f94 RR |
1521 | flags |= TNF_FAULT_LOCAL; |
1522 | } | |
4daae3b4 | 1523 | |
bea66fbd MG |
1524 | /* See similar comment in do_numa_page for explanation */ |
1525 | if (!(vma->vm_flags & VM_WRITE)) | |
6688cc05 PZ |
1526 | flags |= TNF_NO_GROUP; |
1527 | ||
ff9042b1 MG |
1528 | /* |
1529 | * Acquire the page lock to serialise THP migrations but avoid dropping | |
1530 | * page_table_lock if at all possible | |
1531 | */ | |
b8916634 MG |
1532 | page_locked = trylock_page(page); |
1533 | target_nid = mpol_misplaced(page, vma, haddr); | |
1534 | if (target_nid == -1) { | |
1535 | /* If the page was locked, there are no parallel migrations */ | |
a54a407f | 1536 | if (page_locked) |
b8916634 | 1537 | goto clear_pmdnuma; |
2b4847e7 | 1538 | } |
4daae3b4 | 1539 | |
de466bd6 | 1540 | /* Migration could have started since the pmd_trans_migrating check */ |
2b4847e7 | 1541 | if (!page_locked) { |
bae473a4 | 1542 | spin_unlock(fe->ptl); |
b8916634 | 1543 | wait_on_page_locked(page); |
a54a407f | 1544 | page_nid = -1; |
b8916634 MG |
1545 | goto out; |
1546 | } | |
1547 | ||
2b4847e7 MG |
1548 | /* |
1549 | * Page is misplaced. Page lock serialises migrations. Acquire anon_vma | |
1550 | * to serialises splits | |
1551 | */ | |
b8916634 | 1552 | get_page(page); |
bae473a4 | 1553 | spin_unlock(fe->ptl); |
b8916634 | 1554 | anon_vma = page_lock_anon_vma_read(page); |
4daae3b4 | 1555 | |
c69307d5 | 1556 | /* Confirm the PMD did not change while page_table_lock was released */ |
bae473a4 KS |
1557 | spin_lock(fe->ptl); |
1558 | if (unlikely(!pmd_same(pmd, *fe->pmd))) { | |
b32967ff MG |
1559 | unlock_page(page); |
1560 | put_page(page); | |
a54a407f | 1561 | page_nid = -1; |
4daae3b4 | 1562 | goto out_unlock; |
b32967ff | 1563 | } |
ff9042b1 | 1564 | |
c3a489ca MG |
1565 | /* Bail if we fail to protect against THP splits for any reason */ |
1566 | if (unlikely(!anon_vma)) { | |
1567 | put_page(page); | |
1568 | page_nid = -1; | |
1569 | goto clear_pmdnuma; | |
1570 | } | |
1571 | ||
a54a407f MG |
1572 | /* |
1573 | * Migrate the THP to the requested node, returns with page unlocked | |
8a0516ed | 1574 | * and access rights restored. |
a54a407f | 1575 | */ |
bae473a4 KS |
1576 | spin_unlock(fe->ptl); |
1577 | migrated = migrate_misplaced_transhuge_page(vma->vm_mm, vma, | |
1578 | fe->pmd, pmd, fe->address, page, target_nid); | |
6688cc05 PZ |
1579 | if (migrated) { |
1580 | flags |= TNF_MIGRATED; | |
8191acbd | 1581 | page_nid = target_nid; |
074c2381 MG |
1582 | } else |
1583 | flags |= TNF_MIGRATE_FAIL; | |
b32967ff | 1584 | |
8191acbd | 1585 | goto out; |
b32967ff | 1586 | clear_pmdnuma: |
a54a407f | 1587 | BUG_ON(!PageLocked(page)); |
b191f9b1 | 1588 | was_writable = pmd_write(pmd); |
4d942466 | 1589 | pmd = pmd_modify(pmd, vma->vm_page_prot); |
b7b04004 | 1590 | pmd = pmd_mkyoung(pmd); |
b191f9b1 MG |
1591 | if (was_writable) |
1592 | pmd = pmd_mkwrite(pmd); | |
bae473a4 KS |
1593 | set_pmd_at(vma->vm_mm, haddr, fe->pmd, pmd); |
1594 | update_mmu_cache_pmd(vma, fe->address, fe->pmd); | |
a54a407f | 1595 | unlock_page(page); |
d10e63f2 | 1596 | out_unlock: |
bae473a4 | 1597 | spin_unlock(fe->ptl); |
b8916634 MG |
1598 | |
1599 | out: | |
1600 | if (anon_vma) | |
1601 | page_unlock_anon_vma_read(anon_vma); | |
1602 | ||
8191acbd | 1603 | if (page_nid != -1) |
bae473a4 | 1604 | task_numa_fault(last_cpupid, page_nid, HPAGE_PMD_NR, fe->flags); |
8191acbd | 1605 | |
d10e63f2 MG |
1606 | return 0; |
1607 | } | |
1608 | ||
b8d3c4c3 MK |
1609 | int madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
1610 | pmd_t *pmd, unsigned long addr, unsigned long next) | |
1611 | ||
1612 | { | |
1613 | spinlock_t *ptl; | |
1614 | pmd_t orig_pmd; | |
1615 | struct page *page; | |
1616 | struct mm_struct *mm = tlb->mm; | |
1617 | int ret = 0; | |
1618 | ||
b6ec57f4 KS |
1619 | ptl = pmd_trans_huge_lock(pmd, vma); |
1620 | if (!ptl) | |
25eedabe | 1621 | goto out_unlocked; |
b8d3c4c3 MK |
1622 | |
1623 | orig_pmd = *pmd; | |
1624 | if (is_huge_zero_pmd(orig_pmd)) { | |
1625 | ret = 1; | |
1626 | goto out; | |
1627 | } | |
1628 | ||
1629 | page = pmd_page(orig_pmd); | |
1630 | /* | |
1631 | * If other processes are mapping this page, we couldn't discard | |
1632 | * the page unless they all do MADV_FREE so let's skip the page. | |
1633 | */ | |
1634 | if (page_mapcount(page) != 1) | |
1635 | goto out; | |
1636 | ||
1637 | if (!trylock_page(page)) | |
1638 | goto out; | |
1639 | ||
1640 | /* | |
1641 | * If user want to discard part-pages of THP, split it so MADV_FREE | |
1642 | * will deactivate only them. | |
1643 | */ | |
1644 | if (next - addr != HPAGE_PMD_SIZE) { | |
1645 | get_page(page); | |
1646 | spin_unlock(ptl); | |
9818b8cd | 1647 | split_huge_page(page); |
b8d3c4c3 MK |
1648 | put_page(page); |
1649 | unlock_page(page); | |
b8d3c4c3 MK |
1650 | goto out_unlocked; |
1651 | } | |
1652 | ||
1653 | if (PageDirty(page)) | |
1654 | ClearPageDirty(page); | |
1655 | unlock_page(page); | |
1656 | ||
1657 | if (PageActive(page)) | |
1658 | deactivate_page(page); | |
1659 | ||
1660 | if (pmd_young(orig_pmd) || pmd_dirty(orig_pmd)) { | |
1661 | orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd, | |
1662 | tlb->fullmm); | |
1663 | orig_pmd = pmd_mkold(orig_pmd); | |
1664 | orig_pmd = pmd_mkclean(orig_pmd); | |
1665 | ||
1666 | set_pmd_at(mm, addr, pmd, orig_pmd); | |
1667 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); | |
1668 | } | |
1669 | ret = 1; | |
1670 | out: | |
1671 | spin_unlock(ptl); | |
1672 | out_unlocked: | |
1673 | return ret; | |
1674 | } | |
1675 | ||
71e3aac0 | 1676 | int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, |
f21760b1 | 1677 | pmd_t *pmd, unsigned long addr) |
71e3aac0 | 1678 | { |
da146769 | 1679 | pmd_t orig_pmd; |
bf929152 | 1680 | spinlock_t *ptl; |
71e3aac0 | 1681 | |
b6ec57f4 KS |
1682 | ptl = __pmd_trans_huge_lock(pmd, vma); |
1683 | if (!ptl) | |
da146769 KS |
1684 | return 0; |
1685 | /* | |
1686 | * For architectures like ppc64 we look at deposited pgtable | |
1687 | * when calling pmdp_huge_get_and_clear. So do the | |
1688 | * pgtable_trans_huge_withdraw after finishing pmdp related | |
1689 | * operations. | |
1690 | */ | |
1691 | orig_pmd = pmdp_huge_get_and_clear_full(tlb->mm, addr, pmd, | |
1692 | tlb->fullmm); | |
1693 | tlb_remove_pmd_tlb_entry(tlb, pmd, addr); | |
1694 | if (vma_is_dax(vma)) { | |
1695 | spin_unlock(ptl); | |
1696 | if (is_huge_zero_pmd(orig_pmd)) | |
aa88b68c | 1697 | tlb_remove_page(tlb, pmd_page(orig_pmd)); |
da146769 KS |
1698 | } else if (is_huge_zero_pmd(orig_pmd)) { |
1699 | pte_free(tlb->mm, pgtable_trans_huge_withdraw(tlb->mm, pmd)); | |
1700 | atomic_long_dec(&tlb->mm->nr_ptes); | |
1701 | spin_unlock(ptl); | |
aa88b68c | 1702 | tlb_remove_page(tlb, pmd_page(orig_pmd)); |
da146769 KS |
1703 | } else { |
1704 | struct page *page = pmd_page(orig_pmd); | |
d281ee61 | 1705 | page_remove_rmap(page, true); |
da146769 | 1706 | VM_BUG_ON_PAGE(page_mapcount(page) < 0, page); |
da146769 | 1707 | VM_BUG_ON_PAGE(!PageHead(page), page); |
b5072380 KS |
1708 | if (PageAnon(page)) { |
1709 | pgtable_t pgtable; | |
1710 | pgtable = pgtable_trans_huge_withdraw(tlb->mm, pmd); | |
1711 | pte_free(tlb->mm, pgtable); | |
1712 | atomic_long_dec(&tlb->mm->nr_ptes); | |
1713 | add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); | |
1714 | } else { | |
1715 | add_mm_counter(tlb->mm, MM_FILEPAGES, -HPAGE_PMD_NR); | |
1716 | } | |
da146769 | 1717 | spin_unlock(ptl); |
e77b0852 | 1718 | tlb_remove_page_size(tlb, page, HPAGE_PMD_SIZE); |
025c5b24 | 1719 | } |
da146769 | 1720 | return 1; |
71e3aac0 AA |
1721 | } |
1722 | ||
bf8616d5 | 1723 | bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, |
37a1c49a AA |
1724 | unsigned long new_addr, unsigned long old_end, |
1725 | pmd_t *old_pmd, pmd_t *new_pmd) | |
1726 | { | |
bf929152 | 1727 | spinlock_t *old_ptl, *new_ptl; |
37a1c49a | 1728 | pmd_t pmd; |
37a1c49a AA |
1729 | struct mm_struct *mm = vma->vm_mm; |
1730 | ||
1731 | if ((old_addr & ~HPAGE_PMD_MASK) || | |
1732 | (new_addr & ~HPAGE_PMD_MASK) || | |
bf8616d5 | 1733 | old_end - old_addr < HPAGE_PMD_SIZE) |
4b471e88 | 1734 | return false; |
37a1c49a AA |
1735 | |
1736 | /* | |
1737 | * The destination pmd shouldn't be established, free_pgtables() | |
1738 | * should have release it. | |
1739 | */ | |
1740 | if (WARN_ON(!pmd_none(*new_pmd))) { | |
1741 | VM_BUG_ON(pmd_trans_huge(*new_pmd)); | |
4b471e88 | 1742 | return false; |
37a1c49a AA |
1743 | } |
1744 | ||
bf929152 KS |
1745 | /* |
1746 | * We don't have to worry about the ordering of src and dst | |
1747 | * ptlocks because exclusive mmap_sem prevents deadlock. | |
1748 | */ | |
b6ec57f4 KS |
1749 | old_ptl = __pmd_trans_huge_lock(old_pmd, vma); |
1750 | if (old_ptl) { | |
bf929152 KS |
1751 | new_ptl = pmd_lockptr(mm, new_pmd); |
1752 | if (new_ptl != old_ptl) | |
1753 | spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING); | |
8809aa2d | 1754 | pmd = pmdp_huge_get_and_clear(mm, old_addr, old_pmd); |
025c5b24 | 1755 | VM_BUG_ON(!pmd_none(*new_pmd)); |
3592806c | 1756 | |
69a8ec2d KS |
1757 | if (pmd_move_must_withdraw(new_ptl, old_ptl) && |
1758 | vma_is_anonymous(vma)) { | |
b3084f4d | 1759 | pgtable_t pgtable; |
3592806c KS |
1760 | pgtable = pgtable_trans_huge_withdraw(mm, old_pmd); |
1761 | pgtable_trans_huge_deposit(mm, new_pmd, pgtable); | |
3592806c | 1762 | } |
b3084f4d AK |
1763 | set_pmd_at(mm, new_addr, new_pmd, pmd_mksoft_dirty(pmd)); |
1764 | if (new_ptl != old_ptl) | |
1765 | spin_unlock(new_ptl); | |
bf929152 | 1766 | spin_unlock(old_ptl); |
4b471e88 | 1767 | return true; |
37a1c49a | 1768 | } |
4b471e88 | 1769 | return false; |
37a1c49a AA |
1770 | } |
1771 | ||
f123d74a MG |
1772 | /* |
1773 | * Returns | |
1774 | * - 0 if PMD could not be locked | |
1775 | * - 1 if PMD was locked but protections unchange and TLB flush unnecessary | |
1776 | * - HPAGE_PMD_NR is protections changed and TLB flush necessary | |
1777 | */ | |
cd7548ab | 1778 | int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, |
e944fd67 | 1779 | unsigned long addr, pgprot_t newprot, int prot_numa) |
cd7548ab JW |
1780 | { |
1781 | struct mm_struct *mm = vma->vm_mm; | |
bf929152 | 1782 | spinlock_t *ptl; |
cd7548ab JW |
1783 | int ret = 0; |
1784 | ||
b6ec57f4 KS |
1785 | ptl = __pmd_trans_huge_lock(pmd, vma); |
1786 | if (ptl) { | |
025c5b24 | 1787 | pmd_t entry; |
b191f9b1 | 1788 | bool preserve_write = prot_numa && pmd_write(*pmd); |
ba68bc01 | 1789 | ret = 1; |
e944fd67 MG |
1790 | |
1791 | /* | |
1792 | * Avoid trapping faults against the zero page. The read-only | |
1793 | * data is likely to be read-cached on the local CPU and | |
1794 | * local/remote hits to the zero page are not interesting. | |
1795 | */ | |
1796 | if (prot_numa && is_huge_zero_pmd(*pmd)) { | |
1797 | spin_unlock(ptl); | |
ba68bc01 | 1798 | return ret; |
e944fd67 MG |
1799 | } |
1800 | ||
10c1045f | 1801 | if (!prot_numa || !pmd_protnone(*pmd)) { |
8809aa2d | 1802 | entry = pmdp_huge_get_and_clear_notify(mm, addr, pmd); |
10c1045f | 1803 | entry = pmd_modify(entry, newprot); |
b191f9b1 MG |
1804 | if (preserve_write) |
1805 | entry = pmd_mkwrite(entry); | |
10c1045f MG |
1806 | ret = HPAGE_PMD_NR; |
1807 | set_pmd_at(mm, addr, pmd, entry); | |
b237aded KS |
1808 | BUG_ON(vma_is_anonymous(vma) && !preserve_write && |
1809 | pmd_write(entry)); | |
10c1045f | 1810 | } |
bf929152 | 1811 | spin_unlock(ptl); |
025c5b24 NH |
1812 | } |
1813 | ||
1814 | return ret; | |
1815 | } | |
1816 | ||
1817 | /* | |
4b471e88 | 1818 | * Returns true if a given pmd maps a thp, false otherwise. |
025c5b24 | 1819 | * |
4b471e88 KS |
1820 | * Note that if it returns true, this routine returns without unlocking page |
1821 | * table lock. So callers must unlock it. | |
025c5b24 | 1822 | */ |
b6ec57f4 | 1823 | spinlock_t *__pmd_trans_huge_lock(pmd_t *pmd, struct vm_area_struct *vma) |
025c5b24 | 1824 | { |
b6ec57f4 KS |
1825 | spinlock_t *ptl; |
1826 | ptl = pmd_lock(vma->vm_mm, pmd); | |
5c7fb56e | 1827 | if (likely(pmd_trans_huge(*pmd) || pmd_devmap(*pmd))) |
b6ec57f4 KS |
1828 | return ptl; |
1829 | spin_unlock(ptl); | |
1830 | return NULL; | |
cd7548ab JW |
1831 | } |
1832 | ||
9050d7eb | 1833 | #define VM_NO_THP (VM_SPECIAL | VM_HUGETLB | VM_SHARED | VM_MAYSHARE) |
78f11a25 | 1834 | |
60ab3244 AA |
1835 | int hugepage_madvise(struct vm_area_struct *vma, |
1836 | unsigned long *vm_flags, int advice) | |
0af4e98b | 1837 | { |
a664b2d8 AA |
1838 | switch (advice) { |
1839 | case MADV_HUGEPAGE: | |
1e1836e8 AT |
1840 | #ifdef CONFIG_S390 |
1841 | /* | |
1842 | * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390 | |
1843 | * can't handle this properly after s390_enable_sie, so we simply | |
1844 | * ignore the madvise to prevent qemu from causing a SIGSEGV. | |
1845 | */ | |
1846 | if (mm_has_pgste(vma->vm_mm)) | |
1847 | return 0; | |
1848 | #endif | |
a664b2d8 AA |
1849 | /* |
1850 | * Be somewhat over-protective like KSM for now! | |
1851 | */ | |
1a763615 | 1852 | if (*vm_flags & VM_NO_THP) |
a664b2d8 AA |
1853 | return -EINVAL; |
1854 | *vm_flags &= ~VM_NOHUGEPAGE; | |
1855 | *vm_flags |= VM_HUGEPAGE; | |
60ab3244 AA |
1856 | /* |
1857 | * If the vma become good for khugepaged to scan, | |
1858 | * register it here without waiting a page fault that | |
1859 | * may not happen any time soon. | |
1860 | */ | |
6d50e60c | 1861 | if (unlikely(khugepaged_enter_vma_merge(vma, *vm_flags))) |
60ab3244 | 1862 | return -ENOMEM; |
a664b2d8 AA |
1863 | break; |
1864 | case MADV_NOHUGEPAGE: | |
1865 | /* | |
1866 | * Be somewhat over-protective like KSM for now! | |
1867 | */ | |
1a763615 | 1868 | if (*vm_flags & VM_NO_THP) |
a664b2d8 AA |
1869 | return -EINVAL; |
1870 | *vm_flags &= ~VM_HUGEPAGE; | |
1871 | *vm_flags |= VM_NOHUGEPAGE; | |
60ab3244 AA |
1872 | /* |
1873 | * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning | |
1874 | * this vma even if we leave the mm registered in khugepaged if | |
1875 | * it got registered before VM_NOHUGEPAGE was set. | |
1876 | */ | |
a664b2d8 AA |
1877 | break; |
1878 | } | |
0af4e98b AA |
1879 | |
1880 | return 0; | |
1881 | } | |
1882 | ||
ba76149f AA |
1883 | static int __init khugepaged_slab_init(void) |
1884 | { | |
1885 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", | |
1886 | sizeof(struct mm_slot), | |
1887 | __alignof__(struct mm_slot), 0, NULL); | |
1888 | if (!mm_slot_cache) | |
1889 | return -ENOMEM; | |
1890 | ||
1891 | return 0; | |
1892 | } | |
1893 | ||
65ebb64f KS |
1894 | static void __init khugepaged_slab_exit(void) |
1895 | { | |
1896 | kmem_cache_destroy(mm_slot_cache); | |
1897 | } | |
1898 | ||
ba76149f AA |
1899 | static inline struct mm_slot *alloc_mm_slot(void) |
1900 | { | |
1901 | if (!mm_slot_cache) /* initialization failed */ | |
1902 | return NULL; | |
1903 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); | |
1904 | } | |
1905 | ||
1906 | static inline void free_mm_slot(struct mm_slot *mm_slot) | |
1907 | { | |
1908 | kmem_cache_free(mm_slot_cache, mm_slot); | |
1909 | } | |
1910 | ||
ba76149f AA |
1911 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) |
1912 | { | |
1913 | struct mm_slot *mm_slot; | |
ba76149f | 1914 | |
b67bfe0d | 1915 | hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm) |
ba76149f AA |
1916 | if (mm == mm_slot->mm) |
1917 | return mm_slot; | |
43b5fbbd | 1918 | |
ba76149f AA |
1919 | return NULL; |
1920 | } | |
1921 | ||
1922 | static void insert_to_mm_slots_hash(struct mm_struct *mm, | |
1923 | struct mm_slot *mm_slot) | |
1924 | { | |
ba76149f | 1925 | mm_slot->mm = mm; |
43b5fbbd | 1926 | hash_add(mm_slots_hash, &mm_slot->hash, (long)mm); |
ba76149f AA |
1927 | } |
1928 | ||
1929 | static inline int khugepaged_test_exit(struct mm_struct *mm) | |
1930 | { | |
1931 | return atomic_read(&mm->mm_users) == 0; | |
1932 | } | |
1933 | ||
1934 | int __khugepaged_enter(struct mm_struct *mm) | |
1935 | { | |
1936 | struct mm_slot *mm_slot; | |
1937 | int wakeup; | |
1938 | ||
1939 | mm_slot = alloc_mm_slot(); | |
1940 | if (!mm_slot) | |
1941 | return -ENOMEM; | |
1942 | ||
1943 | /* __khugepaged_exit() must not run from under us */ | |
96dad67f | 1944 | VM_BUG_ON_MM(khugepaged_test_exit(mm), mm); |
ba76149f AA |
1945 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { |
1946 | free_mm_slot(mm_slot); | |
1947 | return 0; | |
1948 | } | |
1949 | ||
1950 | spin_lock(&khugepaged_mm_lock); | |
1951 | insert_to_mm_slots_hash(mm, mm_slot); | |
1952 | /* | |
1953 | * Insert just behind the scanning cursor, to let the area settle | |
1954 | * down a little. | |
1955 | */ | |
1956 | wakeup = list_empty(&khugepaged_scan.mm_head); | |
1957 | list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head); | |
1958 | spin_unlock(&khugepaged_mm_lock); | |
1959 | ||
1960 | atomic_inc(&mm->mm_count); | |
1961 | if (wakeup) | |
1962 | wake_up_interruptible(&khugepaged_wait); | |
1963 | ||
1964 | return 0; | |
1965 | } | |
1966 | ||
6d50e60c DR |
1967 | int khugepaged_enter_vma_merge(struct vm_area_struct *vma, |
1968 | unsigned long vm_flags) | |
ba76149f AA |
1969 | { |
1970 | unsigned long hstart, hend; | |
1971 | if (!vma->anon_vma) | |
1972 | /* | |
1973 | * Not yet faulted in so we will register later in the | |
1974 | * page fault if needed. | |
1975 | */ | |
1976 | return 0; | |
3486b85a | 1977 | if (vma->vm_ops || (vm_flags & VM_NO_THP)) |
ba76149f AA |
1978 | /* khugepaged not yet working on file or special mappings */ |
1979 | return 0; | |
ba76149f AA |
1980 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
1981 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
1982 | if (hstart < hend) | |
6d50e60c | 1983 | return khugepaged_enter(vma, vm_flags); |
ba76149f AA |
1984 | return 0; |
1985 | } | |
1986 | ||
1987 | void __khugepaged_exit(struct mm_struct *mm) | |
1988 | { | |
1989 | struct mm_slot *mm_slot; | |
1990 | int free = 0; | |
1991 | ||
1992 | spin_lock(&khugepaged_mm_lock); | |
1993 | mm_slot = get_mm_slot(mm); | |
1994 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { | |
43b5fbbd | 1995 | hash_del(&mm_slot->hash); |
ba76149f AA |
1996 | list_del(&mm_slot->mm_node); |
1997 | free = 1; | |
1998 | } | |
d788e80a | 1999 | spin_unlock(&khugepaged_mm_lock); |
ba76149f AA |
2000 | |
2001 | if (free) { | |
ba76149f AA |
2002 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); |
2003 | free_mm_slot(mm_slot); | |
2004 | mmdrop(mm); | |
2005 | } else if (mm_slot) { | |
ba76149f AA |
2006 | /* |
2007 | * This is required to serialize against | |
2008 | * khugepaged_test_exit() (which is guaranteed to run | |
2009 | * under mmap sem read mode). Stop here (after we | |
2010 | * return all pagetables will be destroyed) until | |
2011 | * khugepaged has finished working on the pagetables | |
2012 | * under the mmap_sem. | |
2013 | */ | |
2014 | down_write(&mm->mmap_sem); | |
2015 | up_write(&mm->mmap_sem); | |
d788e80a | 2016 | } |
ba76149f AA |
2017 | } |
2018 | ||
2019 | static void release_pte_page(struct page *page) | |
2020 | { | |
2021 | /* 0 stands for page_is_file_cache(page) == false */ | |
2022 | dec_zone_page_state(page, NR_ISOLATED_ANON + 0); | |
2023 | unlock_page(page); | |
2024 | putback_lru_page(page); | |
2025 | } | |
2026 | ||
2027 | static void release_pte_pages(pte_t *pte, pte_t *_pte) | |
2028 | { | |
2029 | while (--_pte >= pte) { | |
2030 | pte_t pteval = *_pte; | |
ca0984ca | 2031 | if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval))) |
ba76149f AA |
2032 | release_pte_page(pte_page(pteval)); |
2033 | } | |
2034 | } | |
2035 | ||
ba76149f AA |
2036 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, |
2037 | unsigned long address, | |
2038 | pte_t *pte) | |
2039 | { | |
7d2eba05 | 2040 | struct page *page = NULL; |
ba76149f | 2041 | pte_t *_pte; |
7d2eba05 | 2042 | int none_or_zero = 0, result = 0; |
10359213 | 2043 | bool referenced = false, writable = false; |
7d2eba05 | 2044 | |
ba76149f AA |
2045 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; |
2046 | _pte++, address += PAGE_SIZE) { | |
2047 | pte_t pteval = *_pte; | |
47aee4d8 MK |
2048 | if (pte_none(pteval) || (pte_present(pteval) && |
2049 | is_zero_pfn(pte_pfn(pteval)))) { | |
c1294d05 | 2050 | if (!userfaultfd_armed(vma) && |
7d2eba05 | 2051 | ++none_or_zero <= khugepaged_max_ptes_none) { |
ba76149f | 2052 | continue; |
7d2eba05 EA |
2053 | } else { |
2054 | result = SCAN_EXCEED_NONE_PTE; | |
ba76149f | 2055 | goto out; |
7d2eba05 | 2056 | } |
ba76149f | 2057 | } |
7d2eba05 EA |
2058 | if (!pte_present(pteval)) { |
2059 | result = SCAN_PTE_NON_PRESENT; | |
ba76149f | 2060 | goto out; |
7d2eba05 | 2061 | } |
ba76149f | 2062 | page = vm_normal_page(vma, address, pteval); |
7d2eba05 EA |
2063 | if (unlikely(!page)) { |
2064 | result = SCAN_PAGE_NULL; | |
ba76149f | 2065 | goto out; |
7d2eba05 | 2066 | } |
344aa35c | 2067 | |
309381fe SL |
2068 | VM_BUG_ON_PAGE(PageCompound(page), page); |
2069 | VM_BUG_ON_PAGE(!PageAnon(page), page); | |
2070 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); | |
ba76149f | 2071 | |
ba76149f AA |
2072 | /* |
2073 | * We can do it before isolate_lru_page because the | |
2074 | * page can't be freed from under us. NOTE: PG_lock | |
2075 | * is needed to serialize against split_huge_page | |
2076 | * when invoked from the VM. | |
2077 | */ | |
7d2eba05 EA |
2078 | if (!trylock_page(page)) { |
2079 | result = SCAN_PAGE_LOCK; | |
ba76149f | 2080 | goto out; |
7d2eba05 | 2081 | } |
10359213 EA |
2082 | |
2083 | /* | |
2084 | * cannot use mapcount: can't collapse if there's a gup pin. | |
2085 | * The page must only be referenced by the scanned process | |
2086 | * and page swap cache. | |
2087 | */ | |
2088 | if (page_count(page) != 1 + !!PageSwapCache(page)) { | |
2089 | unlock_page(page); | |
7d2eba05 | 2090 | result = SCAN_PAGE_COUNT; |
10359213 EA |
2091 | goto out; |
2092 | } | |
2093 | if (pte_write(pteval)) { | |
2094 | writable = true; | |
2095 | } else { | |
6d0a07ed AA |
2096 | if (PageSwapCache(page) && |
2097 | !reuse_swap_page(page, NULL)) { | |
10359213 | 2098 | unlock_page(page); |
7d2eba05 | 2099 | result = SCAN_SWAP_CACHE_PAGE; |
10359213 EA |
2100 | goto out; |
2101 | } | |
2102 | /* | |
2103 | * Page is not in the swap cache. It can be collapsed | |
2104 | * into a THP. | |
2105 | */ | |
2106 | } | |
2107 | ||
ba76149f AA |
2108 | /* |
2109 | * Isolate the page to avoid collapsing an hugepage | |
2110 | * currently in use by the VM. | |
2111 | */ | |
2112 | if (isolate_lru_page(page)) { | |
2113 | unlock_page(page); | |
7d2eba05 | 2114 | result = SCAN_DEL_PAGE_LRU; |
ba76149f AA |
2115 | goto out; |
2116 | } | |
2117 | /* 0 stands for page_is_file_cache(page) == false */ | |
2118 | inc_zone_page_state(page, NR_ISOLATED_ANON + 0); | |
309381fe SL |
2119 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
2120 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
ba76149f AA |
2121 | |
2122 | /* If there is no mapped pte young don't collapse the page */ | |
33c3fc71 VD |
2123 | if (pte_young(pteval) || |
2124 | page_is_young(page) || PageReferenced(page) || | |
8ee53820 | 2125 | mmu_notifier_test_young(vma->vm_mm, address)) |
10359213 | 2126 | referenced = true; |
ba76149f | 2127 | } |
7d2eba05 EA |
2128 | if (likely(writable)) { |
2129 | if (likely(referenced)) { | |
2130 | result = SCAN_SUCCEED; | |
16fd0fe4 | 2131 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, |
7d2eba05 EA |
2132 | referenced, writable, result); |
2133 | return 1; | |
2134 | } | |
2135 | } else { | |
2136 | result = SCAN_PAGE_RO; | |
2137 | } | |
2138 | ||
ba76149f | 2139 | out: |
344aa35c | 2140 | release_pte_pages(pte, _pte); |
16fd0fe4 | 2141 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, |
7d2eba05 | 2142 | referenced, writable, result); |
344aa35c | 2143 | return 0; |
ba76149f AA |
2144 | } |
2145 | ||
2146 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, | |
2147 | struct vm_area_struct *vma, | |
2148 | unsigned long address, | |
2149 | spinlock_t *ptl) | |
2150 | { | |
2151 | pte_t *_pte; | |
2152 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) { | |
2153 | pte_t pteval = *_pte; | |
2154 | struct page *src_page; | |
2155 | ||
ca0984ca | 2156 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
ba76149f AA |
2157 | clear_user_highpage(page, address); |
2158 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); | |
ca0984ca EA |
2159 | if (is_zero_pfn(pte_pfn(pteval))) { |
2160 | /* | |
2161 | * ptl mostly unnecessary. | |
2162 | */ | |
2163 | spin_lock(ptl); | |
2164 | /* | |
2165 | * paravirt calls inside pte_clear here are | |
2166 | * superfluous. | |
2167 | */ | |
2168 | pte_clear(vma->vm_mm, address, _pte); | |
2169 | spin_unlock(ptl); | |
2170 | } | |
ba76149f AA |
2171 | } else { |
2172 | src_page = pte_page(pteval); | |
2173 | copy_user_highpage(page, src_page, address, vma); | |
309381fe | 2174 | VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page); |
ba76149f AA |
2175 | release_pte_page(src_page); |
2176 | /* | |
2177 | * ptl mostly unnecessary, but preempt has to | |
2178 | * be disabled to update the per-cpu stats | |
2179 | * inside page_remove_rmap(). | |
2180 | */ | |
2181 | spin_lock(ptl); | |
2182 | /* | |
2183 | * paravirt calls inside pte_clear here are | |
2184 | * superfluous. | |
2185 | */ | |
2186 | pte_clear(vma->vm_mm, address, _pte); | |
d281ee61 | 2187 | page_remove_rmap(src_page, false); |
ba76149f AA |
2188 | spin_unlock(ptl); |
2189 | free_page_and_swap_cache(src_page); | |
2190 | } | |
2191 | ||
2192 | address += PAGE_SIZE; | |
2193 | page++; | |
2194 | } | |
2195 | } | |
2196 | ||
26234f36 | 2197 | static void khugepaged_alloc_sleep(void) |
ba76149f | 2198 | { |
bde43c6c PM |
2199 | DEFINE_WAIT(wait); |
2200 | ||
2201 | add_wait_queue(&khugepaged_wait, &wait); | |
2202 | freezable_schedule_timeout_interruptible( | |
2203 | msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); | |
2204 | remove_wait_queue(&khugepaged_wait, &wait); | |
26234f36 | 2205 | } |
ba76149f | 2206 | |
9f1b868a BL |
2207 | static int khugepaged_node_load[MAX_NUMNODES]; |
2208 | ||
14a4e214 DR |
2209 | static bool khugepaged_scan_abort(int nid) |
2210 | { | |
2211 | int i; | |
2212 | ||
2213 | /* | |
2214 | * If zone_reclaim_mode is disabled, then no extra effort is made to | |
2215 | * allocate memory locally. | |
2216 | */ | |
2217 | if (!zone_reclaim_mode) | |
2218 | return false; | |
2219 | ||
2220 | /* If there is a count for this node already, it must be acceptable */ | |
2221 | if (khugepaged_node_load[nid]) | |
2222 | return false; | |
2223 | ||
2224 | for (i = 0; i < MAX_NUMNODES; i++) { | |
2225 | if (!khugepaged_node_load[i]) | |
2226 | continue; | |
2227 | if (node_distance(nid, i) > RECLAIM_DISTANCE) | |
2228 | return true; | |
2229 | } | |
2230 | return false; | |
2231 | } | |
2232 | ||
26234f36 | 2233 | #ifdef CONFIG_NUMA |
9f1b868a BL |
2234 | static int khugepaged_find_target_node(void) |
2235 | { | |
2236 | static int last_khugepaged_target_node = NUMA_NO_NODE; | |
2237 | int nid, target_node = 0, max_value = 0; | |
2238 | ||
2239 | /* find first node with max normal pages hit */ | |
2240 | for (nid = 0; nid < MAX_NUMNODES; nid++) | |
2241 | if (khugepaged_node_load[nid] > max_value) { | |
2242 | max_value = khugepaged_node_load[nid]; | |
2243 | target_node = nid; | |
2244 | } | |
2245 | ||
2246 | /* do some balance if several nodes have the same hit record */ | |
2247 | if (target_node <= last_khugepaged_target_node) | |
2248 | for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES; | |
2249 | nid++) | |
2250 | if (max_value == khugepaged_node_load[nid]) { | |
2251 | target_node = nid; | |
2252 | break; | |
2253 | } | |
2254 | ||
2255 | last_khugepaged_target_node = target_node; | |
2256 | return target_node; | |
2257 | } | |
2258 | ||
26234f36 XG |
2259 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) |
2260 | { | |
2261 | if (IS_ERR(*hpage)) { | |
2262 | if (!*wait) | |
2263 | return false; | |
2264 | ||
2265 | *wait = false; | |
e3b4126c | 2266 | *hpage = NULL; |
26234f36 XG |
2267 | khugepaged_alloc_sleep(); |
2268 | } else if (*hpage) { | |
2269 | put_page(*hpage); | |
2270 | *hpage = NULL; | |
2271 | } | |
2272 | ||
2273 | return true; | |
2274 | } | |
2275 | ||
3b363692 MH |
2276 | static struct page * |
2277 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm, | |
d6669d68 | 2278 | unsigned long address, int node) |
26234f36 | 2279 | { |
309381fe | 2280 | VM_BUG_ON_PAGE(*hpage, *hpage); |
8b164568 | 2281 | |
ce83d217 | 2282 | /* |
8b164568 VB |
2283 | * Before allocating the hugepage, release the mmap_sem read lock. |
2284 | * The allocation can take potentially a long time if it involves | |
2285 | * sync compaction, and we do not need to hold the mmap_sem during | |
2286 | * that. We will recheck the vma after taking it again in write mode. | |
ce83d217 | 2287 | */ |
8b164568 VB |
2288 | up_read(&mm->mmap_sem); |
2289 | ||
96db800f | 2290 | *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER); |
26234f36 | 2291 | if (unlikely(!*hpage)) { |
81ab4201 | 2292 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); |
ce83d217 | 2293 | *hpage = ERR_PTR(-ENOMEM); |
26234f36 | 2294 | return NULL; |
ce83d217 | 2295 | } |
26234f36 | 2296 | |
9a982250 | 2297 | prep_transhuge_page(*hpage); |
65b3c07b | 2298 | count_vm_event(THP_COLLAPSE_ALLOC); |
26234f36 XG |
2299 | return *hpage; |
2300 | } | |
2301 | #else | |
9f1b868a BL |
2302 | static int khugepaged_find_target_node(void) |
2303 | { | |
2304 | return 0; | |
2305 | } | |
2306 | ||
444eb2a4 | 2307 | static inline struct page *alloc_khugepaged_hugepage(void) |
10dc4155 | 2308 | { |
9a982250 KS |
2309 | struct page *page; |
2310 | ||
444eb2a4 MG |
2311 | page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(), |
2312 | HPAGE_PMD_ORDER); | |
9a982250 KS |
2313 | if (page) |
2314 | prep_transhuge_page(page); | |
2315 | return page; | |
10dc4155 BL |
2316 | } |
2317 | ||
26234f36 XG |
2318 | static struct page *khugepaged_alloc_hugepage(bool *wait) |
2319 | { | |
2320 | struct page *hpage; | |
2321 | ||
2322 | do { | |
444eb2a4 | 2323 | hpage = alloc_khugepaged_hugepage(); |
26234f36 XG |
2324 | if (!hpage) { |
2325 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); | |
2326 | if (!*wait) | |
2327 | return NULL; | |
2328 | ||
2329 | *wait = false; | |
2330 | khugepaged_alloc_sleep(); | |
2331 | } else | |
2332 | count_vm_event(THP_COLLAPSE_ALLOC); | |
2333 | } while (unlikely(!hpage) && likely(khugepaged_enabled())); | |
2334 | ||
2335 | return hpage; | |
2336 | } | |
2337 | ||
2338 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) | |
2339 | { | |
2340 | if (!*hpage) | |
2341 | *hpage = khugepaged_alloc_hugepage(wait); | |
2342 | ||
2343 | if (unlikely(!*hpage)) | |
2344 | return false; | |
2345 | ||
2346 | return true; | |
2347 | } | |
2348 | ||
3b363692 MH |
2349 | static struct page * |
2350 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, struct mm_struct *mm, | |
d6669d68 | 2351 | unsigned long address, int node) |
26234f36 XG |
2352 | { |
2353 | up_read(&mm->mmap_sem); | |
2354 | VM_BUG_ON(!*hpage); | |
3b363692 | 2355 | |
26234f36 XG |
2356 | return *hpage; |
2357 | } | |
692e0b35 AA |
2358 | #endif |
2359 | ||
fa475e51 BL |
2360 | static bool hugepage_vma_check(struct vm_area_struct *vma) |
2361 | { | |
2362 | if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || | |
2363 | (vma->vm_flags & VM_NOHUGEPAGE)) | |
2364 | return false; | |
fa475e51 BL |
2365 | if (!vma->anon_vma || vma->vm_ops) |
2366 | return false; | |
2367 | if (is_vma_temporary_stack(vma)) | |
2368 | return false; | |
3486b85a | 2369 | return !(vma->vm_flags & VM_NO_THP); |
fa475e51 BL |
2370 | } |
2371 | ||
72695862 EA |
2372 | /* |
2373 | * If mmap_sem temporarily dropped, revalidate vma | |
2374 | * before taking mmap_sem. | |
2375 | * Return 0 if succeeds, otherwise return none-zero | |
2376 | * value (scan code). | |
2377 | */ | |
2378 | ||
2379 | static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address) | |
2380 | { | |
2381 | struct vm_area_struct *vma; | |
2382 | unsigned long hstart, hend; | |
2383 | ||
2384 | if (unlikely(khugepaged_test_exit(mm))) | |
2385 | return SCAN_ANY_PROCESS; | |
2386 | ||
2387 | vma = find_vma(mm, address); | |
2388 | if (!vma) | |
2389 | return SCAN_VMA_NULL; | |
2390 | ||
2391 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; | |
2392 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
2393 | if (address < hstart || address + HPAGE_PMD_SIZE > hend) | |
2394 | return SCAN_ADDRESS_RANGE; | |
2395 | if (!hugepage_vma_check(vma)) | |
2396 | return SCAN_VMA_CHECK; | |
2397 | return 0; | |
2398 | } | |
2399 | ||
8a966ed7 EA |
2400 | /* |
2401 | * Bring missing pages in from swap, to complete THP collapse. | |
2402 | * Only done if khugepaged_scan_pmd believes it is worthwhile. | |
2403 | * | |
2404 | * Called and returns without pte mapped or spinlocks held, | |
2405 | * but with mmap_sem held to protect against vma changes. | |
2406 | */ | |
2407 | ||
72695862 | 2408 | static bool __collapse_huge_page_swapin(struct mm_struct *mm, |
8a966ed7 EA |
2409 | struct vm_area_struct *vma, |
2410 | unsigned long address, pmd_t *pmd) | |
2411 | { | |
bae473a4 | 2412 | pte_t pteval; |
8a966ed7 | 2413 | int swapped_in = 0, ret = 0; |
bae473a4 KS |
2414 | struct fault_env fe = { |
2415 | .vma = vma, | |
2416 | .address = address, | |
2417 | .flags = FAULT_FLAG_ALLOW_RETRY, | |
2418 | .pmd = pmd, | |
2419 | }; | |
2420 | ||
2421 | fe.pte = pte_offset_map(pmd, address); | |
2422 | for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE; | |
2423 | fe.pte++, fe.address += PAGE_SIZE) { | |
2424 | pteval = *fe.pte; | |
8a966ed7 EA |
2425 | if (!is_swap_pte(pteval)) |
2426 | continue; | |
2427 | swapped_in++; | |
bae473a4 | 2428 | ret = do_swap_page(&fe, pteval); |
72695862 EA |
2429 | /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */ |
2430 | if (ret & VM_FAULT_RETRY) { | |
2431 | down_read(&mm->mmap_sem); | |
2432 | /* vma is no longer available, don't continue to swapin */ | |
2433 | if (hugepage_vma_revalidate(mm, address)) | |
2434 | return false; | |
1f52e67e KS |
2435 | /* check if the pmd is still valid */ |
2436 | if (mm_find_pmd(mm, address) != pmd) | |
2437 | return false; | |
72695862 | 2438 | } |
8a966ed7 EA |
2439 | if (ret & VM_FAULT_ERROR) { |
2440 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, 0); | |
72695862 | 2441 | return false; |
8a966ed7 EA |
2442 | } |
2443 | /* pte is unmapped now, we need to map it */ | |
bae473a4 | 2444 | fe.pte = pte_offset_map(pmd, fe.address); |
8a966ed7 | 2445 | } |
bae473a4 KS |
2446 | fe.pte--; |
2447 | pte_unmap(fe.pte); | |
8a966ed7 | 2448 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, 1); |
72695862 | 2449 | return true; |
8a966ed7 EA |
2450 | } |
2451 | ||
26234f36 XG |
2452 | static void collapse_huge_page(struct mm_struct *mm, |
2453 | unsigned long address, | |
2454 | struct page **hpage, | |
2455 | struct vm_area_struct *vma, | |
2456 | int node) | |
2457 | { | |
26234f36 XG |
2458 | pmd_t *pmd, _pmd; |
2459 | pte_t *pte; | |
2460 | pgtable_t pgtable; | |
2461 | struct page *new_page; | |
c4088ebd | 2462 | spinlock_t *pmd_ptl, *pte_ptl; |
629d9d1c | 2463 | int isolated = 0, result = 0; |
00501b53 | 2464 | struct mem_cgroup *memcg; |
2ec74c3e SG |
2465 | unsigned long mmun_start; /* For mmu_notifiers */ |
2466 | unsigned long mmun_end; /* For mmu_notifiers */ | |
3b363692 | 2467 | gfp_t gfp; |
26234f36 XG |
2468 | |
2469 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); | |
2470 | ||
3b363692 | 2471 | /* Only allocate from the target node */ |
444eb2a4 | 2472 | gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE; |
3b363692 | 2473 | |
26234f36 | 2474 | /* release the mmap_sem read lock. */ |
d6669d68 | 2475 | new_page = khugepaged_alloc_page(hpage, gfp, mm, address, node); |
7d2eba05 EA |
2476 | if (!new_page) { |
2477 | result = SCAN_ALLOC_HUGE_PAGE_FAIL; | |
2478 | goto out_nolock; | |
2479 | } | |
26234f36 | 2480 | |
f627c2f5 | 2481 | if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) { |
7d2eba05 EA |
2482 | result = SCAN_CGROUP_CHARGE_FAIL; |
2483 | goto out_nolock; | |
2484 | } | |
ba76149f | 2485 | |
72695862 EA |
2486 | down_read(&mm->mmap_sem); |
2487 | result = hugepage_vma_revalidate(mm, address); | |
8024ee2a EA |
2488 | if (result) { |
2489 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
2490 | up_read(&mm->mmap_sem); | |
2491 | goto out_nolock; | |
2492 | } | |
ba76149f | 2493 | |
6219049a | 2494 | pmd = mm_find_pmd(mm, address); |
7d2eba05 EA |
2495 | if (!pmd) { |
2496 | result = SCAN_PMD_NULL; | |
8024ee2a EA |
2497 | mem_cgroup_cancel_charge(new_page, memcg, true); |
2498 | up_read(&mm->mmap_sem); | |
2499 | goto out_nolock; | |
7d2eba05 | 2500 | } |
ba76149f | 2501 | |
72695862 EA |
2502 | /* |
2503 | * __collapse_huge_page_swapin always returns with mmap_sem locked. | |
2504 | * If it fails, release mmap_sem and jump directly out. | |
2505 | * Continuing to collapse causes inconsistency. | |
2506 | */ | |
2507 | if (!__collapse_huge_page_swapin(mm, vma, address, pmd)) { | |
8024ee2a | 2508 | mem_cgroup_cancel_charge(new_page, memcg, true); |
72695862 | 2509 | up_read(&mm->mmap_sem); |
8024ee2a | 2510 | goto out_nolock; |
72695862 EA |
2511 | } |
2512 | ||
2513 | up_read(&mm->mmap_sem); | |
2514 | /* | |
2515 | * Prevent all access to pagetables with the exception of | |
2516 | * gup_fast later handled by the ptep_clear_flush and the VM | |
2517 | * handled by the anon_vma lock + PG_lock. | |
2518 | */ | |
2519 | down_write(&mm->mmap_sem); | |
2520 | result = hugepage_vma_revalidate(mm, address); | |
2521 | if (result) | |
2522 | goto out; | |
1f52e67e KS |
2523 | /* check if the pmd is still valid */ |
2524 | if (mm_find_pmd(mm, address) != pmd) | |
2525 | goto out; | |
8a966ed7 | 2526 | |
4fc3f1d6 | 2527 | anon_vma_lock_write(vma->anon_vma); |
ba76149f AA |
2528 | |
2529 | pte = pte_offset_map(pmd, address); | |
c4088ebd | 2530 | pte_ptl = pte_lockptr(mm, pmd); |
ba76149f | 2531 | |
2ec74c3e SG |
2532 | mmun_start = address; |
2533 | mmun_end = address + HPAGE_PMD_SIZE; | |
2534 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
c4088ebd | 2535 | pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ |
ba76149f AA |
2536 | /* |
2537 | * After this gup_fast can't run anymore. This also removes | |
2538 | * any huge TLB entry from the CPU so we won't allow | |
2539 | * huge and small TLB entries for the same virtual address | |
2540 | * to avoid the risk of CPU bugs in that area. | |
2541 | */ | |
15a25b2e | 2542 | _pmd = pmdp_collapse_flush(vma, address, pmd); |
c4088ebd | 2543 | spin_unlock(pmd_ptl); |
2ec74c3e | 2544 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
ba76149f | 2545 | |
c4088ebd | 2546 | spin_lock(pte_ptl); |
ba76149f | 2547 | isolated = __collapse_huge_page_isolate(vma, address, pte); |
c4088ebd | 2548 | spin_unlock(pte_ptl); |
ba76149f AA |
2549 | |
2550 | if (unlikely(!isolated)) { | |
453c7192 | 2551 | pte_unmap(pte); |
c4088ebd | 2552 | spin_lock(pmd_ptl); |
ba76149f | 2553 | BUG_ON(!pmd_none(*pmd)); |
7c342512 AK |
2554 | /* |
2555 | * We can only use set_pmd_at when establishing | |
2556 | * hugepmds and never for establishing regular pmds that | |
2557 | * points to regular pagetables. Use pmd_populate for that | |
2558 | */ | |
2559 | pmd_populate(mm, pmd, pmd_pgtable(_pmd)); | |
c4088ebd | 2560 | spin_unlock(pmd_ptl); |
08b52706 | 2561 | anon_vma_unlock_write(vma->anon_vma); |
7d2eba05 | 2562 | result = SCAN_FAIL; |
ce83d217 | 2563 | goto out; |
ba76149f AA |
2564 | } |
2565 | ||
2566 | /* | |
2567 | * All pages are isolated and locked so anon_vma rmap | |
2568 | * can't run anymore. | |
2569 | */ | |
08b52706 | 2570 | anon_vma_unlock_write(vma->anon_vma); |
ba76149f | 2571 | |
c4088ebd | 2572 | __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl); |
453c7192 | 2573 | pte_unmap(pte); |
ba76149f AA |
2574 | __SetPageUptodate(new_page); |
2575 | pgtable = pmd_pgtable(_pmd); | |
ba76149f | 2576 | |
3122359a KS |
2577 | _pmd = mk_huge_pmd(new_page, vma->vm_page_prot); |
2578 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); | |
ba76149f AA |
2579 | |
2580 | /* | |
2581 | * spin_lock() below is not the equivalent of smp_wmb(), so | |
2582 | * this is needed to avoid the copy_huge_page writes to become | |
2583 | * visible after the set_pmd_at() write. | |
2584 | */ | |
2585 | smp_wmb(); | |
2586 | ||
c4088ebd | 2587 | spin_lock(pmd_ptl); |
ba76149f | 2588 | BUG_ON(!pmd_none(*pmd)); |
d281ee61 | 2589 | page_add_new_anon_rmap(new_page, vma, address, true); |
f627c2f5 | 2590 | mem_cgroup_commit_charge(new_page, memcg, false, true); |
00501b53 | 2591 | lru_cache_add_active_or_unevictable(new_page, vma); |
fce144b4 | 2592 | pgtable_trans_huge_deposit(mm, pmd, pgtable); |
ba76149f | 2593 | set_pmd_at(mm, address, pmd, _pmd); |
b113da65 | 2594 | update_mmu_cache_pmd(vma, address, pmd); |
c4088ebd | 2595 | spin_unlock(pmd_ptl); |
ba76149f AA |
2596 | |
2597 | *hpage = NULL; | |
420256ef | 2598 | |
ba76149f | 2599 | khugepaged_pages_collapsed++; |
7d2eba05 | 2600 | result = SCAN_SUCCEED; |
ce83d217 | 2601 | out_up_write: |
ba76149f | 2602 | up_write(&mm->mmap_sem); |
7d2eba05 EA |
2603 | out_nolock: |
2604 | trace_mm_collapse_huge_page(mm, isolated, result); | |
2605 | return; | |
ce83d217 | 2606 | out: |
f627c2f5 | 2607 | mem_cgroup_cancel_charge(new_page, memcg, true); |
ce83d217 | 2608 | goto out_up_write; |
ba76149f AA |
2609 | } |
2610 | ||
2611 | static int khugepaged_scan_pmd(struct mm_struct *mm, | |
2612 | struct vm_area_struct *vma, | |
2613 | unsigned long address, | |
2614 | struct page **hpage) | |
2615 | { | |
ba76149f AA |
2616 | pmd_t *pmd; |
2617 | pte_t *pte, *_pte; | |
7d2eba05 EA |
2618 | int ret = 0, none_or_zero = 0, result = 0; |
2619 | struct page *page = NULL; | |
ba76149f AA |
2620 | unsigned long _address; |
2621 | spinlock_t *ptl; | |
70652f6e | 2622 | int node = NUMA_NO_NODE, unmapped = 0; |
10359213 | 2623 | bool writable = false, referenced = false; |
ba76149f AA |
2624 | |
2625 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); | |
2626 | ||
6219049a | 2627 | pmd = mm_find_pmd(mm, address); |
7d2eba05 EA |
2628 | if (!pmd) { |
2629 | result = SCAN_PMD_NULL; | |
ba76149f | 2630 | goto out; |
7d2eba05 | 2631 | } |
ba76149f | 2632 | |
9f1b868a | 2633 | memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load)); |
ba76149f AA |
2634 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); |
2635 | for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR; | |
2636 | _pte++, _address += PAGE_SIZE) { | |
2637 | pte_t pteval = *_pte; | |
70652f6e EA |
2638 | if (is_swap_pte(pteval)) { |
2639 | if (++unmapped <= khugepaged_max_ptes_swap) { | |
2640 | continue; | |
2641 | } else { | |
2642 | result = SCAN_EXCEED_SWAP_PTE; | |
2643 | goto out_unmap; | |
2644 | } | |
2645 | } | |
ca0984ca | 2646 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { |
c1294d05 | 2647 | if (!userfaultfd_armed(vma) && |
7d2eba05 | 2648 | ++none_or_zero <= khugepaged_max_ptes_none) { |
ba76149f | 2649 | continue; |
7d2eba05 EA |
2650 | } else { |
2651 | result = SCAN_EXCEED_NONE_PTE; | |
ba76149f | 2652 | goto out_unmap; |
7d2eba05 | 2653 | } |
ba76149f | 2654 | } |
7d2eba05 EA |
2655 | if (!pte_present(pteval)) { |
2656 | result = SCAN_PTE_NON_PRESENT; | |
ba76149f | 2657 | goto out_unmap; |
7d2eba05 | 2658 | } |
10359213 EA |
2659 | if (pte_write(pteval)) |
2660 | writable = true; | |
2661 | ||
ba76149f | 2662 | page = vm_normal_page(vma, _address, pteval); |
7d2eba05 EA |
2663 | if (unlikely(!page)) { |
2664 | result = SCAN_PAGE_NULL; | |
ba76149f | 2665 | goto out_unmap; |
7d2eba05 | 2666 | } |
b1caa957 KS |
2667 | |
2668 | /* TODO: teach khugepaged to collapse THP mapped with pte */ | |
2669 | if (PageCompound(page)) { | |
2670 | result = SCAN_PAGE_COMPOUND; | |
2671 | goto out_unmap; | |
2672 | } | |
2673 | ||
5c4b4be3 | 2674 | /* |
9f1b868a BL |
2675 | * Record which node the original page is from and save this |
2676 | * information to khugepaged_node_load[]. | |
2677 | * Khupaged will allocate hugepage from the node has the max | |
2678 | * hit record. | |
5c4b4be3 | 2679 | */ |
9f1b868a | 2680 | node = page_to_nid(page); |
7d2eba05 EA |
2681 | if (khugepaged_scan_abort(node)) { |
2682 | result = SCAN_SCAN_ABORT; | |
14a4e214 | 2683 | goto out_unmap; |
7d2eba05 | 2684 | } |
9f1b868a | 2685 | khugepaged_node_load[node]++; |
7d2eba05 | 2686 | if (!PageLRU(page)) { |
0fda2788 | 2687 | result = SCAN_PAGE_LRU; |
7d2eba05 EA |
2688 | goto out_unmap; |
2689 | } | |
2690 | if (PageLocked(page)) { | |
2691 | result = SCAN_PAGE_LOCK; | |
ba76149f | 2692 | goto out_unmap; |
7d2eba05 EA |
2693 | } |
2694 | if (!PageAnon(page)) { | |
2695 | result = SCAN_PAGE_ANON; | |
2696 | goto out_unmap; | |
2697 | } | |
2698 | ||
10359213 EA |
2699 | /* |
2700 | * cannot use mapcount: can't collapse if there's a gup pin. | |
2701 | * The page must only be referenced by the scanned process | |
2702 | * and page swap cache. | |
2703 | */ | |
7d2eba05 EA |
2704 | if (page_count(page) != 1 + !!PageSwapCache(page)) { |
2705 | result = SCAN_PAGE_COUNT; | |
ba76149f | 2706 | goto out_unmap; |
7d2eba05 | 2707 | } |
33c3fc71 VD |
2708 | if (pte_young(pteval) || |
2709 | page_is_young(page) || PageReferenced(page) || | |
8ee53820 | 2710 | mmu_notifier_test_young(vma->vm_mm, address)) |
10359213 | 2711 | referenced = true; |
ba76149f | 2712 | } |
7d2eba05 EA |
2713 | if (writable) { |
2714 | if (referenced) { | |
2715 | result = SCAN_SUCCEED; | |
2716 | ret = 1; | |
2717 | } else { | |
2718 | result = SCAN_NO_REFERENCED_PAGE; | |
2719 | } | |
2720 | } else { | |
2721 | result = SCAN_PAGE_RO; | |
2722 | } | |
ba76149f AA |
2723 | out_unmap: |
2724 | pte_unmap_unlock(pte, ptl); | |
9f1b868a BL |
2725 | if (ret) { |
2726 | node = khugepaged_find_target_node(); | |
ce83d217 | 2727 | /* collapse_huge_page will return with the mmap_sem released */ |
5c4b4be3 | 2728 | collapse_huge_page(mm, address, hpage, vma, node); |
9f1b868a | 2729 | } |
ba76149f | 2730 | out: |
16fd0fe4 | 2731 | trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced, |
70652f6e | 2732 | none_or_zero, result, unmapped); |
ba76149f AA |
2733 | return ret; |
2734 | } | |
2735 | ||
2736 | static void collect_mm_slot(struct mm_slot *mm_slot) | |
2737 | { | |
2738 | struct mm_struct *mm = mm_slot->mm; | |
2739 | ||
b9980cdc | 2740 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
ba76149f AA |
2741 | |
2742 | if (khugepaged_test_exit(mm)) { | |
2743 | /* free mm_slot */ | |
43b5fbbd | 2744 | hash_del(&mm_slot->hash); |
ba76149f AA |
2745 | list_del(&mm_slot->mm_node); |
2746 | ||
2747 | /* | |
2748 | * Not strictly needed because the mm exited already. | |
2749 | * | |
2750 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); | |
2751 | */ | |
2752 | ||
2753 | /* khugepaged_mm_lock actually not necessary for the below */ | |
2754 | free_mm_slot(mm_slot); | |
2755 | mmdrop(mm); | |
2756 | } | |
2757 | } | |
2758 | ||
2759 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, | |
2760 | struct page **hpage) | |
2f1da642 HS |
2761 | __releases(&khugepaged_mm_lock) |
2762 | __acquires(&khugepaged_mm_lock) | |
ba76149f AA |
2763 | { |
2764 | struct mm_slot *mm_slot; | |
2765 | struct mm_struct *mm; | |
2766 | struct vm_area_struct *vma; | |
2767 | int progress = 0; | |
2768 | ||
2769 | VM_BUG_ON(!pages); | |
b9980cdc | 2770 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); |
ba76149f AA |
2771 | |
2772 | if (khugepaged_scan.mm_slot) | |
2773 | mm_slot = khugepaged_scan.mm_slot; | |
2774 | else { | |
2775 | mm_slot = list_entry(khugepaged_scan.mm_head.next, | |
2776 | struct mm_slot, mm_node); | |
2777 | khugepaged_scan.address = 0; | |
2778 | khugepaged_scan.mm_slot = mm_slot; | |
2779 | } | |
2780 | spin_unlock(&khugepaged_mm_lock); | |
2781 | ||
2782 | mm = mm_slot->mm; | |
2783 | down_read(&mm->mmap_sem); | |
2784 | if (unlikely(khugepaged_test_exit(mm))) | |
2785 | vma = NULL; | |
2786 | else | |
2787 | vma = find_vma(mm, khugepaged_scan.address); | |
2788 | ||
2789 | progress++; | |
2790 | for (; vma; vma = vma->vm_next) { | |
2791 | unsigned long hstart, hend; | |
2792 | ||
2793 | cond_resched(); | |
2794 | if (unlikely(khugepaged_test_exit(mm))) { | |
2795 | progress++; | |
2796 | break; | |
2797 | } | |
fa475e51 BL |
2798 | if (!hugepage_vma_check(vma)) { |
2799 | skip: | |
ba76149f AA |
2800 | progress++; |
2801 | continue; | |
2802 | } | |
ba76149f AA |
2803 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; |
2804 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
a7d6e4ec AA |
2805 | if (hstart >= hend) |
2806 | goto skip; | |
2807 | if (khugepaged_scan.address > hend) | |
2808 | goto skip; | |
ba76149f AA |
2809 | if (khugepaged_scan.address < hstart) |
2810 | khugepaged_scan.address = hstart; | |
a7d6e4ec | 2811 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); |
ba76149f AA |
2812 | |
2813 | while (khugepaged_scan.address < hend) { | |
2814 | int ret; | |
2815 | cond_resched(); | |
2816 | if (unlikely(khugepaged_test_exit(mm))) | |
2817 | goto breakouterloop; | |
2818 | ||
2819 | VM_BUG_ON(khugepaged_scan.address < hstart || | |
2820 | khugepaged_scan.address + HPAGE_PMD_SIZE > | |
2821 | hend); | |
2822 | ret = khugepaged_scan_pmd(mm, vma, | |
2823 | khugepaged_scan.address, | |
2824 | hpage); | |
2825 | /* move to next address */ | |
2826 | khugepaged_scan.address += HPAGE_PMD_SIZE; | |
2827 | progress += HPAGE_PMD_NR; | |
2828 | if (ret) | |
2829 | /* we released mmap_sem so break loop */ | |
2830 | goto breakouterloop_mmap_sem; | |
2831 | if (progress >= pages) | |
2832 | goto breakouterloop; | |
2833 | } | |
2834 | } | |
2835 | breakouterloop: | |
2836 | up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */ | |
2837 | breakouterloop_mmap_sem: | |
2838 | ||
2839 | spin_lock(&khugepaged_mm_lock); | |
a7d6e4ec | 2840 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); |
ba76149f AA |
2841 | /* |
2842 | * Release the current mm_slot if this mm is about to die, or | |
2843 | * if we scanned all vmas of this mm. | |
2844 | */ | |
2845 | if (khugepaged_test_exit(mm) || !vma) { | |
2846 | /* | |
2847 | * Make sure that if mm_users is reaching zero while | |
2848 | * khugepaged runs here, khugepaged_exit will find | |
2849 | * mm_slot not pointing to the exiting mm. | |
2850 | */ | |
2851 | if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) { | |
2852 | khugepaged_scan.mm_slot = list_entry( | |
2853 | mm_slot->mm_node.next, | |
2854 | struct mm_slot, mm_node); | |
2855 | khugepaged_scan.address = 0; | |
2856 | } else { | |
2857 | khugepaged_scan.mm_slot = NULL; | |
2858 | khugepaged_full_scans++; | |
2859 | } | |
2860 | ||
2861 | collect_mm_slot(mm_slot); | |
2862 | } | |
2863 | ||
2864 | return progress; | |
2865 | } | |
2866 | ||
2867 | static int khugepaged_has_work(void) | |
2868 | { | |
2869 | return !list_empty(&khugepaged_scan.mm_head) && | |
2870 | khugepaged_enabled(); | |
2871 | } | |
2872 | ||
2873 | static int khugepaged_wait_event(void) | |
2874 | { | |
2875 | return !list_empty(&khugepaged_scan.mm_head) || | |
2017c0bf | 2876 | kthread_should_stop(); |
ba76149f AA |
2877 | } |
2878 | ||
d516904b | 2879 | static void khugepaged_do_scan(void) |
ba76149f | 2880 | { |
d516904b | 2881 | struct page *hpage = NULL; |
ba76149f AA |
2882 | unsigned int progress = 0, pass_through_head = 0; |
2883 | unsigned int pages = khugepaged_pages_to_scan; | |
d516904b | 2884 | bool wait = true; |
ba76149f AA |
2885 | |
2886 | barrier(); /* write khugepaged_pages_to_scan to local stack */ | |
2887 | ||
2888 | while (progress < pages) { | |
26234f36 | 2889 | if (!khugepaged_prealloc_page(&hpage, &wait)) |
d516904b | 2890 | break; |
26234f36 | 2891 | |
420256ef | 2892 | cond_resched(); |
ba76149f | 2893 | |
cd092411 | 2894 | if (unlikely(kthread_should_stop() || try_to_freeze())) |
878aee7d AA |
2895 | break; |
2896 | ||
ba76149f AA |
2897 | spin_lock(&khugepaged_mm_lock); |
2898 | if (!khugepaged_scan.mm_slot) | |
2899 | pass_through_head++; | |
2900 | if (khugepaged_has_work() && | |
2901 | pass_through_head < 2) | |
2902 | progress += khugepaged_scan_mm_slot(pages - progress, | |
d516904b | 2903 | &hpage); |
ba76149f AA |
2904 | else |
2905 | progress = pages; | |
2906 | spin_unlock(&khugepaged_mm_lock); | |
2907 | } | |
ba76149f | 2908 | |
d516904b XG |
2909 | if (!IS_ERR_OR_NULL(hpage)) |
2910 | put_page(hpage); | |
0bbbc0b3 AA |
2911 | } |
2912 | ||
f0508977 DR |
2913 | static bool khugepaged_should_wakeup(void) |
2914 | { | |
2915 | return kthread_should_stop() || | |
2916 | time_after_eq(jiffies, khugepaged_sleep_expire); | |
2917 | } | |
2918 | ||
2017c0bf XG |
2919 | static void khugepaged_wait_work(void) |
2920 | { | |
2017c0bf | 2921 | if (khugepaged_has_work()) { |
f0508977 DR |
2922 | const unsigned long scan_sleep_jiffies = |
2923 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs); | |
2924 | ||
2925 | if (!scan_sleep_jiffies) | |
2017c0bf XG |
2926 | return; |
2927 | ||
f0508977 | 2928 | khugepaged_sleep_expire = jiffies + scan_sleep_jiffies; |
2017c0bf | 2929 | wait_event_freezable_timeout(khugepaged_wait, |
f0508977 DR |
2930 | khugepaged_should_wakeup(), |
2931 | scan_sleep_jiffies); | |
2017c0bf XG |
2932 | return; |
2933 | } | |
2934 | ||
2935 | if (khugepaged_enabled()) | |
2936 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); | |
2937 | } | |
2938 | ||
ba76149f AA |
2939 | static int khugepaged(void *none) |
2940 | { | |
2941 | struct mm_slot *mm_slot; | |
2942 | ||
878aee7d | 2943 | set_freezable(); |
8698a745 | 2944 | set_user_nice(current, MAX_NICE); |
ba76149f | 2945 | |
b7231789 XG |
2946 | while (!kthread_should_stop()) { |
2947 | khugepaged_do_scan(); | |
2948 | khugepaged_wait_work(); | |
2949 | } | |
ba76149f AA |
2950 | |
2951 | spin_lock(&khugepaged_mm_lock); | |
2952 | mm_slot = khugepaged_scan.mm_slot; | |
2953 | khugepaged_scan.mm_slot = NULL; | |
2954 | if (mm_slot) | |
2955 | collect_mm_slot(mm_slot); | |
2956 | spin_unlock(&khugepaged_mm_lock); | |
ba76149f AA |
2957 | return 0; |
2958 | } | |
2959 | ||
eef1b3ba KS |
2960 | static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, |
2961 | unsigned long haddr, pmd_t *pmd) | |
2962 | { | |
2963 | struct mm_struct *mm = vma->vm_mm; | |
2964 | pgtable_t pgtable; | |
2965 | pmd_t _pmd; | |
2966 | int i; | |
2967 | ||
2968 | /* leave pmd empty until pte is filled */ | |
2969 | pmdp_huge_clear_flush_notify(vma, haddr, pmd); | |
2970 | ||
2971 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); | |
2972 | pmd_populate(mm, &_pmd, pgtable); | |
2973 | ||
2974 | for (i = 0; i < HPAGE_PMD_NR; i++, haddr += PAGE_SIZE) { | |
2975 | pte_t *pte, entry; | |
2976 | entry = pfn_pte(my_zero_pfn(haddr), vma->vm_page_prot); | |
2977 | entry = pte_mkspecial(entry); | |
2978 | pte = pte_offset_map(&_pmd, haddr); | |
2979 | VM_BUG_ON(!pte_none(*pte)); | |
2980 | set_pte_at(mm, haddr, pte, entry); | |
2981 | pte_unmap(pte); | |
2982 | } | |
2983 | smp_wmb(); /* make pte visible before pmd */ | |
2984 | pmd_populate(mm, pmd, pgtable); | |
2985 | put_huge_zero_page(); | |
2986 | } | |
2987 | ||
2988 | static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, | |
ba988280 | 2989 | unsigned long haddr, bool freeze) |
eef1b3ba KS |
2990 | { |
2991 | struct mm_struct *mm = vma->vm_mm; | |
2992 | struct page *page; | |
2993 | pgtable_t pgtable; | |
2994 | pmd_t _pmd; | |
b8d3c4c3 | 2995 | bool young, write, dirty; |
2ac015e2 | 2996 | unsigned long addr; |
eef1b3ba KS |
2997 | int i; |
2998 | ||
2999 | VM_BUG_ON(haddr & ~HPAGE_PMD_MASK); | |
3000 | VM_BUG_ON_VMA(vma->vm_start > haddr, vma); | |
3001 | VM_BUG_ON_VMA(vma->vm_end < haddr + HPAGE_PMD_SIZE, vma); | |
5c7fb56e | 3002 | VM_BUG_ON(!pmd_trans_huge(*pmd) && !pmd_devmap(*pmd)); |
eef1b3ba KS |
3003 | |
3004 | count_vm_event(THP_SPLIT_PMD); | |
3005 | ||
d21b9e57 KS |
3006 | if (!vma_is_anonymous(vma)) { |
3007 | _pmd = pmdp_huge_clear_flush_notify(vma, haddr, pmd); | |
eef1b3ba KS |
3008 | if (is_huge_zero_pmd(_pmd)) |
3009 | put_huge_zero_page(); | |
d21b9e57 KS |
3010 | if (vma_is_dax(vma)) |
3011 | return; | |
3012 | page = pmd_page(_pmd); | |
3013 | if (!PageReferenced(page) && pmd_young(_pmd)) | |
3014 | SetPageReferenced(page); | |
3015 | page_remove_rmap(page, true); | |
3016 | put_page(page); | |
3017 | add_mm_counter(mm, MM_FILEPAGES, -HPAGE_PMD_NR); | |
eef1b3ba KS |
3018 | return; |
3019 | } else if (is_huge_zero_pmd(*pmd)) { | |
3020 | return __split_huge_zero_page_pmd(vma, haddr, pmd); | |
3021 | } | |
3022 | ||
3023 | page = pmd_page(*pmd); | |
3024 | VM_BUG_ON_PAGE(!page_count(page), page); | |
fe896d18 | 3025 | page_ref_add(page, HPAGE_PMD_NR - 1); |
eef1b3ba KS |
3026 | write = pmd_write(*pmd); |
3027 | young = pmd_young(*pmd); | |
b8d3c4c3 | 3028 | dirty = pmd_dirty(*pmd); |
eef1b3ba | 3029 | |
c777e2a8 | 3030 | pmdp_huge_split_prepare(vma, haddr, pmd); |
eef1b3ba KS |
3031 | pgtable = pgtable_trans_huge_withdraw(mm, pmd); |
3032 | pmd_populate(mm, &_pmd, pgtable); | |
3033 | ||
2ac015e2 | 3034 | for (i = 0, addr = haddr; i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE) { |
eef1b3ba KS |
3035 | pte_t entry, *pte; |
3036 | /* | |
3037 | * Note that NUMA hinting access restrictions are not | |
3038 | * transferred to avoid any possibility of altering | |
3039 | * permissions across VMAs. | |
3040 | */ | |
ba988280 KS |
3041 | if (freeze) { |
3042 | swp_entry_t swp_entry; | |
3043 | swp_entry = make_migration_entry(page + i, write); | |
3044 | entry = swp_entry_to_pte(swp_entry); | |
3045 | } else { | |
3046 | entry = mk_pte(page + i, vma->vm_page_prot); | |
b8d3c4c3 | 3047 | entry = maybe_mkwrite(entry, vma); |
ba988280 KS |
3048 | if (!write) |
3049 | entry = pte_wrprotect(entry); | |
3050 | if (!young) | |
3051 | entry = pte_mkold(entry); | |
3052 | } | |
b8d3c4c3 MK |
3053 | if (dirty) |
3054 | SetPageDirty(page + i); | |
2ac015e2 | 3055 | pte = pte_offset_map(&_pmd, addr); |
eef1b3ba | 3056 | BUG_ON(!pte_none(*pte)); |
2ac015e2 | 3057 | set_pte_at(mm, addr, pte, entry); |
eef1b3ba KS |
3058 | atomic_inc(&page[i]._mapcount); |
3059 | pte_unmap(pte); | |
3060 | } | |
3061 | ||
3062 | /* | |
3063 | * Set PG_double_map before dropping compound_mapcount to avoid | |
3064 | * false-negative page_mapped(). | |
3065 | */ | |
3066 | if (compound_mapcount(page) > 1 && !TestSetPageDoubleMap(page)) { | |
3067 | for (i = 0; i < HPAGE_PMD_NR; i++) | |
3068 | atomic_inc(&page[i]._mapcount); | |
3069 | } | |
3070 | ||
3071 | if (atomic_add_negative(-1, compound_mapcount_ptr(page))) { | |
3072 | /* Last compound_mapcount is gone. */ | |
65c45377 | 3073 | __dec_zone_page_state(page, NR_ANON_THPS); |
eef1b3ba KS |
3074 | if (TestClearPageDoubleMap(page)) { |
3075 | /* No need in mapcount reference anymore */ | |
3076 | for (i = 0; i < HPAGE_PMD_NR; i++) | |
3077 | atomic_dec(&page[i]._mapcount); | |
3078 | } | |
3079 | } | |
3080 | ||
3081 | smp_wmb(); /* make pte visible before pmd */ | |
e9b61f19 KS |
3082 | /* |
3083 | * Up to this point the pmd is present and huge and userland has the | |
3084 | * whole access to the hugepage during the split (which happens in | |
3085 | * place). If we overwrite the pmd with the not-huge version pointing | |
3086 | * to the pte here (which of course we could if all CPUs were bug | |
3087 | * free), userland could trigger a small page size TLB miss on the | |
3088 | * small sized TLB while the hugepage TLB entry is still established in | |
3089 | * the huge TLB. Some CPU doesn't like that. | |
3090 | * See http://support.amd.com/us/Processor_TechDocs/41322.pdf, Erratum | |
3091 | * 383 on page 93. Intel should be safe but is also warns that it's | |
3092 | * only safe if the permission and cache attributes of the two entries | |
3093 | * loaded in the two TLB is identical (which should be the case here). | |
3094 | * But it is generally safer to never allow small and huge TLB entries | |
3095 | * for the same virtual address to be loaded simultaneously. So instead | |
3096 | * of doing "pmd_populate(); flush_pmd_tlb_range();" we first mark the | |
3097 | * current pmd notpresent (atomically because here the pmd_trans_huge | |
3098 | * and pmd_trans_splitting must remain set at all times on the pmd | |
3099 | * until the split is complete for this pmd), then we flush the SMP TLB | |
3100 | * and finally we write the non-huge version of the pmd entry with | |
3101 | * pmd_populate. | |
3102 | */ | |
3103 | pmdp_invalidate(vma, haddr, pmd); | |
eef1b3ba | 3104 | pmd_populate(mm, pmd, pgtable); |
e9b61f19 KS |
3105 | |
3106 | if (freeze) { | |
2ac015e2 | 3107 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
e9b61f19 KS |
3108 | page_remove_rmap(page + i, false); |
3109 | put_page(page + i); | |
3110 | } | |
3111 | } | |
eef1b3ba KS |
3112 | } |
3113 | ||
3114 | void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, | |
33f4751e | 3115 | unsigned long address, bool freeze, struct page *page) |
eef1b3ba KS |
3116 | { |
3117 | spinlock_t *ptl; | |
3118 | struct mm_struct *mm = vma->vm_mm; | |
3119 | unsigned long haddr = address & HPAGE_PMD_MASK; | |
3120 | ||
3121 | mmu_notifier_invalidate_range_start(mm, haddr, haddr + HPAGE_PMD_SIZE); | |
3122 | ptl = pmd_lock(mm, pmd); | |
33f4751e NH |
3123 | |
3124 | /* | |
3125 | * If caller asks to setup a migration entries, we need a page to check | |
3126 | * pmd against. Otherwise we can end up replacing wrong page. | |
3127 | */ | |
3128 | VM_BUG_ON(freeze && !page); | |
3129 | if (page && page != pmd_page(*pmd)) | |
3130 | goto out; | |
3131 | ||
5c7fb56e | 3132 | if (pmd_trans_huge(*pmd)) { |
33f4751e | 3133 | page = pmd_page(*pmd); |
5c7fb56e | 3134 | if (PageMlocked(page)) |
5f737714 | 3135 | clear_page_mlock(page); |
5c7fb56e | 3136 | } else if (!pmd_devmap(*pmd)) |
e90309c9 | 3137 | goto out; |
fec89c10 | 3138 | __split_huge_pmd_locked(vma, pmd, haddr, freeze); |
e90309c9 | 3139 | out: |
eef1b3ba KS |
3140 | spin_unlock(ptl); |
3141 | mmu_notifier_invalidate_range_end(mm, haddr, haddr + HPAGE_PMD_SIZE); | |
3142 | } | |
3143 | ||
fec89c10 KS |
3144 | void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, |
3145 | bool freeze, struct page *page) | |
94fcc585 | 3146 | { |
f72e7dcd HD |
3147 | pgd_t *pgd; |
3148 | pud_t *pud; | |
94fcc585 AA |
3149 | pmd_t *pmd; |
3150 | ||
78ddc534 | 3151 | pgd = pgd_offset(vma->vm_mm, address); |
f72e7dcd HD |
3152 | if (!pgd_present(*pgd)) |
3153 | return; | |
3154 | ||
3155 | pud = pud_offset(pgd, address); | |
3156 | if (!pud_present(*pud)) | |
3157 | return; | |
3158 | ||
3159 | pmd = pmd_offset(pud, address); | |
fec89c10 | 3160 | |
33f4751e | 3161 | __split_huge_pmd(vma, pmd, address, freeze, page); |
94fcc585 AA |
3162 | } |
3163 | ||
e1b9996b | 3164 | void vma_adjust_trans_huge(struct vm_area_struct *vma, |
94fcc585 AA |
3165 | unsigned long start, |
3166 | unsigned long end, | |
3167 | long adjust_next) | |
3168 | { | |
3169 | /* | |
3170 | * If the new start address isn't hpage aligned and it could | |
3171 | * previously contain an hugepage: check if we need to split | |
3172 | * an huge pmd. | |
3173 | */ | |
3174 | if (start & ~HPAGE_PMD_MASK && | |
3175 | (start & HPAGE_PMD_MASK) >= vma->vm_start && | |
3176 | (start & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) | |
fec89c10 | 3177 | split_huge_pmd_address(vma, start, false, NULL); |
94fcc585 AA |
3178 | |
3179 | /* | |
3180 | * If the new end address isn't hpage aligned and it could | |
3181 | * previously contain an hugepage: check if we need to split | |
3182 | * an huge pmd. | |
3183 | */ | |
3184 | if (end & ~HPAGE_PMD_MASK && | |
3185 | (end & HPAGE_PMD_MASK) >= vma->vm_start && | |
3186 | (end & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= vma->vm_end) | |
fec89c10 | 3187 | split_huge_pmd_address(vma, end, false, NULL); |
94fcc585 AA |
3188 | |
3189 | /* | |
3190 | * If we're also updating the vma->vm_next->vm_start, if the new | |
3191 | * vm_next->vm_start isn't page aligned and it could previously | |
3192 | * contain an hugepage: check if we need to split an huge pmd. | |
3193 | */ | |
3194 | if (adjust_next > 0) { | |
3195 | struct vm_area_struct *next = vma->vm_next; | |
3196 | unsigned long nstart = next->vm_start; | |
3197 | nstart += adjust_next << PAGE_SHIFT; | |
3198 | if (nstart & ~HPAGE_PMD_MASK && | |
3199 | (nstart & HPAGE_PMD_MASK) >= next->vm_start && | |
3200 | (nstart & HPAGE_PMD_MASK) + HPAGE_PMD_SIZE <= next->vm_end) | |
fec89c10 | 3201 | split_huge_pmd_address(next, nstart, false, NULL); |
94fcc585 AA |
3202 | } |
3203 | } | |
e9b61f19 | 3204 | |
fec89c10 | 3205 | static void freeze_page(struct page *page) |
e9b61f19 | 3206 | { |
baa355fd KS |
3207 | enum ttu_flags ttu_flags = TTU_IGNORE_MLOCK | TTU_IGNORE_ACCESS | |
3208 | TTU_RMAP_LOCKED; | |
fec89c10 | 3209 | int i, ret; |
e9b61f19 KS |
3210 | |
3211 | VM_BUG_ON_PAGE(!PageHead(page), page); | |
3212 | ||
baa355fd KS |
3213 | if (PageAnon(page)) |
3214 | ttu_flags |= TTU_MIGRATION; | |
3215 | ||
fec89c10 KS |
3216 | /* We only need TTU_SPLIT_HUGE_PMD once */ |
3217 | ret = try_to_unmap(page, ttu_flags | TTU_SPLIT_HUGE_PMD); | |
3218 | for (i = 1; !ret && i < HPAGE_PMD_NR; i++) { | |
3219 | /* Cut short if the page is unmapped */ | |
3220 | if (page_count(page) == 1) | |
3221 | return; | |
e9b61f19 | 3222 | |
fec89c10 | 3223 | ret = try_to_unmap(page + i, ttu_flags); |
e9b61f19 | 3224 | } |
baa355fd | 3225 | VM_BUG_ON_PAGE(ret, page + i - 1); |
e9b61f19 KS |
3226 | } |
3227 | ||
fec89c10 | 3228 | static void unfreeze_page(struct page *page) |
e9b61f19 | 3229 | { |
fec89c10 | 3230 | int i; |
e9b61f19 | 3231 | |
fec89c10 KS |
3232 | for (i = 0; i < HPAGE_PMD_NR; i++) |
3233 | remove_migration_ptes(page + i, page + i, true); | |
e9b61f19 KS |
3234 | } |
3235 | ||
8df651c7 | 3236 | static void __split_huge_page_tail(struct page *head, int tail, |
e9b61f19 KS |
3237 | struct lruvec *lruvec, struct list_head *list) |
3238 | { | |
e9b61f19 KS |
3239 | struct page *page_tail = head + tail; |
3240 | ||
8df651c7 | 3241 | VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); |
fe896d18 | 3242 | VM_BUG_ON_PAGE(page_ref_count(page_tail) != 0, page_tail); |
e9b61f19 KS |
3243 | |
3244 | /* | |
0139aa7b | 3245 | * tail_page->_refcount is zero and not changing from under us. But |
e9b61f19 | 3246 | * get_page_unless_zero() may be running from under us on the |
baa355fd KS |
3247 | * tail_page. If we used atomic_set() below instead of atomic_inc() or |
3248 | * atomic_add(), we would then run atomic_set() concurrently with | |
e9b61f19 KS |
3249 | * get_page_unless_zero(), and atomic_set() is implemented in C not |
3250 | * using locked ops. spin_unlock on x86 sometime uses locked ops | |
3251 | * because of PPro errata 66, 92, so unless somebody can guarantee | |
3252 | * atomic_set() here would be safe on all archs (and not only on x86), | |
baa355fd | 3253 | * it's safer to use atomic_inc()/atomic_add(). |
e9b61f19 | 3254 | */ |
baa355fd KS |
3255 | if (PageAnon(head)) { |
3256 | page_ref_inc(page_tail); | |
3257 | } else { | |
3258 | /* Additional pin to radix tree */ | |
3259 | page_ref_add(page_tail, 2); | |
3260 | } | |
e9b61f19 KS |
3261 | |
3262 | page_tail->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; | |
3263 | page_tail->flags |= (head->flags & | |
3264 | ((1L << PG_referenced) | | |
3265 | (1L << PG_swapbacked) | | |
3266 | (1L << PG_mlocked) | | |
3267 | (1L << PG_uptodate) | | |
3268 | (1L << PG_active) | | |
3269 | (1L << PG_locked) | | |
b8d3c4c3 MK |
3270 | (1L << PG_unevictable) | |
3271 | (1L << PG_dirty))); | |
e9b61f19 KS |
3272 | |
3273 | /* | |
3274 | * After clearing PageTail the gup refcount can be released. | |
3275 | * Page flags also must be visible before we make the page non-compound. | |
3276 | */ | |
3277 | smp_wmb(); | |
3278 | ||
3279 | clear_compound_head(page_tail); | |
3280 | ||
3281 | if (page_is_young(head)) | |
3282 | set_page_young(page_tail); | |
3283 | if (page_is_idle(head)) | |
3284 | set_page_idle(page_tail); | |
3285 | ||
3286 | /* ->mapping in first tail page is compound_mapcount */ | |
9a982250 | 3287 | VM_BUG_ON_PAGE(tail > 2 && page_tail->mapping != TAIL_MAPPING, |
e9b61f19 KS |
3288 | page_tail); |
3289 | page_tail->mapping = head->mapping; | |
3290 | ||
3291 | page_tail->index = head->index + tail; | |
3292 | page_cpupid_xchg_last(page_tail, page_cpupid_last(head)); | |
3293 | lru_add_page_tail(head, page_tail, lruvec, list); | |
e9b61f19 KS |
3294 | } |
3295 | ||
baa355fd KS |
3296 | static void __split_huge_page(struct page *page, struct list_head *list, |
3297 | unsigned long flags) | |
e9b61f19 KS |
3298 | { |
3299 | struct page *head = compound_head(page); | |
3300 | struct zone *zone = page_zone(head); | |
3301 | struct lruvec *lruvec; | |
baa355fd | 3302 | pgoff_t end = -1; |
8df651c7 | 3303 | int i; |
e9b61f19 | 3304 | |
e9b61f19 KS |
3305 | lruvec = mem_cgroup_page_lruvec(head, zone); |
3306 | ||
3307 | /* complete memcg works before add pages to LRU */ | |
3308 | mem_cgroup_split_huge_fixup(head); | |
3309 | ||
baa355fd KS |
3310 | if (!PageAnon(page)) |
3311 | end = DIV_ROUND_UP(i_size_read(head->mapping->host), PAGE_SIZE); | |
3312 | ||
3313 | for (i = HPAGE_PMD_NR - 1; i >= 1; i--) { | |
8df651c7 | 3314 | __split_huge_page_tail(head, i, lruvec, list); |
baa355fd KS |
3315 | /* Some pages can be beyond i_size: drop them from page cache */ |
3316 | if (head[i].index >= end) { | |
3317 | __ClearPageDirty(head + i); | |
3318 | __delete_from_page_cache(head + i, NULL); | |
3319 | put_page(head + i); | |
3320 | } | |
3321 | } | |
e9b61f19 KS |
3322 | |
3323 | ClearPageCompound(head); | |
baa355fd KS |
3324 | /* See comment in __split_huge_page_tail() */ |
3325 | if (PageAnon(head)) { | |
3326 | page_ref_inc(head); | |
3327 | } else { | |
3328 | /* Additional pin to radix tree */ | |
3329 | page_ref_add(head, 2); | |
3330 | spin_unlock(&head->mapping->tree_lock); | |
3331 | } | |
3332 | ||
3333 | spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags); | |
e9b61f19 | 3334 | |
fec89c10 | 3335 | unfreeze_page(head); |
e9b61f19 KS |
3336 | |
3337 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
3338 | struct page *subpage = head + i; | |
3339 | if (subpage == page) | |
3340 | continue; | |
3341 | unlock_page(subpage); | |
3342 | ||
3343 | /* | |
3344 | * Subpages may be freed if there wasn't any mapping | |
3345 | * like if add_to_swap() is running on a lru page that | |
3346 | * had its mapping zapped. And freeing these pages | |
3347 | * requires taking the lru_lock so we do the put_page | |
3348 | * of the tail pages after the split is complete. | |
3349 | */ | |
3350 | put_page(subpage); | |
3351 | } | |
3352 | } | |
3353 | ||
b20ce5e0 KS |
3354 | int total_mapcount(struct page *page) |
3355 | { | |
dd78fedd | 3356 | int i, compound, ret; |
b20ce5e0 KS |
3357 | |
3358 | VM_BUG_ON_PAGE(PageTail(page), page); | |
3359 | ||
3360 | if (likely(!PageCompound(page))) | |
3361 | return atomic_read(&page->_mapcount) + 1; | |
3362 | ||
dd78fedd | 3363 | compound = compound_mapcount(page); |
b20ce5e0 | 3364 | if (PageHuge(page)) |
dd78fedd KS |
3365 | return compound; |
3366 | ret = compound; | |
b20ce5e0 KS |
3367 | for (i = 0; i < HPAGE_PMD_NR; i++) |
3368 | ret += atomic_read(&page[i]._mapcount) + 1; | |
dd78fedd KS |
3369 | /* File pages has compound_mapcount included in _mapcount */ |
3370 | if (!PageAnon(page)) | |
3371 | return ret - compound * HPAGE_PMD_NR; | |
b20ce5e0 KS |
3372 | if (PageDoubleMap(page)) |
3373 | ret -= HPAGE_PMD_NR; | |
3374 | return ret; | |
3375 | } | |
3376 | ||
6d0a07ed AA |
3377 | /* |
3378 | * This calculates accurately how many mappings a transparent hugepage | |
3379 | * has (unlike page_mapcount() which isn't fully accurate). This full | |
3380 | * accuracy is primarily needed to know if copy-on-write faults can | |
3381 | * reuse the page and change the mapping to read-write instead of | |
3382 | * copying them. At the same time this returns the total_mapcount too. | |
3383 | * | |
3384 | * The function returns the highest mapcount any one of the subpages | |
3385 | * has. If the return value is one, even if different processes are | |
3386 | * mapping different subpages of the transparent hugepage, they can | |
3387 | * all reuse it, because each process is reusing a different subpage. | |
3388 | * | |
3389 | * The total_mapcount is instead counting all virtual mappings of the | |
3390 | * subpages. If the total_mapcount is equal to "one", it tells the | |
3391 | * caller all mappings belong to the same "mm" and in turn the | |
3392 | * anon_vma of the transparent hugepage can become the vma->anon_vma | |
3393 | * local one as no other process may be mapping any of the subpages. | |
3394 | * | |
3395 | * It would be more accurate to replace page_mapcount() with | |
3396 | * page_trans_huge_mapcount(), however we only use | |
3397 | * page_trans_huge_mapcount() in the copy-on-write faults where we | |
3398 | * need full accuracy to avoid breaking page pinning, because | |
3399 | * page_trans_huge_mapcount() is slower than page_mapcount(). | |
3400 | */ | |
3401 | int page_trans_huge_mapcount(struct page *page, int *total_mapcount) | |
3402 | { | |
3403 | int i, ret, _total_mapcount, mapcount; | |
3404 | ||
3405 | /* hugetlbfs shouldn't call it */ | |
3406 | VM_BUG_ON_PAGE(PageHuge(page), page); | |
3407 | ||
3408 | if (likely(!PageTransCompound(page))) { | |
3409 | mapcount = atomic_read(&page->_mapcount) + 1; | |
3410 | if (total_mapcount) | |
3411 | *total_mapcount = mapcount; | |
3412 | return mapcount; | |
3413 | } | |
3414 | ||
3415 | page = compound_head(page); | |
3416 | ||
3417 | _total_mapcount = ret = 0; | |
3418 | for (i = 0; i < HPAGE_PMD_NR; i++) { | |
3419 | mapcount = atomic_read(&page[i]._mapcount) + 1; | |
3420 | ret = max(ret, mapcount); | |
3421 | _total_mapcount += mapcount; | |
3422 | } | |
3423 | if (PageDoubleMap(page)) { | |
3424 | ret -= 1; | |
3425 | _total_mapcount -= HPAGE_PMD_NR; | |
3426 | } | |
3427 | mapcount = compound_mapcount(page); | |
3428 | ret += mapcount; | |
3429 | _total_mapcount += mapcount; | |
3430 | if (total_mapcount) | |
3431 | *total_mapcount = _total_mapcount; | |
3432 | return ret; | |
3433 | } | |
3434 | ||
e9b61f19 KS |
3435 | /* |
3436 | * This function splits huge page into normal pages. @page can point to any | |
3437 | * subpage of huge page to split. Split doesn't change the position of @page. | |
3438 | * | |
3439 | * Only caller must hold pin on the @page, otherwise split fails with -EBUSY. | |
3440 | * The huge page must be locked. | |
3441 | * | |
3442 | * If @list is null, tail pages will be added to LRU list, otherwise, to @list. | |
3443 | * | |
3444 | * Both head page and tail pages will inherit mapping, flags, and so on from | |
3445 | * the hugepage. | |
3446 | * | |
3447 | * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if | |
3448 | * they are not mapped. | |
3449 | * | |
3450 | * Returns 0 if the hugepage is split successfully. | |
3451 | * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under | |
3452 | * us. | |
3453 | */ | |
3454 | int split_huge_page_to_list(struct page *page, struct list_head *list) | |
3455 | { | |
3456 | struct page *head = compound_head(page); | |
a3d0a918 | 3457 | struct pglist_data *pgdata = NODE_DATA(page_to_nid(head)); |
baa355fd KS |
3458 | struct anon_vma *anon_vma = NULL; |
3459 | struct address_space *mapping = NULL; | |
3460 | int count, mapcount, extra_pins, ret; | |
d9654322 | 3461 | bool mlocked; |
0b9b6fff | 3462 | unsigned long flags; |
e9b61f19 KS |
3463 | |
3464 | VM_BUG_ON_PAGE(is_huge_zero_page(page), page); | |
e9b61f19 KS |
3465 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
3466 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); | |
3467 | VM_BUG_ON_PAGE(!PageCompound(page), page); | |
3468 | ||
baa355fd KS |
3469 | if (PageAnon(head)) { |
3470 | /* | |
3471 | * The caller does not necessarily hold an mmap_sem that would | |
3472 | * prevent the anon_vma disappearing so we first we take a | |
3473 | * reference to it and then lock the anon_vma for write. This | |
3474 | * is similar to page_lock_anon_vma_read except the write lock | |
3475 | * is taken to serialise against parallel split or collapse | |
3476 | * operations. | |
3477 | */ | |
3478 | anon_vma = page_get_anon_vma(head); | |
3479 | if (!anon_vma) { | |
3480 | ret = -EBUSY; | |
3481 | goto out; | |
3482 | } | |
3483 | extra_pins = 0; | |
3484 | mapping = NULL; | |
3485 | anon_vma_lock_write(anon_vma); | |
3486 | } else { | |
3487 | mapping = head->mapping; | |
3488 | ||
3489 | /* Truncated ? */ | |
3490 | if (!mapping) { | |
3491 | ret = -EBUSY; | |
3492 | goto out; | |
3493 | } | |
3494 | ||
3495 | /* Addidional pins from radix tree */ | |
3496 | extra_pins = HPAGE_PMD_NR; | |
3497 | anon_vma = NULL; | |
3498 | i_mmap_lock_read(mapping); | |
e9b61f19 | 3499 | } |
e9b61f19 KS |
3500 | |
3501 | /* | |
3502 | * Racy check if we can split the page, before freeze_page() will | |
3503 | * split PMDs | |
3504 | */ | |
baa355fd | 3505 | if (total_mapcount(head) != page_count(head) - extra_pins - 1) { |
e9b61f19 KS |
3506 | ret = -EBUSY; |
3507 | goto out_unlock; | |
3508 | } | |
3509 | ||
d9654322 | 3510 | mlocked = PageMlocked(page); |
fec89c10 | 3511 | freeze_page(head); |
e9b61f19 KS |
3512 | VM_BUG_ON_PAGE(compound_mapcount(head), head); |
3513 | ||
d9654322 KS |
3514 | /* Make sure the page is not on per-CPU pagevec as it takes pin */ |
3515 | if (mlocked) | |
3516 | lru_add_drain(); | |
3517 | ||
baa355fd KS |
3518 | /* prevent PageLRU to go away from under us, and freeze lru stats */ |
3519 | spin_lock_irqsave(&page_zone(head)->lru_lock, flags); | |
3520 | ||
3521 | if (mapping) { | |
3522 | void **pslot; | |
3523 | ||
3524 | spin_lock(&mapping->tree_lock); | |
3525 | pslot = radix_tree_lookup_slot(&mapping->page_tree, | |
3526 | page_index(head)); | |
3527 | /* | |
3528 | * Check if the head page is present in radix tree. | |
3529 | * We assume all tail are present too, if head is there. | |
3530 | */ | |
3531 | if (radix_tree_deref_slot_protected(pslot, | |
3532 | &mapping->tree_lock) != head) | |
3533 | goto fail; | |
3534 | } | |
3535 | ||
0139aa7b | 3536 | /* Prevent deferred_split_scan() touching ->_refcount */ |
baa355fd | 3537 | spin_lock(&pgdata->split_queue_lock); |
e9b61f19 KS |
3538 | count = page_count(head); |
3539 | mapcount = total_mapcount(head); | |
baa355fd | 3540 | if (!mapcount && page_ref_freeze(head, 1 + extra_pins)) { |
9a982250 | 3541 | if (!list_empty(page_deferred_list(head))) { |
a3d0a918 | 3542 | pgdata->split_queue_len--; |
9a982250 KS |
3543 | list_del(page_deferred_list(head)); |
3544 | } | |
65c45377 KS |
3545 | if (mapping) |
3546 | __dec_zone_page_state(page, NR_SHMEM_THPS); | |
baa355fd KS |
3547 | spin_unlock(&pgdata->split_queue_lock); |
3548 | __split_huge_page(page, list, flags); | |
e9b61f19 | 3549 | ret = 0; |
e9b61f19 | 3550 | } else { |
baa355fd KS |
3551 | if (IS_ENABLED(CONFIG_DEBUG_VM) && mapcount) { |
3552 | pr_alert("total_mapcount: %u, page_count(): %u\n", | |
3553 | mapcount, count); | |
3554 | if (PageTail(page)) | |
3555 | dump_page(head, NULL); | |
3556 | dump_page(page, "total_mapcount(head) > 0"); | |
3557 | BUG(); | |
3558 | } | |
3559 | spin_unlock(&pgdata->split_queue_lock); | |
3560 | fail: if (mapping) | |
3561 | spin_unlock(&mapping->tree_lock); | |
3562 | spin_unlock_irqrestore(&page_zone(head)->lru_lock, flags); | |
fec89c10 | 3563 | unfreeze_page(head); |
e9b61f19 KS |
3564 | ret = -EBUSY; |
3565 | } | |
3566 | ||
3567 | out_unlock: | |
baa355fd KS |
3568 | if (anon_vma) { |
3569 | anon_vma_unlock_write(anon_vma); | |
3570 | put_anon_vma(anon_vma); | |
3571 | } | |
3572 | if (mapping) | |
3573 | i_mmap_unlock_read(mapping); | |
e9b61f19 KS |
3574 | out: |
3575 | count_vm_event(!ret ? THP_SPLIT_PAGE : THP_SPLIT_PAGE_FAILED); | |
3576 | return ret; | |
3577 | } | |
9a982250 KS |
3578 | |
3579 | void free_transhuge_page(struct page *page) | |
3580 | { | |
a3d0a918 | 3581 | struct pglist_data *pgdata = NODE_DATA(page_to_nid(page)); |
9a982250 KS |
3582 | unsigned long flags; |
3583 | ||
a3d0a918 | 3584 | spin_lock_irqsave(&pgdata->split_queue_lock, flags); |
9a982250 | 3585 | if (!list_empty(page_deferred_list(page))) { |
a3d0a918 | 3586 | pgdata->split_queue_len--; |
9a982250 KS |
3587 | list_del(page_deferred_list(page)); |
3588 | } | |
a3d0a918 | 3589 | spin_unlock_irqrestore(&pgdata->split_queue_lock, flags); |
9a982250 KS |
3590 | free_compound_page(page); |
3591 | } | |
3592 | ||
3593 | void deferred_split_huge_page(struct page *page) | |
3594 | { | |
a3d0a918 | 3595 | struct pglist_data *pgdata = NODE_DATA(page_to_nid(page)); |
9a982250 KS |
3596 | unsigned long flags; |
3597 | ||
3598 | VM_BUG_ON_PAGE(!PageTransHuge(page), page); | |
3599 | ||
a3d0a918 | 3600 | spin_lock_irqsave(&pgdata->split_queue_lock, flags); |
9a982250 | 3601 | if (list_empty(page_deferred_list(page))) { |
f9719a03 | 3602 | count_vm_event(THP_DEFERRED_SPLIT_PAGE); |
a3d0a918 KS |
3603 | list_add_tail(page_deferred_list(page), &pgdata->split_queue); |
3604 | pgdata->split_queue_len++; | |
9a982250 | 3605 | } |
a3d0a918 | 3606 | spin_unlock_irqrestore(&pgdata->split_queue_lock, flags); |
9a982250 KS |
3607 | } |
3608 | ||
3609 | static unsigned long deferred_split_count(struct shrinker *shrink, | |
3610 | struct shrink_control *sc) | |
3611 | { | |
a3d0a918 | 3612 | struct pglist_data *pgdata = NODE_DATA(sc->nid); |
cb8d68ec | 3613 | return ACCESS_ONCE(pgdata->split_queue_len); |
9a982250 KS |
3614 | } |
3615 | ||
3616 | static unsigned long deferred_split_scan(struct shrinker *shrink, | |
3617 | struct shrink_control *sc) | |
3618 | { | |
a3d0a918 | 3619 | struct pglist_data *pgdata = NODE_DATA(sc->nid); |
9a982250 KS |
3620 | unsigned long flags; |
3621 | LIST_HEAD(list), *pos, *next; | |
3622 | struct page *page; | |
3623 | int split = 0; | |
3624 | ||
a3d0a918 | 3625 | spin_lock_irqsave(&pgdata->split_queue_lock, flags); |
9a982250 | 3626 | /* Take pin on all head pages to avoid freeing them under us */ |
ae026204 | 3627 | list_for_each_safe(pos, next, &pgdata->split_queue) { |
9a982250 KS |
3628 | page = list_entry((void *)pos, struct page, mapping); |
3629 | page = compound_head(page); | |
e3ae1953 KS |
3630 | if (get_page_unless_zero(page)) { |
3631 | list_move(page_deferred_list(page), &list); | |
3632 | } else { | |
3633 | /* We lost race with put_compound_page() */ | |
9a982250 | 3634 | list_del_init(page_deferred_list(page)); |
a3d0a918 | 3635 | pgdata->split_queue_len--; |
9a982250 | 3636 | } |
e3ae1953 KS |
3637 | if (!--sc->nr_to_scan) |
3638 | break; | |
9a982250 | 3639 | } |
a3d0a918 | 3640 | spin_unlock_irqrestore(&pgdata->split_queue_lock, flags); |
9a982250 KS |
3641 | |
3642 | list_for_each_safe(pos, next, &list) { | |
3643 | page = list_entry((void *)pos, struct page, mapping); | |
3644 | lock_page(page); | |
3645 | /* split_huge_page() removes page from list on success */ | |
3646 | if (!split_huge_page(page)) | |
3647 | split++; | |
3648 | unlock_page(page); | |
3649 | put_page(page); | |
3650 | } | |
3651 | ||
a3d0a918 KS |
3652 | spin_lock_irqsave(&pgdata->split_queue_lock, flags); |
3653 | list_splice_tail(&list, &pgdata->split_queue); | |
3654 | spin_unlock_irqrestore(&pgdata->split_queue_lock, flags); | |
9a982250 | 3655 | |
cb8d68ec KS |
3656 | /* |
3657 | * Stop shrinker if we didn't split any page, but the queue is empty. | |
3658 | * This can happen if pages were freed under us. | |
3659 | */ | |
3660 | if (!split && list_empty(&pgdata->split_queue)) | |
3661 | return SHRINK_STOP; | |
3662 | return split; | |
9a982250 KS |
3663 | } |
3664 | ||
3665 | static struct shrinker deferred_split_shrinker = { | |
3666 | .count_objects = deferred_split_count, | |
3667 | .scan_objects = deferred_split_scan, | |
3668 | .seeks = DEFAULT_SEEKS, | |
a3d0a918 | 3669 | .flags = SHRINKER_NUMA_AWARE, |
9a982250 | 3670 | }; |
49071d43 KS |
3671 | |
3672 | #ifdef CONFIG_DEBUG_FS | |
3673 | static int split_huge_pages_set(void *data, u64 val) | |
3674 | { | |
3675 | struct zone *zone; | |
3676 | struct page *page; | |
3677 | unsigned long pfn, max_zone_pfn; | |
3678 | unsigned long total = 0, split = 0; | |
3679 | ||
3680 | if (val != 1) | |
3681 | return -EINVAL; | |
3682 | ||
3683 | for_each_populated_zone(zone) { | |
3684 | max_zone_pfn = zone_end_pfn(zone); | |
3685 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) { | |
3686 | if (!pfn_valid(pfn)) | |
3687 | continue; | |
3688 | ||
3689 | page = pfn_to_page(pfn); | |
3690 | if (!get_page_unless_zero(page)) | |
3691 | continue; | |
3692 | ||
3693 | if (zone != page_zone(page)) | |
3694 | goto next; | |
3695 | ||
baa355fd | 3696 | if (!PageHead(page) || PageHuge(page) || !PageLRU(page)) |
49071d43 KS |
3697 | goto next; |
3698 | ||
3699 | total++; | |
3700 | lock_page(page); | |
3701 | if (!split_huge_page(page)) | |
3702 | split++; | |
3703 | unlock_page(page); | |
3704 | next: | |
3705 | put_page(page); | |
3706 | } | |
3707 | } | |
3708 | ||
145bdaa1 | 3709 | pr_info("%lu of %lu THP split\n", split, total); |
49071d43 KS |
3710 | |
3711 | return 0; | |
3712 | } | |
3713 | DEFINE_SIMPLE_ATTRIBUTE(split_huge_pages_fops, NULL, split_huge_pages_set, | |
3714 | "%llu\n"); | |
3715 | ||
3716 | static int __init split_huge_pages_debugfs(void) | |
3717 | { | |
3718 | void *ret; | |
3719 | ||
145bdaa1 | 3720 | ret = debugfs_create_file("split_huge_pages", 0200, NULL, NULL, |
49071d43 KS |
3721 | &split_huge_pages_fops); |
3722 | if (!ret) | |
3723 | pr_warn("Failed to create split_huge_pages in debugfs"); | |
3724 | return 0; | |
3725 | } | |
3726 | late_initcall(split_huge_pages_debugfs); | |
3727 | #endif |