xref: /openbmc/linux/mm/khugepaged.c (revision 0201ebf274a306a6ebb95e5dc2d6a0a27c737cac)
1 // SPDX-License-Identifier: GPL-2.0
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 
4 #include <linux/mm.h>
5 #include <linux/sched.h>
6 #include <linux/sched/mm.h>
7 #include <linux/sched/coredump.h>
8 #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/page_table_check.h>
20 #include <linux/swapops.h>
21 #include <linux/shmem_fs.h>
22 #include <linux/ksm.h>
23 
24 #include <asm/tlb.h>
25 #include <asm/pgalloc.h>
26 #include "internal.h"
27 #include "mm_slot.h"
28 
29 enum scan_result {
30 	SCAN_FAIL,
31 	SCAN_SUCCEED,
32 	SCAN_PMD_NULL,
33 	SCAN_PMD_NONE,
34 	SCAN_PMD_MAPPED,
35 	SCAN_EXCEED_NONE_PTE,
36 	SCAN_EXCEED_SWAP_PTE,
37 	SCAN_EXCEED_SHARED_PTE,
38 	SCAN_PTE_NON_PRESENT,
39 	SCAN_PTE_UFFD_WP,
40 	SCAN_PTE_MAPPED_HUGEPAGE,
41 	SCAN_PAGE_RO,
42 	SCAN_LACK_REFERENCED_PAGE,
43 	SCAN_PAGE_NULL,
44 	SCAN_SCAN_ABORT,
45 	SCAN_PAGE_COUNT,
46 	SCAN_PAGE_LRU,
47 	SCAN_PAGE_LOCK,
48 	SCAN_PAGE_ANON,
49 	SCAN_PAGE_COMPOUND,
50 	SCAN_ANY_PROCESS,
51 	SCAN_VMA_NULL,
52 	SCAN_VMA_CHECK,
53 	SCAN_ADDRESS_RANGE,
54 	SCAN_DEL_PAGE_LRU,
55 	SCAN_ALLOC_HUGE_PAGE_FAIL,
56 	SCAN_CGROUP_CHARGE_FAIL,
57 	SCAN_TRUNCATED,
58 	SCAN_PAGE_HAS_PRIVATE,
59 	SCAN_STORE_FAILED,
60 	SCAN_COPY_MC,
61 	SCAN_PAGE_FILLED,
62 };
63 
64 #define CREATE_TRACE_POINTS
65 #include <trace/events/huge_memory.h>
66 
67 static struct task_struct *khugepaged_thread __read_mostly;
68 static DEFINE_MUTEX(khugepaged_mutex);
69 
70 /* default scan 8*512 pte (or vmas) every 30 second */
71 static unsigned int khugepaged_pages_to_scan __read_mostly;
72 static unsigned int khugepaged_pages_collapsed;
73 static unsigned int khugepaged_full_scans;
74 static unsigned int khugepaged_scan_sleep_millisecs __read_mostly = 10000;
75 /* during fragmentation poll the hugepage allocator once every minute */
76 static unsigned int khugepaged_alloc_sleep_millisecs __read_mostly = 60000;
77 static unsigned long khugepaged_sleep_expire;
78 static DEFINE_SPINLOCK(khugepaged_mm_lock);
79 static DECLARE_WAIT_QUEUE_HEAD(khugepaged_wait);
80 /*
81  * default collapse hugepages if there is at least one pte mapped like
82  * it would have happened if the vma was large enough during page
83  * fault.
84  *
85  * Note that these are only respected if collapse was initiated by khugepaged.
86  */
87 static unsigned int khugepaged_max_ptes_none __read_mostly;
88 static unsigned int khugepaged_max_ptes_swap __read_mostly;
89 static unsigned int khugepaged_max_ptes_shared __read_mostly;
90 
91 #define MM_SLOTS_HASH_BITS 10
92 static DEFINE_READ_MOSTLY_HASHTABLE(mm_slots_hash, MM_SLOTS_HASH_BITS);
93 
94 static struct kmem_cache *mm_slot_cache __read_mostly;
95 
96 #define MAX_PTE_MAPPED_THP 8
97 
98 struct collapse_control {
99 	bool is_khugepaged;
100 
101 	/* Num pages scanned per node */
102 	u32 node_load[MAX_NUMNODES];
103 
104 	/* nodemask for allocation fallback */
105 	nodemask_t alloc_nmask;
106 };
107 
108 /**
109  * struct khugepaged_mm_slot - khugepaged information per mm that is being scanned
110  * @slot: hash lookup from mm to mm_slot
111  * @nr_pte_mapped_thp: number of pte mapped THP
112  * @pte_mapped_thp: address array corresponding pte mapped THP
113  */
114 struct khugepaged_mm_slot {
115 	struct mm_slot slot;
116 
117 	/* pte-mapped THP in this mm */
118 	int nr_pte_mapped_thp;
119 	unsigned long pte_mapped_thp[MAX_PTE_MAPPED_THP];
120 };
121 
122 /**
123  * struct khugepaged_scan - cursor for scanning
124  * @mm_head: the head of the mm list to scan
125  * @mm_slot: the current mm_slot we are scanning
126  * @address: the next address inside that to be scanned
127  *
128  * There is only the one khugepaged_scan instance of this cursor structure.
129  */
130 struct khugepaged_scan {
131 	struct list_head mm_head;
132 	struct khugepaged_mm_slot *mm_slot;
133 	unsigned long address;
134 };
135 
136 static struct khugepaged_scan khugepaged_scan = {
137 	.mm_head = LIST_HEAD_INIT(khugepaged_scan.mm_head),
138 };
139 
140 #ifdef CONFIG_SYSFS
141 static ssize_t scan_sleep_millisecs_show(struct kobject *kobj,
142 					 struct kobj_attribute *attr,
143 					 char *buf)
144 {
145 	return sysfs_emit(buf, "%u\n", khugepaged_scan_sleep_millisecs);
146 }
147 
148 static ssize_t scan_sleep_millisecs_store(struct kobject *kobj,
149 					  struct kobj_attribute *attr,
150 					  const char *buf, size_t count)
151 {
152 	unsigned int msecs;
153 	int err;
154 
155 	err = kstrtouint(buf, 10, &msecs);
156 	if (err)
157 		return -EINVAL;
158 
159 	khugepaged_scan_sleep_millisecs = msecs;
160 	khugepaged_sleep_expire = 0;
161 	wake_up_interruptible(&khugepaged_wait);
162 
163 	return count;
164 }
165 static struct kobj_attribute scan_sleep_millisecs_attr =
166 	__ATTR_RW(scan_sleep_millisecs);
167 
168 static ssize_t alloc_sleep_millisecs_show(struct kobject *kobj,
169 					  struct kobj_attribute *attr,
170 					  char *buf)
171 {
172 	return sysfs_emit(buf, "%u\n", khugepaged_alloc_sleep_millisecs);
173 }
174 
175 static ssize_t alloc_sleep_millisecs_store(struct kobject *kobj,
176 					   struct kobj_attribute *attr,
177 					   const char *buf, size_t count)
178 {
179 	unsigned int msecs;
180 	int err;
181 
182 	err = kstrtouint(buf, 10, &msecs);
183 	if (err)
184 		return -EINVAL;
185 
186 	khugepaged_alloc_sleep_millisecs = msecs;
187 	khugepaged_sleep_expire = 0;
188 	wake_up_interruptible(&khugepaged_wait);
189 
190 	return count;
191 }
192 static struct kobj_attribute alloc_sleep_millisecs_attr =
193 	__ATTR_RW(alloc_sleep_millisecs);
194 
195 static ssize_t pages_to_scan_show(struct kobject *kobj,
196 				  struct kobj_attribute *attr,
197 				  char *buf)
198 {
199 	return sysfs_emit(buf, "%u\n", khugepaged_pages_to_scan);
200 }
201 static ssize_t pages_to_scan_store(struct kobject *kobj,
202 				   struct kobj_attribute *attr,
203 				   const char *buf, size_t count)
204 {
205 	unsigned int pages;
206 	int err;
207 
208 	err = kstrtouint(buf, 10, &pages);
209 	if (err || !pages)
210 		return -EINVAL;
211 
212 	khugepaged_pages_to_scan = pages;
213 
214 	return count;
215 }
216 static struct kobj_attribute pages_to_scan_attr =
217 	__ATTR_RW(pages_to_scan);
218 
219 static ssize_t pages_collapsed_show(struct kobject *kobj,
220 				    struct kobj_attribute *attr,
221 				    char *buf)
222 {
223 	return sysfs_emit(buf, "%u\n", khugepaged_pages_collapsed);
224 }
225 static struct kobj_attribute pages_collapsed_attr =
226 	__ATTR_RO(pages_collapsed);
227 
228 static ssize_t full_scans_show(struct kobject *kobj,
229 			       struct kobj_attribute *attr,
230 			       char *buf)
231 {
232 	return sysfs_emit(buf, "%u\n", khugepaged_full_scans);
233 }
234 static struct kobj_attribute full_scans_attr =
235 	__ATTR_RO(full_scans);
236 
237 static ssize_t defrag_show(struct kobject *kobj,
238 			   struct kobj_attribute *attr, char *buf)
239 {
240 	return single_hugepage_flag_show(kobj, attr, buf,
241 					 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
242 }
243 static ssize_t defrag_store(struct kobject *kobj,
244 			    struct kobj_attribute *attr,
245 			    const char *buf, size_t count)
246 {
247 	return single_hugepage_flag_store(kobj, attr, buf, count,
248 				 TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG);
249 }
250 static struct kobj_attribute khugepaged_defrag_attr =
251 	__ATTR_RW(defrag);
252 
253 /*
254  * max_ptes_none controls if khugepaged should collapse hugepages over
255  * any unmapped ptes in turn potentially increasing the memory
256  * footprint of the vmas. When max_ptes_none is 0 khugepaged will not
257  * reduce the available free memory in the system as it
258  * runs. Increasing max_ptes_none will instead potentially reduce the
259  * free memory in the system during the khugepaged scan.
260  */
261 static ssize_t max_ptes_none_show(struct kobject *kobj,
262 				  struct kobj_attribute *attr,
263 				  char *buf)
264 {
265 	return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_none);
266 }
267 static ssize_t max_ptes_none_store(struct kobject *kobj,
268 				   struct kobj_attribute *attr,
269 				   const char *buf, size_t count)
270 {
271 	int err;
272 	unsigned long max_ptes_none;
273 
274 	err = kstrtoul(buf, 10, &max_ptes_none);
275 	if (err || max_ptes_none > HPAGE_PMD_NR - 1)
276 		return -EINVAL;
277 
278 	khugepaged_max_ptes_none = max_ptes_none;
279 
280 	return count;
281 }
282 static struct kobj_attribute khugepaged_max_ptes_none_attr =
283 	__ATTR_RW(max_ptes_none);
284 
285 static ssize_t max_ptes_swap_show(struct kobject *kobj,
286 				  struct kobj_attribute *attr,
287 				  char *buf)
288 {
289 	return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_swap);
290 }
291 
292 static ssize_t max_ptes_swap_store(struct kobject *kobj,
293 				   struct kobj_attribute *attr,
294 				   const char *buf, size_t count)
295 {
296 	int err;
297 	unsigned long max_ptes_swap;
298 
299 	err  = kstrtoul(buf, 10, &max_ptes_swap);
300 	if (err || max_ptes_swap > HPAGE_PMD_NR - 1)
301 		return -EINVAL;
302 
303 	khugepaged_max_ptes_swap = max_ptes_swap;
304 
305 	return count;
306 }
307 
308 static struct kobj_attribute khugepaged_max_ptes_swap_attr =
309 	__ATTR_RW(max_ptes_swap);
310 
311 static ssize_t max_ptes_shared_show(struct kobject *kobj,
312 				    struct kobj_attribute *attr,
313 				    char *buf)
314 {
315 	return sysfs_emit(buf, "%u\n", khugepaged_max_ptes_shared);
316 }
317 
318 static ssize_t max_ptes_shared_store(struct kobject *kobj,
319 				     struct kobj_attribute *attr,
320 				     const char *buf, size_t count)
321 {
322 	int err;
323 	unsigned long max_ptes_shared;
324 
325 	err  = kstrtoul(buf, 10, &max_ptes_shared);
326 	if (err || max_ptes_shared > HPAGE_PMD_NR - 1)
327 		return -EINVAL;
328 
329 	khugepaged_max_ptes_shared = max_ptes_shared;
330 
331 	return count;
332 }
333 
334 static struct kobj_attribute khugepaged_max_ptes_shared_attr =
335 	__ATTR_RW(max_ptes_shared);
336 
337 static struct attribute *khugepaged_attr[] = {
338 	&khugepaged_defrag_attr.attr,
339 	&khugepaged_max_ptes_none_attr.attr,
340 	&khugepaged_max_ptes_swap_attr.attr,
341 	&khugepaged_max_ptes_shared_attr.attr,
342 	&pages_to_scan_attr.attr,
343 	&pages_collapsed_attr.attr,
344 	&full_scans_attr.attr,
345 	&scan_sleep_millisecs_attr.attr,
346 	&alloc_sleep_millisecs_attr.attr,
347 	NULL,
348 };
349 
350 struct attribute_group khugepaged_attr_group = {
351 	.attrs = khugepaged_attr,
352 	.name = "khugepaged",
353 };
354 #endif /* CONFIG_SYSFS */
355 
356 int hugepage_madvise(struct vm_area_struct *vma,
357 		     unsigned long *vm_flags, int advice)
358 {
359 	switch (advice) {
360 	case MADV_HUGEPAGE:
361 #ifdef CONFIG_S390
362 		/*
363 		 * qemu blindly sets MADV_HUGEPAGE on all allocations, but s390
364 		 * can't handle this properly after s390_enable_sie, so we simply
365 		 * ignore the madvise to prevent qemu from causing a SIGSEGV.
366 		 */
367 		if (mm_has_pgste(vma->vm_mm))
368 			return 0;
369 #endif
370 		*vm_flags &= ~VM_NOHUGEPAGE;
371 		*vm_flags |= VM_HUGEPAGE;
372 		/*
373 		 * If the vma become good for khugepaged to scan,
374 		 * register it here without waiting a page fault that
375 		 * may not happen any time soon.
376 		 */
377 		khugepaged_enter_vma(vma, *vm_flags);
378 		break;
379 	case MADV_NOHUGEPAGE:
380 		*vm_flags &= ~VM_HUGEPAGE;
381 		*vm_flags |= VM_NOHUGEPAGE;
382 		/*
383 		 * Setting VM_NOHUGEPAGE will prevent khugepaged from scanning
384 		 * this vma even if we leave the mm registered in khugepaged if
385 		 * it got registered before VM_NOHUGEPAGE was set.
386 		 */
387 		break;
388 	}
389 
390 	return 0;
391 }
392 
393 int __init khugepaged_init(void)
394 {
395 	mm_slot_cache = kmem_cache_create("khugepaged_mm_slot",
396 					  sizeof(struct khugepaged_mm_slot),
397 					  __alignof__(struct khugepaged_mm_slot),
398 					  0, NULL);
399 	if (!mm_slot_cache)
400 		return -ENOMEM;
401 
402 	khugepaged_pages_to_scan = HPAGE_PMD_NR * 8;
403 	khugepaged_max_ptes_none = HPAGE_PMD_NR - 1;
404 	khugepaged_max_ptes_swap = HPAGE_PMD_NR / 8;
405 	khugepaged_max_ptes_shared = HPAGE_PMD_NR / 2;
406 
407 	return 0;
408 }
409 
410 void __init khugepaged_destroy(void)
411 {
412 	kmem_cache_destroy(mm_slot_cache);
413 }
414 
415 static inline int hpage_collapse_test_exit(struct mm_struct *mm)
416 {
417 	return atomic_read(&mm->mm_users) == 0;
418 }
419 
420 void __khugepaged_enter(struct mm_struct *mm)
421 {
422 	struct khugepaged_mm_slot *mm_slot;
423 	struct mm_slot *slot;
424 	int wakeup;
425 
426 	/* __khugepaged_exit() must not run from under us */
427 	VM_BUG_ON_MM(hpage_collapse_test_exit(mm), mm);
428 	if (unlikely(test_and_set_bit(MMF_VM_HUGEPAGE, &mm->flags)))
429 		return;
430 
431 	mm_slot = mm_slot_alloc(mm_slot_cache);
432 	if (!mm_slot)
433 		return;
434 
435 	slot = &mm_slot->slot;
436 
437 	spin_lock(&khugepaged_mm_lock);
438 	mm_slot_insert(mm_slots_hash, mm, slot);
439 	/*
440 	 * Insert just behind the scanning cursor, to let the area settle
441 	 * down a little.
442 	 */
443 	wakeup = list_empty(&khugepaged_scan.mm_head);
444 	list_add_tail(&slot->mm_node, &khugepaged_scan.mm_head);
445 	spin_unlock(&khugepaged_mm_lock);
446 
447 	mmgrab(mm);
448 	if (wakeup)
449 		wake_up_interruptible(&khugepaged_wait);
450 }
451 
452 void khugepaged_enter_vma(struct vm_area_struct *vma,
453 			  unsigned long vm_flags)
454 {
455 	if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) &&
456 	    hugepage_flags_enabled()) {
457 		if (hugepage_vma_check(vma, vm_flags, false, false, true))
458 			__khugepaged_enter(vma->vm_mm);
459 	}
460 }
461 
462 void __khugepaged_exit(struct mm_struct *mm)
463 {
464 	struct khugepaged_mm_slot *mm_slot;
465 	struct mm_slot *slot;
466 	int free = 0;
467 
468 	spin_lock(&khugepaged_mm_lock);
469 	slot = mm_slot_lookup(mm_slots_hash, mm);
470 	mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
471 	if (mm_slot && khugepaged_scan.mm_slot != mm_slot) {
472 		hash_del(&slot->hash);
473 		list_del(&slot->mm_node);
474 		free = 1;
475 	}
476 	spin_unlock(&khugepaged_mm_lock);
477 
478 	if (free) {
479 		clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
480 		mm_slot_free(mm_slot_cache, mm_slot);
481 		mmdrop(mm);
482 	} else if (mm_slot) {
483 		/*
484 		 * This is required to serialize against
485 		 * hpage_collapse_test_exit() (which is guaranteed to run
486 		 * under mmap sem read mode). Stop here (after we return all
487 		 * pagetables will be destroyed) until khugepaged has finished
488 		 * working on the pagetables under the mmap_lock.
489 		 */
490 		mmap_write_lock(mm);
491 		mmap_write_unlock(mm);
492 	}
493 }
494 
495 static void release_pte_folio(struct folio *folio)
496 {
497 	node_stat_mod_folio(folio,
498 			NR_ISOLATED_ANON + folio_is_file_lru(folio),
499 			-folio_nr_pages(folio));
500 	folio_unlock(folio);
501 	folio_putback_lru(folio);
502 }
503 
504 static void release_pte_page(struct page *page)
505 {
506 	release_pte_folio(page_folio(page));
507 }
508 
509 static void release_pte_pages(pte_t *pte, pte_t *_pte,
510 		struct list_head *compound_pagelist)
511 {
512 	struct folio *folio, *tmp;
513 
514 	while (--_pte >= pte) {
515 		pte_t pteval = ptep_get(_pte);
516 		unsigned long pfn;
517 
518 		if (pte_none(pteval))
519 			continue;
520 		pfn = pte_pfn(pteval);
521 		if (is_zero_pfn(pfn))
522 			continue;
523 		folio = pfn_folio(pfn);
524 		if (folio_test_large(folio))
525 			continue;
526 		release_pte_folio(folio);
527 	}
528 
529 	list_for_each_entry_safe(folio, tmp, compound_pagelist, lru) {
530 		list_del(&folio->lru);
531 		release_pte_folio(folio);
532 	}
533 }
534 
535 static bool is_refcount_suitable(struct page *page)
536 {
537 	int expected_refcount;
538 
539 	expected_refcount = total_mapcount(page);
540 	if (PageSwapCache(page))
541 		expected_refcount += compound_nr(page);
542 
543 	return page_count(page) == expected_refcount;
544 }
545 
546 static int __collapse_huge_page_isolate(struct vm_area_struct *vma,
547 					unsigned long address,
548 					pte_t *pte,
549 					struct collapse_control *cc,
550 					struct list_head *compound_pagelist)
551 {
552 	struct page *page = NULL;
553 	pte_t *_pte;
554 	int none_or_zero = 0, shared = 0, result = SCAN_FAIL, referenced = 0;
555 	bool writable = false;
556 
557 	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
558 	     _pte++, address += PAGE_SIZE) {
559 		pte_t pteval = ptep_get(_pte);
560 		if (pte_none(pteval) || (pte_present(pteval) &&
561 				is_zero_pfn(pte_pfn(pteval)))) {
562 			++none_or_zero;
563 			if (!userfaultfd_armed(vma) &&
564 			    (!cc->is_khugepaged ||
565 			     none_or_zero <= khugepaged_max_ptes_none)) {
566 				continue;
567 			} else {
568 				result = SCAN_EXCEED_NONE_PTE;
569 				count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
570 				goto out;
571 			}
572 		}
573 		if (!pte_present(pteval)) {
574 			result = SCAN_PTE_NON_PRESENT;
575 			goto out;
576 		}
577 		if (pte_uffd_wp(pteval)) {
578 			result = SCAN_PTE_UFFD_WP;
579 			goto out;
580 		}
581 		page = vm_normal_page(vma, address, pteval);
582 		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
583 			result = SCAN_PAGE_NULL;
584 			goto out;
585 		}
586 
587 		VM_BUG_ON_PAGE(!PageAnon(page), page);
588 
589 		if (page_mapcount(page) > 1) {
590 			++shared;
591 			if (cc->is_khugepaged &&
592 			    shared > khugepaged_max_ptes_shared) {
593 				result = SCAN_EXCEED_SHARED_PTE;
594 				count_vm_event(THP_SCAN_EXCEED_SHARED_PTE);
595 				goto out;
596 			}
597 		}
598 
599 		if (PageCompound(page)) {
600 			struct page *p;
601 			page = compound_head(page);
602 
603 			/*
604 			 * Check if we have dealt with the compound page
605 			 * already
606 			 */
607 			list_for_each_entry(p, compound_pagelist, lru) {
608 				if (page == p)
609 					goto next;
610 			}
611 		}
612 
613 		/*
614 		 * We can do it before isolate_lru_page because the
615 		 * page can't be freed from under us. NOTE: PG_lock
616 		 * is needed to serialize against split_huge_page
617 		 * when invoked from the VM.
618 		 */
619 		if (!trylock_page(page)) {
620 			result = SCAN_PAGE_LOCK;
621 			goto out;
622 		}
623 
624 		/*
625 		 * Check if the page has any GUP (or other external) pins.
626 		 *
627 		 * The page table that maps the page has been already unlinked
628 		 * from the page table tree and this process cannot get
629 		 * an additional pin on the page.
630 		 *
631 		 * New pins can come later if the page is shared across fork,
632 		 * but not from this process. The other process cannot write to
633 		 * the page, only trigger CoW.
634 		 */
635 		if (!is_refcount_suitable(page)) {
636 			unlock_page(page);
637 			result = SCAN_PAGE_COUNT;
638 			goto out;
639 		}
640 
641 		/*
642 		 * Isolate the page to avoid collapsing an hugepage
643 		 * currently in use by the VM.
644 		 */
645 		if (!isolate_lru_page(page)) {
646 			unlock_page(page);
647 			result = SCAN_DEL_PAGE_LRU;
648 			goto out;
649 		}
650 		mod_node_page_state(page_pgdat(page),
651 				NR_ISOLATED_ANON + page_is_file_lru(page),
652 				compound_nr(page));
653 		VM_BUG_ON_PAGE(!PageLocked(page), page);
654 		VM_BUG_ON_PAGE(PageLRU(page), page);
655 
656 		if (PageCompound(page))
657 			list_add_tail(&page->lru, compound_pagelist);
658 next:
659 		/*
660 		 * If collapse was initiated by khugepaged, check that there is
661 		 * enough young pte to justify collapsing the page
662 		 */
663 		if (cc->is_khugepaged &&
664 		    (pte_young(pteval) || page_is_young(page) ||
665 		     PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm,
666 								     address)))
667 			referenced++;
668 
669 		if (pte_write(pteval))
670 			writable = true;
671 	}
672 
673 	if (unlikely(!writable)) {
674 		result = SCAN_PAGE_RO;
675 	} else if (unlikely(cc->is_khugepaged && !referenced)) {
676 		result = SCAN_LACK_REFERENCED_PAGE;
677 	} else {
678 		result = SCAN_SUCCEED;
679 		trace_mm_collapse_huge_page_isolate(page, none_or_zero,
680 						    referenced, writable, result);
681 		return result;
682 	}
683 out:
684 	release_pte_pages(pte, _pte, compound_pagelist);
685 	trace_mm_collapse_huge_page_isolate(page, none_or_zero,
686 					    referenced, writable, result);
687 	return result;
688 }
689 
690 static void __collapse_huge_page_copy_succeeded(pte_t *pte,
691 						struct vm_area_struct *vma,
692 						unsigned long address,
693 						spinlock_t *ptl,
694 						struct list_head *compound_pagelist)
695 {
696 	struct page *src_page;
697 	struct page *tmp;
698 	pte_t *_pte;
699 	pte_t pteval;
700 
701 	for (_pte = pte; _pte < pte + HPAGE_PMD_NR;
702 	     _pte++, address += PAGE_SIZE) {
703 		pteval = ptep_get(_pte);
704 		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
705 			add_mm_counter(vma->vm_mm, MM_ANONPAGES, 1);
706 			if (is_zero_pfn(pte_pfn(pteval))) {
707 				/*
708 				 * ptl mostly unnecessary.
709 				 */
710 				spin_lock(ptl);
711 				ptep_clear(vma->vm_mm, address, _pte);
712 				spin_unlock(ptl);
713 				ksm_might_unmap_zero_page(vma->vm_mm, pteval);
714 			}
715 		} else {
716 			src_page = pte_page(pteval);
717 			if (!PageCompound(src_page))
718 				release_pte_page(src_page);
719 			/*
720 			 * ptl mostly unnecessary, but preempt has to
721 			 * be disabled to update the per-cpu stats
722 			 * inside page_remove_rmap().
723 			 */
724 			spin_lock(ptl);
725 			ptep_clear(vma->vm_mm, address, _pte);
726 			page_remove_rmap(src_page, vma, false);
727 			spin_unlock(ptl);
728 			free_page_and_swap_cache(src_page);
729 		}
730 	}
731 
732 	list_for_each_entry_safe(src_page, tmp, compound_pagelist, lru) {
733 		list_del(&src_page->lru);
734 		mod_node_page_state(page_pgdat(src_page),
735 				    NR_ISOLATED_ANON + page_is_file_lru(src_page),
736 				    -compound_nr(src_page));
737 		unlock_page(src_page);
738 		free_swap_cache(src_page);
739 		putback_lru_page(src_page);
740 	}
741 }
742 
743 static void __collapse_huge_page_copy_failed(pte_t *pte,
744 					     pmd_t *pmd,
745 					     pmd_t orig_pmd,
746 					     struct vm_area_struct *vma,
747 					     struct list_head *compound_pagelist)
748 {
749 	spinlock_t *pmd_ptl;
750 
751 	/*
752 	 * Re-establish the PMD to point to the original page table
753 	 * entry. Restoring PMD needs to be done prior to releasing
754 	 * pages. Since pages are still isolated and locked here,
755 	 * acquiring anon_vma_lock_write is unnecessary.
756 	 */
757 	pmd_ptl = pmd_lock(vma->vm_mm, pmd);
758 	pmd_populate(vma->vm_mm, pmd, pmd_pgtable(orig_pmd));
759 	spin_unlock(pmd_ptl);
760 	/*
761 	 * Release both raw and compound pages isolated
762 	 * in __collapse_huge_page_isolate.
763 	 */
764 	release_pte_pages(pte, pte + HPAGE_PMD_NR, compound_pagelist);
765 }
766 
767 /*
768  * __collapse_huge_page_copy - attempts to copy memory contents from raw
769  * pages to a hugepage. Cleans up the raw pages if copying succeeds;
770  * otherwise restores the original page table and releases isolated raw pages.
771  * Returns SCAN_SUCCEED if copying succeeds, otherwise returns SCAN_COPY_MC.
772  *
773  * @pte: starting of the PTEs to copy from
774  * @page: the new hugepage to copy contents to
775  * @pmd: pointer to the new hugepage's PMD
776  * @orig_pmd: the original raw pages' PMD
777  * @vma: the original raw pages' virtual memory area
778  * @address: starting address to copy
779  * @ptl: lock on raw pages' PTEs
780  * @compound_pagelist: list that stores compound pages
781  */
782 static int __collapse_huge_page_copy(pte_t *pte,
783 				     struct page *page,
784 				     pmd_t *pmd,
785 				     pmd_t orig_pmd,
786 				     struct vm_area_struct *vma,
787 				     unsigned long address,
788 				     spinlock_t *ptl,
789 				     struct list_head *compound_pagelist)
790 {
791 	struct page *src_page;
792 	pte_t *_pte;
793 	pte_t pteval;
794 	unsigned long _address;
795 	int result = SCAN_SUCCEED;
796 
797 	/*
798 	 * Copying pages' contents is subject to memory poison at any iteration.
799 	 */
800 	for (_pte = pte, _address = address; _pte < pte + HPAGE_PMD_NR;
801 	     _pte++, page++, _address += PAGE_SIZE) {
802 		pteval = ptep_get(_pte);
803 		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
804 			clear_user_highpage(page, _address);
805 			continue;
806 		}
807 		src_page = pte_page(pteval);
808 		if (copy_mc_user_highpage(page, src_page, _address, vma) > 0) {
809 			result = SCAN_COPY_MC;
810 			break;
811 		}
812 	}
813 
814 	if (likely(result == SCAN_SUCCEED))
815 		__collapse_huge_page_copy_succeeded(pte, vma, address, ptl,
816 						    compound_pagelist);
817 	else
818 		__collapse_huge_page_copy_failed(pte, pmd, orig_pmd, vma,
819 						 compound_pagelist);
820 
821 	return result;
822 }
823 
824 static void khugepaged_alloc_sleep(void)
825 {
826 	DEFINE_WAIT(wait);
827 
828 	add_wait_queue(&khugepaged_wait, &wait);
829 	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
830 	schedule_timeout(msecs_to_jiffies(khugepaged_alloc_sleep_millisecs));
831 	remove_wait_queue(&khugepaged_wait, &wait);
832 }
833 
834 struct collapse_control khugepaged_collapse_control = {
835 	.is_khugepaged = true,
836 };
837 
838 static bool hpage_collapse_scan_abort(int nid, struct collapse_control *cc)
839 {
840 	int i;
841 
842 	/*
843 	 * If node_reclaim_mode is disabled, then no extra effort is made to
844 	 * allocate memory locally.
845 	 */
846 	if (!node_reclaim_enabled())
847 		return false;
848 
849 	/* If there is a count for this node already, it must be acceptable */
850 	if (cc->node_load[nid])
851 		return false;
852 
853 	for (i = 0; i < MAX_NUMNODES; i++) {
854 		if (!cc->node_load[i])
855 			continue;
856 		if (node_distance(nid, i) > node_reclaim_distance)
857 			return true;
858 	}
859 	return false;
860 }
861 
862 #define khugepaged_defrag()					\
863 	(transparent_hugepage_flags &				\
864 	 (1<<TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG))
865 
866 /* Defrag for khugepaged will enter direct reclaim/compaction if necessary */
867 static inline gfp_t alloc_hugepage_khugepaged_gfpmask(void)
868 {
869 	return khugepaged_defrag() ? GFP_TRANSHUGE : GFP_TRANSHUGE_LIGHT;
870 }
871 
872 #ifdef CONFIG_NUMA
873 static int hpage_collapse_find_target_node(struct collapse_control *cc)
874 {
875 	int nid, target_node = 0, max_value = 0;
876 
877 	/* find first node with max normal pages hit */
878 	for (nid = 0; nid < MAX_NUMNODES; nid++)
879 		if (cc->node_load[nid] > max_value) {
880 			max_value = cc->node_load[nid];
881 			target_node = nid;
882 		}
883 
884 	for_each_online_node(nid) {
885 		if (max_value == cc->node_load[nid])
886 			node_set(nid, cc->alloc_nmask);
887 	}
888 
889 	return target_node;
890 }
891 #else
892 static int hpage_collapse_find_target_node(struct collapse_control *cc)
893 {
894 	return 0;
895 }
896 #endif
897 
898 static bool hpage_collapse_alloc_page(struct page **hpage, gfp_t gfp, int node,
899 				      nodemask_t *nmask)
900 {
901 	*hpage = __alloc_pages(gfp, HPAGE_PMD_ORDER, node, nmask);
902 	if (unlikely(!*hpage)) {
903 		count_vm_event(THP_COLLAPSE_ALLOC_FAILED);
904 		return false;
905 	}
906 
907 	prep_transhuge_page(*hpage);
908 	count_vm_event(THP_COLLAPSE_ALLOC);
909 	return true;
910 }
911 
912 /*
913  * If mmap_lock temporarily dropped, revalidate vma
914  * before taking mmap_lock.
915  * Returns enum scan_result value.
916  */
917 
918 static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
919 				   bool expect_anon,
920 				   struct vm_area_struct **vmap,
921 				   struct collapse_control *cc)
922 {
923 	struct vm_area_struct *vma;
924 
925 	if (unlikely(hpage_collapse_test_exit(mm)))
926 		return SCAN_ANY_PROCESS;
927 
928 	*vmap = vma = find_vma(mm, address);
929 	if (!vma)
930 		return SCAN_VMA_NULL;
931 
932 	if (!transhuge_vma_suitable(vma, address))
933 		return SCAN_ADDRESS_RANGE;
934 	if (!hugepage_vma_check(vma, vma->vm_flags, false, false,
935 				cc->is_khugepaged))
936 		return SCAN_VMA_CHECK;
937 	/*
938 	 * Anon VMA expected, the address may be unmapped then
939 	 * remapped to file after khugepaged reaquired the mmap_lock.
940 	 *
941 	 * hugepage_vma_check may return true for qualified file
942 	 * vmas.
943 	 */
944 	if (expect_anon && (!(*vmap)->anon_vma || !vma_is_anonymous(*vmap)))
945 		return SCAN_PAGE_ANON;
946 	return SCAN_SUCCEED;
947 }
948 
949 static int find_pmd_or_thp_or_none(struct mm_struct *mm,
950 				   unsigned long address,
951 				   pmd_t **pmd)
952 {
953 	pmd_t pmde;
954 
955 	*pmd = mm_find_pmd(mm, address);
956 	if (!*pmd)
957 		return SCAN_PMD_NULL;
958 
959 	pmde = pmdp_get_lockless(*pmd);
960 	if (pmd_none(pmde))
961 		return SCAN_PMD_NONE;
962 	if (!pmd_present(pmde))
963 		return SCAN_PMD_NULL;
964 	if (pmd_trans_huge(pmde))
965 		return SCAN_PMD_MAPPED;
966 	if (pmd_devmap(pmde))
967 		return SCAN_PMD_NULL;
968 	if (pmd_bad(pmde))
969 		return SCAN_PMD_NULL;
970 	return SCAN_SUCCEED;
971 }
972 
973 static int check_pmd_still_valid(struct mm_struct *mm,
974 				 unsigned long address,
975 				 pmd_t *pmd)
976 {
977 	pmd_t *new_pmd;
978 	int result = find_pmd_or_thp_or_none(mm, address, &new_pmd);
979 
980 	if (result != SCAN_SUCCEED)
981 		return result;
982 	if (new_pmd != pmd)
983 		return SCAN_FAIL;
984 	return SCAN_SUCCEED;
985 }
986 
987 /*
988  * Bring missing pages in from swap, to complete THP collapse.
989  * Only done if hpage_collapse_scan_pmd believes it is worthwhile.
990  *
991  * Called and returns without pte mapped or spinlocks held.
992  * Returns result: if not SCAN_SUCCEED, mmap_lock has been released.
993  */
994 static int __collapse_huge_page_swapin(struct mm_struct *mm,
995 				       struct vm_area_struct *vma,
996 				       unsigned long haddr, pmd_t *pmd,
997 				       int referenced)
998 {
999 	int swapped_in = 0;
1000 	vm_fault_t ret = 0;
1001 	unsigned long address, end = haddr + (HPAGE_PMD_NR * PAGE_SIZE);
1002 	int result;
1003 	pte_t *pte = NULL;
1004 	spinlock_t *ptl;
1005 
1006 	for (address = haddr; address < end; address += PAGE_SIZE) {
1007 		struct vm_fault vmf = {
1008 			.vma = vma,
1009 			.address = address,
1010 			.pgoff = linear_page_index(vma, address),
1011 			.flags = FAULT_FLAG_ALLOW_RETRY,
1012 			.pmd = pmd,
1013 		};
1014 
1015 		if (!pte++) {
1016 			pte = pte_offset_map_nolock(mm, pmd, address, &ptl);
1017 			if (!pte) {
1018 				mmap_read_unlock(mm);
1019 				result = SCAN_PMD_NULL;
1020 				goto out;
1021 			}
1022 		}
1023 
1024 		vmf.orig_pte = ptep_get_lockless(pte);
1025 		if (!is_swap_pte(vmf.orig_pte))
1026 			continue;
1027 
1028 		vmf.pte = pte;
1029 		vmf.ptl = ptl;
1030 		ret = do_swap_page(&vmf);
1031 		/* Which unmaps pte (after perhaps re-checking the entry) */
1032 		pte = NULL;
1033 
1034 		/*
1035 		 * do_swap_page returns VM_FAULT_RETRY with released mmap_lock.
1036 		 * Note we treat VM_FAULT_RETRY as VM_FAULT_ERROR here because
1037 		 * we do not retry here and swap entry will remain in pagetable
1038 		 * resulting in later failure.
1039 		 */
1040 		if (ret & VM_FAULT_RETRY) {
1041 			/* Likely, but not guaranteed, that page lock failed */
1042 			result = SCAN_PAGE_LOCK;
1043 			goto out;
1044 		}
1045 		if (ret & VM_FAULT_ERROR) {
1046 			mmap_read_unlock(mm);
1047 			result = SCAN_FAIL;
1048 			goto out;
1049 		}
1050 		swapped_in++;
1051 	}
1052 
1053 	if (pte)
1054 		pte_unmap(pte);
1055 
1056 	/* Drain LRU cache to remove extra pin on the swapped in pages */
1057 	if (swapped_in)
1058 		lru_add_drain();
1059 
1060 	result = SCAN_SUCCEED;
1061 out:
1062 	trace_mm_collapse_huge_page_swapin(mm, swapped_in, referenced, result);
1063 	return result;
1064 }
1065 
1066 static int alloc_charge_hpage(struct page **hpage, struct mm_struct *mm,
1067 			      struct collapse_control *cc)
1068 {
1069 	gfp_t gfp = (cc->is_khugepaged ? alloc_hugepage_khugepaged_gfpmask() :
1070 		     GFP_TRANSHUGE);
1071 	int node = hpage_collapse_find_target_node(cc);
1072 	struct folio *folio;
1073 
1074 	if (!hpage_collapse_alloc_page(hpage, gfp, node, &cc->alloc_nmask))
1075 		return SCAN_ALLOC_HUGE_PAGE_FAIL;
1076 
1077 	folio = page_folio(*hpage);
1078 	if (unlikely(mem_cgroup_charge(folio, mm, gfp))) {
1079 		folio_put(folio);
1080 		*hpage = NULL;
1081 		return SCAN_CGROUP_CHARGE_FAIL;
1082 	}
1083 	count_memcg_page_event(*hpage, THP_COLLAPSE_ALLOC);
1084 
1085 	return SCAN_SUCCEED;
1086 }
1087 
1088 static int collapse_huge_page(struct mm_struct *mm, unsigned long address,
1089 			      int referenced, int unmapped,
1090 			      struct collapse_control *cc)
1091 {
1092 	LIST_HEAD(compound_pagelist);
1093 	pmd_t *pmd, _pmd;
1094 	pte_t *pte;
1095 	pgtable_t pgtable;
1096 	struct page *hpage;
1097 	spinlock_t *pmd_ptl, *pte_ptl;
1098 	int result = SCAN_FAIL;
1099 	struct vm_area_struct *vma;
1100 	struct mmu_notifier_range range;
1101 
1102 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1103 
1104 	/*
1105 	 * Before allocating the hugepage, release the mmap_lock read lock.
1106 	 * The allocation can take potentially a long time if it involves
1107 	 * sync compaction, and we do not need to hold the mmap_lock during
1108 	 * that. We will recheck the vma after taking it again in write mode.
1109 	 */
1110 	mmap_read_unlock(mm);
1111 
1112 	result = alloc_charge_hpage(&hpage, mm, cc);
1113 	if (result != SCAN_SUCCEED)
1114 		goto out_nolock;
1115 
1116 	mmap_read_lock(mm);
1117 	result = hugepage_vma_revalidate(mm, address, true, &vma, cc);
1118 	if (result != SCAN_SUCCEED) {
1119 		mmap_read_unlock(mm);
1120 		goto out_nolock;
1121 	}
1122 
1123 	result = find_pmd_or_thp_or_none(mm, address, &pmd);
1124 	if (result != SCAN_SUCCEED) {
1125 		mmap_read_unlock(mm);
1126 		goto out_nolock;
1127 	}
1128 
1129 	if (unmapped) {
1130 		/*
1131 		 * __collapse_huge_page_swapin will return with mmap_lock
1132 		 * released when it fails. So we jump out_nolock directly in
1133 		 * that case.  Continuing to collapse causes inconsistency.
1134 		 */
1135 		result = __collapse_huge_page_swapin(mm, vma, address, pmd,
1136 						     referenced);
1137 		if (result != SCAN_SUCCEED)
1138 			goto out_nolock;
1139 	}
1140 
1141 	mmap_read_unlock(mm);
1142 	/*
1143 	 * Prevent all access to pagetables with the exception of
1144 	 * gup_fast later handled by the ptep_clear_flush and the VM
1145 	 * handled by the anon_vma lock + PG_lock.
1146 	 */
1147 	mmap_write_lock(mm);
1148 	result = hugepage_vma_revalidate(mm, address, true, &vma, cc);
1149 	if (result != SCAN_SUCCEED)
1150 		goto out_up_write;
1151 	/* check if the pmd is still valid */
1152 	result = check_pmd_still_valid(mm, address, pmd);
1153 	if (result != SCAN_SUCCEED)
1154 		goto out_up_write;
1155 
1156 	vma_start_write(vma);
1157 	anon_vma_lock_write(vma->anon_vma);
1158 
1159 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, address,
1160 				address + HPAGE_PMD_SIZE);
1161 	mmu_notifier_invalidate_range_start(&range);
1162 
1163 	pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */
1164 	/*
1165 	 * This removes any huge TLB entry from the CPU so we won't allow
1166 	 * huge and small TLB entries for the same virtual address to
1167 	 * avoid the risk of CPU bugs in that area.
1168 	 *
1169 	 * Parallel fast GUP is fine since fast GUP will back off when
1170 	 * it detects PMD is changed.
1171 	 */
1172 	_pmd = pmdp_collapse_flush(vma, address, pmd);
1173 	spin_unlock(pmd_ptl);
1174 	mmu_notifier_invalidate_range_end(&range);
1175 	tlb_remove_table_sync_one();
1176 
1177 	pte = pte_offset_map_lock(mm, &_pmd, address, &pte_ptl);
1178 	if (pte) {
1179 		result = __collapse_huge_page_isolate(vma, address, pte, cc,
1180 						      &compound_pagelist);
1181 		spin_unlock(pte_ptl);
1182 	} else {
1183 		result = SCAN_PMD_NULL;
1184 	}
1185 
1186 	if (unlikely(result != SCAN_SUCCEED)) {
1187 		if (pte)
1188 			pte_unmap(pte);
1189 		spin_lock(pmd_ptl);
1190 		BUG_ON(!pmd_none(*pmd));
1191 		/*
1192 		 * We can only use set_pmd_at when establishing
1193 		 * hugepmds and never for establishing regular pmds that
1194 		 * points to regular pagetables. Use pmd_populate for that
1195 		 */
1196 		pmd_populate(mm, pmd, pmd_pgtable(_pmd));
1197 		spin_unlock(pmd_ptl);
1198 		anon_vma_unlock_write(vma->anon_vma);
1199 		goto out_up_write;
1200 	}
1201 
1202 	/*
1203 	 * All pages are isolated and locked so anon_vma rmap
1204 	 * can't run anymore.
1205 	 */
1206 	anon_vma_unlock_write(vma->anon_vma);
1207 
1208 	result = __collapse_huge_page_copy(pte, hpage, pmd, _pmd,
1209 					   vma, address, pte_ptl,
1210 					   &compound_pagelist);
1211 	pte_unmap(pte);
1212 	if (unlikely(result != SCAN_SUCCEED))
1213 		goto out_up_write;
1214 
1215 	/*
1216 	 * spin_lock() below is not the equivalent of smp_wmb(), but
1217 	 * the smp_wmb() inside __SetPageUptodate() can be reused to
1218 	 * avoid the copy_huge_page writes to become visible after
1219 	 * the set_pmd_at() write.
1220 	 */
1221 	__SetPageUptodate(hpage);
1222 	pgtable = pmd_pgtable(_pmd);
1223 
1224 	_pmd = mk_huge_pmd(hpage, vma->vm_page_prot);
1225 	_pmd = maybe_pmd_mkwrite(pmd_mkdirty(_pmd), vma);
1226 
1227 	spin_lock(pmd_ptl);
1228 	BUG_ON(!pmd_none(*pmd));
1229 	page_add_new_anon_rmap(hpage, vma, address);
1230 	lru_cache_add_inactive_or_unevictable(hpage, vma);
1231 	pgtable_trans_huge_deposit(mm, pmd, pgtable);
1232 	set_pmd_at(mm, address, pmd, _pmd);
1233 	update_mmu_cache_pmd(vma, address, pmd);
1234 	spin_unlock(pmd_ptl);
1235 
1236 	hpage = NULL;
1237 
1238 	result = SCAN_SUCCEED;
1239 out_up_write:
1240 	mmap_write_unlock(mm);
1241 out_nolock:
1242 	if (hpage)
1243 		put_page(hpage);
1244 	trace_mm_collapse_huge_page(mm, result == SCAN_SUCCEED, result);
1245 	return result;
1246 }
1247 
1248 static int hpage_collapse_scan_pmd(struct mm_struct *mm,
1249 				   struct vm_area_struct *vma,
1250 				   unsigned long address, bool *mmap_locked,
1251 				   struct collapse_control *cc)
1252 {
1253 	pmd_t *pmd;
1254 	pte_t *pte, *_pte;
1255 	int result = SCAN_FAIL, referenced = 0;
1256 	int none_or_zero = 0, shared = 0;
1257 	struct page *page = NULL;
1258 	unsigned long _address;
1259 	spinlock_t *ptl;
1260 	int node = NUMA_NO_NODE, unmapped = 0;
1261 	bool writable = false;
1262 
1263 	VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1264 
1265 	result = find_pmd_or_thp_or_none(mm, address, &pmd);
1266 	if (result != SCAN_SUCCEED)
1267 		goto out;
1268 
1269 	memset(cc->node_load, 0, sizeof(cc->node_load));
1270 	nodes_clear(cc->alloc_nmask);
1271 	pte = pte_offset_map_lock(mm, pmd, address, &ptl);
1272 	if (!pte) {
1273 		result = SCAN_PMD_NULL;
1274 		goto out;
1275 	}
1276 
1277 	for (_address = address, _pte = pte; _pte < pte + HPAGE_PMD_NR;
1278 	     _pte++, _address += PAGE_SIZE) {
1279 		pte_t pteval = ptep_get(_pte);
1280 		if (is_swap_pte(pteval)) {
1281 			++unmapped;
1282 			if (!cc->is_khugepaged ||
1283 			    unmapped <= khugepaged_max_ptes_swap) {
1284 				/*
1285 				 * Always be strict with uffd-wp
1286 				 * enabled swap entries.  Please see
1287 				 * comment below for pte_uffd_wp().
1288 				 */
1289 				if (pte_swp_uffd_wp_any(pteval)) {
1290 					result = SCAN_PTE_UFFD_WP;
1291 					goto out_unmap;
1292 				}
1293 				continue;
1294 			} else {
1295 				result = SCAN_EXCEED_SWAP_PTE;
1296 				count_vm_event(THP_SCAN_EXCEED_SWAP_PTE);
1297 				goto out_unmap;
1298 			}
1299 		}
1300 		if (pte_none(pteval) || is_zero_pfn(pte_pfn(pteval))) {
1301 			++none_or_zero;
1302 			if (!userfaultfd_armed(vma) &&
1303 			    (!cc->is_khugepaged ||
1304 			     none_or_zero <= khugepaged_max_ptes_none)) {
1305 				continue;
1306 			} else {
1307 				result = SCAN_EXCEED_NONE_PTE;
1308 				count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
1309 				goto out_unmap;
1310 			}
1311 		}
1312 		if (pte_uffd_wp(pteval)) {
1313 			/*
1314 			 * Don't collapse the page if any of the small
1315 			 * PTEs are armed with uffd write protection.
1316 			 * Here we can also mark the new huge pmd as
1317 			 * write protected if any of the small ones is
1318 			 * marked but that could bring unknown
1319 			 * userfault messages that falls outside of
1320 			 * the registered range.  So, just be simple.
1321 			 */
1322 			result = SCAN_PTE_UFFD_WP;
1323 			goto out_unmap;
1324 		}
1325 		if (pte_write(pteval))
1326 			writable = true;
1327 
1328 		page = vm_normal_page(vma, _address, pteval);
1329 		if (unlikely(!page) || unlikely(is_zone_device_page(page))) {
1330 			result = SCAN_PAGE_NULL;
1331 			goto out_unmap;
1332 		}
1333 
1334 		if (page_mapcount(page) > 1) {
1335 			++shared;
1336 			if (cc->is_khugepaged &&
1337 			    shared > khugepaged_max_ptes_shared) {
1338 				result = SCAN_EXCEED_SHARED_PTE;
1339 				count_vm_event(THP_SCAN_EXCEED_SHARED_PTE);
1340 				goto out_unmap;
1341 			}
1342 		}
1343 
1344 		page = compound_head(page);
1345 
1346 		/*
1347 		 * Record which node the original page is from and save this
1348 		 * information to cc->node_load[].
1349 		 * Khugepaged will allocate hugepage from the node has the max
1350 		 * hit record.
1351 		 */
1352 		node = page_to_nid(page);
1353 		if (hpage_collapse_scan_abort(node, cc)) {
1354 			result = SCAN_SCAN_ABORT;
1355 			goto out_unmap;
1356 		}
1357 		cc->node_load[node]++;
1358 		if (!PageLRU(page)) {
1359 			result = SCAN_PAGE_LRU;
1360 			goto out_unmap;
1361 		}
1362 		if (PageLocked(page)) {
1363 			result = SCAN_PAGE_LOCK;
1364 			goto out_unmap;
1365 		}
1366 		if (!PageAnon(page)) {
1367 			result = SCAN_PAGE_ANON;
1368 			goto out_unmap;
1369 		}
1370 
1371 		/*
1372 		 * Check if the page has any GUP (or other external) pins.
1373 		 *
1374 		 * Here the check may be racy:
1375 		 * it may see total_mapcount > refcount in some cases?
1376 		 * But such case is ephemeral we could always retry collapse
1377 		 * later.  However it may report false positive if the page
1378 		 * has excessive GUP pins (i.e. 512).  Anyway the same check
1379 		 * will be done again later the risk seems low.
1380 		 */
1381 		if (!is_refcount_suitable(page)) {
1382 			result = SCAN_PAGE_COUNT;
1383 			goto out_unmap;
1384 		}
1385 
1386 		/*
1387 		 * If collapse was initiated by khugepaged, check that there is
1388 		 * enough young pte to justify collapsing the page
1389 		 */
1390 		if (cc->is_khugepaged &&
1391 		    (pte_young(pteval) || page_is_young(page) ||
1392 		     PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm,
1393 								     address)))
1394 			referenced++;
1395 	}
1396 	if (!writable) {
1397 		result = SCAN_PAGE_RO;
1398 	} else if (cc->is_khugepaged &&
1399 		   (!referenced ||
1400 		    (unmapped && referenced < HPAGE_PMD_NR / 2))) {
1401 		result = SCAN_LACK_REFERENCED_PAGE;
1402 	} else {
1403 		result = SCAN_SUCCEED;
1404 	}
1405 out_unmap:
1406 	pte_unmap_unlock(pte, ptl);
1407 	if (result == SCAN_SUCCEED) {
1408 		result = collapse_huge_page(mm, address, referenced,
1409 					    unmapped, cc);
1410 		/* collapse_huge_page will return with the mmap_lock released */
1411 		*mmap_locked = false;
1412 	}
1413 out:
1414 	trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced,
1415 				     none_or_zero, result, unmapped);
1416 	return result;
1417 }
1418 
1419 static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot)
1420 {
1421 	struct mm_slot *slot = &mm_slot->slot;
1422 	struct mm_struct *mm = slot->mm;
1423 
1424 	lockdep_assert_held(&khugepaged_mm_lock);
1425 
1426 	if (hpage_collapse_test_exit(mm)) {
1427 		/* free mm_slot */
1428 		hash_del(&slot->hash);
1429 		list_del(&slot->mm_node);
1430 
1431 		/*
1432 		 * Not strictly needed because the mm exited already.
1433 		 *
1434 		 * clear_bit(MMF_VM_HUGEPAGE, &mm->flags);
1435 		 */
1436 
1437 		/* khugepaged_mm_lock actually not necessary for the below */
1438 		mm_slot_free(mm_slot_cache, mm_slot);
1439 		mmdrop(mm);
1440 	}
1441 }
1442 
1443 #ifdef CONFIG_SHMEM
1444 /*
1445  * Notify khugepaged that given addr of the mm is pte-mapped THP. Then
1446  * khugepaged should try to collapse the page table.
1447  *
1448  * Note that following race exists:
1449  * (1) khugepaged calls khugepaged_collapse_pte_mapped_thps() for mm_struct A,
1450  *     emptying the A's ->pte_mapped_thp[] array.
1451  * (2) MADV_COLLAPSE collapses some file extent with target mm_struct B, and
1452  *     retract_page_tables() finds a VMA in mm_struct A mapping the same extent
1453  *     (at virtual address X) and adds an entry (for X) into mm_struct A's
1454  *     ->pte-mapped_thp[] array.
1455  * (3) khugepaged calls khugepaged_collapse_scan_file() for mm_struct A at X,
1456  *     sees a pte-mapped THP (SCAN_PTE_MAPPED_HUGEPAGE) and adds an entry
1457  *     (for X) into mm_struct A's ->pte-mapped_thp[] array.
1458  * Thus, it's possible the same address is added multiple times for the same
1459  * mm_struct.  Should this happen, we'll simply attempt
1460  * collapse_pte_mapped_thp() multiple times for the same address, under the same
1461  * exclusive mmap_lock, and assuming the first call is successful, subsequent
1462  * attempts will return quickly (without grabbing any additional locks) when
1463  * a huge pmd is found in find_pmd_or_thp_or_none().  Since this is a cheap
1464  * check, and since this is a rare occurrence, the cost of preventing this
1465  * "multiple-add" is thought to be more expensive than just handling it, should
1466  * it occur.
1467  */
1468 static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
1469 					  unsigned long addr)
1470 {
1471 	struct khugepaged_mm_slot *mm_slot;
1472 	struct mm_slot *slot;
1473 	bool ret = false;
1474 
1475 	VM_BUG_ON(addr & ~HPAGE_PMD_MASK);
1476 
1477 	spin_lock(&khugepaged_mm_lock);
1478 	slot = mm_slot_lookup(mm_slots_hash, mm);
1479 	mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
1480 	if (likely(mm_slot && mm_slot->nr_pte_mapped_thp < MAX_PTE_MAPPED_THP)) {
1481 		mm_slot->pte_mapped_thp[mm_slot->nr_pte_mapped_thp++] = addr;
1482 		ret = true;
1483 	}
1484 	spin_unlock(&khugepaged_mm_lock);
1485 	return ret;
1486 }
1487 
1488 /* hpage must be locked, and mmap_lock must be held in write */
1489 static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr,
1490 			pmd_t *pmdp, struct page *hpage)
1491 {
1492 	struct vm_fault vmf = {
1493 		.vma = vma,
1494 		.address = addr,
1495 		.flags = 0,
1496 		.pmd = pmdp,
1497 	};
1498 
1499 	VM_BUG_ON(!PageTransHuge(hpage));
1500 	mmap_assert_write_locked(vma->vm_mm);
1501 
1502 	if (do_set_pmd(&vmf, hpage))
1503 		return SCAN_FAIL;
1504 
1505 	get_page(hpage);
1506 	return SCAN_SUCCEED;
1507 }
1508 
1509 /*
1510  * A note about locking:
1511  * Trying to take the page table spinlocks would be useless here because those
1512  * are only used to synchronize:
1513  *
1514  *  - modifying terminal entries (ones that point to a data page, not to another
1515  *    page table)
1516  *  - installing *new* non-terminal entries
1517  *
1518  * Instead, we need roughly the same kind of protection as free_pgtables() or
1519  * mm_take_all_locks() (but only for a single VMA):
1520  * The mmap lock together with this VMA's rmap locks covers all paths towards
1521  * the page table entries we're messing with here, except for hardware page
1522  * table walks and lockless_pages_from_mm().
1523  */
1524 static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
1525 				  unsigned long addr, pmd_t *pmdp)
1526 {
1527 	pmd_t pmd;
1528 	struct mmu_notifier_range range;
1529 
1530 	mmap_assert_write_locked(mm);
1531 	if (vma->vm_file)
1532 		lockdep_assert_held_write(&vma->vm_file->f_mapping->i_mmap_rwsem);
1533 	/*
1534 	 * All anon_vmas attached to the VMA have the same root and are
1535 	 * therefore locked by the same lock.
1536 	 */
1537 	if (vma->anon_vma)
1538 		lockdep_assert_held_write(&vma->anon_vma->root->rwsem);
1539 
1540 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, mm, addr,
1541 				addr + HPAGE_PMD_SIZE);
1542 	mmu_notifier_invalidate_range_start(&range);
1543 	pmd = pmdp_collapse_flush(vma, addr, pmdp);
1544 	tlb_remove_table_sync_one();
1545 	mmu_notifier_invalidate_range_end(&range);
1546 	mm_dec_nr_ptes(mm);
1547 	page_table_check_pte_clear_range(mm, addr, pmd);
1548 	pte_free(mm, pmd_pgtable(pmd));
1549 }
1550 
1551 /**
1552  * collapse_pte_mapped_thp - Try to collapse a pte-mapped THP for mm at
1553  * address haddr.
1554  *
1555  * @mm: process address space where collapse happens
1556  * @addr: THP collapse address
1557  * @install_pmd: If a huge PMD should be installed
1558  *
1559  * This function checks whether all the PTEs in the PMD are pointing to the
1560  * right THP. If so, retract the page table so the THP can refault in with
1561  * as pmd-mapped. Possibly install a huge PMD mapping the THP.
1562  */
1563 int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
1564 			    bool install_pmd)
1565 {
1566 	unsigned long haddr = addr & HPAGE_PMD_MASK;
1567 	struct vm_area_struct *vma = vma_lookup(mm, haddr);
1568 	struct page *hpage;
1569 	pte_t *start_pte, *pte;
1570 	pmd_t *pmd;
1571 	spinlock_t *ptl;
1572 	int count = 0, result = SCAN_FAIL;
1573 	int i;
1574 
1575 	mmap_assert_write_locked(mm);
1576 
1577 	/* Fast check before locking page if already PMD-mapped */
1578 	result = find_pmd_or_thp_or_none(mm, haddr, &pmd);
1579 	if (result == SCAN_PMD_MAPPED)
1580 		return result;
1581 
1582 	if (!vma || !vma->vm_file ||
1583 	    !range_in_vma(vma, haddr, haddr + HPAGE_PMD_SIZE))
1584 		return SCAN_VMA_CHECK;
1585 
1586 	/*
1587 	 * If we are here, we've succeeded in replacing all the native pages
1588 	 * in the page cache with a single hugepage. If a mm were to fault-in
1589 	 * this memory (mapped by a suitably aligned VMA), we'd get the hugepage
1590 	 * and map it by a PMD, regardless of sysfs THP settings. As such, let's
1591 	 * analogously elide sysfs THP settings here.
1592 	 */
1593 	if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false))
1594 		return SCAN_VMA_CHECK;
1595 
1596 	/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
1597 	if (userfaultfd_wp(vma))
1598 		return SCAN_PTE_UFFD_WP;
1599 
1600 	hpage = find_lock_page(vma->vm_file->f_mapping,
1601 			       linear_page_index(vma, haddr));
1602 	if (!hpage)
1603 		return SCAN_PAGE_NULL;
1604 
1605 	if (!PageHead(hpage)) {
1606 		result = SCAN_FAIL;
1607 		goto drop_hpage;
1608 	}
1609 
1610 	if (compound_order(hpage) != HPAGE_PMD_ORDER) {
1611 		result = SCAN_PAGE_COMPOUND;
1612 		goto drop_hpage;
1613 	}
1614 
1615 	switch (result) {
1616 	case SCAN_SUCCEED:
1617 		break;
1618 	case SCAN_PMD_NONE:
1619 		/*
1620 		 * In MADV_COLLAPSE path, possible race with khugepaged where
1621 		 * all pte entries have been removed and pmd cleared.  If so,
1622 		 * skip all the pte checks and just update the pmd mapping.
1623 		 */
1624 		goto maybe_install_pmd;
1625 	default:
1626 		goto drop_hpage;
1627 	}
1628 
1629 	/* Lock the vma before taking i_mmap and page table locks */
1630 	vma_start_write(vma);
1631 
1632 	/*
1633 	 * We need to lock the mapping so that from here on, only GUP-fast and
1634 	 * hardware page walks can access the parts of the page tables that
1635 	 * we're operating on.
1636 	 * See collapse_and_free_pmd().
1637 	 */
1638 	i_mmap_lock_write(vma->vm_file->f_mapping);
1639 
1640 	/*
1641 	 * This spinlock should be unnecessary: Nobody else should be accessing
1642 	 * the page tables under spinlock protection here, only
1643 	 * lockless_pages_from_mm() and the hardware page walker can access page
1644 	 * tables while all the high-level locks are held in write mode.
1645 	 */
1646 	result = SCAN_FAIL;
1647 	start_pte = pte_offset_map_lock(mm, pmd, haddr, &ptl);
1648 	if (!start_pte)
1649 		goto drop_immap;
1650 
1651 	/* step 1: check all mapped PTEs are to the right huge page */
1652 	for (i = 0, addr = haddr, pte = start_pte;
1653 	     i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1654 		struct page *page;
1655 		pte_t ptent = ptep_get(pte);
1656 
1657 		/* empty pte, skip */
1658 		if (pte_none(ptent))
1659 			continue;
1660 
1661 		/* page swapped out, abort */
1662 		if (!pte_present(ptent)) {
1663 			result = SCAN_PTE_NON_PRESENT;
1664 			goto abort;
1665 		}
1666 
1667 		page = vm_normal_page(vma, addr, ptent);
1668 		if (WARN_ON_ONCE(page && is_zone_device_page(page)))
1669 			page = NULL;
1670 		/*
1671 		 * Note that uprobe, debugger, or MAP_PRIVATE may change the
1672 		 * page table, but the new page will not be a subpage of hpage.
1673 		 */
1674 		if (hpage + i != page)
1675 			goto abort;
1676 		count++;
1677 	}
1678 
1679 	/* step 2: adjust rmap */
1680 	for (i = 0, addr = haddr, pte = start_pte;
1681 	     i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
1682 		struct page *page;
1683 		pte_t ptent = ptep_get(pte);
1684 
1685 		if (pte_none(ptent))
1686 			continue;
1687 		page = vm_normal_page(vma, addr, ptent);
1688 		if (WARN_ON_ONCE(page && is_zone_device_page(page)))
1689 			goto abort;
1690 		page_remove_rmap(page, vma, false);
1691 	}
1692 
1693 	pte_unmap_unlock(start_pte, ptl);
1694 
1695 	/* step 3: set proper refcount and mm_counters. */
1696 	if (count) {
1697 		page_ref_sub(hpage, count);
1698 		add_mm_counter(vma->vm_mm, mm_counter_file(hpage), -count);
1699 	}
1700 
1701 	/* step 4: remove pte entries */
1702 	/* we make no change to anon, but protect concurrent anon page lookup */
1703 	if (vma->anon_vma)
1704 		anon_vma_lock_write(vma->anon_vma);
1705 
1706 	collapse_and_free_pmd(mm, vma, haddr, pmd);
1707 
1708 	if (vma->anon_vma)
1709 		anon_vma_unlock_write(vma->anon_vma);
1710 	i_mmap_unlock_write(vma->vm_file->f_mapping);
1711 
1712 maybe_install_pmd:
1713 	/* step 5: install pmd entry */
1714 	result = install_pmd
1715 			? set_huge_pmd(vma, haddr, pmd, hpage)
1716 			: SCAN_SUCCEED;
1717 
1718 drop_hpage:
1719 	unlock_page(hpage);
1720 	put_page(hpage);
1721 	return result;
1722 
1723 abort:
1724 	pte_unmap_unlock(start_pte, ptl);
1725 drop_immap:
1726 	i_mmap_unlock_write(vma->vm_file->f_mapping);
1727 	goto drop_hpage;
1728 }
1729 
1730 static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot)
1731 {
1732 	struct mm_slot *slot = &mm_slot->slot;
1733 	struct mm_struct *mm = slot->mm;
1734 	int i;
1735 
1736 	if (likely(mm_slot->nr_pte_mapped_thp == 0))
1737 		return;
1738 
1739 	if (!mmap_write_trylock(mm))
1740 		return;
1741 
1742 	if (unlikely(hpage_collapse_test_exit(mm)))
1743 		goto out;
1744 
1745 	for (i = 0; i < mm_slot->nr_pte_mapped_thp; i++)
1746 		collapse_pte_mapped_thp(mm, mm_slot->pte_mapped_thp[i], false);
1747 
1748 out:
1749 	mm_slot->nr_pte_mapped_thp = 0;
1750 	mmap_write_unlock(mm);
1751 }
1752 
1753 static int retract_page_tables(struct address_space *mapping, pgoff_t pgoff,
1754 			       struct mm_struct *target_mm,
1755 			       unsigned long target_addr, struct page *hpage,
1756 			       struct collapse_control *cc)
1757 {
1758 	struct vm_area_struct *vma;
1759 	int target_result = SCAN_FAIL;
1760 
1761 	i_mmap_lock_write(mapping);
1762 	vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
1763 		int result = SCAN_FAIL;
1764 		struct mm_struct *mm = NULL;
1765 		unsigned long addr = 0;
1766 		pmd_t *pmd;
1767 		bool is_target = false;
1768 
1769 		/*
1770 		 * Check vma->anon_vma to exclude MAP_PRIVATE mappings that
1771 		 * got written to. These VMAs are likely not worth investing
1772 		 * mmap_write_lock(mm) as PMD-mapping is likely to be split
1773 		 * later.
1774 		 *
1775 		 * Note that vma->anon_vma check is racy: it can be set up after
1776 		 * the check but before we took mmap_lock by the fault path.
1777 		 * But page lock would prevent establishing any new ptes of the
1778 		 * page, so we are safe.
1779 		 *
1780 		 * An alternative would be drop the check, but check that page
1781 		 * table is clear before calling pmdp_collapse_flush() under
1782 		 * ptl. It has higher chance to recover THP for the VMA, but
1783 		 * has higher cost too. It would also probably require locking
1784 		 * the anon_vma.
1785 		 */
1786 		if (READ_ONCE(vma->anon_vma)) {
1787 			result = SCAN_PAGE_ANON;
1788 			goto next;
1789 		}
1790 		addr = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
1791 		if (addr & ~HPAGE_PMD_MASK ||
1792 		    vma->vm_end < addr + HPAGE_PMD_SIZE) {
1793 			result = SCAN_VMA_CHECK;
1794 			goto next;
1795 		}
1796 		mm = vma->vm_mm;
1797 		is_target = mm == target_mm && addr == target_addr;
1798 		result = find_pmd_or_thp_or_none(mm, addr, &pmd);
1799 		if (result != SCAN_SUCCEED)
1800 			goto next;
1801 		/*
1802 		 * We need exclusive mmap_lock to retract page table.
1803 		 *
1804 		 * We use trylock due to lock inversion: we need to acquire
1805 		 * mmap_lock while holding page lock. Fault path does it in
1806 		 * reverse order. Trylock is a way to avoid deadlock.
1807 		 *
1808 		 * Also, it's not MADV_COLLAPSE's job to collapse other
1809 		 * mappings - let khugepaged take care of them later.
1810 		 */
1811 		result = SCAN_PTE_MAPPED_HUGEPAGE;
1812 		if ((cc->is_khugepaged || is_target) &&
1813 		    mmap_write_trylock(mm)) {
1814 			/* trylock for the same lock inversion as above */
1815 			if (!vma_try_start_write(vma))
1816 				goto unlock_next;
1817 
1818 			/*
1819 			 * Re-check whether we have an ->anon_vma, because
1820 			 * collapse_and_free_pmd() requires that either no
1821 			 * ->anon_vma exists or the anon_vma is locked.
1822 			 * We already checked ->anon_vma above, but that check
1823 			 * is racy because ->anon_vma can be populated under the
1824 			 * mmap lock in read mode.
1825 			 */
1826 			if (vma->anon_vma) {
1827 				result = SCAN_PAGE_ANON;
1828 				goto unlock_next;
1829 			}
1830 			/*
1831 			 * When a vma is registered with uffd-wp, we can't
1832 			 * recycle the pmd pgtable because there can be pte
1833 			 * markers installed.  Skip it only, so the rest mm/vma
1834 			 * can still have the same file mapped hugely, however
1835 			 * it'll always mapped in small page size for uffd-wp
1836 			 * registered ranges.
1837 			 */
1838 			if (hpage_collapse_test_exit(mm)) {
1839 				result = SCAN_ANY_PROCESS;
1840 				goto unlock_next;
1841 			}
1842 			if (userfaultfd_wp(vma)) {
1843 				result = SCAN_PTE_UFFD_WP;
1844 				goto unlock_next;
1845 			}
1846 			collapse_and_free_pmd(mm, vma, addr, pmd);
1847 			if (!cc->is_khugepaged && is_target)
1848 				result = set_huge_pmd(vma, addr, pmd, hpage);
1849 			else
1850 				result = SCAN_SUCCEED;
1851 
1852 unlock_next:
1853 			mmap_write_unlock(mm);
1854 			goto next;
1855 		}
1856 		/*
1857 		 * Calling context will handle target mm/addr. Otherwise, let
1858 		 * khugepaged try again later.
1859 		 */
1860 		if (!is_target) {
1861 			khugepaged_add_pte_mapped_thp(mm, addr);
1862 			continue;
1863 		}
1864 next:
1865 		if (is_target)
1866 			target_result = result;
1867 	}
1868 	i_mmap_unlock_write(mapping);
1869 	return target_result;
1870 }
1871 
1872 /**
1873  * collapse_file - collapse filemap/tmpfs/shmem pages into huge one.
1874  *
1875  * @mm: process address space where collapse happens
1876  * @addr: virtual collapse start address
1877  * @file: file that collapse on
1878  * @start: collapse start address
1879  * @cc: collapse context and scratchpad
1880  *
1881  * Basic scheme is simple, details are more complex:
1882  *  - allocate and lock a new huge page;
1883  *  - scan page cache, locking old pages
1884  *    + swap/gup in pages if necessary;
1885  *  - copy data to new page
1886  *  - handle shmem holes
1887  *    + re-validate that holes weren't filled by someone else
1888  *    + check for userfaultfd
1889  *  - finalize updates to the page cache;
1890  *  - if replacing succeeds:
1891  *    + unlock huge page;
1892  *    + free old pages;
1893  *  - if replacing failed;
1894  *    + unlock old pages
1895  *    + unlock and free huge page;
1896  */
1897 static int collapse_file(struct mm_struct *mm, unsigned long addr,
1898 			 struct file *file, pgoff_t start,
1899 			 struct collapse_control *cc)
1900 {
1901 	struct address_space *mapping = file->f_mapping;
1902 	struct page *hpage;
1903 	struct page *page;
1904 	struct page *tmp;
1905 	struct folio *folio;
1906 	pgoff_t index = 0, end = start + HPAGE_PMD_NR;
1907 	LIST_HEAD(pagelist);
1908 	XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER);
1909 	int nr_none = 0, result = SCAN_SUCCEED;
1910 	bool is_shmem = shmem_file(file);
1911 	int nr = 0;
1912 
1913 	VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem);
1914 	VM_BUG_ON(start & (HPAGE_PMD_NR - 1));
1915 
1916 	result = alloc_charge_hpage(&hpage, mm, cc);
1917 	if (result != SCAN_SUCCEED)
1918 		goto out;
1919 
1920 	__SetPageLocked(hpage);
1921 	if (is_shmem)
1922 		__SetPageSwapBacked(hpage);
1923 	hpage->index = start;
1924 	hpage->mapping = mapping;
1925 
1926 	/*
1927 	 * Ensure we have slots for all the pages in the range.  This is
1928 	 * almost certainly a no-op because most of the pages must be present
1929 	 */
1930 	do {
1931 		xas_lock_irq(&xas);
1932 		xas_create_range(&xas);
1933 		if (!xas_error(&xas))
1934 			break;
1935 		xas_unlock_irq(&xas);
1936 		if (!xas_nomem(&xas, GFP_KERNEL)) {
1937 			result = SCAN_FAIL;
1938 			goto rollback;
1939 		}
1940 	} while (1);
1941 
1942 	for (index = start; index < end; index++) {
1943 		xas_set(&xas, index);
1944 		page = xas_load(&xas);
1945 
1946 		VM_BUG_ON(index != xas.xa_index);
1947 		if (is_shmem) {
1948 			if (!page) {
1949 				/*
1950 				 * Stop if extent has been truncated or
1951 				 * hole-punched, and is now completely
1952 				 * empty.
1953 				 */
1954 				if (index == start) {
1955 					if (!xas_next_entry(&xas, end - 1)) {
1956 						result = SCAN_TRUNCATED;
1957 						goto xa_locked;
1958 					}
1959 				}
1960 				if (!shmem_charge(mapping->host, 1)) {
1961 					result = SCAN_FAIL;
1962 					goto xa_locked;
1963 				}
1964 				nr_none++;
1965 				continue;
1966 			}
1967 
1968 			if (xa_is_value(page) || !PageUptodate(page)) {
1969 				xas_unlock_irq(&xas);
1970 				/* swap in or instantiate fallocated page */
1971 				if (shmem_get_folio(mapping->host, index,
1972 						&folio, SGP_NOALLOC)) {
1973 					result = SCAN_FAIL;
1974 					goto xa_unlocked;
1975 				}
1976 				/* drain lru cache to help isolate_lru_page() */
1977 				lru_add_drain();
1978 				page = folio_file_page(folio, index);
1979 			} else if (trylock_page(page)) {
1980 				get_page(page);
1981 				xas_unlock_irq(&xas);
1982 			} else {
1983 				result = SCAN_PAGE_LOCK;
1984 				goto xa_locked;
1985 			}
1986 		} else {	/* !is_shmem */
1987 			if (!page || xa_is_value(page)) {
1988 				xas_unlock_irq(&xas);
1989 				page_cache_sync_readahead(mapping, &file->f_ra,
1990 							  file, index,
1991 							  end - index);
1992 				/* drain lru cache to help isolate_lru_page() */
1993 				lru_add_drain();
1994 				page = find_lock_page(mapping, index);
1995 				if (unlikely(page == NULL)) {
1996 					result = SCAN_FAIL;
1997 					goto xa_unlocked;
1998 				}
1999 			} else if (PageDirty(page)) {
2000 				/*
2001 				 * khugepaged only works on read-only fd,
2002 				 * so this page is dirty because it hasn't
2003 				 * been flushed since first write. There
2004 				 * won't be new dirty pages.
2005 				 *
2006 				 * Trigger async flush here and hope the
2007 				 * writeback is done when khugepaged
2008 				 * revisits this page.
2009 				 *
2010 				 * This is a one-off situation. We are not
2011 				 * forcing writeback in loop.
2012 				 */
2013 				xas_unlock_irq(&xas);
2014 				filemap_flush(mapping);
2015 				result = SCAN_FAIL;
2016 				goto xa_unlocked;
2017 			} else if (PageWriteback(page)) {
2018 				xas_unlock_irq(&xas);
2019 				result = SCAN_FAIL;
2020 				goto xa_unlocked;
2021 			} else if (trylock_page(page)) {
2022 				get_page(page);
2023 				xas_unlock_irq(&xas);
2024 			} else {
2025 				result = SCAN_PAGE_LOCK;
2026 				goto xa_locked;
2027 			}
2028 		}
2029 
2030 		/*
2031 		 * The page must be locked, so we can drop the i_pages lock
2032 		 * without racing with truncate.
2033 		 */
2034 		VM_BUG_ON_PAGE(!PageLocked(page), page);
2035 
2036 		/* make sure the page is up to date */
2037 		if (unlikely(!PageUptodate(page))) {
2038 			result = SCAN_FAIL;
2039 			goto out_unlock;
2040 		}
2041 
2042 		/*
2043 		 * If file was truncated then extended, or hole-punched, before
2044 		 * we locked the first page, then a THP might be there already.
2045 		 * This will be discovered on the first iteration.
2046 		 */
2047 		if (PageTransCompound(page)) {
2048 			struct page *head = compound_head(page);
2049 
2050 			result = compound_order(head) == HPAGE_PMD_ORDER &&
2051 					head->index == start
2052 					/* Maybe PMD-mapped */
2053 					? SCAN_PTE_MAPPED_HUGEPAGE
2054 					: SCAN_PAGE_COMPOUND;
2055 			goto out_unlock;
2056 		}
2057 
2058 		folio = page_folio(page);
2059 
2060 		if (folio_mapping(folio) != mapping) {
2061 			result = SCAN_TRUNCATED;
2062 			goto out_unlock;
2063 		}
2064 
2065 		if (!is_shmem && (folio_test_dirty(folio) ||
2066 				  folio_test_writeback(folio))) {
2067 			/*
2068 			 * khugepaged only works on read-only fd, so this
2069 			 * page is dirty because it hasn't been flushed
2070 			 * since first write.
2071 			 */
2072 			result = SCAN_FAIL;
2073 			goto out_unlock;
2074 		}
2075 
2076 		if (!folio_isolate_lru(folio)) {
2077 			result = SCAN_DEL_PAGE_LRU;
2078 			goto out_unlock;
2079 		}
2080 
2081 		if (!filemap_release_folio(folio, GFP_KERNEL)) {
2082 			result = SCAN_PAGE_HAS_PRIVATE;
2083 			folio_putback_lru(folio);
2084 			goto out_unlock;
2085 		}
2086 
2087 		if (folio_mapped(folio))
2088 			try_to_unmap(folio,
2089 					TTU_IGNORE_MLOCK | TTU_BATCH_FLUSH);
2090 
2091 		xas_lock_irq(&xas);
2092 
2093 		VM_BUG_ON_PAGE(page != xa_load(xas.xa, index), page);
2094 
2095 		/*
2096 		 * We control three references to the page:
2097 		 *  - we hold a pin on it;
2098 		 *  - one reference from page cache;
2099 		 *  - one from isolate_lru_page;
2100 		 * If those are the only references, then any new usage of the
2101 		 * page will have to fetch it from the page cache. That requires
2102 		 * locking the page to handle truncate, so any new usage will be
2103 		 * blocked until we unlock page after collapse/during rollback.
2104 		 */
2105 		if (page_count(page) != 3) {
2106 			result = SCAN_PAGE_COUNT;
2107 			xas_unlock_irq(&xas);
2108 			putback_lru_page(page);
2109 			goto out_unlock;
2110 		}
2111 
2112 		/*
2113 		 * Accumulate the pages that are being collapsed.
2114 		 */
2115 		list_add_tail(&page->lru, &pagelist);
2116 		continue;
2117 out_unlock:
2118 		unlock_page(page);
2119 		put_page(page);
2120 		goto xa_unlocked;
2121 	}
2122 
2123 	if (!is_shmem) {
2124 		filemap_nr_thps_inc(mapping);
2125 		/*
2126 		 * Paired with smp_mb() in do_dentry_open() to ensure
2127 		 * i_writecount is up to date and the update to nr_thps is
2128 		 * visible. Ensures the page cache will be truncated if the
2129 		 * file is opened writable.
2130 		 */
2131 		smp_mb();
2132 		if (inode_is_open_for_write(mapping->host)) {
2133 			result = SCAN_FAIL;
2134 			filemap_nr_thps_dec(mapping);
2135 		}
2136 	}
2137 
2138 xa_locked:
2139 	xas_unlock_irq(&xas);
2140 xa_unlocked:
2141 
2142 	/*
2143 	 * If collapse is successful, flush must be done now before copying.
2144 	 * If collapse is unsuccessful, does flush actually need to be done?
2145 	 * Do it anyway, to clear the state.
2146 	 */
2147 	try_to_unmap_flush();
2148 
2149 	if (result != SCAN_SUCCEED)
2150 		goto rollback;
2151 
2152 	/*
2153 	 * The old pages are locked, so they won't change anymore.
2154 	 */
2155 	index = start;
2156 	list_for_each_entry(page, &pagelist, lru) {
2157 		while (index < page->index) {
2158 			clear_highpage(hpage + (index % HPAGE_PMD_NR));
2159 			index++;
2160 		}
2161 		if (copy_mc_highpage(hpage + (page->index % HPAGE_PMD_NR), page) > 0) {
2162 			result = SCAN_COPY_MC;
2163 			goto rollback;
2164 		}
2165 		index++;
2166 	}
2167 	while (index < end) {
2168 		clear_highpage(hpage + (index % HPAGE_PMD_NR));
2169 		index++;
2170 	}
2171 
2172 	if (nr_none) {
2173 		struct vm_area_struct *vma;
2174 		int nr_none_check = 0;
2175 
2176 		i_mmap_lock_read(mapping);
2177 		xas_lock_irq(&xas);
2178 
2179 		xas_set(&xas, start);
2180 		for (index = start; index < end; index++) {
2181 			if (!xas_next(&xas)) {
2182 				xas_store(&xas, XA_RETRY_ENTRY);
2183 				if (xas_error(&xas)) {
2184 					result = SCAN_STORE_FAILED;
2185 					goto immap_locked;
2186 				}
2187 				nr_none_check++;
2188 			}
2189 		}
2190 
2191 		if (nr_none != nr_none_check) {
2192 			result = SCAN_PAGE_FILLED;
2193 			goto immap_locked;
2194 		}
2195 
2196 		/*
2197 		 * If userspace observed a missing page in a VMA with a MODE_MISSING
2198 		 * userfaultfd, then it might expect a UFFD_EVENT_PAGEFAULT for that
2199 		 * page. If so, we need to roll back to avoid suppressing such an
2200 		 * event. Since wp/minor userfaultfds don't give userspace any
2201 		 * guarantees that the kernel doesn't fill a missing page with a zero
2202 		 * page, so they don't matter here.
2203 		 *
2204 		 * Any userfaultfds registered after this point will not be able to
2205 		 * observe any missing pages due to the previously inserted retry
2206 		 * entries.
2207 		 */
2208 		vma_interval_tree_foreach(vma, &mapping->i_mmap, start, end) {
2209 			if (userfaultfd_missing(vma)) {
2210 				result = SCAN_EXCEED_NONE_PTE;
2211 				goto immap_locked;
2212 			}
2213 		}
2214 
2215 immap_locked:
2216 		i_mmap_unlock_read(mapping);
2217 		if (result != SCAN_SUCCEED) {
2218 			xas_set(&xas, start);
2219 			for (index = start; index < end; index++) {
2220 				if (xas_next(&xas) == XA_RETRY_ENTRY)
2221 					xas_store(&xas, NULL);
2222 			}
2223 
2224 			xas_unlock_irq(&xas);
2225 			goto rollback;
2226 		}
2227 	} else {
2228 		xas_lock_irq(&xas);
2229 	}
2230 
2231 	nr = thp_nr_pages(hpage);
2232 	if (is_shmem)
2233 		__mod_lruvec_page_state(hpage, NR_SHMEM_THPS, nr);
2234 	else
2235 		__mod_lruvec_page_state(hpage, NR_FILE_THPS, nr);
2236 
2237 	if (nr_none) {
2238 		__mod_lruvec_page_state(hpage, NR_FILE_PAGES, nr_none);
2239 		/* nr_none is always 0 for non-shmem. */
2240 		__mod_lruvec_page_state(hpage, NR_SHMEM, nr_none);
2241 	}
2242 
2243 	/*
2244 	 * Mark hpage as uptodate before inserting it into the page cache so
2245 	 * that it isn't mistaken for an fallocated but unwritten page.
2246 	 */
2247 	folio = page_folio(hpage);
2248 	folio_mark_uptodate(folio);
2249 	folio_ref_add(folio, HPAGE_PMD_NR - 1);
2250 
2251 	if (is_shmem)
2252 		folio_mark_dirty(folio);
2253 	folio_add_lru(folio);
2254 
2255 	/* Join all the small entries into a single multi-index entry. */
2256 	xas_set_order(&xas, start, HPAGE_PMD_ORDER);
2257 	xas_store(&xas, hpage);
2258 	WARN_ON_ONCE(xas_error(&xas));
2259 	xas_unlock_irq(&xas);
2260 
2261 	/*
2262 	 * Remove pte page tables, so we can re-fault the page as huge.
2263 	 */
2264 	result = retract_page_tables(mapping, start, mm, addr, hpage,
2265 				     cc);
2266 	unlock_page(hpage);
2267 
2268 	/*
2269 	 * The collapse has succeeded, so free the old pages.
2270 	 */
2271 	list_for_each_entry_safe(page, tmp, &pagelist, lru) {
2272 		list_del(&page->lru);
2273 		page->mapping = NULL;
2274 		ClearPageActive(page);
2275 		ClearPageUnevictable(page);
2276 		unlock_page(page);
2277 		folio_put_refs(page_folio(page), 3);
2278 	}
2279 
2280 	goto out;
2281 
2282 rollback:
2283 	/* Something went wrong: roll back page cache changes */
2284 	if (nr_none) {
2285 		xas_lock_irq(&xas);
2286 		mapping->nrpages -= nr_none;
2287 		shmem_uncharge(mapping->host, nr_none);
2288 		xas_unlock_irq(&xas);
2289 	}
2290 
2291 	list_for_each_entry_safe(page, tmp, &pagelist, lru) {
2292 		list_del(&page->lru);
2293 		unlock_page(page);
2294 		putback_lru_page(page);
2295 		put_page(page);
2296 	}
2297 	/*
2298 	 * Undo the updates of filemap_nr_thps_inc for non-SHMEM
2299 	 * file only. This undo is not needed unless failure is
2300 	 * due to SCAN_COPY_MC.
2301 	 */
2302 	if (!is_shmem && result == SCAN_COPY_MC) {
2303 		filemap_nr_thps_dec(mapping);
2304 		/*
2305 		 * Paired with smp_mb() in do_dentry_open() to
2306 		 * ensure the update to nr_thps is visible.
2307 		 */
2308 		smp_mb();
2309 	}
2310 
2311 	hpage->mapping = NULL;
2312 
2313 	unlock_page(hpage);
2314 	put_page(hpage);
2315 out:
2316 	VM_BUG_ON(!list_empty(&pagelist));
2317 	trace_mm_khugepaged_collapse_file(mm, hpage, index, is_shmem, addr, file, nr, result);
2318 	return result;
2319 }
2320 
2321 static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2322 				    struct file *file, pgoff_t start,
2323 				    struct collapse_control *cc)
2324 {
2325 	struct page *page = NULL;
2326 	struct address_space *mapping = file->f_mapping;
2327 	XA_STATE(xas, &mapping->i_pages, start);
2328 	int present, swap;
2329 	int node = NUMA_NO_NODE;
2330 	int result = SCAN_SUCCEED;
2331 
2332 	present = 0;
2333 	swap = 0;
2334 	memset(cc->node_load, 0, sizeof(cc->node_load));
2335 	nodes_clear(cc->alloc_nmask);
2336 	rcu_read_lock();
2337 	xas_for_each(&xas, page, start + HPAGE_PMD_NR - 1) {
2338 		if (xas_retry(&xas, page))
2339 			continue;
2340 
2341 		if (xa_is_value(page)) {
2342 			++swap;
2343 			if (cc->is_khugepaged &&
2344 			    swap > khugepaged_max_ptes_swap) {
2345 				result = SCAN_EXCEED_SWAP_PTE;
2346 				count_vm_event(THP_SCAN_EXCEED_SWAP_PTE);
2347 				break;
2348 			}
2349 			continue;
2350 		}
2351 
2352 		/*
2353 		 * TODO: khugepaged should compact smaller compound pages
2354 		 * into a PMD sized page
2355 		 */
2356 		if (PageTransCompound(page)) {
2357 			struct page *head = compound_head(page);
2358 
2359 			result = compound_order(head) == HPAGE_PMD_ORDER &&
2360 					head->index == start
2361 					/* Maybe PMD-mapped */
2362 					? SCAN_PTE_MAPPED_HUGEPAGE
2363 					: SCAN_PAGE_COMPOUND;
2364 			/*
2365 			 * For SCAN_PTE_MAPPED_HUGEPAGE, further processing
2366 			 * by the caller won't touch the page cache, and so
2367 			 * it's safe to skip LRU and refcount checks before
2368 			 * returning.
2369 			 */
2370 			break;
2371 		}
2372 
2373 		node = page_to_nid(page);
2374 		if (hpage_collapse_scan_abort(node, cc)) {
2375 			result = SCAN_SCAN_ABORT;
2376 			break;
2377 		}
2378 		cc->node_load[node]++;
2379 
2380 		if (!PageLRU(page)) {
2381 			result = SCAN_PAGE_LRU;
2382 			break;
2383 		}
2384 
2385 		if (page_count(page) !=
2386 		    1 + page_mapcount(page) + page_has_private(page)) {
2387 			result = SCAN_PAGE_COUNT;
2388 			break;
2389 		}
2390 
2391 		/*
2392 		 * We probably should check if the page is referenced here, but
2393 		 * nobody would transfer pte_young() to PageReferenced() for us.
2394 		 * And rmap walk here is just too costly...
2395 		 */
2396 
2397 		present++;
2398 
2399 		if (need_resched()) {
2400 			xas_pause(&xas);
2401 			cond_resched_rcu();
2402 		}
2403 	}
2404 	rcu_read_unlock();
2405 
2406 	if (result == SCAN_SUCCEED) {
2407 		if (cc->is_khugepaged &&
2408 		    present < HPAGE_PMD_NR - khugepaged_max_ptes_none) {
2409 			result = SCAN_EXCEED_NONE_PTE;
2410 			count_vm_event(THP_SCAN_EXCEED_NONE_PTE);
2411 		} else {
2412 			result = collapse_file(mm, addr, file, start, cc);
2413 		}
2414 	}
2415 
2416 	trace_mm_khugepaged_scan_file(mm, page, file, present, swap, result);
2417 	return result;
2418 }
2419 #else
2420 static int hpage_collapse_scan_file(struct mm_struct *mm, unsigned long addr,
2421 				    struct file *file, pgoff_t start,
2422 				    struct collapse_control *cc)
2423 {
2424 	BUILD_BUG();
2425 }
2426 
2427 static void khugepaged_collapse_pte_mapped_thps(struct khugepaged_mm_slot *mm_slot)
2428 {
2429 }
2430 
2431 static bool khugepaged_add_pte_mapped_thp(struct mm_struct *mm,
2432 					  unsigned long addr)
2433 {
2434 	return false;
2435 }
2436 #endif
2437 
2438 static unsigned int khugepaged_scan_mm_slot(unsigned int pages, int *result,
2439 					    struct collapse_control *cc)
2440 	__releases(&khugepaged_mm_lock)
2441 	__acquires(&khugepaged_mm_lock)
2442 {
2443 	struct vma_iterator vmi;
2444 	struct khugepaged_mm_slot *mm_slot;
2445 	struct mm_slot *slot;
2446 	struct mm_struct *mm;
2447 	struct vm_area_struct *vma;
2448 	int progress = 0;
2449 
2450 	VM_BUG_ON(!pages);
2451 	lockdep_assert_held(&khugepaged_mm_lock);
2452 	*result = SCAN_FAIL;
2453 
2454 	if (khugepaged_scan.mm_slot) {
2455 		mm_slot = khugepaged_scan.mm_slot;
2456 		slot = &mm_slot->slot;
2457 	} else {
2458 		slot = list_entry(khugepaged_scan.mm_head.next,
2459 				     struct mm_slot, mm_node);
2460 		mm_slot = mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
2461 		khugepaged_scan.address = 0;
2462 		khugepaged_scan.mm_slot = mm_slot;
2463 	}
2464 	spin_unlock(&khugepaged_mm_lock);
2465 	khugepaged_collapse_pte_mapped_thps(mm_slot);
2466 
2467 	mm = slot->mm;
2468 	/*
2469 	 * Don't wait for semaphore (to avoid long wait times).  Just move to
2470 	 * the next mm on the list.
2471 	 */
2472 	vma = NULL;
2473 	if (unlikely(!mmap_read_trylock(mm)))
2474 		goto breakouterloop_mmap_lock;
2475 
2476 	progress++;
2477 	if (unlikely(hpage_collapse_test_exit(mm)))
2478 		goto breakouterloop;
2479 
2480 	vma_iter_init(&vmi, mm, khugepaged_scan.address);
2481 	for_each_vma(vmi, vma) {
2482 		unsigned long hstart, hend;
2483 
2484 		cond_resched();
2485 		if (unlikely(hpage_collapse_test_exit(mm))) {
2486 			progress++;
2487 			break;
2488 		}
2489 		if (!hugepage_vma_check(vma, vma->vm_flags, false, false, true)) {
2490 skip:
2491 			progress++;
2492 			continue;
2493 		}
2494 		hstart = round_up(vma->vm_start, HPAGE_PMD_SIZE);
2495 		hend = round_down(vma->vm_end, HPAGE_PMD_SIZE);
2496 		if (khugepaged_scan.address > hend)
2497 			goto skip;
2498 		if (khugepaged_scan.address < hstart)
2499 			khugepaged_scan.address = hstart;
2500 		VM_BUG_ON(khugepaged_scan.address & ~HPAGE_PMD_MASK);
2501 
2502 		while (khugepaged_scan.address < hend) {
2503 			bool mmap_locked = true;
2504 
2505 			cond_resched();
2506 			if (unlikely(hpage_collapse_test_exit(mm)))
2507 				goto breakouterloop;
2508 
2509 			VM_BUG_ON(khugepaged_scan.address < hstart ||
2510 				  khugepaged_scan.address + HPAGE_PMD_SIZE >
2511 				  hend);
2512 			if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
2513 				struct file *file = get_file(vma->vm_file);
2514 				pgoff_t pgoff = linear_page_index(vma,
2515 						khugepaged_scan.address);
2516 
2517 				mmap_read_unlock(mm);
2518 				*result = hpage_collapse_scan_file(mm,
2519 								   khugepaged_scan.address,
2520 								   file, pgoff, cc);
2521 				mmap_locked = false;
2522 				fput(file);
2523 			} else {
2524 				*result = hpage_collapse_scan_pmd(mm, vma,
2525 								  khugepaged_scan.address,
2526 								  &mmap_locked,
2527 								  cc);
2528 			}
2529 			switch (*result) {
2530 			case SCAN_PTE_MAPPED_HUGEPAGE: {
2531 				pmd_t *pmd;
2532 
2533 				*result = find_pmd_or_thp_or_none(mm,
2534 								  khugepaged_scan.address,
2535 								  &pmd);
2536 				if (*result != SCAN_SUCCEED)
2537 					break;
2538 				if (!khugepaged_add_pte_mapped_thp(mm,
2539 								   khugepaged_scan.address))
2540 					break;
2541 			} fallthrough;
2542 			case SCAN_SUCCEED:
2543 				++khugepaged_pages_collapsed;
2544 				break;
2545 			default:
2546 				break;
2547 			}
2548 
2549 			/* move to next address */
2550 			khugepaged_scan.address += HPAGE_PMD_SIZE;
2551 			progress += HPAGE_PMD_NR;
2552 			if (!mmap_locked)
2553 				/*
2554 				 * We released mmap_lock so break loop.  Note
2555 				 * that we drop mmap_lock before all hugepage
2556 				 * allocations, so if allocation fails, we are
2557 				 * guaranteed to break here and report the
2558 				 * correct result back to caller.
2559 				 */
2560 				goto breakouterloop_mmap_lock;
2561 			if (progress >= pages)
2562 				goto breakouterloop;
2563 		}
2564 	}
2565 breakouterloop:
2566 	mmap_read_unlock(mm); /* exit_mmap will destroy ptes after this */
2567 breakouterloop_mmap_lock:
2568 
2569 	spin_lock(&khugepaged_mm_lock);
2570 	VM_BUG_ON(khugepaged_scan.mm_slot != mm_slot);
2571 	/*
2572 	 * Release the current mm_slot if this mm is about to die, or
2573 	 * if we scanned all vmas of this mm.
2574 	 */
2575 	if (hpage_collapse_test_exit(mm) || !vma) {
2576 		/*
2577 		 * Make sure that if mm_users is reaching zero while
2578 		 * khugepaged runs here, khugepaged_exit will find
2579 		 * mm_slot not pointing to the exiting mm.
2580 		 */
2581 		if (slot->mm_node.next != &khugepaged_scan.mm_head) {
2582 			slot = list_entry(slot->mm_node.next,
2583 					  struct mm_slot, mm_node);
2584 			khugepaged_scan.mm_slot =
2585 				mm_slot_entry(slot, struct khugepaged_mm_slot, slot);
2586 			khugepaged_scan.address = 0;
2587 		} else {
2588 			khugepaged_scan.mm_slot = NULL;
2589 			khugepaged_full_scans++;
2590 		}
2591 
2592 		collect_mm_slot(mm_slot);
2593 	}
2594 
2595 	return progress;
2596 }
2597 
2598 static int khugepaged_has_work(void)
2599 {
2600 	return !list_empty(&khugepaged_scan.mm_head) &&
2601 		hugepage_flags_enabled();
2602 }
2603 
2604 static int khugepaged_wait_event(void)
2605 {
2606 	return !list_empty(&khugepaged_scan.mm_head) ||
2607 		kthread_should_stop();
2608 }
2609 
2610 static void khugepaged_do_scan(struct collapse_control *cc)
2611 {
2612 	unsigned int progress = 0, pass_through_head = 0;
2613 	unsigned int pages = READ_ONCE(khugepaged_pages_to_scan);
2614 	bool wait = true;
2615 	int result = SCAN_SUCCEED;
2616 
2617 	lru_add_drain_all();
2618 
2619 	while (true) {
2620 		cond_resched();
2621 
2622 		if (unlikely(kthread_should_stop() || try_to_freeze()))
2623 			break;
2624 
2625 		spin_lock(&khugepaged_mm_lock);
2626 		if (!khugepaged_scan.mm_slot)
2627 			pass_through_head++;
2628 		if (khugepaged_has_work() &&
2629 		    pass_through_head < 2)
2630 			progress += khugepaged_scan_mm_slot(pages - progress,
2631 							    &result, cc);
2632 		else
2633 			progress = pages;
2634 		spin_unlock(&khugepaged_mm_lock);
2635 
2636 		if (progress >= pages)
2637 			break;
2638 
2639 		if (result == SCAN_ALLOC_HUGE_PAGE_FAIL) {
2640 			/*
2641 			 * If fail to allocate the first time, try to sleep for
2642 			 * a while.  When hit again, cancel the scan.
2643 			 */
2644 			if (!wait)
2645 				break;
2646 			wait = false;
2647 			khugepaged_alloc_sleep();
2648 		}
2649 	}
2650 }
2651 
2652 static bool khugepaged_should_wakeup(void)
2653 {
2654 	return kthread_should_stop() ||
2655 	       time_after_eq(jiffies, khugepaged_sleep_expire);
2656 }
2657 
2658 static void khugepaged_wait_work(void)
2659 {
2660 	if (khugepaged_has_work()) {
2661 		const unsigned long scan_sleep_jiffies =
2662 			msecs_to_jiffies(khugepaged_scan_sleep_millisecs);
2663 
2664 		if (!scan_sleep_jiffies)
2665 			return;
2666 
2667 		khugepaged_sleep_expire = jiffies + scan_sleep_jiffies;
2668 		wait_event_freezable_timeout(khugepaged_wait,
2669 					     khugepaged_should_wakeup(),
2670 					     scan_sleep_jiffies);
2671 		return;
2672 	}
2673 
2674 	if (hugepage_flags_enabled())
2675 		wait_event_freezable(khugepaged_wait, khugepaged_wait_event());
2676 }
2677 
2678 static int khugepaged(void *none)
2679 {
2680 	struct khugepaged_mm_slot *mm_slot;
2681 
2682 	set_freezable();
2683 	set_user_nice(current, MAX_NICE);
2684 
2685 	while (!kthread_should_stop()) {
2686 		khugepaged_do_scan(&khugepaged_collapse_control);
2687 		khugepaged_wait_work();
2688 	}
2689 
2690 	spin_lock(&khugepaged_mm_lock);
2691 	mm_slot = khugepaged_scan.mm_slot;
2692 	khugepaged_scan.mm_slot = NULL;
2693 	if (mm_slot)
2694 		collect_mm_slot(mm_slot);
2695 	spin_unlock(&khugepaged_mm_lock);
2696 	return 0;
2697 }
2698 
2699 static void set_recommended_min_free_kbytes(void)
2700 {
2701 	struct zone *zone;
2702 	int nr_zones = 0;
2703 	unsigned long recommended_min;
2704 
2705 	if (!hugepage_flags_enabled()) {
2706 		calculate_min_free_kbytes();
2707 		goto update_wmarks;
2708 	}
2709 
2710 	for_each_populated_zone(zone) {
2711 		/*
2712 		 * We don't need to worry about fragmentation of
2713 		 * ZONE_MOVABLE since it only has movable pages.
2714 		 */
2715 		if (zone_idx(zone) > gfp_zone(GFP_USER))
2716 			continue;
2717 
2718 		nr_zones++;
2719 	}
2720 
2721 	/* Ensure 2 pageblocks are free to assist fragmentation avoidance */
2722 	recommended_min = pageblock_nr_pages * nr_zones * 2;
2723 
2724 	/*
2725 	 * Make sure that on average at least two pageblocks are almost free
2726 	 * of another type, one for a migratetype to fall back to and a
2727 	 * second to avoid subsequent fallbacks of other types There are 3
2728 	 * MIGRATE_TYPES we care about.
2729 	 */
2730 	recommended_min += pageblock_nr_pages * nr_zones *
2731 			   MIGRATE_PCPTYPES * MIGRATE_PCPTYPES;
2732 
2733 	/* don't ever allow to reserve more than 5% of the lowmem */
2734 	recommended_min = min(recommended_min,
2735 			      (unsigned long) nr_free_buffer_pages() / 20);
2736 	recommended_min <<= (PAGE_SHIFT-10);
2737 
2738 	if (recommended_min > min_free_kbytes) {
2739 		if (user_min_free_kbytes >= 0)
2740 			pr_info("raising min_free_kbytes from %d to %lu to help transparent hugepage allocations\n",
2741 				min_free_kbytes, recommended_min);
2742 
2743 		min_free_kbytes = recommended_min;
2744 	}
2745 
2746 update_wmarks:
2747 	setup_per_zone_wmarks();
2748 }
2749 
2750 int start_stop_khugepaged(void)
2751 {
2752 	int err = 0;
2753 
2754 	mutex_lock(&khugepaged_mutex);
2755 	if (hugepage_flags_enabled()) {
2756 		if (!khugepaged_thread)
2757 			khugepaged_thread = kthread_run(khugepaged, NULL,
2758 							"khugepaged");
2759 		if (IS_ERR(khugepaged_thread)) {
2760 			pr_err("khugepaged: kthread_run(khugepaged) failed\n");
2761 			err = PTR_ERR(khugepaged_thread);
2762 			khugepaged_thread = NULL;
2763 			goto fail;
2764 		}
2765 
2766 		if (!list_empty(&khugepaged_scan.mm_head))
2767 			wake_up_interruptible(&khugepaged_wait);
2768 	} else if (khugepaged_thread) {
2769 		kthread_stop(khugepaged_thread);
2770 		khugepaged_thread = NULL;
2771 	}
2772 	set_recommended_min_free_kbytes();
2773 fail:
2774 	mutex_unlock(&khugepaged_mutex);
2775 	return err;
2776 }
2777 
2778 void khugepaged_min_free_kbytes_update(void)
2779 {
2780 	mutex_lock(&khugepaged_mutex);
2781 	if (hugepage_flags_enabled() && khugepaged_thread)
2782 		set_recommended_min_free_kbytes();
2783 	mutex_unlock(&khugepaged_mutex);
2784 }
2785 
2786 bool current_is_khugepaged(void)
2787 {
2788 	return kthread_func(current) == khugepaged;
2789 }
2790 
2791 static int madvise_collapse_errno(enum scan_result r)
2792 {
2793 	/*
2794 	 * MADV_COLLAPSE breaks from existing madvise(2) conventions to provide
2795 	 * actionable feedback to caller, so they may take an appropriate
2796 	 * fallback measure depending on the nature of the failure.
2797 	 */
2798 	switch (r) {
2799 	case SCAN_ALLOC_HUGE_PAGE_FAIL:
2800 		return -ENOMEM;
2801 	case SCAN_CGROUP_CHARGE_FAIL:
2802 	case SCAN_EXCEED_NONE_PTE:
2803 		return -EBUSY;
2804 	/* Resource temporary unavailable - trying again might succeed */
2805 	case SCAN_PAGE_COUNT:
2806 	case SCAN_PAGE_LOCK:
2807 	case SCAN_PAGE_LRU:
2808 	case SCAN_DEL_PAGE_LRU:
2809 	case SCAN_PAGE_FILLED:
2810 		return -EAGAIN;
2811 	/*
2812 	 * Other: Trying again likely not to succeed / error intrinsic to
2813 	 * specified memory range. khugepaged likely won't be able to collapse
2814 	 * either.
2815 	 */
2816 	default:
2817 		return -EINVAL;
2818 	}
2819 }
2820 
2821 int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev,
2822 		     unsigned long start, unsigned long end)
2823 {
2824 	struct collapse_control *cc;
2825 	struct mm_struct *mm = vma->vm_mm;
2826 	unsigned long hstart, hend, addr;
2827 	int thps = 0, last_fail = SCAN_FAIL;
2828 	bool mmap_locked = true;
2829 
2830 	BUG_ON(vma->vm_start > start);
2831 	BUG_ON(vma->vm_end < end);
2832 
2833 	*prev = vma;
2834 
2835 	if (!hugepage_vma_check(vma, vma->vm_flags, false, false, false))
2836 		return -EINVAL;
2837 
2838 	cc = kmalloc(sizeof(*cc), GFP_KERNEL);
2839 	if (!cc)
2840 		return -ENOMEM;
2841 	cc->is_khugepaged = false;
2842 
2843 	mmgrab(mm);
2844 	lru_add_drain_all();
2845 
2846 	hstart = (start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
2847 	hend = end & HPAGE_PMD_MASK;
2848 
2849 	for (addr = hstart; addr < hend; addr += HPAGE_PMD_SIZE) {
2850 		int result = SCAN_FAIL;
2851 
2852 		if (!mmap_locked) {
2853 			cond_resched();
2854 			mmap_read_lock(mm);
2855 			mmap_locked = true;
2856 			result = hugepage_vma_revalidate(mm, addr, false, &vma,
2857 							 cc);
2858 			if (result  != SCAN_SUCCEED) {
2859 				last_fail = result;
2860 				goto out_nolock;
2861 			}
2862 
2863 			hend = min(hend, vma->vm_end & HPAGE_PMD_MASK);
2864 		}
2865 		mmap_assert_locked(mm);
2866 		memset(cc->node_load, 0, sizeof(cc->node_load));
2867 		nodes_clear(cc->alloc_nmask);
2868 		if (IS_ENABLED(CONFIG_SHMEM) && vma->vm_file) {
2869 			struct file *file = get_file(vma->vm_file);
2870 			pgoff_t pgoff = linear_page_index(vma, addr);
2871 
2872 			mmap_read_unlock(mm);
2873 			mmap_locked = false;
2874 			result = hpage_collapse_scan_file(mm, addr, file, pgoff,
2875 							  cc);
2876 			fput(file);
2877 		} else {
2878 			result = hpage_collapse_scan_pmd(mm, vma, addr,
2879 							 &mmap_locked, cc);
2880 		}
2881 		if (!mmap_locked)
2882 			*prev = NULL;  /* Tell caller we dropped mmap_lock */
2883 
2884 handle_result:
2885 		switch (result) {
2886 		case SCAN_SUCCEED:
2887 		case SCAN_PMD_MAPPED:
2888 			++thps;
2889 			break;
2890 		case SCAN_PTE_MAPPED_HUGEPAGE:
2891 			BUG_ON(mmap_locked);
2892 			BUG_ON(*prev);
2893 			mmap_write_lock(mm);
2894 			result = collapse_pte_mapped_thp(mm, addr, true);
2895 			mmap_write_unlock(mm);
2896 			goto handle_result;
2897 		/* Whitelisted set of results where continuing OK */
2898 		case SCAN_PMD_NULL:
2899 		case SCAN_PTE_NON_PRESENT:
2900 		case SCAN_PTE_UFFD_WP:
2901 		case SCAN_PAGE_RO:
2902 		case SCAN_LACK_REFERENCED_PAGE:
2903 		case SCAN_PAGE_NULL:
2904 		case SCAN_PAGE_COUNT:
2905 		case SCAN_PAGE_LOCK:
2906 		case SCAN_PAGE_COMPOUND:
2907 		case SCAN_PAGE_LRU:
2908 		case SCAN_DEL_PAGE_LRU:
2909 			last_fail = result;
2910 			break;
2911 		default:
2912 			last_fail = result;
2913 			/* Other error, exit */
2914 			goto out_maybelock;
2915 		}
2916 	}
2917 
2918 out_maybelock:
2919 	/* Caller expects us to hold mmap_lock on return */
2920 	if (!mmap_locked)
2921 		mmap_read_lock(mm);
2922 out_nolock:
2923 	mmap_assert_locked(mm);
2924 	mmdrop(mm);
2925 	kfree(cc);
2926 
2927 	return thps == ((hend - hstart) >> HPAGE_PMD_SHIFT) ? 0
2928 			: madvise_collapse_errno(last_fail);
2929 }
2930