blob: 3afc1e2d7a55d89a1df729fda47409b13c4a33bd [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,
Song Liu99cb0db2019-09-23 15:38:00 -070051 SCAN_PAGE_HAS_PRIVATE,
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070052};
53
54#define CREATE_TRACE_POINTS
55#include <trace/events/huge_memory.h>
56
57/* default scan 8*512 pte (or vmas) every 30 second */
58static unsigned int khugepaged_pages_to_scan __read_mostly;
59static unsigned int khugepaged_pages_collapsed;
60static unsigned int khugepaged_full_scans;
61static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
62/* during fragmentation poll the hugepage allocator once every minute */
63static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
64static unsigned long khugepaged_sleep_expire;
65static DEFINE_SPINLOCK(khugepaged_mm_lock);
66static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
67/*
68 * default collapse hugepages if there is at least one pte mapped like
69 * it would have happened if the vma was large enough during page
70 * fault.
71 */
72static unsigned int khugepaged_max_ptes_none __read_mostly;
73static unsigned int khugepaged_max_ptes_swap __read_mostly;
74
75#define MM_SLOTS_HASH_BITS 10
76static __read_mostly DEFINE_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
77
78static struct kmem_cache *mm_slot_cache __read_mostly;
79
Song Liu27e1f822019-09-23 15:38:30 -070080#define MAX_PTE_MAPPED_THP 8
81
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070082/**
83 * struct mm_slot - hash lookup from mm to mm_slot
84 * @hash: hash collision list
85 * @mm_node: khugepaged scan list headed in khugepaged_scan.mm_head
86 * @mm: the mm that this information is valid for
87 */
88struct mm_slot {
89 struct hlist_node hash;
90 struct list_head mm_node;
91 struct mm_struct *mm;
Song Liu27e1f822019-09-23 15:38:30 -070092
93 /* pte-mapped THP in this mm */
94 int nr_pte_mapped_thp;
95 unsigned long pte_mapped_thp[MAX_PTE_MAPPED_THP];
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -070096};
97
98/**
99 * struct khugepaged_scan - cursor for scanning
100 * @mm_head: the head of the mm list to scan
101 * @mm_slot: the current mm_slot we are scanning
102 * @address: the next address inside that to be scanned
103 *
104 * There is only the one khugepaged_scan instance of this cursor structure.
105 */
106struct khugepaged_scan {
107 struct list_head mm_head;
108 struct mm_slot *mm_slot;
109 unsigned long address;
110};
111
112static struct khugepaged_scan khugepaged_scan = {
113 .mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
114};
115
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800116#ifdef CONFIG_SYSFS
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700117static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
118 struct kobj_attribute *attr,
119 char *buf)
120{
121 return sprintf(buf, "%u\n", khugepaged_scan_sleep_millisecs);
122}
123
124static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
125 struct kobj_attribute *attr,
126 const char *buf, size_t count)
127{
128 unsigned long msecs;
129 int err;
130
131 err = kstrtoul(buf, 10, &msecs);
132 if (err || msecs > UINT_MAX)
133 return -EINVAL;
134
135 khugepaged_scan_sleep_millisecs = msecs;
136 khugepaged_sleep_expire = 0;
137 wake_up_interruptible(&khugepaged_wait);
138
139 return count;
140}
141static struct kobj_attribute scan_sleep_millisecs_attr =
142 __ATTR(scan_sleep_millisecs, 0644, scan_sleep_millisecs_show,
143 scan_sleep_millisecs_store);
144
145static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
146 struct kobj_attribute *attr,
147 char *buf)
148{
149 return sprintf(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
150}
151
152static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
153 struct kobj_attribute *attr,
154 const char *buf, size_t count)
155{
156 unsigned long msecs;
157 int err;
158
159 err = kstrtoul(buf, 10, &msecs);
160 if (err || msecs > UINT_MAX)
161 return -EINVAL;
162
163 khugepaged_alloc_sleep_millisecs = msecs;
164 khugepaged_sleep_expire = 0;
165 wake_up_interruptible(&khugepaged_wait);
166
167 return count;
168}
169static struct kobj_attribute alloc_sleep_millisecs_attr =
170 __ATTR(alloc_sleep_millisecs, 0644, alloc_sleep_millisecs_show,
171 alloc_sleep_millisecs_store);
172
173static ssize_t pages_to_scan_show(struct kobject *kobj,
174 struct kobj_attribute *attr,
175 char *buf)
176{
177 return sprintf(buf, "%u\n", khugepaged_pages_to_scan);
178}
179static ssize_t pages_to_scan_store(struct kobject *kobj,
180 struct kobj_attribute *attr,
181 const char *buf, size_t count)
182{
183 int err;
184 unsigned long pages;
185
186 err = kstrtoul(buf, 10, &pages);
187 if (err || !pages || pages > UINT_MAX)
188 return -EINVAL;
189
190 khugepaged_pages_to_scan = pages;
191
192 return count;
193}
194static struct kobj_attribute pages_to_scan_attr =
195 __ATTR(pages_to_scan, 0644, pages_to_scan_show,
196 pages_to_scan_store);
197
198static ssize_t pages_collapsed_show(struct kobject *kobj,
199 struct kobj_attribute *attr,
200 char *buf)
201{
202 return sprintf(buf, "%u\n", khugepaged_pages_collapsed);
203}
204static struct kobj_attribute pages_collapsed_attr =
205 __ATTR_RO(pages_collapsed);
206
207static ssize_t full_scans_show(struct kobject *kobj,
208 struct kobj_attribute *attr,
209 char *buf)
210{
211 return sprintf(buf, "%u\n", khugepaged_full_scans);
212}
213static struct kobj_attribute full_scans_attr =
214 __ATTR_RO(full_scans);
215
216static ssize_t khugepaged_defrag_show(struct kobject *kobj,
217 struct kobj_attribute *attr, char *buf)
218{
219 return single_hugepage_flag_show(kobj, attr, buf,
220 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
221}
222static ssize_t khugepaged_defrag_store(struct kobject *kobj,
223 struct kobj_attribute *attr,
224 const char *buf, size_t count)
225{
226 return single_hugepage_flag_store(kobj, attr, buf, count,
227 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
228}
229static struct kobj_attribute khugepaged_defrag_attr =
230 __ATTR(defrag, 0644, khugepaged_defrag_show,
231 khugepaged_defrag_store);
232
233/*
234 * max_ptes_none controls if khugepaged should collapse hugepages over
235 * any unmapped ptes in turn potentially increasing the memory
236 * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
237 * reduce the available free memory in the system as it
238 * runs. Increasing max_ptes_none will instead potentially reduce the
239 * free memory in the system during the khugepaged scan.
240 */
241static ssize_t khugepaged_max_ptes_none_show(struct kobject *kobj,
242 struct kobj_attribute *attr,
243 char *buf)
244{
245 return sprintf(buf, "%u\n", khugepaged_max_ptes_none);
246}
247static ssize_t khugepaged_max_ptes_none_store(struct kobject *kobj,
248 struct kobj_attribute *attr,
249 const char *buf, size_t count)
250{
251 int err;
252 unsigned long max_ptes_none;
253
254 err = kstrtoul(buf, 10, &max_ptes_none);
255 if (err || max_ptes_none > HPAGE_PMD_NR-1)
256 return -EINVAL;
257
258 khugepaged_max_ptes_none = max_ptes_none;
259
260 return count;
261}
262static struct kobj_attribute khugepaged_max_ptes_none_attr =
263 __ATTR(max_ptes_none, 0644, khugepaged_max_ptes_none_show,
264 khugepaged_max_ptes_none_store);
265
266static ssize_t khugepaged_max_ptes_swap_show(struct kobject *kobj,
267 struct kobj_attribute *attr,
268 char *buf)
269{
270 return sprintf(buf, "%u\n", khugepaged_max_ptes_swap);
271}
272
273static ssize_t khugepaged_max_ptes_swap_store(struct kobject *kobj,
274 struct kobj_attribute *attr,
275 const char *buf, size_t count)
276{
277 int err;
278 unsigned long max_ptes_swap;
279
280 err = kstrtoul(buf, 10, &max_ptes_swap);
281 if (err || max_ptes_swap > HPAGE_PMD_NR-1)
282 return -EINVAL;
283
284 khugepaged_max_ptes_swap = max_ptes_swap;
285
286 return count;
287}
288
289static struct kobj_attribute khugepaged_max_ptes_swap_attr =
290 __ATTR(max_ptes_swap, 0644, khugepaged_max_ptes_swap_show,
291 khugepaged_max_ptes_swap_store);
292
293static struct attribute *khugepaged_attr[] = {
294 &khugepaged_defrag_attr.attr,
295 &khugepaged_max_ptes_none_attr.attr,
296 &pages_to_scan_attr.attr,
297 &pages_collapsed_attr.attr,
298 &full_scans_attr.attr,
299 &scan_sleep_millisecs_attr.attr,
300 &alloc_sleep_millisecs_attr.attr,
301 &khugepaged_max_ptes_swap_attr.attr,
302 NULL,
303};
304
305struct attribute_group khugepaged_attr_group = {
306 .attrs = khugepaged_attr,
307 .name = "khugepaged",
308};
Jérémy Lefauree1465d12016-11-30 15:54:02 -0800309#endif /* CONFIG_SYSFS */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700310
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700311int hugepage_madvise(struct vm_area_struct *vma,
312 unsigned long *vm_flags, int advice)
313{
314 switch (advice) {
315 case MADV_HUGEPAGE:
316#ifdef CONFIG_S390
317 /*
318 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
319 * can't handle this properly after s390_enable_sie, so we simply
320 * ignore the madvise to prevent qemu from causing a SIGSEGV.
321 */
322 if (mm_has_pgste(vma->vm_mm))
323 return 0;
324#endif
325 *vm_flags &= ~VM_NOHUGEPAGE;
326 *vm_flags |= VM_HUGEPAGE;
327 /*
328 * If the vma become good for khugepaged to scan,
329 * register it here without waiting a page fault that
330 * may not happen any time soon.
331 */
332 if (!(*vm_flags & VM_NO_KHUGEPAGED) &&
333 khugepaged_enter_vma_merge(vma, *vm_flags))
334 return -ENOMEM;
335 break;
336 case MADV_NOHUGEPAGE:
337 *vm_flags &= ~VM_HUGEPAGE;
338 *vm_flags |= VM_NOHUGEPAGE;
339 /*
340 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
341 * this vma even if we leave the mm registered in khugepaged if
342 * it got registered before VM_NOHUGEPAGE was set.
343 */
344 break;
345 }
346
347 return 0;
348}
349
350int __init khugepaged_init(void)
351{
352 mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
353 sizeof(struct mm_slot),
354 __alignof__(struct mm_slot), 0, NULL);
355 if (!mm_slot_cache)
356 return -ENOMEM;
357
358 khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
359 khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
360 khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
361
362 return 0;
363}
364
365void __init khugepaged_destroy(void)
366{
367 kmem_cache_destroy(mm_slot_cache);
368}
369
370static inline struct mm_slot *alloc_mm_slot(void)
371{
372 if (!mm_slot_cache) /* initialization failed */
373 return NULL;
374 return kmem_cache_zalloc(mm_slot_cache, GFP_KERNEL);
375}
376
377static inline void free_mm_slot(struct mm_slot *mm_slot)
378{
379 kmem_cache_free(mm_slot_cache, mm_slot);
380}
381
382static struct mm_slot *get_mm_slot(struct mm_struct *mm)
383{
384 struct mm_slot *mm_slot;
385
386 hash_for_each_possible(mm_slots_hash, mm_slot, hash, (unsigned long)mm)
387 if (mm == mm_slot->mm)
388 return mm_slot;
389
390 return NULL;
391}
392
393static void insert_to_mm_slots_hash(struct mm_struct *mm,
394 struct mm_slot *mm_slot)
395{
396 mm_slot->mm = mm;
397 hash_add(mm_slots_hash, &mm_slot->hash, (long)mm);
398}
399
400static inline int khugepaged_test_exit(struct mm_struct *mm)
401{
402 return atomic_read(&mm->mm_users) == 0;
403}
404
Song Liu50f8b922018-08-17 15:47:00 -0700405static bool hugepage_vma_check(struct vm_area_struct *vma,
406 unsigned long vm_flags)
Yang Shic2231022018-08-17 15:45:26 -0700407{
Song Liu50f8b922018-08-17 15:47:00 -0700408 if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
409 (vm_flags & VM_NOHUGEPAGE) ||
Yang Shic2231022018-08-17 15:45:26 -0700410 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
411 return false;
Song Liu99cb0db2019-09-23 15:38:00 -0700412
413 if (shmem_file(vma->vm_file) ||
414 (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) &&
415 vma->vm_file &&
416 (vm_flags & VM_DENYWRITE))) {
Yang Shic2231022018-08-17 15:45:26 -0700417 return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
418 HPAGE_PMD_NR);
419 }
420 if (!vma->anon_vma || vma->vm_ops)
421 return false;
Anshuman Khandual222100e2020-04-01 21:07:52 -0700422 if (vma_is_temporary_stack(vma))
Yang Shic2231022018-08-17 15:45:26 -0700423 return false;
Song Liu50f8b922018-08-17 15:47:00 -0700424 return !(vm_flags & VM_NO_KHUGEPAGED);
Yang Shic2231022018-08-17 15:45:26 -0700425}
426
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700427int __khugepaged_enter(struct mm_struct *mm)
428{
429 struct mm_slot *mm_slot;
430 int wakeup;
431
432 mm_slot = alloc_mm_slot();
433 if (!mm_slot)
434 return -ENOMEM;
435
436 /* __khugepaged_exit() must not run from under us */
437 VM_BUG_ON_MM(khugepaged_test_exit(mm), mm);
438 if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags))) {
439 free_mm_slot(mm_slot);
440 return 0;
441 }
442
443 spin_lock(&khugepaged_mm_lock);
444 insert_to_mm_slots_hash(mm, mm_slot);
445 /*
446 * Insert just behind the scanning cursor, to let the area settle
447 * down a little.
448 */
449 wakeup = list_empty(&khugepaged_scan.mm_head);
450 list_add_tail(&mm_slot->mm_node, &khugepaged_scan.mm_head);
451 spin_unlock(&khugepaged_mm_lock);
452
Vegard Nossumf1f10072017-02-27 14:30:07 -0800453 mmgrab(mm);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700454 if (wakeup)
455 wake_up_interruptible(&khugepaged_wait);
456
457 return 0;
458}
459
460int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
461 unsigned long vm_flags)
462{
463 unsigned long hstart, hend;
Yang Shic2231022018-08-17 15:45:26 -0700464
465 /*
Song Liu99cb0db2019-09-23 15:38:00 -0700466 * khugepaged only supports read-only files for non-shmem files.
467 * khugepaged does not yet work on special mappings. And
468 * file-private shmem THP is not supported.
Yang Shic2231022018-08-17 15:45:26 -0700469 */
Song Liu50f8b922018-08-17 15:47:00 -0700470 if (!hugepage_vma_check(vma, vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700471 return 0;
Yang Shic2231022018-08-17 15:45:26 -0700472
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700473 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
474 hend = vma->vm_end & HPAGE_PMD_MASK;
475 if (hstart < hend)
476 return khugepaged_enter(vma, vm_flags);
477 return 0;
478}
479
480void __khugepaged_exit(struct mm_struct *mm)
481{
482 struct mm_slot *mm_slot;
483 int free = 0;
484
485 spin_lock(&khugepaged_mm_lock);
486 mm_slot = get_mm_slot(mm);
487 if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
488 hash_del(&mm_slot->hash);
489 list_del(&mm_slot->mm_node);
490 free = 1;
491 }
492 spin_unlock(&khugepaged_mm_lock);
493
494 if (free) {
495 clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
496 free_mm_slot(mm_slot);
497 mmdrop(mm);
498 } else if (mm_slot) {
499 /*
500 * This is required to serialize against
501 * khugepaged_test_exit() (which is guaranteed to run
502 * under mmap sem read mode). Stop here (after we
503 * return all pagetables will be destroyed) until
504 * khugepaged has finished working on the pagetables
505 * under the mmap_sem.
506 */
507 down_write(&mm->mmap_sem);
508 up_write(&mm->mmap_sem);
509 }
510}
511
512static void release_pte_page(struct page *page)
513{
Huang Ying9de4f222020-04-06 20:04:41 -0700514 dec_node_page_state(page, NR_ISOLATED_ANON + page_is_file_lru(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700515 unlock_page(page);
516 putback_lru_page(page);
517}
518
519static void release_pte_pages(pte_t *pte, pte_t *_pte)
520{
521 while (--_pte >= pte) {
522 pte_t pteval = *_pte;
523 if (!pte_none(pteval) && !is_zero_pfn(pte_pfn(pteval)))
524 release_pte_page(pte_page(pteval));
525 }
526}
527
528static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
529 unsigned long address,
530 pte_t *pte)
531{
532 struct page *page = NULL;
533 pte_t *_pte;
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700534 int none_or_zero = 0, result = 0, referenced = 0;
535 bool writable = false;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700536
537 for (_pte = pte; _pte < pte+HPAGE_PMD_NR;
538 _pte++, address += PAGE_SIZE) {
539 pte_t pteval = *_pte;
540 if (pte_none(pteval) || (pte_present(pteval) &&
541 is_zero_pfn(pte_pfn(pteval)))) {
542 if (!userfaultfd_armed(vma) &&
543 ++none_or_zero <= khugepaged_max_ptes_none) {
544 continue;
545 } else {
546 result = SCAN_EXCEED_NONE_PTE;
547 goto out;
548 }
549 }
550 if (!pte_present(pteval)) {
551 result = SCAN_PTE_NON_PRESENT;
552 goto out;
553 }
554 page = vm_normal_page(vma, address, pteval);
555 if (unlikely(!page)) {
556 result = SCAN_PAGE_NULL;
557 goto out;
558 }
559
Kirill A. Shutemovfece2022018-03-22 16:17:28 -0700560 /* TODO: teach khugepaged to collapse THP mapped with pte */
561 if (PageCompound(page)) {
562 result = SCAN_PAGE_COMPOUND;
563 goto out;
564 }
565
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700566 VM_BUG_ON_PAGE(!PageAnon(page), page);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700567
568 /*
569 * We can do it before isolate_lru_page because the
570 * page can't be freed from under us. NOTE: PG_lock
571 * is needed to serialize against split_huge_page
572 * when invoked from the VM.
573 */
574 if (!trylock_page(page)) {
575 result = SCAN_PAGE_LOCK;
576 goto out;
577 }
578
579 /*
580 * cannot use mapcount: can't collapse if there's a gup pin.
581 * The page must only be referenced by the scanned process
582 * and page swap cache.
583 */
Minchan Kim2948be52017-05-03 14:53:35 -0700584 if (page_count(page) != 1 + PageSwapCache(page)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700585 unlock_page(page);
586 result = SCAN_PAGE_COUNT;
587 goto out;
588 }
589 if (pte_write(pteval)) {
590 writable = true;
591 } else {
592 if (PageSwapCache(page) &&
593 !reuse_swap_page(page, NULL)) {
594 unlock_page(page);
595 result = SCAN_SWAP_CACHE_PAGE;
596 goto out;
597 }
598 /*
599 * Page is not in the swap cache. It can be collapsed
600 * into a THP.
601 */
602 }
603
604 /*
605 * Isolate the page to avoid collapsing an hugepage
606 * currently in use by the VM.
607 */
608 if (isolate_lru_page(page)) {
609 unlock_page(page);
610 result = SCAN_DEL_PAGE_LRU;
611 goto out;
612 }
Shaohua Lid44d3632017-05-03 14:52:26 -0700613 inc_node_page_state(page,
Huang Ying9de4f222020-04-06 20:04:41 -0700614 NR_ISOLATED_ANON + page_is_file_lru(page));
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700615 VM_BUG_ON_PAGE(!PageLocked(page), page);
616 VM_BUG_ON_PAGE(PageLRU(page), page);
617
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700618 /* There should be enough young pte to collapse the page */
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700619 if (pte_young(pteval) ||
620 page_is_young(page) || PageReferenced(page) ||
621 mmu_notifier_test_young(vma->vm_mm, address))
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700622 referenced++;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700623 }
624 if (likely(writable)) {
625 if (likely(referenced)) {
626 result = SCAN_SUCCEED;
627 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
628 referenced, writable, result);
629 return 1;
630 }
631 } else {
632 result = SCAN_PAGE_RO;
633 }
634
635out:
636 release_pte_pages(pte, _pte);
637 trace_mm_collapse_huge_page_isolate(page, none_or_zero,
638 referenced, writable, result);
639 return 0;
640}
641
642static void __collapse_huge_page_copy(pte_t *pte, struct page *page,
643 struct vm_area_struct *vma,
644 unsigned long address,
645 spinlock_t *ptl)
646{
647 pte_t *_pte;
David Rientjes338a16b2017-05-12 15:47:03 -0700648 for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
649 _pte++, page++, address += PAGE_SIZE) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700650 pte_t pteval = *_pte;
651 struct page *src_page;
652
653 if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
654 clear_user_highpage(page, address);
655 add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
656 if (is_zero_pfn(pte_pfn(pteval))) {
657 /*
658 * ptl mostly unnecessary.
659 */
660 spin_lock(ptl);
661 /*
662 * paravirt calls inside pte_clear here are
663 * superfluous.
664 */
665 pte_clear(vma->vm_mm, address, _pte);
666 spin_unlock(ptl);
667 }
668 } else {
669 src_page = pte_page(pteval);
670 copy_user_highpage(page, src_page, address, vma);
671 VM_BUG_ON_PAGE(page_mapcount(src_page) != 1, src_page);
672 release_pte_page(src_page);
673 /*
674 * ptl mostly unnecessary, but preempt has to
675 * be disabled to update the per-cpu stats
676 * inside page_remove_rmap().
677 */
678 spin_lock(ptl);
679 /*
680 * paravirt calls inside pte_clear here are
681 * superfluous.
682 */
683 pte_clear(vma->vm_mm, address, _pte);
684 page_remove_rmap(src_page, false);
685 spin_unlock(ptl);
686 free_page_and_swap_cache(src_page);
687 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700688 }
689}
690
691static void khugepaged_alloc_sleep(void)
692{
693 DEFINE_WAIT(wait);
694
695 add_wait_queue(&khugepaged_wait, &wait);
696 freezable_schedule_timeout_interruptible(
697 msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
698 remove_wait_queue(&khugepaged_wait, &wait);
699}
700
701static int khugepaged_node_load[MAX_NUMNODES];
702
703static bool khugepaged_scan_abort(int nid)
704{
705 int i;
706
707 /*
Mel Gormana5f5f912016-07-28 15:46:32 -0700708 * If node_reclaim_mode is disabled, then no extra effort is made to
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700709 * allocate memory locally.
710 */
Mel Gormana5f5f912016-07-28 15:46:32 -0700711 if (!node_reclaim_mode)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700712 return false;
713
714 /* If there is a count for this node already, it must be acceptable */
715 if (khugepaged_node_load[nid])
716 return false;
717
718 for (i = 0; i < MAX_NUMNODES; i++) {
719 if (!khugepaged_node_load[i])
720 continue;
Matt Fleminga55c7452019-08-08 20:53:01 +0100721 if (node_distance(nid, i) > node_reclaim_distance)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700722 return true;
723 }
724 return false;
725}
726
727/* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
728static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
729{
Vlastimil Babka25160352016-07-28 15:49:25 -0700730 return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700731}
732
733#ifdef CONFIG_NUMA
734static int khugepaged_find_target_node(void)
735{
736 static int last_khugepaged_target_node = NUMA_NO_NODE;
737 int nid, target_node = 0, max_value = 0;
738
739 /* find first node with max normal pages hit */
740 for (nid = 0; nid < MAX_NUMNODES; nid++)
741 if (khugepaged_node_load[nid] > max_value) {
742 max_value = khugepaged_node_load[nid];
743 target_node = nid;
744 }
745
746 /* do some balance if several nodes have the same hit record */
747 if (target_node <= last_khugepaged_target_node)
748 for (nid = last_khugepaged_target_node + 1; nid < MAX_NUMNODES;
749 nid++)
750 if (max_value == khugepaged_node_load[nid]) {
751 target_node = nid;
752 break;
753 }
754
755 last_khugepaged_target_node = target_node;
756 return target_node;
757}
758
759static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
760{
761 if (IS_ERR(*hpage)) {
762 if (!*wait)
763 return false;
764
765 *wait = false;
766 *hpage = NULL;
767 khugepaged_alloc_sleep();
768 } else if (*hpage) {
769 put_page(*hpage);
770 *hpage = NULL;
771 }
772
773 return true;
774}
775
776static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700777khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700778{
779 VM_BUG_ON_PAGE(*hpage, *hpage);
780
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700781 *hpage = __alloc_pages_node(node, gfp, HPAGE_PMD_ORDER);
782 if (unlikely(!*hpage)) {
783 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
784 *hpage = ERR_PTR(-ENOMEM);
785 return NULL;
786 }
787
788 prep_transhuge_page(*hpage);
789 count_vm_event(THP_COLLAPSE_ALLOC);
790 return *hpage;
791}
792#else
793static int khugepaged_find_target_node(void)
794{
795 return 0;
796}
797
798static inline struct page *alloc_khugepaged_hugepage(void)
799{
800 struct page *page;
801
802 page = alloc_pages(alloc_hugepage_khugepaged_gfpmask(),
803 HPAGE_PMD_ORDER);
804 if (page)
805 prep_transhuge_page(page);
806 return page;
807}
808
809static struct page *khugepaged_alloc_hugepage(bool *wait)
810{
811 struct page *hpage;
812
813 do {
814 hpage = alloc_khugepaged_hugepage();
815 if (!hpage) {
816 count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
817 if (!*wait)
818 return NULL;
819
820 *wait = false;
821 khugepaged_alloc_sleep();
822 } else
823 count_vm_event(THP_COLLAPSE_ALLOC);
824 } while (unlikely(!hpage) && likely(khugepaged_enabled()));
825
826 return hpage;
827}
828
829static bool khugepaged_prealloc_page(struct page **hpage, bool *wait)
830{
831 if (!*hpage)
832 *hpage = khugepaged_alloc_hugepage(wait);
833
834 if (unlikely(!*hpage))
835 return false;
836
837 return true;
838}
839
840static struct page *
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700841khugepaged_alloc_page(struct page **hpage, gfp_t gfp, int node)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700842{
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700843 VM_BUG_ON(!*hpage);
844
845 return *hpage;
846}
847#endif
848
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700849/*
850 * If mmap_sem temporarily dropped, revalidate vma
851 * before taking mmap_sem.
852 * Return 0 if succeeds, otherwise return none-zero
853 * value (scan code).
854 */
855
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700856static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
857 struct vm_area_struct **vmap)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700858{
859 struct vm_area_struct *vma;
860 unsigned long hstart, hend;
861
862 if (unlikely(khugepaged_test_exit(mm)))
863 return SCAN_ANY_PROCESS;
864
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700865 *vmap = vma = find_vma(mm, address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700866 if (!vma)
867 return SCAN_VMA_NULL;
868
869 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
870 hend = vma->vm_end & HPAGE_PMD_MASK;
871 if (address < hstart || address + HPAGE_PMD_SIZE > hend)
872 return SCAN_ADDRESS_RANGE;
Song Liu50f8b922018-08-17 15:47:00 -0700873 if (!hugepage_vma_check(vma, vma->vm_flags))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700874 return SCAN_VMA_CHECK;
875 return 0;
876}
877
878/*
879 * Bring missing pages in from swap, to complete THP collapse.
880 * Only done if khugepaged_scan_pmd believes it is worthwhile.
881 *
882 * Called and returns without pte mapped or spinlocks held,
883 * but with mmap_sem held to protect against vma changes.
884 */
885
886static bool __collapse_huge_page_swapin(struct mm_struct *mm,
887 struct vm_area_struct *vma,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700888 unsigned long address, pmd_t *pmd,
889 int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700890{
Souptick Joarder2b740302018-08-23 17:01:36 -0700891 int swapped_in = 0;
892 vm_fault_t ret = 0;
Jan Kara82b0f8c2016-12-14 15:06:58 -0800893 struct vm_fault vmf = {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700894 .vma = vma,
895 .address = address,
896 .flags = FAULT_FLAG_ALLOW_RETRY,
897 .pmd = pmd,
Jan Kara0721ec82016-12-14 15:07:04 -0800898 .pgoff = linear_page_index(vma, address),
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700899 };
900
Ebru Akagunduz982785c2016-09-19 14:44:04 -0700901 /* we only decide to swapin, if there is enough young ptes */
902 if (referenced < HPAGE_PMD_NR/2) {
903 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
904 return false;
905 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800906 vmf.pte = pte_offset_map(pmd, address);
907 for (; vmf.address < address + HPAGE_PMD_NR*PAGE_SIZE;
908 vmf.pte++, vmf.address += PAGE_SIZE) {
Jan Kara29943022016-12-14 15:07:16 -0800909 vmf.orig_pte = *vmf.pte;
910 if (!is_swap_pte(vmf.orig_pte))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700911 continue;
912 swapped_in++;
Jan Kara29943022016-12-14 15:07:16 -0800913 ret = do_swap_page(&vmf);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700914
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700915 /* do_swap_page returns VM_FAULT_RETRY with released mmap_sem */
916 if (ret & VM_FAULT_RETRY) {
917 down_read(&mm->mmap_sem);
Jan Kara82b0f8c2016-12-14 15:06:58 -0800918 if (hugepage_vma_revalidate(mm, address, &vmf.vma)) {
Ebru Akagunduz47f863e2016-07-26 15:26:43 -0700919 /* vma is no longer available, don't continue to swapin */
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700920 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700921 return false;
Ebru Akagunduz47f863e2016-07-26 15:26:43 -0700922 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700923 /* check if the pmd is still valid */
SeongJae Park835152a2017-05-12 15:46:38 -0700924 if (mm_find_pmd(mm, address) != pmd) {
925 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700926 return false;
SeongJae Park835152a2017-05-12 15:46:38 -0700927 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700928 }
929 if (ret & VM_FAULT_ERROR) {
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700930 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 0);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700931 return false;
932 }
933 /* pte is unmapped now, we need to map it */
Jan Kara82b0f8c2016-12-14 15:06:58 -0800934 vmf.pte = pte_offset_map(pmd, vmf.address);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700935 }
Jan Kara82b0f8c2016-12-14 15:06:58 -0800936 vmf.pte--;
937 pte_unmap(vmf.pte);
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700938 trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, 1);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700939 return true;
940}
941
942static void collapse_huge_page(struct mm_struct *mm,
943 unsigned long address,
944 struct page **hpage,
Ebru Akagunduz0db501f2016-07-26 15:26:46 -0700945 int node, int referenced)
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700946{
947 pmd_t *pmd, _pmd;
948 pte_t *pte;
949 pgtable_t pgtable;
950 struct page *new_page;
951 spinlock_t *pmd_ptl, *pte_ptl;
952 int isolated = 0, result = 0;
953 struct mem_cgroup *memcg;
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700954 struct vm_area_struct *vma;
Jérôme Glisseac46d4f2018-12-28 00:38:09 -0800955 struct mmu_notifier_range range;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700956 gfp_t gfp;
957
958 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
959
960 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -0800961 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700962
Kirill A. Shutemov988ddb72016-07-26 15:26:26 -0700963 /*
964 * Before allocating the hugepage, release the mmap_sem read lock.
965 * The allocation can take potentially a long time if it involves
966 * sync compaction, and we do not need to hold the mmap_sem during
967 * that. We will recheck the vma after taking it again in write mode.
968 */
969 up_read(&mm->mmap_sem);
970 new_page = khugepaged_alloc_page(hpage, gfp, node);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700971 if (!new_page) {
972 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
973 goto out_nolock;
974 }
975
Michal Hocko2a70f6a2018-04-10 16:29:30 -0700976 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700977 result = SCAN_CGROUP_CHARGE_FAIL;
978 goto out_nolock;
979 }
980
981 down_read(&mm->mmap_sem);
Kirill A. Shutemovc131f752016-09-19 14:44:01 -0700982 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -0700983 if (result) {
984 mem_cgroup_cancel_charge(new_page, memcg, true);
985 up_read(&mm->mmap_sem);
986 goto out_nolock;
987 }
988
989 pmd = mm_find_pmd(mm, address);
990 if (!pmd) {
991 result = SCAN_PMD_NULL;
992 mem_cgroup_cancel_charge(new_page, memcg, true);
993 up_read(&mm->mmap_sem);
994 goto out_nolock;
995 }
996
997 /*
998 * __collapse_huge_page_swapin always returns with mmap_sem locked.
Ebru Akagunduz47f863e2016-07-26 15:26:43 -0700999 * If it fails, we release mmap_sem and jump out_nolock.
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001000 * Continuing to collapse causes inconsistency.
1001 */
Ebru Akagunduz0db501f2016-07-26 15:26:46 -07001002 if (!__collapse_huge_page_swapin(mm, vma, address, pmd, referenced)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001003 mem_cgroup_cancel_charge(new_page, memcg, true);
1004 up_read(&mm->mmap_sem);
1005 goto out_nolock;
1006 }
1007
1008 up_read(&mm->mmap_sem);
1009 /*
1010 * Prevent all access to pagetables with the exception of
1011 * gup_fast later handled by the ptep_clear_flush and the VM
1012 * handled by the anon_vma lock + PG_lock.
1013 */
1014 down_write(&mm->mmap_sem);
Andrea Arcangeli59ea6d02019-06-13 15:56:11 -07001015 result = SCAN_ANY_PROCESS;
1016 if (!mmget_still_valid(mm))
1017 goto out;
Kirill A. Shutemovc131f752016-09-19 14:44:01 -07001018 result = hugepage_vma_revalidate(mm, address, &vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001019 if (result)
1020 goto out;
1021 /* check if the pmd is still valid */
1022 if (mm_find_pmd(mm, address) != pmd)
1023 goto out;
1024
1025 anon_vma_lock_write(vma->anon_vma);
1026
Jérôme Glisse7269f992019-05-13 17:20:53 -07001027 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, NULL, mm,
Jérôme Glisse6f4f13e2019-05-13 17:20:49 -07001028 address, address + HPAGE_PMD_SIZE);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001029 mmu_notifier_invalidate_range_start(&range);
Ville Syrjäläec649c9d2019-11-05 21:16:48 -08001030
1031 pte = pte_offset_map(pmd, address);
1032 pte_ptl = pte_lockptr(mm, pmd);
1033
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001034 pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1035 /*
1036 * After this gup_fast can't run anymore. This also removes
1037 * any huge TLB entry from the CPU so we won't allow
1038 * huge and small TLB entries for the same virtual address
1039 * to avoid the risk of CPU bugs in that area.
1040 */
1041 _pmd = pmdp_collapse_flush(vma, address, pmd);
1042 spin_unlock(pmd_ptl);
Jérôme Glisseac46d4f2018-12-28 00:38:09 -08001043 mmu_notifier_invalidate_range_end(&range);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001044
1045 spin_lock(pte_ptl);
1046 isolated = __collapse_huge_page_isolate(vma, address, pte);
1047 spin_unlock(pte_ptl);
1048
1049 if (unlikely(!isolated)) {
1050 pte_unmap(pte);
1051 spin_lock(pmd_ptl);
1052 BUG_ON(!pmd_none(*pmd));
1053 /*
1054 * We can only use set_pmd_at when establishing
1055 * hugepmds and never for establishing regular pmds that
1056 * points to regular pagetables. Use pmd_populate for that
1057 */
1058 pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1059 spin_unlock(pmd_ptl);
1060 anon_vma_unlock_write(vma->anon_vma);
1061 result = SCAN_FAIL;
1062 goto out;
1063 }
1064
1065 /*
1066 * All pages are isolated and locked so anon_vma rmap
1067 * can't run anymore.
1068 */
1069 anon_vma_unlock_write(vma->anon_vma);
1070
1071 __collapse_huge_page_copy(pte, new_page, vma, address, pte_ptl);
1072 pte_unmap(pte);
1073 __SetPageUptodate(new_page);
1074 pgtable = pmd_pgtable(_pmd);
1075
1076 _pmd = mk_huge_pmd(new_page, vma->vm_page_prot);
Linus Torvaldsf55e1012017-11-29 09:01:01 -08001077 _pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001078
1079 /*
1080 * spin_lock() below is not the equivalent of smp_wmb(), so
1081 * this is needed to avoid the copy_huge_page writes to become
1082 * visible after the set_pmd_at() write.
1083 */
1084 smp_wmb();
1085
1086 spin_lock(pmd_ptl);
1087 BUG_ON(!pmd_none(*pmd));
1088 page_add_new_anon_rmap(new_page, vma, address, true);
1089 mem_cgroup_commit_charge(new_page, memcg, false, true);
Chris Down1ff9e6e2019-03-05 15:48:09 -08001090 count_memcg_events(memcg, THP_COLLAPSE_ALLOC, 1);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001091 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
Lance Roy35f3aa32018-10-04 23:45:47 -07001240 lockdep_assert_held(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001241
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
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001259#ifdef CONFIG_SHMEM
Song Liu27e1f822019-09-23 15:38:30 -07001260/*
1261 * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1262 * khugepaged should try to collapse the page table.
1263 */
1264static int khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
1265 unsigned long addr)
1266{
1267 struct mm_slot *mm_slot;
1268
1269 VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1270
1271 spin_lock(&khugepaged_mm_lock);
1272 mm_slot = get_mm_slot(mm);
1273 if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP))
1274 mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
1275 spin_unlock(&khugepaged_mm_lock);
1276 return 0;
1277}
1278
1279/**
1280 * Try to collapse a pte-mapped THP for mm at address haddr.
1281 *
1282 * This function checks whether all the PTEs in the PMD are pointing to the
1283 * right THP. If so, retract the page table so the THP can refault in with
1284 * as pmd-mapped.
1285 */
1286void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
1287{
1288 unsigned long haddr = addr & HPAGE_PMD_MASK;
1289 struct vm_area_struct *vma = find_vma(mm, haddr);
1290 struct page *hpage = NULL;
1291 pte_t *start_pte, *pte;
1292 pmd_t *pmd, _pmd;
1293 spinlock_t *ptl;
1294 int count = 0;
1295 int i;
1296
1297 if (!vma || !vma->vm_file ||
1298 vma->vm_start > haddr || vma->vm_end < haddr + HPAGE_PMD_SIZE)
1299 return;
1300
1301 /*
1302 * This vm_flags may not have VM_HUGEPAGE if the page was not
1303 * collapsed by this mm. But we can still collapse if the page is
1304 * the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
1305 * will not fail the vma for missing VM_HUGEPAGE
1306 */
1307 if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
1308 return;
1309
1310 pmd = mm_find_pmd(mm, haddr);
1311 if (!pmd)
1312 return;
1313
1314 start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
1315
1316 /* step 1: check all mapped PTEs are to the right huge page */
1317 for (i = 0, addr = haddr, pte = start_pte;
1318 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1319 struct page *page;
1320
1321 /* empty pte, skip */
1322 if (pte_none(*pte))
1323 continue;
1324
1325 /* page swapped out, abort */
1326 if (!pte_present(*pte))
1327 goto abort;
1328
1329 page = vm_normal_page(vma, addr, *pte);
1330
1331 if (!page || !PageCompound(page))
1332 goto abort;
1333
1334 if (!hpage) {
1335 hpage = compound_head(page);
1336 /*
1337 * The mapping of the THP should not change.
1338 *
1339 * Note that uprobe, debugger, or MAP_PRIVATE may
1340 * change the page table, but the new page will
1341 * not pass PageCompound() check.
1342 */
1343 if (WARN_ON(hpage->mapping != vma->vm_file->f_mapping))
1344 goto abort;
1345 }
1346
1347 /*
1348 * Confirm the page maps to the correct subpage.
1349 *
1350 * Note that uprobe, debugger, or MAP_PRIVATE may change
1351 * the page table, but the new page will not pass
1352 * PageCompound() check.
1353 */
1354 if (WARN_ON(hpage + i != page))
1355 goto abort;
1356 count++;
1357 }
1358
1359 /* step 2: adjust rmap */
1360 for (i = 0, addr = haddr, pte = start_pte;
1361 i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1362 struct page *page;
1363
1364 if (pte_none(*pte))
1365 continue;
1366 page = vm_normal_page(vma, addr, *pte);
1367 page_remove_rmap(page, false);
1368 }
1369
1370 pte_unmap_unlock(start_pte, ptl);
1371
1372 /* step 3: set proper refcount and mm_counters. */
1373 if (hpage) {
1374 page_ref_sub(hpage, count);
1375 add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1376 }
1377
1378 /* step 4: collapse pmd */
1379 ptl = pmd_lock(vma->vm_mm, pmd);
1380 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1381 spin_unlock(ptl);
1382 mm_dec_nr_ptes(mm);
1383 pte_free(mm, pmd_pgtable(_pmd));
1384 return;
1385
1386abort:
1387 pte_unmap_unlock(start_pte, ptl);
1388}
1389
1390static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
1391{
1392 struct mm_struct *mm = mm_slot->mm;
1393 int i;
1394
1395 if (likely(mm_slot->nr_pte_mapped_thp == 0))
1396 return 0;
1397
1398 if (!down_write_trylock(&mm->mmap_sem))
1399 return -EBUSY;
1400
1401 if (unlikely(khugepaged_test_exit(mm)))
1402 goto out;
1403
1404 for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
1405 collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i]);
1406
1407out:
1408 mm_slot->nr_pte_mapped_thp = 0;
1409 up_write(&mm->mmap_sem);
1410 return 0;
1411}
1412
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001413static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)
1414{
1415 struct vm_area_struct *vma;
1416 unsigned long addr;
1417 pmd_t *pmd, _pmd;
1418
1419 i_mmap_lock_write(mapping);
1420 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Song Liu27e1f822019-09-23 15:38:30 -07001421 /*
1422 * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1423 * got written to. These VMAs are likely not worth investing
1424 * down_write(mmap_sem) as PMD-mapping is likely to be split
1425 * later.
1426 *
1427 * Not that vma->anon_vma check is racy: it can be set up after
1428 * the check but before we took mmap_sem by the fault path.
1429 * But page lock would prevent establishing any new ptes of the
1430 * page, so we are safe.
1431 *
1432 * An alternative would be drop the check, but check that page
1433 * table is clear before calling pmdp_collapse_flush() under
1434 * ptl. It has higher chance to recover THP for the VMA, but
1435 * has higher cost too.
1436 */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001437 if (vma->anon_vma)
1438 continue;
1439 addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
1440 if (addr & ~HPAGE_PMD_MASK)
1441 continue;
1442 if (vma->vm_end < addr + HPAGE_PMD_SIZE)
1443 continue;
1444 pmd = mm_find_pmd(vma->vm_mm, addr);
1445 if (!pmd)
1446 continue;
1447 /*
1448 * We need exclusive mmap_sem to retract page table.
Song Liu27e1f822019-09-23 15:38:30 -07001449 *
1450 * We use trylock due to lock inversion: we need to acquire
1451 * mmap_sem while holding page lock. Fault path does it in
1452 * reverse order. Trylock is a way to avoid deadlock.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001453 */
1454 if (down_write_trylock(&vma->vm_mm->mmap_sem)) {
1455 spinlock_t *ptl = pmd_lock(vma->vm_mm, pmd);
1456 /* assume page table is clear */
1457 _pmd = pmdp_collapse_flush(vma, addr, pmd);
1458 spin_unlock(ptl);
1459 up_write(&vma->vm_mm->mmap_sem);
Kirill A. Shutemovc4812902017-11-15 17:35:37 -08001460 mm_dec_nr_ptes(vma->vm_mm);
Aneesh Kumar K.Vd670ffd2017-01-10 16:57:18 -08001461 pte_free(vma->vm_mm, pmd_pgtable(_pmd));
Song Liu27e1f822019-09-23 15:38:30 -07001462 } else {
1463 /* Try again later */
1464 khugepaged_add_pte_mapped_thp(vma->vm_mm, addr);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001465 }
1466 }
1467 i_mmap_unlock_write(mapping);
1468}
1469
1470/**
Song Liu99cb0db2019-09-23 15:38:00 -07001471 * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001472 *
1473 * Basic scheme is simple, details are more complex:
Hugh Dickins87c460a2018-11-30 14:10:43 -08001474 * - allocate and lock a new huge page;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001475 * - scan page cache replacing old pages with the new one
Song Liu99cb0db2019-09-23 15:38:00 -07001476 * + swap/gup in pages if necessary;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001477 * + fill in gaps;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001478 * + keep old pages around in case rollback is required;
1479 * - if replacing succeeds:
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001480 * + copy data over;
1481 * + free old pages;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001482 * + unlock huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001483 * - if replacing failed;
1484 * + put all pages back and unfreeze them;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001485 * + restore gaps in the page cache;
Hugh Dickins87c460a2018-11-30 14:10:43 -08001486 * + unlock and free huge page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001487 */
Song Liu579c5712019-09-23 15:37:57 -07001488static void collapse_file(struct mm_struct *mm,
1489 struct file *file, pgoff_t start,
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001490 struct page **hpage, int node)
1491{
Song Liu579c5712019-09-23 15:37:57 -07001492 struct address_space *mapping = file->f_mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001493 gfp_t gfp;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001494 struct page *new_page;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001495 struct mem_cgroup *memcg;
1496 pgoff_t index, end = start + HPAGE_PMD_NR;
1497 LIST_HEAD(pagelist);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001498 XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001499 int nr_none = 0, result = SCAN_SUCCEED;
Song Liu99cb0db2019-09-23 15:38:00 -07001500 bool is_shmem = shmem_file(file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001501
Song Liu99cb0db2019-09-23 15:38:00 -07001502 VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001503 VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1504
1505 /* Only allocate from the target node */
Michal Hocko41b61672017-01-10 16:57:42 -08001506 gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001507
1508 new_page = khugepaged_alloc_page(hpage, gfp, node);
1509 if (!new_page) {
1510 result = SCAN_ALLOC_HUGE_PAGE_FAIL;
1511 goto out;
1512 }
1513
Michal Hocko2a70f6a2018-04-10 16:29:30 -07001514 if (unlikely(mem_cgroup_try_charge(new_page, mm, gfp, &memcg, true))) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001515 result = SCAN_CGROUP_CHARGE_FAIL;
1516 goto out;
1517 }
1518
Hugh Dickins95feeab2018-11-30 14:10:50 -08001519 /* This will be less messy when we use multi-index entries */
1520 do {
1521 xas_lock_irq(&xas);
1522 xas_create_range(&xas);
1523 if (!xas_error(&xas))
1524 break;
1525 xas_unlock_irq(&xas);
1526 if (!xas_nomem(&xas, GFP_KERNEL)) {
1527 mem_cgroup_cancel_charge(new_page, memcg, true);
1528 result = SCAN_FAIL;
1529 goto out;
1530 }
1531 } while (1);
1532
Hugh Dickins042a3082018-11-30 14:10:39 -08001533 __SetPageLocked(new_page);
Song Liu99cb0db2019-09-23 15:38:00 -07001534 if (is_shmem)
1535 __SetPageSwapBacked(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001536 new_page->index = start;
1537 new_page->mapping = mapping;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001538
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001539 /*
Hugh Dickins87c460a2018-11-30 14:10:43 -08001540 * At this point the new_page is locked and not up-to-date.
1541 * It's safe to insert it into the page cache, because nobody would
1542 * be able to map it or use it in another way until we unlock it.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001543 */
1544
Matthew Wilcox77da9382017-12-04 14:56:08 -05001545 xas_set(&xas, start);
1546 for (index = start; index < end; index++) {
1547 struct page *page = xas_next(&xas);
1548
1549 VM_BUG_ON(index != xas.xa_index);
Song Liu99cb0db2019-09-23 15:38:00 -07001550 if (is_shmem) {
1551 if (!page) {
1552 /*
1553 * Stop if extent has been truncated or
1554 * hole-punched, and is now completely
1555 * empty.
1556 */
1557 if (index == start) {
1558 if (!xas_next_entry(&xas, end - 1)) {
1559 result = SCAN_TRUNCATED;
1560 goto xa_locked;
1561 }
1562 xas_set(&xas, index);
1563 }
1564 if (!shmem_charge(mapping->host, 1)) {
1565 result = SCAN_FAIL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001566 goto xa_locked;
Hugh Dickins701270f2018-11-30 14:10:25 -08001567 }
Song Liu99cb0db2019-09-23 15:38:00 -07001568 xas_store(&xas, new_page);
1569 nr_none++;
1570 continue;
Hugh Dickins701270f2018-11-30 14:10:25 -08001571 }
Song Liu99cb0db2019-09-23 15:38:00 -07001572
1573 if (xa_is_value(page) || !PageUptodate(page)) {
1574 xas_unlock_irq(&xas);
1575 /* swap in or instantiate fallocated page */
1576 if (shmem_getpage(mapping->host, index, &page,
1577 SGP_NOHUGE)) {
1578 result = SCAN_FAIL;
1579 goto xa_unlocked;
1580 }
1581 } else if (trylock_page(page)) {
1582 get_page(page);
1583 xas_unlock_irq(&xas);
1584 } else {
1585 result = SCAN_PAGE_LOCK;
Hugh Dickins042a3082018-11-30 14:10:39 -08001586 goto xa_locked;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001587 }
Song Liu99cb0db2019-09-23 15:38:00 -07001588 } else { /* !is_shmem */
1589 if (!page || xa_is_value(page)) {
1590 xas_unlock_irq(&xas);
1591 page_cache_sync_readahead(mapping, &file->f_ra,
1592 file, index,
1593 PAGE_SIZE);
1594 /* drain pagevecs to help isolate_lru_page() */
1595 lru_add_drain();
1596 page = find_lock_page(mapping, index);
1597 if (unlikely(page == NULL)) {
1598 result = SCAN_FAIL;
1599 goto xa_unlocked;
1600 }
Song Liu75f36062019-11-30 17:57:19 -08001601 } else if (PageDirty(page)) {
1602 /*
1603 * khugepaged only works on read-only fd,
1604 * so this page is dirty because it hasn't
1605 * been flushed since first write. There
1606 * won't be new dirty pages.
1607 *
1608 * Trigger async flush here and hope the
1609 * writeback is done when khugepaged
1610 * revisits this page.
1611 *
1612 * This is a one-off situation. We are not
1613 * forcing writeback in loop.
1614 */
1615 xas_unlock_irq(&xas);
1616 filemap_flush(mapping);
1617 result = SCAN_FAIL;
1618 goto xa_unlocked;
Song Liu99cb0db2019-09-23 15:38:00 -07001619 } else if (trylock_page(page)) {
1620 get_page(page);
1621 xas_unlock_irq(&xas);
1622 } else {
1623 result = SCAN_PAGE_LOCK;
1624 goto xa_locked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001625 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001626 }
1627
1628 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001629 * The page must be locked, so we can drop the i_pages lock
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001630 * without racing with truncate.
1631 */
1632 VM_BUG_ON_PAGE(!PageLocked(page), page);
Song Liu4655e5e2019-11-15 17:34:53 -08001633
1634 /* make sure the page is up to date */
1635 if (unlikely(!PageUptodate(page))) {
1636 result = SCAN_FAIL;
1637 goto out_unlock;
1638 }
Hugh Dickins06a5e122018-11-30 14:10:47 -08001639
1640 /*
1641 * If file was truncated then extended, or hole-punched, before
1642 * we locked the first page, then a THP might be there already.
1643 */
1644 if (PageTransCompound(page)) {
1645 result = SCAN_PAGE_COMPOUND;
1646 goto out_unlock;
1647 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001648
1649 if (page_mapping(page) != mapping) {
1650 result = SCAN_TRUNCATED;
1651 goto out_unlock;
1652 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001653
Song Liu4655e5e2019-11-15 17:34:53 -08001654 if (!is_shmem && PageDirty(page)) {
1655 /*
1656 * khugepaged only works on read-only fd, so this
1657 * page is dirty because it hasn't been flushed
1658 * since first write.
1659 */
1660 result = SCAN_FAIL;
1661 goto out_unlock;
1662 }
1663
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001664 if (isolate_lru_page(page)) {
1665 result = SCAN_DEL_PAGE_LRU;
Hugh Dickins042a3082018-11-30 14:10:39 -08001666 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001667 }
1668
Song Liu99cb0db2019-09-23 15:38:00 -07001669 if (page_has_private(page) &&
1670 !try_to_release_page(page, GFP_KERNEL)) {
1671 result = SCAN_PAGE_HAS_PRIVATE;
1672 goto out_unlock;
1673 }
1674
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001675 if (page_mapped(page))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -08001676 unmap_mapping_pages(mapping, index, 1, false);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001677
Matthew Wilcox77da9382017-12-04 14:56:08 -05001678 xas_lock_irq(&xas);
1679 xas_set(&xas, index);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001680
Matthew Wilcox77da9382017-12-04 14:56:08 -05001681 VM_BUG_ON_PAGE(page != xas_load(&xas), page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001682 VM_BUG_ON_PAGE(page_mapped(page), page);
1683
1684 /*
1685 * The page is expected to have page_count() == 3:
1686 * - we hold a pin on it;
Matthew Wilcox77da9382017-12-04 14:56:08 -05001687 * - one reference from page cache;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001688 * - one from isolate_lru_page;
1689 */
1690 if (!page_ref_freeze(page, 3)) {
1691 result = SCAN_PAGE_COUNT;
Hugh Dickins042a3082018-11-30 14:10:39 -08001692 xas_unlock_irq(&xas);
1693 putback_lru_page(page);
1694 goto out_unlock;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001695 }
1696
1697 /*
1698 * Add the page to the list to be able to undo the collapse if
1699 * something go wrong.
1700 */
1701 list_add_tail(&page->lru, &pagelist);
1702
1703 /* Finally, replace with the new page. */
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -07001704 xas_store(&xas, new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001705 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001706out_unlock:
1707 unlock_page(page);
1708 put_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001709 goto xa_unlocked;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001710 }
1711
Song Liu99cb0db2019-09-23 15:38:00 -07001712 if (is_shmem)
1713 __inc_node_page_state(new_page, NR_SHMEM_THPS);
Song Liu09d91cd2019-09-23 15:38:03 -07001714 else {
Song Liu99cb0db2019-09-23 15:38:00 -07001715 __inc_node_page_state(new_page, NR_FILE_THPS);
Song Liu09d91cd2019-09-23 15:38:03 -07001716 filemap_nr_thps_inc(mapping);
1717 }
Song Liu99cb0db2019-09-23 15:38:00 -07001718
Hugh Dickins042a3082018-11-30 14:10:39 -08001719 if (nr_none) {
1720 struct zone *zone = page_zone(new_page);
1721
1722 __mod_node_page_state(zone->zone_pgdat, NR_FILE_PAGES, nr_none);
Song Liu99cb0db2019-09-23 15:38:00 -07001723 if (is_shmem)
1724 __mod_node_page_state(zone->zone_pgdat,
1725 NR_SHMEM, nr_none);
Hugh Dickins042a3082018-11-30 14:10:39 -08001726 }
1727
1728xa_locked:
1729 xas_unlock_irq(&xas);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001730xa_unlocked:
Hugh Dickins042a3082018-11-30 14:10:39 -08001731
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001732 if (result == SCAN_SUCCEED) {
Matthew Wilcox77da9382017-12-04 14:56:08 -05001733 struct page *page, *tmp;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001734
1735 /*
Matthew Wilcox77da9382017-12-04 14:56:08 -05001736 * Replacing old pages with new one has succeeded, now we
1737 * need to copy the content and free the old pages.
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001738 */
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001739 index = start;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001740 list_for_each_entry_safe(page, tmp, &pagelist, lru) {
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001741 while (index < page->index) {
1742 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1743 index++;
1744 }
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001745 copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
1746 page);
1747 list_del(&page->lru);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001748 page->mapping = NULL;
Hugh Dickins042a3082018-11-30 14:10:39 -08001749 page_ref_unfreeze(page, 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001750 ClearPageActive(page);
1751 ClearPageUnevictable(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001752 unlock_page(page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001753 put_page(page);
Hugh Dickins2af8ff22018-11-30 14:10:35 -08001754 index++;
1755 }
1756 while (index < end) {
1757 clear_highpage(new_page + (index % HPAGE_PMD_NR));
1758 index++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001759 }
1760
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001761 SetPageUptodate(new_page);
Hugh Dickins87c460a2018-11-30 14:10:43 -08001762 page_ref_add(new_page, HPAGE_PMD_NR - 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001763 mem_cgroup_commit_charge(new_page, memcg, false, true);
Song Liu99cb0db2019-09-23 15:38:00 -07001764
1765 if (is_shmem) {
1766 set_page_dirty(new_page);
1767 lru_cache_add_anon(new_page);
1768 } else {
1769 lru_cache_add_file(new_page);
1770 }
Chris Down1ff9e6e2019-03-05 15:48:09 -08001771 count_memcg_events(memcg, THP_COLLAPSE_ALLOC, 1);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001772
Hugh Dickins042a3082018-11-30 14:10:39 -08001773 /*
1774 * Remove pte page tables, so we can re-fault the page as huge.
1775 */
1776 retract_page_tables(mapping, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001777 *hpage = NULL;
Yang Shi87aa7522018-08-17 15:45:29 -07001778
1779 khugepaged_pages_collapsed++;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001780 } else {
Matthew Wilcox77da9382017-12-04 14:56:08 -05001781 struct page *page;
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001782
Matthew Wilcox77da9382017-12-04 14:56:08 -05001783 /* Something went wrong: roll back page cache changes */
Matthew Wilcox77da9382017-12-04 14:56:08 -05001784 xas_lock_irq(&xas);
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001785 mapping->nrpages -= nr_none;
Song Liu99cb0db2019-09-23 15:38:00 -07001786
1787 if (is_shmem)
1788 shmem_uncharge(mapping->host, nr_none);
Hugh Dickinsaaa52e32018-11-30 14:10:29 -08001789
Matthew Wilcox77da9382017-12-04 14:56:08 -05001790 xas_set(&xas, start);
1791 xas_for_each(&xas, page, end - 1) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001792 page = list_first_entry_or_null(&pagelist,
1793 struct page, lru);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001794 if (!page || xas.xa_index < page->index) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001795 if (!nr_none)
1796 break;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001797 nr_none--;
Johannes Weiner59749e62016-12-12 16:43:35 -08001798 /* Put holes back where they were */
Matthew Wilcox77da9382017-12-04 14:56:08 -05001799 xas_store(&xas, NULL);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001800 continue;
1801 }
1802
Matthew Wilcox77da9382017-12-04 14:56:08 -05001803 VM_BUG_ON_PAGE(page->index != xas.xa_index, page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001804
1805 /* Unfreeze the page. */
1806 list_del(&page->lru);
1807 page_ref_unfreeze(page, 2);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001808 xas_store(&xas, page);
1809 xas_pause(&xas);
1810 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001811 unlock_page(page);
Hugh Dickins042a3082018-11-30 14:10:39 -08001812 putback_lru_page(page);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001813 xas_lock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001814 }
1815 VM_BUG_ON(nr_none);
Matthew Wilcox77da9382017-12-04 14:56:08 -05001816 xas_unlock_irq(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001817
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001818 mem_cgroup_cancel_charge(new_page, memcg, true);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001819 new_page->mapping = NULL;
1820 }
Hugh Dickins042a3082018-11-30 14:10:39 -08001821
1822 unlock_page(new_page);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001823out:
1824 VM_BUG_ON(!list_empty(&pagelist));
1825 /* TODO: tracepoints */
1826}
1827
Song Liu579c5712019-09-23 15:37:57 -07001828static void khugepaged_scan_file(struct mm_struct *mm,
1829 struct file *file, pgoff_t start, struct page **hpage)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001830{
1831 struct page *page = NULL;
Song Liu579c5712019-09-23 15:37:57 -07001832 struct address_space *mapping = file->f_mapping;
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001833 XA_STATE(xas, &mapping->i_pages, start);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001834 int present, swap;
1835 int node = NUMA_NO_NODE;
1836 int result = SCAN_SUCCEED;
1837
1838 present = 0;
1839 swap = 0;
1840 memset(khugepaged_node_load, 0, sizeof(khugepaged_node_load));
1841 rcu_read_lock();
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001842 xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
1843 if (xas_retry(&xas, page))
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001844 continue;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001845
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001846 if (xa_is_value(page)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001847 if (++swap > khugepaged_max_ptes_swap) {
1848 result = SCAN_EXCEED_SWAP_PTE;
1849 break;
1850 }
1851 continue;
1852 }
1853
1854 if (PageTransCompound(page)) {
1855 result = SCAN_PAGE_COMPOUND;
1856 break;
1857 }
1858
1859 node = page_to_nid(page);
1860 if (khugepaged_scan_abort(node)) {
1861 result = SCAN_SCAN_ABORT;
1862 break;
1863 }
1864 khugepaged_node_load[node]++;
1865
1866 if (!PageLRU(page)) {
1867 result = SCAN_PAGE_LRU;
1868 break;
1869 }
1870
Song Liu99cb0db2019-09-23 15:38:00 -07001871 if (page_count(page) !=
1872 1 + page_mapcount(page) + page_has_private(page)) {
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001873 result = SCAN_PAGE_COUNT;
1874 break;
1875 }
1876
1877 /*
1878 * We probably should check if the page is referenced here, but
1879 * nobody would transfer pte_young() to PageReferenced() for us.
1880 * And rmap walk here is just too costly...
1881 */
1882
1883 present++;
1884
1885 if (need_resched()) {
Matthew Wilcox85b392d2017-12-04 15:06:23 -05001886 xas_pause(&xas);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001887 cond_resched_rcu();
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001888 }
1889 }
1890 rcu_read_unlock();
1891
1892 if (result == SCAN_SUCCEED) {
1893 if (present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
1894 result = SCAN_EXCEED_NONE_PTE;
1895 } else {
1896 node = khugepaged_find_target_node();
Song Liu579c5712019-09-23 15:37:57 -07001897 collapse_file(mm, file, start, hpage, node);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001898 }
1899 }
1900
1901 /* TODO: tracepoints */
1902}
1903#else
Song Liu579c5712019-09-23 15:37:57 -07001904static void khugepaged_scan_file(struct mm_struct *mm,
1905 struct file *file, pgoff_t start, struct page **hpage)
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001906{
1907 BUILD_BUG();
1908}
Song Liu27e1f822019-09-23 15:38:30 -07001909
1910static int khugepaged_collapse_pte_mapped_thps(struct mm_slot *mm_slot)
1911{
1912 return 0;
1913}
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001914#endif
1915
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001916static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
1917 struct page **hpage)
1918 __releases(&khugepaged_mm_lock)
1919 __acquires(&khugepaged_mm_lock)
1920{
1921 struct mm_slot *mm_slot;
1922 struct mm_struct *mm;
1923 struct vm_area_struct *vma;
1924 int progress = 0;
1925
1926 VM_BUG_ON(!pages);
Lance Roy35f3aa32018-10-04 23:45:47 -07001927 lockdep_assert_held(&khugepaged_mm_lock);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001928
1929 if (khugepaged_scan.mm_slot)
1930 mm_slot = khugepaged_scan.mm_slot;
1931 else {
1932 mm_slot = list_entry(khugepaged_scan.mm_head.next,
1933 struct mm_slot, mm_node);
1934 khugepaged_scan.address = 0;
1935 khugepaged_scan.mm_slot = mm_slot;
1936 }
1937 spin_unlock(&khugepaged_mm_lock);
Song Liu27e1f822019-09-23 15:38:30 -07001938 khugepaged_collapse_pte_mapped_thps(mm_slot);
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001939
1940 mm = mm_slot->mm;
Yang Shi3b454ad2018-01-31 16:18:28 -08001941 /*
1942 * Don't wait for semaphore (to avoid long wait times). Just move to
1943 * the next mm on the list.
1944 */
1945 vma = NULL;
1946 if (unlikely(!down_read_trylock(&mm->mmap_sem)))
1947 goto breakouterloop_mmap_sem;
1948 if (likely(!khugepaged_test_exit(mm)))
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001949 vma = find_vma(mm, khugepaged_scan.address);
1950
1951 progress++;
1952 for (; vma; vma = vma->vm_next) {
1953 unsigned long hstart, hend;
1954
1955 cond_resched();
1956 if (unlikely(khugepaged_test_exit(mm))) {
1957 progress++;
1958 break;
1959 }
Song Liu50f8b922018-08-17 15:47:00 -07001960 if (!hugepage_vma_check(vma, vma->vm_flags)) {
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001961skip:
1962 progress++;
1963 continue;
1964 }
1965 hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
1966 hend = vma->vm_end & HPAGE_PMD_MASK;
1967 if (hstart >= hend)
1968 goto skip;
1969 if (khugepaged_scan.address > hend)
1970 goto skip;
1971 if (khugepaged_scan.address < hstart)
1972 khugepaged_scan.address = hstart;
1973 VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001974 if (shmem_file(vma->vm_file) && !shmem_huge_enabled(vma))
1975 goto skip;
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07001976
1977 while (khugepaged_scan.address < hend) {
1978 int ret;
1979 cond_resched();
1980 if (unlikely(khugepaged_test_exit(mm)))
1981 goto breakouterloop;
1982
1983 VM_BUG_ON(khugepaged_scan.address < hstart ||
1984 khugepaged_scan.address + HPAGE_PMD_SIZE >
1985 hend);
Song Liu99cb0db2019-09-23 15:38:00 -07001986 if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001987 struct file *file = get_file(vma->vm_file);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001988 pgoff_t pgoff = linear_page_index(vma,
1989 khugepaged_scan.address);
Song Liu99cb0db2019-09-23 15:38:00 -07001990
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001991 up_read(&mm->mmap_sem);
1992 ret = 1;
Song Liu579c5712019-09-23 15:37:57 -07001993 khugepaged_scan_file(mm, file, pgoff, hpage);
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07001994 fput(file);
1995 } else {
1996 ret = khugepaged_scan_pmd(mm, vma,
1997 khugepaged_scan.address,
1998 hpage);
1999 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002000 /* move to next address */
2001 khugepaged_scan.address += HPAGE_PMD_SIZE;
2002 progress += HPAGE_PMD_NR;
2003 if (ret)
2004 /* we released mmap_sem so break loop */
2005 goto breakouterloop_mmap_sem;
2006 if (progress >= pages)
2007 goto breakouterloop;
2008 }
2009 }
2010breakouterloop:
2011 up_read(&mm->mmap_sem); /* exit_mmap will destroy ptes after this */
2012breakouterloop_mmap_sem:
2013
2014 spin_lock(&khugepaged_mm_lock);
2015 VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2016 /*
2017 * Release the current mm_slot if this mm is about to die, or
2018 * if we scanned all vmas of this mm.
2019 */
2020 if (khugepaged_test_exit(mm) || !vma) {
2021 /*
2022 * Make sure that if mm_users is reaching zero while
2023 * khugepaged runs here, khugepaged_exit will find
2024 * mm_slot not pointing to the exiting mm.
2025 */
2026 if (mm_slot->mm_node.next != &khugepaged_scan.mm_head) {
2027 khugepaged_scan.mm_slot = list_entry(
2028 mm_slot->mm_node.next,
2029 struct mm_slot, mm_node);
2030 khugepaged_scan.address = 0;
2031 } else {
2032 khugepaged_scan.mm_slot = NULL;
2033 khugepaged_full_scans++;
2034 }
2035
2036 collect_mm_slot(mm_slot);
2037 }
2038
2039 return progress;
2040}
2041
2042static int khugepaged_has_work(void)
2043{
2044 return !list_empty(&khugepaged_scan.mm_head) &&
2045 khugepaged_enabled();
2046}
2047
2048static int khugepaged_wait_event(void)
2049{
2050 return !list_empty(&khugepaged_scan.mm_head) ||
2051 kthread_should_stop();
2052}
2053
2054static void khugepaged_do_scan(void)
2055{
2056 struct page *hpage = NULL;
2057 unsigned int progress = 0, pass_through_head = 0;
2058 unsigned int pages = khugepaged_pages_to_scan;
2059 bool wait = true;
2060
2061 barrier(); /* write khugepaged_pages_to_scan to local stack */
2062
2063 while (progress < pages) {
2064 if (!khugepaged_prealloc_page(&hpage, &wait))
2065 break;
2066
2067 cond_resched();
2068
2069 if (unlikely(kthread_should_stop() || try_to_freeze()))
2070 break;
2071
2072 spin_lock(&khugepaged_mm_lock);
2073 if (!khugepaged_scan.mm_slot)
2074 pass_through_head++;
2075 if (khugepaged_has_work() &&
2076 pass_through_head < 2)
2077 progress += khugepaged_scan_mm_slot(pages - progress,
2078 &hpage);
2079 else
2080 progress = pages;
2081 spin_unlock(&khugepaged_mm_lock);
2082 }
2083
2084 if (!IS_ERR_OR_NULL(hpage))
2085 put_page(hpage);
2086}
2087
2088static bool khugepaged_should_wakeup(void)
2089{
2090 return kthread_should_stop() ||
2091 time_after_eq(jiffies, khugepaged_sleep_expire);
2092}
2093
2094static void khugepaged_wait_work(void)
2095{
2096 if (khugepaged_has_work()) {
2097 const unsigned long scan_sleep_jiffies =
2098 msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2099
2100 if (!scan_sleep_jiffies)
2101 return;
2102
2103 khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
2104 wait_event_freezable_timeout(khugepaged_wait,
2105 khugepaged_should_wakeup(),
2106 scan_sleep_jiffies);
2107 return;
2108 }
2109
2110 if (khugepaged_enabled())
2111 wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2112}
2113
2114static int khugepaged(void *none)
2115{
2116 struct mm_slot *mm_slot;
2117
2118 set_freezable();
2119 set_user_nice(current, MAX_NICE);
2120
2121 while (!kthread_should_stop()) {
2122 khugepaged_do_scan();
2123 khugepaged_wait_work();
2124 }
2125
2126 spin_lock(&khugepaged_mm_lock);
2127 mm_slot = khugepaged_scan.mm_slot;
2128 khugepaged_scan.mm_slot = NULL;
2129 if (mm_slot)
2130 collect_mm_slot(mm_slot);
2131 spin_unlock(&khugepaged_mm_lock);
2132 return 0;
2133}
2134
2135static void set_recommended_min_free_kbytes(void)
2136{
2137 struct zone *zone;
2138 int nr_zones = 0;
2139 unsigned long recommended_min;
2140
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002141 for_each_populated_zone(zone) {
2142 /*
2143 * We don't need to worry about fragmentation of
2144 * ZONE_MOVABLE since it only has movable pages.
2145 */
2146 if (zone_idx(zone) > gfp_zone(GFP_USER))
2147 continue;
2148
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002149 nr_zones++;
Joonsoo Kimb7d349c2018-04-10 16:30:27 -07002150 }
Kirill A. Shutemovb46e7562016-07-26 15:26:24 -07002151
2152 /* Ensure 2 pageblocks are free to assist fragmentation avoidance */
2153 recommended_min = pageblock_nr_pages * nr_zones * 2;
2154
2155 /*
2156 * Make sure that on average at least two pageblocks are almost free
2157 * of another type, one for a migratetype to fall back to and a
2158 * second to avoid subsequent fallbacks of other types There are 3
2159 * MIGRATE_TYPES we care about.
2160 */
2161 recommended_min += pageblock_nr_pages * nr_zones *
2162 MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
2163
2164 /* don't ever allow to reserve more than 5% of the lowmem */
2165 recommended_min = min(recommended_min,
2166 (unsigned long) nr_free_buffer_pages() / 20);
2167 recommended_min <<= (PAGE_SHIFT-10);
2168
2169 if (recommended_min > min_free_kbytes) {
2170 if (user_min_free_kbytes >= 0)
2171 pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
2172 min_free_kbytes, recommended_min);
2173
2174 min_free_kbytes = recommended_min;
2175 }
2176 setup_per_zone_wmarks();
2177}
2178
2179int start_stop_khugepaged(void)
2180{
2181 static struct task_struct *khugepaged_thread __read_mostly;
2182 static DEFINE_MUTEX(khugepaged_mutex);
2183 int err = 0;
2184
2185 mutex_lock(&khugepaged_mutex);
2186 if (khugepaged_enabled()) {
2187 if (!khugepaged_thread)
2188 khugepaged_thread = kthread_run(khugepaged, NULL,
2189 "khugepaged");
2190 if (IS_ERR(khugepaged_thread)) {
2191 pr_err("khugepaged: kthread_run(khugepaged) failed\n");
2192 err = PTR_ERR(khugepaged_thread);
2193 khugepaged_thread = NULL;
2194 goto fail;
2195 }
2196
2197 if (!list_empty(&khugepaged_scan.mm_head))
2198 wake_up_interruptible(&khugepaged_wait);
2199
2200 set_recommended_min_free_kbytes();
2201 } else if (khugepaged_thread) {
2202 kthread_stop(khugepaged_thread);
2203 khugepaged_thread = NULL;
2204 }
2205fail:
2206 mutex_unlock(&khugepaged_mutex);
2207 return err;
2208}