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