Commit | Line | Data |
---|---|---|
b46e756f KS |
1 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
2 | ||
3 | #include <linux/mm.h> | |
4 | #include <linux/sched.h> | |
5 | #include <linux/mmu_notifier.h> | |
6 | #include <linux/rmap.h> | |
7 | #include <linux/swap.h> | |
8 | #include <linux/mm_inline.h> | |
9 | #include <linux/kthread.h> | |
10 | #include <linux/khugepaged.h> | |
11 | #include <linux/freezer.h> | |
12 | #include <linux/mman.h> | |
13 | #include <linux/hashtable.h> | |
14 | #include <linux/userfaultfd_k.h> | |
15 | #include <linux/page_idle.h> | |
16 | #include <linux/swapops.h> | |
f3f0e1d2 | 17 | #include <linux/shmem_fs.h> |
b46e756f KS |
18 | |
19 | #include <asm/tlb.h> | |
20 | #include <asm/pgalloc.h> | |
21 | #include "internal.h" | |
22 | ||
23 | enum scan_result { | |
24 | SCAN_FAIL, | |
25 | SCAN_SUCCEED, | |
26 | SCAN_PMD_NULL, | |
27 | SCAN_EXCEED_NONE_PTE, | |
28 | SCAN_PTE_NON_PRESENT, | |
29 | SCAN_PAGE_RO, | |
30 | SCAN_NO_REFERENCED_PAGE, | |
31 | SCAN_PAGE_NULL, | |
32 | SCAN_SCAN_ABORT, | |
33 | SCAN_PAGE_COUNT, | |
34 | SCAN_PAGE_LRU, | |
35 | SCAN_PAGE_LOCK, | |
36 | SCAN_PAGE_ANON, | |
37 | SCAN_PAGE_COMPOUND, | |
38 | SCAN_ANY_PROCESS, | |
39 | SCAN_VMA_NULL, | |
40 | SCAN_VMA_CHECK, | |
41 | SCAN_ADDRESS_RANGE, | |
42 | SCAN_SWAP_CACHE_PAGE, | |
43 | SCAN_DEL_PAGE_LRU, | |
44 | SCAN_ALLOC_HUGE_PAGE_FAIL, | |
45 | SCAN_CGROUP_CHARGE_FAIL, | |
f3f0e1d2 KS |
46 | SCAN_EXCEED_SWAP_PTE, |
47 | SCAN_TRUNCATED, | |
b46e756f KS |
48 | }; |
49 | ||
50 | #define CREATE_TRACE_POINTS | |
51 | #include <trace/events/huge_memory.h> | |
52 | ||
53 | /* default scan 8*512 pte (or vmas) every 30 second */ | |
54 | static unsigned int khugepaged_pages_to_scan __read_mostly; | |
55 | static unsigned int khugepaged_pages_collapsed; | |
56 | static unsigned int khugepaged_full_scans; | |
57 | static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000; | |
58 | /* during fragmentation poll the hugepage allocator once every minute */ | |
59 | static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000; | |
60 | static unsigned long khugepaged_sleep_expire; | |
61 | static DEFINE_SPINLOCK(khugepaged_mm_lock); | |
62 | static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait); | |
63 | /* | |
64 | * default collapse hugepages if there is at least one pte mapped like | |
65 | * it would have happened if the vma was large enough during page | |
66 | * fault. | |
67 | */ | |
68 | static unsigned int khugepaged_max_ptes_none __read_mostly; | |
69 | static unsigned int khugepaged_max_ptes_swap __read_mostly; | |
70 | ||
71 | #define MM_SLOTS_HASH_BITS 10 | |
72 | static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS); | |
73 | ||
74 | static struct kmem_cache *mm_slot_cache __read_mostly; | |
75 | ||
76 | /** | |
77 | * struct mm_slot - hash lookup from mm to mm_slot | |
78 | * @hash: hash collision list | |
79 | * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head | |
80 | * @mm: the mm that this information is valid for | |
81 | */ | |
82 | struct mm_slot { | |
83 | struct hlist_node hash; | |
84 | struct list_head mm_node; | |
85 | struct mm_struct *mm; | |
86 | }; | |
87 | ||
88 | /** | |
89 | * struct khugepaged_scan - cursor for scanning | |
90 | * @mm_head: the head of the mm list to scan | |
91 | * @mm_slot: the current mm_slot we are scanning | |
92 | * @address: the next address inside that to be scanned | |
93 | * | |
94 | * There is only the one khugepaged_scan instance of this cursor structure. | |
95 | */ | |
96 | struct khugepaged_scan { | |
97 | struct list_head mm_head; | |
98 | struct mm_slot *mm_slot; | |
99 | unsigned long address; | |
100 | }; | |
101 | ||
102 | static struct khugepaged_scan khugepaged_scan = { | |
103 | .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head), | |
104 | }; | |
105 | ||
106 | static ssize_t scan_sleep_millisecs_show(struct kobject *kobj, | |
107 | struct kobj_attribute *attr, | |
108 | char *buf) | |
109 | { | |
110 | return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs); | |
111 | } | |
112 | ||
113 | static ssize_t scan_sleep_millisecs_store(struct kobject *kobj, | |
114 | struct kobj_attribute *attr, | |
115 | const char *buf, size_t count) | |
116 | { | |
117 | unsigned long msecs; | |
118 | int err; | |
119 | ||
120 | err = kstrtoul(buf, 10, &msecs); | |
121 | if (err || msecs > UINT_MAX) | |
122 | return -EINVAL; | |
123 | ||
124 | khugepaged_scan_sleep_millisecs = msecs; | |
125 | khugepaged_sleep_expire = 0; | |
126 | wake_up_interruptible(&khugepaged_wait); | |
127 | ||
128 | return count; | |
129 | } | |
130 | static struct kobj_attribute scan_sleep_millisecs_attr = | |
131 | __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show, | |
132 | scan_sleep_millisecs_store); | |
133 | ||
134 | static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj, | |
135 | struct kobj_attribute *attr, | |
136 | char *buf) | |
137 | { | |
138 | return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs); | |
139 | } | |
140 | ||
141 | static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj, | |
142 | struct kobj_attribute *attr, | |
143 | const char *buf, size_t count) | |
144 | { | |
145 | unsigned long msecs; | |
146 | int err; | |
147 | ||
148 | err = kstrtoul(buf, 10, &msecs); | |
149 | if (err || msecs > UINT_MAX) | |
150 | return -EINVAL; | |
151 | ||
152 | khugepaged_alloc_sleep_millisecs = msecs; | |
153 | khugepaged_sleep_expire = 0; | |
154 | wake_up_interruptible(&khugepaged_wait); | |
155 | ||
156 | return count; | |
157 | } | |
158 | static struct kobj_attribute alloc_sleep_millisecs_attr = | |
159 | __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show, | |
160 | alloc_sleep_millisecs_store); | |
161 | ||
162 | static ssize_t pages_to_scan_show(struct kobject *kobj, | |
163 | struct kobj_attribute *attr, | |
164 | char *buf) | |
165 | { | |
166 | return sprintf(buf, "%u\n", khugepaged_pages_to_scan); | |
167 | } | |
168 | static ssize_t pages_to_scan_store(struct kobject *kobj, | |
169 | struct kobj_attribute *attr, | |
170 | const char *buf, size_t count) | |
171 | { | |
172 | int err; | |
173 | unsigned long pages; | |
174 | ||
175 | err = kstrtoul(buf, 10, &pages); | |
176 | if (err || !pages || pages > UINT_MAX) | |
177 | return -EINVAL; | |
178 | ||
179 | khugepaged_pages_to_scan = pages; | |
180 | ||
181 | return count; | |
182 | } | |
183 | static struct kobj_attribute pages_to_scan_attr = | |
184 | __ATTR(pages_to_scan, 0644, pages_to_scan_show, | |
185 | pages_to_scan_store); | |
186 | ||
187 | static ssize_t pages_collapsed_show(struct kobject *kobj, | |
188 | struct kobj_attribute *attr, | |
189 | char *buf) | |
190 | { | |
191 | return sprintf(buf, "%u\n", khugepaged_pages_collapsed); | |
192 | } | |
193 | static struct kobj_attribute pages_collapsed_attr = | |
194 | __ATTR_RO(pages_collapsed); | |
195 | ||
196 | static ssize_t full_scans_show(struct kobject *kobj, | |
197 | struct kobj_attribute *attr, | |
198 | char *buf) | |
199 | { | |
200 | return sprintf(buf, "%u\n", khugepaged_full_scans); | |
201 | } | |
202 | static struct kobj_attribute full_scans_attr = | |
203 | __ATTR_RO(full_scans); | |
204 | ||
205 | static ssize_t khugepaged_defrag_show(struct kobject *kobj, | |
206 | struct kobj_attribute *attr, char *buf) | |
207 | { | |
208 | return single_hugepage_flag_show(kobj, attr, buf, | |
209 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); | |
210 | } | |
211 | static ssize_t khugepaged_defrag_store(struct kobject *kobj, | |
212 | struct kobj_attribute *attr, | |
213 | const char *buf, size_t count) | |
214 | { | |
215 | return single_hugepage_flag_store(kobj, attr, buf, count, | |
216 | TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG); | |
217 | } | |
218 | static struct kobj_attribute khugepaged_defrag_attr = | |
219 | __ATTR(defrag, 0644, khugepaged_defrag_show, | |
220 | khugepaged_defrag_store); | |
221 | ||
222 | /* | |
223 | * max_ptes_none controls if khugepaged should collapse hugepages over | |
224 | * any unmapped ptes in turn potentially increasing the memory | |
225 | * footprint of the vmas. When max_ptes_none is 0 khugepaged will not | |
226 | * reduce the available free memory in the system as it | |
227 | * runs. Increasing max_ptes_none will instead potentially reduce the | |
228 | * free memory in the system during the khugepaged scan. | |
229 | */ | |
230 | static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj, | |
231 | struct kobj_attribute *attr, | |
232 | char *buf) | |
233 | { | |
234 | return sprintf(buf, "%u\n", khugepaged_max_ptes_none); | |
235 | } | |
236 | static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj, | |
237 | struct kobj_attribute *attr, | |
238 | const char *buf, size_t count) | |
239 | { | |
240 | int err; | |
241 | unsigned long max_ptes_none; | |
242 | ||
243 | err = kstrtoul(buf, 10, &max_ptes_none); | |
244 | if (err || max_ptes_none > HPAGE_PMD_NR-1) | |
245 | return -EINVAL; | |
246 | ||
247 | khugepaged_max_ptes_none = max_ptes_none; | |
248 | ||
249 | return count; | |
250 | } | |
251 | static struct kobj_attribute khugepaged_max_ptes_none_attr = | |
252 | __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show, | |
253 | khugepaged_max_ptes_none_store); | |
254 | ||
255 | static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj, | |
256 | struct kobj_attribute *attr, | |
257 | char *buf) | |
258 | { | |
259 | return sprintf(buf, "%u\n", khugepaged_max_ptes_swap); | |
260 | } | |
261 | ||
262 | static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj, | |
263 | struct kobj_attribute *attr, | |
264 | const char *buf, size_t count) | |
265 | { | |
266 | int err; | |
267 | unsigned long max_ptes_swap; | |
268 | ||
269 | err = kstrtoul(buf, 10, &max_ptes_swap); | |
270 | if (err || max_ptes_swap > HPAGE_PMD_NR-1) | |
271 | return -EINVAL; | |
272 | ||
273 | khugepaged_max_ptes_swap = max_ptes_swap; | |
274 | ||
275 | return count; | |
276 | } | |
277 | ||
278 | static struct kobj_attribute khugepaged_max_ptes_swap_attr = | |
279 | __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show, | |
280 | khugepaged_max_ptes_swap_store); | |
281 | ||
282 | static struct attribute *khugepaged_attr[] = { | |
283 | &khugepaged_defrag_attr.attr, | |
284 | &khugepaged_max_ptes_none_attr.attr, | |
285 | &pages_to_scan_attr.attr, | |
286 | &pages_collapsed_attr.attr, | |
287 | &full_scans_attr.attr, | |
288 | &scan_sleep_millisecs_attr.attr, | |
289 | &alloc_sleep_millisecs_attr.attr, | |
290 | &khugepaged_max_ptes_swap_attr.attr, | |
291 | NULL, | |
292 | }; | |
293 | ||
294 | struct attribute_group khugepaged_attr_group = { | |
295 | .attrs = khugepaged_attr, | |
296 | .name = "khugepaged", | |
297 | }; | |
298 | ||
f3f0e1d2 | 299 | #define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB) |
b46e756f KS |
300 | |
301 | int hugepage_madvise(struct vm_area_struct *vma, | |
302 | unsigned long *vm_flags, int advice) | |
303 | { | |
304 | switch (advice) { | |
305 | case MADV_HUGEPAGE: | |
306 | #ifdef CONFIG_S390 | |
307 | /* | |
308 | * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390 | |
309 | * can't handle this properly after s390_enable_sie, so we simply | |
310 | * ignore the madvise to prevent qemu from causing a SIGSEGV. | |
311 | */ | |
312 | if (mm_has_pgste(vma->vm_mm)) | |
313 | return 0; | |
314 | #endif | |
315 | *vm_flags &= ~VM_NOHUGEPAGE; | |
316 | *vm_flags |= VM_HUGEPAGE; | |
317 | /* | |
318 | * If the vma become good for khugepaged to scan, | |
319 | * register it here without waiting a page fault that | |
320 | * may not happen any time soon. | |
321 | */ | |
322 | if (!(*vm_flags & VM_NO_KHUGEPAGED) && | |
323 | khugepaged_enter_vma_merge(vma, *vm_flags)) | |
324 | return -ENOMEM; | |
325 | break; | |
326 | case MADV_NOHUGEPAGE: | |
327 | *vm_flags &= ~VM_HUGEPAGE; | |
328 | *vm_flags |= VM_NOHUGEPAGE; | |
329 | /* | |
330 | * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning | |
331 | * this vma even if we leave the mm registered in khugepaged if | |
332 | * it got registered before VM_NOHUGEPAGE was set. | |
333 | */ | |
334 | break; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | } | |
339 | ||
340 | int __init khugepaged_init(void) | |
341 | { | |
342 | mm_slot_cache = kmem_cache_create("khugepaged_mm_slot", | |
343 | sizeof(struct mm_slot), | |
344 | __alignof__(struct mm_slot), 0, NULL); | |
345 | if (!mm_slot_cache) | |
346 | return -ENOMEM; | |
347 | ||
348 | khugepaged_pages_to_scan = HPAGE_PMD_NR * 8; | |
349 | khugepaged_max_ptes_none = HPAGE_PMD_NR - 1; | |
350 | khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8; | |
351 | ||
352 | return 0; | |
353 | } | |
354 | ||
355 | void __init khugepaged_destroy(void) | |
356 | { | |
357 | kmem_cache_destroy(mm_slot_cache); | |
358 | } | |
359 | ||
360 | static inline struct mm_slot *alloc_mm_slot(void) | |
361 | { | |
362 | if (!mm_slot_cache) /* initialization failed */ | |
363 | return NULL; | |
364 | return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL); | |
365 | } | |
366 | ||
367 | static inline void free_mm_slot(struct mm_slot *mm_slot) | |
368 | { | |
369 | kmem_cache_free(mm_slot_cache, mm_slot); | |
370 | } | |
371 | ||
372 | static struct mm_slot *get_mm_slot(struct mm_struct *mm) | |
373 | { | |
374 | struct mm_slot *mm_slot; | |
375 | ||
376 | hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm) | |
377 | if (mm == mm_slot->mm) | |
378 | return mm_slot; | |
379 | ||
380 | return NULL; | |
381 | } | |
382 | ||
383 | static void insert_to_mm_slots_hash(struct mm_struct *mm, | |
384 | struct mm_slot *mm_slot) | |
385 | { | |
386 | mm_slot->mm = mm; | |
387 | hash_add(mm_slots_hash, &mm_slot->hash, (long)mm); | |
388 | } | |
389 | ||
390 | static inline int khugepaged_test_exit(struct mm_struct *mm) | |
391 | { | |
392 | return atomic_read(&mm->mm_users) == 0; | |
393 | } | |
394 | ||
395 | int __khugepaged_enter(struct mm_struct *mm) | |
396 | { | |
397 | struct mm_slot *mm_slot; | |
398 | int wakeup; | |
399 | ||
400 | mm_slot = alloc_mm_slot(); | |
401 | if (!mm_slot) | |
402 | return -ENOMEM; | |
403 | ||
404 | /* __khugepaged_exit() must not run from under us */ | |
405 | VM_BUG_ON_MM(khugepaged_test_exit(mm), mm); | |
406 | if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) { | |
407 | free_mm_slot(mm_slot); | |
408 | return 0; | |
409 | } | |
410 | ||
411 | spin_lock(&khugepaged_mm_lock); | |
412 | insert_to_mm_slots_hash(mm, mm_slot); | |
413 | /* | |
414 | * Insert just behind the scanning cursor, to let the area settle | |
415 | * down a little. | |
416 | */ | |
417 | wakeup = list_empty(&khugepaged_scan.mm_head); | |
418 | list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head); | |
419 | spin_unlock(&khugepaged_mm_lock); | |
420 | ||
421 | atomic_inc(&mm->mm_count); | |
422 | if (wakeup) | |
423 | wake_up_interruptible(&khugepaged_wait); | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | int khugepaged_enter_vma_merge(struct vm_area_struct *vma, | |
429 | unsigned long vm_flags) | |
430 | { | |
431 | unsigned long hstart, hend; | |
432 | if (!vma->anon_vma) | |
433 | /* | |
434 | * Not yet faulted in so we will register later in the | |
435 | * page fault if needed. | |
436 | */ | |
437 | return 0; | |
438 | if (vma->vm_ops || (vm_flags & VM_NO_KHUGEPAGED)) | |
439 | /* khugepaged not yet working on file or special mappings */ | |
440 | return 0; | |
441 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; | |
442 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
443 | if (hstart < hend) | |
444 | return khugepaged_enter(vma, vm_flags); | |
445 | return 0; | |
446 | } | |
447 | ||
448 | void __khugepaged_exit(struct mm_struct *mm) | |
449 | { | |
450 | struct mm_slot *mm_slot; | |
451 | int free = 0; | |
452 | ||
453 | spin_lock(&khugepaged_mm_lock); | |
454 | mm_slot = get_mm_slot(mm); | |
455 | if (mm_slot && khugepaged_scan.mm_slot != mm_slot) { | |
456 | hash_del(&mm_slot->hash); | |
457 | list_del(&mm_slot->mm_node); | |
458 | free = 1; | |
459 | } | |
460 | spin_unlock(&khugepaged_mm_lock); | |
461 | ||
462 | if (free) { | |
463 | clear_bit(MMF_VM_HUGEPAGE, &mm->flags); | |
464 | free_mm_slot(mm_slot); | |
465 | mmdrop(mm); | |
466 | } else if (mm_slot) { | |
467 | /* | |
468 | * This is required to serialize against | |
469 | * khugepaged_test_exit() (which is guaranteed to run | |
470 | * under mmap sem read mode). Stop here (after we | |
471 | * return all pagetables will be destroyed) until | |
472 | * khugepaged has finished working on the pagetables | |
473 | * under the mmap_sem. | |
474 | */ | |
475 | down_write(&mm->mmap_sem); | |
476 | up_write(&mm->mmap_sem); | |
477 | } | |
478 | } | |
479 | ||
480 | static void release_pte_page(struct page *page) | |
481 | { | |
482 | /* 0 stands for page_is_file_cache(page) == false */ | |
483 | dec_zone_page_state(page, NR_ISOLATED_ANON + 0); | |
484 | unlock_page(page); | |
485 | putback_lru_page(page); | |
486 | } | |
487 | ||
488 | static void release_pte_pages(pte_t *pte, pte_t *_pte) | |
489 | { | |
490 | while (--_pte >= pte) { | |
491 | pte_t pteval = *_pte; | |
492 | if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval))) | |
493 | release_pte_page(pte_page(pteval)); | |
494 | } | |
495 | } | |
496 | ||
497 | static int __collapse_huge_page_isolate(struct vm_area_struct *vma, | |
498 | unsigned long address, | |
499 | pte_t *pte) | |
500 | { | |
501 | struct page *page = NULL; | |
502 | pte_t *_pte; | |
503 | int none_or_zero = 0, result = 0; | |
504 | bool referenced = false, writable = false; | |
505 | ||
506 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; | |
507 | _pte++, address += PAGE_SIZE) { | |
508 | pte_t pteval = *_pte; | |
509 | if (pte_none(pteval) || (pte_present(pteval) && | |
510 | is_zero_pfn(pte_pfn(pteval)))) { | |
511 | if (!userfaultfd_armed(vma) && | |
512 | ++none_or_zero <= khugepaged_max_ptes_none) { | |
513 | continue; | |
514 | } else { | |
515 | result = SCAN_EXCEED_NONE_PTE; | |
516 | goto out; | |
517 | } | |
518 | } | |
519 | if (!pte_present(pteval)) { | |
520 | result = SCAN_PTE_NON_PRESENT; | |
521 | goto out; | |
522 | } | |
523 | page = vm_normal_page(vma, address, pteval); | |
524 | if (unlikely(!page)) { | |
525 | result = SCAN_PAGE_NULL; | |
526 | goto out; | |
527 | } | |
528 | ||
529 | VM_BUG_ON_PAGE(PageCompound(page), page); | |
530 | VM_BUG_ON_PAGE(!PageAnon(page), page); | |
531 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); | |
532 | ||
533 | /* | |
534 | * We can do it before isolate_lru_page because the | |
535 | * page can't be freed from under us. NOTE: PG_lock | |
536 | * is needed to serialize against split_huge_page | |
537 | * when invoked from the VM. | |
538 | */ | |
539 | if (!trylock_page(page)) { | |
540 | result = SCAN_PAGE_LOCK; | |
541 | goto out; | |
542 | } | |
543 | ||
544 | /* | |
545 | * cannot use mapcount: can't collapse if there's a gup pin. | |
546 | * The page must only be referenced by the scanned process | |
547 | * and page swap cache. | |
548 | */ | |
549 | if (page_count(page) != 1 + !!PageSwapCache(page)) { | |
550 | unlock_page(page); | |
551 | result = SCAN_PAGE_COUNT; | |
552 | goto out; | |
553 | } | |
554 | if (pte_write(pteval)) { | |
555 | writable = true; | |
556 | } else { | |
557 | if (PageSwapCache(page) && | |
558 | !reuse_swap_page(page, NULL)) { | |
559 | unlock_page(page); | |
560 | result = SCAN_SWAP_CACHE_PAGE; | |
561 | goto out; | |
562 | } | |
563 | /* | |
564 | * Page is not in the swap cache. It can be collapsed | |
565 | * into a THP. | |
566 | */ | |
567 | } | |
568 | ||
569 | /* | |
570 | * Isolate the page to avoid collapsing an hugepage | |
571 | * currently in use by the VM. | |
572 | */ | |
573 | if (isolate_lru_page(page)) { | |
574 | unlock_page(page); | |
575 | result = SCAN_DEL_PAGE_LRU; | |
576 | goto out; | |
577 | } | |
578 | /* 0 stands for page_is_file_cache(page) == false */ | |
579 | inc_zone_page_state(page, NR_ISOLATED_ANON + 0); | |
580 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
581 | VM_BUG_ON_PAGE(PageLRU(page), page); | |
582 | ||
583 | /* If there is no mapped pte young don't collapse the page */ | |
584 | if (pte_young(pteval) || | |
585 | page_is_young(page) || PageReferenced(page) || | |
586 | mmu_notifier_test_young(vma->vm_mm, address)) | |
587 | referenced = true; | |
588 | } | |
589 | if (likely(writable)) { | |
590 | if (likely(referenced)) { | |
591 | result = SCAN_SUCCEED; | |
592 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, | |
593 | referenced, writable, result); | |
594 | return 1; | |
595 | } | |
596 | } else { | |
597 | result = SCAN_PAGE_RO; | |
598 | } | |
599 | ||
600 | out: | |
601 | release_pte_pages(pte, _pte); | |
602 | trace_mm_collapse_huge_page_isolate(page, none_or_zero, | |
603 | referenced, writable, result); | |
604 | return 0; | |
605 | } | |
606 | ||
607 | static void __collapse_huge_page_copy(pte_t *pte, struct page *page, | |
608 | struct vm_area_struct *vma, | |
609 | unsigned long address, | |
610 | spinlock_t *ptl) | |
611 | { | |
612 | pte_t *_pte; | |
613 | for (_pte = pte; _pte < pte+HPAGE_PMD_NR; _pte++) { | |
614 | pte_t pteval = *_pte; | |
615 | struct page *src_page; | |
616 | ||
617 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { | |
618 | clear_user_highpage(page, address); | |
619 | add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1); | |
620 | if (is_zero_pfn(pte_pfn(pteval))) { | |
621 | /* | |
622 | * ptl mostly unnecessary. | |
623 | */ | |
624 | spin_lock(ptl); | |
625 | /* | |
626 | * paravirt calls inside pte_clear here are | |
627 | * superfluous. | |
628 | */ | |
629 | pte_clear(vma->vm_mm, address, _pte); | |
630 | spin_unlock(ptl); | |
631 | } | |
632 | } else { | |
633 | src_page = pte_page(pteval); | |
634 | copy_user_highpage(page, src_page, address, vma); | |
635 | VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page); | |
636 | release_pte_page(src_page); | |
637 | /* | |
638 | * ptl mostly unnecessary, but preempt has to | |
639 | * be disabled to update the per-cpu stats | |
640 | * inside page_remove_rmap(). | |
641 | */ | |
642 | spin_lock(ptl); | |
643 | /* | |
644 | * paravirt calls inside pte_clear here are | |
645 | * superfluous. | |
646 | */ | |
647 | pte_clear(vma->vm_mm, address, _pte); | |
648 | page_remove_rmap(src_page, false); | |
649 | spin_unlock(ptl); | |
650 | free_page_and_swap_cache(src_page); | |
651 | } | |
652 | ||
653 | address += PAGE_SIZE; | |
654 | page++; | |
655 | } | |
656 | } | |
657 | ||
658 | static void khugepaged_alloc_sleep(void) | |
659 | { | |
660 | DEFINE_WAIT(wait); | |
661 | ||
662 | add_wait_queue(&khugepaged_wait, &wait); | |
663 | freezable_schedule_timeout_interruptible( | |
664 | msecs_to_jiffies(khugepaged_alloc_sleep_millisecs)); | |
665 | remove_wait_queue(&khugepaged_wait, &wait); | |
666 | } | |
667 | ||
668 | static int khugepaged_node_load[MAX_NUMNODES]; | |
669 | ||
670 | static bool khugepaged_scan_abort(int nid) | |
671 | { | |
672 | int i; | |
673 | ||
674 | /* | |
675 | * If zone_reclaim_mode is disabled, then no extra effort is made to | |
676 | * allocate memory locally. | |
677 | */ | |
678 | if (!zone_reclaim_mode) | |
679 | return false; | |
680 | ||
681 | /* If there is a count for this node already, it must be acceptable */ | |
682 | if (khugepaged_node_load[nid]) | |
683 | return false; | |
684 | ||
685 | for (i = 0; i < MAX_NUMNODES; i++) { | |
686 | if (!khugepaged_node_load[i]) | |
687 | continue; | |
688 | if (node_distance(nid, i) > RECLAIM_DISTANCE) | |
689 | return true; | |
690 | } | |
691 | return false; | |
692 | } | |
693 | ||
694 | /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */ | |
695 | static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void) | |
696 | { | |
697 | return GFP_TRANSHUGE | (khugepaged_defrag() ? __GFP_DIRECT_RECLAIM : 0); | |
698 | } | |
699 | ||
700 | #ifdef CONFIG_NUMA | |
701 | static int khugepaged_find_target_node(void) | |
702 | { | |
703 | static int last_khugepaged_target_node = NUMA_NO_NODE; | |
704 | int nid, target_node = 0, max_value = 0; | |
705 | ||
706 | /* find first node with max normal pages hit */ | |
707 | for (nid = 0; nid < MAX_NUMNODES; nid++) | |
708 | if (khugepaged_node_load[nid] > max_value) { | |
709 | max_value = khugepaged_node_load[nid]; | |
710 | target_node = nid; | |
711 | } | |
712 | ||
713 | /* do some balance if several nodes have the same hit record */ | |
714 | if (target_node <= last_khugepaged_target_node) | |
715 | for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES; | |
716 | nid++) | |
717 | if (max_value == khugepaged_node_load[nid]) { | |
718 | target_node = nid; | |
719 | break; | |
720 | } | |
721 | ||
722 | last_khugepaged_target_node = target_node; | |
723 | return target_node; | |
724 | } | |
725 | ||
726 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) | |
727 | { | |
728 | if (IS_ERR(*hpage)) { | |
729 | if (!*wait) | |
730 | return false; | |
731 | ||
732 | *wait = false; | |
733 | *hpage = NULL; | |
734 | khugepaged_alloc_sleep(); | |
735 | } else if (*hpage) { | |
736 | put_page(*hpage); | |
737 | *hpage = NULL; | |
738 | } | |
739 | ||
740 | return true; | |
741 | } | |
742 | ||
743 | static struct page * | |
988ddb71 | 744 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node) |
b46e756f KS |
745 | { |
746 | VM_BUG_ON_PAGE(*hpage, *hpage); | |
747 | ||
b46e756f KS |
748 | *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER); |
749 | if (unlikely(!*hpage)) { | |
750 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); | |
751 | *hpage = ERR_PTR(-ENOMEM); | |
752 | return NULL; | |
753 | } | |
754 | ||
755 | prep_transhuge_page(*hpage); | |
756 | count_vm_event(THP_COLLAPSE_ALLOC); | |
757 | return *hpage; | |
758 | } | |
759 | #else | |
760 | static int khugepaged_find_target_node(void) | |
761 | { | |
762 | return 0; | |
763 | } | |
764 | ||
765 | static inline struct page *alloc_khugepaged_hugepage(void) | |
766 | { | |
767 | struct page *page; | |
768 | ||
769 | page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(), | |
770 | HPAGE_PMD_ORDER); | |
771 | if (page) | |
772 | prep_transhuge_page(page); | |
773 | return page; | |
774 | } | |
775 | ||
776 | static struct page *khugepaged_alloc_hugepage(bool *wait) | |
777 | { | |
778 | struct page *hpage; | |
779 | ||
780 | do { | |
781 | hpage = alloc_khugepaged_hugepage(); | |
782 | if (!hpage) { | |
783 | count_vm_event(THP_COLLAPSE_ALLOC_FAILED); | |
784 | if (!*wait) | |
785 | return NULL; | |
786 | ||
787 | *wait = false; | |
788 | khugepaged_alloc_sleep(); | |
789 | } else | |
790 | count_vm_event(THP_COLLAPSE_ALLOC); | |
791 | } while (unlikely(!hpage) && likely(khugepaged_enabled())); | |
792 | ||
793 | return hpage; | |
794 | } | |
795 | ||
796 | static bool khugepaged_prealloc_page(struct page **hpage, bool *wait) | |
797 | { | |
798 | if (!*hpage) | |
799 | *hpage = khugepaged_alloc_hugepage(wait); | |
800 | ||
801 | if (unlikely(!*hpage)) | |
802 | return false; | |
803 | ||
804 | return true; | |
805 | } | |
806 | ||
807 | static struct page * | |
988ddb71 | 808 | khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node) |
b46e756f | 809 | { |
b46e756f KS |
810 | VM_BUG_ON(!*hpage); |
811 | ||
812 | return *hpage; | |
813 | } | |
814 | #endif | |
815 | ||
816 | static bool hugepage_vma_check(struct vm_area_struct *vma) | |
817 | { | |
818 | if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) || | |
819 | (vma->vm_flags & VM_NOHUGEPAGE)) | |
820 | return false; | |
f3f0e1d2 KS |
821 | if (shmem_file(vma->vm_file)) { |
822 | return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff, | |
823 | HPAGE_PMD_NR); | |
824 | } | |
b46e756f KS |
825 | if (!vma->anon_vma || vma->vm_ops) |
826 | return false; | |
827 | if (is_vma_temporary_stack(vma)) | |
828 | return false; | |
829 | return !(vma->vm_flags & VM_NO_KHUGEPAGED); | |
830 | } | |
831 | ||
832 | /* | |
833 | * If mmap_sem temporarily dropped, revalidate vma | |
834 | * before taking mmap_sem. | |
835 | * Return 0 if succeeds, otherwise return none-zero | |
836 | * value (scan code). | |
837 | */ | |
838 | ||
839 | static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address) | |
840 | { | |
841 | struct vm_area_struct *vma; | |
842 | unsigned long hstart, hend; | |
843 | ||
844 | if (unlikely(khugepaged_test_exit(mm))) | |
845 | return SCAN_ANY_PROCESS; | |
846 | ||
847 | vma = find_vma(mm, address); | |
848 | if (!vma) | |
849 | return SCAN_VMA_NULL; | |
850 | ||
851 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; | |
852 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
853 | if (address < hstart || address + HPAGE_PMD_SIZE > hend) | |
854 | return SCAN_ADDRESS_RANGE; | |
855 | if (!hugepage_vma_check(vma)) | |
856 | return SCAN_VMA_CHECK; | |
857 | return 0; | |
858 | } | |
859 | ||
860 | /* | |
861 | * Bring missing pages in from swap, to complete THP collapse. | |
862 | * Only done if khugepaged_scan_pmd believes it is worthwhile. | |
863 | * | |
864 | * Called and returns without pte mapped or spinlocks held, | |
865 | * but with mmap_sem held to protect against vma changes. | |
866 | */ | |
867 | ||
868 | static bool __collapse_huge_page_swapin(struct mm_struct *mm, | |
869 | struct vm_area_struct *vma, | |
870 | unsigned long address, pmd_t *pmd) | |
871 | { | |
872 | pte_t pteval; | |
873 | int swapped_in = 0, ret = 0; | |
874 | struct fault_env fe = { | |
875 | .vma = vma, | |
876 | .address = address, | |
877 | .flags = FAULT_FLAG_ALLOW_RETRY, | |
878 | .pmd = pmd, | |
879 | }; | |
880 | ||
881 | fe.pte = pte_offset_map(pmd, address); | |
882 | for (; fe.address < address + HPAGE_PMD_NR*PAGE_SIZE; | |
883 | fe.pte++, fe.address += PAGE_SIZE) { | |
884 | pteval = *fe.pte; | |
885 | if (!is_swap_pte(pteval)) | |
886 | continue; | |
887 | swapped_in++; | |
888 | ret = do_swap_page(&fe, pteval); | |
889 | /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */ | |
890 | if (ret & VM_FAULT_RETRY) { | |
891 | down_read(&mm->mmap_sem); | |
892 | /* vma is no longer available, don't continue to swapin */ | |
893 | if (hugepage_vma_revalidate(mm, address)) | |
894 | return false; | |
895 | /* check if the pmd is still valid */ | |
896 | if (mm_find_pmd(mm, address) != pmd) | |
897 | return false; | |
898 | } | |
899 | if (ret & VM_FAULT_ERROR) { | |
900 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, 0); | |
901 | return false; | |
902 | } | |
903 | /* pte is unmapped now, we need to map it */ | |
904 | fe.pte = pte_offset_map(pmd, fe.address); | |
905 | } | |
906 | fe.pte--; | |
907 | pte_unmap(fe.pte); | |
908 | trace_mm_collapse_huge_page_swapin(mm, swapped_in, 1); | |
909 | return true; | |
910 | } | |
911 | ||
912 | static void collapse_huge_page(struct mm_struct *mm, | |
913 | unsigned long address, | |
914 | struct page **hpage, | |
915 | struct vm_area_struct *vma, | |
916 | int node) | |
917 | { | |
918 | pmd_t *pmd, _pmd; | |
919 | pte_t *pte; | |
920 | pgtable_t pgtable; | |
921 | struct page *new_page; | |
922 | spinlock_t *pmd_ptl, *pte_ptl; | |
923 | int isolated = 0, result = 0; | |
924 | struct mem_cgroup *memcg; | |
925 | unsigned long mmun_start; /* For mmu_notifiers */ | |
926 | unsigned long mmun_end; /* For mmu_notifiers */ | |
927 | gfp_t gfp; | |
928 | ||
929 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); | |
930 | ||
931 | /* Only allocate from the target node */ | |
932 | gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_OTHER_NODE | __GFP_THISNODE; | |
933 | ||
988ddb71 KS |
934 | /* |
935 | * Before allocating the hugepage, release the mmap_sem read lock. | |
936 | * The allocation can take potentially a long time if it involves | |
937 | * sync compaction, and we do not need to hold the mmap_sem during | |
938 | * that. We will recheck the vma after taking it again in write mode. | |
939 | */ | |
940 | up_read(&mm->mmap_sem); | |
941 | new_page = khugepaged_alloc_page(hpage, gfp, node); | |
b46e756f KS |
942 | if (!new_page) { |
943 | result = SCAN_ALLOC_HUGE_PAGE_FAIL; | |
944 | goto out_nolock; | |
945 | } | |
946 | ||
947 | if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) { | |
948 | result = SCAN_CGROUP_CHARGE_FAIL; | |
949 | goto out_nolock; | |
950 | } | |
951 | ||
952 | down_read(&mm->mmap_sem); | |
953 | result = hugepage_vma_revalidate(mm, address); | |
954 | if (result) { | |
955 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
956 | up_read(&mm->mmap_sem); | |
957 | goto out_nolock; | |
958 | } | |
959 | ||
960 | pmd = mm_find_pmd(mm, address); | |
961 | if (!pmd) { | |
962 | result = SCAN_PMD_NULL; | |
963 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
964 | up_read(&mm->mmap_sem); | |
965 | goto out_nolock; | |
966 | } | |
967 | ||
968 | /* | |
969 | * __collapse_huge_page_swapin always returns with mmap_sem locked. | |
970 | * If it fails, release mmap_sem and jump directly out. | |
971 | * Continuing to collapse causes inconsistency. | |
972 | */ | |
973 | if (!__collapse_huge_page_swapin(mm, vma, address, pmd)) { | |
974 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
975 | up_read(&mm->mmap_sem); | |
976 | goto out_nolock; | |
977 | } | |
978 | ||
979 | up_read(&mm->mmap_sem); | |
980 | /* | |
981 | * Prevent all access to pagetables with the exception of | |
982 | * gup_fast later handled by the ptep_clear_flush and the VM | |
983 | * handled by the anon_vma lock + PG_lock. | |
984 | */ | |
985 | down_write(&mm->mmap_sem); | |
986 | result = hugepage_vma_revalidate(mm, address); | |
987 | if (result) | |
988 | goto out; | |
989 | /* check if the pmd is still valid */ | |
990 | if (mm_find_pmd(mm, address) != pmd) | |
991 | goto out; | |
992 | ||
993 | anon_vma_lock_write(vma->anon_vma); | |
994 | ||
995 | pte = pte_offset_map(pmd, address); | |
996 | pte_ptl = pte_lockptr(mm, pmd); | |
997 | ||
998 | mmun_start = address; | |
999 | mmun_end = address + HPAGE_PMD_SIZE; | |
1000 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); | |
1001 | pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ | |
1002 | /* | |
1003 | * After this gup_fast can't run anymore. This also removes | |
1004 | * any huge TLB entry from the CPU so we won't allow | |
1005 | * huge and small TLB entries for the same virtual address | |
1006 | * to avoid the risk of CPU bugs in that area. | |
1007 | */ | |
1008 | _pmd = pmdp_collapse_flush(vma, address, pmd); | |
1009 | spin_unlock(pmd_ptl); | |
1010 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); | |
1011 | ||
1012 | spin_lock(pte_ptl); | |
1013 | isolated = __collapse_huge_page_isolate(vma, address, pte); | |
1014 | spin_unlock(pte_ptl); | |
1015 | ||
1016 | if (unlikely(!isolated)) { | |
1017 | pte_unmap(pte); | |
1018 | spin_lock(pmd_ptl); | |
1019 | BUG_ON(!pmd_none(*pmd)); | |
1020 | /* | |
1021 | * We can only use set_pmd_at when establishing | |
1022 | * hugepmds and never for establishing regular pmds that | |
1023 | * points to regular pagetables. Use pmd_populate for that | |
1024 | */ | |
1025 | pmd_populate(mm, pmd, pmd_pgtable(_pmd)); | |
1026 | spin_unlock(pmd_ptl); | |
1027 | anon_vma_unlock_write(vma->anon_vma); | |
1028 | result = SCAN_FAIL; | |
1029 | goto out; | |
1030 | } | |
1031 | ||
1032 | /* | |
1033 | * All pages are isolated and locked so anon_vma rmap | |
1034 | * can't run anymore. | |
1035 | */ | |
1036 | anon_vma_unlock_write(vma->anon_vma); | |
1037 | ||
1038 | __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl); | |
1039 | pte_unmap(pte); | |
1040 | __SetPageUptodate(new_page); | |
1041 | pgtable = pmd_pgtable(_pmd); | |
1042 | ||
1043 | _pmd = mk_huge_pmd(new_page, vma->vm_page_prot); | |
1044 | _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma); | |
1045 | ||
1046 | /* | |
1047 | * spin_lock() below is not the equivalent of smp_wmb(), so | |
1048 | * this is needed to avoid the copy_huge_page writes to become | |
1049 | * visible after the set_pmd_at() write. | |
1050 | */ | |
1051 | smp_wmb(); | |
1052 | ||
1053 | spin_lock(pmd_ptl); | |
1054 | BUG_ON(!pmd_none(*pmd)); | |
1055 | page_add_new_anon_rmap(new_page, vma, address, true); | |
1056 | mem_cgroup_commit_charge(new_page, memcg, false, true); | |
1057 | lru_cache_add_active_or_unevictable(new_page, vma); | |
1058 | pgtable_trans_huge_deposit(mm, pmd, pgtable); | |
1059 | set_pmd_at(mm, address, pmd, _pmd); | |
1060 | update_mmu_cache_pmd(vma, address, pmd); | |
1061 | spin_unlock(pmd_ptl); | |
1062 | ||
1063 | *hpage = NULL; | |
1064 | ||
1065 | khugepaged_pages_collapsed++; | |
1066 | result = SCAN_SUCCEED; | |
1067 | out_up_write: | |
1068 | up_write(&mm->mmap_sem); | |
1069 | out_nolock: | |
1070 | trace_mm_collapse_huge_page(mm, isolated, result); | |
1071 | return; | |
1072 | out: | |
1073 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
1074 | goto out_up_write; | |
1075 | } | |
1076 | ||
1077 | static int khugepaged_scan_pmd(struct mm_struct *mm, | |
1078 | struct vm_area_struct *vma, | |
1079 | unsigned long address, | |
1080 | struct page **hpage) | |
1081 | { | |
1082 | pmd_t *pmd; | |
1083 | pte_t *pte, *_pte; | |
1084 | int ret = 0, none_or_zero = 0, result = 0; | |
1085 | struct page *page = NULL; | |
1086 | unsigned long _address; | |
1087 | spinlock_t *ptl; | |
1088 | int node = NUMA_NO_NODE, unmapped = 0; | |
1089 | bool writable = false, referenced = false; | |
1090 | ||
1091 | VM_BUG_ON(address & ~HPAGE_PMD_MASK); | |
1092 | ||
1093 | pmd = mm_find_pmd(mm, address); | |
1094 | if (!pmd) { | |
1095 | result = SCAN_PMD_NULL; | |
1096 | goto out; | |
1097 | } | |
1098 | ||
1099 | memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load)); | |
1100 | pte = pte_offset_map_lock(mm, pmd, address, &ptl); | |
1101 | for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR; | |
1102 | _pte++, _address += PAGE_SIZE) { | |
1103 | pte_t pteval = *_pte; | |
1104 | if (is_swap_pte(pteval)) { | |
1105 | if (++unmapped <= khugepaged_max_ptes_swap) { | |
1106 | continue; | |
1107 | } else { | |
1108 | result = SCAN_EXCEED_SWAP_PTE; | |
1109 | goto out_unmap; | |
1110 | } | |
1111 | } | |
1112 | if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) { | |
1113 | if (!userfaultfd_armed(vma) && | |
1114 | ++none_or_zero <= khugepaged_max_ptes_none) { | |
1115 | continue; | |
1116 | } else { | |
1117 | result = SCAN_EXCEED_NONE_PTE; | |
1118 | goto out_unmap; | |
1119 | } | |
1120 | } | |
1121 | if (!pte_present(pteval)) { | |
1122 | result = SCAN_PTE_NON_PRESENT; | |
1123 | goto out_unmap; | |
1124 | } | |
1125 | if (pte_write(pteval)) | |
1126 | writable = true; | |
1127 | ||
1128 | page = vm_normal_page(vma, _address, pteval); | |
1129 | if (unlikely(!page)) { | |
1130 | result = SCAN_PAGE_NULL; | |
1131 | goto out_unmap; | |
1132 | } | |
1133 | ||
1134 | /* TODO: teach khugepaged to collapse THP mapped with pte */ | |
1135 | if (PageCompound(page)) { | |
1136 | result = SCAN_PAGE_COMPOUND; | |
1137 | goto out_unmap; | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * Record which node the original page is from and save this | |
1142 | * information to khugepaged_node_load[]. | |
1143 | * Khupaged will allocate hugepage from the node has the max | |
1144 | * hit record. | |
1145 | */ | |
1146 | node = page_to_nid(page); | |
1147 | if (khugepaged_scan_abort(node)) { | |
1148 | result = SCAN_SCAN_ABORT; | |
1149 | goto out_unmap; | |
1150 | } | |
1151 | khugepaged_node_load[node]++; | |
1152 | if (!PageLRU(page)) { | |
1153 | result = SCAN_PAGE_LRU; | |
1154 | goto out_unmap; | |
1155 | } | |
1156 | if (PageLocked(page)) { | |
1157 | result = SCAN_PAGE_LOCK; | |
1158 | goto out_unmap; | |
1159 | } | |
1160 | if (!PageAnon(page)) { | |
1161 | result = SCAN_PAGE_ANON; | |
1162 | goto out_unmap; | |
1163 | } | |
1164 | ||
1165 | /* | |
1166 | * cannot use mapcount: can't collapse if there's a gup pin. | |
1167 | * The page must only be referenced by the scanned process | |
1168 | * and page swap cache. | |
1169 | */ | |
1170 | if (page_count(page) != 1 + !!PageSwapCache(page)) { | |
1171 | result = SCAN_PAGE_COUNT; | |
1172 | goto out_unmap; | |
1173 | } | |
1174 | if (pte_young(pteval) || | |
1175 | page_is_young(page) || PageReferenced(page) || | |
1176 | mmu_notifier_test_young(vma->vm_mm, address)) | |
1177 | referenced = true; | |
1178 | } | |
1179 | if (writable) { | |
1180 | if (referenced) { | |
1181 | result = SCAN_SUCCEED; | |
1182 | ret = 1; | |
1183 | } else { | |
1184 | result = SCAN_NO_REFERENCED_PAGE; | |
1185 | } | |
1186 | } else { | |
1187 | result = SCAN_PAGE_RO; | |
1188 | } | |
1189 | out_unmap: | |
1190 | pte_unmap_unlock(pte, ptl); | |
1191 | if (ret) { | |
1192 | node = khugepaged_find_target_node(); | |
1193 | /* collapse_huge_page will return with the mmap_sem released */ | |
1194 | collapse_huge_page(mm, address, hpage, vma, node); | |
1195 | } | |
1196 | out: | |
1197 | trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced, | |
1198 | none_or_zero, result, unmapped); | |
1199 | return ret; | |
1200 | } | |
1201 | ||
1202 | static void collect_mm_slot(struct mm_slot *mm_slot) | |
1203 | { | |
1204 | struct mm_struct *mm = mm_slot->mm; | |
1205 | ||
1206 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); | |
1207 | ||
1208 | if (khugepaged_test_exit(mm)) { | |
1209 | /* free mm_slot */ | |
1210 | hash_del(&mm_slot->hash); | |
1211 | list_del(&mm_slot->mm_node); | |
1212 | ||
1213 | /* | |
1214 | * Not strictly needed because the mm exited already. | |
1215 | * | |
1216 | * clear_bit(MMF_VM_HUGEPAGE, &mm->flags); | |
1217 | */ | |
1218 | ||
1219 | /* khugepaged_mm_lock actually not necessary for the below */ | |
1220 | free_mm_slot(mm_slot); | |
1221 | mmdrop(mm); | |
1222 | } | |
1223 | } | |
1224 | ||
f3f0e1d2 KS |
1225 | #ifdef CONFIG_SHMEM |
1226 | static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff) | |
1227 | { | |
1228 | struct vm_area_struct *vma; | |
1229 | unsigned long addr; | |
1230 | pmd_t *pmd, _pmd; | |
1231 | ||
1232 | i_mmap_lock_write(mapping); | |
1233 | vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { | |
1234 | /* probably overkill */ | |
1235 | if (vma->anon_vma) | |
1236 | continue; | |
1237 | addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); | |
1238 | if (addr & ~HPAGE_PMD_MASK) | |
1239 | continue; | |
1240 | if (vma->vm_end < addr + HPAGE_PMD_SIZE) | |
1241 | continue; | |
1242 | pmd = mm_find_pmd(vma->vm_mm, addr); | |
1243 | if (!pmd) | |
1244 | continue; | |
1245 | /* | |
1246 | * We need exclusive mmap_sem to retract page table. | |
1247 | * If trylock fails we would end up with pte-mapped THP after | |
1248 | * re-fault. Not ideal, but it's more important to not disturb | |
1249 | * the system too much. | |
1250 | */ | |
1251 | if (down_write_trylock(&vma->vm_mm->mmap_sem)) { | |
1252 | spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd); | |
1253 | /* assume page table is clear */ | |
1254 | _pmd = pmdp_collapse_flush(vma, addr, pmd); | |
1255 | spin_unlock(ptl); | |
1256 | up_write(&vma->vm_mm->mmap_sem); | |
1257 | atomic_long_dec(&vma->vm_mm->nr_ptes); | |
1258 | pte_free(vma->vm_mm, pmd_pgtable(_pmd)); | |
1259 | } | |
1260 | } | |
1261 | i_mmap_unlock_write(mapping); | |
1262 | } | |
1263 | ||
1264 | /** | |
1265 | * collapse_shmem - collapse small tmpfs/shmem pages into huge one. | |
1266 | * | |
1267 | * Basic scheme is simple, details are more complex: | |
1268 | * - allocate and freeze a new huge page; | |
1269 | * - scan over radix tree replacing old pages the new one | |
1270 | * + swap in pages if necessary; | |
1271 | * + fill in gaps; | |
1272 | * + keep old pages around in case if rollback is required; | |
1273 | * - if replacing succeed: | |
1274 | * + copy data over; | |
1275 | * + free old pages; | |
1276 | * + unfreeze huge page; | |
1277 | * - if replacing failed; | |
1278 | * + put all pages back and unfreeze them; | |
1279 | * + restore gaps in the radix-tree; | |
1280 | * + free huge page; | |
1281 | */ | |
1282 | static void collapse_shmem(struct mm_struct *mm, | |
1283 | struct address_space *mapping, pgoff_t start, | |
1284 | struct page **hpage, int node) | |
1285 | { | |
1286 | gfp_t gfp; | |
1287 | struct page *page, *new_page, *tmp; | |
1288 | struct mem_cgroup *memcg; | |
1289 | pgoff_t index, end = start + HPAGE_PMD_NR; | |
1290 | LIST_HEAD(pagelist); | |
1291 | struct radix_tree_iter iter; | |
1292 | void **slot; | |
1293 | int nr_none = 0, result = SCAN_SUCCEED; | |
1294 | ||
1295 | VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); | |
1296 | ||
1297 | /* Only allocate from the target node */ | |
1298 | gfp = alloc_hugepage_khugepaged_gfpmask() | | |
1299 | __GFP_OTHER_NODE | __GFP_THISNODE; | |
1300 | ||
1301 | new_page = khugepaged_alloc_page(hpage, gfp, node); | |
1302 | if (!new_page) { | |
1303 | result = SCAN_ALLOC_HUGE_PAGE_FAIL; | |
1304 | goto out; | |
1305 | } | |
1306 | ||
1307 | if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) { | |
1308 | result = SCAN_CGROUP_CHARGE_FAIL; | |
1309 | goto out; | |
1310 | } | |
1311 | ||
1312 | new_page->index = start; | |
1313 | new_page->mapping = mapping; | |
1314 | __SetPageSwapBacked(new_page); | |
1315 | __SetPageLocked(new_page); | |
1316 | BUG_ON(!page_ref_freeze(new_page, 1)); | |
1317 | ||
1318 | ||
1319 | /* | |
1320 | * At this point the new_page is 'frozen' (page_count() is zero), locked | |
1321 | * and not up-to-date. It's safe to insert it into radix tree, because | |
1322 | * nobody would be able to map it or use it in other way until we | |
1323 | * unfreeze it. | |
1324 | */ | |
1325 | ||
1326 | index = start; | |
1327 | spin_lock_irq(&mapping->tree_lock); | |
1328 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { | |
1329 | int n = min(iter.index, end) - index; | |
1330 | ||
1331 | /* | |
1332 | * Handle holes in the radix tree: charge it from shmem and | |
1333 | * insert relevant subpage of new_page into the radix-tree. | |
1334 | */ | |
1335 | if (n && !shmem_charge(mapping->host, n)) { | |
1336 | result = SCAN_FAIL; | |
1337 | break; | |
1338 | } | |
1339 | nr_none += n; | |
1340 | for (; index < min(iter.index, end); index++) { | |
1341 | radix_tree_insert(&mapping->page_tree, index, | |
1342 | new_page + (index % HPAGE_PMD_NR)); | |
1343 | } | |
1344 | ||
1345 | /* We are done. */ | |
1346 | if (index >= end) | |
1347 | break; | |
1348 | ||
1349 | page = radix_tree_deref_slot_protected(slot, | |
1350 | &mapping->tree_lock); | |
1351 | if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) { | |
1352 | spin_unlock_irq(&mapping->tree_lock); | |
1353 | /* swap in or instantiate fallocated page */ | |
1354 | if (shmem_getpage(mapping->host, index, &page, | |
1355 | SGP_NOHUGE)) { | |
1356 | result = SCAN_FAIL; | |
1357 | goto tree_unlocked; | |
1358 | } | |
1359 | spin_lock_irq(&mapping->tree_lock); | |
1360 | } else if (trylock_page(page)) { | |
1361 | get_page(page); | |
1362 | } else { | |
1363 | result = SCAN_PAGE_LOCK; | |
1364 | break; | |
1365 | } | |
1366 | ||
1367 | /* | |
1368 | * The page must be locked, so we can drop the tree_lock | |
1369 | * without racing with truncate. | |
1370 | */ | |
1371 | VM_BUG_ON_PAGE(!PageLocked(page), page); | |
1372 | VM_BUG_ON_PAGE(!PageUptodate(page), page); | |
1373 | VM_BUG_ON_PAGE(PageTransCompound(page), page); | |
1374 | ||
1375 | if (page_mapping(page) != mapping) { | |
1376 | result = SCAN_TRUNCATED; | |
1377 | goto out_unlock; | |
1378 | } | |
1379 | spin_unlock_irq(&mapping->tree_lock); | |
1380 | ||
1381 | if (isolate_lru_page(page)) { | |
1382 | result = SCAN_DEL_PAGE_LRU; | |
1383 | goto out_isolate_failed; | |
1384 | } | |
1385 | ||
1386 | if (page_mapped(page)) | |
1387 | unmap_mapping_range(mapping, index << PAGE_SHIFT, | |
1388 | PAGE_SIZE, 0); | |
1389 | ||
1390 | spin_lock_irq(&mapping->tree_lock); | |
1391 | ||
1392 | VM_BUG_ON_PAGE(page_mapped(page), page); | |
1393 | ||
1394 | /* | |
1395 | * The page is expected to have page_count() == 3: | |
1396 | * - we hold a pin on it; | |
1397 | * - one reference from radix tree; | |
1398 | * - one from isolate_lru_page; | |
1399 | */ | |
1400 | if (!page_ref_freeze(page, 3)) { | |
1401 | result = SCAN_PAGE_COUNT; | |
1402 | goto out_lru; | |
1403 | } | |
1404 | ||
1405 | /* | |
1406 | * Add the page to the list to be able to undo the collapse if | |
1407 | * something go wrong. | |
1408 | */ | |
1409 | list_add_tail(&page->lru, &pagelist); | |
1410 | ||
1411 | /* Finally, replace with the new page. */ | |
1412 | radix_tree_replace_slot(slot, | |
1413 | new_page + (index % HPAGE_PMD_NR)); | |
1414 | ||
1415 | index++; | |
1416 | continue; | |
1417 | out_lru: | |
1418 | spin_unlock_irq(&mapping->tree_lock); | |
1419 | putback_lru_page(page); | |
1420 | out_isolate_failed: | |
1421 | unlock_page(page); | |
1422 | put_page(page); | |
1423 | goto tree_unlocked; | |
1424 | out_unlock: | |
1425 | unlock_page(page); | |
1426 | put_page(page); | |
1427 | break; | |
1428 | } | |
1429 | ||
1430 | /* | |
1431 | * Handle hole in radix tree at the end of the range. | |
1432 | * This code only triggers if there's nothing in radix tree | |
1433 | * beyond 'end'. | |
1434 | */ | |
1435 | if (result == SCAN_SUCCEED && index < end) { | |
1436 | int n = end - index; | |
1437 | ||
1438 | if (!shmem_charge(mapping->host, n)) { | |
1439 | result = SCAN_FAIL; | |
1440 | goto tree_locked; | |
1441 | } | |
1442 | ||
1443 | for (; index < end; index++) { | |
1444 | radix_tree_insert(&mapping->page_tree, index, | |
1445 | new_page + (index % HPAGE_PMD_NR)); | |
1446 | } | |
1447 | nr_none += n; | |
1448 | } | |
1449 | ||
1450 | tree_locked: | |
1451 | spin_unlock_irq(&mapping->tree_lock); | |
1452 | tree_unlocked: | |
1453 | ||
1454 | if (result == SCAN_SUCCEED) { | |
1455 | unsigned long flags; | |
1456 | struct zone *zone = page_zone(new_page); | |
1457 | ||
1458 | /* | |
1459 | * Replacing old pages with new one has succeed, now we need to | |
1460 | * copy the content and free old pages. | |
1461 | */ | |
1462 | list_for_each_entry_safe(page, tmp, &pagelist, lru) { | |
1463 | copy_highpage(new_page + (page->index % HPAGE_PMD_NR), | |
1464 | page); | |
1465 | list_del(&page->lru); | |
1466 | unlock_page(page); | |
1467 | page_ref_unfreeze(page, 1); | |
1468 | page->mapping = NULL; | |
1469 | ClearPageActive(page); | |
1470 | ClearPageUnevictable(page); | |
1471 | put_page(page); | |
1472 | } | |
1473 | ||
1474 | local_irq_save(flags); | |
1475 | __inc_zone_page_state(new_page, NR_SHMEM_THPS); | |
1476 | if (nr_none) { | |
1477 | __mod_zone_page_state(zone, NR_FILE_PAGES, nr_none); | |
1478 | __mod_zone_page_state(zone, NR_SHMEM, nr_none); | |
1479 | } | |
1480 | local_irq_restore(flags); | |
1481 | ||
1482 | /* | |
1483 | * Remove pte page tables, so we can re-faulti | |
1484 | * the page as huge. | |
1485 | */ | |
1486 | retract_page_tables(mapping, start); | |
1487 | ||
1488 | /* Everything is ready, let's unfreeze the new_page */ | |
1489 | set_page_dirty(new_page); | |
1490 | SetPageUptodate(new_page); | |
1491 | page_ref_unfreeze(new_page, HPAGE_PMD_NR); | |
1492 | mem_cgroup_commit_charge(new_page, memcg, false, true); | |
1493 | lru_cache_add_anon(new_page); | |
1494 | unlock_page(new_page); | |
1495 | ||
1496 | *hpage = NULL; | |
1497 | } else { | |
1498 | /* Something went wrong: rollback changes to the radix-tree */ | |
1499 | shmem_uncharge(mapping->host, nr_none); | |
1500 | spin_lock_irq(&mapping->tree_lock); | |
1501 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, | |
1502 | start) { | |
1503 | if (iter.index >= end) | |
1504 | break; | |
1505 | page = list_first_entry_or_null(&pagelist, | |
1506 | struct page, lru); | |
1507 | if (!page || iter.index < page->index) { | |
1508 | if (!nr_none) | |
1509 | break; | |
1510 | /* Put holes back where they were */ | |
1511 | radix_tree_replace_slot(slot, NULL); | |
1512 | nr_none--; | |
1513 | continue; | |
1514 | } | |
1515 | ||
1516 | VM_BUG_ON_PAGE(page->index != iter.index, page); | |
1517 | ||
1518 | /* Unfreeze the page. */ | |
1519 | list_del(&page->lru); | |
1520 | page_ref_unfreeze(page, 2); | |
1521 | radix_tree_replace_slot(slot, page); | |
1522 | spin_unlock_irq(&mapping->tree_lock); | |
1523 | putback_lru_page(page); | |
1524 | unlock_page(page); | |
1525 | spin_lock_irq(&mapping->tree_lock); | |
1526 | } | |
1527 | VM_BUG_ON(nr_none); | |
1528 | spin_unlock_irq(&mapping->tree_lock); | |
1529 | ||
1530 | /* Unfreeze new_page, caller would take care about freeing it */ | |
1531 | page_ref_unfreeze(new_page, 1); | |
1532 | mem_cgroup_cancel_charge(new_page, memcg, true); | |
1533 | unlock_page(new_page); | |
1534 | new_page->mapping = NULL; | |
1535 | } | |
1536 | out: | |
1537 | VM_BUG_ON(!list_empty(&pagelist)); | |
1538 | /* TODO: tracepoints */ | |
1539 | } | |
1540 | ||
1541 | static void khugepaged_scan_shmem(struct mm_struct *mm, | |
1542 | struct address_space *mapping, | |
1543 | pgoff_t start, struct page **hpage) | |
1544 | { | |
1545 | struct page *page = NULL; | |
1546 | struct radix_tree_iter iter; | |
1547 | void **slot; | |
1548 | int present, swap; | |
1549 | int node = NUMA_NO_NODE; | |
1550 | int result = SCAN_SUCCEED; | |
1551 | ||
1552 | present = 0; | |
1553 | swap = 0; | |
1554 | memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load)); | |
1555 | rcu_read_lock(); | |
1556 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { | |
1557 | if (iter.index >= start + HPAGE_PMD_NR) | |
1558 | break; | |
1559 | ||
1560 | page = radix_tree_deref_slot(slot); | |
1561 | if (radix_tree_deref_retry(page)) { | |
1562 | slot = radix_tree_iter_retry(&iter); | |
1563 | continue; | |
1564 | } | |
1565 | ||
1566 | if (radix_tree_exception(page)) { | |
1567 | if (++swap > khugepaged_max_ptes_swap) { | |
1568 | result = SCAN_EXCEED_SWAP_PTE; | |
1569 | break; | |
1570 | } | |
1571 | continue; | |
1572 | } | |
1573 | ||
1574 | if (PageTransCompound(page)) { | |
1575 | result = SCAN_PAGE_COMPOUND; | |
1576 | break; | |
1577 | } | |
1578 | ||
1579 | node = page_to_nid(page); | |
1580 | if (khugepaged_scan_abort(node)) { | |
1581 | result = SCAN_SCAN_ABORT; | |
1582 | break; | |
1583 | } | |
1584 | khugepaged_node_load[node]++; | |
1585 | ||
1586 | if (!PageLRU(page)) { | |
1587 | result = SCAN_PAGE_LRU; | |
1588 | break; | |
1589 | } | |
1590 | ||
1591 | if (page_count(page) != 1 + page_mapcount(page)) { | |
1592 | result = SCAN_PAGE_COUNT; | |
1593 | break; | |
1594 | } | |
1595 | ||
1596 | /* | |
1597 | * We probably should check if the page is referenced here, but | |
1598 | * nobody would transfer pte_young() to PageReferenced() for us. | |
1599 | * And rmap walk here is just too costly... | |
1600 | */ | |
1601 | ||
1602 | present++; | |
1603 | ||
1604 | if (need_resched()) { | |
1605 | cond_resched_rcu(); | |
1606 | slot = radix_tree_iter_next(&iter); | |
1607 | } | |
1608 | } | |
1609 | rcu_read_unlock(); | |
1610 | ||
1611 | if (result == SCAN_SUCCEED) { | |
1612 | if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) { | |
1613 | result = SCAN_EXCEED_NONE_PTE; | |
1614 | } else { | |
1615 | node = khugepaged_find_target_node(); | |
1616 | collapse_shmem(mm, mapping, start, hpage, node); | |
1617 | } | |
1618 | } | |
1619 | ||
1620 | /* TODO: tracepoints */ | |
1621 | } | |
1622 | #else | |
1623 | static void khugepaged_scan_shmem(struct mm_struct *mm, | |
1624 | struct address_space *mapping, | |
1625 | pgoff_t start, struct page **hpage) | |
1626 | { | |
1627 | BUILD_BUG(); | |
1628 | } | |
1629 | #endif | |
1630 | ||
b46e756f KS |
1631 | static unsigned int khugepaged_scan_mm_slot(unsigned int pages, |
1632 | struct page **hpage) | |
1633 | __releases(&khugepaged_mm_lock) | |
1634 | __acquires(&khugepaged_mm_lock) | |
1635 | { | |
1636 | struct mm_slot *mm_slot; | |
1637 | struct mm_struct *mm; | |
1638 | struct vm_area_struct *vma; | |
1639 | int progress = 0; | |
1640 | ||
1641 | VM_BUG_ON(!pages); | |
1642 | VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock)); | |
1643 | ||
1644 | if (khugepaged_scan.mm_slot) | |
1645 | mm_slot = khugepaged_scan.mm_slot; | |
1646 | else { | |
1647 | mm_slot = list_entry(khugepaged_scan.mm_head.next, | |
1648 | struct mm_slot, mm_node); | |
1649 | khugepaged_scan.address = 0; | |
1650 | khugepaged_scan.mm_slot = mm_slot; | |
1651 | } | |
1652 | spin_unlock(&khugepaged_mm_lock); | |
1653 | ||
1654 | mm = mm_slot->mm; | |
1655 | down_read(&mm->mmap_sem); | |
1656 | if (unlikely(khugepaged_test_exit(mm))) | |
1657 | vma = NULL; | |
1658 | else | |
1659 | vma = find_vma(mm, khugepaged_scan.address); | |
1660 | ||
1661 | progress++; | |
1662 | for (; vma; vma = vma->vm_next) { | |
1663 | unsigned long hstart, hend; | |
1664 | ||
1665 | cond_resched(); | |
1666 | if (unlikely(khugepaged_test_exit(mm))) { | |
1667 | progress++; | |
1668 | break; | |
1669 | } | |
1670 | if (!hugepage_vma_check(vma)) { | |
1671 | skip: | |
1672 | progress++; | |
1673 | continue; | |
1674 | } | |
1675 | hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK; | |
1676 | hend = vma->vm_end & HPAGE_PMD_MASK; | |
1677 | if (hstart >= hend) | |
1678 | goto skip; | |
1679 | if (khugepaged_scan.address > hend) | |
1680 | goto skip; | |
1681 | if (khugepaged_scan.address < hstart) | |
1682 | khugepaged_scan.address = hstart; | |
1683 | VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK); | |
f3f0e1d2 KS |
1684 | if (shmem_file(vma->vm_file) && !shmem_huge_enabled(vma)) |
1685 | goto skip; | |
b46e756f KS |
1686 | |
1687 | while (khugepaged_scan.address < hend) { | |
1688 | int ret; | |
1689 | cond_resched(); | |
1690 | if (unlikely(khugepaged_test_exit(mm))) | |
1691 | goto breakouterloop; | |
1692 | ||
1693 | VM_BUG_ON(khugepaged_scan.address < hstart || | |
1694 | khugepaged_scan.address + HPAGE_PMD_SIZE > | |
1695 | hend); | |
f3f0e1d2 KS |
1696 | if (shmem_file(vma->vm_file)) { |
1697 | struct file *file = get_file(vma->vm_file); | |
1698 | pgoff_t pgoff = linear_page_index(vma, | |
1699 | khugepaged_scan.address); | |
1700 | up_read(&mm->mmap_sem); | |
1701 | ret = 1; | |
1702 | khugepaged_scan_shmem(mm, file->f_mapping, | |
1703 | pgoff, hpage); | |
1704 | fput(file); | |
1705 | } else { | |
1706 | ret = khugepaged_scan_pmd(mm, vma, | |
1707 | khugepaged_scan.address, | |
1708 | hpage); | |
1709 | } | |
b46e756f KS |
1710 | /* move to next address */ |
1711 | khugepaged_scan.address += HPAGE_PMD_SIZE; | |
1712 | progress += HPAGE_PMD_NR; | |
1713 | if (ret) | |
1714 | /* we released mmap_sem so break loop */ | |
1715 | goto breakouterloop_mmap_sem; | |
1716 | if (progress >= pages) | |
1717 | goto breakouterloop; | |
1718 | } | |
1719 | } | |
1720 | breakouterloop: | |
1721 | up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */ | |
1722 | breakouterloop_mmap_sem: | |
1723 | ||
1724 | spin_lock(&khugepaged_mm_lock); | |
1725 | VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot); | |
1726 | /* | |
1727 | * Release the current mm_slot if this mm is about to die, or | |
1728 | * if we scanned all vmas of this mm. | |
1729 | */ | |
1730 | if (khugepaged_test_exit(mm) || !vma) { | |
1731 | /* | |
1732 | * Make sure that if mm_users is reaching zero while | |
1733 | * khugepaged runs here, khugepaged_exit will find | |
1734 | * mm_slot not pointing to the exiting mm. | |
1735 | */ | |
1736 | if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) { | |
1737 | khugepaged_scan.mm_slot = list_entry( | |
1738 | mm_slot->mm_node.next, | |
1739 | struct mm_slot, mm_node); | |
1740 | khugepaged_scan.address = 0; | |
1741 | } else { | |
1742 | khugepaged_scan.mm_slot = NULL; | |
1743 | khugepaged_full_scans++; | |
1744 | } | |
1745 | ||
1746 | collect_mm_slot(mm_slot); | |
1747 | } | |
1748 | ||
1749 | return progress; | |
1750 | } | |
1751 | ||
1752 | static int khugepaged_has_work(void) | |
1753 | { | |
1754 | return !list_empty(&khugepaged_scan.mm_head) && | |
1755 | khugepaged_enabled(); | |
1756 | } | |
1757 | ||
1758 | static int khugepaged_wait_event(void) | |
1759 | { | |
1760 | return !list_empty(&khugepaged_scan.mm_head) || | |
1761 | kthread_should_stop(); | |
1762 | } | |
1763 | ||
1764 | static void khugepaged_do_scan(void) | |
1765 | { | |
1766 | struct page *hpage = NULL; | |
1767 | unsigned int progress = 0, pass_through_head = 0; | |
1768 | unsigned int pages = khugepaged_pages_to_scan; | |
1769 | bool wait = true; | |
1770 | ||
1771 | barrier(); /* write khugepaged_pages_to_scan to local stack */ | |
1772 | ||
1773 | while (progress < pages) { | |
1774 | if (!khugepaged_prealloc_page(&hpage, &wait)) | |
1775 | break; | |
1776 | ||
1777 | cond_resched(); | |
1778 | ||
1779 | if (unlikely(kthread_should_stop() || try_to_freeze())) | |
1780 | break; | |
1781 | ||
1782 | spin_lock(&khugepaged_mm_lock); | |
1783 | if (!khugepaged_scan.mm_slot) | |
1784 | pass_through_head++; | |
1785 | if (khugepaged_has_work() && | |
1786 | pass_through_head < 2) | |
1787 | progress += khugepaged_scan_mm_slot(pages - progress, | |
1788 | &hpage); | |
1789 | else | |
1790 | progress = pages; | |
1791 | spin_unlock(&khugepaged_mm_lock); | |
1792 | } | |
1793 | ||
1794 | if (!IS_ERR_OR_NULL(hpage)) | |
1795 | put_page(hpage); | |
1796 | } | |
1797 | ||
1798 | static bool khugepaged_should_wakeup(void) | |
1799 | { | |
1800 | return kthread_should_stop() || | |
1801 | time_after_eq(jiffies, khugepaged_sleep_expire); | |
1802 | } | |
1803 | ||
1804 | static void khugepaged_wait_work(void) | |
1805 | { | |
1806 | if (khugepaged_has_work()) { | |
1807 | const unsigned long scan_sleep_jiffies = | |
1808 | msecs_to_jiffies(khugepaged_scan_sleep_millisecs); | |
1809 | ||
1810 | if (!scan_sleep_jiffies) | |
1811 | return; | |
1812 | ||
1813 | khugepaged_sleep_expire = jiffies + scan_sleep_jiffies; | |
1814 | wait_event_freezable_timeout(khugepaged_wait, | |
1815 | khugepaged_should_wakeup(), | |
1816 | scan_sleep_jiffies); | |
1817 | return; | |
1818 | } | |
1819 | ||
1820 | if (khugepaged_enabled()) | |
1821 | wait_event_freezable(khugepaged_wait, khugepaged_wait_event()); | |
1822 | } | |
1823 | ||
1824 | static int khugepaged(void *none) | |
1825 | { | |
1826 | struct mm_slot *mm_slot; | |
1827 | ||
1828 | set_freezable(); | |
1829 | set_user_nice(current, MAX_NICE); | |
1830 | ||
1831 | while (!kthread_should_stop()) { | |
1832 | khugepaged_do_scan(); | |
1833 | khugepaged_wait_work(); | |
1834 | } | |
1835 | ||
1836 | spin_lock(&khugepaged_mm_lock); | |
1837 | mm_slot = khugepaged_scan.mm_slot; | |
1838 | khugepaged_scan.mm_slot = NULL; | |
1839 | if (mm_slot) | |
1840 | collect_mm_slot(mm_slot); | |
1841 | spin_unlock(&khugepaged_mm_lock); | |
1842 | return 0; | |
1843 | } | |
1844 | ||
1845 | static void set_recommended_min_free_kbytes(void) | |
1846 | { | |
1847 | struct zone *zone; | |
1848 | int nr_zones = 0; | |
1849 | unsigned long recommended_min; | |
1850 | ||
1851 | for_each_populated_zone(zone) | |
1852 | nr_zones++; | |
1853 | ||
1854 | /* Ensure 2 pageblocks are free to assist fragmentation avoidance */ | |
1855 | recommended_min = pageblock_nr_pages * nr_zones * 2; | |
1856 | ||
1857 | /* | |
1858 | * Make sure that on average at least two pageblocks are almost free | |
1859 | * of another type, one for a migratetype to fall back to and a | |
1860 | * second to avoid subsequent fallbacks of other types There are 3 | |
1861 | * MIGRATE_TYPES we care about. | |
1862 | */ | |
1863 | recommended_min += pageblock_nr_pages * nr_zones * | |
1864 | MIGRATE_PCPTYPES * MIGRATE_PCPTYPES; | |
1865 | ||
1866 | /* don't ever allow to reserve more than 5% of the lowmem */ | |
1867 | recommended_min = min(recommended_min, | |
1868 | (unsigned long) nr_free_buffer_pages() / 20); | |
1869 | recommended_min <<= (PAGE_SHIFT-10); | |
1870 | ||
1871 | if (recommended_min > min_free_kbytes) { | |
1872 | if (user_min_free_kbytes >= 0) | |
1873 | pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n", | |
1874 | min_free_kbytes, recommended_min); | |
1875 | ||
1876 | min_free_kbytes = recommended_min; | |
1877 | } | |
1878 | setup_per_zone_wmarks(); | |
1879 | } | |
1880 | ||
1881 | int start_stop_khugepaged(void) | |
1882 | { | |
1883 | static struct task_struct *khugepaged_thread __read_mostly; | |
1884 | static DEFINE_MUTEX(khugepaged_mutex); | |
1885 | int err = 0; | |
1886 | ||
1887 | mutex_lock(&khugepaged_mutex); | |
1888 | if (khugepaged_enabled()) { | |
1889 | if (!khugepaged_thread) | |
1890 | khugepaged_thread = kthread_run(khugepaged, NULL, | |
1891 | "khugepaged"); | |
1892 | if (IS_ERR(khugepaged_thread)) { | |
1893 | pr_err("khugepaged: kthread_run(khugepaged) failed\n"); | |
1894 | err = PTR_ERR(khugepaged_thread); | |
1895 | khugepaged_thread = NULL; | |
1896 | goto fail; | |
1897 | } | |
1898 | ||
1899 | if (!list_empty(&khugepaged_scan.mm_head)) | |
1900 | wake_up_interruptible(&khugepaged_wait); | |
1901 | ||
1902 | set_recommended_min_free_kbytes(); | |
1903 | } else if (khugepaged_thread) { | |
1904 | kthread_stop(khugepaged_thread); | |
1905 | khugepaged_thread = NULL; | |
1906 | } | |
1907 | fail: | |
1908 | mutex_unlock(&khugepaged_mutex); | |
1909 | return err; | |
1910 | } |