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