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