blob: 30553d7df402aff4a1f4ed8593a5d4040016898f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3
4#include <linux/mm.h>
5#include <linux/sched.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +01006#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +01007#include <linux/sched/coredump.h>
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07008#include <linux/mmu_notifier.h>
9#include <linux/rmap.h>
10#include <linux/swap.h>
11#include <linux/mm_inline.h>
12#include <linux/kthread.h>
13#include <linux/khugepaged.h>
14#include <linux/freezer.h>
15#include <linux/mman.h>
16#include <linux/hashtable.h>
17#include <linux/userfaultfd_k.h>
18#include <linux/page_idle.h>
19#include <linux/swapops.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070020#include <linux/shmem_fs.h>
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070021
22#include <asm/tlb.h>
23#include <asm/pgalloc.h>
24#include "internal.h"
25
26enum scan_result {
27 SCAN_FAIL,
28 SCAN_SUCCEED,
29 SCAN_PMD_NULL,
30 SCAN_EXCEED_NONE_PTE,
31 SCAN_PTE_NON_PRESENT,
32 SCAN_PAGE_RO,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -070033 SCAN_LACK_REFERENCED_PAGE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070034 SCAN_PAGE_NULL,
35 SCAN_SCAN_ABORT,
36 SCAN_PAGE_COUNT,
37 SCAN_PAGE_LRU,
38 SCAN_PAGE_LOCK,
39 SCAN_PAGE_ANON,
40 SCAN_PAGE_COMPOUND,
41 SCAN_ANY_PROCESS,
42 SCAN_VMA_NULL,
43 SCAN_VMA_CHECK,
44 SCAN_ADDRESS_RANGE,
45 SCAN_SWAP_CACHE_PAGE,
46 SCAN_DEL_PAGE_LRU,
47 SCAN_ALLOC_HUGE_PAGE_FAIL,
48 SCAN_CGROUP_CHARGE_FAIL,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070049 SCAN_EXCEED_SWAP_PTE,
50 SCAN_TRUNCATED,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070051};
52
53#define CREATE_TRACE_POINTS
54#include <trace/events/huge_memory.h>
55
56/* default scan 8*512 pte (or vmas) every 30 second */
57static unsigned int khugepaged_pages_to_scan __read_mostly;
58static unsigned int khugepaged_pages_collapsed;
59static unsigned int khugepaged_full_scans;
60static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
61/* during fragmentation poll the hugepage allocator once every minute */
62static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
63static unsigned long khugepaged_sleep_expire;
64static DEFINE_SPINLOCK(khugepaged_mm_lock);
65static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
66/*
67 * default collapse hugepages if there is at least one pte mapped like
68 * it would have happened if the vma was large enough during page
69 * fault.
70 */
71static unsigned int khugepaged_max_ptes_none __read_mostly;
72static unsigned int khugepaged_max_ptes_swap __read_mostly;
73
74#define MM_SLOTS_HASH_BITS 10
75static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
76
77static struct kmem_cache *mm_slot_cache __read_mostly;
78
79/**
80 * struct mm_slot - hash lookup from mm to mm_slot
81 * @hash: hash collision list
82 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
83 * @mm: the mm that this information is valid for
84 */
85struct mm_slot {
86 struct hlist_node hash;
87 struct list_head mm_node;
88 struct mm_struct *mm;
89};
90
91/**
92 * struct khugepaged_scan - cursor for scanning
93 * @mm_head: the head of the mm list to scan
94 * @mm_slot: the current mm_slot we are scanning
95 * @address: the next address inside that to be scanned
96 *
97 * There is only the one khugepaged_scan instance of this cursor structure.
98 */
99struct khugepaged_scan {
100 struct list_head mm_head;
101 struct mm_slot *mm_slot;
102 unsigned long address;
103};
104
105static struct khugepaged_scan khugepaged_scan = {
106 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
107};
108
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800109#ifdef CONFIG_SYSFS
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700110static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
111 struct kobj_attribute *attr,
112 char *buf)
113{
114 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
115}
116
117static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
118 struct kobj_attribute *attr,
119 const char *buf, size_t count)
120{
121 unsigned long msecs;
122 int err;
123
124 err = kstrtoul(buf, 10, &msecs);
125 if (err || msecs > UINT_MAX)
126 return -EINVAL;
127
128 khugepaged_scan_sleep_millisecs = msecs;
129 khugepaged_sleep_expire = 0;
130 wake_up_interruptible(&khugepaged_wait);
131
132 return count;
133}
134static struct kobj_attribute scan_sleep_millisecs_attr =
135 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
136 scan_sleep_millisecs_store);
137
138static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
139 struct kobj_attribute *attr,
140 char *buf)
141{
142 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
143}
144
145static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
146 struct kobj_attribute *attr,
147 const char *buf, size_t count)
148{
149 unsigned long msecs;
150 int err;
151
152 err = kstrtoul(buf, 10, &msecs);
153 if (err || msecs > UINT_MAX)
154 return -EINVAL;
155
156 khugepaged_alloc_sleep_millisecs = msecs;
157 khugepaged_sleep_expire = 0;
158 wake_up_interruptible(&khugepaged_wait);
159
160 return count;
161}
162static struct kobj_attribute alloc_sleep_millisecs_attr =
163 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
164 alloc_sleep_millisecs_store);
165
166static ssize_t pages_to_scan_show(struct kobject *kobj,
167 struct kobj_attribute *attr,
168 char *buf)
169{
170 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
171}
172static ssize_t pages_to_scan_store(struct kobject *kobj,
173 struct kobj_attribute *attr,
174 const char *buf, size_t count)
175{
176 int err;
177 unsigned long pages;
178
179 err = kstrtoul(buf, 10, &pages);
180 if (err || !pages || pages > UINT_MAX)
181 return -EINVAL;
182
183 khugepaged_pages_to_scan = pages;
184
185 return count;
186}
187static struct kobj_attribute pages_to_scan_attr =
188 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
189 pages_to_scan_store);
190
191static ssize_t pages_collapsed_show(struct kobject *kobj,
192 struct kobj_attribute *attr,
193 char *buf)
194{
195 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
196}
197static struct kobj_attribute pages_collapsed_attr =
198 __ATTR_RO(pages_collapsed);
199
200static ssize_t full_scans_show(struct kobject *kobj,
201 struct kobj_attribute *attr,
202 char *buf)
203{
204 return sprintf(buf, "%u\n", khugepaged_full_scans);
205}
206static struct kobj_attribute full_scans_attr =
207 __ATTR_RO(full_scans);
208
209static ssize_t khugepaged_defrag_show(struct kobject *kobj,
210 struct kobj_attribute *attr, char *buf)
211{
212 return single_hugepage_flag_show(kobj, attr, buf,
213 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
214}
215static ssize_t khugepaged_defrag_store(struct kobject *kobj,
216 struct kobj_attribute *attr,
217 const char *buf, size_t count)
218{
219 return single_hugepage_flag_store(kobj, attr, buf, count,
220 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
221}
222static struct kobj_attribute khugepaged_defrag_attr =
223 __ATTR(defrag, 0644, khugepaged_defrag_show,
224 khugepaged_defrag_store);
225
226/*
227 * max_ptes_none controls if khugepaged should collapse hugepages over
228 * any unmapped ptes in turn potentially increasing the memory
229 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
230 * reduce the available free memory in the system as it
231 * runs. Increasing max_ptes_none will instead potentially reduce the
232 * free memory in the system during the khugepaged scan.
233 */
234static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
235 struct kobj_attribute *attr,
236 char *buf)
237{
238 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
239}
240static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
241 struct kobj_attribute *attr,
242 const char *buf, size_t count)
243{
244 int err;
245 unsigned long max_ptes_none;
246
247 err = kstrtoul(buf, 10, &max_ptes_none);
248 if (err || max_ptes_none > HPAGE_PMD_NR-1)
249 return -EINVAL;
250
251 khugepaged_max_ptes_none = max_ptes_none;
252
253 return count;
254}
255static struct kobj_attribute khugepaged_max_ptes_none_attr =
256 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
257 khugepaged_max_ptes_none_store);
258
259static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
260 struct kobj_attribute *attr,
261 char *buf)
262{
263 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
264}
265
266static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
267 struct kobj_attribute *attr,
268 const char *buf, size_t count)
269{
270 int err;
271 unsigned long max_ptes_swap;
272
273 err = kstrtoul(buf, 10, &max_ptes_swap);
274 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
275 return -EINVAL;
276
277 khugepaged_max_ptes_swap = max_ptes_swap;
278
279 return count;
280}
281
282static struct kobj_attribute khugepaged_max_ptes_swap_attr =
283 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
284 khugepaged_max_ptes_swap_store);
285
286static struct attribute *khugepaged_attr[] = {
287 &khugepaged_defrag_attr.attr,
288 &khugepaged_max_ptes_none_attr.attr,
289 &pages_to_scan_attr.attr,
290 &pages_collapsed_attr.attr,
291 &full_scans_attr.attr,
292 &scan_sleep_millisecs_attr.attr,
293 &alloc_sleep_millisecs_attr.attr,
294 &khugepaged_max_ptes_swap_attr.attr,
295 NULL,
296};
297
298struct attribute_group khugepaged_attr_group = {
299 .attrs = khugepaged_attr,
300 .name = "khugepaged",
301};
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800302#endif /* CONFIG_SYSFS */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700303
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700304#define VM_NO_KHUGEPAGED (VM_SPECIAL | VM_HUGETLB)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700305
306int hugepage_madvise(struct vm_area_struct *vma,
307 unsigned long *vm_flags, int advice)
308{
309 switch (advice) {
310 case MADV_HUGEPAGE:
311#ifdef CONFIG_S390
312 /*
313 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
314 * can't handle this properly after s390_enable_sie, so we simply
315 * ignore the madvise to prevent qemu from causing a SIGSEGV.
316 */
317 if (mm_has_pgste(vma->vm_mm))
318 return 0;
319#endif
320 *vm_flags &= ~VM_NOHUGEPAGE;
321 *vm_flags |= VM_HUGEPAGE;
322 /*
323 * If the vma become good for khugepaged to scan,
324 * register it here without waiting a page fault that
325 * may not happen any time soon.
326 */
327 if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
328 khugepaged_enter_vma_merge(vma, *vm_flags))
329 return -ENOMEM;
330 break;
331 case MADV_NOHUGEPAGE:
332 *vm_flags &= ~VM_HUGEPAGE;
333 *vm_flags |= VM_NOHUGEPAGE;
334 /*
335 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
336 * this vma even if we leave the mm registered in khugepaged if
337 * it got registered before VM_NOHUGEPAGE was set.
338 */
339 break;
340 }
341
342 return 0;
343}
344
345int __init khugepaged_init(void)
346{
347 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
348 sizeof(struct mm_slot),
349 __alignof__(struct mm_slot), 0, NULL);
350 if (!mm_slot_cache)
351 return -ENOMEM;
352
353 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
354 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
355 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
356
357 return 0;
358}
359
360void __init khugepaged_destroy(void)
361{
362 kmem_cache_destroy(mm_slot_cache);
363}
364
365static inline struct mm_slot *alloc_mm_slot(void)
366{
367 if (!mm_slot_cache) /* initialization failed */
368 return NULL;
369 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
370}
371
372static inline void free_mm_slot(struct mm_slot *mm_slot)
373{
374 kmem_cache_free(mm_slot_cache, mm_slot);
375}
376
377static struct mm_slot *get_mm_slot(struct mm_struct *mm)
378{
379 struct mm_slot *mm_slot;
380
381 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
382 if (mm == mm_slot->mm)
383 return mm_slot;
384
385 return NULL;
386}
387
388static void insert_to_mm_slots_hash(struct mm_struct *mm,
389 struct mm_slot *mm_slot)
390{
391 mm_slot->mm = mm;
392 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
393}
394
395static inline int khugepaged_test_exit(struct mm_struct *mm)
396{
Hugh Dickins17c08ee2020-08-06 23:26:25 -0700397 return atomic_read(&mm->mm_users) == 0 || !mmget_still_valid(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700398}
399
Song Liu50f8b922018-08-17 15:47:00 -0700400static bool hugepage_vma_check(struct vm_area_struct *vma,
401 unsigned long vm_flags)
Yang Shic2231022018-08-17 15:45:26 -0700402{
Song Liu50f8b922018-08-17 15:47:00 -0700403 if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
404 (vm_flags & VM_NOHUGEPAGE) ||
Yang Shic2231022018-08-17 15:45:26 -0700405 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
406 return false;
407 if (shmem_file(vma->vm_file)) {
408 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
409 return false;
410 return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
411 HPAGE_PMD_NR);
412 }
413 if (!vma->anon_vma || vma->vm_ops)
414 return false;
415 if (is_vma_temporary_stack(vma))
416 return false;
Song Liu50f8b922018-08-17 15:47:00 -0700417 return !(vm_flags & VM_NO_KHUGEPAGED);
Yang Shic2231022018-08-17 15:45:26 -0700418}
419
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700420int __khugepaged_enter(struct mm_struct *mm)
421{
422 struct mm_slot *mm_slot;
423 int wakeup;
424
425 mm_slot = alloc_mm_slot();
426 if (!mm_slot)
427 return -ENOMEM;
428
429 /* __khugepaged_exit() must not run from under us */
Hugh Dickins2ef7ebb2020-08-20 17:42:02 -0700430 VM_BUG_ON_MM(atomic_read(&mm->mm_users) == 0, mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700431 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
432 free_mm_slot(mm_slot);
433 return 0;
434 }
435
436 spin_lock(&khugepaged_mm_lock);
437 insert_to_mm_slots_hash(mm, mm_slot);
438 /*
439 * Insert just behind the scanning cursor, to let the area settle
440 * down a little.
441 */
442 wakeup = list_empty(&khugepaged_scan.mm_head);
443 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
444 spin_unlock(&khugepaged_mm_lock);
445
Vegard Nossumf1f10072017-02-27 14:30:07 -0800446 mmgrab(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700447 if (wakeup)
448 wake_up_interruptible(&khugepaged_wait);
449
450 return 0;
451}
452
453int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
454 unsigned long vm_flags)
455{
456 unsigned long hstart, hend;
Yang Shic2231022018-08-17 15:45:26 -0700457
458 /*
459 * khugepaged does not yet work on non-shmem files or special
460 * mappings. And file-private shmem THP is not supported.
461 */
Song Liu50f8b922018-08-17 15:47:00 -0700462 if (!hugepage_vma_check(vma, vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700463 return 0;
Yang Shic2231022018-08-17 15:45:26 -0700464
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700465 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
466 hend = vma->vm_end & HPAGE_PMD_MASK;
467 if (hstart < hend)
468 return khugepaged_enter(vma, vm_flags);
469 return 0;
470}
471
472void __khugepaged_exit(struct mm_struct *mm)
473{
474 struct mm_slot *mm_slot;
475 int free = 0;
476
477 spin_lock(&khugepaged_mm_lock);
478 mm_slot = get_mm_slot(mm);
479 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
480 hash_del(&mm_slot->hash);
481 list_del(&mm_slot->mm_node);
482 free = 1;
483 }
484 spin_unlock(&khugepaged_mm_lock);
485
486 if (free) {
487 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
488 free_mm_slot(mm_slot);
489 mmdrop(mm);
490 } else if (mm_slot) {
491 /*
492 * This is required to serialize against
493 * khugepaged_test_exit() (which is guaranteed to run
494 * under mmap sem read mode). Stop here (after we
495 * return all pagetables will be destroyed) until
496 * khugepaged has finished working on the pagetables
497 * under the mmap_sem.
498 */
499 down_write(&mm->mmap_sem);
500 up_write(&mm->mmap_sem);
501 }
502}
503
504static void release_pte_page(struct page *page)
505{
Shaohua Lid44d3632017-05-03 14:52:26 -0700506 dec_node_page_state(page, NR_ISOLATED_ANON + page_is_file_cache(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700507 unlock_page(page);
508 putback_lru_page(page);
509}
510
511static void release_pte_pages(pte_t *pte, pte_t *_pte)
512{
513 while (--_pte >= pte) {
514 pte_t pteval = *_pte;
515 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
516 release_pte_page(pte_page(pteval));
517 }
518}
519
520static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
521 unsigned long address,
522 pte_t *pte)
523{
524 struct page *page = NULL;
525 pte_t *_pte;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700526 int none_or_zero = 0, result = 0, referenced = 0;
527 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700528
529 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
530 _pte++, address += PAGE_SIZE) {
531 pte_t pteval = *_pte;
532 if (pte_none(pteval) || (pte_present(pteval) &&
533 is_zero_pfn(pte_pfn(pteval)))) {
534 if (!userfaultfd_armed(vma) &&
535 ++none_or_zero <= khugepaged_max_ptes_none) {
536 continue;
537 } else {
538 result = SCAN_EXCEED_NONE_PTE;
539 goto out;
540 }
541 }
542 if (!pte_present(pteval)) {
543 result = SCAN_PTE_NON_PRESENT;
544 goto out;
545 }
546 page = vm_normal_page(vma, address, pteval);
547 if (unlikely(!page)) {
548 result = SCAN_PAGE_NULL;
549 goto out;
550 }
551
Kirill A. Shutemovfece2022018-03-22 16:17:28 -0700552 /* TODO: teach khugepaged to collapse THP mapped with pte */
553 if (PageCompound(page)) {
554 result = SCAN_PAGE_COMPOUND;
555 goto out;
556 }
557
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700558 VM_BUG_ON_PAGE(!PageAnon(page), page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700559
560 /*
561 * We can do it before isolate_lru_page because the
562 * page can't be freed from under us. NOTE: PG_lock
563 * is needed to serialize against split_huge_page
564 * when invoked from the VM.
565 */
566 if (!trylock_page(page)) {
567 result = SCAN_PAGE_LOCK;
568 goto out;
569 }
570
571 /*
572 * cannot use mapcount: can't collapse if there's a gup pin.
573 * The page must only be referenced by the scanned process
574 * and page swap cache.
575 */
Minchan Kim2948be52017-05-03 14:53:35 -0700576 if (page_count(page) != 1 + PageSwapCache(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700577 unlock_page(page);
578 result = SCAN_PAGE_COUNT;
579 goto out;
580 }
581 if (pte_write(pteval)) {
582 writable = true;
583 } else {
584 if (PageSwapCache(page) &&
585 !reuse_swap_page(page, NULL)) {
586 unlock_page(page);
587 result = SCAN_SWAP_CACHE_PAGE;
588 goto out;
589 }
590 /*
591 * Page is not in the swap cache. It can be collapsed
592 * into a THP.
593 */
594 }
595
596 /*
597 * Isolate the page to avoid collapsing an hugepage
598 * currently in use by the VM.
599 */
600 if (isolate_lru_page(page)) {
601 unlock_page(page);
602 result = SCAN_DEL_PAGE_LRU;
603 goto out;
604 }
Shaohua Lid44d3632017-05-03 14:52:26 -0700605 inc_node_page_state(page,
606 NR_ISOLATED_ANON + page_is_file_cache(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700607 VM_BUG_ON_PAGE(!PageLocked(page), page);
608 VM_BUG_ON_PAGE(PageLRU(page), page);
609
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700610 /* There should be enough young pte to collapse the page */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700611 if (pte_young(pteval) ||
612 page_is_young(page) || PageReferenced(page) ||
613 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700614 referenced++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700615 }
616 if (likely(writable)) {
617 if (likely(referenced)) {
618 result = SCAN_SUCCEED;
619 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
620 referenced, writable, result);
621 return 1;
622 }
623 } else {
624 result = SCAN_PAGE_RO;
625 }
626
627out:
628 release_pte_pages(pte, _pte);
629 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
630 referenced, writable, result);
631 return 0;
632}
633
634static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
635 struct vm_area_struct *vma,
636 unsigned long address,
637 spinlock_t *ptl)
638{
639 pte_t *_pte;
David Rientjes338a16b2017-05-12 15:47:03 -0700640 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
641 _pte++, page++, address += PAGE_SIZE) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700642 pte_t pteval = *_pte;
643 struct page *src_page;
644
645 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
646 clear_user_highpage(page, address);
647 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
648 if (is_zero_pfn(pte_pfn(pteval))) {
649 /*
650 * ptl mostly unnecessary.
651 */
652 spin_lock(ptl);
653 /*
654 * paravirt calls inside pte_clear here are
655 * superfluous.
656 */
657 pte_clear(vma->vm_mm, address, _pte);
658 spin_unlock(ptl);
659 }
660 } else {
661 src_page = pte_page(pteval);
662 copy_user_highpage(page, src_page, address, vma);
663 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
664 release_pte_page(src_page);
665 /*
666 * ptl mostly unnecessary, but preempt has to
667 * be disabled to update the per-cpu stats
668 * inside page_remove_rmap().
669 */
670 spin_lock(ptl);
671 /*
672 * paravirt calls inside pte_clear here are
673 * superfluous.
674 */
675 pte_clear(vma->vm_mm, address, _pte);
676 page_remove_rmap(src_page, false);
677 spin_unlock(ptl);
678 free_page_and_swap_cache(src_page);
679 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700680 }
681}
682
683static void khugepaged_alloc_sleep(void)
684{
685 DEFINE_WAIT(wait);
686
687 add_wait_queue(&khugepaged_wait, &wait);
688 freezable_schedule_timeout_interruptible(
689 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
690 remove_wait_queue(&khugepaged_wait, &wait);
691}
692
693static int khugepaged_node_load[MAX_NUMNODES];
694
695static bool khugepaged_scan_abort(int nid)
696{
697 int i;
698
699 /*
Mel Gormana5f5f912016-07-28 15:46:32 -0700700 * If node_reclaim_mode is disabled, then no extra effort is made to
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700701 * allocate memory locally.
702 */
Mel Gormana5f5f912016-07-28 15:46:32 -0700703 if (!node_reclaim_mode)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700704 return false;
705
706 /* If there is a count for this node already, it must be acceptable */
707 if (khugepaged_node_load[nid])
708 return false;
709
710 for (i = 0; i < MAX_NUMNODES; i++) {
711 if (!khugepaged_node_load[i])
712 continue;
713 if (node_distance(nid, i) > RECLAIM_DISTANCE)
714 return true;
715 }
716 return false;
717}
718
719/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
720static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
721{
Vlastimil Babka25160352016-07-28 15:49:25 -0700722 return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700723}
724
725#ifdef CONFIG_NUMA
726static int khugepaged_find_target_node(void)
727{
728 static int last_khugepaged_target_node = NUMA_NO_NODE;
729 int nid, target_node = 0, max_value = 0;
730
731 /* find first node with max normal pages hit */
732 for (nid = 0; nid < MAX_NUMNODES; nid++)
733 if (khugepaged_node_load[nid] > max_value) {
734 max_value = khugepaged_node_load[nid];
735 target_node = nid;
736 }
737
738 /* do some balance if several nodes have the same hit record */
739 if (target_node <= last_khugepaged_target_node)
740 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
741 nid++)
742 if (max_value == khugepaged_node_load[nid]) {
743 target_node = nid;
744 break;
745 }
746
747 last_khugepaged_target_node = target_node;
748 return target_node;
749}
750
751static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
752{
753 if (IS_ERR(*hpage)) {
754 if (!*wait)
755 return false;
756
757 *wait = false;
758 *hpage = NULL;
759 khugepaged_alloc_sleep();
760 } else if (*hpage) {
761 put_page(*hpage);
762 *hpage = NULL;
763 }
764
765 return true;
766}
767
768static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700769khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700770{
771 VM_BUG_ON_PAGE(*hpage, *hpage);
772
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700773 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
774 if (unlikely(!*hpage)) {
775 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
776 *hpage = ERR_PTR(-ENOMEM);
777 return NULL;
778 }
779
780 prep_transhuge_page(*hpage);
781 count_vm_event(THP_COLLAPSE_ALLOC);
782 return *hpage;
783}
784#else
785static int khugepaged_find_target_node(void)
786{
787 return 0;
788}
789
790static inline struct page *alloc_khugepaged_hugepage(void)
791{
792 struct page *page;
793
794 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
795 HPAGE_PMD_ORDER);
796 if (page)
797 prep_transhuge_page(page);
798 return page;
799}
800
801static struct page *khugepaged_alloc_hugepage(bool *wait)
802{
803 struct page *hpage;
804
805 do {
806 hpage = alloc_khugepaged_hugepage();
807 if (!hpage) {
808 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
809 if (!*wait)
810 return NULL;
811
812 *wait = false;
813 khugepaged_alloc_sleep();
814 } else
815 count_vm_event(THP_COLLAPSE_ALLOC);
816 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
817
818 return hpage;
819}
820
821static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
822{
Hugh Dickinsfbe96d52020-10-09 20:07:59 -0700823 /*
824 * If the hpage allocated earlier was briefly exposed in page cache
825 * before collapse_file() failed, it is possible that racing lookups
826 * have not yet completed, and would then be unpleasantly surprised by
827 * finding the hpage reused for the same mapping at a different offset.
828 * Just release the previous allocation if there is any danger of that.
829 */
830 if (*hpage && page_count(*hpage) > 1) {
831 put_page(*hpage);
832 *hpage = NULL;
833 }
834
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700835 if (!*hpage)
836 *hpage = khugepaged_alloc_hugepage(wait);
837
838 if (unlikely(!*hpage))
839 return false;
840
841 return true;
842}
843
844static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700845khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700846{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700847 VM_BUG_ON(!*hpage);
848
849 return *hpage;
850}
851#endif
852
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700853/*
854 * If mmap_sem temporarily dropped, revalidate vma
855 * before taking mmap_sem.
856 * Return 0 if succeeds, otherwise return none-zero
857 * value (scan code).
858 */
859
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700860static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
861 struct vm_area_struct **vmap)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700862{
863 struct vm_area_struct *vma;
864 unsigned long hstart, hend;
865
866 if (unlikely(khugepaged_test_exit(mm)))
867 return SCAN_ANY_PROCESS;
868
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700869 *vmap = vma = find_vma(mm, address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700870 if (!vma)
871 return SCAN_VMA_NULL;
872
873 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
874 hend = vma->vm_end & HPAGE_PMD_MASK;
875 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
876 return SCAN_ADDRESS_RANGE;
Song Liu50f8b922018-08-17 15:47:00 -0700877 if (!hugepage_vma_check(vma, vma->vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700878 return SCAN_VMA_CHECK;
879 return 0;
880}
881
882/*
883 * Bring missing pages in from swap, to complete THP collapse.
884 * Only done if khugepaged_scan_pmd believes it is worthwhile.
885 *
886 * Called and returns without pte mapped or spinlocks held,
887 * but with mmap_sem held to protect against vma changes.
888 */
889
890static bool __collapse_huge_page_swapin(struct mm_struct *mm,
891 struct vm_area_struct *vma,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700892 unsigned long address, pmd_t *pmd,
893 int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700894{
Souptick Joarder2b740302018-08-23 17:01:36 -0700895 int swapped_in = 0;
896 vm_fault_t ret = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800897 struct vm_fault vmf = {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700898 .vma = vma,
899 .address = address,
900 .flags = FAULT_FLAG_ALLOW_RETRY,
901 .pmd = pmd,
Jan Kara0721ec82016-12-14 15:07:04 -0800902 .pgoff = linear_page_index(vma, address),
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700903 };
904
Ebru Akagunduz982785c2016-09-19 14:44:04 -0700905 /* we only decide to swapin, if there is enough young ptes */
906 if (referenced < HPAGE_PMD_NR/2) {
907 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
908 return false;
909 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800910 vmf.pte = pte_offset_map(pmd, address);
911 for (; vmf.address < address + HPAGE_PMD_NR*PAGE_SIZE;
912 vmf.pte++, vmf.address += PAGE_SIZE) {
Jan Kara29943022016-12-14 15:07:16 -0800913 vmf.orig_pte = *vmf.pte;
914 if (!is_swap_pte(vmf.orig_pte))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700915 continue;
916 swapped_in++;
Jan Kara29943022016-12-14 15:07:16 -0800917 ret = do_swap_page(&vmf);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700918
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700919 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
920 if (ret & VM_FAULT_RETRY) {
921 down_read(&mm->mmap_sem);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800922 if (hugepage_vma_revalidate(mm, address, &vmf.vma)) {
Ebru Akagunduz47f863e2016-07-26 15:26:43 -0700923 /* vma is no longer available, don't continue to swapin */
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700924 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700925 return false;
Ebru Akagunduz47f863e2016-07-26 15:26:43 -0700926 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700927 /* check if the pmd is still valid */
SeongJae Park835152a2017-05-12 15:46:38 -0700928 if (mm_find_pmd(mm, address) != pmd) {
929 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700930 return false;
SeongJae Park835152a2017-05-12 15:46:38 -0700931 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700932 }
933 if (ret & VM_FAULT_ERROR) {
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700934 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700935 return false;
936 }
937 /* pte is unmapped now, we need to map it */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800938 vmf.pte = pte_offset_map(pmd, vmf.address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700939 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800940 vmf.pte--;
941 pte_unmap(vmf.pte);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700942 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700943 return true;
944}
945
946static void collapse_huge_page(struct mm_struct *mm,
947 unsigned long address,
948 struct page **hpage,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700949 int node, int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700950{
951 pmd_t *pmd, _pmd;
952 pte_t *pte;
953 pgtable_t pgtable;
954 struct page *new_page;
955 spinlock_t *pmd_ptl, *pte_ptl;
956 int isolated = 0, result = 0;
957 struct mem_cgroup *memcg;
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700958 struct vm_area_struct *vma;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700959 unsigned long mmun_start; /* For mmu_notifiers */
960 unsigned long mmun_end; /* For mmu_notifiers */
961 gfp_t gfp;
962
963 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
964
965 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -0800966 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700967
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700968 /*
969 * Before allocating the hugepage, release the mmap_sem read lock.
970 * The allocation can take potentially a long time if it involves
971 * sync compaction, and we do not need to hold the mmap_sem during
972 * that. We will recheck the vma after taking it again in write mode.
973 */
974 up_read(&mm->mmap_sem);
975 new_page = khugepaged_alloc_page(hpage, gfp, node);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700976 if (!new_page) {
977 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
978 goto out_nolock;
979 }
980
Michal Hocko2a70f6a2018-04-10 16:29:30 -0700981 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700982 result = SCAN_CGROUP_CHARGE_FAIL;
983 goto out_nolock;
984 }
985
986 down_read(&mm->mmap_sem);
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700987 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700988 if (result) {
989 mem_cgroup_cancel_charge(new_page, memcg, true);
990 up_read(&mm->mmap_sem);
991 goto out_nolock;
992 }
993
994 pmd = mm_find_pmd(mm, address);
995 if (!pmd) {
996 result = SCAN_PMD_NULL;
997 mem_cgroup_cancel_charge(new_page, memcg, true);
998 up_read(&mm->mmap_sem);
999 goto out_nolock;
1000 }
1001
1002 /*
1003 * __collapse_huge_page_swapin always returns with mmap_sem locked.
Ebru Akagunduz47f863e2016-07-26 15:26:43 -07001004 * If it fails, we release mmap_sem and jump out_nolock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001005 * Continuing to collapse causes inconsistency.
1006 */
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001007 if (!__collapse_huge_page_swapin(mm, vma, address, pmd, referenced)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001008 mem_cgroup_cancel_charge(new_page, memcg, true);
1009 up_read(&mm->mmap_sem);
1010 goto out_nolock;
1011 }
1012
1013 up_read(&mm->mmap_sem);
1014 /*
1015 * Prevent all access to pagetables with the exception of
1016 * gup_fast later handled by the ptep_clear_flush and the VM
1017 * handled by the anon_vma lock + PG_lock.
1018 */
1019 down_write(&mm->mmap_sem);
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001020 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001021 if (result)
1022 goto out;
1023 /* check if the pmd is still valid */
1024 if (mm_find_pmd(mm, address) != pmd)
1025 goto out;
1026
1027 anon_vma_lock_write(vma->anon_vma);
1028
1029 pte = pte_offset_map(pmd, address);
1030 pte_ptl = pte_lockptr(mm, pmd);
1031
1032 mmun_start = address;
1033 mmun_end = address + HPAGE_PMD_SIZE;
1034 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
1035 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1036 /*
1037 * After this gup_fast can't run anymore. This also removes
1038 * any huge TLB entry from the CPU so we won't allow
1039 * huge and small TLB entries for the same virtual address
1040 * to avoid the risk of CPU bugs in that area.
1041 */
1042 _pmd = pmdp_collapse_flush(vma, address, pmd);
1043 spin_unlock(pmd_ptl);
1044 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
1045
1046 spin_lock(pte_ptl);
1047 isolated = __collapse_huge_page_isolate(vma, address, pte);
1048 spin_unlock(pte_ptl);
1049
1050 if (unlikely(!isolated)) {
1051 pte_unmap(pte);
1052 spin_lock(pmd_ptl);
1053 BUG_ON(!pmd_none(*pmd));
1054 /*
1055 * We can only use set_pmd_at when establishing
1056 * hugepmds and never for establishing regular pmds that
1057 * points to regular pagetables. Use pmd_populate for that
1058 */
1059 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1060 spin_unlock(pmd_ptl);
1061 anon_vma_unlock_write(vma->anon_vma);
1062 result = SCAN_FAIL;
1063 goto out;
1064 }
1065
1066 /*
1067 * All pages are isolated and locked so anon_vma rmap
1068 * can't run anymore.
1069 */
1070 anon_vma_unlock_write(vma->anon_vma);
1071
1072 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
1073 pte_unmap(pte);
1074 __SetPageUptodate(new_page);
1075 pgtable = pmd_pgtable(_pmd);
1076
1077 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001078 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001079
1080 /*
1081 * spin_lock() below is not the equivalent of smp_wmb(), so
1082 * this is needed to avoid the copy_huge_page writes to become
1083 * visible after the set_pmd_at() write.
1084 */
1085 smp_wmb();
1086
1087 spin_lock(pmd_ptl);
1088 BUG_ON(!pmd_none(*pmd));
1089 page_add_new_anon_rmap(new_page, vma, address, true);
1090 mem_cgroup_commit_charge(new_page, memcg, false, true);
1091 lru_cache_add_active_or_unevictable(new_page, vma);
1092 pgtable_trans_huge_deposit(mm, pmd, pgtable);
1093 set_pmd_at(mm, address, pmd, _pmd);
1094 update_mmu_cache_pmd(vma, address, pmd);
1095 spin_unlock(pmd_ptl);
1096
1097 *hpage = NULL;
1098
1099 khugepaged_pages_collapsed++;
1100 result = SCAN_SUCCEED;
1101out_up_write:
1102 up_write(&mm->mmap_sem);
1103out_nolock:
1104 trace_mm_collapse_huge_page(mm, isolated, result);
1105 return;
1106out:
1107 mem_cgroup_cancel_charge(new_page, memcg, true);
1108 goto out_up_write;
1109}
1110
1111static int khugepaged_scan_pmd(struct mm_struct *mm,
1112 struct vm_area_struct *vma,
1113 unsigned long address,
1114 struct page **hpage)
1115{
1116 pmd_t *pmd;
1117 pte_t *pte, *_pte;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001118 int ret = 0, none_or_zero = 0, result = 0, referenced = 0;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001119 struct page *page = NULL;
1120 unsigned long _address;
1121 spinlock_t *ptl;
1122 int node = NUMA_NO_NODE, unmapped = 0;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001123 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001124
1125 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1126
1127 pmd = mm_find_pmd(mm, address);
1128 if (!pmd) {
1129 result = SCAN_PMD_NULL;
1130 goto out;
1131 }
1132
1133 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1134 pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1135 for (_address = address, _pte = pte; _pte < pte+HPAGE_PMD_NR;
1136 _pte++, _address += PAGE_SIZE) {
1137 pte_t pteval = *_pte;
1138 if (is_swap_pte(pteval)) {
1139 if (++unmapped <= khugepaged_max_ptes_swap) {
1140 continue;
1141 } else {
1142 result = SCAN_EXCEED_SWAP_PTE;
1143 goto out_unmap;
1144 }
1145 }
1146 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
1147 if (!userfaultfd_armed(vma) &&
1148 ++none_or_zero <= khugepaged_max_ptes_none) {
1149 continue;
1150 } else {
1151 result = SCAN_EXCEED_NONE_PTE;
1152 goto out_unmap;
1153 }
1154 }
1155 if (!pte_present(pteval)) {
1156 result = SCAN_PTE_NON_PRESENT;
1157 goto out_unmap;
1158 }
1159 if (pte_write(pteval))
1160 writable = true;
1161
1162 page = vm_normal_page(vma, _address, pteval);
1163 if (unlikely(!page)) {
1164 result = SCAN_PAGE_NULL;
1165 goto out_unmap;
1166 }
1167
1168 /* TODO: teach khugepaged to collapse THP mapped with pte */
1169 if (PageCompound(page)) {
1170 result = SCAN_PAGE_COMPOUND;
1171 goto out_unmap;
1172 }
1173
1174 /*
1175 * Record which node the original page is from and save this
1176 * information to khugepaged_node_load[].
1177 * Khupaged will allocate hugepage from the node has the max
1178 * hit record.
1179 */
1180 node = page_to_nid(page);
1181 if (khugepaged_scan_abort(node)) {
1182 result = SCAN_SCAN_ABORT;
1183 goto out_unmap;
1184 }
1185 khugepaged_node_load[node]++;
1186 if (!PageLRU(page)) {
1187 result = SCAN_PAGE_LRU;
1188 goto out_unmap;
1189 }
1190 if (PageLocked(page)) {
1191 result = SCAN_PAGE_LOCK;
1192 goto out_unmap;
1193 }
1194 if (!PageAnon(page)) {
1195 result = SCAN_PAGE_ANON;
1196 goto out_unmap;
1197 }
1198
1199 /*
1200 * cannot use mapcount: can't collapse if there's a gup pin.
1201 * The page must only be referenced by the scanned process
1202 * and page swap cache.
1203 */
Minchan Kim2948be52017-05-03 14:53:35 -07001204 if (page_count(page) != 1 + PageSwapCache(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001205 result = SCAN_PAGE_COUNT;
1206 goto out_unmap;
1207 }
1208 if (pte_young(pteval) ||
1209 page_is_young(page) || PageReferenced(page) ||
1210 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001211 referenced++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001212 }
1213 if (writable) {
1214 if (referenced) {
1215 result = SCAN_SUCCEED;
1216 ret = 1;
1217 } else {
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001218 result = SCAN_LACK_REFERENCED_PAGE;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001219 }
1220 } else {
1221 result = SCAN_PAGE_RO;
1222 }
1223out_unmap:
1224 pte_unmap_unlock(pte, ptl);
1225 if (ret) {
1226 node = khugepaged_find_target_node();
1227 /* collapse_huge_page will return with the mmap_sem released */
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001228 collapse_huge_page(mm, address, hpage, node, referenced);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001229 }
1230out:
1231 trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
1232 none_or_zero, result, unmapped);
1233 return ret;
1234}
1235
1236static void collect_mm_slot(struct mm_slot *mm_slot)
1237{
1238 struct mm_struct *mm = mm_slot->mm;
1239
1240 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1241
1242 if (khugepaged_test_exit(mm)) {
1243 /* free mm_slot */
1244 hash_del(&mm_slot->hash);
1245 list_del(&mm_slot->mm_node);
1246
1247 /*
1248 * Not strictly needed because the mm exited already.
1249 *
1250 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1251 */
1252
1253 /* khugepaged_mm_lock actually not necessary for the below */
1254 free_mm_slot(mm_slot);
1255 mmdrop(mm);
1256 }
1257}
1258
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001259#if defined(CONFIG_SHMEM) && defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001260static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
1261{
1262 struct vm_area_struct *vma;
Hugh Dickins2406c452020-08-06 23:26:22 -07001263 struct mm_struct *mm;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001264 unsigned long addr;
1265 pmd_t *pmd, _pmd;
1266
1267 i_mmap_lock_write(mapping);
1268 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1269 /* probably overkill */
1270 if (vma->anon_vma)
1271 continue;
1272 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
1273 if (addr & ~HPAGE_PMD_MASK)
1274 continue;
1275 if (vma->vm_end < addr + HPAGE_PMD_SIZE)
1276 continue;
Hugh Dickins2406c452020-08-06 23:26:22 -07001277 mm = vma->vm_mm;
1278 pmd = mm_find_pmd(mm, addr);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001279 if (!pmd)
1280 continue;
1281 /*
1282 * We need exclusive mmap_sem to retract page table.
1283 * If trylock fails we would end up with pte-mapped THP after
1284 * re-fault. Not ideal, but it's more important to not disturb
1285 * the system too much.
1286 */
Hugh Dickins2406c452020-08-06 23:26:22 -07001287 if (down_write_trylock(&mm->mmap_sem)) {
1288 if (!khugepaged_test_exit(mm)) {
1289 spinlock_t *ptl = pmd_lock(mm, pmd);
1290 /* assume page table is clear */
1291 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1292 spin_unlock(ptl);
1293 mm_dec_nr_ptes(mm);
1294 pte_free(mm, pmd_pgtable(_pmd));
1295 }
1296 up_write(&mm->mmap_sem);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001297 }
1298 }
1299 i_mmap_unlock_write(mapping);
1300}
1301
1302/**
1303 * collapse_shmem - collapse small tmpfs/shmem pages into huge one.
1304 *
1305 * Basic scheme is simple, details are more complex:
Hugh Dickinsaf24c012018-11-30 14:10:43 -08001306 * - allocate and lock a new huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001307 * - scan over radix tree replacing old pages the new one
1308 * + swap in pages if necessary;
1309 * + fill in gaps;
1310 * + keep old pages around in case if rollback is required;
1311 * - if replacing succeed:
1312 * + copy data over;
1313 * + free old pages;
Hugh Dickinsaf24c012018-11-30 14:10:43 -08001314 * + unlock huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001315 * - if replacing failed;
1316 * + put all pages back and unfreeze them;
1317 * + restore gaps in the radix-tree;
Hugh Dickinsaf24c012018-11-30 14:10:43 -08001318 * + unlock and free huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001319 */
1320static void collapse_shmem(struct mm_struct *mm,
1321 struct address_space *mapping, pgoff_t start,
1322 struct page **hpage, int node)
1323{
1324 gfp_t gfp;
1325 struct page *page, *new_page, *tmp;
1326 struct mem_cgroup *memcg;
1327 pgoff_t index, end = start + HPAGE_PMD_NR;
1328 LIST_HEAD(pagelist);
1329 struct radix_tree_iter iter;
1330 void **slot;
1331 int nr_none = 0, result = SCAN_SUCCEED;
1332
1333 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1334
1335 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -08001336 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001337
1338 new_page = khugepaged_alloc_page(hpage, gfp, node);
1339 if (!new_page) {
1340 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
1341 goto out;
1342 }
1343
Michal Hocko2a70f6a2018-04-10 16:29:30 -07001344 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001345 result = SCAN_CGROUP_CHARGE_FAIL;
1346 goto out;
1347 }
1348
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001349 __SetPageLocked(new_page);
1350 __SetPageSwapBacked(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001351 new_page->index = start;
1352 new_page->mapping = mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001353
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001354 /*
Hugh Dickinsaf24c012018-11-30 14:10:43 -08001355 * At this point the new_page is locked and not up-to-date.
1356 * It's safe to insert it into the page cache, because nobody would
1357 * be able to map it or use it in another way until we unlock it.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001358 */
1359
1360 index = start;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001361 xa_lock_irq(&mapping->i_pages);
1362 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001363 int n = min(iter.index, end) - index;
1364
1365 /*
Hugh Dickins8797f2f2018-11-30 14:10:25 -08001366 * Stop if extent has been hole-punched, and is now completely
1367 * empty (the more obvious i_size_read() check would take an
1368 * irq-unsafe seqlock on 32-bit).
1369 */
1370 if (n >= HPAGE_PMD_NR) {
1371 result = SCAN_TRUNCATED;
1372 goto tree_locked;
1373 }
1374
1375 /*
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001376 * Handle holes in the radix tree: charge it from shmem and
1377 * insert relevant subpage of new_page into the radix-tree.
1378 */
1379 if (n && !shmem_charge(mapping->host, n)) {
1380 result = SCAN_FAIL;
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001381 goto tree_locked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001382 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001383 for (; index < min(iter.index, end); index++) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001384 radix_tree_insert(&mapping->i_pages, index,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001385 new_page + (index % HPAGE_PMD_NR));
1386 }
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001387 nr_none += n;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001388
1389 /* We are done. */
1390 if (index >= end)
1391 break;
1392
1393 page = radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001394 &mapping->i_pages.xa_lock);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001395 if (radix_tree_exceptional_entry(page) || !PageUptodate(page)) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001396 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001397 /* swap in or instantiate fallocated page */
1398 if (shmem_getpage(mapping->host, index, &page,
1399 SGP_NOHUGE)) {
1400 result = SCAN_FAIL;
1401 goto tree_unlocked;
1402 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001403 } else if (trylock_page(page)) {
1404 get_page(page);
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001405 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001406 } else {
1407 result = SCAN_PAGE_LOCK;
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001408 goto tree_locked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001409 }
1410
1411 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001412 * The page must be locked, so we can drop the i_pages lock
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001413 * without racing with truncate.
1414 */
1415 VM_BUG_ON_PAGE(!PageLocked(page), page);
1416 VM_BUG_ON_PAGE(!PageUptodate(page), page);
Hugh Dickins8b37c402018-11-30 14:10:47 -08001417
1418 /*
1419 * If file was truncated then extended, or hole-punched, before
1420 * we locked the first page, then a THP might be there already.
1421 */
1422 if (PageTransCompound(page)) {
1423 result = SCAN_PAGE_COMPOUND;
1424 goto out_unlock;
1425 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001426
1427 if (page_mapping(page) != mapping) {
1428 result = SCAN_TRUNCATED;
1429 goto out_unlock;
1430 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001431
1432 if (isolate_lru_page(page)) {
1433 result = SCAN_DEL_PAGE_LRU;
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001434 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001435 }
1436
1437 if (page_mapped(page))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001438 unmap_mapping_pages(mapping, index, 1, false);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001439
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001440 xa_lock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001441
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001442 slot = radix_tree_lookup_slot(&mapping->i_pages, index);
Johannes Weiner91a45f72016-12-12 16:43:32 -08001443 VM_BUG_ON_PAGE(page != radix_tree_deref_slot_protected(slot,
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001444 &mapping->i_pages.xa_lock), page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001445 VM_BUG_ON_PAGE(page_mapped(page), page);
1446
1447 /*
1448 * The page is expected to have page_count() == 3:
1449 * - we hold a pin on it;
1450 * - one reference from radix tree;
1451 * - one from isolate_lru_page;
1452 */
1453 if (!page_ref_freeze(page, 3)) {
1454 result = SCAN_PAGE_COUNT;
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001455 xa_unlock_irq(&mapping->i_pages);
1456 putback_lru_page(page);
1457 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001458 }
1459
1460 /*
1461 * Add the page to the list to be able to undo the collapse if
1462 * something go wrong.
1463 */
1464 list_add_tail(&page->lru, &pagelist);
1465
1466 /* Finally, replace with the new page. */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001467 radix_tree_replace_slot(&mapping->i_pages, slot,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001468 new_page + (index % HPAGE_PMD_NR));
1469
Matthew Wilcox148deab2016-12-14 15:08:49 -08001470 slot = radix_tree_iter_resume(slot, &iter);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001471 index++;
1472 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001473out_unlock:
1474 unlock_page(page);
1475 put_page(page);
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001476 goto tree_unlocked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001477 }
1478
1479 /*
1480 * Handle hole in radix tree at the end of the range.
1481 * This code only triggers if there's nothing in radix tree
1482 * beyond 'end'.
1483 */
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001484 if (index < end) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001485 int n = end - index;
1486
Hugh Dickins8797f2f2018-11-30 14:10:25 -08001487 /* Stop if extent has been truncated, and is now empty */
1488 if (n >= HPAGE_PMD_NR) {
1489 result = SCAN_TRUNCATED;
1490 goto tree_locked;
1491 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001492 if (!shmem_charge(mapping->host, n)) {
1493 result = SCAN_FAIL;
1494 goto tree_locked;
1495 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001496 for (; index < end; index++) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001497 radix_tree_insert(&mapping->i_pages, index,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001498 new_page + (index % HPAGE_PMD_NR));
1499 }
1500 nr_none += n;
1501 }
1502
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001503 __inc_node_page_state(new_page, NR_SHMEM_THPS);
1504 if (nr_none) {
1505 struct zone *zone = page_zone(new_page);
1506
1507 __mod_node_page_state(zone->zone_pgdat, NR_FILE_PAGES, nr_none);
1508 __mod_node_page_state(zone->zone_pgdat, NR_SHMEM, nr_none);
1509 }
1510
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001511tree_locked:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001512 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001513tree_unlocked:
1514
1515 if (result == SCAN_SUCCEED) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001516 /*
1517 * Replacing old pages with new one has succeed, now we need to
1518 * copy the content and free old pages.
1519 */
Hugh Dickinsee13d692018-11-30 14:10:35 -08001520 index = start;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001521 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
Hugh Dickinsee13d692018-11-30 14:10:35 -08001522 while (index < page->index) {
1523 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1524 index++;
1525 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001526 copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
1527 page);
1528 list_del(&page->lru);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001529 page->mapping = NULL;
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001530 page_ref_unfreeze(page, 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001531 ClearPageActive(page);
1532 ClearPageUnevictable(page);
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001533 unlock_page(page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001534 put_page(page);
Hugh Dickinsee13d692018-11-30 14:10:35 -08001535 index++;
1536 }
1537 while (index < end) {
1538 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1539 index++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001540 }
1541
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001542 SetPageUptodate(new_page);
Hugh Dickinsaf24c012018-11-30 14:10:43 -08001543 page_ref_add(new_page, HPAGE_PMD_NR - 1);
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001544 set_page_dirty(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001545 mem_cgroup_commit_charge(new_page, memcg, false, true);
1546 lru_cache_add_anon(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001547
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001548 /*
1549 * Remove pte page tables, so we can re-fault the page as huge.
1550 */
1551 retract_page_tables(mapping, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001552 *hpage = NULL;
Yang Shi87aa7522018-08-17 15:45:29 -07001553
1554 khugepaged_pages_collapsed++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001555 } else {
1556 /* Something went wrong: rollback changes to the radix-tree */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001557 xa_lock_irq(&mapping->i_pages);
Hugh Dickins78141aa2018-11-30 14:10:29 -08001558 mapping->nrpages -= nr_none;
1559 shmem_uncharge(mapping->host, nr_none);
1560
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001561 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001562 if (iter.index >= end)
1563 break;
1564 page = list_first_entry_or_null(&pagelist,
1565 struct page, lru);
1566 if (!page || iter.index < page->index) {
1567 if (!nr_none)
1568 break;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001569 nr_none--;
Johannes Weiner59749e62016-12-12 16:43:35 -08001570 /* Put holes back where they were */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001571 radix_tree_delete(&mapping->i_pages, iter.index);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001572 continue;
1573 }
1574
1575 VM_BUG_ON_PAGE(page->index != iter.index, page);
1576
1577 /* Unfreeze the page. */
1578 list_del(&page->lru);
1579 page_ref_unfreeze(page, 2);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001580 radix_tree_replace_slot(&mapping->i_pages, slot, page);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001581 slot = radix_tree_iter_resume(slot, &iter);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001582 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001583 unlock_page(page);
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001584 putback_lru_page(page);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001585 xa_lock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001586 }
1587 VM_BUG_ON(nr_none);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001588 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001589
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001590 mem_cgroup_cancel_charge(new_page, memcg, true);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001591 new_page->mapping = NULL;
1592 }
Hugh Dickins3e9646c2018-11-30 14:10:39 -08001593
1594 unlock_page(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001595out:
1596 VM_BUG_ON(!list_empty(&pagelist));
1597 /* TODO: tracepoints */
1598}
1599
1600static void khugepaged_scan_shmem(struct mm_struct *mm,
1601 struct address_space *mapping,
1602 pgoff_t start, struct page **hpage)
1603{
1604 struct page *page = NULL;
1605 struct radix_tree_iter iter;
1606 void **slot;
1607 int present, swap;
1608 int node = NUMA_NO_NODE;
1609 int result = SCAN_SUCCEED;
1610
1611 present = 0;
1612 swap = 0;
1613 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1614 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001615 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001616 if (iter.index >= start + HPAGE_PMD_NR)
1617 break;
1618
1619 page = radix_tree_deref_slot(slot);
1620 if (radix_tree_deref_retry(page)) {
1621 slot = radix_tree_iter_retry(&iter);
1622 continue;
1623 }
1624
1625 if (radix_tree_exception(page)) {
1626 if (++swap > khugepaged_max_ptes_swap) {
1627 result = SCAN_EXCEED_SWAP_PTE;
1628 break;
1629 }
1630 continue;
1631 }
1632
1633 if (PageTransCompound(page)) {
1634 result = SCAN_PAGE_COMPOUND;
1635 break;
1636 }
1637
1638 node = page_to_nid(page);
1639 if (khugepaged_scan_abort(node)) {
1640 result = SCAN_SCAN_ABORT;
1641 break;
1642 }
1643 khugepaged_node_load[node]++;
1644
1645 if (!PageLRU(page)) {
1646 result = SCAN_PAGE_LRU;
1647 break;
1648 }
1649
1650 if (page_count(page) != 1 + page_mapcount(page)) {
1651 result = SCAN_PAGE_COUNT;
1652 break;
1653 }
1654
1655 /*
1656 * We probably should check if the page is referenced here, but
1657 * nobody would transfer pte_young() to PageReferenced() for us.
1658 * And rmap walk here is just too costly...
1659 */
1660
1661 present++;
1662
1663 if (need_resched()) {
Matthew Wilcox148deab2016-12-14 15:08:49 -08001664 slot = radix_tree_iter_resume(slot, &iter);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001665 cond_resched_rcu();
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001666 }
1667 }
1668 rcu_read_unlock();
1669
1670 if (result == SCAN_SUCCEED) {
1671 if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
1672 result = SCAN_EXCEED_NONE_PTE;
1673 } else {
1674 node = khugepaged_find_target_node();
1675 collapse_shmem(mm, mapping, start, hpage, node);
1676 }
1677 }
1678
1679 /* TODO: tracepoints */
1680}
1681#else
1682static void khugepaged_scan_shmem(struct mm_struct *mm,
1683 struct address_space *mapping,
1684 pgoff_t start, struct page **hpage)
1685{
1686 BUILD_BUG();
1687}
1688#endif
1689
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001690static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
1691 struct page **hpage)
1692 __releases(&khugepaged_mm_lock)
1693 __acquires(&khugepaged_mm_lock)
1694{
1695 struct mm_slot *mm_slot;
1696 struct mm_struct *mm;
1697 struct vm_area_struct *vma;
1698 int progress = 0;
1699
1700 VM_BUG_ON(!pages);
1701 VM_BUG_ON(NR_CPUS != 1 && !spin_is_locked(&khugepaged_mm_lock));
1702
1703 if (khugepaged_scan.mm_slot)
1704 mm_slot = khugepaged_scan.mm_slot;
1705 else {
1706 mm_slot = list_entry(khugepaged_scan.mm_head.next,
1707 struct mm_slot, mm_node);
1708 khugepaged_scan.address = 0;
1709 khugepaged_scan.mm_slot = mm_slot;
1710 }
1711 spin_unlock(&khugepaged_mm_lock);
1712
1713 mm = mm_slot->mm;
Yang Shi3b454ad2018-01-31 16:18:28 -08001714 /*
1715 * Don't wait for semaphore (to avoid long wait times). Just move to
1716 * the next mm on the list.
1717 */
1718 vma = NULL;
1719 if (unlikely(!down_read_trylock(&mm->mmap_sem)))
1720 goto breakouterloop_mmap_sem;
1721 if (likely(!khugepaged_test_exit(mm)))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001722 vma = find_vma(mm, khugepaged_scan.address);
1723
1724 progress++;
1725 for (; vma; vma = vma->vm_next) {
1726 unsigned long hstart, hend;
1727
1728 cond_resched();
1729 if (unlikely(khugepaged_test_exit(mm))) {
1730 progress++;
1731 break;
1732 }
Song Liu50f8b922018-08-17 15:47:00 -07001733 if (!hugepage_vma_check(vma, vma->vm_flags)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001734skip:
1735 progress++;
1736 continue;
1737 }
1738 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1739 hend = vma->vm_end & HPAGE_PMD_MASK;
1740 if (hstart >= hend)
1741 goto skip;
1742 if (khugepaged_scan.address > hend)
1743 goto skip;
1744 if (khugepaged_scan.address < hstart)
1745 khugepaged_scan.address = hstart;
1746 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
1747
1748 while (khugepaged_scan.address < hend) {
1749 int ret;
1750 cond_resched();
1751 if (unlikely(khugepaged_test_exit(mm)))
1752 goto breakouterloop;
1753
1754 VM_BUG_ON(khugepaged_scan.address < hstart ||
1755 khugepaged_scan.address + HPAGE_PMD_SIZE >
1756 hend);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001757 if (shmem_file(vma->vm_file)) {
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001758 struct file *file;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001759 pgoff_t pgoff = linear_page_index(vma,
1760 khugepaged_scan.address);
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001761 if (!shmem_huge_enabled(vma))
1762 goto skip;
1763 file = get_file(vma->vm_file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001764 up_read(&mm->mmap_sem);
1765 ret = 1;
1766 khugepaged_scan_shmem(mm, file->f_mapping,
1767 pgoff, hpage);
1768 fput(file);
1769 } else {
1770 ret = khugepaged_scan_pmd(mm, vma,
1771 khugepaged_scan.address,
1772 hpage);
1773 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001774 /* move to next address */
1775 khugepaged_scan.address += HPAGE_PMD_SIZE;
1776 progress += HPAGE_PMD_NR;
1777 if (ret)
1778 /* we released mmap_sem so break loop */
1779 goto breakouterloop_mmap_sem;
1780 if (progress >= pages)
1781 goto breakouterloop;
1782 }
1783 }
1784breakouterloop:
1785 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
1786breakouterloop_mmap_sem:
1787
1788 spin_lock(&khugepaged_mm_lock);
1789 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
1790 /*
1791 * Release the current mm_slot if this mm is about to die, or
1792 * if we scanned all vmas of this mm.
1793 */
1794 if (khugepaged_test_exit(mm) || !vma) {
1795 /*
1796 * Make sure that if mm_users is reaching zero while
1797 * khugepaged runs here, khugepaged_exit will find
1798 * mm_slot not pointing to the exiting mm.
1799 */
1800 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
1801 khugepaged_scan.mm_slot = list_entry(
1802 mm_slot->mm_node.next,
1803 struct mm_slot, mm_node);
1804 khugepaged_scan.address = 0;
1805 } else {
1806 khugepaged_scan.mm_slot = NULL;
1807 khugepaged_full_scans++;
1808 }
1809
1810 collect_mm_slot(mm_slot);
1811 }
1812
1813 return progress;
1814}
1815
1816static int khugepaged_has_work(void)
1817{
1818 return !list_empty(&khugepaged_scan.mm_head) &&
1819 khugepaged_enabled();
1820}
1821
1822static int khugepaged_wait_event(void)
1823{
1824 return !list_empty(&khugepaged_scan.mm_head) ||
1825 kthread_should_stop();
1826}
1827
1828static void khugepaged_do_scan(void)
1829{
1830 struct page *hpage = NULL;
1831 unsigned int progress = 0, pass_through_head = 0;
1832 unsigned int pages = khugepaged_pages_to_scan;
1833 bool wait = true;
1834
1835 barrier(); /* write khugepaged_pages_to_scan to local stack */
1836
1837 while (progress < pages) {
1838 if (!khugepaged_prealloc_page(&hpage, &wait))
1839 break;
1840
1841 cond_resched();
1842
1843 if (unlikely(kthread_should_stop() || try_to_freeze()))
1844 break;
1845
1846 spin_lock(&khugepaged_mm_lock);
1847 if (!khugepaged_scan.mm_slot)
1848 pass_through_head++;
1849 if (khugepaged_has_work() &&
1850 pass_through_head < 2)
1851 progress += khugepaged_scan_mm_slot(pages - progress,
1852 &hpage);
1853 else
1854 progress = pages;
1855 spin_unlock(&khugepaged_mm_lock);
1856 }
1857
1858 if (!IS_ERR_OR_NULL(hpage))
1859 put_page(hpage);
1860}
1861
1862static bool khugepaged_should_wakeup(void)
1863{
1864 return kthread_should_stop() ||
1865 time_after_eq(jiffies, khugepaged_sleep_expire);
1866}
1867
1868static void khugepaged_wait_work(void)
1869{
1870 if (khugepaged_has_work()) {
1871 const unsigned long scan_sleep_jiffies =
1872 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
1873
1874 if (!scan_sleep_jiffies)
1875 return;
1876
1877 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
1878 wait_event_freezable_timeout(khugepaged_wait,
1879 khugepaged_should_wakeup(),
1880 scan_sleep_jiffies);
1881 return;
1882 }
1883
1884 if (khugepaged_enabled())
1885 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
1886}
1887
1888static int khugepaged(void *none)
1889{
1890 struct mm_slot *mm_slot;
1891
1892 set_freezable();
1893 set_user_nice(current, MAX_NICE);
1894
1895 while (!kthread_should_stop()) {
1896 khugepaged_do_scan();
1897 khugepaged_wait_work();
1898 }
1899
1900 spin_lock(&khugepaged_mm_lock);
1901 mm_slot = khugepaged_scan.mm_slot;
1902 khugepaged_scan.mm_slot = NULL;
1903 if (mm_slot)
1904 collect_mm_slot(mm_slot);
1905 spin_unlock(&khugepaged_mm_lock);
1906 return 0;
1907}
1908
1909static void set_recommended_min_free_kbytes(void)
1910{
1911 struct zone *zone;
1912 int nr_zones = 0;
1913 unsigned long recommended_min;
1914
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07001915 for_each_populated_zone(zone) {
1916 /*
1917 * We don't need to worry about fragmentation of
1918 * ZONE_MOVABLE since it only has movable pages.
1919 */
1920 if (zone_idx(zone) > gfp_zone(GFP_USER))
1921 continue;
1922
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001923 nr_zones++;
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07001924 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001925
1926 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
1927 recommended_min = pageblock_nr_pages * nr_zones * 2;
1928
1929 /*
1930 * Make sure that on average at least two pageblocks are almost free
1931 * of another type, one for a migratetype to fall back to and a
1932 * second to avoid subsequent fallbacks of other types There are 3
1933 * MIGRATE_TYPES we care about.
1934 */
1935 recommended_min += pageblock_nr_pages * nr_zones *
1936 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
1937
1938 /* don't ever allow to reserve more than 5% of the lowmem */
1939 recommended_min = min(recommended_min,
1940 (unsigned long) nr_free_buffer_pages() / 20);
1941 recommended_min <<= (PAGE_SHIFT-10);
1942
1943 if (recommended_min > min_free_kbytes) {
1944 if (user_min_free_kbytes >= 0)
1945 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
1946 min_free_kbytes, recommended_min);
1947
1948 min_free_kbytes = recommended_min;
1949 }
1950 setup_per_zone_wmarks();
1951}
1952
1953int start_stop_khugepaged(void)
1954{
1955 static struct task_struct *khugepaged_thread __read_mostly;
1956 static DEFINE_MUTEX(khugepaged_mutex);
1957 int err = 0;
1958
1959 mutex_lock(&khugepaged_mutex);
1960 if (khugepaged_enabled()) {
1961 if (!khugepaged_thread)
1962 khugepaged_thread = kthread_run(khugepaged, NULL,
1963 "khugepaged");
1964 if (IS_ERR(khugepaged_thread)) {
1965 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
1966 err = PTR_ERR(khugepaged_thread);
1967 khugepaged_thread = NULL;
1968 goto fail;
1969 }
1970
1971 if (!list_empty(&khugepaged_scan.mm_head))
1972 wake_up_interruptible(&khugepaged_wait);
1973
1974 set_recommended_min_free_kbytes();
1975 } else if (khugepaged_thread) {
1976 kthread_stop(khugepaged_thread);
1977 khugepaged_thread = NULL;
1978 }
1979fail:
1980 mutex_unlock(&khugepaged_mutex);
1981 return err;
1982}